isl_aff.c: pw_aff_aff_on_domain: explicitly insert domain
[isl.git] / isl_aff.c
blobcab1239dbd67e04965ca44c49443ca2232cf8936
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 /* Return the local space of the domain of "aff".
359 * This may be either a copy or the local space itself
360 * if there is only one reference to "aff".
361 * This allows the local space to be modified inplace
362 * if both the expression and its local space have only a single reference.
363 * The caller is not allowed to modify "aff" between this call and
364 * a subsequent call to isl_aff_restore_domain_local_space.
365 * The only exception is that isl_aff_free can be called instead.
367 __isl_give isl_local_space *isl_aff_take_domain_local_space(
368 __isl_keep isl_aff *aff)
370 isl_local_space *ls;
372 if (!aff)
373 return NULL;
374 if (aff->ref != 1)
375 return isl_aff_get_domain_local_space(aff);
376 ls = aff->ls;
377 aff->ls = NULL;
378 return ls;
381 /* Set the local space of the domain of "aff" to "ls",
382 * where the local space of "aff" may be missing
383 * due to a preceding call to isl_aff_take_domain_local_space.
384 * However, in this case, "aff" only has a single reference and
385 * then the call to isl_aff_cow has no effect.
387 __isl_give isl_aff *isl_aff_restore_domain_local_space(
388 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
390 if (!aff || !ls)
391 goto error;
393 if (aff->ls == ls) {
394 isl_local_space_free(ls);
395 return aff;
398 aff = isl_aff_cow(aff);
399 if (!aff)
400 goto error;
401 isl_local_space_free(aff->ls);
402 aff->ls = ls;
404 return aff;
405 error:
406 isl_aff_free(aff);
407 isl_local_space_free(ls);
408 return NULL;
411 /* Externally, an isl_aff has a map space, but internally, the
412 * ls field corresponds to the domain of that space.
414 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
415 enum isl_dim_type type, unsigned pos)
417 if (!aff)
418 return NULL;
419 if (type == isl_dim_out)
420 return NULL;
421 if (type == isl_dim_in)
422 type = isl_dim_set;
423 return isl_local_space_get_dim_name(aff->ls, type, pos);
426 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
427 __isl_take isl_space *dim)
429 aff = isl_aff_cow(aff);
430 if (!aff || !dim)
431 goto error;
433 aff->ls = isl_local_space_reset_space(aff->ls, dim);
434 if (!aff->ls)
435 return isl_aff_free(aff);
437 return aff;
438 error:
439 isl_aff_free(aff);
440 isl_space_free(dim);
441 return NULL;
444 /* Reset the space of "aff". This function is called from isl_pw_templ.c
445 * and doesn't know if the space of an element object is represented
446 * directly or through its domain. It therefore passes along both.
448 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
449 __isl_take isl_space *space, __isl_take isl_space *domain)
451 isl_space_free(space);
452 return isl_aff_reset_domain_space(aff, domain);
455 /* Reorder the coefficients of the affine expression based
456 * on the given reordering.
457 * The reordering r is assumed to have been extended with the local
458 * variables.
460 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
461 __isl_take isl_reordering *r, int n_div)
463 isl_vec *res;
464 int i;
466 if (!vec || !r)
467 goto error;
469 res = isl_vec_alloc(vec->ctx,
470 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
471 if (!res)
472 goto error;
473 isl_seq_cpy(res->el, vec->el, 2);
474 isl_seq_clr(res->el + 2, res->size - 2);
475 for (i = 0; i < r->len; ++i)
476 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
478 isl_reordering_free(r);
479 isl_vec_free(vec);
480 return res;
481 error:
482 isl_vec_free(vec);
483 isl_reordering_free(r);
484 return NULL;
487 /* Reorder the dimensions of the domain of "aff" according
488 * to the given reordering.
490 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
491 __isl_take isl_reordering *r)
493 aff = isl_aff_cow(aff);
494 if (!aff)
495 goto error;
497 r = isl_reordering_extend(r, aff->ls->div->n_row);
498 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
499 aff->ls->div->n_row);
500 aff->ls = isl_local_space_realign(aff->ls, r);
502 if (!aff->v || !aff->ls)
503 return isl_aff_free(aff);
505 return aff;
506 error:
507 isl_aff_free(aff);
508 isl_reordering_free(r);
509 return NULL;
512 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
513 __isl_take isl_space *model)
515 isl_bool equal_params;
517 if (!aff || !model)
518 goto error;
520 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
521 if (equal_params < 0)
522 goto error;
523 if (!equal_params) {
524 isl_reordering *exp;
526 model = isl_space_drop_dims(model, isl_dim_in,
527 0, isl_space_dim(model, isl_dim_in));
528 model = isl_space_drop_dims(model, isl_dim_out,
529 0, isl_space_dim(model, isl_dim_out));
530 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
531 exp = isl_reordering_extend_space(exp,
532 isl_aff_get_domain_space(aff));
533 aff = isl_aff_realign_domain(aff, exp);
536 isl_space_free(model);
537 return aff;
538 error:
539 isl_space_free(model);
540 isl_aff_free(aff);
541 return NULL;
544 /* Is "aff" obviously equal to zero?
546 * If the denominator is zero, then "aff" is not equal to zero.
548 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
550 if (!aff)
551 return isl_bool_error;
553 if (isl_int_is_zero(aff->v->el[0]))
554 return isl_bool_false;
555 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
558 /* Does "aff" represent NaN?
560 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
562 if (!aff)
563 return isl_bool_error;
565 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
568 /* Are "aff1" and "aff2" obviously equal?
570 * NaN is not equal to anything, not even to another NaN.
572 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
573 __isl_keep isl_aff *aff2)
575 isl_bool equal;
577 if (!aff1 || !aff2)
578 return isl_bool_error;
580 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
581 return isl_bool_false;
583 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
584 if (equal < 0 || !equal)
585 return equal;
587 return isl_vec_is_equal(aff1->v, aff2->v);
590 /* Return the common denominator of "aff" in "v".
592 * We cannot return anything meaningful in case of a NaN.
594 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
596 if (!aff)
597 return isl_stat_error;
598 if (isl_aff_is_nan(aff))
599 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
600 "cannot get denominator of NaN", return isl_stat_error);
601 isl_int_set(*v, aff->v->el[0]);
602 return isl_stat_ok;
605 /* Return the common denominator of "aff".
607 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
609 isl_ctx *ctx;
611 if (!aff)
612 return NULL;
614 ctx = isl_aff_get_ctx(aff);
615 if (isl_aff_is_nan(aff))
616 return isl_val_nan(ctx);
617 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
620 /* Return the constant term of "aff".
622 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
624 isl_ctx *ctx;
625 isl_val *v;
627 if (!aff)
628 return NULL;
630 ctx = isl_aff_get_ctx(aff);
631 if (isl_aff_is_nan(aff))
632 return isl_val_nan(ctx);
633 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
634 return isl_val_normalize(v);
637 /* Return the coefficient of the variable of type "type" at position "pos"
638 * of "aff".
640 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
641 enum isl_dim_type type, int pos)
643 isl_ctx *ctx;
644 isl_val *v;
646 if (!aff)
647 return NULL;
649 ctx = isl_aff_get_ctx(aff);
650 if (type == isl_dim_out)
651 isl_die(ctx, isl_error_invalid,
652 "output/set dimension does not have a coefficient",
653 return NULL);
654 if (type == isl_dim_in)
655 type = isl_dim_set;
657 if (pos >= isl_local_space_dim(aff->ls, type))
658 isl_die(ctx, isl_error_invalid,
659 "position out of bounds", return NULL);
661 if (isl_aff_is_nan(aff))
662 return isl_val_nan(ctx);
663 pos += isl_local_space_offset(aff->ls, type);
664 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
665 return isl_val_normalize(v);
668 /* Return the sign of the coefficient of the variable of type "type"
669 * at position "pos" of "aff".
671 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
672 int pos)
674 isl_ctx *ctx;
676 if (!aff)
677 return 0;
679 ctx = isl_aff_get_ctx(aff);
680 if (type == isl_dim_out)
681 isl_die(ctx, isl_error_invalid,
682 "output/set dimension does not have a coefficient",
683 return 0);
684 if (type == isl_dim_in)
685 type = isl_dim_set;
687 if (pos >= isl_local_space_dim(aff->ls, type))
688 isl_die(ctx, isl_error_invalid,
689 "position out of bounds", return 0);
691 pos += isl_local_space_offset(aff->ls, type);
692 return isl_int_sgn(aff->v->el[1 + pos]);
695 /* Replace the numerator of the constant term of "aff" by "v".
697 * A NaN is unaffected by this operation.
699 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
701 if (!aff)
702 return NULL;
703 if (isl_aff_is_nan(aff))
704 return aff;
705 aff = isl_aff_cow(aff);
706 if (!aff)
707 return NULL;
709 aff->v = isl_vec_cow(aff->v);
710 if (!aff->v)
711 return isl_aff_free(aff);
713 isl_int_set(aff->v->el[1], v);
715 return aff;
718 /* Replace the constant term of "aff" by "v".
720 * A NaN is unaffected by this operation.
722 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
723 __isl_take isl_val *v)
725 if (!aff || !v)
726 goto error;
728 if (isl_aff_is_nan(aff)) {
729 isl_val_free(v);
730 return aff;
733 if (!isl_val_is_rat(v))
734 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
735 "expecting rational value", goto error);
737 if (isl_int_eq(aff->v->el[1], v->n) &&
738 isl_int_eq(aff->v->el[0], v->d)) {
739 isl_val_free(v);
740 return aff;
743 aff = isl_aff_cow(aff);
744 if (!aff)
745 goto error;
746 aff->v = isl_vec_cow(aff->v);
747 if (!aff->v)
748 goto error;
750 if (isl_int_eq(aff->v->el[0], v->d)) {
751 isl_int_set(aff->v->el[1], v->n);
752 } else if (isl_int_is_one(v->d)) {
753 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
754 } else {
755 isl_seq_scale(aff->v->el + 1,
756 aff->v->el + 1, v->d, aff->v->size - 1);
757 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
758 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
759 aff->v = isl_vec_normalize(aff->v);
760 if (!aff->v)
761 goto error;
764 isl_val_free(v);
765 return aff;
766 error:
767 isl_aff_free(aff);
768 isl_val_free(v);
769 return NULL;
772 /* Add "v" to the constant term of "aff".
774 * A NaN is unaffected by this operation.
776 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
778 if (isl_int_is_zero(v))
779 return aff;
781 if (!aff)
782 return NULL;
783 if (isl_aff_is_nan(aff))
784 return aff;
785 aff = isl_aff_cow(aff);
786 if (!aff)
787 return NULL;
789 aff->v = isl_vec_cow(aff->v);
790 if (!aff->v)
791 return isl_aff_free(aff);
793 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
795 return aff;
798 /* Add "v" to the constant term of "aff".
800 * A NaN is unaffected by this operation.
802 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
803 __isl_take isl_val *v)
805 if (!aff || !v)
806 goto error;
808 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
809 isl_val_free(v);
810 return aff;
813 if (!isl_val_is_rat(v))
814 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
815 "expecting rational value", goto error);
817 aff = isl_aff_cow(aff);
818 if (!aff)
819 goto error;
821 aff->v = isl_vec_cow(aff->v);
822 if (!aff->v)
823 goto error;
825 if (isl_int_is_one(v->d)) {
826 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
827 } else if (isl_int_eq(aff->v->el[0], v->d)) {
828 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
829 aff->v = isl_vec_normalize(aff->v);
830 if (!aff->v)
831 goto error;
832 } else {
833 isl_seq_scale(aff->v->el + 1,
834 aff->v->el + 1, v->d, aff->v->size - 1);
835 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
836 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
837 aff->v = isl_vec_normalize(aff->v);
838 if (!aff->v)
839 goto error;
842 isl_val_free(v);
843 return aff;
844 error:
845 isl_aff_free(aff);
846 isl_val_free(v);
847 return NULL;
850 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
852 isl_int t;
854 isl_int_init(t);
855 isl_int_set_si(t, v);
856 aff = isl_aff_add_constant(aff, t);
857 isl_int_clear(t);
859 return aff;
862 /* Add "v" to the numerator of the constant term of "aff".
864 * A NaN is unaffected by this operation.
866 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
868 if (isl_int_is_zero(v))
869 return aff;
871 if (!aff)
872 return NULL;
873 if (isl_aff_is_nan(aff))
874 return aff;
875 aff = isl_aff_cow(aff);
876 if (!aff)
877 return NULL;
879 aff->v = isl_vec_cow(aff->v);
880 if (!aff->v)
881 return isl_aff_free(aff);
883 isl_int_add(aff->v->el[1], aff->v->el[1], v);
885 return aff;
888 /* Add "v" to the numerator of the constant term of "aff".
890 * A NaN is unaffected by this operation.
892 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
894 isl_int t;
896 if (v == 0)
897 return aff;
899 isl_int_init(t);
900 isl_int_set_si(t, v);
901 aff = isl_aff_add_constant_num(aff, t);
902 isl_int_clear(t);
904 return aff;
907 /* Replace the numerator of the constant term of "aff" by "v".
909 * A NaN is unaffected by this operation.
911 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
913 if (!aff)
914 return NULL;
915 if (isl_aff_is_nan(aff))
916 return aff;
917 aff = isl_aff_cow(aff);
918 if (!aff)
919 return NULL;
921 aff->v = isl_vec_cow(aff->v);
922 if (!aff->v)
923 return isl_aff_free(aff);
925 isl_int_set_si(aff->v->el[1], v);
927 return aff;
930 /* Replace the numerator of the coefficient of the variable of type "type"
931 * at position "pos" of "aff" by "v".
933 * A NaN is unaffected by this operation.
935 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
936 enum isl_dim_type type, int pos, isl_int v)
938 if (!aff)
939 return NULL;
941 if (type == isl_dim_out)
942 isl_die(aff->v->ctx, isl_error_invalid,
943 "output/set dimension does not have a coefficient",
944 return isl_aff_free(aff));
945 if (type == isl_dim_in)
946 type = isl_dim_set;
948 if (pos >= isl_local_space_dim(aff->ls, type))
949 isl_die(aff->v->ctx, isl_error_invalid,
950 "position out of bounds", return isl_aff_free(aff));
952 if (isl_aff_is_nan(aff))
953 return aff;
954 aff = isl_aff_cow(aff);
955 if (!aff)
956 return NULL;
958 aff->v = isl_vec_cow(aff->v);
959 if (!aff->v)
960 return isl_aff_free(aff);
962 pos += isl_local_space_offset(aff->ls, type);
963 isl_int_set(aff->v->el[1 + pos], v);
965 return aff;
968 /* Replace the numerator of the coefficient of the variable of type "type"
969 * at position "pos" of "aff" by "v".
971 * A NaN is unaffected by this operation.
973 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
974 enum isl_dim_type type, int pos, int v)
976 if (!aff)
977 return NULL;
979 if (type == isl_dim_out)
980 isl_die(aff->v->ctx, isl_error_invalid,
981 "output/set dimension does not have a coefficient",
982 return isl_aff_free(aff));
983 if (type == isl_dim_in)
984 type = isl_dim_set;
986 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
987 isl_die(aff->v->ctx, isl_error_invalid,
988 "position out of bounds", return isl_aff_free(aff));
990 if (isl_aff_is_nan(aff))
991 return aff;
992 pos += isl_local_space_offset(aff->ls, type);
993 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
994 return aff;
996 aff = isl_aff_cow(aff);
997 if (!aff)
998 return NULL;
1000 aff->v = isl_vec_cow(aff->v);
1001 if (!aff->v)
1002 return isl_aff_free(aff);
1004 isl_int_set_si(aff->v->el[1 + pos], v);
1006 return aff;
1009 /* Replace the coefficient of the variable of type "type" at position "pos"
1010 * of "aff" by "v".
1012 * A NaN is unaffected by this operation.
1014 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1015 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1017 if (!aff || !v)
1018 goto error;
1020 if (type == isl_dim_out)
1021 isl_die(aff->v->ctx, isl_error_invalid,
1022 "output/set dimension does not have a coefficient",
1023 goto error);
1024 if (type == isl_dim_in)
1025 type = isl_dim_set;
1027 if (pos >= isl_local_space_dim(aff->ls, type))
1028 isl_die(aff->v->ctx, isl_error_invalid,
1029 "position out of bounds", goto error);
1031 if (isl_aff_is_nan(aff)) {
1032 isl_val_free(v);
1033 return aff;
1035 if (!isl_val_is_rat(v))
1036 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1037 "expecting rational value", goto error);
1039 pos += isl_local_space_offset(aff->ls, type);
1040 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1041 isl_int_eq(aff->v->el[0], v->d)) {
1042 isl_val_free(v);
1043 return aff;
1046 aff = isl_aff_cow(aff);
1047 if (!aff)
1048 goto error;
1049 aff->v = isl_vec_cow(aff->v);
1050 if (!aff->v)
1051 goto error;
1053 if (isl_int_eq(aff->v->el[0], v->d)) {
1054 isl_int_set(aff->v->el[1 + pos], v->n);
1055 } else if (isl_int_is_one(v->d)) {
1056 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1057 } else {
1058 isl_seq_scale(aff->v->el + 1,
1059 aff->v->el + 1, v->d, aff->v->size - 1);
1060 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1061 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1062 aff->v = isl_vec_normalize(aff->v);
1063 if (!aff->v)
1064 goto error;
1067 isl_val_free(v);
1068 return aff;
1069 error:
1070 isl_aff_free(aff);
1071 isl_val_free(v);
1072 return NULL;
1075 /* Add "v" to the coefficient of the variable of type "type"
1076 * at position "pos" of "aff".
1078 * A NaN is unaffected by this operation.
1080 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1081 enum isl_dim_type type, int pos, isl_int v)
1083 if (!aff)
1084 return NULL;
1086 if (type == isl_dim_out)
1087 isl_die(aff->v->ctx, isl_error_invalid,
1088 "output/set dimension does not have a coefficient",
1089 return isl_aff_free(aff));
1090 if (type == isl_dim_in)
1091 type = isl_dim_set;
1093 if (pos >= isl_local_space_dim(aff->ls, type))
1094 isl_die(aff->v->ctx, isl_error_invalid,
1095 "position out of bounds", return isl_aff_free(aff));
1097 if (isl_aff_is_nan(aff))
1098 return aff;
1099 aff = isl_aff_cow(aff);
1100 if (!aff)
1101 return NULL;
1103 aff->v = isl_vec_cow(aff->v);
1104 if (!aff->v)
1105 return isl_aff_free(aff);
1107 pos += isl_local_space_offset(aff->ls, type);
1108 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1110 return aff;
1113 /* Add "v" to the coefficient of the variable of type "type"
1114 * at position "pos" of "aff".
1116 * A NaN is unaffected by this operation.
1118 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1119 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1121 if (!aff || !v)
1122 goto error;
1124 if (isl_val_is_zero(v)) {
1125 isl_val_free(v);
1126 return aff;
1129 if (type == isl_dim_out)
1130 isl_die(aff->v->ctx, isl_error_invalid,
1131 "output/set dimension does not have a coefficient",
1132 goto error);
1133 if (type == isl_dim_in)
1134 type = isl_dim_set;
1136 if (pos >= isl_local_space_dim(aff->ls, type))
1137 isl_die(aff->v->ctx, isl_error_invalid,
1138 "position out of bounds", goto error);
1140 if (isl_aff_is_nan(aff)) {
1141 isl_val_free(v);
1142 return aff;
1144 if (!isl_val_is_rat(v))
1145 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1146 "expecting rational value", goto error);
1148 aff = isl_aff_cow(aff);
1149 if (!aff)
1150 goto error;
1152 aff->v = isl_vec_cow(aff->v);
1153 if (!aff->v)
1154 goto error;
1156 pos += isl_local_space_offset(aff->ls, type);
1157 if (isl_int_is_one(v->d)) {
1158 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1159 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1160 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1161 aff->v = isl_vec_normalize(aff->v);
1162 if (!aff->v)
1163 goto error;
1164 } else {
1165 isl_seq_scale(aff->v->el + 1,
1166 aff->v->el + 1, v->d, aff->v->size - 1);
1167 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1168 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1169 aff->v = isl_vec_normalize(aff->v);
1170 if (!aff->v)
1171 goto error;
1174 isl_val_free(v);
1175 return aff;
1176 error:
1177 isl_aff_free(aff);
1178 isl_val_free(v);
1179 return NULL;
1182 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1183 enum isl_dim_type type, int pos, int v)
1185 isl_int t;
1187 isl_int_init(t);
1188 isl_int_set_si(t, v);
1189 aff = isl_aff_add_coefficient(aff, type, pos, t);
1190 isl_int_clear(t);
1192 return aff;
1195 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1197 if (!aff)
1198 return NULL;
1200 return isl_local_space_get_div(aff->ls, pos);
1203 /* Return the negation of "aff".
1205 * As a special case, -NaN = NaN.
1207 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1209 if (!aff)
1210 return NULL;
1211 if (isl_aff_is_nan(aff))
1212 return aff;
1213 aff = isl_aff_cow(aff);
1214 if (!aff)
1215 return NULL;
1216 aff->v = isl_vec_cow(aff->v);
1217 if (!aff->v)
1218 return isl_aff_free(aff);
1220 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1222 return aff;
1225 /* Remove divs from the local space that do not appear in the affine
1226 * expression.
1227 * We currently only remove divs at the end.
1228 * Some intermediate divs may also not appear directly in the affine
1229 * expression, but we would also need to check that no other divs are
1230 * defined in terms of them.
1232 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1234 int pos;
1235 int off;
1236 int n;
1238 if (!aff)
1239 return NULL;
1241 n = isl_local_space_dim(aff->ls, isl_dim_div);
1242 off = isl_local_space_offset(aff->ls, isl_dim_div);
1244 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1245 if (pos == n)
1246 return aff;
1248 aff = isl_aff_cow(aff);
1249 if (!aff)
1250 return NULL;
1252 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1253 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1254 if (!aff->ls || !aff->v)
1255 return isl_aff_free(aff);
1257 return aff;
1260 /* Look for any divs in the aff->ls with a denominator equal to one
1261 * and plug them into the affine expression and any subsequent divs
1262 * that may reference the div.
1264 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1266 int i, n;
1267 int len;
1268 isl_int v;
1269 isl_vec *vec;
1270 isl_local_space *ls;
1271 unsigned pos;
1273 if (!aff)
1274 return NULL;
1276 n = isl_local_space_dim(aff->ls, isl_dim_div);
1277 len = aff->v->size;
1278 for (i = 0; i < n; ++i) {
1279 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1280 continue;
1281 ls = isl_local_space_copy(aff->ls);
1282 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1283 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1284 vec = isl_vec_copy(aff->v);
1285 vec = isl_vec_cow(vec);
1286 if (!ls || !vec)
1287 goto error;
1289 isl_int_init(v);
1291 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1292 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1293 len, len, v);
1295 isl_int_clear(v);
1297 isl_vec_free(aff->v);
1298 aff->v = vec;
1299 isl_local_space_free(aff->ls);
1300 aff->ls = ls;
1303 return aff;
1304 error:
1305 isl_vec_free(vec);
1306 isl_local_space_free(ls);
1307 return isl_aff_free(aff);
1310 /* Look for any divs j that appear with a unit coefficient inside
1311 * the definitions of other divs i and plug them into the definitions
1312 * of the divs i.
1314 * In particular, an expression of the form
1316 * floor((f(..) + floor(g(..)/n))/m)
1318 * is simplified to
1320 * floor((n * f(..) + g(..))/(n * m))
1322 * This simplification is correct because we can move the expression
1323 * f(..) into the inner floor in the original expression to obtain
1325 * floor(floor((n * f(..) + g(..))/n)/m)
1327 * from which we can derive the simplified expression.
1329 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1331 int i, j, n;
1332 int off;
1334 if (!aff)
1335 return NULL;
1337 n = isl_local_space_dim(aff->ls, isl_dim_div);
1338 off = isl_local_space_offset(aff->ls, isl_dim_div);
1339 for (i = 1; i < n; ++i) {
1340 for (j = 0; j < i; ++j) {
1341 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1342 continue;
1343 aff->ls = isl_local_space_substitute_seq(aff->ls,
1344 isl_dim_div, j, aff->ls->div->row[j],
1345 aff->v->size, i, 1);
1346 if (!aff->ls)
1347 return isl_aff_free(aff);
1351 return aff;
1354 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1356 * Even though this function is only called on isl_affs with a single
1357 * reference, we are careful to only change aff->v and aff->ls together.
1359 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1361 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1362 isl_local_space *ls;
1363 isl_vec *v;
1365 ls = isl_local_space_copy(aff->ls);
1366 ls = isl_local_space_swap_div(ls, a, b);
1367 v = isl_vec_copy(aff->v);
1368 v = isl_vec_cow(v);
1369 if (!ls || !v)
1370 goto error;
1372 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1373 isl_vec_free(aff->v);
1374 aff->v = v;
1375 isl_local_space_free(aff->ls);
1376 aff->ls = ls;
1378 return aff;
1379 error:
1380 isl_vec_free(v);
1381 isl_local_space_free(ls);
1382 return isl_aff_free(aff);
1385 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1387 * We currently do not actually remove div "b", but simply add its
1388 * coefficient to that of "a" and then zero it out.
1390 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1392 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1394 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1395 return aff;
1397 aff->v = isl_vec_cow(aff->v);
1398 if (!aff->v)
1399 return isl_aff_free(aff);
1401 isl_int_add(aff->v->el[1 + off + a],
1402 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1403 isl_int_set_si(aff->v->el[1 + off + b], 0);
1405 return aff;
1408 /* Sort the divs in the local space of "aff" according to
1409 * the comparison function "cmp_row" in isl_local_space.c,
1410 * combining the coefficients of identical divs.
1412 * Reordering divs does not change the semantics of "aff",
1413 * so there is no need to call isl_aff_cow.
1414 * Moreover, this function is currently only called on isl_affs
1415 * with a single reference.
1417 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1419 int i, j, n;
1421 if (!aff)
1422 return NULL;
1424 n = isl_aff_dim(aff, isl_dim_div);
1425 for (i = 1; i < n; ++i) {
1426 for (j = i - 1; j >= 0; --j) {
1427 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1428 if (cmp < 0)
1429 break;
1430 if (cmp == 0)
1431 aff = merge_divs(aff, j, j + 1);
1432 else
1433 aff = swap_div(aff, j, j + 1);
1434 if (!aff)
1435 return NULL;
1439 return aff;
1442 /* Normalize the representation of "aff".
1444 * This function should only be called of "new" isl_affs, i.e.,
1445 * with only a single reference. We therefore do not need to
1446 * worry about affecting other instances.
1448 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1450 if (!aff)
1451 return NULL;
1452 aff->v = isl_vec_normalize(aff->v);
1453 if (!aff->v)
1454 return isl_aff_free(aff);
1455 aff = plug_in_integral_divs(aff);
1456 aff = plug_in_unit_divs(aff);
1457 aff = sort_divs(aff);
1458 aff = isl_aff_remove_unused_divs(aff);
1459 return aff;
1462 /* Given f, return floor(f).
1463 * If f is an integer expression, then just return f.
1464 * If f is a constant, then return the constant floor(f).
1465 * Otherwise, if f = g/m, write g = q m + r,
1466 * create a new div d = [r/m] and return the expression q + d.
1467 * The coefficients in r are taken to lie between -m/2 and m/2.
1469 * reduce_div_coefficients performs the same normalization.
1471 * As a special case, floor(NaN) = NaN.
1473 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1475 int i;
1476 int size;
1477 isl_ctx *ctx;
1478 isl_vec *div;
1480 if (!aff)
1481 return NULL;
1483 if (isl_aff_is_nan(aff))
1484 return aff;
1485 if (isl_int_is_one(aff->v->el[0]))
1486 return aff;
1488 aff = isl_aff_cow(aff);
1489 if (!aff)
1490 return NULL;
1492 aff->v = isl_vec_cow(aff->v);
1493 if (!aff->v)
1494 return isl_aff_free(aff);
1496 if (isl_aff_is_cst(aff)) {
1497 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1498 isl_int_set_si(aff->v->el[0], 1);
1499 return aff;
1502 div = isl_vec_copy(aff->v);
1503 div = isl_vec_cow(div);
1504 if (!div)
1505 return isl_aff_free(aff);
1507 ctx = isl_aff_get_ctx(aff);
1508 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1509 for (i = 1; i < aff->v->size; ++i) {
1510 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1511 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1512 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1513 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1514 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1518 aff->ls = isl_local_space_add_div(aff->ls, div);
1519 if (!aff->ls)
1520 return isl_aff_free(aff);
1522 size = aff->v->size;
1523 aff->v = isl_vec_extend(aff->v, size + 1);
1524 if (!aff->v)
1525 return isl_aff_free(aff);
1526 isl_int_set_si(aff->v->el[0], 1);
1527 isl_int_set_si(aff->v->el[size], 1);
1529 aff = isl_aff_normalize(aff);
1531 return aff;
1534 /* Compute
1536 * aff mod m = aff - m * floor(aff/m)
1538 * with m an integer value.
1540 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1541 __isl_take isl_val *m)
1543 isl_aff *res;
1545 if (!aff || !m)
1546 goto error;
1548 if (!isl_val_is_int(m))
1549 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1550 "expecting integer modulo", goto error);
1552 res = isl_aff_copy(aff);
1553 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1554 aff = isl_aff_floor(aff);
1555 aff = isl_aff_scale_val(aff, m);
1556 res = isl_aff_sub(res, aff);
1558 return res;
1559 error:
1560 isl_aff_free(aff);
1561 isl_val_free(m);
1562 return NULL;
1565 /* Compute
1567 * pwaff mod m = pwaff - m * floor(pwaff/m)
1569 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1571 isl_pw_aff *res;
1573 res = isl_pw_aff_copy(pwaff);
1574 pwaff = isl_pw_aff_scale_down(pwaff, m);
1575 pwaff = isl_pw_aff_floor(pwaff);
1576 pwaff = isl_pw_aff_scale(pwaff, m);
1577 res = isl_pw_aff_sub(res, pwaff);
1579 return res;
1582 /* Compute
1584 * pa mod m = pa - m * floor(pa/m)
1586 * with m an integer value.
1588 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1589 __isl_take isl_val *m)
1591 if (!pa || !m)
1592 goto error;
1593 if (!isl_val_is_int(m))
1594 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1595 "expecting integer modulo", goto error);
1596 pa = isl_pw_aff_mod(pa, m->n);
1597 isl_val_free(m);
1598 return pa;
1599 error:
1600 isl_pw_aff_free(pa);
1601 isl_val_free(m);
1602 return NULL;
1605 /* Given f, return ceil(f).
1606 * If f is an integer expression, then just return f.
1607 * Otherwise, let f be the expression
1609 * e/m
1611 * then return
1613 * floor((e + m - 1)/m)
1615 * As a special case, ceil(NaN) = NaN.
1617 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1619 if (!aff)
1620 return NULL;
1622 if (isl_aff_is_nan(aff))
1623 return aff;
1624 if (isl_int_is_one(aff->v->el[0]))
1625 return aff;
1627 aff = isl_aff_cow(aff);
1628 if (!aff)
1629 return NULL;
1630 aff->v = isl_vec_cow(aff->v);
1631 if (!aff->v)
1632 return isl_aff_free(aff);
1634 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1635 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1636 aff = isl_aff_floor(aff);
1638 return aff;
1641 /* Apply the expansion computed by isl_merge_divs.
1642 * The expansion itself is given by "exp" while the resulting
1643 * list of divs is given by "div".
1645 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1646 __isl_take isl_mat *div, int *exp)
1648 int old_n_div;
1649 int new_n_div;
1650 int offset;
1652 aff = isl_aff_cow(aff);
1653 if (!aff || !div)
1654 goto error;
1656 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1657 new_n_div = isl_mat_rows(div);
1658 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1660 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1661 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1662 if (!aff->v || !aff->ls)
1663 return isl_aff_free(aff);
1664 return aff;
1665 error:
1666 isl_aff_free(aff);
1667 isl_mat_free(div);
1668 return NULL;
1671 /* Add two affine expressions that live in the same local space.
1673 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1674 __isl_take isl_aff *aff2)
1676 isl_int gcd, f;
1678 aff1 = isl_aff_cow(aff1);
1679 if (!aff1 || !aff2)
1680 goto error;
1682 aff1->v = isl_vec_cow(aff1->v);
1683 if (!aff1->v)
1684 goto error;
1686 isl_int_init(gcd);
1687 isl_int_init(f);
1688 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1689 isl_int_divexact(f, aff2->v->el[0], gcd);
1690 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1691 isl_int_divexact(f, aff1->v->el[0], gcd);
1692 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1693 isl_int_divexact(f, aff2->v->el[0], gcd);
1694 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1695 isl_int_clear(f);
1696 isl_int_clear(gcd);
1698 isl_aff_free(aff2);
1699 return aff1;
1700 error:
1701 isl_aff_free(aff1);
1702 isl_aff_free(aff2);
1703 return NULL;
1706 /* Return the sum of "aff1" and "aff2".
1708 * If either of the two is NaN, then the result is NaN.
1710 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1711 __isl_take isl_aff *aff2)
1713 isl_ctx *ctx;
1714 int *exp1 = NULL;
1715 int *exp2 = NULL;
1716 isl_mat *div;
1717 int n_div1, n_div2;
1719 if (!aff1 || !aff2)
1720 goto error;
1722 ctx = isl_aff_get_ctx(aff1);
1723 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1724 isl_die(ctx, isl_error_invalid,
1725 "spaces don't match", goto error);
1727 if (isl_aff_is_nan(aff1)) {
1728 isl_aff_free(aff2);
1729 return aff1;
1731 if (isl_aff_is_nan(aff2)) {
1732 isl_aff_free(aff1);
1733 return aff2;
1736 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1737 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1738 if (n_div1 == 0 && n_div2 == 0)
1739 return add_expanded(aff1, aff2);
1741 exp1 = isl_alloc_array(ctx, int, n_div1);
1742 exp2 = isl_alloc_array(ctx, int, n_div2);
1743 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1744 goto error;
1746 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1747 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1748 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1749 free(exp1);
1750 free(exp2);
1752 return add_expanded(aff1, aff2);
1753 error:
1754 free(exp1);
1755 free(exp2);
1756 isl_aff_free(aff1);
1757 isl_aff_free(aff2);
1758 return NULL;
1761 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1762 __isl_take isl_aff *aff2)
1764 return isl_aff_add(aff1, isl_aff_neg(aff2));
1767 /* Return the result of scaling "aff" by a factor of "f".
1769 * As a special case, f * NaN = NaN.
1771 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1773 isl_int gcd;
1775 if (!aff)
1776 return NULL;
1777 if (isl_aff_is_nan(aff))
1778 return aff;
1780 if (isl_int_is_one(f))
1781 return aff;
1783 aff = isl_aff_cow(aff);
1784 if (!aff)
1785 return NULL;
1786 aff->v = isl_vec_cow(aff->v);
1787 if (!aff->v)
1788 return isl_aff_free(aff);
1790 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1791 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1792 return aff;
1795 isl_int_init(gcd);
1796 isl_int_gcd(gcd, aff->v->el[0], f);
1797 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1798 isl_int_divexact(gcd, f, gcd);
1799 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1800 isl_int_clear(gcd);
1802 return aff;
1805 /* Multiple "aff" by "v".
1807 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1808 __isl_take isl_val *v)
1810 if (!aff || !v)
1811 goto error;
1813 if (isl_val_is_one(v)) {
1814 isl_val_free(v);
1815 return aff;
1818 if (!isl_val_is_rat(v))
1819 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1820 "expecting rational factor", goto error);
1822 aff = isl_aff_scale(aff, v->n);
1823 aff = isl_aff_scale_down(aff, v->d);
1825 isl_val_free(v);
1826 return aff;
1827 error:
1828 isl_aff_free(aff);
1829 isl_val_free(v);
1830 return NULL;
1833 /* Return the result of scaling "aff" down by a factor of "f".
1835 * As a special case, NaN/f = NaN.
1837 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1839 isl_int gcd;
1841 if (!aff)
1842 return NULL;
1843 if (isl_aff_is_nan(aff))
1844 return aff;
1846 if (isl_int_is_one(f))
1847 return aff;
1849 aff = isl_aff_cow(aff);
1850 if (!aff)
1851 return NULL;
1853 if (isl_int_is_zero(f))
1854 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1855 "cannot scale down by zero", return isl_aff_free(aff));
1857 aff->v = isl_vec_cow(aff->v);
1858 if (!aff->v)
1859 return isl_aff_free(aff);
1861 isl_int_init(gcd);
1862 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1863 isl_int_gcd(gcd, gcd, f);
1864 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1865 isl_int_divexact(gcd, f, gcd);
1866 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1867 isl_int_clear(gcd);
1869 return aff;
1872 /* Divide "aff" by "v".
1874 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1875 __isl_take isl_val *v)
1877 if (!aff || !v)
1878 goto error;
1880 if (isl_val_is_one(v)) {
1881 isl_val_free(v);
1882 return aff;
1885 if (!isl_val_is_rat(v))
1886 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1887 "expecting rational factor", goto error);
1888 if (!isl_val_is_pos(v))
1889 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1890 "factor needs to be positive", goto error);
1892 aff = isl_aff_scale(aff, v->d);
1893 aff = isl_aff_scale_down(aff, v->n);
1895 isl_val_free(v);
1896 return aff;
1897 error:
1898 isl_aff_free(aff);
1899 isl_val_free(v);
1900 return NULL;
1903 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1905 isl_int v;
1907 if (f == 1)
1908 return aff;
1910 isl_int_init(v);
1911 isl_int_set_ui(v, f);
1912 aff = isl_aff_scale_down(aff, v);
1913 isl_int_clear(v);
1915 return aff;
1918 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1919 enum isl_dim_type type, unsigned pos, const char *s)
1921 aff = isl_aff_cow(aff);
1922 if (!aff)
1923 return NULL;
1924 if (type == isl_dim_out)
1925 isl_die(aff->v->ctx, isl_error_invalid,
1926 "cannot set name of output/set dimension",
1927 return isl_aff_free(aff));
1928 if (type == isl_dim_in)
1929 type = isl_dim_set;
1930 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1931 if (!aff->ls)
1932 return isl_aff_free(aff);
1934 return aff;
1937 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1938 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1940 aff = isl_aff_cow(aff);
1941 if (!aff)
1942 goto error;
1943 if (type == isl_dim_out)
1944 isl_die(aff->v->ctx, isl_error_invalid,
1945 "cannot set name of output/set dimension",
1946 goto error);
1947 if (type == isl_dim_in)
1948 type = isl_dim_set;
1949 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1950 if (!aff->ls)
1951 return isl_aff_free(aff);
1953 return aff;
1954 error:
1955 isl_id_free(id);
1956 isl_aff_free(aff);
1957 return NULL;
1960 /* Replace the identifier of the input tuple of "aff" by "id".
1961 * type is currently required to be equal to isl_dim_in
1963 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1964 enum isl_dim_type type, __isl_take isl_id *id)
1966 aff = isl_aff_cow(aff);
1967 if (!aff)
1968 goto error;
1969 if (type != isl_dim_out)
1970 isl_die(aff->v->ctx, isl_error_invalid,
1971 "cannot only set id of input tuple", goto error);
1972 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
1973 if (!aff->ls)
1974 return isl_aff_free(aff);
1976 return aff;
1977 error:
1978 isl_id_free(id);
1979 isl_aff_free(aff);
1980 return NULL;
1983 /* Exploit the equalities in "eq" to simplify the affine expression
1984 * and the expressions of the integer divisions in the local space.
1985 * The integer divisions in this local space are assumed to appear
1986 * as regular dimensions in "eq".
1988 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
1989 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
1991 int i, j;
1992 unsigned total;
1993 unsigned n_div;
1995 if (!eq)
1996 goto error;
1997 if (eq->n_eq == 0) {
1998 isl_basic_set_free(eq);
1999 return aff;
2002 aff = isl_aff_cow(aff);
2003 if (!aff)
2004 goto error;
2006 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2007 isl_basic_set_copy(eq));
2008 aff->v = isl_vec_cow(aff->v);
2009 if (!aff->ls || !aff->v)
2010 goto error;
2012 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2013 n_div = eq->n_div;
2014 for (i = 0; i < eq->n_eq; ++i) {
2015 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2016 if (j < 0 || j == 0 || j >= total)
2017 continue;
2019 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2020 &aff->v->el[0]);
2023 isl_basic_set_free(eq);
2024 aff = isl_aff_normalize(aff);
2025 return aff;
2026 error:
2027 isl_basic_set_free(eq);
2028 isl_aff_free(aff);
2029 return NULL;
2032 /* Exploit the equalities in "eq" to simplify the affine expression
2033 * and the expressions of the integer divisions in the local space.
2035 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2036 __isl_take isl_basic_set *eq)
2038 int n_div;
2040 if (!aff || !eq)
2041 goto error;
2042 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2043 if (n_div > 0)
2044 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2045 return isl_aff_substitute_equalities_lifted(aff, eq);
2046 error:
2047 isl_basic_set_free(eq);
2048 isl_aff_free(aff);
2049 return NULL;
2052 /* Look for equalities among the variables shared by context and aff
2053 * and the integer divisions of aff, if any.
2054 * The equalities are then used to eliminate coefficients and/or integer
2055 * divisions from aff.
2057 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2058 __isl_take isl_set *context)
2060 isl_basic_set *hull;
2061 int n_div;
2063 if (!aff)
2064 goto error;
2065 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2066 if (n_div > 0) {
2067 isl_basic_set *bset;
2068 isl_local_space *ls;
2069 context = isl_set_add_dims(context, isl_dim_set, n_div);
2070 ls = isl_aff_get_domain_local_space(aff);
2071 bset = isl_basic_set_from_local_space(ls);
2072 bset = isl_basic_set_lift(bset);
2073 bset = isl_basic_set_flatten(bset);
2074 context = isl_set_intersect(context,
2075 isl_set_from_basic_set(bset));
2078 hull = isl_set_affine_hull(context);
2079 return isl_aff_substitute_equalities_lifted(aff, hull);
2080 error:
2081 isl_aff_free(aff);
2082 isl_set_free(context);
2083 return NULL;
2086 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2087 __isl_take isl_set *context)
2089 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2090 dom_context = isl_set_intersect_params(dom_context, context);
2091 return isl_aff_gist(aff, dom_context);
2094 /* Return a basic set containing those elements in the space
2095 * of aff where it is positive. "rational" should not be set.
2097 * If "aff" is NaN, then it is not positive.
2099 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2100 int rational)
2102 isl_constraint *ineq;
2103 isl_basic_set *bset;
2104 isl_val *c;
2106 if (!aff)
2107 return NULL;
2108 if (isl_aff_is_nan(aff)) {
2109 isl_space *space = isl_aff_get_domain_space(aff);
2110 isl_aff_free(aff);
2111 return isl_basic_set_empty(space);
2113 if (rational)
2114 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2115 "rational sets not supported", goto error);
2117 ineq = isl_inequality_from_aff(aff);
2118 c = isl_constraint_get_constant_val(ineq);
2119 c = isl_val_sub_ui(c, 1);
2120 ineq = isl_constraint_set_constant_val(ineq, c);
2122 bset = isl_basic_set_from_constraint(ineq);
2123 bset = isl_basic_set_simplify(bset);
2124 return bset;
2125 error:
2126 isl_aff_free(aff);
2127 return NULL;
2130 /* Return a basic set containing those elements in the space
2131 * of aff where it is non-negative.
2132 * If "rational" is set, then return a rational basic set.
2134 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2136 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2137 __isl_take isl_aff *aff, int rational)
2139 isl_constraint *ineq;
2140 isl_basic_set *bset;
2142 if (!aff)
2143 return NULL;
2144 if (isl_aff_is_nan(aff)) {
2145 isl_space *space = isl_aff_get_domain_space(aff);
2146 isl_aff_free(aff);
2147 return isl_basic_set_empty(space);
2150 ineq = isl_inequality_from_aff(aff);
2152 bset = isl_basic_set_from_constraint(ineq);
2153 if (rational)
2154 bset = isl_basic_set_set_rational(bset);
2155 bset = isl_basic_set_simplify(bset);
2156 return bset;
2159 /* Return a basic set containing those elements in the space
2160 * of aff where it is non-negative.
2162 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2164 return aff_nonneg_basic_set(aff, 0);
2167 /* Return a basic set containing those elements in the domain space
2168 * of "aff" where it is positive.
2170 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2172 aff = isl_aff_add_constant_num_si(aff, -1);
2173 return isl_aff_nonneg_basic_set(aff);
2176 /* Return a basic set containing those elements in the domain space
2177 * of aff where it is negative.
2179 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2181 aff = isl_aff_neg(aff);
2182 return isl_aff_pos_basic_set(aff);
2185 /* Return a basic set containing those elements in the space
2186 * of aff where it is zero.
2187 * If "rational" is set, then return a rational basic set.
2189 * If "aff" is NaN, then it is not zero.
2191 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2192 int rational)
2194 isl_constraint *ineq;
2195 isl_basic_set *bset;
2197 if (!aff)
2198 return NULL;
2199 if (isl_aff_is_nan(aff)) {
2200 isl_space *space = isl_aff_get_domain_space(aff);
2201 isl_aff_free(aff);
2202 return isl_basic_set_empty(space);
2205 ineq = isl_equality_from_aff(aff);
2207 bset = isl_basic_set_from_constraint(ineq);
2208 if (rational)
2209 bset = isl_basic_set_set_rational(bset);
2210 bset = isl_basic_set_simplify(bset);
2211 return bset;
2214 /* Return a basic set containing those elements in the space
2215 * of aff where it is zero.
2217 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2219 return aff_zero_basic_set(aff, 0);
2222 /* Return a basic set containing those elements in the shared space
2223 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2225 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2226 __isl_take isl_aff *aff2)
2228 aff1 = isl_aff_sub(aff1, aff2);
2230 return isl_aff_nonneg_basic_set(aff1);
2233 /* Return a basic set containing those elements in the shared domain space
2234 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2236 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2237 __isl_take isl_aff *aff2)
2239 aff1 = isl_aff_sub(aff1, aff2);
2241 return isl_aff_pos_basic_set(aff1);
2244 /* Return a set containing those elements in the shared space
2245 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2247 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2248 __isl_take isl_aff *aff2)
2250 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2253 /* Return a set containing those elements in the shared domain space
2254 * of aff1 and aff2 where aff1 is greater than aff2.
2256 * If either of the two inputs is NaN, then the result is empty,
2257 * as comparisons with NaN always return false.
2259 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2260 __isl_take isl_aff *aff2)
2262 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2265 /* Return a basic set containing those elements in the shared space
2266 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2268 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2269 __isl_take isl_aff *aff2)
2271 return isl_aff_ge_basic_set(aff2, aff1);
2274 /* Return a basic set containing those elements in the shared domain space
2275 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2277 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2278 __isl_take isl_aff *aff2)
2280 return isl_aff_gt_basic_set(aff2, aff1);
2283 /* Return a set containing those elements in the shared space
2284 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2286 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2287 __isl_take isl_aff *aff2)
2289 return isl_aff_ge_set(aff2, aff1);
2292 /* Return a set containing those elements in the shared domain space
2293 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2295 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2296 __isl_take isl_aff *aff2)
2298 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2301 /* Return a basic set containing those elements in the shared space
2302 * of aff1 and aff2 where aff1 and aff2 are equal.
2304 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2305 __isl_take isl_aff *aff2)
2307 aff1 = isl_aff_sub(aff1, aff2);
2309 return isl_aff_zero_basic_set(aff1);
2312 /* Return a set containing those elements in the shared space
2313 * of aff1 and aff2 where aff1 and aff2 are equal.
2315 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2316 __isl_take isl_aff *aff2)
2318 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2321 /* Return a set containing those elements in the shared domain space
2322 * of aff1 and aff2 where aff1 and aff2 are not equal.
2324 * If either of the two inputs is NaN, then the result is empty,
2325 * as comparisons with NaN always return false.
2327 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2328 __isl_take isl_aff *aff2)
2330 isl_set *set_lt, *set_gt;
2332 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2333 isl_aff_copy(aff2));
2334 set_gt = isl_aff_gt_set(aff1, aff2);
2335 return isl_set_union_disjoint(set_lt, set_gt);
2338 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2339 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2341 aff1 = isl_aff_add(aff1, aff2);
2342 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2343 return aff1;
2346 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2348 if (!aff)
2349 return -1;
2351 return 0;
2354 /* Check whether the given affine expression has non-zero coefficient
2355 * for any dimension in the given range or if any of these dimensions
2356 * appear with non-zero coefficients in any of the integer divisions
2357 * involved in the affine expression.
2359 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2360 enum isl_dim_type type, unsigned first, unsigned n)
2362 int i;
2363 isl_ctx *ctx;
2364 int *active = NULL;
2365 isl_bool involves = isl_bool_false;
2367 if (!aff)
2368 return isl_bool_error;
2369 if (n == 0)
2370 return isl_bool_false;
2372 ctx = isl_aff_get_ctx(aff);
2373 if (first + n > isl_aff_dim(aff, type))
2374 isl_die(ctx, isl_error_invalid,
2375 "range out of bounds", return isl_bool_error);
2377 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2378 if (!active)
2379 goto error;
2381 first += isl_local_space_offset(aff->ls, type) - 1;
2382 for (i = 0; i < n; ++i)
2383 if (active[first + i]) {
2384 involves = isl_bool_true;
2385 break;
2388 free(active);
2390 return involves;
2391 error:
2392 free(active);
2393 return isl_bool_error;
2396 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2397 enum isl_dim_type type, unsigned first, unsigned n)
2399 isl_ctx *ctx;
2401 if (!aff)
2402 return NULL;
2403 if (type == isl_dim_out)
2404 isl_die(aff->v->ctx, isl_error_invalid,
2405 "cannot drop output/set dimension",
2406 return isl_aff_free(aff));
2407 if (type == isl_dim_in)
2408 type = isl_dim_set;
2409 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2410 return aff;
2412 ctx = isl_aff_get_ctx(aff);
2413 if (first + n > isl_local_space_dim(aff->ls, type))
2414 isl_die(ctx, isl_error_invalid, "range out of bounds",
2415 return isl_aff_free(aff));
2417 aff = isl_aff_cow(aff);
2418 if (!aff)
2419 return NULL;
2421 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2422 if (!aff->ls)
2423 return isl_aff_free(aff);
2425 first += 1 + isl_local_space_offset(aff->ls, type);
2426 aff->v = isl_vec_drop_els(aff->v, first, n);
2427 if (!aff->v)
2428 return isl_aff_free(aff);
2430 return aff;
2433 /* Project the domain of the affine expression onto its parameter space.
2434 * The affine expression may not involve any of the domain dimensions.
2436 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2438 isl_space *space;
2439 unsigned n;
2440 int involves;
2442 n = isl_aff_dim(aff, isl_dim_in);
2443 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2444 if (involves < 0)
2445 return isl_aff_free(aff);
2446 if (involves)
2447 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2448 "affine expression involves some of the domain dimensions",
2449 return isl_aff_free(aff));
2450 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2451 space = isl_aff_get_domain_space(aff);
2452 space = isl_space_params(space);
2453 aff = isl_aff_reset_domain_space(aff, space);
2454 return aff;
2457 /* Convert an affine expression defined over a parameter domain
2458 * into one that is defined over a zero-dimensional set.
2460 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2462 isl_local_space *ls;
2464 ls = isl_aff_take_domain_local_space(aff);
2465 ls = isl_local_space_set_from_params(ls);
2466 aff = isl_aff_restore_domain_local_space(aff, ls);
2468 return aff;
2471 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2472 enum isl_dim_type type, unsigned first, unsigned n)
2474 isl_ctx *ctx;
2476 if (!aff)
2477 return NULL;
2478 if (type == isl_dim_out)
2479 isl_die(aff->v->ctx, isl_error_invalid,
2480 "cannot insert output/set dimensions",
2481 return isl_aff_free(aff));
2482 if (type == isl_dim_in)
2483 type = isl_dim_set;
2484 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2485 return aff;
2487 ctx = isl_aff_get_ctx(aff);
2488 if (first > isl_local_space_dim(aff->ls, type))
2489 isl_die(ctx, isl_error_invalid, "position out of bounds",
2490 return isl_aff_free(aff));
2492 aff = isl_aff_cow(aff);
2493 if (!aff)
2494 return NULL;
2496 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2497 if (!aff->ls)
2498 return isl_aff_free(aff);
2500 first += 1 + isl_local_space_offset(aff->ls, type);
2501 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2502 if (!aff->v)
2503 return isl_aff_free(aff);
2505 return aff;
2508 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2509 enum isl_dim_type type, unsigned n)
2511 unsigned pos;
2513 pos = isl_aff_dim(aff, type);
2515 return isl_aff_insert_dims(aff, type, pos, n);
2518 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2519 enum isl_dim_type type, unsigned n)
2521 unsigned pos;
2523 pos = isl_pw_aff_dim(pwaff, type);
2525 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2528 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2529 * to dimensions of "dst_type" at "dst_pos".
2531 * We only support moving input dimensions to parameters and vice versa.
2533 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2534 enum isl_dim_type dst_type, unsigned dst_pos,
2535 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2537 unsigned g_dst_pos;
2538 unsigned g_src_pos;
2540 if (!aff)
2541 return NULL;
2542 if (n == 0 &&
2543 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2544 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2545 return aff;
2547 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2548 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2549 "cannot move output/set dimension",
2550 return isl_aff_free(aff));
2551 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2552 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2553 "cannot move divs", return isl_aff_free(aff));
2554 if (dst_type == isl_dim_in)
2555 dst_type = isl_dim_set;
2556 if (src_type == isl_dim_in)
2557 src_type = isl_dim_set;
2559 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2560 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2561 "range out of bounds", return isl_aff_free(aff));
2562 if (dst_type == src_type)
2563 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2564 "moving dims within the same type not supported",
2565 return isl_aff_free(aff));
2567 aff = isl_aff_cow(aff);
2568 if (!aff)
2569 return NULL;
2571 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2572 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2573 if (dst_type > src_type)
2574 g_dst_pos -= n;
2576 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2577 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2578 src_type, src_pos, n);
2579 if (!aff->v || !aff->ls)
2580 return isl_aff_free(aff);
2582 aff = sort_divs(aff);
2584 return aff;
2587 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2589 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2590 return isl_pw_aff_alloc(dom, aff);
2593 #define isl_aff_involves_nan isl_aff_is_nan
2595 #undef PW
2596 #define PW isl_pw_aff
2597 #undef EL
2598 #define EL isl_aff
2599 #undef EL_IS_ZERO
2600 #define EL_IS_ZERO is_empty
2601 #undef ZERO
2602 #define ZERO empty
2603 #undef IS_ZERO
2604 #define IS_ZERO is_empty
2605 #undef FIELD
2606 #define FIELD aff
2607 #undef DEFAULT_IS_ZERO
2608 #define DEFAULT_IS_ZERO 0
2610 #define NO_EVAL
2611 #define NO_OPT
2612 #define NO_LIFT
2613 #define NO_MORPH
2615 #include <isl_pw_templ.c>
2616 #include <isl_pw_hash.c>
2617 #include <isl_pw_union_opt.c>
2619 #undef UNION
2620 #define UNION isl_union_pw_aff
2621 #undef PART
2622 #define PART isl_pw_aff
2623 #undef PARTS
2624 #define PARTS pw_aff
2626 #include <isl_union_single.c>
2627 #include <isl_union_neg.c>
2629 static __isl_give isl_set *align_params_pw_pw_set_and(
2630 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2631 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2632 __isl_take isl_pw_aff *pwaff2))
2634 isl_bool equal_params;
2636 if (!pwaff1 || !pwaff2)
2637 goto error;
2638 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2639 if (equal_params < 0)
2640 goto error;
2641 if (equal_params)
2642 return fn(pwaff1, pwaff2);
2643 if (!isl_space_has_named_params(pwaff1->dim) ||
2644 !isl_space_has_named_params(pwaff2->dim))
2645 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2646 "unaligned unnamed parameters", goto error);
2647 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2648 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2649 return fn(pwaff1, pwaff2);
2650 error:
2651 isl_pw_aff_free(pwaff1);
2652 isl_pw_aff_free(pwaff2);
2653 return NULL;
2656 /* Align the parameters of the to isl_pw_aff arguments and
2657 * then apply a function "fn" on them that returns an isl_map.
2659 static __isl_give isl_map *align_params_pw_pw_map_and(
2660 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2661 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2662 __isl_take isl_pw_aff *pa2))
2664 isl_bool equal_params;
2666 if (!pa1 || !pa2)
2667 goto error;
2668 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2669 if (equal_params < 0)
2670 goto error;
2671 if (equal_params)
2672 return fn(pa1, pa2);
2673 if (!isl_space_has_named_params(pa1->dim) ||
2674 !isl_space_has_named_params(pa2->dim))
2675 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2676 "unaligned unnamed parameters", goto error);
2677 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2678 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2679 return fn(pa1, pa2);
2680 error:
2681 isl_pw_aff_free(pa1);
2682 isl_pw_aff_free(pa2);
2683 return NULL;
2686 /* Compute a piecewise quasi-affine expression with a domain that
2687 * is the union of those of pwaff1 and pwaff2 and such that on each
2688 * cell, the quasi-affine expression is the maximum of those of pwaff1
2689 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2690 * cell, then the associated expression is the defined one.
2692 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2693 __isl_take isl_pw_aff *pwaff2)
2695 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2698 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2699 __isl_take isl_pw_aff *pwaff2)
2701 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2702 &pw_aff_union_max);
2705 /* Compute a piecewise quasi-affine expression with a domain that
2706 * is the union of those of pwaff1 and pwaff2 and such that on each
2707 * cell, the quasi-affine expression is the minimum of those of pwaff1
2708 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2709 * cell, then the associated expression is the defined one.
2711 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2712 __isl_take isl_pw_aff *pwaff2)
2714 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2717 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2718 __isl_take isl_pw_aff *pwaff2)
2720 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2721 &pw_aff_union_min);
2724 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2725 __isl_take isl_pw_aff *pwaff2, int max)
2727 if (max)
2728 return isl_pw_aff_union_max(pwaff1, pwaff2);
2729 else
2730 return isl_pw_aff_union_min(pwaff1, pwaff2);
2733 /* Construct a map with as domain the domain of pwaff and
2734 * one-dimensional range corresponding to the affine expressions.
2736 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2738 int i;
2739 isl_space *dim;
2740 isl_map *map;
2742 if (!pwaff)
2743 return NULL;
2745 dim = isl_pw_aff_get_space(pwaff);
2746 map = isl_map_empty(dim);
2748 for (i = 0; i < pwaff->n; ++i) {
2749 isl_basic_map *bmap;
2750 isl_map *map_i;
2752 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2753 map_i = isl_map_from_basic_map(bmap);
2754 map_i = isl_map_intersect_domain(map_i,
2755 isl_set_copy(pwaff->p[i].set));
2756 map = isl_map_union_disjoint(map, map_i);
2759 isl_pw_aff_free(pwaff);
2761 return map;
2764 /* Construct a map with as domain the domain of pwaff and
2765 * one-dimensional range corresponding to the affine expressions.
2767 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2769 if (!pwaff)
2770 return NULL;
2771 if (isl_space_is_set(pwaff->dim))
2772 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2773 "space of input is not a map", goto error);
2774 return map_from_pw_aff(pwaff);
2775 error:
2776 isl_pw_aff_free(pwaff);
2777 return NULL;
2780 /* Construct a one-dimensional set with as parameter domain
2781 * the domain of pwaff and the single set dimension
2782 * corresponding to the affine expressions.
2784 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2786 if (!pwaff)
2787 return NULL;
2788 if (!isl_space_is_set(pwaff->dim))
2789 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2790 "space of input is not a set", goto error);
2791 return map_from_pw_aff(pwaff);
2792 error:
2793 isl_pw_aff_free(pwaff);
2794 return NULL;
2797 /* Return a set containing those elements in the domain
2798 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2799 * does not satisfy "fn" (if complement is 1).
2801 * The pieces with a NaN never belong to the result since
2802 * NaN does not satisfy any property.
2804 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2805 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2806 int complement)
2808 int i;
2809 isl_set *set;
2811 if (!pwaff)
2812 return NULL;
2814 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2816 for (i = 0; i < pwaff->n; ++i) {
2817 isl_basic_set *bset;
2818 isl_set *set_i, *locus;
2819 isl_bool rational;
2821 if (isl_aff_is_nan(pwaff->p[i].aff))
2822 continue;
2824 rational = isl_set_has_rational(pwaff->p[i].set);
2825 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2826 locus = isl_set_from_basic_set(bset);
2827 set_i = isl_set_copy(pwaff->p[i].set);
2828 if (complement)
2829 set_i = isl_set_subtract(set_i, locus);
2830 else
2831 set_i = isl_set_intersect(set_i, locus);
2832 set = isl_set_union_disjoint(set, set_i);
2835 isl_pw_aff_free(pwaff);
2837 return set;
2840 /* Return a set containing those elements in the domain
2841 * of "pa" where it is positive.
2843 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2845 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2848 /* Return a set containing those elements in the domain
2849 * of pwaff where it is non-negative.
2851 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2853 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2856 /* Return a set containing those elements in the domain
2857 * of pwaff where it is zero.
2859 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2861 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2864 /* Return a set containing those elements in the domain
2865 * of pwaff where it is not zero.
2867 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2869 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2872 /* Return a set containing those elements in the shared domain
2873 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2875 * We compute the difference on the shared domain and then construct
2876 * the set of values where this difference is non-negative.
2877 * If strict is set, we first subtract 1 from the difference.
2878 * If equal is set, we only return the elements where pwaff1 and pwaff2
2879 * are equal.
2881 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2882 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2884 isl_set *set1, *set2;
2886 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2887 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2888 set1 = isl_set_intersect(set1, set2);
2889 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2890 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2891 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2893 if (strict) {
2894 isl_space *dim = isl_set_get_space(set1);
2895 isl_aff *aff;
2896 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2897 aff = isl_aff_add_constant_si(aff, -1);
2898 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2899 } else
2900 isl_set_free(set1);
2902 if (equal)
2903 return isl_pw_aff_zero_set(pwaff1);
2904 return isl_pw_aff_nonneg_set(pwaff1);
2907 /* Return a set containing those elements in the shared domain
2908 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2910 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2911 __isl_take isl_pw_aff *pwaff2)
2913 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2916 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2917 __isl_take isl_pw_aff *pwaff2)
2919 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2922 /* Return a set containing those elements in the shared domain
2923 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2925 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2926 __isl_take isl_pw_aff *pwaff2)
2928 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2931 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2932 __isl_take isl_pw_aff *pwaff2)
2934 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2937 /* Return a set containing those elements in the shared domain
2938 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2940 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2941 __isl_take isl_pw_aff *pwaff2)
2943 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2946 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2947 __isl_take isl_pw_aff *pwaff2)
2949 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2952 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2953 __isl_take isl_pw_aff *pwaff2)
2955 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2958 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2959 __isl_take isl_pw_aff *pwaff2)
2961 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2964 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2965 * where the function values are ordered in the same way as "order",
2966 * which returns a set in the shared domain of its two arguments.
2967 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2969 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2970 * We first pull back the two functions such that they are defined on
2971 * the domain [A -> B]. Then we apply "order", resulting in a set
2972 * in the space [A -> B]. Finally, we unwrap this set to obtain
2973 * a map in the space A -> B.
2975 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2976 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2977 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2978 __isl_take isl_pw_aff *pa2))
2980 isl_space *space1, *space2;
2981 isl_multi_aff *ma;
2982 isl_set *set;
2984 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2985 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2986 space1 = isl_space_map_from_domain_and_range(space1, space2);
2987 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2988 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2989 ma = isl_multi_aff_range_map(space1);
2990 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2991 set = order(pa1, pa2);
2993 return isl_set_unwrap(set);
2996 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2997 * where the function values are equal.
2998 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3000 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3001 __isl_take isl_pw_aff *pa2)
3003 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3006 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3007 * where the function values are equal.
3009 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3010 __isl_take isl_pw_aff *pa2)
3012 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3015 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3016 * where the function value of "pa1" is less than the function value of "pa2".
3017 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3019 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3020 __isl_take isl_pw_aff *pa2)
3022 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3025 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3026 * where the function value of "pa1" is less than the function value of "pa2".
3028 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3029 __isl_take isl_pw_aff *pa2)
3031 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3034 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3035 * where the function value of "pa1" is greater than the function value
3036 * of "pa2".
3037 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3039 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3040 __isl_take isl_pw_aff *pa2)
3042 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3045 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3046 * where the function value of "pa1" is greater than the function value
3047 * of "pa2".
3049 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3050 __isl_take isl_pw_aff *pa2)
3052 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3055 /* Return a set containing those elements in the shared domain
3056 * of the elements of list1 and list2 where each element in list1
3057 * has the relation specified by "fn" with each element in list2.
3059 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3060 __isl_take isl_pw_aff_list *list2,
3061 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3062 __isl_take isl_pw_aff *pwaff2))
3064 int i, j;
3065 isl_ctx *ctx;
3066 isl_set *set;
3068 if (!list1 || !list2)
3069 goto error;
3071 ctx = isl_pw_aff_list_get_ctx(list1);
3072 if (list1->n < 1 || list2->n < 1)
3073 isl_die(ctx, isl_error_invalid,
3074 "list should contain at least one element", goto error);
3076 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3077 for (i = 0; i < list1->n; ++i)
3078 for (j = 0; j < list2->n; ++j) {
3079 isl_set *set_ij;
3081 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3082 isl_pw_aff_copy(list2->p[j]));
3083 set = isl_set_intersect(set, set_ij);
3086 isl_pw_aff_list_free(list1);
3087 isl_pw_aff_list_free(list2);
3088 return set;
3089 error:
3090 isl_pw_aff_list_free(list1);
3091 isl_pw_aff_list_free(list2);
3092 return NULL;
3095 /* Return a set containing those elements in the shared domain
3096 * of the elements of list1 and list2 where each element in list1
3097 * is equal to each element in list2.
3099 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3100 __isl_take isl_pw_aff_list *list2)
3102 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3105 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3106 __isl_take isl_pw_aff_list *list2)
3108 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3111 /* Return a set containing those elements in the shared domain
3112 * of the elements of list1 and list2 where each element in list1
3113 * is less than or equal to each element in list2.
3115 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3116 __isl_take isl_pw_aff_list *list2)
3118 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3121 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3122 __isl_take isl_pw_aff_list *list2)
3124 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3127 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3128 __isl_take isl_pw_aff_list *list2)
3130 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3133 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3134 __isl_take isl_pw_aff_list *list2)
3136 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3140 /* Return a set containing those elements in the shared domain
3141 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3143 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3144 __isl_take isl_pw_aff *pwaff2)
3146 isl_set *set_lt, *set_gt;
3148 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3149 isl_pw_aff_copy(pwaff2));
3150 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3151 return isl_set_union_disjoint(set_lt, set_gt);
3154 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3155 __isl_take isl_pw_aff *pwaff2)
3157 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3160 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3161 isl_int v)
3163 int i;
3165 if (isl_int_is_one(v))
3166 return pwaff;
3167 if (!isl_int_is_pos(v))
3168 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3169 "factor needs to be positive",
3170 return isl_pw_aff_free(pwaff));
3171 pwaff = isl_pw_aff_cow(pwaff);
3172 if (!pwaff)
3173 return NULL;
3174 if (pwaff->n == 0)
3175 return pwaff;
3177 for (i = 0; i < pwaff->n; ++i) {
3178 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3179 if (!pwaff->p[i].aff)
3180 return isl_pw_aff_free(pwaff);
3183 return pwaff;
3186 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3188 int i;
3190 pwaff = isl_pw_aff_cow(pwaff);
3191 if (!pwaff)
3192 return NULL;
3193 if (pwaff->n == 0)
3194 return pwaff;
3196 for (i = 0; i < pwaff->n; ++i) {
3197 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3198 if (!pwaff->p[i].aff)
3199 return isl_pw_aff_free(pwaff);
3202 return pwaff;
3205 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3207 int i;
3209 pwaff = isl_pw_aff_cow(pwaff);
3210 if (!pwaff)
3211 return NULL;
3212 if (pwaff->n == 0)
3213 return pwaff;
3215 for (i = 0; i < pwaff->n; ++i) {
3216 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3217 if (!pwaff->p[i].aff)
3218 return isl_pw_aff_free(pwaff);
3221 return pwaff;
3224 /* Assuming that "cond1" and "cond2" are disjoint,
3225 * return an affine expression that is equal to pwaff1 on cond1
3226 * and to pwaff2 on cond2.
3228 static __isl_give isl_pw_aff *isl_pw_aff_select(
3229 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3230 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3232 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3233 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3235 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3238 /* Return an affine expression that is equal to pwaff_true for elements
3239 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3240 * is zero.
3241 * That is, return cond ? pwaff_true : pwaff_false;
3243 * If "cond" involves and NaN, then we conservatively return a NaN
3244 * on its entire domain. In principle, we could consider the pieces
3245 * where it is NaN separately from those where it is not.
3247 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3248 * then only use the domain of "cond" to restrict the domain.
3250 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3251 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3253 isl_set *cond_true, *cond_false;
3254 isl_bool equal;
3256 if (!cond)
3257 goto error;
3258 if (isl_pw_aff_involves_nan(cond)) {
3259 isl_space *space = isl_pw_aff_get_domain_space(cond);
3260 isl_local_space *ls = isl_local_space_from_space(space);
3261 isl_pw_aff_free(cond);
3262 isl_pw_aff_free(pwaff_true);
3263 isl_pw_aff_free(pwaff_false);
3264 return isl_pw_aff_nan_on_domain(ls);
3267 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3268 isl_pw_aff_get_space(pwaff_false));
3269 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3270 isl_pw_aff_get_space(pwaff_true));
3271 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3272 if (equal < 0)
3273 goto error;
3274 if (equal) {
3275 isl_set *dom;
3277 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3278 isl_pw_aff_free(pwaff_false);
3279 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3282 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3283 cond_false = isl_pw_aff_zero_set(cond);
3284 return isl_pw_aff_select(cond_true, pwaff_true,
3285 cond_false, pwaff_false);
3286 error:
3287 isl_pw_aff_free(cond);
3288 isl_pw_aff_free(pwaff_true);
3289 isl_pw_aff_free(pwaff_false);
3290 return NULL;
3293 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3295 if (!aff)
3296 return isl_bool_error;
3298 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3301 /* Check whether pwaff is a piecewise constant.
3303 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3305 int i;
3307 if (!pwaff)
3308 return isl_bool_error;
3310 for (i = 0; i < pwaff->n; ++i) {
3311 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3312 if (is_cst < 0 || !is_cst)
3313 return is_cst;
3316 return isl_bool_true;
3319 /* Are all elements of "mpa" piecewise constants?
3321 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3323 int i;
3325 if (!mpa)
3326 return isl_bool_error;
3328 for (i = 0; i < mpa->n; ++i) {
3329 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3330 if (is_cst < 0 || !is_cst)
3331 return is_cst;
3334 return isl_bool_true;
3337 /* Return the product of "aff1" and "aff2".
3339 * If either of the two is NaN, then the result is NaN.
3341 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3343 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3344 __isl_take isl_aff *aff2)
3346 if (!aff1 || !aff2)
3347 goto error;
3349 if (isl_aff_is_nan(aff1)) {
3350 isl_aff_free(aff2);
3351 return aff1;
3353 if (isl_aff_is_nan(aff2)) {
3354 isl_aff_free(aff1);
3355 return aff2;
3358 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3359 return isl_aff_mul(aff2, aff1);
3361 if (!isl_aff_is_cst(aff2))
3362 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3363 "at least one affine expression should be constant",
3364 goto error);
3366 aff1 = isl_aff_cow(aff1);
3367 if (!aff1 || !aff2)
3368 goto error;
3370 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3371 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3373 isl_aff_free(aff2);
3374 return aff1;
3375 error:
3376 isl_aff_free(aff1);
3377 isl_aff_free(aff2);
3378 return NULL;
3381 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3383 * If either of the two is NaN, then the result is NaN.
3385 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3386 __isl_take isl_aff *aff2)
3388 int is_cst;
3389 int neg;
3391 if (!aff1 || !aff2)
3392 goto error;
3394 if (isl_aff_is_nan(aff1)) {
3395 isl_aff_free(aff2);
3396 return aff1;
3398 if (isl_aff_is_nan(aff2)) {
3399 isl_aff_free(aff1);
3400 return aff2;
3403 is_cst = isl_aff_is_cst(aff2);
3404 if (is_cst < 0)
3405 goto error;
3406 if (!is_cst)
3407 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3408 "second argument should be a constant", goto error);
3410 if (!aff2)
3411 goto error;
3413 neg = isl_int_is_neg(aff2->v->el[1]);
3414 if (neg) {
3415 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3416 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3419 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3420 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3422 if (neg) {
3423 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3424 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3427 isl_aff_free(aff2);
3428 return aff1;
3429 error:
3430 isl_aff_free(aff1);
3431 isl_aff_free(aff2);
3432 return NULL;
3435 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3436 __isl_take isl_pw_aff *pwaff2)
3438 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3441 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3442 __isl_take isl_pw_aff *pwaff2)
3444 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3447 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3448 __isl_take isl_pw_aff *pwaff2)
3450 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3453 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3454 __isl_take isl_pw_aff *pwaff2)
3456 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3459 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3460 __isl_take isl_pw_aff *pwaff2)
3462 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3465 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3466 __isl_take isl_pw_aff *pa2)
3468 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3471 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3473 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3474 __isl_take isl_pw_aff *pa2)
3476 int is_cst;
3478 is_cst = isl_pw_aff_is_cst(pa2);
3479 if (is_cst < 0)
3480 goto error;
3481 if (!is_cst)
3482 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3483 "second argument should be a piecewise constant",
3484 goto error);
3485 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3486 error:
3487 isl_pw_aff_free(pa1);
3488 isl_pw_aff_free(pa2);
3489 return NULL;
3492 /* Compute the quotient of the integer division of "pa1" by "pa2"
3493 * with rounding towards zero.
3494 * "pa2" is assumed to be a piecewise constant.
3496 * In particular, return
3498 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3501 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3502 __isl_take isl_pw_aff *pa2)
3504 int is_cst;
3505 isl_set *cond;
3506 isl_pw_aff *f, *c;
3508 is_cst = isl_pw_aff_is_cst(pa2);
3509 if (is_cst < 0)
3510 goto error;
3511 if (!is_cst)
3512 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3513 "second argument should be a piecewise constant",
3514 goto error);
3516 pa1 = isl_pw_aff_div(pa1, pa2);
3518 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3519 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3520 c = isl_pw_aff_ceil(pa1);
3521 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3522 error:
3523 isl_pw_aff_free(pa1);
3524 isl_pw_aff_free(pa2);
3525 return NULL;
3528 /* Compute the remainder of the integer division of "pa1" by "pa2"
3529 * with rounding towards zero.
3530 * "pa2" is assumed to be a piecewise constant.
3532 * In particular, return
3534 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3537 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3538 __isl_take isl_pw_aff *pa2)
3540 int is_cst;
3541 isl_pw_aff *res;
3543 is_cst = isl_pw_aff_is_cst(pa2);
3544 if (is_cst < 0)
3545 goto error;
3546 if (!is_cst)
3547 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3548 "second argument should be a piecewise constant",
3549 goto error);
3550 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3551 res = isl_pw_aff_mul(pa2, res);
3552 res = isl_pw_aff_sub(pa1, res);
3553 return res;
3554 error:
3555 isl_pw_aff_free(pa1);
3556 isl_pw_aff_free(pa2);
3557 return NULL;
3560 /* Does either of "pa1" or "pa2" involve any NaN2?
3562 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3563 __isl_keep isl_pw_aff *pa2)
3565 isl_bool has_nan;
3567 has_nan = isl_pw_aff_involves_nan(pa1);
3568 if (has_nan < 0 || has_nan)
3569 return has_nan;
3570 return isl_pw_aff_involves_nan(pa2);
3573 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3574 * by a NaN on their shared domain.
3576 * In principle, the result could be refined to only being NaN
3577 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3579 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3580 __isl_take isl_pw_aff *pa2)
3582 isl_local_space *ls;
3583 isl_set *dom;
3584 isl_pw_aff *pa;
3586 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3587 ls = isl_local_space_from_space(isl_set_get_space(dom));
3588 pa = isl_pw_aff_nan_on_domain(ls);
3589 pa = isl_pw_aff_intersect_domain(pa, dom);
3591 return pa;
3594 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3595 __isl_take isl_pw_aff *pwaff2)
3597 isl_set *le;
3598 isl_set *dom;
3600 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3601 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3602 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3603 isl_pw_aff_copy(pwaff2));
3604 dom = isl_set_subtract(dom, isl_set_copy(le));
3605 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3608 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3609 __isl_take isl_pw_aff *pwaff2)
3611 isl_set *ge;
3612 isl_set *dom;
3614 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3615 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3616 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3617 isl_pw_aff_copy(pwaff2));
3618 dom = isl_set_subtract(dom, isl_set_copy(ge));
3619 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3622 /* Return an expression for the minimum (if "max" is not set) or
3623 * the maximum (if "max" is set) of "pa1" and "pa2".
3624 * If either expression involves any NaN, then return a NaN
3625 * on the shared domain as result.
3627 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3628 __isl_take isl_pw_aff *pa2, int max)
3630 isl_bool has_nan;
3632 has_nan = either_involves_nan(pa1, pa2);
3633 if (has_nan < 0)
3634 pa1 = isl_pw_aff_free(pa1);
3635 else if (has_nan)
3636 return replace_by_nan(pa1, pa2);
3638 if (max)
3639 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3640 else
3641 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3644 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3646 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3647 __isl_take isl_pw_aff *pwaff2)
3649 return pw_aff_min_max(pwaff1, pwaff2, 0);
3652 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3654 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3655 __isl_take isl_pw_aff *pwaff2)
3657 return pw_aff_min_max(pwaff1, pwaff2, 1);
3660 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3661 __isl_take isl_pw_aff_list *list,
3662 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3663 __isl_take isl_pw_aff *pwaff2))
3665 int i;
3666 isl_ctx *ctx;
3667 isl_pw_aff *res;
3669 if (!list)
3670 return NULL;
3672 ctx = isl_pw_aff_list_get_ctx(list);
3673 if (list->n < 1)
3674 isl_die(ctx, isl_error_invalid,
3675 "list should contain at least one element", goto error);
3677 res = isl_pw_aff_copy(list->p[0]);
3678 for (i = 1; i < list->n; ++i)
3679 res = fn(res, isl_pw_aff_copy(list->p[i]));
3681 isl_pw_aff_list_free(list);
3682 return res;
3683 error:
3684 isl_pw_aff_list_free(list);
3685 return NULL;
3688 /* Return an isl_pw_aff that maps each element in the intersection of the
3689 * domains of the elements of list to the minimal corresponding affine
3690 * expression.
3692 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3694 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3697 /* Return an isl_pw_aff that maps each element in the intersection of the
3698 * domains of the elements of list to the maximal corresponding affine
3699 * expression.
3701 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3703 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3706 /* Mark the domains of "pwaff" as rational.
3708 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3710 int i;
3712 pwaff = isl_pw_aff_cow(pwaff);
3713 if (!pwaff)
3714 return NULL;
3715 if (pwaff->n == 0)
3716 return pwaff;
3718 for (i = 0; i < pwaff->n; ++i) {
3719 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3720 if (!pwaff->p[i].set)
3721 return isl_pw_aff_free(pwaff);
3724 return pwaff;
3727 /* Mark the domains of the elements of "list" as rational.
3729 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3730 __isl_take isl_pw_aff_list *list)
3732 int i, n;
3734 if (!list)
3735 return NULL;
3736 if (list->n == 0)
3737 return list;
3739 n = list->n;
3740 for (i = 0; i < n; ++i) {
3741 isl_pw_aff *pa;
3743 pa = isl_pw_aff_list_get_pw_aff(list, i);
3744 pa = isl_pw_aff_set_rational(pa);
3745 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3748 return list;
3751 /* Do the parameters of "aff" match those of "space"?
3753 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3754 __isl_keep isl_space *space)
3756 isl_space *aff_space;
3757 isl_bool match;
3759 if (!aff || !space)
3760 return isl_bool_error;
3762 aff_space = isl_aff_get_domain_space(aff);
3764 match = isl_space_has_equal_params(space, aff_space);
3766 isl_space_free(aff_space);
3767 return match;
3770 /* Check that the domain space of "aff" matches "space".
3772 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3773 __isl_keep isl_space *space)
3775 isl_space *aff_space;
3776 isl_bool match;
3778 if (!aff || !space)
3779 return isl_stat_error;
3781 aff_space = isl_aff_get_domain_space(aff);
3783 match = isl_space_has_equal_params(space, aff_space);
3784 if (match < 0)
3785 goto error;
3786 if (!match)
3787 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3788 "parameters don't match", goto error);
3789 match = isl_space_tuple_is_equal(space, isl_dim_in,
3790 aff_space, isl_dim_set);
3791 if (match < 0)
3792 goto error;
3793 if (!match)
3794 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3795 "domains don't match", goto error);
3796 isl_space_free(aff_space);
3797 return isl_stat_ok;
3798 error:
3799 isl_space_free(aff_space);
3800 return isl_stat_error;
3803 #undef BASE
3804 #define BASE aff
3805 #undef DOMBASE
3806 #define DOMBASE set
3807 #define NO_DOMAIN
3809 #include <isl_multi_templ.c>
3810 #include <isl_multi_apply_set.c>
3811 #include <isl_multi_cmp.c>
3812 #include <isl_multi_dims.c>
3813 #include <isl_multi_floor.c>
3814 #include <isl_multi_gist.c>
3816 #undef NO_DOMAIN
3818 /* Construct an isl_multi_aff living in "space" that corresponds
3819 * to the affine transformation matrix "mat".
3821 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3822 __isl_take isl_space *space, __isl_take isl_mat *mat)
3824 isl_ctx *ctx;
3825 isl_local_space *ls = NULL;
3826 isl_multi_aff *ma = NULL;
3827 int n_row, n_col, n_out, total;
3828 int i;
3830 if (!space || !mat)
3831 goto error;
3833 ctx = isl_mat_get_ctx(mat);
3835 n_row = isl_mat_rows(mat);
3836 n_col = isl_mat_cols(mat);
3837 if (n_row < 1)
3838 isl_die(ctx, isl_error_invalid,
3839 "insufficient number of rows", goto error);
3840 if (n_col < 1)
3841 isl_die(ctx, isl_error_invalid,
3842 "insufficient number of columns", goto error);
3843 n_out = isl_space_dim(space, isl_dim_out);
3844 total = isl_space_dim(space, isl_dim_all);
3845 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3846 isl_die(ctx, isl_error_invalid,
3847 "dimension mismatch", goto error);
3849 ma = isl_multi_aff_zero(isl_space_copy(space));
3850 ls = isl_local_space_from_space(isl_space_domain(space));
3852 for (i = 0; i < n_row - 1; ++i) {
3853 isl_vec *v;
3854 isl_aff *aff;
3856 v = isl_vec_alloc(ctx, 1 + n_col);
3857 if (!v)
3858 goto error;
3859 isl_int_set(v->el[0], mat->row[0][0]);
3860 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3861 v = isl_vec_normalize(v);
3862 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3863 ma = isl_multi_aff_set_aff(ma, i, aff);
3866 isl_local_space_free(ls);
3867 isl_mat_free(mat);
3868 return ma;
3869 error:
3870 isl_local_space_free(ls);
3871 isl_mat_free(mat);
3872 isl_multi_aff_free(ma);
3873 return NULL;
3876 /* Remove any internal structure of the domain of "ma".
3877 * If there is any such internal structure in the input,
3878 * then the name of the corresponding space is also removed.
3880 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3881 __isl_take isl_multi_aff *ma)
3883 isl_space *space;
3885 if (!ma)
3886 return NULL;
3888 if (!ma->space->nested[0])
3889 return ma;
3891 space = isl_multi_aff_get_space(ma);
3892 space = isl_space_flatten_domain(space);
3893 ma = isl_multi_aff_reset_space(ma, space);
3895 return ma;
3898 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3899 * of the space to its domain.
3901 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3903 int i, n_in;
3904 isl_local_space *ls;
3905 isl_multi_aff *ma;
3907 if (!space)
3908 return NULL;
3909 if (!isl_space_is_map(space))
3910 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3911 "not a map space", goto error);
3913 n_in = isl_space_dim(space, isl_dim_in);
3914 space = isl_space_domain_map(space);
3916 ma = isl_multi_aff_alloc(isl_space_copy(space));
3917 if (n_in == 0) {
3918 isl_space_free(space);
3919 return ma;
3922 space = isl_space_domain(space);
3923 ls = isl_local_space_from_space(space);
3924 for (i = 0; i < n_in; ++i) {
3925 isl_aff *aff;
3927 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3928 isl_dim_set, i);
3929 ma = isl_multi_aff_set_aff(ma, i, aff);
3931 isl_local_space_free(ls);
3932 return ma;
3933 error:
3934 isl_space_free(space);
3935 return NULL;
3938 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3939 * of the space to its range.
3941 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3943 int i, n_in, n_out;
3944 isl_local_space *ls;
3945 isl_multi_aff *ma;
3947 if (!space)
3948 return NULL;
3949 if (!isl_space_is_map(space))
3950 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3951 "not a map space", goto error);
3953 n_in = isl_space_dim(space, isl_dim_in);
3954 n_out = isl_space_dim(space, isl_dim_out);
3955 space = isl_space_range_map(space);
3957 ma = isl_multi_aff_alloc(isl_space_copy(space));
3958 if (n_out == 0) {
3959 isl_space_free(space);
3960 return ma;
3963 space = isl_space_domain(space);
3964 ls = isl_local_space_from_space(space);
3965 for (i = 0; i < n_out; ++i) {
3966 isl_aff *aff;
3968 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3969 isl_dim_set, n_in + i);
3970 ma = isl_multi_aff_set_aff(ma, i, aff);
3972 isl_local_space_free(ls);
3973 return ma;
3974 error:
3975 isl_space_free(space);
3976 return NULL;
3979 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3980 * of the space to its range.
3982 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3983 __isl_take isl_space *space)
3985 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3988 /* Given the space of a set and a range of set dimensions,
3989 * construct an isl_multi_aff that projects out those dimensions.
3991 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3992 __isl_take isl_space *space, enum isl_dim_type type,
3993 unsigned first, unsigned n)
3995 int i, dim;
3996 isl_local_space *ls;
3997 isl_multi_aff *ma;
3999 if (!space)
4000 return NULL;
4001 if (!isl_space_is_set(space))
4002 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4003 "expecting set space", goto error);
4004 if (type != isl_dim_set)
4005 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4006 "only set dimensions can be projected out", goto error);
4008 dim = isl_space_dim(space, isl_dim_set);
4009 if (first + n > dim)
4010 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4011 "range out of bounds", goto error);
4013 space = isl_space_from_domain(space);
4014 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4016 if (dim == n)
4017 return isl_multi_aff_alloc(space);
4019 ma = isl_multi_aff_alloc(isl_space_copy(space));
4020 space = isl_space_domain(space);
4021 ls = isl_local_space_from_space(space);
4023 for (i = 0; i < first; ++i) {
4024 isl_aff *aff;
4026 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4027 isl_dim_set, i);
4028 ma = isl_multi_aff_set_aff(ma, i, aff);
4031 for (i = 0; i < dim - (first + n); ++i) {
4032 isl_aff *aff;
4034 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4035 isl_dim_set, first + n + i);
4036 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4039 isl_local_space_free(ls);
4040 return ma;
4041 error:
4042 isl_space_free(space);
4043 return NULL;
4046 /* Given the space of a set and a range of set dimensions,
4047 * construct an isl_pw_multi_aff that projects out those dimensions.
4049 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4050 __isl_take isl_space *space, enum isl_dim_type type,
4051 unsigned first, unsigned n)
4053 isl_multi_aff *ma;
4055 ma = isl_multi_aff_project_out_map(space, type, first, n);
4056 return isl_pw_multi_aff_from_multi_aff(ma);
4059 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4060 * domain.
4062 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4063 __isl_take isl_multi_aff *ma)
4065 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4066 return isl_pw_multi_aff_alloc(dom, ma);
4069 /* Create a piecewise multi-affine expression in the given space that maps each
4070 * input dimension to the corresponding output dimension.
4072 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4073 __isl_take isl_space *space)
4075 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4078 /* Exploit the equalities in "eq" to simplify the affine expressions.
4080 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4081 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4083 int i;
4085 maff = isl_multi_aff_cow(maff);
4086 if (!maff || !eq)
4087 goto error;
4089 for (i = 0; i < maff->n; ++i) {
4090 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
4091 isl_basic_set_copy(eq));
4092 if (!maff->p[i])
4093 goto error;
4096 isl_basic_set_free(eq);
4097 return maff;
4098 error:
4099 isl_basic_set_free(eq);
4100 isl_multi_aff_free(maff);
4101 return NULL;
4104 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4105 isl_int f)
4107 int i;
4109 maff = isl_multi_aff_cow(maff);
4110 if (!maff)
4111 return NULL;
4113 for (i = 0; i < maff->n; ++i) {
4114 maff->p[i] = isl_aff_scale(maff->p[i], f);
4115 if (!maff->p[i])
4116 return isl_multi_aff_free(maff);
4119 return maff;
4122 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4123 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4125 maff1 = isl_multi_aff_add(maff1, maff2);
4126 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4127 return maff1;
4130 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4132 if (!maff)
4133 return -1;
4135 return 0;
4138 /* Return the set of domain elements where "ma1" is lexicographically
4139 * smaller than or equal to "ma2".
4141 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4142 __isl_take isl_multi_aff *ma2)
4144 return isl_multi_aff_lex_ge_set(ma2, ma1);
4147 /* Return the set of domain elements where "ma1" is lexicographically
4148 * smaller than "ma2".
4150 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4151 __isl_take isl_multi_aff *ma2)
4153 return isl_multi_aff_lex_gt_set(ma2, ma1);
4156 /* Return the set of domain elements where "ma1" and "ma2"
4157 * satisfy "order".
4159 static __isl_give isl_set *isl_multi_aff_order_set(
4160 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4161 __isl_give isl_map *order(__isl_take isl_space *set_space))
4163 isl_space *space;
4164 isl_map *map1, *map2;
4165 isl_map *map, *ge;
4167 map1 = isl_map_from_multi_aff(ma1);
4168 map2 = isl_map_from_multi_aff(ma2);
4169 map = isl_map_range_product(map1, map2);
4170 space = isl_space_range(isl_map_get_space(map));
4171 space = isl_space_domain(isl_space_unwrap(space));
4172 ge = order(space);
4173 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4175 return isl_map_domain(map);
4178 /* Return the set of domain elements where "ma1" is lexicographically
4179 * greater than or equal to "ma2".
4181 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4182 __isl_take isl_multi_aff *ma2)
4184 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4187 /* Return the set of domain elements where "ma1" is lexicographically
4188 * greater than "ma2".
4190 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4191 __isl_take isl_multi_aff *ma2)
4193 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4196 #undef PW
4197 #define PW isl_pw_multi_aff
4198 #undef EL
4199 #define EL isl_multi_aff
4200 #undef EL_IS_ZERO
4201 #define EL_IS_ZERO is_empty
4202 #undef ZERO
4203 #define ZERO empty
4204 #undef IS_ZERO
4205 #define IS_ZERO is_empty
4206 #undef FIELD
4207 #define FIELD maff
4208 #undef DEFAULT_IS_ZERO
4209 #define DEFAULT_IS_ZERO 0
4211 #define NO_SUB
4212 #define NO_EVAL
4213 #define NO_OPT
4214 #define NO_INVOLVES_DIMS
4215 #define NO_INSERT_DIMS
4216 #define NO_LIFT
4217 #define NO_MORPH
4219 #include <isl_pw_templ.c>
4220 #include <isl_pw_union_opt.c>
4222 #undef NO_SUB
4224 #undef UNION
4225 #define UNION isl_union_pw_multi_aff
4226 #undef PART
4227 #define PART isl_pw_multi_aff
4228 #undef PARTS
4229 #define PARTS pw_multi_aff
4231 #include <isl_union_multi.c>
4232 #include <isl_union_neg.c>
4234 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4235 __isl_take isl_pw_multi_aff *pma1,
4236 __isl_take isl_pw_multi_aff *pma2)
4238 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4239 &isl_multi_aff_lex_ge_set);
4242 /* Given two piecewise multi affine expressions, return a piecewise
4243 * multi-affine expression defined on the union of the definition domains
4244 * of the inputs that is equal to the lexicographic maximum of the two
4245 * inputs on each cell. If only one of the two inputs is defined on
4246 * a given cell, then it is considered to be the maximum.
4248 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4249 __isl_take isl_pw_multi_aff *pma1,
4250 __isl_take isl_pw_multi_aff *pma2)
4252 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4253 &pw_multi_aff_union_lexmax);
4256 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4257 __isl_take isl_pw_multi_aff *pma1,
4258 __isl_take isl_pw_multi_aff *pma2)
4260 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4261 &isl_multi_aff_lex_le_set);
4264 /* Given two piecewise multi affine expressions, return a piecewise
4265 * multi-affine expression defined on the union of the definition domains
4266 * of the inputs that is equal to the lexicographic minimum of the two
4267 * inputs on each cell. If only one of the two inputs is defined on
4268 * a given cell, then it is considered to be the minimum.
4270 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4271 __isl_take isl_pw_multi_aff *pma1,
4272 __isl_take isl_pw_multi_aff *pma2)
4274 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4275 &pw_multi_aff_union_lexmin);
4278 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4279 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4281 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4282 &isl_multi_aff_add);
4285 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4286 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4288 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4289 &pw_multi_aff_add);
4292 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4293 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4295 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4296 &isl_multi_aff_sub);
4299 /* Subtract "pma2" from "pma1" and return the result.
4301 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4302 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4304 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4305 &pw_multi_aff_sub);
4308 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4309 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4311 return isl_pw_multi_aff_union_add_(pma1, pma2);
4314 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4315 * with the actual sum on the shared domain and
4316 * the defined expression on the symmetric difference of the domains.
4318 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4319 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4321 return isl_union_pw_aff_union_add_(upa1, upa2);
4324 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4325 * with the actual sum on the shared domain and
4326 * the defined expression on the symmetric difference of the domains.
4328 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4329 __isl_take isl_union_pw_multi_aff *upma1,
4330 __isl_take isl_union_pw_multi_aff *upma2)
4332 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4335 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4336 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4338 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4339 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4341 int i, j, n;
4342 isl_space *space;
4343 isl_pw_multi_aff *res;
4345 if (!pma1 || !pma2)
4346 goto error;
4348 n = pma1->n * pma2->n;
4349 space = isl_space_product(isl_space_copy(pma1->dim),
4350 isl_space_copy(pma2->dim));
4351 res = isl_pw_multi_aff_alloc_size(space, n);
4353 for (i = 0; i < pma1->n; ++i) {
4354 for (j = 0; j < pma2->n; ++j) {
4355 isl_set *domain;
4356 isl_multi_aff *ma;
4358 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4359 isl_set_copy(pma2->p[j].set));
4360 ma = isl_multi_aff_product(
4361 isl_multi_aff_copy(pma1->p[i].maff),
4362 isl_multi_aff_copy(pma2->p[j].maff));
4363 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4367 isl_pw_multi_aff_free(pma1);
4368 isl_pw_multi_aff_free(pma2);
4369 return res;
4370 error:
4371 isl_pw_multi_aff_free(pma1);
4372 isl_pw_multi_aff_free(pma2);
4373 return NULL;
4376 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4377 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4379 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4380 &pw_multi_aff_product);
4383 /* Construct a map mapping the domain of the piecewise multi-affine expression
4384 * to its range, with each dimension in the range equated to the
4385 * corresponding affine expression on its cell.
4387 * If the domain of "pma" is rational, then so is the constructed "map".
4389 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4391 int i;
4392 isl_map *map;
4394 if (!pma)
4395 return NULL;
4397 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4399 for (i = 0; i < pma->n; ++i) {
4400 isl_bool rational;
4401 isl_multi_aff *maff;
4402 isl_basic_map *bmap;
4403 isl_map *map_i;
4405 rational = isl_set_is_rational(pma->p[i].set);
4406 if (rational < 0)
4407 map = isl_map_free(map);
4408 maff = isl_multi_aff_copy(pma->p[i].maff);
4409 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4410 map_i = isl_map_from_basic_map(bmap);
4411 map_i = isl_map_intersect_domain(map_i,
4412 isl_set_copy(pma->p[i].set));
4413 map = isl_map_union_disjoint(map, map_i);
4416 isl_pw_multi_aff_free(pma);
4417 return map;
4420 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4422 if (!pma)
4423 return NULL;
4425 if (!isl_space_is_set(pma->dim))
4426 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4427 "isl_pw_multi_aff cannot be converted into an isl_set",
4428 goto error);
4430 return isl_map_from_pw_multi_aff(pma);
4431 error:
4432 isl_pw_multi_aff_free(pma);
4433 return NULL;
4436 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4437 * denominator "denom".
4438 * "denom" is allowed to be negative, in which case the actual denominator
4439 * is -denom and the expressions are added instead.
4441 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4442 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4444 int i, first;
4445 int sign;
4446 isl_int d;
4448 first = isl_seq_first_non_zero(c, n);
4449 if (first == -1)
4450 return aff;
4452 sign = isl_int_sgn(denom);
4453 isl_int_init(d);
4454 isl_int_abs(d, denom);
4455 for (i = first; i < n; ++i) {
4456 isl_aff *aff_i;
4458 if (isl_int_is_zero(c[i]))
4459 continue;
4460 aff_i = isl_multi_aff_get_aff(ma, i);
4461 aff_i = isl_aff_scale(aff_i, c[i]);
4462 aff_i = isl_aff_scale_down(aff_i, d);
4463 if (sign >= 0)
4464 aff = isl_aff_sub(aff, aff_i);
4465 else
4466 aff = isl_aff_add(aff, aff_i);
4468 isl_int_clear(d);
4470 return aff;
4473 /* Extract an affine expression that expresses the output dimension "pos"
4474 * of "bmap" in terms of the parameters and input dimensions from
4475 * equality "eq".
4476 * Note that this expression may involve integer divisions defined
4477 * in terms of parameters and input dimensions.
4478 * The equality may also involve references to earlier (but not later)
4479 * output dimensions. These are replaced by the corresponding elements
4480 * in "ma".
4482 * If the equality is of the form
4484 * f(i) + h(j) + a x + g(i) = 0,
4486 * with f(i) a linear combinations of the parameters and input dimensions,
4487 * g(i) a linear combination of integer divisions defined in terms of the same
4488 * and h(j) a linear combinations of earlier output dimensions,
4489 * then the affine expression is
4491 * (-f(i) - g(i))/a - h(j)/a
4493 * If the equality is of the form
4495 * f(i) + h(j) - a x + g(i) = 0,
4497 * then the affine expression is
4499 * (f(i) + g(i))/a - h(j)/(-a)
4502 * If "div" refers to an integer division (i.e., it is smaller than
4503 * the number of integer divisions), then the equality constraint
4504 * does involve an integer division (the one at position "div") that
4505 * is defined in terms of output dimensions. However, this integer
4506 * division can be eliminated by exploiting a pair of constraints
4507 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4508 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4509 * -l + x >= 0.
4510 * In particular, let
4512 * x = e(i) + m floor(...)
4514 * with e(i) the expression derived above and floor(...) the integer
4515 * division involving output dimensions.
4516 * From
4518 * l <= x <= l + n,
4520 * we have
4522 * 0 <= x - l <= n
4524 * This means
4526 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4527 * = (e(i) - l) mod m
4529 * Therefore,
4531 * x - l = (e(i) - l) mod m
4533 * or
4535 * x = ((e(i) - l) mod m) + l
4537 * The variable "shift" below contains the expression -l, which may
4538 * also involve a linear combination of earlier output dimensions.
4540 static __isl_give isl_aff *extract_aff_from_equality(
4541 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4542 __isl_keep isl_multi_aff *ma)
4544 unsigned o_out;
4545 unsigned n_div, n_out;
4546 isl_ctx *ctx;
4547 isl_local_space *ls;
4548 isl_aff *aff, *shift;
4549 isl_val *mod;
4551 ctx = isl_basic_map_get_ctx(bmap);
4552 ls = isl_basic_map_get_local_space(bmap);
4553 ls = isl_local_space_domain(ls);
4554 aff = isl_aff_alloc(isl_local_space_copy(ls));
4555 if (!aff)
4556 goto error;
4557 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4558 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4559 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4560 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4561 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4562 isl_seq_cpy(aff->v->el + 1 + o_out,
4563 bmap->eq[eq] + o_out + n_out, n_div);
4564 } else {
4565 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4566 isl_seq_neg(aff->v->el + 1 + o_out,
4567 bmap->eq[eq] + o_out + n_out, n_div);
4569 if (div < n_div)
4570 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4571 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4572 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4573 bmap->eq[eq][o_out + pos]);
4574 if (div < n_div) {
4575 shift = isl_aff_alloc(isl_local_space_copy(ls));
4576 if (!shift)
4577 goto error;
4578 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4579 isl_seq_cpy(shift->v->el + 1 + o_out,
4580 bmap->ineq[ineq] + o_out + n_out, n_div);
4581 isl_int_set_si(shift->v->el[0], 1);
4582 shift = subtract_initial(shift, ma, pos,
4583 bmap->ineq[ineq] + o_out, ctx->negone);
4584 aff = isl_aff_add(aff, isl_aff_copy(shift));
4585 mod = isl_val_int_from_isl_int(ctx,
4586 bmap->eq[eq][o_out + n_out + div]);
4587 mod = isl_val_abs(mod);
4588 aff = isl_aff_mod_val(aff, mod);
4589 aff = isl_aff_sub(aff, shift);
4592 isl_local_space_free(ls);
4593 return aff;
4594 error:
4595 isl_local_space_free(ls);
4596 isl_aff_free(aff);
4597 return NULL;
4600 /* Given a basic map with output dimensions defined
4601 * in terms of the parameters input dimensions and earlier
4602 * output dimensions using an equality (and possibly a pair on inequalities),
4603 * extract an isl_aff that expresses output dimension "pos" in terms
4604 * of the parameters and input dimensions.
4605 * Note that this expression may involve integer divisions defined
4606 * in terms of parameters and input dimensions.
4607 * "ma" contains the expressions corresponding to earlier output dimensions.
4609 * This function shares some similarities with
4610 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4612 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4613 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4615 int eq, div, ineq;
4616 isl_aff *aff;
4618 if (!bmap)
4619 return NULL;
4620 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4621 if (eq >= bmap->n_eq)
4622 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4623 "unable to find suitable equality", return NULL);
4624 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4626 aff = isl_aff_remove_unused_divs(aff);
4627 return aff;
4630 /* Given a basic map where each output dimension is defined
4631 * in terms of the parameters and input dimensions using an equality,
4632 * extract an isl_multi_aff that expresses the output dimensions in terms
4633 * of the parameters and input dimensions.
4635 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4636 __isl_take isl_basic_map *bmap)
4638 int i;
4639 unsigned n_out;
4640 isl_multi_aff *ma;
4642 if (!bmap)
4643 return NULL;
4645 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4646 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4648 for (i = 0; i < n_out; ++i) {
4649 isl_aff *aff;
4651 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4652 ma = isl_multi_aff_set_aff(ma, i, aff);
4655 isl_basic_map_free(bmap);
4657 return ma;
4660 /* Given a basic set where each set dimension is defined
4661 * in terms of the parameters using an equality,
4662 * extract an isl_multi_aff that expresses the set dimensions in terms
4663 * of the parameters.
4665 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4666 __isl_take isl_basic_set *bset)
4668 return extract_isl_multi_aff_from_basic_map(bset);
4671 /* Create an isl_pw_multi_aff that is equivalent to
4672 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4673 * The given basic map is such that each output dimension is defined
4674 * in terms of the parameters and input dimensions using an equality.
4676 * Since some applications expect the result of isl_pw_multi_aff_from_map
4677 * to only contain integer affine expressions, we compute the floor
4678 * of the expression before returning.
4680 * Remove all constraints involving local variables without
4681 * an explicit representation (resulting in the removal of those
4682 * local variables) prior to the actual extraction to ensure
4683 * that the local spaces in which the resulting affine expressions
4684 * are created do not contain any unknown local variables.
4685 * Removing such constraints is safe because constraints involving
4686 * unknown local variables are not used to determine whether
4687 * a basic map is obviously single-valued.
4689 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4690 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4692 isl_multi_aff *ma;
4694 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4695 ma = extract_isl_multi_aff_from_basic_map(bmap);
4696 ma = isl_multi_aff_floor(ma);
4697 return isl_pw_multi_aff_alloc(domain, ma);
4700 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4701 * This obviously only works if the input "map" is single-valued.
4702 * If so, we compute the lexicographic minimum of the image in the form
4703 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4704 * to its lexicographic minimum.
4705 * If the input is not single-valued, we produce an error.
4707 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4708 __isl_take isl_map *map)
4710 int i;
4711 int sv;
4712 isl_pw_multi_aff *pma;
4714 sv = isl_map_is_single_valued(map);
4715 if (sv < 0)
4716 goto error;
4717 if (!sv)
4718 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4719 "map is not single-valued", goto error);
4720 map = isl_map_make_disjoint(map);
4721 if (!map)
4722 return NULL;
4724 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4726 for (i = 0; i < map->n; ++i) {
4727 isl_pw_multi_aff *pma_i;
4728 isl_basic_map *bmap;
4729 bmap = isl_basic_map_copy(map->p[i]);
4730 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4731 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4734 isl_map_free(map);
4735 return pma;
4736 error:
4737 isl_map_free(map);
4738 return NULL;
4741 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4742 * taking into account that the output dimension at position "d"
4743 * can be represented as
4745 * x = floor((e(...) + c1) / m)
4747 * given that constraint "i" is of the form
4749 * e(...) + c1 - m x >= 0
4752 * Let "map" be of the form
4754 * A -> B
4756 * We construct a mapping
4758 * A -> [A -> x = floor(...)]
4760 * apply that to the map, obtaining
4762 * [A -> x = floor(...)] -> B
4764 * and equate dimension "d" to x.
4765 * We then compute a isl_pw_multi_aff representation of the resulting map
4766 * and plug in the mapping above.
4768 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4769 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4771 isl_ctx *ctx;
4772 isl_space *space;
4773 isl_local_space *ls;
4774 isl_multi_aff *ma;
4775 isl_aff *aff;
4776 isl_vec *v;
4777 isl_map *insert;
4778 int offset;
4779 int n;
4780 int n_in;
4781 isl_pw_multi_aff *pma;
4782 isl_bool is_set;
4784 is_set = isl_map_is_set(map);
4785 if (is_set < 0)
4786 goto error;
4788 offset = isl_basic_map_offset(hull, isl_dim_out);
4789 ctx = isl_map_get_ctx(map);
4790 space = isl_space_domain(isl_map_get_space(map));
4791 n_in = isl_space_dim(space, isl_dim_set);
4792 n = isl_space_dim(space, isl_dim_all);
4794 v = isl_vec_alloc(ctx, 1 + 1 + n);
4795 if (v) {
4796 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4797 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4799 isl_basic_map_free(hull);
4801 ls = isl_local_space_from_space(isl_space_copy(space));
4802 aff = isl_aff_alloc_vec(ls, v);
4803 aff = isl_aff_floor(aff);
4804 if (is_set) {
4805 isl_space_free(space);
4806 ma = isl_multi_aff_from_aff(aff);
4807 } else {
4808 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4809 ma = isl_multi_aff_range_product(ma,
4810 isl_multi_aff_from_aff(aff));
4813 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4814 map = isl_map_apply_domain(map, insert);
4815 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4816 pma = isl_pw_multi_aff_from_map(map);
4817 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4819 return pma;
4820 error:
4821 isl_map_free(map);
4822 isl_basic_map_free(hull);
4823 return NULL;
4826 /* Is constraint "c" of the form
4828 * e(...) + c1 - m x >= 0
4830 * or
4832 * -e(...) + c2 + m x >= 0
4834 * where m > 1 and e only depends on parameters and input dimemnsions?
4836 * "offset" is the offset of the output dimensions
4837 * "pos" is the position of output dimension x.
4839 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4841 if (isl_int_is_zero(c[offset + d]))
4842 return 0;
4843 if (isl_int_is_one(c[offset + d]))
4844 return 0;
4845 if (isl_int_is_negone(c[offset + d]))
4846 return 0;
4847 if (isl_seq_first_non_zero(c + offset, d) != -1)
4848 return 0;
4849 if (isl_seq_first_non_zero(c + offset + d + 1,
4850 total - (offset + d + 1)) != -1)
4851 return 0;
4852 return 1;
4855 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4857 * As a special case, we first check if there is any pair of constraints,
4858 * shared by all the basic maps in "map" that force a given dimension
4859 * to be equal to the floor of some affine combination of the input dimensions.
4861 * In particular, if we can find two constraints
4863 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4865 * and
4867 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4869 * where m > 1 and e only depends on parameters and input dimemnsions,
4870 * and such that
4872 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4874 * then we know that we can take
4876 * x = floor((e(...) + c1) / m)
4878 * without having to perform any computation.
4880 * Note that we know that
4882 * c1 + c2 >= 1
4884 * If c1 + c2 were 0, then we would have detected an equality during
4885 * simplification. If c1 + c2 were negative, then we would have detected
4886 * a contradiction.
4888 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4889 __isl_take isl_map *map)
4891 int d, dim;
4892 int i, j, n;
4893 int offset, total;
4894 isl_int sum;
4895 isl_basic_map *hull;
4897 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4898 if (!hull)
4899 goto error;
4901 isl_int_init(sum);
4902 dim = isl_map_dim(map, isl_dim_out);
4903 offset = isl_basic_map_offset(hull, isl_dim_out);
4904 total = 1 + isl_basic_map_total_dim(hull);
4905 n = hull->n_ineq;
4906 for (d = 0; d < dim; ++d) {
4907 for (i = 0; i < n; ++i) {
4908 if (!is_potential_div_constraint(hull->ineq[i],
4909 offset, d, total))
4910 continue;
4911 for (j = i + 1; j < n; ++j) {
4912 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4913 hull->ineq[j] + 1, total - 1))
4914 continue;
4915 isl_int_add(sum, hull->ineq[i][0],
4916 hull->ineq[j][0]);
4917 if (isl_int_abs_lt(sum,
4918 hull->ineq[i][offset + d]))
4919 break;
4922 if (j >= n)
4923 continue;
4924 isl_int_clear(sum);
4925 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4926 j = i;
4927 return pw_multi_aff_from_map_div(map, hull, d, j);
4930 isl_int_clear(sum);
4931 isl_basic_map_free(hull);
4932 return pw_multi_aff_from_map_base(map);
4933 error:
4934 isl_map_free(map);
4935 isl_basic_map_free(hull);
4936 return NULL;
4939 /* Given an affine expression
4941 * [A -> B] -> f(A,B)
4943 * construct an isl_multi_aff
4945 * [A -> B] -> B'
4947 * such that dimension "d" in B' is set to "aff" and the remaining
4948 * dimensions are set equal to the corresponding dimensions in B.
4949 * "n_in" is the dimension of the space A.
4950 * "n_out" is the dimension of the space B.
4952 * If "is_set" is set, then the affine expression is of the form
4954 * [B] -> f(B)
4956 * and we construct an isl_multi_aff
4958 * B -> B'
4960 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4961 unsigned n_in, unsigned n_out, int is_set)
4963 int i;
4964 isl_multi_aff *ma;
4965 isl_space *space, *space2;
4966 isl_local_space *ls;
4968 space = isl_aff_get_domain_space(aff);
4969 ls = isl_local_space_from_space(isl_space_copy(space));
4970 space2 = isl_space_copy(space);
4971 if (!is_set)
4972 space2 = isl_space_range(isl_space_unwrap(space2));
4973 space = isl_space_map_from_domain_and_range(space, space2);
4974 ma = isl_multi_aff_alloc(space);
4975 ma = isl_multi_aff_set_aff(ma, d, aff);
4977 for (i = 0; i < n_out; ++i) {
4978 if (i == d)
4979 continue;
4980 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4981 isl_dim_set, n_in + i);
4982 ma = isl_multi_aff_set_aff(ma, i, aff);
4985 isl_local_space_free(ls);
4987 return ma;
4990 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4991 * taking into account that the dimension at position "d" can be written as
4993 * x = m a + f(..) (1)
4995 * where m is equal to "gcd".
4996 * "i" is the index of the equality in "hull" that defines f(..).
4997 * In particular, the equality is of the form
4999 * f(..) - x + m g(existentials) = 0
5001 * or
5003 * -f(..) + x + m g(existentials) = 0
5005 * We basically plug (1) into "map", resulting in a map with "a"
5006 * in the range instead of "x". The corresponding isl_pw_multi_aff
5007 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5009 * Specifically, given the input map
5011 * A -> B
5013 * We first wrap it into a set
5015 * [A -> B]
5017 * and define (1) on top of the corresponding space, resulting in "aff".
5018 * We use this to create an isl_multi_aff that maps the output position "d"
5019 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5020 * We plug this into the wrapped map, unwrap the result and compute the
5021 * corresponding isl_pw_multi_aff.
5022 * The result is an expression
5024 * A -> T(A)
5026 * We adjust that to
5028 * A -> [A -> T(A)]
5030 * so that we can plug that into "aff", after extending the latter to
5031 * a mapping
5033 * [A -> B] -> B'
5036 * If "map" is actually a set, then there is no "A" space, meaning
5037 * that we do not need to perform any wrapping, and that the result
5038 * of the recursive call is of the form
5040 * [T]
5042 * which is plugged into a mapping of the form
5044 * B -> B'
5046 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5047 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5048 isl_int gcd)
5050 isl_set *set;
5051 isl_space *space;
5052 isl_local_space *ls;
5053 isl_aff *aff;
5054 isl_multi_aff *ma;
5055 isl_pw_multi_aff *pma, *id;
5056 unsigned n_in;
5057 unsigned o_out;
5058 unsigned n_out;
5059 isl_bool is_set;
5061 is_set = isl_map_is_set(map);
5062 if (is_set < 0)
5063 goto error;
5065 n_in = isl_basic_map_dim(hull, isl_dim_in);
5066 n_out = isl_basic_map_dim(hull, isl_dim_out);
5067 o_out = isl_basic_map_offset(hull, isl_dim_out);
5069 if (is_set)
5070 set = map;
5071 else
5072 set = isl_map_wrap(map);
5073 space = isl_space_map_from_set(isl_set_get_space(set));
5074 ma = isl_multi_aff_identity(space);
5075 ls = isl_local_space_from_space(isl_set_get_space(set));
5076 aff = isl_aff_alloc(ls);
5077 if (aff) {
5078 isl_int_set_si(aff->v->el[0], 1);
5079 if (isl_int_is_one(hull->eq[i][o_out + d]))
5080 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5081 aff->v->size - 1);
5082 else
5083 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5084 aff->v->size - 1);
5085 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5087 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5088 set = isl_set_preimage_multi_aff(set, ma);
5090 ma = range_map(aff, d, n_in, n_out, is_set);
5092 if (is_set)
5093 map = set;
5094 else
5095 map = isl_set_unwrap(set);
5096 pma = isl_pw_multi_aff_from_map(map);
5098 if (!is_set) {
5099 space = isl_pw_multi_aff_get_domain_space(pma);
5100 space = isl_space_map_from_set(space);
5101 id = isl_pw_multi_aff_identity(space);
5102 pma = isl_pw_multi_aff_range_product(id, pma);
5104 id = isl_pw_multi_aff_from_multi_aff(ma);
5105 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5107 isl_basic_map_free(hull);
5108 return pma;
5109 error:
5110 isl_map_free(map);
5111 isl_basic_map_free(hull);
5112 return NULL;
5115 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5116 * "hull" contains the equalities valid for "map".
5118 * Check if any of the output dimensions is "strided".
5119 * That is, we check if it can be written as
5121 * x = m a + f(..)
5123 * with m greater than 1, a some combination of existentially quantified
5124 * variables and f an expression in the parameters and input dimensions.
5125 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5127 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5128 * special case.
5130 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5131 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5133 int i, j;
5134 unsigned n_out;
5135 unsigned o_out;
5136 unsigned n_div;
5137 unsigned o_div;
5138 isl_int gcd;
5140 n_div = isl_basic_map_dim(hull, isl_dim_div);
5141 o_div = isl_basic_map_offset(hull, isl_dim_div);
5143 if (n_div == 0) {
5144 isl_basic_map_free(hull);
5145 return pw_multi_aff_from_map_check_div(map);
5148 isl_int_init(gcd);
5150 n_out = isl_basic_map_dim(hull, isl_dim_out);
5151 o_out = isl_basic_map_offset(hull, isl_dim_out);
5153 for (i = 0; i < n_out; ++i) {
5154 for (j = 0; j < hull->n_eq; ++j) {
5155 isl_int *eq = hull->eq[j];
5156 isl_pw_multi_aff *res;
5158 if (!isl_int_is_one(eq[o_out + i]) &&
5159 !isl_int_is_negone(eq[o_out + i]))
5160 continue;
5161 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5162 continue;
5163 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5164 n_out - (i + 1)) != -1)
5165 continue;
5166 isl_seq_gcd(eq + o_div, n_div, &gcd);
5167 if (isl_int_is_zero(gcd))
5168 continue;
5169 if (isl_int_is_one(gcd))
5170 continue;
5172 res = pw_multi_aff_from_map_stride(map, hull,
5173 i, j, gcd);
5174 isl_int_clear(gcd);
5175 return res;
5179 isl_int_clear(gcd);
5180 isl_basic_map_free(hull);
5181 return pw_multi_aff_from_map_check_div(map);
5184 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5186 * As a special case, we first check if all output dimensions are uniquely
5187 * defined in terms of the parameters and input dimensions over the entire
5188 * domain. If so, we extract the desired isl_pw_multi_aff directly
5189 * from the affine hull of "map" and its domain.
5191 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5192 * special cases.
5194 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5196 isl_bool sv;
5197 isl_basic_map *hull;
5199 if (!map)
5200 return NULL;
5202 if (isl_map_n_basic_map(map) == 1) {
5203 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5204 hull = isl_basic_map_plain_affine_hull(hull);
5205 sv = isl_basic_map_plain_is_single_valued(hull);
5206 if (sv >= 0 && sv)
5207 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5208 hull);
5209 isl_basic_map_free(hull);
5211 map = isl_map_detect_equalities(map);
5212 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5213 sv = isl_basic_map_plain_is_single_valued(hull);
5214 if (sv >= 0 && sv)
5215 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5216 if (sv >= 0)
5217 return pw_multi_aff_from_map_check_strides(map, hull);
5218 isl_basic_map_free(hull);
5219 isl_map_free(map);
5220 return NULL;
5223 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5225 return isl_pw_multi_aff_from_map(set);
5228 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5229 * add it to *user.
5231 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5233 isl_union_pw_multi_aff **upma = user;
5234 isl_pw_multi_aff *pma;
5236 pma = isl_pw_multi_aff_from_map(map);
5237 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5239 return *upma ? isl_stat_ok : isl_stat_error;
5242 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5243 * domain.
5245 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5246 __isl_take isl_aff *aff)
5248 isl_multi_aff *ma;
5249 isl_pw_multi_aff *pma;
5251 ma = isl_multi_aff_from_aff(aff);
5252 pma = isl_pw_multi_aff_from_multi_aff(ma);
5253 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5256 /* Try and create an isl_union_pw_multi_aff that is equivalent
5257 * to the given isl_union_map.
5258 * The isl_union_map is required to be single-valued in each space.
5259 * Otherwise, an error is produced.
5261 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5262 __isl_take isl_union_map *umap)
5264 isl_space *space;
5265 isl_union_pw_multi_aff *upma;
5267 space = isl_union_map_get_space(umap);
5268 upma = isl_union_pw_multi_aff_empty(space);
5269 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5270 upma = isl_union_pw_multi_aff_free(upma);
5271 isl_union_map_free(umap);
5273 return upma;
5276 /* Try and create an isl_union_pw_multi_aff that is equivalent
5277 * to the given isl_union_set.
5278 * The isl_union_set is required to be a singleton in each space.
5279 * Otherwise, an error is produced.
5281 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5282 __isl_take isl_union_set *uset)
5284 return isl_union_pw_multi_aff_from_union_map(uset);
5287 /* Return the piecewise affine expression "set ? 1 : 0".
5289 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5291 isl_pw_aff *pa;
5292 isl_space *space = isl_set_get_space(set);
5293 isl_local_space *ls = isl_local_space_from_space(space);
5294 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5295 isl_aff *one = isl_aff_zero_on_domain(ls);
5297 one = isl_aff_add_constant_si(one, 1);
5298 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5299 set = isl_set_complement(set);
5300 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5302 return pa;
5305 /* Plug in "subs" for dimension "type", "pos" of "aff".
5307 * Let i be the dimension to replace and let "subs" be of the form
5309 * f/d
5311 * and "aff" of the form
5313 * (a i + g)/m
5315 * The result is
5317 * (a f + d g')/(m d)
5319 * where g' is the result of plugging in "subs" in each of the integer
5320 * divisions in g.
5322 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5323 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5325 isl_ctx *ctx;
5326 isl_int v;
5328 aff = isl_aff_cow(aff);
5329 if (!aff || !subs)
5330 return isl_aff_free(aff);
5332 ctx = isl_aff_get_ctx(aff);
5333 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5334 isl_die(ctx, isl_error_invalid,
5335 "spaces don't match", return isl_aff_free(aff));
5336 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5337 isl_die(ctx, isl_error_unsupported,
5338 "cannot handle divs yet", return isl_aff_free(aff));
5340 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5341 if (!aff->ls)
5342 return isl_aff_free(aff);
5344 aff->v = isl_vec_cow(aff->v);
5345 if (!aff->v)
5346 return isl_aff_free(aff);
5348 pos += isl_local_space_offset(aff->ls, type);
5350 isl_int_init(v);
5351 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5352 aff->v->size, subs->v->size, v);
5353 isl_int_clear(v);
5355 return aff;
5358 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5359 * expressions in "maff".
5361 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5362 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5363 __isl_keep isl_aff *subs)
5365 int i;
5367 maff = isl_multi_aff_cow(maff);
5368 if (!maff || !subs)
5369 return isl_multi_aff_free(maff);
5371 if (type == isl_dim_in)
5372 type = isl_dim_set;
5374 for (i = 0; i < maff->n; ++i) {
5375 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5376 if (!maff->p[i])
5377 return isl_multi_aff_free(maff);
5380 return maff;
5383 /* Plug in "subs" for dimension "type", "pos" of "pma".
5385 * pma is of the form
5387 * A_i(v) -> M_i(v)
5389 * while subs is of the form
5391 * v' = B_j(v) -> S_j
5393 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5394 * has a contribution in the result, in particular
5396 * C_ij(S_j) -> M_i(S_j)
5398 * Note that plugging in S_j in C_ij may also result in an empty set
5399 * and this contribution should simply be discarded.
5401 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5402 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5403 __isl_keep isl_pw_aff *subs)
5405 int i, j, n;
5406 isl_pw_multi_aff *res;
5408 if (!pma || !subs)
5409 return isl_pw_multi_aff_free(pma);
5411 n = pma->n * subs->n;
5412 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5414 for (i = 0; i < pma->n; ++i) {
5415 for (j = 0; j < subs->n; ++j) {
5416 isl_set *common;
5417 isl_multi_aff *res_ij;
5418 int empty;
5420 common = isl_set_intersect(
5421 isl_set_copy(pma->p[i].set),
5422 isl_set_copy(subs->p[j].set));
5423 common = isl_set_substitute(common,
5424 type, pos, subs->p[j].aff);
5425 empty = isl_set_plain_is_empty(common);
5426 if (empty < 0 || empty) {
5427 isl_set_free(common);
5428 if (empty < 0)
5429 goto error;
5430 continue;
5433 res_ij = isl_multi_aff_substitute(
5434 isl_multi_aff_copy(pma->p[i].maff),
5435 type, pos, subs->p[j].aff);
5437 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5441 isl_pw_multi_aff_free(pma);
5442 return res;
5443 error:
5444 isl_pw_multi_aff_free(pma);
5445 isl_pw_multi_aff_free(res);
5446 return NULL;
5449 /* Compute the preimage of a range of dimensions in the affine expression "src"
5450 * under "ma" and put the result in "dst". The number of dimensions in "src"
5451 * that precede the range is given by "n_before". The number of dimensions
5452 * in the range is given by the number of output dimensions of "ma".
5453 * The number of dimensions that follow the range is given by "n_after".
5454 * If "has_denom" is set (to one),
5455 * then "src" and "dst" have an extra initial denominator.
5456 * "n_div_ma" is the number of existentials in "ma"
5457 * "n_div_bset" is the number of existentials in "src"
5458 * The resulting "dst" (which is assumed to have been allocated by
5459 * the caller) contains coefficients for both sets of existentials,
5460 * first those in "ma" and then those in "src".
5461 * f, c1, c2 and g are temporary objects that have been initialized
5462 * by the caller.
5464 * Let src represent the expression
5466 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5468 * and let ma represent the expressions
5470 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5472 * We start out with the following expression for dst:
5474 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5476 * with the multiplication factor f initially equal to 1
5477 * and f \sum_i b_i v_i kept separately.
5478 * For each x_i that we substitute, we multiply the numerator
5479 * (and denominator) of dst by c_1 = m_i and add the numerator
5480 * of the x_i expression multiplied by c_2 = f b_i,
5481 * after removing the common factors of c_1 and c_2.
5482 * The multiplication factor f also needs to be multiplied by c_1
5483 * for the next x_j, j > i.
5485 void isl_seq_preimage(isl_int *dst, isl_int *src,
5486 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5487 int n_div_ma, int n_div_bmap,
5488 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5490 int i;
5491 int n_param, n_in, n_out;
5492 int o_dst, o_src;
5494 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5495 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5496 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5498 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5499 o_dst = o_src = has_denom + 1 + n_param + n_before;
5500 isl_seq_clr(dst + o_dst, n_in);
5501 o_dst += n_in;
5502 o_src += n_out;
5503 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5504 o_dst += n_after;
5505 o_src += n_after;
5506 isl_seq_clr(dst + o_dst, n_div_ma);
5507 o_dst += n_div_ma;
5508 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5510 isl_int_set_si(f, 1);
5512 for (i = 0; i < n_out; ++i) {
5513 int offset = has_denom + 1 + n_param + n_before + i;
5515 if (isl_int_is_zero(src[offset]))
5516 continue;
5517 isl_int_set(c1, ma->p[i]->v->el[0]);
5518 isl_int_mul(c2, f, src[offset]);
5519 isl_int_gcd(g, c1, c2);
5520 isl_int_divexact(c1, c1, g);
5521 isl_int_divexact(c2, c2, g);
5523 isl_int_mul(f, f, c1);
5524 o_dst = has_denom;
5525 o_src = 1;
5526 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5527 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5528 o_dst += 1 + n_param;
5529 o_src += 1 + n_param;
5530 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5531 o_dst += n_before;
5532 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5533 c2, ma->p[i]->v->el + o_src, n_in);
5534 o_dst += n_in;
5535 o_src += n_in;
5536 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5537 o_dst += n_after;
5538 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5539 c2, ma->p[i]->v->el + o_src, n_div_ma);
5540 o_dst += n_div_ma;
5541 o_src += n_div_ma;
5542 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5543 if (has_denom)
5544 isl_int_mul(dst[0], dst[0], c1);
5548 /* Compute the pullback of "aff" by the function represented by "ma".
5549 * In other words, plug in "ma" in "aff". The result is an affine expression
5550 * defined over the domain space of "ma".
5552 * If "aff" is represented by
5554 * (a(p) + b x + c(divs))/d
5556 * and ma is represented by
5558 * x = D(p) + F(y) + G(divs')
5560 * then the result is
5562 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5564 * The divs in the local space of the input are similarly adjusted
5565 * through a call to isl_local_space_preimage_multi_aff.
5567 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5568 __isl_take isl_multi_aff *ma)
5570 isl_aff *res = NULL;
5571 isl_local_space *ls;
5572 int n_div_aff, n_div_ma;
5573 isl_int f, c1, c2, g;
5575 ma = isl_multi_aff_align_divs(ma);
5576 if (!aff || !ma)
5577 goto error;
5579 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5580 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5582 ls = isl_aff_get_domain_local_space(aff);
5583 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5584 res = isl_aff_alloc(ls);
5585 if (!res)
5586 goto error;
5588 isl_int_init(f);
5589 isl_int_init(c1);
5590 isl_int_init(c2);
5591 isl_int_init(g);
5593 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5594 f, c1, c2, g, 1);
5596 isl_int_clear(f);
5597 isl_int_clear(c1);
5598 isl_int_clear(c2);
5599 isl_int_clear(g);
5601 isl_aff_free(aff);
5602 isl_multi_aff_free(ma);
5603 res = isl_aff_normalize(res);
5604 return res;
5605 error:
5606 isl_aff_free(aff);
5607 isl_multi_aff_free(ma);
5608 isl_aff_free(res);
5609 return NULL;
5612 /* Compute the pullback of "aff1" by the function represented by "aff2".
5613 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5614 * defined over the domain space of "aff1".
5616 * The domain of "aff1" should match the range of "aff2", which means
5617 * that it should be single-dimensional.
5619 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5620 __isl_take isl_aff *aff2)
5622 isl_multi_aff *ma;
5624 ma = isl_multi_aff_from_aff(aff2);
5625 return isl_aff_pullback_multi_aff(aff1, ma);
5628 /* Compute the pullback of "ma1" by the function represented by "ma2".
5629 * In other words, plug in "ma2" in "ma1".
5631 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5633 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5634 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5636 int i;
5637 isl_space *space = NULL;
5639 ma2 = isl_multi_aff_align_divs(ma2);
5640 ma1 = isl_multi_aff_cow(ma1);
5641 if (!ma1 || !ma2)
5642 goto error;
5644 space = isl_space_join(isl_multi_aff_get_space(ma2),
5645 isl_multi_aff_get_space(ma1));
5647 for (i = 0; i < ma1->n; ++i) {
5648 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5649 isl_multi_aff_copy(ma2));
5650 if (!ma1->p[i])
5651 goto error;
5654 ma1 = isl_multi_aff_reset_space(ma1, space);
5655 isl_multi_aff_free(ma2);
5656 return ma1;
5657 error:
5658 isl_space_free(space);
5659 isl_multi_aff_free(ma2);
5660 isl_multi_aff_free(ma1);
5661 return NULL;
5664 /* Compute the pullback of "ma1" by the function represented by "ma2".
5665 * In other words, plug in "ma2" in "ma1".
5667 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5668 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5670 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5671 &isl_multi_aff_pullback_multi_aff_aligned);
5674 /* Extend the local space of "dst" to include the divs
5675 * in the local space of "src".
5677 * If "src" does not have any divs or if the local spaces of "dst" and
5678 * "src" are the same, then no extension is required.
5680 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5681 __isl_keep isl_aff *src)
5683 isl_ctx *ctx;
5684 int src_n_div, dst_n_div;
5685 int *exp1 = NULL;
5686 int *exp2 = NULL;
5687 isl_bool equal;
5688 isl_mat *div;
5690 if (!src || !dst)
5691 return isl_aff_free(dst);
5693 ctx = isl_aff_get_ctx(src);
5694 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5695 if (equal < 0)
5696 return isl_aff_free(dst);
5697 if (!equal)
5698 isl_die(ctx, isl_error_invalid,
5699 "spaces don't match", goto error);
5701 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5702 if (src_n_div == 0)
5703 return dst;
5704 equal = isl_local_space_is_equal(src->ls, dst->ls);
5705 if (equal < 0)
5706 return isl_aff_free(dst);
5707 if (equal)
5708 return dst;
5710 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5711 exp1 = isl_alloc_array(ctx, int, src_n_div);
5712 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5713 if (!exp1 || (dst_n_div && !exp2))
5714 goto error;
5716 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5717 dst = isl_aff_expand_divs(dst, div, exp2);
5718 free(exp1);
5719 free(exp2);
5721 return dst;
5722 error:
5723 free(exp1);
5724 free(exp2);
5725 return isl_aff_free(dst);
5728 /* Adjust the local spaces of the affine expressions in "maff"
5729 * such that they all have the save divs.
5731 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5732 __isl_take isl_multi_aff *maff)
5734 int i;
5736 if (!maff)
5737 return NULL;
5738 if (maff->n == 0)
5739 return maff;
5740 maff = isl_multi_aff_cow(maff);
5741 if (!maff)
5742 return NULL;
5744 for (i = 1; i < maff->n; ++i)
5745 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5746 for (i = 1; i < maff->n; ++i) {
5747 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5748 if (!maff->p[i])
5749 return isl_multi_aff_free(maff);
5752 return maff;
5755 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5757 aff = isl_aff_cow(aff);
5758 if (!aff)
5759 return NULL;
5761 aff->ls = isl_local_space_lift(aff->ls);
5762 if (!aff->ls)
5763 return isl_aff_free(aff);
5765 return aff;
5768 /* Lift "maff" to a space with extra dimensions such that the result
5769 * has no more existentially quantified variables.
5770 * If "ls" is not NULL, then *ls is assigned the local space that lies
5771 * at the basis of the lifting applied to "maff".
5773 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5774 __isl_give isl_local_space **ls)
5776 int i;
5777 isl_space *space;
5778 unsigned n_div;
5780 if (ls)
5781 *ls = NULL;
5783 if (!maff)
5784 return NULL;
5786 if (maff->n == 0) {
5787 if (ls) {
5788 isl_space *space = isl_multi_aff_get_domain_space(maff);
5789 *ls = isl_local_space_from_space(space);
5790 if (!*ls)
5791 return isl_multi_aff_free(maff);
5793 return maff;
5796 maff = isl_multi_aff_cow(maff);
5797 maff = isl_multi_aff_align_divs(maff);
5798 if (!maff)
5799 return NULL;
5801 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5802 space = isl_multi_aff_get_space(maff);
5803 space = isl_space_lift(isl_space_domain(space), n_div);
5804 space = isl_space_extend_domain_with_range(space,
5805 isl_multi_aff_get_space(maff));
5806 if (!space)
5807 return isl_multi_aff_free(maff);
5808 isl_space_free(maff->space);
5809 maff->space = space;
5811 if (ls) {
5812 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5813 if (!*ls)
5814 return isl_multi_aff_free(maff);
5817 for (i = 0; i < maff->n; ++i) {
5818 maff->p[i] = isl_aff_lift(maff->p[i]);
5819 if (!maff->p[i])
5820 goto error;
5823 return maff;
5824 error:
5825 if (ls)
5826 isl_local_space_free(*ls);
5827 return isl_multi_aff_free(maff);
5831 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5833 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5834 __isl_keep isl_pw_multi_aff *pma, int pos)
5836 int i;
5837 int n_out;
5838 isl_space *space;
5839 isl_pw_aff *pa;
5841 if (!pma)
5842 return NULL;
5844 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5845 if (pos < 0 || pos >= n_out)
5846 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5847 "index out of bounds", return NULL);
5849 space = isl_pw_multi_aff_get_space(pma);
5850 space = isl_space_drop_dims(space, isl_dim_out,
5851 pos + 1, n_out - pos - 1);
5852 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5854 pa = isl_pw_aff_alloc_size(space, pma->n);
5855 for (i = 0; i < pma->n; ++i) {
5856 isl_aff *aff;
5857 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5858 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5861 return pa;
5864 /* Return an isl_pw_multi_aff with the given "set" as domain and
5865 * an unnamed zero-dimensional range.
5867 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5868 __isl_take isl_set *set)
5870 isl_multi_aff *ma;
5871 isl_space *space;
5873 space = isl_set_get_space(set);
5874 space = isl_space_from_domain(space);
5875 ma = isl_multi_aff_zero(space);
5876 return isl_pw_multi_aff_alloc(set, ma);
5879 /* Add an isl_pw_multi_aff with the given "set" as domain and
5880 * an unnamed zero-dimensional range to *user.
5882 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5883 void *user)
5885 isl_union_pw_multi_aff **upma = user;
5886 isl_pw_multi_aff *pma;
5888 pma = isl_pw_multi_aff_from_domain(set);
5889 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5891 return isl_stat_ok;
5894 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5895 * an unnamed zero-dimensional range.
5897 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5898 __isl_take isl_union_set *uset)
5900 isl_space *space;
5901 isl_union_pw_multi_aff *upma;
5903 if (!uset)
5904 return NULL;
5906 space = isl_union_set_get_space(uset);
5907 upma = isl_union_pw_multi_aff_empty(space);
5909 if (isl_union_set_foreach_set(uset,
5910 &add_pw_multi_aff_from_domain, &upma) < 0)
5911 goto error;
5913 isl_union_set_free(uset);
5914 return upma;
5915 error:
5916 isl_union_set_free(uset);
5917 isl_union_pw_multi_aff_free(upma);
5918 return NULL;
5921 /* Convert "pma" to an isl_map and add it to *umap.
5923 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5924 void *user)
5926 isl_union_map **umap = user;
5927 isl_map *map;
5929 map = isl_map_from_pw_multi_aff(pma);
5930 *umap = isl_union_map_add_map(*umap, map);
5932 return isl_stat_ok;
5935 /* Construct a union map mapping the domain of the union
5936 * piecewise multi-affine expression to its range, with each dimension
5937 * in the range equated to the corresponding affine expression on its cell.
5939 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5940 __isl_take isl_union_pw_multi_aff *upma)
5942 isl_space *space;
5943 isl_union_map *umap;
5945 if (!upma)
5946 return NULL;
5948 space = isl_union_pw_multi_aff_get_space(upma);
5949 umap = isl_union_map_empty(space);
5951 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5952 &map_from_pw_multi_aff, &umap) < 0)
5953 goto error;
5955 isl_union_pw_multi_aff_free(upma);
5956 return umap;
5957 error:
5958 isl_union_pw_multi_aff_free(upma);
5959 isl_union_map_free(umap);
5960 return NULL;
5963 /* Local data for bin_entry and the callback "fn".
5965 struct isl_union_pw_multi_aff_bin_data {
5966 isl_union_pw_multi_aff *upma2;
5967 isl_union_pw_multi_aff *res;
5968 isl_pw_multi_aff *pma;
5969 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5972 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5973 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5975 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5977 struct isl_union_pw_multi_aff_bin_data *data = user;
5978 isl_stat r;
5980 data->pma = pma;
5981 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5982 data->fn, data);
5983 isl_pw_multi_aff_free(pma);
5985 return r;
5988 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5989 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5990 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5991 * as *entry. The callback should adjust data->res if desired.
5993 static __isl_give isl_union_pw_multi_aff *bin_op(
5994 __isl_take isl_union_pw_multi_aff *upma1,
5995 __isl_take isl_union_pw_multi_aff *upma2,
5996 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5998 isl_space *space;
5999 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6001 space = isl_union_pw_multi_aff_get_space(upma2);
6002 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6003 space = isl_union_pw_multi_aff_get_space(upma1);
6004 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6006 if (!upma1 || !upma2)
6007 goto error;
6009 data.upma2 = upma2;
6010 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6011 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6012 &bin_entry, &data) < 0)
6013 goto error;
6015 isl_union_pw_multi_aff_free(upma1);
6016 isl_union_pw_multi_aff_free(upma2);
6017 return data.res;
6018 error:
6019 isl_union_pw_multi_aff_free(upma1);
6020 isl_union_pw_multi_aff_free(upma2);
6021 isl_union_pw_multi_aff_free(data.res);
6022 return NULL;
6025 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6026 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6028 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6029 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6031 isl_space *space;
6033 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6034 isl_pw_multi_aff_get_space(pma2));
6035 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6036 &isl_multi_aff_range_product);
6039 /* Given two isl_pw_multi_affs A -> B and C -> D,
6040 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6042 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6043 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6045 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6046 &pw_multi_aff_range_product);
6049 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6050 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6052 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6053 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6055 isl_space *space;
6057 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6058 isl_pw_multi_aff_get_space(pma2));
6059 space = isl_space_flatten_range(space);
6060 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6061 &isl_multi_aff_flat_range_product);
6064 /* Given two isl_pw_multi_affs A -> B and C -> D,
6065 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6067 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6068 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6070 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6071 &pw_multi_aff_flat_range_product);
6074 /* If data->pma and "pma2" have the same domain space, then compute
6075 * their flat range product and the result to data->res.
6077 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6078 void *user)
6080 struct isl_union_pw_multi_aff_bin_data *data = user;
6082 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6083 pma2->dim, isl_dim_in)) {
6084 isl_pw_multi_aff_free(pma2);
6085 return isl_stat_ok;
6088 pma2 = isl_pw_multi_aff_flat_range_product(
6089 isl_pw_multi_aff_copy(data->pma), pma2);
6091 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6093 return isl_stat_ok;
6096 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6097 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6099 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6100 __isl_take isl_union_pw_multi_aff *upma1,
6101 __isl_take isl_union_pw_multi_aff *upma2)
6103 return bin_op(upma1, upma2, &flat_range_product_entry);
6106 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6107 * The parameters are assumed to have been aligned.
6109 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6110 * except that it works on two different isl_pw_* types.
6112 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6113 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6114 __isl_take isl_pw_aff *pa)
6116 int i, j, n;
6117 isl_pw_multi_aff *res = NULL;
6119 if (!pma || !pa)
6120 goto error;
6122 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6123 pa->dim, isl_dim_in))
6124 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6125 "domains don't match", goto error);
6126 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6127 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6128 "index out of bounds", goto error);
6130 n = pma->n * pa->n;
6131 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6133 for (i = 0; i < pma->n; ++i) {
6134 for (j = 0; j < pa->n; ++j) {
6135 isl_set *common;
6136 isl_multi_aff *res_ij;
6137 int empty;
6139 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6140 isl_set_copy(pa->p[j].set));
6141 empty = isl_set_plain_is_empty(common);
6142 if (empty < 0 || empty) {
6143 isl_set_free(common);
6144 if (empty < 0)
6145 goto error;
6146 continue;
6149 res_ij = isl_multi_aff_set_aff(
6150 isl_multi_aff_copy(pma->p[i].maff), pos,
6151 isl_aff_copy(pa->p[j].aff));
6152 res_ij = isl_multi_aff_gist(res_ij,
6153 isl_set_copy(common));
6155 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6159 isl_pw_multi_aff_free(pma);
6160 isl_pw_aff_free(pa);
6161 return res;
6162 error:
6163 isl_pw_multi_aff_free(pma);
6164 isl_pw_aff_free(pa);
6165 return isl_pw_multi_aff_free(res);
6168 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6170 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6171 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6172 __isl_take isl_pw_aff *pa)
6174 isl_bool equal_params;
6176 if (!pma || !pa)
6177 goto error;
6178 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6179 if (equal_params < 0)
6180 goto error;
6181 if (equal_params)
6182 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6183 if (!isl_space_has_named_params(pma->dim) ||
6184 !isl_space_has_named_params(pa->dim))
6185 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6186 "unaligned unnamed parameters", goto error);
6187 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6188 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6189 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6190 error:
6191 isl_pw_multi_aff_free(pma);
6192 isl_pw_aff_free(pa);
6193 return NULL;
6196 /* Do the parameters of "pa" match those of "space"?
6198 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6199 __isl_keep isl_space *space)
6201 isl_space *pa_space;
6202 isl_bool match;
6204 if (!pa || !space)
6205 return isl_bool_error;
6207 pa_space = isl_pw_aff_get_space(pa);
6209 match = isl_space_has_equal_params(space, pa_space);
6211 isl_space_free(pa_space);
6212 return match;
6215 /* Check that the domain space of "pa" matches "space".
6217 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6218 __isl_keep isl_space *space)
6220 isl_space *pa_space;
6221 isl_bool match;
6223 if (!pa || !space)
6224 return isl_stat_error;
6226 pa_space = isl_pw_aff_get_space(pa);
6228 match = isl_space_has_equal_params(space, pa_space);
6229 if (match < 0)
6230 goto error;
6231 if (!match)
6232 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6233 "parameters don't match", goto error);
6234 match = isl_space_tuple_is_equal(space, isl_dim_in,
6235 pa_space, isl_dim_in);
6236 if (match < 0)
6237 goto error;
6238 if (!match)
6239 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6240 "domains don't match", goto error);
6241 isl_space_free(pa_space);
6242 return isl_stat_ok;
6243 error:
6244 isl_space_free(pa_space);
6245 return isl_stat_error;
6248 #undef BASE
6249 #define BASE pw_aff
6250 #undef DOMBASE
6251 #define DOMBASE set
6253 #include <isl_multi_templ.c>
6254 #include <isl_multi_apply_set.c>
6255 #include <isl_multi_coalesce.c>
6256 #include <isl_multi_dims.c>
6257 #include <isl_multi_gist.c>
6258 #include <isl_multi_hash.c>
6259 #include <isl_multi_intersect.c>
6261 /* Scale the elements of "pma" by the corresponding elements of "mv".
6263 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6264 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6266 int i;
6267 isl_bool equal_params;
6269 pma = isl_pw_multi_aff_cow(pma);
6270 if (!pma || !mv)
6271 goto error;
6272 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6273 mv->space, isl_dim_set))
6274 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6275 "spaces don't match", goto error);
6276 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6277 if (equal_params < 0)
6278 goto error;
6279 if (!equal_params) {
6280 pma = isl_pw_multi_aff_align_params(pma,
6281 isl_multi_val_get_space(mv));
6282 mv = isl_multi_val_align_params(mv,
6283 isl_pw_multi_aff_get_space(pma));
6284 if (!pma || !mv)
6285 goto error;
6288 for (i = 0; i < pma->n; ++i) {
6289 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6290 isl_multi_val_copy(mv));
6291 if (!pma->p[i].maff)
6292 goto error;
6295 isl_multi_val_free(mv);
6296 return pma;
6297 error:
6298 isl_multi_val_free(mv);
6299 isl_pw_multi_aff_free(pma);
6300 return NULL;
6303 /* This function is called for each entry of an isl_union_pw_multi_aff.
6304 * If the space of the entry matches that of data->mv,
6305 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6306 * Otherwise, return an empty isl_pw_multi_aff.
6308 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6309 __isl_take isl_pw_multi_aff *pma, void *user)
6311 isl_multi_val *mv = user;
6313 if (!pma)
6314 return NULL;
6315 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6316 mv->space, isl_dim_set)) {
6317 isl_space *space = isl_pw_multi_aff_get_space(pma);
6318 isl_pw_multi_aff_free(pma);
6319 return isl_pw_multi_aff_empty(space);
6322 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6325 /* Scale the elements of "upma" by the corresponding elements of "mv",
6326 * for those entries that match the space of "mv".
6328 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6329 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6331 upma = isl_union_pw_multi_aff_align_params(upma,
6332 isl_multi_val_get_space(mv));
6333 mv = isl_multi_val_align_params(mv,
6334 isl_union_pw_multi_aff_get_space(upma));
6335 if (!upma || !mv)
6336 goto error;
6338 return isl_union_pw_multi_aff_transform(upma,
6339 &union_pw_multi_aff_scale_multi_val_entry, mv);
6341 isl_multi_val_free(mv);
6342 return upma;
6343 error:
6344 isl_multi_val_free(mv);
6345 isl_union_pw_multi_aff_free(upma);
6346 return NULL;
6349 /* Construct and return a piecewise multi affine expression
6350 * in the given space with value zero in each of the output dimensions and
6351 * a universe domain.
6353 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6355 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6358 /* Construct and return a piecewise multi affine expression
6359 * that is equal to the given piecewise affine expression.
6361 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6362 __isl_take isl_pw_aff *pa)
6364 int i;
6365 isl_space *space;
6366 isl_pw_multi_aff *pma;
6368 if (!pa)
6369 return NULL;
6371 space = isl_pw_aff_get_space(pa);
6372 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6374 for (i = 0; i < pa->n; ++i) {
6375 isl_set *set;
6376 isl_multi_aff *ma;
6378 set = isl_set_copy(pa->p[i].set);
6379 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6380 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6383 isl_pw_aff_free(pa);
6384 return pma;
6387 /* Construct a set or map mapping the shared (parameter) domain
6388 * of the piecewise affine expressions to the range of "mpa"
6389 * with each dimension in the range equated to the
6390 * corresponding piecewise affine expression.
6392 static __isl_give isl_map *map_from_multi_pw_aff(
6393 __isl_take isl_multi_pw_aff *mpa)
6395 int i;
6396 isl_space *space;
6397 isl_map *map;
6399 if (!mpa)
6400 return NULL;
6402 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6403 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6404 "invalid space", goto error);
6406 space = isl_multi_pw_aff_get_domain_space(mpa);
6407 map = isl_map_universe(isl_space_from_domain(space));
6409 for (i = 0; i < mpa->n; ++i) {
6410 isl_pw_aff *pa;
6411 isl_map *map_i;
6413 pa = isl_pw_aff_copy(mpa->p[i]);
6414 map_i = map_from_pw_aff(pa);
6416 map = isl_map_flat_range_product(map, map_i);
6419 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6421 isl_multi_pw_aff_free(mpa);
6422 return map;
6423 error:
6424 isl_multi_pw_aff_free(mpa);
6425 return NULL;
6428 /* Construct a map mapping the shared domain
6429 * of the piecewise affine expressions to the range of "mpa"
6430 * with each dimension in the range equated to the
6431 * corresponding piecewise affine expression.
6433 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6435 if (!mpa)
6436 return NULL;
6437 if (isl_space_is_set(mpa->space))
6438 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6439 "space of input is not a map", goto error);
6441 return map_from_multi_pw_aff(mpa);
6442 error:
6443 isl_multi_pw_aff_free(mpa);
6444 return NULL;
6447 /* Construct a set mapping the shared parameter domain
6448 * of the piecewise affine expressions to the space of "mpa"
6449 * with each dimension in the range equated to the
6450 * corresponding piecewise affine expression.
6452 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6454 if (!mpa)
6455 return NULL;
6456 if (!isl_space_is_set(mpa->space))
6457 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6458 "space of input is not a set", goto error);
6460 return map_from_multi_pw_aff(mpa);
6461 error:
6462 isl_multi_pw_aff_free(mpa);
6463 return NULL;
6466 /* Construct and return a piecewise multi affine expression
6467 * that is equal to the given multi piecewise affine expression
6468 * on the shared domain of the piecewise affine expressions.
6470 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6471 __isl_take isl_multi_pw_aff *mpa)
6473 int i;
6474 isl_space *space;
6475 isl_pw_aff *pa;
6476 isl_pw_multi_aff *pma;
6478 if (!mpa)
6479 return NULL;
6481 space = isl_multi_pw_aff_get_space(mpa);
6483 if (mpa->n == 0) {
6484 isl_multi_pw_aff_free(mpa);
6485 return isl_pw_multi_aff_zero(space);
6488 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6489 pma = isl_pw_multi_aff_from_pw_aff(pa);
6491 for (i = 1; i < mpa->n; ++i) {
6492 isl_pw_multi_aff *pma_i;
6494 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6495 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6496 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6499 pma = isl_pw_multi_aff_reset_space(pma, space);
6501 isl_multi_pw_aff_free(mpa);
6502 return pma;
6505 /* Construct and return a multi piecewise affine expression
6506 * that is equal to the given multi affine expression.
6508 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6509 __isl_take isl_multi_aff *ma)
6511 int i, n;
6512 isl_multi_pw_aff *mpa;
6514 if (!ma)
6515 return NULL;
6517 n = isl_multi_aff_dim(ma, isl_dim_out);
6518 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6520 for (i = 0; i < n; ++i) {
6521 isl_pw_aff *pa;
6523 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6524 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6527 isl_multi_aff_free(ma);
6528 return mpa;
6531 /* Construct and return a multi piecewise affine expression
6532 * that is equal to the given piecewise multi affine expression.
6534 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6535 __isl_take isl_pw_multi_aff *pma)
6537 int i, n;
6538 isl_space *space;
6539 isl_multi_pw_aff *mpa;
6541 if (!pma)
6542 return NULL;
6544 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6545 space = isl_pw_multi_aff_get_space(pma);
6546 mpa = isl_multi_pw_aff_alloc(space);
6548 for (i = 0; i < n; ++i) {
6549 isl_pw_aff *pa;
6551 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6552 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6555 isl_pw_multi_aff_free(pma);
6556 return mpa;
6559 /* Do "pa1" and "pa2" represent the same function?
6561 * We first check if they are obviously equal.
6562 * If not, we convert them to maps and check if those are equal.
6564 * If "pa1" or "pa2" contain any NaNs, then they are considered
6565 * not to be the same. A NaN is not equal to anything, not even
6566 * to another NaN.
6568 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6569 __isl_keep isl_pw_aff *pa2)
6571 isl_bool equal;
6572 isl_bool has_nan;
6573 isl_map *map1, *map2;
6575 if (!pa1 || !pa2)
6576 return isl_bool_error;
6578 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6579 if (equal < 0 || equal)
6580 return equal;
6581 has_nan = either_involves_nan(pa1, pa2);
6582 if (has_nan < 0)
6583 return isl_bool_error;
6584 if (has_nan)
6585 return isl_bool_false;
6587 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6588 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6589 equal = isl_map_is_equal(map1, map2);
6590 isl_map_free(map1);
6591 isl_map_free(map2);
6593 return equal;
6596 /* Do "mpa1" and "mpa2" represent the same function?
6598 * Note that we cannot convert the entire isl_multi_pw_aff
6599 * to a map because the domains of the piecewise affine expressions
6600 * may not be the same.
6602 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6603 __isl_keep isl_multi_pw_aff *mpa2)
6605 int i;
6606 isl_bool equal, equal_params;
6608 if (!mpa1 || !mpa2)
6609 return isl_bool_error;
6611 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6612 if (equal_params < 0)
6613 return isl_bool_error;
6614 if (!equal_params) {
6615 if (!isl_space_has_named_params(mpa1->space))
6616 return isl_bool_false;
6617 if (!isl_space_has_named_params(mpa2->space))
6618 return isl_bool_false;
6619 mpa1 = isl_multi_pw_aff_copy(mpa1);
6620 mpa2 = isl_multi_pw_aff_copy(mpa2);
6621 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6622 isl_multi_pw_aff_get_space(mpa2));
6623 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6624 isl_multi_pw_aff_get_space(mpa1));
6625 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6626 isl_multi_pw_aff_free(mpa1);
6627 isl_multi_pw_aff_free(mpa2);
6628 return equal;
6631 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6632 if (equal < 0 || !equal)
6633 return equal;
6635 for (i = 0; i < mpa1->n; ++i) {
6636 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6637 if (equal < 0 || !equal)
6638 return equal;
6641 return isl_bool_true;
6644 /* Do "pma1" and "pma2" represent the same function?
6646 * First check if they are obviously equal.
6647 * If not, then convert them to maps and check if those are equal.
6649 * If "pa1" or "pa2" contain any NaNs, then they are considered
6650 * not to be the same. A NaN is not equal to anything, not even
6651 * to another NaN.
6653 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6654 __isl_keep isl_pw_multi_aff *pma2)
6656 isl_bool equal;
6657 isl_bool has_nan;
6658 isl_map *map1, *map2;
6660 if (!pma1 || !pma2)
6661 return isl_bool_error;
6663 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6664 if (equal < 0 || equal)
6665 return equal;
6666 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6667 if (has_nan >= 0 && !has_nan)
6668 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6669 if (has_nan < 0 || has_nan)
6670 return isl_bool_not(has_nan);
6672 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6673 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6674 equal = isl_map_is_equal(map1, map2);
6675 isl_map_free(map1);
6676 isl_map_free(map2);
6678 return equal;
6681 /* Compute the pullback of "mpa" by the function represented by "ma".
6682 * In other words, plug in "ma" in "mpa".
6684 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6686 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6687 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6689 int i;
6690 isl_space *space = NULL;
6692 mpa = isl_multi_pw_aff_cow(mpa);
6693 if (!mpa || !ma)
6694 goto error;
6696 space = isl_space_join(isl_multi_aff_get_space(ma),
6697 isl_multi_pw_aff_get_space(mpa));
6698 if (!space)
6699 goto error;
6701 for (i = 0; i < mpa->n; ++i) {
6702 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6703 isl_multi_aff_copy(ma));
6704 if (!mpa->p[i])
6705 goto error;
6708 isl_multi_aff_free(ma);
6709 isl_space_free(mpa->space);
6710 mpa->space = space;
6711 return mpa;
6712 error:
6713 isl_space_free(space);
6714 isl_multi_pw_aff_free(mpa);
6715 isl_multi_aff_free(ma);
6716 return NULL;
6719 /* Compute the pullback of "mpa" by the function represented by "ma".
6720 * In other words, plug in "ma" in "mpa".
6722 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6723 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6725 isl_bool equal_params;
6727 if (!mpa || !ma)
6728 goto error;
6729 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6730 if (equal_params < 0)
6731 goto error;
6732 if (equal_params)
6733 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6734 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6735 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6736 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6737 error:
6738 isl_multi_pw_aff_free(mpa);
6739 isl_multi_aff_free(ma);
6740 return NULL;
6743 /* Compute the pullback of "mpa" by the function represented by "pma".
6744 * In other words, plug in "pma" in "mpa".
6746 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6748 static __isl_give isl_multi_pw_aff *
6749 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6750 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6752 int i;
6753 isl_space *space = NULL;
6755 mpa = isl_multi_pw_aff_cow(mpa);
6756 if (!mpa || !pma)
6757 goto error;
6759 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6760 isl_multi_pw_aff_get_space(mpa));
6762 for (i = 0; i < mpa->n; ++i) {
6763 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6764 isl_pw_multi_aff_copy(pma));
6765 if (!mpa->p[i])
6766 goto error;
6769 isl_pw_multi_aff_free(pma);
6770 isl_space_free(mpa->space);
6771 mpa->space = space;
6772 return mpa;
6773 error:
6774 isl_space_free(space);
6775 isl_multi_pw_aff_free(mpa);
6776 isl_pw_multi_aff_free(pma);
6777 return NULL;
6780 /* Compute the pullback of "mpa" by the function represented by "pma".
6781 * In other words, plug in "pma" in "mpa".
6783 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6784 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6786 isl_bool equal_params;
6788 if (!mpa || !pma)
6789 goto error;
6790 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6791 if (equal_params < 0)
6792 goto error;
6793 if (equal_params)
6794 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6795 mpa = isl_multi_pw_aff_align_params(mpa,
6796 isl_pw_multi_aff_get_space(pma));
6797 pma = isl_pw_multi_aff_align_params(pma,
6798 isl_multi_pw_aff_get_space(mpa));
6799 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6800 error:
6801 isl_multi_pw_aff_free(mpa);
6802 isl_pw_multi_aff_free(pma);
6803 return NULL;
6806 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6807 * with the domain of "aff". The domain of the result is the same
6808 * as that of "mpa".
6809 * "mpa" and "aff" are assumed to have been aligned.
6811 * We first extract the parametric constant from "aff", defined
6812 * over the correct domain.
6813 * Then we add the appropriate combinations of the members of "mpa".
6814 * Finally, we add the integer divisions through recursive calls.
6816 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6817 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6819 int i, n_in, n_div;
6820 isl_space *space;
6821 isl_val *v;
6822 isl_pw_aff *pa;
6823 isl_aff *tmp;
6825 n_in = isl_aff_dim(aff, isl_dim_in);
6826 n_div = isl_aff_dim(aff, isl_dim_div);
6828 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6829 tmp = isl_aff_copy(aff);
6830 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6831 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6832 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6833 isl_space_dim(space, isl_dim_set));
6834 tmp = isl_aff_reset_domain_space(tmp, space);
6835 pa = isl_pw_aff_from_aff(tmp);
6837 for (i = 0; i < n_in; ++i) {
6838 isl_pw_aff *pa_i;
6840 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6841 continue;
6842 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6843 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6844 pa_i = isl_pw_aff_scale_val(pa_i, v);
6845 pa = isl_pw_aff_add(pa, pa_i);
6848 for (i = 0; i < n_div; ++i) {
6849 isl_aff *div;
6850 isl_pw_aff *pa_i;
6852 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6853 continue;
6854 div = isl_aff_get_div(aff, i);
6855 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6856 isl_multi_pw_aff_copy(mpa), div);
6857 pa_i = isl_pw_aff_floor(pa_i);
6858 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6859 pa_i = isl_pw_aff_scale_val(pa_i, v);
6860 pa = isl_pw_aff_add(pa, pa_i);
6863 isl_multi_pw_aff_free(mpa);
6864 isl_aff_free(aff);
6866 return pa;
6869 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6870 * with the domain of "aff". The domain of the result is the same
6871 * as that of "mpa".
6873 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6874 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6876 isl_bool equal_params;
6878 if (!aff || !mpa)
6879 goto error;
6880 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6881 if (equal_params < 0)
6882 goto error;
6883 if (equal_params)
6884 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6886 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6887 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6889 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6890 error:
6891 isl_aff_free(aff);
6892 isl_multi_pw_aff_free(mpa);
6893 return NULL;
6896 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6897 * with the domain of "pa". The domain of the result is the same
6898 * as that of "mpa".
6899 * "mpa" and "pa" are assumed to have been aligned.
6901 * We consider each piece in turn. Note that the domains of the
6902 * pieces are assumed to be disjoint and they remain disjoint
6903 * after taking the preimage (over the same function).
6905 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6906 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6908 isl_space *space;
6909 isl_pw_aff *res;
6910 int i;
6912 if (!mpa || !pa)
6913 goto error;
6915 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6916 isl_pw_aff_get_space(pa));
6917 res = isl_pw_aff_empty(space);
6919 for (i = 0; i < pa->n; ++i) {
6920 isl_pw_aff *pa_i;
6921 isl_set *domain;
6923 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6924 isl_multi_pw_aff_copy(mpa),
6925 isl_aff_copy(pa->p[i].aff));
6926 domain = isl_set_copy(pa->p[i].set);
6927 domain = isl_set_preimage_multi_pw_aff(domain,
6928 isl_multi_pw_aff_copy(mpa));
6929 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6930 res = isl_pw_aff_add_disjoint(res, pa_i);
6933 isl_pw_aff_free(pa);
6934 isl_multi_pw_aff_free(mpa);
6935 return res;
6936 error:
6937 isl_pw_aff_free(pa);
6938 isl_multi_pw_aff_free(mpa);
6939 return NULL;
6942 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6943 * with the domain of "pa". The domain of the result is the same
6944 * as that of "mpa".
6946 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6947 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6949 isl_bool equal_params;
6951 if (!pa || !mpa)
6952 goto error;
6953 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6954 if (equal_params < 0)
6955 goto error;
6956 if (equal_params)
6957 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6959 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6960 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6962 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6963 error:
6964 isl_pw_aff_free(pa);
6965 isl_multi_pw_aff_free(mpa);
6966 return NULL;
6969 /* Compute the pullback of "pa" by the function represented by "mpa".
6970 * In other words, plug in "mpa" in "pa".
6971 * "pa" and "mpa" are assumed to have been aligned.
6973 * The pullback is computed by applying "pa" to "mpa".
6975 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6976 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6978 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6981 /* Compute the pullback of "pa" by the function represented by "mpa".
6982 * In other words, plug in "mpa" in "pa".
6984 * The pullback is computed by applying "pa" to "mpa".
6986 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6987 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6989 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6992 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6993 * In other words, plug in "mpa2" in "mpa1".
6995 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6997 * We pullback each member of "mpa1" in turn.
6999 static __isl_give isl_multi_pw_aff *
7000 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7001 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7003 int i;
7004 isl_space *space = NULL;
7006 mpa1 = isl_multi_pw_aff_cow(mpa1);
7007 if (!mpa1 || !mpa2)
7008 goto error;
7010 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7011 isl_multi_pw_aff_get_space(mpa1));
7013 for (i = 0; i < mpa1->n; ++i) {
7014 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7015 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
7016 if (!mpa1->p[i])
7017 goto error;
7020 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7022 isl_multi_pw_aff_free(mpa2);
7023 return mpa1;
7024 error:
7025 isl_space_free(space);
7026 isl_multi_pw_aff_free(mpa1);
7027 isl_multi_pw_aff_free(mpa2);
7028 return NULL;
7031 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7032 * In other words, plug in "mpa2" in "mpa1".
7034 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7035 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7037 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7038 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7041 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7042 * of "mpa1" and "mpa2" live in the same space, construct map space
7043 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7044 * with this map space as extract argument.
7046 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7047 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7048 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7049 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7051 int match;
7052 isl_space *space1, *space2;
7053 isl_map *res;
7055 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7056 isl_multi_pw_aff_get_space(mpa2));
7057 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7058 isl_multi_pw_aff_get_space(mpa1));
7059 if (!mpa1 || !mpa2)
7060 goto error;
7061 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7062 mpa2->space, isl_dim_out);
7063 if (match < 0)
7064 goto error;
7065 if (!match)
7066 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7067 "range spaces don't match", goto error);
7068 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7069 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7070 space1 = isl_space_map_from_domain_and_range(space1, space2);
7072 res = order(mpa1, mpa2, space1);
7073 isl_multi_pw_aff_free(mpa1);
7074 isl_multi_pw_aff_free(mpa2);
7075 return res;
7076 error:
7077 isl_multi_pw_aff_free(mpa1);
7078 isl_multi_pw_aff_free(mpa2);
7079 return NULL;
7082 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7083 * where the function values are equal. "space" is the space of the result.
7084 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7086 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7087 * in the sequences are equal.
7089 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7090 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7091 __isl_take isl_space *space)
7093 int i, n;
7094 isl_map *res;
7096 res = isl_map_universe(space);
7098 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7099 for (i = 0; i < n; ++i) {
7100 isl_pw_aff *pa1, *pa2;
7101 isl_map *map;
7103 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7104 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7105 map = isl_pw_aff_eq_map(pa1, pa2);
7106 res = isl_map_intersect(res, map);
7109 return res;
7112 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7113 * where the function values are equal.
7115 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7116 __isl_take isl_multi_pw_aff *mpa2)
7118 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7119 &isl_multi_pw_aff_eq_map_on_space);
7122 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7123 * where the function values of "mpa1" is lexicographically satisfies "base"
7124 * compared to that of "mpa2". "space" is the space of the result.
7125 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7127 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7128 * if its i-th element satisfies "base" when compared to
7129 * the i-th element of "mpa2" while all previous elements are
7130 * pairwise equal.
7132 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7133 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7134 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7135 __isl_take isl_pw_aff *pa2),
7136 __isl_take isl_space *space)
7138 int i, n;
7139 isl_map *res, *rest;
7141 res = isl_map_empty(isl_space_copy(space));
7142 rest = isl_map_universe(space);
7144 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7145 for (i = 0; i < n; ++i) {
7146 isl_pw_aff *pa1, *pa2;
7147 isl_map *map;
7149 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7150 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7151 map = base(pa1, pa2);
7152 map = isl_map_intersect(map, isl_map_copy(rest));
7153 res = isl_map_union(res, map);
7155 if (i == n - 1)
7156 continue;
7158 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7159 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7160 map = isl_pw_aff_eq_map(pa1, pa2);
7161 rest = isl_map_intersect(rest, map);
7164 isl_map_free(rest);
7165 return res;
7168 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7169 * where the function value of "mpa1" is lexicographically less than that
7170 * of "mpa2". "space" is the space of the result.
7171 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7173 * "mpa1" is less than "mpa2" if its i-th element is smaller
7174 * than the i-th element of "mpa2" while all previous elements are
7175 * pairwise equal.
7177 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7178 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7179 __isl_take isl_space *space)
7181 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7182 &isl_pw_aff_lt_map, space);
7185 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7186 * where the function value of "mpa1" is lexicographically less than that
7187 * of "mpa2".
7189 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7190 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7192 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7193 &isl_multi_pw_aff_lex_lt_map_on_space);
7196 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7197 * where the function value of "mpa1" is lexicographically greater than that
7198 * of "mpa2". "space" is the space of the result.
7199 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7201 * "mpa1" is greater than "mpa2" if its i-th element is greater
7202 * than the i-th element of "mpa2" while all previous elements are
7203 * pairwise equal.
7205 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7206 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7207 __isl_take isl_space *space)
7209 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7210 &isl_pw_aff_gt_map, space);
7213 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7214 * where the function value of "mpa1" is lexicographically greater than that
7215 * of "mpa2".
7217 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7218 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7220 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7221 &isl_multi_pw_aff_lex_gt_map_on_space);
7224 /* Compare two isl_affs.
7226 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7227 * than "aff2" and 0 if they are equal.
7229 * The order is fairly arbitrary. We do consider expressions that only involve
7230 * earlier dimensions as "smaller".
7232 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7234 int cmp;
7235 int last1, last2;
7237 if (aff1 == aff2)
7238 return 0;
7240 if (!aff1)
7241 return -1;
7242 if (!aff2)
7243 return 1;
7245 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7246 if (cmp != 0)
7247 return cmp;
7249 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7250 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7251 if (last1 != last2)
7252 return last1 - last2;
7254 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7257 /* Compare two isl_pw_affs.
7259 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7260 * than "pa2" and 0 if they are equal.
7262 * The order is fairly arbitrary. We do consider expressions that only involve
7263 * earlier dimensions as "smaller".
7265 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7266 __isl_keep isl_pw_aff *pa2)
7268 int i;
7269 int cmp;
7271 if (pa1 == pa2)
7272 return 0;
7274 if (!pa1)
7275 return -1;
7276 if (!pa2)
7277 return 1;
7279 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7280 if (cmp != 0)
7281 return cmp;
7283 if (pa1->n != pa2->n)
7284 return pa1->n - pa2->n;
7286 for (i = 0; i < pa1->n; ++i) {
7287 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7288 if (cmp != 0)
7289 return cmp;
7290 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7291 if (cmp != 0)
7292 return cmp;
7295 return 0;
7298 /* Return a piecewise affine expression that is equal to "v" on "domain".
7300 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7301 __isl_take isl_val *v)
7303 isl_space *space;
7304 isl_local_space *ls;
7305 isl_aff *aff;
7307 space = isl_set_get_space(domain);
7308 ls = isl_local_space_from_space(space);
7309 aff = isl_aff_val_on_domain(ls, v);
7311 return isl_pw_aff_alloc(domain, aff);
7314 /* Return a multi affine expression that is equal to "mv" on domain
7315 * space "space".
7317 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7318 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7320 int i, n;
7321 isl_space *space2;
7322 isl_local_space *ls;
7323 isl_multi_aff *ma;
7325 if (!space || !mv)
7326 goto error;
7328 n = isl_multi_val_dim(mv, isl_dim_set);
7329 space2 = isl_multi_val_get_space(mv);
7330 space2 = isl_space_align_params(space2, isl_space_copy(space));
7331 space = isl_space_align_params(space, isl_space_copy(space2));
7332 space = isl_space_map_from_domain_and_range(space, space2);
7333 ma = isl_multi_aff_alloc(isl_space_copy(space));
7334 ls = isl_local_space_from_space(isl_space_domain(space));
7335 for (i = 0; i < n; ++i) {
7336 isl_val *v;
7337 isl_aff *aff;
7339 v = isl_multi_val_get_val(mv, i);
7340 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7341 ma = isl_multi_aff_set_aff(ma, i, aff);
7343 isl_local_space_free(ls);
7345 isl_multi_val_free(mv);
7346 return ma;
7347 error:
7348 isl_space_free(space);
7349 isl_multi_val_free(mv);
7350 return NULL;
7353 /* Return a piecewise multi-affine expression
7354 * that is equal to "mv" on "domain".
7356 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7357 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7359 isl_space *space;
7360 isl_multi_aff *ma;
7362 space = isl_set_get_space(domain);
7363 ma = isl_multi_aff_multi_val_on_space(space, mv);
7365 return isl_pw_multi_aff_alloc(domain, ma);
7368 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7369 * mv is the value that should be attained on each domain set
7370 * res collects the results
7372 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7373 isl_multi_val *mv;
7374 isl_union_pw_multi_aff *res;
7377 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7378 * and add it to data->res.
7380 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7381 void *user)
7383 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7384 isl_pw_multi_aff *pma;
7385 isl_multi_val *mv;
7387 mv = isl_multi_val_copy(data->mv);
7388 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7389 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7391 return data->res ? isl_stat_ok : isl_stat_error;
7394 /* Return a union piecewise multi-affine expression
7395 * that is equal to "mv" on "domain".
7397 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7398 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7400 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7401 isl_space *space;
7403 space = isl_union_set_get_space(domain);
7404 data.res = isl_union_pw_multi_aff_empty(space);
7405 data.mv = mv;
7406 if (isl_union_set_foreach_set(domain,
7407 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7408 data.res = isl_union_pw_multi_aff_free(data.res);
7409 isl_union_set_free(domain);
7410 isl_multi_val_free(mv);
7411 return data.res;
7414 /* Compute the pullback of data->pma by the function represented by "pma2",
7415 * provided the spaces match, and add the results to data->res.
7417 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7419 struct isl_union_pw_multi_aff_bin_data *data = user;
7421 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7422 pma2->dim, isl_dim_out)) {
7423 isl_pw_multi_aff_free(pma2);
7424 return isl_stat_ok;
7427 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7428 isl_pw_multi_aff_copy(data->pma), pma2);
7430 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7431 if (!data->res)
7432 return isl_stat_error;
7434 return isl_stat_ok;
7437 /* Compute the pullback of "upma1" by the function represented by "upma2".
7439 __isl_give isl_union_pw_multi_aff *
7440 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7441 __isl_take isl_union_pw_multi_aff *upma1,
7442 __isl_take isl_union_pw_multi_aff *upma2)
7444 return bin_op(upma1, upma2, &pullback_entry);
7447 /* Check that the domain space of "upa" matches "space".
7449 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7450 * can in principle never fail since the space "space" is that
7451 * of the isl_multi_union_pw_aff and is a set space such that
7452 * there is no domain space to match.
7454 * We check the parameters and double-check that "space" is
7455 * indeed that of a set.
7457 static isl_stat isl_union_pw_aff_check_match_domain_space(
7458 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7460 isl_space *upa_space;
7461 isl_bool match;
7463 if (!upa || !space)
7464 return isl_stat_error;
7466 match = isl_space_is_set(space);
7467 if (match < 0)
7468 return isl_stat_error;
7469 if (!match)
7470 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7471 "expecting set space", return -1);
7473 upa_space = isl_union_pw_aff_get_space(upa);
7474 match = isl_space_has_equal_params(space, upa_space);
7475 if (match < 0)
7476 goto error;
7477 if (!match)
7478 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7479 "parameters don't match", goto error);
7481 isl_space_free(upa_space);
7482 return isl_stat_ok;
7483 error:
7484 isl_space_free(upa_space);
7485 return isl_stat_error;
7488 /* Do the parameters of "upa" match those of "space"?
7490 static isl_bool isl_union_pw_aff_matching_params(
7491 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7493 isl_space *upa_space;
7494 isl_bool match;
7496 if (!upa || !space)
7497 return isl_bool_error;
7499 upa_space = isl_union_pw_aff_get_space(upa);
7501 match = isl_space_has_equal_params(space, upa_space);
7503 isl_space_free(upa_space);
7504 return match;
7507 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7508 * space represents the new parameters.
7509 * res collects the results.
7511 struct isl_union_pw_aff_reset_params_data {
7512 isl_space *space;
7513 isl_union_pw_aff *res;
7516 /* Replace the parameters of "pa" by data->space and
7517 * add the result to data->res.
7519 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7521 struct isl_union_pw_aff_reset_params_data *data = user;
7522 isl_space *space;
7524 space = isl_pw_aff_get_space(pa);
7525 space = isl_space_replace_params(space, data->space);
7526 pa = isl_pw_aff_reset_space(pa, space);
7527 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7529 return data->res ? isl_stat_ok : isl_stat_error;
7532 /* Replace the domain space of "upa" by "space".
7533 * Since a union expression does not have a (single) domain space,
7534 * "space" is necessarily a parameter space.
7536 * Since the order and the names of the parameters determine
7537 * the hash value, we need to create a new hash table.
7539 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7540 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7542 struct isl_union_pw_aff_reset_params_data data = { space };
7543 isl_bool match;
7545 match = isl_union_pw_aff_matching_params(upa, space);
7546 if (match < 0)
7547 upa = isl_union_pw_aff_free(upa);
7548 else if (match) {
7549 isl_space_free(space);
7550 return upa;
7553 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7554 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7555 data.res = isl_union_pw_aff_free(data.res);
7557 isl_union_pw_aff_free(upa);
7558 isl_space_free(space);
7559 return data.res;
7562 /* Return the floor of "pa".
7564 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7566 return isl_pw_aff_floor(pa);
7569 /* Given f, return floor(f).
7571 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7572 __isl_take isl_union_pw_aff *upa)
7574 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7577 /* Compute
7579 * upa mod m = upa - m * floor(upa/m)
7581 * with m an integer value.
7583 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7584 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7586 isl_union_pw_aff *res;
7588 if (!upa || !m)
7589 goto error;
7591 if (!isl_val_is_int(m))
7592 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7593 "expecting integer modulo", goto error);
7594 if (!isl_val_is_pos(m))
7595 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7596 "expecting positive modulo", goto error);
7598 res = isl_union_pw_aff_copy(upa);
7599 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7600 upa = isl_union_pw_aff_floor(upa);
7601 upa = isl_union_pw_aff_scale_val(upa, m);
7602 res = isl_union_pw_aff_sub(res, upa);
7604 return res;
7605 error:
7606 isl_val_free(m);
7607 isl_union_pw_aff_free(upa);
7608 return NULL;
7611 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7612 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7613 * needs to attain.
7614 * "res" collects the results.
7616 struct isl_union_pw_aff_aff_on_domain_data {
7617 isl_aff *aff;
7618 isl_union_pw_aff *res;
7621 /* Construct a piecewise affine expression that is equal to data->aff
7622 * on "domain" and add the result to data->res.
7624 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7626 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7627 isl_pw_aff *pa;
7628 isl_aff *aff;
7629 int dim;
7631 aff = isl_aff_copy(data->aff);
7632 dim = isl_set_dim(domain, isl_dim_set);
7633 aff = isl_aff_from_range(aff);
7634 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7635 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7636 pa = isl_pw_aff_alloc(domain, aff);
7637 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7639 return data->res ? isl_stat_ok : isl_stat_error;
7642 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7643 * pos is the output position that needs to be extracted.
7644 * res collects the results.
7646 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7647 int pos;
7648 isl_union_pw_aff *res;
7651 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7652 * (assuming it has such a dimension) and add it to data->res.
7654 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7656 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7657 int n_out;
7658 isl_pw_aff *pa;
7660 if (!pma)
7661 return isl_stat_error;
7663 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7664 if (data->pos >= n_out) {
7665 isl_pw_multi_aff_free(pma);
7666 return isl_stat_ok;
7669 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7670 isl_pw_multi_aff_free(pma);
7672 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7674 return data->res ? isl_stat_ok : isl_stat_error;
7677 /* Extract an isl_union_pw_aff corresponding to
7678 * output dimension "pos" of "upma".
7680 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7681 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7683 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7684 isl_space *space;
7686 if (!upma)
7687 return NULL;
7689 if (pos < 0)
7690 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7691 "cannot extract at negative position", return NULL);
7693 space = isl_union_pw_multi_aff_get_space(upma);
7694 data.res = isl_union_pw_aff_empty(space);
7695 data.pos = pos;
7696 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7697 &get_union_pw_aff, &data) < 0)
7698 data.res = isl_union_pw_aff_free(data.res);
7700 return data.res;
7703 /* Return a union piecewise affine expression
7704 * that is equal to "aff" on "domain", assuming "domain" and "aff"
7705 * have been aligned.
7707 * Construct an isl_pw_aff on each of the sets in "domain" and
7708 * collect the results.
7710 static __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain_aligned(
7711 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7713 struct isl_union_pw_aff_aff_on_domain_data data;
7714 isl_space *space;
7716 space = isl_union_set_get_space(domain);
7717 data.res = isl_union_pw_aff_empty(space);
7718 data.aff = aff;
7719 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7720 data.res = isl_union_pw_aff_free(data.res);
7721 isl_union_set_free(domain);
7722 isl_aff_free(aff);
7723 return data.res;
7726 /* Return a union piecewise affine expression
7727 * that is equal to "aff" on "domain".
7729 * Check that "aff" is a parametric expression,
7730 * align the parameters if needed and call
7731 * isl_union_pw_aff_aff_on_domain_aligned.
7733 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7734 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7736 isl_bool domain_is_params;
7737 isl_bool equal_params;
7738 isl_space *domain_space, *aff_space;
7740 if (!domain || !aff)
7741 goto error;
7742 domain_is_params = isl_local_space_is_params(aff->ls);
7743 if (domain_is_params < 0)
7744 goto error;
7745 if (!domain_is_params)
7746 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7747 "expecting parametric expression", goto error);
7749 domain_space = isl_union_set_get_space(domain);
7750 aff_space = isl_aff_get_domain_space(aff);
7751 equal_params = isl_space_has_equal_params(domain_space, aff_space);
7752 if (equal_params >= 0 && !equal_params) {
7753 isl_space *space;
7755 space = isl_space_align_params(domain_space, aff_space);
7756 aff = isl_aff_align_params(aff, isl_space_copy(space));
7757 domain = isl_union_set_align_params(domain, space);
7758 } else {
7759 isl_space_free(domain_space);
7760 isl_space_free(aff_space);
7763 if (equal_params < 0)
7764 goto error;
7765 return isl_union_pw_aff_aff_on_domain_aligned(domain, aff);
7766 error:
7767 isl_union_set_free(domain);
7768 isl_aff_free(aff);
7769 return NULL;
7772 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7773 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7774 * "res" collects the results.
7776 struct isl_union_pw_aff_val_on_domain_data {
7777 isl_val *v;
7778 isl_union_pw_aff *res;
7781 /* Construct a piecewise affine expression that is equal to data->v
7782 * on "domain" and add the result to data->res.
7784 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7786 struct isl_union_pw_aff_val_on_domain_data *data = user;
7787 isl_pw_aff *pa;
7788 isl_val *v;
7790 v = isl_val_copy(data->v);
7791 pa = isl_pw_aff_val_on_domain(domain, v);
7792 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7794 return data->res ? isl_stat_ok : isl_stat_error;
7797 /* Return a union piecewise affine expression
7798 * that is equal to "v" on "domain".
7800 * Construct an isl_pw_aff on each of the sets in "domain" and
7801 * collect the results.
7803 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7804 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7806 struct isl_union_pw_aff_val_on_domain_data data;
7807 isl_space *space;
7809 space = isl_union_set_get_space(domain);
7810 data.res = isl_union_pw_aff_empty(space);
7811 data.v = v;
7812 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7813 data.res = isl_union_pw_aff_free(data.res);
7814 isl_union_set_free(domain);
7815 isl_val_free(v);
7816 return data.res;
7819 /* Construct a piecewise multi affine expression
7820 * that is equal to "pa" and add it to upma.
7822 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7823 void *user)
7825 isl_union_pw_multi_aff **upma = user;
7826 isl_pw_multi_aff *pma;
7828 pma = isl_pw_multi_aff_from_pw_aff(pa);
7829 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7831 return *upma ? isl_stat_ok : isl_stat_error;
7834 /* Construct and return a union piecewise multi affine expression
7835 * that is equal to the given union piecewise affine expression.
7837 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7838 __isl_take isl_union_pw_aff *upa)
7840 isl_space *space;
7841 isl_union_pw_multi_aff *upma;
7843 if (!upa)
7844 return NULL;
7846 space = isl_union_pw_aff_get_space(upa);
7847 upma = isl_union_pw_multi_aff_empty(space);
7849 if (isl_union_pw_aff_foreach_pw_aff(upa,
7850 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7851 upma = isl_union_pw_multi_aff_free(upma);
7853 isl_union_pw_aff_free(upa);
7854 return upma;
7857 /* Compute the set of elements in the domain of "pa" where it is zero and
7858 * add this set to "uset".
7860 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7862 isl_union_set **uset = (isl_union_set **)user;
7864 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7866 return *uset ? isl_stat_ok : isl_stat_error;
7869 /* Return a union set containing those elements in the domain
7870 * of "upa" where it is zero.
7872 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7873 __isl_take isl_union_pw_aff *upa)
7875 isl_union_set *zero;
7877 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7878 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7879 zero = isl_union_set_free(zero);
7881 isl_union_pw_aff_free(upa);
7882 return zero;
7885 /* Convert "pa" to an isl_map and add it to *umap.
7887 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7889 isl_union_map **umap = user;
7890 isl_map *map;
7892 map = isl_map_from_pw_aff(pa);
7893 *umap = isl_union_map_add_map(*umap, map);
7895 return *umap ? isl_stat_ok : isl_stat_error;
7898 /* Construct a union map mapping the domain of the union
7899 * piecewise affine expression to its range, with the single output dimension
7900 * equated to the corresponding affine expressions on their cells.
7902 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7903 __isl_take isl_union_pw_aff *upa)
7905 isl_space *space;
7906 isl_union_map *umap;
7908 if (!upa)
7909 return NULL;
7911 space = isl_union_pw_aff_get_space(upa);
7912 umap = isl_union_map_empty(space);
7914 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7915 &umap) < 0)
7916 umap = isl_union_map_free(umap);
7918 isl_union_pw_aff_free(upa);
7919 return umap;
7922 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7923 * upma is the function that is plugged in.
7924 * pa is the current part of the function in which upma is plugged in.
7925 * res collects the results.
7927 struct isl_union_pw_aff_pullback_upma_data {
7928 isl_union_pw_multi_aff *upma;
7929 isl_pw_aff *pa;
7930 isl_union_pw_aff *res;
7933 /* Check if "pma" can be plugged into data->pa.
7934 * If so, perform the pullback and add the result to data->res.
7936 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7938 struct isl_union_pw_aff_pullback_upma_data *data = user;
7939 isl_pw_aff *pa;
7941 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7942 pma->dim, isl_dim_out)) {
7943 isl_pw_multi_aff_free(pma);
7944 return isl_stat_ok;
7947 pa = isl_pw_aff_copy(data->pa);
7948 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7950 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7952 return data->res ? isl_stat_ok : isl_stat_error;
7955 /* Check if any of the elements of data->upma can be plugged into pa,
7956 * add if so add the result to data->res.
7958 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7960 struct isl_union_pw_aff_pullback_upma_data *data = user;
7961 isl_stat r;
7963 data->pa = pa;
7964 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7965 &pa_pb_pma, data);
7966 isl_pw_aff_free(pa);
7968 return r;
7971 /* Compute the pullback of "upa" by the function represented by "upma".
7972 * In other words, plug in "upma" in "upa". The result contains
7973 * expressions defined over the domain space of "upma".
7975 * Run over all pairs of elements in "upa" and "upma", perform
7976 * the pullback when appropriate and collect the results.
7977 * If the hash value were based on the domain space rather than
7978 * the function space, then we could run through all elements
7979 * of "upma" and directly pick out the corresponding element of "upa".
7981 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7982 __isl_take isl_union_pw_aff *upa,
7983 __isl_take isl_union_pw_multi_aff *upma)
7985 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7986 isl_space *space;
7988 space = isl_union_pw_multi_aff_get_space(upma);
7989 upa = isl_union_pw_aff_align_params(upa, space);
7990 space = isl_union_pw_aff_get_space(upa);
7991 upma = isl_union_pw_multi_aff_align_params(upma, space);
7993 if (!upa || !upma)
7994 goto error;
7996 data.upma = upma;
7997 data.res = isl_union_pw_aff_alloc_same_size(upa);
7998 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7999 data.res = isl_union_pw_aff_free(data.res);
8001 isl_union_pw_aff_free(upa);
8002 isl_union_pw_multi_aff_free(upma);
8003 return data.res;
8004 error:
8005 isl_union_pw_aff_free(upa);
8006 isl_union_pw_multi_aff_free(upma);
8007 return NULL;
8010 #undef BASE
8011 #define BASE union_pw_aff
8012 #undef DOMBASE
8013 #define DOMBASE union_set
8015 #define NO_MOVE_DIMS
8016 #define NO_DOMAIN
8017 #define NO_PRODUCT
8018 #define NO_SPLICE
8019 #define NO_ZERO
8020 #define NO_IDENTITY
8021 #define NO_GIST
8023 #include <isl_multi_templ.c>
8024 #include <isl_multi_apply_set.c>
8025 #include <isl_multi_apply_union_set.c>
8026 #include <isl_multi_coalesce.c>
8027 #include <isl_multi_floor.c>
8028 #include <isl_multi_gist.c>
8029 #include <isl_multi_intersect.c>
8031 /* Construct a multiple union piecewise affine expression
8032 * in the given space with value zero in each of the output dimensions.
8034 * Since there is no canonical zero value for
8035 * a union piecewise affine expression, we can only construct
8036 * a zero-dimensional "zero" value.
8038 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8039 __isl_take isl_space *space)
8041 isl_bool params;
8043 if (!space)
8044 return NULL;
8046 params = isl_space_is_params(space);
8047 if (params < 0)
8048 goto error;
8049 if (params)
8050 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8051 "expecting proper set space", goto error);
8052 if (!isl_space_is_set(space))
8053 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8054 "expecting set space", goto error);
8055 if (isl_space_dim(space , isl_dim_out) != 0)
8056 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8057 "expecting 0D space", goto error);
8059 return isl_multi_union_pw_aff_alloc(space);
8060 error:
8061 isl_space_free(space);
8062 return NULL;
8065 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8066 * with the actual sum on the shared domain and
8067 * the defined expression on the symmetric difference of the domains.
8069 * We simply iterate over the elements in both arguments and
8070 * call isl_union_pw_aff_union_add on each of them.
8072 static __isl_give isl_multi_union_pw_aff *
8073 isl_multi_union_pw_aff_union_add_aligned(
8074 __isl_take isl_multi_union_pw_aff *mupa1,
8075 __isl_take isl_multi_union_pw_aff *mupa2)
8077 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8078 &isl_union_pw_aff_union_add);
8081 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8082 * with the actual sum on the shared domain and
8083 * the defined expression on the symmetric difference of the domains.
8085 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8086 __isl_take isl_multi_union_pw_aff *mupa1,
8087 __isl_take isl_multi_union_pw_aff *mupa2)
8089 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8090 &isl_multi_union_pw_aff_union_add_aligned);
8093 /* Construct and return a multi union piecewise affine expression
8094 * that is equal to the given multi affine expression.
8096 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8097 __isl_take isl_multi_aff *ma)
8099 isl_multi_pw_aff *mpa;
8101 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8102 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8105 /* Construct and return a multi union piecewise affine expression
8106 * that is equal to the given multi piecewise affine expression.
8108 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8109 __isl_take isl_multi_pw_aff *mpa)
8111 int i, n;
8112 isl_space *space;
8113 isl_multi_union_pw_aff *mupa;
8115 if (!mpa)
8116 return NULL;
8118 space = isl_multi_pw_aff_get_space(mpa);
8119 space = isl_space_range(space);
8120 mupa = isl_multi_union_pw_aff_alloc(space);
8122 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8123 for (i = 0; i < n; ++i) {
8124 isl_pw_aff *pa;
8125 isl_union_pw_aff *upa;
8127 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8128 upa = isl_union_pw_aff_from_pw_aff(pa);
8129 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8132 isl_multi_pw_aff_free(mpa);
8134 return mupa;
8137 /* Extract the range space of "pma" and assign it to *space.
8138 * If *space has already been set (through a previous call to this function),
8139 * then check that the range space is the same.
8141 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8143 isl_space **space = user;
8144 isl_space *pma_space;
8145 isl_bool equal;
8147 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8148 isl_pw_multi_aff_free(pma);
8150 if (!pma_space)
8151 return isl_stat_error;
8152 if (!*space) {
8153 *space = pma_space;
8154 return isl_stat_ok;
8157 equal = isl_space_is_equal(pma_space, *space);
8158 isl_space_free(pma_space);
8160 if (equal < 0)
8161 return isl_stat_error;
8162 if (!equal)
8163 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8164 "range spaces not the same", return isl_stat_error);
8165 return isl_stat_ok;
8168 /* Construct and return a multi union piecewise affine expression
8169 * that is equal to the given union piecewise multi affine expression.
8171 * In order to be able to perform the conversion, the input
8172 * needs to be non-empty and may only involve a single range space.
8174 __isl_give isl_multi_union_pw_aff *
8175 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8176 __isl_take isl_union_pw_multi_aff *upma)
8178 isl_space *space = NULL;
8179 isl_multi_union_pw_aff *mupa;
8180 int i, n;
8182 if (!upma)
8183 return NULL;
8184 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8185 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8186 "cannot extract range space from empty input",
8187 goto error);
8188 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8189 &space) < 0)
8190 goto error;
8192 if (!space)
8193 goto error;
8195 n = isl_space_dim(space, isl_dim_set);
8196 mupa = isl_multi_union_pw_aff_alloc(space);
8198 for (i = 0; i < n; ++i) {
8199 isl_union_pw_aff *upa;
8201 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8202 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8205 isl_union_pw_multi_aff_free(upma);
8206 return mupa;
8207 error:
8208 isl_space_free(space);
8209 isl_union_pw_multi_aff_free(upma);
8210 return NULL;
8213 /* Try and create an isl_multi_union_pw_aff that is equivalent
8214 * to the given isl_union_map.
8215 * The isl_union_map is required to be single-valued in each space.
8216 * Moreover, it cannot be empty and all range spaces need to be the same.
8217 * Otherwise, an error is produced.
8219 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8220 __isl_take isl_union_map *umap)
8222 isl_union_pw_multi_aff *upma;
8224 upma = isl_union_pw_multi_aff_from_union_map(umap);
8225 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8228 /* Return a multiple union piecewise affine expression
8229 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8230 * have been aligned.
8232 static __isl_give isl_multi_union_pw_aff *
8233 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8234 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8236 int i, n;
8237 isl_space *space;
8238 isl_multi_union_pw_aff *mupa;
8240 if (!domain || !mv)
8241 goto error;
8243 n = isl_multi_val_dim(mv, isl_dim_set);
8244 space = isl_multi_val_get_space(mv);
8245 mupa = isl_multi_union_pw_aff_alloc(space);
8246 for (i = 0; i < n; ++i) {
8247 isl_val *v;
8248 isl_union_pw_aff *upa;
8250 v = isl_multi_val_get_val(mv, i);
8251 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8253 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8256 isl_union_set_free(domain);
8257 isl_multi_val_free(mv);
8258 return mupa;
8259 error:
8260 isl_union_set_free(domain);
8261 isl_multi_val_free(mv);
8262 return NULL;
8265 /* Return a multiple union piecewise affine expression
8266 * that is equal to "mv" on "domain".
8268 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8269 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8271 isl_bool equal_params;
8273 if (!domain || !mv)
8274 goto error;
8275 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8276 if (equal_params < 0)
8277 goto error;
8278 if (equal_params)
8279 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8280 domain, mv);
8281 domain = isl_union_set_align_params(domain,
8282 isl_multi_val_get_space(mv));
8283 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8284 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8285 error:
8286 isl_union_set_free(domain);
8287 isl_multi_val_free(mv);
8288 return NULL;
8291 /* Return a multiple union piecewise affine expression
8292 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8293 * have been aligned.
8295 static __isl_give isl_multi_union_pw_aff *
8296 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8297 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8299 int i, n;
8300 isl_space *space;
8301 isl_multi_union_pw_aff *mupa;
8303 if (!domain || !ma)
8304 goto error;
8306 n = isl_multi_aff_dim(ma, isl_dim_set);
8307 space = isl_multi_aff_get_space(ma);
8308 mupa = isl_multi_union_pw_aff_alloc(space);
8309 for (i = 0; i < n; ++i) {
8310 isl_aff *aff;
8311 isl_union_pw_aff *upa;
8313 aff = isl_multi_aff_get_aff(ma, i);
8314 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8315 aff);
8316 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8319 isl_union_set_free(domain);
8320 isl_multi_aff_free(ma);
8321 return mupa;
8322 error:
8323 isl_union_set_free(domain);
8324 isl_multi_aff_free(ma);
8325 return NULL;
8328 /* Return a multiple union piecewise affine expression
8329 * that is equal to "ma" on "domain".
8331 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8332 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8334 isl_bool equal_params;
8336 if (!domain || !ma)
8337 goto error;
8338 equal_params = isl_space_has_equal_params(domain->dim, ma->space);
8339 if (equal_params < 0)
8340 goto error;
8341 if (equal_params)
8342 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8343 domain, ma);
8344 domain = isl_union_set_align_params(domain,
8345 isl_multi_aff_get_space(ma));
8346 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8347 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8348 error:
8349 isl_union_set_free(domain);
8350 isl_multi_aff_free(ma);
8351 return NULL;
8354 /* Return a union set containing those elements in the domains
8355 * of the elements of "mupa" where they are all zero.
8357 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8358 __isl_take isl_multi_union_pw_aff *mupa)
8360 int i, n;
8361 isl_union_pw_aff *upa;
8362 isl_union_set *zero;
8364 if (!mupa)
8365 return NULL;
8367 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8368 if (n == 0)
8369 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8370 "cannot determine zero set "
8371 "of zero-dimensional function", goto error);
8373 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8374 zero = isl_union_pw_aff_zero_union_set(upa);
8376 for (i = 1; i < n; ++i) {
8377 isl_union_set *zero_i;
8379 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8380 zero_i = isl_union_pw_aff_zero_union_set(upa);
8382 zero = isl_union_set_intersect(zero, zero_i);
8385 isl_multi_union_pw_aff_free(mupa);
8386 return zero;
8387 error:
8388 isl_multi_union_pw_aff_free(mupa);
8389 return NULL;
8392 /* Construct a union map mapping the shared domain
8393 * of the union piecewise affine expressions to the range of "mupa"
8394 * with each dimension in the range equated to the
8395 * corresponding union piecewise affine expression.
8397 * The input cannot be zero-dimensional as there is
8398 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8400 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8401 __isl_take isl_multi_union_pw_aff *mupa)
8403 int i, n;
8404 isl_space *space;
8405 isl_union_map *umap;
8406 isl_union_pw_aff *upa;
8408 if (!mupa)
8409 return NULL;
8411 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8412 if (n == 0)
8413 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8414 "cannot determine domain of zero-dimensional "
8415 "isl_multi_union_pw_aff", goto error);
8417 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8418 umap = isl_union_map_from_union_pw_aff(upa);
8420 for (i = 1; i < n; ++i) {
8421 isl_union_map *umap_i;
8423 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8424 umap_i = isl_union_map_from_union_pw_aff(upa);
8425 umap = isl_union_map_flat_range_product(umap, umap_i);
8428 space = isl_multi_union_pw_aff_get_space(mupa);
8429 umap = isl_union_map_reset_range_space(umap, space);
8431 isl_multi_union_pw_aff_free(mupa);
8432 return umap;
8433 error:
8434 isl_multi_union_pw_aff_free(mupa);
8435 return NULL;
8438 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8439 * "range" is the space from which to set the range space.
8440 * "res" collects the results.
8442 struct isl_union_pw_multi_aff_reset_range_space_data {
8443 isl_space *range;
8444 isl_union_pw_multi_aff *res;
8447 /* Replace the range space of "pma" by the range space of data->range and
8448 * add the result to data->res.
8450 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8452 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8453 isl_space *space;
8455 space = isl_pw_multi_aff_get_space(pma);
8456 space = isl_space_domain(space);
8457 space = isl_space_extend_domain_with_range(space,
8458 isl_space_copy(data->range));
8459 pma = isl_pw_multi_aff_reset_space(pma, space);
8460 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8462 return data->res ? isl_stat_ok : isl_stat_error;
8465 /* Replace the range space of all the piecewise affine expressions in "upma" by
8466 * the range space of "space".
8468 * This assumes that all these expressions have the same output dimension.
8470 * Since the spaces of the expressions change, so do their hash values.
8471 * We therefore need to create a new isl_union_pw_multi_aff.
8472 * Note that the hash value is currently computed based on the entire
8473 * space even though there can only be a single expression with a given
8474 * domain space.
8476 static __isl_give isl_union_pw_multi_aff *
8477 isl_union_pw_multi_aff_reset_range_space(
8478 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8480 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8481 isl_space *space_upma;
8483 space_upma = isl_union_pw_multi_aff_get_space(upma);
8484 data.res = isl_union_pw_multi_aff_empty(space_upma);
8485 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8486 &reset_range_space, &data) < 0)
8487 data.res = isl_union_pw_multi_aff_free(data.res);
8489 isl_space_free(space);
8490 isl_union_pw_multi_aff_free(upma);
8491 return data.res;
8494 /* Construct and return a union piecewise multi affine expression
8495 * that is equal to the given multi union piecewise affine expression.
8497 * In order to be able to perform the conversion, the input
8498 * needs to have a least one output dimension.
8500 __isl_give isl_union_pw_multi_aff *
8501 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8502 __isl_take isl_multi_union_pw_aff *mupa)
8504 int i, n;
8505 isl_space *space;
8506 isl_union_pw_multi_aff *upma;
8507 isl_union_pw_aff *upa;
8509 if (!mupa)
8510 return NULL;
8512 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8513 if (n == 0)
8514 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8515 "cannot determine domain of zero-dimensional "
8516 "isl_multi_union_pw_aff", goto error);
8518 space = isl_multi_union_pw_aff_get_space(mupa);
8519 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8520 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8522 for (i = 1; i < n; ++i) {
8523 isl_union_pw_multi_aff *upma_i;
8525 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8526 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8527 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8530 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8532 isl_multi_union_pw_aff_free(mupa);
8533 return upma;
8534 error:
8535 isl_multi_union_pw_aff_free(mupa);
8536 return NULL;
8539 /* Intersect the range of "mupa" with "range".
8540 * That is, keep only those domain elements that have a function value
8541 * in "range".
8543 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8544 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8546 isl_union_pw_multi_aff *upma;
8547 isl_union_set *domain;
8548 isl_space *space;
8549 int n;
8550 int match;
8552 if (!mupa || !range)
8553 goto error;
8555 space = isl_set_get_space(range);
8556 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8557 space, isl_dim_set);
8558 isl_space_free(space);
8559 if (match < 0)
8560 goto error;
8561 if (!match)
8562 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8563 "space don't match", goto error);
8564 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8565 if (n == 0)
8566 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8567 "cannot intersect range of zero-dimensional "
8568 "isl_multi_union_pw_aff", goto error);
8570 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8571 isl_multi_union_pw_aff_copy(mupa));
8572 domain = isl_union_set_from_set(range);
8573 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8574 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8576 return mupa;
8577 error:
8578 isl_multi_union_pw_aff_free(mupa);
8579 isl_set_free(range);
8580 return NULL;
8583 /* Return the shared domain of the elements of "mupa".
8585 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8586 __isl_take isl_multi_union_pw_aff *mupa)
8588 int i, n;
8589 isl_union_pw_aff *upa;
8590 isl_union_set *dom;
8592 if (!mupa)
8593 return NULL;
8595 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8596 if (n == 0)
8597 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8598 "cannot determine domain", goto error);
8600 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8601 dom = isl_union_pw_aff_domain(upa);
8602 for (i = 1; i < n; ++i) {
8603 isl_union_set *dom_i;
8605 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8606 dom_i = isl_union_pw_aff_domain(upa);
8607 dom = isl_union_set_intersect(dom, dom_i);
8610 isl_multi_union_pw_aff_free(mupa);
8611 return dom;
8612 error:
8613 isl_multi_union_pw_aff_free(mupa);
8614 return NULL;
8617 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8618 * In particular, the spaces have been aligned.
8619 * The result is defined over the shared domain of the elements of "mupa"
8621 * We first extract the parametric constant part of "aff" and
8622 * define that over the shared domain.
8623 * Then we iterate over all input dimensions of "aff" and add the corresponding
8624 * multiples of the elements of "mupa".
8625 * Finally, we consider the integer divisions, calling the function
8626 * recursively to obtain an isl_union_pw_aff corresponding to the
8627 * integer division argument.
8629 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8630 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8632 int i, n_in, n_div;
8633 isl_union_pw_aff *upa;
8634 isl_union_set *uset;
8635 isl_val *v;
8636 isl_aff *cst;
8638 n_in = isl_aff_dim(aff, isl_dim_in);
8639 n_div = isl_aff_dim(aff, isl_dim_div);
8641 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8642 cst = isl_aff_copy(aff);
8643 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8644 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8645 cst = isl_aff_project_domain_on_params(cst);
8646 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8648 for (i = 0; i < n_in; ++i) {
8649 isl_union_pw_aff *upa_i;
8651 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8652 continue;
8653 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8654 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8655 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8656 upa = isl_union_pw_aff_add(upa, upa_i);
8659 for (i = 0; i < n_div; ++i) {
8660 isl_aff *div;
8661 isl_union_pw_aff *upa_i;
8663 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8664 continue;
8665 div = isl_aff_get_div(aff, i);
8666 upa_i = multi_union_pw_aff_apply_aff(
8667 isl_multi_union_pw_aff_copy(mupa), div);
8668 upa_i = isl_union_pw_aff_floor(upa_i);
8669 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8670 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8671 upa = isl_union_pw_aff_add(upa, upa_i);
8674 isl_multi_union_pw_aff_free(mupa);
8675 isl_aff_free(aff);
8677 return upa;
8680 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8681 * with the domain of "aff".
8682 * Furthermore, the dimension of this space needs to be greater than zero.
8683 * The result is defined over the shared domain of the elements of "mupa"
8685 * We perform these checks and then hand over control to
8686 * multi_union_pw_aff_apply_aff.
8688 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8689 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8691 isl_space *space1, *space2;
8692 int equal;
8694 mupa = isl_multi_union_pw_aff_align_params(mupa,
8695 isl_aff_get_space(aff));
8696 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8697 if (!mupa || !aff)
8698 goto error;
8700 space1 = isl_multi_union_pw_aff_get_space(mupa);
8701 space2 = isl_aff_get_domain_space(aff);
8702 equal = isl_space_is_equal(space1, space2);
8703 isl_space_free(space1);
8704 isl_space_free(space2);
8705 if (equal < 0)
8706 goto error;
8707 if (!equal)
8708 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8709 "spaces don't match", goto error);
8710 if (isl_aff_dim(aff, isl_dim_in) == 0)
8711 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8712 "cannot determine domains", goto error);
8714 return multi_union_pw_aff_apply_aff(mupa, aff);
8715 error:
8716 isl_multi_union_pw_aff_free(mupa);
8717 isl_aff_free(aff);
8718 return NULL;
8721 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8722 * with the domain of "ma".
8723 * Furthermore, the dimension of this space needs to be greater than zero,
8724 * unless the dimension of the target space of "ma" is also zero.
8725 * The result is defined over the shared domain of the elements of "mupa"
8727 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8728 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8730 isl_space *space1, *space2;
8731 isl_multi_union_pw_aff *res;
8732 int equal;
8733 int i, n_out;
8735 mupa = isl_multi_union_pw_aff_align_params(mupa,
8736 isl_multi_aff_get_space(ma));
8737 ma = isl_multi_aff_align_params(ma,
8738 isl_multi_union_pw_aff_get_space(mupa));
8739 if (!mupa || !ma)
8740 goto error;
8742 space1 = isl_multi_union_pw_aff_get_space(mupa);
8743 space2 = isl_multi_aff_get_domain_space(ma);
8744 equal = isl_space_is_equal(space1, space2);
8745 isl_space_free(space1);
8746 isl_space_free(space2);
8747 if (equal < 0)
8748 goto error;
8749 if (!equal)
8750 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8751 "spaces don't match", goto error);
8752 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8753 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8754 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8755 "cannot determine domains", goto error);
8757 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8758 res = isl_multi_union_pw_aff_alloc(space1);
8760 for (i = 0; i < n_out; ++i) {
8761 isl_aff *aff;
8762 isl_union_pw_aff *upa;
8764 aff = isl_multi_aff_get_aff(ma, i);
8765 upa = multi_union_pw_aff_apply_aff(
8766 isl_multi_union_pw_aff_copy(mupa), aff);
8767 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8770 isl_multi_aff_free(ma);
8771 isl_multi_union_pw_aff_free(mupa);
8772 return res;
8773 error:
8774 isl_multi_union_pw_aff_free(mupa);
8775 isl_multi_aff_free(ma);
8776 return NULL;
8779 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8780 * with the domain of "pa".
8781 * Furthermore, the dimension of this space needs to be greater than zero.
8782 * The result is defined over the shared domain of the elements of "mupa"
8784 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8785 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8787 int i;
8788 int equal;
8789 isl_space *space, *space2;
8790 isl_union_pw_aff *upa;
8792 mupa = isl_multi_union_pw_aff_align_params(mupa,
8793 isl_pw_aff_get_space(pa));
8794 pa = isl_pw_aff_align_params(pa,
8795 isl_multi_union_pw_aff_get_space(mupa));
8796 if (!mupa || !pa)
8797 goto error;
8799 space = isl_multi_union_pw_aff_get_space(mupa);
8800 space2 = isl_pw_aff_get_domain_space(pa);
8801 equal = isl_space_is_equal(space, space2);
8802 isl_space_free(space);
8803 isl_space_free(space2);
8804 if (equal < 0)
8805 goto error;
8806 if (!equal)
8807 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8808 "spaces don't match", goto error);
8809 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8810 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8811 "cannot determine domains", goto error);
8813 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8814 upa = isl_union_pw_aff_empty(space);
8816 for (i = 0; i < pa->n; ++i) {
8817 isl_aff *aff;
8818 isl_set *domain;
8819 isl_multi_union_pw_aff *mupa_i;
8820 isl_union_pw_aff *upa_i;
8822 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8823 domain = isl_set_copy(pa->p[i].set);
8824 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8825 aff = isl_aff_copy(pa->p[i].aff);
8826 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8827 upa = isl_union_pw_aff_union_add(upa, upa_i);
8830 isl_multi_union_pw_aff_free(mupa);
8831 isl_pw_aff_free(pa);
8832 return upa;
8833 error:
8834 isl_multi_union_pw_aff_free(mupa);
8835 isl_pw_aff_free(pa);
8836 return NULL;
8839 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8840 * with the domain of "pma".
8841 * Furthermore, the dimension of this space needs to be greater than zero,
8842 * unless the dimension of the target space of "pma" is also zero.
8843 * The result is defined over the shared domain of the elements of "mupa"
8845 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8846 __isl_take isl_multi_union_pw_aff *mupa,
8847 __isl_take isl_pw_multi_aff *pma)
8849 isl_space *space1, *space2;
8850 isl_multi_union_pw_aff *res;
8851 int equal;
8852 int i, n_out;
8854 mupa = isl_multi_union_pw_aff_align_params(mupa,
8855 isl_pw_multi_aff_get_space(pma));
8856 pma = isl_pw_multi_aff_align_params(pma,
8857 isl_multi_union_pw_aff_get_space(mupa));
8858 if (!mupa || !pma)
8859 goto error;
8861 space1 = isl_multi_union_pw_aff_get_space(mupa);
8862 space2 = isl_pw_multi_aff_get_domain_space(pma);
8863 equal = isl_space_is_equal(space1, space2);
8864 isl_space_free(space1);
8865 isl_space_free(space2);
8866 if (equal < 0)
8867 goto error;
8868 if (!equal)
8869 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8870 "spaces don't match", goto error);
8871 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8872 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8873 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8874 "cannot determine domains", goto error);
8876 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8877 res = isl_multi_union_pw_aff_alloc(space1);
8879 for (i = 0; i < n_out; ++i) {
8880 isl_pw_aff *pa;
8881 isl_union_pw_aff *upa;
8883 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8884 upa = isl_multi_union_pw_aff_apply_pw_aff(
8885 isl_multi_union_pw_aff_copy(mupa), pa);
8886 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8889 isl_pw_multi_aff_free(pma);
8890 isl_multi_union_pw_aff_free(mupa);
8891 return res;
8892 error:
8893 isl_multi_union_pw_aff_free(mupa);
8894 isl_pw_multi_aff_free(pma);
8895 return NULL;
8898 /* Compute the pullback of "mupa" by the function represented by "upma".
8899 * In other words, plug in "upma" in "mupa". The result contains
8900 * expressions defined over the domain space of "upma".
8902 * Run over all elements of "mupa" and plug in "upma" in each of them.
8904 __isl_give isl_multi_union_pw_aff *
8905 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8906 __isl_take isl_multi_union_pw_aff *mupa,
8907 __isl_take isl_union_pw_multi_aff *upma)
8909 int i, n;
8911 mupa = isl_multi_union_pw_aff_align_params(mupa,
8912 isl_union_pw_multi_aff_get_space(upma));
8913 upma = isl_union_pw_multi_aff_align_params(upma,
8914 isl_multi_union_pw_aff_get_space(mupa));
8915 if (!mupa || !upma)
8916 goto error;
8918 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8919 for (i = 0; i < n; ++i) {
8920 isl_union_pw_aff *upa;
8922 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8923 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8924 isl_union_pw_multi_aff_copy(upma));
8925 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8928 isl_union_pw_multi_aff_free(upma);
8929 return mupa;
8930 error:
8931 isl_multi_union_pw_aff_free(mupa);
8932 isl_union_pw_multi_aff_free(upma);
8933 return NULL;
8936 /* Extract the sequence of elements in "mupa" with domain space "space"
8937 * (ignoring parameters).
8939 * For the elements of "mupa" that are not defined on the specified space,
8940 * the corresponding element in the result is empty.
8942 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8943 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8945 int i, n;
8946 isl_bool equal_params;
8947 isl_space *space_mpa = NULL;
8948 isl_multi_pw_aff *mpa;
8950 if (!mupa || !space)
8951 goto error;
8953 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8954 equal_params = isl_space_has_equal_params(space_mpa, space);
8955 if (equal_params < 0)
8956 goto error;
8957 if (!equal_params) {
8958 space = isl_space_drop_dims(space, isl_dim_param,
8959 0, isl_space_dim(space, isl_dim_param));
8960 space = isl_space_align_params(space,
8961 isl_space_copy(space_mpa));
8962 if (!space)
8963 goto error;
8965 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8966 space_mpa);
8967 mpa = isl_multi_pw_aff_alloc(space_mpa);
8969 space = isl_space_from_domain(space);
8970 space = isl_space_add_dims(space, isl_dim_out, 1);
8971 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8972 for (i = 0; i < n; ++i) {
8973 isl_union_pw_aff *upa;
8974 isl_pw_aff *pa;
8976 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8977 pa = isl_union_pw_aff_extract_pw_aff(upa,
8978 isl_space_copy(space));
8979 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8980 isl_union_pw_aff_free(upa);
8983 isl_space_free(space);
8984 return mpa;
8985 error:
8986 isl_space_free(space_mpa);
8987 isl_space_free(space);
8988 return NULL;