isl_map_simplify.c: remove_redundant_divs: drop div constraints of redundant div
[isl.git] / isl_aff.c
blobd9456507d670f87d72dea6175bb142ea143b3239
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_local_space *ls;
2096 isl_basic_set *hull;
2098 ls = isl_aff_get_domain_local_space(aff);
2099 context = isl_local_space_lift_set(ls, context);
2101 hull = isl_set_affine_hull(context);
2102 return isl_aff_substitute_equalities_lifted(aff, hull);
2105 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2106 __isl_take isl_set *context)
2108 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2109 dom_context = isl_set_intersect_params(dom_context, context);
2110 return isl_aff_gist(aff, dom_context);
2113 /* Return a basic set containing those elements in the space
2114 * of aff where it is positive. "rational" should not be set.
2116 * If "aff" is NaN, then it is not positive.
2118 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2119 int rational)
2121 isl_constraint *ineq;
2122 isl_basic_set *bset;
2123 isl_val *c;
2125 if (!aff)
2126 return NULL;
2127 if (isl_aff_is_nan(aff)) {
2128 isl_space *space = isl_aff_get_domain_space(aff);
2129 isl_aff_free(aff);
2130 return isl_basic_set_empty(space);
2132 if (rational)
2133 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2134 "rational sets not supported", goto error);
2136 ineq = isl_inequality_from_aff(aff);
2137 c = isl_constraint_get_constant_val(ineq);
2138 c = isl_val_sub_ui(c, 1);
2139 ineq = isl_constraint_set_constant_val(ineq, c);
2141 bset = isl_basic_set_from_constraint(ineq);
2142 bset = isl_basic_set_simplify(bset);
2143 return bset;
2144 error:
2145 isl_aff_free(aff);
2146 return NULL;
2149 /* Return a basic set containing those elements in the space
2150 * of aff where it is non-negative.
2151 * If "rational" is set, then return a rational basic set.
2153 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2155 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2156 __isl_take isl_aff *aff, int rational)
2158 isl_constraint *ineq;
2159 isl_basic_set *bset;
2161 if (!aff)
2162 return NULL;
2163 if (isl_aff_is_nan(aff)) {
2164 isl_space *space = isl_aff_get_domain_space(aff);
2165 isl_aff_free(aff);
2166 return isl_basic_set_empty(space);
2169 ineq = isl_inequality_from_aff(aff);
2171 bset = isl_basic_set_from_constraint(ineq);
2172 if (rational)
2173 bset = isl_basic_set_set_rational(bset);
2174 bset = isl_basic_set_simplify(bset);
2175 return bset;
2178 /* Return a basic set containing those elements in the space
2179 * of aff where it is non-negative.
2181 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2183 return aff_nonneg_basic_set(aff, 0);
2186 /* Return a basic set containing those elements in the domain space
2187 * of "aff" where it is positive.
2189 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2191 aff = isl_aff_add_constant_num_si(aff, -1);
2192 return isl_aff_nonneg_basic_set(aff);
2195 /* Return a basic set containing those elements in the domain space
2196 * of aff where it is negative.
2198 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2200 aff = isl_aff_neg(aff);
2201 return isl_aff_pos_basic_set(aff);
2204 /* Return a basic set containing those elements in the space
2205 * of aff where it is zero.
2206 * If "rational" is set, then return a rational basic set.
2208 * If "aff" is NaN, then it is not zero.
2210 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2211 int rational)
2213 isl_constraint *ineq;
2214 isl_basic_set *bset;
2216 if (!aff)
2217 return NULL;
2218 if (isl_aff_is_nan(aff)) {
2219 isl_space *space = isl_aff_get_domain_space(aff);
2220 isl_aff_free(aff);
2221 return isl_basic_set_empty(space);
2224 ineq = isl_equality_from_aff(aff);
2226 bset = isl_basic_set_from_constraint(ineq);
2227 if (rational)
2228 bset = isl_basic_set_set_rational(bset);
2229 bset = isl_basic_set_simplify(bset);
2230 return bset;
2233 /* Return a basic set containing those elements in the space
2234 * of aff where it is zero.
2236 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2238 return aff_zero_basic_set(aff, 0);
2241 /* Return a basic set containing those elements in the shared space
2242 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2244 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2245 __isl_take isl_aff *aff2)
2247 aff1 = isl_aff_sub(aff1, aff2);
2249 return isl_aff_nonneg_basic_set(aff1);
2252 /* Return a basic set containing those elements in the shared domain space
2253 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2255 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2256 __isl_take isl_aff *aff2)
2258 aff1 = isl_aff_sub(aff1, aff2);
2260 return isl_aff_pos_basic_set(aff1);
2263 /* Return a set containing those elements in the shared space
2264 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2266 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2267 __isl_take isl_aff *aff2)
2269 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2272 /* Return a set containing those elements in the shared domain space
2273 * of aff1 and aff2 where aff1 is greater than aff2.
2275 * If either of the two inputs is NaN, then the result is empty,
2276 * as comparisons with NaN always return false.
2278 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2279 __isl_take isl_aff *aff2)
2281 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2284 /* Return a basic set containing those elements in the shared space
2285 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2287 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2288 __isl_take isl_aff *aff2)
2290 return isl_aff_ge_basic_set(aff2, aff1);
2293 /* Return a basic set containing those elements in the shared domain space
2294 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2296 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2297 __isl_take isl_aff *aff2)
2299 return isl_aff_gt_basic_set(aff2, aff1);
2302 /* Return a set containing those elements in the shared space
2303 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2305 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2306 __isl_take isl_aff *aff2)
2308 return isl_aff_ge_set(aff2, aff1);
2311 /* Return a set containing those elements in the shared domain space
2312 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2314 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2315 __isl_take isl_aff *aff2)
2317 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2320 /* Return a basic set containing those elements in the shared space
2321 * of aff1 and aff2 where aff1 and aff2 are equal.
2323 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2324 __isl_take isl_aff *aff2)
2326 aff1 = isl_aff_sub(aff1, aff2);
2328 return isl_aff_zero_basic_set(aff1);
2331 /* Return a set containing those elements in the shared space
2332 * of aff1 and aff2 where aff1 and aff2 are equal.
2334 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2335 __isl_take isl_aff *aff2)
2337 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2340 /* Return a set containing those elements in the shared domain space
2341 * of aff1 and aff2 where aff1 and aff2 are not equal.
2343 * If either of the two inputs is NaN, then the result is empty,
2344 * as comparisons with NaN always return false.
2346 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2347 __isl_take isl_aff *aff2)
2349 isl_set *set_lt, *set_gt;
2351 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2352 isl_aff_copy(aff2));
2353 set_gt = isl_aff_gt_set(aff1, aff2);
2354 return isl_set_union_disjoint(set_lt, set_gt);
2357 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2358 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2360 aff1 = isl_aff_add(aff1, aff2);
2361 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2362 return aff1;
2365 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2367 if (!aff)
2368 return -1;
2370 return 0;
2373 /* Check whether the given affine expression has non-zero coefficient
2374 * for any dimension in the given range or if any of these dimensions
2375 * appear with non-zero coefficients in any of the integer divisions
2376 * involved in the affine expression.
2378 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2379 enum isl_dim_type type, unsigned first, unsigned n)
2381 int i;
2382 isl_ctx *ctx;
2383 int *active = NULL;
2384 isl_bool involves = isl_bool_false;
2386 if (!aff)
2387 return isl_bool_error;
2388 if (n == 0)
2389 return isl_bool_false;
2391 ctx = isl_aff_get_ctx(aff);
2392 if (first + n > isl_aff_dim(aff, type))
2393 isl_die(ctx, isl_error_invalid,
2394 "range out of bounds", return isl_bool_error);
2396 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2397 if (!active)
2398 goto error;
2400 first += isl_local_space_offset(aff->ls, type) - 1;
2401 for (i = 0; i < n; ++i)
2402 if (active[first + i]) {
2403 involves = isl_bool_true;
2404 break;
2407 free(active);
2409 return involves;
2410 error:
2411 free(active);
2412 return isl_bool_error;
2415 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2416 enum isl_dim_type type, unsigned first, unsigned n)
2418 isl_ctx *ctx;
2420 if (!aff)
2421 return NULL;
2422 if (type == isl_dim_out)
2423 isl_die(aff->v->ctx, isl_error_invalid,
2424 "cannot drop output/set dimension",
2425 return isl_aff_free(aff));
2426 if (type == isl_dim_in)
2427 type = isl_dim_set;
2428 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2429 return aff;
2431 ctx = isl_aff_get_ctx(aff);
2432 if (first + n > isl_local_space_dim(aff->ls, type))
2433 isl_die(ctx, isl_error_invalid, "range out of bounds",
2434 return isl_aff_free(aff));
2436 aff = isl_aff_cow(aff);
2437 if (!aff)
2438 return NULL;
2440 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2441 if (!aff->ls)
2442 return isl_aff_free(aff);
2444 first += 1 + isl_local_space_offset(aff->ls, type);
2445 aff->v = isl_vec_drop_els(aff->v, first, n);
2446 if (!aff->v)
2447 return isl_aff_free(aff);
2449 return aff;
2452 /* Drop the "n" domain dimensions starting at "first" from "aff",
2453 * after checking that they do not appear in the affine expression.
2455 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2456 unsigned n)
2458 isl_bool involves;
2460 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2461 if (involves < 0)
2462 return isl_aff_free(aff);
2463 if (involves)
2464 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2465 "affine expression involves some of the domain dimensions",
2466 return isl_aff_free(aff));
2467 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2470 /* Project the domain of the affine expression onto its parameter space.
2471 * The affine expression may not involve any of the domain dimensions.
2473 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2475 isl_space *space;
2476 unsigned n;
2478 n = isl_aff_dim(aff, isl_dim_in);
2479 aff = drop_domain(aff, 0, n);
2480 space = isl_aff_get_domain_space(aff);
2481 space = isl_space_params(space);
2482 aff = isl_aff_reset_domain_space(aff, space);
2483 return aff;
2486 /* Check that the domain of "aff" is a product.
2488 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2490 isl_bool is_product;
2492 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2493 if (is_product < 0)
2494 return isl_stat_error;
2495 if (!is_product)
2496 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2497 "domain is not a product", return isl_stat_error);
2498 return isl_stat_ok;
2501 /* Given an affine function with a domain of the form [A -> B] that
2502 * does not depend on B, return the same function on domain A.
2504 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2506 isl_space *space;
2507 int n, n_in;
2509 if (check_domain_product(aff) < 0)
2510 return isl_aff_free(aff);
2511 space = isl_aff_get_domain_space(aff);
2512 n = isl_space_dim(space, isl_dim_set);
2513 space = isl_space_factor_domain(space);
2514 n_in = isl_space_dim(space, isl_dim_set);
2515 aff = drop_domain(aff, n_in, n - n_in);
2516 aff = isl_aff_reset_domain_space(aff, space);
2517 return aff;
2520 /* Convert an affine expression defined over a parameter domain
2521 * into one that is defined over a zero-dimensional set.
2523 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2525 isl_local_space *ls;
2527 ls = isl_aff_take_domain_local_space(aff);
2528 ls = isl_local_space_set_from_params(ls);
2529 aff = isl_aff_restore_domain_local_space(aff, ls);
2531 return aff;
2534 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2535 enum isl_dim_type type, unsigned first, unsigned n)
2537 isl_ctx *ctx;
2539 if (!aff)
2540 return NULL;
2541 if (type == isl_dim_out)
2542 isl_die(aff->v->ctx, isl_error_invalid,
2543 "cannot insert output/set dimensions",
2544 return isl_aff_free(aff));
2545 if (type == isl_dim_in)
2546 type = isl_dim_set;
2547 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2548 return aff;
2550 ctx = isl_aff_get_ctx(aff);
2551 if (first > isl_local_space_dim(aff->ls, type))
2552 isl_die(ctx, isl_error_invalid, "position out of bounds",
2553 return isl_aff_free(aff));
2555 aff = isl_aff_cow(aff);
2556 if (!aff)
2557 return NULL;
2559 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2560 if (!aff->ls)
2561 return isl_aff_free(aff);
2563 first += 1 + isl_local_space_offset(aff->ls, type);
2564 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2565 if (!aff->v)
2566 return isl_aff_free(aff);
2568 return aff;
2571 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2572 enum isl_dim_type type, unsigned n)
2574 unsigned pos;
2576 pos = isl_aff_dim(aff, type);
2578 return isl_aff_insert_dims(aff, type, pos, n);
2581 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2582 enum isl_dim_type type, unsigned n)
2584 unsigned pos;
2586 pos = isl_pw_aff_dim(pwaff, type);
2588 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2591 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2592 * to dimensions of "dst_type" at "dst_pos".
2594 * We only support moving input dimensions to parameters and vice versa.
2596 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2597 enum isl_dim_type dst_type, unsigned dst_pos,
2598 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2600 unsigned g_dst_pos;
2601 unsigned g_src_pos;
2603 if (!aff)
2604 return NULL;
2605 if (n == 0 &&
2606 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2607 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2608 return aff;
2610 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2611 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2612 "cannot move output/set dimension",
2613 return isl_aff_free(aff));
2614 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2615 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2616 "cannot move divs", return isl_aff_free(aff));
2617 if (dst_type == isl_dim_in)
2618 dst_type = isl_dim_set;
2619 if (src_type == isl_dim_in)
2620 src_type = isl_dim_set;
2622 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2623 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2624 "range out of bounds", return isl_aff_free(aff));
2625 if (dst_type == src_type)
2626 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2627 "moving dims within the same type not supported",
2628 return isl_aff_free(aff));
2630 aff = isl_aff_cow(aff);
2631 if (!aff)
2632 return NULL;
2634 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2635 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2636 if (dst_type > src_type)
2637 g_dst_pos -= n;
2639 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2640 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2641 src_type, src_pos, n);
2642 if (!aff->v || !aff->ls)
2643 return isl_aff_free(aff);
2645 aff = sort_divs(aff);
2647 return aff;
2650 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2652 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2653 return isl_pw_aff_alloc(dom, aff);
2656 #define isl_aff_involves_nan isl_aff_is_nan
2658 #undef PW
2659 #define PW isl_pw_aff
2660 #undef EL
2661 #define EL isl_aff
2662 #undef EL_IS_ZERO
2663 #define EL_IS_ZERO is_empty
2664 #undef ZERO
2665 #define ZERO empty
2666 #undef IS_ZERO
2667 #define IS_ZERO is_empty
2668 #undef FIELD
2669 #define FIELD aff
2670 #undef DEFAULT_IS_ZERO
2671 #define DEFAULT_IS_ZERO 0
2673 #define NO_OPT
2674 #define NO_LIFT
2675 #define NO_MORPH
2677 #include <isl_pw_templ.c>
2678 #include <isl_pw_eval.c>
2679 #include <isl_pw_hash.c>
2680 #include <isl_pw_union_opt.c>
2682 #undef BASE
2683 #define BASE pw_aff
2685 #include <isl_union_single.c>
2686 #include <isl_union_neg.c>
2688 static __isl_give isl_set *align_params_pw_pw_set_and(
2689 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2690 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2691 __isl_take isl_pw_aff *pwaff2))
2693 isl_bool equal_params;
2695 if (!pwaff1 || !pwaff2)
2696 goto error;
2697 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2698 if (equal_params < 0)
2699 goto error;
2700 if (equal_params)
2701 return fn(pwaff1, pwaff2);
2702 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2703 isl_pw_aff_check_named_params(pwaff2) < 0)
2704 goto error;
2705 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2706 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2707 return fn(pwaff1, pwaff2);
2708 error:
2709 isl_pw_aff_free(pwaff1);
2710 isl_pw_aff_free(pwaff2);
2711 return NULL;
2714 /* Align the parameters of the to isl_pw_aff arguments and
2715 * then apply a function "fn" on them that returns an isl_map.
2717 static __isl_give isl_map *align_params_pw_pw_map_and(
2718 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2719 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2720 __isl_take isl_pw_aff *pa2))
2722 isl_bool equal_params;
2724 if (!pa1 || !pa2)
2725 goto error;
2726 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2727 if (equal_params < 0)
2728 goto error;
2729 if (equal_params)
2730 return fn(pa1, pa2);
2731 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2732 isl_pw_aff_check_named_params(pa2) < 0)
2733 goto error;
2734 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2735 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2736 return fn(pa1, pa2);
2737 error:
2738 isl_pw_aff_free(pa1);
2739 isl_pw_aff_free(pa2);
2740 return NULL;
2743 /* Compute a piecewise quasi-affine expression with a domain that
2744 * is the union of those of pwaff1 and pwaff2 and such that on each
2745 * cell, the quasi-affine expression is the maximum of those of pwaff1
2746 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2747 * cell, then the associated expression is the defined one.
2749 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2750 __isl_take isl_pw_aff *pwaff2)
2752 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2755 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2756 __isl_take isl_pw_aff *pwaff2)
2758 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2759 &pw_aff_union_max);
2762 /* Compute a piecewise quasi-affine expression with a domain that
2763 * is the union of those of pwaff1 and pwaff2 and such that on each
2764 * cell, the quasi-affine expression is the minimum of those of pwaff1
2765 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2766 * cell, then the associated expression is the defined one.
2768 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2769 __isl_take isl_pw_aff *pwaff2)
2771 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2774 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2775 __isl_take isl_pw_aff *pwaff2)
2777 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2778 &pw_aff_union_min);
2781 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2782 __isl_take isl_pw_aff *pwaff2, int max)
2784 if (max)
2785 return isl_pw_aff_union_max(pwaff1, pwaff2);
2786 else
2787 return isl_pw_aff_union_min(pwaff1, pwaff2);
2790 /* Return a set containing those elements in the domain
2791 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2792 * does not satisfy "fn" (if complement is 1).
2794 * The pieces with a NaN never belong to the result since
2795 * NaN does not satisfy any property.
2797 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2798 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2799 int complement)
2801 int i;
2802 isl_set *set;
2804 if (!pwaff)
2805 return NULL;
2807 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2809 for (i = 0; i < pwaff->n; ++i) {
2810 isl_basic_set *bset;
2811 isl_set *set_i, *locus;
2812 isl_bool rational;
2814 if (isl_aff_is_nan(pwaff->p[i].aff))
2815 continue;
2817 rational = isl_set_has_rational(pwaff->p[i].set);
2818 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2819 locus = isl_set_from_basic_set(bset);
2820 set_i = isl_set_copy(pwaff->p[i].set);
2821 if (complement)
2822 set_i = isl_set_subtract(set_i, locus);
2823 else
2824 set_i = isl_set_intersect(set_i, locus);
2825 set = isl_set_union_disjoint(set, set_i);
2828 isl_pw_aff_free(pwaff);
2830 return set;
2833 /* Return a set containing those elements in the domain
2834 * of "pa" where it is positive.
2836 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2838 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2841 /* Return a set containing those elements in the domain
2842 * of pwaff where it is non-negative.
2844 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2846 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2849 /* Return a set containing those elements in the domain
2850 * of pwaff where it is zero.
2852 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2854 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2857 /* Return a set containing those elements in the domain
2858 * of pwaff where it is not zero.
2860 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2862 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2865 /* Return a set containing those elements in the shared domain
2866 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2868 * We compute the difference on the shared domain and then construct
2869 * the set of values where this difference is non-negative.
2870 * If strict is set, we first subtract 1 from the difference.
2871 * If equal is set, we only return the elements where pwaff1 and pwaff2
2872 * are equal.
2874 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2875 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2877 isl_set *set1, *set2;
2879 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2880 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2881 set1 = isl_set_intersect(set1, set2);
2882 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2883 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2884 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2886 if (strict) {
2887 isl_space *space = isl_set_get_space(set1);
2888 isl_aff *aff;
2889 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2890 aff = isl_aff_add_constant_si(aff, -1);
2891 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2892 } else
2893 isl_set_free(set1);
2895 if (equal)
2896 return isl_pw_aff_zero_set(pwaff1);
2897 return isl_pw_aff_nonneg_set(pwaff1);
2900 /* Return a set containing those elements in the shared domain
2901 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2903 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2904 __isl_take isl_pw_aff *pwaff2)
2906 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2909 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2910 __isl_take isl_pw_aff *pwaff2)
2912 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2915 /* Return a set containing those elements in the shared domain
2916 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2918 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2919 __isl_take isl_pw_aff *pwaff2)
2921 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2924 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2925 __isl_take isl_pw_aff *pwaff2)
2927 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2930 /* Return a set containing those elements in the shared domain
2931 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2933 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2934 __isl_take isl_pw_aff *pwaff2)
2936 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2939 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2940 __isl_take isl_pw_aff *pwaff2)
2942 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2945 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2946 __isl_take isl_pw_aff *pwaff2)
2948 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2951 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2952 __isl_take isl_pw_aff *pwaff2)
2954 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2957 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2958 * where the function values are ordered in the same way as "order",
2959 * which returns a set in the shared domain of its two arguments.
2960 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2962 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2963 * We first pull back the two functions such that they are defined on
2964 * the domain [A -> B]. Then we apply "order", resulting in a set
2965 * in the space [A -> B]. Finally, we unwrap this set to obtain
2966 * a map in the space A -> B.
2968 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2969 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2970 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2971 __isl_take isl_pw_aff *pa2))
2973 isl_space *space1, *space2;
2974 isl_multi_aff *ma;
2975 isl_set *set;
2977 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2978 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2979 space1 = isl_space_map_from_domain_and_range(space1, space2);
2980 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2981 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2982 ma = isl_multi_aff_range_map(space1);
2983 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2984 set = order(pa1, pa2);
2986 return isl_set_unwrap(set);
2989 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2990 * where the function values are equal.
2991 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2993 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
2994 __isl_take isl_pw_aff *pa2)
2996 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
2999 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3000 * where the function values are equal.
3002 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3003 __isl_take isl_pw_aff *pa2)
3005 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3008 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3009 * where the function value of "pa1" is less than the function value of "pa2".
3010 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3012 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3013 __isl_take isl_pw_aff *pa2)
3015 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3018 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3019 * where the function value of "pa1" is less than the function value of "pa2".
3021 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3022 __isl_take isl_pw_aff *pa2)
3024 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3027 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3028 * where the function value of "pa1" is greater than the function value
3029 * of "pa2".
3030 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3032 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3033 __isl_take isl_pw_aff *pa2)
3035 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3038 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3039 * where the function value of "pa1" is greater than the function value
3040 * of "pa2".
3042 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3043 __isl_take isl_pw_aff *pa2)
3045 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3048 /* Return a set containing those elements in the shared domain
3049 * of the elements of list1 and list2 where each element in list1
3050 * has the relation specified by "fn" with each element in list2.
3052 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3053 __isl_take isl_pw_aff_list *list2,
3054 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3055 __isl_take isl_pw_aff *pwaff2))
3057 int i, j;
3058 isl_ctx *ctx;
3059 isl_set *set;
3061 if (!list1 || !list2)
3062 goto error;
3064 ctx = isl_pw_aff_list_get_ctx(list1);
3065 if (list1->n < 1 || list2->n < 1)
3066 isl_die(ctx, isl_error_invalid,
3067 "list should contain at least one element", goto error);
3069 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3070 for (i = 0; i < list1->n; ++i)
3071 for (j = 0; j < list2->n; ++j) {
3072 isl_set *set_ij;
3074 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3075 isl_pw_aff_copy(list2->p[j]));
3076 set = isl_set_intersect(set, set_ij);
3079 isl_pw_aff_list_free(list1);
3080 isl_pw_aff_list_free(list2);
3081 return set;
3082 error:
3083 isl_pw_aff_list_free(list1);
3084 isl_pw_aff_list_free(list2);
3085 return NULL;
3088 /* Return a set containing those elements in the shared domain
3089 * of the elements of list1 and list2 where each element in list1
3090 * is equal to each element in list2.
3092 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3093 __isl_take isl_pw_aff_list *list2)
3095 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3098 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3099 __isl_take isl_pw_aff_list *list2)
3101 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
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 less than or equal to each element in list2.
3108 __isl_give isl_set *isl_pw_aff_list_le_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_le_set);
3114 __isl_give isl_set *isl_pw_aff_list_lt_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_lt_set);
3120 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3121 __isl_take isl_pw_aff_list *list2)
3123 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3126 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3127 __isl_take isl_pw_aff_list *list2)
3129 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3133 /* Return a set containing those elements in the shared domain
3134 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3136 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3137 __isl_take isl_pw_aff *pwaff2)
3139 isl_set *set_lt, *set_gt;
3141 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3142 isl_pw_aff_copy(pwaff2));
3143 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3144 return isl_set_union_disjoint(set_lt, set_gt);
3147 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3148 __isl_take isl_pw_aff *pwaff2)
3150 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3153 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3154 isl_int v)
3156 int i;
3158 if (isl_int_is_one(v))
3159 return pwaff;
3160 if (!isl_int_is_pos(v))
3161 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3162 "factor needs to be positive",
3163 return isl_pw_aff_free(pwaff));
3164 pwaff = isl_pw_aff_cow(pwaff);
3165 if (!pwaff)
3166 return NULL;
3167 if (pwaff->n == 0)
3168 return pwaff;
3170 for (i = 0; i < pwaff->n; ++i) {
3171 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3172 if (!pwaff->p[i].aff)
3173 return isl_pw_aff_free(pwaff);
3176 return pwaff;
3179 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3181 int i;
3183 pwaff = isl_pw_aff_cow(pwaff);
3184 if (!pwaff)
3185 return NULL;
3186 if (pwaff->n == 0)
3187 return pwaff;
3189 for (i = 0; i < pwaff->n; ++i) {
3190 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3191 if (!pwaff->p[i].aff)
3192 return isl_pw_aff_free(pwaff);
3195 return pwaff;
3198 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3200 int i;
3202 pwaff = isl_pw_aff_cow(pwaff);
3203 if (!pwaff)
3204 return NULL;
3205 if (pwaff->n == 0)
3206 return pwaff;
3208 for (i = 0; i < pwaff->n; ++i) {
3209 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3210 if (!pwaff->p[i].aff)
3211 return isl_pw_aff_free(pwaff);
3214 return pwaff;
3217 /* Assuming that "cond1" and "cond2" are disjoint,
3218 * return an affine expression that is equal to pwaff1 on cond1
3219 * and to pwaff2 on cond2.
3221 static __isl_give isl_pw_aff *isl_pw_aff_select(
3222 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3223 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3225 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3226 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3228 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3231 /* Return an affine expression that is equal to pwaff_true for elements
3232 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3233 * is zero.
3234 * That is, return cond ? pwaff_true : pwaff_false;
3236 * If "cond" involves and NaN, then we conservatively return a NaN
3237 * on its entire domain. In principle, we could consider the pieces
3238 * where it is NaN separately from those where it is not.
3240 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3241 * then only use the domain of "cond" to restrict the domain.
3243 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3244 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3246 isl_set *cond_true, *cond_false;
3247 isl_bool equal;
3249 if (!cond)
3250 goto error;
3251 if (isl_pw_aff_involves_nan(cond)) {
3252 isl_space *space = isl_pw_aff_get_domain_space(cond);
3253 isl_local_space *ls = isl_local_space_from_space(space);
3254 isl_pw_aff_free(cond);
3255 isl_pw_aff_free(pwaff_true);
3256 isl_pw_aff_free(pwaff_false);
3257 return isl_pw_aff_nan_on_domain(ls);
3260 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3261 isl_pw_aff_get_space(pwaff_false));
3262 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3263 isl_pw_aff_get_space(pwaff_true));
3264 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3265 if (equal < 0)
3266 goto error;
3267 if (equal) {
3268 isl_set *dom;
3270 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3271 isl_pw_aff_free(pwaff_false);
3272 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3275 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3276 cond_false = isl_pw_aff_zero_set(cond);
3277 return isl_pw_aff_select(cond_true, pwaff_true,
3278 cond_false, pwaff_false);
3279 error:
3280 isl_pw_aff_free(cond);
3281 isl_pw_aff_free(pwaff_true);
3282 isl_pw_aff_free(pwaff_false);
3283 return NULL;
3286 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3288 if (!aff)
3289 return isl_bool_error;
3291 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3294 /* Check whether pwaff is a piecewise constant.
3296 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3298 int i;
3300 if (!pwaff)
3301 return isl_bool_error;
3303 for (i = 0; i < pwaff->n; ++i) {
3304 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3305 if (is_cst < 0 || !is_cst)
3306 return is_cst;
3309 return isl_bool_true;
3312 /* Are all elements of "mpa" piecewise constants?
3314 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3316 int i;
3318 if (!mpa)
3319 return isl_bool_error;
3321 for (i = 0; i < mpa->n; ++i) {
3322 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3323 if (is_cst < 0 || !is_cst)
3324 return is_cst;
3327 return isl_bool_true;
3330 /* Return the product of "aff1" and "aff2".
3332 * If either of the two is NaN, then the result is NaN.
3334 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3336 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3337 __isl_take isl_aff *aff2)
3339 if (!aff1 || !aff2)
3340 goto error;
3342 if (isl_aff_is_nan(aff1)) {
3343 isl_aff_free(aff2);
3344 return aff1;
3346 if (isl_aff_is_nan(aff2)) {
3347 isl_aff_free(aff1);
3348 return aff2;
3351 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3352 return isl_aff_mul(aff2, aff1);
3354 if (!isl_aff_is_cst(aff2))
3355 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3356 "at least one affine expression should be constant",
3357 goto error);
3359 aff1 = isl_aff_cow(aff1);
3360 if (!aff1 || !aff2)
3361 goto error;
3363 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3364 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3366 isl_aff_free(aff2);
3367 return aff1;
3368 error:
3369 isl_aff_free(aff1);
3370 isl_aff_free(aff2);
3371 return NULL;
3374 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3376 * If either of the two is NaN, then the result is NaN.
3378 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3379 __isl_take isl_aff *aff2)
3381 int is_cst;
3382 int neg;
3384 if (!aff1 || !aff2)
3385 goto error;
3387 if (isl_aff_is_nan(aff1)) {
3388 isl_aff_free(aff2);
3389 return aff1;
3391 if (isl_aff_is_nan(aff2)) {
3392 isl_aff_free(aff1);
3393 return aff2;
3396 is_cst = isl_aff_is_cst(aff2);
3397 if (is_cst < 0)
3398 goto error;
3399 if (!is_cst)
3400 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3401 "second argument should be a constant", goto error);
3403 if (!aff2)
3404 goto error;
3406 neg = isl_int_is_neg(aff2->v->el[1]);
3407 if (neg) {
3408 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3409 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3412 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3413 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3415 if (neg) {
3416 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3417 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3420 isl_aff_free(aff2);
3421 return aff1;
3422 error:
3423 isl_aff_free(aff1);
3424 isl_aff_free(aff2);
3425 return NULL;
3428 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3429 __isl_take isl_pw_aff *pwaff2)
3431 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3434 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3435 __isl_take isl_pw_aff *pwaff2)
3437 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3440 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3441 __isl_take isl_pw_aff *pwaff2)
3443 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3446 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3447 __isl_take isl_pw_aff *pwaff2)
3449 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3452 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3453 __isl_take isl_pw_aff *pwaff2)
3455 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3458 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3459 __isl_take isl_pw_aff *pa2)
3461 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3464 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3466 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3467 __isl_take isl_pw_aff *pa2)
3469 int is_cst;
3471 is_cst = isl_pw_aff_is_cst(pa2);
3472 if (is_cst < 0)
3473 goto error;
3474 if (!is_cst)
3475 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3476 "second argument should be a piecewise constant",
3477 goto error);
3478 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3479 error:
3480 isl_pw_aff_free(pa1);
3481 isl_pw_aff_free(pa2);
3482 return NULL;
3485 /* Compute the quotient of the integer division of "pa1" by "pa2"
3486 * with rounding towards zero.
3487 * "pa2" is assumed to be a piecewise constant.
3489 * In particular, return
3491 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3494 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3495 __isl_take isl_pw_aff *pa2)
3497 int is_cst;
3498 isl_set *cond;
3499 isl_pw_aff *f, *c;
3501 is_cst = isl_pw_aff_is_cst(pa2);
3502 if (is_cst < 0)
3503 goto error;
3504 if (!is_cst)
3505 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3506 "second argument should be a piecewise constant",
3507 goto error);
3509 pa1 = isl_pw_aff_div(pa1, pa2);
3511 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3512 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3513 c = isl_pw_aff_ceil(pa1);
3514 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3515 error:
3516 isl_pw_aff_free(pa1);
3517 isl_pw_aff_free(pa2);
3518 return NULL;
3521 /* Compute the remainder of the integer division of "pa1" by "pa2"
3522 * with rounding towards zero.
3523 * "pa2" is assumed to be a piecewise constant.
3525 * In particular, return
3527 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3530 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3531 __isl_take isl_pw_aff *pa2)
3533 int is_cst;
3534 isl_pw_aff *res;
3536 is_cst = isl_pw_aff_is_cst(pa2);
3537 if (is_cst < 0)
3538 goto error;
3539 if (!is_cst)
3540 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3541 "second argument should be a piecewise constant",
3542 goto error);
3543 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3544 res = isl_pw_aff_mul(pa2, res);
3545 res = isl_pw_aff_sub(pa1, res);
3546 return res;
3547 error:
3548 isl_pw_aff_free(pa1);
3549 isl_pw_aff_free(pa2);
3550 return NULL;
3553 /* Does either of "pa1" or "pa2" involve any NaN2?
3555 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3556 __isl_keep isl_pw_aff *pa2)
3558 isl_bool has_nan;
3560 has_nan = isl_pw_aff_involves_nan(pa1);
3561 if (has_nan < 0 || has_nan)
3562 return has_nan;
3563 return isl_pw_aff_involves_nan(pa2);
3566 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3567 * by a NaN on their shared domain.
3569 * In principle, the result could be refined to only being NaN
3570 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3572 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3573 __isl_take isl_pw_aff *pa2)
3575 isl_local_space *ls;
3576 isl_set *dom;
3577 isl_pw_aff *pa;
3579 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3580 ls = isl_local_space_from_space(isl_set_get_space(dom));
3581 pa = isl_pw_aff_nan_on_domain(ls);
3582 pa = isl_pw_aff_intersect_domain(pa, dom);
3584 return pa;
3587 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3588 __isl_take isl_pw_aff *pwaff2)
3590 isl_set *le;
3591 isl_set *dom;
3593 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3594 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3595 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3596 isl_pw_aff_copy(pwaff2));
3597 dom = isl_set_subtract(dom, isl_set_copy(le));
3598 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3601 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3602 __isl_take isl_pw_aff *pwaff2)
3604 isl_set *ge;
3605 isl_set *dom;
3607 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3608 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3609 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3610 isl_pw_aff_copy(pwaff2));
3611 dom = isl_set_subtract(dom, isl_set_copy(ge));
3612 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3615 /* Return an expression for the minimum (if "max" is not set) or
3616 * the maximum (if "max" is set) of "pa1" and "pa2".
3617 * If either expression involves any NaN, then return a NaN
3618 * on the shared domain as result.
3620 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3621 __isl_take isl_pw_aff *pa2, int max)
3623 isl_bool has_nan;
3625 has_nan = either_involves_nan(pa1, pa2);
3626 if (has_nan < 0)
3627 pa1 = isl_pw_aff_free(pa1);
3628 else if (has_nan)
3629 return replace_by_nan(pa1, pa2);
3631 if (max)
3632 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3633 else
3634 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3637 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3639 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3640 __isl_take isl_pw_aff *pwaff2)
3642 return pw_aff_min_max(pwaff1, pwaff2, 0);
3645 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3647 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3648 __isl_take isl_pw_aff *pwaff2)
3650 return pw_aff_min_max(pwaff1, pwaff2, 1);
3653 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3654 __isl_take isl_pw_aff_list *list,
3655 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3656 __isl_take isl_pw_aff *pwaff2))
3658 int i;
3659 isl_ctx *ctx;
3660 isl_pw_aff *res;
3662 if (!list)
3663 return NULL;
3665 ctx = isl_pw_aff_list_get_ctx(list);
3666 if (list->n < 1)
3667 isl_die(ctx, isl_error_invalid,
3668 "list should contain at least one element", goto error);
3670 res = isl_pw_aff_copy(list->p[0]);
3671 for (i = 1; i < list->n; ++i)
3672 res = fn(res, isl_pw_aff_copy(list->p[i]));
3674 isl_pw_aff_list_free(list);
3675 return res;
3676 error:
3677 isl_pw_aff_list_free(list);
3678 return NULL;
3681 /* Return an isl_pw_aff that maps each element in the intersection of the
3682 * domains of the elements of list to the minimal corresponding affine
3683 * expression.
3685 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3687 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3690 /* Return an isl_pw_aff that maps each element in the intersection of the
3691 * domains of the elements of list to the maximal corresponding affine
3692 * expression.
3694 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3696 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3699 /* Mark the domains of "pwaff" as rational.
3701 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3703 int i;
3705 pwaff = isl_pw_aff_cow(pwaff);
3706 if (!pwaff)
3707 return NULL;
3708 if (pwaff->n == 0)
3709 return pwaff;
3711 for (i = 0; i < pwaff->n; ++i) {
3712 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3713 if (!pwaff->p[i].set)
3714 return isl_pw_aff_free(pwaff);
3717 return pwaff;
3720 /* Mark the domains of the elements of "list" as rational.
3722 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3723 __isl_take isl_pw_aff_list *list)
3725 int i, n;
3727 if (!list)
3728 return NULL;
3729 if (list->n == 0)
3730 return list;
3732 n = list->n;
3733 for (i = 0; i < n; ++i) {
3734 isl_pw_aff *pa;
3736 pa = isl_pw_aff_list_get_pw_aff(list, i);
3737 pa = isl_pw_aff_set_rational(pa);
3738 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3741 return list;
3744 /* Do the parameters of "aff" match those of "space"?
3746 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3747 __isl_keep isl_space *space)
3749 isl_space *aff_space;
3750 isl_bool match;
3752 if (!aff || !space)
3753 return isl_bool_error;
3755 aff_space = isl_aff_get_domain_space(aff);
3757 match = isl_space_has_equal_params(space, aff_space);
3759 isl_space_free(aff_space);
3760 return match;
3763 /* Check that the domain space of "aff" matches "space".
3765 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3766 __isl_keep isl_space *space)
3768 isl_space *aff_space;
3769 isl_bool match;
3771 if (!aff || !space)
3772 return isl_stat_error;
3774 aff_space = isl_aff_get_domain_space(aff);
3776 match = isl_space_has_equal_params(space, aff_space);
3777 if (match < 0)
3778 goto error;
3779 if (!match)
3780 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3781 "parameters don't match", goto error);
3782 match = isl_space_tuple_is_equal(space, isl_dim_in,
3783 aff_space, isl_dim_set);
3784 if (match < 0)
3785 goto error;
3786 if (!match)
3787 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3788 "domains don't match", goto error);
3789 isl_space_free(aff_space);
3790 return isl_stat_ok;
3791 error:
3792 isl_space_free(aff_space);
3793 return isl_stat_error;
3796 #undef BASE
3797 #define BASE aff
3798 #undef DOMBASE
3799 #define DOMBASE set
3801 #include <isl_multi_no_explicit_domain.c>
3802 #include <isl_multi_templ.c>
3803 #include <isl_multi_apply_set.c>
3804 #include <isl_multi_cmp.c>
3805 #include <isl_multi_dims.c>
3806 #include <isl_multi_floor.c>
3807 #include <isl_multi_from_base_templ.c>
3808 #include <isl_multi_gist.c>
3809 #include <isl_multi_identity_templ.c>
3810 #include <isl_multi_move_dims_templ.c>
3811 #include <isl_multi_product_templ.c>
3812 #include <isl_multi_splice_templ.c>
3813 #include <isl_multi_zero_templ.c>
3815 /* Construct an isl_multi_aff living in "space" that corresponds
3816 * to the affine transformation matrix "mat".
3818 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3819 __isl_take isl_space *space, __isl_take isl_mat *mat)
3821 isl_ctx *ctx;
3822 isl_local_space *ls = NULL;
3823 isl_multi_aff *ma = NULL;
3824 int n_row, n_col, n_out, total;
3825 int i;
3827 if (!space || !mat)
3828 goto error;
3830 ctx = isl_mat_get_ctx(mat);
3832 n_row = isl_mat_rows(mat);
3833 n_col = isl_mat_cols(mat);
3834 if (n_row < 1)
3835 isl_die(ctx, isl_error_invalid,
3836 "insufficient number of rows", goto error);
3837 if (n_col < 1)
3838 isl_die(ctx, isl_error_invalid,
3839 "insufficient number of columns", goto error);
3840 n_out = isl_space_dim(space, isl_dim_out);
3841 total = isl_space_dim(space, isl_dim_all);
3842 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3843 isl_die(ctx, isl_error_invalid,
3844 "dimension mismatch", goto error);
3846 ma = isl_multi_aff_zero(isl_space_copy(space));
3847 ls = isl_local_space_from_space(isl_space_domain(space));
3849 for (i = 0; i < n_row - 1; ++i) {
3850 isl_vec *v;
3851 isl_aff *aff;
3853 v = isl_vec_alloc(ctx, 1 + n_col);
3854 if (!v)
3855 goto error;
3856 isl_int_set(v->el[0], mat->row[0][0]);
3857 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3858 v = isl_vec_normalize(v);
3859 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3860 ma = isl_multi_aff_set_aff(ma, i, aff);
3863 isl_local_space_free(ls);
3864 isl_mat_free(mat);
3865 return ma;
3866 error:
3867 isl_local_space_free(ls);
3868 isl_mat_free(mat);
3869 isl_multi_aff_free(ma);
3870 return NULL;
3873 /* Remove any internal structure of the domain of "ma".
3874 * If there is any such internal structure in the input,
3875 * then the name of the corresponding space is also removed.
3877 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3878 __isl_take isl_multi_aff *ma)
3880 isl_space *space;
3882 if (!ma)
3883 return NULL;
3885 if (!ma->space->nested[0])
3886 return ma;
3888 space = isl_multi_aff_get_space(ma);
3889 space = isl_space_flatten_domain(space);
3890 ma = isl_multi_aff_reset_space(ma, space);
3892 return ma;
3895 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3896 * of the space to its domain.
3898 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3900 int i, n_in;
3901 isl_local_space *ls;
3902 isl_multi_aff *ma;
3904 if (!space)
3905 return NULL;
3906 if (!isl_space_is_map(space))
3907 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3908 "not a map space", goto error);
3910 n_in = isl_space_dim(space, isl_dim_in);
3911 space = isl_space_domain_map(space);
3913 ma = isl_multi_aff_alloc(isl_space_copy(space));
3914 if (n_in == 0) {
3915 isl_space_free(space);
3916 return ma;
3919 space = isl_space_domain(space);
3920 ls = isl_local_space_from_space(space);
3921 for (i = 0; i < n_in; ++i) {
3922 isl_aff *aff;
3924 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3925 isl_dim_set, i);
3926 ma = isl_multi_aff_set_aff(ma, i, aff);
3928 isl_local_space_free(ls);
3929 return ma;
3930 error:
3931 isl_space_free(space);
3932 return NULL;
3935 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3936 * of the space to its range.
3938 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3940 int i, n_in, n_out;
3941 isl_local_space *ls;
3942 isl_multi_aff *ma;
3944 if (!space)
3945 return NULL;
3946 if (!isl_space_is_map(space))
3947 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3948 "not a map space", goto error);
3950 n_in = isl_space_dim(space, isl_dim_in);
3951 n_out = isl_space_dim(space, isl_dim_out);
3952 space = isl_space_range_map(space);
3954 ma = isl_multi_aff_alloc(isl_space_copy(space));
3955 if (n_out == 0) {
3956 isl_space_free(space);
3957 return ma;
3960 space = isl_space_domain(space);
3961 ls = isl_local_space_from_space(space);
3962 for (i = 0; i < n_out; ++i) {
3963 isl_aff *aff;
3965 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3966 isl_dim_set, n_in + i);
3967 ma = isl_multi_aff_set_aff(ma, i, aff);
3969 isl_local_space_free(ls);
3970 return ma;
3971 error:
3972 isl_space_free(space);
3973 return NULL;
3976 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3977 * of the space to its range.
3979 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3980 __isl_take isl_space *space)
3982 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3985 /* Given the space of a set and a range of set dimensions,
3986 * construct an isl_multi_aff that projects out those dimensions.
3988 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3989 __isl_take isl_space *space, enum isl_dim_type type,
3990 unsigned first, unsigned n)
3992 int i, dim;
3993 isl_local_space *ls;
3994 isl_multi_aff *ma;
3996 if (!space)
3997 return NULL;
3998 if (!isl_space_is_set(space))
3999 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4000 "expecting set space", goto error);
4001 if (type != isl_dim_set)
4002 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4003 "only set dimensions can be projected out", goto error);
4005 dim = isl_space_dim(space, isl_dim_set);
4006 if (first + n > dim)
4007 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4008 "range out of bounds", goto error);
4010 space = isl_space_from_domain(space);
4011 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4013 if (dim == n)
4014 return isl_multi_aff_alloc(space);
4016 ma = isl_multi_aff_alloc(isl_space_copy(space));
4017 space = isl_space_domain(space);
4018 ls = isl_local_space_from_space(space);
4020 for (i = 0; i < first; ++i) {
4021 isl_aff *aff;
4023 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4024 isl_dim_set, i);
4025 ma = isl_multi_aff_set_aff(ma, i, aff);
4028 for (i = 0; i < dim - (first + n); ++i) {
4029 isl_aff *aff;
4031 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4032 isl_dim_set, first + n + i);
4033 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4036 isl_local_space_free(ls);
4037 return ma;
4038 error:
4039 isl_space_free(space);
4040 return NULL;
4043 /* Given the space of a set and a range of set dimensions,
4044 * construct an isl_pw_multi_aff that projects out those dimensions.
4046 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4047 __isl_take isl_space *space, enum isl_dim_type type,
4048 unsigned first, unsigned n)
4050 isl_multi_aff *ma;
4052 ma = isl_multi_aff_project_out_map(space, type, first, n);
4053 return isl_pw_multi_aff_from_multi_aff(ma);
4056 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4057 * domain.
4059 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4060 __isl_take isl_multi_aff *ma)
4062 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4063 return isl_pw_multi_aff_alloc(dom, ma);
4066 /* Create a piecewise multi-affine expression in the given space that maps each
4067 * input dimension to the corresponding output dimension.
4069 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4070 __isl_take isl_space *space)
4072 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4075 /* Exploit the equalities in "eq" to simplify the affine expressions.
4077 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4078 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4080 int i;
4082 maff = isl_multi_aff_cow(maff);
4083 if (!maff || !eq)
4084 goto error;
4086 for (i = 0; i < maff->n; ++i) {
4087 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4088 isl_basic_set_copy(eq));
4089 if (!maff->u.p[i])
4090 goto error;
4093 isl_basic_set_free(eq);
4094 return maff;
4095 error:
4096 isl_basic_set_free(eq);
4097 isl_multi_aff_free(maff);
4098 return NULL;
4101 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4102 isl_int f)
4104 int i;
4106 maff = isl_multi_aff_cow(maff);
4107 if (!maff)
4108 return NULL;
4110 for (i = 0; i < maff->n; ++i) {
4111 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4112 if (!maff->u.p[i])
4113 return isl_multi_aff_free(maff);
4116 return maff;
4119 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4120 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4122 maff1 = isl_multi_aff_add(maff1, maff2);
4123 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4124 return maff1;
4127 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4129 if (!maff)
4130 return -1;
4132 return 0;
4135 /* Return the set of domain elements where "ma1" is lexicographically
4136 * smaller than or equal to "ma2".
4138 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4139 __isl_take isl_multi_aff *ma2)
4141 return isl_multi_aff_lex_ge_set(ma2, ma1);
4144 /* Return the set of domain elements where "ma1" is lexicographically
4145 * smaller than "ma2".
4147 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4148 __isl_take isl_multi_aff *ma2)
4150 return isl_multi_aff_lex_gt_set(ma2, ma1);
4153 /* Return the set of domain elements where "ma1" and "ma2"
4154 * satisfy "order".
4156 static __isl_give isl_set *isl_multi_aff_order_set(
4157 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4158 __isl_give isl_map *order(__isl_take isl_space *set_space))
4160 isl_space *space;
4161 isl_map *map1, *map2;
4162 isl_map *map, *ge;
4164 map1 = isl_map_from_multi_aff_internal(ma1);
4165 map2 = isl_map_from_multi_aff_internal(ma2);
4166 map = isl_map_range_product(map1, map2);
4167 space = isl_space_range(isl_map_get_space(map));
4168 space = isl_space_domain(isl_space_unwrap(space));
4169 ge = order(space);
4170 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4172 return isl_map_domain(map);
4175 /* Return the set of domain elements where "ma1" is lexicographically
4176 * greater than or equal to "ma2".
4178 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4179 __isl_take isl_multi_aff *ma2)
4181 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4184 /* Return the set of domain elements where "ma1" is lexicographically
4185 * greater than "ma2".
4187 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4188 __isl_take isl_multi_aff *ma2)
4190 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4193 #undef PW
4194 #define PW isl_pw_multi_aff
4195 #undef EL
4196 #define EL isl_multi_aff
4197 #undef EL_IS_ZERO
4198 #define EL_IS_ZERO is_empty
4199 #undef ZERO
4200 #define ZERO empty
4201 #undef IS_ZERO
4202 #define IS_ZERO is_empty
4203 #undef FIELD
4204 #define FIELD maff
4205 #undef DEFAULT_IS_ZERO
4206 #define DEFAULT_IS_ZERO 0
4208 #define NO_SUB
4209 #define NO_OPT
4210 #define NO_INSERT_DIMS
4211 #define NO_LIFT
4212 #define NO_MORPH
4214 #include <isl_pw_templ.c>
4215 #include <isl_pw_union_opt.c>
4217 #undef NO_SUB
4219 #undef BASE
4220 #define BASE pw_multi_aff
4222 #include <isl_union_multi.c>
4223 #include <isl_union_neg.c>
4225 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4226 __isl_take isl_pw_multi_aff *pma1,
4227 __isl_take isl_pw_multi_aff *pma2)
4229 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4230 &isl_multi_aff_lex_ge_set);
4233 /* Given two piecewise multi affine expressions, return a piecewise
4234 * multi-affine expression defined on the union of the definition domains
4235 * of the inputs that is equal to the lexicographic maximum of the two
4236 * inputs on each cell. If only one of the two inputs is defined on
4237 * a given cell, then it is considered to be the maximum.
4239 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4240 __isl_take isl_pw_multi_aff *pma1,
4241 __isl_take isl_pw_multi_aff *pma2)
4243 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4244 &pw_multi_aff_union_lexmax);
4247 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4248 __isl_take isl_pw_multi_aff *pma1,
4249 __isl_take isl_pw_multi_aff *pma2)
4251 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4252 &isl_multi_aff_lex_le_set);
4255 /* Given two piecewise multi affine expressions, return a piecewise
4256 * multi-affine expression defined on the union of the definition domains
4257 * of the inputs that is equal to the lexicographic minimum of the two
4258 * inputs on each cell. If only one of the two inputs is defined on
4259 * a given cell, then it is considered to be the minimum.
4261 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4262 __isl_take isl_pw_multi_aff *pma1,
4263 __isl_take isl_pw_multi_aff *pma2)
4265 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4266 &pw_multi_aff_union_lexmin);
4269 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4270 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4272 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4273 &isl_multi_aff_add);
4276 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4277 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4279 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4280 &pw_multi_aff_add);
4283 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4284 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4286 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4287 &isl_multi_aff_sub);
4290 /* Subtract "pma2" from "pma1" and return the result.
4292 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4293 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4295 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4296 &pw_multi_aff_sub);
4299 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4300 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4302 return isl_pw_multi_aff_union_add_(pma1, pma2);
4305 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4306 * with the actual sum on the shared domain and
4307 * the defined expression on the symmetric difference of the domains.
4309 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4310 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4312 return isl_union_pw_aff_union_add_(upa1, upa2);
4315 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4316 * with the actual sum on the shared domain and
4317 * the defined expression on the symmetric difference of the domains.
4319 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4320 __isl_take isl_union_pw_multi_aff *upma1,
4321 __isl_take isl_union_pw_multi_aff *upma2)
4323 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4326 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4327 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4329 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4330 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4332 int i, j, n;
4333 isl_space *space;
4334 isl_pw_multi_aff *res;
4336 if (!pma1 || !pma2)
4337 goto error;
4339 n = pma1->n * pma2->n;
4340 space = isl_space_product(isl_space_copy(pma1->dim),
4341 isl_space_copy(pma2->dim));
4342 res = isl_pw_multi_aff_alloc_size(space, n);
4344 for (i = 0; i < pma1->n; ++i) {
4345 for (j = 0; j < pma2->n; ++j) {
4346 isl_set *domain;
4347 isl_multi_aff *ma;
4349 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4350 isl_set_copy(pma2->p[j].set));
4351 ma = isl_multi_aff_product(
4352 isl_multi_aff_copy(pma1->p[i].maff),
4353 isl_multi_aff_copy(pma2->p[j].maff));
4354 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4358 isl_pw_multi_aff_free(pma1);
4359 isl_pw_multi_aff_free(pma2);
4360 return res;
4361 error:
4362 isl_pw_multi_aff_free(pma1);
4363 isl_pw_multi_aff_free(pma2);
4364 return NULL;
4367 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4368 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4370 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4371 &pw_multi_aff_product);
4374 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4375 * denominator "denom".
4376 * "denom" is allowed to be negative, in which case the actual denominator
4377 * is -denom and the expressions are added instead.
4379 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4380 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4382 int i, first;
4383 int sign;
4384 isl_int d;
4386 first = isl_seq_first_non_zero(c, n);
4387 if (first == -1)
4388 return aff;
4390 sign = isl_int_sgn(denom);
4391 isl_int_init(d);
4392 isl_int_abs(d, denom);
4393 for (i = first; i < n; ++i) {
4394 isl_aff *aff_i;
4396 if (isl_int_is_zero(c[i]))
4397 continue;
4398 aff_i = isl_multi_aff_get_aff(ma, i);
4399 aff_i = isl_aff_scale(aff_i, c[i]);
4400 aff_i = isl_aff_scale_down(aff_i, d);
4401 if (sign >= 0)
4402 aff = isl_aff_sub(aff, aff_i);
4403 else
4404 aff = isl_aff_add(aff, aff_i);
4406 isl_int_clear(d);
4408 return aff;
4411 /* Extract an affine expression that expresses the output dimension "pos"
4412 * of "bmap" in terms of the parameters and input dimensions from
4413 * equality "eq".
4414 * Note that this expression may involve integer divisions defined
4415 * in terms of parameters and input dimensions.
4416 * The equality may also involve references to earlier (but not later)
4417 * output dimensions. These are replaced by the corresponding elements
4418 * in "ma".
4420 * If the equality is of the form
4422 * f(i) + h(j) + a x + g(i) = 0,
4424 * with f(i) a linear combinations of the parameters and input dimensions,
4425 * g(i) a linear combination of integer divisions defined in terms of the same
4426 * and h(j) a linear combinations of earlier output dimensions,
4427 * then the affine expression is
4429 * (-f(i) - g(i))/a - h(j)/a
4431 * If the equality is of the form
4433 * f(i) + h(j) - a x + g(i) = 0,
4435 * then the affine expression is
4437 * (f(i) + g(i))/a - h(j)/(-a)
4440 * If "div" refers to an integer division (i.e., it is smaller than
4441 * the number of integer divisions), then the equality constraint
4442 * does involve an integer division (the one at position "div") that
4443 * is defined in terms of output dimensions. However, this integer
4444 * division can be eliminated by exploiting a pair of constraints
4445 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4446 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4447 * -l + x >= 0.
4448 * In particular, let
4450 * x = e(i) + m floor(...)
4452 * with e(i) the expression derived above and floor(...) the integer
4453 * division involving output dimensions.
4454 * From
4456 * l <= x <= l + n,
4458 * we have
4460 * 0 <= x - l <= n
4462 * This means
4464 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4465 * = (e(i) - l) mod m
4467 * Therefore,
4469 * x - l = (e(i) - l) mod m
4471 * or
4473 * x = ((e(i) - l) mod m) + l
4475 * The variable "shift" below contains the expression -l, which may
4476 * also involve a linear combination of earlier output dimensions.
4478 static __isl_give isl_aff *extract_aff_from_equality(
4479 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4480 __isl_keep isl_multi_aff *ma)
4482 unsigned o_out;
4483 unsigned n_div, n_out;
4484 isl_ctx *ctx;
4485 isl_local_space *ls;
4486 isl_aff *aff, *shift;
4487 isl_val *mod;
4489 ctx = isl_basic_map_get_ctx(bmap);
4490 ls = isl_basic_map_get_local_space(bmap);
4491 ls = isl_local_space_domain(ls);
4492 aff = isl_aff_alloc(isl_local_space_copy(ls));
4493 if (!aff)
4494 goto error;
4495 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4496 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4497 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4498 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4499 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4500 isl_seq_cpy(aff->v->el + 1 + o_out,
4501 bmap->eq[eq] + o_out + n_out, n_div);
4502 } else {
4503 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4504 isl_seq_neg(aff->v->el + 1 + o_out,
4505 bmap->eq[eq] + o_out + n_out, n_div);
4507 if (div < n_div)
4508 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4509 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4510 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4511 bmap->eq[eq][o_out + pos]);
4512 if (div < n_div) {
4513 shift = isl_aff_alloc(isl_local_space_copy(ls));
4514 if (!shift)
4515 goto error;
4516 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4517 isl_seq_cpy(shift->v->el + 1 + o_out,
4518 bmap->ineq[ineq] + o_out + n_out, n_div);
4519 isl_int_set_si(shift->v->el[0], 1);
4520 shift = subtract_initial(shift, ma, pos,
4521 bmap->ineq[ineq] + o_out, ctx->negone);
4522 aff = isl_aff_add(aff, isl_aff_copy(shift));
4523 mod = isl_val_int_from_isl_int(ctx,
4524 bmap->eq[eq][o_out + n_out + div]);
4525 mod = isl_val_abs(mod);
4526 aff = isl_aff_mod_val(aff, mod);
4527 aff = isl_aff_sub(aff, shift);
4530 isl_local_space_free(ls);
4531 return aff;
4532 error:
4533 isl_local_space_free(ls);
4534 isl_aff_free(aff);
4535 return NULL;
4538 /* Given a basic map with output dimensions defined
4539 * in terms of the parameters input dimensions and earlier
4540 * output dimensions using an equality (and possibly a pair on inequalities),
4541 * extract an isl_aff that expresses output dimension "pos" in terms
4542 * of the parameters and input dimensions.
4543 * Note that this expression may involve integer divisions defined
4544 * in terms of parameters and input dimensions.
4545 * "ma" contains the expressions corresponding to earlier output dimensions.
4547 * This function shares some similarities with
4548 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4550 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4551 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4553 int eq, div, ineq;
4554 isl_aff *aff;
4556 if (!bmap)
4557 return NULL;
4558 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4559 if (eq >= bmap->n_eq)
4560 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4561 "unable to find suitable equality", return NULL);
4562 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4564 aff = isl_aff_remove_unused_divs(aff);
4565 return aff;
4568 /* Given a basic map where each output dimension is defined
4569 * in terms of the parameters and input dimensions using an equality,
4570 * extract an isl_multi_aff that expresses the output dimensions in terms
4571 * of the parameters and input dimensions.
4573 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4574 __isl_take isl_basic_map *bmap)
4576 int i;
4577 unsigned n_out;
4578 isl_multi_aff *ma;
4580 if (!bmap)
4581 return NULL;
4583 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4584 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4586 for (i = 0; i < n_out; ++i) {
4587 isl_aff *aff;
4589 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4590 ma = isl_multi_aff_set_aff(ma, i, aff);
4593 isl_basic_map_free(bmap);
4595 return ma;
4598 /* Given a basic set where each set dimension is defined
4599 * in terms of the parameters using an equality,
4600 * extract an isl_multi_aff that expresses the set dimensions in terms
4601 * of the parameters.
4603 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4604 __isl_take isl_basic_set *bset)
4606 return extract_isl_multi_aff_from_basic_map(bset);
4609 /* Create an isl_pw_multi_aff that is equivalent to
4610 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4611 * The given basic map is such that each output dimension is defined
4612 * in terms of the parameters and input dimensions using an equality.
4614 * Since some applications expect the result of isl_pw_multi_aff_from_map
4615 * to only contain integer affine expressions, we compute the floor
4616 * of the expression before returning.
4618 * Remove all constraints involving local variables without
4619 * an explicit representation (resulting in the removal of those
4620 * local variables) prior to the actual extraction to ensure
4621 * that the local spaces in which the resulting affine expressions
4622 * are created do not contain any unknown local variables.
4623 * Removing such constraints is safe because constraints involving
4624 * unknown local variables are not used to determine whether
4625 * a basic map is obviously single-valued.
4627 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4628 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4630 isl_multi_aff *ma;
4632 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4633 ma = extract_isl_multi_aff_from_basic_map(bmap);
4634 ma = isl_multi_aff_floor(ma);
4635 return isl_pw_multi_aff_alloc(domain, ma);
4638 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4639 * This obviously only works if the input "map" is single-valued.
4640 * If so, we compute the lexicographic minimum of the image in the form
4641 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4642 * to its lexicographic minimum.
4643 * If the input is not single-valued, we produce an error.
4645 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4646 __isl_take isl_map *map)
4648 int i;
4649 int sv;
4650 isl_pw_multi_aff *pma;
4652 sv = isl_map_is_single_valued(map);
4653 if (sv < 0)
4654 goto error;
4655 if (!sv)
4656 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4657 "map is not single-valued", goto error);
4658 map = isl_map_make_disjoint(map);
4659 if (!map)
4660 return NULL;
4662 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4664 for (i = 0; i < map->n; ++i) {
4665 isl_pw_multi_aff *pma_i;
4666 isl_basic_map *bmap;
4667 bmap = isl_basic_map_copy(map->p[i]);
4668 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4669 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4672 isl_map_free(map);
4673 return pma;
4674 error:
4675 isl_map_free(map);
4676 return NULL;
4679 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4680 * taking into account that the output dimension at position "d"
4681 * can be represented as
4683 * x = floor((e(...) + c1) / m)
4685 * given that constraint "i" is of the form
4687 * e(...) + c1 - m x >= 0
4690 * Let "map" be of the form
4692 * A -> B
4694 * We construct a mapping
4696 * A -> [A -> x = floor(...)]
4698 * apply that to the map, obtaining
4700 * [A -> x = floor(...)] -> B
4702 * and equate dimension "d" to x.
4703 * We then compute a isl_pw_multi_aff representation of the resulting map
4704 * and plug in the mapping above.
4706 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4707 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4709 isl_ctx *ctx;
4710 isl_space *space;
4711 isl_local_space *ls;
4712 isl_multi_aff *ma;
4713 isl_aff *aff;
4714 isl_vec *v;
4715 isl_map *insert;
4716 int offset;
4717 int n;
4718 int n_in;
4719 isl_pw_multi_aff *pma;
4720 isl_bool is_set;
4722 is_set = isl_map_is_set(map);
4723 if (is_set < 0)
4724 goto error;
4726 offset = isl_basic_map_offset(hull, isl_dim_out);
4727 ctx = isl_map_get_ctx(map);
4728 space = isl_space_domain(isl_map_get_space(map));
4729 n_in = isl_space_dim(space, isl_dim_set);
4730 n = isl_space_dim(space, isl_dim_all);
4732 v = isl_vec_alloc(ctx, 1 + 1 + n);
4733 if (v) {
4734 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4735 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4737 isl_basic_map_free(hull);
4739 ls = isl_local_space_from_space(isl_space_copy(space));
4740 aff = isl_aff_alloc_vec(ls, v);
4741 aff = isl_aff_floor(aff);
4742 if (is_set) {
4743 isl_space_free(space);
4744 ma = isl_multi_aff_from_aff(aff);
4745 } else {
4746 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4747 ma = isl_multi_aff_range_product(ma,
4748 isl_multi_aff_from_aff(aff));
4751 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4752 map = isl_map_apply_domain(map, insert);
4753 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4754 pma = isl_pw_multi_aff_from_map(map);
4755 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4757 return pma;
4758 error:
4759 isl_map_free(map);
4760 isl_basic_map_free(hull);
4761 return NULL;
4764 /* Is constraint "c" of the form
4766 * e(...) + c1 - m x >= 0
4768 * or
4770 * -e(...) + c2 + m x >= 0
4772 * where m > 1 and e only depends on parameters and input dimemnsions?
4774 * "offset" is the offset of the output dimensions
4775 * "pos" is the position of output dimension x.
4777 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4779 if (isl_int_is_zero(c[offset + d]))
4780 return 0;
4781 if (isl_int_is_one(c[offset + d]))
4782 return 0;
4783 if (isl_int_is_negone(c[offset + d]))
4784 return 0;
4785 if (isl_seq_first_non_zero(c + offset, d) != -1)
4786 return 0;
4787 if (isl_seq_first_non_zero(c + offset + d + 1,
4788 total - (offset + d + 1)) != -1)
4789 return 0;
4790 return 1;
4793 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4795 * As a special case, we first check if there is any pair of constraints,
4796 * shared by all the basic maps in "map" that force a given dimension
4797 * to be equal to the floor of some affine combination of the input dimensions.
4799 * In particular, if we can find two constraints
4801 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4803 * and
4805 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4807 * where m > 1 and e only depends on parameters and input dimemnsions,
4808 * and such that
4810 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4812 * then we know that we can take
4814 * x = floor((e(...) + c1) / m)
4816 * without having to perform any computation.
4818 * Note that we know that
4820 * c1 + c2 >= 1
4822 * If c1 + c2 were 0, then we would have detected an equality during
4823 * simplification. If c1 + c2 were negative, then we would have detected
4824 * a contradiction.
4826 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4827 __isl_take isl_map *map)
4829 int d, dim;
4830 int i, j, n;
4831 int offset, total;
4832 isl_int sum;
4833 isl_basic_map *hull;
4835 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4836 if (!hull)
4837 goto error;
4839 isl_int_init(sum);
4840 dim = isl_map_dim(map, isl_dim_out);
4841 offset = isl_basic_map_offset(hull, isl_dim_out);
4842 total = 1 + isl_basic_map_total_dim(hull);
4843 n = hull->n_ineq;
4844 for (d = 0; d < dim; ++d) {
4845 for (i = 0; i < n; ++i) {
4846 if (!is_potential_div_constraint(hull->ineq[i],
4847 offset, d, total))
4848 continue;
4849 for (j = i + 1; j < n; ++j) {
4850 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4851 hull->ineq[j] + 1, total - 1))
4852 continue;
4853 isl_int_add(sum, hull->ineq[i][0],
4854 hull->ineq[j][0]);
4855 if (isl_int_abs_lt(sum,
4856 hull->ineq[i][offset + d]))
4857 break;
4860 if (j >= n)
4861 continue;
4862 isl_int_clear(sum);
4863 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4864 j = i;
4865 return pw_multi_aff_from_map_div(map, hull, d, j);
4868 isl_int_clear(sum);
4869 isl_basic_map_free(hull);
4870 return pw_multi_aff_from_map_base(map);
4871 error:
4872 isl_map_free(map);
4873 isl_basic_map_free(hull);
4874 return NULL;
4877 /* Given an affine expression
4879 * [A -> B] -> f(A,B)
4881 * construct an isl_multi_aff
4883 * [A -> B] -> B'
4885 * such that dimension "d" in B' is set to "aff" and the remaining
4886 * dimensions are set equal to the corresponding dimensions in B.
4887 * "n_in" is the dimension of the space A.
4888 * "n_out" is the dimension of the space B.
4890 * If "is_set" is set, then the affine expression is of the form
4892 * [B] -> f(B)
4894 * and we construct an isl_multi_aff
4896 * B -> B'
4898 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4899 unsigned n_in, unsigned n_out, int is_set)
4901 int i;
4902 isl_multi_aff *ma;
4903 isl_space *space, *space2;
4904 isl_local_space *ls;
4906 space = isl_aff_get_domain_space(aff);
4907 ls = isl_local_space_from_space(isl_space_copy(space));
4908 space2 = isl_space_copy(space);
4909 if (!is_set)
4910 space2 = isl_space_range(isl_space_unwrap(space2));
4911 space = isl_space_map_from_domain_and_range(space, space2);
4912 ma = isl_multi_aff_alloc(space);
4913 ma = isl_multi_aff_set_aff(ma, d, aff);
4915 for (i = 0; i < n_out; ++i) {
4916 if (i == d)
4917 continue;
4918 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4919 isl_dim_set, n_in + i);
4920 ma = isl_multi_aff_set_aff(ma, i, aff);
4923 isl_local_space_free(ls);
4925 return ma;
4928 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4929 * taking into account that the dimension at position "d" can be written as
4931 * x = m a + f(..) (1)
4933 * where m is equal to "gcd".
4934 * "i" is the index of the equality in "hull" that defines f(..).
4935 * In particular, the equality is of the form
4937 * f(..) - x + m g(existentials) = 0
4939 * or
4941 * -f(..) + x + m g(existentials) = 0
4943 * We basically plug (1) into "map", resulting in a map with "a"
4944 * in the range instead of "x". The corresponding isl_pw_multi_aff
4945 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4947 * Specifically, given the input map
4949 * A -> B
4951 * We first wrap it into a set
4953 * [A -> B]
4955 * and define (1) on top of the corresponding space, resulting in "aff".
4956 * We use this to create an isl_multi_aff that maps the output position "d"
4957 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4958 * We plug this into the wrapped map, unwrap the result and compute the
4959 * corresponding isl_pw_multi_aff.
4960 * The result is an expression
4962 * A -> T(A)
4964 * We adjust that to
4966 * A -> [A -> T(A)]
4968 * so that we can plug that into "aff", after extending the latter to
4969 * a mapping
4971 * [A -> B] -> B'
4974 * If "map" is actually a set, then there is no "A" space, meaning
4975 * that we do not need to perform any wrapping, and that the result
4976 * of the recursive call is of the form
4978 * [T]
4980 * which is plugged into a mapping of the form
4982 * B -> B'
4984 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4985 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4986 isl_int gcd)
4988 isl_set *set;
4989 isl_space *space;
4990 isl_local_space *ls;
4991 isl_aff *aff;
4992 isl_multi_aff *ma;
4993 isl_pw_multi_aff *pma, *id;
4994 unsigned n_in;
4995 unsigned o_out;
4996 unsigned n_out;
4997 isl_bool is_set;
4999 is_set = isl_map_is_set(map);
5000 if (is_set < 0)
5001 goto error;
5003 n_in = isl_basic_map_dim(hull, isl_dim_in);
5004 n_out = isl_basic_map_dim(hull, isl_dim_out);
5005 o_out = isl_basic_map_offset(hull, isl_dim_out);
5007 if (is_set)
5008 set = map;
5009 else
5010 set = isl_map_wrap(map);
5011 space = isl_space_map_from_set(isl_set_get_space(set));
5012 ma = isl_multi_aff_identity(space);
5013 ls = isl_local_space_from_space(isl_set_get_space(set));
5014 aff = isl_aff_alloc(ls);
5015 if (aff) {
5016 isl_int_set_si(aff->v->el[0], 1);
5017 if (isl_int_is_one(hull->eq[i][o_out + d]))
5018 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5019 aff->v->size - 1);
5020 else
5021 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5022 aff->v->size - 1);
5023 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5025 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5026 set = isl_set_preimage_multi_aff(set, ma);
5028 ma = range_map(aff, d, n_in, n_out, is_set);
5030 if (is_set)
5031 map = set;
5032 else
5033 map = isl_set_unwrap(set);
5034 pma = isl_pw_multi_aff_from_map(map);
5036 if (!is_set) {
5037 space = isl_pw_multi_aff_get_domain_space(pma);
5038 space = isl_space_map_from_set(space);
5039 id = isl_pw_multi_aff_identity(space);
5040 pma = isl_pw_multi_aff_range_product(id, pma);
5042 id = isl_pw_multi_aff_from_multi_aff(ma);
5043 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5045 isl_basic_map_free(hull);
5046 return pma;
5047 error:
5048 isl_map_free(map);
5049 isl_basic_map_free(hull);
5050 return NULL;
5053 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5054 * "hull" contains the equalities valid for "map".
5056 * Check if any of the output dimensions is "strided".
5057 * That is, we check if it can be written as
5059 * x = m a + f(..)
5061 * with m greater than 1, a some combination of existentially quantified
5062 * variables and f an expression in the parameters and input dimensions.
5063 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5065 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5066 * special case.
5068 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5069 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5071 int i, j;
5072 unsigned n_out;
5073 unsigned o_out;
5074 unsigned n_div;
5075 unsigned o_div;
5076 isl_int gcd;
5078 n_div = isl_basic_map_dim(hull, isl_dim_div);
5079 o_div = isl_basic_map_offset(hull, isl_dim_div);
5081 if (n_div == 0) {
5082 isl_basic_map_free(hull);
5083 return pw_multi_aff_from_map_check_div(map);
5086 isl_int_init(gcd);
5088 n_out = isl_basic_map_dim(hull, isl_dim_out);
5089 o_out = isl_basic_map_offset(hull, isl_dim_out);
5091 for (i = 0; i < n_out; ++i) {
5092 for (j = 0; j < hull->n_eq; ++j) {
5093 isl_int *eq = hull->eq[j];
5094 isl_pw_multi_aff *res;
5096 if (!isl_int_is_one(eq[o_out + i]) &&
5097 !isl_int_is_negone(eq[o_out + i]))
5098 continue;
5099 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5100 continue;
5101 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5102 n_out - (i + 1)) != -1)
5103 continue;
5104 isl_seq_gcd(eq + o_div, n_div, &gcd);
5105 if (isl_int_is_zero(gcd))
5106 continue;
5107 if (isl_int_is_one(gcd))
5108 continue;
5110 res = pw_multi_aff_from_map_stride(map, hull,
5111 i, j, gcd);
5112 isl_int_clear(gcd);
5113 return res;
5117 isl_int_clear(gcd);
5118 isl_basic_map_free(hull);
5119 return pw_multi_aff_from_map_check_div(map);
5122 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5124 * As a special case, we first check if all output dimensions are uniquely
5125 * defined in terms of the parameters and input dimensions over the entire
5126 * domain. If so, we extract the desired isl_pw_multi_aff directly
5127 * from the affine hull of "map" and its domain.
5129 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5130 * special cases.
5132 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5134 isl_bool sv;
5135 isl_basic_map *hull;
5137 if (!map)
5138 return NULL;
5140 if (isl_map_n_basic_map(map) == 1) {
5141 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5142 hull = isl_basic_map_plain_affine_hull(hull);
5143 sv = isl_basic_map_plain_is_single_valued(hull);
5144 if (sv >= 0 && sv)
5145 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5146 hull);
5147 isl_basic_map_free(hull);
5149 map = isl_map_detect_equalities(map);
5150 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5151 sv = isl_basic_map_plain_is_single_valued(hull);
5152 if (sv >= 0 && sv)
5153 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5154 if (sv >= 0)
5155 return pw_multi_aff_from_map_check_strides(map, hull);
5156 isl_basic_map_free(hull);
5157 isl_map_free(map);
5158 return NULL;
5161 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5163 return isl_pw_multi_aff_from_map(set);
5166 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5167 * add it to *user.
5169 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5171 isl_union_pw_multi_aff **upma = user;
5172 isl_pw_multi_aff *pma;
5174 pma = isl_pw_multi_aff_from_map(map);
5175 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5177 return *upma ? isl_stat_ok : isl_stat_error;
5180 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5181 * domain.
5183 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5184 __isl_take isl_aff *aff)
5186 isl_multi_aff *ma;
5187 isl_pw_multi_aff *pma;
5189 ma = isl_multi_aff_from_aff(aff);
5190 pma = isl_pw_multi_aff_from_multi_aff(ma);
5191 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5194 /* Try and create an isl_union_pw_multi_aff that is equivalent
5195 * to the given isl_union_map.
5196 * The isl_union_map is required to be single-valued in each space.
5197 * Otherwise, an error is produced.
5199 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5200 __isl_take isl_union_map *umap)
5202 isl_space *space;
5203 isl_union_pw_multi_aff *upma;
5205 space = isl_union_map_get_space(umap);
5206 upma = isl_union_pw_multi_aff_empty(space);
5207 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5208 upma = isl_union_pw_multi_aff_free(upma);
5209 isl_union_map_free(umap);
5211 return upma;
5214 /* Try and create an isl_union_pw_multi_aff that is equivalent
5215 * to the given isl_union_set.
5216 * The isl_union_set is required to be a singleton in each space.
5217 * Otherwise, an error is produced.
5219 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5220 __isl_take isl_union_set *uset)
5222 return isl_union_pw_multi_aff_from_union_map(uset);
5225 /* Return the piecewise affine expression "set ? 1 : 0".
5227 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5229 isl_pw_aff *pa;
5230 isl_space *space = isl_set_get_space(set);
5231 isl_local_space *ls = isl_local_space_from_space(space);
5232 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5233 isl_aff *one = isl_aff_zero_on_domain(ls);
5235 one = isl_aff_add_constant_si(one, 1);
5236 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5237 set = isl_set_complement(set);
5238 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5240 return pa;
5243 /* Plug in "subs" for dimension "type", "pos" of "aff".
5245 * Let i be the dimension to replace and let "subs" be of the form
5247 * f/d
5249 * and "aff" of the form
5251 * (a i + g)/m
5253 * The result is
5255 * (a f + d g')/(m d)
5257 * where g' is the result of plugging in "subs" in each of the integer
5258 * divisions in g.
5260 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5261 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5263 isl_ctx *ctx;
5264 isl_int v;
5266 aff = isl_aff_cow(aff);
5267 if (!aff || !subs)
5268 return isl_aff_free(aff);
5270 ctx = isl_aff_get_ctx(aff);
5271 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5272 isl_die(ctx, isl_error_invalid,
5273 "spaces don't match", return isl_aff_free(aff));
5274 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5275 isl_die(ctx, isl_error_unsupported,
5276 "cannot handle divs yet", return isl_aff_free(aff));
5278 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5279 if (!aff->ls)
5280 return isl_aff_free(aff);
5282 aff->v = isl_vec_cow(aff->v);
5283 if (!aff->v)
5284 return isl_aff_free(aff);
5286 pos += isl_local_space_offset(aff->ls, type);
5288 isl_int_init(v);
5289 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5290 aff->v->size, subs->v->size, v);
5291 isl_int_clear(v);
5293 return aff;
5296 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5297 * expressions in "maff".
5299 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5300 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5301 __isl_keep isl_aff *subs)
5303 int i;
5305 maff = isl_multi_aff_cow(maff);
5306 if (!maff || !subs)
5307 return isl_multi_aff_free(maff);
5309 if (type == isl_dim_in)
5310 type = isl_dim_set;
5312 for (i = 0; i < maff->n; ++i) {
5313 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5314 type, pos, subs);
5315 if (!maff->u.p[i])
5316 return isl_multi_aff_free(maff);
5319 return maff;
5322 /* Plug in "subs" for dimension "type", "pos" of "pma".
5324 * pma is of the form
5326 * A_i(v) -> M_i(v)
5328 * while subs is of the form
5330 * v' = B_j(v) -> S_j
5332 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5333 * has a contribution in the result, in particular
5335 * C_ij(S_j) -> M_i(S_j)
5337 * Note that plugging in S_j in C_ij may also result in an empty set
5338 * and this contribution should simply be discarded.
5340 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5341 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5342 __isl_keep isl_pw_aff *subs)
5344 int i, j, n;
5345 isl_pw_multi_aff *res;
5347 if (!pma || !subs)
5348 return isl_pw_multi_aff_free(pma);
5350 n = pma->n * subs->n;
5351 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5353 for (i = 0; i < pma->n; ++i) {
5354 for (j = 0; j < subs->n; ++j) {
5355 isl_set *common;
5356 isl_multi_aff *res_ij;
5357 int empty;
5359 common = isl_set_intersect(
5360 isl_set_copy(pma->p[i].set),
5361 isl_set_copy(subs->p[j].set));
5362 common = isl_set_substitute(common,
5363 type, pos, subs->p[j].aff);
5364 empty = isl_set_plain_is_empty(common);
5365 if (empty < 0 || empty) {
5366 isl_set_free(common);
5367 if (empty < 0)
5368 goto error;
5369 continue;
5372 res_ij = isl_multi_aff_substitute(
5373 isl_multi_aff_copy(pma->p[i].maff),
5374 type, pos, subs->p[j].aff);
5376 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5380 isl_pw_multi_aff_free(pma);
5381 return res;
5382 error:
5383 isl_pw_multi_aff_free(pma);
5384 isl_pw_multi_aff_free(res);
5385 return NULL;
5388 /* Compute the preimage of a range of dimensions in the affine expression "src"
5389 * under "ma" and put the result in "dst". The number of dimensions in "src"
5390 * that precede the range is given by "n_before". The number of dimensions
5391 * in the range is given by the number of output dimensions of "ma".
5392 * The number of dimensions that follow the range is given by "n_after".
5393 * If "has_denom" is set (to one),
5394 * then "src" and "dst" have an extra initial denominator.
5395 * "n_div_ma" is the number of existentials in "ma"
5396 * "n_div_bset" is the number of existentials in "src"
5397 * The resulting "dst" (which is assumed to have been allocated by
5398 * the caller) contains coefficients for both sets of existentials,
5399 * first those in "ma" and then those in "src".
5400 * f, c1, c2 and g are temporary objects that have been initialized
5401 * by the caller.
5403 * Let src represent the expression
5405 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5407 * and let ma represent the expressions
5409 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5411 * We start out with the following expression for dst:
5413 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5415 * with the multiplication factor f initially equal to 1
5416 * and f \sum_i b_i v_i kept separately.
5417 * For each x_i that we substitute, we multiply the numerator
5418 * (and denominator) of dst by c_1 = m_i and add the numerator
5419 * of the x_i expression multiplied by c_2 = f b_i,
5420 * after removing the common factors of c_1 and c_2.
5421 * The multiplication factor f also needs to be multiplied by c_1
5422 * for the next x_j, j > i.
5424 void isl_seq_preimage(isl_int *dst, isl_int *src,
5425 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5426 int n_div_ma, int n_div_bmap,
5427 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5429 int i;
5430 int n_param, n_in, n_out;
5431 int o_dst, o_src;
5433 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5434 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5435 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5437 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5438 o_dst = o_src = has_denom + 1 + n_param + n_before;
5439 isl_seq_clr(dst + o_dst, n_in);
5440 o_dst += n_in;
5441 o_src += n_out;
5442 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5443 o_dst += n_after;
5444 o_src += n_after;
5445 isl_seq_clr(dst + o_dst, n_div_ma);
5446 o_dst += n_div_ma;
5447 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5449 isl_int_set_si(f, 1);
5451 for (i = 0; i < n_out; ++i) {
5452 int offset = has_denom + 1 + n_param + n_before + i;
5454 if (isl_int_is_zero(src[offset]))
5455 continue;
5456 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5457 isl_int_mul(c2, f, src[offset]);
5458 isl_int_gcd(g, c1, c2);
5459 isl_int_divexact(c1, c1, g);
5460 isl_int_divexact(c2, c2, g);
5462 isl_int_mul(f, f, c1);
5463 o_dst = has_denom;
5464 o_src = 1;
5465 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5466 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5467 o_dst += 1 + n_param;
5468 o_src += 1 + n_param;
5469 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5470 o_dst += n_before;
5471 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5472 c2, ma->u.p[i]->v->el + o_src, n_in);
5473 o_dst += n_in;
5474 o_src += n_in;
5475 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5476 o_dst += n_after;
5477 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5478 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5479 o_dst += n_div_ma;
5480 o_src += n_div_ma;
5481 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5482 if (has_denom)
5483 isl_int_mul(dst[0], dst[0], c1);
5487 /* Compute the pullback of "aff" by the function represented by "ma".
5488 * In other words, plug in "ma" in "aff". The result is an affine expression
5489 * defined over the domain space of "ma".
5491 * If "aff" is represented by
5493 * (a(p) + b x + c(divs))/d
5495 * and ma is represented by
5497 * x = D(p) + F(y) + G(divs')
5499 * then the result is
5501 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5503 * The divs in the local space of the input are similarly adjusted
5504 * through a call to isl_local_space_preimage_multi_aff.
5506 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5507 __isl_take isl_multi_aff *ma)
5509 isl_aff *res = NULL;
5510 isl_local_space *ls;
5511 int n_div_aff, n_div_ma;
5512 isl_int f, c1, c2, g;
5514 ma = isl_multi_aff_align_divs(ma);
5515 if (!aff || !ma)
5516 goto error;
5518 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5519 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5521 ls = isl_aff_get_domain_local_space(aff);
5522 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5523 res = isl_aff_alloc(ls);
5524 if (!res)
5525 goto error;
5527 isl_int_init(f);
5528 isl_int_init(c1);
5529 isl_int_init(c2);
5530 isl_int_init(g);
5532 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5533 f, c1, c2, g, 1);
5535 isl_int_clear(f);
5536 isl_int_clear(c1);
5537 isl_int_clear(c2);
5538 isl_int_clear(g);
5540 isl_aff_free(aff);
5541 isl_multi_aff_free(ma);
5542 res = isl_aff_normalize(res);
5543 return res;
5544 error:
5545 isl_aff_free(aff);
5546 isl_multi_aff_free(ma);
5547 isl_aff_free(res);
5548 return NULL;
5551 /* Compute the pullback of "aff1" by the function represented by "aff2".
5552 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5553 * defined over the domain space of "aff1".
5555 * The domain of "aff1" should match the range of "aff2", which means
5556 * that it should be single-dimensional.
5558 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5559 __isl_take isl_aff *aff2)
5561 isl_multi_aff *ma;
5563 ma = isl_multi_aff_from_aff(aff2);
5564 return isl_aff_pullback_multi_aff(aff1, ma);
5567 /* Compute the pullback of "ma1" by the function represented by "ma2".
5568 * In other words, plug in "ma2" in "ma1".
5570 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5572 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5573 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5575 int i;
5576 isl_space *space = NULL;
5578 ma2 = isl_multi_aff_align_divs(ma2);
5579 ma1 = isl_multi_aff_cow(ma1);
5580 if (!ma1 || !ma2)
5581 goto error;
5583 space = isl_space_join(isl_multi_aff_get_space(ma2),
5584 isl_multi_aff_get_space(ma1));
5586 for (i = 0; i < ma1->n; ++i) {
5587 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5588 isl_multi_aff_copy(ma2));
5589 if (!ma1->u.p[i])
5590 goto error;
5593 ma1 = isl_multi_aff_reset_space(ma1, space);
5594 isl_multi_aff_free(ma2);
5595 return ma1;
5596 error:
5597 isl_space_free(space);
5598 isl_multi_aff_free(ma2);
5599 isl_multi_aff_free(ma1);
5600 return NULL;
5603 /* Compute the pullback of "ma1" by the function represented by "ma2".
5604 * In other words, plug in "ma2" in "ma1".
5606 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5607 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5609 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5610 &isl_multi_aff_pullback_multi_aff_aligned);
5613 /* Extend the local space of "dst" to include the divs
5614 * in the local space of "src".
5616 * If "src" does not have any divs or if the local spaces of "dst" and
5617 * "src" are the same, then no extension is required.
5619 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5620 __isl_keep isl_aff *src)
5622 isl_ctx *ctx;
5623 int src_n_div, dst_n_div;
5624 int *exp1 = NULL;
5625 int *exp2 = NULL;
5626 isl_bool equal;
5627 isl_mat *div;
5629 if (!src || !dst)
5630 return isl_aff_free(dst);
5632 ctx = isl_aff_get_ctx(src);
5633 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5634 if (equal < 0)
5635 return isl_aff_free(dst);
5636 if (!equal)
5637 isl_die(ctx, isl_error_invalid,
5638 "spaces don't match", goto error);
5640 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5641 if (src_n_div == 0)
5642 return dst;
5643 equal = isl_local_space_is_equal(src->ls, dst->ls);
5644 if (equal < 0)
5645 return isl_aff_free(dst);
5646 if (equal)
5647 return dst;
5649 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5650 exp1 = isl_alloc_array(ctx, int, src_n_div);
5651 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5652 if (!exp1 || (dst_n_div && !exp2))
5653 goto error;
5655 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5656 dst = isl_aff_expand_divs(dst, div, exp2);
5657 free(exp1);
5658 free(exp2);
5660 return dst;
5661 error:
5662 free(exp1);
5663 free(exp2);
5664 return isl_aff_free(dst);
5667 /* Adjust the local spaces of the affine expressions in "maff"
5668 * such that they all have the save divs.
5670 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5671 __isl_take isl_multi_aff *maff)
5673 int i;
5675 if (!maff)
5676 return NULL;
5677 if (maff->n == 0)
5678 return maff;
5679 maff = isl_multi_aff_cow(maff);
5680 if (!maff)
5681 return NULL;
5683 for (i = 1; i < maff->n; ++i)
5684 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5685 for (i = 1; i < maff->n; ++i) {
5686 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5687 if (!maff->u.p[i])
5688 return isl_multi_aff_free(maff);
5691 return maff;
5694 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5696 aff = isl_aff_cow(aff);
5697 if (!aff)
5698 return NULL;
5700 aff->ls = isl_local_space_lift(aff->ls);
5701 if (!aff->ls)
5702 return isl_aff_free(aff);
5704 return aff;
5707 /* Lift "maff" to a space with extra dimensions such that the result
5708 * has no more existentially quantified variables.
5709 * If "ls" is not NULL, then *ls is assigned the local space that lies
5710 * at the basis of the lifting applied to "maff".
5712 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5713 __isl_give isl_local_space **ls)
5715 int i;
5716 isl_space *space;
5717 unsigned n_div;
5719 if (ls)
5720 *ls = NULL;
5722 if (!maff)
5723 return NULL;
5725 if (maff->n == 0) {
5726 if (ls) {
5727 isl_space *space = isl_multi_aff_get_domain_space(maff);
5728 *ls = isl_local_space_from_space(space);
5729 if (!*ls)
5730 return isl_multi_aff_free(maff);
5732 return maff;
5735 maff = isl_multi_aff_cow(maff);
5736 maff = isl_multi_aff_align_divs(maff);
5737 if (!maff)
5738 return NULL;
5740 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5741 space = isl_multi_aff_get_space(maff);
5742 space = isl_space_lift(isl_space_domain(space), n_div);
5743 space = isl_space_extend_domain_with_range(space,
5744 isl_multi_aff_get_space(maff));
5745 if (!space)
5746 return isl_multi_aff_free(maff);
5747 isl_space_free(maff->space);
5748 maff->space = space;
5750 if (ls) {
5751 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5752 if (!*ls)
5753 return isl_multi_aff_free(maff);
5756 for (i = 0; i < maff->n; ++i) {
5757 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5758 if (!maff->u.p[i])
5759 goto error;
5762 return maff;
5763 error:
5764 if (ls)
5765 isl_local_space_free(*ls);
5766 return isl_multi_aff_free(maff);
5770 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5772 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5773 __isl_keep isl_pw_multi_aff *pma, int pos)
5775 int i;
5776 int n_out;
5777 isl_space *space;
5778 isl_pw_aff *pa;
5780 if (!pma)
5781 return NULL;
5783 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5784 if (pos < 0 || pos >= n_out)
5785 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5786 "index out of bounds", return NULL);
5788 space = isl_pw_multi_aff_get_space(pma);
5789 space = isl_space_drop_dims(space, isl_dim_out,
5790 pos + 1, n_out - pos - 1);
5791 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5793 pa = isl_pw_aff_alloc_size(space, pma->n);
5794 for (i = 0; i < pma->n; ++i) {
5795 isl_aff *aff;
5796 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5797 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5800 return pa;
5803 /* Return an isl_pw_multi_aff with the given "set" as domain and
5804 * an unnamed zero-dimensional range.
5806 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5807 __isl_take isl_set *set)
5809 isl_multi_aff *ma;
5810 isl_space *space;
5812 space = isl_set_get_space(set);
5813 space = isl_space_from_domain(space);
5814 ma = isl_multi_aff_zero(space);
5815 return isl_pw_multi_aff_alloc(set, ma);
5818 /* Add an isl_pw_multi_aff with the given "set" as domain and
5819 * an unnamed zero-dimensional range to *user.
5821 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5822 void *user)
5824 isl_union_pw_multi_aff **upma = user;
5825 isl_pw_multi_aff *pma;
5827 pma = isl_pw_multi_aff_from_domain(set);
5828 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5830 return isl_stat_ok;
5833 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5834 * an unnamed zero-dimensional range.
5836 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5837 __isl_take isl_union_set *uset)
5839 isl_space *space;
5840 isl_union_pw_multi_aff *upma;
5842 if (!uset)
5843 return NULL;
5845 space = isl_union_set_get_space(uset);
5846 upma = isl_union_pw_multi_aff_empty(space);
5848 if (isl_union_set_foreach_set(uset,
5849 &add_pw_multi_aff_from_domain, &upma) < 0)
5850 goto error;
5852 isl_union_set_free(uset);
5853 return upma;
5854 error:
5855 isl_union_set_free(uset);
5856 isl_union_pw_multi_aff_free(upma);
5857 return NULL;
5860 /* Local data for bin_entry and the callback "fn".
5862 struct isl_union_pw_multi_aff_bin_data {
5863 isl_union_pw_multi_aff *upma2;
5864 isl_union_pw_multi_aff *res;
5865 isl_pw_multi_aff *pma;
5866 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5869 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5870 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5872 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5874 struct isl_union_pw_multi_aff_bin_data *data = user;
5875 isl_stat r;
5877 data->pma = pma;
5878 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5879 data->fn, data);
5880 isl_pw_multi_aff_free(pma);
5882 return r;
5885 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5886 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5887 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5888 * as *entry. The callback should adjust data->res if desired.
5890 static __isl_give isl_union_pw_multi_aff *bin_op(
5891 __isl_take isl_union_pw_multi_aff *upma1,
5892 __isl_take isl_union_pw_multi_aff *upma2,
5893 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5895 isl_space *space;
5896 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5898 space = isl_union_pw_multi_aff_get_space(upma2);
5899 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5900 space = isl_union_pw_multi_aff_get_space(upma1);
5901 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5903 if (!upma1 || !upma2)
5904 goto error;
5906 data.upma2 = upma2;
5907 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5908 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5909 &bin_entry, &data) < 0)
5910 goto error;
5912 isl_union_pw_multi_aff_free(upma1);
5913 isl_union_pw_multi_aff_free(upma2);
5914 return data.res;
5915 error:
5916 isl_union_pw_multi_aff_free(upma1);
5917 isl_union_pw_multi_aff_free(upma2);
5918 isl_union_pw_multi_aff_free(data.res);
5919 return NULL;
5922 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5923 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5925 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5926 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5928 isl_space *space;
5930 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5931 isl_pw_multi_aff_get_space(pma2));
5932 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5933 &isl_multi_aff_range_product);
5936 /* Given two isl_pw_multi_affs A -> B and C -> D,
5937 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5939 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5940 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5942 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5943 &pw_multi_aff_range_product);
5946 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5947 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5949 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5950 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5952 isl_space *space;
5954 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5955 isl_pw_multi_aff_get_space(pma2));
5956 space = isl_space_flatten_range(space);
5957 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5958 &isl_multi_aff_flat_range_product);
5961 /* Given two isl_pw_multi_affs A -> B and C -> D,
5962 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5964 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5965 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5967 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5968 &pw_multi_aff_flat_range_product);
5971 /* If data->pma and "pma2" have the same domain space, then compute
5972 * their flat range product and the result to data->res.
5974 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
5975 void *user)
5977 struct isl_union_pw_multi_aff_bin_data *data = user;
5979 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5980 pma2->dim, isl_dim_in)) {
5981 isl_pw_multi_aff_free(pma2);
5982 return isl_stat_ok;
5985 pma2 = isl_pw_multi_aff_flat_range_product(
5986 isl_pw_multi_aff_copy(data->pma), pma2);
5988 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5990 return isl_stat_ok;
5993 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5994 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5996 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5997 __isl_take isl_union_pw_multi_aff *upma1,
5998 __isl_take isl_union_pw_multi_aff *upma2)
6000 return bin_op(upma1, upma2, &flat_range_product_entry);
6003 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6004 * The parameters are assumed to have been aligned.
6006 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6007 * except that it works on two different isl_pw_* types.
6009 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6010 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6011 __isl_take isl_pw_aff *pa)
6013 int i, j, n;
6014 isl_pw_multi_aff *res = NULL;
6016 if (!pma || !pa)
6017 goto error;
6019 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6020 pa->dim, isl_dim_in))
6021 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6022 "domains don't match", goto error);
6023 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6024 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6025 "index out of bounds", goto error);
6027 n = pma->n * pa->n;
6028 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6030 for (i = 0; i < pma->n; ++i) {
6031 for (j = 0; j < pa->n; ++j) {
6032 isl_set *common;
6033 isl_multi_aff *res_ij;
6034 int empty;
6036 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6037 isl_set_copy(pa->p[j].set));
6038 empty = isl_set_plain_is_empty(common);
6039 if (empty < 0 || empty) {
6040 isl_set_free(common);
6041 if (empty < 0)
6042 goto error;
6043 continue;
6046 res_ij = isl_multi_aff_set_aff(
6047 isl_multi_aff_copy(pma->p[i].maff), pos,
6048 isl_aff_copy(pa->p[j].aff));
6049 res_ij = isl_multi_aff_gist(res_ij,
6050 isl_set_copy(common));
6052 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6056 isl_pw_multi_aff_free(pma);
6057 isl_pw_aff_free(pa);
6058 return res;
6059 error:
6060 isl_pw_multi_aff_free(pma);
6061 isl_pw_aff_free(pa);
6062 return isl_pw_multi_aff_free(res);
6065 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6067 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6068 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6069 __isl_take isl_pw_aff *pa)
6071 isl_bool equal_params;
6073 if (!pma || !pa)
6074 goto error;
6075 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6076 if (equal_params < 0)
6077 goto error;
6078 if (equal_params)
6079 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6080 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6081 isl_pw_aff_check_named_params(pa) < 0)
6082 goto error;
6083 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6084 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6085 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6086 error:
6087 isl_pw_multi_aff_free(pma);
6088 isl_pw_aff_free(pa);
6089 return NULL;
6092 /* Do the parameters of "pa" match those of "space"?
6094 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6095 __isl_keep isl_space *space)
6097 isl_space *pa_space;
6098 isl_bool match;
6100 if (!pa || !space)
6101 return isl_bool_error;
6103 pa_space = isl_pw_aff_get_space(pa);
6105 match = isl_space_has_equal_params(space, pa_space);
6107 isl_space_free(pa_space);
6108 return match;
6111 /* Check that the domain space of "pa" matches "space".
6113 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6114 __isl_keep isl_space *space)
6116 isl_space *pa_space;
6117 isl_bool match;
6119 if (!pa || !space)
6120 return isl_stat_error;
6122 pa_space = isl_pw_aff_get_space(pa);
6124 match = isl_space_has_equal_params(space, pa_space);
6125 if (match < 0)
6126 goto error;
6127 if (!match)
6128 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6129 "parameters don't match", goto error);
6130 match = isl_space_tuple_is_equal(space, isl_dim_in,
6131 pa_space, isl_dim_in);
6132 if (match < 0)
6133 goto error;
6134 if (!match)
6135 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6136 "domains don't match", goto error);
6137 isl_space_free(pa_space);
6138 return isl_stat_ok;
6139 error:
6140 isl_space_free(pa_space);
6141 return isl_stat_error;
6144 #undef BASE
6145 #define BASE pw_aff
6146 #undef DOMBASE
6147 #define DOMBASE set
6149 #include <isl_multi_explicit_domain.c>
6150 #include <isl_multi_pw_aff_explicit_domain.c>
6151 #include <isl_multi_templ.c>
6152 #include <isl_multi_apply_set.c>
6153 #include <isl_multi_coalesce.c>
6154 #include <isl_multi_domain_templ.c>
6155 #include <isl_multi_dims.c>
6156 #include <isl_multi_from_base_templ.c>
6157 #include <isl_multi_gist.c>
6158 #include <isl_multi_hash.c>
6159 #include <isl_multi_identity_templ.c>
6160 #include <isl_multi_align_set.c>
6161 #include <isl_multi_intersect.c>
6162 #include <isl_multi_move_dims_templ.c>
6163 #include <isl_multi_product_templ.c>
6164 #include <isl_multi_splice_templ.c>
6165 #include <isl_multi_zero_templ.c>
6167 /* Does "mpa" have a non-trivial explicit domain?
6169 * The explicit domain, if present, is trivial if it represents
6170 * an (obviously) universe set.
6172 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6173 __isl_keep isl_multi_pw_aff *mpa)
6175 if (!mpa)
6176 return isl_bool_error;
6177 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6178 return isl_bool_false;
6179 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6182 /* Scale the elements of "pma" by the corresponding elements of "mv".
6184 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6185 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6187 int i;
6188 isl_bool equal_params;
6190 pma = isl_pw_multi_aff_cow(pma);
6191 if (!pma || !mv)
6192 goto error;
6193 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6194 mv->space, isl_dim_set))
6195 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6196 "spaces don't match", goto error);
6197 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6198 if (equal_params < 0)
6199 goto error;
6200 if (!equal_params) {
6201 pma = isl_pw_multi_aff_align_params(pma,
6202 isl_multi_val_get_space(mv));
6203 mv = isl_multi_val_align_params(mv,
6204 isl_pw_multi_aff_get_space(pma));
6205 if (!pma || !mv)
6206 goto error;
6209 for (i = 0; i < pma->n; ++i) {
6210 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6211 isl_multi_val_copy(mv));
6212 if (!pma->p[i].maff)
6213 goto error;
6216 isl_multi_val_free(mv);
6217 return pma;
6218 error:
6219 isl_multi_val_free(mv);
6220 isl_pw_multi_aff_free(pma);
6221 return NULL;
6224 /* This function is called for each entry of an isl_union_pw_multi_aff.
6225 * If the space of the entry matches that of data->mv,
6226 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6227 * Otherwise, return an empty isl_pw_multi_aff.
6229 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6230 __isl_take isl_pw_multi_aff *pma, void *user)
6232 isl_multi_val *mv = user;
6234 if (!pma)
6235 return NULL;
6236 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6237 mv->space, isl_dim_set)) {
6238 isl_space *space = isl_pw_multi_aff_get_space(pma);
6239 isl_pw_multi_aff_free(pma);
6240 return isl_pw_multi_aff_empty(space);
6243 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6246 /* Scale the elements of "upma" by the corresponding elements of "mv",
6247 * for those entries that match the space of "mv".
6249 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6250 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6252 upma = isl_union_pw_multi_aff_align_params(upma,
6253 isl_multi_val_get_space(mv));
6254 mv = isl_multi_val_align_params(mv,
6255 isl_union_pw_multi_aff_get_space(upma));
6256 if (!upma || !mv)
6257 goto error;
6259 return isl_union_pw_multi_aff_transform(upma,
6260 &union_pw_multi_aff_scale_multi_val_entry, mv);
6262 isl_multi_val_free(mv);
6263 return upma;
6264 error:
6265 isl_multi_val_free(mv);
6266 isl_union_pw_multi_aff_free(upma);
6267 return NULL;
6270 /* Construct and return a piecewise multi affine expression
6271 * in the given space with value zero in each of the output dimensions and
6272 * a universe domain.
6274 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6276 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6279 /* Construct and return a piecewise multi affine expression
6280 * that is equal to the given piecewise affine expression.
6282 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6283 __isl_take isl_pw_aff *pa)
6285 int i;
6286 isl_space *space;
6287 isl_pw_multi_aff *pma;
6289 if (!pa)
6290 return NULL;
6292 space = isl_pw_aff_get_space(pa);
6293 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6295 for (i = 0; i < pa->n; ++i) {
6296 isl_set *set;
6297 isl_multi_aff *ma;
6299 set = isl_set_copy(pa->p[i].set);
6300 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6301 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6304 isl_pw_aff_free(pa);
6305 return pma;
6308 /* Construct and return a piecewise multi affine expression
6309 * that is equal to the given multi piecewise affine expression
6310 * on the shared domain of the piecewise affine expressions,
6311 * in the special case of a 0D multi piecewise affine expression.
6313 * Create a piecewise multi affine expression with the explicit domain of
6314 * the 0D multi piecewise affine expression as domain.
6316 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6317 __isl_take isl_multi_pw_aff *mpa)
6319 isl_space *space;
6320 isl_set *dom;
6321 isl_multi_aff *ma;
6323 space = isl_multi_pw_aff_get_space(mpa);
6324 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6325 isl_multi_pw_aff_free(mpa);
6327 ma = isl_multi_aff_zero(space);
6328 return isl_pw_multi_aff_alloc(dom, ma);
6331 /* Construct and return a piecewise multi affine expression
6332 * that is equal to the given multi piecewise affine expression
6333 * on the shared domain of the piecewise affine expressions.
6335 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6336 __isl_take isl_multi_pw_aff *mpa)
6338 int i;
6339 isl_space *space;
6340 isl_pw_aff *pa;
6341 isl_pw_multi_aff *pma;
6343 if (!mpa)
6344 return NULL;
6346 if (mpa->n == 0)
6347 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6349 space = isl_multi_pw_aff_get_space(mpa);
6350 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6351 pma = isl_pw_multi_aff_from_pw_aff(pa);
6353 for (i = 1; i < mpa->n; ++i) {
6354 isl_pw_multi_aff *pma_i;
6356 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6357 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6358 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6361 pma = isl_pw_multi_aff_reset_space(pma, space);
6363 isl_multi_pw_aff_free(mpa);
6364 return pma;
6367 /* Construct and return a multi piecewise affine expression
6368 * that is equal to the given multi affine expression.
6370 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6371 __isl_take isl_multi_aff *ma)
6373 int i, n;
6374 isl_multi_pw_aff *mpa;
6376 if (!ma)
6377 return NULL;
6379 n = isl_multi_aff_dim(ma, isl_dim_out);
6380 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6382 for (i = 0; i < n; ++i) {
6383 isl_pw_aff *pa;
6385 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6386 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6389 isl_multi_aff_free(ma);
6390 return mpa;
6393 /* Construct and return a multi piecewise affine expression
6394 * that is equal to the given piecewise multi affine expression.
6396 * If the resulting multi piecewise affine expression has
6397 * an explicit domain, then assign it the domain of the input.
6398 * In other cases, the domain is stored in the individual elements.
6400 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6401 __isl_take isl_pw_multi_aff *pma)
6403 int i, n;
6404 isl_space *space;
6405 isl_multi_pw_aff *mpa;
6407 if (!pma)
6408 return NULL;
6410 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6411 space = isl_pw_multi_aff_get_space(pma);
6412 mpa = isl_multi_pw_aff_alloc(space);
6414 for (i = 0; i < n; ++i) {
6415 isl_pw_aff *pa;
6417 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6418 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6420 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6421 isl_set *dom;
6423 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6424 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6427 isl_pw_multi_aff_free(pma);
6428 return mpa;
6431 /* Do "pa1" and "pa2" represent the same function?
6433 * We first check if they are obviously equal.
6434 * If not, we convert them to maps and check if those are equal.
6436 * If "pa1" or "pa2" contain any NaNs, then they are considered
6437 * not to be the same. A NaN is not equal to anything, not even
6438 * to another NaN.
6440 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6441 __isl_keep isl_pw_aff *pa2)
6443 isl_bool equal;
6444 isl_bool has_nan;
6445 isl_map *map1, *map2;
6447 if (!pa1 || !pa2)
6448 return isl_bool_error;
6450 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6451 if (equal < 0 || equal)
6452 return equal;
6453 has_nan = either_involves_nan(pa1, pa2);
6454 if (has_nan < 0)
6455 return isl_bool_error;
6456 if (has_nan)
6457 return isl_bool_false;
6459 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6460 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6461 equal = isl_map_is_equal(map1, map2);
6462 isl_map_free(map1);
6463 isl_map_free(map2);
6465 return equal;
6468 /* Do "mpa1" and "mpa2" represent the same function?
6470 * Note that we cannot convert the entire isl_multi_pw_aff
6471 * to a map because the domains of the piecewise affine expressions
6472 * may not be the same.
6474 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6475 __isl_keep isl_multi_pw_aff *mpa2)
6477 int i;
6478 isl_bool equal, equal_params;
6480 if (!mpa1 || !mpa2)
6481 return isl_bool_error;
6483 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6484 if (equal_params < 0)
6485 return isl_bool_error;
6486 if (!equal_params) {
6487 if (!isl_space_has_named_params(mpa1->space))
6488 return isl_bool_false;
6489 if (!isl_space_has_named_params(mpa2->space))
6490 return isl_bool_false;
6491 mpa1 = isl_multi_pw_aff_copy(mpa1);
6492 mpa2 = isl_multi_pw_aff_copy(mpa2);
6493 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6494 isl_multi_pw_aff_get_space(mpa2));
6495 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6496 isl_multi_pw_aff_get_space(mpa1));
6497 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6498 isl_multi_pw_aff_free(mpa1);
6499 isl_multi_pw_aff_free(mpa2);
6500 return equal;
6503 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6504 if (equal < 0 || !equal)
6505 return equal;
6507 for (i = 0; i < mpa1->n; ++i) {
6508 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6509 if (equal < 0 || !equal)
6510 return equal;
6513 return isl_bool_true;
6516 /* Do "pma1" and "pma2" represent the same function?
6518 * First check if they are obviously equal.
6519 * If not, then convert them to maps and check if those are equal.
6521 * If "pa1" or "pa2" contain any NaNs, then they are considered
6522 * not to be the same. A NaN is not equal to anything, not even
6523 * to another NaN.
6525 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6526 __isl_keep isl_pw_multi_aff *pma2)
6528 isl_bool equal;
6529 isl_bool has_nan;
6530 isl_map *map1, *map2;
6532 if (!pma1 || !pma2)
6533 return isl_bool_error;
6535 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6536 if (equal < 0 || equal)
6537 return equal;
6538 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6539 if (has_nan >= 0 && !has_nan)
6540 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6541 if (has_nan < 0 || has_nan)
6542 return isl_bool_not(has_nan);
6544 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6545 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6546 equal = isl_map_is_equal(map1, map2);
6547 isl_map_free(map1);
6548 isl_map_free(map2);
6550 return equal;
6553 /* Compute the pullback of "mpa" by the function represented by "ma".
6554 * In other words, plug in "ma" in "mpa".
6556 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6558 * If "mpa" has an explicit domain, then it is this domain
6559 * that needs to undergo a pullback, i.e., a preimage.
6561 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6562 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6564 int i;
6565 isl_space *space = NULL;
6567 mpa = isl_multi_pw_aff_cow(mpa);
6568 if (!mpa || !ma)
6569 goto error;
6571 space = isl_space_join(isl_multi_aff_get_space(ma),
6572 isl_multi_pw_aff_get_space(mpa));
6573 if (!space)
6574 goto error;
6576 for (i = 0; i < mpa->n; ++i) {
6577 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6578 isl_multi_aff_copy(ma));
6579 if (!mpa->u.p[i])
6580 goto error;
6582 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6583 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6584 isl_multi_aff_copy(ma));
6585 if (!mpa->u.dom)
6586 goto error;
6589 isl_multi_aff_free(ma);
6590 isl_space_free(mpa->space);
6591 mpa->space = space;
6592 return mpa;
6593 error:
6594 isl_space_free(space);
6595 isl_multi_pw_aff_free(mpa);
6596 isl_multi_aff_free(ma);
6597 return NULL;
6600 /* Compute the pullback of "mpa" by the function represented by "ma".
6601 * In other words, plug in "ma" in "mpa".
6603 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6604 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6606 isl_bool equal_params;
6608 if (!mpa || !ma)
6609 goto error;
6610 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6611 if (equal_params < 0)
6612 goto error;
6613 if (equal_params)
6614 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6615 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6616 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6617 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6618 error:
6619 isl_multi_pw_aff_free(mpa);
6620 isl_multi_aff_free(ma);
6621 return NULL;
6624 /* Compute the pullback of "mpa" by the function represented by "pma".
6625 * In other words, plug in "pma" in "mpa".
6627 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6629 * If "mpa" has an explicit domain, then it is this domain
6630 * that needs to undergo a pullback, i.e., a preimage.
6632 static __isl_give isl_multi_pw_aff *
6633 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6634 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6636 int i;
6637 isl_space *space = NULL;
6639 mpa = isl_multi_pw_aff_cow(mpa);
6640 if (!mpa || !pma)
6641 goto error;
6643 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6644 isl_multi_pw_aff_get_space(mpa));
6646 for (i = 0; i < mpa->n; ++i) {
6647 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6648 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6649 if (!mpa->u.p[i])
6650 goto error;
6652 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6653 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6654 isl_pw_multi_aff_copy(pma));
6655 if (!mpa->u.dom)
6656 goto error;
6659 isl_pw_multi_aff_free(pma);
6660 isl_space_free(mpa->space);
6661 mpa->space = space;
6662 return mpa;
6663 error:
6664 isl_space_free(space);
6665 isl_multi_pw_aff_free(mpa);
6666 isl_pw_multi_aff_free(pma);
6667 return NULL;
6670 /* Compute the pullback of "mpa" by the function represented by "pma".
6671 * In other words, plug in "pma" in "mpa".
6673 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6674 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6676 isl_bool equal_params;
6678 if (!mpa || !pma)
6679 goto error;
6680 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6681 if (equal_params < 0)
6682 goto error;
6683 if (equal_params)
6684 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6685 mpa = isl_multi_pw_aff_align_params(mpa,
6686 isl_pw_multi_aff_get_space(pma));
6687 pma = isl_pw_multi_aff_align_params(pma,
6688 isl_multi_pw_aff_get_space(mpa));
6689 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6690 error:
6691 isl_multi_pw_aff_free(mpa);
6692 isl_pw_multi_aff_free(pma);
6693 return NULL;
6696 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6697 * with the domain of "aff". The domain of the result is the same
6698 * as that of "mpa".
6699 * "mpa" and "aff" are assumed to have been aligned.
6701 * We first extract the parametric constant from "aff", defined
6702 * over the correct domain.
6703 * Then we add the appropriate combinations of the members of "mpa".
6704 * Finally, we add the integer divisions through recursive calls.
6706 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6707 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6709 int i, n_in, n_div;
6710 isl_space *space;
6711 isl_val *v;
6712 isl_pw_aff *pa;
6713 isl_aff *tmp;
6715 n_in = isl_aff_dim(aff, isl_dim_in);
6716 n_div = isl_aff_dim(aff, isl_dim_div);
6718 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6719 tmp = isl_aff_copy(aff);
6720 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6721 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6722 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6723 isl_space_dim(space, isl_dim_set));
6724 tmp = isl_aff_reset_domain_space(tmp, space);
6725 pa = isl_pw_aff_from_aff(tmp);
6727 for (i = 0; i < n_in; ++i) {
6728 isl_pw_aff *pa_i;
6730 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6731 continue;
6732 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6733 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6734 pa_i = isl_pw_aff_scale_val(pa_i, v);
6735 pa = isl_pw_aff_add(pa, pa_i);
6738 for (i = 0; i < n_div; ++i) {
6739 isl_aff *div;
6740 isl_pw_aff *pa_i;
6742 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6743 continue;
6744 div = isl_aff_get_div(aff, i);
6745 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6746 isl_multi_pw_aff_copy(mpa), div);
6747 pa_i = isl_pw_aff_floor(pa_i);
6748 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6749 pa_i = isl_pw_aff_scale_val(pa_i, v);
6750 pa = isl_pw_aff_add(pa, pa_i);
6753 isl_multi_pw_aff_free(mpa);
6754 isl_aff_free(aff);
6756 return pa;
6759 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6760 * with the domain of "aff". The domain of the result is the same
6761 * as that of "mpa".
6763 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6764 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6766 isl_bool equal_params;
6768 if (!aff || !mpa)
6769 goto error;
6770 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6771 if (equal_params < 0)
6772 goto error;
6773 if (equal_params)
6774 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6776 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6777 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6779 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6780 error:
6781 isl_aff_free(aff);
6782 isl_multi_pw_aff_free(mpa);
6783 return NULL;
6786 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6787 * with the domain of "pa". The domain of the result is the same
6788 * as that of "mpa".
6789 * "mpa" and "pa" are assumed to have been aligned.
6791 * We consider each piece in turn. Note that the domains of the
6792 * pieces are assumed to be disjoint and they remain disjoint
6793 * after taking the preimage (over the same function).
6795 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6796 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6798 isl_space *space;
6799 isl_pw_aff *res;
6800 int i;
6802 if (!mpa || !pa)
6803 goto error;
6805 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6806 isl_pw_aff_get_space(pa));
6807 res = isl_pw_aff_empty(space);
6809 for (i = 0; i < pa->n; ++i) {
6810 isl_pw_aff *pa_i;
6811 isl_set *domain;
6813 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6814 isl_multi_pw_aff_copy(mpa),
6815 isl_aff_copy(pa->p[i].aff));
6816 domain = isl_set_copy(pa->p[i].set);
6817 domain = isl_set_preimage_multi_pw_aff(domain,
6818 isl_multi_pw_aff_copy(mpa));
6819 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6820 res = isl_pw_aff_add_disjoint(res, pa_i);
6823 isl_pw_aff_free(pa);
6824 isl_multi_pw_aff_free(mpa);
6825 return res;
6826 error:
6827 isl_pw_aff_free(pa);
6828 isl_multi_pw_aff_free(mpa);
6829 return NULL;
6832 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6833 * with the domain of "pa". The domain of the result is the same
6834 * as that of "mpa".
6836 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6837 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6839 isl_bool equal_params;
6841 if (!pa || !mpa)
6842 goto error;
6843 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6844 if (equal_params < 0)
6845 goto error;
6846 if (equal_params)
6847 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6849 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6850 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6852 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6853 error:
6854 isl_pw_aff_free(pa);
6855 isl_multi_pw_aff_free(mpa);
6856 return NULL;
6859 /* Compute the pullback of "pa" by the function represented by "mpa".
6860 * In other words, plug in "mpa" in "pa".
6861 * "pa" and "mpa" are assumed to have been aligned.
6863 * The pullback is computed by applying "pa" to "mpa".
6865 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6866 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6868 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6871 /* Compute the pullback of "pa" by the function represented by "mpa".
6872 * In other words, plug in "mpa" in "pa".
6874 * The pullback is computed by applying "pa" to "mpa".
6876 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6877 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6879 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6882 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6883 * In other words, plug in "mpa2" in "mpa1".
6885 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6887 * We pullback each member of "mpa1" in turn.
6889 * If "mpa1" has an explicit domain, then it is this domain
6890 * that needs to undergo a pullback instead, i.e., a preimage.
6892 static __isl_give isl_multi_pw_aff *
6893 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6894 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6896 int i;
6897 isl_space *space = NULL;
6899 mpa1 = isl_multi_pw_aff_cow(mpa1);
6900 if (!mpa1 || !mpa2)
6901 goto error;
6903 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6904 isl_multi_pw_aff_get_space(mpa1));
6906 for (i = 0; i < mpa1->n; ++i) {
6907 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6908 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
6909 if (!mpa1->u.p[i])
6910 goto error;
6913 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
6914 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
6915 isl_multi_pw_aff_copy(mpa2));
6916 if (!mpa1->u.dom)
6917 goto error;
6919 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6921 isl_multi_pw_aff_free(mpa2);
6922 return mpa1;
6923 error:
6924 isl_space_free(space);
6925 isl_multi_pw_aff_free(mpa1);
6926 isl_multi_pw_aff_free(mpa2);
6927 return NULL;
6930 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6931 * In other words, plug in "mpa2" in "mpa1".
6933 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6934 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6936 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6937 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6940 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6941 * of "mpa1" and "mpa2" live in the same space, construct map space
6942 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6943 * with this map space as extract argument.
6945 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6946 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6947 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6948 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6950 int match;
6951 isl_space *space1, *space2;
6952 isl_map *res;
6954 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6955 isl_multi_pw_aff_get_space(mpa2));
6956 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6957 isl_multi_pw_aff_get_space(mpa1));
6958 if (!mpa1 || !mpa2)
6959 goto error;
6960 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6961 mpa2->space, isl_dim_out);
6962 if (match < 0)
6963 goto error;
6964 if (!match)
6965 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6966 "range spaces don't match", goto error);
6967 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6968 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6969 space1 = isl_space_map_from_domain_and_range(space1, space2);
6971 res = order(mpa1, mpa2, space1);
6972 isl_multi_pw_aff_free(mpa1);
6973 isl_multi_pw_aff_free(mpa2);
6974 return res;
6975 error:
6976 isl_multi_pw_aff_free(mpa1);
6977 isl_multi_pw_aff_free(mpa2);
6978 return NULL;
6981 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6982 * where the function values are equal. "space" is the space of the result.
6983 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6985 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6986 * in the sequences are equal.
6988 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6989 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6990 __isl_take isl_space *space)
6992 int i, n;
6993 isl_map *res;
6995 res = isl_map_universe(space);
6997 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6998 for (i = 0; i < n; ++i) {
6999 isl_pw_aff *pa1, *pa2;
7000 isl_map *map;
7002 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7003 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7004 map = isl_pw_aff_eq_map(pa1, pa2);
7005 res = isl_map_intersect(res, map);
7008 return res;
7011 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7012 * where the function values are equal.
7014 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7015 __isl_take isl_multi_pw_aff *mpa2)
7017 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7018 &isl_multi_pw_aff_eq_map_on_space);
7021 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7022 * where the function values of "mpa1" is lexicographically satisfies "base"
7023 * compared to that of "mpa2". "space" is the space of the result.
7024 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7026 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7027 * if its i-th element satisfies "base" when compared to
7028 * the i-th element of "mpa2" while all previous elements are
7029 * pairwise equal.
7031 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7032 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7033 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7034 __isl_take isl_pw_aff *pa2),
7035 __isl_take isl_space *space)
7037 int i, n;
7038 isl_map *res, *rest;
7040 res = isl_map_empty(isl_space_copy(space));
7041 rest = isl_map_universe(space);
7043 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7044 for (i = 0; i < n; ++i) {
7045 isl_pw_aff *pa1, *pa2;
7046 isl_map *map;
7048 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7049 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7050 map = base(pa1, pa2);
7051 map = isl_map_intersect(map, isl_map_copy(rest));
7052 res = isl_map_union(res, map);
7054 if (i == n - 1)
7055 continue;
7057 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7058 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7059 map = isl_pw_aff_eq_map(pa1, pa2);
7060 rest = isl_map_intersect(rest, map);
7063 isl_map_free(rest);
7064 return res;
7067 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7068 * where the function value of "mpa1" is lexicographically less than that
7069 * of "mpa2". "space" is the space of the result.
7070 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7072 * "mpa1" is less than "mpa2" if its i-th element is smaller
7073 * than the i-th element of "mpa2" while all previous elements are
7074 * pairwise equal.
7076 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7077 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7078 __isl_take isl_space *space)
7080 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7081 &isl_pw_aff_lt_map, space);
7084 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7085 * where the function value of "mpa1" is lexicographically less than that
7086 * of "mpa2".
7088 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7089 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7091 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7092 &isl_multi_pw_aff_lex_lt_map_on_space);
7095 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7096 * where the function value of "mpa1" is lexicographically greater than that
7097 * of "mpa2". "space" is the space of the result.
7098 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7100 * "mpa1" is greater than "mpa2" if its i-th element is greater
7101 * than the i-th element of "mpa2" while all previous elements are
7102 * pairwise equal.
7104 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7105 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7106 __isl_take isl_space *space)
7108 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7109 &isl_pw_aff_gt_map, space);
7112 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7113 * where the function value of "mpa1" is lexicographically greater than that
7114 * of "mpa2".
7116 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7117 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7119 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7120 &isl_multi_pw_aff_lex_gt_map_on_space);
7123 /* Compare two isl_affs.
7125 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7126 * than "aff2" and 0 if they are equal.
7128 * The order is fairly arbitrary. We do consider expressions that only involve
7129 * earlier dimensions as "smaller".
7131 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7133 int cmp;
7134 int last1, last2;
7136 if (aff1 == aff2)
7137 return 0;
7139 if (!aff1)
7140 return -1;
7141 if (!aff2)
7142 return 1;
7144 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7145 if (cmp != 0)
7146 return cmp;
7148 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7149 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7150 if (last1 != last2)
7151 return last1 - last2;
7153 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7156 /* Compare two isl_pw_affs.
7158 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7159 * than "pa2" and 0 if they are equal.
7161 * The order is fairly arbitrary. We do consider expressions that only involve
7162 * earlier dimensions as "smaller".
7164 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7165 __isl_keep isl_pw_aff *pa2)
7167 int i;
7168 int cmp;
7170 if (pa1 == pa2)
7171 return 0;
7173 if (!pa1)
7174 return -1;
7175 if (!pa2)
7176 return 1;
7178 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7179 if (cmp != 0)
7180 return cmp;
7182 if (pa1->n != pa2->n)
7183 return pa1->n - pa2->n;
7185 for (i = 0; i < pa1->n; ++i) {
7186 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7187 if (cmp != 0)
7188 return cmp;
7189 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7190 if (cmp != 0)
7191 return cmp;
7194 return 0;
7197 /* Return a piecewise affine expression that is equal to "v" on "domain".
7199 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7200 __isl_take isl_val *v)
7202 isl_space *space;
7203 isl_local_space *ls;
7204 isl_aff *aff;
7206 space = isl_set_get_space(domain);
7207 ls = isl_local_space_from_space(space);
7208 aff = isl_aff_val_on_domain(ls, v);
7210 return isl_pw_aff_alloc(domain, aff);
7213 /* Return a multi affine expression that is equal to "mv" on domain
7214 * space "space".
7216 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7217 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7219 int i, n;
7220 isl_space *space2;
7221 isl_local_space *ls;
7222 isl_multi_aff *ma;
7224 if (!space || !mv)
7225 goto error;
7227 n = isl_multi_val_dim(mv, isl_dim_set);
7228 space2 = isl_multi_val_get_space(mv);
7229 space2 = isl_space_align_params(space2, isl_space_copy(space));
7230 space = isl_space_align_params(space, isl_space_copy(space2));
7231 space = isl_space_map_from_domain_and_range(space, space2);
7232 ma = isl_multi_aff_alloc(isl_space_copy(space));
7233 ls = isl_local_space_from_space(isl_space_domain(space));
7234 for (i = 0; i < n; ++i) {
7235 isl_val *v;
7236 isl_aff *aff;
7238 v = isl_multi_val_get_val(mv, i);
7239 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7240 ma = isl_multi_aff_set_aff(ma, i, aff);
7242 isl_local_space_free(ls);
7244 isl_multi_val_free(mv);
7245 return ma;
7246 error:
7247 isl_space_free(space);
7248 isl_multi_val_free(mv);
7249 return NULL;
7252 /* Return a piecewise multi-affine expression
7253 * that is equal to "mv" on "domain".
7255 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7256 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7258 isl_space *space;
7259 isl_multi_aff *ma;
7261 space = isl_set_get_space(domain);
7262 ma = isl_multi_aff_multi_val_on_space(space, mv);
7264 return isl_pw_multi_aff_alloc(domain, ma);
7267 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7268 * mv is the value that should be attained on each domain set
7269 * res collects the results
7271 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7272 isl_multi_val *mv;
7273 isl_union_pw_multi_aff *res;
7276 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7277 * and add it to data->res.
7279 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7280 void *user)
7282 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7283 isl_pw_multi_aff *pma;
7284 isl_multi_val *mv;
7286 mv = isl_multi_val_copy(data->mv);
7287 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7288 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7290 return data->res ? isl_stat_ok : isl_stat_error;
7293 /* Return a union piecewise multi-affine expression
7294 * that is equal to "mv" on "domain".
7296 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7297 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7299 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7300 isl_space *space;
7302 space = isl_union_set_get_space(domain);
7303 data.res = isl_union_pw_multi_aff_empty(space);
7304 data.mv = mv;
7305 if (isl_union_set_foreach_set(domain,
7306 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7307 data.res = isl_union_pw_multi_aff_free(data.res);
7308 isl_union_set_free(domain);
7309 isl_multi_val_free(mv);
7310 return data.res;
7313 /* Compute the pullback of data->pma by the function represented by "pma2",
7314 * provided the spaces match, and add the results to data->res.
7316 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7318 struct isl_union_pw_multi_aff_bin_data *data = user;
7320 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7321 pma2->dim, isl_dim_out)) {
7322 isl_pw_multi_aff_free(pma2);
7323 return isl_stat_ok;
7326 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7327 isl_pw_multi_aff_copy(data->pma), pma2);
7329 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7330 if (!data->res)
7331 return isl_stat_error;
7333 return isl_stat_ok;
7336 /* Compute the pullback of "upma1" by the function represented by "upma2".
7338 __isl_give isl_union_pw_multi_aff *
7339 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7340 __isl_take isl_union_pw_multi_aff *upma1,
7341 __isl_take isl_union_pw_multi_aff *upma2)
7343 return bin_op(upma1, upma2, &pullback_entry);
7346 /* Check that the domain space of "upa" matches "space".
7348 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7349 * can in principle never fail since the space "space" is that
7350 * of the isl_multi_union_pw_aff and is a set space such that
7351 * there is no domain space to match.
7353 * We check the parameters and double-check that "space" is
7354 * indeed that of a set.
7356 static isl_stat isl_union_pw_aff_check_match_domain_space(
7357 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7359 isl_space *upa_space;
7360 isl_bool match;
7362 if (!upa || !space)
7363 return isl_stat_error;
7365 match = isl_space_is_set(space);
7366 if (match < 0)
7367 return isl_stat_error;
7368 if (!match)
7369 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7370 "expecting set space", return isl_stat_error);
7372 upa_space = isl_union_pw_aff_get_space(upa);
7373 match = isl_space_has_equal_params(space, upa_space);
7374 if (match < 0)
7375 goto error;
7376 if (!match)
7377 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7378 "parameters don't match", goto error);
7380 isl_space_free(upa_space);
7381 return isl_stat_ok;
7382 error:
7383 isl_space_free(upa_space);
7384 return isl_stat_error;
7387 /* Do the parameters of "upa" match those of "space"?
7389 static isl_bool isl_union_pw_aff_matching_params(
7390 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7392 isl_space *upa_space;
7393 isl_bool match;
7395 if (!upa || !space)
7396 return isl_bool_error;
7398 upa_space = isl_union_pw_aff_get_space(upa);
7400 match = isl_space_has_equal_params(space, upa_space);
7402 isl_space_free(upa_space);
7403 return match;
7406 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7407 * space represents the new parameters.
7408 * res collects the results.
7410 struct isl_union_pw_aff_reset_params_data {
7411 isl_space *space;
7412 isl_union_pw_aff *res;
7415 /* Replace the parameters of "pa" by data->space and
7416 * add the result to data->res.
7418 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7420 struct isl_union_pw_aff_reset_params_data *data = user;
7421 isl_space *space;
7423 space = isl_pw_aff_get_space(pa);
7424 space = isl_space_replace_params(space, data->space);
7425 pa = isl_pw_aff_reset_space(pa, space);
7426 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7428 return data->res ? isl_stat_ok : isl_stat_error;
7431 /* Replace the domain space of "upa" by "space".
7432 * Since a union expression does not have a (single) domain space,
7433 * "space" is necessarily a parameter space.
7435 * Since the order and the names of the parameters determine
7436 * the hash value, we need to create a new hash table.
7438 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7439 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7441 struct isl_union_pw_aff_reset_params_data data = { space };
7442 isl_bool match;
7444 match = isl_union_pw_aff_matching_params(upa, space);
7445 if (match < 0)
7446 upa = isl_union_pw_aff_free(upa);
7447 else if (match) {
7448 isl_space_free(space);
7449 return upa;
7452 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7453 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7454 data.res = isl_union_pw_aff_free(data.res);
7456 isl_union_pw_aff_free(upa);
7457 isl_space_free(space);
7458 return data.res;
7461 /* Return the floor of "pa".
7463 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7465 return isl_pw_aff_floor(pa);
7468 /* Given f, return floor(f).
7470 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7471 __isl_take isl_union_pw_aff *upa)
7473 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7476 /* Compute
7478 * upa mod m = upa - m * floor(upa/m)
7480 * with m an integer value.
7482 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7483 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7485 isl_union_pw_aff *res;
7487 if (!upa || !m)
7488 goto error;
7490 if (!isl_val_is_int(m))
7491 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7492 "expecting integer modulo", goto error);
7493 if (!isl_val_is_pos(m))
7494 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7495 "expecting positive modulo", goto error);
7497 res = isl_union_pw_aff_copy(upa);
7498 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7499 upa = isl_union_pw_aff_floor(upa);
7500 upa = isl_union_pw_aff_scale_val(upa, m);
7501 res = isl_union_pw_aff_sub(res, upa);
7503 return res;
7504 error:
7505 isl_val_free(m);
7506 isl_union_pw_aff_free(upa);
7507 return NULL;
7510 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7511 * pos is the output position that needs to be extracted.
7512 * res collects the results.
7514 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7515 int pos;
7516 isl_union_pw_aff *res;
7519 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7520 * (assuming it has such a dimension) and add it to data->res.
7522 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7524 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7525 int n_out;
7526 isl_pw_aff *pa;
7528 if (!pma)
7529 return isl_stat_error;
7531 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7532 if (data->pos >= n_out) {
7533 isl_pw_multi_aff_free(pma);
7534 return isl_stat_ok;
7537 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7538 isl_pw_multi_aff_free(pma);
7540 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7542 return data->res ? isl_stat_ok : isl_stat_error;
7545 /* Extract an isl_union_pw_aff corresponding to
7546 * output dimension "pos" of "upma".
7548 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7549 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7551 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7552 isl_space *space;
7554 if (!upma)
7555 return NULL;
7557 if (pos < 0)
7558 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7559 "cannot extract at negative position", return NULL);
7561 space = isl_union_pw_multi_aff_get_space(upma);
7562 data.res = isl_union_pw_aff_empty(space);
7563 data.pos = pos;
7564 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7565 &get_union_pw_aff, &data) < 0)
7566 data.res = isl_union_pw_aff_free(data.res);
7568 return data.res;
7571 /* Return a union piecewise affine expression
7572 * that is equal to "aff" on "domain".
7574 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7575 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7577 isl_pw_aff *pa;
7579 pa = isl_pw_aff_from_aff(aff);
7580 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7583 /* Return a union piecewise affine expression
7584 * that is equal to the parameter identified by "id" on "domain".
7586 * Make sure the parameter appears in the space passed to
7587 * isl_aff_param_on_domain_space_id.
7589 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7590 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7592 isl_space *space;
7593 isl_aff *aff;
7595 space = isl_union_set_get_space(domain);
7596 space = isl_space_add_param_id(space, isl_id_copy(id));
7597 aff = isl_aff_param_on_domain_space_id(space, id);
7598 return isl_union_pw_aff_aff_on_domain(domain, aff);
7601 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7602 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7603 * needs to attain.
7604 * "res" collects the results.
7606 struct isl_union_pw_aff_pw_aff_on_domain_data {
7607 isl_pw_aff *pa;
7608 isl_union_pw_aff *res;
7611 /* Construct a piecewise affine expression that is equal to data->pa
7612 * on "domain" and add the result to data->res.
7614 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7616 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7617 isl_pw_aff *pa;
7618 int dim;
7620 pa = isl_pw_aff_copy(data->pa);
7621 dim = isl_set_dim(domain, isl_dim_set);
7622 pa = isl_pw_aff_from_range(pa);
7623 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7624 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7625 pa = isl_pw_aff_intersect_domain(pa, domain);
7626 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7628 return data->res ? isl_stat_ok : isl_stat_error;
7631 /* Return a union piecewise affine expression
7632 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7633 * have been aligned.
7635 * Construct an isl_pw_aff on each of the sets in "domain" and
7636 * collect the results.
7638 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7639 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7641 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7642 isl_space *space;
7644 space = isl_union_set_get_space(domain);
7645 data.res = isl_union_pw_aff_empty(space);
7646 data.pa = pa;
7647 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7648 data.res = isl_union_pw_aff_free(data.res);
7649 isl_union_set_free(domain);
7650 isl_pw_aff_free(pa);
7651 return data.res;
7654 /* Return a union piecewise affine expression
7655 * that is equal to "pa" on "domain".
7657 * Check that "pa" is a parametric expression,
7658 * align the parameters if needed and call
7659 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7661 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7662 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7664 isl_bool is_set;
7665 isl_bool equal_params;
7666 isl_space *domain_space, *pa_space;
7668 pa_space = isl_pw_aff_peek_space(pa);
7669 is_set = isl_space_is_set(pa_space);
7670 if (is_set < 0)
7671 goto error;
7672 if (!is_set)
7673 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7674 "expecting parametric expression", goto error);
7676 domain_space = isl_union_set_get_space(domain);
7677 pa_space = isl_pw_aff_get_space(pa);
7678 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7679 if (equal_params >= 0 && !equal_params) {
7680 isl_space *space;
7682 space = isl_space_align_params(domain_space, pa_space);
7683 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7684 domain = isl_union_set_align_params(domain, space);
7685 } else {
7686 isl_space_free(domain_space);
7687 isl_space_free(pa_space);
7690 if (equal_params < 0)
7691 goto error;
7692 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7693 error:
7694 isl_union_set_free(domain);
7695 isl_pw_aff_free(pa);
7696 return NULL;
7699 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7700 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7701 * "res" collects the results.
7703 struct isl_union_pw_aff_val_on_domain_data {
7704 isl_val *v;
7705 isl_union_pw_aff *res;
7708 /* Construct a piecewise affine expression that is equal to data->v
7709 * on "domain" and add the result to data->res.
7711 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7713 struct isl_union_pw_aff_val_on_domain_data *data = user;
7714 isl_pw_aff *pa;
7715 isl_val *v;
7717 v = isl_val_copy(data->v);
7718 pa = isl_pw_aff_val_on_domain(domain, v);
7719 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7721 return data->res ? isl_stat_ok : isl_stat_error;
7724 /* Return a union piecewise affine expression
7725 * that is equal to "v" on "domain".
7727 * Construct an isl_pw_aff on each of the sets in "domain" and
7728 * collect the results.
7730 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7731 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7733 struct isl_union_pw_aff_val_on_domain_data data;
7734 isl_space *space;
7736 space = isl_union_set_get_space(domain);
7737 data.res = isl_union_pw_aff_empty(space);
7738 data.v = v;
7739 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7740 data.res = isl_union_pw_aff_free(data.res);
7741 isl_union_set_free(domain);
7742 isl_val_free(v);
7743 return data.res;
7746 /* Construct a piecewise multi affine expression
7747 * that is equal to "pa" and add it to upma.
7749 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7750 void *user)
7752 isl_union_pw_multi_aff **upma = user;
7753 isl_pw_multi_aff *pma;
7755 pma = isl_pw_multi_aff_from_pw_aff(pa);
7756 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7758 return *upma ? isl_stat_ok : isl_stat_error;
7761 /* Construct and return a union piecewise multi affine expression
7762 * that is equal to the given union piecewise affine expression.
7764 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7765 __isl_take isl_union_pw_aff *upa)
7767 isl_space *space;
7768 isl_union_pw_multi_aff *upma;
7770 if (!upa)
7771 return NULL;
7773 space = isl_union_pw_aff_get_space(upa);
7774 upma = isl_union_pw_multi_aff_empty(space);
7776 if (isl_union_pw_aff_foreach_pw_aff(upa,
7777 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7778 upma = isl_union_pw_multi_aff_free(upma);
7780 isl_union_pw_aff_free(upa);
7781 return upma;
7784 /* Compute the set of elements in the domain of "pa" where it is zero and
7785 * add this set to "uset".
7787 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7789 isl_union_set **uset = (isl_union_set **)user;
7791 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7793 return *uset ? isl_stat_ok : isl_stat_error;
7796 /* Return a union set containing those elements in the domain
7797 * of "upa" where it is zero.
7799 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7800 __isl_take isl_union_pw_aff *upa)
7802 isl_union_set *zero;
7804 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7805 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7806 zero = isl_union_set_free(zero);
7808 isl_union_pw_aff_free(upa);
7809 return zero;
7812 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7813 * upma is the function that is plugged in.
7814 * pa is the current part of the function in which upma is plugged in.
7815 * res collects the results.
7817 struct isl_union_pw_aff_pullback_upma_data {
7818 isl_union_pw_multi_aff *upma;
7819 isl_pw_aff *pa;
7820 isl_union_pw_aff *res;
7823 /* Check if "pma" can be plugged into data->pa.
7824 * If so, perform the pullback and add the result to data->res.
7826 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7828 struct isl_union_pw_aff_pullback_upma_data *data = user;
7829 isl_pw_aff *pa;
7831 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7832 pma->dim, isl_dim_out)) {
7833 isl_pw_multi_aff_free(pma);
7834 return isl_stat_ok;
7837 pa = isl_pw_aff_copy(data->pa);
7838 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7840 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7842 return data->res ? isl_stat_ok : isl_stat_error;
7845 /* Check if any of the elements of data->upma can be plugged into pa,
7846 * add if so add the result to data->res.
7848 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7850 struct isl_union_pw_aff_pullback_upma_data *data = user;
7851 isl_stat r;
7853 data->pa = pa;
7854 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7855 &pa_pb_pma, data);
7856 isl_pw_aff_free(pa);
7858 return r;
7861 /* Compute the pullback of "upa" by the function represented by "upma".
7862 * In other words, plug in "upma" in "upa". The result contains
7863 * expressions defined over the domain space of "upma".
7865 * Run over all pairs of elements in "upa" and "upma", perform
7866 * the pullback when appropriate and collect the results.
7867 * If the hash value were based on the domain space rather than
7868 * the function space, then we could run through all elements
7869 * of "upma" and directly pick out the corresponding element of "upa".
7871 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7872 __isl_take isl_union_pw_aff *upa,
7873 __isl_take isl_union_pw_multi_aff *upma)
7875 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7876 isl_space *space;
7878 space = isl_union_pw_multi_aff_get_space(upma);
7879 upa = isl_union_pw_aff_align_params(upa, space);
7880 space = isl_union_pw_aff_get_space(upa);
7881 upma = isl_union_pw_multi_aff_align_params(upma, space);
7883 if (!upa || !upma)
7884 goto error;
7886 data.upma = upma;
7887 data.res = isl_union_pw_aff_alloc_same_size(upa);
7888 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7889 data.res = isl_union_pw_aff_free(data.res);
7891 isl_union_pw_aff_free(upa);
7892 isl_union_pw_multi_aff_free(upma);
7893 return data.res;
7894 error:
7895 isl_union_pw_aff_free(upa);
7896 isl_union_pw_multi_aff_free(upma);
7897 return NULL;
7900 #undef BASE
7901 #define BASE union_pw_aff
7902 #undef DOMBASE
7903 #define DOMBASE union_set
7905 #include <isl_multi_explicit_domain.c>
7906 #include <isl_multi_union_pw_aff_explicit_domain.c>
7907 #include <isl_multi_templ.c>
7908 #include <isl_multi_apply_set.c>
7909 #include <isl_multi_apply_union_set.c>
7910 #include <isl_multi_coalesce.c>
7911 #include <isl_multi_floor.c>
7912 #include <isl_multi_from_base_templ.c>
7913 #include <isl_multi_gist.c>
7914 #include <isl_multi_align_set.c>
7915 #include <isl_multi_align_union_set.c>
7916 #include <isl_multi_intersect.c>
7918 /* Does "mupa" have a non-trivial explicit domain?
7920 * The explicit domain, if present, is trivial if it represents
7921 * an (obviously) universe parameter set.
7923 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
7924 __isl_keep isl_multi_union_pw_aff *mupa)
7926 isl_bool is_params, trivial;
7927 isl_set *set;
7929 if (!mupa)
7930 return isl_bool_error;
7931 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
7932 return isl_bool_false;
7933 is_params = isl_union_set_is_params(mupa->u.dom);
7934 if (is_params < 0 || !is_params)
7935 return isl_bool_not(is_params);
7936 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
7937 trivial = isl_set_plain_is_universe(set);
7938 isl_set_free(set);
7939 return isl_bool_not(trivial);
7942 /* Construct a multiple union piecewise affine expression
7943 * in the given space with value zero in each of the output dimensions.
7945 * Since there is no canonical zero value for
7946 * a union piecewise affine expression, we can only construct
7947 * a zero-dimensional "zero" value.
7949 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7950 __isl_take isl_space *space)
7952 isl_bool params;
7954 if (!space)
7955 return NULL;
7957 params = isl_space_is_params(space);
7958 if (params < 0)
7959 goto error;
7960 if (params)
7961 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7962 "expecting proper set space", goto error);
7963 if (!isl_space_is_set(space))
7964 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7965 "expecting set space", goto error);
7966 if (isl_space_dim(space , isl_dim_out) != 0)
7967 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7968 "expecting 0D space", goto error);
7970 return isl_multi_union_pw_aff_alloc(space);
7971 error:
7972 isl_space_free(space);
7973 return NULL;
7976 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7977 * with the actual sum on the shared domain and
7978 * the defined expression on the symmetric difference of the domains.
7980 * We simply iterate over the elements in both arguments and
7981 * call isl_union_pw_aff_union_add on each of them, if there is
7982 * at least one element.
7984 * Otherwise, the two expressions have an explicit domain and
7985 * the union of these explicit domains is computed.
7986 * This assumes that the explicit domains are either both in terms
7987 * of specific domains elements or both in terms of parameters.
7988 * However, if one of the expressions does not have any constraints
7989 * on its explicit domain, then this is allowed as well and the result
7990 * is the expression with no constraints on its explicit domain.
7992 static __isl_give isl_multi_union_pw_aff *
7993 isl_multi_union_pw_aff_union_add_aligned(
7994 __isl_take isl_multi_union_pw_aff *mupa1,
7995 __isl_take isl_multi_union_pw_aff *mupa2)
7997 isl_bool has_domain, is_params1, is_params2;
7999 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8000 goto error;
8001 if (mupa1->n > 0)
8002 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8003 &isl_union_pw_aff_union_add);
8004 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8005 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8006 goto error;
8008 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8009 if (has_domain < 0)
8010 goto error;
8011 if (!has_domain) {
8012 isl_multi_union_pw_aff_free(mupa2);
8013 return mupa1;
8015 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8016 if (has_domain < 0)
8017 goto error;
8018 if (!has_domain) {
8019 isl_multi_union_pw_aff_free(mupa1);
8020 return mupa2;
8023 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8024 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8025 if (is_params1 < 0 || is_params2 < 0)
8026 goto error;
8027 if (is_params1 != is_params2)
8028 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8029 isl_error_invalid,
8030 "cannot compute union of concrete domain and "
8031 "parameter constraints", goto error);
8032 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8033 if (!mupa1)
8034 goto error;
8035 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8036 isl_union_set_copy(mupa2->u.dom));
8037 if (!mupa1->u.dom)
8038 goto error;
8039 isl_multi_union_pw_aff_free(mupa2);
8040 return mupa1;
8041 error:
8042 isl_multi_union_pw_aff_free(mupa1);
8043 isl_multi_union_pw_aff_free(mupa2);
8044 return NULL;
8047 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8048 * with the actual sum on the shared domain and
8049 * the defined expression on the symmetric difference of the domains.
8051 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8052 __isl_take isl_multi_union_pw_aff *mupa1,
8053 __isl_take isl_multi_union_pw_aff *mupa2)
8055 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8056 &isl_multi_union_pw_aff_union_add_aligned);
8059 /* Construct and return a multi union piecewise affine expression
8060 * that is equal to the given multi affine expression.
8062 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8063 __isl_take isl_multi_aff *ma)
8065 isl_multi_pw_aff *mpa;
8067 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8068 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8071 /* Construct and return a multi union piecewise affine expression
8072 * that is equal to the given multi piecewise affine expression.
8074 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8075 __isl_take isl_multi_pw_aff *mpa)
8077 int i, n;
8078 isl_space *space;
8079 isl_multi_union_pw_aff *mupa;
8081 if (!mpa)
8082 return NULL;
8084 space = isl_multi_pw_aff_get_space(mpa);
8085 space = isl_space_range(space);
8086 mupa = isl_multi_union_pw_aff_alloc(space);
8088 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8089 for (i = 0; i < n; ++i) {
8090 isl_pw_aff *pa;
8091 isl_union_pw_aff *upa;
8093 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8094 upa = isl_union_pw_aff_from_pw_aff(pa);
8095 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8098 isl_multi_pw_aff_free(mpa);
8100 return mupa;
8103 /* Extract the range space of "pma" and assign it to *space.
8104 * If *space has already been set (through a previous call to this function),
8105 * then check that the range space is the same.
8107 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8109 isl_space **space = user;
8110 isl_space *pma_space;
8111 isl_bool equal;
8113 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8114 isl_pw_multi_aff_free(pma);
8116 if (!pma_space)
8117 return isl_stat_error;
8118 if (!*space) {
8119 *space = pma_space;
8120 return isl_stat_ok;
8123 equal = isl_space_is_equal(pma_space, *space);
8124 isl_space_free(pma_space);
8126 if (equal < 0)
8127 return isl_stat_error;
8128 if (!equal)
8129 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8130 "range spaces not the same", return isl_stat_error);
8131 return isl_stat_ok;
8134 /* Construct and return a multi union piecewise affine expression
8135 * that is equal to the given union piecewise multi affine expression.
8137 * In order to be able to perform the conversion, the input
8138 * needs to be non-empty and may only involve a single range space.
8140 * If the resulting multi union piecewise affine expression has
8141 * an explicit domain, then assign it the domain of the input.
8142 * In other cases, the domain is stored in the individual elements.
8144 __isl_give isl_multi_union_pw_aff *
8145 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8146 __isl_take isl_union_pw_multi_aff *upma)
8148 isl_space *space = NULL;
8149 isl_multi_union_pw_aff *mupa;
8150 int i, n;
8152 if (!upma)
8153 return NULL;
8154 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8155 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8156 "cannot extract range space from empty input",
8157 goto error);
8158 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8159 &space) < 0)
8160 goto error;
8162 if (!space)
8163 goto error;
8165 n = isl_space_dim(space, isl_dim_set);
8166 mupa = isl_multi_union_pw_aff_alloc(space);
8168 for (i = 0; i < n; ++i) {
8169 isl_union_pw_aff *upa;
8171 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8172 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8174 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8175 isl_union_set *dom;
8176 isl_union_pw_multi_aff *copy;
8178 copy = isl_union_pw_multi_aff_copy(upma);
8179 dom = isl_union_pw_multi_aff_domain(copy);
8180 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8183 isl_union_pw_multi_aff_free(upma);
8184 return mupa;
8185 error:
8186 isl_space_free(space);
8187 isl_union_pw_multi_aff_free(upma);
8188 return NULL;
8191 /* Try and create an isl_multi_union_pw_aff that is equivalent
8192 * to the given isl_union_map.
8193 * The isl_union_map is required to be single-valued in each space.
8194 * Moreover, it cannot be empty and all range spaces need to be the same.
8195 * Otherwise, an error is produced.
8197 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8198 __isl_take isl_union_map *umap)
8200 isl_union_pw_multi_aff *upma;
8202 upma = isl_union_pw_multi_aff_from_union_map(umap);
8203 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8206 /* Return a multiple union piecewise affine expression
8207 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8208 * have been aligned.
8210 * If the resulting multi union piecewise affine expression has
8211 * an explicit domain, then assign it the input domain.
8212 * In other cases, the domain is stored in the individual elements.
8214 static __isl_give isl_multi_union_pw_aff *
8215 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8216 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8218 int i, n;
8219 isl_space *space;
8220 isl_multi_union_pw_aff *mupa;
8222 if (!domain || !mv)
8223 goto error;
8225 n = isl_multi_val_dim(mv, isl_dim_set);
8226 space = isl_multi_val_get_space(mv);
8227 mupa = isl_multi_union_pw_aff_alloc(space);
8228 for (i = 0; i < n; ++i) {
8229 isl_val *v;
8230 isl_union_pw_aff *upa;
8232 v = isl_multi_val_get_val(mv, i);
8233 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8235 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8237 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8238 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8239 isl_union_set_copy(domain));
8241 isl_union_set_free(domain);
8242 isl_multi_val_free(mv);
8243 return mupa;
8244 error:
8245 isl_union_set_free(domain);
8246 isl_multi_val_free(mv);
8247 return NULL;
8250 /* Return a multiple union piecewise affine expression
8251 * that is equal to "mv" on "domain".
8253 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8254 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8256 isl_bool equal_params;
8258 if (!domain || !mv)
8259 goto error;
8260 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8261 if (equal_params < 0)
8262 goto error;
8263 if (equal_params)
8264 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8265 domain, mv);
8266 domain = isl_union_set_align_params(domain,
8267 isl_multi_val_get_space(mv));
8268 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8269 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8270 error:
8271 isl_union_set_free(domain);
8272 isl_multi_val_free(mv);
8273 return NULL;
8276 /* Return a multiple union piecewise affine expression
8277 * that is equal to "ma" on "domain".
8279 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8280 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8282 isl_pw_multi_aff *pma;
8284 pma = isl_pw_multi_aff_from_multi_aff(ma);
8285 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8288 /* Return a multiple union piecewise affine expression
8289 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8290 * have been aligned.
8292 * If the resulting multi union piecewise affine expression has
8293 * an explicit domain, then assign it the input domain.
8294 * In other cases, the domain is stored in the individual elements.
8296 static __isl_give isl_multi_union_pw_aff *
8297 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8298 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8300 int i, n;
8301 isl_space *space;
8302 isl_multi_union_pw_aff *mupa;
8304 if (!domain || !pma)
8305 goto error;
8307 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8308 space = isl_pw_multi_aff_get_space(pma);
8309 mupa = isl_multi_union_pw_aff_alloc(space);
8310 for (i = 0; i < n; ++i) {
8311 isl_pw_aff *pa;
8312 isl_union_pw_aff *upa;
8314 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8315 upa = isl_union_pw_aff_pw_aff_on_domain(
8316 isl_union_set_copy(domain), pa);
8317 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8319 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8320 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8321 isl_union_set_copy(domain));
8323 isl_union_set_free(domain);
8324 isl_pw_multi_aff_free(pma);
8325 return mupa;
8326 error:
8327 isl_union_set_free(domain);
8328 isl_pw_multi_aff_free(pma);
8329 return NULL;
8332 /* Return a multiple union piecewise affine expression
8333 * that is equal to "pma" on "domain".
8335 __isl_give isl_multi_union_pw_aff *
8336 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8337 __isl_take isl_pw_multi_aff *pma)
8339 isl_bool equal_params;
8340 isl_space *space;
8342 space = isl_pw_multi_aff_peek_space(pma);
8343 equal_params = isl_union_set_space_has_equal_params(domain, space);
8344 if (equal_params < 0)
8345 goto error;
8346 if (equal_params)
8347 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8348 domain, pma);
8349 domain = isl_union_set_align_params(domain,
8350 isl_pw_multi_aff_get_space(pma));
8351 pma = isl_pw_multi_aff_align_params(pma,
8352 isl_union_set_get_space(domain));
8353 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8354 pma);
8355 error:
8356 isl_union_set_free(domain);
8357 isl_pw_multi_aff_free(pma);
8358 return NULL;
8361 /* Return a union set containing those elements in the domains
8362 * of the elements of "mupa" where they are all zero.
8364 * If there are no elements, then simply return the entire domain.
8366 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8367 __isl_take isl_multi_union_pw_aff *mupa)
8369 int i, n;
8370 isl_union_pw_aff *upa;
8371 isl_union_set *zero;
8373 if (!mupa)
8374 return NULL;
8376 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8377 if (n == 0)
8378 return isl_multi_union_pw_aff_domain(mupa);
8380 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8381 zero = isl_union_pw_aff_zero_union_set(upa);
8383 for (i = 1; i < n; ++i) {
8384 isl_union_set *zero_i;
8386 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8387 zero_i = isl_union_pw_aff_zero_union_set(upa);
8389 zero = isl_union_set_intersect(zero, zero_i);
8392 isl_multi_union_pw_aff_free(mupa);
8393 return zero;
8396 /* Construct a union map mapping the shared domain
8397 * of the union piecewise affine expressions to the range of "mupa"
8398 * in the special case of a 0D multi union piecewise affine expression.
8400 * Construct a map between the explicit domain of "mupa" and
8401 * the range space.
8402 * Note that this assumes that the domain consists of explicit elements.
8404 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8405 __isl_take isl_multi_union_pw_aff *mupa)
8407 isl_bool is_params;
8408 isl_space *space;
8409 isl_union_set *dom, *ran;
8411 space = isl_multi_union_pw_aff_get_space(mupa);
8412 dom = isl_multi_union_pw_aff_domain(mupa);
8413 ran = isl_union_set_from_set(isl_set_universe(space));
8415 is_params = isl_union_set_is_params(dom);
8416 if (is_params < 0)
8417 dom = isl_union_set_free(dom);
8418 else if (is_params)
8419 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8420 "cannot create union map from expression without "
8421 "explicit domain elements",
8422 dom = isl_union_set_free(dom));
8424 return isl_union_map_from_domain_and_range(dom, ran);
8427 /* Construct a union map mapping the shared domain
8428 * of the union piecewise affine expressions to the range of "mupa"
8429 * with each dimension in the range equated to the
8430 * corresponding union piecewise affine expression.
8432 * If the input is zero-dimensional, then construct a mapping
8433 * from its explicit domain.
8435 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8436 __isl_take isl_multi_union_pw_aff *mupa)
8438 int i, n;
8439 isl_space *space;
8440 isl_union_map *umap;
8441 isl_union_pw_aff *upa;
8443 if (!mupa)
8444 return NULL;
8446 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8447 if (n == 0)
8448 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8450 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8451 umap = isl_union_map_from_union_pw_aff(upa);
8453 for (i = 1; i < n; ++i) {
8454 isl_union_map *umap_i;
8456 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8457 umap_i = isl_union_map_from_union_pw_aff(upa);
8458 umap = isl_union_map_flat_range_product(umap, umap_i);
8461 space = isl_multi_union_pw_aff_get_space(mupa);
8462 umap = isl_union_map_reset_range_space(umap, space);
8464 isl_multi_union_pw_aff_free(mupa);
8465 return umap;
8468 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8469 * "range" is the space from which to set the range space.
8470 * "res" collects the results.
8472 struct isl_union_pw_multi_aff_reset_range_space_data {
8473 isl_space *range;
8474 isl_union_pw_multi_aff *res;
8477 /* Replace the range space of "pma" by the range space of data->range and
8478 * add the result to data->res.
8480 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8482 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8483 isl_space *space;
8485 space = isl_pw_multi_aff_get_space(pma);
8486 space = isl_space_domain(space);
8487 space = isl_space_extend_domain_with_range(space,
8488 isl_space_copy(data->range));
8489 pma = isl_pw_multi_aff_reset_space(pma, space);
8490 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8492 return data->res ? isl_stat_ok : isl_stat_error;
8495 /* Replace the range space of all the piecewise affine expressions in "upma" by
8496 * the range space of "space".
8498 * This assumes that all these expressions have the same output dimension.
8500 * Since the spaces of the expressions change, so do their hash values.
8501 * We therefore need to create a new isl_union_pw_multi_aff.
8502 * Note that the hash value is currently computed based on the entire
8503 * space even though there can only be a single expression with a given
8504 * domain space.
8506 static __isl_give isl_union_pw_multi_aff *
8507 isl_union_pw_multi_aff_reset_range_space(
8508 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8510 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8511 isl_space *space_upma;
8513 space_upma = isl_union_pw_multi_aff_get_space(upma);
8514 data.res = isl_union_pw_multi_aff_empty(space_upma);
8515 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8516 &reset_range_space, &data) < 0)
8517 data.res = isl_union_pw_multi_aff_free(data.res);
8519 isl_space_free(space);
8520 isl_union_pw_multi_aff_free(upma);
8521 return data.res;
8524 /* Construct and return a union piecewise multi affine expression
8525 * that is equal to the given multi union piecewise affine expression,
8526 * in the special case of a 0D multi union piecewise affine expression.
8528 * Construct a union piecewise multi affine expression
8529 * on top of the explicit domain of the input.
8531 __isl_give isl_union_pw_multi_aff *
8532 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8533 __isl_take isl_multi_union_pw_aff *mupa)
8535 isl_space *space;
8536 isl_multi_val *mv;
8537 isl_union_set *domain;
8539 space = isl_multi_union_pw_aff_get_space(mupa);
8540 mv = isl_multi_val_zero(space);
8541 domain = isl_multi_union_pw_aff_domain(mupa);
8542 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8545 /* Construct and return a union piecewise multi affine expression
8546 * that is equal to the given multi union piecewise affine expression.
8548 * If the input is zero-dimensional, then
8549 * construct a union piecewise multi affine expression
8550 * on top of the explicit domain of the input.
8552 __isl_give isl_union_pw_multi_aff *
8553 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8554 __isl_take isl_multi_union_pw_aff *mupa)
8556 int i, n;
8557 isl_space *space;
8558 isl_union_pw_multi_aff *upma;
8559 isl_union_pw_aff *upa;
8561 if (!mupa)
8562 return NULL;
8564 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8565 if (n == 0)
8566 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8568 space = isl_multi_union_pw_aff_get_space(mupa);
8569 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8570 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8572 for (i = 1; i < n; ++i) {
8573 isl_union_pw_multi_aff *upma_i;
8575 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8576 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8577 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8580 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8582 isl_multi_union_pw_aff_free(mupa);
8583 return upma;
8586 /* Intersect the range of "mupa" with "range",
8587 * in the special case where "mupa" is 0D.
8589 * Intersect the domain of "mupa" with the constraints on the parameters
8590 * of "range".
8592 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8593 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8595 range = isl_set_params(range);
8596 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8597 return mupa;
8600 /* Intersect the range of "mupa" with "range".
8601 * That is, keep only those domain elements that have a function value
8602 * in "range".
8604 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8605 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8607 isl_union_pw_multi_aff *upma;
8608 isl_union_set *domain;
8609 isl_space *space;
8610 int n;
8611 int match;
8613 if (!mupa || !range)
8614 goto error;
8616 space = isl_set_get_space(range);
8617 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8618 space, isl_dim_set);
8619 isl_space_free(space);
8620 if (match < 0)
8621 goto error;
8622 if (!match)
8623 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8624 "space don't match", goto error);
8625 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8626 if (n == 0)
8627 return mupa_intersect_range_0D(mupa, range);
8629 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8630 isl_multi_union_pw_aff_copy(mupa));
8631 domain = isl_union_set_from_set(range);
8632 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8633 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8635 return mupa;
8636 error:
8637 isl_multi_union_pw_aff_free(mupa);
8638 isl_set_free(range);
8639 return NULL;
8642 /* Return the shared domain of the elements of "mupa",
8643 * in the special case where "mupa" is zero-dimensional.
8645 * Return the explicit domain of "mupa".
8646 * Note that this domain may be a parameter set, either
8647 * because "mupa" is meant to live in a set space or
8648 * because no explicit domain has been set.
8650 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8651 __isl_take isl_multi_union_pw_aff *mupa)
8653 isl_union_set *dom;
8655 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8656 isl_multi_union_pw_aff_free(mupa);
8658 return dom;
8661 /* Return the shared domain of the elements of "mupa".
8663 * If "mupa" is zero-dimensional, then return its explicit domain.
8665 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8666 __isl_take isl_multi_union_pw_aff *mupa)
8668 int i, n;
8669 isl_union_pw_aff *upa;
8670 isl_union_set *dom;
8672 if (!mupa)
8673 return NULL;
8675 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8676 if (n == 0)
8677 return isl_multi_union_pw_aff_domain_0D(mupa);
8679 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8680 dom = isl_union_pw_aff_domain(upa);
8681 for (i = 1; i < n; ++i) {
8682 isl_union_set *dom_i;
8684 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8685 dom_i = isl_union_pw_aff_domain(upa);
8686 dom = isl_union_set_intersect(dom, dom_i);
8689 isl_multi_union_pw_aff_free(mupa);
8690 return dom;
8693 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8694 * In particular, the spaces have been aligned.
8695 * The result is defined over the shared domain of the elements of "mupa"
8697 * We first extract the parametric constant part of "aff" and
8698 * define that over the shared domain.
8699 * Then we iterate over all input dimensions of "aff" and add the corresponding
8700 * multiples of the elements of "mupa".
8701 * Finally, we consider the integer divisions, calling the function
8702 * recursively to obtain an isl_union_pw_aff corresponding to the
8703 * integer division argument.
8705 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8706 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8708 int i, n_in, n_div;
8709 isl_union_pw_aff *upa;
8710 isl_union_set *uset;
8711 isl_val *v;
8712 isl_aff *cst;
8714 n_in = isl_aff_dim(aff, isl_dim_in);
8715 n_div = isl_aff_dim(aff, isl_dim_div);
8717 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8718 cst = isl_aff_copy(aff);
8719 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8720 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8721 cst = isl_aff_project_domain_on_params(cst);
8722 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8724 for (i = 0; i < n_in; ++i) {
8725 isl_union_pw_aff *upa_i;
8727 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8728 continue;
8729 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8730 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8731 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8732 upa = isl_union_pw_aff_add(upa, upa_i);
8735 for (i = 0; i < n_div; ++i) {
8736 isl_aff *div;
8737 isl_union_pw_aff *upa_i;
8739 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8740 continue;
8741 div = isl_aff_get_div(aff, i);
8742 upa_i = multi_union_pw_aff_apply_aff(
8743 isl_multi_union_pw_aff_copy(mupa), div);
8744 upa_i = isl_union_pw_aff_floor(upa_i);
8745 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8746 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8747 upa = isl_union_pw_aff_add(upa, upa_i);
8750 isl_multi_union_pw_aff_free(mupa);
8751 isl_aff_free(aff);
8753 return upa;
8756 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8757 * with the domain of "aff".
8758 * Furthermore, the dimension of this space needs to be greater than zero.
8759 * The result is defined over the shared domain of the elements of "mupa"
8761 * We perform these checks and then hand over control to
8762 * multi_union_pw_aff_apply_aff.
8764 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8765 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8767 isl_space *space1, *space2;
8768 isl_bool equal;
8770 mupa = isl_multi_union_pw_aff_align_params(mupa,
8771 isl_aff_get_space(aff));
8772 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8773 if (!mupa || !aff)
8774 goto error;
8776 space1 = isl_multi_union_pw_aff_get_space(mupa);
8777 space2 = isl_aff_get_domain_space(aff);
8778 equal = isl_space_is_equal(space1, space2);
8779 isl_space_free(space1);
8780 isl_space_free(space2);
8781 if (equal < 0)
8782 goto error;
8783 if (!equal)
8784 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8785 "spaces don't match", goto error);
8786 if (isl_aff_dim(aff, isl_dim_in) == 0)
8787 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8788 "cannot determine domains", goto error);
8790 return multi_union_pw_aff_apply_aff(mupa, aff);
8791 error:
8792 isl_multi_union_pw_aff_free(mupa);
8793 isl_aff_free(aff);
8794 return NULL;
8797 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8798 * The space of "mupa" is known to be compatible with the domain of "ma".
8800 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8801 * on the domain of "mupa".
8803 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
8804 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8806 isl_union_set *dom;
8808 dom = isl_multi_union_pw_aff_domain(mupa);
8809 ma = isl_multi_aff_project_domain_on_params(ma);
8811 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
8814 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8815 * with the domain of "ma".
8816 * The result is defined over the shared domain of the elements of "mupa"
8818 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8819 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8821 isl_space *space1, *space2;
8822 isl_multi_union_pw_aff *res;
8823 isl_bool equal;
8824 int i, n_out;
8826 mupa = isl_multi_union_pw_aff_align_params(mupa,
8827 isl_multi_aff_get_space(ma));
8828 ma = isl_multi_aff_align_params(ma,
8829 isl_multi_union_pw_aff_get_space(mupa));
8830 if (!mupa || !ma)
8831 goto error;
8833 space1 = isl_multi_union_pw_aff_get_space(mupa);
8834 space2 = isl_multi_aff_get_domain_space(ma);
8835 equal = isl_space_is_equal(space1, space2);
8836 isl_space_free(space1);
8837 isl_space_free(space2);
8838 if (equal < 0)
8839 goto error;
8840 if (!equal)
8841 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8842 "spaces don't match", goto error);
8843 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8844 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
8845 return mupa_apply_multi_aff_0D(mupa, ma);
8847 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8848 res = isl_multi_union_pw_aff_alloc(space1);
8850 for (i = 0; i < n_out; ++i) {
8851 isl_aff *aff;
8852 isl_union_pw_aff *upa;
8854 aff = isl_multi_aff_get_aff(ma, i);
8855 upa = multi_union_pw_aff_apply_aff(
8856 isl_multi_union_pw_aff_copy(mupa), aff);
8857 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8860 isl_multi_aff_free(ma);
8861 isl_multi_union_pw_aff_free(mupa);
8862 return res;
8863 error:
8864 isl_multi_union_pw_aff_free(mupa);
8865 isl_multi_aff_free(ma);
8866 return NULL;
8869 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
8870 * The space of "mupa" is known to be compatible with the domain of "pa".
8872 * Construct an isl_multi_union_pw_aff that is equal to "pa"
8873 * on the domain of "mupa".
8875 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
8876 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8878 isl_union_set *dom;
8880 dom = isl_multi_union_pw_aff_domain(mupa);
8881 pa = isl_pw_aff_project_domain_on_params(pa);
8883 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
8886 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8887 * with the domain of "pa".
8888 * Furthermore, the dimension of this space needs to be greater than zero.
8889 * The result is defined over the shared domain of the elements of "mupa"
8891 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8892 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8894 int i;
8895 isl_bool equal;
8896 isl_space *space, *space2;
8897 isl_union_pw_aff *upa;
8899 mupa = isl_multi_union_pw_aff_align_params(mupa,
8900 isl_pw_aff_get_space(pa));
8901 pa = isl_pw_aff_align_params(pa,
8902 isl_multi_union_pw_aff_get_space(mupa));
8903 if (!mupa || !pa)
8904 goto error;
8906 space = isl_multi_union_pw_aff_get_space(mupa);
8907 space2 = isl_pw_aff_get_domain_space(pa);
8908 equal = isl_space_is_equal(space, space2);
8909 isl_space_free(space);
8910 isl_space_free(space2);
8911 if (equal < 0)
8912 goto error;
8913 if (!equal)
8914 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8915 "spaces don't match", goto error);
8916 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8917 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
8919 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8920 upa = isl_union_pw_aff_empty(space);
8922 for (i = 0; i < pa->n; ++i) {
8923 isl_aff *aff;
8924 isl_set *domain;
8925 isl_multi_union_pw_aff *mupa_i;
8926 isl_union_pw_aff *upa_i;
8928 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8929 domain = isl_set_copy(pa->p[i].set);
8930 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8931 aff = isl_aff_copy(pa->p[i].aff);
8932 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8933 upa = isl_union_pw_aff_union_add(upa, upa_i);
8936 isl_multi_union_pw_aff_free(mupa);
8937 isl_pw_aff_free(pa);
8938 return upa;
8939 error:
8940 isl_multi_union_pw_aff_free(mupa);
8941 isl_pw_aff_free(pa);
8942 return NULL;
8945 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
8946 * The space of "mupa" is known to be compatible with the domain of "pma".
8948 * Construct an isl_multi_union_pw_aff that is equal to "pma"
8949 * on the domain of "mupa".
8951 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
8952 __isl_take isl_multi_union_pw_aff *mupa,
8953 __isl_take isl_pw_multi_aff *pma)
8955 isl_union_set *dom;
8957 dom = isl_multi_union_pw_aff_domain(mupa);
8958 pma = isl_pw_multi_aff_project_domain_on_params(pma);
8960 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
8963 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8964 * with the domain of "pma".
8965 * The result is defined over the shared domain of the elements of "mupa"
8967 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8968 __isl_take isl_multi_union_pw_aff *mupa,
8969 __isl_take isl_pw_multi_aff *pma)
8971 isl_space *space1, *space2;
8972 isl_multi_union_pw_aff *res;
8973 isl_bool equal;
8974 int i, n_out;
8976 mupa = isl_multi_union_pw_aff_align_params(mupa,
8977 isl_pw_multi_aff_get_space(pma));
8978 pma = isl_pw_multi_aff_align_params(pma,
8979 isl_multi_union_pw_aff_get_space(mupa));
8980 if (!mupa || !pma)
8981 goto error;
8983 space1 = isl_multi_union_pw_aff_get_space(mupa);
8984 space2 = isl_pw_multi_aff_get_domain_space(pma);
8985 equal = isl_space_is_equal(space1, space2);
8986 isl_space_free(space1);
8987 isl_space_free(space2);
8988 if (equal < 0)
8989 goto error;
8990 if (!equal)
8991 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8992 "spaces don't match", goto error);
8993 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8994 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
8995 return mupa_apply_pw_multi_aff_0D(mupa, pma);
8997 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8998 res = isl_multi_union_pw_aff_alloc(space1);
9000 for (i = 0; i < n_out; ++i) {
9001 isl_pw_aff *pa;
9002 isl_union_pw_aff *upa;
9004 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9005 upa = isl_multi_union_pw_aff_apply_pw_aff(
9006 isl_multi_union_pw_aff_copy(mupa), pa);
9007 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9010 isl_pw_multi_aff_free(pma);
9011 isl_multi_union_pw_aff_free(mupa);
9012 return res;
9013 error:
9014 isl_multi_union_pw_aff_free(mupa);
9015 isl_pw_multi_aff_free(pma);
9016 return NULL;
9019 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9020 * If the explicit domain only keeps track of constraints on the parameters,
9021 * then only update those constraints.
9023 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9024 __isl_take isl_multi_union_pw_aff *mupa,
9025 __isl_keep isl_union_pw_multi_aff *upma)
9027 isl_bool is_params;
9029 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9030 return isl_multi_union_pw_aff_free(mupa);
9032 mupa = isl_multi_union_pw_aff_cow(mupa);
9033 if (!mupa)
9034 return NULL;
9036 is_params = isl_union_set_is_params(mupa->u.dom);
9037 if (is_params < 0)
9038 return isl_multi_union_pw_aff_free(mupa);
9040 upma = isl_union_pw_multi_aff_copy(upma);
9041 if (is_params)
9042 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9043 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9044 else
9045 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9046 mupa->u.dom, upma);
9047 if (!mupa->u.dom)
9048 return isl_multi_union_pw_aff_free(mupa);
9049 return mupa;
9052 /* Compute the pullback of "mupa" by the function represented by "upma".
9053 * In other words, plug in "upma" in "mupa". The result contains
9054 * expressions defined over the domain space of "upma".
9056 * Run over all elements of "mupa" and plug in "upma" in each of them.
9058 * If "mupa" has an explicit domain, then it is this domain
9059 * that needs to undergo a pullback instead, i.e., a preimage.
9061 __isl_give isl_multi_union_pw_aff *
9062 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9063 __isl_take isl_multi_union_pw_aff *mupa,
9064 __isl_take isl_union_pw_multi_aff *upma)
9066 int i, n;
9068 mupa = isl_multi_union_pw_aff_align_params(mupa,
9069 isl_union_pw_multi_aff_get_space(upma));
9070 upma = isl_union_pw_multi_aff_align_params(upma,
9071 isl_multi_union_pw_aff_get_space(mupa));
9072 mupa = isl_multi_union_pw_aff_cow(mupa);
9073 if (!mupa || !upma)
9074 goto error;
9076 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9077 for (i = 0; i < n; ++i) {
9078 isl_union_pw_aff *upa;
9080 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9081 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9082 isl_union_pw_multi_aff_copy(upma));
9083 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9086 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9087 mupa = preimage_explicit_domain(mupa, upma);
9089 isl_union_pw_multi_aff_free(upma);
9090 return mupa;
9091 error:
9092 isl_multi_union_pw_aff_free(mupa);
9093 isl_union_pw_multi_aff_free(upma);
9094 return NULL;
9097 /* Extract the sequence of elements in "mupa" with domain space "space"
9098 * (ignoring parameters).
9100 * For the elements of "mupa" that are not defined on the specified space,
9101 * the corresponding element in the result is empty.
9103 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9104 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9106 int i, n;
9107 isl_space *space_mpa;
9108 isl_multi_pw_aff *mpa;
9110 if (!mupa || !space)
9111 goto error;
9113 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9114 space = isl_space_replace_params(space, space_mpa);
9115 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9116 space_mpa);
9117 mpa = isl_multi_pw_aff_alloc(space_mpa);
9119 space = isl_space_from_domain(space);
9120 space = isl_space_add_dims(space, isl_dim_out, 1);
9121 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9122 for (i = 0; i < n; ++i) {
9123 isl_union_pw_aff *upa;
9124 isl_pw_aff *pa;
9126 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9127 pa = isl_union_pw_aff_extract_pw_aff(upa,
9128 isl_space_copy(space));
9129 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9130 isl_union_pw_aff_free(upa);
9133 isl_space_free(space);
9134 return mpa;
9135 error:
9136 isl_space_free(space);
9137 return NULL;
9140 /* Evaluate the affine function "aff" in the void point "pnt".
9141 * In particular, return the value NaN.
9143 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9144 __isl_take isl_point *pnt)
9146 isl_ctx *ctx;
9148 ctx = isl_point_get_ctx(pnt);
9149 isl_aff_free(aff);
9150 isl_point_free(pnt);
9151 return isl_val_nan(ctx);
9154 /* Evaluate the affine expression "aff"
9155 * in the coordinates (with denominator) "pnt".
9157 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9158 __isl_keep isl_vec *pnt)
9160 isl_int n, d;
9161 isl_ctx *ctx;
9162 isl_val *v;
9164 if (!aff || !pnt)
9165 return NULL;
9167 ctx = isl_vec_get_ctx(aff);
9168 isl_int_init(n);
9169 isl_int_init(d);
9170 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9171 isl_int_mul(d, aff->el[0], pnt->el[0]);
9172 v = isl_val_rat_from_isl_int(ctx, n, d);
9173 v = isl_val_normalize(v);
9174 isl_int_clear(n);
9175 isl_int_clear(d);
9177 return v;
9180 /* Check that the domain space of "aff" is equal to "space".
9182 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9183 __isl_keep isl_space *space)
9185 isl_bool ok;
9187 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9188 if (ok < 0)
9189 return isl_stat_error;
9190 if (!ok)
9191 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9192 "incompatible spaces", return isl_stat_error);
9193 return isl_stat_ok;
9196 /* Evaluate the affine function "aff" in "pnt".
9198 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9199 __isl_take isl_point *pnt)
9201 isl_bool is_void;
9202 isl_val *v;
9203 isl_local_space *ls;
9205 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9206 goto error;
9207 is_void = isl_point_is_void(pnt);
9208 if (is_void < 0)
9209 goto error;
9210 if (is_void)
9211 return eval_void(aff, pnt);
9213 ls = isl_aff_get_domain_local_space(aff);
9214 pnt = isl_local_space_lift_point(ls, pnt);
9216 v = eval(aff->v, isl_point_peek_vec(pnt));
9218 isl_aff_free(aff);
9219 isl_point_free(pnt);
9221 return v;
9222 error:
9223 isl_aff_free(aff);
9224 isl_point_free(pnt);
9225 return NULL;