add isl_pw_qpolynomial_list
[isl.git] / isl_aff.c
blob7daa9dc094513681a10aa38ff5826a79ece00a34
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
25 #include <isl/id.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
53 #undef BASE
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
59 __isl_take isl_vec *v)
61 isl_aff *aff;
63 if (!ls || !v)
64 goto error;
66 aff = isl_calloc_type(v->ctx, struct isl_aff);
67 if (!aff)
68 goto error;
70 aff->ref = 1;
71 aff->ls = ls;
72 aff->v = v;
74 return aff;
75 error:
76 isl_local_space_free(ls);
77 isl_vec_free(v);
78 return NULL;
81 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
83 isl_ctx *ctx;
84 isl_vec *v;
85 unsigned total;
87 if (!ls)
88 return NULL;
90 ctx = isl_local_space_get_ctx(ls);
91 if (!isl_local_space_divs_known(ls))
92 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
93 goto error);
94 if (!isl_local_space_is_set(ls))
95 isl_die(ctx, isl_error_invalid,
96 "domain of affine expression should be a set",
97 goto error);
99 total = isl_local_space_dim(ls, isl_dim_all);
100 v = isl_vec_alloc(ctx, 1 + 1 + total);
101 return isl_aff_alloc_vec(ls, v);
102 error:
103 isl_local_space_free(ls);
104 return NULL;
107 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
109 isl_aff *aff;
111 aff = isl_aff_alloc(ls);
112 if (!aff)
113 return NULL;
115 isl_int_set_si(aff->v->el[0], 1);
116 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
118 return aff;
121 /* Return a piecewise affine expression defined on the specified domain
122 * that is equal to zero.
124 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
126 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
129 /* Return an affine expression defined on the specified domain
130 * that represents NaN.
132 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
134 isl_aff *aff;
136 aff = isl_aff_alloc(ls);
137 if (!aff)
138 return NULL;
140 isl_seq_clr(aff->v->el, aff->v->size);
142 return aff;
145 /* Return a piecewise affine expression defined on the specified domain
146 * that represents NaN.
148 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
150 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
153 /* Return an affine expression that is equal to "val" on
154 * domain local space "ls".
156 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
157 __isl_take isl_val *val)
159 isl_aff *aff;
161 if (!ls || !val)
162 goto error;
163 if (!isl_val_is_rat(val))
164 isl_die(isl_val_get_ctx(val), isl_error_invalid,
165 "expecting rational value", goto error);
167 aff = isl_aff_alloc(isl_local_space_copy(ls));
168 if (!aff)
169 goto error;
171 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
172 isl_int_set(aff->v->el[1], val->n);
173 isl_int_set(aff->v->el[0], val->d);
175 isl_local_space_free(ls);
176 isl_val_free(val);
177 return aff;
178 error:
179 isl_local_space_free(ls);
180 isl_val_free(val);
181 return NULL;
184 /* Return an affine expression that is equal to the specified dimension
185 * in "ls".
187 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
188 enum isl_dim_type type, unsigned pos)
190 isl_space *space;
191 isl_aff *aff;
193 if (!ls)
194 return NULL;
196 space = isl_local_space_get_space(ls);
197 if (!space)
198 goto error;
199 if (isl_space_is_map(space))
200 isl_die(isl_space_get_ctx(space), isl_error_invalid,
201 "expecting (parameter) set space", goto error);
202 if (pos >= isl_local_space_dim(ls, type))
203 isl_die(isl_space_get_ctx(space), isl_error_invalid,
204 "position out of bounds", goto error);
206 isl_space_free(space);
207 aff = isl_aff_alloc(ls);
208 if (!aff)
209 return NULL;
211 pos += isl_local_space_offset(aff->ls, type);
213 isl_int_set_si(aff->v->el[0], 1);
214 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
215 isl_int_set_si(aff->v->el[1 + pos], 1);
217 return aff;
218 error:
219 isl_local_space_free(ls);
220 isl_space_free(space);
221 return NULL;
224 /* Return a piecewise affine expression that is equal to
225 * the specified dimension in "ls".
227 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
228 enum isl_dim_type type, unsigned pos)
230 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
233 /* Return an affine expression that is equal to the parameter
234 * in the domain space "space" with identifier "id".
236 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
237 __isl_take isl_space *space, __isl_take isl_id *id)
239 int pos;
240 isl_local_space *ls;
242 if (!space || !id)
243 goto error;
244 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
245 if (pos < 0)
246 isl_die(isl_space_get_ctx(space), isl_error_invalid,
247 "parameter not found in space", goto error);
248 isl_id_free(id);
249 ls = isl_local_space_from_space(space);
250 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
251 error:
252 isl_space_free(space);
253 isl_id_free(id);
254 return NULL;
257 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
259 if (!aff)
260 return NULL;
262 aff->ref++;
263 return aff;
266 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
268 if (!aff)
269 return NULL;
271 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
272 isl_vec_copy(aff->v));
275 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
277 if (!aff)
278 return NULL;
280 if (aff->ref == 1)
281 return aff;
282 aff->ref--;
283 return isl_aff_dup(aff);
286 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
288 if (!aff)
289 return NULL;
291 if (--aff->ref > 0)
292 return NULL;
294 isl_local_space_free(aff->ls);
295 isl_vec_free(aff->v);
297 free(aff);
299 return NULL;
302 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
304 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
307 /* Return a hash value that digests "aff".
309 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
311 uint32_t hash, ls_hash, v_hash;
313 if (!aff)
314 return 0;
316 hash = isl_hash_init();
317 ls_hash = isl_local_space_get_hash(aff->ls);
318 isl_hash_hash(hash, ls_hash);
319 v_hash = isl_vec_get_hash(aff->v);
320 isl_hash_hash(hash, v_hash);
322 return hash;
325 /* Externally, an isl_aff has a map space, but internally, the
326 * ls field corresponds to the domain of that space.
328 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
330 if (!aff)
331 return 0;
332 if (type == isl_dim_out)
333 return 1;
334 if (type == isl_dim_in)
335 type = isl_dim_set;
336 return isl_local_space_dim(aff->ls, type);
339 /* Return the position of the dimension of the given type and name
340 * in "aff".
341 * Return -1 if no such dimension can be found.
343 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
344 const char *name)
346 if (!aff)
347 return -1;
348 if (type == isl_dim_out)
349 return -1;
350 if (type == isl_dim_in)
351 type = isl_dim_set;
352 return isl_local_space_find_dim_by_name(aff->ls, type, name);
355 /* Return the domain space of "aff".
357 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
359 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
362 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
364 return isl_space_copy(isl_aff_peek_domain_space(aff));
367 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
369 isl_space *space;
370 if (!aff)
371 return NULL;
372 space = isl_local_space_get_space(aff->ls);
373 space = isl_space_from_domain(space);
374 space = isl_space_add_dims(space, isl_dim_out, 1);
375 return space;
378 __isl_give isl_local_space *isl_aff_get_domain_local_space(
379 __isl_keep isl_aff *aff)
381 return aff ? isl_local_space_copy(aff->ls) : NULL;
384 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
386 isl_local_space *ls;
387 if (!aff)
388 return NULL;
389 ls = isl_local_space_copy(aff->ls);
390 ls = isl_local_space_from_domain(ls);
391 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
392 return ls;
395 /* Return the local space of the domain of "aff".
396 * This may be either a copy or the local space itself
397 * if there is only one reference to "aff".
398 * This allows the local space to be modified inplace
399 * if both the expression and its local space have only a single reference.
400 * The caller is not allowed to modify "aff" between this call and
401 * a subsequent call to isl_aff_restore_domain_local_space.
402 * The only exception is that isl_aff_free can be called instead.
404 __isl_give isl_local_space *isl_aff_take_domain_local_space(
405 __isl_keep isl_aff *aff)
407 isl_local_space *ls;
409 if (!aff)
410 return NULL;
411 if (aff->ref != 1)
412 return isl_aff_get_domain_local_space(aff);
413 ls = aff->ls;
414 aff->ls = NULL;
415 return ls;
418 /* Set the local space of the domain of "aff" to "ls",
419 * where the local space of "aff" may be missing
420 * due to a preceding call to isl_aff_take_domain_local_space.
421 * However, in this case, "aff" only has a single reference and
422 * then the call to isl_aff_cow has no effect.
424 __isl_give isl_aff *isl_aff_restore_domain_local_space(
425 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
427 if (!aff || !ls)
428 goto error;
430 if (aff->ls == ls) {
431 isl_local_space_free(ls);
432 return aff;
435 aff = isl_aff_cow(aff);
436 if (!aff)
437 goto error;
438 isl_local_space_free(aff->ls);
439 aff->ls = ls;
441 return aff;
442 error:
443 isl_aff_free(aff);
444 isl_local_space_free(ls);
445 return NULL;
448 /* Externally, an isl_aff has a map space, but internally, the
449 * ls field corresponds to the domain of that space.
451 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
452 enum isl_dim_type type, unsigned pos)
454 if (!aff)
455 return NULL;
456 if (type == isl_dim_out)
457 return NULL;
458 if (type == isl_dim_in)
459 type = isl_dim_set;
460 return isl_local_space_get_dim_name(aff->ls, type, pos);
463 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
464 __isl_take isl_space *dim)
466 aff = isl_aff_cow(aff);
467 if (!aff || !dim)
468 goto error;
470 aff->ls = isl_local_space_reset_space(aff->ls, dim);
471 if (!aff->ls)
472 return isl_aff_free(aff);
474 return aff;
475 error:
476 isl_aff_free(aff);
477 isl_space_free(dim);
478 return NULL;
481 /* Reset the space of "aff". This function is called from isl_pw_templ.c
482 * and doesn't know if the space of an element object is represented
483 * directly or through its domain. It therefore passes along both.
485 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
486 __isl_take isl_space *space, __isl_take isl_space *domain)
488 isl_space_free(space);
489 return isl_aff_reset_domain_space(aff, domain);
492 /* Reorder the coefficients of the affine expression based
493 * on the given reordering.
494 * The reordering r is assumed to have been extended with the local
495 * variables.
497 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
498 __isl_take isl_reordering *r, int n_div)
500 isl_space *space;
501 isl_vec *res;
502 int i;
504 if (!vec || !r)
505 goto error;
507 space = isl_reordering_peek_space(r);
508 res = isl_vec_alloc(vec->ctx,
509 2 + isl_space_dim(space, isl_dim_all) + n_div);
510 if (!res)
511 goto error;
512 isl_seq_cpy(res->el, vec->el, 2);
513 isl_seq_clr(res->el + 2, res->size - 2);
514 for (i = 0; i < r->len; ++i)
515 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
517 isl_reordering_free(r);
518 isl_vec_free(vec);
519 return res;
520 error:
521 isl_vec_free(vec);
522 isl_reordering_free(r);
523 return NULL;
526 /* Reorder the dimensions of the domain of "aff" according
527 * to the given reordering.
529 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
530 __isl_take isl_reordering *r)
532 aff = isl_aff_cow(aff);
533 if (!aff)
534 goto error;
536 r = isl_reordering_extend(r, aff->ls->div->n_row);
537 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
538 aff->ls->div->n_row);
539 aff->ls = isl_local_space_realign(aff->ls, r);
541 if (!aff->v || !aff->ls)
542 return isl_aff_free(aff);
544 return aff;
545 error:
546 isl_aff_free(aff);
547 isl_reordering_free(r);
548 return NULL;
551 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
552 __isl_take isl_space *model)
554 isl_bool equal_params;
556 if (!aff || !model)
557 goto error;
559 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
560 if (equal_params < 0)
561 goto error;
562 if (!equal_params) {
563 isl_reordering *exp;
565 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
566 exp = isl_reordering_extend_space(exp,
567 isl_aff_get_domain_space(aff));
568 aff = isl_aff_realign_domain(aff, exp);
571 isl_space_free(model);
572 return aff;
573 error:
574 isl_space_free(model);
575 isl_aff_free(aff);
576 return NULL;
579 /* Is "aff" obviously equal to zero?
581 * If the denominator is zero, then "aff" is not equal to zero.
583 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
585 if (!aff)
586 return isl_bool_error;
588 if (isl_int_is_zero(aff->v->el[0]))
589 return isl_bool_false;
590 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
593 /* Does "aff" represent NaN?
595 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
597 if (!aff)
598 return isl_bool_error;
600 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
603 /* Are "aff1" and "aff2" obviously equal?
605 * NaN is not equal to anything, not even to another NaN.
607 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
608 __isl_keep isl_aff *aff2)
610 isl_bool equal;
612 if (!aff1 || !aff2)
613 return isl_bool_error;
615 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
616 return isl_bool_false;
618 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
619 if (equal < 0 || !equal)
620 return equal;
622 return isl_vec_is_equal(aff1->v, aff2->v);
625 /* Return the common denominator of "aff" in "v".
627 * We cannot return anything meaningful in case of a NaN.
629 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
631 if (!aff)
632 return isl_stat_error;
633 if (isl_aff_is_nan(aff))
634 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
635 "cannot get denominator of NaN", return isl_stat_error);
636 isl_int_set(*v, aff->v->el[0]);
637 return isl_stat_ok;
640 /* Return the common denominator of "aff".
642 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
644 isl_ctx *ctx;
646 if (!aff)
647 return NULL;
649 ctx = isl_aff_get_ctx(aff);
650 if (isl_aff_is_nan(aff))
651 return isl_val_nan(ctx);
652 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
655 /* Return the constant term of "aff".
657 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
659 isl_ctx *ctx;
660 isl_val *v;
662 if (!aff)
663 return NULL;
665 ctx = isl_aff_get_ctx(aff);
666 if (isl_aff_is_nan(aff))
667 return isl_val_nan(ctx);
668 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
669 return isl_val_normalize(v);
672 /* Return the coefficient of the variable of type "type" at position "pos"
673 * of "aff".
675 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
676 enum isl_dim_type type, int pos)
678 isl_ctx *ctx;
679 isl_val *v;
681 if (!aff)
682 return NULL;
684 ctx = isl_aff_get_ctx(aff);
685 if (type == isl_dim_out)
686 isl_die(ctx, isl_error_invalid,
687 "output/set dimension does not have a coefficient",
688 return NULL);
689 if (type == isl_dim_in)
690 type = isl_dim_set;
692 if (pos >= isl_local_space_dim(aff->ls, type))
693 isl_die(ctx, isl_error_invalid,
694 "position out of bounds", return NULL);
696 if (isl_aff_is_nan(aff))
697 return isl_val_nan(ctx);
698 pos += isl_local_space_offset(aff->ls, type);
699 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
700 return isl_val_normalize(v);
703 /* Return the sign of the coefficient of the variable of type "type"
704 * at position "pos" of "aff".
706 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
707 int pos)
709 isl_ctx *ctx;
711 if (!aff)
712 return 0;
714 ctx = isl_aff_get_ctx(aff);
715 if (type == isl_dim_out)
716 isl_die(ctx, isl_error_invalid,
717 "output/set dimension does not have a coefficient",
718 return 0);
719 if (type == isl_dim_in)
720 type = isl_dim_set;
722 if (pos >= isl_local_space_dim(aff->ls, type))
723 isl_die(ctx, isl_error_invalid,
724 "position out of bounds", return 0);
726 pos += isl_local_space_offset(aff->ls, type);
727 return isl_int_sgn(aff->v->el[1 + pos]);
730 /* Replace the numerator of the constant term of "aff" by "v".
732 * A NaN is unaffected by this operation.
734 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
736 if (!aff)
737 return NULL;
738 if (isl_aff_is_nan(aff))
739 return aff;
740 aff = isl_aff_cow(aff);
741 if (!aff)
742 return NULL;
744 aff->v = isl_vec_cow(aff->v);
745 if (!aff->v)
746 return isl_aff_free(aff);
748 isl_int_set(aff->v->el[1], v);
750 return aff;
753 /* Replace the constant term of "aff" by "v".
755 * A NaN is unaffected by this operation.
757 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
758 __isl_take isl_val *v)
760 if (!aff || !v)
761 goto error;
763 if (isl_aff_is_nan(aff)) {
764 isl_val_free(v);
765 return aff;
768 if (!isl_val_is_rat(v))
769 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
770 "expecting rational value", goto error);
772 if (isl_int_eq(aff->v->el[1], v->n) &&
773 isl_int_eq(aff->v->el[0], v->d)) {
774 isl_val_free(v);
775 return aff;
778 aff = isl_aff_cow(aff);
779 if (!aff)
780 goto error;
781 aff->v = isl_vec_cow(aff->v);
782 if (!aff->v)
783 goto error;
785 if (isl_int_eq(aff->v->el[0], v->d)) {
786 isl_int_set(aff->v->el[1], v->n);
787 } else if (isl_int_is_one(v->d)) {
788 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
789 } else {
790 isl_seq_scale(aff->v->el + 1,
791 aff->v->el + 1, v->d, aff->v->size - 1);
792 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
793 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
794 aff->v = isl_vec_normalize(aff->v);
795 if (!aff->v)
796 goto error;
799 isl_val_free(v);
800 return aff;
801 error:
802 isl_aff_free(aff);
803 isl_val_free(v);
804 return NULL;
807 /* Add "v" to the constant term of "aff".
809 * A NaN is unaffected by this operation.
811 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
813 if (isl_int_is_zero(v))
814 return aff;
816 if (!aff)
817 return NULL;
818 if (isl_aff_is_nan(aff))
819 return aff;
820 aff = isl_aff_cow(aff);
821 if (!aff)
822 return NULL;
824 aff->v = isl_vec_cow(aff->v);
825 if (!aff->v)
826 return isl_aff_free(aff);
828 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
830 return aff;
833 /* Add "v" to the constant term of "aff".
835 * A NaN is unaffected by this operation.
837 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
838 __isl_take isl_val *v)
840 if (!aff || !v)
841 goto error;
843 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
844 isl_val_free(v);
845 return aff;
848 if (!isl_val_is_rat(v))
849 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
850 "expecting rational value", goto error);
852 aff = isl_aff_cow(aff);
853 if (!aff)
854 goto error;
856 aff->v = isl_vec_cow(aff->v);
857 if (!aff->v)
858 goto error;
860 if (isl_int_is_one(v->d)) {
861 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
862 } else if (isl_int_eq(aff->v->el[0], v->d)) {
863 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
864 aff->v = isl_vec_normalize(aff->v);
865 if (!aff->v)
866 goto error;
867 } else {
868 isl_seq_scale(aff->v->el + 1,
869 aff->v->el + 1, v->d, aff->v->size - 1);
870 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
871 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
872 aff->v = isl_vec_normalize(aff->v);
873 if (!aff->v)
874 goto error;
877 isl_val_free(v);
878 return aff;
879 error:
880 isl_aff_free(aff);
881 isl_val_free(v);
882 return NULL;
885 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
887 isl_int t;
889 isl_int_init(t);
890 isl_int_set_si(t, v);
891 aff = isl_aff_add_constant(aff, t);
892 isl_int_clear(t);
894 return aff;
897 /* Add "v" to the numerator of the constant term of "aff".
899 * A NaN is unaffected by this operation.
901 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
903 if (isl_int_is_zero(v))
904 return aff;
906 if (!aff)
907 return NULL;
908 if (isl_aff_is_nan(aff))
909 return aff;
910 aff = isl_aff_cow(aff);
911 if (!aff)
912 return NULL;
914 aff->v = isl_vec_cow(aff->v);
915 if (!aff->v)
916 return isl_aff_free(aff);
918 isl_int_add(aff->v->el[1], aff->v->el[1], v);
920 return aff;
923 /* Add "v" to the numerator of the constant term of "aff".
925 * A NaN is unaffected by this operation.
927 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
929 isl_int t;
931 if (v == 0)
932 return aff;
934 isl_int_init(t);
935 isl_int_set_si(t, v);
936 aff = isl_aff_add_constant_num(aff, t);
937 isl_int_clear(t);
939 return aff;
942 /* Replace the numerator of the constant term of "aff" by "v".
944 * A NaN is unaffected by this operation.
946 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
948 if (!aff)
949 return NULL;
950 if (isl_aff_is_nan(aff))
951 return aff;
952 aff = isl_aff_cow(aff);
953 if (!aff)
954 return NULL;
956 aff->v = isl_vec_cow(aff->v);
957 if (!aff->v)
958 return isl_aff_free(aff);
960 isl_int_set_si(aff->v->el[1], v);
962 return aff;
965 /* Replace the numerator of the coefficient of the variable of type "type"
966 * at position "pos" of "aff" by "v".
968 * A NaN is unaffected by this operation.
970 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
971 enum isl_dim_type type, int pos, isl_int v)
973 if (!aff)
974 return NULL;
976 if (type == isl_dim_out)
977 isl_die(aff->v->ctx, isl_error_invalid,
978 "output/set dimension does not have a coefficient",
979 return isl_aff_free(aff));
980 if (type == isl_dim_in)
981 type = isl_dim_set;
983 if (pos >= isl_local_space_dim(aff->ls, type))
984 isl_die(aff->v->ctx, isl_error_invalid,
985 "position out of bounds", return isl_aff_free(aff));
987 if (isl_aff_is_nan(aff))
988 return aff;
989 aff = isl_aff_cow(aff);
990 if (!aff)
991 return NULL;
993 aff->v = isl_vec_cow(aff->v);
994 if (!aff->v)
995 return isl_aff_free(aff);
997 pos += isl_local_space_offset(aff->ls, type);
998 isl_int_set(aff->v->el[1 + pos], v);
1000 return aff;
1003 /* Replace the numerator of the coefficient of the variable of type "type"
1004 * at position "pos" of "aff" by "v".
1006 * A NaN is unaffected by this operation.
1008 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1009 enum isl_dim_type type, int pos, int v)
1011 if (!aff)
1012 return NULL;
1014 if (type == isl_dim_out)
1015 isl_die(aff->v->ctx, isl_error_invalid,
1016 "output/set dimension does not have a coefficient",
1017 return isl_aff_free(aff));
1018 if (type == isl_dim_in)
1019 type = isl_dim_set;
1021 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1022 isl_die(aff->v->ctx, isl_error_invalid,
1023 "position out of bounds", return isl_aff_free(aff));
1025 if (isl_aff_is_nan(aff))
1026 return aff;
1027 pos += isl_local_space_offset(aff->ls, type);
1028 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1029 return aff;
1031 aff = isl_aff_cow(aff);
1032 if (!aff)
1033 return NULL;
1035 aff->v = isl_vec_cow(aff->v);
1036 if (!aff->v)
1037 return isl_aff_free(aff);
1039 isl_int_set_si(aff->v->el[1 + pos], v);
1041 return aff;
1044 /* Replace the coefficient of the variable of type "type" at position "pos"
1045 * of "aff" by "v".
1047 * A NaN is unaffected by this operation.
1049 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1050 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1052 if (!aff || !v)
1053 goto error;
1055 if (type == isl_dim_out)
1056 isl_die(aff->v->ctx, isl_error_invalid,
1057 "output/set dimension does not have a coefficient",
1058 goto error);
1059 if (type == isl_dim_in)
1060 type = isl_dim_set;
1062 if (pos >= isl_local_space_dim(aff->ls, type))
1063 isl_die(aff->v->ctx, isl_error_invalid,
1064 "position out of bounds", goto error);
1066 if (isl_aff_is_nan(aff)) {
1067 isl_val_free(v);
1068 return aff;
1070 if (!isl_val_is_rat(v))
1071 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1072 "expecting rational value", goto error);
1074 pos += isl_local_space_offset(aff->ls, type);
1075 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1076 isl_int_eq(aff->v->el[0], v->d)) {
1077 isl_val_free(v);
1078 return aff;
1081 aff = isl_aff_cow(aff);
1082 if (!aff)
1083 goto error;
1084 aff->v = isl_vec_cow(aff->v);
1085 if (!aff->v)
1086 goto error;
1088 if (isl_int_eq(aff->v->el[0], v->d)) {
1089 isl_int_set(aff->v->el[1 + pos], v->n);
1090 } else if (isl_int_is_one(v->d)) {
1091 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1092 } else {
1093 isl_seq_scale(aff->v->el + 1,
1094 aff->v->el + 1, v->d, aff->v->size - 1);
1095 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1096 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1097 aff->v = isl_vec_normalize(aff->v);
1098 if (!aff->v)
1099 goto error;
1102 isl_val_free(v);
1103 return aff;
1104 error:
1105 isl_aff_free(aff);
1106 isl_val_free(v);
1107 return NULL;
1110 /* Add "v" to the coefficient of the variable of type "type"
1111 * at position "pos" of "aff".
1113 * A NaN is unaffected by this operation.
1115 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1116 enum isl_dim_type type, int pos, isl_int v)
1118 if (!aff)
1119 return NULL;
1121 if (type == isl_dim_out)
1122 isl_die(aff->v->ctx, isl_error_invalid,
1123 "output/set dimension does not have a coefficient",
1124 return isl_aff_free(aff));
1125 if (type == isl_dim_in)
1126 type = isl_dim_set;
1128 if (pos >= isl_local_space_dim(aff->ls, type))
1129 isl_die(aff->v->ctx, isl_error_invalid,
1130 "position out of bounds", return isl_aff_free(aff));
1132 if (isl_aff_is_nan(aff))
1133 return aff;
1134 aff = isl_aff_cow(aff);
1135 if (!aff)
1136 return NULL;
1138 aff->v = isl_vec_cow(aff->v);
1139 if (!aff->v)
1140 return isl_aff_free(aff);
1142 pos += isl_local_space_offset(aff->ls, type);
1143 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1145 return aff;
1148 /* Add "v" to the coefficient of the variable of type "type"
1149 * at position "pos" of "aff".
1151 * A NaN is unaffected by this operation.
1153 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1154 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1156 if (!aff || !v)
1157 goto error;
1159 if (isl_val_is_zero(v)) {
1160 isl_val_free(v);
1161 return aff;
1164 if (type == isl_dim_out)
1165 isl_die(aff->v->ctx, isl_error_invalid,
1166 "output/set dimension does not have a coefficient",
1167 goto error);
1168 if (type == isl_dim_in)
1169 type = isl_dim_set;
1171 if (pos >= isl_local_space_dim(aff->ls, type))
1172 isl_die(aff->v->ctx, isl_error_invalid,
1173 "position out of bounds", goto error);
1175 if (isl_aff_is_nan(aff)) {
1176 isl_val_free(v);
1177 return aff;
1179 if (!isl_val_is_rat(v))
1180 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1181 "expecting rational value", goto error);
1183 aff = isl_aff_cow(aff);
1184 if (!aff)
1185 goto error;
1187 aff->v = isl_vec_cow(aff->v);
1188 if (!aff->v)
1189 goto error;
1191 pos += isl_local_space_offset(aff->ls, type);
1192 if (isl_int_is_one(v->d)) {
1193 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1194 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1195 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1196 aff->v = isl_vec_normalize(aff->v);
1197 if (!aff->v)
1198 goto error;
1199 } else {
1200 isl_seq_scale(aff->v->el + 1,
1201 aff->v->el + 1, v->d, aff->v->size - 1);
1202 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1203 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1204 aff->v = isl_vec_normalize(aff->v);
1205 if (!aff->v)
1206 goto error;
1209 isl_val_free(v);
1210 return aff;
1211 error:
1212 isl_aff_free(aff);
1213 isl_val_free(v);
1214 return NULL;
1217 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1218 enum isl_dim_type type, int pos, int v)
1220 isl_int t;
1222 isl_int_init(t);
1223 isl_int_set_si(t, v);
1224 aff = isl_aff_add_coefficient(aff, type, pos, t);
1225 isl_int_clear(t);
1227 return aff;
1230 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1232 if (!aff)
1233 return NULL;
1235 return isl_local_space_get_div(aff->ls, pos);
1238 /* Return the negation of "aff".
1240 * As a special case, -NaN = NaN.
1242 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1244 if (!aff)
1245 return NULL;
1246 if (isl_aff_is_nan(aff))
1247 return aff;
1248 aff = isl_aff_cow(aff);
1249 if (!aff)
1250 return NULL;
1251 aff->v = isl_vec_cow(aff->v);
1252 if (!aff->v)
1253 return isl_aff_free(aff);
1255 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1257 return aff;
1260 /* Remove divs from the local space that do not appear in the affine
1261 * expression.
1262 * We currently only remove divs at the end.
1263 * Some intermediate divs may also not appear directly in the affine
1264 * expression, but we would also need to check that no other divs are
1265 * defined in terms of them.
1267 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1269 int pos;
1270 int off;
1271 int n;
1273 if (!aff)
1274 return NULL;
1276 n = isl_local_space_dim(aff->ls, isl_dim_div);
1277 off = isl_local_space_offset(aff->ls, isl_dim_div);
1279 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1280 if (pos == n)
1281 return aff;
1283 aff = isl_aff_cow(aff);
1284 if (!aff)
1285 return NULL;
1287 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1288 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1289 if (!aff->ls || !aff->v)
1290 return isl_aff_free(aff);
1292 return aff;
1295 /* Look for any divs in the aff->ls with a denominator equal to one
1296 * and plug them into the affine expression and any subsequent divs
1297 * that may reference the div.
1299 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1301 int i, n;
1302 int len;
1303 isl_int v;
1304 isl_vec *vec;
1305 isl_local_space *ls;
1306 unsigned pos;
1308 if (!aff)
1309 return NULL;
1311 n = isl_local_space_dim(aff->ls, isl_dim_div);
1312 len = aff->v->size;
1313 for (i = 0; i < n; ++i) {
1314 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1315 continue;
1316 ls = isl_local_space_copy(aff->ls);
1317 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1318 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1319 vec = isl_vec_copy(aff->v);
1320 vec = isl_vec_cow(vec);
1321 if (!ls || !vec)
1322 goto error;
1324 isl_int_init(v);
1326 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1327 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1328 len, len, v);
1330 isl_int_clear(v);
1332 isl_vec_free(aff->v);
1333 aff->v = vec;
1334 isl_local_space_free(aff->ls);
1335 aff->ls = ls;
1338 return aff;
1339 error:
1340 isl_vec_free(vec);
1341 isl_local_space_free(ls);
1342 return isl_aff_free(aff);
1345 /* Look for any divs j that appear with a unit coefficient inside
1346 * the definitions of other divs i and plug them into the definitions
1347 * of the divs i.
1349 * In particular, an expression of the form
1351 * floor((f(..) + floor(g(..)/n))/m)
1353 * is simplified to
1355 * floor((n * f(..) + g(..))/(n * m))
1357 * This simplification is correct because we can move the expression
1358 * f(..) into the inner floor in the original expression to obtain
1360 * floor(floor((n * f(..) + g(..))/n)/m)
1362 * from which we can derive the simplified expression.
1364 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1366 int i, j, n;
1367 int off;
1369 if (!aff)
1370 return NULL;
1372 n = isl_local_space_dim(aff->ls, isl_dim_div);
1373 off = isl_local_space_offset(aff->ls, isl_dim_div);
1374 for (i = 1; i < n; ++i) {
1375 for (j = 0; j < i; ++j) {
1376 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1377 continue;
1378 aff->ls = isl_local_space_substitute_seq(aff->ls,
1379 isl_dim_div, j, aff->ls->div->row[j],
1380 aff->v->size, i, 1);
1381 if (!aff->ls)
1382 return isl_aff_free(aff);
1386 return aff;
1389 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1391 * Even though this function is only called on isl_affs with a single
1392 * reference, we are careful to only change aff->v and aff->ls together.
1394 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1396 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1397 isl_local_space *ls;
1398 isl_vec *v;
1400 ls = isl_local_space_copy(aff->ls);
1401 ls = isl_local_space_swap_div(ls, a, b);
1402 v = isl_vec_copy(aff->v);
1403 v = isl_vec_cow(v);
1404 if (!ls || !v)
1405 goto error;
1407 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1408 isl_vec_free(aff->v);
1409 aff->v = v;
1410 isl_local_space_free(aff->ls);
1411 aff->ls = ls;
1413 return aff;
1414 error:
1415 isl_vec_free(v);
1416 isl_local_space_free(ls);
1417 return isl_aff_free(aff);
1420 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1422 * We currently do not actually remove div "b", but simply add its
1423 * coefficient to that of "a" and then zero it out.
1425 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1427 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1429 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1430 return aff;
1432 aff->v = isl_vec_cow(aff->v);
1433 if (!aff->v)
1434 return isl_aff_free(aff);
1436 isl_int_add(aff->v->el[1 + off + a],
1437 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1438 isl_int_set_si(aff->v->el[1 + off + b], 0);
1440 return aff;
1443 /* Sort the divs in the local space of "aff" according to
1444 * the comparison function "cmp_row" in isl_local_space.c,
1445 * combining the coefficients of identical divs.
1447 * Reordering divs does not change the semantics of "aff",
1448 * so there is no need to call isl_aff_cow.
1449 * Moreover, this function is currently only called on isl_affs
1450 * with a single reference.
1452 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1454 int i, j, n;
1456 if (!aff)
1457 return NULL;
1459 n = isl_aff_dim(aff, isl_dim_div);
1460 for (i = 1; i < n; ++i) {
1461 for (j = i - 1; j >= 0; --j) {
1462 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1463 if (cmp < 0)
1464 break;
1465 if (cmp == 0)
1466 aff = merge_divs(aff, j, j + 1);
1467 else
1468 aff = swap_div(aff, j, j + 1);
1469 if (!aff)
1470 return NULL;
1474 return aff;
1477 /* Normalize the representation of "aff".
1479 * This function should only be called of "new" isl_affs, i.e.,
1480 * with only a single reference. We therefore do not need to
1481 * worry about affecting other instances.
1483 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1485 if (!aff)
1486 return NULL;
1487 aff->v = isl_vec_normalize(aff->v);
1488 if (!aff->v)
1489 return isl_aff_free(aff);
1490 aff = plug_in_integral_divs(aff);
1491 aff = plug_in_unit_divs(aff);
1492 aff = sort_divs(aff);
1493 aff = isl_aff_remove_unused_divs(aff);
1494 return aff;
1497 /* Given f, return floor(f).
1498 * If f is an integer expression, then just return f.
1499 * If f is a constant, then return the constant floor(f).
1500 * Otherwise, if f = g/m, write g = q m + r,
1501 * create a new div d = [r/m] and return the expression q + d.
1502 * The coefficients in r are taken to lie between -m/2 and m/2.
1504 * reduce_div_coefficients performs the same normalization.
1506 * As a special case, floor(NaN) = NaN.
1508 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1510 int i;
1511 int size;
1512 isl_ctx *ctx;
1513 isl_vec *div;
1515 if (!aff)
1516 return NULL;
1518 if (isl_aff_is_nan(aff))
1519 return aff;
1520 if (isl_int_is_one(aff->v->el[0]))
1521 return aff;
1523 aff = isl_aff_cow(aff);
1524 if (!aff)
1525 return NULL;
1527 aff->v = isl_vec_cow(aff->v);
1528 if (!aff->v)
1529 return isl_aff_free(aff);
1531 if (isl_aff_is_cst(aff)) {
1532 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1533 isl_int_set_si(aff->v->el[0], 1);
1534 return aff;
1537 div = isl_vec_copy(aff->v);
1538 div = isl_vec_cow(div);
1539 if (!div)
1540 return isl_aff_free(aff);
1542 ctx = isl_aff_get_ctx(aff);
1543 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1544 for (i = 1; i < aff->v->size; ++i) {
1545 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1546 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1547 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1548 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1549 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1553 aff->ls = isl_local_space_add_div(aff->ls, div);
1554 if (!aff->ls)
1555 return isl_aff_free(aff);
1557 size = aff->v->size;
1558 aff->v = isl_vec_extend(aff->v, size + 1);
1559 if (!aff->v)
1560 return isl_aff_free(aff);
1561 isl_int_set_si(aff->v->el[0], 1);
1562 isl_int_set_si(aff->v->el[size], 1);
1564 aff = isl_aff_normalize(aff);
1566 return aff;
1569 /* Compute
1571 * aff mod m = aff - m * floor(aff/m)
1573 * with m an integer value.
1575 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1576 __isl_take isl_val *m)
1578 isl_aff *res;
1580 if (!aff || !m)
1581 goto error;
1583 if (!isl_val_is_int(m))
1584 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1585 "expecting integer modulo", goto error);
1587 res = isl_aff_copy(aff);
1588 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1589 aff = isl_aff_floor(aff);
1590 aff = isl_aff_scale_val(aff, m);
1591 res = isl_aff_sub(res, aff);
1593 return res;
1594 error:
1595 isl_aff_free(aff);
1596 isl_val_free(m);
1597 return NULL;
1600 /* Compute
1602 * pwaff mod m = pwaff - m * floor(pwaff/m)
1604 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1606 isl_pw_aff *res;
1608 res = isl_pw_aff_copy(pwaff);
1609 pwaff = isl_pw_aff_scale_down(pwaff, m);
1610 pwaff = isl_pw_aff_floor(pwaff);
1611 pwaff = isl_pw_aff_scale(pwaff, m);
1612 res = isl_pw_aff_sub(res, pwaff);
1614 return res;
1617 /* Compute
1619 * pa mod m = pa - m * floor(pa/m)
1621 * with m an integer value.
1623 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1624 __isl_take isl_val *m)
1626 if (!pa || !m)
1627 goto error;
1628 if (!isl_val_is_int(m))
1629 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1630 "expecting integer modulo", goto error);
1631 pa = isl_pw_aff_mod(pa, m->n);
1632 isl_val_free(m);
1633 return pa;
1634 error:
1635 isl_pw_aff_free(pa);
1636 isl_val_free(m);
1637 return NULL;
1640 /* Given f, return ceil(f).
1641 * If f is an integer expression, then just return f.
1642 * Otherwise, let f be the expression
1644 * e/m
1646 * then return
1648 * floor((e + m - 1)/m)
1650 * As a special case, ceil(NaN) = NaN.
1652 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1654 if (!aff)
1655 return NULL;
1657 if (isl_aff_is_nan(aff))
1658 return aff;
1659 if (isl_int_is_one(aff->v->el[0]))
1660 return aff;
1662 aff = isl_aff_cow(aff);
1663 if (!aff)
1664 return NULL;
1665 aff->v = isl_vec_cow(aff->v);
1666 if (!aff->v)
1667 return isl_aff_free(aff);
1669 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1670 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1671 aff = isl_aff_floor(aff);
1673 return aff;
1676 /* Apply the expansion computed by isl_merge_divs.
1677 * The expansion itself is given by "exp" while the resulting
1678 * list of divs is given by "div".
1680 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1681 __isl_take isl_mat *div, int *exp)
1683 int old_n_div;
1684 int new_n_div;
1685 int offset;
1687 aff = isl_aff_cow(aff);
1688 if (!aff || !div)
1689 goto error;
1691 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1692 new_n_div = isl_mat_rows(div);
1693 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1695 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1696 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1697 if (!aff->v || !aff->ls)
1698 return isl_aff_free(aff);
1699 return aff;
1700 error:
1701 isl_aff_free(aff);
1702 isl_mat_free(div);
1703 return NULL;
1706 /* Add two affine expressions that live in the same local space.
1708 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1709 __isl_take isl_aff *aff2)
1711 isl_int gcd, f;
1713 aff1 = isl_aff_cow(aff1);
1714 if (!aff1 || !aff2)
1715 goto error;
1717 aff1->v = isl_vec_cow(aff1->v);
1718 if (!aff1->v)
1719 goto error;
1721 isl_int_init(gcd);
1722 isl_int_init(f);
1723 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1724 isl_int_divexact(f, aff2->v->el[0], gcd);
1725 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1726 isl_int_divexact(f, aff1->v->el[0], gcd);
1727 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1728 isl_int_divexact(f, aff2->v->el[0], gcd);
1729 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1730 isl_int_clear(f);
1731 isl_int_clear(gcd);
1733 isl_aff_free(aff2);
1734 return aff1;
1735 error:
1736 isl_aff_free(aff1);
1737 isl_aff_free(aff2);
1738 return NULL;
1741 /* Return the sum of "aff1" and "aff2".
1743 * If either of the two is NaN, then the result is NaN.
1745 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1746 __isl_take isl_aff *aff2)
1748 isl_ctx *ctx;
1749 int *exp1 = NULL;
1750 int *exp2 = NULL;
1751 isl_mat *div;
1752 int n_div1, n_div2;
1754 if (!aff1 || !aff2)
1755 goto error;
1757 ctx = isl_aff_get_ctx(aff1);
1758 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1759 isl_die(ctx, isl_error_invalid,
1760 "spaces don't match", goto error);
1762 if (isl_aff_is_nan(aff1)) {
1763 isl_aff_free(aff2);
1764 return aff1;
1766 if (isl_aff_is_nan(aff2)) {
1767 isl_aff_free(aff1);
1768 return aff2;
1771 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1772 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1773 if (n_div1 == 0 && n_div2 == 0)
1774 return add_expanded(aff1, aff2);
1776 exp1 = isl_alloc_array(ctx, int, n_div1);
1777 exp2 = isl_alloc_array(ctx, int, n_div2);
1778 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1779 goto error;
1781 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1782 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1783 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1784 free(exp1);
1785 free(exp2);
1787 return add_expanded(aff1, aff2);
1788 error:
1789 free(exp1);
1790 free(exp2);
1791 isl_aff_free(aff1);
1792 isl_aff_free(aff2);
1793 return NULL;
1796 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1797 __isl_take isl_aff *aff2)
1799 return isl_aff_add(aff1, isl_aff_neg(aff2));
1802 /* Return the result of scaling "aff" by a factor of "f".
1804 * As a special case, f * NaN = NaN.
1806 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1808 isl_int gcd;
1810 if (!aff)
1811 return NULL;
1812 if (isl_aff_is_nan(aff))
1813 return aff;
1815 if (isl_int_is_one(f))
1816 return aff;
1818 aff = isl_aff_cow(aff);
1819 if (!aff)
1820 return NULL;
1821 aff->v = isl_vec_cow(aff->v);
1822 if (!aff->v)
1823 return isl_aff_free(aff);
1825 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1826 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1827 return aff;
1830 isl_int_init(gcd);
1831 isl_int_gcd(gcd, aff->v->el[0], f);
1832 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1833 isl_int_divexact(gcd, f, gcd);
1834 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1835 isl_int_clear(gcd);
1837 return aff;
1840 /* Multiple "aff" by "v".
1842 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1843 __isl_take isl_val *v)
1845 if (!aff || !v)
1846 goto error;
1848 if (isl_val_is_one(v)) {
1849 isl_val_free(v);
1850 return aff;
1853 if (!isl_val_is_rat(v))
1854 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1855 "expecting rational factor", goto error);
1857 aff = isl_aff_scale(aff, v->n);
1858 aff = isl_aff_scale_down(aff, v->d);
1860 isl_val_free(v);
1861 return aff;
1862 error:
1863 isl_aff_free(aff);
1864 isl_val_free(v);
1865 return NULL;
1868 /* Return the result of scaling "aff" down by a factor of "f".
1870 * As a special case, NaN/f = NaN.
1872 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1874 isl_int gcd;
1876 if (!aff)
1877 return NULL;
1878 if (isl_aff_is_nan(aff))
1879 return aff;
1881 if (isl_int_is_one(f))
1882 return aff;
1884 aff = isl_aff_cow(aff);
1885 if (!aff)
1886 return NULL;
1888 if (isl_int_is_zero(f))
1889 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1890 "cannot scale down by zero", return isl_aff_free(aff));
1892 aff->v = isl_vec_cow(aff->v);
1893 if (!aff->v)
1894 return isl_aff_free(aff);
1896 isl_int_init(gcd);
1897 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1898 isl_int_gcd(gcd, gcd, f);
1899 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1900 isl_int_divexact(gcd, f, gcd);
1901 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1902 isl_int_clear(gcd);
1904 return aff;
1907 /* Divide "aff" by "v".
1909 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1910 __isl_take isl_val *v)
1912 if (!aff || !v)
1913 goto error;
1915 if (isl_val_is_one(v)) {
1916 isl_val_free(v);
1917 return aff;
1920 if (!isl_val_is_rat(v))
1921 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1922 "expecting rational factor", goto error);
1923 if (!isl_val_is_pos(v))
1924 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1925 "factor needs to be positive", goto error);
1927 aff = isl_aff_scale(aff, v->d);
1928 aff = isl_aff_scale_down(aff, v->n);
1930 isl_val_free(v);
1931 return aff;
1932 error:
1933 isl_aff_free(aff);
1934 isl_val_free(v);
1935 return NULL;
1938 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1940 isl_int v;
1942 if (f == 1)
1943 return aff;
1945 isl_int_init(v);
1946 isl_int_set_ui(v, f);
1947 aff = isl_aff_scale_down(aff, v);
1948 isl_int_clear(v);
1950 return aff;
1953 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1954 enum isl_dim_type type, unsigned pos, const char *s)
1956 aff = isl_aff_cow(aff);
1957 if (!aff)
1958 return NULL;
1959 if (type == isl_dim_out)
1960 isl_die(aff->v->ctx, isl_error_invalid,
1961 "cannot set name of output/set dimension",
1962 return isl_aff_free(aff));
1963 if (type == isl_dim_in)
1964 type = isl_dim_set;
1965 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1966 if (!aff->ls)
1967 return isl_aff_free(aff);
1969 return aff;
1972 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1973 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1975 aff = isl_aff_cow(aff);
1976 if (!aff)
1977 goto error;
1978 if (type == isl_dim_out)
1979 isl_die(aff->v->ctx, isl_error_invalid,
1980 "cannot set name of output/set dimension",
1981 goto error);
1982 if (type == isl_dim_in)
1983 type = isl_dim_set;
1984 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1985 if (!aff->ls)
1986 return isl_aff_free(aff);
1988 return aff;
1989 error:
1990 isl_id_free(id);
1991 isl_aff_free(aff);
1992 return NULL;
1995 /* Replace the identifier of the input tuple of "aff" by "id".
1996 * type is currently required to be equal to isl_dim_in
1998 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1999 enum isl_dim_type type, __isl_take isl_id *id)
2001 aff = isl_aff_cow(aff);
2002 if (!aff)
2003 goto error;
2004 if (type != isl_dim_in)
2005 isl_die(aff->v->ctx, isl_error_invalid,
2006 "cannot only set id of input tuple", goto error);
2007 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2008 if (!aff->ls)
2009 return isl_aff_free(aff);
2011 return aff;
2012 error:
2013 isl_id_free(id);
2014 isl_aff_free(aff);
2015 return NULL;
2018 /* Exploit the equalities in "eq" to simplify the affine expression
2019 * and the expressions of the integer divisions in the local space.
2020 * The integer divisions in this local space are assumed to appear
2021 * as regular dimensions in "eq".
2023 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2024 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2026 int i, j;
2027 unsigned total;
2028 unsigned n_div;
2030 if (!eq)
2031 goto error;
2032 if (eq->n_eq == 0) {
2033 isl_basic_set_free(eq);
2034 return aff;
2037 aff = isl_aff_cow(aff);
2038 if (!aff)
2039 goto error;
2041 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2042 isl_basic_set_copy(eq));
2043 aff->v = isl_vec_cow(aff->v);
2044 if (!aff->ls || !aff->v)
2045 goto error;
2047 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2048 n_div = eq->n_div;
2049 for (i = 0; i < eq->n_eq; ++i) {
2050 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2051 if (j < 0 || j == 0 || j >= total)
2052 continue;
2054 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2055 &aff->v->el[0]);
2058 isl_basic_set_free(eq);
2059 aff = isl_aff_normalize(aff);
2060 return aff;
2061 error:
2062 isl_basic_set_free(eq);
2063 isl_aff_free(aff);
2064 return NULL;
2067 /* Exploit the equalities in "eq" to simplify the affine expression
2068 * and the expressions of the integer divisions in the local space.
2070 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2071 __isl_take isl_basic_set *eq)
2073 int n_div;
2075 if (!aff || !eq)
2076 goto error;
2077 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2078 if (n_div > 0)
2079 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2080 return isl_aff_substitute_equalities_lifted(aff, eq);
2081 error:
2082 isl_basic_set_free(eq);
2083 isl_aff_free(aff);
2084 return NULL;
2087 /* Look for equalities among the variables shared by context and aff
2088 * and the integer divisions of aff, if any.
2089 * The equalities are then used to eliminate coefficients and/or integer
2090 * divisions from aff.
2092 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2093 __isl_take isl_set *context)
2095 isl_basic_set *hull;
2096 int n_div;
2098 if (!aff)
2099 goto error;
2100 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2101 if (n_div > 0) {
2102 isl_basic_set *bset;
2103 isl_local_space *ls;
2104 context = isl_set_add_dims(context, isl_dim_set, n_div);
2105 ls = isl_aff_get_domain_local_space(aff);
2106 bset = isl_basic_set_from_local_space(ls);
2107 bset = isl_basic_set_lift(bset);
2108 bset = isl_basic_set_flatten(bset);
2109 context = isl_set_intersect(context,
2110 isl_set_from_basic_set(bset));
2113 hull = isl_set_affine_hull(context);
2114 return isl_aff_substitute_equalities_lifted(aff, hull);
2115 error:
2116 isl_aff_free(aff);
2117 isl_set_free(context);
2118 return NULL;
2121 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2122 __isl_take isl_set *context)
2124 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2125 dom_context = isl_set_intersect_params(dom_context, context);
2126 return isl_aff_gist(aff, dom_context);
2129 /* Return a basic set containing those elements in the space
2130 * of aff where it is positive. "rational" should not be set.
2132 * If "aff" is NaN, then it is not positive.
2134 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2135 int rational)
2137 isl_constraint *ineq;
2138 isl_basic_set *bset;
2139 isl_val *c;
2141 if (!aff)
2142 return NULL;
2143 if (isl_aff_is_nan(aff)) {
2144 isl_space *space = isl_aff_get_domain_space(aff);
2145 isl_aff_free(aff);
2146 return isl_basic_set_empty(space);
2148 if (rational)
2149 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2150 "rational sets not supported", goto error);
2152 ineq = isl_inequality_from_aff(aff);
2153 c = isl_constraint_get_constant_val(ineq);
2154 c = isl_val_sub_ui(c, 1);
2155 ineq = isl_constraint_set_constant_val(ineq, c);
2157 bset = isl_basic_set_from_constraint(ineq);
2158 bset = isl_basic_set_simplify(bset);
2159 return bset;
2160 error:
2161 isl_aff_free(aff);
2162 return NULL;
2165 /* Return a basic set containing those elements in the space
2166 * of aff where it is non-negative.
2167 * If "rational" is set, then return a rational basic set.
2169 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2171 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2172 __isl_take isl_aff *aff, int rational)
2174 isl_constraint *ineq;
2175 isl_basic_set *bset;
2177 if (!aff)
2178 return NULL;
2179 if (isl_aff_is_nan(aff)) {
2180 isl_space *space = isl_aff_get_domain_space(aff);
2181 isl_aff_free(aff);
2182 return isl_basic_set_empty(space);
2185 ineq = isl_inequality_from_aff(aff);
2187 bset = isl_basic_set_from_constraint(ineq);
2188 if (rational)
2189 bset = isl_basic_set_set_rational(bset);
2190 bset = isl_basic_set_simplify(bset);
2191 return bset;
2194 /* Return a basic set containing those elements in the space
2195 * of aff where it is non-negative.
2197 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2199 return aff_nonneg_basic_set(aff, 0);
2202 /* Return a basic set containing those elements in the domain space
2203 * of "aff" where it is positive.
2205 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2207 aff = isl_aff_add_constant_num_si(aff, -1);
2208 return isl_aff_nonneg_basic_set(aff);
2211 /* Return a basic set containing those elements in the domain space
2212 * of aff where it is negative.
2214 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2216 aff = isl_aff_neg(aff);
2217 return isl_aff_pos_basic_set(aff);
2220 /* Return a basic set containing those elements in the space
2221 * of aff where it is zero.
2222 * If "rational" is set, then return a rational basic set.
2224 * If "aff" is NaN, then it is not zero.
2226 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2227 int rational)
2229 isl_constraint *ineq;
2230 isl_basic_set *bset;
2232 if (!aff)
2233 return NULL;
2234 if (isl_aff_is_nan(aff)) {
2235 isl_space *space = isl_aff_get_domain_space(aff);
2236 isl_aff_free(aff);
2237 return isl_basic_set_empty(space);
2240 ineq = isl_equality_from_aff(aff);
2242 bset = isl_basic_set_from_constraint(ineq);
2243 if (rational)
2244 bset = isl_basic_set_set_rational(bset);
2245 bset = isl_basic_set_simplify(bset);
2246 return bset;
2249 /* Return a basic set containing those elements in the space
2250 * of aff where it is zero.
2252 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2254 return aff_zero_basic_set(aff, 0);
2257 /* Return a basic set containing those elements in the shared space
2258 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2260 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2261 __isl_take isl_aff *aff2)
2263 aff1 = isl_aff_sub(aff1, aff2);
2265 return isl_aff_nonneg_basic_set(aff1);
2268 /* Return a basic set containing those elements in the shared domain space
2269 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2271 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2272 __isl_take isl_aff *aff2)
2274 aff1 = isl_aff_sub(aff1, aff2);
2276 return isl_aff_pos_basic_set(aff1);
2279 /* Return a set containing those elements in the shared space
2280 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2282 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2283 __isl_take isl_aff *aff2)
2285 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2288 /* Return a set containing those elements in the shared domain space
2289 * of aff1 and aff2 where aff1 is greater than aff2.
2291 * If either of the two inputs is NaN, then the result is empty,
2292 * as comparisons with NaN always return false.
2294 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2295 __isl_take isl_aff *aff2)
2297 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2300 /* Return a basic set containing those elements in the shared space
2301 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2303 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2304 __isl_take isl_aff *aff2)
2306 return isl_aff_ge_basic_set(aff2, aff1);
2309 /* Return a basic set containing those elements in the shared domain space
2310 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2312 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2313 __isl_take isl_aff *aff2)
2315 return isl_aff_gt_basic_set(aff2, aff1);
2318 /* Return a set containing those elements in the shared space
2319 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2321 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2322 __isl_take isl_aff *aff2)
2324 return isl_aff_ge_set(aff2, aff1);
2327 /* Return a set containing those elements in the shared domain space
2328 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2330 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2331 __isl_take isl_aff *aff2)
2333 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2336 /* Return a basic set containing those elements in the shared space
2337 * of aff1 and aff2 where aff1 and aff2 are equal.
2339 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2340 __isl_take isl_aff *aff2)
2342 aff1 = isl_aff_sub(aff1, aff2);
2344 return isl_aff_zero_basic_set(aff1);
2347 /* Return a set containing those elements in the shared space
2348 * of aff1 and aff2 where aff1 and aff2 are equal.
2350 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2351 __isl_take isl_aff *aff2)
2353 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2356 /* Return a set containing those elements in the shared domain space
2357 * of aff1 and aff2 where aff1 and aff2 are not equal.
2359 * If either of the two inputs is NaN, then the result is empty,
2360 * as comparisons with NaN always return false.
2362 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2363 __isl_take isl_aff *aff2)
2365 isl_set *set_lt, *set_gt;
2367 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2368 isl_aff_copy(aff2));
2369 set_gt = isl_aff_gt_set(aff1, aff2);
2370 return isl_set_union_disjoint(set_lt, set_gt);
2373 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2374 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2376 aff1 = isl_aff_add(aff1, aff2);
2377 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2378 return aff1;
2381 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2383 if (!aff)
2384 return -1;
2386 return 0;
2389 /* Check whether the given affine expression has non-zero coefficient
2390 * for any dimension in the given range or if any of these dimensions
2391 * appear with non-zero coefficients in any of the integer divisions
2392 * involved in the affine expression.
2394 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2395 enum isl_dim_type type, unsigned first, unsigned n)
2397 int i;
2398 isl_ctx *ctx;
2399 int *active = NULL;
2400 isl_bool involves = isl_bool_false;
2402 if (!aff)
2403 return isl_bool_error;
2404 if (n == 0)
2405 return isl_bool_false;
2407 ctx = isl_aff_get_ctx(aff);
2408 if (first + n > isl_aff_dim(aff, type))
2409 isl_die(ctx, isl_error_invalid,
2410 "range out of bounds", return isl_bool_error);
2412 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2413 if (!active)
2414 goto error;
2416 first += isl_local_space_offset(aff->ls, type) - 1;
2417 for (i = 0; i < n; ++i)
2418 if (active[first + i]) {
2419 involves = isl_bool_true;
2420 break;
2423 free(active);
2425 return involves;
2426 error:
2427 free(active);
2428 return isl_bool_error;
2431 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2432 enum isl_dim_type type, unsigned first, unsigned n)
2434 isl_ctx *ctx;
2436 if (!aff)
2437 return NULL;
2438 if (type == isl_dim_out)
2439 isl_die(aff->v->ctx, isl_error_invalid,
2440 "cannot drop output/set dimension",
2441 return isl_aff_free(aff));
2442 if (type == isl_dim_in)
2443 type = isl_dim_set;
2444 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2445 return aff;
2447 ctx = isl_aff_get_ctx(aff);
2448 if (first + n > isl_local_space_dim(aff->ls, type))
2449 isl_die(ctx, isl_error_invalid, "range out of bounds",
2450 return isl_aff_free(aff));
2452 aff = isl_aff_cow(aff);
2453 if (!aff)
2454 return NULL;
2456 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2457 if (!aff->ls)
2458 return isl_aff_free(aff);
2460 first += 1 + isl_local_space_offset(aff->ls, type);
2461 aff->v = isl_vec_drop_els(aff->v, first, n);
2462 if (!aff->v)
2463 return isl_aff_free(aff);
2465 return aff;
2468 /* Drop the "n" domain dimensions starting at "first" from "aff",
2469 * after checking that they do not appear in the affine expression.
2471 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2472 unsigned n)
2474 isl_bool involves;
2476 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2477 if (involves < 0)
2478 return isl_aff_free(aff);
2479 if (involves)
2480 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2481 "affine expression involves some of the domain dimensions",
2482 return isl_aff_free(aff));
2483 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2486 /* Project the domain of the affine expression onto its parameter space.
2487 * The affine expression may not involve any of the domain dimensions.
2489 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2491 isl_space *space;
2492 unsigned n;
2494 n = isl_aff_dim(aff, isl_dim_in);
2495 aff = drop_domain(aff, 0, n);
2496 space = isl_aff_get_domain_space(aff);
2497 space = isl_space_params(space);
2498 aff = isl_aff_reset_domain_space(aff, space);
2499 return aff;
2502 /* Check that the domain of "aff" is a product.
2504 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2506 isl_bool is_product;
2508 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2509 if (is_product < 0)
2510 return isl_stat_error;
2511 if (!is_product)
2512 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2513 "domain is not a product", return isl_stat_error);
2514 return isl_stat_ok;
2517 /* Given an affine function with a domain of the form [A -> B] that
2518 * does not depend on B, return the same function on domain A.
2520 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2522 isl_space *space;
2523 int n, n_in;
2525 if (check_domain_product(aff) < 0)
2526 return isl_aff_free(aff);
2527 space = isl_aff_get_domain_space(aff);
2528 n = isl_space_dim(space, isl_dim_set);
2529 space = isl_space_factor_domain(space);
2530 n_in = isl_space_dim(space, isl_dim_set);
2531 aff = drop_domain(aff, n_in, n - n_in);
2532 aff = isl_aff_reset_domain_space(aff, space);
2533 return aff;
2536 /* Convert an affine expression defined over a parameter domain
2537 * into one that is defined over a zero-dimensional set.
2539 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2541 isl_local_space *ls;
2543 ls = isl_aff_take_domain_local_space(aff);
2544 ls = isl_local_space_set_from_params(ls);
2545 aff = isl_aff_restore_domain_local_space(aff, ls);
2547 return aff;
2550 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2551 enum isl_dim_type type, unsigned first, unsigned n)
2553 isl_ctx *ctx;
2555 if (!aff)
2556 return NULL;
2557 if (type == isl_dim_out)
2558 isl_die(aff->v->ctx, isl_error_invalid,
2559 "cannot insert output/set dimensions",
2560 return isl_aff_free(aff));
2561 if (type == isl_dim_in)
2562 type = isl_dim_set;
2563 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2564 return aff;
2566 ctx = isl_aff_get_ctx(aff);
2567 if (first > isl_local_space_dim(aff->ls, type))
2568 isl_die(ctx, isl_error_invalid, "position out of bounds",
2569 return isl_aff_free(aff));
2571 aff = isl_aff_cow(aff);
2572 if (!aff)
2573 return NULL;
2575 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2576 if (!aff->ls)
2577 return isl_aff_free(aff);
2579 first += 1 + isl_local_space_offset(aff->ls, type);
2580 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2581 if (!aff->v)
2582 return isl_aff_free(aff);
2584 return aff;
2587 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2588 enum isl_dim_type type, unsigned n)
2590 unsigned pos;
2592 pos = isl_aff_dim(aff, type);
2594 return isl_aff_insert_dims(aff, type, pos, n);
2597 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2598 enum isl_dim_type type, unsigned n)
2600 unsigned pos;
2602 pos = isl_pw_aff_dim(pwaff, type);
2604 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2607 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2608 * to dimensions of "dst_type" at "dst_pos".
2610 * We only support moving input dimensions to parameters and vice versa.
2612 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2613 enum isl_dim_type dst_type, unsigned dst_pos,
2614 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2616 unsigned g_dst_pos;
2617 unsigned g_src_pos;
2619 if (!aff)
2620 return NULL;
2621 if (n == 0 &&
2622 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2623 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2624 return aff;
2626 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2627 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2628 "cannot move output/set dimension",
2629 return isl_aff_free(aff));
2630 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2631 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2632 "cannot move divs", return isl_aff_free(aff));
2633 if (dst_type == isl_dim_in)
2634 dst_type = isl_dim_set;
2635 if (src_type == isl_dim_in)
2636 src_type = isl_dim_set;
2638 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2639 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2640 "range out of bounds", return isl_aff_free(aff));
2641 if (dst_type == src_type)
2642 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2643 "moving dims within the same type not supported",
2644 return isl_aff_free(aff));
2646 aff = isl_aff_cow(aff);
2647 if (!aff)
2648 return NULL;
2650 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2651 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2652 if (dst_type > src_type)
2653 g_dst_pos -= n;
2655 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2656 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2657 src_type, src_pos, n);
2658 if (!aff->v || !aff->ls)
2659 return isl_aff_free(aff);
2661 aff = sort_divs(aff);
2663 return aff;
2666 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2668 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2669 return isl_pw_aff_alloc(dom, aff);
2672 #define isl_aff_involves_nan isl_aff_is_nan
2674 #undef PW
2675 #define PW isl_pw_aff
2676 #undef EL
2677 #define EL isl_aff
2678 #undef EL_IS_ZERO
2679 #define EL_IS_ZERO is_empty
2680 #undef ZERO
2681 #define ZERO empty
2682 #undef IS_ZERO
2683 #define IS_ZERO is_empty
2684 #undef FIELD
2685 #define FIELD aff
2686 #undef DEFAULT_IS_ZERO
2687 #define DEFAULT_IS_ZERO 0
2689 #define NO_OPT
2690 #define NO_LIFT
2691 #define NO_MORPH
2693 #include <isl_pw_templ.c>
2694 #include <isl_pw_eval.c>
2695 #include <isl_pw_hash.c>
2696 #include <isl_pw_union_opt.c>
2698 #undef UNION
2699 #define UNION isl_union_pw_aff
2700 #undef PART
2701 #define PART isl_pw_aff
2702 #undef PARTS
2703 #define PARTS pw_aff
2705 #include <isl_union_single.c>
2706 #include <isl_union_neg.c>
2708 static __isl_give isl_set *align_params_pw_pw_set_and(
2709 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2710 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2711 __isl_take isl_pw_aff *pwaff2))
2713 isl_bool equal_params;
2715 if (!pwaff1 || !pwaff2)
2716 goto error;
2717 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2718 if (equal_params < 0)
2719 goto error;
2720 if (equal_params)
2721 return fn(pwaff1, pwaff2);
2722 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2723 isl_pw_aff_check_named_params(pwaff2) < 0)
2724 goto error;
2725 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2726 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2727 return fn(pwaff1, pwaff2);
2728 error:
2729 isl_pw_aff_free(pwaff1);
2730 isl_pw_aff_free(pwaff2);
2731 return NULL;
2734 /* Align the parameters of the to isl_pw_aff arguments and
2735 * then apply a function "fn" on them that returns an isl_map.
2737 static __isl_give isl_map *align_params_pw_pw_map_and(
2738 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2739 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2740 __isl_take isl_pw_aff *pa2))
2742 isl_bool equal_params;
2744 if (!pa1 || !pa2)
2745 goto error;
2746 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2747 if (equal_params < 0)
2748 goto error;
2749 if (equal_params)
2750 return fn(pa1, pa2);
2751 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2752 isl_pw_aff_check_named_params(pa2) < 0)
2753 goto error;
2754 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2755 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2756 return fn(pa1, pa2);
2757 error:
2758 isl_pw_aff_free(pa1);
2759 isl_pw_aff_free(pa2);
2760 return NULL;
2763 /* Compute a piecewise quasi-affine expression with a domain that
2764 * is the union of those of pwaff1 and pwaff2 and such that on each
2765 * cell, the quasi-affine expression is the maximum of those of pwaff1
2766 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2767 * cell, then the associated expression is the defined one.
2769 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2770 __isl_take isl_pw_aff *pwaff2)
2772 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2775 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2776 __isl_take isl_pw_aff *pwaff2)
2778 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2779 &pw_aff_union_max);
2782 /* Compute a piecewise quasi-affine expression with a domain that
2783 * is the union of those of pwaff1 and pwaff2 and such that on each
2784 * cell, the quasi-affine expression is the minimum of those of pwaff1
2785 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2786 * cell, then the associated expression is the defined one.
2788 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2789 __isl_take isl_pw_aff *pwaff2)
2791 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2794 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2795 __isl_take isl_pw_aff *pwaff2)
2797 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2798 &pw_aff_union_min);
2801 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2802 __isl_take isl_pw_aff *pwaff2, int max)
2804 if (max)
2805 return isl_pw_aff_union_max(pwaff1, pwaff2);
2806 else
2807 return isl_pw_aff_union_min(pwaff1, pwaff2);
2810 /* Construct a map with as domain the domain of pwaff and
2811 * one-dimensional range corresponding to the affine expressions.
2813 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2815 int i;
2816 isl_space *dim;
2817 isl_map *map;
2819 if (!pwaff)
2820 return NULL;
2822 dim = isl_pw_aff_get_space(pwaff);
2823 map = isl_map_empty(dim);
2825 for (i = 0; i < pwaff->n; ++i) {
2826 isl_basic_map *bmap;
2827 isl_map *map_i;
2829 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2830 map_i = isl_map_from_basic_map(bmap);
2831 map_i = isl_map_intersect_domain(map_i,
2832 isl_set_copy(pwaff->p[i].set));
2833 map = isl_map_union_disjoint(map, map_i);
2836 isl_pw_aff_free(pwaff);
2838 return map;
2841 /* Construct a map with as domain the domain of pwaff and
2842 * one-dimensional range corresponding to the affine expressions.
2844 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2846 if (!pwaff)
2847 return NULL;
2848 if (isl_space_is_set(pwaff->dim))
2849 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2850 "space of input is not a map", goto error);
2851 return map_from_pw_aff(pwaff);
2852 error:
2853 isl_pw_aff_free(pwaff);
2854 return NULL;
2857 /* Construct a one-dimensional set with as parameter domain
2858 * the domain of pwaff and the single set dimension
2859 * corresponding to the affine expressions.
2861 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2863 if (!pwaff)
2864 return NULL;
2865 if (!isl_space_is_set(pwaff->dim))
2866 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2867 "space of input is not a set", goto error);
2868 return map_from_pw_aff(pwaff);
2869 error:
2870 isl_pw_aff_free(pwaff);
2871 return NULL;
2874 /* Return a set containing those elements in the domain
2875 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2876 * does not satisfy "fn" (if complement is 1).
2878 * The pieces with a NaN never belong to the result since
2879 * NaN does not satisfy any property.
2881 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2882 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2883 int complement)
2885 int i;
2886 isl_set *set;
2888 if (!pwaff)
2889 return NULL;
2891 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2893 for (i = 0; i < pwaff->n; ++i) {
2894 isl_basic_set *bset;
2895 isl_set *set_i, *locus;
2896 isl_bool rational;
2898 if (isl_aff_is_nan(pwaff->p[i].aff))
2899 continue;
2901 rational = isl_set_has_rational(pwaff->p[i].set);
2902 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2903 locus = isl_set_from_basic_set(bset);
2904 set_i = isl_set_copy(pwaff->p[i].set);
2905 if (complement)
2906 set_i = isl_set_subtract(set_i, locus);
2907 else
2908 set_i = isl_set_intersect(set_i, locus);
2909 set = isl_set_union_disjoint(set, set_i);
2912 isl_pw_aff_free(pwaff);
2914 return set;
2917 /* Return a set containing those elements in the domain
2918 * of "pa" where it is positive.
2920 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2922 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2925 /* Return a set containing those elements in the domain
2926 * of pwaff where it is non-negative.
2928 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2930 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2933 /* Return a set containing those elements in the domain
2934 * of pwaff where it is zero.
2936 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2938 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2941 /* Return a set containing those elements in the domain
2942 * of pwaff where it is not zero.
2944 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2946 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2949 /* Return a set containing those elements in the shared domain
2950 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2952 * We compute the difference on the shared domain and then construct
2953 * the set of values where this difference is non-negative.
2954 * If strict is set, we first subtract 1 from the difference.
2955 * If equal is set, we only return the elements where pwaff1 and pwaff2
2956 * are equal.
2958 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2959 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2961 isl_set *set1, *set2;
2963 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2964 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2965 set1 = isl_set_intersect(set1, set2);
2966 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2967 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2968 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2970 if (strict) {
2971 isl_space *dim = isl_set_get_space(set1);
2972 isl_aff *aff;
2973 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2974 aff = isl_aff_add_constant_si(aff, -1);
2975 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2976 } else
2977 isl_set_free(set1);
2979 if (equal)
2980 return isl_pw_aff_zero_set(pwaff1);
2981 return isl_pw_aff_nonneg_set(pwaff1);
2984 /* Return a set containing those elements in the shared domain
2985 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2987 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2988 __isl_take isl_pw_aff *pwaff2)
2990 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2993 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2994 __isl_take isl_pw_aff *pwaff2)
2996 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2999 /* Return a set containing those elements in the shared domain
3000 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3002 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3003 __isl_take isl_pw_aff *pwaff2)
3005 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3008 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3009 __isl_take isl_pw_aff *pwaff2)
3011 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
3014 /* Return a set containing those elements in the shared domain
3015 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3017 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3018 __isl_take isl_pw_aff *pwaff2)
3020 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3023 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3024 __isl_take isl_pw_aff *pwaff2)
3026 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
3029 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3030 __isl_take isl_pw_aff *pwaff2)
3032 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3035 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3036 __isl_take isl_pw_aff *pwaff2)
3038 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3041 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3042 * where the function values are ordered in the same way as "order",
3043 * which returns a set in the shared domain of its two arguments.
3044 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3046 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3047 * We first pull back the two functions such that they are defined on
3048 * the domain [A -> B]. Then we apply "order", resulting in a set
3049 * in the space [A -> B]. Finally, we unwrap this set to obtain
3050 * a map in the space A -> B.
3052 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3053 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3054 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3055 __isl_take isl_pw_aff *pa2))
3057 isl_space *space1, *space2;
3058 isl_multi_aff *ma;
3059 isl_set *set;
3061 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3062 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3063 space1 = isl_space_map_from_domain_and_range(space1, space2);
3064 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3065 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3066 ma = isl_multi_aff_range_map(space1);
3067 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3068 set = order(pa1, pa2);
3070 return isl_set_unwrap(set);
3073 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3074 * where the function values are equal.
3075 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3077 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3078 __isl_take isl_pw_aff *pa2)
3080 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3083 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3084 * where the function values are equal.
3086 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3087 __isl_take isl_pw_aff *pa2)
3089 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3092 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3093 * where the function value of "pa1" is less than the function value of "pa2".
3094 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3096 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3097 __isl_take isl_pw_aff *pa2)
3099 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3102 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3103 * where the function value of "pa1" is less than the function value of "pa2".
3105 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3106 __isl_take isl_pw_aff *pa2)
3108 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3111 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3112 * where the function value of "pa1" is greater than the function value
3113 * of "pa2".
3114 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3116 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3117 __isl_take isl_pw_aff *pa2)
3119 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3122 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3123 * where the function value of "pa1" is greater than the function value
3124 * of "pa2".
3126 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3127 __isl_take isl_pw_aff *pa2)
3129 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3132 /* Return a set containing those elements in the shared domain
3133 * of the elements of list1 and list2 where each element in list1
3134 * has the relation specified by "fn" with each element in list2.
3136 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3137 __isl_take isl_pw_aff_list *list2,
3138 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3139 __isl_take isl_pw_aff *pwaff2))
3141 int i, j;
3142 isl_ctx *ctx;
3143 isl_set *set;
3145 if (!list1 || !list2)
3146 goto error;
3148 ctx = isl_pw_aff_list_get_ctx(list1);
3149 if (list1->n < 1 || list2->n < 1)
3150 isl_die(ctx, isl_error_invalid,
3151 "list should contain at least one element", goto error);
3153 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3154 for (i = 0; i < list1->n; ++i)
3155 for (j = 0; j < list2->n; ++j) {
3156 isl_set *set_ij;
3158 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3159 isl_pw_aff_copy(list2->p[j]));
3160 set = isl_set_intersect(set, set_ij);
3163 isl_pw_aff_list_free(list1);
3164 isl_pw_aff_list_free(list2);
3165 return set;
3166 error:
3167 isl_pw_aff_list_free(list1);
3168 isl_pw_aff_list_free(list2);
3169 return NULL;
3172 /* Return a set containing those elements in the shared domain
3173 * of the elements of list1 and list2 where each element in list1
3174 * is equal to each element in list2.
3176 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3177 __isl_take isl_pw_aff_list *list2)
3179 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3182 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3183 __isl_take isl_pw_aff_list *list2)
3185 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3188 /* Return a set containing those elements in the shared domain
3189 * of the elements of list1 and list2 where each element in list1
3190 * is less than or equal to each element in list2.
3192 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3193 __isl_take isl_pw_aff_list *list2)
3195 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3198 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3199 __isl_take isl_pw_aff_list *list2)
3201 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3204 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3205 __isl_take isl_pw_aff_list *list2)
3207 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3210 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3211 __isl_take isl_pw_aff_list *list2)
3213 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3217 /* Return a set containing those elements in the shared domain
3218 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3220 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3221 __isl_take isl_pw_aff *pwaff2)
3223 isl_set *set_lt, *set_gt;
3225 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3226 isl_pw_aff_copy(pwaff2));
3227 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3228 return isl_set_union_disjoint(set_lt, set_gt);
3231 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3232 __isl_take isl_pw_aff *pwaff2)
3234 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3237 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3238 isl_int v)
3240 int i;
3242 if (isl_int_is_one(v))
3243 return pwaff;
3244 if (!isl_int_is_pos(v))
3245 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3246 "factor needs to be positive",
3247 return isl_pw_aff_free(pwaff));
3248 pwaff = isl_pw_aff_cow(pwaff);
3249 if (!pwaff)
3250 return NULL;
3251 if (pwaff->n == 0)
3252 return pwaff;
3254 for (i = 0; i < pwaff->n; ++i) {
3255 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3256 if (!pwaff->p[i].aff)
3257 return isl_pw_aff_free(pwaff);
3260 return pwaff;
3263 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3265 int i;
3267 pwaff = isl_pw_aff_cow(pwaff);
3268 if (!pwaff)
3269 return NULL;
3270 if (pwaff->n == 0)
3271 return pwaff;
3273 for (i = 0; i < pwaff->n; ++i) {
3274 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3275 if (!pwaff->p[i].aff)
3276 return isl_pw_aff_free(pwaff);
3279 return pwaff;
3282 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3284 int i;
3286 pwaff = isl_pw_aff_cow(pwaff);
3287 if (!pwaff)
3288 return NULL;
3289 if (pwaff->n == 0)
3290 return pwaff;
3292 for (i = 0; i < pwaff->n; ++i) {
3293 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3294 if (!pwaff->p[i].aff)
3295 return isl_pw_aff_free(pwaff);
3298 return pwaff;
3301 /* Assuming that "cond1" and "cond2" are disjoint,
3302 * return an affine expression that is equal to pwaff1 on cond1
3303 * and to pwaff2 on cond2.
3305 static __isl_give isl_pw_aff *isl_pw_aff_select(
3306 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3307 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3309 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3310 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3312 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3315 /* Return an affine expression that is equal to pwaff_true for elements
3316 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3317 * is zero.
3318 * That is, return cond ? pwaff_true : pwaff_false;
3320 * If "cond" involves and NaN, then we conservatively return a NaN
3321 * on its entire domain. In principle, we could consider the pieces
3322 * where it is NaN separately from those where it is not.
3324 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3325 * then only use the domain of "cond" to restrict the domain.
3327 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3328 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3330 isl_set *cond_true, *cond_false;
3331 isl_bool equal;
3333 if (!cond)
3334 goto error;
3335 if (isl_pw_aff_involves_nan(cond)) {
3336 isl_space *space = isl_pw_aff_get_domain_space(cond);
3337 isl_local_space *ls = isl_local_space_from_space(space);
3338 isl_pw_aff_free(cond);
3339 isl_pw_aff_free(pwaff_true);
3340 isl_pw_aff_free(pwaff_false);
3341 return isl_pw_aff_nan_on_domain(ls);
3344 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3345 isl_pw_aff_get_space(pwaff_false));
3346 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3347 isl_pw_aff_get_space(pwaff_true));
3348 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3349 if (equal < 0)
3350 goto error;
3351 if (equal) {
3352 isl_set *dom;
3354 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3355 isl_pw_aff_free(pwaff_false);
3356 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3359 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3360 cond_false = isl_pw_aff_zero_set(cond);
3361 return isl_pw_aff_select(cond_true, pwaff_true,
3362 cond_false, pwaff_false);
3363 error:
3364 isl_pw_aff_free(cond);
3365 isl_pw_aff_free(pwaff_true);
3366 isl_pw_aff_free(pwaff_false);
3367 return NULL;
3370 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3372 if (!aff)
3373 return isl_bool_error;
3375 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3378 /* Check whether pwaff is a piecewise constant.
3380 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3382 int i;
3384 if (!pwaff)
3385 return isl_bool_error;
3387 for (i = 0; i < pwaff->n; ++i) {
3388 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3389 if (is_cst < 0 || !is_cst)
3390 return is_cst;
3393 return isl_bool_true;
3396 /* Are all elements of "mpa" piecewise constants?
3398 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3400 int i;
3402 if (!mpa)
3403 return isl_bool_error;
3405 for (i = 0; i < mpa->n; ++i) {
3406 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3407 if (is_cst < 0 || !is_cst)
3408 return is_cst;
3411 return isl_bool_true;
3414 /* Return the product of "aff1" and "aff2".
3416 * If either of the two is NaN, then the result is NaN.
3418 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3420 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3421 __isl_take isl_aff *aff2)
3423 if (!aff1 || !aff2)
3424 goto error;
3426 if (isl_aff_is_nan(aff1)) {
3427 isl_aff_free(aff2);
3428 return aff1;
3430 if (isl_aff_is_nan(aff2)) {
3431 isl_aff_free(aff1);
3432 return aff2;
3435 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3436 return isl_aff_mul(aff2, aff1);
3438 if (!isl_aff_is_cst(aff2))
3439 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3440 "at least one affine expression should be constant",
3441 goto error);
3443 aff1 = isl_aff_cow(aff1);
3444 if (!aff1 || !aff2)
3445 goto error;
3447 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3448 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3450 isl_aff_free(aff2);
3451 return aff1;
3452 error:
3453 isl_aff_free(aff1);
3454 isl_aff_free(aff2);
3455 return NULL;
3458 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3460 * If either of the two is NaN, then the result is NaN.
3462 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3463 __isl_take isl_aff *aff2)
3465 int is_cst;
3466 int neg;
3468 if (!aff1 || !aff2)
3469 goto error;
3471 if (isl_aff_is_nan(aff1)) {
3472 isl_aff_free(aff2);
3473 return aff1;
3475 if (isl_aff_is_nan(aff2)) {
3476 isl_aff_free(aff1);
3477 return aff2;
3480 is_cst = isl_aff_is_cst(aff2);
3481 if (is_cst < 0)
3482 goto error;
3483 if (!is_cst)
3484 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3485 "second argument should be a constant", goto error);
3487 if (!aff2)
3488 goto error;
3490 neg = isl_int_is_neg(aff2->v->el[1]);
3491 if (neg) {
3492 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3493 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3496 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3497 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3499 if (neg) {
3500 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3501 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3504 isl_aff_free(aff2);
3505 return aff1;
3506 error:
3507 isl_aff_free(aff1);
3508 isl_aff_free(aff2);
3509 return NULL;
3512 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3513 __isl_take isl_pw_aff *pwaff2)
3515 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3518 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3519 __isl_take isl_pw_aff *pwaff2)
3521 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3524 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3525 __isl_take isl_pw_aff *pwaff2)
3527 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3530 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3531 __isl_take isl_pw_aff *pwaff2)
3533 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3536 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3537 __isl_take isl_pw_aff *pwaff2)
3539 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3542 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3543 __isl_take isl_pw_aff *pa2)
3545 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3548 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3550 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3551 __isl_take isl_pw_aff *pa2)
3553 int is_cst;
3555 is_cst = isl_pw_aff_is_cst(pa2);
3556 if (is_cst < 0)
3557 goto error;
3558 if (!is_cst)
3559 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3560 "second argument should be a piecewise constant",
3561 goto error);
3562 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3563 error:
3564 isl_pw_aff_free(pa1);
3565 isl_pw_aff_free(pa2);
3566 return NULL;
3569 /* Compute the quotient of the integer division of "pa1" by "pa2"
3570 * with rounding towards zero.
3571 * "pa2" is assumed to be a piecewise constant.
3573 * In particular, return
3575 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3578 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3579 __isl_take isl_pw_aff *pa2)
3581 int is_cst;
3582 isl_set *cond;
3583 isl_pw_aff *f, *c;
3585 is_cst = isl_pw_aff_is_cst(pa2);
3586 if (is_cst < 0)
3587 goto error;
3588 if (!is_cst)
3589 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3590 "second argument should be a piecewise constant",
3591 goto error);
3593 pa1 = isl_pw_aff_div(pa1, pa2);
3595 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3596 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3597 c = isl_pw_aff_ceil(pa1);
3598 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3599 error:
3600 isl_pw_aff_free(pa1);
3601 isl_pw_aff_free(pa2);
3602 return NULL;
3605 /* Compute the remainder of the integer division of "pa1" by "pa2"
3606 * with rounding towards zero.
3607 * "pa2" is assumed to be a piecewise constant.
3609 * In particular, return
3611 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3614 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3615 __isl_take isl_pw_aff *pa2)
3617 int is_cst;
3618 isl_pw_aff *res;
3620 is_cst = isl_pw_aff_is_cst(pa2);
3621 if (is_cst < 0)
3622 goto error;
3623 if (!is_cst)
3624 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3625 "second argument should be a piecewise constant",
3626 goto error);
3627 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3628 res = isl_pw_aff_mul(pa2, res);
3629 res = isl_pw_aff_sub(pa1, res);
3630 return res;
3631 error:
3632 isl_pw_aff_free(pa1);
3633 isl_pw_aff_free(pa2);
3634 return NULL;
3637 /* Does either of "pa1" or "pa2" involve any NaN2?
3639 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3640 __isl_keep isl_pw_aff *pa2)
3642 isl_bool has_nan;
3644 has_nan = isl_pw_aff_involves_nan(pa1);
3645 if (has_nan < 0 || has_nan)
3646 return has_nan;
3647 return isl_pw_aff_involves_nan(pa2);
3650 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3651 * by a NaN on their shared domain.
3653 * In principle, the result could be refined to only being NaN
3654 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3656 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3657 __isl_take isl_pw_aff *pa2)
3659 isl_local_space *ls;
3660 isl_set *dom;
3661 isl_pw_aff *pa;
3663 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3664 ls = isl_local_space_from_space(isl_set_get_space(dom));
3665 pa = isl_pw_aff_nan_on_domain(ls);
3666 pa = isl_pw_aff_intersect_domain(pa, dom);
3668 return pa;
3671 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3672 __isl_take isl_pw_aff *pwaff2)
3674 isl_set *le;
3675 isl_set *dom;
3677 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3678 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3679 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3680 isl_pw_aff_copy(pwaff2));
3681 dom = isl_set_subtract(dom, isl_set_copy(le));
3682 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3685 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3686 __isl_take isl_pw_aff *pwaff2)
3688 isl_set *ge;
3689 isl_set *dom;
3691 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3692 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3693 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3694 isl_pw_aff_copy(pwaff2));
3695 dom = isl_set_subtract(dom, isl_set_copy(ge));
3696 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3699 /* Return an expression for the minimum (if "max" is not set) or
3700 * the maximum (if "max" is set) of "pa1" and "pa2".
3701 * If either expression involves any NaN, then return a NaN
3702 * on the shared domain as result.
3704 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3705 __isl_take isl_pw_aff *pa2, int max)
3707 isl_bool has_nan;
3709 has_nan = either_involves_nan(pa1, pa2);
3710 if (has_nan < 0)
3711 pa1 = isl_pw_aff_free(pa1);
3712 else if (has_nan)
3713 return replace_by_nan(pa1, pa2);
3715 if (max)
3716 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3717 else
3718 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3721 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3723 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3724 __isl_take isl_pw_aff *pwaff2)
3726 return pw_aff_min_max(pwaff1, pwaff2, 0);
3729 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3731 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3732 __isl_take isl_pw_aff *pwaff2)
3734 return pw_aff_min_max(pwaff1, pwaff2, 1);
3737 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3738 __isl_take isl_pw_aff_list *list,
3739 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3740 __isl_take isl_pw_aff *pwaff2))
3742 int i;
3743 isl_ctx *ctx;
3744 isl_pw_aff *res;
3746 if (!list)
3747 return NULL;
3749 ctx = isl_pw_aff_list_get_ctx(list);
3750 if (list->n < 1)
3751 isl_die(ctx, isl_error_invalid,
3752 "list should contain at least one element", goto error);
3754 res = isl_pw_aff_copy(list->p[0]);
3755 for (i = 1; i < list->n; ++i)
3756 res = fn(res, isl_pw_aff_copy(list->p[i]));
3758 isl_pw_aff_list_free(list);
3759 return res;
3760 error:
3761 isl_pw_aff_list_free(list);
3762 return NULL;
3765 /* Return an isl_pw_aff that maps each element in the intersection of the
3766 * domains of the elements of list to the minimal corresponding affine
3767 * expression.
3769 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3771 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3774 /* Return an isl_pw_aff that maps each element in the intersection of the
3775 * domains of the elements of list to the maximal corresponding affine
3776 * expression.
3778 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3780 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3783 /* Mark the domains of "pwaff" as rational.
3785 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3787 int i;
3789 pwaff = isl_pw_aff_cow(pwaff);
3790 if (!pwaff)
3791 return NULL;
3792 if (pwaff->n == 0)
3793 return pwaff;
3795 for (i = 0; i < pwaff->n; ++i) {
3796 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3797 if (!pwaff->p[i].set)
3798 return isl_pw_aff_free(pwaff);
3801 return pwaff;
3804 /* Mark the domains of the elements of "list" as rational.
3806 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3807 __isl_take isl_pw_aff_list *list)
3809 int i, n;
3811 if (!list)
3812 return NULL;
3813 if (list->n == 0)
3814 return list;
3816 n = list->n;
3817 for (i = 0; i < n; ++i) {
3818 isl_pw_aff *pa;
3820 pa = isl_pw_aff_list_get_pw_aff(list, i);
3821 pa = isl_pw_aff_set_rational(pa);
3822 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3825 return list;
3828 /* Do the parameters of "aff" match those of "space"?
3830 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3831 __isl_keep isl_space *space)
3833 isl_space *aff_space;
3834 isl_bool match;
3836 if (!aff || !space)
3837 return isl_bool_error;
3839 aff_space = isl_aff_get_domain_space(aff);
3841 match = isl_space_has_equal_params(space, aff_space);
3843 isl_space_free(aff_space);
3844 return match;
3847 /* Check that the domain space of "aff" matches "space".
3849 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3850 __isl_keep isl_space *space)
3852 isl_space *aff_space;
3853 isl_bool match;
3855 if (!aff || !space)
3856 return isl_stat_error;
3858 aff_space = isl_aff_get_domain_space(aff);
3860 match = isl_space_has_equal_params(space, aff_space);
3861 if (match < 0)
3862 goto error;
3863 if (!match)
3864 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3865 "parameters don't match", goto error);
3866 match = isl_space_tuple_is_equal(space, isl_dim_in,
3867 aff_space, isl_dim_set);
3868 if (match < 0)
3869 goto error;
3870 if (!match)
3871 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3872 "domains don't match", goto error);
3873 isl_space_free(aff_space);
3874 return isl_stat_ok;
3875 error:
3876 isl_space_free(aff_space);
3877 return isl_stat_error;
3880 #undef BASE
3881 #define BASE aff
3882 #undef DOMBASE
3883 #define DOMBASE set
3884 #define NO_DOMAIN
3886 #include <isl_multi_no_explicit_domain.c>
3887 #include <isl_multi_templ.c>
3888 #include <isl_multi_apply_set.c>
3889 #include <isl_multi_cmp.c>
3890 #include <isl_multi_dims.c>
3891 #include <isl_multi_floor.c>
3892 #include <isl_multi_gist.c>
3894 #undef NO_DOMAIN
3896 /* Construct an isl_multi_aff living in "space" that corresponds
3897 * to the affine transformation matrix "mat".
3899 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3900 __isl_take isl_space *space, __isl_take isl_mat *mat)
3902 isl_ctx *ctx;
3903 isl_local_space *ls = NULL;
3904 isl_multi_aff *ma = NULL;
3905 int n_row, n_col, n_out, total;
3906 int i;
3908 if (!space || !mat)
3909 goto error;
3911 ctx = isl_mat_get_ctx(mat);
3913 n_row = isl_mat_rows(mat);
3914 n_col = isl_mat_cols(mat);
3915 if (n_row < 1)
3916 isl_die(ctx, isl_error_invalid,
3917 "insufficient number of rows", goto error);
3918 if (n_col < 1)
3919 isl_die(ctx, isl_error_invalid,
3920 "insufficient number of columns", goto error);
3921 n_out = isl_space_dim(space, isl_dim_out);
3922 total = isl_space_dim(space, isl_dim_all);
3923 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3924 isl_die(ctx, isl_error_invalid,
3925 "dimension mismatch", goto error);
3927 ma = isl_multi_aff_zero(isl_space_copy(space));
3928 ls = isl_local_space_from_space(isl_space_domain(space));
3930 for (i = 0; i < n_row - 1; ++i) {
3931 isl_vec *v;
3932 isl_aff *aff;
3934 v = isl_vec_alloc(ctx, 1 + n_col);
3935 if (!v)
3936 goto error;
3937 isl_int_set(v->el[0], mat->row[0][0]);
3938 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3939 v = isl_vec_normalize(v);
3940 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3941 ma = isl_multi_aff_set_aff(ma, i, aff);
3944 isl_local_space_free(ls);
3945 isl_mat_free(mat);
3946 return ma;
3947 error:
3948 isl_local_space_free(ls);
3949 isl_mat_free(mat);
3950 isl_multi_aff_free(ma);
3951 return NULL;
3954 /* Remove any internal structure of the domain of "ma".
3955 * If there is any such internal structure in the input,
3956 * then the name of the corresponding space is also removed.
3958 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3959 __isl_take isl_multi_aff *ma)
3961 isl_space *space;
3963 if (!ma)
3964 return NULL;
3966 if (!ma->space->nested[0])
3967 return ma;
3969 space = isl_multi_aff_get_space(ma);
3970 space = isl_space_flatten_domain(space);
3971 ma = isl_multi_aff_reset_space(ma, space);
3973 return ma;
3976 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3977 * of the space to its domain.
3979 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3981 int i, n_in;
3982 isl_local_space *ls;
3983 isl_multi_aff *ma;
3985 if (!space)
3986 return NULL;
3987 if (!isl_space_is_map(space))
3988 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3989 "not a map space", goto error);
3991 n_in = isl_space_dim(space, isl_dim_in);
3992 space = isl_space_domain_map(space);
3994 ma = isl_multi_aff_alloc(isl_space_copy(space));
3995 if (n_in == 0) {
3996 isl_space_free(space);
3997 return ma;
4000 space = isl_space_domain(space);
4001 ls = isl_local_space_from_space(space);
4002 for (i = 0; i < n_in; ++i) {
4003 isl_aff *aff;
4005 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4006 isl_dim_set, i);
4007 ma = isl_multi_aff_set_aff(ma, i, aff);
4009 isl_local_space_free(ls);
4010 return ma;
4011 error:
4012 isl_space_free(space);
4013 return NULL;
4016 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4017 * of the space to its range.
4019 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4021 int i, n_in, n_out;
4022 isl_local_space *ls;
4023 isl_multi_aff *ma;
4025 if (!space)
4026 return NULL;
4027 if (!isl_space_is_map(space))
4028 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4029 "not a map space", goto error);
4031 n_in = isl_space_dim(space, isl_dim_in);
4032 n_out = isl_space_dim(space, isl_dim_out);
4033 space = isl_space_range_map(space);
4035 ma = isl_multi_aff_alloc(isl_space_copy(space));
4036 if (n_out == 0) {
4037 isl_space_free(space);
4038 return ma;
4041 space = isl_space_domain(space);
4042 ls = isl_local_space_from_space(space);
4043 for (i = 0; i < n_out; ++i) {
4044 isl_aff *aff;
4046 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4047 isl_dim_set, n_in + i);
4048 ma = isl_multi_aff_set_aff(ma, i, aff);
4050 isl_local_space_free(ls);
4051 return ma;
4052 error:
4053 isl_space_free(space);
4054 return NULL;
4057 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4058 * of the space to its range.
4060 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4061 __isl_take isl_space *space)
4063 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4066 /* Given the space of a set and a range of set dimensions,
4067 * construct an isl_multi_aff that projects out those dimensions.
4069 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4070 __isl_take isl_space *space, enum isl_dim_type type,
4071 unsigned first, unsigned n)
4073 int i, dim;
4074 isl_local_space *ls;
4075 isl_multi_aff *ma;
4077 if (!space)
4078 return NULL;
4079 if (!isl_space_is_set(space))
4080 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4081 "expecting set space", goto error);
4082 if (type != isl_dim_set)
4083 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4084 "only set dimensions can be projected out", goto error);
4086 dim = isl_space_dim(space, isl_dim_set);
4087 if (first + n > dim)
4088 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4089 "range out of bounds", goto error);
4091 space = isl_space_from_domain(space);
4092 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4094 if (dim == n)
4095 return isl_multi_aff_alloc(space);
4097 ma = isl_multi_aff_alloc(isl_space_copy(space));
4098 space = isl_space_domain(space);
4099 ls = isl_local_space_from_space(space);
4101 for (i = 0; i < first; ++i) {
4102 isl_aff *aff;
4104 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4105 isl_dim_set, i);
4106 ma = isl_multi_aff_set_aff(ma, i, aff);
4109 for (i = 0; i < dim - (first + n); ++i) {
4110 isl_aff *aff;
4112 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4113 isl_dim_set, first + n + i);
4114 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4117 isl_local_space_free(ls);
4118 return ma;
4119 error:
4120 isl_space_free(space);
4121 return NULL;
4124 /* Given the space of a set and a range of set dimensions,
4125 * construct an isl_pw_multi_aff that projects out those dimensions.
4127 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4128 __isl_take isl_space *space, enum isl_dim_type type,
4129 unsigned first, unsigned n)
4131 isl_multi_aff *ma;
4133 ma = isl_multi_aff_project_out_map(space, type, first, n);
4134 return isl_pw_multi_aff_from_multi_aff(ma);
4137 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4138 * domain.
4140 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4141 __isl_take isl_multi_aff *ma)
4143 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4144 return isl_pw_multi_aff_alloc(dom, ma);
4147 /* Create a piecewise multi-affine expression in the given space that maps each
4148 * input dimension to the corresponding output dimension.
4150 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4151 __isl_take isl_space *space)
4153 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4156 /* Exploit the equalities in "eq" to simplify the affine expressions.
4158 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4159 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4161 int i;
4163 maff = isl_multi_aff_cow(maff);
4164 if (!maff || !eq)
4165 goto error;
4167 for (i = 0; i < maff->n; ++i) {
4168 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4169 isl_basic_set_copy(eq));
4170 if (!maff->u.p[i])
4171 goto error;
4174 isl_basic_set_free(eq);
4175 return maff;
4176 error:
4177 isl_basic_set_free(eq);
4178 isl_multi_aff_free(maff);
4179 return NULL;
4182 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4183 isl_int f)
4185 int i;
4187 maff = isl_multi_aff_cow(maff);
4188 if (!maff)
4189 return NULL;
4191 for (i = 0; i < maff->n; ++i) {
4192 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4193 if (!maff->u.p[i])
4194 return isl_multi_aff_free(maff);
4197 return maff;
4200 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4201 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4203 maff1 = isl_multi_aff_add(maff1, maff2);
4204 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4205 return maff1;
4208 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4210 if (!maff)
4211 return -1;
4213 return 0;
4216 /* Return the set of domain elements where "ma1" is lexicographically
4217 * smaller than or equal to "ma2".
4219 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4220 __isl_take isl_multi_aff *ma2)
4222 return isl_multi_aff_lex_ge_set(ma2, ma1);
4225 /* Return the set of domain elements where "ma1" is lexicographically
4226 * smaller than "ma2".
4228 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4229 __isl_take isl_multi_aff *ma2)
4231 return isl_multi_aff_lex_gt_set(ma2, ma1);
4234 /* Return the set of domain elements where "ma1" and "ma2"
4235 * satisfy "order".
4237 static __isl_give isl_set *isl_multi_aff_order_set(
4238 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4239 __isl_give isl_map *order(__isl_take isl_space *set_space))
4241 isl_space *space;
4242 isl_map *map1, *map2;
4243 isl_map *map, *ge;
4245 map1 = isl_map_from_multi_aff(ma1);
4246 map2 = isl_map_from_multi_aff(ma2);
4247 map = isl_map_range_product(map1, map2);
4248 space = isl_space_range(isl_map_get_space(map));
4249 space = isl_space_domain(isl_space_unwrap(space));
4250 ge = order(space);
4251 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4253 return isl_map_domain(map);
4256 /* Return the set of domain elements where "ma1" is lexicographically
4257 * greater than or equal to "ma2".
4259 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4260 __isl_take isl_multi_aff *ma2)
4262 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4265 /* Return the set of domain elements where "ma1" is lexicographically
4266 * greater than "ma2".
4268 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4269 __isl_take isl_multi_aff *ma2)
4271 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4274 #undef PW
4275 #define PW isl_pw_multi_aff
4276 #undef EL
4277 #define EL isl_multi_aff
4278 #undef EL_IS_ZERO
4279 #define EL_IS_ZERO is_empty
4280 #undef ZERO
4281 #define ZERO empty
4282 #undef IS_ZERO
4283 #define IS_ZERO is_empty
4284 #undef FIELD
4285 #define FIELD maff
4286 #undef DEFAULT_IS_ZERO
4287 #define DEFAULT_IS_ZERO 0
4289 #define NO_SUB
4290 #define NO_OPT
4291 #define NO_INSERT_DIMS
4292 #define NO_LIFT
4293 #define NO_MORPH
4295 #include <isl_pw_templ.c>
4296 #include <isl_pw_union_opt.c>
4298 #undef NO_SUB
4300 #undef UNION
4301 #define UNION isl_union_pw_multi_aff
4302 #undef PART
4303 #define PART isl_pw_multi_aff
4304 #undef PARTS
4305 #define PARTS pw_multi_aff
4307 #include <isl_union_multi.c>
4308 #include <isl_union_neg.c>
4310 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4311 __isl_take isl_pw_multi_aff *pma1,
4312 __isl_take isl_pw_multi_aff *pma2)
4314 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4315 &isl_multi_aff_lex_ge_set);
4318 /* Given two piecewise multi affine expressions, return a piecewise
4319 * multi-affine expression defined on the union of the definition domains
4320 * of the inputs that is equal to the lexicographic maximum of the two
4321 * inputs on each cell. If only one of the two inputs is defined on
4322 * a given cell, then it is considered to be the maximum.
4324 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4325 __isl_take isl_pw_multi_aff *pma1,
4326 __isl_take isl_pw_multi_aff *pma2)
4328 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4329 &pw_multi_aff_union_lexmax);
4332 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4333 __isl_take isl_pw_multi_aff *pma1,
4334 __isl_take isl_pw_multi_aff *pma2)
4336 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4337 &isl_multi_aff_lex_le_set);
4340 /* Given two piecewise multi affine expressions, return a piecewise
4341 * multi-affine expression defined on the union of the definition domains
4342 * of the inputs that is equal to the lexicographic minimum of the two
4343 * inputs on each cell. If only one of the two inputs is defined on
4344 * a given cell, then it is considered to be the minimum.
4346 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4347 __isl_take isl_pw_multi_aff *pma1,
4348 __isl_take isl_pw_multi_aff *pma2)
4350 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4351 &pw_multi_aff_union_lexmin);
4354 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4355 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4357 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4358 &isl_multi_aff_add);
4361 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4362 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4364 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4365 &pw_multi_aff_add);
4368 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4369 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4371 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4372 &isl_multi_aff_sub);
4375 /* Subtract "pma2" from "pma1" and return the result.
4377 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4378 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4380 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4381 &pw_multi_aff_sub);
4384 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4385 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4387 return isl_pw_multi_aff_union_add_(pma1, pma2);
4390 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4391 * with the actual sum on the shared domain and
4392 * the defined expression on the symmetric difference of the domains.
4394 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4395 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4397 return isl_union_pw_aff_union_add_(upa1, upa2);
4400 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4401 * with the actual sum on the shared domain and
4402 * the defined expression on the symmetric difference of the domains.
4404 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4405 __isl_take isl_union_pw_multi_aff *upma1,
4406 __isl_take isl_union_pw_multi_aff *upma2)
4408 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4411 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4412 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4414 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4415 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4417 int i, j, n;
4418 isl_space *space;
4419 isl_pw_multi_aff *res;
4421 if (!pma1 || !pma2)
4422 goto error;
4424 n = pma1->n * pma2->n;
4425 space = isl_space_product(isl_space_copy(pma1->dim),
4426 isl_space_copy(pma2->dim));
4427 res = isl_pw_multi_aff_alloc_size(space, n);
4429 for (i = 0; i < pma1->n; ++i) {
4430 for (j = 0; j < pma2->n; ++j) {
4431 isl_set *domain;
4432 isl_multi_aff *ma;
4434 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4435 isl_set_copy(pma2->p[j].set));
4436 ma = isl_multi_aff_product(
4437 isl_multi_aff_copy(pma1->p[i].maff),
4438 isl_multi_aff_copy(pma2->p[j].maff));
4439 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4443 isl_pw_multi_aff_free(pma1);
4444 isl_pw_multi_aff_free(pma2);
4445 return res;
4446 error:
4447 isl_pw_multi_aff_free(pma1);
4448 isl_pw_multi_aff_free(pma2);
4449 return NULL;
4452 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4453 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4455 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4456 &pw_multi_aff_product);
4459 /* Construct a map mapping the domain of the piecewise multi-affine expression
4460 * to its range, with each dimension in the range equated to the
4461 * corresponding affine expression on its cell.
4463 * If the domain of "pma" is rational, then so is the constructed "map".
4465 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4467 int i;
4468 isl_map *map;
4470 if (!pma)
4471 return NULL;
4473 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4475 for (i = 0; i < pma->n; ++i) {
4476 isl_bool rational;
4477 isl_multi_aff *maff;
4478 isl_basic_map *bmap;
4479 isl_map *map_i;
4481 rational = isl_set_is_rational(pma->p[i].set);
4482 if (rational < 0)
4483 map = isl_map_free(map);
4484 maff = isl_multi_aff_copy(pma->p[i].maff);
4485 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4486 map_i = isl_map_from_basic_map(bmap);
4487 map_i = isl_map_intersect_domain(map_i,
4488 isl_set_copy(pma->p[i].set));
4489 map = isl_map_union_disjoint(map, map_i);
4492 isl_pw_multi_aff_free(pma);
4493 return map;
4496 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4498 if (!pma)
4499 return NULL;
4501 if (!isl_space_is_set(pma->dim))
4502 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4503 "isl_pw_multi_aff cannot be converted into an isl_set",
4504 goto error);
4506 return isl_map_from_pw_multi_aff(pma);
4507 error:
4508 isl_pw_multi_aff_free(pma);
4509 return NULL;
4512 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4513 * denominator "denom".
4514 * "denom" is allowed to be negative, in which case the actual denominator
4515 * is -denom and the expressions are added instead.
4517 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4518 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4520 int i, first;
4521 int sign;
4522 isl_int d;
4524 first = isl_seq_first_non_zero(c, n);
4525 if (first == -1)
4526 return aff;
4528 sign = isl_int_sgn(denom);
4529 isl_int_init(d);
4530 isl_int_abs(d, denom);
4531 for (i = first; i < n; ++i) {
4532 isl_aff *aff_i;
4534 if (isl_int_is_zero(c[i]))
4535 continue;
4536 aff_i = isl_multi_aff_get_aff(ma, i);
4537 aff_i = isl_aff_scale(aff_i, c[i]);
4538 aff_i = isl_aff_scale_down(aff_i, d);
4539 if (sign >= 0)
4540 aff = isl_aff_sub(aff, aff_i);
4541 else
4542 aff = isl_aff_add(aff, aff_i);
4544 isl_int_clear(d);
4546 return aff;
4549 /* Extract an affine expression that expresses the output dimension "pos"
4550 * of "bmap" in terms of the parameters and input dimensions from
4551 * equality "eq".
4552 * Note that this expression may involve integer divisions defined
4553 * in terms of parameters and input dimensions.
4554 * The equality may also involve references to earlier (but not later)
4555 * output dimensions. These are replaced by the corresponding elements
4556 * in "ma".
4558 * If the equality is of the form
4560 * f(i) + h(j) + a x + g(i) = 0,
4562 * with f(i) a linear combinations of the parameters and input dimensions,
4563 * g(i) a linear combination of integer divisions defined in terms of the same
4564 * and h(j) a linear combinations of earlier output dimensions,
4565 * then the affine expression is
4567 * (-f(i) - g(i))/a - h(j)/a
4569 * If the equality is of the form
4571 * f(i) + h(j) - a x + g(i) = 0,
4573 * then the affine expression is
4575 * (f(i) + g(i))/a - h(j)/(-a)
4578 * If "div" refers to an integer division (i.e., it is smaller than
4579 * the number of integer divisions), then the equality constraint
4580 * does involve an integer division (the one at position "div") that
4581 * is defined in terms of output dimensions. However, this integer
4582 * division can be eliminated by exploiting a pair of constraints
4583 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4584 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4585 * -l + x >= 0.
4586 * In particular, let
4588 * x = e(i) + m floor(...)
4590 * with e(i) the expression derived above and floor(...) the integer
4591 * division involving output dimensions.
4592 * From
4594 * l <= x <= l + n,
4596 * we have
4598 * 0 <= x - l <= n
4600 * This means
4602 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4603 * = (e(i) - l) mod m
4605 * Therefore,
4607 * x - l = (e(i) - l) mod m
4609 * or
4611 * x = ((e(i) - l) mod m) + l
4613 * The variable "shift" below contains the expression -l, which may
4614 * also involve a linear combination of earlier output dimensions.
4616 static __isl_give isl_aff *extract_aff_from_equality(
4617 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4618 __isl_keep isl_multi_aff *ma)
4620 unsigned o_out;
4621 unsigned n_div, n_out;
4622 isl_ctx *ctx;
4623 isl_local_space *ls;
4624 isl_aff *aff, *shift;
4625 isl_val *mod;
4627 ctx = isl_basic_map_get_ctx(bmap);
4628 ls = isl_basic_map_get_local_space(bmap);
4629 ls = isl_local_space_domain(ls);
4630 aff = isl_aff_alloc(isl_local_space_copy(ls));
4631 if (!aff)
4632 goto error;
4633 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4634 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4635 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4636 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4637 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4638 isl_seq_cpy(aff->v->el + 1 + o_out,
4639 bmap->eq[eq] + o_out + n_out, n_div);
4640 } else {
4641 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4642 isl_seq_neg(aff->v->el + 1 + o_out,
4643 bmap->eq[eq] + o_out + n_out, n_div);
4645 if (div < n_div)
4646 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4647 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4648 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4649 bmap->eq[eq][o_out + pos]);
4650 if (div < n_div) {
4651 shift = isl_aff_alloc(isl_local_space_copy(ls));
4652 if (!shift)
4653 goto error;
4654 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4655 isl_seq_cpy(shift->v->el + 1 + o_out,
4656 bmap->ineq[ineq] + o_out + n_out, n_div);
4657 isl_int_set_si(shift->v->el[0], 1);
4658 shift = subtract_initial(shift, ma, pos,
4659 bmap->ineq[ineq] + o_out, ctx->negone);
4660 aff = isl_aff_add(aff, isl_aff_copy(shift));
4661 mod = isl_val_int_from_isl_int(ctx,
4662 bmap->eq[eq][o_out + n_out + div]);
4663 mod = isl_val_abs(mod);
4664 aff = isl_aff_mod_val(aff, mod);
4665 aff = isl_aff_sub(aff, shift);
4668 isl_local_space_free(ls);
4669 return aff;
4670 error:
4671 isl_local_space_free(ls);
4672 isl_aff_free(aff);
4673 return NULL;
4676 /* Given a basic map with output dimensions defined
4677 * in terms of the parameters input dimensions and earlier
4678 * output dimensions using an equality (and possibly a pair on inequalities),
4679 * extract an isl_aff that expresses output dimension "pos" in terms
4680 * of the parameters and input dimensions.
4681 * Note that this expression may involve integer divisions defined
4682 * in terms of parameters and input dimensions.
4683 * "ma" contains the expressions corresponding to earlier output dimensions.
4685 * This function shares some similarities with
4686 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4688 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4689 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4691 int eq, div, ineq;
4692 isl_aff *aff;
4694 if (!bmap)
4695 return NULL;
4696 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4697 if (eq >= bmap->n_eq)
4698 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4699 "unable to find suitable equality", return NULL);
4700 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4702 aff = isl_aff_remove_unused_divs(aff);
4703 return aff;
4706 /* Given a basic map where each output dimension is defined
4707 * in terms of the parameters and input dimensions using an equality,
4708 * extract an isl_multi_aff that expresses the output dimensions in terms
4709 * of the parameters and input dimensions.
4711 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4712 __isl_take isl_basic_map *bmap)
4714 int i;
4715 unsigned n_out;
4716 isl_multi_aff *ma;
4718 if (!bmap)
4719 return NULL;
4721 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4722 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4724 for (i = 0; i < n_out; ++i) {
4725 isl_aff *aff;
4727 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4728 ma = isl_multi_aff_set_aff(ma, i, aff);
4731 isl_basic_map_free(bmap);
4733 return ma;
4736 /* Given a basic set where each set dimension is defined
4737 * in terms of the parameters using an equality,
4738 * extract an isl_multi_aff that expresses the set dimensions in terms
4739 * of the parameters.
4741 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4742 __isl_take isl_basic_set *bset)
4744 return extract_isl_multi_aff_from_basic_map(bset);
4747 /* Create an isl_pw_multi_aff that is equivalent to
4748 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4749 * The given basic map is such that each output dimension is defined
4750 * in terms of the parameters and input dimensions using an equality.
4752 * Since some applications expect the result of isl_pw_multi_aff_from_map
4753 * to only contain integer affine expressions, we compute the floor
4754 * of the expression before returning.
4756 * Remove all constraints involving local variables without
4757 * an explicit representation (resulting in the removal of those
4758 * local variables) prior to the actual extraction to ensure
4759 * that the local spaces in which the resulting affine expressions
4760 * are created do not contain any unknown local variables.
4761 * Removing such constraints is safe because constraints involving
4762 * unknown local variables are not used to determine whether
4763 * a basic map is obviously single-valued.
4765 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4766 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4768 isl_multi_aff *ma;
4770 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4771 ma = extract_isl_multi_aff_from_basic_map(bmap);
4772 ma = isl_multi_aff_floor(ma);
4773 return isl_pw_multi_aff_alloc(domain, ma);
4776 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4777 * This obviously only works if the input "map" is single-valued.
4778 * If so, we compute the lexicographic minimum of the image in the form
4779 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4780 * to its lexicographic minimum.
4781 * If the input is not single-valued, we produce an error.
4783 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4784 __isl_take isl_map *map)
4786 int i;
4787 int sv;
4788 isl_pw_multi_aff *pma;
4790 sv = isl_map_is_single_valued(map);
4791 if (sv < 0)
4792 goto error;
4793 if (!sv)
4794 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4795 "map is not single-valued", goto error);
4796 map = isl_map_make_disjoint(map);
4797 if (!map)
4798 return NULL;
4800 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4802 for (i = 0; i < map->n; ++i) {
4803 isl_pw_multi_aff *pma_i;
4804 isl_basic_map *bmap;
4805 bmap = isl_basic_map_copy(map->p[i]);
4806 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4807 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4810 isl_map_free(map);
4811 return pma;
4812 error:
4813 isl_map_free(map);
4814 return NULL;
4817 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4818 * taking into account that the output dimension at position "d"
4819 * can be represented as
4821 * x = floor((e(...) + c1) / m)
4823 * given that constraint "i" is of the form
4825 * e(...) + c1 - m x >= 0
4828 * Let "map" be of the form
4830 * A -> B
4832 * We construct a mapping
4834 * A -> [A -> x = floor(...)]
4836 * apply that to the map, obtaining
4838 * [A -> x = floor(...)] -> B
4840 * and equate dimension "d" to x.
4841 * We then compute a isl_pw_multi_aff representation of the resulting map
4842 * and plug in the mapping above.
4844 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4845 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4847 isl_ctx *ctx;
4848 isl_space *space;
4849 isl_local_space *ls;
4850 isl_multi_aff *ma;
4851 isl_aff *aff;
4852 isl_vec *v;
4853 isl_map *insert;
4854 int offset;
4855 int n;
4856 int n_in;
4857 isl_pw_multi_aff *pma;
4858 isl_bool is_set;
4860 is_set = isl_map_is_set(map);
4861 if (is_set < 0)
4862 goto error;
4864 offset = isl_basic_map_offset(hull, isl_dim_out);
4865 ctx = isl_map_get_ctx(map);
4866 space = isl_space_domain(isl_map_get_space(map));
4867 n_in = isl_space_dim(space, isl_dim_set);
4868 n = isl_space_dim(space, isl_dim_all);
4870 v = isl_vec_alloc(ctx, 1 + 1 + n);
4871 if (v) {
4872 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4873 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4875 isl_basic_map_free(hull);
4877 ls = isl_local_space_from_space(isl_space_copy(space));
4878 aff = isl_aff_alloc_vec(ls, v);
4879 aff = isl_aff_floor(aff);
4880 if (is_set) {
4881 isl_space_free(space);
4882 ma = isl_multi_aff_from_aff(aff);
4883 } else {
4884 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4885 ma = isl_multi_aff_range_product(ma,
4886 isl_multi_aff_from_aff(aff));
4889 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4890 map = isl_map_apply_domain(map, insert);
4891 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4892 pma = isl_pw_multi_aff_from_map(map);
4893 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4895 return pma;
4896 error:
4897 isl_map_free(map);
4898 isl_basic_map_free(hull);
4899 return NULL;
4902 /* Is constraint "c" of the form
4904 * e(...) + c1 - m x >= 0
4906 * or
4908 * -e(...) + c2 + m x >= 0
4910 * where m > 1 and e only depends on parameters and input dimemnsions?
4912 * "offset" is the offset of the output dimensions
4913 * "pos" is the position of output dimension x.
4915 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4917 if (isl_int_is_zero(c[offset + d]))
4918 return 0;
4919 if (isl_int_is_one(c[offset + d]))
4920 return 0;
4921 if (isl_int_is_negone(c[offset + d]))
4922 return 0;
4923 if (isl_seq_first_non_zero(c + offset, d) != -1)
4924 return 0;
4925 if (isl_seq_first_non_zero(c + offset + d + 1,
4926 total - (offset + d + 1)) != -1)
4927 return 0;
4928 return 1;
4931 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4933 * As a special case, we first check if there is any pair of constraints,
4934 * shared by all the basic maps in "map" that force a given dimension
4935 * to be equal to the floor of some affine combination of the input dimensions.
4937 * In particular, if we can find two constraints
4939 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4941 * and
4943 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4945 * where m > 1 and e only depends on parameters and input dimemnsions,
4946 * and such that
4948 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4950 * then we know that we can take
4952 * x = floor((e(...) + c1) / m)
4954 * without having to perform any computation.
4956 * Note that we know that
4958 * c1 + c2 >= 1
4960 * If c1 + c2 were 0, then we would have detected an equality during
4961 * simplification. If c1 + c2 were negative, then we would have detected
4962 * a contradiction.
4964 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4965 __isl_take isl_map *map)
4967 int d, dim;
4968 int i, j, n;
4969 int offset, total;
4970 isl_int sum;
4971 isl_basic_map *hull;
4973 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4974 if (!hull)
4975 goto error;
4977 isl_int_init(sum);
4978 dim = isl_map_dim(map, isl_dim_out);
4979 offset = isl_basic_map_offset(hull, isl_dim_out);
4980 total = 1 + isl_basic_map_total_dim(hull);
4981 n = hull->n_ineq;
4982 for (d = 0; d < dim; ++d) {
4983 for (i = 0; i < n; ++i) {
4984 if (!is_potential_div_constraint(hull->ineq[i],
4985 offset, d, total))
4986 continue;
4987 for (j = i + 1; j < n; ++j) {
4988 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4989 hull->ineq[j] + 1, total - 1))
4990 continue;
4991 isl_int_add(sum, hull->ineq[i][0],
4992 hull->ineq[j][0]);
4993 if (isl_int_abs_lt(sum,
4994 hull->ineq[i][offset + d]))
4995 break;
4998 if (j >= n)
4999 continue;
5000 isl_int_clear(sum);
5001 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5002 j = i;
5003 return pw_multi_aff_from_map_div(map, hull, d, j);
5006 isl_int_clear(sum);
5007 isl_basic_map_free(hull);
5008 return pw_multi_aff_from_map_base(map);
5009 error:
5010 isl_map_free(map);
5011 isl_basic_map_free(hull);
5012 return NULL;
5015 /* Given an affine expression
5017 * [A -> B] -> f(A,B)
5019 * construct an isl_multi_aff
5021 * [A -> B] -> B'
5023 * such that dimension "d" in B' is set to "aff" and the remaining
5024 * dimensions are set equal to the corresponding dimensions in B.
5025 * "n_in" is the dimension of the space A.
5026 * "n_out" is the dimension of the space B.
5028 * If "is_set" is set, then the affine expression is of the form
5030 * [B] -> f(B)
5032 * and we construct an isl_multi_aff
5034 * B -> B'
5036 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5037 unsigned n_in, unsigned n_out, int is_set)
5039 int i;
5040 isl_multi_aff *ma;
5041 isl_space *space, *space2;
5042 isl_local_space *ls;
5044 space = isl_aff_get_domain_space(aff);
5045 ls = isl_local_space_from_space(isl_space_copy(space));
5046 space2 = isl_space_copy(space);
5047 if (!is_set)
5048 space2 = isl_space_range(isl_space_unwrap(space2));
5049 space = isl_space_map_from_domain_and_range(space, space2);
5050 ma = isl_multi_aff_alloc(space);
5051 ma = isl_multi_aff_set_aff(ma, d, aff);
5053 for (i = 0; i < n_out; ++i) {
5054 if (i == d)
5055 continue;
5056 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5057 isl_dim_set, n_in + i);
5058 ma = isl_multi_aff_set_aff(ma, i, aff);
5061 isl_local_space_free(ls);
5063 return ma;
5066 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5067 * taking into account that the dimension at position "d" can be written as
5069 * x = m a + f(..) (1)
5071 * where m is equal to "gcd".
5072 * "i" is the index of the equality in "hull" that defines f(..).
5073 * In particular, the equality is of the form
5075 * f(..) - x + m g(existentials) = 0
5077 * or
5079 * -f(..) + x + m g(existentials) = 0
5081 * We basically plug (1) into "map", resulting in a map with "a"
5082 * in the range instead of "x". The corresponding isl_pw_multi_aff
5083 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5085 * Specifically, given the input map
5087 * A -> B
5089 * We first wrap it into a set
5091 * [A -> B]
5093 * and define (1) on top of the corresponding space, resulting in "aff".
5094 * We use this to create an isl_multi_aff that maps the output position "d"
5095 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5096 * We plug this into the wrapped map, unwrap the result and compute the
5097 * corresponding isl_pw_multi_aff.
5098 * The result is an expression
5100 * A -> T(A)
5102 * We adjust that to
5104 * A -> [A -> T(A)]
5106 * so that we can plug that into "aff", after extending the latter to
5107 * a mapping
5109 * [A -> B] -> B'
5112 * If "map" is actually a set, then there is no "A" space, meaning
5113 * that we do not need to perform any wrapping, and that the result
5114 * of the recursive call is of the form
5116 * [T]
5118 * which is plugged into a mapping of the form
5120 * B -> B'
5122 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5123 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5124 isl_int gcd)
5126 isl_set *set;
5127 isl_space *space;
5128 isl_local_space *ls;
5129 isl_aff *aff;
5130 isl_multi_aff *ma;
5131 isl_pw_multi_aff *pma, *id;
5132 unsigned n_in;
5133 unsigned o_out;
5134 unsigned n_out;
5135 isl_bool is_set;
5137 is_set = isl_map_is_set(map);
5138 if (is_set < 0)
5139 goto error;
5141 n_in = isl_basic_map_dim(hull, isl_dim_in);
5142 n_out = isl_basic_map_dim(hull, isl_dim_out);
5143 o_out = isl_basic_map_offset(hull, isl_dim_out);
5145 if (is_set)
5146 set = map;
5147 else
5148 set = isl_map_wrap(map);
5149 space = isl_space_map_from_set(isl_set_get_space(set));
5150 ma = isl_multi_aff_identity(space);
5151 ls = isl_local_space_from_space(isl_set_get_space(set));
5152 aff = isl_aff_alloc(ls);
5153 if (aff) {
5154 isl_int_set_si(aff->v->el[0], 1);
5155 if (isl_int_is_one(hull->eq[i][o_out + d]))
5156 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5157 aff->v->size - 1);
5158 else
5159 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5160 aff->v->size - 1);
5161 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5163 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5164 set = isl_set_preimage_multi_aff(set, ma);
5166 ma = range_map(aff, d, n_in, n_out, is_set);
5168 if (is_set)
5169 map = set;
5170 else
5171 map = isl_set_unwrap(set);
5172 pma = isl_pw_multi_aff_from_map(map);
5174 if (!is_set) {
5175 space = isl_pw_multi_aff_get_domain_space(pma);
5176 space = isl_space_map_from_set(space);
5177 id = isl_pw_multi_aff_identity(space);
5178 pma = isl_pw_multi_aff_range_product(id, pma);
5180 id = isl_pw_multi_aff_from_multi_aff(ma);
5181 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5183 isl_basic_map_free(hull);
5184 return pma;
5185 error:
5186 isl_map_free(map);
5187 isl_basic_map_free(hull);
5188 return NULL;
5191 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5192 * "hull" contains the equalities valid for "map".
5194 * Check if any of the output dimensions is "strided".
5195 * That is, we check if it can be written as
5197 * x = m a + f(..)
5199 * with m greater than 1, a some combination of existentially quantified
5200 * variables and f an expression in the parameters and input dimensions.
5201 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5203 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5204 * special case.
5206 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5207 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5209 int i, j;
5210 unsigned n_out;
5211 unsigned o_out;
5212 unsigned n_div;
5213 unsigned o_div;
5214 isl_int gcd;
5216 n_div = isl_basic_map_dim(hull, isl_dim_div);
5217 o_div = isl_basic_map_offset(hull, isl_dim_div);
5219 if (n_div == 0) {
5220 isl_basic_map_free(hull);
5221 return pw_multi_aff_from_map_check_div(map);
5224 isl_int_init(gcd);
5226 n_out = isl_basic_map_dim(hull, isl_dim_out);
5227 o_out = isl_basic_map_offset(hull, isl_dim_out);
5229 for (i = 0; i < n_out; ++i) {
5230 for (j = 0; j < hull->n_eq; ++j) {
5231 isl_int *eq = hull->eq[j];
5232 isl_pw_multi_aff *res;
5234 if (!isl_int_is_one(eq[o_out + i]) &&
5235 !isl_int_is_negone(eq[o_out + i]))
5236 continue;
5237 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5238 continue;
5239 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5240 n_out - (i + 1)) != -1)
5241 continue;
5242 isl_seq_gcd(eq + o_div, n_div, &gcd);
5243 if (isl_int_is_zero(gcd))
5244 continue;
5245 if (isl_int_is_one(gcd))
5246 continue;
5248 res = pw_multi_aff_from_map_stride(map, hull,
5249 i, j, gcd);
5250 isl_int_clear(gcd);
5251 return res;
5255 isl_int_clear(gcd);
5256 isl_basic_map_free(hull);
5257 return pw_multi_aff_from_map_check_div(map);
5260 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5262 * As a special case, we first check if all output dimensions are uniquely
5263 * defined in terms of the parameters and input dimensions over the entire
5264 * domain. If so, we extract the desired isl_pw_multi_aff directly
5265 * from the affine hull of "map" and its domain.
5267 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5268 * special cases.
5270 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5272 isl_bool sv;
5273 isl_basic_map *hull;
5275 if (!map)
5276 return NULL;
5278 if (isl_map_n_basic_map(map) == 1) {
5279 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5280 hull = isl_basic_map_plain_affine_hull(hull);
5281 sv = isl_basic_map_plain_is_single_valued(hull);
5282 if (sv >= 0 && sv)
5283 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5284 hull);
5285 isl_basic_map_free(hull);
5287 map = isl_map_detect_equalities(map);
5288 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5289 sv = isl_basic_map_plain_is_single_valued(hull);
5290 if (sv >= 0 && sv)
5291 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5292 if (sv >= 0)
5293 return pw_multi_aff_from_map_check_strides(map, hull);
5294 isl_basic_map_free(hull);
5295 isl_map_free(map);
5296 return NULL;
5299 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5301 return isl_pw_multi_aff_from_map(set);
5304 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5305 * add it to *user.
5307 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5309 isl_union_pw_multi_aff **upma = user;
5310 isl_pw_multi_aff *pma;
5312 pma = isl_pw_multi_aff_from_map(map);
5313 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5315 return *upma ? isl_stat_ok : isl_stat_error;
5318 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5319 * domain.
5321 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5322 __isl_take isl_aff *aff)
5324 isl_multi_aff *ma;
5325 isl_pw_multi_aff *pma;
5327 ma = isl_multi_aff_from_aff(aff);
5328 pma = isl_pw_multi_aff_from_multi_aff(ma);
5329 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5332 /* Try and create an isl_union_pw_multi_aff that is equivalent
5333 * to the given isl_union_map.
5334 * The isl_union_map is required to be single-valued in each space.
5335 * Otherwise, an error is produced.
5337 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5338 __isl_take isl_union_map *umap)
5340 isl_space *space;
5341 isl_union_pw_multi_aff *upma;
5343 space = isl_union_map_get_space(umap);
5344 upma = isl_union_pw_multi_aff_empty(space);
5345 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5346 upma = isl_union_pw_multi_aff_free(upma);
5347 isl_union_map_free(umap);
5349 return upma;
5352 /* Try and create an isl_union_pw_multi_aff that is equivalent
5353 * to the given isl_union_set.
5354 * The isl_union_set is required to be a singleton in each space.
5355 * Otherwise, an error is produced.
5357 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5358 __isl_take isl_union_set *uset)
5360 return isl_union_pw_multi_aff_from_union_map(uset);
5363 /* Return the piecewise affine expression "set ? 1 : 0".
5365 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5367 isl_pw_aff *pa;
5368 isl_space *space = isl_set_get_space(set);
5369 isl_local_space *ls = isl_local_space_from_space(space);
5370 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5371 isl_aff *one = isl_aff_zero_on_domain(ls);
5373 one = isl_aff_add_constant_si(one, 1);
5374 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5375 set = isl_set_complement(set);
5376 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5378 return pa;
5381 /* Plug in "subs" for dimension "type", "pos" of "aff".
5383 * Let i be the dimension to replace and let "subs" be of the form
5385 * f/d
5387 * and "aff" of the form
5389 * (a i + g)/m
5391 * The result is
5393 * (a f + d g')/(m d)
5395 * where g' is the result of plugging in "subs" in each of the integer
5396 * divisions in g.
5398 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5399 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5401 isl_ctx *ctx;
5402 isl_int v;
5404 aff = isl_aff_cow(aff);
5405 if (!aff || !subs)
5406 return isl_aff_free(aff);
5408 ctx = isl_aff_get_ctx(aff);
5409 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5410 isl_die(ctx, isl_error_invalid,
5411 "spaces don't match", return isl_aff_free(aff));
5412 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5413 isl_die(ctx, isl_error_unsupported,
5414 "cannot handle divs yet", return isl_aff_free(aff));
5416 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5417 if (!aff->ls)
5418 return isl_aff_free(aff);
5420 aff->v = isl_vec_cow(aff->v);
5421 if (!aff->v)
5422 return isl_aff_free(aff);
5424 pos += isl_local_space_offset(aff->ls, type);
5426 isl_int_init(v);
5427 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5428 aff->v->size, subs->v->size, v);
5429 isl_int_clear(v);
5431 return aff;
5434 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5435 * expressions in "maff".
5437 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5438 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5439 __isl_keep isl_aff *subs)
5441 int i;
5443 maff = isl_multi_aff_cow(maff);
5444 if (!maff || !subs)
5445 return isl_multi_aff_free(maff);
5447 if (type == isl_dim_in)
5448 type = isl_dim_set;
5450 for (i = 0; i < maff->n; ++i) {
5451 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5452 type, pos, subs);
5453 if (!maff->u.p[i])
5454 return isl_multi_aff_free(maff);
5457 return maff;
5460 /* Plug in "subs" for dimension "type", "pos" of "pma".
5462 * pma is of the form
5464 * A_i(v) -> M_i(v)
5466 * while subs is of the form
5468 * v' = B_j(v) -> S_j
5470 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5471 * has a contribution in the result, in particular
5473 * C_ij(S_j) -> M_i(S_j)
5475 * Note that plugging in S_j in C_ij may also result in an empty set
5476 * and this contribution should simply be discarded.
5478 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5479 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5480 __isl_keep isl_pw_aff *subs)
5482 int i, j, n;
5483 isl_pw_multi_aff *res;
5485 if (!pma || !subs)
5486 return isl_pw_multi_aff_free(pma);
5488 n = pma->n * subs->n;
5489 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5491 for (i = 0; i < pma->n; ++i) {
5492 for (j = 0; j < subs->n; ++j) {
5493 isl_set *common;
5494 isl_multi_aff *res_ij;
5495 int empty;
5497 common = isl_set_intersect(
5498 isl_set_copy(pma->p[i].set),
5499 isl_set_copy(subs->p[j].set));
5500 common = isl_set_substitute(common,
5501 type, pos, subs->p[j].aff);
5502 empty = isl_set_plain_is_empty(common);
5503 if (empty < 0 || empty) {
5504 isl_set_free(common);
5505 if (empty < 0)
5506 goto error;
5507 continue;
5510 res_ij = isl_multi_aff_substitute(
5511 isl_multi_aff_copy(pma->p[i].maff),
5512 type, pos, subs->p[j].aff);
5514 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5518 isl_pw_multi_aff_free(pma);
5519 return res;
5520 error:
5521 isl_pw_multi_aff_free(pma);
5522 isl_pw_multi_aff_free(res);
5523 return NULL;
5526 /* Compute the preimage of a range of dimensions in the affine expression "src"
5527 * under "ma" and put the result in "dst". The number of dimensions in "src"
5528 * that precede the range is given by "n_before". The number of dimensions
5529 * in the range is given by the number of output dimensions of "ma".
5530 * The number of dimensions that follow the range is given by "n_after".
5531 * If "has_denom" is set (to one),
5532 * then "src" and "dst" have an extra initial denominator.
5533 * "n_div_ma" is the number of existentials in "ma"
5534 * "n_div_bset" is the number of existentials in "src"
5535 * The resulting "dst" (which is assumed to have been allocated by
5536 * the caller) contains coefficients for both sets of existentials,
5537 * first those in "ma" and then those in "src".
5538 * f, c1, c2 and g are temporary objects that have been initialized
5539 * by the caller.
5541 * Let src represent the expression
5543 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5545 * and let ma represent the expressions
5547 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5549 * We start out with the following expression for dst:
5551 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5553 * with the multiplication factor f initially equal to 1
5554 * and f \sum_i b_i v_i kept separately.
5555 * For each x_i that we substitute, we multiply the numerator
5556 * (and denominator) of dst by c_1 = m_i and add the numerator
5557 * of the x_i expression multiplied by c_2 = f b_i,
5558 * after removing the common factors of c_1 and c_2.
5559 * The multiplication factor f also needs to be multiplied by c_1
5560 * for the next x_j, j > i.
5562 void isl_seq_preimage(isl_int *dst, isl_int *src,
5563 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5564 int n_div_ma, int n_div_bmap,
5565 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5567 int i;
5568 int n_param, n_in, n_out;
5569 int o_dst, o_src;
5571 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5572 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5573 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5575 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5576 o_dst = o_src = has_denom + 1 + n_param + n_before;
5577 isl_seq_clr(dst + o_dst, n_in);
5578 o_dst += n_in;
5579 o_src += n_out;
5580 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5581 o_dst += n_after;
5582 o_src += n_after;
5583 isl_seq_clr(dst + o_dst, n_div_ma);
5584 o_dst += n_div_ma;
5585 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5587 isl_int_set_si(f, 1);
5589 for (i = 0; i < n_out; ++i) {
5590 int offset = has_denom + 1 + n_param + n_before + i;
5592 if (isl_int_is_zero(src[offset]))
5593 continue;
5594 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5595 isl_int_mul(c2, f, src[offset]);
5596 isl_int_gcd(g, c1, c2);
5597 isl_int_divexact(c1, c1, g);
5598 isl_int_divexact(c2, c2, g);
5600 isl_int_mul(f, f, c1);
5601 o_dst = has_denom;
5602 o_src = 1;
5603 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5604 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5605 o_dst += 1 + n_param;
5606 o_src += 1 + n_param;
5607 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5608 o_dst += n_before;
5609 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5610 c2, ma->u.p[i]->v->el + o_src, n_in);
5611 o_dst += n_in;
5612 o_src += n_in;
5613 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5614 o_dst += n_after;
5615 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5616 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5617 o_dst += n_div_ma;
5618 o_src += n_div_ma;
5619 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5620 if (has_denom)
5621 isl_int_mul(dst[0], dst[0], c1);
5625 /* Compute the pullback of "aff" by the function represented by "ma".
5626 * In other words, plug in "ma" in "aff". The result is an affine expression
5627 * defined over the domain space of "ma".
5629 * If "aff" is represented by
5631 * (a(p) + b x + c(divs))/d
5633 * and ma is represented by
5635 * x = D(p) + F(y) + G(divs')
5637 * then the result is
5639 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5641 * The divs in the local space of the input are similarly adjusted
5642 * through a call to isl_local_space_preimage_multi_aff.
5644 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5645 __isl_take isl_multi_aff *ma)
5647 isl_aff *res = NULL;
5648 isl_local_space *ls;
5649 int n_div_aff, n_div_ma;
5650 isl_int f, c1, c2, g;
5652 ma = isl_multi_aff_align_divs(ma);
5653 if (!aff || !ma)
5654 goto error;
5656 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5657 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5659 ls = isl_aff_get_domain_local_space(aff);
5660 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5661 res = isl_aff_alloc(ls);
5662 if (!res)
5663 goto error;
5665 isl_int_init(f);
5666 isl_int_init(c1);
5667 isl_int_init(c2);
5668 isl_int_init(g);
5670 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5671 f, c1, c2, g, 1);
5673 isl_int_clear(f);
5674 isl_int_clear(c1);
5675 isl_int_clear(c2);
5676 isl_int_clear(g);
5678 isl_aff_free(aff);
5679 isl_multi_aff_free(ma);
5680 res = isl_aff_normalize(res);
5681 return res;
5682 error:
5683 isl_aff_free(aff);
5684 isl_multi_aff_free(ma);
5685 isl_aff_free(res);
5686 return NULL;
5689 /* Compute the pullback of "aff1" by the function represented by "aff2".
5690 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5691 * defined over the domain space of "aff1".
5693 * The domain of "aff1" should match the range of "aff2", which means
5694 * that it should be single-dimensional.
5696 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5697 __isl_take isl_aff *aff2)
5699 isl_multi_aff *ma;
5701 ma = isl_multi_aff_from_aff(aff2);
5702 return isl_aff_pullback_multi_aff(aff1, ma);
5705 /* Compute the pullback of "ma1" by the function represented by "ma2".
5706 * In other words, plug in "ma2" in "ma1".
5708 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5710 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5711 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5713 int i;
5714 isl_space *space = NULL;
5716 ma2 = isl_multi_aff_align_divs(ma2);
5717 ma1 = isl_multi_aff_cow(ma1);
5718 if (!ma1 || !ma2)
5719 goto error;
5721 space = isl_space_join(isl_multi_aff_get_space(ma2),
5722 isl_multi_aff_get_space(ma1));
5724 for (i = 0; i < ma1->n; ++i) {
5725 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5726 isl_multi_aff_copy(ma2));
5727 if (!ma1->u.p[i])
5728 goto error;
5731 ma1 = isl_multi_aff_reset_space(ma1, space);
5732 isl_multi_aff_free(ma2);
5733 return ma1;
5734 error:
5735 isl_space_free(space);
5736 isl_multi_aff_free(ma2);
5737 isl_multi_aff_free(ma1);
5738 return NULL;
5741 /* Compute the pullback of "ma1" by the function represented by "ma2".
5742 * In other words, plug in "ma2" in "ma1".
5744 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5745 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5747 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5748 &isl_multi_aff_pullback_multi_aff_aligned);
5751 /* Extend the local space of "dst" to include the divs
5752 * in the local space of "src".
5754 * If "src" does not have any divs or if the local spaces of "dst" and
5755 * "src" are the same, then no extension is required.
5757 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5758 __isl_keep isl_aff *src)
5760 isl_ctx *ctx;
5761 int src_n_div, dst_n_div;
5762 int *exp1 = NULL;
5763 int *exp2 = NULL;
5764 isl_bool equal;
5765 isl_mat *div;
5767 if (!src || !dst)
5768 return isl_aff_free(dst);
5770 ctx = isl_aff_get_ctx(src);
5771 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5772 if (equal < 0)
5773 return isl_aff_free(dst);
5774 if (!equal)
5775 isl_die(ctx, isl_error_invalid,
5776 "spaces don't match", goto error);
5778 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5779 if (src_n_div == 0)
5780 return dst;
5781 equal = isl_local_space_is_equal(src->ls, dst->ls);
5782 if (equal < 0)
5783 return isl_aff_free(dst);
5784 if (equal)
5785 return dst;
5787 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5788 exp1 = isl_alloc_array(ctx, int, src_n_div);
5789 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5790 if (!exp1 || (dst_n_div && !exp2))
5791 goto error;
5793 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5794 dst = isl_aff_expand_divs(dst, div, exp2);
5795 free(exp1);
5796 free(exp2);
5798 return dst;
5799 error:
5800 free(exp1);
5801 free(exp2);
5802 return isl_aff_free(dst);
5805 /* Adjust the local spaces of the affine expressions in "maff"
5806 * such that they all have the save divs.
5808 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5809 __isl_take isl_multi_aff *maff)
5811 int i;
5813 if (!maff)
5814 return NULL;
5815 if (maff->n == 0)
5816 return maff;
5817 maff = isl_multi_aff_cow(maff);
5818 if (!maff)
5819 return NULL;
5821 for (i = 1; i < maff->n; ++i)
5822 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5823 for (i = 1; i < maff->n; ++i) {
5824 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5825 if (!maff->u.p[i])
5826 return isl_multi_aff_free(maff);
5829 return maff;
5832 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5834 aff = isl_aff_cow(aff);
5835 if (!aff)
5836 return NULL;
5838 aff->ls = isl_local_space_lift(aff->ls);
5839 if (!aff->ls)
5840 return isl_aff_free(aff);
5842 return aff;
5845 /* Lift "maff" to a space with extra dimensions such that the result
5846 * has no more existentially quantified variables.
5847 * If "ls" is not NULL, then *ls is assigned the local space that lies
5848 * at the basis of the lifting applied to "maff".
5850 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5851 __isl_give isl_local_space **ls)
5853 int i;
5854 isl_space *space;
5855 unsigned n_div;
5857 if (ls)
5858 *ls = NULL;
5860 if (!maff)
5861 return NULL;
5863 if (maff->n == 0) {
5864 if (ls) {
5865 isl_space *space = isl_multi_aff_get_domain_space(maff);
5866 *ls = isl_local_space_from_space(space);
5867 if (!*ls)
5868 return isl_multi_aff_free(maff);
5870 return maff;
5873 maff = isl_multi_aff_cow(maff);
5874 maff = isl_multi_aff_align_divs(maff);
5875 if (!maff)
5876 return NULL;
5878 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5879 space = isl_multi_aff_get_space(maff);
5880 space = isl_space_lift(isl_space_domain(space), n_div);
5881 space = isl_space_extend_domain_with_range(space,
5882 isl_multi_aff_get_space(maff));
5883 if (!space)
5884 return isl_multi_aff_free(maff);
5885 isl_space_free(maff->space);
5886 maff->space = space;
5888 if (ls) {
5889 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5890 if (!*ls)
5891 return isl_multi_aff_free(maff);
5894 for (i = 0; i < maff->n; ++i) {
5895 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5896 if (!maff->u.p[i])
5897 goto error;
5900 return maff;
5901 error:
5902 if (ls)
5903 isl_local_space_free(*ls);
5904 return isl_multi_aff_free(maff);
5908 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5910 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5911 __isl_keep isl_pw_multi_aff *pma, int pos)
5913 int i;
5914 int n_out;
5915 isl_space *space;
5916 isl_pw_aff *pa;
5918 if (!pma)
5919 return NULL;
5921 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5922 if (pos < 0 || pos >= n_out)
5923 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5924 "index out of bounds", return NULL);
5926 space = isl_pw_multi_aff_get_space(pma);
5927 space = isl_space_drop_dims(space, isl_dim_out,
5928 pos + 1, n_out - pos - 1);
5929 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5931 pa = isl_pw_aff_alloc_size(space, pma->n);
5932 for (i = 0; i < pma->n; ++i) {
5933 isl_aff *aff;
5934 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5935 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5938 return pa;
5941 /* Return an isl_pw_multi_aff with the given "set" as domain and
5942 * an unnamed zero-dimensional range.
5944 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5945 __isl_take isl_set *set)
5947 isl_multi_aff *ma;
5948 isl_space *space;
5950 space = isl_set_get_space(set);
5951 space = isl_space_from_domain(space);
5952 ma = isl_multi_aff_zero(space);
5953 return isl_pw_multi_aff_alloc(set, ma);
5956 /* Add an isl_pw_multi_aff with the given "set" as domain and
5957 * an unnamed zero-dimensional range to *user.
5959 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5960 void *user)
5962 isl_union_pw_multi_aff **upma = user;
5963 isl_pw_multi_aff *pma;
5965 pma = isl_pw_multi_aff_from_domain(set);
5966 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5968 return isl_stat_ok;
5971 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5972 * an unnamed zero-dimensional range.
5974 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5975 __isl_take isl_union_set *uset)
5977 isl_space *space;
5978 isl_union_pw_multi_aff *upma;
5980 if (!uset)
5981 return NULL;
5983 space = isl_union_set_get_space(uset);
5984 upma = isl_union_pw_multi_aff_empty(space);
5986 if (isl_union_set_foreach_set(uset,
5987 &add_pw_multi_aff_from_domain, &upma) < 0)
5988 goto error;
5990 isl_union_set_free(uset);
5991 return upma;
5992 error:
5993 isl_union_set_free(uset);
5994 isl_union_pw_multi_aff_free(upma);
5995 return NULL;
5998 /* Convert "pma" to an isl_map and add it to *umap.
6000 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
6001 void *user)
6003 isl_union_map **umap = user;
6004 isl_map *map;
6006 map = isl_map_from_pw_multi_aff(pma);
6007 *umap = isl_union_map_add_map(*umap, map);
6009 return isl_stat_ok;
6012 /* Construct a union map mapping the domain of the union
6013 * piecewise multi-affine expression to its range, with each dimension
6014 * in the range equated to the corresponding affine expression on its cell.
6016 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
6017 __isl_take isl_union_pw_multi_aff *upma)
6019 isl_space *space;
6020 isl_union_map *umap;
6022 if (!upma)
6023 return NULL;
6025 space = isl_union_pw_multi_aff_get_space(upma);
6026 umap = isl_union_map_empty(space);
6028 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
6029 &map_from_pw_multi_aff, &umap) < 0)
6030 goto error;
6032 isl_union_pw_multi_aff_free(upma);
6033 return umap;
6034 error:
6035 isl_union_pw_multi_aff_free(upma);
6036 isl_union_map_free(umap);
6037 return NULL;
6040 /* Local data for bin_entry and the callback "fn".
6042 struct isl_union_pw_multi_aff_bin_data {
6043 isl_union_pw_multi_aff *upma2;
6044 isl_union_pw_multi_aff *res;
6045 isl_pw_multi_aff *pma;
6046 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6049 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6050 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6052 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6054 struct isl_union_pw_multi_aff_bin_data *data = user;
6055 isl_stat r;
6057 data->pma = pma;
6058 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6059 data->fn, data);
6060 isl_pw_multi_aff_free(pma);
6062 return r;
6065 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6066 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6067 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6068 * as *entry. The callback should adjust data->res if desired.
6070 static __isl_give isl_union_pw_multi_aff *bin_op(
6071 __isl_take isl_union_pw_multi_aff *upma1,
6072 __isl_take isl_union_pw_multi_aff *upma2,
6073 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6075 isl_space *space;
6076 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6078 space = isl_union_pw_multi_aff_get_space(upma2);
6079 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6080 space = isl_union_pw_multi_aff_get_space(upma1);
6081 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6083 if (!upma1 || !upma2)
6084 goto error;
6086 data.upma2 = upma2;
6087 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6088 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6089 &bin_entry, &data) < 0)
6090 goto error;
6092 isl_union_pw_multi_aff_free(upma1);
6093 isl_union_pw_multi_aff_free(upma2);
6094 return data.res;
6095 error:
6096 isl_union_pw_multi_aff_free(upma1);
6097 isl_union_pw_multi_aff_free(upma2);
6098 isl_union_pw_multi_aff_free(data.res);
6099 return NULL;
6102 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6103 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6105 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6106 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6108 isl_space *space;
6110 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6111 isl_pw_multi_aff_get_space(pma2));
6112 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6113 &isl_multi_aff_range_product);
6116 /* Given two isl_pw_multi_affs A -> B and C -> D,
6117 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6119 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6120 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6122 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6123 &pw_multi_aff_range_product);
6126 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6127 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6129 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6130 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6132 isl_space *space;
6134 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6135 isl_pw_multi_aff_get_space(pma2));
6136 space = isl_space_flatten_range(space);
6137 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6138 &isl_multi_aff_flat_range_product);
6141 /* Given two isl_pw_multi_affs A -> B and C -> D,
6142 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6144 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6145 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6147 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6148 &pw_multi_aff_flat_range_product);
6151 /* If data->pma and "pma2" have the same domain space, then compute
6152 * their flat range product and the result to data->res.
6154 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6155 void *user)
6157 struct isl_union_pw_multi_aff_bin_data *data = user;
6159 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6160 pma2->dim, isl_dim_in)) {
6161 isl_pw_multi_aff_free(pma2);
6162 return isl_stat_ok;
6165 pma2 = isl_pw_multi_aff_flat_range_product(
6166 isl_pw_multi_aff_copy(data->pma), pma2);
6168 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6170 return isl_stat_ok;
6173 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6174 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6176 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6177 __isl_take isl_union_pw_multi_aff *upma1,
6178 __isl_take isl_union_pw_multi_aff *upma2)
6180 return bin_op(upma1, upma2, &flat_range_product_entry);
6183 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6184 * The parameters are assumed to have been aligned.
6186 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6187 * except that it works on two different isl_pw_* types.
6189 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6190 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6191 __isl_take isl_pw_aff *pa)
6193 int i, j, n;
6194 isl_pw_multi_aff *res = NULL;
6196 if (!pma || !pa)
6197 goto error;
6199 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6200 pa->dim, isl_dim_in))
6201 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6202 "domains don't match", goto error);
6203 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6204 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6205 "index out of bounds", goto error);
6207 n = pma->n * pa->n;
6208 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6210 for (i = 0; i < pma->n; ++i) {
6211 for (j = 0; j < pa->n; ++j) {
6212 isl_set *common;
6213 isl_multi_aff *res_ij;
6214 int empty;
6216 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6217 isl_set_copy(pa->p[j].set));
6218 empty = isl_set_plain_is_empty(common);
6219 if (empty < 0 || empty) {
6220 isl_set_free(common);
6221 if (empty < 0)
6222 goto error;
6223 continue;
6226 res_ij = isl_multi_aff_set_aff(
6227 isl_multi_aff_copy(pma->p[i].maff), pos,
6228 isl_aff_copy(pa->p[j].aff));
6229 res_ij = isl_multi_aff_gist(res_ij,
6230 isl_set_copy(common));
6232 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6236 isl_pw_multi_aff_free(pma);
6237 isl_pw_aff_free(pa);
6238 return res;
6239 error:
6240 isl_pw_multi_aff_free(pma);
6241 isl_pw_aff_free(pa);
6242 return isl_pw_multi_aff_free(res);
6245 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6247 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6248 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6249 __isl_take isl_pw_aff *pa)
6251 isl_bool equal_params;
6253 if (!pma || !pa)
6254 goto error;
6255 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6256 if (equal_params < 0)
6257 goto error;
6258 if (equal_params)
6259 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6260 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6261 isl_pw_aff_check_named_params(pa) < 0)
6262 goto error;
6263 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6264 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6265 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6266 error:
6267 isl_pw_multi_aff_free(pma);
6268 isl_pw_aff_free(pa);
6269 return NULL;
6272 /* Do the parameters of "pa" match those of "space"?
6274 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6275 __isl_keep isl_space *space)
6277 isl_space *pa_space;
6278 isl_bool match;
6280 if (!pa || !space)
6281 return isl_bool_error;
6283 pa_space = isl_pw_aff_get_space(pa);
6285 match = isl_space_has_equal_params(space, pa_space);
6287 isl_space_free(pa_space);
6288 return match;
6291 /* Check that the domain space of "pa" matches "space".
6293 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6294 __isl_keep isl_space *space)
6296 isl_space *pa_space;
6297 isl_bool match;
6299 if (!pa || !space)
6300 return isl_stat_error;
6302 pa_space = isl_pw_aff_get_space(pa);
6304 match = isl_space_has_equal_params(space, pa_space);
6305 if (match < 0)
6306 goto error;
6307 if (!match)
6308 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6309 "parameters don't match", goto error);
6310 match = isl_space_tuple_is_equal(space, isl_dim_in,
6311 pa_space, isl_dim_in);
6312 if (match < 0)
6313 goto error;
6314 if (!match)
6315 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6316 "domains don't match", goto error);
6317 isl_space_free(pa_space);
6318 return isl_stat_ok;
6319 error:
6320 isl_space_free(pa_space);
6321 return isl_stat_error;
6324 #undef BASE
6325 #define BASE pw_aff
6326 #undef DOMBASE
6327 #define DOMBASE set
6329 #include <isl_multi_explicit_domain.c>
6330 #include <isl_multi_pw_aff_explicit_domain.c>
6331 #include <isl_multi_templ.c>
6332 #include <isl_multi_apply_set.c>
6333 #include <isl_multi_coalesce.c>
6334 #include <isl_multi_dims.c>
6335 #include <isl_multi_gist.c>
6336 #include <isl_multi_hash.c>
6337 #include <isl_multi_align_set.c>
6338 #include <isl_multi_intersect.c>
6340 /* Does "mpa" have a non-trivial explicit domain?
6342 * The explicit domain, if present, is trivial if it represents
6343 * an (obviously) universe set.
6345 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6346 __isl_keep isl_multi_pw_aff *mpa)
6348 if (!mpa)
6349 return isl_bool_error;
6350 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6351 return isl_bool_false;
6352 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6355 /* Scale the elements of "pma" by the corresponding elements of "mv".
6357 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6358 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6360 int i;
6361 isl_bool equal_params;
6363 pma = isl_pw_multi_aff_cow(pma);
6364 if (!pma || !mv)
6365 goto error;
6366 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6367 mv->space, isl_dim_set))
6368 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6369 "spaces don't match", goto error);
6370 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6371 if (equal_params < 0)
6372 goto error;
6373 if (!equal_params) {
6374 pma = isl_pw_multi_aff_align_params(pma,
6375 isl_multi_val_get_space(mv));
6376 mv = isl_multi_val_align_params(mv,
6377 isl_pw_multi_aff_get_space(pma));
6378 if (!pma || !mv)
6379 goto error;
6382 for (i = 0; i < pma->n; ++i) {
6383 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6384 isl_multi_val_copy(mv));
6385 if (!pma->p[i].maff)
6386 goto error;
6389 isl_multi_val_free(mv);
6390 return pma;
6391 error:
6392 isl_multi_val_free(mv);
6393 isl_pw_multi_aff_free(pma);
6394 return NULL;
6397 /* This function is called for each entry of an isl_union_pw_multi_aff.
6398 * If the space of the entry matches that of data->mv,
6399 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6400 * Otherwise, return an empty isl_pw_multi_aff.
6402 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6403 __isl_take isl_pw_multi_aff *pma, void *user)
6405 isl_multi_val *mv = user;
6407 if (!pma)
6408 return NULL;
6409 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6410 mv->space, isl_dim_set)) {
6411 isl_space *space = isl_pw_multi_aff_get_space(pma);
6412 isl_pw_multi_aff_free(pma);
6413 return isl_pw_multi_aff_empty(space);
6416 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6419 /* Scale the elements of "upma" by the corresponding elements of "mv",
6420 * for those entries that match the space of "mv".
6422 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6423 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6425 upma = isl_union_pw_multi_aff_align_params(upma,
6426 isl_multi_val_get_space(mv));
6427 mv = isl_multi_val_align_params(mv,
6428 isl_union_pw_multi_aff_get_space(upma));
6429 if (!upma || !mv)
6430 goto error;
6432 return isl_union_pw_multi_aff_transform(upma,
6433 &union_pw_multi_aff_scale_multi_val_entry, mv);
6435 isl_multi_val_free(mv);
6436 return upma;
6437 error:
6438 isl_multi_val_free(mv);
6439 isl_union_pw_multi_aff_free(upma);
6440 return NULL;
6443 /* Construct and return a piecewise multi affine expression
6444 * in the given space with value zero in each of the output dimensions and
6445 * a universe domain.
6447 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6449 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6452 /* Construct and return a piecewise multi affine expression
6453 * that is equal to the given piecewise affine expression.
6455 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6456 __isl_take isl_pw_aff *pa)
6458 int i;
6459 isl_space *space;
6460 isl_pw_multi_aff *pma;
6462 if (!pa)
6463 return NULL;
6465 space = isl_pw_aff_get_space(pa);
6466 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6468 for (i = 0; i < pa->n; ++i) {
6469 isl_set *set;
6470 isl_multi_aff *ma;
6472 set = isl_set_copy(pa->p[i].set);
6473 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6474 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6477 isl_pw_aff_free(pa);
6478 return pma;
6481 /* Construct a set or map mapping the shared (parameter) domain
6482 * of the piecewise affine expressions to the range of "mpa"
6483 * with each dimension in the range equated to the
6484 * corresponding piecewise affine expression.
6486 static __isl_give isl_map *map_from_multi_pw_aff(
6487 __isl_take isl_multi_pw_aff *mpa)
6489 int i;
6490 isl_space *space;
6491 isl_map *map;
6493 if (!mpa)
6494 return NULL;
6496 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6497 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6498 "invalid space", goto error);
6500 space = isl_multi_pw_aff_get_domain_space(mpa);
6501 map = isl_map_universe(isl_space_from_domain(space));
6503 for (i = 0; i < mpa->n; ++i) {
6504 isl_pw_aff *pa;
6505 isl_map *map_i;
6507 pa = isl_pw_aff_copy(mpa->u.p[i]);
6508 map_i = map_from_pw_aff(pa);
6510 map = isl_map_flat_range_product(map, map_i);
6513 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6515 isl_multi_pw_aff_free(mpa);
6516 return map;
6517 error:
6518 isl_multi_pw_aff_free(mpa);
6519 return NULL;
6522 /* Construct a map mapping the shared domain
6523 * of the piecewise affine expressions to the range of "mpa"
6524 * with each dimension in the range equated to the
6525 * corresponding piecewise affine expression.
6527 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6529 if (!mpa)
6530 return NULL;
6531 if (isl_space_is_set(mpa->space))
6532 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6533 "space of input is not a map", goto error);
6535 return map_from_multi_pw_aff(mpa);
6536 error:
6537 isl_multi_pw_aff_free(mpa);
6538 return NULL;
6541 /* Construct a set mapping the shared parameter domain
6542 * of the piecewise affine expressions to the space of "mpa"
6543 * with each dimension in the range equated to the
6544 * corresponding piecewise affine expression.
6546 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6548 if (!mpa)
6549 return NULL;
6550 if (!isl_space_is_set(mpa->space))
6551 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6552 "space of input is not a set", goto error);
6554 return map_from_multi_pw_aff(mpa);
6555 error:
6556 isl_multi_pw_aff_free(mpa);
6557 return NULL;
6560 /* Construct and return a piecewise multi affine expression
6561 * that is equal to the given multi piecewise affine expression
6562 * on the shared domain of the piecewise affine expressions,
6563 * in the special case of a 0D multi piecewise affine expression.
6565 * Create a piecewise multi affine expression with the explicit domain of
6566 * the 0D multi piecewise affine expression as domain.
6568 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6569 __isl_take isl_multi_pw_aff *mpa)
6571 isl_space *space;
6572 isl_set *dom;
6573 isl_multi_aff *ma;
6575 space = isl_multi_pw_aff_get_space(mpa);
6576 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6577 isl_multi_pw_aff_free(mpa);
6579 ma = isl_multi_aff_zero(space);
6580 return isl_pw_multi_aff_alloc(dom, ma);
6583 /* Construct and return a piecewise multi affine expression
6584 * that is equal to the given multi piecewise affine expression
6585 * on the shared domain of the piecewise affine expressions.
6587 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6588 __isl_take isl_multi_pw_aff *mpa)
6590 int i;
6591 isl_space *space;
6592 isl_pw_aff *pa;
6593 isl_pw_multi_aff *pma;
6595 if (!mpa)
6596 return NULL;
6598 if (mpa->n == 0)
6599 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6601 space = isl_multi_pw_aff_get_space(mpa);
6602 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6603 pma = isl_pw_multi_aff_from_pw_aff(pa);
6605 for (i = 1; i < mpa->n; ++i) {
6606 isl_pw_multi_aff *pma_i;
6608 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6609 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6610 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6613 pma = isl_pw_multi_aff_reset_space(pma, space);
6615 isl_multi_pw_aff_free(mpa);
6616 return pma;
6619 /* Construct and return a multi piecewise affine expression
6620 * that is equal to the given multi affine expression.
6622 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6623 __isl_take isl_multi_aff *ma)
6625 int i, n;
6626 isl_multi_pw_aff *mpa;
6628 if (!ma)
6629 return NULL;
6631 n = isl_multi_aff_dim(ma, isl_dim_out);
6632 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6634 for (i = 0; i < n; ++i) {
6635 isl_pw_aff *pa;
6637 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6638 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6641 isl_multi_aff_free(ma);
6642 return mpa;
6645 /* Construct and return a multi piecewise affine expression
6646 * that is equal to the given piecewise multi affine expression.
6648 * If the resulting multi piecewise affine expression has
6649 * an explicit domain, then assign it the domain of the input.
6650 * In other cases, the domain is stored in the individual elements.
6652 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6653 __isl_take isl_pw_multi_aff *pma)
6655 int i, n;
6656 isl_space *space;
6657 isl_multi_pw_aff *mpa;
6659 if (!pma)
6660 return NULL;
6662 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6663 space = isl_pw_multi_aff_get_space(pma);
6664 mpa = isl_multi_pw_aff_alloc(space);
6666 for (i = 0; i < n; ++i) {
6667 isl_pw_aff *pa;
6669 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6670 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6672 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6673 isl_set *dom;
6675 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6676 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6679 isl_pw_multi_aff_free(pma);
6680 return mpa;
6683 /* Do "pa1" and "pa2" represent the same function?
6685 * We first check if they are obviously equal.
6686 * If not, we convert them to maps and check if those are equal.
6688 * If "pa1" or "pa2" contain any NaNs, then they are considered
6689 * not to be the same. A NaN is not equal to anything, not even
6690 * to another NaN.
6692 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6693 __isl_keep isl_pw_aff *pa2)
6695 isl_bool equal;
6696 isl_bool has_nan;
6697 isl_map *map1, *map2;
6699 if (!pa1 || !pa2)
6700 return isl_bool_error;
6702 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6703 if (equal < 0 || equal)
6704 return equal;
6705 has_nan = either_involves_nan(pa1, pa2);
6706 if (has_nan < 0)
6707 return isl_bool_error;
6708 if (has_nan)
6709 return isl_bool_false;
6711 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6712 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6713 equal = isl_map_is_equal(map1, map2);
6714 isl_map_free(map1);
6715 isl_map_free(map2);
6717 return equal;
6720 /* Do "mpa1" and "mpa2" represent the same function?
6722 * Note that we cannot convert the entire isl_multi_pw_aff
6723 * to a map because the domains of the piecewise affine expressions
6724 * may not be the same.
6726 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6727 __isl_keep isl_multi_pw_aff *mpa2)
6729 int i;
6730 isl_bool equal, equal_params;
6732 if (!mpa1 || !mpa2)
6733 return isl_bool_error;
6735 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6736 if (equal_params < 0)
6737 return isl_bool_error;
6738 if (!equal_params) {
6739 if (!isl_space_has_named_params(mpa1->space))
6740 return isl_bool_false;
6741 if (!isl_space_has_named_params(mpa2->space))
6742 return isl_bool_false;
6743 mpa1 = isl_multi_pw_aff_copy(mpa1);
6744 mpa2 = isl_multi_pw_aff_copy(mpa2);
6745 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6746 isl_multi_pw_aff_get_space(mpa2));
6747 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6748 isl_multi_pw_aff_get_space(mpa1));
6749 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6750 isl_multi_pw_aff_free(mpa1);
6751 isl_multi_pw_aff_free(mpa2);
6752 return equal;
6755 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6756 if (equal < 0 || !equal)
6757 return equal;
6759 for (i = 0; i < mpa1->n; ++i) {
6760 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6761 if (equal < 0 || !equal)
6762 return equal;
6765 return isl_bool_true;
6768 /* Do "pma1" and "pma2" represent the same function?
6770 * First check if they are obviously equal.
6771 * If not, then convert them to maps and check if those are equal.
6773 * If "pa1" or "pa2" contain any NaNs, then they are considered
6774 * not to be the same. A NaN is not equal to anything, not even
6775 * to another NaN.
6777 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6778 __isl_keep isl_pw_multi_aff *pma2)
6780 isl_bool equal;
6781 isl_bool has_nan;
6782 isl_map *map1, *map2;
6784 if (!pma1 || !pma2)
6785 return isl_bool_error;
6787 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6788 if (equal < 0 || equal)
6789 return equal;
6790 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6791 if (has_nan >= 0 && !has_nan)
6792 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6793 if (has_nan < 0 || has_nan)
6794 return isl_bool_not(has_nan);
6796 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6797 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6798 equal = isl_map_is_equal(map1, map2);
6799 isl_map_free(map1);
6800 isl_map_free(map2);
6802 return equal;
6805 /* Compute the pullback of "mpa" by the function represented by "ma".
6806 * In other words, plug in "ma" in "mpa".
6808 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6810 * If "mpa" has an explicit domain, then it is this domain
6811 * that needs to undergo a pullback, i.e., a preimage.
6813 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6814 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6816 int i;
6817 isl_space *space = NULL;
6819 mpa = isl_multi_pw_aff_cow(mpa);
6820 if (!mpa || !ma)
6821 goto error;
6823 space = isl_space_join(isl_multi_aff_get_space(ma),
6824 isl_multi_pw_aff_get_space(mpa));
6825 if (!space)
6826 goto error;
6828 for (i = 0; i < mpa->n; ++i) {
6829 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6830 isl_multi_aff_copy(ma));
6831 if (!mpa->u.p[i])
6832 goto error;
6834 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6835 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6836 isl_multi_aff_copy(ma));
6837 if (!mpa->u.dom)
6838 goto error;
6841 isl_multi_aff_free(ma);
6842 isl_space_free(mpa->space);
6843 mpa->space = space;
6844 return mpa;
6845 error:
6846 isl_space_free(space);
6847 isl_multi_pw_aff_free(mpa);
6848 isl_multi_aff_free(ma);
6849 return NULL;
6852 /* Compute the pullback of "mpa" by the function represented by "ma".
6853 * In other words, plug in "ma" in "mpa".
6855 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6856 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6858 isl_bool equal_params;
6860 if (!mpa || !ma)
6861 goto error;
6862 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6863 if (equal_params < 0)
6864 goto error;
6865 if (equal_params)
6866 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6867 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6868 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6869 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6870 error:
6871 isl_multi_pw_aff_free(mpa);
6872 isl_multi_aff_free(ma);
6873 return NULL;
6876 /* Compute the pullback of "mpa" by the function represented by "pma".
6877 * In other words, plug in "pma" in "mpa".
6879 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6881 * If "mpa" has an explicit domain, then it is this domain
6882 * that needs to undergo a pullback, i.e., a preimage.
6884 static __isl_give isl_multi_pw_aff *
6885 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6886 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6888 int i;
6889 isl_space *space = NULL;
6891 mpa = isl_multi_pw_aff_cow(mpa);
6892 if (!mpa || !pma)
6893 goto error;
6895 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6896 isl_multi_pw_aff_get_space(mpa));
6898 for (i = 0; i < mpa->n; ++i) {
6899 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6900 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6901 if (!mpa->u.p[i])
6902 goto error;
6904 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6905 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6906 isl_pw_multi_aff_copy(pma));
6907 if (!mpa->u.dom)
6908 goto error;
6911 isl_pw_multi_aff_free(pma);
6912 isl_space_free(mpa->space);
6913 mpa->space = space;
6914 return mpa;
6915 error:
6916 isl_space_free(space);
6917 isl_multi_pw_aff_free(mpa);
6918 isl_pw_multi_aff_free(pma);
6919 return NULL;
6922 /* Compute the pullback of "mpa" by the function represented by "pma".
6923 * In other words, plug in "pma" in "mpa".
6925 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6926 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6928 isl_bool equal_params;
6930 if (!mpa || !pma)
6931 goto error;
6932 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6933 if (equal_params < 0)
6934 goto error;
6935 if (equal_params)
6936 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6937 mpa = isl_multi_pw_aff_align_params(mpa,
6938 isl_pw_multi_aff_get_space(pma));
6939 pma = isl_pw_multi_aff_align_params(pma,
6940 isl_multi_pw_aff_get_space(mpa));
6941 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6942 error:
6943 isl_multi_pw_aff_free(mpa);
6944 isl_pw_multi_aff_free(pma);
6945 return NULL;
6948 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6949 * with the domain of "aff". The domain of the result is the same
6950 * as that of "mpa".
6951 * "mpa" and "aff" are assumed to have been aligned.
6953 * We first extract the parametric constant from "aff", defined
6954 * over the correct domain.
6955 * Then we add the appropriate combinations of the members of "mpa".
6956 * Finally, we add the integer divisions through recursive calls.
6958 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6959 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6961 int i, n_in, n_div;
6962 isl_space *space;
6963 isl_val *v;
6964 isl_pw_aff *pa;
6965 isl_aff *tmp;
6967 n_in = isl_aff_dim(aff, isl_dim_in);
6968 n_div = isl_aff_dim(aff, isl_dim_div);
6970 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6971 tmp = isl_aff_copy(aff);
6972 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6973 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6974 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6975 isl_space_dim(space, isl_dim_set));
6976 tmp = isl_aff_reset_domain_space(tmp, space);
6977 pa = isl_pw_aff_from_aff(tmp);
6979 for (i = 0; i < n_in; ++i) {
6980 isl_pw_aff *pa_i;
6982 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6983 continue;
6984 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6985 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6986 pa_i = isl_pw_aff_scale_val(pa_i, v);
6987 pa = isl_pw_aff_add(pa, pa_i);
6990 for (i = 0; i < n_div; ++i) {
6991 isl_aff *div;
6992 isl_pw_aff *pa_i;
6994 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6995 continue;
6996 div = isl_aff_get_div(aff, i);
6997 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6998 isl_multi_pw_aff_copy(mpa), div);
6999 pa_i = isl_pw_aff_floor(pa_i);
7000 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7001 pa_i = isl_pw_aff_scale_val(pa_i, v);
7002 pa = isl_pw_aff_add(pa, pa_i);
7005 isl_multi_pw_aff_free(mpa);
7006 isl_aff_free(aff);
7008 return pa;
7011 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7012 * with the domain of "aff". The domain of the result is the same
7013 * as that of "mpa".
7015 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7016 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7018 isl_bool equal_params;
7020 if (!aff || !mpa)
7021 goto error;
7022 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7023 if (equal_params < 0)
7024 goto error;
7025 if (equal_params)
7026 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7028 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7029 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7031 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7032 error:
7033 isl_aff_free(aff);
7034 isl_multi_pw_aff_free(mpa);
7035 return NULL;
7038 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7039 * with the domain of "pa". The domain of the result is the same
7040 * as that of "mpa".
7041 * "mpa" and "pa" are assumed to have been aligned.
7043 * We consider each piece in turn. Note that the domains of the
7044 * pieces are assumed to be disjoint and they remain disjoint
7045 * after taking the preimage (over the same function).
7047 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7048 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7050 isl_space *space;
7051 isl_pw_aff *res;
7052 int i;
7054 if (!mpa || !pa)
7055 goto error;
7057 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7058 isl_pw_aff_get_space(pa));
7059 res = isl_pw_aff_empty(space);
7061 for (i = 0; i < pa->n; ++i) {
7062 isl_pw_aff *pa_i;
7063 isl_set *domain;
7065 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7066 isl_multi_pw_aff_copy(mpa),
7067 isl_aff_copy(pa->p[i].aff));
7068 domain = isl_set_copy(pa->p[i].set);
7069 domain = isl_set_preimage_multi_pw_aff(domain,
7070 isl_multi_pw_aff_copy(mpa));
7071 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7072 res = isl_pw_aff_add_disjoint(res, pa_i);
7075 isl_pw_aff_free(pa);
7076 isl_multi_pw_aff_free(mpa);
7077 return res;
7078 error:
7079 isl_pw_aff_free(pa);
7080 isl_multi_pw_aff_free(mpa);
7081 return NULL;
7084 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7085 * with the domain of "pa". The domain of the result is the same
7086 * as that of "mpa".
7088 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7089 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7091 isl_bool equal_params;
7093 if (!pa || !mpa)
7094 goto error;
7095 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7096 if (equal_params < 0)
7097 goto error;
7098 if (equal_params)
7099 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7101 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7102 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7104 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7105 error:
7106 isl_pw_aff_free(pa);
7107 isl_multi_pw_aff_free(mpa);
7108 return NULL;
7111 /* Compute the pullback of "pa" by the function represented by "mpa".
7112 * In other words, plug in "mpa" in "pa".
7113 * "pa" and "mpa" are assumed to have been aligned.
7115 * The pullback is computed by applying "pa" to "mpa".
7117 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7118 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7120 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7123 /* Compute the pullback of "pa" by the function represented by "mpa".
7124 * In other words, plug in "mpa" in "pa".
7126 * The pullback is computed by applying "pa" to "mpa".
7128 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7129 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7131 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7134 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7135 * In other words, plug in "mpa2" in "mpa1".
7137 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7139 * We pullback each member of "mpa1" in turn.
7141 * If "mpa1" has an explicit domain, then it is this domain
7142 * that needs to undergo a pullback instead, i.e., a preimage.
7144 static __isl_give isl_multi_pw_aff *
7145 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7146 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7148 int i;
7149 isl_space *space = NULL;
7151 mpa1 = isl_multi_pw_aff_cow(mpa1);
7152 if (!mpa1 || !mpa2)
7153 goto error;
7155 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7156 isl_multi_pw_aff_get_space(mpa1));
7158 for (i = 0; i < mpa1->n; ++i) {
7159 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7160 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7161 if (!mpa1->u.p[i])
7162 goto error;
7165 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7166 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7167 isl_multi_pw_aff_copy(mpa2));
7168 if (!mpa1->u.dom)
7169 goto error;
7171 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7173 isl_multi_pw_aff_free(mpa2);
7174 return mpa1;
7175 error:
7176 isl_space_free(space);
7177 isl_multi_pw_aff_free(mpa1);
7178 isl_multi_pw_aff_free(mpa2);
7179 return NULL;
7182 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7183 * In other words, plug in "mpa2" in "mpa1".
7185 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7186 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7188 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7189 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7192 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7193 * of "mpa1" and "mpa2" live in the same space, construct map space
7194 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7195 * with this map space as extract argument.
7197 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7198 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7199 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7200 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7202 int match;
7203 isl_space *space1, *space2;
7204 isl_map *res;
7206 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7207 isl_multi_pw_aff_get_space(mpa2));
7208 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7209 isl_multi_pw_aff_get_space(mpa1));
7210 if (!mpa1 || !mpa2)
7211 goto error;
7212 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7213 mpa2->space, isl_dim_out);
7214 if (match < 0)
7215 goto error;
7216 if (!match)
7217 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7218 "range spaces don't match", goto error);
7219 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7220 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7221 space1 = isl_space_map_from_domain_and_range(space1, space2);
7223 res = order(mpa1, mpa2, space1);
7224 isl_multi_pw_aff_free(mpa1);
7225 isl_multi_pw_aff_free(mpa2);
7226 return res;
7227 error:
7228 isl_multi_pw_aff_free(mpa1);
7229 isl_multi_pw_aff_free(mpa2);
7230 return NULL;
7233 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7234 * where the function values are equal. "space" is the space of the result.
7235 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7237 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7238 * in the sequences are equal.
7240 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7241 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7242 __isl_take isl_space *space)
7244 int i, n;
7245 isl_map *res;
7247 res = isl_map_universe(space);
7249 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7250 for (i = 0; i < n; ++i) {
7251 isl_pw_aff *pa1, *pa2;
7252 isl_map *map;
7254 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7255 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7256 map = isl_pw_aff_eq_map(pa1, pa2);
7257 res = isl_map_intersect(res, map);
7260 return res;
7263 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7264 * where the function values are equal.
7266 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7267 __isl_take isl_multi_pw_aff *mpa2)
7269 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7270 &isl_multi_pw_aff_eq_map_on_space);
7273 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7274 * where the function values of "mpa1" is lexicographically satisfies "base"
7275 * compared to that of "mpa2". "space" is the space of the result.
7276 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7278 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7279 * if its i-th element satisfies "base" when compared to
7280 * the i-th element of "mpa2" while all previous elements are
7281 * pairwise equal.
7283 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7284 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7285 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7286 __isl_take isl_pw_aff *pa2),
7287 __isl_take isl_space *space)
7289 int i, n;
7290 isl_map *res, *rest;
7292 res = isl_map_empty(isl_space_copy(space));
7293 rest = isl_map_universe(space);
7295 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7296 for (i = 0; i < n; ++i) {
7297 isl_pw_aff *pa1, *pa2;
7298 isl_map *map;
7300 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7301 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7302 map = base(pa1, pa2);
7303 map = isl_map_intersect(map, isl_map_copy(rest));
7304 res = isl_map_union(res, map);
7306 if (i == n - 1)
7307 continue;
7309 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7310 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7311 map = isl_pw_aff_eq_map(pa1, pa2);
7312 rest = isl_map_intersect(rest, map);
7315 isl_map_free(rest);
7316 return res;
7319 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7320 * where the function value of "mpa1" is lexicographically less than that
7321 * of "mpa2". "space" is the space of the result.
7322 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7324 * "mpa1" is less than "mpa2" if its i-th element is smaller
7325 * than the i-th element of "mpa2" while all previous elements are
7326 * pairwise equal.
7328 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7329 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7330 __isl_take isl_space *space)
7332 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7333 &isl_pw_aff_lt_map, space);
7336 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7337 * where the function value of "mpa1" is lexicographically less than that
7338 * of "mpa2".
7340 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7341 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7343 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7344 &isl_multi_pw_aff_lex_lt_map_on_space);
7347 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7348 * where the function value of "mpa1" is lexicographically greater than that
7349 * of "mpa2". "space" is the space of the result.
7350 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7352 * "mpa1" is greater than "mpa2" if its i-th element is greater
7353 * than the i-th element of "mpa2" while all previous elements are
7354 * pairwise equal.
7356 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7357 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7358 __isl_take isl_space *space)
7360 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7361 &isl_pw_aff_gt_map, space);
7364 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7365 * where the function value of "mpa1" is lexicographically greater than that
7366 * of "mpa2".
7368 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7369 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7371 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7372 &isl_multi_pw_aff_lex_gt_map_on_space);
7375 /* Compare two isl_affs.
7377 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7378 * than "aff2" and 0 if they are equal.
7380 * The order is fairly arbitrary. We do consider expressions that only involve
7381 * earlier dimensions as "smaller".
7383 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7385 int cmp;
7386 int last1, last2;
7388 if (aff1 == aff2)
7389 return 0;
7391 if (!aff1)
7392 return -1;
7393 if (!aff2)
7394 return 1;
7396 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7397 if (cmp != 0)
7398 return cmp;
7400 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7401 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7402 if (last1 != last2)
7403 return last1 - last2;
7405 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7408 /* Compare two isl_pw_affs.
7410 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7411 * than "pa2" and 0 if they are equal.
7413 * The order is fairly arbitrary. We do consider expressions that only involve
7414 * earlier dimensions as "smaller".
7416 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7417 __isl_keep isl_pw_aff *pa2)
7419 int i;
7420 int cmp;
7422 if (pa1 == pa2)
7423 return 0;
7425 if (!pa1)
7426 return -1;
7427 if (!pa2)
7428 return 1;
7430 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7431 if (cmp != 0)
7432 return cmp;
7434 if (pa1->n != pa2->n)
7435 return pa1->n - pa2->n;
7437 for (i = 0; i < pa1->n; ++i) {
7438 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7439 if (cmp != 0)
7440 return cmp;
7441 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7442 if (cmp != 0)
7443 return cmp;
7446 return 0;
7449 /* Return a piecewise affine expression that is equal to "v" on "domain".
7451 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7452 __isl_take isl_val *v)
7454 isl_space *space;
7455 isl_local_space *ls;
7456 isl_aff *aff;
7458 space = isl_set_get_space(domain);
7459 ls = isl_local_space_from_space(space);
7460 aff = isl_aff_val_on_domain(ls, v);
7462 return isl_pw_aff_alloc(domain, aff);
7465 /* Return a multi affine expression that is equal to "mv" on domain
7466 * space "space".
7468 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7469 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7471 int i, n;
7472 isl_space *space2;
7473 isl_local_space *ls;
7474 isl_multi_aff *ma;
7476 if (!space || !mv)
7477 goto error;
7479 n = isl_multi_val_dim(mv, isl_dim_set);
7480 space2 = isl_multi_val_get_space(mv);
7481 space2 = isl_space_align_params(space2, isl_space_copy(space));
7482 space = isl_space_align_params(space, isl_space_copy(space2));
7483 space = isl_space_map_from_domain_and_range(space, space2);
7484 ma = isl_multi_aff_alloc(isl_space_copy(space));
7485 ls = isl_local_space_from_space(isl_space_domain(space));
7486 for (i = 0; i < n; ++i) {
7487 isl_val *v;
7488 isl_aff *aff;
7490 v = isl_multi_val_get_val(mv, i);
7491 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7492 ma = isl_multi_aff_set_aff(ma, i, aff);
7494 isl_local_space_free(ls);
7496 isl_multi_val_free(mv);
7497 return ma;
7498 error:
7499 isl_space_free(space);
7500 isl_multi_val_free(mv);
7501 return NULL;
7504 /* Return a piecewise multi-affine expression
7505 * that is equal to "mv" on "domain".
7507 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7508 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7510 isl_space *space;
7511 isl_multi_aff *ma;
7513 space = isl_set_get_space(domain);
7514 ma = isl_multi_aff_multi_val_on_space(space, mv);
7516 return isl_pw_multi_aff_alloc(domain, ma);
7519 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7520 * mv is the value that should be attained on each domain set
7521 * res collects the results
7523 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7524 isl_multi_val *mv;
7525 isl_union_pw_multi_aff *res;
7528 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7529 * and add it to data->res.
7531 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7532 void *user)
7534 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7535 isl_pw_multi_aff *pma;
7536 isl_multi_val *mv;
7538 mv = isl_multi_val_copy(data->mv);
7539 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7540 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7542 return data->res ? isl_stat_ok : isl_stat_error;
7545 /* Return a union piecewise multi-affine expression
7546 * that is equal to "mv" on "domain".
7548 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7549 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7551 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7552 isl_space *space;
7554 space = isl_union_set_get_space(domain);
7555 data.res = isl_union_pw_multi_aff_empty(space);
7556 data.mv = mv;
7557 if (isl_union_set_foreach_set(domain,
7558 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7559 data.res = isl_union_pw_multi_aff_free(data.res);
7560 isl_union_set_free(domain);
7561 isl_multi_val_free(mv);
7562 return data.res;
7565 /* Compute the pullback of data->pma by the function represented by "pma2",
7566 * provided the spaces match, and add the results to data->res.
7568 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7570 struct isl_union_pw_multi_aff_bin_data *data = user;
7572 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7573 pma2->dim, isl_dim_out)) {
7574 isl_pw_multi_aff_free(pma2);
7575 return isl_stat_ok;
7578 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7579 isl_pw_multi_aff_copy(data->pma), pma2);
7581 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7582 if (!data->res)
7583 return isl_stat_error;
7585 return isl_stat_ok;
7588 /* Compute the pullback of "upma1" by the function represented by "upma2".
7590 __isl_give isl_union_pw_multi_aff *
7591 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7592 __isl_take isl_union_pw_multi_aff *upma1,
7593 __isl_take isl_union_pw_multi_aff *upma2)
7595 return bin_op(upma1, upma2, &pullback_entry);
7598 /* Check that the domain space of "upa" matches "space".
7600 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7601 * can in principle never fail since the space "space" is that
7602 * of the isl_multi_union_pw_aff and is a set space such that
7603 * there is no domain space to match.
7605 * We check the parameters and double-check that "space" is
7606 * indeed that of a set.
7608 static isl_stat isl_union_pw_aff_check_match_domain_space(
7609 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7611 isl_space *upa_space;
7612 isl_bool match;
7614 if (!upa || !space)
7615 return isl_stat_error;
7617 match = isl_space_is_set(space);
7618 if (match < 0)
7619 return isl_stat_error;
7620 if (!match)
7621 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7622 "expecting set space", return isl_stat_error);
7624 upa_space = isl_union_pw_aff_get_space(upa);
7625 match = isl_space_has_equal_params(space, upa_space);
7626 if (match < 0)
7627 goto error;
7628 if (!match)
7629 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7630 "parameters don't match", goto error);
7632 isl_space_free(upa_space);
7633 return isl_stat_ok;
7634 error:
7635 isl_space_free(upa_space);
7636 return isl_stat_error;
7639 /* Do the parameters of "upa" match those of "space"?
7641 static isl_bool isl_union_pw_aff_matching_params(
7642 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7644 isl_space *upa_space;
7645 isl_bool match;
7647 if (!upa || !space)
7648 return isl_bool_error;
7650 upa_space = isl_union_pw_aff_get_space(upa);
7652 match = isl_space_has_equal_params(space, upa_space);
7654 isl_space_free(upa_space);
7655 return match;
7658 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7659 * space represents the new parameters.
7660 * res collects the results.
7662 struct isl_union_pw_aff_reset_params_data {
7663 isl_space *space;
7664 isl_union_pw_aff *res;
7667 /* Replace the parameters of "pa" by data->space and
7668 * add the result to data->res.
7670 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7672 struct isl_union_pw_aff_reset_params_data *data = user;
7673 isl_space *space;
7675 space = isl_pw_aff_get_space(pa);
7676 space = isl_space_replace_params(space, data->space);
7677 pa = isl_pw_aff_reset_space(pa, space);
7678 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7680 return data->res ? isl_stat_ok : isl_stat_error;
7683 /* Replace the domain space of "upa" by "space".
7684 * Since a union expression does not have a (single) domain space,
7685 * "space" is necessarily a parameter space.
7687 * Since the order and the names of the parameters determine
7688 * the hash value, we need to create a new hash table.
7690 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7691 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7693 struct isl_union_pw_aff_reset_params_data data = { space };
7694 isl_bool match;
7696 match = isl_union_pw_aff_matching_params(upa, space);
7697 if (match < 0)
7698 upa = isl_union_pw_aff_free(upa);
7699 else if (match) {
7700 isl_space_free(space);
7701 return upa;
7704 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7705 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7706 data.res = isl_union_pw_aff_free(data.res);
7708 isl_union_pw_aff_free(upa);
7709 isl_space_free(space);
7710 return data.res;
7713 /* Return the floor of "pa".
7715 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7717 return isl_pw_aff_floor(pa);
7720 /* Given f, return floor(f).
7722 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7723 __isl_take isl_union_pw_aff *upa)
7725 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7728 /* Compute
7730 * upa mod m = upa - m * floor(upa/m)
7732 * with m an integer value.
7734 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7735 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7737 isl_union_pw_aff *res;
7739 if (!upa || !m)
7740 goto error;
7742 if (!isl_val_is_int(m))
7743 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7744 "expecting integer modulo", goto error);
7745 if (!isl_val_is_pos(m))
7746 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7747 "expecting positive modulo", goto error);
7749 res = isl_union_pw_aff_copy(upa);
7750 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7751 upa = isl_union_pw_aff_floor(upa);
7752 upa = isl_union_pw_aff_scale_val(upa, m);
7753 res = isl_union_pw_aff_sub(res, upa);
7755 return res;
7756 error:
7757 isl_val_free(m);
7758 isl_union_pw_aff_free(upa);
7759 return NULL;
7762 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7763 * pos is the output position that needs to be extracted.
7764 * res collects the results.
7766 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7767 int pos;
7768 isl_union_pw_aff *res;
7771 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7772 * (assuming it has such a dimension) and add it to data->res.
7774 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7776 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7777 int n_out;
7778 isl_pw_aff *pa;
7780 if (!pma)
7781 return isl_stat_error;
7783 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7784 if (data->pos >= n_out) {
7785 isl_pw_multi_aff_free(pma);
7786 return isl_stat_ok;
7789 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7790 isl_pw_multi_aff_free(pma);
7792 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7794 return data->res ? isl_stat_ok : isl_stat_error;
7797 /* Extract an isl_union_pw_aff corresponding to
7798 * output dimension "pos" of "upma".
7800 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7801 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7803 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7804 isl_space *space;
7806 if (!upma)
7807 return NULL;
7809 if (pos < 0)
7810 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7811 "cannot extract at negative position", return NULL);
7813 space = isl_union_pw_multi_aff_get_space(upma);
7814 data.res = isl_union_pw_aff_empty(space);
7815 data.pos = pos;
7816 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7817 &get_union_pw_aff, &data) < 0)
7818 data.res = isl_union_pw_aff_free(data.res);
7820 return data.res;
7823 /* Return a union piecewise affine expression
7824 * that is equal to "aff" on "domain".
7826 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7827 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7829 isl_pw_aff *pa;
7831 pa = isl_pw_aff_from_aff(aff);
7832 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7835 /* Return a union piecewise affine expression
7836 * that is equal to the parameter identified by "id" on "domain".
7838 * Make sure the parameter appears in the space passed to
7839 * isl_aff_param_on_domain_space_id.
7841 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7842 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7844 isl_space *space;
7845 isl_aff *aff;
7847 space = isl_union_set_get_space(domain);
7848 space = isl_space_add_param_id(space, isl_id_copy(id));
7849 aff = isl_aff_param_on_domain_space_id(space, id);
7850 return isl_union_pw_aff_aff_on_domain(domain, aff);
7853 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7854 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7855 * needs to attain.
7856 * "res" collects the results.
7858 struct isl_union_pw_aff_pw_aff_on_domain_data {
7859 isl_pw_aff *pa;
7860 isl_union_pw_aff *res;
7863 /* Construct a piecewise affine expression that is equal to data->pa
7864 * on "domain" and add the result to data->res.
7866 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7868 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7869 isl_pw_aff *pa;
7870 int dim;
7872 pa = isl_pw_aff_copy(data->pa);
7873 dim = isl_set_dim(domain, isl_dim_set);
7874 pa = isl_pw_aff_from_range(pa);
7875 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7876 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7877 pa = isl_pw_aff_intersect_domain(pa, domain);
7878 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7880 return data->res ? isl_stat_ok : isl_stat_error;
7883 /* Return a union piecewise affine expression
7884 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7885 * have been aligned.
7887 * Construct an isl_pw_aff on each of the sets in "domain" and
7888 * collect the results.
7890 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7891 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7893 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7894 isl_space *space;
7896 space = isl_union_set_get_space(domain);
7897 data.res = isl_union_pw_aff_empty(space);
7898 data.pa = pa;
7899 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7900 data.res = isl_union_pw_aff_free(data.res);
7901 isl_union_set_free(domain);
7902 isl_pw_aff_free(pa);
7903 return data.res;
7906 /* Return a union piecewise affine expression
7907 * that is equal to "pa" on "domain".
7909 * Check that "pa" is a parametric expression,
7910 * align the parameters if needed and call
7911 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7913 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7914 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7916 isl_bool is_set;
7917 isl_bool equal_params;
7918 isl_space *domain_space, *pa_space;
7920 pa_space = isl_pw_aff_peek_space(pa);
7921 is_set = isl_space_is_set(pa_space);
7922 if (is_set < 0)
7923 goto error;
7924 if (!is_set)
7925 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7926 "expecting parametric expression", goto error);
7928 domain_space = isl_union_set_get_space(domain);
7929 pa_space = isl_pw_aff_get_space(pa);
7930 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7931 if (equal_params >= 0 && !equal_params) {
7932 isl_space *space;
7934 space = isl_space_align_params(domain_space, pa_space);
7935 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7936 domain = isl_union_set_align_params(domain, space);
7937 } else {
7938 isl_space_free(domain_space);
7939 isl_space_free(pa_space);
7942 if (equal_params < 0)
7943 goto error;
7944 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7945 error:
7946 isl_union_set_free(domain);
7947 isl_pw_aff_free(pa);
7948 return NULL;
7951 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7952 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7953 * "res" collects the results.
7955 struct isl_union_pw_aff_val_on_domain_data {
7956 isl_val *v;
7957 isl_union_pw_aff *res;
7960 /* Construct a piecewise affine expression that is equal to data->v
7961 * on "domain" and add the result to data->res.
7963 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7965 struct isl_union_pw_aff_val_on_domain_data *data = user;
7966 isl_pw_aff *pa;
7967 isl_val *v;
7969 v = isl_val_copy(data->v);
7970 pa = isl_pw_aff_val_on_domain(domain, v);
7971 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7973 return data->res ? isl_stat_ok : isl_stat_error;
7976 /* Return a union piecewise affine expression
7977 * that is equal to "v" on "domain".
7979 * Construct an isl_pw_aff on each of the sets in "domain" and
7980 * collect the results.
7982 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7983 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7985 struct isl_union_pw_aff_val_on_domain_data data;
7986 isl_space *space;
7988 space = isl_union_set_get_space(domain);
7989 data.res = isl_union_pw_aff_empty(space);
7990 data.v = v;
7991 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7992 data.res = isl_union_pw_aff_free(data.res);
7993 isl_union_set_free(domain);
7994 isl_val_free(v);
7995 return data.res;
7998 /* Construct a piecewise multi affine expression
7999 * that is equal to "pa" and add it to upma.
8001 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8002 void *user)
8004 isl_union_pw_multi_aff **upma = user;
8005 isl_pw_multi_aff *pma;
8007 pma = isl_pw_multi_aff_from_pw_aff(pa);
8008 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8010 return *upma ? isl_stat_ok : isl_stat_error;
8013 /* Construct and return a union piecewise multi affine expression
8014 * that is equal to the given union piecewise affine expression.
8016 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8017 __isl_take isl_union_pw_aff *upa)
8019 isl_space *space;
8020 isl_union_pw_multi_aff *upma;
8022 if (!upa)
8023 return NULL;
8025 space = isl_union_pw_aff_get_space(upa);
8026 upma = isl_union_pw_multi_aff_empty(space);
8028 if (isl_union_pw_aff_foreach_pw_aff(upa,
8029 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8030 upma = isl_union_pw_multi_aff_free(upma);
8032 isl_union_pw_aff_free(upa);
8033 return upma;
8036 /* Compute the set of elements in the domain of "pa" where it is zero and
8037 * add this set to "uset".
8039 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8041 isl_union_set **uset = (isl_union_set **)user;
8043 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8045 return *uset ? isl_stat_ok : isl_stat_error;
8048 /* Return a union set containing those elements in the domain
8049 * of "upa" where it is zero.
8051 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8052 __isl_take isl_union_pw_aff *upa)
8054 isl_union_set *zero;
8056 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8057 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8058 zero = isl_union_set_free(zero);
8060 isl_union_pw_aff_free(upa);
8061 return zero;
8064 /* Convert "pa" to an isl_map and add it to *umap.
8066 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8068 isl_union_map **umap = user;
8069 isl_map *map;
8071 map = isl_map_from_pw_aff(pa);
8072 *umap = isl_union_map_add_map(*umap, map);
8074 return *umap ? isl_stat_ok : isl_stat_error;
8077 /* Construct a union map mapping the domain of the union
8078 * piecewise affine expression to its range, with the single output dimension
8079 * equated to the corresponding affine expressions on their cells.
8081 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8082 __isl_take isl_union_pw_aff *upa)
8084 isl_space *space;
8085 isl_union_map *umap;
8087 if (!upa)
8088 return NULL;
8090 space = isl_union_pw_aff_get_space(upa);
8091 umap = isl_union_map_empty(space);
8093 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8094 &umap) < 0)
8095 umap = isl_union_map_free(umap);
8097 isl_union_pw_aff_free(upa);
8098 return umap;
8101 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8102 * upma is the function that is plugged in.
8103 * pa is the current part of the function in which upma is plugged in.
8104 * res collects the results.
8106 struct isl_union_pw_aff_pullback_upma_data {
8107 isl_union_pw_multi_aff *upma;
8108 isl_pw_aff *pa;
8109 isl_union_pw_aff *res;
8112 /* Check if "pma" can be plugged into data->pa.
8113 * If so, perform the pullback and add the result to data->res.
8115 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8117 struct isl_union_pw_aff_pullback_upma_data *data = user;
8118 isl_pw_aff *pa;
8120 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8121 pma->dim, isl_dim_out)) {
8122 isl_pw_multi_aff_free(pma);
8123 return isl_stat_ok;
8126 pa = isl_pw_aff_copy(data->pa);
8127 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8129 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8131 return data->res ? isl_stat_ok : isl_stat_error;
8134 /* Check if any of the elements of data->upma can be plugged into pa,
8135 * add if so add the result to data->res.
8137 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8139 struct isl_union_pw_aff_pullback_upma_data *data = user;
8140 isl_stat r;
8142 data->pa = pa;
8143 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8144 &pa_pb_pma, data);
8145 isl_pw_aff_free(pa);
8147 return r;
8150 /* Compute the pullback of "upa" by the function represented by "upma".
8151 * In other words, plug in "upma" in "upa". The result contains
8152 * expressions defined over the domain space of "upma".
8154 * Run over all pairs of elements in "upa" and "upma", perform
8155 * the pullback when appropriate and collect the results.
8156 * If the hash value were based on the domain space rather than
8157 * the function space, then we could run through all elements
8158 * of "upma" and directly pick out the corresponding element of "upa".
8160 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8161 __isl_take isl_union_pw_aff *upa,
8162 __isl_take isl_union_pw_multi_aff *upma)
8164 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8165 isl_space *space;
8167 space = isl_union_pw_multi_aff_get_space(upma);
8168 upa = isl_union_pw_aff_align_params(upa, space);
8169 space = isl_union_pw_aff_get_space(upa);
8170 upma = isl_union_pw_multi_aff_align_params(upma, space);
8172 if (!upa || !upma)
8173 goto error;
8175 data.upma = upma;
8176 data.res = isl_union_pw_aff_alloc_same_size(upa);
8177 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8178 data.res = isl_union_pw_aff_free(data.res);
8180 isl_union_pw_aff_free(upa);
8181 isl_union_pw_multi_aff_free(upma);
8182 return data.res;
8183 error:
8184 isl_union_pw_aff_free(upa);
8185 isl_union_pw_multi_aff_free(upma);
8186 return NULL;
8189 #undef BASE
8190 #define BASE union_pw_aff
8191 #undef DOMBASE
8192 #define DOMBASE union_set
8194 #define NO_MOVE_DIMS
8195 #define NO_DOMAIN
8196 #define NO_PRODUCT
8197 #define NO_SPLICE
8198 #define NO_ZERO
8199 #define NO_IDENTITY
8201 #include <isl_multi_explicit_domain.c>
8202 #include <isl_multi_union_pw_aff_explicit_domain.c>
8203 #include <isl_multi_templ.c>
8204 #include <isl_multi_apply_set.c>
8205 #include <isl_multi_apply_union_set.c>
8206 #include <isl_multi_coalesce.c>
8207 #include <isl_multi_floor.c>
8208 #include <isl_multi_gist.c>
8209 #include <isl_multi_align_set.c>
8210 #include <isl_multi_align_union_set.c>
8211 #include <isl_multi_intersect.c>
8213 /* Does "mupa" have a non-trivial explicit domain?
8215 * The explicit domain, if present, is trivial if it represents
8216 * an (obviously) universe parameter set.
8218 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8219 __isl_keep isl_multi_union_pw_aff *mupa)
8221 isl_bool is_params, trivial;
8222 isl_set *set;
8224 if (!mupa)
8225 return isl_bool_error;
8226 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8227 return isl_bool_false;
8228 is_params = isl_union_set_is_params(mupa->u.dom);
8229 if (is_params < 0 || !is_params)
8230 return isl_bool_not(is_params);
8231 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8232 trivial = isl_set_plain_is_universe(set);
8233 isl_set_free(set);
8234 return isl_bool_not(trivial);
8237 /* Construct a multiple union piecewise affine expression
8238 * in the given space with value zero in each of the output dimensions.
8240 * Since there is no canonical zero value for
8241 * a union piecewise affine expression, we can only construct
8242 * a zero-dimensional "zero" value.
8244 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8245 __isl_take isl_space *space)
8247 isl_bool params;
8249 if (!space)
8250 return NULL;
8252 params = isl_space_is_params(space);
8253 if (params < 0)
8254 goto error;
8255 if (params)
8256 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8257 "expecting proper set space", goto error);
8258 if (!isl_space_is_set(space))
8259 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8260 "expecting set space", goto error);
8261 if (isl_space_dim(space , isl_dim_out) != 0)
8262 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8263 "expecting 0D space", goto error);
8265 return isl_multi_union_pw_aff_alloc(space);
8266 error:
8267 isl_space_free(space);
8268 return NULL;
8271 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8272 * with the actual sum on the shared domain and
8273 * the defined expression on the symmetric difference of the domains.
8275 * We simply iterate over the elements in both arguments and
8276 * call isl_union_pw_aff_union_add on each of them, if there is
8277 * at least one element.
8279 * Otherwise, the two expressions have an explicit domain and
8280 * the union of these explicit domains is computed.
8281 * This assumes that the explicit domains are either both in terms
8282 * of specific domains elements or both in terms of parameters.
8283 * However, if one of the expressions does not have any constraints
8284 * on its explicit domain, then this is allowed as well and the result
8285 * is the expression with no constraints on its explicit domain.
8287 static __isl_give isl_multi_union_pw_aff *
8288 isl_multi_union_pw_aff_union_add_aligned(
8289 __isl_take isl_multi_union_pw_aff *mupa1,
8290 __isl_take isl_multi_union_pw_aff *mupa2)
8292 isl_bool has_domain, is_params1, is_params2;
8294 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8295 goto error;
8296 if (mupa1->n > 0)
8297 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8298 &isl_union_pw_aff_union_add);
8299 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8300 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8301 goto error;
8303 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8304 if (has_domain < 0)
8305 goto error;
8306 if (!has_domain) {
8307 isl_multi_union_pw_aff_free(mupa2);
8308 return mupa1;
8310 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8311 if (has_domain < 0)
8312 goto error;
8313 if (!has_domain) {
8314 isl_multi_union_pw_aff_free(mupa1);
8315 return mupa2;
8318 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8319 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8320 if (is_params1 < 0 || is_params2 < 0)
8321 goto error;
8322 if (is_params1 != is_params2)
8323 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8324 isl_error_invalid,
8325 "cannot compute union of concrete domain and "
8326 "parameter constraints", goto error);
8327 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8328 if (!mupa1)
8329 goto error;
8330 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8331 isl_union_set_copy(mupa2->u.dom));
8332 if (!mupa1->u.dom)
8333 goto error;
8334 isl_multi_union_pw_aff_free(mupa2);
8335 return mupa1;
8336 error:
8337 isl_multi_union_pw_aff_free(mupa1);
8338 isl_multi_union_pw_aff_free(mupa2);
8339 return NULL;
8342 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8343 * with the actual sum on the shared domain and
8344 * the defined expression on the symmetric difference of the domains.
8346 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8347 __isl_take isl_multi_union_pw_aff *mupa1,
8348 __isl_take isl_multi_union_pw_aff *mupa2)
8350 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8351 &isl_multi_union_pw_aff_union_add_aligned);
8354 /* Construct and return a multi union piecewise affine expression
8355 * that is equal to the given multi affine expression.
8357 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8358 __isl_take isl_multi_aff *ma)
8360 isl_multi_pw_aff *mpa;
8362 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8363 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8366 /* Construct and return a multi union piecewise affine expression
8367 * that is equal to the given multi piecewise affine expression.
8369 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8370 __isl_take isl_multi_pw_aff *mpa)
8372 int i, n;
8373 isl_space *space;
8374 isl_multi_union_pw_aff *mupa;
8376 if (!mpa)
8377 return NULL;
8379 space = isl_multi_pw_aff_get_space(mpa);
8380 space = isl_space_range(space);
8381 mupa = isl_multi_union_pw_aff_alloc(space);
8383 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8384 for (i = 0; i < n; ++i) {
8385 isl_pw_aff *pa;
8386 isl_union_pw_aff *upa;
8388 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8389 upa = isl_union_pw_aff_from_pw_aff(pa);
8390 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8393 isl_multi_pw_aff_free(mpa);
8395 return mupa;
8398 /* Extract the range space of "pma" and assign it to *space.
8399 * If *space has already been set (through a previous call to this function),
8400 * then check that the range space is the same.
8402 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8404 isl_space **space = user;
8405 isl_space *pma_space;
8406 isl_bool equal;
8408 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8409 isl_pw_multi_aff_free(pma);
8411 if (!pma_space)
8412 return isl_stat_error;
8413 if (!*space) {
8414 *space = pma_space;
8415 return isl_stat_ok;
8418 equal = isl_space_is_equal(pma_space, *space);
8419 isl_space_free(pma_space);
8421 if (equal < 0)
8422 return isl_stat_error;
8423 if (!equal)
8424 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8425 "range spaces not the same", return isl_stat_error);
8426 return isl_stat_ok;
8429 /* Construct and return a multi union piecewise affine expression
8430 * that is equal to the given union piecewise multi affine expression.
8432 * In order to be able to perform the conversion, the input
8433 * needs to be non-empty and may only involve a single range space.
8435 * If the resulting multi union piecewise affine expression has
8436 * an explicit domain, then assign it the domain of the input.
8437 * In other cases, the domain is stored in the individual elements.
8439 __isl_give isl_multi_union_pw_aff *
8440 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8441 __isl_take isl_union_pw_multi_aff *upma)
8443 isl_space *space = NULL;
8444 isl_multi_union_pw_aff *mupa;
8445 int i, n;
8447 if (!upma)
8448 return NULL;
8449 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8450 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8451 "cannot extract range space from empty input",
8452 goto error);
8453 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8454 &space) < 0)
8455 goto error;
8457 if (!space)
8458 goto error;
8460 n = isl_space_dim(space, isl_dim_set);
8461 mupa = isl_multi_union_pw_aff_alloc(space);
8463 for (i = 0; i < n; ++i) {
8464 isl_union_pw_aff *upa;
8466 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8467 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8469 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8470 isl_union_set *dom;
8471 isl_union_pw_multi_aff *copy;
8473 copy = isl_union_pw_multi_aff_copy(upma);
8474 dom = isl_union_pw_multi_aff_domain(copy);
8475 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8478 isl_union_pw_multi_aff_free(upma);
8479 return mupa;
8480 error:
8481 isl_space_free(space);
8482 isl_union_pw_multi_aff_free(upma);
8483 return NULL;
8486 /* Try and create an isl_multi_union_pw_aff that is equivalent
8487 * to the given isl_union_map.
8488 * The isl_union_map is required to be single-valued in each space.
8489 * Moreover, it cannot be empty and all range spaces need to be the same.
8490 * Otherwise, an error is produced.
8492 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8493 __isl_take isl_union_map *umap)
8495 isl_union_pw_multi_aff *upma;
8497 upma = isl_union_pw_multi_aff_from_union_map(umap);
8498 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8501 /* Return a multiple union piecewise affine expression
8502 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8503 * have been aligned.
8505 * If the resulting multi union piecewise affine expression has
8506 * an explicit domain, then assign it the input domain.
8507 * In other cases, the domain is stored in the individual elements.
8509 static __isl_give isl_multi_union_pw_aff *
8510 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8511 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8513 int i, n;
8514 isl_space *space;
8515 isl_multi_union_pw_aff *mupa;
8517 if (!domain || !mv)
8518 goto error;
8520 n = isl_multi_val_dim(mv, isl_dim_set);
8521 space = isl_multi_val_get_space(mv);
8522 mupa = isl_multi_union_pw_aff_alloc(space);
8523 for (i = 0; i < n; ++i) {
8524 isl_val *v;
8525 isl_union_pw_aff *upa;
8527 v = isl_multi_val_get_val(mv, i);
8528 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8530 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8532 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8533 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8534 isl_union_set_copy(domain));
8536 isl_union_set_free(domain);
8537 isl_multi_val_free(mv);
8538 return mupa;
8539 error:
8540 isl_union_set_free(domain);
8541 isl_multi_val_free(mv);
8542 return NULL;
8545 /* Return a multiple union piecewise affine expression
8546 * that is equal to "mv" on "domain".
8548 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8549 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8551 isl_bool equal_params;
8553 if (!domain || !mv)
8554 goto error;
8555 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8556 if (equal_params < 0)
8557 goto error;
8558 if (equal_params)
8559 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8560 domain, mv);
8561 domain = isl_union_set_align_params(domain,
8562 isl_multi_val_get_space(mv));
8563 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8564 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8565 error:
8566 isl_union_set_free(domain);
8567 isl_multi_val_free(mv);
8568 return NULL;
8571 /* Return a multiple union piecewise affine expression
8572 * that is equal to "ma" on "domain".
8574 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8575 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8577 isl_pw_multi_aff *pma;
8579 pma = isl_pw_multi_aff_from_multi_aff(ma);
8580 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8583 /* Return a multiple union piecewise affine expression
8584 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8585 * have been aligned.
8587 * If the resulting multi union piecewise affine expression has
8588 * an explicit domain, then assign it the input domain.
8589 * In other cases, the domain is stored in the individual elements.
8591 static __isl_give isl_multi_union_pw_aff *
8592 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8593 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8595 int i, n;
8596 isl_space *space;
8597 isl_multi_union_pw_aff *mupa;
8599 if (!domain || !pma)
8600 goto error;
8602 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8603 space = isl_pw_multi_aff_get_space(pma);
8604 mupa = isl_multi_union_pw_aff_alloc(space);
8605 for (i = 0; i < n; ++i) {
8606 isl_pw_aff *pa;
8607 isl_union_pw_aff *upa;
8609 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8610 upa = isl_union_pw_aff_pw_aff_on_domain(
8611 isl_union_set_copy(domain), pa);
8612 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8614 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8615 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8616 isl_union_set_copy(domain));
8618 isl_union_set_free(domain);
8619 isl_pw_multi_aff_free(pma);
8620 return mupa;
8621 error:
8622 isl_union_set_free(domain);
8623 isl_pw_multi_aff_free(pma);
8624 return NULL;
8627 /* Return a multiple union piecewise affine expression
8628 * that is equal to "pma" on "domain".
8630 __isl_give isl_multi_union_pw_aff *
8631 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8632 __isl_take isl_pw_multi_aff *pma)
8634 isl_bool equal_params;
8635 isl_space *space;
8637 space = isl_pw_multi_aff_peek_space(pma);
8638 equal_params = isl_union_set_space_has_equal_params(domain, space);
8639 if (equal_params < 0)
8640 goto error;
8641 if (equal_params)
8642 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8643 domain, pma);
8644 domain = isl_union_set_align_params(domain,
8645 isl_pw_multi_aff_get_space(pma));
8646 pma = isl_pw_multi_aff_align_params(pma,
8647 isl_union_set_get_space(domain));
8648 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8649 pma);
8650 error:
8651 isl_union_set_free(domain);
8652 isl_pw_multi_aff_free(pma);
8653 return NULL;
8656 /* Return a union set containing those elements in the domains
8657 * of the elements of "mupa" where they are all zero.
8659 * If there are no elements, then simply return the entire domain.
8661 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8662 __isl_take isl_multi_union_pw_aff *mupa)
8664 int i, n;
8665 isl_union_pw_aff *upa;
8666 isl_union_set *zero;
8668 if (!mupa)
8669 return NULL;
8671 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8672 if (n == 0)
8673 return isl_multi_union_pw_aff_domain(mupa);
8675 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8676 zero = isl_union_pw_aff_zero_union_set(upa);
8678 for (i = 1; i < n; ++i) {
8679 isl_union_set *zero_i;
8681 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8682 zero_i = isl_union_pw_aff_zero_union_set(upa);
8684 zero = isl_union_set_intersect(zero, zero_i);
8687 isl_multi_union_pw_aff_free(mupa);
8688 return zero;
8691 /* Construct a union map mapping the shared domain
8692 * of the union piecewise affine expressions to the range of "mupa"
8693 * in the special case of a 0D multi union piecewise affine expression.
8695 * Construct a map between the explicit domain of "mupa" and
8696 * the range space.
8697 * Note that this assumes that the domain consists of explicit elements.
8699 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8700 __isl_take isl_multi_union_pw_aff *mupa)
8702 isl_bool is_params;
8703 isl_space *space;
8704 isl_union_set *dom, *ran;
8706 space = isl_multi_union_pw_aff_get_space(mupa);
8707 dom = isl_multi_union_pw_aff_domain(mupa);
8708 ran = isl_union_set_from_set(isl_set_universe(space));
8710 is_params = isl_union_set_is_params(dom);
8711 if (is_params < 0)
8712 dom = isl_union_set_free(dom);
8713 else if (is_params)
8714 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8715 "cannot create union map from expression without "
8716 "explicit domain elements",
8717 dom = isl_union_set_free(dom));
8719 return isl_union_map_from_domain_and_range(dom, ran);
8722 /* Construct a union map mapping the shared domain
8723 * of the union piecewise affine expressions to the range of "mupa"
8724 * with each dimension in the range equated to the
8725 * corresponding union piecewise affine expression.
8727 * If the input is zero-dimensional, then construct a mapping
8728 * from its explicit domain.
8730 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8731 __isl_take isl_multi_union_pw_aff *mupa)
8733 int i, n;
8734 isl_space *space;
8735 isl_union_map *umap;
8736 isl_union_pw_aff *upa;
8738 if (!mupa)
8739 return NULL;
8741 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8742 if (n == 0)
8743 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8745 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8746 umap = isl_union_map_from_union_pw_aff(upa);
8748 for (i = 1; i < n; ++i) {
8749 isl_union_map *umap_i;
8751 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8752 umap_i = isl_union_map_from_union_pw_aff(upa);
8753 umap = isl_union_map_flat_range_product(umap, umap_i);
8756 space = isl_multi_union_pw_aff_get_space(mupa);
8757 umap = isl_union_map_reset_range_space(umap, space);
8759 isl_multi_union_pw_aff_free(mupa);
8760 return umap;
8763 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8764 * "range" is the space from which to set the range space.
8765 * "res" collects the results.
8767 struct isl_union_pw_multi_aff_reset_range_space_data {
8768 isl_space *range;
8769 isl_union_pw_multi_aff *res;
8772 /* Replace the range space of "pma" by the range space of data->range and
8773 * add the result to data->res.
8775 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8777 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8778 isl_space *space;
8780 space = isl_pw_multi_aff_get_space(pma);
8781 space = isl_space_domain(space);
8782 space = isl_space_extend_domain_with_range(space,
8783 isl_space_copy(data->range));
8784 pma = isl_pw_multi_aff_reset_space(pma, space);
8785 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8787 return data->res ? isl_stat_ok : isl_stat_error;
8790 /* Replace the range space of all the piecewise affine expressions in "upma" by
8791 * the range space of "space".
8793 * This assumes that all these expressions have the same output dimension.
8795 * Since the spaces of the expressions change, so do their hash values.
8796 * We therefore need to create a new isl_union_pw_multi_aff.
8797 * Note that the hash value is currently computed based on the entire
8798 * space even though there can only be a single expression with a given
8799 * domain space.
8801 static __isl_give isl_union_pw_multi_aff *
8802 isl_union_pw_multi_aff_reset_range_space(
8803 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8805 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8806 isl_space *space_upma;
8808 space_upma = isl_union_pw_multi_aff_get_space(upma);
8809 data.res = isl_union_pw_multi_aff_empty(space_upma);
8810 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8811 &reset_range_space, &data) < 0)
8812 data.res = isl_union_pw_multi_aff_free(data.res);
8814 isl_space_free(space);
8815 isl_union_pw_multi_aff_free(upma);
8816 return data.res;
8819 /* Construct and return a union piecewise multi affine expression
8820 * that is equal to the given multi union piecewise affine expression,
8821 * in the special case of a 0D multi union piecewise affine expression.
8823 * Construct a union piecewise multi affine expression
8824 * on top of the explicit domain of the input.
8826 __isl_give isl_union_pw_multi_aff *
8827 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8828 __isl_take isl_multi_union_pw_aff *mupa)
8830 isl_space *space;
8831 isl_multi_val *mv;
8832 isl_union_set *domain;
8834 space = isl_multi_union_pw_aff_get_space(mupa);
8835 mv = isl_multi_val_zero(space);
8836 domain = isl_multi_union_pw_aff_domain(mupa);
8837 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8840 /* Construct and return a union piecewise multi affine expression
8841 * that is equal to the given multi union piecewise affine expression.
8843 * If the input is zero-dimensional, then
8844 * construct a union piecewise multi affine expression
8845 * on top of the explicit domain of the input.
8847 __isl_give isl_union_pw_multi_aff *
8848 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8849 __isl_take isl_multi_union_pw_aff *mupa)
8851 int i, n;
8852 isl_space *space;
8853 isl_union_pw_multi_aff *upma;
8854 isl_union_pw_aff *upa;
8856 if (!mupa)
8857 return NULL;
8859 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8860 if (n == 0)
8861 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8863 space = isl_multi_union_pw_aff_get_space(mupa);
8864 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8865 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8867 for (i = 1; i < n; ++i) {
8868 isl_union_pw_multi_aff *upma_i;
8870 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8871 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8872 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8875 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8877 isl_multi_union_pw_aff_free(mupa);
8878 return upma;
8881 /* Intersect the range of "mupa" with "range",
8882 * in the special case where "mupa" is 0D.
8884 * Intersect the domain of "mupa" with the constraints on the parameters
8885 * of "range".
8887 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8888 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8890 range = isl_set_params(range);
8891 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8892 return mupa;
8895 /* Intersect the range of "mupa" with "range".
8896 * That is, keep only those domain elements that have a function value
8897 * in "range".
8899 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8900 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8902 isl_union_pw_multi_aff *upma;
8903 isl_union_set *domain;
8904 isl_space *space;
8905 int n;
8906 int match;
8908 if (!mupa || !range)
8909 goto error;
8911 space = isl_set_get_space(range);
8912 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8913 space, isl_dim_set);
8914 isl_space_free(space);
8915 if (match < 0)
8916 goto error;
8917 if (!match)
8918 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8919 "space don't match", goto error);
8920 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8921 if (n == 0)
8922 return mupa_intersect_range_0D(mupa, range);
8924 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8925 isl_multi_union_pw_aff_copy(mupa));
8926 domain = isl_union_set_from_set(range);
8927 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8928 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8930 return mupa;
8931 error:
8932 isl_multi_union_pw_aff_free(mupa);
8933 isl_set_free(range);
8934 return NULL;
8937 /* Return the shared domain of the elements of "mupa",
8938 * in the special case where "mupa" is zero-dimensional.
8940 * Return the explicit domain of "mupa".
8941 * Note that this domain may be a parameter set, either
8942 * because "mupa" is meant to live in a set space or
8943 * because no explicit domain has been set.
8945 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8946 __isl_take isl_multi_union_pw_aff *mupa)
8948 isl_union_set *dom;
8950 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8951 isl_multi_union_pw_aff_free(mupa);
8953 return dom;
8956 /* Return the shared domain of the elements of "mupa".
8958 * If "mupa" is zero-dimensional, then return its explicit domain.
8960 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8961 __isl_take isl_multi_union_pw_aff *mupa)
8963 int i, n;
8964 isl_union_pw_aff *upa;
8965 isl_union_set *dom;
8967 if (!mupa)
8968 return NULL;
8970 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8971 if (n == 0)
8972 return isl_multi_union_pw_aff_domain_0D(mupa);
8974 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8975 dom = isl_union_pw_aff_domain(upa);
8976 for (i = 1; i < n; ++i) {
8977 isl_union_set *dom_i;
8979 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8980 dom_i = isl_union_pw_aff_domain(upa);
8981 dom = isl_union_set_intersect(dom, dom_i);
8984 isl_multi_union_pw_aff_free(mupa);
8985 return dom;
8988 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8989 * In particular, the spaces have been aligned.
8990 * The result is defined over the shared domain of the elements of "mupa"
8992 * We first extract the parametric constant part of "aff" and
8993 * define that over the shared domain.
8994 * Then we iterate over all input dimensions of "aff" and add the corresponding
8995 * multiples of the elements of "mupa".
8996 * Finally, we consider the integer divisions, calling the function
8997 * recursively to obtain an isl_union_pw_aff corresponding to the
8998 * integer division argument.
9000 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9001 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9003 int i, n_in, n_div;
9004 isl_union_pw_aff *upa;
9005 isl_union_set *uset;
9006 isl_val *v;
9007 isl_aff *cst;
9009 n_in = isl_aff_dim(aff, isl_dim_in);
9010 n_div = isl_aff_dim(aff, isl_dim_div);
9012 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9013 cst = isl_aff_copy(aff);
9014 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9015 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9016 cst = isl_aff_project_domain_on_params(cst);
9017 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9019 for (i = 0; i < n_in; ++i) {
9020 isl_union_pw_aff *upa_i;
9022 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9023 continue;
9024 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9025 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9026 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9027 upa = isl_union_pw_aff_add(upa, upa_i);
9030 for (i = 0; i < n_div; ++i) {
9031 isl_aff *div;
9032 isl_union_pw_aff *upa_i;
9034 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9035 continue;
9036 div = isl_aff_get_div(aff, i);
9037 upa_i = multi_union_pw_aff_apply_aff(
9038 isl_multi_union_pw_aff_copy(mupa), div);
9039 upa_i = isl_union_pw_aff_floor(upa_i);
9040 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9041 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9042 upa = isl_union_pw_aff_add(upa, upa_i);
9045 isl_multi_union_pw_aff_free(mupa);
9046 isl_aff_free(aff);
9048 return upa;
9051 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9052 * with the domain of "aff".
9053 * Furthermore, the dimension of this space needs to be greater than zero.
9054 * The result is defined over the shared domain of the elements of "mupa"
9056 * We perform these checks and then hand over control to
9057 * multi_union_pw_aff_apply_aff.
9059 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9060 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9062 isl_space *space1, *space2;
9063 int equal;
9065 mupa = isl_multi_union_pw_aff_align_params(mupa,
9066 isl_aff_get_space(aff));
9067 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9068 if (!mupa || !aff)
9069 goto error;
9071 space1 = isl_multi_union_pw_aff_get_space(mupa);
9072 space2 = isl_aff_get_domain_space(aff);
9073 equal = isl_space_is_equal(space1, space2);
9074 isl_space_free(space1);
9075 isl_space_free(space2);
9076 if (equal < 0)
9077 goto error;
9078 if (!equal)
9079 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9080 "spaces don't match", goto error);
9081 if (isl_aff_dim(aff, isl_dim_in) == 0)
9082 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9083 "cannot determine domains", goto error);
9085 return multi_union_pw_aff_apply_aff(mupa, aff);
9086 error:
9087 isl_multi_union_pw_aff_free(mupa);
9088 isl_aff_free(aff);
9089 return NULL;
9092 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9093 * The space of "mupa" is known to be compatible with the domain of "ma".
9095 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9096 * on the domain of "mupa".
9098 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9099 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9101 isl_union_set *dom;
9103 dom = isl_multi_union_pw_aff_domain(mupa);
9104 ma = isl_multi_aff_project_domain_on_params(ma);
9106 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9109 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9110 * with the domain of "ma".
9111 * The result is defined over the shared domain of the elements of "mupa"
9113 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9114 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9116 isl_space *space1, *space2;
9117 isl_multi_union_pw_aff *res;
9118 int equal;
9119 int i, n_out;
9121 mupa = isl_multi_union_pw_aff_align_params(mupa,
9122 isl_multi_aff_get_space(ma));
9123 ma = isl_multi_aff_align_params(ma,
9124 isl_multi_union_pw_aff_get_space(mupa));
9125 if (!mupa || !ma)
9126 goto error;
9128 space1 = isl_multi_union_pw_aff_get_space(mupa);
9129 space2 = isl_multi_aff_get_domain_space(ma);
9130 equal = isl_space_is_equal(space1, space2);
9131 isl_space_free(space1);
9132 isl_space_free(space2);
9133 if (equal < 0)
9134 goto error;
9135 if (!equal)
9136 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9137 "spaces don't match", goto error);
9138 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9139 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9140 return mupa_apply_multi_aff_0D(mupa, ma);
9142 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9143 res = isl_multi_union_pw_aff_alloc(space1);
9145 for (i = 0; i < n_out; ++i) {
9146 isl_aff *aff;
9147 isl_union_pw_aff *upa;
9149 aff = isl_multi_aff_get_aff(ma, i);
9150 upa = multi_union_pw_aff_apply_aff(
9151 isl_multi_union_pw_aff_copy(mupa), aff);
9152 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9155 isl_multi_aff_free(ma);
9156 isl_multi_union_pw_aff_free(mupa);
9157 return res;
9158 error:
9159 isl_multi_union_pw_aff_free(mupa);
9160 isl_multi_aff_free(ma);
9161 return NULL;
9164 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9165 * The space of "mupa" is known to be compatible with the domain of "pa".
9167 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9168 * on the domain of "mupa".
9170 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9171 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9173 isl_union_set *dom;
9175 dom = isl_multi_union_pw_aff_domain(mupa);
9176 pa = isl_pw_aff_project_domain_on_params(pa);
9178 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9181 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9182 * with the domain of "pa".
9183 * Furthermore, the dimension of this space needs to be greater than zero.
9184 * The result is defined over the shared domain of the elements of "mupa"
9186 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9187 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9189 int i;
9190 int equal;
9191 isl_space *space, *space2;
9192 isl_union_pw_aff *upa;
9194 mupa = isl_multi_union_pw_aff_align_params(mupa,
9195 isl_pw_aff_get_space(pa));
9196 pa = isl_pw_aff_align_params(pa,
9197 isl_multi_union_pw_aff_get_space(mupa));
9198 if (!mupa || !pa)
9199 goto error;
9201 space = isl_multi_union_pw_aff_get_space(mupa);
9202 space2 = isl_pw_aff_get_domain_space(pa);
9203 equal = isl_space_is_equal(space, space2);
9204 isl_space_free(space);
9205 isl_space_free(space2);
9206 if (equal < 0)
9207 goto error;
9208 if (!equal)
9209 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9210 "spaces don't match", goto error);
9211 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9212 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9214 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9215 upa = isl_union_pw_aff_empty(space);
9217 for (i = 0; i < pa->n; ++i) {
9218 isl_aff *aff;
9219 isl_set *domain;
9220 isl_multi_union_pw_aff *mupa_i;
9221 isl_union_pw_aff *upa_i;
9223 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9224 domain = isl_set_copy(pa->p[i].set);
9225 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9226 aff = isl_aff_copy(pa->p[i].aff);
9227 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9228 upa = isl_union_pw_aff_union_add(upa, upa_i);
9231 isl_multi_union_pw_aff_free(mupa);
9232 isl_pw_aff_free(pa);
9233 return upa;
9234 error:
9235 isl_multi_union_pw_aff_free(mupa);
9236 isl_pw_aff_free(pa);
9237 return NULL;
9240 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9241 * The space of "mupa" is known to be compatible with the domain of "pma".
9243 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9244 * on the domain of "mupa".
9246 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9247 __isl_take isl_multi_union_pw_aff *mupa,
9248 __isl_take isl_pw_multi_aff *pma)
9250 isl_union_set *dom;
9252 dom = isl_multi_union_pw_aff_domain(mupa);
9253 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9255 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9258 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9259 * with the domain of "pma".
9260 * The result is defined over the shared domain of the elements of "mupa"
9262 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9263 __isl_take isl_multi_union_pw_aff *mupa,
9264 __isl_take isl_pw_multi_aff *pma)
9266 isl_space *space1, *space2;
9267 isl_multi_union_pw_aff *res;
9268 int equal;
9269 int i, n_out;
9271 mupa = isl_multi_union_pw_aff_align_params(mupa,
9272 isl_pw_multi_aff_get_space(pma));
9273 pma = isl_pw_multi_aff_align_params(pma,
9274 isl_multi_union_pw_aff_get_space(mupa));
9275 if (!mupa || !pma)
9276 goto error;
9278 space1 = isl_multi_union_pw_aff_get_space(mupa);
9279 space2 = isl_pw_multi_aff_get_domain_space(pma);
9280 equal = isl_space_is_equal(space1, space2);
9281 isl_space_free(space1);
9282 isl_space_free(space2);
9283 if (equal < 0)
9284 goto error;
9285 if (!equal)
9286 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9287 "spaces don't match", goto error);
9288 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9289 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9290 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9292 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9293 res = isl_multi_union_pw_aff_alloc(space1);
9295 for (i = 0; i < n_out; ++i) {
9296 isl_pw_aff *pa;
9297 isl_union_pw_aff *upa;
9299 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9300 upa = isl_multi_union_pw_aff_apply_pw_aff(
9301 isl_multi_union_pw_aff_copy(mupa), pa);
9302 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9305 isl_pw_multi_aff_free(pma);
9306 isl_multi_union_pw_aff_free(mupa);
9307 return res;
9308 error:
9309 isl_multi_union_pw_aff_free(mupa);
9310 isl_pw_multi_aff_free(pma);
9311 return NULL;
9314 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9315 * If the explicit domain only keeps track of constraints on the parameters,
9316 * then only update those constraints.
9318 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9319 __isl_take isl_multi_union_pw_aff *mupa,
9320 __isl_keep isl_union_pw_multi_aff *upma)
9322 isl_bool is_params;
9324 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9325 return isl_multi_union_pw_aff_free(mupa);
9327 mupa = isl_multi_union_pw_aff_cow(mupa);
9328 if (!mupa)
9329 return NULL;
9331 is_params = isl_union_set_is_params(mupa->u.dom);
9332 if (is_params < 0)
9333 return isl_multi_union_pw_aff_free(mupa);
9335 upma = isl_union_pw_multi_aff_copy(upma);
9336 if (is_params)
9337 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9338 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9339 else
9340 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9341 mupa->u.dom, upma);
9342 if (!mupa->u.dom)
9343 return isl_multi_union_pw_aff_free(mupa);
9344 return mupa;
9347 /* Compute the pullback of "mupa" by the function represented by "upma".
9348 * In other words, plug in "upma" in "mupa". The result contains
9349 * expressions defined over the domain space of "upma".
9351 * Run over all elements of "mupa" and plug in "upma" in each of them.
9353 * If "mupa" has an explicit domain, then it is this domain
9354 * that needs to undergo a pullback instead, i.e., a preimage.
9356 __isl_give isl_multi_union_pw_aff *
9357 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9358 __isl_take isl_multi_union_pw_aff *mupa,
9359 __isl_take isl_union_pw_multi_aff *upma)
9361 int i, n;
9363 mupa = isl_multi_union_pw_aff_align_params(mupa,
9364 isl_union_pw_multi_aff_get_space(upma));
9365 upma = isl_union_pw_multi_aff_align_params(upma,
9366 isl_multi_union_pw_aff_get_space(mupa));
9367 mupa = isl_multi_union_pw_aff_cow(mupa);
9368 if (!mupa || !upma)
9369 goto error;
9371 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9372 for (i = 0; i < n; ++i) {
9373 isl_union_pw_aff *upa;
9375 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9376 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9377 isl_union_pw_multi_aff_copy(upma));
9378 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9381 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9382 mupa = preimage_explicit_domain(mupa, upma);
9384 isl_union_pw_multi_aff_free(upma);
9385 return mupa;
9386 error:
9387 isl_multi_union_pw_aff_free(mupa);
9388 isl_union_pw_multi_aff_free(upma);
9389 return NULL;
9392 /* Extract the sequence of elements in "mupa" with domain space "space"
9393 * (ignoring parameters).
9395 * For the elements of "mupa" that are not defined on the specified space,
9396 * the corresponding element in the result is empty.
9398 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9399 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9401 int i, n;
9402 isl_space *space_mpa;
9403 isl_multi_pw_aff *mpa;
9405 if (!mupa || !space)
9406 goto error;
9408 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9409 space = isl_space_replace_params(space, space_mpa);
9410 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9411 space_mpa);
9412 mpa = isl_multi_pw_aff_alloc(space_mpa);
9414 space = isl_space_from_domain(space);
9415 space = isl_space_add_dims(space, isl_dim_out, 1);
9416 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9417 for (i = 0; i < n; ++i) {
9418 isl_union_pw_aff *upa;
9419 isl_pw_aff *pa;
9421 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9422 pa = isl_union_pw_aff_extract_pw_aff(upa,
9423 isl_space_copy(space));
9424 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9425 isl_union_pw_aff_free(upa);
9428 isl_space_free(space);
9429 return mpa;
9430 error:
9431 isl_space_free(space);
9432 return NULL;
9435 /* Evaluate the affine function "aff" in the void point "pnt".
9436 * In particular, return the value NaN.
9438 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9439 __isl_take isl_point *pnt)
9441 isl_ctx *ctx;
9443 ctx = isl_point_get_ctx(pnt);
9444 isl_aff_free(aff);
9445 isl_point_free(pnt);
9446 return isl_val_nan(ctx);
9449 /* Evaluate the affine expression "aff"
9450 * in the coordinates (with denominator) "pnt".
9452 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9453 __isl_keep isl_vec *pnt)
9455 isl_int n, d;
9456 isl_ctx *ctx;
9457 isl_val *v;
9459 if (!aff || !pnt)
9460 return NULL;
9462 ctx = isl_vec_get_ctx(aff);
9463 isl_int_init(n);
9464 isl_int_init(d);
9465 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9466 isl_int_mul(d, aff->el[0], pnt->el[0]);
9467 v = isl_val_rat_from_isl_int(ctx, n, d);
9468 v = isl_val_normalize(v);
9469 isl_int_clear(n);
9470 isl_int_clear(d);
9472 return v;
9475 /* Check that the domain space of "aff" is equal to "space".
9477 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9478 __isl_keep isl_space *space)
9480 isl_bool ok;
9482 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9483 if (ok < 0)
9484 return isl_stat_error;
9485 if (!ok)
9486 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9487 "incompatible spaces", return isl_stat_error);
9488 return isl_stat_ok;
9491 /* Evaluate the affine function "aff" in "pnt".
9493 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9494 __isl_take isl_point *pnt)
9496 isl_bool is_void;
9497 isl_val *v;
9498 isl_local_space *ls;
9500 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9501 goto error;
9502 is_void = isl_point_is_void(pnt);
9503 if (is_void < 0)
9504 goto error;
9505 if (is_void)
9506 return eval_void(aff, pnt);
9508 ls = isl_aff_get_domain_local_space(aff);
9509 pnt = isl_local_space_lift_point(ls, pnt);
9511 v = eval(aff->v, isl_point_peek_vec(pnt));
9513 isl_aff_free(aff);
9514 isl_point_free(pnt);
9516 return v;
9517 error:
9518 isl_aff_free(aff);
9519 isl_point_free(pnt);
9520 return NULL;