add isl_map_is_identity
[isl.git] / isl_aff.c
blob4523b4c0107add81f8f1355a04f7400f3e748f22
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #define ISL_DIM_H
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl/deprecated/aff_int.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE union_pw_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
54 __isl_take isl_vec *v)
56 isl_aff *aff;
58 if (!ls || !v)
59 goto error;
61 aff = isl_calloc_type(v->ctx, struct isl_aff);
62 if (!aff)
63 goto error;
65 aff->ref = 1;
66 aff->ls = ls;
67 aff->v = v;
69 return aff;
70 error:
71 isl_local_space_free(ls);
72 isl_vec_free(v);
73 return NULL;
76 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
78 isl_ctx *ctx;
79 isl_vec *v;
80 unsigned total;
82 if (!ls)
83 return NULL;
85 ctx = isl_local_space_get_ctx(ls);
86 if (!isl_local_space_divs_known(ls))
87 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
88 goto error);
89 if (!isl_local_space_is_set(ls))
90 isl_die(ctx, isl_error_invalid,
91 "domain of affine expression should be a set",
92 goto error);
94 total = isl_local_space_dim(ls, isl_dim_all);
95 v = isl_vec_alloc(ctx, 1 + 1 + total);
96 return isl_aff_alloc_vec(ls, v);
97 error:
98 isl_local_space_free(ls);
99 return NULL;
102 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
104 isl_aff *aff;
106 aff = isl_aff_alloc(ls);
107 if (!aff)
108 return NULL;
110 isl_int_set_si(aff->v->el[0], 1);
111 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
113 return aff;
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
129 isl_aff *aff;
131 aff = isl_aff_alloc(ls);
132 if (!aff)
133 return NULL;
135 isl_seq_clr(aff->v->el, aff->v->size);
137 return aff;
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
152 __isl_take isl_val *val)
154 isl_aff *aff;
156 if (!ls || !val)
157 goto error;
158 if (!isl_val_is_rat(val))
159 isl_die(isl_val_get_ctx(val), isl_error_invalid,
160 "expecting rational value", goto error);
162 aff = isl_aff_alloc(isl_local_space_copy(ls));
163 if (!aff)
164 goto error;
166 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
167 isl_int_set(aff->v->el[1], val->n);
168 isl_int_set(aff->v->el[0], val->d);
170 isl_local_space_free(ls);
171 isl_val_free(val);
172 return aff;
173 error:
174 isl_local_space_free(ls);
175 isl_val_free(val);
176 return NULL;
179 /* Return an affine expression that is equal to the specified dimension
180 * in "ls".
182 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
183 enum isl_dim_type type, unsigned pos)
185 isl_space *space;
186 isl_aff *aff;
188 if (!ls)
189 return NULL;
191 space = isl_local_space_get_space(ls);
192 if (!space)
193 goto error;
194 if (isl_space_is_map(space))
195 isl_die(isl_space_get_ctx(space), isl_error_invalid,
196 "expecting (parameter) set space", goto error);
197 if (pos >= isl_local_space_dim(ls, type))
198 isl_die(isl_space_get_ctx(space), isl_error_invalid,
199 "position out of bounds", goto error);
201 isl_space_free(space);
202 aff = isl_aff_alloc(ls);
203 if (!aff)
204 return NULL;
206 pos += isl_local_space_offset(aff->ls, type);
208 isl_int_set_si(aff->v->el[0], 1);
209 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
210 isl_int_set_si(aff->v->el[1 + pos], 1);
212 return aff;
213 error:
214 isl_local_space_free(ls);
215 isl_space_free(space);
216 return NULL;
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
223 enum isl_dim_type type, unsigned pos)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
228 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
230 if (!aff)
231 return NULL;
233 aff->ref++;
234 return aff;
237 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
239 if (!aff)
240 return NULL;
242 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
243 isl_vec_copy(aff->v));
246 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
248 if (!aff)
249 return NULL;
251 if (aff->ref == 1)
252 return aff;
253 aff->ref--;
254 return isl_aff_dup(aff);
257 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
259 if (!aff)
260 return NULL;
262 if (--aff->ref > 0)
263 return NULL;
265 isl_local_space_free(aff->ls);
266 isl_vec_free(aff->v);
268 free(aff);
270 return NULL;
273 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
275 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
278 /* Return a hash value that digests "aff".
280 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
282 uint32_t hash, ls_hash, v_hash;
284 if (!aff)
285 return 0;
287 hash = isl_hash_init();
288 ls_hash = isl_local_space_get_hash(aff->ls);
289 isl_hash_hash(hash, ls_hash);
290 v_hash = isl_vec_get_hash(aff->v);
291 isl_hash_hash(hash, v_hash);
293 return hash;
296 /* Externally, an isl_aff has a map space, but internally, the
297 * ls field corresponds to the domain of that space.
299 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
301 if (!aff)
302 return 0;
303 if (type == isl_dim_out)
304 return 1;
305 if (type == isl_dim_in)
306 type = isl_dim_set;
307 return isl_local_space_dim(aff->ls, type);
310 /* Return the position of the dimension of the given type and name
311 * in "aff".
312 * Return -1 if no such dimension can be found.
314 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
315 const char *name)
317 if (!aff)
318 return -1;
319 if (type == isl_dim_out)
320 return -1;
321 if (type == isl_dim_in)
322 type = isl_dim_set;
323 return isl_local_space_find_dim_by_name(aff->ls, type, name);
326 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
328 return aff ? isl_local_space_get_space(aff->ls) : NULL;
331 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
333 isl_space *space;
334 if (!aff)
335 return NULL;
336 space = isl_local_space_get_space(aff->ls);
337 space = isl_space_from_domain(space);
338 space = isl_space_add_dims(space, isl_dim_out, 1);
339 return space;
342 __isl_give isl_local_space *isl_aff_get_domain_local_space(
343 __isl_keep isl_aff *aff)
345 return aff ? isl_local_space_copy(aff->ls) : NULL;
348 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
350 isl_local_space *ls;
351 if (!aff)
352 return NULL;
353 ls = isl_local_space_copy(aff->ls);
354 ls = isl_local_space_from_domain(ls);
355 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
356 return ls;
359 /* Externally, an isl_aff has a map space, but internally, the
360 * ls field corresponds to the domain of that space.
362 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
363 enum isl_dim_type type, unsigned pos)
365 if (!aff)
366 return NULL;
367 if (type == isl_dim_out)
368 return NULL;
369 if (type == isl_dim_in)
370 type = isl_dim_set;
371 return isl_local_space_get_dim_name(aff->ls, type, pos);
374 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
375 __isl_take isl_space *dim)
377 aff = isl_aff_cow(aff);
378 if (!aff || !dim)
379 goto error;
381 aff->ls = isl_local_space_reset_space(aff->ls, dim);
382 if (!aff->ls)
383 return isl_aff_free(aff);
385 return aff;
386 error:
387 isl_aff_free(aff);
388 isl_space_free(dim);
389 return NULL;
392 /* Reset the space of "aff". This function is called from isl_pw_templ.c
393 * and doesn't know if the space of an element object is represented
394 * directly or through its domain. It therefore passes along both.
396 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
397 __isl_take isl_space *space, __isl_take isl_space *domain)
399 isl_space_free(space);
400 return isl_aff_reset_domain_space(aff, domain);
403 /* Reorder the coefficients of the affine expression based
404 * on the given reodering.
405 * The reordering r is assumed to have been extended with the local
406 * variables.
408 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
409 __isl_take isl_reordering *r, int n_div)
411 isl_vec *res;
412 int i;
414 if (!vec || !r)
415 goto error;
417 res = isl_vec_alloc(vec->ctx,
418 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
419 isl_seq_cpy(res->el, vec->el, 2);
420 isl_seq_clr(res->el + 2, res->size - 2);
421 for (i = 0; i < r->len; ++i)
422 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
424 isl_reordering_free(r);
425 isl_vec_free(vec);
426 return res;
427 error:
428 isl_vec_free(vec);
429 isl_reordering_free(r);
430 return NULL;
433 /* Reorder the dimensions of the domain of "aff" according
434 * to the given reordering.
436 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
437 __isl_take isl_reordering *r)
439 aff = isl_aff_cow(aff);
440 if (!aff)
441 goto error;
443 r = isl_reordering_extend(r, aff->ls->div->n_row);
444 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
445 aff->ls->div->n_row);
446 aff->ls = isl_local_space_realign(aff->ls, r);
448 if (!aff->v || !aff->ls)
449 return isl_aff_free(aff);
451 return aff;
452 error:
453 isl_aff_free(aff);
454 isl_reordering_free(r);
455 return NULL;
458 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
459 __isl_take isl_space *model)
461 if (!aff || !model)
462 goto error;
464 if (!isl_space_match(aff->ls->dim, isl_dim_param,
465 model, isl_dim_param)) {
466 isl_reordering *exp;
468 model = isl_space_drop_dims(model, isl_dim_in,
469 0, isl_space_dim(model, isl_dim_in));
470 model = isl_space_drop_dims(model, isl_dim_out,
471 0, isl_space_dim(model, isl_dim_out));
472 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
473 exp = isl_reordering_extend_space(exp,
474 isl_aff_get_domain_space(aff));
475 aff = isl_aff_realign_domain(aff, exp);
478 isl_space_free(model);
479 return aff;
480 error:
481 isl_space_free(model);
482 isl_aff_free(aff);
483 return NULL;
486 /* Is "aff" obviously equal to zero?
488 * If the denominator is zero, then "aff" is not equal to zero.
490 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
492 if (!aff)
493 return isl_bool_error;
495 if (isl_int_is_zero(aff->v->el[0]))
496 return isl_bool_false;
497 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
500 /* Does "aff" represent NaN?
502 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
504 if (!aff)
505 return isl_bool_error;
507 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
510 /* Does "pa" involve any NaNs?
512 isl_bool isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
514 int i;
516 if (!pa)
517 return isl_bool_error;
518 if (pa->n == 0)
519 return isl_bool_false;
521 for (i = 0; i < pa->n; ++i) {
522 isl_bool is_nan = isl_aff_is_nan(pa->p[i].aff);
523 if (is_nan < 0 || is_nan)
524 return is_nan;
527 return isl_bool_false;
530 /* Are "aff1" and "aff2" obviously equal?
532 * NaN is not equal to anything, not even to another NaN.
534 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
535 __isl_keep isl_aff *aff2)
537 isl_bool equal;
539 if (!aff1 || !aff2)
540 return isl_bool_error;
542 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
543 return isl_bool_false;
545 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
546 if (equal < 0 || !equal)
547 return equal;
549 return isl_vec_is_equal(aff1->v, aff2->v);
552 /* Return the common denominator of "aff" in "v".
554 * We cannot return anything meaningful in case of a NaN.
556 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
558 if (!aff)
559 return -1;
560 if (isl_aff_is_nan(aff))
561 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
562 "cannot get denominator of NaN", return -1);
563 isl_int_set(*v, aff->v->el[0]);
564 return 0;
567 /* Return the common denominator of "aff".
569 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
571 isl_ctx *ctx;
573 if (!aff)
574 return NULL;
576 ctx = isl_aff_get_ctx(aff);
577 if (isl_aff_is_nan(aff))
578 return isl_val_nan(ctx);
579 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
582 /* Return the constant term of "aff" in "v".
584 * We cannot return anything meaningful in case of a NaN.
586 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
588 if (!aff)
589 return -1;
590 if (isl_aff_is_nan(aff))
591 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
592 "cannot get constant term of NaN", return -1);
593 isl_int_set(*v, aff->v->el[1]);
594 return 0;
597 /* Return the constant term of "aff".
599 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
601 isl_ctx *ctx;
602 isl_val *v;
604 if (!aff)
605 return NULL;
607 ctx = isl_aff_get_ctx(aff);
608 if (isl_aff_is_nan(aff))
609 return isl_val_nan(ctx);
610 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
611 return isl_val_normalize(v);
614 /* Return the coefficient of the variable of type "type" at position "pos"
615 * of "aff" in "v".
617 * We cannot return anything meaningful in case of a NaN.
619 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
620 enum isl_dim_type type, int pos, isl_int *v)
622 if (!aff)
623 return -1;
625 if (type == isl_dim_out)
626 isl_die(aff->v->ctx, isl_error_invalid,
627 "output/set dimension does not have a coefficient",
628 return -1);
629 if (type == isl_dim_in)
630 type = isl_dim_set;
632 if (pos >= isl_local_space_dim(aff->ls, type))
633 isl_die(aff->v->ctx, isl_error_invalid,
634 "position out of bounds", return -1);
636 if (isl_aff_is_nan(aff))
637 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
638 "cannot get coefficient of NaN", return -1);
639 pos += isl_local_space_offset(aff->ls, type);
640 isl_int_set(*v, aff->v->el[1 + pos]);
642 return 0;
645 /* Return the coefficient of the variable of type "type" at position "pos"
646 * of "aff".
648 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
649 enum isl_dim_type type, int pos)
651 isl_ctx *ctx;
652 isl_val *v;
654 if (!aff)
655 return NULL;
657 ctx = isl_aff_get_ctx(aff);
658 if (type == isl_dim_out)
659 isl_die(ctx, isl_error_invalid,
660 "output/set dimension does not have a coefficient",
661 return NULL);
662 if (type == isl_dim_in)
663 type = isl_dim_set;
665 if (pos >= isl_local_space_dim(aff->ls, type))
666 isl_die(ctx, isl_error_invalid,
667 "position out of bounds", return NULL);
669 if (isl_aff_is_nan(aff))
670 return isl_val_nan(ctx);
671 pos += isl_local_space_offset(aff->ls, type);
672 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
673 return isl_val_normalize(v);
676 /* Return the sign of the coefficient of the variable of type "type"
677 * at position "pos" of "aff".
679 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
680 int pos)
682 isl_ctx *ctx;
684 if (!aff)
685 return 0;
687 ctx = isl_aff_get_ctx(aff);
688 if (type == isl_dim_out)
689 isl_die(ctx, isl_error_invalid,
690 "output/set dimension does not have a coefficient",
691 return 0);
692 if (type == isl_dim_in)
693 type = isl_dim_set;
695 if (pos >= isl_local_space_dim(aff->ls, type))
696 isl_die(ctx, isl_error_invalid,
697 "position out of bounds", return 0);
699 pos += isl_local_space_offset(aff->ls, type);
700 return isl_int_sgn(aff->v->el[1 + pos]);
703 /* Replace the denominator of "aff" by "v".
705 * A NaN is unaffected by this operation.
707 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
709 if (!aff)
710 return NULL;
711 if (isl_aff_is_nan(aff))
712 return aff;
713 aff = isl_aff_cow(aff);
714 if (!aff)
715 return NULL;
717 aff->v = isl_vec_cow(aff->v);
718 if (!aff->v)
719 return isl_aff_free(aff);
721 isl_int_set(aff->v->el[0], v);
723 return aff;
726 /* Replace the numerator of the constant term of "aff" by "v".
728 * A NaN is unaffected by this operation.
730 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
732 if (!aff)
733 return NULL;
734 if (isl_aff_is_nan(aff))
735 return aff;
736 aff = isl_aff_cow(aff);
737 if (!aff)
738 return NULL;
740 aff->v = isl_vec_cow(aff->v);
741 if (!aff->v)
742 return isl_aff_free(aff);
744 isl_int_set(aff->v->el[1], v);
746 return aff;
749 /* Replace the constant term of "aff" by "v".
751 * A NaN is unaffected by this operation.
753 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
754 __isl_take isl_val *v)
756 if (!aff || !v)
757 goto error;
759 if (isl_aff_is_nan(aff)) {
760 isl_val_free(v);
761 return aff;
764 if (!isl_val_is_rat(v))
765 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
766 "expecting rational value", goto error);
768 if (isl_int_eq(aff->v->el[1], v->n) &&
769 isl_int_eq(aff->v->el[0], v->d)) {
770 isl_val_free(v);
771 return aff;
774 aff = isl_aff_cow(aff);
775 if (!aff)
776 goto error;
777 aff->v = isl_vec_cow(aff->v);
778 if (!aff->v)
779 goto error;
781 if (isl_int_eq(aff->v->el[0], v->d)) {
782 isl_int_set(aff->v->el[1], v->n);
783 } else if (isl_int_is_one(v->d)) {
784 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
785 } else {
786 isl_seq_scale(aff->v->el + 1,
787 aff->v->el + 1, v->d, aff->v->size - 1);
788 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
789 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
790 aff->v = isl_vec_normalize(aff->v);
791 if (!aff->v)
792 goto error;
795 isl_val_free(v);
796 return aff;
797 error:
798 isl_aff_free(aff);
799 isl_val_free(v);
800 return NULL;
803 /* Add "v" to the constant term of "aff".
805 * A NaN is unaffected by this operation.
807 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
809 if (isl_int_is_zero(v))
810 return aff;
812 if (!aff)
813 return NULL;
814 if (isl_aff_is_nan(aff))
815 return aff;
816 aff = isl_aff_cow(aff);
817 if (!aff)
818 return NULL;
820 aff->v = isl_vec_cow(aff->v);
821 if (!aff->v)
822 return isl_aff_free(aff);
824 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
826 return aff;
829 /* Add "v" to the constant term of "aff".
831 * A NaN is unaffected by this operation.
833 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
834 __isl_take isl_val *v)
836 if (!aff || !v)
837 goto error;
839 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
840 isl_val_free(v);
841 return aff;
844 if (!isl_val_is_rat(v))
845 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
846 "expecting rational value", goto error);
848 aff = isl_aff_cow(aff);
849 if (!aff)
850 goto error;
852 aff->v = isl_vec_cow(aff->v);
853 if (!aff->v)
854 goto error;
856 if (isl_int_is_one(v->d)) {
857 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
858 } else if (isl_int_eq(aff->v->el[0], v->d)) {
859 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
860 aff->v = isl_vec_normalize(aff->v);
861 if (!aff->v)
862 goto error;
863 } else {
864 isl_seq_scale(aff->v->el + 1,
865 aff->v->el + 1, v->d, aff->v->size - 1);
866 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
867 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
868 aff->v = isl_vec_normalize(aff->v);
869 if (!aff->v)
870 goto error;
873 isl_val_free(v);
874 return aff;
875 error:
876 isl_aff_free(aff);
877 isl_val_free(v);
878 return NULL;
881 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
883 isl_int t;
885 isl_int_init(t);
886 isl_int_set_si(t, v);
887 aff = isl_aff_add_constant(aff, t);
888 isl_int_clear(t);
890 return aff;
893 /* Add "v" to the numerator of the constant term of "aff".
895 * A NaN is unaffected by this operation.
897 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
899 if (isl_int_is_zero(v))
900 return aff;
902 if (!aff)
903 return NULL;
904 if (isl_aff_is_nan(aff))
905 return aff;
906 aff = isl_aff_cow(aff);
907 if (!aff)
908 return NULL;
910 aff->v = isl_vec_cow(aff->v);
911 if (!aff->v)
912 return isl_aff_free(aff);
914 isl_int_add(aff->v->el[1], aff->v->el[1], v);
916 return aff;
919 /* Add "v" to the numerator of the constant term of "aff".
921 * A NaN is unaffected by this operation.
923 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
925 isl_int t;
927 if (v == 0)
928 return aff;
930 isl_int_init(t);
931 isl_int_set_si(t, v);
932 aff = isl_aff_add_constant_num(aff, t);
933 isl_int_clear(t);
935 return aff;
938 /* Replace the numerator of the constant term of "aff" by "v".
940 * A NaN is unaffected by this operation.
942 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
944 if (!aff)
945 return NULL;
946 if (isl_aff_is_nan(aff))
947 return aff;
948 aff = isl_aff_cow(aff);
949 if (!aff)
950 return NULL;
952 aff->v = isl_vec_cow(aff->v);
953 if (!aff->v)
954 return isl_aff_free(aff);
956 isl_int_set_si(aff->v->el[1], v);
958 return aff;
961 /* Replace the numerator of the coefficient of the variable of type "type"
962 * at position "pos" of "aff" by "v".
964 * A NaN is unaffected by this operation.
966 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
967 enum isl_dim_type type, int pos, isl_int v)
969 if (!aff)
970 return NULL;
972 if (type == isl_dim_out)
973 isl_die(aff->v->ctx, isl_error_invalid,
974 "output/set dimension does not have a coefficient",
975 return isl_aff_free(aff));
976 if (type == isl_dim_in)
977 type = isl_dim_set;
979 if (pos >= isl_local_space_dim(aff->ls, type))
980 isl_die(aff->v->ctx, isl_error_invalid,
981 "position out of bounds", return isl_aff_free(aff));
983 if (isl_aff_is_nan(aff))
984 return aff;
985 aff = isl_aff_cow(aff);
986 if (!aff)
987 return NULL;
989 aff->v = isl_vec_cow(aff->v);
990 if (!aff->v)
991 return isl_aff_free(aff);
993 pos += isl_local_space_offset(aff->ls, type);
994 isl_int_set(aff->v->el[1 + pos], v);
996 return aff;
999 /* Replace the numerator of the coefficient of the variable of type "type"
1000 * at position "pos" of "aff" by "v".
1002 * A NaN is unaffected by this operation.
1004 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1005 enum isl_dim_type type, int pos, int v)
1007 if (!aff)
1008 return NULL;
1010 if (type == isl_dim_out)
1011 isl_die(aff->v->ctx, isl_error_invalid,
1012 "output/set dimension does not have a coefficient",
1013 return isl_aff_free(aff));
1014 if (type == isl_dim_in)
1015 type = isl_dim_set;
1017 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1018 isl_die(aff->v->ctx, isl_error_invalid,
1019 "position out of bounds", return isl_aff_free(aff));
1021 if (isl_aff_is_nan(aff))
1022 return aff;
1023 pos += isl_local_space_offset(aff->ls, type);
1024 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1025 return aff;
1027 aff = isl_aff_cow(aff);
1028 if (!aff)
1029 return NULL;
1031 aff->v = isl_vec_cow(aff->v);
1032 if (!aff->v)
1033 return isl_aff_free(aff);
1035 isl_int_set_si(aff->v->el[1 + pos], v);
1037 return aff;
1040 /* Replace the coefficient of the variable of type "type" at position "pos"
1041 * of "aff" by "v".
1043 * A NaN is unaffected by this operation.
1045 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1046 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1048 if (!aff || !v)
1049 goto error;
1051 if (type == isl_dim_out)
1052 isl_die(aff->v->ctx, isl_error_invalid,
1053 "output/set dimension does not have a coefficient",
1054 goto error);
1055 if (type == isl_dim_in)
1056 type = isl_dim_set;
1058 if (pos >= isl_local_space_dim(aff->ls, type))
1059 isl_die(aff->v->ctx, isl_error_invalid,
1060 "position out of bounds", goto error);
1062 if (isl_aff_is_nan(aff)) {
1063 isl_val_free(v);
1064 return aff;
1066 if (!isl_val_is_rat(v))
1067 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1068 "expecting rational value", goto error);
1070 pos += isl_local_space_offset(aff->ls, type);
1071 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1072 isl_int_eq(aff->v->el[0], v->d)) {
1073 isl_val_free(v);
1074 return aff;
1077 aff = isl_aff_cow(aff);
1078 if (!aff)
1079 goto error;
1080 aff->v = isl_vec_cow(aff->v);
1081 if (!aff->v)
1082 goto error;
1084 if (isl_int_eq(aff->v->el[0], v->d)) {
1085 isl_int_set(aff->v->el[1 + pos], v->n);
1086 } else if (isl_int_is_one(v->d)) {
1087 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1088 } else {
1089 isl_seq_scale(aff->v->el + 1,
1090 aff->v->el + 1, v->d, aff->v->size - 1);
1091 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1092 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1093 aff->v = isl_vec_normalize(aff->v);
1094 if (!aff->v)
1095 goto error;
1098 isl_val_free(v);
1099 return aff;
1100 error:
1101 isl_aff_free(aff);
1102 isl_val_free(v);
1103 return NULL;
1106 /* Add "v" to the coefficient of the variable of type "type"
1107 * at position "pos" of "aff".
1109 * A NaN is unaffected by this operation.
1111 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1112 enum isl_dim_type type, int pos, isl_int v)
1114 if (!aff)
1115 return NULL;
1117 if (type == isl_dim_out)
1118 isl_die(aff->v->ctx, isl_error_invalid,
1119 "output/set dimension does not have a coefficient",
1120 return isl_aff_free(aff));
1121 if (type == isl_dim_in)
1122 type = isl_dim_set;
1124 if (pos >= isl_local_space_dim(aff->ls, type))
1125 isl_die(aff->v->ctx, isl_error_invalid,
1126 "position out of bounds", return isl_aff_free(aff));
1128 if (isl_aff_is_nan(aff))
1129 return aff;
1130 aff = isl_aff_cow(aff);
1131 if (!aff)
1132 return NULL;
1134 aff->v = isl_vec_cow(aff->v);
1135 if (!aff->v)
1136 return isl_aff_free(aff);
1138 pos += isl_local_space_offset(aff->ls, type);
1139 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1141 return aff;
1144 /* Add "v" to the coefficient of the variable of type "type"
1145 * at position "pos" of "aff".
1147 * A NaN is unaffected by this operation.
1149 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1150 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1152 if (!aff || !v)
1153 goto error;
1155 if (isl_val_is_zero(v)) {
1156 isl_val_free(v);
1157 return aff;
1160 if (type == isl_dim_out)
1161 isl_die(aff->v->ctx, isl_error_invalid,
1162 "output/set dimension does not have a coefficient",
1163 goto error);
1164 if (type == isl_dim_in)
1165 type = isl_dim_set;
1167 if (pos >= isl_local_space_dim(aff->ls, type))
1168 isl_die(aff->v->ctx, isl_error_invalid,
1169 "position out of bounds", goto error);
1171 if (isl_aff_is_nan(aff)) {
1172 isl_val_free(v);
1173 return aff;
1175 if (!isl_val_is_rat(v))
1176 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1177 "expecting rational value", goto error);
1179 aff = isl_aff_cow(aff);
1180 if (!aff)
1181 goto error;
1183 aff->v = isl_vec_cow(aff->v);
1184 if (!aff->v)
1185 goto error;
1187 pos += isl_local_space_offset(aff->ls, type);
1188 if (isl_int_is_one(v->d)) {
1189 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1190 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1191 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1192 aff->v = isl_vec_normalize(aff->v);
1193 if (!aff->v)
1194 goto error;
1195 } else {
1196 isl_seq_scale(aff->v->el + 1,
1197 aff->v->el + 1, v->d, aff->v->size - 1);
1198 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1199 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1200 aff->v = isl_vec_normalize(aff->v);
1201 if (!aff->v)
1202 goto error;
1205 isl_val_free(v);
1206 return aff;
1207 error:
1208 isl_aff_free(aff);
1209 isl_val_free(v);
1210 return NULL;
1213 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1214 enum isl_dim_type type, int pos, int v)
1216 isl_int t;
1218 isl_int_init(t);
1219 isl_int_set_si(t, v);
1220 aff = isl_aff_add_coefficient(aff, type, pos, t);
1221 isl_int_clear(t);
1223 return aff;
1226 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1228 if (!aff)
1229 return NULL;
1231 return isl_local_space_get_div(aff->ls, pos);
1234 /* Return the negation of "aff".
1236 * As a special case, -NaN = NaN.
1238 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1240 if (!aff)
1241 return NULL;
1242 if (isl_aff_is_nan(aff))
1243 return aff;
1244 aff = isl_aff_cow(aff);
1245 if (!aff)
1246 return NULL;
1247 aff->v = isl_vec_cow(aff->v);
1248 if (!aff->v)
1249 return isl_aff_free(aff);
1251 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1253 return aff;
1256 /* Remove divs from the local space that do not appear in the affine
1257 * expression.
1258 * We currently only remove divs at the end.
1259 * Some intermediate divs may also not appear directly in the affine
1260 * expression, but we would also need to check that no other divs are
1261 * defined in terms of them.
1263 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1265 int pos;
1266 int off;
1267 int n;
1269 if (!aff)
1270 return NULL;
1272 n = isl_local_space_dim(aff->ls, isl_dim_div);
1273 off = isl_local_space_offset(aff->ls, isl_dim_div);
1275 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1276 if (pos == n)
1277 return aff;
1279 aff = isl_aff_cow(aff);
1280 if (!aff)
1281 return NULL;
1283 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1284 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1285 if (!aff->ls || !aff->v)
1286 return isl_aff_free(aff);
1288 return aff;
1291 /* Given two affine expressions "p" of length p_len (including the
1292 * denominator and the constant term) and "subs" of length subs_len,
1293 * plug in "subs" for the variable at position "pos".
1294 * The variables of "subs" and "p" are assumed to match up to subs_len,
1295 * but "p" may have additional variables.
1296 * "v" is an initialized isl_int that can be used internally.
1298 * In particular, if "p" represents the expression
1300 * (a i + g)/m
1302 * with i the variable at position "pos" and "subs" represents the expression
1304 * f/d
1306 * then the result represents the expression
1308 * (a f + d g)/(m d)
1311 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1312 int p_len, int subs_len, isl_int v)
1314 isl_int_set(v, p[1 + pos]);
1315 isl_int_set_si(p[1 + pos], 0);
1316 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1317 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1318 isl_int_mul(p[0], p[0], subs[0]);
1321 /* Look for any divs in the aff->ls with a denominator equal to one
1322 * and plug them into the affine expression and any subsequent divs
1323 * that may reference the div.
1325 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1327 int i, n;
1328 int len;
1329 isl_int v;
1330 isl_vec *vec;
1331 isl_local_space *ls;
1332 unsigned pos;
1334 if (!aff)
1335 return NULL;
1337 n = isl_local_space_dim(aff->ls, isl_dim_div);
1338 len = aff->v->size;
1339 for (i = 0; i < n; ++i) {
1340 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1341 continue;
1342 ls = isl_local_space_copy(aff->ls);
1343 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1344 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1345 vec = isl_vec_copy(aff->v);
1346 vec = isl_vec_cow(vec);
1347 if (!ls || !vec)
1348 goto error;
1350 isl_int_init(v);
1352 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1353 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1354 len, len, v);
1356 isl_int_clear(v);
1358 isl_vec_free(aff->v);
1359 aff->v = vec;
1360 isl_local_space_free(aff->ls);
1361 aff->ls = ls;
1364 return aff;
1365 error:
1366 isl_vec_free(vec);
1367 isl_local_space_free(ls);
1368 return isl_aff_free(aff);
1371 /* Look for any divs j that appear with a unit coefficient inside
1372 * the definitions of other divs i and plug them into the definitions
1373 * of the divs i.
1375 * In particular, an expression of the form
1377 * floor((f(..) + floor(g(..)/n))/m)
1379 * is simplified to
1381 * floor((n * f(..) + g(..))/(n * m))
1383 * This simplification is correct because we can move the expression
1384 * f(..) into the inner floor in the original expression to obtain
1386 * floor(floor((n * f(..) + g(..))/n)/m)
1388 * from which we can derive the simplified expression.
1390 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1392 int i, j, n;
1393 int off;
1395 if (!aff)
1396 return NULL;
1398 n = isl_local_space_dim(aff->ls, isl_dim_div);
1399 off = isl_local_space_offset(aff->ls, isl_dim_div);
1400 for (i = 1; i < n; ++i) {
1401 for (j = 0; j < i; ++j) {
1402 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1403 continue;
1404 aff->ls = isl_local_space_substitute_seq(aff->ls,
1405 isl_dim_div, j, aff->ls->div->row[j],
1406 aff->v->size, i, 1);
1407 if (!aff->ls)
1408 return isl_aff_free(aff);
1412 return aff;
1415 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1417 * Even though this function is only called on isl_affs with a single
1418 * reference, we are careful to only change aff->v and aff->ls together.
1420 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1422 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1423 isl_local_space *ls;
1424 isl_vec *v;
1426 ls = isl_local_space_copy(aff->ls);
1427 ls = isl_local_space_swap_div(ls, a, b);
1428 v = isl_vec_copy(aff->v);
1429 v = isl_vec_cow(v);
1430 if (!ls || !v)
1431 goto error;
1433 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1434 isl_vec_free(aff->v);
1435 aff->v = v;
1436 isl_local_space_free(aff->ls);
1437 aff->ls = ls;
1439 return aff;
1440 error:
1441 isl_vec_free(v);
1442 isl_local_space_free(ls);
1443 return isl_aff_free(aff);
1446 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1448 * We currently do not actually remove div "b", but simply add its
1449 * coefficient to that of "a" and then zero it out.
1451 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1453 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1455 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1456 return aff;
1458 aff->v = isl_vec_cow(aff->v);
1459 if (!aff->v)
1460 return isl_aff_free(aff);
1462 isl_int_add(aff->v->el[1 + off + a],
1463 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1464 isl_int_set_si(aff->v->el[1 + off + b], 0);
1466 return aff;
1469 /* Sort the divs in the local space of "aff" according to
1470 * the comparison function "cmp_row" in isl_local_space.c,
1471 * combining the coefficients of identical divs.
1473 * Reordering divs does not change the semantics of "aff",
1474 * so there is no need to call isl_aff_cow.
1475 * Moreover, this function is currently only called on isl_affs
1476 * with a single reference.
1478 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1480 int i, j, n;
1482 if (!aff)
1483 return NULL;
1485 n = isl_aff_dim(aff, isl_dim_div);
1486 for (i = 1; i < n; ++i) {
1487 for (j = i - 1; j >= 0; --j) {
1488 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1489 if (cmp < 0)
1490 break;
1491 if (cmp == 0)
1492 aff = merge_divs(aff, j, j + 1);
1493 else
1494 aff = swap_div(aff, j, j + 1);
1495 if (!aff)
1496 return NULL;
1500 return aff;
1503 /* Normalize the representation of "aff".
1505 * This function should only be called of "new" isl_affs, i.e.,
1506 * with only a single reference. We therefore do not need to
1507 * worry about affecting other instances.
1509 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1511 if (!aff)
1512 return NULL;
1513 aff->v = isl_vec_normalize(aff->v);
1514 if (!aff->v)
1515 return isl_aff_free(aff);
1516 aff = plug_in_integral_divs(aff);
1517 aff = plug_in_unit_divs(aff);
1518 aff = sort_divs(aff);
1519 aff = isl_aff_remove_unused_divs(aff);
1520 return aff;
1523 /* Given f, return floor(f).
1524 * If f is an integer expression, then just return f.
1525 * If f is a constant, then return the constant floor(f).
1526 * Otherwise, if f = g/m, write g = q m + r,
1527 * create a new div d = [r/m] and return the expression q + d.
1528 * The coefficients in r are taken to lie between -m/2 and m/2.
1530 * As a special case, floor(NaN) = NaN.
1532 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1534 int i;
1535 int size;
1536 isl_ctx *ctx;
1537 isl_vec *div;
1539 if (!aff)
1540 return NULL;
1542 if (isl_aff_is_nan(aff))
1543 return aff;
1544 if (isl_int_is_one(aff->v->el[0]))
1545 return aff;
1547 aff = isl_aff_cow(aff);
1548 if (!aff)
1549 return NULL;
1551 aff->v = isl_vec_cow(aff->v);
1552 if (!aff->v)
1553 return isl_aff_free(aff);
1555 if (isl_aff_is_cst(aff)) {
1556 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1557 isl_int_set_si(aff->v->el[0], 1);
1558 return aff;
1561 div = isl_vec_copy(aff->v);
1562 div = isl_vec_cow(div);
1563 if (!div)
1564 return isl_aff_free(aff);
1566 ctx = isl_aff_get_ctx(aff);
1567 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1568 for (i = 1; i < aff->v->size; ++i) {
1569 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1570 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1571 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1572 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1573 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1577 aff->ls = isl_local_space_add_div(aff->ls, div);
1578 if (!aff->ls)
1579 return isl_aff_free(aff);
1581 size = aff->v->size;
1582 aff->v = isl_vec_extend(aff->v, size + 1);
1583 if (!aff->v)
1584 return isl_aff_free(aff);
1585 isl_int_set_si(aff->v->el[0], 1);
1586 isl_int_set_si(aff->v->el[size], 1);
1588 aff = isl_aff_normalize(aff);
1590 return aff;
1593 /* Compute
1595 * aff mod m = aff - m * floor(aff/m)
1597 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1599 isl_aff *res;
1601 res = isl_aff_copy(aff);
1602 aff = isl_aff_scale_down(aff, m);
1603 aff = isl_aff_floor(aff);
1604 aff = isl_aff_scale(aff, m);
1605 res = isl_aff_sub(res, aff);
1607 return res;
1610 /* Compute
1612 * aff mod m = aff - m * floor(aff/m)
1614 * with m an integer value.
1616 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1617 __isl_take isl_val *m)
1619 isl_aff *res;
1621 if (!aff || !m)
1622 goto error;
1624 if (!isl_val_is_int(m))
1625 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1626 "expecting integer modulo", goto error);
1628 res = isl_aff_copy(aff);
1629 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1630 aff = isl_aff_floor(aff);
1631 aff = isl_aff_scale_val(aff, m);
1632 res = isl_aff_sub(res, aff);
1634 return res;
1635 error:
1636 isl_aff_free(aff);
1637 isl_val_free(m);
1638 return NULL;
1641 /* Compute
1643 * pwaff mod m = pwaff - m * floor(pwaff/m)
1645 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1647 isl_pw_aff *res;
1649 res = isl_pw_aff_copy(pwaff);
1650 pwaff = isl_pw_aff_scale_down(pwaff, m);
1651 pwaff = isl_pw_aff_floor(pwaff);
1652 pwaff = isl_pw_aff_scale(pwaff, m);
1653 res = isl_pw_aff_sub(res, pwaff);
1655 return res;
1658 /* Compute
1660 * pa mod m = pa - m * floor(pa/m)
1662 * with m an integer value.
1664 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1665 __isl_take isl_val *m)
1667 if (!pa || !m)
1668 goto error;
1669 if (!isl_val_is_int(m))
1670 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1671 "expecting integer modulo", goto error);
1672 pa = isl_pw_aff_mod(pa, m->n);
1673 isl_val_free(m);
1674 return pa;
1675 error:
1676 isl_pw_aff_free(pa);
1677 isl_val_free(m);
1678 return NULL;
1681 /* Given f, return ceil(f).
1682 * If f is an integer expression, then just return f.
1683 * Otherwise, let f be the expression
1685 * e/m
1687 * then return
1689 * floor((e + m - 1)/m)
1691 * As a special case, ceil(NaN) = NaN.
1693 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1695 if (!aff)
1696 return NULL;
1698 if (isl_aff_is_nan(aff))
1699 return aff;
1700 if (isl_int_is_one(aff->v->el[0]))
1701 return aff;
1703 aff = isl_aff_cow(aff);
1704 if (!aff)
1705 return NULL;
1706 aff->v = isl_vec_cow(aff->v);
1707 if (!aff->v)
1708 return isl_aff_free(aff);
1710 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1711 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1712 aff = isl_aff_floor(aff);
1714 return aff;
1717 /* Apply the expansion computed by isl_merge_divs.
1718 * The expansion itself is given by "exp" while the resulting
1719 * list of divs is given by "div".
1721 __isl_give isl_aff *isl_aff_expand_divs( __isl_take isl_aff *aff,
1722 __isl_take isl_mat *div, int *exp)
1724 int i, j;
1725 int old_n_div;
1726 int new_n_div;
1727 int offset;
1729 aff = isl_aff_cow(aff);
1730 if (!aff || !div)
1731 goto error;
1733 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1734 new_n_div = isl_mat_rows(div);
1735 if (new_n_div < old_n_div)
1736 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1737 "not an expansion", goto error);
1739 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1740 if (!aff->v)
1741 goto error;
1743 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1744 j = old_n_div - 1;
1745 for (i = new_n_div - 1; i >= 0; --i) {
1746 if (j >= 0 && exp[j] == i) {
1747 if (i != j)
1748 isl_int_swap(aff->v->el[offset + i],
1749 aff->v->el[offset + j]);
1750 j--;
1751 } else
1752 isl_int_set_si(aff->v->el[offset + i], 0);
1755 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1756 if (!aff->ls)
1757 goto error;
1758 isl_mat_free(div);
1759 return aff;
1760 error:
1761 isl_aff_free(aff);
1762 isl_mat_free(div);
1763 return NULL;
1766 /* Add two affine expressions that live in the same local space.
1768 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1769 __isl_take isl_aff *aff2)
1771 isl_int gcd, f;
1773 aff1 = isl_aff_cow(aff1);
1774 if (!aff1 || !aff2)
1775 goto error;
1777 aff1->v = isl_vec_cow(aff1->v);
1778 if (!aff1->v)
1779 goto error;
1781 isl_int_init(gcd);
1782 isl_int_init(f);
1783 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1784 isl_int_divexact(f, aff2->v->el[0], gcd);
1785 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1786 isl_int_divexact(f, aff1->v->el[0], gcd);
1787 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1788 isl_int_divexact(f, aff2->v->el[0], gcd);
1789 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1790 isl_int_clear(f);
1791 isl_int_clear(gcd);
1793 isl_aff_free(aff2);
1794 return aff1;
1795 error:
1796 isl_aff_free(aff1);
1797 isl_aff_free(aff2);
1798 return NULL;
1801 /* Return the sum of "aff1" and "aff2".
1803 * If either of the two is NaN, then the result is NaN.
1805 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1806 __isl_take isl_aff *aff2)
1808 isl_ctx *ctx;
1809 int *exp1 = NULL;
1810 int *exp2 = NULL;
1811 isl_mat *div;
1812 int n_div1, n_div2;
1814 if (!aff1 || !aff2)
1815 goto error;
1817 ctx = isl_aff_get_ctx(aff1);
1818 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1819 isl_die(ctx, isl_error_invalid,
1820 "spaces don't match", goto error);
1822 if (isl_aff_is_nan(aff1)) {
1823 isl_aff_free(aff2);
1824 return aff1;
1826 if (isl_aff_is_nan(aff2)) {
1827 isl_aff_free(aff1);
1828 return aff2;
1831 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1832 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1833 if (n_div1 == 0 && n_div2 == 0)
1834 return add_expanded(aff1, aff2);
1836 exp1 = isl_alloc_array(ctx, int, n_div1);
1837 exp2 = isl_alloc_array(ctx, int, n_div2);
1838 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1839 goto error;
1841 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1842 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1843 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1844 free(exp1);
1845 free(exp2);
1847 return add_expanded(aff1, aff2);
1848 error:
1849 free(exp1);
1850 free(exp2);
1851 isl_aff_free(aff1);
1852 isl_aff_free(aff2);
1853 return NULL;
1856 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1857 __isl_take isl_aff *aff2)
1859 return isl_aff_add(aff1, isl_aff_neg(aff2));
1862 /* Return the result of scaling "aff" by a factor of "f".
1864 * As a special case, f * NaN = NaN.
1866 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1868 isl_int gcd;
1870 if (!aff)
1871 return NULL;
1872 if (isl_aff_is_nan(aff))
1873 return aff;
1875 if (isl_int_is_one(f))
1876 return aff;
1878 aff = isl_aff_cow(aff);
1879 if (!aff)
1880 return NULL;
1881 aff->v = isl_vec_cow(aff->v);
1882 if (!aff->v)
1883 return isl_aff_free(aff);
1885 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1886 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1887 return aff;
1890 isl_int_init(gcd);
1891 isl_int_gcd(gcd, aff->v->el[0], f);
1892 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1893 isl_int_divexact(gcd, f, gcd);
1894 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1895 isl_int_clear(gcd);
1897 return aff;
1900 /* Multiple "aff" by "v".
1902 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1903 __isl_take isl_val *v)
1905 if (!aff || !v)
1906 goto error;
1908 if (isl_val_is_one(v)) {
1909 isl_val_free(v);
1910 return aff;
1913 if (!isl_val_is_rat(v))
1914 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1915 "expecting rational factor", goto error);
1917 aff = isl_aff_scale(aff, v->n);
1918 aff = isl_aff_scale_down(aff, v->d);
1920 isl_val_free(v);
1921 return aff;
1922 error:
1923 isl_aff_free(aff);
1924 isl_val_free(v);
1925 return NULL;
1928 /* Return the result of scaling "aff" down by a factor of "f".
1930 * As a special case, NaN/f = NaN.
1932 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1934 isl_int gcd;
1936 if (!aff)
1937 return NULL;
1938 if (isl_aff_is_nan(aff))
1939 return aff;
1941 if (isl_int_is_one(f))
1942 return aff;
1944 aff = isl_aff_cow(aff);
1945 if (!aff)
1946 return NULL;
1948 if (isl_int_is_zero(f))
1949 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1950 "cannot scale down by zero", return isl_aff_free(aff));
1952 aff->v = isl_vec_cow(aff->v);
1953 if (!aff->v)
1954 return isl_aff_free(aff);
1956 isl_int_init(gcd);
1957 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1958 isl_int_gcd(gcd, gcd, f);
1959 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1960 isl_int_divexact(gcd, f, gcd);
1961 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1962 isl_int_clear(gcd);
1964 return aff;
1967 /* Divide "aff" by "v".
1969 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1970 __isl_take isl_val *v)
1972 if (!aff || !v)
1973 goto error;
1975 if (isl_val_is_one(v)) {
1976 isl_val_free(v);
1977 return aff;
1980 if (!isl_val_is_rat(v))
1981 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1982 "expecting rational factor", goto error);
1983 if (!isl_val_is_pos(v))
1984 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1985 "factor needs to be positive", goto error);
1987 aff = isl_aff_scale(aff, v->d);
1988 aff = isl_aff_scale_down(aff, v->n);
1990 isl_val_free(v);
1991 return aff;
1992 error:
1993 isl_aff_free(aff);
1994 isl_val_free(v);
1995 return NULL;
1998 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2000 isl_int v;
2002 if (f == 1)
2003 return aff;
2005 isl_int_init(v);
2006 isl_int_set_ui(v, f);
2007 aff = isl_aff_scale_down(aff, v);
2008 isl_int_clear(v);
2010 return aff;
2013 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2014 enum isl_dim_type type, unsigned pos, const char *s)
2016 aff = isl_aff_cow(aff);
2017 if (!aff)
2018 return NULL;
2019 if (type == isl_dim_out)
2020 isl_die(aff->v->ctx, isl_error_invalid,
2021 "cannot set name of output/set dimension",
2022 return isl_aff_free(aff));
2023 if (type == isl_dim_in)
2024 type = isl_dim_set;
2025 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2026 if (!aff->ls)
2027 return isl_aff_free(aff);
2029 return aff;
2032 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2033 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2035 aff = isl_aff_cow(aff);
2036 if (!aff)
2037 goto error;
2038 if (type == isl_dim_out)
2039 isl_die(aff->v->ctx, isl_error_invalid,
2040 "cannot set name of output/set dimension",
2041 goto error);
2042 if (type == isl_dim_in)
2043 type = isl_dim_set;
2044 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2045 if (!aff->ls)
2046 return isl_aff_free(aff);
2048 return aff;
2049 error:
2050 isl_id_free(id);
2051 isl_aff_free(aff);
2052 return NULL;
2055 /* Replace the identifier of the input tuple of "aff" by "id".
2056 * type is currently required to be equal to isl_dim_in
2058 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2059 enum isl_dim_type type, __isl_take isl_id *id)
2061 aff = isl_aff_cow(aff);
2062 if (!aff)
2063 goto error;
2064 if (type != isl_dim_out)
2065 isl_die(aff->v->ctx, isl_error_invalid,
2066 "cannot only set id of input tuple", goto error);
2067 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2068 if (!aff->ls)
2069 return isl_aff_free(aff);
2071 return aff;
2072 error:
2073 isl_id_free(id);
2074 isl_aff_free(aff);
2075 return NULL;
2078 /* Exploit the equalities in "eq" to simplify the affine expression
2079 * and the expressions of the integer divisions in the local space.
2080 * The integer divisions in this local space are assumed to appear
2081 * as regular dimensions in "eq".
2083 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2084 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2086 int i, j;
2087 unsigned total;
2088 unsigned n_div;
2090 if (!eq)
2091 goto error;
2092 if (eq->n_eq == 0) {
2093 isl_basic_set_free(eq);
2094 return aff;
2097 aff = isl_aff_cow(aff);
2098 if (!aff)
2099 goto error;
2101 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2102 isl_basic_set_copy(eq));
2103 aff->v = isl_vec_cow(aff->v);
2104 if (!aff->ls || !aff->v)
2105 goto error;
2107 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2108 n_div = eq->n_div;
2109 for (i = 0; i < eq->n_eq; ++i) {
2110 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2111 if (j < 0 || j == 0 || j >= total)
2112 continue;
2114 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2115 &aff->v->el[0]);
2118 isl_basic_set_free(eq);
2119 aff = isl_aff_normalize(aff);
2120 return aff;
2121 error:
2122 isl_basic_set_free(eq);
2123 isl_aff_free(aff);
2124 return NULL;
2127 /* Exploit the equalities in "eq" to simplify the affine expression
2128 * and the expressions of the integer divisions in the local space.
2130 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2131 __isl_take isl_basic_set *eq)
2133 int n_div;
2135 if (!aff || !eq)
2136 goto error;
2137 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2138 if (n_div > 0)
2139 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2140 return isl_aff_substitute_equalities_lifted(aff, eq);
2141 error:
2142 isl_basic_set_free(eq);
2143 isl_aff_free(aff);
2144 return NULL;
2147 /* Look for equalities among the variables shared by context and aff
2148 * and the integer divisions of aff, if any.
2149 * The equalities are then used to eliminate coefficients and/or integer
2150 * divisions from aff.
2152 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2153 __isl_take isl_set *context)
2155 isl_basic_set *hull;
2156 int n_div;
2158 if (!aff)
2159 goto error;
2160 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2161 if (n_div > 0) {
2162 isl_basic_set *bset;
2163 isl_local_space *ls;
2164 context = isl_set_add_dims(context, isl_dim_set, n_div);
2165 ls = isl_aff_get_domain_local_space(aff);
2166 bset = isl_basic_set_from_local_space(ls);
2167 bset = isl_basic_set_lift(bset);
2168 bset = isl_basic_set_flatten(bset);
2169 context = isl_set_intersect(context,
2170 isl_set_from_basic_set(bset));
2173 hull = isl_set_affine_hull(context);
2174 return isl_aff_substitute_equalities_lifted(aff, hull);
2175 error:
2176 isl_aff_free(aff);
2177 isl_set_free(context);
2178 return NULL;
2181 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2182 __isl_take isl_set *context)
2184 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2185 dom_context = isl_set_intersect_params(dom_context, context);
2186 return isl_aff_gist(aff, dom_context);
2189 /* Return a basic set containing those elements in the space
2190 * of aff where it is positive. "rational" should not be set.
2192 * If "aff" is NaN, then it is not positive.
2194 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2195 int rational)
2197 isl_constraint *ineq;
2198 isl_basic_set *bset;
2199 isl_val *c;
2201 if (!aff)
2202 return NULL;
2203 if (isl_aff_is_nan(aff)) {
2204 isl_space *space = isl_aff_get_domain_space(aff);
2205 isl_aff_free(aff);
2206 return isl_basic_set_empty(space);
2208 if (rational)
2209 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2210 "rational sets not supported", goto error);
2212 ineq = isl_inequality_from_aff(aff);
2213 c = isl_constraint_get_constant_val(ineq);
2214 c = isl_val_sub_ui(c, 1);
2215 ineq = isl_constraint_set_constant_val(ineq, c);
2217 bset = isl_basic_set_from_constraint(ineq);
2218 bset = isl_basic_set_simplify(bset);
2219 return bset;
2220 error:
2221 isl_aff_free(aff);
2222 return NULL;
2225 /* Return a basic set containing those elements in the space
2226 * of aff where it is non-negative.
2227 * If "rational" is set, then return a rational basic set.
2229 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2231 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2232 __isl_take isl_aff *aff, int rational)
2234 isl_constraint *ineq;
2235 isl_basic_set *bset;
2237 if (!aff)
2238 return NULL;
2239 if (isl_aff_is_nan(aff)) {
2240 isl_space *space = isl_aff_get_domain_space(aff);
2241 isl_aff_free(aff);
2242 return isl_basic_set_empty(space);
2245 ineq = isl_inequality_from_aff(aff);
2247 bset = isl_basic_set_from_constraint(ineq);
2248 if (rational)
2249 bset = isl_basic_set_set_rational(bset);
2250 bset = isl_basic_set_simplify(bset);
2251 return bset;
2254 /* Return a basic set containing those elements in the space
2255 * of aff where it is non-negative.
2257 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2259 return aff_nonneg_basic_set(aff, 0);
2262 /* Return a basic set containing those elements in the domain space
2263 * of aff where it is negative.
2265 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2267 aff = isl_aff_neg(aff);
2268 aff = isl_aff_add_constant_num_si(aff, -1);
2269 return isl_aff_nonneg_basic_set(aff);
2272 /* Return a basic set containing those elements in the space
2273 * of aff where it is zero.
2274 * If "rational" is set, then return a rational basic set.
2276 * If "aff" is NaN, then it is not zero.
2278 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2279 int rational)
2281 isl_constraint *ineq;
2282 isl_basic_set *bset;
2284 if (!aff)
2285 return NULL;
2286 if (isl_aff_is_nan(aff)) {
2287 isl_space *space = isl_aff_get_domain_space(aff);
2288 isl_aff_free(aff);
2289 return isl_basic_set_empty(space);
2292 ineq = isl_equality_from_aff(aff);
2294 bset = isl_basic_set_from_constraint(ineq);
2295 if (rational)
2296 bset = isl_basic_set_set_rational(bset);
2297 bset = isl_basic_set_simplify(bset);
2298 return bset;
2301 /* Return a basic set containing those elements in the space
2302 * of aff where it is zero.
2304 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2306 return aff_zero_basic_set(aff, 0);
2309 /* Return a basic set containing those elements in the shared space
2310 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2312 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2313 __isl_take isl_aff *aff2)
2315 aff1 = isl_aff_sub(aff1, aff2);
2317 return isl_aff_nonneg_basic_set(aff1);
2320 /* Return a set containing those elements in the shared space
2321 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2323 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2324 __isl_take isl_aff *aff2)
2326 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2329 /* Return a basic set containing those elements in the shared space
2330 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2332 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2333 __isl_take isl_aff *aff2)
2335 return isl_aff_ge_basic_set(aff2, aff1);
2338 /* Return a set containing those elements in the shared space
2339 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2341 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2342 __isl_take isl_aff *aff2)
2344 return isl_aff_ge_set(aff2, aff1);
2347 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2348 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2350 aff1 = isl_aff_add(aff1, aff2);
2351 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2352 return aff1;
2355 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2357 if (!aff)
2358 return -1;
2360 return 0;
2363 /* Check whether the given affine expression has non-zero coefficient
2364 * for any dimension in the given range or if any of these dimensions
2365 * appear with non-zero coefficients in any of the integer divisions
2366 * involved in the affine expression.
2368 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2369 enum isl_dim_type type, unsigned first, unsigned n)
2371 int i;
2372 isl_ctx *ctx;
2373 int *active = NULL;
2374 isl_bool involves = isl_bool_false;
2376 if (!aff)
2377 return isl_bool_error;
2378 if (n == 0)
2379 return isl_bool_false;
2381 ctx = isl_aff_get_ctx(aff);
2382 if (first + n > isl_aff_dim(aff, type))
2383 isl_die(ctx, isl_error_invalid,
2384 "range out of bounds", return isl_bool_error);
2386 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2387 if (!active)
2388 goto error;
2390 first += isl_local_space_offset(aff->ls, type) - 1;
2391 for (i = 0; i < n; ++i)
2392 if (active[first + i]) {
2393 involves = isl_bool_true;
2394 break;
2397 free(active);
2399 return involves;
2400 error:
2401 free(active);
2402 return isl_bool_error;
2405 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2406 enum isl_dim_type type, unsigned first, unsigned n)
2408 isl_ctx *ctx;
2410 if (!aff)
2411 return NULL;
2412 if (type == isl_dim_out)
2413 isl_die(aff->v->ctx, isl_error_invalid,
2414 "cannot drop output/set dimension",
2415 return isl_aff_free(aff));
2416 if (type == isl_dim_in)
2417 type = isl_dim_set;
2418 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2419 return aff;
2421 ctx = isl_aff_get_ctx(aff);
2422 if (first + n > isl_local_space_dim(aff->ls, type))
2423 isl_die(ctx, isl_error_invalid, "range out of bounds",
2424 return isl_aff_free(aff));
2426 aff = isl_aff_cow(aff);
2427 if (!aff)
2428 return NULL;
2430 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2431 if (!aff->ls)
2432 return isl_aff_free(aff);
2434 first += 1 + isl_local_space_offset(aff->ls, type);
2435 aff->v = isl_vec_drop_els(aff->v, first, n);
2436 if (!aff->v)
2437 return isl_aff_free(aff);
2439 return aff;
2442 /* Project the domain of the affine expression onto its parameter space.
2443 * The affine expression may not involve any of the domain dimensions.
2445 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2447 isl_space *space;
2448 unsigned n;
2449 int involves;
2451 n = isl_aff_dim(aff, isl_dim_in);
2452 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2453 if (involves < 0)
2454 return isl_aff_free(aff);
2455 if (involves)
2456 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2457 "affine expression involves some of the domain dimensions",
2458 return isl_aff_free(aff));
2459 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2460 space = isl_aff_get_domain_space(aff);
2461 space = isl_space_params(space);
2462 aff = isl_aff_reset_domain_space(aff, space);
2463 return aff;
2466 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2467 enum isl_dim_type type, unsigned first, unsigned n)
2469 isl_ctx *ctx;
2471 if (!aff)
2472 return NULL;
2473 if (type == isl_dim_out)
2474 isl_die(aff->v->ctx, isl_error_invalid,
2475 "cannot insert output/set dimensions",
2476 return isl_aff_free(aff));
2477 if (type == isl_dim_in)
2478 type = isl_dim_set;
2479 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2480 return aff;
2482 ctx = isl_aff_get_ctx(aff);
2483 if (first > isl_local_space_dim(aff->ls, type))
2484 isl_die(ctx, isl_error_invalid, "position out of bounds",
2485 return isl_aff_free(aff));
2487 aff = isl_aff_cow(aff);
2488 if (!aff)
2489 return NULL;
2491 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2492 if (!aff->ls)
2493 return isl_aff_free(aff);
2495 first += 1 + isl_local_space_offset(aff->ls, type);
2496 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2497 if (!aff->v)
2498 return isl_aff_free(aff);
2500 return aff;
2503 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2504 enum isl_dim_type type, unsigned n)
2506 unsigned pos;
2508 pos = isl_aff_dim(aff, type);
2510 return isl_aff_insert_dims(aff, type, pos, n);
2513 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2514 enum isl_dim_type type, unsigned n)
2516 unsigned pos;
2518 pos = isl_pw_aff_dim(pwaff, type);
2520 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2523 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2524 * to dimensions of "dst_type" at "dst_pos".
2526 * We only support moving input dimensions to parameters and vice versa.
2528 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2529 enum isl_dim_type dst_type, unsigned dst_pos,
2530 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2532 unsigned g_dst_pos;
2533 unsigned g_src_pos;
2535 if (!aff)
2536 return NULL;
2537 if (n == 0 &&
2538 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2539 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2540 return aff;
2542 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2543 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2544 "cannot move output/set dimension",
2545 return isl_aff_free(aff));
2546 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2547 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2548 "cannot move divs", return isl_aff_free(aff));
2549 if (dst_type == isl_dim_in)
2550 dst_type = isl_dim_set;
2551 if (src_type == isl_dim_in)
2552 src_type = isl_dim_set;
2554 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2555 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2556 "range out of bounds", return isl_aff_free(aff));
2557 if (dst_type == src_type)
2558 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2559 "moving dims within the same type not supported",
2560 return isl_aff_free(aff));
2562 aff = isl_aff_cow(aff);
2563 if (!aff)
2564 return NULL;
2566 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2567 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2568 if (dst_type > src_type)
2569 g_dst_pos -= n;
2571 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2572 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2573 src_type, src_pos, n);
2574 if (!aff->v || !aff->ls)
2575 return isl_aff_free(aff);
2577 aff = sort_divs(aff);
2579 return aff;
2582 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2584 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2585 return isl_pw_aff_alloc(dom, aff);
2588 #undef PW
2589 #define PW isl_pw_aff
2590 #undef EL
2591 #define EL isl_aff
2592 #undef EL_IS_ZERO
2593 #define EL_IS_ZERO is_empty
2594 #undef ZERO
2595 #define ZERO empty
2596 #undef IS_ZERO
2597 #define IS_ZERO is_empty
2598 #undef FIELD
2599 #define FIELD aff
2600 #undef DEFAULT_IS_ZERO
2601 #define DEFAULT_IS_ZERO 0
2603 #define NO_EVAL
2604 #define NO_OPT
2605 #define NO_LIFT
2606 #define NO_MORPH
2608 #include <isl_pw_templ.c>
2609 #include <isl_pw_hash.c>
2610 #include <isl_pw_union_opt.c>
2612 #undef UNION
2613 #define UNION isl_union_pw_aff
2614 #undef PART
2615 #define PART isl_pw_aff
2616 #undef PARTS
2617 #define PARTS pw_aff
2619 #include <isl_union_single.c>
2620 #include <isl_union_neg.c>
2622 static __isl_give isl_set *align_params_pw_pw_set_and(
2623 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2624 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2625 __isl_take isl_pw_aff *pwaff2))
2627 if (!pwaff1 || !pwaff2)
2628 goto error;
2629 if (isl_space_match(pwaff1->dim, isl_dim_param,
2630 pwaff2->dim, isl_dim_param))
2631 return fn(pwaff1, pwaff2);
2632 if (!isl_space_has_named_params(pwaff1->dim) ||
2633 !isl_space_has_named_params(pwaff2->dim))
2634 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2635 "unaligned unnamed parameters", goto error);
2636 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2637 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2638 return fn(pwaff1, pwaff2);
2639 error:
2640 isl_pw_aff_free(pwaff1);
2641 isl_pw_aff_free(pwaff2);
2642 return NULL;
2645 /* Align the parameters of the to isl_pw_aff arguments and
2646 * then apply a function "fn" on them that returns an isl_map.
2648 static __isl_give isl_map *align_params_pw_pw_map_and(
2649 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2650 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2651 __isl_take isl_pw_aff *pa2))
2653 if (!pa1 || !pa2)
2654 goto error;
2655 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
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 int 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 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3545 __isl_take isl_pw_aff *pwaff2)
3547 isl_set *le;
3548 isl_set *dom;
3550 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3551 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3552 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3553 isl_pw_aff_copy(pwaff2));
3554 dom = isl_set_subtract(dom, isl_set_copy(le));
3555 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3558 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3559 __isl_take isl_pw_aff *pwaff2)
3561 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3564 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3565 __isl_take isl_pw_aff *pwaff2)
3567 isl_set *ge;
3568 isl_set *dom;
3570 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3571 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3572 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3573 isl_pw_aff_copy(pwaff2));
3574 dom = isl_set_subtract(dom, isl_set_copy(ge));
3575 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3578 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3579 __isl_take isl_pw_aff *pwaff2)
3581 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3584 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3585 __isl_take isl_pw_aff_list *list,
3586 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3587 __isl_take isl_pw_aff *pwaff2))
3589 int i;
3590 isl_ctx *ctx;
3591 isl_pw_aff *res;
3593 if (!list)
3594 return NULL;
3596 ctx = isl_pw_aff_list_get_ctx(list);
3597 if (list->n < 1)
3598 isl_die(ctx, isl_error_invalid,
3599 "list should contain at least one element", goto error);
3601 res = isl_pw_aff_copy(list->p[0]);
3602 for (i = 1; i < list->n; ++i)
3603 res = fn(res, isl_pw_aff_copy(list->p[i]));
3605 isl_pw_aff_list_free(list);
3606 return res;
3607 error:
3608 isl_pw_aff_list_free(list);
3609 return NULL;
3612 /* Return an isl_pw_aff that maps each element in the intersection of the
3613 * domains of the elements of list to the minimal corresponding affine
3614 * expression.
3616 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3618 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3621 /* Return an isl_pw_aff that maps each element in the intersection of the
3622 * domains of the elements of list to the maximal corresponding affine
3623 * expression.
3625 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3627 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3630 /* Mark the domains of "pwaff" as rational.
3632 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3634 int i;
3636 pwaff = isl_pw_aff_cow(pwaff);
3637 if (!pwaff)
3638 return NULL;
3639 if (pwaff->n == 0)
3640 return pwaff;
3642 for (i = 0; i < pwaff->n; ++i) {
3643 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3644 if (!pwaff->p[i].set)
3645 return isl_pw_aff_free(pwaff);
3648 return pwaff;
3651 /* Mark the domains of the elements of "list" as rational.
3653 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3654 __isl_take isl_pw_aff_list *list)
3656 int i, n;
3658 if (!list)
3659 return NULL;
3660 if (list->n == 0)
3661 return list;
3663 n = list->n;
3664 for (i = 0; i < n; ++i) {
3665 isl_pw_aff *pa;
3667 pa = isl_pw_aff_list_get_pw_aff(list, i);
3668 pa = isl_pw_aff_set_rational(pa);
3669 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3672 return list;
3675 /* Do the parameters of "aff" match those of "space"?
3677 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3678 __isl_keep isl_space *space)
3680 isl_space *aff_space;
3681 int match;
3683 if (!aff || !space)
3684 return -1;
3686 aff_space = isl_aff_get_domain_space(aff);
3688 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3690 isl_space_free(aff_space);
3691 return match;
3694 /* Check that the domain space of "aff" matches "space".
3696 * Return 0 on success and -1 on error.
3698 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3699 __isl_keep isl_space *space)
3701 isl_space *aff_space;
3702 int match;
3704 if (!aff || !space)
3705 return -1;
3707 aff_space = isl_aff_get_domain_space(aff);
3709 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3710 if (match < 0)
3711 goto error;
3712 if (!match)
3713 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3714 "parameters don't match", goto error);
3715 match = isl_space_tuple_is_equal(space, isl_dim_in,
3716 aff_space, isl_dim_set);
3717 if (match < 0)
3718 goto error;
3719 if (!match)
3720 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3721 "domains don't match", goto error);
3722 isl_space_free(aff_space);
3723 return 0;
3724 error:
3725 isl_space_free(aff_space);
3726 return -1;
3729 #undef BASE
3730 #define BASE aff
3731 #undef DOMBASE
3732 #define DOMBASE set
3733 #define NO_DOMAIN
3735 #include <isl_multi_templ.c>
3736 #include <isl_multi_apply_set.c>
3737 #include <isl_multi_cmp.c>
3738 #include <isl_multi_floor.c>
3739 #include <isl_multi_gist.c>
3741 #undef NO_DOMAIN
3743 /* Remove any internal structure of the domain of "ma".
3744 * If there is any such internal structure in the input,
3745 * then the name of the corresponding space is also removed.
3747 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3748 __isl_take isl_multi_aff *ma)
3750 isl_space *space;
3752 if (!ma)
3753 return NULL;
3755 if (!ma->space->nested[0])
3756 return ma;
3758 space = isl_multi_aff_get_space(ma);
3759 space = isl_space_flatten_domain(space);
3760 ma = isl_multi_aff_reset_space(ma, space);
3762 return ma;
3765 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3766 * of the space to its domain.
3768 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3770 int i, n_in;
3771 isl_local_space *ls;
3772 isl_multi_aff *ma;
3774 if (!space)
3775 return NULL;
3776 if (!isl_space_is_map(space))
3777 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3778 "not a map space", goto error);
3780 n_in = isl_space_dim(space, isl_dim_in);
3781 space = isl_space_domain_map(space);
3783 ma = isl_multi_aff_alloc(isl_space_copy(space));
3784 if (n_in == 0) {
3785 isl_space_free(space);
3786 return ma;
3789 space = isl_space_domain(space);
3790 ls = isl_local_space_from_space(space);
3791 for (i = 0; i < n_in; ++i) {
3792 isl_aff *aff;
3794 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3795 isl_dim_set, i);
3796 ma = isl_multi_aff_set_aff(ma, i, aff);
3798 isl_local_space_free(ls);
3799 return ma;
3800 error:
3801 isl_space_free(space);
3802 return NULL;
3805 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3806 * of the space to its range.
3808 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3810 int i, n_in, n_out;
3811 isl_local_space *ls;
3812 isl_multi_aff *ma;
3814 if (!space)
3815 return NULL;
3816 if (!isl_space_is_map(space))
3817 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3818 "not a map space", goto error);
3820 n_in = isl_space_dim(space, isl_dim_in);
3821 n_out = isl_space_dim(space, isl_dim_out);
3822 space = isl_space_range_map(space);
3824 ma = isl_multi_aff_alloc(isl_space_copy(space));
3825 if (n_out == 0) {
3826 isl_space_free(space);
3827 return ma;
3830 space = isl_space_domain(space);
3831 ls = isl_local_space_from_space(space);
3832 for (i = 0; i < n_out; ++i) {
3833 isl_aff *aff;
3835 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3836 isl_dim_set, n_in + i);
3837 ma = isl_multi_aff_set_aff(ma, i, aff);
3839 isl_local_space_free(ls);
3840 return ma;
3841 error:
3842 isl_space_free(space);
3843 return NULL;
3846 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3847 * of the space to its range.
3849 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3850 __isl_take isl_space *space)
3852 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3855 /* Given the space of a set and a range of set dimensions,
3856 * construct an isl_multi_aff that projects out those dimensions.
3858 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3859 __isl_take isl_space *space, enum isl_dim_type type,
3860 unsigned first, unsigned n)
3862 int i, dim;
3863 isl_local_space *ls;
3864 isl_multi_aff *ma;
3866 if (!space)
3867 return NULL;
3868 if (!isl_space_is_set(space))
3869 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3870 "expecting set space", goto error);
3871 if (type != isl_dim_set)
3872 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3873 "only set dimensions can be projected out", goto error);
3875 dim = isl_space_dim(space, isl_dim_set);
3876 if (first + n > dim)
3877 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3878 "range out of bounds", goto error);
3880 space = isl_space_from_domain(space);
3881 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3883 if (dim == n)
3884 return isl_multi_aff_alloc(space);
3886 ma = isl_multi_aff_alloc(isl_space_copy(space));
3887 space = isl_space_domain(space);
3888 ls = isl_local_space_from_space(space);
3890 for (i = 0; i < first; ++i) {
3891 isl_aff *aff;
3893 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3894 isl_dim_set, i);
3895 ma = isl_multi_aff_set_aff(ma, i, aff);
3898 for (i = 0; i < dim - (first + n); ++i) {
3899 isl_aff *aff;
3901 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3902 isl_dim_set, first + n + i);
3903 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3906 isl_local_space_free(ls);
3907 return ma;
3908 error:
3909 isl_space_free(space);
3910 return NULL;
3913 /* Given the space of a set and a range of set dimensions,
3914 * construct an isl_pw_multi_aff that projects out those dimensions.
3916 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3917 __isl_take isl_space *space, enum isl_dim_type type,
3918 unsigned first, unsigned n)
3920 isl_multi_aff *ma;
3922 ma = isl_multi_aff_project_out_map(space, type, first, n);
3923 return isl_pw_multi_aff_from_multi_aff(ma);
3926 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3927 * domain.
3929 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3930 __isl_take isl_multi_aff *ma)
3932 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3933 return isl_pw_multi_aff_alloc(dom, ma);
3936 /* Create a piecewise multi-affine expression in the given space that maps each
3937 * input dimension to the corresponding output dimension.
3939 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3940 __isl_take isl_space *space)
3942 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3945 /* Exploit the equalities in "eq" to simplify the affine expressions.
3947 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3948 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3950 int i;
3952 maff = isl_multi_aff_cow(maff);
3953 if (!maff || !eq)
3954 goto error;
3956 for (i = 0; i < maff->n; ++i) {
3957 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3958 isl_basic_set_copy(eq));
3959 if (!maff->p[i])
3960 goto error;
3963 isl_basic_set_free(eq);
3964 return maff;
3965 error:
3966 isl_basic_set_free(eq);
3967 isl_multi_aff_free(maff);
3968 return NULL;
3971 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3972 isl_int f)
3974 int i;
3976 maff = isl_multi_aff_cow(maff);
3977 if (!maff)
3978 return NULL;
3980 for (i = 0; i < maff->n; ++i) {
3981 maff->p[i] = isl_aff_scale(maff->p[i], f);
3982 if (!maff->p[i])
3983 return isl_multi_aff_free(maff);
3986 return maff;
3989 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3990 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3992 maff1 = isl_multi_aff_add(maff1, maff2);
3993 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3994 return maff1;
3997 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
3999 if (!maff)
4000 return -1;
4002 return 0;
4005 /* Return the set of domain elements where "ma1" is lexicographically
4006 * smaller than or equal to "ma2".
4008 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4009 __isl_take isl_multi_aff *ma2)
4011 return isl_multi_aff_lex_ge_set(ma2, ma1);
4014 /* Return the set of domain elements where "ma1" is lexicographically
4015 * smaller than "ma2".
4017 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4018 __isl_take isl_multi_aff *ma2)
4020 return isl_multi_aff_lex_gt_set(ma2, ma1);
4023 /* Return the set of domain elements where "ma1" and "ma2"
4024 * satisfy "order".
4026 static __isl_give isl_set *isl_multi_aff_order_set(
4027 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4028 __isl_give isl_map *order(__isl_take isl_space *set_space))
4030 isl_space *space;
4031 isl_map *map1, *map2;
4032 isl_map *map, *ge;
4034 map1 = isl_map_from_multi_aff(ma1);
4035 map2 = isl_map_from_multi_aff(ma2);
4036 map = isl_map_range_product(map1, map2);
4037 space = isl_space_range(isl_map_get_space(map));
4038 space = isl_space_domain(isl_space_unwrap(space));
4039 ge = order(space);
4040 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4042 return isl_map_domain(map);
4045 /* Return the set of domain elements where "ma1" is lexicographically
4046 * greater than or equal to "ma2".
4048 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4049 __isl_take isl_multi_aff *ma2)
4051 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4054 /* Return the set of domain elements where "ma1" is lexicographically
4055 * greater than "ma2".
4057 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4058 __isl_take isl_multi_aff *ma2)
4060 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4063 #undef PW
4064 #define PW isl_pw_multi_aff
4065 #undef EL
4066 #define EL isl_multi_aff
4067 #undef EL_IS_ZERO
4068 #define EL_IS_ZERO is_empty
4069 #undef ZERO
4070 #define ZERO empty
4071 #undef IS_ZERO
4072 #define IS_ZERO is_empty
4073 #undef FIELD
4074 #define FIELD maff
4075 #undef DEFAULT_IS_ZERO
4076 #define DEFAULT_IS_ZERO 0
4078 #define NO_SUB
4079 #define NO_EVAL
4080 #define NO_OPT
4081 #define NO_INVOLVES_DIMS
4082 #define NO_INSERT_DIMS
4083 #define NO_LIFT
4084 #define NO_MORPH
4086 #include <isl_pw_templ.c>
4087 #include <isl_pw_union_opt.c>
4089 #undef NO_SUB
4091 #undef UNION
4092 #define UNION isl_union_pw_multi_aff
4093 #undef PART
4094 #define PART isl_pw_multi_aff
4095 #undef PARTS
4096 #define PARTS pw_multi_aff
4098 #include <isl_union_multi.c>
4099 #include <isl_union_neg.c>
4101 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4102 __isl_take isl_pw_multi_aff *pma1,
4103 __isl_take isl_pw_multi_aff *pma2)
4105 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4106 &isl_multi_aff_lex_ge_set);
4109 /* Given two piecewise multi affine expressions, return a piecewise
4110 * multi-affine expression defined on the union of the definition domains
4111 * of the inputs that is equal to the lexicographic maximum of the two
4112 * inputs on each cell. If only one of the two inputs is defined on
4113 * a given cell, then it is considered to be the maximum.
4115 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4116 __isl_take isl_pw_multi_aff *pma1,
4117 __isl_take isl_pw_multi_aff *pma2)
4119 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4120 &pw_multi_aff_union_lexmax);
4123 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4124 __isl_take isl_pw_multi_aff *pma1,
4125 __isl_take isl_pw_multi_aff *pma2)
4127 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4128 &isl_multi_aff_lex_le_set);
4131 /* Given two piecewise multi affine expressions, return a piecewise
4132 * multi-affine expression defined on the union of the definition domains
4133 * of the inputs that is equal to the lexicographic minimum of the two
4134 * inputs on each cell. If only one of the two inputs is defined on
4135 * a given cell, then it is considered to be the minimum.
4137 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4138 __isl_take isl_pw_multi_aff *pma1,
4139 __isl_take isl_pw_multi_aff *pma2)
4141 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4142 &pw_multi_aff_union_lexmin);
4145 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4146 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4148 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4149 &isl_multi_aff_add);
4152 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4153 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4155 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4156 &pw_multi_aff_add);
4159 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4160 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4162 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4163 &isl_multi_aff_sub);
4166 /* Subtract "pma2" from "pma1" and return the result.
4168 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4169 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4171 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4172 &pw_multi_aff_sub);
4175 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4176 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4178 return isl_pw_multi_aff_union_add_(pma1, pma2);
4181 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4182 * with the actual sum on the shared domain and
4183 * the defined expression on the symmetric difference of the domains.
4185 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4186 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4188 return isl_union_pw_aff_union_add_(upa1, upa2);
4191 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4192 * with the actual sum on the shared domain and
4193 * the defined expression on the symmetric difference of the domains.
4195 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4196 __isl_take isl_union_pw_multi_aff *upma1,
4197 __isl_take isl_union_pw_multi_aff *upma2)
4199 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4202 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4203 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4205 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4206 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4208 int i, j, n;
4209 isl_space *space;
4210 isl_pw_multi_aff *res;
4212 if (!pma1 || !pma2)
4213 goto error;
4215 n = pma1->n * pma2->n;
4216 space = isl_space_product(isl_space_copy(pma1->dim),
4217 isl_space_copy(pma2->dim));
4218 res = isl_pw_multi_aff_alloc_size(space, n);
4220 for (i = 0; i < pma1->n; ++i) {
4221 for (j = 0; j < pma2->n; ++j) {
4222 isl_set *domain;
4223 isl_multi_aff *ma;
4225 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4226 isl_set_copy(pma2->p[j].set));
4227 ma = isl_multi_aff_product(
4228 isl_multi_aff_copy(pma1->p[i].maff),
4229 isl_multi_aff_copy(pma2->p[j].maff));
4230 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4234 isl_pw_multi_aff_free(pma1);
4235 isl_pw_multi_aff_free(pma2);
4236 return res;
4237 error:
4238 isl_pw_multi_aff_free(pma1);
4239 isl_pw_multi_aff_free(pma2);
4240 return NULL;
4243 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4244 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4246 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4247 &pw_multi_aff_product);
4250 /* Construct a map mapping the domain of the piecewise multi-affine expression
4251 * to its range, with each dimension in the range equated to the
4252 * corresponding affine expression on its cell.
4254 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4256 int i;
4257 isl_map *map;
4259 if (!pma)
4260 return NULL;
4262 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4264 for (i = 0; i < pma->n; ++i) {
4265 isl_multi_aff *maff;
4266 isl_basic_map *bmap;
4267 isl_map *map_i;
4269 maff = isl_multi_aff_copy(pma->p[i].maff);
4270 bmap = isl_basic_map_from_multi_aff(maff);
4271 map_i = isl_map_from_basic_map(bmap);
4272 map_i = isl_map_intersect_domain(map_i,
4273 isl_set_copy(pma->p[i].set));
4274 map = isl_map_union_disjoint(map, map_i);
4277 isl_pw_multi_aff_free(pma);
4278 return map;
4281 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4283 if (!pma)
4284 return NULL;
4286 if (!isl_space_is_set(pma->dim))
4287 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4288 "isl_pw_multi_aff cannot be converted into an isl_set",
4289 goto error);
4291 return isl_map_from_pw_multi_aff(pma);
4292 error:
4293 isl_pw_multi_aff_free(pma);
4294 return NULL;
4297 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4298 * denominator "denom".
4299 * "denom" is allowed to be negative, in which case the actual denominator
4300 * is -denom and the expressions are added instead.
4302 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4303 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4305 int i, first;
4306 int sign;
4307 isl_int d;
4309 first = isl_seq_first_non_zero(c, n);
4310 if (first == -1)
4311 return aff;
4313 sign = isl_int_sgn(denom);
4314 isl_int_init(d);
4315 isl_int_abs(d, denom);
4316 for (i = first; i < n; ++i) {
4317 isl_aff *aff_i;
4319 if (isl_int_is_zero(c[i]))
4320 continue;
4321 aff_i = isl_multi_aff_get_aff(ma, i);
4322 aff_i = isl_aff_scale(aff_i, c[i]);
4323 aff_i = isl_aff_scale_down(aff_i, d);
4324 if (sign >= 0)
4325 aff = isl_aff_sub(aff, aff_i);
4326 else
4327 aff = isl_aff_add(aff, aff_i);
4329 isl_int_clear(d);
4331 return aff;
4334 /* Extract an affine expression that expresses the output dimension "pos"
4335 * of "bmap" in terms of the parameters and input dimensions from
4336 * equality "eq".
4337 * Note that this expression may involve integer divisions defined
4338 * in terms of parameters and input dimensions.
4339 * The equality may also involve references to earlier (but not later)
4340 * output dimensions. These are replaced by the corresponding elements
4341 * in "ma".
4343 * If the equality is of the form
4345 * f(i) + h(j) + a x + g(i) = 0,
4347 * with f(i) a linear combinations of the parameters and input dimensions,
4348 * g(i) a linear combination of integer divisions defined in terms of the same
4349 * and h(j) a linear combinations of earlier output dimensions,
4350 * then the affine expression is
4352 * (-f(i) - g(i))/a - h(j)/a
4354 * If the equality is of the form
4356 * f(i) + h(j) - a x + g(i) = 0,
4358 * then the affine expression is
4360 * (f(i) + g(i))/a - h(j)/(-a)
4363 * If "div" refers to an integer division (i.e., it is smaller than
4364 * the number of integer divisions), then the equality constraint
4365 * does involve an integer division (the one at position "div") that
4366 * is defined in terms of output dimensions. However, this integer
4367 * division can be eliminated by exploiting a pair of constraints
4368 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4369 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4370 * -l + x >= 0.
4371 * In particular, let
4373 * x = e(i) + m floor(...)
4375 * with e(i) the expression derived above and floor(...) the integer
4376 * division involving output dimensions.
4377 * From
4379 * l <= x <= l + n,
4381 * we have
4383 * 0 <= x - l <= n
4385 * This means
4387 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4388 * = (e(i) - l) mod m
4390 * Therefore,
4392 * x - l = (e(i) - l) mod m
4394 * or
4396 * x = ((e(i) - l) mod m) + l
4398 * The variable "shift" below contains the expression -l, which may
4399 * also involve a linear combination of earlier output dimensions.
4401 static __isl_give isl_aff *extract_aff_from_equality(
4402 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4403 __isl_keep isl_multi_aff *ma)
4405 unsigned o_out;
4406 unsigned n_div, n_out;
4407 isl_ctx *ctx;
4408 isl_local_space *ls;
4409 isl_aff *aff, *shift;
4410 isl_val *mod;
4412 ctx = isl_basic_map_get_ctx(bmap);
4413 ls = isl_basic_map_get_local_space(bmap);
4414 ls = isl_local_space_domain(ls);
4415 aff = isl_aff_alloc(isl_local_space_copy(ls));
4416 if (!aff)
4417 goto error;
4418 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4419 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4420 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4421 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4422 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4423 isl_seq_cpy(aff->v->el + 1 + o_out,
4424 bmap->eq[eq] + o_out + n_out, n_div);
4425 } else {
4426 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4427 isl_seq_neg(aff->v->el + 1 + o_out,
4428 bmap->eq[eq] + o_out + n_out, n_div);
4430 if (div < n_div)
4431 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4432 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4433 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4434 bmap->eq[eq][o_out + pos]);
4435 if (div < n_div) {
4436 shift = isl_aff_alloc(isl_local_space_copy(ls));
4437 if (!shift)
4438 goto error;
4439 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4440 isl_seq_cpy(shift->v->el + 1 + o_out,
4441 bmap->ineq[ineq] + o_out + n_out, n_div);
4442 isl_int_set_si(shift->v->el[0], 1);
4443 shift = subtract_initial(shift, ma, pos,
4444 bmap->ineq[ineq] + o_out, ctx->negone);
4445 aff = isl_aff_add(aff, isl_aff_copy(shift));
4446 mod = isl_val_int_from_isl_int(ctx,
4447 bmap->eq[eq][o_out + n_out + div]);
4448 mod = isl_val_abs(mod);
4449 aff = isl_aff_mod_val(aff, mod);
4450 aff = isl_aff_sub(aff, shift);
4453 isl_local_space_free(ls);
4454 return aff;
4455 error:
4456 isl_local_space_free(ls);
4457 isl_aff_free(aff);
4458 return NULL;
4461 /* Given a basic map with output dimensions defined
4462 * in terms of the parameters input dimensions and earlier
4463 * output dimensions using an equality (and possibly a pair on inequalities),
4464 * extract an isl_aff that expresses output dimension "pos" in terms
4465 * of the parameters and input dimensions.
4466 * Note that this expression may involve integer divisions defined
4467 * in terms of parameters and input dimensions.
4468 * "ma" contains the expressions corresponding to earlier output dimensions.
4470 * This function shares some similarities with
4471 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4473 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4474 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4476 int eq, div, ineq;
4477 isl_aff *aff;
4479 if (!bmap)
4480 return NULL;
4481 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4482 if (eq >= bmap->n_eq)
4483 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4484 "unable to find suitable equality", return NULL);
4485 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4487 aff = isl_aff_remove_unused_divs(aff);
4488 return aff;
4491 /* Given a basic map where each output dimension is defined
4492 * in terms of the parameters and input dimensions using an equality,
4493 * extract an isl_multi_aff that expresses the output dimensions in terms
4494 * of the parameters and input dimensions.
4496 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4497 __isl_take isl_basic_map *bmap)
4499 int i;
4500 unsigned n_out;
4501 isl_multi_aff *ma;
4503 if (!bmap)
4504 return NULL;
4506 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4507 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4509 for (i = 0; i < n_out; ++i) {
4510 isl_aff *aff;
4512 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4513 ma = isl_multi_aff_set_aff(ma, i, aff);
4516 isl_basic_map_free(bmap);
4518 return ma;
4521 /* Given a basic set where each set dimension is defined
4522 * in terms of the parameters using an equality,
4523 * extract an isl_multi_aff that expresses the set dimensions in terms
4524 * of the parameters.
4526 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4527 __isl_take isl_basic_set *bset)
4529 return extract_isl_multi_aff_from_basic_map(bset);
4532 /* Create an isl_pw_multi_aff that is equivalent to
4533 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4534 * The given basic map is such that each output dimension is defined
4535 * in terms of the parameters and input dimensions using an equality.
4537 * Since some applications expect the result of isl_pw_multi_aff_from_map
4538 * to only contain integer affine expressions, we compute the floor
4539 * of the expression before returning.
4541 * Remove all constraints involving local variables without
4542 * an explicit representation (resulting in the removal of those
4543 * local variables) prior to the actual extraction to ensure
4544 * that the local spaces in which the resulting affine expressions
4545 * are created do not contain any unknown local variables.
4546 * Removing such constraints is safe because constraints involving
4547 * unknown local variables are not used to determine whether
4548 * a basic map is obviously single-valued.
4550 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4551 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4553 isl_multi_aff *ma;
4555 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4556 ma = extract_isl_multi_aff_from_basic_map(bmap);
4557 ma = isl_multi_aff_floor(ma);
4558 return isl_pw_multi_aff_alloc(domain, ma);
4561 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4562 * This obviously only works if the input "map" is single-valued.
4563 * If so, we compute the lexicographic minimum of the image in the form
4564 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4565 * to its lexicographic minimum.
4566 * If the input is not single-valued, we produce an error.
4568 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4569 __isl_take isl_map *map)
4571 int i;
4572 int sv;
4573 isl_pw_multi_aff *pma;
4575 sv = isl_map_is_single_valued(map);
4576 if (sv < 0)
4577 goto error;
4578 if (!sv)
4579 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4580 "map is not single-valued", goto error);
4581 map = isl_map_make_disjoint(map);
4582 if (!map)
4583 return NULL;
4585 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4587 for (i = 0; i < map->n; ++i) {
4588 isl_pw_multi_aff *pma_i;
4589 isl_basic_map *bmap;
4590 bmap = isl_basic_map_copy(map->p[i]);
4591 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4592 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4595 isl_map_free(map);
4596 return pma;
4597 error:
4598 isl_map_free(map);
4599 return NULL;
4602 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4603 * taking into account that the output dimension at position "d"
4604 * can be represented as
4606 * x = floor((e(...) + c1) / m)
4608 * given that constraint "i" is of the form
4610 * e(...) + c1 - m x >= 0
4613 * Let "map" be of the form
4615 * A -> B
4617 * We construct a mapping
4619 * A -> [A -> x = floor(...)]
4621 * apply that to the map, obtaining
4623 * [A -> x = floor(...)] -> B
4625 * and equate dimension "d" to x.
4626 * We then compute a isl_pw_multi_aff representation of the resulting map
4627 * and plug in the mapping above.
4629 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4630 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4632 isl_ctx *ctx;
4633 isl_space *space;
4634 isl_local_space *ls;
4635 isl_multi_aff *ma;
4636 isl_aff *aff;
4637 isl_vec *v;
4638 isl_map *insert;
4639 int offset;
4640 int n;
4641 int n_in;
4642 isl_pw_multi_aff *pma;
4643 int is_set;
4645 is_set = isl_map_is_set(map);
4647 offset = isl_basic_map_offset(hull, isl_dim_out);
4648 ctx = isl_map_get_ctx(map);
4649 space = isl_space_domain(isl_map_get_space(map));
4650 n_in = isl_space_dim(space, isl_dim_set);
4651 n = isl_space_dim(space, isl_dim_all);
4653 v = isl_vec_alloc(ctx, 1 + 1 + n);
4654 if (v) {
4655 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4656 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4658 isl_basic_map_free(hull);
4660 ls = isl_local_space_from_space(isl_space_copy(space));
4661 aff = isl_aff_alloc_vec(ls, v);
4662 aff = isl_aff_floor(aff);
4663 if (is_set) {
4664 isl_space_free(space);
4665 ma = isl_multi_aff_from_aff(aff);
4666 } else {
4667 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4668 ma = isl_multi_aff_range_product(ma,
4669 isl_multi_aff_from_aff(aff));
4672 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4673 map = isl_map_apply_domain(map, insert);
4674 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4675 pma = isl_pw_multi_aff_from_map(map);
4676 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4678 return pma;
4681 /* Is constraint "c" of the form
4683 * e(...) + c1 - m x >= 0
4685 * or
4687 * -e(...) + c2 + m x >= 0
4689 * where m > 1 and e only depends on parameters and input dimemnsions?
4691 * "offset" is the offset of the output dimensions
4692 * "pos" is the position of output dimension x.
4694 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4696 if (isl_int_is_zero(c[offset + d]))
4697 return 0;
4698 if (isl_int_is_one(c[offset + d]))
4699 return 0;
4700 if (isl_int_is_negone(c[offset + d]))
4701 return 0;
4702 if (isl_seq_first_non_zero(c + offset, d) != -1)
4703 return 0;
4704 if (isl_seq_first_non_zero(c + offset + d + 1,
4705 total - (offset + d + 1)) != -1)
4706 return 0;
4707 return 1;
4710 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4712 * As a special case, we first check if there is any pair of constraints,
4713 * shared by all the basic maps in "map" that force a given dimension
4714 * to be equal to the floor of some affine combination of the input dimensions.
4716 * In particular, if we can find two constraints
4718 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4720 * and
4722 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4724 * where m > 1 and e only depends on parameters and input dimemnsions,
4725 * and such that
4727 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4729 * then we know that we can take
4731 * x = floor((e(...) + c1) / m)
4733 * without having to perform any computation.
4735 * Note that we know that
4737 * c1 + c2 >= 1
4739 * If c1 + c2 were 0, then we would have detected an equality during
4740 * simplification. If c1 + c2 were negative, then we would have detected
4741 * a contradiction.
4743 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4744 __isl_take isl_map *map)
4746 int d, dim;
4747 int i, j, n;
4748 int offset, total;
4749 isl_int sum;
4750 isl_basic_map *hull;
4752 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4753 if (!hull)
4754 goto error;
4756 isl_int_init(sum);
4757 dim = isl_map_dim(map, isl_dim_out);
4758 offset = isl_basic_map_offset(hull, isl_dim_out);
4759 total = 1 + isl_basic_map_total_dim(hull);
4760 n = hull->n_ineq;
4761 for (d = 0; d < dim; ++d) {
4762 for (i = 0; i < n; ++i) {
4763 if (!is_potential_div_constraint(hull->ineq[i],
4764 offset, d, total))
4765 continue;
4766 for (j = i + 1; j < n; ++j) {
4767 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4768 hull->ineq[j] + 1, total - 1))
4769 continue;
4770 isl_int_add(sum, hull->ineq[i][0],
4771 hull->ineq[j][0]);
4772 if (isl_int_abs_lt(sum,
4773 hull->ineq[i][offset + d]))
4774 break;
4777 if (j >= n)
4778 continue;
4779 isl_int_clear(sum);
4780 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4781 j = i;
4782 return pw_multi_aff_from_map_div(map, hull, d, j);
4785 isl_int_clear(sum);
4786 isl_basic_map_free(hull);
4787 return pw_multi_aff_from_map_base(map);
4788 error:
4789 isl_map_free(map);
4790 isl_basic_map_free(hull);
4791 return NULL;
4794 /* Given an affine expression
4796 * [A -> B] -> f(A,B)
4798 * construct an isl_multi_aff
4800 * [A -> B] -> B'
4802 * such that dimension "d" in B' is set to "aff" and the remaining
4803 * dimensions are set equal to the corresponding dimensions in B.
4804 * "n_in" is the dimension of the space A.
4805 * "n_out" is the dimension of the space B.
4807 * If "is_set" is set, then the affine expression is of the form
4809 * [B] -> f(B)
4811 * and we construct an isl_multi_aff
4813 * B -> B'
4815 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4816 unsigned n_in, unsigned n_out, int is_set)
4818 int i;
4819 isl_multi_aff *ma;
4820 isl_space *space, *space2;
4821 isl_local_space *ls;
4823 space = isl_aff_get_domain_space(aff);
4824 ls = isl_local_space_from_space(isl_space_copy(space));
4825 space2 = isl_space_copy(space);
4826 if (!is_set)
4827 space2 = isl_space_range(isl_space_unwrap(space2));
4828 space = isl_space_map_from_domain_and_range(space, space2);
4829 ma = isl_multi_aff_alloc(space);
4830 ma = isl_multi_aff_set_aff(ma, d, aff);
4832 for (i = 0; i < n_out; ++i) {
4833 if (i == d)
4834 continue;
4835 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4836 isl_dim_set, n_in + i);
4837 ma = isl_multi_aff_set_aff(ma, i, aff);
4840 isl_local_space_free(ls);
4842 return ma;
4845 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4846 * taking into account that the dimension at position "d" can be written as
4848 * x = m a + f(..) (1)
4850 * where m is equal to "gcd".
4851 * "i" is the index of the equality in "hull" that defines f(..).
4852 * In particular, the equality is of the form
4854 * f(..) - x + m g(existentials) = 0
4856 * or
4858 * -f(..) + x + m g(existentials) = 0
4860 * We basically plug (1) into "map", resulting in a map with "a"
4861 * in the range instead of "x". The corresponding isl_pw_multi_aff
4862 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4864 * Specifically, given the input map
4866 * A -> B
4868 * We first wrap it into a set
4870 * [A -> B]
4872 * and define (1) on top of the corresponding space, resulting in "aff".
4873 * We use this to create an isl_multi_aff that maps the output position "d"
4874 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4875 * We plug this into the wrapped map, unwrap the result and compute the
4876 * corresponding isl_pw_multi_aff.
4877 * The result is an expression
4879 * A -> T(A)
4881 * We adjust that to
4883 * A -> [A -> T(A)]
4885 * so that we can plug that into "aff", after extending the latter to
4886 * a mapping
4888 * [A -> B] -> B'
4891 * If "map" is actually a set, then there is no "A" space, meaning
4892 * that we do not need to perform any wrapping, and that the result
4893 * of the recursive call is of the form
4895 * [T]
4897 * which is plugged into a mapping of the form
4899 * B -> B'
4901 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4902 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4903 isl_int gcd)
4905 isl_set *set;
4906 isl_space *space;
4907 isl_local_space *ls;
4908 isl_aff *aff;
4909 isl_multi_aff *ma;
4910 isl_pw_multi_aff *pma, *id;
4911 unsigned n_in;
4912 unsigned o_out;
4913 unsigned n_out;
4914 int is_set;
4916 is_set = isl_map_is_set(map);
4918 n_in = isl_basic_map_dim(hull, isl_dim_in);
4919 n_out = isl_basic_map_dim(hull, isl_dim_out);
4920 o_out = isl_basic_map_offset(hull, isl_dim_out);
4922 if (is_set)
4923 set = map;
4924 else
4925 set = isl_map_wrap(map);
4926 space = isl_space_map_from_set(isl_set_get_space(set));
4927 ma = isl_multi_aff_identity(space);
4928 ls = isl_local_space_from_space(isl_set_get_space(set));
4929 aff = isl_aff_alloc(ls);
4930 if (aff) {
4931 isl_int_set_si(aff->v->el[0], 1);
4932 if (isl_int_is_one(hull->eq[i][o_out + d]))
4933 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4934 aff->v->size - 1);
4935 else
4936 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4937 aff->v->size - 1);
4938 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4940 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4941 set = isl_set_preimage_multi_aff(set, ma);
4943 ma = range_map(aff, d, n_in, n_out, is_set);
4945 if (is_set)
4946 map = set;
4947 else
4948 map = isl_set_unwrap(set);
4949 pma = isl_pw_multi_aff_from_map(map);
4951 if (!is_set) {
4952 space = isl_pw_multi_aff_get_domain_space(pma);
4953 space = isl_space_map_from_set(space);
4954 id = isl_pw_multi_aff_identity(space);
4955 pma = isl_pw_multi_aff_range_product(id, pma);
4957 id = isl_pw_multi_aff_from_multi_aff(ma);
4958 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4960 isl_basic_map_free(hull);
4961 return pma;
4964 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4966 * As a special case, we first check if all output dimensions are uniquely
4967 * defined in terms of the parameters and input dimensions over the entire
4968 * domain. If so, we extract the desired isl_pw_multi_aff directly
4969 * from the affine hull of "map" and its domain.
4971 * Otherwise, we check if any of the output dimensions is "strided".
4972 * That is, we check if can be written as
4974 * x = m a + f(..)
4976 * with m greater than 1, a some combination of existentially quantified
4977 * variables and f an expression in the parameters and input dimensions.
4978 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4980 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4981 * special case.
4983 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
4985 int i, j;
4986 isl_bool sv;
4987 isl_basic_map *hull;
4988 unsigned n_out;
4989 unsigned o_out;
4990 unsigned n_div;
4991 unsigned o_div;
4992 isl_int gcd;
4994 if (!map)
4995 return NULL;
4997 map = isl_map_detect_equalities(map);
4998 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4999 sv = isl_basic_map_plain_is_single_valued(hull);
5000 if (sv >= 0 && sv)
5001 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5002 if (sv < 0)
5003 hull = isl_basic_map_free(hull);
5004 if (!hull)
5005 goto error;
5007 n_div = isl_basic_map_dim(hull, isl_dim_div);
5008 o_div = isl_basic_map_offset(hull, isl_dim_div);
5010 if (n_div == 0) {
5011 isl_basic_map_free(hull);
5012 return pw_multi_aff_from_map_check_div(map);
5015 isl_int_init(gcd);
5017 n_out = isl_basic_map_dim(hull, isl_dim_out);
5018 o_out = isl_basic_map_offset(hull, isl_dim_out);
5020 for (i = 0; i < n_out; ++i) {
5021 for (j = 0; j < hull->n_eq; ++j) {
5022 isl_int *eq = hull->eq[j];
5023 isl_pw_multi_aff *res;
5025 if (!isl_int_is_one(eq[o_out + i]) &&
5026 !isl_int_is_negone(eq[o_out + i]))
5027 continue;
5028 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5029 continue;
5030 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5031 n_out - (i + 1)) != -1)
5032 continue;
5033 isl_seq_gcd(eq + o_div, n_div, &gcd);
5034 if (isl_int_is_zero(gcd))
5035 continue;
5036 if (isl_int_is_one(gcd))
5037 continue;
5039 res = pw_multi_aff_from_map_stride(map, hull,
5040 i, j, gcd);
5041 isl_int_clear(gcd);
5042 return res;
5046 isl_int_clear(gcd);
5047 isl_basic_map_free(hull);
5048 return pw_multi_aff_from_map_check_div(map);
5049 error:
5050 isl_map_free(map);
5051 return NULL;
5054 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5056 return isl_pw_multi_aff_from_map(set);
5059 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5060 * add it to *user.
5062 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5064 isl_union_pw_multi_aff **upma = user;
5065 isl_pw_multi_aff *pma;
5067 pma = isl_pw_multi_aff_from_map(map);
5068 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5070 return *upma ? isl_stat_ok : isl_stat_error;
5073 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5074 * domain.
5076 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5077 __isl_take isl_aff *aff)
5079 isl_multi_aff *ma;
5080 isl_pw_multi_aff *pma;
5082 ma = isl_multi_aff_from_aff(aff);
5083 pma = isl_pw_multi_aff_from_multi_aff(ma);
5084 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5087 /* Try and create an isl_union_pw_multi_aff that is equivalent
5088 * to the given isl_union_map.
5089 * The isl_union_map is required to be single-valued in each space.
5090 * Otherwise, an error is produced.
5092 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5093 __isl_take isl_union_map *umap)
5095 isl_space *space;
5096 isl_union_pw_multi_aff *upma;
5098 space = isl_union_map_get_space(umap);
5099 upma = isl_union_pw_multi_aff_empty(space);
5100 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5101 upma = isl_union_pw_multi_aff_free(upma);
5102 isl_union_map_free(umap);
5104 return upma;
5107 /* Try and create an isl_union_pw_multi_aff that is equivalent
5108 * to the given isl_union_set.
5109 * The isl_union_set is required to be a singleton in each space.
5110 * Otherwise, an error is produced.
5112 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5113 __isl_take isl_union_set *uset)
5115 return isl_union_pw_multi_aff_from_union_map(uset);
5118 /* Return the piecewise affine expression "set ? 1 : 0".
5120 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5122 isl_pw_aff *pa;
5123 isl_space *space = isl_set_get_space(set);
5124 isl_local_space *ls = isl_local_space_from_space(space);
5125 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5126 isl_aff *one = isl_aff_zero_on_domain(ls);
5128 one = isl_aff_add_constant_si(one, 1);
5129 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5130 set = isl_set_complement(set);
5131 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5133 return pa;
5136 /* Plug in "subs" for dimension "type", "pos" of "aff".
5138 * Let i be the dimension to replace and let "subs" be of the form
5140 * f/d
5142 * and "aff" of the form
5144 * (a i + g)/m
5146 * The result is
5148 * (a f + d g')/(m d)
5150 * where g' is the result of plugging in "subs" in each of the integer
5151 * divisions in g.
5153 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5154 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5156 isl_ctx *ctx;
5157 isl_int v;
5159 aff = isl_aff_cow(aff);
5160 if (!aff || !subs)
5161 return isl_aff_free(aff);
5163 ctx = isl_aff_get_ctx(aff);
5164 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5165 isl_die(ctx, isl_error_invalid,
5166 "spaces don't match", return isl_aff_free(aff));
5167 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5168 isl_die(ctx, isl_error_unsupported,
5169 "cannot handle divs yet", return isl_aff_free(aff));
5171 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5172 if (!aff->ls)
5173 return isl_aff_free(aff);
5175 aff->v = isl_vec_cow(aff->v);
5176 if (!aff->v)
5177 return isl_aff_free(aff);
5179 pos += isl_local_space_offset(aff->ls, type);
5181 isl_int_init(v);
5182 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5183 aff->v->size, subs->v->size, v);
5184 isl_int_clear(v);
5186 return aff;
5189 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5190 * expressions in "maff".
5192 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5193 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5194 __isl_keep isl_aff *subs)
5196 int i;
5198 maff = isl_multi_aff_cow(maff);
5199 if (!maff || !subs)
5200 return isl_multi_aff_free(maff);
5202 if (type == isl_dim_in)
5203 type = isl_dim_set;
5205 for (i = 0; i < maff->n; ++i) {
5206 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5207 if (!maff->p[i])
5208 return isl_multi_aff_free(maff);
5211 return maff;
5214 /* Plug in "subs" for dimension "type", "pos" of "pma".
5216 * pma is of the form
5218 * A_i(v) -> M_i(v)
5220 * while subs is of the form
5222 * v' = B_j(v) -> S_j
5224 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5225 * has a contribution in the result, in particular
5227 * C_ij(S_j) -> M_i(S_j)
5229 * Note that plugging in S_j in C_ij may also result in an empty set
5230 * and this contribution should simply be discarded.
5232 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5233 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5234 __isl_keep isl_pw_aff *subs)
5236 int i, j, n;
5237 isl_pw_multi_aff *res;
5239 if (!pma || !subs)
5240 return isl_pw_multi_aff_free(pma);
5242 n = pma->n * subs->n;
5243 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5245 for (i = 0; i < pma->n; ++i) {
5246 for (j = 0; j < subs->n; ++j) {
5247 isl_set *common;
5248 isl_multi_aff *res_ij;
5249 int empty;
5251 common = isl_set_intersect(
5252 isl_set_copy(pma->p[i].set),
5253 isl_set_copy(subs->p[j].set));
5254 common = isl_set_substitute(common,
5255 type, pos, subs->p[j].aff);
5256 empty = isl_set_plain_is_empty(common);
5257 if (empty < 0 || empty) {
5258 isl_set_free(common);
5259 if (empty < 0)
5260 goto error;
5261 continue;
5264 res_ij = isl_multi_aff_substitute(
5265 isl_multi_aff_copy(pma->p[i].maff),
5266 type, pos, subs->p[j].aff);
5268 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5272 isl_pw_multi_aff_free(pma);
5273 return res;
5274 error:
5275 isl_pw_multi_aff_free(pma);
5276 isl_pw_multi_aff_free(res);
5277 return NULL;
5280 /* Compute the preimage of a range of dimensions in the affine expression "src"
5281 * under "ma" and put the result in "dst". The number of dimensions in "src"
5282 * that precede the range is given by "n_before". The number of dimensions
5283 * in the range is given by the number of output dimensions of "ma".
5284 * The number of dimensions that follow the range is given by "n_after".
5285 * If "has_denom" is set (to one),
5286 * then "src" and "dst" have an extra initial denominator.
5287 * "n_div_ma" is the number of existentials in "ma"
5288 * "n_div_bset" is the number of existentials in "src"
5289 * The resulting "dst" (which is assumed to have been allocated by
5290 * the caller) contains coefficients for both sets of existentials,
5291 * first those in "ma" and then those in "src".
5292 * f, c1, c2 and g are temporary objects that have been initialized
5293 * by the caller.
5295 * Let src represent the expression
5297 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5299 * and let ma represent the expressions
5301 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5303 * We start out with the following expression for dst:
5305 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5307 * with the multiplication factor f initially equal to 1
5308 * and f \sum_i b_i v_i kept separately.
5309 * For each x_i that we substitute, we multiply the numerator
5310 * (and denominator) of dst by c_1 = m_i and add the numerator
5311 * of the x_i expression multiplied by c_2 = f b_i,
5312 * after removing the common factors of c_1 and c_2.
5313 * The multiplication factor f also needs to be multiplied by c_1
5314 * for the next x_j, j > i.
5316 void isl_seq_preimage(isl_int *dst, isl_int *src,
5317 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5318 int n_div_ma, int n_div_bmap,
5319 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5321 int i;
5322 int n_param, n_in, n_out;
5323 int o_dst, o_src;
5325 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5326 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5327 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5329 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5330 o_dst = o_src = has_denom + 1 + n_param + n_before;
5331 isl_seq_clr(dst + o_dst, n_in);
5332 o_dst += n_in;
5333 o_src += n_out;
5334 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5335 o_dst += n_after;
5336 o_src += n_after;
5337 isl_seq_clr(dst + o_dst, n_div_ma);
5338 o_dst += n_div_ma;
5339 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5341 isl_int_set_si(f, 1);
5343 for (i = 0; i < n_out; ++i) {
5344 int offset = has_denom + 1 + n_param + n_before + i;
5346 if (isl_int_is_zero(src[offset]))
5347 continue;
5348 isl_int_set(c1, ma->p[i]->v->el[0]);
5349 isl_int_mul(c2, f, src[offset]);
5350 isl_int_gcd(g, c1, c2);
5351 isl_int_divexact(c1, c1, g);
5352 isl_int_divexact(c2, c2, g);
5354 isl_int_mul(f, f, c1);
5355 o_dst = has_denom;
5356 o_src = 1;
5357 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5358 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5359 o_dst += 1 + n_param;
5360 o_src += 1 + n_param;
5361 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5362 o_dst += n_before;
5363 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5364 c2, ma->p[i]->v->el + o_src, n_in);
5365 o_dst += n_in;
5366 o_src += n_in;
5367 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5368 o_dst += n_after;
5369 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5370 c2, ma->p[i]->v->el + o_src, n_div_ma);
5371 o_dst += n_div_ma;
5372 o_src += n_div_ma;
5373 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5374 if (has_denom)
5375 isl_int_mul(dst[0], dst[0], c1);
5379 /* Compute the pullback of "aff" by the function represented by "ma".
5380 * In other words, plug in "ma" in "aff". The result is an affine expression
5381 * defined over the domain space of "ma".
5383 * If "aff" is represented by
5385 * (a(p) + b x + c(divs))/d
5387 * and ma is represented by
5389 * x = D(p) + F(y) + G(divs')
5391 * then the result is
5393 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5395 * The divs in the local space of the input are similarly adjusted
5396 * through a call to isl_local_space_preimage_multi_aff.
5398 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5399 __isl_take isl_multi_aff *ma)
5401 isl_aff *res = NULL;
5402 isl_local_space *ls;
5403 int n_div_aff, n_div_ma;
5404 isl_int f, c1, c2, g;
5406 ma = isl_multi_aff_align_divs(ma);
5407 if (!aff || !ma)
5408 goto error;
5410 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5411 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5413 ls = isl_aff_get_domain_local_space(aff);
5414 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5415 res = isl_aff_alloc(ls);
5416 if (!res)
5417 goto error;
5419 isl_int_init(f);
5420 isl_int_init(c1);
5421 isl_int_init(c2);
5422 isl_int_init(g);
5424 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5425 f, c1, c2, g, 1);
5427 isl_int_clear(f);
5428 isl_int_clear(c1);
5429 isl_int_clear(c2);
5430 isl_int_clear(g);
5432 isl_aff_free(aff);
5433 isl_multi_aff_free(ma);
5434 res = isl_aff_normalize(res);
5435 return res;
5436 error:
5437 isl_aff_free(aff);
5438 isl_multi_aff_free(ma);
5439 isl_aff_free(res);
5440 return NULL;
5443 /* Compute the pullback of "aff1" by the function represented by "aff2".
5444 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5445 * defined over the domain space of "aff1".
5447 * The domain of "aff1" should match the range of "aff2", which means
5448 * that it should be single-dimensional.
5450 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5451 __isl_take isl_aff *aff2)
5453 isl_multi_aff *ma;
5455 ma = isl_multi_aff_from_aff(aff2);
5456 return isl_aff_pullback_multi_aff(aff1, ma);
5459 /* Compute the pullback of "ma1" by the function represented by "ma2".
5460 * In other words, plug in "ma2" in "ma1".
5462 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5464 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5465 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5467 int i;
5468 isl_space *space = NULL;
5470 ma2 = isl_multi_aff_align_divs(ma2);
5471 ma1 = isl_multi_aff_cow(ma1);
5472 if (!ma1 || !ma2)
5473 goto error;
5475 space = isl_space_join(isl_multi_aff_get_space(ma2),
5476 isl_multi_aff_get_space(ma1));
5478 for (i = 0; i < ma1->n; ++i) {
5479 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5480 isl_multi_aff_copy(ma2));
5481 if (!ma1->p[i])
5482 goto error;
5485 ma1 = isl_multi_aff_reset_space(ma1, space);
5486 isl_multi_aff_free(ma2);
5487 return ma1;
5488 error:
5489 isl_space_free(space);
5490 isl_multi_aff_free(ma2);
5491 isl_multi_aff_free(ma1);
5492 return NULL;
5495 /* Compute the pullback of "ma1" by the function represented by "ma2".
5496 * In other words, plug in "ma2" in "ma1".
5498 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5499 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5501 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5502 &isl_multi_aff_pullback_multi_aff_aligned);
5505 /* Extend the local space of "dst" to include the divs
5506 * in the local space of "src".
5508 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5509 __isl_keep isl_aff *src)
5511 isl_ctx *ctx;
5512 int *exp1 = NULL;
5513 int *exp2 = NULL;
5514 isl_mat *div;
5516 if (!src || !dst)
5517 return isl_aff_free(dst);
5519 ctx = isl_aff_get_ctx(src);
5520 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5521 isl_die(ctx, isl_error_invalid,
5522 "spaces don't match", goto error);
5524 if (src->ls->div->n_row == 0)
5525 return dst;
5527 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5528 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5529 if (!exp1 || (dst->ls->div->n_row && !exp2))
5530 goto error;
5532 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5533 dst = isl_aff_expand_divs(dst, div, exp2);
5534 free(exp1);
5535 free(exp2);
5537 return dst;
5538 error:
5539 free(exp1);
5540 free(exp2);
5541 return isl_aff_free(dst);
5544 /* Adjust the local spaces of the affine expressions in "maff"
5545 * such that they all have the save divs.
5547 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5548 __isl_take isl_multi_aff *maff)
5550 int i;
5552 if (!maff)
5553 return NULL;
5554 if (maff->n == 0)
5555 return maff;
5556 maff = isl_multi_aff_cow(maff);
5557 if (!maff)
5558 return NULL;
5560 for (i = 1; i < maff->n; ++i)
5561 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5562 for (i = 1; i < maff->n; ++i) {
5563 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5564 if (!maff->p[i])
5565 return isl_multi_aff_free(maff);
5568 return maff;
5571 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5573 aff = isl_aff_cow(aff);
5574 if (!aff)
5575 return NULL;
5577 aff->ls = isl_local_space_lift(aff->ls);
5578 if (!aff->ls)
5579 return isl_aff_free(aff);
5581 return aff;
5584 /* Lift "maff" to a space with extra dimensions such that the result
5585 * has no more existentially quantified variables.
5586 * If "ls" is not NULL, then *ls is assigned the local space that lies
5587 * at the basis of the lifting applied to "maff".
5589 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5590 __isl_give isl_local_space **ls)
5592 int i;
5593 isl_space *space;
5594 unsigned n_div;
5596 if (ls)
5597 *ls = NULL;
5599 if (!maff)
5600 return NULL;
5602 if (maff->n == 0) {
5603 if (ls) {
5604 isl_space *space = isl_multi_aff_get_domain_space(maff);
5605 *ls = isl_local_space_from_space(space);
5606 if (!*ls)
5607 return isl_multi_aff_free(maff);
5609 return maff;
5612 maff = isl_multi_aff_cow(maff);
5613 maff = isl_multi_aff_align_divs(maff);
5614 if (!maff)
5615 return NULL;
5617 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5618 space = isl_multi_aff_get_space(maff);
5619 space = isl_space_lift(isl_space_domain(space), n_div);
5620 space = isl_space_extend_domain_with_range(space,
5621 isl_multi_aff_get_space(maff));
5622 if (!space)
5623 return isl_multi_aff_free(maff);
5624 isl_space_free(maff->space);
5625 maff->space = space;
5627 if (ls) {
5628 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5629 if (!*ls)
5630 return isl_multi_aff_free(maff);
5633 for (i = 0; i < maff->n; ++i) {
5634 maff->p[i] = isl_aff_lift(maff->p[i]);
5635 if (!maff->p[i])
5636 goto error;
5639 return maff;
5640 error:
5641 if (ls)
5642 isl_local_space_free(*ls);
5643 return isl_multi_aff_free(maff);
5647 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5649 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5650 __isl_keep isl_pw_multi_aff *pma, int pos)
5652 int i;
5653 int n_out;
5654 isl_space *space;
5655 isl_pw_aff *pa;
5657 if (!pma)
5658 return NULL;
5660 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5661 if (pos < 0 || pos >= n_out)
5662 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5663 "index out of bounds", return NULL);
5665 space = isl_pw_multi_aff_get_space(pma);
5666 space = isl_space_drop_dims(space, isl_dim_out,
5667 pos + 1, n_out - pos - 1);
5668 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5670 pa = isl_pw_aff_alloc_size(space, pma->n);
5671 for (i = 0; i < pma->n; ++i) {
5672 isl_aff *aff;
5673 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5674 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5677 return pa;
5680 /* Return an isl_pw_multi_aff with the given "set" as domain and
5681 * an unnamed zero-dimensional range.
5683 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5684 __isl_take isl_set *set)
5686 isl_multi_aff *ma;
5687 isl_space *space;
5689 space = isl_set_get_space(set);
5690 space = isl_space_from_domain(space);
5691 ma = isl_multi_aff_zero(space);
5692 return isl_pw_multi_aff_alloc(set, ma);
5695 /* Add an isl_pw_multi_aff with the given "set" as domain and
5696 * an unnamed zero-dimensional range to *user.
5698 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5699 void *user)
5701 isl_union_pw_multi_aff **upma = user;
5702 isl_pw_multi_aff *pma;
5704 pma = isl_pw_multi_aff_from_domain(set);
5705 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5707 return isl_stat_ok;
5710 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5711 * an unnamed zero-dimensional range.
5713 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5714 __isl_take isl_union_set *uset)
5716 isl_space *space;
5717 isl_union_pw_multi_aff *upma;
5719 if (!uset)
5720 return NULL;
5722 space = isl_union_set_get_space(uset);
5723 upma = isl_union_pw_multi_aff_empty(space);
5725 if (isl_union_set_foreach_set(uset,
5726 &add_pw_multi_aff_from_domain, &upma) < 0)
5727 goto error;
5729 isl_union_set_free(uset);
5730 return upma;
5731 error:
5732 isl_union_set_free(uset);
5733 isl_union_pw_multi_aff_free(upma);
5734 return NULL;
5737 /* Convert "pma" to an isl_map and add it to *umap.
5739 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5740 void *user)
5742 isl_union_map **umap = user;
5743 isl_map *map;
5745 map = isl_map_from_pw_multi_aff(pma);
5746 *umap = isl_union_map_add_map(*umap, map);
5748 return isl_stat_ok;
5751 /* Construct a union map mapping the domain of the union
5752 * piecewise multi-affine expression to its range, with each dimension
5753 * in the range equated to the corresponding affine expression on its cell.
5755 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5756 __isl_take isl_union_pw_multi_aff *upma)
5758 isl_space *space;
5759 isl_union_map *umap;
5761 if (!upma)
5762 return NULL;
5764 space = isl_union_pw_multi_aff_get_space(upma);
5765 umap = isl_union_map_empty(space);
5767 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5768 &map_from_pw_multi_aff, &umap) < 0)
5769 goto error;
5771 isl_union_pw_multi_aff_free(upma);
5772 return umap;
5773 error:
5774 isl_union_pw_multi_aff_free(upma);
5775 isl_union_map_free(umap);
5776 return NULL;
5779 /* Local data for bin_entry and the callback "fn".
5781 struct isl_union_pw_multi_aff_bin_data {
5782 isl_union_pw_multi_aff *upma2;
5783 isl_union_pw_multi_aff *res;
5784 isl_pw_multi_aff *pma;
5785 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5788 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5789 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5791 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5793 struct isl_union_pw_multi_aff_bin_data *data = user;
5794 isl_stat r;
5796 data->pma = pma;
5797 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5798 data->fn, data);
5799 isl_pw_multi_aff_free(pma);
5801 return r;
5804 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5805 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5806 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5807 * as *entry. The callback should adjust data->res if desired.
5809 static __isl_give isl_union_pw_multi_aff *bin_op(
5810 __isl_take isl_union_pw_multi_aff *upma1,
5811 __isl_take isl_union_pw_multi_aff *upma2,
5812 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5814 isl_space *space;
5815 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5817 space = isl_union_pw_multi_aff_get_space(upma2);
5818 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5819 space = isl_union_pw_multi_aff_get_space(upma1);
5820 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5822 if (!upma1 || !upma2)
5823 goto error;
5825 data.upma2 = upma2;
5826 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5827 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5828 &bin_entry, &data) < 0)
5829 goto error;
5831 isl_union_pw_multi_aff_free(upma1);
5832 isl_union_pw_multi_aff_free(upma2);
5833 return data.res;
5834 error:
5835 isl_union_pw_multi_aff_free(upma1);
5836 isl_union_pw_multi_aff_free(upma2);
5837 isl_union_pw_multi_aff_free(data.res);
5838 return NULL;
5841 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5842 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5844 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5845 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5847 isl_space *space;
5849 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5850 isl_pw_multi_aff_get_space(pma2));
5851 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5852 &isl_multi_aff_range_product);
5855 /* Given two isl_pw_multi_affs A -> B and C -> D,
5856 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5858 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5859 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5861 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5862 &pw_multi_aff_range_product);
5865 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5866 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5868 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5869 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5871 isl_space *space;
5873 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5874 isl_pw_multi_aff_get_space(pma2));
5875 space = isl_space_flatten_range(space);
5876 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5877 &isl_multi_aff_flat_range_product);
5880 /* Given two isl_pw_multi_affs A -> B and C -> D,
5881 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5883 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5884 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5886 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5887 &pw_multi_aff_flat_range_product);
5890 /* If data->pma and "pma2" have the same domain space, then compute
5891 * their flat range product and the result to data->res.
5893 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
5894 void *user)
5896 struct isl_union_pw_multi_aff_bin_data *data = user;
5898 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5899 pma2->dim, isl_dim_in)) {
5900 isl_pw_multi_aff_free(pma2);
5901 return isl_stat_ok;
5904 pma2 = isl_pw_multi_aff_flat_range_product(
5905 isl_pw_multi_aff_copy(data->pma), pma2);
5907 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5909 return isl_stat_ok;
5912 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5913 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5915 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5916 __isl_take isl_union_pw_multi_aff *upma1,
5917 __isl_take isl_union_pw_multi_aff *upma2)
5919 return bin_op(upma1, upma2, &flat_range_product_entry);
5922 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5923 * The parameters are assumed to have been aligned.
5925 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5926 * except that it works on two different isl_pw_* types.
5928 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5929 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5930 __isl_take isl_pw_aff *pa)
5932 int i, j, n;
5933 isl_pw_multi_aff *res = NULL;
5935 if (!pma || !pa)
5936 goto error;
5938 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5939 pa->dim, isl_dim_in))
5940 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5941 "domains don't match", goto error);
5942 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5943 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5944 "index out of bounds", goto error);
5946 n = pma->n * pa->n;
5947 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5949 for (i = 0; i < pma->n; ++i) {
5950 for (j = 0; j < pa->n; ++j) {
5951 isl_set *common;
5952 isl_multi_aff *res_ij;
5953 int empty;
5955 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5956 isl_set_copy(pa->p[j].set));
5957 empty = isl_set_plain_is_empty(common);
5958 if (empty < 0 || empty) {
5959 isl_set_free(common);
5960 if (empty < 0)
5961 goto error;
5962 continue;
5965 res_ij = isl_multi_aff_set_aff(
5966 isl_multi_aff_copy(pma->p[i].maff), pos,
5967 isl_aff_copy(pa->p[j].aff));
5968 res_ij = isl_multi_aff_gist(res_ij,
5969 isl_set_copy(common));
5971 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5975 isl_pw_multi_aff_free(pma);
5976 isl_pw_aff_free(pa);
5977 return res;
5978 error:
5979 isl_pw_multi_aff_free(pma);
5980 isl_pw_aff_free(pa);
5981 return isl_pw_multi_aff_free(res);
5984 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5986 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
5987 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5988 __isl_take isl_pw_aff *pa)
5990 if (!pma || !pa)
5991 goto error;
5992 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
5993 return pw_multi_aff_set_pw_aff(pma, pos, pa);
5994 if (!isl_space_has_named_params(pma->dim) ||
5995 !isl_space_has_named_params(pa->dim))
5996 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5997 "unaligned unnamed parameters", goto error);
5998 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
5999 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6000 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6001 error:
6002 isl_pw_multi_aff_free(pma);
6003 isl_pw_aff_free(pa);
6004 return NULL;
6007 /* Do the parameters of "pa" match those of "space"?
6009 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6010 __isl_keep isl_space *space)
6012 isl_space *pa_space;
6013 int match;
6015 if (!pa || !space)
6016 return -1;
6018 pa_space = isl_pw_aff_get_space(pa);
6020 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6022 isl_space_free(pa_space);
6023 return match;
6026 /* Check that the domain space of "pa" matches "space".
6028 * Return 0 on success and -1 on error.
6030 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6031 __isl_keep isl_space *space)
6033 isl_space *pa_space;
6034 int match;
6036 if (!pa || !space)
6037 return -1;
6039 pa_space = isl_pw_aff_get_space(pa);
6041 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6042 if (match < 0)
6043 goto error;
6044 if (!match)
6045 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6046 "parameters don't match", goto error);
6047 match = isl_space_tuple_is_equal(space, isl_dim_in,
6048 pa_space, isl_dim_in);
6049 if (match < 0)
6050 goto error;
6051 if (!match)
6052 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6053 "domains don't match", goto error);
6054 isl_space_free(pa_space);
6055 return 0;
6056 error:
6057 isl_space_free(pa_space);
6058 return -1;
6061 #undef BASE
6062 #define BASE pw_aff
6063 #undef DOMBASE
6064 #define DOMBASE set
6066 #include <isl_multi_templ.c>
6067 #include <isl_multi_apply_set.c>
6068 #include <isl_multi_coalesce.c>
6069 #include <isl_multi_gist.c>
6070 #include <isl_multi_hash.c>
6071 #include <isl_multi_intersect.c>
6073 /* Scale the elements of "pma" by the corresponding elements of "mv".
6075 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6076 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6078 int i;
6080 pma = isl_pw_multi_aff_cow(pma);
6081 if (!pma || !mv)
6082 goto error;
6083 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6084 mv->space, isl_dim_set))
6085 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6086 "spaces don't match", goto error);
6087 if (!isl_space_match(pma->dim, isl_dim_param,
6088 mv->space, isl_dim_param)) {
6089 pma = isl_pw_multi_aff_align_params(pma,
6090 isl_multi_val_get_space(mv));
6091 mv = isl_multi_val_align_params(mv,
6092 isl_pw_multi_aff_get_space(pma));
6093 if (!pma || !mv)
6094 goto error;
6097 for (i = 0; i < pma->n; ++i) {
6098 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6099 isl_multi_val_copy(mv));
6100 if (!pma->p[i].maff)
6101 goto error;
6104 isl_multi_val_free(mv);
6105 return pma;
6106 error:
6107 isl_multi_val_free(mv);
6108 isl_pw_multi_aff_free(pma);
6109 return NULL;
6112 /* This function is called for each entry of an isl_union_pw_multi_aff.
6113 * If the space of the entry matches that of data->mv,
6114 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6115 * Otherwise, return an empty isl_pw_multi_aff.
6117 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6118 __isl_take isl_pw_multi_aff *pma, void *user)
6120 isl_multi_val *mv = user;
6122 if (!pma)
6123 return NULL;
6124 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6125 mv->space, isl_dim_set)) {
6126 isl_space *space = isl_pw_multi_aff_get_space(pma);
6127 isl_pw_multi_aff_free(pma);
6128 return isl_pw_multi_aff_empty(space);
6131 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6134 /* Scale the elements of "upma" by the corresponding elements of "mv",
6135 * for those entries that match the space of "mv".
6137 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6138 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6140 upma = isl_union_pw_multi_aff_align_params(upma,
6141 isl_multi_val_get_space(mv));
6142 mv = isl_multi_val_align_params(mv,
6143 isl_union_pw_multi_aff_get_space(upma));
6144 if (!upma || !mv)
6145 goto error;
6147 return isl_union_pw_multi_aff_transform(upma,
6148 &union_pw_multi_aff_scale_multi_val_entry, mv);
6150 isl_multi_val_free(mv);
6151 return upma;
6152 error:
6153 isl_multi_val_free(mv);
6154 isl_union_pw_multi_aff_free(upma);
6155 return NULL;
6158 /* Construct and return a piecewise multi affine expression
6159 * in the given space with value zero in each of the output dimensions and
6160 * a universe domain.
6162 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6164 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6167 /* Construct and return a piecewise multi affine expression
6168 * that is equal to the given piecewise affine expression.
6170 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6171 __isl_take isl_pw_aff *pa)
6173 int i;
6174 isl_space *space;
6175 isl_pw_multi_aff *pma;
6177 if (!pa)
6178 return NULL;
6180 space = isl_pw_aff_get_space(pa);
6181 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6183 for (i = 0; i < pa->n; ++i) {
6184 isl_set *set;
6185 isl_multi_aff *ma;
6187 set = isl_set_copy(pa->p[i].set);
6188 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6189 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6192 isl_pw_aff_free(pa);
6193 return pma;
6196 /* Construct a set or map mapping the shared (parameter) domain
6197 * of the piecewise affine expressions to the range of "mpa"
6198 * with each dimension in the range equated to the
6199 * corresponding piecewise affine expression.
6201 static __isl_give isl_map *map_from_multi_pw_aff(
6202 __isl_take isl_multi_pw_aff *mpa)
6204 int i;
6205 isl_space *space;
6206 isl_map *map;
6208 if (!mpa)
6209 return NULL;
6211 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6212 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6213 "invalid space", goto error);
6215 space = isl_multi_pw_aff_get_domain_space(mpa);
6216 map = isl_map_universe(isl_space_from_domain(space));
6218 for (i = 0; i < mpa->n; ++i) {
6219 isl_pw_aff *pa;
6220 isl_map *map_i;
6222 pa = isl_pw_aff_copy(mpa->p[i]);
6223 map_i = map_from_pw_aff(pa);
6225 map = isl_map_flat_range_product(map, map_i);
6228 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6230 isl_multi_pw_aff_free(mpa);
6231 return map;
6232 error:
6233 isl_multi_pw_aff_free(mpa);
6234 return NULL;
6237 /* Construct a map mapping the shared domain
6238 * of the piecewise affine expressions to the range of "mpa"
6239 * with each dimension in the range equated to the
6240 * corresponding piecewise affine expression.
6242 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6244 if (!mpa)
6245 return NULL;
6246 if (isl_space_is_set(mpa->space))
6247 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6248 "space of input is not a map", goto error);
6250 return map_from_multi_pw_aff(mpa);
6251 error:
6252 isl_multi_pw_aff_free(mpa);
6253 return NULL;
6256 /* Construct a set mapping the shared parameter domain
6257 * of the piecewise affine expressions to the space of "mpa"
6258 * with each dimension in the range equated to the
6259 * corresponding piecewise affine expression.
6261 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6263 if (!mpa)
6264 return NULL;
6265 if (!isl_space_is_set(mpa->space))
6266 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6267 "space of input is not a set", goto error);
6269 return map_from_multi_pw_aff(mpa);
6270 error:
6271 isl_multi_pw_aff_free(mpa);
6272 return NULL;
6275 /* Construct and return a piecewise multi affine expression
6276 * that is equal to the given multi piecewise affine expression
6277 * on the shared domain of the piecewise affine expressions.
6279 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6280 __isl_take isl_multi_pw_aff *mpa)
6282 int i;
6283 isl_space *space;
6284 isl_pw_aff *pa;
6285 isl_pw_multi_aff *pma;
6287 if (!mpa)
6288 return NULL;
6290 space = isl_multi_pw_aff_get_space(mpa);
6292 if (mpa->n == 0) {
6293 isl_multi_pw_aff_free(mpa);
6294 return isl_pw_multi_aff_zero(space);
6297 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6298 pma = isl_pw_multi_aff_from_pw_aff(pa);
6300 for (i = 1; i < mpa->n; ++i) {
6301 isl_pw_multi_aff *pma_i;
6303 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6304 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6305 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6308 pma = isl_pw_multi_aff_reset_space(pma, space);
6310 isl_multi_pw_aff_free(mpa);
6311 return pma;
6314 /* Construct and return a multi piecewise affine expression
6315 * that is equal to the given multi affine expression.
6317 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6318 __isl_take isl_multi_aff *ma)
6320 int i, n;
6321 isl_multi_pw_aff *mpa;
6323 if (!ma)
6324 return NULL;
6326 n = isl_multi_aff_dim(ma, isl_dim_out);
6327 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6329 for (i = 0; i < n; ++i) {
6330 isl_pw_aff *pa;
6332 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6333 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6336 isl_multi_aff_free(ma);
6337 return mpa;
6340 /* Construct and return a multi piecewise affine expression
6341 * that is equal to the given piecewise multi affine expression.
6343 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6344 __isl_take isl_pw_multi_aff *pma)
6346 int i, n;
6347 isl_space *space;
6348 isl_multi_pw_aff *mpa;
6350 if (!pma)
6351 return NULL;
6353 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6354 space = isl_pw_multi_aff_get_space(pma);
6355 mpa = isl_multi_pw_aff_alloc(space);
6357 for (i = 0; i < n; ++i) {
6358 isl_pw_aff *pa;
6360 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6361 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6364 isl_pw_multi_aff_free(pma);
6365 return mpa;
6368 /* Do "pa1" and "pa2" represent the same function?
6370 * We first check if they are obviously equal.
6371 * If not, we convert them to maps and check if those are equal.
6373 * If "pa1" or "pa2" contain any NaNs, then they are considered
6374 * not to be the same. A NaN is not equal to anything, not even
6375 * to another NaN.
6377 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6379 int equal;
6380 isl_bool has_nan;
6381 isl_map *map1, *map2;
6383 if (!pa1 || !pa2)
6384 return -1;
6386 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6387 if (equal < 0 || equal)
6388 return equal;
6389 has_nan = isl_pw_aff_involves_nan(pa1);
6390 if (has_nan >= 0 && !has_nan)
6391 has_nan = isl_pw_aff_involves_nan(pa2);
6392 if (has_nan < 0)
6393 return -1;
6394 if (has_nan)
6395 return 0;
6397 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6398 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6399 equal = isl_map_is_equal(map1, map2);
6400 isl_map_free(map1);
6401 isl_map_free(map2);
6403 return equal;
6406 /* Do "mpa1" and "mpa2" represent the same function?
6408 * Note that we cannot convert the entire isl_multi_pw_aff
6409 * to a map because the domains of the piecewise affine expressions
6410 * may not be the same.
6412 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6413 __isl_keep isl_multi_pw_aff *mpa2)
6415 int i;
6416 isl_bool equal;
6418 if (!mpa1 || !mpa2)
6419 return isl_bool_error;
6421 if (!isl_space_match(mpa1->space, isl_dim_param,
6422 mpa2->space, isl_dim_param)) {
6423 if (!isl_space_has_named_params(mpa1->space))
6424 return isl_bool_false;
6425 if (!isl_space_has_named_params(mpa2->space))
6426 return isl_bool_false;
6427 mpa1 = isl_multi_pw_aff_copy(mpa1);
6428 mpa2 = isl_multi_pw_aff_copy(mpa2);
6429 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6430 isl_multi_pw_aff_get_space(mpa2));
6431 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6432 isl_multi_pw_aff_get_space(mpa1));
6433 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6434 isl_multi_pw_aff_free(mpa1);
6435 isl_multi_pw_aff_free(mpa2);
6436 return equal;
6439 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6440 if (equal < 0 || !equal)
6441 return equal;
6443 for (i = 0; i < mpa1->n; ++i) {
6444 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6445 if (equal < 0 || !equal)
6446 return equal;
6449 return isl_bool_true;
6452 /* Compute the pullback of "mpa" by the function represented by "ma".
6453 * In other words, plug in "ma" in "mpa".
6455 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6457 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6458 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6460 int i;
6461 isl_space *space = NULL;
6463 mpa = isl_multi_pw_aff_cow(mpa);
6464 if (!mpa || !ma)
6465 goto error;
6467 space = isl_space_join(isl_multi_aff_get_space(ma),
6468 isl_multi_pw_aff_get_space(mpa));
6469 if (!space)
6470 goto error;
6472 for (i = 0; i < mpa->n; ++i) {
6473 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6474 isl_multi_aff_copy(ma));
6475 if (!mpa->p[i])
6476 goto error;
6479 isl_multi_aff_free(ma);
6480 isl_space_free(mpa->space);
6481 mpa->space = space;
6482 return mpa;
6483 error:
6484 isl_space_free(space);
6485 isl_multi_pw_aff_free(mpa);
6486 isl_multi_aff_free(ma);
6487 return NULL;
6490 /* Compute the pullback of "mpa" by the function represented by "ma".
6491 * In other words, plug in "ma" in "mpa".
6493 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6494 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6496 if (!mpa || !ma)
6497 goto error;
6498 if (isl_space_match(mpa->space, isl_dim_param,
6499 ma->space, isl_dim_param))
6500 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6501 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6502 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6503 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6504 error:
6505 isl_multi_pw_aff_free(mpa);
6506 isl_multi_aff_free(ma);
6507 return NULL;
6510 /* Compute the pullback of "mpa" by the function represented by "pma".
6511 * In other words, plug in "pma" in "mpa".
6513 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6515 static __isl_give isl_multi_pw_aff *
6516 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6517 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6519 int i;
6520 isl_space *space = NULL;
6522 mpa = isl_multi_pw_aff_cow(mpa);
6523 if (!mpa || !pma)
6524 goto error;
6526 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6527 isl_multi_pw_aff_get_space(mpa));
6529 for (i = 0; i < mpa->n; ++i) {
6530 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6531 isl_pw_multi_aff_copy(pma));
6532 if (!mpa->p[i])
6533 goto error;
6536 isl_pw_multi_aff_free(pma);
6537 isl_space_free(mpa->space);
6538 mpa->space = space;
6539 return mpa;
6540 error:
6541 isl_space_free(space);
6542 isl_multi_pw_aff_free(mpa);
6543 isl_pw_multi_aff_free(pma);
6544 return NULL;
6547 /* Compute the pullback of "mpa" by the function represented by "pma".
6548 * In other words, plug in "pma" in "mpa".
6550 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6551 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6553 if (!mpa || !pma)
6554 goto error;
6555 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6556 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6557 mpa = isl_multi_pw_aff_align_params(mpa,
6558 isl_pw_multi_aff_get_space(pma));
6559 pma = isl_pw_multi_aff_align_params(pma,
6560 isl_multi_pw_aff_get_space(mpa));
6561 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6562 error:
6563 isl_multi_pw_aff_free(mpa);
6564 isl_pw_multi_aff_free(pma);
6565 return NULL;
6568 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6569 * with the domain of "aff". The domain of the result is the same
6570 * as that of "mpa".
6571 * "mpa" and "aff" are assumed to have been aligned.
6573 * We first extract the parametric constant from "aff", defined
6574 * over the correct domain.
6575 * Then we add the appropriate combinations of the members of "mpa".
6576 * Finally, we add the integer divisions through recursive calls.
6578 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6579 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6581 int i, n_in, n_div;
6582 isl_space *space;
6583 isl_val *v;
6584 isl_pw_aff *pa;
6585 isl_aff *tmp;
6587 n_in = isl_aff_dim(aff, isl_dim_in);
6588 n_div = isl_aff_dim(aff, isl_dim_div);
6590 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6591 tmp = isl_aff_copy(aff);
6592 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6593 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6594 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6595 isl_space_dim(space, isl_dim_set));
6596 tmp = isl_aff_reset_domain_space(tmp, space);
6597 pa = isl_pw_aff_from_aff(tmp);
6599 for (i = 0; i < n_in; ++i) {
6600 isl_pw_aff *pa_i;
6602 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6603 continue;
6604 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6605 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6606 pa_i = isl_pw_aff_scale_val(pa_i, v);
6607 pa = isl_pw_aff_add(pa, pa_i);
6610 for (i = 0; i < n_div; ++i) {
6611 isl_aff *div;
6612 isl_pw_aff *pa_i;
6614 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6615 continue;
6616 div = isl_aff_get_div(aff, i);
6617 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6618 isl_multi_pw_aff_copy(mpa), div);
6619 pa_i = isl_pw_aff_floor(pa_i);
6620 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6621 pa_i = isl_pw_aff_scale_val(pa_i, v);
6622 pa = isl_pw_aff_add(pa, pa_i);
6625 isl_multi_pw_aff_free(mpa);
6626 isl_aff_free(aff);
6628 return pa;
6631 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6632 * with the domain of "aff". The domain of the result is the same
6633 * as that of "mpa".
6635 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6636 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6638 if (!aff || !mpa)
6639 goto error;
6640 if (isl_space_match(aff->ls->dim, isl_dim_param,
6641 mpa->space, isl_dim_param))
6642 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6644 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6645 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6647 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6648 error:
6649 isl_aff_free(aff);
6650 isl_multi_pw_aff_free(mpa);
6651 return NULL;
6654 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6655 * with the domain of "pa". The domain of the result is the same
6656 * as that of "mpa".
6657 * "mpa" and "pa" are assumed to have been aligned.
6659 * We consider each piece in turn. Note that the domains of the
6660 * pieces are assumed to be disjoint and they remain disjoint
6661 * after taking the preimage (over the same function).
6663 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6664 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6666 isl_space *space;
6667 isl_pw_aff *res;
6668 int i;
6670 if (!mpa || !pa)
6671 goto error;
6673 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6674 isl_pw_aff_get_space(pa));
6675 res = isl_pw_aff_empty(space);
6677 for (i = 0; i < pa->n; ++i) {
6678 isl_pw_aff *pa_i;
6679 isl_set *domain;
6681 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6682 isl_multi_pw_aff_copy(mpa),
6683 isl_aff_copy(pa->p[i].aff));
6684 domain = isl_set_copy(pa->p[i].set);
6685 domain = isl_set_preimage_multi_pw_aff(domain,
6686 isl_multi_pw_aff_copy(mpa));
6687 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6688 res = isl_pw_aff_add_disjoint(res, pa_i);
6691 isl_pw_aff_free(pa);
6692 isl_multi_pw_aff_free(mpa);
6693 return res;
6694 error:
6695 isl_pw_aff_free(pa);
6696 isl_multi_pw_aff_free(mpa);
6697 return NULL;
6700 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6701 * with the domain of "pa". The domain of the result is the same
6702 * as that of "mpa".
6704 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6705 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6707 if (!pa || !mpa)
6708 goto error;
6709 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6710 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6712 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6713 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6715 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6716 error:
6717 isl_pw_aff_free(pa);
6718 isl_multi_pw_aff_free(mpa);
6719 return NULL;
6722 /* Compute the pullback of "pa" by the function represented by "mpa".
6723 * In other words, plug in "mpa" in "pa".
6724 * "pa" and "mpa" are assumed to have been aligned.
6726 * The pullback is computed by applying "pa" to "mpa".
6728 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6729 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6731 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6734 /* Compute the pullback of "pa" by the function represented by "mpa".
6735 * In other words, plug in "mpa" in "pa".
6737 * The pullback is computed by applying "pa" to "mpa".
6739 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6740 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6742 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6745 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6746 * In other words, plug in "mpa2" in "mpa1".
6748 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6750 * We pullback each member of "mpa1" in turn.
6752 static __isl_give isl_multi_pw_aff *
6753 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6754 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6756 int i;
6757 isl_space *space = NULL;
6759 mpa1 = isl_multi_pw_aff_cow(mpa1);
6760 if (!mpa1 || !mpa2)
6761 goto error;
6763 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6764 isl_multi_pw_aff_get_space(mpa1));
6766 for (i = 0; i < mpa1->n; ++i) {
6767 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6768 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6769 if (!mpa1->p[i])
6770 goto error;
6773 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6775 isl_multi_pw_aff_free(mpa2);
6776 return mpa1;
6777 error:
6778 isl_space_free(space);
6779 isl_multi_pw_aff_free(mpa1);
6780 isl_multi_pw_aff_free(mpa2);
6781 return NULL;
6784 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6785 * In other words, plug in "mpa2" in "mpa1".
6787 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6788 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6790 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6791 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6794 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6795 * of "mpa1" and "mpa2" live in the same space, construct map space
6796 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6797 * with this map space as extract argument.
6799 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6800 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6801 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6802 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6804 int match;
6805 isl_space *space1, *space2;
6806 isl_map *res;
6808 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6809 isl_multi_pw_aff_get_space(mpa2));
6810 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6811 isl_multi_pw_aff_get_space(mpa1));
6812 if (!mpa1 || !mpa2)
6813 goto error;
6814 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6815 mpa2->space, isl_dim_out);
6816 if (match < 0)
6817 goto error;
6818 if (!match)
6819 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6820 "range spaces don't match", goto error);
6821 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6822 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6823 space1 = isl_space_map_from_domain_and_range(space1, space2);
6825 res = order(mpa1, mpa2, space1);
6826 isl_multi_pw_aff_free(mpa1);
6827 isl_multi_pw_aff_free(mpa2);
6828 return res;
6829 error:
6830 isl_multi_pw_aff_free(mpa1);
6831 isl_multi_pw_aff_free(mpa2);
6832 return NULL;
6835 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6836 * where the function values are equal. "space" is the space of the result.
6837 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6839 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6840 * in the sequences are equal.
6842 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6843 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6844 __isl_take isl_space *space)
6846 int i, n;
6847 isl_map *res;
6849 res = isl_map_universe(space);
6851 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6852 for (i = 0; i < n; ++i) {
6853 isl_pw_aff *pa1, *pa2;
6854 isl_map *map;
6856 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6857 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6858 map = isl_pw_aff_eq_map(pa1, pa2);
6859 res = isl_map_intersect(res, map);
6862 return res;
6865 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6866 * where the function values are equal.
6868 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6869 __isl_take isl_multi_pw_aff *mpa2)
6871 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6872 &isl_multi_pw_aff_eq_map_on_space);
6875 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6876 * where the function values of "mpa1" is lexicographically satisfies "base"
6877 * compared to that of "mpa2". "space" is the space of the result.
6878 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6880 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6881 * if its i-th element satisfies "base" when compared to
6882 * the i-th element of "mpa2" while all previous elements are
6883 * pairwise equal.
6885 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6886 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6887 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6888 __isl_take isl_pw_aff *pa2),
6889 __isl_take isl_space *space)
6891 int i, n;
6892 isl_map *res, *rest;
6894 res = isl_map_empty(isl_space_copy(space));
6895 rest = isl_map_universe(space);
6897 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6898 for (i = 0; i < n; ++i) {
6899 isl_pw_aff *pa1, *pa2;
6900 isl_map *map;
6902 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6903 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6904 map = base(pa1, pa2);
6905 map = isl_map_intersect(map, isl_map_copy(rest));
6906 res = isl_map_union(res, map);
6908 if (i == n - 1)
6909 continue;
6911 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6912 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6913 map = isl_pw_aff_eq_map(pa1, pa2);
6914 rest = isl_map_intersect(rest, map);
6917 isl_map_free(rest);
6918 return res;
6921 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6922 * where the function value of "mpa1" is lexicographically less than that
6923 * of "mpa2". "space" is the space of the result.
6924 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6926 * "mpa1" is less than "mpa2" if its i-th element is smaller
6927 * than the i-th element of "mpa2" while all previous elements are
6928 * pairwise equal.
6930 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
6931 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6932 __isl_take isl_space *space)
6934 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6935 &isl_pw_aff_lt_map, space);
6938 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6939 * where the function value of "mpa1" is lexicographically less than that
6940 * of "mpa2".
6942 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
6943 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6945 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6946 &isl_multi_pw_aff_lex_lt_map_on_space);
6949 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6950 * where the function value of "mpa1" is lexicographically greater than that
6951 * of "mpa2". "space" is the space of the result.
6952 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6954 * "mpa1" is greater than "mpa2" if its i-th element is greater
6955 * than the i-th element of "mpa2" while all previous elements are
6956 * pairwise equal.
6958 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
6959 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6960 __isl_take isl_space *space)
6962 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6963 &isl_pw_aff_gt_map, space);
6966 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6967 * where the function value of "mpa1" is lexicographically greater than that
6968 * of "mpa2".
6970 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
6971 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6973 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6974 &isl_multi_pw_aff_lex_gt_map_on_space);
6977 /* Compare two isl_affs.
6979 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
6980 * than "aff2" and 0 if they are equal.
6982 * The order is fairly arbitrary. We do consider expressions that only involve
6983 * earlier dimensions as "smaller".
6985 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
6987 int cmp;
6988 int last1, last2;
6990 if (aff1 == aff2)
6991 return 0;
6993 if (!aff1)
6994 return -1;
6995 if (!aff2)
6996 return 1;
6998 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
6999 if (cmp != 0)
7000 return cmp;
7002 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7003 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7004 if (last1 != last2)
7005 return last1 - last2;
7007 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7010 /* Compare two isl_pw_affs.
7012 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7013 * than "pa2" and 0 if they are equal.
7015 * The order is fairly arbitrary. We do consider expressions that only involve
7016 * earlier dimensions as "smaller".
7018 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7019 __isl_keep isl_pw_aff *pa2)
7021 int i;
7022 int cmp;
7024 if (pa1 == pa2)
7025 return 0;
7027 if (!pa1)
7028 return -1;
7029 if (!pa2)
7030 return 1;
7032 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7033 if (cmp != 0)
7034 return cmp;
7036 if (pa1->n != pa2->n)
7037 return pa1->n - pa2->n;
7039 for (i = 0; i < pa1->n; ++i) {
7040 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7041 if (cmp != 0)
7042 return cmp;
7043 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7044 if (cmp != 0)
7045 return cmp;
7048 return 0;
7051 /* Return a piecewise affine expression that is equal to "v" on "domain".
7053 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7054 __isl_take isl_val *v)
7056 isl_space *space;
7057 isl_local_space *ls;
7058 isl_aff *aff;
7060 space = isl_set_get_space(domain);
7061 ls = isl_local_space_from_space(space);
7062 aff = isl_aff_val_on_domain(ls, v);
7064 return isl_pw_aff_alloc(domain, aff);
7067 /* Return a multi affine expression that is equal to "mv" on domain
7068 * space "space".
7070 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7071 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7073 int i, n;
7074 isl_space *space2;
7075 isl_local_space *ls;
7076 isl_multi_aff *ma;
7078 if (!space || !mv)
7079 goto error;
7081 n = isl_multi_val_dim(mv, isl_dim_set);
7082 space2 = isl_multi_val_get_space(mv);
7083 space2 = isl_space_align_params(space2, isl_space_copy(space));
7084 space = isl_space_align_params(space, isl_space_copy(space2));
7085 space = isl_space_map_from_domain_and_range(space, space2);
7086 ma = isl_multi_aff_alloc(isl_space_copy(space));
7087 ls = isl_local_space_from_space(isl_space_domain(space));
7088 for (i = 0; i < n; ++i) {
7089 isl_val *v;
7090 isl_aff *aff;
7092 v = isl_multi_val_get_val(mv, i);
7093 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7094 ma = isl_multi_aff_set_aff(ma, i, aff);
7096 isl_local_space_free(ls);
7098 isl_multi_val_free(mv);
7099 return ma;
7100 error:
7101 isl_space_free(space);
7102 isl_multi_val_free(mv);
7103 return NULL;
7106 /* Return a piecewise multi-affine expression
7107 * that is equal to "mv" on "domain".
7109 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7110 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7112 isl_space *space;
7113 isl_multi_aff *ma;
7115 space = isl_set_get_space(domain);
7116 ma = isl_multi_aff_multi_val_on_space(space, mv);
7118 return isl_pw_multi_aff_alloc(domain, ma);
7121 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7122 * mv is the value that should be attained on each domain set
7123 * res collects the results
7125 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7126 isl_multi_val *mv;
7127 isl_union_pw_multi_aff *res;
7130 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7131 * and add it to data->res.
7133 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7134 void *user)
7136 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7137 isl_pw_multi_aff *pma;
7138 isl_multi_val *mv;
7140 mv = isl_multi_val_copy(data->mv);
7141 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7142 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7144 return data->res ? isl_stat_ok : isl_stat_error;
7147 /* Return a union piecewise multi-affine expression
7148 * that is equal to "mv" on "domain".
7150 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7151 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7153 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7154 isl_space *space;
7156 space = isl_union_set_get_space(domain);
7157 data.res = isl_union_pw_multi_aff_empty(space);
7158 data.mv = mv;
7159 if (isl_union_set_foreach_set(domain,
7160 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7161 data.res = isl_union_pw_multi_aff_free(data.res);
7162 isl_union_set_free(domain);
7163 isl_multi_val_free(mv);
7164 return data.res;
7167 /* Compute the pullback of data->pma by the function represented by "pma2",
7168 * provided the spaces match, and add the results to data->res.
7170 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7172 struct isl_union_pw_multi_aff_bin_data *data = user;
7174 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7175 pma2->dim, isl_dim_out)) {
7176 isl_pw_multi_aff_free(pma2);
7177 return isl_stat_ok;
7180 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7181 isl_pw_multi_aff_copy(data->pma), pma2);
7183 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7184 if (!data->res)
7185 return isl_stat_error;
7187 return isl_stat_ok;
7190 /* Compute the pullback of "upma1" by the function represented by "upma2".
7192 __isl_give isl_union_pw_multi_aff *
7193 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7194 __isl_take isl_union_pw_multi_aff *upma1,
7195 __isl_take isl_union_pw_multi_aff *upma2)
7197 return bin_op(upma1, upma2, &pullback_entry);
7200 /* Check that the domain space of "upa" matches "space".
7202 * Return 0 on success and -1 on error.
7204 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7205 * can in principle never fail since the space "space" is that
7206 * of the isl_multi_union_pw_aff and is a set space such that
7207 * there is no domain space to match.
7209 * We check the parameters and double-check that "space" is
7210 * indeed that of a set.
7212 static int isl_union_pw_aff_check_match_domain_space(
7213 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7215 isl_space *upa_space;
7216 int match;
7218 if (!upa || !space)
7219 return -1;
7221 match = isl_space_is_set(space);
7222 if (match < 0)
7223 return -1;
7224 if (!match)
7225 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7226 "expecting set space", return -1);
7228 upa_space = isl_union_pw_aff_get_space(upa);
7229 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7230 if (match < 0)
7231 goto error;
7232 if (!match)
7233 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7234 "parameters don't match", goto error);
7236 isl_space_free(upa_space);
7237 return 0;
7238 error:
7239 isl_space_free(upa_space);
7240 return -1;
7243 /* Do the parameters of "upa" match those of "space"?
7245 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7246 __isl_keep isl_space *space)
7248 isl_space *upa_space;
7249 int match;
7251 if (!upa || !space)
7252 return -1;
7254 upa_space = isl_union_pw_aff_get_space(upa);
7256 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7258 isl_space_free(upa_space);
7259 return match;
7262 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7263 * space represents the new parameters.
7264 * res collects the results.
7266 struct isl_union_pw_aff_reset_params_data {
7267 isl_space *space;
7268 isl_union_pw_aff *res;
7271 /* Replace the parameters of "pa" by data->space and
7272 * add the result to data->res.
7274 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7276 struct isl_union_pw_aff_reset_params_data *data = user;
7277 isl_space *space;
7279 space = isl_pw_aff_get_space(pa);
7280 space = isl_space_replace(space, isl_dim_param, data->space);
7281 pa = isl_pw_aff_reset_space(pa, space);
7282 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7284 return data->res ? isl_stat_ok : isl_stat_error;
7287 /* Replace the domain space of "upa" by "space".
7288 * Since a union expression does not have a (single) domain space,
7289 * "space" is necessarily a parameter space.
7291 * Since the order and the names of the parameters determine
7292 * the hash value, we need to create a new hash table.
7294 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7295 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7297 struct isl_union_pw_aff_reset_params_data data = { space };
7298 int match;
7300 match = isl_union_pw_aff_matching_params(upa, space);
7301 if (match < 0)
7302 upa = isl_union_pw_aff_free(upa);
7303 else if (match) {
7304 isl_space_free(space);
7305 return upa;
7308 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7309 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7310 data.res = isl_union_pw_aff_free(data.res);
7312 isl_union_pw_aff_free(upa);
7313 isl_space_free(space);
7314 return data.res;
7317 /* Return the floor of "pa".
7319 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7321 return isl_pw_aff_floor(pa);
7324 /* Given f, return floor(f).
7326 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7327 __isl_take isl_union_pw_aff *upa)
7329 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7332 /* Compute
7334 * upa mod m = upa - m * floor(upa/m)
7336 * with m an integer value.
7338 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7339 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7341 isl_union_pw_aff *res;
7343 if (!upa || !m)
7344 goto error;
7346 if (!isl_val_is_int(m))
7347 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7348 "expecting integer modulo", goto error);
7349 if (!isl_val_is_pos(m))
7350 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7351 "expecting positive modulo", goto error);
7353 res = isl_union_pw_aff_copy(upa);
7354 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7355 upa = isl_union_pw_aff_floor(upa);
7356 upa = isl_union_pw_aff_scale_val(upa, m);
7357 res = isl_union_pw_aff_sub(res, upa);
7359 return res;
7360 error:
7361 isl_val_free(m);
7362 isl_union_pw_aff_free(upa);
7363 return NULL;
7366 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7367 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7368 * needs to attain.
7369 * "res" collects the results.
7371 struct isl_union_pw_aff_aff_on_domain_data {
7372 isl_aff *aff;
7373 isl_union_pw_aff *res;
7376 /* Construct a piecewise affine expression that is equal to data->aff
7377 * on "domain" and add the result to data->res.
7379 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7381 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7382 isl_pw_aff *pa;
7383 isl_aff *aff;
7384 int dim;
7386 aff = isl_aff_copy(data->aff);
7387 dim = isl_set_dim(domain, isl_dim_set);
7388 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7389 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7390 pa = isl_pw_aff_alloc(domain, aff);
7391 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7393 return data->res ? isl_stat_ok : isl_stat_error;
7396 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7397 * pos is the output position that needs to be extracted.
7398 * res collects the results.
7400 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7401 int pos;
7402 isl_union_pw_aff *res;
7405 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7406 * (assuming it has such a dimension) and add it to data->res.
7408 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7410 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7411 int n_out;
7412 isl_pw_aff *pa;
7414 if (!pma)
7415 return isl_stat_error;
7417 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7418 if (data->pos >= n_out) {
7419 isl_pw_multi_aff_free(pma);
7420 return isl_stat_ok;
7423 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7424 isl_pw_multi_aff_free(pma);
7426 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7428 return data->res ? isl_stat_ok : isl_stat_error;
7431 /* Extract an isl_union_pw_aff corresponding to
7432 * output dimension "pos" of "upma".
7434 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7435 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7437 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7438 isl_space *space;
7440 if (!upma)
7441 return NULL;
7443 if (pos < 0)
7444 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7445 "cannot extract at negative position", return NULL);
7447 space = isl_union_pw_multi_aff_get_space(upma);
7448 data.res = isl_union_pw_aff_empty(space);
7449 data.pos = pos;
7450 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7451 &get_union_pw_aff, &data) < 0)
7452 data.res = isl_union_pw_aff_free(data.res);
7454 return data.res;
7457 /* Return a union piecewise affine expression
7458 * that is equal to "aff" on "domain".
7460 * Construct an isl_pw_aff on each of the sets in "domain" and
7461 * collect the results.
7463 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7464 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7466 struct isl_union_pw_aff_aff_on_domain_data data;
7467 isl_space *space;
7469 if (!domain || !aff)
7470 goto error;
7471 if (!isl_local_space_is_params(aff->ls))
7472 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7473 "expecting parametric expression", goto error);
7475 space = isl_union_set_get_space(domain);
7476 data.res = isl_union_pw_aff_empty(space);
7477 data.aff = aff;
7478 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7479 data.res = isl_union_pw_aff_free(data.res);
7480 isl_union_set_free(domain);
7481 isl_aff_free(aff);
7482 return data.res;
7483 error:
7484 isl_union_set_free(domain);
7485 isl_aff_free(aff);
7486 return NULL;
7489 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7490 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7491 * "res" collects the results.
7493 struct isl_union_pw_aff_val_on_domain_data {
7494 isl_val *v;
7495 isl_union_pw_aff *res;
7498 /* Construct a piecewise affine expression that is equal to data->v
7499 * on "domain" and add the result to data->res.
7501 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7503 struct isl_union_pw_aff_val_on_domain_data *data = user;
7504 isl_pw_aff *pa;
7505 isl_val *v;
7507 v = isl_val_copy(data->v);
7508 pa = isl_pw_aff_val_on_domain(domain, v);
7509 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7511 return data->res ? isl_stat_ok : isl_stat_error;
7514 /* Return a union piecewise affine expression
7515 * that is equal to "v" on "domain".
7517 * Construct an isl_pw_aff on each of the sets in "domain" and
7518 * collect the results.
7520 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7521 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7523 struct isl_union_pw_aff_val_on_domain_data data;
7524 isl_space *space;
7526 space = isl_union_set_get_space(domain);
7527 data.res = isl_union_pw_aff_empty(space);
7528 data.v = v;
7529 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7530 data.res = isl_union_pw_aff_free(data.res);
7531 isl_union_set_free(domain);
7532 isl_val_free(v);
7533 return data.res;
7536 /* Construct a piecewise multi affine expression
7537 * that is equal to "pa" and add it to upma.
7539 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7540 void *user)
7542 isl_union_pw_multi_aff **upma = user;
7543 isl_pw_multi_aff *pma;
7545 pma = isl_pw_multi_aff_from_pw_aff(pa);
7546 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7548 return *upma ? isl_stat_ok : isl_stat_error;
7551 /* Construct and return a union piecewise multi affine expression
7552 * that is equal to the given union piecewise affine expression.
7554 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7555 __isl_take isl_union_pw_aff *upa)
7557 isl_space *space;
7558 isl_union_pw_multi_aff *upma;
7560 if (!upa)
7561 return NULL;
7563 space = isl_union_pw_aff_get_space(upa);
7564 upma = isl_union_pw_multi_aff_empty(space);
7566 if (isl_union_pw_aff_foreach_pw_aff(upa,
7567 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7568 upma = isl_union_pw_multi_aff_free(upma);
7570 isl_union_pw_aff_free(upa);
7571 return upma;
7574 /* Compute the set of elements in the domain of "pa" where it is zero and
7575 * add this set to "uset".
7577 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7579 isl_union_set **uset = (isl_union_set **)user;
7581 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7583 return *uset ? isl_stat_ok : isl_stat_error;
7586 /* Return a union set containing those elements in the domain
7587 * of "upa" where it is zero.
7589 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7590 __isl_take isl_union_pw_aff *upa)
7592 isl_union_set *zero;
7594 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7595 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7596 zero = isl_union_set_free(zero);
7598 isl_union_pw_aff_free(upa);
7599 return zero;
7602 /* Convert "pa" to an isl_map and add it to *umap.
7604 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7606 isl_union_map **umap = user;
7607 isl_map *map;
7609 map = isl_map_from_pw_aff(pa);
7610 *umap = isl_union_map_add_map(*umap, map);
7612 return *umap ? isl_stat_ok : isl_stat_error;
7615 /* Construct a union map mapping the domain of the union
7616 * piecewise affine expression to its range, with the single output dimension
7617 * equated to the corresponding affine expressions on their cells.
7619 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7620 __isl_take isl_union_pw_aff *upa)
7622 isl_space *space;
7623 isl_union_map *umap;
7625 if (!upa)
7626 return NULL;
7628 space = isl_union_pw_aff_get_space(upa);
7629 umap = isl_union_map_empty(space);
7631 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7632 &umap) < 0)
7633 umap = isl_union_map_free(umap);
7635 isl_union_pw_aff_free(upa);
7636 return umap;
7639 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7640 * upma is the function that is plugged in.
7641 * pa is the current part of the function in which upma is plugged in.
7642 * res collects the results.
7644 struct isl_union_pw_aff_pullback_upma_data {
7645 isl_union_pw_multi_aff *upma;
7646 isl_pw_aff *pa;
7647 isl_union_pw_aff *res;
7650 /* Check if "pma" can be plugged into data->pa.
7651 * If so, perform the pullback and add the result to data->res.
7653 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7655 struct isl_union_pw_aff_pullback_upma_data *data = user;
7656 isl_pw_aff *pa;
7658 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7659 pma->dim, isl_dim_out)) {
7660 isl_pw_multi_aff_free(pma);
7661 return isl_stat_ok;
7664 pa = isl_pw_aff_copy(data->pa);
7665 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7667 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7669 return data->res ? isl_stat_ok : isl_stat_error;
7672 /* Check if any of the elements of data->upma can be plugged into pa,
7673 * add if so add the result to data->res.
7675 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7677 struct isl_union_pw_aff_pullback_upma_data *data = user;
7678 isl_stat r;
7680 data->pa = pa;
7681 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7682 &pa_pb_pma, data);
7683 isl_pw_aff_free(pa);
7685 return r;
7688 /* Compute the pullback of "upa" by the function represented by "upma".
7689 * In other words, plug in "upma" in "upa". The result contains
7690 * expressions defined over the domain space of "upma".
7692 * Run over all pairs of elements in "upa" and "upma", perform
7693 * the pullback when appropriate and collect the results.
7694 * If the hash value were based on the domain space rather than
7695 * the function space, then we could run through all elements
7696 * of "upma" and directly pick out the corresponding element of "upa".
7698 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7699 __isl_take isl_union_pw_aff *upa,
7700 __isl_take isl_union_pw_multi_aff *upma)
7702 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7703 isl_space *space;
7705 space = isl_union_pw_multi_aff_get_space(upma);
7706 upa = isl_union_pw_aff_align_params(upa, space);
7707 space = isl_union_pw_aff_get_space(upa);
7708 upma = isl_union_pw_multi_aff_align_params(upma, space);
7710 if (!upa || !upma)
7711 goto error;
7713 data.upma = upma;
7714 data.res = isl_union_pw_aff_alloc_same_size(upa);
7715 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7716 data.res = isl_union_pw_aff_free(data.res);
7718 isl_union_pw_aff_free(upa);
7719 isl_union_pw_multi_aff_free(upma);
7720 return data.res;
7721 error:
7722 isl_union_pw_aff_free(upa);
7723 isl_union_pw_multi_aff_free(upma);
7724 return NULL;
7727 #undef BASE
7728 #define BASE union_pw_aff
7729 #undef DOMBASE
7730 #define DOMBASE union_set
7732 #define NO_MOVE_DIMS
7733 #define NO_DIMS
7734 #define NO_DOMAIN
7735 #define NO_PRODUCT
7736 #define NO_SPLICE
7737 #define NO_ZERO
7738 #define NO_IDENTITY
7739 #define NO_GIST
7741 #include <isl_multi_templ.c>
7742 #include <isl_multi_apply_set.c>
7743 #include <isl_multi_apply_union_set.c>
7744 #include <isl_multi_coalesce.c>
7745 #include <isl_multi_floor.c>
7746 #include <isl_multi_gist.c>
7747 #include <isl_multi_intersect.c>
7749 /* Construct a multiple union piecewise affine expression
7750 * in the given space with value zero in each of the output dimensions.
7752 * Since there is no canonical zero value for
7753 * a union piecewise affine expression, we can only construct
7754 * zero-dimensional "zero" value.
7756 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7757 __isl_take isl_space *space)
7759 if (!space)
7760 return NULL;
7762 if (!isl_space_is_set(space))
7763 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7764 "expecting set space", goto error);
7765 if (isl_space_dim(space , isl_dim_out) != 0)
7766 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7767 "expecting 0D space", goto error);
7769 return isl_multi_union_pw_aff_alloc(space);
7770 error:
7771 isl_space_free(space);
7772 return NULL;
7775 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7776 * with the actual sum on the shared domain and
7777 * the defined expression on the symmetric difference of the domains.
7779 * We simply iterate over the elements in both arguments and
7780 * call isl_union_pw_aff_union_add on each of them.
7782 static __isl_give isl_multi_union_pw_aff *
7783 isl_multi_union_pw_aff_union_add_aligned(
7784 __isl_take isl_multi_union_pw_aff *mupa1,
7785 __isl_take isl_multi_union_pw_aff *mupa2)
7787 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7788 &isl_union_pw_aff_union_add);
7791 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7792 * with the actual sum on the shared domain and
7793 * the defined expression on the symmetric difference of the domains.
7795 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7796 __isl_take isl_multi_union_pw_aff *mupa1,
7797 __isl_take isl_multi_union_pw_aff *mupa2)
7799 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7800 &isl_multi_union_pw_aff_union_add_aligned);
7803 /* Construct and return a multi union piecewise affine expression
7804 * that is equal to the given multi affine expression.
7806 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7807 __isl_take isl_multi_aff *ma)
7809 isl_multi_pw_aff *mpa;
7811 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7812 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7815 /* Construct and return a multi union piecewise affine expression
7816 * that is equal to the given multi piecewise affine expression.
7818 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7819 __isl_take isl_multi_pw_aff *mpa)
7821 int i, n;
7822 isl_space *space;
7823 isl_multi_union_pw_aff *mupa;
7825 if (!mpa)
7826 return NULL;
7828 space = isl_multi_pw_aff_get_space(mpa);
7829 space = isl_space_range(space);
7830 mupa = isl_multi_union_pw_aff_alloc(space);
7832 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7833 for (i = 0; i < n; ++i) {
7834 isl_pw_aff *pa;
7835 isl_union_pw_aff *upa;
7837 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7838 upa = isl_union_pw_aff_from_pw_aff(pa);
7839 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7842 isl_multi_pw_aff_free(mpa);
7844 return mupa;
7847 /* Extract the range space of "pma" and assign it to *space.
7848 * If *space has already been set (through a previous call to this function),
7849 * then check that the range space is the same.
7851 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7853 isl_space **space = user;
7854 isl_space *pma_space;
7855 isl_bool equal;
7857 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7858 isl_pw_multi_aff_free(pma);
7860 if (!pma_space)
7861 return isl_stat_error;
7862 if (!*space) {
7863 *space = pma_space;
7864 return isl_stat_ok;
7867 equal = isl_space_is_equal(pma_space, *space);
7868 isl_space_free(pma_space);
7870 if (equal < 0)
7871 return isl_stat_error;
7872 if (!equal)
7873 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7874 "range spaces not the same", return isl_stat_error);
7875 return isl_stat_ok;
7878 /* Construct and return a multi union piecewise affine expression
7879 * that is equal to the given union piecewise multi affine expression.
7881 * In order to be able to perform the conversion, the input
7882 * needs to be non-empty and may only involve a single range space.
7884 __isl_give isl_multi_union_pw_aff *
7885 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7886 __isl_take isl_union_pw_multi_aff *upma)
7888 isl_space *space = NULL;
7889 isl_multi_union_pw_aff *mupa;
7890 int i, n;
7892 if (!upma)
7893 return NULL;
7894 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
7895 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7896 "cannot extract range space from empty input",
7897 goto error);
7898 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
7899 &space) < 0)
7900 goto error;
7902 if (!space)
7903 goto error;
7905 n = isl_space_dim(space, isl_dim_set);
7906 mupa = isl_multi_union_pw_aff_alloc(space);
7908 for (i = 0; i < n; ++i) {
7909 isl_union_pw_aff *upa;
7911 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
7912 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7915 isl_union_pw_multi_aff_free(upma);
7916 return mupa;
7917 error:
7918 isl_space_free(space);
7919 isl_union_pw_multi_aff_free(upma);
7920 return NULL;
7923 /* Try and create an isl_multi_union_pw_aff that is equivalent
7924 * to the given isl_union_map.
7925 * The isl_union_map is required to be single-valued in each space.
7926 * Moreover, it cannot be empty and all range spaces need to be the same.
7927 * Otherwise, an error is produced.
7929 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
7930 __isl_take isl_union_map *umap)
7932 isl_union_pw_multi_aff *upma;
7934 upma = isl_union_pw_multi_aff_from_union_map(umap);
7935 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
7938 /* Return a multiple union piecewise affine expression
7939 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7940 * have been aligned.
7942 static __isl_give isl_multi_union_pw_aff *
7943 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7944 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7946 int i, n;
7947 isl_space *space;
7948 isl_multi_union_pw_aff *mupa;
7950 if (!domain || !mv)
7951 goto error;
7953 n = isl_multi_val_dim(mv, isl_dim_set);
7954 space = isl_multi_val_get_space(mv);
7955 mupa = isl_multi_union_pw_aff_alloc(space);
7956 for (i = 0; i < n; ++i) {
7957 isl_val *v;
7958 isl_union_pw_aff *upa;
7960 v = isl_multi_val_get_val(mv, i);
7961 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
7963 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7966 isl_union_set_free(domain);
7967 isl_multi_val_free(mv);
7968 return mupa;
7969 error:
7970 isl_union_set_free(domain);
7971 isl_multi_val_free(mv);
7972 return NULL;
7975 /* Return a multiple union piecewise affine expression
7976 * that is equal to "mv" on "domain".
7978 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
7979 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7981 if (!domain || !mv)
7982 goto error;
7983 if (isl_space_match(domain->dim, isl_dim_param,
7984 mv->space, isl_dim_param))
7985 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7986 domain, mv);
7987 domain = isl_union_set_align_params(domain,
7988 isl_multi_val_get_space(mv));
7989 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
7990 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
7991 error:
7992 isl_union_set_free(domain);
7993 isl_multi_val_free(mv);
7994 return NULL;
7997 /* Return a multiple union piecewise affine expression
7998 * that is equal to "ma" on "domain", assuming "domain" and "ma"
7999 * have been aligned.
8001 static __isl_give isl_multi_union_pw_aff *
8002 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8003 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8005 int i, n;
8006 isl_space *space;
8007 isl_multi_union_pw_aff *mupa;
8009 if (!domain || !ma)
8010 goto error;
8012 n = isl_multi_aff_dim(ma, isl_dim_set);
8013 space = isl_multi_aff_get_space(ma);
8014 mupa = isl_multi_union_pw_aff_alloc(space);
8015 for (i = 0; i < n; ++i) {
8016 isl_aff *aff;
8017 isl_union_pw_aff *upa;
8019 aff = isl_multi_aff_get_aff(ma, i);
8020 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8021 aff);
8022 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8025 isl_union_set_free(domain);
8026 isl_multi_aff_free(ma);
8027 return mupa;
8028 error:
8029 isl_union_set_free(domain);
8030 isl_multi_aff_free(ma);
8031 return NULL;
8034 /* Return a multiple union piecewise affine expression
8035 * that is equal to "ma" on "domain".
8037 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8038 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8040 if (!domain || !ma)
8041 goto error;
8042 if (isl_space_match(domain->dim, isl_dim_param,
8043 ma->space, isl_dim_param))
8044 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8045 domain, ma);
8046 domain = isl_union_set_align_params(domain,
8047 isl_multi_aff_get_space(ma));
8048 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8049 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8050 error:
8051 isl_union_set_free(domain);
8052 isl_multi_aff_free(ma);
8053 return NULL;
8056 /* Return a union set containing those elements in the domains
8057 * of the elements of "mupa" where they are all zero.
8059 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8060 __isl_take isl_multi_union_pw_aff *mupa)
8062 int i, n;
8063 isl_union_pw_aff *upa;
8064 isl_union_set *zero;
8066 if (!mupa)
8067 return NULL;
8069 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8070 if (n == 0)
8071 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8072 "cannot determine zero set "
8073 "of zero-dimensional function", goto error);
8075 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8076 zero = isl_union_pw_aff_zero_union_set(upa);
8078 for (i = 1; i < n; ++i) {
8079 isl_union_set *zero_i;
8081 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8082 zero_i = isl_union_pw_aff_zero_union_set(upa);
8084 zero = isl_union_set_intersect(zero, zero_i);
8087 isl_multi_union_pw_aff_free(mupa);
8088 return zero;
8089 error:
8090 isl_multi_union_pw_aff_free(mupa);
8091 return NULL;
8094 /* Construct a union map mapping the shared domain
8095 * of the union piecewise affine expressions to the range of "mupa"
8096 * with each dimension in the range equated to the
8097 * corresponding union piecewise affine expression.
8099 * The input cannot be zero-dimensional as there is
8100 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8102 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8103 __isl_take isl_multi_union_pw_aff *mupa)
8105 int i, n;
8106 isl_space *space;
8107 isl_union_map *umap;
8108 isl_union_pw_aff *upa;
8110 if (!mupa)
8111 return NULL;
8113 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8114 if (n == 0)
8115 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8116 "cannot determine domain of zero-dimensional "
8117 "isl_multi_union_pw_aff", goto error);
8119 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8120 umap = isl_union_map_from_union_pw_aff(upa);
8122 for (i = 1; i < n; ++i) {
8123 isl_union_map *umap_i;
8125 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8126 umap_i = isl_union_map_from_union_pw_aff(upa);
8127 umap = isl_union_map_flat_range_product(umap, umap_i);
8130 space = isl_multi_union_pw_aff_get_space(mupa);
8131 umap = isl_union_map_reset_range_space(umap, space);
8133 isl_multi_union_pw_aff_free(mupa);
8134 return umap;
8135 error:
8136 isl_multi_union_pw_aff_free(mupa);
8137 return NULL;
8140 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8141 * "range" is the space from which to set the range space.
8142 * "res" collects the results.
8144 struct isl_union_pw_multi_aff_reset_range_space_data {
8145 isl_space *range;
8146 isl_union_pw_multi_aff *res;
8149 /* Replace the range space of "pma" by the range space of data->range and
8150 * add the result to data->res.
8152 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8154 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8155 isl_space *space;
8157 space = isl_pw_multi_aff_get_space(pma);
8158 space = isl_space_domain(space);
8159 space = isl_space_extend_domain_with_range(space,
8160 isl_space_copy(data->range));
8161 pma = isl_pw_multi_aff_reset_space(pma, space);
8162 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8164 return data->res ? isl_stat_ok : isl_stat_error;
8167 /* Replace the range space of all the piecewise affine expressions in "upma" by
8168 * the range space of "space".
8170 * This assumes that all these expressions have the same output dimension.
8172 * Since the spaces of the expressions change, so do their hash values.
8173 * We therefore need to create a new isl_union_pw_multi_aff.
8174 * Note that the hash value is currently computed based on the entire
8175 * space even though there can only be a single expression with a given
8176 * domain space.
8178 static __isl_give isl_union_pw_multi_aff *
8179 isl_union_pw_multi_aff_reset_range_space(
8180 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8182 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8183 isl_space *space_upma;
8185 space_upma = isl_union_pw_multi_aff_get_space(upma);
8186 data.res = isl_union_pw_multi_aff_empty(space_upma);
8187 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8188 &reset_range_space, &data) < 0)
8189 data.res = isl_union_pw_multi_aff_free(data.res);
8191 isl_space_free(space);
8192 isl_union_pw_multi_aff_free(upma);
8193 return data.res;
8196 /* Construct and return a union piecewise multi affine expression
8197 * that is equal to the given multi union piecewise affine expression.
8199 * In order to be able to perform the conversion, the input
8200 * needs to have a least one output dimension.
8202 __isl_give isl_union_pw_multi_aff *
8203 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8204 __isl_take isl_multi_union_pw_aff *mupa)
8206 int i, n;
8207 isl_space *space;
8208 isl_union_pw_multi_aff *upma;
8209 isl_union_pw_aff *upa;
8211 if (!mupa)
8212 return NULL;
8214 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8215 if (n == 0)
8216 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8217 "cannot determine domain of zero-dimensional "
8218 "isl_multi_union_pw_aff", goto error);
8220 space = isl_multi_union_pw_aff_get_space(mupa);
8221 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8222 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8224 for (i = 1; i < n; ++i) {
8225 isl_union_pw_multi_aff *upma_i;
8227 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8228 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8229 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8232 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8234 isl_multi_union_pw_aff_free(mupa);
8235 return upma;
8236 error:
8237 isl_multi_union_pw_aff_free(mupa);
8238 return NULL;
8241 /* Intersect the range of "mupa" with "range".
8242 * That is, keep only those domain elements that have a function value
8243 * in "range".
8245 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8246 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8248 isl_union_pw_multi_aff *upma;
8249 isl_union_set *domain;
8250 isl_space *space;
8251 int n;
8252 int match;
8254 if (!mupa || !range)
8255 goto error;
8257 space = isl_set_get_space(range);
8258 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8259 space, isl_dim_set);
8260 isl_space_free(space);
8261 if (match < 0)
8262 goto error;
8263 if (!match)
8264 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8265 "space don't match", goto error);
8266 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8267 if (n == 0)
8268 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8269 "cannot intersect range of zero-dimensional "
8270 "isl_multi_union_pw_aff", goto error);
8272 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8273 isl_multi_union_pw_aff_copy(mupa));
8274 domain = isl_union_set_from_set(range);
8275 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8276 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8278 return mupa;
8279 error:
8280 isl_multi_union_pw_aff_free(mupa);
8281 isl_set_free(range);
8282 return NULL;
8285 /* Return the shared domain of the elements of "mupa".
8287 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8288 __isl_take isl_multi_union_pw_aff *mupa)
8290 int i, n;
8291 isl_union_pw_aff *upa;
8292 isl_union_set *dom;
8294 if (!mupa)
8295 return NULL;
8297 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8298 if (n == 0)
8299 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8300 "cannot determine domain", goto error);
8302 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8303 dom = isl_union_pw_aff_domain(upa);
8304 for (i = 1; i < n; ++i) {
8305 isl_union_set *dom_i;
8307 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8308 dom_i = isl_union_pw_aff_domain(upa);
8309 dom = isl_union_set_intersect(dom, dom_i);
8312 isl_multi_union_pw_aff_free(mupa);
8313 return dom;
8314 error:
8315 isl_multi_union_pw_aff_free(mupa);
8316 return NULL;
8319 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8320 * In particular, the spaces have been aligned.
8321 * The result is defined over the shared domain of the elements of "mupa"
8323 * We first extract the parametric constant part of "aff" and
8324 * define that over the shared domain.
8325 * Then we iterate over all input dimensions of "aff" and add the corresponding
8326 * multiples of the elements of "mupa".
8327 * Finally, we consider the integer divisions, calling the function
8328 * recursively to obtain an isl_union_pw_aff corresponding to the
8329 * integer division argument.
8331 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8332 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8334 int i, n_in, n_div;
8335 isl_union_pw_aff *upa;
8336 isl_union_set *uset;
8337 isl_val *v;
8338 isl_aff *cst;
8340 n_in = isl_aff_dim(aff, isl_dim_in);
8341 n_div = isl_aff_dim(aff, isl_dim_div);
8343 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8344 cst = isl_aff_copy(aff);
8345 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8346 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8347 cst = isl_aff_project_domain_on_params(cst);
8348 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8350 for (i = 0; i < n_in; ++i) {
8351 isl_union_pw_aff *upa_i;
8353 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8354 continue;
8355 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8356 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8357 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8358 upa = isl_union_pw_aff_add(upa, upa_i);
8361 for (i = 0; i < n_div; ++i) {
8362 isl_aff *div;
8363 isl_union_pw_aff *upa_i;
8365 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8366 continue;
8367 div = isl_aff_get_div(aff, i);
8368 upa_i = multi_union_pw_aff_apply_aff(
8369 isl_multi_union_pw_aff_copy(mupa), div);
8370 upa_i = isl_union_pw_aff_floor(upa_i);
8371 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8372 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8373 upa = isl_union_pw_aff_add(upa, upa_i);
8376 isl_multi_union_pw_aff_free(mupa);
8377 isl_aff_free(aff);
8379 return upa;
8382 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8383 * with the domain of "aff".
8384 * Furthermore, the dimension of this space needs to be greater than zero.
8385 * The result is defined over the shared domain of the elements of "mupa"
8387 * We perform these checks and then hand over control to
8388 * multi_union_pw_aff_apply_aff.
8390 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8391 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8393 isl_space *space1, *space2;
8394 int equal;
8396 mupa = isl_multi_union_pw_aff_align_params(mupa,
8397 isl_aff_get_space(aff));
8398 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8399 if (!mupa || !aff)
8400 goto error;
8402 space1 = isl_multi_union_pw_aff_get_space(mupa);
8403 space2 = isl_aff_get_domain_space(aff);
8404 equal = isl_space_is_equal(space1, space2);
8405 isl_space_free(space1);
8406 isl_space_free(space2);
8407 if (equal < 0)
8408 goto error;
8409 if (!equal)
8410 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8411 "spaces don't match", goto error);
8412 if (isl_aff_dim(aff, isl_dim_in) == 0)
8413 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8414 "cannot determine domains", goto error);
8416 return multi_union_pw_aff_apply_aff(mupa, aff);
8417 error:
8418 isl_multi_union_pw_aff_free(mupa);
8419 isl_aff_free(aff);
8420 return NULL;
8423 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8424 * with the domain of "ma".
8425 * Furthermore, the dimension of this space needs to be greater than zero,
8426 * unless the dimension of the target space of "ma" is also zero.
8427 * The result is defined over the shared domain of the elements of "mupa"
8429 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8430 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8432 isl_space *space1, *space2;
8433 isl_multi_union_pw_aff *res;
8434 int equal;
8435 int i, n_out;
8437 mupa = isl_multi_union_pw_aff_align_params(mupa,
8438 isl_multi_aff_get_space(ma));
8439 ma = isl_multi_aff_align_params(ma,
8440 isl_multi_union_pw_aff_get_space(mupa));
8441 if (!mupa || !ma)
8442 goto error;
8444 space1 = isl_multi_union_pw_aff_get_space(mupa);
8445 space2 = isl_multi_aff_get_domain_space(ma);
8446 equal = isl_space_is_equal(space1, space2);
8447 isl_space_free(space1);
8448 isl_space_free(space2);
8449 if (equal < 0)
8450 goto error;
8451 if (!equal)
8452 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8453 "spaces don't match", goto error);
8454 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8455 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8456 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8457 "cannot determine domains", goto error);
8459 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8460 res = isl_multi_union_pw_aff_alloc(space1);
8462 for (i = 0; i < n_out; ++i) {
8463 isl_aff *aff;
8464 isl_union_pw_aff *upa;
8466 aff = isl_multi_aff_get_aff(ma, i);
8467 upa = multi_union_pw_aff_apply_aff(
8468 isl_multi_union_pw_aff_copy(mupa), aff);
8469 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8472 isl_multi_aff_free(ma);
8473 isl_multi_union_pw_aff_free(mupa);
8474 return res;
8475 error:
8476 isl_multi_union_pw_aff_free(mupa);
8477 isl_multi_aff_free(ma);
8478 return NULL;
8481 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8482 * with the domain of "pa".
8483 * Furthermore, the dimension of this space needs to be greater than zero.
8484 * The result is defined over the shared domain of the elements of "mupa"
8486 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8487 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8489 int i;
8490 int equal;
8491 isl_space *space, *space2;
8492 isl_union_pw_aff *upa;
8494 mupa = isl_multi_union_pw_aff_align_params(mupa,
8495 isl_pw_aff_get_space(pa));
8496 pa = isl_pw_aff_align_params(pa,
8497 isl_multi_union_pw_aff_get_space(mupa));
8498 if (!mupa || !pa)
8499 goto error;
8501 space = isl_multi_union_pw_aff_get_space(mupa);
8502 space2 = isl_pw_aff_get_domain_space(pa);
8503 equal = isl_space_is_equal(space, space2);
8504 isl_space_free(space);
8505 isl_space_free(space2);
8506 if (equal < 0)
8507 goto error;
8508 if (!equal)
8509 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8510 "spaces don't match", goto error);
8511 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8512 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8513 "cannot determine domains", goto error);
8515 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8516 upa = isl_union_pw_aff_empty(space);
8518 for (i = 0; i < pa->n; ++i) {
8519 isl_aff *aff;
8520 isl_set *domain;
8521 isl_multi_union_pw_aff *mupa_i;
8522 isl_union_pw_aff *upa_i;
8524 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8525 domain = isl_set_copy(pa->p[i].set);
8526 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8527 aff = isl_aff_copy(pa->p[i].aff);
8528 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8529 upa = isl_union_pw_aff_union_add(upa, upa_i);
8532 isl_multi_union_pw_aff_free(mupa);
8533 isl_pw_aff_free(pa);
8534 return upa;
8535 error:
8536 isl_multi_union_pw_aff_free(mupa);
8537 isl_pw_aff_free(pa);
8538 return NULL;
8541 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8542 * with the domain of "pma".
8543 * Furthermore, the dimension of this space needs to be greater than zero,
8544 * unless the dimension of the target space of "pma" is also zero.
8545 * The result is defined over the shared domain of the elements of "mupa"
8547 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8548 __isl_take isl_multi_union_pw_aff *mupa,
8549 __isl_take isl_pw_multi_aff *pma)
8551 isl_space *space1, *space2;
8552 isl_multi_union_pw_aff *res;
8553 int equal;
8554 int i, n_out;
8556 mupa = isl_multi_union_pw_aff_align_params(mupa,
8557 isl_pw_multi_aff_get_space(pma));
8558 pma = isl_pw_multi_aff_align_params(pma,
8559 isl_multi_union_pw_aff_get_space(mupa));
8560 if (!mupa || !pma)
8561 goto error;
8563 space1 = isl_multi_union_pw_aff_get_space(mupa);
8564 space2 = isl_pw_multi_aff_get_domain_space(pma);
8565 equal = isl_space_is_equal(space1, space2);
8566 isl_space_free(space1);
8567 isl_space_free(space2);
8568 if (equal < 0)
8569 goto error;
8570 if (!equal)
8571 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8572 "spaces don't match", goto error);
8573 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8574 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8575 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8576 "cannot determine domains", goto error);
8578 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8579 res = isl_multi_union_pw_aff_alloc(space1);
8581 for (i = 0; i < n_out; ++i) {
8582 isl_pw_aff *pa;
8583 isl_union_pw_aff *upa;
8585 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8586 upa = isl_multi_union_pw_aff_apply_pw_aff(
8587 isl_multi_union_pw_aff_copy(mupa), pa);
8588 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8591 isl_pw_multi_aff_free(pma);
8592 isl_multi_union_pw_aff_free(mupa);
8593 return res;
8594 error:
8595 isl_multi_union_pw_aff_free(mupa);
8596 isl_pw_multi_aff_free(pma);
8597 return NULL;
8600 /* Compute the pullback of "mupa" by the function represented by "upma".
8601 * In other words, plug in "upma" in "mupa". The result contains
8602 * expressions defined over the domain space of "upma".
8604 * Run over all elements of "mupa" and plug in "upma" in each of them.
8606 __isl_give isl_multi_union_pw_aff *
8607 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8608 __isl_take isl_multi_union_pw_aff *mupa,
8609 __isl_take isl_union_pw_multi_aff *upma)
8611 int i, n;
8613 mupa = isl_multi_union_pw_aff_align_params(mupa,
8614 isl_union_pw_multi_aff_get_space(upma));
8615 upma = isl_union_pw_multi_aff_align_params(upma,
8616 isl_multi_union_pw_aff_get_space(mupa));
8617 if (!mupa || !upma)
8618 goto error;
8620 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8621 for (i = 0; i < n; ++i) {
8622 isl_union_pw_aff *upa;
8624 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8625 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8626 isl_union_pw_multi_aff_copy(upma));
8627 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8630 isl_union_pw_multi_aff_free(upma);
8631 return mupa;
8632 error:
8633 isl_multi_union_pw_aff_free(mupa);
8634 isl_union_pw_multi_aff_free(upma);
8635 return NULL;
8638 /* Extract the sequence of elements in "mupa" with domain space "space"
8639 * (ignoring parameters).
8641 * For the elements of "mupa" that are not defined on the specified space,
8642 * the corresponding element in the result is empty.
8644 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8645 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8647 int i, n;
8648 isl_space *space_mpa = NULL;
8649 isl_multi_pw_aff *mpa;
8651 if (!mupa || !space)
8652 goto error;
8654 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8655 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8656 space = isl_space_drop_dims(space, isl_dim_param,
8657 0, isl_space_dim(space, isl_dim_param));
8658 space = isl_space_align_params(space,
8659 isl_space_copy(space_mpa));
8660 if (!space)
8661 goto error;
8663 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8664 space_mpa);
8665 mpa = isl_multi_pw_aff_alloc(space_mpa);
8667 space = isl_space_from_domain(space);
8668 space = isl_space_add_dims(space, isl_dim_out, 1);
8669 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8670 for (i = 0; i < n; ++i) {
8671 isl_union_pw_aff *upa;
8672 isl_pw_aff *pa;
8674 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8675 pa = isl_union_pw_aff_extract_pw_aff(upa,
8676 isl_space_copy(space));
8677 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8678 isl_union_pw_aff_free(upa);
8681 isl_space_free(space);
8682 return mpa;
8683 error:
8684 isl_space_free(space_mpa);
8685 isl_space_free(space);
8686 return NULL;