add isl_local_space_set_from_params
[isl.git] / isl_aff.c
blob6bae0b1ab4fc76b2f5742d7ffb20ff9788872b68
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_config.h>
32 #undef BASE
33 #define BASE aff
35 #include <isl_list_templ.c>
37 #undef BASE
38 #define BASE pw_aff
40 #include <isl_list_templ.c>
42 #undef BASE
43 #define BASE union_pw_aff
45 #include <isl_list_templ.c>
47 #undef BASE
48 #define BASE union_pw_multi_aff
50 #include <isl_list_templ.c>
52 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
53 __isl_take isl_vec *v)
55 isl_aff *aff;
57 if (!ls || !v)
58 goto error;
60 aff = isl_calloc_type(v->ctx, struct isl_aff);
61 if (!aff)
62 goto error;
64 aff->ref = 1;
65 aff->ls = ls;
66 aff->v = v;
68 return aff;
69 error:
70 isl_local_space_free(ls);
71 isl_vec_free(v);
72 return NULL;
75 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
77 isl_ctx *ctx;
78 isl_vec *v;
79 unsigned total;
81 if (!ls)
82 return NULL;
84 ctx = isl_local_space_get_ctx(ls);
85 if (!isl_local_space_divs_known(ls))
86 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
87 goto error);
88 if (!isl_local_space_is_set(ls))
89 isl_die(ctx, isl_error_invalid,
90 "domain of affine expression should be a set",
91 goto error);
93 total = isl_local_space_dim(ls, isl_dim_all);
94 v = isl_vec_alloc(ctx, 1 + 1 + total);
95 return isl_aff_alloc_vec(ls, v);
96 error:
97 isl_local_space_free(ls);
98 return NULL;
101 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
103 isl_aff *aff;
105 aff = isl_aff_alloc(ls);
106 if (!aff)
107 return NULL;
109 isl_int_set_si(aff->v->el[0], 1);
110 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
112 return aff;
115 /* Return a piecewise affine expression defined on the specified domain
116 * that is equal to zero.
118 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
120 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
123 /* Return an affine expression defined on the specified domain
124 * that represents NaN.
126 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
128 isl_aff *aff;
130 aff = isl_aff_alloc(ls);
131 if (!aff)
132 return NULL;
134 isl_seq_clr(aff->v->el, aff->v->size);
136 return aff;
139 /* Return a piecewise affine expression defined on the specified domain
140 * that represents NaN.
142 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
144 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
147 /* Return an affine expression that is equal to "val" on
148 * domain local space "ls".
150 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
151 __isl_take isl_val *val)
153 isl_aff *aff;
155 if (!ls || !val)
156 goto error;
157 if (!isl_val_is_rat(val))
158 isl_die(isl_val_get_ctx(val), isl_error_invalid,
159 "expecting rational value", goto error);
161 aff = isl_aff_alloc(isl_local_space_copy(ls));
162 if (!aff)
163 goto error;
165 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
166 isl_int_set(aff->v->el[1], val->n);
167 isl_int_set(aff->v->el[0], val->d);
169 isl_local_space_free(ls);
170 isl_val_free(val);
171 return aff;
172 error:
173 isl_local_space_free(ls);
174 isl_val_free(val);
175 return NULL;
178 /* Return an affine expression that is equal to the specified dimension
179 * in "ls".
181 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
182 enum isl_dim_type type, unsigned pos)
184 isl_space *space;
185 isl_aff *aff;
187 if (!ls)
188 return NULL;
190 space = isl_local_space_get_space(ls);
191 if (!space)
192 goto error;
193 if (isl_space_is_map(space))
194 isl_die(isl_space_get_ctx(space), isl_error_invalid,
195 "expecting (parameter) set space", goto error);
196 if (pos >= isl_local_space_dim(ls, type))
197 isl_die(isl_space_get_ctx(space), isl_error_invalid,
198 "position out of bounds", goto error);
200 isl_space_free(space);
201 aff = isl_aff_alloc(ls);
202 if (!aff)
203 return NULL;
205 pos += isl_local_space_offset(aff->ls, type);
207 isl_int_set_si(aff->v->el[0], 1);
208 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
209 isl_int_set_si(aff->v->el[1 + pos], 1);
211 return aff;
212 error:
213 isl_local_space_free(ls);
214 isl_space_free(space);
215 return NULL;
218 /* Return a piecewise affine expression that is equal to
219 * the specified dimension in "ls".
221 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
222 enum isl_dim_type type, unsigned pos)
224 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
227 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
229 if (!aff)
230 return NULL;
232 aff->ref++;
233 return aff;
236 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
238 if (!aff)
239 return NULL;
241 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
242 isl_vec_copy(aff->v));
245 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
247 if (!aff)
248 return NULL;
250 if (aff->ref == 1)
251 return aff;
252 aff->ref--;
253 return isl_aff_dup(aff);
256 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
258 if (!aff)
259 return NULL;
261 if (--aff->ref > 0)
262 return NULL;
264 isl_local_space_free(aff->ls);
265 isl_vec_free(aff->v);
267 free(aff);
269 return NULL;
272 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
274 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
277 /* Return a hash value that digests "aff".
279 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
281 uint32_t hash, ls_hash, v_hash;
283 if (!aff)
284 return 0;
286 hash = isl_hash_init();
287 ls_hash = isl_local_space_get_hash(aff->ls);
288 isl_hash_hash(hash, ls_hash);
289 v_hash = isl_vec_get_hash(aff->v);
290 isl_hash_hash(hash, v_hash);
292 return hash;
295 /* Externally, an isl_aff has a map space, but internally, the
296 * ls field corresponds to the domain of that space.
298 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
300 if (!aff)
301 return 0;
302 if (type == isl_dim_out)
303 return 1;
304 if (type == isl_dim_in)
305 type = isl_dim_set;
306 return isl_local_space_dim(aff->ls, type);
309 /* Return the position of the dimension of the given type and name
310 * in "aff".
311 * Return -1 if no such dimension can be found.
313 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
314 const char *name)
316 if (!aff)
317 return -1;
318 if (type == isl_dim_out)
319 return -1;
320 if (type == isl_dim_in)
321 type = isl_dim_set;
322 return isl_local_space_find_dim_by_name(aff->ls, type, name);
325 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
327 return aff ? isl_local_space_get_space(aff->ls) : NULL;
330 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
332 isl_space *space;
333 if (!aff)
334 return NULL;
335 space = isl_local_space_get_space(aff->ls);
336 space = isl_space_from_domain(space);
337 space = isl_space_add_dims(space, isl_dim_out, 1);
338 return space;
341 __isl_give isl_local_space *isl_aff_get_domain_local_space(
342 __isl_keep isl_aff *aff)
344 return aff ? isl_local_space_copy(aff->ls) : NULL;
347 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
349 isl_local_space *ls;
350 if (!aff)
351 return NULL;
352 ls = isl_local_space_copy(aff->ls);
353 ls = isl_local_space_from_domain(ls);
354 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
355 return ls;
358 /* Externally, an isl_aff has a map space, but internally, the
359 * ls field corresponds to the domain of that space.
361 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
362 enum isl_dim_type type, unsigned pos)
364 if (!aff)
365 return NULL;
366 if (type == isl_dim_out)
367 return NULL;
368 if (type == isl_dim_in)
369 type = isl_dim_set;
370 return isl_local_space_get_dim_name(aff->ls, type, pos);
373 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
374 __isl_take isl_space *dim)
376 aff = isl_aff_cow(aff);
377 if (!aff || !dim)
378 goto error;
380 aff->ls = isl_local_space_reset_space(aff->ls, dim);
381 if (!aff->ls)
382 return isl_aff_free(aff);
384 return aff;
385 error:
386 isl_aff_free(aff);
387 isl_space_free(dim);
388 return NULL;
391 /* Reset the space of "aff". This function is called from isl_pw_templ.c
392 * and doesn't know if the space of an element object is represented
393 * directly or through its domain. It therefore passes along both.
395 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
396 __isl_take isl_space *space, __isl_take isl_space *domain)
398 isl_space_free(space);
399 return isl_aff_reset_domain_space(aff, domain);
402 /* Reorder the coefficients of the affine expression based
403 * on the given reordering.
404 * The reordering r is assumed to have been extended with the local
405 * variables.
407 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
408 __isl_take isl_reordering *r, int n_div)
410 isl_vec *res;
411 int i;
413 if (!vec || !r)
414 goto error;
416 res = isl_vec_alloc(vec->ctx,
417 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
418 if (!res)
419 goto error;
420 isl_seq_cpy(res->el, vec->el, 2);
421 isl_seq_clr(res->el + 2, res->size - 2);
422 for (i = 0; i < r->len; ++i)
423 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
425 isl_reordering_free(r);
426 isl_vec_free(vec);
427 return res;
428 error:
429 isl_vec_free(vec);
430 isl_reordering_free(r);
431 return NULL;
434 /* Reorder the dimensions of the domain of "aff" according
435 * to the given reordering.
437 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
438 __isl_take isl_reordering *r)
440 aff = isl_aff_cow(aff);
441 if (!aff)
442 goto error;
444 r = isl_reordering_extend(r, aff->ls->div->n_row);
445 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
446 aff->ls->div->n_row);
447 aff->ls = isl_local_space_realign(aff->ls, r);
449 if (!aff->v || !aff->ls)
450 return isl_aff_free(aff);
452 return aff;
453 error:
454 isl_aff_free(aff);
455 isl_reordering_free(r);
456 return NULL;
459 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
460 __isl_take isl_space *model)
462 isl_bool equal_params;
464 if (!aff || !model)
465 goto error;
467 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
468 if (equal_params < 0)
469 goto error;
470 if (!equal_params) {
471 isl_reordering *exp;
473 model = isl_space_drop_dims(model, isl_dim_in,
474 0, isl_space_dim(model, isl_dim_in));
475 model = isl_space_drop_dims(model, isl_dim_out,
476 0, isl_space_dim(model, isl_dim_out));
477 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
478 exp = isl_reordering_extend_space(exp,
479 isl_aff_get_domain_space(aff));
480 aff = isl_aff_realign_domain(aff, exp);
483 isl_space_free(model);
484 return aff;
485 error:
486 isl_space_free(model);
487 isl_aff_free(aff);
488 return NULL;
491 /* Is "aff" obviously equal to zero?
493 * If the denominator is zero, then "aff" is not equal to zero.
495 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
497 if (!aff)
498 return isl_bool_error;
500 if (isl_int_is_zero(aff->v->el[0]))
501 return isl_bool_false;
502 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
505 /* Does "aff" represent NaN?
507 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
509 if (!aff)
510 return isl_bool_error;
512 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
515 /* Are "aff1" and "aff2" obviously equal?
517 * NaN is not equal to anything, not even to another NaN.
519 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
520 __isl_keep isl_aff *aff2)
522 isl_bool equal;
524 if (!aff1 || !aff2)
525 return isl_bool_error;
527 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
528 return isl_bool_false;
530 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
531 if (equal < 0 || !equal)
532 return equal;
534 return isl_vec_is_equal(aff1->v, aff2->v);
537 /* Return the common denominator of "aff" in "v".
539 * We cannot return anything meaningful in case of a NaN.
541 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
543 if (!aff)
544 return isl_stat_error;
545 if (isl_aff_is_nan(aff))
546 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
547 "cannot get denominator of NaN", return isl_stat_error);
548 isl_int_set(*v, aff->v->el[0]);
549 return isl_stat_ok;
552 /* Return the common denominator of "aff".
554 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
556 isl_ctx *ctx;
558 if (!aff)
559 return NULL;
561 ctx = isl_aff_get_ctx(aff);
562 if (isl_aff_is_nan(aff))
563 return isl_val_nan(ctx);
564 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
567 /* Return the constant term of "aff".
569 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
571 isl_ctx *ctx;
572 isl_val *v;
574 if (!aff)
575 return NULL;
577 ctx = isl_aff_get_ctx(aff);
578 if (isl_aff_is_nan(aff))
579 return isl_val_nan(ctx);
580 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
581 return isl_val_normalize(v);
584 /* Return the coefficient of the variable of type "type" at position "pos"
585 * of "aff".
587 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
588 enum isl_dim_type type, int pos)
590 isl_ctx *ctx;
591 isl_val *v;
593 if (!aff)
594 return NULL;
596 ctx = isl_aff_get_ctx(aff);
597 if (type == isl_dim_out)
598 isl_die(ctx, isl_error_invalid,
599 "output/set dimension does not have a coefficient",
600 return NULL);
601 if (type == isl_dim_in)
602 type = isl_dim_set;
604 if (pos >= isl_local_space_dim(aff->ls, type))
605 isl_die(ctx, isl_error_invalid,
606 "position out of bounds", return NULL);
608 if (isl_aff_is_nan(aff))
609 return isl_val_nan(ctx);
610 pos += isl_local_space_offset(aff->ls, type);
611 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
612 return isl_val_normalize(v);
615 /* Return the sign of the coefficient of the variable of type "type"
616 * at position "pos" of "aff".
618 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
619 int pos)
621 isl_ctx *ctx;
623 if (!aff)
624 return 0;
626 ctx = isl_aff_get_ctx(aff);
627 if (type == isl_dim_out)
628 isl_die(ctx, isl_error_invalid,
629 "output/set dimension does not have a coefficient",
630 return 0);
631 if (type == isl_dim_in)
632 type = isl_dim_set;
634 if (pos >= isl_local_space_dim(aff->ls, type))
635 isl_die(ctx, isl_error_invalid,
636 "position out of bounds", return 0);
638 pos += isl_local_space_offset(aff->ls, type);
639 return isl_int_sgn(aff->v->el[1 + pos]);
642 /* Replace the numerator of the constant term of "aff" by "v".
644 * A NaN is unaffected by this operation.
646 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
648 if (!aff)
649 return NULL;
650 if (isl_aff_is_nan(aff))
651 return aff;
652 aff = isl_aff_cow(aff);
653 if (!aff)
654 return NULL;
656 aff->v = isl_vec_cow(aff->v);
657 if (!aff->v)
658 return isl_aff_free(aff);
660 isl_int_set(aff->v->el[1], v);
662 return aff;
665 /* Replace the constant term of "aff" by "v".
667 * A NaN is unaffected by this operation.
669 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
670 __isl_take isl_val *v)
672 if (!aff || !v)
673 goto error;
675 if (isl_aff_is_nan(aff)) {
676 isl_val_free(v);
677 return aff;
680 if (!isl_val_is_rat(v))
681 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
682 "expecting rational value", goto error);
684 if (isl_int_eq(aff->v->el[1], v->n) &&
685 isl_int_eq(aff->v->el[0], v->d)) {
686 isl_val_free(v);
687 return aff;
690 aff = isl_aff_cow(aff);
691 if (!aff)
692 goto error;
693 aff->v = isl_vec_cow(aff->v);
694 if (!aff->v)
695 goto error;
697 if (isl_int_eq(aff->v->el[0], v->d)) {
698 isl_int_set(aff->v->el[1], v->n);
699 } else if (isl_int_is_one(v->d)) {
700 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
701 } else {
702 isl_seq_scale(aff->v->el + 1,
703 aff->v->el + 1, v->d, aff->v->size - 1);
704 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
705 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
706 aff->v = isl_vec_normalize(aff->v);
707 if (!aff->v)
708 goto error;
711 isl_val_free(v);
712 return aff;
713 error:
714 isl_aff_free(aff);
715 isl_val_free(v);
716 return NULL;
719 /* Add "v" to the constant term of "aff".
721 * A NaN is unaffected by this operation.
723 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
725 if (isl_int_is_zero(v))
726 return aff;
728 if (!aff)
729 return NULL;
730 if (isl_aff_is_nan(aff))
731 return aff;
732 aff = isl_aff_cow(aff);
733 if (!aff)
734 return NULL;
736 aff->v = isl_vec_cow(aff->v);
737 if (!aff->v)
738 return isl_aff_free(aff);
740 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
742 return aff;
745 /* Add "v" to the constant term of "aff".
747 * A NaN is unaffected by this operation.
749 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
750 __isl_take isl_val *v)
752 if (!aff || !v)
753 goto error;
755 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
756 isl_val_free(v);
757 return aff;
760 if (!isl_val_is_rat(v))
761 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
762 "expecting rational value", goto error);
764 aff = isl_aff_cow(aff);
765 if (!aff)
766 goto error;
768 aff->v = isl_vec_cow(aff->v);
769 if (!aff->v)
770 goto error;
772 if (isl_int_is_one(v->d)) {
773 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
774 } else if (isl_int_eq(aff->v->el[0], v->d)) {
775 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
776 aff->v = isl_vec_normalize(aff->v);
777 if (!aff->v)
778 goto error;
779 } else {
780 isl_seq_scale(aff->v->el + 1,
781 aff->v->el + 1, v->d, aff->v->size - 1);
782 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
783 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
784 aff->v = isl_vec_normalize(aff->v);
785 if (!aff->v)
786 goto error;
789 isl_val_free(v);
790 return aff;
791 error:
792 isl_aff_free(aff);
793 isl_val_free(v);
794 return NULL;
797 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
799 isl_int t;
801 isl_int_init(t);
802 isl_int_set_si(t, v);
803 aff = isl_aff_add_constant(aff, t);
804 isl_int_clear(t);
806 return aff;
809 /* Add "v" to the numerator of the constant term of "aff".
811 * A NaN is unaffected by this operation.
813 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
815 if (isl_int_is_zero(v))
816 return aff;
818 if (!aff)
819 return NULL;
820 if (isl_aff_is_nan(aff))
821 return aff;
822 aff = isl_aff_cow(aff);
823 if (!aff)
824 return NULL;
826 aff->v = isl_vec_cow(aff->v);
827 if (!aff->v)
828 return isl_aff_free(aff);
830 isl_int_add(aff->v->el[1], aff->v->el[1], v);
832 return aff;
835 /* Add "v" to the numerator of the constant term of "aff".
837 * A NaN is unaffected by this operation.
839 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
841 isl_int t;
843 if (v == 0)
844 return aff;
846 isl_int_init(t);
847 isl_int_set_si(t, v);
848 aff = isl_aff_add_constant_num(aff, t);
849 isl_int_clear(t);
851 return aff;
854 /* Replace the numerator of the constant term of "aff" by "v".
856 * A NaN is unaffected by this operation.
858 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
860 if (!aff)
861 return NULL;
862 if (isl_aff_is_nan(aff))
863 return aff;
864 aff = isl_aff_cow(aff);
865 if (!aff)
866 return NULL;
868 aff->v = isl_vec_cow(aff->v);
869 if (!aff->v)
870 return isl_aff_free(aff);
872 isl_int_set_si(aff->v->el[1], v);
874 return aff;
877 /* Replace the numerator of the coefficient of the variable of type "type"
878 * at position "pos" of "aff" by "v".
880 * A NaN is unaffected by this operation.
882 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
883 enum isl_dim_type type, int pos, isl_int v)
885 if (!aff)
886 return NULL;
888 if (type == isl_dim_out)
889 isl_die(aff->v->ctx, isl_error_invalid,
890 "output/set dimension does not have a coefficient",
891 return isl_aff_free(aff));
892 if (type == isl_dim_in)
893 type = isl_dim_set;
895 if (pos >= isl_local_space_dim(aff->ls, type))
896 isl_die(aff->v->ctx, isl_error_invalid,
897 "position out of bounds", return isl_aff_free(aff));
899 if (isl_aff_is_nan(aff))
900 return aff;
901 aff = isl_aff_cow(aff);
902 if (!aff)
903 return NULL;
905 aff->v = isl_vec_cow(aff->v);
906 if (!aff->v)
907 return isl_aff_free(aff);
909 pos += isl_local_space_offset(aff->ls, type);
910 isl_int_set(aff->v->el[1 + pos], v);
912 return aff;
915 /* Replace the numerator of the coefficient of the variable of type "type"
916 * at position "pos" of "aff" by "v".
918 * A NaN is unaffected by this operation.
920 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
921 enum isl_dim_type type, int pos, int v)
923 if (!aff)
924 return NULL;
926 if (type == isl_dim_out)
927 isl_die(aff->v->ctx, isl_error_invalid,
928 "output/set dimension does not have a coefficient",
929 return isl_aff_free(aff));
930 if (type == isl_dim_in)
931 type = isl_dim_set;
933 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
934 isl_die(aff->v->ctx, isl_error_invalid,
935 "position out of bounds", return isl_aff_free(aff));
937 if (isl_aff_is_nan(aff))
938 return aff;
939 pos += isl_local_space_offset(aff->ls, type);
940 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
941 return aff;
943 aff = isl_aff_cow(aff);
944 if (!aff)
945 return NULL;
947 aff->v = isl_vec_cow(aff->v);
948 if (!aff->v)
949 return isl_aff_free(aff);
951 isl_int_set_si(aff->v->el[1 + pos], v);
953 return aff;
956 /* Replace the coefficient of the variable of type "type" at position "pos"
957 * of "aff" by "v".
959 * A NaN is unaffected by this operation.
961 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
962 enum isl_dim_type type, int pos, __isl_take isl_val *v)
964 if (!aff || !v)
965 goto error;
967 if (type == isl_dim_out)
968 isl_die(aff->v->ctx, isl_error_invalid,
969 "output/set dimension does not have a coefficient",
970 goto error);
971 if (type == isl_dim_in)
972 type = isl_dim_set;
974 if (pos >= isl_local_space_dim(aff->ls, type))
975 isl_die(aff->v->ctx, isl_error_invalid,
976 "position out of bounds", goto error);
978 if (isl_aff_is_nan(aff)) {
979 isl_val_free(v);
980 return aff;
982 if (!isl_val_is_rat(v))
983 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
984 "expecting rational value", goto error);
986 pos += isl_local_space_offset(aff->ls, type);
987 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
988 isl_int_eq(aff->v->el[0], v->d)) {
989 isl_val_free(v);
990 return aff;
993 aff = isl_aff_cow(aff);
994 if (!aff)
995 goto error;
996 aff->v = isl_vec_cow(aff->v);
997 if (!aff->v)
998 goto error;
1000 if (isl_int_eq(aff->v->el[0], v->d)) {
1001 isl_int_set(aff->v->el[1 + pos], v->n);
1002 } else if (isl_int_is_one(v->d)) {
1003 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1004 } else {
1005 isl_seq_scale(aff->v->el + 1,
1006 aff->v->el + 1, v->d, aff->v->size - 1);
1007 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1008 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1009 aff->v = isl_vec_normalize(aff->v);
1010 if (!aff->v)
1011 goto error;
1014 isl_val_free(v);
1015 return aff;
1016 error:
1017 isl_aff_free(aff);
1018 isl_val_free(v);
1019 return NULL;
1022 /* Add "v" to the coefficient of the variable of type "type"
1023 * at position "pos" of "aff".
1025 * A NaN is unaffected by this operation.
1027 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1028 enum isl_dim_type type, int pos, isl_int v)
1030 if (!aff)
1031 return NULL;
1033 if (type == isl_dim_out)
1034 isl_die(aff->v->ctx, isl_error_invalid,
1035 "output/set dimension does not have a coefficient",
1036 return isl_aff_free(aff));
1037 if (type == isl_dim_in)
1038 type = isl_dim_set;
1040 if (pos >= isl_local_space_dim(aff->ls, type))
1041 isl_die(aff->v->ctx, isl_error_invalid,
1042 "position out of bounds", return isl_aff_free(aff));
1044 if (isl_aff_is_nan(aff))
1045 return aff;
1046 aff = isl_aff_cow(aff);
1047 if (!aff)
1048 return NULL;
1050 aff->v = isl_vec_cow(aff->v);
1051 if (!aff->v)
1052 return isl_aff_free(aff);
1054 pos += isl_local_space_offset(aff->ls, type);
1055 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1057 return aff;
1060 /* Add "v" to the coefficient of the variable of type "type"
1061 * at position "pos" of "aff".
1063 * A NaN is unaffected by this operation.
1065 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1066 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1068 if (!aff || !v)
1069 goto error;
1071 if (isl_val_is_zero(v)) {
1072 isl_val_free(v);
1073 return aff;
1076 if (type == isl_dim_out)
1077 isl_die(aff->v->ctx, isl_error_invalid,
1078 "output/set dimension does not have a coefficient",
1079 goto error);
1080 if (type == isl_dim_in)
1081 type = isl_dim_set;
1083 if (pos >= isl_local_space_dim(aff->ls, type))
1084 isl_die(aff->v->ctx, isl_error_invalid,
1085 "position out of bounds", goto error);
1087 if (isl_aff_is_nan(aff)) {
1088 isl_val_free(v);
1089 return aff;
1091 if (!isl_val_is_rat(v))
1092 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1093 "expecting rational value", goto error);
1095 aff = isl_aff_cow(aff);
1096 if (!aff)
1097 goto error;
1099 aff->v = isl_vec_cow(aff->v);
1100 if (!aff->v)
1101 goto error;
1103 pos += isl_local_space_offset(aff->ls, type);
1104 if (isl_int_is_one(v->d)) {
1105 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1106 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1107 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1108 aff->v = isl_vec_normalize(aff->v);
1109 if (!aff->v)
1110 goto error;
1111 } else {
1112 isl_seq_scale(aff->v->el + 1,
1113 aff->v->el + 1, v->d, aff->v->size - 1);
1114 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1115 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1116 aff->v = isl_vec_normalize(aff->v);
1117 if (!aff->v)
1118 goto error;
1121 isl_val_free(v);
1122 return aff;
1123 error:
1124 isl_aff_free(aff);
1125 isl_val_free(v);
1126 return NULL;
1129 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1130 enum isl_dim_type type, int pos, int v)
1132 isl_int t;
1134 isl_int_init(t);
1135 isl_int_set_si(t, v);
1136 aff = isl_aff_add_coefficient(aff, type, pos, t);
1137 isl_int_clear(t);
1139 return aff;
1142 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1144 if (!aff)
1145 return NULL;
1147 return isl_local_space_get_div(aff->ls, pos);
1150 /* Return the negation of "aff".
1152 * As a special case, -NaN = NaN.
1154 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1156 if (!aff)
1157 return NULL;
1158 if (isl_aff_is_nan(aff))
1159 return aff;
1160 aff = isl_aff_cow(aff);
1161 if (!aff)
1162 return NULL;
1163 aff->v = isl_vec_cow(aff->v);
1164 if (!aff->v)
1165 return isl_aff_free(aff);
1167 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1169 return aff;
1172 /* Remove divs from the local space that do not appear in the affine
1173 * expression.
1174 * We currently only remove divs at the end.
1175 * Some intermediate divs may also not appear directly in the affine
1176 * expression, but we would also need to check that no other divs are
1177 * defined in terms of them.
1179 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1181 int pos;
1182 int off;
1183 int n;
1185 if (!aff)
1186 return NULL;
1188 n = isl_local_space_dim(aff->ls, isl_dim_div);
1189 off = isl_local_space_offset(aff->ls, isl_dim_div);
1191 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1192 if (pos == n)
1193 return aff;
1195 aff = isl_aff_cow(aff);
1196 if (!aff)
1197 return NULL;
1199 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1200 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1201 if (!aff->ls || !aff->v)
1202 return isl_aff_free(aff);
1204 return aff;
1207 /* Look for any divs in the aff->ls with a denominator equal to one
1208 * and plug them into the affine expression and any subsequent divs
1209 * that may reference the div.
1211 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1213 int i, n;
1214 int len;
1215 isl_int v;
1216 isl_vec *vec;
1217 isl_local_space *ls;
1218 unsigned pos;
1220 if (!aff)
1221 return NULL;
1223 n = isl_local_space_dim(aff->ls, isl_dim_div);
1224 len = aff->v->size;
1225 for (i = 0; i < n; ++i) {
1226 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1227 continue;
1228 ls = isl_local_space_copy(aff->ls);
1229 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1230 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1231 vec = isl_vec_copy(aff->v);
1232 vec = isl_vec_cow(vec);
1233 if (!ls || !vec)
1234 goto error;
1236 isl_int_init(v);
1238 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1239 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1240 len, len, v);
1242 isl_int_clear(v);
1244 isl_vec_free(aff->v);
1245 aff->v = vec;
1246 isl_local_space_free(aff->ls);
1247 aff->ls = ls;
1250 return aff;
1251 error:
1252 isl_vec_free(vec);
1253 isl_local_space_free(ls);
1254 return isl_aff_free(aff);
1257 /* Look for any divs j that appear with a unit coefficient inside
1258 * the definitions of other divs i and plug them into the definitions
1259 * of the divs i.
1261 * In particular, an expression of the form
1263 * floor((f(..) + floor(g(..)/n))/m)
1265 * is simplified to
1267 * floor((n * f(..) + g(..))/(n * m))
1269 * This simplification is correct because we can move the expression
1270 * f(..) into the inner floor in the original expression to obtain
1272 * floor(floor((n * f(..) + g(..))/n)/m)
1274 * from which we can derive the simplified expression.
1276 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1278 int i, j, n;
1279 int off;
1281 if (!aff)
1282 return NULL;
1284 n = isl_local_space_dim(aff->ls, isl_dim_div);
1285 off = isl_local_space_offset(aff->ls, isl_dim_div);
1286 for (i = 1; i < n; ++i) {
1287 for (j = 0; j < i; ++j) {
1288 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1289 continue;
1290 aff->ls = isl_local_space_substitute_seq(aff->ls,
1291 isl_dim_div, j, aff->ls->div->row[j],
1292 aff->v->size, i, 1);
1293 if (!aff->ls)
1294 return isl_aff_free(aff);
1298 return aff;
1301 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1303 * Even though this function is only called on isl_affs with a single
1304 * reference, we are careful to only change aff->v and aff->ls together.
1306 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1308 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1309 isl_local_space *ls;
1310 isl_vec *v;
1312 ls = isl_local_space_copy(aff->ls);
1313 ls = isl_local_space_swap_div(ls, a, b);
1314 v = isl_vec_copy(aff->v);
1315 v = isl_vec_cow(v);
1316 if (!ls || !v)
1317 goto error;
1319 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1320 isl_vec_free(aff->v);
1321 aff->v = v;
1322 isl_local_space_free(aff->ls);
1323 aff->ls = ls;
1325 return aff;
1326 error:
1327 isl_vec_free(v);
1328 isl_local_space_free(ls);
1329 return isl_aff_free(aff);
1332 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1334 * We currently do not actually remove div "b", but simply add its
1335 * coefficient to that of "a" and then zero it out.
1337 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1339 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1341 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1342 return aff;
1344 aff->v = isl_vec_cow(aff->v);
1345 if (!aff->v)
1346 return isl_aff_free(aff);
1348 isl_int_add(aff->v->el[1 + off + a],
1349 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1350 isl_int_set_si(aff->v->el[1 + off + b], 0);
1352 return aff;
1355 /* Sort the divs in the local space of "aff" according to
1356 * the comparison function "cmp_row" in isl_local_space.c,
1357 * combining the coefficients of identical divs.
1359 * Reordering divs does not change the semantics of "aff",
1360 * so there is no need to call isl_aff_cow.
1361 * Moreover, this function is currently only called on isl_affs
1362 * with a single reference.
1364 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1366 int i, j, n;
1368 if (!aff)
1369 return NULL;
1371 n = isl_aff_dim(aff, isl_dim_div);
1372 for (i = 1; i < n; ++i) {
1373 for (j = i - 1; j >= 0; --j) {
1374 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1375 if (cmp < 0)
1376 break;
1377 if (cmp == 0)
1378 aff = merge_divs(aff, j, j + 1);
1379 else
1380 aff = swap_div(aff, j, j + 1);
1381 if (!aff)
1382 return NULL;
1386 return aff;
1389 /* Normalize the representation of "aff".
1391 * This function should only be called of "new" isl_affs, i.e.,
1392 * with only a single reference. We therefore do not need to
1393 * worry about affecting other instances.
1395 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1397 if (!aff)
1398 return NULL;
1399 aff->v = isl_vec_normalize(aff->v);
1400 if (!aff->v)
1401 return isl_aff_free(aff);
1402 aff = plug_in_integral_divs(aff);
1403 aff = plug_in_unit_divs(aff);
1404 aff = sort_divs(aff);
1405 aff = isl_aff_remove_unused_divs(aff);
1406 return aff;
1409 /* Given f, return floor(f).
1410 * If f is an integer expression, then just return f.
1411 * If f is a constant, then return the constant floor(f).
1412 * Otherwise, if f = g/m, write g = q m + r,
1413 * create a new div d = [r/m] and return the expression q + d.
1414 * The coefficients in r are taken to lie between -m/2 and m/2.
1416 * reduce_div_coefficients performs the same normalization.
1418 * As a special case, floor(NaN) = NaN.
1420 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1422 int i;
1423 int size;
1424 isl_ctx *ctx;
1425 isl_vec *div;
1427 if (!aff)
1428 return NULL;
1430 if (isl_aff_is_nan(aff))
1431 return aff;
1432 if (isl_int_is_one(aff->v->el[0]))
1433 return aff;
1435 aff = isl_aff_cow(aff);
1436 if (!aff)
1437 return NULL;
1439 aff->v = isl_vec_cow(aff->v);
1440 if (!aff->v)
1441 return isl_aff_free(aff);
1443 if (isl_aff_is_cst(aff)) {
1444 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1445 isl_int_set_si(aff->v->el[0], 1);
1446 return aff;
1449 div = isl_vec_copy(aff->v);
1450 div = isl_vec_cow(div);
1451 if (!div)
1452 return isl_aff_free(aff);
1454 ctx = isl_aff_get_ctx(aff);
1455 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1456 for (i = 1; i < aff->v->size; ++i) {
1457 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1458 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1459 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1460 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1461 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1465 aff->ls = isl_local_space_add_div(aff->ls, div);
1466 if (!aff->ls)
1467 return isl_aff_free(aff);
1469 size = aff->v->size;
1470 aff->v = isl_vec_extend(aff->v, size + 1);
1471 if (!aff->v)
1472 return isl_aff_free(aff);
1473 isl_int_set_si(aff->v->el[0], 1);
1474 isl_int_set_si(aff->v->el[size], 1);
1476 aff = isl_aff_normalize(aff);
1478 return aff;
1481 /* Compute
1483 * aff mod m = aff - m * floor(aff/m)
1485 * with m an integer value.
1487 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1488 __isl_take isl_val *m)
1490 isl_aff *res;
1492 if (!aff || !m)
1493 goto error;
1495 if (!isl_val_is_int(m))
1496 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1497 "expecting integer modulo", goto error);
1499 res = isl_aff_copy(aff);
1500 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1501 aff = isl_aff_floor(aff);
1502 aff = isl_aff_scale_val(aff, m);
1503 res = isl_aff_sub(res, aff);
1505 return res;
1506 error:
1507 isl_aff_free(aff);
1508 isl_val_free(m);
1509 return NULL;
1512 /* Compute
1514 * pwaff mod m = pwaff - m * floor(pwaff/m)
1516 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1518 isl_pw_aff *res;
1520 res = isl_pw_aff_copy(pwaff);
1521 pwaff = isl_pw_aff_scale_down(pwaff, m);
1522 pwaff = isl_pw_aff_floor(pwaff);
1523 pwaff = isl_pw_aff_scale(pwaff, m);
1524 res = isl_pw_aff_sub(res, pwaff);
1526 return res;
1529 /* Compute
1531 * pa mod m = pa - m * floor(pa/m)
1533 * with m an integer value.
1535 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1536 __isl_take isl_val *m)
1538 if (!pa || !m)
1539 goto error;
1540 if (!isl_val_is_int(m))
1541 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1542 "expecting integer modulo", goto error);
1543 pa = isl_pw_aff_mod(pa, m->n);
1544 isl_val_free(m);
1545 return pa;
1546 error:
1547 isl_pw_aff_free(pa);
1548 isl_val_free(m);
1549 return NULL;
1552 /* Given f, return ceil(f).
1553 * If f is an integer expression, then just return f.
1554 * Otherwise, let f be the expression
1556 * e/m
1558 * then return
1560 * floor((e + m - 1)/m)
1562 * As a special case, ceil(NaN) = NaN.
1564 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1566 if (!aff)
1567 return NULL;
1569 if (isl_aff_is_nan(aff))
1570 return aff;
1571 if (isl_int_is_one(aff->v->el[0]))
1572 return aff;
1574 aff = isl_aff_cow(aff);
1575 if (!aff)
1576 return NULL;
1577 aff->v = isl_vec_cow(aff->v);
1578 if (!aff->v)
1579 return isl_aff_free(aff);
1581 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1582 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1583 aff = isl_aff_floor(aff);
1585 return aff;
1588 /* Apply the expansion computed by isl_merge_divs.
1589 * The expansion itself is given by "exp" while the resulting
1590 * list of divs is given by "div".
1592 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1593 __isl_take isl_mat *div, int *exp)
1595 int old_n_div;
1596 int new_n_div;
1597 int offset;
1599 aff = isl_aff_cow(aff);
1600 if (!aff || !div)
1601 goto error;
1603 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1604 new_n_div = isl_mat_rows(div);
1605 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1607 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1608 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1609 if (!aff->v || !aff->ls)
1610 return isl_aff_free(aff);
1611 return aff;
1612 error:
1613 isl_aff_free(aff);
1614 isl_mat_free(div);
1615 return NULL;
1618 /* Add two affine expressions that live in the same local space.
1620 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1621 __isl_take isl_aff *aff2)
1623 isl_int gcd, f;
1625 aff1 = isl_aff_cow(aff1);
1626 if (!aff1 || !aff2)
1627 goto error;
1629 aff1->v = isl_vec_cow(aff1->v);
1630 if (!aff1->v)
1631 goto error;
1633 isl_int_init(gcd);
1634 isl_int_init(f);
1635 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1636 isl_int_divexact(f, aff2->v->el[0], gcd);
1637 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1638 isl_int_divexact(f, aff1->v->el[0], gcd);
1639 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1640 isl_int_divexact(f, aff2->v->el[0], gcd);
1641 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1642 isl_int_clear(f);
1643 isl_int_clear(gcd);
1645 isl_aff_free(aff2);
1646 return aff1;
1647 error:
1648 isl_aff_free(aff1);
1649 isl_aff_free(aff2);
1650 return NULL;
1653 /* Return the sum of "aff1" and "aff2".
1655 * If either of the two is NaN, then the result is NaN.
1657 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1658 __isl_take isl_aff *aff2)
1660 isl_ctx *ctx;
1661 int *exp1 = NULL;
1662 int *exp2 = NULL;
1663 isl_mat *div;
1664 int n_div1, n_div2;
1666 if (!aff1 || !aff2)
1667 goto error;
1669 ctx = isl_aff_get_ctx(aff1);
1670 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1671 isl_die(ctx, isl_error_invalid,
1672 "spaces don't match", goto error);
1674 if (isl_aff_is_nan(aff1)) {
1675 isl_aff_free(aff2);
1676 return aff1;
1678 if (isl_aff_is_nan(aff2)) {
1679 isl_aff_free(aff1);
1680 return aff2;
1683 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1684 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1685 if (n_div1 == 0 && n_div2 == 0)
1686 return add_expanded(aff1, aff2);
1688 exp1 = isl_alloc_array(ctx, int, n_div1);
1689 exp2 = isl_alloc_array(ctx, int, n_div2);
1690 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1691 goto error;
1693 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1694 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1695 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1696 free(exp1);
1697 free(exp2);
1699 return add_expanded(aff1, aff2);
1700 error:
1701 free(exp1);
1702 free(exp2);
1703 isl_aff_free(aff1);
1704 isl_aff_free(aff2);
1705 return NULL;
1708 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1709 __isl_take isl_aff *aff2)
1711 return isl_aff_add(aff1, isl_aff_neg(aff2));
1714 /* Return the result of scaling "aff" by a factor of "f".
1716 * As a special case, f * NaN = NaN.
1718 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1720 isl_int gcd;
1722 if (!aff)
1723 return NULL;
1724 if (isl_aff_is_nan(aff))
1725 return aff;
1727 if (isl_int_is_one(f))
1728 return aff;
1730 aff = isl_aff_cow(aff);
1731 if (!aff)
1732 return NULL;
1733 aff->v = isl_vec_cow(aff->v);
1734 if (!aff->v)
1735 return isl_aff_free(aff);
1737 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1738 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1739 return aff;
1742 isl_int_init(gcd);
1743 isl_int_gcd(gcd, aff->v->el[0], f);
1744 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1745 isl_int_divexact(gcd, f, gcd);
1746 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1747 isl_int_clear(gcd);
1749 return aff;
1752 /* Multiple "aff" by "v".
1754 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1755 __isl_take isl_val *v)
1757 if (!aff || !v)
1758 goto error;
1760 if (isl_val_is_one(v)) {
1761 isl_val_free(v);
1762 return aff;
1765 if (!isl_val_is_rat(v))
1766 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1767 "expecting rational factor", goto error);
1769 aff = isl_aff_scale(aff, v->n);
1770 aff = isl_aff_scale_down(aff, v->d);
1772 isl_val_free(v);
1773 return aff;
1774 error:
1775 isl_aff_free(aff);
1776 isl_val_free(v);
1777 return NULL;
1780 /* Return the result of scaling "aff" down by a factor of "f".
1782 * As a special case, NaN/f = NaN.
1784 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1786 isl_int gcd;
1788 if (!aff)
1789 return NULL;
1790 if (isl_aff_is_nan(aff))
1791 return aff;
1793 if (isl_int_is_one(f))
1794 return aff;
1796 aff = isl_aff_cow(aff);
1797 if (!aff)
1798 return NULL;
1800 if (isl_int_is_zero(f))
1801 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1802 "cannot scale down by zero", return isl_aff_free(aff));
1804 aff->v = isl_vec_cow(aff->v);
1805 if (!aff->v)
1806 return isl_aff_free(aff);
1808 isl_int_init(gcd);
1809 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1810 isl_int_gcd(gcd, gcd, f);
1811 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1812 isl_int_divexact(gcd, f, gcd);
1813 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1814 isl_int_clear(gcd);
1816 return aff;
1819 /* Divide "aff" by "v".
1821 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1822 __isl_take isl_val *v)
1824 if (!aff || !v)
1825 goto error;
1827 if (isl_val_is_one(v)) {
1828 isl_val_free(v);
1829 return aff;
1832 if (!isl_val_is_rat(v))
1833 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1834 "expecting rational factor", goto error);
1835 if (!isl_val_is_pos(v))
1836 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1837 "factor needs to be positive", goto error);
1839 aff = isl_aff_scale(aff, v->d);
1840 aff = isl_aff_scale_down(aff, v->n);
1842 isl_val_free(v);
1843 return aff;
1844 error:
1845 isl_aff_free(aff);
1846 isl_val_free(v);
1847 return NULL;
1850 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1852 isl_int v;
1854 if (f == 1)
1855 return aff;
1857 isl_int_init(v);
1858 isl_int_set_ui(v, f);
1859 aff = isl_aff_scale_down(aff, v);
1860 isl_int_clear(v);
1862 return aff;
1865 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1866 enum isl_dim_type type, unsigned pos, const char *s)
1868 aff = isl_aff_cow(aff);
1869 if (!aff)
1870 return NULL;
1871 if (type == isl_dim_out)
1872 isl_die(aff->v->ctx, isl_error_invalid,
1873 "cannot set name of output/set dimension",
1874 return isl_aff_free(aff));
1875 if (type == isl_dim_in)
1876 type = isl_dim_set;
1877 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1878 if (!aff->ls)
1879 return isl_aff_free(aff);
1881 return aff;
1884 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1885 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1887 aff = isl_aff_cow(aff);
1888 if (!aff)
1889 goto error;
1890 if (type == isl_dim_out)
1891 isl_die(aff->v->ctx, isl_error_invalid,
1892 "cannot set name of output/set dimension",
1893 goto error);
1894 if (type == isl_dim_in)
1895 type = isl_dim_set;
1896 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1897 if (!aff->ls)
1898 return isl_aff_free(aff);
1900 return aff;
1901 error:
1902 isl_id_free(id);
1903 isl_aff_free(aff);
1904 return NULL;
1907 /* Replace the identifier of the input tuple of "aff" by "id".
1908 * type is currently required to be equal to isl_dim_in
1910 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1911 enum isl_dim_type type, __isl_take isl_id *id)
1913 aff = isl_aff_cow(aff);
1914 if (!aff)
1915 goto error;
1916 if (type != isl_dim_out)
1917 isl_die(aff->v->ctx, isl_error_invalid,
1918 "cannot only set id of input tuple", goto error);
1919 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
1920 if (!aff->ls)
1921 return isl_aff_free(aff);
1923 return aff;
1924 error:
1925 isl_id_free(id);
1926 isl_aff_free(aff);
1927 return NULL;
1930 /* Exploit the equalities in "eq" to simplify the affine expression
1931 * and the expressions of the integer divisions in the local space.
1932 * The integer divisions in this local space are assumed to appear
1933 * as regular dimensions in "eq".
1935 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1936 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1938 int i, j;
1939 unsigned total;
1940 unsigned n_div;
1942 if (!eq)
1943 goto error;
1944 if (eq->n_eq == 0) {
1945 isl_basic_set_free(eq);
1946 return aff;
1949 aff = isl_aff_cow(aff);
1950 if (!aff)
1951 goto error;
1953 aff->ls = isl_local_space_substitute_equalities(aff->ls,
1954 isl_basic_set_copy(eq));
1955 aff->v = isl_vec_cow(aff->v);
1956 if (!aff->ls || !aff->v)
1957 goto error;
1959 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
1960 n_div = eq->n_div;
1961 for (i = 0; i < eq->n_eq; ++i) {
1962 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
1963 if (j < 0 || j == 0 || j >= total)
1964 continue;
1966 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
1967 &aff->v->el[0]);
1970 isl_basic_set_free(eq);
1971 aff = isl_aff_normalize(aff);
1972 return aff;
1973 error:
1974 isl_basic_set_free(eq);
1975 isl_aff_free(aff);
1976 return NULL;
1979 /* Exploit the equalities in "eq" to simplify the affine expression
1980 * and the expressions of the integer divisions in the local space.
1982 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
1983 __isl_take isl_basic_set *eq)
1985 int n_div;
1987 if (!aff || !eq)
1988 goto error;
1989 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1990 if (n_div > 0)
1991 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
1992 return isl_aff_substitute_equalities_lifted(aff, eq);
1993 error:
1994 isl_basic_set_free(eq);
1995 isl_aff_free(aff);
1996 return NULL;
1999 /* Look for equalities among the variables shared by context and aff
2000 * and the integer divisions of aff, if any.
2001 * The equalities are then used to eliminate coefficients and/or integer
2002 * divisions from aff.
2004 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2005 __isl_take isl_set *context)
2007 isl_basic_set *hull;
2008 int n_div;
2010 if (!aff)
2011 goto error;
2012 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2013 if (n_div > 0) {
2014 isl_basic_set *bset;
2015 isl_local_space *ls;
2016 context = isl_set_add_dims(context, isl_dim_set, n_div);
2017 ls = isl_aff_get_domain_local_space(aff);
2018 bset = isl_basic_set_from_local_space(ls);
2019 bset = isl_basic_set_lift(bset);
2020 bset = isl_basic_set_flatten(bset);
2021 context = isl_set_intersect(context,
2022 isl_set_from_basic_set(bset));
2025 hull = isl_set_affine_hull(context);
2026 return isl_aff_substitute_equalities_lifted(aff, hull);
2027 error:
2028 isl_aff_free(aff);
2029 isl_set_free(context);
2030 return NULL;
2033 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2034 __isl_take isl_set *context)
2036 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2037 dom_context = isl_set_intersect_params(dom_context, context);
2038 return isl_aff_gist(aff, dom_context);
2041 /* Return a basic set containing those elements in the space
2042 * of aff where it is positive. "rational" should not be set.
2044 * If "aff" is NaN, then it is not positive.
2046 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2047 int rational)
2049 isl_constraint *ineq;
2050 isl_basic_set *bset;
2051 isl_val *c;
2053 if (!aff)
2054 return NULL;
2055 if (isl_aff_is_nan(aff)) {
2056 isl_space *space = isl_aff_get_domain_space(aff);
2057 isl_aff_free(aff);
2058 return isl_basic_set_empty(space);
2060 if (rational)
2061 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2062 "rational sets not supported", goto error);
2064 ineq = isl_inequality_from_aff(aff);
2065 c = isl_constraint_get_constant_val(ineq);
2066 c = isl_val_sub_ui(c, 1);
2067 ineq = isl_constraint_set_constant_val(ineq, c);
2069 bset = isl_basic_set_from_constraint(ineq);
2070 bset = isl_basic_set_simplify(bset);
2071 return bset;
2072 error:
2073 isl_aff_free(aff);
2074 return NULL;
2077 /* Return a basic set containing those elements in the space
2078 * of aff where it is non-negative.
2079 * If "rational" is set, then return a rational basic set.
2081 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2083 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2084 __isl_take isl_aff *aff, int rational)
2086 isl_constraint *ineq;
2087 isl_basic_set *bset;
2089 if (!aff)
2090 return NULL;
2091 if (isl_aff_is_nan(aff)) {
2092 isl_space *space = isl_aff_get_domain_space(aff);
2093 isl_aff_free(aff);
2094 return isl_basic_set_empty(space);
2097 ineq = isl_inequality_from_aff(aff);
2099 bset = isl_basic_set_from_constraint(ineq);
2100 if (rational)
2101 bset = isl_basic_set_set_rational(bset);
2102 bset = isl_basic_set_simplify(bset);
2103 return bset;
2106 /* Return a basic set containing those elements in the space
2107 * of aff where it is non-negative.
2109 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2111 return aff_nonneg_basic_set(aff, 0);
2114 /* Return a basic set containing those elements in the domain space
2115 * of "aff" where it is positive.
2117 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2119 aff = isl_aff_add_constant_num_si(aff, -1);
2120 return isl_aff_nonneg_basic_set(aff);
2123 /* Return a basic set containing those elements in the domain space
2124 * of aff where it is negative.
2126 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2128 aff = isl_aff_neg(aff);
2129 return isl_aff_pos_basic_set(aff);
2132 /* Return a basic set containing those elements in the space
2133 * of aff where it is zero.
2134 * If "rational" is set, then return a rational basic set.
2136 * If "aff" is NaN, then it is not zero.
2138 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2139 int rational)
2141 isl_constraint *ineq;
2142 isl_basic_set *bset;
2144 if (!aff)
2145 return NULL;
2146 if (isl_aff_is_nan(aff)) {
2147 isl_space *space = isl_aff_get_domain_space(aff);
2148 isl_aff_free(aff);
2149 return isl_basic_set_empty(space);
2152 ineq = isl_equality_from_aff(aff);
2154 bset = isl_basic_set_from_constraint(ineq);
2155 if (rational)
2156 bset = isl_basic_set_set_rational(bset);
2157 bset = isl_basic_set_simplify(bset);
2158 return bset;
2161 /* Return a basic set containing those elements in the space
2162 * of aff where it is zero.
2164 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2166 return aff_zero_basic_set(aff, 0);
2169 /* Return a basic set containing those elements in the shared space
2170 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2172 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2173 __isl_take isl_aff *aff2)
2175 aff1 = isl_aff_sub(aff1, aff2);
2177 return isl_aff_nonneg_basic_set(aff1);
2180 /* Return a basic set containing those elements in the shared domain space
2181 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2183 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2184 __isl_take isl_aff *aff2)
2186 aff1 = isl_aff_sub(aff1, aff2);
2188 return isl_aff_pos_basic_set(aff1);
2191 /* Return a set containing those elements in the shared space
2192 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2194 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2195 __isl_take isl_aff *aff2)
2197 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2200 /* Return a set containing those elements in the shared domain space
2201 * of aff1 and aff2 where aff1 is greater than aff2.
2203 * If either of the two inputs is NaN, then the result is empty,
2204 * as comparisons with NaN always return false.
2206 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2207 __isl_take isl_aff *aff2)
2209 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2212 /* Return a basic set containing those elements in the shared space
2213 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2215 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2216 __isl_take isl_aff *aff2)
2218 return isl_aff_ge_basic_set(aff2, aff1);
2221 /* Return a basic set containing those elements in the shared domain space
2222 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2224 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2225 __isl_take isl_aff *aff2)
2227 return isl_aff_gt_basic_set(aff2, aff1);
2230 /* Return a set containing those elements in the shared space
2231 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2233 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2234 __isl_take isl_aff *aff2)
2236 return isl_aff_ge_set(aff2, aff1);
2239 /* Return a set containing those elements in the shared domain space
2240 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2242 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2243 __isl_take isl_aff *aff2)
2245 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2248 /* Return a basic set containing those elements in the shared space
2249 * of aff1 and aff2 where aff1 and aff2 are equal.
2251 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2252 __isl_take isl_aff *aff2)
2254 aff1 = isl_aff_sub(aff1, aff2);
2256 return isl_aff_zero_basic_set(aff1);
2259 /* Return a set containing those elements in the shared space
2260 * of aff1 and aff2 where aff1 and aff2 are equal.
2262 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2263 __isl_take isl_aff *aff2)
2265 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2268 /* Return a set containing those elements in the shared domain space
2269 * of aff1 and aff2 where aff1 and aff2 are not equal.
2271 * If either of the two inputs is NaN, then the result is empty,
2272 * as comparisons with NaN always return false.
2274 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2275 __isl_take isl_aff *aff2)
2277 isl_set *set_lt, *set_gt;
2279 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2280 isl_aff_copy(aff2));
2281 set_gt = isl_aff_gt_set(aff1, aff2);
2282 return isl_set_union_disjoint(set_lt, set_gt);
2285 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2286 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2288 aff1 = isl_aff_add(aff1, aff2);
2289 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2290 return aff1;
2293 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2295 if (!aff)
2296 return -1;
2298 return 0;
2301 /* Check whether the given affine expression has non-zero coefficient
2302 * for any dimension in the given range or if any of these dimensions
2303 * appear with non-zero coefficients in any of the integer divisions
2304 * involved in the affine expression.
2306 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2307 enum isl_dim_type type, unsigned first, unsigned n)
2309 int i;
2310 isl_ctx *ctx;
2311 int *active = NULL;
2312 isl_bool involves = isl_bool_false;
2314 if (!aff)
2315 return isl_bool_error;
2316 if (n == 0)
2317 return isl_bool_false;
2319 ctx = isl_aff_get_ctx(aff);
2320 if (first + n > isl_aff_dim(aff, type))
2321 isl_die(ctx, isl_error_invalid,
2322 "range out of bounds", return isl_bool_error);
2324 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2325 if (!active)
2326 goto error;
2328 first += isl_local_space_offset(aff->ls, type) - 1;
2329 for (i = 0; i < n; ++i)
2330 if (active[first + i]) {
2331 involves = isl_bool_true;
2332 break;
2335 free(active);
2337 return involves;
2338 error:
2339 free(active);
2340 return isl_bool_error;
2343 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2344 enum isl_dim_type type, unsigned first, unsigned n)
2346 isl_ctx *ctx;
2348 if (!aff)
2349 return NULL;
2350 if (type == isl_dim_out)
2351 isl_die(aff->v->ctx, isl_error_invalid,
2352 "cannot drop output/set dimension",
2353 return isl_aff_free(aff));
2354 if (type == isl_dim_in)
2355 type = isl_dim_set;
2356 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2357 return aff;
2359 ctx = isl_aff_get_ctx(aff);
2360 if (first + n > isl_local_space_dim(aff->ls, type))
2361 isl_die(ctx, isl_error_invalid, "range out of bounds",
2362 return isl_aff_free(aff));
2364 aff = isl_aff_cow(aff);
2365 if (!aff)
2366 return NULL;
2368 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2369 if (!aff->ls)
2370 return isl_aff_free(aff);
2372 first += 1 + isl_local_space_offset(aff->ls, type);
2373 aff->v = isl_vec_drop_els(aff->v, first, n);
2374 if (!aff->v)
2375 return isl_aff_free(aff);
2377 return aff;
2380 /* Project the domain of the affine expression onto its parameter space.
2381 * The affine expression may not involve any of the domain dimensions.
2383 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2385 isl_space *space;
2386 unsigned n;
2387 int involves;
2389 n = isl_aff_dim(aff, isl_dim_in);
2390 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2391 if (involves < 0)
2392 return isl_aff_free(aff);
2393 if (involves)
2394 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2395 "affine expression involves some of the domain dimensions",
2396 return isl_aff_free(aff));
2397 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2398 space = isl_aff_get_domain_space(aff);
2399 space = isl_space_params(space);
2400 aff = isl_aff_reset_domain_space(aff, space);
2401 return aff;
2404 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2405 enum isl_dim_type type, unsigned first, unsigned n)
2407 isl_ctx *ctx;
2409 if (!aff)
2410 return NULL;
2411 if (type == isl_dim_out)
2412 isl_die(aff->v->ctx, isl_error_invalid,
2413 "cannot insert output/set dimensions",
2414 return isl_aff_free(aff));
2415 if (type == isl_dim_in)
2416 type = isl_dim_set;
2417 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2418 return aff;
2420 ctx = isl_aff_get_ctx(aff);
2421 if (first > isl_local_space_dim(aff->ls, type))
2422 isl_die(ctx, isl_error_invalid, "position out of bounds",
2423 return isl_aff_free(aff));
2425 aff = isl_aff_cow(aff);
2426 if (!aff)
2427 return NULL;
2429 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2430 if (!aff->ls)
2431 return isl_aff_free(aff);
2433 first += 1 + isl_local_space_offset(aff->ls, type);
2434 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2435 if (!aff->v)
2436 return isl_aff_free(aff);
2438 return aff;
2441 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2442 enum isl_dim_type type, unsigned n)
2444 unsigned pos;
2446 pos = isl_aff_dim(aff, type);
2448 return isl_aff_insert_dims(aff, type, pos, n);
2451 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2452 enum isl_dim_type type, unsigned n)
2454 unsigned pos;
2456 pos = isl_pw_aff_dim(pwaff, type);
2458 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2461 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2462 * to dimensions of "dst_type" at "dst_pos".
2464 * We only support moving input dimensions to parameters and vice versa.
2466 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2467 enum isl_dim_type dst_type, unsigned dst_pos,
2468 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2470 unsigned g_dst_pos;
2471 unsigned g_src_pos;
2473 if (!aff)
2474 return NULL;
2475 if (n == 0 &&
2476 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2477 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2478 return aff;
2480 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2481 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2482 "cannot move output/set dimension",
2483 return isl_aff_free(aff));
2484 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2485 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2486 "cannot move divs", return isl_aff_free(aff));
2487 if (dst_type == isl_dim_in)
2488 dst_type = isl_dim_set;
2489 if (src_type == isl_dim_in)
2490 src_type = isl_dim_set;
2492 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2493 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2494 "range out of bounds", return isl_aff_free(aff));
2495 if (dst_type == src_type)
2496 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2497 "moving dims within the same type not supported",
2498 return isl_aff_free(aff));
2500 aff = isl_aff_cow(aff);
2501 if (!aff)
2502 return NULL;
2504 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2505 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2506 if (dst_type > src_type)
2507 g_dst_pos -= n;
2509 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2510 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2511 src_type, src_pos, n);
2512 if (!aff->v || !aff->ls)
2513 return isl_aff_free(aff);
2515 aff = sort_divs(aff);
2517 return aff;
2520 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2522 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2523 return isl_pw_aff_alloc(dom, aff);
2526 #define isl_aff_involves_nan isl_aff_is_nan
2528 #undef PW
2529 #define PW isl_pw_aff
2530 #undef EL
2531 #define EL isl_aff
2532 #undef EL_IS_ZERO
2533 #define EL_IS_ZERO is_empty
2534 #undef ZERO
2535 #define ZERO empty
2536 #undef IS_ZERO
2537 #define IS_ZERO is_empty
2538 #undef FIELD
2539 #define FIELD aff
2540 #undef DEFAULT_IS_ZERO
2541 #define DEFAULT_IS_ZERO 0
2543 #define NO_EVAL
2544 #define NO_OPT
2545 #define NO_LIFT
2546 #define NO_MORPH
2548 #include <isl_pw_templ.c>
2549 #include <isl_pw_hash.c>
2550 #include <isl_pw_union_opt.c>
2552 #undef UNION
2553 #define UNION isl_union_pw_aff
2554 #undef PART
2555 #define PART isl_pw_aff
2556 #undef PARTS
2557 #define PARTS pw_aff
2559 #include <isl_union_single.c>
2560 #include <isl_union_neg.c>
2562 static __isl_give isl_set *align_params_pw_pw_set_and(
2563 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2564 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2565 __isl_take isl_pw_aff *pwaff2))
2567 isl_bool equal_params;
2569 if (!pwaff1 || !pwaff2)
2570 goto error;
2571 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2572 if (equal_params < 0)
2573 goto error;
2574 if (equal_params)
2575 return fn(pwaff1, pwaff2);
2576 if (!isl_space_has_named_params(pwaff1->dim) ||
2577 !isl_space_has_named_params(pwaff2->dim))
2578 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2579 "unaligned unnamed parameters", goto error);
2580 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2581 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2582 return fn(pwaff1, pwaff2);
2583 error:
2584 isl_pw_aff_free(pwaff1);
2585 isl_pw_aff_free(pwaff2);
2586 return NULL;
2589 /* Align the parameters of the to isl_pw_aff arguments and
2590 * then apply a function "fn" on them that returns an isl_map.
2592 static __isl_give isl_map *align_params_pw_pw_map_and(
2593 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2594 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2595 __isl_take isl_pw_aff *pa2))
2597 isl_bool equal_params;
2599 if (!pa1 || !pa2)
2600 goto error;
2601 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2602 if (equal_params < 0)
2603 goto error;
2604 if (equal_params)
2605 return fn(pa1, pa2);
2606 if (!isl_space_has_named_params(pa1->dim) ||
2607 !isl_space_has_named_params(pa2->dim))
2608 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2609 "unaligned unnamed parameters", goto error);
2610 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2611 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2612 return fn(pa1, pa2);
2613 error:
2614 isl_pw_aff_free(pa1);
2615 isl_pw_aff_free(pa2);
2616 return NULL;
2619 /* Compute a piecewise quasi-affine expression with a domain that
2620 * is the union of those of pwaff1 and pwaff2 and such that on each
2621 * cell, the quasi-affine expression is the maximum of those of pwaff1
2622 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2623 * cell, then the associated expression is the defined one.
2625 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2626 __isl_take isl_pw_aff *pwaff2)
2628 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2631 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2632 __isl_take isl_pw_aff *pwaff2)
2634 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2635 &pw_aff_union_max);
2638 /* Compute a piecewise quasi-affine expression with a domain that
2639 * is the union of those of pwaff1 and pwaff2 and such that on each
2640 * cell, the quasi-affine expression is the minimum of those of pwaff1
2641 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2642 * cell, then the associated expression is the defined one.
2644 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2645 __isl_take isl_pw_aff *pwaff2)
2647 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2650 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2651 __isl_take isl_pw_aff *pwaff2)
2653 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2654 &pw_aff_union_min);
2657 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2658 __isl_take isl_pw_aff *pwaff2, int max)
2660 if (max)
2661 return isl_pw_aff_union_max(pwaff1, pwaff2);
2662 else
2663 return isl_pw_aff_union_min(pwaff1, pwaff2);
2666 /* Construct a map with as domain the domain of pwaff and
2667 * one-dimensional range corresponding to the affine expressions.
2669 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2671 int i;
2672 isl_space *dim;
2673 isl_map *map;
2675 if (!pwaff)
2676 return NULL;
2678 dim = isl_pw_aff_get_space(pwaff);
2679 map = isl_map_empty(dim);
2681 for (i = 0; i < pwaff->n; ++i) {
2682 isl_basic_map *bmap;
2683 isl_map *map_i;
2685 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2686 map_i = isl_map_from_basic_map(bmap);
2687 map_i = isl_map_intersect_domain(map_i,
2688 isl_set_copy(pwaff->p[i].set));
2689 map = isl_map_union_disjoint(map, map_i);
2692 isl_pw_aff_free(pwaff);
2694 return map;
2697 /* Construct a map with as domain the domain of pwaff and
2698 * one-dimensional range corresponding to the affine expressions.
2700 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2702 if (!pwaff)
2703 return NULL;
2704 if (isl_space_is_set(pwaff->dim))
2705 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2706 "space of input is not a map", goto error);
2707 return map_from_pw_aff(pwaff);
2708 error:
2709 isl_pw_aff_free(pwaff);
2710 return NULL;
2713 /* Construct a one-dimensional set with as parameter domain
2714 * the domain of pwaff and the single set dimension
2715 * corresponding to the affine expressions.
2717 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2719 if (!pwaff)
2720 return NULL;
2721 if (!isl_space_is_set(pwaff->dim))
2722 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2723 "space of input is not a set", goto error);
2724 return map_from_pw_aff(pwaff);
2725 error:
2726 isl_pw_aff_free(pwaff);
2727 return NULL;
2730 /* Return a set containing those elements in the domain
2731 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2732 * does not satisfy "fn" (if complement is 1).
2734 * The pieces with a NaN never belong to the result since
2735 * NaN does not satisfy any property.
2737 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2738 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2739 int complement)
2741 int i;
2742 isl_set *set;
2744 if (!pwaff)
2745 return NULL;
2747 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2749 for (i = 0; i < pwaff->n; ++i) {
2750 isl_basic_set *bset;
2751 isl_set *set_i, *locus;
2752 isl_bool rational;
2754 if (isl_aff_is_nan(pwaff->p[i].aff))
2755 continue;
2757 rational = isl_set_has_rational(pwaff->p[i].set);
2758 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2759 locus = isl_set_from_basic_set(bset);
2760 set_i = isl_set_copy(pwaff->p[i].set);
2761 if (complement)
2762 set_i = isl_set_subtract(set_i, locus);
2763 else
2764 set_i = isl_set_intersect(set_i, locus);
2765 set = isl_set_union_disjoint(set, set_i);
2768 isl_pw_aff_free(pwaff);
2770 return set;
2773 /* Return a set containing those elements in the domain
2774 * of "pa" where it is positive.
2776 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2778 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2781 /* Return a set containing those elements in the domain
2782 * of pwaff where it is non-negative.
2784 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2786 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2789 /* Return a set containing those elements in the domain
2790 * of pwaff where it is zero.
2792 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2794 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2797 /* Return a set containing those elements in the domain
2798 * of pwaff where it is not zero.
2800 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2802 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2805 /* Return a set containing those elements in the shared domain
2806 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2808 * We compute the difference on the shared domain and then construct
2809 * the set of values where this difference is non-negative.
2810 * If strict is set, we first subtract 1 from the difference.
2811 * If equal is set, we only return the elements where pwaff1 and pwaff2
2812 * are equal.
2814 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2815 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2817 isl_set *set1, *set2;
2819 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2820 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2821 set1 = isl_set_intersect(set1, set2);
2822 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2823 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2824 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2826 if (strict) {
2827 isl_space *dim = isl_set_get_space(set1);
2828 isl_aff *aff;
2829 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2830 aff = isl_aff_add_constant_si(aff, -1);
2831 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2832 } else
2833 isl_set_free(set1);
2835 if (equal)
2836 return isl_pw_aff_zero_set(pwaff1);
2837 return isl_pw_aff_nonneg_set(pwaff1);
2840 /* Return a set containing those elements in the shared domain
2841 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2843 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2844 __isl_take isl_pw_aff *pwaff2)
2846 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2849 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2850 __isl_take isl_pw_aff *pwaff2)
2852 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2855 /* Return a set containing those elements in the shared domain
2856 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2858 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2859 __isl_take isl_pw_aff *pwaff2)
2861 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2864 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2865 __isl_take isl_pw_aff *pwaff2)
2867 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2870 /* Return a set containing those elements in the shared domain
2871 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2873 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2874 __isl_take isl_pw_aff *pwaff2)
2876 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2879 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2880 __isl_take isl_pw_aff *pwaff2)
2882 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2885 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2886 __isl_take isl_pw_aff *pwaff2)
2888 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2891 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2892 __isl_take isl_pw_aff *pwaff2)
2894 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2897 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2898 * where the function values are ordered in the same way as "order",
2899 * which returns a set in the shared domain of its two arguments.
2900 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2902 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2903 * We first pull back the two functions such that they are defined on
2904 * the domain [A -> B]. Then we apply "order", resulting in a set
2905 * in the space [A -> B]. Finally, we unwrap this set to obtain
2906 * a map in the space A -> B.
2908 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2909 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2910 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2911 __isl_take isl_pw_aff *pa2))
2913 isl_space *space1, *space2;
2914 isl_multi_aff *ma;
2915 isl_set *set;
2917 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2918 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2919 space1 = isl_space_map_from_domain_and_range(space1, space2);
2920 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2921 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2922 ma = isl_multi_aff_range_map(space1);
2923 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2924 set = order(pa1, pa2);
2926 return isl_set_unwrap(set);
2929 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2930 * where the function values are equal.
2931 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2933 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
2934 __isl_take isl_pw_aff *pa2)
2936 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
2939 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2940 * where the function values are equal.
2942 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
2943 __isl_take isl_pw_aff *pa2)
2945 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
2948 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2949 * where the function value of "pa1" is less than the function value of "pa2".
2950 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2952 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
2953 __isl_take isl_pw_aff *pa2)
2955 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
2958 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2959 * where the function value of "pa1" is less than the function value of "pa2".
2961 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
2962 __isl_take isl_pw_aff *pa2)
2964 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
2967 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2968 * where the function value of "pa1" is greater than the function value
2969 * of "pa2".
2970 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2972 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
2973 __isl_take isl_pw_aff *pa2)
2975 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
2978 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2979 * where the function value of "pa1" is greater than the function value
2980 * of "pa2".
2982 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
2983 __isl_take isl_pw_aff *pa2)
2985 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
2988 /* Return a set containing those elements in the shared domain
2989 * of the elements of list1 and list2 where each element in list1
2990 * has the relation specified by "fn" with each element in list2.
2992 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
2993 __isl_take isl_pw_aff_list *list2,
2994 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2995 __isl_take isl_pw_aff *pwaff2))
2997 int i, j;
2998 isl_ctx *ctx;
2999 isl_set *set;
3001 if (!list1 || !list2)
3002 goto error;
3004 ctx = isl_pw_aff_list_get_ctx(list1);
3005 if (list1->n < 1 || list2->n < 1)
3006 isl_die(ctx, isl_error_invalid,
3007 "list should contain at least one element", goto error);
3009 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3010 for (i = 0; i < list1->n; ++i)
3011 for (j = 0; j < list2->n; ++j) {
3012 isl_set *set_ij;
3014 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3015 isl_pw_aff_copy(list2->p[j]));
3016 set = isl_set_intersect(set, set_ij);
3019 isl_pw_aff_list_free(list1);
3020 isl_pw_aff_list_free(list2);
3021 return set;
3022 error:
3023 isl_pw_aff_list_free(list1);
3024 isl_pw_aff_list_free(list2);
3025 return NULL;
3028 /* Return a set containing those elements in the shared domain
3029 * of the elements of list1 and list2 where each element in list1
3030 * is equal to each element in list2.
3032 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3033 __isl_take isl_pw_aff_list *list2)
3035 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3038 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3039 __isl_take isl_pw_aff_list *list2)
3041 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3044 /* Return a set containing those elements in the shared domain
3045 * of the elements of list1 and list2 where each element in list1
3046 * is less than or equal to each element in list2.
3048 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3049 __isl_take isl_pw_aff_list *list2)
3051 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3054 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3055 __isl_take isl_pw_aff_list *list2)
3057 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3060 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3061 __isl_take isl_pw_aff_list *list2)
3063 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3066 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3067 __isl_take isl_pw_aff_list *list2)
3069 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3073 /* Return a set containing those elements in the shared domain
3074 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3076 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3077 __isl_take isl_pw_aff *pwaff2)
3079 isl_set *set_lt, *set_gt;
3081 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3082 isl_pw_aff_copy(pwaff2));
3083 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3084 return isl_set_union_disjoint(set_lt, set_gt);
3087 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3088 __isl_take isl_pw_aff *pwaff2)
3090 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3093 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3094 isl_int v)
3096 int i;
3098 if (isl_int_is_one(v))
3099 return pwaff;
3100 if (!isl_int_is_pos(v))
3101 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3102 "factor needs to be positive",
3103 return isl_pw_aff_free(pwaff));
3104 pwaff = isl_pw_aff_cow(pwaff);
3105 if (!pwaff)
3106 return NULL;
3107 if (pwaff->n == 0)
3108 return pwaff;
3110 for (i = 0; i < pwaff->n; ++i) {
3111 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3112 if (!pwaff->p[i].aff)
3113 return isl_pw_aff_free(pwaff);
3116 return pwaff;
3119 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3121 int i;
3123 pwaff = isl_pw_aff_cow(pwaff);
3124 if (!pwaff)
3125 return NULL;
3126 if (pwaff->n == 0)
3127 return pwaff;
3129 for (i = 0; i < pwaff->n; ++i) {
3130 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3131 if (!pwaff->p[i].aff)
3132 return isl_pw_aff_free(pwaff);
3135 return pwaff;
3138 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3140 int i;
3142 pwaff = isl_pw_aff_cow(pwaff);
3143 if (!pwaff)
3144 return NULL;
3145 if (pwaff->n == 0)
3146 return pwaff;
3148 for (i = 0; i < pwaff->n; ++i) {
3149 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3150 if (!pwaff->p[i].aff)
3151 return isl_pw_aff_free(pwaff);
3154 return pwaff;
3157 /* Assuming that "cond1" and "cond2" are disjoint,
3158 * return an affine expression that is equal to pwaff1 on cond1
3159 * and to pwaff2 on cond2.
3161 static __isl_give isl_pw_aff *isl_pw_aff_select(
3162 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3163 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3165 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3166 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3168 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3171 /* Return an affine expression that is equal to pwaff_true for elements
3172 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3173 * is zero.
3174 * That is, return cond ? pwaff_true : pwaff_false;
3176 * If "cond" involves and NaN, then we conservatively return a NaN
3177 * on its entire domain. In principle, we could consider the pieces
3178 * where it is NaN separately from those where it is not.
3180 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3181 * then only use the domain of "cond" to restrict the domain.
3183 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3184 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3186 isl_set *cond_true, *cond_false;
3187 isl_bool equal;
3189 if (!cond)
3190 goto error;
3191 if (isl_pw_aff_involves_nan(cond)) {
3192 isl_space *space = isl_pw_aff_get_domain_space(cond);
3193 isl_local_space *ls = isl_local_space_from_space(space);
3194 isl_pw_aff_free(cond);
3195 isl_pw_aff_free(pwaff_true);
3196 isl_pw_aff_free(pwaff_false);
3197 return isl_pw_aff_nan_on_domain(ls);
3200 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3201 isl_pw_aff_get_space(pwaff_false));
3202 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3203 isl_pw_aff_get_space(pwaff_true));
3204 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3205 if (equal < 0)
3206 goto error;
3207 if (equal) {
3208 isl_set *dom;
3210 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3211 isl_pw_aff_free(pwaff_false);
3212 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3215 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3216 cond_false = isl_pw_aff_zero_set(cond);
3217 return isl_pw_aff_select(cond_true, pwaff_true,
3218 cond_false, pwaff_false);
3219 error:
3220 isl_pw_aff_free(cond);
3221 isl_pw_aff_free(pwaff_true);
3222 isl_pw_aff_free(pwaff_false);
3223 return NULL;
3226 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3228 if (!aff)
3229 return isl_bool_error;
3231 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3234 /* Check whether pwaff is a piecewise constant.
3236 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3238 int i;
3240 if (!pwaff)
3241 return isl_bool_error;
3243 for (i = 0; i < pwaff->n; ++i) {
3244 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3245 if (is_cst < 0 || !is_cst)
3246 return is_cst;
3249 return isl_bool_true;
3252 /* Are all elements of "mpa" piecewise constants?
3254 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3256 int i;
3258 if (!mpa)
3259 return isl_bool_error;
3261 for (i = 0; i < mpa->n; ++i) {
3262 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3263 if (is_cst < 0 || !is_cst)
3264 return is_cst;
3267 return isl_bool_true;
3270 /* Return the product of "aff1" and "aff2".
3272 * If either of the two is NaN, then the result is NaN.
3274 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3276 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3277 __isl_take isl_aff *aff2)
3279 if (!aff1 || !aff2)
3280 goto error;
3282 if (isl_aff_is_nan(aff1)) {
3283 isl_aff_free(aff2);
3284 return aff1;
3286 if (isl_aff_is_nan(aff2)) {
3287 isl_aff_free(aff1);
3288 return aff2;
3291 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3292 return isl_aff_mul(aff2, aff1);
3294 if (!isl_aff_is_cst(aff2))
3295 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3296 "at least one affine expression should be constant",
3297 goto error);
3299 aff1 = isl_aff_cow(aff1);
3300 if (!aff1 || !aff2)
3301 goto error;
3303 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3304 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3306 isl_aff_free(aff2);
3307 return aff1;
3308 error:
3309 isl_aff_free(aff1);
3310 isl_aff_free(aff2);
3311 return NULL;
3314 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3316 * If either of the two is NaN, then the result is NaN.
3318 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3319 __isl_take isl_aff *aff2)
3321 int is_cst;
3322 int neg;
3324 if (!aff1 || !aff2)
3325 goto error;
3327 if (isl_aff_is_nan(aff1)) {
3328 isl_aff_free(aff2);
3329 return aff1;
3331 if (isl_aff_is_nan(aff2)) {
3332 isl_aff_free(aff1);
3333 return aff2;
3336 is_cst = isl_aff_is_cst(aff2);
3337 if (is_cst < 0)
3338 goto error;
3339 if (!is_cst)
3340 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3341 "second argument should be a constant", goto error);
3343 if (!aff2)
3344 goto error;
3346 neg = isl_int_is_neg(aff2->v->el[1]);
3347 if (neg) {
3348 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3349 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3352 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3353 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3355 if (neg) {
3356 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3357 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3360 isl_aff_free(aff2);
3361 return aff1;
3362 error:
3363 isl_aff_free(aff1);
3364 isl_aff_free(aff2);
3365 return NULL;
3368 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3369 __isl_take isl_pw_aff *pwaff2)
3371 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3374 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3375 __isl_take isl_pw_aff *pwaff2)
3377 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3380 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3381 __isl_take isl_pw_aff *pwaff2)
3383 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3386 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3387 __isl_take isl_pw_aff *pwaff2)
3389 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3392 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3393 __isl_take isl_pw_aff *pwaff2)
3395 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3398 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3399 __isl_take isl_pw_aff *pa2)
3401 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3404 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3406 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3407 __isl_take isl_pw_aff *pa2)
3409 int is_cst;
3411 is_cst = isl_pw_aff_is_cst(pa2);
3412 if (is_cst < 0)
3413 goto error;
3414 if (!is_cst)
3415 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3416 "second argument should be a piecewise constant",
3417 goto error);
3418 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3419 error:
3420 isl_pw_aff_free(pa1);
3421 isl_pw_aff_free(pa2);
3422 return NULL;
3425 /* Compute the quotient of the integer division of "pa1" by "pa2"
3426 * with rounding towards zero.
3427 * "pa2" is assumed to be a piecewise constant.
3429 * In particular, return
3431 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3434 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3435 __isl_take isl_pw_aff *pa2)
3437 int is_cst;
3438 isl_set *cond;
3439 isl_pw_aff *f, *c;
3441 is_cst = isl_pw_aff_is_cst(pa2);
3442 if (is_cst < 0)
3443 goto error;
3444 if (!is_cst)
3445 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3446 "second argument should be a piecewise constant",
3447 goto error);
3449 pa1 = isl_pw_aff_div(pa1, pa2);
3451 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3452 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3453 c = isl_pw_aff_ceil(pa1);
3454 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3455 error:
3456 isl_pw_aff_free(pa1);
3457 isl_pw_aff_free(pa2);
3458 return NULL;
3461 /* Compute the remainder of the integer division of "pa1" by "pa2"
3462 * with rounding towards zero.
3463 * "pa2" is assumed to be a piecewise constant.
3465 * In particular, return
3467 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3470 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3471 __isl_take isl_pw_aff *pa2)
3473 int is_cst;
3474 isl_pw_aff *res;
3476 is_cst = isl_pw_aff_is_cst(pa2);
3477 if (is_cst < 0)
3478 goto error;
3479 if (!is_cst)
3480 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3481 "second argument should be a piecewise constant",
3482 goto error);
3483 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3484 res = isl_pw_aff_mul(pa2, res);
3485 res = isl_pw_aff_sub(pa1, res);
3486 return res;
3487 error:
3488 isl_pw_aff_free(pa1);
3489 isl_pw_aff_free(pa2);
3490 return NULL;
3493 /* Does either of "pa1" or "pa2" involve any NaN2?
3495 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3496 __isl_keep isl_pw_aff *pa2)
3498 isl_bool has_nan;
3500 has_nan = isl_pw_aff_involves_nan(pa1);
3501 if (has_nan < 0 || has_nan)
3502 return has_nan;
3503 return isl_pw_aff_involves_nan(pa2);
3506 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3507 * by a NaN on their shared domain.
3509 * In principle, the result could be refined to only being NaN
3510 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3512 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3513 __isl_take isl_pw_aff *pa2)
3515 isl_local_space *ls;
3516 isl_set *dom;
3517 isl_pw_aff *pa;
3519 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3520 ls = isl_local_space_from_space(isl_set_get_space(dom));
3521 pa = isl_pw_aff_nan_on_domain(ls);
3522 pa = isl_pw_aff_intersect_domain(pa, dom);
3524 return pa;
3527 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3528 __isl_take isl_pw_aff *pwaff2)
3530 isl_set *le;
3531 isl_set *dom;
3533 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3534 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3535 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3536 isl_pw_aff_copy(pwaff2));
3537 dom = isl_set_subtract(dom, isl_set_copy(le));
3538 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3541 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3542 __isl_take isl_pw_aff *pwaff2)
3544 isl_set *ge;
3545 isl_set *dom;
3547 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3548 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3549 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3550 isl_pw_aff_copy(pwaff2));
3551 dom = isl_set_subtract(dom, isl_set_copy(ge));
3552 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3555 /* Return an expression for the minimum (if "max" is not set) or
3556 * the maximum (if "max" is set) of "pa1" and "pa2".
3557 * If either expression involves any NaN, then return a NaN
3558 * on the shared domain as result.
3560 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3561 __isl_take isl_pw_aff *pa2, int max)
3563 isl_bool has_nan;
3565 has_nan = either_involves_nan(pa1, pa2);
3566 if (has_nan < 0)
3567 pa1 = isl_pw_aff_free(pa1);
3568 else if (has_nan)
3569 return replace_by_nan(pa1, pa2);
3571 if (max)
3572 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3573 else
3574 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3577 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3579 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3580 __isl_take isl_pw_aff *pwaff2)
3582 return pw_aff_min_max(pwaff1, pwaff2, 0);
3585 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3587 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3588 __isl_take isl_pw_aff *pwaff2)
3590 return pw_aff_min_max(pwaff1, pwaff2, 1);
3593 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3594 __isl_take isl_pw_aff_list *list,
3595 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3596 __isl_take isl_pw_aff *pwaff2))
3598 int i;
3599 isl_ctx *ctx;
3600 isl_pw_aff *res;
3602 if (!list)
3603 return NULL;
3605 ctx = isl_pw_aff_list_get_ctx(list);
3606 if (list->n < 1)
3607 isl_die(ctx, isl_error_invalid,
3608 "list should contain at least one element", goto error);
3610 res = isl_pw_aff_copy(list->p[0]);
3611 for (i = 1; i < list->n; ++i)
3612 res = fn(res, isl_pw_aff_copy(list->p[i]));
3614 isl_pw_aff_list_free(list);
3615 return res;
3616 error:
3617 isl_pw_aff_list_free(list);
3618 return NULL;
3621 /* Return an isl_pw_aff that maps each element in the intersection of the
3622 * domains of the elements of list to the minimal corresponding affine
3623 * expression.
3625 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3627 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3630 /* Return an isl_pw_aff that maps each element in the intersection of the
3631 * domains of the elements of list to the maximal corresponding affine
3632 * expression.
3634 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3636 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3639 /* Mark the domains of "pwaff" as rational.
3641 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3643 int i;
3645 pwaff = isl_pw_aff_cow(pwaff);
3646 if (!pwaff)
3647 return NULL;
3648 if (pwaff->n == 0)
3649 return pwaff;
3651 for (i = 0; i < pwaff->n; ++i) {
3652 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3653 if (!pwaff->p[i].set)
3654 return isl_pw_aff_free(pwaff);
3657 return pwaff;
3660 /* Mark the domains of the elements of "list" as rational.
3662 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3663 __isl_take isl_pw_aff_list *list)
3665 int i, n;
3667 if (!list)
3668 return NULL;
3669 if (list->n == 0)
3670 return list;
3672 n = list->n;
3673 for (i = 0; i < n; ++i) {
3674 isl_pw_aff *pa;
3676 pa = isl_pw_aff_list_get_pw_aff(list, i);
3677 pa = isl_pw_aff_set_rational(pa);
3678 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3681 return list;
3684 /* Do the parameters of "aff" match those of "space"?
3686 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3687 __isl_keep isl_space *space)
3689 isl_space *aff_space;
3690 isl_bool match;
3692 if (!aff || !space)
3693 return isl_bool_error;
3695 aff_space = isl_aff_get_domain_space(aff);
3697 match = isl_space_has_equal_params(space, aff_space);
3699 isl_space_free(aff_space);
3700 return match;
3703 /* Check that the domain space of "aff" matches "space".
3705 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3706 __isl_keep isl_space *space)
3708 isl_space *aff_space;
3709 isl_bool match;
3711 if (!aff || !space)
3712 return isl_stat_error;
3714 aff_space = isl_aff_get_domain_space(aff);
3716 match = isl_space_has_equal_params(space, aff_space);
3717 if (match < 0)
3718 goto error;
3719 if (!match)
3720 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3721 "parameters don't match", goto error);
3722 match = isl_space_tuple_is_equal(space, isl_dim_in,
3723 aff_space, isl_dim_set);
3724 if (match < 0)
3725 goto error;
3726 if (!match)
3727 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3728 "domains don't match", goto error);
3729 isl_space_free(aff_space);
3730 return isl_stat_ok;
3731 error:
3732 isl_space_free(aff_space);
3733 return isl_stat_error;
3736 #undef BASE
3737 #define BASE aff
3738 #undef DOMBASE
3739 #define DOMBASE set
3740 #define NO_DOMAIN
3742 #include <isl_multi_templ.c>
3743 #include <isl_multi_apply_set.c>
3744 #include <isl_multi_cmp.c>
3745 #include <isl_multi_dims.c>
3746 #include <isl_multi_floor.c>
3747 #include <isl_multi_gist.c>
3749 #undef NO_DOMAIN
3751 /* Construct an isl_multi_aff living in "space" that corresponds
3752 * to the affine transformation matrix "mat".
3754 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3755 __isl_take isl_space *space, __isl_take isl_mat *mat)
3757 isl_ctx *ctx;
3758 isl_local_space *ls = NULL;
3759 isl_multi_aff *ma = NULL;
3760 int n_row, n_col, n_out, total;
3761 int i;
3763 if (!space || !mat)
3764 goto error;
3766 ctx = isl_mat_get_ctx(mat);
3768 n_row = isl_mat_rows(mat);
3769 n_col = isl_mat_cols(mat);
3770 if (n_row < 1)
3771 isl_die(ctx, isl_error_invalid,
3772 "insufficient number of rows", goto error);
3773 if (n_col < 1)
3774 isl_die(ctx, isl_error_invalid,
3775 "insufficient number of columns", goto error);
3776 n_out = isl_space_dim(space, isl_dim_out);
3777 total = isl_space_dim(space, isl_dim_all);
3778 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3779 isl_die(ctx, isl_error_invalid,
3780 "dimension mismatch", goto error);
3782 ma = isl_multi_aff_zero(isl_space_copy(space));
3783 ls = isl_local_space_from_space(isl_space_domain(space));
3785 for (i = 0; i < n_row - 1; ++i) {
3786 isl_vec *v;
3787 isl_aff *aff;
3789 v = isl_vec_alloc(ctx, 1 + n_col);
3790 if (!v)
3791 goto error;
3792 isl_int_set(v->el[0], mat->row[0][0]);
3793 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3794 v = isl_vec_normalize(v);
3795 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3796 ma = isl_multi_aff_set_aff(ma, i, aff);
3799 isl_local_space_free(ls);
3800 isl_mat_free(mat);
3801 return ma;
3802 error:
3803 isl_local_space_free(ls);
3804 isl_mat_free(mat);
3805 isl_multi_aff_free(ma);
3806 return NULL;
3809 /* Remove any internal structure of the domain of "ma".
3810 * If there is any such internal structure in the input,
3811 * then the name of the corresponding space is also removed.
3813 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3814 __isl_take isl_multi_aff *ma)
3816 isl_space *space;
3818 if (!ma)
3819 return NULL;
3821 if (!ma->space->nested[0])
3822 return ma;
3824 space = isl_multi_aff_get_space(ma);
3825 space = isl_space_flatten_domain(space);
3826 ma = isl_multi_aff_reset_space(ma, space);
3828 return ma;
3831 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3832 * of the space to its domain.
3834 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3836 int i, n_in;
3837 isl_local_space *ls;
3838 isl_multi_aff *ma;
3840 if (!space)
3841 return NULL;
3842 if (!isl_space_is_map(space))
3843 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3844 "not a map space", goto error);
3846 n_in = isl_space_dim(space, isl_dim_in);
3847 space = isl_space_domain_map(space);
3849 ma = isl_multi_aff_alloc(isl_space_copy(space));
3850 if (n_in == 0) {
3851 isl_space_free(space);
3852 return ma;
3855 space = isl_space_domain(space);
3856 ls = isl_local_space_from_space(space);
3857 for (i = 0; i < n_in; ++i) {
3858 isl_aff *aff;
3860 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3861 isl_dim_set, i);
3862 ma = isl_multi_aff_set_aff(ma, i, aff);
3864 isl_local_space_free(ls);
3865 return ma;
3866 error:
3867 isl_space_free(space);
3868 return NULL;
3871 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3872 * of the space to its range.
3874 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3876 int i, n_in, n_out;
3877 isl_local_space *ls;
3878 isl_multi_aff *ma;
3880 if (!space)
3881 return NULL;
3882 if (!isl_space_is_map(space))
3883 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3884 "not a map space", goto error);
3886 n_in = isl_space_dim(space, isl_dim_in);
3887 n_out = isl_space_dim(space, isl_dim_out);
3888 space = isl_space_range_map(space);
3890 ma = isl_multi_aff_alloc(isl_space_copy(space));
3891 if (n_out == 0) {
3892 isl_space_free(space);
3893 return ma;
3896 space = isl_space_domain(space);
3897 ls = isl_local_space_from_space(space);
3898 for (i = 0; i < n_out; ++i) {
3899 isl_aff *aff;
3901 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3902 isl_dim_set, n_in + i);
3903 ma = isl_multi_aff_set_aff(ma, i, aff);
3905 isl_local_space_free(ls);
3906 return ma;
3907 error:
3908 isl_space_free(space);
3909 return NULL;
3912 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3913 * of the space to its range.
3915 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3916 __isl_take isl_space *space)
3918 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3921 /* Given the space of a set and a range of set dimensions,
3922 * construct an isl_multi_aff that projects out those dimensions.
3924 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3925 __isl_take isl_space *space, enum isl_dim_type type,
3926 unsigned first, unsigned n)
3928 int i, dim;
3929 isl_local_space *ls;
3930 isl_multi_aff *ma;
3932 if (!space)
3933 return NULL;
3934 if (!isl_space_is_set(space))
3935 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3936 "expecting set space", goto error);
3937 if (type != isl_dim_set)
3938 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3939 "only set dimensions can be projected out", goto error);
3941 dim = isl_space_dim(space, isl_dim_set);
3942 if (first + n > dim)
3943 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3944 "range out of bounds", goto error);
3946 space = isl_space_from_domain(space);
3947 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3949 if (dim == n)
3950 return isl_multi_aff_alloc(space);
3952 ma = isl_multi_aff_alloc(isl_space_copy(space));
3953 space = isl_space_domain(space);
3954 ls = isl_local_space_from_space(space);
3956 for (i = 0; i < first; ++i) {
3957 isl_aff *aff;
3959 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3960 isl_dim_set, i);
3961 ma = isl_multi_aff_set_aff(ma, i, aff);
3964 for (i = 0; i < dim - (first + n); ++i) {
3965 isl_aff *aff;
3967 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3968 isl_dim_set, first + n + i);
3969 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3972 isl_local_space_free(ls);
3973 return ma;
3974 error:
3975 isl_space_free(space);
3976 return NULL;
3979 /* Given the space of a set and a range of set dimensions,
3980 * construct an isl_pw_multi_aff that projects out those dimensions.
3982 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3983 __isl_take isl_space *space, enum isl_dim_type type,
3984 unsigned first, unsigned n)
3986 isl_multi_aff *ma;
3988 ma = isl_multi_aff_project_out_map(space, type, first, n);
3989 return isl_pw_multi_aff_from_multi_aff(ma);
3992 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3993 * domain.
3995 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3996 __isl_take isl_multi_aff *ma)
3998 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3999 return isl_pw_multi_aff_alloc(dom, ma);
4002 /* Create a piecewise multi-affine expression in the given space that maps each
4003 * input dimension to the corresponding output dimension.
4005 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4006 __isl_take isl_space *space)
4008 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4011 /* Exploit the equalities in "eq" to simplify the affine expressions.
4013 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4014 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4016 int i;
4018 maff = isl_multi_aff_cow(maff);
4019 if (!maff || !eq)
4020 goto error;
4022 for (i = 0; i < maff->n; ++i) {
4023 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
4024 isl_basic_set_copy(eq));
4025 if (!maff->p[i])
4026 goto error;
4029 isl_basic_set_free(eq);
4030 return maff;
4031 error:
4032 isl_basic_set_free(eq);
4033 isl_multi_aff_free(maff);
4034 return NULL;
4037 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4038 isl_int f)
4040 int i;
4042 maff = isl_multi_aff_cow(maff);
4043 if (!maff)
4044 return NULL;
4046 for (i = 0; i < maff->n; ++i) {
4047 maff->p[i] = isl_aff_scale(maff->p[i], f);
4048 if (!maff->p[i])
4049 return isl_multi_aff_free(maff);
4052 return maff;
4055 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4056 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4058 maff1 = isl_multi_aff_add(maff1, maff2);
4059 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4060 return maff1;
4063 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4065 if (!maff)
4066 return -1;
4068 return 0;
4071 /* Return the set of domain elements where "ma1" is lexicographically
4072 * smaller than or equal to "ma2".
4074 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4075 __isl_take isl_multi_aff *ma2)
4077 return isl_multi_aff_lex_ge_set(ma2, ma1);
4080 /* Return the set of domain elements where "ma1" is lexicographically
4081 * smaller than "ma2".
4083 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4084 __isl_take isl_multi_aff *ma2)
4086 return isl_multi_aff_lex_gt_set(ma2, ma1);
4089 /* Return the set of domain elements where "ma1" and "ma2"
4090 * satisfy "order".
4092 static __isl_give isl_set *isl_multi_aff_order_set(
4093 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4094 __isl_give isl_map *order(__isl_take isl_space *set_space))
4096 isl_space *space;
4097 isl_map *map1, *map2;
4098 isl_map *map, *ge;
4100 map1 = isl_map_from_multi_aff(ma1);
4101 map2 = isl_map_from_multi_aff(ma2);
4102 map = isl_map_range_product(map1, map2);
4103 space = isl_space_range(isl_map_get_space(map));
4104 space = isl_space_domain(isl_space_unwrap(space));
4105 ge = order(space);
4106 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4108 return isl_map_domain(map);
4111 /* Return the set of domain elements where "ma1" is lexicographically
4112 * greater than or equal to "ma2".
4114 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4115 __isl_take isl_multi_aff *ma2)
4117 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4120 /* Return the set of domain elements where "ma1" is lexicographically
4121 * greater than "ma2".
4123 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4124 __isl_take isl_multi_aff *ma2)
4126 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4129 #undef PW
4130 #define PW isl_pw_multi_aff
4131 #undef EL
4132 #define EL isl_multi_aff
4133 #undef EL_IS_ZERO
4134 #define EL_IS_ZERO is_empty
4135 #undef ZERO
4136 #define ZERO empty
4137 #undef IS_ZERO
4138 #define IS_ZERO is_empty
4139 #undef FIELD
4140 #define FIELD maff
4141 #undef DEFAULT_IS_ZERO
4142 #define DEFAULT_IS_ZERO 0
4144 #define NO_SUB
4145 #define NO_EVAL
4146 #define NO_OPT
4147 #define NO_INVOLVES_DIMS
4148 #define NO_INSERT_DIMS
4149 #define NO_LIFT
4150 #define NO_MORPH
4152 #include <isl_pw_templ.c>
4153 #include <isl_pw_union_opt.c>
4155 #undef NO_SUB
4157 #undef UNION
4158 #define UNION isl_union_pw_multi_aff
4159 #undef PART
4160 #define PART isl_pw_multi_aff
4161 #undef PARTS
4162 #define PARTS pw_multi_aff
4164 #include <isl_union_multi.c>
4165 #include <isl_union_neg.c>
4167 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4168 __isl_take isl_pw_multi_aff *pma1,
4169 __isl_take isl_pw_multi_aff *pma2)
4171 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4172 &isl_multi_aff_lex_ge_set);
4175 /* Given two piecewise multi affine expressions, return a piecewise
4176 * multi-affine expression defined on the union of the definition domains
4177 * of the inputs that is equal to the lexicographic maximum of the two
4178 * inputs on each cell. If only one of the two inputs is defined on
4179 * a given cell, then it is considered to be the maximum.
4181 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4182 __isl_take isl_pw_multi_aff *pma1,
4183 __isl_take isl_pw_multi_aff *pma2)
4185 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4186 &pw_multi_aff_union_lexmax);
4189 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4190 __isl_take isl_pw_multi_aff *pma1,
4191 __isl_take isl_pw_multi_aff *pma2)
4193 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4194 &isl_multi_aff_lex_le_set);
4197 /* Given two piecewise multi affine expressions, return a piecewise
4198 * multi-affine expression defined on the union of the definition domains
4199 * of the inputs that is equal to the lexicographic minimum of the two
4200 * inputs on each cell. If only one of the two inputs is defined on
4201 * a given cell, then it is considered to be the minimum.
4203 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4204 __isl_take isl_pw_multi_aff *pma1,
4205 __isl_take isl_pw_multi_aff *pma2)
4207 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4208 &pw_multi_aff_union_lexmin);
4211 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4212 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4214 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4215 &isl_multi_aff_add);
4218 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4219 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4221 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4222 &pw_multi_aff_add);
4225 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4226 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4228 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4229 &isl_multi_aff_sub);
4232 /* Subtract "pma2" from "pma1" and return the result.
4234 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4235 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4237 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4238 &pw_multi_aff_sub);
4241 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4242 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4244 return isl_pw_multi_aff_union_add_(pma1, pma2);
4247 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4248 * with the actual sum on the shared domain and
4249 * the defined expression on the symmetric difference of the domains.
4251 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4252 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4254 return isl_union_pw_aff_union_add_(upa1, upa2);
4257 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4258 * with the actual sum on the shared domain and
4259 * the defined expression on the symmetric difference of the domains.
4261 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4262 __isl_take isl_union_pw_multi_aff *upma1,
4263 __isl_take isl_union_pw_multi_aff *upma2)
4265 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4268 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4269 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4271 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4272 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4274 int i, j, n;
4275 isl_space *space;
4276 isl_pw_multi_aff *res;
4278 if (!pma1 || !pma2)
4279 goto error;
4281 n = pma1->n * pma2->n;
4282 space = isl_space_product(isl_space_copy(pma1->dim),
4283 isl_space_copy(pma2->dim));
4284 res = isl_pw_multi_aff_alloc_size(space, n);
4286 for (i = 0; i < pma1->n; ++i) {
4287 for (j = 0; j < pma2->n; ++j) {
4288 isl_set *domain;
4289 isl_multi_aff *ma;
4291 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4292 isl_set_copy(pma2->p[j].set));
4293 ma = isl_multi_aff_product(
4294 isl_multi_aff_copy(pma1->p[i].maff),
4295 isl_multi_aff_copy(pma2->p[j].maff));
4296 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4300 isl_pw_multi_aff_free(pma1);
4301 isl_pw_multi_aff_free(pma2);
4302 return res;
4303 error:
4304 isl_pw_multi_aff_free(pma1);
4305 isl_pw_multi_aff_free(pma2);
4306 return NULL;
4309 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4310 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4312 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4313 &pw_multi_aff_product);
4316 /* Construct a map mapping the domain of the piecewise multi-affine expression
4317 * to its range, with each dimension in the range equated to the
4318 * corresponding affine expression on its cell.
4320 * If the domain of "pma" is rational, then so is the constructed "map".
4322 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4324 int i;
4325 isl_map *map;
4327 if (!pma)
4328 return NULL;
4330 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4332 for (i = 0; i < pma->n; ++i) {
4333 isl_bool rational;
4334 isl_multi_aff *maff;
4335 isl_basic_map *bmap;
4336 isl_map *map_i;
4338 rational = isl_set_is_rational(pma->p[i].set);
4339 if (rational < 0)
4340 map = isl_map_free(map);
4341 maff = isl_multi_aff_copy(pma->p[i].maff);
4342 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4343 map_i = isl_map_from_basic_map(bmap);
4344 map_i = isl_map_intersect_domain(map_i,
4345 isl_set_copy(pma->p[i].set));
4346 map = isl_map_union_disjoint(map, map_i);
4349 isl_pw_multi_aff_free(pma);
4350 return map;
4353 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4355 if (!pma)
4356 return NULL;
4358 if (!isl_space_is_set(pma->dim))
4359 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4360 "isl_pw_multi_aff cannot be converted into an isl_set",
4361 goto error);
4363 return isl_map_from_pw_multi_aff(pma);
4364 error:
4365 isl_pw_multi_aff_free(pma);
4366 return NULL;
4369 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4370 * denominator "denom".
4371 * "denom" is allowed to be negative, in which case the actual denominator
4372 * is -denom and the expressions are added instead.
4374 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4375 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4377 int i, first;
4378 int sign;
4379 isl_int d;
4381 first = isl_seq_first_non_zero(c, n);
4382 if (first == -1)
4383 return aff;
4385 sign = isl_int_sgn(denom);
4386 isl_int_init(d);
4387 isl_int_abs(d, denom);
4388 for (i = first; i < n; ++i) {
4389 isl_aff *aff_i;
4391 if (isl_int_is_zero(c[i]))
4392 continue;
4393 aff_i = isl_multi_aff_get_aff(ma, i);
4394 aff_i = isl_aff_scale(aff_i, c[i]);
4395 aff_i = isl_aff_scale_down(aff_i, d);
4396 if (sign >= 0)
4397 aff = isl_aff_sub(aff, aff_i);
4398 else
4399 aff = isl_aff_add(aff, aff_i);
4401 isl_int_clear(d);
4403 return aff;
4406 /* Extract an affine expression that expresses the output dimension "pos"
4407 * of "bmap" in terms of the parameters and input dimensions from
4408 * equality "eq".
4409 * Note that this expression may involve integer divisions defined
4410 * in terms of parameters and input dimensions.
4411 * The equality may also involve references to earlier (but not later)
4412 * output dimensions. These are replaced by the corresponding elements
4413 * in "ma".
4415 * If the equality is of the form
4417 * f(i) + h(j) + a x + g(i) = 0,
4419 * with f(i) a linear combinations of the parameters and input dimensions,
4420 * g(i) a linear combination of integer divisions defined in terms of the same
4421 * and h(j) a linear combinations of earlier output dimensions,
4422 * then the affine expression is
4424 * (-f(i) - g(i))/a - h(j)/a
4426 * If the equality is of the form
4428 * f(i) + h(j) - a x + g(i) = 0,
4430 * then the affine expression is
4432 * (f(i) + g(i))/a - h(j)/(-a)
4435 * If "div" refers to an integer division (i.e., it is smaller than
4436 * the number of integer divisions), then the equality constraint
4437 * does involve an integer division (the one at position "div") that
4438 * is defined in terms of output dimensions. However, this integer
4439 * division can be eliminated by exploiting a pair of constraints
4440 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4441 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4442 * -l + x >= 0.
4443 * In particular, let
4445 * x = e(i) + m floor(...)
4447 * with e(i) the expression derived above and floor(...) the integer
4448 * division involving output dimensions.
4449 * From
4451 * l <= x <= l + n,
4453 * we have
4455 * 0 <= x - l <= n
4457 * This means
4459 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4460 * = (e(i) - l) mod m
4462 * Therefore,
4464 * x - l = (e(i) - l) mod m
4466 * or
4468 * x = ((e(i) - l) mod m) + l
4470 * The variable "shift" below contains the expression -l, which may
4471 * also involve a linear combination of earlier output dimensions.
4473 static __isl_give isl_aff *extract_aff_from_equality(
4474 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4475 __isl_keep isl_multi_aff *ma)
4477 unsigned o_out;
4478 unsigned n_div, n_out;
4479 isl_ctx *ctx;
4480 isl_local_space *ls;
4481 isl_aff *aff, *shift;
4482 isl_val *mod;
4484 ctx = isl_basic_map_get_ctx(bmap);
4485 ls = isl_basic_map_get_local_space(bmap);
4486 ls = isl_local_space_domain(ls);
4487 aff = isl_aff_alloc(isl_local_space_copy(ls));
4488 if (!aff)
4489 goto error;
4490 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4491 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4492 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4493 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4494 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4495 isl_seq_cpy(aff->v->el + 1 + o_out,
4496 bmap->eq[eq] + o_out + n_out, n_div);
4497 } else {
4498 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4499 isl_seq_neg(aff->v->el + 1 + o_out,
4500 bmap->eq[eq] + o_out + n_out, n_div);
4502 if (div < n_div)
4503 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4504 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4505 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4506 bmap->eq[eq][o_out + pos]);
4507 if (div < n_div) {
4508 shift = isl_aff_alloc(isl_local_space_copy(ls));
4509 if (!shift)
4510 goto error;
4511 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4512 isl_seq_cpy(shift->v->el + 1 + o_out,
4513 bmap->ineq[ineq] + o_out + n_out, n_div);
4514 isl_int_set_si(shift->v->el[0], 1);
4515 shift = subtract_initial(shift, ma, pos,
4516 bmap->ineq[ineq] + o_out, ctx->negone);
4517 aff = isl_aff_add(aff, isl_aff_copy(shift));
4518 mod = isl_val_int_from_isl_int(ctx,
4519 bmap->eq[eq][o_out + n_out + div]);
4520 mod = isl_val_abs(mod);
4521 aff = isl_aff_mod_val(aff, mod);
4522 aff = isl_aff_sub(aff, shift);
4525 isl_local_space_free(ls);
4526 return aff;
4527 error:
4528 isl_local_space_free(ls);
4529 isl_aff_free(aff);
4530 return NULL;
4533 /* Given a basic map with output dimensions defined
4534 * in terms of the parameters input dimensions and earlier
4535 * output dimensions using an equality (and possibly a pair on inequalities),
4536 * extract an isl_aff that expresses output dimension "pos" in terms
4537 * of the parameters and input dimensions.
4538 * Note that this expression may involve integer divisions defined
4539 * in terms of parameters and input dimensions.
4540 * "ma" contains the expressions corresponding to earlier output dimensions.
4542 * This function shares some similarities with
4543 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4545 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4546 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4548 int eq, div, ineq;
4549 isl_aff *aff;
4551 if (!bmap)
4552 return NULL;
4553 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4554 if (eq >= bmap->n_eq)
4555 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4556 "unable to find suitable equality", return NULL);
4557 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4559 aff = isl_aff_remove_unused_divs(aff);
4560 return aff;
4563 /* Given a basic map where each output dimension is defined
4564 * in terms of the parameters and input dimensions using an equality,
4565 * extract an isl_multi_aff that expresses the output dimensions in terms
4566 * of the parameters and input dimensions.
4568 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4569 __isl_take isl_basic_map *bmap)
4571 int i;
4572 unsigned n_out;
4573 isl_multi_aff *ma;
4575 if (!bmap)
4576 return NULL;
4578 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4579 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4581 for (i = 0; i < n_out; ++i) {
4582 isl_aff *aff;
4584 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4585 ma = isl_multi_aff_set_aff(ma, i, aff);
4588 isl_basic_map_free(bmap);
4590 return ma;
4593 /* Given a basic set where each set dimension is defined
4594 * in terms of the parameters using an equality,
4595 * extract an isl_multi_aff that expresses the set dimensions in terms
4596 * of the parameters.
4598 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4599 __isl_take isl_basic_set *bset)
4601 return extract_isl_multi_aff_from_basic_map(bset);
4604 /* Create an isl_pw_multi_aff that is equivalent to
4605 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4606 * The given basic map is such that each output dimension is defined
4607 * in terms of the parameters and input dimensions using an equality.
4609 * Since some applications expect the result of isl_pw_multi_aff_from_map
4610 * to only contain integer affine expressions, we compute the floor
4611 * of the expression before returning.
4613 * Remove all constraints involving local variables without
4614 * an explicit representation (resulting in the removal of those
4615 * local variables) prior to the actual extraction to ensure
4616 * that the local spaces in which the resulting affine expressions
4617 * are created do not contain any unknown local variables.
4618 * Removing such constraints is safe because constraints involving
4619 * unknown local variables are not used to determine whether
4620 * a basic map is obviously single-valued.
4622 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4623 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4625 isl_multi_aff *ma;
4627 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4628 ma = extract_isl_multi_aff_from_basic_map(bmap);
4629 ma = isl_multi_aff_floor(ma);
4630 return isl_pw_multi_aff_alloc(domain, ma);
4633 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4634 * This obviously only works if the input "map" is single-valued.
4635 * If so, we compute the lexicographic minimum of the image in the form
4636 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4637 * to its lexicographic minimum.
4638 * If the input is not single-valued, we produce an error.
4640 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4641 __isl_take isl_map *map)
4643 int i;
4644 int sv;
4645 isl_pw_multi_aff *pma;
4647 sv = isl_map_is_single_valued(map);
4648 if (sv < 0)
4649 goto error;
4650 if (!sv)
4651 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4652 "map is not single-valued", goto error);
4653 map = isl_map_make_disjoint(map);
4654 if (!map)
4655 return NULL;
4657 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4659 for (i = 0; i < map->n; ++i) {
4660 isl_pw_multi_aff *pma_i;
4661 isl_basic_map *bmap;
4662 bmap = isl_basic_map_copy(map->p[i]);
4663 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4664 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4667 isl_map_free(map);
4668 return pma;
4669 error:
4670 isl_map_free(map);
4671 return NULL;
4674 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4675 * taking into account that the output dimension at position "d"
4676 * can be represented as
4678 * x = floor((e(...) + c1) / m)
4680 * given that constraint "i" is of the form
4682 * e(...) + c1 - m x >= 0
4685 * Let "map" be of the form
4687 * A -> B
4689 * We construct a mapping
4691 * A -> [A -> x = floor(...)]
4693 * apply that to the map, obtaining
4695 * [A -> x = floor(...)] -> B
4697 * and equate dimension "d" to x.
4698 * We then compute a isl_pw_multi_aff representation of the resulting map
4699 * and plug in the mapping above.
4701 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4702 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4704 isl_ctx *ctx;
4705 isl_space *space;
4706 isl_local_space *ls;
4707 isl_multi_aff *ma;
4708 isl_aff *aff;
4709 isl_vec *v;
4710 isl_map *insert;
4711 int offset;
4712 int n;
4713 int n_in;
4714 isl_pw_multi_aff *pma;
4715 isl_bool is_set;
4717 is_set = isl_map_is_set(map);
4718 if (is_set < 0)
4719 goto error;
4721 offset = isl_basic_map_offset(hull, isl_dim_out);
4722 ctx = isl_map_get_ctx(map);
4723 space = isl_space_domain(isl_map_get_space(map));
4724 n_in = isl_space_dim(space, isl_dim_set);
4725 n = isl_space_dim(space, isl_dim_all);
4727 v = isl_vec_alloc(ctx, 1 + 1 + n);
4728 if (v) {
4729 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4730 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4732 isl_basic_map_free(hull);
4734 ls = isl_local_space_from_space(isl_space_copy(space));
4735 aff = isl_aff_alloc_vec(ls, v);
4736 aff = isl_aff_floor(aff);
4737 if (is_set) {
4738 isl_space_free(space);
4739 ma = isl_multi_aff_from_aff(aff);
4740 } else {
4741 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4742 ma = isl_multi_aff_range_product(ma,
4743 isl_multi_aff_from_aff(aff));
4746 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4747 map = isl_map_apply_domain(map, insert);
4748 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4749 pma = isl_pw_multi_aff_from_map(map);
4750 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4752 return pma;
4753 error:
4754 isl_map_free(map);
4755 isl_basic_map_free(hull);
4756 return NULL;
4759 /* Is constraint "c" of the form
4761 * e(...) + c1 - m x >= 0
4763 * or
4765 * -e(...) + c2 + m x >= 0
4767 * where m > 1 and e only depends on parameters and input dimemnsions?
4769 * "offset" is the offset of the output dimensions
4770 * "pos" is the position of output dimension x.
4772 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4774 if (isl_int_is_zero(c[offset + d]))
4775 return 0;
4776 if (isl_int_is_one(c[offset + d]))
4777 return 0;
4778 if (isl_int_is_negone(c[offset + d]))
4779 return 0;
4780 if (isl_seq_first_non_zero(c + offset, d) != -1)
4781 return 0;
4782 if (isl_seq_first_non_zero(c + offset + d + 1,
4783 total - (offset + d + 1)) != -1)
4784 return 0;
4785 return 1;
4788 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4790 * As a special case, we first check if there is any pair of constraints,
4791 * shared by all the basic maps in "map" that force a given dimension
4792 * to be equal to the floor of some affine combination of the input dimensions.
4794 * In particular, if we can find two constraints
4796 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4798 * and
4800 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4802 * where m > 1 and e only depends on parameters and input dimemnsions,
4803 * and such that
4805 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4807 * then we know that we can take
4809 * x = floor((e(...) + c1) / m)
4811 * without having to perform any computation.
4813 * Note that we know that
4815 * c1 + c2 >= 1
4817 * If c1 + c2 were 0, then we would have detected an equality during
4818 * simplification. If c1 + c2 were negative, then we would have detected
4819 * a contradiction.
4821 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4822 __isl_take isl_map *map)
4824 int d, dim;
4825 int i, j, n;
4826 int offset, total;
4827 isl_int sum;
4828 isl_basic_map *hull;
4830 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4831 if (!hull)
4832 goto error;
4834 isl_int_init(sum);
4835 dim = isl_map_dim(map, isl_dim_out);
4836 offset = isl_basic_map_offset(hull, isl_dim_out);
4837 total = 1 + isl_basic_map_total_dim(hull);
4838 n = hull->n_ineq;
4839 for (d = 0; d < dim; ++d) {
4840 for (i = 0; i < n; ++i) {
4841 if (!is_potential_div_constraint(hull->ineq[i],
4842 offset, d, total))
4843 continue;
4844 for (j = i + 1; j < n; ++j) {
4845 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4846 hull->ineq[j] + 1, total - 1))
4847 continue;
4848 isl_int_add(sum, hull->ineq[i][0],
4849 hull->ineq[j][0]);
4850 if (isl_int_abs_lt(sum,
4851 hull->ineq[i][offset + d]))
4852 break;
4855 if (j >= n)
4856 continue;
4857 isl_int_clear(sum);
4858 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4859 j = i;
4860 return pw_multi_aff_from_map_div(map, hull, d, j);
4863 isl_int_clear(sum);
4864 isl_basic_map_free(hull);
4865 return pw_multi_aff_from_map_base(map);
4866 error:
4867 isl_map_free(map);
4868 isl_basic_map_free(hull);
4869 return NULL;
4872 /* Given an affine expression
4874 * [A -> B] -> f(A,B)
4876 * construct an isl_multi_aff
4878 * [A -> B] -> B'
4880 * such that dimension "d" in B' is set to "aff" and the remaining
4881 * dimensions are set equal to the corresponding dimensions in B.
4882 * "n_in" is the dimension of the space A.
4883 * "n_out" is the dimension of the space B.
4885 * If "is_set" is set, then the affine expression is of the form
4887 * [B] -> f(B)
4889 * and we construct an isl_multi_aff
4891 * B -> B'
4893 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4894 unsigned n_in, unsigned n_out, int is_set)
4896 int i;
4897 isl_multi_aff *ma;
4898 isl_space *space, *space2;
4899 isl_local_space *ls;
4901 space = isl_aff_get_domain_space(aff);
4902 ls = isl_local_space_from_space(isl_space_copy(space));
4903 space2 = isl_space_copy(space);
4904 if (!is_set)
4905 space2 = isl_space_range(isl_space_unwrap(space2));
4906 space = isl_space_map_from_domain_and_range(space, space2);
4907 ma = isl_multi_aff_alloc(space);
4908 ma = isl_multi_aff_set_aff(ma, d, aff);
4910 for (i = 0; i < n_out; ++i) {
4911 if (i == d)
4912 continue;
4913 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4914 isl_dim_set, n_in + i);
4915 ma = isl_multi_aff_set_aff(ma, i, aff);
4918 isl_local_space_free(ls);
4920 return ma;
4923 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4924 * taking into account that the dimension at position "d" can be written as
4926 * x = m a + f(..) (1)
4928 * where m is equal to "gcd".
4929 * "i" is the index of the equality in "hull" that defines f(..).
4930 * In particular, the equality is of the form
4932 * f(..) - x + m g(existentials) = 0
4934 * or
4936 * -f(..) + x + m g(existentials) = 0
4938 * We basically plug (1) into "map", resulting in a map with "a"
4939 * in the range instead of "x". The corresponding isl_pw_multi_aff
4940 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4942 * Specifically, given the input map
4944 * A -> B
4946 * We first wrap it into a set
4948 * [A -> B]
4950 * and define (1) on top of the corresponding space, resulting in "aff".
4951 * We use this to create an isl_multi_aff that maps the output position "d"
4952 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4953 * We plug this into the wrapped map, unwrap the result and compute the
4954 * corresponding isl_pw_multi_aff.
4955 * The result is an expression
4957 * A -> T(A)
4959 * We adjust that to
4961 * A -> [A -> T(A)]
4963 * so that we can plug that into "aff", after extending the latter to
4964 * a mapping
4966 * [A -> B] -> B'
4969 * If "map" is actually a set, then there is no "A" space, meaning
4970 * that we do not need to perform any wrapping, and that the result
4971 * of the recursive call is of the form
4973 * [T]
4975 * which is plugged into a mapping of the form
4977 * B -> B'
4979 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4980 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4981 isl_int gcd)
4983 isl_set *set;
4984 isl_space *space;
4985 isl_local_space *ls;
4986 isl_aff *aff;
4987 isl_multi_aff *ma;
4988 isl_pw_multi_aff *pma, *id;
4989 unsigned n_in;
4990 unsigned o_out;
4991 unsigned n_out;
4992 isl_bool is_set;
4994 is_set = isl_map_is_set(map);
4995 if (is_set < 0)
4996 goto error;
4998 n_in = isl_basic_map_dim(hull, isl_dim_in);
4999 n_out = isl_basic_map_dim(hull, isl_dim_out);
5000 o_out = isl_basic_map_offset(hull, isl_dim_out);
5002 if (is_set)
5003 set = map;
5004 else
5005 set = isl_map_wrap(map);
5006 space = isl_space_map_from_set(isl_set_get_space(set));
5007 ma = isl_multi_aff_identity(space);
5008 ls = isl_local_space_from_space(isl_set_get_space(set));
5009 aff = isl_aff_alloc(ls);
5010 if (aff) {
5011 isl_int_set_si(aff->v->el[0], 1);
5012 if (isl_int_is_one(hull->eq[i][o_out + d]))
5013 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5014 aff->v->size - 1);
5015 else
5016 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5017 aff->v->size - 1);
5018 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5020 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5021 set = isl_set_preimage_multi_aff(set, ma);
5023 ma = range_map(aff, d, n_in, n_out, is_set);
5025 if (is_set)
5026 map = set;
5027 else
5028 map = isl_set_unwrap(set);
5029 pma = isl_pw_multi_aff_from_map(map);
5031 if (!is_set) {
5032 space = isl_pw_multi_aff_get_domain_space(pma);
5033 space = isl_space_map_from_set(space);
5034 id = isl_pw_multi_aff_identity(space);
5035 pma = isl_pw_multi_aff_range_product(id, pma);
5037 id = isl_pw_multi_aff_from_multi_aff(ma);
5038 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5040 isl_basic_map_free(hull);
5041 return pma;
5042 error:
5043 isl_map_free(map);
5044 isl_basic_map_free(hull);
5045 return NULL;
5048 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5049 * "hull" contains the equalities valid for "map".
5051 * Check if any of the output dimensions is "strided".
5052 * That is, we check if it can be written as
5054 * x = m a + f(..)
5056 * with m greater than 1, a some combination of existentially quantified
5057 * variables and f an expression in the parameters and input dimensions.
5058 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5060 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5061 * special case.
5063 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5064 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5066 int i, j;
5067 unsigned n_out;
5068 unsigned o_out;
5069 unsigned n_div;
5070 unsigned o_div;
5071 isl_int gcd;
5073 n_div = isl_basic_map_dim(hull, isl_dim_div);
5074 o_div = isl_basic_map_offset(hull, isl_dim_div);
5076 if (n_div == 0) {
5077 isl_basic_map_free(hull);
5078 return pw_multi_aff_from_map_check_div(map);
5081 isl_int_init(gcd);
5083 n_out = isl_basic_map_dim(hull, isl_dim_out);
5084 o_out = isl_basic_map_offset(hull, isl_dim_out);
5086 for (i = 0; i < n_out; ++i) {
5087 for (j = 0; j < hull->n_eq; ++j) {
5088 isl_int *eq = hull->eq[j];
5089 isl_pw_multi_aff *res;
5091 if (!isl_int_is_one(eq[o_out + i]) &&
5092 !isl_int_is_negone(eq[o_out + i]))
5093 continue;
5094 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5095 continue;
5096 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5097 n_out - (i + 1)) != -1)
5098 continue;
5099 isl_seq_gcd(eq + o_div, n_div, &gcd);
5100 if (isl_int_is_zero(gcd))
5101 continue;
5102 if (isl_int_is_one(gcd))
5103 continue;
5105 res = pw_multi_aff_from_map_stride(map, hull,
5106 i, j, gcd);
5107 isl_int_clear(gcd);
5108 return res;
5112 isl_int_clear(gcd);
5113 isl_basic_map_free(hull);
5114 return pw_multi_aff_from_map_check_div(map);
5117 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5119 * As a special case, we first check if all output dimensions are uniquely
5120 * defined in terms of the parameters and input dimensions over the entire
5121 * domain. If so, we extract the desired isl_pw_multi_aff directly
5122 * from the affine hull of "map" and its domain.
5124 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5125 * special cases.
5127 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5129 isl_bool sv;
5130 isl_basic_map *hull;
5132 if (!map)
5133 return NULL;
5135 if (isl_map_n_basic_map(map) == 1) {
5136 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5137 hull = isl_basic_map_plain_affine_hull(hull);
5138 sv = isl_basic_map_plain_is_single_valued(hull);
5139 if (sv >= 0 && sv)
5140 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5141 hull);
5142 isl_basic_map_free(hull);
5144 map = isl_map_detect_equalities(map);
5145 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5146 sv = isl_basic_map_plain_is_single_valued(hull);
5147 if (sv >= 0 && sv)
5148 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5149 if (sv >= 0)
5150 return pw_multi_aff_from_map_check_strides(map, hull);
5151 isl_basic_map_free(hull);
5152 isl_map_free(map);
5153 return NULL;
5156 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5158 return isl_pw_multi_aff_from_map(set);
5161 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5162 * add it to *user.
5164 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5166 isl_union_pw_multi_aff **upma = user;
5167 isl_pw_multi_aff *pma;
5169 pma = isl_pw_multi_aff_from_map(map);
5170 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5172 return *upma ? isl_stat_ok : isl_stat_error;
5175 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5176 * domain.
5178 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5179 __isl_take isl_aff *aff)
5181 isl_multi_aff *ma;
5182 isl_pw_multi_aff *pma;
5184 ma = isl_multi_aff_from_aff(aff);
5185 pma = isl_pw_multi_aff_from_multi_aff(ma);
5186 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5189 /* Try and create an isl_union_pw_multi_aff that is equivalent
5190 * to the given isl_union_map.
5191 * The isl_union_map is required to be single-valued in each space.
5192 * Otherwise, an error is produced.
5194 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5195 __isl_take isl_union_map *umap)
5197 isl_space *space;
5198 isl_union_pw_multi_aff *upma;
5200 space = isl_union_map_get_space(umap);
5201 upma = isl_union_pw_multi_aff_empty(space);
5202 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5203 upma = isl_union_pw_multi_aff_free(upma);
5204 isl_union_map_free(umap);
5206 return upma;
5209 /* Try and create an isl_union_pw_multi_aff that is equivalent
5210 * to the given isl_union_set.
5211 * The isl_union_set is required to be a singleton in each space.
5212 * Otherwise, an error is produced.
5214 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5215 __isl_take isl_union_set *uset)
5217 return isl_union_pw_multi_aff_from_union_map(uset);
5220 /* Return the piecewise affine expression "set ? 1 : 0".
5222 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5224 isl_pw_aff *pa;
5225 isl_space *space = isl_set_get_space(set);
5226 isl_local_space *ls = isl_local_space_from_space(space);
5227 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5228 isl_aff *one = isl_aff_zero_on_domain(ls);
5230 one = isl_aff_add_constant_si(one, 1);
5231 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5232 set = isl_set_complement(set);
5233 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5235 return pa;
5238 /* Plug in "subs" for dimension "type", "pos" of "aff".
5240 * Let i be the dimension to replace and let "subs" be of the form
5242 * f/d
5244 * and "aff" of the form
5246 * (a i + g)/m
5248 * The result is
5250 * (a f + d g')/(m d)
5252 * where g' is the result of plugging in "subs" in each of the integer
5253 * divisions in g.
5255 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5256 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5258 isl_ctx *ctx;
5259 isl_int v;
5261 aff = isl_aff_cow(aff);
5262 if (!aff || !subs)
5263 return isl_aff_free(aff);
5265 ctx = isl_aff_get_ctx(aff);
5266 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5267 isl_die(ctx, isl_error_invalid,
5268 "spaces don't match", return isl_aff_free(aff));
5269 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5270 isl_die(ctx, isl_error_unsupported,
5271 "cannot handle divs yet", return isl_aff_free(aff));
5273 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5274 if (!aff->ls)
5275 return isl_aff_free(aff);
5277 aff->v = isl_vec_cow(aff->v);
5278 if (!aff->v)
5279 return isl_aff_free(aff);
5281 pos += isl_local_space_offset(aff->ls, type);
5283 isl_int_init(v);
5284 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5285 aff->v->size, subs->v->size, v);
5286 isl_int_clear(v);
5288 return aff;
5291 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5292 * expressions in "maff".
5294 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5295 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5296 __isl_keep isl_aff *subs)
5298 int i;
5300 maff = isl_multi_aff_cow(maff);
5301 if (!maff || !subs)
5302 return isl_multi_aff_free(maff);
5304 if (type == isl_dim_in)
5305 type = isl_dim_set;
5307 for (i = 0; i < maff->n; ++i) {
5308 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5309 if (!maff->p[i])
5310 return isl_multi_aff_free(maff);
5313 return maff;
5316 /* Plug in "subs" for dimension "type", "pos" of "pma".
5318 * pma is of the form
5320 * A_i(v) -> M_i(v)
5322 * while subs is of the form
5324 * v' = B_j(v) -> S_j
5326 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5327 * has a contribution in the result, in particular
5329 * C_ij(S_j) -> M_i(S_j)
5331 * Note that plugging in S_j in C_ij may also result in an empty set
5332 * and this contribution should simply be discarded.
5334 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5335 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5336 __isl_keep isl_pw_aff *subs)
5338 int i, j, n;
5339 isl_pw_multi_aff *res;
5341 if (!pma || !subs)
5342 return isl_pw_multi_aff_free(pma);
5344 n = pma->n * subs->n;
5345 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5347 for (i = 0; i < pma->n; ++i) {
5348 for (j = 0; j < subs->n; ++j) {
5349 isl_set *common;
5350 isl_multi_aff *res_ij;
5351 int empty;
5353 common = isl_set_intersect(
5354 isl_set_copy(pma->p[i].set),
5355 isl_set_copy(subs->p[j].set));
5356 common = isl_set_substitute(common,
5357 type, pos, subs->p[j].aff);
5358 empty = isl_set_plain_is_empty(common);
5359 if (empty < 0 || empty) {
5360 isl_set_free(common);
5361 if (empty < 0)
5362 goto error;
5363 continue;
5366 res_ij = isl_multi_aff_substitute(
5367 isl_multi_aff_copy(pma->p[i].maff),
5368 type, pos, subs->p[j].aff);
5370 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5374 isl_pw_multi_aff_free(pma);
5375 return res;
5376 error:
5377 isl_pw_multi_aff_free(pma);
5378 isl_pw_multi_aff_free(res);
5379 return NULL;
5382 /* Compute the preimage of a range of dimensions in the affine expression "src"
5383 * under "ma" and put the result in "dst". The number of dimensions in "src"
5384 * that precede the range is given by "n_before". The number of dimensions
5385 * in the range is given by the number of output dimensions of "ma".
5386 * The number of dimensions that follow the range is given by "n_after".
5387 * If "has_denom" is set (to one),
5388 * then "src" and "dst" have an extra initial denominator.
5389 * "n_div_ma" is the number of existentials in "ma"
5390 * "n_div_bset" is the number of existentials in "src"
5391 * The resulting "dst" (which is assumed to have been allocated by
5392 * the caller) contains coefficients for both sets of existentials,
5393 * first those in "ma" and then those in "src".
5394 * f, c1, c2 and g are temporary objects that have been initialized
5395 * by the caller.
5397 * Let src represent the expression
5399 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5401 * and let ma represent the expressions
5403 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5405 * We start out with the following expression for dst:
5407 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5409 * with the multiplication factor f initially equal to 1
5410 * and f \sum_i b_i v_i kept separately.
5411 * For each x_i that we substitute, we multiply the numerator
5412 * (and denominator) of dst by c_1 = m_i and add the numerator
5413 * of the x_i expression multiplied by c_2 = f b_i,
5414 * after removing the common factors of c_1 and c_2.
5415 * The multiplication factor f also needs to be multiplied by c_1
5416 * for the next x_j, j > i.
5418 void isl_seq_preimage(isl_int *dst, isl_int *src,
5419 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5420 int n_div_ma, int n_div_bmap,
5421 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5423 int i;
5424 int n_param, n_in, n_out;
5425 int o_dst, o_src;
5427 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5428 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5429 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5431 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5432 o_dst = o_src = has_denom + 1 + n_param + n_before;
5433 isl_seq_clr(dst + o_dst, n_in);
5434 o_dst += n_in;
5435 o_src += n_out;
5436 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5437 o_dst += n_after;
5438 o_src += n_after;
5439 isl_seq_clr(dst + o_dst, n_div_ma);
5440 o_dst += n_div_ma;
5441 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5443 isl_int_set_si(f, 1);
5445 for (i = 0; i < n_out; ++i) {
5446 int offset = has_denom + 1 + n_param + n_before + i;
5448 if (isl_int_is_zero(src[offset]))
5449 continue;
5450 isl_int_set(c1, ma->p[i]->v->el[0]);
5451 isl_int_mul(c2, f, src[offset]);
5452 isl_int_gcd(g, c1, c2);
5453 isl_int_divexact(c1, c1, g);
5454 isl_int_divexact(c2, c2, g);
5456 isl_int_mul(f, f, c1);
5457 o_dst = has_denom;
5458 o_src = 1;
5459 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5460 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5461 o_dst += 1 + n_param;
5462 o_src += 1 + n_param;
5463 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5464 o_dst += n_before;
5465 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5466 c2, ma->p[i]->v->el + o_src, n_in);
5467 o_dst += n_in;
5468 o_src += n_in;
5469 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5470 o_dst += n_after;
5471 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5472 c2, ma->p[i]->v->el + o_src, n_div_ma);
5473 o_dst += n_div_ma;
5474 o_src += n_div_ma;
5475 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5476 if (has_denom)
5477 isl_int_mul(dst[0], dst[0], c1);
5481 /* Compute the pullback of "aff" by the function represented by "ma".
5482 * In other words, plug in "ma" in "aff". The result is an affine expression
5483 * defined over the domain space of "ma".
5485 * If "aff" is represented by
5487 * (a(p) + b x + c(divs))/d
5489 * and ma is represented by
5491 * x = D(p) + F(y) + G(divs')
5493 * then the result is
5495 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5497 * The divs in the local space of the input are similarly adjusted
5498 * through a call to isl_local_space_preimage_multi_aff.
5500 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5501 __isl_take isl_multi_aff *ma)
5503 isl_aff *res = NULL;
5504 isl_local_space *ls;
5505 int n_div_aff, n_div_ma;
5506 isl_int f, c1, c2, g;
5508 ma = isl_multi_aff_align_divs(ma);
5509 if (!aff || !ma)
5510 goto error;
5512 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5513 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5515 ls = isl_aff_get_domain_local_space(aff);
5516 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5517 res = isl_aff_alloc(ls);
5518 if (!res)
5519 goto error;
5521 isl_int_init(f);
5522 isl_int_init(c1);
5523 isl_int_init(c2);
5524 isl_int_init(g);
5526 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5527 f, c1, c2, g, 1);
5529 isl_int_clear(f);
5530 isl_int_clear(c1);
5531 isl_int_clear(c2);
5532 isl_int_clear(g);
5534 isl_aff_free(aff);
5535 isl_multi_aff_free(ma);
5536 res = isl_aff_normalize(res);
5537 return res;
5538 error:
5539 isl_aff_free(aff);
5540 isl_multi_aff_free(ma);
5541 isl_aff_free(res);
5542 return NULL;
5545 /* Compute the pullback of "aff1" by the function represented by "aff2".
5546 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5547 * defined over the domain space of "aff1".
5549 * The domain of "aff1" should match the range of "aff2", which means
5550 * that it should be single-dimensional.
5552 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5553 __isl_take isl_aff *aff2)
5555 isl_multi_aff *ma;
5557 ma = isl_multi_aff_from_aff(aff2);
5558 return isl_aff_pullback_multi_aff(aff1, ma);
5561 /* Compute the pullback of "ma1" by the function represented by "ma2".
5562 * In other words, plug in "ma2" in "ma1".
5564 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5566 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5567 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5569 int i;
5570 isl_space *space = NULL;
5572 ma2 = isl_multi_aff_align_divs(ma2);
5573 ma1 = isl_multi_aff_cow(ma1);
5574 if (!ma1 || !ma2)
5575 goto error;
5577 space = isl_space_join(isl_multi_aff_get_space(ma2),
5578 isl_multi_aff_get_space(ma1));
5580 for (i = 0; i < ma1->n; ++i) {
5581 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5582 isl_multi_aff_copy(ma2));
5583 if (!ma1->p[i])
5584 goto error;
5587 ma1 = isl_multi_aff_reset_space(ma1, space);
5588 isl_multi_aff_free(ma2);
5589 return ma1;
5590 error:
5591 isl_space_free(space);
5592 isl_multi_aff_free(ma2);
5593 isl_multi_aff_free(ma1);
5594 return NULL;
5597 /* Compute the pullback of "ma1" by the function represented by "ma2".
5598 * In other words, plug in "ma2" in "ma1".
5600 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5601 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5603 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5604 &isl_multi_aff_pullback_multi_aff_aligned);
5607 /* Extend the local space of "dst" to include the divs
5608 * in the local space of "src".
5610 * If "src" does not have any divs or if the local spaces of "dst" and
5611 * "src" are the same, then no extension is required.
5613 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5614 __isl_keep isl_aff *src)
5616 isl_ctx *ctx;
5617 int src_n_div, dst_n_div;
5618 int *exp1 = NULL;
5619 int *exp2 = NULL;
5620 isl_bool equal;
5621 isl_mat *div;
5623 if (!src || !dst)
5624 return isl_aff_free(dst);
5626 ctx = isl_aff_get_ctx(src);
5627 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5628 if (equal < 0)
5629 return isl_aff_free(dst);
5630 if (!equal)
5631 isl_die(ctx, isl_error_invalid,
5632 "spaces don't match", goto error);
5634 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5635 if (src_n_div == 0)
5636 return dst;
5637 equal = isl_local_space_is_equal(src->ls, dst->ls);
5638 if (equal < 0)
5639 return isl_aff_free(dst);
5640 if (equal)
5641 return dst;
5643 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5644 exp1 = isl_alloc_array(ctx, int, src_n_div);
5645 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5646 if (!exp1 || (dst_n_div && !exp2))
5647 goto error;
5649 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5650 dst = isl_aff_expand_divs(dst, div, exp2);
5651 free(exp1);
5652 free(exp2);
5654 return dst;
5655 error:
5656 free(exp1);
5657 free(exp2);
5658 return isl_aff_free(dst);
5661 /* Adjust the local spaces of the affine expressions in "maff"
5662 * such that they all have the save divs.
5664 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5665 __isl_take isl_multi_aff *maff)
5667 int i;
5669 if (!maff)
5670 return NULL;
5671 if (maff->n == 0)
5672 return maff;
5673 maff = isl_multi_aff_cow(maff);
5674 if (!maff)
5675 return NULL;
5677 for (i = 1; i < maff->n; ++i)
5678 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5679 for (i = 1; i < maff->n; ++i) {
5680 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5681 if (!maff->p[i])
5682 return isl_multi_aff_free(maff);
5685 return maff;
5688 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5690 aff = isl_aff_cow(aff);
5691 if (!aff)
5692 return NULL;
5694 aff->ls = isl_local_space_lift(aff->ls);
5695 if (!aff->ls)
5696 return isl_aff_free(aff);
5698 return aff;
5701 /* Lift "maff" to a space with extra dimensions such that the result
5702 * has no more existentially quantified variables.
5703 * If "ls" is not NULL, then *ls is assigned the local space that lies
5704 * at the basis of the lifting applied to "maff".
5706 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5707 __isl_give isl_local_space **ls)
5709 int i;
5710 isl_space *space;
5711 unsigned n_div;
5713 if (ls)
5714 *ls = NULL;
5716 if (!maff)
5717 return NULL;
5719 if (maff->n == 0) {
5720 if (ls) {
5721 isl_space *space = isl_multi_aff_get_domain_space(maff);
5722 *ls = isl_local_space_from_space(space);
5723 if (!*ls)
5724 return isl_multi_aff_free(maff);
5726 return maff;
5729 maff = isl_multi_aff_cow(maff);
5730 maff = isl_multi_aff_align_divs(maff);
5731 if (!maff)
5732 return NULL;
5734 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5735 space = isl_multi_aff_get_space(maff);
5736 space = isl_space_lift(isl_space_domain(space), n_div);
5737 space = isl_space_extend_domain_with_range(space,
5738 isl_multi_aff_get_space(maff));
5739 if (!space)
5740 return isl_multi_aff_free(maff);
5741 isl_space_free(maff->space);
5742 maff->space = space;
5744 if (ls) {
5745 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5746 if (!*ls)
5747 return isl_multi_aff_free(maff);
5750 for (i = 0; i < maff->n; ++i) {
5751 maff->p[i] = isl_aff_lift(maff->p[i]);
5752 if (!maff->p[i])
5753 goto error;
5756 return maff;
5757 error:
5758 if (ls)
5759 isl_local_space_free(*ls);
5760 return isl_multi_aff_free(maff);
5764 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5766 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5767 __isl_keep isl_pw_multi_aff *pma, int pos)
5769 int i;
5770 int n_out;
5771 isl_space *space;
5772 isl_pw_aff *pa;
5774 if (!pma)
5775 return NULL;
5777 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5778 if (pos < 0 || pos >= n_out)
5779 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5780 "index out of bounds", return NULL);
5782 space = isl_pw_multi_aff_get_space(pma);
5783 space = isl_space_drop_dims(space, isl_dim_out,
5784 pos + 1, n_out - pos - 1);
5785 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5787 pa = isl_pw_aff_alloc_size(space, pma->n);
5788 for (i = 0; i < pma->n; ++i) {
5789 isl_aff *aff;
5790 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5791 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5794 return pa;
5797 /* Return an isl_pw_multi_aff with the given "set" as domain and
5798 * an unnamed zero-dimensional range.
5800 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5801 __isl_take isl_set *set)
5803 isl_multi_aff *ma;
5804 isl_space *space;
5806 space = isl_set_get_space(set);
5807 space = isl_space_from_domain(space);
5808 ma = isl_multi_aff_zero(space);
5809 return isl_pw_multi_aff_alloc(set, ma);
5812 /* Add an isl_pw_multi_aff with the given "set" as domain and
5813 * an unnamed zero-dimensional range to *user.
5815 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5816 void *user)
5818 isl_union_pw_multi_aff **upma = user;
5819 isl_pw_multi_aff *pma;
5821 pma = isl_pw_multi_aff_from_domain(set);
5822 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5824 return isl_stat_ok;
5827 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5828 * an unnamed zero-dimensional range.
5830 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5831 __isl_take isl_union_set *uset)
5833 isl_space *space;
5834 isl_union_pw_multi_aff *upma;
5836 if (!uset)
5837 return NULL;
5839 space = isl_union_set_get_space(uset);
5840 upma = isl_union_pw_multi_aff_empty(space);
5842 if (isl_union_set_foreach_set(uset,
5843 &add_pw_multi_aff_from_domain, &upma) < 0)
5844 goto error;
5846 isl_union_set_free(uset);
5847 return upma;
5848 error:
5849 isl_union_set_free(uset);
5850 isl_union_pw_multi_aff_free(upma);
5851 return NULL;
5854 /* Convert "pma" to an isl_map and add it to *umap.
5856 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5857 void *user)
5859 isl_union_map **umap = user;
5860 isl_map *map;
5862 map = isl_map_from_pw_multi_aff(pma);
5863 *umap = isl_union_map_add_map(*umap, map);
5865 return isl_stat_ok;
5868 /* Construct a union map mapping the domain of the union
5869 * piecewise multi-affine expression to its range, with each dimension
5870 * in the range equated to the corresponding affine expression on its cell.
5872 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5873 __isl_take isl_union_pw_multi_aff *upma)
5875 isl_space *space;
5876 isl_union_map *umap;
5878 if (!upma)
5879 return NULL;
5881 space = isl_union_pw_multi_aff_get_space(upma);
5882 umap = isl_union_map_empty(space);
5884 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5885 &map_from_pw_multi_aff, &umap) < 0)
5886 goto error;
5888 isl_union_pw_multi_aff_free(upma);
5889 return umap;
5890 error:
5891 isl_union_pw_multi_aff_free(upma);
5892 isl_union_map_free(umap);
5893 return NULL;
5896 /* Local data for bin_entry and the callback "fn".
5898 struct isl_union_pw_multi_aff_bin_data {
5899 isl_union_pw_multi_aff *upma2;
5900 isl_union_pw_multi_aff *res;
5901 isl_pw_multi_aff *pma;
5902 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5905 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5906 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5908 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5910 struct isl_union_pw_multi_aff_bin_data *data = user;
5911 isl_stat r;
5913 data->pma = pma;
5914 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5915 data->fn, data);
5916 isl_pw_multi_aff_free(pma);
5918 return r;
5921 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5922 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5923 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5924 * as *entry. The callback should adjust data->res if desired.
5926 static __isl_give isl_union_pw_multi_aff *bin_op(
5927 __isl_take isl_union_pw_multi_aff *upma1,
5928 __isl_take isl_union_pw_multi_aff *upma2,
5929 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5931 isl_space *space;
5932 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5934 space = isl_union_pw_multi_aff_get_space(upma2);
5935 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5936 space = isl_union_pw_multi_aff_get_space(upma1);
5937 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5939 if (!upma1 || !upma2)
5940 goto error;
5942 data.upma2 = upma2;
5943 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5944 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5945 &bin_entry, &data) < 0)
5946 goto error;
5948 isl_union_pw_multi_aff_free(upma1);
5949 isl_union_pw_multi_aff_free(upma2);
5950 return data.res;
5951 error:
5952 isl_union_pw_multi_aff_free(upma1);
5953 isl_union_pw_multi_aff_free(upma2);
5954 isl_union_pw_multi_aff_free(data.res);
5955 return NULL;
5958 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5959 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5961 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5962 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5964 isl_space *space;
5966 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5967 isl_pw_multi_aff_get_space(pma2));
5968 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5969 &isl_multi_aff_range_product);
5972 /* Given two isl_pw_multi_affs A -> B and C -> D,
5973 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5975 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5976 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5978 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5979 &pw_multi_aff_range_product);
5982 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5983 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5985 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5986 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5988 isl_space *space;
5990 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5991 isl_pw_multi_aff_get_space(pma2));
5992 space = isl_space_flatten_range(space);
5993 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5994 &isl_multi_aff_flat_range_product);
5997 /* Given two isl_pw_multi_affs A -> B and C -> D,
5998 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6000 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6001 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6003 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6004 &pw_multi_aff_flat_range_product);
6007 /* If data->pma and "pma2" have the same domain space, then compute
6008 * their flat range product and the result to data->res.
6010 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6011 void *user)
6013 struct isl_union_pw_multi_aff_bin_data *data = user;
6015 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6016 pma2->dim, isl_dim_in)) {
6017 isl_pw_multi_aff_free(pma2);
6018 return isl_stat_ok;
6021 pma2 = isl_pw_multi_aff_flat_range_product(
6022 isl_pw_multi_aff_copy(data->pma), pma2);
6024 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6026 return isl_stat_ok;
6029 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6030 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6032 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6033 __isl_take isl_union_pw_multi_aff *upma1,
6034 __isl_take isl_union_pw_multi_aff *upma2)
6036 return bin_op(upma1, upma2, &flat_range_product_entry);
6039 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6040 * The parameters are assumed to have been aligned.
6042 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6043 * except that it works on two different isl_pw_* types.
6045 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6046 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6047 __isl_take isl_pw_aff *pa)
6049 int i, j, n;
6050 isl_pw_multi_aff *res = NULL;
6052 if (!pma || !pa)
6053 goto error;
6055 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6056 pa->dim, isl_dim_in))
6057 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6058 "domains don't match", goto error);
6059 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6060 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6061 "index out of bounds", goto error);
6063 n = pma->n * pa->n;
6064 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6066 for (i = 0; i < pma->n; ++i) {
6067 for (j = 0; j < pa->n; ++j) {
6068 isl_set *common;
6069 isl_multi_aff *res_ij;
6070 int empty;
6072 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6073 isl_set_copy(pa->p[j].set));
6074 empty = isl_set_plain_is_empty(common);
6075 if (empty < 0 || empty) {
6076 isl_set_free(common);
6077 if (empty < 0)
6078 goto error;
6079 continue;
6082 res_ij = isl_multi_aff_set_aff(
6083 isl_multi_aff_copy(pma->p[i].maff), pos,
6084 isl_aff_copy(pa->p[j].aff));
6085 res_ij = isl_multi_aff_gist(res_ij,
6086 isl_set_copy(common));
6088 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6092 isl_pw_multi_aff_free(pma);
6093 isl_pw_aff_free(pa);
6094 return res;
6095 error:
6096 isl_pw_multi_aff_free(pma);
6097 isl_pw_aff_free(pa);
6098 return isl_pw_multi_aff_free(res);
6101 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6103 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6104 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6105 __isl_take isl_pw_aff *pa)
6107 isl_bool equal_params;
6109 if (!pma || !pa)
6110 goto error;
6111 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6112 if (equal_params < 0)
6113 goto error;
6114 if (equal_params)
6115 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6116 if (!isl_space_has_named_params(pma->dim) ||
6117 !isl_space_has_named_params(pa->dim))
6118 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6119 "unaligned unnamed parameters", goto error);
6120 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6121 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6122 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6123 error:
6124 isl_pw_multi_aff_free(pma);
6125 isl_pw_aff_free(pa);
6126 return NULL;
6129 /* Do the parameters of "pa" match those of "space"?
6131 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6132 __isl_keep isl_space *space)
6134 isl_space *pa_space;
6135 isl_bool match;
6137 if (!pa || !space)
6138 return isl_bool_error;
6140 pa_space = isl_pw_aff_get_space(pa);
6142 match = isl_space_has_equal_params(space, pa_space);
6144 isl_space_free(pa_space);
6145 return match;
6148 /* Check that the domain space of "pa" matches "space".
6150 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6151 __isl_keep isl_space *space)
6153 isl_space *pa_space;
6154 isl_bool match;
6156 if (!pa || !space)
6157 return isl_stat_error;
6159 pa_space = isl_pw_aff_get_space(pa);
6161 match = isl_space_has_equal_params(space, pa_space);
6162 if (match < 0)
6163 goto error;
6164 if (!match)
6165 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6166 "parameters don't match", goto error);
6167 match = isl_space_tuple_is_equal(space, isl_dim_in,
6168 pa_space, isl_dim_in);
6169 if (match < 0)
6170 goto error;
6171 if (!match)
6172 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6173 "domains don't match", goto error);
6174 isl_space_free(pa_space);
6175 return isl_stat_ok;
6176 error:
6177 isl_space_free(pa_space);
6178 return isl_stat_error;
6181 #undef BASE
6182 #define BASE pw_aff
6183 #undef DOMBASE
6184 #define DOMBASE set
6186 #include <isl_multi_templ.c>
6187 #include <isl_multi_apply_set.c>
6188 #include <isl_multi_coalesce.c>
6189 #include <isl_multi_dims.c>
6190 #include <isl_multi_gist.c>
6191 #include <isl_multi_hash.c>
6192 #include <isl_multi_intersect.c>
6194 /* Scale the elements of "pma" by the corresponding elements of "mv".
6196 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6197 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6199 int i;
6200 isl_bool equal_params;
6202 pma = isl_pw_multi_aff_cow(pma);
6203 if (!pma || !mv)
6204 goto error;
6205 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6206 mv->space, isl_dim_set))
6207 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6208 "spaces don't match", goto error);
6209 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6210 if (equal_params < 0)
6211 goto error;
6212 if (!equal_params) {
6213 pma = isl_pw_multi_aff_align_params(pma,
6214 isl_multi_val_get_space(mv));
6215 mv = isl_multi_val_align_params(mv,
6216 isl_pw_multi_aff_get_space(pma));
6217 if (!pma || !mv)
6218 goto error;
6221 for (i = 0; i < pma->n; ++i) {
6222 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6223 isl_multi_val_copy(mv));
6224 if (!pma->p[i].maff)
6225 goto error;
6228 isl_multi_val_free(mv);
6229 return pma;
6230 error:
6231 isl_multi_val_free(mv);
6232 isl_pw_multi_aff_free(pma);
6233 return NULL;
6236 /* This function is called for each entry of an isl_union_pw_multi_aff.
6237 * If the space of the entry matches that of data->mv,
6238 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6239 * Otherwise, return an empty isl_pw_multi_aff.
6241 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6242 __isl_take isl_pw_multi_aff *pma, void *user)
6244 isl_multi_val *mv = user;
6246 if (!pma)
6247 return NULL;
6248 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6249 mv->space, isl_dim_set)) {
6250 isl_space *space = isl_pw_multi_aff_get_space(pma);
6251 isl_pw_multi_aff_free(pma);
6252 return isl_pw_multi_aff_empty(space);
6255 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6258 /* Scale the elements of "upma" by the corresponding elements of "mv",
6259 * for those entries that match the space of "mv".
6261 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6262 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6264 upma = isl_union_pw_multi_aff_align_params(upma,
6265 isl_multi_val_get_space(mv));
6266 mv = isl_multi_val_align_params(mv,
6267 isl_union_pw_multi_aff_get_space(upma));
6268 if (!upma || !mv)
6269 goto error;
6271 return isl_union_pw_multi_aff_transform(upma,
6272 &union_pw_multi_aff_scale_multi_val_entry, mv);
6274 isl_multi_val_free(mv);
6275 return upma;
6276 error:
6277 isl_multi_val_free(mv);
6278 isl_union_pw_multi_aff_free(upma);
6279 return NULL;
6282 /* Construct and return a piecewise multi affine expression
6283 * in the given space with value zero in each of the output dimensions and
6284 * a universe domain.
6286 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6288 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6291 /* Construct and return a piecewise multi affine expression
6292 * that is equal to the given piecewise affine expression.
6294 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6295 __isl_take isl_pw_aff *pa)
6297 int i;
6298 isl_space *space;
6299 isl_pw_multi_aff *pma;
6301 if (!pa)
6302 return NULL;
6304 space = isl_pw_aff_get_space(pa);
6305 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6307 for (i = 0; i < pa->n; ++i) {
6308 isl_set *set;
6309 isl_multi_aff *ma;
6311 set = isl_set_copy(pa->p[i].set);
6312 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6313 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6316 isl_pw_aff_free(pa);
6317 return pma;
6320 /* Construct a set or map mapping the shared (parameter) domain
6321 * of the piecewise affine expressions to the range of "mpa"
6322 * with each dimension in the range equated to the
6323 * corresponding piecewise affine expression.
6325 static __isl_give isl_map *map_from_multi_pw_aff(
6326 __isl_take isl_multi_pw_aff *mpa)
6328 int i;
6329 isl_space *space;
6330 isl_map *map;
6332 if (!mpa)
6333 return NULL;
6335 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6336 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6337 "invalid space", goto error);
6339 space = isl_multi_pw_aff_get_domain_space(mpa);
6340 map = isl_map_universe(isl_space_from_domain(space));
6342 for (i = 0; i < mpa->n; ++i) {
6343 isl_pw_aff *pa;
6344 isl_map *map_i;
6346 pa = isl_pw_aff_copy(mpa->p[i]);
6347 map_i = map_from_pw_aff(pa);
6349 map = isl_map_flat_range_product(map, map_i);
6352 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6354 isl_multi_pw_aff_free(mpa);
6355 return map;
6356 error:
6357 isl_multi_pw_aff_free(mpa);
6358 return NULL;
6361 /* Construct a map mapping the shared domain
6362 * of the piecewise affine expressions to the range of "mpa"
6363 * with each dimension in the range equated to the
6364 * corresponding piecewise affine expression.
6366 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6368 if (!mpa)
6369 return NULL;
6370 if (isl_space_is_set(mpa->space))
6371 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6372 "space of input is not a map", goto error);
6374 return map_from_multi_pw_aff(mpa);
6375 error:
6376 isl_multi_pw_aff_free(mpa);
6377 return NULL;
6380 /* Construct a set mapping the shared parameter domain
6381 * of the piecewise affine expressions to the space of "mpa"
6382 * with each dimension in the range equated to the
6383 * corresponding piecewise affine expression.
6385 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6387 if (!mpa)
6388 return NULL;
6389 if (!isl_space_is_set(mpa->space))
6390 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6391 "space of input is not a set", goto error);
6393 return map_from_multi_pw_aff(mpa);
6394 error:
6395 isl_multi_pw_aff_free(mpa);
6396 return NULL;
6399 /* Construct and return a piecewise multi affine expression
6400 * that is equal to the given multi piecewise affine expression
6401 * on the shared domain of the piecewise affine expressions.
6403 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6404 __isl_take isl_multi_pw_aff *mpa)
6406 int i;
6407 isl_space *space;
6408 isl_pw_aff *pa;
6409 isl_pw_multi_aff *pma;
6411 if (!mpa)
6412 return NULL;
6414 space = isl_multi_pw_aff_get_space(mpa);
6416 if (mpa->n == 0) {
6417 isl_multi_pw_aff_free(mpa);
6418 return isl_pw_multi_aff_zero(space);
6421 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6422 pma = isl_pw_multi_aff_from_pw_aff(pa);
6424 for (i = 1; i < mpa->n; ++i) {
6425 isl_pw_multi_aff *pma_i;
6427 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6428 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6429 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6432 pma = isl_pw_multi_aff_reset_space(pma, space);
6434 isl_multi_pw_aff_free(mpa);
6435 return pma;
6438 /* Construct and return a multi piecewise affine expression
6439 * that is equal to the given multi affine expression.
6441 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6442 __isl_take isl_multi_aff *ma)
6444 int i, n;
6445 isl_multi_pw_aff *mpa;
6447 if (!ma)
6448 return NULL;
6450 n = isl_multi_aff_dim(ma, isl_dim_out);
6451 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6453 for (i = 0; i < n; ++i) {
6454 isl_pw_aff *pa;
6456 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6457 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6460 isl_multi_aff_free(ma);
6461 return mpa;
6464 /* Construct and return a multi piecewise affine expression
6465 * that is equal to the given piecewise multi affine expression.
6467 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6468 __isl_take isl_pw_multi_aff *pma)
6470 int i, n;
6471 isl_space *space;
6472 isl_multi_pw_aff *mpa;
6474 if (!pma)
6475 return NULL;
6477 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6478 space = isl_pw_multi_aff_get_space(pma);
6479 mpa = isl_multi_pw_aff_alloc(space);
6481 for (i = 0; i < n; ++i) {
6482 isl_pw_aff *pa;
6484 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6485 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6488 isl_pw_multi_aff_free(pma);
6489 return mpa;
6492 /* Do "pa1" and "pa2" represent the same function?
6494 * We first check if they are obviously equal.
6495 * If not, we convert them to maps and check if those are equal.
6497 * If "pa1" or "pa2" contain any NaNs, then they are considered
6498 * not to be the same. A NaN is not equal to anything, not even
6499 * to another NaN.
6501 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6502 __isl_keep isl_pw_aff *pa2)
6504 isl_bool equal;
6505 isl_bool has_nan;
6506 isl_map *map1, *map2;
6508 if (!pa1 || !pa2)
6509 return isl_bool_error;
6511 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6512 if (equal < 0 || equal)
6513 return equal;
6514 has_nan = either_involves_nan(pa1, pa2);
6515 if (has_nan < 0)
6516 return isl_bool_error;
6517 if (has_nan)
6518 return isl_bool_false;
6520 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6521 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6522 equal = isl_map_is_equal(map1, map2);
6523 isl_map_free(map1);
6524 isl_map_free(map2);
6526 return equal;
6529 /* Do "mpa1" and "mpa2" represent the same function?
6531 * Note that we cannot convert the entire isl_multi_pw_aff
6532 * to a map because the domains of the piecewise affine expressions
6533 * may not be the same.
6535 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6536 __isl_keep isl_multi_pw_aff *mpa2)
6538 int i;
6539 isl_bool equal, equal_params;
6541 if (!mpa1 || !mpa2)
6542 return isl_bool_error;
6544 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6545 if (equal_params < 0)
6546 return isl_bool_error;
6547 if (!equal_params) {
6548 if (!isl_space_has_named_params(mpa1->space))
6549 return isl_bool_false;
6550 if (!isl_space_has_named_params(mpa2->space))
6551 return isl_bool_false;
6552 mpa1 = isl_multi_pw_aff_copy(mpa1);
6553 mpa2 = isl_multi_pw_aff_copy(mpa2);
6554 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6555 isl_multi_pw_aff_get_space(mpa2));
6556 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6557 isl_multi_pw_aff_get_space(mpa1));
6558 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6559 isl_multi_pw_aff_free(mpa1);
6560 isl_multi_pw_aff_free(mpa2);
6561 return equal;
6564 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6565 if (equal < 0 || !equal)
6566 return equal;
6568 for (i = 0; i < mpa1->n; ++i) {
6569 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6570 if (equal < 0 || !equal)
6571 return equal;
6574 return isl_bool_true;
6577 /* Do "pma1" and "pma2" represent the same function?
6579 * First check if they are obviously equal.
6580 * If not, then convert them to maps and check if those are equal.
6582 * If "pa1" or "pa2" contain any NaNs, then they are considered
6583 * not to be the same. A NaN is not equal to anything, not even
6584 * to another NaN.
6586 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6587 __isl_keep isl_pw_multi_aff *pma2)
6589 isl_bool equal;
6590 isl_bool has_nan;
6591 isl_map *map1, *map2;
6593 if (!pma1 || !pma2)
6594 return isl_bool_error;
6596 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6597 if (equal < 0 || equal)
6598 return equal;
6599 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6600 if (has_nan >= 0 && !has_nan)
6601 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6602 if (has_nan < 0 || has_nan)
6603 return isl_bool_not(has_nan);
6605 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6606 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6607 equal = isl_map_is_equal(map1, map2);
6608 isl_map_free(map1);
6609 isl_map_free(map2);
6611 return equal;
6614 /* Compute the pullback of "mpa" by the function represented by "ma".
6615 * In other words, plug in "ma" in "mpa".
6617 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6619 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6620 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6622 int i;
6623 isl_space *space = NULL;
6625 mpa = isl_multi_pw_aff_cow(mpa);
6626 if (!mpa || !ma)
6627 goto error;
6629 space = isl_space_join(isl_multi_aff_get_space(ma),
6630 isl_multi_pw_aff_get_space(mpa));
6631 if (!space)
6632 goto error;
6634 for (i = 0; i < mpa->n; ++i) {
6635 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6636 isl_multi_aff_copy(ma));
6637 if (!mpa->p[i])
6638 goto error;
6641 isl_multi_aff_free(ma);
6642 isl_space_free(mpa->space);
6643 mpa->space = space;
6644 return mpa;
6645 error:
6646 isl_space_free(space);
6647 isl_multi_pw_aff_free(mpa);
6648 isl_multi_aff_free(ma);
6649 return NULL;
6652 /* Compute the pullback of "mpa" by the function represented by "ma".
6653 * In other words, plug in "ma" in "mpa".
6655 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6656 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6658 isl_bool equal_params;
6660 if (!mpa || !ma)
6661 goto error;
6662 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6663 if (equal_params < 0)
6664 goto error;
6665 if (equal_params)
6666 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6667 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6668 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6669 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6670 error:
6671 isl_multi_pw_aff_free(mpa);
6672 isl_multi_aff_free(ma);
6673 return NULL;
6676 /* Compute the pullback of "mpa" by the function represented by "pma".
6677 * In other words, plug in "pma" in "mpa".
6679 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6681 static __isl_give isl_multi_pw_aff *
6682 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6683 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6685 int i;
6686 isl_space *space = NULL;
6688 mpa = isl_multi_pw_aff_cow(mpa);
6689 if (!mpa || !pma)
6690 goto error;
6692 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6693 isl_multi_pw_aff_get_space(mpa));
6695 for (i = 0; i < mpa->n; ++i) {
6696 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6697 isl_pw_multi_aff_copy(pma));
6698 if (!mpa->p[i])
6699 goto error;
6702 isl_pw_multi_aff_free(pma);
6703 isl_space_free(mpa->space);
6704 mpa->space = space;
6705 return mpa;
6706 error:
6707 isl_space_free(space);
6708 isl_multi_pw_aff_free(mpa);
6709 isl_pw_multi_aff_free(pma);
6710 return NULL;
6713 /* Compute the pullback of "mpa" by the function represented by "pma".
6714 * In other words, plug in "pma" in "mpa".
6716 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6717 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6719 isl_bool equal_params;
6721 if (!mpa || !pma)
6722 goto error;
6723 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6724 if (equal_params < 0)
6725 goto error;
6726 if (equal_params)
6727 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6728 mpa = isl_multi_pw_aff_align_params(mpa,
6729 isl_pw_multi_aff_get_space(pma));
6730 pma = isl_pw_multi_aff_align_params(pma,
6731 isl_multi_pw_aff_get_space(mpa));
6732 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6733 error:
6734 isl_multi_pw_aff_free(mpa);
6735 isl_pw_multi_aff_free(pma);
6736 return NULL;
6739 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6740 * with the domain of "aff". The domain of the result is the same
6741 * as that of "mpa".
6742 * "mpa" and "aff" are assumed to have been aligned.
6744 * We first extract the parametric constant from "aff", defined
6745 * over the correct domain.
6746 * Then we add the appropriate combinations of the members of "mpa".
6747 * Finally, we add the integer divisions through recursive calls.
6749 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6750 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6752 int i, n_in, n_div;
6753 isl_space *space;
6754 isl_val *v;
6755 isl_pw_aff *pa;
6756 isl_aff *tmp;
6758 n_in = isl_aff_dim(aff, isl_dim_in);
6759 n_div = isl_aff_dim(aff, isl_dim_div);
6761 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6762 tmp = isl_aff_copy(aff);
6763 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6764 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6765 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6766 isl_space_dim(space, isl_dim_set));
6767 tmp = isl_aff_reset_domain_space(tmp, space);
6768 pa = isl_pw_aff_from_aff(tmp);
6770 for (i = 0; i < n_in; ++i) {
6771 isl_pw_aff *pa_i;
6773 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6774 continue;
6775 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6776 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6777 pa_i = isl_pw_aff_scale_val(pa_i, v);
6778 pa = isl_pw_aff_add(pa, pa_i);
6781 for (i = 0; i < n_div; ++i) {
6782 isl_aff *div;
6783 isl_pw_aff *pa_i;
6785 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6786 continue;
6787 div = isl_aff_get_div(aff, i);
6788 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6789 isl_multi_pw_aff_copy(mpa), div);
6790 pa_i = isl_pw_aff_floor(pa_i);
6791 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6792 pa_i = isl_pw_aff_scale_val(pa_i, v);
6793 pa = isl_pw_aff_add(pa, pa_i);
6796 isl_multi_pw_aff_free(mpa);
6797 isl_aff_free(aff);
6799 return pa;
6802 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6803 * with the domain of "aff". The domain of the result is the same
6804 * as that of "mpa".
6806 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6807 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6809 isl_bool equal_params;
6811 if (!aff || !mpa)
6812 goto error;
6813 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6814 if (equal_params < 0)
6815 goto error;
6816 if (equal_params)
6817 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6819 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6820 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6822 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6823 error:
6824 isl_aff_free(aff);
6825 isl_multi_pw_aff_free(mpa);
6826 return NULL;
6829 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6830 * with the domain of "pa". The domain of the result is the same
6831 * as that of "mpa".
6832 * "mpa" and "pa" are assumed to have been aligned.
6834 * We consider each piece in turn. Note that the domains of the
6835 * pieces are assumed to be disjoint and they remain disjoint
6836 * after taking the preimage (over the same function).
6838 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6839 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6841 isl_space *space;
6842 isl_pw_aff *res;
6843 int i;
6845 if (!mpa || !pa)
6846 goto error;
6848 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6849 isl_pw_aff_get_space(pa));
6850 res = isl_pw_aff_empty(space);
6852 for (i = 0; i < pa->n; ++i) {
6853 isl_pw_aff *pa_i;
6854 isl_set *domain;
6856 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6857 isl_multi_pw_aff_copy(mpa),
6858 isl_aff_copy(pa->p[i].aff));
6859 domain = isl_set_copy(pa->p[i].set);
6860 domain = isl_set_preimage_multi_pw_aff(domain,
6861 isl_multi_pw_aff_copy(mpa));
6862 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6863 res = isl_pw_aff_add_disjoint(res, pa_i);
6866 isl_pw_aff_free(pa);
6867 isl_multi_pw_aff_free(mpa);
6868 return res;
6869 error:
6870 isl_pw_aff_free(pa);
6871 isl_multi_pw_aff_free(mpa);
6872 return NULL;
6875 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6876 * with the domain of "pa". The domain of the result is the same
6877 * as that of "mpa".
6879 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6880 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6882 isl_bool equal_params;
6884 if (!pa || !mpa)
6885 goto error;
6886 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6887 if (equal_params < 0)
6888 goto error;
6889 if (equal_params)
6890 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6892 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6893 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6895 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6896 error:
6897 isl_pw_aff_free(pa);
6898 isl_multi_pw_aff_free(mpa);
6899 return NULL;
6902 /* Compute the pullback of "pa" by the function represented by "mpa".
6903 * In other words, plug in "mpa" in "pa".
6904 * "pa" and "mpa" are assumed to have been aligned.
6906 * The pullback is computed by applying "pa" to "mpa".
6908 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6909 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6911 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6914 /* Compute the pullback of "pa" by the function represented by "mpa".
6915 * In other words, plug in "mpa" in "pa".
6917 * The pullback is computed by applying "pa" to "mpa".
6919 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6920 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6922 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6925 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6926 * In other words, plug in "mpa2" in "mpa1".
6928 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6930 * We pullback each member of "mpa1" in turn.
6932 static __isl_give isl_multi_pw_aff *
6933 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6934 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6936 int i;
6937 isl_space *space = NULL;
6939 mpa1 = isl_multi_pw_aff_cow(mpa1);
6940 if (!mpa1 || !mpa2)
6941 goto error;
6943 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6944 isl_multi_pw_aff_get_space(mpa1));
6946 for (i = 0; i < mpa1->n; ++i) {
6947 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6948 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6949 if (!mpa1->p[i])
6950 goto error;
6953 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6955 isl_multi_pw_aff_free(mpa2);
6956 return mpa1;
6957 error:
6958 isl_space_free(space);
6959 isl_multi_pw_aff_free(mpa1);
6960 isl_multi_pw_aff_free(mpa2);
6961 return NULL;
6964 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6965 * In other words, plug in "mpa2" in "mpa1".
6967 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6968 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6970 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6971 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6974 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6975 * of "mpa1" and "mpa2" live in the same space, construct map space
6976 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6977 * with this map space as extract argument.
6979 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6980 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6981 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6982 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6984 int match;
6985 isl_space *space1, *space2;
6986 isl_map *res;
6988 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6989 isl_multi_pw_aff_get_space(mpa2));
6990 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6991 isl_multi_pw_aff_get_space(mpa1));
6992 if (!mpa1 || !mpa2)
6993 goto error;
6994 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6995 mpa2->space, isl_dim_out);
6996 if (match < 0)
6997 goto error;
6998 if (!match)
6999 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7000 "range spaces don't match", goto error);
7001 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7002 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7003 space1 = isl_space_map_from_domain_and_range(space1, space2);
7005 res = order(mpa1, mpa2, space1);
7006 isl_multi_pw_aff_free(mpa1);
7007 isl_multi_pw_aff_free(mpa2);
7008 return res;
7009 error:
7010 isl_multi_pw_aff_free(mpa1);
7011 isl_multi_pw_aff_free(mpa2);
7012 return NULL;
7015 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7016 * where the function values are equal. "space" is the space of the result.
7017 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7019 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7020 * in the sequences are equal.
7022 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7023 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7024 __isl_take isl_space *space)
7026 int i, n;
7027 isl_map *res;
7029 res = isl_map_universe(space);
7031 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7032 for (i = 0; i < n; ++i) {
7033 isl_pw_aff *pa1, *pa2;
7034 isl_map *map;
7036 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7037 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7038 map = isl_pw_aff_eq_map(pa1, pa2);
7039 res = isl_map_intersect(res, map);
7042 return res;
7045 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7046 * where the function values are equal.
7048 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7049 __isl_take isl_multi_pw_aff *mpa2)
7051 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7052 &isl_multi_pw_aff_eq_map_on_space);
7055 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7056 * where the function values of "mpa1" is lexicographically satisfies "base"
7057 * compared to that of "mpa2". "space" is the space of the result.
7058 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7060 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7061 * if its i-th element satisfies "base" when compared to
7062 * the i-th element of "mpa2" while all previous elements are
7063 * pairwise equal.
7065 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7066 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7067 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7068 __isl_take isl_pw_aff *pa2),
7069 __isl_take isl_space *space)
7071 int i, n;
7072 isl_map *res, *rest;
7074 res = isl_map_empty(isl_space_copy(space));
7075 rest = isl_map_universe(space);
7077 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7078 for (i = 0; i < n; ++i) {
7079 isl_pw_aff *pa1, *pa2;
7080 isl_map *map;
7082 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7083 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7084 map = base(pa1, pa2);
7085 map = isl_map_intersect(map, isl_map_copy(rest));
7086 res = isl_map_union(res, map);
7088 if (i == n - 1)
7089 continue;
7091 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7092 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7093 map = isl_pw_aff_eq_map(pa1, pa2);
7094 rest = isl_map_intersect(rest, map);
7097 isl_map_free(rest);
7098 return res;
7101 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7102 * where the function value of "mpa1" is lexicographically less than that
7103 * of "mpa2". "space" is the space of the result.
7104 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7106 * "mpa1" is less than "mpa2" if its i-th element is smaller
7107 * than the i-th element of "mpa2" while all previous elements are
7108 * pairwise equal.
7110 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7111 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7112 __isl_take isl_space *space)
7114 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7115 &isl_pw_aff_lt_map, space);
7118 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7119 * where the function value of "mpa1" is lexicographically less than that
7120 * of "mpa2".
7122 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7123 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7125 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7126 &isl_multi_pw_aff_lex_lt_map_on_space);
7129 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7130 * where the function value of "mpa1" is lexicographically greater than that
7131 * of "mpa2". "space" is the space of the result.
7132 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7134 * "mpa1" is greater than "mpa2" if its i-th element is greater
7135 * than the i-th element of "mpa2" while all previous elements are
7136 * pairwise equal.
7138 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7139 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7140 __isl_take isl_space *space)
7142 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7143 &isl_pw_aff_gt_map, space);
7146 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7147 * where the function value of "mpa1" is lexicographically greater than that
7148 * of "mpa2".
7150 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7151 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7153 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7154 &isl_multi_pw_aff_lex_gt_map_on_space);
7157 /* Compare two isl_affs.
7159 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7160 * than "aff2" and 0 if they are equal.
7162 * The order is fairly arbitrary. We do consider expressions that only involve
7163 * earlier dimensions as "smaller".
7165 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7167 int cmp;
7168 int last1, last2;
7170 if (aff1 == aff2)
7171 return 0;
7173 if (!aff1)
7174 return -1;
7175 if (!aff2)
7176 return 1;
7178 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7179 if (cmp != 0)
7180 return cmp;
7182 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7183 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7184 if (last1 != last2)
7185 return last1 - last2;
7187 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7190 /* Compare two isl_pw_affs.
7192 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7193 * than "pa2" and 0 if they are equal.
7195 * The order is fairly arbitrary. We do consider expressions that only involve
7196 * earlier dimensions as "smaller".
7198 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7199 __isl_keep isl_pw_aff *pa2)
7201 int i;
7202 int cmp;
7204 if (pa1 == pa2)
7205 return 0;
7207 if (!pa1)
7208 return -1;
7209 if (!pa2)
7210 return 1;
7212 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7213 if (cmp != 0)
7214 return cmp;
7216 if (pa1->n != pa2->n)
7217 return pa1->n - pa2->n;
7219 for (i = 0; i < pa1->n; ++i) {
7220 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7221 if (cmp != 0)
7222 return cmp;
7223 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7224 if (cmp != 0)
7225 return cmp;
7228 return 0;
7231 /* Return a piecewise affine expression that is equal to "v" on "domain".
7233 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7234 __isl_take isl_val *v)
7236 isl_space *space;
7237 isl_local_space *ls;
7238 isl_aff *aff;
7240 space = isl_set_get_space(domain);
7241 ls = isl_local_space_from_space(space);
7242 aff = isl_aff_val_on_domain(ls, v);
7244 return isl_pw_aff_alloc(domain, aff);
7247 /* Return a multi affine expression that is equal to "mv" on domain
7248 * space "space".
7250 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7251 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7253 int i, n;
7254 isl_space *space2;
7255 isl_local_space *ls;
7256 isl_multi_aff *ma;
7258 if (!space || !mv)
7259 goto error;
7261 n = isl_multi_val_dim(mv, isl_dim_set);
7262 space2 = isl_multi_val_get_space(mv);
7263 space2 = isl_space_align_params(space2, isl_space_copy(space));
7264 space = isl_space_align_params(space, isl_space_copy(space2));
7265 space = isl_space_map_from_domain_and_range(space, space2);
7266 ma = isl_multi_aff_alloc(isl_space_copy(space));
7267 ls = isl_local_space_from_space(isl_space_domain(space));
7268 for (i = 0; i < n; ++i) {
7269 isl_val *v;
7270 isl_aff *aff;
7272 v = isl_multi_val_get_val(mv, i);
7273 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7274 ma = isl_multi_aff_set_aff(ma, i, aff);
7276 isl_local_space_free(ls);
7278 isl_multi_val_free(mv);
7279 return ma;
7280 error:
7281 isl_space_free(space);
7282 isl_multi_val_free(mv);
7283 return NULL;
7286 /* Return a piecewise multi-affine expression
7287 * that is equal to "mv" on "domain".
7289 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7290 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7292 isl_space *space;
7293 isl_multi_aff *ma;
7295 space = isl_set_get_space(domain);
7296 ma = isl_multi_aff_multi_val_on_space(space, mv);
7298 return isl_pw_multi_aff_alloc(domain, ma);
7301 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7302 * mv is the value that should be attained on each domain set
7303 * res collects the results
7305 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7306 isl_multi_val *mv;
7307 isl_union_pw_multi_aff *res;
7310 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7311 * and add it to data->res.
7313 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7314 void *user)
7316 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7317 isl_pw_multi_aff *pma;
7318 isl_multi_val *mv;
7320 mv = isl_multi_val_copy(data->mv);
7321 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7322 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7324 return data->res ? isl_stat_ok : isl_stat_error;
7327 /* Return a union piecewise multi-affine expression
7328 * that is equal to "mv" on "domain".
7330 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7331 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7333 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7334 isl_space *space;
7336 space = isl_union_set_get_space(domain);
7337 data.res = isl_union_pw_multi_aff_empty(space);
7338 data.mv = mv;
7339 if (isl_union_set_foreach_set(domain,
7340 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7341 data.res = isl_union_pw_multi_aff_free(data.res);
7342 isl_union_set_free(domain);
7343 isl_multi_val_free(mv);
7344 return data.res;
7347 /* Compute the pullback of data->pma by the function represented by "pma2",
7348 * provided the spaces match, and add the results to data->res.
7350 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7352 struct isl_union_pw_multi_aff_bin_data *data = user;
7354 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7355 pma2->dim, isl_dim_out)) {
7356 isl_pw_multi_aff_free(pma2);
7357 return isl_stat_ok;
7360 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7361 isl_pw_multi_aff_copy(data->pma), pma2);
7363 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7364 if (!data->res)
7365 return isl_stat_error;
7367 return isl_stat_ok;
7370 /* Compute the pullback of "upma1" by the function represented by "upma2".
7372 __isl_give isl_union_pw_multi_aff *
7373 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7374 __isl_take isl_union_pw_multi_aff *upma1,
7375 __isl_take isl_union_pw_multi_aff *upma2)
7377 return bin_op(upma1, upma2, &pullback_entry);
7380 /* Check that the domain space of "upa" matches "space".
7382 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7383 * can in principle never fail since the space "space" is that
7384 * of the isl_multi_union_pw_aff and is a set space such that
7385 * there is no domain space to match.
7387 * We check the parameters and double-check that "space" is
7388 * indeed that of a set.
7390 static isl_stat isl_union_pw_aff_check_match_domain_space(
7391 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7393 isl_space *upa_space;
7394 isl_bool match;
7396 if (!upa || !space)
7397 return isl_stat_error;
7399 match = isl_space_is_set(space);
7400 if (match < 0)
7401 return isl_stat_error;
7402 if (!match)
7403 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7404 "expecting set space", return -1);
7406 upa_space = isl_union_pw_aff_get_space(upa);
7407 match = isl_space_has_equal_params(space, upa_space);
7408 if (match < 0)
7409 goto error;
7410 if (!match)
7411 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7412 "parameters don't match", goto error);
7414 isl_space_free(upa_space);
7415 return isl_stat_ok;
7416 error:
7417 isl_space_free(upa_space);
7418 return isl_stat_error;
7421 /* Do the parameters of "upa" match those of "space"?
7423 static isl_bool isl_union_pw_aff_matching_params(
7424 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7426 isl_space *upa_space;
7427 isl_bool match;
7429 if (!upa || !space)
7430 return isl_bool_error;
7432 upa_space = isl_union_pw_aff_get_space(upa);
7434 match = isl_space_has_equal_params(space, upa_space);
7436 isl_space_free(upa_space);
7437 return match;
7440 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7441 * space represents the new parameters.
7442 * res collects the results.
7444 struct isl_union_pw_aff_reset_params_data {
7445 isl_space *space;
7446 isl_union_pw_aff *res;
7449 /* Replace the parameters of "pa" by data->space and
7450 * add the result to data->res.
7452 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7454 struct isl_union_pw_aff_reset_params_data *data = user;
7455 isl_space *space;
7457 space = isl_pw_aff_get_space(pa);
7458 space = isl_space_replace_params(space, data->space);
7459 pa = isl_pw_aff_reset_space(pa, space);
7460 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7462 return data->res ? isl_stat_ok : isl_stat_error;
7465 /* Replace the domain space of "upa" by "space".
7466 * Since a union expression does not have a (single) domain space,
7467 * "space" is necessarily a parameter space.
7469 * Since the order and the names of the parameters determine
7470 * the hash value, we need to create a new hash table.
7472 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7473 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7475 struct isl_union_pw_aff_reset_params_data data = { space };
7476 isl_bool match;
7478 match = isl_union_pw_aff_matching_params(upa, space);
7479 if (match < 0)
7480 upa = isl_union_pw_aff_free(upa);
7481 else if (match) {
7482 isl_space_free(space);
7483 return upa;
7486 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7487 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7488 data.res = isl_union_pw_aff_free(data.res);
7490 isl_union_pw_aff_free(upa);
7491 isl_space_free(space);
7492 return data.res;
7495 /* Return the floor of "pa".
7497 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7499 return isl_pw_aff_floor(pa);
7502 /* Given f, return floor(f).
7504 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7505 __isl_take isl_union_pw_aff *upa)
7507 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7510 /* Compute
7512 * upa mod m = upa - m * floor(upa/m)
7514 * with m an integer value.
7516 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7517 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7519 isl_union_pw_aff *res;
7521 if (!upa || !m)
7522 goto error;
7524 if (!isl_val_is_int(m))
7525 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7526 "expecting integer modulo", goto error);
7527 if (!isl_val_is_pos(m))
7528 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7529 "expecting positive modulo", goto error);
7531 res = isl_union_pw_aff_copy(upa);
7532 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7533 upa = isl_union_pw_aff_floor(upa);
7534 upa = isl_union_pw_aff_scale_val(upa, m);
7535 res = isl_union_pw_aff_sub(res, upa);
7537 return res;
7538 error:
7539 isl_val_free(m);
7540 isl_union_pw_aff_free(upa);
7541 return NULL;
7544 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7545 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7546 * needs to attain.
7547 * "res" collects the results.
7549 struct isl_union_pw_aff_aff_on_domain_data {
7550 isl_aff *aff;
7551 isl_union_pw_aff *res;
7554 /* Construct a piecewise affine expression that is equal to data->aff
7555 * on "domain" and add the result to data->res.
7557 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7559 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7560 isl_pw_aff *pa;
7561 isl_aff *aff;
7562 int dim;
7564 aff = isl_aff_copy(data->aff);
7565 dim = isl_set_dim(domain, isl_dim_set);
7566 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7567 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7568 pa = isl_pw_aff_alloc(domain, aff);
7569 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7571 return data->res ? isl_stat_ok : isl_stat_error;
7574 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7575 * pos is the output position that needs to be extracted.
7576 * res collects the results.
7578 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7579 int pos;
7580 isl_union_pw_aff *res;
7583 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7584 * (assuming it has such a dimension) and add it to data->res.
7586 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7588 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7589 int n_out;
7590 isl_pw_aff *pa;
7592 if (!pma)
7593 return isl_stat_error;
7595 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7596 if (data->pos >= n_out) {
7597 isl_pw_multi_aff_free(pma);
7598 return isl_stat_ok;
7601 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7602 isl_pw_multi_aff_free(pma);
7604 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7606 return data->res ? isl_stat_ok : isl_stat_error;
7609 /* Extract an isl_union_pw_aff corresponding to
7610 * output dimension "pos" of "upma".
7612 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7613 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7615 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7616 isl_space *space;
7618 if (!upma)
7619 return NULL;
7621 if (pos < 0)
7622 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7623 "cannot extract at negative position", return NULL);
7625 space = isl_union_pw_multi_aff_get_space(upma);
7626 data.res = isl_union_pw_aff_empty(space);
7627 data.pos = pos;
7628 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7629 &get_union_pw_aff, &data) < 0)
7630 data.res = isl_union_pw_aff_free(data.res);
7632 return data.res;
7635 /* Return a union piecewise affine expression
7636 * that is equal to "aff" on "domain", assuming "domain" and "aff"
7637 * have been aligned.
7639 * Construct an isl_pw_aff on each of the sets in "domain" and
7640 * collect the results.
7642 static __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain_aligned(
7643 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7645 struct isl_union_pw_aff_aff_on_domain_data data;
7646 isl_space *space;
7648 space = isl_union_set_get_space(domain);
7649 data.res = isl_union_pw_aff_empty(space);
7650 data.aff = aff;
7651 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7652 data.res = isl_union_pw_aff_free(data.res);
7653 isl_union_set_free(domain);
7654 isl_aff_free(aff);
7655 return data.res;
7658 /* Return a union piecewise affine expression
7659 * that is equal to "aff" on "domain".
7661 * Check that "aff" is a parametric expression,
7662 * align the parameters if needed and call
7663 * isl_union_pw_aff_aff_on_domain_aligned.
7665 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7666 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7668 isl_bool domain_is_params;
7669 isl_bool equal_params;
7670 isl_space *domain_space, *aff_space;
7672 if (!domain || !aff)
7673 goto error;
7674 domain_is_params = isl_local_space_is_params(aff->ls);
7675 if (domain_is_params < 0)
7676 goto error;
7677 if (!domain_is_params)
7678 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7679 "expecting parametric expression", goto error);
7681 domain_space = isl_union_set_get_space(domain);
7682 aff_space = isl_aff_get_domain_space(aff);
7683 equal_params = isl_space_has_equal_params(domain_space, aff_space);
7684 if (equal_params >= 0 && !equal_params) {
7685 isl_space *space;
7687 space = isl_space_align_params(domain_space, aff_space);
7688 aff = isl_aff_align_params(aff, isl_space_copy(space));
7689 domain = isl_union_set_align_params(domain, space);
7690 } else {
7691 isl_space_free(domain_space);
7692 isl_space_free(aff_space);
7695 if (equal_params < 0)
7696 goto error;
7697 return isl_union_pw_aff_aff_on_domain_aligned(domain, aff);
7698 error:
7699 isl_union_set_free(domain);
7700 isl_aff_free(aff);
7701 return NULL;
7704 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7705 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7706 * "res" collects the results.
7708 struct isl_union_pw_aff_val_on_domain_data {
7709 isl_val *v;
7710 isl_union_pw_aff *res;
7713 /* Construct a piecewise affine expression that is equal to data->v
7714 * on "domain" and add the result to data->res.
7716 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7718 struct isl_union_pw_aff_val_on_domain_data *data = user;
7719 isl_pw_aff *pa;
7720 isl_val *v;
7722 v = isl_val_copy(data->v);
7723 pa = isl_pw_aff_val_on_domain(domain, v);
7724 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7726 return data->res ? isl_stat_ok : isl_stat_error;
7729 /* Return a union piecewise affine expression
7730 * that is equal to "v" on "domain".
7732 * Construct an isl_pw_aff on each of the sets in "domain" and
7733 * collect the results.
7735 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7736 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7738 struct isl_union_pw_aff_val_on_domain_data data;
7739 isl_space *space;
7741 space = isl_union_set_get_space(domain);
7742 data.res = isl_union_pw_aff_empty(space);
7743 data.v = v;
7744 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7745 data.res = isl_union_pw_aff_free(data.res);
7746 isl_union_set_free(domain);
7747 isl_val_free(v);
7748 return data.res;
7751 /* Construct a piecewise multi affine expression
7752 * that is equal to "pa" and add it to upma.
7754 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7755 void *user)
7757 isl_union_pw_multi_aff **upma = user;
7758 isl_pw_multi_aff *pma;
7760 pma = isl_pw_multi_aff_from_pw_aff(pa);
7761 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7763 return *upma ? isl_stat_ok : isl_stat_error;
7766 /* Construct and return a union piecewise multi affine expression
7767 * that is equal to the given union piecewise affine expression.
7769 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7770 __isl_take isl_union_pw_aff *upa)
7772 isl_space *space;
7773 isl_union_pw_multi_aff *upma;
7775 if (!upa)
7776 return NULL;
7778 space = isl_union_pw_aff_get_space(upa);
7779 upma = isl_union_pw_multi_aff_empty(space);
7781 if (isl_union_pw_aff_foreach_pw_aff(upa,
7782 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7783 upma = isl_union_pw_multi_aff_free(upma);
7785 isl_union_pw_aff_free(upa);
7786 return upma;
7789 /* Compute the set of elements in the domain of "pa" where it is zero and
7790 * add this set to "uset".
7792 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7794 isl_union_set **uset = (isl_union_set **)user;
7796 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7798 return *uset ? isl_stat_ok : isl_stat_error;
7801 /* Return a union set containing those elements in the domain
7802 * of "upa" where it is zero.
7804 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7805 __isl_take isl_union_pw_aff *upa)
7807 isl_union_set *zero;
7809 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7810 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7811 zero = isl_union_set_free(zero);
7813 isl_union_pw_aff_free(upa);
7814 return zero;
7817 /* Convert "pa" to an isl_map and add it to *umap.
7819 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7821 isl_union_map **umap = user;
7822 isl_map *map;
7824 map = isl_map_from_pw_aff(pa);
7825 *umap = isl_union_map_add_map(*umap, map);
7827 return *umap ? isl_stat_ok : isl_stat_error;
7830 /* Construct a union map mapping the domain of the union
7831 * piecewise affine expression to its range, with the single output dimension
7832 * equated to the corresponding affine expressions on their cells.
7834 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7835 __isl_take isl_union_pw_aff *upa)
7837 isl_space *space;
7838 isl_union_map *umap;
7840 if (!upa)
7841 return NULL;
7843 space = isl_union_pw_aff_get_space(upa);
7844 umap = isl_union_map_empty(space);
7846 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7847 &umap) < 0)
7848 umap = isl_union_map_free(umap);
7850 isl_union_pw_aff_free(upa);
7851 return umap;
7854 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7855 * upma is the function that is plugged in.
7856 * pa is the current part of the function in which upma is plugged in.
7857 * res collects the results.
7859 struct isl_union_pw_aff_pullback_upma_data {
7860 isl_union_pw_multi_aff *upma;
7861 isl_pw_aff *pa;
7862 isl_union_pw_aff *res;
7865 /* Check if "pma" can be plugged into data->pa.
7866 * If so, perform the pullback and add the result to data->res.
7868 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7870 struct isl_union_pw_aff_pullback_upma_data *data = user;
7871 isl_pw_aff *pa;
7873 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7874 pma->dim, isl_dim_out)) {
7875 isl_pw_multi_aff_free(pma);
7876 return isl_stat_ok;
7879 pa = isl_pw_aff_copy(data->pa);
7880 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7882 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7884 return data->res ? isl_stat_ok : isl_stat_error;
7887 /* Check if any of the elements of data->upma can be plugged into pa,
7888 * add if so add the result to data->res.
7890 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7892 struct isl_union_pw_aff_pullback_upma_data *data = user;
7893 isl_stat r;
7895 data->pa = pa;
7896 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7897 &pa_pb_pma, data);
7898 isl_pw_aff_free(pa);
7900 return r;
7903 /* Compute the pullback of "upa" by the function represented by "upma".
7904 * In other words, plug in "upma" in "upa". The result contains
7905 * expressions defined over the domain space of "upma".
7907 * Run over all pairs of elements in "upa" and "upma", perform
7908 * the pullback when appropriate and collect the results.
7909 * If the hash value were based on the domain space rather than
7910 * the function space, then we could run through all elements
7911 * of "upma" and directly pick out the corresponding element of "upa".
7913 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7914 __isl_take isl_union_pw_aff *upa,
7915 __isl_take isl_union_pw_multi_aff *upma)
7917 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7918 isl_space *space;
7920 space = isl_union_pw_multi_aff_get_space(upma);
7921 upa = isl_union_pw_aff_align_params(upa, space);
7922 space = isl_union_pw_aff_get_space(upa);
7923 upma = isl_union_pw_multi_aff_align_params(upma, space);
7925 if (!upa || !upma)
7926 goto error;
7928 data.upma = upma;
7929 data.res = isl_union_pw_aff_alloc_same_size(upa);
7930 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7931 data.res = isl_union_pw_aff_free(data.res);
7933 isl_union_pw_aff_free(upa);
7934 isl_union_pw_multi_aff_free(upma);
7935 return data.res;
7936 error:
7937 isl_union_pw_aff_free(upa);
7938 isl_union_pw_multi_aff_free(upma);
7939 return NULL;
7942 #undef BASE
7943 #define BASE union_pw_aff
7944 #undef DOMBASE
7945 #define DOMBASE union_set
7947 #define NO_MOVE_DIMS
7948 #define NO_DOMAIN
7949 #define NO_PRODUCT
7950 #define NO_SPLICE
7951 #define NO_ZERO
7952 #define NO_IDENTITY
7953 #define NO_GIST
7955 #include <isl_multi_templ.c>
7956 #include <isl_multi_apply_set.c>
7957 #include <isl_multi_apply_union_set.c>
7958 #include <isl_multi_coalesce.c>
7959 #include <isl_multi_floor.c>
7960 #include <isl_multi_gist.c>
7961 #include <isl_multi_intersect.c>
7963 /* Construct a multiple union piecewise affine expression
7964 * in the given space with value zero in each of the output dimensions.
7966 * Since there is no canonical zero value for
7967 * a union piecewise affine expression, we can only construct
7968 * a zero-dimensional "zero" value.
7970 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7971 __isl_take isl_space *space)
7973 isl_bool params;
7975 if (!space)
7976 return NULL;
7978 params = isl_space_is_params(space);
7979 if (params < 0)
7980 goto error;
7981 if (params)
7982 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7983 "expecting proper set space", goto error);
7984 if (!isl_space_is_set(space))
7985 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7986 "expecting set space", goto error);
7987 if (isl_space_dim(space , isl_dim_out) != 0)
7988 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7989 "expecting 0D space", goto error);
7991 return isl_multi_union_pw_aff_alloc(space);
7992 error:
7993 isl_space_free(space);
7994 return NULL;
7997 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7998 * with the actual sum on the shared domain and
7999 * the defined expression on the symmetric difference of the domains.
8001 * We simply iterate over the elements in both arguments and
8002 * call isl_union_pw_aff_union_add on each of them.
8004 static __isl_give isl_multi_union_pw_aff *
8005 isl_multi_union_pw_aff_union_add_aligned(
8006 __isl_take isl_multi_union_pw_aff *mupa1,
8007 __isl_take isl_multi_union_pw_aff *mupa2)
8009 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8010 &isl_union_pw_aff_union_add);
8013 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8014 * with the actual sum on the shared domain and
8015 * the defined expression on the symmetric difference of the domains.
8017 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8018 __isl_take isl_multi_union_pw_aff *mupa1,
8019 __isl_take isl_multi_union_pw_aff *mupa2)
8021 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8022 &isl_multi_union_pw_aff_union_add_aligned);
8025 /* Construct and return a multi union piecewise affine expression
8026 * that is equal to the given multi affine expression.
8028 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8029 __isl_take isl_multi_aff *ma)
8031 isl_multi_pw_aff *mpa;
8033 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8034 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8037 /* Construct and return a multi union piecewise affine expression
8038 * that is equal to the given multi piecewise affine expression.
8040 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8041 __isl_take isl_multi_pw_aff *mpa)
8043 int i, n;
8044 isl_space *space;
8045 isl_multi_union_pw_aff *mupa;
8047 if (!mpa)
8048 return NULL;
8050 space = isl_multi_pw_aff_get_space(mpa);
8051 space = isl_space_range(space);
8052 mupa = isl_multi_union_pw_aff_alloc(space);
8054 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8055 for (i = 0; i < n; ++i) {
8056 isl_pw_aff *pa;
8057 isl_union_pw_aff *upa;
8059 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8060 upa = isl_union_pw_aff_from_pw_aff(pa);
8061 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8064 isl_multi_pw_aff_free(mpa);
8066 return mupa;
8069 /* Extract the range space of "pma" and assign it to *space.
8070 * If *space has already been set (through a previous call to this function),
8071 * then check that the range space is the same.
8073 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8075 isl_space **space = user;
8076 isl_space *pma_space;
8077 isl_bool equal;
8079 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8080 isl_pw_multi_aff_free(pma);
8082 if (!pma_space)
8083 return isl_stat_error;
8084 if (!*space) {
8085 *space = pma_space;
8086 return isl_stat_ok;
8089 equal = isl_space_is_equal(pma_space, *space);
8090 isl_space_free(pma_space);
8092 if (equal < 0)
8093 return isl_stat_error;
8094 if (!equal)
8095 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8096 "range spaces not the same", return isl_stat_error);
8097 return isl_stat_ok;
8100 /* Construct and return a multi union piecewise affine expression
8101 * that is equal to the given union piecewise multi affine expression.
8103 * In order to be able to perform the conversion, the input
8104 * needs to be non-empty and may only involve a single range space.
8106 __isl_give isl_multi_union_pw_aff *
8107 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8108 __isl_take isl_union_pw_multi_aff *upma)
8110 isl_space *space = NULL;
8111 isl_multi_union_pw_aff *mupa;
8112 int i, n;
8114 if (!upma)
8115 return NULL;
8116 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8117 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8118 "cannot extract range space from empty input",
8119 goto error);
8120 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8121 &space) < 0)
8122 goto error;
8124 if (!space)
8125 goto error;
8127 n = isl_space_dim(space, isl_dim_set);
8128 mupa = isl_multi_union_pw_aff_alloc(space);
8130 for (i = 0; i < n; ++i) {
8131 isl_union_pw_aff *upa;
8133 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8134 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8137 isl_union_pw_multi_aff_free(upma);
8138 return mupa;
8139 error:
8140 isl_space_free(space);
8141 isl_union_pw_multi_aff_free(upma);
8142 return NULL;
8145 /* Try and create an isl_multi_union_pw_aff that is equivalent
8146 * to the given isl_union_map.
8147 * The isl_union_map is required to be single-valued in each space.
8148 * Moreover, it cannot be empty and all range spaces need to be the same.
8149 * Otherwise, an error is produced.
8151 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8152 __isl_take isl_union_map *umap)
8154 isl_union_pw_multi_aff *upma;
8156 upma = isl_union_pw_multi_aff_from_union_map(umap);
8157 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8160 /* Return a multiple union piecewise affine expression
8161 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8162 * have been aligned.
8164 static __isl_give isl_multi_union_pw_aff *
8165 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8166 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8168 int i, n;
8169 isl_space *space;
8170 isl_multi_union_pw_aff *mupa;
8172 if (!domain || !mv)
8173 goto error;
8175 n = isl_multi_val_dim(mv, isl_dim_set);
8176 space = isl_multi_val_get_space(mv);
8177 mupa = isl_multi_union_pw_aff_alloc(space);
8178 for (i = 0; i < n; ++i) {
8179 isl_val *v;
8180 isl_union_pw_aff *upa;
8182 v = isl_multi_val_get_val(mv, i);
8183 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8185 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8188 isl_union_set_free(domain);
8189 isl_multi_val_free(mv);
8190 return mupa;
8191 error:
8192 isl_union_set_free(domain);
8193 isl_multi_val_free(mv);
8194 return NULL;
8197 /* Return a multiple union piecewise affine expression
8198 * that is equal to "mv" on "domain".
8200 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8201 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8203 isl_bool equal_params;
8205 if (!domain || !mv)
8206 goto error;
8207 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8208 if (equal_params < 0)
8209 goto error;
8210 if (equal_params)
8211 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8212 domain, mv);
8213 domain = isl_union_set_align_params(domain,
8214 isl_multi_val_get_space(mv));
8215 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8216 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8217 error:
8218 isl_union_set_free(domain);
8219 isl_multi_val_free(mv);
8220 return NULL;
8223 /* Return a multiple union piecewise affine expression
8224 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8225 * have been aligned.
8227 static __isl_give isl_multi_union_pw_aff *
8228 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8229 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8231 int i, n;
8232 isl_space *space;
8233 isl_multi_union_pw_aff *mupa;
8235 if (!domain || !ma)
8236 goto error;
8238 n = isl_multi_aff_dim(ma, isl_dim_set);
8239 space = isl_multi_aff_get_space(ma);
8240 mupa = isl_multi_union_pw_aff_alloc(space);
8241 for (i = 0; i < n; ++i) {
8242 isl_aff *aff;
8243 isl_union_pw_aff *upa;
8245 aff = isl_multi_aff_get_aff(ma, i);
8246 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8247 aff);
8248 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8251 isl_union_set_free(domain);
8252 isl_multi_aff_free(ma);
8253 return mupa;
8254 error:
8255 isl_union_set_free(domain);
8256 isl_multi_aff_free(ma);
8257 return NULL;
8260 /* Return a multiple union piecewise affine expression
8261 * that is equal to "ma" on "domain".
8263 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8264 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8266 isl_bool equal_params;
8268 if (!domain || !ma)
8269 goto error;
8270 equal_params = isl_space_has_equal_params(domain->dim, ma->space);
8271 if (equal_params < 0)
8272 goto error;
8273 if (equal_params)
8274 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8275 domain, ma);
8276 domain = isl_union_set_align_params(domain,
8277 isl_multi_aff_get_space(ma));
8278 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8279 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8280 error:
8281 isl_union_set_free(domain);
8282 isl_multi_aff_free(ma);
8283 return NULL;
8286 /* Return a union set containing those elements in the domains
8287 * of the elements of "mupa" where they are all zero.
8289 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8290 __isl_take isl_multi_union_pw_aff *mupa)
8292 int i, n;
8293 isl_union_pw_aff *upa;
8294 isl_union_set *zero;
8296 if (!mupa)
8297 return NULL;
8299 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8300 if (n == 0)
8301 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8302 "cannot determine zero set "
8303 "of zero-dimensional function", goto error);
8305 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8306 zero = isl_union_pw_aff_zero_union_set(upa);
8308 for (i = 1; i < n; ++i) {
8309 isl_union_set *zero_i;
8311 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8312 zero_i = isl_union_pw_aff_zero_union_set(upa);
8314 zero = isl_union_set_intersect(zero, zero_i);
8317 isl_multi_union_pw_aff_free(mupa);
8318 return zero;
8319 error:
8320 isl_multi_union_pw_aff_free(mupa);
8321 return NULL;
8324 /* Construct a union map mapping the shared domain
8325 * of the union piecewise affine expressions to the range of "mupa"
8326 * with each dimension in the range equated to the
8327 * corresponding union piecewise affine expression.
8329 * The input cannot be zero-dimensional as there is
8330 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8332 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8333 __isl_take isl_multi_union_pw_aff *mupa)
8335 int i, n;
8336 isl_space *space;
8337 isl_union_map *umap;
8338 isl_union_pw_aff *upa;
8340 if (!mupa)
8341 return NULL;
8343 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8344 if (n == 0)
8345 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8346 "cannot determine domain of zero-dimensional "
8347 "isl_multi_union_pw_aff", goto error);
8349 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8350 umap = isl_union_map_from_union_pw_aff(upa);
8352 for (i = 1; i < n; ++i) {
8353 isl_union_map *umap_i;
8355 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8356 umap_i = isl_union_map_from_union_pw_aff(upa);
8357 umap = isl_union_map_flat_range_product(umap, umap_i);
8360 space = isl_multi_union_pw_aff_get_space(mupa);
8361 umap = isl_union_map_reset_range_space(umap, space);
8363 isl_multi_union_pw_aff_free(mupa);
8364 return umap;
8365 error:
8366 isl_multi_union_pw_aff_free(mupa);
8367 return NULL;
8370 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8371 * "range" is the space from which to set the range space.
8372 * "res" collects the results.
8374 struct isl_union_pw_multi_aff_reset_range_space_data {
8375 isl_space *range;
8376 isl_union_pw_multi_aff *res;
8379 /* Replace the range space of "pma" by the range space of data->range and
8380 * add the result to data->res.
8382 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8384 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8385 isl_space *space;
8387 space = isl_pw_multi_aff_get_space(pma);
8388 space = isl_space_domain(space);
8389 space = isl_space_extend_domain_with_range(space,
8390 isl_space_copy(data->range));
8391 pma = isl_pw_multi_aff_reset_space(pma, space);
8392 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8394 return data->res ? isl_stat_ok : isl_stat_error;
8397 /* Replace the range space of all the piecewise affine expressions in "upma" by
8398 * the range space of "space".
8400 * This assumes that all these expressions have the same output dimension.
8402 * Since the spaces of the expressions change, so do their hash values.
8403 * We therefore need to create a new isl_union_pw_multi_aff.
8404 * Note that the hash value is currently computed based on the entire
8405 * space even though there can only be a single expression with a given
8406 * domain space.
8408 static __isl_give isl_union_pw_multi_aff *
8409 isl_union_pw_multi_aff_reset_range_space(
8410 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8412 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8413 isl_space *space_upma;
8415 space_upma = isl_union_pw_multi_aff_get_space(upma);
8416 data.res = isl_union_pw_multi_aff_empty(space_upma);
8417 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8418 &reset_range_space, &data) < 0)
8419 data.res = isl_union_pw_multi_aff_free(data.res);
8421 isl_space_free(space);
8422 isl_union_pw_multi_aff_free(upma);
8423 return data.res;
8426 /* Construct and return a union piecewise multi affine expression
8427 * that is equal to the given multi union piecewise affine expression.
8429 * In order to be able to perform the conversion, the input
8430 * needs to have a least one output dimension.
8432 __isl_give isl_union_pw_multi_aff *
8433 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8434 __isl_take isl_multi_union_pw_aff *mupa)
8436 int i, n;
8437 isl_space *space;
8438 isl_union_pw_multi_aff *upma;
8439 isl_union_pw_aff *upa;
8441 if (!mupa)
8442 return NULL;
8444 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8445 if (n == 0)
8446 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8447 "cannot determine domain of zero-dimensional "
8448 "isl_multi_union_pw_aff", goto error);
8450 space = isl_multi_union_pw_aff_get_space(mupa);
8451 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8452 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8454 for (i = 1; i < n; ++i) {
8455 isl_union_pw_multi_aff *upma_i;
8457 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8458 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8459 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8462 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8464 isl_multi_union_pw_aff_free(mupa);
8465 return upma;
8466 error:
8467 isl_multi_union_pw_aff_free(mupa);
8468 return NULL;
8471 /* Intersect the range of "mupa" with "range".
8472 * That is, keep only those domain elements that have a function value
8473 * in "range".
8475 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8476 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8478 isl_union_pw_multi_aff *upma;
8479 isl_union_set *domain;
8480 isl_space *space;
8481 int n;
8482 int match;
8484 if (!mupa || !range)
8485 goto error;
8487 space = isl_set_get_space(range);
8488 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8489 space, isl_dim_set);
8490 isl_space_free(space);
8491 if (match < 0)
8492 goto error;
8493 if (!match)
8494 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8495 "space don't match", goto error);
8496 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8497 if (n == 0)
8498 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8499 "cannot intersect range of zero-dimensional "
8500 "isl_multi_union_pw_aff", goto error);
8502 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8503 isl_multi_union_pw_aff_copy(mupa));
8504 domain = isl_union_set_from_set(range);
8505 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8506 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8508 return mupa;
8509 error:
8510 isl_multi_union_pw_aff_free(mupa);
8511 isl_set_free(range);
8512 return NULL;
8515 /* Return the shared domain of the elements of "mupa".
8517 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8518 __isl_take isl_multi_union_pw_aff *mupa)
8520 int i, n;
8521 isl_union_pw_aff *upa;
8522 isl_union_set *dom;
8524 if (!mupa)
8525 return NULL;
8527 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8528 if (n == 0)
8529 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8530 "cannot determine domain", goto error);
8532 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8533 dom = isl_union_pw_aff_domain(upa);
8534 for (i = 1; i < n; ++i) {
8535 isl_union_set *dom_i;
8537 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8538 dom_i = isl_union_pw_aff_domain(upa);
8539 dom = isl_union_set_intersect(dom, dom_i);
8542 isl_multi_union_pw_aff_free(mupa);
8543 return dom;
8544 error:
8545 isl_multi_union_pw_aff_free(mupa);
8546 return NULL;
8549 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8550 * In particular, the spaces have been aligned.
8551 * The result is defined over the shared domain of the elements of "mupa"
8553 * We first extract the parametric constant part of "aff" and
8554 * define that over the shared domain.
8555 * Then we iterate over all input dimensions of "aff" and add the corresponding
8556 * multiples of the elements of "mupa".
8557 * Finally, we consider the integer divisions, calling the function
8558 * recursively to obtain an isl_union_pw_aff corresponding to the
8559 * integer division argument.
8561 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8562 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8564 int i, n_in, n_div;
8565 isl_union_pw_aff *upa;
8566 isl_union_set *uset;
8567 isl_val *v;
8568 isl_aff *cst;
8570 n_in = isl_aff_dim(aff, isl_dim_in);
8571 n_div = isl_aff_dim(aff, isl_dim_div);
8573 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8574 cst = isl_aff_copy(aff);
8575 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8576 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8577 cst = isl_aff_project_domain_on_params(cst);
8578 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8580 for (i = 0; i < n_in; ++i) {
8581 isl_union_pw_aff *upa_i;
8583 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8584 continue;
8585 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8586 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8587 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8588 upa = isl_union_pw_aff_add(upa, upa_i);
8591 for (i = 0; i < n_div; ++i) {
8592 isl_aff *div;
8593 isl_union_pw_aff *upa_i;
8595 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8596 continue;
8597 div = isl_aff_get_div(aff, i);
8598 upa_i = multi_union_pw_aff_apply_aff(
8599 isl_multi_union_pw_aff_copy(mupa), div);
8600 upa_i = isl_union_pw_aff_floor(upa_i);
8601 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8602 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8603 upa = isl_union_pw_aff_add(upa, upa_i);
8606 isl_multi_union_pw_aff_free(mupa);
8607 isl_aff_free(aff);
8609 return upa;
8612 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8613 * with the domain of "aff".
8614 * Furthermore, the dimension of this space needs to be greater than zero.
8615 * The result is defined over the shared domain of the elements of "mupa"
8617 * We perform these checks and then hand over control to
8618 * multi_union_pw_aff_apply_aff.
8620 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8621 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8623 isl_space *space1, *space2;
8624 int equal;
8626 mupa = isl_multi_union_pw_aff_align_params(mupa,
8627 isl_aff_get_space(aff));
8628 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8629 if (!mupa || !aff)
8630 goto error;
8632 space1 = isl_multi_union_pw_aff_get_space(mupa);
8633 space2 = isl_aff_get_domain_space(aff);
8634 equal = isl_space_is_equal(space1, space2);
8635 isl_space_free(space1);
8636 isl_space_free(space2);
8637 if (equal < 0)
8638 goto error;
8639 if (!equal)
8640 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8641 "spaces don't match", goto error);
8642 if (isl_aff_dim(aff, isl_dim_in) == 0)
8643 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8644 "cannot determine domains", goto error);
8646 return multi_union_pw_aff_apply_aff(mupa, aff);
8647 error:
8648 isl_multi_union_pw_aff_free(mupa);
8649 isl_aff_free(aff);
8650 return NULL;
8653 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8654 * with the domain of "ma".
8655 * Furthermore, the dimension of this space needs to be greater than zero,
8656 * unless the dimension of the target space of "ma" is also zero.
8657 * The result is defined over the shared domain of the elements of "mupa"
8659 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8660 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8662 isl_space *space1, *space2;
8663 isl_multi_union_pw_aff *res;
8664 int equal;
8665 int i, n_out;
8667 mupa = isl_multi_union_pw_aff_align_params(mupa,
8668 isl_multi_aff_get_space(ma));
8669 ma = isl_multi_aff_align_params(ma,
8670 isl_multi_union_pw_aff_get_space(mupa));
8671 if (!mupa || !ma)
8672 goto error;
8674 space1 = isl_multi_union_pw_aff_get_space(mupa);
8675 space2 = isl_multi_aff_get_domain_space(ma);
8676 equal = isl_space_is_equal(space1, space2);
8677 isl_space_free(space1);
8678 isl_space_free(space2);
8679 if (equal < 0)
8680 goto error;
8681 if (!equal)
8682 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8683 "spaces don't match", goto error);
8684 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8685 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8686 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8687 "cannot determine domains", goto error);
8689 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8690 res = isl_multi_union_pw_aff_alloc(space1);
8692 for (i = 0; i < n_out; ++i) {
8693 isl_aff *aff;
8694 isl_union_pw_aff *upa;
8696 aff = isl_multi_aff_get_aff(ma, i);
8697 upa = multi_union_pw_aff_apply_aff(
8698 isl_multi_union_pw_aff_copy(mupa), aff);
8699 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8702 isl_multi_aff_free(ma);
8703 isl_multi_union_pw_aff_free(mupa);
8704 return res;
8705 error:
8706 isl_multi_union_pw_aff_free(mupa);
8707 isl_multi_aff_free(ma);
8708 return NULL;
8711 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8712 * with the domain of "pa".
8713 * Furthermore, the dimension of this space needs to be greater than zero.
8714 * The result is defined over the shared domain of the elements of "mupa"
8716 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8717 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8719 int i;
8720 int equal;
8721 isl_space *space, *space2;
8722 isl_union_pw_aff *upa;
8724 mupa = isl_multi_union_pw_aff_align_params(mupa,
8725 isl_pw_aff_get_space(pa));
8726 pa = isl_pw_aff_align_params(pa,
8727 isl_multi_union_pw_aff_get_space(mupa));
8728 if (!mupa || !pa)
8729 goto error;
8731 space = isl_multi_union_pw_aff_get_space(mupa);
8732 space2 = isl_pw_aff_get_domain_space(pa);
8733 equal = isl_space_is_equal(space, space2);
8734 isl_space_free(space);
8735 isl_space_free(space2);
8736 if (equal < 0)
8737 goto error;
8738 if (!equal)
8739 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8740 "spaces don't match", goto error);
8741 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8742 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8743 "cannot determine domains", goto error);
8745 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8746 upa = isl_union_pw_aff_empty(space);
8748 for (i = 0; i < pa->n; ++i) {
8749 isl_aff *aff;
8750 isl_set *domain;
8751 isl_multi_union_pw_aff *mupa_i;
8752 isl_union_pw_aff *upa_i;
8754 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8755 domain = isl_set_copy(pa->p[i].set);
8756 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8757 aff = isl_aff_copy(pa->p[i].aff);
8758 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8759 upa = isl_union_pw_aff_union_add(upa, upa_i);
8762 isl_multi_union_pw_aff_free(mupa);
8763 isl_pw_aff_free(pa);
8764 return upa;
8765 error:
8766 isl_multi_union_pw_aff_free(mupa);
8767 isl_pw_aff_free(pa);
8768 return NULL;
8771 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8772 * with the domain of "pma".
8773 * Furthermore, the dimension of this space needs to be greater than zero,
8774 * unless the dimension of the target space of "pma" is also zero.
8775 * The result is defined over the shared domain of the elements of "mupa"
8777 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8778 __isl_take isl_multi_union_pw_aff *mupa,
8779 __isl_take isl_pw_multi_aff *pma)
8781 isl_space *space1, *space2;
8782 isl_multi_union_pw_aff *res;
8783 int equal;
8784 int i, n_out;
8786 mupa = isl_multi_union_pw_aff_align_params(mupa,
8787 isl_pw_multi_aff_get_space(pma));
8788 pma = isl_pw_multi_aff_align_params(pma,
8789 isl_multi_union_pw_aff_get_space(mupa));
8790 if (!mupa || !pma)
8791 goto error;
8793 space1 = isl_multi_union_pw_aff_get_space(mupa);
8794 space2 = isl_pw_multi_aff_get_domain_space(pma);
8795 equal = isl_space_is_equal(space1, space2);
8796 isl_space_free(space1);
8797 isl_space_free(space2);
8798 if (equal < 0)
8799 goto error;
8800 if (!equal)
8801 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8802 "spaces don't match", goto error);
8803 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8804 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8805 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8806 "cannot determine domains", goto error);
8808 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8809 res = isl_multi_union_pw_aff_alloc(space1);
8811 for (i = 0; i < n_out; ++i) {
8812 isl_pw_aff *pa;
8813 isl_union_pw_aff *upa;
8815 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8816 upa = isl_multi_union_pw_aff_apply_pw_aff(
8817 isl_multi_union_pw_aff_copy(mupa), pa);
8818 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8821 isl_pw_multi_aff_free(pma);
8822 isl_multi_union_pw_aff_free(mupa);
8823 return res;
8824 error:
8825 isl_multi_union_pw_aff_free(mupa);
8826 isl_pw_multi_aff_free(pma);
8827 return NULL;
8830 /* Compute the pullback of "mupa" by the function represented by "upma".
8831 * In other words, plug in "upma" in "mupa". The result contains
8832 * expressions defined over the domain space of "upma".
8834 * Run over all elements of "mupa" and plug in "upma" in each of them.
8836 __isl_give isl_multi_union_pw_aff *
8837 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8838 __isl_take isl_multi_union_pw_aff *mupa,
8839 __isl_take isl_union_pw_multi_aff *upma)
8841 int i, n;
8843 mupa = isl_multi_union_pw_aff_align_params(mupa,
8844 isl_union_pw_multi_aff_get_space(upma));
8845 upma = isl_union_pw_multi_aff_align_params(upma,
8846 isl_multi_union_pw_aff_get_space(mupa));
8847 if (!mupa || !upma)
8848 goto error;
8850 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8851 for (i = 0; i < n; ++i) {
8852 isl_union_pw_aff *upa;
8854 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8855 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8856 isl_union_pw_multi_aff_copy(upma));
8857 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8860 isl_union_pw_multi_aff_free(upma);
8861 return mupa;
8862 error:
8863 isl_multi_union_pw_aff_free(mupa);
8864 isl_union_pw_multi_aff_free(upma);
8865 return NULL;
8868 /* Extract the sequence of elements in "mupa" with domain space "space"
8869 * (ignoring parameters).
8871 * For the elements of "mupa" that are not defined on the specified space,
8872 * the corresponding element in the result is empty.
8874 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8875 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8877 int i, n;
8878 isl_bool equal_params;
8879 isl_space *space_mpa = NULL;
8880 isl_multi_pw_aff *mpa;
8882 if (!mupa || !space)
8883 goto error;
8885 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8886 equal_params = isl_space_has_equal_params(space_mpa, space);
8887 if (equal_params < 0)
8888 goto error;
8889 if (!equal_params) {
8890 space = isl_space_drop_dims(space, isl_dim_param,
8891 0, isl_space_dim(space, isl_dim_param));
8892 space = isl_space_align_params(space,
8893 isl_space_copy(space_mpa));
8894 if (!space)
8895 goto error;
8897 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8898 space_mpa);
8899 mpa = isl_multi_pw_aff_alloc(space_mpa);
8901 space = isl_space_from_domain(space);
8902 space = isl_space_add_dims(space, isl_dim_out, 1);
8903 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8904 for (i = 0; i < n; ++i) {
8905 isl_union_pw_aff *upa;
8906 isl_pw_aff *pa;
8908 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8909 pa = isl_union_pw_aff_extract_pw_aff(upa,
8910 isl_space_copy(space));
8911 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8912 isl_union_pw_aff_free(upa);
8915 isl_space_free(space);
8916 return mpa;
8917 error:
8918 isl_space_free(space_mpa);
8919 isl_space_free(space);
8920 return NULL;