deprecate isl_map_n_*
[isl.git] / isl_aff.c
blob79bbb2d462f50b5c2b80e781332ba4a8368fc377
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 reordering.
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 isl_bool equal_params;
465 if (!aff || !model)
466 goto error;
468 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
469 if (equal_params < 0)
470 goto error;
471 if (!equal_params) {
472 isl_reordering *exp;
474 model = isl_space_drop_dims(model, isl_dim_in,
475 0, isl_space_dim(model, isl_dim_in));
476 model = isl_space_drop_dims(model, isl_dim_out,
477 0, isl_space_dim(model, isl_dim_out));
478 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
479 exp = isl_reordering_extend_space(exp,
480 isl_aff_get_domain_space(aff));
481 aff = isl_aff_realign_domain(aff, exp);
484 isl_space_free(model);
485 return aff;
486 error:
487 isl_space_free(model);
488 isl_aff_free(aff);
489 return NULL;
492 /* Is "aff" obviously equal to zero?
494 * If the denominator is zero, then "aff" is not equal to zero.
496 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
498 if (!aff)
499 return isl_bool_error;
501 if (isl_int_is_zero(aff->v->el[0]))
502 return isl_bool_false;
503 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
506 /* Does "aff" represent NaN?
508 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
510 if (!aff)
511 return isl_bool_error;
513 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
516 /* Are "aff1" and "aff2" obviously equal?
518 * NaN is not equal to anything, not even to another NaN.
520 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
521 __isl_keep isl_aff *aff2)
523 isl_bool equal;
525 if (!aff1 || !aff2)
526 return isl_bool_error;
528 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
529 return isl_bool_false;
531 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
532 if (equal < 0 || !equal)
533 return equal;
535 return isl_vec_is_equal(aff1->v, aff2->v);
538 /* Return the common denominator of "aff" in "v".
540 * We cannot return anything meaningful in case of a NaN.
542 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
544 if (!aff)
545 return isl_stat_error;
546 if (isl_aff_is_nan(aff))
547 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
548 "cannot get denominator of NaN", return isl_stat_error);
549 isl_int_set(*v, aff->v->el[0]);
550 return isl_stat_ok;
553 /* Return the common denominator of "aff".
555 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
557 isl_ctx *ctx;
559 if (!aff)
560 return NULL;
562 ctx = isl_aff_get_ctx(aff);
563 if (isl_aff_is_nan(aff))
564 return isl_val_nan(ctx);
565 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
568 /* Return the constant term of "aff" in "v".
570 * We cannot return anything meaningful in case of a NaN.
572 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
574 if (!aff)
575 return -1;
576 if (isl_aff_is_nan(aff))
577 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
578 "cannot get constant term of NaN", return -1);
579 isl_int_set(*v, aff->v->el[1]);
580 return 0;
583 /* Return the constant term of "aff".
585 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
587 isl_ctx *ctx;
588 isl_val *v;
590 if (!aff)
591 return NULL;
593 ctx = isl_aff_get_ctx(aff);
594 if (isl_aff_is_nan(aff))
595 return isl_val_nan(ctx);
596 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
597 return isl_val_normalize(v);
600 /* Return the coefficient of the variable of type "type" at position "pos"
601 * of "aff" in "v".
603 * We cannot return anything meaningful in case of a NaN.
605 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
606 enum isl_dim_type type, int pos, isl_int *v)
608 if (!aff)
609 return -1;
611 if (type == isl_dim_out)
612 isl_die(aff->v->ctx, isl_error_invalid,
613 "output/set dimension does not have a coefficient",
614 return -1);
615 if (type == isl_dim_in)
616 type = isl_dim_set;
618 if (pos >= isl_local_space_dim(aff->ls, type))
619 isl_die(aff->v->ctx, isl_error_invalid,
620 "position out of bounds", return -1);
622 if (isl_aff_is_nan(aff))
623 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
624 "cannot get coefficient of NaN", return -1);
625 pos += isl_local_space_offset(aff->ls, type);
626 isl_int_set(*v, aff->v->el[1 + pos]);
628 return 0;
631 /* Return the coefficient of the variable of type "type" at position "pos"
632 * of "aff".
634 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
635 enum isl_dim_type type, int pos)
637 isl_ctx *ctx;
638 isl_val *v;
640 if (!aff)
641 return NULL;
643 ctx = isl_aff_get_ctx(aff);
644 if (type == isl_dim_out)
645 isl_die(ctx, isl_error_invalid,
646 "output/set dimension does not have a coefficient",
647 return NULL);
648 if (type == isl_dim_in)
649 type = isl_dim_set;
651 if (pos >= isl_local_space_dim(aff->ls, type))
652 isl_die(ctx, isl_error_invalid,
653 "position out of bounds", return NULL);
655 if (isl_aff_is_nan(aff))
656 return isl_val_nan(ctx);
657 pos += isl_local_space_offset(aff->ls, type);
658 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
659 return isl_val_normalize(v);
662 /* Return the sign of the coefficient of the variable of type "type"
663 * at position "pos" of "aff".
665 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
666 int pos)
668 isl_ctx *ctx;
670 if (!aff)
671 return 0;
673 ctx = isl_aff_get_ctx(aff);
674 if (type == isl_dim_out)
675 isl_die(ctx, isl_error_invalid,
676 "output/set dimension does not have a coefficient",
677 return 0);
678 if (type == isl_dim_in)
679 type = isl_dim_set;
681 if (pos >= isl_local_space_dim(aff->ls, type))
682 isl_die(ctx, isl_error_invalid,
683 "position out of bounds", return 0);
685 pos += isl_local_space_offset(aff->ls, type);
686 return isl_int_sgn(aff->v->el[1 + pos]);
689 /* Replace the denominator of "aff" by "v".
691 * A NaN is unaffected by this operation.
693 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
695 if (!aff)
696 return NULL;
697 if (isl_aff_is_nan(aff))
698 return aff;
699 aff = isl_aff_cow(aff);
700 if (!aff)
701 return NULL;
703 aff->v = isl_vec_cow(aff->v);
704 if (!aff->v)
705 return isl_aff_free(aff);
707 isl_int_set(aff->v->el[0], v);
709 return aff;
712 /* Replace the numerator of the constant term of "aff" by "v".
714 * A NaN is unaffected by this operation.
716 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
718 if (!aff)
719 return NULL;
720 if (isl_aff_is_nan(aff))
721 return aff;
722 aff = isl_aff_cow(aff);
723 if (!aff)
724 return NULL;
726 aff->v = isl_vec_cow(aff->v);
727 if (!aff->v)
728 return isl_aff_free(aff);
730 isl_int_set(aff->v->el[1], v);
732 return aff;
735 /* Replace the constant term of "aff" by "v".
737 * A NaN is unaffected by this operation.
739 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
740 __isl_take isl_val *v)
742 if (!aff || !v)
743 goto error;
745 if (isl_aff_is_nan(aff)) {
746 isl_val_free(v);
747 return aff;
750 if (!isl_val_is_rat(v))
751 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
752 "expecting rational value", goto error);
754 if (isl_int_eq(aff->v->el[1], v->n) &&
755 isl_int_eq(aff->v->el[0], v->d)) {
756 isl_val_free(v);
757 return aff;
760 aff = isl_aff_cow(aff);
761 if (!aff)
762 goto error;
763 aff->v = isl_vec_cow(aff->v);
764 if (!aff->v)
765 goto error;
767 if (isl_int_eq(aff->v->el[0], v->d)) {
768 isl_int_set(aff->v->el[1], v->n);
769 } else if (isl_int_is_one(v->d)) {
770 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
771 } else {
772 isl_seq_scale(aff->v->el + 1,
773 aff->v->el + 1, v->d, aff->v->size - 1);
774 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
775 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
776 aff->v = isl_vec_normalize(aff->v);
777 if (!aff->v)
778 goto error;
781 isl_val_free(v);
782 return aff;
783 error:
784 isl_aff_free(aff);
785 isl_val_free(v);
786 return NULL;
789 /* Add "v" to the constant term of "aff".
791 * A NaN is unaffected by this operation.
793 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
795 if (isl_int_is_zero(v))
796 return aff;
798 if (!aff)
799 return NULL;
800 if (isl_aff_is_nan(aff))
801 return aff;
802 aff = isl_aff_cow(aff);
803 if (!aff)
804 return NULL;
806 aff->v = isl_vec_cow(aff->v);
807 if (!aff->v)
808 return isl_aff_free(aff);
810 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
812 return aff;
815 /* Add "v" to the constant term of "aff".
817 * A NaN is unaffected by this operation.
819 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
820 __isl_take isl_val *v)
822 if (!aff || !v)
823 goto error;
825 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
826 isl_val_free(v);
827 return aff;
830 if (!isl_val_is_rat(v))
831 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
832 "expecting rational value", goto error);
834 aff = isl_aff_cow(aff);
835 if (!aff)
836 goto error;
838 aff->v = isl_vec_cow(aff->v);
839 if (!aff->v)
840 goto error;
842 if (isl_int_is_one(v->d)) {
843 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
844 } else if (isl_int_eq(aff->v->el[0], v->d)) {
845 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
846 aff->v = isl_vec_normalize(aff->v);
847 if (!aff->v)
848 goto error;
849 } else {
850 isl_seq_scale(aff->v->el + 1,
851 aff->v->el + 1, v->d, aff->v->size - 1);
852 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
853 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
854 aff->v = isl_vec_normalize(aff->v);
855 if (!aff->v)
856 goto error;
859 isl_val_free(v);
860 return aff;
861 error:
862 isl_aff_free(aff);
863 isl_val_free(v);
864 return NULL;
867 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
869 isl_int t;
871 isl_int_init(t);
872 isl_int_set_si(t, v);
873 aff = isl_aff_add_constant(aff, t);
874 isl_int_clear(t);
876 return aff;
879 /* Add "v" to the numerator of the constant term of "aff".
881 * A NaN is unaffected by this operation.
883 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
885 if (isl_int_is_zero(v))
886 return aff;
888 if (!aff)
889 return NULL;
890 if (isl_aff_is_nan(aff))
891 return aff;
892 aff = isl_aff_cow(aff);
893 if (!aff)
894 return NULL;
896 aff->v = isl_vec_cow(aff->v);
897 if (!aff->v)
898 return isl_aff_free(aff);
900 isl_int_add(aff->v->el[1], aff->v->el[1], v);
902 return aff;
905 /* Add "v" to the numerator of the constant term of "aff".
907 * A NaN is unaffected by this operation.
909 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
911 isl_int t;
913 if (v == 0)
914 return aff;
916 isl_int_init(t);
917 isl_int_set_si(t, v);
918 aff = isl_aff_add_constant_num(aff, t);
919 isl_int_clear(t);
921 return aff;
924 /* Replace the numerator of the constant term of "aff" by "v".
926 * A NaN is unaffected by this operation.
928 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
930 if (!aff)
931 return NULL;
932 if (isl_aff_is_nan(aff))
933 return aff;
934 aff = isl_aff_cow(aff);
935 if (!aff)
936 return NULL;
938 aff->v = isl_vec_cow(aff->v);
939 if (!aff->v)
940 return isl_aff_free(aff);
942 isl_int_set_si(aff->v->el[1], v);
944 return aff;
947 /* Replace the numerator of the coefficient of the variable of type "type"
948 * at position "pos" of "aff" by "v".
950 * A NaN is unaffected by this operation.
952 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
953 enum isl_dim_type type, int pos, isl_int v)
955 if (!aff)
956 return NULL;
958 if (type == isl_dim_out)
959 isl_die(aff->v->ctx, isl_error_invalid,
960 "output/set dimension does not have a coefficient",
961 return isl_aff_free(aff));
962 if (type == isl_dim_in)
963 type = isl_dim_set;
965 if (pos >= isl_local_space_dim(aff->ls, type))
966 isl_die(aff->v->ctx, isl_error_invalid,
967 "position out of bounds", return isl_aff_free(aff));
969 if (isl_aff_is_nan(aff))
970 return aff;
971 aff = isl_aff_cow(aff);
972 if (!aff)
973 return NULL;
975 aff->v = isl_vec_cow(aff->v);
976 if (!aff->v)
977 return isl_aff_free(aff);
979 pos += isl_local_space_offset(aff->ls, type);
980 isl_int_set(aff->v->el[1 + pos], v);
982 return aff;
985 /* Replace the numerator of the coefficient of the variable of type "type"
986 * at position "pos" of "aff" by "v".
988 * A NaN is unaffected by this operation.
990 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
991 enum isl_dim_type type, int pos, int v)
993 if (!aff)
994 return NULL;
996 if (type == isl_dim_out)
997 isl_die(aff->v->ctx, isl_error_invalid,
998 "output/set dimension does not have a coefficient",
999 return isl_aff_free(aff));
1000 if (type == isl_dim_in)
1001 type = isl_dim_set;
1003 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1004 isl_die(aff->v->ctx, isl_error_invalid,
1005 "position out of bounds", return isl_aff_free(aff));
1007 if (isl_aff_is_nan(aff))
1008 return aff;
1009 pos += isl_local_space_offset(aff->ls, type);
1010 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1011 return aff;
1013 aff = isl_aff_cow(aff);
1014 if (!aff)
1015 return NULL;
1017 aff->v = isl_vec_cow(aff->v);
1018 if (!aff->v)
1019 return isl_aff_free(aff);
1021 isl_int_set_si(aff->v->el[1 + pos], v);
1023 return aff;
1026 /* Replace the coefficient of the variable of type "type" at position "pos"
1027 * of "aff" by "v".
1029 * A NaN is unaffected by this operation.
1031 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1032 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1034 if (!aff || !v)
1035 goto error;
1037 if (type == isl_dim_out)
1038 isl_die(aff->v->ctx, isl_error_invalid,
1039 "output/set dimension does not have a coefficient",
1040 goto error);
1041 if (type == isl_dim_in)
1042 type = isl_dim_set;
1044 if (pos >= isl_local_space_dim(aff->ls, type))
1045 isl_die(aff->v->ctx, isl_error_invalid,
1046 "position out of bounds", goto error);
1048 if (isl_aff_is_nan(aff)) {
1049 isl_val_free(v);
1050 return aff;
1052 if (!isl_val_is_rat(v))
1053 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1054 "expecting rational value", goto error);
1056 pos += isl_local_space_offset(aff->ls, type);
1057 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1058 isl_int_eq(aff->v->el[0], v->d)) {
1059 isl_val_free(v);
1060 return aff;
1063 aff = isl_aff_cow(aff);
1064 if (!aff)
1065 goto error;
1066 aff->v = isl_vec_cow(aff->v);
1067 if (!aff->v)
1068 goto error;
1070 if (isl_int_eq(aff->v->el[0], v->d)) {
1071 isl_int_set(aff->v->el[1 + pos], v->n);
1072 } else if (isl_int_is_one(v->d)) {
1073 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1074 } else {
1075 isl_seq_scale(aff->v->el + 1,
1076 aff->v->el + 1, v->d, aff->v->size - 1);
1077 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1078 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1079 aff->v = isl_vec_normalize(aff->v);
1080 if (!aff->v)
1081 goto error;
1084 isl_val_free(v);
1085 return aff;
1086 error:
1087 isl_aff_free(aff);
1088 isl_val_free(v);
1089 return NULL;
1092 /* Add "v" to the coefficient of the variable of type "type"
1093 * at position "pos" of "aff".
1095 * A NaN is unaffected by this operation.
1097 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1098 enum isl_dim_type type, int pos, isl_int v)
1100 if (!aff)
1101 return NULL;
1103 if (type == isl_dim_out)
1104 isl_die(aff->v->ctx, isl_error_invalid,
1105 "output/set dimension does not have a coefficient",
1106 return isl_aff_free(aff));
1107 if (type == isl_dim_in)
1108 type = isl_dim_set;
1110 if (pos >= isl_local_space_dim(aff->ls, type))
1111 isl_die(aff->v->ctx, isl_error_invalid,
1112 "position out of bounds", return isl_aff_free(aff));
1114 if (isl_aff_is_nan(aff))
1115 return aff;
1116 aff = isl_aff_cow(aff);
1117 if (!aff)
1118 return NULL;
1120 aff->v = isl_vec_cow(aff->v);
1121 if (!aff->v)
1122 return isl_aff_free(aff);
1124 pos += isl_local_space_offset(aff->ls, type);
1125 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1127 return aff;
1130 /* Add "v" to the coefficient of the variable of type "type"
1131 * at position "pos" of "aff".
1133 * A NaN is unaffected by this operation.
1135 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1136 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1138 if (!aff || !v)
1139 goto error;
1141 if (isl_val_is_zero(v)) {
1142 isl_val_free(v);
1143 return aff;
1146 if (type == isl_dim_out)
1147 isl_die(aff->v->ctx, isl_error_invalid,
1148 "output/set dimension does not have a coefficient",
1149 goto error);
1150 if (type == isl_dim_in)
1151 type = isl_dim_set;
1153 if (pos >= isl_local_space_dim(aff->ls, type))
1154 isl_die(aff->v->ctx, isl_error_invalid,
1155 "position out of bounds", goto error);
1157 if (isl_aff_is_nan(aff)) {
1158 isl_val_free(v);
1159 return aff;
1161 if (!isl_val_is_rat(v))
1162 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1163 "expecting rational value", goto error);
1165 aff = isl_aff_cow(aff);
1166 if (!aff)
1167 goto error;
1169 aff->v = isl_vec_cow(aff->v);
1170 if (!aff->v)
1171 goto error;
1173 pos += isl_local_space_offset(aff->ls, type);
1174 if (isl_int_is_one(v->d)) {
1175 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1176 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1177 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1178 aff->v = isl_vec_normalize(aff->v);
1179 if (!aff->v)
1180 goto error;
1181 } else {
1182 isl_seq_scale(aff->v->el + 1,
1183 aff->v->el + 1, v->d, aff->v->size - 1);
1184 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1185 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1186 aff->v = isl_vec_normalize(aff->v);
1187 if (!aff->v)
1188 goto error;
1191 isl_val_free(v);
1192 return aff;
1193 error:
1194 isl_aff_free(aff);
1195 isl_val_free(v);
1196 return NULL;
1199 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1200 enum isl_dim_type type, int pos, int v)
1202 isl_int t;
1204 isl_int_init(t);
1205 isl_int_set_si(t, v);
1206 aff = isl_aff_add_coefficient(aff, type, pos, t);
1207 isl_int_clear(t);
1209 return aff;
1212 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1214 if (!aff)
1215 return NULL;
1217 return isl_local_space_get_div(aff->ls, pos);
1220 /* Return the negation of "aff".
1222 * As a special case, -NaN = NaN.
1224 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1226 if (!aff)
1227 return NULL;
1228 if (isl_aff_is_nan(aff))
1229 return aff;
1230 aff = isl_aff_cow(aff);
1231 if (!aff)
1232 return NULL;
1233 aff->v = isl_vec_cow(aff->v);
1234 if (!aff->v)
1235 return isl_aff_free(aff);
1237 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1239 return aff;
1242 /* Remove divs from the local space that do not appear in the affine
1243 * expression.
1244 * We currently only remove divs at the end.
1245 * Some intermediate divs may also not appear directly in the affine
1246 * expression, but we would also need to check that no other divs are
1247 * defined in terms of them.
1249 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1251 int pos;
1252 int off;
1253 int n;
1255 if (!aff)
1256 return NULL;
1258 n = isl_local_space_dim(aff->ls, isl_dim_div);
1259 off = isl_local_space_offset(aff->ls, isl_dim_div);
1261 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1262 if (pos == n)
1263 return aff;
1265 aff = isl_aff_cow(aff);
1266 if (!aff)
1267 return NULL;
1269 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1270 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1271 if (!aff->ls || !aff->v)
1272 return isl_aff_free(aff);
1274 return aff;
1277 /* Given two affine expressions "p" of length p_len (including the
1278 * denominator and the constant term) and "subs" of length subs_len,
1279 * plug in "subs" for the variable at position "pos".
1280 * The variables of "subs" and "p" are assumed to match up to subs_len,
1281 * but "p" may have additional variables.
1282 * "v" is an initialized isl_int that can be used internally.
1284 * In particular, if "p" represents the expression
1286 * (a i + g)/m
1288 * with i the variable at position "pos" and "subs" represents the expression
1290 * f/d
1292 * then the result represents the expression
1294 * (a f + d g)/(m d)
1297 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1298 int p_len, int subs_len, isl_int v)
1300 isl_int_set(v, p[1 + pos]);
1301 isl_int_set_si(p[1 + pos], 0);
1302 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1303 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1304 isl_int_mul(p[0], p[0], subs[0]);
1307 /* Look for any divs in the aff->ls with a denominator equal to one
1308 * and plug them into the affine expression and any subsequent divs
1309 * that may reference the div.
1311 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1313 int i, n;
1314 int len;
1315 isl_int v;
1316 isl_vec *vec;
1317 isl_local_space *ls;
1318 unsigned pos;
1320 if (!aff)
1321 return NULL;
1323 n = isl_local_space_dim(aff->ls, isl_dim_div);
1324 len = aff->v->size;
1325 for (i = 0; i < n; ++i) {
1326 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1327 continue;
1328 ls = isl_local_space_copy(aff->ls);
1329 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1330 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1331 vec = isl_vec_copy(aff->v);
1332 vec = isl_vec_cow(vec);
1333 if (!ls || !vec)
1334 goto error;
1336 isl_int_init(v);
1338 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1339 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1340 len, len, v);
1342 isl_int_clear(v);
1344 isl_vec_free(aff->v);
1345 aff->v = vec;
1346 isl_local_space_free(aff->ls);
1347 aff->ls = ls;
1350 return aff;
1351 error:
1352 isl_vec_free(vec);
1353 isl_local_space_free(ls);
1354 return isl_aff_free(aff);
1357 /* Look for any divs j that appear with a unit coefficient inside
1358 * the definitions of other divs i and plug them into the definitions
1359 * of the divs i.
1361 * In particular, an expression of the form
1363 * floor((f(..) + floor(g(..)/n))/m)
1365 * is simplified to
1367 * floor((n * f(..) + g(..))/(n * m))
1369 * This simplification is correct because we can move the expression
1370 * f(..) into the inner floor in the original expression to obtain
1372 * floor(floor((n * f(..) + g(..))/n)/m)
1374 * from which we can derive the simplified expression.
1376 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1378 int i, j, n;
1379 int off;
1381 if (!aff)
1382 return NULL;
1384 n = isl_local_space_dim(aff->ls, isl_dim_div);
1385 off = isl_local_space_offset(aff->ls, isl_dim_div);
1386 for (i = 1; i < n; ++i) {
1387 for (j = 0; j < i; ++j) {
1388 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1389 continue;
1390 aff->ls = isl_local_space_substitute_seq(aff->ls,
1391 isl_dim_div, j, aff->ls->div->row[j],
1392 aff->v->size, i, 1);
1393 if (!aff->ls)
1394 return isl_aff_free(aff);
1398 return aff;
1401 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1403 * Even though this function is only called on isl_affs with a single
1404 * reference, we are careful to only change aff->v and aff->ls together.
1406 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1408 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1409 isl_local_space *ls;
1410 isl_vec *v;
1412 ls = isl_local_space_copy(aff->ls);
1413 ls = isl_local_space_swap_div(ls, a, b);
1414 v = isl_vec_copy(aff->v);
1415 v = isl_vec_cow(v);
1416 if (!ls || !v)
1417 goto error;
1419 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1420 isl_vec_free(aff->v);
1421 aff->v = v;
1422 isl_local_space_free(aff->ls);
1423 aff->ls = ls;
1425 return aff;
1426 error:
1427 isl_vec_free(v);
1428 isl_local_space_free(ls);
1429 return isl_aff_free(aff);
1432 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1434 * We currently do not actually remove div "b", but simply add its
1435 * coefficient to that of "a" and then zero it out.
1437 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1439 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1441 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1442 return aff;
1444 aff->v = isl_vec_cow(aff->v);
1445 if (!aff->v)
1446 return isl_aff_free(aff);
1448 isl_int_add(aff->v->el[1 + off + a],
1449 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1450 isl_int_set_si(aff->v->el[1 + off + b], 0);
1452 return aff;
1455 /* Sort the divs in the local space of "aff" according to
1456 * the comparison function "cmp_row" in isl_local_space.c,
1457 * combining the coefficients of identical divs.
1459 * Reordering divs does not change the semantics of "aff",
1460 * so there is no need to call isl_aff_cow.
1461 * Moreover, this function is currently only called on isl_affs
1462 * with a single reference.
1464 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1466 int i, j, n;
1468 if (!aff)
1469 return NULL;
1471 n = isl_aff_dim(aff, isl_dim_div);
1472 for (i = 1; i < n; ++i) {
1473 for (j = i - 1; j >= 0; --j) {
1474 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1475 if (cmp < 0)
1476 break;
1477 if (cmp == 0)
1478 aff = merge_divs(aff, j, j + 1);
1479 else
1480 aff = swap_div(aff, j, j + 1);
1481 if (!aff)
1482 return NULL;
1486 return aff;
1489 /* Normalize the representation of "aff".
1491 * This function should only be called of "new" isl_affs, i.e.,
1492 * with only a single reference. We therefore do not need to
1493 * worry about affecting other instances.
1495 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1497 if (!aff)
1498 return NULL;
1499 aff->v = isl_vec_normalize(aff->v);
1500 if (!aff->v)
1501 return isl_aff_free(aff);
1502 aff = plug_in_integral_divs(aff);
1503 aff = plug_in_unit_divs(aff);
1504 aff = sort_divs(aff);
1505 aff = isl_aff_remove_unused_divs(aff);
1506 return aff;
1509 /* Given f, return floor(f).
1510 * If f is an integer expression, then just return f.
1511 * If f is a constant, then return the constant floor(f).
1512 * Otherwise, if f = g/m, write g = q m + r,
1513 * create a new div d = [r/m] and return the expression q + d.
1514 * The coefficients in r are taken to lie between -m/2 and m/2.
1516 * reduce_div_coefficients performs the same normalization.
1518 * As a special case, floor(NaN) = NaN.
1520 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1522 int i;
1523 int size;
1524 isl_ctx *ctx;
1525 isl_vec *div;
1527 if (!aff)
1528 return NULL;
1530 if (isl_aff_is_nan(aff))
1531 return aff;
1532 if (isl_int_is_one(aff->v->el[0]))
1533 return aff;
1535 aff = isl_aff_cow(aff);
1536 if (!aff)
1537 return NULL;
1539 aff->v = isl_vec_cow(aff->v);
1540 if (!aff->v)
1541 return isl_aff_free(aff);
1543 if (isl_aff_is_cst(aff)) {
1544 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1545 isl_int_set_si(aff->v->el[0], 1);
1546 return aff;
1549 div = isl_vec_copy(aff->v);
1550 div = isl_vec_cow(div);
1551 if (!div)
1552 return isl_aff_free(aff);
1554 ctx = isl_aff_get_ctx(aff);
1555 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1556 for (i = 1; i < aff->v->size; ++i) {
1557 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1558 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1559 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1560 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1561 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1565 aff->ls = isl_local_space_add_div(aff->ls, div);
1566 if (!aff->ls)
1567 return isl_aff_free(aff);
1569 size = aff->v->size;
1570 aff->v = isl_vec_extend(aff->v, size + 1);
1571 if (!aff->v)
1572 return isl_aff_free(aff);
1573 isl_int_set_si(aff->v->el[0], 1);
1574 isl_int_set_si(aff->v->el[size], 1);
1576 aff = isl_aff_normalize(aff);
1578 return aff;
1581 /* Compute
1583 * aff mod m = aff - m * floor(aff/m)
1585 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1587 isl_aff *res;
1589 res = isl_aff_copy(aff);
1590 aff = isl_aff_scale_down(aff, m);
1591 aff = isl_aff_floor(aff);
1592 aff = isl_aff_scale(aff, m);
1593 res = isl_aff_sub(res, aff);
1595 return res;
1598 /* Compute
1600 * aff mod m = aff - m * floor(aff/m)
1602 * with m an integer value.
1604 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1605 __isl_take isl_val *m)
1607 isl_aff *res;
1609 if (!aff || !m)
1610 goto error;
1612 if (!isl_val_is_int(m))
1613 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1614 "expecting integer modulo", goto error);
1616 res = isl_aff_copy(aff);
1617 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1618 aff = isl_aff_floor(aff);
1619 aff = isl_aff_scale_val(aff, m);
1620 res = isl_aff_sub(res, aff);
1622 return res;
1623 error:
1624 isl_aff_free(aff);
1625 isl_val_free(m);
1626 return NULL;
1629 /* Compute
1631 * pwaff mod m = pwaff - m * floor(pwaff/m)
1633 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1635 isl_pw_aff *res;
1637 res = isl_pw_aff_copy(pwaff);
1638 pwaff = isl_pw_aff_scale_down(pwaff, m);
1639 pwaff = isl_pw_aff_floor(pwaff);
1640 pwaff = isl_pw_aff_scale(pwaff, m);
1641 res = isl_pw_aff_sub(res, pwaff);
1643 return res;
1646 /* Compute
1648 * pa mod m = pa - m * floor(pa/m)
1650 * with m an integer value.
1652 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1653 __isl_take isl_val *m)
1655 if (!pa || !m)
1656 goto error;
1657 if (!isl_val_is_int(m))
1658 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1659 "expecting integer modulo", goto error);
1660 pa = isl_pw_aff_mod(pa, m->n);
1661 isl_val_free(m);
1662 return pa;
1663 error:
1664 isl_pw_aff_free(pa);
1665 isl_val_free(m);
1666 return NULL;
1669 /* Given f, return ceil(f).
1670 * If f is an integer expression, then just return f.
1671 * Otherwise, let f be the expression
1673 * e/m
1675 * then return
1677 * floor((e + m - 1)/m)
1679 * As a special case, ceil(NaN) = NaN.
1681 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1683 if (!aff)
1684 return NULL;
1686 if (isl_aff_is_nan(aff))
1687 return aff;
1688 if (isl_int_is_one(aff->v->el[0]))
1689 return aff;
1691 aff = isl_aff_cow(aff);
1692 if (!aff)
1693 return NULL;
1694 aff->v = isl_vec_cow(aff->v);
1695 if (!aff->v)
1696 return isl_aff_free(aff);
1698 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1699 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1700 aff = isl_aff_floor(aff);
1702 return aff;
1705 /* Apply the expansion computed by isl_merge_divs.
1706 * The expansion itself is given by "exp" while the resulting
1707 * list of divs is given by "div".
1709 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1710 __isl_take isl_mat *div, int *exp)
1712 int old_n_div;
1713 int new_n_div;
1714 int offset;
1716 aff = isl_aff_cow(aff);
1717 if (!aff || !div)
1718 goto error;
1720 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1721 new_n_div = isl_mat_rows(div);
1722 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1724 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1725 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1726 if (!aff->v || !aff->ls)
1727 return isl_aff_free(aff);
1728 return aff;
1729 error:
1730 isl_aff_free(aff);
1731 isl_mat_free(div);
1732 return NULL;
1735 /* Add two affine expressions that live in the same local space.
1737 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1738 __isl_take isl_aff *aff2)
1740 isl_int gcd, f;
1742 aff1 = isl_aff_cow(aff1);
1743 if (!aff1 || !aff2)
1744 goto error;
1746 aff1->v = isl_vec_cow(aff1->v);
1747 if (!aff1->v)
1748 goto error;
1750 isl_int_init(gcd);
1751 isl_int_init(f);
1752 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1753 isl_int_divexact(f, aff2->v->el[0], gcd);
1754 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1755 isl_int_divexact(f, aff1->v->el[0], gcd);
1756 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1757 isl_int_divexact(f, aff2->v->el[0], gcd);
1758 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1759 isl_int_clear(f);
1760 isl_int_clear(gcd);
1762 isl_aff_free(aff2);
1763 return aff1;
1764 error:
1765 isl_aff_free(aff1);
1766 isl_aff_free(aff2);
1767 return NULL;
1770 /* Return the sum of "aff1" and "aff2".
1772 * If either of the two is NaN, then the result is NaN.
1774 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1775 __isl_take isl_aff *aff2)
1777 isl_ctx *ctx;
1778 int *exp1 = NULL;
1779 int *exp2 = NULL;
1780 isl_mat *div;
1781 int n_div1, n_div2;
1783 if (!aff1 || !aff2)
1784 goto error;
1786 ctx = isl_aff_get_ctx(aff1);
1787 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1788 isl_die(ctx, isl_error_invalid,
1789 "spaces don't match", goto error);
1791 if (isl_aff_is_nan(aff1)) {
1792 isl_aff_free(aff2);
1793 return aff1;
1795 if (isl_aff_is_nan(aff2)) {
1796 isl_aff_free(aff1);
1797 return aff2;
1800 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1801 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1802 if (n_div1 == 0 && n_div2 == 0)
1803 return add_expanded(aff1, aff2);
1805 exp1 = isl_alloc_array(ctx, int, n_div1);
1806 exp2 = isl_alloc_array(ctx, int, n_div2);
1807 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1808 goto error;
1810 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1811 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1812 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1813 free(exp1);
1814 free(exp2);
1816 return add_expanded(aff1, aff2);
1817 error:
1818 free(exp1);
1819 free(exp2);
1820 isl_aff_free(aff1);
1821 isl_aff_free(aff2);
1822 return NULL;
1825 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1826 __isl_take isl_aff *aff2)
1828 return isl_aff_add(aff1, isl_aff_neg(aff2));
1831 /* Return the result of scaling "aff" by a factor of "f".
1833 * As a special case, f * NaN = NaN.
1835 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1837 isl_int gcd;
1839 if (!aff)
1840 return NULL;
1841 if (isl_aff_is_nan(aff))
1842 return aff;
1844 if (isl_int_is_one(f))
1845 return aff;
1847 aff = isl_aff_cow(aff);
1848 if (!aff)
1849 return NULL;
1850 aff->v = isl_vec_cow(aff->v);
1851 if (!aff->v)
1852 return isl_aff_free(aff);
1854 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1855 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1856 return aff;
1859 isl_int_init(gcd);
1860 isl_int_gcd(gcd, aff->v->el[0], f);
1861 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1862 isl_int_divexact(gcd, f, gcd);
1863 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1864 isl_int_clear(gcd);
1866 return aff;
1869 /* Multiple "aff" by "v".
1871 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1872 __isl_take isl_val *v)
1874 if (!aff || !v)
1875 goto error;
1877 if (isl_val_is_one(v)) {
1878 isl_val_free(v);
1879 return aff;
1882 if (!isl_val_is_rat(v))
1883 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1884 "expecting rational factor", goto error);
1886 aff = isl_aff_scale(aff, v->n);
1887 aff = isl_aff_scale_down(aff, v->d);
1889 isl_val_free(v);
1890 return aff;
1891 error:
1892 isl_aff_free(aff);
1893 isl_val_free(v);
1894 return NULL;
1897 /* Return the result of scaling "aff" down by a factor of "f".
1899 * As a special case, NaN/f = NaN.
1901 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1903 isl_int gcd;
1905 if (!aff)
1906 return NULL;
1907 if (isl_aff_is_nan(aff))
1908 return aff;
1910 if (isl_int_is_one(f))
1911 return aff;
1913 aff = isl_aff_cow(aff);
1914 if (!aff)
1915 return NULL;
1917 if (isl_int_is_zero(f))
1918 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1919 "cannot scale down by zero", return isl_aff_free(aff));
1921 aff->v = isl_vec_cow(aff->v);
1922 if (!aff->v)
1923 return isl_aff_free(aff);
1925 isl_int_init(gcd);
1926 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1927 isl_int_gcd(gcd, gcd, f);
1928 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1929 isl_int_divexact(gcd, f, gcd);
1930 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1931 isl_int_clear(gcd);
1933 return aff;
1936 /* Divide "aff" by "v".
1938 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1939 __isl_take isl_val *v)
1941 if (!aff || !v)
1942 goto error;
1944 if (isl_val_is_one(v)) {
1945 isl_val_free(v);
1946 return aff;
1949 if (!isl_val_is_rat(v))
1950 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1951 "expecting rational factor", goto error);
1952 if (!isl_val_is_pos(v))
1953 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1954 "factor needs to be positive", goto error);
1956 aff = isl_aff_scale(aff, v->d);
1957 aff = isl_aff_scale_down(aff, v->n);
1959 isl_val_free(v);
1960 return aff;
1961 error:
1962 isl_aff_free(aff);
1963 isl_val_free(v);
1964 return NULL;
1967 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1969 isl_int v;
1971 if (f == 1)
1972 return aff;
1974 isl_int_init(v);
1975 isl_int_set_ui(v, f);
1976 aff = isl_aff_scale_down(aff, v);
1977 isl_int_clear(v);
1979 return aff;
1982 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1983 enum isl_dim_type type, unsigned pos, const char *s)
1985 aff = isl_aff_cow(aff);
1986 if (!aff)
1987 return NULL;
1988 if (type == isl_dim_out)
1989 isl_die(aff->v->ctx, isl_error_invalid,
1990 "cannot set name of output/set dimension",
1991 return isl_aff_free(aff));
1992 if (type == isl_dim_in)
1993 type = isl_dim_set;
1994 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1995 if (!aff->ls)
1996 return isl_aff_free(aff);
1998 return aff;
2001 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2002 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2004 aff = isl_aff_cow(aff);
2005 if (!aff)
2006 goto error;
2007 if (type == isl_dim_out)
2008 isl_die(aff->v->ctx, isl_error_invalid,
2009 "cannot set name of output/set dimension",
2010 goto error);
2011 if (type == isl_dim_in)
2012 type = isl_dim_set;
2013 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2014 if (!aff->ls)
2015 return isl_aff_free(aff);
2017 return aff;
2018 error:
2019 isl_id_free(id);
2020 isl_aff_free(aff);
2021 return NULL;
2024 /* Replace the identifier of the input tuple of "aff" by "id".
2025 * type is currently required to be equal to isl_dim_in
2027 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2028 enum isl_dim_type type, __isl_take isl_id *id)
2030 aff = isl_aff_cow(aff);
2031 if (!aff)
2032 goto error;
2033 if (type != isl_dim_out)
2034 isl_die(aff->v->ctx, isl_error_invalid,
2035 "cannot only set id of input tuple", goto error);
2036 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2037 if (!aff->ls)
2038 return isl_aff_free(aff);
2040 return aff;
2041 error:
2042 isl_id_free(id);
2043 isl_aff_free(aff);
2044 return NULL;
2047 /* Exploit the equalities in "eq" to simplify the affine expression
2048 * and the expressions of the integer divisions in the local space.
2049 * The integer divisions in this local space are assumed to appear
2050 * as regular dimensions in "eq".
2052 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2053 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2055 int i, j;
2056 unsigned total;
2057 unsigned n_div;
2059 if (!eq)
2060 goto error;
2061 if (eq->n_eq == 0) {
2062 isl_basic_set_free(eq);
2063 return aff;
2066 aff = isl_aff_cow(aff);
2067 if (!aff)
2068 goto error;
2070 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2071 isl_basic_set_copy(eq));
2072 aff->v = isl_vec_cow(aff->v);
2073 if (!aff->ls || !aff->v)
2074 goto error;
2076 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2077 n_div = eq->n_div;
2078 for (i = 0; i < eq->n_eq; ++i) {
2079 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2080 if (j < 0 || j == 0 || j >= total)
2081 continue;
2083 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2084 &aff->v->el[0]);
2087 isl_basic_set_free(eq);
2088 aff = isl_aff_normalize(aff);
2089 return aff;
2090 error:
2091 isl_basic_set_free(eq);
2092 isl_aff_free(aff);
2093 return NULL;
2096 /* Exploit the equalities in "eq" to simplify the affine expression
2097 * and the expressions of the integer divisions in the local space.
2099 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2100 __isl_take isl_basic_set *eq)
2102 int n_div;
2104 if (!aff || !eq)
2105 goto error;
2106 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2107 if (n_div > 0)
2108 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2109 return isl_aff_substitute_equalities_lifted(aff, eq);
2110 error:
2111 isl_basic_set_free(eq);
2112 isl_aff_free(aff);
2113 return NULL;
2116 /* Look for equalities among the variables shared by context and aff
2117 * and the integer divisions of aff, if any.
2118 * The equalities are then used to eliminate coefficients and/or integer
2119 * divisions from aff.
2121 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2122 __isl_take isl_set *context)
2124 isl_basic_set *hull;
2125 int n_div;
2127 if (!aff)
2128 goto error;
2129 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2130 if (n_div > 0) {
2131 isl_basic_set *bset;
2132 isl_local_space *ls;
2133 context = isl_set_add_dims(context, isl_dim_set, n_div);
2134 ls = isl_aff_get_domain_local_space(aff);
2135 bset = isl_basic_set_from_local_space(ls);
2136 bset = isl_basic_set_lift(bset);
2137 bset = isl_basic_set_flatten(bset);
2138 context = isl_set_intersect(context,
2139 isl_set_from_basic_set(bset));
2142 hull = isl_set_affine_hull(context);
2143 return isl_aff_substitute_equalities_lifted(aff, hull);
2144 error:
2145 isl_aff_free(aff);
2146 isl_set_free(context);
2147 return NULL;
2150 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2151 __isl_take isl_set *context)
2153 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2154 dom_context = isl_set_intersect_params(dom_context, context);
2155 return isl_aff_gist(aff, dom_context);
2158 /* Return a basic set containing those elements in the space
2159 * of aff where it is positive. "rational" should not be set.
2161 * If "aff" is NaN, then it is not positive.
2163 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2164 int rational)
2166 isl_constraint *ineq;
2167 isl_basic_set *bset;
2168 isl_val *c;
2170 if (!aff)
2171 return NULL;
2172 if (isl_aff_is_nan(aff)) {
2173 isl_space *space = isl_aff_get_domain_space(aff);
2174 isl_aff_free(aff);
2175 return isl_basic_set_empty(space);
2177 if (rational)
2178 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2179 "rational sets not supported", goto error);
2181 ineq = isl_inequality_from_aff(aff);
2182 c = isl_constraint_get_constant_val(ineq);
2183 c = isl_val_sub_ui(c, 1);
2184 ineq = isl_constraint_set_constant_val(ineq, c);
2186 bset = isl_basic_set_from_constraint(ineq);
2187 bset = isl_basic_set_simplify(bset);
2188 return bset;
2189 error:
2190 isl_aff_free(aff);
2191 return NULL;
2194 /* Return a basic set containing those elements in the space
2195 * of aff where it is non-negative.
2196 * If "rational" is set, then return a rational basic set.
2198 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2200 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2201 __isl_take isl_aff *aff, int rational)
2203 isl_constraint *ineq;
2204 isl_basic_set *bset;
2206 if (!aff)
2207 return NULL;
2208 if (isl_aff_is_nan(aff)) {
2209 isl_space *space = isl_aff_get_domain_space(aff);
2210 isl_aff_free(aff);
2211 return isl_basic_set_empty(space);
2214 ineq = isl_inequality_from_aff(aff);
2216 bset = isl_basic_set_from_constraint(ineq);
2217 if (rational)
2218 bset = isl_basic_set_set_rational(bset);
2219 bset = isl_basic_set_simplify(bset);
2220 return bset;
2223 /* Return a basic set containing those elements in the space
2224 * of aff where it is non-negative.
2226 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2228 return aff_nonneg_basic_set(aff, 0);
2231 /* Return a basic set containing those elements in the domain space
2232 * of aff where it is negative.
2234 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2236 aff = isl_aff_neg(aff);
2237 aff = isl_aff_add_constant_num_si(aff, -1);
2238 return isl_aff_nonneg_basic_set(aff);
2241 /* Return a basic set containing those elements in the space
2242 * of aff where it is zero.
2243 * If "rational" is set, then return a rational basic set.
2245 * If "aff" is NaN, then it is not zero.
2247 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2248 int rational)
2250 isl_constraint *ineq;
2251 isl_basic_set *bset;
2253 if (!aff)
2254 return NULL;
2255 if (isl_aff_is_nan(aff)) {
2256 isl_space *space = isl_aff_get_domain_space(aff);
2257 isl_aff_free(aff);
2258 return isl_basic_set_empty(space);
2261 ineq = isl_equality_from_aff(aff);
2263 bset = isl_basic_set_from_constraint(ineq);
2264 if (rational)
2265 bset = isl_basic_set_set_rational(bset);
2266 bset = isl_basic_set_simplify(bset);
2267 return bset;
2270 /* Return a basic set containing those elements in the space
2271 * of aff where it is zero.
2273 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2275 return aff_zero_basic_set(aff, 0);
2278 /* Return a basic set containing those elements in the shared space
2279 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2281 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2282 __isl_take isl_aff *aff2)
2284 aff1 = isl_aff_sub(aff1, aff2);
2286 return isl_aff_nonneg_basic_set(aff1);
2289 /* Return a set containing those elements in the shared space
2290 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2292 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2293 __isl_take isl_aff *aff2)
2295 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2298 /* Return a basic set containing those elements in the shared space
2299 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2301 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2302 __isl_take isl_aff *aff2)
2304 return isl_aff_ge_basic_set(aff2, aff1);
2307 /* Return a set containing those elements in the shared space
2308 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2310 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2311 __isl_take isl_aff *aff2)
2313 return isl_aff_ge_set(aff2, aff1);
2316 /* Return a basic set containing those elements in the shared space
2317 * of aff1 and aff2 where aff1 and aff2 are equal.
2319 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2320 __isl_take isl_aff *aff2)
2322 aff1 = isl_aff_sub(aff1, aff2);
2324 return isl_aff_zero_basic_set(aff1);
2327 /* Return a set containing those elements in the shared space
2328 * of aff1 and aff2 where aff1 and aff2 are equal.
2330 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2331 __isl_take isl_aff *aff2)
2333 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2336 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2337 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2339 aff1 = isl_aff_add(aff1, aff2);
2340 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2341 return aff1;
2344 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2346 if (!aff)
2347 return -1;
2349 return 0;
2352 /* Check whether the given affine expression has non-zero coefficient
2353 * for any dimension in the given range or if any of these dimensions
2354 * appear with non-zero coefficients in any of the integer divisions
2355 * involved in the affine expression.
2357 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2358 enum isl_dim_type type, unsigned first, unsigned n)
2360 int i;
2361 isl_ctx *ctx;
2362 int *active = NULL;
2363 isl_bool involves = isl_bool_false;
2365 if (!aff)
2366 return isl_bool_error;
2367 if (n == 0)
2368 return isl_bool_false;
2370 ctx = isl_aff_get_ctx(aff);
2371 if (first + n > isl_aff_dim(aff, type))
2372 isl_die(ctx, isl_error_invalid,
2373 "range out of bounds", return isl_bool_error);
2375 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2376 if (!active)
2377 goto error;
2379 first += isl_local_space_offset(aff->ls, type) - 1;
2380 for (i = 0; i < n; ++i)
2381 if (active[first + i]) {
2382 involves = isl_bool_true;
2383 break;
2386 free(active);
2388 return involves;
2389 error:
2390 free(active);
2391 return isl_bool_error;
2394 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2395 enum isl_dim_type type, unsigned first, unsigned n)
2397 isl_ctx *ctx;
2399 if (!aff)
2400 return NULL;
2401 if (type == isl_dim_out)
2402 isl_die(aff->v->ctx, isl_error_invalid,
2403 "cannot drop output/set dimension",
2404 return isl_aff_free(aff));
2405 if (type == isl_dim_in)
2406 type = isl_dim_set;
2407 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2408 return aff;
2410 ctx = isl_aff_get_ctx(aff);
2411 if (first + n > isl_local_space_dim(aff->ls, type))
2412 isl_die(ctx, isl_error_invalid, "range out of bounds",
2413 return isl_aff_free(aff));
2415 aff = isl_aff_cow(aff);
2416 if (!aff)
2417 return NULL;
2419 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2420 if (!aff->ls)
2421 return isl_aff_free(aff);
2423 first += 1 + isl_local_space_offset(aff->ls, type);
2424 aff->v = isl_vec_drop_els(aff->v, first, n);
2425 if (!aff->v)
2426 return isl_aff_free(aff);
2428 return aff;
2431 /* Project the domain of the affine expression onto its parameter space.
2432 * The affine expression may not involve any of the domain dimensions.
2434 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2436 isl_space *space;
2437 unsigned n;
2438 int involves;
2440 n = isl_aff_dim(aff, isl_dim_in);
2441 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2442 if (involves < 0)
2443 return isl_aff_free(aff);
2444 if (involves)
2445 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2446 "affine expression involves some of the domain dimensions",
2447 return isl_aff_free(aff));
2448 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2449 space = isl_aff_get_domain_space(aff);
2450 space = isl_space_params(space);
2451 aff = isl_aff_reset_domain_space(aff, space);
2452 return aff;
2455 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2456 enum isl_dim_type type, unsigned first, unsigned n)
2458 isl_ctx *ctx;
2460 if (!aff)
2461 return NULL;
2462 if (type == isl_dim_out)
2463 isl_die(aff->v->ctx, isl_error_invalid,
2464 "cannot insert output/set dimensions",
2465 return isl_aff_free(aff));
2466 if (type == isl_dim_in)
2467 type = isl_dim_set;
2468 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2469 return aff;
2471 ctx = isl_aff_get_ctx(aff);
2472 if (first > isl_local_space_dim(aff->ls, type))
2473 isl_die(ctx, isl_error_invalid, "position out of bounds",
2474 return isl_aff_free(aff));
2476 aff = isl_aff_cow(aff);
2477 if (!aff)
2478 return NULL;
2480 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2481 if (!aff->ls)
2482 return isl_aff_free(aff);
2484 first += 1 + isl_local_space_offset(aff->ls, type);
2485 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2486 if (!aff->v)
2487 return isl_aff_free(aff);
2489 return aff;
2492 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2493 enum isl_dim_type type, unsigned n)
2495 unsigned pos;
2497 pos = isl_aff_dim(aff, type);
2499 return isl_aff_insert_dims(aff, type, pos, n);
2502 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2503 enum isl_dim_type type, unsigned n)
2505 unsigned pos;
2507 pos = isl_pw_aff_dim(pwaff, type);
2509 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2512 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2513 * to dimensions of "dst_type" at "dst_pos".
2515 * We only support moving input dimensions to parameters and vice versa.
2517 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2518 enum isl_dim_type dst_type, unsigned dst_pos,
2519 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2521 unsigned g_dst_pos;
2522 unsigned g_src_pos;
2524 if (!aff)
2525 return NULL;
2526 if (n == 0 &&
2527 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2528 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2529 return aff;
2531 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2532 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2533 "cannot move output/set dimension",
2534 return isl_aff_free(aff));
2535 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2536 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2537 "cannot move divs", return isl_aff_free(aff));
2538 if (dst_type == isl_dim_in)
2539 dst_type = isl_dim_set;
2540 if (src_type == isl_dim_in)
2541 src_type = isl_dim_set;
2543 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2544 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2545 "range out of bounds", return isl_aff_free(aff));
2546 if (dst_type == src_type)
2547 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2548 "moving dims within the same type not supported",
2549 return isl_aff_free(aff));
2551 aff = isl_aff_cow(aff);
2552 if (!aff)
2553 return NULL;
2555 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2556 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2557 if (dst_type > src_type)
2558 g_dst_pos -= n;
2560 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2561 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2562 src_type, src_pos, n);
2563 if (!aff->v || !aff->ls)
2564 return isl_aff_free(aff);
2566 aff = sort_divs(aff);
2568 return aff;
2571 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2573 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2574 return isl_pw_aff_alloc(dom, aff);
2577 #define isl_aff_involves_nan isl_aff_is_nan
2579 #undef PW
2580 #define PW isl_pw_aff
2581 #undef EL
2582 #define EL isl_aff
2583 #undef EL_IS_ZERO
2584 #define EL_IS_ZERO is_empty
2585 #undef ZERO
2586 #define ZERO empty
2587 #undef IS_ZERO
2588 #define IS_ZERO is_empty
2589 #undef FIELD
2590 #define FIELD aff
2591 #undef DEFAULT_IS_ZERO
2592 #define DEFAULT_IS_ZERO 0
2594 #define NO_EVAL
2595 #define NO_OPT
2596 #define NO_LIFT
2597 #define NO_MORPH
2599 #include <isl_pw_templ.c>
2600 #include <isl_pw_hash.c>
2601 #include <isl_pw_union_opt.c>
2603 #undef UNION
2604 #define UNION isl_union_pw_aff
2605 #undef PART
2606 #define PART isl_pw_aff
2607 #undef PARTS
2608 #define PARTS pw_aff
2610 #include <isl_union_single.c>
2611 #include <isl_union_neg.c>
2613 static __isl_give isl_set *align_params_pw_pw_set_and(
2614 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2615 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2616 __isl_take isl_pw_aff *pwaff2))
2618 isl_bool equal_params;
2620 if (!pwaff1 || !pwaff2)
2621 goto error;
2622 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2623 if (equal_params < 0)
2624 goto error;
2625 if (equal_params)
2626 return fn(pwaff1, pwaff2);
2627 if (!isl_space_has_named_params(pwaff1->dim) ||
2628 !isl_space_has_named_params(pwaff2->dim))
2629 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2630 "unaligned unnamed parameters", goto error);
2631 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2632 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2633 return fn(pwaff1, pwaff2);
2634 error:
2635 isl_pw_aff_free(pwaff1);
2636 isl_pw_aff_free(pwaff2);
2637 return NULL;
2640 /* Align the parameters of the to isl_pw_aff arguments and
2641 * then apply a function "fn" on them that returns an isl_map.
2643 static __isl_give isl_map *align_params_pw_pw_map_and(
2644 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2645 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2646 __isl_take isl_pw_aff *pa2))
2648 isl_bool equal_params;
2650 if (!pa1 || !pa2)
2651 goto error;
2652 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2653 if (equal_params < 0)
2654 goto error;
2655 if (equal_params)
2656 return fn(pa1, pa2);
2657 if (!isl_space_has_named_params(pa1->dim) ||
2658 !isl_space_has_named_params(pa2->dim))
2659 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2660 "unaligned unnamed parameters", goto error);
2661 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2662 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2663 return fn(pa1, pa2);
2664 error:
2665 isl_pw_aff_free(pa1);
2666 isl_pw_aff_free(pa2);
2667 return NULL;
2670 /* Compute a piecewise quasi-affine expression with a domain that
2671 * is the union of those of pwaff1 and pwaff2 and such that on each
2672 * cell, the quasi-affine expression is the maximum of those of pwaff1
2673 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2674 * cell, then the associated expression is the defined one.
2676 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2677 __isl_take isl_pw_aff *pwaff2)
2679 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2682 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2683 __isl_take isl_pw_aff *pwaff2)
2685 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2686 &pw_aff_union_max);
2689 /* Compute a piecewise quasi-affine expression with a domain that
2690 * is the union of those of pwaff1 and pwaff2 and such that on each
2691 * cell, the quasi-affine expression is the minimum of those of pwaff1
2692 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2693 * cell, then the associated expression is the defined one.
2695 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2696 __isl_take isl_pw_aff *pwaff2)
2698 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2701 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2702 __isl_take isl_pw_aff *pwaff2)
2704 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2705 &pw_aff_union_min);
2708 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2709 __isl_take isl_pw_aff *pwaff2, int max)
2711 if (max)
2712 return isl_pw_aff_union_max(pwaff1, pwaff2);
2713 else
2714 return isl_pw_aff_union_min(pwaff1, pwaff2);
2717 /* Construct a map with as domain the domain of pwaff and
2718 * one-dimensional range corresponding to the affine expressions.
2720 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2722 int i;
2723 isl_space *dim;
2724 isl_map *map;
2726 if (!pwaff)
2727 return NULL;
2729 dim = isl_pw_aff_get_space(pwaff);
2730 map = isl_map_empty(dim);
2732 for (i = 0; i < pwaff->n; ++i) {
2733 isl_basic_map *bmap;
2734 isl_map *map_i;
2736 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2737 map_i = isl_map_from_basic_map(bmap);
2738 map_i = isl_map_intersect_domain(map_i,
2739 isl_set_copy(pwaff->p[i].set));
2740 map = isl_map_union_disjoint(map, map_i);
2743 isl_pw_aff_free(pwaff);
2745 return map;
2748 /* Construct a map with as domain the domain of pwaff and
2749 * one-dimensional range corresponding to the affine expressions.
2751 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2753 if (!pwaff)
2754 return NULL;
2755 if (isl_space_is_set(pwaff->dim))
2756 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2757 "space of input is not a map", goto error);
2758 return map_from_pw_aff(pwaff);
2759 error:
2760 isl_pw_aff_free(pwaff);
2761 return NULL;
2764 /* Construct a one-dimensional set with as parameter domain
2765 * the domain of pwaff and the single set dimension
2766 * corresponding to the affine expressions.
2768 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2770 if (!pwaff)
2771 return NULL;
2772 if (!isl_space_is_set(pwaff->dim))
2773 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2774 "space of input is not a set", goto error);
2775 return map_from_pw_aff(pwaff);
2776 error:
2777 isl_pw_aff_free(pwaff);
2778 return NULL;
2781 /* Return a set containing those elements in the domain
2782 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2783 * does not satisfy "fn" (if complement is 1).
2785 * The pieces with a NaN never belong to the result since
2786 * NaN does not satisfy any property.
2788 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2789 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2790 int complement)
2792 int i;
2793 isl_set *set;
2795 if (!pwaff)
2796 return NULL;
2798 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2800 for (i = 0; i < pwaff->n; ++i) {
2801 isl_basic_set *bset;
2802 isl_set *set_i, *locus;
2803 isl_bool rational;
2805 if (isl_aff_is_nan(pwaff->p[i].aff))
2806 continue;
2808 rational = isl_set_has_rational(pwaff->p[i].set);
2809 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2810 locus = isl_set_from_basic_set(bset);
2811 set_i = isl_set_copy(pwaff->p[i].set);
2812 if (complement)
2813 set_i = isl_set_subtract(set_i, locus);
2814 else
2815 set_i = isl_set_intersect(set_i, locus);
2816 set = isl_set_union_disjoint(set, set_i);
2819 isl_pw_aff_free(pwaff);
2821 return set;
2824 /* Return a set containing those elements in the domain
2825 * of "pa" where it is positive.
2827 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2829 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2832 /* Return a set containing those elements in the domain
2833 * of pwaff where it is non-negative.
2835 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2837 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2840 /* Return a set containing those elements in the domain
2841 * of pwaff where it is zero.
2843 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2845 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2848 /* Return a set containing those elements in the domain
2849 * of pwaff where it is not zero.
2851 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2853 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2856 /* Return a set containing those elements in the shared domain
2857 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2859 * We compute the difference on the shared domain and then construct
2860 * the set of values where this difference is non-negative.
2861 * If strict is set, we first subtract 1 from the difference.
2862 * If equal is set, we only return the elements where pwaff1 and pwaff2
2863 * are equal.
2865 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2866 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2868 isl_set *set1, *set2;
2870 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2871 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2872 set1 = isl_set_intersect(set1, set2);
2873 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2874 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2875 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2877 if (strict) {
2878 isl_space *dim = isl_set_get_space(set1);
2879 isl_aff *aff;
2880 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2881 aff = isl_aff_add_constant_si(aff, -1);
2882 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2883 } else
2884 isl_set_free(set1);
2886 if (equal)
2887 return isl_pw_aff_zero_set(pwaff1);
2888 return isl_pw_aff_nonneg_set(pwaff1);
2891 /* Return a set containing those elements in the shared domain
2892 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2894 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2895 __isl_take isl_pw_aff *pwaff2)
2897 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2900 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2901 __isl_take isl_pw_aff *pwaff2)
2903 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2906 /* Return a set containing those elements in the shared domain
2907 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2909 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2910 __isl_take isl_pw_aff *pwaff2)
2912 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2915 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2916 __isl_take isl_pw_aff *pwaff2)
2918 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2921 /* Return a set containing those elements in the shared domain
2922 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2924 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2925 __isl_take isl_pw_aff *pwaff2)
2927 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2930 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2931 __isl_take isl_pw_aff *pwaff2)
2933 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2936 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2937 __isl_take isl_pw_aff *pwaff2)
2939 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2942 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2943 __isl_take isl_pw_aff *pwaff2)
2945 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2948 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2949 * where the function values are ordered in the same way as "order",
2950 * which returns a set in the shared domain of its two arguments.
2951 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2953 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2954 * We first pull back the two functions such that they are defined on
2955 * the domain [A -> B]. Then we apply "order", resulting in a set
2956 * in the space [A -> B]. Finally, we unwrap this set to obtain
2957 * a map in the space A -> B.
2959 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2960 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2961 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2962 __isl_take isl_pw_aff *pa2))
2964 isl_space *space1, *space2;
2965 isl_multi_aff *ma;
2966 isl_set *set;
2968 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2969 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2970 space1 = isl_space_map_from_domain_and_range(space1, space2);
2971 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2972 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2973 ma = isl_multi_aff_range_map(space1);
2974 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2975 set = order(pa1, pa2);
2977 return isl_set_unwrap(set);
2980 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2981 * where the function values are equal.
2982 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2984 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
2985 __isl_take isl_pw_aff *pa2)
2987 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
2990 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2991 * where the function values are equal.
2993 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
2994 __isl_take isl_pw_aff *pa2)
2996 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
2999 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3000 * where the function value of "pa1" is less than the function value of "pa2".
3001 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3003 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3004 __isl_take isl_pw_aff *pa2)
3006 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3009 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3010 * where the function value of "pa1" is less than the function value of "pa2".
3012 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3013 __isl_take isl_pw_aff *pa2)
3015 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3018 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3019 * where the function value of "pa1" is greater than the function value
3020 * of "pa2".
3021 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3023 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3024 __isl_take isl_pw_aff *pa2)
3026 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3029 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3030 * where the function value of "pa1" is greater than the function value
3031 * of "pa2".
3033 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3034 __isl_take isl_pw_aff *pa2)
3036 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3039 /* Return a set containing those elements in the shared domain
3040 * of the elements of list1 and list2 where each element in list1
3041 * has the relation specified by "fn" with each element in list2.
3043 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3044 __isl_take isl_pw_aff_list *list2,
3045 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3046 __isl_take isl_pw_aff *pwaff2))
3048 int i, j;
3049 isl_ctx *ctx;
3050 isl_set *set;
3052 if (!list1 || !list2)
3053 goto error;
3055 ctx = isl_pw_aff_list_get_ctx(list1);
3056 if (list1->n < 1 || list2->n < 1)
3057 isl_die(ctx, isl_error_invalid,
3058 "list should contain at least one element", goto error);
3060 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3061 for (i = 0; i < list1->n; ++i)
3062 for (j = 0; j < list2->n; ++j) {
3063 isl_set *set_ij;
3065 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3066 isl_pw_aff_copy(list2->p[j]));
3067 set = isl_set_intersect(set, set_ij);
3070 isl_pw_aff_list_free(list1);
3071 isl_pw_aff_list_free(list2);
3072 return set;
3073 error:
3074 isl_pw_aff_list_free(list1);
3075 isl_pw_aff_list_free(list2);
3076 return NULL;
3079 /* Return a set containing those elements in the shared domain
3080 * of the elements of list1 and list2 where each element in list1
3081 * is equal to each element in list2.
3083 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3084 __isl_take isl_pw_aff_list *list2)
3086 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3089 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3090 __isl_take isl_pw_aff_list *list2)
3092 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3095 /* Return a set containing those elements in the shared domain
3096 * of the elements of list1 and list2 where each element in list1
3097 * is less than or equal to each element in list2.
3099 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3100 __isl_take isl_pw_aff_list *list2)
3102 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3105 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3106 __isl_take isl_pw_aff_list *list2)
3108 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3111 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3112 __isl_take isl_pw_aff_list *list2)
3114 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3117 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3118 __isl_take isl_pw_aff_list *list2)
3120 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3124 /* Return a set containing those elements in the shared domain
3125 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3127 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3128 __isl_take isl_pw_aff *pwaff2)
3130 isl_set *set_lt, *set_gt;
3132 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3133 isl_pw_aff_copy(pwaff2));
3134 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3135 return isl_set_union_disjoint(set_lt, set_gt);
3138 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3139 __isl_take isl_pw_aff *pwaff2)
3141 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3144 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3145 isl_int v)
3147 int i;
3149 if (isl_int_is_one(v))
3150 return pwaff;
3151 if (!isl_int_is_pos(v))
3152 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3153 "factor needs to be positive",
3154 return isl_pw_aff_free(pwaff));
3155 pwaff = isl_pw_aff_cow(pwaff);
3156 if (!pwaff)
3157 return NULL;
3158 if (pwaff->n == 0)
3159 return pwaff;
3161 for (i = 0; i < pwaff->n; ++i) {
3162 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3163 if (!pwaff->p[i].aff)
3164 return isl_pw_aff_free(pwaff);
3167 return pwaff;
3170 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3172 int i;
3174 pwaff = isl_pw_aff_cow(pwaff);
3175 if (!pwaff)
3176 return NULL;
3177 if (pwaff->n == 0)
3178 return pwaff;
3180 for (i = 0; i < pwaff->n; ++i) {
3181 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3182 if (!pwaff->p[i].aff)
3183 return isl_pw_aff_free(pwaff);
3186 return pwaff;
3189 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3191 int i;
3193 pwaff = isl_pw_aff_cow(pwaff);
3194 if (!pwaff)
3195 return NULL;
3196 if (pwaff->n == 0)
3197 return pwaff;
3199 for (i = 0; i < pwaff->n; ++i) {
3200 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3201 if (!pwaff->p[i].aff)
3202 return isl_pw_aff_free(pwaff);
3205 return pwaff;
3208 /* Assuming that "cond1" and "cond2" are disjoint,
3209 * return an affine expression that is equal to pwaff1 on cond1
3210 * and to pwaff2 on cond2.
3212 static __isl_give isl_pw_aff *isl_pw_aff_select(
3213 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3214 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3216 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3217 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3219 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3222 /* Return an affine expression that is equal to pwaff_true for elements
3223 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3224 * is zero.
3225 * That is, return cond ? pwaff_true : pwaff_false;
3227 * If "cond" involves and NaN, then we conservatively return a NaN
3228 * on its entire domain. In principle, we could consider the pieces
3229 * where it is NaN separately from those where it is not.
3231 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3232 * then only use the domain of "cond" to restrict the domain.
3234 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3235 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3237 isl_set *cond_true, *cond_false;
3238 isl_bool equal;
3240 if (!cond)
3241 goto error;
3242 if (isl_pw_aff_involves_nan(cond)) {
3243 isl_space *space = isl_pw_aff_get_domain_space(cond);
3244 isl_local_space *ls = isl_local_space_from_space(space);
3245 isl_pw_aff_free(cond);
3246 isl_pw_aff_free(pwaff_true);
3247 isl_pw_aff_free(pwaff_false);
3248 return isl_pw_aff_nan_on_domain(ls);
3251 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3252 isl_pw_aff_get_space(pwaff_false));
3253 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3254 isl_pw_aff_get_space(pwaff_true));
3255 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3256 if (equal < 0)
3257 goto error;
3258 if (equal) {
3259 isl_set *dom;
3261 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3262 isl_pw_aff_free(pwaff_false);
3263 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3266 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3267 cond_false = isl_pw_aff_zero_set(cond);
3268 return isl_pw_aff_select(cond_true, pwaff_true,
3269 cond_false, pwaff_false);
3270 error:
3271 isl_pw_aff_free(cond);
3272 isl_pw_aff_free(pwaff_true);
3273 isl_pw_aff_free(pwaff_false);
3274 return NULL;
3277 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3279 if (!aff)
3280 return isl_bool_error;
3282 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3285 /* Check whether pwaff is a piecewise constant.
3287 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3289 int i;
3291 if (!pwaff)
3292 return isl_bool_error;
3294 for (i = 0; i < pwaff->n; ++i) {
3295 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3296 if (is_cst < 0 || !is_cst)
3297 return is_cst;
3300 return isl_bool_true;
3303 /* Are all elements of "mpa" piecewise constants?
3305 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3307 int i;
3309 if (!mpa)
3310 return isl_bool_error;
3312 for (i = 0; i < mpa->n; ++i) {
3313 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3314 if (is_cst < 0 || !is_cst)
3315 return is_cst;
3318 return isl_bool_true;
3321 /* Return the product of "aff1" and "aff2".
3323 * If either of the two is NaN, then the result is NaN.
3325 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3327 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3328 __isl_take isl_aff *aff2)
3330 if (!aff1 || !aff2)
3331 goto error;
3333 if (isl_aff_is_nan(aff1)) {
3334 isl_aff_free(aff2);
3335 return aff1;
3337 if (isl_aff_is_nan(aff2)) {
3338 isl_aff_free(aff1);
3339 return aff2;
3342 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3343 return isl_aff_mul(aff2, aff1);
3345 if (!isl_aff_is_cst(aff2))
3346 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3347 "at least one affine expression should be constant",
3348 goto error);
3350 aff1 = isl_aff_cow(aff1);
3351 if (!aff1 || !aff2)
3352 goto error;
3354 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3355 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3357 isl_aff_free(aff2);
3358 return aff1;
3359 error:
3360 isl_aff_free(aff1);
3361 isl_aff_free(aff2);
3362 return NULL;
3365 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3367 * If either of the two is NaN, then the result is NaN.
3369 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3370 __isl_take isl_aff *aff2)
3372 int is_cst;
3373 int neg;
3375 if (!aff1 || !aff2)
3376 goto error;
3378 if (isl_aff_is_nan(aff1)) {
3379 isl_aff_free(aff2);
3380 return aff1;
3382 if (isl_aff_is_nan(aff2)) {
3383 isl_aff_free(aff1);
3384 return aff2;
3387 is_cst = isl_aff_is_cst(aff2);
3388 if (is_cst < 0)
3389 goto error;
3390 if (!is_cst)
3391 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3392 "second argument should be a constant", goto error);
3394 if (!aff2)
3395 goto error;
3397 neg = isl_int_is_neg(aff2->v->el[1]);
3398 if (neg) {
3399 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3400 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3403 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3404 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3406 if (neg) {
3407 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3408 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3411 isl_aff_free(aff2);
3412 return aff1;
3413 error:
3414 isl_aff_free(aff1);
3415 isl_aff_free(aff2);
3416 return NULL;
3419 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3420 __isl_take isl_pw_aff *pwaff2)
3422 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3425 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3426 __isl_take isl_pw_aff *pwaff2)
3428 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3431 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3432 __isl_take isl_pw_aff *pwaff2)
3434 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3437 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3438 __isl_take isl_pw_aff *pwaff2)
3440 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3443 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3444 __isl_take isl_pw_aff *pwaff2)
3446 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3449 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3450 __isl_take isl_pw_aff *pa2)
3452 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3455 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3457 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3458 __isl_take isl_pw_aff *pa2)
3460 int is_cst;
3462 is_cst = isl_pw_aff_is_cst(pa2);
3463 if (is_cst < 0)
3464 goto error;
3465 if (!is_cst)
3466 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3467 "second argument should be a piecewise constant",
3468 goto error);
3469 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3470 error:
3471 isl_pw_aff_free(pa1);
3472 isl_pw_aff_free(pa2);
3473 return NULL;
3476 /* Compute the quotient of the integer division of "pa1" by "pa2"
3477 * with rounding towards zero.
3478 * "pa2" is assumed to be a piecewise constant.
3480 * In particular, return
3482 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3485 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3486 __isl_take isl_pw_aff *pa2)
3488 int is_cst;
3489 isl_set *cond;
3490 isl_pw_aff *f, *c;
3492 is_cst = isl_pw_aff_is_cst(pa2);
3493 if (is_cst < 0)
3494 goto error;
3495 if (!is_cst)
3496 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3497 "second argument should be a piecewise constant",
3498 goto error);
3500 pa1 = isl_pw_aff_div(pa1, pa2);
3502 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3503 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3504 c = isl_pw_aff_ceil(pa1);
3505 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3506 error:
3507 isl_pw_aff_free(pa1);
3508 isl_pw_aff_free(pa2);
3509 return NULL;
3512 /* Compute the remainder of the integer division of "pa1" by "pa2"
3513 * with rounding towards zero.
3514 * "pa2" is assumed to be a piecewise constant.
3516 * In particular, return
3518 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3521 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3522 __isl_take isl_pw_aff *pa2)
3524 int is_cst;
3525 isl_pw_aff *res;
3527 is_cst = isl_pw_aff_is_cst(pa2);
3528 if (is_cst < 0)
3529 goto error;
3530 if (!is_cst)
3531 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3532 "second argument should be a piecewise constant",
3533 goto error);
3534 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3535 res = isl_pw_aff_mul(pa2, res);
3536 res = isl_pw_aff_sub(pa1, res);
3537 return res;
3538 error:
3539 isl_pw_aff_free(pa1);
3540 isl_pw_aff_free(pa2);
3541 return NULL;
3544 /* Does either of "pa1" or "pa2" involve any NaN2?
3546 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3547 __isl_keep isl_pw_aff *pa2)
3549 isl_bool has_nan;
3551 has_nan = isl_pw_aff_involves_nan(pa1);
3552 if (has_nan < 0 || has_nan)
3553 return has_nan;
3554 return isl_pw_aff_involves_nan(pa2);
3557 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3558 * by a NaN on their shared domain.
3560 * In principle, the result could be refined to only being NaN
3561 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3563 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3564 __isl_take isl_pw_aff *pa2)
3566 isl_local_space *ls;
3567 isl_set *dom;
3568 isl_pw_aff *pa;
3570 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3571 ls = isl_local_space_from_space(isl_set_get_space(dom));
3572 pa = isl_pw_aff_nan_on_domain(ls);
3573 pa = isl_pw_aff_intersect_domain(pa, dom);
3575 return pa;
3578 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3579 __isl_take isl_pw_aff *pwaff2)
3581 isl_set *le;
3582 isl_set *dom;
3584 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3585 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3586 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3587 isl_pw_aff_copy(pwaff2));
3588 dom = isl_set_subtract(dom, isl_set_copy(le));
3589 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3592 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3593 __isl_take isl_pw_aff *pwaff2)
3595 isl_set *ge;
3596 isl_set *dom;
3598 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3599 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3600 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3601 isl_pw_aff_copy(pwaff2));
3602 dom = isl_set_subtract(dom, isl_set_copy(ge));
3603 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3606 /* Return an expression for the minimum (if "max" is not set) or
3607 * the maximum (if "max" is set) of "pa1" and "pa2".
3608 * If either expression involves any NaN, then return a NaN
3609 * on the shared domain as result.
3611 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3612 __isl_take isl_pw_aff *pa2, int max)
3614 isl_bool has_nan;
3616 has_nan = either_involves_nan(pa1, pa2);
3617 if (has_nan < 0)
3618 pa1 = isl_pw_aff_free(pa1);
3619 else if (has_nan)
3620 return replace_by_nan(pa1, pa2);
3622 if (max)
3623 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3624 else
3625 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3628 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3630 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3631 __isl_take isl_pw_aff *pwaff2)
3633 return pw_aff_min_max(pwaff1, pwaff2, 0);
3636 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3638 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3639 __isl_take isl_pw_aff *pwaff2)
3641 return pw_aff_min_max(pwaff1, pwaff2, 1);
3644 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3645 __isl_take isl_pw_aff_list *list,
3646 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3647 __isl_take isl_pw_aff *pwaff2))
3649 int i;
3650 isl_ctx *ctx;
3651 isl_pw_aff *res;
3653 if (!list)
3654 return NULL;
3656 ctx = isl_pw_aff_list_get_ctx(list);
3657 if (list->n < 1)
3658 isl_die(ctx, isl_error_invalid,
3659 "list should contain at least one element", goto error);
3661 res = isl_pw_aff_copy(list->p[0]);
3662 for (i = 1; i < list->n; ++i)
3663 res = fn(res, isl_pw_aff_copy(list->p[i]));
3665 isl_pw_aff_list_free(list);
3666 return res;
3667 error:
3668 isl_pw_aff_list_free(list);
3669 return NULL;
3672 /* Return an isl_pw_aff that maps each element in the intersection of the
3673 * domains of the elements of list to the minimal corresponding affine
3674 * expression.
3676 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3678 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3681 /* Return an isl_pw_aff that maps each element in the intersection of the
3682 * domains of the elements of list to the maximal corresponding affine
3683 * expression.
3685 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3687 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3690 /* Mark the domains of "pwaff" as rational.
3692 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3694 int i;
3696 pwaff = isl_pw_aff_cow(pwaff);
3697 if (!pwaff)
3698 return NULL;
3699 if (pwaff->n == 0)
3700 return pwaff;
3702 for (i = 0; i < pwaff->n; ++i) {
3703 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3704 if (!pwaff->p[i].set)
3705 return isl_pw_aff_free(pwaff);
3708 return pwaff;
3711 /* Mark the domains of the elements of "list" as rational.
3713 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3714 __isl_take isl_pw_aff_list *list)
3716 int i, n;
3718 if (!list)
3719 return NULL;
3720 if (list->n == 0)
3721 return list;
3723 n = list->n;
3724 for (i = 0; i < n; ++i) {
3725 isl_pw_aff *pa;
3727 pa = isl_pw_aff_list_get_pw_aff(list, i);
3728 pa = isl_pw_aff_set_rational(pa);
3729 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3732 return list;
3735 /* Do the parameters of "aff" match those of "space"?
3737 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3738 __isl_keep isl_space *space)
3740 isl_space *aff_space;
3741 isl_bool match;
3743 if (!aff || !space)
3744 return isl_bool_error;
3746 aff_space = isl_aff_get_domain_space(aff);
3748 match = isl_space_has_equal_params(space, aff_space);
3750 isl_space_free(aff_space);
3751 return match;
3754 /* Check that the domain space of "aff" matches "space".
3756 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3757 __isl_keep isl_space *space)
3759 isl_space *aff_space;
3760 isl_bool match;
3762 if (!aff || !space)
3763 return isl_stat_error;
3765 aff_space = isl_aff_get_domain_space(aff);
3767 match = isl_space_has_equal_params(space, aff_space);
3768 if (match < 0)
3769 goto error;
3770 if (!match)
3771 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3772 "parameters don't match", goto error);
3773 match = isl_space_tuple_is_equal(space, isl_dim_in,
3774 aff_space, isl_dim_set);
3775 if (match < 0)
3776 goto error;
3777 if (!match)
3778 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3779 "domains don't match", goto error);
3780 isl_space_free(aff_space);
3781 return isl_stat_ok;
3782 error:
3783 isl_space_free(aff_space);
3784 return isl_stat_error;
3787 #undef BASE
3788 #define BASE aff
3789 #undef DOMBASE
3790 #define DOMBASE set
3791 #define NO_DOMAIN
3793 #include <isl_multi_templ.c>
3794 #include <isl_multi_apply_set.c>
3795 #include <isl_multi_cmp.c>
3796 #include <isl_multi_floor.c>
3797 #include <isl_multi_gist.c>
3799 #undef NO_DOMAIN
3801 /* Remove any internal structure of the domain of "ma".
3802 * If there is any such internal structure in the input,
3803 * then the name of the corresponding space is also removed.
3805 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3806 __isl_take isl_multi_aff *ma)
3808 isl_space *space;
3810 if (!ma)
3811 return NULL;
3813 if (!ma->space->nested[0])
3814 return ma;
3816 space = isl_multi_aff_get_space(ma);
3817 space = isl_space_flatten_domain(space);
3818 ma = isl_multi_aff_reset_space(ma, space);
3820 return ma;
3823 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3824 * of the space to its domain.
3826 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3828 int i, n_in;
3829 isl_local_space *ls;
3830 isl_multi_aff *ma;
3832 if (!space)
3833 return NULL;
3834 if (!isl_space_is_map(space))
3835 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3836 "not a map space", goto error);
3838 n_in = isl_space_dim(space, isl_dim_in);
3839 space = isl_space_domain_map(space);
3841 ma = isl_multi_aff_alloc(isl_space_copy(space));
3842 if (n_in == 0) {
3843 isl_space_free(space);
3844 return ma;
3847 space = isl_space_domain(space);
3848 ls = isl_local_space_from_space(space);
3849 for (i = 0; i < n_in; ++i) {
3850 isl_aff *aff;
3852 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3853 isl_dim_set, i);
3854 ma = isl_multi_aff_set_aff(ma, i, aff);
3856 isl_local_space_free(ls);
3857 return ma;
3858 error:
3859 isl_space_free(space);
3860 return NULL;
3863 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3864 * of the space to its range.
3866 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3868 int i, n_in, n_out;
3869 isl_local_space *ls;
3870 isl_multi_aff *ma;
3872 if (!space)
3873 return NULL;
3874 if (!isl_space_is_map(space))
3875 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3876 "not a map space", goto error);
3878 n_in = isl_space_dim(space, isl_dim_in);
3879 n_out = isl_space_dim(space, isl_dim_out);
3880 space = isl_space_range_map(space);
3882 ma = isl_multi_aff_alloc(isl_space_copy(space));
3883 if (n_out == 0) {
3884 isl_space_free(space);
3885 return ma;
3888 space = isl_space_domain(space);
3889 ls = isl_local_space_from_space(space);
3890 for (i = 0; i < n_out; ++i) {
3891 isl_aff *aff;
3893 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3894 isl_dim_set, n_in + i);
3895 ma = isl_multi_aff_set_aff(ma, i, aff);
3897 isl_local_space_free(ls);
3898 return ma;
3899 error:
3900 isl_space_free(space);
3901 return NULL;
3904 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3905 * of the space to its range.
3907 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3908 __isl_take isl_space *space)
3910 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3913 /* Given the space of a set and a range of set dimensions,
3914 * construct an isl_multi_aff that projects out those dimensions.
3916 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3917 __isl_take isl_space *space, enum isl_dim_type type,
3918 unsigned first, unsigned n)
3920 int i, dim;
3921 isl_local_space *ls;
3922 isl_multi_aff *ma;
3924 if (!space)
3925 return NULL;
3926 if (!isl_space_is_set(space))
3927 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3928 "expecting set space", goto error);
3929 if (type != isl_dim_set)
3930 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3931 "only set dimensions can be projected out", goto error);
3933 dim = isl_space_dim(space, isl_dim_set);
3934 if (first + n > dim)
3935 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3936 "range out of bounds", goto error);
3938 space = isl_space_from_domain(space);
3939 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3941 if (dim == n)
3942 return isl_multi_aff_alloc(space);
3944 ma = isl_multi_aff_alloc(isl_space_copy(space));
3945 space = isl_space_domain(space);
3946 ls = isl_local_space_from_space(space);
3948 for (i = 0; i < first; ++i) {
3949 isl_aff *aff;
3951 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3952 isl_dim_set, i);
3953 ma = isl_multi_aff_set_aff(ma, i, aff);
3956 for (i = 0; i < dim - (first + n); ++i) {
3957 isl_aff *aff;
3959 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3960 isl_dim_set, first + n + i);
3961 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3964 isl_local_space_free(ls);
3965 return ma;
3966 error:
3967 isl_space_free(space);
3968 return NULL;
3971 /* Given the space of a set and a range of set dimensions,
3972 * construct an isl_pw_multi_aff that projects out those dimensions.
3974 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3975 __isl_take isl_space *space, enum isl_dim_type type,
3976 unsigned first, unsigned n)
3978 isl_multi_aff *ma;
3980 ma = isl_multi_aff_project_out_map(space, type, first, n);
3981 return isl_pw_multi_aff_from_multi_aff(ma);
3984 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3985 * domain.
3987 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3988 __isl_take isl_multi_aff *ma)
3990 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3991 return isl_pw_multi_aff_alloc(dom, ma);
3994 /* Create a piecewise multi-affine expression in the given space that maps each
3995 * input dimension to the corresponding output dimension.
3997 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3998 __isl_take isl_space *space)
4000 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4003 /* Exploit the equalities in "eq" to simplify the affine expressions.
4005 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4006 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4008 int i;
4010 maff = isl_multi_aff_cow(maff);
4011 if (!maff || !eq)
4012 goto error;
4014 for (i = 0; i < maff->n; ++i) {
4015 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
4016 isl_basic_set_copy(eq));
4017 if (!maff->p[i])
4018 goto error;
4021 isl_basic_set_free(eq);
4022 return maff;
4023 error:
4024 isl_basic_set_free(eq);
4025 isl_multi_aff_free(maff);
4026 return NULL;
4029 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4030 isl_int f)
4032 int i;
4034 maff = isl_multi_aff_cow(maff);
4035 if (!maff)
4036 return NULL;
4038 for (i = 0; i < maff->n; ++i) {
4039 maff->p[i] = isl_aff_scale(maff->p[i], f);
4040 if (!maff->p[i])
4041 return isl_multi_aff_free(maff);
4044 return maff;
4047 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4048 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4050 maff1 = isl_multi_aff_add(maff1, maff2);
4051 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4052 return maff1;
4055 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4057 if (!maff)
4058 return -1;
4060 return 0;
4063 /* Return the set of domain elements where "ma1" is lexicographically
4064 * smaller than or equal to "ma2".
4066 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4067 __isl_take isl_multi_aff *ma2)
4069 return isl_multi_aff_lex_ge_set(ma2, ma1);
4072 /* Return the set of domain elements where "ma1" is lexicographically
4073 * smaller than "ma2".
4075 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4076 __isl_take isl_multi_aff *ma2)
4078 return isl_multi_aff_lex_gt_set(ma2, ma1);
4081 /* Return the set of domain elements where "ma1" and "ma2"
4082 * satisfy "order".
4084 static __isl_give isl_set *isl_multi_aff_order_set(
4085 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4086 __isl_give isl_map *order(__isl_take isl_space *set_space))
4088 isl_space *space;
4089 isl_map *map1, *map2;
4090 isl_map *map, *ge;
4092 map1 = isl_map_from_multi_aff(ma1);
4093 map2 = isl_map_from_multi_aff(ma2);
4094 map = isl_map_range_product(map1, map2);
4095 space = isl_space_range(isl_map_get_space(map));
4096 space = isl_space_domain(isl_space_unwrap(space));
4097 ge = order(space);
4098 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4100 return isl_map_domain(map);
4103 /* Return the set of domain elements where "ma1" is lexicographically
4104 * greater than or equal to "ma2".
4106 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4107 __isl_take isl_multi_aff *ma2)
4109 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4112 /* Return the set of domain elements where "ma1" is lexicographically
4113 * greater than "ma2".
4115 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4116 __isl_take isl_multi_aff *ma2)
4118 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4121 #undef PW
4122 #define PW isl_pw_multi_aff
4123 #undef EL
4124 #define EL isl_multi_aff
4125 #undef EL_IS_ZERO
4126 #define EL_IS_ZERO is_empty
4127 #undef ZERO
4128 #define ZERO empty
4129 #undef IS_ZERO
4130 #define IS_ZERO is_empty
4131 #undef FIELD
4132 #define FIELD maff
4133 #undef DEFAULT_IS_ZERO
4134 #define DEFAULT_IS_ZERO 0
4136 #define NO_SUB
4137 #define NO_EVAL
4138 #define NO_OPT
4139 #define NO_INVOLVES_DIMS
4140 #define NO_INSERT_DIMS
4141 #define NO_LIFT
4142 #define NO_MORPH
4144 #include <isl_pw_templ.c>
4145 #include <isl_pw_union_opt.c>
4147 #undef NO_SUB
4149 #undef UNION
4150 #define UNION isl_union_pw_multi_aff
4151 #undef PART
4152 #define PART isl_pw_multi_aff
4153 #undef PARTS
4154 #define PARTS pw_multi_aff
4156 #include <isl_union_multi.c>
4157 #include <isl_union_neg.c>
4159 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4160 __isl_take isl_pw_multi_aff *pma1,
4161 __isl_take isl_pw_multi_aff *pma2)
4163 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4164 &isl_multi_aff_lex_ge_set);
4167 /* Given two piecewise multi affine expressions, return a piecewise
4168 * multi-affine expression defined on the union of the definition domains
4169 * of the inputs that is equal to the lexicographic maximum of the two
4170 * inputs on each cell. If only one of the two inputs is defined on
4171 * a given cell, then it is considered to be the maximum.
4173 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4174 __isl_take isl_pw_multi_aff *pma1,
4175 __isl_take isl_pw_multi_aff *pma2)
4177 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4178 &pw_multi_aff_union_lexmax);
4181 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4182 __isl_take isl_pw_multi_aff *pma1,
4183 __isl_take isl_pw_multi_aff *pma2)
4185 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4186 &isl_multi_aff_lex_le_set);
4189 /* Given two piecewise multi affine expressions, return a piecewise
4190 * multi-affine expression defined on the union of the definition domains
4191 * of the inputs that is equal to the lexicographic minimum of the two
4192 * inputs on each cell. If only one of the two inputs is defined on
4193 * a given cell, then it is considered to be the minimum.
4195 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4196 __isl_take isl_pw_multi_aff *pma1,
4197 __isl_take isl_pw_multi_aff *pma2)
4199 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4200 &pw_multi_aff_union_lexmin);
4203 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4204 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4206 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4207 &isl_multi_aff_add);
4210 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4211 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4213 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4214 &pw_multi_aff_add);
4217 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4218 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4220 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4221 &isl_multi_aff_sub);
4224 /* Subtract "pma2" from "pma1" and return the result.
4226 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4227 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4229 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4230 &pw_multi_aff_sub);
4233 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4234 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4236 return isl_pw_multi_aff_union_add_(pma1, pma2);
4239 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4240 * with the actual sum on the shared domain and
4241 * the defined expression on the symmetric difference of the domains.
4243 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4244 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4246 return isl_union_pw_aff_union_add_(upa1, upa2);
4249 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4250 * with the actual sum on the shared domain and
4251 * the defined expression on the symmetric difference of the domains.
4253 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4254 __isl_take isl_union_pw_multi_aff *upma1,
4255 __isl_take isl_union_pw_multi_aff *upma2)
4257 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4260 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4261 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4263 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4264 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4266 int i, j, n;
4267 isl_space *space;
4268 isl_pw_multi_aff *res;
4270 if (!pma1 || !pma2)
4271 goto error;
4273 n = pma1->n * pma2->n;
4274 space = isl_space_product(isl_space_copy(pma1->dim),
4275 isl_space_copy(pma2->dim));
4276 res = isl_pw_multi_aff_alloc_size(space, n);
4278 for (i = 0; i < pma1->n; ++i) {
4279 for (j = 0; j < pma2->n; ++j) {
4280 isl_set *domain;
4281 isl_multi_aff *ma;
4283 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4284 isl_set_copy(pma2->p[j].set));
4285 ma = isl_multi_aff_product(
4286 isl_multi_aff_copy(pma1->p[i].maff),
4287 isl_multi_aff_copy(pma2->p[j].maff));
4288 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4292 isl_pw_multi_aff_free(pma1);
4293 isl_pw_multi_aff_free(pma2);
4294 return res;
4295 error:
4296 isl_pw_multi_aff_free(pma1);
4297 isl_pw_multi_aff_free(pma2);
4298 return NULL;
4301 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4302 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4304 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4305 &pw_multi_aff_product);
4308 /* Construct a map mapping the domain of the piecewise multi-affine expression
4309 * to its range, with each dimension in the range equated to the
4310 * corresponding affine expression on its cell.
4312 * If the domain of "pma" is rational, then so is the constructed "map".
4314 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4316 int i;
4317 isl_map *map;
4319 if (!pma)
4320 return NULL;
4322 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4324 for (i = 0; i < pma->n; ++i) {
4325 isl_bool rational;
4326 isl_multi_aff *maff;
4327 isl_basic_map *bmap;
4328 isl_map *map_i;
4330 rational = isl_set_is_rational(pma->p[i].set);
4331 if (rational < 0)
4332 map = isl_map_free(map);
4333 maff = isl_multi_aff_copy(pma->p[i].maff);
4334 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4335 map_i = isl_map_from_basic_map(bmap);
4336 map_i = isl_map_intersect_domain(map_i,
4337 isl_set_copy(pma->p[i].set));
4338 map = isl_map_union_disjoint(map, map_i);
4341 isl_pw_multi_aff_free(pma);
4342 return map;
4345 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4347 if (!pma)
4348 return NULL;
4350 if (!isl_space_is_set(pma->dim))
4351 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4352 "isl_pw_multi_aff cannot be converted into an isl_set",
4353 goto error);
4355 return isl_map_from_pw_multi_aff(pma);
4356 error:
4357 isl_pw_multi_aff_free(pma);
4358 return NULL;
4361 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4362 * denominator "denom".
4363 * "denom" is allowed to be negative, in which case the actual denominator
4364 * is -denom and the expressions are added instead.
4366 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4367 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4369 int i, first;
4370 int sign;
4371 isl_int d;
4373 first = isl_seq_first_non_zero(c, n);
4374 if (first == -1)
4375 return aff;
4377 sign = isl_int_sgn(denom);
4378 isl_int_init(d);
4379 isl_int_abs(d, denom);
4380 for (i = first; i < n; ++i) {
4381 isl_aff *aff_i;
4383 if (isl_int_is_zero(c[i]))
4384 continue;
4385 aff_i = isl_multi_aff_get_aff(ma, i);
4386 aff_i = isl_aff_scale(aff_i, c[i]);
4387 aff_i = isl_aff_scale_down(aff_i, d);
4388 if (sign >= 0)
4389 aff = isl_aff_sub(aff, aff_i);
4390 else
4391 aff = isl_aff_add(aff, aff_i);
4393 isl_int_clear(d);
4395 return aff;
4398 /* Extract an affine expression that expresses the output dimension "pos"
4399 * of "bmap" in terms of the parameters and input dimensions from
4400 * equality "eq".
4401 * Note that this expression may involve integer divisions defined
4402 * in terms of parameters and input dimensions.
4403 * The equality may also involve references to earlier (but not later)
4404 * output dimensions. These are replaced by the corresponding elements
4405 * in "ma".
4407 * If the equality is of the form
4409 * f(i) + h(j) + a x + g(i) = 0,
4411 * with f(i) a linear combinations of the parameters and input dimensions,
4412 * g(i) a linear combination of integer divisions defined in terms of the same
4413 * and h(j) a linear combinations of earlier output dimensions,
4414 * then the affine expression is
4416 * (-f(i) - g(i))/a - h(j)/a
4418 * If the equality is of the form
4420 * f(i) + h(j) - a x + g(i) = 0,
4422 * then the affine expression is
4424 * (f(i) + g(i))/a - h(j)/(-a)
4427 * If "div" refers to an integer division (i.e., it is smaller than
4428 * the number of integer divisions), then the equality constraint
4429 * does involve an integer division (the one at position "div") that
4430 * is defined in terms of output dimensions. However, this integer
4431 * division can be eliminated by exploiting a pair of constraints
4432 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4433 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4434 * -l + x >= 0.
4435 * In particular, let
4437 * x = e(i) + m floor(...)
4439 * with e(i) the expression derived above and floor(...) the integer
4440 * division involving output dimensions.
4441 * From
4443 * l <= x <= l + n,
4445 * we have
4447 * 0 <= x - l <= n
4449 * This means
4451 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4452 * = (e(i) - l) mod m
4454 * Therefore,
4456 * x - l = (e(i) - l) mod m
4458 * or
4460 * x = ((e(i) - l) mod m) + l
4462 * The variable "shift" below contains the expression -l, which may
4463 * also involve a linear combination of earlier output dimensions.
4465 static __isl_give isl_aff *extract_aff_from_equality(
4466 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4467 __isl_keep isl_multi_aff *ma)
4469 unsigned o_out;
4470 unsigned n_div, n_out;
4471 isl_ctx *ctx;
4472 isl_local_space *ls;
4473 isl_aff *aff, *shift;
4474 isl_val *mod;
4476 ctx = isl_basic_map_get_ctx(bmap);
4477 ls = isl_basic_map_get_local_space(bmap);
4478 ls = isl_local_space_domain(ls);
4479 aff = isl_aff_alloc(isl_local_space_copy(ls));
4480 if (!aff)
4481 goto error;
4482 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4483 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4484 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4485 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4486 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4487 isl_seq_cpy(aff->v->el + 1 + o_out,
4488 bmap->eq[eq] + o_out + n_out, n_div);
4489 } else {
4490 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4491 isl_seq_neg(aff->v->el + 1 + o_out,
4492 bmap->eq[eq] + o_out + n_out, n_div);
4494 if (div < n_div)
4495 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4496 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4497 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4498 bmap->eq[eq][o_out + pos]);
4499 if (div < n_div) {
4500 shift = isl_aff_alloc(isl_local_space_copy(ls));
4501 if (!shift)
4502 goto error;
4503 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4504 isl_seq_cpy(shift->v->el + 1 + o_out,
4505 bmap->ineq[ineq] + o_out + n_out, n_div);
4506 isl_int_set_si(shift->v->el[0], 1);
4507 shift = subtract_initial(shift, ma, pos,
4508 bmap->ineq[ineq] + o_out, ctx->negone);
4509 aff = isl_aff_add(aff, isl_aff_copy(shift));
4510 mod = isl_val_int_from_isl_int(ctx,
4511 bmap->eq[eq][o_out + n_out + div]);
4512 mod = isl_val_abs(mod);
4513 aff = isl_aff_mod_val(aff, mod);
4514 aff = isl_aff_sub(aff, shift);
4517 isl_local_space_free(ls);
4518 return aff;
4519 error:
4520 isl_local_space_free(ls);
4521 isl_aff_free(aff);
4522 return NULL;
4525 /* Given a basic map with output dimensions defined
4526 * in terms of the parameters input dimensions and earlier
4527 * output dimensions using an equality (and possibly a pair on inequalities),
4528 * extract an isl_aff that expresses output dimension "pos" in terms
4529 * of the parameters and input dimensions.
4530 * Note that this expression may involve integer divisions defined
4531 * in terms of parameters and input dimensions.
4532 * "ma" contains the expressions corresponding to earlier output dimensions.
4534 * This function shares some similarities with
4535 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4537 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4538 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4540 int eq, div, ineq;
4541 isl_aff *aff;
4543 if (!bmap)
4544 return NULL;
4545 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4546 if (eq >= bmap->n_eq)
4547 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4548 "unable to find suitable equality", return NULL);
4549 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4551 aff = isl_aff_remove_unused_divs(aff);
4552 return aff;
4555 /* Given a basic map where each output dimension is defined
4556 * in terms of the parameters and input dimensions using an equality,
4557 * extract an isl_multi_aff that expresses the output dimensions in terms
4558 * of the parameters and input dimensions.
4560 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4561 __isl_take isl_basic_map *bmap)
4563 int i;
4564 unsigned n_out;
4565 isl_multi_aff *ma;
4567 if (!bmap)
4568 return NULL;
4570 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4571 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4573 for (i = 0; i < n_out; ++i) {
4574 isl_aff *aff;
4576 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4577 ma = isl_multi_aff_set_aff(ma, i, aff);
4580 isl_basic_map_free(bmap);
4582 return ma;
4585 /* Given a basic set where each set dimension is defined
4586 * in terms of the parameters using an equality,
4587 * extract an isl_multi_aff that expresses the set dimensions in terms
4588 * of the parameters.
4590 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4591 __isl_take isl_basic_set *bset)
4593 return extract_isl_multi_aff_from_basic_map(bset);
4596 /* Create an isl_pw_multi_aff that is equivalent to
4597 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4598 * The given basic map is such that each output dimension is defined
4599 * in terms of the parameters and input dimensions using an equality.
4601 * Since some applications expect the result of isl_pw_multi_aff_from_map
4602 * to only contain integer affine expressions, we compute the floor
4603 * of the expression before returning.
4605 * Remove all constraints involving local variables without
4606 * an explicit representation (resulting in the removal of those
4607 * local variables) prior to the actual extraction to ensure
4608 * that the local spaces in which the resulting affine expressions
4609 * are created do not contain any unknown local variables.
4610 * Removing such constraints is safe because constraints involving
4611 * unknown local variables are not used to determine whether
4612 * a basic map is obviously single-valued.
4614 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4615 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4617 isl_multi_aff *ma;
4619 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4620 ma = extract_isl_multi_aff_from_basic_map(bmap);
4621 ma = isl_multi_aff_floor(ma);
4622 return isl_pw_multi_aff_alloc(domain, ma);
4625 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4626 * This obviously only works if the input "map" is single-valued.
4627 * If so, we compute the lexicographic minimum of the image in the form
4628 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4629 * to its lexicographic minimum.
4630 * If the input is not single-valued, we produce an error.
4632 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4633 __isl_take isl_map *map)
4635 int i;
4636 int sv;
4637 isl_pw_multi_aff *pma;
4639 sv = isl_map_is_single_valued(map);
4640 if (sv < 0)
4641 goto error;
4642 if (!sv)
4643 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4644 "map is not single-valued", goto error);
4645 map = isl_map_make_disjoint(map);
4646 if (!map)
4647 return NULL;
4649 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4651 for (i = 0; i < map->n; ++i) {
4652 isl_pw_multi_aff *pma_i;
4653 isl_basic_map *bmap;
4654 bmap = isl_basic_map_copy(map->p[i]);
4655 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4656 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4659 isl_map_free(map);
4660 return pma;
4661 error:
4662 isl_map_free(map);
4663 return NULL;
4666 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4667 * taking into account that the output dimension at position "d"
4668 * can be represented as
4670 * x = floor((e(...) + c1) / m)
4672 * given that constraint "i" is of the form
4674 * e(...) + c1 - m x >= 0
4677 * Let "map" be of the form
4679 * A -> B
4681 * We construct a mapping
4683 * A -> [A -> x = floor(...)]
4685 * apply that to the map, obtaining
4687 * [A -> x = floor(...)] -> B
4689 * and equate dimension "d" to x.
4690 * We then compute a isl_pw_multi_aff representation of the resulting map
4691 * and plug in the mapping above.
4693 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4694 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4696 isl_ctx *ctx;
4697 isl_space *space;
4698 isl_local_space *ls;
4699 isl_multi_aff *ma;
4700 isl_aff *aff;
4701 isl_vec *v;
4702 isl_map *insert;
4703 int offset;
4704 int n;
4705 int n_in;
4706 isl_pw_multi_aff *pma;
4707 isl_bool is_set;
4709 is_set = isl_map_is_set(map);
4710 if (is_set < 0)
4711 goto error;
4713 offset = isl_basic_map_offset(hull, isl_dim_out);
4714 ctx = isl_map_get_ctx(map);
4715 space = isl_space_domain(isl_map_get_space(map));
4716 n_in = isl_space_dim(space, isl_dim_set);
4717 n = isl_space_dim(space, isl_dim_all);
4719 v = isl_vec_alloc(ctx, 1 + 1 + n);
4720 if (v) {
4721 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4722 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4724 isl_basic_map_free(hull);
4726 ls = isl_local_space_from_space(isl_space_copy(space));
4727 aff = isl_aff_alloc_vec(ls, v);
4728 aff = isl_aff_floor(aff);
4729 if (is_set) {
4730 isl_space_free(space);
4731 ma = isl_multi_aff_from_aff(aff);
4732 } else {
4733 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4734 ma = isl_multi_aff_range_product(ma,
4735 isl_multi_aff_from_aff(aff));
4738 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4739 map = isl_map_apply_domain(map, insert);
4740 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4741 pma = isl_pw_multi_aff_from_map(map);
4742 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4744 return pma;
4745 error:
4746 isl_map_free(map);
4747 isl_basic_map_free(hull);
4748 return NULL;
4751 /* Is constraint "c" of the form
4753 * e(...) + c1 - m x >= 0
4755 * or
4757 * -e(...) + c2 + m x >= 0
4759 * where m > 1 and e only depends on parameters and input dimemnsions?
4761 * "offset" is the offset of the output dimensions
4762 * "pos" is the position of output dimension x.
4764 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4766 if (isl_int_is_zero(c[offset + d]))
4767 return 0;
4768 if (isl_int_is_one(c[offset + d]))
4769 return 0;
4770 if (isl_int_is_negone(c[offset + d]))
4771 return 0;
4772 if (isl_seq_first_non_zero(c + offset, d) != -1)
4773 return 0;
4774 if (isl_seq_first_non_zero(c + offset + d + 1,
4775 total - (offset + d + 1)) != -1)
4776 return 0;
4777 return 1;
4780 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4782 * As a special case, we first check if there is any pair of constraints,
4783 * shared by all the basic maps in "map" that force a given dimension
4784 * to be equal to the floor of some affine combination of the input dimensions.
4786 * In particular, if we can find two constraints
4788 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4790 * and
4792 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4794 * where m > 1 and e only depends on parameters and input dimemnsions,
4795 * and such that
4797 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4799 * then we know that we can take
4801 * x = floor((e(...) + c1) / m)
4803 * without having to perform any computation.
4805 * Note that we know that
4807 * c1 + c2 >= 1
4809 * If c1 + c2 were 0, then we would have detected an equality during
4810 * simplification. If c1 + c2 were negative, then we would have detected
4811 * a contradiction.
4813 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4814 __isl_take isl_map *map)
4816 int d, dim;
4817 int i, j, n;
4818 int offset, total;
4819 isl_int sum;
4820 isl_basic_map *hull;
4822 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4823 if (!hull)
4824 goto error;
4826 isl_int_init(sum);
4827 dim = isl_map_dim(map, isl_dim_out);
4828 offset = isl_basic_map_offset(hull, isl_dim_out);
4829 total = 1 + isl_basic_map_total_dim(hull);
4830 n = hull->n_ineq;
4831 for (d = 0; d < dim; ++d) {
4832 for (i = 0; i < n; ++i) {
4833 if (!is_potential_div_constraint(hull->ineq[i],
4834 offset, d, total))
4835 continue;
4836 for (j = i + 1; j < n; ++j) {
4837 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4838 hull->ineq[j] + 1, total - 1))
4839 continue;
4840 isl_int_add(sum, hull->ineq[i][0],
4841 hull->ineq[j][0]);
4842 if (isl_int_abs_lt(sum,
4843 hull->ineq[i][offset + d]))
4844 break;
4847 if (j >= n)
4848 continue;
4849 isl_int_clear(sum);
4850 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4851 j = i;
4852 return pw_multi_aff_from_map_div(map, hull, d, j);
4855 isl_int_clear(sum);
4856 isl_basic_map_free(hull);
4857 return pw_multi_aff_from_map_base(map);
4858 error:
4859 isl_map_free(map);
4860 isl_basic_map_free(hull);
4861 return NULL;
4864 /* Given an affine expression
4866 * [A -> B] -> f(A,B)
4868 * construct an isl_multi_aff
4870 * [A -> B] -> B'
4872 * such that dimension "d" in B' is set to "aff" and the remaining
4873 * dimensions are set equal to the corresponding dimensions in B.
4874 * "n_in" is the dimension of the space A.
4875 * "n_out" is the dimension of the space B.
4877 * If "is_set" is set, then the affine expression is of the form
4879 * [B] -> f(B)
4881 * and we construct an isl_multi_aff
4883 * B -> B'
4885 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4886 unsigned n_in, unsigned n_out, int is_set)
4888 int i;
4889 isl_multi_aff *ma;
4890 isl_space *space, *space2;
4891 isl_local_space *ls;
4893 space = isl_aff_get_domain_space(aff);
4894 ls = isl_local_space_from_space(isl_space_copy(space));
4895 space2 = isl_space_copy(space);
4896 if (!is_set)
4897 space2 = isl_space_range(isl_space_unwrap(space2));
4898 space = isl_space_map_from_domain_and_range(space, space2);
4899 ma = isl_multi_aff_alloc(space);
4900 ma = isl_multi_aff_set_aff(ma, d, aff);
4902 for (i = 0; i < n_out; ++i) {
4903 if (i == d)
4904 continue;
4905 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4906 isl_dim_set, n_in + i);
4907 ma = isl_multi_aff_set_aff(ma, i, aff);
4910 isl_local_space_free(ls);
4912 return ma;
4915 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4916 * taking into account that the dimension at position "d" can be written as
4918 * x = m a + f(..) (1)
4920 * where m is equal to "gcd".
4921 * "i" is the index of the equality in "hull" that defines f(..).
4922 * In particular, the equality is of the form
4924 * f(..) - x + m g(existentials) = 0
4926 * or
4928 * -f(..) + x + m g(existentials) = 0
4930 * We basically plug (1) into "map", resulting in a map with "a"
4931 * in the range instead of "x". The corresponding isl_pw_multi_aff
4932 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4934 * Specifically, given the input map
4936 * A -> B
4938 * We first wrap it into a set
4940 * [A -> B]
4942 * and define (1) on top of the corresponding space, resulting in "aff".
4943 * We use this to create an isl_multi_aff that maps the output position "d"
4944 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4945 * We plug this into the wrapped map, unwrap the result and compute the
4946 * corresponding isl_pw_multi_aff.
4947 * The result is an expression
4949 * A -> T(A)
4951 * We adjust that to
4953 * A -> [A -> T(A)]
4955 * so that we can plug that into "aff", after extending the latter to
4956 * a mapping
4958 * [A -> B] -> B'
4961 * If "map" is actually a set, then there is no "A" space, meaning
4962 * that we do not need to perform any wrapping, and that the result
4963 * of the recursive call is of the form
4965 * [T]
4967 * which is plugged into a mapping of the form
4969 * B -> B'
4971 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4972 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4973 isl_int gcd)
4975 isl_set *set;
4976 isl_space *space;
4977 isl_local_space *ls;
4978 isl_aff *aff;
4979 isl_multi_aff *ma;
4980 isl_pw_multi_aff *pma, *id;
4981 unsigned n_in;
4982 unsigned o_out;
4983 unsigned n_out;
4984 isl_bool is_set;
4986 is_set = isl_map_is_set(map);
4987 if (is_set < 0)
4988 goto error;
4990 n_in = isl_basic_map_dim(hull, isl_dim_in);
4991 n_out = isl_basic_map_dim(hull, isl_dim_out);
4992 o_out = isl_basic_map_offset(hull, isl_dim_out);
4994 if (is_set)
4995 set = map;
4996 else
4997 set = isl_map_wrap(map);
4998 space = isl_space_map_from_set(isl_set_get_space(set));
4999 ma = isl_multi_aff_identity(space);
5000 ls = isl_local_space_from_space(isl_set_get_space(set));
5001 aff = isl_aff_alloc(ls);
5002 if (aff) {
5003 isl_int_set_si(aff->v->el[0], 1);
5004 if (isl_int_is_one(hull->eq[i][o_out + d]))
5005 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5006 aff->v->size - 1);
5007 else
5008 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5009 aff->v->size - 1);
5010 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5012 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5013 set = isl_set_preimage_multi_aff(set, ma);
5015 ma = range_map(aff, d, n_in, n_out, is_set);
5017 if (is_set)
5018 map = set;
5019 else
5020 map = isl_set_unwrap(set);
5021 pma = isl_pw_multi_aff_from_map(map);
5023 if (!is_set) {
5024 space = isl_pw_multi_aff_get_domain_space(pma);
5025 space = isl_space_map_from_set(space);
5026 id = isl_pw_multi_aff_identity(space);
5027 pma = isl_pw_multi_aff_range_product(id, pma);
5029 id = isl_pw_multi_aff_from_multi_aff(ma);
5030 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5032 isl_basic_map_free(hull);
5033 return pma;
5034 error:
5035 isl_map_free(map);
5036 isl_basic_map_free(hull);
5037 return NULL;
5040 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5041 * "hull" contains the equalities valid for "map".
5043 * Check if any of the output dimensions is "strided".
5044 * That is, we check if it can be written as
5046 * x = m a + f(..)
5048 * with m greater than 1, a some combination of existentially quantified
5049 * variables and f an expression in the parameters and input dimensions.
5050 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5052 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5053 * special case.
5055 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5056 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5058 int i, j;
5059 unsigned n_out;
5060 unsigned o_out;
5061 unsigned n_div;
5062 unsigned o_div;
5063 isl_int gcd;
5065 n_div = isl_basic_map_dim(hull, isl_dim_div);
5066 o_div = isl_basic_map_offset(hull, isl_dim_div);
5068 if (n_div == 0) {
5069 isl_basic_map_free(hull);
5070 return pw_multi_aff_from_map_check_div(map);
5073 isl_int_init(gcd);
5075 n_out = isl_basic_map_dim(hull, isl_dim_out);
5076 o_out = isl_basic_map_offset(hull, isl_dim_out);
5078 for (i = 0; i < n_out; ++i) {
5079 for (j = 0; j < hull->n_eq; ++j) {
5080 isl_int *eq = hull->eq[j];
5081 isl_pw_multi_aff *res;
5083 if (!isl_int_is_one(eq[o_out + i]) &&
5084 !isl_int_is_negone(eq[o_out + i]))
5085 continue;
5086 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5087 continue;
5088 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5089 n_out - (i + 1)) != -1)
5090 continue;
5091 isl_seq_gcd(eq + o_div, n_div, &gcd);
5092 if (isl_int_is_zero(gcd))
5093 continue;
5094 if (isl_int_is_one(gcd))
5095 continue;
5097 res = pw_multi_aff_from_map_stride(map, hull,
5098 i, j, gcd);
5099 isl_int_clear(gcd);
5100 return res;
5104 isl_int_clear(gcd);
5105 isl_basic_map_free(hull);
5106 return pw_multi_aff_from_map_check_div(map);
5109 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5111 * As a special case, we first check if all output dimensions are uniquely
5112 * defined in terms of the parameters and input dimensions over the entire
5113 * domain. If so, we extract the desired isl_pw_multi_aff directly
5114 * from the affine hull of "map" and its domain.
5116 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5117 * special cases.
5119 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5121 isl_bool sv;
5122 isl_basic_map *hull;
5124 if (!map)
5125 return NULL;
5127 if (isl_map_n_basic_map(map) == 1) {
5128 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5129 hull = isl_basic_map_plain_affine_hull(hull);
5130 sv = isl_basic_map_plain_is_single_valued(hull);
5131 if (sv >= 0 && sv)
5132 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5133 hull);
5134 isl_basic_map_free(hull);
5136 map = isl_map_detect_equalities(map);
5137 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5138 sv = isl_basic_map_plain_is_single_valued(hull);
5139 if (sv >= 0 && sv)
5140 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5141 if (sv >= 0)
5142 return pw_multi_aff_from_map_check_strides(map, hull);
5143 isl_basic_map_free(hull);
5144 isl_map_free(map);
5145 return NULL;
5148 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5150 return isl_pw_multi_aff_from_map(set);
5153 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5154 * add it to *user.
5156 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5158 isl_union_pw_multi_aff **upma = user;
5159 isl_pw_multi_aff *pma;
5161 pma = isl_pw_multi_aff_from_map(map);
5162 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5164 return *upma ? isl_stat_ok : isl_stat_error;
5167 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5168 * domain.
5170 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5171 __isl_take isl_aff *aff)
5173 isl_multi_aff *ma;
5174 isl_pw_multi_aff *pma;
5176 ma = isl_multi_aff_from_aff(aff);
5177 pma = isl_pw_multi_aff_from_multi_aff(ma);
5178 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5181 /* Try and create an isl_union_pw_multi_aff that is equivalent
5182 * to the given isl_union_map.
5183 * The isl_union_map is required to be single-valued in each space.
5184 * Otherwise, an error is produced.
5186 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5187 __isl_take isl_union_map *umap)
5189 isl_space *space;
5190 isl_union_pw_multi_aff *upma;
5192 space = isl_union_map_get_space(umap);
5193 upma = isl_union_pw_multi_aff_empty(space);
5194 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5195 upma = isl_union_pw_multi_aff_free(upma);
5196 isl_union_map_free(umap);
5198 return upma;
5201 /* Try and create an isl_union_pw_multi_aff that is equivalent
5202 * to the given isl_union_set.
5203 * The isl_union_set is required to be a singleton in each space.
5204 * Otherwise, an error is produced.
5206 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5207 __isl_take isl_union_set *uset)
5209 return isl_union_pw_multi_aff_from_union_map(uset);
5212 /* Return the piecewise affine expression "set ? 1 : 0".
5214 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5216 isl_pw_aff *pa;
5217 isl_space *space = isl_set_get_space(set);
5218 isl_local_space *ls = isl_local_space_from_space(space);
5219 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5220 isl_aff *one = isl_aff_zero_on_domain(ls);
5222 one = isl_aff_add_constant_si(one, 1);
5223 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5224 set = isl_set_complement(set);
5225 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5227 return pa;
5230 /* Plug in "subs" for dimension "type", "pos" of "aff".
5232 * Let i be the dimension to replace and let "subs" be of the form
5234 * f/d
5236 * and "aff" of the form
5238 * (a i + g)/m
5240 * The result is
5242 * (a f + d g')/(m d)
5244 * where g' is the result of plugging in "subs" in each of the integer
5245 * divisions in g.
5247 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5248 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5250 isl_ctx *ctx;
5251 isl_int v;
5253 aff = isl_aff_cow(aff);
5254 if (!aff || !subs)
5255 return isl_aff_free(aff);
5257 ctx = isl_aff_get_ctx(aff);
5258 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5259 isl_die(ctx, isl_error_invalid,
5260 "spaces don't match", return isl_aff_free(aff));
5261 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5262 isl_die(ctx, isl_error_unsupported,
5263 "cannot handle divs yet", return isl_aff_free(aff));
5265 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5266 if (!aff->ls)
5267 return isl_aff_free(aff);
5269 aff->v = isl_vec_cow(aff->v);
5270 if (!aff->v)
5271 return isl_aff_free(aff);
5273 pos += isl_local_space_offset(aff->ls, type);
5275 isl_int_init(v);
5276 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5277 aff->v->size, subs->v->size, v);
5278 isl_int_clear(v);
5280 return aff;
5283 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5284 * expressions in "maff".
5286 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5287 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5288 __isl_keep isl_aff *subs)
5290 int i;
5292 maff = isl_multi_aff_cow(maff);
5293 if (!maff || !subs)
5294 return isl_multi_aff_free(maff);
5296 if (type == isl_dim_in)
5297 type = isl_dim_set;
5299 for (i = 0; i < maff->n; ++i) {
5300 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5301 if (!maff->p[i])
5302 return isl_multi_aff_free(maff);
5305 return maff;
5308 /* Plug in "subs" for dimension "type", "pos" of "pma".
5310 * pma is of the form
5312 * A_i(v) -> M_i(v)
5314 * while subs is of the form
5316 * v' = B_j(v) -> S_j
5318 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5319 * has a contribution in the result, in particular
5321 * C_ij(S_j) -> M_i(S_j)
5323 * Note that plugging in S_j in C_ij may also result in an empty set
5324 * and this contribution should simply be discarded.
5326 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5327 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5328 __isl_keep isl_pw_aff *subs)
5330 int i, j, n;
5331 isl_pw_multi_aff *res;
5333 if (!pma || !subs)
5334 return isl_pw_multi_aff_free(pma);
5336 n = pma->n * subs->n;
5337 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5339 for (i = 0; i < pma->n; ++i) {
5340 for (j = 0; j < subs->n; ++j) {
5341 isl_set *common;
5342 isl_multi_aff *res_ij;
5343 int empty;
5345 common = isl_set_intersect(
5346 isl_set_copy(pma->p[i].set),
5347 isl_set_copy(subs->p[j].set));
5348 common = isl_set_substitute(common,
5349 type, pos, subs->p[j].aff);
5350 empty = isl_set_plain_is_empty(common);
5351 if (empty < 0 || empty) {
5352 isl_set_free(common);
5353 if (empty < 0)
5354 goto error;
5355 continue;
5358 res_ij = isl_multi_aff_substitute(
5359 isl_multi_aff_copy(pma->p[i].maff),
5360 type, pos, subs->p[j].aff);
5362 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5366 isl_pw_multi_aff_free(pma);
5367 return res;
5368 error:
5369 isl_pw_multi_aff_free(pma);
5370 isl_pw_multi_aff_free(res);
5371 return NULL;
5374 /* Compute the preimage of a range of dimensions in the affine expression "src"
5375 * under "ma" and put the result in "dst". The number of dimensions in "src"
5376 * that precede the range is given by "n_before". The number of dimensions
5377 * in the range is given by the number of output dimensions of "ma".
5378 * The number of dimensions that follow the range is given by "n_after".
5379 * If "has_denom" is set (to one),
5380 * then "src" and "dst" have an extra initial denominator.
5381 * "n_div_ma" is the number of existentials in "ma"
5382 * "n_div_bset" is the number of existentials in "src"
5383 * The resulting "dst" (which is assumed to have been allocated by
5384 * the caller) contains coefficients for both sets of existentials,
5385 * first those in "ma" and then those in "src".
5386 * f, c1, c2 and g are temporary objects that have been initialized
5387 * by the caller.
5389 * Let src represent the expression
5391 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5393 * and let ma represent the expressions
5395 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5397 * We start out with the following expression for dst:
5399 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5401 * with the multiplication factor f initially equal to 1
5402 * and f \sum_i b_i v_i kept separately.
5403 * For each x_i that we substitute, we multiply the numerator
5404 * (and denominator) of dst by c_1 = m_i and add the numerator
5405 * of the x_i expression multiplied by c_2 = f b_i,
5406 * after removing the common factors of c_1 and c_2.
5407 * The multiplication factor f also needs to be multiplied by c_1
5408 * for the next x_j, j > i.
5410 void isl_seq_preimage(isl_int *dst, isl_int *src,
5411 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5412 int n_div_ma, int n_div_bmap,
5413 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5415 int i;
5416 int n_param, n_in, n_out;
5417 int o_dst, o_src;
5419 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5420 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5421 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5423 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5424 o_dst = o_src = has_denom + 1 + n_param + n_before;
5425 isl_seq_clr(dst + o_dst, n_in);
5426 o_dst += n_in;
5427 o_src += n_out;
5428 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5429 o_dst += n_after;
5430 o_src += n_after;
5431 isl_seq_clr(dst + o_dst, n_div_ma);
5432 o_dst += n_div_ma;
5433 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5435 isl_int_set_si(f, 1);
5437 for (i = 0; i < n_out; ++i) {
5438 int offset = has_denom + 1 + n_param + n_before + i;
5440 if (isl_int_is_zero(src[offset]))
5441 continue;
5442 isl_int_set(c1, ma->p[i]->v->el[0]);
5443 isl_int_mul(c2, f, src[offset]);
5444 isl_int_gcd(g, c1, c2);
5445 isl_int_divexact(c1, c1, g);
5446 isl_int_divexact(c2, c2, g);
5448 isl_int_mul(f, f, c1);
5449 o_dst = has_denom;
5450 o_src = 1;
5451 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5452 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5453 o_dst += 1 + n_param;
5454 o_src += 1 + n_param;
5455 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5456 o_dst += n_before;
5457 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5458 c2, ma->p[i]->v->el + o_src, n_in);
5459 o_dst += n_in;
5460 o_src += n_in;
5461 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5462 o_dst += n_after;
5463 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5464 c2, ma->p[i]->v->el + o_src, n_div_ma);
5465 o_dst += n_div_ma;
5466 o_src += n_div_ma;
5467 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5468 if (has_denom)
5469 isl_int_mul(dst[0], dst[0], c1);
5473 /* Compute the pullback of "aff" by the function represented by "ma".
5474 * In other words, plug in "ma" in "aff". The result is an affine expression
5475 * defined over the domain space of "ma".
5477 * If "aff" is represented by
5479 * (a(p) + b x + c(divs))/d
5481 * and ma is represented by
5483 * x = D(p) + F(y) + G(divs')
5485 * then the result is
5487 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5489 * The divs in the local space of the input are similarly adjusted
5490 * through a call to isl_local_space_preimage_multi_aff.
5492 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5493 __isl_take isl_multi_aff *ma)
5495 isl_aff *res = NULL;
5496 isl_local_space *ls;
5497 int n_div_aff, n_div_ma;
5498 isl_int f, c1, c2, g;
5500 ma = isl_multi_aff_align_divs(ma);
5501 if (!aff || !ma)
5502 goto error;
5504 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5505 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5507 ls = isl_aff_get_domain_local_space(aff);
5508 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5509 res = isl_aff_alloc(ls);
5510 if (!res)
5511 goto error;
5513 isl_int_init(f);
5514 isl_int_init(c1);
5515 isl_int_init(c2);
5516 isl_int_init(g);
5518 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5519 f, c1, c2, g, 1);
5521 isl_int_clear(f);
5522 isl_int_clear(c1);
5523 isl_int_clear(c2);
5524 isl_int_clear(g);
5526 isl_aff_free(aff);
5527 isl_multi_aff_free(ma);
5528 res = isl_aff_normalize(res);
5529 return res;
5530 error:
5531 isl_aff_free(aff);
5532 isl_multi_aff_free(ma);
5533 isl_aff_free(res);
5534 return NULL;
5537 /* Compute the pullback of "aff1" by the function represented by "aff2".
5538 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5539 * defined over the domain space of "aff1".
5541 * The domain of "aff1" should match the range of "aff2", which means
5542 * that it should be single-dimensional.
5544 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5545 __isl_take isl_aff *aff2)
5547 isl_multi_aff *ma;
5549 ma = isl_multi_aff_from_aff(aff2);
5550 return isl_aff_pullback_multi_aff(aff1, ma);
5553 /* Compute the pullback of "ma1" by the function represented by "ma2".
5554 * In other words, plug in "ma2" in "ma1".
5556 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5558 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5559 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5561 int i;
5562 isl_space *space = NULL;
5564 ma2 = isl_multi_aff_align_divs(ma2);
5565 ma1 = isl_multi_aff_cow(ma1);
5566 if (!ma1 || !ma2)
5567 goto error;
5569 space = isl_space_join(isl_multi_aff_get_space(ma2),
5570 isl_multi_aff_get_space(ma1));
5572 for (i = 0; i < ma1->n; ++i) {
5573 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5574 isl_multi_aff_copy(ma2));
5575 if (!ma1->p[i])
5576 goto error;
5579 ma1 = isl_multi_aff_reset_space(ma1, space);
5580 isl_multi_aff_free(ma2);
5581 return ma1;
5582 error:
5583 isl_space_free(space);
5584 isl_multi_aff_free(ma2);
5585 isl_multi_aff_free(ma1);
5586 return NULL;
5589 /* Compute the pullback of "ma1" by the function represented by "ma2".
5590 * In other words, plug in "ma2" in "ma1".
5592 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5593 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5595 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5596 &isl_multi_aff_pullback_multi_aff_aligned);
5599 /* Extend the local space of "dst" to include the divs
5600 * in the local space of "src".
5602 * If "src" does not have any divs or if the local spaces of "dst" and
5603 * "src" are the same, then no extension is required.
5605 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5606 __isl_keep isl_aff *src)
5608 isl_ctx *ctx;
5609 int src_n_div, dst_n_div;
5610 int *exp1 = NULL;
5611 int *exp2 = NULL;
5612 isl_bool equal;
5613 isl_mat *div;
5615 if (!src || !dst)
5616 return isl_aff_free(dst);
5618 ctx = isl_aff_get_ctx(src);
5619 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5620 if (equal < 0)
5621 return isl_aff_free(dst);
5622 if (!equal)
5623 isl_die(ctx, isl_error_invalid,
5624 "spaces don't match", goto error);
5626 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5627 if (src_n_div == 0)
5628 return dst;
5629 equal = isl_local_space_is_equal(src->ls, dst->ls);
5630 if (equal < 0)
5631 return isl_aff_free(dst);
5632 if (equal)
5633 return dst;
5635 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5636 exp1 = isl_alloc_array(ctx, int, src_n_div);
5637 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5638 if (!exp1 || (dst_n_div && !exp2))
5639 goto error;
5641 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5642 dst = isl_aff_expand_divs(dst, div, exp2);
5643 free(exp1);
5644 free(exp2);
5646 return dst;
5647 error:
5648 free(exp1);
5649 free(exp2);
5650 return isl_aff_free(dst);
5653 /* Adjust the local spaces of the affine expressions in "maff"
5654 * such that they all have the save divs.
5656 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5657 __isl_take isl_multi_aff *maff)
5659 int i;
5661 if (!maff)
5662 return NULL;
5663 if (maff->n == 0)
5664 return maff;
5665 maff = isl_multi_aff_cow(maff);
5666 if (!maff)
5667 return NULL;
5669 for (i = 1; i < maff->n; ++i)
5670 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5671 for (i = 1; i < maff->n; ++i) {
5672 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5673 if (!maff->p[i])
5674 return isl_multi_aff_free(maff);
5677 return maff;
5680 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5682 aff = isl_aff_cow(aff);
5683 if (!aff)
5684 return NULL;
5686 aff->ls = isl_local_space_lift(aff->ls);
5687 if (!aff->ls)
5688 return isl_aff_free(aff);
5690 return aff;
5693 /* Lift "maff" to a space with extra dimensions such that the result
5694 * has no more existentially quantified variables.
5695 * If "ls" is not NULL, then *ls is assigned the local space that lies
5696 * at the basis of the lifting applied to "maff".
5698 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5699 __isl_give isl_local_space **ls)
5701 int i;
5702 isl_space *space;
5703 unsigned n_div;
5705 if (ls)
5706 *ls = NULL;
5708 if (!maff)
5709 return NULL;
5711 if (maff->n == 0) {
5712 if (ls) {
5713 isl_space *space = isl_multi_aff_get_domain_space(maff);
5714 *ls = isl_local_space_from_space(space);
5715 if (!*ls)
5716 return isl_multi_aff_free(maff);
5718 return maff;
5721 maff = isl_multi_aff_cow(maff);
5722 maff = isl_multi_aff_align_divs(maff);
5723 if (!maff)
5724 return NULL;
5726 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5727 space = isl_multi_aff_get_space(maff);
5728 space = isl_space_lift(isl_space_domain(space), n_div);
5729 space = isl_space_extend_domain_with_range(space,
5730 isl_multi_aff_get_space(maff));
5731 if (!space)
5732 return isl_multi_aff_free(maff);
5733 isl_space_free(maff->space);
5734 maff->space = space;
5736 if (ls) {
5737 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5738 if (!*ls)
5739 return isl_multi_aff_free(maff);
5742 for (i = 0; i < maff->n; ++i) {
5743 maff->p[i] = isl_aff_lift(maff->p[i]);
5744 if (!maff->p[i])
5745 goto error;
5748 return maff;
5749 error:
5750 if (ls)
5751 isl_local_space_free(*ls);
5752 return isl_multi_aff_free(maff);
5756 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5758 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5759 __isl_keep isl_pw_multi_aff *pma, int pos)
5761 int i;
5762 int n_out;
5763 isl_space *space;
5764 isl_pw_aff *pa;
5766 if (!pma)
5767 return NULL;
5769 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5770 if (pos < 0 || pos >= n_out)
5771 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5772 "index out of bounds", return NULL);
5774 space = isl_pw_multi_aff_get_space(pma);
5775 space = isl_space_drop_dims(space, isl_dim_out,
5776 pos + 1, n_out - pos - 1);
5777 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5779 pa = isl_pw_aff_alloc_size(space, pma->n);
5780 for (i = 0; i < pma->n; ++i) {
5781 isl_aff *aff;
5782 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5783 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5786 return pa;
5789 /* Return an isl_pw_multi_aff with the given "set" as domain and
5790 * an unnamed zero-dimensional range.
5792 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5793 __isl_take isl_set *set)
5795 isl_multi_aff *ma;
5796 isl_space *space;
5798 space = isl_set_get_space(set);
5799 space = isl_space_from_domain(space);
5800 ma = isl_multi_aff_zero(space);
5801 return isl_pw_multi_aff_alloc(set, ma);
5804 /* Add an isl_pw_multi_aff with the given "set" as domain and
5805 * an unnamed zero-dimensional range to *user.
5807 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5808 void *user)
5810 isl_union_pw_multi_aff **upma = user;
5811 isl_pw_multi_aff *pma;
5813 pma = isl_pw_multi_aff_from_domain(set);
5814 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5816 return isl_stat_ok;
5819 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5820 * an unnamed zero-dimensional range.
5822 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5823 __isl_take isl_union_set *uset)
5825 isl_space *space;
5826 isl_union_pw_multi_aff *upma;
5828 if (!uset)
5829 return NULL;
5831 space = isl_union_set_get_space(uset);
5832 upma = isl_union_pw_multi_aff_empty(space);
5834 if (isl_union_set_foreach_set(uset,
5835 &add_pw_multi_aff_from_domain, &upma) < 0)
5836 goto error;
5838 isl_union_set_free(uset);
5839 return upma;
5840 error:
5841 isl_union_set_free(uset);
5842 isl_union_pw_multi_aff_free(upma);
5843 return NULL;
5846 /* Convert "pma" to an isl_map and add it to *umap.
5848 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5849 void *user)
5851 isl_union_map **umap = user;
5852 isl_map *map;
5854 map = isl_map_from_pw_multi_aff(pma);
5855 *umap = isl_union_map_add_map(*umap, map);
5857 return isl_stat_ok;
5860 /* Construct a union map mapping the domain of the union
5861 * piecewise multi-affine expression to its range, with each dimension
5862 * in the range equated to the corresponding affine expression on its cell.
5864 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5865 __isl_take isl_union_pw_multi_aff *upma)
5867 isl_space *space;
5868 isl_union_map *umap;
5870 if (!upma)
5871 return NULL;
5873 space = isl_union_pw_multi_aff_get_space(upma);
5874 umap = isl_union_map_empty(space);
5876 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5877 &map_from_pw_multi_aff, &umap) < 0)
5878 goto error;
5880 isl_union_pw_multi_aff_free(upma);
5881 return umap;
5882 error:
5883 isl_union_pw_multi_aff_free(upma);
5884 isl_union_map_free(umap);
5885 return NULL;
5888 /* Local data for bin_entry and the callback "fn".
5890 struct isl_union_pw_multi_aff_bin_data {
5891 isl_union_pw_multi_aff *upma2;
5892 isl_union_pw_multi_aff *res;
5893 isl_pw_multi_aff *pma;
5894 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5897 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5898 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5900 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5902 struct isl_union_pw_multi_aff_bin_data *data = user;
5903 isl_stat r;
5905 data->pma = pma;
5906 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5907 data->fn, data);
5908 isl_pw_multi_aff_free(pma);
5910 return r;
5913 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5914 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5915 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5916 * as *entry. The callback should adjust data->res if desired.
5918 static __isl_give isl_union_pw_multi_aff *bin_op(
5919 __isl_take isl_union_pw_multi_aff *upma1,
5920 __isl_take isl_union_pw_multi_aff *upma2,
5921 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5923 isl_space *space;
5924 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5926 space = isl_union_pw_multi_aff_get_space(upma2);
5927 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5928 space = isl_union_pw_multi_aff_get_space(upma1);
5929 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5931 if (!upma1 || !upma2)
5932 goto error;
5934 data.upma2 = upma2;
5935 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5936 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5937 &bin_entry, &data) < 0)
5938 goto error;
5940 isl_union_pw_multi_aff_free(upma1);
5941 isl_union_pw_multi_aff_free(upma2);
5942 return data.res;
5943 error:
5944 isl_union_pw_multi_aff_free(upma1);
5945 isl_union_pw_multi_aff_free(upma2);
5946 isl_union_pw_multi_aff_free(data.res);
5947 return NULL;
5950 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5951 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5953 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5954 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5956 isl_space *space;
5958 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5959 isl_pw_multi_aff_get_space(pma2));
5960 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5961 &isl_multi_aff_range_product);
5964 /* Given two isl_pw_multi_affs A -> B and C -> D,
5965 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5967 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5968 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5970 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5971 &pw_multi_aff_range_product);
5974 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5975 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5977 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5978 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5980 isl_space *space;
5982 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5983 isl_pw_multi_aff_get_space(pma2));
5984 space = isl_space_flatten_range(space);
5985 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5986 &isl_multi_aff_flat_range_product);
5989 /* Given two isl_pw_multi_affs A -> B and C -> D,
5990 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5992 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5993 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5995 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5996 &pw_multi_aff_flat_range_product);
5999 /* If data->pma and "pma2" have the same domain space, then compute
6000 * their flat range product and the result to data->res.
6002 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6003 void *user)
6005 struct isl_union_pw_multi_aff_bin_data *data = user;
6007 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6008 pma2->dim, isl_dim_in)) {
6009 isl_pw_multi_aff_free(pma2);
6010 return isl_stat_ok;
6013 pma2 = isl_pw_multi_aff_flat_range_product(
6014 isl_pw_multi_aff_copy(data->pma), pma2);
6016 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6018 return isl_stat_ok;
6021 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6022 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6024 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6025 __isl_take isl_union_pw_multi_aff *upma1,
6026 __isl_take isl_union_pw_multi_aff *upma2)
6028 return bin_op(upma1, upma2, &flat_range_product_entry);
6031 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6032 * The parameters are assumed to have been aligned.
6034 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6035 * except that it works on two different isl_pw_* types.
6037 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6038 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6039 __isl_take isl_pw_aff *pa)
6041 int i, j, n;
6042 isl_pw_multi_aff *res = NULL;
6044 if (!pma || !pa)
6045 goto error;
6047 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6048 pa->dim, isl_dim_in))
6049 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6050 "domains don't match", goto error);
6051 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6052 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6053 "index out of bounds", goto error);
6055 n = pma->n * pa->n;
6056 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6058 for (i = 0; i < pma->n; ++i) {
6059 for (j = 0; j < pa->n; ++j) {
6060 isl_set *common;
6061 isl_multi_aff *res_ij;
6062 int empty;
6064 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6065 isl_set_copy(pa->p[j].set));
6066 empty = isl_set_plain_is_empty(common);
6067 if (empty < 0 || empty) {
6068 isl_set_free(common);
6069 if (empty < 0)
6070 goto error;
6071 continue;
6074 res_ij = isl_multi_aff_set_aff(
6075 isl_multi_aff_copy(pma->p[i].maff), pos,
6076 isl_aff_copy(pa->p[j].aff));
6077 res_ij = isl_multi_aff_gist(res_ij,
6078 isl_set_copy(common));
6080 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6084 isl_pw_multi_aff_free(pma);
6085 isl_pw_aff_free(pa);
6086 return res;
6087 error:
6088 isl_pw_multi_aff_free(pma);
6089 isl_pw_aff_free(pa);
6090 return isl_pw_multi_aff_free(res);
6093 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6095 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6096 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6097 __isl_take isl_pw_aff *pa)
6099 isl_bool equal_params;
6101 if (!pma || !pa)
6102 goto error;
6103 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6104 if (equal_params < 0)
6105 goto error;
6106 if (equal_params)
6107 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6108 if (!isl_space_has_named_params(pma->dim) ||
6109 !isl_space_has_named_params(pa->dim))
6110 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6111 "unaligned unnamed parameters", goto error);
6112 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6113 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6114 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6115 error:
6116 isl_pw_multi_aff_free(pma);
6117 isl_pw_aff_free(pa);
6118 return NULL;
6121 /* Do the parameters of "pa" match those of "space"?
6123 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6124 __isl_keep isl_space *space)
6126 isl_space *pa_space;
6127 isl_bool match;
6129 if (!pa || !space)
6130 return isl_bool_error;
6132 pa_space = isl_pw_aff_get_space(pa);
6134 match = isl_space_has_equal_params(space, pa_space);
6136 isl_space_free(pa_space);
6137 return match;
6140 /* Check that the domain space of "pa" matches "space".
6142 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6143 __isl_keep isl_space *space)
6145 isl_space *pa_space;
6146 isl_bool match;
6148 if (!pa || !space)
6149 return isl_stat_error;
6151 pa_space = isl_pw_aff_get_space(pa);
6153 match = isl_space_has_equal_params(space, pa_space);
6154 if (match < 0)
6155 goto error;
6156 if (!match)
6157 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6158 "parameters don't match", goto error);
6159 match = isl_space_tuple_is_equal(space, isl_dim_in,
6160 pa_space, isl_dim_in);
6161 if (match < 0)
6162 goto error;
6163 if (!match)
6164 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6165 "domains don't match", goto error);
6166 isl_space_free(pa_space);
6167 return isl_stat_ok;
6168 error:
6169 isl_space_free(pa_space);
6170 return isl_stat_error;
6173 #undef BASE
6174 #define BASE pw_aff
6175 #undef DOMBASE
6176 #define DOMBASE set
6178 #include <isl_multi_templ.c>
6179 #include <isl_multi_apply_set.c>
6180 #include <isl_multi_coalesce.c>
6181 #include <isl_multi_gist.c>
6182 #include <isl_multi_hash.c>
6183 #include <isl_multi_intersect.c>
6185 /* Scale the elements of "pma" by the corresponding elements of "mv".
6187 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6188 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6190 int i;
6191 isl_bool equal_params;
6193 pma = isl_pw_multi_aff_cow(pma);
6194 if (!pma || !mv)
6195 goto error;
6196 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6197 mv->space, isl_dim_set))
6198 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6199 "spaces don't match", goto error);
6200 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6201 if (equal_params < 0)
6202 goto error;
6203 if (!equal_params) {
6204 pma = isl_pw_multi_aff_align_params(pma,
6205 isl_multi_val_get_space(mv));
6206 mv = isl_multi_val_align_params(mv,
6207 isl_pw_multi_aff_get_space(pma));
6208 if (!pma || !mv)
6209 goto error;
6212 for (i = 0; i < pma->n; ++i) {
6213 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6214 isl_multi_val_copy(mv));
6215 if (!pma->p[i].maff)
6216 goto error;
6219 isl_multi_val_free(mv);
6220 return pma;
6221 error:
6222 isl_multi_val_free(mv);
6223 isl_pw_multi_aff_free(pma);
6224 return NULL;
6227 /* This function is called for each entry of an isl_union_pw_multi_aff.
6228 * If the space of the entry matches that of data->mv,
6229 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6230 * Otherwise, return an empty isl_pw_multi_aff.
6232 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6233 __isl_take isl_pw_multi_aff *pma, void *user)
6235 isl_multi_val *mv = user;
6237 if (!pma)
6238 return NULL;
6239 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6240 mv->space, isl_dim_set)) {
6241 isl_space *space = isl_pw_multi_aff_get_space(pma);
6242 isl_pw_multi_aff_free(pma);
6243 return isl_pw_multi_aff_empty(space);
6246 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6249 /* Scale the elements of "upma" by the corresponding elements of "mv",
6250 * for those entries that match the space of "mv".
6252 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6253 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6255 upma = isl_union_pw_multi_aff_align_params(upma,
6256 isl_multi_val_get_space(mv));
6257 mv = isl_multi_val_align_params(mv,
6258 isl_union_pw_multi_aff_get_space(upma));
6259 if (!upma || !mv)
6260 goto error;
6262 return isl_union_pw_multi_aff_transform(upma,
6263 &union_pw_multi_aff_scale_multi_val_entry, mv);
6265 isl_multi_val_free(mv);
6266 return upma;
6267 error:
6268 isl_multi_val_free(mv);
6269 isl_union_pw_multi_aff_free(upma);
6270 return NULL;
6273 /* Construct and return a piecewise multi affine expression
6274 * in the given space with value zero in each of the output dimensions and
6275 * a universe domain.
6277 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6279 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6282 /* Construct and return a piecewise multi affine expression
6283 * that is equal to the given piecewise affine expression.
6285 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6286 __isl_take isl_pw_aff *pa)
6288 int i;
6289 isl_space *space;
6290 isl_pw_multi_aff *pma;
6292 if (!pa)
6293 return NULL;
6295 space = isl_pw_aff_get_space(pa);
6296 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6298 for (i = 0; i < pa->n; ++i) {
6299 isl_set *set;
6300 isl_multi_aff *ma;
6302 set = isl_set_copy(pa->p[i].set);
6303 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6304 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6307 isl_pw_aff_free(pa);
6308 return pma;
6311 /* Construct a set or map mapping the shared (parameter) domain
6312 * of the piecewise affine expressions to the range of "mpa"
6313 * with each dimension in the range equated to the
6314 * corresponding piecewise affine expression.
6316 static __isl_give isl_map *map_from_multi_pw_aff(
6317 __isl_take isl_multi_pw_aff *mpa)
6319 int i;
6320 isl_space *space;
6321 isl_map *map;
6323 if (!mpa)
6324 return NULL;
6326 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6327 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6328 "invalid space", goto error);
6330 space = isl_multi_pw_aff_get_domain_space(mpa);
6331 map = isl_map_universe(isl_space_from_domain(space));
6333 for (i = 0; i < mpa->n; ++i) {
6334 isl_pw_aff *pa;
6335 isl_map *map_i;
6337 pa = isl_pw_aff_copy(mpa->p[i]);
6338 map_i = map_from_pw_aff(pa);
6340 map = isl_map_flat_range_product(map, map_i);
6343 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6345 isl_multi_pw_aff_free(mpa);
6346 return map;
6347 error:
6348 isl_multi_pw_aff_free(mpa);
6349 return NULL;
6352 /* Construct a map mapping the shared domain
6353 * of the piecewise affine expressions to the range of "mpa"
6354 * with each dimension in the range equated to the
6355 * corresponding piecewise affine expression.
6357 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6359 if (!mpa)
6360 return NULL;
6361 if (isl_space_is_set(mpa->space))
6362 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6363 "space of input is not a map", goto error);
6365 return map_from_multi_pw_aff(mpa);
6366 error:
6367 isl_multi_pw_aff_free(mpa);
6368 return NULL;
6371 /* Construct a set mapping the shared parameter domain
6372 * of the piecewise affine expressions to the space of "mpa"
6373 * with each dimension in the range equated to the
6374 * corresponding piecewise affine expression.
6376 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6378 if (!mpa)
6379 return NULL;
6380 if (!isl_space_is_set(mpa->space))
6381 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6382 "space of input is not a set", goto error);
6384 return map_from_multi_pw_aff(mpa);
6385 error:
6386 isl_multi_pw_aff_free(mpa);
6387 return NULL;
6390 /* Construct and return a piecewise multi affine expression
6391 * that is equal to the given multi piecewise affine expression
6392 * on the shared domain of the piecewise affine expressions.
6394 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6395 __isl_take isl_multi_pw_aff *mpa)
6397 int i;
6398 isl_space *space;
6399 isl_pw_aff *pa;
6400 isl_pw_multi_aff *pma;
6402 if (!mpa)
6403 return NULL;
6405 space = isl_multi_pw_aff_get_space(mpa);
6407 if (mpa->n == 0) {
6408 isl_multi_pw_aff_free(mpa);
6409 return isl_pw_multi_aff_zero(space);
6412 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6413 pma = isl_pw_multi_aff_from_pw_aff(pa);
6415 for (i = 1; i < mpa->n; ++i) {
6416 isl_pw_multi_aff *pma_i;
6418 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6419 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6420 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6423 pma = isl_pw_multi_aff_reset_space(pma, space);
6425 isl_multi_pw_aff_free(mpa);
6426 return pma;
6429 /* Construct and return a multi piecewise affine expression
6430 * that is equal to the given multi affine expression.
6432 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6433 __isl_take isl_multi_aff *ma)
6435 int i, n;
6436 isl_multi_pw_aff *mpa;
6438 if (!ma)
6439 return NULL;
6441 n = isl_multi_aff_dim(ma, isl_dim_out);
6442 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6444 for (i = 0; i < n; ++i) {
6445 isl_pw_aff *pa;
6447 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6448 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6451 isl_multi_aff_free(ma);
6452 return mpa;
6455 /* Construct and return a multi piecewise affine expression
6456 * that is equal to the given piecewise multi affine expression.
6458 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6459 __isl_take isl_pw_multi_aff *pma)
6461 int i, n;
6462 isl_space *space;
6463 isl_multi_pw_aff *mpa;
6465 if (!pma)
6466 return NULL;
6468 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6469 space = isl_pw_multi_aff_get_space(pma);
6470 mpa = isl_multi_pw_aff_alloc(space);
6472 for (i = 0; i < n; ++i) {
6473 isl_pw_aff *pa;
6475 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6476 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6479 isl_pw_multi_aff_free(pma);
6480 return mpa;
6483 /* Do "pa1" and "pa2" represent the same function?
6485 * We first check if they are obviously equal.
6486 * If not, we convert them to maps and check if those are equal.
6488 * If "pa1" or "pa2" contain any NaNs, then they are considered
6489 * not to be the same. A NaN is not equal to anything, not even
6490 * to another NaN.
6492 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6493 __isl_keep isl_pw_aff *pa2)
6495 isl_bool equal;
6496 isl_bool has_nan;
6497 isl_map *map1, *map2;
6499 if (!pa1 || !pa2)
6500 return isl_bool_error;
6502 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6503 if (equal < 0 || equal)
6504 return equal;
6505 has_nan = either_involves_nan(pa1, pa2);
6506 if (has_nan < 0)
6507 return isl_bool_error;
6508 if (has_nan)
6509 return isl_bool_false;
6511 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6512 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6513 equal = isl_map_is_equal(map1, map2);
6514 isl_map_free(map1);
6515 isl_map_free(map2);
6517 return equal;
6520 /* Do "mpa1" and "mpa2" represent the same function?
6522 * Note that we cannot convert the entire isl_multi_pw_aff
6523 * to a map because the domains of the piecewise affine expressions
6524 * may not be the same.
6526 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6527 __isl_keep isl_multi_pw_aff *mpa2)
6529 int i;
6530 isl_bool equal, equal_params;
6532 if (!mpa1 || !mpa2)
6533 return isl_bool_error;
6535 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6536 if (equal_params < 0)
6537 return isl_bool_error;
6538 if (!equal_params) {
6539 if (!isl_space_has_named_params(mpa1->space))
6540 return isl_bool_false;
6541 if (!isl_space_has_named_params(mpa2->space))
6542 return isl_bool_false;
6543 mpa1 = isl_multi_pw_aff_copy(mpa1);
6544 mpa2 = isl_multi_pw_aff_copy(mpa2);
6545 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6546 isl_multi_pw_aff_get_space(mpa2));
6547 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6548 isl_multi_pw_aff_get_space(mpa1));
6549 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6550 isl_multi_pw_aff_free(mpa1);
6551 isl_multi_pw_aff_free(mpa2);
6552 return equal;
6555 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6556 if (equal < 0 || !equal)
6557 return equal;
6559 for (i = 0; i < mpa1->n; ++i) {
6560 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6561 if (equal < 0 || !equal)
6562 return equal;
6565 return isl_bool_true;
6568 /* Do "pma1" and "pma2" represent the same function?
6570 * First check if they are obviously equal.
6571 * If not, then convert them to maps and check if those are equal.
6573 * If "pa1" or "pa2" contain any NaNs, then they are considered
6574 * not to be the same. A NaN is not equal to anything, not even
6575 * to another NaN.
6577 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6578 __isl_keep isl_pw_multi_aff *pma2)
6580 isl_bool equal;
6581 isl_bool has_nan;
6582 isl_map *map1, *map2;
6584 if (!pma1 || !pma2)
6585 return isl_bool_error;
6587 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6588 if (equal < 0 || equal)
6589 return equal;
6590 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6591 if (has_nan >= 0 && !has_nan)
6592 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6593 if (has_nan < 0 || has_nan)
6594 return isl_bool_not(has_nan);
6596 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6597 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6598 equal = isl_map_is_equal(map1, map2);
6599 isl_map_free(map1);
6600 isl_map_free(map2);
6602 return equal;
6605 /* Compute the pullback of "mpa" by the function represented by "ma".
6606 * In other words, plug in "ma" in "mpa".
6608 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6610 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6611 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6613 int i;
6614 isl_space *space = NULL;
6616 mpa = isl_multi_pw_aff_cow(mpa);
6617 if (!mpa || !ma)
6618 goto error;
6620 space = isl_space_join(isl_multi_aff_get_space(ma),
6621 isl_multi_pw_aff_get_space(mpa));
6622 if (!space)
6623 goto error;
6625 for (i = 0; i < mpa->n; ++i) {
6626 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6627 isl_multi_aff_copy(ma));
6628 if (!mpa->p[i])
6629 goto error;
6632 isl_multi_aff_free(ma);
6633 isl_space_free(mpa->space);
6634 mpa->space = space;
6635 return mpa;
6636 error:
6637 isl_space_free(space);
6638 isl_multi_pw_aff_free(mpa);
6639 isl_multi_aff_free(ma);
6640 return NULL;
6643 /* Compute the pullback of "mpa" by the function represented by "ma".
6644 * In other words, plug in "ma" in "mpa".
6646 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6647 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6649 isl_bool equal_params;
6651 if (!mpa || !ma)
6652 goto error;
6653 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6654 if (equal_params < 0)
6655 goto error;
6656 if (equal_params)
6657 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6658 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6659 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6660 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6661 error:
6662 isl_multi_pw_aff_free(mpa);
6663 isl_multi_aff_free(ma);
6664 return NULL;
6667 /* Compute the pullback of "mpa" by the function represented by "pma".
6668 * In other words, plug in "pma" in "mpa".
6670 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6672 static __isl_give isl_multi_pw_aff *
6673 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6674 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6676 int i;
6677 isl_space *space = NULL;
6679 mpa = isl_multi_pw_aff_cow(mpa);
6680 if (!mpa || !pma)
6681 goto error;
6683 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6684 isl_multi_pw_aff_get_space(mpa));
6686 for (i = 0; i < mpa->n; ++i) {
6687 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6688 isl_pw_multi_aff_copy(pma));
6689 if (!mpa->p[i])
6690 goto error;
6693 isl_pw_multi_aff_free(pma);
6694 isl_space_free(mpa->space);
6695 mpa->space = space;
6696 return mpa;
6697 error:
6698 isl_space_free(space);
6699 isl_multi_pw_aff_free(mpa);
6700 isl_pw_multi_aff_free(pma);
6701 return NULL;
6704 /* Compute the pullback of "mpa" by the function represented by "pma".
6705 * In other words, plug in "pma" in "mpa".
6707 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6708 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6710 isl_bool equal_params;
6712 if (!mpa || !pma)
6713 goto error;
6714 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6715 if (equal_params < 0)
6716 goto error;
6717 if (equal_params)
6718 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6719 mpa = isl_multi_pw_aff_align_params(mpa,
6720 isl_pw_multi_aff_get_space(pma));
6721 pma = isl_pw_multi_aff_align_params(pma,
6722 isl_multi_pw_aff_get_space(mpa));
6723 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6724 error:
6725 isl_multi_pw_aff_free(mpa);
6726 isl_pw_multi_aff_free(pma);
6727 return NULL;
6730 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6731 * with the domain of "aff". The domain of the result is the same
6732 * as that of "mpa".
6733 * "mpa" and "aff" are assumed to have been aligned.
6735 * We first extract the parametric constant from "aff", defined
6736 * over the correct domain.
6737 * Then we add the appropriate combinations of the members of "mpa".
6738 * Finally, we add the integer divisions through recursive calls.
6740 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6741 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6743 int i, n_in, n_div;
6744 isl_space *space;
6745 isl_val *v;
6746 isl_pw_aff *pa;
6747 isl_aff *tmp;
6749 n_in = isl_aff_dim(aff, isl_dim_in);
6750 n_div = isl_aff_dim(aff, isl_dim_div);
6752 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6753 tmp = isl_aff_copy(aff);
6754 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6755 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6756 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6757 isl_space_dim(space, isl_dim_set));
6758 tmp = isl_aff_reset_domain_space(tmp, space);
6759 pa = isl_pw_aff_from_aff(tmp);
6761 for (i = 0; i < n_in; ++i) {
6762 isl_pw_aff *pa_i;
6764 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6765 continue;
6766 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6767 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6768 pa_i = isl_pw_aff_scale_val(pa_i, v);
6769 pa = isl_pw_aff_add(pa, pa_i);
6772 for (i = 0; i < n_div; ++i) {
6773 isl_aff *div;
6774 isl_pw_aff *pa_i;
6776 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6777 continue;
6778 div = isl_aff_get_div(aff, i);
6779 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6780 isl_multi_pw_aff_copy(mpa), div);
6781 pa_i = isl_pw_aff_floor(pa_i);
6782 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6783 pa_i = isl_pw_aff_scale_val(pa_i, v);
6784 pa = isl_pw_aff_add(pa, pa_i);
6787 isl_multi_pw_aff_free(mpa);
6788 isl_aff_free(aff);
6790 return pa;
6793 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6794 * with the domain of "aff". The domain of the result is the same
6795 * as that of "mpa".
6797 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6798 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6800 isl_bool equal_params;
6802 if (!aff || !mpa)
6803 goto error;
6804 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6805 if (equal_params < 0)
6806 goto error;
6807 if (equal_params)
6808 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6810 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6811 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6813 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6814 error:
6815 isl_aff_free(aff);
6816 isl_multi_pw_aff_free(mpa);
6817 return NULL;
6820 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6821 * with the domain of "pa". The domain of the result is the same
6822 * as that of "mpa".
6823 * "mpa" and "pa" are assumed to have been aligned.
6825 * We consider each piece in turn. Note that the domains of the
6826 * pieces are assumed to be disjoint and they remain disjoint
6827 * after taking the preimage (over the same function).
6829 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6830 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6832 isl_space *space;
6833 isl_pw_aff *res;
6834 int i;
6836 if (!mpa || !pa)
6837 goto error;
6839 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6840 isl_pw_aff_get_space(pa));
6841 res = isl_pw_aff_empty(space);
6843 for (i = 0; i < pa->n; ++i) {
6844 isl_pw_aff *pa_i;
6845 isl_set *domain;
6847 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6848 isl_multi_pw_aff_copy(mpa),
6849 isl_aff_copy(pa->p[i].aff));
6850 domain = isl_set_copy(pa->p[i].set);
6851 domain = isl_set_preimage_multi_pw_aff(domain,
6852 isl_multi_pw_aff_copy(mpa));
6853 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6854 res = isl_pw_aff_add_disjoint(res, pa_i);
6857 isl_pw_aff_free(pa);
6858 isl_multi_pw_aff_free(mpa);
6859 return res;
6860 error:
6861 isl_pw_aff_free(pa);
6862 isl_multi_pw_aff_free(mpa);
6863 return NULL;
6866 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6867 * with the domain of "pa". The domain of the result is the same
6868 * as that of "mpa".
6870 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6871 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6873 isl_bool equal_params;
6875 if (!pa || !mpa)
6876 goto error;
6877 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6878 if (equal_params < 0)
6879 goto error;
6880 if (equal_params)
6881 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6883 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6884 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6886 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6887 error:
6888 isl_pw_aff_free(pa);
6889 isl_multi_pw_aff_free(mpa);
6890 return NULL;
6893 /* Compute the pullback of "pa" by the function represented by "mpa".
6894 * In other words, plug in "mpa" in "pa".
6895 * "pa" and "mpa" are assumed to have been aligned.
6897 * The pullback is computed by applying "pa" to "mpa".
6899 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6900 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6902 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6905 /* Compute the pullback of "pa" by the function represented by "mpa".
6906 * In other words, plug in "mpa" in "pa".
6908 * The pullback is computed by applying "pa" to "mpa".
6910 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6911 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6913 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6916 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6917 * In other words, plug in "mpa2" in "mpa1".
6919 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6921 * We pullback each member of "mpa1" in turn.
6923 static __isl_give isl_multi_pw_aff *
6924 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6925 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6927 int i;
6928 isl_space *space = NULL;
6930 mpa1 = isl_multi_pw_aff_cow(mpa1);
6931 if (!mpa1 || !mpa2)
6932 goto error;
6934 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6935 isl_multi_pw_aff_get_space(mpa1));
6937 for (i = 0; i < mpa1->n; ++i) {
6938 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6939 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6940 if (!mpa1->p[i])
6941 goto error;
6944 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6946 isl_multi_pw_aff_free(mpa2);
6947 return mpa1;
6948 error:
6949 isl_space_free(space);
6950 isl_multi_pw_aff_free(mpa1);
6951 isl_multi_pw_aff_free(mpa2);
6952 return NULL;
6955 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6956 * In other words, plug in "mpa2" in "mpa1".
6958 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6959 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6961 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6962 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6965 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6966 * of "mpa1" and "mpa2" live in the same space, construct map space
6967 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6968 * with this map space as extract argument.
6970 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6971 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6972 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6973 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6975 int match;
6976 isl_space *space1, *space2;
6977 isl_map *res;
6979 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6980 isl_multi_pw_aff_get_space(mpa2));
6981 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6982 isl_multi_pw_aff_get_space(mpa1));
6983 if (!mpa1 || !mpa2)
6984 goto error;
6985 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6986 mpa2->space, isl_dim_out);
6987 if (match < 0)
6988 goto error;
6989 if (!match)
6990 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6991 "range spaces don't match", goto error);
6992 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6993 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6994 space1 = isl_space_map_from_domain_and_range(space1, space2);
6996 res = order(mpa1, mpa2, space1);
6997 isl_multi_pw_aff_free(mpa1);
6998 isl_multi_pw_aff_free(mpa2);
6999 return res;
7000 error:
7001 isl_multi_pw_aff_free(mpa1);
7002 isl_multi_pw_aff_free(mpa2);
7003 return NULL;
7006 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7007 * where the function values are equal. "space" is the space of the result.
7008 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7010 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7011 * in the sequences are equal.
7013 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7014 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7015 __isl_take isl_space *space)
7017 int i, n;
7018 isl_map *res;
7020 res = isl_map_universe(space);
7022 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7023 for (i = 0; i < n; ++i) {
7024 isl_pw_aff *pa1, *pa2;
7025 isl_map *map;
7027 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7028 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7029 map = isl_pw_aff_eq_map(pa1, pa2);
7030 res = isl_map_intersect(res, map);
7033 return res;
7036 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7037 * where the function values are equal.
7039 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7040 __isl_take isl_multi_pw_aff *mpa2)
7042 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7043 &isl_multi_pw_aff_eq_map_on_space);
7046 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7047 * where the function values of "mpa1" is lexicographically satisfies "base"
7048 * compared to that of "mpa2". "space" is the space of the result.
7049 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7051 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7052 * if its i-th element satisfies "base" when compared to
7053 * the i-th element of "mpa2" while all previous elements are
7054 * pairwise equal.
7056 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7057 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7058 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7059 __isl_take isl_pw_aff *pa2),
7060 __isl_take isl_space *space)
7062 int i, n;
7063 isl_map *res, *rest;
7065 res = isl_map_empty(isl_space_copy(space));
7066 rest = isl_map_universe(space);
7068 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7069 for (i = 0; i < n; ++i) {
7070 isl_pw_aff *pa1, *pa2;
7071 isl_map *map;
7073 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7074 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7075 map = base(pa1, pa2);
7076 map = isl_map_intersect(map, isl_map_copy(rest));
7077 res = isl_map_union(res, map);
7079 if (i == n - 1)
7080 continue;
7082 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7083 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7084 map = isl_pw_aff_eq_map(pa1, pa2);
7085 rest = isl_map_intersect(rest, map);
7088 isl_map_free(rest);
7089 return res;
7092 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7093 * where the function value of "mpa1" is lexicographically less than that
7094 * of "mpa2". "space" is the space of the result.
7095 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7097 * "mpa1" is less than "mpa2" if its i-th element is smaller
7098 * than the i-th element of "mpa2" while all previous elements are
7099 * pairwise equal.
7101 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7102 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7103 __isl_take isl_space *space)
7105 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7106 &isl_pw_aff_lt_map, space);
7109 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7110 * where the function value of "mpa1" is lexicographically less than that
7111 * of "mpa2".
7113 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7114 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7116 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7117 &isl_multi_pw_aff_lex_lt_map_on_space);
7120 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7121 * where the function value of "mpa1" is lexicographically greater than that
7122 * of "mpa2". "space" is the space of the result.
7123 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7125 * "mpa1" is greater than "mpa2" if its i-th element is greater
7126 * than the i-th element of "mpa2" while all previous elements are
7127 * pairwise equal.
7129 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7130 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7131 __isl_take isl_space *space)
7133 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7134 &isl_pw_aff_gt_map, space);
7137 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7138 * where the function value of "mpa1" is lexicographically greater than that
7139 * of "mpa2".
7141 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7142 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7144 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7145 &isl_multi_pw_aff_lex_gt_map_on_space);
7148 /* Compare two isl_affs.
7150 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7151 * than "aff2" and 0 if they are equal.
7153 * The order is fairly arbitrary. We do consider expressions that only involve
7154 * earlier dimensions as "smaller".
7156 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7158 int cmp;
7159 int last1, last2;
7161 if (aff1 == aff2)
7162 return 0;
7164 if (!aff1)
7165 return -1;
7166 if (!aff2)
7167 return 1;
7169 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7170 if (cmp != 0)
7171 return cmp;
7173 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7174 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7175 if (last1 != last2)
7176 return last1 - last2;
7178 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7181 /* Compare two isl_pw_affs.
7183 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7184 * than "pa2" and 0 if they are equal.
7186 * The order is fairly arbitrary. We do consider expressions that only involve
7187 * earlier dimensions as "smaller".
7189 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7190 __isl_keep isl_pw_aff *pa2)
7192 int i;
7193 int cmp;
7195 if (pa1 == pa2)
7196 return 0;
7198 if (!pa1)
7199 return -1;
7200 if (!pa2)
7201 return 1;
7203 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7204 if (cmp != 0)
7205 return cmp;
7207 if (pa1->n != pa2->n)
7208 return pa1->n - pa2->n;
7210 for (i = 0; i < pa1->n; ++i) {
7211 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7212 if (cmp != 0)
7213 return cmp;
7214 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7215 if (cmp != 0)
7216 return cmp;
7219 return 0;
7222 /* Return a piecewise affine expression that is equal to "v" on "domain".
7224 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7225 __isl_take isl_val *v)
7227 isl_space *space;
7228 isl_local_space *ls;
7229 isl_aff *aff;
7231 space = isl_set_get_space(domain);
7232 ls = isl_local_space_from_space(space);
7233 aff = isl_aff_val_on_domain(ls, v);
7235 return isl_pw_aff_alloc(domain, aff);
7238 /* Return a multi affine expression that is equal to "mv" on domain
7239 * space "space".
7241 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7242 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7244 int i, n;
7245 isl_space *space2;
7246 isl_local_space *ls;
7247 isl_multi_aff *ma;
7249 if (!space || !mv)
7250 goto error;
7252 n = isl_multi_val_dim(mv, isl_dim_set);
7253 space2 = isl_multi_val_get_space(mv);
7254 space2 = isl_space_align_params(space2, isl_space_copy(space));
7255 space = isl_space_align_params(space, isl_space_copy(space2));
7256 space = isl_space_map_from_domain_and_range(space, space2);
7257 ma = isl_multi_aff_alloc(isl_space_copy(space));
7258 ls = isl_local_space_from_space(isl_space_domain(space));
7259 for (i = 0; i < n; ++i) {
7260 isl_val *v;
7261 isl_aff *aff;
7263 v = isl_multi_val_get_val(mv, i);
7264 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7265 ma = isl_multi_aff_set_aff(ma, i, aff);
7267 isl_local_space_free(ls);
7269 isl_multi_val_free(mv);
7270 return ma;
7271 error:
7272 isl_space_free(space);
7273 isl_multi_val_free(mv);
7274 return NULL;
7277 /* Return a piecewise multi-affine expression
7278 * that is equal to "mv" on "domain".
7280 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7281 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7283 isl_space *space;
7284 isl_multi_aff *ma;
7286 space = isl_set_get_space(domain);
7287 ma = isl_multi_aff_multi_val_on_space(space, mv);
7289 return isl_pw_multi_aff_alloc(domain, ma);
7292 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7293 * mv is the value that should be attained on each domain set
7294 * res collects the results
7296 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7297 isl_multi_val *mv;
7298 isl_union_pw_multi_aff *res;
7301 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7302 * and add it to data->res.
7304 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7305 void *user)
7307 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7308 isl_pw_multi_aff *pma;
7309 isl_multi_val *mv;
7311 mv = isl_multi_val_copy(data->mv);
7312 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7313 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7315 return data->res ? isl_stat_ok : isl_stat_error;
7318 /* Return a union piecewise multi-affine expression
7319 * that is equal to "mv" on "domain".
7321 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7322 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7324 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7325 isl_space *space;
7327 space = isl_union_set_get_space(domain);
7328 data.res = isl_union_pw_multi_aff_empty(space);
7329 data.mv = mv;
7330 if (isl_union_set_foreach_set(domain,
7331 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7332 data.res = isl_union_pw_multi_aff_free(data.res);
7333 isl_union_set_free(domain);
7334 isl_multi_val_free(mv);
7335 return data.res;
7338 /* Compute the pullback of data->pma by the function represented by "pma2",
7339 * provided the spaces match, and add the results to data->res.
7341 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7343 struct isl_union_pw_multi_aff_bin_data *data = user;
7345 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7346 pma2->dim, isl_dim_out)) {
7347 isl_pw_multi_aff_free(pma2);
7348 return isl_stat_ok;
7351 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7352 isl_pw_multi_aff_copy(data->pma), pma2);
7354 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7355 if (!data->res)
7356 return isl_stat_error;
7358 return isl_stat_ok;
7361 /* Compute the pullback of "upma1" by the function represented by "upma2".
7363 __isl_give isl_union_pw_multi_aff *
7364 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7365 __isl_take isl_union_pw_multi_aff *upma1,
7366 __isl_take isl_union_pw_multi_aff *upma2)
7368 return bin_op(upma1, upma2, &pullback_entry);
7371 /* Check that the domain space of "upa" matches "space".
7373 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7374 * can in principle never fail since the space "space" is that
7375 * of the isl_multi_union_pw_aff and is a set space such that
7376 * there is no domain space to match.
7378 * We check the parameters and double-check that "space" is
7379 * indeed that of a set.
7381 static isl_stat isl_union_pw_aff_check_match_domain_space(
7382 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7384 isl_space *upa_space;
7385 isl_bool match;
7387 if (!upa || !space)
7388 return isl_stat_error;
7390 match = isl_space_is_set(space);
7391 if (match < 0)
7392 return isl_stat_error;
7393 if (!match)
7394 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7395 "expecting set space", return -1);
7397 upa_space = isl_union_pw_aff_get_space(upa);
7398 match = isl_space_has_equal_params(space, upa_space);
7399 if (match < 0)
7400 goto error;
7401 if (!match)
7402 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7403 "parameters don't match", goto error);
7405 isl_space_free(upa_space);
7406 return isl_stat_ok;
7407 error:
7408 isl_space_free(upa_space);
7409 return isl_stat_error;
7412 /* Do the parameters of "upa" match those of "space"?
7414 static isl_bool isl_union_pw_aff_matching_params(
7415 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7417 isl_space *upa_space;
7418 isl_bool match;
7420 if (!upa || !space)
7421 return isl_bool_error;
7423 upa_space = isl_union_pw_aff_get_space(upa);
7425 match = isl_space_has_equal_params(space, upa_space);
7427 isl_space_free(upa_space);
7428 return match;
7431 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7432 * space represents the new parameters.
7433 * res collects the results.
7435 struct isl_union_pw_aff_reset_params_data {
7436 isl_space *space;
7437 isl_union_pw_aff *res;
7440 /* Replace the parameters of "pa" by data->space and
7441 * add the result to data->res.
7443 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7445 struct isl_union_pw_aff_reset_params_data *data = user;
7446 isl_space *space;
7448 space = isl_pw_aff_get_space(pa);
7449 space = isl_space_replace(space, isl_dim_param, data->space);
7450 pa = isl_pw_aff_reset_space(pa, space);
7451 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7453 return data->res ? isl_stat_ok : isl_stat_error;
7456 /* Replace the domain space of "upa" by "space".
7457 * Since a union expression does not have a (single) domain space,
7458 * "space" is necessarily a parameter space.
7460 * Since the order and the names of the parameters determine
7461 * the hash value, we need to create a new hash table.
7463 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7464 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7466 struct isl_union_pw_aff_reset_params_data data = { space };
7467 isl_bool match;
7469 match = isl_union_pw_aff_matching_params(upa, space);
7470 if (match < 0)
7471 upa = isl_union_pw_aff_free(upa);
7472 else if (match) {
7473 isl_space_free(space);
7474 return upa;
7477 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7478 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7479 data.res = isl_union_pw_aff_free(data.res);
7481 isl_union_pw_aff_free(upa);
7482 isl_space_free(space);
7483 return data.res;
7486 /* Return the floor of "pa".
7488 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7490 return isl_pw_aff_floor(pa);
7493 /* Given f, return floor(f).
7495 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7496 __isl_take isl_union_pw_aff *upa)
7498 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7501 /* Compute
7503 * upa mod m = upa - m * floor(upa/m)
7505 * with m an integer value.
7507 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7508 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7510 isl_union_pw_aff *res;
7512 if (!upa || !m)
7513 goto error;
7515 if (!isl_val_is_int(m))
7516 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7517 "expecting integer modulo", goto error);
7518 if (!isl_val_is_pos(m))
7519 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7520 "expecting positive modulo", goto error);
7522 res = isl_union_pw_aff_copy(upa);
7523 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7524 upa = isl_union_pw_aff_floor(upa);
7525 upa = isl_union_pw_aff_scale_val(upa, m);
7526 res = isl_union_pw_aff_sub(res, upa);
7528 return res;
7529 error:
7530 isl_val_free(m);
7531 isl_union_pw_aff_free(upa);
7532 return NULL;
7535 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7536 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7537 * needs to attain.
7538 * "res" collects the results.
7540 struct isl_union_pw_aff_aff_on_domain_data {
7541 isl_aff *aff;
7542 isl_union_pw_aff *res;
7545 /* Construct a piecewise affine expression that is equal to data->aff
7546 * on "domain" and add the result to data->res.
7548 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7550 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7551 isl_pw_aff *pa;
7552 isl_aff *aff;
7553 int dim;
7555 aff = isl_aff_copy(data->aff);
7556 dim = isl_set_dim(domain, isl_dim_set);
7557 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7558 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7559 pa = isl_pw_aff_alloc(domain, aff);
7560 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7562 return data->res ? isl_stat_ok : isl_stat_error;
7565 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7566 * pos is the output position that needs to be extracted.
7567 * res collects the results.
7569 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7570 int pos;
7571 isl_union_pw_aff *res;
7574 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7575 * (assuming it has such a dimension) and add it to data->res.
7577 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7579 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7580 int n_out;
7581 isl_pw_aff *pa;
7583 if (!pma)
7584 return isl_stat_error;
7586 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7587 if (data->pos >= n_out) {
7588 isl_pw_multi_aff_free(pma);
7589 return isl_stat_ok;
7592 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7593 isl_pw_multi_aff_free(pma);
7595 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7597 return data->res ? isl_stat_ok : isl_stat_error;
7600 /* Extract an isl_union_pw_aff corresponding to
7601 * output dimension "pos" of "upma".
7603 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7604 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7606 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7607 isl_space *space;
7609 if (!upma)
7610 return NULL;
7612 if (pos < 0)
7613 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7614 "cannot extract at negative position", return NULL);
7616 space = isl_union_pw_multi_aff_get_space(upma);
7617 data.res = isl_union_pw_aff_empty(space);
7618 data.pos = pos;
7619 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7620 &get_union_pw_aff, &data) < 0)
7621 data.res = isl_union_pw_aff_free(data.res);
7623 return data.res;
7626 /* Return a union piecewise affine expression
7627 * that is equal to "aff" on "domain".
7629 * Construct an isl_pw_aff on each of the sets in "domain" and
7630 * collect the results.
7632 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7633 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7635 struct isl_union_pw_aff_aff_on_domain_data data;
7636 isl_space *space;
7638 if (!domain || !aff)
7639 goto error;
7640 if (!isl_local_space_is_params(aff->ls))
7641 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7642 "expecting parametric expression", goto error);
7644 space = isl_union_set_get_space(domain);
7645 data.res = isl_union_pw_aff_empty(space);
7646 data.aff = aff;
7647 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7648 data.res = isl_union_pw_aff_free(data.res);
7649 isl_union_set_free(domain);
7650 isl_aff_free(aff);
7651 return data.res;
7652 error:
7653 isl_union_set_free(domain);
7654 isl_aff_free(aff);
7655 return NULL;
7658 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7659 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7660 * "res" collects the results.
7662 struct isl_union_pw_aff_val_on_domain_data {
7663 isl_val *v;
7664 isl_union_pw_aff *res;
7667 /* Construct a piecewise affine expression that is equal to data->v
7668 * on "domain" and add the result to data->res.
7670 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7672 struct isl_union_pw_aff_val_on_domain_data *data = user;
7673 isl_pw_aff *pa;
7674 isl_val *v;
7676 v = isl_val_copy(data->v);
7677 pa = isl_pw_aff_val_on_domain(domain, v);
7678 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7680 return data->res ? isl_stat_ok : isl_stat_error;
7683 /* Return a union piecewise affine expression
7684 * that is equal to "v" on "domain".
7686 * Construct an isl_pw_aff on each of the sets in "domain" and
7687 * collect the results.
7689 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7690 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7692 struct isl_union_pw_aff_val_on_domain_data data;
7693 isl_space *space;
7695 space = isl_union_set_get_space(domain);
7696 data.res = isl_union_pw_aff_empty(space);
7697 data.v = v;
7698 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7699 data.res = isl_union_pw_aff_free(data.res);
7700 isl_union_set_free(domain);
7701 isl_val_free(v);
7702 return data.res;
7705 /* Construct a piecewise multi affine expression
7706 * that is equal to "pa" and add it to upma.
7708 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7709 void *user)
7711 isl_union_pw_multi_aff **upma = user;
7712 isl_pw_multi_aff *pma;
7714 pma = isl_pw_multi_aff_from_pw_aff(pa);
7715 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7717 return *upma ? isl_stat_ok : isl_stat_error;
7720 /* Construct and return a union piecewise multi affine expression
7721 * that is equal to the given union piecewise affine expression.
7723 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7724 __isl_take isl_union_pw_aff *upa)
7726 isl_space *space;
7727 isl_union_pw_multi_aff *upma;
7729 if (!upa)
7730 return NULL;
7732 space = isl_union_pw_aff_get_space(upa);
7733 upma = isl_union_pw_multi_aff_empty(space);
7735 if (isl_union_pw_aff_foreach_pw_aff(upa,
7736 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7737 upma = isl_union_pw_multi_aff_free(upma);
7739 isl_union_pw_aff_free(upa);
7740 return upma;
7743 /* Compute the set of elements in the domain of "pa" where it is zero and
7744 * add this set to "uset".
7746 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7748 isl_union_set **uset = (isl_union_set **)user;
7750 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7752 return *uset ? isl_stat_ok : isl_stat_error;
7755 /* Return a union set containing those elements in the domain
7756 * of "upa" where it is zero.
7758 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7759 __isl_take isl_union_pw_aff *upa)
7761 isl_union_set *zero;
7763 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7764 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7765 zero = isl_union_set_free(zero);
7767 isl_union_pw_aff_free(upa);
7768 return zero;
7771 /* Convert "pa" to an isl_map and add it to *umap.
7773 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7775 isl_union_map **umap = user;
7776 isl_map *map;
7778 map = isl_map_from_pw_aff(pa);
7779 *umap = isl_union_map_add_map(*umap, map);
7781 return *umap ? isl_stat_ok : isl_stat_error;
7784 /* Construct a union map mapping the domain of the union
7785 * piecewise affine expression to its range, with the single output dimension
7786 * equated to the corresponding affine expressions on their cells.
7788 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7789 __isl_take isl_union_pw_aff *upa)
7791 isl_space *space;
7792 isl_union_map *umap;
7794 if (!upa)
7795 return NULL;
7797 space = isl_union_pw_aff_get_space(upa);
7798 umap = isl_union_map_empty(space);
7800 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7801 &umap) < 0)
7802 umap = isl_union_map_free(umap);
7804 isl_union_pw_aff_free(upa);
7805 return umap;
7808 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7809 * upma is the function that is plugged in.
7810 * pa is the current part of the function in which upma is plugged in.
7811 * res collects the results.
7813 struct isl_union_pw_aff_pullback_upma_data {
7814 isl_union_pw_multi_aff *upma;
7815 isl_pw_aff *pa;
7816 isl_union_pw_aff *res;
7819 /* Check if "pma" can be plugged into data->pa.
7820 * If so, perform the pullback and add the result to data->res.
7822 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7824 struct isl_union_pw_aff_pullback_upma_data *data = user;
7825 isl_pw_aff *pa;
7827 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7828 pma->dim, isl_dim_out)) {
7829 isl_pw_multi_aff_free(pma);
7830 return isl_stat_ok;
7833 pa = isl_pw_aff_copy(data->pa);
7834 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7836 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7838 return data->res ? isl_stat_ok : isl_stat_error;
7841 /* Check if any of the elements of data->upma can be plugged into pa,
7842 * add if so add the result to data->res.
7844 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7846 struct isl_union_pw_aff_pullback_upma_data *data = user;
7847 isl_stat r;
7849 data->pa = pa;
7850 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7851 &pa_pb_pma, data);
7852 isl_pw_aff_free(pa);
7854 return r;
7857 /* Compute the pullback of "upa" by the function represented by "upma".
7858 * In other words, plug in "upma" in "upa". The result contains
7859 * expressions defined over the domain space of "upma".
7861 * Run over all pairs of elements in "upa" and "upma", perform
7862 * the pullback when appropriate and collect the results.
7863 * If the hash value were based on the domain space rather than
7864 * the function space, then we could run through all elements
7865 * of "upma" and directly pick out the corresponding element of "upa".
7867 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7868 __isl_take isl_union_pw_aff *upa,
7869 __isl_take isl_union_pw_multi_aff *upma)
7871 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7872 isl_space *space;
7874 space = isl_union_pw_multi_aff_get_space(upma);
7875 upa = isl_union_pw_aff_align_params(upa, space);
7876 space = isl_union_pw_aff_get_space(upa);
7877 upma = isl_union_pw_multi_aff_align_params(upma, space);
7879 if (!upa || !upma)
7880 goto error;
7882 data.upma = upma;
7883 data.res = isl_union_pw_aff_alloc_same_size(upa);
7884 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7885 data.res = isl_union_pw_aff_free(data.res);
7887 isl_union_pw_aff_free(upa);
7888 isl_union_pw_multi_aff_free(upma);
7889 return data.res;
7890 error:
7891 isl_union_pw_aff_free(upa);
7892 isl_union_pw_multi_aff_free(upma);
7893 return NULL;
7896 #undef BASE
7897 #define BASE union_pw_aff
7898 #undef DOMBASE
7899 #define DOMBASE union_set
7901 #define NO_MOVE_DIMS
7902 #define NO_DIMS
7903 #define NO_DOMAIN
7904 #define NO_PRODUCT
7905 #define NO_SPLICE
7906 #define NO_ZERO
7907 #define NO_IDENTITY
7908 #define NO_GIST
7910 #include <isl_multi_templ.c>
7911 #include <isl_multi_apply_set.c>
7912 #include <isl_multi_apply_union_set.c>
7913 #include <isl_multi_coalesce.c>
7914 #include <isl_multi_floor.c>
7915 #include <isl_multi_gist.c>
7916 #include <isl_multi_intersect.c>
7918 /* Construct a multiple union piecewise affine expression
7919 * in the given space with value zero in each of the output dimensions.
7921 * Since there is no canonical zero value for
7922 * a union piecewise affine expression, we can only construct
7923 * zero-dimensional "zero" value.
7925 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7926 __isl_take isl_space *space)
7928 if (!space)
7929 return NULL;
7931 if (!isl_space_is_set(space))
7932 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7933 "expecting set space", goto error);
7934 if (isl_space_dim(space , isl_dim_out) != 0)
7935 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7936 "expecting 0D space", goto error);
7938 return isl_multi_union_pw_aff_alloc(space);
7939 error:
7940 isl_space_free(space);
7941 return NULL;
7944 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7945 * with the actual sum on the shared domain and
7946 * the defined expression on the symmetric difference of the domains.
7948 * We simply iterate over the elements in both arguments and
7949 * call isl_union_pw_aff_union_add on each of them.
7951 static __isl_give isl_multi_union_pw_aff *
7952 isl_multi_union_pw_aff_union_add_aligned(
7953 __isl_take isl_multi_union_pw_aff *mupa1,
7954 __isl_take isl_multi_union_pw_aff *mupa2)
7956 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7957 &isl_union_pw_aff_union_add);
7960 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7961 * with the actual sum on the shared domain and
7962 * the defined expression on the symmetric difference of the domains.
7964 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7965 __isl_take isl_multi_union_pw_aff *mupa1,
7966 __isl_take isl_multi_union_pw_aff *mupa2)
7968 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7969 &isl_multi_union_pw_aff_union_add_aligned);
7972 /* Construct and return a multi union piecewise affine expression
7973 * that is equal to the given multi affine expression.
7975 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7976 __isl_take isl_multi_aff *ma)
7978 isl_multi_pw_aff *mpa;
7980 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7981 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7984 /* Construct and return a multi union piecewise affine expression
7985 * that is equal to the given multi piecewise affine expression.
7987 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7988 __isl_take isl_multi_pw_aff *mpa)
7990 int i, n;
7991 isl_space *space;
7992 isl_multi_union_pw_aff *mupa;
7994 if (!mpa)
7995 return NULL;
7997 space = isl_multi_pw_aff_get_space(mpa);
7998 space = isl_space_range(space);
7999 mupa = isl_multi_union_pw_aff_alloc(space);
8001 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8002 for (i = 0; i < n; ++i) {
8003 isl_pw_aff *pa;
8004 isl_union_pw_aff *upa;
8006 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8007 upa = isl_union_pw_aff_from_pw_aff(pa);
8008 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8011 isl_multi_pw_aff_free(mpa);
8013 return mupa;
8016 /* Extract the range space of "pma" and assign it to *space.
8017 * If *space has already been set (through a previous call to this function),
8018 * then check that the range space is the same.
8020 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8022 isl_space **space = user;
8023 isl_space *pma_space;
8024 isl_bool equal;
8026 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8027 isl_pw_multi_aff_free(pma);
8029 if (!pma_space)
8030 return isl_stat_error;
8031 if (!*space) {
8032 *space = pma_space;
8033 return isl_stat_ok;
8036 equal = isl_space_is_equal(pma_space, *space);
8037 isl_space_free(pma_space);
8039 if (equal < 0)
8040 return isl_stat_error;
8041 if (!equal)
8042 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8043 "range spaces not the same", return isl_stat_error);
8044 return isl_stat_ok;
8047 /* Construct and return a multi union piecewise affine expression
8048 * that is equal to the given union piecewise multi affine expression.
8050 * In order to be able to perform the conversion, the input
8051 * needs to be non-empty and may only involve a single range space.
8053 __isl_give isl_multi_union_pw_aff *
8054 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8055 __isl_take isl_union_pw_multi_aff *upma)
8057 isl_space *space = NULL;
8058 isl_multi_union_pw_aff *mupa;
8059 int i, n;
8061 if (!upma)
8062 return NULL;
8063 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8064 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8065 "cannot extract range space from empty input",
8066 goto error);
8067 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8068 &space) < 0)
8069 goto error;
8071 if (!space)
8072 goto error;
8074 n = isl_space_dim(space, isl_dim_set);
8075 mupa = isl_multi_union_pw_aff_alloc(space);
8077 for (i = 0; i < n; ++i) {
8078 isl_union_pw_aff *upa;
8080 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8081 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8084 isl_union_pw_multi_aff_free(upma);
8085 return mupa;
8086 error:
8087 isl_space_free(space);
8088 isl_union_pw_multi_aff_free(upma);
8089 return NULL;
8092 /* Try and create an isl_multi_union_pw_aff that is equivalent
8093 * to the given isl_union_map.
8094 * The isl_union_map is required to be single-valued in each space.
8095 * Moreover, it cannot be empty and all range spaces need to be the same.
8096 * Otherwise, an error is produced.
8098 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8099 __isl_take isl_union_map *umap)
8101 isl_union_pw_multi_aff *upma;
8103 upma = isl_union_pw_multi_aff_from_union_map(umap);
8104 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8107 /* Return a multiple union piecewise affine expression
8108 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8109 * have been aligned.
8111 static __isl_give isl_multi_union_pw_aff *
8112 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8113 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8115 int i, n;
8116 isl_space *space;
8117 isl_multi_union_pw_aff *mupa;
8119 if (!domain || !mv)
8120 goto error;
8122 n = isl_multi_val_dim(mv, isl_dim_set);
8123 space = isl_multi_val_get_space(mv);
8124 mupa = isl_multi_union_pw_aff_alloc(space);
8125 for (i = 0; i < n; ++i) {
8126 isl_val *v;
8127 isl_union_pw_aff *upa;
8129 v = isl_multi_val_get_val(mv, i);
8130 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8132 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8135 isl_union_set_free(domain);
8136 isl_multi_val_free(mv);
8137 return mupa;
8138 error:
8139 isl_union_set_free(domain);
8140 isl_multi_val_free(mv);
8141 return NULL;
8144 /* Return a multiple union piecewise affine expression
8145 * that is equal to "mv" on "domain".
8147 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8148 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8150 isl_bool equal_params;
8152 if (!domain || !mv)
8153 goto error;
8154 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8155 if (equal_params < 0)
8156 goto error;
8157 if (equal_params)
8158 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8159 domain, mv);
8160 domain = isl_union_set_align_params(domain,
8161 isl_multi_val_get_space(mv));
8162 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8163 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8164 error:
8165 isl_union_set_free(domain);
8166 isl_multi_val_free(mv);
8167 return NULL;
8170 /* Return a multiple union piecewise affine expression
8171 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8172 * have been aligned.
8174 static __isl_give isl_multi_union_pw_aff *
8175 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8176 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8178 int i, n;
8179 isl_space *space;
8180 isl_multi_union_pw_aff *mupa;
8182 if (!domain || !ma)
8183 goto error;
8185 n = isl_multi_aff_dim(ma, isl_dim_set);
8186 space = isl_multi_aff_get_space(ma);
8187 mupa = isl_multi_union_pw_aff_alloc(space);
8188 for (i = 0; i < n; ++i) {
8189 isl_aff *aff;
8190 isl_union_pw_aff *upa;
8192 aff = isl_multi_aff_get_aff(ma, i);
8193 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8194 aff);
8195 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8198 isl_union_set_free(domain);
8199 isl_multi_aff_free(ma);
8200 return mupa;
8201 error:
8202 isl_union_set_free(domain);
8203 isl_multi_aff_free(ma);
8204 return NULL;
8207 /* Return a multiple union piecewise affine expression
8208 * that is equal to "ma" on "domain".
8210 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8211 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8213 isl_bool equal_params;
8215 if (!domain || !ma)
8216 goto error;
8217 equal_params = isl_space_has_equal_params(domain->dim, ma->space);
8218 if (equal_params < 0)
8219 goto error;
8220 if (equal_params)
8221 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8222 domain, ma);
8223 domain = isl_union_set_align_params(domain,
8224 isl_multi_aff_get_space(ma));
8225 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8226 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8227 error:
8228 isl_union_set_free(domain);
8229 isl_multi_aff_free(ma);
8230 return NULL;
8233 /* Return a union set containing those elements in the domains
8234 * of the elements of "mupa" where they are all zero.
8236 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8237 __isl_take isl_multi_union_pw_aff *mupa)
8239 int i, n;
8240 isl_union_pw_aff *upa;
8241 isl_union_set *zero;
8243 if (!mupa)
8244 return NULL;
8246 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8247 if (n == 0)
8248 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8249 "cannot determine zero set "
8250 "of zero-dimensional function", goto error);
8252 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8253 zero = isl_union_pw_aff_zero_union_set(upa);
8255 for (i = 1; i < n; ++i) {
8256 isl_union_set *zero_i;
8258 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8259 zero_i = isl_union_pw_aff_zero_union_set(upa);
8261 zero = isl_union_set_intersect(zero, zero_i);
8264 isl_multi_union_pw_aff_free(mupa);
8265 return zero;
8266 error:
8267 isl_multi_union_pw_aff_free(mupa);
8268 return NULL;
8271 /* Construct a union map mapping the shared domain
8272 * of the union piecewise affine expressions to the range of "mupa"
8273 * with each dimension in the range equated to the
8274 * corresponding union piecewise affine expression.
8276 * The input cannot be zero-dimensional as there is
8277 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8279 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8280 __isl_take isl_multi_union_pw_aff *mupa)
8282 int i, n;
8283 isl_space *space;
8284 isl_union_map *umap;
8285 isl_union_pw_aff *upa;
8287 if (!mupa)
8288 return NULL;
8290 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8291 if (n == 0)
8292 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8293 "cannot determine domain of zero-dimensional "
8294 "isl_multi_union_pw_aff", goto error);
8296 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8297 umap = isl_union_map_from_union_pw_aff(upa);
8299 for (i = 1; i < n; ++i) {
8300 isl_union_map *umap_i;
8302 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8303 umap_i = isl_union_map_from_union_pw_aff(upa);
8304 umap = isl_union_map_flat_range_product(umap, umap_i);
8307 space = isl_multi_union_pw_aff_get_space(mupa);
8308 umap = isl_union_map_reset_range_space(umap, space);
8310 isl_multi_union_pw_aff_free(mupa);
8311 return umap;
8312 error:
8313 isl_multi_union_pw_aff_free(mupa);
8314 return NULL;
8317 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8318 * "range" is the space from which to set the range space.
8319 * "res" collects the results.
8321 struct isl_union_pw_multi_aff_reset_range_space_data {
8322 isl_space *range;
8323 isl_union_pw_multi_aff *res;
8326 /* Replace the range space of "pma" by the range space of data->range and
8327 * add the result to data->res.
8329 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8331 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8332 isl_space *space;
8334 space = isl_pw_multi_aff_get_space(pma);
8335 space = isl_space_domain(space);
8336 space = isl_space_extend_domain_with_range(space,
8337 isl_space_copy(data->range));
8338 pma = isl_pw_multi_aff_reset_space(pma, space);
8339 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8341 return data->res ? isl_stat_ok : isl_stat_error;
8344 /* Replace the range space of all the piecewise affine expressions in "upma" by
8345 * the range space of "space".
8347 * This assumes that all these expressions have the same output dimension.
8349 * Since the spaces of the expressions change, so do their hash values.
8350 * We therefore need to create a new isl_union_pw_multi_aff.
8351 * Note that the hash value is currently computed based on the entire
8352 * space even though there can only be a single expression with a given
8353 * domain space.
8355 static __isl_give isl_union_pw_multi_aff *
8356 isl_union_pw_multi_aff_reset_range_space(
8357 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8359 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8360 isl_space *space_upma;
8362 space_upma = isl_union_pw_multi_aff_get_space(upma);
8363 data.res = isl_union_pw_multi_aff_empty(space_upma);
8364 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8365 &reset_range_space, &data) < 0)
8366 data.res = isl_union_pw_multi_aff_free(data.res);
8368 isl_space_free(space);
8369 isl_union_pw_multi_aff_free(upma);
8370 return data.res;
8373 /* Construct and return a union piecewise multi affine expression
8374 * that is equal to the given multi union piecewise affine expression.
8376 * In order to be able to perform the conversion, the input
8377 * needs to have a least one output dimension.
8379 __isl_give isl_union_pw_multi_aff *
8380 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8381 __isl_take isl_multi_union_pw_aff *mupa)
8383 int i, n;
8384 isl_space *space;
8385 isl_union_pw_multi_aff *upma;
8386 isl_union_pw_aff *upa;
8388 if (!mupa)
8389 return NULL;
8391 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8392 if (n == 0)
8393 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8394 "cannot determine domain of zero-dimensional "
8395 "isl_multi_union_pw_aff", goto error);
8397 space = isl_multi_union_pw_aff_get_space(mupa);
8398 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8399 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8401 for (i = 1; i < n; ++i) {
8402 isl_union_pw_multi_aff *upma_i;
8404 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8405 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8406 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8409 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8411 isl_multi_union_pw_aff_free(mupa);
8412 return upma;
8413 error:
8414 isl_multi_union_pw_aff_free(mupa);
8415 return NULL;
8418 /* Intersect the range of "mupa" with "range".
8419 * That is, keep only those domain elements that have a function value
8420 * in "range".
8422 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8423 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8425 isl_union_pw_multi_aff *upma;
8426 isl_union_set *domain;
8427 isl_space *space;
8428 int n;
8429 int match;
8431 if (!mupa || !range)
8432 goto error;
8434 space = isl_set_get_space(range);
8435 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8436 space, isl_dim_set);
8437 isl_space_free(space);
8438 if (match < 0)
8439 goto error;
8440 if (!match)
8441 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8442 "space don't match", goto error);
8443 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8444 if (n == 0)
8445 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8446 "cannot intersect range of zero-dimensional "
8447 "isl_multi_union_pw_aff", goto error);
8449 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8450 isl_multi_union_pw_aff_copy(mupa));
8451 domain = isl_union_set_from_set(range);
8452 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8453 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8455 return mupa;
8456 error:
8457 isl_multi_union_pw_aff_free(mupa);
8458 isl_set_free(range);
8459 return NULL;
8462 /* Return the shared domain of the elements of "mupa".
8464 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8465 __isl_take isl_multi_union_pw_aff *mupa)
8467 int i, n;
8468 isl_union_pw_aff *upa;
8469 isl_union_set *dom;
8471 if (!mupa)
8472 return NULL;
8474 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8475 if (n == 0)
8476 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8477 "cannot determine domain", goto error);
8479 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8480 dom = isl_union_pw_aff_domain(upa);
8481 for (i = 1; i < n; ++i) {
8482 isl_union_set *dom_i;
8484 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8485 dom_i = isl_union_pw_aff_domain(upa);
8486 dom = isl_union_set_intersect(dom, dom_i);
8489 isl_multi_union_pw_aff_free(mupa);
8490 return dom;
8491 error:
8492 isl_multi_union_pw_aff_free(mupa);
8493 return NULL;
8496 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8497 * In particular, the spaces have been aligned.
8498 * The result is defined over the shared domain of the elements of "mupa"
8500 * We first extract the parametric constant part of "aff" and
8501 * define that over the shared domain.
8502 * Then we iterate over all input dimensions of "aff" and add the corresponding
8503 * multiples of the elements of "mupa".
8504 * Finally, we consider the integer divisions, calling the function
8505 * recursively to obtain an isl_union_pw_aff corresponding to the
8506 * integer division argument.
8508 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8509 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8511 int i, n_in, n_div;
8512 isl_union_pw_aff *upa;
8513 isl_union_set *uset;
8514 isl_val *v;
8515 isl_aff *cst;
8517 n_in = isl_aff_dim(aff, isl_dim_in);
8518 n_div = isl_aff_dim(aff, isl_dim_div);
8520 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8521 cst = isl_aff_copy(aff);
8522 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8523 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8524 cst = isl_aff_project_domain_on_params(cst);
8525 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8527 for (i = 0; i < n_in; ++i) {
8528 isl_union_pw_aff *upa_i;
8530 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8531 continue;
8532 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8533 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8534 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8535 upa = isl_union_pw_aff_add(upa, upa_i);
8538 for (i = 0; i < n_div; ++i) {
8539 isl_aff *div;
8540 isl_union_pw_aff *upa_i;
8542 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8543 continue;
8544 div = isl_aff_get_div(aff, i);
8545 upa_i = multi_union_pw_aff_apply_aff(
8546 isl_multi_union_pw_aff_copy(mupa), div);
8547 upa_i = isl_union_pw_aff_floor(upa_i);
8548 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8549 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8550 upa = isl_union_pw_aff_add(upa, upa_i);
8553 isl_multi_union_pw_aff_free(mupa);
8554 isl_aff_free(aff);
8556 return upa;
8559 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8560 * with the domain of "aff".
8561 * Furthermore, the dimension of this space needs to be greater than zero.
8562 * The result is defined over the shared domain of the elements of "mupa"
8564 * We perform these checks and then hand over control to
8565 * multi_union_pw_aff_apply_aff.
8567 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8568 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8570 isl_space *space1, *space2;
8571 int equal;
8573 mupa = isl_multi_union_pw_aff_align_params(mupa,
8574 isl_aff_get_space(aff));
8575 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8576 if (!mupa || !aff)
8577 goto error;
8579 space1 = isl_multi_union_pw_aff_get_space(mupa);
8580 space2 = isl_aff_get_domain_space(aff);
8581 equal = isl_space_is_equal(space1, space2);
8582 isl_space_free(space1);
8583 isl_space_free(space2);
8584 if (equal < 0)
8585 goto error;
8586 if (!equal)
8587 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8588 "spaces don't match", goto error);
8589 if (isl_aff_dim(aff, isl_dim_in) == 0)
8590 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8591 "cannot determine domains", goto error);
8593 return multi_union_pw_aff_apply_aff(mupa, aff);
8594 error:
8595 isl_multi_union_pw_aff_free(mupa);
8596 isl_aff_free(aff);
8597 return NULL;
8600 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8601 * with the domain of "ma".
8602 * Furthermore, the dimension of this space needs to be greater than zero,
8603 * unless the dimension of the target space of "ma" is also zero.
8604 * The result is defined over the shared domain of the elements of "mupa"
8606 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8607 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8609 isl_space *space1, *space2;
8610 isl_multi_union_pw_aff *res;
8611 int equal;
8612 int i, n_out;
8614 mupa = isl_multi_union_pw_aff_align_params(mupa,
8615 isl_multi_aff_get_space(ma));
8616 ma = isl_multi_aff_align_params(ma,
8617 isl_multi_union_pw_aff_get_space(mupa));
8618 if (!mupa || !ma)
8619 goto error;
8621 space1 = isl_multi_union_pw_aff_get_space(mupa);
8622 space2 = isl_multi_aff_get_domain_space(ma);
8623 equal = isl_space_is_equal(space1, space2);
8624 isl_space_free(space1);
8625 isl_space_free(space2);
8626 if (equal < 0)
8627 goto error;
8628 if (!equal)
8629 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8630 "spaces don't match", goto error);
8631 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8632 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8633 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8634 "cannot determine domains", goto error);
8636 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8637 res = isl_multi_union_pw_aff_alloc(space1);
8639 for (i = 0; i < n_out; ++i) {
8640 isl_aff *aff;
8641 isl_union_pw_aff *upa;
8643 aff = isl_multi_aff_get_aff(ma, i);
8644 upa = multi_union_pw_aff_apply_aff(
8645 isl_multi_union_pw_aff_copy(mupa), aff);
8646 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8649 isl_multi_aff_free(ma);
8650 isl_multi_union_pw_aff_free(mupa);
8651 return res;
8652 error:
8653 isl_multi_union_pw_aff_free(mupa);
8654 isl_multi_aff_free(ma);
8655 return NULL;
8658 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8659 * with the domain of "pa".
8660 * Furthermore, the dimension of this space needs to be greater than zero.
8661 * The result is defined over the shared domain of the elements of "mupa"
8663 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8664 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8666 int i;
8667 int equal;
8668 isl_space *space, *space2;
8669 isl_union_pw_aff *upa;
8671 mupa = isl_multi_union_pw_aff_align_params(mupa,
8672 isl_pw_aff_get_space(pa));
8673 pa = isl_pw_aff_align_params(pa,
8674 isl_multi_union_pw_aff_get_space(mupa));
8675 if (!mupa || !pa)
8676 goto error;
8678 space = isl_multi_union_pw_aff_get_space(mupa);
8679 space2 = isl_pw_aff_get_domain_space(pa);
8680 equal = isl_space_is_equal(space, space2);
8681 isl_space_free(space);
8682 isl_space_free(space2);
8683 if (equal < 0)
8684 goto error;
8685 if (!equal)
8686 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8687 "spaces don't match", goto error);
8688 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8689 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8690 "cannot determine domains", goto error);
8692 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8693 upa = isl_union_pw_aff_empty(space);
8695 for (i = 0; i < pa->n; ++i) {
8696 isl_aff *aff;
8697 isl_set *domain;
8698 isl_multi_union_pw_aff *mupa_i;
8699 isl_union_pw_aff *upa_i;
8701 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8702 domain = isl_set_copy(pa->p[i].set);
8703 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8704 aff = isl_aff_copy(pa->p[i].aff);
8705 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8706 upa = isl_union_pw_aff_union_add(upa, upa_i);
8709 isl_multi_union_pw_aff_free(mupa);
8710 isl_pw_aff_free(pa);
8711 return upa;
8712 error:
8713 isl_multi_union_pw_aff_free(mupa);
8714 isl_pw_aff_free(pa);
8715 return NULL;
8718 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8719 * with the domain of "pma".
8720 * Furthermore, the dimension of this space needs to be greater than zero,
8721 * unless the dimension of the target space of "pma" is also zero.
8722 * The result is defined over the shared domain of the elements of "mupa"
8724 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8725 __isl_take isl_multi_union_pw_aff *mupa,
8726 __isl_take isl_pw_multi_aff *pma)
8728 isl_space *space1, *space2;
8729 isl_multi_union_pw_aff *res;
8730 int equal;
8731 int i, n_out;
8733 mupa = isl_multi_union_pw_aff_align_params(mupa,
8734 isl_pw_multi_aff_get_space(pma));
8735 pma = isl_pw_multi_aff_align_params(pma,
8736 isl_multi_union_pw_aff_get_space(mupa));
8737 if (!mupa || !pma)
8738 goto error;
8740 space1 = isl_multi_union_pw_aff_get_space(mupa);
8741 space2 = isl_pw_multi_aff_get_domain_space(pma);
8742 equal = isl_space_is_equal(space1, space2);
8743 isl_space_free(space1);
8744 isl_space_free(space2);
8745 if (equal < 0)
8746 goto error;
8747 if (!equal)
8748 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8749 "spaces don't match", goto error);
8750 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8751 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8752 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8753 "cannot determine domains", goto error);
8755 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8756 res = isl_multi_union_pw_aff_alloc(space1);
8758 for (i = 0; i < n_out; ++i) {
8759 isl_pw_aff *pa;
8760 isl_union_pw_aff *upa;
8762 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8763 upa = isl_multi_union_pw_aff_apply_pw_aff(
8764 isl_multi_union_pw_aff_copy(mupa), pa);
8765 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8768 isl_pw_multi_aff_free(pma);
8769 isl_multi_union_pw_aff_free(mupa);
8770 return res;
8771 error:
8772 isl_multi_union_pw_aff_free(mupa);
8773 isl_pw_multi_aff_free(pma);
8774 return NULL;
8777 /* Compute the pullback of "mupa" by the function represented by "upma".
8778 * In other words, plug in "upma" in "mupa". The result contains
8779 * expressions defined over the domain space of "upma".
8781 * Run over all elements of "mupa" and plug in "upma" in each of them.
8783 __isl_give isl_multi_union_pw_aff *
8784 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8785 __isl_take isl_multi_union_pw_aff *mupa,
8786 __isl_take isl_union_pw_multi_aff *upma)
8788 int i, n;
8790 mupa = isl_multi_union_pw_aff_align_params(mupa,
8791 isl_union_pw_multi_aff_get_space(upma));
8792 upma = isl_union_pw_multi_aff_align_params(upma,
8793 isl_multi_union_pw_aff_get_space(mupa));
8794 if (!mupa || !upma)
8795 goto error;
8797 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8798 for (i = 0; i < n; ++i) {
8799 isl_union_pw_aff *upa;
8801 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8802 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8803 isl_union_pw_multi_aff_copy(upma));
8804 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8807 isl_union_pw_multi_aff_free(upma);
8808 return mupa;
8809 error:
8810 isl_multi_union_pw_aff_free(mupa);
8811 isl_union_pw_multi_aff_free(upma);
8812 return NULL;
8815 /* Extract the sequence of elements in "mupa" with domain space "space"
8816 * (ignoring parameters).
8818 * For the elements of "mupa" that are not defined on the specified space,
8819 * the corresponding element in the result is empty.
8821 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8822 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8824 int i, n;
8825 isl_bool equal_params;
8826 isl_space *space_mpa = NULL;
8827 isl_multi_pw_aff *mpa;
8829 if (!mupa || !space)
8830 goto error;
8832 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8833 equal_params = isl_space_has_equal_params(space_mpa, space);
8834 if (equal_params < 0)
8835 goto error;
8836 if (!equal_params) {
8837 space = isl_space_drop_dims(space, isl_dim_param,
8838 0, isl_space_dim(space, isl_dim_param));
8839 space = isl_space_align_params(space,
8840 isl_space_copy(space_mpa));
8841 if (!space)
8842 goto error;
8844 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8845 space_mpa);
8846 mpa = isl_multi_pw_aff_alloc(space_mpa);
8848 space = isl_space_from_domain(space);
8849 space = isl_space_add_dims(space, isl_dim_out, 1);
8850 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8851 for (i = 0; i < n; ++i) {
8852 isl_union_pw_aff *upa;
8853 isl_pw_aff *pa;
8855 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8856 pa = isl_union_pw_aff_extract_pw_aff(upa,
8857 isl_space_copy(space));
8858 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8859 isl_union_pw_aff_free(upa);
8862 isl_space_free(space);
8863 return mpa;
8864 error:
8865 isl_space_free(space_mpa);
8866 isl_space_free(space);
8867 return NULL;