isl_pw_templ.c: extract out isl_pw_sub_templ.c
[isl.git] / isl_aff.c
blob2022f9eac03f3d113786605de0a6ede3ade2dc1d
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 "val" on domain space "space".
190 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
191 __isl_take isl_val *val)
193 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
196 /* Return an affine expression that is equal to the specified dimension
197 * in "ls".
199 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
200 enum isl_dim_type type, unsigned pos)
202 isl_space *space;
203 isl_aff *aff;
205 if (!ls)
206 return NULL;
208 space = isl_local_space_get_space(ls);
209 if (!space)
210 goto error;
211 if (isl_space_is_map(space))
212 isl_die(isl_space_get_ctx(space), isl_error_invalid,
213 "expecting (parameter) set space", goto error);
214 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
215 goto error;
217 isl_space_free(space);
218 aff = isl_aff_alloc(ls);
219 if (!aff)
220 return NULL;
222 pos += isl_local_space_offset(aff->ls, type);
224 isl_int_set_si(aff->v->el[0], 1);
225 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
226 isl_int_set_si(aff->v->el[1 + pos], 1);
228 return aff;
229 error:
230 isl_local_space_free(ls);
231 isl_space_free(space);
232 return NULL;
235 /* Return a piecewise affine expression that is equal to
236 * the specified dimension in "ls".
238 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
239 enum isl_dim_type type, unsigned pos)
241 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
244 /* Return an affine expression that is equal to the parameter
245 * in the domain space "space" with identifier "id".
247 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
248 __isl_take isl_space *space, __isl_take isl_id *id)
250 int pos;
251 isl_local_space *ls;
253 if (!space || !id)
254 goto error;
255 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
256 if (pos < 0)
257 isl_die(isl_space_get_ctx(space), isl_error_invalid,
258 "parameter not found in space", goto error);
259 isl_id_free(id);
260 ls = isl_local_space_from_space(space);
261 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
262 error:
263 isl_space_free(space);
264 isl_id_free(id);
265 return NULL;
268 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
270 if (!aff)
271 return NULL;
273 aff->ref++;
274 return aff;
277 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
279 if (!aff)
280 return NULL;
282 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
283 isl_vec_copy(aff->v));
286 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
288 if (!aff)
289 return NULL;
291 if (aff->ref == 1)
292 return aff;
293 aff->ref--;
294 return isl_aff_dup(aff);
297 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
299 if (!aff)
300 return NULL;
302 if (--aff->ref > 0)
303 return NULL;
305 isl_local_space_free(aff->ls);
306 isl_vec_free(aff->v);
308 free(aff);
310 return NULL;
313 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
315 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
318 /* Return a hash value that digests "aff".
320 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
322 uint32_t hash, ls_hash, v_hash;
324 if (!aff)
325 return 0;
327 hash = isl_hash_init();
328 ls_hash = isl_local_space_get_hash(aff->ls);
329 isl_hash_hash(hash, ls_hash);
330 v_hash = isl_vec_get_hash(aff->v);
331 isl_hash_hash(hash, v_hash);
333 return hash;
336 /* Return the domain local space of "aff".
338 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
339 __isl_keep isl_aff *aff)
341 return aff ? aff->ls : NULL;
344 /* Return the number of variables of the given type in the domain of "aff".
346 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
348 isl_local_space *ls;
350 ls = isl_aff_peek_domain_local_space(aff);
351 return isl_local_space_dim(ls, type);
354 /* Externally, an isl_aff has a map space, but internally, the
355 * ls field corresponds to the domain of that space.
357 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
359 if (!aff)
360 return isl_size_error;
361 if (type == isl_dim_out)
362 return 1;
363 if (type == isl_dim_in)
364 type = isl_dim_set;
365 return isl_aff_domain_dim(aff, type);
368 /* Return the offset of the first coefficient of type "type" in
369 * the domain of "aff".
371 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
373 isl_local_space *ls;
375 ls = isl_aff_peek_domain_local_space(aff);
376 return isl_local_space_offset(ls, type);
379 /* Return the position of the dimension of the given type and name
380 * in "aff".
381 * Return -1 if no such dimension can be found.
383 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
384 const char *name)
386 if (!aff)
387 return -1;
388 if (type == isl_dim_out)
389 return -1;
390 if (type == isl_dim_in)
391 type = isl_dim_set;
392 return isl_local_space_find_dim_by_name(aff->ls, type, name);
395 /* Return the domain space of "aff".
397 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
399 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
402 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
404 return isl_space_copy(isl_aff_peek_domain_space(aff));
407 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
409 isl_space *space;
410 if (!aff)
411 return NULL;
412 space = isl_local_space_get_space(aff->ls);
413 space = isl_space_from_domain(space);
414 space = isl_space_add_dims(space, isl_dim_out, 1);
415 return space;
418 /* Return a copy of the domain space of "aff".
420 __isl_give isl_local_space *isl_aff_get_domain_local_space(
421 __isl_keep isl_aff *aff)
423 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
426 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
428 isl_local_space *ls;
429 if (!aff)
430 return NULL;
431 ls = isl_local_space_copy(aff->ls);
432 ls = isl_local_space_from_domain(ls);
433 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
434 return ls;
437 /* Return the local space of the domain of "aff".
438 * This may be either a copy or the local space itself
439 * if there is only one reference to "aff".
440 * This allows the local space to be modified inplace
441 * if both the expression and its local space have only a single reference.
442 * The caller is not allowed to modify "aff" between this call and
443 * a subsequent call to isl_aff_restore_domain_local_space.
444 * The only exception is that isl_aff_free can be called instead.
446 __isl_give isl_local_space *isl_aff_take_domain_local_space(
447 __isl_keep isl_aff *aff)
449 isl_local_space *ls;
451 if (!aff)
452 return NULL;
453 if (aff->ref != 1)
454 return isl_aff_get_domain_local_space(aff);
455 ls = aff->ls;
456 aff->ls = NULL;
457 return ls;
460 /* Set the local space of the domain of "aff" to "ls",
461 * where the local space of "aff" may be missing
462 * due to a preceding call to isl_aff_take_domain_local_space.
463 * However, in this case, "aff" only has a single reference and
464 * then the call to isl_aff_cow has no effect.
466 __isl_give isl_aff *isl_aff_restore_domain_local_space(
467 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
469 if (!aff || !ls)
470 goto error;
472 if (aff->ls == ls) {
473 isl_local_space_free(ls);
474 return aff;
477 aff = isl_aff_cow(aff);
478 if (!aff)
479 goto error;
480 isl_local_space_free(aff->ls);
481 aff->ls = ls;
483 return aff;
484 error:
485 isl_aff_free(aff);
486 isl_local_space_free(ls);
487 return NULL;
490 /* Externally, an isl_aff has a map space, but internally, the
491 * ls field corresponds to the domain of that space.
493 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
494 enum isl_dim_type type, unsigned pos)
496 if (!aff)
497 return NULL;
498 if (type == isl_dim_out)
499 return NULL;
500 if (type == isl_dim_in)
501 type = isl_dim_set;
502 return isl_local_space_get_dim_name(aff->ls, type, pos);
505 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
506 __isl_take isl_space *dim)
508 aff = isl_aff_cow(aff);
509 if (!aff || !dim)
510 goto error;
512 aff->ls = isl_local_space_reset_space(aff->ls, dim);
513 if (!aff->ls)
514 return isl_aff_free(aff);
516 return aff;
517 error:
518 isl_aff_free(aff);
519 isl_space_free(dim);
520 return NULL;
523 /* Reset the space of "aff". This function is called from isl_pw_templ.c
524 * and doesn't know if the space of an element object is represented
525 * directly or through its domain. It therefore passes along both.
527 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
528 __isl_take isl_space *space, __isl_take isl_space *domain)
530 isl_space_free(space);
531 return isl_aff_reset_domain_space(aff, domain);
534 /* Reorder the coefficients of the affine expression based
535 * on the given reordering.
536 * The reordering r is assumed to have been extended with the local
537 * variables.
539 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
540 __isl_take isl_reordering *r, int n_div)
542 isl_space *space;
543 isl_vec *res;
544 isl_size dim;
545 int i;
547 if (!vec || !r)
548 goto error;
550 space = isl_reordering_peek_space(r);
551 dim = isl_space_dim(space, isl_dim_all);
552 if (dim < 0)
553 goto error;
554 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
555 if (!res)
556 goto error;
557 isl_seq_cpy(res->el, vec->el, 2);
558 isl_seq_clr(res->el + 2, res->size - 2);
559 for (i = 0; i < r->len; ++i)
560 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
562 isl_reordering_free(r);
563 isl_vec_free(vec);
564 return res;
565 error:
566 isl_vec_free(vec);
567 isl_reordering_free(r);
568 return NULL;
571 /* Reorder the dimensions of the domain of "aff" according
572 * to the given reordering.
574 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
575 __isl_take isl_reordering *r)
577 aff = isl_aff_cow(aff);
578 if (!aff)
579 goto error;
581 r = isl_reordering_extend(r, aff->ls->div->n_row);
582 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
583 aff->ls->div->n_row);
584 aff->ls = isl_local_space_realign(aff->ls, r);
586 if (!aff->v || !aff->ls)
587 return isl_aff_free(aff);
589 return aff;
590 error:
591 isl_aff_free(aff);
592 isl_reordering_free(r);
593 return NULL;
596 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
597 __isl_take isl_space *model)
599 isl_bool equal_params;
601 if (!aff || !model)
602 goto error;
604 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
605 if (equal_params < 0)
606 goto error;
607 if (!equal_params) {
608 isl_reordering *exp;
610 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
611 exp = isl_reordering_extend_space(exp,
612 isl_aff_get_domain_space(aff));
613 aff = isl_aff_realign_domain(aff, exp);
616 isl_space_free(model);
617 return aff;
618 error:
619 isl_space_free(model);
620 isl_aff_free(aff);
621 return NULL;
624 /* Given an affine function "aff" defined over a parameter domain,
625 * convert it to a function defined over a domain corresponding
626 * to "domain".
627 * Any parameters with identifiers in "domain" are reinterpreted
628 * as the corresponding domain dimensions.
630 __isl_give isl_aff *isl_aff_unbind_params_insert_domain(
631 __isl_take isl_aff *aff, __isl_take isl_multi_id *domain)
633 isl_bool is_params;
634 isl_space *space;
635 isl_reordering *r;
637 space = isl_aff_peek_domain_space(aff);
638 is_params = isl_space_is_params(space);
639 if (is_params < 0)
640 domain = isl_multi_id_free(domain);
641 else if (!is_params)
642 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
643 "expecting function with parameter domain",
644 domain = isl_multi_id_free(domain));
645 r = isl_reordering_unbind_params_insert_domain(space, domain);
646 isl_multi_id_free(domain);
648 return isl_aff_realign_domain(aff, r);
651 /* Is "aff" obviously equal to zero?
653 * If the denominator is zero, then "aff" is not equal to zero.
655 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
657 int pos;
659 if (!aff)
660 return isl_bool_error;
662 if (isl_int_is_zero(aff->v->el[0]))
663 return isl_bool_false;
664 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
665 return isl_bool_ok(pos < 0);
668 /* Does "aff" represent NaN?
670 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
672 if (!aff)
673 return isl_bool_error;
675 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
678 /* Are "aff1" and "aff2" obviously equal?
680 * NaN is not equal to anything, not even to another NaN.
682 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
683 __isl_keep isl_aff *aff2)
685 isl_bool equal;
687 if (!aff1 || !aff2)
688 return isl_bool_error;
690 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
691 return isl_bool_false;
693 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
694 if (equal < 0 || !equal)
695 return equal;
697 return isl_vec_is_equal(aff1->v, aff2->v);
700 /* Return the common denominator of "aff" in "v".
702 * We cannot return anything meaningful in case of a NaN.
704 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
706 if (!aff)
707 return isl_stat_error;
708 if (isl_aff_is_nan(aff))
709 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
710 "cannot get denominator of NaN", return isl_stat_error);
711 isl_int_set(*v, aff->v->el[0]);
712 return isl_stat_ok;
715 /* Return the common denominator of "aff".
717 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
719 isl_ctx *ctx;
721 if (!aff)
722 return NULL;
724 ctx = isl_aff_get_ctx(aff);
725 if (isl_aff_is_nan(aff))
726 return isl_val_nan(ctx);
727 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
730 /* Return the constant term of "aff".
732 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
734 isl_ctx *ctx;
735 isl_val *v;
737 if (!aff)
738 return NULL;
740 ctx = isl_aff_get_ctx(aff);
741 if (isl_aff_is_nan(aff))
742 return isl_val_nan(ctx);
743 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
744 return isl_val_normalize(v);
747 /* Return the coefficient of the variable of type "type" at position "pos"
748 * of "aff".
750 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
751 enum isl_dim_type type, int pos)
753 isl_ctx *ctx;
754 isl_val *v;
756 if (!aff)
757 return NULL;
759 ctx = isl_aff_get_ctx(aff);
760 if (type == isl_dim_out)
761 isl_die(ctx, isl_error_invalid,
762 "output/set dimension does not have a coefficient",
763 return NULL);
764 if (type == isl_dim_in)
765 type = isl_dim_set;
767 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
768 return NULL;
770 if (isl_aff_is_nan(aff))
771 return isl_val_nan(ctx);
772 pos += isl_local_space_offset(aff->ls, type);
773 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
774 return isl_val_normalize(v);
777 /* Return the sign of the coefficient of the variable of type "type"
778 * at position "pos" of "aff".
780 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
781 int pos)
783 isl_ctx *ctx;
785 if (!aff)
786 return 0;
788 ctx = isl_aff_get_ctx(aff);
789 if (type == isl_dim_out)
790 isl_die(ctx, isl_error_invalid,
791 "output/set dimension does not have a coefficient",
792 return 0);
793 if (type == isl_dim_in)
794 type = isl_dim_set;
796 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
797 return 0;
799 pos += isl_local_space_offset(aff->ls, type);
800 return isl_int_sgn(aff->v->el[1 + pos]);
803 /* Replace the numerator of the constant term of "aff" by "v".
805 * A NaN is unaffected by this operation.
807 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
809 if (!aff)
810 return NULL;
811 if (isl_aff_is_nan(aff))
812 return aff;
813 aff = isl_aff_cow(aff);
814 if (!aff)
815 return NULL;
817 aff->v = isl_vec_cow(aff->v);
818 if (!aff->v)
819 return isl_aff_free(aff);
821 isl_int_set(aff->v->el[1], v);
823 return aff;
826 /* Replace the constant term of "aff" by "v".
828 * A NaN is unaffected by this operation.
830 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
831 __isl_take isl_val *v)
833 if (!aff || !v)
834 goto error;
836 if (isl_aff_is_nan(aff)) {
837 isl_val_free(v);
838 return aff;
841 if (!isl_val_is_rat(v))
842 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
843 "expecting rational value", goto error);
845 if (isl_int_eq(aff->v->el[1], v->n) &&
846 isl_int_eq(aff->v->el[0], v->d)) {
847 isl_val_free(v);
848 return aff;
851 aff = isl_aff_cow(aff);
852 if (!aff)
853 goto error;
854 aff->v = isl_vec_cow(aff->v);
855 if (!aff->v)
856 goto error;
858 if (isl_int_eq(aff->v->el[0], v->d)) {
859 isl_int_set(aff->v->el[1], v->n);
860 } else if (isl_int_is_one(v->d)) {
861 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
862 } else {
863 isl_seq_scale(aff->v->el + 1,
864 aff->v->el + 1, v->d, aff->v->size - 1);
865 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
866 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
867 aff->v = isl_vec_normalize(aff->v);
868 if (!aff->v)
869 goto error;
872 isl_val_free(v);
873 return aff;
874 error:
875 isl_aff_free(aff);
876 isl_val_free(v);
877 return NULL;
880 /* Add "v" to the constant term of "aff".
882 * A NaN is unaffected by this operation.
884 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
886 if (isl_int_is_zero(v))
887 return aff;
889 if (!aff)
890 return NULL;
891 if (isl_aff_is_nan(aff))
892 return aff;
893 aff = isl_aff_cow(aff);
894 if (!aff)
895 return NULL;
897 aff->v = isl_vec_cow(aff->v);
898 if (!aff->v)
899 return isl_aff_free(aff);
901 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
903 return aff;
906 /* Add "v" to the constant term of "aff".
908 * A NaN is unaffected by this operation.
910 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
911 __isl_take isl_val *v)
913 if (!aff || !v)
914 goto error;
916 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
917 isl_val_free(v);
918 return aff;
921 if (!isl_val_is_rat(v))
922 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
923 "expecting rational value", goto error);
925 aff = isl_aff_cow(aff);
926 if (!aff)
927 goto error;
929 aff->v = isl_vec_cow(aff->v);
930 if (!aff->v)
931 goto error;
933 if (isl_int_is_one(v->d)) {
934 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
935 } else if (isl_int_eq(aff->v->el[0], v->d)) {
936 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
937 aff->v = isl_vec_normalize(aff->v);
938 if (!aff->v)
939 goto error;
940 } else {
941 isl_seq_scale(aff->v->el + 1,
942 aff->v->el + 1, v->d, aff->v->size - 1);
943 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
944 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
945 aff->v = isl_vec_normalize(aff->v);
946 if (!aff->v)
947 goto error;
950 isl_val_free(v);
951 return aff;
952 error:
953 isl_aff_free(aff);
954 isl_val_free(v);
955 return NULL;
958 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
960 isl_int t;
962 isl_int_init(t);
963 isl_int_set_si(t, v);
964 aff = isl_aff_add_constant(aff, t);
965 isl_int_clear(t);
967 return aff;
970 /* Add "v" to the numerator of the constant term of "aff".
972 * A NaN is unaffected by this operation.
974 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
976 if (isl_int_is_zero(v))
977 return aff;
979 if (!aff)
980 return NULL;
981 if (isl_aff_is_nan(aff))
982 return aff;
983 aff = isl_aff_cow(aff);
984 if (!aff)
985 return NULL;
987 aff->v = isl_vec_cow(aff->v);
988 if (!aff->v)
989 return isl_aff_free(aff);
991 isl_int_add(aff->v->el[1], aff->v->el[1], v);
993 return aff;
996 /* Add "v" to the numerator of the constant term of "aff".
998 * A NaN is unaffected by this operation.
1000 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1002 isl_int t;
1004 if (v == 0)
1005 return aff;
1007 isl_int_init(t);
1008 isl_int_set_si(t, v);
1009 aff = isl_aff_add_constant_num(aff, t);
1010 isl_int_clear(t);
1012 return aff;
1015 /* Replace the numerator of the constant term of "aff" by "v".
1017 * A NaN is unaffected by this operation.
1019 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1021 if (!aff)
1022 return NULL;
1023 if (isl_aff_is_nan(aff))
1024 return aff;
1025 aff = isl_aff_cow(aff);
1026 if (!aff)
1027 return NULL;
1029 aff->v = isl_vec_cow(aff->v);
1030 if (!aff->v)
1031 return isl_aff_free(aff);
1033 isl_int_set_si(aff->v->el[1], v);
1035 return aff;
1038 /* Replace the numerator of the coefficient of the variable of type "type"
1039 * at position "pos" of "aff" by "v".
1041 * A NaN is unaffected by this operation.
1043 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1044 enum isl_dim_type type, int pos, isl_int v)
1046 if (!aff)
1047 return NULL;
1049 if (type == isl_dim_out)
1050 isl_die(aff->v->ctx, isl_error_invalid,
1051 "output/set dimension does not have a coefficient",
1052 return isl_aff_free(aff));
1053 if (type == isl_dim_in)
1054 type = isl_dim_set;
1056 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1057 return isl_aff_free(aff);
1059 if (isl_aff_is_nan(aff))
1060 return aff;
1061 aff = isl_aff_cow(aff);
1062 if (!aff)
1063 return NULL;
1065 aff->v = isl_vec_cow(aff->v);
1066 if (!aff->v)
1067 return isl_aff_free(aff);
1069 pos += isl_local_space_offset(aff->ls, type);
1070 isl_int_set(aff->v->el[1 + pos], v);
1072 return aff;
1075 /* Replace the numerator of the coefficient of the variable of type "type"
1076 * at position "pos" of "aff" by "v".
1078 * A NaN is unaffected by this operation.
1080 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1081 enum isl_dim_type type, int pos, int v)
1083 if (!aff)
1084 return NULL;
1086 if (type == isl_dim_out)
1087 isl_die(aff->v->ctx, isl_error_invalid,
1088 "output/set dimension does not have a coefficient",
1089 return isl_aff_free(aff));
1090 if (type == isl_dim_in)
1091 type = isl_dim_set;
1093 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1094 return isl_aff_free(aff);
1096 if (isl_aff_is_nan(aff))
1097 return aff;
1098 pos += isl_local_space_offset(aff->ls, type);
1099 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1100 return aff;
1102 aff = isl_aff_cow(aff);
1103 if (!aff)
1104 return NULL;
1106 aff->v = isl_vec_cow(aff->v);
1107 if (!aff->v)
1108 return isl_aff_free(aff);
1110 isl_int_set_si(aff->v->el[1 + pos], v);
1112 return aff;
1115 /* Replace the coefficient of the variable of type "type" at position "pos"
1116 * of "aff" by "v".
1118 * A NaN is unaffected by this operation.
1120 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1121 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1123 if (!aff || !v)
1124 goto error;
1126 if (type == isl_dim_out)
1127 isl_die(aff->v->ctx, isl_error_invalid,
1128 "output/set dimension does not have a coefficient",
1129 goto error);
1130 if (type == isl_dim_in)
1131 type = isl_dim_set;
1133 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1134 return isl_aff_free(aff);
1136 if (isl_aff_is_nan(aff)) {
1137 isl_val_free(v);
1138 return aff;
1140 if (!isl_val_is_rat(v))
1141 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1142 "expecting rational value", goto error);
1144 pos += isl_local_space_offset(aff->ls, type);
1145 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1146 isl_int_eq(aff->v->el[0], v->d)) {
1147 isl_val_free(v);
1148 return aff;
1151 aff = isl_aff_cow(aff);
1152 if (!aff)
1153 goto error;
1154 aff->v = isl_vec_cow(aff->v);
1155 if (!aff->v)
1156 goto error;
1158 if (isl_int_eq(aff->v->el[0], v->d)) {
1159 isl_int_set(aff->v->el[1 + pos], v->n);
1160 } else if (isl_int_is_one(v->d)) {
1161 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1162 } else {
1163 isl_seq_scale(aff->v->el + 1,
1164 aff->v->el + 1, v->d, aff->v->size - 1);
1165 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1166 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1167 aff->v = isl_vec_normalize(aff->v);
1168 if (!aff->v)
1169 goto error;
1172 isl_val_free(v);
1173 return aff;
1174 error:
1175 isl_aff_free(aff);
1176 isl_val_free(v);
1177 return NULL;
1180 /* Add "v" to the coefficient of the variable of type "type"
1181 * at position "pos" of "aff".
1183 * A NaN is unaffected by this operation.
1185 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1186 enum isl_dim_type type, int pos, isl_int v)
1188 if (!aff)
1189 return NULL;
1191 if (type == isl_dim_out)
1192 isl_die(aff->v->ctx, isl_error_invalid,
1193 "output/set dimension does not have a coefficient",
1194 return isl_aff_free(aff));
1195 if (type == isl_dim_in)
1196 type = isl_dim_set;
1198 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1199 return isl_aff_free(aff);
1201 if (isl_aff_is_nan(aff))
1202 return aff;
1203 aff = isl_aff_cow(aff);
1204 if (!aff)
1205 return NULL;
1207 aff->v = isl_vec_cow(aff->v);
1208 if (!aff->v)
1209 return isl_aff_free(aff);
1211 pos += isl_local_space_offset(aff->ls, type);
1212 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1214 return aff;
1217 /* Add "v" to the coefficient of the variable of type "type"
1218 * at position "pos" of "aff".
1220 * A NaN is unaffected by this operation.
1222 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1223 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1225 if (!aff || !v)
1226 goto error;
1228 if (isl_val_is_zero(v)) {
1229 isl_val_free(v);
1230 return aff;
1233 if (type == isl_dim_out)
1234 isl_die(aff->v->ctx, isl_error_invalid,
1235 "output/set dimension does not have a coefficient",
1236 goto error);
1237 if (type == isl_dim_in)
1238 type = isl_dim_set;
1240 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1241 goto error;
1243 if (isl_aff_is_nan(aff)) {
1244 isl_val_free(v);
1245 return aff;
1247 if (!isl_val_is_rat(v))
1248 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1249 "expecting rational value", goto error);
1251 aff = isl_aff_cow(aff);
1252 if (!aff)
1253 goto error;
1255 aff->v = isl_vec_cow(aff->v);
1256 if (!aff->v)
1257 goto error;
1259 pos += isl_local_space_offset(aff->ls, type);
1260 if (isl_int_is_one(v->d)) {
1261 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1262 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1263 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1264 aff->v = isl_vec_normalize(aff->v);
1265 if (!aff->v)
1266 goto error;
1267 } else {
1268 isl_seq_scale(aff->v->el + 1,
1269 aff->v->el + 1, v->d, aff->v->size - 1);
1270 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1271 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1272 aff->v = isl_vec_normalize(aff->v);
1273 if (!aff->v)
1274 goto error;
1277 isl_val_free(v);
1278 return aff;
1279 error:
1280 isl_aff_free(aff);
1281 isl_val_free(v);
1282 return NULL;
1285 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1286 enum isl_dim_type type, int pos, int v)
1288 isl_int t;
1290 isl_int_init(t);
1291 isl_int_set_si(t, v);
1292 aff = isl_aff_add_coefficient(aff, type, pos, t);
1293 isl_int_clear(t);
1295 return aff;
1298 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1300 if (!aff)
1301 return NULL;
1303 return isl_local_space_get_div(aff->ls, pos);
1306 /* Return the negation of "aff".
1308 * As a special case, -NaN = NaN.
1310 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1312 if (!aff)
1313 return NULL;
1314 if (isl_aff_is_nan(aff))
1315 return aff;
1316 aff = isl_aff_cow(aff);
1317 if (!aff)
1318 return NULL;
1319 aff->v = isl_vec_cow(aff->v);
1320 if (!aff->v)
1321 return isl_aff_free(aff);
1323 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1325 return aff;
1328 /* Remove divs from the local space that do not appear in the affine
1329 * expression.
1330 * We currently only remove divs at the end.
1331 * Some intermediate divs may also not appear directly in the affine
1332 * expression, but we would also need to check that no other divs are
1333 * defined in terms of them.
1335 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1337 int pos;
1338 isl_size off;
1339 isl_size n;
1341 n = isl_aff_domain_dim(aff, isl_dim_div);
1342 off = isl_aff_domain_offset(aff, isl_dim_div);
1343 if (n < 0 || off < 0)
1344 return isl_aff_free(aff);
1346 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1347 if (pos == n)
1348 return aff;
1350 aff = isl_aff_cow(aff);
1351 if (!aff)
1352 return NULL;
1354 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1355 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1356 if (!aff->ls || !aff->v)
1357 return isl_aff_free(aff);
1359 return aff;
1362 /* Look for any divs in the aff->ls with a denominator equal to one
1363 * and plug them into the affine expression and any subsequent divs
1364 * that may reference the div.
1366 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1368 int i;
1369 isl_size n;
1370 int len;
1371 isl_int v;
1372 isl_vec *vec;
1373 isl_local_space *ls;
1374 isl_size off;
1376 n = isl_aff_domain_dim(aff, isl_dim_div);
1377 off = isl_aff_domain_offset(aff, isl_dim_div);
1378 if (n < 0 || off < 0)
1379 return isl_aff_free(aff);
1380 len = aff->v->size;
1381 for (i = 0; i < n; ++i) {
1382 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1383 continue;
1384 ls = isl_local_space_copy(aff->ls);
1385 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1386 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1387 vec = isl_vec_copy(aff->v);
1388 vec = isl_vec_cow(vec);
1389 if (!ls || !vec)
1390 goto error;
1392 isl_int_init(v);
1394 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1395 len, len, v);
1397 isl_int_clear(v);
1399 isl_vec_free(aff->v);
1400 aff->v = vec;
1401 isl_local_space_free(aff->ls);
1402 aff->ls = ls;
1405 return aff;
1406 error:
1407 isl_vec_free(vec);
1408 isl_local_space_free(ls);
1409 return isl_aff_free(aff);
1412 /* Look for any divs j that appear with a unit coefficient inside
1413 * the definitions of other divs i and plug them into the definitions
1414 * of the divs i.
1416 * In particular, an expression of the form
1418 * floor((f(..) + floor(g(..)/n))/m)
1420 * is simplified to
1422 * floor((n * f(..) + g(..))/(n * m))
1424 * This simplification is correct because we can move the expression
1425 * f(..) into the inner floor in the original expression to obtain
1427 * floor(floor((n * f(..) + g(..))/n)/m)
1429 * from which we can derive the simplified expression.
1431 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1433 int i, j;
1434 isl_size n;
1435 isl_size off;
1437 n = isl_aff_domain_dim(aff, isl_dim_div);
1438 off = isl_aff_domain_offset(aff, isl_dim_div);
1439 if (n < 0 || off < 0)
1440 return isl_aff_free(aff);
1441 for (i = 1; i < n; ++i) {
1442 for (j = 0; j < i; ++j) {
1443 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1444 continue;
1445 aff->ls = isl_local_space_substitute_seq(aff->ls,
1446 isl_dim_div, j, aff->ls->div->row[j],
1447 aff->v->size, i, 1);
1448 if (!aff->ls)
1449 return isl_aff_free(aff);
1453 return aff;
1456 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1458 * Even though this function is only called on isl_affs with a single
1459 * reference, we are careful to only change aff->v and aff->ls together.
1461 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1463 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1464 isl_local_space *ls;
1465 isl_vec *v;
1467 if (off < 0)
1468 return isl_aff_free(aff);
1470 ls = isl_local_space_copy(aff->ls);
1471 ls = isl_local_space_swap_div(ls, a, b);
1472 v = isl_vec_copy(aff->v);
1473 v = isl_vec_cow(v);
1474 if (!ls || !v)
1475 goto error;
1477 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1478 isl_vec_free(aff->v);
1479 aff->v = v;
1480 isl_local_space_free(aff->ls);
1481 aff->ls = ls;
1483 return aff;
1484 error:
1485 isl_vec_free(v);
1486 isl_local_space_free(ls);
1487 return isl_aff_free(aff);
1490 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1492 * We currently do not actually remove div "b", but simply add its
1493 * coefficient to that of "a" and then zero it out.
1495 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1497 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1499 if (off < 0)
1500 return isl_aff_free(aff);
1502 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1503 return aff;
1505 aff->v = isl_vec_cow(aff->v);
1506 if (!aff->v)
1507 return isl_aff_free(aff);
1509 isl_int_add(aff->v->el[1 + off + a],
1510 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1511 isl_int_set_si(aff->v->el[1 + off + b], 0);
1513 return aff;
1516 /* Sort the divs in the local space of "aff" according to
1517 * the comparison function "cmp_row" in isl_local_space.c,
1518 * combining the coefficients of identical divs.
1520 * Reordering divs does not change the semantics of "aff",
1521 * so there is no need to call isl_aff_cow.
1522 * Moreover, this function is currently only called on isl_affs
1523 * with a single reference.
1525 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1527 isl_size n;
1528 int i, j;
1530 n = isl_aff_dim(aff, isl_dim_div);
1531 if (n < 0)
1532 return isl_aff_free(aff);
1533 for (i = 1; i < n; ++i) {
1534 for (j = i - 1; j >= 0; --j) {
1535 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1536 if (cmp < 0)
1537 break;
1538 if (cmp == 0)
1539 aff = merge_divs(aff, j, j + 1);
1540 else
1541 aff = swap_div(aff, j, j + 1);
1542 if (!aff)
1543 return NULL;
1547 return aff;
1550 /* Normalize the representation of "aff".
1552 * This function should only be called of "new" isl_affs, i.e.,
1553 * with only a single reference. We therefore do not need to
1554 * worry about affecting other instances.
1556 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1558 if (!aff)
1559 return NULL;
1560 aff->v = isl_vec_normalize(aff->v);
1561 if (!aff->v)
1562 return isl_aff_free(aff);
1563 aff = plug_in_integral_divs(aff);
1564 aff = plug_in_unit_divs(aff);
1565 aff = sort_divs(aff);
1566 aff = isl_aff_remove_unused_divs(aff);
1567 return aff;
1570 /* Given f, return floor(f).
1571 * If f is an integer expression, then just return f.
1572 * If f is a constant, then return the constant floor(f).
1573 * Otherwise, if f = g/m, write g = q m + r,
1574 * create a new div d = [r/m] and return the expression q + d.
1575 * The coefficients in r are taken to lie between -m/2 and m/2.
1577 * reduce_div_coefficients performs the same normalization.
1579 * As a special case, floor(NaN) = NaN.
1581 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1583 int i;
1584 int size;
1585 isl_ctx *ctx;
1586 isl_vec *div;
1588 if (!aff)
1589 return NULL;
1591 if (isl_aff_is_nan(aff))
1592 return aff;
1593 if (isl_int_is_one(aff->v->el[0]))
1594 return aff;
1596 aff = isl_aff_cow(aff);
1597 if (!aff)
1598 return NULL;
1600 aff->v = isl_vec_cow(aff->v);
1601 if (!aff->v)
1602 return isl_aff_free(aff);
1604 if (isl_aff_is_cst(aff)) {
1605 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1606 isl_int_set_si(aff->v->el[0], 1);
1607 return aff;
1610 div = isl_vec_copy(aff->v);
1611 div = isl_vec_cow(div);
1612 if (!div)
1613 return isl_aff_free(aff);
1615 ctx = isl_aff_get_ctx(aff);
1616 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1617 for (i = 1; i < aff->v->size; ++i) {
1618 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1619 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1620 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1621 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1622 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1626 aff->ls = isl_local_space_add_div(aff->ls, div);
1627 if (!aff->ls)
1628 return isl_aff_free(aff);
1630 size = aff->v->size;
1631 aff->v = isl_vec_extend(aff->v, size + 1);
1632 if (!aff->v)
1633 return isl_aff_free(aff);
1634 isl_int_set_si(aff->v->el[0], 1);
1635 isl_int_set_si(aff->v->el[size], 1);
1637 aff = isl_aff_normalize(aff);
1639 return aff;
1642 /* Compute
1644 * aff mod m = aff - m * floor(aff/m)
1646 * with m an integer value.
1648 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1649 __isl_take isl_val *m)
1651 isl_aff *res;
1653 if (!aff || !m)
1654 goto error;
1656 if (!isl_val_is_int(m))
1657 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1658 "expecting integer modulo", goto error);
1660 res = isl_aff_copy(aff);
1661 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1662 aff = isl_aff_floor(aff);
1663 aff = isl_aff_scale_val(aff, m);
1664 res = isl_aff_sub(res, aff);
1666 return res;
1667 error:
1668 isl_aff_free(aff);
1669 isl_val_free(m);
1670 return NULL;
1673 /* Compute
1675 * pwaff mod m = pwaff - m * floor(pwaff/m)
1677 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1679 isl_pw_aff *res;
1681 res = isl_pw_aff_copy(pwaff);
1682 pwaff = isl_pw_aff_scale_down(pwaff, m);
1683 pwaff = isl_pw_aff_floor(pwaff);
1684 pwaff = isl_pw_aff_scale(pwaff, m);
1685 res = isl_pw_aff_sub(res, pwaff);
1687 return res;
1690 /* Compute
1692 * pa mod m = pa - m * floor(pa/m)
1694 * with m an integer value.
1696 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1697 __isl_take isl_val *m)
1699 if (!pa || !m)
1700 goto error;
1701 if (!isl_val_is_int(m))
1702 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1703 "expecting integer modulo", goto error);
1704 pa = isl_pw_aff_mod(pa, m->n);
1705 isl_val_free(m);
1706 return pa;
1707 error:
1708 isl_pw_aff_free(pa);
1709 isl_val_free(m);
1710 return NULL;
1713 /* Given f, return ceil(f).
1714 * If f is an integer expression, then just return f.
1715 * Otherwise, let f be the expression
1717 * e/m
1719 * then return
1721 * floor((e + m - 1)/m)
1723 * As a special case, ceil(NaN) = NaN.
1725 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1727 if (!aff)
1728 return NULL;
1730 if (isl_aff_is_nan(aff))
1731 return aff;
1732 if (isl_int_is_one(aff->v->el[0]))
1733 return aff;
1735 aff = isl_aff_cow(aff);
1736 if (!aff)
1737 return NULL;
1738 aff->v = isl_vec_cow(aff->v);
1739 if (!aff->v)
1740 return isl_aff_free(aff);
1742 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1743 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1744 aff = isl_aff_floor(aff);
1746 return aff;
1749 /* Apply the expansion computed by isl_merge_divs.
1750 * The expansion itself is given by "exp" while the resulting
1751 * list of divs is given by "div".
1753 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1754 __isl_take isl_mat *div, int *exp)
1756 isl_size old_n_div;
1757 isl_size new_n_div;
1758 isl_size offset;
1760 aff = isl_aff_cow(aff);
1762 offset = isl_aff_domain_offset(aff, isl_dim_div);
1763 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1764 new_n_div = isl_mat_rows(div);
1765 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1766 goto error;
1768 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1769 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1770 if (!aff->v || !aff->ls)
1771 return isl_aff_free(aff);
1772 return aff;
1773 error:
1774 isl_aff_free(aff);
1775 isl_mat_free(div);
1776 return NULL;
1779 /* Add two affine expressions that live in the same local space.
1781 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1782 __isl_take isl_aff *aff2)
1784 isl_int gcd, f;
1786 aff1 = isl_aff_cow(aff1);
1787 if (!aff1 || !aff2)
1788 goto error;
1790 aff1->v = isl_vec_cow(aff1->v);
1791 if (!aff1->v)
1792 goto error;
1794 isl_int_init(gcd);
1795 isl_int_init(f);
1796 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1797 isl_int_divexact(f, aff2->v->el[0], gcd);
1798 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1799 isl_int_divexact(f, aff1->v->el[0], gcd);
1800 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1801 isl_int_divexact(f, aff2->v->el[0], gcd);
1802 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1803 isl_int_clear(f);
1804 isl_int_clear(gcd);
1806 isl_aff_free(aff2);
1807 return aff1;
1808 error:
1809 isl_aff_free(aff1);
1810 isl_aff_free(aff2);
1811 return NULL;
1814 /* Return the sum of "aff1" and "aff2".
1816 * If either of the two is NaN, then the result is NaN.
1818 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1819 __isl_take isl_aff *aff2)
1821 isl_ctx *ctx;
1822 int *exp1 = NULL;
1823 int *exp2 = NULL;
1824 isl_mat *div;
1825 isl_size n_div1, n_div2;
1827 if (!aff1 || !aff2)
1828 goto error;
1830 ctx = isl_aff_get_ctx(aff1);
1831 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1832 isl_die(ctx, isl_error_invalid,
1833 "spaces don't match", goto error);
1835 if (isl_aff_is_nan(aff1)) {
1836 isl_aff_free(aff2);
1837 return aff1;
1839 if (isl_aff_is_nan(aff2)) {
1840 isl_aff_free(aff1);
1841 return aff2;
1844 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1845 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1846 if (n_div1 < 0 || n_div2 < 0)
1847 goto error;
1848 if (n_div1 == 0 && n_div2 == 0)
1849 return add_expanded(aff1, aff2);
1851 exp1 = isl_alloc_array(ctx, int, n_div1);
1852 exp2 = isl_alloc_array(ctx, int, n_div2);
1853 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1854 goto error;
1856 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1857 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1858 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1859 free(exp1);
1860 free(exp2);
1862 return add_expanded(aff1, aff2);
1863 error:
1864 free(exp1);
1865 free(exp2);
1866 isl_aff_free(aff1);
1867 isl_aff_free(aff2);
1868 return NULL;
1871 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1872 __isl_take isl_aff *aff2)
1874 return isl_aff_add(aff1, isl_aff_neg(aff2));
1877 /* Return the result of scaling "aff" by a factor of "f".
1879 * As a special case, f * NaN = NaN.
1881 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1883 isl_int gcd;
1885 if (!aff)
1886 return NULL;
1887 if (isl_aff_is_nan(aff))
1888 return aff;
1890 if (isl_int_is_one(f))
1891 return aff;
1893 aff = isl_aff_cow(aff);
1894 if (!aff)
1895 return NULL;
1896 aff->v = isl_vec_cow(aff->v);
1897 if (!aff->v)
1898 return isl_aff_free(aff);
1900 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1901 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1902 return aff;
1905 isl_int_init(gcd);
1906 isl_int_gcd(gcd, aff->v->el[0], f);
1907 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1908 isl_int_divexact(gcd, f, gcd);
1909 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1910 isl_int_clear(gcd);
1912 return aff;
1915 /* Multiple "aff" by "v".
1917 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1918 __isl_take isl_val *v)
1920 if (!aff || !v)
1921 goto error;
1923 if (isl_val_is_one(v)) {
1924 isl_val_free(v);
1925 return aff;
1928 if (!isl_val_is_rat(v))
1929 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1930 "expecting rational factor", goto error);
1932 aff = isl_aff_scale(aff, v->n);
1933 aff = isl_aff_scale_down(aff, v->d);
1935 isl_val_free(v);
1936 return aff;
1937 error:
1938 isl_aff_free(aff);
1939 isl_val_free(v);
1940 return NULL;
1943 /* Return the result of scaling "aff" down by a factor of "f".
1945 * As a special case, NaN/f = NaN.
1947 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1949 isl_int gcd;
1951 if (!aff)
1952 return NULL;
1953 if (isl_aff_is_nan(aff))
1954 return aff;
1956 if (isl_int_is_one(f))
1957 return aff;
1959 aff = isl_aff_cow(aff);
1960 if (!aff)
1961 return NULL;
1963 if (isl_int_is_zero(f))
1964 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1965 "cannot scale down by zero", return isl_aff_free(aff));
1967 aff->v = isl_vec_cow(aff->v);
1968 if (!aff->v)
1969 return isl_aff_free(aff);
1971 isl_int_init(gcd);
1972 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1973 isl_int_gcd(gcd, gcd, f);
1974 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1975 isl_int_divexact(gcd, f, gcd);
1976 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1977 isl_int_clear(gcd);
1979 return aff;
1982 /* Divide "aff" by "v".
1984 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1985 __isl_take isl_val *v)
1987 if (!aff || !v)
1988 goto error;
1990 if (isl_val_is_one(v)) {
1991 isl_val_free(v);
1992 return aff;
1995 if (!isl_val_is_rat(v))
1996 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1997 "expecting rational factor", goto error);
1998 if (!isl_val_is_pos(v))
1999 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2000 "factor needs to be positive", goto error);
2002 aff = isl_aff_scale(aff, v->d);
2003 aff = isl_aff_scale_down(aff, v->n);
2005 isl_val_free(v);
2006 return aff;
2007 error:
2008 isl_aff_free(aff);
2009 isl_val_free(v);
2010 return NULL;
2013 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2015 isl_int v;
2017 if (f == 1)
2018 return aff;
2020 isl_int_init(v);
2021 isl_int_set_ui(v, f);
2022 aff = isl_aff_scale_down(aff, v);
2023 isl_int_clear(v);
2025 return aff;
2028 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2029 enum isl_dim_type type, unsigned pos, const char *s)
2031 aff = isl_aff_cow(aff);
2032 if (!aff)
2033 return NULL;
2034 if (type == isl_dim_out)
2035 isl_die(aff->v->ctx, isl_error_invalid,
2036 "cannot set name of output/set dimension",
2037 return isl_aff_free(aff));
2038 if (type == isl_dim_in)
2039 type = isl_dim_set;
2040 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2041 if (!aff->ls)
2042 return isl_aff_free(aff);
2044 return aff;
2047 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2048 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2050 aff = isl_aff_cow(aff);
2051 if (!aff)
2052 goto error;
2053 if (type == isl_dim_out)
2054 isl_die(aff->v->ctx, isl_error_invalid,
2055 "cannot set name of output/set dimension",
2056 goto error);
2057 if (type == isl_dim_in)
2058 type = isl_dim_set;
2059 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2060 if (!aff->ls)
2061 return isl_aff_free(aff);
2063 return aff;
2064 error:
2065 isl_id_free(id);
2066 isl_aff_free(aff);
2067 return NULL;
2070 /* Replace the identifier of the input tuple of "aff" by "id".
2071 * type is currently required to be equal to isl_dim_in
2073 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2074 enum isl_dim_type type, __isl_take isl_id *id)
2076 aff = isl_aff_cow(aff);
2077 if (!aff)
2078 goto error;
2079 if (type != isl_dim_in)
2080 isl_die(aff->v->ctx, isl_error_invalid,
2081 "cannot only set id of input tuple", goto error);
2082 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2083 if (!aff->ls)
2084 return isl_aff_free(aff);
2086 return aff;
2087 error:
2088 isl_id_free(id);
2089 isl_aff_free(aff);
2090 return NULL;
2093 /* Exploit the equalities in "eq" to simplify the affine expression
2094 * and the expressions of the integer divisions in the local space.
2095 * The integer divisions in this local space are assumed to appear
2096 * as regular dimensions in "eq".
2098 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2099 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2101 int i, j;
2102 unsigned o_div;
2103 unsigned n_div;
2105 if (!eq)
2106 goto error;
2107 if (eq->n_eq == 0) {
2108 isl_basic_set_free(eq);
2109 return aff;
2112 aff = isl_aff_cow(aff);
2113 if (!aff)
2114 goto error;
2116 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2117 isl_basic_set_copy(eq));
2118 aff->v = isl_vec_cow(aff->v);
2119 if (!aff->ls || !aff->v)
2120 goto error;
2122 o_div = isl_basic_set_offset(eq, isl_dim_div);
2123 n_div = eq->n_div;
2124 for (i = 0; i < eq->n_eq; ++i) {
2125 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2126 if (j < 0 || j == 0 || j >= o_div)
2127 continue;
2129 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2130 &aff->v->el[0]);
2133 isl_basic_set_free(eq);
2134 aff = isl_aff_normalize(aff);
2135 return aff;
2136 error:
2137 isl_basic_set_free(eq);
2138 isl_aff_free(aff);
2139 return NULL;
2142 /* Exploit the equalities in "eq" to simplify the affine expression
2143 * and the expressions of the integer divisions in the local space.
2145 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2146 __isl_take isl_basic_set *eq)
2148 isl_size n_div;
2150 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2151 if (n_div < 0)
2152 goto error;
2153 if (n_div > 0)
2154 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2155 return isl_aff_substitute_equalities_lifted(aff, eq);
2156 error:
2157 isl_basic_set_free(eq);
2158 isl_aff_free(aff);
2159 return NULL;
2162 /* Look for equalities among the variables shared by context and aff
2163 * and the integer divisions of aff, if any.
2164 * The equalities are then used to eliminate coefficients and/or integer
2165 * divisions from aff.
2167 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2168 __isl_take isl_set *context)
2170 isl_local_space *ls;
2171 isl_basic_set *hull;
2173 ls = isl_aff_get_domain_local_space(aff);
2174 context = isl_local_space_lift_set(ls, context);
2176 hull = isl_set_affine_hull(context);
2177 return isl_aff_substitute_equalities_lifted(aff, hull);
2180 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2181 __isl_take isl_set *context)
2183 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2184 dom_context = isl_set_intersect_params(dom_context, context);
2185 return isl_aff_gist(aff, dom_context);
2188 /* Return a basic set containing those elements in the space
2189 * of aff where it is positive. "rational" should not be set.
2191 * If "aff" is NaN, then it is not positive.
2193 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2194 int rational, void *user)
2196 isl_constraint *ineq;
2197 isl_basic_set *bset;
2198 isl_val *c;
2200 if (!aff)
2201 return NULL;
2202 if (isl_aff_is_nan(aff)) {
2203 isl_space *space = isl_aff_get_domain_space(aff);
2204 isl_aff_free(aff);
2205 return isl_basic_set_empty(space);
2207 if (rational)
2208 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2209 "rational sets not supported", goto error);
2211 ineq = isl_inequality_from_aff(aff);
2212 c = isl_constraint_get_constant_val(ineq);
2213 c = isl_val_sub_ui(c, 1);
2214 ineq = isl_constraint_set_constant_val(ineq, c);
2216 bset = isl_basic_set_from_constraint(ineq);
2217 bset = isl_basic_set_simplify(bset);
2218 return bset;
2219 error:
2220 isl_aff_free(aff);
2221 return NULL;
2224 /* Return a basic set containing those elements in the space
2225 * of aff where it is non-negative.
2226 * If "rational" is set, then return a rational basic set.
2228 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2230 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2231 __isl_take isl_aff *aff, int rational, void *user)
2233 isl_constraint *ineq;
2234 isl_basic_set *bset;
2236 if (!aff)
2237 return NULL;
2238 if (isl_aff_is_nan(aff)) {
2239 isl_space *space = isl_aff_get_domain_space(aff);
2240 isl_aff_free(aff);
2241 return isl_basic_set_empty(space);
2244 ineq = isl_inequality_from_aff(aff);
2246 bset = isl_basic_set_from_constraint(ineq);
2247 if (rational)
2248 bset = isl_basic_set_set_rational(bset);
2249 bset = isl_basic_set_simplify(bset);
2250 return bset;
2253 /* Return a basic set containing those elements in the space
2254 * of aff where it is non-negative.
2256 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2258 return aff_nonneg_basic_set(aff, 0, NULL);
2261 /* Return a basic set containing those elements in the domain space
2262 * of "aff" where it is positive.
2264 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2266 aff = isl_aff_add_constant_num_si(aff, -1);
2267 return isl_aff_nonneg_basic_set(aff);
2270 /* Return a basic set containing those elements in the domain space
2271 * of aff where it is negative.
2273 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2275 aff = isl_aff_neg(aff);
2276 return isl_aff_pos_basic_set(aff);
2279 /* Return a basic set containing those elements in the space
2280 * of aff where it is zero.
2281 * If "rational" is set, then return a rational basic set.
2283 * If "aff" is NaN, then it is not zero.
2285 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2286 int rational, void *user)
2288 isl_constraint *ineq;
2289 isl_basic_set *bset;
2291 if (!aff)
2292 return NULL;
2293 if (isl_aff_is_nan(aff)) {
2294 isl_space *space = isl_aff_get_domain_space(aff);
2295 isl_aff_free(aff);
2296 return isl_basic_set_empty(space);
2299 ineq = isl_equality_from_aff(aff);
2301 bset = isl_basic_set_from_constraint(ineq);
2302 if (rational)
2303 bset = isl_basic_set_set_rational(bset);
2304 bset = isl_basic_set_simplify(bset);
2305 return bset;
2308 /* Return a basic set containing those elements in the space
2309 * of aff where it is zero.
2311 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2313 return aff_zero_basic_set(aff, 0, NULL);
2316 /* Return a basic set containing those elements in the shared space
2317 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2319 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2320 __isl_take isl_aff *aff2)
2322 aff1 = isl_aff_sub(aff1, aff2);
2324 return isl_aff_nonneg_basic_set(aff1);
2327 /* Return a basic set containing those elements in the shared domain space
2328 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2330 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2331 __isl_take isl_aff *aff2)
2333 aff1 = isl_aff_sub(aff1, aff2);
2335 return isl_aff_pos_basic_set(aff1);
2338 /* Return a set containing those elements in the shared space
2339 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2341 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2342 __isl_take isl_aff *aff2)
2344 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2347 /* Return a set containing those elements in the shared domain space
2348 * of aff1 and aff2 where aff1 is greater than aff2.
2350 * If either of the two inputs is NaN, then the result is empty,
2351 * as comparisons with NaN always return false.
2353 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2354 __isl_take isl_aff *aff2)
2356 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2359 /* Return a basic set containing those elements in the shared space
2360 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2362 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2363 __isl_take isl_aff *aff2)
2365 return isl_aff_ge_basic_set(aff2, aff1);
2368 /* Return a basic set containing those elements in the shared domain space
2369 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2371 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2372 __isl_take isl_aff *aff2)
2374 return isl_aff_gt_basic_set(aff2, aff1);
2377 /* Return a set containing those elements in the shared space
2378 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2380 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2381 __isl_take isl_aff *aff2)
2383 return isl_aff_ge_set(aff2, aff1);
2386 /* Return a set containing those elements in the shared domain space
2387 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2389 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2390 __isl_take isl_aff *aff2)
2392 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2395 /* Return a basic set containing those elements in the shared space
2396 * of aff1 and aff2 where aff1 and aff2 are equal.
2398 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2399 __isl_take isl_aff *aff2)
2401 aff1 = isl_aff_sub(aff1, aff2);
2403 return isl_aff_zero_basic_set(aff1);
2406 /* Return a set containing those elements in the shared space
2407 * of aff1 and aff2 where aff1 and aff2 are equal.
2409 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2410 __isl_take isl_aff *aff2)
2412 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2415 /* Return a set containing those elements in the shared domain space
2416 * of aff1 and aff2 where aff1 and aff2 are not equal.
2418 * If either of the two inputs is NaN, then the result is empty,
2419 * as comparisons with NaN always return false.
2421 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2422 __isl_take isl_aff *aff2)
2424 isl_set *set_lt, *set_gt;
2426 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2427 isl_aff_copy(aff2));
2428 set_gt = isl_aff_gt_set(aff1, aff2);
2429 return isl_set_union_disjoint(set_lt, set_gt);
2432 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2433 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2435 aff1 = isl_aff_add(aff1, aff2);
2436 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2437 return aff1;
2440 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2442 if (!aff)
2443 return isl_bool_error;
2445 return isl_bool_false;
2448 #undef TYPE
2449 #define TYPE isl_aff
2450 static
2451 #include "check_type_range_templ.c"
2453 /* Check whether the given affine expression has non-zero coefficient
2454 * for any dimension in the given range or if any of these dimensions
2455 * appear with non-zero coefficients in any of the integer divisions
2456 * involved in the affine expression.
2458 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2459 enum isl_dim_type type, unsigned first, unsigned n)
2461 int i;
2462 int *active = NULL;
2463 isl_bool involves = isl_bool_false;
2465 if (!aff)
2466 return isl_bool_error;
2467 if (n == 0)
2468 return isl_bool_false;
2469 if (isl_aff_check_range(aff, type, first, n) < 0)
2470 return isl_bool_error;
2472 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2473 if (!active)
2474 goto error;
2476 first += isl_local_space_offset(aff->ls, type) - 1;
2477 for (i = 0; i < n; ++i)
2478 if (active[first + i]) {
2479 involves = isl_bool_true;
2480 break;
2483 free(active);
2485 return involves;
2486 error:
2487 free(active);
2488 return isl_bool_error;
2491 /* Does "aff" involve any local variables, i.e., integer divisions?
2493 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2495 isl_size n;
2497 n = isl_aff_dim(aff, isl_dim_div);
2498 if (n < 0)
2499 return isl_bool_error;
2500 return isl_aff_involves_dims(aff, isl_dim_div, 0, n);
2503 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2504 enum isl_dim_type type, unsigned first, unsigned n)
2506 isl_ctx *ctx;
2508 if (!aff)
2509 return NULL;
2510 if (type == isl_dim_out)
2511 isl_die(aff->v->ctx, isl_error_invalid,
2512 "cannot drop output/set dimension",
2513 return isl_aff_free(aff));
2514 if (type == isl_dim_in)
2515 type = isl_dim_set;
2516 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2517 return aff;
2519 ctx = isl_aff_get_ctx(aff);
2520 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2521 return isl_aff_free(aff);
2523 aff = isl_aff_cow(aff);
2524 if (!aff)
2525 return NULL;
2527 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2528 if (!aff->ls)
2529 return isl_aff_free(aff);
2531 first += 1 + isl_local_space_offset(aff->ls, type);
2532 aff->v = isl_vec_drop_els(aff->v, first, n);
2533 if (!aff->v)
2534 return isl_aff_free(aff);
2536 return aff;
2539 /* Is the domain of "aff" a product?
2541 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2543 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2546 #undef TYPE
2547 #define TYPE isl_aff
2548 #include <isl_domain_factor_templ.c>
2550 /* Project the domain of the affine expression onto its parameter space.
2551 * The affine expression may not involve any of the domain dimensions.
2553 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2555 isl_space *space;
2556 isl_size n;
2558 n = isl_aff_dim(aff, isl_dim_in);
2559 if (n < 0)
2560 return isl_aff_free(aff);
2561 aff = isl_aff_drop_domain(aff, 0, n);
2562 space = isl_aff_get_domain_space(aff);
2563 space = isl_space_params(space);
2564 aff = isl_aff_reset_domain_space(aff, space);
2565 return aff;
2568 /* Convert an affine expression defined over a parameter domain
2569 * into one that is defined over a zero-dimensional set.
2571 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2573 isl_local_space *ls;
2575 ls = isl_aff_take_domain_local_space(aff);
2576 ls = isl_local_space_set_from_params(ls);
2577 aff = isl_aff_restore_domain_local_space(aff, ls);
2579 return aff;
2582 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2583 enum isl_dim_type type, unsigned first, unsigned n)
2585 isl_ctx *ctx;
2587 if (!aff)
2588 return NULL;
2589 if (type == isl_dim_out)
2590 isl_die(aff->v->ctx, isl_error_invalid,
2591 "cannot insert output/set dimensions",
2592 return isl_aff_free(aff));
2593 if (type == isl_dim_in)
2594 type = isl_dim_set;
2595 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2596 return aff;
2598 ctx = isl_aff_get_ctx(aff);
2599 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2600 return isl_aff_free(aff);
2602 aff = isl_aff_cow(aff);
2603 if (!aff)
2604 return NULL;
2606 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2607 if (!aff->ls)
2608 return isl_aff_free(aff);
2610 first += 1 + isl_local_space_offset(aff->ls, type);
2611 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2612 if (!aff->v)
2613 return isl_aff_free(aff);
2615 return aff;
2618 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2619 enum isl_dim_type type, unsigned n)
2621 isl_size pos;
2623 pos = isl_aff_dim(aff, type);
2624 if (pos < 0)
2625 return isl_aff_free(aff);
2627 return isl_aff_insert_dims(aff, type, pos, n);
2630 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2631 enum isl_dim_type type, unsigned n)
2633 isl_size pos;
2635 pos = isl_pw_aff_dim(pwaff, type);
2636 if (pos < 0)
2637 return isl_pw_aff_free(pwaff);
2639 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2642 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2643 * to dimensions of "dst_type" at "dst_pos".
2645 * We only support moving input dimensions to parameters and vice versa.
2647 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2648 enum isl_dim_type dst_type, unsigned dst_pos,
2649 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2651 unsigned g_dst_pos;
2652 unsigned g_src_pos;
2653 isl_size src_off, dst_off;
2655 if (!aff)
2656 return NULL;
2657 if (n == 0 &&
2658 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2659 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2660 return aff;
2662 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2663 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2664 "cannot move output/set dimension",
2665 return isl_aff_free(aff));
2666 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2667 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2668 "cannot move divs", return isl_aff_free(aff));
2669 if (dst_type == isl_dim_in)
2670 dst_type = isl_dim_set;
2671 if (src_type == isl_dim_in)
2672 src_type = isl_dim_set;
2674 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2675 return isl_aff_free(aff);
2676 if (dst_type == src_type)
2677 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2678 "moving dims within the same type not supported",
2679 return isl_aff_free(aff));
2681 aff = isl_aff_cow(aff);
2682 src_off = isl_aff_domain_offset(aff, src_type);
2683 dst_off = isl_aff_domain_offset(aff, dst_type);
2684 if (src_off < 0 || dst_off < 0)
2685 return isl_aff_free(aff);
2687 g_src_pos = 1 + src_off + src_pos;
2688 g_dst_pos = 1 + dst_off + dst_pos;
2689 if (dst_type > src_type)
2690 g_dst_pos -= n;
2692 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2693 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2694 src_type, src_pos, n);
2695 if (!aff->v || !aff->ls)
2696 return isl_aff_free(aff);
2698 aff = sort_divs(aff);
2700 return aff;
2703 /* Return a zero isl_aff in the given space.
2705 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2706 * interface over all piecewise types.
2708 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2710 isl_local_space *ls;
2712 ls = isl_local_space_from_space(isl_space_domain(space));
2713 return isl_aff_zero_on_domain(ls);
2716 #define isl_aff_involves_nan isl_aff_is_nan
2718 #undef PW
2719 #define PW isl_pw_aff
2720 #undef BASE
2721 #define BASE aff
2722 #undef EL_IS_ZERO
2723 #define EL_IS_ZERO is_empty
2724 #undef ZERO
2725 #define ZERO empty
2726 #undef IS_ZERO
2727 #define IS_ZERO is_empty
2728 #undef FIELD
2729 #define FIELD aff
2730 #undef DEFAULT_IS_ZERO
2731 #define DEFAULT_IS_ZERO 0
2733 #define NO_OPT
2735 #include <isl_pw_templ.c>
2736 #include <isl_pw_bind_domain_templ.c>
2737 #include <isl_pw_eval.c>
2738 #include <isl_pw_hash.c>
2739 #include <isl_pw_neg_templ.c>
2740 #include <isl_pw_pullback_templ.c>
2741 #include <isl_pw_sub_templ.c>
2742 #include <isl_pw_union_opt.c>
2744 #undef BASE
2745 #define BASE pw_aff
2747 #include <isl_union_single.c>
2748 #include <isl_union_neg.c>
2750 static __isl_give isl_set *align_params_pw_pw_set_and(
2751 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2752 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2753 __isl_take isl_pw_aff *pwaff2))
2755 isl_bool equal_params;
2757 if (!pwaff1 || !pwaff2)
2758 goto error;
2759 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2760 if (equal_params < 0)
2761 goto error;
2762 if (equal_params)
2763 return fn(pwaff1, pwaff2);
2764 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2765 isl_pw_aff_check_named_params(pwaff2) < 0)
2766 goto error;
2767 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2768 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2769 return fn(pwaff1, pwaff2);
2770 error:
2771 isl_pw_aff_free(pwaff1);
2772 isl_pw_aff_free(pwaff2);
2773 return NULL;
2776 /* Align the parameters of the two isl_pw_aff arguments and
2777 * then apply a function "fn" on them that returns an isl_map.
2779 static __isl_give isl_map *align_params_pw_pw_map_and(
2780 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2781 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2782 __isl_take isl_pw_aff *pa2))
2784 isl_bool equal_params;
2786 if (!pa1 || !pa2)
2787 goto error;
2788 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2789 if (equal_params < 0)
2790 goto error;
2791 if (equal_params)
2792 return fn(pa1, pa2);
2793 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2794 isl_pw_aff_check_named_params(pa2) < 0)
2795 goto error;
2796 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2797 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2798 return fn(pa1, pa2);
2799 error:
2800 isl_pw_aff_free(pa1);
2801 isl_pw_aff_free(pa2);
2802 return NULL;
2805 /* Compute a piecewise quasi-affine expression with a domain that
2806 * is the union of those of pwaff1 and pwaff2 and such that on each
2807 * cell, the quasi-affine expression is the maximum of those of pwaff1
2808 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2809 * cell, then the associated expression is the defined one.
2811 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2812 __isl_take isl_pw_aff *pwaff2)
2814 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2817 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2818 __isl_take isl_pw_aff *pwaff2)
2820 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2821 &pw_aff_union_max);
2824 /* Compute a piecewise quasi-affine expression with a domain that
2825 * is the union of those of pwaff1 and pwaff2 and such that on each
2826 * cell, the quasi-affine expression is the minimum of those of pwaff1
2827 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2828 * cell, then the associated expression is the defined one.
2830 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2831 __isl_take isl_pw_aff *pwaff2)
2833 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2836 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2837 __isl_take isl_pw_aff *pwaff2)
2839 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2840 &pw_aff_union_min);
2843 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2844 __isl_take isl_pw_aff *pwaff2, int max)
2846 if (max)
2847 return isl_pw_aff_union_max(pwaff1, pwaff2);
2848 else
2849 return isl_pw_aff_union_min(pwaff1, pwaff2);
2852 /* Is the domain of "pa" a product?
2854 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2856 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2859 #undef TYPE
2860 #define TYPE isl_pw_aff
2861 #include <isl_domain_factor_templ.c>
2863 /* Return a set containing those elements in the domain
2864 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2865 * does not satisfy "fn" (if complement is 1).
2867 * The pieces with a NaN never belong to the result since
2868 * NaN does not satisfy any property.
2870 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2871 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2872 void *user),
2873 int complement, void *user)
2875 int i;
2876 isl_set *set;
2878 if (!pwaff)
2879 return NULL;
2881 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2883 for (i = 0; i < pwaff->n; ++i) {
2884 isl_basic_set *bset;
2885 isl_set *set_i, *locus;
2886 isl_bool rational;
2888 if (isl_aff_is_nan(pwaff->p[i].aff))
2889 continue;
2891 rational = isl_set_has_rational(pwaff->p[i].set);
2892 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2893 locus = isl_set_from_basic_set(bset);
2894 set_i = isl_set_copy(pwaff->p[i].set);
2895 if (complement)
2896 set_i = isl_set_subtract(set_i, locus);
2897 else
2898 set_i = isl_set_intersect(set_i, locus);
2899 set = isl_set_union_disjoint(set, set_i);
2902 isl_pw_aff_free(pwaff);
2904 return set;
2907 /* Return a set containing those elements in the domain
2908 * of "pa" where it is positive.
2910 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2912 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2915 /* Return a set containing those elements in the domain
2916 * of pwaff where it is non-negative.
2918 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2920 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2923 /* Return a set containing those elements in the domain
2924 * of pwaff where it is zero.
2926 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2928 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2931 /* Return a set containing those elements in the domain
2932 * of pwaff where it is not zero.
2934 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2936 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2939 /* Bind the affine function "aff" to the parameter "id",
2940 * returning the elements in the domain where the affine expression
2941 * is equal to the parameter.
2943 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2944 __isl_take isl_id *id)
2946 isl_space *space;
2947 isl_aff *aff_id;
2949 space = isl_aff_get_domain_space(aff);
2950 space = isl_space_add_param_id(space, isl_id_copy(id));
2952 aff = isl_aff_align_params(aff, isl_space_copy(space));
2953 aff_id = isl_aff_param_on_domain_space_id(space, id);
2955 return isl_aff_eq_basic_set(aff, aff_id);
2958 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2959 * "rational" should not be set.
2961 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2962 int rational, void *user)
2964 isl_id *id = user;
2966 if (!aff)
2967 return NULL;
2968 if (rational)
2969 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2970 "rational binding not supported", goto error);
2971 return isl_aff_bind_id(aff, isl_id_copy(id));
2972 error:
2973 isl_aff_free(aff);
2974 return NULL;
2977 /* Bind the piecewise affine function "pa" to the parameter "id",
2978 * returning the elements in the domain where the expression
2979 * is equal to the parameter.
2981 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2982 __isl_take isl_id *id)
2984 isl_set *bound;
2986 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2987 isl_id_free(id);
2989 return bound;
2992 /* Return a set containing those elements in the shared domain
2993 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2995 * We compute the difference on the shared domain and then construct
2996 * the set of values where this difference is non-negative.
2997 * If strict is set, we first subtract 1 from the difference.
2998 * If equal is set, we only return the elements where pwaff1 and pwaff2
2999 * are equal.
3001 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3002 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3004 isl_set *set1, *set2;
3006 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3007 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3008 set1 = isl_set_intersect(set1, set2);
3009 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3010 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3011 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3013 if (strict) {
3014 isl_space *space = isl_set_get_space(set1);
3015 isl_aff *aff;
3016 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3017 aff = isl_aff_add_constant_si(aff, -1);
3018 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3019 } else
3020 isl_set_free(set1);
3022 if (equal)
3023 return isl_pw_aff_zero_set(pwaff1);
3024 return isl_pw_aff_nonneg_set(pwaff1);
3027 /* Return a set containing those elements in the shared domain
3028 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3030 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3031 __isl_take isl_pw_aff *pwaff2)
3033 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3036 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3037 __isl_take isl_pw_aff *pwaff2)
3039 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
3042 /* Return a set containing those elements in the shared domain
3043 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3045 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3046 __isl_take isl_pw_aff *pwaff2)
3048 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3051 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3052 __isl_take isl_pw_aff *pwaff2)
3054 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
3057 /* Return a set containing those elements in the shared domain
3058 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3060 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3061 __isl_take isl_pw_aff *pwaff2)
3063 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3066 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3067 __isl_take isl_pw_aff *pwaff2)
3069 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
3072 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3073 __isl_take isl_pw_aff *pwaff2)
3075 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3078 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3079 __isl_take isl_pw_aff *pwaff2)
3081 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3084 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3085 * where the function values are ordered in the same way as "order",
3086 * which returns a set in the shared domain of its two arguments.
3087 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3089 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3090 * We first pull back the two functions such that they are defined on
3091 * the domain [A -> B]. Then we apply "order", resulting in a set
3092 * in the space [A -> B]. Finally, we unwrap this set to obtain
3093 * a map in the space A -> B.
3095 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3096 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3097 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3098 __isl_take isl_pw_aff *pa2))
3100 isl_space *space1, *space2;
3101 isl_multi_aff *ma;
3102 isl_set *set;
3104 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3105 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3106 space1 = isl_space_map_from_domain_and_range(space1, space2);
3107 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3108 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3109 ma = isl_multi_aff_range_map(space1);
3110 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3111 set = order(pa1, pa2);
3113 return isl_set_unwrap(set);
3116 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3117 * where the function values are equal.
3118 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3120 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3121 __isl_take isl_pw_aff *pa2)
3123 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3126 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3127 * where the function values are equal.
3129 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3130 __isl_take isl_pw_aff *pa2)
3132 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3135 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3136 * where the function value of "pa1" is less than the function value of "pa2".
3137 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3139 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3140 __isl_take isl_pw_aff *pa2)
3142 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3145 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3146 * where the function value of "pa1" is less than the function value of "pa2".
3148 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3149 __isl_take isl_pw_aff *pa2)
3151 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3154 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3155 * where the function value of "pa1" is greater than the function value
3156 * of "pa2".
3157 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3159 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3160 __isl_take isl_pw_aff *pa2)
3162 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3165 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3166 * where the function value of "pa1" is greater than the function value
3167 * of "pa2".
3169 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3170 __isl_take isl_pw_aff *pa2)
3172 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3175 /* Return a set containing those elements in the shared domain
3176 * of the elements of list1 and list2 where each element in list1
3177 * has the relation specified by "fn" with each element in list2.
3179 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3180 __isl_take isl_pw_aff_list *list2,
3181 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3182 __isl_take isl_pw_aff *pwaff2))
3184 int i, j;
3185 isl_ctx *ctx;
3186 isl_set *set;
3188 if (!list1 || !list2)
3189 goto error;
3191 ctx = isl_pw_aff_list_get_ctx(list1);
3192 if (list1->n < 1 || list2->n < 1)
3193 isl_die(ctx, isl_error_invalid,
3194 "list should contain at least one element", goto error);
3196 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3197 for (i = 0; i < list1->n; ++i)
3198 for (j = 0; j < list2->n; ++j) {
3199 isl_set *set_ij;
3201 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3202 isl_pw_aff_copy(list2->p[j]));
3203 set = isl_set_intersect(set, set_ij);
3206 isl_pw_aff_list_free(list1);
3207 isl_pw_aff_list_free(list2);
3208 return set;
3209 error:
3210 isl_pw_aff_list_free(list1);
3211 isl_pw_aff_list_free(list2);
3212 return NULL;
3215 /* Return a set containing those elements in the shared domain
3216 * of the elements of list1 and list2 where each element in list1
3217 * is equal to each element in list2.
3219 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3220 __isl_take isl_pw_aff_list *list2)
3222 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3225 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3226 __isl_take isl_pw_aff_list *list2)
3228 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3231 /* Return a set containing those elements in the shared domain
3232 * of the elements of list1 and list2 where each element in list1
3233 * is less than or equal to each element in list2.
3235 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3236 __isl_take isl_pw_aff_list *list2)
3238 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3241 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3242 __isl_take isl_pw_aff_list *list2)
3244 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3247 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3248 __isl_take isl_pw_aff_list *list2)
3250 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3253 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3254 __isl_take isl_pw_aff_list *list2)
3256 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3260 /* Return a set containing those elements in the shared domain
3261 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3263 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3264 __isl_take isl_pw_aff *pwaff2)
3266 isl_set *set_lt, *set_gt;
3268 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3269 isl_pw_aff_copy(pwaff2));
3270 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3271 return isl_set_union_disjoint(set_lt, set_gt);
3274 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3275 __isl_take isl_pw_aff *pwaff2)
3277 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3280 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3281 isl_int v)
3283 int i;
3285 if (isl_int_is_one(v))
3286 return pwaff;
3287 if (!isl_int_is_pos(v))
3288 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3289 "factor needs to be positive",
3290 return isl_pw_aff_free(pwaff));
3291 pwaff = isl_pw_aff_cow(pwaff);
3292 if (!pwaff)
3293 return NULL;
3294 if (pwaff->n == 0)
3295 return pwaff;
3297 for (i = 0; i < pwaff->n; ++i) {
3298 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3299 if (!pwaff->p[i].aff)
3300 return isl_pw_aff_free(pwaff);
3303 return pwaff;
3306 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3308 int i;
3310 pwaff = isl_pw_aff_cow(pwaff);
3311 if (!pwaff)
3312 return NULL;
3313 if (pwaff->n == 0)
3314 return pwaff;
3316 for (i = 0; i < pwaff->n; ++i) {
3317 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3318 if (!pwaff->p[i].aff)
3319 return isl_pw_aff_free(pwaff);
3322 return pwaff;
3325 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3327 int i;
3329 pwaff = isl_pw_aff_cow(pwaff);
3330 if (!pwaff)
3331 return NULL;
3332 if (pwaff->n == 0)
3333 return pwaff;
3335 for (i = 0; i < pwaff->n; ++i) {
3336 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3337 if (!pwaff->p[i].aff)
3338 return isl_pw_aff_free(pwaff);
3341 return pwaff;
3344 /* Assuming that "cond1" and "cond2" are disjoint,
3345 * return an affine expression that is equal to pwaff1 on cond1
3346 * and to pwaff2 on cond2.
3348 static __isl_give isl_pw_aff *isl_pw_aff_select(
3349 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3350 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3352 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3353 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3355 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3358 /* Return an affine expression that is equal to pwaff_true for elements
3359 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3360 * is zero.
3361 * That is, return cond ? pwaff_true : pwaff_false;
3363 * If "cond" involves and NaN, then we conservatively return a NaN
3364 * on its entire domain. In principle, we could consider the pieces
3365 * where it is NaN separately from those where it is not.
3367 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3368 * then only use the domain of "cond" to restrict the domain.
3370 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3371 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3373 isl_set *cond_true, *cond_false;
3374 isl_bool equal;
3376 if (!cond)
3377 goto error;
3378 if (isl_pw_aff_involves_nan(cond)) {
3379 isl_space *space = isl_pw_aff_get_domain_space(cond);
3380 isl_local_space *ls = isl_local_space_from_space(space);
3381 isl_pw_aff_free(cond);
3382 isl_pw_aff_free(pwaff_true);
3383 isl_pw_aff_free(pwaff_false);
3384 return isl_pw_aff_nan_on_domain(ls);
3387 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3388 isl_pw_aff_get_space(pwaff_false));
3389 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3390 isl_pw_aff_get_space(pwaff_true));
3391 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3392 if (equal < 0)
3393 goto error;
3394 if (equal) {
3395 isl_set *dom;
3397 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3398 isl_pw_aff_free(pwaff_false);
3399 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3402 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3403 cond_false = isl_pw_aff_zero_set(cond);
3404 return isl_pw_aff_select(cond_true, pwaff_true,
3405 cond_false, pwaff_false);
3406 error:
3407 isl_pw_aff_free(cond);
3408 isl_pw_aff_free(pwaff_true);
3409 isl_pw_aff_free(pwaff_false);
3410 return NULL;
3413 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3415 int pos;
3417 if (!aff)
3418 return isl_bool_error;
3420 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3421 return isl_bool_ok(pos == -1);
3424 /* Check whether pwaff is a piecewise constant.
3426 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3428 int i;
3430 if (!pwaff)
3431 return isl_bool_error;
3433 for (i = 0; i < pwaff->n; ++i) {
3434 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3435 if (is_cst < 0 || !is_cst)
3436 return is_cst;
3439 return isl_bool_true;
3442 /* Are all elements of "mpa" piecewise constants?
3444 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3446 int i;
3448 if (!mpa)
3449 return isl_bool_error;
3451 for (i = 0; i < mpa->n; ++i) {
3452 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3453 if (is_cst < 0 || !is_cst)
3454 return is_cst;
3457 return isl_bool_true;
3460 /* Return the product of "aff1" and "aff2".
3462 * If either of the two is NaN, then the result is NaN.
3464 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3466 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3467 __isl_take isl_aff *aff2)
3469 if (!aff1 || !aff2)
3470 goto error;
3472 if (isl_aff_is_nan(aff1)) {
3473 isl_aff_free(aff2);
3474 return aff1;
3476 if (isl_aff_is_nan(aff2)) {
3477 isl_aff_free(aff1);
3478 return aff2;
3481 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3482 return isl_aff_mul(aff2, aff1);
3484 if (!isl_aff_is_cst(aff2))
3485 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3486 "at least one affine expression should be constant",
3487 goto error);
3489 aff1 = isl_aff_cow(aff1);
3490 if (!aff1 || !aff2)
3491 goto error;
3493 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3494 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3496 isl_aff_free(aff2);
3497 return aff1;
3498 error:
3499 isl_aff_free(aff1);
3500 isl_aff_free(aff2);
3501 return NULL;
3504 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3506 * If either of the two is NaN, then the result is NaN.
3508 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3509 __isl_take isl_aff *aff2)
3511 int is_cst;
3512 int neg;
3514 if (!aff1 || !aff2)
3515 goto error;
3517 if (isl_aff_is_nan(aff1)) {
3518 isl_aff_free(aff2);
3519 return aff1;
3521 if (isl_aff_is_nan(aff2)) {
3522 isl_aff_free(aff1);
3523 return aff2;
3526 is_cst = isl_aff_is_cst(aff2);
3527 if (is_cst < 0)
3528 goto error;
3529 if (!is_cst)
3530 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3531 "second argument should be a constant", goto error);
3533 if (!aff2)
3534 goto error;
3536 neg = isl_int_is_neg(aff2->v->el[1]);
3537 if (neg) {
3538 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3539 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3542 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3543 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3545 if (neg) {
3546 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3547 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3550 isl_aff_free(aff2);
3551 return aff1;
3552 error:
3553 isl_aff_free(aff1);
3554 isl_aff_free(aff2);
3555 return NULL;
3558 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3559 __isl_take isl_pw_aff *pwaff2)
3561 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3564 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3565 __isl_take isl_pw_aff *pwaff2)
3567 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3570 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3571 __isl_take isl_pw_aff *pwaff2)
3573 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3576 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3577 __isl_take isl_pw_aff *pwaff2)
3579 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3582 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3583 __isl_take isl_pw_aff *pwaff2)
3585 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3588 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3589 __isl_take isl_pw_aff *pa2)
3591 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3594 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3596 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3597 __isl_take isl_pw_aff *pa2)
3599 int is_cst;
3601 is_cst = isl_pw_aff_is_cst(pa2);
3602 if (is_cst < 0)
3603 goto error;
3604 if (!is_cst)
3605 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3606 "second argument should be a piecewise constant",
3607 goto error);
3608 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3609 error:
3610 isl_pw_aff_free(pa1);
3611 isl_pw_aff_free(pa2);
3612 return NULL;
3615 /* Compute the quotient of the integer division of "pa1" by "pa2"
3616 * with rounding towards zero.
3617 * "pa2" is assumed to be a piecewise constant.
3619 * In particular, return
3621 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3624 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3625 __isl_take isl_pw_aff *pa2)
3627 int is_cst;
3628 isl_set *cond;
3629 isl_pw_aff *f, *c;
3631 is_cst = isl_pw_aff_is_cst(pa2);
3632 if (is_cst < 0)
3633 goto error;
3634 if (!is_cst)
3635 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3636 "second argument should be a piecewise constant",
3637 goto error);
3639 pa1 = isl_pw_aff_div(pa1, pa2);
3641 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3642 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3643 c = isl_pw_aff_ceil(pa1);
3644 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3645 error:
3646 isl_pw_aff_free(pa1);
3647 isl_pw_aff_free(pa2);
3648 return NULL;
3651 /* Compute the remainder of the integer division of "pa1" by "pa2"
3652 * with rounding towards zero.
3653 * "pa2" is assumed to be a piecewise constant.
3655 * In particular, return
3657 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3660 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3661 __isl_take isl_pw_aff *pa2)
3663 int is_cst;
3664 isl_pw_aff *res;
3666 is_cst = isl_pw_aff_is_cst(pa2);
3667 if (is_cst < 0)
3668 goto error;
3669 if (!is_cst)
3670 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3671 "second argument should be a piecewise constant",
3672 goto error);
3673 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3674 res = isl_pw_aff_mul(pa2, res);
3675 res = isl_pw_aff_sub(pa1, res);
3676 return res;
3677 error:
3678 isl_pw_aff_free(pa1);
3679 isl_pw_aff_free(pa2);
3680 return NULL;
3683 /* Does either of "pa1" or "pa2" involve any NaN2?
3685 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3686 __isl_keep isl_pw_aff *pa2)
3688 isl_bool has_nan;
3690 has_nan = isl_pw_aff_involves_nan(pa1);
3691 if (has_nan < 0 || has_nan)
3692 return has_nan;
3693 return isl_pw_aff_involves_nan(pa2);
3696 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3697 * by a NaN on their shared domain.
3699 * In principle, the result could be refined to only being NaN
3700 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3702 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3703 __isl_take isl_pw_aff *pa2)
3705 isl_local_space *ls;
3706 isl_set *dom;
3707 isl_pw_aff *pa;
3709 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3710 ls = isl_local_space_from_space(isl_set_get_space(dom));
3711 pa = isl_pw_aff_nan_on_domain(ls);
3712 pa = isl_pw_aff_intersect_domain(pa, dom);
3714 return pa;
3717 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3718 __isl_take isl_pw_aff *pwaff2)
3720 isl_set *le;
3721 isl_set *dom;
3723 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3724 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3725 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3726 isl_pw_aff_copy(pwaff2));
3727 dom = isl_set_subtract(dom, isl_set_copy(le));
3728 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3731 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3732 __isl_take isl_pw_aff *pwaff2)
3734 isl_set *ge;
3735 isl_set *dom;
3737 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3738 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3739 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3740 isl_pw_aff_copy(pwaff2));
3741 dom = isl_set_subtract(dom, isl_set_copy(ge));
3742 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3745 /* Return an expression for the minimum (if "max" is not set) or
3746 * the maximum (if "max" is set) of "pa1" and "pa2".
3747 * If either expression involves any NaN, then return a NaN
3748 * on the shared domain as result.
3750 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3751 __isl_take isl_pw_aff *pa2, int max)
3753 isl_bool has_nan;
3755 has_nan = either_involves_nan(pa1, pa2);
3756 if (has_nan < 0)
3757 pa1 = isl_pw_aff_free(pa1);
3758 else if (has_nan)
3759 return replace_by_nan(pa1, pa2);
3761 if (max)
3762 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3763 else
3764 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3767 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3769 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3770 __isl_take isl_pw_aff *pwaff2)
3772 return pw_aff_min_max(pwaff1, pwaff2, 0);
3775 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3777 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3778 __isl_take isl_pw_aff *pwaff2)
3780 return pw_aff_min_max(pwaff1, pwaff2, 1);
3783 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3784 __isl_take isl_pw_aff_list *list,
3785 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3786 __isl_take isl_pw_aff *pwaff2))
3788 int i;
3789 isl_ctx *ctx;
3790 isl_pw_aff *res;
3792 if (!list)
3793 return NULL;
3795 ctx = isl_pw_aff_list_get_ctx(list);
3796 if (list->n < 1)
3797 isl_die(ctx, isl_error_invalid,
3798 "list should contain at least one element", goto error);
3800 res = isl_pw_aff_copy(list->p[0]);
3801 for (i = 1; i < list->n; ++i)
3802 res = fn(res, isl_pw_aff_copy(list->p[i]));
3804 isl_pw_aff_list_free(list);
3805 return res;
3806 error:
3807 isl_pw_aff_list_free(list);
3808 return NULL;
3811 /* Return an isl_pw_aff that maps each element in the intersection of the
3812 * domains of the elements of list to the minimal corresponding affine
3813 * expression.
3815 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3817 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3820 /* Return an isl_pw_aff that maps each element in the intersection of the
3821 * domains of the elements of list to the maximal corresponding affine
3822 * expression.
3824 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3826 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3829 /* Mark the domains of "pwaff" as rational.
3831 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3833 int i;
3835 pwaff = isl_pw_aff_cow(pwaff);
3836 if (!pwaff)
3837 return NULL;
3838 if (pwaff->n == 0)
3839 return pwaff;
3841 for (i = 0; i < pwaff->n; ++i) {
3842 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3843 if (!pwaff->p[i].set)
3844 return isl_pw_aff_free(pwaff);
3847 return pwaff;
3850 /* Mark the domains of the elements of "list" as rational.
3852 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3853 __isl_take isl_pw_aff_list *list)
3855 int i, n;
3857 if (!list)
3858 return NULL;
3859 if (list->n == 0)
3860 return list;
3862 n = list->n;
3863 for (i = 0; i < n; ++i) {
3864 isl_pw_aff *pa;
3866 pa = isl_pw_aff_list_get_pw_aff(list, i);
3867 pa = isl_pw_aff_set_rational(pa);
3868 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3871 return list;
3874 /* Do the parameters of "aff" match those of "space"?
3876 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3877 __isl_keep isl_space *space)
3879 isl_space *aff_space;
3880 isl_bool match;
3882 if (!aff || !space)
3883 return isl_bool_error;
3885 aff_space = isl_aff_get_domain_space(aff);
3887 match = isl_space_has_equal_params(space, aff_space);
3889 isl_space_free(aff_space);
3890 return match;
3893 /* Check that the domain space of "aff" matches "space".
3895 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3896 __isl_keep isl_space *space)
3898 isl_space *aff_space;
3899 isl_bool match;
3901 if (!aff || !space)
3902 return isl_stat_error;
3904 aff_space = isl_aff_get_domain_space(aff);
3906 match = isl_space_has_equal_params(space, aff_space);
3907 if (match < 0)
3908 goto error;
3909 if (!match)
3910 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3911 "parameters don't match", goto error);
3912 match = isl_space_tuple_is_equal(space, isl_dim_in,
3913 aff_space, isl_dim_set);
3914 if (match < 0)
3915 goto error;
3916 if (!match)
3917 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3918 "domains don't match", goto error);
3919 isl_space_free(aff_space);
3920 return isl_stat_ok;
3921 error:
3922 isl_space_free(aff_space);
3923 return isl_stat_error;
3926 /* Return the shared (universe) domain of the elements of "ma".
3928 * Since an isl_multi_aff (and an isl_aff) is always total,
3929 * the domain is always the universe set in its domain space.
3930 * This is a helper function for use in the generic isl_multi_*_bind.
3932 static __isl_give isl_basic_set *isl_multi_aff_domain(
3933 __isl_take isl_multi_aff *ma)
3935 isl_space *space;
3937 space = isl_multi_aff_get_space(ma);
3938 isl_multi_aff_free(ma);
3940 return isl_basic_set_universe(isl_space_domain(space));
3943 #undef BASE
3944 #define BASE aff
3946 #include <isl_multi_no_explicit_domain.c>
3947 #include <isl_multi_templ.c>
3948 #include <isl_multi_apply_set.c>
3949 #include <isl_multi_arith_templ.c>
3950 #include <isl_multi_bind_domain_templ.c>
3951 #include <isl_multi_cmp.c>
3952 #include <isl_multi_dim_id_templ.c>
3953 #include <isl_multi_dims.c>
3954 #include <isl_multi_floor.c>
3955 #include <isl_multi_from_base_templ.c>
3956 #include <isl_multi_identity_templ.c>
3957 #include <isl_multi_locals_templ.c>
3958 #include <isl_multi_move_dims_templ.c>
3959 #include <isl_multi_nan_templ.c>
3960 #include <isl_multi_product_templ.c>
3961 #include <isl_multi_splice_templ.c>
3962 #include <isl_multi_tuple_id_templ.c>
3963 #include <isl_multi_zero_templ.c>
3965 #undef DOMBASE
3966 #define DOMBASE set
3967 #include <isl_multi_gist.c>
3969 #undef DOMBASE
3970 #define DOMBASE basic_set
3971 #include <isl_multi_bind_templ.c>
3973 /* Construct an isl_multi_aff living in "space" that corresponds
3974 * to the affine transformation matrix "mat".
3976 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3977 __isl_take isl_space *space, __isl_take isl_mat *mat)
3979 isl_ctx *ctx;
3980 isl_local_space *ls = NULL;
3981 isl_multi_aff *ma = NULL;
3982 isl_size n_row, n_col, n_out, total;
3983 int i;
3985 if (!space || !mat)
3986 goto error;
3988 ctx = isl_mat_get_ctx(mat);
3990 n_row = isl_mat_rows(mat);
3991 n_col = isl_mat_cols(mat);
3992 n_out = isl_space_dim(space, isl_dim_out);
3993 total = isl_space_dim(space, isl_dim_all);
3994 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3995 goto error;
3996 if (n_row < 1)
3997 isl_die(ctx, isl_error_invalid,
3998 "insufficient number of rows", goto error);
3999 if (n_col < 1)
4000 isl_die(ctx, isl_error_invalid,
4001 "insufficient number of columns", goto error);
4002 if (1 + n_out != n_row || 2 + total != n_row + n_col)
4003 isl_die(ctx, isl_error_invalid,
4004 "dimension mismatch", goto error);
4006 ma = isl_multi_aff_zero(isl_space_copy(space));
4007 ls = isl_local_space_from_space(isl_space_domain(space));
4009 for (i = 0; i < n_row - 1; ++i) {
4010 isl_vec *v;
4011 isl_aff *aff;
4013 v = isl_vec_alloc(ctx, 1 + n_col);
4014 if (!v)
4015 goto error;
4016 isl_int_set(v->el[0], mat->row[0][0]);
4017 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
4018 v = isl_vec_normalize(v);
4019 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
4020 ma = isl_multi_aff_set_aff(ma, i, aff);
4023 isl_local_space_free(ls);
4024 isl_mat_free(mat);
4025 return ma;
4026 error:
4027 isl_local_space_free(ls);
4028 isl_mat_free(mat);
4029 isl_multi_aff_free(ma);
4030 return NULL;
4033 /* Remove any internal structure of the domain of "ma".
4034 * If there is any such internal structure in the input,
4035 * then the name of the corresponding space is also removed.
4037 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4038 __isl_take isl_multi_aff *ma)
4040 isl_space *space;
4042 if (!ma)
4043 return NULL;
4045 if (!ma->space->nested[0])
4046 return ma;
4048 space = isl_multi_aff_get_space(ma);
4049 space = isl_space_flatten_domain(space);
4050 ma = isl_multi_aff_reset_space(ma, space);
4052 return ma;
4055 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4056 * of the space to its domain.
4058 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4060 int i;
4061 isl_size n_in;
4062 isl_local_space *ls;
4063 isl_multi_aff *ma;
4065 if (!space)
4066 return NULL;
4067 if (!isl_space_is_map(space))
4068 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4069 "not a map space", goto error);
4071 n_in = isl_space_dim(space, isl_dim_in);
4072 if (n_in < 0)
4073 goto error;
4074 space = isl_space_domain_map(space);
4076 ma = isl_multi_aff_alloc(isl_space_copy(space));
4077 if (n_in == 0) {
4078 isl_space_free(space);
4079 return ma;
4082 space = isl_space_domain(space);
4083 ls = isl_local_space_from_space(space);
4084 for (i = 0; i < n_in; ++i) {
4085 isl_aff *aff;
4087 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4088 isl_dim_set, i);
4089 ma = isl_multi_aff_set_aff(ma, i, aff);
4091 isl_local_space_free(ls);
4092 return ma;
4093 error:
4094 isl_space_free(space);
4095 return NULL;
4098 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4099 * of the space to its range.
4101 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4103 int i;
4104 isl_size n_in, n_out;
4105 isl_local_space *ls;
4106 isl_multi_aff *ma;
4108 if (!space)
4109 return NULL;
4110 if (!isl_space_is_map(space))
4111 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4112 "not a map space", goto error);
4114 n_in = isl_space_dim(space, isl_dim_in);
4115 n_out = isl_space_dim(space, isl_dim_out);
4116 if (n_in < 0 || n_out < 0)
4117 goto error;
4118 space = isl_space_range_map(space);
4120 ma = isl_multi_aff_alloc(isl_space_copy(space));
4121 if (n_out == 0) {
4122 isl_space_free(space);
4123 return ma;
4126 space = isl_space_domain(space);
4127 ls = isl_local_space_from_space(space);
4128 for (i = 0; i < n_out; ++i) {
4129 isl_aff *aff;
4131 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4132 isl_dim_set, n_in + i);
4133 ma = isl_multi_aff_set_aff(ma, i, aff);
4135 isl_local_space_free(ls);
4136 return ma;
4137 error:
4138 isl_space_free(space);
4139 return NULL;
4142 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4143 * of the space to its range.
4145 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4146 __isl_take isl_space *space)
4148 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4151 /* Given the space of a set and a range of set dimensions,
4152 * construct an isl_multi_aff that projects out those dimensions.
4154 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4155 __isl_take isl_space *space, enum isl_dim_type type,
4156 unsigned first, unsigned n)
4158 int i;
4159 isl_size dim;
4160 isl_local_space *ls;
4161 isl_multi_aff *ma;
4163 if (!space)
4164 return NULL;
4165 if (!isl_space_is_set(space))
4166 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4167 "expecting set space", goto error);
4168 if (type != isl_dim_set)
4169 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4170 "only set dimensions can be projected out", goto error);
4171 if (isl_space_check_range(space, type, first, n) < 0)
4172 goto error;
4174 dim = isl_space_dim(space, isl_dim_set);
4175 if (dim < 0)
4176 goto error;
4178 space = isl_space_from_domain(space);
4179 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4181 if (dim == n)
4182 return isl_multi_aff_alloc(space);
4184 ma = isl_multi_aff_alloc(isl_space_copy(space));
4185 space = isl_space_domain(space);
4186 ls = isl_local_space_from_space(space);
4188 for (i = 0; i < first; ++i) {
4189 isl_aff *aff;
4191 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4192 isl_dim_set, i);
4193 ma = isl_multi_aff_set_aff(ma, i, aff);
4196 for (i = 0; i < dim - (first + n); ++i) {
4197 isl_aff *aff;
4199 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4200 isl_dim_set, first + n + i);
4201 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4204 isl_local_space_free(ls);
4205 return ma;
4206 error:
4207 isl_space_free(space);
4208 return NULL;
4211 /* Given the space of a set and a range of set dimensions,
4212 * construct an isl_pw_multi_aff that projects out those dimensions.
4214 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4215 __isl_take isl_space *space, enum isl_dim_type type,
4216 unsigned first, unsigned n)
4218 isl_multi_aff *ma;
4220 ma = isl_multi_aff_project_out_map(space, type, first, n);
4221 return isl_pw_multi_aff_from_multi_aff(ma);
4224 /* Create a piecewise multi-affine expression in the given space that maps each
4225 * input dimension to the corresponding output dimension.
4227 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4228 __isl_take isl_space *space)
4230 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4233 /* Exploit the equalities in "eq" to simplify the affine expressions.
4235 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4236 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4238 int i;
4240 maff = isl_multi_aff_cow(maff);
4241 if (!maff || !eq)
4242 goto error;
4244 for (i = 0; i < maff->n; ++i) {
4245 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4246 isl_basic_set_copy(eq));
4247 if (!maff->u.p[i])
4248 goto error;
4251 isl_basic_set_free(eq);
4252 return maff;
4253 error:
4254 isl_basic_set_free(eq);
4255 isl_multi_aff_free(maff);
4256 return NULL;
4259 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4260 isl_int f)
4262 int i;
4264 maff = isl_multi_aff_cow(maff);
4265 if (!maff)
4266 return NULL;
4268 for (i = 0; i < maff->n; ++i) {
4269 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4270 if (!maff->u.p[i])
4271 return isl_multi_aff_free(maff);
4274 return maff;
4277 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4278 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4280 maff1 = isl_multi_aff_add(maff1, maff2);
4281 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4282 return maff1;
4285 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4287 if (!maff)
4288 return isl_bool_error;
4290 return isl_bool_false;
4293 /* Return the set of domain elements where "ma1" is lexicographically
4294 * smaller than or equal to "ma2".
4296 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4297 __isl_take isl_multi_aff *ma2)
4299 return isl_multi_aff_lex_ge_set(ma2, ma1);
4302 /* Return the set of domain elements where "ma1" is lexicographically
4303 * smaller than "ma2".
4305 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4306 __isl_take isl_multi_aff *ma2)
4308 return isl_multi_aff_lex_gt_set(ma2, ma1);
4311 /* Return the set of domain elements where "ma1" and "ma2"
4312 * satisfy "order".
4314 static __isl_give isl_set *isl_multi_aff_order_set(
4315 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4316 __isl_give isl_map *order(__isl_take isl_space *set_space))
4318 isl_space *space;
4319 isl_map *map1, *map2;
4320 isl_map *map, *ge;
4322 map1 = isl_map_from_multi_aff_internal(ma1);
4323 map2 = isl_map_from_multi_aff_internal(ma2);
4324 map = isl_map_range_product(map1, map2);
4325 space = isl_space_range(isl_map_get_space(map));
4326 space = isl_space_domain(isl_space_unwrap(space));
4327 ge = order(space);
4328 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4330 return isl_map_domain(map);
4333 /* Return the set of domain elements where "ma1" is lexicographically
4334 * greater than or equal to "ma2".
4336 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4337 __isl_take isl_multi_aff *ma2)
4339 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4342 /* Return the set of domain elements where "ma1" is lexicographically
4343 * greater than "ma2".
4345 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4346 __isl_take isl_multi_aff *ma2)
4348 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4351 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4353 #undef PW
4354 #define PW isl_pw_multi_aff
4355 #undef BASE
4356 #define BASE multi_aff
4357 #undef EL_IS_ZERO
4358 #define EL_IS_ZERO is_empty
4359 #undef ZERO
4360 #define ZERO empty
4361 #undef IS_ZERO
4362 #define IS_ZERO is_empty
4363 #undef FIELD
4364 #define FIELD maff
4365 #undef DEFAULT_IS_ZERO
4366 #define DEFAULT_IS_ZERO 0
4368 #define NO_OPT
4369 #define NO_INSERT_DIMS
4371 #include <isl_pw_templ.c>
4372 #include <isl_pw_bind_domain_templ.c>
4373 #include <isl_pw_neg_templ.c>
4374 #include <isl_pw_pullback_templ.c>
4375 #include <isl_pw_union_opt.c>
4377 #undef BASE
4378 #define BASE pw_multi_aff
4380 #include <isl_union_multi.c>
4381 #include <isl_union_neg.c>
4383 /* Generic function for extracting a factor from a product "pma".
4384 * "check_space" checks that the space is that of the right kind of product.
4385 * "space_factor" extracts the factor from the space.
4386 * "multi_aff_factor" extracts the factor from the constituent functions.
4388 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4389 __isl_take isl_pw_multi_aff *pma,
4390 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4391 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4392 __isl_give isl_multi_aff *(*multi_aff_factor)(
4393 __isl_take isl_multi_aff *ma))
4395 int i;
4396 isl_space *space;
4398 if (check_space(pma) < 0)
4399 return isl_pw_multi_aff_free(pma);
4401 space = isl_pw_multi_aff_take_space(pma);
4402 space = space_factor(space);
4404 for (i = 0; pma && i < pma->n; ++i) {
4405 isl_multi_aff *ma;
4407 ma = isl_pw_multi_aff_take_base_at(pma, i);
4408 ma = multi_aff_factor(ma);
4409 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4412 pma = isl_pw_multi_aff_restore_space(pma, space);
4414 return pma;
4417 /* Is the range of "pma" a wrapped relation?
4419 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4420 __isl_keep isl_pw_multi_aff *pma)
4422 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4425 /* Check that the range of "pma" is a product.
4427 static isl_stat pw_multi_aff_check_range_product(
4428 __isl_keep isl_pw_multi_aff *pma)
4430 isl_bool wraps;
4432 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4433 if (wraps < 0)
4434 return isl_stat_error;
4435 if (!wraps)
4436 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4437 "range is not a product", return isl_stat_error);
4438 return isl_stat_ok;
4441 /* Given a function A -> [B -> C], extract the function A -> B.
4443 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4444 __isl_take isl_pw_multi_aff *pma)
4446 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4447 &isl_space_range_factor_domain,
4448 &isl_multi_aff_range_factor_domain);
4451 /* Given a function A -> [B -> C], extract the function A -> C.
4453 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4454 __isl_take isl_pw_multi_aff *pma)
4456 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4457 &isl_space_range_factor_range,
4458 &isl_multi_aff_range_factor_range);
4461 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4462 __isl_take isl_pw_multi_aff *pma1,
4463 __isl_take isl_pw_multi_aff *pma2)
4465 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4466 &isl_multi_aff_lex_ge_set);
4469 /* Given two piecewise multi affine expressions, return a piecewise
4470 * multi-affine expression defined on the union of the definition domains
4471 * of the inputs that is equal to the lexicographic maximum of the two
4472 * inputs on each cell. If only one of the two inputs is defined on
4473 * a given cell, then it is considered to be the maximum.
4475 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4476 __isl_take isl_pw_multi_aff *pma1,
4477 __isl_take isl_pw_multi_aff *pma2)
4479 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4480 &pw_multi_aff_union_lexmax);
4483 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4484 __isl_take isl_pw_multi_aff *pma1,
4485 __isl_take isl_pw_multi_aff *pma2)
4487 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4488 &isl_multi_aff_lex_le_set);
4491 /* Given two piecewise multi affine expressions, return a piecewise
4492 * multi-affine expression defined on the union of the definition domains
4493 * of the inputs that is equal to the lexicographic minimum of the two
4494 * inputs on each cell. If only one of the two inputs is defined on
4495 * a given cell, then it is considered to be the minimum.
4497 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4498 __isl_take isl_pw_multi_aff *pma1,
4499 __isl_take isl_pw_multi_aff *pma2)
4501 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4502 &pw_multi_aff_union_lexmin);
4505 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4506 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4508 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4509 &isl_multi_aff_add);
4512 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4513 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4515 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4516 &pw_multi_aff_add);
4519 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4520 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4522 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4523 &isl_multi_aff_sub);
4526 /* Subtract "pma2" from "pma1" and return the result.
4528 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4529 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4531 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4532 &pw_multi_aff_sub);
4535 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4536 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4538 return isl_pw_multi_aff_union_add_(pma1, pma2);
4541 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4542 * with the actual sum on the shared domain and
4543 * the defined expression on the symmetric difference of the domains.
4545 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4546 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4548 return isl_union_pw_aff_union_add_(upa1, upa2);
4551 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4552 * with the actual sum on the shared domain and
4553 * the defined expression on the symmetric difference of the domains.
4555 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4556 __isl_take isl_union_pw_multi_aff *upma1,
4557 __isl_take isl_union_pw_multi_aff *upma2)
4559 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4562 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4563 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4565 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4566 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4568 int i, j, n;
4569 isl_space *space;
4570 isl_pw_multi_aff *res;
4572 if (!pma1 || !pma2)
4573 goto error;
4575 n = pma1->n * pma2->n;
4576 space = isl_space_product(isl_space_copy(pma1->dim),
4577 isl_space_copy(pma2->dim));
4578 res = isl_pw_multi_aff_alloc_size(space, n);
4580 for (i = 0; i < pma1->n; ++i) {
4581 for (j = 0; j < pma2->n; ++j) {
4582 isl_set *domain;
4583 isl_multi_aff *ma;
4585 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4586 isl_set_copy(pma2->p[j].set));
4587 ma = isl_multi_aff_product(
4588 isl_multi_aff_copy(pma1->p[i].maff),
4589 isl_multi_aff_copy(pma2->p[j].maff));
4590 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4594 isl_pw_multi_aff_free(pma1);
4595 isl_pw_multi_aff_free(pma2);
4596 return res;
4597 error:
4598 isl_pw_multi_aff_free(pma1);
4599 isl_pw_multi_aff_free(pma2);
4600 return NULL;
4603 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4604 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4606 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4607 &pw_multi_aff_product);
4610 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4611 * denominator "denom".
4612 * "denom" is allowed to be negative, in which case the actual denominator
4613 * is -denom and the expressions are added instead.
4615 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4616 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4618 int i, first;
4619 int sign;
4620 isl_int d;
4622 first = isl_seq_first_non_zero(c, n);
4623 if (first == -1)
4624 return aff;
4626 sign = isl_int_sgn(denom);
4627 isl_int_init(d);
4628 isl_int_abs(d, denom);
4629 for (i = first; i < n; ++i) {
4630 isl_aff *aff_i;
4632 if (isl_int_is_zero(c[i]))
4633 continue;
4634 aff_i = isl_multi_aff_get_aff(ma, i);
4635 aff_i = isl_aff_scale(aff_i, c[i]);
4636 aff_i = isl_aff_scale_down(aff_i, d);
4637 if (sign >= 0)
4638 aff = isl_aff_sub(aff, aff_i);
4639 else
4640 aff = isl_aff_add(aff, aff_i);
4642 isl_int_clear(d);
4644 return aff;
4647 /* Extract an affine expression that expresses the output dimension "pos"
4648 * of "bmap" in terms of the parameters and input dimensions from
4649 * equality "eq".
4650 * Note that this expression may involve integer divisions defined
4651 * in terms of parameters and input dimensions.
4652 * The equality may also involve references to earlier (but not later)
4653 * output dimensions. These are replaced by the corresponding elements
4654 * in "ma".
4656 * If the equality is of the form
4658 * f(i) + h(j) + a x + g(i) = 0,
4660 * with f(i) a linear combinations of the parameters and input dimensions,
4661 * g(i) a linear combination of integer divisions defined in terms of the same
4662 * and h(j) a linear combinations of earlier output dimensions,
4663 * then the affine expression is
4665 * (-f(i) - g(i))/a - h(j)/a
4667 * If the equality is of the form
4669 * f(i) + h(j) - a x + g(i) = 0,
4671 * then the affine expression is
4673 * (f(i) + g(i))/a - h(j)/(-a)
4676 * If "div" refers to an integer division (i.e., it is smaller than
4677 * the number of integer divisions), then the equality constraint
4678 * does involve an integer division (the one at position "div") that
4679 * is defined in terms of output dimensions. However, this integer
4680 * division can be eliminated by exploiting a pair of constraints
4681 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4682 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4683 * -l + x >= 0.
4684 * In particular, let
4686 * x = e(i) + m floor(...)
4688 * with e(i) the expression derived above and floor(...) the integer
4689 * division involving output dimensions.
4690 * From
4692 * l <= x <= l + n,
4694 * we have
4696 * 0 <= x - l <= n
4698 * This means
4700 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4701 * = (e(i) - l) mod m
4703 * Therefore,
4705 * x - l = (e(i) - l) mod m
4707 * or
4709 * x = ((e(i) - l) mod m) + l
4711 * The variable "shift" below contains the expression -l, which may
4712 * also involve a linear combination of earlier output dimensions.
4714 static __isl_give isl_aff *extract_aff_from_equality(
4715 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4716 __isl_keep isl_multi_aff *ma)
4718 unsigned o_out;
4719 isl_size n_div, n_out;
4720 isl_ctx *ctx;
4721 isl_local_space *ls;
4722 isl_aff *aff, *shift;
4723 isl_val *mod;
4725 ctx = isl_basic_map_get_ctx(bmap);
4726 ls = isl_basic_map_get_local_space(bmap);
4727 ls = isl_local_space_domain(ls);
4728 aff = isl_aff_alloc(isl_local_space_copy(ls));
4729 if (!aff)
4730 goto error;
4731 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4732 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4733 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4734 if (n_out < 0 || n_div < 0)
4735 goto error;
4736 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4737 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4738 isl_seq_cpy(aff->v->el + 1 + o_out,
4739 bmap->eq[eq] + o_out + n_out, n_div);
4740 } else {
4741 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4742 isl_seq_neg(aff->v->el + 1 + o_out,
4743 bmap->eq[eq] + o_out + n_out, n_div);
4745 if (div < n_div)
4746 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4747 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4748 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4749 bmap->eq[eq][o_out + pos]);
4750 if (div < n_div) {
4751 shift = isl_aff_alloc(isl_local_space_copy(ls));
4752 if (!shift)
4753 goto error;
4754 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4755 isl_seq_cpy(shift->v->el + 1 + o_out,
4756 bmap->ineq[ineq] + o_out + n_out, n_div);
4757 isl_int_set_si(shift->v->el[0], 1);
4758 shift = subtract_initial(shift, ma, pos,
4759 bmap->ineq[ineq] + o_out, ctx->negone);
4760 aff = isl_aff_add(aff, isl_aff_copy(shift));
4761 mod = isl_val_int_from_isl_int(ctx,
4762 bmap->eq[eq][o_out + n_out + div]);
4763 mod = isl_val_abs(mod);
4764 aff = isl_aff_mod_val(aff, mod);
4765 aff = isl_aff_sub(aff, shift);
4768 isl_local_space_free(ls);
4769 return aff;
4770 error:
4771 isl_local_space_free(ls);
4772 isl_aff_free(aff);
4773 return NULL;
4776 /* Given a basic map with output dimensions defined
4777 * in terms of the parameters input dimensions and earlier
4778 * output dimensions using an equality (and possibly a pair on inequalities),
4779 * extract an isl_aff that expresses output dimension "pos" in terms
4780 * of the parameters and input dimensions.
4781 * Note that this expression may involve integer divisions defined
4782 * in terms of parameters and input dimensions.
4783 * "ma" contains the expressions corresponding to earlier output dimensions.
4785 * This function shares some similarities with
4786 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4788 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4789 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4791 int eq, div, ineq;
4792 isl_aff *aff;
4794 if (!bmap)
4795 return NULL;
4796 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4797 if (eq >= bmap->n_eq)
4798 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4799 "unable to find suitable equality", return NULL);
4800 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4802 aff = isl_aff_remove_unused_divs(aff);
4803 return aff;
4806 /* Given a basic map where each output dimension is defined
4807 * in terms of the parameters and input dimensions using an equality,
4808 * extract an isl_multi_aff that expresses the output dimensions in terms
4809 * of the parameters and input dimensions.
4811 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4812 __isl_take isl_basic_map *bmap)
4814 int i;
4815 isl_size n_out;
4816 isl_multi_aff *ma;
4818 if (!bmap)
4819 return NULL;
4821 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4822 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4823 if (n_out < 0)
4824 ma = isl_multi_aff_free(ma);
4826 for (i = 0; i < n_out; ++i) {
4827 isl_aff *aff;
4829 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4830 ma = isl_multi_aff_set_aff(ma, i, aff);
4833 isl_basic_map_free(bmap);
4835 return ma;
4838 /* Given a basic set where each set dimension is defined
4839 * in terms of the parameters using an equality,
4840 * extract an isl_multi_aff that expresses the set dimensions in terms
4841 * of the parameters.
4843 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4844 __isl_take isl_basic_set *bset)
4846 return extract_isl_multi_aff_from_basic_map(bset);
4849 /* Create an isl_pw_multi_aff that is equivalent to
4850 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4851 * The given basic map is such that each output dimension is defined
4852 * in terms of the parameters and input dimensions using an equality.
4854 * Since some applications expect the result of isl_pw_multi_aff_from_map
4855 * to only contain integer affine expressions, we compute the floor
4856 * of the expression before returning.
4858 * Remove all constraints involving local variables without
4859 * an explicit representation (resulting in the removal of those
4860 * local variables) prior to the actual extraction to ensure
4861 * that the local spaces in which the resulting affine expressions
4862 * are created do not contain any unknown local variables.
4863 * Removing such constraints is safe because constraints involving
4864 * unknown local variables are not used to determine whether
4865 * a basic map is obviously single-valued.
4867 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4868 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4870 isl_multi_aff *ma;
4872 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4873 ma = extract_isl_multi_aff_from_basic_map(bmap);
4874 ma = isl_multi_aff_floor(ma);
4875 return isl_pw_multi_aff_alloc(domain, ma);
4878 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4879 * This obviously only works if the input "map" is single-valued.
4880 * If so, we compute the lexicographic minimum of the image in the form
4881 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4882 * to its lexicographic minimum.
4883 * If the input is not single-valued, we produce an error.
4885 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4886 __isl_take isl_map *map)
4888 int i;
4889 int sv;
4890 isl_pw_multi_aff *pma;
4892 sv = isl_map_is_single_valued(map);
4893 if (sv < 0)
4894 goto error;
4895 if (!sv)
4896 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4897 "map is not single-valued", goto error);
4898 map = isl_map_make_disjoint(map);
4899 if (!map)
4900 return NULL;
4902 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4904 for (i = 0; i < map->n; ++i) {
4905 isl_pw_multi_aff *pma_i;
4906 isl_basic_map *bmap;
4907 bmap = isl_basic_map_copy(map->p[i]);
4908 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4909 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4912 isl_map_free(map);
4913 return pma;
4914 error:
4915 isl_map_free(map);
4916 return NULL;
4919 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4920 * taking into account that the output dimension at position "d"
4921 * can be represented as
4923 * x = floor((e(...) + c1) / m)
4925 * given that constraint "i" is of the form
4927 * e(...) + c1 - m x >= 0
4930 * Let "map" be of the form
4932 * A -> B
4934 * We construct a mapping
4936 * A -> [A -> x = floor(...)]
4938 * apply that to the map, obtaining
4940 * [A -> x = floor(...)] -> B
4942 * and equate dimension "d" to x.
4943 * We then compute a isl_pw_multi_aff representation of the resulting map
4944 * and plug in the mapping above.
4946 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4947 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4949 isl_ctx *ctx;
4950 isl_space *space = NULL;
4951 isl_local_space *ls;
4952 isl_multi_aff *ma;
4953 isl_aff *aff;
4954 isl_vec *v;
4955 isl_map *insert;
4956 int offset;
4957 isl_size n;
4958 isl_size n_in;
4959 isl_pw_multi_aff *pma;
4960 isl_bool is_set;
4962 is_set = isl_map_is_set(map);
4963 if (is_set < 0)
4964 goto error;
4966 offset = isl_basic_map_offset(hull, isl_dim_out);
4967 ctx = isl_map_get_ctx(map);
4968 space = isl_space_domain(isl_map_get_space(map));
4969 n_in = isl_space_dim(space, isl_dim_set);
4970 n = isl_space_dim(space, isl_dim_all);
4971 if (n_in < 0 || n < 0)
4972 goto error;
4974 v = isl_vec_alloc(ctx, 1 + 1 + n);
4975 if (v) {
4976 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4977 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4979 isl_basic_map_free(hull);
4981 ls = isl_local_space_from_space(isl_space_copy(space));
4982 aff = isl_aff_alloc_vec(ls, v);
4983 aff = isl_aff_floor(aff);
4984 if (is_set) {
4985 isl_space_free(space);
4986 ma = isl_multi_aff_from_aff(aff);
4987 } else {
4988 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4989 ma = isl_multi_aff_range_product(ma,
4990 isl_multi_aff_from_aff(aff));
4993 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4994 map = isl_map_apply_domain(map, insert);
4995 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4996 pma = isl_pw_multi_aff_from_map(map);
4997 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4999 return pma;
5000 error:
5001 isl_space_free(space);
5002 isl_map_free(map);
5003 isl_basic_map_free(hull);
5004 return NULL;
5007 /* Is constraint "c" of the form
5009 * e(...) + c1 - m x >= 0
5011 * or
5013 * -e(...) + c2 + m x >= 0
5015 * where m > 1 and e only depends on parameters and input dimemnsions?
5017 * "offset" is the offset of the output dimensions
5018 * "pos" is the position of output dimension x.
5020 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5022 if (isl_int_is_zero(c[offset + d]))
5023 return 0;
5024 if (isl_int_is_one(c[offset + d]))
5025 return 0;
5026 if (isl_int_is_negone(c[offset + d]))
5027 return 0;
5028 if (isl_seq_first_non_zero(c + offset, d) != -1)
5029 return 0;
5030 if (isl_seq_first_non_zero(c + offset + d + 1,
5031 total - (offset + d + 1)) != -1)
5032 return 0;
5033 return 1;
5036 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5038 * As a special case, we first check if there is any pair of constraints,
5039 * shared by all the basic maps in "map" that force a given dimension
5040 * to be equal to the floor of some affine combination of the input dimensions.
5042 * In particular, if we can find two constraints
5044 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5046 * and
5048 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5050 * where m > 1 and e only depends on parameters and input dimemnsions,
5051 * and such that
5053 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5055 * then we know that we can take
5057 * x = floor((e(...) + c1) / m)
5059 * without having to perform any computation.
5061 * Note that we know that
5063 * c1 + c2 >= 1
5065 * If c1 + c2 were 0, then we would have detected an equality during
5066 * simplification. If c1 + c2 were negative, then we would have detected
5067 * a contradiction.
5069 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5070 __isl_take isl_map *map)
5072 int d;
5073 isl_size dim;
5074 int i, j, n;
5075 int offset;
5076 isl_size total;
5077 isl_int sum;
5078 isl_basic_map *hull;
5080 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5081 dim = isl_map_dim(map, isl_dim_out);
5082 total = isl_basic_map_dim(hull, isl_dim_all);
5083 if (dim < 0 || total < 0)
5084 goto error;
5086 isl_int_init(sum);
5087 offset = isl_basic_map_offset(hull, isl_dim_out);
5088 n = hull->n_ineq;
5089 for (d = 0; d < dim; ++d) {
5090 for (i = 0; i < n; ++i) {
5091 if (!is_potential_div_constraint(hull->ineq[i],
5092 offset, d, 1 + total))
5093 continue;
5094 for (j = i + 1; j < n; ++j) {
5095 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5096 hull->ineq[j] + 1, total))
5097 continue;
5098 isl_int_add(sum, hull->ineq[i][0],
5099 hull->ineq[j][0]);
5100 if (isl_int_abs_lt(sum,
5101 hull->ineq[i][offset + d]))
5102 break;
5105 if (j >= n)
5106 continue;
5107 isl_int_clear(sum);
5108 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5109 j = i;
5110 return pw_multi_aff_from_map_div(map, hull, d, j);
5113 isl_int_clear(sum);
5114 isl_basic_map_free(hull);
5115 return pw_multi_aff_from_map_base(map);
5116 error:
5117 isl_map_free(map);
5118 isl_basic_map_free(hull);
5119 return NULL;
5122 /* Given an affine expression
5124 * [A -> B] -> f(A,B)
5126 * construct an isl_multi_aff
5128 * [A -> B] -> B'
5130 * such that dimension "d" in B' is set to "aff" and the remaining
5131 * dimensions are set equal to the corresponding dimensions in B.
5132 * "n_in" is the dimension of the space A.
5133 * "n_out" is the dimension of the space B.
5135 * If "is_set" is set, then the affine expression is of the form
5137 * [B] -> f(B)
5139 * and we construct an isl_multi_aff
5141 * B -> B'
5143 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5144 unsigned n_in, unsigned n_out, int is_set)
5146 int i;
5147 isl_multi_aff *ma;
5148 isl_space *space, *space2;
5149 isl_local_space *ls;
5151 space = isl_aff_get_domain_space(aff);
5152 ls = isl_local_space_from_space(isl_space_copy(space));
5153 space2 = isl_space_copy(space);
5154 if (!is_set)
5155 space2 = isl_space_range(isl_space_unwrap(space2));
5156 space = isl_space_map_from_domain_and_range(space, space2);
5157 ma = isl_multi_aff_alloc(space);
5158 ma = isl_multi_aff_set_aff(ma, d, aff);
5160 for (i = 0; i < n_out; ++i) {
5161 if (i == d)
5162 continue;
5163 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5164 isl_dim_set, n_in + i);
5165 ma = isl_multi_aff_set_aff(ma, i, aff);
5168 isl_local_space_free(ls);
5170 return ma;
5173 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5174 * taking into account that the dimension at position "d" can be written as
5176 * x = m a + f(..) (1)
5178 * where m is equal to "gcd".
5179 * "i" is the index of the equality in "hull" that defines f(..).
5180 * In particular, the equality is of the form
5182 * f(..) - x + m g(existentials) = 0
5184 * or
5186 * -f(..) + x + m g(existentials) = 0
5188 * We basically plug (1) into "map", resulting in a map with "a"
5189 * in the range instead of "x". The corresponding isl_pw_multi_aff
5190 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5192 * Specifically, given the input map
5194 * A -> B
5196 * We first wrap it into a set
5198 * [A -> B]
5200 * and define (1) on top of the corresponding space, resulting in "aff".
5201 * We use this to create an isl_multi_aff that maps the output position "d"
5202 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5203 * We plug this into the wrapped map, unwrap the result and compute the
5204 * corresponding isl_pw_multi_aff.
5205 * The result is an expression
5207 * A -> T(A)
5209 * We adjust that to
5211 * A -> [A -> T(A)]
5213 * so that we can plug that into "aff", after extending the latter to
5214 * a mapping
5216 * [A -> B] -> B'
5219 * If "map" is actually a set, then there is no "A" space, meaning
5220 * that we do not need to perform any wrapping, and that the result
5221 * of the recursive call is of the form
5223 * [T]
5225 * which is plugged into a mapping of the form
5227 * B -> B'
5229 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5230 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5231 isl_int gcd)
5233 isl_set *set;
5234 isl_space *space;
5235 isl_local_space *ls;
5236 isl_aff *aff;
5237 isl_multi_aff *ma;
5238 isl_pw_multi_aff *pma, *id;
5239 isl_size n_in;
5240 unsigned o_out;
5241 isl_size n_out;
5242 isl_bool is_set;
5244 is_set = isl_map_is_set(map);
5245 if (is_set < 0)
5246 goto error;
5248 n_in = isl_basic_map_dim(hull, isl_dim_in);
5249 n_out = isl_basic_map_dim(hull, isl_dim_out);
5250 if (n_in < 0 || n_out < 0)
5251 goto error;
5252 o_out = isl_basic_map_offset(hull, isl_dim_out);
5254 if (is_set)
5255 set = map;
5256 else
5257 set = isl_map_wrap(map);
5258 space = isl_space_map_from_set(isl_set_get_space(set));
5259 ma = isl_multi_aff_identity(space);
5260 ls = isl_local_space_from_space(isl_set_get_space(set));
5261 aff = isl_aff_alloc(ls);
5262 if (aff) {
5263 isl_int_set_si(aff->v->el[0], 1);
5264 if (isl_int_is_one(hull->eq[i][o_out + d]))
5265 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5266 aff->v->size - 1);
5267 else
5268 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5269 aff->v->size - 1);
5270 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5272 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5273 set = isl_set_preimage_multi_aff(set, ma);
5275 ma = range_map(aff, d, n_in, n_out, is_set);
5277 if (is_set)
5278 map = set;
5279 else
5280 map = isl_set_unwrap(set);
5281 pma = isl_pw_multi_aff_from_map(map);
5283 if (!is_set) {
5284 space = isl_pw_multi_aff_get_domain_space(pma);
5285 space = isl_space_map_from_set(space);
5286 id = isl_pw_multi_aff_identity(space);
5287 pma = isl_pw_multi_aff_range_product(id, pma);
5289 id = isl_pw_multi_aff_from_multi_aff(ma);
5290 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5292 isl_basic_map_free(hull);
5293 return pma;
5294 error:
5295 isl_map_free(map);
5296 isl_basic_map_free(hull);
5297 return NULL;
5300 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5301 * "hull" contains the equalities valid for "map".
5303 * Check if any of the output dimensions is "strided".
5304 * That is, we check if it can be written as
5306 * x = m a + f(..)
5308 * with m greater than 1, a some combination of existentially quantified
5309 * variables and f an expression in the parameters and input dimensions.
5310 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5312 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5313 * special case.
5315 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5316 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5318 int i, j;
5319 isl_size n_out;
5320 unsigned o_out;
5321 isl_size n_div;
5322 unsigned o_div;
5323 isl_int gcd;
5325 n_div = isl_basic_map_dim(hull, isl_dim_div);
5326 n_out = isl_basic_map_dim(hull, isl_dim_out);
5327 if (n_div < 0 || n_out < 0)
5328 goto error;
5330 if (n_div == 0) {
5331 isl_basic_map_free(hull);
5332 return pw_multi_aff_from_map_check_div(map);
5335 isl_int_init(gcd);
5337 o_div = isl_basic_map_offset(hull, isl_dim_div);
5338 o_out = isl_basic_map_offset(hull, isl_dim_out);
5340 for (i = 0; i < n_out; ++i) {
5341 for (j = 0; j < hull->n_eq; ++j) {
5342 isl_int *eq = hull->eq[j];
5343 isl_pw_multi_aff *res;
5345 if (!isl_int_is_one(eq[o_out + i]) &&
5346 !isl_int_is_negone(eq[o_out + i]))
5347 continue;
5348 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5349 continue;
5350 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5351 n_out - (i + 1)) != -1)
5352 continue;
5353 isl_seq_gcd(eq + o_div, n_div, &gcd);
5354 if (isl_int_is_zero(gcd))
5355 continue;
5356 if (isl_int_is_one(gcd))
5357 continue;
5359 res = pw_multi_aff_from_map_stride(map, hull,
5360 i, j, gcd);
5361 isl_int_clear(gcd);
5362 return res;
5366 isl_int_clear(gcd);
5367 isl_basic_map_free(hull);
5368 return pw_multi_aff_from_map_check_div(map);
5369 error:
5370 isl_map_free(map);
5371 isl_basic_map_free(hull);
5372 return NULL;
5375 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5377 * As a special case, we first check if all output dimensions are uniquely
5378 * defined in terms of the parameters and input dimensions over the entire
5379 * domain. If so, we extract the desired isl_pw_multi_aff directly
5380 * from the affine hull of "map" and its domain.
5382 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5383 * special cases.
5385 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5387 isl_bool sv;
5388 isl_size n;
5389 isl_basic_map *hull;
5391 n = isl_map_n_basic_map(map);
5392 if (n < 0)
5393 goto error;
5395 if (n == 1) {
5396 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5397 hull = isl_basic_map_plain_affine_hull(hull);
5398 sv = isl_basic_map_plain_is_single_valued(hull);
5399 if (sv >= 0 && sv)
5400 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5401 hull);
5402 isl_basic_map_free(hull);
5404 map = isl_map_detect_equalities(map);
5405 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5406 sv = isl_basic_map_plain_is_single_valued(hull);
5407 if (sv >= 0 && sv)
5408 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5409 if (sv >= 0)
5410 return pw_multi_aff_from_map_check_strides(map, hull);
5411 isl_basic_map_free(hull);
5412 error:
5413 isl_map_free(map);
5414 return NULL;
5417 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5419 return isl_pw_multi_aff_from_map(set);
5422 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5423 * add it to *user.
5425 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5427 isl_union_pw_multi_aff **upma = user;
5428 isl_pw_multi_aff *pma;
5430 pma = isl_pw_multi_aff_from_map(map);
5431 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5433 return *upma ? isl_stat_ok : isl_stat_error;
5436 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5437 * domain.
5439 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5440 __isl_take isl_aff *aff)
5442 isl_multi_aff *ma;
5443 isl_pw_multi_aff *pma;
5445 ma = isl_multi_aff_from_aff(aff);
5446 pma = isl_pw_multi_aff_from_multi_aff(ma);
5447 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5450 /* Try and create an isl_union_pw_multi_aff that is equivalent
5451 * to the given isl_union_map.
5452 * The isl_union_map is required to be single-valued in each space.
5453 * Otherwise, an error is produced.
5455 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5456 __isl_take isl_union_map *umap)
5458 isl_space *space;
5459 isl_union_pw_multi_aff *upma;
5461 space = isl_union_map_get_space(umap);
5462 upma = isl_union_pw_multi_aff_empty(space);
5463 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5464 upma = isl_union_pw_multi_aff_free(upma);
5465 isl_union_map_free(umap);
5467 return upma;
5470 /* Try and create an isl_union_pw_multi_aff that is equivalent
5471 * to the given isl_union_set.
5472 * The isl_union_set is required to be a singleton in each space.
5473 * Otherwise, an error is produced.
5475 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5476 __isl_take isl_union_set *uset)
5478 return isl_union_pw_multi_aff_from_union_map(uset);
5481 /* Return the piecewise affine expression "set ? 1 : 0".
5483 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5485 isl_pw_aff *pa;
5486 isl_space *space = isl_set_get_space(set);
5487 isl_local_space *ls = isl_local_space_from_space(space);
5488 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5489 isl_aff *one = isl_aff_zero_on_domain(ls);
5491 one = isl_aff_add_constant_si(one, 1);
5492 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5493 set = isl_set_complement(set);
5494 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5496 return pa;
5499 /* Plug in "subs" for dimension "type", "pos" of "aff".
5501 * Let i be the dimension to replace and let "subs" be of the form
5503 * f/d
5505 * and "aff" of the form
5507 * (a i + g)/m
5509 * The result is
5511 * (a f + d g')/(m d)
5513 * where g' is the result of plugging in "subs" in each of the integer
5514 * divisions in g.
5516 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5517 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5519 isl_ctx *ctx;
5520 isl_int v;
5521 isl_size n_div;
5523 aff = isl_aff_cow(aff);
5524 if (!aff || !subs)
5525 return isl_aff_free(aff);
5527 ctx = isl_aff_get_ctx(aff);
5528 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5529 isl_die(ctx, isl_error_invalid,
5530 "spaces don't match", return isl_aff_free(aff));
5531 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5532 if (n_div < 0)
5533 return isl_aff_free(aff);
5534 if (n_div != 0)
5535 isl_die(ctx, isl_error_unsupported,
5536 "cannot handle divs yet", return isl_aff_free(aff));
5538 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5539 if (!aff->ls)
5540 return isl_aff_free(aff);
5542 aff->v = isl_vec_cow(aff->v);
5543 if (!aff->v)
5544 return isl_aff_free(aff);
5546 pos += isl_local_space_offset(aff->ls, type);
5548 isl_int_init(v);
5549 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5550 aff->v->size, subs->v->size, v);
5551 isl_int_clear(v);
5553 return aff;
5556 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5557 * expressions in "maff".
5559 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5560 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5561 __isl_keep isl_aff *subs)
5563 int i;
5565 maff = isl_multi_aff_cow(maff);
5566 if (!maff || !subs)
5567 return isl_multi_aff_free(maff);
5569 if (type == isl_dim_in)
5570 type = isl_dim_set;
5572 for (i = 0; i < maff->n; ++i) {
5573 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5574 type, pos, subs);
5575 if (!maff->u.p[i])
5576 return isl_multi_aff_free(maff);
5579 return maff;
5582 /* Plug in "subs" for dimension "type", "pos" of "pma".
5584 * pma is of the form
5586 * A_i(v) -> M_i(v)
5588 * while subs is of the form
5590 * v' = B_j(v) -> S_j
5592 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5593 * has a contribution in the result, in particular
5595 * C_ij(S_j) -> M_i(S_j)
5597 * Note that plugging in S_j in C_ij may also result in an empty set
5598 * and this contribution should simply be discarded.
5600 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5601 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5602 __isl_keep isl_pw_aff *subs)
5604 int i, j, n;
5605 isl_pw_multi_aff *res;
5607 if (!pma || !subs)
5608 return isl_pw_multi_aff_free(pma);
5610 n = pma->n * subs->n;
5611 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5613 for (i = 0; i < pma->n; ++i) {
5614 for (j = 0; j < subs->n; ++j) {
5615 isl_set *common;
5616 isl_multi_aff *res_ij;
5617 int empty;
5619 common = isl_set_intersect(
5620 isl_set_copy(pma->p[i].set),
5621 isl_set_copy(subs->p[j].set));
5622 common = isl_set_substitute(common,
5623 type, pos, subs->p[j].aff);
5624 empty = isl_set_plain_is_empty(common);
5625 if (empty < 0 || empty) {
5626 isl_set_free(common);
5627 if (empty < 0)
5628 goto error;
5629 continue;
5632 res_ij = isl_multi_aff_substitute(
5633 isl_multi_aff_copy(pma->p[i].maff),
5634 type, pos, subs->p[j].aff);
5636 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5640 isl_pw_multi_aff_free(pma);
5641 return res;
5642 error:
5643 isl_pw_multi_aff_free(pma);
5644 isl_pw_multi_aff_free(res);
5645 return NULL;
5648 /* Compute the preimage of a range of dimensions in the affine expression "src"
5649 * under "ma" and put the result in "dst". The number of dimensions in "src"
5650 * that precede the range is given by "n_before". The number of dimensions
5651 * in the range is given by the number of output dimensions of "ma".
5652 * The number of dimensions that follow the range is given by "n_after".
5653 * If "has_denom" is set (to one),
5654 * then "src" and "dst" have an extra initial denominator.
5655 * "n_div_ma" is the number of existentials in "ma"
5656 * "n_div_bset" is the number of existentials in "src"
5657 * The resulting "dst" (which is assumed to have been allocated by
5658 * the caller) contains coefficients for both sets of existentials,
5659 * first those in "ma" and then those in "src".
5660 * f, c1, c2 and g are temporary objects that have been initialized
5661 * by the caller.
5663 * Let src represent the expression
5665 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5667 * and let ma represent the expressions
5669 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5671 * We start out with the following expression for dst:
5673 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5675 * with the multiplication factor f initially equal to 1
5676 * and f \sum_i b_i v_i kept separately.
5677 * For each x_i that we substitute, we multiply the numerator
5678 * (and denominator) of dst by c_1 = m_i and add the numerator
5679 * of the x_i expression multiplied by c_2 = f b_i,
5680 * after removing the common factors of c_1 and c_2.
5681 * The multiplication factor f also needs to be multiplied by c_1
5682 * for the next x_j, j > i.
5684 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5685 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5686 int n_div_ma, int n_div_bmap,
5687 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5689 int i;
5690 isl_size n_param, n_in, n_out;
5691 int o_dst, o_src;
5693 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5694 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5695 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5696 if (n_param < 0 || n_in < 0 || n_out < 0)
5697 return isl_stat_error;
5699 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5700 o_dst = o_src = has_denom + 1 + n_param + n_before;
5701 isl_seq_clr(dst + o_dst, n_in);
5702 o_dst += n_in;
5703 o_src += n_out;
5704 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5705 o_dst += n_after;
5706 o_src += n_after;
5707 isl_seq_clr(dst + o_dst, n_div_ma);
5708 o_dst += n_div_ma;
5709 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5711 isl_int_set_si(f, 1);
5713 for (i = 0; i < n_out; ++i) {
5714 int offset = has_denom + 1 + n_param + n_before + i;
5716 if (isl_int_is_zero(src[offset]))
5717 continue;
5718 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5719 isl_int_mul(c2, f, src[offset]);
5720 isl_int_gcd(g, c1, c2);
5721 isl_int_divexact(c1, c1, g);
5722 isl_int_divexact(c2, c2, g);
5724 isl_int_mul(f, f, c1);
5725 o_dst = has_denom;
5726 o_src = 1;
5727 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5728 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5729 o_dst += 1 + n_param;
5730 o_src += 1 + n_param;
5731 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5732 o_dst += n_before;
5733 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5734 c2, ma->u.p[i]->v->el + o_src, n_in);
5735 o_dst += n_in;
5736 o_src += n_in;
5737 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5738 o_dst += n_after;
5739 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5740 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5741 o_dst += n_div_ma;
5742 o_src += n_div_ma;
5743 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5744 if (has_denom)
5745 isl_int_mul(dst[0], dst[0], c1);
5748 return isl_stat_ok;
5751 /* Compute the pullback of "aff" by the function represented by "ma".
5752 * In other words, plug in "ma" in "aff". The result is an affine expression
5753 * defined over the domain space of "ma".
5755 * If "aff" is represented by
5757 * (a(p) + b x + c(divs))/d
5759 * and ma is represented by
5761 * x = D(p) + F(y) + G(divs')
5763 * then the result is
5765 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5767 * The divs in the local space of the input are similarly adjusted
5768 * through a call to isl_local_space_preimage_multi_aff.
5770 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5771 __isl_take isl_multi_aff *ma)
5773 isl_aff *res = NULL;
5774 isl_local_space *ls;
5775 isl_size n_div_aff, n_div_ma;
5776 isl_int f, c1, c2, g;
5778 ma = isl_multi_aff_align_divs(ma);
5779 if (!aff || !ma)
5780 goto error;
5782 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5783 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5784 if (n_div_aff < 0 || n_div_ma < 0)
5785 goto error;
5787 ls = isl_aff_get_domain_local_space(aff);
5788 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5789 res = isl_aff_alloc(ls);
5790 if (!res)
5791 goto error;
5793 isl_int_init(f);
5794 isl_int_init(c1);
5795 isl_int_init(c2);
5796 isl_int_init(g);
5798 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5799 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5800 res = isl_aff_free(res);
5802 isl_int_clear(f);
5803 isl_int_clear(c1);
5804 isl_int_clear(c2);
5805 isl_int_clear(g);
5807 isl_aff_free(aff);
5808 isl_multi_aff_free(ma);
5809 res = isl_aff_normalize(res);
5810 return res;
5811 error:
5812 isl_aff_free(aff);
5813 isl_multi_aff_free(ma);
5814 isl_aff_free(res);
5815 return NULL;
5818 /* Compute the pullback of "aff1" by the function represented by "aff2".
5819 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5820 * defined over the domain space of "aff1".
5822 * The domain of "aff1" should match the range of "aff2", which means
5823 * that it should be single-dimensional.
5825 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5826 __isl_take isl_aff *aff2)
5828 isl_multi_aff *ma;
5830 ma = isl_multi_aff_from_aff(aff2);
5831 return isl_aff_pullback_multi_aff(aff1, ma);
5834 /* Compute the pullback of "ma1" by the function represented by "ma2".
5835 * In other words, plug in "ma2" in "ma1".
5837 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5839 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5840 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5842 int i;
5843 isl_space *space = NULL;
5845 ma2 = isl_multi_aff_align_divs(ma2);
5846 ma1 = isl_multi_aff_cow(ma1);
5847 if (!ma1 || !ma2)
5848 goto error;
5850 space = isl_space_join(isl_multi_aff_get_space(ma2),
5851 isl_multi_aff_get_space(ma1));
5853 for (i = 0; i < ma1->n; ++i) {
5854 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5855 isl_multi_aff_copy(ma2));
5856 if (!ma1->u.p[i])
5857 goto error;
5860 ma1 = isl_multi_aff_reset_space(ma1, space);
5861 isl_multi_aff_free(ma2);
5862 return ma1;
5863 error:
5864 isl_space_free(space);
5865 isl_multi_aff_free(ma2);
5866 isl_multi_aff_free(ma1);
5867 return NULL;
5870 /* Compute the pullback of "ma1" by the function represented by "ma2".
5871 * In other words, plug in "ma2" in "ma1".
5873 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5874 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5876 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5877 &isl_multi_aff_pullback_multi_aff_aligned);
5880 /* Extend the local space of "dst" to include the divs
5881 * in the local space of "src".
5883 * If "src" does not have any divs or if the local spaces of "dst" and
5884 * "src" are the same, then no extension is required.
5886 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5887 __isl_keep isl_aff *src)
5889 isl_ctx *ctx;
5890 isl_size src_n_div, dst_n_div;
5891 int *exp1 = NULL;
5892 int *exp2 = NULL;
5893 isl_bool equal;
5894 isl_mat *div;
5896 if (!src || !dst)
5897 return isl_aff_free(dst);
5899 ctx = isl_aff_get_ctx(src);
5900 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5901 if (equal < 0)
5902 return isl_aff_free(dst);
5903 if (!equal)
5904 isl_die(ctx, isl_error_invalid,
5905 "spaces don't match", goto error);
5907 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
5908 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
5909 if (src_n_div == 0)
5910 return dst;
5911 equal = isl_local_space_is_equal(src->ls, dst->ls);
5912 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5913 return isl_aff_free(dst);
5914 if (equal)
5915 return dst;
5917 exp1 = isl_alloc_array(ctx, int, src_n_div);
5918 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5919 if (!exp1 || (dst_n_div && !exp2))
5920 goto error;
5922 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5923 dst = isl_aff_expand_divs(dst, div, exp2);
5924 free(exp1);
5925 free(exp2);
5927 return dst;
5928 error:
5929 free(exp1);
5930 free(exp2);
5931 return isl_aff_free(dst);
5934 /* Adjust the local spaces of the affine expressions in "maff"
5935 * such that they all have the save divs.
5937 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5938 __isl_take isl_multi_aff *maff)
5940 int i;
5942 if (!maff)
5943 return NULL;
5944 if (maff->n == 0)
5945 return maff;
5946 maff = isl_multi_aff_cow(maff);
5947 if (!maff)
5948 return NULL;
5950 for (i = 1; i < maff->n; ++i)
5951 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5952 for (i = 1; i < maff->n; ++i) {
5953 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5954 if (!maff->u.p[i])
5955 return isl_multi_aff_free(maff);
5958 return maff;
5961 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5963 aff = isl_aff_cow(aff);
5964 if (!aff)
5965 return NULL;
5967 aff->ls = isl_local_space_lift(aff->ls);
5968 if (!aff->ls)
5969 return isl_aff_free(aff);
5971 return aff;
5974 /* Lift "maff" to a space with extra dimensions such that the result
5975 * has no more existentially quantified variables.
5976 * If "ls" is not NULL, then *ls is assigned the local space that lies
5977 * at the basis of the lifting applied to "maff".
5979 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5980 __isl_give isl_local_space **ls)
5982 int i;
5983 isl_space *space;
5984 isl_size n_div;
5986 if (ls)
5987 *ls = NULL;
5989 if (!maff)
5990 return NULL;
5992 if (maff->n == 0) {
5993 if (ls) {
5994 isl_space *space = isl_multi_aff_get_domain_space(maff);
5995 *ls = isl_local_space_from_space(space);
5996 if (!*ls)
5997 return isl_multi_aff_free(maff);
5999 return maff;
6002 maff = isl_multi_aff_cow(maff);
6003 maff = isl_multi_aff_align_divs(maff);
6004 if (!maff)
6005 return NULL;
6007 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
6008 if (n_div < 0)
6009 return isl_multi_aff_free(maff);
6010 space = isl_multi_aff_get_space(maff);
6011 space = isl_space_lift(isl_space_domain(space), n_div);
6012 space = isl_space_extend_domain_with_range(space,
6013 isl_multi_aff_get_space(maff));
6014 if (!space)
6015 return isl_multi_aff_free(maff);
6016 isl_space_free(maff->space);
6017 maff->space = space;
6019 if (ls) {
6020 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
6021 if (!*ls)
6022 return isl_multi_aff_free(maff);
6025 for (i = 0; i < maff->n; ++i) {
6026 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
6027 if (!maff->u.p[i])
6028 goto error;
6031 return maff;
6032 error:
6033 if (ls)
6034 isl_local_space_free(*ls);
6035 return isl_multi_aff_free(maff);
6038 #undef TYPE
6039 #define TYPE isl_pw_multi_aff
6040 static
6041 #include "check_type_range_templ.c"
6043 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6045 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6046 __isl_keep isl_pw_multi_aff *pma, int pos)
6048 int i;
6049 isl_size n_out;
6050 isl_space *space;
6051 isl_pw_aff *pa;
6053 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6054 return NULL;
6056 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6057 if (n_out < 0)
6058 return NULL;
6060 space = isl_pw_multi_aff_get_space(pma);
6061 space = isl_space_drop_dims(space, isl_dim_out,
6062 pos + 1, n_out - pos - 1);
6063 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6065 pa = isl_pw_aff_alloc_size(space, pma->n);
6066 for (i = 0; i < pma->n; ++i) {
6067 isl_aff *aff;
6068 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6069 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6072 return pa;
6075 /* Return an isl_pw_multi_aff with the given "set" as domain and
6076 * an unnamed zero-dimensional range.
6078 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6079 __isl_take isl_set *set)
6081 isl_multi_aff *ma;
6082 isl_space *space;
6084 space = isl_set_get_space(set);
6085 space = isl_space_from_domain(space);
6086 ma = isl_multi_aff_zero(space);
6087 return isl_pw_multi_aff_alloc(set, ma);
6090 /* Add an isl_pw_multi_aff with the given "set" as domain and
6091 * an unnamed zero-dimensional range to *user.
6093 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6094 void *user)
6096 isl_union_pw_multi_aff **upma = user;
6097 isl_pw_multi_aff *pma;
6099 pma = isl_pw_multi_aff_from_domain(set);
6100 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6102 return isl_stat_ok;
6105 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6106 * an unnamed zero-dimensional range.
6108 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6109 __isl_take isl_union_set *uset)
6111 isl_space *space;
6112 isl_union_pw_multi_aff *upma;
6114 if (!uset)
6115 return NULL;
6117 space = isl_union_set_get_space(uset);
6118 upma = isl_union_pw_multi_aff_empty(space);
6120 if (isl_union_set_foreach_set(uset,
6121 &add_pw_multi_aff_from_domain, &upma) < 0)
6122 goto error;
6124 isl_union_set_free(uset);
6125 return upma;
6126 error:
6127 isl_union_set_free(uset);
6128 isl_union_pw_multi_aff_free(upma);
6129 return NULL;
6132 /* Local data for bin_entry and the callback "fn".
6134 struct isl_union_pw_multi_aff_bin_data {
6135 isl_union_pw_multi_aff *upma2;
6136 isl_union_pw_multi_aff *res;
6137 isl_pw_multi_aff *pma;
6138 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6141 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6142 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6144 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6146 struct isl_union_pw_multi_aff_bin_data *data = user;
6147 isl_stat r;
6149 data->pma = pma;
6150 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6151 data->fn, data);
6152 isl_pw_multi_aff_free(pma);
6154 return r;
6157 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6158 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6159 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6160 * as *entry. The callback should adjust data->res if desired.
6162 static __isl_give isl_union_pw_multi_aff *bin_op(
6163 __isl_take isl_union_pw_multi_aff *upma1,
6164 __isl_take isl_union_pw_multi_aff *upma2,
6165 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6167 isl_space *space;
6168 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6170 space = isl_union_pw_multi_aff_get_space(upma2);
6171 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6172 space = isl_union_pw_multi_aff_get_space(upma1);
6173 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6175 if (!upma1 || !upma2)
6176 goto error;
6178 data.upma2 = upma2;
6179 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6180 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6181 &bin_entry, &data) < 0)
6182 goto error;
6184 isl_union_pw_multi_aff_free(upma1);
6185 isl_union_pw_multi_aff_free(upma2);
6186 return data.res;
6187 error:
6188 isl_union_pw_multi_aff_free(upma1);
6189 isl_union_pw_multi_aff_free(upma2);
6190 isl_union_pw_multi_aff_free(data.res);
6191 return NULL;
6194 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6195 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6197 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6198 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6200 isl_space *space;
6202 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6203 isl_pw_multi_aff_get_space(pma2));
6204 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6205 &isl_multi_aff_range_product);
6208 /* Given two isl_pw_multi_affs A -> B and C -> D,
6209 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6211 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6212 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6214 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6215 &pw_multi_aff_range_product);
6218 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6219 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6221 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6222 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6224 isl_space *space;
6226 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6227 isl_pw_multi_aff_get_space(pma2));
6228 space = isl_space_flatten_range(space);
6229 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6230 &isl_multi_aff_flat_range_product);
6233 /* Given two isl_pw_multi_affs A -> B and C -> D,
6234 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6236 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6237 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6239 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6240 &pw_multi_aff_flat_range_product);
6243 /* If data->pma and "pma2" have the same domain space, then compute
6244 * their flat range product and the result to data->res.
6246 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6247 void *user)
6249 struct isl_union_pw_multi_aff_bin_data *data = user;
6251 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6252 pma2->dim, isl_dim_in)) {
6253 isl_pw_multi_aff_free(pma2);
6254 return isl_stat_ok;
6257 pma2 = isl_pw_multi_aff_flat_range_product(
6258 isl_pw_multi_aff_copy(data->pma), pma2);
6260 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6262 return isl_stat_ok;
6265 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6266 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6268 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6269 __isl_take isl_union_pw_multi_aff *upma1,
6270 __isl_take isl_union_pw_multi_aff *upma2)
6272 return bin_op(upma1, upma2, &flat_range_product_entry);
6275 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6276 * The parameters are assumed to have been aligned.
6278 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6279 * except that it works on two different isl_pw_* types.
6281 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6282 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6283 __isl_take isl_pw_aff *pa)
6285 int i, j, n;
6286 isl_pw_multi_aff *res = NULL;
6288 if (!pma || !pa)
6289 goto error;
6291 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6292 pa->dim, isl_dim_in))
6293 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6294 "domains don't match", goto error);
6295 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6296 goto error;
6298 n = pma->n * pa->n;
6299 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6301 for (i = 0; i < pma->n; ++i) {
6302 for (j = 0; j < pa->n; ++j) {
6303 isl_set *common;
6304 isl_multi_aff *res_ij;
6305 int empty;
6307 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6308 isl_set_copy(pa->p[j].set));
6309 empty = isl_set_plain_is_empty(common);
6310 if (empty < 0 || empty) {
6311 isl_set_free(common);
6312 if (empty < 0)
6313 goto error;
6314 continue;
6317 res_ij = isl_multi_aff_set_aff(
6318 isl_multi_aff_copy(pma->p[i].maff), pos,
6319 isl_aff_copy(pa->p[j].aff));
6320 res_ij = isl_multi_aff_gist(res_ij,
6321 isl_set_copy(common));
6323 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6327 isl_pw_multi_aff_free(pma);
6328 isl_pw_aff_free(pa);
6329 return res;
6330 error:
6331 isl_pw_multi_aff_free(pma);
6332 isl_pw_aff_free(pa);
6333 return isl_pw_multi_aff_free(res);
6336 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6338 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6339 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6340 __isl_take isl_pw_aff *pa)
6342 isl_bool equal_params;
6344 if (!pma || !pa)
6345 goto error;
6346 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6347 if (equal_params < 0)
6348 goto error;
6349 if (equal_params)
6350 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6351 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6352 isl_pw_aff_check_named_params(pa) < 0)
6353 goto error;
6354 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6355 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6356 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6357 error:
6358 isl_pw_multi_aff_free(pma);
6359 isl_pw_aff_free(pa);
6360 return NULL;
6363 /* Do the parameters of "pa" match those of "space"?
6365 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6366 __isl_keep isl_space *space)
6368 isl_space *pa_space;
6369 isl_bool match;
6371 if (!pa || !space)
6372 return isl_bool_error;
6374 pa_space = isl_pw_aff_get_space(pa);
6376 match = isl_space_has_equal_params(space, pa_space);
6378 isl_space_free(pa_space);
6379 return match;
6382 /* Check that the domain space of "pa" matches "space".
6384 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6385 __isl_keep isl_space *space)
6387 isl_space *pa_space;
6388 isl_bool match;
6390 if (!pa || !space)
6391 return isl_stat_error;
6393 pa_space = isl_pw_aff_get_space(pa);
6395 match = isl_space_has_equal_params(space, pa_space);
6396 if (match < 0)
6397 goto error;
6398 if (!match)
6399 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6400 "parameters don't match", goto error);
6401 match = isl_space_tuple_is_equal(space, isl_dim_in,
6402 pa_space, isl_dim_in);
6403 if (match < 0)
6404 goto error;
6405 if (!match)
6406 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6407 "domains don't match", goto error);
6408 isl_space_free(pa_space);
6409 return isl_stat_ok;
6410 error:
6411 isl_space_free(pa_space);
6412 return isl_stat_error;
6415 #undef BASE
6416 #define BASE pw_aff
6417 #undef DOMBASE
6418 #define DOMBASE set
6420 #include <isl_multi_explicit_domain.c>
6421 #include <isl_multi_pw_aff_explicit_domain.c>
6422 #include <isl_multi_templ.c>
6423 #include <isl_multi_apply_set.c>
6424 #include <isl_multi_arith_templ.c>
6425 #include <isl_multi_bind_templ.c>
6426 #include <isl_multi_bind_domain_templ.c>
6427 #include <isl_multi_coalesce.c>
6428 #include <isl_multi_domain_templ.c>
6429 #include <isl_multi_dim_id_templ.c>
6430 #include <isl_multi_dims.c>
6431 #include <isl_multi_from_base_templ.c>
6432 #include <isl_multi_gist.c>
6433 #include <isl_multi_hash.c>
6434 #include <isl_multi_identity_templ.c>
6435 #include <isl_multi_align_set.c>
6436 #include <isl_multi_intersect.c>
6437 #include <isl_multi_move_dims_templ.c>
6438 #include <isl_multi_nan_templ.c>
6439 #include <isl_multi_param_templ.c>
6440 #include <isl_multi_product_templ.c>
6441 #include <isl_multi_splice_templ.c>
6442 #include <isl_multi_tuple_id_templ.c>
6443 #include <isl_multi_zero_templ.c>
6445 /* Does "mpa" have a non-trivial explicit domain?
6447 * The explicit domain, if present, is trivial if it represents
6448 * an (obviously) universe set.
6450 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6451 __isl_keep isl_multi_pw_aff *mpa)
6453 if (!mpa)
6454 return isl_bool_error;
6455 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6456 return isl_bool_false;
6457 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6460 /* Scale the elements of "pma" by the corresponding elements of "mv".
6462 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6463 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6465 int i;
6466 isl_bool equal_params;
6468 pma = isl_pw_multi_aff_cow(pma);
6469 if (!pma || !mv)
6470 goto error;
6471 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6472 mv->space, isl_dim_set))
6473 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6474 "spaces don't match", goto error);
6475 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6476 if (equal_params < 0)
6477 goto error;
6478 if (!equal_params) {
6479 pma = isl_pw_multi_aff_align_params(pma,
6480 isl_multi_val_get_space(mv));
6481 mv = isl_multi_val_align_params(mv,
6482 isl_pw_multi_aff_get_space(pma));
6483 if (!pma || !mv)
6484 goto error;
6487 for (i = 0; i < pma->n; ++i) {
6488 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6489 isl_multi_val_copy(mv));
6490 if (!pma->p[i].maff)
6491 goto error;
6494 isl_multi_val_free(mv);
6495 return pma;
6496 error:
6497 isl_multi_val_free(mv);
6498 isl_pw_multi_aff_free(pma);
6499 return NULL;
6502 /* This function is called for each entry of an isl_union_pw_multi_aff.
6503 * If the space of the entry matches that of data->mv,
6504 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6505 * Otherwise, return an empty isl_pw_multi_aff.
6507 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6508 __isl_take isl_pw_multi_aff *pma, void *user)
6510 isl_multi_val *mv = user;
6512 if (!pma)
6513 return NULL;
6514 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6515 mv->space, isl_dim_set)) {
6516 isl_space *space = isl_pw_multi_aff_get_space(pma);
6517 isl_pw_multi_aff_free(pma);
6518 return isl_pw_multi_aff_empty(space);
6521 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6524 /* Scale the elements of "upma" by the corresponding elements of "mv",
6525 * for those entries that match the space of "mv".
6527 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6528 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6530 struct isl_union_pw_multi_aff_transform_control control = {
6531 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6532 .fn_user = mv,
6535 upma = isl_union_pw_multi_aff_align_params(upma,
6536 isl_multi_val_get_space(mv));
6537 mv = isl_multi_val_align_params(mv,
6538 isl_union_pw_multi_aff_get_space(upma));
6539 if (!upma || !mv)
6540 goto error;
6542 return isl_union_pw_multi_aff_transform(upma, &control);
6544 isl_multi_val_free(mv);
6545 return upma;
6546 error:
6547 isl_multi_val_free(mv);
6548 isl_union_pw_multi_aff_free(upma);
6549 return NULL;
6552 /* Construct and return a piecewise multi affine expression
6553 * in the given space with value zero in each of the output dimensions and
6554 * a universe domain.
6556 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6558 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6561 /* Construct and return a piecewise multi affine expression
6562 * that is equal to the given piecewise affine expression.
6564 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6565 __isl_take isl_pw_aff *pa)
6567 int i;
6568 isl_space *space;
6569 isl_pw_multi_aff *pma;
6571 if (!pa)
6572 return NULL;
6574 space = isl_pw_aff_get_space(pa);
6575 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6577 for (i = 0; i < pa->n; ++i) {
6578 isl_set *set;
6579 isl_multi_aff *ma;
6581 set = isl_set_copy(pa->p[i].set);
6582 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6583 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6586 isl_pw_aff_free(pa);
6587 return pma;
6590 /* Construct and return a piecewise multi affine expression
6591 * that is equal to the given multi piecewise affine expression
6592 * on the shared domain of the piecewise affine expressions,
6593 * in the special case of a 0D multi piecewise affine expression.
6595 * Create a piecewise multi affine expression with the explicit domain of
6596 * the 0D multi piecewise affine expression as domain.
6598 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6599 __isl_take isl_multi_pw_aff *mpa)
6601 isl_space *space;
6602 isl_set *dom;
6603 isl_multi_aff *ma;
6605 space = isl_multi_pw_aff_get_space(mpa);
6606 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6607 isl_multi_pw_aff_free(mpa);
6609 ma = isl_multi_aff_zero(space);
6610 return isl_pw_multi_aff_alloc(dom, ma);
6613 /* Construct and return a piecewise multi affine expression
6614 * that is equal to the given multi piecewise affine expression
6615 * on the shared domain of the piecewise affine expressions.
6617 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6618 __isl_take isl_multi_pw_aff *mpa)
6620 int i;
6621 isl_space *space;
6622 isl_pw_aff *pa;
6623 isl_pw_multi_aff *pma;
6625 if (!mpa)
6626 return NULL;
6628 if (mpa->n == 0)
6629 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6631 space = isl_multi_pw_aff_get_space(mpa);
6632 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6633 pma = isl_pw_multi_aff_from_pw_aff(pa);
6635 for (i = 1; i < mpa->n; ++i) {
6636 isl_pw_multi_aff *pma_i;
6638 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6639 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6640 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6643 pma = isl_pw_multi_aff_reset_space(pma, space);
6645 isl_multi_pw_aff_free(mpa);
6646 return pma;
6649 /* Construct and return a multi piecewise affine expression
6650 * that is equal to the given multi affine expression.
6652 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6653 __isl_take isl_multi_aff *ma)
6655 int i;
6656 isl_size n;
6657 isl_multi_pw_aff *mpa;
6659 n = isl_multi_aff_dim(ma, isl_dim_out);
6660 if (n < 0)
6661 ma = isl_multi_aff_free(ma);
6662 if (!ma)
6663 return NULL;
6665 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6667 for (i = 0; i < n; ++i) {
6668 isl_pw_aff *pa;
6670 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6671 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6674 isl_multi_aff_free(ma);
6675 return mpa;
6678 /* Construct and return a multi piecewise affine expression
6679 * that is equal to the given piecewise multi affine expression.
6681 * If the resulting multi piecewise affine expression has
6682 * an explicit domain, then assign it the domain of the input.
6683 * In other cases, the domain is stored in the individual elements.
6685 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6686 __isl_take isl_pw_multi_aff *pma)
6688 int i;
6689 isl_size n;
6690 isl_space *space;
6691 isl_multi_pw_aff *mpa;
6693 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6694 if (n < 0)
6695 pma = isl_pw_multi_aff_free(pma);
6696 space = isl_pw_multi_aff_get_space(pma);
6697 mpa = isl_multi_pw_aff_alloc(space);
6699 for (i = 0; i < n; ++i) {
6700 isl_pw_aff *pa;
6702 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6703 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6705 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6706 isl_set *dom;
6708 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6709 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6712 isl_pw_multi_aff_free(pma);
6713 return mpa;
6716 /* Do "pa1" and "pa2" represent the same function?
6718 * We first check if they are obviously equal.
6719 * If not, we convert them to maps and check if those are equal.
6721 * If "pa1" or "pa2" contain any NaNs, then they are considered
6722 * not to be the same. A NaN is not equal to anything, not even
6723 * to another NaN.
6725 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6726 __isl_keep isl_pw_aff *pa2)
6728 isl_bool equal;
6729 isl_bool has_nan;
6730 isl_map *map1, *map2;
6732 if (!pa1 || !pa2)
6733 return isl_bool_error;
6735 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6736 if (equal < 0 || equal)
6737 return equal;
6738 has_nan = either_involves_nan(pa1, pa2);
6739 if (has_nan < 0)
6740 return isl_bool_error;
6741 if (has_nan)
6742 return isl_bool_false;
6744 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6745 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6746 equal = isl_map_is_equal(map1, map2);
6747 isl_map_free(map1);
6748 isl_map_free(map2);
6750 return equal;
6753 /* Do "mpa1" and "mpa2" represent the same function?
6755 * Note that we cannot convert the entire isl_multi_pw_aff
6756 * to a map because the domains of the piecewise affine expressions
6757 * may not be the same.
6759 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6760 __isl_keep isl_multi_pw_aff *mpa2)
6762 int i;
6763 isl_bool equal, equal_params;
6765 if (!mpa1 || !mpa2)
6766 return isl_bool_error;
6768 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6769 if (equal_params < 0)
6770 return isl_bool_error;
6771 if (!equal_params) {
6772 if (!isl_space_has_named_params(mpa1->space))
6773 return isl_bool_false;
6774 if (!isl_space_has_named_params(mpa2->space))
6775 return isl_bool_false;
6776 mpa1 = isl_multi_pw_aff_copy(mpa1);
6777 mpa2 = isl_multi_pw_aff_copy(mpa2);
6778 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6779 isl_multi_pw_aff_get_space(mpa2));
6780 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6781 isl_multi_pw_aff_get_space(mpa1));
6782 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6783 isl_multi_pw_aff_free(mpa1);
6784 isl_multi_pw_aff_free(mpa2);
6785 return equal;
6788 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6789 if (equal < 0 || !equal)
6790 return equal;
6792 for (i = 0; i < mpa1->n; ++i) {
6793 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6794 if (equal < 0 || !equal)
6795 return equal;
6798 return isl_bool_true;
6801 /* Do "pma1" and "pma2" represent the same function?
6803 * First check if they are obviously equal.
6804 * If not, then convert them to maps and check if those are equal.
6806 * If "pa1" or "pa2" contain any NaNs, then they are considered
6807 * not to be the same. A NaN is not equal to anything, not even
6808 * to another NaN.
6810 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6811 __isl_keep isl_pw_multi_aff *pma2)
6813 isl_bool equal;
6814 isl_bool has_nan;
6815 isl_map *map1, *map2;
6817 if (!pma1 || !pma2)
6818 return isl_bool_error;
6820 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6821 if (equal < 0 || equal)
6822 return equal;
6823 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6824 if (has_nan >= 0 && !has_nan)
6825 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6826 if (has_nan < 0 || has_nan)
6827 return isl_bool_not(has_nan);
6829 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6830 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6831 equal = isl_map_is_equal(map1, map2);
6832 isl_map_free(map1);
6833 isl_map_free(map2);
6835 return equal;
6838 /* Compute the pullback of "mpa" by the function represented by "ma".
6839 * In other words, plug in "ma" in "mpa".
6841 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6843 * If "mpa" has an explicit domain, then it is this domain
6844 * that needs to undergo a pullback, i.e., a preimage.
6846 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6847 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6849 int i;
6850 isl_space *space = NULL;
6852 mpa = isl_multi_pw_aff_cow(mpa);
6853 if (!mpa || !ma)
6854 goto error;
6856 space = isl_space_join(isl_multi_aff_get_space(ma),
6857 isl_multi_pw_aff_get_space(mpa));
6858 if (!space)
6859 goto error;
6861 for (i = 0; i < mpa->n; ++i) {
6862 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6863 isl_multi_aff_copy(ma));
6864 if (!mpa->u.p[i])
6865 goto error;
6867 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6868 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6869 isl_multi_aff_copy(ma));
6870 if (!mpa->u.dom)
6871 goto error;
6874 isl_multi_aff_free(ma);
6875 isl_space_free(mpa->space);
6876 mpa->space = space;
6877 return mpa;
6878 error:
6879 isl_space_free(space);
6880 isl_multi_pw_aff_free(mpa);
6881 isl_multi_aff_free(ma);
6882 return NULL;
6885 /* Compute the pullback of "mpa" by the function represented by "ma".
6886 * In other words, plug in "ma" in "mpa".
6888 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6889 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6891 isl_bool equal_params;
6893 if (!mpa || !ma)
6894 goto error;
6895 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6896 if (equal_params < 0)
6897 goto error;
6898 if (equal_params)
6899 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6900 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6901 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6902 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6903 error:
6904 isl_multi_pw_aff_free(mpa);
6905 isl_multi_aff_free(ma);
6906 return NULL;
6909 /* Compute the pullback of "mpa" by the function represented by "pma".
6910 * In other words, plug in "pma" in "mpa".
6912 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6914 * If "mpa" has an explicit domain, then it is this domain
6915 * that needs to undergo a pullback, i.e., a preimage.
6917 static __isl_give isl_multi_pw_aff *
6918 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6919 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6921 int i;
6922 isl_space *space = NULL;
6924 mpa = isl_multi_pw_aff_cow(mpa);
6925 if (!mpa || !pma)
6926 goto error;
6928 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6929 isl_multi_pw_aff_get_space(mpa));
6931 for (i = 0; i < mpa->n; ++i) {
6932 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6933 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6934 if (!mpa->u.p[i])
6935 goto error;
6937 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6938 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6939 isl_pw_multi_aff_copy(pma));
6940 if (!mpa->u.dom)
6941 goto error;
6944 isl_pw_multi_aff_free(pma);
6945 isl_space_free(mpa->space);
6946 mpa->space = space;
6947 return mpa;
6948 error:
6949 isl_space_free(space);
6950 isl_multi_pw_aff_free(mpa);
6951 isl_pw_multi_aff_free(pma);
6952 return NULL;
6955 /* Compute the pullback of "mpa" by the function represented by "pma".
6956 * In other words, plug in "pma" in "mpa".
6958 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6959 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6961 isl_bool equal_params;
6963 if (!mpa || !pma)
6964 goto error;
6965 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6966 if (equal_params < 0)
6967 goto error;
6968 if (equal_params)
6969 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6970 mpa = isl_multi_pw_aff_align_params(mpa,
6971 isl_pw_multi_aff_get_space(pma));
6972 pma = isl_pw_multi_aff_align_params(pma,
6973 isl_multi_pw_aff_get_space(mpa));
6974 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6975 error:
6976 isl_multi_pw_aff_free(mpa);
6977 isl_pw_multi_aff_free(pma);
6978 return NULL;
6981 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6982 * with the domain of "aff". The domain of the result is the same
6983 * as that of "mpa".
6984 * "mpa" and "aff" are assumed to have been aligned.
6986 * We first extract the parametric constant from "aff", defined
6987 * over the correct domain.
6988 * Then we add the appropriate combinations of the members of "mpa".
6989 * Finally, we add the integer divisions through recursive calls.
6991 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6992 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6994 int i;
6995 isl_size n_in, n_div, n_mpa_in;
6996 isl_space *space;
6997 isl_val *v;
6998 isl_pw_aff *pa;
6999 isl_aff *tmp;
7001 n_in = isl_aff_dim(aff, isl_dim_in);
7002 n_div = isl_aff_dim(aff, isl_dim_div);
7003 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7004 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7005 goto error;
7007 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7008 tmp = isl_aff_copy(aff);
7009 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7010 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7011 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7012 tmp = isl_aff_reset_domain_space(tmp, space);
7013 pa = isl_pw_aff_from_aff(tmp);
7015 for (i = 0; i < n_in; ++i) {
7016 isl_pw_aff *pa_i;
7018 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7019 continue;
7020 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7021 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7022 pa_i = isl_pw_aff_scale_val(pa_i, v);
7023 pa = isl_pw_aff_add(pa, pa_i);
7026 for (i = 0; i < n_div; ++i) {
7027 isl_aff *div;
7028 isl_pw_aff *pa_i;
7030 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7031 continue;
7032 div = isl_aff_get_div(aff, i);
7033 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7034 isl_multi_pw_aff_copy(mpa), div);
7035 pa_i = isl_pw_aff_floor(pa_i);
7036 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7037 pa_i = isl_pw_aff_scale_val(pa_i, v);
7038 pa = isl_pw_aff_add(pa, pa_i);
7041 isl_multi_pw_aff_free(mpa);
7042 isl_aff_free(aff);
7044 return pa;
7045 error:
7046 isl_multi_pw_aff_free(mpa);
7047 isl_aff_free(aff);
7048 return NULL;
7051 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7052 * with the domain of "aff". The domain of the result is the same
7053 * as that of "mpa".
7055 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7056 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7058 isl_bool equal_params;
7060 if (!aff || !mpa)
7061 goto error;
7062 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7063 if (equal_params < 0)
7064 goto error;
7065 if (equal_params)
7066 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7068 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7069 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7071 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7072 error:
7073 isl_aff_free(aff);
7074 isl_multi_pw_aff_free(mpa);
7075 return NULL;
7078 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7079 * with the domain of "pa". The domain of the result is the same
7080 * as that of "mpa".
7081 * "mpa" and "pa" are assumed to have been aligned.
7083 * We consider each piece in turn. Note that the domains of the
7084 * pieces are assumed to be disjoint and they remain disjoint
7085 * after taking the preimage (over the same function).
7087 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7088 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7090 isl_space *space;
7091 isl_pw_aff *res;
7092 int i;
7094 if (!mpa || !pa)
7095 goto error;
7097 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7098 isl_pw_aff_get_space(pa));
7099 res = isl_pw_aff_empty(space);
7101 for (i = 0; i < pa->n; ++i) {
7102 isl_pw_aff *pa_i;
7103 isl_set *domain;
7105 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7106 isl_multi_pw_aff_copy(mpa),
7107 isl_aff_copy(pa->p[i].aff));
7108 domain = isl_set_copy(pa->p[i].set);
7109 domain = isl_set_preimage_multi_pw_aff(domain,
7110 isl_multi_pw_aff_copy(mpa));
7111 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7112 res = isl_pw_aff_add_disjoint(res, pa_i);
7115 isl_pw_aff_free(pa);
7116 isl_multi_pw_aff_free(mpa);
7117 return res;
7118 error:
7119 isl_pw_aff_free(pa);
7120 isl_multi_pw_aff_free(mpa);
7121 return NULL;
7124 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7125 * with the domain of "pa". The domain of the result is the same
7126 * as that of "mpa".
7128 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7129 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7131 isl_bool equal_params;
7133 if (!pa || !mpa)
7134 goto error;
7135 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7136 if (equal_params < 0)
7137 goto error;
7138 if (equal_params)
7139 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7141 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7142 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7144 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7145 error:
7146 isl_pw_aff_free(pa);
7147 isl_multi_pw_aff_free(mpa);
7148 return NULL;
7151 /* Compute the pullback of "pa" by the function represented by "mpa".
7152 * In other words, plug in "mpa" in "pa".
7153 * "pa" and "mpa" are assumed to have been aligned.
7155 * The pullback is computed by applying "pa" to "mpa".
7157 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7158 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7160 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7163 /* Compute the pullback of "pa" by the function represented by "mpa".
7164 * In other words, plug in "mpa" in "pa".
7166 * The pullback is computed by applying "pa" to "mpa".
7168 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7169 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7171 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7174 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7175 * In other words, plug in "mpa2" in "mpa1".
7177 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7179 * We pullback each member of "mpa1" in turn.
7181 * If "mpa1" has an explicit domain, then it is this domain
7182 * that needs to undergo a pullback instead, i.e., a preimage.
7184 static __isl_give isl_multi_pw_aff *
7185 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7186 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7188 int i;
7189 isl_space *space = NULL;
7191 mpa1 = isl_multi_pw_aff_cow(mpa1);
7192 if (!mpa1 || !mpa2)
7193 goto error;
7195 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7196 isl_multi_pw_aff_get_space(mpa1));
7198 for (i = 0; i < mpa1->n; ++i) {
7199 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7200 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7201 if (!mpa1->u.p[i])
7202 goto error;
7205 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7206 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7207 isl_multi_pw_aff_copy(mpa2));
7208 if (!mpa1->u.dom)
7209 goto error;
7211 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7213 isl_multi_pw_aff_free(mpa2);
7214 return mpa1;
7215 error:
7216 isl_space_free(space);
7217 isl_multi_pw_aff_free(mpa1);
7218 isl_multi_pw_aff_free(mpa2);
7219 return NULL;
7222 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7223 * In other words, plug in "mpa2" in "mpa1".
7225 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7226 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7228 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7229 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7232 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7233 * of "mpa1" and "mpa2" live in the same space, construct map space
7234 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7235 * with this map space as extract argument.
7237 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7238 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7239 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7240 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7242 int match;
7243 isl_space *space1, *space2;
7244 isl_map *res;
7246 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7247 isl_multi_pw_aff_get_space(mpa2));
7248 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7249 isl_multi_pw_aff_get_space(mpa1));
7250 if (!mpa1 || !mpa2)
7251 goto error;
7252 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7253 mpa2->space, isl_dim_out);
7254 if (match < 0)
7255 goto error;
7256 if (!match)
7257 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7258 "range spaces don't match", goto error);
7259 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7260 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7261 space1 = isl_space_map_from_domain_and_range(space1, space2);
7263 res = order(mpa1, mpa2, space1);
7264 isl_multi_pw_aff_free(mpa1);
7265 isl_multi_pw_aff_free(mpa2);
7266 return res;
7267 error:
7268 isl_multi_pw_aff_free(mpa1);
7269 isl_multi_pw_aff_free(mpa2);
7270 return NULL;
7273 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7274 * where the function values are equal. "space" is the space of the result.
7275 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7277 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7278 * in the sequences are equal.
7280 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7281 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7282 __isl_take isl_space *space)
7284 int i;
7285 isl_size n;
7286 isl_map *res;
7288 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7289 if (n < 0)
7290 space = isl_space_free(space);
7291 res = isl_map_universe(space);
7293 for (i = 0; i < n; ++i) {
7294 isl_pw_aff *pa1, *pa2;
7295 isl_map *map;
7297 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7298 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7299 map = isl_pw_aff_eq_map(pa1, pa2);
7300 res = isl_map_intersect(res, map);
7303 return res;
7306 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7307 * where the function values are equal.
7309 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7310 __isl_take isl_multi_pw_aff *mpa2)
7312 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7313 &isl_multi_pw_aff_eq_map_on_space);
7316 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7317 * where the function values of "mpa1" lexicographically satisfies "base"
7318 * compared to that of "mpa2". "space" is the space of the result.
7319 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7321 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7322 * if its i-th element satisfies "base" when compared to
7323 * the i-th element of "mpa2" while all previous elements are
7324 * pairwise equal.
7326 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7327 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7328 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7329 __isl_take isl_pw_aff *pa2),
7330 __isl_take isl_space *space)
7332 int i;
7333 isl_size n;
7334 isl_map *res, *rest;
7336 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7337 if (n < 0)
7338 space = isl_space_free(space);
7339 res = isl_map_empty(isl_space_copy(space));
7340 rest = isl_map_universe(space);
7342 for (i = 0; i < n; ++i) {
7343 isl_pw_aff *pa1, *pa2;
7344 isl_map *map;
7346 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7347 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7348 map = base(pa1, pa2);
7349 map = isl_map_intersect(map, isl_map_copy(rest));
7350 res = isl_map_union(res, map);
7352 if (i == n - 1)
7353 continue;
7355 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7356 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7357 map = isl_pw_aff_eq_map(pa1, pa2);
7358 rest = isl_map_intersect(rest, map);
7361 isl_map_free(rest);
7362 return res;
7365 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7366 * where the function value of "mpa1" is lexicographically less than that
7367 * of "mpa2". "space" is the space of the result.
7368 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7370 * "mpa1" is less than "mpa2" if its i-th element is smaller
7371 * than the i-th element of "mpa2" while all previous elements are
7372 * pairwise equal.
7374 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7375 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7376 __isl_take isl_space *space)
7378 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7379 &isl_pw_aff_lt_map, space);
7382 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7383 * where the function value of "mpa1" is lexicographically less than that
7384 * of "mpa2".
7386 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7387 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7389 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7390 &isl_multi_pw_aff_lex_lt_map_on_space);
7393 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7394 * where the function value of "mpa1" is lexicographically greater than that
7395 * of "mpa2". "space" is the space of the result.
7396 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7398 * "mpa1" is greater than "mpa2" if its i-th element is greater
7399 * than the i-th element of "mpa2" while all previous elements are
7400 * pairwise equal.
7402 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7403 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7404 __isl_take isl_space *space)
7406 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7407 &isl_pw_aff_gt_map, space);
7410 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7411 * where the function value of "mpa1" is lexicographically greater than that
7412 * of "mpa2".
7414 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7415 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7417 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7418 &isl_multi_pw_aff_lex_gt_map_on_space);
7421 /* Compare two isl_affs.
7423 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7424 * than "aff2" and 0 if they are equal.
7426 * The order is fairly arbitrary. We do consider expressions that only involve
7427 * earlier dimensions as "smaller".
7429 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7431 int cmp;
7432 int last1, last2;
7434 if (aff1 == aff2)
7435 return 0;
7437 if (!aff1)
7438 return -1;
7439 if (!aff2)
7440 return 1;
7442 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7443 if (cmp != 0)
7444 return cmp;
7446 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7447 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7448 if (last1 != last2)
7449 return last1 - last2;
7451 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7454 /* Compare two isl_pw_affs.
7456 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7457 * than "pa2" and 0 if they are equal.
7459 * The order is fairly arbitrary. We do consider expressions that only involve
7460 * earlier dimensions as "smaller".
7462 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7463 __isl_keep isl_pw_aff *pa2)
7465 int i;
7466 int cmp;
7468 if (pa1 == pa2)
7469 return 0;
7471 if (!pa1)
7472 return -1;
7473 if (!pa2)
7474 return 1;
7476 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7477 if (cmp != 0)
7478 return cmp;
7480 if (pa1->n != pa2->n)
7481 return pa1->n - pa2->n;
7483 for (i = 0; i < pa1->n; ++i) {
7484 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7485 if (cmp != 0)
7486 return cmp;
7487 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7488 if (cmp != 0)
7489 return cmp;
7492 return 0;
7495 /* Return a piecewise affine expression that is equal to "v" on "domain".
7497 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7498 __isl_take isl_val *v)
7500 isl_space *space;
7501 isl_local_space *ls;
7502 isl_aff *aff;
7504 space = isl_set_get_space(domain);
7505 ls = isl_local_space_from_space(space);
7506 aff = isl_aff_val_on_domain(ls, v);
7508 return isl_pw_aff_alloc(domain, aff);
7511 /* Return a piecewise affine expression that is equal to the parameter
7512 * with identifier "id" on "domain".
7514 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7515 __isl_take isl_set *domain, __isl_take isl_id *id)
7517 isl_space *space;
7518 isl_aff *aff;
7520 space = isl_set_get_space(domain);
7521 space = isl_space_add_param_id(space, isl_id_copy(id));
7522 domain = isl_set_align_params(domain, isl_space_copy(space));
7523 aff = isl_aff_param_on_domain_space_id(space, id);
7525 return isl_pw_aff_alloc(domain, aff);
7528 /* Return a multi affine expression that is equal to "mv" on domain
7529 * space "space".
7531 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7532 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7534 int i;
7535 isl_size n;
7536 isl_space *space2;
7537 isl_local_space *ls;
7538 isl_multi_aff *ma;
7540 n = isl_multi_val_dim(mv, isl_dim_set);
7541 if (!space || n < 0)
7542 goto error;
7544 space2 = isl_multi_val_get_space(mv);
7545 space2 = isl_space_align_params(space2, isl_space_copy(space));
7546 space = isl_space_align_params(space, isl_space_copy(space2));
7547 space = isl_space_map_from_domain_and_range(space, space2);
7548 ma = isl_multi_aff_alloc(isl_space_copy(space));
7549 ls = isl_local_space_from_space(isl_space_domain(space));
7550 for (i = 0; i < n; ++i) {
7551 isl_val *v;
7552 isl_aff *aff;
7554 v = isl_multi_val_get_val(mv, i);
7555 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7556 ma = isl_multi_aff_set_aff(ma, i, aff);
7558 isl_local_space_free(ls);
7560 isl_multi_val_free(mv);
7561 return ma;
7562 error:
7563 isl_space_free(space);
7564 isl_multi_val_free(mv);
7565 return NULL;
7568 /* Return a piecewise multi-affine expression
7569 * that is equal to "mv" on "domain".
7571 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7572 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7574 isl_space *space;
7575 isl_multi_aff *ma;
7577 space = isl_set_get_space(domain);
7578 ma = isl_multi_aff_multi_val_on_space(space, mv);
7580 return isl_pw_multi_aff_alloc(domain, ma);
7583 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7584 * mv is the value that should be attained on each domain set
7585 * res collects the results
7587 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7588 isl_multi_val *mv;
7589 isl_union_pw_multi_aff *res;
7592 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7593 * and add it to data->res.
7595 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7596 void *user)
7598 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7599 isl_pw_multi_aff *pma;
7600 isl_multi_val *mv;
7602 mv = isl_multi_val_copy(data->mv);
7603 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7604 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7606 return data->res ? isl_stat_ok : isl_stat_error;
7609 /* Return a union piecewise multi-affine expression
7610 * that is equal to "mv" on "domain".
7612 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7613 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7615 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7616 isl_space *space;
7618 space = isl_union_set_get_space(domain);
7619 data.res = isl_union_pw_multi_aff_empty(space);
7620 data.mv = mv;
7621 if (isl_union_set_foreach_set(domain,
7622 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7623 data.res = isl_union_pw_multi_aff_free(data.res);
7624 isl_union_set_free(domain);
7625 isl_multi_val_free(mv);
7626 return data.res;
7629 /* Compute the pullback of data->pma by the function represented by "pma2",
7630 * provided the spaces match, and add the results to data->res.
7632 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7634 struct isl_union_pw_multi_aff_bin_data *data = user;
7636 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7637 pma2->dim, isl_dim_out)) {
7638 isl_pw_multi_aff_free(pma2);
7639 return isl_stat_ok;
7642 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7643 isl_pw_multi_aff_copy(data->pma), pma2);
7645 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7646 if (!data->res)
7647 return isl_stat_error;
7649 return isl_stat_ok;
7652 /* Compute the pullback of "upma1" by the function represented by "upma2".
7654 __isl_give isl_union_pw_multi_aff *
7655 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7656 __isl_take isl_union_pw_multi_aff *upma1,
7657 __isl_take isl_union_pw_multi_aff *upma2)
7659 return bin_op(upma1, upma2, &pullback_entry);
7662 /* Check that the domain space of "upa" matches "space".
7664 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7665 * can in principle never fail since the space "space" is that
7666 * of the isl_multi_union_pw_aff and is a set space such that
7667 * there is no domain space to match.
7669 * We check the parameters and double-check that "space" is
7670 * indeed that of a set.
7672 static isl_stat isl_union_pw_aff_check_match_domain_space(
7673 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7675 isl_space *upa_space;
7676 isl_bool match;
7678 if (!upa || !space)
7679 return isl_stat_error;
7681 match = isl_space_is_set(space);
7682 if (match < 0)
7683 return isl_stat_error;
7684 if (!match)
7685 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7686 "expecting set space", return isl_stat_error);
7688 upa_space = isl_union_pw_aff_get_space(upa);
7689 match = isl_space_has_equal_params(space, upa_space);
7690 if (match < 0)
7691 goto error;
7692 if (!match)
7693 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7694 "parameters don't match", goto error);
7696 isl_space_free(upa_space);
7697 return isl_stat_ok;
7698 error:
7699 isl_space_free(upa_space);
7700 return isl_stat_error;
7703 /* Do the parameters of "upa" match those of "space"?
7705 static isl_bool isl_union_pw_aff_matching_params(
7706 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7708 isl_space *upa_space;
7709 isl_bool match;
7711 if (!upa || !space)
7712 return isl_bool_error;
7714 upa_space = isl_union_pw_aff_get_space(upa);
7716 match = isl_space_has_equal_params(space, upa_space);
7718 isl_space_free(upa_space);
7719 return match;
7722 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7723 * space represents the new parameters.
7724 * res collects the results.
7726 struct isl_union_pw_aff_reset_params_data {
7727 isl_space *space;
7728 isl_union_pw_aff *res;
7731 /* Replace the parameters of "pa" by data->space and
7732 * add the result to data->res.
7734 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7736 struct isl_union_pw_aff_reset_params_data *data = user;
7737 isl_space *space;
7739 space = isl_pw_aff_get_space(pa);
7740 space = isl_space_replace_params(space, data->space);
7741 pa = isl_pw_aff_reset_space(pa, space);
7742 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7744 return data->res ? isl_stat_ok : isl_stat_error;
7747 /* Replace the domain space of "upa" by "space".
7748 * Since a union expression does not have a (single) domain space,
7749 * "space" is necessarily a parameter space.
7751 * Since the order and the names of the parameters determine
7752 * the hash value, we need to create a new hash table.
7754 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7755 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7757 struct isl_union_pw_aff_reset_params_data data = { space };
7758 isl_bool match;
7760 match = isl_union_pw_aff_matching_params(upa, space);
7761 if (match < 0)
7762 upa = isl_union_pw_aff_free(upa);
7763 else if (match) {
7764 isl_space_free(space);
7765 return upa;
7768 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7769 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7770 data.res = isl_union_pw_aff_free(data.res);
7772 isl_union_pw_aff_free(upa);
7773 isl_space_free(space);
7774 return data.res;
7777 /* Return the floor of "pa".
7779 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7781 return isl_pw_aff_floor(pa);
7784 /* Given f, return floor(f).
7786 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7787 __isl_take isl_union_pw_aff *upa)
7789 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7792 /* Compute
7794 * upa mod m = upa - m * floor(upa/m)
7796 * with m an integer value.
7798 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7799 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7801 isl_union_pw_aff *res;
7803 if (!upa || !m)
7804 goto error;
7806 if (!isl_val_is_int(m))
7807 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7808 "expecting integer modulo", goto error);
7809 if (!isl_val_is_pos(m))
7810 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7811 "expecting positive modulo", goto error);
7813 res = isl_union_pw_aff_copy(upa);
7814 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7815 upa = isl_union_pw_aff_floor(upa);
7816 upa = isl_union_pw_aff_scale_val(upa, m);
7817 res = isl_union_pw_aff_sub(res, upa);
7819 return res;
7820 error:
7821 isl_val_free(m);
7822 isl_union_pw_aff_free(upa);
7823 return NULL;
7826 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7827 * pos is the output position that needs to be extracted.
7828 * res collects the results.
7830 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7831 int pos;
7832 isl_union_pw_aff *res;
7835 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7836 * (assuming it has such a dimension) and add it to data->res.
7838 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7840 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7841 isl_size n_out;
7842 isl_pw_aff *pa;
7844 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7845 if (n_out < 0)
7846 return isl_stat_error;
7847 if (data->pos >= n_out) {
7848 isl_pw_multi_aff_free(pma);
7849 return isl_stat_ok;
7852 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7853 isl_pw_multi_aff_free(pma);
7855 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7857 return data->res ? isl_stat_ok : isl_stat_error;
7860 /* Extract an isl_union_pw_aff corresponding to
7861 * output dimension "pos" of "upma".
7863 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7864 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7866 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7867 isl_space *space;
7869 if (!upma)
7870 return NULL;
7872 if (pos < 0)
7873 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7874 "cannot extract at negative position", return NULL);
7876 space = isl_union_pw_multi_aff_get_space(upma);
7877 data.res = isl_union_pw_aff_empty(space);
7878 data.pos = pos;
7879 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7880 &get_union_pw_aff, &data) < 0)
7881 data.res = isl_union_pw_aff_free(data.res);
7883 return data.res;
7886 /* Return a union piecewise affine expression
7887 * that is equal to "aff" on "domain".
7889 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7890 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7892 isl_pw_aff *pa;
7894 pa = isl_pw_aff_from_aff(aff);
7895 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7898 /* Return a union piecewise affine expression
7899 * that is equal to the parameter identified by "id" on "domain".
7901 * Make sure the parameter appears in the space passed to
7902 * isl_aff_param_on_domain_space_id.
7904 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7905 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7907 isl_space *space;
7908 isl_aff *aff;
7910 space = isl_union_set_get_space(domain);
7911 space = isl_space_add_param_id(space, isl_id_copy(id));
7912 aff = isl_aff_param_on_domain_space_id(space, id);
7913 return isl_union_pw_aff_aff_on_domain(domain, aff);
7916 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7917 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7918 * needs to attain.
7919 * "res" collects the results.
7921 struct isl_union_pw_aff_pw_aff_on_domain_data {
7922 isl_pw_aff *pa;
7923 isl_union_pw_aff *res;
7926 /* Construct a piecewise affine expression that is equal to data->pa
7927 * on "domain" and add the result to data->res.
7929 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7931 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7932 isl_pw_aff *pa;
7933 isl_size dim;
7935 pa = isl_pw_aff_copy(data->pa);
7936 dim = isl_set_dim(domain, isl_dim_set);
7937 if (dim < 0)
7938 pa = isl_pw_aff_free(pa);
7939 pa = isl_pw_aff_from_range(pa);
7940 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7941 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7942 pa = isl_pw_aff_intersect_domain(pa, domain);
7943 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7945 return data->res ? isl_stat_ok : isl_stat_error;
7948 /* Return a union piecewise affine expression
7949 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7950 * have been aligned.
7952 * Construct an isl_pw_aff on each of the sets in "domain" and
7953 * collect the results.
7955 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7956 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7958 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7959 isl_space *space;
7961 space = isl_union_set_get_space(domain);
7962 data.res = isl_union_pw_aff_empty(space);
7963 data.pa = pa;
7964 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7965 data.res = isl_union_pw_aff_free(data.res);
7966 isl_union_set_free(domain);
7967 isl_pw_aff_free(pa);
7968 return data.res;
7971 /* Return a union piecewise affine expression
7972 * that is equal to "pa" on "domain".
7974 * Check that "pa" is a parametric expression,
7975 * align the parameters if needed and call
7976 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7978 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7979 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7981 isl_bool is_set;
7982 isl_bool equal_params;
7983 isl_space *domain_space, *pa_space;
7985 pa_space = isl_pw_aff_peek_space(pa);
7986 is_set = isl_space_is_set(pa_space);
7987 if (is_set < 0)
7988 goto error;
7989 if (!is_set)
7990 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7991 "expecting parametric expression", goto error);
7993 domain_space = isl_union_set_get_space(domain);
7994 pa_space = isl_pw_aff_get_space(pa);
7995 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7996 if (equal_params >= 0 && !equal_params) {
7997 isl_space *space;
7999 space = isl_space_align_params(domain_space, pa_space);
8000 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8001 domain = isl_union_set_align_params(domain, space);
8002 } else {
8003 isl_space_free(domain_space);
8004 isl_space_free(pa_space);
8007 if (equal_params < 0)
8008 goto error;
8009 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8010 error:
8011 isl_union_set_free(domain);
8012 isl_pw_aff_free(pa);
8013 return NULL;
8016 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8017 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8018 * "res" collects the results.
8020 struct isl_union_pw_aff_val_on_domain_data {
8021 isl_val *v;
8022 isl_union_pw_aff *res;
8025 /* Construct a piecewise affine expression that is equal to data->v
8026 * on "domain" and add the result to data->res.
8028 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8030 struct isl_union_pw_aff_val_on_domain_data *data = user;
8031 isl_pw_aff *pa;
8032 isl_val *v;
8034 v = isl_val_copy(data->v);
8035 pa = isl_pw_aff_val_on_domain(domain, v);
8036 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8038 return data->res ? isl_stat_ok : isl_stat_error;
8041 /* Return a union piecewise affine expression
8042 * that is equal to "v" on "domain".
8044 * Construct an isl_pw_aff on each of the sets in "domain" and
8045 * collect the results.
8047 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8048 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8050 struct isl_union_pw_aff_val_on_domain_data data;
8051 isl_space *space;
8053 space = isl_union_set_get_space(domain);
8054 data.res = isl_union_pw_aff_empty(space);
8055 data.v = v;
8056 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8057 data.res = isl_union_pw_aff_free(data.res);
8058 isl_union_set_free(domain);
8059 isl_val_free(v);
8060 return data.res;
8063 /* Construct a piecewise multi affine expression
8064 * that is equal to "pa" and add it to upma.
8066 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8067 void *user)
8069 isl_union_pw_multi_aff **upma = user;
8070 isl_pw_multi_aff *pma;
8072 pma = isl_pw_multi_aff_from_pw_aff(pa);
8073 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8075 return *upma ? isl_stat_ok : isl_stat_error;
8078 /* Construct and return a union piecewise multi affine expression
8079 * that is equal to the given union piecewise affine expression.
8081 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8082 __isl_take isl_union_pw_aff *upa)
8084 isl_space *space;
8085 isl_union_pw_multi_aff *upma;
8087 if (!upa)
8088 return NULL;
8090 space = isl_union_pw_aff_get_space(upa);
8091 upma = isl_union_pw_multi_aff_empty(space);
8093 if (isl_union_pw_aff_foreach_pw_aff(upa,
8094 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8095 upma = isl_union_pw_multi_aff_free(upma);
8097 isl_union_pw_aff_free(upa);
8098 return upma;
8101 /* Compute the set of elements in the domain of "pa" where it is zero and
8102 * add this set to "uset".
8104 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8106 isl_union_set **uset = (isl_union_set **)user;
8108 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8110 return *uset ? isl_stat_ok : isl_stat_error;
8113 /* Return a union set containing those elements in the domain
8114 * of "upa" where it is zero.
8116 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8117 __isl_take isl_union_pw_aff *upa)
8119 isl_union_set *zero;
8121 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8122 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8123 zero = isl_union_set_free(zero);
8125 isl_union_pw_aff_free(upa);
8126 return zero;
8129 /* Internal data structure for isl_union_pw_aff_bind_id,
8130 * storing the parameter that needs to be bound and
8131 * the accumulated results.
8133 struct isl_bind_id_data {
8134 isl_id *id;
8135 isl_union_set *bound;
8138 /* Bind the piecewise affine function "pa" to the parameter data->id,
8139 * adding the resulting elements in the domain where the expression
8140 * is equal to the parameter to data->bound.
8142 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8144 struct isl_bind_id_data *data = user;
8145 isl_set *bound;
8147 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8148 data->bound = isl_union_set_add_set(data->bound, bound);
8150 return data->bound ? isl_stat_ok : isl_stat_error;
8153 /* Bind the union piecewise affine function "upa" to the parameter "id",
8154 * returning the elements in the domain where the expression
8155 * is equal to the parameter.
8157 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8158 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8160 struct isl_bind_id_data data = { id };
8162 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8163 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8164 data.bound = isl_union_set_free(data.bound);
8166 isl_union_pw_aff_free(upa);
8167 isl_id_free(id);
8168 return data.bound;
8171 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8172 * upma is the function that is plugged in.
8173 * pa is the current part of the function in which upma is plugged in.
8174 * res collects the results.
8176 struct isl_union_pw_aff_pullback_upma_data {
8177 isl_union_pw_multi_aff *upma;
8178 isl_pw_aff *pa;
8179 isl_union_pw_aff *res;
8182 /* Check if "pma" can be plugged into data->pa.
8183 * If so, perform the pullback and add the result to data->res.
8185 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8187 struct isl_union_pw_aff_pullback_upma_data *data = user;
8188 isl_pw_aff *pa;
8190 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8191 pma->dim, isl_dim_out)) {
8192 isl_pw_multi_aff_free(pma);
8193 return isl_stat_ok;
8196 pa = isl_pw_aff_copy(data->pa);
8197 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8199 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8201 return data->res ? isl_stat_ok : isl_stat_error;
8204 /* Check if any of the elements of data->upma can be plugged into pa,
8205 * add if so add the result to data->res.
8207 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8209 struct isl_union_pw_aff_pullback_upma_data *data = user;
8210 isl_stat r;
8212 data->pa = pa;
8213 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8214 &pa_pb_pma, data);
8215 isl_pw_aff_free(pa);
8217 return r;
8220 /* Compute the pullback of "upa" by the function represented by "upma".
8221 * In other words, plug in "upma" in "upa". The result contains
8222 * expressions defined over the domain space of "upma".
8224 * Run over all pairs of elements in "upa" and "upma", perform
8225 * the pullback when appropriate and collect the results.
8226 * If the hash value were based on the domain space rather than
8227 * the function space, then we could run through all elements
8228 * of "upma" and directly pick out the corresponding element of "upa".
8230 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8231 __isl_take isl_union_pw_aff *upa,
8232 __isl_take isl_union_pw_multi_aff *upma)
8234 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8235 isl_space *space;
8237 space = isl_union_pw_multi_aff_get_space(upma);
8238 upa = isl_union_pw_aff_align_params(upa, space);
8239 space = isl_union_pw_aff_get_space(upa);
8240 upma = isl_union_pw_multi_aff_align_params(upma, space);
8242 if (!upa || !upma)
8243 goto error;
8245 data.upma = upma;
8246 data.res = isl_union_pw_aff_alloc_same_size(upa);
8247 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8248 data.res = isl_union_pw_aff_free(data.res);
8250 isl_union_pw_aff_free(upa);
8251 isl_union_pw_multi_aff_free(upma);
8252 return data.res;
8253 error:
8254 isl_union_pw_aff_free(upa);
8255 isl_union_pw_multi_aff_free(upma);
8256 return NULL;
8259 #undef BASE
8260 #define BASE union_pw_aff
8261 #undef DOMBASE
8262 #define DOMBASE union_set
8264 #include <isl_multi_explicit_domain.c>
8265 #include <isl_multi_union_pw_aff_explicit_domain.c>
8266 #include <isl_multi_templ.c>
8267 #include <isl_multi_apply_set.c>
8268 #include <isl_multi_apply_union_set.c>
8269 #include <isl_multi_arith_templ.c>
8270 #include <isl_multi_bind_templ.c>
8271 #include <isl_multi_coalesce.c>
8272 #include <isl_multi_dim_id_templ.c>
8273 #include <isl_multi_floor.c>
8274 #include <isl_multi_from_base_templ.c>
8275 #include <isl_multi_gist.c>
8276 #include <isl_multi_align_set.c>
8277 #include <isl_multi_align_union_set.c>
8278 #include <isl_multi_intersect.c>
8279 #include <isl_multi_nan_templ.c>
8280 #include <isl_multi_tuple_id_templ.c>
8282 /* Does "mupa" have a non-trivial explicit domain?
8284 * The explicit domain, if present, is trivial if it represents
8285 * an (obviously) universe parameter set.
8287 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8288 __isl_keep isl_multi_union_pw_aff *mupa)
8290 isl_bool is_params, trivial;
8291 isl_set *set;
8293 if (!mupa)
8294 return isl_bool_error;
8295 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8296 return isl_bool_false;
8297 is_params = isl_union_set_is_params(mupa->u.dom);
8298 if (is_params < 0 || !is_params)
8299 return isl_bool_not(is_params);
8300 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8301 trivial = isl_set_plain_is_universe(set);
8302 isl_set_free(set);
8303 return isl_bool_not(trivial);
8306 /* Construct a multiple union piecewise affine expression
8307 * in the given space with value zero in each of the output dimensions.
8309 * Since there is no canonical zero value for
8310 * a union piecewise affine expression, we can only construct
8311 * a zero-dimensional "zero" value.
8313 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8314 __isl_take isl_space *space)
8316 isl_bool params;
8317 isl_size dim;
8319 if (!space)
8320 return NULL;
8322 params = isl_space_is_params(space);
8323 if (params < 0)
8324 goto error;
8325 if (params)
8326 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8327 "expecting proper set space", goto error);
8328 if (!isl_space_is_set(space))
8329 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8330 "expecting set space", goto error);
8331 dim = isl_space_dim(space, isl_dim_out);
8332 if (dim < 0)
8333 goto error;
8334 if (dim != 0)
8335 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8336 "expecting 0D space", goto error);
8338 return isl_multi_union_pw_aff_alloc(space);
8339 error:
8340 isl_space_free(space);
8341 return NULL;
8344 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8345 * with the actual sum on the shared domain and
8346 * the defined expression on the symmetric difference of the domains.
8348 * We simply iterate over the elements in both arguments and
8349 * call isl_union_pw_aff_union_add on each of them, if there is
8350 * at least one element.
8352 * Otherwise, the two expressions have an explicit domain and
8353 * the union of these explicit domains is computed.
8354 * This assumes that the explicit domains are either both in terms
8355 * of specific domains elements or both in terms of parameters.
8356 * However, if one of the expressions does not have any constraints
8357 * on its explicit domain, then this is allowed as well and the result
8358 * is the expression with no constraints on its explicit domain.
8360 static __isl_give isl_multi_union_pw_aff *
8361 isl_multi_union_pw_aff_union_add_aligned(
8362 __isl_take isl_multi_union_pw_aff *mupa1,
8363 __isl_take isl_multi_union_pw_aff *mupa2)
8365 isl_bool has_domain, is_params1, is_params2;
8367 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8368 goto error;
8369 if (mupa1->n > 0)
8370 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8371 &isl_union_pw_aff_union_add);
8372 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8373 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8374 goto error;
8376 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8377 if (has_domain < 0)
8378 goto error;
8379 if (!has_domain) {
8380 isl_multi_union_pw_aff_free(mupa2);
8381 return mupa1;
8383 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8384 if (has_domain < 0)
8385 goto error;
8386 if (!has_domain) {
8387 isl_multi_union_pw_aff_free(mupa1);
8388 return mupa2;
8391 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8392 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8393 if (is_params1 < 0 || is_params2 < 0)
8394 goto error;
8395 if (is_params1 != is_params2)
8396 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8397 isl_error_invalid,
8398 "cannot compute union of concrete domain and "
8399 "parameter constraints", goto error);
8400 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8401 if (!mupa1)
8402 goto error;
8403 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8404 isl_union_set_copy(mupa2->u.dom));
8405 if (!mupa1->u.dom)
8406 goto error;
8407 isl_multi_union_pw_aff_free(mupa2);
8408 return mupa1;
8409 error:
8410 isl_multi_union_pw_aff_free(mupa1);
8411 isl_multi_union_pw_aff_free(mupa2);
8412 return NULL;
8415 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8416 * with the actual sum on the shared domain and
8417 * the defined expression on the symmetric difference of the domains.
8419 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8420 __isl_take isl_multi_union_pw_aff *mupa1,
8421 __isl_take isl_multi_union_pw_aff *mupa2)
8423 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8424 &isl_multi_union_pw_aff_union_add_aligned);
8427 /* Construct and return a multi union piecewise affine expression
8428 * that is equal to the given multi affine expression.
8430 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8431 __isl_take isl_multi_aff *ma)
8433 isl_multi_pw_aff *mpa;
8435 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8436 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8439 /* Construct and return a multi union piecewise affine expression
8440 * that is equal to the given multi piecewise affine expression.
8442 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8443 __isl_take isl_multi_pw_aff *mpa)
8445 int i;
8446 isl_size n;
8447 isl_space *space;
8448 isl_multi_union_pw_aff *mupa;
8450 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8451 if (n < 0)
8452 mpa = isl_multi_pw_aff_free(mpa);
8453 if (!mpa)
8454 return NULL;
8456 space = isl_multi_pw_aff_get_space(mpa);
8457 space = isl_space_range(space);
8458 mupa = isl_multi_union_pw_aff_alloc(space);
8460 for (i = 0; i < n; ++i) {
8461 isl_pw_aff *pa;
8462 isl_union_pw_aff *upa;
8464 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8465 upa = isl_union_pw_aff_from_pw_aff(pa);
8466 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8469 isl_multi_pw_aff_free(mpa);
8471 return mupa;
8474 /* Extract the range space of "pma" and assign it to *space.
8475 * If *space has already been set (through a previous call to this function),
8476 * then check that the range space is the same.
8478 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8480 isl_space **space = user;
8481 isl_space *pma_space;
8482 isl_bool equal;
8484 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8485 isl_pw_multi_aff_free(pma);
8487 if (!pma_space)
8488 return isl_stat_error;
8489 if (!*space) {
8490 *space = pma_space;
8491 return isl_stat_ok;
8494 equal = isl_space_is_equal(pma_space, *space);
8495 isl_space_free(pma_space);
8497 if (equal < 0)
8498 return isl_stat_error;
8499 if (!equal)
8500 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8501 "range spaces not the same", return isl_stat_error);
8502 return isl_stat_ok;
8505 /* Construct and return a multi union piecewise affine expression
8506 * that is equal to the given union piecewise multi affine expression.
8508 * In order to be able to perform the conversion, the input
8509 * needs to be non-empty and may only involve a single range space.
8511 * If the resulting multi union piecewise affine expression has
8512 * an explicit domain, then assign it the domain of the input.
8513 * In other cases, the domain is stored in the individual elements.
8515 __isl_give isl_multi_union_pw_aff *
8516 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8517 __isl_take isl_union_pw_multi_aff *upma)
8519 isl_space *space = NULL;
8520 isl_multi_union_pw_aff *mupa;
8521 int i;
8522 isl_size n;
8524 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8525 if (n < 0)
8526 goto error;
8527 if (n == 0)
8528 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8529 "cannot extract range space from empty input",
8530 goto error);
8531 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8532 &space) < 0)
8533 goto error;
8535 if (!space)
8536 goto error;
8538 n = isl_space_dim(space, isl_dim_set);
8539 if (n < 0)
8540 space = isl_space_free(space);
8541 mupa = isl_multi_union_pw_aff_alloc(space);
8543 for (i = 0; i < n; ++i) {
8544 isl_union_pw_aff *upa;
8546 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8547 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8549 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8550 isl_union_set *dom;
8551 isl_union_pw_multi_aff *copy;
8553 copy = isl_union_pw_multi_aff_copy(upma);
8554 dom = isl_union_pw_multi_aff_domain(copy);
8555 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8558 isl_union_pw_multi_aff_free(upma);
8559 return mupa;
8560 error:
8561 isl_space_free(space);
8562 isl_union_pw_multi_aff_free(upma);
8563 return NULL;
8566 /* Try and create an isl_multi_union_pw_aff that is equivalent
8567 * to the given isl_union_map.
8568 * The isl_union_map is required to be single-valued in each space.
8569 * Moreover, it cannot be empty and all range spaces need to be the same.
8570 * Otherwise, an error is produced.
8572 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8573 __isl_take isl_union_map *umap)
8575 isl_union_pw_multi_aff *upma;
8577 upma = isl_union_pw_multi_aff_from_union_map(umap);
8578 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8581 /* Return a multiple union piecewise affine expression
8582 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8583 * have been aligned.
8585 * If the resulting multi union piecewise affine expression has
8586 * an explicit domain, then assign it the input domain.
8587 * In other cases, the domain is stored in the individual elements.
8589 static __isl_give isl_multi_union_pw_aff *
8590 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8591 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8593 int i;
8594 isl_size n;
8595 isl_space *space;
8596 isl_multi_union_pw_aff *mupa;
8598 n = isl_multi_val_dim(mv, isl_dim_set);
8599 if (!domain || n < 0)
8600 goto error;
8602 space = isl_multi_val_get_space(mv);
8603 mupa = isl_multi_union_pw_aff_alloc(space);
8604 for (i = 0; i < n; ++i) {
8605 isl_val *v;
8606 isl_union_pw_aff *upa;
8608 v = isl_multi_val_get_val(mv, i);
8609 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8611 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8613 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8614 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8615 isl_union_set_copy(domain));
8617 isl_union_set_free(domain);
8618 isl_multi_val_free(mv);
8619 return mupa;
8620 error:
8621 isl_union_set_free(domain);
8622 isl_multi_val_free(mv);
8623 return NULL;
8626 /* Return a multiple union piecewise affine expression
8627 * that is equal to "mv" on "domain".
8629 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8630 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8632 isl_bool equal_params;
8634 if (!domain || !mv)
8635 goto error;
8636 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8637 if (equal_params < 0)
8638 goto error;
8639 if (equal_params)
8640 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8641 domain, mv);
8642 domain = isl_union_set_align_params(domain,
8643 isl_multi_val_get_space(mv));
8644 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8645 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8646 error:
8647 isl_union_set_free(domain);
8648 isl_multi_val_free(mv);
8649 return NULL;
8652 /* Return a multiple union piecewise affine expression
8653 * that is equal to "ma" on "domain".
8655 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8656 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8658 isl_pw_multi_aff *pma;
8660 pma = isl_pw_multi_aff_from_multi_aff(ma);
8661 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8664 /* Return a multiple union piecewise affine expression
8665 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8666 * have been aligned.
8668 * If the resulting multi union piecewise affine expression has
8669 * an explicit domain, then assign it the input domain.
8670 * In other cases, the domain is stored in the individual elements.
8672 static __isl_give isl_multi_union_pw_aff *
8673 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8674 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8676 int i;
8677 isl_size n;
8678 isl_space *space;
8679 isl_multi_union_pw_aff *mupa;
8681 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8682 if (!domain || n < 0)
8683 goto error;
8684 space = isl_pw_multi_aff_get_space(pma);
8685 mupa = isl_multi_union_pw_aff_alloc(space);
8686 for (i = 0; i < n; ++i) {
8687 isl_pw_aff *pa;
8688 isl_union_pw_aff *upa;
8690 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8691 upa = isl_union_pw_aff_pw_aff_on_domain(
8692 isl_union_set_copy(domain), pa);
8693 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8695 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8696 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8697 isl_union_set_copy(domain));
8699 isl_union_set_free(domain);
8700 isl_pw_multi_aff_free(pma);
8701 return mupa;
8702 error:
8703 isl_union_set_free(domain);
8704 isl_pw_multi_aff_free(pma);
8705 return NULL;
8708 /* Return a multiple union piecewise affine expression
8709 * that is equal to "pma" on "domain".
8711 __isl_give isl_multi_union_pw_aff *
8712 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8713 __isl_take isl_pw_multi_aff *pma)
8715 isl_bool equal_params;
8716 isl_space *space;
8718 space = isl_pw_multi_aff_peek_space(pma);
8719 equal_params = isl_union_set_space_has_equal_params(domain, space);
8720 if (equal_params < 0)
8721 goto error;
8722 if (equal_params)
8723 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8724 domain, pma);
8725 domain = isl_union_set_align_params(domain,
8726 isl_pw_multi_aff_get_space(pma));
8727 pma = isl_pw_multi_aff_align_params(pma,
8728 isl_union_set_get_space(domain));
8729 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8730 pma);
8731 error:
8732 isl_union_set_free(domain);
8733 isl_pw_multi_aff_free(pma);
8734 return NULL;
8737 /* Return a union set containing those elements in the domains
8738 * of the elements of "mupa" where they are all zero.
8740 * If there are no elements, then simply return the entire domain.
8742 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8743 __isl_take isl_multi_union_pw_aff *mupa)
8745 int i;
8746 isl_size n;
8747 isl_union_pw_aff *upa;
8748 isl_union_set *zero;
8750 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8751 if (n < 0)
8752 mupa = isl_multi_union_pw_aff_free(mupa);
8753 if (!mupa)
8754 return NULL;
8756 if (n == 0)
8757 return isl_multi_union_pw_aff_domain(mupa);
8759 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8760 zero = isl_union_pw_aff_zero_union_set(upa);
8762 for (i = 1; i < n; ++i) {
8763 isl_union_set *zero_i;
8765 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8766 zero_i = isl_union_pw_aff_zero_union_set(upa);
8768 zero = isl_union_set_intersect(zero, zero_i);
8771 isl_multi_union_pw_aff_free(mupa);
8772 return zero;
8775 /* Construct a union map mapping the shared domain
8776 * of the union piecewise affine expressions to the range of "mupa"
8777 * in the special case of a 0D multi union piecewise affine expression.
8779 * Construct a map between the explicit domain of "mupa" and
8780 * the range space.
8781 * Note that this assumes that the domain consists of explicit elements.
8783 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8784 __isl_take isl_multi_union_pw_aff *mupa)
8786 isl_bool is_params;
8787 isl_space *space;
8788 isl_union_set *dom, *ran;
8790 space = isl_multi_union_pw_aff_get_space(mupa);
8791 dom = isl_multi_union_pw_aff_domain(mupa);
8792 ran = isl_union_set_from_set(isl_set_universe(space));
8794 is_params = isl_union_set_is_params(dom);
8795 if (is_params < 0)
8796 dom = isl_union_set_free(dom);
8797 else if (is_params)
8798 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8799 "cannot create union map from expression without "
8800 "explicit domain elements",
8801 dom = isl_union_set_free(dom));
8803 return isl_union_map_from_domain_and_range(dom, ran);
8806 /* Construct a union map mapping the shared domain
8807 * of the union piecewise affine expressions to the range of "mupa"
8808 * with each dimension in the range equated to the
8809 * corresponding union piecewise affine expression.
8811 * If the input is zero-dimensional, then construct a mapping
8812 * from its explicit domain.
8814 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8815 __isl_take isl_multi_union_pw_aff *mupa)
8817 int i;
8818 isl_size n;
8819 isl_space *space;
8820 isl_union_map *umap;
8821 isl_union_pw_aff *upa;
8823 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8824 if (n < 0)
8825 mupa = isl_multi_union_pw_aff_free(mupa);
8826 if (!mupa)
8827 return NULL;
8829 if (n == 0)
8830 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8832 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8833 umap = isl_union_map_from_union_pw_aff(upa);
8835 for (i = 1; i < n; ++i) {
8836 isl_union_map *umap_i;
8838 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8839 umap_i = isl_union_map_from_union_pw_aff(upa);
8840 umap = isl_union_map_flat_range_product(umap, umap_i);
8843 space = isl_multi_union_pw_aff_get_space(mupa);
8844 umap = isl_union_map_reset_range_space(umap, space);
8846 isl_multi_union_pw_aff_free(mupa);
8847 return umap;
8850 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8851 * "range" is the space from which to set the range space.
8852 * "res" collects the results.
8854 struct isl_union_pw_multi_aff_reset_range_space_data {
8855 isl_space *range;
8856 isl_union_pw_multi_aff *res;
8859 /* Replace the range space of "pma" by the range space of data->range and
8860 * add the result to data->res.
8862 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8864 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8865 isl_space *space;
8867 space = isl_pw_multi_aff_get_space(pma);
8868 space = isl_space_domain(space);
8869 space = isl_space_extend_domain_with_range(space,
8870 isl_space_copy(data->range));
8871 pma = isl_pw_multi_aff_reset_space(pma, space);
8872 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8874 return data->res ? isl_stat_ok : isl_stat_error;
8877 /* Replace the range space of all the piecewise affine expressions in "upma" by
8878 * the range space of "space".
8880 * This assumes that all these expressions have the same output dimension.
8882 * Since the spaces of the expressions change, so do their hash values.
8883 * We therefore need to create a new isl_union_pw_multi_aff.
8884 * Note that the hash value is currently computed based on the entire
8885 * space even though there can only be a single expression with a given
8886 * domain space.
8888 static __isl_give isl_union_pw_multi_aff *
8889 isl_union_pw_multi_aff_reset_range_space(
8890 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8892 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8893 isl_space *space_upma;
8895 space_upma = isl_union_pw_multi_aff_get_space(upma);
8896 data.res = isl_union_pw_multi_aff_empty(space_upma);
8897 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8898 &reset_range_space, &data) < 0)
8899 data.res = isl_union_pw_multi_aff_free(data.res);
8901 isl_space_free(space);
8902 isl_union_pw_multi_aff_free(upma);
8903 return data.res;
8906 /* Construct and return a union piecewise multi affine expression
8907 * that is equal to the given multi union piecewise affine expression,
8908 * in the special case of a 0D multi union piecewise affine expression.
8910 * Construct a union piecewise multi affine expression
8911 * on top of the explicit domain of the input.
8913 __isl_give isl_union_pw_multi_aff *
8914 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8915 __isl_take isl_multi_union_pw_aff *mupa)
8917 isl_space *space;
8918 isl_multi_val *mv;
8919 isl_union_set *domain;
8921 space = isl_multi_union_pw_aff_get_space(mupa);
8922 mv = isl_multi_val_zero(space);
8923 domain = isl_multi_union_pw_aff_domain(mupa);
8924 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8927 /* Construct and return a union piecewise multi affine expression
8928 * that is equal to the given multi union piecewise affine expression.
8930 * If the input is zero-dimensional, then
8931 * construct a union piecewise multi affine expression
8932 * on top of the explicit domain of the input.
8934 __isl_give isl_union_pw_multi_aff *
8935 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8936 __isl_take isl_multi_union_pw_aff *mupa)
8938 int i;
8939 isl_size n;
8940 isl_space *space;
8941 isl_union_pw_multi_aff *upma;
8942 isl_union_pw_aff *upa;
8944 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8945 if (n < 0)
8946 mupa = isl_multi_union_pw_aff_free(mupa);
8947 if (!mupa)
8948 return NULL;
8950 if (n == 0)
8951 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8953 space = isl_multi_union_pw_aff_get_space(mupa);
8954 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8955 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8957 for (i = 1; i < n; ++i) {
8958 isl_union_pw_multi_aff *upma_i;
8960 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8961 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8962 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8965 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8967 isl_multi_union_pw_aff_free(mupa);
8968 return upma;
8971 /* Intersect the range of "mupa" with "range",
8972 * in the special case where "mupa" is 0D.
8974 * Intersect the domain of "mupa" with the constraints on the parameters
8975 * of "range".
8977 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8978 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8980 range = isl_set_params(range);
8981 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8982 return mupa;
8985 /* Intersect the range of "mupa" with "range".
8986 * That is, keep only those domain elements that have a function value
8987 * in "range".
8989 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8990 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8992 isl_union_pw_multi_aff *upma;
8993 isl_union_set *domain;
8994 isl_space *space;
8995 isl_size n;
8996 int match;
8998 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8999 if (n < 0 || !range)
9000 goto error;
9002 space = isl_set_get_space(range);
9003 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9004 space, isl_dim_set);
9005 isl_space_free(space);
9006 if (match < 0)
9007 goto error;
9008 if (!match)
9009 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9010 "space don't match", goto error);
9011 if (n == 0)
9012 return mupa_intersect_range_0D(mupa, range);
9014 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9015 isl_multi_union_pw_aff_copy(mupa));
9016 domain = isl_union_set_from_set(range);
9017 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9018 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9020 return mupa;
9021 error:
9022 isl_multi_union_pw_aff_free(mupa);
9023 isl_set_free(range);
9024 return NULL;
9027 /* Return the shared domain of the elements of "mupa",
9028 * in the special case where "mupa" is zero-dimensional.
9030 * Return the explicit domain of "mupa".
9031 * Note that this domain may be a parameter set, either
9032 * because "mupa" is meant to live in a set space or
9033 * because no explicit domain has been set.
9035 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9036 __isl_take isl_multi_union_pw_aff *mupa)
9038 isl_union_set *dom;
9040 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9041 isl_multi_union_pw_aff_free(mupa);
9043 return dom;
9046 /* Return the shared domain of the elements of "mupa".
9048 * If "mupa" is zero-dimensional, then return its explicit domain.
9050 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9051 __isl_take isl_multi_union_pw_aff *mupa)
9053 int i;
9054 isl_size n;
9055 isl_union_pw_aff *upa;
9056 isl_union_set *dom;
9058 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9059 if (n < 0)
9060 mupa = isl_multi_union_pw_aff_free(mupa);
9061 if (!mupa)
9062 return NULL;
9064 if (n == 0)
9065 return isl_multi_union_pw_aff_domain_0D(mupa);
9067 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9068 dom = isl_union_pw_aff_domain(upa);
9069 for (i = 1; i < n; ++i) {
9070 isl_union_set *dom_i;
9072 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9073 dom_i = isl_union_pw_aff_domain(upa);
9074 dom = isl_union_set_intersect(dom, dom_i);
9077 isl_multi_union_pw_aff_free(mupa);
9078 return dom;
9081 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9082 * In particular, the spaces have been aligned.
9083 * The result is defined over the shared domain of the elements of "mupa"
9085 * We first extract the parametric constant part of "aff" and
9086 * define that over the shared domain.
9087 * Then we iterate over all input dimensions of "aff" and add the corresponding
9088 * multiples of the elements of "mupa".
9089 * Finally, we consider the integer divisions, calling the function
9090 * recursively to obtain an isl_union_pw_aff corresponding to the
9091 * integer division argument.
9093 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9094 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9096 int i;
9097 isl_size n_in, n_div;
9098 isl_union_pw_aff *upa;
9099 isl_union_set *uset;
9100 isl_val *v;
9101 isl_aff *cst;
9103 n_in = isl_aff_dim(aff, isl_dim_in);
9104 n_div = isl_aff_dim(aff, isl_dim_div);
9105 if (n_in < 0 || n_div < 0)
9106 goto error;
9108 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9109 cst = isl_aff_copy(aff);
9110 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9111 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9112 cst = isl_aff_project_domain_on_params(cst);
9113 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9115 for (i = 0; i < n_in; ++i) {
9116 isl_union_pw_aff *upa_i;
9118 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9119 continue;
9120 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9121 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9122 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9123 upa = isl_union_pw_aff_add(upa, upa_i);
9126 for (i = 0; i < n_div; ++i) {
9127 isl_aff *div;
9128 isl_union_pw_aff *upa_i;
9130 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9131 continue;
9132 div = isl_aff_get_div(aff, i);
9133 upa_i = multi_union_pw_aff_apply_aff(
9134 isl_multi_union_pw_aff_copy(mupa), div);
9135 upa_i = isl_union_pw_aff_floor(upa_i);
9136 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9137 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9138 upa = isl_union_pw_aff_add(upa, upa_i);
9141 isl_multi_union_pw_aff_free(mupa);
9142 isl_aff_free(aff);
9144 return upa;
9145 error:
9146 isl_multi_union_pw_aff_free(mupa);
9147 isl_aff_free(aff);
9148 return NULL;
9151 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9152 * with the domain of "aff".
9153 * Furthermore, the dimension of this space needs to be greater than zero.
9154 * The result is defined over the shared domain of the elements of "mupa"
9156 * We perform these checks and then hand over control to
9157 * multi_union_pw_aff_apply_aff.
9159 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9160 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9162 isl_size dim;
9163 isl_space *space1, *space2;
9164 isl_bool equal;
9166 mupa = isl_multi_union_pw_aff_align_params(mupa,
9167 isl_aff_get_space(aff));
9168 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9169 if (!mupa || !aff)
9170 goto error;
9172 space1 = isl_multi_union_pw_aff_get_space(mupa);
9173 space2 = isl_aff_get_domain_space(aff);
9174 equal = isl_space_is_equal(space1, space2);
9175 isl_space_free(space1);
9176 isl_space_free(space2);
9177 if (equal < 0)
9178 goto error;
9179 if (!equal)
9180 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9181 "spaces don't match", goto error);
9182 dim = isl_aff_dim(aff, isl_dim_in);
9183 if (dim < 0)
9184 goto error;
9185 if (dim == 0)
9186 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9187 "cannot determine domains", goto error);
9189 return multi_union_pw_aff_apply_aff(mupa, aff);
9190 error:
9191 isl_multi_union_pw_aff_free(mupa);
9192 isl_aff_free(aff);
9193 return NULL;
9196 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9197 * The space of "mupa" is known to be compatible with the domain of "ma".
9199 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9200 * on the domain of "mupa".
9202 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9203 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9205 isl_union_set *dom;
9207 dom = isl_multi_union_pw_aff_domain(mupa);
9208 ma = isl_multi_aff_project_domain_on_params(ma);
9210 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9213 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9214 * with the domain of "ma".
9215 * The result is defined over the shared domain of the elements of "mupa"
9217 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9218 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9220 isl_space *space1, *space2;
9221 isl_multi_union_pw_aff *res;
9222 isl_bool equal;
9223 int i;
9224 isl_size n_in, n_out;
9226 mupa = isl_multi_union_pw_aff_align_params(mupa,
9227 isl_multi_aff_get_space(ma));
9228 ma = isl_multi_aff_align_params(ma,
9229 isl_multi_union_pw_aff_get_space(mupa));
9230 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9231 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9232 if (!mupa || n_in < 0 || n_out < 0)
9233 goto error;
9235 space1 = isl_multi_union_pw_aff_get_space(mupa);
9236 space2 = isl_multi_aff_get_domain_space(ma);
9237 equal = isl_space_is_equal(space1, space2);
9238 isl_space_free(space1);
9239 isl_space_free(space2);
9240 if (equal < 0)
9241 goto error;
9242 if (!equal)
9243 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9244 "spaces don't match", goto error);
9245 if (n_in == 0)
9246 return mupa_apply_multi_aff_0D(mupa, ma);
9248 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9249 res = isl_multi_union_pw_aff_alloc(space1);
9251 for (i = 0; i < n_out; ++i) {
9252 isl_aff *aff;
9253 isl_union_pw_aff *upa;
9255 aff = isl_multi_aff_get_aff(ma, i);
9256 upa = multi_union_pw_aff_apply_aff(
9257 isl_multi_union_pw_aff_copy(mupa), aff);
9258 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9261 isl_multi_aff_free(ma);
9262 isl_multi_union_pw_aff_free(mupa);
9263 return res;
9264 error:
9265 isl_multi_union_pw_aff_free(mupa);
9266 isl_multi_aff_free(ma);
9267 return NULL;
9270 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9271 * The space of "mupa" is known to be compatible with the domain of "pa".
9273 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9274 * on the domain of "mupa".
9276 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9277 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9279 isl_union_set *dom;
9281 dom = isl_multi_union_pw_aff_domain(mupa);
9282 pa = isl_pw_aff_project_domain_on_params(pa);
9284 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9287 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9288 * with the domain of "pa".
9289 * Furthermore, the dimension of this space needs to be greater than zero.
9290 * The result is defined over the shared domain of the elements of "mupa"
9292 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9293 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9295 int i;
9296 isl_bool equal;
9297 isl_size n_in;
9298 isl_space *space, *space2;
9299 isl_union_pw_aff *upa;
9301 mupa = isl_multi_union_pw_aff_align_params(mupa,
9302 isl_pw_aff_get_space(pa));
9303 pa = isl_pw_aff_align_params(pa,
9304 isl_multi_union_pw_aff_get_space(mupa));
9305 if (!mupa || !pa)
9306 goto error;
9308 space = isl_multi_union_pw_aff_get_space(mupa);
9309 space2 = isl_pw_aff_get_domain_space(pa);
9310 equal = isl_space_is_equal(space, space2);
9311 isl_space_free(space);
9312 isl_space_free(space2);
9313 if (equal < 0)
9314 goto error;
9315 if (!equal)
9316 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9317 "spaces don't match", goto error);
9318 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9319 if (n_in < 0)
9320 goto error;
9321 if (n_in == 0)
9322 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9324 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9325 upa = isl_union_pw_aff_empty(space);
9327 for (i = 0; i < pa->n; ++i) {
9328 isl_aff *aff;
9329 isl_set *domain;
9330 isl_multi_union_pw_aff *mupa_i;
9331 isl_union_pw_aff *upa_i;
9333 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9334 domain = isl_set_copy(pa->p[i].set);
9335 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9336 aff = isl_aff_copy(pa->p[i].aff);
9337 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9338 upa = isl_union_pw_aff_union_add(upa, upa_i);
9341 isl_multi_union_pw_aff_free(mupa);
9342 isl_pw_aff_free(pa);
9343 return upa;
9344 error:
9345 isl_multi_union_pw_aff_free(mupa);
9346 isl_pw_aff_free(pa);
9347 return NULL;
9350 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9351 * The space of "mupa" is known to be compatible with the domain of "pma".
9353 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9354 * on the domain of "mupa".
9356 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9357 __isl_take isl_multi_union_pw_aff *mupa,
9358 __isl_take isl_pw_multi_aff *pma)
9360 isl_union_set *dom;
9362 dom = isl_multi_union_pw_aff_domain(mupa);
9363 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9365 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9368 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9369 * with the domain of "pma".
9370 * The result is defined over the shared domain of the elements of "mupa"
9372 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9373 __isl_take isl_multi_union_pw_aff *mupa,
9374 __isl_take isl_pw_multi_aff *pma)
9376 isl_space *space1, *space2;
9377 isl_multi_union_pw_aff *res;
9378 isl_bool equal;
9379 int i;
9380 isl_size n_in, n_out;
9382 mupa = isl_multi_union_pw_aff_align_params(mupa,
9383 isl_pw_multi_aff_get_space(pma));
9384 pma = isl_pw_multi_aff_align_params(pma,
9385 isl_multi_union_pw_aff_get_space(mupa));
9386 if (!mupa || !pma)
9387 goto error;
9389 space1 = isl_multi_union_pw_aff_get_space(mupa);
9390 space2 = isl_pw_multi_aff_get_domain_space(pma);
9391 equal = isl_space_is_equal(space1, space2);
9392 isl_space_free(space1);
9393 isl_space_free(space2);
9394 if (equal < 0)
9395 goto error;
9396 if (!equal)
9397 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9398 "spaces don't match", goto error);
9399 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9400 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9401 if (n_in < 0 || n_out < 0)
9402 goto error;
9403 if (n_in == 0)
9404 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9406 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9407 res = isl_multi_union_pw_aff_alloc(space1);
9409 for (i = 0; i < n_out; ++i) {
9410 isl_pw_aff *pa;
9411 isl_union_pw_aff *upa;
9413 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9414 upa = isl_multi_union_pw_aff_apply_pw_aff(
9415 isl_multi_union_pw_aff_copy(mupa), pa);
9416 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9419 isl_pw_multi_aff_free(pma);
9420 isl_multi_union_pw_aff_free(mupa);
9421 return res;
9422 error:
9423 isl_multi_union_pw_aff_free(mupa);
9424 isl_pw_multi_aff_free(pma);
9425 return NULL;
9428 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9429 * If the explicit domain only keeps track of constraints on the parameters,
9430 * then only update those constraints.
9432 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9433 __isl_take isl_multi_union_pw_aff *mupa,
9434 __isl_keep isl_union_pw_multi_aff *upma)
9436 isl_bool is_params;
9438 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9439 return isl_multi_union_pw_aff_free(mupa);
9441 mupa = isl_multi_union_pw_aff_cow(mupa);
9442 if (!mupa)
9443 return NULL;
9445 is_params = isl_union_set_is_params(mupa->u.dom);
9446 if (is_params < 0)
9447 return isl_multi_union_pw_aff_free(mupa);
9449 upma = isl_union_pw_multi_aff_copy(upma);
9450 if (is_params)
9451 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9452 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9453 else
9454 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9455 mupa->u.dom, upma);
9456 if (!mupa->u.dom)
9457 return isl_multi_union_pw_aff_free(mupa);
9458 return mupa;
9461 /* Compute the pullback of "mupa" by the function represented by "upma".
9462 * In other words, plug in "upma" in "mupa". The result contains
9463 * expressions defined over the domain space of "upma".
9465 * Run over all elements of "mupa" and plug in "upma" in each of them.
9467 * If "mupa" has an explicit domain, then it is this domain
9468 * that needs to undergo a pullback instead, i.e., a preimage.
9470 __isl_give isl_multi_union_pw_aff *
9471 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9472 __isl_take isl_multi_union_pw_aff *mupa,
9473 __isl_take isl_union_pw_multi_aff *upma)
9475 int i;
9476 isl_size n;
9478 mupa = isl_multi_union_pw_aff_align_params(mupa,
9479 isl_union_pw_multi_aff_get_space(upma));
9480 upma = isl_union_pw_multi_aff_align_params(upma,
9481 isl_multi_union_pw_aff_get_space(mupa));
9482 mupa = isl_multi_union_pw_aff_cow(mupa);
9483 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9484 if (n < 0 || !upma)
9485 goto error;
9487 for (i = 0; i < n; ++i) {
9488 isl_union_pw_aff *upa;
9490 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9491 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9492 isl_union_pw_multi_aff_copy(upma));
9493 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9496 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9497 mupa = preimage_explicit_domain(mupa, upma);
9499 isl_union_pw_multi_aff_free(upma);
9500 return mupa;
9501 error:
9502 isl_multi_union_pw_aff_free(mupa);
9503 isl_union_pw_multi_aff_free(upma);
9504 return NULL;
9507 /* Extract the sequence of elements in "mupa" with domain space "space"
9508 * (ignoring parameters).
9510 * For the elements of "mupa" that are not defined on the specified space,
9511 * the corresponding element in the result is empty.
9513 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9514 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9516 int i;
9517 isl_size n;
9518 isl_space *space_mpa;
9519 isl_multi_pw_aff *mpa;
9521 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9522 if (n < 0 || !space)
9523 goto error;
9525 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9526 space = isl_space_replace_params(space, space_mpa);
9527 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9528 space_mpa);
9529 mpa = isl_multi_pw_aff_alloc(space_mpa);
9531 space = isl_space_from_domain(space);
9532 space = isl_space_add_dims(space, isl_dim_out, 1);
9533 for (i = 0; i < n; ++i) {
9534 isl_union_pw_aff *upa;
9535 isl_pw_aff *pa;
9537 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9538 pa = isl_union_pw_aff_extract_pw_aff(upa,
9539 isl_space_copy(space));
9540 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9541 isl_union_pw_aff_free(upa);
9544 isl_space_free(space);
9545 return mpa;
9546 error:
9547 isl_space_free(space);
9548 return NULL;
9551 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9552 * should modify the base expressions in the input.
9554 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9555 * are taken into account.
9556 * "fn" is applied to each entry in the input.
9558 struct isl_union_pw_multi_aff_un_op_control {
9559 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9560 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9563 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9564 * a second argument) for use as an isl_union_pw_multi_aff_transform
9565 * base function (which does take a second argument).
9566 * Simply call control->fn without the second argument.
9568 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9569 __isl_take isl_pw_multi_aff *pma, void *user)
9571 struct isl_union_pw_multi_aff_un_op_control *control = user;
9573 return control->fn(pma);
9576 /* Construct an isl_union_pw_multi_aff that is obtained by
9577 * modifying "upma" according to "control".
9579 * isl_union_pw_multi_aff_transform performs essentially
9580 * the same operation, but takes a callback function
9581 * of a different form (with an extra argument).
9582 * Call isl_union_pw_multi_aff_transform with a wrapper
9583 * that removes this extra argument.
9585 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9586 __isl_take isl_union_pw_multi_aff *upma,
9587 struct isl_union_pw_multi_aff_un_op_control *control)
9589 struct isl_union_pw_multi_aff_transform_control t_control = {
9590 .filter = control->filter,
9591 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9592 .fn_user = control,
9595 return isl_union_pw_multi_aff_transform(upma, &t_control);
9598 /* For each function in "upma" of the form A -> [B -> C],
9599 * extract the function A -> B and collect the results.
9601 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9602 __isl_take isl_union_pw_multi_aff *upma)
9604 struct isl_union_pw_multi_aff_un_op_control control = {
9605 .filter = &isl_pw_multi_aff_range_is_wrapping,
9606 .fn = &isl_pw_multi_aff_range_factor_domain,
9608 return isl_union_pw_multi_aff_un_op(upma, &control);
9611 /* For each function in "upma" of the form A -> [B -> C],
9612 * extract the function A -> C and collect the results.
9614 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9615 __isl_take isl_union_pw_multi_aff *upma)
9617 struct isl_union_pw_multi_aff_un_op_control control = {
9618 .filter = &isl_pw_multi_aff_range_is_wrapping,
9619 .fn = &isl_pw_multi_aff_range_factor_range,
9621 return isl_union_pw_multi_aff_un_op(upma, &control);
9624 /* Evaluate the affine function "aff" in the void point "pnt".
9625 * In particular, return the value NaN.
9627 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9628 __isl_take isl_point *pnt)
9630 isl_ctx *ctx;
9632 ctx = isl_point_get_ctx(pnt);
9633 isl_aff_free(aff);
9634 isl_point_free(pnt);
9635 return isl_val_nan(ctx);
9638 /* Evaluate the affine expression "aff"
9639 * in the coordinates (with denominator) "pnt".
9641 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9642 __isl_keep isl_vec *pnt)
9644 isl_int n, d;
9645 isl_ctx *ctx;
9646 isl_val *v;
9648 if (!aff || !pnt)
9649 return NULL;
9651 ctx = isl_vec_get_ctx(aff);
9652 isl_int_init(n);
9653 isl_int_init(d);
9654 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9655 isl_int_mul(d, aff->el[0], pnt->el[0]);
9656 v = isl_val_rat_from_isl_int(ctx, n, d);
9657 v = isl_val_normalize(v);
9658 isl_int_clear(n);
9659 isl_int_clear(d);
9661 return v;
9664 /* Check that the domain space of "aff" is equal to "space".
9666 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9667 __isl_keep isl_space *space)
9669 isl_bool ok;
9671 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9672 if (ok < 0)
9673 return isl_stat_error;
9674 if (!ok)
9675 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9676 "incompatible spaces", return isl_stat_error);
9677 return isl_stat_ok;
9680 /* Evaluate the affine function "aff" in "pnt".
9682 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9683 __isl_take isl_point *pnt)
9685 isl_bool is_void;
9686 isl_val *v;
9687 isl_local_space *ls;
9689 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9690 goto error;
9691 is_void = isl_point_is_void(pnt);
9692 if (is_void < 0)
9693 goto error;
9694 if (is_void)
9695 return eval_void(aff, pnt);
9697 ls = isl_aff_get_domain_local_space(aff);
9698 pnt = isl_local_space_lift_point(ls, pnt);
9700 v = eval(aff->v, isl_point_peek_vec(pnt));
9702 isl_aff_free(aff);
9703 isl_point_free(pnt);
9705 return v;
9706 error:
9707 isl_aff_free(aff);
9708 isl_point_free(pnt);
9709 return NULL;