isl_local_space_divs_known: extract out isl_local_divs_known
[isl.git] / isl_aff.c
blobb0a2aa366cb8724f9e6cc1f40f2fa57ad623885a
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 /* Return an affine expression that is equal to the parameter
228 * in the domain space "space" with identifier "id".
230 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
231 __isl_take isl_space *space, __isl_take isl_id *id)
233 int pos;
234 isl_local_space *ls;
236 if (!space || !id)
237 goto error;
238 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
239 if (pos < 0)
240 isl_die(isl_space_get_ctx(space), isl_error_invalid,
241 "parameter not found in space", goto error);
242 isl_id_free(id);
243 ls = isl_local_space_from_space(space);
244 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
245 error:
246 isl_space_free(space);
247 isl_id_free(id);
248 return NULL;
251 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
253 if (!aff)
254 return NULL;
256 aff->ref++;
257 return aff;
260 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
262 if (!aff)
263 return NULL;
265 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
266 isl_vec_copy(aff->v));
269 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
271 if (!aff)
272 return NULL;
274 if (aff->ref == 1)
275 return aff;
276 aff->ref--;
277 return isl_aff_dup(aff);
280 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
282 if (!aff)
283 return NULL;
285 if (--aff->ref > 0)
286 return NULL;
288 isl_local_space_free(aff->ls);
289 isl_vec_free(aff->v);
291 free(aff);
293 return NULL;
296 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
298 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
301 /* Return a hash value that digests "aff".
303 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
305 uint32_t hash, ls_hash, v_hash;
307 if (!aff)
308 return 0;
310 hash = isl_hash_init();
311 ls_hash = isl_local_space_get_hash(aff->ls);
312 isl_hash_hash(hash, ls_hash);
313 v_hash = isl_vec_get_hash(aff->v);
314 isl_hash_hash(hash, v_hash);
316 return hash;
319 /* Externally, an isl_aff has a map space, but internally, the
320 * ls field corresponds to the domain of that space.
322 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
324 if (!aff)
325 return 0;
326 if (type == isl_dim_out)
327 return 1;
328 if (type == isl_dim_in)
329 type = isl_dim_set;
330 return isl_local_space_dim(aff->ls, type);
333 /* Return the position of the dimension of the given type and name
334 * in "aff".
335 * Return -1 if no such dimension can be found.
337 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
338 const char *name)
340 if (!aff)
341 return -1;
342 if (type == isl_dim_out)
343 return -1;
344 if (type == isl_dim_in)
345 type = isl_dim_set;
346 return isl_local_space_find_dim_by_name(aff->ls, type, name);
349 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
351 return aff ? isl_local_space_get_space(aff->ls) : NULL;
354 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
356 isl_space *space;
357 if (!aff)
358 return NULL;
359 space = isl_local_space_get_space(aff->ls);
360 space = isl_space_from_domain(space);
361 space = isl_space_add_dims(space, isl_dim_out, 1);
362 return space;
365 __isl_give isl_local_space *isl_aff_get_domain_local_space(
366 __isl_keep isl_aff *aff)
368 return aff ? isl_local_space_copy(aff->ls) : NULL;
371 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
373 isl_local_space *ls;
374 if (!aff)
375 return NULL;
376 ls = isl_local_space_copy(aff->ls);
377 ls = isl_local_space_from_domain(ls);
378 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
379 return ls;
382 /* Return the local space of the domain of "aff".
383 * This may be either a copy or the local space itself
384 * if there is only one reference to "aff".
385 * This allows the local space to be modified inplace
386 * if both the expression and its local space have only a single reference.
387 * The caller is not allowed to modify "aff" between this call and
388 * a subsequent call to isl_aff_restore_domain_local_space.
389 * The only exception is that isl_aff_free can be called instead.
391 __isl_give isl_local_space *isl_aff_take_domain_local_space(
392 __isl_keep isl_aff *aff)
394 isl_local_space *ls;
396 if (!aff)
397 return NULL;
398 if (aff->ref != 1)
399 return isl_aff_get_domain_local_space(aff);
400 ls = aff->ls;
401 aff->ls = NULL;
402 return ls;
405 /* Set the local space of the domain of "aff" to "ls",
406 * where the local space of "aff" may be missing
407 * due to a preceding call to isl_aff_take_domain_local_space.
408 * However, in this case, "aff" only has a single reference and
409 * then the call to isl_aff_cow has no effect.
411 __isl_give isl_aff *isl_aff_restore_domain_local_space(
412 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
414 if (!aff || !ls)
415 goto error;
417 if (aff->ls == ls) {
418 isl_local_space_free(ls);
419 return aff;
422 aff = isl_aff_cow(aff);
423 if (!aff)
424 goto error;
425 isl_local_space_free(aff->ls);
426 aff->ls = ls;
428 return aff;
429 error:
430 isl_aff_free(aff);
431 isl_local_space_free(ls);
432 return NULL;
435 /* Externally, an isl_aff has a map space, but internally, the
436 * ls field corresponds to the domain of that space.
438 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
439 enum isl_dim_type type, unsigned pos)
441 if (!aff)
442 return NULL;
443 if (type == isl_dim_out)
444 return NULL;
445 if (type == isl_dim_in)
446 type = isl_dim_set;
447 return isl_local_space_get_dim_name(aff->ls, type, pos);
450 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
451 __isl_take isl_space *dim)
453 aff = isl_aff_cow(aff);
454 if (!aff || !dim)
455 goto error;
457 aff->ls = isl_local_space_reset_space(aff->ls, dim);
458 if (!aff->ls)
459 return isl_aff_free(aff);
461 return aff;
462 error:
463 isl_aff_free(aff);
464 isl_space_free(dim);
465 return NULL;
468 /* Reset the space of "aff". This function is called from isl_pw_templ.c
469 * and doesn't know if the space of an element object is represented
470 * directly or through its domain. It therefore passes along both.
472 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
473 __isl_take isl_space *space, __isl_take isl_space *domain)
475 isl_space_free(space);
476 return isl_aff_reset_domain_space(aff, domain);
479 /* Reorder the coefficients of the affine expression based
480 * on the given reordering.
481 * The reordering r is assumed to have been extended with the local
482 * variables.
484 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
485 __isl_take isl_reordering *r, int n_div)
487 isl_vec *res;
488 int i;
490 if (!vec || !r)
491 goto error;
493 res = isl_vec_alloc(vec->ctx,
494 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
495 if (!res)
496 goto error;
497 isl_seq_cpy(res->el, vec->el, 2);
498 isl_seq_clr(res->el + 2, res->size - 2);
499 for (i = 0; i < r->len; ++i)
500 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
502 isl_reordering_free(r);
503 isl_vec_free(vec);
504 return res;
505 error:
506 isl_vec_free(vec);
507 isl_reordering_free(r);
508 return NULL;
511 /* Reorder the dimensions of the domain of "aff" according
512 * to the given reordering.
514 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
515 __isl_take isl_reordering *r)
517 aff = isl_aff_cow(aff);
518 if (!aff)
519 goto error;
521 r = isl_reordering_extend(r, aff->ls->div->n_row);
522 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
523 aff->ls->div->n_row);
524 aff->ls = isl_local_space_realign(aff->ls, r);
526 if (!aff->v || !aff->ls)
527 return isl_aff_free(aff);
529 return aff;
530 error:
531 isl_aff_free(aff);
532 isl_reordering_free(r);
533 return NULL;
536 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
537 __isl_take isl_space *model)
539 isl_bool equal_params;
541 if (!aff || !model)
542 goto error;
544 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
545 if (equal_params < 0)
546 goto error;
547 if (!equal_params) {
548 isl_reordering *exp;
550 model = isl_space_drop_dims(model, isl_dim_in,
551 0, isl_space_dim(model, isl_dim_in));
552 model = isl_space_drop_dims(model, isl_dim_out,
553 0, isl_space_dim(model, isl_dim_out));
554 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
555 exp = isl_reordering_extend_space(exp,
556 isl_aff_get_domain_space(aff));
557 aff = isl_aff_realign_domain(aff, exp);
560 isl_space_free(model);
561 return aff;
562 error:
563 isl_space_free(model);
564 isl_aff_free(aff);
565 return NULL;
568 /* Is "aff" obviously equal to zero?
570 * If the denominator is zero, then "aff" is not equal to zero.
572 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
574 if (!aff)
575 return isl_bool_error;
577 if (isl_int_is_zero(aff->v->el[0]))
578 return isl_bool_false;
579 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
582 /* Does "aff" represent NaN?
584 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
586 if (!aff)
587 return isl_bool_error;
589 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
592 /* Are "aff1" and "aff2" obviously equal?
594 * NaN is not equal to anything, not even to another NaN.
596 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
597 __isl_keep isl_aff *aff2)
599 isl_bool equal;
601 if (!aff1 || !aff2)
602 return isl_bool_error;
604 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
605 return isl_bool_false;
607 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
608 if (equal < 0 || !equal)
609 return equal;
611 return isl_vec_is_equal(aff1->v, aff2->v);
614 /* Return the common denominator of "aff" in "v".
616 * We cannot return anything meaningful in case of a NaN.
618 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
620 if (!aff)
621 return isl_stat_error;
622 if (isl_aff_is_nan(aff))
623 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
624 "cannot get denominator of NaN", return isl_stat_error);
625 isl_int_set(*v, aff->v->el[0]);
626 return isl_stat_ok;
629 /* Return the common denominator of "aff".
631 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
633 isl_ctx *ctx;
635 if (!aff)
636 return NULL;
638 ctx = isl_aff_get_ctx(aff);
639 if (isl_aff_is_nan(aff))
640 return isl_val_nan(ctx);
641 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
644 /* Return the constant term of "aff".
646 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
648 isl_ctx *ctx;
649 isl_val *v;
651 if (!aff)
652 return NULL;
654 ctx = isl_aff_get_ctx(aff);
655 if (isl_aff_is_nan(aff))
656 return isl_val_nan(ctx);
657 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
658 return isl_val_normalize(v);
661 /* Return the coefficient of the variable of type "type" at position "pos"
662 * of "aff".
664 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
665 enum isl_dim_type type, int pos)
667 isl_ctx *ctx;
668 isl_val *v;
670 if (!aff)
671 return NULL;
673 ctx = isl_aff_get_ctx(aff);
674 if (type == isl_dim_out)
675 isl_die(ctx, isl_error_invalid,
676 "output/set dimension does not have a coefficient",
677 return NULL);
678 if (type == isl_dim_in)
679 type = isl_dim_set;
681 if (pos >= isl_local_space_dim(aff->ls, type))
682 isl_die(ctx, isl_error_invalid,
683 "position out of bounds", return NULL);
685 if (isl_aff_is_nan(aff))
686 return isl_val_nan(ctx);
687 pos += isl_local_space_offset(aff->ls, type);
688 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
689 return isl_val_normalize(v);
692 /* Return the sign of the coefficient of the variable of type "type"
693 * at position "pos" of "aff".
695 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
696 int pos)
698 isl_ctx *ctx;
700 if (!aff)
701 return 0;
703 ctx = isl_aff_get_ctx(aff);
704 if (type == isl_dim_out)
705 isl_die(ctx, isl_error_invalid,
706 "output/set dimension does not have a coefficient",
707 return 0);
708 if (type == isl_dim_in)
709 type = isl_dim_set;
711 if (pos >= isl_local_space_dim(aff->ls, type))
712 isl_die(ctx, isl_error_invalid,
713 "position out of bounds", return 0);
715 pos += isl_local_space_offset(aff->ls, type);
716 return isl_int_sgn(aff->v->el[1 + pos]);
719 /* Replace the numerator of the constant term of "aff" by "v".
721 * A NaN is unaffected by this operation.
723 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
725 if (!aff)
726 return NULL;
727 if (isl_aff_is_nan(aff))
728 return aff;
729 aff = isl_aff_cow(aff);
730 if (!aff)
731 return NULL;
733 aff->v = isl_vec_cow(aff->v);
734 if (!aff->v)
735 return isl_aff_free(aff);
737 isl_int_set(aff->v->el[1], v);
739 return aff;
742 /* Replace the constant term of "aff" by "v".
744 * A NaN is unaffected by this operation.
746 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
747 __isl_take isl_val *v)
749 if (!aff || !v)
750 goto error;
752 if (isl_aff_is_nan(aff)) {
753 isl_val_free(v);
754 return aff;
757 if (!isl_val_is_rat(v))
758 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
759 "expecting rational value", goto error);
761 if (isl_int_eq(aff->v->el[1], v->n) &&
762 isl_int_eq(aff->v->el[0], v->d)) {
763 isl_val_free(v);
764 return aff;
767 aff = isl_aff_cow(aff);
768 if (!aff)
769 goto error;
770 aff->v = isl_vec_cow(aff->v);
771 if (!aff->v)
772 goto error;
774 if (isl_int_eq(aff->v->el[0], v->d)) {
775 isl_int_set(aff->v->el[1], v->n);
776 } else if (isl_int_is_one(v->d)) {
777 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
778 } else {
779 isl_seq_scale(aff->v->el + 1,
780 aff->v->el + 1, v->d, aff->v->size - 1);
781 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
782 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
783 aff->v = isl_vec_normalize(aff->v);
784 if (!aff->v)
785 goto error;
788 isl_val_free(v);
789 return aff;
790 error:
791 isl_aff_free(aff);
792 isl_val_free(v);
793 return NULL;
796 /* Add "v" to the constant term of "aff".
798 * A NaN is unaffected by this operation.
800 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
802 if (isl_int_is_zero(v))
803 return aff;
805 if (!aff)
806 return NULL;
807 if (isl_aff_is_nan(aff))
808 return aff;
809 aff = isl_aff_cow(aff);
810 if (!aff)
811 return NULL;
813 aff->v = isl_vec_cow(aff->v);
814 if (!aff->v)
815 return isl_aff_free(aff);
817 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
819 return aff;
822 /* Add "v" to the constant term of "aff".
824 * A NaN is unaffected by this operation.
826 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
827 __isl_take isl_val *v)
829 if (!aff || !v)
830 goto error;
832 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
833 isl_val_free(v);
834 return aff;
837 if (!isl_val_is_rat(v))
838 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
839 "expecting rational value", goto error);
841 aff = isl_aff_cow(aff);
842 if (!aff)
843 goto error;
845 aff->v = isl_vec_cow(aff->v);
846 if (!aff->v)
847 goto error;
849 if (isl_int_is_one(v->d)) {
850 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
851 } else if (isl_int_eq(aff->v->el[0], v->d)) {
852 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
853 aff->v = isl_vec_normalize(aff->v);
854 if (!aff->v)
855 goto error;
856 } else {
857 isl_seq_scale(aff->v->el + 1,
858 aff->v->el + 1, v->d, aff->v->size - 1);
859 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
860 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
861 aff->v = isl_vec_normalize(aff->v);
862 if (!aff->v)
863 goto error;
866 isl_val_free(v);
867 return aff;
868 error:
869 isl_aff_free(aff);
870 isl_val_free(v);
871 return NULL;
874 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
876 isl_int t;
878 isl_int_init(t);
879 isl_int_set_si(t, v);
880 aff = isl_aff_add_constant(aff, t);
881 isl_int_clear(t);
883 return aff;
886 /* Add "v" to the numerator of the constant term of "aff".
888 * A NaN is unaffected by this operation.
890 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
892 if (isl_int_is_zero(v))
893 return aff;
895 if (!aff)
896 return NULL;
897 if (isl_aff_is_nan(aff))
898 return aff;
899 aff = isl_aff_cow(aff);
900 if (!aff)
901 return NULL;
903 aff->v = isl_vec_cow(aff->v);
904 if (!aff->v)
905 return isl_aff_free(aff);
907 isl_int_add(aff->v->el[1], aff->v->el[1], v);
909 return aff;
912 /* Add "v" to the numerator of the constant term of "aff".
914 * A NaN is unaffected by this operation.
916 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
918 isl_int t;
920 if (v == 0)
921 return aff;
923 isl_int_init(t);
924 isl_int_set_si(t, v);
925 aff = isl_aff_add_constant_num(aff, t);
926 isl_int_clear(t);
928 return aff;
931 /* Replace the numerator of the constant term of "aff" by "v".
933 * A NaN is unaffected by this operation.
935 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
937 if (!aff)
938 return NULL;
939 if (isl_aff_is_nan(aff))
940 return aff;
941 aff = isl_aff_cow(aff);
942 if (!aff)
943 return NULL;
945 aff->v = isl_vec_cow(aff->v);
946 if (!aff->v)
947 return isl_aff_free(aff);
949 isl_int_set_si(aff->v->el[1], v);
951 return aff;
954 /* Replace the numerator of the coefficient of the variable of type "type"
955 * at position "pos" of "aff" by "v".
957 * A NaN is unaffected by this operation.
959 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
960 enum isl_dim_type type, int pos, isl_int v)
962 if (!aff)
963 return NULL;
965 if (type == isl_dim_out)
966 isl_die(aff->v->ctx, isl_error_invalid,
967 "output/set dimension does not have a coefficient",
968 return isl_aff_free(aff));
969 if (type == isl_dim_in)
970 type = isl_dim_set;
972 if (pos >= isl_local_space_dim(aff->ls, type))
973 isl_die(aff->v->ctx, isl_error_invalid,
974 "position out of bounds", return isl_aff_free(aff));
976 if (isl_aff_is_nan(aff))
977 return aff;
978 aff = isl_aff_cow(aff);
979 if (!aff)
980 return NULL;
982 aff->v = isl_vec_cow(aff->v);
983 if (!aff->v)
984 return isl_aff_free(aff);
986 pos += isl_local_space_offset(aff->ls, type);
987 isl_int_set(aff->v->el[1 + pos], v);
989 return aff;
992 /* Replace the numerator of the coefficient of the variable of type "type"
993 * at position "pos" of "aff" by "v".
995 * A NaN is unaffected by this operation.
997 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
998 enum isl_dim_type type, int pos, int v)
1000 if (!aff)
1001 return NULL;
1003 if (type == isl_dim_out)
1004 isl_die(aff->v->ctx, isl_error_invalid,
1005 "output/set dimension does not have a coefficient",
1006 return isl_aff_free(aff));
1007 if (type == isl_dim_in)
1008 type = isl_dim_set;
1010 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1011 isl_die(aff->v->ctx, isl_error_invalid,
1012 "position out of bounds", return isl_aff_free(aff));
1014 if (isl_aff_is_nan(aff))
1015 return aff;
1016 pos += isl_local_space_offset(aff->ls, type);
1017 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1018 return aff;
1020 aff = isl_aff_cow(aff);
1021 if (!aff)
1022 return NULL;
1024 aff->v = isl_vec_cow(aff->v);
1025 if (!aff->v)
1026 return isl_aff_free(aff);
1028 isl_int_set_si(aff->v->el[1 + pos], v);
1030 return aff;
1033 /* Replace the coefficient of the variable of type "type" at position "pos"
1034 * of "aff" by "v".
1036 * A NaN is unaffected by this operation.
1038 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1039 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1041 if (!aff || !v)
1042 goto error;
1044 if (type == isl_dim_out)
1045 isl_die(aff->v->ctx, isl_error_invalid,
1046 "output/set dimension does not have a coefficient",
1047 goto error);
1048 if (type == isl_dim_in)
1049 type = isl_dim_set;
1051 if (pos >= isl_local_space_dim(aff->ls, type))
1052 isl_die(aff->v->ctx, isl_error_invalid,
1053 "position out of bounds", goto error);
1055 if (isl_aff_is_nan(aff)) {
1056 isl_val_free(v);
1057 return aff;
1059 if (!isl_val_is_rat(v))
1060 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1061 "expecting rational value", goto error);
1063 pos += isl_local_space_offset(aff->ls, type);
1064 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1065 isl_int_eq(aff->v->el[0], v->d)) {
1066 isl_val_free(v);
1067 return aff;
1070 aff = isl_aff_cow(aff);
1071 if (!aff)
1072 goto error;
1073 aff->v = isl_vec_cow(aff->v);
1074 if (!aff->v)
1075 goto error;
1077 if (isl_int_eq(aff->v->el[0], v->d)) {
1078 isl_int_set(aff->v->el[1 + pos], v->n);
1079 } else if (isl_int_is_one(v->d)) {
1080 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1081 } else {
1082 isl_seq_scale(aff->v->el + 1,
1083 aff->v->el + 1, v->d, aff->v->size - 1);
1084 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1085 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1086 aff->v = isl_vec_normalize(aff->v);
1087 if (!aff->v)
1088 goto error;
1091 isl_val_free(v);
1092 return aff;
1093 error:
1094 isl_aff_free(aff);
1095 isl_val_free(v);
1096 return NULL;
1099 /* Add "v" to the coefficient of the variable of type "type"
1100 * at position "pos" of "aff".
1102 * A NaN is unaffected by this operation.
1104 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1105 enum isl_dim_type type, int pos, isl_int v)
1107 if (!aff)
1108 return NULL;
1110 if (type == isl_dim_out)
1111 isl_die(aff->v->ctx, isl_error_invalid,
1112 "output/set dimension does not have a coefficient",
1113 return isl_aff_free(aff));
1114 if (type == isl_dim_in)
1115 type = isl_dim_set;
1117 if (pos >= isl_local_space_dim(aff->ls, type))
1118 isl_die(aff->v->ctx, isl_error_invalid,
1119 "position out of bounds", return isl_aff_free(aff));
1121 if (isl_aff_is_nan(aff))
1122 return aff;
1123 aff = isl_aff_cow(aff);
1124 if (!aff)
1125 return NULL;
1127 aff->v = isl_vec_cow(aff->v);
1128 if (!aff->v)
1129 return isl_aff_free(aff);
1131 pos += isl_local_space_offset(aff->ls, type);
1132 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1134 return aff;
1137 /* Add "v" to the coefficient of the variable of type "type"
1138 * at position "pos" of "aff".
1140 * A NaN is unaffected by this operation.
1142 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1143 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1145 if (!aff || !v)
1146 goto error;
1148 if (isl_val_is_zero(v)) {
1149 isl_val_free(v);
1150 return aff;
1153 if (type == isl_dim_out)
1154 isl_die(aff->v->ctx, isl_error_invalid,
1155 "output/set dimension does not have a coefficient",
1156 goto error);
1157 if (type == isl_dim_in)
1158 type = isl_dim_set;
1160 if (pos >= isl_local_space_dim(aff->ls, type))
1161 isl_die(aff->v->ctx, isl_error_invalid,
1162 "position out of bounds", goto error);
1164 if (isl_aff_is_nan(aff)) {
1165 isl_val_free(v);
1166 return aff;
1168 if (!isl_val_is_rat(v))
1169 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1170 "expecting rational value", goto error);
1172 aff = isl_aff_cow(aff);
1173 if (!aff)
1174 goto error;
1176 aff->v = isl_vec_cow(aff->v);
1177 if (!aff->v)
1178 goto error;
1180 pos += isl_local_space_offset(aff->ls, type);
1181 if (isl_int_is_one(v->d)) {
1182 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1183 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1184 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1185 aff->v = isl_vec_normalize(aff->v);
1186 if (!aff->v)
1187 goto error;
1188 } else {
1189 isl_seq_scale(aff->v->el + 1,
1190 aff->v->el + 1, v->d, aff->v->size - 1);
1191 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1192 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1193 aff->v = isl_vec_normalize(aff->v);
1194 if (!aff->v)
1195 goto error;
1198 isl_val_free(v);
1199 return aff;
1200 error:
1201 isl_aff_free(aff);
1202 isl_val_free(v);
1203 return NULL;
1206 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1207 enum isl_dim_type type, int pos, int v)
1209 isl_int t;
1211 isl_int_init(t);
1212 isl_int_set_si(t, v);
1213 aff = isl_aff_add_coefficient(aff, type, pos, t);
1214 isl_int_clear(t);
1216 return aff;
1219 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1221 if (!aff)
1222 return NULL;
1224 return isl_local_space_get_div(aff->ls, pos);
1227 /* Return the negation of "aff".
1229 * As a special case, -NaN = NaN.
1231 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1233 if (!aff)
1234 return NULL;
1235 if (isl_aff_is_nan(aff))
1236 return aff;
1237 aff = isl_aff_cow(aff);
1238 if (!aff)
1239 return NULL;
1240 aff->v = isl_vec_cow(aff->v);
1241 if (!aff->v)
1242 return isl_aff_free(aff);
1244 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1246 return aff;
1249 /* Remove divs from the local space that do not appear in the affine
1250 * expression.
1251 * We currently only remove divs at the end.
1252 * Some intermediate divs may also not appear directly in the affine
1253 * expression, but we would also need to check that no other divs are
1254 * defined in terms of them.
1256 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1258 int pos;
1259 int off;
1260 int n;
1262 if (!aff)
1263 return NULL;
1265 n = isl_local_space_dim(aff->ls, isl_dim_div);
1266 off = isl_local_space_offset(aff->ls, isl_dim_div);
1268 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1269 if (pos == n)
1270 return aff;
1272 aff = isl_aff_cow(aff);
1273 if (!aff)
1274 return NULL;
1276 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1277 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1278 if (!aff->ls || !aff->v)
1279 return isl_aff_free(aff);
1281 return aff;
1284 /* Look for any divs in the aff->ls with a denominator equal to one
1285 * and plug them into the affine expression and any subsequent divs
1286 * that may reference the div.
1288 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1290 int i, n;
1291 int len;
1292 isl_int v;
1293 isl_vec *vec;
1294 isl_local_space *ls;
1295 unsigned pos;
1297 if (!aff)
1298 return NULL;
1300 n = isl_local_space_dim(aff->ls, isl_dim_div);
1301 len = aff->v->size;
1302 for (i = 0; i < n; ++i) {
1303 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1304 continue;
1305 ls = isl_local_space_copy(aff->ls);
1306 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1307 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1308 vec = isl_vec_copy(aff->v);
1309 vec = isl_vec_cow(vec);
1310 if (!ls || !vec)
1311 goto error;
1313 isl_int_init(v);
1315 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1316 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1317 len, len, v);
1319 isl_int_clear(v);
1321 isl_vec_free(aff->v);
1322 aff->v = vec;
1323 isl_local_space_free(aff->ls);
1324 aff->ls = ls;
1327 return aff;
1328 error:
1329 isl_vec_free(vec);
1330 isl_local_space_free(ls);
1331 return isl_aff_free(aff);
1334 /* Look for any divs j that appear with a unit coefficient inside
1335 * the definitions of other divs i and plug them into the definitions
1336 * of the divs i.
1338 * In particular, an expression of the form
1340 * floor((f(..) + floor(g(..)/n))/m)
1342 * is simplified to
1344 * floor((n * f(..) + g(..))/(n * m))
1346 * This simplification is correct because we can move the expression
1347 * f(..) into the inner floor in the original expression to obtain
1349 * floor(floor((n * f(..) + g(..))/n)/m)
1351 * from which we can derive the simplified expression.
1353 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1355 int i, j, n;
1356 int off;
1358 if (!aff)
1359 return NULL;
1361 n = isl_local_space_dim(aff->ls, isl_dim_div);
1362 off = isl_local_space_offset(aff->ls, isl_dim_div);
1363 for (i = 1; i < n; ++i) {
1364 for (j = 0; j < i; ++j) {
1365 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1366 continue;
1367 aff->ls = isl_local_space_substitute_seq(aff->ls,
1368 isl_dim_div, j, aff->ls->div->row[j],
1369 aff->v->size, i, 1);
1370 if (!aff->ls)
1371 return isl_aff_free(aff);
1375 return aff;
1378 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1380 * Even though this function is only called on isl_affs with a single
1381 * reference, we are careful to only change aff->v and aff->ls together.
1383 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1385 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1386 isl_local_space *ls;
1387 isl_vec *v;
1389 ls = isl_local_space_copy(aff->ls);
1390 ls = isl_local_space_swap_div(ls, a, b);
1391 v = isl_vec_copy(aff->v);
1392 v = isl_vec_cow(v);
1393 if (!ls || !v)
1394 goto error;
1396 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1397 isl_vec_free(aff->v);
1398 aff->v = v;
1399 isl_local_space_free(aff->ls);
1400 aff->ls = ls;
1402 return aff;
1403 error:
1404 isl_vec_free(v);
1405 isl_local_space_free(ls);
1406 return isl_aff_free(aff);
1409 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1411 * We currently do not actually remove div "b", but simply add its
1412 * coefficient to that of "a" and then zero it out.
1414 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1416 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1418 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1419 return aff;
1421 aff->v = isl_vec_cow(aff->v);
1422 if (!aff->v)
1423 return isl_aff_free(aff);
1425 isl_int_add(aff->v->el[1 + off + a],
1426 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1427 isl_int_set_si(aff->v->el[1 + off + b], 0);
1429 return aff;
1432 /* Sort the divs in the local space of "aff" according to
1433 * the comparison function "cmp_row" in isl_local_space.c,
1434 * combining the coefficients of identical divs.
1436 * Reordering divs does not change the semantics of "aff",
1437 * so there is no need to call isl_aff_cow.
1438 * Moreover, this function is currently only called on isl_affs
1439 * with a single reference.
1441 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1443 int i, j, n;
1445 if (!aff)
1446 return NULL;
1448 n = isl_aff_dim(aff, isl_dim_div);
1449 for (i = 1; i < n; ++i) {
1450 for (j = i - 1; j >= 0; --j) {
1451 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1452 if (cmp < 0)
1453 break;
1454 if (cmp == 0)
1455 aff = merge_divs(aff, j, j + 1);
1456 else
1457 aff = swap_div(aff, j, j + 1);
1458 if (!aff)
1459 return NULL;
1463 return aff;
1466 /* Normalize the representation of "aff".
1468 * This function should only be called of "new" isl_affs, i.e.,
1469 * with only a single reference. We therefore do not need to
1470 * worry about affecting other instances.
1472 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1474 if (!aff)
1475 return NULL;
1476 aff->v = isl_vec_normalize(aff->v);
1477 if (!aff->v)
1478 return isl_aff_free(aff);
1479 aff = plug_in_integral_divs(aff);
1480 aff = plug_in_unit_divs(aff);
1481 aff = sort_divs(aff);
1482 aff = isl_aff_remove_unused_divs(aff);
1483 return aff;
1486 /* Given f, return floor(f).
1487 * If f is an integer expression, then just return f.
1488 * If f is a constant, then return the constant floor(f).
1489 * Otherwise, if f = g/m, write g = q m + r,
1490 * create a new div d = [r/m] and return the expression q + d.
1491 * The coefficients in r are taken to lie between -m/2 and m/2.
1493 * reduce_div_coefficients performs the same normalization.
1495 * As a special case, floor(NaN) = NaN.
1497 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1499 int i;
1500 int size;
1501 isl_ctx *ctx;
1502 isl_vec *div;
1504 if (!aff)
1505 return NULL;
1507 if (isl_aff_is_nan(aff))
1508 return aff;
1509 if (isl_int_is_one(aff->v->el[0]))
1510 return aff;
1512 aff = isl_aff_cow(aff);
1513 if (!aff)
1514 return NULL;
1516 aff->v = isl_vec_cow(aff->v);
1517 if (!aff->v)
1518 return isl_aff_free(aff);
1520 if (isl_aff_is_cst(aff)) {
1521 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1522 isl_int_set_si(aff->v->el[0], 1);
1523 return aff;
1526 div = isl_vec_copy(aff->v);
1527 div = isl_vec_cow(div);
1528 if (!div)
1529 return isl_aff_free(aff);
1531 ctx = isl_aff_get_ctx(aff);
1532 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1533 for (i = 1; i < aff->v->size; ++i) {
1534 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1535 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1536 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1537 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1538 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1542 aff->ls = isl_local_space_add_div(aff->ls, div);
1543 if (!aff->ls)
1544 return isl_aff_free(aff);
1546 size = aff->v->size;
1547 aff->v = isl_vec_extend(aff->v, size + 1);
1548 if (!aff->v)
1549 return isl_aff_free(aff);
1550 isl_int_set_si(aff->v->el[0], 1);
1551 isl_int_set_si(aff->v->el[size], 1);
1553 aff = isl_aff_normalize(aff);
1555 return aff;
1558 /* Compute
1560 * aff mod m = aff - m * floor(aff/m)
1562 * with m an integer value.
1564 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1565 __isl_take isl_val *m)
1567 isl_aff *res;
1569 if (!aff || !m)
1570 goto error;
1572 if (!isl_val_is_int(m))
1573 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1574 "expecting integer modulo", goto error);
1576 res = isl_aff_copy(aff);
1577 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1578 aff = isl_aff_floor(aff);
1579 aff = isl_aff_scale_val(aff, m);
1580 res = isl_aff_sub(res, aff);
1582 return res;
1583 error:
1584 isl_aff_free(aff);
1585 isl_val_free(m);
1586 return NULL;
1589 /* Compute
1591 * pwaff mod m = pwaff - m * floor(pwaff/m)
1593 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1595 isl_pw_aff *res;
1597 res = isl_pw_aff_copy(pwaff);
1598 pwaff = isl_pw_aff_scale_down(pwaff, m);
1599 pwaff = isl_pw_aff_floor(pwaff);
1600 pwaff = isl_pw_aff_scale(pwaff, m);
1601 res = isl_pw_aff_sub(res, pwaff);
1603 return res;
1606 /* Compute
1608 * pa mod m = pa - m * floor(pa/m)
1610 * with m an integer value.
1612 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1613 __isl_take isl_val *m)
1615 if (!pa || !m)
1616 goto error;
1617 if (!isl_val_is_int(m))
1618 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1619 "expecting integer modulo", goto error);
1620 pa = isl_pw_aff_mod(pa, m->n);
1621 isl_val_free(m);
1622 return pa;
1623 error:
1624 isl_pw_aff_free(pa);
1625 isl_val_free(m);
1626 return NULL;
1629 /* Given f, return ceil(f).
1630 * If f is an integer expression, then just return f.
1631 * Otherwise, let f be the expression
1633 * e/m
1635 * then return
1637 * floor((e + m - 1)/m)
1639 * As a special case, ceil(NaN) = NaN.
1641 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1643 if (!aff)
1644 return NULL;
1646 if (isl_aff_is_nan(aff))
1647 return aff;
1648 if (isl_int_is_one(aff->v->el[0]))
1649 return aff;
1651 aff = isl_aff_cow(aff);
1652 if (!aff)
1653 return NULL;
1654 aff->v = isl_vec_cow(aff->v);
1655 if (!aff->v)
1656 return isl_aff_free(aff);
1658 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1659 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1660 aff = isl_aff_floor(aff);
1662 return aff;
1665 /* Apply the expansion computed by isl_merge_divs.
1666 * The expansion itself is given by "exp" while the resulting
1667 * list of divs is given by "div".
1669 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1670 __isl_take isl_mat *div, int *exp)
1672 int old_n_div;
1673 int new_n_div;
1674 int offset;
1676 aff = isl_aff_cow(aff);
1677 if (!aff || !div)
1678 goto error;
1680 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1681 new_n_div = isl_mat_rows(div);
1682 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1684 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1685 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1686 if (!aff->v || !aff->ls)
1687 return isl_aff_free(aff);
1688 return aff;
1689 error:
1690 isl_aff_free(aff);
1691 isl_mat_free(div);
1692 return NULL;
1695 /* Add two affine expressions that live in the same local space.
1697 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1698 __isl_take isl_aff *aff2)
1700 isl_int gcd, f;
1702 aff1 = isl_aff_cow(aff1);
1703 if (!aff1 || !aff2)
1704 goto error;
1706 aff1->v = isl_vec_cow(aff1->v);
1707 if (!aff1->v)
1708 goto error;
1710 isl_int_init(gcd);
1711 isl_int_init(f);
1712 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1713 isl_int_divexact(f, aff2->v->el[0], gcd);
1714 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1715 isl_int_divexact(f, aff1->v->el[0], gcd);
1716 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1717 isl_int_divexact(f, aff2->v->el[0], gcd);
1718 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1719 isl_int_clear(f);
1720 isl_int_clear(gcd);
1722 isl_aff_free(aff2);
1723 return aff1;
1724 error:
1725 isl_aff_free(aff1);
1726 isl_aff_free(aff2);
1727 return NULL;
1730 /* Return the sum of "aff1" and "aff2".
1732 * If either of the two is NaN, then the result is NaN.
1734 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1735 __isl_take isl_aff *aff2)
1737 isl_ctx *ctx;
1738 int *exp1 = NULL;
1739 int *exp2 = NULL;
1740 isl_mat *div;
1741 int n_div1, n_div2;
1743 if (!aff1 || !aff2)
1744 goto error;
1746 ctx = isl_aff_get_ctx(aff1);
1747 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1748 isl_die(ctx, isl_error_invalid,
1749 "spaces don't match", goto error);
1751 if (isl_aff_is_nan(aff1)) {
1752 isl_aff_free(aff2);
1753 return aff1;
1755 if (isl_aff_is_nan(aff2)) {
1756 isl_aff_free(aff1);
1757 return aff2;
1760 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1761 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1762 if (n_div1 == 0 && n_div2 == 0)
1763 return add_expanded(aff1, aff2);
1765 exp1 = isl_alloc_array(ctx, int, n_div1);
1766 exp2 = isl_alloc_array(ctx, int, n_div2);
1767 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1768 goto error;
1770 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1771 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1772 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1773 free(exp1);
1774 free(exp2);
1776 return add_expanded(aff1, aff2);
1777 error:
1778 free(exp1);
1779 free(exp2);
1780 isl_aff_free(aff1);
1781 isl_aff_free(aff2);
1782 return NULL;
1785 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1786 __isl_take isl_aff *aff2)
1788 return isl_aff_add(aff1, isl_aff_neg(aff2));
1791 /* Return the result of scaling "aff" by a factor of "f".
1793 * As a special case, f * NaN = NaN.
1795 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1797 isl_int gcd;
1799 if (!aff)
1800 return NULL;
1801 if (isl_aff_is_nan(aff))
1802 return aff;
1804 if (isl_int_is_one(f))
1805 return aff;
1807 aff = isl_aff_cow(aff);
1808 if (!aff)
1809 return NULL;
1810 aff->v = isl_vec_cow(aff->v);
1811 if (!aff->v)
1812 return isl_aff_free(aff);
1814 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1815 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1816 return aff;
1819 isl_int_init(gcd);
1820 isl_int_gcd(gcd, aff->v->el[0], f);
1821 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1822 isl_int_divexact(gcd, f, gcd);
1823 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1824 isl_int_clear(gcd);
1826 return aff;
1829 /* Multiple "aff" by "v".
1831 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1832 __isl_take isl_val *v)
1834 if (!aff || !v)
1835 goto error;
1837 if (isl_val_is_one(v)) {
1838 isl_val_free(v);
1839 return aff;
1842 if (!isl_val_is_rat(v))
1843 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1844 "expecting rational factor", goto error);
1846 aff = isl_aff_scale(aff, v->n);
1847 aff = isl_aff_scale_down(aff, v->d);
1849 isl_val_free(v);
1850 return aff;
1851 error:
1852 isl_aff_free(aff);
1853 isl_val_free(v);
1854 return NULL;
1857 /* Return the result of scaling "aff" down by a factor of "f".
1859 * As a special case, NaN/f = NaN.
1861 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1863 isl_int gcd;
1865 if (!aff)
1866 return NULL;
1867 if (isl_aff_is_nan(aff))
1868 return aff;
1870 if (isl_int_is_one(f))
1871 return aff;
1873 aff = isl_aff_cow(aff);
1874 if (!aff)
1875 return NULL;
1877 if (isl_int_is_zero(f))
1878 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1879 "cannot scale down by zero", return isl_aff_free(aff));
1881 aff->v = isl_vec_cow(aff->v);
1882 if (!aff->v)
1883 return isl_aff_free(aff);
1885 isl_int_init(gcd);
1886 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1887 isl_int_gcd(gcd, gcd, f);
1888 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1889 isl_int_divexact(gcd, f, gcd);
1890 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1891 isl_int_clear(gcd);
1893 return aff;
1896 /* Divide "aff" by "v".
1898 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1899 __isl_take isl_val *v)
1901 if (!aff || !v)
1902 goto error;
1904 if (isl_val_is_one(v)) {
1905 isl_val_free(v);
1906 return aff;
1909 if (!isl_val_is_rat(v))
1910 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1911 "expecting rational factor", goto error);
1912 if (!isl_val_is_pos(v))
1913 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1914 "factor needs to be positive", goto error);
1916 aff = isl_aff_scale(aff, v->d);
1917 aff = isl_aff_scale_down(aff, v->n);
1919 isl_val_free(v);
1920 return aff;
1921 error:
1922 isl_aff_free(aff);
1923 isl_val_free(v);
1924 return NULL;
1927 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1929 isl_int v;
1931 if (f == 1)
1932 return aff;
1934 isl_int_init(v);
1935 isl_int_set_ui(v, f);
1936 aff = isl_aff_scale_down(aff, v);
1937 isl_int_clear(v);
1939 return aff;
1942 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1943 enum isl_dim_type type, unsigned pos, const char *s)
1945 aff = isl_aff_cow(aff);
1946 if (!aff)
1947 return NULL;
1948 if (type == isl_dim_out)
1949 isl_die(aff->v->ctx, isl_error_invalid,
1950 "cannot set name of output/set dimension",
1951 return isl_aff_free(aff));
1952 if (type == isl_dim_in)
1953 type = isl_dim_set;
1954 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1955 if (!aff->ls)
1956 return isl_aff_free(aff);
1958 return aff;
1961 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1962 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1964 aff = isl_aff_cow(aff);
1965 if (!aff)
1966 goto error;
1967 if (type == isl_dim_out)
1968 isl_die(aff->v->ctx, isl_error_invalid,
1969 "cannot set name of output/set dimension",
1970 goto error);
1971 if (type == isl_dim_in)
1972 type = isl_dim_set;
1973 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1974 if (!aff->ls)
1975 return isl_aff_free(aff);
1977 return aff;
1978 error:
1979 isl_id_free(id);
1980 isl_aff_free(aff);
1981 return NULL;
1984 /* Replace the identifier of the input tuple of "aff" by "id".
1985 * type is currently required to be equal to isl_dim_in
1987 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1988 enum isl_dim_type type, __isl_take isl_id *id)
1990 aff = isl_aff_cow(aff);
1991 if (!aff)
1992 goto error;
1993 if (type != isl_dim_out)
1994 isl_die(aff->v->ctx, isl_error_invalid,
1995 "cannot only set id of input tuple", goto error);
1996 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
1997 if (!aff->ls)
1998 return isl_aff_free(aff);
2000 return aff;
2001 error:
2002 isl_id_free(id);
2003 isl_aff_free(aff);
2004 return NULL;
2007 /* Exploit the equalities in "eq" to simplify the affine expression
2008 * and the expressions of the integer divisions in the local space.
2009 * The integer divisions in this local space are assumed to appear
2010 * as regular dimensions in "eq".
2012 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2013 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2015 int i, j;
2016 unsigned total;
2017 unsigned n_div;
2019 if (!eq)
2020 goto error;
2021 if (eq->n_eq == 0) {
2022 isl_basic_set_free(eq);
2023 return aff;
2026 aff = isl_aff_cow(aff);
2027 if (!aff)
2028 goto error;
2030 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2031 isl_basic_set_copy(eq));
2032 aff->v = isl_vec_cow(aff->v);
2033 if (!aff->ls || !aff->v)
2034 goto error;
2036 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2037 n_div = eq->n_div;
2038 for (i = 0; i < eq->n_eq; ++i) {
2039 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2040 if (j < 0 || j == 0 || j >= total)
2041 continue;
2043 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2044 &aff->v->el[0]);
2047 isl_basic_set_free(eq);
2048 aff = isl_aff_normalize(aff);
2049 return aff;
2050 error:
2051 isl_basic_set_free(eq);
2052 isl_aff_free(aff);
2053 return NULL;
2056 /* Exploit the equalities in "eq" to simplify the affine expression
2057 * and the expressions of the integer divisions in the local space.
2059 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2060 __isl_take isl_basic_set *eq)
2062 int n_div;
2064 if (!aff || !eq)
2065 goto error;
2066 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2067 if (n_div > 0)
2068 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2069 return isl_aff_substitute_equalities_lifted(aff, eq);
2070 error:
2071 isl_basic_set_free(eq);
2072 isl_aff_free(aff);
2073 return NULL;
2076 /* Look for equalities among the variables shared by context and aff
2077 * and the integer divisions of aff, if any.
2078 * The equalities are then used to eliminate coefficients and/or integer
2079 * divisions from aff.
2081 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2082 __isl_take isl_set *context)
2084 isl_basic_set *hull;
2085 int n_div;
2087 if (!aff)
2088 goto error;
2089 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2090 if (n_div > 0) {
2091 isl_basic_set *bset;
2092 isl_local_space *ls;
2093 context = isl_set_add_dims(context, isl_dim_set, n_div);
2094 ls = isl_aff_get_domain_local_space(aff);
2095 bset = isl_basic_set_from_local_space(ls);
2096 bset = isl_basic_set_lift(bset);
2097 bset = isl_basic_set_flatten(bset);
2098 context = isl_set_intersect(context,
2099 isl_set_from_basic_set(bset));
2102 hull = isl_set_affine_hull(context);
2103 return isl_aff_substitute_equalities_lifted(aff, hull);
2104 error:
2105 isl_aff_free(aff);
2106 isl_set_free(context);
2107 return NULL;
2110 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2111 __isl_take isl_set *context)
2113 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2114 dom_context = isl_set_intersect_params(dom_context, context);
2115 return isl_aff_gist(aff, dom_context);
2118 /* Return a basic set containing those elements in the space
2119 * of aff where it is positive. "rational" should not be set.
2121 * If "aff" is NaN, then it is not positive.
2123 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2124 int rational)
2126 isl_constraint *ineq;
2127 isl_basic_set *bset;
2128 isl_val *c;
2130 if (!aff)
2131 return NULL;
2132 if (isl_aff_is_nan(aff)) {
2133 isl_space *space = isl_aff_get_domain_space(aff);
2134 isl_aff_free(aff);
2135 return isl_basic_set_empty(space);
2137 if (rational)
2138 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2139 "rational sets not supported", goto error);
2141 ineq = isl_inequality_from_aff(aff);
2142 c = isl_constraint_get_constant_val(ineq);
2143 c = isl_val_sub_ui(c, 1);
2144 ineq = isl_constraint_set_constant_val(ineq, c);
2146 bset = isl_basic_set_from_constraint(ineq);
2147 bset = isl_basic_set_simplify(bset);
2148 return bset;
2149 error:
2150 isl_aff_free(aff);
2151 return NULL;
2154 /* Return a basic set containing those elements in the space
2155 * of aff where it is non-negative.
2156 * If "rational" is set, then return a rational basic set.
2158 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2160 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2161 __isl_take isl_aff *aff, int rational)
2163 isl_constraint *ineq;
2164 isl_basic_set *bset;
2166 if (!aff)
2167 return NULL;
2168 if (isl_aff_is_nan(aff)) {
2169 isl_space *space = isl_aff_get_domain_space(aff);
2170 isl_aff_free(aff);
2171 return isl_basic_set_empty(space);
2174 ineq = isl_inequality_from_aff(aff);
2176 bset = isl_basic_set_from_constraint(ineq);
2177 if (rational)
2178 bset = isl_basic_set_set_rational(bset);
2179 bset = isl_basic_set_simplify(bset);
2180 return bset;
2183 /* Return a basic set containing those elements in the space
2184 * of aff where it is non-negative.
2186 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2188 return aff_nonneg_basic_set(aff, 0);
2191 /* Return a basic set containing those elements in the domain space
2192 * of "aff" where it is positive.
2194 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2196 aff = isl_aff_add_constant_num_si(aff, -1);
2197 return isl_aff_nonneg_basic_set(aff);
2200 /* Return a basic set containing those elements in the domain space
2201 * of aff where it is negative.
2203 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2205 aff = isl_aff_neg(aff);
2206 return isl_aff_pos_basic_set(aff);
2209 /* Return a basic set containing those elements in the space
2210 * of aff where it is zero.
2211 * If "rational" is set, then return a rational basic set.
2213 * If "aff" is NaN, then it is not zero.
2215 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2216 int rational)
2218 isl_constraint *ineq;
2219 isl_basic_set *bset;
2221 if (!aff)
2222 return NULL;
2223 if (isl_aff_is_nan(aff)) {
2224 isl_space *space = isl_aff_get_domain_space(aff);
2225 isl_aff_free(aff);
2226 return isl_basic_set_empty(space);
2229 ineq = isl_equality_from_aff(aff);
2231 bset = isl_basic_set_from_constraint(ineq);
2232 if (rational)
2233 bset = isl_basic_set_set_rational(bset);
2234 bset = isl_basic_set_simplify(bset);
2235 return bset;
2238 /* Return a basic set containing those elements in the space
2239 * of aff where it is zero.
2241 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2243 return aff_zero_basic_set(aff, 0);
2246 /* Return a basic set containing those elements in the shared space
2247 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2249 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2250 __isl_take isl_aff *aff2)
2252 aff1 = isl_aff_sub(aff1, aff2);
2254 return isl_aff_nonneg_basic_set(aff1);
2257 /* Return a basic set containing those elements in the shared domain space
2258 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2260 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2261 __isl_take isl_aff *aff2)
2263 aff1 = isl_aff_sub(aff1, aff2);
2265 return isl_aff_pos_basic_set(aff1);
2268 /* Return a set containing those elements in the shared space
2269 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2271 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2272 __isl_take isl_aff *aff2)
2274 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2277 /* Return a set containing those elements in the shared domain space
2278 * of aff1 and aff2 where aff1 is greater than aff2.
2280 * If either of the two inputs is NaN, then the result is empty,
2281 * as comparisons with NaN always return false.
2283 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2284 __isl_take isl_aff *aff2)
2286 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2289 /* Return a basic set containing those elements in the shared space
2290 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2292 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2293 __isl_take isl_aff *aff2)
2295 return isl_aff_ge_basic_set(aff2, aff1);
2298 /* Return a basic set containing those elements in the shared domain space
2299 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2301 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2302 __isl_take isl_aff *aff2)
2304 return isl_aff_gt_basic_set(aff2, aff1);
2307 /* Return a set containing those elements in the shared space
2308 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2310 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2311 __isl_take isl_aff *aff2)
2313 return isl_aff_ge_set(aff2, aff1);
2316 /* Return a set containing those elements in the shared domain space
2317 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2319 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2320 __isl_take isl_aff *aff2)
2322 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2325 /* Return a basic set containing those elements in the shared space
2326 * of aff1 and aff2 where aff1 and aff2 are equal.
2328 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2329 __isl_take isl_aff *aff2)
2331 aff1 = isl_aff_sub(aff1, aff2);
2333 return isl_aff_zero_basic_set(aff1);
2336 /* Return a set containing those elements in the shared space
2337 * of aff1 and aff2 where aff1 and aff2 are equal.
2339 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2340 __isl_take isl_aff *aff2)
2342 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2345 /* Return a set containing those elements in the shared domain space
2346 * of aff1 and aff2 where aff1 and aff2 are not equal.
2348 * If either of the two inputs is NaN, then the result is empty,
2349 * as comparisons with NaN always return false.
2351 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2352 __isl_take isl_aff *aff2)
2354 isl_set *set_lt, *set_gt;
2356 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2357 isl_aff_copy(aff2));
2358 set_gt = isl_aff_gt_set(aff1, aff2);
2359 return isl_set_union_disjoint(set_lt, set_gt);
2362 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2363 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2365 aff1 = isl_aff_add(aff1, aff2);
2366 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2367 return aff1;
2370 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2372 if (!aff)
2373 return -1;
2375 return 0;
2378 /* Check whether the given affine expression has non-zero coefficient
2379 * for any dimension in the given range or if any of these dimensions
2380 * appear with non-zero coefficients in any of the integer divisions
2381 * involved in the affine expression.
2383 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2384 enum isl_dim_type type, unsigned first, unsigned n)
2386 int i;
2387 isl_ctx *ctx;
2388 int *active = NULL;
2389 isl_bool involves = isl_bool_false;
2391 if (!aff)
2392 return isl_bool_error;
2393 if (n == 0)
2394 return isl_bool_false;
2396 ctx = isl_aff_get_ctx(aff);
2397 if (first + n > isl_aff_dim(aff, type))
2398 isl_die(ctx, isl_error_invalid,
2399 "range out of bounds", return isl_bool_error);
2401 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2402 if (!active)
2403 goto error;
2405 first += isl_local_space_offset(aff->ls, type) - 1;
2406 for (i = 0; i < n; ++i)
2407 if (active[first + i]) {
2408 involves = isl_bool_true;
2409 break;
2412 free(active);
2414 return involves;
2415 error:
2416 free(active);
2417 return isl_bool_error;
2420 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2421 enum isl_dim_type type, unsigned first, unsigned n)
2423 isl_ctx *ctx;
2425 if (!aff)
2426 return NULL;
2427 if (type == isl_dim_out)
2428 isl_die(aff->v->ctx, isl_error_invalid,
2429 "cannot drop output/set dimension",
2430 return isl_aff_free(aff));
2431 if (type == isl_dim_in)
2432 type = isl_dim_set;
2433 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2434 return aff;
2436 ctx = isl_aff_get_ctx(aff);
2437 if (first + n > isl_local_space_dim(aff->ls, type))
2438 isl_die(ctx, isl_error_invalid, "range out of bounds",
2439 return isl_aff_free(aff));
2441 aff = isl_aff_cow(aff);
2442 if (!aff)
2443 return NULL;
2445 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2446 if (!aff->ls)
2447 return isl_aff_free(aff);
2449 first += 1 + isl_local_space_offset(aff->ls, type);
2450 aff->v = isl_vec_drop_els(aff->v, first, n);
2451 if (!aff->v)
2452 return isl_aff_free(aff);
2454 return aff;
2457 /* Project the domain of the affine expression onto its parameter space.
2458 * The affine expression may not involve any of the domain dimensions.
2460 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2462 isl_space *space;
2463 unsigned n;
2464 int involves;
2466 n = isl_aff_dim(aff, isl_dim_in);
2467 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2468 if (involves < 0)
2469 return isl_aff_free(aff);
2470 if (involves)
2471 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2472 "affine expression involves some of the domain dimensions",
2473 return isl_aff_free(aff));
2474 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2475 space = isl_aff_get_domain_space(aff);
2476 space = isl_space_params(space);
2477 aff = isl_aff_reset_domain_space(aff, space);
2478 return aff;
2481 /* Convert an affine expression defined over a parameter domain
2482 * into one that is defined over a zero-dimensional set.
2484 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2486 isl_local_space *ls;
2488 ls = isl_aff_take_domain_local_space(aff);
2489 ls = isl_local_space_set_from_params(ls);
2490 aff = isl_aff_restore_domain_local_space(aff, ls);
2492 return aff;
2495 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2496 enum isl_dim_type type, unsigned first, unsigned n)
2498 isl_ctx *ctx;
2500 if (!aff)
2501 return NULL;
2502 if (type == isl_dim_out)
2503 isl_die(aff->v->ctx, isl_error_invalid,
2504 "cannot insert output/set dimensions",
2505 return isl_aff_free(aff));
2506 if (type == isl_dim_in)
2507 type = isl_dim_set;
2508 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2509 return aff;
2511 ctx = isl_aff_get_ctx(aff);
2512 if (first > isl_local_space_dim(aff->ls, type))
2513 isl_die(ctx, isl_error_invalid, "position out of bounds",
2514 return isl_aff_free(aff));
2516 aff = isl_aff_cow(aff);
2517 if (!aff)
2518 return NULL;
2520 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2521 if (!aff->ls)
2522 return isl_aff_free(aff);
2524 first += 1 + isl_local_space_offset(aff->ls, type);
2525 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2526 if (!aff->v)
2527 return isl_aff_free(aff);
2529 return aff;
2532 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2533 enum isl_dim_type type, unsigned n)
2535 unsigned pos;
2537 pos = isl_aff_dim(aff, type);
2539 return isl_aff_insert_dims(aff, type, pos, n);
2542 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2543 enum isl_dim_type type, unsigned n)
2545 unsigned pos;
2547 pos = isl_pw_aff_dim(pwaff, type);
2549 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2552 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2553 * to dimensions of "dst_type" at "dst_pos".
2555 * We only support moving input dimensions to parameters and vice versa.
2557 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2558 enum isl_dim_type dst_type, unsigned dst_pos,
2559 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2561 unsigned g_dst_pos;
2562 unsigned g_src_pos;
2564 if (!aff)
2565 return NULL;
2566 if (n == 0 &&
2567 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2568 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2569 return aff;
2571 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2572 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2573 "cannot move output/set dimension",
2574 return isl_aff_free(aff));
2575 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2576 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2577 "cannot move divs", return isl_aff_free(aff));
2578 if (dst_type == isl_dim_in)
2579 dst_type = isl_dim_set;
2580 if (src_type == isl_dim_in)
2581 src_type = isl_dim_set;
2583 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2584 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2585 "range out of bounds", return isl_aff_free(aff));
2586 if (dst_type == src_type)
2587 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2588 "moving dims within the same type not supported",
2589 return isl_aff_free(aff));
2591 aff = isl_aff_cow(aff);
2592 if (!aff)
2593 return NULL;
2595 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2596 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2597 if (dst_type > src_type)
2598 g_dst_pos -= n;
2600 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2601 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2602 src_type, src_pos, n);
2603 if (!aff->v || !aff->ls)
2604 return isl_aff_free(aff);
2606 aff = sort_divs(aff);
2608 return aff;
2611 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2613 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2614 return isl_pw_aff_alloc(dom, aff);
2617 #define isl_aff_involves_nan isl_aff_is_nan
2619 #undef PW
2620 #define PW isl_pw_aff
2621 #undef EL
2622 #define EL isl_aff
2623 #undef EL_IS_ZERO
2624 #define EL_IS_ZERO is_empty
2625 #undef ZERO
2626 #define ZERO empty
2627 #undef IS_ZERO
2628 #define IS_ZERO is_empty
2629 #undef FIELD
2630 #define FIELD aff
2631 #undef DEFAULT_IS_ZERO
2632 #define DEFAULT_IS_ZERO 0
2634 #define NO_EVAL
2635 #define NO_OPT
2636 #define NO_LIFT
2637 #define NO_MORPH
2639 #include <isl_pw_templ.c>
2640 #include <isl_pw_hash.c>
2641 #include <isl_pw_union_opt.c>
2643 #undef UNION
2644 #define UNION isl_union_pw_aff
2645 #undef PART
2646 #define PART isl_pw_aff
2647 #undef PARTS
2648 #define PARTS pw_aff
2650 #include <isl_union_single.c>
2651 #include <isl_union_neg.c>
2653 static __isl_give isl_set *align_params_pw_pw_set_and(
2654 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2655 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2656 __isl_take isl_pw_aff *pwaff2))
2658 isl_bool equal_params;
2660 if (!pwaff1 || !pwaff2)
2661 goto error;
2662 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2663 if (equal_params < 0)
2664 goto error;
2665 if (equal_params)
2666 return fn(pwaff1, pwaff2);
2667 if (!isl_space_has_named_params(pwaff1->dim) ||
2668 !isl_space_has_named_params(pwaff2->dim))
2669 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2670 "unaligned unnamed parameters", goto error);
2671 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2672 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2673 return fn(pwaff1, pwaff2);
2674 error:
2675 isl_pw_aff_free(pwaff1);
2676 isl_pw_aff_free(pwaff2);
2677 return NULL;
2680 /* Align the parameters of the to isl_pw_aff arguments and
2681 * then apply a function "fn" on them that returns an isl_map.
2683 static __isl_give isl_map *align_params_pw_pw_map_and(
2684 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2685 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2686 __isl_take isl_pw_aff *pa2))
2688 isl_bool equal_params;
2690 if (!pa1 || !pa2)
2691 goto error;
2692 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2693 if (equal_params < 0)
2694 goto error;
2695 if (equal_params)
2696 return fn(pa1, pa2);
2697 if (!isl_space_has_named_params(pa1->dim) ||
2698 !isl_space_has_named_params(pa2->dim))
2699 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2700 "unaligned unnamed parameters", goto error);
2701 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2702 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2703 return fn(pa1, pa2);
2704 error:
2705 isl_pw_aff_free(pa1);
2706 isl_pw_aff_free(pa2);
2707 return NULL;
2710 /* Compute a piecewise quasi-affine expression with a domain that
2711 * is the union of those of pwaff1 and pwaff2 and such that on each
2712 * cell, the quasi-affine expression is the maximum of those of pwaff1
2713 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2714 * cell, then the associated expression is the defined one.
2716 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2717 __isl_take isl_pw_aff *pwaff2)
2719 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2722 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2723 __isl_take isl_pw_aff *pwaff2)
2725 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2726 &pw_aff_union_max);
2729 /* Compute a piecewise quasi-affine expression with a domain that
2730 * is the union of those of pwaff1 and pwaff2 and such that on each
2731 * cell, the quasi-affine expression is the minimum of those of pwaff1
2732 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2733 * cell, then the associated expression is the defined one.
2735 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2736 __isl_take isl_pw_aff *pwaff2)
2738 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2741 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2742 __isl_take isl_pw_aff *pwaff2)
2744 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2745 &pw_aff_union_min);
2748 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2749 __isl_take isl_pw_aff *pwaff2, int max)
2751 if (max)
2752 return isl_pw_aff_union_max(pwaff1, pwaff2);
2753 else
2754 return isl_pw_aff_union_min(pwaff1, pwaff2);
2757 /* Construct a map with as domain the domain of pwaff and
2758 * one-dimensional range corresponding to the affine expressions.
2760 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2762 int i;
2763 isl_space *dim;
2764 isl_map *map;
2766 if (!pwaff)
2767 return NULL;
2769 dim = isl_pw_aff_get_space(pwaff);
2770 map = isl_map_empty(dim);
2772 for (i = 0; i < pwaff->n; ++i) {
2773 isl_basic_map *bmap;
2774 isl_map *map_i;
2776 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2777 map_i = isl_map_from_basic_map(bmap);
2778 map_i = isl_map_intersect_domain(map_i,
2779 isl_set_copy(pwaff->p[i].set));
2780 map = isl_map_union_disjoint(map, map_i);
2783 isl_pw_aff_free(pwaff);
2785 return map;
2788 /* Construct a map with as domain the domain of pwaff and
2789 * one-dimensional range corresponding to the affine expressions.
2791 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2793 if (!pwaff)
2794 return NULL;
2795 if (isl_space_is_set(pwaff->dim))
2796 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2797 "space of input is not a map", goto error);
2798 return map_from_pw_aff(pwaff);
2799 error:
2800 isl_pw_aff_free(pwaff);
2801 return NULL;
2804 /* Construct a one-dimensional set with as parameter domain
2805 * the domain of pwaff and the single set dimension
2806 * corresponding to the affine expressions.
2808 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2810 if (!pwaff)
2811 return NULL;
2812 if (!isl_space_is_set(pwaff->dim))
2813 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2814 "space of input is not a set", goto error);
2815 return map_from_pw_aff(pwaff);
2816 error:
2817 isl_pw_aff_free(pwaff);
2818 return NULL;
2821 /* Return a set containing those elements in the domain
2822 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2823 * does not satisfy "fn" (if complement is 1).
2825 * The pieces with a NaN never belong to the result since
2826 * NaN does not satisfy any property.
2828 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2829 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2830 int complement)
2832 int i;
2833 isl_set *set;
2835 if (!pwaff)
2836 return NULL;
2838 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2840 for (i = 0; i < pwaff->n; ++i) {
2841 isl_basic_set *bset;
2842 isl_set *set_i, *locus;
2843 isl_bool rational;
2845 if (isl_aff_is_nan(pwaff->p[i].aff))
2846 continue;
2848 rational = isl_set_has_rational(pwaff->p[i].set);
2849 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2850 locus = isl_set_from_basic_set(bset);
2851 set_i = isl_set_copy(pwaff->p[i].set);
2852 if (complement)
2853 set_i = isl_set_subtract(set_i, locus);
2854 else
2855 set_i = isl_set_intersect(set_i, locus);
2856 set = isl_set_union_disjoint(set, set_i);
2859 isl_pw_aff_free(pwaff);
2861 return set;
2864 /* Return a set containing those elements in the domain
2865 * of "pa" where it is positive.
2867 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2869 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2872 /* Return a set containing those elements in the domain
2873 * of pwaff where it is non-negative.
2875 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2877 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2880 /* Return a set containing those elements in the domain
2881 * of pwaff where it is zero.
2883 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2885 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2888 /* Return a set containing those elements in the domain
2889 * of pwaff where it is not zero.
2891 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2893 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2896 /* Return a set containing those elements in the shared domain
2897 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2899 * We compute the difference on the shared domain and then construct
2900 * the set of values where this difference is non-negative.
2901 * If strict is set, we first subtract 1 from the difference.
2902 * If equal is set, we only return the elements where pwaff1 and pwaff2
2903 * are equal.
2905 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2906 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2908 isl_set *set1, *set2;
2910 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2911 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2912 set1 = isl_set_intersect(set1, set2);
2913 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2914 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2915 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2917 if (strict) {
2918 isl_space *dim = isl_set_get_space(set1);
2919 isl_aff *aff;
2920 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2921 aff = isl_aff_add_constant_si(aff, -1);
2922 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2923 } else
2924 isl_set_free(set1);
2926 if (equal)
2927 return isl_pw_aff_zero_set(pwaff1);
2928 return isl_pw_aff_nonneg_set(pwaff1);
2931 /* Return a set containing those elements in the shared domain
2932 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2934 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2935 __isl_take isl_pw_aff *pwaff2)
2937 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2940 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2941 __isl_take isl_pw_aff *pwaff2)
2943 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2946 /* Return a set containing those elements in the shared domain
2947 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2949 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2950 __isl_take isl_pw_aff *pwaff2)
2952 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2955 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2956 __isl_take isl_pw_aff *pwaff2)
2958 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2961 /* Return a set containing those elements in the shared domain
2962 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2964 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2965 __isl_take isl_pw_aff *pwaff2)
2967 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2970 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2971 __isl_take isl_pw_aff *pwaff2)
2973 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2976 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2977 __isl_take isl_pw_aff *pwaff2)
2979 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2982 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2983 __isl_take isl_pw_aff *pwaff2)
2985 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2988 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2989 * where the function values are ordered in the same way as "order",
2990 * which returns a set in the shared domain of its two arguments.
2991 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2993 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2994 * We first pull back the two functions such that they are defined on
2995 * the domain [A -> B]. Then we apply "order", resulting in a set
2996 * in the space [A -> B]. Finally, we unwrap this set to obtain
2997 * a map in the space A -> B.
2999 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3000 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3001 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3002 __isl_take isl_pw_aff *pa2))
3004 isl_space *space1, *space2;
3005 isl_multi_aff *ma;
3006 isl_set *set;
3008 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3009 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3010 space1 = isl_space_map_from_domain_and_range(space1, space2);
3011 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3012 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3013 ma = isl_multi_aff_range_map(space1);
3014 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3015 set = order(pa1, pa2);
3017 return isl_set_unwrap(set);
3020 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3021 * where the function values are equal.
3022 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3024 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3025 __isl_take isl_pw_aff *pa2)
3027 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3030 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3031 * where the function values are equal.
3033 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3034 __isl_take isl_pw_aff *pa2)
3036 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3039 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3040 * where the function value of "pa1" is less than the function value of "pa2".
3041 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3043 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3044 __isl_take isl_pw_aff *pa2)
3046 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3049 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3050 * where the function value of "pa1" is less than the function value of "pa2".
3052 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3053 __isl_take isl_pw_aff *pa2)
3055 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3058 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3059 * where the function value of "pa1" is greater than the function value
3060 * of "pa2".
3061 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3063 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3064 __isl_take isl_pw_aff *pa2)
3066 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3069 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3070 * where the function value of "pa1" is greater than the function value
3071 * of "pa2".
3073 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3074 __isl_take isl_pw_aff *pa2)
3076 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3079 /* Return a set containing those elements in the shared domain
3080 * of the elements of list1 and list2 where each element in list1
3081 * has the relation specified by "fn" with each element in list2.
3083 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3084 __isl_take isl_pw_aff_list *list2,
3085 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3086 __isl_take isl_pw_aff *pwaff2))
3088 int i, j;
3089 isl_ctx *ctx;
3090 isl_set *set;
3092 if (!list1 || !list2)
3093 goto error;
3095 ctx = isl_pw_aff_list_get_ctx(list1);
3096 if (list1->n < 1 || list2->n < 1)
3097 isl_die(ctx, isl_error_invalid,
3098 "list should contain at least one element", goto error);
3100 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3101 for (i = 0; i < list1->n; ++i)
3102 for (j = 0; j < list2->n; ++j) {
3103 isl_set *set_ij;
3105 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3106 isl_pw_aff_copy(list2->p[j]));
3107 set = isl_set_intersect(set, set_ij);
3110 isl_pw_aff_list_free(list1);
3111 isl_pw_aff_list_free(list2);
3112 return set;
3113 error:
3114 isl_pw_aff_list_free(list1);
3115 isl_pw_aff_list_free(list2);
3116 return NULL;
3119 /* Return a set containing those elements in the shared domain
3120 * of the elements of list1 and list2 where each element in list1
3121 * is equal to each element in list2.
3123 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3124 __isl_take isl_pw_aff_list *list2)
3126 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3129 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3130 __isl_take isl_pw_aff_list *list2)
3132 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3135 /* Return a set containing those elements in the shared domain
3136 * of the elements of list1 and list2 where each element in list1
3137 * is less than or equal to each element in list2.
3139 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3140 __isl_take isl_pw_aff_list *list2)
3142 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3145 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3146 __isl_take isl_pw_aff_list *list2)
3148 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3151 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3152 __isl_take isl_pw_aff_list *list2)
3154 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3157 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3158 __isl_take isl_pw_aff_list *list2)
3160 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3164 /* Return a set containing those elements in the shared domain
3165 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3167 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3168 __isl_take isl_pw_aff *pwaff2)
3170 isl_set *set_lt, *set_gt;
3172 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3173 isl_pw_aff_copy(pwaff2));
3174 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3175 return isl_set_union_disjoint(set_lt, set_gt);
3178 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3179 __isl_take isl_pw_aff *pwaff2)
3181 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3184 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3185 isl_int v)
3187 int i;
3189 if (isl_int_is_one(v))
3190 return pwaff;
3191 if (!isl_int_is_pos(v))
3192 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3193 "factor needs to be positive",
3194 return isl_pw_aff_free(pwaff));
3195 pwaff = isl_pw_aff_cow(pwaff);
3196 if (!pwaff)
3197 return NULL;
3198 if (pwaff->n == 0)
3199 return pwaff;
3201 for (i = 0; i < pwaff->n; ++i) {
3202 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3203 if (!pwaff->p[i].aff)
3204 return isl_pw_aff_free(pwaff);
3207 return pwaff;
3210 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3212 int i;
3214 pwaff = isl_pw_aff_cow(pwaff);
3215 if (!pwaff)
3216 return NULL;
3217 if (pwaff->n == 0)
3218 return pwaff;
3220 for (i = 0; i < pwaff->n; ++i) {
3221 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3222 if (!pwaff->p[i].aff)
3223 return isl_pw_aff_free(pwaff);
3226 return pwaff;
3229 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3231 int i;
3233 pwaff = isl_pw_aff_cow(pwaff);
3234 if (!pwaff)
3235 return NULL;
3236 if (pwaff->n == 0)
3237 return pwaff;
3239 for (i = 0; i < pwaff->n; ++i) {
3240 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3241 if (!pwaff->p[i].aff)
3242 return isl_pw_aff_free(pwaff);
3245 return pwaff;
3248 /* Assuming that "cond1" and "cond2" are disjoint,
3249 * return an affine expression that is equal to pwaff1 on cond1
3250 * and to pwaff2 on cond2.
3252 static __isl_give isl_pw_aff *isl_pw_aff_select(
3253 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3254 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3256 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3257 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3259 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3262 /* Return an affine expression that is equal to pwaff_true for elements
3263 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3264 * is zero.
3265 * That is, return cond ? pwaff_true : pwaff_false;
3267 * If "cond" involves and NaN, then we conservatively return a NaN
3268 * on its entire domain. In principle, we could consider the pieces
3269 * where it is NaN separately from those where it is not.
3271 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3272 * then only use the domain of "cond" to restrict the domain.
3274 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3275 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3277 isl_set *cond_true, *cond_false;
3278 isl_bool equal;
3280 if (!cond)
3281 goto error;
3282 if (isl_pw_aff_involves_nan(cond)) {
3283 isl_space *space = isl_pw_aff_get_domain_space(cond);
3284 isl_local_space *ls = isl_local_space_from_space(space);
3285 isl_pw_aff_free(cond);
3286 isl_pw_aff_free(pwaff_true);
3287 isl_pw_aff_free(pwaff_false);
3288 return isl_pw_aff_nan_on_domain(ls);
3291 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3292 isl_pw_aff_get_space(pwaff_false));
3293 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3294 isl_pw_aff_get_space(pwaff_true));
3295 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3296 if (equal < 0)
3297 goto error;
3298 if (equal) {
3299 isl_set *dom;
3301 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3302 isl_pw_aff_free(pwaff_false);
3303 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3306 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3307 cond_false = isl_pw_aff_zero_set(cond);
3308 return isl_pw_aff_select(cond_true, pwaff_true,
3309 cond_false, pwaff_false);
3310 error:
3311 isl_pw_aff_free(cond);
3312 isl_pw_aff_free(pwaff_true);
3313 isl_pw_aff_free(pwaff_false);
3314 return NULL;
3317 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3319 if (!aff)
3320 return isl_bool_error;
3322 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3325 /* Check whether pwaff is a piecewise constant.
3327 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3329 int i;
3331 if (!pwaff)
3332 return isl_bool_error;
3334 for (i = 0; i < pwaff->n; ++i) {
3335 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3336 if (is_cst < 0 || !is_cst)
3337 return is_cst;
3340 return isl_bool_true;
3343 /* Are all elements of "mpa" piecewise constants?
3345 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3347 int i;
3349 if (!mpa)
3350 return isl_bool_error;
3352 for (i = 0; i < mpa->n; ++i) {
3353 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3354 if (is_cst < 0 || !is_cst)
3355 return is_cst;
3358 return isl_bool_true;
3361 /* Return the product of "aff1" and "aff2".
3363 * If either of the two is NaN, then the result is NaN.
3365 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3367 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3368 __isl_take isl_aff *aff2)
3370 if (!aff1 || !aff2)
3371 goto error;
3373 if (isl_aff_is_nan(aff1)) {
3374 isl_aff_free(aff2);
3375 return aff1;
3377 if (isl_aff_is_nan(aff2)) {
3378 isl_aff_free(aff1);
3379 return aff2;
3382 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3383 return isl_aff_mul(aff2, aff1);
3385 if (!isl_aff_is_cst(aff2))
3386 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3387 "at least one affine expression should be constant",
3388 goto error);
3390 aff1 = isl_aff_cow(aff1);
3391 if (!aff1 || !aff2)
3392 goto error;
3394 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3395 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3397 isl_aff_free(aff2);
3398 return aff1;
3399 error:
3400 isl_aff_free(aff1);
3401 isl_aff_free(aff2);
3402 return NULL;
3405 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3407 * If either of the two is NaN, then the result is NaN.
3409 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3410 __isl_take isl_aff *aff2)
3412 int is_cst;
3413 int neg;
3415 if (!aff1 || !aff2)
3416 goto error;
3418 if (isl_aff_is_nan(aff1)) {
3419 isl_aff_free(aff2);
3420 return aff1;
3422 if (isl_aff_is_nan(aff2)) {
3423 isl_aff_free(aff1);
3424 return aff2;
3427 is_cst = isl_aff_is_cst(aff2);
3428 if (is_cst < 0)
3429 goto error;
3430 if (!is_cst)
3431 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3432 "second argument should be a constant", goto error);
3434 if (!aff2)
3435 goto error;
3437 neg = isl_int_is_neg(aff2->v->el[1]);
3438 if (neg) {
3439 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3440 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3443 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3444 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3446 if (neg) {
3447 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3448 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3451 isl_aff_free(aff2);
3452 return aff1;
3453 error:
3454 isl_aff_free(aff1);
3455 isl_aff_free(aff2);
3456 return NULL;
3459 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3460 __isl_take isl_pw_aff *pwaff2)
3462 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3465 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3466 __isl_take isl_pw_aff *pwaff2)
3468 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3471 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3472 __isl_take isl_pw_aff *pwaff2)
3474 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3477 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3478 __isl_take isl_pw_aff *pwaff2)
3480 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3483 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3484 __isl_take isl_pw_aff *pwaff2)
3486 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3489 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3490 __isl_take isl_pw_aff *pa2)
3492 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3495 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3497 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3498 __isl_take isl_pw_aff *pa2)
3500 int is_cst;
3502 is_cst = isl_pw_aff_is_cst(pa2);
3503 if (is_cst < 0)
3504 goto error;
3505 if (!is_cst)
3506 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3507 "second argument should be a piecewise constant",
3508 goto error);
3509 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3510 error:
3511 isl_pw_aff_free(pa1);
3512 isl_pw_aff_free(pa2);
3513 return NULL;
3516 /* Compute the quotient of the integer division of "pa1" by "pa2"
3517 * with rounding towards zero.
3518 * "pa2" is assumed to be a piecewise constant.
3520 * In particular, return
3522 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3525 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3526 __isl_take isl_pw_aff *pa2)
3528 int is_cst;
3529 isl_set *cond;
3530 isl_pw_aff *f, *c;
3532 is_cst = isl_pw_aff_is_cst(pa2);
3533 if (is_cst < 0)
3534 goto error;
3535 if (!is_cst)
3536 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3537 "second argument should be a piecewise constant",
3538 goto error);
3540 pa1 = isl_pw_aff_div(pa1, pa2);
3542 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3543 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3544 c = isl_pw_aff_ceil(pa1);
3545 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3546 error:
3547 isl_pw_aff_free(pa1);
3548 isl_pw_aff_free(pa2);
3549 return NULL;
3552 /* Compute the remainder of the integer division of "pa1" by "pa2"
3553 * with rounding towards zero.
3554 * "pa2" is assumed to be a piecewise constant.
3556 * In particular, return
3558 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3561 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3562 __isl_take isl_pw_aff *pa2)
3564 int is_cst;
3565 isl_pw_aff *res;
3567 is_cst = isl_pw_aff_is_cst(pa2);
3568 if (is_cst < 0)
3569 goto error;
3570 if (!is_cst)
3571 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3572 "second argument should be a piecewise constant",
3573 goto error);
3574 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3575 res = isl_pw_aff_mul(pa2, res);
3576 res = isl_pw_aff_sub(pa1, res);
3577 return res;
3578 error:
3579 isl_pw_aff_free(pa1);
3580 isl_pw_aff_free(pa2);
3581 return NULL;
3584 /* Does either of "pa1" or "pa2" involve any NaN2?
3586 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3587 __isl_keep isl_pw_aff *pa2)
3589 isl_bool has_nan;
3591 has_nan = isl_pw_aff_involves_nan(pa1);
3592 if (has_nan < 0 || has_nan)
3593 return has_nan;
3594 return isl_pw_aff_involves_nan(pa2);
3597 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3598 * by a NaN on their shared domain.
3600 * In principle, the result could be refined to only being NaN
3601 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3603 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3604 __isl_take isl_pw_aff *pa2)
3606 isl_local_space *ls;
3607 isl_set *dom;
3608 isl_pw_aff *pa;
3610 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3611 ls = isl_local_space_from_space(isl_set_get_space(dom));
3612 pa = isl_pw_aff_nan_on_domain(ls);
3613 pa = isl_pw_aff_intersect_domain(pa, dom);
3615 return pa;
3618 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3619 __isl_take isl_pw_aff *pwaff2)
3621 isl_set *le;
3622 isl_set *dom;
3624 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3625 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3626 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3627 isl_pw_aff_copy(pwaff2));
3628 dom = isl_set_subtract(dom, isl_set_copy(le));
3629 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3632 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3633 __isl_take isl_pw_aff *pwaff2)
3635 isl_set *ge;
3636 isl_set *dom;
3638 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3639 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3640 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3641 isl_pw_aff_copy(pwaff2));
3642 dom = isl_set_subtract(dom, isl_set_copy(ge));
3643 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3646 /* Return an expression for the minimum (if "max" is not set) or
3647 * the maximum (if "max" is set) of "pa1" and "pa2".
3648 * If either expression involves any NaN, then return a NaN
3649 * on the shared domain as result.
3651 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3652 __isl_take isl_pw_aff *pa2, int max)
3654 isl_bool has_nan;
3656 has_nan = either_involves_nan(pa1, pa2);
3657 if (has_nan < 0)
3658 pa1 = isl_pw_aff_free(pa1);
3659 else if (has_nan)
3660 return replace_by_nan(pa1, pa2);
3662 if (max)
3663 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3664 else
3665 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3668 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3670 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3671 __isl_take isl_pw_aff *pwaff2)
3673 return pw_aff_min_max(pwaff1, pwaff2, 0);
3676 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3678 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3679 __isl_take isl_pw_aff *pwaff2)
3681 return pw_aff_min_max(pwaff1, pwaff2, 1);
3684 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3685 __isl_take isl_pw_aff_list *list,
3686 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3687 __isl_take isl_pw_aff *pwaff2))
3689 int i;
3690 isl_ctx *ctx;
3691 isl_pw_aff *res;
3693 if (!list)
3694 return NULL;
3696 ctx = isl_pw_aff_list_get_ctx(list);
3697 if (list->n < 1)
3698 isl_die(ctx, isl_error_invalid,
3699 "list should contain at least one element", goto error);
3701 res = isl_pw_aff_copy(list->p[0]);
3702 for (i = 1; i < list->n; ++i)
3703 res = fn(res, isl_pw_aff_copy(list->p[i]));
3705 isl_pw_aff_list_free(list);
3706 return res;
3707 error:
3708 isl_pw_aff_list_free(list);
3709 return NULL;
3712 /* Return an isl_pw_aff that maps each element in the intersection of the
3713 * domains of the elements of list to the minimal corresponding affine
3714 * expression.
3716 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3718 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3721 /* Return an isl_pw_aff that maps each element in the intersection of the
3722 * domains of the elements of list to the maximal corresponding affine
3723 * expression.
3725 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3727 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3730 /* Mark the domains of "pwaff" as rational.
3732 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3734 int i;
3736 pwaff = isl_pw_aff_cow(pwaff);
3737 if (!pwaff)
3738 return NULL;
3739 if (pwaff->n == 0)
3740 return pwaff;
3742 for (i = 0; i < pwaff->n; ++i) {
3743 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3744 if (!pwaff->p[i].set)
3745 return isl_pw_aff_free(pwaff);
3748 return pwaff;
3751 /* Mark the domains of the elements of "list" as rational.
3753 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3754 __isl_take isl_pw_aff_list *list)
3756 int i, n;
3758 if (!list)
3759 return NULL;
3760 if (list->n == 0)
3761 return list;
3763 n = list->n;
3764 for (i = 0; i < n; ++i) {
3765 isl_pw_aff *pa;
3767 pa = isl_pw_aff_list_get_pw_aff(list, i);
3768 pa = isl_pw_aff_set_rational(pa);
3769 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3772 return list;
3775 /* Do the parameters of "aff" match those of "space"?
3777 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3778 __isl_keep isl_space *space)
3780 isl_space *aff_space;
3781 isl_bool match;
3783 if (!aff || !space)
3784 return isl_bool_error;
3786 aff_space = isl_aff_get_domain_space(aff);
3788 match = isl_space_has_equal_params(space, aff_space);
3790 isl_space_free(aff_space);
3791 return match;
3794 /* Check that the domain space of "aff" matches "space".
3796 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3797 __isl_keep isl_space *space)
3799 isl_space *aff_space;
3800 isl_bool match;
3802 if (!aff || !space)
3803 return isl_stat_error;
3805 aff_space = isl_aff_get_domain_space(aff);
3807 match = isl_space_has_equal_params(space, aff_space);
3808 if (match < 0)
3809 goto error;
3810 if (!match)
3811 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3812 "parameters don't match", goto error);
3813 match = isl_space_tuple_is_equal(space, isl_dim_in,
3814 aff_space, isl_dim_set);
3815 if (match < 0)
3816 goto error;
3817 if (!match)
3818 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3819 "domains don't match", goto error);
3820 isl_space_free(aff_space);
3821 return isl_stat_ok;
3822 error:
3823 isl_space_free(aff_space);
3824 return isl_stat_error;
3827 #undef BASE
3828 #define BASE aff
3829 #undef DOMBASE
3830 #define DOMBASE set
3831 #define NO_DOMAIN
3833 #include <isl_multi_no_explicit_domain.c>
3834 #include <isl_multi_templ.c>
3835 #include <isl_multi_apply_set.c>
3836 #include <isl_multi_cmp.c>
3837 #include <isl_multi_dims.c>
3838 #include <isl_multi_floor.c>
3839 #include <isl_multi_gist.c>
3841 #undef NO_DOMAIN
3843 /* Construct an isl_multi_aff living in "space" that corresponds
3844 * to the affine transformation matrix "mat".
3846 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3847 __isl_take isl_space *space, __isl_take isl_mat *mat)
3849 isl_ctx *ctx;
3850 isl_local_space *ls = NULL;
3851 isl_multi_aff *ma = NULL;
3852 int n_row, n_col, n_out, total;
3853 int i;
3855 if (!space || !mat)
3856 goto error;
3858 ctx = isl_mat_get_ctx(mat);
3860 n_row = isl_mat_rows(mat);
3861 n_col = isl_mat_cols(mat);
3862 if (n_row < 1)
3863 isl_die(ctx, isl_error_invalid,
3864 "insufficient number of rows", goto error);
3865 if (n_col < 1)
3866 isl_die(ctx, isl_error_invalid,
3867 "insufficient number of columns", goto error);
3868 n_out = isl_space_dim(space, isl_dim_out);
3869 total = isl_space_dim(space, isl_dim_all);
3870 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3871 isl_die(ctx, isl_error_invalid,
3872 "dimension mismatch", goto error);
3874 ma = isl_multi_aff_zero(isl_space_copy(space));
3875 ls = isl_local_space_from_space(isl_space_domain(space));
3877 for (i = 0; i < n_row - 1; ++i) {
3878 isl_vec *v;
3879 isl_aff *aff;
3881 v = isl_vec_alloc(ctx, 1 + n_col);
3882 if (!v)
3883 goto error;
3884 isl_int_set(v->el[0], mat->row[0][0]);
3885 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3886 v = isl_vec_normalize(v);
3887 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3888 ma = isl_multi_aff_set_aff(ma, i, aff);
3891 isl_local_space_free(ls);
3892 isl_mat_free(mat);
3893 return ma;
3894 error:
3895 isl_local_space_free(ls);
3896 isl_mat_free(mat);
3897 isl_multi_aff_free(ma);
3898 return NULL;
3901 /* Remove any internal structure of the domain of "ma".
3902 * If there is any such internal structure in the input,
3903 * then the name of the corresponding space is also removed.
3905 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3906 __isl_take isl_multi_aff *ma)
3908 isl_space *space;
3910 if (!ma)
3911 return NULL;
3913 if (!ma->space->nested[0])
3914 return ma;
3916 space = isl_multi_aff_get_space(ma);
3917 space = isl_space_flatten_domain(space);
3918 ma = isl_multi_aff_reset_space(ma, space);
3920 return ma;
3923 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3924 * of the space to its domain.
3926 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3928 int i, n_in;
3929 isl_local_space *ls;
3930 isl_multi_aff *ma;
3932 if (!space)
3933 return NULL;
3934 if (!isl_space_is_map(space))
3935 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3936 "not a map space", goto error);
3938 n_in = isl_space_dim(space, isl_dim_in);
3939 space = isl_space_domain_map(space);
3941 ma = isl_multi_aff_alloc(isl_space_copy(space));
3942 if (n_in == 0) {
3943 isl_space_free(space);
3944 return ma;
3947 space = isl_space_domain(space);
3948 ls = isl_local_space_from_space(space);
3949 for (i = 0; i < n_in; ++i) {
3950 isl_aff *aff;
3952 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3953 isl_dim_set, i);
3954 ma = isl_multi_aff_set_aff(ma, i, aff);
3956 isl_local_space_free(ls);
3957 return ma;
3958 error:
3959 isl_space_free(space);
3960 return NULL;
3963 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3964 * of the space to its range.
3966 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3968 int i, n_in, n_out;
3969 isl_local_space *ls;
3970 isl_multi_aff *ma;
3972 if (!space)
3973 return NULL;
3974 if (!isl_space_is_map(space))
3975 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3976 "not a map space", goto error);
3978 n_in = isl_space_dim(space, isl_dim_in);
3979 n_out = isl_space_dim(space, isl_dim_out);
3980 space = isl_space_range_map(space);
3982 ma = isl_multi_aff_alloc(isl_space_copy(space));
3983 if (n_out == 0) {
3984 isl_space_free(space);
3985 return ma;
3988 space = isl_space_domain(space);
3989 ls = isl_local_space_from_space(space);
3990 for (i = 0; i < n_out; ++i) {
3991 isl_aff *aff;
3993 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3994 isl_dim_set, n_in + i);
3995 ma = isl_multi_aff_set_aff(ma, i, aff);
3997 isl_local_space_free(ls);
3998 return ma;
3999 error:
4000 isl_space_free(space);
4001 return NULL;
4004 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4005 * of the space to its range.
4007 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4008 __isl_take isl_space *space)
4010 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4013 /* Given the space of a set and a range of set dimensions,
4014 * construct an isl_multi_aff that projects out those dimensions.
4016 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4017 __isl_take isl_space *space, enum isl_dim_type type,
4018 unsigned first, unsigned n)
4020 int i, dim;
4021 isl_local_space *ls;
4022 isl_multi_aff *ma;
4024 if (!space)
4025 return NULL;
4026 if (!isl_space_is_set(space))
4027 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4028 "expecting set space", goto error);
4029 if (type != isl_dim_set)
4030 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4031 "only set dimensions can be projected out", goto error);
4033 dim = isl_space_dim(space, isl_dim_set);
4034 if (first + n > dim)
4035 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4036 "range out of bounds", goto error);
4038 space = isl_space_from_domain(space);
4039 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4041 if (dim == n)
4042 return isl_multi_aff_alloc(space);
4044 ma = isl_multi_aff_alloc(isl_space_copy(space));
4045 space = isl_space_domain(space);
4046 ls = isl_local_space_from_space(space);
4048 for (i = 0; i < first; ++i) {
4049 isl_aff *aff;
4051 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4052 isl_dim_set, i);
4053 ma = isl_multi_aff_set_aff(ma, i, aff);
4056 for (i = 0; i < dim - (first + n); ++i) {
4057 isl_aff *aff;
4059 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4060 isl_dim_set, first + n + i);
4061 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4064 isl_local_space_free(ls);
4065 return ma;
4066 error:
4067 isl_space_free(space);
4068 return NULL;
4071 /* Given the space of a set and a range of set dimensions,
4072 * construct an isl_pw_multi_aff that projects out those dimensions.
4074 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4075 __isl_take isl_space *space, enum isl_dim_type type,
4076 unsigned first, unsigned n)
4078 isl_multi_aff *ma;
4080 ma = isl_multi_aff_project_out_map(space, type, first, n);
4081 return isl_pw_multi_aff_from_multi_aff(ma);
4084 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4085 * domain.
4087 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4088 __isl_take isl_multi_aff *ma)
4090 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4091 return isl_pw_multi_aff_alloc(dom, ma);
4094 /* Create a piecewise multi-affine expression in the given space that maps each
4095 * input dimension to the corresponding output dimension.
4097 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4098 __isl_take isl_space *space)
4100 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4103 /* Exploit the equalities in "eq" to simplify the affine expressions.
4105 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4106 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4108 int i;
4110 maff = isl_multi_aff_cow(maff);
4111 if (!maff || !eq)
4112 goto error;
4114 for (i = 0; i < maff->n; ++i) {
4115 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4116 isl_basic_set_copy(eq));
4117 if (!maff->u.p[i])
4118 goto error;
4121 isl_basic_set_free(eq);
4122 return maff;
4123 error:
4124 isl_basic_set_free(eq);
4125 isl_multi_aff_free(maff);
4126 return NULL;
4129 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4130 isl_int f)
4132 int i;
4134 maff = isl_multi_aff_cow(maff);
4135 if (!maff)
4136 return NULL;
4138 for (i = 0; i < maff->n; ++i) {
4139 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4140 if (!maff->u.p[i])
4141 return isl_multi_aff_free(maff);
4144 return maff;
4147 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4148 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4150 maff1 = isl_multi_aff_add(maff1, maff2);
4151 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4152 return maff1;
4155 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4157 if (!maff)
4158 return -1;
4160 return 0;
4163 /* Return the set of domain elements where "ma1" is lexicographically
4164 * smaller than or equal to "ma2".
4166 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4167 __isl_take isl_multi_aff *ma2)
4169 return isl_multi_aff_lex_ge_set(ma2, ma1);
4172 /* Return the set of domain elements where "ma1" is lexicographically
4173 * smaller than "ma2".
4175 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4176 __isl_take isl_multi_aff *ma2)
4178 return isl_multi_aff_lex_gt_set(ma2, ma1);
4181 /* Return the set of domain elements where "ma1" and "ma2"
4182 * satisfy "order".
4184 static __isl_give isl_set *isl_multi_aff_order_set(
4185 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4186 __isl_give isl_map *order(__isl_take isl_space *set_space))
4188 isl_space *space;
4189 isl_map *map1, *map2;
4190 isl_map *map, *ge;
4192 map1 = isl_map_from_multi_aff(ma1);
4193 map2 = isl_map_from_multi_aff(ma2);
4194 map = isl_map_range_product(map1, map2);
4195 space = isl_space_range(isl_map_get_space(map));
4196 space = isl_space_domain(isl_space_unwrap(space));
4197 ge = order(space);
4198 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4200 return isl_map_domain(map);
4203 /* Return the set of domain elements where "ma1" is lexicographically
4204 * greater than or equal to "ma2".
4206 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4207 __isl_take isl_multi_aff *ma2)
4209 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4212 /* Return the set of domain elements where "ma1" is lexicographically
4213 * greater than "ma2".
4215 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4216 __isl_take isl_multi_aff *ma2)
4218 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4221 #undef PW
4222 #define PW isl_pw_multi_aff
4223 #undef EL
4224 #define EL isl_multi_aff
4225 #undef EL_IS_ZERO
4226 #define EL_IS_ZERO is_empty
4227 #undef ZERO
4228 #define ZERO empty
4229 #undef IS_ZERO
4230 #define IS_ZERO is_empty
4231 #undef FIELD
4232 #define FIELD maff
4233 #undef DEFAULT_IS_ZERO
4234 #define DEFAULT_IS_ZERO 0
4236 #define NO_SUB
4237 #define NO_EVAL
4238 #define NO_OPT
4239 #define NO_INVOLVES_DIMS
4240 #define NO_INSERT_DIMS
4241 #define NO_LIFT
4242 #define NO_MORPH
4244 #include <isl_pw_templ.c>
4245 #include <isl_pw_union_opt.c>
4247 #undef NO_SUB
4249 #undef UNION
4250 #define UNION isl_union_pw_multi_aff
4251 #undef PART
4252 #define PART isl_pw_multi_aff
4253 #undef PARTS
4254 #define PARTS pw_multi_aff
4256 #include <isl_union_multi.c>
4257 #include <isl_union_neg.c>
4259 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4260 __isl_take isl_pw_multi_aff *pma1,
4261 __isl_take isl_pw_multi_aff *pma2)
4263 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4264 &isl_multi_aff_lex_ge_set);
4267 /* Given two piecewise multi affine expressions, return a piecewise
4268 * multi-affine expression defined on the union of the definition domains
4269 * of the inputs that is equal to the lexicographic maximum of the two
4270 * inputs on each cell. If only one of the two inputs is defined on
4271 * a given cell, then it is considered to be the maximum.
4273 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4274 __isl_take isl_pw_multi_aff *pma1,
4275 __isl_take isl_pw_multi_aff *pma2)
4277 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4278 &pw_multi_aff_union_lexmax);
4281 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4282 __isl_take isl_pw_multi_aff *pma1,
4283 __isl_take isl_pw_multi_aff *pma2)
4285 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4286 &isl_multi_aff_lex_le_set);
4289 /* Given two piecewise multi affine expressions, return a piecewise
4290 * multi-affine expression defined on the union of the definition domains
4291 * of the inputs that is equal to the lexicographic minimum of the two
4292 * inputs on each cell. If only one of the two inputs is defined on
4293 * a given cell, then it is considered to be the minimum.
4295 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4296 __isl_take isl_pw_multi_aff *pma1,
4297 __isl_take isl_pw_multi_aff *pma2)
4299 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4300 &pw_multi_aff_union_lexmin);
4303 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4304 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4306 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4307 &isl_multi_aff_add);
4310 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4311 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4313 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4314 &pw_multi_aff_add);
4317 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4318 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4320 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4321 &isl_multi_aff_sub);
4324 /* Subtract "pma2" from "pma1" and return the result.
4326 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4327 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4329 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4330 &pw_multi_aff_sub);
4333 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4334 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4336 return isl_pw_multi_aff_union_add_(pma1, pma2);
4339 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4340 * with the actual sum on the shared domain and
4341 * the defined expression on the symmetric difference of the domains.
4343 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4344 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4346 return isl_union_pw_aff_union_add_(upa1, upa2);
4349 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4350 * with the actual sum on the shared domain and
4351 * the defined expression on the symmetric difference of the domains.
4353 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4354 __isl_take isl_union_pw_multi_aff *upma1,
4355 __isl_take isl_union_pw_multi_aff *upma2)
4357 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4360 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4361 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4363 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4364 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4366 int i, j, n;
4367 isl_space *space;
4368 isl_pw_multi_aff *res;
4370 if (!pma1 || !pma2)
4371 goto error;
4373 n = pma1->n * pma2->n;
4374 space = isl_space_product(isl_space_copy(pma1->dim),
4375 isl_space_copy(pma2->dim));
4376 res = isl_pw_multi_aff_alloc_size(space, n);
4378 for (i = 0; i < pma1->n; ++i) {
4379 for (j = 0; j < pma2->n; ++j) {
4380 isl_set *domain;
4381 isl_multi_aff *ma;
4383 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4384 isl_set_copy(pma2->p[j].set));
4385 ma = isl_multi_aff_product(
4386 isl_multi_aff_copy(pma1->p[i].maff),
4387 isl_multi_aff_copy(pma2->p[j].maff));
4388 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4392 isl_pw_multi_aff_free(pma1);
4393 isl_pw_multi_aff_free(pma2);
4394 return res;
4395 error:
4396 isl_pw_multi_aff_free(pma1);
4397 isl_pw_multi_aff_free(pma2);
4398 return NULL;
4401 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4402 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4404 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4405 &pw_multi_aff_product);
4408 /* Construct a map mapping the domain of the piecewise multi-affine expression
4409 * to its range, with each dimension in the range equated to the
4410 * corresponding affine expression on its cell.
4412 * If the domain of "pma" is rational, then so is the constructed "map".
4414 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4416 int i;
4417 isl_map *map;
4419 if (!pma)
4420 return NULL;
4422 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4424 for (i = 0; i < pma->n; ++i) {
4425 isl_bool rational;
4426 isl_multi_aff *maff;
4427 isl_basic_map *bmap;
4428 isl_map *map_i;
4430 rational = isl_set_is_rational(pma->p[i].set);
4431 if (rational < 0)
4432 map = isl_map_free(map);
4433 maff = isl_multi_aff_copy(pma->p[i].maff);
4434 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4435 map_i = isl_map_from_basic_map(bmap);
4436 map_i = isl_map_intersect_domain(map_i,
4437 isl_set_copy(pma->p[i].set));
4438 map = isl_map_union_disjoint(map, map_i);
4441 isl_pw_multi_aff_free(pma);
4442 return map;
4445 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4447 if (!pma)
4448 return NULL;
4450 if (!isl_space_is_set(pma->dim))
4451 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4452 "isl_pw_multi_aff cannot be converted into an isl_set",
4453 goto error);
4455 return isl_map_from_pw_multi_aff(pma);
4456 error:
4457 isl_pw_multi_aff_free(pma);
4458 return NULL;
4461 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4462 * denominator "denom".
4463 * "denom" is allowed to be negative, in which case the actual denominator
4464 * is -denom and the expressions are added instead.
4466 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4467 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4469 int i, first;
4470 int sign;
4471 isl_int d;
4473 first = isl_seq_first_non_zero(c, n);
4474 if (first == -1)
4475 return aff;
4477 sign = isl_int_sgn(denom);
4478 isl_int_init(d);
4479 isl_int_abs(d, denom);
4480 for (i = first; i < n; ++i) {
4481 isl_aff *aff_i;
4483 if (isl_int_is_zero(c[i]))
4484 continue;
4485 aff_i = isl_multi_aff_get_aff(ma, i);
4486 aff_i = isl_aff_scale(aff_i, c[i]);
4487 aff_i = isl_aff_scale_down(aff_i, d);
4488 if (sign >= 0)
4489 aff = isl_aff_sub(aff, aff_i);
4490 else
4491 aff = isl_aff_add(aff, aff_i);
4493 isl_int_clear(d);
4495 return aff;
4498 /* Extract an affine expression that expresses the output dimension "pos"
4499 * of "bmap" in terms of the parameters and input dimensions from
4500 * equality "eq".
4501 * Note that this expression may involve integer divisions defined
4502 * in terms of parameters and input dimensions.
4503 * The equality may also involve references to earlier (but not later)
4504 * output dimensions. These are replaced by the corresponding elements
4505 * in "ma".
4507 * If the equality is of the form
4509 * f(i) + h(j) + a x + g(i) = 0,
4511 * with f(i) a linear combinations of the parameters and input dimensions,
4512 * g(i) a linear combination of integer divisions defined in terms of the same
4513 * and h(j) a linear combinations of earlier output dimensions,
4514 * then the affine expression is
4516 * (-f(i) - g(i))/a - h(j)/a
4518 * If the equality is of the form
4520 * f(i) + h(j) - a x + g(i) = 0,
4522 * then the affine expression is
4524 * (f(i) + g(i))/a - h(j)/(-a)
4527 * If "div" refers to an integer division (i.e., it is smaller than
4528 * the number of integer divisions), then the equality constraint
4529 * does involve an integer division (the one at position "div") that
4530 * is defined in terms of output dimensions. However, this integer
4531 * division can be eliminated by exploiting a pair of constraints
4532 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4533 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4534 * -l + x >= 0.
4535 * In particular, let
4537 * x = e(i) + m floor(...)
4539 * with e(i) the expression derived above and floor(...) the integer
4540 * division involving output dimensions.
4541 * From
4543 * l <= x <= l + n,
4545 * we have
4547 * 0 <= x - l <= n
4549 * This means
4551 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4552 * = (e(i) - l) mod m
4554 * Therefore,
4556 * x - l = (e(i) - l) mod m
4558 * or
4560 * x = ((e(i) - l) mod m) + l
4562 * The variable "shift" below contains the expression -l, which may
4563 * also involve a linear combination of earlier output dimensions.
4565 static __isl_give isl_aff *extract_aff_from_equality(
4566 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4567 __isl_keep isl_multi_aff *ma)
4569 unsigned o_out;
4570 unsigned n_div, n_out;
4571 isl_ctx *ctx;
4572 isl_local_space *ls;
4573 isl_aff *aff, *shift;
4574 isl_val *mod;
4576 ctx = isl_basic_map_get_ctx(bmap);
4577 ls = isl_basic_map_get_local_space(bmap);
4578 ls = isl_local_space_domain(ls);
4579 aff = isl_aff_alloc(isl_local_space_copy(ls));
4580 if (!aff)
4581 goto error;
4582 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4583 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4584 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4585 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4586 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4587 isl_seq_cpy(aff->v->el + 1 + o_out,
4588 bmap->eq[eq] + o_out + n_out, n_div);
4589 } else {
4590 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4591 isl_seq_neg(aff->v->el + 1 + o_out,
4592 bmap->eq[eq] + o_out + n_out, n_div);
4594 if (div < n_div)
4595 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4596 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4597 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4598 bmap->eq[eq][o_out + pos]);
4599 if (div < n_div) {
4600 shift = isl_aff_alloc(isl_local_space_copy(ls));
4601 if (!shift)
4602 goto error;
4603 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4604 isl_seq_cpy(shift->v->el + 1 + o_out,
4605 bmap->ineq[ineq] + o_out + n_out, n_div);
4606 isl_int_set_si(shift->v->el[0], 1);
4607 shift = subtract_initial(shift, ma, pos,
4608 bmap->ineq[ineq] + o_out, ctx->negone);
4609 aff = isl_aff_add(aff, isl_aff_copy(shift));
4610 mod = isl_val_int_from_isl_int(ctx,
4611 bmap->eq[eq][o_out + n_out + div]);
4612 mod = isl_val_abs(mod);
4613 aff = isl_aff_mod_val(aff, mod);
4614 aff = isl_aff_sub(aff, shift);
4617 isl_local_space_free(ls);
4618 return aff;
4619 error:
4620 isl_local_space_free(ls);
4621 isl_aff_free(aff);
4622 return NULL;
4625 /* Given a basic map with output dimensions defined
4626 * in terms of the parameters input dimensions and earlier
4627 * output dimensions using an equality (and possibly a pair on inequalities),
4628 * extract an isl_aff that expresses output dimension "pos" in terms
4629 * of the parameters and input dimensions.
4630 * Note that this expression may involve integer divisions defined
4631 * in terms of parameters and input dimensions.
4632 * "ma" contains the expressions corresponding to earlier output dimensions.
4634 * This function shares some similarities with
4635 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4637 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4638 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4640 int eq, div, ineq;
4641 isl_aff *aff;
4643 if (!bmap)
4644 return NULL;
4645 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4646 if (eq >= bmap->n_eq)
4647 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4648 "unable to find suitable equality", return NULL);
4649 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4651 aff = isl_aff_remove_unused_divs(aff);
4652 return aff;
4655 /* Given a basic map where each output dimension is defined
4656 * in terms of the parameters and input dimensions using an equality,
4657 * extract an isl_multi_aff that expresses the output dimensions in terms
4658 * of the parameters and input dimensions.
4660 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4661 __isl_take isl_basic_map *bmap)
4663 int i;
4664 unsigned n_out;
4665 isl_multi_aff *ma;
4667 if (!bmap)
4668 return NULL;
4670 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4671 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4673 for (i = 0; i < n_out; ++i) {
4674 isl_aff *aff;
4676 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4677 ma = isl_multi_aff_set_aff(ma, i, aff);
4680 isl_basic_map_free(bmap);
4682 return ma;
4685 /* Given a basic set where each set dimension is defined
4686 * in terms of the parameters using an equality,
4687 * extract an isl_multi_aff that expresses the set dimensions in terms
4688 * of the parameters.
4690 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4691 __isl_take isl_basic_set *bset)
4693 return extract_isl_multi_aff_from_basic_map(bset);
4696 /* Create an isl_pw_multi_aff that is equivalent to
4697 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4698 * The given basic map is such that each output dimension is defined
4699 * in terms of the parameters and input dimensions using an equality.
4701 * Since some applications expect the result of isl_pw_multi_aff_from_map
4702 * to only contain integer affine expressions, we compute the floor
4703 * of the expression before returning.
4705 * Remove all constraints involving local variables without
4706 * an explicit representation (resulting in the removal of those
4707 * local variables) prior to the actual extraction to ensure
4708 * that the local spaces in which the resulting affine expressions
4709 * are created do not contain any unknown local variables.
4710 * Removing such constraints is safe because constraints involving
4711 * unknown local variables are not used to determine whether
4712 * a basic map is obviously single-valued.
4714 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4715 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4717 isl_multi_aff *ma;
4719 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4720 ma = extract_isl_multi_aff_from_basic_map(bmap);
4721 ma = isl_multi_aff_floor(ma);
4722 return isl_pw_multi_aff_alloc(domain, ma);
4725 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4726 * This obviously only works if the input "map" is single-valued.
4727 * If so, we compute the lexicographic minimum of the image in the form
4728 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4729 * to its lexicographic minimum.
4730 * If the input is not single-valued, we produce an error.
4732 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4733 __isl_take isl_map *map)
4735 int i;
4736 int sv;
4737 isl_pw_multi_aff *pma;
4739 sv = isl_map_is_single_valued(map);
4740 if (sv < 0)
4741 goto error;
4742 if (!sv)
4743 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4744 "map is not single-valued", goto error);
4745 map = isl_map_make_disjoint(map);
4746 if (!map)
4747 return NULL;
4749 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4751 for (i = 0; i < map->n; ++i) {
4752 isl_pw_multi_aff *pma_i;
4753 isl_basic_map *bmap;
4754 bmap = isl_basic_map_copy(map->p[i]);
4755 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4756 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4759 isl_map_free(map);
4760 return pma;
4761 error:
4762 isl_map_free(map);
4763 return NULL;
4766 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4767 * taking into account that the output dimension at position "d"
4768 * can be represented as
4770 * x = floor((e(...) + c1) / m)
4772 * given that constraint "i" is of the form
4774 * e(...) + c1 - m x >= 0
4777 * Let "map" be of the form
4779 * A -> B
4781 * We construct a mapping
4783 * A -> [A -> x = floor(...)]
4785 * apply that to the map, obtaining
4787 * [A -> x = floor(...)] -> B
4789 * and equate dimension "d" to x.
4790 * We then compute a isl_pw_multi_aff representation of the resulting map
4791 * and plug in the mapping above.
4793 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4794 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4796 isl_ctx *ctx;
4797 isl_space *space;
4798 isl_local_space *ls;
4799 isl_multi_aff *ma;
4800 isl_aff *aff;
4801 isl_vec *v;
4802 isl_map *insert;
4803 int offset;
4804 int n;
4805 int n_in;
4806 isl_pw_multi_aff *pma;
4807 isl_bool is_set;
4809 is_set = isl_map_is_set(map);
4810 if (is_set < 0)
4811 goto error;
4813 offset = isl_basic_map_offset(hull, isl_dim_out);
4814 ctx = isl_map_get_ctx(map);
4815 space = isl_space_domain(isl_map_get_space(map));
4816 n_in = isl_space_dim(space, isl_dim_set);
4817 n = isl_space_dim(space, isl_dim_all);
4819 v = isl_vec_alloc(ctx, 1 + 1 + n);
4820 if (v) {
4821 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4822 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4824 isl_basic_map_free(hull);
4826 ls = isl_local_space_from_space(isl_space_copy(space));
4827 aff = isl_aff_alloc_vec(ls, v);
4828 aff = isl_aff_floor(aff);
4829 if (is_set) {
4830 isl_space_free(space);
4831 ma = isl_multi_aff_from_aff(aff);
4832 } else {
4833 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4834 ma = isl_multi_aff_range_product(ma,
4835 isl_multi_aff_from_aff(aff));
4838 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4839 map = isl_map_apply_domain(map, insert);
4840 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4841 pma = isl_pw_multi_aff_from_map(map);
4842 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4844 return pma;
4845 error:
4846 isl_map_free(map);
4847 isl_basic_map_free(hull);
4848 return NULL;
4851 /* Is constraint "c" of the form
4853 * e(...) + c1 - m x >= 0
4855 * or
4857 * -e(...) + c2 + m x >= 0
4859 * where m > 1 and e only depends on parameters and input dimemnsions?
4861 * "offset" is the offset of the output dimensions
4862 * "pos" is the position of output dimension x.
4864 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4866 if (isl_int_is_zero(c[offset + d]))
4867 return 0;
4868 if (isl_int_is_one(c[offset + d]))
4869 return 0;
4870 if (isl_int_is_negone(c[offset + d]))
4871 return 0;
4872 if (isl_seq_first_non_zero(c + offset, d) != -1)
4873 return 0;
4874 if (isl_seq_first_non_zero(c + offset + d + 1,
4875 total - (offset + d + 1)) != -1)
4876 return 0;
4877 return 1;
4880 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4882 * As a special case, we first check if there is any pair of constraints,
4883 * shared by all the basic maps in "map" that force a given dimension
4884 * to be equal to the floor of some affine combination of the input dimensions.
4886 * In particular, if we can find two constraints
4888 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4890 * and
4892 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4894 * where m > 1 and e only depends on parameters and input dimemnsions,
4895 * and such that
4897 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4899 * then we know that we can take
4901 * x = floor((e(...) + c1) / m)
4903 * without having to perform any computation.
4905 * Note that we know that
4907 * c1 + c2 >= 1
4909 * If c1 + c2 were 0, then we would have detected an equality during
4910 * simplification. If c1 + c2 were negative, then we would have detected
4911 * a contradiction.
4913 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4914 __isl_take isl_map *map)
4916 int d, dim;
4917 int i, j, n;
4918 int offset, total;
4919 isl_int sum;
4920 isl_basic_map *hull;
4922 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4923 if (!hull)
4924 goto error;
4926 isl_int_init(sum);
4927 dim = isl_map_dim(map, isl_dim_out);
4928 offset = isl_basic_map_offset(hull, isl_dim_out);
4929 total = 1 + isl_basic_map_total_dim(hull);
4930 n = hull->n_ineq;
4931 for (d = 0; d < dim; ++d) {
4932 for (i = 0; i < n; ++i) {
4933 if (!is_potential_div_constraint(hull->ineq[i],
4934 offset, d, total))
4935 continue;
4936 for (j = i + 1; j < n; ++j) {
4937 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4938 hull->ineq[j] + 1, total - 1))
4939 continue;
4940 isl_int_add(sum, hull->ineq[i][0],
4941 hull->ineq[j][0]);
4942 if (isl_int_abs_lt(sum,
4943 hull->ineq[i][offset + d]))
4944 break;
4947 if (j >= n)
4948 continue;
4949 isl_int_clear(sum);
4950 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4951 j = i;
4952 return pw_multi_aff_from_map_div(map, hull, d, j);
4955 isl_int_clear(sum);
4956 isl_basic_map_free(hull);
4957 return pw_multi_aff_from_map_base(map);
4958 error:
4959 isl_map_free(map);
4960 isl_basic_map_free(hull);
4961 return NULL;
4964 /* Given an affine expression
4966 * [A -> B] -> f(A,B)
4968 * construct an isl_multi_aff
4970 * [A -> B] -> B'
4972 * such that dimension "d" in B' is set to "aff" and the remaining
4973 * dimensions are set equal to the corresponding dimensions in B.
4974 * "n_in" is the dimension of the space A.
4975 * "n_out" is the dimension of the space B.
4977 * If "is_set" is set, then the affine expression is of the form
4979 * [B] -> f(B)
4981 * and we construct an isl_multi_aff
4983 * B -> B'
4985 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4986 unsigned n_in, unsigned n_out, int is_set)
4988 int i;
4989 isl_multi_aff *ma;
4990 isl_space *space, *space2;
4991 isl_local_space *ls;
4993 space = isl_aff_get_domain_space(aff);
4994 ls = isl_local_space_from_space(isl_space_copy(space));
4995 space2 = isl_space_copy(space);
4996 if (!is_set)
4997 space2 = isl_space_range(isl_space_unwrap(space2));
4998 space = isl_space_map_from_domain_and_range(space, space2);
4999 ma = isl_multi_aff_alloc(space);
5000 ma = isl_multi_aff_set_aff(ma, d, aff);
5002 for (i = 0; i < n_out; ++i) {
5003 if (i == d)
5004 continue;
5005 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5006 isl_dim_set, n_in + i);
5007 ma = isl_multi_aff_set_aff(ma, i, aff);
5010 isl_local_space_free(ls);
5012 return ma;
5015 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5016 * taking into account that the dimension at position "d" can be written as
5018 * x = m a + f(..) (1)
5020 * where m is equal to "gcd".
5021 * "i" is the index of the equality in "hull" that defines f(..).
5022 * In particular, the equality is of the form
5024 * f(..) - x + m g(existentials) = 0
5026 * or
5028 * -f(..) + x + m g(existentials) = 0
5030 * We basically plug (1) into "map", resulting in a map with "a"
5031 * in the range instead of "x". The corresponding isl_pw_multi_aff
5032 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5034 * Specifically, given the input map
5036 * A -> B
5038 * We first wrap it into a set
5040 * [A -> B]
5042 * and define (1) on top of the corresponding space, resulting in "aff".
5043 * We use this to create an isl_multi_aff that maps the output position "d"
5044 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5045 * We plug this into the wrapped map, unwrap the result and compute the
5046 * corresponding isl_pw_multi_aff.
5047 * The result is an expression
5049 * A -> T(A)
5051 * We adjust that to
5053 * A -> [A -> T(A)]
5055 * so that we can plug that into "aff", after extending the latter to
5056 * a mapping
5058 * [A -> B] -> B'
5061 * If "map" is actually a set, then there is no "A" space, meaning
5062 * that we do not need to perform any wrapping, and that the result
5063 * of the recursive call is of the form
5065 * [T]
5067 * which is plugged into a mapping of the form
5069 * B -> B'
5071 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5072 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5073 isl_int gcd)
5075 isl_set *set;
5076 isl_space *space;
5077 isl_local_space *ls;
5078 isl_aff *aff;
5079 isl_multi_aff *ma;
5080 isl_pw_multi_aff *pma, *id;
5081 unsigned n_in;
5082 unsigned o_out;
5083 unsigned n_out;
5084 isl_bool is_set;
5086 is_set = isl_map_is_set(map);
5087 if (is_set < 0)
5088 goto error;
5090 n_in = isl_basic_map_dim(hull, isl_dim_in);
5091 n_out = isl_basic_map_dim(hull, isl_dim_out);
5092 o_out = isl_basic_map_offset(hull, isl_dim_out);
5094 if (is_set)
5095 set = map;
5096 else
5097 set = isl_map_wrap(map);
5098 space = isl_space_map_from_set(isl_set_get_space(set));
5099 ma = isl_multi_aff_identity(space);
5100 ls = isl_local_space_from_space(isl_set_get_space(set));
5101 aff = isl_aff_alloc(ls);
5102 if (aff) {
5103 isl_int_set_si(aff->v->el[0], 1);
5104 if (isl_int_is_one(hull->eq[i][o_out + d]))
5105 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5106 aff->v->size - 1);
5107 else
5108 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5109 aff->v->size - 1);
5110 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5112 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5113 set = isl_set_preimage_multi_aff(set, ma);
5115 ma = range_map(aff, d, n_in, n_out, is_set);
5117 if (is_set)
5118 map = set;
5119 else
5120 map = isl_set_unwrap(set);
5121 pma = isl_pw_multi_aff_from_map(map);
5123 if (!is_set) {
5124 space = isl_pw_multi_aff_get_domain_space(pma);
5125 space = isl_space_map_from_set(space);
5126 id = isl_pw_multi_aff_identity(space);
5127 pma = isl_pw_multi_aff_range_product(id, pma);
5129 id = isl_pw_multi_aff_from_multi_aff(ma);
5130 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5132 isl_basic_map_free(hull);
5133 return pma;
5134 error:
5135 isl_map_free(map);
5136 isl_basic_map_free(hull);
5137 return NULL;
5140 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5141 * "hull" contains the equalities valid for "map".
5143 * Check if any of the output dimensions is "strided".
5144 * That is, we check if it can be written as
5146 * x = m a + f(..)
5148 * with m greater than 1, a some combination of existentially quantified
5149 * variables and f an expression in the parameters and input dimensions.
5150 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5152 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5153 * special case.
5155 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5156 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5158 int i, j;
5159 unsigned n_out;
5160 unsigned o_out;
5161 unsigned n_div;
5162 unsigned o_div;
5163 isl_int gcd;
5165 n_div = isl_basic_map_dim(hull, isl_dim_div);
5166 o_div = isl_basic_map_offset(hull, isl_dim_div);
5168 if (n_div == 0) {
5169 isl_basic_map_free(hull);
5170 return pw_multi_aff_from_map_check_div(map);
5173 isl_int_init(gcd);
5175 n_out = isl_basic_map_dim(hull, isl_dim_out);
5176 o_out = isl_basic_map_offset(hull, isl_dim_out);
5178 for (i = 0; i < n_out; ++i) {
5179 for (j = 0; j < hull->n_eq; ++j) {
5180 isl_int *eq = hull->eq[j];
5181 isl_pw_multi_aff *res;
5183 if (!isl_int_is_one(eq[o_out + i]) &&
5184 !isl_int_is_negone(eq[o_out + i]))
5185 continue;
5186 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5187 continue;
5188 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5189 n_out - (i + 1)) != -1)
5190 continue;
5191 isl_seq_gcd(eq + o_div, n_div, &gcd);
5192 if (isl_int_is_zero(gcd))
5193 continue;
5194 if (isl_int_is_one(gcd))
5195 continue;
5197 res = pw_multi_aff_from_map_stride(map, hull,
5198 i, j, gcd);
5199 isl_int_clear(gcd);
5200 return res;
5204 isl_int_clear(gcd);
5205 isl_basic_map_free(hull);
5206 return pw_multi_aff_from_map_check_div(map);
5209 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5211 * As a special case, we first check if all output dimensions are uniquely
5212 * defined in terms of the parameters and input dimensions over the entire
5213 * domain. If so, we extract the desired isl_pw_multi_aff directly
5214 * from the affine hull of "map" and its domain.
5216 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5217 * special cases.
5219 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5221 isl_bool sv;
5222 isl_basic_map *hull;
5224 if (!map)
5225 return NULL;
5227 if (isl_map_n_basic_map(map) == 1) {
5228 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5229 hull = isl_basic_map_plain_affine_hull(hull);
5230 sv = isl_basic_map_plain_is_single_valued(hull);
5231 if (sv >= 0 && sv)
5232 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5233 hull);
5234 isl_basic_map_free(hull);
5236 map = isl_map_detect_equalities(map);
5237 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5238 sv = isl_basic_map_plain_is_single_valued(hull);
5239 if (sv >= 0 && sv)
5240 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5241 if (sv >= 0)
5242 return pw_multi_aff_from_map_check_strides(map, hull);
5243 isl_basic_map_free(hull);
5244 isl_map_free(map);
5245 return NULL;
5248 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5250 return isl_pw_multi_aff_from_map(set);
5253 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5254 * add it to *user.
5256 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5258 isl_union_pw_multi_aff **upma = user;
5259 isl_pw_multi_aff *pma;
5261 pma = isl_pw_multi_aff_from_map(map);
5262 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5264 return *upma ? isl_stat_ok : isl_stat_error;
5267 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5268 * domain.
5270 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5271 __isl_take isl_aff *aff)
5273 isl_multi_aff *ma;
5274 isl_pw_multi_aff *pma;
5276 ma = isl_multi_aff_from_aff(aff);
5277 pma = isl_pw_multi_aff_from_multi_aff(ma);
5278 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5281 /* Try and create an isl_union_pw_multi_aff that is equivalent
5282 * to the given isl_union_map.
5283 * The isl_union_map is required to be single-valued in each space.
5284 * Otherwise, an error is produced.
5286 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5287 __isl_take isl_union_map *umap)
5289 isl_space *space;
5290 isl_union_pw_multi_aff *upma;
5292 space = isl_union_map_get_space(umap);
5293 upma = isl_union_pw_multi_aff_empty(space);
5294 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5295 upma = isl_union_pw_multi_aff_free(upma);
5296 isl_union_map_free(umap);
5298 return upma;
5301 /* Try and create an isl_union_pw_multi_aff that is equivalent
5302 * to the given isl_union_set.
5303 * The isl_union_set is required to be a singleton in each space.
5304 * Otherwise, an error is produced.
5306 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5307 __isl_take isl_union_set *uset)
5309 return isl_union_pw_multi_aff_from_union_map(uset);
5312 /* Return the piecewise affine expression "set ? 1 : 0".
5314 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5316 isl_pw_aff *pa;
5317 isl_space *space = isl_set_get_space(set);
5318 isl_local_space *ls = isl_local_space_from_space(space);
5319 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5320 isl_aff *one = isl_aff_zero_on_domain(ls);
5322 one = isl_aff_add_constant_si(one, 1);
5323 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5324 set = isl_set_complement(set);
5325 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5327 return pa;
5330 /* Plug in "subs" for dimension "type", "pos" of "aff".
5332 * Let i be the dimension to replace and let "subs" be of the form
5334 * f/d
5336 * and "aff" of the form
5338 * (a i + g)/m
5340 * The result is
5342 * (a f + d g')/(m d)
5344 * where g' is the result of plugging in "subs" in each of the integer
5345 * divisions in g.
5347 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5348 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5350 isl_ctx *ctx;
5351 isl_int v;
5353 aff = isl_aff_cow(aff);
5354 if (!aff || !subs)
5355 return isl_aff_free(aff);
5357 ctx = isl_aff_get_ctx(aff);
5358 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5359 isl_die(ctx, isl_error_invalid,
5360 "spaces don't match", return isl_aff_free(aff));
5361 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5362 isl_die(ctx, isl_error_unsupported,
5363 "cannot handle divs yet", return isl_aff_free(aff));
5365 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5366 if (!aff->ls)
5367 return isl_aff_free(aff);
5369 aff->v = isl_vec_cow(aff->v);
5370 if (!aff->v)
5371 return isl_aff_free(aff);
5373 pos += isl_local_space_offset(aff->ls, type);
5375 isl_int_init(v);
5376 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5377 aff->v->size, subs->v->size, v);
5378 isl_int_clear(v);
5380 return aff;
5383 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5384 * expressions in "maff".
5386 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5387 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5388 __isl_keep isl_aff *subs)
5390 int i;
5392 maff = isl_multi_aff_cow(maff);
5393 if (!maff || !subs)
5394 return isl_multi_aff_free(maff);
5396 if (type == isl_dim_in)
5397 type = isl_dim_set;
5399 for (i = 0; i < maff->n; ++i) {
5400 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5401 type, pos, subs);
5402 if (!maff->u.p[i])
5403 return isl_multi_aff_free(maff);
5406 return maff;
5409 /* Plug in "subs" for dimension "type", "pos" of "pma".
5411 * pma is of the form
5413 * A_i(v) -> M_i(v)
5415 * while subs is of the form
5417 * v' = B_j(v) -> S_j
5419 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5420 * has a contribution in the result, in particular
5422 * C_ij(S_j) -> M_i(S_j)
5424 * Note that plugging in S_j in C_ij may also result in an empty set
5425 * and this contribution should simply be discarded.
5427 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5428 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5429 __isl_keep isl_pw_aff *subs)
5431 int i, j, n;
5432 isl_pw_multi_aff *res;
5434 if (!pma || !subs)
5435 return isl_pw_multi_aff_free(pma);
5437 n = pma->n * subs->n;
5438 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5440 for (i = 0; i < pma->n; ++i) {
5441 for (j = 0; j < subs->n; ++j) {
5442 isl_set *common;
5443 isl_multi_aff *res_ij;
5444 int empty;
5446 common = isl_set_intersect(
5447 isl_set_copy(pma->p[i].set),
5448 isl_set_copy(subs->p[j].set));
5449 common = isl_set_substitute(common,
5450 type, pos, subs->p[j].aff);
5451 empty = isl_set_plain_is_empty(common);
5452 if (empty < 0 || empty) {
5453 isl_set_free(common);
5454 if (empty < 0)
5455 goto error;
5456 continue;
5459 res_ij = isl_multi_aff_substitute(
5460 isl_multi_aff_copy(pma->p[i].maff),
5461 type, pos, subs->p[j].aff);
5463 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5467 isl_pw_multi_aff_free(pma);
5468 return res;
5469 error:
5470 isl_pw_multi_aff_free(pma);
5471 isl_pw_multi_aff_free(res);
5472 return NULL;
5475 /* Compute the preimage of a range of dimensions in the affine expression "src"
5476 * under "ma" and put the result in "dst". The number of dimensions in "src"
5477 * that precede the range is given by "n_before". The number of dimensions
5478 * in the range is given by the number of output dimensions of "ma".
5479 * The number of dimensions that follow the range is given by "n_after".
5480 * If "has_denom" is set (to one),
5481 * then "src" and "dst" have an extra initial denominator.
5482 * "n_div_ma" is the number of existentials in "ma"
5483 * "n_div_bset" is the number of existentials in "src"
5484 * The resulting "dst" (which is assumed to have been allocated by
5485 * the caller) contains coefficients for both sets of existentials,
5486 * first those in "ma" and then those in "src".
5487 * f, c1, c2 and g are temporary objects that have been initialized
5488 * by the caller.
5490 * Let src represent the expression
5492 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5494 * and let ma represent the expressions
5496 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5498 * We start out with the following expression for dst:
5500 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5502 * with the multiplication factor f initially equal to 1
5503 * and f \sum_i b_i v_i kept separately.
5504 * For each x_i that we substitute, we multiply the numerator
5505 * (and denominator) of dst by c_1 = m_i and add the numerator
5506 * of the x_i expression multiplied by c_2 = f b_i,
5507 * after removing the common factors of c_1 and c_2.
5508 * The multiplication factor f also needs to be multiplied by c_1
5509 * for the next x_j, j > i.
5511 void isl_seq_preimage(isl_int *dst, isl_int *src,
5512 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5513 int n_div_ma, int n_div_bmap,
5514 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5516 int i;
5517 int n_param, n_in, n_out;
5518 int o_dst, o_src;
5520 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5521 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5522 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5524 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5525 o_dst = o_src = has_denom + 1 + n_param + n_before;
5526 isl_seq_clr(dst + o_dst, n_in);
5527 o_dst += n_in;
5528 o_src += n_out;
5529 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5530 o_dst += n_after;
5531 o_src += n_after;
5532 isl_seq_clr(dst + o_dst, n_div_ma);
5533 o_dst += n_div_ma;
5534 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5536 isl_int_set_si(f, 1);
5538 for (i = 0; i < n_out; ++i) {
5539 int offset = has_denom + 1 + n_param + n_before + i;
5541 if (isl_int_is_zero(src[offset]))
5542 continue;
5543 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5544 isl_int_mul(c2, f, src[offset]);
5545 isl_int_gcd(g, c1, c2);
5546 isl_int_divexact(c1, c1, g);
5547 isl_int_divexact(c2, c2, g);
5549 isl_int_mul(f, f, c1);
5550 o_dst = has_denom;
5551 o_src = 1;
5552 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5553 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5554 o_dst += 1 + n_param;
5555 o_src += 1 + n_param;
5556 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5557 o_dst += n_before;
5558 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5559 c2, ma->u.p[i]->v->el + o_src, n_in);
5560 o_dst += n_in;
5561 o_src += n_in;
5562 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5563 o_dst += n_after;
5564 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5565 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5566 o_dst += n_div_ma;
5567 o_src += n_div_ma;
5568 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5569 if (has_denom)
5570 isl_int_mul(dst[0], dst[0], c1);
5574 /* Compute the pullback of "aff" by the function represented by "ma".
5575 * In other words, plug in "ma" in "aff". The result is an affine expression
5576 * defined over the domain space of "ma".
5578 * If "aff" is represented by
5580 * (a(p) + b x + c(divs))/d
5582 * and ma is represented by
5584 * x = D(p) + F(y) + G(divs')
5586 * then the result is
5588 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5590 * The divs in the local space of the input are similarly adjusted
5591 * through a call to isl_local_space_preimage_multi_aff.
5593 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5594 __isl_take isl_multi_aff *ma)
5596 isl_aff *res = NULL;
5597 isl_local_space *ls;
5598 int n_div_aff, n_div_ma;
5599 isl_int f, c1, c2, g;
5601 ma = isl_multi_aff_align_divs(ma);
5602 if (!aff || !ma)
5603 goto error;
5605 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5606 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5608 ls = isl_aff_get_domain_local_space(aff);
5609 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5610 res = isl_aff_alloc(ls);
5611 if (!res)
5612 goto error;
5614 isl_int_init(f);
5615 isl_int_init(c1);
5616 isl_int_init(c2);
5617 isl_int_init(g);
5619 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5620 f, c1, c2, g, 1);
5622 isl_int_clear(f);
5623 isl_int_clear(c1);
5624 isl_int_clear(c2);
5625 isl_int_clear(g);
5627 isl_aff_free(aff);
5628 isl_multi_aff_free(ma);
5629 res = isl_aff_normalize(res);
5630 return res;
5631 error:
5632 isl_aff_free(aff);
5633 isl_multi_aff_free(ma);
5634 isl_aff_free(res);
5635 return NULL;
5638 /* Compute the pullback of "aff1" by the function represented by "aff2".
5639 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5640 * defined over the domain space of "aff1".
5642 * The domain of "aff1" should match the range of "aff2", which means
5643 * that it should be single-dimensional.
5645 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5646 __isl_take isl_aff *aff2)
5648 isl_multi_aff *ma;
5650 ma = isl_multi_aff_from_aff(aff2);
5651 return isl_aff_pullback_multi_aff(aff1, ma);
5654 /* Compute the pullback of "ma1" by the function represented by "ma2".
5655 * In other words, plug in "ma2" in "ma1".
5657 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5659 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5660 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5662 int i;
5663 isl_space *space = NULL;
5665 ma2 = isl_multi_aff_align_divs(ma2);
5666 ma1 = isl_multi_aff_cow(ma1);
5667 if (!ma1 || !ma2)
5668 goto error;
5670 space = isl_space_join(isl_multi_aff_get_space(ma2),
5671 isl_multi_aff_get_space(ma1));
5673 for (i = 0; i < ma1->n; ++i) {
5674 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5675 isl_multi_aff_copy(ma2));
5676 if (!ma1->u.p[i])
5677 goto error;
5680 ma1 = isl_multi_aff_reset_space(ma1, space);
5681 isl_multi_aff_free(ma2);
5682 return ma1;
5683 error:
5684 isl_space_free(space);
5685 isl_multi_aff_free(ma2);
5686 isl_multi_aff_free(ma1);
5687 return NULL;
5690 /* Compute the pullback of "ma1" by the function represented by "ma2".
5691 * In other words, plug in "ma2" in "ma1".
5693 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5694 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5696 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5697 &isl_multi_aff_pullback_multi_aff_aligned);
5700 /* Extend the local space of "dst" to include the divs
5701 * in the local space of "src".
5703 * If "src" does not have any divs or if the local spaces of "dst" and
5704 * "src" are the same, then no extension is required.
5706 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5707 __isl_keep isl_aff *src)
5709 isl_ctx *ctx;
5710 int src_n_div, dst_n_div;
5711 int *exp1 = NULL;
5712 int *exp2 = NULL;
5713 isl_bool equal;
5714 isl_mat *div;
5716 if (!src || !dst)
5717 return isl_aff_free(dst);
5719 ctx = isl_aff_get_ctx(src);
5720 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5721 if (equal < 0)
5722 return isl_aff_free(dst);
5723 if (!equal)
5724 isl_die(ctx, isl_error_invalid,
5725 "spaces don't match", goto error);
5727 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5728 if (src_n_div == 0)
5729 return dst;
5730 equal = isl_local_space_is_equal(src->ls, dst->ls);
5731 if (equal < 0)
5732 return isl_aff_free(dst);
5733 if (equal)
5734 return dst;
5736 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5737 exp1 = isl_alloc_array(ctx, int, src_n_div);
5738 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5739 if (!exp1 || (dst_n_div && !exp2))
5740 goto error;
5742 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5743 dst = isl_aff_expand_divs(dst, div, exp2);
5744 free(exp1);
5745 free(exp2);
5747 return dst;
5748 error:
5749 free(exp1);
5750 free(exp2);
5751 return isl_aff_free(dst);
5754 /* Adjust the local spaces of the affine expressions in "maff"
5755 * such that they all have the save divs.
5757 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5758 __isl_take isl_multi_aff *maff)
5760 int i;
5762 if (!maff)
5763 return NULL;
5764 if (maff->n == 0)
5765 return maff;
5766 maff = isl_multi_aff_cow(maff);
5767 if (!maff)
5768 return NULL;
5770 for (i = 1; i < maff->n; ++i)
5771 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5772 for (i = 1; i < maff->n; ++i) {
5773 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5774 if (!maff->u.p[i])
5775 return isl_multi_aff_free(maff);
5778 return maff;
5781 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5783 aff = isl_aff_cow(aff);
5784 if (!aff)
5785 return NULL;
5787 aff->ls = isl_local_space_lift(aff->ls);
5788 if (!aff->ls)
5789 return isl_aff_free(aff);
5791 return aff;
5794 /* Lift "maff" to a space with extra dimensions such that the result
5795 * has no more existentially quantified variables.
5796 * If "ls" is not NULL, then *ls is assigned the local space that lies
5797 * at the basis of the lifting applied to "maff".
5799 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5800 __isl_give isl_local_space **ls)
5802 int i;
5803 isl_space *space;
5804 unsigned n_div;
5806 if (ls)
5807 *ls = NULL;
5809 if (!maff)
5810 return NULL;
5812 if (maff->n == 0) {
5813 if (ls) {
5814 isl_space *space = isl_multi_aff_get_domain_space(maff);
5815 *ls = isl_local_space_from_space(space);
5816 if (!*ls)
5817 return isl_multi_aff_free(maff);
5819 return maff;
5822 maff = isl_multi_aff_cow(maff);
5823 maff = isl_multi_aff_align_divs(maff);
5824 if (!maff)
5825 return NULL;
5827 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5828 space = isl_multi_aff_get_space(maff);
5829 space = isl_space_lift(isl_space_domain(space), n_div);
5830 space = isl_space_extend_domain_with_range(space,
5831 isl_multi_aff_get_space(maff));
5832 if (!space)
5833 return isl_multi_aff_free(maff);
5834 isl_space_free(maff->space);
5835 maff->space = space;
5837 if (ls) {
5838 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5839 if (!*ls)
5840 return isl_multi_aff_free(maff);
5843 for (i = 0; i < maff->n; ++i) {
5844 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5845 if (!maff->u.p[i])
5846 goto error;
5849 return maff;
5850 error:
5851 if (ls)
5852 isl_local_space_free(*ls);
5853 return isl_multi_aff_free(maff);
5857 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5859 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5860 __isl_keep isl_pw_multi_aff *pma, int pos)
5862 int i;
5863 int n_out;
5864 isl_space *space;
5865 isl_pw_aff *pa;
5867 if (!pma)
5868 return NULL;
5870 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5871 if (pos < 0 || pos >= n_out)
5872 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5873 "index out of bounds", return NULL);
5875 space = isl_pw_multi_aff_get_space(pma);
5876 space = isl_space_drop_dims(space, isl_dim_out,
5877 pos + 1, n_out - pos - 1);
5878 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5880 pa = isl_pw_aff_alloc_size(space, pma->n);
5881 for (i = 0; i < pma->n; ++i) {
5882 isl_aff *aff;
5883 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5884 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5887 return pa;
5890 /* Return an isl_pw_multi_aff with the given "set" as domain and
5891 * an unnamed zero-dimensional range.
5893 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5894 __isl_take isl_set *set)
5896 isl_multi_aff *ma;
5897 isl_space *space;
5899 space = isl_set_get_space(set);
5900 space = isl_space_from_domain(space);
5901 ma = isl_multi_aff_zero(space);
5902 return isl_pw_multi_aff_alloc(set, ma);
5905 /* Add an isl_pw_multi_aff with the given "set" as domain and
5906 * an unnamed zero-dimensional range to *user.
5908 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5909 void *user)
5911 isl_union_pw_multi_aff **upma = user;
5912 isl_pw_multi_aff *pma;
5914 pma = isl_pw_multi_aff_from_domain(set);
5915 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5917 return isl_stat_ok;
5920 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5921 * an unnamed zero-dimensional range.
5923 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5924 __isl_take isl_union_set *uset)
5926 isl_space *space;
5927 isl_union_pw_multi_aff *upma;
5929 if (!uset)
5930 return NULL;
5932 space = isl_union_set_get_space(uset);
5933 upma = isl_union_pw_multi_aff_empty(space);
5935 if (isl_union_set_foreach_set(uset,
5936 &add_pw_multi_aff_from_domain, &upma) < 0)
5937 goto error;
5939 isl_union_set_free(uset);
5940 return upma;
5941 error:
5942 isl_union_set_free(uset);
5943 isl_union_pw_multi_aff_free(upma);
5944 return NULL;
5947 /* Convert "pma" to an isl_map and add it to *umap.
5949 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5950 void *user)
5952 isl_union_map **umap = user;
5953 isl_map *map;
5955 map = isl_map_from_pw_multi_aff(pma);
5956 *umap = isl_union_map_add_map(*umap, map);
5958 return isl_stat_ok;
5961 /* Construct a union map mapping the domain of the union
5962 * piecewise multi-affine expression to its range, with each dimension
5963 * in the range equated to the corresponding affine expression on its cell.
5965 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5966 __isl_take isl_union_pw_multi_aff *upma)
5968 isl_space *space;
5969 isl_union_map *umap;
5971 if (!upma)
5972 return NULL;
5974 space = isl_union_pw_multi_aff_get_space(upma);
5975 umap = isl_union_map_empty(space);
5977 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5978 &map_from_pw_multi_aff, &umap) < 0)
5979 goto error;
5981 isl_union_pw_multi_aff_free(upma);
5982 return umap;
5983 error:
5984 isl_union_pw_multi_aff_free(upma);
5985 isl_union_map_free(umap);
5986 return NULL;
5989 /* Local data for bin_entry and the callback "fn".
5991 struct isl_union_pw_multi_aff_bin_data {
5992 isl_union_pw_multi_aff *upma2;
5993 isl_union_pw_multi_aff *res;
5994 isl_pw_multi_aff *pma;
5995 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5998 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5999 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6001 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6003 struct isl_union_pw_multi_aff_bin_data *data = user;
6004 isl_stat r;
6006 data->pma = pma;
6007 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6008 data->fn, data);
6009 isl_pw_multi_aff_free(pma);
6011 return r;
6014 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6015 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6016 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6017 * as *entry. The callback should adjust data->res if desired.
6019 static __isl_give isl_union_pw_multi_aff *bin_op(
6020 __isl_take isl_union_pw_multi_aff *upma1,
6021 __isl_take isl_union_pw_multi_aff *upma2,
6022 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6024 isl_space *space;
6025 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6027 space = isl_union_pw_multi_aff_get_space(upma2);
6028 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6029 space = isl_union_pw_multi_aff_get_space(upma1);
6030 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6032 if (!upma1 || !upma2)
6033 goto error;
6035 data.upma2 = upma2;
6036 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6037 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6038 &bin_entry, &data) < 0)
6039 goto error;
6041 isl_union_pw_multi_aff_free(upma1);
6042 isl_union_pw_multi_aff_free(upma2);
6043 return data.res;
6044 error:
6045 isl_union_pw_multi_aff_free(upma1);
6046 isl_union_pw_multi_aff_free(upma2);
6047 isl_union_pw_multi_aff_free(data.res);
6048 return NULL;
6051 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6052 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6054 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6055 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6057 isl_space *space;
6059 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6060 isl_pw_multi_aff_get_space(pma2));
6061 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6062 &isl_multi_aff_range_product);
6065 /* Given two isl_pw_multi_affs A -> B and C -> D,
6066 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6068 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6069 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6071 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6072 &pw_multi_aff_range_product);
6075 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6076 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6078 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6079 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6081 isl_space *space;
6083 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6084 isl_pw_multi_aff_get_space(pma2));
6085 space = isl_space_flatten_range(space);
6086 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6087 &isl_multi_aff_flat_range_product);
6090 /* Given two isl_pw_multi_affs A -> B and C -> D,
6091 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6093 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6094 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6096 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6097 &pw_multi_aff_flat_range_product);
6100 /* If data->pma and "pma2" have the same domain space, then compute
6101 * their flat range product and the result to data->res.
6103 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6104 void *user)
6106 struct isl_union_pw_multi_aff_bin_data *data = user;
6108 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6109 pma2->dim, isl_dim_in)) {
6110 isl_pw_multi_aff_free(pma2);
6111 return isl_stat_ok;
6114 pma2 = isl_pw_multi_aff_flat_range_product(
6115 isl_pw_multi_aff_copy(data->pma), pma2);
6117 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6119 return isl_stat_ok;
6122 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6123 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6125 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6126 __isl_take isl_union_pw_multi_aff *upma1,
6127 __isl_take isl_union_pw_multi_aff *upma2)
6129 return bin_op(upma1, upma2, &flat_range_product_entry);
6132 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6133 * The parameters are assumed to have been aligned.
6135 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6136 * except that it works on two different isl_pw_* types.
6138 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6139 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6140 __isl_take isl_pw_aff *pa)
6142 int i, j, n;
6143 isl_pw_multi_aff *res = NULL;
6145 if (!pma || !pa)
6146 goto error;
6148 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6149 pa->dim, isl_dim_in))
6150 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6151 "domains don't match", goto error);
6152 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6153 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6154 "index out of bounds", goto error);
6156 n = pma->n * pa->n;
6157 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6159 for (i = 0; i < pma->n; ++i) {
6160 for (j = 0; j < pa->n; ++j) {
6161 isl_set *common;
6162 isl_multi_aff *res_ij;
6163 int empty;
6165 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6166 isl_set_copy(pa->p[j].set));
6167 empty = isl_set_plain_is_empty(common);
6168 if (empty < 0 || empty) {
6169 isl_set_free(common);
6170 if (empty < 0)
6171 goto error;
6172 continue;
6175 res_ij = isl_multi_aff_set_aff(
6176 isl_multi_aff_copy(pma->p[i].maff), pos,
6177 isl_aff_copy(pa->p[j].aff));
6178 res_ij = isl_multi_aff_gist(res_ij,
6179 isl_set_copy(common));
6181 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6185 isl_pw_multi_aff_free(pma);
6186 isl_pw_aff_free(pa);
6187 return res;
6188 error:
6189 isl_pw_multi_aff_free(pma);
6190 isl_pw_aff_free(pa);
6191 return isl_pw_multi_aff_free(res);
6194 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6196 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6197 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6198 __isl_take isl_pw_aff *pa)
6200 isl_bool equal_params;
6202 if (!pma || !pa)
6203 goto error;
6204 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6205 if (equal_params < 0)
6206 goto error;
6207 if (equal_params)
6208 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6209 if (!isl_space_has_named_params(pma->dim) ||
6210 !isl_space_has_named_params(pa->dim))
6211 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6212 "unaligned unnamed parameters", goto error);
6213 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6214 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6215 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6216 error:
6217 isl_pw_multi_aff_free(pma);
6218 isl_pw_aff_free(pa);
6219 return NULL;
6222 /* Do the parameters of "pa" match those of "space"?
6224 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6225 __isl_keep isl_space *space)
6227 isl_space *pa_space;
6228 isl_bool match;
6230 if (!pa || !space)
6231 return isl_bool_error;
6233 pa_space = isl_pw_aff_get_space(pa);
6235 match = isl_space_has_equal_params(space, pa_space);
6237 isl_space_free(pa_space);
6238 return match;
6241 /* Check that the domain space of "pa" matches "space".
6243 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6244 __isl_keep isl_space *space)
6246 isl_space *pa_space;
6247 isl_bool match;
6249 if (!pa || !space)
6250 return isl_stat_error;
6252 pa_space = isl_pw_aff_get_space(pa);
6254 match = isl_space_has_equal_params(space, pa_space);
6255 if (match < 0)
6256 goto error;
6257 if (!match)
6258 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6259 "parameters don't match", goto error);
6260 match = isl_space_tuple_is_equal(space, isl_dim_in,
6261 pa_space, isl_dim_in);
6262 if (match < 0)
6263 goto error;
6264 if (!match)
6265 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6266 "domains don't match", goto error);
6267 isl_space_free(pa_space);
6268 return isl_stat_ok;
6269 error:
6270 isl_space_free(pa_space);
6271 return isl_stat_error;
6274 #undef BASE
6275 #define BASE pw_aff
6276 #undef DOMBASE
6277 #define DOMBASE set
6279 #include <isl_multi_explicit_domain.c>
6280 #include <isl_multi_pw_aff_explicit_domain.c>
6281 #include <isl_multi_templ.c>
6282 #include <isl_multi_apply_set.c>
6283 #include <isl_multi_coalesce.c>
6284 #include <isl_multi_dims.c>
6285 #include <isl_multi_gist.c>
6286 #include <isl_multi_hash.c>
6287 #include <isl_multi_align_set.c>
6288 #include <isl_multi_intersect.c>
6290 /* Does "mpa" have a non-trivial explicit domain?
6292 * The explicit domain, if present, is trivial if it represents
6293 * an (obviously) universe set.
6295 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6296 __isl_keep isl_multi_pw_aff *mpa)
6298 if (!mpa)
6299 return isl_bool_error;
6300 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6301 return isl_bool_false;
6302 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6305 /* Scale the elements of "pma" by the corresponding elements of "mv".
6307 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6308 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6310 int i;
6311 isl_bool equal_params;
6313 pma = isl_pw_multi_aff_cow(pma);
6314 if (!pma || !mv)
6315 goto error;
6316 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6317 mv->space, isl_dim_set))
6318 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6319 "spaces don't match", goto error);
6320 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6321 if (equal_params < 0)
6322 goto error;
6323 if (!equal_params) {
6324 pma = isl_pw_multi_aff_align_params(pma,
6325 isl_multi_val_get_space(mv));
6326 mv = isl_multi_val_align_params(mv,
6327 isl_pw_multi_aff_get_space(pma));
6328 if (!pma || !mv)
6329 goto error;
6332 for (i = 0; i < pma->n; ++i) {
6333 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6334 isl_multi_val_copy(mv));
6335 if (!pma->p[i].maff)
6336 goto error;
6339 isl_multi_val_free(mv);
6340 return pma;
6341 error:
6342 isl_multi_val_free(mv);
6343 isl_pw_multi_aff_free(pma);
6344 return NULL;
6347 /* This function is called for each entry of an isl_union_pw_multi_aff.
6348 * If the space of the entry matches that of data->mv,
6349 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6350 * Otherwise, return an empty isl_pw_multi_aff.
6352 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6353 __isl_take isl_pw_multi_aff *pma, void *user)
6355 isl_multi_val *mv = user;
6357 if (!pma)
6358 return NULL;
6359 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6360 mv->space, isl_dim_set)) {
6361 isl_space *space = isl_pw_multi_aff_get_space(pma);
6362 isl_pw_multi_aff_free(pma);
6363 return isl_pw_multi_aff_empty(space);
6366 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6369 /* Scale the elements of "upma" by the corresponding elements of "mv",
6370 * for those entries that match the space of "mv".
6372 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6373 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6375 upma = isl_union_pw_multi_aff_align_params(upma,
6376 isl_multi_val_get_space(mv));
6377 mv = isl_multi_val_align_params(mv,
6378 isl_union_pw_multi_aff_get_space(upma));
6379 if (!upma || !mv)
6380 goto error;
6382 return isl_union_pw_multi_aff_transform(upma,
6383 &union_pw_multi_aff_scale_multi_val_entry, mv);
6385 isl_multi_val_free(mv);
6386 return upma;
6387 error:
6388 isl_multi_val_free(mv);
6389 isl_union_pw_multi_aff_free(upma);
6390 return NULL;
6393 /* Construct and return a piecewise multi affine expression
6394 * in the given space with value zero in each of the output dimensions and
6395 * a universe domain.
6397 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6399 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6402 /* Construct and return a piecewise multi affine expression
6403 * that is equal to the given piecewise affine expression.
6405 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6406 __isl_take isl_pw_aff *pa)
6408 int i;
6409 isl_space *space;
6410 isl_pw_multi_aff *pma;
6412 if (!pa)
6413 return NULL;
6415 space = isl_pw_aff_get_space(pa);
6416 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6418 for (i = 0; i < pa->n; ++i) {
6419 isl_set *set;
6420 isl_multi_aff *ma;
6422 set = isl_set_copy(pa->p[i].set);
6423 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6424 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6427 isl_pw_aff_free(pa);
6428 return pma;
6431 /* Construct a set or map mapping the shared (parameter) domain
6432 * of the piecewise affine expressions to the range of "mpa"
6433 * with each dimension in the range equated to the
6434 * corresponding piecewise affine expression.
6436 static __isl_give isl_map *map_from_multi_pw_aff(
6437 __isl_take isl_multi_pw_aff *mpa)
6439 int i;
6440 isl_space *space;
6441 isl_map *map;
6443 if (!mpa)
6444 return NULL;
6446 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6447 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6448 "invalid space", goto error);
6450 space = isl_multi_pw_aff_get_domain_space(mpa);
6451 map = isl_map_universe(isl_space_from_domain(space));
6453 for (i = 0; i < mpa->n; ++i) {
6454 isl_pw_aff *pa;
6455 isl_map *map_i;
6457 pa = isl_pw_aff_copy(mpa->u.p[i]);
6458 map_i = map_from_pw_aff(pa);
6460 map = isl_map_flat_range_product(map, map_i);
6463 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6465 isl_multi_pw_aff_free(mpa);
6466 return map;
6467 error:
6468 isl_multi_pw_aff_free(mpa);
6469 return NULL;
6472 /* Construct a map mapping the shared domain
6473 * of the piecewise affine expressions to the range of "mpa"
6474 * with each dimension in the range equated to the
6475 * corresponding piecewise affine expression.
6477 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6479 if (!mpa)
6480 return NULL;
6481 if (isl_space_is_set(mpa->space))
6482 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6483 "space of input is not a map", goto error);
6485 return map_from_multi_pw_aff(mpa);
6486 error:
6487 isl_multi_pw_aff_free(mpa);
6488 return NULL;
6491 /* Construct a set mapping the shared parameter domain
6492 * of the piecewise affine expressions to the space of "mpa"
6493 * with each dimension in the range equated to the
6494 * corresponding piecewise affine expression.
6496 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6498 if (!mpa)
6499 return NULL;
6500 if (!isl_space_is_set(mpa->space))
6501 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6502 "space of input is not a set", goto error);
6504 return map_from_multi_pw_aff(mpa);
6505 error:
6506 isl_multi_pw_aff_free(mpa);
6507 return NULL;
6510 /* Construct and return a piecewise multi affine expression
6511 * that is equal to the given multi piecewise affine expression
6512 * on the shared domain of the piecewise affine expressions,
6513 * in the special case of a 0D multi piecewise affine expression.
6515 * Create a piecewise multi affine expression with the explicit domain of
6516 * the 0D multi piecewise affine expression as domain.
6518 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6519 __isl_take isl_multi_pw_aff *mpa)
6521 isl_space *space;
6522 isl_set *dom;
6523 isl_multi_aff *ma;
6525 space = isl_multi_pw_aff_get_space(mpa);
6526 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6527 isl_multi_pw_aff_free(mpa);
6529 ma = isl_multi_aff_zero(space);
6530 return isl_pw_multi_aff_alloc(dom, ma);
6533 /* Construct and return a piecewise multi affine expression
6534 * that is equal to the given multi piecewise affine expression
6535 * on the shared domain of the piecewise affine expressions.
6537 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6538 __isl_take isl_multi_pw_aff *mpa)
6540 int i;
6541 isl_space *space;
6542 isl_pw_aff *pa;
6543 isl_pw_multi_aff *pma;
6545 if (!mpa)
6546 return NULL;
6548 if (mpa->n == 0)
6549 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6551 space = isl_multi_pw_aff_get_space(mpa);
6552 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6553 pma = isl_pw_multi_aff_from_pw_aff(pa);
6555 for (i = 1; i < mpa->n; ++i) {
6556 isl_pw_multi_aff *pma_i;
6558 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6559 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6560 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6563 pma = isl_pw_multi_aff_reset_space(pma, space);
6565 isl_multi_pw_aff_free(mpa);
6566 return pma;
6569 /* Construct and return a multi piecewise affine expression
6570 * that is equal to the given multi affine expression.
6572 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6573 __isl_take isl_multi_aff *ma)
6575 int i, n;
6576 isl_multi_pw_aff *mpa;
6578 if (!ma)
6579 return NULL;
6581 n = isl_multi_aff_dim(ma, isl_dim_out);
6582 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6584 for (i = 0; i < n; ++i) {
6585 isl_pw_aff *pa;
6587 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6588 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6591 isl_multi_aff_free(ma);
6592 return mpa;
6595 /* Construct and return a multi piecewise affine expression
6596 * that is equal to the given piecewise multi affine expression.
6598 * If the resulting multi piecewise affine expression has
6599 * an explicit domain, then assign it the domain of the input.
6600 * In other cases, the domain is stored in the individual elements.
6602 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6603 __isl_take isl_pw_multi_aff *pma)
6605 int i, n;
6606 isl_space *space;
6607 isl_multi_pw_aff *mpa;
6609 if (!pma)
6610 return NULL;
6612 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6613 space = isl_pw_multi_aff_get_space(pma);
6614 mpa = isl_multi_pw_aff_alloc(space);
6616 for (i = 0; i < n; ++i) {
6617 isl_pw_aff *pa;
6619 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6620 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6622 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6623 isl_set *dom;
6625 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6626 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6629 isl_pw_multi_aff_free(pma);
6630 return mpa;
6633 /* Do "pa1" and "pa2" represent the same function?
6635 * We first check if they are obviously equal.
6636 * If not, we convert them to maps and check if those are equal.
6638 * If "pa1" or "pa2" contain any NaNs, then they are considered
6639 * not to be the same. A NaN is not equal to anything, not even
6640 * to another NaN.
6642 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6643 __isl_keep isl_pw_aff *pa2)
6645 isl_bool equal;
6646 isl_bool has_nan;
6647 isl_map *map1, *map2;
6649 if (!pa1 || !pa2)
6650 return isl_bool_error;
6652 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6653 if (equal < 0 || equal)
6654 return equal;
6655 has_nan = either_involves_nan(pa1, pa2);
6656 if (has_nan < 0)
6657 return isl_bool_error;
6658 if (has_nan)
6659 return isl_bool_false;
6661 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6662 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6663 equal = isl_map_is_equal(map1, map2);
6664 isl_map_free(map1);
6665 isl_map_free(map2);
6667 return equal;
6670 /* Do "mpa1" and "mpa2" represent the same function?
6672 * Note that we cannot convert the entire isl_multi_pw_aff
6673 * to a map because the domains of the piecewise affine expressions
6674 * may not be the same.
6676 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6677 __isl_keep isl_multi_pw_aff *mpa2)
6679 int i;
6680 isl_bool equal, equal_params;
6682 if (!mpa1 || !mpa2)
6683 return isl_bool_error;
6685 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6686 if (equal_params < 0)
6687 return isl_bool_error;
6688 if (!equal_params) {
6689 if (!isl_space_has_named_params(mpa1->space))
6690 return isl_bool_false;
6691 if (!isl_space_has_named_params(mpa2->space))
6692 return isl_bool_false;
6693 mpa1 = isl_multi_pw_aff_copy(mpa1);
6694 mpa2 = isl_multi_pw_aff_copy(mpa2);
6695 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6696 isl_multi_pw_aff_get_space(mpa2));
6697 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6698 isl_multi_pw_aff_get_space(mpa1));
6699 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6700 isl_multi_pw_aff_free(mpa1);
6701 isl_multi_pw_aff_free(mpa2);
6702 return equal;
6705 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6706 if (equal < 0 || !equal)
6707 return equal;
6709 for (i = 0; i < mpa1->n; ++i) {
6710 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6711 if (equal < 0 || !equal)
6712 return equal;
6715 return isl_bool_true;
6718 /* Do "pma1" and "pma2" represent the same function?
6720 * First check if they are obviously equal.
6721 * If not, then convert them to maps and check if those are equal.
6723 * If "pa1" or "pa2" contain any NaNs, then they are considered
6724 * not to be the same. A NaN is not equal to anything, not even
6725 * to another NaN.
6727 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6728 __isl_keep isl_pw_multi_aff *pma2)
6730 isl_bool equal;
6731 isl_bool has_nan;
6732 isl_map *map1, *map2;
6734 if (!pma1 || !pma2)
6735 return isl_bool_error;
6737 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6738 if (equal < 0 || equal)
6739 return equal;
6740 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6741 if (has_nan >= 0 && !has_nan)
6742 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6743 if (has_nan < 0 || has_nan)
6744 return isl_bool_not(has_nan);
6746 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6747 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6748 equal = isl_map_is_equal(map1, map2);
6749 isl_map_free(map1);
6750 isl_map_free(map2);
6752 return equal;
6755 /* Compute the pullback of "mpa" by the function represented by "ma".
6756 * In other words, plug in "ma" in "mpa".
6758 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6760 * If "mpa" has an explicit domain, then it is this domain
6761 * that needs to undergo a pullback, i.e., a preimage.
6763 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6764 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6766 int i;
6767 isl_space *space = NULL;
6769 mpa = isl_multi_pw_aff_cow(mpa);
6770 if (!mpa || !ma)
6771 goto error;
6773 space = isl_space_join(isl_multi_aff_get_space(ma),
6774 isl_multi_pw_aff_get_space(mpa));
6775 if (!space)
6776 goto error;
6778 for (i = 0; i < mpa->n; ++i) {
6779 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6780 isl_multi_aff_copy(ma));
6781 if (!mpa->u.p[i])
6782 goto error;
6784 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6785 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6786 isl_multi_aff_copy(ma));
6787 if (!mpa->u.dom)
6788 goto error;
6791 isl_multi_aff_free(ma);
6792 isl_space_free(mpa->space);
6793 mpa->space = space;
6794 return mpa;
6795 error:
6796 isl_space_free(space);
6797 isl_multi_pw_aff_free(mpa);
6798 isl_multi_aff_free(ma);
6799 return NULL;
6802 /* Compute the pullback of "mpa" by the function represented by "ma".
6803 * In other words, plug in "ma" in "mpa".
6805 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6806 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6808 isl_bool equal_params;
6810 if (!mpa || !ma)
6811 goto error;
6812 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6813 if (equal_params < 0)
6814 goto error;
6815 if (equal_params)
6816 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6817 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6818 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6819 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6820 error:
6821 isl_multi_pw_aff_free(mpa);
6822 isl_multi_aff_free(ma);
6823 return NULL;
6826 /* Compute the pullback of "mpa" by the function represented by "pma".
6827 * In other words, plug in "pma" in "mpa".
6829 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6831 * If "mpa" has an explicit domain, then it is this domain
6832 * that needs to undergo a pullback, i.e., a preimage.
6834 static __isl_give isl_multi_pw_aff *
6835 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6836 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6838 int i;
6839 isl_space *space = NULL;
6841 mpa = isl_multi_pw_aff_cow(mpa);
6842 if (!mpa || !pma)
6843 goto error;
6845 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6846 isl_multi_pw_aff_get_space(mpa));
6848 for (i = 0; i < mpa->n; ++i) {
6849 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6850 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6851 if (!mpa->u.p[i])
6852 goto error;
6854 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6855 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6856 isl_pw_multi_aff_copy(pma));
6857 if (!mpa->u.dom)
6858 goto error;
6861 isl_pw_multi_aff_free(pma);
6862 isl_space_free(mpa->space);
6863 mpa->space = space;
6864 return mpa;
6865 error:
6866 isl_space_free(space);
6867 isl_multi_pw_aff_free(mpa);
6868 isl_pw_multi_aff_free(pma);
6869 return NULL;
6872 /* Compute the pullback of "mpa" by the function represented by "pma".
6873 * In other words, plug in "pma" in "mpa".
6875 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6876 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6878 isl_bool equal_params;
6880 if (!mpa || !pma)
6881 goto error;
6882 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6883 if (equal_params < 0)
6884 goto error;
6885 if (equal_params)
6886 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6887 mpa = isl_multi_pw_aff_align_params(mpa,
6888 isl_pw_multi_aff_get_space(pma));
6889 pma = isl_pw_multi_aff_align_params(pma,
6890 isl_multi_pw_aff_get_space(mpa));
6891 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6892 error:
6893 isl_multi_pw_aff_free(mpa);
6894 isl_pw_multi_aff_free(pma);
6895 return NULL;
6898 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6899 * with the domain of "aff". The domain of the result is the same
6900 * as that of "mpa".
6901 * "mpa" and "aff" are assumed to have been aligned.
6903 * We first extract the parametric constant from "aff", defined
6904 * over the correct domain.
6905 * Then we add the appropriate combinations of the members of "mpa".
6906 * Finally, we add the integer divisions through recursive calls.
6908 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6909 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6911 int i, n_in, n_div;
6912 isl_space *space;
6913 isl_val *v;
6914 isl_pw_aff *pa;
6915 isl_aff *tmp;
6917 n_in = isl_aff_dim(aff, isl_dim_in);
6918 n_div = isl_aff_dim(aff, isl_dim_div);
6920 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6921 tmp = isl_aff_copy(aff);
6922 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6923 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6924 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6925 isl_space_dim(space, isl_dim_set));
6926 tmp = isl_aff_reset_domain_space(tmp, space);
6927 pa = isl_pw_aff_from_aff(tmp);
6929 for (i = 0; i < n_in; ++i) {
6930 isl_pw_aff *pa_i;
6932 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6933 continue;
6934 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6935 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6936 pa_i = isl_pw_aff_scale_val(pa_i, v);
6937 pa = isl_pw_aff_add(pa, pa_i);
6940 for (i = 0; i < n_div; ++i) {
6941 isl_aff *div;
6942 isl_pw_aff *pa_i;
6944 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6945 continue;
6946 div = isl_aff_get_div(aff, i);
6947 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6948 isl_multi_pw_aff_copy(mpa), div);
6949 pa_i = isl_pw_aff_floor(pa_i);
6950 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6951 pa_i = isl_pw_aff_scale_val(pa_i, v);
6952 pa = isl_pw_aff_add(pa, pa_i);
6955 isl_multi_pw_aff_free(mpa);
6956 isl_aff_free(aff);
6958 return pa;
6961 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6962 * with the domain of "aff". The domain of the result is the same
6963 * as that of "mpa".
6965 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6966 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6968 isl_bool equal_params;
6970 if (!aff || !mpa)
6971 goto error;
6972 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6973 if (equal_params < 0)
6974 goto error;
6975 if (equal_params)
6976 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6978 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6979 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6981 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6982 error:
6983 isl_aff_free(aff);
6984 isl_multi_pw_aff_free(mpa);
6985 return NULL;
6988 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6989 * with the domain of "pa". The domain of the result is the same
6990 * as that of "mpa".
6991 * "mpa" and "pa" are assumed to have been aligned.
6993 * We consider each piece in turn. Note that the domains of the
6994 * pieces are assumed to be disjoint and they remain disjoint
6995 * after taking the preimage (over the same function).
6997 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6998 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7000 isl_space *space;
7001 isl_pw_aff *res;
7002 int i;
7004 if (!mpa || !pa)
7005 goto error;
7007 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7008 isl_pw_aff_get_space(pa));
7009 res = isl_pw_aff_empty(space);
7011 for (i = 0; i < pa->n; ++i) {
7012 isl_pw_aff *pa_i;
7013 isl_set *domain;
7015 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7016 isl_multi_pw_aff_copy(mpa),
7017 isl_aff_copy(pa->p[i].aff));
7018 domain = isl_set_copy(pa->p[i].set);
7019 domain = isl_set_preimage_multi_pw_aff(domain,
7020 isl_multi_pw_aff_copy(mpa));
7021 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7022 res = isl_pw_aff_add_disjoint(res, pa_i);
7025 isl_pw_aff_free(pa);
7026 isl_multi_pw_aff_free(mpa);
7027 return res;
7028 error:
7029 isl_pw_aff_free(pa);
7030 isl_multi_pw_aff_free(mpa);
7031 return NULL;
7034 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7035 * with the domain of "pa". The domain of the result is the same
7036 * as that of "mpa".
7038 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7039 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7041 isl_bool equal_params;
7043 if (!pa || !mpa)
7044 goto error;
7045 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7046 if (equal_params < 0)
7047 goto error;
7048 if (equal_params)
7049 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7051 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7052 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7054 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7055 error:
7056 isl_pw_aff_free(pa);
7057 isl_multi_pw_aff_free(mpa);
7058 return NULL;
7061 /* Compute the pullback of "pa" by the function represented by "mpa".
7062 * In other words, plug in "mpa" in "pa".
7063 * "pa" and "mpa" are assumed to have been aligned.
7065 * The pullback is computed by applying "pa" to "mpa".
7067 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7068 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7070 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7073 /* Compute the pullback of "pa" by the function represented by "mpa".
7074 * In other words, plug in "mpa" in "pa".
7076 * The pullback is computed by applying "pa" to "mpa".
7078 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7079 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7081 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7084 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7085 * In other words, plug in "mpa2" in "mpa1".
7087 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7089 * We pullback each member of "mpa1" in turn.
7091 * If "mpa1" has an explicit domain, then it is this domain
7092 * that needs to undergo a pullback instead, i.e., a preimage.
7094 static __isl_give isl_multi_pw_aff *
7095 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7096 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7098 int i;
7099 isl_space *space = NULL;
7101 mpa1 = isl_multi_pw_aff_cow(mpa1);
7102 if (!mpa1 || !mpa2)
7103 goto error;
7105 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7106 isl_multi_pw_aff_get_space(mpa1));
7108 for (i = 0; i < mpa1->n; ++i) {
7109 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7110 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7111 if (!mpa1->u.p[i])
7112 goto error;
7115 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7116 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7117 isl_multi_pw_aff_copy(mpa2));
7118 if (!mpa1->u.dom)
7119 goto error;
7121 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7123 isl_multi_pw_aff_free(mpa2);
7124 return mpa1;
7125 error:
7126 isl_space_free(space);
7127 isl_multi_pw_aff_free(mpa1);
7128 isl_multi_pw_aff_free(mpa2);
7129 return NULL;
7132 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7133 * In other words, plug in "mpa2" in "mpa1".
7135 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7136 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7138 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7139 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7142 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7143 * of "mpa1" and "mpa2" live in the same space, construct map space
7144 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7145 * with this map space as extract argument.
7147 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7148 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7149 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7150 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7152 int match;
7153 isl_space *space1, *space2;
7154 isl_map *res;
7156 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7157 isl_multi_pw_aff_get_space(mpa2));
7158 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7159 isl_multi_pw_aff_get_space(mpa1));
7160 if (!mpa1 || !mpa2)
7161 goto error;
7162 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7163 mpa2->space, isl_dim_out);
7164 if (match < 0)
7165 goto error;
7166 if (!match)
7167 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7168 "range spaces don't match", goto error);
7169 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7170 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7171 space1 = isl_space_map_from_domain_and_range(space1, space2);
7173 res = order(mpa1, mpa2, space1);
7174 isl_multi_pw_aff_free(mpa1);
7175 isl_multi_pw_aff_free(mpa2);
7176 return res;
7177 error:
7178 isl_multi_pw_aff_free(mpa1);
7179 isl_multi_pw_aff_free(mpa2);
7180 return NULL;
7183 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7184 * where the function values are equal. "space" is the space of the result.
7185 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7187 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7188 * in the sequences are equal.
7190 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7191 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7192 __isl_take isl_space *space)
7194 int i, n;
7195 isl_map *res;
7197 res = isl_map_universe(space);
7199 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7200 for (i = 0; i < n; ++i) {
7201 isl_pw_aff *pa1, *pa2;
7202 isl_map *map;
7204 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7205 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7206 map = isl_pw_aff_eq_map(pa1, pa2);
7207 res = isl_map_intersect(res, map);
7210 return res;
7213 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7214 * where the function values are equal.
7216 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7217 __isl_take isl_multi_pw_aff *mpa2)
7219 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7220 &isl_multi_pw_aff_eq_map_on_space);
7223 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7224 * where the function values of "mpa1" is lexicographically satisfies "base"
7225 * compared to that of "mpa2". "space" is the space of the result.
7226 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7228 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7229 * if its i-th element satisfies "base" when compared to
7230 * the i-th element of "mpa2" while all previous elements are
7231 * pairwise equal.
7233 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7234 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7235 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7236 __isl_take isl_pw_aff *pa2),
7237 __isl_take isl_space *space)
7239 int i, n;
7240 isl_map *res, *rest;
7242 res = isl_map_empty(isl_space_copy(space));
7243 rest = isl_map_universe(space);
7245 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7246 for (i = 0; i < n; ++i) {
7247 isl_pw_aff *pa1, *pa2;
7248 isl_map *map;
7250 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7251 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7252 map = base(pa1, pa2);
7253 map = isl_map_intersect(map, isl_map_copy(rest));
7254 res = isl_map_union(res, map);
7256 if (i == n - 1)
7257 continue;
7259 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7260 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7261 map = isl_pw_aff_eq_map(pa1, pa2);
7262 rest = isl_map_intersect(rest, map);
7265 isl_map_free(rest);
7266 return res;
7269 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7270 * where the function value of "mpa1" is lexicographically less than that
7271 * of "mpa2". "space" is the space of the result.
7272 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7274 * "mpa1" is less than "mpa2" if its i-th element is smaller
7275 * than the i-th element of "mpa2" while all previous elements are
7276 * pairwise equal.
7278 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7279 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7280 __isl_take isl_space *space)
7282 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7283 &isl_pw_aff_lt_map, space);
7286 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7287 * where the function value of "mpa1" is lexicographically less than that
7288 * of "mpa2".
7290 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7291 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7293 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7294 &isl_multi_pw_aff_lex_lt_map_on_space);
7297 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7298 * where the function value of "mpa1" is lexicographically greater than that
7299 * of "mpa2". "space" is the space of the result.
7300 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7302 * "mpa1" is greater than "mpa2" if its i-th element is greater
7303 * than the i-th element of "mpa2" while all previous elements are
7304 * pairwise equal.
7306 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7307 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7308 __isl_take isl_space *space)
7310 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7311 &isl_pw_aff_gt_map, space);
7314 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7315 * where the function value of "mpa1" is lexicographically greater than that
7316 * of "mpa2".
7318 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7319 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7321 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7322 &isl_multi_pw_aff_lex_gt_map_on_space);
7325 /* Compare two isl_affs.
7327 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7328 * than "aff2" and 0 if they are equal.
7330 * The order is fairly arbitrary. We do consider expressions that only involve
7331 * earlier dimensions as "smaller".
7333 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7335 int cmp;
7336 int last1, last2;
7338 if (aff1 == aff2)
7339 return 0;
7341 if (!aff1)
7342 return -1;
7343 if (!aff2)
7344 return 1;
7346 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7347 if (cmp != 0)
7348 return cmp;
7350 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7351 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7352 if (last1 != last2)
7353 return last1 - last2;
7355 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7358 /* Compare two isl_pw_affs.
7360 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7361 * than "pa2" and 0 if they are equal.
7363 * The order is fairly arbitrary. We do consider expressions that only involve
7364 * earlier dimensions as "smaller".
7366 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7367 __isl_keep isl_pw_aff *pa2)
7369 int i;
7370 int cmp;
7372 if (pa1 == pa2)
7373 return 0;
7375 if (!pa1)
7376 return -1;
7377 if (!pa2)
7378 return 1;
7380 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7381 if (cmp != 0)
7382 return cmp;
7384 if (pa1->n != pa2->n)
7385 return pa1->n - pa2->n;
7387 for (i = 0; i < pa1->n; ++i) {
7388 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7389 if (cmp != 0)
7390 return cmp;
7391 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7392 if (cmp != 0)
7393 return cmp;
7396 return 0;
7399 /* Return a piecewise affine expression that is equal to "v" on "domain".
7401 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7402 __isl_take isl_val *v)
7404 isl_space *space;
7405 isl_local_space *ls;
7406 isl_aff *aff;
7408 space = isl_set_get_space(domain);
7409 ls = isl_local_space_from_space(space);
7410 aff = isl_aff_val_on_domain(ls, v);
7412 return isl_pw_aff_alloc(domain, aff);
7415 /* Return a multi affine expression that is equal to "mv" on domain
7416 * space "space".
7418 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7419 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7421 int i, n;
7422 isl_space *space2;
7423 isl_local_space *ls;
7424 isl_multi_aff *ma;
7426 if (!space || !mv)
7427 goto error;
7429 n = isl_multi_val_dim(mv, isl_dim_set);
7430 space2 = isl_multi_val_get_space(mv);
7431 space2 = isl_space_align_params(space2, isl_space_copy(space));
7432 space = isl_space_align_params(space, isl_space_copy(space2));
7433 space = isl_space_map_from_domain_and_range(space, space2);
7434 ma = isl_multi_aff_alloc(isl_space_copy(space));
7435 ls = isl_local_space_from_space(isl_space_domain(space));
7436 for (i = 0; i < n; ++i) {
7437 isl_val *v;
7438 isl_aff *aff;
7440 v = isl_multi_val_get_val(mv, i);
7441 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7442 ma = isl_multi_aff_set_aff(ma, i, aff);
7444 isl_local_space_free(ls);
7446 isl_multi_val_free(mv);
7447 return ma;
7448 error:
7449 isl_space_free(space);
7450 isl_multi_val_free(mv);
7451 return NULL;
7454 /* Return a piecewise multi-affine expression
7455 * that is equal to "mv" on "domain".
7457 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7458 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7460 isl_space *space;
7461 isl_multi_aff *ma;
7463 space = isl_set_get_space(domain);
7464 ma = isl_multi_aff_multi_val_on_space(space, mv);
7466 return isl_pw_multi_aff_alloc(domain, ma);
7469 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7470 * mv is the value that should be attained on each domain set
7471 * res collects the results
7473 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7474 isl_multi_val *mv;
7475 isl_union_pw_multi_aff *res;
7478 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7479 * and add it to data->res.
7481 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7482 void *user)
7484 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7485 isl_pw_multi_aff *pma;
7486 isl_multi_val *mv;
7488 mv = isl_multi_val_copy(data->mv);
7489 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7490 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7492 return data->res ? isl_stat_ok : isl_stat_error;
7495 /* Return a union piecewise multi-affine expression
7496 * that is equal to "mv" on "domain".
7498 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7499 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7501 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7502 isl_space *space;
7504 space = isl_union_set_get_space(domain);
7505 data.res = isl_union_pw_multi_aff_empty(space);
7506 data.mv = mv;
7507 if (isl_union_set_foreach_set(domain,
7508 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7509 data.res = isl_union_pw_multi_aff_free(data.res);
7510 isl_union_set_free(domain);
7511 isl_multi_val_free(mv);
7512 return data.res;
7515 /* Compute the pullback of data->pma by the function represented by "pma2",
7516 * provided the spaces match, and add the results to data->res.
7518 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7520 struct isl_union_pw_multi_aff_bin_data *data = user;
7522 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7523 pma2->dim, isl_dim_out)) {
7524 isl_pw_multi_aff_free(pma2);
7525 return isl_stat_ok;
7528 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7529 isl_pw_multi_aff_copy(data->pma), pma2);
7531 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7532 if (!data->res)
7533 return isl_stat_error;
7535 return isl_stat_ok;
7538 /* Compute the pullback of "upma1" by the function represented by "upma2".
7540 __isl_give isl_union_pw_multi_aff *
7541 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7542 __isl_take isl_union_pw_multi_aff *upma1,
7543 __isl_take isl_union_pw_multi_aff *upma2)
7545 return bin_op(upma1, upma2, &pullback_entry);
7548 /* Check that the domain space of "upa" matches "space".
7550 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7551 * can in principle never fail since the space "space" is that
7552 * of the isl_multi_union_pw_aff and is a set space such that
7553 * there is no domain space to match.
7555 * We check the parameters and double-check that "space" is
7556 * indeed that of a set.
7558 static isl_stat isl_union_pw_aff_check_match_domain_space(
7559 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7561 isl_space *upa_space;
7562 isl_bool match;
7564 if (!upa || !space)
7565 return isl_stat_error;
7567 match = isl_space_is_set(space);
7568 if (match < 0)
7569 return isl_stat_error;
7570 if (!match)
7571 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7572 "expecting set space", return -1);
7574 upa_space = isl_union_pw_aff_get_space(upa);
7575 match = isl_space_has_equal_params(space, upa_space);
7576 if (match < 0)
7577 goto error;
7578 if (!match)
7579 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7580 "parameters don't match", goto error);
7582 isl_space_free(upa_space);
7583 return isl_stat_ok;
7584 error:
7585 isl_space_free(upa_space);
7586 return isl_stat_error;
7589 /* Do the parameters of "upa" match those of "space"?
7591 static isl_bool isl_union_pw_aff_matching_params(
7592 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7594 isl_space *upa_space;
7595 isl_bool match;
7597 if (!upa || !space)
7598 return isl_bool_error;
7600 upa_space = isl_union_pw_aff_get_space(upa);
7602 match = isl_space_has_equal_params(space, upa_space);
7604 isl_space_free(upa_space);
7605 return match;
7608 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7609 * space represents the new parameters.
7610 * res collects the results.
7612 struct isl_union_pw_aff_reset_params_data {
7613 isl_space *space;
7614 isl_union_pw_aff *res;
7617 /* Replace the parameters of "pa" by data->space and
7618 * add the result to data->res.
7620 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7622 struct isl_union_pw_aff_reset_params_data *data = user;
7623 isl_space *space;
7625 space = isl_pw_aff_get_space(pa);
7626 space = isl_space_replace_params(space, data->space);
7627 pa = isl_pw_aff_reset_space(pa, space);
7628 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7630 return data->res ? isl_stat_ok : isl_stat_error;
7633 /* Replace the domain space of "upa" by "space".
7634 * Since a union expression does not have a (single) domain space,
7635 * "space" is necessarily a parameter space.
7637 * Since the order and the names of the parameters determine
7638 * the hash value, we need to create a new hash table.
7640 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7641 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7643 struct isl_union_pw_aff_reset_params_data data = { space };
7644 isl_bool match;
7646 match = isl_union_pw_aff_matching_params(upa, space);
7647 if (match < 0)
7648 upa = isl_union_pw_aff_free(upa);
7649 else if (match) {
7650 isl_space_free(space);
7651 return upa;
7654 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7655 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7656 data.res = isl_union_pw_aff_free(data.res);
7658 isl_union_pw_aff_free(upa);
7659 isl_space_free(space);
7660 return data.res;
7663 /* Return the floor of "pa".
7665 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7667 return isl_pw_aff_floor(pa);
7670 /* Given f, return floor(f).
7672 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7673 __isl_take isl_union_pw_aff *upa)
7675 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7678 /* Compute
7680 * upa mod m = upa - m * floor(upa/m)
7682 * with m an integer value.
7684 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7685 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7687 isl_union_pw_aff *res;
7689 if (!upa || !m)
7690 goto error;
7692 if (!isl_val_is_int(m))
7693 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7694 "expecting integer modulo", goto error);
7695 if (!isl_val_is_pos(m))
7696 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7697 "expecting positive modulo", goto error);
7699 res = isl_union_pw_aff_copy(upa);
7700 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7701 upa = isl_union_pw_aff_floor(upa);
7702 upa = isl_union_pw_aff_scale_val(upa, m);
7703 res = isl_union_pw_aff_sub(res, upa);
7705 return res;
7706 error:
7707 isl_val_free(m);
7708 isl_union_pw_aff_free(upa);
7709 return NULL;
7712 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7713 * pos is the output position that needs to be extracted.
7714 * res collects the results.
7716 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7717 int pos;
7718 isl_union_pw_aff *res;
7721 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7722 * (assuming it has such a dimension) and add it to data->res.
7724 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7726 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7727 int n_out;
7728 isl_pw_aff *pa;
7730 if (!pma)
7731 return isl_stat_error;
7733 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7734 if (data->pos >= n_out) {
7735 isl_pw_multi_aff_free(pma);
7736 return isl_stat_ok;
7739 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7740 isl_pw_multi_aff_free(pma);
7742 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7744 return data->res ? isl_stat_ok : isl_stat_error;
7747 /* Extract an isl_union_pw_aff corresponding to
7748 * output dimension "pos" of "upma".
7750 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7751 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7753 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7754 isl_space *space;
7756 if (!upma)
7757 return NULL;
7759 if (pos < 0)
7760 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7761 "cannot extract at negative position", return NULL);
7763 space = isl_union_pw_multi_aff_get_space(upma);
7764 data.res = isl_union_pw_aff_empty(space);
7765 data.pos = pos;
7766 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7767 &get_union_pw_aff, &data) < 0)
7768 data.res = isl_union_pw_aff_free(data.res);
7770 return data.res;
7773 /* Return a union piecewise affine expression
7774 * that is equal to "aff" on "domain".
7776 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7777 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7779 isl_pw_aff *pa;
7781 pa = isl_pw_aff_from_aff(aff);
7782 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7785 /* Return a union piecewise affine expression
7786 * that is equal to the parameter identified by "id" on "domain".
7788 * Make sure the parameter appears in the space passed to
7789 * isl_aff_param_on_domain_space_id.
7791 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7792 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7794 isl_space *space;
7795 isl_aff *aff;
7797 space = isl_union_set_get_space(domain);
7798 space = isl_space_add_param_id(space, isl_id_copy(id));
7799 aff = isl_aff_param_on_domain_space_id(space, id);
7800 return isl_union_pw_aff_aff_on_domain(domain, aff);
7803 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7804 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7805 * needs to attain.
7806 * "res" collects the results.
7808 struct isl_union_pw_aff_pw_aff_on_domain_data {
7809 isl_pw_aff *pa;
7810 isl_union_pw_aff *res;
7813 /* Construct a piecewise affine expression that is equal to data->pa
7814 * on "domain" and add the result to data->res.
7816 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7818 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7819 isl_pw_aff *pa;
7820 int dim;
7822 pa = isl_pw_aff_copy(data->pa);
7823 dim = isl_set_dim(domain, isl_dim_set);
7824 pa = isl_pw_aff_from_range(pa);
7825 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7826 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7827 pa = isl_pw_aff_intersect_domain(pa, domain);
7828 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7830 return data->res ? isl_stat_ok : isl_stat_error;
7833 /* Return a union piecewise affine expression
7834 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7835 * have been aligned.
7837 * Construct an isl_pw_aff on each of the sets in "domain" and
7838 * collect the results.
7840 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7841 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7843 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7844 isl_space *space;
7846 space = isl_union_set_get_space(domain);
7847 data.res = isl_union_pw_aff_empty(space);
7848 data.pa = pa;
7849 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7850 data.res = isl_union_pw_aff_free(data.res);
7851 isl_union_set_free(domain);
7852 isl_pw_aff_free(pa);
7853 return data.res;
7856 /* Return a union piecewise affine expression
7857 * that is equal to "pa" on "domain".
7859 * Check that "pa" is a parametric expression,
7860 * align the parameters if needed and call
7861 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7863 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7864 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7866 isl_bool is_set;
7867 isl_bool equal_params;
7868 isl_space *domain_space, *pa_space;
7870 pa_space = isl_pw_aff_peek_space(pa);
7871 is_set = isl_space_is_set(pa_space);
7872 if (is_set < 0)
7873 goto error;
7874 if (!is_set)
7875 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7876 "expecting parametric expression", goto error);
7878 domain_space = isl_union_set_get_space(domain);
7879 pa_space = isl_pw_aff_get_space(pa);
7880 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7881 if (equal_params >= 0 && !equal_params) {
7882 isl_space *space;
7884 space = isl_space_align_params(domain_space, pa_space);
7885 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7886 domain = isl_union_set_align_params(domain, space);
7887 } else {
7888 isl_space_free(domain_space);
7889 isl_space_free(pa_space);
7892 if (equal_params < 0)
7893 goto error;
7894 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7895 error:
7896 isl_union_set_free(domain);
7897 isl_pw_aff_free(pa);
7898 return NULL;
7901 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7902 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7903 * "res" collects the results.
7905 struct isl_union_pw_aff_val_on_domain_data {
7906 isl_val *v;
7907 isl_union_pw_aff *res;
7910 /* Construct a piecewise affine expression that is equal to data->v
7911 * on "domain" and add the result to data->res.
7913 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7915 struct isl_union_pw_aff_val_on_domain_data *data = user;
7916 isl_pw_aff *pa;
7917 isl_val *v;
7919 v = isl_val_copy(data->v);
7920 pa = isl_pw_aff_val_on_domain(domain, v);
7921 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7923 return data->res ? isl_stat_ok : isl_stat_error;
7926 /* Return a union piecewise affine expression
7927 * that is equal to "v" on "domain".
7929 * Construct an isl_pw_aff on each of the sets in "domain" and
7930 * collect the results.
7932 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7933 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7935 struct isl_union_pw_aff_val_on_domain_data data;
7936 isl_space *space;
7938 space = isl_union_set_get_space(domain);
7939 data.res = isl_union_pw_aff_empty(space);
7940 data.v = v;
7941 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7942 data.res = isl_union_pw_aff_free(data.res);
7943 isl_union_set_free(domain);
7944 isl_val_free(v);
7945 return data.res;
7948 /* Construct a piecewise multi affine expression
7949 * that is equal to "pa" and add it to upma.
7951 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7952 void *user)
7954 isl_union_pw_multi_aff **upma = user;
7955 isl_pw_multi_aff *pma;
7957 pma = isl_pw_multi_aff_from_pw_aff(pa);
7958 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7960 return *upma ? isl_stat_ok : isl_stat_error;
7963 /* Construct and return a union piecewise multi affine expression
7964 * that is equal to the given union piecewise affine expression.
7966 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7967 __isl_take isl_union_pw_aff *upa)
7969 isl_space *space;
7970 isl_union_pw_multi_aff *upma;
7972 if (!upa)
7973 return NULL;
7975 space = isl_union_pw_aff_get_space(upa);
7976 upma = isl_union_pw_multi_aff_empty(space);
7978 if (isl_union_pw_aff_foreach_pw_aff(upa,
7979 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7980 upma = isl_union_pw_multi_aff_free(upma);
7982 isl_union_pw_aff_free(upa);
7983 return upma;
7986 /* Compute the set of elements in the domain of "pa" where it is zero and
7987 * add this set to "uset".
7989 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7991 isl_union_set **uset = (isl_union_set **)user;
7993 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7995 return *uset ? isl_stat_ok : isl_stat_error;
7998 /* Return a union set containing those elements in the domain
7999 * of "upa" where it is zero.
8001 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8002 __isl_take isl_union_pw_aff *upa)
8004 isl_union_set *zero;
8006 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8007 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8008 zero = isl_union_set_free(zero);
8010 isl_union_pw_aff_free(upa);
8011 return zero;
8014 /* Convert "pa" to an isl_map and add it to *umap.
8016 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8018 isl_union_map **umap = user;
8019 isl_map *map;
8021 map = isl_map_from_pw_aff(pa);
8022 *umap = isl_union_map_add_map(*umap, map);
8024 return *umap ? isl_stat_ok : isl_stat_error;
8027 /* Construct a union map mapping the domain of the union
8028 * piecewise affine expression to its range, with the single output dimension
8029 * equated to the corresponding affine expressions on their cells.
8031 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8032 __isl_take isl_union_pw_aff *upa)
8034 isl_space *space;
8035 isl_union_map *umap;
8037 if (!upa)
8038 return NULL;
8040 space = isl_union_pw_aff_get_space(upa);
8041 umap = isl_union_map_empty(space);
8043 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8044 &umap) < 0)
8045 umap = isl_union_map_free(umap);
8047 isl_union_pw_aff_free(upa);
8048 return umap;
8051 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8052 * upma is the function that is plugged in.
8053 * pa is the current part of the function in which upma is plugged in.
8054 * res collects the results.
8056 struct isl_union_pw_aff_pullback_upma_data {
8057 isl_union_pw_multi_aff *upma;
8058 isl_pw_aff *pa;
8059 isl_union_pw_aff *res;
8062 /* Check if "pma" can be plugged into data->pa.
8063 * If so, perform the pullback and add the result to data->res.
8065 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8067 struct isl_union_pw_aff_pullback_upma_data *data = user;
8068 isl_pw_aff *pa;
8070 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8071 pma->dim, isl_dim_out)) {
8072 isl_pw_multi_aff_free(pma);
8073 return isl_stat_ok;
8076 pa = isl_pw_aff_copy(data->pa);
8077 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8079 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8081 return data->res ? isl_stat_ok : isl_stat_error;
8084 /* Check if any of the elements of data->upma can be plugged into pa,
8085 * add if so add the result to data->res.
8087 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8089 struct isl_union_pw_aff_pullback_upma_data *data = user;
8090 isl_stat r;
8092 data->pa = pa;
8093 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8094 &pa_pb_pma, data);
8095 isl_pw_aff_free(pa);
8097 return r;
8100 /* Compute the pullback of "upa" by the function represented by "upma".
8101 * In other words, plug in "upma" in "upa". The result contains
8102 * expressions defined over the domain space of "upma".
8104 * Run over all pairs of elements in "upa" and "upma", perform
8105 * the pullback when appropriate and collect the results.
8106 * If the hash value were based on the domain space rather than
8107 * the function space, then we could run through all elements
8108 * of "upma" and directly pick out the corresponding element of "upa".
8110 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8111 __isl_take isl_union_pw_aff *upa,
8112 __isl_take isl_union_pw_multi_aff *upma)
8114 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8115 isl_space *space;
8117 space = isl_union_pw_multi_aff_get_space(upma);
8118 upa = isl_union_pw_aff_align_params(upa, space);
8119 space = isl_union_pw_aff_get_space(upa);
8120 upma = isl_union_pw_multi_aff_align_params(upma, space);
8122 if (!upa || !upma)
8123 goto error;
8125 data.upma = upma;
8126 data.res = isl_union_pw_aff_alloc_same_size(upa);
8127 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8128 data.res = isl_union_pw_aff_free(data.res);
8130 isl_union_pw_aff_free(upa);
8131 isl_union_pw_multi_aff_free(upma);
8132 return data.res;
8133 error:
8134 isl_union_pw_aff_free(upa);
8135 isl_union_pw_multi_aff_free(upma);
8136 return NULL;
8139 #undef BASE
8140 #define BASE union_pw_aff
8141 #undef DOMBASE
8142 #define DOMBASE union_set
8144 #define NO_MOVE_DIMS
8145 #define NO_DOMAIN
8146 #define NO_PRODUCT
8147 #define NO_SPLICE
8148 #define NO_ZERO
8149 #define NO_IDENTITY
8150 #define NO_GIST
8152 #include <isl_multi_explicit_domain.c>
8153 #include <isl_multi_union_pw_aff_explicit_domain.c>
8154 #include <isl_multi_templ.c>
8155 #include <isl_multi_apply_set.c>
8156 #include <isl_multi_apply_union_set.c>
8157 #include <isl_multi_coalesce.c>
8158 #include <isl_multi_floor.c>
8159 #include <isl_multi_gist.c>
8160 #include <isl_multi_align_set.c>
8161 #include <isl_multi_align_union_set.c>
8162 #include <isl_multi_intersect.c>
8164 /* Does "mupa" have a non-trivial explicit domain?
8166 * The explicit domain, if present, is trivial if it represents
8167 * an (obviously) universe parameter set.
8169 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8170 __isl_keep isl_multi_union_pw_aff *mupa)
8172 isl_bool is_params, trivial;
8173 isl_set *set;
8175 if (!mupa)
8176 return isl_bool_error;
8177 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8178 return isl_bool_false;
8179 is_params = isl_union_set_is_params(mupa->u.dom);
8180 if (is_params < 0 || !is_params)
8181 return isl_bool_not(is_params);
8182 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8183 trivial = isl_set_plain_is_universe(set);
8184 isl_set_free(set);
8185 return isl_bool_not(trivial);
8188 /* Construct a multiple union piecewise affine expression
8189 * in the given space with value zero in each of the output dimensions.
8191 * Since there is no canonical zero value for
8192 * a union piecewise affine expression, we can only construct
8193 * a zero-dimensional "zero" value.
8195 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8196 __isl_take isl_space *space)
8198 isl_bool params;
8200 if (!space)
8201 return NULL;
8203 params = isl_space_is_params(space);
8204 if (params < 0)
8205 goto error;
8206 if (params)
8207 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8208 "expecting proper set space", goto error);
8209 if (!isl_space_is_set(space))
8210 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8211 "expecting set space", goto error);
8212 if (isl_space_dim(space , isl_dim_out) != 0)
8213 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8214 "expecting 0D space", goto error);
8216 return isl_multi_union_pw_aff_alloc(space);
8217 error:
8218 isl_space_free(space);
8219 return NULL;
8222 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8223 * with the actual sum on the shared domain and
8224 * the defined expression on the symmetric difference of the domains.
8226 * We simply iterate over the elements in both arguments and
8227 * call isl_union_pw_aff_union_add on each of them, if there is
8228 * at least one element.
8230 * Otherwise, the two expressions have an explicit domain and
8231 * the union of these explicit domains is computed.
8232 * This assumes that the explicit domains are either both in terms
8233 * of specific domains elements or both in terms of parameters.
8234 * However, if one of the expressions does not have any constraints
8235 * on its explicit domain, then this is allowed as well and the result
8236 * is the expression with no constraints on its explicit domain.
8238 static __isl_give isl_multi_union_pw_aff *
8239 isl_multi_union_pw_aff_union_add_aligned(
8240 __isl_take isl_multi_union_pw_aff *mupa1,
8241 __isl_take isl_multi_union_pw_aff *mupa2)
8243 isl_bool has_domain, is_params1, is_params2;
8245 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8246 goto error;
8247 if (mupa1->n > 0)
8248 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8249 &isl_union_pw_aff_union_add);
8250 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8251 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8252 goto error;
8254 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8255 if (has_domain < 0)
8256 goto error;
8257 if (!has_domain) {
8258 isl_multi_union_pw_aff_free(mupa2);
8259 return mupa1;
8261 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8262 if (has_domain < 0)
8263 goto error;
8264 if (!has_domain) {
8265 isl_multi_union_pw_aff_free(mupa1);
8266 return mupa2;
8269 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8270 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8271 if (is_params1 < 0 || is_params2 < 0)
8272 goto error;
8273 if (is_params1 != is_params2)
8274 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8275 isl_error_invalid,
8276 "cannot compute union of concrete domain and "
8277 "parameter constraints", goto error);
8278 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8279 if (!mupa1)
8280 goto error;
8281 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8282 isl_union_set_copy(mupa2->u.dom));
8283 if (!mupa1->u.dom)
8284 goto error;
8285 isl_multi_union_pw_aff_free(mupa2);
8286 return mupa1;
8287 error:
8288 isl_multi_union_pw_aff_free(mupa1);
8289 isl_multi_union_pw_aff_free(mupa2);
8290 return NULL;
8293 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8294 * with the actual sum on the shared domain and
8295 * the defined expression on the symmetric difference of the domains.
8297 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8298 __isl_take isl_multi_union_pw_aff *mupa1,
8299 __isl_take isl_multi_union_pw_aff *mupa2)
8301 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8302 &isl_multi_union_pw_aff_union_add_aligned);
8305 /* Construct and return a multi union piecewise affine expression
8306 * that is equal to the given multi affine expression.
8308 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8309 __isl_take isl_multi_aff *ma)
8311 isl_multi_pw_aff *mpa;
8313 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8314 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8317 /* Construct and return a multi union piecewise affine expression
8318 * that is equal to the given multi piecewise affine expression.
8320 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8321 __isl_take isl_multi_pw_aff *mpa)
8323 int i, n;
8324 isl_space *space;
8325 isl_multi_union_pw_aff *mupa;
8327 if (!mpa)
8328 return NULL;
8330 space = isl_multi_pw_aff_get_space(mpa);
8331 space = isl_space_range(space);
8332 mupa = isl_multi_union_pw_aff_alloc(space);
8334 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8335 for (i = 0; i < n; ++i) {
8336 isl_pw_aff *pa;
8337 isl_union_pw_aff *upa;
8339 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8340 upa = isl_union_pw_aff_from_pw_aff(pa);
8341 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8344 isl_multi_pw_aff_free(mpa);
8346 return mupa;
8349 /* Extract the range space of "pma" and assign it to *space.
8350 * If *space has already been set (through a previous call to this function),
8351 * then check that the range space is the same.
8353 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8355 isl_space **space = user;
8356 isl_space *pma_space;
8357 isl_bool equal;
8359 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8360 isl_pw_multi_aff_free(pma);
8362 if (!pma_space)
8363 return isl_stat_error;
8364 if (!*space) {
8365 *space = pma_space;
8366 return isl_stat_ok;
8369 equal = isl_space_is_equal(pma_space, *space);
8370 isl_space_free(pma_space);
8372 if (equal < 0)
8373 return isl_stat_error;
8374 if (!equal)
8375 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8376 "range spaces not the same", return isl_stat_error);
8377 return isl_stat_ok;
8380 /* Construct and return a multi union piecewise affine expression
8381 * that is equal to the given union piecewise multi affine expression.
8383 * In order to be able to perform the conversion, the input
8384 * needs to be non-empty and may only involve a single range space.
8386 * If the resulting multi union piecewise affine expression has
8387 * an explicit domain, then assign it the domain of the input.
8388 * In other cases, the domain is stored in the individual elements.
8390 __isl_give isl_multi_union_pw_aff *
8391 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8392 __isl_take isl_union_pw_multi_aff *upma)
8394 isl_space *space = NULL;
8395 isl_multi_union_pw_aff *mupa;
8396 int i, n;
8398 if (!upma)
8399 return NULL;
8400 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8401 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8402 "cannot extract range space from empty input",
8403 goto error);
8404 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8405 &space) < 0)
8406 goto error;
8408 if (!space)
8409 goto error;
8411 n = isl_space_dim(space, isl_dim_set);
8412 mupa = isl_multi_union_pw_aff_alloc(space);
8414 for (i = 0; i < n; ++i) {
8415 isl_union_pw_aff *upa;
8417 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8418 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8420 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8421 isl_union_set *dom;
8422 isl_union_pw_multi_aff *copy;
8424 copy = isl_union_pw_multi_aff_copy(upma);
8425 dom = isl_union_pw_multi_aff_domain(copy);
8426 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8429 isl_union_pw_multi_aff_free(upma);
8430 return mupa;
8431 error:
8432 isl_space_free(space);
8433 isl_union_pw_multi_aff_free(upma);
8434 return NULL;
8437 /* Try and create an isl_multi_union_pw_aff that is equivalent
8438 * to the given isl_union_map.
8439 * The isl_union_map is required to be single-valued in each space.
8440 * Moreover, it cannot be empty and all range spaces need to be the same.
8441 * Otherwise, an error is produced.
8443 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8444 __isl_take isl_union_map *umap)
8446 isl_union_pw_multi_aff *upma;
8448 upma = isl_union_pw_multi_aff_from_union_map(umap);
8449 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8452 /* Return a multiple union piecewise affine expression
8453 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8454 * have been aligned.
8456 static __isl_give isl_multi_union_pw_aff *
8457 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8458 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8460 int i, n;
8461 isl_space *space;
8462 isl_multi_union_pw_aff *mupa;
8464 if (!domain || !mv)
8465 goto error;
8467 n = isl_multi_val_dim(mv, isl_dim_set);
8468 space = isl_multi_val_get_space(mv);
8469 mupa = isl_multi_union_pw_aff_alloc(space);
8470 for (i = 0; i < n; ++i) {
8471 isl_val *v;
8472 isl_union_pw_aff *upa;
8474 v = isl_multi_val_get_val(mv, i);
8475 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8477 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8480 isl_union_set_free(domain);
8481 isl_multi_val_free(mv);
8482 return mupa;
8483 error:
8484 isl_union_set_free(domain);
8485 isl_multi_val_free(mv);
8486 return NULL;
8489 /* Return a multiple union piecewise affine expression
8490 * that is equal to "mv" on "domain".
8492 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8493 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8495 isl_bool equal_params;
8497 if (!domain || !mv)
8498 goto error;
8499 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8500 if (equal_params < 0)
8501 goto error;
8502 if (equal_params)
8503 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8504 domain, mv);
8505 domain = isl_union_set_align_params(domain,
8506 isl_multi_val_get_space(mv));
8507 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8508 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8509 error:
8510 isl_union_set_free(domain);
8511 isl_multi_val_free(mv);
8512 return NULL;
8515 /* Return a multiple union piecewise affine expression
8516 * that is equal to "ma" on "domain".
8518 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8519 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8521 isl_pw_multi_aff *pma;
8523 pma = isl_pw_multi_aff_from_multi_aff(ma);
8524 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8527 /* Return a multiple union piecewise affine expression
8528 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8529 * have been aligned.
8531 * If the resulting multi union piecewise affine expression has
8532 * an explicit domain, then assign it the input domain.
8533 * In other cases, the domain is stored in the individual elements.
8535 static __isl_give isl_multi_union_pw_aff *
8536 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8537 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8539 int i, n;
8540 isl_space *space;
8541 isl_multi_union_pw_aff *mupa;
8543 if (!domain || !pma)
8544 goto error;
8546 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8547 space = isl_pw_multi_aff_get_space(pma);
8548 mupa = isl_multi_union_pw_aff_alloc(space);
8549 for (i = 0; i < n; ++i) {
8550 isl_pw_aff *pa;
8551 isl_union_pw_aff *upa;
8553 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8554 upa = isl_union_pw_aff_pw_aff_on_domain(
8555 isl_union_set_copy(domain), pa);
8556 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8558 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8559 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8560 isl_union_set_copy(domain));
8562 isl_union_set_free(domain);
8563 isl_pw_multi_aff_free(pma);
8564 return mupa;
8565 error:
8566 isl_union_set_free(domain);
8567 isl_pw_multi_aff_free(pma);
8568 return NULL;
8571 /* Return a multiple union piecewise affine expression
8572 * that is equal to "pma" on "domain".
8574 __isl_give isl_multi_union_pw_aff *
8575 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8576 __isl_take isl_pw_multi_aff *pma)
8578 isl_bool equal_params;
8579 isl_space *space;
8581 space = isl_pw_multi_aff_peek_space(pma);
8582 equal_params = isl_union_set_space_has_equal_params(domain, space);
8583 if (equal_params < 0)
8584 goto error;
8585 if (equal_params)
8586 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8587 domain, pma);
8588 domain = isl_union_set_align_params(domain,
8589 isl_pw_multi_aff_get_space(pma));
8590 pma = isl_pw_multi_aff_align_params(pma,
8591 isl_union_set_get_space(domain));
8592 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8593 pma);
8594 error:
8595 isl_union_set_free(domain);
8596 isl_pw_multi_aff_free(pma);
8597 return NULL;
8600 /* Return a union set containing those elements in the domains
8601 * of the elements of "mupa" where they are all zero.
8603 * If there are no elements, then simply return the entire domain.
8605 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8606 __isl_take isl_multi_union_pw_aff *mupa)
8608 int i, n;
8609 isl_union_pw_aff *upa;
8610 isl_union_set *zero;
8612 if (!mupa)
8613 return NULL;
8615 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8616 if (n == 0)
8617 return isl_multi_union_pw_aff_domain(mupa);
8619 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8620 zero = isl_union_pw_aff_zero_union_set(upa);
8622 for (i = 1; i < n; ++i) {
8623 isl_union_set *zero_i;
8625 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8626 zero_i = isl_union_pw_aff_zero_union_set(upa);
8628 zero = isl_union_set_intersect(zero, zero_i);
8631 isl_multi_union_pw_aff_free(mupa);
8632 return zero;
8635 /* Construct a union map mapping the shared domain
8636 * of the union piecewise affine expressions to the range of "mupa"
8637 * in the special case of a 0D multi union piecewise affine expression.
8639 * Construct a map between the explicit domain of "mupa" and
8640 * the range space.
8641 * Note that this assumes that the domain consists of explicit elements.
8643 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8644 __isl_take isl_multi_union_pw_aff *mupa)
8646 isl_bool is_params;
8647 isl_space *space;
8648 isl_union_set *dom, *ran;
8650 space = isl_multi_union_pw_aff_get_space(mupa);
8651 dom = isl_multi_union_pw_aff_domain(mupa);
8652 ran = isl_union_set_from_set(isl_set_universe(space));
8654 is_params = isl_union_set_is_params(dom);
8655 if (is_params < 0)
8656 dom = isl_union_set_free(dom);
8657 else if (is_params)
8658 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8659 "cannot create union map from expression without "
8660 "explicit domain elements",
8661 dom = isl_union_set_free(dom));
8663 return isl_union_map_from_domain_and_range(dom, ran);
8666 /* Construct a union map mapping the shared domain
8667 * of the union piecewise affine expressions to the range of "mupa"
8668 * with each dimension in the range equated to the
8669 * corresponding union piecewise affine expression.
8671 * If the input is zero-dimensional, then construct a mapping
8672 * from its explicit domain.
8674 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8675 __isl_take isl_multi_union_pw_aff *mupa)
8677 int i, n;
8678 isl_space *space;
8679 isl_union_map *umap;
8680 isl_union_pw_aff *upa;
8682 if (!mupa)
8683 return NULL;
8685 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8686 if (n == 0)
8687 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8689 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8690 umap = isl_union_map_from_union_pw_aff(upa);
8692 for (i = 1; i < n; ++i) {
8693 isl_union_map *umap_i;
8695 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8696 umap_i = isl_union_map_from_union_pw_aff(upa);
8697 umap = isl_union_map_flat_range_product(umap, umap_i);
8700 space = isl_multi_union_pw_aff_get_space(mupa);
8701 umap = isl_union_map_reset_range_space(umap, space);
8703 isl_multi_union_pw_aff_free(mupa);
8704 return umap;
8707 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8708 * "range" is the space from which to set the range space.
8709 * "res" collects the results.
8711 struct isl_union_pw_multi_aff_reset_range_space_data {
8712 isl_space *range;
8713 isl_union_pw_multi_aff *res;
8716 /* Replace the range space of "pma" by the range space of data->range and
8717 * add the result to data->res.
8719 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8721 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8722 isl_space *space;
8724 space = isl_pw_multi_aff_get_space(pma);
8725 space = isl_space_domain(space);
8726 space = isl_space_extend_domain_with_range(space,
8727 isl_space_copy(data->range));
8728 pma = isl_pw_multi_aff_reset_space(pma, space);
8729 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8731 return data->res ? isl_stat_ok : isl_stat_error;
8734 /* Replace the range space of all the piecewise affine expressions in "upma" by
8735 * the range space of "space".
8737 * This assumes that all these expressions have the same output dimension.
8739 * Since the spaces of the expressions change, so do their hash values.
8740 * We therefore need to create a new isl_union_pw_multi_aff.
8741 * Note that the hash value is currently computed based on the entire
8742 * space even though there can only be a single expression with a given
8743 * domain space.
8745 static __isl_give isl_union_pw_multi_aff *
8746 isl_union_pw_multi_aff_reset_range_space(
8747 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8749 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8750 isl_space *space_upma;
8752 space_upma = isl_union_pw_multi_aff_get_space(upma);
8753 data.res = isl_union_pw_multi_aff_empty(space_upma);
8754 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8755 &reset_range_space, &data) < 0)
8756 data.res = isl_union_pw_multi_aff_free(data.res);
8758 isl_space_free(space);
8759 isl_union_pw_multi_aff_free(upma);
8760 return data.res;
8763 /* Construct and return a union piecewise multi affine expression
8764 * that is equal to the given multi union piecewise affine expression,
8765 * in the special case of a 0D multi union piecewise affine expression.
8767 * Construct a union piecewise multi affine expression
8768 * on top of the explicit domain of the input.
8770 __isl_give isl_union_pw_multi_aff *
8771 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8772 __isl_take isl_multi_union_pw_aff *mupa)
8774 isl_space *space;
8775 isl_multi_val *mv;
8776 isl_union_set *domain;
8778 space = isl_multi_union_pw_aff_get_space(mupa);
8779 mv = isl_multi_val_zero(space);
8780 domain = isl_multi_union_pw_aff_domain(mupa);
8781 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8784 /* Construct and return a union piecewise multi affine expression
8785 * that is equal to the given multi union piecewise affine expression.
8787 * If the input is zero-dimensional, then
8788 * construct a union piecewise multi affine expression
8789 * on top of the explicit domain of the input.
8791 __isl_give isl_union_pw_multi_aff *
8792 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8793 __isl_take isl_multi_union_pw_aff *mupa)
8795 int i, n;
8796 isl_space *space;
8797 isl_union_pw_multi_aff *upma;
8798 isl_union_pw_aff *upa;
8800 if (!mupa)
8801 return NULL;
8803 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8804 if (n == 0)
8805 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8807 space = isl_multi_union_pw_aff_get_space(mupa);
8808 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8809 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8811 for (i = 1; i < n; ++i) {
8812 isl_union_pw_multi_aff *upma_i;
8814 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8815 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8816 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8819 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8821 isl_multi_union_pw_aff_free(mupa);
8822 return upma;
8825 /* Intersect the range of "mupa" with "range",
8826 * in the special case where "mupa" is 0D.
8828 * Intersect the domain of "mupa" with the constraints on the parameters
8829 * of "range".
8831 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8832 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8834 range = isl_set_params(range);
8835 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8836 return mupa;
8839 /* Intersect the range of "mupa" with "range".
8840 * That is, keep only those domain elements that have a function value
8841 * in "range".
8843 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8844 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8846 isl_union_pw_multi_aff *upma;
8847 isl_union_set *domain;
8848 isl_space *space;
8849 int n;
8850 int match;
8852 if (!mupa || !range)
8853 goto error;
8855 space = isl_set_get_space(range);
8856 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8857 space, isl_dim_set);
8858 isl_space_free(space);
8859 if (match < 0)
8860 goto error;
8861 if (!match)
8862 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8863 "space don't match", goto error);
8864 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8865 if (n == 0)
8866 return mupa_intersect_range_0D(mupa, range);
8868 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8869 isl_multi_union_pw_aff_copy(mupa));
8870 domain = isl_union_set_from_set(range);
8871 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8872 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8874 return mupa;
8875 error:
8876 isl_multi_union_pw_aff_free(mupa);
8877 isl_set_free(range);
8878 return NULL;
8881 /* Return the shared domain of the elements of "mupa",
8882 * in the special case where "mupa" is zero-dimensional.
8884 * Return the explicit domain of "mupa".
8885 * Note that this domain may be a parameter set, either
8886 * because "mupa" is meant to live in a set space or
8887 * because no explicit domain has been set.
8889 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8890 __isl_take isl_multi_union_pw_aff *mupa)
8892 isl_union_set *dom;
8894 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8895 isl_multi_union_pw_aff_free(mupa);
8897 return dom;
8900 /* Return the shared domain of the elements of "mupa".
8902 * If "mupa" is zero-dimensional, then return its explicit domain.
8904 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8905 __isl_take isl_multi_union_pw_aff *mupa)
8907 int i, n;
8908 isl_union_pw_aff *upa;
8909 isl_union_set *dom;
8911 if (!mupa)
8912 return NULL;
8914 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8915 if (n == 0)
8916 return isl_multi_union_pw_aff_domain_0D(mupa);
8918 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8919 dom = isl_union_pw_aff_domain(upa);
8920 for (i = 1; i < n; ++i) {
8921 isl_union_set *dom_i;
8923 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8924 dom_i = isl_union_pw_aff_domain(upa);
8925 dom = isl_union_set_intersect(dom, dom_i);
8928 isl_multi_union_pw_aff_free(mupa);
8929 return dom;
8932 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8933 * In particular, the spaces have been aligned.
8934 * The result is defined over the shared domain of the elements of "mupa"
8936 * We first extract the parametric constant part of "aff" and
8937 * define that over the shared domain.
8938 * Then we iterate over all input dimensions of "aff" and add the corresponding
8939 * multiples of the elements of "mupa".
8940 * Finally, we consider the integer divisions, calling the function
8941 * recursively to obtain an isl_union_pw_aff corresponding to the
8942 * integer division argument.
8944 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8945 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8947 int i, n_in, n_div;
8948 isl_union_pw_aff *upa;
8949 isl_union_set *uset;
8950 isl_val *v;
8951 isl_aff *cst;
8953 n_in = isl_aff_dim(aff, isl_dim_in);
8954 n_div = isl_aff_dim(aff, isl_dim_div);
8956 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8957 cst = isl_aff_copy(aff);
8958 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8959 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8960 cst = isl_aff_project_domain_on_params(cst);
8961 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8963 for (i = 0; i < n_in; ++i) {
8964 isl_union_pw_aff *upa_i;
8966 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8967 continue;
8968 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8969 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8970 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8971 upa = isl_union_pw_aff_add(upa, upa_i);
8974 for (i = 0; i < n_div; ++i) {
8975 isl_aff *div;
8976 isl_union_pw_aff *upa_i;
8978 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8979 continue;
8980 div = isl_aff_get_div(aff, i);
8981 upa_i = multi_union_pw_aff_apply_aff(
8982 isl_multi_union_pw_aff_copy(mupa), div);
8983 upa_i = isl_union_pw_aff_floor(upa_i);
8984 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8985 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8986 upa = isl_union_pw_aff_add(upa, upa_i);
8989 isl_multi_union_pw_aff_free(mupa);
8990 isl_aff_free(aff);
8992 return upa;
8995 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8996 * with the domain of "aff".
8997 * Furthermore, the dimension of this space needs to be greater than zero.
8998 * The result is defined over the shared domain of the elements of "mupa"
9000 * We perform these checks and then hand over control to
9001 * multi_union_pw_aff_apply_aff.
9003 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9004 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9006 isl_space *space1, *space2;
9007 int equal;
9009 mupa = isl_multi_union_pw_aff_align_params(mupa,
9010 isl_aff_get_space(aff));
9011 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9012 if (!mupa || !aff)
9013 goto error;
9015 space1 = isl_multi_union_pw_aff_get_space(mupa);
9016 space2 = isl_aff_get_domain_space(aff);
9017 equal = isl_space_is_equal(space1, space2);
9018 isl_space_free(space1);
9019 isl_space_free(space2);
9020 if (equal < 0)
9021 goto error;
9022 if (!equal)
9023 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9024 "spaces don't match", goto error);
9025 if (isl_aff_dim(aff, isl_dim_in) == 0)
9026 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9027 "cannot determine domains", goto error);
9029 return multi_union_pw_aff_apply_aff(mupa, aff);
9030 error:
9031 isl_multi_union_pw_aff_free(mupa);
9032 isl_aff_free(aff);
9033 return NULL;
9036 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9037 * The space of "mupa" is known to be compatible with the domain of "ma".
9039 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9040 * on the domain of "mupa".
9042 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9043 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9045 isl_union_set *dom;
9047 dom = isl_multi_union_pw_aff_domain(mupa);
9048 ma = isl_multi_aff_project_domain_on_params(ma);
9050 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9053 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9054 * with the domain of "ma".
9055 * The result is defined over the shared domain of the elements of "mupa"
9057 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9058 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9060 isl_space *space1, *space2;
9061 isl_multi_union_pw_aff *res;
9062 int equal;
9063 int i, n_out;
9065 mupa = isl_multi_union_pw_aff_align_params(mupa,
9066 isl_multi_aff_get_space(ma));
9067 ma = isl_multi_aff_align_params(ma,
9068 isl_multi_union_pw_aff_get_space(mupa));
9069 if (!mupa || !ma)
9070 goto error;
9072 space1 = isl_multi_union_pw_aff_get_space(mupa);
9073 space2 = isl_multi_aff_get_domain_space(ma);
9074 equal = isl_space_is_equal(space1, space2);
9075 isl_space_free(space1);
9076 isl_space_free(space2);
9077 if (equal < 0)
9078 goto error;
9079 if (!equal)
9080 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9081 "spaces don't match", goto error);
9082 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9083 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9084 return mupa_apply_multi_aff_0D(mupa, ma);
9086 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9087 res = isl_multi_union_pw_aff_alloc(space1);
9089 for (i = 0; i < n_out; ++i) {
9090 isl_aff *aff;
9091 isl_union_pw_aff *upa;
9093 aff = isl_multi_aff_get_aff(ma, i);
9094 upa = multi_union_pw_aff_apply_aff(
9095 isl_multi_union_pw_aff_copy(mupa), aff);
9096 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9099 isl_multi_aff_free(ma);
9100 isl_multi_union_pw_aff_free(mupa);
9101 return res;
9102 error:
9103 isl_multi_union_pw_aff_free(mupa);
9104 isl_multi_aff_free(ma);
9105 return NULL;
9108 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9109 * The space of "mupa" is known to be compatible with the domain of "pa".
9111 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9112 * on the domain of "mupa".
9114 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9115 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9117 isl_union_set *dom;
9119 dom = isl_multi_union_pw_aff_domain(mupa);
9120 pa = isl_pw_aff_project_domain_on_params(pa);
9122 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9125 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9126 * with the domain of "pa".
9127 * Furthermore, the dimension of this space needs to be greater than zero.
9128 * The result is defined over the shared domain of the elements of "mupa"
9130 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9131 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9133 int i;
9134 int equal;
9135 isl_space *space, *space2;
9136 isl_union_pw_aff *upa;
9138 mupa = isl_multi_union_pw_aff_align_params(mupa,
9139 isl_pw_aff_get_space(pa));
9140 pa = isl_pw_aff_align_params(pa,
9141 isl_multi_union_pw_aff_get_space(mupa));
9142 if (!mupa || !pa)
9143 goto error;
9145 space = isl_multi_union_pw_aff_get_space(mupa);
9146 space2 = isl_pw_aff_get_domain_space(pa);
9147 equal = isl_space_is_equal(space, space2);
9148 isl_space_free(space);
9149 isl_space_free(space2);
9150 if (equal < 0)
9151 goto error;
9152 if (!equal)
9153 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9154 "spaces don't match", goto error);
9155 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9156 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9158 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9159 upa = isl_union_pw_aff_empty(space);
9161 for (i = 0; i < pa->n; ++i) {
9162 isl_aff *aff;
9163 isl_set *domain;
9164 isl_multi_union_pw_aff *mupa_i;
9165 isl_union_pw_aff *upa_i;
9167 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9168 domain = isl_set_copy(pa->p[i].set);
9169 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9170 aff = isl_aff_copy(pa->p[i].aff);
9171 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9172 upa = isl_union_pw_aff_union_add(upa, upa_i);
9175 isl_multi_union_pw_aff_free(mupa);
9176 isl_pw_aff_free(pa);
9177 return upa;
9178 error:
9179 isl_multi_union_pw_aff_free(mupa);
9180 isl_pw_aff_free(pa);
9181 return NULL;
9184 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9185 * The space of "mupa" is known to be compatible with the domain of "pma".
9187 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9188 * on the domain of "mupa".
9190 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9191 __isl_take isl_multi_union_pw_aff *mupa,
9192 __isl_take isl_pw_multi_aff *pma)
9194 isl_union_set *dom;
9196 dom = isl_multi_union_pw_aff_domain(mupa);
9197 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9199 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9202 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9203 * with the domain of "pma".
9204 * The result is defined over the shared domain of the elements of "mupa"
9206 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9207 __isl_take isl_multi_union_pw_aff *mupa,
9208 __isl_take isl_pw_multi_aff *pma)
9210 isl_space *space1, *space2;
9211 isl_multi_union_pw_aff *res;
9212 int equal;
9213 int i, n_out;
9215 mupa = isl_multi_union_pw_aff_align_params(mupa,
9216 isl_pw_multi_aff_get_space(pma));
9217 pma = isl_pw_multi_aff_align_params(pma,
9218 isl_multi_union_pw_aff_get_space(mupa));
9219 if (!mupa || !pma)
9220 goto error;
9222 space1 = isl_multi_union_pw_aff_get_space(mupa);
9223 space2 = isl_pw_multi_aff_get_domain_space(pma);
9224 equal = isl_space_is_equal(space1, space2);
9225 isl_space_free(space1);
9226 isl_space_free(space2);
9227 if (equal < 0)
9228 goto error;
9229 if (!equal)
9230 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9231 "spaces don't match", goto error);
9232 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9233 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9234 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9236 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9237 res = isl_multi_union_pw_aff_alloc(space1);
9239 for (i = 0; i < n_out; ++i) {
9240 isl_pw_aff *pa;
9241 isl_union_pw_aff *upa;
9243 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9244 upa = isl_multi_union_pw_aff_apply_pw_aff(
9245 isl_multi_union_pw_aff_copy(mupa), pa);
9246 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9249 isl_pw_multi_aff_free(pma);
9250 isl_multi_union_pw_aff_free(mupa);
9251 return res;
9252 error:
9253 isl_multi_union_pw_aff_free(mupa);
9254 isl_pw_multi_aff_free(pma);
9255 return NULL;
9258 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9259 * If the explicit domain only keeps track of constraints on the parameters,
9260 * then only update those constraints.
9262 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9263 __isl_take isl_multi_union_pw_aff *mupa,
9264 __isl_keep isl_union_pw_multi_aff *upma)
9266 isl_bool is_params;
9268 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9269 return isl_multi_union_pw_aff_free(mupa);
9271 mupa = isl_multi_union_pw_aff_cow(mupa);
9272 if (!mupa)
9273 return NULL;
9275 is_params = isl_union_set_is_params(mupa->u.dom);
9276 if (is_params < 0)
9277 return isl_multi_union_pw_aff_free(mupa);
9279 upma = isl_union_pw_multi_aff_copy(upma);
9280 if (is_params)
9281 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9282 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9283 else
9284 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9285 mupa->u.dom, upma);
9286 if (!mupa->u.dom)
9287 return isl_multi_union_pw_aff_free(mupa);
9288 return mupa;
9291 /* Compute the pullback of "mupa" by the function represented by "upma".
9292 * In other words, plug in "upma" in "mupa". The result contains
9293 * expressions defined over the domain space of "upma".
9295 * Run over all elements of "mupa" and plug in "upma" in each of them.
9297 * If "mupa" has an explicit domain, then it is this domain
9298 * that needs to undergo a pullback instead, i.e., a preimage.
9300 __isl_give isl_multi_union_pw_aff *
9301 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9302 __isl_take isl_multi_union_pw_aff *mupa,
9303 __isl_take isl_union_pw_multi_aff *upma)
9305 int i, n;
9307 mupa = isl_multi_union_pw_aff_align_params(mupa,
9308 isl_union_pw_multi_aff_get_space(upma));
9309 upma = isl_union_pw_multi_aff_align_params(upma,
9310 isl_multi_union_pw_aff_get_space(mupa));
9311 mupa = isl_multi_union_pw_aff_cow(mupa);
9312 if (!mupa || !upma)
9313 goto error;
9315 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9316 for (i = 0; i < n; ++i) {
9317 isl_union_pw_aff *upa;
9319 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9320 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9321 isl_union_pw_multi_aff_copy(upma));
9322 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9325 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9326 mupa = preimage_explicit_domain(mupa, upma);
9328 isl_union_pw_multi_aff_free(upma);
9329 return mupa;
9330 error:
9331 isl_multi_union_pw_aff_free(mupa);
9332 isl_union_pw_multi_aff_free(upma);
9333 return NULL;
9336 /* Extract the sequence of elements in "mupa" with domain space "space"
9337 * (ignoring parameters).
9339 * For the elements of "mupa" that are not defined on the specified space,
9340 * the corresponding element in the result is empty.
9342 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9343 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9345 int i, n;
9346 isl_bool equal_params;
9347 isl_space *space_mpa = NULL;
9348 isl_multi_pw_aff *mpa;
9350 if (!mupa || !space)
9351 goto error;
9353 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9354 equal_params = isl_space_has_equal_params(space_mpa, space);
9355 if (equal_params < 0)
9356 goto error;
9357 if (!equal_params) {
9358 space = isl_space_drop_dims(space, isl_dim_param,
9359 0, isl_space_dim(space, isl_dim_param));
9360 space = isl_space_align_params(space,
9361 isl_space_copy(space_mpa));
9362 if (!space)
9363 goto error;
9365 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9366 space_mpa);
9367 mpa = isl_multi_pw_aff_alloc(space_mpa);
9369 space = isl_space_from_domain(space);
9370 space = isl_space_add_dims(space, isl_dim_out, 1);
9371 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9372 for (i = 0; i < n; ++i) {
9373 isl_union_pw_aff *upa;
9374 isl_pw_aff *pa;
9376 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9377 pa = isl_union_pw_aff_extract_pw_aff(upa,
9378 isl_space_copy(space));
9379 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9380 isl_union_pw_aff_free(upa);
9383 isl_space_free(space);
9384 return mpa;
9385 error:
9386 isl_space_free(space_mpa);
9387 isl_space_free(space);
9388 return NULL;