isl_test.c: test_lexmin: abort on failed test
[isl.git] / isl_aff.c
blob39e232d5d7a3f044a7a1195c03bbee075699e12a
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2018 Cerebras Systems
8 * Use of this software is governed by the MIT license
10 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
11 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
12 * 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
15 * B.P. 105 - 78153 Le Chesnay, France
16 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
19 #include <isl_ctx_private.h>
20 #include <isl_map_private.h>
21 #include <isl_union_map_private.h>
22 #include <isl_aff_private.h>
23 #include <isl_space_private.h>
24 #include <isl_local_space_private.h>
25 #include <isl_vec_private.h>
26 #include <isl_mat_private.h>
27 #include <isl_id_private.h>
28 #include <isl/constraint.h>
29 #include <isl_seq.h>
30 #include <isl/set.h>
31 #include <isl_val_private.h>
32 #include <isl_point_private.h>
33 #include <isl_config.h>
35 #undef EL_BASE
36 #define EL_BASE aff
38 #include <isl_list_templ.c>
40 #undef EL_BASE
41 #define EL_BASE pw_aff
43 #include <isl_list_templ.c>
45 #undef EL_BASE
46 #define EL_BASE pw_multi_aff
48 #include <isl_list_templ.c>
50 #undef EL_BASE
51 #define EL_BASE union_pw_aff
53 #include <isl_list_templ.c>
55 #undef EL_BASE
56 #define EL_BASE union_pw_multi_aff
58 #include <isl_list_templ.c>
60 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
61 __isl_take isl_vec *v)
63 isl_aff *aff;
65 if (!ls || !v)
66 goto error;
68 aff = isl_calloc_type(v->ctx, struct isl_aff);
69 if (!aff)
70 goto error;
72 aff->ref = 1;
73 aff->ls = ls;
74 aff->v = v;
76 return aff;
77 error:
78 isl_local_space_free(ls);
79 isl_vec_free(v);
80 return NULL;
83 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
85 isl_ctx *ctx;
86 isl_vec *v;
87 isl_size total;
89 if (!ls)
90 return NULL;
92 ctx = isl_local_space_get_ctx(ls);
93 if (!isl_local_space_divs_known(ls))
94 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
95 goto error);
96 if (!isl_local_space_is_set(ls))
97 isl_die(ctx, isl_error_invalid,
98 "domain of affine expression should be a set",
99 goto error);
101 total = isl_local_space_dim(ls, isl_dim_all);
102 if (total < 0)
103 goto error;
104 v = isl_vec_alloc(ctx, 1 + 1 + total);
105 return isl_aff_alloc_vec(ls, v);
106 error:
107 isl_local_space_free(ls);
108 return NULL;
111 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
113 isl_aff *aff;
115 aff = isl_aff_alloc(ls);
116 if (!aff)
117 return NULL;
119 isl_int_set_si(aff->v->el[0], 1);
120 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
122 return aff;
125 /* Return a piecewise affine expression defined on the specified domain
126 * that is equal to zero.
128 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
130 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
133 /* Return an affine expression defined on the specified domain
134 * that represents NaN.
136 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
138 isl_aff *aff;
140 aff = isl_aff_alloc(ls);
141 if (!aff)
142 return NULL;
144 isl_seq_clr(aff->v->el, aff->v->size);
146 return aff;
149 /* Return a piecewise affine expression defined on the specified domain
150 * that represents NaN.
152 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
154 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
157 /* Return an affine expression that is equal to "val" on
158 * domain local space "ls".
160 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
161 __isl_take isl_val *val)
163 isl_aff *aff;
165 if (!ls || !val)
166 goto error;
167 if (!isl_val_is_rat(val))
168 isl_die(isl_val_get_ctx(val), isl_error_invalid,
169 "expecting rational value", goto error);
171 aff = isl_aff_alloc(isl_local_space_copy(ls));
172 if (!aff)
173 goto error;
175 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
176 isl_int_set(aff->v->el[1], val->n);
177 isl_int_set(aff->v->el[0], val->d);
179 isl_local_space_free(ls);
180 isl_val_free(val);
181 return aff;
182 error:
183 isl_local_space_free(ls);
184 isl_val_free(val);
185 return NULL;
188 /* Return an affine expression that is equal to the specified dimension
189 * in "ls".
191 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
192 enum isl_dim_type type, unsigned pos)
194 isl_space *space;
195 isl_aff *aff;
197 if (!ls)
198 return NULL;
200 space = isl_local_space_get_space(ls);
201 if (!space)
202 goto error;
203 if (isl_space_is_map(space))
204 isl_die(isl_space_get_ctx(space), isl_error_invalid,
205 "expecting (parameter) set space", goto error);
206 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
207 goto error;
209 isl_space_free(space);
210 aff = isl_aff_alloc(ls);
211 if (!aff)
212 return NULL;
214 pos += isl_local_space_offset(aff->ls, type);
216 isl_int_set_si(aff->v->el[0], 1);
217 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
218 isl_int_set_si(aff->v->el[1 + pos], 1);
220 return aff;
221 error:
222 isl_local_space_free(ls);
223 isl_space_free(space);
224 return NULL;
227 /* Return a piecewise affine expression that is equal to
228 * the specified dimension in "ls".
230 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
231 enum isl_dim_type type, unsigned pos)
233 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
236 /* Return an affine expression that is equal to the parameter
237 * in the domain space "space" with identifier "id".
239 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
240 __isl_take isl_space *space, __isl_take isl_id *id)
242 int pos;
243 isl_local_space *ls;
245 if (!space || !id)
246 goto error;
247 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
248 if (pos < 0)
249 isl_die(isl_space_get_ctx(space), isl_error_invalid,
250 "parameter not found in space", goto error);
251 isl_id_free(id);
252 ls = isl_local_space_from_space(space);
253 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
254 error:
255 isl_space_free(space);
256 isl_id_free(id);
257 return NULL;
260 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
262 if (!aff)
263 return NULL;
265 aff->ref++;
266 return aff;
269 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
271 if (!aff)
272 return NULL;
274 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
275 isl_vec_copy(aff->v));
278 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
280 if (!aff)
281 return NULL;
283 if (aff->ref == 1)
284 return aff;
285 aff->ref--;
286 return isl_aff_dup(aff);
289 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
291 if (!aff)
292 return NULL;
294 if (--aff->ref > 0)
295 return NULL;
297 isl_local_space_free(aff->ls);
298 isl_vec_free(aff->v);
300 free(aff);
302 return NULL;
305 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
307 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
310 /* Return a hash value that digests "aff".
312 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
314 uint32_t hash, ls_hash, v_hash;
316 if (!aff)
317 return 0;
319 hash = isl_hash_init();
320 ls_hash = isl_local_space_get_hash(aff->ls);
321 isl_hash_hash(hash, ls_hash);
322 v_hash = isl_vec_get_hash(aff->v);
323 isl_hash_hash(hash, v_hash);
325 return hash;
328 /* Externally, an isl_aff has a map space, but internally, the
329 * ls field corresponds to the domain of that space.
331 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
333 if (!aff)
334 return isl_size_error;
335 if (type == isl_dim_out)
336 return 1;
337 if (type == isl_dim_in)
338 type = isl_dim_set;
339 return isl_local_space_dim(aff->ls, type);
342 /* Return the position of the dimension of the given type and name
343 * in "aff".
344 * Return -1 if no such dimension can be found.
346 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
347 const char *name)
349 if (!aff)
350 return -1;
351 if (type == isl_dim_out)
352 return -1;
353 if (type == isl_dim_in)
354 type = isl_dim_set;
355 return isl_local_space_find_dim_by_name(aff->ls, type, name);
358 /* Return the domain space of "aff".
360 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
362 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
365 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
367 return isl_space_copy(isl_aff_peek_domain_space(aff));
370 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
372 isl_space *space;
373 if (!aff)
374 return NULL;
375 space = isl_local_space_get_space(aff->ls);
376 space = isl_space_from_domain(space);
377 space = isl_space_add_dims(space, isl_dim_out, 1);
378 return space;
381 __isl_give isl_local_space *isl_aff_get_domain_local_space(
382 __isl_keep isl_aff *aff)
384 return aff ? isl_local_space_copy(aff->ls) : NULL;
387 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
389 isl_local_space *ls;
390 if (!aff)
391 return NULL;
392 ls = isl_local_space_copy(aff->ls);
393 ls = isl_local_space_from_domain(ls);
394 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
395 return ls;
398 /* Return the local space of the domain of "aff".
399 * This may be either a copy or the local space itself
400 * if there is only one reference to "aff".
401 * This allows the local space to be modified inplace
402 * if both the expression and its local space have only a single reference.
403 * The caller is not allowed to modify "aff" between this call and
404 * a subsequent call to isl_aff_restore_domain_local_space.
405 * The only exception is that isl_aff_free can be called instead.
407 __isl_give isl_local_space *isl_aff_take_domain_local_space(
408 __isl_keep isl_aff *aff)
410 isl_local_space *ls;
412 if (!aff)
413 return NULL;
414 if (aff->ref != 1)
415 return isl_aff_get_domain_local_space(aff);
416 ls = aff->ls;
417 aff->ls = NULL;
418 return ls;
421 /* Set the local space of the domain of "aff" to "ls",
422 * where the local space of "aff" may be missing
423 * due to a preceding call to isl_aff_take_domain_local_space.
424 * However, in this case, "aff" only has a single reference and
425 * then the call to isl_aff_cow has no effect.
427 __isl_give isl_aff *isl_aff_restore_domain_local_space(
428 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
430 if (!aff || !ls)
431 goto error;
433 if (aff->ls == ls) {
434 isl_local_space_free(ls);
435 return aff;
438 aff = isl_aff_cow(aff);
439 if (!aff)
440 goto error;
441 isl_local_space_free(aff->ls);
442 aff->ls = ls;
444 return aff;
445 error:
446 isl_aff_free(aff);
447 isl_local_space_free(ls);
448 return NULL;
451 /* Externally, an isl_aff has a map space, but internally, the
452 * ls field corresponds to the domain of that space.
454 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
455 enum isl_dim_type type, unsigned pos)
457 if (!aff)
458 return NULL;
459 if (type == isl_dim_out)
460 return NULL;
461 if (type == isl_dim_in)
462 type = isl_dim_set;
463 return isl_local_space_get_dim_name(aff->ls, type, pos);
466 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
467 __isl_take isl_space *dim)
469 aff = isl_aff_cow(aff);
470 if (!aff || !dim)
471 goto error;
473 aff->ls = isl_local_space_reset_space(aff->ls, dim);
474 if (!aff->ls)
475 return isl_aff_free(aff);
477 return aff;
478 error:
479 isl_aff_free(aff);
480 isl_space_free(dim);
481 return NULL;
484 /* Reset the space of "aff". This function is called from isl_pw_templ.c
485 * and doesn't know if the space of an element object is represented
486 * directly or through its domain. It therefore passes along both.
488 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
489 __isl_take isl_space *space, __isl_take isl_space *domain)
491 isl_space_free(space);
492 return isl_aff_reset_domain_space(aff, domain);
495 /* Reorder the coefficients of the affine expression based
496 * on the given reordering.
497 * The reordering r is assumed to have been extended with the local
498 * variables.
500 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
501 __isl_take isl_reordering *r, int n_div)
503 isl_space *space;
504 isl_vec *res;
505 isl_size dim;
506 int i;
508 if (!vec || !r)
509 goto error;
511 space = isl_reordering_peek_space(r);
512 dim = isl_space_dim(space, isl_dim_all);
513 if (dim < 0)
514 goto error;
515 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
516 if (!res)
517 goto error;
518 isl_seq_cpy(res->el, vec->el, 2);
519 isl_seq_clr(res->el + 2, res->size - 2);
520 for (i = 0; i < r->len; ++i)
521 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
523 isl_reordering_free(r);
524 isl_vec_free(vec);
525 return res;
526 error:
527 isl_vec_free(vec);
528 isl_reordering_free(r);
529 return NULL;
532 /* Reorder the dimensions of the domain of "aff" according
533 * to the given reordering.
535 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
536 __isl_take isl_reordering *r)
538 aff = isl_aff_cow(aff);
539 if (!aff)
540 goto error;
542 r = isl_reordering_extend(r, aff->ls->div->n_row);
543 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
544 aff->ls->div->n_row);
545 aff->ls = isl_local_space_realign(aff->ls, r);
547 if (!aff->v || !aff->ls)
548 return isl_aff_free(aff);
550 return aff;
551 error:
552 isl_aff_free(aff);
553 isl_reordering_free(r);
554 return NULL;
557 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
558 __isl_take isl_space *model)
560 isl_bool equal_params;
562 if (!aff || !model)
563 goto error;
565 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
566 if (equal_params < 0)
567 goto error;
568 if (!equal_params) {
569 isl_reordering *exp;
571 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
572 exp = isl_reordering_extend_space(exp,
573 isl_aff_get_domain_space(aff));
574 aff = isl_aff_realign_domain(aff, exp);
577 isl_space_free(model);
578 return aff;
579 error:
580 isl_space_free(model);
581 isl_aff_free(aff);
582 return NULL;
585 /* Given an affine function "aff" defined over a parameter domain,
586 * convert it to a function defined over a domain corresponding
587 * to "domain".
588 * Any parameters with identifiers in "domain" are reinterpreted
589 * as the corresponding domain dimensions.
591 __isl_give isl_aff *isl_aff_unbind_params_insert_domain(
592 __isl_take isl_aff *aff, __isl_take isl_multi_id *domain)
594 isl_bool is_params;
595 isl_space *space;
596 isl_reordering *r;
598 space = isl_aff_peek_domain_space(aff);
599 is_params = isl_space_is_params(space);
600 if (is_params < 0)
601 domain = isl_multi_id_free(domain);
602 else if (!is_params)
603 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
604 "expecting function with parameter domain",
605 domain = isl_multi_id_free(domain));
606 r = isl_reordering_unbind_params_insert_domain(space, domain);
607 isl_multi_id_free(domain);
609 return isl_aff_realign_domain(aff, r);
612 /* Is "aff" obviously equal to zero?
614 * If the denominator is zero, then "aff" is not equal to zero.
616 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
618 int pos;
620 if (!aff)
621 return isl_bool_error;
623 if (isl_int_is_zero(aff->v->el[0]))
624 return isl_bool_false;
625 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
626 return isl_bool_ok(pos < 0);
629 /* Does "aff" represent NaN?
631 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
633 if (!aff)
634 return isl_bool_error;
636 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
639 /* Are "aff1" and "aff2" obviously equal?
641 * NaN is not equal to anything, not even to another NaN.
643 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
644 __isl_keep isl_aff *aff2)
646 isl_bool equal;
648 if (!aff1 || !aff2)
649 return isl_bool_error;
651 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
652 return isl_bool_false;
654 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
655 if (equal < 0 || !equal)
656 return equal;
658 return isl_vec_is_equal(aff1->v, aff2->v);
661 /* Return the common denominator of "aff" in "v".
663 * We cannot return anything meaningful in case of a NaN.
665 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
667 if (!aff)
668 return isl_stat_error;
669 if (isl_aff_is_nan(aff))
670 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
671 "cannot get denominator of NaN", return isl_stat_error);
672 isl_int_set(*v, aff->v->el[0]);
673 return isl_stat_ok;
676 /* Return the common denominator of "aff".
678 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
680 isl_ctx *ctx;
682 if (!aff)
683 return NULL;
685 ctx = isl_aff_get_ctx(aff);
686 if (isl_aff_is_nan(aff))
687 return isl_val_nan(ctx);
688 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
691 /* Return the constant term of "aff".
693 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
695 isl_ctx *ctx;
696 isl_val *v;
698 if (!aff)
699 return NULL;
701 ctx = isl_aff_get_ctx(aff);
702 if (isl_aff_is_nan(aff))
703 return isl_val_nan(ctx);
704 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
705 return isl_val_normalize(v);
708 /* Return the coefficient of the variable of type "type" at position "pos"
709 * of "aff".
711 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
712 enum isl_dim_type type, int pos)
714 isl_ctx *ctx;
715 isl_val *v;
717 if (!aff)
718 return NULL;
720 ctx = isl_aff_get_ctx(aff);
721 if (type == isl_dim_out)
722 isl_die(ctx, isl_error_invalid,
723 "output/set dimension does not have a coefficient",
724 return NULL);
725 if (type == isl_dim_in)
726 type = isl_dim_set;
728 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
729 return NULL;
731 if (isl_aff_is_nan(aff))
732 return isl_val_nan(ctx);
733 pos += isl_local_space_offset(aff->ls, type);
734 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
735 return isl_val_normalize(v);
738 /* Return the sign of the coefficient of the variable of type "type"
739 * at position "pos" of "aff".
741 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
742 int pos)
744 isl_ctx *ctx;
746 if (!aff)
747 return 0;
749 ctx = isl_aff_get_ctx(aff);
750 if (type == isl_dim_out)
751 isl_die(ctx, isl_error_invalid,
752 "output/set dimension does not have a coefficient",
753 return 0);
754 if (type == isl_dim_in)
755 type = isl_dim_set;
757 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
758 return 0;
760 pos += isl_local_space_offset(aff->ls, type);
761 return isl_int_sgn(aff->v->el[1 + pos]);
764 /* Replace the numerator of the constant term of "aff" by "v".
766 * A NaN is unaffected by this operation.
768 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
770 if (!aff)
771 return NULL;
772 if (isl_aff_is_nan(aff))
773 return aff;
774 aff = isl_aff_cow(aff);
775 if (!aff)
776 return NULL;
778 aff->v = isl_vec_cow(aff->v);
779 if (!aff->v)
780 return isl_aff_free(aff);
782 isl_int_set(aff->v->el[1], v);
784 return aff;
787 /* Replace the constant term of "aff" by "v".
789 * A NaN is unaffected by this operation.
791 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
792 __isl_take isl_val *v)
794 if (!aff || !v)
795 goto error;
797 if (isl_aff_is_nan(aff)) {
798 isl_val_free(v);
799 return aff;
802 if (!isl_val_is_rat(v))
803 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
804 "expecting rational value", goto error);
806 if (isl_int_eq(aff->v->el[1], v->n) &&
807 isl_int_eq(aff->v->el[0], v->d)) {
808 isl_val_free(v);
809 return aff;
812 aff = isl_aff_cow(aff);
813 if (!aff)
814 goto error;
815 aff->v = isl_vec_cow(aff->v);
816 if (!aff->v)
817 goto error;
819 if (isl_int_eq(aff->v->el[0], v->d)) {
820 isl_int_set(aff->v->el[1], v->n);
821 } else if (isl_int_is_one(v->d)) {
822 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
823 } else {
824 isl_seq_scale(aff->v->el + 1,
825 aff->v->el + 1, v->d, aff->v->size - 1);
826 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
827 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
828 aff->v = isl_vec_normalize(aff->v);
829 if (!aff->v)
830 goto error;
833 isl_val_free(v);
834 return aff;
835 error:
836 isl_aff_free(aff);
837 isl_val_free(v);
838 return NULL;
841 /* Add "v" to the constant term of "aff".
843 * A NaN is unaffected by this operation.
845 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
847 if (isl_int_is_zero(v))
848 return aff;
850 if (!aff)
851 return NULL;
852 if (isl_aff_is_nan(aff))
853 return aff;
854 aff = isl_aff_cow(aff);
855 if (!aff)
856 return NULL;
858 aff->v = isl_vec_cow(aff->v);
859 if (!aff->v)
860 return isl_aff_free(aff);
862 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
864 return aff;
867 /* Add "v" to the constant term of "aff".
869 * A NaN is unaffected by this operation.
871 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
872 __isl_take isl_val *v)
874 if (!aff || !v)
875 goto error;
877 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
878 isl_val_free(v);
879 return aff;
882 if (!isl_val_is_rat(v))
883 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
884 "expecting rational value", goto error);
886 aff = isl_aff_cow(aff);
887 if (!aff)
888 goto error;
890 aff->v = isl_vec_cow(aff->v);
891 if (!aff->v)
892 goto error;
894 if (isl_int_is_one(v->d)) {
895 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
896 } else if (isl_int_eq(aff->v->el[0], v->d)) {
897 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
898 aff->v = isl_vec_normalize(aff->v);
899 if (!aff->v)
900 goto error;
901 } else {
902 isl_seq_scale(aff->v->el + 1,
903 aff->v->el + 1, v->d, aff->v->size - 1);
904 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
905 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
906 aff->v = isl_vec_normalize(aff->v);
907 if (!aff->v)
908 goto error;
911 isl_val_free(v);
912 return aff;
913 error:
914 isl_aff_free(aff);
915 isl_val_free(v);
916 return NULL;
919 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
921 isl_int t;
923 isl_int_init(t);
924 isl_int_set_si(t, v);
925 aff = isl_aff_add_constant(aff, t);
926 isl_int_clear(t);
928 return aff;
931 /* Add "v" to the numerator of the constant term of "aff".
933 * A NaN is unaffected by this operation.
935 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
937 if (isl_int_is_zero(v))
938 return aff;
940 if (!aff)
941 return NULL;
942 if (isl_aff_is_nan(aff))
943 return aff;
944 aff = isl_aff_cow(aff);
945 if (!aff)
946 return NULL;
948 aff->v = isl_vec_cow(aff->v);
949 if (!aff->v)
950 return isl_aff_free(aff);
952 isl_int_add(aff->v->el[1], aff->v->el[1], v);
954 return aff;
957 /* Add "v" to the numerator of the constant term of "aff".
959 * A NaN is unaffected by this operation.
961 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
963 isl_int t;
965 if (v == 0)
966 return aff;
968 isl_int_init(t);
969 isl_int_set_si(t, v);
970 aff = isl_aff_add_constant_num(aff, t);
971 isl_int_clear(t);
973 return aff;
976 /* Replace the numerator of the constant term of "aff" by "v".
978 * A NaN is unaffected by this operation.
980 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
982 if (!aff)
983 return NULL;
984 if (isl_aff_is_nan(aff))
985 return aff;
986 aff = isl_aff_cow(aff);
987 if (!aff)
988 return NULL;
990 aff->v = isl_vec_cow(aff->v);
991 if (!aff->v)
992 return isl_aff_free(aff);
994 isl_int_set_si(aff->v->el[1], v);
996 return aff;
999 /* Replace the numerator of the coefficient of the variable of type "type"
1000 * at position "pos" of "aff" by "v".
1002 * A NaN is unaffected by this operation.
1004 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1005 enum isl_dim_type type, int pos, isl_int v)
1007 if (!aff)
1008 return NULL;
1010 if (type == isl_dim_out)
1011 isl_die(aff->v->ctx, isl_error_invalid,
1012 "output/set dimension does not have a coefficient",
1013 return isl_aff_free(aff));
1014 if (type == isl_dim_in)
1015 type = isl_dim_set;
1017 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1018 return isl_aff_free(aff);
1020 if (isl_aff_is_nan(aff))
1021 return aff;
1022 aff = isl_aff_cow(aff);
1023 if (!aff)
1024 return NULL;
1026 aff->v = isl_vec_cow(aff->v);
1027 if (!aff->v)
1028 return isl_aff_free(aff);
1030 pos += isl_local_space_offset(aff->ls, type);
1031 isl_int_set(aff->v->el[1 + pos], v);
1033 return aff;
1036 /* Replace the numerator of the coefficient of the variable of type "type"
1037 * at position "pos" of "aff" by "v".
1039 * A NaN is unaffected by this operation.
1041 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1042 enum isl_dim_type type, int pos, int v)
1044 if (!aff)
1045 return NULL;
1047 if (type == isl_dim_out)
1048 isl_die(aff->v->ctx, isl_error_invalid,
1049 "output/set dimension does not have a coefficient",
1050 return isl_aff_free(aff));
1051 if (type == isl_dim_in)
1052 type = isl_dim_set;
1054 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1055 return isl_aff_free(aff);
1057 if (isl_aff_is_nan(aff))
1058 return aff;
1059 pos += isl_local_space_offset(aff->ls, type);
1060 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1061 return aff;
1063 aff = isl_aff_cow(aff);
1064 if (!aff)
1065 return NULL;
1067 aff->v = isl_vec_cow(aff->v);
1068 if (!aff->v)
1069 return isl_aff_free(aff);
1071 isl_int_set_si(aff->v->el[1 + pos], v);
1073 return aff;
1076 /* Replace the coefficient of the variable of type "type" at position "pos"
1077 * of "aff" by "v".
1079 * A NaN is unaffected by this operation.
1081 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1082 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1084 if (!aff || !v)
1085 goto error;
1087 if (type == isl_dim_out)
1088 isl_die(aff->v->ctx, isl_error_invalid,
1089 "output/set dimension does not have a coefficient",
1090 goto error);
1091 if (type == isl_dim_in)
1092 type = isl_dim_set;
1094 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1095 return isl_aff_free(aff);
1097 if (isl_aff_is_nan(aff)) {
1098 isl_val_free(v);
1099 return aff;
1101 if (!isl_val_is_rat(v))
1102 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1103 "expecting rational value", goto error);
1105 pos += isl_local_space_offset(aff->ls, type);
1106 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1107 isl_int_eq(aff->v->el[0], v->d)) {
1108 isl_val_free(v);
1109 return aff;
1112 aff = isl_aff_cow(aff);
1113 if (!aff)
1114 goto error;
1115 aff->v = isl_vec_cow(aff->v);
1116 if (!aff->v)
1117 goto error;
1119 if (isl_int_eq(aff->v->el[0], v->d)) {
1120 isl_int_set(aff->v->el[1 + pos], v->n);
1121 } else if (isl_int_is_one(v->d)) {
1122 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1123 } else {
1124 isl_seq_scale(aff->v->el + 1,
1125 aff->v->el + 1, v->d, aff->v->size - 1);
1126 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1127 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1128 aff->v = isl_vec_normalize(aff->v);
1129 if (!aff->v)
1130 goto error;
1133 isl_val_free(v);
1134 return aff;
1135 error:
1136 isl_aff_free(aff);
1137 isl_val_free(v);
1138 return NULL;
1141 /* Add "v" to the coefficient of the variable of type "type"
1142 * at position "pos" of "aff".
1144 * A NaN is unaffected by this operation.
1146 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1147 enum isl_dim_type type, int pos, isl_int v)
1149 if (!aff)
1150 return NULL;
1152 if (type == isl_dim_out)
1153 isl_die(aff->v->ctx, isl_error_invalid,
1154 "output/set dimension does not have a coefficient",
1155 return isl_aff_free(aff));
1156 if (type == isl_dim_in)
1157 type = isl_dim_set;
1159 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1160 return isl_aff_free(aff);
1162 if (isl_aff_is_nan(aff))
1163 return aff;
1164 aff = isl_aff_cow(aff);
1165 if (!aff)
1166 return NULL;
1168 aff->v = isl_vec_cow(aff->v);
1169 if (!aff->v)
1170 return isl_aff_free(aff);
1172 pos += isl_local_space_offset(aff->ls, type);
1173 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1175 return aff;
1178 /* Add "v" to the coefficient of the variable of type "type"
1179 * at position "pos" of "aff".
1181 * A NaN is unaffected by this operation.
1183 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1184 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1186 if (!aff || !v)
1187 goto error;
1189 if (isl_val_is_zero(v)) {
1190 isl_val_free(v);
1191 return aff;
1194 if (type == isl_dim_out)
1195 isl_die(aff->v->ctx, isl_error_invalid,
1196 "output/set dimension does not have a coefficient",
1197 goto error);
1198 if (type == isl_dim_in)
1199 type = isl_dim_set;
1201 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1202 goto error;
1204 if (isl_aff_is_nan(aff)) {
1205 isl_val_free(v);
1206 return aff;
1208 if (!isl_val_is_rat(v))
1209 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1210 "expecting rational value", goto error);
1212 aff = isl_aff_cow(aff);
1213 if (!aff)
1214 goto error;
1216 aff->v = isl_vec_cow(aff->v);
1217 if (!aff->v)
1218 goto error;
1220 pos += isl_local_space_offset(aff->ls, type);
1221 if (isl_int_is_one(v->d)) {
1222 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1223 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1224 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1225 aff->v = isl_vec_normalize(aff->v);
1226 if (!aff->v)
1227 goto error;
1228 } else {
1229 isl_seq_scale(aff->v->el + 1,
1230 aff->v->el + 1, v->d, aff->v->size - 1);
1231 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1232 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1233 aff->v = isl_vec_normalize(aff->v);
1234 if (!aff->v)
1235 goto error;
1238 isl_val_free(v);
1239 return aff;
1240 error:
1241 isl_aff_free(aff);
1242 isl_val_free(v);
1243 return NULL;
1246 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1247 enum isl_dim_type type, int pos, int v)
1249 isl_int t;
1251 isl_int_init(t);
1252 isl_int_set_si(t, v);
1253 aff = isl_aff_add_coefficient(aff, type, pos, t);
1254 isl_int_clear(t);
1256 return aff;
1259 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1261 if (!aff)
1262 return NULL;
1264 return isl_local_space_get_div(aff->ls, pos);
1267 /* Return the negation of "aff".
1269 * As a special case, -NaN = NaN.
1271 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1273 if (!aff)
1274 return NULL;
1275 if (isl_aff_is_nan(aff))
1276 return aff;
1277 aff = isl_aff_cow(aff);
1278 if (!aff)
1279 return NULL;
1280 aff->v = isl_vec_cow(aff->v);
1281 if (!aff->v)
1282 return isl_aff_free(aff);
1284 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1286 return aff;
1289 /* Remove divs from the local space that do not appear in the affine
1290 * expression.
1291 * We currently only remove divs at the end.
1292 * Some intermediate divs may also not appear directly in the affine
1293 * expression, but we would also need to check that no other divs are
1294 * defined in terms of them.
1296 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1298 int pos;
1299 int off;
1300 isl_size n;
1302 if (!aff)
1303 return NULL;
1305 n = isl_local_space_dim(aff->ls, isl_dim_div);
1306 if (n < 0)
1307 return isl_aff_free(aff);
1308 off = isl_local_space_offset(aff->ls, isl_dim_div);
1310 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1311 if (pos == n)
1312 return aff;
1314 aff = isl_aff_cow(aff);
1315 if (!aff)
1316 return NULL;
1318 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1319 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1320 if (!aff->ls || !aff->v)
1321 return isl_aff_free(aff);
1323 return aff;
1326 /* Look for any divs in the aff->ls with a denominator equal to one
1327 * and plug them into the affine expression and any subsequent divs
1328 * that may reference the div.
1330 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1332 int i;
1333 isl_size n;
1334 int len;
1335 isl_int v;
1336 isl_vec *vec;
1337 isl_local_space *ls;
1338 unsigned pos;
1340 if (!aff)
1341 return NULL;
1343 n = isl_local_space_dim(aff->ls, isl_dim_div);
1344 if (n < 0)
1345 return isl_aff_free(aff);
1346 len = aff->v->size;
1347 for (i = 0; i < n; ++i) {
1348 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1349 continue;
1350 ls = isl_local_space_copy(aff->ls);
1351 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1352 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1353 vec = isl_vec_copy(aff->v);
1354 vec = isl_vec_cow(vec);
1355 if (!ls || !vec)
1356 goto error;
1358 isl_int_init(v);
1360 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1361 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1362 len, len, v);
1364 isl_int_clear(v);
1366 isl_vec_free(aff->v);
1367 aff->v = vec;
1368 isl_local_space_free(aff->ls);
1369 aff->ls = ls;
1372 return aff;
1373 error:
1374 isl_vec_free(vec);
1375 isl_local_space_free(ls);
1376 return isl_aff_free(aff);
1379 /* Look for any divs j that appear with a unit coefficient inside
1380 * the definitions of other divs i and plug them into the definitions
1381 * of the divs i.
1383 * In particular, an expression of the form
1385 * floor((f(..) + floor(g(..)/n))/m)
1387 * is simplified to
1389 * floor((n * f(..) + g(..))/(n * m))
1391 * This simplification is correct because we can move the expression
1392 * f(..) into the inner floor in the original expression to obtain
1394 * floor(floor((n * f(..) + g(..))/n)/m)
1396 * from which we can derive the simplified expression.
1398 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1400 int i, j;
1401 isl_size n;
1402 int off;
1404 if (!aff)
1405 return NULL;
1407 n = isl_local_space_dim(aff->ls, isl_dim_div);
1408 if (n < 0)
1409 return isl_aff_free(aff);
1410 off = isl_local_space_offset(aff->ls, isl_dim_div);
1411 for (i = 1; i < n; ++i) {
1412 for (j = 0; j < i; ++j) {
1413 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1414 continue;
1415 aff->ls = isl_local_space_substitute_seq(aff->ls,
1416 isl_dim_div, j, aff->ls->div->row[j],
1417 aff->v->size, i, 1);
1418 if (!aff->ls)
1419 return isl_aff_free(aff);
1423 return aff;
1426 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1428 * Even though this function is only called on isl_affs with a single
1429 * reference, we are careful to only change aff->v and aff->ls together.
1431 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1433 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1434 isl_local_space *ls;
1435 isl_vec *v;
1437 ls = isl_local_space_copy(aff->ls);
1438 ls = isl_local_space_swap_div(ls, a, b);
1439 v = isl_vec_copy(aff->v);
1440 v = isl_vec_cow(v);
1441 if (!ls || !v)
1442 goto error;
1444 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1445 isl_vec_free(aff->v);
1446 aff->v = v;
1447 isl_local_space_free(aff->ls);
1448 aff->ls = ls;
1450 return aff;
1451 error:
1452 isl_vec_free(v);
1453 isl_local_space_free(ls);
1454 return isl_aff_free(aff);
1457 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1459 * We currently do not actually remove div "b", but simply add its
1460 * coefficient to that of "a" and then zero it out.
1462 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1464 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1466 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1467 return aff;
1469 aff->v = isl_vec_cow(aff->v);
1470 if (!aff->v)
1471 return isl_aff_free(aff);
1473 isl_int_add(aff->v->el[1 + off + a],
1474 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1475 isl_int_set_si(aff->v->el[1 + off + b], 0);
1477 return aff;
1480 /* Sort the divs in the local space of "aff" according to
1481 * the comparison function "cmp_row" in isl_local_space.c,
1482 * combining the coefficients of identical divs.
1484 * Reordering divs does not change the semantics of "aff",
1485 * so there is no need to call isl_aff_cow.
1486 * Moreover, this function is currently only called on isl_affs
1487 * with a single reference.
1489 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1491 isl_size n;
1492 int i, j;
1494 n = isl_aff_dim(aff, isl_dim_div);
1495 if (n < 0)
1496 return isl_aff_free(aff);
1497 for (i = 1; i < n; ++i) {
1498 for (j = i - 1; j >= 0; --j) {
1499 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1500 if (cmp < 0)
1501 break;
1502 if (cmp == 0)
1503 aff = merge_divs(aff, j, j + 1);
1504 else
1505 aff = swap_div(aff, j, j + 1);
1506 if (!aff)
1507 return NULL;
1511 return aff;
1514 /* Normalize the representation of "aff".
1516 * This function should only be called of "new" isl_affs, i.e.,
1517 * with only a single reference. We therefore do not need to
1518 * worry about affecting other instances.
1520 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1522 if (!aff)
1523 return NULL;
1524 aff->v = isl_vec_normalize(aff->v);
1525 if (!aff->v)
1526 return isl_aff_free(aff);
1527 aff = plug_in_integral_divs(aff);
1528 aff = plug_in_unit_divs(aff);
1529 aff = sort_divs(aff);
1530 aff = isl_aff_remove_unused_divs(aff);
1531 return aff;
1534 /* Given f, return floor(f).
1535 * If f is an integer expression, then just return f.
1536 * If f is a constant, then return the constant floor(f).
1537 * Otherwise, if f = g/m, write g = q m + r,
1538 * create a new div d = [r/m] and return the expression q + d.
1539 * The coefficients in r are taken to lie between -m/2 and m/2.
1541 * reduce_div_coefficients performs the same normalization.
1543 * As a special case, floor(NaN) = NaN.
1545 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1547 int i;
1548 int size;
1549 isl_ctx *ctx;
1550 isl_vec *div;
1552 if (!aff)
1553 return NULL;
1555 if (isl_aff_is_nan(aff))
1556 return aff;
1557 if (isl_int_is_one(aff->v->el[0]))
1558 return aff;
1560 aff = isl_aff_cow(aff);
1561 if (!aff)
1562 return NULL;
1564 aff->v = isl_vec_cow(aff->v);
1565 if (!aff->v)
1566 return isl_aff_free(aff);
1568 if (isl_aff_is_cst(aff)) {
1569 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1570 isl_int_set_si(aff->v->el[0], 1);
1571 return aff;
1574 div = isl_vec_copy(aff->v);
1575 div = isl_vec_cow(div);
1576 if (!div)
1577 return isl_aff_free(aff);
1579 ctx = isl_aff_get_ctx(aff);
1580 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1581 for (i = 1; i < aff->v->size; ++i) {
1582 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1583 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1584 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1585 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1586 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1590 aff->ls = isl_local_space_add_div(aff->ls, div);
1591 if (!aff->ls)
1592 return isl_aff_free(aff);
1594 size = aff->v->size;
1595 aff->v = isl_vec_extend(aff->v, size + 1);
1596 if (!aff->v)
1597 return isl_aff_free(aff);
1598 isl_int_set_si(aff->v->el[0], 1);
1599 isl_int_set_si(aff->v->el[size], 1);
1601 aff = isl_aff_normalize(aff);
1603 return aff;
1606 /* Compute
1608 * aff mod m = aff - m * floor(aff/m)
1610 * with m an integer value.
1612 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1613 __isl_take isl_val *m)
1615 isl_aff *res;
1617 if (!aff || !m)
1618 goto error;
1620 if (!isl_val_is_int(m))
1621 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1622 "expecting integer modulo", goto error);
1624 res = isl_aff_copy(aff);
1625 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1626 aff = isl_aff_floor(aff);
1627 aff = isl_aff_scale_val(aff, m);
1628 res = isl_aff_sub(res, aff);
1630 return res;
1631 error:
1632 isl_aff_free(aff);
1633 isl_val_free(m);
1634 return NULL;
1637 /* Compute
1639 * pwaff mod m = pwaff - m * floor(pwaff/m)
1641 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1643 isl_pw_aff *res;
1645 res = isl_pw_aff_copy(pwaff);
1646 pwaff = isl_pw_aff_scale_down(pwaff, m);
1647 pwaff = isl_pw_aff_floor(pwaff);
1648 pwaff = isl_pw_aff_scale(pwaff, m);
1649 res = isl_pw_aff_sub(res, pwaff);
1651 return res;
1654 /* Compute
1656 * pa mod m = pa - m * floor(pa/m)
1658 * with m an integer value.
1660 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1661 __isl_take isl_val *m)
1663 if (!pa || !m)
1664 goto error;
1665 if (!isl_val_is_int(m))
1666 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1667 "expecting integer modulo", goto error);
1668 pa = isl_pw_aff_mod(pa, m->n);
1669 isl_val_free(m);
1670 return pa;
1671 error:
1672 isl_pw_aff_free(pa);
1673 isl_val_free(m);
1674 return NULL;
1677 /* Given f, return ceil(f).
1678 * If f is an integer expression, then just return f.
1679 * Otherwise, let f be the expression
1681 * e/m
1683 * then return
1685 * floor((e + m - 1)/m)
1687 * As a special case, ceil(NaN) = NaN.
1689 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1691 if (!aff)
1692 return NULL;
1694 if (isl_aff_is_nan(aff))
1695 return aff;
1696 if (isl_int_is_one(aff->v->el[0]))
1697 return aff;
1699 aff = isl_aff_cow(aff);
1700 if (!aff)
1701 return NULL;
1702 aff->v = isl_vec_cow(aff->v);
1703 if (!aff->v)
1704 return isl_aff_free(aff);
1706 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1707 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1708 aff = isl_aff_floor(aff);
1710 return aff;
1713 /* Apply the expansion computed by isl_merge_divs.
1714 * The expansion itself is given by "exp" while the resulting
1715 * list of divs is given by "div".
1717 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1718 __isl_take isl_mat *div, int *exp)
1720 isl_size old_n_div;
1721 isl_size new_n_div;
1722 int offset;
1724 aff = isl_aff_cow(aff);
1725 if (!aff || !div)
1726 goto error;
1728 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1729 new_n_div = isl_mat_rows(div);
1730 if (old_n_div < 0 || new_n_div < 0)
1731 goto error;
1732 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1734 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1735 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1736 if (!aff->v || !aff->ls)
1737 return isl_aff_free(aff);
1738 return aff;
1739 error:
1740 isl_aff_free(aff);
1741 isl_mat_free(div);
1742 return NULL;
1745 /* Add two affine expressions that live in the same local space.
1747 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1748 __isl_take isl_aff *aff2)
1750 isl_int gcd, f;
1752 aff1 = isl_aff_cow(aff1);
1753 if (!aff1 || !aff2)
1754 goto error;
1756 aff1->v = isl_vec_cow(aff1->v);
1757 if (!aff1->v)
1758 goto error;
1760 isl_int_init(gcd);
1761 isl_int_init(f);
1762 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1763 isl_int_divexact(f, aff2->v->el[0], gcd);
1764 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1765 isl_int_divexact(f, aff1->v->el[0], gcd);
1766 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1767 isl_int_divexact(f, aff2->v->el[0], gcd);
1768 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1769 isl_int_clear(f);
1770 isl_int_clear(gcd);
1772 isl_aff_free(aff2);
1773 return aff1;
1774 error:
1775 isl_aff_free(aff1);
1776 isl_aff_free(aff2);
1777 return NULL;
1780 /* Return the sum of "aff1" and "aff2".
1782 * If either of the two is NaN, then the result is NaN.
1784 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1785 __isl_take isl_aff *aff2)
1787 isl_ctx *ctx;
1788 int *exp1 = NULL;
1789 int *exp2 = NULL;
1790 isl_mat *div;
1791 isl_size n_div1, n_div2;
1793 if (!aff1 || !aff2)
1794 goto error;
1796 ctx = isl_aff_get_ctx(aff1);
1797 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1798 isl_die(ctx, isl_error_invalid,
1799 "spaces don't match", goto error);
1801 if (isl_aff_is_nan(aff1)) {
1802 isl_aff_free(aff2);
1803 return aff1;
1805 if (isl_aff_is_nan(aff2)) {
1806 isl_aff_free(aff1);
1807 return aff2;
1810 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1811 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1812 if (n_div1 < 0 || n_div2 < 0)
1813 goto error;
1814 if (n_div1 == 0 && n_div2 == 0)
1815 return add_expanded(aff1, aff2);
1817 exp1 = isl_alloc_array(ctx, int, n_div1);
1818 exp2 = isl_alloc_array(ctx, int, n_div2);
1819 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1820 goto error;
1822 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1823 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1824 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1825 free(exp1);
1826 free(exp2);
1828 return add_expanded(aff1, aff2);
1829 error:
1830 free(exp1);
1831 free(exp2);
1832 isl_aff_free(aff1);
1833 isl_aff_free(aff2);
1834 return NULL;
1837 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1838 __isl_take isl_aff *aff2)
1840 return isl_aff_add(aff1, isl_aff_neg(aff2));
1843 /* Return the result of scaling "aff" by a factor of "f".
1845 * As a special case, f * NaN = NaN.
1847 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1849 isl_int gcd;
1851 if (!aff)
1852 return NULL;
1853 if (isl_aff_is_nan(aff))
1854 return aff;
1856 if (isl_int_is_one(f))
1857 return aff;
1859 aff = isl_aff_cow(aff);
1860 if (!aff)
1861 return NULL;
1862 aff->v = isl_vec_cow(aff->v);
1863 if (!aff->v)
1864 return isl_aff_free(aff);
1866 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1867 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1868 return aff;
1871 isl_int_init(gcd);
1872 isl_int_gcd(gcd, aff->v->el[0], f);
1873 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1874 isl_int_divexact(gcd, f, gcd);
1875 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1876 isl_int_clear(gcd);
1878 return aff;
1881 /* Multiple "aff" by "v".
1883 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1884 __isl_take isl_val *v)
1886 if (!aff || !v)
1887 goto error;
1889 if (isl_val_is_one(v)) {
1890 isl_val_free(v);
1891 return aff;
1894 if (!isl_val_is_rat(v))
1895 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1896 "expecting rational factor", goto error);
1898 aff = isl_aff_scale(aff, v->n);
1899 aff = isl_aff_scale_down(aff, v->d);
1901 isl_val_free(v);
1902 return aff;
1903 error:
1904 isl_aff_free(aff);
1905 isl_val_free(v);
1906 return NULL;
1909 /* Return the result of scaling "aff" down by a factor of "f".
1911 * As a special case, NaN/f = NaN.
1913 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1915 isl_int gcd;
1917 if (!aff)
1918 return NULL;
1919 if (isl_aff_is_nan(aff))
1920 return aff;
1922 if (isl_int_is_one(f))
1923 return aff;
1925 aff = isl_aff_cow(aff);
1926 if (!aff)
1927 return NULL;
1929 if (isl_int_is_zero(f))
1930 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1931 "cannot scale down by zero", return isl_aff_free(aff));
1933 aff->v = isl_vec_cow(aff->v);
1934 if (!aff->v)
1935 return isl_aff_free(aff);
1937 isl_int_init(gcd);
1938 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1939 isl_int_gcd(gcd, gcd, f);
1940 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1941 isl_int_divexact(gcd, f, gcd);
1942 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1943 isl_int_clear(gcd);
1945 return aff;
1948 /* Divide "aff" by "v".
1950 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1951 __isl_take isl_val *v)
1953 if (!aff || !v)
1954 goto error;
1956 if (isl_val_is_one(v)) {
1957 isl_val_free(v);
1958 return aff;
1961 if (!isl_val_is_rat(v))
1962 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1963 "expecting rational factor", goto error);
1964 if (!isl_val_is_pos(v))
1965 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1966 "factor needs to be positive", goto error);
1968 aff = isl_aff_scale(aff, v->d);
1969 aff = isl_aff_scale_down(aff, v->n);
1971 isl_val_free(v);
1972 return aff;
1973 error:
1974 isl_aff_free(aff);
1975 isl_val_free(v);
1976 return NULL;
1979 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1981 isl_int v;
1983 if (f == 1)
1984 return aff;
1986 isl_int_init(v);
1987 isl_int_set_ui(v, f);
1988 aff = isl_aff_scale_down(aff, v);
1989 isl_int_clear(v);
1991 return aff;
1994 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1995 enum isl_dim_type type, unsigned pos, const char *s)
1997 aff = isl_aff_cow(aff);
1998 if (!aff)
1999 return NULL;
2000 if (type == isl_dim_out)
2001 isl_die(aff->v->ctx, isl_error_invalid,
2002 "cannot set name of output/set dimension",
2003 return isl_aff_free(aff));
2004 if (type == isl_dim_in)
2005 type = isl_dim_set;
2006 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2007 if (!aff->ls)
2008 return isl_aff_free(aff);
2010 return aff;
2013 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2014 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2016 aff = isl_aff_cow(aff);
2017 if (!aff)
2018 goto error;
2019 if (type == isl_dim_out)
2020 isl_die(aff->v->ctx, isl_error_invalid,
2021 "cannot set name of output/set dimension",
2022 goto error);
2023 if (type == isl_dim_in)
2024 type = isl_dim_set;
2025 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2026 if (!aff->ls)
2027 return isl_aff_free(aff);
2029 return aff;
2030 error:
2031 isl_id_free(id);
2032 isl_aff_free(aff);
2033 return NULL;
2036 /* Replace the identifier of the input tuple of "aff" by "id".
2037 * type is currently required to be equal to isl_dim_in
2039 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2040 enum isl_dim_type type, __isl_take isl_id *id)
2042 aff = isl_aff_cow(aff);
2043 if (!aff)
2044 goto error;
2045 if (type != isl_dim_in)
2046 isl_die(aff->v->ctx, isl_error_invalid,
2047 "cannot only set id of input tuple", goto error);
2048 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2049 if (!aff->ls)
2050 return isl_aff_free(aff);
2052 return aff;
2053 error:
2054 isl_id_free(id);
2055 isl_aff_free(aff);
2056 return NULL;
2059 /* Exploit the equalities in "eq" to simplify the affine expression
2060 * and the expressions of the integer divisions in the local space.
2061 * The integer divisions in this local space are assumed to appear
2062 * as regular dimensions in "eq".
2064 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2065 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2067 int i, j;
2068 unsigned o_div;
2069 unsigned n_div;
2071 if (!eq)
2072 goto error;
2073 if (eq->n_eq == 0) {
2074 isl_basic_set_free(eq);
2075 return aff;
2078 aff = isl_aff_cow(aff);
2079 if (!aff)
2080 goto error;
2082 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2083 isl_basic_set_copy(eq));
2084 aff->v = isl_vec_cow(aff->v);
2085 if (!aff->ls || !aff->v)
2086 goto error;
2088 o_div = isl_basic_set_offset(eq, isl_dim_div);
2089 n_div = eq->n_div;
2090 for (i = 0; i < eq->n_eq; ++i) {
2091 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2092 if (j < 0 || j == 0 || j >= o_div)
2093 continue;
2095 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2096 &aff->v->el[0]);
2099 isl_basic_set_free(eq);
2100 aff = isl_aff_normalize(aff);
2101 return aff;
2102 error:
2103 isl_basic_set_free(eq);
2104 isl_aff_free(aff);
2105 return NULL;
2108 /* Exploit the equalities in "eq" to simplify the affine expression
2109 * and the expressions of the integer divisions in the local space.
2111 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2112 __isl_take isl_basic_set *eq)
2114 isl_size n_div;
2116 if (!aff || !eq)
2117 goto error;
2118 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2119 if (n_div < 0)
2120 goto error;
2121 if (n_div > 0)
2122 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2123 return isl_aff_substitute_equalities_lifted(aff, eq);
2124 error:
2125 isl_basic_set_free(eq);
2126 isl_aff_free(aff);
2127 return NULL;
2130 /* Look for equalities among the variables shared by context and aff
2131 * and the integer divisions of aff, if any.
2132 * The equalities are then used to eliminate coefficients and/or integer
2133 * divisions from aff.
2135 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2136 __isl_take isl_set *context)
2138 isl_local_space *ls;
2139 isl_basic_set *hull;
2141 ls = isl_aff_get_domain_local_space(aff);
2142 context = isl_local_space_lift_set(ls, context);
2144 hull = isl_set_affine_hull(context);
2145 return isl_aff_substitute_equalities_lifted(aff, hull);
2148 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2149 __isl_take isl_set *context)
2151 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2152 dom_context = isl_set_intersect_params(dom_context, context);
2153 return isl_aff_gist(aff, dom_context);
2156 /* Return a basic set containing those elements in the space
2157 * of aff where it is positive. "rational" should not be set.
2159 * If "aff" is NaN, then it is not positive.
2161 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2162 int rational, void *user)
2164 isl_constraint *ineq;
2165 isl_basic_set *bset;
2166 isl_val *c;
2168 if (!aff)
2169 return NULL;
2170 if (isl_aff_is_nan(aff)) {
2171 isl_space *space = isl_aff_get_domain_space(aff);
2172 isl_aff_free(aff);
2173 return isl_basic_set_empty(space);
2175 if (rational)
2176 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2177 "rational sets not supported", goto error);
2179 ineq = isl_inequality_from_aff(aff);
2180 c = isl_constraint_get_constant_val(ineq);
2181 c = isl_val_sub_ui(c, 1);
2182 ineq = isl_constraint_set_constant_val(ineq, c);
2184 bset = isl_basic_set_from_constraint(ineq);
2185 bset = isl_basic_set_simplify(bset);
2186 return bset;
2187 error:
2188 isl_aff_free(aff);
2189 return NULL;
2192 /* Return a basic set containing those elements in the space
2193 * of aff where it is non-negative.
2194 * If "rational" is set, then return a rational basic set.
2196 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2198 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2199 __isl_take isl_aff *aff, int rational, void *user)
2201 isl_constraint *ineq;
2202 isl_basic_set *bset;
2204 if (!aff)
2205 return NULL;
2206 if (isl_aff_is_nan(aff)) {
2207 isl_space *space = isl_aff_get_domain_space(aff);
2208 isl_aff_free(aff);
2209 return isl_basic_set_empty(space);
2212 ineq = isl_inequality_from_aff(aff);
2214 bset = isl_basic_set_from_constraint(ineq);
2215 if (rational)
2216 bset = isl_basic_set_set_rational(bset);
2217 bset = isl_basic_set_simplify(bset);
2218 return bset;
2221 /* Return a basic set containing those elements in the space
2222 * of aff where it is non-negative.
2224 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2226 return aff_nonneg_basic_set(aff, 0, NULL);
2229 /* Return a basic set containing those elements in the domain space
2230 * of "aff" where it is positive.
2232 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2234 aff = isl_aff_add_constant_num_si(aff, -1);
2235 return isl_aff_nonneg_basic_set(aff);
2238 /* Return a basic set containing those elements in the domain space
2239 * of aff where it is negative.
2241 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2243 aff = isl_aff_neg(aff);
2244 return isl_aff_pos_basic_set(aff);
2247 /* Return a basic set containing those elements in the space
2248 * of aff where it is zero.
2249 * If "rational" is set, then return a rational basic set.
2251 * If "aff" is NaN, then it is not zero.
2253 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2254 int rational, void *user)
2256 isl_constraint *ineq;
2257 isl_basic_set *bset;
2259 if (!aff)
2260 return NULL;
2261 if (isl_aff_is_nan(aff)) {
2262 isl_space *space = isl_aff_get_domain_space(aff);
2263 isl_aff_free(aff);
2264 return isl_basic_set_empty(space);
2267 ineq = isl_equality_from_aff(aff);
2269 bset = isl_basic_set_from_constraint(ineq);
2270 if (rational)
2271 bset = isl_basic_set_set_rational(bset);
2272 bset = isl_basic_set_simplify(bset);
2273 return bset;
2276 /* Return a basic set containing those elements in the space
2277 * of aff where it is zero.
2279 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2281 return aff_zero_basic_set(aff, 0, NULL);
2284 /* Return a basic set containing those elements in the shared space
2285 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2287 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2288 __isl_take isl_aff *aff2)
2290 aff1 = isl_aff_sub(aff1, aff2);
2292 return isl_aff_nonneg_basic_set(aff1);
2295 /* Return a basic set containing those elements in the shared domain space
2296 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2298 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2299 __isl_take isl_aff *aff2)
2301 aff1 = isl_aff_sub(aff1, aff2);
2303 return isl_aff_pos_basic_set(aff1);
2306 /* Return a set containing those elements in the shared space
2307 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2309 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2310 __isl_take isl_aff *aff2)
2312 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2315 /* Return a set containing those elements in the shared domain space
2316 * of aff1 and aff2 where aff1 is greater than aff2.
2318 * If either of the two inputs is NaN, then the result is empty,
2319 * as comparisons with NaN always return false.
2321 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2322 __isl_take isl_aff *aff2)
2324 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2327 /* Return a basic set containing those elements in the shared space
2328 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2330 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2331 __isl_take isl_aff *aff2)
2333 return isl_aff_ge_basic_set(aff2, aff1);
2336 /* Return a basic set containing those elements in the shared domain space
2337 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2339 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2340 __isl_take isl_aff *aff2)
2342 return isl_aff_gt_basic_set(aff2, aff1);
2345 /* Return a set containing those elements in the shared space
2346 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2348 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2349 __isl_take isl_aff *aff2)
2351 return isl_aff_ge_set(aff2, aff1);
2354 /* Return a set containing those elements in the shared domain space
2355 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2357 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2358 __isl_take isl_aff *aff2)
2360 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2363 /* Return a basic set containing those elements in the shared space
2364 * of aff1 and aff2 where aff1 and aff2 are equal.
2366 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2367 __isl_take isl_aff *aff2)
2369 aff1 = isl_aff_sub(aff1, aff2);
2371 return isl_aff_zero_basic_set(aff1);
2374 /* Return a set containing those elements in the shared space
2375 * of aff1 and aff2 where aff1 and aff2 are equal.
2377 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2378 __isl_take isl_aff *aff2)
2380 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2383 /* Return a set containing those elements in the shared domain space
2384 * of aff1 and aff2 where aff1 and aff2 are not equal.
2386 * If either of the two inputs is NaN, then the result is empty,
2387 * as comparisons with NaN always return false.
2389 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2390 __isl_take isl_aff *aff2)
2392 isl_set *set_lt, *set_gt;
2394 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2395 isl_aff_copy(aff2));
2396 set_gt = isl_aff_gt_set(aff1, aff2);
2397 return isl_set_union_disjoint(set_lt, set_gt);
2400 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2401 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2403 aff1 = isl_aff_add(aff1, aff2);
2404 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2405 return aff1;
2408 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2410 if (!aff)
2411 return -1;
2413 return 0;
2416 #undef TYPE
2417 #define TYPE isl_aff
2418 static
2419 #include "check_type_range_templ.c"
2421 /* Check whether the given affine expression has non-zero coefficient
2422 * for any dimension in the given range or if any of these dimensions
2423 * appear with non-zero coefficients in any of the integer divisions
2424 * involved in the affine expression.
2426 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2427 enum isl_dim_type type, unsigned first, unsigned n)
2429 int i;
2430 int *active = NULL;
2431 isl_bool involves = isl_bool_false;
2433 if (!aff)
2434 return isl_bool_error;
2435 if (n == 0)
2436 return isl_bool_false;
2437 if (isl_aff_check_range(aff, type, first, n) < 0)
2438 return isl_bool_error;
2440 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2441 if (!active)
2442 goto error;
2444 first += isl_local_space_offset(aff->ls, type) - 1;
2445 for (i = 0; i < n; ++i)
2446 if (active[first + i]) {
2447 involves = isl_bool_true;
2448 break;
2451 free(active);
2453 return involves;
2454 error:
2455 free(active);
2456 return isl_bool_error;
2459 /* Does "aff" involve any local variables, i.e., integer divisions?
2461 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2463 isl_size n;
2465 n = isl_aff_dim(aff, isl_dim_div);
2466 if (n < 0)
2467 return isl_bool_error;
2468 return isl_aff_involves_dims(aff, isl_dim_div, 0, n);
2471 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2472 enum isl_dim_type type, unsigned first, unsigned n)
2474 isl_ctx *ctx;
2476 if (!aff)
2477 return NULL;
2478 if (type == isl_dim_out)
2479 isl_die(aff->v->ctx, isl_error_invalid,
2480 "cannot drop output/set dimension",
2481 return isl_aff_free(aff));
2482 if (type == isl_dim_in)
2483 type = isl_dim_set;
2484 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2485 return aff;
2487 ctx = isl_aff_get_ctx(aff);
2488 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2489 return isl_aff_free(aff);
2491 aff = isl_aff_cow(aff);
2492 if (!aff)
2493 return NULL;
2495 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2496 if (!aff->ls)
2497 return isl_aff_free(aff);
2499 first += 1 + isl_local_space_offset(aff->ls, type);
2500 aff->v = isl_vec_drop_els(aff->v, first, n);
2501 if (!aff->v)
2502 return isl_aff_free(aff);
2504 return aff;
2507 /* Is the domain of "aff" a product?
2509 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2511 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2514 #undef TYPE
2515 #define TYPE isl_aff
2516 #include <isl_domain_factor_templ.c>
2518 /* Project the domain of the affine expression onto its parameter space.
2519 * The affine expression may not involve any of the domain dimensions.
2521 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2523 isl_space *space;
2524 isl_size n;
2526 n = isl_aff_dim(aff, isl_dim_in);
2527 if (n < 0)
2528 return isl_aff_free(aff);
2529 aff = isl_aff_drop_domain(aff, 0, n);
2530 space = isl_aff_get_domain_space(aff);
2531 space = isl_space_params(space);
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 (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2568 return isl_aff_free(aff);
2570 aff = isl_aff_cow(aff);
2571 if (!aff)
2572 return NULL;
2574 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2575 if (!aff->ls)
2576 return isl_aff_free(aff);
2578 first += 1 + isl_local_space_offset(aff->ls, type);
2579 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2580 if (!aff->v)
2581 return isl_aff_free(aff);
2583 return aff;
2586 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2587 enum isl_dim_type type, unsigned n)
2589 isl_size pos;
2591 pos = isl_aff_dim(aff, type);
2592 if (pos < 0)
2593 return isl_aff_free(aff);
2595 return isl_aff_insert_dims(aff, type, pos, n);
2598 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2599 enum isl_dim_type type, unsigned n)
2601 isl_size pos;
2603 pos = isl_pw_aff_dim(pwaff, type);
2604 if (pos < 0)
2605 return isl_pw_aff_free(pwaff);
2607 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2610 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2611 * to dimensions of "dst_type" at "dst_pos".
2613 * We only support moving input dimensions to parameters and vice versa.
2615 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2616 enum isl_dim_type dst_type, unsigned dst_pos,
2617 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2619 unsigned g_dst_pos;
2620 unsigned g_src_pos;
2622 if (!aff)
2623 return NULL;
2624 if (n == 0 &&
2625 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2626 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2627 return aff;
2629 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2630 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2631 "cannot move output/set dimension",
2632 return isl_aff_free(aff));
2633 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2634 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2635 "cannot move divs", return isl_aff_free(aff));
2636 if (dst_type == isl_dim_in)
2637 dst_type = isl_dim_set;
2638 if (src_type == isl_dim_in)
2639 src_type = isl_dim_set;
2641 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2642 return isl_aff_free(aff);
2643 if (dst_type == src_type)
2644 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2645 "moving dims within the same type not supported",
2646 return isl_aff_free(aff));
2648 aff = isl_aff_cow(aff);
2649 if (!aff)
2650 return NULL;
2652 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2653 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2654 if (dst_type > src_type)
2655 g_dst_pos -= n;
2657 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2658 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2659 src_type, src_pos, n);
2660 if (!aff->v || !aff->ls)
2661 return isl_aff_free(aff);
2663 aff = sort_divs(aff);
2665 return aff;
2668 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2670 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2671 return isl_pw_aff_alloc(dom, aff);
2674 #define isl_aff_involves_nan isl_aff_is_nan
2676 #undef PW
2677 #define PW isl_pw_aff
2678 #undef EL
2679 #define EL isl_aff
2680 #undef EL_IS_ZERO
2681 #define EL_IS_ZERO is_empty
2682 #undef ZERO
2683 #define ZERO empty
2684 #undef IS_ZERO
2685 #define IS_ZERO is_empty
2686 #undef FIELD
2687 #define FIELD aff
2688 #undef DEFAULT_IS_ZERO
2689 #define DEFAULT_IS_ZERO 0
2691 #define NO_OPT
2692 #define NO_LIFT
2693 #define NO_MORPH
2695 #include <isl_pw_templ.c>
2696 #include <isl_pw_bind_domain_templ.c>
2697 #include <isl_pw_eval.c>
2698 #include <isl_pw_hash.c>
2699 #include <isl_pw_union_opt.c>
2701 #undef BASE
2702 #define BASE pw_aff
2704 #include <isl_union_single.c>
2705 #include <isl_union_neg.c>
2707 static __isl_give isl_set *align_params_pw_pw_set_and(
2708 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2709 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2710 __isl_take isl_pw_aff *pwaff2))
2712 isl_bool equal_params;
2714 if (!pwaff1 || !pwaff2)
2715 goto error;
2716 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2717 if (equal_params < 0)
2718 goto error;
2719 if (equal_params)
2720 return fn(pwaff1, pwaff2);
2721 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2722 isl_pw_aff_check_named_params(pwaff2) < 0)
2723 goto error;
2724 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2725 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2726 return fn(pwaff1, pwaff2);
2727 error:
2728 isl_pw_aff_free(pwaff1);
2729 isl_pw_aff_free(pwaff2);
2730 return NULL;
2733 /* Align the parameters of the to isl_pw_aff arguments and
2734 * then apply a function "fn" on them that returns an isl_map.
2736 static __isl_give isl_map *align_params_pw_pw_map_and(
2737 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2738 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2739 __isl_take isl_pw_aff *pa2))
2741 isl_bool equal_params;
2743 if (!pa1 || !pa2)
2744 goto error;
2745 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2746 if (equal_params < 0)
2747 goto error;
2748 if (equal_params)
2749 return fn(pa1, pa2);
2750 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2751 isl_pw_aff_check_named_params(pa2) < 0)
2752 goto error;
2753 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2754 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2755 return fn(pa1, pa2);
2756 error:
2757 isl_pw_aff_free(pa1);
2758 isl_pw_aff_free(pa2);
2759 return NULL;
2762 /* Compute a piecewise quasi-affine expression with a domain that
2763 * is the union of those of pwaff1 and pwaff2 and such that on each
2764 * cell, the quasi-affine expression is the maximum of those of pwaff1
2765 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2766 * cell, then the associated expression is the defined one.
2768 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2769 __isl_take isl_pw_aff *pwaff2)
2771 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2774 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2775 __isl_take isl_pw_aff *pwaff2)
2777 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2778 &pw_aff_union_max);
2781 /* Compute a piecewise quasi-affine expression with a domain that
2782 * is the union of those of pwaff1 and pwaff2 and such that on each
2783 * cell, the quasi-affine expression is the minimum of those of pwaff1
2784 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2785 * cell, then the associated expression is the defined one.
2787 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2788 __isl_take isl_pw_aff *pwaff2)
2790 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2793 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2794 __isl_take isl_pw_aff *pwaff2)
2796 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2797 &pw_aff_union_min);
2800 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2801 __isl_take isl_pw_aff *pwaff2, int max)
2803 if (max)
2804 return isl_pw_aff_union_max(pwaff1, pwaff2);
2805 else
2806 return isl_pw_aff_union_min(pwaff1, pwaff2);
2809 /* Is the domain of "pa" a product?
2811 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2813 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2816 #undef TYPE
2817 #define TYPE isl_pw_aff
2818 #include <isl_domain_factor_templ.c>
2820 /* Return a set containing those elements in the domain
2821 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2822 * does not satisfy "fn" (if complement is 1).
2824 * The pieces with a NaN never belong to the result since
2825 * NaN does not satisfy any property.
2827 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2828 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2829 void *user),
2830 int complement, void *user)
2832 int i;
2833 isl_set *set;
2835 if (!pwaff)
2836 return NULL;
2838 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2840 for (i = 0; i < pwaff->n; ++i) {
2841 isl_basic_set *bset;
2842 isl_set *set_i, *locus;
2843 isl_bool rational;
2845 if (isl_aff_is_nan(pwaff->p[i].aff))
2846 continue;
2848 rational = isl_set_has_rational(pwaff->p[i].set);
2849 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2850 locus = isl_set_from_basic_set(bset);
2851 set_i = isl_set_copy(pwaff->p[i].set);
2852 if (complement)
2853 set_i = isl_set_subtract(set_i, locus);
2854 else
2855 set_i = isl_set_intersect(set_i, locus);
2856 set = isl_set_union_disjoint(set, set_i);
2859 isl_pw_aff_free(pwaff);
2861 return set;
2864 /* Return a set containing those elements in the domain
2865 * of "pa" where it is positive.
2867 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2869 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2872 /* Return a set containing those elements in the domain
2873 * of pwaff where it is non-negative.
2875 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2877 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2880 /* Return a set containing those elements in the domain
2881 * of pwaff where it is zero.
2883 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2885 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2888 /* Return a set containing those elements in the domain
2889 * of pwaff where it is not zero.
2891 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2893 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2896 /* Bind the affine function "aff" to the parameter "id",
2897 * returning the elements in the domain where the affine expression
2898 * is equal to the parameter.
2900 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2901 __isl_take isl_id *id)
2903 isl_space *space;
2904 isl_aff *aff_id;
2906 space = isl_aff_get_domain_space(aff);
2907 space = isl_space_add_param_id(space, isl_id_copy(id));
2909 aff = isl_aff_align_params(aff, isl_space_copy(space));
2910 aff_id = isl_aff_param_on_domain_space_id(space, id);
2912 return isl_aff_eq_basic_set(aff, aff_id);
2915 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2916 * "rational" should not be set.
2918 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2919 int rational, void *user)
2921 isl_id *id = user;
2923 if (!aff)
2924 return NULL;
2925 if (rational)
2926 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2927 "rational binding not supported", goto error);
2928 return isl_aff_bind_id(aff, isl_id_copy(id));
2929 error:
2930 isl_aff_free(aff);
2931 return NULL;
2934 /* Bind the piecewise affine function "pa" to the parameter "id",
2935 * returning the elements in the domain where the expression
2936 * is equal to the parameter.
2938 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2939 __isl_take isl_id *id)
2941 isl_set *bound;
2943 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2944 isl_id_free(id);
2946 return bound;
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 *space = isl_set_get_space(set1);
2972 isl_aff *aff;
2973 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
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 int pos;
3374 if (!aff)
3375 return isl_bool_error;
3377 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3378 return isl_bool_ok(pos == -1);
3381 /* Check whether pwaff is a piecewise constant.
3383 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3385 int i;
3387 if (!pwaff)
3388 return isl_bool_error;
3390 for (i = 0; i < pwaff->n; ++i) {
3391 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3392 if (is_cst < 0 || !is_cst)
3393 return is_cst;
3396 return isl_bool_true;
3399 /* Are all elements of "mpa" piecewise constants?
3401 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3403 int i;
3405 if (!mpa)
3406 return isl_bool_error;
3408 for (i = 0; i < mpa->n; ++i) {
3409 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3410 if (is_cst < 0 || !is_cst)
3411 return is_cst;
3414 return isl_bool_true;
3417 /* Return the product of "aff1" and "aff2".
3419 * If either of the two is NaN, then the result is NaN.
3421 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3423 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3424 __isl_take isl_aff *aff2)
3426 if (!aff1 || !aff2)
3427 goto error;
3429 if (isl_aff_is_nan(aff1)) {
3430 isl_aff_free(aff2);
3431 return aff1;
3433 if (isl_aff_is_nan(aff2)) {
3434 isl_aff_free(aff1);
3435 return aff2;
3438 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3439 return isl_aff_mul(aff2, aff1);
3441 if (!isl_aff_is_cst(aff2))
3442 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3443 "at least one affine expression should be constant",
3444 goto error);
3446 aff1 = isl_aff_cow(aff1);
3447 if (!aff1 || !aff2)
3448 goto error;
3450 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3451 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3453 isl_aff_free(aff2);
3454 return aff1;
3455 error:
3456 isl_aff_free(aff1);
3457 isl_aff_free(aff2);
3458 return NULL;
3461 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3463 * If either of the two is NaN, then the result is NaN.
3465 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3466 __isl_take isl_aff *aff2)
3468 int is_cst;
3469 int neg;
3471 if (!aff1 || !aff2)
3472 goto error;
3474 if (isl_aff_is_nan(aff1)) {
3475 isl_aff_free(aff2);
3476 return aff1;
3478 if (isl_aff_is_nan(aff2)) {
3479 isl_aff_free(aff1);
3480 return aff2;
3483 is_cst = isl_aff_is_cst(aff2);
3484 if (is_cst < 0)
3485 goto error;
3486 if (!is_cst)
3487 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3488 "second argument should be a constant", goto error);
3490 if (!aff2)
3491 goto error;
3493 neg = isl_int_is_neg(aff2->v->el[1]);
3494 if (neg) {
3495 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3496 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3499 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3500 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3502 if (neg) {
3503 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3504 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3507 isl_aff_free(aff2);
3508 return aff1;
3509 error:
3510 isl_aff_free(aff1);
3511 isl_aff_free(aff2);
3512 return NULL;
3515 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3516 __isl_take isl_pw_aff *pwaff2)
3518 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3521 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3522 __isl_take isl_pw_aff *pwaff2)
3524 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3527 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3528 __isl_take isl_pw_aff *pwaff2)
3530 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3533 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3534 __isl_take isl_pw_aff *pwaff2)
3536 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3539 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3540 __isl_take isl_pw_aff *pwaff2)
3542 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3545 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3546 __isl_take isl_pw_aff *pa2)
3548 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3551 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3553 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3554 __isl_take isl_pw_aff *pa2)
3556 int is_cst;
3558 is_cst = isl_pw_aff_is_cst(pa2);
3559 if (is_cst < 0)
3560 goto error;
3561 if (!is_cst)
3562 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3563 "second argument should be a piecewise constant",
3564 goto error);
3565 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3566 error:
3567 isl_pw_aff_free(pa1);
3568 isl_pw_aff_free(pa2);
3569 return NULL;
3572 /* Compute the quotient of the integer division of "pa1" by "pa2"
3573 * with rounding towards zero.
3574 * "pa2" is assumed to be a piecewise constant.
3576 * In particular, return
3578 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3581 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3582 __isl_take isl_pw_aff *pa2)
3584 int is_cst;
3585 isl_set *cond;
3586 isl_pw_aff *f, *c;
3588 is_cst = isl_pw_aff_is_cst(pa2);
3589 if (is_cst < 0)
3590 goto error;
3591 if (!is_cst)
3592 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3593 "second argument should be a piecewise constant",
3594 goto error);
3596 pa1 = isl_pw_aff_div(pa1, pa2);
3598 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3599 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3600 c = isl_pw_aff_ceil(pa1);
3601 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3602 error:
3603 isl_pw_aff_free(pa1);
3604 isl_pw_aff_free(pa2);
3605 return NULL;
3608 /* Compute the remainder of the integer division of "pa1" by "pa2"
3609 * with rounding towards zero.
3610 * "pa2" is assumed to be a piecewise constant.
3612 * In particular, return
3614 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3617 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3618 __isl_take isl_pw_aff *pa2)
3620 int is_cst;
3621 isl_pw_aff *res;
3623 is_cst = isl_pw_aff_is_cst(pa2);
3624 if (is_cst < 0)
3625 goto error;
3626 if (!is_cst)
3627 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3628 "second argument should be a piecewise constant",
3629 goto error);
3630 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3631 res = isl_pw_aff_mul(pa2, res);
3632 res = isl_pw_aff_sub(pa1, res);
3633 return res;
3634 error:
3635 isl_pw_aff_free(pa1);
3636 isl_pw_aff_free(pa2);
3637 return NULL;
3640 /* Does either of "pa1" or "pa2" involve any NaN2?
3642 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3643 __isl_keep isl_pw_aff *pa2)
3645 isl_bool has_nan;
3647 has_nan = isl_pw_aff_involves_nan(pa1);
3648 if (has_nan < 0 || has_nan)
3649 return has_nan;
3650 return isl_pw_aff_involves_nan(pa2);
3653 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3654 * by a NaN on their shared domain.
3656 * In principle, the result could be refined to only being NaN
3657 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3659 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3660 __isl_take isl_pw_aff *pa2)
3662 isl_local_space *ls;
3663 isl_set *dom;
3664 isl_pw_aff *pa;
3666 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3667 ls = isl_local_space_from_space(isl_set_get_space(dom));
3668 pa = isl_pw_aff_nan_on_domain(ls);
3669 pa = isl_pw_aff_intersect_domain(pa, dom);
3671 return pa;
3674 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3675 __isl_take isl_pw_aff *pwaff2)
3677 isl_set *le;
3678 isl_set *dom;
3680 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3681 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3682 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3683 isl_pw_aff_copy(pwaff2));
3684 dom = isl_set_subtract(dom, isl_set_copy(le));
3685 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3688 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3689 __isl_take isl_pw_aff *pwaff2)
3691 isl_set *ge;
3692 isl_set *dom;
3694 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3695 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3696 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3697 isl_pw_aff_copy(pwaff2));
3698 dom = isl_set_subtract(dom, isl_set_copy(ge));
3699 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3702 /* Return an expression for the minimum (if "max" is not set) or
3703 * the maximum (if "max" is set) of "pa1" and "pa2".
3704 * If either expression involves any NaN, then return a NaN
3705 * on the shared domain as result.
3707 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3708 __isl_take isl_pw_aff *pa2, int max)
3710 isl_bool has_nan;
3712 has_nan = either_involves_nan(pa1, pa2);
3713 if (has_nan < 0)
3714 pa1 = isl_pw_aff_free(pa1);
3715 else if (has_nan)
3716 return replace_by_nan(pa1, pa2);
3718 if (max)
3719 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3720 else
3721 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3724 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3726 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3727 __isl_take isl_pw_aff *pwaff2)
3729 return pw_aff_min_max(pwaff1, pwaff2, 0);
3732 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3734 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3735 __isl_take isl_pw_aff *pwaff2)
3737 return pw_aff_min_max(pwaff1, pwaff2, 1);
3740 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3741 __isl_take isl_pw_aff_list *list,
3742 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3743 __isl_take isl_pw_aff *pwaff2))
3745 int i;
3746 isl_ctx *ctx;
3747 isl_pw_aff *res;
3749 if (!list)
3750 return NULL;
3752 ctx = isl_pw_aff_list_get_ctx(list);
3753 if (list->n < 1)
3754 isl_die(ctx, isl_error_invalid,
3755 "list should contain at least one element", goto error);
3757 res = isl_pw_aff_copy(list->p[0]);
3758 for (i = 1; i < list->n; ++i)
3759 res = fn(res, isl_pw_aff_copy(list->p[i]));
3761 isl_pw_aff_list_free(list);
3762 return res;
3763 error:
3764 isl_pw_aff_list_free(list);
3765 return NULL;
3768 /* Return an isl_pw_aff that maps each element in the intersection of the
3769 * domains of the elements of list to the minimal corresponding affine
3770 * expression.
3772 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3774 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3777 /* Return an isl_pw_aff that maps each element in the intersection of the
3778 * domains of the elements of list to the maximal corresponding affine
3779 * expression.
3781 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3783 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3786 /* Mark the domains of "pwaff" as rational.
3788 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3790 int i;
3792 pwaff = isl_pw_aff_cow(pwaff);
3793 if (!pwaff)
3794 return NULL;
3795 if (pwaff->n == 0)
3796 return pwaff;
3798 for (i = 0; i < pwaff->n; ++i) {
3799 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3800 if (!pwaff->p[i].set)
3801 return isl_pw_aff_free(pwaff);
3804 return pwaff;
3807 /* Mark the domains of the elements of "list" as rational.
3809 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3810 __isl_take isl_pw_aff_list *list)
3812 int i, n;
3814 if (!list)
3815 return NULL;
3816 if (list->n == 0)
3817 return list;
3819 n = list->n;
3820 for (i = 0; i < n; ++i) {
3821 isl_pw_aff *pa;
3823 pa = isl_pw_aff_list_get_pw_aff(list, i);
3824 pa = isl_pw_aff_set_rational(pa);
3825 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3828 return list;
3831 /* Do the parameters of "aff" match those of "space"?
3833 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3834 __isl_keep isl_space *space)
3836 isl_space *aff_space;
3837 isl_bool match;
3839 if (!aff || !space)
3840 return isl_bool_error;
3842 aff_space = isl_aff_get_domain_space(aff);
3844 match = isl_space_has_equal_params(space, aff_space);
3846 isl_space_free(aff_space);
3847 return match;
3850 /* Check that the domain space of "aff" matches "space".
3852 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3853 __isl_keep isl_space *space)
3855 isl_space *aff_space;
3856 isl_bool match;
3858 if (!aff || !space)
3859 return isl_stat_error;
3861 aff_space = isl_aff_get_domain_space(aff);
3863 match = isl_space_has_equal_params(space, aff_space);
3864 if (match < 0)
3865 goto error;
3866 if (!match)
3867 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3868 "parameters don't match", goto error);
3869 match = isl_space_tuple_is_equal(space, isl_dim_in,
3870 aff_space, isl_dim_set);
3871 if (match < 0)
3872 goto error;
3873 if (!match)
3874 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3875 "domains don't match", goto error);
3876 isl_space_free(aff_space);
3877 return isl_stat_ok;
3878 error:
3879 isl_space_free(aff_space);
3880 return isl_stat_error;
3883 /* Return the shared (universe) domain of the elements of "ma".
3885 * Since an isl_multi_aff (and an isl_aff) is always total,
3886 * the domain is always the universe set in its domain space.
3887 * This is a helper function for use in the generic isl_multi_*_bind.
3889 static __isl_give isl_basic_set *isl_multi_aff_domain(
3890 __isl_take isl_multi_aff *ma)
3892 isl_space *space;
3894 space = isl_multi_aff_get_space(ma);
3895 isl_multi_aff_free(ma);
3897 return isl_basic_set_universe(isl_space_domain(space));
3900 #undef BASE
3901 #define BASE aff
3903 #include <isl_multi_no_explicit_domain.c>
3904 #include <isl_multi_templ.c>
3905 #include <isl_multi_apply_set.c>
3906 #include <isl_multi_arith_templ.c>
3907 #include <isl_multi_bind_domain_templ.c>
3908 #include <isl_multi_cmp.c>
3909 #include <isl_multi_dim_id_templ.c>
3910 #include <isl_multi_dims.c>
3911 #include <isl_multi_floor.c>
3912 #include <isl_multi_from_base_templ.c>
3913 #include <isl_multi_identity_templ.c>
3914 #include <isl_multi_locals_templ.c>
3915 #include <isl_multi_move_dims_templ.c>
3916 #include <isl_multi_nan_templ.c>
3917 #include <isl_multi_product_templ.c>
3918 #include <isl_multi_splice_templ.c>
3919 #include <isl_multi_tuple_id_templ.c>
3920 #include <isl_multi_zero_templ.c>
3922 #undef DOMBASE
3923 #define DOMBASE set
3924 #include <isl_multi_gist.c>
3926 #undef DOMBASE
3927 #define DOMBASE basic_set
3928 #include <isl_multi_bind_templ.c>
3930 /* Construct an isl_multi_aff living in "space" that corresponds
3931 * to the affine transformation matrix "mat".
3933 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3934 __isl_take isl_space *space, __isl_take isl_mat *mat)
3936 isl_ctx *ctx;
3937 isl_local_space *ls = NULL;
3938 isl_multi_aff *ma = NULL;
3939 isl_size n_row, n_col, n_out, total;
3940 int i;
3942 if (!space || !mat)
3943 goto error;
3945 ctx = isl_mat_get_ctx(mat);
3947 n_row = isl_mat_rows(mat);
3948 n_col = isl_mat_cols(mat);
3949 n_out = isl_space_dim(space, isl_dim_out);
3950 total = isl_space_dim(space, isl_dim_all);
3951 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3952 goto error;
3953 if (n_row < 1)
3954 isl_die(ctx, isl_error_invalid,
3955 "insufficient number of rows", goto error);
3956 if (n_col < 1)
3957 isl_die(ctx, isl_error_invalid,
3958 "insufficient number of columns", goto error);
3959 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3960 isl_die(ctx, isl_error_invalid,
3961 "dimension mismatch", goto error);
3963 ma = isl_multi_aff_zero(isl_space_copy(space));
3964 ls = isl_local_space_from_space(isl_space_domain(space));
3966 for (i = 0; i < n_row - 1; ++i) {
3967 isl_vec *v;
3968 isl_aff *aff;
3970 v = isl_vec_alloc(ctx, 1 + n_col);
3971 if (!v)
3972 goto error;
3973 isl_int_set(v->el[0], mat->row[0][0]);
3974 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3975 v = isl_vec_normalize(v);
3976 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3977 ma = isl_multi_aff_set_aff(ma, i, aff);
3980 isl_local_space_free(ls);
3981 isl_mat_free(mat);
3982 return ma;
3983 error:
3984 isl_local_space_free(ls);
3985 isl_mat_free(mat);
3986 isl_multi_aff_free(ma);
3987 return NULL;
3990 /* Remove any internal structure of the domain of "ma".
3991 * If there is any such internal structure in the input,
3992 * then the name of the corresponding space is also removed.
3994 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3995 __isl_take isl_multi_aff *ma)
3997 isl_space *space;
3999 if (!ma)
4000 return NULL;
4002 if (!ma->space->nested[0])
4003 return ma;
4005 space = isl_multi_aff_get_space(ma);
4006 space = isl_space_flatten_domain(space);
4007 ma = isl_multi_aff_reset_space(ma, space);
4009 return ma;
4012 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4013 * of the space to its domain.
4015 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4017 int i;
4018 isl_size n_in;
4019 isl_local_space *ls;
4020 isl_multi_aff *ma;
4022 if (!space)
4023 return NULL;
4024 if (!isl_space_is_map(space))
4025 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4026 "not a map space", goto error);
4028 n_in = isl_space_dim(space, isl_dim_in);
4029 if (n_in < 0)
4030 goto error;
4031 space = isl_space_domain_map(space);
4033 ma = isl_multi_aff_alloc(isl_space_copy(space));
4034 if (n_in == 0) {
4035 isl_space_free(space);
4036 return ma;
4039 space = isl_space_domain(space);
4040 ls = isl_local_space_from_space(space);
4041 for (i = 0; i < n_in; ++i) {
4042 isl_aff *aff;
4044 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4045 isl_dim_set, i);
4046 ma = isl_multi_aff_set_aff(ma, i, aff);
4048 isl_local_space_free(ls);
4049 return ma;
4050 error:
4051 isl_space_free(space);
4052 return NULL;
4055 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4056 * of the space to its range.
4058 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4060 int i;
4061 isl_size n_in, n_out;
4062 isl_local_space *ls;
4063 isl_multi_aff *ma;
4065 if (!space)
4066 return NULL;
4067 if (!isl_space_is_map(space))
4068 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4069 "not a map space", goto error);
4071 n_in = isl_space_dim(space, isl_dim_in);
4072 n_out = isl_space_dim(space, isl_dim_out);
4073 if (n_in < 0 || n_out < 0)
4074 goto error;
4075 space = isl_space_range_map(space);
4077 ma = isl_multi_aff_alloc(isl_space_copy(space));
4078 if (n_out == 0) {
4079 isl_space_free(space);
4080 return ma;
4083 space = isl_space_domain(space);
4084 ls = isl_local_space_from_space(space);
4085 for (i = 0; i < n_out; ++i) {
4086 isl_aff *aff;
4088 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4089 isl_dim_set, n_in + i);
4090 ma = isl_multi_aff_set_aff(ma, i, aff);
4092 isl_local_space_free(ls);
4093 return ma;
4094 error:
4095 isl_space_free(space);
4096 return NULL;
4099 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4100 * of the space to its range.
4102 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4103 __isl_take isl_space *space)
4105 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4108 /* Given the space of a set and a range of set dimensions,
4109 * construct an isl_multi_aff that projects out those dimensions.
4111 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4112 __isl_take isl_space *space, enum isl_dim_type type,
4113 unsigned first, unsigned n)
4115 int i;
4116 isl_size dim;
4117 isl_local_space *ls;
4118 isl_multi_aff *ma;
4120 if (!space)
4121 return NULL;
4122 if (!isl_space_is_set(space))
4123 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4124 "expecting set space", goto error);
4125 if (type != isl_dim_set)
4126 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4127 "only set dimensions can be projected out", goto error);
4128 if (isl_space_check_range(space, type, first, n) < 0)
4129 goto error;
4131 dim = isl_space_dim(space, isl_dim_set);
4132 if (dim < 0)
4133 goto error;
4135 space = isl_space_from_domain(space);
4136 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4138 if (dim == n)
4139 return isl_multi_aff_alloc(space);
4141 ma = isl_multi_aff_alloc(isl_space_copy(space));
4142 space = isl_space_domain(space);
4143 ls = isl_local_space_from_space(space);
4145 for (i = 0; i < first; ++i) {
4146 isl_aff *aff;
4148 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4149 isl_dim_set, i);
4150 ma = isl_multi_aff_set_aff(ma, i, aff);
4153 for (i = 0; i < dim - (first + n); ++i) {
4154 isl_aff *aff;
4156 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4157 isl_dim_set, first + n + i);
4158 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4161 isl_local_space_free(ls);
4162 return ma;
4163 error:
4164 isl_space_free(space);
4165 return NULL;
4168 /* Given the space of a set and a range of set dimensions,
4169 * construct an isl_pw_multi_aff that projects out those dimensions.
4171 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4172 __isl_take isl_space *space, enum isl_dim_type type,
4173 unsigned first, unsigned n)
4175 isl_multi_aff *ma;
4177 ma = isl_multi_aff_project_out_map(space, type, first, n);
4178 return isl_pw_multi_aff_from_multi_aff(ma);
4181 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4182 * domain.
4184 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4185 __isl_take isl_multi_aff *ma)
4187 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4188 return isl_pw_multi_aff_alloc(dom, ma);
4191 /* Create a piecewise multi-affine expression in the given space that maps each
4192 * input dimension to the corresponding output dimension.
4194 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4195 __isl_take isl_space *space)
4197 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4200 /* Exploit the equalities in "eq" to simplify the affine expressions.
4202 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4203 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4205 int i;
4207 maff = isl_multi_aff_cow(maff);
4208 if (!maff || !eq)
4209 goto error;
4211 for (i = 0; i < maff->n; ++i) {
4212 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4213 isl_basic_set_copy(eq));
4214 if (!maff->u.p[i])
4215 goto error;
4218 isl_basic_set_free(eq);
4219 return maff;
4220 error:
4221 isl_basic_set_free(eq);
4222 isl_multi_aff_free(maff);
4223 return NULL;
4226 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4227 isl_int f)
4229 int i;
4231 maff = isl_multi_aff_cow(maff);
4232 if (!maff)
4233 return NULL;
4235 for (i = 0; i < maff->n; ++i) {
4236 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4237 if (!maff->u.p[i])
4238 return isl_multi_aff_free(maff);
4241 return maff;
4244 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4245 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4247 maff1 = isl_multi_aff_add(maff1, maff2);
4248 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4249 return maff1;
4252 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4254 if (!maff)
4255 return -1;
4257 return 0;
4260 /* Return the set of domain elements where "ma1" is lexicographically
4261 * smaller than or equal to "ma2".
4263 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4264 __isl_take isl_multi_aff *ma2)
4266 return isl_multi_aff_lex_ge_set(ma2, ma1);
4269 /* Return the set of domain elements where "ma1" is lexicographically
4270 * smaller than "ma2".
4272 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4273 __isl_take isl_multi_aff *ma2)
4275 return isl_multi_aff_lex_gt_set(ma2, ma1);
4278 /* Return the set of domain elements where "ma1" and "ma2"
4279 * satisfy "order".
4281 static __isl_give isl_set *isl_multi_aff_order_set(
4282 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4283 __isl_give isl_map *order(__isl_take isl_space *set_space))
4285 isl_space *space;
4286 isl_map *map1, *map2;
4287 isl_map *map, *ge;
4289 map1 = isl_map_from_multi_aff_internal(ma1);
4290 map2 = isl_map_from_multi_aff_internal(ma2);
4291 map = isl_map_range_product(map1, map2);
4292 space = isl_space_range(isl_map_get_space(map));
4293 space = isl_space_domain(isl_space_unwrap(space));
4294 ge = order(space);
4295 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4297 return isl_map_domain(map);
4300 /* Return the set of domain elements where "ma1" is lexicographically
4301 * greater than or equal to "ma2".
4303 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4304 __isl_take isl_multi_aff *ma2)
4306 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4309 /* Return the set of domain elements where "ma1" is lexicographically
4310 * greater than "ma2".
4312 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4313 __isl_take isl_multi_aff *ma2)
4315 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4318 #undef PW
4319 #define PW isl_pw_multi_aff
4320 #undef EL
4321 #define EL isl_multi_aff
4322 #undef EL_IS_ZERO
4323 #define EL_IS_ZERO is_empty
4324 #undef ZERO
4325 #define ZERO empty
4326 #undef IS_ZERO
4327 #define IS_ZERO is_empty
4328 #undef FIELD
4329 #define FIELD maff
4330 #undef DEFAULT_IS_ZERO
4331 #define DEFAULT_IS_ZERO 0
4333 #define NO_SUB
4334 #define NO_OPT
4335 #define NO_INSERT_DIMS
4336 #define NO_LIFT
4337 #define NO_MORPH
4339 #include <isl_pw_templ.c>
4340 #include <isl_pw_bind_domain_templ.c>
4341 #include <isl_pw_union_opt.c>
4343 #undef NO_SUB
4345 #undef BASE
4346 #define BASE pw_multi_aff
4348 #include <isl_union_multi.c>
4349 #include <isl_union_neg.c>
4351 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4352 __isl_take isl_pw_multi_aff *pma1,
4353 __isl_take isl_pw_multi_aff *pma2)
4355 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4356 &isl_multi_aff_lex_ge_set);
4359 /* Given two piecewise multi affine expressions, return a piecewise
4360 * multi-affine expression defined on the union of the definition domains
4361 * of the inputs that is equal to the lexicographic maximum of the two
4362 * inputs on each cell. If only one of the two inputs is defined on
4363 * a given cell, then it is considered to be the maximum.
4365 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4366 __isl_take isl_pw_multi_aff *pma1,
4367 __isl_take isl_pw_multi_aff *pma2)
4369 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4370 &pw_multi_aff_union_lexmax);
4373 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4374 __isl_take isl_pw_multi_aff *pma1,
4375 __isl_take isl_pw_multi_aff *pma2)
4377 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4378 &isl_multi_aff_lex_le_set);
4381 /* Given two piecewise multi affine expressions, return a piecewise
4382 * multi-affine expression defined on the union of the definition domains
4383 * of the inputs that is equal to the lexicographic minimum of the two
4384 * inputs on each cell. If only one of the two inputs is defined on
4385 * a given cell, then it is considered to be the minimum.
4387 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4388 __isl_take isl_pw_multi_aff *pma1,
4389 __isl_take isl_pw_multi_aff *pma2)
4391 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4392 &pw_multi_aff_union_lexmin);
4395 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4396 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4398 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4399 &isl_multi_aff_add);
4402 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4403 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4405 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4406 &pw_multi_aff_add);
4409 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4410 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4412 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4413 &isl_multi_aff_sub);
4416 /* Subtract "pma2" from "pma1" and return the result.
4418 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4419 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4421 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4422 &pw_multi_aff_sub);
4425 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4426 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4428 return isl_pw_multi_aff_union_add_(pma1, pma2);
4431 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4432 * with the actual sum on the shared domain and
4433 * the defined expression on the symmetric difference of the domains.
4435 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4436 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4438 return isl_union_pw_aff_union_add_(upa1, upa2);
4441 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4442 * with the actual sum on the shared domain and
4443 * the defined expression on the symmetric difference of the domains.
4445 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4446 __isl_take isl_union_pw_multi_aff *upma1,
4447 __isl_take isl_union_pw_multi_aff *upma2)
4449 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4452 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4453 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4455 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4456 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4458 int i, j, n;
4459 isl_space *space;
4460 isl_pw_multi_aff *res;
4462 if (!pma1 || !pma2)
4463 goto error;
4465 n = pma1->n * pma2->n;
4466 space = isl_space_product(isl_space_copy(pma1->dim),
4467 isl_space_copy(pma2->dim));
4468 res = isl_pw_multi_aff_alloc_size(space, n);
4470 for (i = 0; i < pma1->n; ++i) {
4471 for (j = 0; j < pma2->n; ++j) {
4472 isl_set *domain;
4473 isl_multi_aff *ma;
4475 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4476 isl_set_copy(pma2->p[j].set));
4477 ma = isl_multi_aff_product(
4478 isl_multi_aff_copy(pma1->p[i].maff),
4479 isl_multi_aff_copy(pma2->p[j].maff));
4480 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4484 isl_pw_multi_aff_free(pma1);
4485 isl_pw_multi_aff_free(pma2);
4486 return res;
4487 error:
4488 isl_pw_multi_aff_free(pma1);
4489 isl_pw_multi_aff_free(pma2);
4490 return NULL;
4493 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4494 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4496 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4497 &pw_multi_aff_product);
4500 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4501 * denominator "denom".
4502 * "denom" is allowed to be negative, in which case the actual denominator
4503 * is -denom and the expressions are added instead.
4505 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4506 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4508 int i, first;
4509 int sign;
4510 isl_int d;
4512 first = isl_seq_first_non_zero(c, n);
4513 if (first == -1)
4514 return aff;
4516 sign = isl_int_sgn(denom);
4517 isl_int_init(d);
4518 isl_int_abs(d, denom);
4519 for (i = first; i < n; ++i) {
4520 isl_aff *aff_i;
4522 if (isl_int_is_zero(c[i]))
4523 continue;
4524 aff_i = isl_multi_aff_get_aff(ma, i);
4525 aff_i = isl_aff_scale(aff_i, c[i]);
4526 aff_i = isl_aff_scale_down(aff_i, d);
4527 if (sign >= 0)
4528 aff = isl_aff_sub(aff, aff_i);
4529 else
4530 aff = isl_aff_add(aff, aff_i);
4532 isl_int_clear(d);
4534 return aff;
4537 /* Extract an affine expression that expresses the output dimension "pos"
4538 * of "bmap" in terms of the parameters and input dimensions from
4539 * equality "eq".
4540 * Note that this expression may involve integer divisions defined
4541 * in terms of parameters and input dimensions.
4542 * The equality may also involve references to earlier (but not later)
4543 * output dimensions. These are replaced by the corresponding elements
4544 * in "ma".
4546 * If the equality is of the form
4548 * f(i) + h(j) + a x + g(i) = 0,
4550 * with f(i) a linear combinations of the parameters and input dimensions,
4551 * g(i) a linear combination of integer divisions defined in terms of the same
4552 * and h(j) a linear combinations of earlier output dimensions,
4553 * then the affine expression is
4555 * (-f(i) - g(i))/a - h(j)/a
4557 * If the equality is of the form
4559 * f(i) + h(j) - a x + g(i) = 0,
4561 * then the affine expression is
4563 * (f(i) + g(i))/a - h(j)/(-a)
4566 * If "div" refers to an integer division (i.e., it is smaller than
4567 * the number of integer divisions), then the equality constraint
4568 * does involve an integer division (the one at position "div") that
4569 * is defined in terms of output dimensions. However, this integer
4570 * division can be eliminated by exploiting a pair of constraints
4571 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4572 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4573 * -l + x >= 0.
4574 * In particular, let
4576 * x = e(i) + m floor(...)
4578 * with e(i) the expression derived above and floor(...) the integer
4579 * division involving output dimensions.
4580 * From
4582 * l <= x <= l + n,
4584 * we have
4586 * 0 <= x - l <= n
4588 * This means
4590 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4591 * = (e(i) - l) mod m
4593 * Therefore,
4595 * x - l = (e(i) - l) mod m
4597 * or
4599 * x = ((e(i) - l) mod m) + l
4601 * The variable "shift" below contains the expression -l, which may
4602 * also involve a linear combination of earlier output dimensions.
4604 static __isl_give isl_aff *extract_aff_from_equality(
4605 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4606 __isl_keep isl_multi_aff *ma)
4608 unsigned o_out;
4609 isl_size n_div, n_out;
4610 isl_ctx *ctx;
4611 isl_local_space *ls;
4612 isl_aff *aff, *shift;
4613 isl_val *mod;
4615 ctx = isl_basic_map_get_ctx(bmap);
4616 ls = isl_basic_map_get_local_space(bmap);
4617 ls = isl_local_space_domain(ls);
4618 aff = isl_aff_alloc(isl_local_space_copy(ls));
4619 if (!aff)
4620 goto error;
4621 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4622 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4623 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4624 if (n_out < 0 || n_div < 0)
4625 goto error;
4626 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4627 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4628 isl_seq_cpy(aff->v->el + 1 + o_out,
4629 bmap->eq[eq] + o_out + n_out, n_div);
4630 } else {
4631 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4632 isl_seq_neg(aff->v->el + 1 + o_out,
4633 bmap->eq[eq] + o_out + n_out, n_div);
4635 if (div < n_div)
4636 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4637 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4638 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4639 bmap->eq[eq][o_out + pos]);
4640 if (div < n_div) {
4641 shift = isl_aff_alloc(isl_local_space_copy(ls));
4642 if (!shift)
4643 goto error;
4644 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4645 isl_seq_cpy(shift->v->el + 1 + o_out,
4646 bmap->ineq[ineq] + o_out + n_out, n_div);
4647 isl_int_set_si(shift->v->el[0], 1);
4648 shift = subtract_initial(shift, ma, pos,
4649 bmap->ineq[ineq] + o_out, ctx->negone);
4650 aff = isl_aff_add(aff, isl_aff_copy(shift));
4651 mod = isl_val_int_from_isl_int(ctx,
4652 bmap->eq[eq][o_out + n_out + div]);
4653 mod = isl_val_abs(mod);
4654 aff = isl_aff_mod_val(aff, mod);
4655 aff = isl_aff_sub(aff, shift);
4658 isl_local_space_free(ls);
4659 return aff;
4660 error:
4661 isl_local_space_free(ls);
4662 isl_aff_free(aff);
4663 return NULL;
4666 /* Given a basic map with output dimensions defined
4667 * in terms of the parameters input dimensions and earlier
4668 * output dimensions using an equality (and possibly a pair on inequalities),
4669 * extract an isl_aff that expresses output dimension "pos" in terms
4670 * of the parameters and input dimensions.
4671 * Note that this expression may involve integer divisions defined
4672 * in terms of parameters and input dimensions.
4673 * "ma" contains the expressions corresponding to earlier output dimensions.
4675 * This function shares some similarities with
4676 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4678 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4679 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4681 int eq, div, ineq;
4682 isl_aff *aff;
4684 if (!bmap)
4685 return NULL;
4686 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4687 if (eq >= bmap->n_eq)
4688 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4689 "unable to find suitable equality", return NULL);
4690 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4692 aff = isl_aff_remove_unused_divs(aff);
4693 return aff;
4696 /* Given a basic map where each output dimension is defined
4697 * in terms of the parameters and input dimensions using an equality,
4698 * extract an isl_multi_aff that expresses the output dimensions in terms
4699 * of the parameters and input dimensions.
4701 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4702 __isl_take isl_basic_map *bmap)
4704 int i;
4705 isl_size n_out;
4706 isl_multi_aff *ma;
4708 if (!bmap)
4709 return NULL;
4711 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4712 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4713 if (n_out < 0)
4714 ma = isl_multi_aff_free(ma);
4716 for (i = 0; i < n_out; ++i) {
4717 isl_aff *aff;
4719 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4720 ma = isl_multi_aff_set_aff(ma, i, aff);
4723 isl_basic_map_free(bmap);
4725 return ma;
4728 /* Given a basic set where each set dimension is defined
4729 * in terms of the parameters using an equality,
4730 * extract an isl_multi_aff that expresses the set dimensions in terms
4731 * of the parameters.
4733 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4734 __isl_take isl_basic_set *bset)
4736 return extract_isl_multi_aff_from_basic_map(bset);
4739 /* Create an isl_pw_multi_aff that is equivalent to
4740 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4741 * The given basic map is such that each output dimension is defined
4742 * in terms of the parameters and input dimensions using an equality.
4744 * Since some applications expect the result of isl_pw_multi_aff_from_map
4745 * to only contain integer affine expressions, we compute the floor
4746 * of the expression before returning.
4748 * Remove all constraints involving local variables without
4749 * an explicit representation (resulting in the removal of those
4750 * local variables) prior to the actual extraction to ensure
4751 * that the local spaces in which the resulting affine expressions
4752 * are created do not contain any unknown local variables.
4753 * Removing such constraints is safe because constraints involving
4754 * unknown local variables are not used to determine whether
4755 * a basic map is obviously single-valued.
4757 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4758 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4760 isl_multi_aff *ma;
4762 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4763 ma = extract_isl_multi_aff_from_basic_map(bmap);
4764 ma = isl_multi_aff_floor(ma);
4765 return isl_pw_multi_aff_alloc(domain, ma);
4768 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4769 * This obviously only works if the input "map" is single-valued.
4770 * If so, we compute the lexicographic minimum of the image in the form
4771 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4772 * to its lexicographic minimum.
4773 * If the input is not single-valued, we produce an error.
4775 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4776 __isl_take isl_map *map)
4778 int i;
4779 int sv;
4780 isl_pw_multi_aff *pma;
4782 sv = isl_map_is_single_valued(map);
4783 if (sv < 0)
4784 goto error;
4785 if (!sv)
4786 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4787 "map is not single-valued", goto error);
4788 map = isl_map_make_disjoint(map);
4789 if (!map)
4790 return NULL;
4792 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4794 for (i = 0; i < map->n; ++i) {
4795 isl_pw_multi_aff *pma_i;
4796 isl_basic_map *bmap;
4797 bmap = isl_basic_map_copy(map->p[i]);
4798 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4799 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4802 isl_map_free(map);
4803 return pma;
4804 error:
4805 isl_map_free(map);
4806 return NULL;
4809 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4810 * taking into account that the output dimension at position "d"
4811 * can be represented as
4813 * x = floor((e(...) + c1) / m)
4815 * given that constraint "i" is of the form
4817 * e(...) + c1 - m x >= 0
4820 * Let "map" be of the form
4822 * A -> B
4824 * We construct a mapping
4826 * A -> [A -> x = floor(...)]
4828 * apply that to the map, obtaining
4830 * [A -> x = floor(...)] -> B
4832 * and equate dimension "d" to x.
4833 * We then compute a isl_pw_multi_aff representation of the resulting map
4834 * and plug in the mapping above.
4836 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4837 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4839 isl_ctx *ctx;
4840 isl_space *space = NULL;
4841 isl_local_space *ls;
4842 isl_multi_aff *ma;
4843 isl_aff *aff;
4844 isl_vec *v;
4845 isl_map *insert;
4846 int offset;
4847 isl_size n;
4848 isl_size n_in;
4849 isl_pw_multi_aff *pma;
4850 isl_bool is_set;
4852 is_set = isl_map_is_set(map);
4853 if (is_set < 0)
4854 goto error;
4856 offset = isl_basic_map_offset(hull, isl_dim_out);
4857 ctx = isl_map_get_ctx(map);
4858 space = isl_space_domain(isl_map_get_space(map));
4859 n_in = isl_space_dim(space, isl_dim_set);
4860 n = isl_space_dim(space, isl_dim_all);
4861 if (n_in < 0 || n < 0)
4862 goto error;
4864 v = isl_vec_alloc(ctx, 1 + 1 + n);
4865 if (v) {
4866 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4867 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4869 isl_basic_map_free(hull);
4871 ls = isl_local_space_from_space(isl_space_copy(space));
4872 aff = isl_aff_alloc_vec(ls, v);
4873 aff = isl_aff_floor(aff);
4874 if (is_set) {
4875 isl_space_free(space);
4876 ma = isl_multi_aff_from_aff(aff);
4877 } else {
4878 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4879 ma = isl_multi_aff_range_product(ma,
4880 isl_multi_aff_from_aff(aff));
4883 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4884 map = isl_map_apply_domain(map, insert);
4885 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4886 pma = isl_pw_multi_aff_from_map(map);
4887 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4889 return pma;
4890 error:
4891 isl_space_free(space);
4892 isl_map_free(map);
4893 isl_basic_map_free(hull);
4894 return NULL;
4897 /* Is constraint "c" of the form
4899 * e(...) + c1 - m x >= 0
4901 * or
4903 * -e(...) + c2 + m x >= 0
4905 * where m > 1 and e only depends on parameters and input dimemnsions?
4907 * "offset" is the offset of the output dimensions
4908 * "pos" is the position of output dimension x.
4910 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4912 if (isl_int_is_zero(c[offset + d]))
4913 return 0;
4914 if (isl_int_is_one(c[offset + d]))
4915 return 0;
4916 if (isl_int_is_negone(c[offset + d]))
4917 return 0;
4918 if (isl_seq_first_non_zero(c + offset, d) != -1)
4919 return 0;
4920 if (isl_seq_first_non_zero(c + offset + d + 1,
4921 total - (offset + d + 1)) != -1)
4922 return 0;
4923 return 1;
4926 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4928 * As a special case, we first check if there is any pair of constraints,
4929 * shared by all the basic maps in "map" that force a given dimension
4930 * to be equal to the floor of some affine combination of the input dimensions.
4932 * In particular, if we can find two constraints
4934 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4936 * and
4938 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4940 * where m > 1 and e only depends on parameters and input dimemnsions,
4941 * and such that
4943 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4945 * then we know that we can take
4947 * x = floor((e(...) + c1) / m)
4949 * without having to perform any computation.
4951 * Note that we know that
4953 * c1 + c2 >= 1
4955 * If c1 + c2 were 0, then we would have detected an equality during
4956 * simplification. If c1 + c2 were negative, then we would have detected
4957 * a contradiction.
4959 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4960 __isl_take isl_map *map)
4962 int d;
4963 isl_size dim;
4964 int i, j, n;
4965 int offset;
4966 isl_size total;
4967 isl_int sum;
4968 isl_basic_map *hull;
4970 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4971 dim = isl_map_dim(map, isl_dim_out);
4972 total = isl_basic_map_dim(hull, isl_dim_all);
4973 if (dim < 0 || total < 0)
4974 goto error;
4976 isl_int_init(sum);
4977 offset = isl_basic_map_offset(hull, isl_dim_out);
4978 n = hull->n_ineq;
4979 for (d = 0; d < dim; ++d) {
4980 for (i = 0; i < n; ++i) {
4981 if (!is_potential_div_constraint(hull->ineq[i],
4982 offset, d, 1 + total))
4983 continue;
4984 for (j = i + 1; j < n; ++j) {
4985 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4986 hull->ineq[j] + 1, total))
4987 continue;
4988 isl_int_add(sum, hull->ineq[i][0],
4989 hull->ineq[j][0]);
4990 if (isl_int_abs_lt(sum,
4991 hull->ineq[i][offset + d]))
4992 break;
4995 if (j >= n)
4996 continue;
4997 isl_int_clear(sum);
4998 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4999 j = i;
5000 return pw_multi_aff_from_map_div(map, hull, d, j);
5003 isl_int_clear(sum);
5004 isl_basic_map_free(hull);
5005 return pw_multi_aff_from_map_base(map);
5006 error:
5007 isl_map_free(map);
5008 isl_basic_map_free(hull);
5009 return NULL;
5012 /* Given an affine expression
5014 * [A -> B] -> f(A,B)
5016 * construct an isl_multi_aff
5018 * [A -> B] -> B'
5020 * such that dimension "d" in B' is set to "aff" and the remaining
5021 * dimensions are set equal to the corresponding dimensions in B.
5022 * "n_in" is the dimension of the space A.
5023 * "n_out" is the dimension of the space B.
5025 * If "is_set" is set, then the affine expression is of the form
5027 * [B] -> f(B)
5029 * and we construct an isl_multi_aff
5031 * B -> B'
5033 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5034 unsigned n_in, unsigned n_out, int is_set)
5036 int i;
5037 isl_multi_aff *ma;
5038 isl_space *space, *space2;
5039 isl_local_space *ls;
5041 space = isl_aff_get_domain_space(aff);
5042 ls = isl_local_space_from_space(isl_space_copy(space));
5043 space2 = isl_space_copy(space);
5044 if (!is_set)
5045 space2 = isl_space_range(isl_space_unwrap(space2));
5046 space = isl_space_map_from_domain_and_range(space, space2);
5047 ma = isl_multi_aff_alloc(space);
5048 ma = isl_multi_aff_set_aff(ma, d, aff);
5050 for (i = 0; i < n_out; ++i) {
5051 if (i == d)
5052 continue;
5053 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5054 isl_dim_set, n_in + i);
5055 ma = isl_multi_aff_set_aff(ma, i, aff);
5058 isl_local_space_free(ls);
5060 return ma;
5063 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5064 * taking into account that the dimension at position "d" can be written as
5066 * x = m a + f(..) (1)
5068 * where m is equal to "gcd".
5069 * "i" is the index of the equality in "hull" that defines f(..).
5070 * In particular, the equality is of the form
5072 * f(..) - x + m g(existentials) = 0
5074 * or
5076 * -f(..) + x + m g(existentials) = 0
5078 * We basically plug (1) into "map", resulting in a map with "a"
5079 * in the range instead of "x". The corresponding isl_pw_multi_aff
5080 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5082 * Specifically, given the input map
5084 * A -> B
5086 * We first wrap it into a set
5088 * [A -> B]
5090 * and define (1) on top of the corresponding space, resulting in "aff".
5091 * We use this to create an isl_multi_aff that maps the output position "d"
5092 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5093 * We plug this into the wrapped map, unwrap the result and compute the
5094 * corresponding isl_pw_multi_aff.
5095 * The result is an expression
5097 * A -> T(A)
5099 * We adjust that to
5101 * A -> [A -> T(A)]
5103 * so that we can plug that into "aff", after extending the latter to
5104 * a mapping
5106 * [A -> B] -> B'
5109 * If "map" is actually a set, then there is no "A" space, meaning
5110 * that we do not need to perform any wrapping, and that the result
5111 * of the recursive call is of the form
5113 * [T]
5115 * which is plugged into a mapping of the form
5117 * B -> B'
5119 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5120 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5121 isl_int gcd)
5123 isl_set *set;
5124 isl_space *space;
5125 isl_local_space *ls;
5126 isl_aff *aff;
5127 isl_multi_aff *ma;
5128 isl_pw_multi_aff *pma, *id;
5129 isl_size n_in;
5130 unsigned o_out;
5131 isl_size n_out;
5132 isl_bool is_set;
5134 is_set = isl_map_is_set(map);
5135 if (is_set < 0)
5136 goto error;
5138 n_in = isl_basic_map_dim(hull, isl_dim_in);
5139 n_out = isl_basic_map_dim(hull, isl_dim_out);
5140 if (n_in < 0 || n_out < 0)
5141 goto error;
5142 o_out = isl_basic_map_offset(hull, isl_dim_out);
5144 if (is_set)
5145 set = map;
5146 else
5147 set = isl_map_wrap(map);
5148 space = isl_space_map_from_set(isl_set_get_space(set));
5149 ma = isl_multi_aff_identity(space);
5150 ls = isl_local_space_from_space(isl_set_get_space(set));
5151 aff = isl_aff_alloc(ls);
5152 if (aff) {
5153 isl_int_set_si(aff->v->el[0], 1);
5154 if (isl_int_is_one(hull->eq[i][o_out + d]))
5155 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5156 aff->v->size - 1);
5157 else
5158 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5159 aff->v->size - 1);
5160 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5162 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5163 set = isl_set_preimage_multi_aff(set, ma);
5165 ma = range_map(aff, d, n_in, n_out, is_set);
5167 if (is_set)
5168 map = set;
5169 else
5170 map = isl_set_unwrap(set);
5171 pma = isl_pw_multi_aff_from_map(map);
5173 if (!is_set) {
5174 space = isl_pw_multi_aff_get_domain_space(pma);
5175 space = isl_space_map_from_set(space);
5176 id = isl_pw_multi_aff_identity(space);
5177 pma = isl_pw_multi_aff_range_product(id, pma);
5179 id = isl_pw_multi_aff_from_multi_aff(ma);
5180 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5182 isl_basic_map_free(hull);
5183 return pma;
5184 error:
5185 isl_map_free(map);
5186 isl_basic_map_free(hull);
5187 return NULL;
5190 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5191 * "hull" contains the equalities valid for "map".
5193 * Check if any of the output dimensions is "strided".
5194 * That is, we check if it can be written as
5196 * x = m a + f(..)
5198 * with m greater than 1, a some combination of existentially quantified
5199 * variables and f an expression in the parameters and input dimensions.
5200 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5202 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5203 * special case.
5205 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5206 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5208 int i, j;
5209 isl_size n_out;
5210 unsigned o_out;
5211 isl_size n_div;
5212 unsigned o_div;
5213 isl_int gcd;
5215 n_div = isl_basic_map_dim(hull, isl_dim_div);
5216 n_out = isl_basic_map_dim(hull, isl_dim_out);
5217 if (n_div < 0 || n_out < 0)
5218 goto error;
5220 if (n_div == 0) {
5221 isl_basic_map_free(hull);
5222 return pw_multi_aff_from_map_check_div(map);
5225 isl_int_init(gcd);
5227 o_div = isl_basic_map_offset(hull, isl_dim_div);
5228 o_out = isl_basic_map_offset(hull, isl_dim_out);
5230 for (i = 0; i < n_out; ++i) {
5231 for (j = 0; j < hull->n_eq; ++j) {
5232 isl_int *eq = hull->eq[j];
5233 isl_pw_multi_aff *res;
5235 if (!isl_int_is_one(eq[o_out + i]) &&
5236 !isl_int_is_negone(eq[o_out + i]))
5237 continue;
5238 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5239 continue;
5240 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5241 n_out - (i + 1)) != -1)
5242 continue;
5243 isl_seq_gcd(eq + o_div, n_div, &gcd);
5244 if (isl_int_is_zero(gcd))
5245 continue;
5246 if (isl_int_is_one(gcd))
5247 continue;
5249 res = pw_multi_aff_from_map_stride(map, hull,
5250 i, j, gcd);
5251 isl_int_clear(gcd);
5252 return res;
5256 isl_int_clear(gcd);
5257 isl_basic_map_free(hull);
5258 return pw_multi_aff_from_map_check_div(map);
5259 error:
5260 isl_map_free(map);
5261 isl_basic_map_free(hull);
5262 return NULL;
5265 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5267 * As a special case, we first check if all output dimensions are uniquely
5268 * defined in terms of the parameters and input dimensions over the entire
5269 * domain. If so, we extract the desired isl_pw_multi_aff directly
5270 * from the affine hull of "map" and its domain.
5272 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5273 * special cases.
5275 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5277 isl_bool sv;
5278 isl_size n;
5279 isl_basic_map *hull;
5281 n = isl_map_n_basic_map(map);
5282 if (n < 0)
5283 goto error;
5285 if (n == 1) {
5286 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5287 hull = isl_basic_map_plain_affine_hull(hull);
5288 sv = isl_basic_map_plain_is_single_valued(hull);
5289 if (sv >= 0 && sv)
5290 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5291 hull);
5292 isl_basic_map_free(hull);
5294 map = isl_map_detect_equalities(map);
5295 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5296 sv = isl_basic_map_plain_is_single_valued(hull);
5297 if (sv >= 0 && sv)
5298 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5299 if (sv >= 0)
5300 return pw_multi_aff_from_map_check_strides(map, hull);
5301 isl_basic_map_free(hull);
5302 error:
5303 isl_map_free(map);
5304 return NULL;
5307 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5309 return isl_pw_multi_aff_from_map(set);
5312 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5313 * add it to *user.
5315 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5317 isl_union_pw_multi_aff **upma = user;
5318 isl_pw_multi_aff *pma;
5320 pma = isl_pw_multi_aff_from_map(map);
5321 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5323 return *upma ? isl_stat_ok : isl_stat_error;
5326 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5327 * domain.
5329 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5330 __isl_take isl_aff *aff)
5332 isl_multi_aff *ma;
5333 isl_pw_multi_aff *pma;
5335 ma = isl_multi_aff_from_aff(aff);
5336 pma = isl_pw_multi_aff_from_multi_aff(ma);
5337 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5340 /* Try and create an isl_union_pw_multi_aff that is equivalent
5341 * to the given isl_union_map.
5342 * The isl_union_map is required to be single-valued in each space.
5343 * Otherwise, an error is produced.
5345 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5346 __isl_take isl_union_map *umap)
5348 isl_space *space;
5349 isl_union_pw_multi_aff *upma;
5351 space = isl_union_map_get_space(umap);
5352 upma = isl_union_pw_multi_aff_empty(space);
5353 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5354 upma = isl_union_pw_multi_aff_free(upma);
5355 isl_union_map_free(umap);
5357 return upma;
5360 /* Try and create an isl_union_pw_multi_aff that is equivalent
5361 * to the given isl_union_set.
5362 * The isl_union_set is required to be a singleton in each space.
5363 * Otherwise, an error is produced.
5365 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5366 __isl_take isl_union_set *uset)
5368 return isl_union_pw_multi_aff_from_union_map(uset);
5371 /* Return the piecewise affine expression "set ? 1 : 0".
5373 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5375 isl_pw_aff *pa;
5376 isl_space *space = isl_set_get_space(set);
5377 isl_local_space *ls = isl_local_space_from_space(space);
5378 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5379 isl_aff *one = isl_aff_zero_on_domain(ls);
5381 one = isl_aff_add_constant_si(one, 1);
5382 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5383 set = isl_set_complement(set);
5384 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5386 return pa;
5389 /* Plug in "subs" for dimension "type", "pos" of "aff".
5391 * Let i be the dimension to replace and let "subs" be of the form
5393 * f/d
5395 * and "aff" of the form
5397 * (a i + g)/m
5399 * The result is
5401 * (a f + d g')/(m d)
5403 * where g' is the result of plugging in "subs" in each of the integer
5404 * divisions in g.
5406 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5407 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5409 isl_ctx *ctx;
5410 isl_int v;
5411 isl_size n_div;
5413 aff = isl_aff_cow(aff);
5414 if (!aff || !subs)
5415 return isl_aff_free(aff);
5417 ctx = isl_aff_get_ctx(aff);
5418 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5419 isl_die(ctx, isl_error_invalid,
5420 "spaces don't match", return isl_aff_free(aff));
5421 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
5422 if (n_div < 0)
5423 return isl_aff_free(aff);
5424 if (n_div != 0)
5425 isl_die(ctx, isl_error_unsupported,
5426 "cannot handle divs yet", return isl_aff_free(aff));
5428 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5429 if (!aff->ls)
5430 return isl_aff_free(aff);
5432 aff->v = isl_vec_cow(aff->v);
5433 if (!aff->v)
5434 return isl_aff_free(aff);
5436 pos += isl_local_space_offset(aff->ls, type);
5438 isl_int_init(v);
5439 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5440 aff->v->size, subs->v->size, v);
5441 isl_int_clear(v);
5443 return aff;
5446 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5447 * expressions in "maff".
5449 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5450 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5451 __isl_keep isl_aff *subs)
5453 int i;
5455 maff = isl_multi_aff_cow(maff);
5456 if (!maff || !subs)
5457 return isl_multi_aff_free(maff);
5459 if (type == isl_dim_in)
5460 type = isl_dim_set;
5462 for (i = 0; i < maff->n; ++i) {
5463 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5464 type, pos, subs);
5465 if (!maff->u.p[i])
5466 return isl_multi_aff_free(maff);
5469 return maff;
5472 /* Plug in "subs" for dimension "type", "pos" of "pma".
5474 * pma is of the form
5476 * A_i(v) -> M_i(v)
5478 * while subs is of the form
5480 * v' = B_j(v) -> S_j
5482 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5483 * has a contribution in the result, in particular
5485 * C_ij(S_j) -> M_i(S_j)
5487 * Note that plugging in S_j in C_ij may also result in an empty set
5488 * and this contribution should simply be discarded.
5490 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5491 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5492 __isl_keep isl_pw_aff *subs)
5494 int i, j, n;
5495 isl_pw_multi_aff *res;
5497 if (!pma || !subs)
5498 return isl_pw_multi_aff_free(pma);
5500 n = pma->n * subs->n;
5501 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5503 for (i = 0; i < pma->n; ++i) {
5504 for (j = 0; j < subs->n; ++j) {
5505 isl_set *common;
5506 isl_multi_aff *res_ij;
5507 int empty;
5509 common = isl_set_intersect(
5510 isl_set_copy(pma->p[i].set),
5511 isl_set_copy(subs->p[j].set));
5512 common = isl_set_substitute(common,
5513 type, pos, subs->p[j].aff);
5514 empty = isl_set_plain_is_empty(common);
5515 if (empty < 0 || empty) {
5516 isl_set_free(common);
5517 if (empty < 0)
5518 goto error;
5519 continue;
5522 res_ij = isl_multi_aff_substitute(
5523 isl_multi_aff_copy(pma->p[i].maff),
5524 type, pos, subs->p[j].aff);
5526 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5530 isl_pw_multi_aff_free(pma);
5531 return res;
5532 error:
5533 isl_pw_multi_aff_free(pma);
5534 isl_pw_multi_aff_free(res);
5535 return NULL;
5538 /* Compute the preimage of a range of dimensions in the affine expression "src"
5539 * under "ma" and put the result in "dst". The number of dimensions in "src"
5540 * that precede the range is given by "n_before". The number of dimensions
5541 * in the range is given by the number of output dimensions of "ma".
5542 * The number of dimensions that follow the range is given by "n_after".
5543 * If "has_denom" is set (to one),
5544 * then "src" and "dst" have an extra initial denominator.
5545 * "n_div_ma" is the number of existentials in "ma"
5546 * "n_div_bset" is the number of existentials in "src"
5547 * The resulting "dst" (which is assumed to have been allocated by
5548 * the caller) contains coefficients for both sets of existentials,
5549 * first those in "ma" and then those in "src".
5550 * f, c1, c2 and g are temporary objects that have been initialized
5551 * by the caller.
5553 * Let src represent the expression
5555 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5557 * and let ma represent the expressions
5559 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5561 * We start out with the following expression for dst:
5563 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5565 * with the multiplication factor f initially equal to 1
5566 * and f \sum_i b_i v_i kept separately.
5567 * For each x_i that we substitute, we multiply the numerator
5568 * (and denominator) of dst by c_1 = m_i and add the numerator
5569 * of the x_i expression multiplied by c_2 = f b_i,
5570 * after removing the common factors of c_1 and c_2.
5571 * The multiplication factor f also needs to be multiplied by c_1
5572 * for the next x_j, j > i.
5574 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5575 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5576 int n_div_ma, int n_div_bmap,
5577 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5579 int i;
5580 isl_size n_param, n_in, n_out;
5581 int o_dst, o_src;
5583 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5584 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5585 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5586 if (n_param < 0 || n_in < 0 || n_out < 0)
5587 return isl_stat_error;
5589 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5590 o_dst = o_src = has_denom + 1 + n_param + n_before;
5591 isl_seq_clr(dst + o_dst, n_in);
5592 o_dst += n_in;
5593 o_src += n_out;
5594 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5595 o_dst += n_after;
5596 o_src += n_after;
5597 isl_seq_clr(dst + o_dst, n_div_ma);
5598 o_dst += n_div_ma;
5599 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5601 isl_int_set_si(f, 1);
5603 for (i = 0; i < n_out; ++i) {
5604 int offset = has_denom + 1 + n_param + n_before + i;
5606 if (isl_int_is_zero(src[offset]))
5607 continue;
5608 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5609 isl_int_mul(c2, f, src[offset]);
5610 isl_int_gcd(g, c1, c2);
5611 isl_int_divexact(c1, c1, g);
5612 isl_int_divexact(c2, c2, g);
5614 isl_int_mul(f, f, c1);
5615 o_dst = has_denom;
5616 o_src = 1;
5617 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5618 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5619 o_dst += 1 + n_param;
5620 o_src += 1 + n_param;
5621 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5622 o_dst += n_before;
5623 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5624 c2, ma->u.p[i]->v->el + o_src, n_in);
5625 o_dst += n_in;
5626 o_src += n_in;
5627 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5628 o_dst += n_after;
5629 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5630 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5631 o_dst += n_div_ma;
5632 o_src += n_div_ma;
5633 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5634 if (has_denom)
5635 isl_int_mul(dst[0], dst[0], c1);
5638 return isl_stat_ok;
5641 /* Compute the pullback of "aff" by the function represented by "ma".
5642 * In other words, plug in "ma" in "aff". The result is an affine expression
5643 * defined over the domain space of "ma".
5645 * If "aff" is represented by
5647 * (a(p) + b x + c(divs))/d
5649 * and ma is represented by
5651 * x = D(p) + F(y) + G(divs')
5653 * then the result is
5655 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5657 * The divs in the local space of the input are similarly adjusted
5658 * through a call to isl_local_space_preimage_multi_aff.
5660 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5661 __isl_take isl_multi_aff *ma)
5663 isl_aff *res = NULL;
5664 isl_local_space *ls;
5665 isl_size n_div_aff, n_div_ma;
5666 isl_int f, c1, c2, g;
5668 ma = isl_multi_aff_align_divs(ma);
5669 if (!aff || !ma)
5670 goto error;
5672 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5673 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5674 if (n_div_aff < 0 || n_div_ma < 0)
5675 goto error;
5677 ls = isl_aff_get_domain_local_space(aff);
5678 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5679 res = isl_aff_alloc(ls);
5680 if (!res)
5681 goto error;
5683 isl_int_init(f);
5684 isl_int_init(c1);
5685 isl_int_init(c2);
5686 isl_int_init(g);
5688 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5689 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5690 res = isl_aff_free(res);
5692 isl_int_clear(f);
5693 isl_int_clear(c1);
5694 isl_int_clear(c2);
5695 isl_int_clear(g);
5697 isl_aff_free(aff);
5698 isl_multi_aff_free(ma);
5699 res = isl_aff_normalize(res);
5700 return res;
5701 error:
5702 isl_aff_free(aff);
5703 isl_multi_aff_free(ma);
5704 isl_aff_free(res);
5705 return NULL;
5708 /* Compute the pullback of "aff1" by the function represented by "aff2".
5709 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5710 * defined over the domain space of "aff1".
5712 * The domain of "aff1" should match the range of "aff2", which means
5713 * that it should be single-dimensional.
5715 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5716 __isl_take isl_aff *aff2)
5718 isl_multi_aff *ma;
5720 ma = isl_multi_aff_from_aff(aff2);
5721 return isl_aff_pullback_multi_aff(aff1, ma);
5724 /* Compute the pullback of "ma1" by the function represented by "ma2".
5725 * In other words, plug in "ma2" in "ma1".
5727 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5729 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5730 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5732 int i;
5733 isl_space *space = NULL;
5735 ma2 = isl_multi_aff_align_divs(ma2);
5736 ma1 = isl_multi_aff_cow(ma1);
5737 if (!ma1 || !ma2)
5738 goto error;
5740 space = isl_space_join(isl_multi_aff_get_space(ma2),
5741 isl_multi_aff_get_space(ma1));
5743 for (i = 0; i < ma1->n; ++i) {
5744 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5745 isl_multi_aff_copy(ma2));
5746 if (!ma1->u.p[i])
5747 goto error;
5750 ma1 = isl_multi_aff_reset_space(ma1, space);
5751 isl_multi_aff_free(ma2);
5752 return ma1;
5753 error:
5754 isl_space_free(space);
5755 isl_multi_aff_free(ma2);
5756 isl_multi_aff_free(ma1);
5757 return NULL;
5760 /* Compute the pullback of "ma1" by the function represented by "ma2".
5761 * In other words, plug in "ma2" in "ma1".
5763 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5764 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5766 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5767 &isl_multi_aff_pullback_multi_aff_aligned);
5770 /* Extend the local space of "dst" to include the divs
5771 * in the local space of "src".
5773 * If "src" does not have any divs or if the local spaces of "dst" and
5774 * "src" are the same, then no extension is required.
5776 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5777 __isl_keep isl_aff *src)
5779 isl_ctx *ctx;
5780 isl_size src_n_div, dst_n_div;
5781 int *exp1 = NULL;
5782 int *exp2 = NULL;
5783 isl_bool equal;
5784 isl_mat *div;
5786 if (!src || !dst)
5787 return isl_aff_free(dst);
5789 ctx = isl_aff_get_ctx(src);
5790 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5791 if (equal < 0)
5792 return isl_aff_free(dst);
5793 if (!equal)
5794 isl_die(ctx, isl_error_invalid,
5795 "spaces don't match", goto error);
5797 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5798 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5799 if (src_n_div == 0)
5800 return dst;
5801 equal = isl_local_space_is_equal(src->ls, dst->ls);
5802 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5803 return isl_aff_free(dst);
5804 if (equal)
5805 return dst;
5807 exp1 = isl_alloc_array(ctx, int, src_n_div);
5808 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5809 if (!exp1 || (dst_n_div && !exp2))
5810 goto error;
5812 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5813 dst = isl_aff_expand_divs(dst, div, exp2);
5814 free(exp1);
5815 free(exp2);
5817 return dst;
5818 error:
5819 free(exp1);
5820 free(exp2);
5821 return isl_aff_free(dst);
5824 /* Adjust the local spaces of the affine expressions in "maff"
5825 * such that they all have the save divs.
5827 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5828 __isl_take isl_multi_aff *maff)
5830 int i;
5832 if (!maff)
5833 return NULL;
5834 if (maff->n == 0)
5835 return maff;
5836 maff = isl_multi_aff_cow(maff);
5837 if (!maff)
5838 return NULL;
5840 for (i = 1; i < maff->n; ++i)
5841 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5842 for (i = 1; i < maff->n; ++i) {
5843 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5844 if (!maff->u.p[i])
5845 return isl_multi_aff_free(maff);
5848 return maff;
5851 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5853 aff = isl_aff_cow(aff);
5854 if (!aff)
5855 return NULL;
5857 aff->ls = isl_local_space_lift(aff->ls);
5858 if (!aff->ls)
5859 return isl_aff_free(aff);
5861 return aff;
5864 /* Lift "maff" to a space with extra dimensions such that the result
5865 * has no more existentially quantified variables.
5866 * If "ls" is not NULL, then *ls is assigned the local space that lies
5867 * at the basis of the lifting applied to "maff".
5869 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5870 __isl_give isl_local_space **ls)
5872 int i;
5873 isl_space *space;
5874 isl_size n_div;
5876 if (ls)
5877 *ls = NULL;
5879 if (!maff)
5880 return NULL;
5882 if (maff->n == 0) {
5883 if (ls) {
5884 isl_space *space = isl_multi_aff_get_domain_space(maff);
5885 *ls = isl_local_space_from_space(space);
5886 if (!*ls)
5887 return isl_multi_aff_free(maff);
5889 return maff;
5892 maff = isl_multi_aff_cow(maff);
5893 maff = isl_multi_aff_align_divs(maff);
5894 if (!maff)
5895 return NULL;
5897 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5898 if (n_div < 0)
5899 return isl_multi_aff_free(maff);
5900 space = isl_multi_aff_get_space(maff);
5901 space = isl_space_lift(isl_space_domain(space), n_div);
5902 space = isl_space_extend_domain_with_range(space,
5903 isl_multi_aff_get_space(maff));
5904 if (!space)
5905 return isl_multi_aff_free(maff);
5906 isl_space_free(maff->space);
5907 maff->space = space;
5909 if (ls) {
5910 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5911 if (!*ls)
5912 return isl_multi_aff_free(maff);
5915 for (i = 0; i < maff->n; ++i) {
5916 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5917 if (!maff->u.p[i])
5918 goto error;
5921 return maff;
5922 error:
5923 if (ls)
5924 isl_local_space_free(*ls);
5925 return isl_multi_aff_free(maff);
5928 #undef TYPE
5929 #define TYPE isl_pw_multi_aff
5930 static
5931 #include "check_type_range_templ.c"
5933 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5935 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5936 __isl_keep isl_pw_multi_aff *pma, int pos)
5938 int i;
5939 isl_size n_out;
5940 isl_space *space;
5941 isl_pw_aff *pa;
5943 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
5944 return NULL;
5946 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5947 if (n_out < 0)
5948 return NULL;
5950 space = isl_pw_multi_aff_get_space(pma);
5951 space = isl_space_drop_dims(space, isl_dim_out,
5952 pos + 1, n_out - pos - 1);
5953 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5955 pa = isl_pw_aff_alloc_size(space, pma->n);
5956 for (i = 0; i < pma->n; ++i) {
5957 isl_aff *aff;
5958 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5959 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5962 return pa;
5965 /* Return an isl_pw_multi_aff with the given "set" as domain and
5966 * an unnamed zero-dimensional range.
5968 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5969 __isl_take isl_set *set)
5971 isl_multi_aff *ma;
5972 isl_space *space;
5974 space = isl_set_get_space(set);
5975 space = isl_space_from_domain(space);
5976 ma = isl_multi_aff_zero(space);
5977 return isl_pw_multi_aff_alloc(set, ma);
5980 /* Add an isl_pw_multi_aff with the given "set" as domain and
5981 * an unnamed zero-dimensional range to *user.
5983 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5984 void *user)
5986 isl_union_pw_multi_aff **upma = user;
5987 isl_pw_multi_aff *pma;
5989 pma = isl_pw_multi_aff_from_domain(set);
5990 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5992 return isl_stat_ok;
5995 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5996 * an unnamed zero-dimensional range.
5998 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5999 __isl_take isl_union_set *uset)
6001 isl_space *space;
6002 isl_union_pw_multi_aff *upma;
6004 if (!uset)
6005 return NULL;
6007 space = isl_union_set_get_space(uset);
6008 upma = isl_union_pw_multi_aff_empty(space);
6010 if (isl_union_set_foreach_set(uset,
6011 &add_pw_multi_aff_from_domain, &upma) < 0)
6012 goto error;
6014 isl_union_set_free(uset);
6015 return upma;
6016 error:
6017 isl_union_set_free(uset);
6018 isl_union_pw_multi_aff_free(upma);
6019 return NULL;
6022 /* Local data for bin_entry and the callback "fn".
6024 struct isl_union_pw_multi_aff_bin_data {
6025 isl_union_pw_multi_aff *upma2;
6026 isl_union_pw_multi_aff *res;
6027 isl_pw_multi_aff *pma;
6028 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6031 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6032 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6034 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6036 struct isl_union_pw_multi_aff_bin_data *data = user;
6037 isl_stat r;
6039 data->pma = pma;
6040 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6041 data->fn, data);
6042 isl_pw_multi_aff_free(pma);
6044 return r;
6047 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6048 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6049 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6050 * as *entry. The callback should adjust data->res if desired.
6052 static __isl_give isl_union_pw_multi_aff *bin_op(
6053 __isl_take isl_union_pw_multi_aff *upma1,
6054 __isl_take isl_union_pw_multi_aff *upma2,
6055 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6057 isl_space *space;
6058 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6060 space = isl_union_pw_multi_aff_get_space(upma2);
6061 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6062 space = isl_union_pw_multi_aff_get_space(upma1);
6063 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6065 if (!upma1 || !upma2)
6066 goto error;
6068 data.upma2 = upma2;
6069 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6070 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6071 &bin_entry, &data) < 0)
6072 goto error;
6074 isl_union_pw_multi_aff_free(upma1);
6075 isl_union_pw_multi_aff_free(upma2);
6076 return data.res;
6077 error:
6078 isl_union_pw_multi_aff_free(upma1);
6079 isl_union_pw_multi_aff_free(upma2);
6080 isl_union_pw_multi_aff_free(data.res);
6081 return NULL;
6084 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6085 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6087 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6088 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6090 isl_space *space;
6092 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6093 isl_pw_multi_aff_get_space(pma2));
6094 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6095 &isl_multi_aff_range_product);
6098 /* Given two isl_pw_multi_affs A -> B and C -> D,
6099 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6101 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6102 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6104 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6105 &pw_multi_aff_range_product);
6108 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6109 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6111 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6112 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6114 isl_space *space;
6116 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6117 isl_pw_multi_aff_get_space(pma2));
6118 space = isl_space_flatten_range(space);
6119 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6120 &isl_multi_aff_flat_range_product);
6123 /* Given two isl_pw_multi_affs A -> B and C -> D,
6124 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6126 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6127 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6129 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6130 &pw_multi_aff_flat_range_product);
6133 /* If data->pma and "pma2" have the same domain space, then compute
6134 * their flat range product and the result to data->res.
6136 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6137 void *user)
6139 struct isl_union_pw_multi_aff_bin_data *data = user;
6141 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6142 pma2->dim, isl_dim_in)) {
6143 isl_pw_multi_aff_free(pma2);
6144 return isl_stat_ok;
6147 pma2 = isl_pw_multi_aff_flat_range_product(
6148 isl_pw_multi_aff_copy(data->pma), pma2);
6150 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6152 return isl_stat_ok;
6155 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6156 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6158 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6159 __isl_take isl_union_pw_multi_aff *upma1,
6160 __isl_take isl_union_pw_multi_aff *upma2)
6162 return bin_op(upma1, upma2, &flat_range_product_entry);
6165 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6166 * The parameters are assumed to have been aligned.
6168 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6169 * except that it works on two different isl_pw_* types.
6171 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6172 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6173 __isl_take isl_pw_aff *pa)
6175 int i, j, n;
6176 isl_pw_multi_aff *res = NULL;
6178 if (!pma || !pa)
6179 goto error;
6181 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6182 pa->dim, isl_dim_in))
6183 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6184 "domains don't match", goto error);
6185 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6186 goto error;
6188 n = pma->n * pa->n;
6189 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6191 for (i = 0; i < pma->n; ++i) {
6192 for (j = 0; j < pa->n; ++j) {
6193 isl_set *common;
6194 isl_multi_aff *res_ij;
6195 int empty;
6197 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6198 isl_set_copy(pa->p[j].set));
6199 empty = isl_set_plain_is_empty(common);
6200 if (empty < 0 || empty) {
6201 isl_set_free(common);
6202 if (empty < 0)
6203 goto error;
6204 continue;
6207 res_ij = isl_multi_aff_set_aff(
6208 isl_multi_aff_copy(pma->p[i].maff), pos,
6209 isl_aff_copy(pa->p[j].aff));
6210 res_ij = isl_multi_aff_gist(res_ij,
6211 isl_set_copy(common));
6213 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6217 isl_pw_multi_aff_free(pma);
6218 isl_pw_aff_free(pa);
6219 return res;
6220 error:
6221 isl_pw_multi_aff_free(pma);
6222 isl_pw_aff_free(pa);
6223 return isl_pw_multi_aff_free(res);
6226 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6228 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6229 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6230 __isl_take isl_pw_aff *pa)
6232 isl_bool equal_params;
6234 if (!pma || !pa)
6235 goto error;
6236 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6237 if (equal_params < 0)
6238 goto error;
6239 if (equal_params)
6240 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6241 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6242 isl_pw_aff_check_named_params(pa) < 0)
6243 goto error;
6244 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6245 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6246 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6247 error:
6248 isl_pw_multi_aff_free(pma);
6249 isl_pw_aff_free(pa);
6250 return NULL;
6253 /* Do the parameters of "pa" match those of "space"?
6255 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6256 __isl_keep isl_space *space)
6258 isl_space *pa_space;
6259 isl_bool match;
6261 if (!pa || !space)
6262 return isl_bool_error;
6264 pa_space = isl_pw_aff_get_space(pa);
6266 match = isl_space_has_equal_params(space, pa_space);
6268 isl_space_free(pa_space);
6269 return match;
6272 /* Check that the domain space of "pa" matches "space".
6274 isl_stat isl_pw_aff_check_match_domain_space(__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_stat_error;
6283 pa_space = isl_pw_aff_get_space(pa);
6285 match = isl_space_has_equal_params(space, pa_space);
6286 if (match < 0)
6287 goto error;
6288 if (!match)
6289 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6290 "parameters don't match", goto error);
6291 match = isl_space_tuple_is_equal(space, isl_dim_in,
6292 pa_space, isl_dim_in);
6293 if (match < 0)
6294 goto error;
6295 if (!match)
6296 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6297 "domains don't match", goto error);
6298 isl_space_free(pa_space);
6299 return isl_stat_ok;
6300 error:
6301 isl_space_free(pa_space);
6302 return isl_stat_error;
6305 #undef BASE
6306 #define BASE pw_aff
6307 #undef DOMBASE
6308 #define DOMBASE set
6310 #include <isl_multi_explicit_domain.c>
6311 #include <isl_multi_pw_aff_explicit_domain.c>
6312 #include <isl_multi_templ.c>
6313 #include <isl_multi_apply_set.c>
6314 #include <isl_multi_arith_templ.c>
6315 #include <isl_multi_bind_templ.c>
6316 #include <isl_multi_bind_domain_templ.c>
6317 #include <isl_multi_coalesce.c>
6318 #include <isl_multi_domain_templ.c>
6319 #include <isl_multi_dim_id_templ.c>
6320 #include <isl_multi_dims.c>
6321 #include <isl_multi_from_base_templ.c>
6322 #include <isl_multi_gist.c>
6323 #include <isl_multi_hash.c>
6324 #include <isl_multi_identity_templ.c>
6325 #include <isl_multi_align_set.c>
6326 #include <isl_multi_intersect.c>
6327 #include <isl_multi_move_dims_templ.c>
6328 #include <isl_multi_nan_templ.c>
6329 #include <isl_multi_param_templ.c>
6330 #include <isl_multi_product_templ.c>
6331 #include <isl_multi_splice_templ.c>
6332 #include <isl_multi_tuple_id_templ.c>
6333 #include <isl_multi_zero_templ.c>
6335 /* Does "mpa" have a non-trivial explicit domain?
6337 * The explicit domain, if present, is trivial if it represents
6338 * an (obviously) universe set.
6340 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6341 __isl_keep isl_multi_pw_aff *mpa)
6343 if (!mpa)
6344 return isl_bool_error;
6345 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6346 return isl_bool_false;
6347 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6350 /* Scale the elements of "pma" by the corresponding elements of "mv".
6352 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6353 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6355 int i;
6356 isl_bool equal_params;
6358 pma = isl_pw_multi_aff_cow(pma);
6359 if (!pma || !mv)
6360 goto error;
6361 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6362 mv->space, isl_dim_set))
6363 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6364 "spaces don't match", goto error);
6365 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6366 if (equal_params < 0)
6367 goto error;
6368 if (!equal_params) {
6369 pma = isl_pw_multi_aff_align_params(pma,
6370 isl_multi_val_get_space(mv));
6371 mv = isl_multi_val_align_params(mv,
6372 isl_pw_multi_aff_get_space(pma));
6373 if (!pma || !mv)
6374 goto error;
6377 for (i = 0; i < pma->n; ++i) {
6378 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6379 isl_multi_val_copy(mv));
6380 if (!pma->p[i].maff)
6381 goto error;
6384 isl_multi_val_free(mv);
6385 return pma;
6386 error:
6387 isl_multi_val_free(mv);
6388 isl_pw_multi_aff_free(pma);
6389 return NULL;
6392 /* This function is called for each entry of an isl_union_pw_multi_aff.
6393 * If the space of the entry matches that of data->mv,
6394 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6395 * Otherwise, return an empty isl_pw_multi_aff.
6397 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6398 __isl_take isl_pw_multi_aff *pma, void *user)
6400 isl_multi_val *mv = user;
6402 if (!pma)
6403 return NULL;
6404 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6405 mv->space, isl_dim_set)) {
6406 isl_space *space = isl_pw_multi_aff_get_space(pma);
6407 isl_pw_multi_aff_free(pma);
6408 return isl_pw_multi_aff_empty(space);
6411 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6414 /* Scale the elements of "upma" by the corresponding elements of "mv",
6415 * for those entries that match the space of "mv".
6417 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6418 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6420 upma = isl_union_pw_multi_aff_align_params(upma,
6421 isl_multi_val_get_space(mv));
6422 mv = isl_multi_val_align_params(mv,
6423 isl_union_pw_multi_aff_get_space(upma));
6424 if (!upma || !mv)
6425 goto error;
6427 return isl_union_pw_multi_aff_transform(upma,
6428 &union_pw_multi_aff_scale_multi_val_entry, mv);
6430 isl_multi_val_free(mv);
6431 return upma;
6432 error:
6433 isl_multi_val_free(mv);
6434 isl_union_pw_multi_aff_free(upma);
6435 return NULL;
6438 /* Construct and return a piecewise multi affine expression
6439 * in the given space with value zero in each of the output dimensions and
6440 * a universe domain.
6442 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6444 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6447 /* Construct and return a piecewise multi affine expression
6448 * that is equal to the given piecewise affine expression.
6450 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6451 __isl_take isl_pw_aff *pa)
6453 int i;
6454 isl_space *space;
6455 isl_pw_multi_aff *pma;
6457 if (!pa)
6458 return NULL;
6460 space = isl_pw_aff_get_space(pa);
6461 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6463 for (i = 0; i < pa->n; ++i) {
6464 isl_set *set;
6465 isl_multi_aff *ma;
6467 set = isl_set_copy(pa->p[i].set);
6468 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6469 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6472 isl_pw_aff_free(pa);
6473 return pma;
6476 /* Construct and return a piecewise multi affine expression
6477 * that is equal to the given multi piecewise affine expression
6478 * on the shared domain of the piecewise affine expressions,
6479 * in the special case of a 0D multi piecewise affine expression.
6481 * Create a piecewise multi affine expression with the explicit domain of
6482 * the 0D multi piecewise affine expression as domain.
6484 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6485 __isl_take isl_multi_pw_aff *mpa)
6487 isl_space *space;
6488 isl_set *dom;
6489 isl_multi_aff *ma;
6491 space = isl_multi_pw_aff_get_space(mpa);
6492 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6493 isl_multi_pw_aff_free(mpa);
6495 ma = isl_multi_aff_zero(space);
6496 return isl_pw_multi_aff_alloc(dom, ma);
6499 /* Construct and return a piecewise multi affine expression
6500 * that is equal to the given multi piecewise affine expression
6501 * on the shared domain of the piecewise affine expressions.
6503 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6504 __isl_take isl_multi_pw_aff *mpa)
6506 int i;
6507 isl_space *space;
6508 isl_pw_aff *pa;
6509 isl_pw_multi_aff *pma;
6511 if (!mpa)
6512 return NULL;
6514 if (mpa->n == 0)
6515 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6517 space = isl_multi_pw_aff_get_space(mpa);
6518 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6519 pma = isl_pw_multi_aff_from_pw_aff(pa);
6521 for (i = 1; i < mpa->n; ++i) {
6522 isl_pw_multi_aff *pma_i;
6524 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6525 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6526 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6529 pma = isl_pw_multi_aff_reset_space(pma, space);
6531 isl_multi_pw_aff_free(mpa);
6532 return pma;
6535 /* Construct and return a multi piecewise affine expression
6536 * that is equal to the given multi affine expression.
6538 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6539 __isl_take isl_multi_aff *ma)
6541 int i;
6542 isl_size n;
6543 isl_multi_pw_aff *mpa;
6545 n = isl_multi_aff_dim(ma, isl_dim_out);
6546 if (n < 0)
6547 ma = isl_multi_aff_free(ma);
6548 if (!ma)
6549 return NULL;
6551 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6553 for (i = 0; i < n; ++i) {
6554 isl_pw_aff *pa;
6556 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6557 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6560 isl_multi_aff_free(ma);
6561 return mpa;
6564 /* Construct and return a multi piecewise affine expression
6565 * that is equal to the given piecewise multi affine expression.
6567 * If the resulting multi piecewise affine expression has
6568 * an explicit domain, then assign it the domain of the input.
6569 * In other cases, the domain is stored in the individual elements.
6571 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6572 __isl_take isl_pw_multi_aff *pma)
6574 int i;
6575 isl_size n;
6576 isl_space *space;
6577 isl_multi_pw_aff *mpa;
6579 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6580 if (n < 0)
6581 pma = isl_pw_multi_aff_free(pma);
6582 space = isl_pw_multi_aff_get_space(pma);
6583 mpa = isl_multi_pw_aff_alloc(space);
6585 for (i = 0; i < n; ++i) {
6586 isl_pw_aff *pa;
6588 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6589 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6591 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6592 isl_set *dom;
6594 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6595 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6598 isl_pw_multi_aff_free(pma);
6599 return mpa;
6602 /* Do "pa1" and "pa2" represent the same function?
6604 * We first check if they are obviously equal.
6605 * If not, we convert them to maps and check if those are equal.
6607 * If "pa1" or "pa2" contain any NaNs, then they are considered
6608 * not to be the same. A NaN is not equal to anything, not even
6609 * to another NaN.
6611 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6612 __isl_keep isl_pw_aff *pa2)
6614 isl_bool equal;
6615 isl_bool has_nan;
6616 isl_map *map1, *map2;
6618 if (!pa1 || !pa2)
6619 return isl_bool_error;
6621 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6622 if (equal < 0 || equal)
6623 return equal;
6624 has_nan = either_involves_nan(pa1, pa2);
6625 if (has_nan < 0)
6626 return isl_bool_error;
6627 if (has_nan)
6628 return isl_bool_false;
6630 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6631 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6632 equal = isl_map_is_equal(map1, map2);
6633 isl_map_free(map1);
6634 isl_map_free(map2);
6636 return equal;
6639 /* Do "mpa1" and "mpa2" represent the same function?
6641 * Note that we cannot convert the entire isl_multi_pw_aff
6642 * to a map because the domains of the piecewise affine expressions
6643 * may not be the same.
6645 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6646 __isl_keep isl_multi_pw_aff *mpa2)
6648 int i;
6649 isl_bool equal, equal_params;
6651 if (!mpa1 || !mpa2)
6652 return isl_bool_error;
6654 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6655 if (equal_params < 0)
6656 return isl_bool_error;
6657 if (!equal_params) {
6658 if (!isl_space_has_named_params(mpa1->space))
6659 return isl_bool_false;
6660 if (!isl_space_has_named_params(mpa2->space))
6661 return isl_bool_false;
6662 mpa1 = isl_multi_pw_aff_copy(mpa1);
6663 mpa2 = isl_multi_pw_aff_copy(mpa2);
6664 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6665 isl_multi_pw_aff_get_space(mpa2));
6666 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6667 isl_multi_pw_aff_get_space(mpa1));
6668 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6669 isl_multi_pw_aff_free(mpa1);
6670 isl_multi_pw_aff_free(mpa2);
6671 return equal;
6674 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6675 if (equal < 0 || !equal)
6676 return equal;
6678 for (i = 0; i < mpa1->n; ++i) {
6679 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6680 if (equal < 0 || !equal)
6681 return equal;
6684 return isl_bool_true;
6687 /* Do "pma1" and "pma2" represent the same function?
6689 * First check if they are obviously equal.
6690 * If not, then convert them to maps and check if those are equal.
6692 * If "pa1" or "pa2" contain any NaNs, then they are considered
6693 * not to be the same. A NaN is not equal to anything, not even
6694 * to another NaN.
6696 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6697 __isl_keep isl_pw_multi_aff *pma2)
6699 isl_bool equal;
6700 isl_bool has_nan;
6701 isl_map *map1, *map2;
6703 if (!pma1 || !pma2)
6704 return isl_bool_error;
6706 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6707 if (equal < 0 || equal)
6708 return equal;
6709 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6710 if (has_nan >= 0 && !has_nan)
6711 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6712 if (has_nan < 0 || has_nan)
6713 return isl_bool_not(has_nan);
6715 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6716 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6717 equal = isl_map_is_equal(map1, map2);
6718 isl_map_free(map1);
6719 isl_map_free(map2);
6721 return equal;
6724 /* Compute the pullback of "mpa" by the function represented by "ma".
6725 * In other words, plug in "ma" in "mpa".
6727 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6729 * If "mpa" has an explicit domain, then it is this domain
6730 * that needs to undergo a pullback, i.e., a preimage.
6732 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6733 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6735 int i;
6736 isl_space *space = NULL;
6738 mpa = isl_multi_pw_aff_cow(mpa);
6739 if (!mpa || !ma)
6740 goto error;
6742 space = isl_space_join(isl_multi_aff_get_space(ma),
6743 isl_multi_pw_aff_get_space(mpa));
6744 if (!space)
6745 goto error;
6747 for (i = 0; i < mpa->n; ++i) {
6748 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6749 isl_multi_aff_copy(ma));
6750 if (!mpa->u.p[i])
6751 goto error;
6753 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6754 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6755 isl_multi_aff_copy(ma));
6756 if (!mpa->u.dom)
6757 goto error;
6760 isl_multi_aff_free(ma);
6761 isl_space_free(mpa->space);
6762 mpa->space = space;
6763 return mpa;
6764 error:
6765 isl_space_free(space);
6766 isl_multi_pw_aff_free(mpa);
6767 isl_multi_aff_free(ma);
6768 return NULL;
6771 /* Compute the pullback of "mpa" by the function represented by "ma".
6772 * In other words, plug in "ma" in "mpa".
6774 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6775 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6777 isl_bool equal_params;
6779 if (!mpa || !ma)
6780 goto error;
6781 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6782 if (equal_params < 0)
6783 goto error;
6784 if (equal_params)
6785 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6786 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6787 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6788 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6789 error:
6790 isl_multi_pw_aff_free(mpa);
6791 isl_multi_aff_free(ma);
6792 return NULL;
6795 /* Compute the pullback of "mpa" by the function represented by "pma".
6796 * In other words, plug in "pma" in "mpa".
6798 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6800 * If "mpa" has an explicit domain, then it is this domain
6801 * that needs to undergo a pullback, i.e., a preimage.
6803 static __isl_give isl_multi_pw_aff *
6804 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6805 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6807 int i;
6808 isl_space *space = NULL;
6810 mpa = isl_multi_pw_aff_cow(mpa);
6811 if (!mpa || !pma)
6812 goto error;
6814 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6815 isl_multi_pw_aff_get_space(mpa));
6817 for (i = 0; i < mpa->n; ++i) {
6818 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6819 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6820 if (!mpa->u.p[i])
6821 goto error;
6823 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6824 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6825 isl_pw_multi_aff_copy(pma));
6826 if (!mpa->u.dom)
6827 goto error;
6830 isl_pw_multi_aff_free(pma);
6831 isl_space_free(mpa->space);
6832 mpa->space = space;
6833 return mpa;
6834 error:
6835 isl_space_free(space);
6836 isl_multi_pw_aff_free(mpa);
6837 isl_pw_multi_aff_free(pma);
6838 return NULL;
6841 /* Compute the pullback of "mpa" by the function represented by "pma".
6842 * In other words, plug in "pma" in "mpa".
6844 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6845 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6847 isl_bool equal_params;
6849 if (!mpa || !pma)
6850 goto error;
6851 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6852 if (equal_params < 0)
6853 goto error;
6854 if (equal_params)
6855 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6856 mpa = isl_multi_pw_aff_align_params(mpa,
6857 isl_pw_multi_aff_get_space(pma));
6858 pma = isl_pw_multi_aff_align_params(pma,
6859 isl_multi_pw_aff_get_space(mpa));
6860 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6861 error:
6862 isl_multi_pw_aff_free(mpa);
6863 isl_pw_multi_aff_free(pma);
6864 return NULL;
6867 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6868 * with the domain of "aff". The domain of the result is the same
6869 * as that of "mpa".
6870 * "mpa" and "aff" are assumed to have been aligned.
6872 * We first extract the parametric constant from "aff", defined
6873 * over the correct domain.
6874 * Then we add the appropriate combinations of the members of "mpa".
6875 * Finally, we add the integer divisions through recursive calls.
6877 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6878 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6880 int i;
6881 isl_size n_in, n_div, n_mpa_in;
6882 isl_space *space;
6883 isl_val *v;
6884 isl_pw_aff *pa;
6885 isl_aff *tmp;
6887 n_in = isl_aff_dim(aff, isl_dim_in);
6888 n_div = isl_aff_dim(aff, isl_dim_div);
6889 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
6890 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
6891 goto error;
6893 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6894 tmp = isl_aff_copy(aff);
6895 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6896 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6897 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
6898 tmp = isl_aff_reset_domain_space(tmp, space);
6899 pa = isl_pw_aff_from_aff(tmp);
6901 for (i = 0; i < n_in; ++i) {
6902 isl_pw_aff *pa_i;
6904 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6905 continue;
6906 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6907 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6908 pa_i = isl_pw_aff_scale_val(pa_i, v);
6909 pa = isl_pw_aff_add(pa, pa_i);
6912 for (i = 0; i < n_div; ++i) {
6913 isl_aff *div;
6914 isl_pw_aff *pa_i;
6916 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6917 continue;
6918 div = isl_aff_get_div(aff, i);
6919 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6920 isl_multi_pw_aff_copy(mpa), div);
6921 pa_i = isl_pw_aff_floor(pa_i);
6922 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6923 pa_i = isl_pw_aff_scale_val(pa_i, v);
6924 pa = isl_pw_aff_add(pa, pa_i);
6927 isl_multi_pw_aff_free(mpa);
6928 isl_aff_free(aff);
6930 return pa;
6931 error:
6932 isl_multi_pw_aff_free(mpa);
6933 isl_aff_free(aff);
6934 return NULL;
6937 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6938 * with the domain of "aff". The domain of the result is the same
6939 * as that of "mpa".
6941 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6942 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6944 isl_bool equal_params;
6946 if (!aff || !mpa)
6947 goto error;
6948 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6949 if (equal_params < 0)
6950 goto error;
6951 if (equal_params)
6952 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6954 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6955 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6957 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6958 error:
6959 isl_aff_free(aff);
6960 isl_multi_pw_aff_free(mpa);
6961 return NULL;
6964 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6965 * with the domain of "pa". The domain of the result is the same
6966 * as that of "mpa".
6967 * "mpa" and "pa" are assumed to have been aligned.
6969 * We consider each piece in turn. Note that the domains of the
6970 * pieces are assumed to be disjoint and they remain disjoint
6971 * after taking the preimage (over the same function).
6973 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6974 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6976 isl_space *space;
6977 isl_pw_aff *res;
6978 int i;
6980 if (!mpa || !pa)
6981 goto error;
6983 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6984 isl_pw_aff_get_space(pa));
6985 res = isl_pw_aff_empty(space);
6987 for (i = 0; i < pa->n; ++i) {
6988 isl_pw_aff *pa_i;
6989 isl_set *domain;
6991 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6992 isl_multi_pw_aff_copy(mpa),
6993 isl_aff_copy(pa->p[i].aff));
6994 domain = isl_set_copy(pa->p[i].set);
6995 domain = isl_set_preimage_multi_pw_aff(domain,
6996 isl_multi_pw_aff_copy(mpa));
6997 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6998 res = isl_pw_aff_add_disjoint(res, pa_i);
7001 isl_pw_aff_free(pa);
7002 isl_multi_pw_aff_free(mpa);
7003 return res;
7004 error:
7005 isl_pw_aff_free(pa);
7006 isl_multi_pw_aff_free(mpa);
7007 return NULL;
7010 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7011 * with the domain of "pa". The domain of the result is the same
7012 * as that of "mpa".
7014 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7015 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7017 isl_bool equal_params;
7019 if (!pa || !mpa)
7020 goto error;
7021 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7022 if (equal_params < 0)
7023 goto error;
7024 if (equal_params)
7025 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7027 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7028 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7030 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7031 error:
7032 isl_pw_aff_free(pa);
7033 isl_multi_pw_aff_free(mpa);
7034 return NULL;
7037 /* Compute the pullback of "pa" by the function represented by "mpa".
7038 * In other words, plug in "mpa" in "pa".
7039 * "pa" and "mpa" are assumed to have been aligned.
7041 * The pullback is computed by applying "pa" to "mpa".
7043 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7044 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7046 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7049 /* Compute the pullback of "pa" by the function represented by "mpa".
7050 * In other words, plug in "mpa" in "pa".
7052 * The pullback is computed by applying "pa" to "mpa".
7054 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7055 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7057 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7060 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7061 * In other words, plug in "mpa2" in "mpa1".
7063 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7065 * We pullback each member of "mpa1" in turn.
7067 * If "mpa1" has an explicit domain, then it is this domain
7068 * that needs to undergo a pullback instead, i.e., a preimage.
7070 static __isl_give isl_multi_pw_aff *
7071 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7072 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7074 int i;
7075 isl_space *space = NULL;
7077 mpa1 = isl_multi_pw_aff_cow(mpa1);
7078 if (!mpa1 || !mpa2)
7079 goto error;
7081 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7082 isl_multi_pw_aff_get_space(mpa1));
7084 for (i = 0; i < mpa1->n; ++i) {
7085 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7086 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7087 if (!mpa1->u.p[i])
7088 goto error;
7091 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7092 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7093 isl_multi_pw_aff_copy(mpa2));
7094 if (!mpa1->u.dom)
7095 goto error;
7097 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7099 isl_multi_pw_aff_free(mpa2);
7100 return mpa1;
7101 error:
7102 isl_space_free(space);
7103 isl_multi_pw_aff_free(mpa1);
7104 isl_multi_pw_aff_free(mpa2);
7105 return NULL;
7108 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7109 * In other words, plug in "mpa2" in "mpa1".
7111 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7112 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7114 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7115 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7118 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7119 * of "mpa1" and "mpa2" live in the same space, construct map space
7120 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7121 * with this map space as extract argument.
7123 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7124 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7125 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7126 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7128 int match;
7129 isl_space *space1, *space2;
7130 isl_map *res;
7132 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7133 isl_multi_pw_aff_get_space(mpa2));
7134 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7135 isl_multi_pw_aff_get_space(mpa1));
7136 if (!mpa1 || !mpa2)
7137 goto error;
7138 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7139 mpa2->space, isl_dim_out);
7140 if (match < 0)
7141 goto error;
7142 if (!match)
7143 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7144 "range spaces don't match", goto error);
7145 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7146 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7147 space1 = isl_space_map_from_domain_and_range(space1, space2);
7149 res = order(mpa1, mpa2, space1);
7150 isl_multi_pw_aff_free(mpa1);
7151 isl_multi_pw_aff_free(mpa2);
7152 return res;
7153 error:
7154 isl_multi_pw_aff_free(mpa1);
7155 isl_multi_pw_aff_free(mpa2);
7156 return NULL;
7159 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7160 * where the function values are equal. "space" is the space of the result.
7161 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7163 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7164 * in the sequences are equal.
7166 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7167 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7168 __isl_take isl_space *space)
7170 int i;
7171 isl_size n;
7172 isl_map *res;
7174 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7175 if (n < 0)
7176 space = isl_space_free(space);
7177 res = isl_map_universe(space);
7179 for (i = 0; i < n; ++i) {
7180 isl_pw_aff *pa1, *pa2;
7181 isl_map *map;
7183 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7184 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7185 map = isl_pw_aff_eq_map(pa1, pa2);
7186 res = isl_map_intersect(res, map);
7189 return res;
7192 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7193 * where the function values are equal.
7195 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7196 __isl_take isl_multi_pw_aff *mpa2)
7198 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7199 &isl_multi_pw_aff_eq_map_on_space);
7202 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7203 * where the function values of "mpa1" is lexicographically satisfies "base"
7204 * compared to that of "mpa2". "space" is the space of the result.
7205 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7207 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7208 * if its i-th element satisfies "base" when compared to
7209 * the i-th element of "mpa2" while all previous elements are
7210 * pairwise equal.
7212 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7213 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7214 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7215 __isl_take isl_pw_aff *pa2),
7216 __isl_take isl_space *space)
7218 int i;
7219 isl_size n;
7220 isl_map *res, *rest;
7222 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7223 if (n < 0)
7224 space = isl_space_free(space);
7225 res = isl_map_empty(isl_space_copy(space));
7226 rest = isl_map_universe(space);
7228 for (i = 0; i < n; ++i) {
7229 isl_pw_aff *pa1, *pa2;
7230 isl_map *map;
7232 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7233 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7234 map = base(pa1, pa2);
7235 map = isl_map_intersect(map, isl_map_copy(rest));
7236 res = isl_map_union(res, map);
7238 if (i == n - 1)
7239 continue;
7241 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7242 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7243 map = isl_pw_aff_eq_map(pa1, pa2);
7244 rest = isl_map_intersect(rest, map);
7247 isl_map_free(rest);
7248 return res;
7251 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7252 * where the function value of "mpa1" is lexicographically less than that
7253 * of "mpa2". "space" is the space of the result.
7254 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7256 * "mpa1" is less than "mpa2" if its i-th element is smaller
7257 * than the i-th element of "mpa2" while all previous elements are
7258 * pairwise equal.
7260 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7261 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7262 __isl_take isl_space *space)
7264 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7265 &isl_pw_aff_lt_map, space);
7268 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7269 * where the function value of "mpa1" is lexicographically less than that
7270 * of "mpa2".
7272 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7273 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7275 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7276 &isl_multi_pw_aff_lex_lt_map_on_space);
7279 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7280 * where the function value of "mpa1" is lexicographically greater than that
7281 * of "mpa2". "space" is the space of the result.
7282 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7284 * "mpa1" is greater than "mpa2" if its i-th element is greater
7285 * than the i-th element of "mpa2" while all previous elements are
7286 * pairwise equal.
7288 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7289 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7290 __isl_take isl_space *space)
7292 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7293 &isl_pw_aff_gt_map, space);
7296 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7297 * where the function value of "mpa1" is lexicographically greater than that
7298 * of "mpa2".
7300 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7301 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7303 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7304 &isl_multi_pw_aff_lex_gt_map_on_space);
7307 /* Compare two isl_affs.
7309 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7310 * than "aff2" and 0 if they are equal.
7312 * The order is fairly arbitrary. We do consider expressions that only involve
7313 * earlier dimensions as "smaller".
7315 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7317 int cmp;
7318 int last1, last2;
7320 if (aff1 == aff2)
7321 return 0;
7323 if (!aff1)
7324 return -1;
7325 if (!aff2)
7326 return 1;
7328 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7329 if (cmp != 0)
7330 return cmp;
7332 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7333 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7334 if (last1 != last2)
7335 return last1 - last2;
7337 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7340 /* Compare two isl_pw_affs.
7342 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7343 * than "pa2" and 0 if they are equal.
7345 * The order is fairly arbitrary. We do consider expressions that only involve
7346 * earlier dimensions as "smaller".
7348 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7349 __isl_keep isl_pw_aff *pa2)
7351 int i;
7352 int cmp;
7354 if (pa1 == pa2)
7355 return 0;
7357 if (!pa1)
7358 return -1;
7359 if (!pa2)
7360 return 1;
7362 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7363 if (cmp != 0)
7364 return cmp;
7366 if (pa1->n != pa2->n)
7367 return pa1->n - pa2->n;
7369 for (i = 0; i < pa1->n; ++i) {
7370 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7371 if (cmp != 0)
7372 return cmp;
7373 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7374 if (cmp != 0)
7375 return cmp;
7378 return 0;
7381 /* Return a piecewise affine expression that is equal to "v" on "domain".
7383 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7384 __isl_take isl_val *v)
7386 isl_space *space;
7387 isl_local_space *ls;
7388 isl_aff *aff;
7390 space = isl_set_get_space(domain);
7391 ls = isl_local_space_from_space(space);
7392 aff = isl_aff_val_on_domain(ls, v);
7394 return isl_pw_aff_alloc(domain, aff);
7397 /* Return a multi affine expression that is equal to "mv" on domain
7398 * space "space".
7400 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7401 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7403 int i;
7404 isl_size n;
7405 isl_space *space2;
7406 isl_local_space *ls;
7407 isl_multi_aff *ma;
7409 n = isl_multi_val_dim(mv, isl_dim_set);
7410 if (!space || n < 0)
7411 goto error;
7413 space2 = isl_multi_val_get_space(mv);
7414 space2 = isl_space_align_params(space2, isl_space_copy(space));
7415 space = isl_space_align_params(space, isl_space_copy(space2));
7416 space = isl_space_map_from_domain_and_range(space, space2);
7417 ma = isl_multi_aff_alloc(isl_space_copy(space));
7418 ls = isl_local_space_from_space(isl_space_domain(space));
7419 for (i = 0; i < n; ++i) {
7420 isl_val *v;
7421 isl_aff *aff;
7423 v = isl_multi_val_get_val(mv, i);
7424 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7425 ma = isl_multi_aff_set_aff(ma, i, aff);
7427 isl_local_space_free(ls);
7429 isl_multi_val_free(mv);
7430 return ma;
7431 error:
7432 isl_space_free(space);
7433 isl_multi_val_free(mv);
7434 return NULL;
7437 /* Return a piecewise multi-affine expression
7438 * that is equal to "mv" on "domain".
7440 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7441 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7443 isl_space *space;
7444 isl_multi_aff *ma;
7446 space = isl_set_get_space(domain);
7447 ma = isl_multi_aff_multi_val_on_space(space, mv);
7449 return isl_pw_multi_aff_alloc(domain, ma);
7452 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7453 * mv is the value that should be attained on each domain set
7454 * res collects the results
7456 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7457 isl_multi_val *mv;
7458 isl_union_pw_multi_aff *res;
7461 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7462 * and add it to data->res.
7464 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7465 void *user)
7467 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7468 isl_pw_multi_aff *pma;
7469 isl_multi_val *mv;
7471 mv = isl_multi_val_copy(data->mv);
7472 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7473 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7475 return data->res ? isl_stat_ok : isl_stat_error;
7478 /* Return a union piecewise multi-affine expression
7479 * that is equal to "mv" on "domain".
7481 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7482 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7484 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7485 isl_space *space;
7487 space = isl_union_set_get_space(domain);
7488 data.res = isl_union_pw_multi_aff_empty(space);
7489 data.mv = mv;
7490 if (isl_union_set_foreach_set(domain,
7491 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7492 data.res = isl_union_pw_multi_aff_free(data.res);
7493 isl_union_set_free(domain);
7494 isl_multi_val_free(mv);
7495 return data.res;
7498 /* Compute the pullback of data->pma by the function represented by "pma2",
7499 * provided the spaces match, and add the results to data->res.
7501 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7503 struct isl_union_pw_multi_aff_bin_data *data = user;
7505 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7506 pma2->dim, isl_dim_out)) {
7507 isl_pw_multi_aff_free(pma2);
7508 return isl_stat_ok;
7511 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7512 isl_pw_multi_aff_copy(data->pma), pma2);
7514 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7515 if (!data->res)
7516 return isl_stat_error;
7518 return isl_stat_ok;
7521 /* Compute the pullback of "upma1" by the function represented by "upma2".
7523 __isl_give isl_union_pw_multi_aff *
7524 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7525 __isl_take isl_union_pw_multi_aff *upma1,
7526 __isl_take isl_union_pw_multi_aff *upma2)
7528 return bin_op(upma1, upma2, &pullback_entry);
7531 /* Check that the domain space of "upa" matches "space".
7533 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7534 * can in principle never fail since the space "space" is that
7535 * of the isl_multi_union_pw_aff and is a set space such that
7536 * there is no domain space to match.
7538 * We check the parameters and double-check that "space" is
7539 * indeed that of a set.
7541 static isl_stat isl_union_pw_aff_check_match_domain_space(
7542 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7544 isl_space *upa_space;
7545 isl_bool match;
7547 if (!upa || !space)
7548 return isl_stat_error;
7550 match = isl_space_is_set(space);
7551 if (match < 0)
7552 return isl_stat_error;
7553 if (!match)
7554 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7555 "expecting set space", return isl_stat_error);
7557 upa_space = isl_union_pw_aff_get_space(upa);
7558 match = isl_space_has_equal_params(space, upa_space);
7559 if (match < 0)
7560 goto error;
7561 if (!match)
7562 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7563 "parameters don't match", goto error);
7565 isl_space_free(upa_space);
7566 return isl_stat_ok;
7567 error:
7568 isl_space_free(upa_space);
7569 return isl_stat_error;
7572 /* Do the parameters of "upa" match those of "space"?
7574 static isl_bool isl_union_pw_aff_matching_params(
7575 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7577 isl_space *upa_space;
7578 isl_bool match;
7580 if (!upa || !space)
7581 return isl_bool_error;
7583 upa_space = isl_union_pw_aff_get_space(upa);
7585 match = isl_space_has_equal_params(space, upa_space);
7587 isl_space_free(upa_space);
7588 return match;
7591 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7592 * space represents the new parameters.
7593 * res collects the results.
7595 struct isl_union_pw_aff_reset_params_data {
7596 isl_space *space;
7597 isl_union_pw_aff *res;
7600 /* Replace the parameters of "pa" by data->space and
7601 * add the result to data->res.
7603 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7605 struct isl_union_pw_aff_reset_params_data *data = user;
7606 isl_space *space;
7608 space = isl_pw_aff_get_space(pa);
7609 space = isl_space_replace_params(space, data->space);
7610 pa = isl_pw_aff_reset_space(pa, space);
7611 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7613 return data->res ? isl_stat_ok : isl_stat_error;
7616 /* Replace the domain space of "upa" by "space".
7617 * Since a union expression does not have a (single) domain space,
7618 * "space" is necessarily a parameter space.
7620 * Since the order and the names of the parameters determine
7621 * the hash value, we need to create a new hash table.
7623 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7624 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7626 struct isl_union_pw_aff_reset_params_data data = { space };
7627 isl_bool match;
7629 match = isl_union_pw_aff_matching_params(upa, space);
7630 if (match < 0)
7631 upa = isl_union_pw_aff_free(upa);
7632 else if (match) {
7633 isl_space_free(space);
7634 return upa;
7637 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7638 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7639 data.res = isl_union_pw_aff_free(data.res);
7641 isl_union_pw_aff_free(upa);
7642 isl_space_free(space);
7643 return data.res;
7646 /* Return the floor of "pa".
7648 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7650 return isl_pw_aff_floor(pa);
7653 /* Given f, return floor(f).
7655 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7656 __isl_take isl_union_pw_aff *upa)
7658 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7661 /* Compute
7663 * upa mod m = upa - m * floor(upa/m)
7665 * with m an integer value.
7667 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7668 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7670 isl_union_pw_aff *res;
7672 if (!upa || !m)
7673 goto error;
7675 if (!isl_val_is_int(m))
7676 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7677 "expecting integer modulo", goto error);
7678 if (!isl_val_is_pos(m))
7679 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7680 "expecting positive modulo", goto error);
7682 res = isl_union_pw_aff_copy(upa);
7683 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7684 upa = isl_union_pw_aff_floor(upa);
7685 upa = isl_union_pw_aff_scale_val(upa, m);
7686 res = isl_union_pw_aff_sub(res, upa);
7688 return res;
7689 error:
7690 isl_val_free(m);
7691 isl_union_pw_aff_free(upa);
7692 return NULL;
7695 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7696 * pos is the output position that needs to be extracted.
7697 * res collects the results.
7699 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7700 int pos;
7701 isl_union_pw_aff *res;
7704 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7705 * (assuming it has such a dimension) and add it to data->res.
7707 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7709 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7710 isl_size n_out;
7711 isl_pw_aff *pa;
7713 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7714 if (n_out < 0)
7715 return isl_stat_error;
7716 if (data->pos >= n_out) {
7717 isl_pw_multi_aff_free(pma);
7718 return isl_stat_ok;
7721 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7722 isl_pw_multi_aff_free(pma);
7724 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7726 return data->res ? isl_stat_ok : isl_stat_error;
7729 /* Extract an isl_union_pw_aff corresponding to
7730 * output dimension "pos" of "upma".
7732 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7733 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7735 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7736 isl_space *space;
7738 if (!upma)
7739 return NULL;
7741 if (pos < 0)
7742 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7743 "cannot extract at negative position", return NULL);
7745 space = isl_union_pw_multi_aff_get_space(upma);
7746 data.res = isl_union_pw_aff_empty(space);
7747 data.pos = pos;
7748 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7749 &get_union_pw_aff, &data) < 0)
7750 data.res = isl_union_pw_aff_free(data.res);
7752 return data.res;
7755 /* Return a union piecewise affine expression
7756 * that is equal to "aff" on "domain".
7758 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7759 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7761 isl_pw_aff *pa;
7763 pa = isl_pw_aff_from_aff(aff);
7764 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7767 /* Return a union piecewise affine expression
7768 * that is equal to the parameter identified by "id" on "domain".
7770 * Make sure the parameter appears in the space passed to
7771 * isl_aff_param_on_domain_space_id.
7773 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7774 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7776 isl_space *space;
7777 isl_aff *aff;
7779 space = isl_union_set_get_space(domain);
7780 space = isl_space_add_param_id(space, isl_id_copy(id));
7781 aff = isl_aff_param_on_domain_space_id(space, id);
7782 return isl_union_pw_aff_aff_on_domain(domain, aff);
7785 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7786 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7787 * needs to attain.
7788 * "res" collects the results.
7790 struct isl_union_pw_aff_pw_aff_on_domain_data {
7791 isl_pw_aff *pa;
7792 isl_union_pw_aff *res;
7795 /* Construct a piecewise affine expression that is equal to data->pa
7796 * on "domain" and add the result to data->res.
7798 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7800 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7801 isl_pw_aff *pa;
7802 isl_size dim;
7804 pa = isl_pw_aff_copy(data->pa);
7805 dim = isl_set_dim(domain, isl_dim_set);
7806 if (dim < 0)
7807 pa = isl_pw_aff_free(pa);
7808 pa = isl_pw_aff_from_range(pa);
7809 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7810 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7811 pa = isl_pw_aff_intersect_domain(pa, domain);
7812 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7814 return data->res ? isl_stat_ok : isl_stat_error;
7817 /* Return a union piecewise affine expression
7818 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7819 * have been aligned.
7821 * Construct an isl_pw_aff on each of the sets in "domain" and
7822 * collect the results.
7824 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7825 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7827 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7828 isl_space *space;
7830 space = isl_union_set_get_space(domain);
7831 data.res = isl_union_pw_aff_empty(space);
7832 data.pa = pa;
7833 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7834 data.res = isl_union_pw_aff_free(data.res);
7835 isl_union_set_free(domain);
7836 isl_pw_aff_free(pa);
7837 return data.res;
7840 /* Return a union piecewise affine expression
7841 * that is equal to "pa" on "domain".
7843 * Check that "pa" is a parametric expression,
7844 * align the parameters if needed and call
7845 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7847 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7848 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7850 isl_bool is_set;
7851 isl_bool equal_params;
7852 isl_space *domain_space, *pa_space;
7854 pa_space = isl_pw_aff_peek_space(pa);
7855 is_set = isl_space_is_set(pa_space);
7856 if (is_set < 0)
7857 goto error;
7858 if (!is_set)
7859 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7860 "expecting parametric expression", goto error);
7862 domain_space = isl_union_set_get_space(domain);
7863 pa_space = isl_pw_aff_get_space(pa);
7864 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7865 if (equal_params >= 0 && !equal_params) {
7866 isl_space *space;
7868 space = isl_space_align_params(domain_space, pa_space);
7869 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7870 domain = isl_union_set_align_params(domain, space);
7871 } else {
7872 isl_space_free(domain_space);
7873 isl_space_free(pa_space);
7876 if (equal_params < 0)
7877 goto error;
7878 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7879 error:
7880 isl_union_set_free(domain);
7881 isl_pw_aff_free(pa);
7882 return NULL;
7885 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7886 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7887 * "res" collects the results.
7889 struct isl_union_pw_aff_val_on_domain_data {
7890 isl_val *v;
7891 isl_union_pw_aff *res;
7894 /* Construct a piecewise affine expression that is equal to data->v
7895 * on "domain" and add the result to data->res.
7897 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7899 struct isl_union_pw_aff_val_on_domain_data *data = user;
7900 isl_pw_aff *pa;
7901 isl_val *v;
7903 v = isl_val_copy(data->v);
7904 pa = isl_pw_aff_val_on_domain(domain, v);
7905 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7907 return data->res ? isl_stat_ok : isl_stat_error;
7910 /* Return a union piecewise affine expression
7911 * that is equal to "v" on "domain".
7913 * Construct an isl_pw_aff on each of the sets in "domain" and
7914 * collect the results.
7916 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7917 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7919 struct isl_union_pw_aff_val_on_domain_data data;
7920 isl_space *space;
7922 space = isl_union_set_get_space(domain);
7923 data.res = isl_union_pw_aff_empty(space);
7924 data.v = v;
7925 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7926 data.res = isl_union_pw_aff_free(data.res);
7927 isl_union_set_free(domain);
7928 isl_val_free(v);
7929 return data.res;
7932 /* Construct a piecewise multi affine expression
7933 * that is equal to "pa" and add it to upma.
7935 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7936 void *user)
7938 isl_union_pw_multi_aff **upma = user;
7939 isl_pw_multi_aff *pma;
7941 pma = isl_pw_multi_aff_from_pw_aff(pa);
7942 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7944 return *upma ? isl_stat_ok : isl_stat_error;
7947 /* Construct and return a union piecewise multi affine expression
7948 * that is equal to the given union piecewise affine expression.
7950 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7951 __isl_take isl_union_pw_aff *upa)
7953 isl_space *space;
7954 isl_union_pw_multi_aff *upma;
7956 if (!upa)
7957 return NULL;
7959 space = isl_union_pw_aff_get_space(upa);
7960 upma = isl_union_pw_multi_aff_empty(space);
7962 if (isl_union_pw_aff_foreach_pw_aff(upa,
7963 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7964 upma = isl_union_pw_multi_aff_free(upma);
7966 isl_union_pw_aff_free(upa);
7967 return upma;
7970 /* Compute the set of elements in the domain of "pa" where it is zero and
7971 * add this set to "uset".
7973 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7975 isl_union_set **uset = (isl_union_set **)user;
7977 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7979 return *uset ? isl_stat_ok : isl_stat_error;
7982 /* Return a union set containing those elements in the domain
7983 * of "upa" where it is zero.
7985 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7986 __isl_take isl_union_pw_aff *upa)
7988 isl_union_set *zero;
7990 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7991 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7992 zero = isl_union_set_free(zero);
7994 isl_union_pw_aff_free(upa);
7995 return zero;
7998 /* Internal data structure for isl_union_pw_aff_bind_id,
7999 * storing the parameter that needs to be bound and
8000 * the accumulated results.
8002 struct isl_bind_id_data {
8003 isl_id *id;
8004 isl_union_set *bound;
8007 /* Bind the piecewise affine function "pa" to the parameter data->id,
8008 * adding the resulting elements in the domain where the expression
8009 * is equal to the parameter to data->bound.
8011 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8013 struct isl_bind_id_data *data = user;
8014 isl_set *bound;
8016 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8017 data->bound = isl_union_set_add_set(data->bound, bound);
8019 return data->bound ? isl_stat_ok : isl_stat_error;
8022 /* Bind the union piecewise affine function "upa" to the parameter "id",
8023 * returning the elements in the domain where the expression
8024 * is equal to the parameter.
8026 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8027 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8029 struct isl_bind_id_data data = { id };
8031 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8032 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8033 data.bound = isl_union_set_free(data.bound);
8035 isl_union_pw_aff_free(upa);
8036 isl_id_free(id);
8037 return data.bound;
8040 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8041 * upma is the function that is plugged in.
8042 * pa is the current part of the function in which upma is plugged in.
8043 * res collects the results.
8045 struct isl_union_pw_aff_pullback_upma_data {
8046 isl_union_pw_multi_aff *upma;
8047 isl_pw_aff *pa;
8048 isl_union_pw_aff *res;
8051 /* Check if "pma" can be plugged into data->pa.
8052 * If so, perform the pullback and add the result to data->res.
8054 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8056 struct isl_union_pw_aff_pullback_upma_data *data = user;
8057 isl_pw_aff *pa;
8059 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8060 pma->dim, isl_dim_out)) {
8061 isl_pw_multi_aff_free(pma);
8062 return isl_stat_ok;
8065 pa = isl_pw_aff_copy(data->pa);
8066 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8068 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8070 return data->res ? isl_stat_ok : isl_stat_error;
8073 /* Check if any of the elements of data->upma can be plugged into pa,
8074 * add if so add the result to data->res.
8076 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8078 struct isl_union_pw_aff_pullback_upma_data *data = user;
8079 isl_stat r;
8081 data->pa = pa;
8082 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8083 &pa_pb_pma, data);
8084 isl_pw_aff_free(pa);
8086 return r;
8089 /* Compute the pullback of "upa" by the function represented by "upma".
8090 * In other words, plug in "upma" in "upa". The result contains
8091 * expressions defined over the domain space of "upma".
8093 * Run over all pairs of elements in "upa" and "upma", perform
8094 * the pullback when appropriate and collect the results.
8095 * If the hash value were based on the domain space rather than
8096 * the function space, then we could run through all elements
8097 * of "upma" and directly pick out the corresponding element of "upa".
8099 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8100 __isl_take isl_union_pw_aff *upa,
8101 __isl_take isl_union_pw_multi_aff *upma)
8103 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8104 isl_space *space;
8106 space = isl_union_pw_multi_aff_get_space(upma);
8107 upa = isl_union_pw_aff_align_params(upa, space);
8108 space = isl_union_pw_aff_get_space(upa);
8109 upma = isl_union_pw_multi_aff_align_params(upma, space);
8111 if (!upa || !upma)
8112 goto error;
8114 data.upma = upma;
8115 data.res = isl_union_pw_aff_alloc_same_size(upa);
8116 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8117 data.res = isl_union_pw_aff_free(data.res);
8119 isl_union_pw_aff_free(upa);
8120 isl_union_pw_multi_aff_free(upma);
8121 return data.res;
8122 error:
8123 isl_union_pw_aff_free(upa);
8124 isl_union_pw_multi_aff_free(upma);
8125 return NULL;
8128 #undef BASE
8129 #define BASE union_pw_aff
8130 #undef DOMBASE
8131 #define DOMBASE union_set
8133 #include <isl_multi_explicit_domain.c>
8134 #include <isl_multi_union_pw_aff_explicit_domain.c>
8135 #include <isl_multi_templ.c>
8136 #include <isl_multi_apply_set.c>
8137 #include <isl_multi_apply_union_set.c>
8138 #include <isl_multi_arith_templ.c>
8139 #include <isl_multi_bind_templ.c>
8140 #include <isl_multi_coalesce.c>
8141 #include <isl_multi_dim_id_templ.c>
8142 #include <isl_multi_floor.c>
8143 #include <isl_multi_from_base_templ.c>
8144 #include <isl_multi_gist.c>
8145 #include <isl_multi_align_set.c>
8146 #include <isl_multi_align_union_set.c>
8147 #include <isl_multi_intersect.c>
8148 #include <isl_multi_nan_templ.c>
8149 #include <isl_multi_tuple_id_templ.c>
8151 /* Does "mupa" have a non-trivial explicit domain?
8153 * The explicit domain, if present, is trivial if it represents
8154 * an (obviously) universe parameter set.
8156 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8157 __isl_keep isl_multi_union_pw_aff *mupa)
8159 isl_bool is_params, trivial;
8160 isl_set *set;
8162 if (!mupa)
8163 return isl_bool_error;
8164 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8165 return isl_bool_false;
8166 is_params = isl_union_set_is_params(mupa->u.dom);
8167 if (is_params < 0 || !is_params)
8168 return isl_bool_not(is_params);
8169 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8170 trivial = isl_set_plain_is_universe(set);
8171 isl_set_free(set);
8172 return isl_bool_not(trivial);
8175 /* Construct a multiple union piecewise affine expression
8176 * in the given space with value zero in each of the output dimensions.
8178 * Since there is no canonical zero value for
8179 * a union piecewise affine expression, we can only construct
8180 * a zero-dimensional "zero" value.
8182 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8183 __isl_take isl_space *space)
8185 isl_bool params;
8186 isl_size dim;
8188 if (!space)
8189 return NULL;
8191 params = isl_space_is_params(space);
8192 if (params < 0)
8193 goto error;
8194 if (params)
8195 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8196 "expecting proper set space", goto error);
8197 if (!isl_space_is_set(space))
8198 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8199 "expecting set space", goto error);
8200 dim = isl_space_dim(space, isl_dim_out);
8201 if (dim < 0)
8202 goto error;
8203 if (dim != 0)
8204 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8205 "expecting 0D space", goto error);
8207 return isl_multi_union_pw_aff_alloc(space);
8208 error:
8209 isl_space_free(space);
8210 return NULL;
8213 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8214 * with the actual sum on the shared domain and
8215 * the defined expression on the symmetric difference of the domains.
8217 * We simply iterate over the elements in both arguments and
8218 * call isl_union_pw_aff_union_add on each of them, if there is
8219 * at least one element.
8221 * Otherwise, the two expressions have an explicit domain and
8222 * the union of these explicit domains is computed.
8223 * This assumes that the explicit domains are either both in terms
8224 * of specific domains elements or both in terms of parameters.
8225 * However, if one of the expressions does not have any constraints
8226 * on its explicit domain, then this is allowed as well and the result
8227 * is the expression with no constraints on its explicit domain.
8229 static __isl_give isl_multi_union_pw_aff *
8230 isl_multi_union_pw_aff_union_add_aligned(
8231 __isl_take isl_multi_union_pw_aff *mupa1,
8232 __isl_take isl_multi_union_pw_aff *mupa2)
8234 isl_bool has_domain, is_params1, is_params2;
8236 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8237 goto error;
8238 if (mupa1->n > 0)
8239 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8240 &isl_union_pw_aff_union_add);
8241 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8242 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8243 goto error;
8245 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8246 if (has_domain < 0)
8247 goto error;
8248 if (!has_domain) {
8249 isl_multi_union_pw_aff_free(mupa2);
8250 return mupa1;
8252 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8253 if (has_domain < 0)
8254 goto error;
8255 if (!has_domain) {
8256 isl_multi_union_pw_aff_free(mupa1);
8257 return mupa2;
8260 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8261 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8262 if (is_params1 < 0 || is_params2 < 0)
8263 goto error;
8264 if (is_params1 != is_params2)
8265 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8266 isl_error_invalid,
8267 "cannot compute union of concrete domain and "
8268 "parameter constraints", goto error);
8269 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8270 if (!mupa1)
8271 goto error;
8272 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8273 isl_union_set_copy(mupa2->u.dom));
8274 if (!mupa1->u.dom)
8275 goto error;
8276 isl_multi_union_pw_aff_free(mupa2);
8277 return mupa1;
8278 error:
8279 isl_multi_union_pw_aff_free(mupa1);
8280 isl_multi_union_pw_aff_free(mupa2);
8281 return NULL;
8284 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8285 * with the actual sum on the shared domain and
8286 * the defined expression on the symmetric difference of the domains.
8288 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8289 __isl_take isl_multi_union_pw_aff *mupa1,
8290 __isl_take isl_multi_union_pw_aff *mupa2)
8292 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8293 &isl_multi_union_pw_aff_union_add_aligned);
8296 /* Construct and return a multi union piecewise affine expression
8297 * that is equal to the given multi affine expression.
8299 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8300 __isl_take isl_multi_aff *ma)
8302 isl_multi_pw_aff *mpa;
8304 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8305 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8308 /* Construct and return a multi union piecewise affine expression
8309 * that is equal to the given multi piecewise affine expression.
8311 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8312 __isl_take isl_multi_pw_aff *mpa)
8314 int i;
8315 isl_size n;
8316 isl_space *space;
8317 isl_multi_union_pw_aff *mupa;
8319 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8320 if (n < 0)
8321 mpa = isl_multi_pw_aff_free(mpa);
8322 if (!mpa)
8323 return NULL;
8325 space = isl_multi_pw_aff_get_space(mpa);
8326 space = isl_space_range(space);
8327 mupa = isl_multi_union_pw_aff_alloc(space);
8329 for (i = 0; i < n; ++i) {
8330 isl_pw_aff *pa;
8331 isl_union_pw_aff *upa;
8333 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8334 upa = isl_union_pw_aff_from_pw_aff(pa);
8335 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8338 isl_multi_pw_aff_free(mpa);
8340 return mupa;
8343 /* Extract the range space of "pma" and assign it to *space.
8344 * If *space has already been set (through a previous call to this function),
8345 * then check that the range space is the same.
8347 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8349 isl_space **space = user;
8350 isl_space *pma_space;
8351 isl_bool equal;
8353 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8354 isl_pw_multi_aff_free(pma);
8356 if (!pma_space)
8357 return isl_stat_error;
8358 if (!*space) {
8359 *space = pma_space;
8360 return isl_stat_ok;
8363 equal = isl_space_is_equal(pma_space, *space);
8364 isl_space_free(pma_space);
8366 if (equal < 0)
8367 return isl_stat_error;
8368 if (!equal)
8369 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8370 "range spaces not the same", return isl_stat_error);
8371 return isl_stat_ok;
8374 /* Construct and return a multi union piecewise affine expression
8375 * that is equal to the given union piecewise multi affine expression.
8377 * In order to be able to perform the conversion, the input
8378 * needs to be non-empty and may only involve a single range space.
8380 * If the resulting multi union piecewise affine expression has
8381 * an explicit domain, then assign it the domain of the input.
8382 * In other cases, the domain is stored in the individual elements.
8384 __isl_give isl_multi_union_pw_aff *
8385 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8386 __isl_take isl_union_pw_multi_aff *upma)
8388 isl_space *space = NULL;
8389 isl_multi_union_pw_aff *mupa;
8390 int i;
8391 isl_size n;
8393 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8394 if (n < 0)
8395 goto error;
8396 if (n == 0)
8397 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8398 "cannot extract range space from empty input",
8399 goto error);
8400 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8401 &space) < 0)
8402 goto error;
8404 if (!space)
8405 goto error;
8407 n = isl_space_dim(space, isl_dim_set);
8408 if (n < 0)
8409 space = isl_space_free(space);
8410 mupa = isl_multi_union_pw_aff_alloc(space);
8412 for (i = 0; i < n; ++i) {
8413 isl_union_pw_aff *upa;
8415 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8416 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8418 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8419 isl_union_set *dom;
8420 isl_union_pw_multi_aff *copy;
8422 copy = isl_union_pw_multi_aff_copy(upma);
8423 dom = isl_union_pw_multi_aff_domain(copy);
8424 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8427 isl_union_pw_multi_aff_free(upma);
8428 return mupa;
8429 error:
8430 isl_space_free(space);
8431 isl_union_pw_multi_aff_free(upma);
8432 return NULL;
8435 /* Try and create an isl_multi_union_pw_aff that is equivalent
8436 * to the given isl_union_map.
8437 * The isl_union_map is required to be single-valued in each space.
8438 * Moreover, it cannot be empty and all range spaces need to be the same.
8439 * Otherwise, an error is produced.
8441 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8442 __isl_take isl_union_map *umap)
8444 isl_union_pw_multi_aff *upma;
8446 upma = isl_union_pw_multi_aff_from_union_map(umap);
8447 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8450 /* Return a multiple union piecewise affine expression
8451 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8452 * have been aligned.
8454 * If the resulting multi union piecewise affine expression has
8455 * an explicit domain, then assign it the input domain.
8456 * In other cases, the domain is stored in the individual elements.
8458 static __isl_give isl_multi_union_pw_aff *
8459 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8460 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8462 int i;
8463 isl_size n;
8464 isl_space *space;
8465 isl_multi_union_pw_aff *mupa;
8467 n = isl_multi_val_dim(mv, isl_dim_set);
8468 if (!domain || n < 0)
8469 goto error;
8471 space = isl_multi_val_get_space(mv);
8472 mupa = isl_multi_union_pw_aff_alloc(space);
8473 for (i = 0; i < n; ++i) {
8474 isl_val *v;
8475 isl_union_pw_aff *upa;
8477 v = isl_multi_val_get_val(mv, i);
8478 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8480 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8482 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8483 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8484 isl_union_set_copy(domain));
8486 isl_union_set_free(domain);
8487 isl_multi_val_free(mv);
8488 return mupa;
8489 error:
8490 isl_union_set_free(domain);
8491 isl_multi_val_free(mv);
8492 return NULL;
8495 /* Return a multiple union piecewise affine expression
8496 * that is equal to "mv" on "domain".
8498 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8499 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8501 isl_bool equal_params;
8503 if (!domain || !mv)
8504 goto error;
8505 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8506 if (equal_params < 0)
8507 goto error;
8508 if (equal_params)
8509 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8510 domain, mv);
8511 domain = isl_union_set_align_params(domain,
8512 isl_multi_val_get_space(mv));
8513 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8514 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8515 error:
8516 isl_union_set_free(domain);
8517 isl_multi_val_free(mv);
8518 return NULL;
8521 /* Return a multiple union piecewise affine expression
8522 * that is equal to "ma" on "domain".
8524 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8525 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8527 isl_pw_multi_aff *pma;
8529 pma = isl_pw_multi_aff_from_multi_aff(ma);
8530 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8533 /* Return a multiple union piecewise affine expression
8534 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8535 * have been aligned.
8537 * If the resulting multi union piecewise affine expression has
8538 * an explicit domain, then assign it the input domain.
8539 * In other cases, the domain is stored in the individual elements.
8541 static __isl_give isl_multi_union_pw_aff *
8542 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8543 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8545 int i;
8546 isl_size n;
8547 isl_space *space;
8548 isl_multi_union_pw_aff *mupa;
8550 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8551 if (!domain || n < 0)
8552 goto error;
8553 space = isl_pw_multi_aff_get_space(pma);
8554 mupa = isl_multi_union_pw_aff_alloc(space);
8555 for (i = 0; i < n; ++i) {
8556 isl_pw_aff *pa;
8557 isl_union_pw_aff *upa;
8559 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8560 upa = isl_union_pw_aff_pw_aff_on_domain(
8561 isl_union_set_copy(domain), pa);
8562 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8564 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8565 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8566 isl_union_set_copy(domain));
8568 isl_union_set_free(domain);
8569 isl_pw_multi_aff_free(pma);
8570 return mupa;
8571 error:
8572 isl_union_set_free(domain);
8573 isl_pw_multi_aff_free(pma);
8574 return NULL;
8577 /* Return a multiple union piecewise affine expression
8578 * that is equal to "pma" on "domain".
8580 __isl_give isl_multi_union_pw_aff *
8581 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8582 __isl_take isl_pw_multi_aff *pma)
8584 isl_bool equal_params;
8585 isl_space *space;
8587 space = isl_pw_multi_aff_peek_space(pma);
8588 equal_params = isl_union_set_space_has_equal_params(domain, space);
8589 if (equal_params < 0)
8590 goto error;
8591 if (equal_params)
8592 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8593 domain, pma);
8594 domain = isl_union_set_align_params(domain,
8595 isl_pw_multi_aff_get_space(pma));
8596 pma = isl_pw_multi_aff_align_params(pma,
8597 isl_union_set_get_space(domain));
8598 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8599 pma);
8600 error:
8601 isl_union_set_free(domain);
8602 isl_pw_multi_aff_free(pma);
8603 return NULL;
8606 /* Return a union set containing those elements in the domains
8607 * of the elements of "mupa" where they are all zero.
8609 * If there are no elements, then simply return the entire domain.
8611 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8612 __isl_take isl_multi_union_pw_aff *mupa)
8614 int i;
8615 isl_size n;
8616 isl_union_pw_aff *upa;
8617 isl_union_set *zero;
8619 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8620 if (n < 0)
8621 mupa = isl_multi_union_pw_aff_free(mupa);
8622 if (!mupa)
8623 return NULL;
8625 if (n == 0)
8626 return isl_multi_union_pw_aff_domain(mupa);
8628 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8629 zero = isl_union_pw_aff_zero_union_set(upa);
8631 for (i = 1; i < n; ++i) {
8632 isl_union_set *zero_i;
8634 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8635 zero_i = isl_union_pw_aff_zero_union_set(upa);
8637 zero = isl_union_set_intersect(zero, zero_i);
8640 isl_multi_union_pw_aff_free(mupa);
8641 return zero;
8644 /* Construct a union map mapping the shared domain
8645 * of the union piecewise affine expressions to the range of "mupa"
8646 * in the special case of a 0D multi union piecewise affine expression.
8648 * Construct a map between the explicit domain of "mupa" and
8649 * the range space.
8650 * Note that this assumes that the domain consists of explicit elements.
8652 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8653 __isl_take isl_multi_union_pw_aff *mupa)
8655 isl_bool is_params;
8656 isl_space *space;
8657 isl_union_set *dom, *ran;
8659 space = isl_multi_union_pw_aff_get_space(mupa);
8660 dom = isl_multi_union_pw_aff_domain(mupa);
8661 ran = isl_union_set_from_set(isl_set_universe(space));
8663 is_params = isl_union_set_is_params(dom);
8664 if (is_params < 0)
8665 dom = isl_union_set_free(dom);
8666 else if (is_params)
8667 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8668 "cannot create union map from expression without "
8669 "explicit domain elements",
8670 dom = isl_union_set_free(dom));
8672 return isl_union_map_from_domain_and_range(dom, ran);
8675 /* Construct a union map mapping the shared domain
8676 * of the union piecewise affine expressions to the range of "mupa"
8677 * with each dimension in the range equated to the
8678 * corresponding union piecewise affine expression.
8680 * If the input is zero-dimensional, then construct a mapping
8681 * from its explicit domain.
8683 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8684 __isl_take isl_multi_union_pw_aff *mupa)
8686 int i;
8687 isl_size n;
8688 isl_space *space;
8689 isl_union_map *umap;
8690 isl_union_pw_aff *upa;
8692 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8693 if (n < 0)
8694 mupa = isl_multi_union_pw_aff_free(mupa);
8695 if (!mupa)
8696 return NULL;
8698 if (n == 0)
8699 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8701 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8702 umap = isl_union_map_from_union_pw_aff(upa);
8704 for (i = 1; i < n; ++i) {
8705 isl_union_map *umap_i;
8707 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8708 umap_i = isl_union_map_from_union_pw_aff(upa);
8709 umap = isl_union_map_flat_range_product(umap, umap_i);
8712 space = isl_multi_union_pw_aff_get_space(mupa);
8713 umap = isl_union_map_reset_range_space(umap, space);
8715 isl_multi_union_pw_aff_free(mupa);
8716 return umap;
8719 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8720 * "range" is the space from which to set the range space.
8721 * "res" collects the results.
8723 struct isl_union_pw_multi_aff_reset_range_space_data {
8724 isl_space *range;
8725 isl_union_pw_multi_aff *res;
8728 /* Replace the range space of "pma" by the range space of data->range and
8729 * add the result to data->res.
8731 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8733 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8734 isl_space *space;
8736 space = isl_pw_multi_aff_get_space(pma);
8737 space = isl_space_domain(space);
8738 space = isl_space_extend_domain_with_range(space,
8739 isl_space_copy(data->range));
8740 pma = isl_pw_multi_aff_reset_space(pma, space);
8741 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8743 return data->res ? isl_stat_ok : isl_stat_error;
8746 /* Replace the range space of all the piecewise affine expressions in "upma" by
8747 * the range space of "space".
8749 * This assumes that all these expressions have the same output dimension.
8751 * Since the spaces of the expressions change, so do their hash values.
8752 * We therefore need to create a new isl_union_pw_multi_aff.
8753 * Note that the hash value is currently computed based on the entire
8754 * space even though there can only be a single expression with a given
8755 * domain space.
8757 static __isl_give isl_union_pw_multi_aff *
8758 isl_union_pw_multi_aff_reset_range_space(
8759 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8761 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8762 isl_space *space_upma;
8764 space_upma = isl_union_pw_multi_aff_get_space(upma);
8765 data.res = isl_union_pw_multi_aff_empty(space_upma);
8766 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8767 &reset_range_space, &data) < 0)
8768 data.res = isl_union_pw_multi_aff_free(data.res);
8770 isl_space_free(space);
8771 isl_union_pw_multi_aff_free(upma);
8772 return data.res;
8775 /* Construct and return a union piecewise multi affine expression
8776 * that is equal to the given multi union piecewise affine expression,
8777 * in the special case of a 0D multi union piecewise affine expression.
8779 * Construct a union piecewise multi affine expression
8780 * on top of the explicit domain of the input.
8782 __isl_give isl_union_pw_multi_aff *
8783 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8784 __isl_take isl_multi_union_pw_aff *mupa)
8786 isl_space *space;
8787 isl_multi_val *mv;
8788 isl_union_set *domain;
8790 space = isl_multi_union_pw_aff_get_space(mupa);
8791 mv = isl_multi_val_zero(space);
8792 domain = isl_multi_union_pw_aff_domain(mupa);
8793 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8796 /* Construct and return a union piecewise multi affine expression
8797 * that is equal to the given multi union piecewise affine expression.
8799 * If the input is zero-dimensional, then
8800 * construct a union piecewise multi affine expression
8801 * on top of the explicit domain of the input.
8803 __isl_give isl_union_pw_multi_aff *
8804 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8805 __isl_take isl_multi_union_pw_aff *mupa)
8807 int i;
8808 isl_size n;
8809 isl_space *space;
8810 isl_union_pw_multi_aff *upma;
8811 isl_union_pw_aff *upa;
8813 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8814 if (n < 0)
8815 mupa = isl_multi_union_pw_aff_free(mupa);
8816 if (!mupa)
8817 return NULL;
8819 if (n == 0)
8820 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8822 space = isl_multi_union_pw_aff_get_space(mupa);
8823 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8824 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8826 for (i = 1; i < n; ++i) {
8827 isl_union_pw_multi_aff *upma_i;
8829 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8830 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8831 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8834 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8836 isl_multi_union_pw_aff_free(mupa);
8837 return upma;
8840 /* Intersect the range of "mupa" with "range",
8841 * in the special case where "mupa" is 0D.
8843 * Intersect the domain of "mupa" with the constraints on the parameters
8844 * of "range".
8846 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8847 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8849 range = isl_set_params(range);
8850 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8851 return mupa;
8854 /* Intersect the range of "mupa" with "range".
8855 * That is, keep only those domain elements that have a function value
8856 * in "range".
8858 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8859 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8861 isl_union_pw_multi_aff *upma;
8862 isl_union_set *domain;
8863 isl_space *space;
8864 isl_size n;
8865 int match;
8867 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8868 if (n < 0 || !range)
8869 goto error;
8871 space = isl_set_get_space(range);
8872 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8873 space, isl_dim_set);
8874 isl_space_free(space);
8875 if (match < 0)
8876 goto error;
8877 if (!match)
8878 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8879 "space don't match", goto error);
8880 if (n == 0)
8881 return mupa_intersect_range_0D(mupa, range);
8883 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8884 isl_multi_union_pw_aff_copy(mupa));
8885 domain = isl_union_set_from_set(range);
8886 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8887 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8889 return mupa;
8890 error:
8891 isl_multi_union_pw_aff_free(mupa);
8892 isl_set_free(range);
8893 return NULL;
8896 /* Return the shared domain of the elements of "mupa",
8897 * in the special case where "mupa" is zero-dimensional.
8899 * Return the explicit domain of "mupa".
8900 * Note that this domain may be a parameter set, either
8901 * because "mupa" is meant to live in a set space or
8902 * because no explicit domain has been set.
8904 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8905 __isl_take isl_multi_union_pw_aff *mupa)
8907 isl_union_set *dom;
8909 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8910 isl_multi_union_pw_aff_free(mupa);
8912 return dom;
8915 /* Return the shared domain of the elements of "mupa".
8917 * If "mupa" is zero-dimensional, then return its explicit domain.
8919 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8920 __isl_take isl_multi_union_pw_aff *mupa)
8922 int i;
8923 isl_size n;
8924 isl_union_pw_aff *upa;
8925 isl_union_set *dom;
8927 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8928 if (n < 0)
8929 mupa = isl_multi_union_pw_aff_free(mupa);
8930 if (!mupa)
8931 return NULL;
8933 if (n == 0)
8934 return isl_multi_union_pw_aff_domain_0D(mupa);
8936 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8937 dom = isl_union_pw_aff_domain(upa);
8938 for (i = 1; i < n; ++i) {
8939 isl_union_set *dom_i;
8941 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8942 dom_i = isl_union_pw_aff_domain(upa);
8943 dom = isl_union_set_intersect(dom, dom_i);
8946 isl_multi_union_pw_aff_free(mupa);
8947 return dom;
8950 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8951 * In particular, the spaces have been aligned.
8952 * The result is defined over the shared domain of the elements of "mupa"
8954 * We first extract the parametric constant part of "aff" and
8955 * define that over the shared domain.
8956 * Then we iterate over all input dimensions of "aff" and add the corresponding
8957 * multiples of the elements of "mupa".
8958 * Finally, we consider the integer divisions, calling the function
8959 * recursively to obtain an isl_union_pw_aff corresponding to the
8960 * integer division argument.
8962 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8963 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8965 int i;
8966 isl_size n_in, n_div;
8967 isl_union_pw_aff *upa;
8968 isl_union_set *uset;
8969 isl_val *v;
8970 isl_aff *cst;
8972 n_in = isl_aff_dim(aff, isl_dim_in);
8973 n_div = isl_aff_dim(aff, isl_dim_div);
8974 if (n_in < 0 || n_div < 0)
8975 goto error;
8977 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8978 cst = isl_aff_copy(aff);
8979 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8980 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8981 cst = isl_aff_project_domain_on_params(cst);
8982 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8984 for (i = 0; i < n_in; ++i) {
8985 isl_union_pw_aff *upa_i;
8987 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8988 continue;
8989 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8990 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8991 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8992 upa = isl_union_pw_aff_add(upa, upa_i);
8995 for (i = 0; i < n_div; ++i) {
8996 isl_aff *div;
8997 isl_union_pw_aff *upa_i;
8999 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9000 continue;
9001 div = isl_aff_get_div(aff, i);
9002 upa_i = multi_union_pw_aff_apply_aff(
9003 isl_multi_union_pw_aff_copy(mupa), div);
9004 upa_i = isl_union_pw_aff_floor(upa_i);
9005 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9006 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9007 upa = isl_union_pw_aff_add(upa, upa_i);
9010 isl_multi_union_pw_aff_free(mupa);
9011 isl_aff_free(aff);
9013 return upa;
9014 error:
9015 isl_multi_union_pw_aff_free(mupa);
9016 isl_aff_free(aff);
9017 return NULL;
9020 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9021 * with the domain of "aff".
9022 * Furthermore, the dimension of this space needs to be greater than zero.
9023 * The result is defined over the shared domain of the elements of "mupa"
9025 * We perform these checks and then hand over control to
9026 * multi_union_pw_aff_apply_aff.
9028 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9029 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9031 isl_size dim;
9032 isl_space *space1, *space2;
9033 isl_bool equal;
9035 mupa = isl_multi_union_pw_aff_align_params(mupa,
9036 isl_aff_get_space(aff));
9037 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9038 if (!mupa || !aff)
9039 goto error;
9041 space1 = isl_multi_union_pw_aff_get_space(mupa);
9042 space2 = isl_aff_get_domain_space(aff);
9043 equal = isl_space_is_equal(space1, space2);
9044 isl_space_free(space1);
9045 isl_space_free(space2);
9046 if (equal < 0)
9047 goto error;
9048 if (!equal)
9049 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9050 "spaces don't match", goto error);
9051 dim = isl_aff_dim(aff, isl_dim_in);
9052 if (dim < 0)
9053 goto error;
9054 if (dim == 0)
9055 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9056 "cannot determine domains", goto error);
9058 return multi_union_pw_aff_apply_aff(mupa, aff);
9059 error:
9060 isl_multi_union_pw_aff_free(mupa);
9061 isl_aff_free(aff);
9062 return NULL;
9065 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9066 * The space of "mupa" is known to be compatible with the domain of "ma".
9068 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9069 * on the domain of "mupa".
9071 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9072 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9074 isl_union_set *dom;
9076 dom = isl_multi_union_pw_aff_domain(mupa);
9077 ma = isl_multi_aff_project_domain_on_params(ma);
9079 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9082 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9083 * with the domain of "ma".
9084 * The result is defined over the shared domain of the elements of "mupa"
9086 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9087 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9089 isl_space *space1, *space2;
9090 isl_multi_union_pw_aff *res;
9091 isl_bool equal;
9092 int i;
9093 isl_size n_in, n_out;
9095 mupa = isl_multi_union_pw_aff_align_params(mupa,
9096 isl_multi_aff_get_space(ma));
9097 ma = isl_multi_aff_align_params(ma,
9098 isl_multi_union_pw_aff_get_space(mupa));
9099 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9100 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9101 if (!mupa || n_in < 0 || n_out < 0)
9102 goto error;
9104 space1 = isl_multi_union_pw_aff_get_space(mupa);
9105 space2 = isl_multi_aff_get_domain_space(ma);
9106 equal = isl_space_is_equal(space1, space2);
9107 isl_space_free(space1);
9108 isl_space_free(space2);
9109 if (equal < 0)
9110 goto error;
9111 if (!equal)
9112 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9113 "spaces don't match", goto error);
9114 if (n_in == 0)
9115 return mupa_apply_multi_aff_0D(mupa, ma);
9117 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9118 res = isl_multi_union_pw_aff_alloc(space1);
9120 for (i = 0; i < n_out; ++i) {
9121 isl_aff *aff;
9122 isl_union_pw_aff *upa;
9124 aff = isl_multi_aff_get_aff(ma, i);
9125 upa = multi_union_pw_aff_apply_aff(
9126 isl_multi_union_pw_aff_copy(mupa), aff);
9127 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9130 isl_multi_aff_free(ma);
9131 isl_multi_union_pw_aff_free(mupa);
9132 return res;
9133 error:
9134 isl_multi_union_pw_aff_free(mupa);
9135 isl_multi_aff_free(ma);
9136 return NULL;
9139 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9140 * The space of "mupa" is known to be compatible with the domain of "pa".
9142 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9143 * on the domain of "mupa".
9145 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9146 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9148 isl_union_set *dom;
9150 dom = isl_multi_union_pw_aff_domain(mupa);
9151 pa = isl_pw_aff_project_domain_on_params(pa);
9153 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9156 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9157 * with the domain of "pa".
9158 * Furthermore, the dimension of this space needs to be greater than zero.
9159 * The result is defined over the shared domain of the elements of "mupa"
9161 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9162 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9164 int i;
9165 isl_bool equal;
9166 isl_size n_in;
9167 isl_space *space, *space2;
9168 isl_union_pw_aff *upa;
9170 mupa = isl_multi_union_pw_aff_align_params(mupa,
9171 isl_pw_aff_get_space(pa));
9172 pa = isl_pw_aff_align_params(pa,
9173 isl_multi_union_pw_aff_get_space(mupa));
9174 if (!mupa || !pa)
9175 goto error;
9177 space = isl_multi_union_pw_aff_get_space(mupa);
9178 space2 = isl_pw_aff_get_domain_space(pa);
9179 equal = isl_space_is_equal(space, space2);
9180 isl_space_free(space);
9181 isl_space_free(space2);
9182 if (equal < 0)
9183 goto error;
9184 if (!equal)
9185 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9186 "spaces don't match", goto error);
9187 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9188 if (n_in < 0)
9189 goto error;
9190 if (n_in == 0)
9191 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9193 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9194 upa = isl_union_pw_aff_empty(space);
9196 for (i = 0; i < pa->n; ++i) {
9197 isl_aff *aff;
9198 isl_set *domain;
9199 isl_multi_union_pw_aff *mupa_i;
9200 isl_union_pw_aff *upa_i;
9202 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9203 domain = isl_set_copy(pa->p[i].set);
9204 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9205 aff = isl_aff_copy(pa->p[i].aff);
9206 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9207 upa = isl_union_pw_aff_union_add(upa, upa_i);
9210 isl_multi_union_pw_aff_free(mupa);
9211 isl_pw_aff_free(pa);
9212 return upa;
9213 error:
9214 isl_multi_union_pw_aff_free(mupa);
9215 isl_pw_aff_free(pa);
9216 return NULL;
9219 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9220 * The space of "mupa" is known to be compatible with the domain of "pma".
9222 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9223 * on the domain of "mupa".
9225 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9226 __isl_take isl_multi_union_pw_aff *mupa,
9227 __isl_take isl_pw_multi_aff *pma)
9229 isl_union_set *dom;
9231 dom = isl_multi_union_pw_aff_domain(mupa);
9232 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9234 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9237 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9238 * with the domain of "pma".
9239 * The result is defined over the shared domain of the elements of "mupa"
9241 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9242 __isl_take isl_multi_union_pw_aff *mupa,
9243 __isl_take isl_pw_multi_aff *pma)
9245 isl_space *space1, *space2;
9246 isl_multi_union_pw_aff *res;
9247 isl_bool equal;
9248 int i;
9249 isl_size n_in, n_out;
9251 mupa = isl_multi_union_pw_aff_align_params(mupa,
9252 isl_pw_multi_aff_get_space(pma));
9253 pma = isl_pw_multi_aff_align_params(pma,
9254 isl_multi_union_pw_aff_get_space(mupa));
9255 if (!mupa || !pma)
9256 goto error;
9258 space1 = isl_multi_union_pw_aff_get_space(mupa);
9259 space2 = isl_pw_multi_aff_get_domain_space(pma);
9260 equal = isl_space_is_equal(space1, space2);
9261 isl_space_free(space1);
9262 isl_space_free(space2);
9263 if (equal < 0)
9264 goto error;
9265 if (!equal)
9266 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9267 "spaces don't match", goto error);
9268 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9269 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9270 if (n_in < 0 || n_out < 0)
9271 goto error;
9272 if (n_in == 0)
9273 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9275 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9276 res = isl_multi_union_pw_aff_alloc(space1);
9278 for (i = 0; i < n_out; ++i) {
9279 isl_pw_aff *pa;
9280 isl_union_pw_aff *upa;
9282 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9283 upa = isl_multi_union_pw_aff_apply_pw_aff(
9284 isl_multi_union_pw_aff_copy(mupa), pa);
9285 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9288 isl_pw_multi_aff_free(pma);
9289 isl_multi_union_pw_aff_free(mupa);
9290 return res;
9291 error:
9292 isl_multi_union_pw_aff_free(mupa);
9293 isl_pw_multi_aff_free(pma);
9294 return NULL;
9297 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9298 * If the explicit domain only keeps track of constraints on the parameters,
9299 * then only update those constraints.
9301 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9302 __isl_take isl_multi_union_pw_aff *mupa,
9303 __isl_keep isl_union_pw_multi_aff *upma)
9305 isl_bool is_params;
9307 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9308 return isl_multi_union_pw_aff_free(mupa);
9310 mupa = isl_multi_union_pw_aff_cow(mupa);
9311 if (!mupa)
9312 return NULL;
9314 is_params = isl_union_set_is_params(mupa->u.dom);
9315 if (is_params < 0)
9316 return isl_multi_union_pw_aff_free(mupa);
9318 upma = isl_union_pw_multi_aff_copy(upma);
9319 if (is_params)
9320 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9321 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9322 else
9323 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9324 mupa->u.dom, upma);
9325 if (!mupa->u.dom)
9326 return isl_multi_union_pw_aff_free(mupa);
9327 return mupa;
9330 /* Compute the pullback of "mupa" by the function represented by "upma".
9331 * In other words, plug in "upma" in "mupa". The result contains
9332 * expressions defined over the domain space of "upma".
9334 * Run over all elements of "mupa" and plug in "upma" in each of them.
9336 * If "mupa" has an explicit domain, then it is this domain
9337 * that needs to undergo a pullback instead, i.e., a preimage.
9339 __isl_give isl_multi_union_pw_aff *
9340 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9341 __isl_take isl_multi_union_pw_aff *mupa,
9342 __isl_take isl_union_pw_multi_aff *upma)
9344 int i;
9345 isl_size n;
9347 mupa = isl_multi_union_pw_aff_align_params(mupa,
9348 isl_union_pw_multi_aff_get_space(upma));
9349 upma = isl_union_pw_multi_aff_align_params(upma,
9350 isl_multi_union_pw_aff_get_space(mupa));
9351 mupa = isl_multi_union_pw_aff_cow(mupa);
9352 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9353 if (n < 0 || !upma)
9354 goto error;
9356 for (i = 0; i < n; ++i) {
9357 isl_union_pw_aff *upa;
9359 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9360 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9361 isl_union_pw_multi_aff_copy(upma));
9362 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9365 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9366 mupa = preimage_explicit_domain(mupa, upma);
9368 isl_union_pw_multi_aff_free(upma);
9369 return mupa;
9370 error:
9371 isl_multi_union_pw_aff_free(mupa);
9372 isl_union_pw_multi_aff_free(upma);
9373 return NULL;
9376 /* Extract the sequence of elements in "mupa" with domain space "space"
9377 * (ignoring parameters).
9379 * For the elements of "mupa" that are not defined on the specified space,
9380 * the corresponding element in the result is empty.
9382 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9383 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9385 int i;
9386 isl_size n;
9387 isl_space *space_mpa;
9388 isl_multi_pw_aff *mpa;
9390 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9391 if (n < 0 || !space)
9392 goto error;
9394 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9395 space = isl_space_replace_params(space, space_mpa);
9396 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9397 space_mpa);
9398 mpa = isl_multi_pw_aff_alloc(space_mpa);
9400 space = isl_space_from_domain(space);
9401 space = isl_space_add_dims(space, isl_dim_out, 1);
9402 for (i = 0; i < n; ++i) {
9403 isl_union_pw_aff *upa;
9404 isl_pw_aff *pa;
9406 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9407 pa = isl_union_pw_aff_extract_pw_aff(upa,
9408 isl_space_copy(space));
9409 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9410 isl_union_pw_aff_free(upa);
9413 isl_space_free(space);
9414 return mpa;
9415 error:
9416 isl_space_free(space);
9417 return NULL;
9420 /* Evaluate the affine function "aff" in the void point "pnt".
9421 * In particular, return the value NaN.
9423 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9424 __isl_take isl_point *pnt)
9426 isl_ctx *ctx;
9428 ctx = isl_point_get_ctx(pnt);
9429 isl_aff_free(aff);
9430 isl_point_free(pnt);
9431 return isl_val_nan(ctx);
9434 /* Evaluate the affine expression "aff"
9435 * in the coordinates (with denominator) "pnt".
9437 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9438 __isl_keep isl_vec *pnt)
9440 isl_int n, d;
9441 isl_ctx *ctx;
9442 isl_val *v;
9444 if (!aff || !pnt)
9445 return NULL;
9447 ctx = isl_vec_get_ctx(aff);
9448 isl_int_init(n);
9449 isl_int_init(d);
9450 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9451 isl_int_mul(d, aff->el[0], pnt->el[0]);
9452 v = isl_val_rat_from_isl_int(ctx, n, d);
9453 v = isl_val_normalize(v);
9454 isl_int_clear(n);
9455 isl_int_clear(d);
9457 return v;
9460 /* Check that the domain space of "aff" is equal to "space".
9462 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9463 __isl_keep isl_space *space)
9465 isl_bool ok;
9467 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9468 if (ok < 0)
9469 return isl_stat_error;
9470 if (!ok)
9471 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9472 "incompatible spaces", return isl_stat_error);
9473 return isl_stat_ok;
9476 /* Evaluate the affine function "aff" in "pnt".
9478 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9479 __isl_take isl_point *pnt)
9481 isl_bool is_void;
9482 isl_val *v;
9483 isl_local_space *ls;
9485 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9486 goto error;
9487 is_void = isl_point_is_void(pnt);
9488 if (is_void < 0)
9489 goto error;
9490 if (is_void)
9491 return eval_void(aff, pnt);
9493 ls = isl_aff_get_domain_local_space(aff);
9494 pnt = isl_local_space_lift_point(ls, pnt);
9496 v = eval(aff->v, isl_point_peek_vec(pnt));
9498 isl_aff_free(aff);
9499 isl_point_free(pnt);
9501 return v;
9502 error:
9503 isl_aff_free(aff);
9504 isl_point_free(pnt);
9505 return NULL;