isl_union_*_set_has_dim: rename to isl_union_*_set_has_space
[isl.git] / isl_aff.c
blob87ba4e62407636cbfa7195571c51bc406edfa178
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/id.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
53 #undef BASE
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
59 __isl_take isl_vec *v)
61 isl_aff *aff;
63 if (!ls || !v)
64 goto error;
66 aff = isl_calloc_type(v->ctx, struct isl_aff);
67 if (!aff)
68 goto error;
70 aff->ref = 1;
71 aff->ls = ls;
72 aff->v = v;
74 return aff;
75 error:
76 isl_local_space_free(ls);
77 isl_vec_free(v);
78 return NULL;
81 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
83 isl_ctx *ctx;
84 isl_vec *v;
85 unsigned total;
87 if (!ls)
88 return NULL;
90 ctx = isl_local_space_get_ctx(ls);
91 if (!isl_local_space_divs_known(ls))
92 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
93 goto error);
94 if (!isl_local_space_is_set(ls))
95 isl_die(ctx, isl_error_invalid,
96 "domain of affine expression should be a set",
97 goto error);
99 total = isl_local_space_dim(ls, isl_dim_all);
100 v = isl_vec_alloc(ctx, 1 + 1 + total);
101 return isl_aff_alloc_vec(ls, v);
102 error:
103 isl_local_space_free(ls);
104 return NULL;
107 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
109 isl_aff *aff;
111 aff = isl_aff_alloc(ls);
112 if (!aff)
113 return NULL;
115 isl_int_set_si(aff->v->el[0], 1);
116 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
118 return aff;
121 /* Return a piecewise affine expression defined on the specified domain
122 * that is equal to zero.
124 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
126 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
129 /* Return an affine expression defined on the specified domain
130 * that represents NaN.
132 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
134 isl_aff *aff;
136 aff = isl_aff_alloc(ls);
137 if (!aff)
138 return NULL;
140 isl_seq_clr(aff->v->el, aff->v->size);
142 return aff;
145 /* Return a piecewise affine expression defined on the specified domain
146 * that represents NaN.
148 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
150 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
153 /* Return an affine expression that is equal to "val" on
154 * domain local space "ls".
156 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
157 __isl_take isl_val *val)
159 isl_aff *aff;
161 if (!ls || !val)
162 goto error;
163 if (!isl_val_is_rat(val))
164 isl_die(isl_val_get_ctx(val), isl_error_invalid,
165 "expecting rational value", goto error);
167 aff = isl_aff_alloc(isl_local_space_copy(ls));
168 if (!aff)
169 goto error;
171 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
172 isl_int_set(aff->v->el[1], val->n);
173 isl_int_set(aff->v->el[0], val->d);
175 isl_local_space_free(ls);
176 isl_val_free(val);
177 return aff;
178 error:
179 isl_local_space_free(ls);
180 isl_val_free(val);
181 return NULL;
184 /* Return an affine expression that is equal to the specified dimension
185 * in "ls".
187 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
188 enum isl_dim_type type, unsigned pos)
190 isl_space *space;
191 isl_aff *aff;
193 if (!ls)
194 return NULL;
196 space = isl_local_space_get_space(ls);
197 if (!space)
198 goto error;
199 if (isl_space_is_map(space))
200 isl_die(isl_space_get_ctx(space), isl_error_invalid,
201 "expecting (parameter) set space", goto error);
202 if (pos >= isl_local_space_dim(ls, type))
203 isl_die(isl_space_get_ctx(space), isl_error_invalid,
204 "position out of bounds", goto error);
206 isl_space_free(space);
207 aff = isl_aff_alloc(ls);
208 if (!aff)
209 return NULL;
211 pos += isl_local_space_offset(aff->ls, type);
213 isl_int_set_si(aff->v->el[0], 1);
214 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
215 isl_int_set_si(aff->v->el[1 + pos], 1);
217 return aff;
218 error:
219 isl_local_space_free(ls);
220 isl_space_free(space);
221 return NULL;
224 /* Return a piecewise affine expression that is equal to
225 * the specified dimension in "ls".
227 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
228 enum isl_dim_type type, unsigned pos)
230 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
233 /* Return an affine expression that is equal to the parameter
234 * in the domain space "space" with identifier "id".
236 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
237 __isl_take isl_space *space, __isl_take isl_id *id)
239 int pos;
240 isl_local_space *ls;
242 if (!space || !id)
243 goto error;
244 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
245 if (pos < 0)
246 isl_die(isl_space_get_ctx(space), isl_error_invalid,
247 "parameter not found in space", goto error);
248 isl_id_free(id);
249 ls = isl_local_space_from_space(space);
250 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
251 error:
252 isl_space_free(space);
253 isl_id_free(id);
254 return NULL;
257 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
259 if (!aff)
260 return NULL;
262 aff->ref++;
263 return aff;
266 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
268 if (!aff)
269 return NULL;
271 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
272 isl_vec_copy(aff->v));
275 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
277 if (!aff)
278 return NULL;
280 if (aff->ref == 1)
281 return aff;
282 aff->ref--;
283 return isl_aff_dup(aff);
286 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
288 if (!aff)
289 return NULL;
291 if (--aff->ref > 0)
292 return NULL;
294 isl_local_space_free(aff->ls);
295 isl_vec_free(aff->v);
297 free(aff);
299 return NULL;
302 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
304 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
307 /* Return a hash value that digests "aff".
309 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
311 uint32_t hash, ls_hash, v_hash;
313 if (!aff)
314 return 0;
316 hash = isl_hash_init();
317 ls_hash = isl_local_space_get_hash(aff->ls);
318 isl_hash_hash(hash, ls_hash);
319 v_hash = isl_vec_get_hash(aff->v);
320 isl_hash_hash(hash, v_hash);
322 return hash;
325 /* Externally, an isl_aff has a map space, but internally, the
326 * ls field corresponds to the domain of that space.
328 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
330 if (!aff)
331 return 0;
332 if (type == isl_dim_out)
333 return 1;
334 if (type == isl_dim_in)
335 type = isl_dim_set;
336 return isl_local_space_dim(aff->ls, type);
339 /* Return the position of the dimension of the given type and name
340 * in "aff".
341 * Return -1 if no such dimension can be found.
343 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
344 const char *name)
346 if (!aff)
347 return -1;
348 if (type == isl_dim_out)
349 return -1;
350 if (type == isl_dim_in)
351 type = isl_dim_set;
352 return isl_local_space_find_dim_by_name(aff->ls, type, name);
355 /* Return the domain space of "aff".
357 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
359 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
362 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
364 return isl_space_copy(isl_aff_peek_domain_space(aff));
367 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
369 isl_space *space;
370 if (!aff)
371 return NULL;
372 space = isl_local_space_get_space(aff->ls);
373 space = isl_space_from_domain(space);
374 space = isl_space_add_dims(space, isl_dim_out, 1);
375 return space;
378 __isl_give isl_local_space *isl_aff_get_domain_local_space(
379 __isl_keep isl_aff *aff)
381 return aff ? isl_local_space_copy(aff->ls) : NULL;
384 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
386 isl_local_space *ls;
387 if (!aff)
388 return NULL;
389 ls = isl_local_space_copy(aff->ls);
390 ls = isl_local_space_from_domain(ls);
391 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
392 return ls;
395 /* Return the local space of the domain of "aff".
396 * This may be either a copy or the local space itself
397 * if there is only one reference to "aff".
398 * This allows the local space to be modified inplace
399 * if both the expression and its local space have only a single reference.
400 * The caller is not allowed to modify "aff" between this call and
401 * a subsequent call to isl_aff_restore_domain_local_space.
402 * The only exception is that isl_aff_free can be called instead.
404 __isl_give isl_local_space *isl_aff_take_domain_local_space(
405 __isl_keep isl_aff *aff)
407 isl_local_space *ls;
409 if (!aff)
410 return NULL;
411 if (aff->ref != 1)
412 return isl_aff_get_domain_local_space(aff);
413 ls = aff->ls;
414 aff->ls = NULL;
415 return ls;
418 /* Set the local space of the domain of "aff" to "ls",
419 * where the local space of "aff" may be missing
420 * due to a preceding call to isl_aff_take_domain_local_space.
421 * However, in this case, "aff" only has a single reference and
422 * then the call to isl_aff_cow has no effect.
424 __isl_give isl_aff *isl_aff_restore_domain_local_space(
425 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
427 if (!aff || !ls)
428 goto error;
430 if (aff->ls == ls) {
431 isl_local_space_free(ls);
432 return aff;
435 aff = isl_aff_cow(aff);
436 if (!aff)
437 goto error;
438 isl_local_space_free(aff->ls);
439 aff->ls = ls;
441 return aff;
442 error:
443 isl_aff_free(aff);
444 isl_local_space_free(ls);
445 return NULL;
448 /* Externally, an isl_aff has a map space, but internally, the
449 * ls field corresponds to the domain of that space.
451 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
452 enum isl_dim_type type, unsigned pos)
454 if (!aff)
455 return NULL;
456 if (type == isl_dim_out)
457 return NULL;
458 if (type == isl_dim_in)
459 type = isl_dim_set;
460 return isl_local_space_get_dim_name(aff->ls, type, pos);
463 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
464 __isl_take isl_space *dim)
466 aff = isl_aff_cow(aff);
467 if (!aff || !dim)
468 goto error;
470 aff->ls = isl_local_space_reset_space(aff->ls, dim);
471 if (!aff->ls)
472 return isl_aff_free(aff);
474 return aff;
475 error:
476 isl_aff_free(aff);
477 isl_space_free(dim);
478 return NULL;
481 /* Reset the space of "aff". This function is called from isl_pw_templ.c
482 * and doesn't know if the space of an element object is represented
483 * directly or through its domain. It therefore passes along both.
485 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
486 __isl_take isl_space *space, __isl_take isl_space *domain)
488 isl_space_free(space);
489 return isl_aff_reset_domain_space(aff, domain);
492 /* Reorder the coefficients of the affine expression based
493 * on the given reordering.
494 * The reordering r is assumed to have been extended with the local
495 * variables.
497 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
498 __isl_take isl_reordering *r, int n_div)
500 isl_space *space;
501 isl_vec *res;
502 int i;
504 if (!vec || !r)
505 goto error;
507 space = isl_reordering_peek_space(r);
508 res = isl_vec_alloc(vec->ctx,
509 2 + isl_space_dim(space, isl_dim_all) + n_div);
510 if (!res)
511 goto error;
512 isl_seq_cpy(res->el, vec->el, 2);
513 isl_seq_clr(res->el + 2, res->size - 2);
514 for (i = 0; i < r->len; ++i)
515 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
517 isl_reordering_free(r);
518 isl_vec_free(vec);
519 return res;
520 error:
521 isl_vec_free(vec);
522 isl_reordering_free(r);
523 return NULL;
526 /* Reorder the dimensions of the domain of "aff" according
527 * to the given reordering.
529 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
530 __isl_take isl_reordering *r)
532 aff = isl_aff_cow(aff);
533 if (!aff)
534 goto error;
536 r = isl_reordering_extend(r, aff->ls->div->n_row);
537 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
538 aff->ls->div->n_row);
539 aff->ls = isl_local_space_realign(aff->ls, r);
541 if (!aff->v || !aff->ls)
542 return isl_aff_free(aff);
544 return aff;
545 error:
546 isl_aff_free(aff);
547 isl_reordering_free(r);
548 return NULL;
551 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
552 __isl_take isl_space *model)
554 isl_bool equal_params;
556 if (!aff || !model)
557 goto error;
559 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
560 if (equal_params < 0)
561 goto error;
562 if (!equal_params) {
563 isl_reordering *exp;
565 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
566 exp = isl_reordering_extend_space(exp,
567 isl_aff_get_domain_space(aff));
568 aff = isl_aff_realign_domain(aff, exp);
571 isl_space_free(model);
572 return aff;
573 error:
574 isl_space_free(model);
575 isl_aff_free(aff);
576 return NULL;
579 /* Is "aff" obviously equal to zero?
581 * If the denominator is zero, then "aff" is not equal to zero.
583 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
585 if (!aff)
586 return isl_bool_error;
588 if (isl_int_is_zero(aff->v->el[0]))
589 return isl_bool_false;
590 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
593 /* Does "aff" represent NaN?
595 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
597 if (!aff)
598 return isl_bool_error;
600 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
603 /* Are "aff1" and "aff2" obviously equal?
605 * NaN is not equal to anything, not even to another NaN.
607 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
608 __isl_keep isl_aff *aff2)
610 isl_bool equal;
612 if (!aff1 || !aff2)
613 return isl_bool_error;
615 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
616 return isl_bool_false;
618 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
619 if (equal < 0 || !equal)
620 return equal;
622 return isl_vec_is_equal(aff1->v, aff2->v);
625 /* Return the common denominator of "aff" in "v".
627 * We cannot return anything meaningful in case of a NaN.
629 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
631 if (!aff)
632 return isl_stat_error;
633 if (isl_aff_is_nan(aff))
634 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
635 "cannot get denominator of NaN", return isl_stat_error);
636 isl_int_set(*v, aff->v->el[0]);
637 return isl_stat_ok;
640 /* Return the common denominator of "aff".
642 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
644 isl_ctx *ctx;
646 if (!aff)
647 return NULL;
649 ctx = isl_aff_get_ctx(aff);
650 if (isl_aff_is_nan(aff))
651 return isl_val_nan(ctx);
652 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
655 /* Return the constant term of "aff".
657 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
659 isl_ctx *ctx;
660 isl_val *v;
662 if (!aff)
663 return NULL;
665 ctx = isl_aff_get_ctx(aff);
666 if (isl_aff_is_nan(aff))
667 return isl_val_nan(ctx);
668 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
669 return isl_val_normalize(v);
672 /* Return the coefficient of the variable of type "type" at position "pos"
673 * of "aff".
675 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
676 enum isl_dim_type type, int pos)
678 isl_ctx *ctx;
679 isl_val *v;
681 if (!aff)
682 return NULL;
684 ctx = isl_aff_get_ctx(aff);
685 if (type == isl_dim_out)
686 isl_die(ctx, isl_error_invalid,
687 "output/set dimension does not have a coefficient",
688 return NULL);
689 if (type == isl_dim_in)
690 type = isl_dim_set;
692 if (pos >= isl_local_space_dim(aff->ls, type))
693 isl_die(ctx, isl_error_invalid,
694 "position out of bounds", return NULL);
696 if (isl_aff_is_nan(aff))
697 return isl_val_nan(ctx);
698 pos += isl_local_space_offset(aff->ls, type);
699 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
700 return isl_val_normalize(v);
703 /* Return the sign of the coefficient of the variable of type "type"
704 * at position "pos" of "aff".
706 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
707 int pos)
709 isl_ctx *ctx;
711 if (!aff)
712 return 0;
714 ctx = isl_aff_get_ctx(aff);
715 if (type == isl_dim_out)
716 isl_die(ctx, isl_error_invalid,
717 "output/set dimension does not have a coefficient",
718 return 0);
719 if (type == isl_dim_in)
720 type = isl_dim_set;
722 if (pos >= isl_local_space_dim(aff->ls, type))
723 isl_die(ctx, isl_error_invalid,
724 "position out of bounds", return 0);
726 pos += isl_local_space_offset(aff->ls, type);
727 return isl_int_sgn(aff->v->el[1 + pos]);
730 /* Replace the numerator of the constant term of "aff" by "v".
732 * A NaN is unaffected by this operation.
734 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
736 if (!aff)
737 return NULL;
738 if (isl_aff_is_nan(aff))
739 return aff;
740 aff = isl_aff_cow(aff);
741 if (!aff)
742 return NULL;
744 aff->v = isl_vec_cow(aff->v);
745 if (!aff->v)
746 return isl_aff_free(aff);
748 isl_int_set(aff->v->el[1], v);
750 return aff;
753 /* Replace the constant term of "aff" by "v".
755 * A NaN is unaffected by this operation.
757 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
758 __isl_take isl_val *v)
760 if (!aff || !v)
761 goto error;
763 if (isl_aff_is_nan(aff)) {
764 isl_val_free(v);
765 return aff;
768 if (!isl_val_is_rat(v))
769 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
770 "expecting rational value", goto error);
772 if (isl_int_eq(aff->v->el[1], v->n) &&
773 isl_int_eq(aff->v->el[0], v->d)) {
774 isl_val_free(v);
775 return aff;
778 aff = isl_aff_cow(aff);
779 if (!aff)
780 goto error;
781 aff->v = isl_vec_cow(aff->v);
782 if (!aff->v)
783 goto error;
785 if (isl_int_eq(aff->v->el[0], v->d)) {
786 isl_int_set(aff->v->el[1], v->n);
787 } else if (isl_int_is_one(v->d)) {
788 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
789 } else {
790 isl_seq_scale(aff->v->el + 1,
791 aff->v->el + 1, v->d, aff->v->size - 1);
792 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
793 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
794 aff->v = isl_vec_normalize(aff->v);
795 if (!aff->v)
796 goto error;
799 isl_val_free(v);
800 return aff;
801 error:
802 isl_aff_free(aff);
803 isl_val_free(v);
804 return NULL;
807 /* Add "v" to the constant term of "aff".
809 * A NaN is unaffected by this operation.
811 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
813 if (isl_int_is_zero(v))
814 return aff;
816 if (!aff)
817 return NULL;
818 if (isl_aff_is_nan(aff))
819 return aff;
820 aff = isl_aff_cow(aff);
821 if (!aff)
822 return NULL;
824 aff->v = isl_vec_cow(aff->v);
825 if (!aff->v)
826 return isl_aff_free(aff);
828 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
830 return aff;
833 /* Add "v" to the constant term of "aff".
835 * A NaN is unaffected by this operation.
837 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
838 __isl_take isl_val *v)
840 if (!aff || !v)
841 goto error;
843 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
844 isl_val_free(v);
845 return aff;
848 if (!isl_val_is_rat(v))
849 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
850 "expecting rational value", goto error);
852 aff = isl_aff_cow(aff);
853 if (!aff)
854 goto error;
856 aff->v = isl_vec_cow(aff->v);
857 if (!aff->v)
858 goto error;
860 if (isl_int_is_one(v->d)) {
861 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
862 } else if (isl_int_eq(aff->v->el[0], v->d)) {
863 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
864 aff->v = isl_vec_normalize(aff->v);
865 if (!aff->v)
866 goto error;
867 } else {
868 isl_seq_scale(aff->v->el + 1,
869 aff->v->el + 1, v->d, aff->v->size - 1);
870 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
871 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
872 aff->v = isl_vec_normalize(aff->v);
873 if (!aff->v)
874 goto error;
877 isl_val_free(v);
878 return aff;
879 error:
880 isl_aff_free(aff);
881 isl_val_free(v);
882 return NULL;
885 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
887 isl_int t;
889 isl_int_init(t);
890 isl_int_set_si(t, v);
891 aff = isl_aff_add_constant(aff, t);
892 isl_int_clear(t);
894 return aff;
897 /* Add "v" to the numerator of the constant term of "aff".
899 * A NaN is unaffected by this operation.
901 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
903 if (isl_int_is_zero(v))
904 return aff;
906 if (!aff)
907 return NULL;
908 if (isl_aff_is_nan(aff))
909 return aff;
910 aff = isl_aff_cow(aff);
911 if (!aff)
912 return NULL;
914 aff->v = isl_vec_cow(aff->v);
915 if (!aff->v)
916 return isl_aff_free(aff);
918 isl_int_add(aff->v->el[1], aff->v->el[1], v);
920 return aff;
923 /* Add "v" to the numerator of the constant term of "aff".
925 * A NaN is unaffected by this operation.
927 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
929 isl_int t;
931 if (v == 0)
932 return aff;
934 isl_int_init(t);
935 isl_int_set_si(t, v);
936 aff = isl_aff_add_constant_num(aff, t);
937 isl_int_clear(t);
939 return aff;
942 /* Replace the numerator of the constant term of "aff" by "v".
944 * A NaN is unaffected by this operation.
946 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
948 if (!aff)
949 return NULL;
950 if (isl_aff_is_nan(aff))
951 return aff;
952 aff = isl_aff_cow(aff);
953 if (!aff)
954 return NULL;
956 aff->v = isl_vec_cow(aff->v);
957 if (!aff->v)
958 return isl_aff_free(aff);
960 isl_int_set_si(aff->v->el[1], v);
962 return aff;
965 /* Replace the numerator of the coefficient of the variable of type "type"
966 * at position "pos" of "aff" by "v".
968 * A NaN is unaffected by this operation.
970 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
971 enum isl_dim_type type, int pos, isl_int v)
973 if (!aff)
974 return NULL;
976 if (type == isl_dim_out)
977 isl_die(aff->v->ctx, isl_error_invalid,
978 "output/set dimension does not have a coefficient",
979 return isl_aff_free(aff));
980 if (type == isl_dim_in)
981 type = isl_dim_set;
983 if (pos >= isl_local_space_dim(aff->ls, type))
984 isl_die(aff->v->ctx, isl_error_invalid,
985 "position out of bounds", return isl_aff_free(aff));
987 if (isl_aff_is_nan(aff))
988 return aff;
989 aff = isl_aff_cow(aff);
990 if (!aff)
991 return NULL;
993 aff->v = isl_vec_cow(aff->v);
994 if (!aff->v)
995 return isl_aff_free(aff);
997 pos += isl_local_space_offset(aff->ls, type);
998 isl_int_set(aff->v->el[1 + pos], v);
1000 return aff;
1003 /* Replace the numerator of the coefficient of the variable of type "type"
1004 * at position "pos" of "aff" by "v".
1006 * A NaN is unaffected by this operation.
1008 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1009 enum isl_dim_type type, int pos, int v)
1011 if (!aff)
1012 return NULL;
1014 if (type == isl_dim_out)
1015 isl_die(aff->v->ctx, isl_error_invalid,
1016 "output/set dimension does not have a coefficient",
1017 return isl_aff_free(aff));
1018 if (type == isl_dim_in)
1019 type = isl_dim_set;
1021 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1022 isl_die(aff->v->ctx, isl_error_invalid,
1023 "position out of bounds", return isl_aff_free(aff));
1025 if (isl_aff_is_nan(aff))
1026 return aff;
1027 pos += isl_local_space_offset(aff->ls, type);
1028 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1029 return aff;
1031 aff = isl_aff_cow(aff);
1032 if (!aff)
1033 return NULL;
1035 aff->v = isl_vec_cow(aff->v);
1036 if (!aff->v)
1037 return isl_aff_free(aff);
1039 isl_int_set_si(aff->v->el[1 + pos], v);
1041 return aff;
1044 /* Replace the coefficient of the variable of type "type" at position "pos"
1045 * of "aff" by "v".
1047 * A NaN is unaffected by this operation.
1049 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1050 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1052 if (!aff || !v)
1053 goto error;
1055 if (type == isl_dim_out)
1056 isl_die(aff->v->ctx, isl_error_invalid,
1057 "output/set dimension does not have a coefficient",
1058 goto error);
1059 if (type == isl_dim_in)
1060 type = isl_dim_set;
1062 if (pos >= isl_local_space_dim(aff->ls, type))
1063 isl_die(aff->v->ctx, isl_error_invalid,
1064 "position out of bounds", goto error);
1066 if (isl_aff_is_nan(aff)) {
1067 isl_val_free(v);
1068 return aff;
1070 if (!isl_val_is_rat(v))
1071 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1072 "expecting rational value", goto error);
1074 pos += isl_local_space_offset(aff->ls, type);
1075 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1076 isl_int_eq(aff->v->el[0], v->d)) {
1077 isl_val_free(v);
1078 return aff;
1081 aff = isl_aff_cow(aff);
1082 if (!aff)
1083 goto error;
1084 aff->v = isl_vec_cow(aff->v);
1085 if (!aff->v)
1086 goto error;
1088 if (isl_int_eq(aff->v->el[0], v->d)) {
1089 isl_int_set(aff->v->el[1 + pos], v->n);
1090 } else if (isl_int_is_one(v->d)) {
1091 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1092 } else {
1093 isl_seq_scale(aff->v->el + 1,
1094 aff->v->el + 1, v->d, aff->v->size - 1);
1095 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1096 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1097 aff->v = isl_vec_normalize(aff->v);
1098 if (!aff->v)
1099 goto error;
1102 isl_val_free(v);
1103 return aff;
1104 error:
1105 isl_aff_free(aff);
1106 isl_val_free(v);
1107 return NULL;
1110 /* Add "v" to the coefficient of the variable of type "type"
1111 * at position "pos" of "aff".
1113 * A NaN is unaffected by this operation.
1115 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1116 enum isl_dim_type type, int pos, isl_int v)
1118 if (!aff)
1119 return NULL;
1121 if (type == isl_dim_out)
1122 isl_die(aff->v->ctx, isl_error_invalid,
1123 "output/set dimension does not have a coefficient",
1124 return isl_aff_free(aff));
1125 if (type == isl_dim_in)
1126 type = isl_dim_set;
1128 if (pos >= isl_local_space_dim(aff->ls, type))
1129 isl_die(aff->v->ctx, isl_error_invalid,
1130 "position out of bounds", return isl_aff_free(aff));
1132 if (isl_aff_is_nan(aff))
1133 return aff;
1134 aff = isl_aff_cow(aff);
1135 if (!aff)
1136 return NULL;
1138 aff->v = isl_vec_cow(aff->v);
1139 if (!aff->v)
1140 return isl_aff_free(aff);
1142 pos += isl_local_space_offset(aff->ls, type);
1143 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1145 return aff;
1148 /* Add "v" to the coefficient of the variable of type "type"
1149 * at position "pos" of "aff".
1151 * A NaN is unaffected by this operation.
1153 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1154 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1156 if (!aff || !v)
1157 goto error;
1159 if (isl_val_is_zero(v)) {
1160 isl_val_free(v);
1161 return aff;
1164 if (type == isl_dim_out)
1165 isl_die(aff->v->ctx, isl_error_invalid,
1166 "output/set dimension does not have a coefficient",
1167 goto error);
1168 if (type == isl_dim_in)
1169 type = isl_dim_set;
1171 if (pos >= isl_local_space_dim(aff->ls, type))
1172 isl_die(aff->v->ctx, isl_error_invalid,
1173 "position out of bounds", goto error);
1175 if (isl_aff_is_nan(aff)) {
1176 isl_val_free(v);
1177 return aff;
1179 if (!isl_val_is_rat(v))
1180 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1181 "expecting rational value", goto error);
1183 aff = isl_aff_cow(aff);
1184 if (!aff)
1185 goto error;
1187 aff->v = isl_vec_cow(aff->v);
1188 if (!aff->v)
1189 goto error;
1191 pos += isl_local_space_offset(aff->ls, type);
1192 if (isl_int_is_one(v->d)) {
1193 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1194 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1195 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1196 aff->v = isl_vec_normalize(aff->v);
1197 if (!aff->v)
1198 goto error;
1199 } else {
1200 isl_seq_scale(aff->v->el + 1,
1201 aff->v->el + 1, v->d, aff->v->size - 1);
1202 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1203 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1204 aff->v = isl_vec_normalize(aff->v);
1205 if (!aff->v)
1206 goto error;
1209 isl_val_free(v);
1210 return aff;
1211 error:
1212 isl_aff_free(aff);
1213 isl_val_free(v);
1214 return NULL;
1217 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1218 enum isl_dim_type type, int pos, int v)
1220 isl_int t;
1222 isl_int_init(t);
1223 isl_int_set_si(t, v);
1224 aff = isl_aff_add_coefficient(aff, type, pos, t);
1225 isl_int_clear(t);
1227 return aff;
1230 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1232 if (!aff)
1233 return NULL;
1235 return isl_local_space_get_div(aff->ls, pos);
1238 /* Return the negation of "aff".
1240 * As a special case, -NaN = NaN.
1242 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1244 if (!aff)
1245 return NULL;
1246 if (isl_aff_is_nan(aff))
1247 return aff;
1248 aff = isl_aff_cow(aff);
1249 if (!aff)
1250 return NULL;
1251 aff->v = isl_vec_cow(aff->v);
1252 if (!aff->v)
1253 return isl_aff_free(aff);
1255 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1257 return aff;
1260 /* Remove divs from the local space that do not appear in the affine
1261 * expression.
1262 * We currently only remove divs at the end.
1263 * Some intermediate divs may also not appear directly in the affine
1264 * expression, but we would also need to check that no other divs are
1265 * defined in terms of them.
1267 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1269 int pos;
1270 int off;
1271 int n;
1273 if (!aff)
1274 return NULL;
1276 n = isl_local_space_dim(aff->ls, isl_dim_div);
1277 off = isl_local_space_offset(aff->ls, isl_dim_div);
1279 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1280 if (pos == n)
1281 return aff;
1283 aff = isl_aff_cow(aff);
1284 if (!aff)
1285 return NULL;
1287 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1288 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1289 if (!aff->ls || !aff->v)
1290 return isl_aff_free(aff);
1292 return aff;
1295 /* Look for any divs in the aff->ls with a denominator equal to one
1296 * and plug them into the affine expression and any subsequent divs
1297 * that may reference the div.
1299 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1301 int i, n;
1302 int len;
1303 isl_int v;
1304 isl_vec *vec;
1305 isl_local_space *ls;
1306 unsigned pos;
1308 if (!aff)
1309 return NULL;
1311 n = isl_local_space_dim(aff->ls, isl_dim_div);
1312 len = aff->v->size;
1313 for (i = 0; i < n; ++i) {
1314 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1315 continue;
1316 ls = isl_local_space_copy(aff->ls);
1317 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1318 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1319 vec = isl_vec_copy(aff->v);
1320 vec = isl_vec_cow(vec);
1321 if (!ls || !vec)
1322 goto error;
1324 isl_int_init(v);
1326 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1327 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1328 len, len, v);
1330 isl_int_clear(v);
1332 isl_vec_free(aff->v);
1333 aff->v = vec;
1334 isl_local_space_free(aff->ls);
1335 aff->ls = ls;
1338 return aff;
1339 error:
1340 isl_vec_free(vec);
1341 isl_local_space_free(ls);
1342 return isl_aff_free(aff);
1345 /* Look for any divs j that appear with a unit coefficient inside
1346 * the definitions of other divs i and plug them into the definitions
1347 * of the divs i.
1349 * In particular, an expression of the form
1351 * floor((f(..) + floor(g(..)/n))/m)
1353 * is simplified to
1355 * floor((n * f(..) + g(..))/(n * m))
1357 * This simplification is correct because we can move the expression
1358 * f(..) into the inner floor in the original expression to obtain
1360 * floor(floor((n * f(..) + g(..))/n)/m)
1362 * from which we can derive the simplified expression.
1364 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1366 int i, j, n;
1367 int off;
1369 if (!aff)
1370 return NULL;
1372 n = isl_local_space_dim(aff->ls, isl_dim_div);
1373 off = isl_local_space_offset(aff->ls, isl_dim_div);
1374 for (i = 1; i < n; ++i) {
1375 for (j = 0; j < i; ++j) {
1376 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1377 continue;
1378 aff->ls = isl_local_space_substitute_seq(aff->ls,
1379 isl_dim_div, j, aff->ls->div->row[j],
1380 aff->v->size, i, 1);
1381 if (!aff->ls)
1382 return isl_aff_free(aff);
1386 return aff;
1389 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1391 * Even though this function is only called on isl_affs with a single
1392 * reference, we are careful to only change aff->v and aff->ls together.
1394 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1396 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1397 isl_local_space *ls;
1398 isl_vec *v;
1400 ls = isl_local_space_copy(aff->ls);
1401 ls = isl_local_space_swap_div(ls, a, b);
1402 v = isl_vec_copy(aff->v);
1403 v = isl_vec_cow(v);
1404 if (!ls || !v)
1405 goto error;
1407 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1408 isl_vec_free(aff->v);
1409 aff->v = v;
1410 isl_local_space_free(aff->ls);
1411 aff->ls = ls;
1413 return aff;
1414 error:
1415 isl_vec_free(v);
1416 isl_local_space_free(ls);
1417 return isl_aff_free(aff);
1420 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1422 * We currently do not actually remove div "b", but simply add its
1423 * coefficient to that of "a" and then zero it out.
1425 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1427 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1429 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1430 return aff;
1432 aff->v = isl_vec_cow(aff->v);
1433 if (!aff->v)
1434 return isl_aff_free(aff);
1436 isl_int_add(aff->v->el[1 + off + a],
1437 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1438 isl_int_set_si(aff->v->el[1 + off + b], 0);
1440 return aff;
1443 /* Sort the divs in the local space of "aff" according to
1444 * the comparison function "cmp_row" in isl_local_space.c,
1445 * combining the coefficients of identical divs.
1447 * Reordering divs does not change the semantics of "aff",
1448 * so there is no need to call isl_aff_cow.
1449 * Moreover, this function is currently only called on isl_affs
1450 * with a single reference.
1452 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1454 int i, j, n;
1456 if (!aff)
1457 return NULL;
1459 n = isl_aff_dim(aff, isl_dim_div);
1460 for (i = 1; i < n; ++i) {
1461 for (j = i - 1; j >= 0; --j) {
1462 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1463 if (cmp < 0)
1464 break;
1465 if (cmp == 0)
1466 aff = merge_divs(aff, j, j + 1);
1467 else
1468 aff = swap_div(aff, j, j + 1);
1469 if (!aff)
1470 return NULL;
1474 return aff;
1477 /* Normalize the representation of "aff".
1479 * This function should only be called of "new" isl_affs, i.e.,
1480 * with only a single reference. We therefore do not need to
1481 * worry about affecting other instances.
1483 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1485 if (!aff)
1486 return NULL;
1487 aff->v = isl_vec_normalize(aff->v);
1488 if (!aff->v)
1489 return isl_aff_free(aff);
1490 aff = plug_in_integral_divs(aff);
1491 aff = plug_in_unit_divs(aff);
1492 aff = sort_divs(aff);
1493 aff = isl_aff_remove_unused_divs(aff);
1494 return aff;
1497 /* Given f, return floor(f).
1498 * If f is an integer expression, then just return f.
1499 * If f is a constant, then return the constant floor(f).
1500 * Otherwise, if f = g/m, write g = q m + r,
1501 * create a new div d = [r/m] and return the expression q + d.
1502 * The coefficients in r are taken to lie between -m/2 and m/2.
1504 * reduce_div_coefficients performs the same normalization.
1506 * As a special case, floor(NaN) = NaN.
1508 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1510 int i;
1511 int size;
1512 isl_ctx *ctx;
1513 isl_vec *div;
1515 if (!aff)
1516 return NULL;
1518 if (isl_aff_is_nan(aff))
1519 return aff;
1520 if (isl_int_is_one(aff->v->el[0]))
1521 return aff;
1523 aff = isl_aff_cow(aff);
1524 if (!aff)
1525 return NULL;
1527 aff->v = isl_vec_cow(aff->v);
1528 if (!aff->v)
1529 return isl_aff_free(aff);
1531 if (isl_aff_is_cst(aff)) {
1532 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1533 isl_int_set_si(aff->v->el[0], 1);
1534 return aff;
1537 div = isl_vec_copy(aff->v);
1538 div = isl_vec_cow(div);
1539 if (!div)
1540 return isl_aff_free(aff);
1542 ctx = isl_aff_get_ctx(aff);
1543 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1544 for (i = 1; i < aff->v->size; ++i) {
1545 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1546 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1547 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1548 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1549 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1553 aff->ls = isl_local_space_add_div(aff->ls, div);
1554 if (!aff->ls)
1555 return isl_aff_free(aff);
1557 size = aff->v->size;
1558 aff->v = isl_vec_extend(aff->v, size + 1);
1559 if (!aff->v)
1560 return isl_aff_free(aff);
1561 isl_int_set_si(aff->v->el[0], 1);
1562 isl_int_set_si(aff->v->el[size], 1);
1564 aff = isl_aff_normalize(aff);
1566 return aff;
1569 /* Compute
1571 * aff mod m = aff - m * floor(aff/m)
1573 * with m an integer value.
1575 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1576 __isl_take isl_val *m)
1578 isl_aff *res;
1580 if (!aff || !m)
1581 goto error;
1583 if (!isl_val_is_int(m))
1584 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1585 "expecting integer modulo", goto error);
1587 res = isl_aff_copy(aff);
1588 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1589 aff = isl_aff_floor(aff);
1590 aff = isl_aff_scale_val(aff, m);
1591 res = isl_aff_sub(res, aff);
1593 return res;
1594 error:
1595 isl_aff_free(aff);
1596 isl_val_free(m);
1597 return NULL;
1600 /* Compute
1602 * pwaff mod m = pwaff - m * floor(pwaff/m)
1604 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1606 isl_pw_aff *res;
1608 res = isl_pw_aff_copy(pwaff);
1609 pwaff = isl_pw_aff_scale_down(pwaff, m);
1610 pwaff = isl_pw_aff_floor(pwaff);
1611 pwaff = isl_pw_aff_scale(pwaff, m);
1612 res = isl_pw_aff_sub(res, pwaff);
1614 return res;
1617 /* Compute
1619 * pa mod m = pa - m * floor(pa/m)
1621 * with m an integer value.
1623 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1624 __isl_take isl_val *m)
1626 if (!pa || !m)
1627 goto error;
1628 if (!isl_val_is_int(m))
1629 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1630 "expecting integer modulo", goto error);
1631 pa = isl_pw_aff_mod(pa, m->n);
1632 isl_val_free(m);
1633 return pa;
1634 error:
1635 isl_pw_aff_free(pa);
1636 isl_val_free(m);
1637 return NULL;
1640 /* Given f, return ceil(f).
1641 * If f is an integer expression, then just return f.
1642 * Otherwise, let f be the expression
1644 * e/m
1646 * then return
1648 * floor((e + m - 1)/m)
1650 * As a special case, ceil(NaN) = NaN.
1652 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1654 if (!aff)
1655 return NULL;
1657 if (isl_aff_is_nan(aff))
1658 return aff;
1659 if (isl_int_is_one(aff->v->el[0]))
1660 return aff;
1662 aff = isl_aff_cow(aff);
1663 if (!aff)
1664 return NULL;
1665 aff->v = isl_vec_cow(aff->v);
1666 if (!aff->v)
1667 return isl_aff_free(aff);
1669 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1670 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1671 aff = isl_aff_floor(aff);
1673 return aff;
1676 /* Apply the expansion computed by isl_merge_divs.
1677 * The expansion itself is given by "exp" while the resulting
1678 * list of divs is given by "div".
1680 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1681 __isl_take isl_mat *div, int *exp)
1683 int old_n_div;
1684 int new_n_div;
1685 int offset;
1687 aff = isl_aff_cow(aff);
1688 if (!aff || !div)
1689 goto error;
1691 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1692 new_n_div = isl_mat_rows(div);
1693 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1695 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1696 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1697 if (!aff->v || !aff->ls)
1698 return isl_aff_free(aff);
1699 return aff;
1700 error:
1701 isl_aff_free(aff);
1702 isl_mat_free(div);
1703 return NULL;
1706 /* Add two affine expressions that live in the same local space.
1708 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1709 __isl_take isl_aff *aff2)
1711 isl_int gcd, f;
1713 aff1 = isl_aff_cow(aff1);
1714 if (!aff1 || !aff2)
1715 goto error;
1717 aff1->v = isl_vec_cow(aff1->v);
1718 if (!aff1->v)
1719 goto error;
1721 isl_int_init(gcd);
1722 isl_int_init(f);
1723 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1724 isl_int_divexact(f, aff2->v->el[0], gcd);
1725 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1726 isl_int_divexact(f, aff1->v->el[0], gcd);
1727 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1728 isl_int_divexact(f, aff2->v->el[0], gcd);
1729 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1730 isl_int_clear(f);
1731 isl_int_clear(gcd);
1733 isl_aff_free(aff2);
1734 return aff1;
1735 error:
1736 isl_aff_free(aff1);
1737 isl_aff_free(aff2);
1738 return NULL;
1741 /* Return the sum of "aff1" and "aff2".
1743 * If either of the two is NaN, then the result is NaN.
1745 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1746 __isl_take isl_aff *aff2)
1748 isl_ctx *ctx;
1749 int *exp1 = NULL;
1750 int *exp2 = NULL;
1751 isl_mat *div;
1752 int n_div1, n_div2;
1754 if (!aff1 || !aff2)
1755 goto error;
1757 ctx = isl_aff_get_ctx(aff1);
1758 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1759 isl_die(ctx, isl_error_invalid,
1760 "spaces don't match", goto error);
1762 if (isl_aff_is_nan(aff1)) {
1763 isl_aff_free(aff2);
1764 return aff1;
1766 if (isl_aff_is_nan(aff2)) {
1767 isl_aff_free(aff1);
1768 return aff2;
1771 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1772 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1773 if (n_div1 == 0 && n_div2 == 0)
1774 return add_expanded(aff1, aff2);
1776 exp1 = isl_alloc_array(ctx, int, n_div1);
1777 exp2 = isl_alloc_array(ctx, int, n_div2);
1778 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1779 goto error;
1781 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1782 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1783 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1784 free(exp1);
1785 free(exp2);
1787 return add_expanded(aff1, aff2);
1788 error:
1789 free(exp1);
1790 free(exp2);
1791 isl_aff_free(aff1);
1792 isl_aff_free(aff2);
1793 return NULL;
1796 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1797 __isl_take isl_aff *aff2)
1799 return isl_aff_add(aff1, isl_aff_neg(aff2));
1802 /* Return the result of scaling "aff" by a factor of "f".
1804 * As a special case, f * NaN = NaN.
1806 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1808 isl_int gcd;
1810 if (!aff)
1811 return NULL;
1812 if (isl_aff_is_nan(aff))
1813 return aff;
1815 if (isl_int_is_one(f))
1816 return aff;
1818 aff = isl_aff_cow(aff);
1819 if (!aff)
1820 return NULL;
1821 aff->v = isl_vec_cow(aff->v);
1822 if (!aff->v)
1823 return isl_aff_free(aff);
1825 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1826 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1827 return aff;
1830 isl_int_init(gcd);
1831 isl_int_gcd(gcd, aff->v->el[0], f);
1832 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1833 isl_int_divexact(gcd, f, gcd);
1834 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1835 isl_int_clear(gcd);
1837 return aff;
1840 /* Multiple "aff" by "v".
1842 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1843 __isl_take isl_val *v)
1845 if (!aff || !v)
1846 goto error;
1848 if (isl_val_is_one(v)) {
1849 isl_val_free(v);
1850 return aff;
1853 if (!isl_val_is_rat(v))
1854 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1855 "expecting rational factor", goto error);
1857 aff = isl_aff_scale(aff, v->n);
1858 aff = isl_aff_scale_down(aff, v->d);
1860 isl_val_free(v);
1861 return aff;
1862 error:
1863 isl_aff_free(aff);
1864 isl_val_free(v);
1865 return NULL;
1868 /* Return the result of scaling "aff" down by a factor of "f".
1870 * As a special case, NaN/f = NaN.
1872 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1874 isl_int gcd;
1876 if (!aff)
1877 return NULL;
1878 if (isl_aff_is_nan(aff))
1879 return aff;
1881 if (isl_int_is_one(f))
1882 return aff;
1884 aff = isl_aff_cow(aff);
1885 if (!aff)
1886 return NULL;
1888 if (isl_int_is_zero(f))
1889 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1890 "cannot scale down by zero", return isl_aff_free(aff));
1892 aff->v = isl_vec_cow(aff->v);
1893 if (!aff->v)
1894 return isl_aff_free(aff);
1896 isl_int_init(gcd);
1897 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1898 isl_int_gcd(gcd, gcd, f);
1899 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1900 isl_int_divexact(gcd, f, gcd);
1901 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1902 isl_int_clear(gcd);
1904 return aff;
1907 /* Divide "aff" by "v".
1909 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1910 __isl_take isl_val *v)
1912 if (!aff || !v)
1913 goto error;
1915 if (isl_val_is_one(v)) {
1916 isl_val_free(v);
1917 return aff;
1920 if (!isl_val_is_rat(v))
1921 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1922 "expecting rational factor", goto error);
1923 if (!isl_val_is_pos(v))
1924 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1925 "factor needs to be positive", goto error);
1927 aff = isl_aff_scale(aff, v->d);
1928 aff = isl_aff_scale_down(aff, v->n);
1930 isl_val_free(v);
1931 return aff;
1932 error:
1933 isl_aff_free(aff);
1934 isl_val_free(v);
1935 return NULL;
1938 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1940 isl_int v;
1942 if (f == 1)
1943 return aff;
1945 isl_int_init(v);
1946 isl_int_set_ui(v, f);
1947 aff = isl_aff_scale_down(aff, v);
1948 isl_int_clear(v);
1950 return aff;
1953 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1954 enum isl_dim_type type, unsigned pos, const char *s)
1956 aff = isl_aff_cow(aff);
1957 if (!aff)
1958 return NULL;
1959 if (type == isl_dim_out)
1960 isl_die(aff->v->ctx, isl_error_invalid,
1961 "cannot set name of output/set dimension",
1962 return isl_aff_free(aff));
1963 if (type == isl_dim_in)
1964 type = isl_dim_set;
1965 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1966 if (!aff->ls)
1967 return isl_aff_free(aff);
1969 return aff;
1972 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1973 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1975 aff = isl_aff_cow(aff);
1976 if (!aff)
1977 goto error;
1978 if (type == isl_dim_out)
1979 isl_die(aff->v->ctx, isl_error_invalid,
1980 "cannot set name of output/set dimension",
1981 goto error);
1982 if (type == isl_dim_in)
1983 type = isl_dim_set;
1984 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1985 if (!aff->ls)
1986 return isl_aff_free(aff);
1988 return aff;
1989 error:
1990 isl_id_free(id);
1991 isl_aff_free(aff);
1992 return NULL;
1995 /* Replace the identifier of the input tuple of "aff" by "id".
1996 * type is currently required to be equal to isl_dim_in
1998 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1999 enum isl_dim_type type, __isl_take isl_id *id)
2001 aff = isl_aff_cow(aff);
2002 if (!aff)
2003 goto error;
2004 if (type != isl_dim_in)
2005 isl_die(aff->v->ctx, isl_error_invalid,
2006 "cannot only set id of input tuple", goto error);
2007 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2008 if (!aff->ls)
2009 return isl_aff_free(aff);
2011 return aff;
2012 error:
2013 isl_id_free(id);
2014 isl_aff_free(aff);
2015 return NULL;
2018 /* Exploit the equalities in "eq" to simplify the affine expression
2019 * and the expressions of the integer divisions in the local space.
2020 * The integer divisions in this local space are assumed to appear
2021 * as regular dimensions in "eq".
2023 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2024 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2026 int i, j;
2027 unsigned total;
2028 unsigned n_div;
2030 if (!eq)
2031 goto error;
2032 if (eq->n_eq == 0) {
2033 isl_basic_set_free(eq);
2034 return aff;
2037 aff = isl_aff_cow(aff);
2038 if (!aff)
2039 goto error;
2041 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2042 isl_basic_set_copy(eq));
2043 aff->v = isl_vec_cow(aff->v);
2044 if (!aff->ls || !aff->v)
2045 goto error;
2047 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2048 n_div = eq->n_div;
2049 for (i = 0; i < eq->n_eq; ++i) {
2050 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2051 if (j < 0 || j == 0 || j >= total)
2052 continue;
2054 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2055 &aff->v->el[0]);
2058 isl_basic_set_free(eq);
2059 aff = isl_aff_normalize(aff);
2060 return aff;
2061 error:
2062 isl_basic_set_free(eq);
2063 isl_aff_free(aff);
2064 return NULL;
2067 /* Exploit the equalities in "eq" to simplify the affine expression
2068 * and the expressions of the integer divisions in the local space.
2070 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2071 __isl_take isl_basic_set *eq)
2073 int n_div;
2075 if (!aff || !eq)
2076 goto error;
2077 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2078 if (n_div > 0)
2079 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2080 return isl_aff_substitute_equalities_lifted(aff, eq);
2081 error:
2082 isl_basic_set_free(eq);
2083 isl_aff_free(aff);
2084 return NULL;
2087 /* Look for equalities among the variables shared by context and aff
2088 * and the integer divisions of aff, if any.
2089 * The equalities are then used to eliminate coefficients and/or integer
2090 * divisions from aff.
2092 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2093 __isl_take isl_set *context)
2095 isl_basic_set *hull;
2096 int n_div;
2098 if (!aff)
2099 goto error;
2100 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2101 if (n_div > 0) {
2102 isl_basic_set *bset;
2103 isl_local_space *ls;
2104 context = isl_set_add_dims(context, isl_dim_set, n_div);
2105 ls = isl_aff_get_domain_local_space(aff);
2106 bset = isl_basic_set_from_local_space(ls);
2107 bset = isl_basic_set_lift(bset);
2108 bset = isl_basic_set_flatten(bset);
2109 context = isl_set_intersect(context,
2110 isl_set_from_basic_set(bset));
2113 hull = isl_set_affine_hull(context);
2114 return isl_aff_substitute_equalities_lifted(aff, hull);
2115 error:
2116 isl_aff_free(aff);
2117 isl_set_free(context);
2118 return NULL;
2121 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2122 __isl_take isl_set *context)
2124 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2125 dom_context = isl_set_intersect_params(dom_context, context);
2126 return isl_aff_gist(aff, dom_context);
2129 /* Return a basic set containing those elements in the space
2130 * of aff where it is positive. "rational" should not be set.
2132 * If "aff" is NaN, then it is not positive.
2134 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2135 int rational)
2137 isl_constraint *ineq;
2138 isl_basic_set *bset;
2139 isl_val *c;
2141 if (!aff)
2142 return NULL;
2143 if (isl_aff_is_nan(aff)) {
2144 isl_space *space = isl_aff_get_domain_space(aff);
2145 isl_aff_free(aff);
2146 return isl_basic_set_empty(space);
2148 if (rational)
2149 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2150 "rational sets not supported", goto error);
2152 ineq = isl_inequality_from_aff(aff);
2153 c = isl_constraint_get_constant_val(ineq);
2154 c = isl_val_sub_ui(c, 1);
2155 ineq = isl_constraint_set_constant_val(ineq, c);
2157 bset = isl_basic_set_from_constraint(ineq);
2158 bset = isl_basic_set_simplify(bset);
2159 return bset;
2160 error:
2161 isl_aff_free(aff);
2162 return NULL;
2165 /* Return a basic set containing those elements in the space
2166 * of aff where it is non-negative.
2167 * If "rational" is set, then return a rational basic set.
2169 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2171 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2172 __isl_take isl_aff *aff, int rational)
2174 isl_constraint *ineq;
2175 isl_basic_set *bset;
2177 if (!aff)
2178 return NULL;
2179 if (isl_aff_is_nan(aff)) {
2180 isl_space *space = isl_aff_get_domain_space(aff);
2181 isl_aff_free(aff);
2182 return isl_basic_set_empty(space);
2185 ineq = isl_inequality_from_aff(aff);
2187 bset = isl_basic_set_from_constraint(ineq);
2188 if (rational)
2189 bset = isl_basic_set_set_rational(bset);
2190 bset = isl_basic_set_simplify(bset);
2191 return bset;
2194 /* Return a basic set containing those elements in the space
2195 * of aff where it is non-negative.
2197 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2199 return aff_nonneg_basic_set(aff, 0);
2202 /* Return a basic set containing those elements in the domain space
2203 * of "aff" where it is positive.
2205 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2207 aff = isl_aff_add_constant_num_si(aff, -1);
2208 return isl_aff_nonneg_basic_set(aff);
2211 /* Return a basic set containing those elements in the domain space
2212 * of aff where it is negative.
2214 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2216 aff = isl_aff_neg(aff);
2217 return isl_aff_pos_basic_set(aff);
2220 /* Return a basic set containing those elements in the space
2221 * of aff where it is zero.
2222 * If "rational" is set, then return a rational basic set.
2224 * If "aff" is NaN, then it is not zero.
2226 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2227 int rational)
2229 isl_constraint *ineq;
2230 isl_basic_set *bset;
2232 if (!aff)
2233 return NULL;
2234 if (isl_aff_is_nan(aff)) {
2235 isl_space *space = isl_aff_get_domain_space(aff);
2236 isl_aff_free(aff);
2237 return isl_basic_set_empty(space);
2240 ineq = isl_equality_from_aff(aff);
2242 bset = isl_basic_set_from_constraint(ineq);
2243 if (rational)
2244 bset = isl_basic_set_set_rational(bset);
2245 bset = isl_basic_set_simplify(bset);
2246 return bset;
2249 /* Return a basic set containing those elements in the space
2250 * of aff where it is zero.
2252 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2254 return aff_zero_basic_set(aff, 0);
2257 /* Return a basic set containing those elements in the shared space
2258 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2260 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2261 __isl_take isl_aff *aff2)
2263 aff1 = isl_aff_sub(aff1, aff2);
2265 return isl_aff_nonneg_basic_set(aff1);
2268 /* Return a basic set containing those elements in the shared domain space
2269 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2271 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2272 __isl_take isl_aff *aff2)
2274 aff1 = isl_aff_sub(aff1, aff2);
2276 return isl_aff_pos_basic_set(aff1);
2279 /* Return a set containing those elements in the shared space
2280 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2282 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2283 __isl_take isl_aff *aff2)
2285 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2288 /* Return a set containing those elements in the shared domain space
2289 * of aff1 and aff2 where aff1 is greater than aff2.
2291 * If either of the two inputs is NaN, then the result is empty,
2292 * as comparisons with NaN always return false.
2294 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2295 __isl_take isl_aff *aff2)
2297 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2300 /* Return a basic set containing those elements in the shared space
2301 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2303 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2304 __isl_take isl_aff *aff2)
2306 return isl_aff_ge_basic_set(aff2, aff1);
2309 /* Return a basic set containing those elements in the shared domain space
2310 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2312 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2313 __isl_take isl_aff *aff2)
2315 return isl_aff_gt_basic_set(aff2, aff1);
2318 /* Return a set containing those elements in the shared space
2319 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2321 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2322 __isl_take isl_aff *aff2)
2324 return isl_aff_ge_set(aff2, aff1);
2327 /* Return a set containing those elements in the shared domain space
2328 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2330 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2331 __isl_take isl_aff *aff2)
2333 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2336 /* Return a basic set containing those elements in the shared space
2337 * of aff1 and aff2 where aff1 and aff2 are equal.
2339 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2340 __isl_take isl_aff *aff2)
2342 aff1 = isl_aff_sub(aff1, aff2);
2344 return isl_aff_zero_basic_set(aff1);
2347 /* Return a set containing those elements in the shared space
2348 * of aff1 and aff2 where aff1 and aff2 are equal.
2350 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2351 __isl_take isl_aff *aff2)
2353 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2356 /* Return a set containing those elements in the shared domain space
2357 * of aff1 and aff2 where aff1 and aff2 are not equal.
2359 * If either of the two inputs is NaN, then the result is empty,
2360 * as comparisons with NaN always return false.
2362 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2363 __isl_take isl_aff *aff2)
2365 isl_set *set_lt, *set_gt;
2367 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2368 isl_aff_copy(aff2));
2369 set_gt = isl_aff_gt_set(aff1, aff2);
2370 return isl_set_union_disjoint(set_lt, set_gt);
2373 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2374 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2376 aff1 = isl_aff_add(aff1, aff2);
2377 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2378 return aff1;
2381 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2383 if (!aff)
2384 return -1;
2386 return 0;
2389 /* Check whether the given affine expression has non-zero coefficient
2390 * for any dimension in the given range or if any of these dimensions
2391 * appear with non-zero coefficients in any of the integer divisions
2392 * involved in the affine expression.
2394 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2395 enum isl_dim_type type, unsigned first, unsigned n)
2397 int i;
2398 isl_ctx *ctx;
2399 int *active = NULL;
2400 isl_bool involves = isl_bool_false;
2402 if (!aff)
2403 return isl_bool_error;
2404 if (n == 0)
2405 return isl_bool_false;
2407 ctx = isl_aff_get_ctx(aff);
2408 if (first + n > isl_aff_dim(aff, type))
2409 isl_die(ctx, isl_error_invalid,
2410 "range out of bounds", return isl_bool_error);
2412 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2413 if (!active)
2414 goto error;
2416 first += isl_local_space_offset(aff->ls, type) - 1;
2417 for (i = 0; i < n; ++i)
2418 if (active[first + i]) {
2419 involves = isl_bool_true;
2420 break;
2423 free(active);
2425 return involves;
2426 error:
2427 free(active);
2428 return isl_bool_error;
2431 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2432 enum isl_dim_type type, unsigned first, unsigned n)
2434 isl_ctx *ctx;
2436 if (!aff)
2437 return NULL;
2438 if (type == isl_dim_out)
2439 isl_die(aff->v->ctx, isl_error_invalid,
2440 "cannot drop output/set dimension",
2441 return isl_aff_free(aff));
2442 if (type == isl_dim_in)
2443 type = isl_dim_set;
2444 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2445 return aff;
2447 ctx = isl_aff_get_ctx(aff);
2448 if (first + n > isl_local_space_dim(aff->ls, type))
2449 isl_die(ctx, isl_error_invalid, "range out of bounds",
2450 return isl_aff_free(aff));
2452 aff = isl_aff_cow(aff);
2453 if (!aff)
2454 return NULL;
2456 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2457 if (!aff->ls)
2458 return isl_aff_free(aff);
2460 first += 1 + isl_local_space_offset(aff->ls, type);
2461 aff->v = isl_vec_drop_els(aff->v, first, n);
2462 if (!aff->v)
2463 return isl_aff_free(aff);
2465 return aff;
2468 /* Drop the "n" domain dimensions starting at "first" from "aff",
2469 * after checking that they do not appear in the affine expression.
2471 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2472 unsigned n)
2474 isl_bool involves;
2476 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2477 if (involves < 0)
2478 return isl_aff_free(aff);
2479 if (involves)
2480 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2481 "affine expression involves some of the domain dimensions",
2482 return isl_aff_free(aff));
2483 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2486 /* Project the domain of the affine expression onto its parameter space.
2487 * The affine expression may not involve any of the domain dimensions.
2489 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2491 isl_space *space;
2492 unsigned n;
2494 n = isl_aff_dim(aff, isl_dim_in);
2495 aff = drop_domain(aff, 0, n);
2496 space = isl_aff_get_domain_space(aff);
2497 space = isl_space_params(space);
2498 aff = isl_aff_reset_domain_space(aff, space);
2499 return aff;
2502 /* Check that the domain of "aff" is a product.
2504 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2506 isl_bool is_product;
2508 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2509 if (is_product < 0)
2510 return isl_stat_error;
2511 if (!is_product)
2512 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2513 "domain is not a product", return isl_stat_error);
2514 return isl_stat_ok;
2517 /* Given an affine function with a domain of the form [A -> B] that
2518 * does not depend on B, return the same function on domain A.
2520 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2522 isl_space *space;
2523 int n, n_in;
2525 if (check_domain_product(aff) < 0)
2526 return isl_aff_free(aff);
2527 space = isl_aff_get_domain_space(aff);
2528 n = isl_space_dim(space, isl_dim_set);
2529 space = isl_space_factor_domain(space);
2530 n_in = isl_space_dim(space, isl_dim_set);
2531 aff = drop_domain(aff, n_in, n - n_in);
2532 aff = isl_aff_reset_domain_space(aff, space);
2533 return aff;
2536 /* Convert an affine expression defined over a parameter domain
2537 * into one that is defined over a zero-dimensional set.
2539 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2541 isl_local_space *ls;
2543 ls = isl_aff_take_domain_local_space(aff);
2544 ls = isl_local_space_set_from_params(ls);
2545 aff = isl_aff_restore_domain_local_space(aff, ls);
2547 return aff;
2550 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2551 enum isl_dim_type type, unsigned first, unsigned n)
2553 isl_ctx *ctx;
2555 if (!aff)
2556 return NULL;
2557 if (type == isl_dim_out)
2558 isl_die(aff->v->ctx, isl_error_invalid,
2559 "cannot insert output/set dimensions",
2560 return isl_aff_free(aff));
2561 if (type == isl_dim_in)
2562 type = isl_dim_set;
2563 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2564 return aff;
2566 ctx = isl_aff_get_ctx(aff);
2567 if (first > isl_local_space_dim(aff->ls, type))
2568 isl_die(ctx, isl_error_invalid, "position out of bounds",
2569 return isl_aff_free(aff));
2571 aff = isl_aff_cow(aff);
2572 if (!aff)
2573 return NULL;
2575 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2576 if (!aff->ls)
2577 return isl_aff_free(aff);
2579 first += 1 + isl_local_space_offset(aff->ls, type);
2580 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2581 if (!aff->v)
2582 return isl_aff_free(aff);
2584 return aff;
2587 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2588 enum isl_dim_type type, unsigned n)
2590 unsigned pos;
2592 pos = isl_aff_dim(aff, type);
2594 return isl_aff_insert_dims(aff, type, pos, n);
2597 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2598 enum isl_dim_type type, unsigned n)
2600 unsigned pos;
2602 pos = isl_pw_aff_dim(pwaff, type);
2604 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2607 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2608 * to dimensions of "dst_type" at "dst_pos".
2610 * We only support moving input dimensions to parameters and vice versa.
2612 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2613 enum isl_dim_type dst_type, unsigned dst_pos,
2614 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2616 unsigned g_dst_pos;
2617 unsigned g_src_pos;
2619 if (!aff)
2620 return NULL;
2621 if (n == 0 &&
2622 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2623 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2624 return aff;
2626 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2627 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2628 "cannot move output/set dimension",
2629 return isl_aff_free(aff));
2630 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2631 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2632 "cannot move divs", return isl_aff_free(aff));
2633 if (dst_type == isl_dim_in)
2634 dst_type = isl_dim_set;
2635 if (src_type == isl_dim_in)
2636 src_type = isl_dim_set;
2638 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2639 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2640 "range out of bounds", return isl_aff_free(aff));
2641 if (dst_type == src_type)
2642 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2643 "moving dims within the same type not supported",
2644 return isl_aff_free(aff));
2646 aff = isl_aff_cow(aff);
2647 if (!aff)
2648 return NULL;
2650 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2651 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2652 if (dst_type > src_type)
2653 g_dst_pos -= n;
2655 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2656 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2657 src_type, src_pos, n);
2658 if (!aff->v || !aff->ls)
2659 return isl_aff_free(aff);
2661 aff = sort_divs(aff);
2663 return aff;
2666 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2668 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2669 return isl_pw_aff_alloc(dom, aff);
2672 #define isl_aff_involves_nan isl_aff_is_nan
2674 #undef PW
2675 #define PW isl_pw_aff
2676 #undef EL
2677 #define EL isl_aff
2678 #undef EL_IS_ZERO
2679 #define EL_IS_ZERO is_empty
2680 #undef ZERO
2681 #define ZERO empty
2682 #undef IS_ZERO
2683 #define IS_ZERO is_empty
2684 #undef FIELD
2685 #define FIELD aff
2686 #undef DEFAULT_IS_ZERO
2687 #define DEFAULT_IS_ZERO 0
2689 #define NO_OPT
2690 #define NO_LIFT
2691 #define NO_MORPH
2693 #include <isl_pw_templ.c>
2694 #include <isl_pw_eval.c>
2695 #include <isl_pw_hash.c>
2696 #include <isl_pw_union_opt.c>
2698 #undef BASE
2699 #define BASE pw_aff
2701 #include <isl_union_single.c>
2702 #include <isl_union_neg.c>
2704 static __isl_give isl_set *align_params_pw_pw_set_and(
2705 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2706 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2707 __isl_take isl_pw_aff *pwaff2))
2709 isl_bool equal_params;
2711 if (!pwaff1 || !pwaff2)
2712 goto error;
2713 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2714 if (equal_params < 0)
2715 goto error;
2716 if (equal_params)
2717 return fn(pwaff1, pwaff2);
2718 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2719 isl_pw_aff_check_named_params(pwaff2) < 0)
2720 goto error;
2721 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2722 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2723 return fn(pwaff1, pwaff2);
2724 error:
2725 isl_pw_aff_free(pwaff1);
2726 isl_pw_aff_free(pwaff2);
2727 return NULL;
2730 /* Align the parameters of the to isl_pw_aff arguments and
2731 * then apply a function "fn" on them that returns an isl_map.
2733 static __isl_give isl_map *align_params_pw_pw_map_and(
2734 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2735 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2736 __isl_take isl_pw_aff *pa2))
2738 isl_bool equal_params;
2740 if (!pa1 || !pa2)
2741 goto error;
2742 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2743 if (equal_params < 0)
2744 goto error;
2745 if (equal_params)
2746 return fn(pa1, pa2);
2747 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2748 isl_pw_aff_check_named_params(pa2) < 0)
2749 goto error;
2750 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2751 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2752 return fn(pa1, pa2);
2753 error:
2754 isl_pw_aff_free(pa1);
2755 isl_pw_aff_free(pa2);
2756 return NULL;
2759 /* Compute a piecewise quasi-affine expression with a domain that
2760 * is the union of those of pwaff1 and pwaff2 and such that on each
2761 * cell, the quasi-affine expression is the maximum of those of pwaff1
2762 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2763 * cell, then the associated expression is the defined one.
2765 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2766 __isl_take isl_pw_aff *pwaff2)
2768 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2771 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2772 __isl_take isl_pw_aff *pwaff2)
2774 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2775 &pw_aff_union_max);
2778 /* Compute a piecewise quasi-affine expression with a domain that
2779 * is the union of those of pwaff1 and pwaff2 and such that on each
2780 * cell, the quasi-affine expression is the minimum of those of pwaff1
2781 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2782 * cell, then the associated expression is the defined one.
2784 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2785 __isl_take isl_pw_aff *pwaff2)
2787 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2790 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2791 __isl_take isl_pw_aff *pwaff2)
2793 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2794 &pw_aff_union_min);
2797 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2798 __isl_take isl_pw_aff *pwaff2, int max)
2800 if (max)
2801 return isl_pw_aff_union_max(pwaff1, pwaff2);
2802 else
2803 return isl_pw_aff_union_min(pwaff1, pwaff2);
2806 /* Return a set containing those elements in the domain
2807 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2808 * does not satisfy "fn" (if complement is 1).
2810 * The pieces with a NaN never belong to the result since
2811 * NaN does not satisfy any property.
2813 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2814 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2815 int complement)
2817 int i;
2818 isl_set *set;
2820 if (!pwaff)
2821 return NULL;
2823 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2825 for (i = 0; i < pwaff->n; ++i) {
2826 isl_basic_set *bset;
2827 isl_set *set_i, *locus;
2828 isl_bool rational;
2830 if (isl_aff_is_nan(pwaff->p[i].aff))
2831 continue;
2833 rational = isl_set_has_rational(pwaff->p[i].set);
2834 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2835 locus = isl_set_from_basic_set(bset);
2836 set_i = isl_set_copy(pwaff->p[i].set);
2837 if (complement)
2838 set_i = isl_set_subtract(set_i, locus);
2839 else
2840 set_i = isl_set_intersect(set_i, locus);
2841 set = isl_set_union_disjoint(set, set_i);
2844 isl_pw_aff_free(pwaff);
2846 return set;
2849 /* Return a set containing those elements in the domain
2850 * of "pa" where it is positive.
2852 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2854 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2857 /* Return a set containing those elements in the domain
2858 * of pwaff where it is non-negative.
2860 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2862 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2865 /* Return a set containing those elements in the domain
2866 * of pwaff where it is zero.
2868 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2870 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2873 /* Return a set containing those elements in the domain
2874 * of pwaff where it is not zero.
2876 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2878 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2881 /* Return a set containing those elements in the shared domain
2882 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2884 * We compute the difference on the shared domain and then construct
2885 * the set of values where this difference is non-negative.
2886 * If strict is set, we first subtract 1 from the difference.
2887 * If equal is set, we only return the elements where pwaff1 and pwaff2
2888 * are equal.
2890 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2891 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2893 isl_set *set1, *set2;
2895 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2896 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2897 set1 = isl_set_intersect(set1, set2);
2898 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2899 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2900 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2902 if (strict) {
2903 isl_space *space = isl_set_get_space(set1);
2904 isl_aff *aff;
2905 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2906 aff = isl_aff_add_constant_si(aff, -1);
2907 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2908 } else
2909 isl_set_free(set1);
2911 if (equal)
2912 return isl_pw_aff_zero_set(pwaff1);
2913 return isl_pw_aff_nonneg_set(pwaff1);
2916 /* Return a set containing those elements in the shared domain
2917 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2919 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2920 __isl_take isl_pw_aff *pwaff2)
2922 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2925 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2926 __isl_take isl_pw_aff *pwaff2)
2928 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2931 /* Return a set containing those elements in the shared domain
2932 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2934 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2935 __isl_take isl_pw_aff *pwaff2)
2937 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2940 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2941 __isl_take isl_pw_aff *pwaff2)
2943 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2946 /* Return a set containing those elements in the shared domain
2947 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2949 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2950 __isl_take isl_pw_aff *pwaff2)
2952 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2955 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2956 __isl_take isl_pw_aff *pwaff2)
2958 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2961 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2962 __isl_take isl_pw_aff *pwaff2)
2964 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2967 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2968 __isl_take isl_pw_aff *pwaff2)
2970 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2973 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2974 * where the function values are ordered in the same way as "order",
2975 * which returns a set in the shared domain of its two arguments.
2976 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2978 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2979 * We first pull back the two functions such that they are defined on
2980 * the domain [A -> B]. Then we apply "order", resulting in a set
2981 * in the space [A -> B]. Finally, we unwrap this set to obtain
2982 * a map in the space A -> B.
2984 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2985 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2986 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2987 __isl_take isl_pw_aff *pa2))
2989 isl_space *space1, *space2;
2990 isl_multi_aff *ma;
2991 isl_set *set;
2993 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2994 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2995 space1 = isl_space_map_from_domain_and_range(space1, space2);
2996 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2997 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2998 ma = isl_multi_aff_range_map(space1);
2999 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3000 set = order(pa1, pa2);
3002 return isl_set_unwrap(set);
3005 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3006 * where the function values are equal.
3007 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3009 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3010 __isl_take isl_pw_aff *pa2)
3012 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3015 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3016 * where the function values are equal.
3018 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3019 __isl_take isl_pw_aff *pa2)
3021 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3024 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3025 * where the function value of "pa1" is less than the function value of "pa2".
3026 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3028 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3029 __isl_take isl_pw_aff *pa2)
3031 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3034 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3035 * where the function value of "pa1" is less than the function value of "pa2".
3037 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3038 __isl_take isl_pw_aff *pa2)
3040 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3043 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3044 * where the function value of "pa1" is greater than the function value
3045 * of "pa2".
3046 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3048 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3049 __isl_take isl_pw_aff *pa2)
3051 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3054 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3055 * where the function value of "pa1" is greater than the function value
3056 * of "pa2".
3058 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3059 __isl_take isl_pw_aff *pa2)
3061 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3064 /* Return a set containing those elements in the shared domain
3065 * of the elements of list1 and list2 where each element in list1
3066 * has the relation specified by "fn" with each element in list2.
3068 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3069 __isl_take isl_pw_aff_list *list2,
3070 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3071 __isl_take isl_pw_aff *pwaff2))
3073 int i, j;
3074 isl_ctx *ctx;
3075 isl_set *set;
3077 if (!list1 || !list2)
3078 goto error;
3080 ctx = isl_pw_aff_list_get_ctx(list1);
3081 if (list1->n < 1 || list2->n < 1)
3082 isl_die(ctx, isl_error_invalid,
3083 "list should contain at least one element", goto error);
3085 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3086 for (i = 0; i < list1->n; ++i)
3087 for (j = 0; j < list2->n; ++j) {
3088 isl_set *set_ij;
3090 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3091 isl_pw_aff_copy(list2->p[j]));
3092 set = isl_set_intersect(set, set_ij);
3095 isl_pw_aff_list_free(list1);
3096 isl_pw_aff_list_free(list2);
3097 return set;
3098 error:
3099 isl_pw_aff_list_free(list1);
3100 isl_pw_aff_list_free(list2);
3101 return NULL;
3104 /* Return a set containing those elements in the shared domain
3105 * of the elements of list1 and list2 where each element in list1
3106 * is equal to each element in list2.
3108 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3109 __isl_take isl_pw_aff_list *list2)
3111 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3114 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3115 __isl_take isl_pw_aff_list *list2)
3117 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3120 /* Return a set containing those elements in the shared domain
3121 * of the elements of list1 and list2 where each element in list1
3122 * is less than or equal to each element in list2.
3124 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3125 __isl_take isl_pw_aff_list *list2)
3127 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3130 __isl_give isl_set *isl_pw_aff_list_lt_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_lt_set);
3136 __isl_give isl_set *isl_pw_aff_list_ge_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_ge_set);
3142 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3143 __isl_take isl_pw_aff_list *list2)
3145 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3149 /* Return a set containing those elements in the shared domain
3150 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3152 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3153 __isl_take isl_pw_aff *pwaff2)
3155 isl_set *set_lt, *set_gt;
3157 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3158 isl_pw_aff_copy(pwaff2));
3159 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3160 return isl_set_union_disjoint(set_lt, set_gt);
3163 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3164 __isl_take isl_pw_aff *pwaff2)
3166 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3169 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3170 isl_int v)
3172 int i;
3174 if (isl_int_is_one(v))
3175 return pwaff;
3176 if (!isl_int_is_pos(v))
3177 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3178 "factor needs to be positive",
3179 return isl_pw_aff_free(pwaff));
3180 pwaff = isl_pw_aff_cow(pwaff);
3181 if (!pwaff)
3182 return NULL;
3183 if (pwaff->n == 0)
3184 return pwaff;
3186 for (i = 0; i < pwaff->n; ++i) {
3187 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3188 if (!pwaff->p[i].aff)
3189 return isl_pw_aff_free(pwaff);
3192 return pwaff;
3195 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3197 int i;
3199 pwaff = isl_pw_aff_cow(pwaff);
3200 if (!pwaff)
3201 return NULL;
3202 if (pwaff->n == 0)
3203 return pwaff;
3205 for (i = 0; i < pwaff->n; ++i) {
3206 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3207 if (!pwaff->p[i].aff)
3208 return isl_pw_aff_free(pwaff);
3211 return pwaff;
3214 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3216 int i;
3218 pwaff = isl_pw_aff_cow(pwaff);
3219 if (!pwaff)
3220 return NULL;
3221 if (pwaff->n == 0)
3222 return pwaff;
3224 for (i = 0; i < pwaff->n; ++i) {
3225 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3226 if (!pwaff->p[i].aff)
3227 return isl_pw_aff_free(pwaff);
3230 return pwaff;
3233 /* Assuming that "cond1" and "cond2" are disjoint,
3234 * return an affine expression that is equal to pwaff1 on cond1
3235 * and to pwaff2 on cond2.
3237 static __isl_give isl_pw_aff *isl_pw_aff_select(
3238 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3239 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3241 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3242 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3244 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3247 /* Return an affine expression that is equal to pwaff_true for elements
3248 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3249 * is zero.
3250 * That is, return cond ? pwaff_true : pwaff_false;
3252 * If "cond" involves and NaN, then we conservatively return a NaN
3253 * on its entire domain. In principle, we could consider the pieces
3254 * where it is NaN separately from those where it is not.
3256 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3257 * then only use the domain of "cond" to restrict the domain.
3259 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3260 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3262 isl_set *cond_true, *cond_false;
3263 isl_bool equal;
3265 if (!cond)
3266 goto error;
3267 if (isl_pw_aff_involves_nan(cond)) {
3268 isl_space *space = isl_pw_aff_get_domain_space(cond);
3269 isl_local_space *ls = isl_local_space_from_space(space);
3270 isl_pw_aff_free(cond);
3271 isl_pw_aff_free(pwaff_true);
3272 isl_pw_aff_free(pwaff_false);
3273 return isl_pw_aff_nan_on_domain(ls);
3276 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3277 isl_pw_aff_get_space(pwaff_false));
3278 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3279 isl_pw_aff_get_space(pwaff_true));
3280 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3281 if (equal < 0)
3282 goto error;
3283 if (equal) {
3284 isl_set *dom;
3286 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3287 isl_pw_aff_free(pwaff_false);
3288 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3291 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3292 cond_false = isl_pw_aff_zero_set(cond);
3293 return isl_pw_aff_select(cond_true, pwaff_true,
3294 cond_false, pwaff_false);
3295 error:
3296 isl_pw_aff_free(cond);
3297 isl_pw_aff_free(pwaff_true);
3298 isl_pw_aff_free(pwaff_false);
3299 return NULL;
3302 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3304 if (!aff)
3305 return isl_bool_error;
3307 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3310 /* Check whether pwaff is a piecewise constant.
3312 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3314 int i;
3316 if (!pwaff)
3317 return isl_bool_error;
3319 for (i = 0; i < pwaff->n; ++i) {
3320 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3321 if (is_cst < 0 || !is_cst)
3322 return is_cst;
3325 return isl_bool_true;
3328 /* Are all elements of "mpa" piecewise constants?
3330 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3332 int i;
3334 if (!mpa)
3335 return isl_bool_error;
3337 for (i = 0; i < mpa->n; ++i) {
3338 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3339 if (is_cst < 0 || !is_cst)
3340 return is_cst;
3343 return isl_bool_true;
3346 /* Return the product of "aff1" and "aff2".
3348 * If either of the two is NaN, then the result is NaN.
3350 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3352 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3353 __isl_take isl_aff *aff2)
3355 if (!aff1 || !aff2)
3356 goto error;
3358 if (isl_aff_is_nan(aff1)) {
3359 isl_aff_free(aff2);
3360 return aff1;
3362 if (isl_aff_is_nan(aff2)) {
3363 isl_aff_free(aff1);
3364 return aff2;
3367 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3368 return isl_aff_mul(aff2, aff1);
3370 if (!isl_aff_is_cst(aff2))
3371 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3372 "at least one affine expression should be constant",
3373 goto error);
3375 aff1 = isl_aff_cow(aff1);
3376 if (!aff1 || !aff2)
3377 goto error;
3379 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3380 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3382 isl_aff_free(aff2);
3383 return aff1;
3384 error:
3385 isl_aff_free(aff1);
3386 isl_aff_free(aff2);
3387 return NULL;
3390 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3392 * If either of the two is NaN, then the result is NaN.
3394 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3395 __isl_take isl_aff *aff2)
3397 int is_cst;
3398 int neg;
3400 if (!aff1 || !aff2)
3401 goto error;
3403 if (isl_aff_is_nan(aff1)) {
3404 isl_aff_free(aff2);
3405 return aff1;
3407 if (isl_aff_is_nan(aff2)) {
3408 isl_aff_free(aff1);
3409 return aff2;
3412 is_cst = isl_aff_is_cst(aff2);
3413 if (is_cst < 0)
3414 goto error;
3415 if (!is_cst)
3416 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3417 "second argument should be a constant", goto error);
3419 if (!aff2)
3420 goto error;
3422 neg = isl_int_is_neg(aff2->v->el[1]);
3423 if (neg) {
3424 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3425 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3428 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3429 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3431 if (neg) {
3432 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3433 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3436 isl_aff_free(aff2);
3437 return aff1;
3438 error:
3439 isl_aff_free(aff1);
3440 isl_aff_free(aff2);
3441 return NULL;
3444 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3445 __isl_take isl_pw_aff *pwaff2)
3447 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3450 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3451 __isl_take isl_pw_aff *pwaff2)
3453 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3456 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3457 __isl_take isl_pw_aff *pwaff2)
3459 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3462 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3463 __isl_take isl_pw_aff *pwaff2)
3465 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3468 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3469 __isl_take isl_pw_aff *pwaff2)
3471 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3474 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3475 __isl_take isl_pw_aff *pa2)
3477 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3480 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3482 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3483 __isl_take isl_pw_aff *pa2)
3485 int is_cst;
3487 is_cst = isl_pw_aff_is_cst(pa2);
3488 if (is_cst < 0)
3489 goto error;
3490 if (!is_cst)
3491 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3492 "second argument should be a piecewise constant",
3493 goto error);
3494 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3495 error:
3496 isl_pw_aff_free(pa1);
3497 isl_pw_aff_free(pa2);
3498 return NULL;
3501 /* Compute the quotient of the integer division of "pa1" by "pa2"
3502 * with rounding towards zero.
3503 * "pa2" is assumed to be a piecewise constant.
3505 * In particular, return
3507 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3510 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3511 __isl_take isl_pw_aff *pa2)
3513 int is_cst;
3514 isl_set *cond;
3515 isl_pw_aff *f, *c;
3517 is_cst = isl_pw_aff_is_cst(pa2);
3518 if (is_cst < 0)
3519 goto error;
3520 if (!is_cst)
3521 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3522 "second argument should be a piecewise constant",
3523 goto error);
3525 pa1 = isl_pw_aff_div(pa1, pa2);
3527 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3528 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3529 c = isl_pw_aff_ceil(pa1);
3530 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3531 error:
3532 isl_pw_aff_free(pa1);
3533 isl_pw_aff_free(pa2);
3534 return NULL;
3537 /* Compute the remainder of the integer division of "pa1" by "pa2"
3538 * with rounding towards zero.
3539 * "pa2" is assumed to be a piecewise constant.
3541 * In particular, return
3543 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3546 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3547 __isl_take isl_pw_aff *pa2)
3549 int is_cst;
3550 isl_pw_aff *res;
3552 is_cst = isl_pw_aff_is_cst(pa2);
3553 if (is_cst < 0)
3554 goto error;
3555 if (!is_cst)
3556 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3557 "second argument should be a piecewise constant",
3558 goto error);
3559 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3560 res = isl_pw_aff_mul(pa2, res);
3561 res = isl_pw_aff_sub(pa1, res);
3562 return res;
3563 error:
3564 isl_pw_aff_free(pa1);
3565 isl_pw_aff_free(pa2);
3566 return NULL;
3569 /* Does either of "pa1" or "pa2" involve any NaN2?
3571 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3572 __isl_keep isl_pw_aff *pa2)
3574 isl_bool has_nan;
3576 has_nan = isl_pw_aff_involves_nan(pa1);
3577 if (has_nan < 0 || has_nan)
3578 return has_nan;
3579 return isl_pw_aff_involves_nan(pa2);
3582 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3583 * by a NaN on their shared domain.
3585 * In principle, the result could be refined to only being NaN
3586 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3588 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3589 __isl_take isl_pw_aff *pa2)
3591 isl_local_space *ls;
3592 isl_set *dom;
3593 isl_pw_aff *pa;
3595 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3596 ls = isl_local_space_from_space(isl_set_get_space(dom));
3597 pa = isl_pw_aff_nan_on_domain(ls);
3598 pa = isl_pw_aff_intersect_domain(pa, dom);
3600 return pa;
3603 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3604 __isl_take isl_pw_aff *pwaff2)
3606 isl_set *le;
3607 isl_set *dom;
3609 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3610 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3611 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3612 isl_pw_aff_copy(pwaff2));
3613 dom = isl_set_subtract(dom, isl_set_copy(le));
3614 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3617 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3618 __isl_take isl_pw_aff *pwaff2)
3620 isl_set *ge;
3621 isl_set *dom;
3623 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3624 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3625 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3626 isl_pw_aff_copy(pwaff2));
3627 dom = isl_set_subtract(dom, isl_set_copy(ge));
3628 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3631 /* Return an expression for the minimum (if "max" is not set) or
3632 * the maximum (if "max" is set) of "pa1" and "pa2".
3633 * If either expression involves any NaN, then return a NaN
3634 * on the shared domain as result.
3636 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3637 __isl_take isl_pw_aff *pa2, int max)
3639 isl_bool has_nan;
3641 has_nan = either_involves_nan(pa1, pa2);
3642 if (has_nan < 0)
3643 pa1 = isl_pw_aff_free(pa1);
3644 else if (has_nan)
3645 return replace_by_nan(pa1, pa2);
3647 if (max)
3648 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3649 else
3650 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3653 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3655 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3656 __isl_take isl_pw_aff *pwaff2)
3658 return pw_aff_min_max(pwaff1, pwaff2, 0);
3661 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3663 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3664 __isl_take isl_pw_aff *pwaff2)
3666 return pw_aff_min_max(pwaff1, pwaff2, 1);
3669 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3670 __isl_take isl_pw_aff_list *list,
3671 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3672 __isl_take isl_pw_aff *pwaff2))
3674 int i;
3675 isl_ctx *ctx;
3676 isl_pw_aff *res;
3678 if (!list)
3679 return NULL;
3681 ctx = isl_pw_aff_list_get_ctx(list);
3682 if (list->n < 1)
3683 isl_die(ctx, isl_error_invalid,
3684 "list should contain at least one element", goto error);
3686 res = isl_pw_aff_copy(list->p[0]);
3687 for (i = 1; i < list->n; ++i)
3688 res = fn(res, isl_pw_aff_copy(list->p[i]));
3690 isl_pw_aff_list_free(list);
3691 return res;
3692 error:
3693 isl_pw_aff_list_free(list);
3694 return NULL;
3697 /* Return an isl_pw_aff that maps each element in the intersection of the
3698 * domains of the elements of list to the minimal corresponding affine
3699 * expression.
3701 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3703 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3706 /* Return an isl_pw_aff that maps each element in the intersection of the
3707 * domains of the elements of list to the maximal corresponding affine
3708 * expression.
3710 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3712 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3715 /* Mark the domains of "pwaff" as rational.
3717 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3719 int i;
3721 pwaff = isl_pw_aff_cow(pwaff);
3722 if (!pwaff)
3723 return NULL;
3724 if (pwaff->n == 0)
3725 return pwaff;
3727 for (i = 0; i < pwaff->n; ++i) {
3728 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3729 if (!pwaff->p[i].set)
3730 return isl_pw_aff_free(pwaff);
3733 return pwaff;
3736 /* Mark the domains of the elements of "list" as rational.
3738 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3739 __isl_take isl_pw_aff_list *list)
3741 int i, n;
3743 if (!list)
3744 return NULL;
3745 if (list->n == 0)
3746 return list;
3748 n = list->n;
3749 for (i = 0; i < n; ++i) {
3750 isl_pw_aff *pa;
3752 pa = isl_pw_aff_list_get_pw_aff(list, i);
3753 pa = isl_pw_aff_set_rational(pa);
3754 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3757 return list;
3760 /* Do the parameters of "aff" match those of "space"?
3762 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3763 __isl_keep isl_space *space)
3765 isl_space *aff_space;
3766 isl_bool match;
3768 if (!aff || !space)
3769 return isl_bool_error;
3771 aff_space = isl_aff_get_domain_space(aff);
3773 match = isl_space_has_equal_params(space, aff_space);
3775 isl_space_free(aff_space);
3776 return match;
3779 /* Check that the domain space of "aff" matches "space".
3781 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3782 __isl_keep isl_space *space)
3784 isl_space *aff_space;
3785 isl_bool match;
3787 if (!aff || !space)
3788 return isl_stat_error;
3790 aff_space = isl_aff_get_domain_space(aff);
3792 match = isl_space_has_equal_params(space, aff_space);
3793 if (match < 0)
3794 goto error;
3795 if (!match)
3796 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3797 "parameters don't match", goto error);
3798 match = isl_space_tuple_is_equal(space, isl_dim_in,
3799 aff_space, isl_dim_set);
3800 if (match < 0)
3801 goto error;
3802 if (!match)
3803 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3804 "domains don't match", goto error);
3805 isl_space_free(aff_space);
3806 return isl_stat_ok;
3807 error:
3808 isl_space_free(aff_space);
3809 return isl_stat_error;
3812 #undef BASE
3813 #define BASE aff
3814 #undef DOMBASE
3815 #define DOMBASE set
3817 #include <isl_multi_no_explicit_domain.c>
3818 #include <isl_multi_templ.c>
3819 #include <isl_multi_apply_set.c>
3820 #include <isl_multi_cmp.c>
3821 #include <isl_multi_dims.c>
3822 #include <isl_multi_floor.c>
3823 #include <isl_multi_from_base_templ.c>
3824 #include <isl_multi_gist.c>
3825 #include <isl_multi_identity_templ.c>
3826 #include <isl_multi_move_dims_templ.c>
3827 #include <isl_multi_product_templ.c>
3828 #include <isl_multi_splice_templ.c>
3829 #include <isl_multi_zero_templ.c>
3831 /* Construct an isl_multi_aff living in "space" that corresponds
3832 * to the affine transformation matrix "mat".
3834 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3835 __isl_take isl_space *space, __isl_take isl_mat *mat)
3837 isl_ctx *ctx;
3838 isl_local_space *ls = NULL;
3839 isl_multi_aff *ma = NULL;
3840 int n_row, n_col, n_out, total;
3841 int i;
3843 if (!space || !mat)
3844 goto error;
3846 ctx = isl_mat_get_ctx(mat);
3848 n_row = isl_mat_rows(mat);
3849 n_col = isl_mat_cols(mat);
3850 if (n_row < 1)
3851 isl_die(ctx, isl_error_invalid,
3852 "insufficient number of rows", goto error);
3853 if (n_col < 1)
3854 isl_die(ctx, isl_error_invalid,
3855 "insufficient number of columns", goto error);
3856 n_out = isl_space_dim(space, isl_dim_out);
3857 total = isl_space_dim(space, isl_dim_all);
3858 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3859 isl_die(ctx, isl_error_invalid,
3860 "dimension mismatch", goto error);
3862 ma = isl_multi_aff_zero(isl_space_copy(space));
3863 ls = isl_local_space_from_space(isl_space_domain(space));
3865 for (i = 0; i < n_row - 1; ++i) {
3866 isl_vec *v;
3867 isl_aff *aff;
3869 v = isl_vec_alloc(ctx, 1 + n_col);
3870 if (!v)
3871 goto error;
3872 isl_int_set(v->el[0], mat->row[0][0]);
3873 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3874 v = isl_vec_normalize(v);
3875 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3876 ma = isl_multi_aff_set_aff(ma, i, aff);
3879 isl_local_space_free(ls);
3880 isl_mat_free(mat);
3881 return ma;
3882 error:
3883 isl_local_space_free(ls);
3884 isl_mat_free(mat);
3885 isl_multi_aff_free(ma);
3886 return NULL;
3889 /* Remove any internal structure of the domain of "ma".
3890 * If there is any such internal structure in the input,
3891 * then the name of the corresponding space is also removed.
3893 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3894 __isl_take isl_multi_aff *ma)
3896 isl_space *space;
3898 if (!ma)
3899 return NULL;
3901 if (!ma->space->nested[0])
3902 return ma;
3904 space = isl_multi_aff_get_space(ma);
3905 space = isl_space_flatten_domain(space);
3906 ma = isl_multi_aff_reset_space(ma, space);
3908 return ma;
3911 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3912 * of the space to its domain.
3914 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3916 int i, n_in;
3917 isl_local_space *ls;
3918 isl_multi_aff *ma;
3920 if (!space)
3921 return NULL;
3922 if (!isl_space_is_map(space))
3923 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3924 "not a map space", goto error);
3926 n_in = isl_space_dim(space, isl_dim_in);
3927 space = isl_space_domain_map(space);
3929 ma = isl_multi_aff_alloc(isl_space_copy(space));
3930 if (n_in == 0) {
3931 isl_space_free(space);
3932 return ma;
3935 space = isl_space_domain(space);
3936 ls = isl_local_space_from_space(space);
3937 for (i = 0; i < n_in; ++i) {
3938 isl_aff *aff;
3940 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3941 isl_dim_set, i);
3942 ma = isl_multi_aff_set_aff(ma, i, aff);
3944 isl_local_space_free(ls);
3945 return ma;
3946 error:
3947 isl_space_free(space);
3948 return NULL;
3951 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3952 * of the space to its range.
3954 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3956 int i, n_in, n_out;
3957 isl_local_space *ls;
3958 isl_multi_aff *ma;
3960 if (!space)
3961 return NULL;
3962 if (!isl_space_is_map(space))
3963 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3964 "not a map space", goto error);
3966 n_in = isl_space_dim(space, isl_dim_in);
3967 n_out = isl_space_dim(space, isl_dim_out);
3968 space = isl_space_range_map(space);
3970 ma = isl_multi_aff_alloc(isl_space_copy(space));
3971 if (n_out == 0) {
3972 isl_space_free(space);
3973 return ma;
3976 space = isl_space_domain(space);
3977 ls = isl_local_space_from_space(space);
3978 for (i = 0; i < n_out; ++i) {
3979 isl_aff *aff;
3981 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3982 isl_dim_set, n_in + i);
3983 ma = isl_multi_aff_set_aff(ma, i, aff);
3985 isl_local_space_free(ls);
3986 return ma;
3987 error:
3988 isl_space_free(space);
3989 return NULL;
3992 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3993 * of the space to its range.
3995 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3996 __isl_take isl_space *space)
3998 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4001 /* Given the space of a set and a range of set dimensions,
4002 * construct an isl_multi_aff that projects out those dimensions.
4004 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4005 __isl_take isl_space *space, enum isl_dim_type type,
4006 unsigned first, unsigned n)
4008 int i, dim;
4009 isl_local_space *ls;
4010 isl_multi_aff *ma;
4012 if (!space)
4013 return NULL;
4014 if (!isl_space_is_set(space))
4015 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4016 "expecting set space", goto error);
4017 if (type != isl_dim_set)
4018 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4019 "only set dimensions can be projected out", goto error);
4021 dim = isl_space_dim(space, isl_dim_set);
4022 if (first + n > dim)
4023 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4024 "range out of bounds", goto error);
4026 space = isl_space_from_domain(space);
4027 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4029 if (dim == n)
4030 return isl_multi_aff_alloc(space);
4032 ma = isl_multi_aff_alloc(isl_space_copy(space));
4033 space = isl_space_domain(space);
4034 ls = isl_local_space_from_space(space);
4036 for (i = 0; i < first; ++i) {
4037 isl_aff *aff;
4039 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4040 isl_dim_set, i);
4041 ma = isl_multi_aff_set_aff(ma, i, aff);
4044 for (i = 0; i < dim - (first + n); ++i) {
4045 isl_aff *aff;
4047 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4048 isl_dim_set, first + n + i);
4049 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4052 isl_local_space_free(ls);
4053 return ma;
4054 error:
4055 isl_space_free(space);
4056 return NULL;
4059 /* Given the space of a set and a range of set dimensions,
4060 * construct an isl_pw_multi_aff that projects out those dimensions.
4062 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4063 __isl_take isl_space *space, enum isl_dim_type type,
4064 unsigned first, unsigned n)
4066 isl_multi_aff *ma;
4068 ma = isl_multi_aff_project_out_map(space, type, first, n);
4069 return isl_pw_multi_aff_from_multi_aff(ma);
4072 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4073 * domain.
4075 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4076 __isl_take isl_multi_aff *ma)
4078 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4079 return isl_pw_multi_aff_alloc(dom, ma);
4082 /* Create a piecewise multi-affine expression in the given space that maps each
4083 * input dimension to the corresponding output dimension.
4085 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4086 __isl_take isl_space *space)
4088 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4091 /* Exploit the equalities in "eq" to simplify the affine expressions.
4093 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4094 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4096 int i;
4098 maff = isl_multi_aff_cow(maff);
4099 if (!maff || !eq)
4100 goto error;
4102 for (i = 0; i < maff->n; ++i) {
4103 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4104 isl_basic_set_copy(eq));
4105 if (!maff->u.p[i])
4106 goto error;
4109 isl_basic_set_free(eq);
4110 return maff;
4111 error:
4112 isl_basic_set_free(eq);
4113 isl_multi_aff_free(maff);
4114 return NULL;
4117 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4118 isl_int f)
4120 int i;
4122 maff = isl_multi_aff_cow(maff);
4123 if (!maff)
4124 return NULL;
4126 for (i = 0; i < maff->n; ++i) {
4127 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4128 if (!maff->u.p[i])
4129 return isl_multi_aff_free(maff);
4132 return maff;
4135 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4136 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4138 maff1 = isl_multi_aff_add(maff1, maff2);
4139 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4140 return maff1;
4143 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4145 if (!maff)
4146 return -1;
4148 return 0;
4151 /* Return the set of domain elements where "ma1" is lexicographically
4152 * smaller than or equal to "ma2".
4154 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4155 __isl_take isl_multi_aff *ma2)
4157 return isl_multi_aff_lex_ge_set(ma2, ma1);
4160 /* Return the set of domain elements where "ma1" is lexicographically
4161 * smaller than "ma2".
4163 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4164 __isl_take isl_multi_aff *ma2)
4166 return isl_multi_aff_lex_gt_set(ma2, ma1);
4169 /* Return the set of domain elements where "ma1" and "ma2"
4170 * satisfy "order".
4172 static __isl_give isl_set *isl_multi_aff_order_set(
4173 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4174 __isl_give isl_map *order(__isl_take isl_space *set_space))
4176 isl_space *space;
4177 isl_map *map1, *map2;
4178 isl_map *map, *ge;
4180 map1 = isl_map_from_multi_aff_internal(ma1);
4181 map2 = isl_map_from_multi_aff_internal(ma2);
4182 map = isl_map_range_product(map1, map2);
4183 space = isl_space_range(isl_map_get_space(map));
4184 space = isl_space_domain(isl_space_unwrap(space));
4185 ge = order(space);
4186 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4188 return isl_map_domain(map);
4191 /* Return the set of domain elements where "ma1" is lexicographically
4192 * greater than or equal to "ma2".
4194 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4195 __isl_take isl_multi_aff *ma2)
4197 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4200 /* Return the set of domain elements where "ma1" is lexicographically
4201 * greater than "ma2".
4203 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4204 __isl_take isl_multi_aff *ma2)
4206 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4209 #undef PW
4210 #define PW isl_pw_multi_aff
4211 #undef EL
4212 #define EL isl_multi_aff
4213 #undef EL_IS_ZERO
4214 #define EL_IS_ZERO is_empty
4215 #undef ZERO
4216 #define ZERO empty
4217 #undef IS_ZERO
4218 #define IS_ZERO is_empty
4219 #undef FIELD
4220 #define FIELD maff
4221 #undef DEFAULT_IS_ZERO
4222 #define DEFAULT_IS_ZERO 0
4224 #define NO_SUB
4225 #define NO_OPT
4226 #define NO_INSERT_DIMS
4227 #define NO_LIFT
4228 #define NO_MORPH
4230 #include <isl_pw_templ.c>
4231 #include <isl_pw_union_opt.c>
4233 #undef NO_SUB
4235 #undef BASE
4236 #define BASE pw_multi_aff
4238 #include <isl_union_multi.c>
4239 #include <isl_union_neg.c>
4241 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4242 __isl_take isl_pw_multi_aff *pma1,
4243 __isl_take isl_pw_multi_aff *pma2)
4245 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4246 &isl_multi_aff_lex_ge_set);
4249 /* Given two piecewise multi affine expressions, return a piecewise
4250 * multi-affine expression defined on the union of the definition domains
4251 * of the inputs that is equal to the lexicographic maximum of the two
4252 * inputs on each cell. If only one of the two inputs is defined on
4253 * a given cell, then it is considered to be the maximum.
4255 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4256 __isl_take isl_pw_multi_aff *pma1,
4257 __isl_take isl_pw_multi_aff *pma2)
4259 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4260 &pw_multi_aff_union_lexmax);
4263 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4264 __isl_take isl_pw_multi_aff *pma1,
4265 __isl_take isl_pw_multi_aff *pma2)
4267 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4268 &isl_multi_aff_lex_le_set);
4271 /* Given two piecewise multi affine expressions, return a piecewise
4272 * multi-affine expression defined on the union of the definition domains
4273 * of the inputs that is equal to the lexicographic minimum of the two
4274 * inputs on each cell. If only one of the two inputs is defined on
4275 * a given cell, then it is considered to be the minimum.
4277 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4278 __isl_take isl_pw_multi_aff *pma1,
4279 __isl_take isl_pw_multi_aff *pma2)
4281 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4282 &pw_multi_aff_union_lexmin);
4285 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4286 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4288 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4289 &isl_multi_aff_add);
4292 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4293 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4295 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4296 &pw_multi_aff_add);
4299 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4300 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4302 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4303 &isl_multi_aff_sub);
4306 /* Subtract "pma2" from "pma1" and return the result.
4308 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4309 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4311 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4312 &pw_multi_aff_sub);
4315 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4316 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4318 return isl_pw_multi_aff_union_add_(pma1, pma2);
4321 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4322 * with the actual sum on the shared domain and
4323 * the defined expression on the symmetric difference of the domains.
4325 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4326 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4328 return isl_union_pw_aff_union_add_(upa1, upa2);
4331 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4332 * with the actual sum on the shared domain and
4333 * the defined expression on the symmetric difference of the domains.
4335 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4336 __isl_take isl_union_pw_multi_aff *upma1,
4337 __isl_take isl_union_pw_multi_aff *upma2)
4339 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4342 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4343 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4345 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4346 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4348 int i, j, n;
4349 isl_space *space;
4350 isl_pw_multi_aff *res;
4352 if (!pma1 || !pma2)
4353 goto error;
4355 n = pma1->n * pma2->n;
4356 space = isl_space_product(isl_space_copy(pma1->dim),
4357 isl_space_copy(pma2->dim));
4358 res = isl_pw_multi_aff_alloc_size(space, n);
4360 for (i = 0; i < pma1->n; ++i) {
4361 for (j = 0; j < pma2->n; ++j) {
4362 isl_set *domain;
4363 isl_multi_aff *ma;
4365 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4366 isl_set_copy(pma2->p[j].set));
4367 ma = isl_multi_aff_product(
4368 isl_multi_aff_copy(pma1->p[i].maff),
4369 isl_multi_aff_copy(pma2->p[j].maff));
4370 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4374 isl_pw_multi_aff_free(pma1);
4375 isl_pw_multi_aff_free(pma2);
4376 return res;
4377 error:
4378 isl_pw_multi_aff_free(pma1);
4379 isl_pw_multi_aff_free(pma2);
4380 return NULL;
4383 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4384 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4386 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4387 &pw_multi_aff_product);
4390 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4391 * denominator "denom".
4392 * "denom" is allowed to be negative, in which case the actual denominator
4393 * is -denom and the expressions are added instead.
4395 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4396 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4398 int i, first;
4399 int sign;
4400 isl_int d;
4402 first = isl_seq_first_non_zero(c, n);
4403 if (first == -1)
4404 return aff;
4406 sign = isl_int_sgn(denom);
4407 isl_int_init(d);
4408 isl_int_abs(d, denom);
4409 for (i = first; i < n; ++i) {
4410 isl_aff *aff_i;
4412 if (isl_int_is_zero(c[i]))
4413 continue;
4414 aff_i = isl_multi_aff_get_aff(ma, i);
4415 aff_i = isl_aff_scale(aff_i, c[i]);
4416 aff_i = isl_aff_scale_down(aff_i, d);
4417 if (sign >= 0)
4418 aff = isl_aff_sub(aff, aff_i);
4419 else
4420 aff = isl_aff_add(aff, aff_i);
4422 isl_int_clear(d);
4424 return aff;
4427 /* Extract an affine expression that expresses the output dimension "pos"
4428 * of "bmap" in terms of the parameters and input dimensions from
4429 * equality "eq".
4430 * Note that this expression may involve integer divisions defined
4431 * in terms of parameters and input dimensions.
4432 * The equality may also involve references to earlier (but not later)
4433 * output dimensions. These are replaced by the corresponding elements
4434 * in "ma".
4436 * If the equality is of the form
4438 * f(i) + h(j) + a x + g(i) = 0,
4440 * with f(i) a linear combinations of the parameters and input dimensions,
4441 * g(i) a linear combination of integer divisions defined in terms of the same
4442 * and h(j) a linear combinations of earlier output dimensions,
4443 * then the affine expression is
4445 * (-f(i) - g(i))/a - h(j)/a
4447 * If the equality is of the form
4449 * f(i) + h(j) - a x + g(i) = 0,
4451 * then the affine expression is
4453 * (f(i) + g(i))/a - h(j)/(-a)
4456 * If "div" refers to an integer division (i.e., it is smaller than
4457 * the number of integer divisions), then the equality constraint
4458 * does involve an integer division (the one at position "div") that
4459 * is defined in terms of output dimensions. However, this integer
4460 * division can be eliminated by exploiting a pair of constraints
4461 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4462 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4463 * -l + x >= 0.
4464 * In particular, let
4466 * x = e(i) + m floor(...)
4468 * with e(i) the expression derived above and floor(...) the integer
4469 * division involving output dimensions.
4470 * From
4472 * l <= x <= l + n,
4474 * we have
4476 * 0 <= x - l <= n
4478 * This means
4480 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4481 * = (e(i) - l) mod m
4483 * Therefore,
4485 * x - l = (e(i) - l) mod m
4487 * or
4489 * x = ((e(i) - l) mod m) + l
4491 * The variable "shift" below contains the expression -l, which may
4492 * also involve a linear combination of earlier output dimensions.
4494 static __isl_give isl_aff *extract_aff_from_equality(
4495 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4496 __isl_keep isl_multi_aff *ma)
4498 unsigned o_out;
4499 unsigned n_div, n_out;
4500 isl_ctx *ctx;
4501 isl_local_space *ls;
4502 isl_aff *aff, *shift;
4503 isl_val *mod;
4505 ctx = isl_basic_map_get_ctx(bmap);
4506 ls = isl_basic_map_get_local_space(bmap);
4507 ls = isl_local_space_domain(ls);
4508 aff = isl_aff_alloc(isl_local_space_copy(ls));
4509 if (!aff)
4510 goto error;
4511 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4512 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4513 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4514 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4515 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4516 isl_seq_cpy(aff->v->el + 1 + o_out,
4517 bmap->eq[eq] + o_out + n_out, n_div);
4518 } else {
4519 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4520 isl_seq_neg(aff->v->el + 1 + o_out,
4521 bmap->eq[eq] + o_out + n_out, n_div);
4523 if (div < n_div)
4524 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4525 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4526 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4527 bmap->eq[eq][o_out + pos]);
4528 if (div < n_div) {
4529 shift = isl_aff_alloc(isl_local_space_copy(ls));
4530 if (!shift)
4531 goto error;
4532 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4533 isl_seq_cpy(shift->v->el + 1 + o_out,
4534 bmap->ineq[ineq] + o_out + n_out, n_div);
4535 isl_int_set_si(shift->v->el[0], 1);
4536 shift = subtract_initial(shift, ma, pos,
4537 bmap->ineq[ineq] + o_out, ctx->negone);
4538 aff = isl_aff_add(aff, isl_aff_copy(shift));
4539 mod = isl_val_int_from_isl_int(ctx,
4540 bmap->eq[eq][o_out + n_out + div]);
4541 mod = isl_val_abs(mod);
4542 aff = isl_aff_mod_val(aff, mod);
4543 aff = isl_aff_sub(aff, shift);
4546 isl_local_space_free(ls);
4547 return aff;
4548 error:
4549 isl_local_space_free(ls);
4550 isl_aff_free(aff);
4551 return NULL;
4554 /* Given a basic map with output dimensions defined
4555 * in terms of the parameters input dimensions and earlier
4556 * output dimensions using an equality (and possibly a pair on inequalities),
4557 * extract an isl_aff that expresses output dimension "pos" in terms
4558 * of the parameters and input dimensions.
4559 * Note that this expression may involve integer divisions defined
4560 * in terms of parameters and input dimensions.
4561 * "ma" contains the expressions corresponding to earlier output dimensions.
4563 * This function shares some similarities with
4564 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4566 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4567 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4569 int eq, div, ineq;
4570 isl_aff *aff;
4572 if (!bmap)
4573 return NULL;
4574 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4575 if (eq >= bmap->n_eq)
4576 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4577 "unable to find suitable equality", return NULL);
4578 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4580 aff = isl_aff_remove_unused_divs(aff);
4581 return aff;
4584 /* Given a basic map where each output dimension is defined
4585 * in terms of the parameters and input dimensions using an equality,
4586 * extract an isl_multi_aff that expresses the output dimensions in terms
4587 * of the parameters and input dimensions.
4589 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4590 __isl_take isl_basic_map *bmap)
4592 int i;
4593 unsigned n_out;
4594 isl_multi_aff *ma;
4596 if (!bmap)
4597 return NULL;
4599 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4600 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4602 for (i = 0; i < n_out; ++i) {
4603 isl_aff *aff;
4605 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4606 ma = isl_multi_aff_set_aff(ma, i, aff);
4609 isl_basic_map_free(bmap);
4611 return ma;
4614 /* Given a basic set where each set dimension is defined
4615 * in terms of the parameters using an equality,
4616 * extract an isl_multi_aff that expresses the set dimensions in terms
4617 * of the parameters.
4619 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4620 __isl_take isl_basic_set *bset)
4622 return extract_isl_multi_aff_from_basic_map(bset);
4625 /* Create an isl_pw_multi_aff that is equivalent to
4626 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4627 * The given basic map is such that each output dimension is defined
4628 * in terms of the parameters and input dimensions using an equality.
4630 * Since some applications expect the result of isl_pw_multi_aff_from_map
4631 * to only contain integer affine expressions, we compute the floor
4632 * of the expression before returning.
4634 * Remove all constraints involving local variables without
4635 * an explicit representation (resulting in the removal of those
4636 * local variables) prior to the actual extraction to ensure
4637 * that the local spaces in which the resulting affine expressions
4638 * are created do not contain any unknown local variables.
4639 * Removing such constraints is safe because constraints involving
4640 * unknown local variables are not used to determine whether
4641 * a basic map is obviously single-valued.
4643 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4644 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4646 isl_multi_aff *ma;
4648 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4649 ma = extract_isl_multi_aff_from_basic_map(bmap);
4650 ma = isl_multi_aff_floor(ma);
4651 return isl_pw_multi_aff_alloc(domain, ma);
4654 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4655 * This obviously only works if the input "map" is single-valued.
4656 * If so, we compute the lexicographic minimum of the image in the form
4657 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4658 * to its lexicographic minimum.
4659 * If the input is not single-valued, we produce an error.
4661 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4662 __isl_take isl_map *map)
4664 int i;
4665 int sv;
4666 isl_pw_multi_aff *pma;
4668 sv = isl_map_is_single_valued(map);
4669 if (sv < 0)
4670 goto error;
4671 if (!sv)
4672 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4673 "map is not single-valued", goto error);
4674 map = isl_map_make_disjoint(map);
4675 if (!map)
4676 return NULL;
4678 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4680 for (i = 0; i < map->n; ++i) {
4681 isl_pw_multi_aff *pma_i;
4682 isl_basic_map *bmap;
4683 bmap = isl_basic_map_copy(map->p[i]);
4684 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4685 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4688 isl_map_free(map);
4689 return pma;
4690 error:
4691 isl_map_free(map);
4692 return NULL;
4695 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4696 * taking into account that the output dimension at position "d"
4697 * can be represented as
4699 * x = floor((e(...) + c1) / m)
4701 * given that constraint "i" is of the form
4703 * e(...) + c1 - m x >= 0
4706 * Let "map" be of the form
4708 * A -> B
4710 * We construct a mapping
4712 * A -> [A -> x = floor(...)]
4714 * apply that to the map, obtaining
4716 * [A -> x = floor(...)] -> B
4718 * and equate dimension "d" to x.
4719 * We then compute a isl_pw_multi_aff representation of the resulting map
4720 * and plug in the mapping above.
4722 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4723 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4725 isl_ctx *ctx;
4726 isl_space *space;
4727 isl_local_space *ls;
4728 isl_multi_aff *ma;
4729 isl_aff *aff;
4730 isl_vec *v;
4731 isl_map *insert;
4732 int offset;
4733 int n;
4734 int n_in;
4735 isl_pw_multi_aff *pma;
4736 isl_bool is_set;
4738 is_set = isl_map_is_set(map);
4739 if (is_set < 0)
4740 goto error;
4742 offset = isl_basic_map_offset(hull, isl_dim_out);
4743 ctx = isl_map_get_ctx(map);
4744 space = isl_space_domain(isl_map_get_space(map));
4745 n_in = isl_space_dim(space, isl_dim_set);
4746 n = isl_space_dim(space, isl_dim_all);
4748 v = isl_vec_alloc(ctx, 1 + 1 + n);
4749 if (v) {
4750 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4751 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4753 isl_basic_map_free(hull);
4755 ls = isl_local_space_from_space(isl_space_copy(space));
4756 aff = isl_aff_alloc_vec(ls, v);
4757 aff = isl_aff_floor(aff);
4758 if (is_set) {
4759 isl_space_free(space);
4760 ma = isl_multi_aff_from_aff(aff);
4761 } else {
4762 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4763 ma = isl_multi_aff_range_product(ma,
4764 isl_multi_aff_from_aff(aff));
4767 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4768 map = isl_map_apply_domain(map, insert);
4769 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4770 pma = isl_pw_multi_aff_from_map(map);
4771 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4773 return pma;
4774 error:
4775 isl_map_free(map);
4776 isl_basic_map_free(hull);
4777 return NULL;
4780 /* Is constraint "c" of the form
4782 * e(...) + c1 - m x >= 0
4784 * or
4786 * -e(...) + c2 + m x >= 0
4788 * where m > 1 and e only depends on parameters and input dimemnsions?
4790 * "offset" is the offset of the output dimensions
4791 * "pos" is the position of output dimension x.
4793 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4795 if (isl_int_is_zero(c[offset + d]))
4796 return 0;
4797 if (isl_int_is_one(c[offset + d]))
4798 return 0;
4799 if (isl_int_is_negone(c[offset + d]))
4800 return 0;
4801 if (isl_seq_first_non_zero(c + offset, d) != -1)
4802 return 0;
4803 if (isl_seq_first_non_zero(c + offset + d + 1,
4804 total - (offset + d + 1)) != -1)
4805 return 0;
4806 return 1;
4809 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4811 * As a special case, we first check if there is any pair of constraints,
4812 * shared by all the basic maps in "map" that force a given dimension
4813 * to be equal to the floor of some affine combination of the input dimensions.
4815 * In particular, if we can find two constraints
4817 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4819 * and
4821 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4823 * where m > 1 and e only depends on parameters and input dimemnsions,
4824 * and such that
4826 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4828 * then we know that we can take
4830 * x = floor((e(...) + c1) / m)
4832 * without having to perform any computation.
4834 * Note that we know that
4836 * c1 + c2 >= 1
4838 * If c1 + c2 were 0, then we would have detected an equality during
4839 * simplification. If c1 + c2 were negative, then we would have detected
4840 * a contradiction.
4842 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4843 __isl_take isl_map *map)
4845 int d, dim;
4846 int i, j, n;
4847 int offset, total;
4848 isl_int sum;
4849 isl_basic_map *hull;
4851 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4852 if (!hull)
4853 goto error;
4855 isl_int_init(sum);
4856 dim = isl_map_dim(map, isl_dim_out);
4857 offset = isl_basic_map_offset(hull, isl_dim_out);
4858 total = 1 + isl_basic_map_total_dim(hull);
4859 n = hull->n_ineq;
4860 for (d = 0; d < dim; ++d) {
4861 for (i = 0; i < n; ++i) {
4862 if (!is_potential_div_constraint(hull->ineq[i],
4863 offset, d, total))
4864 continue;
4865 for (j = i + 1; j < n; ++j) {
4866 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4867 hull->ineq[j] + 1, total - 1))
4868 continue;
4869 isl_int_add(sum, hull->ineq[i][0],
4870 hull->ineq[j][0]);
4871 if (isl_int_abs_lt(sum,
4872 hull->ineq[i][offset + d]))
4873 break;
4876 if (j >= n)
4877 continue;
4878 isl_int_clear(sum);
4879 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4880 j = i;
4881 return pw_multi_aff_from_map_div(map, hull, d, j);
4884 isl_int_clear(sum);
4885 isl_basic_map_free(hull);
4886 return pw_multi_aff_from_map_base(map);
4887 error:
4888 isl_map_free(map);
4889 isl_basic_map_free(hull);
4890 return NULL;
4893 /* Given an affine expression
4895 * [A -> B] -> f(A,B)
4897 * construct an isl_multi_aff
4899 * [A -> B] -> B'
4901 * such that dimension "d" in B' is set to "aff" and the remaining
4902 * dimensions are set equal to the corresponding dimensions in B.
4903 * "n_in" is the dimension of the space A.
4904 * "n_out" is the dimension of the space B.
4906 * If "is_set" is set, then the affine expression is of the form
4908 * [B] -> f(B)
4910 * and we construct an isl_multi_aff
4912 * B -> B'
4914 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4915 unsigned n_in, unsigned n_out, int is_set)
4917 int i;
4918 isl_multi_aff *ma;
4919 isl_space *space, *space2;
4920 isl_local_space *ls;
4922 space = isl_aff_get_domain_space(aff);
4923 ls = isl_local_space_from_space(isl_space_copy(space));
4924 space2 = isl_space_copy(space);
4925 if (!is_set)
4926 space2 = isl_space_range(isl_space_unwrap(space2));
4927 space = isl_space_map_from_domain_and_range(space, space2);
4928 ma = isl_multi_aff_alloc(space);
4929 ma = isl_multi_aff_set_aff(ma, d, aff);
4931 for (i = 0; i < n_out; ++i) {
4932 if (i == d)
4933 continue;
4934 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4935 isl_dim_set, n_in + i);
4936 ma = isl_multi_aff_set_aff(ma, i, aff);
4939 isl_local_space_free(ls);
4941 return ma;
4944 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4945 * taking into account that the dimension at position "d" can be written as
4947 * x = m a + f(..) (1)
4949 * where m is equal to "gcd".
4950 * "i" is the index of the equality in "hull" that defines f(..).
4951 * In particular, the equality is of the form
4953 * f(..) - x + m g(existentials) = 0
4955 * or
4957 * -f(..) + x + m g(existentials) = 0
4959 * We basically plug (1) into "map", resulting in a map with "a"
4960 * in the range instead of "x". The corresponding isl_pw_multi_aff
4961 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4963 * Specifically, given the input map
4965 * A -> B
4967 * We first wrap it into a set
4969 * [A -> B]
4971 * and define (1) on top of the corresponding space, resulting in "aff".
4972 * We use this to create an isl_multi_aff that maps the output position "d"
4973 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4974 * We plug this into the wrapped map, unwrap the result and compute the
4975 * corresponding isl_pw_multi_aff.
4976 * The result is an expression
4978 * A -> T(A)
4980 * We adjust that to
4982 * A -> [A -> T(A)]
4984 * so that we can plug that into "aff", after extending the latter to
4985 * a mapping
4987 * [A -> B] -> B'
4990 * If "map" is actually a set, then there is no "A" space, meaning
4991 * that we do not need to perform any wrapping, and that the result
4992 * of the recursive call is of the form
4994 * [T]
4996 * which is plugged into a mapping of the form
4998 * B -> B'
5000 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5001 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5002 isl_int gcd)
5004 isl_set *set;
5005 isl_space *space;
5006 isl_local_space *ls;
5007 isl_aff *aff;
5008 isl_multi_aff *ma;
5009 isl_pw_multi_aff *pma, *id;
5010 unsigned n_in;
5011 unsigned o_out;
5012 unsigned n_out;
5013 isl_bool is_set;
5015 is_set = isl_map_is_set(map);
5016 if (is_set < 0)
5017 goto error;
5019 n_in = isl_basic_map_dim(hull, isl_dim_in);
5020 n_out = isl_basic_map_dim(hull, isl_dim_out);
5021 o_out = isl_basic_map_offset(hull, isl_dim_out);
5023 if (is_set)
5024 set = map;
5025 else
5026 set = isl_map_wrap(map);
5027 space = isl_space_map_from_set(isl_set_get_space(set));
5028 ma = isl_multi_aff_identity(space);
5029 ls = isl_local_space_from_space(isl_set_get_space(set));
5030 aff = isl_aff_alloc(ls);
5031 if (aff) {
5032 isl_int_set_si(aff->v->el[0], 1);
5033 if (isl_int_is_one(hull->eq[i][o_out + d]))
5034 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5035 aff->v->size - 1);
5036 else
5037 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5038 aff->v->size - 1);
5039 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5041 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5042 set = isl_set_preimage_multi_aff(set, ma);
5044 ma = range_map(aff, d, n_in, n_out, is_set);
5046 if (is_set)
5047 map = set;
5048 else
5049 map = isl_set_unwrap(set);
5050 pma = isl_pw_multi_aff_from_map(map);
5052 if (!is_set) {
5053 space = isl_pw_multi_aff_get_domain_space(pma);
5054 space = isl_space_map_from_set(space);
5055 id = isl_pw_multi_aff_identity(space);
5056 pma = isl_pw_multi_aff_range_product(id, pma);
5058 id = isl_pw_multi_aff_from_multi_aff(ma);
5059 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5061 isl_basic_map_free(hull);
5062 return pma;
5063 error:
5064 isl_map_free(map);
5065 isl_basic_map_free(hull);
5066 return NULL;
5069 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5070 * "hull" contains the equalities valid for "map".
5072 * Check if any of the output dimensions is "strided".
5073 * That is, we check if it can be written as
5075 * x = m a + f(..)
5077 * with m greater than 1, a some combination of existentially quantified
5078 * variables and f an expression in the parameters and input dimensions.
5079 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5081 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5082 * special case.
5084 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5085 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5087 int i, j;
5088 unsigned n_out;
5089 unsigned o_out;
5090 unsigned n_div;
5091 unsigned o_div;
5092 isl_int gcd;
5094 n_div = isl_basic_map_dim(hull, isl_dim_div);
5095 o_div = isl_basic_map_offset(hull, isl_dim_div);
5097 if (n_div == 0) {
5098 isl_basic_map_free(hull);
5099 return pw_multi_aff_from_map_check_div(map);
5102 isl_int_init(gcd);
5104 n_out = isl_basic_map_dim(hull, isl_dim_out);
5105 o_out = isl_basic_map_offset(hull, isl_dim_out);
5107 for (i = 0; i < n_out; ++i) {
5108 for (j = 0; j < hull->n_eq; ++j) {
5109 isl_int *eq = hull->eq[j];
5110 isl_pw_multi_aff *res;
5112 if (!isl_int_is_one(eq[o_out + i]) &&
5113 !isl_int_is_negone(eq[o_out + i]))
5114 continue;
5115 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5116 continue;
5117 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5118 n_out - (i + 1)) != -1)
5119 continue;
5120 isl_seq_gcd(eq + o_div, n_div, &gcd);
5121 if (isl_int_is_zero(gcd))
5122 continue;
5123 if (isl_int_is_one(gcd))
5124 continue;
5126 res = pw_multi_aff_from_map_stride(map, hull,
5127 i, j, gcd);
5128 isl_int_clear(gcd);
5129 return res;
5133 isl_int_clear(gcd);
5134 isl_basic_map_free(hull);
5135 return pw_multi_aff_from_map_check_div(map);
5138 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5140 * As a special case, we first check if all output dimensions are uniquely
5141 * defined in terms of the parameters and input dimensions over the entire
5142 * domain. If so, we extract the desired isl_pw_multi_aff directly
5143 * from the affine hull of "map" and its domain.
5145 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5146 * special cases.
5148 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5150 isl_bool sv;
5151 isl_basic_map *hull;
5153 if (!map)
5154 return NULL;
5156 if (isl_map_n_basic_map(map) == 1) {
5157 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5158 hull = isl_basic_map_plain_affine_hull(hull);
5159 sv = isl_basic_map_plain_is_single_valued(hull);
5160 if (sv >= 0 && sv)
5161 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5162 hull);
5163 isl_basic_map_free(hull);
5165 map = isl_map_detect_equalities(map);
5166 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5167 sv = isl_basic_map_plain_is_single_valued(hull);
5168 if (sv >= 0 && sv)
5169 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5170 if (sv >= 0)
5171 return pw_multi_aff_from_map_check_strides(map, hull);
5172 isl_basic_map_free(hull);
5173 isl_map_free(map);
5174 return NULL;
5177 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5179 return isl_pw_multi_aff_from_map(set);
5182 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5183 * add it to *user.
5185 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5187 isl_union_pw_multi_aff **upma = user;
5188 isl_pw_multi_aff *pma;
5190 pma = isl_pw_multi_aff_from_map(map);
5191 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5193 return *upma ? isl_stat_ok : isl_stat_error;
5196 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5197 * domain.
5199 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5200 __isl_take isl_aff *aff)
5202 isl_multi_aff *ma;
5203 isl_pw_multi_aff *pma;
5205 ma = isl_multi_aff_from_aff(aff);
5206 pma = isl_pw_multi_aff_from_multi_aff(ma);
5207 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5210 /* Try and create an isl_union_pw_multi_aff that is equivalent
5211 * to the given isl_union_map.
5212 * The isl_union_map is required to be single-valued in each space.
5213 * Otherwise, an error is produced.
5215 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5216 __isl_take isl_union_map *umap)
5218 isl_space *space;
5219 isl_union_pw_multi_aff *upma;
5221 space = isl_union_map_get_space(umap);
5222 upma = isl_union_pw_multi_aff_empty(space);
5223 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5224 upma = isl_union_pw_multi_aff_free(upma);
5225 isl_union_map_free(umap);
5227 return upma;
5230 /* Try and create an isl_union_pw_multi_aff that is equivalent
5231 * to the given isl_union_set.
5232 * The isl_union_set is required to be a singleton in each space.
5233 * Otherwise, an error is produced.
5235 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5236 __isl_take isl_union_set *uset)
5238 return isl_union_pw_multi_aff_from_union_map(uset);
5241 /* Return the piecewise affine expression "set ? 1 : 0".
5243 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5245 isl_pw_aff *pa;
5246 isl_space *space = isl_set_get_space(set);
5247 isl_local_space *ls = isl_local_space_from_space(space);
5248 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5249 isl_aff *one = isl_aff_zero_on_domain(ls);
5251 one = isl_aff_add_constant_si(one, 1);
5252 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5253 set = isl_set_complement(set);
5254 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5256 return pa;
5259 /* Plug in "subs" for dimension "type", "pos" of "aff".
5261 * Let i be the dimension to replace and let "subs" be of the form
5263 * f/d
5265 * and "aff" of the form
5267 * (a i + g)/m
5269 * The result is
5271 * (a f + d g')/(m d)
5273 * where g' is the result of plugging in "subs" in each of the integer
5274 * divisions in g.
5276 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5277 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5279 isl_ctx *ctx;
5280 isl_int v;
5282 aff = isl_aff_cow(aff);
5283 if (!aff || !subs)
5284 return isl_aff_free(aff);
5286 ctx = isl_aff_get_ctx(aff);
5287 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5288 isl_die(ctx, isl_error_invalid,
5289 "spaces don't match", return isl_aff_free(aff));
5290 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5291 isl_die(ctx, isl_error_unsupported,
5292 "cannot handle divs yet", return isl_aff_free(aff));
5294 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5295 if (!aff->ls)
5296 return isl_aff_free(aff);
5298 aff->v = isl_vec_cow(aff->v);
5299 if (!aff->v)
5300 return isl_aff_free(aff);
5302 pos += isl_local_space_offset(aff->ls, type);
5304 isl_int_init(v);
5305 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5306 aff->v->size, subs->v->size, v);
5307 isl_int_clear(v);
5309 return aff;
5312 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5313 * expressions in "maff".
5315 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5316 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5317 __isl_keep isl_aff *subs)
5319 int i;
5321 maff = isl_multi_aff_cow(maff);
5322 if (!maff || !subs)
5323 return isl_multi_aff_free(maff);
5325 if (type == isl_dim_in)
5326 type = isl_dim_set;
5328 for (i = 0; i < maff->n; ++i) {
5329 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5330 type, pos, subs);
5331 if (!maff->u.p[i])
5332 return isl_multi_aff_free(maff);
5335 return maff;
5338 /* Plug in "subs" for dimension "type", "pos" of "pma".
5340 * pma is of the form
5342 * A_i(v) -> M_i(v)
5344 * while subs is of the form
5346 * v' = B_j(v) -> S_j
5348 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5349 * has a contribution in the result, in particular
5351 * C_ij(S_j) -> M_i(S_j)
5353 * Note that plugging in S_j in C_ij may also result in an empty set
5354 * and this contribution should simply be discarded.
5356 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5357 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5358 __isl_keep isl_pw_aff *subs)
5360 int i, j, n;
5361 isl_pw_multi_aff *res;
5363 if (!pma || !subs)
5364 return isl_pw_multi_aff_free(pma);
5366 n = pma->n * subs->n;
5367 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5369 for (i = 0; i < pma->n; ++i) {
5370 for (j = 0; j < subs->n; ++j) {
5371 isl_set *common;
5372 isl_multi_aff *res_ij;
5373 int empty;
5375 common = isl_set_intersect(
5376 isl_set_copy(pma->p[i].set),
5377 isl_set_copy(subs->p[j].set));
5378 common = isl_set_substitute(common,
5379 type, pos, subs->p[j].aff);
5380 empty = isl_set_plain_is_empty(common);
5381 if (empty < 0 || empty) {
5382 isl_set_free(common);
5383 if (empty < 0)
5384 goto error;
5385 continue;
5388 res_ij = isl_multi_aff_substitute(
5389 isl_multi_aff_copy(pma->p[i].maff),
5390 type, pos, subs->p[j].aff);
5392 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5396 isl_pw_multi_aff_free(pma);
5397 return res;
5398 error:
5399 isl_pw_multi_aff_free(pma);
5400 isl_pw_multi_aff_free(res);
5401 return NULL;
5404 /* Compute the preimage of a range of dimensions in the affine expression "src"
5405 * under "ma" and put the result in "dst". The number of dimensions in "src"
5406 * that precede the range is given by "n_before". The number of dimensions
5407 * in the range is given by the number of output dimensions of "ma".
5408 * The number of dimensions that follow the range is given by "n_after".
5409 * If "has_denom" is set (to one),
5410 * then "src" and "dst" have an extra initial denominator.
5411 * "n_div_ma" is the number of existentials in "ma"
5412 * "n_div_bset" is the number of existentials in "src"
5413 * The resulting "dst" (which is assumed to have been allocated by
5414 * the caller) contains coefficients for both sets of existentials,
5415 * first those in "ma" and then those in "src".
5416 * f, c1, c2 and g are temporary objects that have been initialized
5417 * by the caller.
5419 * Let src represent the expression
5421 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5423 * and let ma represent the expressions
5425 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5427 * We start out with the following expression for dst:
5429 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5431 * with the multiplication factor f initially equal to 1
5432 * and f \sum_i b_i v_i kept separately.
5433 * For each x_i that we substitute, we multiply the numerator
5434 * (and denominator) of dst by c_1 = m_i and add the numerator
5435 * of the x_i expression multiplied by c_2 = f b_i,
5436 * after removing the common factors of c_1 and c_2.
5437 * The multiplication factor f also needs to be multiplied by c_1
5438 * for the next x_j, j > i.
5440 void isl_seq_preimage(isl_int *dst, isl_int *src,
5441 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5442 int n_div_ma, int n_div_bmap,
5443 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5445 int i;
5446 int n_param, n_in, n_out;
5447 int o_dst, o_src;
5449 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5450 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5451 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5453 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5454 o_dst = o_src = has_denom + 1 + n_param + n_before;
5455 isl_seq_clr(dst + o_dst, n_in);
5456 o_dst += n_in;
5457 o_src += n_out;
5458 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5459 o_dst += n_after;
5460 o_src += n_after;
5461 isl_seq_clr(dst + o_dst, n_div_ma);
5462 o_dst += n_div_ma;
5463 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5465 isl_int_set_si(f, 1);
5467 for (i = 0; i < n_out; ++i) {
5468 int offset = has_denom + 1 + n_param + n_before + i;
5470 if (isl_int_is_zero(src[offset]))
5471 continue;
5472 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5473 isl_int_mul(c2, f, src[offset]);
5474 isl_int_gcd(g, c1, c2);
5475 isl_int_divexact(c1, c1, g);
5476 isl_int_divexact(c2, c2, g);
5478 isl_int_mul(f, f, c1);
5479 o_dst = has_denom;
5480 o_src = 1;
5481 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5482 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5483 o_dst += 1 + n_param;
5484 o_src += 1 + n_param;
5485 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5486 o_dst += n_before;
5487 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5488 c2, ma->u.p[i]->v->el + o_src, n_in);
5489 o_dst += n_in;
5490 o_src += n_in;
5491 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5492 o_dst += n_after;
5493 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5494 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5495 o_dst += n_div_ma;
5496 o_src += n_div_ma;
5497 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5498 if (has_denom)
5499 isl_int_mul(dst[0], dst[0], c1);
5503 /* Compute the pullback of "aff" by the function represented by "ma".
5504 * In other words, plug in "ma" in "aff". The result is an affine expression
5505 * defined over the domain space of "ma".
5507 * If "aff" is represented by
5509 * (a(p) + b x + c(divs))/d
5511 * and ma is represented by
5513 * x = D(p) + F(y) + G(divs')
5515 * then the result is
5517 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5519 * The divs in the local space of the input are similarly adjusted
5520 * through a call to isl_local_space_preimage_multi_aff.
5522 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5523 __isl_take isl_multi_aff *ma)
5525 isl_aff *res = NULL;
5526 isl_local_space *ls;
5527 int n_div_aff, n_div_ma;
5528 isl_int f, c1, c2, g;
5530 ma = isl_multi_aff_align_divs(ma);
5531 if (!aff || !ma)
5532 goto error;
5534 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5535 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5537 ls = isl_aff_get_domain_local_space(aff);
5538 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5539 res = isl_aff_alloc(ls);
5540 if (!res)
5541 goto error;
5543 isl_int_init(f);
5544 isl_int_init(c1);
5545 isl_int_init(c2);
5546 isl_int_init(g);
5548 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5549 f, c1, c2, g, 1);
5551 isl_int_clear(f);
5552 isl_int_clear(c1);
5553 isl_int_clear(c2);
5554 isl_int_clear(g);
5556 isl_aff_free(aff);
5557 isl_multi_aff_free(ma);
5558 res = isl_aff_normalize(res);
5559 return res;
5560 error:
5561 isl_aff_free(aff);
5562 isl_multi_aff_free(ma);
5563 isl_aff_free(res);
5564 return NULL;
5567 /* Compute the pullback of "aff1" by the function represented by "aff2".
5568 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5569 * defined over the domain space of "aff1".
5571 * The domain of "aff1" should match the range of "aff2", which means
5572 * that it should be single-dimensional.
5574 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5575 __isl_take isl_aff *aff2)
5577 isl_multi_aff *ma;
5579 ma = isl_multi_aff_from_aff(aff2);
5580 return isl_aff_pullback_multi_aff(aff1, ma);
5583 /* Compute the pullback of "ma1" by the function represented by "ma2".
5584 * In other words, plug in "ma2" in "ma1".
5586 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5588 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5589 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5591 int i;
5592 isl_space *space = NULL;
5594 ma2 = isl_multi_aff_align_divs(ma2);
5595 ma1 = isl_multi_aff_cow(ma1);
5596 if (!ma1 || !ma2)
5597 goto error;
5599 space = isl_space_join(isl_multi_aff_get_space(ma2),
5600 isl_multi_aff_get_space(ma1));
5602 for (i = 0; i < ma1->n; ++i) {
5603 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5604 isl_multi_aff_copy(ma2));
5605 if (!ma1->u.p[i])
5606 goto error;
5609 ma1 = isl_multi_aff_reset_space(ma1, space);
5610 isl_multi_aff_free(ma2);
5611 return ma1;
5612 error:
5613 isl_space_free(space);
5614 isl_multi_aff_free(ma2);
5615 isl_multi_aff_free(ma1);
5616 return NULL;
5619 /* Compute the pullback of "ma1" by the function represented by "ma2".
5620 * In other words, plug in "ma2" in "ma1".
5622 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5623 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5625 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5626 &isl_multi_aff_pullback_multi_aff_aligned);
5629 /* Extend the local space of "dst" to include the divs
5630 * in the local space of "src".
5632 * If "src" does not have any divs or if the local spaces of "dst" and
5633 * "src" are the same, then no extension is required.
5635 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5636 __isl_keep isl_aff *src)
5638 isl_ctx *ctx;
5639 int src_n_div, dst_n_div;
5640 int *exp1 = NULL;
5641 int *exp2 = NULL;
5642 isl_bool equal;
5643 isl_mat *div;
5645 if (!src || !dst)
5646 return isl_aff_free(dst);
5648 ctx = isl_aff_get_ctx(src);
5649 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5650 if (equal < 0)
5651 return isl_aff_free(dst);
5652 if (!equal)
5653 isl_die(ctx, isl_error_invalid,
5654 "spaces don't match", goto error);
5656 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5657 if (src_n_div == 0)
5658 return dst;
5659 equal = isl_local_space_is_equal(src->ls, dst->ls);
5660 if (equal < 0)
5661 return isl_aff_free(dst);
5662 if (equal)
5663 return dst;
5665 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5666 exp1 = isl_alloc_array(ctx, int, src_n_div);
5667 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5668 if (!exp1 || (dst_n_div && !exp2))
5669 goto error;
5671 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5672 dst = isl_aff_expand_divs(dst, div, exp2);
5673 free(exp1);
5674 free(exp2);
5676 return dst;
5677 error:
5678 free(exp1);
5679 free(exp2);
5680 return isl_aff_free(dst);
5683 /* Adjust the local spaces of the affine expressions in "maff"
5684 * such that they all have the save divs.
5686 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5687 __isl_take isl_multi_aff *maff)
5689 int i;
5691 if (!maff)
5692 return NULL;
5693 if (maff->n == 0)
5694 return maff;
5695 maff = isl_multi_aff_cow(maff);
5696 if (!maff)
5697 return NULL;
5699 for (i = 1; i < maff->n; ++i)
5700 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5701 for (i = 1; i < maff->n; ++i) {
5702 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5703 if (!maff->u.p[i])
5704 return isl_multi_aff_free(maff);
5707 return maff;
5710 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5712 aff = isl_aff_cow(aff);
5713 if (!aff)
5714 return NULL;
5716 aff->ls = isl_local_space_lift(aff->ls);
5717 if (!aff->ls)
5718 return isl_aff_free(aff);
5720 return aff;
5723 /* Lift "maff" to a space with extra dimensions such that the result
5724 * has no more existentially quantified variables.
5725 * If "ls" is not NULL, then *ls is assigned the local space that lies
5726 * at the basis of the lifting applied to "maff".
5728 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5729 __isl_give isl_local_space **ls)
5731 int i;
5732 isl_space *space;
5733 unsigned n_div;
5735 if (ls)
5736 *ls = NULL;
5738 if (!maff)
5739 return NULL;
5741 if (maff->n == 0) {
5742 if (ls) {
5743 isl_space *space = isl_multi_aff_get_domain_space(maff);
5744 *ls = isl_local_space_from_space(space);
5745 if (!*ls)
5746 return isl_multi_aff_free(maff);
5748 return maff;
5751 maff = isl_multi_aff_cow(maff);
5752 maff = isl_multi_aff_align_divs(maff);
5753 if (!maff)
5754 return NULL;
5756 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5757 space = isl_multi_aff_get_space(maff);
5758 space = isl_space_lift(isl_space_domain(space), n_div);
5759 space = isl_space_extend_domain_with_range(space,
5760 isl_multi_aff_get_space(maff));
5761 if (!space)
5762 return isl_multi_aff_free(maff);
5763 isl_space_free(maff->space);
5764 maff->space = space;
5766 if (ls) {
5767 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5768 if (!*ls)
5769 return isl_multi_aff_free(maff);
5772 for (i = 0; i < maff->n; ++i) {
5773 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5774 if (!maff->u.p[i])
5775 goto error;
5778 return maff;
5779 error:
5780 if (ls)
5781 isl_local_space_free(*ls);
5782 return isl_multi_aff_free(maff);
5786 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5788 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5789 __isl_keep isl_pw_multi_aff *pma, int pos)
5791 int i;
5792 int n_out;
5793 isl_space *space;
5794 isl_pw_aff *pa;
5796 if (!pma)
5797 return NULL;
5799 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5800 if (pos < 0 || pos >= n_out)
5801 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5802 "index out of bounds", return NULL);
5804 space = isl_pw_multi_aff_get_space(pma);
5805 space = isl_space_drop_dims(space, isl_dim_out,
5806 pos + 1, n_out - pos - 1);
5807 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5809 pa = isl_pw_aff_alloc_size(space, pma->n);
5810 for (i = 0; i < pma->n; ++i) {
5811 isl_aff *aff;
5812 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5813 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5816 return pa;
5819 /* Return an isl_pw_multi_aff with the given "set" as domain and
5820 * an unnamed zero-dimensional range.
5822 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5823 __isl_take isl_set *set)
5825 isl_multi_aff *ma;
5826 isl_space *space;
5828 space = isl_set_get_space(set);
5829 space = isl_space_from_domain(space);
5830 ma = isl_multi_aff_zero(space);
5831 return isl_pw_multi_aff_alloc(set, ma);
5834 /* Add an isl_pw_multi_aff with the given "set" as domain and
5835 * an unnamed zero-dimensional range to *user.
5837 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5838 void *user)
5840 isl_union_pw_multi_aff **upma = user;
5841 isl_pw_multi_aff *pma;
5843 pma = isl_pw_multi_aff_from_domain(set);
5844 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5846 return isl_stat_ok;
5849 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5850 * an unnamed zero-dimensional range.
5852 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5853 __isl_take isl_union_set *uset)
5855 isl_space *space;
5856 isl_union_pw_multi_aff *upma;
5858 if (!uset)
5859 return NULL;
5861 space = isl_union_set_get_space(uset);
5862 upma = isl_union_pw_multi_aff_empty(space);
5864 if (isl_union_set_foreach_set(uset,
5865 &add_pw_multi_aff_from_domain, &upma) < 0)
5866 goto error;
5868 isl_union_set_free(uset);
5869 return upma;
5870 error:
5871 isl_union_set_free(uset);
5872 isl_union_pw_multi_aff_free(upma);
5873 return NULL;
5876 /* Local data for bin_entry and the callback "fn".
5878 struct isl_union_pw_multi_aff_bin_data {
5879 isl_union_pw_multi_aff *upma2;
5880 isl_union_pw_multi_aff *res;
5881 isl_pw_multi_aff *pma;
5882 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5885 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5886 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5888 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5890 struct isl_union_pw_multi_aff_bin_data *data = user;
5891 isl_stat r;
5893 data->pma = pma;
5894 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5895 data->fn, data);
5896 isl_pw_multi_aff_free(pma);
5898 return r;
5901 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5902 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5903 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5904 * as *entry. The callback should adjust data->res if desired.
5906 static __isl_give isl_union_pw_multi_aff *bin_op(
5907 __isl_take isl_union_pw_multi_aff *upma1,
5908 __isl_take isl_union_pw_multi_aff *upma2,
5909 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5911 isl_space *space;
5912 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5914 space = isl_union_pw_multi_aff_get_space(upma2);
5915 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5916 space = isl_union_pw_multi_aff_get_space(upma1);
5917 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5919 if (!upma1 || !upma2)
5920 goto error;
5922 data.upma2 = upma2;
5923 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5924 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5925 &bin_entry, &data) < 0)
5926 goto error;
5928 isl_union_pw_multi_aff_free(upma1);
5929 isl_union_pw_multi_aff_free(upma2);
5930 return data.res;
5931 error:
5932 isl_union_pw_multi_aff_free(upma1);
5933 isl_union_pw_multi_aff_free(upma2);
5934 isl_union_pw_multi_aff_free(data.res);
5935 return NULL;
5938 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5939 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5941 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5942 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5944 isl_space *space;
5946 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5947 isl_pw_multi_aff_get_space(pma2));
5948 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5949 &isl_multi_aff_range_product);
5952 /* Given two isl_pw_multi_affs A -> B and C -> D,
5953 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5955 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5956 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5958 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5959 &pw_multi_aff_range_product);
5962 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5963 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5965 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5966 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5968 isl_space *space;
5970 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5971 isl_pw_multi_aff_get_space(pma2));
5972 space = isl_space_flatten_range(space);
5973 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5974 &isl_multi_aff_flat_range_product);
5977 /* Given two isl_pw_multi_affs A -> B and C -> D,
5978 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5980 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5981 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5983 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5984 &pw_multi_aff_flat_range_product);
5987 /* If data->pma and "pma2" have the same domain space, then compute
5988 * their flat range product and the result to data->res.
5990 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
5991 void *user)
5993 struct isl_union_pw_multi_aff_bin_data *data = user;
5995 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5996 pma2->dim, isl_dim_in)) {
5997 isl_pw_multi_aff_free(pma2);
5998 return isl_stat_ok;
6001 pma2 = isl_pw_multi_aff_flat_range_product(
6002 isl_pw_multi_aff_copy(data->pma), pma2);
6004 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6006 return isl_stat_ok;
6009 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6010 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6012 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6013 __isl_take isl_union_pw_multi_aff *upma1,
6014 __isl_take isl_union_pw_multi_aff *upma2)
6016 return bin_op(upma1, upma2, &flat_range_product_entry);
6019 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6020 * The parameters are assumed to have been aligned.
6022 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6023 * except that it works on two different isl_pw_* types.
6025 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6026 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6027 __isl_take isl_pw_aff *pa)
6029 int i, j, n;
6030 isl_pw_multi_aff *res = NULL;
6032 if (!pma || !pa)
6033 goto error;
6035 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6036 pa->dim, isl_dim_in))
6037 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6038 "domains don't match", goto error);
6039 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6040 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6041 "index out of bounds", goto error);
6043 n = pma->n * pa->n;
6044 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6046 for (i = 0; i < pma->n; ++i) {
6047 for (j = 0; j < pa->n; ++j) {
6048 isl_set *common;
6049 isl_multi_aff *res_ij;
6050 int empty;
6052 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6053 isl_set_copy(pa->p[j].set));
6054 empty = isl_set_plain_is_empty(common);
6055 if (empty < 0 || empty) {
6056 isl_set_free(common);
6057 if (empty < 0)
6058 goto error;
6059 continue;
6062 res_ij = isl_multi_aff_set_aff(
6063 isl_multi_aff_copy(pma->p[i].maff), pos,
6064 isl_aff_copy(pa->p[j].aff));
6065 res_ij = isl_multi_aff_gist(res_ij,
6066 isl_set_copy(common));
6068 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6072 isl_pw_multi_aff_free(pma);
6073 isl_pw_aff_free(pa);
6074 return res;
6075 error:
6076 isl_pw_multi_aff_free(pma);
6077 isl_pw_aff_free(pa);
6078 return isl_pw_multi_aff_free(res);
6081 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6083 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6084 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6085 __isl_take isl_pw_aff *pa)
6087 isl_bool equal_params;
6089 if (!pma || !pa)
6090 goto error;
6091 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6092 if (equal_params < 0)
6093 goto error;
6094 if (equal_params)
6095 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6096 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6097 isl_pw_aff_check_named_params(pa) < 0)
6098 goto error;
6099 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6100 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6101 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6102 error:
6103 isl_pw_multi_aff_free(pma);
6104 isl_pw_aff_free(pa);
6105 return NULL;
6108 /* Do the parameters of "pa" match those of "space"?
6110 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6111 __isl_keep isl_space *space)
6113 isl_space *pa_space;
6114 isl_bool match;
6116 if (!pa || !space)
6117 return isl_bool_error;
6119 pa_space = isl_pw_aff_get_space(pa);
6121 match = isl_space_has_equal_params(space, pa_space);
6123 isl_space_free(pa_space);
6124 return match;
6127 /* Check that the domain space of "pa" matches "space".
6129 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6130 __isl_keep isl_space *space)
6132 isl_space *pa_space;
6133 isl_bool match;
6135 if (!pa || !space)
6136 return isl_stat_error;
6138 pa_space = isl_pw_aff_get_space(pa);
6140 match = isl_space_has_equal_params(space, pa_space);
6141 if (match < 0)
6142 goto error;
6143 if (!match)
6144 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6145 "parameters don't match", goto error);
6146 match = isl_space_tuple_is_equal(space, isl_dim_in,
6147 pa_space, isl_dim_in);
6148 if (match < 0)
6149 goto error;
6150 if (!match)
6151 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6152 "domains don't match", goto error);
6153 isl_space_free(pa_space);
6154 return isl_stat_ok;
6155 error:
6156 isl_space_free(pa_space);
6157 return isl_stat_error;
6160 #undef BASE
6161 #define BASE pw_aff
6162 #undef DOMBASE
6163 #define DOMBASE set
6165 #include <isl_multi_explicit_domain.c>
6166 #include <isl_multi_pw_aff_explicit_domain.c>
6167 #include <isl_multi_templ.c>
6168 #include <isl_multi_apply_set.c>
6169 #include <isl_multi_coalesce.c>
6170 #include <isl_multi_domain_templ.c>
6171 #include <isl_multi_dims.c>
6172 #include <isl_multi_from_base_templ.c>
6173 #include <isl_multi_gist.c>
6174 #include <isl_multi_hash.c>
6175 #include <isl_multi_identity_templ.c>
6176 #include <isl_multi_align_set.c>
6177 #include <isl_multi_intersect.c>
6178 #include <isl_multi_move_dims_templ.c>
6179 #include <isl_multi_product_templ.c>
6180 #include <isl_multi_splice_templ.c>
6181 #include <isl_multi_zero_templ.c>
6183 /* Does "mpa" have a non-trivial explicit domain?
6185 * The explicit domain, if present, is trivial if it represents
6186 * an (obviously) universe set.
6188 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6189 __isl_keep isl_multi_pw_aff *mpa)
6191 if (!mpa)
6192 return isl_bool_error;
6193 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6194 return isl_bool_false;
6195 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6198 /* Scale the elements of "pma" by the corresponding elements of "mv".
6200 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6201 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6203 int i;
6204 isl_bool equal_params;
6206 pma = isl_pw_multi_aff_cow(pma);
6207 if (!pma || !mv)
6208 goto error;
6209 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6210 mv->space, isl_dim_set))
6211 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6212 "spaces don't match", goto error);
6213 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6214 if (equal_params < 0)
6215 goto error;
6216 if (!equal_params) {
6217 pma = isl_pw_multi_aff_align_params(pma,
6218 isl_multi_val_get_space(mv));
6219 mv = isl_multi_val_align_params(mv,
6220 isl_pw_multi_aff_get_space(pma));
6221 if (!pma || !mv)
6222 goto error;
6225 for (i = 0; i < pma->n; ++i) {
6226 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6227 isl_multi_val_copy(mv));
6228 if (!pma->p[i].maff)
6229 goto error;
6232 isl_multi_val_free(mv);
6233 return pma;
6234 error:
6235 isl_multi_val_free(mv);
6236 isl_pw_multi_aff_free(pma);
6237 return NULL;
6240 /* This function is called for each entry of an isl_union_pw_multi_aff.
6241 * If the space of the entry matches that of data->mv,
6242 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6243 * Otherwise, return an empty isl_pw_multi_aff.
6245 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6246 __isl_take isl_pw_multi_aff *pma, void *user)
6248 isl_multi_val *mv = user;
6250 if (!pma)
6251 return NULL;
6252 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6253 mv->space, isl_dim_set)) {
6254 isl_space *space = isl_pw_multi_aff_get_space(pma);
6255 isl_pw_multi_aff_free(pma);
6256 return isl_pw_multi_aff_empty(space);
6259 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6262 /* Scale the elements of "upma" by the corresponding elements of "mv",
6263 * for those entries that match the space of "mv".
6265 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6266 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6268 upma = isl_union_pw_multi_aff_align_params(upma,
6269 isl_multi_val_get_space(mv));
6270 mv = isl_multi_val_align_params(mv,
6271 isl_union_pw_multi_aff_get_space(upma));
6272 if (!upma || !mv)
6273 goto error;
6275 return isl_union_pw_multi_aff_transform(upma,
6276 &union_pw_multi_aff_scale_multi_val_entry, mv);
6278 isl_multi_val_free(mv);
6279 return upma;
6280 error:
6281 isl_multi_val_free(mv);
6282 isl_union_pw_multi_aff_free(upma);
6283 return NULL;
6286 /* Construct and return a piecewise multi affine expression
6287 * in the given space with value zero in each of the output dimensions and
6288 * a universe domain.
6290 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6292 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6295 /* Construct and return a piecewise multi affine expression
6296 * that is equal to the given piecewise affine expression.
6298 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6299 __isl_take isl_pw_aff *pa)
6301 int i;
6302 isl_space *space;
6303 isl_pw_multi_aff *pma;
6305 if (!pa)
6306 return NULL;
6308 space = isl_pw_aff_get_space(pa);
6309 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6311 for (i = 0; i < pa->n; ++i) {
6312 isl_set *set;
6313 isl_multi_aff *ma;
6315 set = isl_set_copy(pa->p[i].set);
6316 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6317 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6320 isl_pw_aff_free(pa);
6321 return pma;
6324 /* Construct and return a piecewise multi affine expression
6325 * that is equal to the given multi piecewise affine expression
6326 * on the shared domain of the piecewise affine expressions,
6327 * in the special case of a 0D multi piecewise affine expression.
6329 * Create a piecewise multi affine expression with the explicit domain of
6330 * the 0D multi piecewise affine expression as domain.
6332 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6333 __isl_take isl_multi_pw_aff *mpa)
6335 isl_space *space;
6336 isl_set *dom;
6337 isl_multi_aff *ma;
6339 space = isl_multi_pw_aff_get_space(mpa);
6340 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6341 isl_multi_pw_aff_free(mpa);
6343 ma = isl_multi_aff_zero(space);
6344 return isl_pw_multi_aff_alloc(dom, ma);
6347 /* Construct and return a piecewise multi affine expression
6348 * that is equal to the given multi piecewise affine expression
6349 * on the shared domain of the piecewise affine expressions.
6351 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6352 __isl_take isl_multi_pw_aff *mpa)
6354 int i;
6355 isl_space *space;
6356 isl_pw_aff *pa;
6357 isl_pw_multi_aff *pma;
6359 if (!mpa)
6360 return NULL;
6362 if (mpa->n == 0)
6363 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6365 space = isl_multi_pw_aff_get_space(mpa);
6366 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6367 pma = isl_pw_multi_aff_from_pw_aff(pa);
6369 for (i = 1; i < mpa->n; ++i) {
6370 isl_pw_multi_aff *pma_i;
6372 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6373 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6374 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6377 pma = isl_pw_multi_aff_reset_space(pma, space);
6379 isl_multi_pw_aff_free(mpa);
6380 return pma;
6383 /* Construct and return a multi piecewise affine expression
6384 * that is equal to the given multi affine expression.
6386 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6387 __isl_take isl_multi_aff *ma)
6389 int i, n;
6390 isl_multi_pw_aff *mpa;
6392 if (!ma)
6393 return NULL;
6395 n = isl_multi_aff_dim(ma, isl_dim_out);
6396 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6398 for (i = 0; i < n; ++i) {
6399 isl_pw_aff *pa;
6401 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6402 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6405 isl_multi_aff_free(ma);
6406 return mpa;
6409 /* Construct and return a multi piecewise affine expression
6410 * that is equal to the given piecewise multi affine expression.
6412 * If the resulting multi piecewise affine expression has
6413 * an explicit domain, then assign it the domain of the input.
6414 * In other cases, the domain is stored in the individual elements.
6416 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6417 __isl_take isl_pw_multi_aff *pma)
6419 int i, n;
6420 isl_space *space;
6421 isl_multi_pw_aff *mpa;
6423 if (!pma)
6424 return NULL;
6426 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6427 space = isl_pw_multi_aff_get_space(pma);
6428 mpa = isl_multi_pw_aff_alloc(space);
6430 for (i = 0; i < n; ++i) {
6431 isl_pw_aff *pa;
6433 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6434 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6436 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6437 isl_set *dom;
6439 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6440 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6443 isl_pw_multi_aff_free(pma);
6444 return mpa;
6447 /* Do "pa1" and "pa2" represent the same function?
6449 * We first check if they are obviously equal.
6450 * If not, we convert them to maps and check if those are equal.
6452 * If "pa1" or "pa2" contain any NaNs, then they are considered
6453 * not to be the same. A NaN is not equal to anything, not even
6454 * to another NaN.
6456 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6457 __isl_keep isl_pw_aff *pa2)
6459 isl_bool equal;
6460 isl_bool has_nan;
6461 isl_map *map1, *map2;
6463 if (!pa1 || !pa2)
6464 return isl_bool_error;
6466 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6467 if (equal < 0 || equal)
6468 return equal;
6469 has_nan = either_involves_nan(pa1, pa2);
6470 if (has_nan < 0)
6471 return isl_bool_error;
6472 if (has_nan)
6473 return isl_bool_false;
6475 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6476 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6477 equal = isl_map_is_equal(map1, map2);
6478 isl_map_free(map1);
6479 isl_map_free(map2);
6481 return equal;
6484 /* Do "mpa1" and "mpa2" represent the same function?
6486 * Note that we cannot convert the entire isl_multi_pw_aff
6487 * to a map because the domains of the piecewise affine expressions
6488 * may not be the same.
6490 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6491 __isl_keep isl_multi_pw_aff *mpa2)
6493 int i;
6494 isl_bool equal, equal_params;
6496 if (!mpa1 || !mpa2)
6497 return isl_bool_error;
6499 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6500 if (equal_params < 0)
6501 return isl_bool_error;
6502 if (!equal_params) {
6503 if (!isl_space_has_named_params(mpa1->space))
6504 return isl_bool_false;
6505 if (!isl_space_has_named_params(mpa2->space))
6506 return isl_bool_false;
6507 mpa1 = isl_multi_pw_aff_copy(mpa1);
6508 mpa2 = isl_multi_pw_aff_copy(mpa2);
6509 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6510 isl_multi_pw_aff_get_space(mpa2));
6511 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6512 isl_multi_pw_aff_get_space(mpa1));
6513 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6514 isl_multi_pw_aff_free(mpa1);
6515 isl_multi_pw_aff_free(mpa2);
6516 return equal;
6519 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6520 if (equal < 0 || !equal)
6521 return equal;
6523 for (i = 0; i < mpa1->n; ++i) {
6524 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6525 if (equal < 0 || !equal)
6526 return equal;
6529 return isl_bool_true;
6532 /* Do "pma1" and "pma2" represent the same function?
6534 * First check if they are obviously equal.
6535 * If not, then convert them to maps and check if those are equal.
6537 * If "pa1" or "pa2" contain any NaNs, then they are considered
6538 * not to be the same. A NaN is not equal to anything, not even
6539 * to another NaN.
6541 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6542 __isl_keep isl_pw_multi_aff *pma2)
6544 isl_bool equal;
6545 isl_bool has_nan;
6546 isl_map *map1, *map2;
6548 if (!pma1 || !pma2)
6549 return isl_bool_error;
6551 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6552 if (equal < 0 || equal)
6553 return equal;
6554 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6555 if (has_nan >= 0 && !has_nan)
6556 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6557 if (has_nan < 0 || has_nan)
6558 return isl_bool_not(has_nan);
6560 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6561 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6562 equal = isl_map_is_equal(map1, map2);
6563 isl_map_free(map1);
6564 isl_map_free(map2);
6566 return equal;
6569 /* Compute the pullback of "mpa" by the function represented by "ma".
6570 * In other words, plug in "ma" in "mpa".
6572 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6574 * If "mpa" has an explicit domain, then it is this domain
6575 * that needs to undergo a pullback, i.e., a preimage.
6577 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6578 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6580 int i;
6581 isl_space *space = NULL;
6583 mpa = isl_multi_pw_aff_cow(mpa);
6584 if (!mpa || !ma)
6585 goto error;
6587 space = isl_space_join(isl_multi_aff_get_space(ma),
6588 isl_multi_pw_aff_get_space(mpa));
6589 if (!space)
6590 goto error;
6592 for (i = 0; i < mpa->n; ++i) {
6593 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6594 isl_multi_aff_copy(ma));
6595 if (!mpa->u.p[i])
6596 goto error;
6598 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6599 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6600 isl_multi_aff_copy(ma));
6601 if (!mpa->u.dom)
6602 goto error;
6605 isl_multi_aff_free(ma);
6606 isl_space_free(mpa->space);
6607 mpa->space = space;
6608 return mpa;
6609 error:
6610 isl_space_free(space);
6611 isl_multi_pw_aff_free(mpa);
6612 isl_multi_aff_free(ma);
6613 return NULL;
6616 /* Compute the pullback of "mpa" by the function represented by "ma".
6617 * In other words, plug in "ma" in "mpa".
6619 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6620 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6622 isl_bool equal_params;
6624 if (!mpa || !ma)
6625 goto error;
6626 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6627 if (equal_params < 0)
6628 goto error;
6629 if (equal_params)
6630 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6631 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6632 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6633 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6634 error:
6635 isl_multi_pw_aff_free(mpa);
6636 isl_multi_aff_free(ma);
6637 return NULL;
6640 /* Compute the pullback of "mpa" by the function represented by "pma".
6641 * In other words, plug in "pma" in "mpa".
6643 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6645 * If "mpa" has an explicit domain, then it is this domain
6646 * that needs to undergo a pullback, i.e., a preimage.
6648 static __isl_give isl_multi_pw_aff *
6649 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6650 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6652 int i;
6653 isl_space *space = NULL;
6655 mpa = isl_multi_pw_aff_cow(mpa);
6656 if (!mpa || !pma)
6657 goto error;
6659 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6660 isl_multi_pw_aff_get_space(mpa));
6662 for (i = 0; i < mpa->n; ++i) {
6663 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6664 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6665 if (!mpa->u.p[i])
6666 goto error;
6668 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6669 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6670 isl_pw_multi_aff_copy(pma));
6671 if (!mpa->u.dom)
6672 goto error;
6675 isl_pw_multi_aff_free(pma);
6676 isl_space_free(mpa->space);
6677 mpa->space = space;
6678 return mpa;
6679 error:
6680 isl_space_free(space);
6681 isl_multi_pw_aff_free(mpa);
6682 isl_pw_multi_aff_free(pma);
6683 return NULL;
6686 /* Compute the pullback of "mpa" by the function represented by "pma".
6687 * In other words, plug in "pma" in "mpa".
6689 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6690 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6692 isl_bool equal_params;
6694 if (!mpa || !pma)
6695 goto error;
6696 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6697 if (equal_params < 0)
6698 goto error;
6699 if (equal_params)
6700 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6701 mpa = isl_multi_pw_aff_align_params(mpa,
6702 isl_pw_multi_aff_get_space(pma));
6703 pma = isl_pw_multi_aff_align_params(pma,
6704 isl_multi_pw_aff_get_space(mpa));
6705 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6706 error:
6707 isl_multi_pw_aff_free(mpa);
6708 isl_pw_multi_aff_free(pma);
6709 return NULL;
6712 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6713 * with the domain of "aff". The domain of the result is the same
6714 * as that of "mpa".
6715 * "mpa" and "aff" are assumed to have been aligned.
6717 * We first extract the parametric constant from "aff", defined
6718 * over the correct domain.
6719 * Then we add the appropriate combinations of the members of "mpa".
6720 * Finally, we add the integer divisions through recursive calls.
6722 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6723 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6725 int i, n_in, n_div;
6726 isl_space *space;
6727 isl_val *v;
6728 isl_pw_aff *pa;
6729 isl_aff *tmp;
6731 n_in = isl_aff_dim(aff, isl_dim_in);
6732 n_div = isl_aff_dim(aff, isl_dim_div);
6734 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6735 tmp = isl_aff_copy(aff);
6736 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6737 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6738 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6739 isl_space_dim(space, isl_dim_set));
6740 tmp = isl_aff_reset_domain_space(tmp, space);
6741 pa = isl_pw_aff_from_aff(tmp);
6743 for (i = 0; i < n_in; ++i) {
6744 isl_pw_aff *pa_i;
6746 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6747 continue;
6748 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6749 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6750 pa_i = isl_pw_aff_scale_val(pa_i, v);
6751 pa = isl_pw_aff_add(pa, pa_i);
6754 for (i = 0; i < n_div; ++i) {
6755 isl_aff *div;
6756 isl_pw_aff *pa_i;
6758 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6759 continue;
6760 div = isl_aff_get_div(aff, i);
6761 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6762 isl_multi_pw_aff_copy(mpa), div);
6763 pa_i = isl_pw_aff_floor(pa_i);
6764 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6765 pa_i = isl_pw_aff_scale_val(pa_i, v);
6766 pa = isl_pw_aff_add(pa, pa_i);
6769 isl_multi_pw_aff_free(mpa);
6770 isl_aff_free(aff);
6772 return pa;
6775 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6776 * with the domain of "aff". The domain of the result is the same
6777 * as that of "mpa".
6779 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6780 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6782 isl_bool equal_params;
6784 if (!aff || !mpa)
6785 goto error;
6786 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6787 if (equal_params < 0)
6788 goto error;
6789 if (equal_params)
6790 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6792 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6793 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6795 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6796 error:
6797 isl_aff_free(aff);
6798 isl_multi_pw_aff_free(mpa);
6799 return NULL;
6802 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6803 * with the domain of "pa". The domain of the result is the same
6804 * as that of "mpa".
6805 * "mpa" and "pa" are assumed to have been aligned.
6807 * We consider each piece in turn. Note that the domains of the
6808 * pieces are assumed to be disjoint and they remain disjoint
6809 * after taking the preimage (over the same function).
6811 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6812 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6814 isl_space *space;
6815 isl_pw_aff *res;
6816 int i;
6818 if (!mpa || !pa)
6819 goto error;
6821 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6822 isl_pw_aff_get_space(pa));
6823 res = isl_pw_aff_empty(space);
6825 for (i = 0; i < pa->n; ++i) {
6826 isl_pw_aff *pa_i;
6827 isl_set *domain;
6829 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6830 isl_multi_pw_aff_copy(mpa),
6831 isl_aff_copy(pa->p[i].aff));
6832 domain = isl_set_copy(pa->p[i].set);
6833 domain = isl_set_preimage_multi_pw_aff(domain,
6834 isl_multi_pw_aff_copy(mpa));
6835 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6836 res = isl_pw_aff_add_disjoint(res, pa_i);
6839 isl_pw_aff_free(pa);
6840 isl_multi_pw_aff_free(mpa);
6841 return res;
6842 error:
6843 isl_pw_aff_free(pa);
6844 isl_multi_pw_aff_free(mpa);
6845 return NULL;
6848 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6849 * with the domain of "pa". The domain of the result is the same
6850 * as that of "mpa".
6852 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6853 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6855 isl_bool equal_params;
6857 if (!pa || !mpa)
6858 goto error;
6859 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6860 if (equal_params < 0)
6861 goto error;
6862 if (equal_params)
6863 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6865 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6866 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6868 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6869 error:
6870 isl_pw_aff_free(pa);
6871 isl_multi_pw_aff_free(mpa);
6872 return NULL;
6875 /* Compute the pullback of "pa" by the function represented by "mpa".
6876 * In other words, plug in "mpa" in "pa".
6877 * "pa" and "mpa" are assumed to have been aligned.
6879 * The pullback is computed by applying "pa" to "mpa".
6881 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6882 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6884 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6887 /* Compute the pullback of "pa" by the function represented by "mpa".
6888 * In other words, plug in "mpa" in "pa".
6890 * The pullback is computed by applying "pa" to "mpa".
6892 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6893 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6895 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6898 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6899 * In other words, plug in "mpa2" in "mpa1".
6901 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6903 * We pullback each member of "mpa1" in turn.
6905 * If "mpa1" has an explicit domain, then it is this domain
6906 * that needs to undergo a pullback instead, i.e., a preimage.
6908 static __isl_give isl_multi_pw_aff *
6909 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6910 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6912 int i;
6913 isl_space *space = NULL;
6915 mpa1 = isl_multi_pw_aff_cow(mpa1);
6916 if (!mpa1 || !mpa2)
6917 goto error;
6919 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6920 isl_multi_pw_aff_get_space(mpa1));
6922 for (i = 0; i < mpa1->n; ++i) {
6923 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6924 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
6925 if (!mpa1->u.p[i])
6926 goto error;
6929 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
6930 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
6931 isl_multi_pw_aff_copy(mpa2));
6932 if (!mpa1->u.dom)
6933 goto error;
6935 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6937 isl_multi_pw_aff_free(mpa2);
6938 return mpa1;
6939 error:
6940 isl_space_free(space);
6941 isl_multi_pw_aff_free(mpa1);
6942 isl_multi_pw_aff_free(mpa2);
6943 return NULL;
6946 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6947 * In other words, plug in "mpa2" in "mpa1".
6949 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6950 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6952 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6953 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6956 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6957 * of "mpa1" and "mpa2" live in the same space, construct map space
6958 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6959 * with this map space as extract argument.
6961 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6962 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6963 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6964 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6966 int match;
6967 isl_space *space1, *space2;
6968 isl_map *res;
6970 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6971 isl_multi_pw_aff_get_space(mpa2));
6972 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6973 isl_multi_pw_aff_get_space(mpa1));
6974 if (!mpa1 || !mpa2)
6975 goto error;
6976 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6977 mpa2->space, isl_dim_out);
6978 if (match < 0)
6979 goto error;
6980 if (!match)
6981 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6982 "range spaces don't match", goto error);
6983 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6984 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6985 space1 = isl_space_map_from_domain_and_range(space1, space2);
6987 res = order(mpa1, mpa2, space1);
6988 isl_multi_pw_aff_free(mpa1);
6989 isl_multi_pw_aff_free(mpa2);
6990 return res;
6991 error:
6992 isl_multi_pw_aff_free(mpa1);
6993 isl_multi_pw_aff_free(mpa2);
6994 return NULL;
6997 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6998 * where the function values are equal. "space" is the space of the result.
6999 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7001 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7002 * in the sequences are equal.
7004 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7005 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7006 __isl_take isl_space *space)
7008 int i, n;
7009 isl_map *res;
7011 res = isl_map_universe(space);
7013 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7014 for (i = 0; i < n; ++i) {
7015 isl_pw_aff *pa1, *pa2;
7016 isl_map *map;
7018 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7019 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7020 map = isl_pw_aff_eq_map(pa1, pa2);
7021 res = isl_map_intersect(res, map);
7024 return res;
7027 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7028 * where the function values are equal.
7030 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7031 __isl_take isl_multi_pw_aff *mpa2)
7033 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7034 &isl_multi_pw_aff_eq_map_on_space);
7037 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7038 * where the function values of "mpa1" is lexicographically satisfies "base"
7039 * compared to that of "mpa2". "space" is the space of the result.
7040 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7042 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7043 * if its i-th element satisfies "base" when compared to
7044 * the i-th element of "mpa2" while all previous elements are
7045 * pairwise equal.
7047 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7048 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7049 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7050 __isl_take isl_pw_aff *pa2),
7051 __isl_take isl_space *space)
7053 int i, n;
7054 isl_map *res, *rest;
7056 res = isl_map_empty(isl_space_copy(space));
7057 rest = isl_map_universe(space);
7059 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7060 for (i = 0; i < n; ++i) {
7061 isl_pw_aff *pa1, *pa2;
7062 isl_map *map;
7064 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7065 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7066 map = base(pa1, pa2);
7067 map = isl_map_intersect(map, isl_map_copy(rest));
7068 res = isl_map_union(res, map);
7070 if (i == n - 1)
7071 continue;
7073 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7074 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7075 map = isl_pw_aff_eq_map(pa1, pa2);
7076 rest = isl_map_intersect(rest, map);
7079 isl_map_free(rest);
7080 return res;
7083 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7084 * where the function value of "mpa1" is lexicographically less than that
7085 * of "mpa2". "space" is the space of the result.
7086 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7088 * "mpa1" is less than "mpa2" if its i-th element is smaller
7089 * than the i-th element of "mpa2" while all previous elements are
7090 * pairwise equal.
7092 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7093 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7094 __isl_take isl_space *space)
7096 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7097 &isl_pw_aff_lt_map, space);
7100 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7101 * where the function value of "mpa1" is lexicographically less than that
7102 * of "mpa2".
7104 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7105 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7107 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7108 &isl_multi_pw_aff_lex_lt_map_on_space);
7111 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7112 * where the function value of "mpa1" is lexicographically greater than that
7113 * of "mpa2". "space" is the space of the result.
7114 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7116 * "mpa1" is greater than "mpa2" if its i-th element is greater
7117 * than the i-th element of "mpa2" while all previous elements are
7118 * pairwise equal.
7120 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7121 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7122 __isl_take isl_space *space)
7124 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7125 &isl_pw_aff_gt_map, space);
7128 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7129 * where the function value of "mpa1" is lexicographically greater than that
7130 * of "mpa2".
7132 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7133 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7135 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7136 &isl_multi_pw_aff_lex_gt_map_on_space);
7139 /* Compare two isl_affs.
7141 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7142 * than "aff2" and 0 if they are equal.
7144 * The order is fairly arbitrary. We do consider expressions that only involve
7145 * earlier dimensions as "smaller".
7147 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7149 int cmp;
7150 int last1, last2;
7152 if (aff1 == aff2)
7153 return 0;
7155 if (!aff1)
7156 return -1;
7157 if (!aff2)
7158 return 1;
7160 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7161 if (cmp != 0)
7162 return cmp;
7164 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7165 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7166 if (last1 != last2)
7167 return last1 - last2;
7169 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7172 /* Compare two isl_pw_affs.
7174 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7175 * than "pa2" and 0 if they are equal.
7177 * The order is fairly arbitrary. We do consider expressions that only involve
7178 * earlier dimensions as "smaller".
7180 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7181 __isl_keep isl_pw_aff *pa2)
7183 int i;
7184 int cmp;
7186 if (pa1 == pa2)
7187 return 0;
7189 if (!pa1)
7190 return -1;
7191 if (!pa2)
7192 return 1;
7194 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7195 if (cmp != 0)
7196 return cmp;
7198 if (pa1->n != pa2->n)
7199 return pa1->n - pa2->n;
7201 for (i = 0; i < pa1->n; ++i) {
7202 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7203 if (cmp != 0)
7204 return cmp;
7205 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7206 if (cmp != 0)
7207 return cmp;
7210 return 0;
7213 /* Return a piecewise affine expression that is equal to "v" on "domain".
7215 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7216 __isl_take isl_val *v)
7218 isl_space *space;
7219 isl_local_space *ls;
7220 isl_aff *aff;
7222 space = isl_set_get_space(domain);
7223 ls = isl_local_space_from_space(space);
7224 aff = isl_aff_val_on_domain(ls, v);
7226 return isl_pw_aff_alloc(domain, aff);
7229 /* Return a multi affine expression that is equal to "mv" on domain
7230 * space "space".
7232 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7233 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7235 int i, n;
7236 isl_space *space2;
7237 isl_local_space *ls;
7238 isl_multi_aff *ma;
7240 if (!space || !mv)
7241 goto error;
7243 n = isl_multi_val_dim(mv, isl_dim_set);
7244 space2 = isl_multi_val_get_space(mv);
7245 space2 = isl_space_align_params(space2, isl_space_copy(space));
7246 space = isl_space_align_params(space, isl_space_copy(space2));
7247 space = isl_space_map_from_domain_and_range(space, space2);
7248 ma = isl_multi_aff_alloc(isl_space_copy(space));
7249 ls = isl_local_space_from_space(isl_space_domain(space));
7250 for (i = 0; i < n; ++i) {
7251 isl_val *v;
7252 isl_aff *aff;
7254 v = isl_multi_val_get_val(mv, i);
7255 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7256 ma = isl_multi_aff_set_aff(ma, i, aff);
7258 isl_local_space_free(ls);
7260 isl_multi_val_free(mv);
7261 return ma;
7262 error:
7263 isl_space_free(space);
7264 isl_multi_val_free(mv);
7265 return NULL;
7268 /* Return a piecewise multi-affine expression
7269 * that is equal to "mv" on "domain".
7271 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7272 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7274 isl_space *space;
7275 isl_multi_aff *ma;
7277 space = isl_set_get_space(domain);
7278 ma = isl_multi_aff_multi_val_on_space(space, mv);
7280 return isl_pw_multi_aff_alloc(domain, ma);
7283 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7284 * mv is the value that should be attained on each domain set
7285 * res collects the results
7287 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7288 isl_multi_val *mv;
7289 isl_union_pw_multi_aff *res;
7292 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7293 * and add it to data->res.
7295 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7296 void *user)
7298 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7299 isl_pw_multi_aff *pma;
7300 isl_multi_val *mv;
7302 mv = isl_multi_val_copy(data->mv);
7303 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7304 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7306 return data->res ? isl_stat_ok : isl_stat_error;
7309 /* Return a union piecewise multi-affine expression
7310 * that is equal to "mv" on "domain".
7312 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7313 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7315 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7316 isl_space *space;
7318 space = isl_union_set_get_space(domain);
7319 data.res = isl_union_pw_multi_aff_empty(space);
7320 data.mv = mv;
7321 if (isl_union_set_foreach_set(domain,
7322 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7323 data.res = isl_union_pw_multi_aff_free(data.res);
7324 isl_union_set_free(domain);
7325 isl_multi_val_free(mv);
7326 return data.res;
7329 /* Compute the pullback of data->pma by the function represented by "pma2",
7330 * provided the spaces match, and add the results to data->res.
7332 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7334 struct isl_union_pw_multi_aff_bin_data *data = user;
7336 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7337 pma2->dim, isl_dim_out)) {
7338 isl_pw_multi_aff_free(pma2);
7339 return isl_stat_ok;
7342 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7343 isl_pw_multi_aff_copy(data->pma), pma2);
7345 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7346 if (!data->res)
7347 return isl_stat_error;
7349 return isl_stat_ok;
7352 /* Compute the pullback of "upma1" by the function represented by "upma2".
7354 __isl_give isl_union_pw_multi_aff *
7355 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7356 __isl_take isl_union_pw_multi_aff *upma1,
7357 __isl_take isl_union_pw_multi_aff *upma2)
7359 return bin_op(upma1, upma2, &pullback_entry);
7362 /* Check that the domain space of "upa" matches "space".
7364 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7365 * can in principle never fail since the space "space" is that
7366 * of the isl_multi_union_pw_aff and is a set space such that
7367 * there is no domain space to match.
7369 * We check the parameters and double-check that "space" is
7370 * indeed that of a set.
7372 static isl_stat isl_union_pw_aff_check_match_domain_space(
7373 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7375 isl_space *upa_space;
7376 isl_bool match;
7378 if (!upa || !space)
7379 return isl_stat_error;
7381 match = isl_space_is_set(space);
7382 if (match < 0)
7383 return isl_stat_error;
7384 if (!match)
7385 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7386 "expecting set space", return isl_stat_error);
7388 upa_space = isl_union_pw_aff_get_space(upa);
7389 match = isl_space_has_equal_params(space, upa_space);
7390 if (match < 0)
7391 goto error;
7392 if (!match)
7393 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7394 "parameters don't match", goto error);
7396 isl_space_free(upa_space);
7397 return isl_stat_ok;
7398 error:
7399 isl_space_free(upa_space);
7400 return isl_stat_error;
7403 /* Do the parameters of "upa" match those of "space"?
7405 static isl_bool isl_union_pw_aff_matching_params(
7406 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7408 isl_space *upa_space;
7409 isl_bool match;
7411 if (!upa || !space)
7412 return isl_bool_error;
7414 upa_space = isl_union_pw_aff_get_space(upa);
7416 match = isl_space_has_equal_params(space, upa_space);
7418 isl_space_free(upa_space);
7419 return match;
7422 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7423 * space represents the new parameters.
7424 * res collects the results.
7426 struct isl_union_pw_aff_reset_params_data {
7427 isl_space *space;
7428 isl_union_pw_aff *res;
7431 /* Replace the parameters of "pa" by data->space and
7432 * add the result to data->res.
7434 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7436 struct isl_union_pw_aff_reset_params_data *data = user;
7437 isl_space *space;
7439 space = isl_pw_aff_get_space(pa);
7440 space = isl_space_replace_params(space, data->space);
7441 pa = isl_pw_aff_reset_space(pa, space);
7442 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7444 return data->res ? isl_stat_ok : isl_stat_error;
7447 /* Replace the domain space of "upa" by "space".
7448 * Since a union expression does not have a (single) domain space,
7449 * "space" is necessarily a parameter space.
7451 * Since the order and the names of the parameters determine
7452 * the hash value, we need to create a new hash table.
7454 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7455 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7457 struct isl_union_pw_aff_reset_params_data data = { space };
7458 isl_bool match;
7460 match = isl_union_pw_aff_matching_params(upa, space);
7461 if (match < 0)
7462 upa = isl_union_pw_aff_free(upa);
7463 else if (match) {
7464 isl_space_free(space);
7465 return upa;
7468 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7469 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7470 data.res = isl_union_pw_aff_free(data.res);
7472 isl_union_pw_aff_free(upa);
7473 isl_space_free(space);
7474 return data.res;
7477 /* Return the floor of "pa".
7479 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7481 return isl_pw_aff_floor(pa);
7484 /* Given f, return floor(f).
7486 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7487 __isl_take isl_union_pw_aff *upa)
7489 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7492 /* Compute
7494 * upa mod m = upa - m * floor(upa/m)
7496 * with m an integer value.
7498 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7499 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7501 isl_union_pw_aff *res;
7503 if (!upa || !m)
7504 goto error;
7506 if (!isl_val_is_int(m))
7507 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7508 "expecting integer modulo", goto error);
7509 if (!isl_val_is_pos(m))
7510 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7511 "expecting positive modulo", goto error);
7513 res = isl_union_pw_aff_copy(upa);
7514 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7515 upa = isl_union_pw_aff_floor(upa);
7516 upa = isl_union_pw_aff_scale_val(upa, m);
7517 res = isl_union_pw_aff_sub(res, upa);
7519 return res;
7520 error:
7521 isl_val_free(m);
7522 isl_union_pw_aff_free(upa);
7523 return NULL;
7526 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7527 * pos is the output position that needs to be extracted.
7528 * res collects the results.
7530 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7531 int pos;
7532 isl_union_pw_aff *res;
7535 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7536 * (assuming it has such a dimension) and add it to data->res.
7538 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7540 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7541 int n_out;
7542 isl_pw_aff *pa;
7544 if (!pma)
7545 return isl_stat_error;
7547 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7548 if (data->pos >= n_out) {
7549 isl_pw_multi_aff_free(pma);
7550 return isl_stat_ok;
7553 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7554 isl_pw_multi_aff_free(pma);
7556 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7558 return data->res ? isl_stat_ok : isl_stat_error;
7561 /* Extract an isl_union_pw_aff corresponding to
7562 * output dimension "pos" of "upma".
7564 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7565 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7567 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7568 isl_space *space;
7570 if (!upma)
7571 return NULL;
7573 if (pos < 0)
7574 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7575 "cannot extract at negative position", return NULL);
7577 space = isl_union_pw_multi_aff_get_space(upma);
7578 data.res = isl_union_pw_aff_empty(space);
7579 data.pos = pos;
7580 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7581 &get_union_pw_aff, &data) < 0)
7582 data.res = isl_union_pw_aff_free(data.res);
7584 return data.res;
7587 /* Return a union piecewise affine expression
7588 * that is equal to "aff" on "domain".
7590 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7591 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7593 isl_pw_aff *pa;
7595 pa = isl_pw_aff_from_aff(aff);
7596 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7599 /* Return a union piecewise affine expression
7600 * that is equal to the parameter identified by "id" on "domain".
7602 * Make sure the parameter appears in the space passed to
7603 * isl_aff_param_on_domain_space_id.
7605 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7606 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7608 isl_space *space;
7609 isl_aff *aff;
7611 space = isl_union_set_get_space(domain);
7612 space = isl_space_add_param_id(space, isl_id_copy(id));
7613 aff = isl_aff_param_on_domain_space_id(space, id);
7614 return isl_union_pw_aff_aff_on_domain(domain, aff);
7617 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7618 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7619 * needs to attain.
7620 * "res" collects the results.
7622 struct isl_union_pw_aff_pw_aff_on_domain_data {
7623 isl_pw_aff *pa;
7624 isl_union_pw_aff *res;
7627 /* Construct a piecewise affine expression that is equal to data->pa
7628 * on "domain" and add the result to data->res.
7630 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7632 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7633 isl_pw_aff *pa;
7634 int dim;
7636 pa = isl_pw_aff_copy(data->pa);
7637 dim = isl_set_dim(domain, isl_dim_set);
7638 pa = isl_pw_aff_from_range(pa);
7639 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7640 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7641 pa = isl_pw_aff_intersect_domain(pa, domain);
7642 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7644 return data->res ? isl_stat_ok : isl_stat_error;
7647 /* Return a union piecewise affine expression
7648 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7649 * have been aligned.
7651 * Construct an isl_pw_aff on each of the sets in "domain" and
7652 * collect the results.
7654 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7655 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7657 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7658 isl_space *space;
7660 space = isl_union_set_get_space(domain);
7661 data.res = isl_union_pw_aff_empty(space);
7662 data.pa = pa;
7663 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7664 data.res = isl_union_pw_aff_free(data.res);
7665 isl_union_set_free(domain);
7666 isl_pw_aff_free(pa);
7667 return data.res;
7670 /* Return a union piecewise affine expression
7671 * that is equal to "pa" on "domain".
7673 * Check that "pa" is a parametric expression,
7674 * align the parameters if needed and call
7675 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7677 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7678 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7680 isl_bool is_set;
7681 isl_bool equal_params;
7682 isl_space *domain_space, *pa_space;
7684 pa_space = isl_pw_aff_peek_space(pa);
7685 is_set = isl_space_is_set(pa_space);
7686 if (is_set < 0)
7687 goto error;
7688 if (!is_set)
7689 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7690 "expecting parametric expression", goto error);
7692 domain_space = isl_union_set_get_space(domain);
7693 pa_space = isl_pw_aff_get_space(pa);
7694 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7695 if (equal_params >= 0 && !equal_params) {
7696 isl_space *space;
7698 space = isl_space_align_params(domain_space, pa_space);
7699 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7700 domain = isl_union_set_align_params(domain, space);
7701 } else {
7702 isl_space_free(domain_space);
7703 isl_space_free(pa_space);
7706 if (equal_params < 0)
7707 goto error;
7708 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7709 error:
7710 isl_union_set_free(domain);
7711 isl_pw_aff_free(pa);
7712 return NULL;
7715 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7716 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7717 * "res" collects the results.
7719 struct isl_union_pw_aff_val_on_domain_data {
7720 isl_val *v;
7721 isl_union_pw_aff *res;
7724 /* Construct a piecewise affine expression that is equal to data->v
7725 * on "domain" and add the result to data->res.
7727 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7729 struct isl_union_pw_aff_val_on_domain_data *data = user;
7730 isl_pw_aff *pa;
7731 isl_val *v;
7733 v = isl_val_copy(data->v);
7734 pa = isl_pw_aff_val_on_domain(domain, v);
7735 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7737 return data->res ? isl_stat_ok : isl_stat_error;
7740 /* Return a union piecewise affine expression
7741 * that is equal to "v" on "domain".
7743 * Construct an isl_pw_aff on each of the sets in "domain" and
7744 * collect the results.
7746 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7747 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7749 struct isl_union_pw_aff_val_on_domain_data data;
7750 isl_space *space;
7752 space = isl_union_set_get_space(domain);
7753 data.res = isl_union_pw_aff_empty(space);
7754 data.v = v;
7755 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7756 data.res = isl_union_pw_aff_free(data.res);
7757 isl_union_set_free(domain);
7758 isl_val_free(v);
7759 return data.res;
7762 /* Construct a piecewise multi affine expression
7763 * that is equal to "pa" and add it to upma.
7765 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7766 void *user)
7768 isl_union_pw_multi_aff **upma = user;
7769 isl_pw_multi_aff *pma;
7771 pma = isl_pw_multi_aff_from_pw_aff(pa);
7772 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7774 return *upma ? isl_stat_ok : isl_stat_error;
7777 /* Construct and return a union piecewise multi affine expression
7778 * that is equal to the given union piecewise affine expression.
7780 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7781 __isl_take isl_union_pw_aff *upa)
7783 isl_space *space;
7784 isl_union_pw_multi_aff *upma;
7786 if (!upa)
7787 return NULL;
7789 space = isl_union_pw_aff_get_space(upa);
7790 upma = isl_union_pw_multi_aff_empty(space);
7792 if (isl_union_pw_aff_foreach_pw_aff(upa,
7793 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7794 upma = isl_union_pw_multi_aff_free(upma);
7796 isl_union_pw_aff_free(upa);
7797 return upma;
7800 /* Compute the set of elements in the domain of "pa" where it is zero and
7801 * add this set to "uset".
7803 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7805 isl_union_set **uset = (isl_union_set **)user;
7807 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7809 return *uset ? isl_stat_ok : isl_stat_error;
7812 /* Return a union set containing those elements in the domain
7813 * of "upa" where it is zero.
7815 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7816 __isl_take isl_union_pw_aff *upa)
7818 isl_union_set *zero;
7820 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7821 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7822 zero = isl_union_set_free(zero);
7824 isl_union_pw_aff_free(upa);
7825 return zero;
7828 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7829 * upma is the function that is plugged in.
7830 * pa is the current part of the function in which upma is plugged in.
7831 * res collects the results.
7833 struct isl_union_pw_aff_pullback_upma_data {
7834 isl_union_pw_multi_aff *upma;
7835 isl_pw_aff *pa;
7836 isl_union_pw_aff *res;
7839 /* Check if "pma" can be plugged into data->pa.
7840 * If so, perform the pullback and add the result to data->res.
7842 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7844 struct isl_union_pw_aff_pullback_upma_data *data = user;
7845 isl_pw_aff *pa;
7847 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7848 pma->dim, isl_dim_out)) {
7849 isl_pw_multi_aff_free(pma);
7850 return isl_stat_ok;
7853 pa = isl_pw_aff_copy(data->pa);
7854 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7856 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7858 return data->res ? isl_stat_ok : isl_stat_error;
7861 /* Check if any of the elements of data->upma can be plugged into pa,
7862 * add if so add the result to data->res.
7864 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7866 struct isl_union_pw_aff_pullback_upma_data *data = user;
7867 isl_stat r;
7869 data->pa = pa;
7870 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7871 &pa_pb_pma, data);
7872 isl_pw_aff_free(pa);
7874 return r;
7877 /* Compute the pullback of "upa" by the function represented by "upma".
7878 * In other words, plug in "upma" in "upa". The result contains
7879 * expressions defined over the domain space of "upma".
7881 * Run over all pairs of elements in "upa" and "upma", perform
7882 * the pullback when appropriate and collect the results.
7883 * If the hash value were based on the domain space rather than
7884 * the function space, then we could run through all elements
7885 * of "upma" and directly pick out the corresponding element of "upa".
7887 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7888 __isl_take isl_union_pw_aff *upa,
7889 __isl_take isl_union_pw_multi_aff *upma)
7891 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7892 isl_space *space;
7894 space = isl_union_pw_multi_aff_get_space(upma);
7895 upa = isl_union_pw_aff_align_params(upa, space);
7896 space = isl_union_pw_aff_get_space(upa);
7897 upma = isl_union_pw_multi_aff_align_params(upma, space);
7899 if (!upa || !upma)
7900 goto error;
7902 data.upma = upma;
7903 data.res = isl_union_pw_aff_alloc_same_size(upa);
7904 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7905 data.res = isl_union_pw_aff_free(data.res);
7907 isl_union_pw_aff_free(upa);
7908 isl_union_pw_multi_aff_free(upma);
7909 return data.res;
7910 error:
7911 isl_union_pw_aff_free(upa);
7912 isl_union_pw_multi_aff_free(upma);
7913 return NULL;
7916 #undef BASE
7917 #define BASE union_pw_aff
7918 #undef DOMBASE
7919 #define DOMBASE union_set
7921 #include <isl_multi_explicit_domain.c>
7922 #include <isl_multi_union_pw_aff_explicit_domain.c>
7923 #include <isl_multi_templ.c>
7924 #include <isl_multi_apply_set.c>
7925 #include <isl_multi_apply_union_set.c>
7926 #include <isl_multi_coalesce.c>
7927 #include <isl_multi_floor.c>
7928 #include <isl_multi_from_base_templ.c>
7929 #include <isl_multi_gist.c>
7930 #include <isl_multi_align_set.c>
7931 #include <isl_multi_align_union_set.c>
7932 #include <isl_multi_intersect.c>
7934 /* Does "mupa" have a non-trivial explicit domain?
7936 * The explicit domain, if present, is trivial if it represents
7937 * an (obviously) universe parameter set.
7939 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
7940 __isl_keep isl_multi_union_pw_aff *mupa)
7942 isl_bool is_params, trivial;
7943 isl_set *set;
7945 if (!mupa)
7946 return isl_bool_error;
7947 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
7948 return isl_bool_false;
7949 is_params = isl_union_set_is_params(mupa->u.dom);
7950 if (is_params < 0 || !is_params)
7951 return isl_bool_not(is_params);
7952 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
7953 trivial = isl_set_plain_is_universe(set);
7954 isl_set_free(set);
7955 return isl_bool_not(trivial);
7958 /* Construct a multiple union piecewise affine expression
7959 * in the given space with value zero in each of the output dimensions.
7961 * Since there is no canonical zero value for
7962 * a union piecewise affine expression, we can only construct
7963 * a zero-dimensional "zero" value.
7965 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7966 __isl_take isl_space *space)
7968 isl_bool params;
7970 if (!space)
7971 return NULL;
7973 params = isl_space_is_params(space);
7974 if (params < 0)
7975 goto error;
7976 if (params)
7977 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7978 "expecting proper set space", goto error);
7979 if (!isl_space_is_set(space))
7980 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7981 "expecting set space", goto error);
7982 if (isl_space_dim(space , isl_dim_out) != 0)
7983 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7984 "expecting 0D space", goto error);
7986 return isl_multi_union_pw_aff_alloc(space);
7987 error:
7988 isl_space_free(space);
7989 return NULL;
7992 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7993 * with the actual sum on the shared domain and
7994 * the defined expression on the symmetric difference of the domains.
7996 * We simply iterate over the elements in both arguments and
7997 * call isl_union_pw_aff_union_add on each of them, if there is
7998 * at least one element.
8000 * Otherwise, the two expressions have an explicit domain and
8001 * the union of these explicit domains is computed.
8002 * This assumes that the explicit domains are either both in terms
8003 * of specific domains elements or both in terms of parameters.
8004 * However, if one of the expressions does not have any constraints
8005 * on its explicit domain, then this is allowed as well and the result
8006 * is the expression with no constraints on its explicit domain.
8008 static __isl_give isl_multi_union_pw_aff *
8009 isl_multi_union_pw_aff_union_add_aligned(
8010 __isl_take isl_multi_union_pw_aff *mupa1,
8011 __isl_take isl_multi_union_pw_aff *mupa2)
8013 isl_bool has_domain, is_params1, is_params2;
8015 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8016 goto error;
8017 if (mupa1->n > 0)
8018 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8019 &isl_union_pw_aff_union_add);
8020 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8021 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8022 goto error;
8024 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8025 if (has_domain < 0)
8026 goto error;
8027 if (!has_domain) {
8028 isl_multi_union_pw_aff_free(mupa2);
8029 return mupa1;
8031 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8032 if (has_domain < 0)
8033 goto error;
8034 if (!has_domain) {
8035 isl_multi_union_pw_aff_free(mupa1);
8036 return mupa2;
8039 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8040 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8041 if (is_params1 < 0 || is_params2 < 0)
8042 goto error;
8043 if (is_params1 != is_params2)
8044 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8045 isl_error_invalid,
8046 "cannot compute union of concrete domain and "
8047 "parameter constraints", goto error);
8048 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8049 if (!mupa1)
8050 goto error;
8051 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8052 isl_union_set_copy(mupa2->u.dom));
8053 if (!mupa1->u.dom)
8054 goto error;
8055 isl_multi_union_pw_aff_free(mupa2);
8056 return mupa1;
8057 error:
8058 isl_multi_union_pw_aff_free(mupa1);
8059 isl_multi_union_pw_aff_free(mupa2);
8060 return NULL;
8063 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8064 * with the actual sum on the shared domain and
8065 * the defined expression on the symmetric difference of the domains.
8067 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8068 __isl_take isl_multi_union_pw_aff *mupa1,
8069 __isl_take isl_multi_union_pw_aff *mupa2)
8071 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8072 &isl_multi_union_pw_aff_union_add_aligned);
8075 /* Construct and return a multi union piecewise affine expression
8076 * that is equal to the given multi affine expression.
8078 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8079 __isl_take isl_multi_aff *ma)
8081 isl_multi_pw_aff *mpa;
8083 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8084 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8087 /* Construct and return a multi union piecewise affine expression
8088 * that is equal to the given multi piecewise affine expression.
8090 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8091 __isl_take isl_multi_pw_aff *mpa)
8093 int i, n;
8094 isl_space *space;
8095 isl_multi_union_pw_aff *mupa;
8097 if (!mpa)
8098 return NULL;
8100 space = isl_multi_pw_aff_get_space(mpa);
8101 space = isl_space_range(space);
8102 mupa = isl_multi_union_pw_aff_alloc(space);
8104 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8105 for (i = 0; i < n; ++i) {
8106 isl_pw_aff *pa;
8107 isl_union_pw_aff *upa;
8109 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8110 upa = isl_union_pw_aff_from_pw_aff(pa);
8111 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8114 isl_multi_pw_aff_free(mpa);
8116 return mupa;
8119 /* Extract the range space of "pma" and assign it to *space.
8120 * If *space has already been set (through a previous call to this function),
8121 * then check that the range space is the same.
8123 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8125 isl_space **space = user;
8126 isl_space *pma_space;
8127 isl_bool equal;
8129 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8130 isl_pw_multi_aff_free(pma);
8132 if (!pma_space)
8133 return isl_stat_error;
8134 if (!*space) {
8135 *space = pma_space;
8136 return isl_stat_ok;
8139 equal = isl_space_is_equal(pma_space, *space);
8140 isl_space_free(pma_space);
8142 if (equal < 0)
8143 return isl_stat_error;
8144 if (!equal)
8145 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8146 "range spaces not the same", return isl_stat_error);
8147 return isl_stat_ok;
8150 /* Construct and return a multi union piecewise affine expression
8151 * that is equal to the given union piecewise multi affine expression.
8153 * In order to be able to perform the conversion, the input
8154 * needs to be non-empty and may only involve a single range space.
8156 * If the resulting multi union piecewise affine expression has
8157 * an explicit domain, then assign it the domain of the input.
8158 * In other cases, the domain is stored in the individual elements.
8160 __isl_give isl_multi_union_pw_aff *
8161 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8162 __isl_take isl_union_pw_multi_aff *upma)
8164 isl_space *space = NULL;
8165 isl_multi_union_pw_aff *mupa;
8166 int i, n;
8168 if (!upma)
8169 return NULL;
8170 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8171 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8172 "cannot extract range space from empty input",
8173 goto error);
8174 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8175 &space) < 0)
8176 goto error;
8178 if (!space)
8179 goto error;
8181 n = isl_space_dim(space, isl_dim_set);
8182 mupa = isl_multi_union_pw_aff_alloc(space);
8184 for (i = 0; i < n; ++i) {
8185 isl_union_pw_aff *upa;
8187 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8188 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8190 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8191 isl_union_set *dom;
8192 isl_union_pw_multi_aff *copy;
8194 copy = isl_union_pw_multi_aff_copy(upma);
8195 dom = isl_union_pw_multi_aff_domain(copy);
8196 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8199 isl_union_pw_multi_aff_free(upma);
8200 return mupa;
8201 error:
8202 isl_space_free(space);
8203 isl_union_pw_multi_aff_free(upma);
8204 return NULL;
8207 /* Try and create an isl_multi_union_pw_aff that is equivalent
8208 * to the given isl_union_map.
8209 * The isl_union_map is required to be single-valued in each space.
8210 * Moreover, it cannot be empty and all range spaces need to be the same.
8211 * Otherwise, an error is produced.
8213 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8214 __isl_take isl_union_map *umap)
8216 isl_union_pw_multi_aff *upma;
8218 upma = isl_union_pw_multi_aff_from_union_map(umap);
8219 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8222 /* Return a multiple union piecewise affine expression
8223 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8224 * have been aligned.
8226 * If the resulting multi union piecewise affine expression has
8227 * an explicit domain, then assign it the input domain.
8228 * In other cases, the domain is stored in the individual elements.
8230 static __isl_give isl_multi_union_pw_aff *
8231 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8232 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8234 int i, n;
8235 isl_space *space;
8236 isl_multi_union_pw_aff *mupa;
8238 if (!domain || !mv)
8239 goto error;
8241 n = isl_multi_val_dim(mv, isl_dim_set);
8242 space = isl_multi_val_get_space(mv);
8243 mupa = isl_multi_union_pw_aff_alloc(space);
8244 for (i = 0; i < n; ++i) {
8245 isl_val *v;
8246 isl_union_pw_aff *upa;
8248 v = isl_multi_val_get_val(mv, i);
8249 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8251 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8253 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8254 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8255 isl_union_set_copy(domain));
8257 isl_union_set_free(domain);
8258 isl_multi_val_free(mv);
8259 return mupa;
8260 error:
8261 isl_union_set_free(domain);
8262 isl_multi_val_free(mv);
8263 return NULL;
8266 /* Return a multiple union piecewise affine expression
8267 * that is equal to "mv" on "domain".
8269 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8270 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8272 isl_bool equal_params;
8274 if (!domain || !mv)
8275 goto error;
8276 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8277 if (equal_params < 0)
8278 goto error;
8279 if (equal_params)
8280 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8281 domain, mv);
8282 domain = isl_union_set_align_params(domain,
8283 isl_multi_val_get_space(mv));
8284 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8285 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8286 error:
8287 isl_union_set_free(domain);
8288 isl_multi_val_free(mv);
8289 return NULL;
8292 /* Return a multiple union piecewise affine expression
8293 * that is equal to "ma" on "domain".
8295 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8296 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8298 isl_pw_multi_aff *pma;
8300 pma = isl_pw_multi_aff_from_multi_aff(ma);
8301 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8304 /* Return a multiple union piecewise affine expression
8305 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8306 * have been aligned.
8308 * If the resulting multi union piecewise affine expression has
8309 * an explicit domain, then assign it the input domain.
8310 * In other cases, the domain is stored in the individual elements.
8312 static __isl_give isl_multi_union_pw_aff *
8313 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8314 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8316 int i, n;
8317 isl_space *space;
8318 isl_multi_union_pw_aff *mupa;
8320 if (!domain || !pma)
8321 goto error;
8323 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8324 space = isl_pw_multi_aff_get_space(pma);
8325 mupa = isl_multi_union_pw_aff_alloc(space);
8326 for (i = 0; i < n; ++i) {
8327 isl_pw_aff *pa;
8328 isl_union_pw_aff *upa;
8330 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8331 upa = isl_union_pw_aff_pw_aff_on_domain(
8332 isl_union_set_copy(domain), pa);
8333 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8335 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8336 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8337 isl_union_set_copy(domain));
8339 isl_union_set_free(domain);
8340 isl_pw_multi_aff_free(pma);
8341 return mupa;
8342 error:
8343 isl_union_set_free(domain);
8344 isl_pw_multi_aff_free(pma);
8345 return NULL;
8348 /* Return a multiple union piecewise affine expression
8349 * that is equal to "pma" on "domain".
8351 __isl_give isl_multi_union_pw_aff *
8352 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8353 __isl_take isl_pw_multi_aff *pma)
8355 isl_bool equal_params;
8356 isl_space *space;
8358 space = isl_pw_multi_aff_peek_space(pma);
8359 equal_params = isl_union_set_space_has_equal_params(domain, space);
8360 if (equal_params < 0)
8361 goto error;
8362 if (equal_params)
8363 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8364 domain, pma);
8365 domain = isl_union_set_align_params(domain,
8366 isl_pw_multi_aff_get_space(pma));
8367 pma = isl_pw_multi_aff_align_params(pma,
8368 isl_union_set_get_space(domain));
8369 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8370 pma);
8371 error:
8372 isl_union_set_free(domain);
8373 isl_pw_multi_aff_free(pma);
8374 return NULL;
8377 /* Return a union set containing those elements in the domains
8378 * of the elements of "mupa" where they are all zero.
8380 * If there are no elements, then simply return the entire domain.
8382 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8383 __isl_take isl_multi_union_pw_aff *mupa)
8385 int i, n;
8386 isl_union_pw_aff *upa;
8387 isl_union_set *zero;
8389 if (!mupa)
8390 return NULL;
8392 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8393 if (n == 0)
8394 return isl_multi_union_pw_aff_domain(mupa);
8396 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8397 zero = isl_union_pw_aff_zero_union_set(upa);
8399 for (i = 1; i < n; ++i) {
8400 isl_union_set *zero_i;
8402 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8403 zero_i = isl_union_pw_aff_zero_union_set(upa);
8405 zero = isl_union_set_intersect(zero, zero_i);
8408 isl_multi_union_pw_aff_free(mupa);
8409 return zero;
8412 /* Construct a union map mapping the shared domain
8413 * of the union piecewise affine expressions to the range of "mupa"
8414 * in the special case of a 0D multi union piecewise affine expression.
8416 * Construct a map between the explicit domain of "mupa" and
8417 * the range space.
8418 * Note that this assumes that the domain consists of explicit elements.
8420 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8421 __isl_take isl_multi_union_pw_aff *mupa)
8423 isl_bool is_params;
8424 isl_space *space;
8425 isl_union_set *dom, *ran;
8427 space = isl_multi_union_pw_aff_get_space(mupa);
8428 dom = isl_multi_union_pw_aff_domain(mupa);
8429 ran = isl_union_set_from_set(isl_set_universe(space));
8431 is_params = isl_union_set_is_params(dom);
8432 if (is_params < 0)
8433 dom = isl_union_set_free(dom);
8434 else if (is_params)
8435 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8436 "cannot create union map from expression without "
8437 "explicit domain elements",
8438 dom = isl_union_set_free(dom));
8440 return isl_union_map_from_domain_and_range(dom, ran);
8443 /* Construct a union map mapping the shared domain
8444 * of the union piecewise affine expressions to the range of "mupa"
8445 * with each dimension in the range equated to the
8446 * corresponding union piecewise affine expression.
8448 * If the input is zero-dimensional, then construct a mapping
8449 * from its explicit domain.
8451 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8452 __isl_take isl_multi_union_pw_aff *mupa)
8454 int i, n;
8455 isl_space *space;
8456 isl_union_map *umap;
8457 isl_union_pw_aff *upa;
8459 if (!mupa)
8460 return NULL;
8462 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8463 if (n == 0)
8464 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8466 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8467 umap = isl_union_map_from_union_pw_aff(upa);
8469 for (i = 1; i < n; ++i) {
8470 isl_union_map *umap_i;
8472 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8473 umap_i = isl_union_map_from_union_pw_aff(upa);
8474 umap = isl_union_map_flat_range_product(umap, umap_i);
8477 space = isl_multi_union_pw_aff_get_space(mupa);
8478 umap = isl_union_map_reset_range_space(umap, space);
8480 isl_multi_union_pw_aff_free(mupa);
8481 return umap;
8484 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8485 * "range" is the space from which to set the range space.
8486 * "res" collects the results.
8488 struct isl_union_pw_multi_aff_reset_range_space_data {
8489 isl_space *range;
8490 isl_union_pw_multi_aff *res;
8493 /* Replace the range space of "pma" by the range space of data->range and
8494 * add the result to data->res.
8496 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8498 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8499 isl_space *space;
8501 space = isl_pw_multi_aff_get_space(pma);
8502 space = isl_space_domain(space);
8503 space = isl_space_extend_domain_with_range(space,
8504 isl_space_copy(data->range));
8505 pma = isl_pw_multi_aff_reset_space(pma, space);
8506 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8508 return data->res ? isl_stat_ok : isl_stat_error;
8511 /* Replace the range space of all the piecewise affine expressions in "upma" by
8512 * the range space of "space".
8514 * This assumes that all these expressions have the same output dimension.
8516 * Since the spaces of the expressions change, so do their hash values.
8517 * We therefore need to create a new isl_union_pw_multi_aff.
8518 * Note that the hash value is currently computed based on the entire
8519 * space even though there can only be a single expression with a given
8520 * domain space.
8522 static __isl_give isl_union_pw_multi_aff *
8523 isl_union_pw_multi_aff_reset_range_space(
8524 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8526 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8527 isl_space *space_upma;
8529 space_upma = isl_union_pw_multi_aff_get_space(upma);
8530 data.res = isl_union_pw_multi_aff_empty(space_upma);
8531 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8532 &reset_range_space, &data) < 0)
8533 data.res = isl_union_pw_multi_aff_free(data.res);
8535 isl_space_free(space);
8536 isl_union_pw_multi_aff_free(upma);
8537 return data.res;
8540 /* Construct and return a union piecewise multi affine expression
8541 * that is equal to the given multi union piecewise affine expression,
8542 * in the special case of a 0D multi union piecewise affine expression.
8544 * Construct a union piecewise multi affine expression
8545 * on top of the explicit domain of the input.
8547 __isl_give isl_union_pw_multi_aff *
8548 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8549 __isl_take isl_multi_union_pw_aff *mupa)
8551 isl_space *space;
8552 isl_multi_val *mv;
8553 isl_union_set *domain;
8555 space = isl_multi_union_pw_aff_get_space(mupa);
8556 mv = isl_multi_val_zero(space);
8557 domain = isl_multi_union_pw_aff_domain(mupa);
8558 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8561 /* Construct and return a union piecewise multi affine expression
8562 * that is equal to the given multi union piecewise affine expression.
8564 * If the input is zero-dimensional, then
8565 * construct a union piecewise multi affine expression
8566 * on top of the explicit domain of the input.
8568 __isl_give isl_union_pw_multi_aff *
8569 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8570 __isl_take isl_multi_union_pw_aff *mupa)
8572 int i, n;
8573 isl_space *space;
8574 isl_union_pw_multi_aff *upma;
8575 isl_union_pw_aff *upa;
8577 if (!mupa)
8578 return NULL;
8580 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8581 if (n == 0)
8582 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8584 space = isl_multi_union_pw_aff_get_space(mupa);
8585 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8586 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8588 for (i = 1; i < n; ++i) {
8589 isl_union_pw_multi_aff *upma_i;
8591 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8592 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8593 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8596 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8598 isl_multi_union_pw_aff_free(mupa);
8599 return upma;
8602 /* Intersect the range of "mupa" with "range",
8603 * in the special case where "mupa" is 0D.
8605 * Intersect the domain of "mupa" with the constraints on the parameters
8606 * of "range".
8608 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8609 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8611 range = isl_set_params(range);
8612 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8613 return mupa;
8616 /* Intersect the range of "mupa" with "range".
8617 * That is, keep only those domain elements that have a function value
8618 * in "range".
8620 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8621 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8623 isl_union_pw_multi_aff *upma;
8624 isl_union_set *domain;
8625 isl_space *space;
8626 int n;
8627 int match;
8629 if (!mupa || !range)
8630 goto error;
8632 space = isl_set_get_space(range);
8633 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8634 space, isl_dim_set);
8635 isl_space_free(space);
8636 if (match < 0)
8637 goto error;
8638 if (!match)
8639 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8640 "space don't match", goto error);
8641 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8642 if (n == 0)
8643 return mupa_intersect_range_0D(mupa, range);
8645 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8646 isl_multi_union_pw_aff_copy(mupa));
8647 domain = isl_union_set_from_set(range);
8648 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8649 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8651 return mupa;
8652 error:
8653 isl_multi_union_pw_aff_free(mupa);
8654 isl_set_free(range);
8655 return NULL;
8658 /* Return the shared domain of the elements of "mupa",
8659 * in the special case where "mupa" is zero-dimensional.
8661 * Return the explicit domain of "mupa".
8662 * Note that this domain may be a parameter set, either
8663 * because "mupa" is meant to live in a set space or
8664 * because no explicit domain has been set.
8666 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8667 __isl_take isl_multi_union_pw_aff *mupa)
8669 isl_union_set *dom;
8671 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8672 isl_multi_union_pw_aff_free(mupa);
8674 return dom;
8677 /* Return the shared domain of the elements of "mupa".
8679 * If "mupa" is zero-dimensional, then return its explicit domain.
8681 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8682 __isl_take isl_multi_union_pw_aff *mupa)
8684 int i, n;
8685 isl_union_pw_aff *upa;
8686 isl_union_set *dom;
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_multi_union_pw_aff_domain_0D(mupa);
8695 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8696 dom = isl_union_pw_aff_domain(upa);
8697 for (i = 1; i < n; ++i) {
8698 isl_union_set *dom_i;
8700 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8701 dom_i = isl_union_pw_aff_domain(upa);
8702 dom = isl_union_set_intersect(dom, dom_i);
8705 isl_multi_union_pw_aff_free(mupa);
8706 return dom;
8709 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8710 * In particular, the spaces have been aligned.
8711 * The result is defined over the shared domain of the elements of "mupa"
8713 * We first extract the parametric constant part of "aff" and
8714 * define that over the shared domain.
8715 * Then we iterate over all input dimensions of "aff" and add the corresponding
8716 * multiples of the elements of "mupa".
8717 * Finally, we consider the integer divisions, calling the function
8718 * recursively to obtain an isl_union_pw_aff corresponding to the
8719 * integer division argument.
8721 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8722 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8724 int i, n_in, n_div;
8725 isl_union_pw_aff *upa;
8726 isl_union_set *uset;
8727 isl_val *v;
8728 isl_aff *cst;
8730 n_in = isl_aff_dim(aff, isl_dim_in);
8731 n_div = isl_aff_dim(aff, isl_dim_div);
8733 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8734 cst = isl_aff_copy(aff);
8735 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8736 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8737 cst = isl_aff_project_domain_on_params(cst);
8738 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8740 for (i = 0; i < n_in; ++i) {
8741 isl_union_pw_aff *upa_i;
8743 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8744 continue;
8745 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8746 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8747 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8748 upa = isl_union_pw_aff_add(upa, upa_i);
8751 for (i = 0; i < n_div; ++i) {
8752 isl_aff *div;
8753 isl_union_pw_aff *upa_i;
8755 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8756 continue;
8757 div = isl_aff_get_div(aff, i);
8758 upa_i = multi_union_pw_aff_apply_aff(
8759 isl_multi_union_pw_aff_copy(mupa), div);
8760 upa_i = isl_union_pw_aff_floor(upa_i);
8761 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8762 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8763 upa = isl_union_pw_aff_add(upa, upa_i);
8766 isl_multi_union_pw_aff_free(mupa);
8767 isl_aff_free(aff);
8769 return upa;
8772 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8773 * with the domain of "aff".
8774 * Furthermore, the dimension of this space needs to be greater than zero.
8775 * The result is defined over the shared domain of the elements of "mupa"
8777 * We perform these checks and then hand over control to
8778 * multi_union_pw_aff_apply_aff.
8780 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8781 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8783 isl_space *space1, *space2;
8784 int equal;
8786 mupa = isl_multi_union_pw_aff_align_params(mupa,
8787 isl_aff_get_space(aff));
8788 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8789 if (!mupa || !aff)
8790 goto error;
8792 space1 = isl_multi_union_pw_aff_get_space(mupa);
8793 space2 = isl_aff_get_domain_space(aff);
8794 equal = isl_space_is_equal(space1, space2);
8795 isl_space_free(space1);
8796 isl_space_free(space2);
8797 if (equal < 0)
8798 goto error;
8799 if (!equal)
8800 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8801 "spaces don't match", goto error);
8802 if (isl_aff_dim(aff, isl_dim_in) == 0)
8803 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8804 "cannot determine domains", goto error);
8806 return multi_union_pw_aff_apply_aff(mupa, aff);
8807 error:
8808 isl_multi_union_pw_aff_free(mupa);
8809 isl_aff_free(aff);
8810 return NULL;
8813 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8814 * The space of "mupa" is known to be compatible with the domain of "ma".
8816 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8817 * on the domain of "mupa".
8819 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
8820 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8822 isl_union_set *dom;
8824 dom = isl_multi_union_pw_aff_domain(mupa);
8825 ma = isl_multi_aff_project_domain_on_params(ma);
8827 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
8830 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8831 * with the domain of "ma".
8832 * The result is defined over the shared domain of the elements of "mupa"
8834 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8835 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8837 isl_space *space1, *space2;
8838 isl_multi_union_pw_aff *res;
8839 int equal;
8840 int i, n_out;
8842 mupa = isl_multi_union_pw_aff_align_params(mupa,
8843 isl_multi_aff_get_space(ma));
8844 ma = isl_multi_aff_align_params(ma,
8845 isl_multi_union_pw_aff_get_space(mupa));
8846 if (!mupa || !ma)
8847 goto error;
8849 space1 = isl_multi_union_pw_aff_get_space(mupa);
8850 space2 = isl_multi_aff_get_domain_space(ma);
8851 equal = isl_space_is_equal(space1, space2);
8852 isl_space_free(space1);
8853 isl_space_free(space2);
8854 if (equal < 0)
8855 goto error;
8856 if (!equal)
8857 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8858 "spaces don't match", goto error);
8859 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8860 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
8861 return mupa_apply_multi_aff_0D(mupa, ma);
8863 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8864 res = isl_multi_union_pw_aff_alloc(space1);
8866 for (i = 0; i < n_out; ++i) {
8867 isl_aff *aff;
8868 isl_union_pw_aff *upa;
8870 aff = isl_multi_aff_get_aff(ma, i);
8871 upa = multi_union_pw_aff_apply_aff(
8872 isl_multi_union_pw_aff_copy(mupa), aff);
8873 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8876 isl_multi_aff_free(ma);
8877 isl_multi_union_pw_aff_free(mupa);
8878 return res;
8879 error:
8880 isl_multi_union_pw_aff_free(mupa);
8881 isl_multi_aff_free(ma);
8882 return NULL;
8885 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
8886 * The space of "mupa" is known to be compatible with the domain of "pa".
8888 * Construct an isl_multi_union_pw_aff that is equal to "pa"
8889 * on the domain of "mupa".
8891 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
8892 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8894 isl_union_set *dom;
8896 dom = isl_multi_union_pw_aff_domain(mupa);
8897 pa = isl_pw_aff_project_domain_on_params(pa);
8899 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
8902 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8903 * with the domain of "pa".
8904 * Furthermore, the dimension of this space needs to be greater than zero.
8905 * The result is defined over the shared domain of the elements of "mupa"
8907 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8908 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8910 int i;
8911 int equal;
8912 isl_space *space, *space2;
8913 isl_union_pw_aff *upa;
8915 mupa = isl_multi_union_pw_aff_align_params(mupa,
8916 isl_pw_aff_get_space(pa));
8917 pa = isl_pw_aff_align_params(pa,
8918 isl_multi_union_pw_aff_get_space(mupa));
8919 if (!mupa || !pa)
8920 goto error;
8922 space = isl_multi_union_pw_aff_get_space(mupa);
8923 space2 = isl_pw_aff_get_domain_space(pa);
8924 equal = isl_space_is_equal(space, space2);
8925 isl_space_free(space);
8926 isl_space_free(space2);
8927 if (equal < 0)
8928 goto error;
8929 if (!equal)
8930 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8931 "spaces don't match", goto error);
8932 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8933 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
8935 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8936 upa = isl_union_pw_aff_empty(space);
8938 for (i = 0; i < pa->n; ++i) {
8939 isl_aff *aff;
8940 isl_set *domain;
8941 isl_multi_union_pw_aff *mupa_i;
8942 isl_union_pw_aff *upa_i;
8944 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8945 domain = isl_set_copy(pa->p[i].set);
8946 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8947 aff = isl_aff_copy(pa->p[i].aff);
8948 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8949 upa = isl_union_pw_aff_union_add(upa, upa_i);
8952 isl_multi_union_pw_aff_free(mupa);
8953 isl_pw_aff_free(pa);
8954 return upa;
8955 error:
8956 isl_multi_union_pw_aff_free(mupa);
8957 isl_pw_aff_free(pa);
8958 return NULL;
8961 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
8962 * The space of "mupa" is known to be compatible with the domain of "pma".
8964 * Construct an isl_multi_union_pw_aff that is equal to "pma"
8965 * on the domain of "mupa".
8967 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
8968 __isl_take isl_multi_union_pw_aff *mupa,
8969 __isl_take isl_pw_multi_aff *pma)
8971 isl_union_set *dom;
8973 dom = isl_multi_union_pw_aff_domain(mupa);
8974 pma = isl_pw_multi_aff_project_domain_on_params(pma);
8976 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
8979 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8980 * with the domain of "pma".
8981 * The result is defined over the shared domain of the elements of "mupa"
8983 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8984 __isl_take isl_multi_union_pw_aff *mupa,
8985 __isl_take isl_pw_multi_aff *pma)
8987 isl_space *space1, *space2;
8988 isl_multi_union_pw_aff *res;
8989 int equal;
8990 int i, n_out;
8992 mupa = isl_multi_union_pw_aff_align_params(mupa,
8993 isl_pw_multi_aff_get_space(pma));
8994 pma = isl_pw_multi_aff_align_params(pma,
8995 isl_multi_union_pw_aff_get_space(mupa));
8996 if (!mupa || !pma)
8997 goto error;
8999 space1 = isl_multi_union_pw_aff_get_space(mupa);
9000 space2 = isl_pw_multi_aff_get_domain_space(pma);
9001 equal = isl_space_is_equal(space1, space2);
9002 isl_space_free(space1);
9003 isl_space_free(space2);
9004 if (equal < 0)
9005 goto error;
9006 if (!equal)
9007 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9008 "spaces don't match", goto error);
9009 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9010 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9011 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9013 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9014 res = isl_multi_union_pw_aff_alloc(space1);
9016 for (i = 0; i < n_out; ++i) {
9017 isl_pw_aff *pa;
9018 isl_union_pw_aff *upa;
9020 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9021 upa = isl_multi_union_pw_aff_apply_pw_aff(
9022 isl_multi_union_pw_aff_copy(mupa), pa);
9023 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9026 isl_pw_multi_aff_free(pma);
9027 isl_multi_union_pw_aff_free(mupa);
9028 return res;
9029 error:
9030 isl_multi_union_pw_aff_free(mupa);
9031 isl_pw_multi_aff_free(pma);
9032 return NULL;
9035 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9036 * If the explicit domain only keeps track of constraints on the parameters,
9037 * then only update those constraints.
9039 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9040 __isl_take isl_multi_union_pw_aff *mupa,
9041 __isl_keep isl_union_pw_multi_aff *upma)
9043 isl_bool is_params;
9045 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9046 return isl_multi_union_pw_aff_free(mupa);
9048 mupa = isl_multi_union_pw_aff_cow(mupa);
9049 if (!mupa)
9050 return NULL;
9052 is_params = isl_union_set_is_params(mupa->u.dom);
9053 if (is_params < 0)
9054 return isl_multi_union_pw_aff_free(mupa);
9056 upma = isl_union_pw_multi_aff_copy(upma);
9057 if (is_params)
9058 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9059 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9060 else
9061 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9062 mupa->u.dom, upma);
9063 if (!mupa->u.dom)
9064 return isl_multi_union_pw_aff_free(mupa);
9065 return mupa;
9068 /* Compute the pullback of "mupa" by the function represented by "upma".
9069 * In other words, plug in "upma" in "mupa". The result contains
9070 * expressions defined over the domain space of "upma".
9072 * Run over all elements of "mupa" and plug in "upma" in each of them.
9074 * If "mupa" has an explicit domain, then it is this domain
9075 * that needs to undergo a pullback instead, i.e., a preimage.
9077 __isl_give isl_multi_union_pw_aff *
9078 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9079 __isl_take isl_multi_union_pw_aff *mupa,
9080 __isl_take isl_union_pw_multi_aff *upma)
9082 int i, n;
9084 mupa = isl_multi_union_pw_aff_align_params(mupa,
9085 isl_union_pw_multi_aff_get_space(upma));
9086 upma = isl_union_pw_multi_aff_align_params(upma,
9087 isl_multi_union_pw_aff_get_space(mupa));
9088 mupa = isl_multi_union_pw_aff_cow(mupa);
9089 if (!mupa || !upma)
9090 goto error;
9092 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9093 for (i = 0; i < n; ++i) {
9094 isl_union_pw_aff *upa;
9096 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9097 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9098 isl_union_pw_multi_aff_copy(upma));
9099 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9102 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9103 mupa = preimage_explicit_domain(mupa, upma);
9105 isl_union_pw_multi_aff_free(upma);
9106 return mupa;
9107 error:
9108 isl_multi_union_pw_aff_free(mupa);
9109 isl_union_pw_multi_aff_free(upma);
9110 return NULL;
9113 /* Extract the sequence of elements in "mupa" with domain space "space"
9114 * (ignoring parameters).
9116 * For the elements of "mupa" that are not defined on the specified space,
9117 * the corresponding element in the result is empty.
9119 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9120 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9122 int i, n;
9123 isl_space *space_mpa;
9124 isl_multi_pw_aff *mpa;
9126 if (!mupa || !space)
9127 goto error;
9129 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9130 space = isl_space_replace_params(space, space_mpa);
9131 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9132 space_mpa);
9133 mpa = isl_multi_pw_aff_alloc(space_mpa);
9135 space = isl_space_from_domain(space);
9136 space = isl_space_add_dims(space, isl_dim_out, 1);
9137 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9138 for (i = 0; i < n; ++i) {
9139 isl_union_pw_aff *upa;
9140 isl_pw_aff *pa;
9142 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9143 pa = isl_union_pw_aff_extract_pw_aff(upa,
9144 isl_space_copy(space));
9145 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9146 isl_union_pw_aff_free(upa);
9149 isl_space_free(space);
9150 return mpa;
9151 error:
9152 isl_space_free(space);
9153 return NULL;
9156 /* Evaluate the affine function "aff" in the void point "pnt".
9157 * In particular, return the value NaN.
9159 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9160 __isl_take isl_point *pnt)
9162 isl_ctx *ctx;
9164 ctx = isl_point_get_ctx(pnt);
9165 isl_aff_free(aff);
9166 isl_point_free(pnt);
9167 return isl_val_nan(ctx);
9170 /* Evaluate the affine expression "aff"
9171 * in the coordinates (with denominator) "pnt".
9173 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9174 __isl_keep isl_vec *pnt)
9176 isl_int n, d;
9177 isl_ctx *ctx;
9178 isl_val *v;
9180 if (!aff || !pnt)
9181 return NULL;
9183 ctx = isl_vec_get_ctx(aff);
9184 isl_int_init(n);
9185 isl_int_init(d);
9186 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9187 isl_int_mul(d, aff->el[0], pnt->el[0]);
9188 v = isl_val_rat_from_isl_int(ctx, n, d);
9189 v = isl_val_normalize(v);
9190 isl_int_clear(n);
9191 isl_int_clear(d);
9193 return v;
9196 /* Check that the domain space of "aff" is equal to "space".
9198 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9199 __isl_keep isl_space *space)
9201 isl_bool ok;
9203 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9204 if (ok < 0)
9205 return isl_stat_error;
9206 if (!ok)
9207 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9208 "incompatible spaces", return isl_stat_error);
9209 return isl_stat_ok;
9212 /* Evaluate the affine function "aff" in "pnt".
9214 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9215 __isl_take isl_point *pnt)
9217 isl_bool is_void;
9218 isl_val *v;
9219 isl_local_space *ls;
9221 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9222 goto error;
9223 is_void = isl_point_is_void(pnt);
9224 if (is_void < 0)
9225 goto error;
9226 if (is_void)
9227 return eval_void(aff, pnt);
9229 ls = isl_aff_get_domain_local_space(aff);
9230 pnt = isl_local_space_lift_point(ls, pnt);
9232 v = eval(aff->v, isl_point_peek_vec(pnt));
9234 isl_aff_free(aff);
9235 isl_point_free(pnt);
9237 return v;
9238 error:
9239 isl_aff_free(aff);
9240 isl_point_free(pnt);
9241 return NULL;