export isl_space_is_wrapping
[isl.git] / isl_aff.c
blob4f25259ccf1d334b9629cf33fa92d6880d1e62a2
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 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2460 enum isl_dim_type type, unsigned first, unsigned n)
2462 isl_ctx *ctx;
2464 if (!aff)
2465 return NULL;
2466 if (type == isl_dim_out)
2467 isl_die(aff->v->ctx, isl_error_invalid,
2468 "cannot drop output/set dimension",
2469 return isl_aff_free(aff));
2470 if (type == isl_dim_in)
2471 type = isl_dim_set;
2472 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2473 return aff;
2475 ctx = isl_aff_get_ctx(aff);
2476 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2477 return isl_aff_free(aff);
2479 aff = isl_aff_cow(aff);
2480 if (!aff)
2481 return NULL;
2483 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2484 if (!aff->ls)
2485 return isl_aff_free(aff);
2487 first += 1 + isl_local_space_offset(aff->ls, type);
2488 aff->v = isl_vec_drop_els(aff->v, first, n);
2489 if (!aff->v)
2490 return isl_aff_free(aff);
2492 return aff;
2495 /* Is the domain of "aff" a product?
2497 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2499 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2502 #undef TYPE
2503 #define TYPE isl_aff
2504 #include <isl_domain_factor_templ.c>
2506 /* Project the domain of the affine expression onto its parameter space.
2507 * The affine expression may not involve any of the domain dimensions.
2509 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2511 isl_space *space;
2512 isl_size n;
2514 n = isl_aff_dim(aff, isl_dim_in);
2515 if (n < 0)
2516 return isl_aff_free(aff);
2517 aff = isl_aff_drop_domain(aff, 0, n);
2518 space = isl_aff_get_domain_space(aff);
2519 space = isl_space_params(space);
2520 aff = isl_aff_reset_domain_space(aff, space);
2521 return aff;
2524 /* Convert an affine expression defined over a parameter domain
2525 * into one that is defined over a zero-dimensional set.
2527 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2529 isl_local_space *ls;
2531 ls = isl_aff_take_domain_local_space(aff);
2532 ls = isl_local_space_set_from_params(ls);
2533 aff = isl_aff_restore_domain_local_space(aff, ls);
2535 return aff;
2538 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2539 enum isl_dim_type type, unsigned first, unsigned n)
2541 isl_ctx *ctx;
2543 if (!aff)
2544 return NULL;
2545 if (type == isl_dim_out)
2546 isl_die(aff->v->ctx, isl_error_invalid,
2547 "cannot insert output/set dimensions",
2548 return isl_aff_free(aff));
2549 if (type == isl_dim_in)
2550 type = isl_dim_set;
2551 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2552 return aff;
2554 ctx = isl_aff_get_ctx(aff);
2555 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2556 return isl_aff_free(aff);
2558 aff = isl_aff_cow(aff);
2559 if (!aff)
2560 return NULL;
2562 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2563 if (!aff->ls)
2564 return isl_aff_free(aff);
2566 first += 1 + isl_local_space_offset(aff->ls, type);
2567 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2568 if (!aff->v)
2569 return isl_aff_free(aff);
2571 return aff;
2574 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2575 enum isl_dim_type type, unsigned n)
2577 isl_size pos;
2579 pos = isl_aff_dim(aff, type);
2580 if (pos < 0)
2581 return isl_aff_free(aff);
2583 return isl_aff_insert_dims(aff, type, pos, n);
2586 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2587 enum isl_dim_type type, unsigned n)
2589 isl_size pos;
2591 pos = isl_pw_aff_dim(pwaff, type);
2592 if (pos < 0)
2593 return isl_pw_aff_free(pwaff);
2595 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2598 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2599 * to dimensions of "dst_type" at "dst_pos".
2601 * We only support moving input dimensions to parameters and vice versa.
2603 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2604 enum isl_dim_type dst_type, unsigned dst_pos,
2605 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2607 unsigned g_dst_pos;
2608 unsigned g_src_pos;
2610 if (!aff)
2611 return NULL;
2612 if (n == 0 &&
2613 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2614 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2615 return aff;
2617 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2618 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2619 "cannot move output/set dimension",
2620 return isl_aff_free(aff));
2621 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2622 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2623 "cannot move divs", return isl_aff_free(aff));
2624 if (dst_type == isl_dim_in)
2625 dst_type = isl_dim_set;
2626 if (src_type == isl_dim_in)
2627 src_type = isl_dim_set;
2629 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2630 return isl_aff_free(aff);
2631 if (dst_type == src_type)
2632 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2633 "moving dims within the same type not supported",
2634 return isl_aff_free(aff));
2636 aff = isl_aff_cow(aff);
2637 if (!aff)
2638 return NULL;
2640 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2641 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2642 if (dst_type > src_type)
2643 g_dst_pos -= n;
2645 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2646 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2647 src_type, src_pos, n);
2648 if (!aff->v || !aff->ls)
2649 return isl_aff_free(aff);
2651 aff = sort_divs(aff);
2653 return aff;
2656 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2658 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2659 return isl_pw_aff_alloc(dom, aff);
2662 #define isl_aff_involves_nan isl_aff_is_nan
2664 #undef PW
2665 #define PW isl_pw_aff
2666 #undef EL
2667 #define EL isl_aff
2668 #undef EL_IS_ZERO
2669 #define EL_IS_ZERO is_empty
2670 #undef ZERO
2671 #define ZERO empty
2672 #undef IS_ZERO
2673 #define IS_ZERO is_empty
2674 #undef FIELD
2675 #define FIELD aff
2676 #undef DEFAULT_IS_ZERO
2677 #define DEFAULT_IS_ZERO 0
2679 #define NO_OPT
2680 #define NO_LIFT
2681 #define NO_MORPH
2683 #include <isl_pw_templ.c>
2684 #include <isl_pw_bind_domain_templ.c>
2685 #include <isl_pw_eval.c>
2686 #include <isl_pw_hash.c>
2687 #include <isl_pw_union_opt.c>
2689 #undef BASE
2690 #define BASE pw_aff
2692 #include <isl_union_single.c>
2693 #include <isl_union_neg.c>
2695 static __isl_give isl_set *align_params_pw_pw_set_and(
2696 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2697 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2698 __isl_take isl_pw_aff *pwaff2))
2700 isl_bool equal_params;
2702 if (!pwaff1 || !pwaff2)
2703 goto error;
2704 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2705 if (equal_params < 0)
2706 goto error;
2707 if (equal_params)
2708 return fn(pwaff1, pwaff2);
2709 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2710 isl_pw_aff_check_named_params(pwaff2) < 0)
2711 goto error;
2712 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2713 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2714 return fn(pwaff1, pwaff2);
2715 error:
2716 isl_pw_aff_free(pwaff1);
2717 isl_pw_aff_free(pwaff2);
2718 return NULL;
2721 /* Align the parameters of the to isl_pw_aff arguments and
2722 * then apply a function "fn" on them that returns an isl_map.
2724 static __isl_give isl_map *align_params_pw_pw_map_and(
2725 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2726 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2727 __isl_take isl_pw_aff *pa2))
2729 isl_bool equal_params;
2731 if (!pa1 || !pa2)
2732 goto error;
2733 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2734 if (equal_params < 0)
2735 goto error;
2736 if (equal_params)
2737 return fn(pa1, pa2);
2738 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2739 isl_pw_aff_check_named_params(pa2) < 0)
2740 goto error;
2741 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2742 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2743 return fn(pa1, pa2);
2744 error:
2745 isl_pw_aff_free(pa1);
2746 isl_pw_aff_free(pa2);
2747 return NULL;
2750 /* Compute a piecewise quasi-affine expression with a domain that
2751 * is the union of those of pwaff1 and pwaff2 and such that on each
2752 * cell, the quasi-affine expression is the maximum of those of pwaff1
2753 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2754 * cell, then the associated expression is the defined one.
2756 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2757 __isl_take isl_pw_aff *pwaff2)
2759 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2762 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2763 __isl_take isl_pw_aff *pwaff2)
2765 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2766 &pw_aff_union_max);
2769 /* Compute a piecewise quasi-affine expression with a domain that
2770 * is the union of those of pwaff1 and pwaff2 and such that on each
2771 * cell, the quasi-affine expression is the minimum of those of pwaff1
2772 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2773 * cell, then the associated expression is the defined one.
2775 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2776 __isl_take isl_pw_aff *pwaff2)
2778 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2781 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2782 __isl_take isl_pw_aff *pwaff2)
2784 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2785 &pw_aff_union_min);
2788 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2789 __isl_take isl_pw_aff *pwaff2, int max)
2791 if (max)
2792 return isl_pw_aff_union_max(pwaff1, pwaff2);
2793 else
2794 return isl_pw_aff_union_min(pwaff1, pwaff2);
2797 /* Is the domain of "pa" a product?
2799 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2801 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2804 #undef TYPE
2805 #define TYPE isl_pw_aff
2806 #include <isl_domain_factor_templ.c>
2808 /* Return a set containing those elements in the domain
2809 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2810 * does not satisfy "fn" (if complement is 1).
2812 * The pieces with a NaN never belong to the result since
2813 * NaN does not satisfy any property.
2815 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2816 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2817 void *user),
2818 int complement, void *user)
2820 int i;
2821 isl_set *set;
2823 if (!pwaff)
2824 return NULL;
2826 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2828 for (i = 0; i < pwaff->n; ++i) {
2829 isl_basic_set *bset;
2830 isl_set *set_i, *locus;
2831 isl_bool rational;
2833 if (isl_aff_is_nan(pwaff->p[i].aff))
2834 continue;
2836 rational = isl_set_has_rational(pwaff->p[i].set);
2837 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2838 locus = isl_set_from_basic_set(bset);
2839 set_i = isl_set_copy(pwaff->p[i].set);
2840 if (complement)
2841 set_i = isl_set_subtract(set_i, locus);
2842 else
2843 set_i = isl_set_intersect(set_i, locus);
2844 set = isl_set_union_disjoint(set, set_i);
2847 isl_pw_aff_free(pwaff);
2849 return set;
2852 /* Return a set containing those elements in the domain
2853 * of "pa" where it is positive.
2855 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2857 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2860 /* Return a set containing those elements in the domain
2861 * of pwaff where it is non-negative.
2863 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2865 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2868 /* Return a set containing those elements in the domain
2869 * of pwaff where it is zero.
2871 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2873 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2876 /* Return a set containing those elements in the domain
2877 * of pwaff where it is not zero.
2879 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2881 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2884 /* Bind the affine function "aff" to the parameter "id",
2885 * returning the elements in the domain where the affine expression
2886 * is equal to the parameter.
2888 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2889 __isl_take isl_id *id)
2891 isl_space *space;
2892 isl_aff *aff_id;
2894 space = isl_aff_get_domain_space(aff);
2895 space = isl_space_add_param_id(space, isl_id_copy(id));
2897 aff = isl_aff_align_params(aff, isl_space_copy(space));
2898 aff_id = isl_aff_param_on_domain_space_id(space, id);
2900 return isl_aff_eq_basic_set(aff, aff_id);
2903 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2904 * "rational" should not be set.
2906 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2907 int rational, void *user)
2909 isl_id *id = user;
2911 if (!aff)
2912 return NULL;
2913 if (rational)
2914 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2915 "rational binding not supported", goto error);
2916 return isl_aff_bind_id(aff, isl_id_copy(id));
2917 error:
2918 isl_aff_free(aff);
2919 return NULL;
2922 /* Bind the piecewise affine function "pa" to the parameter "id",
2923 * returning the elements in the domain where the expression
2924 * is equal to the parameter.
2926 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2927 __isl_take isl_id *id)
2929 isl_set *bound;
2931 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2932 isl_id_free(id);
2934 return bound;
2937 /* Return a set containing those elements in the shared domain
2938 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2940 * We compute the difference on the shared domain and then construct
2941 * the set of values where this difference is non-negative.
2942 * If strict is set, we first subtract 1 from the difference.
2943 * If equal is set, we only return the elements where pwaff1 and pwaff2
2944 * are equal.
2946 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2947 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2949 isl_set *set1, *set2;
2951 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2952 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2953 set1 = isl_set_intersect(set1, set2);
2954 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2955 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2956 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2958 if (strict) {
2959 isl_space *space = isl_set_get_space(set1);
2960 isl_aff *aff;
2961 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2962 aff = isl_aff_add_constant_si(aff, -1);
2963 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2964 } else
2965 isl_set_free(set1);
2967 if (equal)
2968 return isl_pw_aff_zero_set(pwaff1);
2969 return isl_pw_aff_nonneg_set(pwaff1);
2972 /* Return a set containing those elements in the shared domain
2973 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2975 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2976 __isl_take isl_pw_aff *pwaff2)
2978 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2981 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2982 __isl_take isl_pw_aff *pwaff2)
2984 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2987 /* Return a set containing those elements in the shared domain
2988 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2990 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2991 __isl_take isl_pw_aff *pwaff2)
2993 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2996 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2997 __isl_take isl_pw_aff *pwaff2)
2999 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
3002 /* Return a set containing those elements in the shared domain
3003 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3005 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3006 __isl_take isl_pw_aff *pwaff2)
3008 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3011 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3012 __isl_take isl_pw_aff *pwaff2)
3014 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
3017 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3018 __isl_take isl_pw_aff *pwaff2)
3020 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3023 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3024 __isl_take isl_pw_aff *pwaff2)
3026 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3029 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3030 * where the function values are ordered in the same way as "order",
3031 * which returns a set in the shared domain of its two arguments.
3032 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3034 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3035 * We first pull back the two functions such that they are defined on
3036 * the domain [A -> B]. Then we apply "order", resulting in a set
3037 * in the space [A -> B]. Finally, we unwrap this set to obtain
3038 * a map in the space A -> B.
3040 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3041 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3042 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3043 __isl_take isl_pw_aff *pa2))
3045 isl_space *space1, *space2;
3046 isl_multi_aff *ma;
3047 isl_set *set;
3049 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3050 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3051 space1 = isl_space_map_from_domain_and_range(space1, space2);
3052 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3053 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3054 ma = isl_multi_aff_range_map(space1);
3055 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3056 set = order(pa1, pa2);
3058 return isl_set_unwrap(set);
3061 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3062 * where the function values are equal.
3063 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3065 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3066 __isl_take isl_pw_aff *pa2)
3068 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3071 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3072 * where the function values are equal.
3074 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3075 __isl_take isl_pw_aff *pa2)
3077 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3080 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3081 * where the function value of "pa1" is less than the function value of "pa2".
3082 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3084 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3085 __isl_take isl_pw_aff *pa2)
3087 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3090 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3091 * where the function value of "pa1" is less than the function value of "pa2".
3093 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3094 __isl_take isl_pw_aff *pa2)
3096 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3099 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3100 * where the function value of "pa1" is greater than the function value
3101 * of "pa2".
3102 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3104 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3105 __isl_take isl_pw_aff *pa2)
3107 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3110 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3111 * where the function value of "pa1" is greater than the function value
3112 * of "pa2".
3114 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3115 __isl_take isl_pw_aff *pa2)
3117 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3120 /* Return a set containing those elements in the shared domain
3121 * of the elements of list1 and list2 where each element in list1
3122 * has the relation specified by "fn" with each element in list2.
3124 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3125 __isl_take isl_pw_aff_list *list2,
3126 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3127 __isl_take isl_pw_aff *pwaff2))
3129 int i, j;
3130 isl_ctx *ctx;
3131 isl_set *set;
3133 if (!list1 || !list2)
3134 goto error;
3136 ctx = isl_pw_aff_list_get_ctx(list1);
3137 if (list1->n < 1 || list2->n < 1)
3138 isl_die(ctx, isl_error_invalid,
3139 "list should contain at least one element", goto error);
3141 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3142 for (i = 0; i < list1->n; ++i)
3143 for (j = 0; j < list2->n; ++j) {
3144 isl_set *set_ij;
3146 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3147 isl_pw_aff_copy(list2->p[j]));
3148 set = isl_set_intersect(set, set_ij);
3151 isl_pw_aff_list_free(list1);
3152 isl_pw_aff_list_free(list2);
3153 return set;
3154 error:
3155 isl_pw_aff_list_free(list1);
3156 isl_pw_aff_list_free(list2);
3157 return NULL;
3160 /* Return a set containing those elements in the shared domain
3161 * of the elements of list1 and list2 where each element in list1
3162 * is equal to each element in list2.
3164 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3165 __isl_take isl_pw_aff_list *list2)
3167 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3170 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3171 __isl_take isl_pw_aff_list *list2)
3173 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3176 /* Return a set containing those elements in the shared domain
3177 * of the elements of list1 and list2 where each element in list1
3178 * is less than or equal to each element in list2.
3180 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3181 __isl_take isl_pw_aff_list *list2)
3183 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3186 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3187 __isl_take isl_pw_aff_list *list2)
3189 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3192 __isl_give isl_set *isl_pw_aff_list_ge_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_ge_set);
3198 __isl_give isl_set *isl_pw_aff_list_gt_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_gt_set);
3205 /* Return a set containing those elements in the shared domain
3206 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3208 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3209 __isl_take isl_pw_aff *pwaff2)
3211 isl_set *set_lt, *set_gt;
3213 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3214 isl_pw_aff_copy(pwaff2));
3215 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3216 return isl_set_union_disjoint(set_lt, set_gt);
3219 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3220 __isl_take isl_pw_aff *pwaff2)
3222 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3225 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3226 isl_int v)
3228 int i;
3230 if (isl_int_is_one(v))
3231 return pwaff;
3232 if (!isl_int_is_pos(v))
3233 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3234 "factor needs to be positive",
3235 return isl_pw_aff_free(pwaff));
3236 pwaff = isl_pw_aff_cow(pwaff);
3237 if (!pwaff)
3238 return NULL;
3239 if (pwaff->n == 0)
3240 return pwaff;
3242 for (i = 0; i < pwaff->n; ++i) {
3243 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3244 if (!pwaff->p[i].aff)
3245 return isl_pw_aff_free(pwaff);
3248 return pwaff;
3251 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3253 int i;
3255 pwaff = isl_pw_aff_cow(pwaff);
3256 if (!pwaff)
3257 return NULL;
3258 if (pwaff->n == 0)
3259 return pwaff;
3261 for (i = 0; i < pwaff->n; ++i) {
3262 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3263 if (!pwaff->p[i].aff)
3264 return isl_pw_aff_free(pwaff);
3267 return pwaff;
3270 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3272 int i;
3274 pwaff = isl_pw_aff_cow(pwaff);
3275 if (!pwaff)
3276 return NULL;
3277 if (pwaff->n == 0)
3278 return pwaff;
3280 for (i = 0; i < pwaff->n; ++i) {
3281 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3282 if (!pwaff->p[i].aff)
3283 return isl_pw_aff_free(pwaff);
3286 return pwaff;
3289 /* Assuming that "cond1" and "cond2" are disjoint,
3290 * return an affine expression that is equal to pwaff1 on cond1
3291 * and to pwaff2 on cond2.
3293 static __isl_give isl_pw_aff *isl_pw_aff_select(
3294 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3295 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3297 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3298 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3300 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3303 /* Return an affine expression that is equal to pwaff_true for elements
3304 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3305 * is zero.
3306 * That is, return cond ? pwaff_true : pwaff_false;
3308 * If "cond" involves and NaN, then we conservatively return a NaN
3309 * on its entire domain. In principle, we could consider the pieces
3310 * where it is NaN separately from those where it is not.
3312 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3313 * then only use the domain of "cond" to restrict the domain.
3315 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3316 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3318 isl_set *cond_true, *cond_false;
3319 isl_bool equal;
3321 if (!cond)
3322 goto error;
3323 if (isl_pw_aff_involves_nan(cond)) {
3324 isl_space *space = isl_pw_aff_get_domain_space(cond);
3325 isl_local_space *ls = isl_local_space_from_space(space);
3326 isl_pw_aff_free(cond);
3327 isl_pw_aff_free(pwaff_true);
3328 isl_pw_aff_free(pwaff_false);
3329 return isl_pw_aff_nan_on_domain(ls);
3332 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3333 isl_pw_aff_get_space(pwaff_false));
3334 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3335 isl_pw_aff_get_space(pwaff_true));
3336 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3337 if (equal < 0)
3338 goto error;
3339 if (equal) {
3340 isl_set *dom;
3342 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3343 isl_pw_aff_free(pwaff_false);
3344 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3347 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3348 cond_false = isl_pw_aff_zero_set(cond);
3349 return isl_pw_aff_select(cond_true, pwaff_true,
3350 cond_false, pwaff_false);
3351 error:
3352 isl_pw_aff_free(cond);
3353 isl_pw_aff_free(pwaff_true);
3354 isl_pw_aff_free(pwaff_false);
3355 return NULL;
3358 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3360 int pos;
3362 if (!aff)
3363 return isl_bool_error;
3365 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3366 return isl_bool_ok(pos == -1);
3369 /* Check whether pwaff is a piecewise constant.
3371 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3373 int i;
3375 if (!pwaff)
3376 return isl_bool_error;
3378 for (i = 0; i < pwaff->n; ++i) {
3379 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3380 if (is_cst < 0 || !is_cst)
3381 return is_cst;
3384 return isl_bool_true;
3387 /* Are all elements of "mpa" piecewise constants?
3389 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3391 int i;
3393 if (!mpa)
3394 return isl_bool_error;
3396 for (i = 0; i < mpa->n; ++i) {
3397 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3398 if (is_cst < 0 || !is_cst)
3399 return is_cst;
3402 return isl_bool_true;
3405 /* Return the product of "aff1" and "aff2".
3407 * If either of the two is NaN, then the result is NaN.
3409 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3411 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3412 __isl_take isl_aff *aff2)
3414 if (!aff1 || !aff2)
3415 goto error;
3417 if (isl_aff_is_nan(aff1)) {
3418 isl_aff_free(aff2);
3419 return aff1;
3421 if (isl_aff_is_nan(aff2)) {
3422 isl_aff_free(aff1);
3423 return aff2;
3426 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3427 return isl_aff_mul(aff2, aff1);
3429 if (!isl_aff_is_cst(aff2))
3430 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3431 "at least one affine expression should be constant",
3432 goto error);
3434 aff1 = isl_aff_cow(aff1);
3435 if (!aff1 || !aff2)
3436 goto error;
3438 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3439 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3441 isl_aff_free(aff2);
3442 return aff1;
3443 error:
3444 isl_aff_free(aff1);
3445 isl_aff_free(aff2);
3446 return NULL;
3449 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3451 * If either of the two is NaN, then the result is NaN.
3453 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3454 __isl_take isl_aff *aff2)
3456 int is_cst;
3457 int neg;
3459 if (!aff1 || !aff2)
3460 goto error;
3462 if (isl_aff_is_nan(aff1)) {
3463 isl_aff_free(aff2);
3464 return aff1;
3466 if (isl_aff_is_nan(aff2)) {
3467 isl_aff_free(aff1);
3468 return aff2;
3471 is_cst = isl_aff_is_cst(aff2);
3472 if (is_cst < 0)
3473 goto error;
3474 if (!is_cst)
3475 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3476 "second argument should be a constant", goto error);
3478 if (!aff2)
3479 goto error;
3481 neg = isl_int_is_neg(aff2->v->el[1]);
3482 if (neg) {
3483 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3484 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3487 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3488 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3490 if (neg) {
3491 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3492 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3495 isl_aff_free(aff2);
3496 return aff1;
3497 error:
3498 isl_aff_free(aff1);
3499 isl_aff_free(aff2);
3500 return NULL;
3503 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3504 __isl_take isl_pw_aff *pwaff2)
3506 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3509 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3510 __isl_take isl_pw_aff *pwaff2)
3512 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3515 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3516 __isl_take isl_pw_aff *pwaff2)
3518 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3521 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3522 __isl_take isl_pw_aff *pwaff2)
3524 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3527 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3528 __isl_take isl_pw_aff *pwaff2)
3530 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3533 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3534 __isl_take isl_pw_aff *pa2)
3536 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3539 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3541 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3542 __isl_take isl_pw_aff *pa2)
3544 int is_cst;
3546 is_cst = isl_pw_aff_is_cst(pa2);
3547 if (is_cst < 0)
3548 goto error;
3549 if (!is_cst)
3550 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3551 "second argument should be a piecewise constant",
3552 goto error);
3553 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3554 error:
3555 isl_pw_aff_free(pa1);
3556 isl_pw_aff_free(pa2);
3557 return NULL;
3560 /* Compute the quotient of the integer division of "pa1" by "pa2"
3561 * with rounding towards zero.
3562 * "pa2" is assumed to be a piecewise constant.
3564 * In particular, return
3566 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3569 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3570 __isl_take isl_pw_aff *pa2)
3572 int is_cst;
3573 isl_set *cond;
3574 isl_pw_aff *f, *c;
3576 is_cst = isl_pw_aff_is_cst(pa2);
3577 if (is_cst < 0)
3578 goto error;
3579 if (!is_cst)
3580 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3581 "second argument should be a piecewise constant",
3582 goto error);
3584 pa1 = isl_pw_aff_div(pa1, pa2);
3586 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3587 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3588 c = isl_pw_aff_ceil(pa1);
3589 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3590 error:
3591 isl_pw_aff_free(pa1);
3592 isl_pw_aff_free(pa2);
3593 return NULL;
3596 /* Compute the remainder of the integer division of "pa1" by "pa2"
3597 * with rounding towards zero.
3598 * "pa2" is assumed to be a piecewise constant.
3600 * In particular, return
3602 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3605 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3606 __isl_take isl_pw_aff *pa2)
3608 int is_cst;
3609 isl_pw_aff *res;
3611 is_cst = isl_pw_aff_is_cst(pa2);
3612 if (is_cst < 0)
3613 goto error;
3614 if (!is_cst)
3615 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3616 "second argument should be a piecewise constant",
3617 goto error);
3618 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3619 res = isl_pw_aff_mul(pa2, res);
3620 res = isl_pw_aff_sub(pa1, res);
3621 return res;
3622 error:
3623 isl_pw_aff_free(pa1);
3624 isl_pw_aff_free(pa2);
3625 return NULL;
3628 /* Does either of "pa1" or "pa2" involve any NaN2?
3630 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3631 __isl_keep isl_pw_aff *pa2)
3633 isl_bool has_nan;
3635 has_nan = isl_pw_aff_involves_nan(pa1);
3636 if (has_nan < 0 || has_nan)
3637 return has_nan;
3638 return isl_pw_aff_involves_nan(pa2);
3641 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3642 * by a NaN on their shared domain.
3644 * In principle, the result could be refined to only being NaN
3645 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3647 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3648 __isl_take isl_pw_aff *pa2)
3650 isl_local_space *ls;
3651 isl_set *dom;
3652 isl_pw_aff *pa;
3654 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3655 ls = isl_local_space_from_space(isl_set_get_space(dom));
3656 pa = isl_pw_aff_nan_on_domain(ls);
3657 pa = isl_pw_aff_intersect_domain(pa, dom);
3659 return pa;
3662 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3663 __isl_take isl_pw_aff *pwaff2)
3665 isl_set *le;
3666 isl_set *dom;
3668 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3669 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3670 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3671 isl_pw_aff_copy(pwaff2));
3672 dom = isl_set_subtract(dom, isl_set_copy(le));
3673 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3676 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3677 __isl_take isl_pw_aff *pwaff2)
3679 isl_set *ge;
3680 isl_set *dom;
3682 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3683 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3684 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3685 isl_pw_aff_copy(pwaff2));
3686 dom = isl_set_subtract(dom, isl_set_copy(ge));
3687 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3690 /* Return an expression for the minimum (if "max" is not set) or
3691 * the maximum (if "max" is set) of "pa1" and "pa2".
3692 * If either expression involves any NaN, then return a NaN
3693 * on the shared domain as result.
3695 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3696 __isl_take isl_pw_aff *pa2, int max)
3698 isl_bool has_nan;
3700 has_nan = either_involves_nan(pa1, pa2);
3701 if (has_nan < 0)
3702 pa1 = isl_pw_aff_free(pa1);
3703 else if (has_nan)
3704 return replace_by_nan(pa1, pa2);
3706 if (max)
3707 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3708 else
3709 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3712 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3714 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3715 __isl_take isl_pw_aff *pwaff2)
3717 return pw_aff_min_max(pwaff1, pwaff2, 0);
3720 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3722 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3723 __isl_take isl_pw_aff *pwaff2)
3725 return pw_aff_min_max(pwaff1, pwaff2, 1);
3728 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3729 __isl_take isl_pw_aff_list *list,
3730 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3731 __isl_take isl_pw_aff *pwaff2))
3733 int i;
3734 isl_ctx *ctx;
3735 isl_pw_aff *res;
3737 if (!list)
3738 return NULL;
3740 ctx = isl_pw_aff_list_get_ctx(list);
3741 if (list->n < 1)
3742 isl_die(ctx, isl_error_invalid,
3743 "list should contain at least one element", goto error);
3745 res = isl_pw_aff_copy(list->p[0]);
3746 for (i = 1; i < list->n; ++i)
3747 res = fn(res, isl_pw_aff_copy(list->p[i]));
3749 isl_pw_aff_list_free(list);
3750 return res;
3751 error:
3752 isl_pw_aff_list_free(list);
3753 return NULL;
3756 /* Return an isl_pw_aff that maps each element in the intersection of the
3757 * domains of the elements of list to the minimal corresponding affine
3758 * expression.
3760 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3762 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3765 /* Return an isl_pw_aff that maps each element in the intersection of the
3766 * domains of the elements of list to the maximal corresponding affine
3767 * expression.
3769 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3771 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3774 /* Mark the domains of "pwaff" as rational.
3776 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3778 int i;
3780 pwaff = isl_pw_aff_cow(pwaff);
3781 if (!pwaff)
3782 return NULL;
3783 if (pwaff->n == 0)
3784 return pwaff;
3786 for (i = 0; i < pwaff->n; ++i) {
3787 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3788 if (!pwaff->p[i].set)
3789 return isl_pw_aff_free(pwaff);
3792 return pwaff;
3795 /* Mark the domains of the elements of "list" as rational.
3797 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3798 __isl_take isl_pw_aff_list *list)
3800 int i, n;
3802 if (!list)
3803 return NULL;
3804 if (list->n == 0)
3805 return list;
3807 n = list->n;
3808 for (i = 0; i < n; ++i) {
3809 isl_pw_aff *pa;
3811 pa = isl_pw_aff_list_get_pw_aff(list, i);
3812 pa = isl_pw_aff_set_rational(pa);
3813 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3816 return list;
3819 /* Do the parameters of "aff" match those of "space"?
3821 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3822 __isl_keep isl_space *space)
3824 isl_space *aff_space;
3825 isl_bool match;
3827 if (!aff || !space)
3828 return isl_bool_error;
3830 aff_space = isl_aff_get_domain_space(aff);
3832 match = isl_space_has_equal_params(space, aff_space);
3834 isl_space_free(aff_space);
3835 return match;
3838 /* Check that the domain space of "aff" matches "space".
3840 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3841 __isl_keep isl_space *space)
3843 isl_space *aff_space;
3844 isl_bool match;
3846 if (!aff || !space)
3847 return isl_stat_error;
3849 aff_space = isl_aff_get_domain_space(aff);
3851 match = isl_space_has_equal_params(space, aff_space);
3852 if (match < 0)
3853 goto error;
3854 if (!match)
3855 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3856 "parameters don't match", goto error);
3857 match = isl_space_tuple_is_equal(space, isl_dim_in,
3858 aff_space, isl_dim_set);
3859 if (match < 0)
3860 goto error;
3861 if (!match)
3862 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3863 "domains don't match", goto error);
3864 isl_space_free(aff_space);
3865 return isl_stat_ok;
3866 error:
3867 isl_space_free(aff_space);
3868 return isl_stat_error;
3871 /* Return the shared (universe) domain of the elements of "ma".
3873 * Since an isl_multi_aff (and an isl_aff) is always total,
3874 * the domain is always the universe set in its domain space.
3875 * This is a helper function for use in the generic isl_multi_*_bind.
3877 static __isl_give isl_basic_set *isl_multi_aff_domain(
3878 __isl_take isl_multi_aff *ma)
3880 isl_space *space;
3882 space = isl_multi_aff_get_space(ma);
3883 isl_multi_aff_free(ma);
3885 return isl_basic_set_universe(isl_space_domain(space));
3888 #undef BASE
3889 #define BASE aff
3891 #include <isl_multi_no_explicit_domain.c>
3892 #include <isl_multi_templ.c>
3893 #include <isl_multi_apply_set.c>
3894 #include <isl_multi_arith_templ.c>
3895 #include <isl_multi_bind_domain_templ.c>
3896 #include <isl_multi_cmp.c>
3897 #include <isl_multi_dim_id_templ.c>
3898 #include <isl_multi_dims.c>
3899 #include <isl_multi_floor.c>
3900 #include <isl_multi_from_base_templ.c>
3901 #include <isl_multi_identity_templ.c>
3902 #include <isl_multi_move_dims_templ.c>
3903 #include <isl_multi_nan_templ.c>
3904 #include <isl_multi_product_templ.c>
3905 #include <isl_multi_splice_templ.c>
3906 #include <isl_multi_tuple_id_templ.c>
3907 #include <isl_multi_zero_templ.c>
3909 #undef DOMBASE
3910 #define DOMBASE set
3911 #include <isl_multi_gist.c>
3913 #undef DOMBASE
3914 #define DOMBASE basic_set
3915 #include <isl_multi_bind_templ.c>
3917 /* Construct an isl_multi_aff living in "space" that corresponds
3918 * to the affine transformation matrix "mat".
3920 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3921 __isl_take isl_space *space, __isl_take isl_mat *mat)
3923 isl_ctx *ctx;
3924 isl_local_space *ls = NULL;
3925 isl_multi_aff *ma = NULL;
3926 isl_size n_row, n_col, n_out, total;
3927 int i;
3929 if (!space || !mat)
3930 goto error;
3932 ctx = isl_mat_get_ctx(mat);
3934 n_row = isl_mat_rows(mat);
3935 n_col = isl_mat_cols(mat);
3936 n_out = isl_space_dim(space, isl_dim_out);
3937 total = isl_space_dim(space, isl_dim_all);
3938 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3939 goto error;
3940 if (n_row < 1)
3941 isl_die(ctx, isl_error_invalid,
3942 "insufficient number of rows", goto error);
3943 if (n_col < 1)
3944 isl_die(ctx, isl_error_invalid,
3945 "insufficient number of columns", goto error);
3946 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3947 isl_die(ctx, isl_error_invalid,
3948 "dimension mismatch", goto error);
3950 ma = isl_multi_aff_zero(isl_space_copy(space));
3951 ls = isl_local_space_from_space(isl_space_domain(space));
3953 for (i = 0; i < n_row - 1; ++i) {
3954 isl_vec *v;
3955 isl_aff *aff;
3957 v = isl_vec_alloc(ctx, 1 + n_col);
3958 if (!v)
3959 goto error;
3960 isl_int_set(v->el[0], mat->row[0][0]);
3961 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3962 v = isl_vec_normalize(v);
3963 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3964 ma = isl_multi_aff_set_aff(ma, i, aff);
3967 isl_local_space_free(ls);
3968 isl_mat_free(mat);
3969 return ma;
3970 error:
3971 isl_local_space_free(ls);
3972 isl_mat_free(mat);
3973 isl_multi_aff_free(ma);
3974 return NULL;
3977 /* Remove any internal structure of the domain of "ma".
3978 * If there is any such internal structure in the input,
3979 * then the name of the corresponding space is also removed.
3981 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3982 __isl_take isl_multi_aff *ma)
3984 isl_space *space;
3986 if (!ma)
3987 return NULL;
3989 if (!ma->space->nested[0])
3990 return ma;
3992 space = isl_multi_aff_get_space(ma);
3993 space = isl_space_flatten_domain(space);
3994 ma = isl_multi_aff_reset_space(ma, space);
3996 return ma;
3999 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4000 * of the space to its domain.
4002 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4004 int i;
4005 isl_size n_in;
4006 isl_local_space *ls;
4007 isl_multi_aff *ma;
4009 if (!space)
4010 return NULL;
4011 if (!isl_space_is_map(space))
4012 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4013 "not a map space", goto error);
4015 n_in = isl_space_dim(space, isl_dim_in);
4016 if (n_in < 0)
4017 goto error;
4018 space = isl_space_domain_map(space);
4020 ma = isl_multi_aff_alloc(isl_space_copy(space));
4021 if (n_in == 0) {
4022 isl_space_free(space);
4023 return ma;
4026 space = isl_space_domain(space);
4027 ls = isl_local_space_from_space(space);
4028 for (i = 0; i < n_in; ++i) {
4029 isl_aff *aff;
4031 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4032 isl_dim_set, i);
4033 ma = isl_multi_aff_set_aff(ma, i, aff);
4035 isl_local_space_free(ls);
4036 return ma;
4037 error:
4038 isl_space_free(space);
4039 return NULL;
4042 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4043 * of the space to its range.
4045 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4047 int i;
4048 isl_size n_in, n_out;
4049 isl_local_space *ls;
4050 isl_multi_aff *ma;
4052 if (!space)
4053 return NULL;
4054 if (!isl_space_is_map(space))
4055 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4056 "not a map space", goto error);
4058 n_in = isl_space_dim(space, isl_dim_in);
4059 n_out = isl_space_dim(space, isl_dim_out);
4060 if (n_in < 0 || n_out < 0)
4061 goto error;
4062 space = isl_space_range_map(space);
4064 ma = isl_multi_aff_alloc(isl_space_copy(space));
4065 if (n_out == 0) {
4066 isl_space_free(space);
4067 return ma;
4070 space = isl_space_domain(space);
4071 ls = isl_local_space_from_space(space);
4072 for (i = 0; i < n_out; ++i) {
4073 isl_aff *aff;
4075 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4076 isl_dim_set, n_in + i);
4077 ma = isl_multi_aff_set_aff(ma, i, aff);
4079 isl_local_space_free(ls);
4080 return ma;
4081 error:
4082 isl_space_free(space);
4083 return NULL;
4086 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4087 * of the space to its range.
4089 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4090 __isl_take isl_space *space)
4092 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4095 /* Given the space of a set and a range of set dimensions,
4096 * construct an isl_multi_aff that projects out those dimensions.
4098 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4099 __isl_take isl_space *space, enum isl_dim_type type,
4100 unsigned first, unsigned n)
4102 int i;
4103 isl_size dim;
4104 isl_local_space *ls;
4105 isl_multi_aff *ma;
4107 if (!space)
4108 return NULL;
4109 if (!isl_space_is_set(space))
4110 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4111 "expecting set space", goto error);
4112 if (type != isl_dim_set)
4113 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4114 "only set dimensions can be projected out", goto error);
4115 if (isl_space_check_range(space, type, first, n) < 0)
4116 goto error;
4118 dim = isl_space_dim(space, isl_dim_set);
4119 if (dim < 0)
4120 goto error;
4122 space = isl_space_from_domain(space);
4123 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4125 if (dim == n)
4126 return isl_multi_aff_alloc(space);
4128 ma = isl_multi_aff_alloc(isl_space_copy(space));
4129 space = isl_space_domain(space);
4130 ls = isl_local_space_from_space(space);
4132 for (i = 0; i < first; ++i) {
4133 isl_aff *aff;
4135 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4136 isl_dim_set, i);
4137 ma = isl_multi_aff_set_aff(ma, i, aff);
4140 for (i = 0; i < dim - (first + n); ++i) {
4141 isl_aff *aff;
4143 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4144 isl_dim_set, first + n + i);
4145 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4148 isl_local_space_free(ls);
4149 return ma;
4150 error:
4151 isl_space_free(space);
4152 return NULL;
4155 /* Given the space of a set and a range of set dimensions,
4156 * construct an isl_pw_multi_aff that projects out those dimensions.
4158 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4159 __isl_take isl_space *space, enum isl_dim_type type,
4160 unsigned first, unsigned n)
4162 isl_multi_aff *ma;
4164 ma = isl_multi_aff_project_out_map(space, type, first, n);
4165 return isl_pw_multi_aff_from_multi_aff(ma);
4168 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4169 * domain.
4171 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4172 __isl_take isl_multi_aff *ma)
4174 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4175 return isl_pw_multi_aff_alloc(dom, ma);
4178 /* Create a piecewise multi-affine expression in the given space that maps each
4179 * input dimension to the corresponding output dimension.
4181 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4182 __isl_take isl_space *space)
4184 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4187 /* Exploit the equalities in "eq" to simplify the affine expressions.
4189 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4190 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4192 int i;
4194 maff = isl_multi_aff_cow(maff);
4195 if (!maff || !eq)
4196 goto error;
4198 for (i = 0; i < maff->n; ++i) {
4199 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4200 isl_basic_set_copy(eq));
4201 if (!maff->u.p[i])
4202 goto error;
4205 isl_basic_set_free(eq);
4206 return maff;
4207 error:
4208 isl_basic_set_free(eq);
4209 isl_multi_aff_free(maff);
4210 return NULL;
4213 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4214 isl_int f)
4216 int i;
4218 maff = isl_multi_aff_cow(maff);
4219 if (!maff)
4220 return NULL;
4222 for (i = 0; i < maff->n; ++i) {
4223 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4224 if (!maff->u.p[i])
4225 return isl_multi_aff_free(maff);
4228 return maff;
4231 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4232 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4234 maff1 = isl_multi_aff_add(maff1, maff2);
4235 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4236 return maff1;
4239 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4241 if (!maff)
4242 return -1;
4244 return 0;
4247 /* Return the set of domain elements where "ma1" is lexicographically
4248 * smaller than or equal to "ma2".
4250 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4251 __isl_take isl_multi_aff *ma2)
4253 return isl_multi_aff_lex_ge_set(ma2, ma1);
4256 /* Return the set of domain elements where "ma1" is lexicographically
4257 * smaller than "ma2".
4259 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4260 __isl_take isl_multi_aff *ma2)
4262 return isl_multi_aff_lex_gt_set(ma2, ma1);
4265 /* Return the set of domain elements where "ma1" and "ma2"
4266 * satisfy "order".
4268 static __isl_give isl_set *isl_multi_aff_order_set(
4269 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4270 __isl_give isl_map *order(__isl_take isl_space *set_space))
4272 isl_space *space;
4273 isl_map *map1, *map2;
4274 isl_map *map, *ge;
4276 map1 = isl_map_from_multi_aff_internal(ma1);
4277 map2 = isl_map_from_multi_aff_internal(ma2);
4278 map = isl_map_range_product(map1, map2);
4279 space = isl_space_range(isl_map_get_space(map));
4280 space = isl_space_domain(isl_space_unwrap(space));
4281 ge = order(space);
4282 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4284 return isl_map_domain(map);
4287 /* Return the set of domain elements where "ma1" is lexicographically
4288 * greater than or equal to "ma2".
4290 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4291 __isl_take isl_multi_aff *ma2)
4293 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4296 /* Return the set of domain elements where "ma1" is lexicographically
4297 * greater than "ma2".
4299 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4300 __isl_take isl_multi_aff *ma2)
4302 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4305 #undef PW
4306 #define PW isl_pw_multi_aff
4307 #undef EL
4308 #define EL isl_multi_aff
4309 #undef EL_IS_ZERO
4310 #define EL_IS_ZERO is_empty
4311 #undef ZERO
4312 #define ZERO empty
4313 #undef IS_ZERO
4314 #define IS_ZERO is_empty
4315 #undef FIELD
4316 #define FIELD maff
4317 #undef DEFAULT_IS_ZERO
4318 #define DEFAULT_IS_ZERO 0
4320 #define NO_SUB
4321 #define NO_OPT
4322 #define NO_INSERT_DIMS
4323 #define NO_LIFT
4324 #define NO_MORPH
4326 #include <isl_pw_templ.c>
4327 #include <isl_pw_bind_domain_templ.c>
4328 #include <isl_pw_union_opt.c>
4330 #undef NO_SUB
4332 #undef BASE
4333 #define BASE pw_multi_aff
4335 #include <isl_union_multi.c>
4336 #include <isl_union_neg.c>
4338 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4339 __isl_take isl_pw_multi_aff *pma1,
4340 __isl_take isl_pw_multi_aff *pma2)
4342 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4343 &isl_multi_aff_lex_ge_set);
4346 /* Given two piecewise multi affine expressions, return a piecewise
4347 * multi-affine expression defined on the union of the definition domains
4348 * of the inputs that is equal to the lexicographic maximum of the two
4349 * inputs on each cell. If only one of the two inputs is defined on
4350 * a given cell, then it is considered to be the maximum.
4352 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4353 __isl_take isl_pw_multi_aff *pma1,
4354 __isl_take isl_pw_multi_aff *pma2)
4356 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4357 &pw_multi_aff_union_lexmax);
4360 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4361 __isl_take isl_pw_multi_aff *pma1,
4362 __isl_take isl_pw_multi_aff *pma2)
4364 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4365 &isl_multi_aff_lex_le_set);
4368 /* Given two piecewise multi affine expressions, return a piecewise
4369 * multi-affine expression defined on the union of the definition domains
4370 * of the inputs that is equal to the lexicographic minimum of the two
4371 * inputs on each cell. If only one of the two inputs is defined on
4372 * a given cell, then it is considered to be the minimum.
4374 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4375 __isl_take isl_pw_multi_aff *pma1,
4376 __isl_take isl_pw_multi_aff *pma2)
4378 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4379 &pw_multi_aff_union_lexmin);
4382 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4383 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4385 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4386 &isl_multi_aff_add);
4389 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4390 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4392 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4393 &pw_multi_aff_add);
4396 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4397 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4399 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4400 &isl_multi_aff_sub);
4403 /* Subtract "pma2" from "pma1" and return the result.
4405 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4406 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4408 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4409 &pw_multi_aff_sub);
4412 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4413 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4415 return isl_pw_multi_aff_union_add_(pma1, pma2);
4418 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4419 * with the actual sum on the shared domain and
4420 * the defined expression on the symmetric difference of the domains.
4422 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4423 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4425 return isl_union_pw_aff_union_add_(upa1, upa2);
4428 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4429 * with the actual sum on the shared domain and
4430 * the defined expression on the symmetric difference of the domains.
4432 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4433 __isl_take isl_union_pw_multi_aff *upma1,
4434 __isl_take isl_union_pw_multi_aff *upma2)
4436 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4439 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4440 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4442 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4443 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4445 int i, j, n;
4446 isl_space *space;
4447 isl_pw_multi_aff *res;
4449 if (!pma1 || !pma2)
4450 goto error;
4452 n = pma1->n * pma2->n;
4453 space = isl_space_product(isl_space_copy(pma1->dim),
4454 isl_space_copy(pma2->dim));
4455 res = isl_pw_multi_aff_alloc_size(space, n);
4457 for (i = 0; i < pma1->n; ++i) {
4458 for (j = 0; j < pma2->n; ++j) {
4459 isl_set *domain;
4460 isl_multi_aff *ma;
4462 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4463 isl_set_copy(pma2->p[j].set));
4464 ma = isl_multi_aff_product(
4465 isl_multi_aff_copy(pma1->p[i].maff),
4466 isl_multi_aff_copy(pma2->p[j].maff));
4467 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4471 isl_pw_multi_aff_free(pma1);
4472 isl_pw_multi_aff_free(pma2);
4473 return res;
4474 error:
4475 isl_pw_multi_aff_free(pma1);
4476 isl_pw_multi_aff_free(pma2);
4477 return NULL;
4480 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4481 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4483 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4484 &pw_multi_aff_product);
4487 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4488 * denominator "denom".
4489 * "denom" is allowed to be negative, in which case the actual denominator
4490 * is -denom and the expressions are added instead.
4492 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4493 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4495 int i, first;
4496 int sign;
4497 isl_int d;
4499 first = isl_seq_first_non_zero(c, n);
4500 if (first == -1)
4501 return aff;
4503 sign = isl_int_sgn(denom);
4504 isl_int_init(d);
4505 isl_int_abs(d, denom);
4506 for (i = first; i < n; ++i) {
4507 isl_aff *aff_i;
4509 if (isl_int_is_zero(c[i]))
4510 continue;
4511 aff_i = isl_multi_aff_get_aff(ma, i);
4512 aff_i = isl_aff_scale(aff_i, c[i]);
4513 aff_i = isl_aff_scale_down(aff_i, d);
4514 if (sign >= 0)
4515 aff = isl_aff_sub(aff, aff_i);
4516 else
4517 aff = isl_aff_add(aff, aff_i);
4519 isl_int_clear(d);
4521 return aff;
4524 /* Extract an affine expression that expresses the output dimension "pos"
4525 * of "bmap" in terms of the parameters and input dimensions from
4526 * equality "eq".
4527 * Note that this expression may involve integer divisions defined
4528 * in terms of parameters and input dimensions.
4529 * The equality may also involve references to earlier (but not later)
4530 * output dimensions. These are replaced by the corresponding elements
4531 * in "ma".
4533 * If the equality is of the form
4535 * f(i) + h(j) + a x + g(i) = 0,
4537 * with f(i) a linear combinations of the parameters and input dimensions,
4538 * g(i) a linear combination of integer divisions defined in terms of the same
4539 * and h(j) a linear combinations of earlier output dimensions,
4540 * then the affine expression is
4542 * (-f(i) - g(i))/a - h(j)/a
4544 * If the equality is of the form
4546 * f(i) + h(j) - a x + g(i) = 0,
4548 * then the affine expression is
4550 * (f(i) + g(i))/a - h(j)/(-a)
4553 * If "div" refers to an integer division (i.e., it is smaller than
4554 * the number of integer divisions), then the equality constraint
4555 * does involve an integer division (the one at position "div") that
4556 * is defined in terms of output dimensions. However, this integer
4557 * division can be eliminated by exploiting a pair of constraints
4558 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4559 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4560 * -l + x >= 0.
4561 * In particular, let
4563 * x = e(i) + m floor(...)
4565 * with e(i) the expression derived above and floor(...) the integer
4566 * division involving output dimensions.
4567 * From
4569 * l <= x <= l + n,
4571 * we have
4573 * 0 <= x - l <= n
4575 * This means
4577 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4578 * = (e(i) - l) mod m
4580 * Therefore,
4582 * x - l = (e(i) - l) mod m
4584 * or
4586 * x = ((e(i) - l) mod m) + l
4588 * The variable "shift" below contains the expression -l, which may
4589 * also involve a linear combination of earlier output dimensions.
4591 static __isl_give isl_aff *extract_aff_from_equality(
4592 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4593 __isl_keep isl_multi_aff *ma)
4595 unsigned o_out;
4596 isl_size n_div, n_out;
4597 isl_ctx *ctx;
4598 isl_local_space *ls;
4599 isl_aff *aff, *shift;
4600 isl_val *mod;
4602 ctx = isl_basic_map_get_ctx(bmap);
4603 ls = isl_basic_map_get_local_space(bmap);
4604 ls = isl_local_space_domain(ls);
4605 aff = isl_aff_alloc(isl_local_space_copy(ls));
4606 if (!aff)
4607 goto error;
4608 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4609 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4610 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4611 if (n_out < 0 || n_div < 0)
4612 goto error;
4613 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4614 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4615 isl_seq_cpy(aff->v->el + 1 + o_out,
4616 bmap->eq[eq] + o_out + n_out, n_div);
4617 } else {
4618 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4619 isl_seq_neg(aff->v->el + 1 + o_out,
4620 bmap->eq[eq] + o_out + n_out, n_div);
4622 if (div < n_div)
4623 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4624 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4625 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4626 bmap->eq[eq][o_out + pos]);
4627 if (div < n_div) {
4628 shift = isl_aff_alloc(isl_local_space_copy(ls));
4629 if (!shift)
4630 goto error;
4631 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4632 isl_seq_cpy(shift->v->el + 1 + o_out,
4633 bmap->ineq[ineq] + o_out + n_out, n_div);
4634 isl_int_set_si(shift->v->el[0], 1);
4635 shift = subtract_initial(shift, ma, pos,
4636 bmap->ineq[ineq] + o_out, ctx->negone);
4637 aff = isl_aff_add(aff, isl_aff_copy(shift));
4638 mod = isl_val_int_from_isl_int(ctx,
4639 bmap->eq[eq][o_out + n_out + div]);
4640 mod = isl_val_abs(mod);
4641 aff = isl_aff_mod_val(aff, mod);
4642 aff = isl_aff_sub(aff, shift);
4645 isl_local_space_free(ls);
4646 return aff;
4647 error:
4648 isl_local_space_free(ls);
4649 isl_aff_free(aff);
4650 return NULL;
4653 /* Given a basic map with output dimensions defined
4654 * in terms of the parameters input dimensions and earlier
4655 * output dimensions using an equality (and possibly a pair on inequalities),
4656 * extract an isl_aff that expresses output dimension "pos" in terms
4657 * of the parameters and input dimensions.
4658 * Note that this expression may involve integer divisions defined
4659 * in terms of parameters and input dimensions.
4660 * "ma" contains the expressions corresponding to earlier output dimensions.
4662 * This function shares some similarities with
4663 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4665 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4666 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4668 int eq, div, ineq;
4669 isl_aff *aff;
4671 if (!bmap)
4672 return NULL;
4673 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4674 if (eq >= bmap->n_eq)
4675 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4676 "unable to find suitable equality", return NULL);
4677 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4679 aff = isl_aff_remove_unused_divs(aff);
4680 return aff;
4683 /* Given a basic map where each output dimension is defined
4684 * in terms of the parameters and input dimensions using an equality,
4685 * extract an isl_multi_aff that expresses the output dimensions in terms
4686 * of the parameters and input dimensions.
4688 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4689 __isl_take isl_basic_map *bmap)
4691 int i;
4692 isl_size n_out;
4693 isl_multi_aff *ma;
4695 if (!bmap)
4696 return NULL;
4698 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4699 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4700 if (n_out < 0)
4701 ma = isl_multi_aff_free(ma);
4703 for (i = 0; i < n_out; ++i) {
4704 isl_aff *aff;
4706 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4707 ma = isl_multi_aff_set_aff(ma, i, aff);
4710 isl_basic_map_free(bmap);
4712 return ma;
4715 /* Given a basic set where each set dimension is defined
4716 * in terms of the parameters using an equality,
4717 * extract an isl_multi_aff that expresses the set dimensions in terms
4718 * of the parameters.
4720 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4721 __isl_take isl_basic_set *bset)
4723 return extract_isl_multi_aff_from_basic_map(bset);
4726 /* Create an isl_pw_multi_aff that is equivalent to
4727 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4728 * The given basic map is such that each output dimension is defined
4729 * in terms of the parameters and input dimensions using an equality.
4731 * Since some applications expect the result of isl_pw_multi_aff_from_map
4732 * to only contain integer affine expressions, we compute the floor
4733 * of the expression before returning.
4735 * Remove all constraints involving local variables without
4736 * an explicit representation (resulting in the removal of those
4737 * local variables) prior to the actual extraction to ensure
4738 * that the local spaces in which the resulting affine expressions
4739 * are created do not contain any unknown local variables.
4740 * Removing such constraints is safe because constraints involving
4741 * unknown local variables are not used to determine whether
4742 * a basic map is obviously single-valued.
4744 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4745 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4747 isl_multi_aff *ma;
4749 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4750 ma = extract_isl_multi_aff_from_basic_map(bmap);
4751 ma = isl_multi_aff_floor(ma);
4752 return isl_pw_multi_aff_alloc(domain, ma);
4755 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4756 * This obviously only works if the input "map" is single-valued.
4757 * If so, we compute the lexicographic minimum of the image in the form
4758 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4759 * to its lexicographic minimum.
4760 * If the input is not single-valued, we produce an error.
4762 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4763 __isl_take isl_map *map)
4765 int i;
4766 int sv;
4767 isl_pw_multi_aff *pma;
4769 sv = isl_map_is_single_valued(map);
4770 if (sv < 0)
4771 goto error;
4772 if (!sv)
4773 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4774 "map is not single-valued", goto error);
4775 map = isl_map_make_disjoint(map);
4776 if (!map)
4777 return NULL;
4779 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4781 for (i = 0; i < map->n; ++i) {
4782 isl_pw_multi_aff *pma_i;
4783 isl_basic_map *bmap;
4784 bmap = isl_basic_map_copy(map->p[i]);
4785 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4786 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4789 isl_map_free(map);
4790 return pma;
4791 error:
4792 isl_map_free(map);
4793 return NULL;
4796 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4797 * taking into account that the output dimension at position "d"
4798 * can be represented as
4800 * x = floor((e(...) + c1) / m)
4802 * given that constraint "i" is of the form
4804 * e(...) + c1 - m x >= 0
4807 * Let "map" be of the form
4809 * A -> B
4811 * We construct a mapping
4813 * A -> [A -> x = floor(...)]
4815 * apply that to the map, obtaining
4817 * [A -> x = floor(...)] -> B
4819 * and equate dimension "d" to x.
4820 * We then compute a isl_pw_multi_aff representation of the resulting map
4821 * and plug in the mapping above.
4823 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4824 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4826 isl_ctx *ctx;
4827 isl_space *space = NULL;
4828 isl_local_space *ls;
4829 isl_multi_aff *ma;
4830 isl_aff *aff;
4831 isl_vec *v;
4832 isl_map *insert;
4833 int offset;
4834 isl_size n;
4835 isl_size n_in;
4836 isl_pw_multi_aff *pma;
4837 isl_bool is_set;
4839 is_set = isl_map_is_set(map);
4840 if (is_set < 0)
4841 goto error;
4843 offset = isl_basic_map_offset(hull, isl_dim_out);
4844 ctx = isl_map_get_ctx(map);
4845 space = isl_space_domain(isl_map_get_space(map));
4846 n_in = isl_space_dim(space, isl_dim_set);
4847 n = isl_space_dim(space, isl_dim_all);
4848 if (n_in < 0 || n < 0)
4849 goto error;
4851 v = isl_vec_alloc(ctx, 1 + 1 + n);
4852 if (v) {
4853 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4854 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4856 isl_basic_map_free(hull);
4858 ls = isl_local_space_from_space(isl_space_copy(space));
4859 aff = isl_aff_alloc_vec(ls, v);
4860 aff = isl_aff_floor(aff);
4861 if (is_set) {
4862 isl_space_free(space);
4863 ma = isl_multi_aff_from_aff(aff);
4864 } else {
4865 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4866 ma = isl_multi_aff_range_product(ma,
4867 isl_multi_aff_from_aff(aff));
4870 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4871 map = isl_map_apply_domain(map, insert);
4872 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4873 pma = isl_pw_multi_aff_from_map(map);
4874 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4876 return pma;
4877 error:
4878 isl_space_free(space);
4879 isl_map_free(map);
4880 isl_basic_map_free(hull);
4881 return NULL;
4884 /* Is constraint "c" of the form
4886 * e(...) + c1 - m x >= 0
4888 * or
4890 * -e(...) + c2 + m x >= 0
4892 * where m > 1 and e only depends on parameters and input dimemnsions?
4894 * "offset" is the offset of the output dimensions
4895 * "pos" is the position of output dimension x.
4897 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4899 if (isl_int_is_zero(c[offset + d]))
4900 return 0;
4901 if (isl_int_is_one(c[offset + d]))
4902 return 0;
4903 if (isl_int_is_negone(c[offset + d]))
4904 return 0;
4905 if (isl_seq_first_non_zero(c + offset, d) != -1)
4906 return 0;
4907 if (isl_seq_first_non_zero(c + offset + d + 1,
4908 total - (offset + d + 1)) != -1)
4909 return 0;
4910 return 1;
4913 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4915 * As a special case, we first check if there is any pair of constraints,
4916 * shared by all the basic maps in "map" that force a given dimension
4917 * to be equal to the floor of some affine combination of the input dimensions.
4919 * In particular, if we can find two constraints
4921 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4923 * and
4925 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4927 * where m > 1 and e only depends on parameters and input dimemnsions,
4928 * and such that
4930 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4932 * then we know that we can take
4934 * x = floor((e(...) + c1) / m)
4936 * without having to perform any computation.
4938 * Note that we know that
4940 * c1 + c2 >= 1
4942 * If c1 + c2 were 0, then we would have detected an equality during
4943 * simplification. If c1 + c2 were negative, then we would have detected
4944 * a contradiction.
4946 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4947 __isl_take isl_map *map)
4949 int d;
4950 isl_size dim;
4951 int i, j, n;
4952 int offset;
4953 isl_size total;
4954 isl_int sum;
4955 isl_basic_map *hull;
4957 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4958 dim = isl_map_dim(map, isl_dim_out);
4959 total = isl_basic_map_dim(hull, isl_dim_all);
4960 if (dim < 0 || total < 0)
4961 goto error;
4963 isl_int_init(sum);
4964 offset = isl_basic_map_offset(hull, isl_dim_out);
4965 n = hull->n_ineq;
4966 for (d = 0; d < dim; ++d) {
4967 for (i = 0; i < n; ++i) {
4968 if (!is_potential_div_constraint(hull->ineq[i],
4969 offset, d, 1 + total))
4970 continue;
4971 for (j = i + 1; j < n; ++j) {
4972 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4973 hull->ineq[j] + 1, total))
4974 continue;
4975 isl_int_add(sum, hull->ineq[i][0],
4976 hull->ineq[j][0]);
4977 if (isl_int_abs_lt(sum,
4978 hull->ineq[i][offset + d]))
4979 break;
4982 if (j >= n)
4983 continue;
4984 isl_int_clear(sum);
4985 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4986 j = i;
4987 return pw_multi_aff_from_map_div(map, hull, d, j);
4990 isl_int_clear(sum);
4991 isl_basic_map_free(hull);
4992 return pw_multi_aff_from_map_base(map);
4993 error:
4994 isl_map_free(map);
4995 isl_basic_map_free(hull);
4996 return NULL;
4999 /* Given an affine expression
5001 * [A -> B] -> f(A,B)
5003 * construct an isl_multi_aff
5005 * [A -> B] -> B'
5007 * such that dimension "d" in B' is set to "aff" and the remaining
5008 * dimensions are set equal to the corresponding dimensions in B.
5009 * "n_in" is the dimension of the space A.
5010 * "n_out" is the dimension of the space B.
5012 * If "is_set" is set, then the affine expression is of the form
5014 * [B] -> f(B)
5016 * and we construct an isl_multi_aff
5018 * B -> B'
5020 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5021 unsigned n_in, unsigned n_out, int is_set)
5023 int i;
5024 isl_multi_aff *ma;
5025 isl_space *space, *space2;
5026 isl_local_space *ls;
5028 space = isl_aff_get_domain_space(aff);
5029 ls = isl_local_space_from_space(isl_space_copy(space));
5030 space2 = isl_space_copy(space);
5031 if (!is_set)
5032 space2 = isl_space_range(isl_space_unwrap(space2));
5033 space = isl_space_map_from_domain_and_range(space, space2);
5034 ma = isl_multi_aff_alloc(space);
5035 ma = isl_multi_aff_set_aff(ma, d, aff);
5037 for (i = 0; i < n_out; ++i) {
5038 if (i == d)
5039 continue;
5040 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5041 isl_dim_set, n_in + i);
5042 ma = isl_multi_aff_set_aff(ma, i, aff);
5045 isl_local_space_free(ls);
5047 return ma;
5050 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5051 * taking into account that the dimension at position "d" can be written as
5053 * x = m a + f(..) (1)
5055 * where m is equal to "gcd".
5056 * "i" is the index of the equality in "hull" that defines f(..).
5057 * In particular, the equality is of the form
5059 * f(..) - x + m g(existentials) = 0
5061 * or
5063 * -f(..) + x + m g(existentials) = 0
5065 * We basically plug (1) into "map", resulting in a map with "a"
5066 * in the range instead of "x". The corresponding isl_pw_multi_aff
5067 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5069 * Specifically, given the input map
5071 * A -> B
5073 * We first wrap it into a set
5075 * [A -> B]
5077 * and define (1) on top of the corresponding space, resulting in "aff".
5078 * We use this to create an isl_multi_aff that maps the output position "d"
5079 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5080 * We plug this into the wrapped map, unwrap the result and compute the
5081 * corresponding isl_pw_multi_aff.
5082 * The result is an expression
5084 * A -> T(A)
5086 * We adjust that to
5088 * A -> [A -> T(A)]
5090 * so that we can plug that into "aff", after extending the latter to
5091 * a mapping
5093 * [A -> B] -> B'
5096 * If "map" is actually a set, then there is no "A" space, meaning
5097 * that we do not need to perform any wrapping, and that the result
5098 * of the recursive call is of the form
5100 * [T]
5102 * which is plugged into a mapping of the form
5104 * B -> B'
5106 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5107 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5108 isl_int gcd)
5110 isl_set *set;
5111 isl_space *space;
5112 isl_local_space *ls;
5113 isl_aff *aff;
5114 isl_multi_aff *ma;
5115 isl_pw_multi_aff *pma, *id;
5116 isl_size n_in;
5117 unsigned o_out;
5118 isl_size n_out;
5119 isl_bool is_set;
5121 is_set = isl_map_is_set(map);
5122 if (is_set < 0)
5123 goto error;
5125 n_in = isl_basic_map_dim(hull, isl_dim_in);
5126 n_out = isl_basic_map_dim(hull, isl_dim_out);
5127 if (n_in < 0 || n_out < 0)
5128 goto error;
5129 o_out = isl_basic_map_offset(hull, isl_dim_out);
5131 if (is_set)
5132 set = map;
5133 else
5134 set = isl_map_wrap(map);
5135 space = isl_space_map_from_set(isl_set_get_space(set));
5136 ma = isl_multi_aff_identity(space);
5137 ls = isl_local_space_from_space(isl_set_get_space(set));
5138 aff = isl_aff_alloc(ls);
5139 if (aff) {
5140 isl_int_set_si(aff->v->el[0], 1);
5141 if (isl_int_is_one(hull->eq[i][o_out + d]))
5142 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5143 aff->v->size - 1);
5144 else
5145 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5146 aff->v->size - 1);
5147 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5149 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5150 set = isl_set_preimage_multi_aff(set, ma);
5152 ma = range_map(aff, d, n_in, n_out, is_set);
5154 if (is_set)
5155 map = set;
5156 else
5157 map = isl_set_unwrap(set);
5158 pma = isl_pw_multi_aff_from_map(map);
5160 if (!is_set) {
5161 space = isl_pw_multi_aff_get_domain_space(pma);
5162 space = isl_space_map_from_set(space);
5163 id = isl_pw_multi_aff_identity(space);
5164 pma = isl_pw_multi_aff_range_product(id, pma);
5166 id = isl_pw_multi_aff_from_multi_aff(ma);
5167 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5169 isl_basic_map_free(hull);
5170 return pma;
5171 error:
5172 isl_map_free(map);
5173 isl_basic_map_free(hull);
5174 return NULL;
5177 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5178 * "hull" contains the equalities valid for "map".
5180 * Check if any of the output dimensions is "strided".
5181 * That is, we check if it can be written as
5183 * x = m a + f(..)
5185 * with m greater than 1, a some combination of existentially quantified
5186 * variables and f an expression in the parameters and input dimensions.
5187 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5189 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5190 * special case.
5192 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5193 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5195 int i, j;
5196 isl_size n_out;
5197 unsigned o_out;
5198 isl_size n_div;
5199 unsigned o_div;
5200 isl_int gcd;
5202 n_div = isl_basic_map_dim(hull, isl_dim_div);
5203 n_out = isl_basic_map_dim(hull, isl_dim_out);
5204 if (n_div < 0 || n_out < 0)
5205 goto error;
5207 if (n_div == 0) {
5208 isl_basic_map_free(hull);
5209 return pw_multi_aff_from_map_check_div(map);
5212 isl_int_init(gcd);
5214 o_div = isl_basic_map_offset(hull, isl_dim_div);
5215 o_out = isl_basic_map_offset(hull, isl_dim_out);
5217 for (i = 0; i < n_out; ++i) {
5218 for (j = 0; j < hull->n_eq; ++j) {
5219 isl_int *eq = hull->eq[j];
5220 isl_pw_multi_aff *res;
5222 if (!isl_int_is_one(eq[o_out + i]) &&
5223 !isl_int_is_negone(eq[o_out + i]))
5224 continue;
5225 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5226 continue;
5227 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5228 n_out - (i + 1)) != -1)
5229 continue;
5230 isl_seq_gcd(eq + o_div, n_div, &gcd);
5231 if (isl_int_is_zero(gcd))
5232 continue;
5233 if (isl_int_is_one(gcd))
5234 continue;
5236 res = pw_multi_aff_from_map_stride(map, hull,
5237 i, j, gcd);
5238 isl_int_clear(gcd);
5239 return res;
5243 isl_int_clear(gcd);
5244 isl_basic_map_free(hull);
5245 return pw_multi_aff_from_map_check_div(map);
5246 error:
5247 isl_map_free(map);
5248 isl_basic_map_free(hull);
5249 return NULL;
5252 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5254 * As a special case, we first check if all output dimensions are uniquely
5255 * defined in terms of the parameters and input dimensions over the entire
5256 * domain. If so, we extract the desired isl_pw_multi_aff directly
5257 * from the affine hull of "map" and its domain.
5259 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5260 * special cases.
5262 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5264 isl_bool sv;
5265 isl_size n;
5266 isl_basic_map *hull;
5268 n = isl_map_n_basic_map(map);
5269 if (n < 0)
5270 goto error;
5272 if (n == 1) {
5273 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5274 hull = isl_basic_map_plain_affine_hull(hull);
5275 sv = isl_basic_map_plain_is_single_valued(hull);
5276 if (sv >= 0 && sv)
5277 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5278 hull);
5279 isl_basic_map_free(hull);
5281 map = isl_map_detect_equalities(map);
5282 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5283 sv = isl_basic_map_plain_is_single_valued(hull);
5284 if (sv >= 0 && sv)
5285 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5286 if (sv >= 0)
5287 return pw_multi_aff_from_map_check_strides(map, hull);
5288 isl_basic_map_free(hull);
5289 error:
5290 isl_map_free(map);
5291 return NULL;
5294 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5296 return isl_pw_multi_aff_from_map(set);
5299 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5300 * add it to *user.
5302 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5304 isl_union_pw_multi_aff **upma = user;
5305 isl_pw_multi_aff *pma;
5307 pma = isl_pw_multi_aff_from_map(map);
5308 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5310 return *upma ? isl_stat_ok : isl_stat_error;
5313 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5314 * domain.
5316 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5317 __isl_take isl_aff *aff)
5319 isl_multi_aff *ma;
5320 isl_pw_multi_aff *pma;
5322 ma = isl_multi_aff_from_aff(aff);
5323 pma = isl_pw_multi_aff_from_multi_aff(ma);
5324 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5327 /* Try and create an isl_union_pw_multi_aff that is equivalent
5328 * to the given isl_union_map.
5329 * The isl_union_map is required to be single-valued in each space.
5330 * Otherwise, an error is produced.
5332 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5333 __isl_take isl_union_map *umap)
5335 isl_space *space;
5336 isl_union_pw_multi_aff *upma;
5338 space = isl_union_map_get_space(umap);
5339 upma = isl_union_pw_multi_aff_empty(space);
5340 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5341 upma = isl_union_pw_multi_aff_free(upma);
5342 isl_union_map_free(umap);
5344 return upma;
5347 /* Try and create an isl_union_pw_multi_aff that is equivalent
5348 * to the given isl_union_set.
5349 * The isl_union_set is required to be a singleton in each space.
5350 * Otherwise, an error is produced.
5352 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5353 __isl_take isl_union_set *uset)
5355 return isl_union_pw_multi_aff_from_union_map(uset);
5358 /* Return the piecewise affine expression "set ? 1 : 0".
5360 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5362 isl_pw_aff *pa;
5363 isl_space *space = isl_set_get_space(set);
5364 isl_local_space *ls = isl_local_space_from_space(space);
5365 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5366 isl_aff *one = isl_aff_zero_on_domain(ls);
5368 one = isl_aff_add_constant_si(one, 1);
5369 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5370 set = isl_set_complement(set);
5371 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5373 return pa;
5376 /* Plug in "subs" for dimension "type", "pos" of "aff".
5378 * Let i be the dimension to replace and let "subs" be of the form
5380 * f/d
5382 * and "aff" of the form
5384 * (a i + g)/m
5386 * The result is
5388 * (a f + d g')/(m d)
5390 * where g' is the result of plugging in "subs" in each of the integer
5391 * divisions in g.
5393 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5394 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5396 isl_ctx *ctx;
5397 isl_int v;
5398 isl_size n_div;
5400 aff = isl_aff_cow(aff);
5401 if (!aff || !subs)
5402 return isl_aff_free(aff);
5404 ctx = isl_aff_get_ctx(aff);
5405 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5406 isl_die(ctx, isl_error_invalid,
5407 "spaces don't match", return isl_aff_free(aff));
5408 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
5409 if (n_div < 0)
5410 return isl_aff_free(aff);
5411 if (n_div != 0)
5412 isl_die(ctx, isl_error_unsupported,
5413 "cannot handle divs yet", return isl_aff_free(aff));
5415 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5416 if (!aff->ls)
5417 return isl_aff_free(aff);
5419 aff->v = isl_vec_cow(aff->v);
5420 if (!aff->v)
5421 return isl_aff_free(aff);
5423 pos += isl_local_space_offset(aff->ls, type);
5425 isl_int_init(v);
5426 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5427 aff->v->size, subs->v->size, v);
5428 isl_int_clear(v);
5430 return aff;
5433 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5434 * expressions in "maff".
5436 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5437 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5438 __isl_keep isl_aff *subs)
5440 int i;
5442 maff = isl_multi_aff_cow(maff);
5443 if (!maff || !subs)
5444 return isl_multi_aff_free(maff);
5446 if (type == isl_dim_in)
5447 type = isl_dim_set;
5449 for (i = 0; i < maff->n; ++i) {
5450 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5451 type, pos, subs);
5452 if (!maff->u.p[i])
5453 return isl_multi_aff_free(maff);
5456 return maff;
5459 /* Plug in "subs" for dimension "type", "pos" of "pma".
5461 * pma is of the form
5463 * A_i(v) -> M_i(v)
5465 * while subs is of the form
5467 * v' = B_j(v) -> S_j
5469 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5470 * has a contribution in the result, in particular
5472 * C_ij(S_j) -> M_i(S_j)
5474 * Note that plugging in S_j in C_ij may also result in an empty set
5475 * and this contribution should simply be discarded.
5477 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5478 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5479 __isl_keep isl_pw_aff *subs)
5481 int i, j, n;
5482 isl_pw_multi_aff *res;
5484 if (!pma || !subs)
5485 return isl_pw_multi_aff_free(pma);
5487 n = pma->n * subs->n;
5488 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5490 for (i = 0; i < pma->n; ++i) {
5491 for (j = 0; j < subs->n; ++j) {
5492 isl_set *common;
5493 isl_multi_aff *res_ij;
5494 int empty;
5496 common = isl_set_intersect(
5497 isl_set_copy(pma->p[i].set),
5498 isl_set_copy(subs->p[j].set));
5499 common = isl_set_substitute(common,
5500 type, pos, subs->p[j].aff);
5501 empty = isl_set_plain_is_empty(common);
5502 if (empty < 0 || empty) {
5503 isl_set_free(common);
5504 if (empty < 0)
5505 goto error;
5506 continue;
5509 res_ij = isl_multi_aff_substitute(
5510 isl_multi_aff_copy(pma->p[i].maff),
5511 type, pos, subs->p[j].aff);
5513 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5517 isl_pw_multi_aff_free(pma);
5518 return res;
5519 error:
5520 isl_pw_multi_aff_free(pma);
5521 isl_pw_multi_aff_free(res);
5522 return NULL;
5525 /* Compute the preimage of a range of dimensions in the affine expression "src"
5526 * under "ma" and put the result in "dst". The number of dimensions in "src"
5527 * that precede the range is given by "n_before". The number of dimensions
5528 * in the range is given by the number of output dimensions of "ma".
5529 * The number of dimensions that follow the range is given by "n_after".
5530 * If "has_denom" is set (to one),
5531 * then "src" and "dst" have an extra initial denominator.
5532 * "n_div_ma" is the number of existentials in "ma"
5533 * "n_div_bset" is the number of existentials in "src"
5534 * The resulting "dst" (which is assumed to have been allocated by
5535 * the caller) contains coefficients for both sets of existentials,
5536 * first those in "ma" and then those in "src".
5537 * f, c1, c2 and g are temporary objects that have been initialized
5538 * by the caller.
5540 * Let src represent the expression
5542 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5544 * and let ma represent the expressions
5546 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5548 * We start out with the following expression for dst:
5550 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5552 * with the multiplication factor f initially equal to 1
5553 * and f \sum_i b_i v_i kept separately.
5554 * For each x_i that we substitute, we multiply the numerator
5555 * (and denominator) of dst by c_1 = m_i and add the numerator
5556 * of the x_i expression multiplied by c_2 = f b_i,
5557 * after removing the common factors of c_1 and c_2.
5558 * The multiplication factor f also needs to be multiplied by c_1
5559 * for the next x_j, j > i.
5561 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5562 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5563 int n_div_ma, int n_div_bmap,
5564 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5566 int i;
5567 isl_size n_param, n_in, n_out;
5568 int o_dst, o_src;
5570 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5571 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5572 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5573 if (n_param < 0 || n_in < 0 || n_out < 0)
5574 return isl_stat_error;
5576 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5577 o_dst = o_src = has_denom + 1 + n_param + n_before;
5578 isl_seq_clr(dst + o_dst, n_in);
5579 o_dst += n_in;
5580 o_src += n_out;
5581 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5582 o_dst += n_after;
5583 o_src += n_after;
5584 isl_seq_clr(dst + o_dst, n_div_ma);
5585 o_dst += n_div_ma;
5586 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5588 isl_int_set_si(f, 1);
5590 for (i = 0; i < n_out; ++i) {
5591 int offset = has_denom + 1 + n_param + n_before + i;
5593 if (isl_int_is_zero(src[offset]))
5594 continue;
5595 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5596 isl_int_mul(c2, f, src[offset]);
5597 isl_int_gcd(g, c1, c2);
5598 isl_int_divexact(c1, c1, g);
5599 isl_int_divexact(c2, c2, g);
5601 isl_int_mul(f, f, c1);
5602 o_dst = has_denom;
5603 o_src = 1;
5604 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5605 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5606 o_dst += 1 + n_param;
5607 o_src += 1 + n_param;
5608 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5609 o_dst += n_before;
5610 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5611 c2, ma->u.p[i]->v->el + o_src, n_in);
5612 o_dst += n_in;
5613 o_src += n_in;
5614 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5615 o_dst += n_after;
5616 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5617 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5618 o_dst += n_div_ma;
5619 o_src += n_div_ma;
5620 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5621 if (has_denom)
5622 isl_int_mul(dst[0], dst[0], c1);
5625 return isl_stat_ok;
5628 /* Compute the pullback of "aff" by the function represented by "ma".
5629 * In other words, plug in "ma" in "aff". The result is an affine expression
5630 * defined over the domain space of "ma".
5632 * If "aff" is represented by
5634 * (a(p) + b x + c(divs))/d
5636 * and ma is represented by
5638 * x = D(p) + F(y) + G(divs')
5640 * then the result is
5642 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5644 * The divs in the local space of the input are similarly adjusted
5645 * through a call to isl_local_space_preimage_multi_aff.
5647 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5648 __isl_take isl_multi_aff *ma)
5650 isl_aff *res = NULL;
5651 isl_local_space *ls;
5652 isl_size n_div_aff, n_div_ma;
5653 isl_int f, c1, c2, g;
5655 ma = isl_multi_aff_align_divs(ma);
5656 if (!aff || !ma)
5657 goto error;
5659 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5660 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5661 if (n_div_aff < 0 || n_div_ma < 0)
5662 goto error;
5664 ls = isl_aff_get_domain_local_space(aff);
5665 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5666 res = isl_aff_alloc(ls);
5667 if (!res)
5668 goto error;
5670 isl_int_init(f);
5671 isl_int_init(c1);
5672 isl_int_init(c2);
5673 isl_int_init(g);
5675 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5676 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5677 res = isl_aff_free(res);
5679 isl_int_clear(f);
5680 isl_int_clear(c1);
5681 isl_int_clear(c2);
5682 isl_int_clear(g);
5684 isl_aff_free(aff);
5685 isl_multi_aff_free(ma);
5686 res = isl_aff_normalize(res);
5687 return res;
5688 error:
5689 isl_aff_free(aff);
5690 isl_multi_aff_free(ma);
5691 isl_aff_free(res);
5692 return NULL;
5695 /* Compute the pullback of "aff1" by the function represented by "aff2".
5696 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5697 * defined over the domain space of "aff1".
5699 * The domain of "aff1" should match the range of "aff2", which means
5700 * that it should be single-dimensional.
5702 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5703 __isl_take isl_aff *aff2)
5705 isl_multi_aff *ma;
5707 ma = isl_multi_aff_from_aff(aff2);
5708 return isl_aff_pullback_multi_aff(aff1, ma);
5711 /* Compute the pullback of "ma1" by the function represented by "ma2".
5712 * In other words, plug in "ma2" in "ma1".
5714 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5716 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5717 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5719 int i;
5720 isl_space *space = NULL;
5722 ma2 = isl_multi_aff_align_divs(ma2);
5723 ma1 = isl_multi_aff_cow(ma1);
5724 if (!ma1 || !ma2)
5725 goto error;
5727 space = isl_space_join(isl_multi_aff_get_space(ma2),
5728 isl_multi_aff_get_space(ma1));
5730 for (i = 0; i < ma1->n; ++i) {
5731 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5732 isl_multi_aff_copy(ma2));
5733 if (!ma1->u.p[i])
5734 goto error;
5737 ma1 = isl_multi_aff_reset_space(ma1, space);
5738 isl_multi_aff_free(ma2);
5739 return ma1;
5740 error:
5741 isl_space_free(space);
5742 isl_multi_aff_free(ma2);
5743 isl_multi_aff_free(ma1);
5744 return NULL;
5747 /* Compute the pullback of "ma1" by the function represented by "ma2".
5748 * In other words, plug in "ma2" in "ma1".
5750 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5751 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5753 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5754 &isl_multi_aff_pullback_multi_aff_aligned);
5757 /* Extend the local space of "dst" to include the divs
5758 * in the local space of "src".
5760 * If "src" does not have any divs or if the local spaces of "dst" and
5761 * "src" are the same, then no extension is required.
5763 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5764 __isl_keep isl_aff *src)
5766 isl_ctx *ctx;
5767 isl_size src_n_div, dst_n_div;
5768 int *exp1 = NULL;
5769 int *exp2 = NULL;
5770 isl_bool equal;
5771 isl_mat *div;
5773 if (!src || !dst)
5774 return isl_aff_free(dst);
5776 ctx = isl_aff_get_ctx(src);
5777 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5778 if (equal < 0)
5779 return isl_aff_free(dst);
5780 if (!equal)
5781 isl_die(ctx, isl_error_invalid,
5782 "spaces don't match", goto error);
5784 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5785 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5786 if (src_n_div == 0)
5787 return dst;
5788 equal = isl_local_space_is_equal(src->ls, dst->ls);
5789 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5790 return isl_aff_free(dst);
5791 if (equal)
5792 return dst;
5794 exp1 = isl_alloc_array(ctx, int, src_n_div);
5795 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5796 if (!exp1 || (dst_n_div && !exp2))
5797 goto error;
5799 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5800 dst = isl_aff_expand_divs(dst, div, exp2);
5801 free(exp1);
5802 free(exp2);
5804 return dst;
5805 error:
5806 free(exp1);
5807 free(exp2);
5808 return isl_aff_free(dst);
5811 /* Adjust the local spaces of the affine expressions in "maff"
5812 * such that they all have the save divs.
5814 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5815 __isl_take isl_multi_aff *maff)
5817 int i;
5819 if (!maff)
5820 return NULL;
5821 if (maff->n == 0)
5822 return maff;
5823 maff = isl_multi_aff_cow(maff);
5824 if (!maff)
5825 return NULL;
5827 for (i = 1; i < maff->n; ++i)
5828 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5829 for (i = 1; i < maff->n; ++i) {
5830 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5831 if (!maff->u.p[i])
5832 return isl_multi_aff_free(maff);
5835 return maff;
5838 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5840 aff = isl_aff_cow(aff);
5841 if (!aff)
5842 return NULL;
5844 aff->ls = isl_local_space_lift(aff->ls);
5845 if (!aff->ls)
5846 return isl_aff_free(aff);
5848 return aff;
5851 /* Lift "maff" to a space with extra dimensions such that the result
5852 * has no more existentially quantified variables.
5853 * If "ls" is not NULL, then *ls is assigned the local space that lies
5854 * at the basis of the lifting applied to "maff".
5856 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5857 __isl_give isl_local_space **ls)
5859 int i;
5860 isl_space *space;
5861 isl_size n_div;
5863 if (ls)
5864 *ls = NULL;
5866 if (!maff)
5867 return NULL;
5869 if (maff->n == 0) {
5870 if (ls) {
5871 isl_space *space = isl_multi_aff_get_domain_space(maff);
5872 *ls = isl_local_space_from_space(space);
5873 if (!*ls)
5874 return isl_multi_aff_free(maff);
5876 return maff;
5879 maff = isl_multi_aff_cow(maff);
5880 maff = isl_multi_aff_align_divs(maff);
5881 if (!maff)
5882 return NULL;
5884 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5885 if (n_div < 0)
5886 return isl_multi_aff_free(maff);
5887 space = isl_multi_aff_get_space(maff);
5888 space = isl_space_lift(isl_space_domain(space), n_div);
5889 space = isl_space_extend_domain_with_range(space,
5890 isl_multi_aff_get_space(maff));
5891 if (!space)
5892 return isl_multi_aff_free(maff);
5893 isl_space_free(maff->space);
5894 maff->space = space;
5896 if (ls) {
5897 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5898 if (!*ls)
5899 return isl_multi_aff_free(maff);
5902 for (i = 0; i < maff->n; ++i) {
5903 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5904 if (!maff->u.p[i])
5905 goto error;
5908 return maff;
5909 error:
5910 if (ls)
5911 isl_local_space_free(*ls);
5912 return isl_multi_aff_free(maff);
5915 #undef TYPE
5916 #define TYPE isl_pw_multi_aff
5917 static
5918 #include "check_type_range_templ.c"
5920 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5922 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5923 __isl_keep isl_pw_multi_aff *pma, int pos)
5925 int i;
5926 isl_size n_out;
5927 isl_space *space;
5928 isl_pw_aff *pa;
5930 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
5931 return NULL;
5933 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5934 if (n_out < 0)
5935 return NULL;
5937 space = isl_pw_multi_aff_get_space(pma);
5938 space = isl_space_drop_dims(space, isl_dim_out,
5939 pos + 1, n_out - pos - 1);
5940 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5942 pa = isl_pw_aff_alloc_size(space, pma->n);
5943 for (i = 0; i < pma->n; ++i) {
5944 isl_aff *aff;
5945 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5946 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5949 return pa;
5952 /* Return an isl_pw_multi_aff with the given "set" as domain and
5953 * an unnamed zero-dimensional range.
5955 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5956 __isl_take isl_set *set)
5958 isl_multi_aff *ma;
5959 isl_space *space;
5961 space = isl_set_get_space(set);
5962 space = isl_space_from_domain(space);
5963 ma = isl_multi_aff_zero(space);
5964 return isl_pw_multi_aff_alloc(set, ma);
5967 /* Add an isl_pw_multi_aff with the given "set" as domain and
5968 * an unnamed zero-dimensional range to *user.
5970 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5971 void *user)
5973 isl_union_pw_multi_aff **upma = user;
5974 isl_pw_multi_aff *pma;
5976 pma = isl_pw_multi_aff_from_domain(set);
5977 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5979 return isl_stat_ok;
5982 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5983 * an unnamed zero-dimensional range.
5985 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5986 __isl_take isl_union_set *uset)
5988 isl_space *space;
5989 isl_union_pw_multi_aff *upma;
5991 if (!uset)
5992 return NULL;
5994 space = isl_union_set_get_space(uset);
5995 upma = isl_union_pw_multi_aff_empty(space);
5997 if (isl_union_set_foreach_set(uset,
5998 &add_pw_multi_aff_from_domain, &upma) < 0)
5999 goto error;
6001 isl_union_set_free(uset);
6002 return upma;
6003 error:
6004 isl_union_set_free(uset);
6005 isl_union_pw_multi_aff_free(upma);
6006 return NULL;
6009 /* Local data for bin_entry and the callback "fn".
6011 struct isl_union_pw_multi_aff_bin_data {
6012 isl_union_pw_multi_aff *upma2;
6013 isl_union_pw_multi_aff *res;
6014 isl_pw_multi_aff *pma;
6015 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6018 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6019 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6021 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6023 struct isl_union_pw_multi_aff_bin_data *data = user;
6024 isl_stat r;
6026 data->pma = pma;
6027 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6028 data->fn, data);
6029 isl_pw_multi_aff_free(pma);
6031 return r;
6034 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6035 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6036 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6037 * as *entry. The callback should adjust data->res if desired.
6039 static __isl_give isl_union_pw_multi_aff *bin_op(
6040 __isl_take isl_union_pw_multi_aff *upma1,
6041 __isl_take isl_union_pw_multi_aff *upma2,
6042 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6044 isl_space *space;
6045 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6047 space = isl_union_pw_multi_aff_get_space(upma2);
6048 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6049 space = isl_union_pw_multi_aff_get_space(upma1);
6050 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6052 if (!upma1 || !upma2)
6053 goto error;
6055 data.upma2 = upma2;
6056 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6057 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6058 &bin_entry, &data) < 0)
6059 goto error;
6061 isl_union_pw_multi_aff_free(upma1);
6062 isl_union_pw_multi_aff_free(upma2);
6063 return data.res;
6064 error:
6065 isl_union_pw_multi_aff_free(upma1);
6066 isl_union_pw_multi_aff_free(upma2);
6067 isl_union_pw_multi_aff_free(data.res);
6068 return NULL;
6071 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6072 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6074 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6075 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6077 isl_space *space;
6079 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6080 isl_pw_multi_aff_get_space(pma2));
6081 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6082 &isl_multi_aff_range_product);
6085 /* Given two isl_pw_multi_affs A -> B and C -> D,
6086 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6088 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6089 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6091 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6092 &pw_multi_aff_range_product);
6095 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6096 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6098 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6099 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6101 isl_space *space;
6103 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6104 isl_pw_multi_aff_get_space(pma2));
6105 space = isl_space_flatten_range(space);
6106 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6107 &isl_multi_aff_flat_range_product);
6110 /* Given two isl_pw_multi_affs A -> B and C -> D,
6111 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6113 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6114 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6116 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6117 &pw_multi_aff_flat_range_product);
6120 /* If data->pma and "pma2" have the same domain space, then compute
6121 * their flat range product and the result to data->res.
6123 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6124 void *user)
6126 struct isl_union_pw_multi_aff_bin_data *data = user;
6128 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6129 pma2->dim, isl_dim_in)) {
6130 isl_pw_multi_aff_free(pma2);
6131 return isl_stat_ok;
6134 pma2 = isl_pw_multi_aff_flat_range_product(
6135 isl_pw_multi_aff_copy(data->pma), pma2);
6137 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6139 return isl_stat_ok;
6142 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6143 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6145 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6146 __isl_take isl_union_pw_multi_aff *upma1,
6147 __isl_take isl_union_pw_multi_aff *upma2)
6149 return bin_op(upma1, upma2, &flat_range_product_entry);
6152 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6153 * The parameters are assumed to have been aligned.
6155 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6156 * except that it works on two different isl_pw_* types.
6158 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6159 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6160 __isl_take isl_pw_aff *pa)
6162 int i, j, n;
6163 isl_pw_multi_aff *res = NULL;
6165 if (!pma || !pa)
6166 goto error;
6168 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6169 pa->dim, isl_dim_in))
6170 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6171 "domains don't match", goto error);
6172 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6173 goto error;
6175 n = pma->n * pa->n;
6176 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6178 for (i = 0; i < pma->n; ++i) {
6179 for (j = 0; j < pa->n; ++j) {
6180 isl_set *common;
6181 isl_multi_aff *res_ij;
6182 int empty;
6184 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6185 isl_set_copy(pa->p[j].set));
6186 empty = isl_set_plain_is_empty(common);
6187 if (empty < 0 || empty) {
6188 isl_set_free(common);
6189 if (empty < 0)
6190 goto error;
6191 continue;
6194 res_ij = isl_multi_aff_set_aff(
6195 isl_multi_aff_copy(pma->p[i].maff), pos,
6196 isl_aff_copy(pa->p[j].aff));
6197 res_ij = isl_multi_aff_gist(res_ij,
6198 isl_set_copy(common));
6200 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6204 isl_pw_multi_aff_free(pma);
6205 isl_pw_aff_free(pa);
6206 return res;
6207 error:
6208 isl_pw_multi_aff_free(pma);
6209 isl_pw_aff_free(pa);
6210 return isl_pw_multi_aff_free(res);
6213 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6215 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6216 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6217 __isl_take isl_pw_aff *pa)
6219 isl_bool equal_params;
6221 if (!pma || !pa)
6222 goto error;
6223 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6224 if (equal_params < 0)
6225 goto error;
6226 if (equal_params)
6227 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6228 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6229 isl_pw_aff_check_named_params(pa) < 0)
6230 goto error;
6231 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6232 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6233 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6234 error:
6235 isl_pw_multi_aff_free(pma);
6236 isl_pw_aff_free(pa);
6237 return NULL;
6240 /* Do the parameters of "pa" match those of "space"?
6242 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6243 __isl_keep isl_space *space)
6245 isl_space *pa_space;
6246 isl_bool match;
6248 if (!pa || !space)
6249 return isl_bool_error;
6251 pa_space = isl_pw_aff_get_space(pa);
6253 match = isl_space_has_equal_params(space, pa_space);
6255 isl_space_free(pa_space);
6256 return match;
6259 /* Check that the domain space of "pa" matches "space".
6261 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6262 __isl_keep isl_space *space)
6264 isl_space *pa_space;
6265 isl_bool match;
6267 if (!pa || !space)
6268 return isl_stat_error;
6270 pa_space = isl_pw_aff_get_space(pa);
6272 match = isl_space_has_equal_params(space, pa_space);
6273 if (match < 0)
6274 goto error;
6275 if (!match)
6276 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6277 "parameters don't match", goto error);
6278 match = isl_space_tuple_is_equal(space, isl_dim_in,
6279 pa_space, isl_dim_in);
6280 if (match < 0)
6281 goto error;
6282 if (!match)
6283 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6284 "domains don't match", goto error);
6285 isl_space_free(pa_space);
6286 return isl_stat_ok;
6287 error:
6288 isl_space_free(pa_space);
6289 return isl_stat_error;
6292 #undef BASE
6293 #define BASE pw_aff
6294 #undef DOMBASE
6295 #define DOMBASE set
6297 #include <isl_multi_explicit_domain.c>
6298 #include <isl_multi_pw_aff_explicit_domain.c>
6299 #include <isl_multi_templ.c>
6300 #include <isl_multi_apply_set.c>
6301 #include <isl_multi_arith_templ.c>
6302 #include <isl_multi_bind_templ.c>
6303 #include <isl_multi_bind_domain_templ.c>
6304 #include <isl_multi_coalesce.c>
6305 #include <isl_multi_domain_templ.c>
6306 #include <isl_multi_dim_id_templ.c>
6307 #include <isl_multi_dims.c>
6308 #include <isl_multi_from_base_templ.c>
6309 #include <isl_multi_gist.c>
6310 #include <isl_multi_hash.c>
6311 #include <isl_multi_identity_templ.c>
6312 #include <isl_multi_align_set.c>
6313 #include <isl_multi_intersect.c>
6314 #include <isl_multi_move_dims_templ.c>
6315 #include <isl_multi_nan_templ.c>
6316 #include <isl_multi_param_templ.c>
6317 #include <isl_multi_product_templ.c>
6318 #include <isl_multi_splice_templ.c>
6319 #include <isl_multi_tuple_id_templ.c>
6320 #include <isl_multi_zero_templ.c>
6322 /* Does "mpa" have a non-trivial explicit domain?
6324 * The explicit domain, if present, is trivial if it represents
6325 * an (obviously) universe set.
6327 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6328 __isl_keep isl_multi_pw_aff *mpa)
6330 if (!mpa)
6331 return isl_bool_error;
6332 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6333 return isl_bool_false;
6334 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6337 /* Scale the elements of "pma" by the corresponding elements of "mv".
6339 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6340 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6342 int i;
6343 isl_bool equal_params;
6345 pma = isl_pw_multi_aff_cow(pma);
6346 if (!pma || !mv)
6347 goto error;
6348 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6349 mv->space, isl_dim_set))
6350 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6351 "spaces don't match", goto error);
6352 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6353 if (equal_params < 0)
6354 goto error;
6355 if (!equal_params) {
6356 pma = isl_pw_multi_aff_align_params(pma,
6357 isl_multi_val_get_space(mv));
6358 mv = isl_multi_val_align_params(mv,
6359 isl_pw_multi_aff_get_space(pma));
6360 if (!pma || !mv)
6361 goto error;
6364 for (i = 0; i < pma->n; ++i) {
6365 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6366 isl_multi_val_copy(mv));
6367 if (!pma->p[i].maff)
6368 goto error;
6371 isl_multi_val_free(mv);
6372 return pma;
6373 error:
6374 isl_multi_val_free(mv);
6375 isl_pw_multi_aff_free(pma);
6376 return NULL;
6379 /* This function is called for each entry of an isl_union_pw_multi_aff.
6380 * If the space of the entry matches that of data->mv,
6381 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6382 * Otherwise, return an empty isl_pw_multi_aff.
6384 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6385 __isl_take isl_pw_multi_aff *pma, void *user)
6387 isl_multi_val *mv = user;
6389 if (!pma)
6390 return NULL;
6391 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6392 mv->space, isl_dim_set)) {
6393 isl_space *space = isl_pw_multi_aff_get_space(pma);
6394 isl_pw_multi_aff_free(pma);
6395 return isl_pw_multi_aff_empty(space);
6398 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6401 /* Scale the elements of "upma" by the corresponding elements of "mv",
6402 * for those entries that match the space of "mv".
6404 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6405 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6407 upma = isl_union_pw_multi_aff_align_params(upma,
6408 isl_multi_val_get_space(mv));
6409 mv = isl_multi_val_align_params(mv,
6410 isl_union_pw_multi_aff_get_space(upma));
6411 if (!upma || !mv)
6412 goto error;
6414 return isl_union_pw_multi_aff_transform(upma,
6415 &union_pw_multi_aff_scale_multi_val_entry, mv);
6417 isl_multi_val_free(mv);
6418 return upma;
6419 error:
6420 isl_multi_val_free(mv);
6421 isl_union_pw_multi_aff_free(upma);
6422 return NULL;
6425 /* Construct and return a piecewise multi affine expression
6426 * in the given space with value zero in each of the output dimensions and
6427 * a universe domain.
6429 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6431 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6434 /* Construct and return a piecewise multi affine expression
6435 * that is equal to the given piecewise affine expression.
6437 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6438 __isl_take isl_pw_aff *pa)
6440 int i;
6441 isl_space *space;
6442 isl_pw_multi_aff *pma;
6444 if (!pa)
6445 return NULL;
6447 space = isl_pw_aff_get_space(pa);
6448 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6450 for (i = 0; i < pa->n; ++i) {
6451 isl_set *set;
6452 isl_multi_aff *ma;
6454 set = isl_set_copy(pa->p[i].set);
6455 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6456 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6459 isl_pw_aff_free(pa);
6460 return pma;
6463 /* Construct and return a piecewise multi affine expression
6464 * that is equal to the given multi piecewise affine expression
6465 * on the shared domain of the piecewise affine expressions,
6466 * in the special case of a 0D multi piecewise affine expression.
6468 * Create a piecewise multi affine expression with the explicit domain of
6469 * the 0D multi piecewise affine expression as domain.
6471 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6472 __isl_take isl_multi_pw_aff *mpa)
6474 isl_space *space;
6475 isl_set *dom;
6476 isl_multi_aff *ma;
6478 space = isl_multi_pw_aff_get_space(mpa);
6479 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6480 isl_multi_pw_aff_free(mpa);
6482 ma = isl_multi_aff_zero(space);
6483 return isl_pw_multi_aff_alloc(dom, ma);
6486 /* Construct and return a piecewise multi affine expression
6487 * that is equal to the given multi piecewise affine expression
6488 * on the shared domain of the piecewise affine expressions.
6490 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6491 __isl_take isl_multi_pw_aff *mpa)
6493 int i;
6494 isl_space *space;
6495 isl_pw_aff *pa;
6496 isl_pw_multi_aff *pma;
6498 if (!mpa)
6499 return NULL;
6501 if (mpa->n == 0)
6502 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6504 space = isl_multi_pw_aff_get_space(mpa);
6505 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6506 pma = isl_pw_multi_aff_from_pw_aff(pa);
6508 for (i = 1; i < mpa->n; ++i) {
6509 isl_pw_multi_aff *pma_i;
6511 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6512 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6513 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6516 pma = isl_pw_multi_aff_reset_space(pma, space);
6518 isl_multi_pw_aff_free(mpa);
6519 return pma;
6522 /* Construct and return a multi piecewise affine expression
6523 * that is equal to the given multi affine expression.
6525 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6526 __isl_take isl_multi_aff *ma)
6528 int i;
6529 isl_size n;
6530 isl_multi_pw_aff *mpa;
6532 n = isl_multi_aff_dim(ma, isl_dim_out);
6533 if (n < 0)
6534 ma = isl_multi_aff_free(ma);
6535 if (!ma)
6536 return NULL;
6538 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6540 for (i = 0; i < n; ++i) {
6541 isl_pw_aff *pa;
6543 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6544 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6547 isl_multi_aff_free(ma);
6548 return mpa;
6551 /* Construct and return a multi piecewise affine expression
6552 * that is equal to the given piecewise multi affine expression.
6554 * If the resulting multi piecewise affine expression has
6555 * an explicit domain, then assign it the domain of the input.
6556 * In other cases, the domain is stored in the individual elements.
6558 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6559 __isl_take isl_pw_multi_aff *pma)
6561 int i;
6562 isl_size n;
6563 isl_space *space;
6564 isl_multi_pw_aff *mpa;
6566 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6567 if (n < 0)
6568 pma = isl_pw_multi_aff_free(pma);
6569 space = isl_pw_multi_aff_get_space(pma);
6570 mpa = isl_multi_pw_aff_alloc(space);
6572 for (i = 0; i < n; ++i) {
6573 isl_pw_aff *pa;
6575 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6576 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6578 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6579 isl_set *dom;
6581 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6582 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6585 isl_pw_multi_aff_free(pma);
6586 return mpa;
6589 /* Do "pa1" and "pa2" represent the same function?
6591 * We first check if they are obviously equal.
6592 * If not, we convert them to maps and check if those are equal.
6594 * If "pa1" or "pa2" contain any NaNs, then they are considered
6595 * not to be the same. A NaN is not equal to anything, not even
6596 * to another NaN.
6598 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6599 __isl_keep isl_pw_aff *pa2)
6601 isl_bool equal;
6602 isl_bool has_nan;
6603 isl_map *map1, *map2;
6605 if (!pa1 || !pa2)
6606 return isl_bool_error;
6608 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6609 if (equal < 0 || equal)
6610 return equal;
6611 has_nan = either_involves_nan(pa1, pa2);
6612 if (has_nan < 0)
6613 return isl_bool_error;
6614 if (has_nan)
6615 return isl_bool_false;
6617 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6618 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6619 equal = isl_map_is_equal(map1, map2);
6620 isl_map_free(map1);
6621 isl_map_free(map2);
6623 return equal;
6626 /* Do "mpa1" and "mpa2" represent the same function?
6628 * Note that we cannot convert the entire isl_multi_pw_aff
6629 * to a map because the domains of the piecewise affine expressions
6630 * may not be the same.
6632 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6633 __isl_keep isl_multi_pw_aff *mpa2)
6635 int i;
6636 isl_bool equal, equal_params;
6638 if (!mpa1 || !mpa2)
6639 return isl_bool_error;
6641 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6642 if (equal_params < 0)
6643 return isl_bool_error;
6644 if (!equal_params) {
6645 if (!isl_space_has_named_params(mpa1->space))
6646 return isl_bool_false;
6647 if (!isl_space_has_named_params(mpa2->space))
6648 return isl_bool_false;
6649 mpa1 = isl_multi_pw_aff_copy(mpa1);
6650 mpa2 = isl_multi_pw_aff_copy(mpa2);
6651 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6652 isl_multi_pw_aff_get_space(mpa2));
6653 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6654 isl_multi_pw_aff_get_space(mpa1));
6655 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6656 isl_multi_pw_aff_free(mpa1);
6657 isl_multi_pw_aff_free(mpa2);
6658 return equal;
6661 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6662 if (equal < 0 || !equal)
6663 return equal;
6665 for (i = 0; i < mpa1->n; ++i) {
6666 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6667 if (equal < 0 || !equal)
6668 return equal;
6671 return isl_bool_true;
6674 /* Do "pma1" and "pma2" represent the same function?
6676 * First check if they are obviously equal.
6677 * If not, then convert them to maps and check if those are equal.
6679 * If "pa1" or "pa2" contain any NaNs, then they are considered
6680 * not to be the same. A NaN is not equal to anything, not even
6681 * to another NaN.
6683 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6684 __isl_keep isl_pw_multi_aff *pma2)
6686 isl_bool equal;
6687 isl_bool has_nan;
6688 isl_map *map1, *map2;
6690 if (!pma1 || !pma2)
6691 return isl_bool_error;
6693 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6694 if (equal < 0 || equal)
6695 return equal;
6696 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6697 if (has_nan >= 0 && !has_nan)
6698 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6699 if (has_nan < 0 || has_nan)
6700 return isl_bool_not(has_nan);
6702 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6703 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6704 equal = isl_map_is_equal(map1, map2);
6705 isl_map_free(map1);
6706 isl_map_free(map2);
6708 return equal;
6711 /* Compute the pullback of "mpa" by the function represented by "ma".
6712 * In other words, plug in "ma" in "mpa".
6714 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6716 * If "mpa" has an explicit domain, then it is this domain
6717 * that needs to undergo a pullback, i.e., a preimage.
6719 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6720 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6722 int i;
6723 isl_space *space = NULL;
6725 mpa = isl_multi_pw_aff_cow(mpa);
6726 if (!mpa || !ma)
6727 goto error;
6729 space = isl_space_join(isl_multi_aff_get_space(ma),
6730 isl_multi_pw_aff_get_space(mpa));
6731 if (!space)
6732 goto error;
6734 for (i = 0; i < mpa->n; ++i) {
6735 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6736 isl_multi_aff_copy(ma));
6737 if (!mpa->u.p[i])
6738 goto error;
6740 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6741 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6742 isl_multi_aff_copy(ma));
6743 if (!mpa->u.dom)
6744 goto error;
6747 isl_multi_aff_free(ma);
6748 isl_space_free(mpa->space);
6749 mpa->space = space;
6750 return mpa;
6751 error:
6752 isl_space_free(space);
6753 isl_multi_pw_aff_free(mpa);
6754 isl_multi_aff_free(ma);
6755 return NULL;
6758 /* Compute the pullback of "mpa" by the function represented by "ma".
6759 * In other words, plug in "ma" in "mpa".
6761 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6762 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6764 isl_bool equal_params;
6766 if (!mpa || !ma)
6767 goto error;
6768 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6769 if (equal_params < 0)
6770 goto error;
6771 if (equal_params)
6772 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6773 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6774 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6775 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6776 error:
6777 isl_multi_pw_aff_free(mpa);
6778 isl_multi_aff_free(ma);
6779 return NULL;
6782 /* Compute the pullback of "mpa" by the function represented by "pma".
6783 * In other words, plug in "pma" in "mpa".
6785 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6787 * If "mpa" has an explicit domain, then it is this domain
6788 * that needs to undergo a pullback, i.e., a preimage.
6790 static __isl_give isl_multi_pw_aff *
6791 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6792 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6794 int i;
6795 isl_space *space = NULL;
6797 mpa = isl_multi_pw_aff_cow(mpa);
6798 if (!mpa || !pma)
6799 goto error;
6801 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6802 isl_multi_pw_aff_get_space(mpa));
6804 for (i = 0; i < mpa->n; ++i) {
6805 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6806 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6807 if (!mpa->u.p[i])
6808 goto error;
6810 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6811 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6812 isl_pw_multi_aff_copy(pma));
6813 if (!mpa->u.dom)
6814 goto error;
6817 isl_pw_multi_aff_free(pma);
6818 isl_space_free(mpa->space);
6819 mpa->space = space;
6820 return mpa;
6821 error:
6822 isl_space_free(space);
6823 isl_multi_pw_aff_free(mpa);
6824 isl_pw_multi_aff_free(pma);
6825 return NULL;
6828 /* Compute the pullback of "mpa" by the function represented by "pma".
6829 * In other words, plug in "pma" in "mpa".
6831 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6832 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6834 isl_bool equal_params;
6836 if (!mpa || !pma)
6837 goto error;
6838 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6839 if (equal_params < 0)
6840 goto error;
6841 if (equal_params)
6842 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6843 mpa = isl_multi_pw_aff_align_params(mpa,
6844 isl_pw_multi_aff_get_space(pma));
6845 pma = isl_pw_multi_aff_align_params(pma,
6846 isl_multi_pw_aff_get_space(mpa));
6847 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6848 error:
6849 isl_multi_pw_aff_free(mpa);
6850 isl_pw_multi_aff_free(pma);
6851 return NULL;
6854 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6855 * with the domain of "aff". The domain of the result is the same
6856 * as that of "mpa".
6857 * "mpa" and "aff" are assumed to have been aligned.
6859 * We first extract the parametric constant from "aff", defined
6860 * over the correct domain.
6861 * Then we add the appropriate combinations of the members of "mpa".
6862 * Finally, we add the integer divisions through recursive calls.
6864 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6865 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6867 int i;
6868 isl_size n_in, n_div, n_mpa_in;
6869 isl_space *space;
6870 isl_val *v;
6871 isl_pw_aff *pa;
6872 isl_aff *tmp;
6874 n_in = isl_aff_dim(aff, isl_dim_in);
6875 n_div = isl_aff_dim(aff, isl_dim_div);
6876 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
6877 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
6878 goto error;
6880 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6881 tmp = isl_aff_copy(aff);
6882 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6883 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6884 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
6885 tmp = isl_aff_reset_domain_space(tmp, space);
6886 pa = isl_pw_aff_from_aff(tmp);
6888 for (i = 0; i < n_in; ++i) {
6889 isl_pw_aff *pa_i;
6891 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6892 continue;
6893 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6894 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6895 pa_i = isl_pw_aff_scale_val(pa_i, v);
6896 pa = isl_pw_aff_add(pa, pa_i);
6899 for (i = 0; i < n_div; ++i) {
6900 isl_aff *div;
6901 isl_pw_aff *pa_i;
6903 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6904 continue;
6905 div = isl_aff_get_div(aff, i);
6906 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6907 isl_multi_pw_aff_copy(mpa), div);
6908 pa_i = isl_pw_aff_floor(pa_i);
6909 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6910 pa_i = isl_pw_aff_scale_val(pa_i, v);
6911 pa = isl_pw_aff_add(pa, pa_i);
6914 isl_multi_pw_aff_free(mpa);
6915 isl_aff_free(aff);
6917 return pa;
6918 error:
6919 isl_multi_pw_aff_free(mpa);
6920 isl_aff_free(aff);
6921 return NULL;
6924 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6925 * with the domain of "aff". The domain of the result is the same
6926 * as that of "mpa".
6928 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6929 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6931 isl_bool equal_params;
6933 if (!aff || !mpa)
6934 goto error;
6935 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6936 if (equal_params < 0)
6937 goto error;
6938 if (equal_params)
6939 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6941 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6942 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6944 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6945 error:
6946 isl_aff_free(aff);
6947 isl_multi_pw_aff_free(mpa);
6948 return NULL;
6951 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6952 * with the domain of "pa". The domain of the result is the same
6953 * as that of "mpa".
6954 * "mpa" and "pa" are assumed to have been aligned.
6956 * We consider each piece in turn. Note that the domains of the
6957 * pieces are assumed to be disjoint and they remain disjoint
6958 * after taking the preimage (over the same function).
6960 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6961 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6963 isl_space *space;
6964 isl_pw_aff *res;
6965 int i;
6967 if (!mpa || !pa)
6968 goto error;
6970 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6971 isl_pw_aff_get_space(pa));
6972 res = isl_pw_aff_empty(space);
6974 for (i = 0; i < pa->n; ++i) {
6975 isl_pw_aff *pa_i;
6976 isl_set *domain;
6978 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6979 isl_multi_pw_aff_copy(mpa),
6980 isl_aff_copy(pa->p[i].aff));
6981 domain = isl_set_copy(pa->p[i].set);
6982 domain = isl_set_preimage_multi_pw_aff(domain,
6983 isl_multi_pw_aff_copy(mpa));
6984 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6985 res = isl_pw_aff_add_disjoint(res, pa_i);
6988 isl_pw_aff_free(pa);
6989 isl_multi_pw_aff_free(mpa);
6990 return res;
6991 error:
6992 isl_pw_aff_free(pa);
6993 isl_multi_pw_aff_free(mpa);
6994 return NULL;
6997 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6998 * with the domain of "pa". The domain of the result is the same
6999 * as that of "mpa".
7001 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7002 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7004 isl_bool equal_params;
7006 if (!pa || !mpa)
7007 goto error;
7008 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7009 if (equal_params < 0)
7010 goto error;
7011 if (equal_params)
7012 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7014 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7015 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7017 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7018 error:
7019 isl_pw_aff_free(pa);
7020 isl_multi_pw_aff_free(mpa);
7021 return NULL;
7024 /* Compute the pullback of "pa" by the function represented by "mpa".
7025 * In other words, plug in "mpa" in "pa".
7026 * "pa" and "mpa" are assumed to have been aligned.
7028 * The pullback is computed by applying "pa" to "mpa".
7030 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7031 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7033 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7036 /* Compute the pullback of "pa" by the function represented by "mpa".
7037 * In other words, plug in "mpa" in "pa".
7039 * The pullback is computed by applying "pa" to "mpa".
7041 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7042 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7044 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7047 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7048 * In other words, plug in "mpa2" in "mpa1".
7050 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7052 * We pullback each member of "mpa1" in turn.
7054 * If "mpa1" has an explicit domain, then it is this domain
7055 * that needs to undergo a pullback instead, i.e., a preimage.
7057 static __isl_give isl_multi_pw_aff *
7058 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7059 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7061 int i;
7062 isl_space *space = NULL;
7064 mpa1 = isl_multi_pw_aff_cow(mpa1);
7065 if (!mpa1 || !mpa2)
7066 goto error;
7068 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7069 isl_multi_pw_aff_get_space(mpa1));
7071 for (i = 0; i < mpa1->n; ++i) {
7072 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7073 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7074 if (!mpa1->u.p[i])
7075 goto error;
7078 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7079 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7080 isl_multi_pw_aff_copy(mpa2));
7081 if (!mpa1->u.dom)
7082 goto error;
7084 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7086 isl_multi_pw_aff_free(mpa2);
7087 return mpa1;
7088 error:
7089 isl_space_free(space);
7090 isl_multi_pw_aff_free(mpa1);
7091 isl_multi_pw_aff_free(mpa2);
7092 return NULL;
7095 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7096 * In other words, plug in "mpa2" in "mpa1".
7098 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7099 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7101 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7102 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7105 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7106 * of "mpa1" and "mpa2" live in the same space, construct map space
7107 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7108 * with this map space as extract argument.
7110 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7111 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7112 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7113 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7115 int match;
7116 isl_space *space1, *space2;
7117 isl_map *res;
7119 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7120 isl_multi_pw_aff_get_space(mpa2));
7121 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7122 isl_multi_pw_aff_get_space(mpa1));
7123 if (!mpa1 || !mpa2)
7124 goto error;
7125 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7126 mpa2->space, isl_dim_out);
7127 if (match < 0)
7128 goto error;
7129 if (!match)
7130 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7131 "range spaces don't match", goto error);
7132 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7133 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7134 space1 = isl_space_map_from_domain_and_range(space1, space2);
7136 res = order(mpa1, mpa2, space1);
7137 isl_multi_pw_aff_free(mpa1);
7138 isl_multi_pw_aff_free(mpa2);
7139 return res;
7140 error:
7141 isl_multi_pw_aff_free(mpa1);
7142 isl_multi_pw_aff_free(mpa2);
7143 return NULL;
7146 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7147 * where the function values are equal. "space" is the space of the result.
7148 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7150 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7151 * in the sequences are equal.
7153 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7154 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7155 __isl_take isl_space *space)
7157 int i;
7158 isl_size n;
7159 isl_map *res;
7161 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7162 if (n < 0)
7163 space = isl_space_free(space);
7164 res = isl_map_universe(space);
7166 for (i = 0; i < n; ++i) {
7167 isl_pw_aff *pa1, *pa2;
7168 isl_map *map;
7170 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7171 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7172 map = isl_pw_aff_eq_map(pa1, pa2);
7173 res = isl_map_intersect(res, map);
7176 return res;
7179 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7180 * where the function values are equal.
7182 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7183 __isl_take isl_multi_pw_aff *mpa2)
7185 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7186 &isl_multi_pw_aff_eq_map_on_space);
7189 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7190 * where the function values of "mpa1" is lexicographically satisfies "base"
7191 * compared to that of "mpa2". "space" is the space of the result.
7192 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7194 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7195 * if its i-th element satisfies "base" when compared to
7196 * the i-th element of "mpa2" while all previous elements are
7197 * pairwise equal.
7199 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7200 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7201 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7202 __isl_take isl_pw_aff *pa2),
7203 __isl_take isl_space *space)
7205 int i;
7206 isl_size n;
7207 isl_map *res, *rest;
7209 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7210 if (n < 0)
7211 space = isl_space_free(space);
7212 res = isl_map_empty(isl_space_copy(space));
7213 rest = isl_map_universe(space);
7215 for (i = 0; i < n; ++i) {
7216 isl_pw_aff *pa1, *pa2;
7217 isl_map *map;
7219 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7220 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7221 map = base(pa1, pa2);
7222 map = isl_map_intersect(map, isl_map_copy(rest));
7223 res = isl_map_union(res, map);
7225 if (i == n - 1)
7226 continue;
7228 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7229 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7230 map = isl_pw_aff_eq_map(pa1, pa2);
7231 rest = isl_map_intersect(rest, map);
7234 isl_map_free(rest);
7235 return res;
7238 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7239 * where the function value of "mpa1" is lexicographically less than that
7240 * of "mpa2". "space" is the space of the result.
7241 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7243 * "mpa1" is less than "mpa2" if its i-th element is smaller
7244 * than the i-th element of "mpa2" while all previous elements are
7245 * pairwise equal.
7247 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7248 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7249 __isl_take isl_space *space)
7251 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7252 &isl_pw_aff_lt_map, space);
7255 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7256 * where the function value of "mpa1" is lexicographically less than that
7257 * of "mpa2".
7259 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7260 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7262 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7263 &isl_multi_pw_aff_lex_lt_map_on_space);
7266 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7267 * where the function value of "mpa1" is lexicographically greater than that
7268 * of "mpa2". "space" is the space of the result.
7269 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7271 * "mpa1" is greater than "mpa2" if its i-th element is greater
7272 * than the i-th element of "mpa2" while all previous elements are
7273 * pairwise equal.
7275 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7276 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7277 __isl_take isl_space *space)
7279 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7280 &isl_pw_aff_gt_map, space);
7283 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7284 * where the function value of "mpa1" is lexicographically greater than that
7285 * of "mpa2".
7287 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7288 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7290 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7291 &isl_multi_pw_aff_lex_gt_map_on_space);
7294 /* Compare two isl_affs.
7296 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7297 * than "aff2" and 0 if they are equal.
7299 * The order is fairly arbitrary. We do consider expressions that only involve
7300 * earlier dimensions as "smaller".
7302 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7304 int cmp;
7305 int last1, last2;
7307 if (aff1 == aff2)
7308 return 0;
7310 if (!aff1)
7311 return -1;
7312 if (!aff2)
7313 return 1;
7315 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7316 if (cmp != 0)
7317 return cmp;
7319 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7320 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7321 if (last1 != last2)
7322 return last1 - last2;
7324 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7327 /* Compare two isl_pw_affs.
7329 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7330 * than "pa2" and 0 if they are equal.
7332 * The order is fairly arbitrary. We do consider expressions that only involve
7333 * earlier dimensions as "smaller".
7335 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7336 __isl_keep isl_pw_aff *pa2)
7338 int i;
7339 int cmp;
7341 if (pa1 == pa2)
7342 return 0;
7344 if (!pa1)
7345 return -1;
7346 if (!pa2)
7347 return 1;
7349 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7350 if (cmp != 0)
7351 return cmp;
7353 if (pa1->n != pa2->n)
7354 return pa1->n - pa2->n;
7356 for (i = 0; i < pa1->n; ++i) {
7357 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7358 if (cmp != 0)
7359 return cmp;
7360 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7361 if (cmp != 0)
7362 return cmp;
7365 return 0;
7368 /* Return a piecewise affine expression that is equal to "v" on "domain".
7370 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7371 __isl_take isl_val *v)
7373 isl_space *space;
7374 isl_local_space *ls;
7375 isl_aff *aff;
7377 space = isl_set_get_space(domain);
7378 ls = isl_local_space_from_space(space);
7379 aff = isl_aff_val_on_domain(ls, v);
7381 return isl_pw_aff_alloc(domain, aff);
7384 /* Return a multi affine expression that is equal to "mv" on domain
7385 * space "space".
7387 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7388 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7390 int i;
7391 isl_size n;
7392 isl_space *space2;
7393 isl_local_space *ls;
7394 isl_multi_aff *ma;
7396 n = isl_multi_val_dim(mv, isl_dim_set);
7397 if (!space || n < 0)
7398 goto error;
7400 space2 = isl_multi_val_get_space(mv);
7401 space2 = isl_space_align_params(space2, isl_space_copy(space));
7402 space = isl_space_align_params(space, isl_space_copy(space2));
7403 space = isl_space_map_from_domain_and_range(space, space2);
7404 ma = isl_multi_aff_alloc(isl_space_copy(space));
7405 ls = isl_local_space_from_space(isl_space_domain(space));
7406 for (i = 0; i < n; ++i) {
7407 isl_val *v;
7408 isl_aff *aff;
7410 v = isl_multi_val_get_val(mv, i);
7411 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7412 ma = isl_multi_aff_set_aff(ma, i, aff);
7414 isl_local_space_free(ls);
7416 isl_multi_val_free(mv);
7417 return ma;
7418 error:
7419 isl_space_free(space);
7420 isl_multi_val_free(mv);
7421 return NULL;
7424 /* Return a piecewise multi-affine expression
7425 * that is equal to "mv" on "domain".
7427 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7428 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7430 isl_space *space;
7431 isl_multi_aff *ma;
7433 space = isl_set_get_space(domain);
7434 ma = isl_multi_aff_multi_val_on_space(space, mv);
7436 return isl_pw_multi_aff_alloc(domain, ma);
7439 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7440 * mv is the value that should be attained on each domain set
7441 * res collects the results
7443 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7444 isl_multi_val *mv;
7445 isl_union_pw_multi_aff *res;
7448 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7449 * and add it to data->res.
7451 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7452 void *user)
7454 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7455 isl_pw_multi_aff *pma;
7456 isl_multi_val *mv;
7458 mv = isl_multi_val_copy(data->mv);
7459 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7460 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7462 return data->res ? isl_stat_ok : isl_stat_error;
7465 /* Return a union piecewise multi-affine expression
7466 * that is equal to "mv" on "domain".
7468 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7469 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7471 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7472 isl_space *space;
7474 space = isl_union_set_get_space(domain);
7475 data.res = isl_union_pw_multi_aff_empty(space);
7476 data.mv = mv;
7477 if (isl_union_set_foreach_set(domain,
7478 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7479 data.res = isl_union_pw_multi_aff_free(data.res);
7480 isl_union_set_free(domain);
7481 isl_multi_val_free(mv);
7482 return data.res;
7485 /* Compute the pullback of data->pma by the function represented by "pma2",
7486 * provided the spaces match, and add the results to data->res.
7488 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7490 struct isl_union_pw_multi_aff_bin_data *data = user;
7492 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7493 pma2->dim, isl_dim_out)) {
7494 isl_pw_multi_aff_free(pma2);
7495 return isl_stat_ok;
7498 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7499 isl_pw_multi_aff_copy(data->pma), pma2);
7501 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7502 if (!data->res)
7503 return isl_stat_error;
7505 return isl_stat_ok;
7508 /* Compute the pullback of "upma1" by the function represented by "upma2".
7510 __isl_give isl_union_pw_multi_aff *
7511 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7512 __isl_take isl_union_pw_multi_aff *upma1,
7513 __isl_take isl_union_pw_multi_aff *upma2)
7515 return bin_op(upma1, upma2, &pullback_entry);
7518 /* Check that the domain space of "upa" matches "space".
7520 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7521 * can in principle never fail since the space "space" is that
7522 * of the isl_multi_union_pw_aff and is a set space such that
7523 * there is no domain space to match.
7525 * We check the parameters and double-check that "space" is
7526 * indeed that of a set.
7528 static isl_stat isl_union_pw_aff_check_match_domain_space(
7529 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7531 isl_space *upa_space;
7532 isl_bool match;
7534 if (!upa || !space)
7535 return isl_stat_error;
7537 match = isl_space_is_set(space);
7538 if (match < 0)
7539 return isl_stat_error;
7540 if (!match)
7541 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7542 "expecting set space", return isl_stat_error);
7544 upa_space = isl_union_pw_aff_get_space(upa);
7545 match = isl_space_has_equal_params(space, upa_space);
7546 if (match < 0)
7547 goto error;
7548 if (!match)
7549 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7550 "parameters don't match", goto error);
7552 isl_space_free(upa_space);
7553 return isl_stat_ok;
7554 error:
7555 isl_space_free(upa_space);
7556 return isl_stat_error;
7559 /* Do the parameters of "upa" match those of "space"?
7561 static isl_bool isl_union_pw_aff_matching_params(
7562 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7564 isl_space *upa_space;
7565 isl_bool match;
7567 if (!upa || !space)
7568 return isl_bool_error;
7570 upa_space = isl_union_pw_aff_get_space(upa);
7572 match = isl_space_has_equal_params(space, upa_space);
7574 isl_space_free(upa_space);
7575 return match;
7578 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7579 * space represents the new parameters.
7580 * res collects the results.
7582 struct isl_union_pw_aff_reset_params_data {
7583 isl_space *space;
7584 isl_union_pw_aff *res;
7587 /* Replace the parameters of "pa" by data->space and
7588 * add the result to data->res.
7590 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7592 struct isl_union_pw_aff_reset_params_data *data = user;
7593 isl_space *space;
7595 space = isl_pw_aff_get_space(pa);
7596 space = isl_space_replace_params(space, data->space);
7597 pa = isl_pw_aff_reset_space(pa, space);
7598 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7600 return data->res ? isl_stat_ok : isl_stat_error;
7603 /* Replace the domain space of "upa" by "space".
7604 * Since a union expression does not have a (single) domain space,
7605 * "space" is necessarily a parameter space.
7607 * Since the order and the names of the parameters determine
7608 * the hash value, we need to create a new hash table.
7610 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7611 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7613 struct isl_union_pw_aff_reset_params_data data = { space };
7614 isl_bool match;
7616 match = isl_union_pw_aff_matching_params(upa, space);
7617 if (match < 0)
7618 upa = isl_union_pw_aff_free(upa);
7619 else if (match) {
7620 isl_space_free(space);
7621 return upa;
7624 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7625 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7626 data.res = isl_union_pw_aff_free(data.res);
7628 isl_union_pw_aff_free(upa);
7629 isl_space_free(space);
7630 return data.res;
7633 /* Return the floor of "pa".
7635 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7637 return isl_pw_aff_floor(pa);
7640 /* Given f, return floor(f).
7642 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7643 __isl_take isl_union_pw_aff *upa)
7645 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7648 /* Compute
7650 * upa mod m = upa - m * floor(upa/m)
7652 * with m an integer value.
7654 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7655 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7657 isl_union_pw_aff *res;
7659 if (!upa || !m)
7660 goto error;
7662 if (!isl_val_is_int(m))
7663 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7664 "expecting integer modulo", goto error);
7665 if (!isl_val_is_pos(m))
7666 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7667 "expecting positive modulo", goto error);
7669 res = isl_union_pw_aff_copy(upa);
7670 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7671 upa = isl_union_pw_aff_floor(upa);
7672 upa = isl_union_pw_aff_scale_val(upa, m);
7673 res = isl_union_pw_aff_sub(res, upa);
7675 return res;
7676 error:
7677 isl_val_free(m);
7678 isl_union_pw_aff_free(upa);
7679 return NULL;
7682 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7683 * pos is the output position that needs to be extracted.
7684 * res collects the results.
7686 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7687 int pos;
7688 isl_union_pw_aff *res;
7691 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7692 * (assuming it has such a dimension) and add it to data->res.
7694 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7696 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7697 isl_size n_out;
7698 isl_pw_aff *pa;
7700 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7701 if (n_out < 0)
7702 return isl_stat_error;
7703 if (data->pos >= n_out) {
7704 isl_pw_multi_aff_free(pma);
7705 return isl_stat_ok;
7708 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7709 isl_pw_multi_aff_free(pma);
7711 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7713 return data->res ? isl_stat_ok : isl_stat_error;
7716 /* Extract an isl_union_pw_aff corresponding to
7717 * output dimension "pos" of "upma".
7719 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7720 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7722 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7723 isl_space *space;
7725 if (!upma)
7726 return NULL;
7728 if (pos < 0)
7729 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7730 "cannot extract at negative position", return NULL);
7732 space = isl_union_pw_multi_aff_get_space(upma);
7733 data.res = isl_union_pw_aff_empty(space);
7734 data.pos = pos;
7735 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7736 &get_union_pw_aff, &data) < 0)
7737 data.res = isl_union_pw_aff_free(data.res);
7739 return data.res;
7742 /* Return a union piecewise affine expression
7743 * that is equal to "aff" on "domain".
7745 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7746 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7748 isl_pw_aff *pa;
7750 pa = isl_pw_aff_from_aff(aff);
7751 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7754 /* Return a union piecewise affine expression
7755 * that is equal to the parameter identified by "id" on "domain".
7757 * Make sure the parameter appears in the space passed to
7758 * isl_aff_param_on_domain_space_id.
7760 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7761 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7763 isl_space *space;
7764 isl_aff *aff;
7766 space = isl_union_set_get_space(domain);
7767 space = isl_space_add_param_id(space, isl_id_copy(id));
7768 aff = isl_aff_param_on_domain_space_id(space, id);
7769 return isl_union_pw_aff_aff_on_domain(domain, aff);
7772 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7773 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7774 * needs to attain.
7775 * "res" collects the results.
7777 struct isl_union_pw_aff_pw_aff_on_domain_data {
7778 isl_pw_aff *pa;
7779 isl_union_pw_aff *res;
7782 /* Construct a piecewise affine expression that is equal to data->pa
7783 * on "domain" and add the result to data->res.
7785 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7787 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7788 isl_pw_aff *pa;
7789 isl_size dim;
7791 pa = isl_pw_aff_copy(data->pa);
7792 dim = isl_set_dim(domain, isl_dim_set);
7793 if (dim < 0)
7794 pa = isl_pw_aff_free(pa);
7795 pa = isl_pw_aff_from_range(pa);
7796 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7797 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7798 pa = isl_pw_aff_intersect_domain(pa, domain);
7799 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7801 return data->res ? isl_stat_ok : isl_stat_error;
7804 /* Return a union piecewise affine expression
7805 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7806 * have been aligned.
7808 * Construct an isl_pw_aff on each of the sets in "domain" and
7809 * collect the results.
7811 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7812 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7814 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7815 isl_space *space;
7817 space = isl_union_set_get_space(domain);
7818 data.res = isl_union_pw_aff_empty(space);
7819 data.pa = pa;
7820 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7821 data.res = isl_union_pw_aff_free(data.res);
7822 isl_union_set_free(domain);
7823 isl_pw_aff_free(pa);
7824 return data.res;
7827 /* Return a union piecewise affine expression
7828 * that is equal to "pa" on "domain".
7830 * Check that "pa" is a parametric expression,
7831 * align the parameters if needed and call
7832 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7834 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7835 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7837 isl_bool is_set;
7838 isl_bool equal_params;
7839 isl_space *domain_space, *pa_space;
7841 pa_space = isl_pw_aff_peek_space(pa);
7842 is_set = isl_space_is_set(pa_space);
7843 if (is_set < 0)
7844 goto error;
7845 if (!is_set)
7846 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7847 "expecting parametric expression", goto error);
7849 domain_space = isl_union_set_get_space(domain);
7850 pa_space = isl_pw_aff_get_space(pa);
7851 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7852 if (equal_params >= 0 && !equal_params) {
7853 isl_space *space;
7855 space = isl_space_align_params(domain_space, pa_space);
7856 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7857 domain = isl_union_set_align_params(domain, space);
7858 } else {
7859 isl_space_free(domain_space);
7860 isl_space_free(pa_space);
7863 if (equal_params < 0)
7864 goto error;
7865 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7866 error:
7867 isl_union_set_free(domain);
7868 isl_pw_aff_free(pa);
7869 return NULL;
7872 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7873 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7874 * "res" collects the results.
7876 struct isl_union_pw_aff_val_on_domain_data {
7877 isl_val *v;
7878 isl_union_pw_aff *res;
7881 /* Construct a piecewise affine expression that is equal to data->v
7882 * on "domain" and add the result to data->res.
7884 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7886 struct isl_union_pw_aff_val_on_domain_data *data = user;
7887 isl_pw_aff *pa;
7888 isl_val *v;
7890 v = isl_val_copy(data->v);
7891 pa = isl_pw_aff_val_on_domain(domain, v);
7892 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7894 return data->res ? isl_stat_ok : isl_stat_error;
7897 /* Return a union piecewise affine expression
7898 * that is equal to "v" on "domain".
7900 * Construct an isl_pw_aff on each of the sets in "domain" and
7901 * collect the results.
7903 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7904 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7906 struct isl_union_pw_aff_val_on_domain_data data;
7907 isl_space *space;
7909 space = isl_union_set_get_space(domain);
7910 data.res = isl_union_pw_aff_empty(space);
7911 data.v = v;
7912 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7913 data.res = isl_union_pw_aff_free(data.res);
7914 isl_union_set_free(domain);
7915 isl_val_free(v);
7916 return data.res;
7919 /* Construct a piecewise multi affine expression
7920 * that is equal to "pa" and add it to upma.
7922 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7923 void *user)
7925 isl_union_pw_multi_aff **upma = user;
7926 isl_pw_multi_aff *pma;
7928 pma = isl_pw_multi_aff_from_pw_aff(pa);
7929 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7931 return *upma ? isl_stat_ok : isl_stat_error;
7934 /* Construct and return a union piecewise multi affine expression
7935 * that is equal to the given union piecewise affine expression.
7937 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7938 __isl_take isl_union_pw_aff *upa)
7940 isl_space *space;
7941 isl_union_pw_multi_aff *upma;
7943 if (!upa)
7944 return NULL;
7946 space = isl_union_pw_aff_get_space(upa);
7947 upma = isl_union_pw_multi_aff_empty(space);
7949 if (isl_union_pw_aff_foreach_pw_aff(upa,
7950 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7951 upma = isl_union_pw_multi_aff_free(upma);
7953 isl_union_pw_aff_free(upa);
7954 return upma;
7957 /* Compute the set of elements in the domain of "pa" where it is zero and
7958 * add this set to "uset".
7960 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7962 isl_union_set **uset = (isl_union_set **)user;
7964 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7966 return *uset ? isl_stat_ok : isl_stat_error;
7969 /* Return a union set containing those elements in the domain
7970 * of "upa" where it is zero.
7972 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7973 __isl_take isl_union_pw_aff *upa)
7975 isl_union_set *zero;
7977 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7978 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7979 zero = isl_union_set_free(zero);
7981 isl_union_pw_aff_free(upa);
7982 return zero;
7985 /* Internal data structure for isl_union_pw_aff_bind_id,
7986 * storing the parameter that needs to be bound and
7987 * the accumulated results.
7989 struct isl_bind_id_data {
7990 isl_id *id;
7991 isl_union_set *bound;
7994 /* Bind the piecewise affine function "pa" to the parameter data->id,
7995 * adding the resulting elements in the domain where the expression
7996 * is equal to the parameter to data->bound.
7998 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8000 struct isl_bind_id_data *data = user;
8001 isl_set *bound;
8003 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8004 data->bound = isl_union_set_add_set(data->bound, bound);
8006 return data->bound ? isl_stat_ok : isl_stat_error;
8009 /* Bind the union piecewise affine function "upa" to the parameter "id",
8010 * returning the elements in the domain where the expression
8011 * is equal to the parameter.
8013 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8014 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8016 struct isl_bind_id_data data = { id };
8018 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8019 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8020 data.bound = isl_union_set_free(data.bound);
8022 isl_union_pw_aff_free(upa);
8023 isl_id_free(id);
8024 return data.bound;
8027 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8028 * upma is the function that is plugged in.
8029 * pa is the current part of the function in which upma is plugged in.
8030 * res collects the results.
8032 struct isl_union_pw_aff_pullback_upma_data {
8033 isl_union_pw_multi_aff *upma;
8034 isl_pw_aff *pa;
8035 isl_union_pw_aff *res;
8038 /* Check if "pma" can be plugged into data->pa.
8039 * If so, perform the pullback and add the result to data->res.
8041 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8043 struct isl_union_pw_aff_pullback_upma_data *data = user;
8044 isl_pw_aff *pa;
8046 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8047 pma->dim, isl_dim_out)) {
8048 isl_pw_multi_aff_free(pma);
8049 return isl_stat_ok;
8052 pa = isl_pw_aff_copy(data->pa);
8053 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8055 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8057 return data->res ? isl_stat_ok : isl_stat_error;
8060 /* Check if any of the elements of data->upma can be plugged into pa,
8061 * add if so add the result to data->res.
8063 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8065 struct isl_union_pw_aff_pullback_upma_data *data = user;
8066 isl_stat r;
8068 data->pa = pa;
8069 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8070 &pa_pb_pma, data);
8071 isl_pw_aff_free(pa);
8073 return r;
8076 /* Compute the pullback of "upa" by the function represented by "upma".
8077 * In other words, plug in "upma" in "upa". The result contains
8078 * expressions defined over the domain space of "upma".
8080 * Run over all pairs of elements in "upa" and "upma", perform
8081 * the pullback when appropriate and collect the results.
8082 * If the hash value were based on the domain space rather than
8083 * the function space, then we could run through all elements
8084 * of "upma" and directly pick out the corresponding element of "upa".
8086 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8087 __isl_take isl_union_pw_aff *upa,
8088 __isl_take isl_union_pw_multi_aff *upma)
8090 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8091 isl_space *space;
8093 space = isl_union_pw_multi_aff_get_space(upma);
8094 upa = isl_union_pw_aff_align_params(upa, space);
8095 space = isl_union_pw_aff_get_space(upa);
8096 upma = isl_union_pw_multi_aff_align_params(upma, space);
8098 if (!upa || !upma)
8099 goto error;
8101 data.upma = upma;
8102 data.res = isl_union_pw_aff_alloc_same_size(upa);
8103 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8104 data.res = isl_union_pw_aff_free(data.res);
8106 isl_union_pw_aff_free(upa);
8107 isl_union_pw_multi_aff_free(upma);
8108 return data.res;
8109 error:
8110 isl_union_pw_aff_free(upa);
8111 isl_union_pw_multi_aff_free(upma);
8112 return NULL;
8115 #undef BASE
8116 #define BASE union_pw_aff
8117 #undef DOMBASE
8118 #define DOMBASE union_set
8120 #include <isl_multi_explicit_domain.c>
8121 #include <isl_multi_union_pw_aff_explicit_domain.c>
8122 #include <isl_multi_templ.c>
8123 #include <isl_multi_apply_set.c>
8124 #include <isl_multi_apply_union_set.c>
8125 #include <isl_multi_arith_templ.c>
8126 #include <isl_multi_bind_templ.c>
8127 #include <isl_multi_coalesce.c>
8128 #include <isl_multi_dim_id_templ.c>
8129 #include <isl_multi_floor.c>
8130 #include <isl_multi_from_base_templ.c>
8131 #include <isl_multi_gist.c>
8132 #include <isl_multi_align_set.c>
8133 #include <isl_multi_align_union_set.c>
8134 #include <isl_multi_intersect.c>
8135 #include <isl_multi_nan_templ.c>
8136 #include <isl_multi_tuple_id_templ.c>
8138 /* Does "mupa" have a non-trivial explicit domain?
8140 * The explicit domain, if present, is trivial if it represents
8141 * an (obviously) universe parameter set.
8143 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8144 __isl_keep isl_multi_union_pw_aff *mupa)
8146 isl_bool is_params, trivial;
8147 isl_set *set;
8149 if (!mupa)
8150 return isl_bool_error;
8151 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8152 return isl_bool_false;
8153 is_params = isl_union_set_is_params(mupa->u.dom);
8154 if (is_params < 0 || !is_params)
8155 return isl_bool_not(is_params);
8156 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8157 trivial = isl_set_plain_is_universe(set);
8158 isl_set_free(set);
8159 return isl_bool_not(trivial);
8162 /* Construct a multiple union piecewise affine expression
8163 * in the given space with value zero in each of the output dimensions.
8165 * Since there is no canonical zero value for
8166 * a union piecewise affine expression, we can only construct
8167 * a zero-dimensional "zero" value.
8169 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8170 __isl_take isl_space *space)
8172 isl_bool params;
8173 isl_size dim;
8175 if (!space)
8176 return NULL;
8178 params = isl_space_is_params(space);
8179 if (params < 0)
8180 goto error;
8181 if (params)
8182 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8183 "expecting proper set space", goto error);
8184 if (!isl_space_is_set(space))
8185 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8186 "expecting set space", goto error);
8187 dim = isl_space_dim(space, isl_dim_out);
8188 if (dim < 0)
8189 goto error;
8190 if (dim != 0)
8191 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8192 "expecting 0D space", goto error);
8194 return isl_multi_union_pw_aff_alloc(space);
8195 error:
8196 isl_space_free(space);
8197 return NULL;
8200 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8201 * with the actual sum on the shared domain and
8202 * the defined expression on the symmetric difference of the domains.
8204 * We simply iterate over the elements in both arguments and
8205 * call isl_union_pw_aff_union_add on each of them, if there is
8206 * at least one element.
8208 * Otherwise, the two expressions have an explicit domain and
8209 * the union of these explicit domains is computed.
8210 * This assumes that the explicit domains are either both in terms
8211 * of specific domains elements or both in terms of parameters.
8212 * However, if one of the expressions does not have any constraints
8213 * on its explicit domain, then this is allowed as well and the result
8214 * is the expression with no constraints on its explicit domain.
8216 static __isl_give isl_multi_union_pw_aff *
8217 isl_multi_union_pw_aff_union_add_aligned(
8218 __isl_take isl_multi_union_pw_aff *mupa1,
8219 __isl_take isl_multi_union_pw_aff *mupa2)
8221 isl_bool has_domain, is_params1, is_params2;
8223 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8224 goto error;
8225 if (mupa1->n > 0)
8226 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8227 &isl_union_pw_aff_union_add);
8228 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8229 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8230 goto error;
8232 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8233 if (has_domain < 0)
8234 goto error;
8235 if (!has_domain) {
8236 isl_multi_union_pw_aff_free(mupa2);
8237 return mupa1;
8239 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8240 if (has_domain < 0)
8241 goto error;
8242 if (!has_domain) {
8243 isl_multi_union_pw_aff_free(mupa1);
8244 return mupa2;
8247 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8248 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8249 if (is_params1 < 0 || is_params2 < 0)
8250 goto error;
8251 if (is_params1 != is_params2)
8252 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8253 isl_error_invalid,
8254 "cannot compute union of concrete domain and "
8255 "parameter constraints", goto error);
8256 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8257 if (!mupa1)
8258 goto error;
8259 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8260 isl_union_set_copy(mupa2->u.dom));
8261 if (!mupa1->u.dom)
8262 goto error;
8263 isl_multi_union_pw_aff_free(mupa2);
8264 return mupa1;
8265 error:
8266 isl_multi_union_pw_aff_free(mupa1);
8267 isl_multi_union_pw_aff_free(mupa2);
8268 return NULL;
8271 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8272 * with the actual sum on the shared domain and
8273 * the defined expression on the symmetric difference of the domains.
8275 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8276 __isl_take isl_multi_union_pw_aff *mupa1,
8277 __isl_take isl_multi_union_pw_aff *mupa2)
8279 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8280 &isl_multi_union_pw_aff_union_add_aligned);
8283 /* Construct and return a multi union piecewise affine expression
8284 * that is equal to the given multi affine expression.
8286 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8287 __isl_take isl_multi_aff *ma)
8289 isl_multi_pw_aff *mpa;
8291 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8292 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8295 /* Construct and return a multi union piecewise affine expression
8296 * that is equal to the given multi piecewise affine expression.
8298 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8299 __isl_take isl_multi_pw_aff *mpa)
8301 int i;
8302 isl_size n;
8303 isl_space *space;
8304 isl_multi_union_pw_aff *mupa;
8306 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8307 if (n < 0)
8308 mpa = isl_multi_pw_aff_free(mpa);
8309 if (!mpa)
8310 return NULL;
8312 space = isl_multi_pw_aff_get_space(mpa);
8313 space = isl_space_range(space);
8314 mupa = isl_multi_union_pw_aff_alloc(space);
8316 for (i = 0; i < n; ++i) {
8317 isl_pw_aff *pa;
8318 isl_union_pw_aff *upa;
8320 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8321 upa = isl_union_pw_aff_from_pw_aff(pa);
8322 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8325 isl_multi_pw_aff_free(mpa);
8327 return mupa;
8330 /* Extract the range space of "pma" and assign it to *space.
8331 * If *space has already been set (through a previous call to this function),
8332 * then check that the range space is the same.
8334 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8336 isl_space **space = user;
8337 isl_space *pma_space;
8338 isl_bool equal;
8340 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8341 isl_pw_multi_aff_free(pma);
8343 if (!pma_space)
8344 return isl_stat_error;
8345 if (!*space) {
8346 *space = pma_space;
8347 return isl_stat_ok;
8350 equal = isl_space_is_equal(pma_space, *space);
8351 isl_space_free(pma_space);
8353 if (equal < 0)
8354 return isl_stat_error;
8355 if (!equal)
8356 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8357 "range spaces not the same", return isl_stat_error);
8358 return isl_stat_ok;
8361 /* Construct and return a multi union piecewise affine expression
8362 * that is equal to the given union piecewise multi affine expression.
8364 * In order to be able to perform the conversion, the input
8365 * needs to be non-empty and may only involve a single range space.
8367 * If the resulting multi union piecewise affine expression has
8368 * an explicit domain, then assign it the domain of the input.
8369 * In other cases, the domain is stored in the individual elements.
8371 __isl_give isl_multi_union_pw_aff *
8372 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8373 __isl_take isl_union_pw_multi_aff *upma)
8375 isl_space *space = NULL;
8376 isl_multi_union_pw_aff *mupa;
8377 int i;
8378 isl_size n;
8380 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8381 if (n < 0)
8382 goto error;
8383 if (n == 0)
8384 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8385 "cannot extract range space from empty input",
8386 goto error);
8387 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8388 &space) < 0)
8389 goto error;
8391 if (!space)
8392 goto error;
8394 n = isl_space_dim(space, isl_dim_set);
8395 if (n < 0)
8396 space = isl_space_free(space);
8397 mupa = isl_multi_union_pw_aff_alloc(space);
8399 for (i = 0; i < n; ++i) {
8400 isl_union_pw_aff *upa;
8402 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8403 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8405 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8406 isl_union_set *dom;
8407 isl_union_pw_multi_aff *copy;
8409 copy = isl_union_pw_multi_aff_copy(upma);
8410 dom = isl_union_pw_multi_aff_domain(copy);
8411 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8414 isl_union_pw_multi_aff_free(upma);
8415 return mupa;
8416 error:
8417 isl_space_free(space);
8418 isl_union_pw_multi_aff_free(upma);
8419 return NULL;
8422 /* Try and create an isl_multi_union_pw_aff that is equivalent
8423 * to the given isl_union_map.
8424 * The isl_union_map is required to be single-valued in each space.
8425 * Moreover, it cannot be empty and all range spaces need to be the same.
8426 * Otherwise, an error is produced.
8428 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8429 __isl_take isl_union_map *umap)
8431 isl_union_pw_multi_aff *upma;
8433 upma = isl_union_pw_multi_aff_from_union_map(umap);
8434 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8437 /* Return a multiple union piecewise affine expression
8438 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8439 * have been aligned.
8441 * If the resulting multi union piecewise affine expression has
8442 * an explicit domain, then assign it the input domain.
8443 * In other cases, the domain is stored in the individual elements.
8445 static __isl_give isl_multi_union_pw_aff *
8446 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8447 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8449 int i;
8450 isl_size n;
8451 isl_space *space;
8452 isl_multi_union_pw_aff *mupa;
8454 n = isl_multi_val_dim(mv, isl_dim_set);
8455 if (!domain || n < 0)
8456 goto error;
8458 space = isl_multi_val_get_space(mv);
8459 mupa = isl_multi_union_pw_aff_alloc(space);
8460 for (i = 0; i < n; ++i) {
8461 isl_val *v;
8462 isl_union_pw_aff *upa;
8464 v = isl_multi_val_get_val(mv, i);
8465 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8467 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8469 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8470 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8471 isl_union_set_copy(domain));
8473 isl_union_set_free(domain);
8474 isl_multi_val_free(mv);
8475 return mupa;
8476 error:
8477 isl_union_set_free(domain);
8478 isl_multi_val_free(mv);
8479 return NULL;
8482 /* Return a multiple union piecewise affine expression
8483 * that is equal to "mv" on "domain".
8485 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8486 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8488 isl_bool equal_params;
8490 if (!domain || !mv)
8491 goto error;
8492 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8493 if (equal_params < 0)
8494 goto error;
8495 if (equal_params)
8496 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8497 domain, mv);
8498 domain = isl_union_set_align_params(domain,
8499 isl_multi_val_get_space(mv));
8500 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8501 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8502 error:
8503 isl_union_set_free(domain);
8504 isl_multi_val_free(mv);
8505 return NULL;
8508 /* Return a multiple union piecewise affine expression
8509 * that is equal to "ma" on "domain".
8511 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8512 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8514 isl_pw_multi_aff *pma;
8516 pma = isl_pw_multi_aff_from_multi_aff(ma);
8517 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8520 /* Return a multiple union piecewise affine expression
8521 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8522 * have been aligned.
8524 * If the resulting multi union piecewise affine expression has
8525 * an explicit domain, then assign it the input domain.
8526 * In other cases, the domain is stored in the individual elements.
8528 static __isl_give isl_multi_union_pw_aff *
8529 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8530 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8532 int i;
8533 isl_size n;
8534 isl_space *space;
8535 isl_multi_union_pw_aff *mupa;
8537 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8538 if (!domain || n < 0)
8539 goto error;
8540 space = isl_pw_multi_aff_get_space(pma);
8541 mupa = isl_multi_union_pw_aff_alloc(space);
8542 for (i = 0; i < n; ++i) {
8543 isl_pw_aff *pa;
8544 isl_union_pw_aff *upa;
8546 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8547 upa = isl_union_pw_aff_pw_aff_on_domain(
8548 isl_union_set_copy(domain), pa);
8549 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8551 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8552 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8553 isl_union_set_copy(domain));
8555 isl_union_set_free(domain);
8556 isl_pw_multi_aff_free(pma);
8557 return mupa;
8558 error:
8559 isl_union_set_free(domain);
8560 isl_pw_multi_aff_free(pma);
8561 return NULL;
8564 /* Return a multiple union piecewise affine expression
8565 * that is equal to "pma" on "domain".
8567 __isl_give isl_multi_union_pw_aff *
8568 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8569 __isl_take isl_pw_multi_aff *pma)
8571 isl_bool equal_params;
8572 isl_space *space;
8574 space = isl_pw_multi_aff_peek_space(pma);
8575 equal_params = isl_union_set_space_has_equal_params(domain, space);
8576 if (equal_params < 0)
8577 goto error;
8578 if (equal_params)
8579 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8580 domain, pma);
8581 domain = isl_union_set_align_params(domain,
8582 isl_pw_multi_aff_get_space(pma));
8583 pma = isl_pw_multi_aff_align_params(pma,
8584 isl_union_set_get_space(domain));
8585 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8586 pma);
8587 error:
8588 isl_union_set_free(domain);
8589 isl_pw_multi_aff_free(pma);
8590 return NULL;
8593 /* Return a union set containing those elements in the domains
8594 * of the elements of "mupa" where they are all zero.
8596 * If there are no elements, then simply return the entire domain.
8598 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8599 __isl_take isl_multi_union_pw_aff *mupa)
8601 int i;
8602 isl_size n;
8603 isl_union_pw_aff *upa;
8604 isl_union_set *zero;
8606 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8607 if (n < 0)
8608 mupa = isl_multi_union_pw_aff_free(mupa);
8609 if (!mupa)
8610 return NULL;
8612 if (n == 0)
8613 return isl_multi_union_pw_aff_domain(mupa);
8615 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8616 zero = isl_union_pw_aff_zero_union_set(upa);
8618 for (i = 1; i < n; ++i) {
8619 isl_union_set *zero_i;
8621 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8622 zero_i = isl_union_pw_aff_zero_union_set(upa);
8624 zero = isl_union_set_intersect(zero, zero_i);
8627 isl_multi_union_pw_aff_free(mupa);
8628 return zero;
8631 /* Construct a union map mapping the shared domain
8632 * of the union piecewise affine expressions to the range of "mupa"
8633 * in the special case of a 0D multi union piecewise affine expression.
8635 * Construct a map between the explicit domain of "mupa" and
8636 * the range space.
8637 * Note that this assumes that the domain consists of explicit elements.
8639 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8640 __isl_take isl_multi_union_pw_aff *mupa)
8642 isl_bool is_params;
8643 isl_space *space;
8644 isl_union_set *dom, *ran;
8646 space = isl_multi_union_pw_aff_get_space(mupa);
8647 dom = isl_multi_union_pw_aff_domain(mupa);
8648 ran = isl_union_set_from_set(isl_set_universe(space));
8650 is_params = isl_union_set_is_params(dom);
8651 if (is_params < 0)
8652 dom = isl_union_set_free(dom);
8653 else if (is_params)
8654 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8655 "cannot create union map from expression without "
8656 "explicit domain elements",
8657 dom = isl_union_set_free(dom));
8659 return isl_union_map_from_domain_and_range(dom, ran);
8662 /* Construct a union map mapping the shared domain
8663 * of the union piecewise affine expressions to the range of "mupa"
8664 * with each dimension in the range equated to the
8665 * corresponding union piecewise affine expression.
8667 * If the input is zero-dimensional, then construct a mapping
8668 * from its explicit domain.
8670 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8671 __isl_take isl_multi_union_pw_aff *mupa)
8673 int i;
8674 isl_size n;
8675 isl_space *space;
8676 isl_union_map *umap;
8677 isl_union_pw_aff *upa;
8679 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8680 if (n < 0)
8681 mupa = isl_multi_union_pw_aff_free(mupa);
8682 if (!mupa)
8683 return NULL;
8685 if (n == 0)
8686 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8688 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8689 umap = isl_union_map_from_union_pw_aff(upa);
8691 for (i = 1; i < n; ++i) {
8692 isl_union_map *umap_i;
8694 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8695 umap_i = isl_union_map_from_union_pw_aff(upa);
8696 umap = isl_union_map_flat_range_product(umap, umap_i);
8699 space = isl_multi_union_pw_aff_get_space(mupa);
8700 umap = isl_union_map_reset_range_space(umap, space);
8702 isl_multi_union_pw_aff_free(mupa);
8703 return umap;
8706 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8707 * "range" is the space from which to set the range space.
8708 * "res" collects the results.
8710 struct isl_union_pw_multi_aff_reset_range_space_data {
8711 isl_space *range;
8712 isl_union_pw_multi_aff *res;
8715 /* Replace the range space of "pma" by the range space of data->range and
8716 * add the result to data->res.
8718 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8720 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8721 isl_space *space;
8723 space = isl_pw_multi_aff_get_space(pma);
8724 space = isl_space_domain(space);
8725 space = isl_space_extend_domain_with_range(space,
8726 isl_space_copy(data->range));
8727 pma = isl_pw_multi_aff_reset_space(pma, space);
8728 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8730 return data->res ? isl_stat_ok : isl_stat_error;
8733 /* Replace the range space of all the piecewise affine expressions in "upma" by
8734 * the range space of "space".
8736 * This assumes that all these expressions have the same output dimension.
8738 * Since the spaces of the expressions change, so do their hash values.
8739 * We therefore need to create a new isl_union_pw_multi_aff.
8740 * Note that the hash value is currently computed based on the entire
8741 * space even though there can only be a single expression with a given
8742 * domain space.
8744 static __isl_give isl_union_pw_multi_aff *
8745 isl_union_pw_multi_aff_reset_range_space(
8746 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8748 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8749 isl_space *space_upma;
8751 space_upma = isl_union_pw_multi_aff_get_space(upma);
8752 data.res = isl_union_pw_multi_aff_empty(space_upma);
8753 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8754 &reset_range_space, &data) < 0)
8755 data.res = isl_union_pw_multi_aff_free(data.res);
8757 isl_space_free(space);
8758 isl_union_pw_multi_aff_free(upma);
8759 return data.res;
8762 /* Construct and return a union piecewise multi affine expression
8763 * that is equal to the given multi union piecewise affine expression,
8764 * in the special case of a 0D multi union piecewise affine expression.
8766 * Construct a union piecewise multi affine expression
8767 * on top of the explicit domain of the input.
8769 __isl_give isl_union_pw_multi_aff *
8770 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8771 __isl_take isl_multi_union_pw_aff *mupa)
8773 isl_space *space;
8774 isl_multi_val *mv;
8775 isl_union_set *domain;
8777 space = isl_multi_union_pw_aff_get_space(mupa);
8778 mv = isl_multi_val_zero(space);
8779 domain = isl_multi_union_pw_aff_domain(mupa);
8780 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8783 /* Construct and return a union piecewise multi affine expression
8784 * that is equal to the given multi union piecewise affine expression.
8786 * If the input is zero-dimensional, then
8787 * construct a union piecewise multi affine expression
8788 * on top of the explicit domain of the input.
8790 __isl_give isl_union_pw_multi_aff *
8791 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8792 __isl_take isl_multi_union_pw_aff *mupa)
8794 int i;
8795 isl_size n;
8796 isl_space *space;
8797 isl_union_pw_multi_aff *upma;
8798 isl_union_pw_aff *upa;
8800 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8801 if (n < 0)
8802 mupa = isl_multi_union_pw_aff_free(mupa);
8803 if (!mupa)
8804 return NULL;
8806 if (n == 0)
8807 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8809 space = isl_multi_union_pw_aff_get_space(mupa);
8810 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8811 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8813 for (i = 1; i < n; ++i) {
8814 isl_union_pw_multi_aff *upma_i;
8816 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8817 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8818 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8821 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8823 isl_multi_union_pw_aff_free(mupa);
8824 return upma;
8827 /* Intersect the range of "mupa" with "range",
8828 * in the special case where "mupa" is 0D.
8830 * Intersect the domain of "mupa" with the constraints on the parameters
8831 * of "range".
8833 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8834 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8836 range = isl_set_params(range);
8837 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8838 return mupa;
8841 /* Intersect the range of "mupa" with "range".
8842 * That is, keep only those domain elements that have a function value
8843 * in "range".
8845 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8846 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8848 isl_union_pw_multi_aff *upma;
8849 isl_union_set *domain;
8850 isl_space *space;
8851 isl_size n;
8852 int match;
8854 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8855 if (n < 0 || !range)
8856 goto error;
8858 space = isl_set_get_space(range);
8859 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8860 space, isl_dim_set);
8861 isl_space_free(space);
8862 if (match < 0)
8863 goto error;
8864 if (!match)
8865 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8866 "space don't match", goto error);
8867 if (n == 0)
8868 return mupa_intersect_range_0D(mupa, range);
8870 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8871 isl_multi_union_pw_aff_copy(mupa));
8872 domain = isl_union_set_from_set(range);
8873 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8874 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8876 return mupa;
8877 error:
8878 isl_multi_union_pw_aff_free(mupa);
8879 isl_set_free(range);
8880 return NULL;
8883 /* Return the shared domain of the elements of "mupa",
8884 * in the special case where "mupa" is zero-dimensional.
8886 * Return the explicit domain of "mupa".
8887 * Note that this domain may be a parameter set, either
8888 * because "mupa" is meant to live in a set space or
8889 * because no explicit domain has been set.
8891 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8892 __isl_take isl_multi_union_pw_aff *mupa)
8894 isl_union_set *dom;
8896 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8897 isl_multi_union_pw_aff_free(mupa);
8899 return dom;
8902 /* Return the shared domain of the elements of "mupa".
8904 * If "mupa" is zero-dimensional, then return its explicit domain.
8906 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8907 __isl_take isl_multi_union_pw_aff *mupa)
8909 int i;
8910 isl_size n;
8911 isl_union_pw_aff *upa;
8912 isl_union_set *dom;
8914 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8915 if (n < 0)
8916 mupa = isl_multi_union_pw_aff_free(mupa);
8917 if (!mupa)
8918 return NULL;
8920 if (n == 0)
8921 return isl_multi_union_pw_aff_domain_0D(mupa);
8923 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8924 dom = isl_union_pw_aff_domain(upa);
8925 for (i = 1; i < n; ++i) {
8926 isl_union_set *dom_i;
8928 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8929 dom_i = isl_union_pw_aff_domain(upa);
8930 dom = isl_union_set_intersect(dom, dom_i);
8933 isl_multi_union_pw_aff_free(mupa);
8934 return dom;
8937 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8938 * In particular, the spaces have been aligned.
8939 * The result is defined over the shared domain of the elements of "mupa"
8941 * We first extract the parametric constant part of "aff" and
8942 * define that over the shared domain.
8943 * Then we iterate over all input dimensions of "aff" and add the corresponding
8944 * multiples of the elements of "mupa".
8945 * Finally, we consider the integer divisions, calling the function
8946 * recursively to obtain an isl_union_pw_aff corresponding to the
8947 * integer division argument.
8949 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8950 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8952 int i;
8953 isl_size n_in, n_div;
8954 isl_union_pw_aff *upa;
8955 isl_union_set *uset;
8956 isl_val *v;
8957 isl_aff *cst;
8959 n_in = isl_aff_dim(aff, isl_dim_in);
8960 n_div = isl_aff_dim(aff, isl_dim_div);
8961 if (n_in < 0 || n_div < 0)
8962 goto error;
8964 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8965 cst = isl_aff_copy(aff);
8966 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8967 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8968 cst = isl_aff_project_domain_on_params(cst);
8969 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8971 for (i = 0; i < n_in; ++i) {
8972 isl_union_pw_aff *upa_i;
8974 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8975 continue;
8976 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8977 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8978 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8979 upa = isl_union_pw_aff_add(upa, upa_i);
8982 for (i = 0; i < n_div; ++i) {
8983 isl_aff *div;
8984 isl_union_pw_aff *upa_i;
8986 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8987 continue;
8988 div = isl_aff_get_div(aff, i);
8989 upa_i = multi_union_pw_aff_apply_aff(
8990 isl_multi_union_pw_aff_copy(mupa), div);
8991 upa_i = isl_union_pw_aff_floor(upa_i);
8992 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8993 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8994 upa = isl_union_pw_aff_add(upa, upa_i);
8997 isl_multi_union_pw_aff_free(mupa);
8998 isl_aff_free(aff);
9000 return upa;
9001 error:
9002 isl_multi_union_pw_aff_free(mupa);
9003 isl_aff_free(aff);
9004 return NULL;
9007 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9008 * with the domain of "aff".
9009 * Furthermore, the dimension of this space needs to be greater than zero.
9010 * The result is defined over the shared domain of the elements of "mupa"
9012 * We perform these checks and then hand over control to
9013 * multi_union_pw_aff_apply_aff.
9015 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9016 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9018 isl_size dim;
9019 isl_space *space1, *space2;
9020 isl_bool equal;
9022 mupa = isl_multi_union_pw_aff_align_params(mupa,
9023 isl_aff_get_space(aff));
9024 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9025 if (!mupa || !aff)
9026 goto error;
9028 space1 = isl_multi_union_pw_aff_get_space(mupa);
9029 space2 = isl_aff_get_domain_space(aff);
9030 equal = isl_space_is_equal(space1, space2);
9031 isl_space_free(space1);
9032 isl_space_free(space2);
9033 if (equal < 0)
9034 goto error;
9035 if (!equal)
9036 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9037 "spaces don't match", goto error);
9038 dim = isl_aff_dim(aff, isl_dim_in);
9039 if (dim < 0)
9040 goto error;
9041 if (dim == 0)
9042 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9043 "cannot determine domains", goto error);
9045 return multi_union_pw_aff_apply_aff(mupa, aff);
9046 error:
9047 isl_multi_union_pw_aff_free(mupa);
9048 isl_aff_free(aff);
9049 return NULL;
9052 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9053 * The space of "mupa" is known to be compatible with the domain of "ma".
9055 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9056 * on the domain of "mupa".
9058 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9059 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9061 isl_union_set *dom;
9063 dom = isl_multi_union_pw_aff_domain(mupa);
9064 ma = isl_multi_aff_project_domain_on_params(ma);
9066 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9069 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9070 * with the domain of "ma".
9071 * The result is defined over the shared domain of the elements of "mupa"
9073 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9074 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9076 isl_space *space1, *space2;
9077 isl_multi_union_pw_aff *res;
9078 isl_bool equal;
9079 int i;
9080 isl_size n_in, n_out;
9082 mupa = isl_multi_union_pw_aff_align_params(mupa,
9083 isl_multi_aff_get_space(ma));
9084 ma = isl_multi_aff_align_params(ma,
9085 isl_multi_union_pw_aff_get_space(mupa));
9086 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9087 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9088 if (!mupa || n_in < 0 || n_out < 0)
9089 goto error;
9091 space1 = isl_multi_union_pw_aff_get_space(mupa);
9092 space2 = isl_multi_aff_get_domain_space(ma);
9093 equal = isl_space_is_equal(space1, space2);
9094 isl_space_free(space1);
9095 isl_space_free(space2);
9096 if (equal < 0)
9097 goto error;
9098 if (!equal)
9099 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9100 "spaces don't match", goto error);
9101 if (n_in == 0)
9102 return mupa_apply_multi_aff_0D(mupa, ma);
9104 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9105 res = isl_multi_union_pw_aff_alloc(space1);
9107 for (i = 0; i < n_out; ++i) {
9108 isl_aff *aff;
9109 isl_union_pw_aff *upa;
9111 aff = isl_multi_aff_get_aff(ma, i);
9112 upa = multi_union_pw_aff_apply_aff(
9113 isl_multi_union_pw_aff_copy(mupa), aff);
9114 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9117 isl_multi_aff_free(ma);
9118 isl_multi_union_pw_aff_free(mupa);
9119 return res;
9120 error:
9121 isl_multi_union_pw_aff_free(mupa);
9122 isl_multi_aff_free(ma);
9123 return NULL;
9126 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9127 * The space of "mupa" is known to be compatible with the domain of "pa".
9129 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9130 * on the domain of "mupa".
9132 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9133 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9135 isl_union_set *dom;
9137 dom = isl_multi_union_pw_aff_domain(mupa);
9138 pa = isl_pw_aff_project_domain_on_params(pa);
9140 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9143 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9144 * with the domain of "pa".
9145 * Furthermore, the dimension of this space needs to be greater than zero.
9146 * The result is defined over the shared domain of the elements of "mupa"
9148 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9149 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9151 int i;
9152 isl_bool equal;
9153 isl_size n_in;
9154 isl_space *space, *space2;
9155 isl_union_pw_aff *upa;
9157 mupa = isl_multi_union_pw_aff_align_params(mupa,
9158 isl_pw_aff_get_space(pa));
9159 pa = isl_pw_aff_align_params(pa,
9160 isl_multi_union_pw_aff_get_space(mupa));
9161 if (!mupa || !pa)
9162 goto error;
9164 space = isl_multi_union_pw_aff_get_space(mupa);
9165 space2 = isl_pw_aff_get_domain_space(pa);
9166 equal = isl_space_is_equal(space, space2);
9167 isl_space_free(space);
9168 isl_space_free(space2);
9169 if (equal < 0)
9170 goto error;
9171 if (!equal)
9172 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9173 "spaces don't match", goto error);
9174 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9175 if (n_in < 0)
9176 goto error;
9177 if (n_in == 0)
9178 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9180 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9181 upa = isl_union_pw_aff_empty(space);
9183 for (i = 0; i < pa->n; ++i) {
9184 isl_aff *aff;
9185 isl_set *domain;
9186 isl_multi_union_pw_aff *mupa_i;
9187 isl_union_pw_aff *upa_i;
9189 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9190 domain = isl_set_copy(pa->p[i].set);
9191 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9192 aff = isl_aff_copy(pa->p[i].aff);
9193 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9194 upa = isl_union_pw_aff_union_add(upa, upa_i);
9197 isl_multi_union_pw_aff_free(mupa);
9198 isl_pw_aff_free(pa);
9199 return upa;
9200 error:
9201 isl_multi_union_pw_aff_free(mupa);
9202 isl_pw_aff_free(pa);
9203 return NULL;
9206 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9207 * The space of "mupa" is known to be compatible with the domain of "pma".
9209 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9210 * on the domain of "mupa".
9212 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9213 __isl_take isl_multi_union_pw_aff *mupa,
9214 __isl_take isl_pw_multi_aff *pma)
9216 isl_union_set *dom;
9218 dom = isl_multi_union_pw_aff_domain(mupa);
9219 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9221 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9224 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9225 * with the domain of "pma".
9226 * The result is defined over the shared domain of the elements of "mupa"
9228 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9229 __isl_take isl_multi_union_pw_aff *mupa,
9230 __isl_take isl_pw_multi_aff *pma)
9232 isl_space *space1, *space2;
9233 isl_multi_union_pw_aff *res;
9234 isl_bool equal;
9235 int i;
9236 isl_size n_in, n_out;
9238 mupa = isl_multi_union_pw_aff_align_params(mupa,
9239 isl_pw_multi_aff_get_space(pma));
9240 pma = isl_pw_multi_aff_align_params(pma,
9241 isl_multi_union_pw_aff_get_space(mupa));
9242 if (!mupa || !pma)
9243 goto error;
9245 space1 = isl_multi_union_pw_aff_get_space(mupa);
9246 space2 = isl_pw_multi_aff_get_domain_space(pma);
9247 equal = isl_space_is_equal(space1, space2);
9248 isl_space_free(space1);
9249 isl_space_free(space2);
9250 if (equal < 0)
9251 goto error;
9252 if (!equal)
9253 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9254 "spaces don't match", goto error);
9255 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9256 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9257 if (n_in < 0 || n_out < 0)
9258 goto error;
9259 if (n_in == 0)
9260 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9262 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9263 res = isl_multi_union_pw_aff_alloc(space1);
9265 for (i = 0; i < n_out; ++i) {
9266 isl_pw_aff *pa;
9267 isl_union_pw_aff *upa;
9269 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9270 upa = isl_multi_union_pw_aff_apply_pw_aff(
9271 isl_multi_union_pw_aff_copy(mupa), pa);
9272 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9275 isl_pw_multi_aff_free(pma);
9276 isl_multi_union_pw_aff_free(mupa);
9277 return res;
9278 error:
9279 isl_multi_union_pw_aff_free(mupa);
9280 isl_pw_multi_aff_free(pma);
9281 return NULL;
9284 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9285 * If the explicit domain only keeps track of constraints on the parameters,
9286 * then only update those constraints.
9288 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9289 __isl_take isl_multi_union_pw_aff *mupa,
9290 __isl_keep isl_union_pw_multi_aff *upma)
9292 isl_bool is_params;
9294 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9295 return isl_multi_union_pw_aff_free(mupa);
9297 mupa = isl_multi_union_pw_aff_cow(mupa);
9298 if (!mupa)
9299 return NULL;
9301 is_params = isl_union_set_is_params(mupa->u.dom);
9302 if (is_params < 0)
9303 return isl_multi_union_pw_aff_free(mupa);
9305 upma = isl_union_pw_multi_aff_copy(upma);
9306 if (is_params)
9307 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9308 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9309 else
9310 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9311 mupa->u.dom, upma);
9312 if (!mupa->u.dom)
9313 return isl_multi_union_pw_aff_free(mupa);
9314 return mupa;
9317 /* Compute the pullback of "mupa" by the function represented by "upma".
9318 * In other words, plug in "upma" in "mupa". The result contains
9319 * expressions defined over the domain space of "upma".
9321 * Run over all elements of "mupa" and plug in "upma" in each of them.
9323 * If "mupa" has an explicit domain, then it is this domain
9324 * that needs to undergo a pullback instead, i.e., a preimage.
9326 __isl_give isl_multi_union_pw_aff *
9327 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9328 __isl_take isl_multi_union_pw_aff *mupa,
9329 __isl_take isl_union_pw_multi_aff *upma)
9331 int i;
9332 isl_size n;
9334 mupa = isl_multi_union_pw_aff_align_params(mupa,
9335 isl_union_pw_multi_aff_get_space(upma));
9336 upma = isl_union_pw_multi_aff_align_params(upma,
9337 isl_multi_union_pw_aff_get_space(mupa));
9338 mupa = isl_multi_union_pw_aff_cow(mupa);
9339 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9340 if (n < 0 || !upma)
9341 goto error;
9343 for (i = 0; i < n; ++i) {
9344 isl_union_pw_aff *upa;
9346 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9347 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9348 isl_union_pw_multi_aff_copy(upma));
9349 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9352 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9353 mupa = preimage_explicit_domain(mupa, upma);
9355 isl_union_pw_multi_aff_free(upma);
9356 return mupa;
9357 error:
9358 isl_multi_union_pw_aff_free(mupa);
9359 isl_union_pw_multi_aff_free(upma);
9360 return NULL;
9363 /* Extract the sequence of elements in "mupa" with domain space "space"
9364 * (ignoring parameters).
9366 * For the elements of "mupa" that are not defined on the specified space,
9367 * the corresponding element in the result is empty.
9369 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9370 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9372 int i;
9373 isl_size n;
9374 isl_space *space_mpa;
9375 isl_multi_pw_aff *mpa;
9377 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9378 if (n < 0 || !space)
9379 goto error;
9381 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9382 space = isl_space_replace_params(space, space_mpa);
9383 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9384 space_mpa);
9385 mpa = isl_multi_pw_aff_alloc(space_mpa);
9387 space = isl_space_from_domain(space);
9388 space = isl_space_add_dims(space, isl_dim_out, 1);
9389 for (i = 0; i < n; ++i) {
9390 isl_union_pw_aff *upa;
9391 isl_pw_aff *pa;
9393 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9394 pa = isl_union_pw_aff_extract_pw_aff(upa,
9395 isl_space_copy(space));
9396 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9397 isl_union_pw_aff_free(upa);
9400 isl_space_free(space);
9401 return mpa;
9402 error:
9403 isl_space_free(space);
9404 return NULL;
9407 /* Evaluate the affine function "aff" in the void point "pnt".
9408 * In particular, return the value NaN.
9410 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9411 __isl_take isl_point *pnt)
9413 isl_ctx *ctx;
9415 ctx = isl_point_get_ctx(pnt);
9416 isl_aff_free(aff);
9417 isl_point_free(pnt);
9418 return isl_val_nan(ctx);
9421 /* Evaluate the affine expression "aff"
9422 * in the coordinates (with denominator) "pnt".
9424 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9425 __isl_keep isl_vec *pnt)
9427 isl_int n, d;
9428 isl_ctx *ctx;
9429 isl_val *v;
9431 if (!aff || !pnt)
9432 return NULL;
9434 ctx = isl_vec_get_ctx(aff);
9435 isl_int_init(n);
9436 isl_int_init(d);
9437 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9438 isl_int_mul(d, aff->el[0], pnt->el[0]);
9439 v = isl_val_rat_from_isl_int(ctx, n, d);
9440 v = isl_val_normalize(v);
9441 isl_int_clear(n);
9442 isl_int_clear(d);
9444 return v;
9447 /* Check that the domain space of "aff" is equal to "space".
9449 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9450 __isl_keep isl_space *space)
9452 isl_bool ok;
9454 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9455 if (ok < 0)
9456 return isl_stat_error;
9457 if (!ok)
9458 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9459 "incompatible spaces", return isl_stat_error);
9460 return isl_stat_ok;
9463 /* Evaluate the affine function "aff" in "pnt".
9465 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9466 __isl_take isl_point *pnt)
9468 isl_bool is_void;
9469 isl_val *v;
9470 isl_local_space *ls;
9472 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9473 goto error;
9474 is_void = isl_point_is_void(pnt);
9475 if (is_void < 0)
9476 goto error;
9477 if (is_void)
9478 return eval_void(aff, pnt);
9480 ls = isl_aff_get_domain_local_space(aff);
9481 pnt = isl_local_space_lift_point(ls, pnt);
9483 v = eval(aff->v, isl_point_peek_vec(pnt));
9485 isl_aff_free(aff);
9486 isl_point_free(pnt);
9488 return v;
9489 error:
9490 isl_aff_free(aff);
9491 isl_point_free(pnt);
9492 return NULL;