drop ISL_DIM_H defines
[isl.git] / isl_aff.c
blob5b277841d72e91745bc6e09347d863be599e151b
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 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
25 #include <isl/constraint.h>
26 #include <isl_seq.h>
27 #include <isl/set.h>
28 #include <isl_val_private.h>
29 #include <isl_point_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 /* Return the domain space of "aff".
351 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
353 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
356 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
358 return isl_space_copy(isl_aff_peek_domain_space(aff));
361 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
363 isl_space *space;
364 if (!aff)
365 return NULL;
366 space = isl_local_space_get_space(aff->ls);
367 space = isl_space_from_domain(space);
368 space = isl_space_add_dims(space, isl_dim_out, 1);
369 return space;
372 __isl_give isl_local_space *isl_aff_get_domain_local_space(
373 __isl_keep isl_aff *aff)
375 return aff ? isl_local_space_copy(aff->ls) : NULL;
378 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
380 isl_local_space *ls;
381 if (!aff)
382 return NULL;
383 ls = isl_local_space_copy(aff->ls);
384 ls = isl_local_space_from_domain(ls);
385 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
386 return ls;
389 /* Return the local space of the domain of "aff".
390 * This may be either a copy or the local space itself
391 * if there is only one reference to "aff".
392 * This allows the local space to be modified inplace
393 * if both the expression and its local space have only a single reference.
394 * The caller is not allowed to modify "aff" between this call and
395 * a subsequent call to isl_aff_restore_domain_local_space.
396 * The only exception is that isl_aff_free can be called instead.
398 __isl_give isl_local_space *isl_aff_take_domain_local_space(
399 __isl_keep isl_aff *aff)
401 isl_local_space *ls;
403 if (!aff)
404 return NULL;
405 if (aff->ref != 1)
406 return isl_aff_get_domain_local_space(aff);
407 ls = aff->ls;
408 aff->ls = NULL;
409 return ls;
412 /* Set the local space of the domain of "aff" to "ls",
413 * where the local space of "aff" may be missing
414 * due to a preceding call to isl_aff_take_domain_local_space.
415 * However, in this case, "aff" only has a single reference and
416 * then the call to isl_aff_cow has no effect.
418 __isl_give isl_aff *isl_aff_restore_domain_local_space(
419 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
421 if (!aff || !ls)
422 goto error;
424 if (aff->ls == ls) {
425 isl_local_space_free(ls);
426 return aff;
429 aff = isl_aff_cow(aff);
430 if (!aff)
431 goto error;
432 isl_local_space_free(aff->ls);
433 aff->ls = ls;
435 return aff;
436 error:
437 isl_aff_free(aff);
438 isl_local_space_free(ls);
439 return NULL;
442 /* Externally, an isl_aff has a map space, but internally, the
443 * ls field corresponds to the domain of that space.
445 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
446 enum isl_dim_type type, unsigned pos)
448 if (!aff)
449 return NULL;
450 if (type == isl_dim_out)
451 return NULL;
452 if (type == isl_dim_in)
453 type = isl_dim_set;
454 return isl_local_space_get_dim_name(aff->ls, type, pos);
457 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
458 __isl_take isl_space *dim)
460 aff = isl_aff_cow(aff);
461 if (!aff || !dim)
462 goto error;
464 aff->ls = isl_local_space_reset_space(aff->ls, dim);
465 if (!aff->ls)
466 return isl_aff_free(aff);
468 return aff;
469 error:
470 isl_aff_free(aff);
471 isl_space_free(dim);
472 return NULL;
475 /* Reset the space of "aff". This function is called from isl_pw_templ.c
476 * and doesn't know if the space of an element object is represented
477 * directly or through its domain. It therefore passes along both.
479 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
480 __isl_take isl_space *space, __isl_take isl_space *domain)
482 isl_space_free(space);
483 return isl_aff_reset_domain_space(aff, domain);
486 /* Reorder the coefficients of the affine expression based
487 * on the given reordering.
488 * The reordering r is assumed to have been extended with the local
489 * variables.
491 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
492 __isl_take isl_reordering *r, int n_div)
494 isl_vec *res;
495 int i;
497 if (!vec || !r)
498 goto error;
500 res = isl_vec_alloc(vec->ctx,
501 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
502 if (!res)
503 goto error;
504 isl_seq_cpy(res->el, vec->el, 2);
505 isl_seq_clr(res->el + 2, res->size - 2);
506 for (i = 0; i < r->len; ++i)
507 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
509 isl_reordering_free(r);
510 isl_vec_free(vec);
511 return res;
512 error:
513 isl_vec_free(vec);
514 isl_reordering_free(r);
515 return NULL;
518 /* Reorder the dimensions of the domain of "aff" according
519 * to the given reordering.
521 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
522 __isl_take isl_reordering *r)
524 aff = isl_aff_cow(aff);
525 if (!aff)
526 goto error;
528 r = isl_reordering_extend(r, aff->ls->div->n_row);
529 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
530 aff->ls->div->n_row);
531 aff->ls = isl_local_space_realign(aff->ls, r);
533 if (!aff->v || !aff->ls)
534 return isl_aff_free(aff);
536 return aff;
537 error:
538 isl_aff_free(aff);
539 isl_reordering_free(r);
540 return NULL;
543 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
544 __isl_take isl_space *model)
546 isl_bool equal_params;
548 if (!aff || !model)
549 goto error;
551 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
552 if (equal_params < 0)
553 goto error;
554 if (!equal_params) {
555 isl_reordering *exp;
557 model = isl_space_drop_dims(model, isl_dim_in,
558 0, isl_space_dim(model, isl_dim_in));
559 model = isl_space_drop_dims(model, isl_dim_out,
560 0, isl_space_dim(model, isl_dim_out));
561 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
562 exp = isl_reordering_extend_space(exp,
563 isl_aff_get_domain_space(aff));
564 aff = isl_aff_realign_domain(aff, exp);
567 isl_space_free(model);
568 return aff;
569 error:
570 isl_space_free(model);
571 isl_aff_free(aff);
572 return NULL;
575 /* Is "aff" obviously equal to zero?
577 * If the denominator is zero, then "aff" is not equal to zero.
579 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
581 if (!aff)
582 return isl_bool_error;
584 if (isl_int_is_zero(aff->v->el[0]))
585 return isl_bool_false;
586 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
589 /* Does "aff" represent NaN?
591 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
593 if (!aff)
594 return isl_bool_error;
596 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
599 /* Are "aff1" and "aff2" obviously equal?
601 * NaN is not equal to anything, not even to another NaN.
603 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
604 __isl_keep isl_aff *aff2)
606 isl_bool equal;
608 if (!aff1 || !aff2)
609 return isl_bool_error;
611 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
612 return isl_bool_false;
614 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
615 if (equal < 0 || !equal)
616 return equal;
618 return isl_vec_is_equal(aff1->v, aff2->v);
621 /* Return the common denominator of "aff" in "v".
623 * We cannot return anything meaningful in case of a NaN.
625 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
627 if (!aff)
628 return isl_stat_error;
629 if (isl_aff_is_nan(aff))
630 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
631 "cannot get denominator of NaN", return isl_stat_error);
632 isl_int_set(*v, aff->v->el[0]);
633 return isl_stat_ok;
636 /* Return the common denominator of "aff".
638 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
640 isl_ctx *ctx;
642 if (!aff)
643 return NULL;
645 ctx = isl_aff_get_ctx(aff);
646 if (isl_aff_is_nan(aff))
647 return isl_val_nan(ctx);
648 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
651 /* Return the constant term of "aff".
653 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
655 isl_ctx *ctx;
656 isl_val *v;
658 if (!aff)
659 return NULL;
661 ctx = isl_aff_get_ctx(aff);
662 if (isl_aff_is_nan(aff))
663 return isl_val_nan(ctx);
664 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
665 return isl_val_normalize(v);
668 /* Return the coefficient of the variable of type "type" at position "pos"
669 * of "aff".
671 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
672 enum isl_dim_type type, int pos)
674 isl_ctx *ctx;
675 isl_val *v;
677 if (!aff)
678 return NULL;
680 ctx = isl_aff_get_ctx(aff);
681 if (type == isl_dim_out)
682 isl_die(ctx, isl_error_invalid,
683 "output/set dimension does not have a coefficient",
684 return NULL);
685 if (type == isl_dim_in)
686 type = isl_dim_set;
688 if (pos >= isl_local_space_dim(aff->ls, type))
689 isl_die(ctx, isl_error_invalid,
690 "position out of bounds", return NULL);
692 if (isl_aff_is_nan(aff))
693 return isl_val_nan(ctx);
694 pos += isl_local_space_offset(aff->ls, type);
695 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
696 return isl_val_normalize(v);
699 /* Return the sign of the coefficient of the variable of type "type"
700 * at position "pos" of "aff".
702 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
703 int pos)
705 isl_ctx *ctx;
707 if (!aff)
708 return 0;
710 ctx = isl_aff_get_ctx(aff);
711 if (type == isl_dim_out)
712 isl_die(ctx, isl_error_invalid,
713 "output/set dimension does not have a coefficient",
714 return 0);
715 if (type == isl_dim_in)
716 type = isl_dim_set;
718 if (pos >= isl_local_space_dim(aff->ls, type))
719 isl_die(ctx, isl_error_invalid,
720 "position out of bounds", return 0);
722 pos += isl_local_space_offset(aff->ls, type);
723 return isl_int_sgn(aff->v->el[1 + pos]);
726 /* Replace the numerator of the constant term of "aff" by "v".
728 * A NaN is unaffected by this operation.
730 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
732 if (!aff)
733 return NULL;
734 if (isl_aff_is_nan(aff))
735 return aff;
736 aff = isl_aff_cow(aff);
737 if (!aff)
738 return NULL;
740 aff->v = isl_vec_cow(aff->v);
741 if (!aff->v)
742 return isl_aff_free(aff);
744 isl_int_set(aff->v->el[1], v);
746 return aff;
749 /* Replace the constant term of "aff" by "v".
751 * A NaN is unaffected by this operation.
753 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
754 __isl_take isl_val *v)
756 if (!aff || !v)
757 goto error;
759 if (isl_aff_is_nan(aff)) {
760 isl_val_free(v);
761 return aff;
764 if (!isl_val_is_rat(v))
765 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
766 "expecting rational value", goto error);
768 if (isl_int_eq(aff->v->el[1], v->n) &&
769 isl_int_eq(aff->v->el[0], v->d)) {
770 isl_val_free(v);
771 return aff;
774 aff = isl_aff_cow(aff);
775 if (!aff)
776 goto error;
777 aff->v = isl_vec_cow(aff->v);
778 if (!aff->v)
779 goto error;
781 if (isl_int_eq(aff->v->el[0], v->d)) {
782 isl_int_set(aff->v->el[1], v->n);
783 } else if (isl_int_is_one(v->d)) {
784 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
785 } else {
786 isl_seq_scale(aff->v->el + 1,
787 aff->v->el + 1, v->d, aff->v->size - 1);
788 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
789 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
790 aff->v = isl_vec_normalize(aff->v);
791 if (!aff->v)
792 goto error;
795 isl_val_free(v);
796 return aff;
797 error:
798 isl_aff_free(aff);
799 isl_val_free(v);
800 return NULL;
803 /* Add "v" to the constant term of "aff".
805 * A NaN is unaffected by this operation.
807 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
809 if (isl_int_is_zero(v))
810 return aff;
812 if (!aff)
813 return NULL;
814 if (isl_aff_is_nan(aff))
815 return aff;
816 aff = isl_aff_cow(aff);
817 if (!aff)
818 return NULL;
820 aff->v = isl_vec_cow(aff->v);
821 if (!aff->v)
822 return isl_aff_free(aff);
824 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
826 return aff;
829 /* Add "v" to the constant term of "aff".
831 * A NaN is unaffected by this operation.
833 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
834 __isl_take isl_val *v)
836 if (!aff || !v)
837 goto error;
839 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
840 isl_val_free(v);
841 return aff;
844 if (!isl_val_is_rat(v))
845 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
846 "expecting rational value", goto error);
848 aff = isl_aff_cow(aff);
849 if (!aff)
850 goto error;
852 aff->v = isl_vec_cow(aff->v);
853 if (!aff->v)
854 goto error;
856 if (isl_int_is_one(v->d)) {
857 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
858 } else if (isl_int_eq(aff->v->el[0], v->d)) {
859 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
860 aff->v = isl_vec_normalize(aff->v);
861 if (!aff->v)
862 goto error;
863 } else {
864 isl_seq_scale(aff->v->el + 1,
865 aff->v->el + 1, v->d, aff->v->size - 1);
866 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
867 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
868 aff->v = isl_vec_normalize(aff->v);
869 if (!aff->v)
870 goto error;
873 isl_val_free(v);
874 return aff;
875 error:
876 isl_aff_free(aff);
877 isl_val_free(v);
878 return NULL;
881 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
883 isl_int t;
885 isl_int_init(t);
886 isl_int_set_si(t, v);
887 aff = isl_aff_add_constant(aff, t);
888 isl_int_clear(t);
890 return aff;
893 /* Add "v" to the numerator of the constant term of "aff".
895 * A NaN is unaffected by this operation.
897 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
899 if (isl_int_is_zero(v))
900 return aff;
902 if (!aff)
903 return NULL;
904 if (isl_aff_is_nan(aff))
905 return aff;
906 aff = isl_aff_cow(aff);
907 if (!aff)
908 return NULL;
910 aff->v = isl_vec_cow(aff->v);
911 if (!aff->v)
912 return isl_aff_free(aff);
914 isl_int_add(aff->v->el[1], aff->v->el[1], v);
916 return aff;
919 /* Add "v" to the numerator of the constant term of "aff".
921 * A NaN is unaffected by this operation.
923 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
925 isl_int t;
927 if (v == 0)
928 return aff;
930 isl_int_init(t);
931 isl_int_set_si(t, v);
932 aff = isl_aff_add_constant_num(aff, t);
933 isl_int_clear(t);
935 return aff;
938 /* Replace the numerator of the constant term of "aff" by "v".
940 * A NaN is unaffected by this operation.
942 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
944 if (!aff)
945 return NULL;
946 if (isl_aff_is_nan(aff))
947 return aff;
948 aff = isl_aff_cow(aff);
949 if (!aff)
950 return NULL;
952 aff->v = isl_vec_cow(aff->v);
953 if (!aff->v)
954 return isl_aff_free(aff);
956 isl_int_set_si(aff->v->el[1], v);
958 return aff;
961 /* Replace the numerator of the coefficient of the variable of type "type"
962 * at position "pos" of "aff" by "v".
964 * A NaN is unaffected by this operation.
966 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
967 enum isl_dim_type type, int pos, isl_int v)
969 if (!aff)
970 return NULL;
972 if (type == isl_dim_out)
973 isl_die(aff->v->ctx, isl_error_invalid,
974 "output/set dimension does not have a coefficient",
975 return isl_aff_free(aff));
976 if (type == isl_dim_in)
977 type = isl_dim_set;
979 if (pos >= isl_local_space_dim(aff->ls, type))
980 isl_die(aff->v->ctx, isl_error_invalid,
981 "position out of bounds", return isl_aff_free(aff));
983 if (isl_aff_is_nan(aff))
984 return aff;
985 aff = isl_aff_cow(aff);
986 if (!aff)
987 return NULL;
989 aff->v = isl_vec_cow(aff->v);
990 if (!aff->v)
991 return isl_aff_free(aff);
993 pos += isl_local_space_offset(aff->ls, type);
994 isl_int_set(aff->v->el[1 + pos], v);
996 return aff;
999 /* Replace the numerator of the coefficient of the variable of type "type"
1000 * at position "pos" of "aff" by "v".
1002 * A NaN is unaffected by this operation.
1004 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1005 enum isl_dim_type type, int pos, int v)
1007 if (!aff)
1008 return NULL;
1010 if (type == isl_dim_out)
1011 isl_die(aff->v->ctx, isl_error_invalid,
1012 "output/set dimension does not have a coefficient",
1013 return isl_aff_free(aff));
1014 if (type == isl_dim_in)
1015 type = isl_dim_set;
1017 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1018 isl_die(aff->v->ctx, isl_error_invalid,
1019 "position out of bounds", return isl_aff_free(aff));
1021 if (isl_aff_is_nan(aff))
1022 return aff;
1023 pos += isl_local_space_offset(aff->ls, type);
1024 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1025 return aff;
1027 aff = isl_aff_cow(aff);
1028 if (!aff)
1029 return NULL;
1031 aff->v = isl_vec_cow(aff->v);
1032 if (!aff->v)
1033 return isl_aff_free(aff);
1035 isl_int_set_si(aff->v->el[1 + pos], v);
1037 return aff;
1040 /* Replace the coefficient of the variable of type "type" at position "pos"
1041 * of "aff" by "v".
1043 * A NaN is unaffected by this operation.
1045 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1046 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1048 if (!aff || !v)
1049 goto error;
1051 if (type == isl_dim_out)
1052 isl_die(aff->v->ctx, isl_error_invalid,
1053 "output/set dimension does not have a coefficient",
1054 goto error);
1055 if (type == isl_dim_in)
1056 type = isl_dim_set;
1058 if (pos >= isl_local_space_dim(aff->ls, type))
1059 isl_die(aff->v->ctx, isl_error_invalid,
1060 "position out of bounds", goto error);
1062 if (isl_aff_is_nan(aff)) {
1063 isl_val_free(v);
1064 return aff;
1066 if (!isl_val_is_rat(v))
1067 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1068 "expecting rational value", goto error);
1070 pos += isl_local_space_offset(aff->ls, type);
1071 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1072 isl_int_eq(aff->v->el[0], v->d)) {
1073 isl_val_free(v);
1074 return aff;
1077 aff = isl_aff_cow(aff);
1078 if (!aff)
1079 goto error;
1080 aff->v = isl_vec_cow(aff->v);
1081 if (!aff->v)
1082 goto error;
1084 if (isl_int_eq(aff->v->el[0], v->d)) {
1085 isl_int_set(aff->v->el[1 + pos], v->n);
1086 } else if (isl_int_is_one(v->d)) {
1087 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1088 } else {
1089 isl_seq_scale(aff->v->el + 1,
1090 aff->v->el + 1, v->d, aff->v->size - 1);
1091 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1092 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1093 aff->v = isl_vec_normalize(aff->v);
1094 if (!aff->v)
1095 goto error;
1098 isl_val_free(v);
1099 return aff;
1100 error:
1101 isl_aff_free(aff);
1102 isl_val_free(v);
1103 return NULL;
1106 /* Add "v" to the coefficient of the variable of type "type"
1107 * at position "pos" of "aff".
1109 * A NaN is unaffected by this operation.
1111 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1112 enum isl_dim_type type, int pos, isl_int v)
1114 if (!aff)
1115 return NULL;
1117 if (type == isl_dim_out)
1118 isl_die(aff->v->ctx, isl_error_invalid,
1119 "output/set dimension does not have a coefficient",
1120 return isl_aff_free(aff));
1121 if (type == isl_dim_in)
1122 type = isl_dim_set;
1124 if (pos >= isl_local_space_dim(aff->ls, type))
1125 isl_die(aff->v->ctx, isl_error_invalid,
1126 "position out of bounds", return isl_aff_free(aff));
1128 if (isl_aff_is_nan(aff))
1129 return aff;
1130 aff = isl_aff_cow(aff);
1131 if (!aff)
1132 return NULL;
1134 aff->v = isl_vec_cow(aff->v);
1135 if (!aff->v)
1136 return isl_aff_free(aff);
1138 pos += isl_local_space_offset(aff->ls, type);
1139 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1141 return aff;
1144 /* Add "v" to the coefficient of the variable of type "type"
1145 * at position "pos" of "aff".
1147 * A NaN is unaffected by this operation.
1149 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1150 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1152 if (!aff || !v)
1153 goto error;
1155 if (isl_val_is_zero(v)) {
1156 isl_val_free(v);
1157 return aff;
1160 if (type == isl_dim_out)
1161 isl_die(aff->v->ctx, isl_error_invalid,
1162 "output/set dimension does not have a coefficient",
1163 goto error);
1164 if (type == isl_dim_in)
1165 type = isl_dim_set;
1167 if (pos >= isl_local_space_dim(aff->ls, type))
1168 isl_die(aff->v->ctx, isl_error_invalid,
1169 "position out of bounds", goto error);
1171 if (isl_aff_is_nan(aff)) {
1172 isl_val_free(v);
1173 return aff;
1175 if (!isl_val_is_rat(v))
1176 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1177 "expecting rational value", goto error);
1179 aff = isl_aff_cow(aff);
1180 if (!aff)
1181 goto error;
1183 aff->v = isl_vec_cow(aff->v);
1184 if (!aff->v)
1185 goto error;
1187 pos += isl_local_space_offset(aff->ls, type);
1188 if (isl_int_is_one(v->d)) {
1189 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1190 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1191 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1192 aff->v = isl_vec_normalize(aff->v);
1193 if (!aff->v)
1194 goto error;
1195 } else {
1196 isl_seq_scale(aff->v->el + 1,
1197 aff->v->el + 1, v->d, aff->v->size - 1);
1198 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1199 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1200 aff->v = isl_vec_normalize(aff->v);
1201 if (!aff->v)
1202 goto error;
1205 isl_val_free(v);
1206 return aff;
1207 error:
1208 isl_aff_free(aff);
1209 isl_val_free(v);
1210 return NULL;
1213 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1214 enum isl_dim_type type, int pos, int v)
1216 isl_int t;
1218 isl_int_init(t);
1219 isl_int_set_si(t, v);
1220 aff = isl_aff_add_coefficient(aff, type, pos, t);
1221 isl_int_clear(t);
1223 return aff;
1226 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1228 if (!aff)
1229 return NULL;
1231 return isl_local_space_get_div(aff->ls, pos);
1234 /* Return the negation of "aff".
1236 * As a special case, -NaN = NaN.
1238 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1240 if (!aff)
1241 return NULL;
1242 if (isl_aff_is_nan(aff))
1243 return aff;
1244 aff = isl_aff_cow(aff);
1245 if (!aff)
1246 return NULL;
1247 aff->v = isl_vec_cow(aff->v);
1248 if (!aff->v)
1249 return isl_aff_free(aff);
1251 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1253 return aff;
1256 /* Remove divs from the local space that do not appear in the affine
1257 * expression.
1258 * We currently only remove divs at the end.
1259 * Some intermediate divs may also not appear directly in the affine
1260 * expression, but we would also need to check that no other divs are
1261 * defined in terms of them.
1263 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1265 int pos;
1266 int off;
1267 int n;
1269 if (!aff)
1270 return NULL;
1272 n = isl_local_space_dim(aff->ls, isl_dim_div);
1273 off = isl_local_space_offset(aff->ls, isl_dim_div);
1275 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1276 if (pos == n)
1277 return aff;
1279 aff = isl_aff_cow(aff);
1280 if (!aff)
1281 return NULL;
1283 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1284 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1285 if (!aff->ls || !aff->v)
1286 return isl_aff_free(aff);
1288 return aff;
1291 /* Look for any divs in the aff->ls with a denominator equal to one
1292 * and plug them into the affine expression and any subsequent divs
1293 * that may reference the div.
1295 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1297 int i, n;
1298 int len;
1299 isl_int v;
1300 isl_vec *vec;
1301 isl_local_space *ls;
1302 unsigned pos;
1304 if (!aff)
1305 return NULL;
1307 n = isl_local_space_dim(aff->ls, isl_dim_div);
1308 len = aff->v->size;
1309 for (i = 0; i < n; ++i) {
1310 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1311 continue;
1312 ls = isl_local_space_copy(aff->ls);
1313 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1314 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1315 vec = isl_vec_copy(aff->v);
1316 vec = isl_vec_cow(vec);
1317 if (!ls || !vec)
1318 goto error;
1320 isl_int_init(v);
1322 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1323 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1324 len, len, v);
1326 isl_int_clear(v);
1328 isl_vec_free(aff->v);
1329 aff->v = vec;
1330 isl_local_space_free(aff->ls);
1331 aff->ls = ls;
1334 return aff;
1335 error:
1336 isl_vec_free(vec);
1337 isl_local_space_free(ls);
1338 return isl_aff_free(aff);
1341 /* Look for any divs j that appear with a unit coefficient inside
1342 * the definitions of other divs i and plug them into the definitions
1343 * of the divs i.
1345 * In particular, an expression of the form
1347 * floor((f(..) + floor(g(..)/n))/m)
1349 * is simplified to
1351 * floor((n * f(..) + g(..))/(n * m))
1353 * This simplification is correct because we can move the expression
1354 * f(..) into the inner floor in the original expression to obtain
1356 * floor(floor((n * f(..) + g(..))/n)/m)
1358 * from which we can derive the simplified expression.
1360 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1362 int i, j, n;
1363 int off;
1365 if (!aff)
1366 return NULL;
1368 n = isl_local_space_dim(aff->ls, isl_dim_div);
1369 off = isl_local_space_offset(aff->ls, isl_dim_div);
1370 for (i = 1; i < n; ++i) {
1371 for (j = 0; j < i; ++j) {
1372 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1373 continue;
1374 aff->ls = isl_local_space_substitute_seq(aff->ls,
1375 isl_dim_div, j, aff->ls->div->row[j],
1376 aff->v->size, i, 1);
1377 if (!aff->ls)
1378 return isl_aff_free(aff);
1382 return aff;
1385 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1387 * Even though this function is only called on isl_affs with a single
1388 * reference, we are careful to only change aff->v and aff->ls together.
1390 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1392 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1393 isl_local_space *ls;
1394 isl_vec *v;
1396 ls = isl_local_space_copy(aff->ls);
1397 ls = isl_local_space_swap_div(ls, a, b);
1398 v = isl_vec_copy(aff->v);
1399 v = isl_vec_cow(v);
1400 if (!ls || !v)
1401 goto error;
1403 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1404 isl_vec_free(aff->v);
1405 aff->v = v;
1406 isl_local_space_free(aff->ls);
1407 aff->ls = ls;
1409 return aff;
1410 error:
1411 isl_vec_free(v);
1412 isl_local_space_free(ls);
1413 return isl_aff_free(aff);
1416 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1418 * We currently do not actually remove div "b", but simply add its
1419 * coefficient to that of "a" and then zero it out.
1421 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1423 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1425 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1426 return aff;
1428 aff->v = isl_vec_cow(aff->v);
1429 if (!aff->v)
1430 return isl_aff_free(aff);
1432 isl_int_add(aff->v->el[1 + off + a],
1433 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1434 isl_int_set_si(aff->v->el[1 + off + b], 0);
1436 return aff;
1439 /* Sort the divs in the local space of "aff" according to
1440 * the comparison function "cmp_row" in isl_local_space.c,
1441 * combining the coefficients of identical divs.
1443 * Reordering divs does not change the semantics of "aff",
1444 * so there is no need to call isl_aff_cow.
1445 * Moreover, this function is currently only called on isl_affs
1446 * with a single reference.
1448 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1450 int i, j, n;
1452 if (!aff)
1453 return NULL;
1455 n = isl_aff_dim(aff, isl_dim_div);
1456 for (i = 1; i < n; ++i) {
1457 for (j = i - 1; j >= 0; --j) {
1458 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1459 if (cmp < 0)
1460 break;
1461 if (cmp == 0)
1462 aff = merge_divs(aff, j, j + 1);
1463 else
1464 aff = swap_div(aff, j, j + 1);
1465 if (!aff)
1466 return NULL;
1470 return aff;
1473 /* Normalize the representation of "aff".
1475 * This function should only be called of "new" isl_affs, i.e.,
1476 * with only a single reference. We therefore do not need to
1477 * worry about affecting other instances.
1479 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1481 if (!aff)
1482 return NULL;
1483 aff->v = isl_vec_normalize(aff->v);
1484 if (!aff->v)
1485 return isl_aff_free(aff);
1486 aff = plug_in_integral_divs(aff);
1487 aff = plug_in_unit_divs(aff);
1488 aff = sort_divs(aff);
1489 aff = isl_aff_remove_unused_divs(aff);
1490 return aff;
1493 /* Given f, return floor(f).
1494 * If f is an integer expression, then just return f.
1495 * If f is a constant, then return the constant floor(f).
1496 * Otherwise, if f = g/m, write g = q m + r,
1497 * create a new div d = [r/m] and return the expression q + d.
1498 * The coefficients in r are taken to lie between -m/2 and m/2.
1500 * reduce_div_coefficients performs the same normalization.
1502 * As a special case, floor(NaN) = NaN.
1504 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1506 int i;
1507 int size;
1508 isl_ctx *ctx;
1509 isl_vec *div;
1511 if (!aff)
1512 return NULL;
1514 if (isl_aff_is_nan(aff))
1515 return aff;
1516 if (isl_int_is_one(aff->v->el[0]))
1517 return aff;
1519 aff = isl_aff_cow(aff);
1520 if (!aff)
1521 return NULL;
1523 aff->v = isl_vec_cow(aff->v);
1524 if (!aff->v)
1525 return isl_aff_free(aff);
1527 if (isl_aff_is_cst(aff)) {
1528 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1529 isl_int_set_si(aff->v->el[0], 1);
1530 return aff;
1533 div = isl_vec_copy(aff->v);
1534 div = isl_vec_cow(div);
1535 if (!div)
1536 return isl_aff_free(aff);
1538 ctx = isl_aff_get_ctx(aff);
1539 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1540 for (i = 1; i < aff->v->size; ++i) {
1541 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1542 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1543 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1544 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1545 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1549 aff->ls = isl_local_space_add_div(aff->ls, div);
1550 if (!aff->ls)
1551 return isl_aff_free(aff);
1553 size = aff->v->size;
1554 aff->v = isl_vec_extend(aff->v, size + 1);
1555 if (!aff->v)
1556 return isl_aff_free(aff);
1557 isl_int_set_si(aff->v->el[0], 1);
1558 isl_int_set_si(aff->v->el[size], 1);
1560 aff = isl_aff_normalize(aff);
1562 return aff;
1565 /* Compute
1567 * aff mod m = aff - m * floor(aff/m)
1569 * with m an integer value.
1571 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1572 __isl_take isl_val *m)
1574 isl_aff *res;
1576 if (!aff || !m)
1577 goto error;
1579 if (!isl_val_is_int(m))
1580 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1581 "expecting integer modulo", goto error);
1583 res = isl_aff_copy(aff);
1584 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1585 aff = isl_aff_floor(aff);
1586 aff = isl_aff_scale_val(aff, m);
1587 res = isl_aff_sub(res, aff);
1589 return res;
1590 error:
1591 isl_aff_free(aff);
1592 isl_val_free(m);
1593 return NULL;
1596 /* Compute
1598 * pwaff mod m = pwaff - m * floor(pwaff/m)
1600 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1602 isl_pw_aff *res;
1604 res = isl_pw_aff_copy(pwaff);
1605 pwaff = isl_pw_aff_scale_down(pwaff, m);
1606 pwaff = isl_pw_aff_floor(pwaff);
1607 pwaff = isl_pw_aff_scale(pwaff, m);
1608 res = isl_pw_aff_sub(res, pwaff);
1610 return res;
1613 /* Compute
1615 * pa mod m = pa - m * floor(pa/m)
1617 * with m an integer value.
1619 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1620 __isl_take isl_val *m)
1622 if (!pa || !m)
1623 goto error;
1624 if (!isl_val_is_int(m))
1625 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1626 "expecting integer modulo", goto error);
1627 pa = isl_pw_aff_mod(pa, m->n);
1628 isl_val_free(m);
1629 return pa;
1630 error:
1631 isl_pw_aff_free(pa);
1632 isl_val_free(m);
1633 return NULL;
1636 /* Given f, return ceil(f).
1637 * If f is an integer expression, then just return f.
1638 * Otherwise, let f be the expression
1640 * e/m
1642 * then return
1644 * floor((e + m - 1)/m)
1646 * As a special case, ceil(NaN) = NaN.
1648 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1650 if (!aff)
1651 return NULL;
1653 if (isl_aff_is_nan(aff))
1654 return aff;
1655 if (isl_int_is_one(aff->v->el[0]))
1656 return aff;
1658 aff = isl_aff_cow(aff);
1659 if (!aff)
1660 return NULL;
1661 aff->v = isl_vec_cow(aff->v);
1662 if (!aff->v)
1663 return isl_aff_free(aff);
1665 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1666 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1667 aff = isl_aff_floor(aff);
1669 return aff;
1672 /* Apply the expansion computed by isl_merge_divs.
1673 * The expansion itself is given by "exp" while the resulting
1674 * list of divs is given by "div".
1676 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1677 __isl_take isl_mat *div, int *exp)
1679 int old_n_div;
1680 int new_n_div;
1681 int offset;
1683 aff = isl_aff_cow(aff);
1684 if (!aff || !div)
1685 goto error;
1687 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1688 new_n_div = isl_mat_rows(div);
1689 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1691 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1692 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1693 if (!aff->v || !aff->ls)
1694 return isl_aff_free(aff);
1695 return aff;
1696 error:
1697 isl_aff_free(aff);
1698 isl_mat_free(div);
1699 return NULL;
1702 /* Add two affine expressions that live in the same local space.
1704 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1705 __isl_take isl_aff *aff2)
1707 isl_int gcd, f;
1709 aff1 = isl_aff_cow(aff1);
1710 if (!aff1 || !aff2)
1711 goto error;
1713 aff1->v = isl_vec_cow(aff1->v);
1714 if (!aff1->v)
1715 goto error;
1717 isl_int_init(gcd);
1718 isl_int_init(f);
1719 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1720 isl_int_divexact(f, aff2->v->el[0], gcd);
1721 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1722 isl_int_divexact(f, aff1->v->el[0], gcd);
1723 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1724 isl_int_divexact(f, aff2->v->el[0], gcd);
1725 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1726 isl_int_clear(f);
1727 isl_int_clear(gcd);
1729 isl_aff_free(aff2);
1730 return aff1;
1731 error:
1732 isl_aff_free(aff1);
1733 isl_aff_free(aff2);
1734 return NULL;
1737 /* Return the sum of "aff1" and "aff2".
1739 * If either of the two is NaN, then the result is NaN.
1741 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1742 __isl_take isl_aff *aff2)
1744 isl_ctx *ctx;
1745 int *exp1 = NULL;
1746 int *exp2 = NULL;
1747 isl_mat *div;
1748 int n_div1, n_div2;
1750 if (!aff1 || !aff2)
1751 goto error;
1753 ctx = isl_aff_get_ctx(aff1);
1754 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1755 isl_die(ctx, isl_error_invalid,
1756 "spaces don't match", goto error);
1758 if (isl_aff_is_nan(aff1)) {
1759 isl_aff_free(aff2);
1760 return aff1;
1762 if (isl_aff_is_nan(aff2)) {
1763 isl_aff_free(aff1);
1764 return aff2;
1767 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1768 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1769 if (n_div1 == 0 && n_div2 == 0)
1770 return add_expanded(aff1, aff2);
1772 exp1 = isl_alloc_array(ctx, int, n_div1);
1773 exp2 = isl_alloc_array(ctx, int, n_div2);
1774 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1775 goto error;
1777 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1778 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1779 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1780 free(exp1);
1781 free(exp2);
1783 return add_expanded(aff1, aff2);
1784 error:
1785 free(exp1);
1786 free(exp2);
1787 isl_aff_free(aff1);
1788 isl_aff_free(aff2);
1789 return NULL;
1792 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1793 __isl_take isl_aff *aff2)
1795 return isl_aff_add(aff1, isl_aff_neg(aff2));
1798 /* Return the result of scaling "aff" by a factor of "f".
1800 * As a special case, f * NaN = NaN.
1802 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1804 isl_int gcd;
1806 if (!aff)
1807 return NULL;
1808 if (isl_aff_is_nan(aff))
1809 return aff;
1811 if (isl_int_is_one(f))
1812 return aff;
1814 aff = isl_aff_cow(aff);
1815 if (!aff)
1816 return NULL;
1817 aff->v = isl_vec_cow(aff->v);
1818 if (!aff->v)
1819 return isl_aff_free(aff);
1821 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1822 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1823 return aff;
1826 isl_int_init(gcd);
1827 isl_int_gcd(gcd, aff->v->el[0], f);
1828 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1829 isl_int_divexact(gcd, f, gcd);
1830 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1831 isl_int_clear(gcd);
1833 return aff;
1836 /* Multiple "aff" by "v".
1838 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1839 __isl_take isl_val *v)
1841 if (!aff || !v)
1842 goto error;
1844 if (isl_val_is_one(v)) {
1845 isl_val_free(v);
1846 return aff;
1849 if (!isl_val_is_rat(v))
1850 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1851 "expecting rational factor", goto error);
1853 aff = isl_aff_scale(aff, v->n);
1854 aff = isl_aff_scale_down(aff, v->d);
1856 isl_val_free(v);
1857 return aff;
1858 error:
1859 isl_aff_free(aff);
1860 isl_val_free(v);
1861 return NULL;
1864 /* Return the result of scaling "aff" down by a factor of "f".
1866 * As a special case, NaN/f = NaN.
1868 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1870 isl_int gcd;
1872 if (!aff)
1873 return NULL;
1874 if (isl_aff_is_nan(aff))
1875 return aff;
1877 if (isl_int_is_one(f))
1878 return aff;
1880 aff = isl_aff_cow(aff);
1881 if (!aff)
1882 return NULL;
1884 if (isl_int_is_zero(f))
1885 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1886 "cannot scale down by zero", return isl_aff_free(aff));
1888 aff->v = isl_vec_cow(aff->v);
1889 if (!aff->v)
1890 return isl_aff_free(aff);
1892 isl_int_init(gcd);
1893 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1894 isl_int_gcd(gcd, gcd, f);
1895 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1896 isl_int_divexact(gcd, f, gcd);
1897 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1898 isl_int_clear(gcd);
1900 return aff;
1903 /* Divide "aff" by "v".
1905 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1906 __isl_take isl_val *v)
1908 if (!aff || !v)
1909 goto error;
1911 if (isl_val_is_one(v)) {
1912 isl_val_free(v);
1913 return aff;
1916 if (!isl_val_is_rat(v))
1917 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1918 "expecting rational factor", goto error);
1919 if (!isl_val_is_pos(v))
1920 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1921 "factor needs to be positive", goto error);
1923 aff = isl_aff_scale(aff, v->d);
1924 aff = isl_aff_scale_down(aff, v->n);
1926 isl_val_free(v);
1927 return aff;
1928 error:
1929 isl_aff_free(aff);
1930 isl_val_free(v);
1931 return NULL;
1934 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1936 isl_int v;
1938 if (f == 1)
1939 return aff;
1941 isl_int_init(v);
1942 isl_int_set_ui(v, f);
1943 aff = isl_aff_scale_down(aff, v);
1944 isl_int_clear(v);
1946 return aff;
1949 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1950 enum isl_dim_type type, unsigned pos, const char *s)
1952 aff = isl_aff_cow(aff);
1953 if (!aff)
1954 return NULL;
1955 if (type == isl_dim_out)
1956 isl_die(aff->v->ctx, isl_error_invalid,
1957 "cannot set name of output/set dimension",
1958 return isl_aff_free(aff));
1959 if (type == isl_dim_in)
1960 type = isl_dim_set;
1961 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1962 if (!aff->ls)
1963 return isl_aff_free(aff);
1965 return aff;
1968 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1969 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1971 aff = isl_aff_cow(aff);
1972 if (!aff)
1973 goto error;
1974 if (type == isl_dim_out)
1975 isl_die(aff->v->ctx, isl_error_invalid,
1976 "cannot set name of output/set dimension",
1977 goto error);
1978 if (type == isl_dim_in)
1979 type = isl_dim_set;
1980 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1981 if (!aff->ls)
1982 return isl_aff_free(aff);
1984 return aff;
1985 error:
1986 isl_id_free(id);
1987 isl_aff_free(aff);
1988 return NULL;
1991 /* Replace the identifier of the input tuple of "aff" by "id".
1992 * type is currently required to be equal to isl_dim_in
1994 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1995 enum isl_dim_type type, __isl_take isl_id *id)
1997 aff = isl_aff_cow(aff);
1998 if (!aff)
1999 goto error;
2000 if (type != isl_dim_out)
2001 isl_die(aff->v->ctx, isl_error_invalid,
2002 "cannot only set id of input tuple", goto error);
2003 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2004 if (!aff->ls)
2005 return isl_aff_free(aff);
2007 return aff;
2008 error:
2009 isl_id_free(id);
2010 isl_aff_free(aff);
2011 return NULL;
2014 /* Exploit the equalities in "eq" to simplify the affine expression
2015 * and the expressions of the integer divisions in the local space.
2016 * The integer divisions in this local space are assumed to appear
2017 * as regular dimensions in "eq".
2019 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2020 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2022 int i, j;
2023 unsigned total;
2024 unsigned n_div;
2026 if (!eq)
2027 goto error;
2028 if (eq->n_eq == 0) {
2029 isl_basic_set_free(eq);
2030 return aff;
2033 aff = isl_aff_cow(aff);
2034 if (!aff)
2035 goto error;
2037 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2038 isl_basic_set_copy(eq));
2039 aff->v = isl_vec_cow(aff->v);
2040 if (!aff->ls || !aff->v)
2041 goto error;
2043 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2044 n_div = eq->n_div;
2045 for (i = 0; i < eq->n_eq; ++i) {
2046 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2047 if (j < 0 || j == 0 || j >= total)
2048 continue;
2050 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2051 &aff->v->el[0]);
2054 isl_basic_set_free(eq);
2055 aff = isl_aff_normalize(aff);
2056 return aff;
2057 error:
2058 isl_basic_set_free(eq);
2059 isl_aff_free(aff);
2060 return NULL;
2063 /* Exploit the equalities in "eq" to simplify the affine expression
2064 * and the expressions of the integer divisions in the local space.
2066 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2067 __isl_take isl_basic_set *eq)
2069 int n_div;
2071 if (!aff || !eq)
2072 goto error;
2073 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2074 if (n_div > 0)
2075 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2076 return isl_aff_substitute_equalities_lifted(aff, eq);
2077 error:
2078 isl_basic_set_free(eq);
2079 isl_aff_free(aff);
2080 return NULL;
2083 /* Look for equalities among the variables shared by context and aff
2084 * and the integer divisions of aff, if any.
2085 * The equalities are then used to eliminate coefficients and/or integer
2086 * divisions from aff.
2088 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2089 __isl_take isl_set *context)
2091 isl_basic_set *hull;
2092 int n_div;
2094 if (!aff)
2095 goto error;
2096 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2097 if (n_div > 0) {
2098 isl_basic_set *bset;
2099 isl_local_space *ls;
2100 context = isl_set_add_dims(context, isl_dim_set, n_div);
2101 ls = isl_aff_get_domain_local_space(aff);
2102 bset = isl_basic_set_from_local_space(ls);
2103 bset = isl_basic_set_lift(bset);
2104 bset = isl_basic_set_flatten(bset);
2105 context = isl_set_intersect(context,
2106 isl_set_from_basic_set(bset));
2109 hull = isl_set_affine_hull(context);
2110 return isl_aff_substitute_equalities_lifted(aff, hull);
2111 error:
2112 isl_aff_free(aff);
2113 isl_set_free(context);
2114 return NULL;
2117 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2118 __isl_take isl_set *context)
2120 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2121 dom_context = isl_set_intersect_params(dom_context, context);
2122 return isl_aff_gist(aff, dom_context);
2125 /* Return a basic set containing those elements in the space
2126 * of aff where it is positive. "rational" should not be set.
2128 * If "aff" is NaN, then it is not positive.
2130 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2131 int rational)
2133 isl_constraint *ineq;
2134 isl_basic_set *bset;
2135 isl_val *c;
2137 if (!aff)
2138 return NULL;
2139 if (isl_aff_is_nan(aff)) {
2140 isl_space *space = isl_aff_get_domain_space(aff);
2141 isl_aff_free(aff);
2142 return isl_basic_set_empty(space);
2144 if (rational)
2145 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2146 "rational sets not supported", goto error);
2148 ineq = isl_inequality_from_aff(aff);
2149 c = isl_constraint_get_constant_val(ineq);
2150 c = isl_val_sub_ui(c, 1);
2151 ineq = isl_constraint_set_constant_val(ineq, c);
2153 bset = isl_basic_set_from_constraint(ineq);
2154 bset = isl_basic_set_simplify(bset);
2155 return bset;
2156 error:
2157 isl_aff_free(aff);
2158 return NULL;
2161 /* Return a basic set containing those elements in the space
2162 * of aff where it is non-negative.
2163 * If "rational" is set, then return a rational basic set.
2165 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2167 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2168 __isl_take isl_aff *aff, int rational)
2170 isl_constraint *ineq;
2171 isl_basic_set *bset;
2173 if (!aff)
2174 return NULL;
2175 if (isl_aff_is_nan(aff)) {
2176 isl_space *space = isl_aff_get_domain_space(aff);
2177 isl_aff_free(aff);
2178 return isl_basic_set_empty(space);
2181 ineq = isl_inequality_from_aff(aff);
2183 bset = isl_basic_set_from_constraint(ineq);
2184 if (rational)
2185 bset = isl_basic_set_set_rational(bset);
2186 bset = isl_basic_set_simplify(bset);
2187 return bset;
2190 /* Return a basic set containing those elements in the space
2191 * of aff where it is non-negative.
2193 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2195 return aff_nonneg_basic_set(aff, 0);
2198 /* Return a basic set containing those elements in the domain space
2199 * of "aff" where it is positive.
2201 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2203 aff = isl_aff_add_constant_num_si(aff, -1);
2204 return isl_aff_nonneg_basic_set(aff);
2207 /* Return a basic set containing those elements in the domain space
2208 * of aff where it is negative.
2210 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2212 aff = isl_aff_neg(aff);
2213 return isl_aff_pos_basic_set(aff);
2216 /* Return a basic set containing those elements in the space
2217 * of aff where it is zero.
2218 * If "rational" is set, then return a rational basic set.
2220 * If "aff" is NaN, then it is not zero.
2222 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2223 int rational)
2225 isl_constraint *ineq;
2226 isl_basic_set *bset;
2228 if (!aff)
2229 return NULL;
2230 if (isl_aff_is_nan(aff)) {
2231 isl_space *space = isl_aff_get_domain_space(aff);
2232 isl_aff_free(aff);
2233 return isl_basic_set_empty(space);
2236 ineq = isl_equality_from_aff(aff);
2238 bset = isl_basic_set_from_constraint(ineq);
2239 if (rational)
2240 bset = isl_basic_set_set_rational(bset);
2241 bset = isl_basic_set_simplify(bset);
2242 return bset;
2245 /* Return a basic set containing those elements in the space
2246 * of aff where it is zero.
2248 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2250 return aff_zero_basic_set(aff, 0);
2253 /* Return a basic set containing those elements in the shared space
2254 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2256 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2257 __isl_take isl_aff *aff2)
2259 aff1 = isl_aff_sub(aff1, aff2);
2261 return isl_aff_nonneg_basic_set(aff1);
2264 /* Return a basic set containing those elements in the shared domain space
2265 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2267 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2268 __isl_take isl_aff *aff2)
2270 aff1 = isl_aff_sub(aff1, aff2);
2272 return isl_aff_pos_basic_set(aff1);
2275 /* Return a set containing those elements in the shared space
2276 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2278 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2279 __isl_take isl_aff *aff2)
2281 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2284 /* Return a set containing those elements in the shared domain space
2285 * of aff1 and aff2 where aff1 is greater than aff2.
2287 * If either of the two inputs is NaN, then the result is empty,
2288 * as comparisons with NaN always return false.
2290 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2291 __isl_take isl_aff *aff2)
2293 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2296 /* Return a basic set containing those elements in the shared space
2297 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2299 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2300 __isl_take isl_aff *aff2)
2302 return isl_aff_ge_basic_set(aff2, aff1);
2305 /* Return a basic set containing those elements in the shared domain space
2306 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2308 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2309 __isl_take isl_aff *aff2)
2311 return isl_aff_gt_basic_set(aff2, aff1);
2314 /* Return a set containing those elements in the shared space
2315 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2317 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2318 __isl_take isl_aff *aff2)
2320 return isl_aff_ge_set(aff2, aff1);
2323 /* Return a set containing those elements in the shared domain space
2324 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2326 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2327 __isl_take isl_aff *aff2)
2329 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2332 /* Return a basic set containing those elements in the shared space
2333 * of aff1 and aff2 where aff1 and aff2 are equal.
2335 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2336 __isl_take isl_aff *aff2)
2338 aff1 = isl_aff_sub(aff1, aff2);
2340 return isl_aff_zero_basic_set(aff1);
2343 /* Return a set containing those elements in the shared space
2344 * of aff1 and aff2 where aff1 and aff2 are equal.
2346 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2347 __isl_take isl_aff *aff2)
2349 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2352 /* Return a set containing those elements in the shared domain space
2353 * of aff1 and aff2 where aff1 and aff2 are not equal.
2355 * If either of the two inputs is NaN, then the result is empty,
2356 * as comparisons with NaN always return false.
2358 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2359 __isl_take isl_aff *aff2)
2361 isl_set *set_lt, *set_gt;
2363 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2364 isl_aff_copy(aff2));
2365 set_gt = isl_aff_gt_set(aff1, aff2);
2366 return isl_set_union_disjoint(set_lt, set_gt);
2369 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2370 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2372 aff1 = isl_aff_add(aff1, aff2);
2373 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2374 return aff1;
2377 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2379 if (!aff)
2380 return -1;
2382 return 0;
2385 /* Check whether the given affine expression has non-zero coefficient
2386 * for any dimension in the given range or if any of these dimensions
2387 * appear with non-zero coefficients in any of the integer divisions
2388 * involved in the affine expression.
2390 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2391 enum isl_dim_type type, unsigned first, unsigned n)
2393 int i;
2394 isl_ctx *ctx;
2395 int *active = NULL;
2396 isl_bool involves = isl_bool_false;
2398 if (!aff)
2399 return isl_bool_error;
2400 if (n == 0)
2401 return isl_bool_false;
2403 ctx = isl_aff_get_ctx(aff);
2404 if (first + n > isl_aff_dim(aff, type))
2405 isl_die(ctx, isl_error_invalid,
2406 "range out of bounds", return isl_bool_error);
2408 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2409 if (!active)
2410 goto error;
2412 first += isl_local_space_offset(aff->ls, type) - 1;
2413 for (i = 0; i < n; ++i)
2414 if (active[first + i]) {
2415 involves = isl_bool_true;
2416 break;
2419 free(active);
2421 return involves;
2422 error:
2423 free(active);
2424 return isl_bool_error;
2427 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2428 enum isl_dim_type type, unsigned first, unsigned n)
2430 isl_ctx *ctx;
2432 if (!aff)
2433 return NULL;
2434 if (type == isl_dim_out)
2435 isl_die(aff->v->ctx, isl_error_invalid,
2436 "cannot drop output/set dimension",
2437 return isl_aff_free(aff));
2438 if (type == isl_dim_in)
2439 type = isl_dim_set;
2440 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2441 return aff;
2443 ctx = isl_aff_get_ctx(aff);
2444 if (first + n > isl_local_space_dim(aff->ls, type))
2445 isl_die(ctx, isl_error_invalid, "range out of bounds",
2446 return isl_aff_free(aff));
2448 aff = isl_aff_cow(aff);
2449 if (!aff)
2450 return NULL;
2452 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2453 if (!aff->ls)
2454 return isl_aff_free(aff);
2456 first += 1 + isl_local_space_offset(aff->ls, type);
2457 aff->v = isl_vec_drop_els(aff->v, first, n);
2458 if (!aff->v)
2459 return isl_aff_free(aff);
2461 return aff;
2464 /* Project the domain of the affine expression onto its parameter space.
2465 * The affine expression may not involve any of the domain dimensions.
2467 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2469 isl_space *space;
2470 unsigned n;
2471 int involves;
2473 n = isl_aff_dim(aff, isl_dim_in);
2474 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2475 if (involves < 0)
2476 return isl_aff_free(aff);
2477 if (involves)
2478 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2479 "affine expression involves some of the domain dimensions",
2480 return isl_aff_free(aff));
2481 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2482 space = isl_aff_get_domain_space(aff);
2483 space = isl_space_params(space);
2484 aff = isl_aff_reset_domain_space(aff, space);
2485 return aff;
2488 /* Convert an affine expression defined over a parameter domain
2489 * into one that is defined over a zero-dimensional set.
2491 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2493 isl_local_space *ls;
2495 ls = isl_aff_take_domain_local_space(aff);
2496 ls = isl_local_space_set_from_params(ls);
2497 aff = isl_aff_restore_domain_local_space(aff, ls);
2499 return aff;
2502 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2503 enum isl_dim_type type, unsigned first, unsigned n)
2505 isl_ctx *ctx;
2507 if (!aff)
2508 return NULL;
2509 if (type == isl_dim_out)
2510 isl_die(aff->v->ctx, isl_error_invalid,
2511 "cannot insert output/set dimensions",
2512 return isl_aff_free(aff));
2513 if (type == isl_dim_in)
2514 type = isl_dim_set;
2515 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2516 return aff;
2518 ctx = isl_aff_get_ctx(aff);
2519 if (first > isl_local_space_dim(aff->ls, type))
2520 isl_die(ctx, isl_error_invalid, "position out of bounds",
2521 return isl_aff_free(aff));
2523 aff = isl_aff_cow(aff);
2524 if (!aff)
2525 return NULL;
2527 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2528 if (!aff->ls)
2529 return isl_aff_free(aff);
2531 first += 1 + isl_local_space_offset(aff->ls, type);
2532 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2533 if (!aff->v)
2534 return isl_aff_free(aff);
2536 return aff;
2539 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2540 enum isl_dim_type type, unsigned n)
2542 unsigned pos;
2544 pos = isl_aff_dim(aff, type);
2546 return isl_aff_insert_dims(aff, type, pos, n);
2549 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2550 enum isl_dim_type type, unsigned n)
2552 unsigned pos;
2554 pos = isl_pw_aff_dim(pwaff, type);
2556 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2559 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2560 * to dimensions of "dst_type" at "dst_pos".
2562 * We only support moving input dimensions to parameters and vice versa.
2564 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2565 enum isl_dim_type dst_type, unsigned dst_pos,
2566 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2568 unsigned g_dst_pos;
2569 unsigned g_src_pos;
2571 if (!aff)
2572 return NULL;
2573 if (n == 0 &&
2574 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2575 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2576 return aff;
2578 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2579 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2580 "cannot move output/set dimension",
2581 return isl_aff_free(aff));
2582 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2583 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2584 "cannot move divs", return isl_aff_free(aff));
2585 if (dst_type == isl_dim_in)
2586 dst_type = isl_dim_set;
2587 if (src_type == isl_dim_in)
2588 src_type = isl_dim_set;
2590 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2591 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2592 "range out of bounds", return isl_aff_free(aff));
2593 if (dst_type == src_type)
2594 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2595 "moving dims within the same type not supported",
2596 return isl_aff_free(aff));
2598 aff = isl_aff_cow(aff);
2599 if (!aff)
2600 return NULL;
2602 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2603 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2604 if (dst_type > src_type)
2605 g_dst_pos -= n;
2607 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2608 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2609 src_type, src_pos, n);
2610 if (!aff->v || !aff->ls)
2611 return isl_aff_free(aff);
2613 aff = sort_divs(aff);
2615 return aff;
2618 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2620 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2621 return isl_pw_aff_alloc(dom, aff);
2624 #define isl_aff_involves_nan isl_aff_is_nan
2626 #undef PW
2627 #define PW isl_pw_aff
2628 #undef EL
2629 #define EL isl_aff
2630 #undef EL_IS_ZERO
2631 #define EL_IS_ZERO is_empty
2632 #undef ZERO
2633 #define ZERO empty
2634 #undef IS_ZERO
2635 #define IS_ZERO is_empty
2636 #undef FIELD
2637 #define FIELD aff
2638 #undef DEFAULT_IS_ZERO
2639 #define DEFAULT_IS_ZERO 0
2641 #define NO_OPT
2642 #define NO_LIFT
2643 #define NO_MORPH
2645 #include <isl_pw_templ.c>
2646 #include <isl_pw_eval.c>
2647 #include <isl_pw_hash.c>
2648 #include <isl_pw_union_opt.c>
2650 #undef UNION
2651 #define UNION isl_union_pw_aff
2652 #undef PART
2653 #define PART isl_pw_aff
2654 #undef PARTS
2655 #define PARTS pw_aff
2657 #include <isl_union_single.c>
2658 #include <isl_union_neg.c>
2660 static __isl_give isl_set *align_params_pw_pw_set_and(
2661 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2662 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2663 __isl_take isl_pw_aff *pwaff2))
2665 isl_bool equal_params;
2667 if (!pwaff1 || !pwaff2)
2668 goto error;
2669 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2670 if (equal_params < 0)
2671 goto error;
2672 if (equal_params)
2673 return fn(pwaff1, pwaff2);
2674 if (!isl_space_has_named_params(pwaff1->dim) ||
2675 !isl_space_has_named_params(pwaff2->dim))
2676 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2677 "unaligned unnamed parameters", goto error);
2678 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2679 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2680 return fn(pwaff1, pwaff2);
2681 error:
2682 isl_pw_aff_free(pwaff1);
2683 isl_pw_aff_free(pwaff2);
2684 return NULL;
2687 /* Align the parameters of the to isl_pw_aff arguments and
2688 * then apply a function "fn" on them that returns an isl_map.
2690 static __isl_give isl_map *align_params_pw_pw_map_and(
2691 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2692 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2693 __isl_take isl_pw_aff *pa2))
2695 isl_bool equal_params;
2697 if (!pa1 || !pa2)
2698 goto error;
2699 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2700 if (equal_params < 0)
2701 goto error;
2702 if (equal_params)
2703 return fn(pa1, pa2);
2704 if (!isl_space_has_named_params(pa1->dim) ||
2705 !isl_space_has_named_params(pa2->dim))
2706 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2707 "unaligned unnamed parameters", goto error);
2708 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2709 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2710 return fn(pa1, pa2);
2711 error:
2712 isl_pw_aff_free(pa1);
2713 isl_pw_aff_free(pa2);
2714 return NULL;
2717 /* Compute a piecewise quasi-affine expression with a domain that
2718 * is the union of those of pwaff1 and pwaff2 and such that on each
2719 * cell, the quasi-affine expression is the maximum of those of pwaff1
2720 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2721 * cell, then the associated expression is the defined one.
2723 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2724 __isl_take isl_pw_aff *pwaff2)
2726 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2729 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2730 __isl_take isl_pw_aff *pwaff2)
2732 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2733 &pw_aff_union_max);
2736 /* Compute a piecewise quasi-affine expression with a domain that
2737 * is the union of those of pwaff1 and pwaff2 and such that on each
2738 * cell, the quasi-affine expression is the minimum of those of pwaff1
2739 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2740 * cell, then the associated expression is the defined one.
2742 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2743 __isl_take isl_pw_aff *pwaff2)
2745 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2748 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2749 __isl_take isl_pw_aff *pwaff2)
2751 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2752 &pw_aff_union_min);
2755 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2756 __isl_take isl_pw_aff *pwaff2, int max)
2758 if (max)
2759 return isl_pw_aff_union_max(pwaff1, pwaff2);
2760 else
2761 return isl_pw_aff_union_min(pwaff1, pwaff2);
2764 /* Construct a map with as domain the domain of pwaff and
2765 * one-dimensional range corresponding to the affine expressions.
2767 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2769 int i;
2770 isl_space *dim;
2771 isl_map *map;
2773 if (!pwaff)
2774 return NULL;
2776 dim = isl_pw_aff_get_space(pwaff);
2777 map = isl_map_empty(dim);
2779 for (i = 0; i < pwaff->n; ++i) {
2780 isl_basic_map *bmap;
2781 isl_map *map_i;
2783 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2784 map_i = isl_map_from_basic_map(bmap);
2785 map_i = isl_map_intersect_domain(map_i,
2786 isl_set_copy(pwaff->p[i].set));
2787 map = isl_map_union_disjoint(map, map_i);
2790 isl_pw_aff_free(pwaff);
2792 return map;
2795 /* Construct a map with as domain the domain of pwaff and
2796 * one-dimensional range corresponding to the affine expressions.
2798 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2800 if (!pwaff)
2801 return NULL;
2802 if (isl_space_is_set(pwaff->dim))
2803 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2804 "space of input is not a map", goto error);
2805 return map_from_pw_aff(pwaff);
2806 error:
2807 isl_pw_aff_free(pwaff);
2808 return NULL;
2811 /* Construct a one-dimensional set with as parameter domain
2812 * the domain of pwaff and the single set dimension
2813 * corresponding to the affine expressions.
2815 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2817 if (!pwaff)
2818 return NULL;
2819 if (!isl_space_is_set(pwaff->dim))
2820 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2821 "space of input is not a set", goto error);
2822 return map_from_pw_aff(pwaff);
2823 error:
2824 isl_pw_aff_free(pwaff);
2825 return NULL;
2828 /* Return a set containing those elements in the domain
2829 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2830 * does not satisfy "fn" (if complement is 1).
2832 * The pieces with a NaN never belong to the result since
2833 * NaN does not satisfy any property.
2835 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2836 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2837 int complement)
2839 int i;
2840 isl_set *set;
2842 if (!pwaff)
2843 return NULL;
2845 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2847 for (i = 0; i < pwaff->n; ++i) {
2848 isl_basic_set *bset;
2849 isl_set *set_i, *locus;
2850 isl_bool rational;
2852 if (isl_aff_is_nan(pwaff->p[i].aff))
2853 continue;
2855 rational = isl_set_has_rational(pwaff->p[i].set);
2856 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2857 locus = isl_set_from_basic_set(bset);
2858 set_i = isl_set_copy(pwaff->p[i].set);
2859 if (complement)
2860 set_i = isl_set_subtract(set_i, locus);
2861 else
2862 set_i = isl_set_intersect(set_i, locus);
2863 set = isl_set_union_disjoint(set, set_i);
2866 isl_pw_aff_free(pwaff);
2868 return set;
2871 /* Return a set containing those elements in the domain
2872 * of "pa" where it is positive.
2874 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2876 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2879 /* Return a set containing those elements in the domain
2880 * of pwaff where it is non-negative.
2882 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2884 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2887 /* Return a set containing those elements in the domain
2888 * of pwaff where it is zero.
2890 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2892 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2895 /* Return a set containing those elements in the domain
2896 * of pwaff where it is not zero.
2898 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2900 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2903 /* Return a set containing those elements in the shared domain
2904 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2906 * We compute the difference on the shared domain and then construct
2907 * the set of values where this difference is non-negative.
2908 * If strict is set, we first subtract 1 from the difference.
2909 * If equal is set, we only return the elements where pwaff1 and pwaff2
2910 * are equal.
2912 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2913 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2915 isl_set *set1, *set2;
2917 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2918 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2919 set1 = isl_set_intersect(set1, set2);
2920 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2921 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2922 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2924 if (strict) {
2925 isl_space *dim = isl_set_get_space(set1);
2926 isl_aff *aff;
2927 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2928 aff = isl_aff_add_constant_si(aff, -1);
2929 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2930 } else
2931 isl_set_free(set1);
2933 if (equal)
2934 return isl_pw_aff_zero_set(pwaff1);
2935 return isl_pw_aff_nonneg_set(pwaff1);
2938 /* Return a set containing those elements in the shared domain
2939 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2941 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2942 __isl_take isl_pw_aff *pwaff2)
2944 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2947 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2948 __isl_take isl_pw_aff *pwaff2)
2950 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2953 /* Return a set containing those elements in the shared domain
2954 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2956 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2957 __isl_take isl_pw_aff *pwaff2)
2959 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2962 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2963 __isl_take isl_pw_aff *pwaff2)
2965 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2968 /* Return a set containing those elements in the shared domain
2969 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2971 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2972 __isl_take isl_pw_aff *pwaff2)
2974 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2977 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2978 __isl_take isl_pw_aff *pwaff2)
2980 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2983 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2984 __isl_take isl_pw_aff *pwaff2)
2986 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2989 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2990 __isl_take isl_pw_aff *pwaff2)
2992 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2995 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2996 * where the function values are ordered in the same way as "order",
2997 * which returns a set in the shared domain of its two arguments.
2998 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3000 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3001 * We first pull back the two functions such that they are defined on
3002 * the domain [A -> B]. Then we apply "order", resulting in a set
3003 * in the space [A -> B]. Finally, we unwrap this set to obtain
3004 * a map in the space A -> B.
3006 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3007 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3008 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3009 __isl_take isl_pw_aff *pa2))
3011 isl_space *space1, *space2;
3012 isl_multi_aff *ma;
3013 isl_set *set;
3015 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3016 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3017 space1 = isl_space_map_from_domain_and_range(space1, space2);
3018 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3019 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3020 ma = isl_multi_aff_range_map(space1);
3021 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3022 set = order(pa1, pa2);
3024 return isl_set_unwrap(set);
3027 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3028 * where the function values are equal.
3029 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3031 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3032 __isl_take isl_pw_aff *pa2)
3034 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3037 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3038 * where the function values are equal.
3040 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3041 __isl_take isl_pw_aff *pa2)
3043 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3046 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3047 * where the function value of "pa1" is less than the function value of "pa2".
3048 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3050 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3051 __isl_take isl_pw_aff *pa2)
3053 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3056 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3057 * where the function value of "pa1" is less than the function value of "pa2".
3059 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3060 __isl_take isl_pw_aff *pa2)
3062 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3065 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3066 * where the function value of "pa1" is greater than the function value
3067 * of "pa2".
3068 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3070 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3071 __isl_take isl_pw_aff *pa2)
3073 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3076 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3077 * where the function value of "pa1" is greater than the function value
3078 * of "pa2".
3080 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3081 __isl_take isl_pw_aff *pa2)
3083 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3086 /* Return a set containing those elements in the shared domain
3087 * of the elements of list1 and list2 where each element in list1
3088 * has the relation specified by "fn" with each element in list2.
3090 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3091 __isl_take isl_pw_aff_list *list2,
3092 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3093 __isl_take isl_pw_aff *pwaff2))
3095 int i, j;
3096 isl_ctx *ctx;
3097 isl_set *set;
3099 if (!list1 || !list2)
3100 goto error;
3102 ctx = isl_pw_aff_list_get_ctx(list1);
3103 if (list1->n < 1 || list2->n < 1)
3104 isl_die(ctx, isl_error_invalid,
3105 "list should contain at least one element", goto error);
3107 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3108 for (i = 0; i < list1->n; ++i)
3109 for (j = 0; j < list2->n; ++j) {
3110 isl_set *set_ij;
3112 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3113 isl_pw_aff_copy(list2->p[j]));
3114 set = isl_set_intersect(set, set_ij);
3117 isl_pw_aff_list_free(list1);
3118 isl_pw_aff_list_free(list2);
3119 return set;
3120 error:
3121 isl_pw_aff_list_free(list1);
3122 isl_pw_aff_list_free(list2);
3123 return NULL;
3126 /* Return a set containing those elements in the shared domain
3127 * of the elements of list1 and list2 where each element in list1
3128 * is equal to each element in list2.
3130 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3131 __isl_take isl_pw_aff_list *list2)
3133 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3136 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3137 __isl_take isl_pw_aff_list *list2)
3139 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3142 /* Return a set containing those elements in the shared domain
3143 * of the elements of list1 and list2 where each element in list1
3144 * is less than or equal to each element in list2.
3146 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3147 __isl_take isl_pw_aff_list *list2)
3149 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3152 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3153 __isl_take isl_pw_aff_list *list2)
3155 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3158 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3159 __isl_take isl_pw_aff_list *list2)
3161 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3164 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3165 __isl_take isl_pw_aff_list *list2)
3167 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3171 /* Return a set containing those elements in the shared domain
3172 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3174 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3175 __isl_take isl_pw_aff *pwaff2)
3177 isl_set *set_lt, *set_gt;
3179 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3180 isl_pw_aff_copy(pwaff2));
3181 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3182 return isl_set_union_disjoint(set_lt, set_gt);
3185 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3186 __isl_take isl_pw_aff *pwaff2)
3188 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3191 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3192 isl_int v)
3194 int i;
3196 if (isl_int_is_one(v))
3197 return pwaff;
3198 if (!isl_int_is_pos(v))
3199 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3200 "factor needs to be positive",
3201 return isl_pw_aff_free(pwaff));
3202 pwaff = isl_pw_aff_cow(pwaff);
3203 if (!pwaff)
3204 return NULL;
3205 if (pwaff->n == 0)
3206 return pwaff;
3208 for (i = 0; i < pwaff->n; ++i) {
3209 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3210 if (!pwaff->p[i].aff)
3211 return isl_pw_aff_free(pwaff);
3214 return pwaff;
3217 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3219 int i;
3221 pwaff = isl_pw_aff_cow(pwaff);
3222 if (!pwaff)
3223 return NULL;
3224 if (pwaff->n == 0)
3225 return pwaff;
3227 for (i = 0; i < pwaff->n; ++i) {
3228 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3229 if (!pwaff->p[i].aff)
3230 return isl_pw_aff_free(pwaff);
3233 return pwaff;
3236 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3238 int i;
3240 pwaff = isl_pw_aff_cow(pwaff);
3241 if (!pwaff)
3242 return NULL;
3243 if (pwaff->n == 0)
3244 return pwaff;
3246 for (i = 0; i < pwaff->n; ++i) {
3247 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3248 if (!pwaff->p[i].aff)
3249 return isl_pw_aff_free(pwaff);
3252 return pwaff;
3255 /* Assuming that "cond1" and "cond2" are disjoint,
3256 * return an affine expression that is equal to pwaff1 on cond1
3257 * and to pwaff2 on cond2.
3259 static __isl_give isl_pw_aff *isl_pw_aff_select(
3260 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3261 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3263 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3264 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3266 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3269 /* Return an affine expression that is equal to pwaff_true for elements
3270 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3271 * is zero.
3272 * That is, return cond ? pwaff_true : pwaff_false;
3274 * If "cond" involves and NaN, then we conservatively return a NaN
3275 * on its entire domain. In principle, we could consider the pieces
3276 * where it is NaN separately from those where it is not.
3278 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3279 * then only use the domain of "cond" to restrict the domain.
3281 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3282 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3284 isl_set *cond_true, *cond_false;
3285 isl_bool equal;
3287 if (!cond)
3288 goto error;
3289 if (isl_pw_aff_involves_nan(cond)) {
3290 isl_space *space = isl_pw_aff_get_domain_space(cond);
3291 isl_local_space *ls = isl_local_space_from_space(space);
3292 isl_pw_aff_free(cond);
3293 isl_pw_aff_free(pwaff_true);
3294 isl_pw_aff_free(pwaff_false);
3295 return isl_pw_aff_nan_on_domain(ls);
3298 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3299 isl_pw_aff_get_space(pwaff_false));
3300 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3301 isl_pw_aff_get_space(pwaff_true));
3302 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3303 if (equal < 0)
3304 goto error;
3305 if (equal) {
3306 isl_set *dom;
3308 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3309 isl_pw_aff_free(pwaff_false);
3310 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3313 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3314 cond_false = isl_pw_aff_zero_set(cond);
3315 return isl_pw_aff_select(cond_true, pwaff_true,
3316 cond_false, pwaff_false);
3317 error:
3318 isl_pw_aff_free(cond);
3319 isl_pw_aff_free(pwaff_true);
3320 isl_pw_aff_free(pwaff_false);
3321 return NULL;
3324 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3326 if (!aff)
3327 return isl_bool_error;
3329 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3332 /* Check whether pwaff is a piecewise constant.
3334 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3336 int i;
3338 if (!pwaff)
3339 return isl_bool_error;
3341 for (i = 0; i < pwaff->n; ++i) {
3342 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3343 if (is_cst < 0 || !is_cst)
3344 return is_cst;
3347 return isl_bool_true;
3350 /* Are all elements of "mpa" piecewise constants?
3352 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3354 int i;
3356 if (!mpa)
3357 return isl_bool_error;
3359 for (i = 0; i < mpa->n; ++i) {
3360 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3361 if (is_cst < 0 || !is_cst)
3362 return is_cst;
3365 return isl_bool_true;
3368 /* Return the product of "aff1" and "aff2".
3370 * If either of the two is NaN, then the result is NaN.
3372 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3374 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3375 __isl_take isl_aff *aff2)
3377 if (!aff1 || !aff2)
3378 goto error;
3380 if (isl_aff_is_nan(aff1)) {
3381 isl_aff_free(aff2);
3382 return aff1;
3384 if (isl_aff_is_nan(aff2)) {
3385 isl_aff_free(aff1);
3386 return aff2;
3389 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3390 return isl_aff_mul(aff2, aff1);
3392 if (!isl_aff_is_cst(aff2))
3393 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3394 "at least one affine expression should be constant",
3395 goto error);
3397 aff1 = isl_aff_cow(aff1);
3398 if (!aff1 || !aff2)
3399 goto error;
3401 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3402 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3404 isl_aff_free(aff2);
3405 return aff1;
3406 error:
3407 isl_aff_free(aff1);
3408 isl_aff_free(aff2);
3409 return NULL;
3412 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3414 * If either of the two is NaN, then the result is NaN.
3416 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3417 __isl_take isl_aff *aff2)
3419 int is_cst;
3420 int neg;
3422 if (!aff1 || !aff2)
3423 goto error;
3425 if (isl_aff_is_nan(aff1)) {
3426 isl_aff_free(aff2);
3427 return aff1;
3429 if (isl_aff_is_nan(aff2)) {
3430 isl_aff_free(aff1);
3431 return aff2;
3434 is_cst = isl_aff_is_cst(aff2);
3435 if (is_cst < 0)
3436 goto error;
3437 if (!is_cst)
3438 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3439 "second argument should be a constant", goto error);
3441 if (!aff2)
3442 goto error;
3444 neg = isl_int_is_neg(aff2->v->el[1]);
3445 if (neg) {
3446 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3447 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3450 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3451 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3453 if (neg) {
3454 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3455 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3458 isl_aff_free(aff2);
3459 return aff1;
3460 error:
3461 isl_aff_free(aff1);
3462 isl_aff_free(aff2);
3463 return NULL;
3466 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3467 __isl_take isl_pw_aff *pwaff2)
3469 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3472 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3473 __isl_take isl_pw_aff *pwaff2)
3475 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3478 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3479 __isl_take isl_pw_aff *pwaff2)
3481 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3484 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3485 __isl_take isl_pw_aff *pwaff2)
3487 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3490 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3491 __isl_take isl_pw_aff *pwaff2)
3493 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3496 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3497 __isl_take isl_pw_aff *pa2)
3499 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3502 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3504 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3505 __isl_take isl_pw_aff *pa2)
3507 int is_cst;
3509 is_cst = isl_pw_aff_is_cst(pa2);
3510 if (is_cst < 0)
3511 goto error;
3512 if (!is_cst)
3513 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3514 "second argument should be a piecewise constant",
3515 goto error);
3516 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3517 error:
3518 isl_pw_aff_free(pa1);
3519 isl_pw_aff_free(pa2);
3520 return NULL;
3523 /* Compute the quotient of the integer division of "pa1" by "pa2"
3524 * with rounding towards zero.
3525 * "pa2" is assumed to be a piecewise constant.
3527 * In particular, return
3529 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3532 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3533 __isl_take isl_pw_aff *pa2)
3535 int is_cst;
3536 isl_set *cond;
3537 isl_pw_aff *f, *c;
3539 is_cst = isl_pw_aff_is_cst(pa2);
3540 if (is_cst < 0)
3541 goto error;
3542 if (!is_cst)
3543 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3544 "second argument should be a piecewise constant",
3545 goto error);
3547 pa1 = isl_pw_aff_div(pa1, pa2);
3549 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3550 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3551 c = isl_pw_aff_ceil(pa1);
3552 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3553 error:
3554 isl_pw_aff_free(pa1);
3555 isl_pw_aff_free(pa2);
3556 return NULL;
3559 /* Compute the remainder of the integer division of "pa1" by "pa2"
3560 * with rounding towards zero.
3561 * "pa2" is assumed to be a piecewise constant.
3563 * In particular, return
3565 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3568 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3569 __isl_take isl_pw_aff *pa2)
3571 int is_cst;
3572 isl_pw_aff *res;
3574 is_cst = isl_pw_aff_is_cst(pa2);
3575 if (is_cst < 0)
3576 goto error;
3577 if (!is_cst)
3578 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3579 "second argument should be a piecewise constant",
3580 goto error);
3581 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3582 res = isl_pw_aff_mul(pa2, res);
3583 res = isl_pw_aff_sub(pa1, res);
3584 return res;
3585 error:
3586 isl_pw_aff_free(pa1);
3587 isl_pw_aff_free(pa2);
3588 return NULL;
3591 /* Does either of "pa1" or "pa2" involve any NaN2?
3593 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3594 __isl_keep isl_pw_aff *pa2)
3596 isl_bool has_nan;
3598 has_nan = isl_pw_aff_involves_nan(pa1);
3599 if (has_nan < 0 || has_nan)
3600 return has_nan;
3601 return isl_pw_aff_involves_nan(pa2);
3604 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3605 * by a NaN on their shared domain.
3607 * In principle, the result could be refined to only being NaN
3608 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3610 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3611 __isl_take isl_pw_aff *pa2)
3613 isl_local_space *ls;
3614 isl_set *dom;
3615 isl_pw_aff *pa;
3617 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3618 ls = isl_local_space_from_space(isl_set_get_space(dom));
3619 pa = isl_pw_aff_nan_on_domain(ls);
3620 pa = isl_pw_aff_intersect_domain(pa, dom);
3622 return pa;
3625 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3626 __isl_take isl_pw_aff *pwaff2)
3628 isl_set *le;
3629 isl_set *dom;
3631 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3632 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3633 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3634 isl_pw_aff_copy(pwaff2));
3635 dom = isl_set_subtract(dom, isl_set_copy(le));
3636 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3639 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3640 __isl_take isl_pw_aff *pwaff2)
3642 isl_set *ge;
3643 isl_set *dom;
3645 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3646 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3647 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3648 isl_pw_aff_copy(pwaff2));
3649 dom = isl_set_subtract(dom, isl_set_copy(ge));
3650 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3653 /* Return an expression for the minimum (if "max" is not set) or
3654 * the maximum (if "max" is set) of "pa1" and "pa2".
3655 * If either expression involves any NaN, then return a NaN
3656 * on the shared domain as result.
3658 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3659 __isl_take isl_pw_aff *pa2, int max)
3661 isl_bool has_nan;
3663 has_nan = either_involves_nan(pa1, pa2);
3664 if (has_nan < 0)
3665 pa1 = isl_pw_aff_free(pa1);
3666 else if (has_nan)
3667 return replace_by_nan(pa1, pa2);
3669 if (max)
3670 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3671 else
3672 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3675 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3677 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3678 __isl_take isl_pw_aff *pwaff2)
3680 return pw_aff_min_max(pwaff1, pwaff2, 0);
3683 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3685 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3686 __isl_take isl_pw_aff *pwaff2)
3688 return pw_aff_min_max(pwaff1, pwaff2, 1);
3691 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3692 __isl_take isl_pw_aff_list *list,
3693 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3694 __isl_take isl_pw_aff *pwaff2))
3696 int i;
3697 isl_ctx *ctx;
3698 isl_pw_aff *res;
3700 if (!list)
3701 return NULL;
3703 ctx = isl_pw_aff_list_get_ctx(list);
3704 if (list->n < 1)
3705 isl_die(ctx, isl_error_invalid,
3706 "list should contain at least one element", goto error);
3708 res = isl_pw_aff_copy(list->p[0]);
3709 for (i = 1; i < list->n; ++i)
3710 res = fn(res, isl_pw_aff_copy(list->p[i]));
3712 isl_pw_aff_list_free(list);
3713 return res;
3714 error:
3715 isl_pw_aff_list_free(list);
3716 return NULL;
3719 /* Return an isl_pw_aff that maps each element in the intersection of the
3720 * domains of the elements of list to the minimal corresponding affine
3721 * expression.
3723 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3725 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3728 /* Return an isl_pw_aff that maps each element in the intersection of the
3729 * domains of the elements of list to the maximal corresponding affine
3730 * expression.
3732 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3734 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3737 /* Mark the domains of "pwaff" as rational.
3739 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3741 int i;
3743 pwaff = isl_pw_aff_cow(pwaff);
3744 if (!pwaff)
3745 return NULL;
3746 if (pwaff->n == 0)
3747 return pwaff;
3749 for (i = 0; i < pwaff->n; ++i) {
3750 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3751 if (!pwaff->p[i].set)
3752 return isl_pw_aff_free(pwaff);
3755 return pwaff;
3758 /* Mark the domains of the elements of "list" as rational.
3760 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3761 __isl_take isl_pw_aff_list *list)
3763 int i, n;
3765 if (!list)
3766 return NULL;
3767 if (list->n == 0)
3768 return list;
3770 n = list->n;
3771 for (i = 0; i < n; ++i) {
3772 isl_pw_aff *pa;
3774 pa = isl_pw_aff_list_get_pw_aff(list, i);
3775 pa = isl_pw_aff_set_rational(pa);
3776 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3779 return list;
3782 /* Do the parameters of "aff" match those of "space"?
3784 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3785 __isl_keep isl_space *space)
3787 isl_space *aff_space;
3788 isl_bool match;
3790 if (!aff || !space)
3791 return isl_bool_error;
3793 aff_space = isl_aff_get_domain_space(aff);
3795 match = isl_space_has_equal_params(space, aff_space);
3797 isl_space_free(aff_space);
3798 return match;
3801 /* Check that the domain space of "aff" matches "space".
3803 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3804 __isl_keep isl_space *space)
3806 isl_space *aff_space;
3807 isl_bool match;
3809 if (!aff || !space)
3810 return isl_stat_error;
3812 aff_space = isl_aff_get_domain_space(aff);
3814 match = isl_space_has_equal_params(space, aff_space);
3815 if (match < 0)
3816 goto error;
3817 if (!match)
3818 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3819 "parameters don't match", goto error);
3820 match = isl_space_tuple_is_equal(space, isl_dim_in,
3821 aff_space, isl_dim_set);
3822 if (match < 0)
3823 goto error;
3824 if (!match)
3825 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3826 "domains don't match", goto error);
3827 isl_space_free(aff_space);
3828 return isl_stat_ok;
3829 error:
3830 isl_space_free(aff_space);
3831 return isl_stat_error;
3834 #undef BASE
3835 #define BASE aff
3836 #undef DOMBASE
3837 #define DOMBASE set
3838 #define NO_DOMAIN
3840 #include <isl_multi_no_explicit_domain.c>
3841 #include <isl_multi_templ.c>
3842 #include <isl_multi_apply_set.c>
3843 #include <isl_multi_cmp.c>
3844 #include <isl_multi_dims.c>
3845 #include <isl_multi_floor.c>
3846 #include <isl_multi_gist.c>
3848 #undef NO_DOMAIN
3850 /* Construct an isl_multi_aff living in "space" that corresponds
3851 * to the affine transformation matrix "mat".
3853 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3854 __isl_take isl_space *space, __isl_take isl_mat *mat)
3856 isl_ctx *ctx;
3857 isl_local_space *ls = NULL;
3858 isl_multi_aff *ma = NULL;
3859 int n_row, n_col, n_out, total;
3860 int i;
3862 if (!space || !mat)
3863 goto error;
3865 ctx = isl_mat_get_ctx(mat);
3867 n_row = isl_mat_rows(mat);
3868 n_col = isl_mat_cols(mat);
3869 if (n_row < 1)
3870 isl_die(ctx, isl_error_invalid,
3871 "insufficient number of rows", goto error);
3872 if (n_col < 1)
3873 isl_die(ctx, isl_error_invalid,
3874 "insufficient number of columns", goto error);
3875 n_out = isl_space_dim(space, isl_dim_out);
3876 total = isl_space_dim(space, isl_dim_all);
3877 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3878 isl_die(ctx, isl_error_invalid,
3879 "dimension mismatch", goto error);
3881 ma = isl_multi_aff_zero(isl_space_copy(space));
3882 ls = isl_local_space_from_space(isl_space_domain(space));
3884 for (i = 0; i < n_row - 1; ++i) {
3885 isl_vec *v;
3886 isl_aff *aff;
3888 v = isl_vec_alloc(ctx, 1 + n_col);
3889 if (!v)
3890 goto error;
3891 isl_int_set(v->el[0], mat->row[0][0]);
3892 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3893 v = isl_vec_normalize(v);
3894 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3895 ma = isl_multi_aff_set_aff(ma, i, aff);
3898 isl_local_space_free(ls);
3899 isl_mat_free(mat);
3900 return ma;
3901 error:
3902 isl_local_space_free(ls);
3903 isl_mat_free(mat);
3904 isl_multi_aff_free(ma);
3905 return NULL;
3908 /* Remove any internal structure of the domain of "ma".
3909 * If there is any such internal structure in the input,
3910 * then the name of the corresponding space is also removed.
3912 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3913 __isl_take isl_multi_aff *ma)
3915 isl_space *space;
3917 if (!ma)
3918 return NULL;
3920 if (!ma->space->nested[0])
3921 return ma;
3923 space = isl_multi_aff_get_space(ma);
3924 space = isl_space_flatten_domain(space);
3925 ma = isl_multi_aff_reset_space(ma, space);
3927 return ma;
3930 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3931 * of the space to its domain.
3933 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3935 int i, n_in;
3936 isl_local_space *ls;
3937 isl_multi_aff *ma;
3939 if (!space)
3940 return NULL;
3941 if (!isl_space_is_map(space))
3942 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3943 "not a map space", goto error);
3945 n_in = isl_space_dim(space, isl_dim_in);
3946 space = isl_space_domain_map(space);
3948 ma = isl_multi_aff_alloc(isl_space_copy(space));
3949 if (n_in == 0) {
3950 isl_space_free(space);
3951 return ma;
3954 space = isl_space_domain(space);
3955 ls = isl_local_space_from_space(space);
3956 for (i = 0; i < n_in; ++i) {
3957 isl_aff *aff;
3959 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3960 isl_dim_set, i);
3961 ma = isl_multi_aff_set_aff(ma, i, aff);
3963 isl_local_space_free(ls);
3964 return ma;
3965 error:
3966 isl_space_free(space);
3967 return NULL;
3970 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3971 * of the space to its range.
3973 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3975 int i, n_in, n_out;
3976 isl_local_space *ls;
3977 isl_multi_aff *ma;
3979 if (!space)
3980 return NULL;
3981 if (!isl_space_is_map(space))
3982 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3983 "not a map space", goto error);
3985 n_in = isl_space_dim(space, isl_dim_in);
3986 n_out = isl_space_dim(space, isl_dim_out);
3987 space = isl_space_range_map(space);
3989 ma = isl_multi_aff_alloc(isl_space_copy(space));
3990 if (n_out == 0) {
3991 isl_space_free(space);
3992 return ma;
3995 space = isl_space_domain(space);
3996 ls = isl_local_space_from_space(space);
3997 for (i = 0; i < n_out; ++i) {
3998 isl_aff *aff;
4000 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4001 isl_dim_set, n_in + i);
4002 ma = isl_multi_aff_set_aff(ma, i, aff);
4004 isl_local_space_free(ls);
4005 return ma;
4006 error:
4007 isl_space_free(space);
4008 return NULL;
4011 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4012 * of the space to its range.
4014 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4015 __isl_take isl_space *space)
4017 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4020 /* Given the space of a set and a range of set dimensions,
4021 * construct an isl_multi_aff that projects out those dimensions.
4023 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4024 __isl_take isl_space *space, enum isl_dim_type type,
4025 unsigned first, unsigned n)
4027 int i, dim;
4028 isl_local_space *ls;
4029 isl_multi_aff *ma;
4031 if (!space)
4032 return NULL;
4033 if (!isl_space_is_set(space))
4034 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4035 "expecting set space", goto error);
4036 if (type != isl_dim_set)
4037 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4038 "only set dimensions can be projected out", goto error);
4040 dim = isl_space_dim(space, isl_dim_set);
4041 if (first + n > dim)
4042 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4043 "range out of bounds", goto error);
4045 space = isl_space_from_domain(space);
4046 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4048 if (dim == n)
4049 return isl_multi_aff_alloc(space);
4051 ma = isl_multi_aff_alloc(isl_space_copy(space));
4052 space = isl_space_domain(space);
4053 ls = isl_local_space_from_space(space);
4055 for (i = 0; i < first; ++i) {
4056 isl_aff *aff;
4058 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4059 isl_dim_set, i);
4060 ma = isl_multi_aff_set_aff(ma, i, aff);
4063 for (i = 0; i < dim - (first + n); ++i) {
4064 isl_aff *aff;
4066 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4067 isl_dim_set, first + n + i);
4068 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4071 isl_local_space_free(ls);
4072 return ma;
4073 error:
4074 isl_space_free(space);
4075 return NULL;
4078 /* Given the space of a set and a range of set dimensions,
4079 * construct an isl_pw_multi_aff that projects out those dimensions.
4081 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4082 __isl_take isl_space *space, enum isl_dim_type type,
4083 unsigned first, unsigned n)
4085 isl_multi_aff *ma;
4087 ma = isl_multi_aff_project_out_map(space, type, first, n);
4088 return isl_pw_multi_aff_from_multi_aff(ma);
4091 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4092 * domain.
4094 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4095 __isl_take isl_multi_aff *ma)
4097 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4098 return isl_pw_multi_aff_alloc(dom, ma);
4101 /* Create a piecewise multi-affine expression in the given space that maps each
4102 * input dimension to the corresponding output dimension.
4104 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4105 __isl_take isl_space *space)
4107 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4110 /* Exploit the equalities in "eq" to simplify the affine expressions.
4112 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4113 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4115 int i;
4117 maff = isl_multi_aff_cow(maff);
4118 if (!maff || !eq)
4119 goto error;
4121 for (i = 0; i < maff->n; ++i) {
4122 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4123 isl_basic_set_copy(eq));
4124 if (!maff->u.p[i])
4125 goto error;
4128 isl_basic_set_free(eq);
4129 return maff;
4130 error:
4131 isl_basic_set_free(eq);
4132 isl_multi_aff_free(maff);
4133 return NULL;
4136 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4137 isl_int f)
4139 int i;
4141 maff = isl_multi_aff_cow(maff);
4142 if (!maff)
4143 return NULL;
4145 for (i = 0; i < maff->n; ++i) {
4146 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4147 if (!maff->u.p[i])
4148 return isl_multi_aff_free(maff);
4151 return maff;
4154 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4155 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4157 maff1 = isl_multi_aff_add(maff1, maff2);
4158 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4159 return maff1;
4162 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4164 if (!maff)
4165 return -1;
4167 return 0;
4170 /* Return the set of domain elements where "ma1" is lexicographically
4171 * smaller than or equal to "ma2".
4173 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4174 __isl_take isl_multi_aff *ma2)
4176 return isl_multi_aff_lex_ge_set(ma2, ma1);
4179 /* Return the set of domain elements where "ma1" is lexicographically
4180 * smaller than "ma2".
4182 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4183 __isl_take isl_multi_aff *ma2)
4185 return isl_multi_aff_lex_gt_set(ma2, ma1);
4188 /* Return the set of domain elements where "ma1" and "ma2"
4189 * satisfy "order".
4191 static __isl_give isl_set *isl_multi_aff_order_set(
4192 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4193 __isl_give isl_map *order(__isl_take isl_space *set_space))
4195 isl_space *space;
4196 isl_map *map1, *map2;
4197 isl_map *map, *ge;
4199 map1 = isl_map_from_multi_aff(ma1);
4200 map2 = isl_map_from_multi_aff(ma2);
4201 map = isl_map_range_product(map1, map2);
4202 space = isl_space_range(isl_map_get_space(map));
4203 space = isl_space_domain(isl_space_unwrap(space));
4204 ge = order(space);
4205 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4207 return isl_map_domain(map);
4210 /* Return the set of domain elements where "ma1" is lexicographically
4211 * greater than or equal to "ma2".
4213 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4214 __isl_take isl_multi_aff *ma2)
4216 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4219 /* Return the set of domain elements where "ma1" is lexicographically
4220 * greater than "ma2".
4222 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4223 __isl_take isl_multi_aff *ma2)
4225 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4228 #undef PW
4229 #define PW isl_pw_multi_aff
4230 #undef EL
4231 #define EL isl_multi_aff
4232 #undef EL_IS_ZERO
4233 #define EL_IS_ZERO is_empty
4234 #undef ZERO
4235 #define ZERO empty
4236 #undef IS_ZERO
4237 #define IS_ZERO is_empty
4238 #undef FIELD
4239 #define FIELD maff
4240 #undef DEFAULT_IS_ZERO
4241 #define DEFAULT_IS_ZERO 0
4243 #define NO_SUB
4244 #define NO_OPT
4245 #define NO_INVOLVES_DIMS
4246 #define NO_INSERT_DIMS
4247 #define NO_LIFT
4248 #define NO_MORPH
4250 #include <isl_pw_templ.c>
4251 #include <isl_pw_union_opt.c>
4253 #undef NO_SUB
4255 #undef UNION
4256 #define UNION isl_union_pw_multi_aff
4257 #undef PART
4258 #define PART isl_pw_multi_aff
4259 #undef PARTS
4260 #define PARTS pw_multi_aff
4262 #include <isl_union_multi.c>
4263 #include <isl_union_neg.c>
4265 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4266 __isl_take isl_pw_multi_aff *pma1,
4267 __isl_take isl_pw_multi_aff *pma2)
4269 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4270 &isl_multi_aff_lex_ge_set);
4273 /* Given two piecewise multi affine expressions, return a piecewise
4274 * multi-affine expression defined on the union of the definition domains
4275 * of the inputs that is equal to the lexicographic maximum of the two
4276 * inputs on each cell. If only one of the two inputs is defined on
4277 * a given cell, then it is considered to be the maximum.
4279 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4280 __isl_take isl_pw_multi_aff *pma1,
4281 __isl_take isl_pw_multi_aff *pma2)
4283 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4284 &pw_multi_aff_union_lexmax);
4287 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4288 __isl_take isl_pw_multi_aff *pma1,
4289 __isl_take isl_pw_multi_aff *pma2)
4291 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4292 &isl_multi_aff_lex_le_set);
4295 /* Given two piecewise multi affine expressions, return a piecewise
4296 * multi-affine expression defined on the union of the definition domains
4297 * of the inputs that is equal to the lexicographic minimum of the two
4298 * inputs on each cell. If only one of the two inputs is defined on
4299 * a given cell, then it is considered to be the minimum.
4301 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4302 __isl_take isl_pw_multi_aff *pma1,
4303 __isl_take isl_pw_multi_aff *pma2)
4305 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4306 &pw_multi_aff_union_lexmin);
4309 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4310 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4312 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4313 &isl_multi_aff_add);
4316 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4317 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4319 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4320 &pw_multi_aff_add);
4323 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4324 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4326 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4327 &isl_multi_aff_sub);
4330 /* Subtract "pma2" from "pma1" and return the result.
4332 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4333 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4335 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4336 &pw_multi_aff_sub);
4339 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4340 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4342 return isl_pw_multi_aff_union_add_(pma1, pma2);
4345 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4346 * with the actual sum on the shared domain and
4347 * the defined expression on the symmetric difference of the domains.
4349 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4350 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4352 return isl_union_pw_aff_union_add_(upa1, upa2);
4355 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4356 * with the actual sum on the shared domain and
4357 * the defined expression on the symmetric difference of the domains.
4359 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4360 __isl_take isl_union_pw_multi_aff *upma1,
4361 __isl_take isl_union_pw_multi_aff *upma2)
4363 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4366 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4367 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4369 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4370 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4372 int i, j, n;
4373 isl_space *space;
4374 isl_pw_multi_aff *res;
4376 if (!pma1 || !pma2)
4377 goto error;
4379 n = pma1->n * pma2->n;
4380 space = isl_space_product(isl_space_copy(pma1->dim),
4381 isl_space_copy(pma2->dim));
4382 res = isl_pw_multi_aff_alloc_size(space, n);
4384 for (i = 0; i < pma1->n; ++i) {
4385 for (j = 0; j < pma2->n; ++j) {
4386 isl_set *domain;
4387 isl_multi_aff *ma;
4389 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4390 isl_set_copy(pma2->p[j].set));
4391 ma = isl_multi_aff_product(
4392 isl_multi_aff_copy(pma1->p[i].maff),
4393 isl_multi_aff_copy(pma2->p[j].maff));
4394 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4398 isl_pw_multi_aff_free(pma1);
4399 isl_pw_multi_aff_free(pma2);
4400 return res;
4401 error:
4402 isl_pw_multi_aff_free(pma1);
4403 isl_pw_multi_aff_free(pma2);
4404 return NULL;
4407 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4408 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4410 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4411 &pw_multi_aff_product);
4414 /* Construct a map mapping the domain of the piecewise multi-affine expression
4415 * to its range, with each dimension in the range equated to the
4416 * corresponding affine expression on its cell.
4418 * If the domain of "pma" is rational, then so is the constructed "map".
4420 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4422 int i;
4423 isl_map *map;
4425 if (!pma)
4426 return NULL;
4428 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4430 for (i = 0; i < pma->n; ++i) {
4431 isl_bool rational;
4432 isl_multi_aff *maff;
4433 isl_basic_map *bmap;
4434 isl_map *map_i;
4436 rational = isl_set_is_rational(pma->p[i].set);
4437 if (rational < 0)
4438 map = isl_map_free(map);
4439 maff = isl_multi_aff_copy(pma->p[i].maff);
4440 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4441 map_i = isl_map_from_basic_map(bmap);
4442 map_i = isl_map_intersect_domain(map_i,
4443 isl_set_copy(pma->p[i].set));
4444 map = isl_map_union_disjoint(map, map_i);
4447 isl_pw_multi_aff_free(pma);
4448 return map;
4451 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4453 if (!pma)
4454 return NULL;
4456 if (!isl_space_is_set(pma->dim))
4457 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4458 "isl_pw_multi_aff cannot be converted into an isl_set",
4459 goto error);
4461 return isl_map_from_pw_multi_aff(pma);
4462 error:
4463 isl_pw_multi_aff_free(pma);
4464 return NULL;
4467 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4468 * denominator "denom".
4469 * "denom" is allowed to be negative, in which case the actual denominator
4470 * is -denom and the expressions are added instead.
4472 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4473 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4475 int i, first;
4476 int sign;
4477 isl_int d;
4479 first = isl_seq_first_non_zero(c, n);
4480 if (first == -1)
4481 return aff;
4483 sign = isl_int_sgn(denom);
4484 isl_int_init(d);
4485 isl_int_abs(d, denom);
4486 for (i = first; i < n; ++i) {
4487 isl_aff *aff_i;
4489 if (isl_int_is_zero(c[i]))
4490 continue;
4491 aff_i = isl_multi_aff_get_aff(ma, i);
4492 aff_i = isl_aff_scale(aff_i, c[i]);
4493 aff_i = isl_aff_scale_down(aff_i, d);
4494 if (sign >= 0)
4495 aff = isl_aff_sub(aff, aff_i);
4496 else
4497 aff = isl_aff_add(aff, aff_i);
4499 isl_int_clear(d);
4501 return aff;
4504 /* Extract an affine expression that expresses the output dimension "pos"
4505 * of "bmap" in terms of the parameters and input dimensions from
4506 * equality "eq".
4507 * Note that this expression may involve integer divisions defined
4508 * in terms of parameters and input dimensions.
4509 * The equality may also involve references to earlier (but not later)
4510 * output dimensions. These are replaced by the corresponding elements
4511 * in "ma".
4513 * If the equality is of the form
4515 * f(i) + h(j) + a x + g(i) = 0,
4517 * with f(i) a linear combinations of the parameters and input dimensions,
4518 * g(i) a linear combination of integer divisions defined in terms of the same
4519 * and h(j) a linear combinations of earlier output dimensions,
4520 * then the affine expression is
4522 * (-f(i) - g(i))/a - h(j)/a
4524 * If the equality is of the form
4526 * f(i) + h(j) - a x + g(i) = 0,
4528 * then the affine expression is
4530 * (f(i) + g(i))/a - h(j)/(-a)
4533 * If "div" refers to an integer division (i.e., it is smaller than
4534 * the number of integer divisions), then the equality constraint
4535 * does involve an integer division (the one at position "div") that
4536 * is defined in terms of output dimensions. However, this integer
4537 * division can be eliminated by exploiting a pair of constraints
4538 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4539 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4540 * -l + x >= 0.
4541 * In particular, let
4543 * x = e(i) + m floor(...)
4545 * with e(i) the expression derived above and floor(...) the integer
4546 * division involving output dimensions.
4547 * From
4549 * l <= x <= l + n,
4551 * we have
4553 * 0 <= x - l <= n
4555 * This means
4557 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4558 * = (e(i) - l) mod m
4560 * Therefore,
4562 * x - l = (e(i) - l) mod m
4564 * or
4566 * x = ((e(i) - l) mod m) + l
4568 * The variable "shift" below contains the expression -l, which may
4569 * also involve a linear combination of earlier output dimensions.
4571 static __isl_give isl_aff *extract_aff_from_equality(
4572 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4573 __isl_keep isl_multi_aff *ma)
4575 unsigned o_out;
4576 unsigned n_div, n_out;
4577 isl_ctx *ctx;
4578 isl_local_space *ls;
4579 isl_aff *aff, *shift;
4580 isl_val *mod;
4582 ctx = isl_basic_map_get_ctx(bmap);
4583 ls = isl_basic_map_get_local_space(bmap);
4584 ls = isl_local_space_domain(ls);
4585 aff = isl_aff_alloc(isl_local_space_copy(ls));
4586 if (!aff)
4587 goto error;
4588 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4589 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4590 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4591 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4592 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4593 isl_seq_cpy(aff->v->el + 1 + o_out,
4594 bmap->eq[eq] + o_out + n_out, n_div);
4595 } else {
4596 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4597 isl_seq_neg(aff->v->el + 1 + o_out,
4598 bmap->eq[eq] + o_out + n_out, n_div);
4600 if (div < n_div)
4601 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4602 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4603 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4604 bmap->eq[eq][o_out + pos]);
4605 if (div < n_div) {
4606 shift = isl_aff_alloc(isl_local_space_copy(ls));
4607 if (!shift)
4608 goto error;
4609 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4610 isl_seq_cpy(shift->v->el + 1 + o_out,
4611 bmap->ineq[ineq] + o_out + n_out, n_div);
4612 isl_int_set_si(shift->v->el[0], 1);
4613 shift = subtract_initial(shift, ma, pos,
4614 bmap->ineq[ineq] + o_out, ctx->negone);
4615 aff = isl_aff_add(aff, isl_aff_copy(shift));
4616 mod = isl_val_int_from_isl_int(ctx,
4617 bmap->eq[eq][o_out + n_out + div]);
4618 mod = isl_val_abs(mod);
4619 aff = isl_aff_mod_val(aff, mod);
4620 aff = isl_aff_sub(aff, shift);
4623 isl_local_space_free(ls);
4624 return aff;
4625 error:
4626 isl_local_space_free(ls);
4627 isl_aff_free(aff);
4628 return NULL;
4631 /* Given a basic map with output dimensions defined
4632 * in terms of the parameters input dimensions and earlier
4633 * output dimensions using an equality (and possibly a pair on inequalities),
4634 * extract an isl_aff that expresses output dimension "pos" in terms
4635 * of the parameters and input dimensions.
4636 * Note that this expression may involve integer divisions defined
4637 * in terms of parameters and input dimensions.
4638 * "ma" contains the expressions corresponding to earlier output dimensions.
4640 * This function shares some similarities with
4641 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4643 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4644 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4646 int eq, div, ineq;
4647 isl_aff *aff;
4649 if (!bmap)
4650 return NULL;
4651 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4652 if (eq >= bmap->n_eq)
4653 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4654 "unable to find suitable equality", return NULL);
4655 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4657 aff = isl_aff_remove_unused_divs(aff);
4658 return aff;
4661 /* Given a basic map where each output dimension is defined
4662 * in terms of the parameters and input dimensions using an equality,
4663 * extract an isl_multi_aff that expresses the output dimensions in terms
4664 * of the parameters and input dimensions.
4666 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4667 __isl_take isl_basic_map *bmap)
4669 int i;
4670 unsigned n_out;
4671 isl_multi_aff *ma;
4673 if (!bmap)
4674 return NULL;
4676 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4677 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4679 for (i = 0; i < n_out; ++i) {
4680 isl_aff *aff;
4682 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4683 ma = isl_multi_aff_set_aff(ma, i, aff);
4686 isl_basic_map_free(bmap);
4688 return ma;
4691 /* Given a basic set where each set dimension is defined
4692 * in terms of the parameters using an equality,
4693 * extract an isl_multi_aff that expresses the set dimensions in terms
4694 * of the parameters.
4696 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4697 __isl_take isl_basic_set *bset)
4699 return extract_isl_multi_aff_from_basic_map(bset);
4702 /* Create an isl_pw_multi_aff that is equivalent to
4703 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4704 * The given basic map is such that each output dimension is defined
4705 * in terms of the parameters and input dimensions using an equality.
4707 * Since some applications expect the result of isl_pw_multi_aff_from_map
4708 * to only contain integer affine expressions, we compute the floor
4709 * of the expression before returning.
4711 * Remove all constraints involving local variables without
4712 * an explicit representation (resulting in the removal of those
4713 * local variables) prior to the actual extraction to ensure
4714 * that the local spaces in which the resulting affine expressions
4715 * are created do not contain any unknown local variables.
4716 * Removing such constraints is safe because constraints involving
4717 * unknown local variables are not used to determine whether
4718 * a basic map is obviously single-valued.
4720 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4721 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4723 isl_multi_aff *ma;
4725 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4726 ma = extract_isl_multi_aff_from_basic_map(bmap);
4727 ma = isl_multi_aff_floor(ma);
4728 return isl_pw_multi_aff_alloc(domain, ma);
4731 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4732 * This obviously only works if the input "map" is single-valued.
4733 * If so, we compute the lexicographic minimum of the image in the form
4734 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4735 * to its lexicographic minimum.
4736 * If the input is not single-valued, we produce an error.
4738 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4739 __isl_take isl_map *map)
4741 int i;
4742 int sv;
4743 isl_pw_multi_aff *pma;
4745 sv = isl_map_is_single_valued(map);
4746 if (sv < 0)
4747 goto error;
4748 if (!sv)
4749 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4750 "map is not single-valued", goto error);
4751 map = isl_map_make_disjoint(map);
4752 if (!map)
4753 return NULL;
4755 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4757 for (i = 0; i < map->n; ++i) {
4758 isl_pw_multi_aff *pma_i;
4759 isl_basic_map *bmap;
4760 bmap = isl_basic_map_copy(map->p[i]);
4761 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4762 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4765 isl_map_free(map);
4766 return pma;
4767 error:
4768 isl_map_free(map);
4769 return NULL;
4772 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4773 * taking into account that the output dimension at position "d"
4774 * can be represented as
4776 * x = floor((e(...) + c1) / m)
4778 * given that constraint "i" is of the form
4780 * e(...) + c1 - m x >= 0
4783 * Let "map" be of the form
4785 * A -> B
4787 * We construct a mapping
4789 * A -> [A -> x = floor(...)]
4791 * apply that to the map, obtaining
4793 * [A -> x = floor(...)] -> B
4795 * and equate dimension "d" to x.
4796 * We then compute a isl_pw_multi_aff representation of the resulting map
4797 * and plug in the mapping above.
4799 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4800 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4802 isl_ctx *ctx;
4803 isl_space *space;
4804 isl_local_space *ls;
4805 isl_multi_aff *ma;
4806 isl_aff *aff;
4807 isl_vec *v;
4808 isl_map *insert;
4809 int offset;
4810 int n;
4811 int n_in;
4812 isl_pw_multi_aff *pma;
4813 isl_bool is_set;
4815 is_set = isl_map_is_set(map);
4816 if (is_set < 0)
4817 goto error;
4819 offset = isl_basic_map_offset(hull, isl_dim_out);
4820 ctx = isl_map_get_ctx(map);
4821 space = isl_space_domain(isl_map_get_space(map));
4822 n_in = isl_space_dim(space, isl_dim_set);
4823 n = isl_space_dim(space, isl_dim_all);
4825 v = isl_vec_alloc(ctx, 1 + 1 + n);
4826 if (v) {
4827 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4828 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4830 isl_basic_map_free(hull);
4832 ls = isl_local_space_from_space(isl_space_copy(space));
4833 aff = isl_aff_alloc_vec(ls, v);
4834 aff = isl_aff_floor(aff);
4835 if (is_set) {
4836 isl_space_free(space);
4837 ma = isl_multi_aff_from_aff(aff);
4838 } else {
4839 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4840 ma = isl_multi_aff_range_product(ma,
4841 isl_multi_aff_from_aff(aff));
4844 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4845 map = isl_map_apply_domain(map, insert);
4846 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4847 pma = isl_pw_multi_aff_from_map(map);
4848 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4850 return pma;
4851 error:
4852 isl_map_free(map);
4853 isl_basic_map_free(hull);
4854 return NULL;
4857 /* Is constraint "c" of the form
4859 * e(...) + c1 - m x >= 0
4861 * or
4863 * -e(...) + c2 + m x >= 0
4865 * where m > 1 and e only depends on parameters and input dimemnsions?
4867 * "offset" is the offset of the output dimensions
4868 * "pos" is the position of output dimension x.
4870 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4872 if (isl_int_is_zero(c[offset + d]))
4873 return 0;
4874 if (isl_int_is_one(c[offset + d]))
4875 return 0;
4876 if (isl_int_is_negone(c[offset + d]))
4877 return 0;
4878 if (isl_seq_first_non_zero(c + offset, d) != -1)
4879 return 0;
4880 if (isl_seq_first_non_zero(c + offset + d + 1,
4881 total - (offset + d + 1)) != -1)
4882 return 0;
4883 return 1;
4886 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4888 * As a special case, we first check if there is any pair of constraints,
4889 * shared by all the basic maps in "map" that force a given dimension
4890 * to be equal to the floor of some affine combination of the input dimensions.
4892 * In particular, if we can find two constraints
4894 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4896 * and
4898 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4900 * where m > 1 and e only depends on parameters and input dimemnsions,
4901 * and such that
4903 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4905 * then we know that we can take
4907 * x = floor((e(...) + c1) / m)
4909 * without having to perform any computation.
4911 * Note that we know that
4913 * c1 + c2 >= 1
4915 * If c1 + c2 were 0, then we would have detected an equality during
4916 * simplification. If c1 + c2 were negative, then we would have detected
4917 * a contradiction.
4919 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4920 __isl_take isl_map *map)
4922 int d, dim;
4923 int i, j, n;
4924 int offset, total;
4925 isl_int sum;
4926 isl_basic_map *hull;
4928 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4929 if (!hull)
4930 goto error;
4932 isl_int_init(sum);
4933 dim = isl_map_dim(map, isl_dim_out);
4934 offset = isl_basic_map_offset(hull, isl_dim_out);
4935 total = 1 + isl_basic_map_total_dim(hull);
4936 n = hull->n_ineq;
4937 for (d = 0; d < dim; ++d) {
4938 for (i = 0; i < n; ++i) {
4939 if (!is_potential_div_constraint(hull->ineq[i],
4940 offset, d, total))
4941 continue;
4942 for (j = i + 1; j < n; ++j) {
4943 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4944 hull->ineq[j] + 1, total - 1))
4945 continue;
4946 isl_int_add(sum, hull->ineq[i][0],
4947 hull->ineq[j][0]);
4948 if (isl_int_abs_lt(sum,
4949 hull->ineq[i][offset + d]))
4950 break;
4953 if (j >= n)
4954 continue;
4955 isl_int_clear(sum);
4956 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4957 j = i;
4958 return pw_multi_aff_from_map_div(map, hull, d, j);
4961 isl_int_clear(sum);
4962 isl_basic_map_free(hull);
4963 return pw_multi_aff_from_map_base(map);
4964 error:
4965 isl_map_free(map);
4966 isl_basic_map_free(hull);
4967 return NULL;
4970 /* Given an affine expression
4972 * [A -> B] -> f(A,B)
4974 * construct an isl_multi_aff
4976 * [A -> B] -> B'
4978 * such that dimension "d" in B' is set to "aff" and the remaining
4979 * dimensions are set equal to the corresponding dimensions in B.
4980 * "n_in" is the dimension of the space A.
4981 * "n_out" is the dimension of the space B.
4983 * If "is_set" is set, then the affine expression is of the form
4985 * [B] -> f(B)
4987 * and we construct an isl_multi_aff
4989 * B -> B'
4991 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4992 unsigned n_in, unsigned n_out, int is_set)
4994 int i;
4995 isl_multi_aff *ma;
4996 isl_space *space, *space2;
4997 isl_local_space *ls;
4999 space = isl_aff_get_domain_space(aff);
5000 ls = isl_local_space_from_space(isl_space_copy(space));
5001 space2 = isl_space_copy(space);
5002 if (!is_set)
5003 space2 = isl_space_range(isl_space_unwrap(space2));
5004 space = isl_space_map_from_domain_and_range(space, space2);
5005 ma = isl_multi_aff_alloc(space);
5006 ma = isl_multi_aff_set_aff(ma, d, aff);
5008 for (i = 0; i < n_out; ++i) {
5009 if (i == d)
5010 continue;
5011 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5012 isl_dim_set, n_in + i);
5013 ma = isl_multi_aff_set_aff(ma, i, aff);
5016 isl_local_space_free(ls);
5018 return ma;
5021 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5022 * taking into account that the dimension at position "d" can be written as
5024 * x = m a + f(..) (1)
5026 * where m is equal to "gcd".
5027 * "i" is the index of the equality in "hull" that defines f(..).
5028 * In particular, the equality is of the form
5030 * f(..) - x + m g(existentials) = 0
5032 * or
5034 * -f(..) + x + m g(existentials) = 0
5036 * We basically plug (1) into "map", resulting in a map with "a"
5037 * in the range instead of "x". The corresponding isl_pw_multi_aff
5038 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5040 * Specifically, given the input map
5042 * A -> B
5044 * We first wrap it into a set
5046 * [A -> B]
5048 * and define (1) on top of the corresponding space, resulting in "aff".
5049 * We use this to create an isl_multi_aff that maps the output position "d"
5050 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5051 * We plug this into the wrapped map, unwrap the result and compute the
5052 * corresponding isl_pw_multi_aff.
5053 * The result is an expression
5055 * A -> T(A)
5057 * We adjust that to
5059 * A -> [A -> T(A)]
5061 * so that we can plug that into "aff", after extending the latter to
5062 * a mapping
5064 * [A -> B] -> B'
5067 * If "map" is actually a set, then there is no "A" space, meaning
5068 * that we do not need to perform any wrapping, and that the result
5069 * of the recursive call is of the form
5071 * [T]
5073 * which is plugged into a mapping of the form
5075 * B -> B'
5077 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5078 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5079 isl_int gcd)
5081 isl_set *set;
5082 isl_space *space;
5083 isl_local_space *ls;
5084 isl_aff *aff;
5085 isl_multi_aff *ma;
5086 isl_pw_multi_aff *pma, *id;
5087 unsigned n_in;
5088 unsigned o_out;
5089 unsigned n_out;
5090 isl_bool is_set;
5092 is_set = isl_map_is_set(map);
5093 if (is_set < 0)
5094 goto error;
5096 n_in = isl_basic_map_dim(hull, isl_dim_in);
5097 n_out = isl_basic_map_dim(hull, isl_dim_out);
5098 o_out = isl_basic_map_offset(hull, isl_dim_out);
5100 if (is_set)
5101 set = map;
5102 else
5103 set = isl_map_wrap(map);
5104 space = isl_space_map_from_set(isl_set_get_space(set));
5105 ma = isl_multi_aff_identity(space);
5106 ls = isl_local_space_from_space(isl_set_get_space(set));
5107 aff = isl_aff_alloc(ls);
5108 if (aff) {
5109 isl_int_set_si(aff->v->el[0], 1);
5110 if (isl_int_is_one(hull->eq[i][o_out + d]))
5111 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5112 aff->v->size - 1);
5113 else
5114 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5115 aff->v->size - 1);
5116 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5118 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5119 set = isl_set_preimage_multi_aff(set, ma);
5121 ma = range_map(aff, d, n_in, n_out, is_set);
5123 if (is_set)
5124 map = set;
5125 else
5126 map = isl_set_unwrap(set);
5127 pma = isl_pw_multi_aff_from_map(map);
5129 if (!is_set) {
5130 space = isl_pw_multi_aff_get_domain_space(pma);
5131 space = isl_space_map_from_set(space);
5132 id = isl_pw_multi_aff_identity(space);
5133 pma = isl_pw_multi_aff_range_product(id, pma);
5135 id = isl_pw_multi_aff_from_multi_aff(ma);
5136 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5138 isl_basic_map_free(hull);
5139 return pma;
5140 error:
5141 isl_map_free(map);
5142 isl_basic_map_free(hull);
5143 return NULL;
5146 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5147 * "hull" contains the equalities valid for "map".
5149 * Check if any of the output dimensions is "strided".
5150 * That is, we check if it can be written as
5152 * x = m a + f(..)
5154 * with m greater than 1, a some combination of existentially quantified
5155 * variables and f an expression in the parameters and input dimensions.
5156 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5158 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5159 * special case.
5161 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5162 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5164 int i, j;
5165 unsigned n_out;
5166 unsigned o_out;
5167 unsigned n_div;
5168 unsigned o_div;
5169 isl_int gcd;
5171 n_div = isl_basic_map_dim(hull, isl_dim_div);
5172 o_div = isl_basic_map_offset(hull, isl_dim_div);
5174 if (n_div == 0) {
5175 isl_basic_map_free(hull);
5176 return pw_multi_aff_from_map_check_div(map);
5179 isl_int_init(gcd);
5181 n_out = isl_basic_map_dim(hull, isl_dim_out);
5182 o_out = isl_basic_map_offset(hull, isl_dim_out);
5184 for (i = 0; i < n_out; ++i) {
5185 for (j = 0; j < hull->n_eq; ++j) {
5186 isl_int *eq = hull->eq[j];
5187 isl_pw_multi_aff *res;
5189 if (!isl_int_is_one(eq[o_out + i]) &&
5190 !isl_int_is_negone(eq[o_out + i]))
5191 continue;
5192 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5193 continue;
5194 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5195 n_out - (i + 1)) != -1)
5196 continue;
5197 isl_seq_gcd(eq + o_div, n_div, &gcd);
5198 if (isl_int_is_zero(gcd))
5199 continue;
5200 if (isl_int_is_one(gcd))
5201 continue;
5203 res = pw_multi_aff_from_map_stride(map, hull,
5204 i, j, gcd);
5205 isl_int_clear(gcd);
5206 return res;
5210 isl_int_clear(gcd);
5211 isl_basic_map_free(hull);
5212 return pw_multi_aff_from_map_check_div(map);
5215 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5217 * As a special case, we first check if all output dimensions are uniquely
5218 * defined in terms of the parameters and input dimensions over the entire
5219 * domain. If so, we extract the desired isl_pw_multi_aff directly
5220 * from the affine hull of "map" and its domain.
5222 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5223 * special cases.
5225 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5227 isl_bool sv;
5228 isl_basic_map *hull;
5230 if (!map)
5231 return NULL;
5233 if (isl_map_n_basic_map(map) == 1) {
5234 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5235 hull = isl_basic_map_plain_affine_hull(hull);
5236 sv = isl_basic_map_plain_is_single_valued(hull);
5237 if (sv >= 0 && sv)
5238 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5239 hull);
5240 isl_basic_map_free(hull);
5242 map = isl_map_detect_equalities(map);
5243 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5244 sv = isl_basic_map_plain_is_single_valued(hull);
5245 if (sv >= 0 && sv)
5246 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5247 if (sv >= 0)
5248 return pw_multi_aff_from_map_check_strides(map, hull);
5249 isl_basic_map_free(hull);
5250 isl_map_free(map);
5251 return NULL;
5254 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5256 return isl_pw_multi_aff_from_map(set);
5259 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5260 * add it to *user.
5262 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5264 isl_union_pw_multi_aff **upma = user;
5265 isl_pw_multi_aff *pma;
5267 pma = isl_pw_multi_aff_from_map(map);
5268 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5270 return *upma ? isl_stat_ok : isl_stat_error;
5273 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5274 * domain.
5276 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5277 __isl_take isl_aff *aff)
5279 isl_multi_aff *ma;
5280 isl_pw_multi_aff *pma;
5282 ma = isl_multi_aff_from_aff(aff);
5283 pma = isl_pw_multi_aff_from_multi_aff(ma);
5284 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5287 /* Try and create an isl_union_pw_multi_aff that is equivalent
5288 * to the given isl_union_map.
5289 * The isl_union_map is required to be single-valued in each space.
5290 * Otherwise, an error is produced.
5292 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5293 __isl_take isl_union_map *umap)
5295 isl_space *space;
5296 isl_union_pw_multi_aff *upma;
5298 space = isl_union_map_get_space(umap);
5299 upma = isl_union_pw_multi_aff_empty(space);
5300 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5301 upma = isl_union_pw_multi_aff_free(upma);
5302 isl_union_map_free(umap);
5304 return upma;
5307 /* Try and create an isl_union_pw_multi_aff that is equivalent
5308 * to the given isl_union_set.
5309 * The isl_union_set is required to be a singleton in each space.
5310 * Otherwise, an error is produced.
5312 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5313 __isl_take isl_union_set *uset)
5315 return isl_union_pw_multi_aff_from_union_map(uset);
5318 /* Return the piecewise affine expression "set ? 1 : 0".
5320 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5322 isl_pw_aff *pa;
5323 isl_space *space = isl_set_get_space(set);
5324 isl_local_space *ls = isl_local_space_from_space(space);
5325 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5326 isl_aff *one = isl_aff_zero_on_domain(ls);
5328 one = isl_aff_add_constant_si(one, 1);
5329 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5330 set = isl_set_complement(set);
5331 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5333 return pa;
5336 /* Plug in "subs" for dimension "type", "pos" of "aff".
5338 * Let i be the dimension to replace and let "subs" be of the form
5340 * f/d
5342 * and "aff" of the form
5344 * (a i + g)/m
5346 * The result is
5348 * (a f + d g')/(m d)
5350 * where g' is the result of plugging in "subs" in each of the integer
5351 * divisions in g.
5353 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5354 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5356 isl_ctx *ctx;
5357 isl_int v;
5359 aff = isl_aff_cow(aff);
5360 if (!aff || !subs)
5361 return isl_aff_free(aff);
5363 ctx = isl_aff_get_ctx(aff);
5364 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5365 isl_die(ctx, isl_error_invalid,
5366 "spaces don't match", return isl_aff_free(aff));
5367 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5368 isl_die(ctx, isl_error_unsupported,
5369 "cannot handle divs yet", return isl_aff_free(aff));
5371 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5372 if (!aff->ls)
5373 return isl_aff_free(aff);
5375 aff->v = isl_vec_cow(aff->v);
5376 if (!aff->v)
5377 return isl_aff_free(aff);
5379 pos += isl_local_space_offset(aff->ls, type);
5381 isl_int_init(v);
5382 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5383 aff->v->size, subs->v->size, v);
5384 isl_int_clear(v);
5386 return aff;
5389 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5390 * expressions in "maff".
5392 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5393 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5394 __isl_keep isl_aff *subs)
5396 int i;
5398 maff = isl_multi_aff_cow(maff);
5399 if (!maff || !subs)
5400 return isl_multi_aff_free(maff);
5402 if (type == isl_dim_in)
5403 type = isl_dim_set;
5405 for (i = 0; i < maff->n; ++i) {
5406 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5407 type, pos, subs);
5408 if (!maff->u.p[i])
5409 return isl_multi_aff_free(maff);
5412 return maff;
5415 /* Plug in "subs" for dimension "type", "pos" of "pma".
5417 * pma is of the form
5419 * A_i(v) -> M_i(v)
5421 * while subs is of the form
5423 * v' = B_j(v) -> S_j
5425 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5426 * has a contribution in the result, in particular
5428 * C_ij(S_j) -> M_i(S_j)
5430 * Note that plugging in S_j in C_ij may also result in an empty set
5431 * and this contribution should simply be discarded.
5433 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5434 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5435 __isl_keep isl_pw_aff *subs)
5437 int i, j, n;
5438 isl_pw_multi_aff *res;
5440 if (!pma || !subs)
5441 return isl_pw_multi_aff_free(pma);
5443 n = pma->n * subs->n;
5444 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5446 for (i = 0; i < pma->n; ++i) {
5447 for (j = 0; j < subs->n; ++j) {
5448 isl_set *common;
5449 isl_multi_aff *res_ij;
5450 int empty;
5452 common = isl_set_intersect(
5453 isl_set_copy(pma->p[i].set),
5454 isl_set_copy(subs->p[j].set));
5455 common = isl_set_substitute(common,
5456 type, pos, subs->p[j].aff);
5457 empty = isl_set_plain_is_empty(common);
5458 if (empty < 0 || empty) {
5459 isl_set_free(common);
5460 if (empty < 0)
5461 goto error;
5462 continue;
5465 res_ij = isl_multi_aff_substitute(
5466 isl_multi_aff_copy(pma->p[i].maff),
5467 type, pos, subs->p[j].aff);
5469 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5473 isl_pw_multi_aff_free(pma);
5474 return res;
5475 error:
5476 isl_pw_multi_aff_free(pma);
5477 isl_pw_multi_aff_free(res);
5478 return NULL;
5481 /* Compute the preimage of a range of dimensions in the affine expression "src"
5482 * under "ma" and put the result in "dst". The number of dimensions in "src"
5483 * that precede the range is given by "n_before". The number of dimensions
5484 * in the range is given by the number of output dimensions of "ma".
5485 * The number of dimensions that follow the range is given by "n_after".
5486 * If "has_denom" is set (to one),
5487 * then "src" and "dst" have an extra initial denominator.
5488 * "n_div_ma" is the number of existentials in "ma"
5489 * "n_div_bset" is the number of existentials in "src"
5490 * The resulting "dst" (which is assumed to have been allocated by
5491 * the caller) contains coefficients for both sets of existentials,
5492 * first those in "ma" and then those in "src".
5493 * f, c1, c2 and g are temporary objects that have been initialized
5494 * by the caller.
5496 * Let src represent the expression
5498 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5500 * and let ma represent the expressions
5502 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5504 * We start out with the following expression for dst:
5506 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5508 * with the multiplication factor f initially equal to 1
5509 * and f \sum_i b_i v_i kept separately.
5510 * For each x_i that we substitute, we multiply the numerator
5511 * (and denominator) of dst by c_1 = m_i and add the numerator
5512 * of the x_i expression multiplied by c_2 = f b_i,
5513 * after removing the common factors of c_1 and c_2.
5514 * The multiplication factor f also needs to be multiplied by c_1
5515 * for the next x_j, j > i.
5517 void isl_seq_preimage(isl_int *dst, isl_int *src,
5518 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5519 int n_div_ma, int n_div_bmap,
5520 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5522 int i;
5523 int n_param, n_in, n_out;
5524 int o_dst, o_src;
5526 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5527 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5528 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5530 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5531 o_dst = o_src = has_denom + 1 + n_param + n_before;
5532 isl_seq_clr(dst + o_dst, n_in);
5533 o_dst += n_in;
5534 o_src += n_out;
5535 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5536 o_dst += n_after;
5537 o_src += n_after;
5538 isl_seq_clr(dst + o_dst, n_div_ma);
5539 o_dst += n_div_ma;
5540 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5542 isl_int_set_si(f, 1);
5544 for (i = 0; i < n_out; ++i) {
5545 int offset = has_denom + 1 + n_param + n_before + i;
5547 if (isl_int_is_zero(src[offset]))
5548 continue;
5549 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5550 isl_int_mul(c2, f, src[offset]);
5551 isl_int_gcd(g, c1, c2);
5552 isl_int_divexact(c1, c1, g);
5553 isl_int_divexact(c2, c2, g);
5555 isl_int_mul(f, f, c1);
5556 o_dst = has_denom;
5557 o_src = 1;
5558 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5559 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5560 o_dst += 1 + n_param;
5561 o_src += 1 + n_param;
5562 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5563 o_dst += n_before;
5564 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5565 c2, ma->u.p[i]->v->el + o_src, n_in);
5566 o_dst += n_in;
5567 o_src += n_in;
5568 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5569 o_dst += n_after;
5570 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5571 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5572 o_dst += n_div_ma;
5573 o_src += n_div_ma;
5574 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5575 if (has_denom)
5576 isl_int_mul(dst[0], dst[0], c1);
5580 /* Compute the pullback of "aff" by the function represented by "ma".
5581 * In other words, plug in "ma" in "aff". The result is an affine expression
5582 * defined over the domain space of "ma".
5584 * If "aff" is represented by
5586 * (a(p) + b x + c(divs))/d
5588 * and ma is represented by
5590 * x = D(p) + F(y) + G(divs')
5592 * then the result is
5594 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5596 * The divs in the local space of the input are similarly adjusted
5597 * through a call to isl_local_space_preimage_multi_aff.
5599 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5600 __isl_take isl_multi_aff *ma)
5602 isl_aff *res = NULL;
5603 isl_local_space *ls;
5604 int n_div_aff, n_div_ma;
5605 isl_int f, c1, c2, g;
5607 ma = isl_multi_aff_align_divs(ma);
5608 if (!aff || !ma)
5609 goto error;
5611 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5612 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5614 ls = isl_aff_get_domain_local_space(aff);
5615 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5616 res = isl_aff_alloc(ls);
5617 if (!res)
5618 goto error;
5620 isl_int_init(f);
5621 isl_int_init(c1);
5622 isl_int_init(c2);
5623 isl_int_init(g);
5625 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5626 f, c1, c2, g, 1);
5628 isl_int_clear(f);
5629 isl_int_clear(c1);
5630 isl_int_clear(c2);
5631 isl_int_clear(g);
5633 isl_aff_free(aff);
5634 isl_multi_aff_free(ma);
5635 res = isl_aff_normalize(res);
5636 return res;
5637 error:
5638 isl_aff_free(aff);
5639 isl_multi_aff_free(ma);
5640 isl_aff_free(res);
5641 return NULL;
5644 /* Compute the pullback of "aff1" by the function represented by "aff2".
5645 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5646 * defined over the domain space of "aff1".
5648 * The domain of "aff1" should match the range of "aff2", which means
5649 * that it should be single-dimensional.
5651 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5652 __isl_take isl_aff *aff2)
5654 isl_multi_aff *ma;
5656 ma = isl_multi_aff_from_aff(aff2);
5657 return isl_aff_pullback_multi_aff(aff1, ma);
5660 /* Compute the pullback of "ma1" by the function represented by "ma2".
5661 * In other words, plug in "ma2" in "ma1".
5663 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5665 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5666 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5668 int i;
5669 isl_space *space = NULL;
5671 ma2 = isl_multi_aff_align_divs(ma2);
5672 ma1 = isl_multi_aff_cow(ma1);
5673 if (!ma1 || !ma2)
5674 goto error;
5676 space = isl_space_join(isl_multi_aff_get_space(ma2),
5677 isl_multi_aff_get_space(ma1));
5679 for (i = 0; i < ma1->n; ++i) {
5680 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5681 isl_multi_aff_copy(ma2));
5682 if (!ma1->u.p[i])
5683 goto error;
5686 ma1 = isl_multi_aff_reset_space(ma1, space);
5687 isl_multi_aff_free(ma2);
5688 return ma1;
5689 error:
5690 isl_space_free(space);
5691 isl_multi_aff_free(ma2);
5692 isl_multi_aff_free(ma1);
5693 return NULL;
5696 /* Compute the pullback of "ma1" by the function represented by "ma2".
5697 * In other words, plug in "ma2" in "ma1".
5699 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5700 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5702 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5703 &isl_multi_aff_pullback_multi_aff_aligned);
5706 /* Extend the local space of "dst" to include the divs
5707 * in the local space of "src".
5709 * If "src" does not have any divs or if the local spaces of "dst" and
5710 * "src" are the same, then no extension is required.
5712 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5713 __isl_keep isl_aff *src)
5715 isl_ctx *ctx;
5716 int src_n_div, dst_n_div;
5717 int *exp1 = NULL;
5718 int *exp2 = NULL;
5719 isl_bool equal;
5720 isl_mat *div;
5722 if (!src || !dst)
5723 return isl_aff_free(dst);
5725 ctx = isl_aff_get_ctx(src);
5726 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5727 if (equal < 0)
5728 return isl_aff_free(dst);
5729 if (!equal)
5730 isl_die(ctx, isl_error_invalid,
5731 "spaces don't match", goto error);
5733 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5734 if (src_n_div == 0)
5735 return dst;
5736 equal = isl_local_space_is_equal(src->ls, dst->ls);
5737 if (equal < 0)
5738 return isl_aff_free(dst);
5739 if (equal)
5740 return dst;
5742 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5743 exp1 = isl_alloc_array(ctx, int, src_n_div);
5744 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5745 if (!exp1 || (dst_n_div && !exp2))
5746 goto error;
5748 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5749 dst = isl_aff_expand_divs(dst, div, exp2);
5750 free(exp1);
5751 free(exp2);
5753 return dst;
5754 error:
5755 free(exp1);
5756 free(exp2);
5757 return isl_aff_free(dst);
5760 /* Adjust the local spaces of the affine expressions in "maff"
5761 * such that they all have the save divs.
5763 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5764 __isl_take isl_multi_aff *maff)
5766 int i;
5768 if (!maff)
5769 return NULL;
5770 if (maff->n == 0)
5771 return maff;
5772 maff = isl_multi_aff_cow(maff);
5773 if (!maff)
5774 return NULL;
5776 for (i = 1; i < maff->n; ++i)
5777 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5778 for (i = 1; i < maff->n; ++i) {
5779 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5780 if (!maff->u.p[i])
5781 return isl_multi_aff_free(maff);
5784 return maff;
5787 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5789 aff = isl_aff_cow(aff);
5790 if (!aff)
5791 return NULL;
5793 aff->ls = isl_local_space_lift(aff->ls);
5794 if (!aff->ls)
5795 return isl_aff_free(aff);
5797 return aff;
5800 /* Lift "maff" to a space with extra dimensions such that the result
5801 * has no more existentially quantified variables.
5802 * If "ls" is not NULL, then *ls is assigned the local space that lies
5803 * at the basis of the lifting applied to "maff".
5805 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5806 __isl_give isl_local_space **ls)
5808 int i;
5809 isl_space *space;
5810 unsigned n_div;
5812 if (ls)
5813 *ls = NULL;
5815 if (!maff)
5816 return NULL;
5818 if (maff->n == 0) {
5819 if (ls) {
5820 isl_space *space = isl_multi_aff_get_domain_space(maff);
5821 *ls = isl_local_space_from_space(space);
5822 if (!*ls)
5823 return isl_multi_aff_free(maff);
5825 return maff;
5828 maff = isl_multi_aff_cow(maff);
5829 maff = isl_multi_aff_align_divs(maff);
5830 if (!maff)
5831 return NULL;
5833 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5834 space = isl_multi_aff_get_space(maff);
5835 space = isl_space_lift(isl_space_domain(space), n_div);
5836 space = isl_space_extend_domain_with_range(space,
5837 isl_multi_aff_get_space(maff));
5838 if (!space)
5839 return isl_multi_aff_free(maff);
5840 isl_space_free(maff->space);
5841 maff->space = space;
5843 if (ls) {
5844 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5845 if (!*ls)
5846 return isl_multi_aff_free(maff);
5849 for (i = 0; i < maff->n; ++i) {
5850 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5851 if (!maff->u.p[i])
5852 goto error;
5855 return maff;
5856 error:
5857 if (ls)
5858 isl_local_space_free(*ls);
5859 return isl_multi_aff_free(maff);
5863 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5865 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5866 __isl_keep isl_pw_multi_aff *pma, int pos)
5868 int i;
5869 int n_out;
5870 isl_space *space;
5871 isl_pw_aff *pa;
5873 if (!pma)
5874 return NULL;
5876 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5877 if (pos < 0 || pos >= n_out)
5878 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5879 "index out of bounds", return NULL);
5881 space = isl_pw_multi_aff_get_space(pma);
5882 space = isl_space_drop_dims(space, isl_dim_out,
5883 pos + 1, n_out - pos - 1);
5884 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5886 pa = isl_pw_aff_alloc_size(space, pma->n);
5887 for (i = 0; i < pma->n; ++i) {
5888 isl_aff *aff;
5889 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5890 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5893 return pa;
5896 /* Return an isl_pw_multi_aff with the given "set" as domain and
5897 * an unnamed zero-dimensional range.
5899 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5900 __isl_take isl_set *set)
5902 isl_multi_aff *ma;
5903 isl_space *space;
5905 space = isl_set_get_space(set);
5906 space = isl_space_from_domain(space);
5907 ma = isl_multi_aff_zero(space);
5908 return isl_pw_multi_aff_alloc(set, ma);
5911 /* Add an isl_pw_multi_aff with the given "set" as domain and
5912 * an unnamed zero-dimensional range to *user.
5914 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5915 void *user)
5917 isl_union_pw_multi_aff **upma = user;
5918 isl_pw_multi_aff *pma;
5920 pma = isl_pw_multi_aff_from_domain(set);
5921 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5923 return isl_stat_ok;
5926 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5927 * an unnamed zero-dimensional range.
5929 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5930 __isl_take isl_union_set *uset)
5932 isl_space *space;
5933 isl_union_pw_multi_aff *upma;
5935 if (!uset)
5936 return NULL;
5938 space = isl_union_set_get_space(uset);
5939 upma = isl_union_pw_multi_aff_empty(space);
5941 if (isl_union_set_foreach_set(uset,
5942 &add_pw_multi_aff_from_domain, &upma) < 0)
5943 goto error;
5945 isl_union_set_free(uset);
5946 return upma;
5947 error:
5948 isl_union_set_free(uset);
5949 isl_union_pw_multi_aff_free(upma);
5950 return NULL;
5953 /* Convert "pma" to an isl_map and add it to *umap.
5955 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5956 void *user)
5958 isl_union_map **umap = user;
5959 isl_map *map;
5961 map = isl_map_from_pw_multi_aff(pma);
5962 *umap = isl_union_map_add_map(*umap, map);
5964 return isl_stat_ok;
5967 /* Construct a union map mapping the domain of the union
5968 * piecewise multi-affine expression to its range, with each dimension
5969 * in the range equated to the corresponding affine expression on its cell.
5971 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5972 __isl_take isl_union_pw_multi_aff *upma)
5974 isl_space *space;
5975 isl_union_map *umap;
5977 if (!upma)
5978 return NULL;
5980 space = isl_union_pw_multi_aff_get_space(upma);
5981 umap = isl_union_map_empty(space);
5983 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5984 &map_from_pw_multi_aff, &umap) < 0)
5985 goto error;
5987 isl_union_pw_multi_aff_free(upma);
5988 return umap;
5989 error:
5990 isl_union_pw_multi_aff_free(upma);
5991 isl_union_map_free(umap);
5992 return NULL;
5995 /* Local data for bin_entry and the callback "fn".
5997 struct isl_union_pw_multi_aff_bin_data {
5998 isl_union_pw_multi_aff *upma2;
5999 isl_union_pw_multi_aff *res;
6000 isl_pw_multi_aff *pma;
6001 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6004 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6005 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6007 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6009 struct isl_union_pw_multi_aff_bin_data *data = user;
6010 isl_stat r;
6012 data->pma = pma;
6013 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6014 data->fn, data);
6015 isl_pw_multi_aff_free(pma);
6017 return r;
6020 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6021 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6022 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6023 * as *entry. The callback should adjust data->res if desired.
6025 static __isl_give isl_union_pw_multi_aff *bin_op(
6026 __isl_take isl_union_pw_multi_aff *upma1,
6027 __isl_take isl_union_pw_multi_aff *upma2,
6028 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6030 isl_space *space;
6031 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6033 space = isl_union_pw_multi_aff_get_space(upma2);
6034 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6035 space = isl_union_pw_multi_aff_get_space(upma1);
6036 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6038 if (!upma1 || !upma2)
6039 goto error;
6041 data.upma2 = upma2;
6042 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6043 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6044 &bin_entry, &data) < 0)
6045 goto error;
6047 isl_union_pw_multi_aff_free(upma1);
6048 isl_union_pw_multi_aff_free(upma2);
6049 return data.res;
6050 error:
6051 isl_union_pw_multi_aff_free(upma1);
6052 isl_union_pw_multi_aff_free(upma2);
6053 isl_union_pw_multi_aff_free(data.res);
6054 return NULL;
6057 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6058 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6060 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6061 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6063 isl_space *space;
6065 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6066 isl_pw_multi_aff_get_space(pma2));
6067 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6068 &isl_multi_aff_range_product);
6071 /* Given two isl_pw_multi_affs A -> B and C -> D,
6072 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6074 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6075 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6077 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6078 &pw_multi_aff_range_product);
6081 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6082 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6084 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6085 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6087 isl_space *space;
6089 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6090 isl_pw_multi_aff_get_space(pma2));
6091 space = isl_space_flatten_range(space);
6092 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6093 &isl_multi_aff_flat_range_product);
6096 /* Given two isl_pw_multi_affs A -> B and C -> D,
6097 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6099 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6100 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6102 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6103 &pw_multi_aff_flat_range_product);
6106 /* If data->pma and "pma2" have the same domain space, then compute
6107 * their flat range product and the result to data->res.
6109 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6110 void *user)
6112 struct isl_union_pw_multi_aff_bin_data *data = user;
6114 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6115 pma2->dim, isl_dim_in)) {
6116 isl_pw_multi_aff_free(pma2);
6117 return isl_stat_ok;
6120 pma2 = isl_pw_multi_aff_flat_range_product(
6121 isl_pw_multi_aff_copy(data->pma), pma2);
6123 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6125 return isl_stat_ok;
6128 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6129 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6131 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6132 __isl_take isl_union_pw_multi_aff *upma1,
6133 __isl_take isl_union_pw_multi_aff *upma2)
6135 return bin_op(upma1, upma2, &flat_range_product_entry);
6138 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6139 * The parameters are assumed to have been aligned.
6141 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6142 * except that it works on two different isl_pw_* types.
6144 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6145 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6146 __isl_take isl_pw_aff *pa)
6148 int i, j, n;
6149 isl_pw_multi_aff *res = NULL;
6151 if (!pma || !pa)
6152 goto error;
6154 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6155 pa->dim, isl_dim_in))
6156 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6157 "domains don't match", goto error);
6158 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6159 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6160 "index out of bounds", goto error);
6162 n = pma->n * pa->n;
6163 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6165 for (i = 0; i < pma->n; ++i) {
6166 for (j = 0; j < pa->n; ++j) {
6167 isl_set *common;
6168 isl_multi_aff *res_ij;
6169 int empty;
6171 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6172 isl_set_copy(pa->p[j].set));
6173 empty = isl_set_plain_is_empty(common);
6174 if (empty < 0 || empty) {
6175 isl_set_free(common);
6176 if (empty < 0)
6177 goto error;
6178 continue;
6181 res_ij = isl_multi_aff_set_aff(
6182 isl_multi_aff_copy(pma->p[i].maff), pos,
6183 isl_aff_copy(pa->p[j].aff));
6184 res_ij = isl_multi_aff_gist(res_ij,
6185 isl_set_copy(common));
6187 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6191 isl_pw_multi_aff_free(pma);
6192 isl_pw_aff_free(pa);
6193 return res;
6194 error:
6195 isl_pw_multi_aff_free(pma);
6196 isl_pw_aff_free(pa);
6197 return isl_pw_multi_aff_free(res);
6200 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6202 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6203 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6204 __isl_take isl_pw_aff *pa)
6206 isl_bool equal_params;
6208 if (!pma || !pa)
6209 goto error;
6210 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6211 if (equal_params < 0)
6212 goto error;
6213 if (equal_params)
6214 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6215 if (!isl_space_has_named_params(pma->dim) ||
6216 !isl_space_has_named_params(pa->dim))
6217 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6218 "unaligned unnamed parameters", goto error);
6219 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6220 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6221 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6222 error:
6223 isl_pw_multi_aff_free(pma);
6224 isl_pw_aff_free(pa);
6225 return NULL;
6228 /* Do the parameters of "pa" match those of "space"?
6230 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6231 __isl_keep isl_space *space)
6233 isl_space *pa_space;
6234 isl_bool match;
6236 if (!pa || !space)
6237 return isl_bool_error;
6239 pa_space = isl_pw_aff_get_space(pa);
6241 match = isl_space_has_equal_params(space, pa_space);
6243 isl_space_free(pa_space);
6244 return match;
6247 /* Check that the domain space of "pa" matches "space".
6249 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6250 __isl_keep isl_space *space)
6252 isl_space *pa_space;
6253 isl_bool match;
6255 if (!pa || !space)
6256 return isl_stat_error;
6258 pa_space = isl_pw_aff_get_space(pa);
6260 match = isl_space_has_equal_params(space, pa_space);
6261 if (match < 0)
6262 goto error;
6263 if (!match)
6264 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6265 "parameters don't match", goto error);
6266 match = isl_space_tuple_is_equal(space, isl_dim_in,
6267 pa_space, isl_dim_in);
6268 if (match < 0)
6269 goto error;
6270 if (!match)
6271 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6272 "domains don't match", goto error);
6273 isl_space_free(pa_space);
6274 return isl_stat_ok;
6275 error:
6276 isl_space_free(pa_space);
6277 return isl_stat_error;
6280 #undef BASE
6281 #define BASE pw_aff
6282 #undef DOMBASE
6283 #define DOMBASE set
6285 #include <isl_multi_explicit_domain.c>
6286 #include <isl_multi_pw_aff_explicit_domain.c>
6287 #include <isl_multi_templ.c>
6288 #include <isl_multi_apply_set.c>
6289 #include <isl_multi_coalesce.c>
6290 #include <isl_multi_dims.c>
6291 #include <isl_multi_gist.c>
6292 #include <isl_multi_hash.c>
6293 #include <isl_multi_align_set.c>
6294 #include <isl_multi_intersect.c>
6296 /* Does "mpa" have a non-trivial explicit domain?
6298 * The explicit domain, if present, is trivial if it represents
6299 * an (obviously) universe set.
6301 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6302 __isl_keep isl_multi_pw_aff *mpa)
6304 if (!mpa)
6305 return isl_bool_error;
6306 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6307 return isl_bool_false;
6308 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6311 /* Scale the elements of "pma" by the corresponding elements of "mv".
6313 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6314 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6316 int i;
6317 isl_bool equal_params;
6319 pma = isl_pw_multi_aff_cow(pma);
6320 if (!pma || !mv)
6321 goto error;
6322 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6323 mv->space, isl_dim_set))
6324 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6325 "spaces don't match", goto error);
6326 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6327 if (equal_params < 0)
6328 goto error;
6329 if (!equal_params) {
6330 pma = isl_pw_multi_aff_align_params(pma,
6331 isl_multi_val_get_space(mv));
6332 mv = isl_multi_val_align_params(mv,
6333 isl_pw_multi_aff_get_space(pma));
6334 if (!pma || !mv)
6335 goto error;
6338 for (i = 0; i < pma->n; ++i) {
6339 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6340 isl_multi_val_copy(mv));
6341 if (!pma->p[i].maff)
6342 goto error;
6345 isl_multi_val_free(mv);
6346 return pma;
6347 error:
6348 isl_multi_val_free(mv);
6349 isl_pw_multi_aff_free(pma);
6350 return NULL;
6353 /* This function is called for each entry of an isl_union_pw_multi_aff.
6354 * If the space of the entry matches that of data->mv,
6355 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6356 * Otherwise, return an empty isl_pw_multi_aff.
6358 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6359 __isl_take isl_pw_multi_aff *pma, void *user)
6361 isl_multi_val *mv = user;
6363 if (!pma)
6364 return NULL;
6365 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6366 mv->space, isl_dim_set)) {
6367 isl_space *space = isl_pw_multi_aff_get_space(pma);
6368 isl_pw_multi_aff_free(pma);
6369 return isl_pw_multi_aff_empty(space);
6372 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6375 /* Scale the elements of "upma" by the corresponding elements of "mv",
6376 * for those entries that match the space of "mv".
6378 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6379 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6381 upma = isl_union_pw_multi_aff_align_params(upma,
6382 isl_multi_val_get_space(mv));
6383 mv = isl_multi_val_align_params(mv,
6384 isl_union_pw_multi_aff_get_space(upma));
6385 if (!upma || !mv)
6386 goto error;
6388 return isl_union_pw_multi_aff_transform(upma,
6389 &union_pw_multi_aff_scale_multi_val_entry, mv);
6391 isl_multi_val_free(mv);
6392 return upma;
6393 error:
6394 isl_multi_val_free(mv);
6395 isl_union_pw_multi_aff_free(upma);
6396 return NULL;
6399 /* Construct and return a piecewise multi affine expression
6400 * in the given space with value zero in each of the output dimensions and
6401 * a universe domain.
6403 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6405 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6408 /* Construct and return a piecewise multi affine expression
6409 * that is equal to the given piecewise affine expression.
6411 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6412 __isl_take isl_pw_aff *pa)
6414 int i;
6415 isl_space *space;
6416 isl_pw_multi_aff *pma;
6418 if (!pa)
6419 return NULL;
6421 space = isl_pw_aff_get_space(pa);
6422 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6424 for (i = 0; i < pa->n; ++i) {
6425 isl_set *set;
6426 isl_multi_aff *ma;
6428 set = isl_set_copy(pa->p[i].set);
6429 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6430 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6433 isl_pw_aff_free(pa);
6434 return pma;
6437 /* Construct a set or map mapping the shared (parameter) domain
6438 * of the piecewise affine expressions to the range of "mpa"
6439 * with each dimension in the range equated to the
6440 * corresponding piecewise affine expression.
6442 static __isl_give isl_map *map_from_multi_pw_aff(
6443 __isl_take isl_multi_pw_aff *mpa)
6445 int i;
6446 isl_space *space;
6447 isl_map *map;
6449 if (!mpa)
6450 return NULL;
6452 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6453 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6454 "invalid space", goto error);
6456 space = isl_multi_pw_aff_get_domain_space(mpa);
6457 map = isl_map_universe(isl_space_from_domain(space));
6459 for (i = 0; i < mpa->n; ++i) {
6460 isl_pw_aff *pa;
6461 isl_map *map_i;
6463 pa = isl_pw_aff_copy(mpa->u.p[i]);
6464 map_i = map_from_pw_aff(pa);
6466 map = isl_map_flat_range_product(map, map_i);
6469 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6471 isl_multi_pw_aff_free(mpa);
6472 return map;
6473 error:
6474 isl_multi_pw_aff_free(mpa);
6475 return NULL;
6478 /* Construct a map mapping the shared domain
6479 * of the piecewise affine expressions to the range of "mpa"
6480 * with each dimension in the range equated to the
6481 * corresponding piecewise affine expression.
6483 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6485 if (!mpa)
6486 return NULL;
6487 if (isl_space_is_set(mpa->space))
6488 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6489 "space of input is not a map", goto error);
6491 return map_from_multi_pw_aff(mpa);
6492 error:
6493 isl_multi_pw_aff_free(mpa);
6494 return NULL;
6497 /* Construct a set mapping the shared parameter domain
6498 * of the piecewise affine expressions to the space of "mpa"
6499 * with each dimension in the range equated to the
6500 * corresponding piecewise affine expression.
6502 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6504 if (!mpa)
6505 return NULL;
6506 if (!isl_space_is_set(mpa->space))
6507 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6508 "space of input is not a set", goto error);
6510 return map_from_multi_pw_aff(mpa);
6511 error:
6512 isl_multi_pw_aff_free(mpa);
6513 return NULL;
6516 /* Construct and return a piecewise multi affine expression
6517 * that is equal to the given multi piecewise affine expression
6518 * on the shared domain of the piecewise affine expressions,
6519 * in the special case of a 0D multi piecewise affine expression.
6521 * Create a piecewise multi affine expression with the explicit domain of
6522 * the 0D multi piecewise affine expression as domain.
6524 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6525 __isl_take isl_multi_pw_aff *mpa)
6527 isl_space *space;
6528 isl_set *dom;
6529 isl_multi_aff *ma;
6531 space = isl_multi_pw_aff_get_space(mpa);
6532 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6533 isl_multi_pw_aff_free(mpa);
6535 ma = isl_multi_aff_zero(space);
6536 return isl_pw_multi_aff_alloc(dom, ma);
6539 /* Construct and return a piecewise multi affine expression
6540 * that is equal to the given multi piecewise affine expression
6541 * on the shared domain of the piecewise affine expressions.
6543 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6544 __isl_take isl_multi_pw_aff *mpa)
6546 int i;
6547 isl_space *space;
6548 isl_pw_aff *pa;
6549 isl_pw_multi_aff *pma;
6551 if (!mpa)
6552 return NULL;
6554 if (mpa->n == 0)
6555 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6557 space = isl_multi_pw_aff_get_space(mpa);
6558 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6559 pma = isl_pw_multi_aff_from_pw_aff(pa);
6561 for (i = 1; i < mpa->n; ++i) {
6562 isl_pw_multi_aff *pma_i;
6564 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6565 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6566 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6569 pma = isl_pw_multi_aff_reset_space(pma, space);
6571 isl_multi_pw_aff_free(mpa);
6572 return pma;
6575 /* Construct and return a multi piecewise affine expression
6576 * that is equal to the given multi affine expression.
6578 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6579 __isl_take isl_multi_aff *ma)
6581 int i, n;
6582 isl_multi_pw_aff *mpa;
6584 if (!ma)
6585 return NULL;
6587 n = isl_multi_aff_dim(ma, isl_dim_out);
6588 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6590 for (i = 0; i < n; ++i) {
6591 isl_pw_aff *pa;
6593 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6594 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6597 isl_multi_aff_free(ma);
6598 return mpa;
6601 /* Construct and return a multi piecewise affine expression
6602 * that is equal to the given piecewise multi affine expression.
6604 * If the resulting multi piecewise affine expression has
6605 * an explicit domain, then assign it the domain of the input.
6606 * In other cases, the domain is stored in the individual elements.
6608 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6609 __isl_take isl_pw_multi_aff *pma)
6611 int i, n;
6612 isl_space *space;
6613 isl_multi_pw_aff *mpa;
6615 if (!pma)
6616 return NULL;
6618 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6619 space = isl_pw_multi_aff_get_space(pma);
6620 mpa = isl_multi_pw_aff_alloc(space);
6622 for (i = 0; i < n; ++i) {
6623 isl_pw_aff *pa;
6625 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6626 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6628 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6629 isl_set *dom;
6631 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6632 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6635 isl_pw_multi_aff_free(pma);
6636 return mpa;
6639 /* Do "pa1" and "pa2" represent the same function?
6641 * We first check if they are obviously equal.
6642 * If not, we convert them to maps and check if those are equal.
6644 * If "pa1" or "pa2" contain any NaNs, then they are considered
6645 * not to be the same. A NaN is not equal to anything, not even
6646 * to another NaN.
6648 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6649 __isl_keep isl_pw_aff *pa2)
6651 isl_bool equal;
6652 isl_bool has_nan;
6653 isl_map *map1, *map2;
6655 if (!pa1 || !pa2)
6656 return isl_bool_error;
6658 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6659 if (equal < 0 || equal)
6660 return equal;
6661 has_nan = either_involves_nan(pa1, pa2);
6662 if (has_nan < 0)
6663 return isl_bool_error;
6664 if (has_nan)
6665 return isl_bool_false;
6667 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6668 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6669 equal = isl_map_is_equal(map1, map2);
6670 isl_map_free(map1);
6671 isl_map_free(map2);
6673 return equal;
6676 /* Do "mpa1" and "mpa2" represent the same function?
6678 * Note that we cannot convert the entire isl_multi_pw_aff
6679 * to a map because the domains of the piecewise affine expressions
6680 * may not be the same.
6682 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6683 __isl_keep isl_multi_pw_aff *mpa2)
6685 int i;
6686 isl_bool equal, equal_params;
6688 if (!mpa1 || !mpa2)
6689 return isl_bool_error;
6691 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6692 if (equal_params < 0)
6693 return isl_bool_error;
6694 if (!equal_params) {
6695 if (!isl_space_has_named_params(mpa1->space))
6696 return isl_bool_false;
6697 if (!isl_space_has_named_params(mpa2->space))
6698 return isl_bool_false;
6699 mpa1 = isl_multi_pw_aff_copy(mpa1);
6700 mpa2 = isl_multi_pw_aff_copy(mpa2);
6701 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6702 isl_multi_pw_aff_get_space(mpa2));
6703 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6704 isl_multi_pw_aff_get_space(mpa1));
6705 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6706 isl_multi_pw_aff_free(mpa1);
6707 isl_multi_pw_aff_free(mpa2);
6708 return equal;
6711 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6712 if (equal < 0 || !equal)
6713 return equal;
6715 for (i = 0; i < mpa1->n; ++i) {
6716 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6717 if (equal < 0 || !equal)
6718 return equal;
6721 return isl_bool_true;
6724 /* Do "pma1" and "pma2" represent the same function?
6726 * First check if they are obviously equal.
6727 * If not, then convert them to maps and check if those are equal.
6729 * If "pa1" or "pa2" contain any NaNs, then they are considered
6730 * not to be the same. A NaN is not equal to anything, not even
6731 * to another NaN.
6733 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6734 __isl_keep isl_pw_multi_aff *pma2)
6736 isl_bool equal;
6737 isl_bool has_nan;
6738 isl_map *map1, *map2;
6740 if (!pma1 || !pma2)
6741 return isl_bool_error;
6743 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6744 if (equal < 0 || equal)
6745 return equal;
6746 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6747 if (has_nan >= 0 && !has_nan)
6748 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6749 if (has_nan < 0 || has_nan)
6750 return isl_bool_not(has_nan);
6752 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6753 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6754 equal = isl_map_is_equal(map1, map2);
6755 isl_map_free(map1);
6756 isl_map_free(map2);
6758 return equal;
6761 /* Compute the pullback of "mpa" by the function represented by "ma".
6762 * In other words, plug in "ma" in "mpa".
6764 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6766 * If "mpa" has an explicit domain, then it is this domain
6767 * that needs to undergo a pullback, i.e., a preimage.
6769 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6770 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6772 int i;
6773 isl_space *space = NULL;
6775 mpa = isl_multi_pw_aff_cow(mpa);
6776 if (!mpa || !ma)
6777 goto error;
6779 space = isl_space_join(isl_multi_aff_get_space(ma),
6780 isl_multi_pw_aff_get_space(mpa));
6781 if (!space)
6782 goto error;
6784 for (i = 0; i < mpa->n; ++i) {
6785 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6786 isl_multi_aff_copy(ma));
6787 if (!mpa->u.p[i])
6788 goto error;
6790 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6791 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6792 isl_multi_aff_copy(ma));
6793 if (!mpa->u.dom)
6794 goto error;
6797 isl_multi_aff_free(ma);
6798 isl_space_free(mpa->space);
6799 mpa->space = space;
6800 return mpa;
6801 error:
6802 isl_space_free(space);
6803 isl_multi_pw_aff_free(mpa);
6804 isl_multi_aff_free(ma);
6805 return NULL;
6808 /* Compute the pullback of "mpa" by the function represented by "ma".
6809 * In other words, plug in "ma" in "mpa".
6811 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6812 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6814 isl_bool equal_params;
6816 if (!mpa || !ma)
6817 goto error;
6818 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6819 if (equal_params < 0)
6820 goto error;
6821 if (equal_params)
6822 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6823 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6824 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6825 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6826 error:
6827 isl_multi_pw_aff_free(mpa);
6828 isl_multi_aff_free(ma);
6829 return NULL;
6832 /* Compute the pullback of "mpa" by the function represented by "pma".
6833 * In other words, plug in "pma" in "mpa".
6835 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6837 * If "mpa" has an explicit domain, then it is this domain
6838 * that needs to undergo a pullback, i.e., a preimage.
6840 static __isl_give isl_multi_pw_aff *
6841 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6842 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6844 int i;
6845 isl_space *space = NULL;
6847 mpa = isl_multi_pw_aff_cow(mpa);
6848 if (!mpa || !pma)
6849 goto error;
6851 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6852 isl_multi_pw_aff_get_space(mpa));
6854 for (i = 0; i < mpa->n; ++i) {
6855 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6856 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6857 if (!mpa->u.p[i])
6858 goto error;
6860 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6861 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6862 isl_pw_multi_aff_copy(pma));
6863 if (!mpa->u.dom)
6864 goto error;
6867 isl_pw_multi_aff_free(pma);
6868 isl_space_free(mpa->space);
6869 mpa->space = space;
6870 return mpa;
6871 error:
6872 isl_space_free(space);
6873 isl_multi_pw_aff_free(mpa);
6874 isl_pw_multi_aff_free(pma);
6875 return NULL;
6878 /* Compute the pullback of "mpa" by the function represented by "pma".
6879 * In other words, plug in "pma" in "mpa".
6881 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6882 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6884 isl_bool equal_params;
6886 if (!mpa || !pma)
6887 goto error;
6888 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6889 if (equal_params < 0)
6890 goto error;
6891 if (equal_params)
6892 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6893 mpa = isl_multi_pw_aff_align_params(mpa,
6894 isl_pw_multi_aff_get_space(pma));
6895 pma = isl_pw_multi_aff_align_params(pma,
6896 isl_multi_pw_aff_get_space(mpa));
6897 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6898 error:
6899 isl_multi_pw_aff_free(mpa);
6900 isl_pw_multi_aff_free(pma);
6901 return NULL;
6904 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6905 * with the domain of "aff". The domain of the result is the same
6906 * as that of "mpa".
6907 * "mpa" and "aff" are assumed to have been aligned.
6909 * We first extract the parametric constant from "aff", defined
6910 * over the correct domain.
6911 * Then we add the appropriate combinations of the members of "mpa".
6912 * Finally, we add the integer divisions through recursive calls.
6914 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6915 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6917 int i, n_in, n_div;
6918 isl_space *space;
6919 isl_val *v;
6920 isl_pw_aff *pa;
6921 isl_aff *tmp;
6923 n_in = isl_aff_dim(aff, isl_dim_in);
6924 n_div = isl_aff_dim(aff, isl_dim_div);
6926 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6927 tmp = isl_aff_copy(aff);
6928 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6929 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6930 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6931 isl_space_dim(space, isl_dim_set));
6932 tmp = isl_aff_reset_domain_space(tmp, space);
6933 pa = isl_pw_aff_from_aff(tmp);
6935 for (i = 0; i < n_in; ++i) {
6936 isl_pw_aff *pa_i;
6938 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6939 continue;
6940 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6941 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6942 pa_i = isl_pw_aff_scale_val(pa_i, v);
6943 pa = isl_pw_aff_add(pa, pa_i);
6946 for (i = 0; i < n_div; ++i) {
6947 isl_aff *div;
6948 isl_pw_aff *pa_i;
6950 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6951 continue;
6952 div = isl_aff_get_div(aff, i);
6953 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6954 isl_multi_pw_aff_copy(mpa), div);
6955 pa_i = isl_pw_aff_floor(pa_i);
6956 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6957 pa_i = isl_pw_aff_scale_val(pa_i, v);
6958 pa = isl_pw_aff_add(pa, pa_i);
6961 isl_multi_pw_aff_free(mpa);
6962 isl_aff_free(aff);
6964 return pa;
6967 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6968 * with the domain of "aff". The domain of the result is the same
6969 * as that of "mpa".
6971 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6972 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6974 isl_bool equal_params;
6976 if (!aff || !mpa)
6977 goto error;
6978 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6979 if (equal_params < 0)
6980 goto error;
6981 if (equal_params)
6982 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6984 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6985 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6987 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6988 error:
6989 isl_aff_free(aff);
6990 isl_multi_pw_aff_free(mpa);
6991 return NULL;
6994 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6995 * with the domain of "pa". The domain of the result is the same
6996 * as that of "mpa".
6997 * "mpa" and "pa" are assumed to have been aligned.
6999 * We consider each piece in turn. Note that the domains of the
7000 * pieces are assumed to be disjoint and they remain disjoint
7001 * after taking the preimage (over the same function).
7003 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7004 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7006 isl_space *space;
7007 isl_pw_aff *res;
7008 int i;
7010 if (!mpa || !pa)
7011 goto error;
7013 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7014 isl_pw_aff_get_space(pa));
7015 res = isl_pw_aff_empty(space);
7017 for (i = 0; i < pa->n; ++i) {
7018 isl_pw_aff *pa_i;
7019 isl_set *domain;
7021 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7022 isl_multi_pw_aff_copy(mpa),
7023 isl_aff_copy(pa->p[i].aff));
7024 domain = isl_set_copy(pa->p[i].set);
7025 domain = isl_set_preimage_multi_pw_aff(domain,
7026 isl_multi_pw_aff_copy(mpa));
7027 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7028 res = isl_pw_aff_add_disjoint(res, pa_i);
7031 isl_pw_aff_free(pa);
7032 isl_multi_pw_aff_free(mpa);
7033 return res;
7034 error:
7035 isl_pw_aff_free(pa);
7036 isl_multi_pw_aff_free(mpa);
7037 return NULL;
7040 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7041 * with the domain of "pa". The domain of the result is the same
7042 * as that of "mpa".
7044 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7045 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7047 isl_bool equal_params;
7049 if (!pa || !mpa)
7050 goto error;
7051 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7052 if (equal_params < 0)
7053 goto error;
7054 if (equal_params)
7055 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7057 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7058 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7060 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7061 error:
7062 isl_pw_aff_free(pa);
7063 isl_multi_pw_aff_free(mpa);
7064 return NULL;
7067 /* Compute the pullback of "pa" by the function represented by "mpa".
7068 * In other words, plug in "mpa" in "pa".
7069 * "pa" and "mpa" are assumed to have been aligned.
7071 * The pullback is computed by applying "pa" to "mpa".
7073 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7074 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7076 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7079 /* Compute the pullback of "pa" by the function represented by "mpa".
7080 * In other words, plug in "mpa" in "pa".
7082 * The pullback is computed by applying "pa" to "mpa".
7084 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7085 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7087 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7090 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7091 * In other words, plug in "mpa2" in "mpa1".
7093 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7095 * We pullback each member of "mpa1" in turn.
7097 * If "mpa1" has an explicit domain, then it is this domain
7098 * that needs to undergo a pullback instead, i.e., a preimage.
7100 static __isl_give isl_multi_pw_aff *
7101 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7102 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7104 int i;
7105 isl_space *space = NULL;
7107 mpa1 = isl_multi_pw_aff_cow(mpa1);
7108 if (!mpa1 || !mpa2)
7109 goto error;
7111 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7112 isl_multi_pw_aff_get_space(mpa1));
7114 for (i = 0; i < mpa1->n; ++i) {
7115 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7116 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7117 if (!mpa1->u.p[i])
7118 goto error;
7121 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7122 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7123 isl_multi_pw_aff_copy(mpa2));
7124 if (!mpa1->u.dom)
7125 goto error;
7127 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7129 isl_multi_pw_aff_free(mpa2);
7130 return mpa1;
7131 error:
7132 isl_space_free(space);
7133 isl_multi_pw_aff_free(mpa1);
7134 isl_multi_pw_aff_free(mpa2);
7135 return NULL;
7138 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7139 * In other words, plug in "mpa2" in "mpa1".
7141 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7142 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7144 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7145 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7148 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7149 * of "mpa1" and "mpa2" live in the same space, construct map space
7150 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7151 * with this map space as extract argument.
7153 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7154 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7155 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7156 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7158 int match;
7159 isl_space *space1, *space2;
7160 isl_map *res;
7162 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7163 isl_multi_pw_aff_get_space(mpa2));
7164 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7165 isl_multi_pw_aff_get_space(mpa1));
7166 if (!mpa1 || !mpa2)
7167 goto error;
7168 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7169 mpa2->space, isl_dim_out);
7170 if (match < 0)
7171 goto error;
7172 if (!match)
7173 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7174 "range spaces don't match", goto error);
7175 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7176 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7177 space1 = isl_space_map_from_domain_and_range(space1, space2);
7179 res = order(mpa1, mpa2, space1);
7180 isl_multi_pw_aff_free(mpa1);
7181 isl_multi_pw_aff_free(mpa2);
7182 return res;
7183 error:
7184 isl_multi_pw_aff_free(mpa1);
7185 isl_multi_pw_aff_free(mpa2);
7186 return NULL;
7189 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7190 * where the function values are equal. "space" is the space of the result.
7191 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7193 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7194 * in the sequences are equal.
7196 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7197 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7198 __isl_take isl_space *space)
7200 int i, n;
7201 isl_map *res;
7203 res = isl_map_universe(space);
7205 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7206 for (i = 0; i < n; ++i) {
7207 isl_pw_aff *pa1, *pa2;
7208 isl_map *map;
7210 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7211 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7212 map = isl_pw_aff_eq_map(pa1, pa2);
7213 res = isl_map_intersect(res, map);
7216 return res;
7219 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7220 * where the function values are equal.
7222 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7223 __isl_take isl_multi_pw_aff *mpa2)
7225 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7226 &isl_multi_pw_aff_eq_map_on_space);
7229 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7230 * where the function values of "mpa1" is lexicographically satisfies "base"
7231 * compared to that of "mpa2". "space" is the space of the result.
7232 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7234 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7235 * if its i-th element satisfies "base" when compared to
7236 * the i-th element of "mpa2" while all previous elements are
7237 * pairwise equal.
7239 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7240 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7241 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7242 __isl_take isl_pw_aff *pa2),
7243 __isl_take isl_space *space)
7245 int i, n;
7246 isl_map *res, *rest;
7248 res = isl_map_empty(isl_space_copy(space));
7249 rest = isl_map_universe(space);
7251 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7252 for (i = 0; i < n; ++i) {
7253 isl_pw_aff *pa1, *pa2;
7254 isl_map *map;
7256 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7257 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7258 map = base(pa1, pa2);
7259 map = isl_map_intersect(map, isl_map_copy(rest));
7260 res = isl_map_union(res, map);
7262 if (i == n - 1)
7263 continue;
7265 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7266 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7267 map = isl_pw_aff_eq_map(pa1, pa2);
7268 rest = isl_map_intersect(rest, map);
7271 isl_map_free(rest);
7272 return res;
7275 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7276 * where the function value of "mpa1" is lexicographically less than that
7277 * of "mpa2". "space" is the space of the result.
7278 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7280 * "mpa1" is less than "mpa2" if its i-th element is smaller
7281 * than the i-th element of "mpa2" while all previous elements are
7282 * pairwise equal.
7284 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7285 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7286 __isl_take isl_space *space)
7288 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7289 &isl_pw_aff_lt_map, space);
7292 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7293 * where the function value of "mpa1" is lexicographically less than that
7294 * of "mpa2".
7296 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7297 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7299 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7300 &isl_multi_pw_aff_lex_lt_map_on_space);
7303 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7304 * where the function value of "mpa1" is lexicographically greater than that
7305 * of "mpa2". "space" is the space of the result.
7306 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7308 * "mpa1" is greater than "mpa2" if its i-th element is greater
7309 * than the i-th element of "mpa2" while all previous elements are
7310 * pairwise equal.
7312 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7313 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7314 __isl_take isl_space *space)
7316 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7317 &isl_pw_aff_gt_map, space);
7320 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7321 * where the function value of "mpa1" is lexicographically greater than that
7322 * of "mpa2".
7324 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7325 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7327 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7328 &isl_multi_pw_aff_lex_gt_map_on_space);
7331 /* Compare two isl_affs.
7333 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7334 * than "aff2" and 0 if they are equal.
7336 * The order is fairly arbitrary. We do consider expressions that only involve
7337 * earlier dimensions as "smaller".
7339 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7341 int cmp;
7342 int last1, last2;
7344 if (aff1 == aff2)
7345 return 0;
7347 if (!aff1)
7348 return -1;
7349 if (!aff2)
7350 return 1;
7352 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7353 if (cmp != 0)
7354 return cmp;
7356 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7357 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7358 if (last1 != last2)
7359 return last1 - last2;
7361 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7364 /* Compare two isl_pw_affs.
7366 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7367 * than "pa2" and 0 if they are equal.
7369 * The order is fairly arbitrary. We do consider expressions that only involve
7370 * earlier dimensions as "smaller".
7372 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7373 __isl_keep isl_pw_aff *pa2)
7375 int i;
7376 int cmp;
7378 if (pa1 == pa2)
7379 return 0;
7381 if (!pa1)
7382 return -1;
7383 if (!pa2)
7384 return 1;
7386 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7387 if (cmp != 0)
7388 return cmp;
7390 if (pa1->n != pa2->n)
7391 return pa1->n - pa2->n;
7393 for (i = 0; i < pa1->n; ++i) {
7394 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7395 if (cmp != 0)
7396 return cmp;
7397 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7398 if (cmp != 0)
7399 return cmp;
7402 return 0;
7405 /* Return a piecewise affine expression that is equal to "v" on "domain".
7407 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7408 __isl_take isl_val *v)
7410 isl_space *space;
7411 isl_local_space *ls;
7412 isl_aff *aff;
7414 space = isl_set_get_space(domain);
7415 ls = isl_local_space_from_space(space);
7416 aff = isl_aff_val_on_domain(ls, v);
7418 return isl_pw_aff_alloc(domain, aff);
7421 /* Return a multi affine expression that is equal to "mv" on domain
7422 * space "space".
7424 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7425 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7427 int i, n;
7428 isl_space *space2;
7429 isl_local_space *ls;
7430 isl_multi_aff *ma;
7432 if (!space || !mv)
7433 goto error;
7435 n = isl_multi_val_dim(mv, isl_dim_set);
7436 space2 = isl_multi_val_get_space(mv);
7437 space2 = isl_space_align_params(space2, isl_space_copy(space));
7438 space = isl_space_align_params(space, isl_space_copy(space2));
7439 space = isl_space_map_from_domain_and_range(space, space2);
7440 ma = isl_multi_aff_alloc(isl_space_copy(space));
7441 ls = isl_local_space_from_space(isl_space_domain(space));
7442 for (i = 0; i < n; ++i) {
7443 isl_val *v;
7444 isl_aff *aff;
7446 v = isl_multi_val_get_val(mv, i);
7447 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7448 ma = isl_multi_aff_set_aff(ma, i, aff);
7450 isl_local_space_free(ls);
7452 isl_multi_val_free(mv);
7453 return ma;
7454 error:
7455 isl_space_free(space);
7456 isl_multi_val_free(mv);
7457 return NULL;
7460 /* Return a piecewise multi-affine expression
7461 * that is equal to "mv" on "domain".
7463 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7464 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7466 isl_space *space;
7467 isl_multi_aff *ma;
7469 space = isl_set_get_space(domain);
7470 ma = isl_multi_aff_multi_val_on_space(space, mv);
7472 return isl_pw_multi_aff_alloc(domain, ma);
7475 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7476 * mv is the value that should be attained on each domain set
7477 * res collects the results
7479 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7480 isl_multi_val *mv;
7481 isl_union_pw_multi_aff *res;
7484 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7485 * and add it to data->res.
7487 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7488 void *user)
7490 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7491 isl_pw_multi_aff *pma;
7492 isl_multi_val *mv;
7494 mv = isl_multi_val_copy(data->mv);
7495 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7496 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7498 return data->res ? isl_stat_ok : isl_stat_error;
7501 /* Return a union piecewise multi-affine expression
7502 * that is equal to "mv" on "domain".
7504 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7505 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7507 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7508 isl_space *space;
7510 space = isl_union_set_get_space(domain);
7511 data.res = isl_union_pw_multi_aff_empty(space);
7512 data.mv = mv;
7513 if (isl_union_set_foreach_set(domain,
7514 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7515 data.res = isl_union_pw_multi_aff_free(data.res);
7516 isl_union_set_free(domain);
7517 isl_multi_val_free(mv);
7518 return data.res;
7521 /* Compute the pullback of data->pma by the function represented by "pma2",
7522 * provided the spaces match, and add the results to data->res.
7524 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7526 struct isl_union_pw_multi_aff_bin_data *data = user;
7528 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7529 pma2->dim, isl_dim_out)) {
7530 isl_pw_multi_aff_free(pma2);
7531 return isl_stat_ok;
7534 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7535 isl_pw_multi_aff_copy(data->pma), pma2);
7537 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7538 if (!data->res)
7539 return isl_stat_error;
7541 return isl_stat_ok;
7544 /* Compute the pullback of "upma1" by the function represented by "upma2".
7546 __isl_give isl_union_pw_multi_aff *
7547 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7548 __isl_take isl_union_pw_multi_aff *upma1,
7549 __isl_take isl_union_pw_multi_aff *upma2)
7551 return bin_op(upma1, upma2, &pullback_entry);
7554 /* Check that the domain space of "upa" matches "space".
7556 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7557 * can in principle never fail since the space "space" is that
7558 * of the isl_multi_union_pw_aff and is a set space such that
7559 * there is no domain space to match.
7561 * We check the parameters and double-check that "space" is
7562 * indeed that of a set.
7564 static isl_stat isl_union_pw_aff_check_match_domain_space(
7565 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7567 isl_space *upa_space;
7568 isl_bool match;
7570 if (!upa || !space)
7571 return isl_stat_error;
7573 match = isl_space_is_set(space);
7574 if (match < 0)
7575 return isl_stat_error;
7576 if (!match)
7577 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7578 "expecting set space", return -1);
7580 upa_space = isl_union_pw_aff_get_space(upa);
7581 match = isl_space_has_equal_params(space, upa_space);
7582 if (match < 0)
7583 goto error;
7584 if (!match)
7585 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7586 "parameters don't match", goto error);
7588 isl_space_free(upa_space);
7589 return isl_stat_ok;
7590 error:
7591 isl_space_free(upa_space);
7592 return isl_stat_error;
7595 /* Do the parameters of "upa" match those of "space"?
7597 static isl_bool isl_union_pw_aff_matching_params(
7598 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7600 isl_space *upa_space;
7601 isl_bool match;
7603 if (!upa || !space)
7604 return isl_bool_error;
7606 upa_space = isl_union_pw_aff_get_space(upa);
7608 match = isl_space_has_equal_params(space, upa_space);
7610 isl_space_free(upa_space);
7611 return match;
7614 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7615 * space represents the new parameters.
7616 * res collects the results.
7618 struct isl_union_pw_aff_reset_params_data {
7619 isl_space *space;
7620 isl_union_pw_aff *res;
7623 /* Replace the parameters of "pa" by data->space and
7624 * add the result to data->res.
7626 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7628 struct isl_union_pw_aff_reset_params_data *data = user;
7629 isl_space *space;
7631 space = isl_pw_aff_get_space(pa);
7632 space = isl_space_replace_params(space, data->space);
7633 pa = isl_pw_aff_reset_space(pa, space);
7634 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7636 return data->res ? isl_stat_ok : isl_stat_error;
7639 /* Replace the domain space of "upa" by "space".
7640 * Since a union expression does not have a (single) domain space,
7641 * "space" is necessarily a parameter space.
7643 * Since the order and the names of the parameters determine
7644 * the hash value, we need to create a new hash table.
7646 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7647 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7649 struct isl_union_pw_aff_reset_params_data data = { space };
7650 isl_bool match;
7652 match = isl_union_pw_aff_matching_params(upa, space);
7653 if (match < 0)
7654 upa = isl_union_pw_aff_free(upa);
7655 else if (match) {
7656 isl_space_free(space);
7657 return upa;
7660 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7661 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7662 data.res = isl_union_pw_aff_free(data.res);
7664 isl_union_pw_aff_free(upa);
7665 isl_space_free(space);
7666 return data.res;
7669 /* Return the floor of "pa".
7671 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7673 return isl_pw_aff_floor(pa);
7676 /* Given f, return floor(f).
7678 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7679 __isl_take isl_union_pw_aff *upa)
7681 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7684 /* Compute
7686 * upa mod m = upa - m * floor(upa/m)
7688 * with m an integer value.
7690 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7691 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7693 isl_union_pw_aff *res;
7695 if (!upa || !m)
7696 goto error;
7698 if (!isl_val_is_int(m))
7699 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7700 "expecting integer modulo", goto error);
7701 if (!isl_val_is_pos(m))
7702 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7703 "expecting positive modulo", goto error);
7705 res = isl_union_pw_aff_copy(upa);
7706 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7707 upa = isl_union_pw_aff_floor(upa);
7708 upa = isl_union_pw_aff_scale_val(upa, m);
7709 res = isl_union_pw_aff_sub(res, upa);
7711 return res;
7712 error:
7713 isl_val_free(m);
7714 isl_union_pw_aff_free(upa);
7715 return NULL;
7718 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7719 * pos is the output position that needs to be extracted.
7720 * res collects the results.
7722 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7723 int pos;
7724 isl_union_pw_aff *res;
7727 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7728 * (assuming it has such a dimension) and add it to data->res.
7730 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7732 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7733 int n_out;
7734 isl_pw_aff *pa;
7736 if (!pma)
7737 return isl_stat_error;
7739 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7740 if (data->pos >= n_out) {
7741 isl_pw_multi_aff_free(pma);
7742 return isl_stat_ok;
7745 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7746 isl_pw_multi_aff_free(pma);
7748 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7750 return data->res ? isl_stat_ok : isl_stat_error;
7753 /* Extract an isl_union_pw_aff corresponding to
7754 * output dimension "pos" of "upma".
7756 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7757 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7759 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7760 isl_space *space;
7762 if (!upma)
7763 return NULL;
7765 if (pos < 0)
7766 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7767 "cannot extract at negative position", return NULL);
7769 space = isl_union_pw_multi_aff_get_space(upma);
7770 data.res = isl_union_pw_aff_empty(space);
7771 data.pos = pos;
7772 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7773 &get_union_pw_aff, &data) < 0)
7774 data.res = isl_union_pw_aff_free(data.res);
7776 return data.res;
7779 /* Return a union piecewise affine expression
7780 * that is equal to "aff" on "domain".
7782 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7783 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7785 isl_pw_aff *pa;
7787 pa = isl_pw_aff_from_aff(aff);
7788 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7791 /* Return a union piecewise affine expression
7792 * that is equal to the parameter identified by "id" on "domain".
7794 * Make sure the parameter appears in the space passed to
7795 * isl_aff_param_on_domain_space_id.
7797 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7798 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7800 isl_space *space;
7801 isl_aff *aff;
7803 space = isl_union_set_get_space(domain);
7804 space = isl_space_add_param_id(space, isl_id_copy(id));
7805 aff = isl_aff_param_on_domain_space_id(space, id);
7806 return isl_union_pw_aff_aff_on_domain(domain, aff);
7809 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7810 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7811 * needs to attain.
7812 * "res" collects the results.
7814 struct isl_union_pw_aff_pw_aff_on_domain_data {
7815 isl_pw_aff *pa;
7816 isl_union_pw_aff *res;
7819 /* Construct a piecewise affine expression that is equal to data->pa
7820 * on "domain" and add the result to data->res.
7822 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7824 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7825 isl_pw_aff *pa;
7826 int dim;
7828 pa = isl_pw_aff_copy(data->pa);
7829 dim = isl_set_dim(domain, isl_dim_set);
7830 pa = isl_pw_aff_from_range(pa);
7831 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7832 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7833 pa = isl_pw_aff_intersect_domain(pa, domain);
7834 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7836 return data->res ? isl_stat_ok : isl_stat_error;
7839 /* Return a union piecewise affine expression
7840 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7841 * have been aligned.
7843 * Construct an isl_pw_aff on each of the sets in "domain" and
7844 * collect the results.
7846 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7847 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7849 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7850 isl_space *space;
7852 space = isl_union_set_get_space(domain);
7853 data.res = isl_union_pw_aff_empty(space);
7854 data.pa = pa;
7855 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7856 data.res = isl_union_pw_aff_free(data.res);
7857 isl_union_set_free(domain);
7858 isl_pw_aff_free(pa);
7859 return data.res;
7862 /* Return a union piecewise affine expression
7863 * that is equal to "pa" on "domain".
7865 * Check that "pa" is a parametric expression,
7866 * align the parameters if needed and call
7867 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7869 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7870 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7872 isl_bool is_set;
7873 isl_bool equal_params;
7874 isl_space *domain_space, *pa_space;
7876 pa_space = isl_pw_aff_peek_space(pa);
7877 is_set = isl_space_is_set(pa_space);
7878 if (is_set < 0)
7879 goto error;
7880 if (!is_set)
7881 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7882 "expecting parametric expression", goto error);
7884 domain_space = isl_union_set_get_space(domain);
7885 pa_space = isl_pw_aff_get_space(pa);
7886 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7887 if (equal_params >= 0 && !equal_params) {
7888 isl_space *space;
7890 space = isl_space_align_params(domain_space, pa_space);
7891 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7892 domain = isl_union_set_align_params(domain, space);
7893 } else {
7894 isl_space_free(domain_space);
7895 isl_space_free(pa_space);
7898 if (equal_params < 0)
7899 goto error;
7900 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7901 error:
7902 isl_union_set_free(domain);
7903 isl_pw_aff_free(pa);
7904 return NULL;
7907 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7908 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7909 * "res" collects the results.
7911 struct isl_union_pw_aff_val_on_domain_data {
7912 isl_val *v;
7913 isl_union_pw_aff *res;
7916 /* Construct a piecewise affine expression that is equal to data->v
7917 * on "domain" and add the result to data->res.
7919 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7921 struct isl_union_pw_aff_val_on_domain_data *data = user;
7922 isl_pw_aff *pa;
7923 isl_val *v;
7925 v = isl_val_copy(data->v);
7926 pa = isl_pw_aff_val_on_domain(domain, v);
7927 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7929 return data->res ? isl_stat_ok : isl_stat_error;
7932 /* Return a union piecewise affine expression
7933 * that is equal to "v" on "domain".
7935 * Construct an isl_pw_aff on each of the sets in "domain" and
7936 * collect the results.
7938 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7939 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7941 struct isl_union_pw_aff_val_on_domain_data data;
7942 isl_space *space;
7944 space = isl_union_set_get_space(domain);
7945 data.res = isl_union_pw_aff_empty(space);
7946 data.v = v;
7947 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7948 data.res = isl_union_pw_aff_free(data.res);
7949 isl_union_set_free(domain);
7950 isl_val_free(v);
7951 return data.res;
7954 /* Construct a piecewise multi affine expression
7955 * that is equal to "pa" and add it to upma.
7957 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7958 void *user)
7960 isl_union_pw_multi_aff **upma = user;
7961 isl_pw_multi_aff *pma;
7963 pma = isl_pw_multi_aff_from_pw_aff(pa);
7964 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7966 return *upma ? isl_stat_ok : isl_stat_error;
7969 /* Construct and return a union piecewise multi affine expression
7970 * that is equal to the given union piecewise affine expression.
7972 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7973 __isl_take isl_union_pw_aff *upa)
7975 isl_space *space;
7976 isl_union_pw_multi_aff *upma;
7978 if (!upa)
7979 return NULL;
7981 space = isl_union_pw_aff_get_space(upa);
7982 upma = isl_union_pw_multi_aff_empty(space);
7984 if (isl_union_pw_aff_foreach_pw_aff(upa,
7985 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7986 upma = isl_union_pw_multi_aff_free(upma);
7988 isl_union_pw_aff_free(upa);
7989 return upma;
7992 /* Compute the set of elements in the domain of "pa" where it is zero and
7993 * add this set to "uset".
7995 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7997 isl_union_set **uset = (isl_union_set **)user;
7999 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8001 return *uset ? isl_stat_ok : isl_stat_error;
8004 /* Return a union set containing those elements in the domain
8005 * of "upa" where it is zero.
8007 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8008 __isl_take isl_union_pw_aff *upa)
8010 isl_union_set *zero;
8012 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8013 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8014 zero = isl_union_set_free(zero);
8016 isl_union_pw_aff_free(upa);
8017 return zero;
8020 /* Convert "pa" to an isl_map and add it to *umap.
8022 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8024 isl_union_map **umap = user;
8025 isl_map *map;
8027 map = isl_map_from_pw_aff(pa);
8028 *umap = isl_union_map_add_map(*umap, map);
8030 return *umap ? isl_stat_ok : isl_stat_error;
8033 /* Construct a union map mapping the domain of the union
8034 * piecewise affine expression to its range, with the single output dimension
8035 * equated to the corresponding affine expressions on their cells.
8037 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8038 __isl_take isl_union_pw_aff *upa)
8040 isl_space *space;
8041 isl_union_map *umap;
8043 if (!upa)
8044 return NULL;
8046 space = isl_union_pw_aff_get_space(upa);
8047 umap = isl_union_map_empty(space);
8049 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8050 &umap) < 0)
8051 umap = isl_union_map_free(umap);
8053 isl_union_pw_aff_free(upa);
8054 return umap;
8057 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8058 * upma is the function that is plugged in.
8059 * pa is the current part of the function in which upma is plugged in.
8060 * res collects the results.
8062 struct isl_union_pw_aff_pullback_upma_data {
8063 isl_union_pw_multi_aff *upma;
8064 isl_pw_aff *pa;
8065 isl_union_pw_aff *res;
8068 /* Check if "pma" can be plugged into data->pa.
8069 * If so, perform the pullback and add the result to data->res.
8071 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8073 struct isl_union_pw_aff_pullback_upma_data *data = user;
8074 isl_pw_aff *pa;
8076 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8077 pma->dim, isl_dim_out)) {
8078 isl_pw_multi_aff_free(pma);
8079 return isl_stat_ok;
8082 pa = isl_pw_aff_copy(data->pa);
8083 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8085 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8087 return data->res ? isl_stat_ok : isl_stat_error;
8090 /* Check if any of the elements of data->upma can be plugged into pa,
8091 * add if so add the result to data->res.
8093 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8095 struct isl_union_pw_aff_pullback_upma_data *data = user;
8096 isl_stat r;
8098 data->pa = pa;
8099 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8100 &pa_pb_pma, data);
8101 isl_pw_aff_free(pa);
8103 return r;
8106 /* Compute the pullback of "upa" by the function represented by "upma".
8107 * In other words, plug in "upma" in "upa". The result contains
8108 * expressions defined over the domain space of "upma".
8110 * Run over all pairs of elements in "upa" and "upma", perform
8111 * the pullback when appropriate and collect the results.
8112 * If the hash value were based on the domain space rather than
8113 * the function space, then we could run through all elements
8114 * of "upma" and directly pick out the corresponding element of "upa".
8116 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8117 __isl_take isl_union_pw_aff *upa,
8118 __isl_take isl_union_pw_multi_aff *upma)
8120 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8121 isl_space *space;
8123 space = isl_union_pw_multi_aff_get_space(upma);
8124 upa = isl_union_pw_aff_align_params(upa, space);
8125 space = isl_union_pw_aff_get_space(upa);
8126 upma = isl_union_pw_multi_aff_align_params(upma, space);
8128 if (!upa || !upma)
8129 goto error;
8131 data.upma = upma;
8132 data.res = isl_union_pw_aff_alloc_same_size(upa);
8133 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8134 data.res = isl_union_pw_aff_free(data.res);
8136 isl_union_pw_aff_free(upa);
8137 isl_union_pw_multi_aff_free(upma);
8138 return data.res;
8139 error:
8140 isl_union_pw_aff_free(upa);
8141 isl_union_pw_multi_aff_free(upma);
8142 return NULL;
8145 #undef BASE
8146 #define BASE union_pw_aff
8147 #undef DOMBASE
8148 #define DOMBASE union_set
8150 #define NO_MOVE_DIMS
8151 #define NO_DOMAIN
8152 #define NO_PRODUCT
8153 #define NO_SPLICE
8154 #define NO_ZERO
8155 #define NO_IDENTITY
8156 #define NO_GIST
8158 #include <isl_multi_explicit_domain.c>
8159 #include <isl_multi_union_pw_aff_explicit_domain.c>
8160 #include <isl_multi_templ.c>
8161 #include <isl_multi_apply_set.c>
8162 #include <isl_multi_apply_union_set.c>
8163 #include <isl_multi_coalesce.c>
8164 #include <isl_multi_floor.c>
8165 #include <isl_multi_gist.c>
8166 #include <isl_multi_align_set.c>
8167 #include <isl_multi_align_union_set.c>
8168 #include <isl_multi_intersect.c>
8170 /* Does "mupa" have a non-trivial explicit domain?
8172 * The explicit domain, if present, is trivial if it represents
8173 * an (obviously) universe parameter set.
8175 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8176 __isl_keep isl_multi_union_pw_aff *mupa)
8178 isl_bool is_params, trivial;
8179 isl_set *set;
8181 if (!mupa)
8182 return isl_bool_error;
8183 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8184 return isl_bool_false;
8185 is_params = isl_union_set_is_params(mupa->u.dom);
8186 if (is_params < 0 || !is_params)
8187 return isl_bool_not(is_params);
8188 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8189 trivial = isl_set_plain_is_universe(set);
8190 isl_set_free(set);
8191 return isl_bool_not(trivial);
8194 /* Construct a multiple union piecewise affine expression
8195 * in the given space with value zero in each of the output dimensions.
8197 * Since there is no canonical zero value for
8198 * a union piecewise affine expression, we can only construct
8199 * a zero-dimensional "zero" value.
8201 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8202 __isl_take isl_space *space)
8204 isl_bool params;
8206 if (!space)
8207 return NULL;
8209 params = isl_space_is_params(space);
8210 if (params < 0)
8211 goto error;
8212 if (params)
8213 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8214 "expecting proper set space", goto error);
8215 if (!isl_space_is_set(space))
8216 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8217 "expecting set space", goto error);
8218 if (isl_space_dim(space , isl_dim_out) != 0)
8219 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8220 "expecting 0D space", goto error);
8222 return isl_multi_union_pw_aff_alloc(space);
8223 error:
8224 isl_space_free(space);
8225 return NULL;
8228 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8229 * with the actual sum on the shared domain and
8230 * the defined expression on the symmetric difference of the domains.
8232 * We simply iterate over the elements in both arguments and
8233 * call isl_union_pw_aff_union_add on each of them, if there is
8234 * at least one element.
8236 * Otherwise, the two expressions have an explicit domain and
8237 * the union of these explicit domains is computed.
8238 * This assumes that the explicit domains are either both in terms
8239 * of specific domains elements or both in terms of parameters.
8240 * However, if one of the expressions does not have any constraints
8241 * on its explicit domain, then this is allowed as well and the result
8242 * is the expression with no constraints on its explicit domain.
8244 static __isl_give isl_multi_union_pw_aff *
8245 isl_multi_union_pw_aff_union_add_aligned(
8246 __isl_take isl_multi_union_pw_aff *mupa1,
8247 __isl_take isl_multi_union_pw_aff *mupa2)
8249 isl_bool has_domain, is_params1, is_params2;
8251 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8252 goto error;
8253 if (mupa1->n > 0)
8254 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8255 &isl_union_pw_aff_union_add);
8256 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8257 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8258 goto error;
8260 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8261 if (has_domain < 0)
8262 goto error;
8263 if (!has_domain) {
8264 isl_multi_union_pw_aff_free(mupa2);
8265 return mupa1;
8267 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8268 if (has_domain < 0)
8269 goto error;
8270 if (!has_domain) {
8271 isl_multi_union_pw_aff_free(mupa1);
8272 return mupa2;
8275 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8276 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8277 if (is_params1 < 0 || is_params2 < 0)
8278 goto error;
8279 if (is_params1 != is_params2)
8280 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8281 isl_error_invalid,
8282 "cannot compute union of concrete domain and "
8283 "parameter constraints", goto error);
8284 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8285 if (!mupa1)
8286 goto error;
8287 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8288 isl_union_set_copy(mupa2->u.dom));
8289 if (!mupa1->u.dom)
8290 goto error;
8291 isl_multi_union_pw_aff_free(mupa2);
8292 return mupa1;
8293 error:
8294 isl_multi_union_pw_aff_free(mupa1);
8295 isl_multi_union_pw_aff_free(mupa2);
8296 return NULL;
8299 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8300 * with the actual sum on the shared domain and
8301 * the defined expression on the symmetric difference of the domains.
8303 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8304 __isl_take isl_multi_union_pw_aff *mupa1,
8305 __isl_take isl_multi_union_pw_aff *mupa2)
8307 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8308 &isl_multi_union_pw_aff_union_add_aligned);
8311 /* Construct and return a multi union piecewise affine expression
8312 * that is equal to the given multi affine expression.
8314 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8315 __isl_take isl_multi_aff *ma)
8317 isl_multi_pw_aff *mpa;
8319 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8320 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8323 /* Construct and return a multi union piecewise affine expression
8324 * that is equal to the given multi piecewise affine expression.
8326 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8327 __isl_take isl_multi_pw_aff *mpa)
8329 int i, n;
8330 isl_space *space;
8331 isl_multi_union_pw_aff *mupa;
8333 if (!mpa)
8334 return NULL;
8336 space = isl_multi_pw_aff_get_space(mpa);
8337 space = isl_space_range(space);
8338 mupa = isl_multi_union_pw_aff_alloc(space);
8340 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8341 for (i = 0; i < n; ++i) {
8342 isl_pw_aff *pa;
8343 isl_union_pw_aff *upa;
8345 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8346 upa = isl_union_pw_aff_from_pw_aff(pa);
8347 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8350 isl_multi_pw_aff_free(mpa);
8352 return mupa;
8355 /* Extract the range space of "pma" and assign it to *space.
8356 * If *space has already been set (through a previous call to this function),
8357 * then check that the range space is the same.
8359 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8361 isl_space **space = user;
8362 isl_space *pma_space;
8363 isl_bool equal;
8365 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8366 isl_pw_multi_aff_free(pma);
8368 if (!pma_space)
8369 return isl_stat_error;
8370 if (!*space) {
8371 *space = pma_space;
8372 return isl_stat_ok;
8375 equal = isl_space_is_equal(pma_space, *space);
8376 isl_space_free(pma_space);
8378 if (equal < 0)
8379 return isl_stat_error;
8380 if (!equal)
8381 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8382 "range spaces not the same", return isl_stat_error);
8383 return isl_stat_ok;
8386 /* Construct and return a multi union piecewise affine expression
8387 * that is equal to the given union piecewise multi affine expression.
8389 * In order to be able to perform the conversion, the input
8390 * needs to be non-empty and may only involve a single range space.
8392 * If the resulting multi union piecewise affine expression has
8393 * an explicit domain, then assign it the domain of the input.
8394 * In other cases, the domain is stored in the individual elements.
8396 __isl_give isl_multi_union_pw_aff *
8397 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8398 __isl_take isl_union_pw_multi_aff *upma)
8400 isl_space *space = NULL;
8401 isl_multi_union_pw_aff *mupa;
8402 int i, n;
8404 if (!upma)
8405 return NULL;
8406 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8407 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8408 "cannot extract range space from empty input",
8409 goto error);
8410 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8411 &space) < 0)
8412 goto error;
8414 if (!space)
8415 goto error;
8417 n = isl_space_dim(space, isl_dim_set);
8418 mupa = isl_multi_union_pw_aff_alloc(space);
8420 for (i = 0; i < n; ++i) {
8421 isl_union_pw_aff *upa;
8423 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8424 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8426 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8427 isl_union_set *dom;
8428 isl_union_pw_multi_aff *copy;
8430 copy = isl_union_pw_multi_aff_copy(upma);
8431 dom = isl_union_pw_multi_aff_domain(copy);
8432 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8435 isl_union_pw_multi_aff_free(upma);
8436 return mupa;
8437 error:
8438 isl_space_free(space);
8439 isl_union_pw_multi_aff_free(upma);
8440 return NULL;
8443 /* Try and create an isl_multi_union_pw_aff that is equivalent
8444 * to the given isl_union_map.
8445 * The isl_union_map is required to be single-valued in each space.
8446 * Moreover, it cannot be empty and all range spaces need to be the same.
8447 * Otherwise, an error is produced.
8449 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8450 __isl_take isl_union_map *umap)
8452 isl_union_pw_multi_aff *upma;
8454 upma = isl_union_pw_multi_aff_from_union_map(umap);
8455 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8458 /* Return a multiple union piecewise affine expression
8459 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8460 * have been aligned.
8462 static __isl_give isl_multi_union_pw_aff *
8463 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8464 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8466 int i, n;
8467 isl_space *space;
8468 isl_multi_union_pw_aff *mupa;
8470 if (!domain || !mv)
8471 goto error;
8473 n = isl_multi_val_dim(mv, isl_dim_set);
8474 space = isl_multi_val_get_space(mv);
8475 mupa = isl_multi_union_pw_aff_alloc(space);
8476 for (i = 0; i < n; ++i) {
8477 isl_val *v;
8478 isl_union_pw_aff *upa;
8480 v = isl_multi_val_get_val(mv, i);
8481 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8483 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8486 isl_union_set_free(domain);
8487 isl_multi_val_free(mv);
8488 return mupa;
8489 error:
8490 isl_union_set_free(domain);
8491 isl_multi_val_free(mv);
8492 return NULL;
8495 /* Return a multiple union piecewise affine expression
8496 * that is equal to "mv" on "domain".
8498 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8499 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8501 isl_bool equal_params;
8503 if (!domain || !mv)
8504 goto error;
8505 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8506 if (equal_params < 0)
8507 goto error;
8508 if (equal_params)
8509 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8510 domain, mv);
8511 domain = isl_union_set_align_params(domain,
8512 isl_multi_val_get_space(mv));
8513 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8514 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8515 error:
8516 isl_union_set_free(domain);
8517 isl_multi_val_free(mv);
8518 return NULL;
8521 /* Return a multiple union piecewise affine expression
8522 * that is equal to "ma" on "domain".
8524 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8525 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8527 isl_pw_multi_aff *pma;
8529 pma = isl_pw_multi_aff_from_multi_aff(ma);
8530 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8533 /* Return a multiple union piecewise affine expression
8534 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8535 * have been aligned.
8537 * If the resulting multi union piecewise affine expression has
8538 * an explicit domain, then assign it the input domain.
8539 * In other cases, the domain is stored in the individual elements.
8541 static __isl_give isl_multi_union_pw_aff *
8542 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8543 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8545 int i, n;
8546 isl_space *space;
8547 isl_multi_union_pw_aff *mupa;
8549 if (!domain || !pma)
8550 goto error;
8552 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8553 space = isl_pw_multi_aff_get_space(pma);
8554 mupa = isl_multi_union_pw_aff_alloc(space);
8555 for (i = 0; i < n; ++i) {
8556 isl_pw_aff *pa;
8557 isl_union_pw_aff *upa;
8559 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8560 upa = isl_union_pw_aff_pw_aff_on_domain(
8561 isl_union_set_copy(domain), pa);
8562 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8564 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8565 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8566 isl_union_set_copy(domain));
8568 isl_union_set_free(domain);
8569 isl_pw_multi_aff_free(pma);
8570 return mupa;
8571 error:
8572 isl_union_set_free(domain);
8573 isl_pw_multi_aff_free(pma);
8574 return NULL;
8577 /* Return a multiple union piecewise affine expression
8578 * that is equal to "pma" on "domain".
8580 __isl_give isl_multi_union_pw_aff *
8581 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8582 __isl_take isl_pw_multi_aff *pma)
8584 isl_bool equal_params;
8585 isl_space *space;
8587 space = isl_pw_multi_aff_peek_space(pma);
8588 equal_params = isl_union_set_space_has_equal_params(domain, space);
8589 if (equal_params < 0)
8590 goto error;
8591 if (equal_params)
8592 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8593 domain, pma);
8594 domain = isl_union_set_align_params(domain,
8595 isl_pw_multi_aff_get_space(pma));
8596 pma = isl_pw_multi_aff_align_params(pma,
8597 isl_union_set_get_space(domain));
8598 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8599 pma);
8600 error:
8601 isl_union_set_free(domain);
8602 isl_pw_multi_aff_free(pma);
8603 return NULL;
8606 /* Return a union set containing those elements in the domains
8607 * of the elements of "mupa" where they are all zero.
8609 * If there are no elements, then simply return the entire domain.
8611 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8612 __isl_take isl_multi_union_pw_aff *mupa)
8614 int i, n;
8615 isl_union_pw_aff *upa;
8616 isl_union_set *zero;
8618 if (!mupa)
8619 return NULL;
8621 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8622 if (n == 0)
8623 return isl_multi_union_pw_aff_domain(mupa);
8625 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8626 zero = isl_union_pw_aff_zero_union_set(upa);
8628 for (i = 1; i < n; ++i) {
8629 isl_union_set *zero_i;
8631 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8632 zero_i = isl_union_pw_aff_zero_union_set(upa);
8634 zero = isl_union_set_intersect(zero, zero_i);
8637 isl_multi_union_pw_aff_free(mupa);
8638 return zero;
8641 /* Construct a union map mapping the shared domain
8642 * of the union piecewise affine expressions to the range of "mupa"
8643 * in the special case of a 0D multi union piecewise affine expression.
8645 * Construct a map between the explicit domain of "mupa" and
8646 * the range space.
8647 * Note that this assumes that the domain consists of explicit elements.
8649 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8650 __isl_take isl_multi_union_pw_aff *mupa)
8652 isl_bool is_params;
8653 isl_space *space;
8654 isl_union_set *dom, *ran;
8656 space = isl_multi_union_pw_aff_get_space(mupa);
8657 dom = isl_multi_union_pw_aff_domain(mupa);
8658 ran = isl_union_set_from_set(isl_set_universe(space));
8660 is_params = isl_union_set_is_params(dom);
8661 if (is_params < 0)
8662 dom = isl_union_set_free(dom);
8663 else if (is_params)
8664 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8665 "cannot create union map from expression without "
8666 "explicit domain elements",
8667 dom = isl_union_set_free(dom));
8669 return isl_union_map_from_domain_and_range(dom, ran);
8672 /* Construct a union map mapping the shared domain
8673 * of the union piecewise affine expressions to the range of "mupa"
8674 * with each dimension in the range equated to the
8675 * corresponding union piecewise affine expression.
8677 * If the input is zero-dimensional, then construct a mapping
8678 * from its explicit domain.
8680 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8681 __isl_take isl_multi_union_pw_aff *mupa)
8683 int i, n;
8684 isl_space *space;
8685 isl_union_map *umap;
8686 isl_union_pw_aff *upa;
8688 if (!mupa)
8689 return NULL;
8691 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8692 if (n == 0)
8693 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8695 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8696 umap = isl_union_map_from_union_pw_aff(upa);
8698 for (i = 1; i < n; ++i) {
8699 isl_union_map *umap_i;
8701 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8702 umap_i = isl_union_map_from_union_pw_aff(upa);
8703 umap = isl_union_map_flat_range_product(umap, umap_i);
8706 space = isl_multi_union_pw_aff_get_space(mupa);
8707 umap = isl_union_map_reset_range_space(umap, space);
8709 isl_multi_union_pw_aff_free(mupa);
8710 return umap;
8713 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8714 * "range" is the space from which to set the range space.
8715 * "res" collects the results.
8717 struct isl_union_pw_multi_aff_reset_range_space_data {
8718 isl_space *range;
8719 isl_union_pw_multi_aff *res;
8722 /* Replace the range space of "pma" by the range space of data->range and
8723 * add the result to data->res.
8725 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8727 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8728 isl_space *space;
8730 space = isl_pw_multi_aff_get_space(pma);
8731 space = isl_space_domain(space);
8732 space = isl_space_extend_domain_with_range(space,
8733 isl_space_copy(data->range));
8734 pma = isl_pw_multi_aff_reset_space(pma, space);
8735 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8737 return data->res ? isl_stat_ok : isl_stat_error;
8740 /* Replace the range space of all the piecewise affine expressions in "upma" by
8741 * the range space of "space".
8743 * This assumes that all these expressions have the same output dimension.
8745 * Since the spaces of the expressions change, so do their hash values.
8746 * We therefore need to create a new isl_union_pw_multi_aff.
8747 * Note that the hash value is currently computed based on the entire
8748 * space even though there can only be a single expression with a given
8749 * domain space.
8751 static __isl_give isl_union_pw_multi_aff *
8752 isl_union_pw_multi_aff_reset_range_space(
8753 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8755 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8756 isl_space *space_upma;
8758 space_upma = isl_union_pw_multi_aff_get_space(upma);
8759 data.res = isl_union_pw_multi_aff_empty(space_upma);
8760 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8761 &reset_range_space, &data) < 0)
8762 data.res = isl_union_pw_multi_aff_free(data.res);
8764 isl_space_free(space);
8765 isl_union_pw_multi_aff_free(upma);
8766 return data.res;
8769 /* Construct and return a union piecewise multi affine expression
8770 * that is equal to the given multi union piecewise affine expression,
8771 * in the special case of a 0D multi union piecewise affine expression.
8773 * Construct a union piecewise multi affine expression
8774 * on top of the explicit domain of the input.
8776 __isl_give isl_union_pw_multi_aff *
8777 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8778 __isl_take isl_multi_union_pw_aff *mupa)
8780 isl_space *space;
8781 isl_multi_val *mv;
8782 isl_union_set *domain;
8784 space = isl_multi_union_pw_aff_get_space(mupa);
8785 mv = isl_multi_val_zero(space);
8786 domain = isl_multi_union_pw_aff_domain(mupa);
8787 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8790 /* Construct and return a union piecewise multi affine expression
8791 * that is equal to the given multi union piecewise affine expression.
8793 * If the input is zero-dimensional, then
8794 * construct a union piecewise multi affine expression
8795 * on top of the explicit domain of the input.
8797 __isl_give isl_union_pw_multi_aff *
8798 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8799 __isl_take isl_multi_union_pw_aff *mupa)
8801 int i, n;
8802 isl_space *space;
8803 isl_union_pw_multi_aff *upma;
8804 isl_union_pw_aff *upa;
8806 if (!mupa)
8807 return NULL;
8809 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8810 if (n == 0)
8811 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8813 space = isl_multi_union_pw_aff_get_space(mupa);
8814 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8815 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8817 for (i = 1; i < n; ++i) {
8818 isl_union_pw_multi_aff *upma_i;
8820 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8821 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8822 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8825 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8827 isl_multi_union_pw_aff_free(mupa);
8828 return upma;
8831 /* Intersect the range of "mupa" with "range",
8832 * in the special case where "mupa" is 0D.
8834 * Intersect the domain of "mupa" with the constraints on the parameters
8835 * of "range".
8837 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8838 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8840 range = isl_set_params(range);
8841 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8842 return mupa;
8845 /* Intersect the range of "mupa" with "range".
8846 * That is, keep only those domain elements that have a function value
8847 * in "range".
8849 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8850 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8852 isl_union_pw_multi_aff *upma;
8853 isl_union_set *domain;
8854 isl_space *space;
8855 int n;
8856 int match;
8858 if (!mupa || !range)
8859 goto error;
8861 space = isl_set_get_space(range);
8862 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8863 space, isl_dim_set);
8864 isl_space_free(space);
8865 if (match < 0)
8866 goto error;
8867 if (!match)
8868 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8869 "space don't match", goto error);
8870 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8871 if (n == 0)
8872 return mupa_intersect_range_0D(mupa, range);
8874 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8875 isl_multi_union_pw_aff_copy(mupa));
8876 domain = isl_union_set_from_set(range);
8877 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8878 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8880 return mupa;
8881 error:
8882 isl_multi_union_pw_aff_free(mupa);
8883 isl_set_free(range);
8884 return NULL;
8887 /* Return the shared domain of the elements of "mupa",
8888 * in the special case where "mupa" is zero-dimensional.
8890 * Return the explicit domain of "mupa".
8891 * Note that this domain may be a parameter set, either
8892 * because "mupa" is meant to live in a set space or
8893 * because no explicit domain has been set.
8895 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8896 __isl_take isl_multi_union_pw_aff *mupa)
8898 isl_union_set *dom;
8900 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8901 isl_multi_union_pw_aff_free(mupa);
8903 return dom;
8906 /* Return the shared domain of the elements of "mupa".
8908 * If "mupa" is zero-dimensional, then return its explicit domain.
8910 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8911 __isl_take isl_multi_union_pw_aff *mupa)
8913 int i, n;
8914 isl_union_pw_aff *upa;
8915 isl_union_set *dom;
8917 if (!mupa)
8918 return NULL;
8920 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8921 if (n == 0)
8922 return isl_multi_union_pw_aff_domain_0D(mupa);
8924 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8925 dom = isl_union_pw_aff_domain(upa);
8926 for (i = 1; i < n; ++i) {
8927 isl_union_set *dom_i;
8929 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8930 dom_i = isl_union_pw_aff_domain(upa);
8931 dom = isl_union_set_intersect(dom, dom_i);
8934 isl_multi_union_pw_aff_free(mupa);
8935 return dom;
8938 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8939 * In particular, the spaces have been aligned.
8940 * The result is defined over the shared domain of the elements of "mupa"
8942 * We first extract the parametric constant part of "aff" and
8943 * define that over the shared domain.
8944 * Then we iterate over all input dimensions of "aff" and add the corresponding
8945 * multiples of the elements of "mupa".
8946 * Finally, we consider the integer divisions, calling the function
8947 * recursively to obtain an isl_union_pw_aff corresponding to the
8948 * integer division argument.
8950 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8951 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8953 int i, n_in, n_div;
8954 isl_union_pw_aff *upa;
8955 isl_union_set *uset;
8956 isl_val *v;
8957 isl_aff *cst;
8959 n_in = isl_aff_dim(aff, isl_dim_in);
8960 n_div = isl_aff_dim(aff, isl_dim_div);
8962 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8963 cst = isl_aff_copy(aff);
8964 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8965 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8966 cst = isl_aff_project_domain_on_params(cst);
8967 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8969 for (i = 0; i < n_in; ++i) {
8970 isl_union_pw_aff *upa_i;
8972 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8973 continue;
8974 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8975 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8976 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8977 upa = isl_union_pw_aff_add(upa, upa_i);
8980 for (i = 0; i < n_div; ++i) {
8981 isl_aff *div;
8982 isl_union_pw_aff *upa_i;
8984 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8985 continue;
8986 div = isl_aff_get_div(aff, i);
8987 upa_i = multi_union_pw_aff_apply_aff(
8988 isl_multi_union_pw_aff_copy(mupa), div);
8989 upa_i = isl_union_pw_aff_floor(upa_i);
8990 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8991 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8992 upa = isl_union_pw_aff_add(upa, upa_i);
8995 isl_multi_union_pw_aff_free(mupa);
8996 isl_aff_free(aff);
8998 return upa;
9001 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9002 * with the domain of "aff".
9003 * Furthermore, the dimension of this space needs to be greater than zero.
9004 * The result is defined over the shared domain of the elements of "mupa"
9006 * We perform these checks and then hand over control to
9007 * multi_union_pw_aff_apply_aff.
9009 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9010 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9012 isl_space *space1, *space2;
9013 int equal;
9015 mupa = isl_multi_union_pw_aff_align_params(mupa,
9016 isl_aff_get_space(aff));
9017 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9018 if (!mupa || !aff)
9019 goto error;
9021 space1 = isl_multi_union_pw_aff_get_space(mupa);
9022 space2 = isl_aff_get_domain_space(aff);
9023 equal = isl_space_is_equal(space1, space2);
9024 isl_space_free(space1);
9025 isl_space_free(space2);
9026 if (equal < 0)
9027 goto error;
9028 if (!equal)
9029 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9030 "spaces don't match", goto error);
9031 if (isl_aff_dim(aff, isl_dim_in) == 0)
9032 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9033 "cannot determine domains", goto error);
9035 return multi_union_pw_aff_apply_aff(mupa, aff);
9036 error:
9037 isl_multi_union_pw_aff_free(mupa);
9038 isl_aff_free(aff);
9039 return NULL;
9042 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9043 * The space of "mupa" is known to be compatible with the domain of "ma".
9045 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9046 * on the domain of "mupa".
9048 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9049 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9051 isl_union_set *dom;
9053 dom = isl_multi_union_pw_aff_domain(mupa);
9054 ma = isl_multi_aff_project_domain_on_params(ma);
9056 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9059 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9060 * with the domain of "ma".
9061 * The result is defined over the shared domain of the elements of "mupa"
9063 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9064 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9066 isl_space *space1, *space2;
9067 isl_multi_union_pw_aff *res;
9068 int equal;
9069 int i, n_out;
9071 mupa = isl_multi_union_pw_aff_align_params(mupa,
9072 isl_multi_aff_get_space(ma));
9073 ma = isl_multi_aff_align_params(ma,
9074 isl_multi_union_pw_aff_get_space(mupa));
9075 if (!mupa || !ma)
9076 goto error;
9078 space1 = isl_multi_union_pw_aff_get_space(mupa);
9079 space2 = isl_multi_aff_get_domain_space(ma);
9080 equal = isl_space_is_equal(space1, space2);
9081 isl_space_free(space1);
9082 isl_space_free(space2);
9083 if (equal < 0)
9084 goto error;
9085 if (!equal)
9086 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9087 "spaces don't match", goto error);
9088 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9089 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9090 return mupa_apply_multi_aff_0D(mupa, ma);
9092 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9093 res = isl_multi_union_pw_aff_alloc(space1);
9095 for (i = 0; i < n_out; ++i) {
9096 isl_aff *aff;
9097 isl_union_pw_aff *upa;
9099 aff = isl_multi_aff_get_aff(ma, i);
9100 upa = multi_union_pw_aff_apply_aff(
9101 isl_multi_union_pw_aff_copy(mupa), aff);
9102 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9105 isl_multi_aff_free(ma);
9106 isl_multi_union_pw_aff_free(mupa);
9107 return res;
9108 error:
9109 isl_multi_union_pw_aff_free(mupa);
9110 isl_multi_aff_free(ma);
9111 return NULL;
9114 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9115 * The space of "mupa" is known to be compatible with the domain of "pa".
9117 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9118 * on the domain of "mupa".
9120 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9121 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9123 isl_union_set *dom;
9125 dom = isl_multi_union_pw_aff_domain(mupa);
9126 pa = isl_pw_aff_project_domain_on_params(pa);
9128 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9131 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9132 * with the domain of "pa".
9133 * Furthermore, the dimension of this space needs to be greater than zero.
9134 * The result is defined over the shared domain of the elements of "mupa"
9136 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9137 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9139 int i;
9140 int equal;
9141 isl_space *space, *space2;
9142 isl_union_pw_aff *upa;
9144 mupa = isl_multi_union_pw_aff_align_params(mupa,
9145 isl_pw_aff_get_space(pa));
9146 pa = isl_pw_aff_align_params(pa,
9147 isl_multi_union_pw_aff_get_space(mupa));
9148 if (!mupa || !pa)
9149 goto error;
9151 space = isl_multi_union_pw_aff_get_space(mupa);
9152 space2 = isl_pw_aff_get_domain_space(pa);
9153 equal = isl_space_is_equal(space, space2);
9154 isl_space_free(space);
9155 isl_space_free(space2);
9156 if (equal < 0)
9157 goto error;
9158 if (!equal)
9159 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9160 "spaces don't match", goto error);
9161 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9162 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9164 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9165 upa = isl_union_pw_aff_empty(space);
9167 for (i = 0; i < pa->n; ++i) {
9168 isl_aff *aff;
9169 isl_set *domain;
9170 isl_multi_union_pw_aff *mupa_i;
9171 isl_union_pw_aff *upa_i;
9173 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9174 domain = isl_set_copy(pa->p[i].set);
9175 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9176 aff = isl_aff_copy(pa->p[i].aff);
9177 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9178 upa = isl_union_pw_aff_union_add(upa, upa_i);
9181 isl_multi_union_pw_aff_free(mupa);
9182 isl_pw_aff_free(pa);
9183 return upa;
9184 error:
9185 isl_multi_union_pw_aff_free(mupa);
9186 isl_pw_aff_free(pa);
9187 return NULL;
9190 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9191 * The space of "mupa" is known to be compatible with the domain of "pma".
9193 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9194 * on the domain of "mupa".
9196 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9197 __isl_take isl_multi_union_pw_aff *mupa,
9198 __isl_take isl_pw_multi_aff *pma)
9200 isl_union_set *dom;
9202 dom = isl_multi_union_pw_aff_domain(mupa);
9203 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9205 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9208 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9209 * with the domain of "pma".
9210 * The result is defined over the shared domain of the elements of "mupa"
9212 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9213 __isl_take isl_multi_union_pw_aff *mupa,
9214 __isl_take isl_pw_multi_aff *pma)
9216 isl_space *space1, *space2;
9217 isl_multi_union_pw_aff *res;
9218 int equal;
9219 int i, n_out;
9221 mupa = isl_multi_union_pw_aff_align_params(mupa,
9222 isl_pw_multi_aff_get_space(pma));
9223 pma = isl_pw_multi_aff_align_params(pma,
9224 isl_multi_union_pw_aff_get_space(mupa));
9225 if (!mupa || !pma)
9226 goto error;
9228 space1 = isl_multi_union_pw_aff_get_space(mupa);
9229 space2 = isl_pw_multi_aff_get_domain_space(pma);
9230 equal = isl_space_is_equal(space1, space2);
9231 isl_space_free(space1);
9232 isl_space_free(space2);
9233 if (equal < 0)
9234 goto error;
9235 if (!equal)
9236 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9237 "spaces don't match", goto error);
9238 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9239 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9240 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9242 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9243 res = isl_multi_union_pw_aff_alloc(space1);
9245 for (i = 0; i < n_out; ++i) {
9246 isl_pw_aff *pa;
9247 isl_union_pw_aff *upa;
9249 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9250 upa = isl_multi_union_pw_aff_apply_pw_aff(
9251 isl_multi_union_pw_aff_copy(mupa), pa);
9252 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9255 isl_pw_multi_aff_free(pma);
9256 isl_multi_union_pw_aff_free(mupa);
9257 return res;
9258 error:
9259 isl_multi_union_pw_aff_free(mupa);
9260 isl_pw_multi_aff_free(pma);
9261 return NULL;
9264 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9265 * If the explicit domain only keeps track of constraints on the parameters,
9266 * then only update those constraints.
9268 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9269 __isl_take isl_multi_union_pw_aff *mupa,
9270 __isl_keep isl_union_pw_multi_aff *upma)
9272 isl_bool is_params;
9274 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9275 return isl_multi_union_pw_aff_free(mupa);
9277 mupa = isl_multi_union_pw_aff_cow(mupa);
9278 if (!mupa)
9279 return NULL;
9281 is_params = isl_union_set_is_params(mupa->u.dom);
9282 if (is_params < 0)
9283 return isl_multi_union_pw_aff_free(mupa);
9285 upma = isl_union_pw_multi_aff_copy(upma);
9286 if (is_params)
9287 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9288 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9289 else
9290 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9291 mupa->u.dom, upma);
9292 if (!mupa->u.dom)
9293 return isl_multi_union_pw_aff_free(mupa);
9294 return mupa;
9297 /* Compute the pullback of "mupa" by the function represented by "upma".
9298 * In other words, plug in "upma" in "mupa". The result contains
9299 * expressions defined over the domain space of "upma".
9301 * Run over all elements of "mupa" and plug in "upma" in each of them.
9303 * If "mupa" has an explicit domain, then it is this domain
9304 * that needs to undergo a pullback instead, i.e., a preimage.
9306 __isl_give isl_multi_union_pw_aff *
9307 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9308 __isl_take isl_multi_union_pw_aff *mupa,
9309 __isl_take isl_union_pw_multi_aff *upma)
9311 int i, n;
9313 mupa = isl_multi_union_pw_aff_align_params(mupa,
9314 isl_union_pw_multi_aff_get_space(upma));
9315 upma = isl_union_pw_multi_aff_align_params(upma,
9316 isl_multi_union_pw_aff_get_space(mupa));
9317 mupa = isl_multi_union_pw_aff_cow(mupa);
9318 if (!mupa || !upma)
9319 goto error;
9321 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9322 for (i = 0; i < n; ++i) {
9323 isl_union_pw_aff *upa;
9325 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9326 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9327 isl_union_pw_multi_aff_copy(upma));
9328 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9331 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9332 mupa = preimage_explicit_domain(mupa, upma);
9334 isl_union_pw_multi_aff_free(upma);
9335 return mupa;
9336 error:
9337 isl_multi_union_pw_aff_free(mupa);
9338 isl_union_pw_multi_aff_free(upma);
9339 return NULL;
9342 /* Extract the sequence of elements in "mupa" with domain space "space"
9343 * (ignoring parameters).
9345 * For the elements of "mupa" that are not defined on the specified space,
9346 * the corresponding element in the result is empty.
9348 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9349 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9351 int i, n;
9352 isl_space *space_mpa;
9353 isl_multi_pw_aff *mpa;
9355 if (!mupa || !space)
9356 goto error;
9358 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9359 space = isl_space_replace_params(space, space_mpa);
9360 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9361 space_mpa);
9362 mpa = isl_multi_pw_aff_alloc(space_mpa);
9364 space = isl_space_from_domain(space);
9365 space = isl_space_add_dims(space, isl_dim_out, 1);
9366 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9367 for (i = 0; i < n; ++i) {
9368 isl_union_pw_aff *upa;
9369 isl_pw_aff *pa;
9371 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9372 pa = isl_union_pw_aff_extract_pw_aff(upa,
9373 isl_space_copy(space));
9374 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9375 isl_union_pw_aff_free(upa);
9378 isl_space_free(space);
9379 return mpa;
9380 error:
9381 isl_space_free(space);
9382 return NULL;
9385 /* Evaluate the affine function "aff" in the void point "pnt".
9386 * In particular, return the value NaN.
9388 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9389 __isl_take isl_point *pnt)
9391 isl_ctx *ctx;
9393 ctx = isl_point_get_ctx(pnt);
9394 isl_aff_free(aff);
9395 isl_point_free(pnt);
9396 return isl_val_nan(ctx);
9399 /* Evaluate the affine expression "aff"
9400 * in the coordinates (with denominator) "pnt".
9402 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9403 __isl_keep isl_vec *pnt)
9405 isl_int n, d;
9406 isl_ctx *ctx;
9407 isl_val *v;
9409 if (!aff || !pnt)
9410 return NULL;
9412 ctx = isl_vec_get_ctx(aff);
9413 isl_int_init(n);
9414 isl_int_init(d);
9415 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9416 isl_int_mul(d, aff->el[0], pnt->el[0]);
9417 v = isl_val_rat_from_isl_int(ctx, n, d);
9418 v = isl_val_normalize(v);
9419 isl_int_clear(n);
9420 isl_int_clear(d);
9422 return v;
9425 /* Check that the domain space of "aff" is equal to "space".
9427 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9428 __isl_keep isl_space *space)
9430 isl_bool ok;
9432 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9433 if (ok < 0)
9434 return isl_stat_error;
9435 if (!ok)
9436 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9437 "incompatible spaces", return isl_stat_error);
9438 return isl_stat_ok;
9441 /* Evaluate the affine function "aff" in "pnt".
9443 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9444 __isl_take isl_point *pnt)
9446 isl_bool is_void;
9447 isl_val *v;
9448 isl_local_space *ls;
9450 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9451 goto error;
9452 is_void = isl_point_is_void(pnt);
9453 if (is_void < 0)
9454 goto error;
9455 if (is_void)
9456 return eval_void(aff, pnt);
9458 ls = isl_aff_get_domain_local_space(aff);
9459 pnt = isl_local_space_lift_point(ls, pnt);
9461 v = eval(aff->v, isl_point_peek_vec(pnt));
9463 isl_aff_free(aff);
9464 isl_point_free(pnt);
9466 return v;
9467 error:
9468 isl_aff_free(aff);
9469 isl_point_free(pnt);
9470 return NULL;