isl_coalesce.c: fix typos in comments
[isl.git] / isl_aff.c
blobd238963dc85ef40ef6ec68650620c77dc7f6cc32
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 2016 Sven Verdoolaege
7 * Copyright 2018,2020 Cerebras Systems
8 * Copyright 2021 Sven Verdoolaege
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
13 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
14 * 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_union_map_private.h>
24 #include <isl_aff_private.h>
25 #include <isl_space_private.h>
26 #include <isl_local_space_private.h>
27 #include <isl_vec_private.h>
28 #include <isl_mat_private.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl_val_private.h>
34 #include <isl_point_private.h>
35 #include <isl_config.h>
37 #undef EL_BASE
38 #define EL_BASE aff
40 #include <isl_list_templ.c>
42 #undef EL_BASE
43 #define EL_BASE pw_aff
45 #include <isl_list_templ.c>
47 #undef EL_BASE
48 #define EL_BASE pw_multi_aff
50 #include <isl_list_templ.c>
52 #undef EL_BASE
53 #define EL_BASE union_pw_aff
55 #include <isl_list_templ.c>
57 #undef EL_BASE
58 #define EL_BASE union_pw_multi_aff
60 #include <isl_list_templ.c>
62 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
63 __isl_take isl_vec *v)
65 isl_aff *aff;
67 if (!ls || !v)
68 goto error;
70 aff = isl_calloc_type(v->ctx, struct isl_aff);
71 if (!aff)
72 goto error;
74 aff->ref = 1;
75 aff->ls = ls;
76 aff->v = v;
78 return aff;
79 error:
80 isl_local_space_free(ls);
81 isl_vec_free(v);
82 return NULL;
85 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
87 isl_ctx *ctx;
88 isl_vec *v;
89 isl_size total;
91 if (!ls)
92 return NULL;
94 ctx = isl_local_space_get_ctx(ls);
95 if (!isl_local_space_divs_known(ls))
96 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
97 goto error);
98 if (!isl_local_space_is_set(ls))
99 isl_die(ctx, isl_error_invalid,
100 "domain of affine expression should be a set",
101 goto error);
103 total = isl_local_space_dim(ls, isl_dim_all);
104 if (total < 0)
105 goto error;
106 v = isl_vec_alloc(ctx, 1 + 1 + total);
107 return isl_aff_alloc_vec(ls, v);
108 error:
109 isl_local_space_free(ls);
110 return NULL;
113 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
115 if (!aff)
116 return NULL;
118 aff->ref++;
119 return aff;
122 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
124 if (!aff)
125 return NULL;
127 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
128 isl_vec_copy(aff->v));
131 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
133 if (!aff)
134 return NULL;
136 if (aff->ref == 1)
137 return aff;
138 aff->ref--;
139 return isl_aff_dup(aff);
142 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
144 isl_aff *aff;
146 aff = isl_aff_alloc(ls);
147 if (!aff)
148 return NULL;
150 isl_int_set_si(aff->v->el[0], 1);
151 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
153 return aff;
156 /* Return an affine expression that is equal to zero on domain space "space".
158 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
160 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
163 /* This function performs the same operation as isl_aff_zero_on_domain_space,
164 * but is considered as a function on an isl_space when exported.
166 __isl_give isl_aff *isl_space_zero_aff_on_domain(__isl_take isl_space *space)
168 return isl_aff_zero_on_domain_space(space);
171 /* Return a piecewise affine expression defined on the specified domain
172 * that is equal to zero.
174 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
176 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
179 /* Change "aff" into a NaN.
181 * Note that this function gets called from isl_aff_nan_on_domain,
182 * so "aff" may not have been initialized yet.
184 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
186 aff = isl_aff_cow(aff);
187 if (!aff)
188 return NULL;
190 aff->v = isl_vec_clr(aff->v);
191 if (!aff->v)
192 return isl_aff_free(aff);
194 return aff;
197 /* Return an affine expression defined on the specified domain
198 * that represents NaN.
200 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
202 isl_aff *aff;
204 aff = isl_aff_alloc(ls);
205 return isl_aff_set_nan(aff);
208 /* Return an affine expression defined on the specified domain space
209 * that represents NaN.
211 __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
213 return isl_aff_nan_on_domain(isl_local_space_from_space(space));
216 /* Return a piecewise affine expression defined on the specified domain space
217 * that represents NaN.
219 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
220 __isl_take isl_space *space)
222 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
225 /* Return a piecewise affine expression defined on the specified domain
226 * that represents NaN.
228 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
230 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
233 /* Return an affine expression that is equal to "val" on
234 * domain local space "ls".
236 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
237 __isl_take isl_val *val)
239 isl_aff *aff;
241 if (!ls || !val)
242 goto error;
243 if (!isl_val_is_rat(val))
244 isl_die(isl_val_get_ctx(val), isl_error_invalid,
245 "expecting rational value", goto error);
247 aff = isl_aff_alloc(isl_local_space_copy(ls));
248 if (!aff)
249 goto error;
251 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
252 isl_int_set(aff->v->el[1], val->n);
253 isl_int_set(aff->v->el[0], val->d);
255 isl_local_space_free(ls);
256 isl_val_free(val);
257 return aff;
258 error:
259 isl_local_space_free(ls);
260 isl_val_free(val);
261 return NULL;
264 /* Return an affine expression that is equal to "val" on domain space "space".
266 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
267 __isl_take isl_val *val)
269 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
272 /* Return an affine expression that is equal to the specified dimension
273 * in "ls".
275 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
276 enum isl_dim_type type, unsigned pos)
278 isl_space *space;
279 isl_aff *aff;
281 if (!ls)
282 return NULL;
284 space = isl_local_space_get_space(ls);
285 if (!space)
286 goto error;
287 if (isl_space_is_map(space))
288 isl_die(isl_space_get_ctx(space), isl_error_invalid,
289 "expecting (parameter) set space", goto error);
290 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
291 goto error;
293 isl_space_free(space);
294 aff = isl_aff_alloc(ls);
295 if (!aff)
296 return NULL;
298 pos += isl_local_space_offset(aff->ls, type);
300 isl_int_set_si(aff->v->el[0], 1);
301 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
302 isl_int_set_si(aff->v->el[1 + pos], 1);
304 return aff;
305 error:
306 isl_local_space_free(ls);
307 isl_space_free(space);
308 return NULL;
311 /* Return a piecewise affine expression that is equal to
312 * the specified dimension in "ls".
314 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
315 enum isl_dim_type type, unsigned pos)
317 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
320 /* Return an affine expression that is equal to the parameter
321 * in the domain space "space" with identifier "id".
323 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
324 __isl_take isl_space *space, __isl_take isl_id *id)
326 int pos;
327 isl_local_space *ls;
329 if (!space || !id)
330 goto error;
331 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
332 if (pos < 0)
333 isl_die(isl_space_get_ctx(space), isl_error_invalid,
334 "parameter not found in space", goto error);
335 isl_id_free(id);
336 ls = isl_local_space_from_space(space);
337 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
338 error:
339 isl_space_free(space);
340 isl_id_free(id);
341 return NULL;
344 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
346 if (!aff)
347 return NULL;
349 if (--aff->ref > 0)
350 return NULL;
352 isl_local_space_free(aff->ls);
353 isl_vec_free(aff->v);
355 free(aff);
357 return NULL;
360 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
362 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
365 /* Return a hash value that digests "aff".
367 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
369 uint32_t hash, ls_hash, v_hash;
371 if (!aff)
372 return 0;
374 hash = isl_hash_init();
375 ls_hash = isl_local_space_get_hash(aff->ls);
376 isl_hash_hash(hash, ls_hash);
377 v_hash = isl_vec_get_hash(aff->v);
378 isl_hash_hash(hash, v_hash);
380 return hash;
383 /* Return the domain local space of "aff".
385 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
386 __isl_keep isl_aff *aff)
388 return aff ? aff->ls : NULL;
391 /* Return the number of variables of the given type in the domain of "aff".
393 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
395 isl_local_space *ls;
397 ls = isl_aff_peek_domain_local_space(aff);
398 return isl_local_space_dim(ls, type);
401 /* Externally, an isl_aff has a map space, but internally, the
402 * ls field corresponds to the domain of that space.
404 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
406 if (!aff)
407 return isl_size_error;
408 if (type == isl_dim_out)
409 return 1;
410 if (type == isl_dim_in)
411 type = isl_dim_set;
412 return isl_aff_domain_dim(aff, type);
415 /* Return the offset of the first coefficient of type "type" in
416 * the domain of "aff".
418 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
420 isl_local_space *ls;
422 ls = isl_aff_peek_domain_local_space(aff);
423 return isl_local_space_offset(ls, type);
426 /* Return the position of the dimension of the given type and name
427 * in "aff".
428 * Return -1 if no such dimension can be found.
430 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
431 const char *name)
433 if (!aff)
434 return -1;
435 if (type == isl_dim_out)
436 return -1;
437 if (type == isl_dim_in)
438 type = isl_dim_set;
439 return isl_local_space_find_dim_by_name(aff->ls, type, name);
442 /* Return the domain space of "aff".
444 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
446 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
449 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
451 return isl_space_copy(isl_aff_peek_domain_space(aff));
454 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
456 isl_space *space;
457 if (!aff)
458 return NULL;
459 space = isl_local_space_get_space(aff->ls);
460 space = isl_space_from_domain(space);
461 space = isl_space_add_dims(space, isl_dim_out, 1);
462 return space;
465 /* Return a copy of the domain space of "aff".
467 __isl_give isl_local_space *isl_aff_get_domain_local_space(
468 __isl_keep isl_aff *aff)
470 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
473 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
475 isl_local_space *ls;
476 if (!aff)
477 return NULL;
478 ls = isl_local_space_copy(aff->ls);
479 ls = isl_local_space_from_domain(ls);
480 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
481 return ls;
484 /* Return the local space of the domain of "aff".
485 * This may be either a copy or the local space itself
486 * if there is only one reference to "aff".
487 * This allows the local space to be modified inplace
488 * if both the expression and its local space have only a single reference.
489 * The caller is not allowed to modify "aff" between this call and
490 * a subsequent call to isl_aff_restore_domain_local_space.
491 * The only exception is that isl_aff_free can be called instead.
493 __isl_give isl_local_space *isl_aff_take_domain_local_space(
494 __isl_keep isl_aff *aff)
496 isl_local_space *ls;
498 if (!aff)
499 return NULL;
500 if (aff->ref != 1)
501 return isl_aff_get_domain_local_space(aff);
502 ls = aff->ls;
503 aff->ls = NULL;
504 return ls;
507 /* Set the local space of the domain of "aff" to "ls",
508 * where the local space of "aff" may be missing
509 * due to a preceding call to isl_aff_take_domain_local_space.
510 * However, in this case, "aff" only has a single reference and
511 * then the call to isl_aff_cow has no effect.
513 __isl_give isl_aff *isl_aff_restore_domain_local_space(
514 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
516 if (!aff || !ls)
517 goto error;
519 if (aff->ls == ls) {
520 isl_local_space_free(ls);
521 return aff;
524 aff = isl_aff_cow(aff);
525 if (!aff)
526 goto error;
527 isl_local_space_free(aff->ls);
528 aff->ls = ls;
530 return aff;
531 error:
532 isl_aff_free(aff);
533 isl_local_space_free(ls);
534 return NULL;
537 /* Externally, an isl_aff has a map space, but internally, the
538 * ls field corresponds to the domain of that space.
540 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
541 enum isl_dim_type type, unsigned pos)
543 if (!aff)
544 return NULL;
545 if (type == isl_dim_out)
546 return NULL;
547 if (type == isl_dim_in)
548 type = isl_dim_set;
549 return isl_local_space_get_dim_name(aff->ls, type, pos);
552 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
553 __isl_take isl_space *space)
555 aff = isl_aff_cow(aff);
556 if (!aff || !space)
557 goto error;
559 aff->ls = isl_local_space_reset_space(aff->ls, space);
560 if (!aff->ls)
561 return isl_aff_free(aff);
563 return aff;
564 error:
565 isl_aff_free(aff);
566 isl_space_free(space);
567 return NULL;
570 /* Reset the space of "aff". This function is called from isl_pw_templ.c
571 * and doesn't know if the space of an element object is represented
572 * directly or through its domain. It therefore passes along both.
574 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
575 __isl_take isl_space *space, __isl_take isl_space *domain)
577 isl_space_free(space);
578 return isl_aff_reset_domain_space(aff, domain);
581 /* Reorder the coefficients of the affine expression based
582 * on the given reordering.
583 * The reordering r is assumed to have been extended with the local
584 * variables.
586 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
587 __isl_take isl_reordering *r, int n_div)
589 isl_space *space;
590 isl_vec *res;
591 isl_size dim;
592 int i;
594 if (!vec || !r)
595 goto error;
597 space = isl_reordering_peek_space(r);
598 dim = isl_space_dim(space, isl_dim_all);
599 if (dim < 0)
600 goto error;
601 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
602 if (!res)
603 goto error;
604 isl_seq_cpy(res->el, vec->el, 2);
605 isl_seq_clr(res->el + 2, res->size - 2);
606 for (i = 0; i < r->len; ++i)
607 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
609 isl_reordering_free(r);
610 isl_vec_free(vec);
611 return res;
612 error:
613 isl_vec_free(vec);
614 isl_reordering_free(r);
615 return NULL;
618 /* Reorder the dimensions of the domain of "aff" according
619 * to the given reordering.
621 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
622 __isl_take isl_reordering *r)
624 aff = isl_aff_cow(aff);
625 if (!aff)
626 goto error;
628 r = isl_reordering_extend(r, aff->ls->div->n_row);
629 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
630 aff->ls->div->n_row);
631 aff->ls = isl_local_space_realign(aff->ls, r);
633 if (!aff->v || !aff->ls)
634 return isl_aff_free(aff);
636 return aff;
637 error:
638 isl_aff_free(aff);
639 isl_reordering_free(r);
640 return NULL;
643 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
644 __isl_take isl_space *model)
646 isl_bool equal_params;
648 if (!aff || !model)
649 goto error;
651 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
652 if (equal_params < 0)
653 goto error;
654 if (!equal_params) {
655 isl_reordering *exp;
657 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
658 exp = isl_reordering_extend_space(exp,
659 isl_aff_get_domain_space(aff));
660 aff = isl_aff_realign_domain(aff, exp);
663 isl_space_free(model);
664 return aff;
665 error:
666 isl_space_free(model);
667 isl_aff_free(aff);
668 return NULL;
671 #undef TYPE
672 #define TYPE isl_aff
673 #include "isl_unbind_params_templ.c"
675 /* Is "aff" obviously equal to zero?
677 * If the denominator is zero, then "aff" is not equal to zero.
679 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
681 int pos;
683 if (!aff)
684 return isl_bool_error;
686 if (isl_int_is_zero(aff->v->el[0]))
687 return isl_bool_false;
688 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
689 return isl_bool_ok(pos < 0);
692 /* Does "aff" represent NaN?
694 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
696 if (!aff)
697 return isl_bool_error;
699 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
702 /* Are "aff1" and "aff2" obviously equal?
704 * NaN is not equal to anything, not even to another NaN.
706 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
707 __isl_keep isl_aff *aff2)
709 isl_bool equal;
711 if (!aff1 || !aff2)
712 return isl_bool_error;
714 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
715 return isl_bool_false;
717 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
718 if (equal < 0 || !equal)
719 return equal;
721 return isl_vec_is_equal(aff1->v, aff2->v);
724 /* Return the common denominator of "aff" in "v".
726 * We cannot return anything meaningful in case of a NaN.
728 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
730 if (!aff)
731 return isl_stat_error;
732 if (isl_aff_is_nan(aff))
733 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
734 "cannot get denominator of NaN", return isl_stat_error);
735 isl_int_set(*v, aff->v->el[0]);
736 return isl_stat_ok;
739 /* Return the common denominator of "aff".
741 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
743 isl_ctx *ctx;
745 if (!aff)
746 return NULL;
748 ctx = isl_aff_get_ctx(aff);
749 if (isl_aff_is_nan(aff))
750 return isl_val_nan(ctx);
751 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
754 /* Return the constant term of "aff".
756 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
758 isl_ctx *ctx;
759 isl_val *v;
761 if (!aff)
762 return NULL;
764 ctx = isl_aff_get_ctx(aff);
765 if (isl_aff_is_nan(aff))
766 return isl_val_nan(ctx);
767 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
768 return isl_val_normalize(v);
771 /* Return the coefficient of the variable of type "type" at position "pos"
772 * of "aff".
774 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
775 enum isl_dim_type type, int pos)
777 isl_ctx *ctx;
778 isl_val *v;
780 if (!aff)
781 return NULL;
783 ctx = isl_aff_get_ctx(aff);
784 if (type == isl_dim_out)
785 isl_die(ctx, isl_error_invalid,
786 "output/set dimension does not have a coefficient",
787 return NULL);
788 if (type == isl_dim_in)
789 type = isl_dim_set;
791 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
792 return NULL;
794 if (isl_aff_is_nan(aff))
795 return isl_val_nan(ctx);
796 pos += isl_local_space_offset(aff->ls, type);
797 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
798 return isl_val_normalize(v);
801 /* Return the sign of the coefficient of the variable of type "type"
802 * at position "pos" of "aff".
804 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
805 int pos)
807 isl_ctx *ctx;
809 if (!aff)
810 return 0;
812 ctx = isl_aff_get_ctx(aff);
813 if (type == isl_dim_out)
814 isl_die(ctx, isl_error_invalid,
815 "output/set dimension does not have a coefficient",
816 return 0);
817 if (type == isl_dim_in)
818 type = isl_dim_set;
820 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
821 return 0;
823 pos += isl_local_space_offset(aff->ls, type);
824 return isl_int_sgn(aff->v->el[1 + pos]);
827 /* Replace the numerator of the constant term of "aff" by "v".
829 * A NaN is unaffected by this operation.
831 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
833 if (!aff)
834 return NULL;
835 if (isl_aff_is_nan(aff))
836 return aff;
837 aff = isl_aff_cow(aff);
838 if (!aff)
839 return NULL;
841 aff->v = isl_vec_cow(aff->v);
842 if (!aff->v)
843 return isl_aff_free(aff);
845 isl_int_set(aff->v->el[1], v);
847 return aff;
850 /* Replace the constant term of "aff" by "v".
852 * A NaN is unaffected by this operation.
854 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
855 __isl_take isl_val *v)
857 if (!aff || !v)
858 goto error;
860 if (isl_aff_is_nan(aff)) {
861 isl_val_free(v);
862 return aff;
865 if (!isl_val_is_rat(v))
866 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
867 "expecting rational value", goto error);
869 if (isl_int_eq(aff->v->el[1], v->n) &&
870 isl_int_eq(aff->v->el[0], v->d)) {
871 isl_val_free(v);
872 return aff;
875 aff = isl_aff_cow(aff);
876 if (!aff)
877 goto error;
878 aff->v = isl_vec_cow(aff->v);
879 if (!aff->v)
880 goto error;
882 if (isl_int_eq(aff->v->el[0], v->d)) {
883 isl_int_set(aff->v->el[1], v->n);
884 } else if (isl_int_is_one(v->d)) {
885 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
886 } else {
887 isl_seq_scale(aff->v->el + 1,
888 aff->v->el + 1, v->d, aff->v->size - 1);
889 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
890 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
891 aff->v = isl_vec_normalize(aff->v);
892 if (!aff->v)
893 goto error;
896 isl_val_free(v);
897 return aff;
898 error:
899 isl_aff_free(aff);
900 isl_val_free(v);
901 return NULL;
904 /* Add "v" to the constant term of "aff".
906 * A NaN is unaffected by this operation.
908 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
910 if (isl_int_is_zero(v))
911 return aff;
913 if (!aff)
914 return NULL;
915 if (isl_aff_is_nan(aff))
916 return aff;
917 aff = isl_aff_cow(aff);
918 if (!aff)
919 return NULL;
921 aff->v = isl_vec_cow(aff->v);
922 if (!aff->v)
923 return isl_aff_free(aff);
925 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
927 return aff;
930 /* Add "v" to the constant term of "aff",
931 * in case "aff" is a rational expression.
933 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
934 __isl_take isl_val *v)
936 aff = isl_aff_cow(aff);
937 if (!aff)
938 goto error;
940 aff->v = isl_vec_cow(aff->v);
941 if (!aff->v)
942 goto error;
944 if (isl_int_is_one(v->d)) {
945 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
946 } else if (isl_int_eq(aff->v->el[0], v->d)) {
947 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
948 aff->v = isl_vec_normalize(aff->v);
949 if (!aff->v)
950 goto error;
951 } else {
952 isl_seq_scale(aff->v->el + 1,
953 aff->v->el + 1, v->d, aff->v->size - 1);
954 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
955 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
956 aff->v = isl_vec_normalize(aff->v);
957 if (!aff->v)
958 goto error;
961 isl_val_free(v);
962 return aff;
963 error:
964 isl_aff_free(aff);
965 isl_val_free(v);
966 return NULL;
969 /* Return the first argument and free the second.
971 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
972 __isl_take isl_val *v)
974 isl_val_free(v);
975 return aff;
978 /* Replace the first argument by NaN and free the second argument.
980 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
981 __isl_take isl_val *v)
983 isl_val_free(v);
984 return isl_aff_set_nan(aff);
987 /* Add "v" to the constant term of "aff".
989 * A NaN is unaffected by this operation.
990 * Conversely, adding a NaN turns "aff" into a NaN.
992 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
993 __isl_take isl_val *v)
995 isl_bool is_nan, is_zero, is_rat;
997 is_nan = isl_aff_is_nan(aff);
998 is_zero = isl_val_is_zero(v);
999 if (is_nan < 0 || is_zero < 0)
1000 goto error;
1001 if (is_nan || is_zero)
1002 return pick_free(aff, v);
1004 is_nan = isl_val_is_nan(v);
1005 is_rat = isl_val_is_rat(v);
1006 if (is_nan < 0 || is_rat < 0)
1007 goto error;
1008 if (is_nan)
1009 return set_nan_free_val(aff, v);
1010 if (!is_rat)
1011 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1012 "expecting rational value or NaN", goto error);
1014 return isl_aff_add_rat_constant_val(aff, v);
1015 error:
1016 isl_aff_free(aff);
1017 isl_val_free(v);
1018 return NULL;
1021 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1023 isl_int t;
1025 isl_int_init(t);
1026 isl_int_set_si(t, v);
1027 aff = isl_aff_add_constant(aff, t);
1028 isl_int_clear(t);
1030 return aff;
1033 /* Add "v" to the numerator of the constant term of "aff".
1035 * A NaN is unaffected by this operation.
1037 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
1039 if (isl_int_is_zero(v))
1040 return aff;
1042 if (!aff)
1043 return NULL;
1044 if (isl_aff_is_nan(aff))
1045 return aff;
1046 aff = isl_aff_cow(aff);
1047 if (!aff)
1048 return NULL;
1050 aff->v = isl_vec_cow(aff->v);
1051 if (!aff->v)
1052 return isl_aff_free(aff);
1054 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1056 return aff;
1059 /* Add "v" to the numerator of the constant term of "aff".
1061 * A NaN is unaffected by this operation.
1063 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1065 isl_int t;
1067 if (v == 0)
1068 return aff;
1070 isl_int_init(t);
1071 isl_int_set_si(t, v);
1072 aff = isl_aff_add_constant_num(aff, t);
1073 isl_int_clear(t);
1075 return aff;
1078 /* Replace the numerator of the constant term of "aff" by "v".
1080 * A NaN is unaffected by this operation.
1082 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1084 if (!aff)
1085 return NULL;
1086 if (isl_aff_is_nan(aff))
1087 return aff;
1088 aff = isl_aff_cow(aff);
1089 if (!aff)
1090 return NULL;
1092 aff->v = isl_vec_cow(aff->v);
1093 if (!aff->v)
1094 return isl_aff_free(aff);
1096 isl_int_set_si(aff->v->el[1], v);
1098 return aff;
1101 /* Replace the numerator of the coefficient of the variable of type "type"
1102 * at position "pos" of "aff" by "v".
1104 * A NaN is unaffected by this operation.
1106 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1107 enum isl_dim_type type, int pos, isl_int v)
1109 if (!aff)
1110 return NULL;
1112 if (type == isl_dim_out)
1113 isl_die(aff->v->ctx, isl_error_invalid,
1114 "output/set dimension does not have a coefficient",
1115 return isl_aff_free(aff));
1116 if (type == isl_dim_in)
1117 type = isl_dim_set;
1119 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1120 return isl_aff_free(aff);
1122 if (isl_aff_is_nan(aff))
1123 return aff;
1124 aff = isl_aff_cow(aff);
1125 if (!aff)
1126 return NULL;
1128 aff->v = isl_vec_cow(aff->v);
1129 if (!aff->v)
1130 return isl_aff_free(aff);
1132 pos += isl_local_space_offset(aff->ls, type);
1133 isl_int_set(aff->v->el[1 + pos], v);
1135 return aff;
1138 /* Replace the numerator of the coefficient of the variable of type "type"
1139 * at position "pos" of "aff" by "v".
1141 * A NaN is unaffected by this operation.
1143 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1144 enum isl_dim_type type, int pos, int v)
1146 if (!aff)
1147 return NULL;
1149 if (type == isl_dim_out)
1150 isl_die(aff->v->ctx, isl_error_invalid,
1151 "output/set dimension does not have a coefficient",
1152 return isl_aff_free(aff));
1153 if (type == isl_dim_in)
1154 type = isl_dim_set;
1156 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1157 return isl_aff_free(aff);
1159 if (isl_aff_is_nan(aff))
1160 return aff;
1161 pos += isl_local_space_offset(aff->ls, type);
1162 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1163 return aff;
1165 aff = isl_aff_cow(aff);
1166 if (!aff)
1167 return NULL;
1169 aff->v = isl_vec_cow(aff->v);
1170 if (!aff->v)
1171 return isl_aff_free(aff);
1173 isl_int_set_si(aff->v->el[1 + pos], v);
1175 return aff;
1178 /* Replace the coefficient of the variable of type "type" at position "pos"
1179 * of "aff" by "v".
1181 * A NaN is unaffected by this operation.
1183 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1184 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1186 if (!aff || !v)
1187 goto error;
1189 if (type == isl_dim_out)
1190 isl_die(aff->v->ctx, isl_error_invalid,
1191 "output/set dimension does not have a coefficient",
1192 goto error);
1193 if (type == isl_dim_in)
1194 type = isl_dim_set;
1196 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1197 return isl_aff_free(aff);
1199 if (isl_aff_is_nan(aff)) {
1200 isl_val_free(v);
1201 return aff;
1203 if (!isl_val_is_rat(v))
1204 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1205 "expecting rational value", goto error);
1207 pos += isl_local_space_offset(aff->ls, type);
1208 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1209 isl_int_eq(aff->v->el[0], v->d)) {
1210 isl_val_free(v);
1211 return aff;
1214 aff = isl_aff_cow(aff);
1215 if (!aff)
1216 goto error;
1217 aff->v = isl_vec_cow(aff->v);
1218 if (!aff->v)
1219 goto error;
1221 if (isl_int_eq(aff->v->el[0], v->d)) {
1222 isl_int_set(aff->v->el[1 + pos], v->n);
1223 } else if (isl_int_is_one(v->d)) {
1224 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1225 } else {
1226 isl_seq_scale(aff->v->el + 1,
1227 aff->v->el + 1, v->d, aff->v->size - 1);
1228 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1229 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1230 aff->v = isl_vec_normalize(aff->v);
1231 if (!aff->v)
1232 goto error;
1235 isl_val_free(v);
1236 return aff;
1237 error:
1238 isl_aff_free(aff);
1239 isl_val_free(v);
1240 return NULL;
1243 /* Add "v" to the coefficient of the variable of type "type"
1244 * at position "pos" of "aff".
1246 * A NaN is unaffected by this operation.
1248 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1249 enum isl_dim_type type, int pos, isl_int v)
1251 if (!aff)
1252 return NULL;
1254 if (type == isl_dim_out)
1255 isl_die(aff->v->ctx, isl_error_invalid,
1256 "output/set dimension does not have a coefficient",
1257 return isl_aff_free(aff));
1258 if (type == isl_dim_in)
1259 type = isl_dim_set;
1261 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1262 return isl_aff_free(aff);
1264 if (isl_aff_is_nan(aff))
1265 return aff;
1266 aff = isl_aff_cow(aff);
1267 if (!aff)
1268 return NULL;
1270 aff->v = isl_vec_cow(aff->v);
1271 if (!aff->v)
1272 return isl_aff_free(aff);
1274 pos += isl_local_space_offset(aff->ls, type);
1275 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1277 return aff;
1280 /* Add "v" to the coefficient of the variable of type "type"
1281 * at position "pos" of "aff".
1283 * A NaN is unaffected by this operation.
1285 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1286 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1288 if (!aff || !v)
1289 goto error;
1291 if (isl_val_is_zero(v)) {
1292 isl_val_free(v);
1293 return aff;
1296 if (type == isl_dim_out)
1297 isl_die(aff->v->ctx, isl_error_invalid,
1298 "output/set dimension does not have a coefficient",
1299 goto error);
1300 if (type == isl_dim_in)
1301 type = isl_dim_set;
1303 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1304 goto error;
1306 if (isl_aff_is_nan(aff)) {
1307 isl_val_free(v);
1308 return aff;
1310 if (!isl_val_is_rat(v))
1311 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1312 "expecting rational value", goto error);
1314 aff = isl_aff_cow(aff);
1315 if (!aff)
1316 goto error;
1318 aff->v = isl_vec_cow(aff->v);
1319 if (!aff->v)
1320 goto error;
1322 pos += isl_local_space_offset(aff->ls, type);
1323 if (isl_int_is_one(v->d)) {
1324 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1325 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1326 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1327 aff->v = isl_vec_normalize(aff->v);
1328 if (!aff->v)
1329 goto error;
1330 } else {
1331 isl_seq_scale(aff->v->el + 1,
1332 aff->v->el + 1, v->d, aff->v->size - 1);
1333 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1334 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1335 aff->v = isl_vec_normalize(aff->v);
1336 if (!aff->v)
1337 goto error;
1340 isl_val_free(v);
1341 return aff;
1342 error:
1343 isl_aff_free(aff);
1344 isl_val_free(v);
1345 return NULL;
1348 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1349 enum isl_dim_type type, int pos, int v)
1351 isl_int t;
1353 isl_int_init(t);
1354 isl_int_set_si(t, v);
1355 aff = isl_aff_add_coefficient(aff, type, pos, t);
1356 isl_int_clear(t);
1358 return aff;
1361 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1363 if (!aff)
1364 return NULL;
1366 return isl_local_space_get_div(aff->ls, pos);
1369 /* Return the negation of "aff".
1371 * As a special case, -NaN = NaN.
1373 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1375 if (!aff)
1376 return NULL;
1377 if (isl_aff_is_nan(aff))
1378 return aff;
1379 aff = isl_aff_cow(aff);
1380 if (!aff)
1381 return NULL;
1382 aff->v = isl_vec_cow(aff->v);
1383 if (!aff->v)
1384 return isl_aff_free(aff);
1386 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1388 return aff;
1391 /* Remove divs from the local space that do not appear in the affine
1392 * expression.
1393 * We currently only remove divs at the end.
1394 * Some intermediate divs may also not appear directly in the affine
1395 * expression, but we would also need to check that no other divs are
1396 * defined in terms of them.
1398 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1400 int pos;
1401 isl_size off;
1402 isl_size n;
1404 n = isl_aff_domain_dim(aff, isl_dim_div);
1405 off = isl_aff_domain_offset(aff, isl_dim_div);
1406 if (n < 0 || off < 0)
1407 return isl_aff_free(aff);
1409 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1410 if (pos == n)
1411 return aff;
1413 aff = isl_aff_cow(aff);
1414 if (!aff)
1415 return NULL;
1417 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1418 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1419 if (!aff->ls || !aff->v)
1420 return isl_aff_free(aff);
1422 return aff;
1425 /* Look for any divs in the aff->ls with a denominator equal to one
1426 * and plug them into the affine expression and any subsequent divs
1427 * that may reference the div.
1429 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1431 int i;
1432 isl_size n;
1433 int len;
1434 isl_int v;
1435 isl_vec *vec;
1436 isl_local_space *ls;
1437 isl_size off;
1439 n = isl_aff_domain_dim(aff, isl_dim_div);
1440 off = isl_aff_domain_offset(aff, isl_dim_div);
1441 if (n < 0 || off < 0)
1442 return isl_aff_free(aff);
1443 len = aff->v->size;
1444 for (i = 0; i < n; ++i) {
1445 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1446 continue;
1447 ls = isl_local_space_copy(aff->ls);
1448 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1449 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1450 vec = isl_vec_copy(aff->v);
1451 vec = isl_vec_cow(vec);
1452 if (!ls || !vec)
1453 goto error;
1455 isl_int_init(v);
1457 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1458 len, len, v);
1460 isl_int_clear(v);
1462 isl_vec_free(aff->v);
1463 aff->v = vec;
1464 isl_local_space_free(aff->ls);
1465 aff->ls = ls;
1468 return aff;
1469 error:
1470 isl_vec_free(vec);
1471 isl_local_space_free(ls);
1472 return isl_aff_free(aff);
1475 /* Look for any divs j that appear with a unit coefficient inside
1476 * the definitions of other divs i and plug them into the definitions
1477 * of the divs i.
1479 * In particular, an expression of the form
1481 * floor((f(..) + floor(g(..)/n))/m)
1483 * is simplified to
1485 * floor((n * f(..) + g(..))/(n * m))
1487 * This simplification is correct because we can move the expression
1488 * f(..) into the inner floor in the original expression to obtain
1490 * floor(floor((n * f(..) + g(..))/n)/m)
1492 * from which we can derive the simplified expression.
1494 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1496 int i, j;
1497 isl_size n;
1498 isl_size off;
1500 n = isl_aff_domain_dim(aff, isl_dim_div);
1501 off = isl_aff_domain_offset(aff, isl_dim_div);
1502 if (n < 0 || off < 0)
1503 return isl_aff_free(aff);
1504 for (i = 1; i < n; ++i) {
1505 for (j = 0; j < i; ++j) {
1506 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1507 continue;
1508 aff->ls = isl_local_space_substitute_seq(aff->ls,
1509 isl_dim_div, j, aff->ls->div->row[j],
1510 aff->v->size, i, 1);
1511 if (!aff->ls)
1512 return isl_aff_free(aff);
1516 return aff;
1519 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1521 * Even though this function is only called on isl_affs with a single
1522 * reference, we are careful to only change aff->v and aff->ls together.
1524 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1526 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1527 isl_local_space *ls;
1528 isl_vec *v;
1530 if (off < 0)
1531 return isl_aff_free(aff);
1533 ls = isl_local_space_copy(aff->ls);
1534 ls = isl_local_space_swap_div(ls, a, b);
1535 v = isl_vec_copy(aff->v);
1536 v = isl_vec_cow(v);
1537 if (!ls || !v)
1538 goto error;
1540 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1541 isl_vec_free(aff->v);
1542 aff->v = v;
1543 isl_local_space_free(aff->ls);
1544 aff->ls = ls;
1546 return aff;
1547 error:
1548 isl_vec_free(v);
1549 isl_local_space_free(ls);
1550 return isl_aff_free(aff);
1553 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1555 * We currently do not actually remove div "b", but simply add its
1556 * coefficient to that of "a" and then zero it out.
1558 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1560 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1562 if (off < 0)
1563 return isl_aff_free(aff);
1565 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1566 return aff;
1568 aff->v = isl_vec_cow(aff->v);
1569 if (!aff->v)
1570 return isl_aff_free(aff);
1572 isl_int_add(aff->v->el[1 + off + a],
1573 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1574 isl_int_set_si(aff->v->el[1 + off + b], 0);
1576 return aff;
1579 /* Sort the divs in the local space of "aff" according to
1580 * the comparison function "cmp_row" in isl_local_space.c,
1581 * combining the coefficients of identical divs.
1583 * Reordering divs does not change the semantics of "aff",
1584 * so there is no need to call isl_aff_cow.
1585 * Moreover, this function is currently only called on isl_affs
1586 * with a single reference.
1588 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1590 isl_size n;
1591 int i, j;
1593 n = isl_aff_dim(aff, isl_dim_div);
1594 if (n < 0)
1595 return isl_aff_free(aff);
1596 for (i = 1; i < n; ++i) {
1597 for (j = i - 1; j >= 0; --j) {
1598 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1599 if (cmp < 0)
1600 break;
1601 if (cmp == 0)
1602 aff = merge_divs(aff, j, j + 1);
1603 else
1604 aff = swap_div(aff, j, j + 1);
1605 if (!aff)
1606 return NULL;
1610 return aff;
1613 /* Normalize the representation of "aff".
1615 * This function should only be called on "new" isl_affs, i.e.,
1616 * with only a single reference. We therefore do not need to
1617 * worry about affecting other instances.
1619 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1621 if (!aff)
1622 return NULL;
1623 aff->v = isl_vec_normalize(aff->v);
1624 if (!aff->v)
1625 return isl_aff_free(aff);
1626 aff = plug_in_integral_divs(aff);
1627 aff = plug_in_unit_divs(aff);
1628 aff = sort_divs(aff);
1629 aff = isl_aff_remove_unused_divs(aff);
1630 return aff;
1633 /* Given f, return floor(f).
1634 * If f is an integer expression, then just return f.
1635 * If f is a constant, then return the constant floor(f).
1636 * Otherwise, if f = g/m, write g = q m + r,
1637 * create a new div d = [r/m] and return the expression q + d.
1638 * The coefficients in r are taken to lie between -m/2 and m/2.
1640 * reduce_div_coefficients performs the same normalization.
1642 * As a special case, floor(NaN) = NaN.
1644 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1646 int i;
1647 int size;
1648 isl_ctx *ctx;
1649 isl_vec *div;
1651 if (!aff)
1652 return NULL;
1654 if (isl_aff_is_nan(aff))
1655 return aff;
1656 if (isl_int_is_one(aff->v->el[0]))
1657 return aff;
1659 aff = isl_aff_cow(aff);
1660 if (!aff)
1661 return NULL;
1663 aff->v = isl_vec_cow(aff->v);
1664 if (!aff->v)
1665 return isl_aff_free(aff);
1667 if (isl_aff_is_cst(aff)) {
1668 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1669 isl_int_set_si(aff->v->el[0], 1);
1670 return aff;
1673 div = isl_vec_copy(aff->v);
1674 div = isl_vec_cow(div);
1675 if (!div)
1676 return isl_aff_free(aff);
1678 ctx = isl_aff_get_ctx(aff);
1679 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1680 for (i = 1; i < aff->v->size; ++i) {
1681 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1682 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1683 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1684 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1685 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1689 aff->ls = isl_local_space_add_div(aff->ls, div);
1690 if (!aff->ls)
1691 return isl_aff_free(aff);
1693 size = aff->v->size;
1694 aff->v = isl_vec_extend(aff->v, size + 1);
1695 if (!aff->v)
1696 return isl_aff_free(aff);
1697 isl_int_set_si(aff->v->el[0], 1);
1698 isl_int_set_si(aff->v->el[size], 1);
1700 aff = isl_aff_normalize(aff);
1702 return aff;
1705 /* Compute
1707 * aff mod m = aff - m * floor(aff/m)
1709 * with m an integer value.
1711 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1712 __isl_take isl_val *m)
1714 isl_aff *res;
1716 if (!aff || !m)
1717 goto error;
1719 if (!isl_val_is_int(m))
1720 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1721 "expecting integer modulo", goto error);
1723 res = isl_aff_copy(aff);
1724 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1725 aff = isl_aff_floor(aff);
1726 aff = isl_aff_scale_val(aff, m);
1727 res = isl_aff_sub(res, aff);
1729 return res;
1730 error:
1731 isl_aff_free(aff);
1732 isl_val_free(m);
1733 return NULL;
1736 /* Compute
1738 * pwaff mod m = pwaff - m * floor(pwaff/m)
1740 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1742 isl_pw_aff *res;
1744 res = isl_pw_aff_copy(pwaff);
1745 pwaff = isl_pw_aff_scale_down(pwaff, m);
1746 pwaff = isl_pw_aff_floor(pwaff);
1747 pwaff = isl_pw_aff_scale(pwaff, m);
1748 res = isl_pw_aff_sub(res, pwaff);
1750 return res;
1753 /* Compute
1755 * pa mod m = pa - m * floor(pa/m)
1757 * with m an integer value.
1759 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1760 __isl_take isl_val *m)
1762 if (!pa || !m)
1763 goto error;
1764 if (!isl_val_is_int(m))
1765 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1766 "expecting integer modulo", goto error);
1767 pa = isl_pw_aff_mod(pa, m->n);
1768 isl_val_free(m);
1769 return pa;
1770 error:
1771 isl_pw_aff_free(pa);
1772 isl_val_free(m);
1773 return NULL;
1776 /* Given f, return ceil(f).
1777 * If f is an integer expression, then just return f.
1778 * Otherwise, let f be the expression
1780 * e/m
1782 * then return
1784 * floor((e + m - 1)/m)
1786 * As a special case, ceil(NaN) = NaN.
1788 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1790 if (!aff)
1791 return NULL;
1793 if (isl_aff_is_nan(aff))
1794 return aff;
1795 if (isl_int_is_one(aff->v->el[0]))
1796 return aff;
1798 aff = isl_aff_cow(aff);
1799 if (!aff)
1800 return NULL;
1801 aff->v = isl_vec_cow(aff->v);
1802 if (!aff->v)
1803 return isl_aff_free(aff);
1805 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1806 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1807 aff = isl_aff_floor(aff);
1809 return aff;
1812 /* Apply the expansion computed by isl_merge_divs.
1813 * The expansion itself is given by "exp" while the resulting
1814 * list of divs is given by "div".
1816 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1817 __isl_take isl_mat *div, int *exp)
1819 isl_size old_n_div;
1820 isl_size new_n_div;
1821 isl_size offset;
1823 aff = isl_aff_cow(aff);
1825 offset = isl_aff_domain_offset(aff, isl_dim_div);
1826 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1827 new_n_div = isl_mat_rows(div);
1828 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1829 goto error;
1831 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1832 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1833 if (!aff->v || !aff->ls)
1834 return isl_aff_free(aff);
1835 return aff;
1836 error:
1837 isl_aff_free(aff);
1838 isl_mat_free(div);
1839 return NULL;
1842 /* Add two affine expressions that live in the same local space.
1844 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1845 __isl_take isl_aff *aff2)
1847 isl_int gcd, f;
1849 aff1 = isl_aff_cow(aff1);
1850 if (!aff1 || !aff2)
1851 goto error;
1853 aff1->v = isl_vec_cow(aff1->v);
1854 if (!aff1->v)
1855 goto error;
1857 isl_int_init(gcd);
1858 isl_int_init(f);
1859 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1860 isl_int_divexact(f, aff2->v->el[0], gcd);
1861 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1862 isl_int_divexact(f, aff1->v->el[0], gcd);
1863 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1864 isl_int_divexact(f, aff2->v->el[0], gcd);
1865 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1866 isl_int_clear(f);
1867 isl_int_clear(gcd);
1869 isl_aff_free(aff2);
1870 aff1 = isl_aff_normalize(aff1);
1871 return aff1;
1872 error:
1873 isl_aff_free(aff1);
1874 isl_aff_free(aff2);
1875 return NULL;
1878 /* Replace one of the arguments by a NaN and free the other one.
1880 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1881 __isl_take isl_aff *aff2)
1883 isl_aff_free(aff2);
1884 return isl_aff_set_nan(aff1);
1887 /* Return the sum of "aff1" and "aff2".
1889 * If either of the two is NaN, then the result is NaN.
1891 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1892 __isl_take isl_aff *aff2)
1894 isl_ctx *ctx;
1895 int *exp1 = NULL;
1896 int *exp2 = NULL;
1897 isl_mat *div;
1898 isl_size n_div1, n_div2;
1900 if (!aff1 || !aff2)
1901 goto error;
1903 ctx = isl_aff_get_ctx(aff1);
1904 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1905 isl_die(ctx, isl_error_invalid,
1906 "spaces don't match", goto error);
1908 if (isl_aff_is_nan(aff1)) {
1909 isl_aff_free(aff2);
1910 return aff1;
1912 if (isl_aff_is_nan(aff2)) {
1913 isl_aff_free(aff1);
1914 return aff2;
1917 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1918 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1919 if (n_div1 < 0 || n_div2 < 0)
1920 goto error;
1921 if (n_div1 == 0 && n_div2 == 0)
1922 return add_expanded(aff1, aff2);
1924 exp1 = isl_alloc_array(ctx, int, n_div1);
1925 exp2 = isl_alloc_array(ctx, int, n_div2);
1926 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1927 goto error;
1929 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1930 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1931 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1932 free(exp1);
1933 free(exp2);
1935 return add_expanded(aff1, aff2);
1936 error:
1937 free(exp1);
1938 free(exp2);
1939 isl_aff_free(aff1);
1940 isl_aff_free(aff2);
1941 return NULL;
1944 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1945 __isl_take isl_aff *aff2)
1947 return isl_aff_add(aff1, isl_aff_neg(aff2));
1950 /* Return the result of scaling "aff" by a factor of "f".
1952 * As a special case, f * NaN = NaN.
1954 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1956 isl_int gcd;
1958 if (!aff)
1959 return NULL;
1960 if (isl_aff_is_nan(aff))
1961 return aff;
1963 if (isl_int_is_one(f))
1964 return aff;
1966 aff = isl_aff_cow(aff);
1967 if (!aff)
1968 return NULL;
1969 aff->v = isl_vec_cow(aff->v);
1970 if (!aff->v)
1971 return isl_aff_free(aff);
1973 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1974 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1975 return aff;
1978 isl_int_init(gcd);
1979 isl_int_gcd(gcd, aff->v->el[0], f);
1980 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1981 isl_int_divexact(gcd, f, gcd);
1982 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1983 isl_int_clear(gcd);
1985 return aff;
1988 /* Multiple "aff" by "v".
1990 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1991 __isl_take isl_val *v)
1993 if (!aff || !v)
1994 goto error;
1996 if (isl_val_is_one(v)) {
1997 isl_val_free(v);
1998 return aff;
2001 if (!isl_val_is_rat(v))
2002 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2003 "expecting rational factor", goto error);
2005 aff = isl_aff_scale(aff, v->n);
2006 aff = isl_aff_scale_down(aff, v->d);
2008 isl_val_free(v);
2009 return aff;
2010 error:
2011 isl_aff_free(aff);
2012 isl_val_free(v);
2013 return NULL;
2016 /* Return the result of scaling "aff" down by a factor of "f".
2018 * As a special case, NaN/f = NaN.
2020 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2022 isl_int gcd;
2024 if (!aff)
2025 return NULL;
2026 if (isl_aff_is_nan(aff))
2027 return aff;
2029 if (isl_int_is_one(f))
2030 return aff;
2032 aff = isl_aff_cow(aff);
2033 if (!aff)
2034 return NULL;
2036 if (isl_int_is_zero(f))
2037 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2038 "cannot scale down by zero", return isl_aff_free(aff));
2040 aff->v = isl_vec_cow(aff->v);
2041 if (!aff->v)
2042 return isl_aff_free(aff);
2044 isl_int_init(gcd);
2045 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2046 isl_int_gcd(gcd, gcd, f);
2047 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2048 isl_int_divexact(gcd, f, gcd);
2049 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2050 isl_int_clear(gcd);
2052 return aff;
2055 /* Divide "aff" by "v".
2057 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2058 __isl_take isl_val *v)
2060 if (!aff || !v)
2061 goto error;
2063 if (isl_val_is_one(v)) {
2064 isl_val_free(v);
2065 return aff;
2068 if (!isl_val_is_rat(v))
2069 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2070 "expecting rational factor", goto error);
2071 if (!isl_val_is_pos(v))
2072 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2073 "factor needs to be positive", goto error);
2075 aff = isl_aff_scale(aff, v->d);
2076 aff = isl_aff_scale_down(aff, v->n);
2078 isl_val_free(v);
2079 return aff;
2080 error:
2081 isl_aff_free(aff);
2082 isl_val_free(v);
2083 return NULL;
2086 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2088 isl_int v;
2090 if (f == 1)
2091 return aff;
2093 isl_int_init(v);
2094 isl_int_set_ui(v, f);
2095 aff = isl_aff_scale_down(aff, v);
2096 isl_int_clear(v);
2098 return aff;
2101 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2102 enum isl_dim_type type, unsigned pos, const char *s)
2104 aff = isl_aff_cow(aff);
2105 if (!aff)
2106 return NULL;
2107 if (type == isl_dim_out)
2108 isl_die(aff->v->ctx, isl_error_invalid,
2109 "cannot set name of output/set dimension",
2110 return isl_aff_free(aff));
2111 if (type == isl_dim_in)
2112 type = isl_dim_set;
2113 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2114 if (!aff->ls)
2115 return isl_aff_free(aff);
2117 return aff;
2120 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2121 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2123 aff = isl_aff_cow(aff);
2124 if (!aff)
2125 goto error;
2126 if (type == isl_dim_out)
2127 isl_die(aff->v->ctx, isl_error_invalid,
2128 "cannot set name of output/set dimension",
2129 goto error);
2130 if (type == isl_dim_in)
2131 type = isl_dim_set;
2132 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2133 if (!aff->ls)
2134 return isl_aff_free(aff);
2136 return aff;
2137 error:
2138 isl_id_free(id);
2139 isl_aff_free(aff);
2140 return NULL;
2143 /* Replace the identifier of the input tuple of "aff" by "id".
2144 * type is currently required to be equal to isl_dim_in
2146 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2147 enum isl_dim_type type, __isl_take isl_id *id)
2149 aff = isl_aff_cow(aff);
2150 if (!aff)
2151 goto error;
2152 if (type != isl_dim_in)
2153 isl_die(aff->v->ctx, isl_error_invalid,
2154 "cannot only set id of input tuple", goto error);
2155 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2156 if (!aff->ls)
2157 return isl_aff_free(aff);
2159 return aff;
2160 error:
2161 isl_id_free(id);
2162 isl_aff_free(aff);
2163 return NULL;
2166 /* Exploit the equalities in "eq" to simplify the affine expression
2167 * and the expressions of the integer divisions in the local space.
2168 * The integer divisions in this local space are assumed to appear
2169 * as regular dimensions in "eq".
2171 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2172 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2174 int i, j;
2175 unsigned o_div;
2176 unsigned n_div;
2178 if (!eq)
2179 goto error;
2180 if (eq->n_eq == 0) {
2181 isl_basic_set_free(eq);
2182 return aff;
2185 aff = isl_aff_cow(aff);
2186 if (!aff)
2187 goto error;
2189 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2190 isl_basic_set_copy(eq));
2191 aff->v = isl_vec_cow(aff->v);
2192 if (!aff->ls || !aff->v)
2193 goto error;
2195 o_div = isl_basic_set_offset(eq, isl_dim_div);
2196 n_div = eq->n_div;
2197 for (i = 0; i < eq->n_eq; ++i) {
2198 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2199 if (j < 0 || j == 0 || j >= o_div)
2200 continue;
2202 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2203 &aff->v->el[0]);
2206 isl_basic_set_free(eq);
2207 aff = isl_aff_normalize(aff);
2208 return aff;
2209 error:
2210 isl_basic_set_free(eq);
2211 isl_aff_free(aff);
2212 return NULL;
2215 /* Exploit the equalities in "eq" to simplify the affine expression
2216 * and the expressions of the integer divisions in the local space.
2218 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2219 __isl_take isl_basic_set *eq)
2221 isl_size n_div;
2223 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2224 if (n_div < 0)
2225 goto error;
2226 if (n_div > 0)
2227 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2228 return isl_aff_substitute_equalities_lifted(aff, eq);
2229 error:
2230 isl_basic_set_free(eq);
2231 isl_aff_free(aff);
2232 return NULL;
2235 /* Look for equalities among the variables shared by context and aff
2236 * and the integer divisions of aff, if any.
2237 * The equalities are then used to eliminate coefficients and/or integer
2238 * divisions from aff.
2240 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2241 __isl_take isl_set *context)
2243 isl_local_space *ls;
2244 isl_basic_set *hull;
2246 ls = isl_aff_get_domain_local_space(aff);
2247 context = isl_local_space_lift_set(ls, context);
2249 hull = isl_set_affine_hull(context);
2250 return isl_aff_substitute_equalities_lifted(aff, hull);
2253 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2254 __isl_take isl_set *context)
2256 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2257 dom_context = isl_set_intersect_params(dom_context, context);
2258 return isl_aff_gist(aff, dom_context);
2261 /* Return a basic set containing those elements in the space
2262 * of aff where it is positive. "rational" should not be set.
2264 * If "aff" is NaN, then it is not positive.
2266 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2267 int rational, void *user)
2269 isl_constraint *ineq;
2270 isl_basic_set *bset;
2271 isl_val *c;
2273 if (!aff)
2274 return NULL;
2275 if (isl_aff_is_nan(aff)) {
2276 isl_space *space = isl_aff_get_domain_space(aff);
2277 isl_aff_free(aff);
2278 return isl_basic_set_empty(space);
2280 if (rational)
2281 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2282 "rational sets not supported", goto error);
2284 ineq = isl_inequality_from_aff(aff);
2285 c = isl_constraint_get_constant_val(ineq);
2286 c = isl_val_sub_ui(c, 1);
2287 ineq = isl_constraint_set_constant_val(ineq, c);
2289 bset = isl_basic_set_from_constraint(ineq);
2290 bset = isl_basic_set_simplify(bset);
2291 return bset;
2292 error:
2293 isl_aff_free(aff);
2294 return NULL;
2297 /* Return a basic set containing those elements in the space
2298 * of aff where it is non-negative.
2299 * If "rational" is set, then return a rational basic set.
2301 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2303 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2304 __isl_take isl_aff *aff, int rational, void *user)
2306 isl_constraint *ineq;
2307 isl_basic_set *bset;
2309 if (!aff)
2310 return NULL;
2311 if (isl_aff_is_nan(aff)) {
2312 isl_space *space = isl_aff_get_domain_space(aff);
2313 isl_aff_free(aff);
2314 return isl_basic_set_empty(space);
2317 ineq = isl_inequality_from_aff(aff);
2319 bset = isl_basic_set_from_constraint(ineq);
2320 if (rational)
2321 bset = isl_basic_set_set_rational(bset);
2322 bset = isl_basic_set_simplify(bset);
2323 return bset;
2326 /* Return a basic set containing those elements in the space
2327 * of aff where it is non-negative.
2329 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2331 return aff_nonneg_basic_set(aff, 0, NULL);
2334 /* Return a basic set containing those elements in the domain space
2335 * of "aff" where it is positive.
2337 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2339 aff = isl_aff_add_constant_num_si(aff, -1);
2340 return isl_aff_nonneg_basic_set(aff);
2343 /* Return a basic set containing those elements in the domain space
2344 * of aff where it is negative.
2346 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2348 aff = isl_aff_neg(aff);
2349 return isl_aff_pos_basic_set(aff);
2352 /* Return a basic set containing those elements in the space
2353 * of aff where it is zero.
2354 * If "rational" is set, then return a rational basic set.
2356 * If "aff" is NaN, then it is not zero.
2358 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2359 int rational, void *user)
2361 isl_constraint *ineq;
2362 isl_basic_set *bset;
2364 if (!aff)
2365 return NULL;
2366 if (isl_aff_is_nan(aff)) {
2367 isl_space *space = isl_aff_get_domain_space(aff);
2368 isl_aff_free(aff);
2369 return isl_basic_set_empty(space);
2372 ineq = isl_equality_from_aff(aff);
2374 bset = isl_basic_set_from_constraint(ineq);
2375 if (rational)
2376 bset = isl_basic_set_set_rational(bset);
2377 bset = isl_basic_set_simplify(bset);
2378 return bset;
2381 /* Return a basic set containing those elements in the space
2382 * of aff where it is zero.
2384 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2386 return aff_zero_basic_set(aff, 0, NULL);
2389 /* Return a basic set containing those elements in the shared space
2390 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2392 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2393 __isl_take isl_aff *aff2)
2395 aff1 = isl_aff_sub(aff1, aff2);
2397 return isl_aff_nonneg_basic_set(aff1);
2400 /* Return a basic set containing those elements in the shared domain space
2401 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2403 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2404 __isl_take isl_aff *aff2)
2406 aff1 = isl_aff_sub(aff1, aff2);
2408 return isl_aff_pos_basic_set(aff1);
2411 /* Return a set containing those elements in the shared space
2412 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2414 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2415 __isl_take isl_aff *aff2)
2417 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2420 /* Return a set containing those elements in the shared domain space
2421 * of aff1 and aff2 where aff1 is greater than aff2.
2423 * If either of the two inputs is NaN, then the result is empty,
2424 * as comparisons with NaN always return false.
2426 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2427 __isl_take isl_aff *aff2)
2429 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2432 /* Return a basic set containing those elements in the shared space
2433 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2435 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2436 __isl_take isl_aff *aff2)
2438 return isl_aff_ge_basic_set(aff2, aff1);
2441 /* Return a basic set containing those elements in the shared domain space
2442 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2444 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2445 __isl_take isl_aff *aff2)
2447 return isl_aff_gt_basic_set(aff2, aff1);
2450 /* Return a set containing those elements in the shared space
2451 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2453 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2454 __isl_take isl_aff *aff2)
2456 return isl_aff_ge_set(aff2, aff1);
2459 /* Return a set containing those elements in the shared domain space
2460 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2462 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2463 __isl_take isl_aff *aff2)
2465 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2468 /* Return a basic set containing those elements in the shared space
2469 * of aff1 and aff2 where aff1 and aff2 are equal.
2471 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2472 __isl_take isl_aff *aff2)
2474 aff1 = isl_aff_sub(aff1, aff2);
2476 return isl_aff_zero_basic_set(aff1);
2479 /* Return a set containing those elements in the shared space
2480 * of aff1 and aff2 where aff1 and aff2 are equal.
2482 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2483 __isl_take isl_aff *aff2)
2485 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2488 /* Return a set containing those elements in the shared domain space
2489 * of aff1 and aff2 where aff1 and aff2 are not equal.
2491 * If either of the two inputs is NaN, then the result is empty,
2492 * as comparisons with NaN always return false.
2494 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2495 __isl_take isl_aff *aff2)
2497 isl_set *set_lt, *set_gt;
2499 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2500 isl_aff_copy(aff2));
2501 set_gt = isl_aff_gt_set(aff1, aff2);
2502 return isl_set_union_disjoint(set_lt, set_gt);
2505 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2506 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2508 aff1 = isl_aff_add(aff1, aff2);
2509 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2510 return aff1;
2513 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2515 if (!aff)
2516 return isl_bool_error;
2518 return isl_bool_false;
2521 #undef TYPE
2522 #define TYPE isl_aff
2523 static
2524 #include "check_type_range_templ.c"
2526 /* Check whether the given affine expression has non-zero coefficient
2527 * for any dimension in the given range or if any of these dimensions
2528 * appear with non-zero coefficients in any of the integer divisions
2529 * involved in the affine expression.
2531 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2532 enum isl_dim_type type, unsigned first, unsigned n)
2534 int i;
2535 int *active = NULL;
2536 isl_bool involves = isl_bool_false;
2538 if (!aff)
2539 return isl_bool_error;
2540 if (n == 0)
2541 return isl_bool_false;
2542 if (isl_aff_check_range(aff, type, first, n) < 0)
2543 return isl_bool_error;
2545 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2546 if (!active)
2547 goto error;
2549 first += isl_local_space_offset(aff->ls, type) - 1;
2550 for (i = 0; i < n; ++i)
2551 if (active[first + i]) {
2552 involves = isl_bool_true;
2553 break;
2556 free(active);
2558 return involves;
2559 error:
2560 free(active);
2561 return isl_bool_error;
2564 /* Does "aff" involve any local variables, i.e., integer divisions?
2566 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2568 isl_size n;
2570 n = isl_aff_dim(aff, isl_dim_div);
2571 if (n < 0)
2572 return isl_bool_error;
2573 return isl_bool_ok(n > 0);
2576 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2577 enum isl_dim_type type, unsigned first, unsigned n)
2579 isl_ctx *ctx;
2581 if (!aff)
2582 return NULL;
2583 if (type == isl_dim_out)
2584 isl_die(aff->v->ctx, isl_error_invalid,
2585 "cannot drop output/set dimension",
2586 return isl_aff_free(aff));
2587 if (type == isl_dim_in)
2588 type = isl_dim_set;
2589 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2590 return aff;
2592 ctx = isl_aff_get_ctx(aff);
2593 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2594 return isl_aff_free(aff);
2596 aff = isl_aff_cow(aff);
2597 if (!aff)
2598 return NULL;
2600 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2601 if (!aff->ls)
2602 return isl_aff_free(aff);
2604 first += 1 + isl_local_space_offset(aff->ls, type);
2605 aff->v = isl_vec_drop_els(aff->v, first, n);
2606 if (!aff->v)
2607 return isl_aff_free(aff);
2609 return aff;
2612 /* Is the domain of "aff" a product?
2614 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2616 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2619 #undef TYPE
2620 #define TYPE isl_aff
2621 #include <isl_domain_factor_templ.c>
2623 /* Project the domain of the affine expression onto its parameter space.
2624 * The affine expression may not involve any of the domain dimensions.
2626 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2628 isl_space *space;
2629 isl_size n;
2631 n = isl_aff_dim(aff, isl_dim_in);
2632 if (n < 0)
2633 return isl_aff_free(aff);
2634 aff = isl_aff_drop_domain(aff, 0, n);
2635 space = isl_aff_get_domain_space(aff);
2636 space = isl_space_params(space);
2637 aff = isl_aff_reset_domain_space(aff, space);
2638 return aff;
2641 /* Convert an affine expression defined over a parameter domain
2642 * into one that is defined over a zero-dimensional set.
2644 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2646 isl_local_space *ls;
2648 ls = isl_aff_take_domain_local_space(aff);
2649 ls = isl_local_space_set_from_params(ls);
2650 aff = isl_aff_restore_domain_local_space(aff, ls);
2652 return aff;
2655 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2656 enum isl_dim_type type, unsigned first, unsigned n)
2658 isl_ctx *ctx;
2660 if (!aff)
2661 return NULL;
2662 if (type == isl_dim_out)
2663 isl_die(aff->v->ctx, isl_error_invalid,
2664 "cannot insert output/set dimensions",
2665 return isl_aff_free(aff));
2666 if (type == isl_dim_in)
2667 type = isl_dim_set;
2668 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2669 return aff;
2671 ctx = isl_aff_get_ctx(aff);
2672 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2673 return isl_aff_free(aff);
2675 aff = isl_aff_cow(aff);
2676 if (!aff)
2677 return NULL;
2679 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2680 if (!aff->ls)
2681 return isl_aff_free(aff);
2683 first += 1 + isl_local_space_offset(aff->ls, type);
2684 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2685 if (!aff->v)
2686 return isl_aff_free(aff);
2688 return aff;
2691 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2692 enum isl_dim_type type, unsigned n)
2694 isl_size pos;
2696 pos = isl_aff_dim(aff, type);
2697 if (pos < 0)
2698 return isl_aff_free(aff);
2700 return isl_aff_insert_dims(aff, type, pos, n);
2703 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2704 * to dimensions of "dst_type" at "dst_pos".
2706 * We only support moving input dimensions to parameters and vice versa.
2708 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2709 enum isl_dim_type dst_type, unsigned dst_pos,
2710 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2712 unsigned g_dst_pos;
2713 unsigned g_src_pos;
2714 isl_size src_off, dst_off;
2716 if (!aff)
2717 return NULL;
2718 if (n == 0 &&
2719 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2720 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2721 return aff;
2723 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2724 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2725 "cannot move output/set dimension",
2726 return isl_aff_free(aff));
2727 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2728 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2729 "cannot move divs", return isl_aff_free(aff));
2730 if (dst_type == isl_dim_in)
2731 dst_type = isl_dim_set;
2732 if (src_type == isl_dim_in)
2733 src_type = isl_dim_set;
2735 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2736 return isl_aff_free(aff);
2737 if (dst_type == src_type)
2738 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2739 "moving dims within the same type not supported",
2740 return isl_aff_free(aff));
2742 aff = isl_aff_cow(aff);
2743 src_off = isl_aff_domain_offset(aff, src_type);
2744 dst_off = isl_aff_domain_offset(aff, dst_type);
2745 if (src_off < 0 || dst_off < 0)
2746 return isl_aff_free(aff);
2748 g_src_pos = 1 + src_off + src_pos;
2749 g_dst_pos = 1 + dst_off + dst_pos;
2750 if (dst_type > src_type)
2751 g_dst_pos -= n;
2753 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2754 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2755 src_type, src_pos, n);
2756 if (!aff->v || !aff->ls)
2757 return isl_aff_free(aff);
2759 aff = sort_divs(aff);
2761 return aff;
2764 /* Return a zero isl_aff in the given space.
2766 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2767 * interface over all piecewise types.
2769 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2771 isl_local_space *ls;
2773 ls = isl_local_space_from_space(isl_space_domain(space));
2774 return isl_aff_zero_on_domain(ls);
2777 #define isl_aff_involves_nan isl_aff_is_nan
2779 #undef PW
2780 #define PW isl_pw_aff
2781 #undef BASE
2782 #define BASE aff
2783 #undef EL_IS_ZERO
2784 #define EL_IS_ZERO is_empty
2785 #undef ZERO
2786 #define ZERO empty
2787 #undef IS_ZERO
2788 #define IS_ZERO is_empty
2789 #undef FIELD
2790 #define FIELD aff
2791 #undef DEFAULT_IS_ZERO
2792 #define DEFAULT_IS_ZERO 0
2794 #include <isl_pw_templ.c>
2795 #include <isl_pw_add_constant_val_templ.c>
2796 #include <isl_pw_bind_domain_templ.c>
2797 #include <isl_pw_eval.c>
2798 #include <isl_pw_hash.c>
2799 #include <isl_pw_insert_dims_templ.c>
2800 #include <isl_pw_insert_domain_templ.c>
2801 #include <isl_pw_move_dims_templ.c>
2802 #include <isl_pw_neg_templ.c>
2803 #include <isl_pw_pullback_templ.c>
2804 #include <isl_pw_sub_templ.c>
2805 #include <isl_pw_union_opt.c>
2807 #undef BASE
2808 #define BASE pw_aff
2810 #include <isl_union_single.c>
2811 #include <isl_union_neg.c>
2813 #undef BASE
2814 #define BASE aff
2816 #include <isl_union_pw_templ.c>
2818 /* Compute a piecewise quasi-affine expression with a domain that
2819 * is the union of those of pwaff1 and pwaff2 and such that on each
2820 * cell, the quasi-affine expression is the maximum of those of pwaff1
2821 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2822 * cell, then the associated expression is the defined one.
2824 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2825 __isl_take isl_pw_aff *pwaff2)
2827 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2828 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2831 /* Compute a piecewise quasi-affine expression with a domain that
2832 * is the union of those of pwaff1 and pwaff2 and such that on each
2833 * cell, the quasi-affine expression is the minimum of those of pwaff1
2834 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2835 * cell, then the associated expression is the defined one.
2837 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2838 __isl_take isl_pw_aff *pwaff2)
2840 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2841 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2844 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2845 __isl_take isl_pw_aff *pwaff2, int max)
2847 if (max)
2848 return isl_pw_aff_union_max(pwaff1, pwaff2);
2849 else
2850 return isl_pw_aff_union_min(pwaff1, pwaff2);
2853 /* Is the domain of "pa" a product?
2855 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2857 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2860 #undef TYPE
2861 #define TYPE isl_pw_aff
2862 #include <isl_domain_factor_templ.c>
2864 /* Return a set containing those elements in the domain
2865 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2866 * does not satisfy "fn" (if complement is 1).
2868 * The pieces with a NaN never belong to the result since
2869 * NaN does not satisfy any property.
2871 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2872 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2873 void *user),
2874 int complement, void *user)
2876 int i;
2877 isl_set *set;
2879 if (!pwaff)
2880 return NULL;
2882 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2884 for (i = 0; i < pwaff->n; ++i) {
2885 isl_basic_set *bset;
2886 isl_set *set_i, *locus;
2887 isl_bool rational;
2889 if (isl_aff_is_nan(pwaff->p[i].aff))
2890 continue;
2892 rational = isl_set_has_rational(pwaff->p[i].set);
2893 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2894 locus = isl_set_from_basic_set(bset);
2895 set_i = isl_set_copy(pwaff->p[i].set);
2896 if (complement)
2897 set_i = isl_set_subtract(set_i, locus);
2898 else
2899 set_i = isl_set_intersect(set_i, locus);
2900 set = isl_set_union_disjoint(set, set_i);
2903 isl_pw_aff_free(pwaff);
2905 return set;
2908 /* Return a set containing those elements in the domain
2909 * of "pa" where it is positive.
2911 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2913 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2916 /* Return a set containing those elements in the domain
2917 * of pwaff where it is non-negative.
2919 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2921 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2924 /* Return a set containing those elements in the domain
2925 * of pwaff where it is zero.
2927 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2929 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2932 /* Return a set containing those elements in the domain
2933 * of pwaff where it is not zero.
2935 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2937 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2940 /* Bind the affine function "aff" to the parameter "id",
2941 * returning the elements in the domain where the affine expression
2942 * is equal to the parameter.
2944 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2945 __isl_take isl_id *id)
2947 isl_space *space;
2948 isl_aff *aff_id;
2950 space = isl_aff_get_domain_space(aff);
2951 space = isl_space_add_param_id(space, isl_id_copy(id));
2953 aff = isl_aff_align_params(aff, isl_space_copy(space));
2954 aff_id = isl_aff_param_on_domain_space_id(space, id);
2956 return isl_aff_eq_basic_set(aff, aff_id);
2959 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2960 * "rational" should not be set.
2962 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2963 int rational, void *user)
2965 isl_id *id = user;
2967 if (!aff)
2968 return NULL;
2969 if (rational)
2970 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2971 "rational binding not supported", goto error);
2972 return isl_aff_bind_id(aff, isl_id_copy(id));
2973 error:
2974 isl_aff_free(aff);
2975 return NULL;
2978 /* Bind the piecewise affine function "pa" to the parameter "id",
2979 * returning the elements in the domain where the expression
2980 * is equal to the parameter.
2982 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2983 __isl_take isl_id *id)
2985 isl_set *bound;
2987 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2988 isl_id_free(id);
2990 return bound;
2993 /* Return a set containing those elements in the shared domain
2994 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2996 * We compute the difference on the shared domain and then construct
2997 * the set of values where this difference is non-negative.
2998 * If strict is set, we first subtract 1 from the difference.
2999 * If equal is set, we only return the elements where pwaff1 and pwaff2
3000 * are equal.
3002 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3003 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3005 isl_set *set1, *set2;
3007 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3008 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3009 set1 = isl_set_intersect(set1, set2);
3010 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3011 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3012 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3014 if (strict) {
3015 isl_space *space = isl_set_get_space(set1);
3016 isl_aff *aff;
3017 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3018 aff = isl_aff_add_constant_si(aff, -1);
3019 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3020 } else
3021 isl_set_free(set1);
3023 if (equal)
3024 return isl_pw_aff_zero_set(pwaff1);
3025 return isl_pw_aff_nonneg_set(pwaff1);
3028 /* Return a set containing those elements in the shared domain
3029 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3031 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3032 __isl_take isl_pw_aff *pwaff2)
3034 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3035 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3038 /* Return a set containing those elements in the shared domain
3039 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3041 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3042 __isl_take isl_pw_aff *pwaff2)
3044 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3045 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3048 /* Return a set containing those elements in the shared domain
3049 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3051 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3052 __isl_take isl_pw_aff *pwaff2)
3054 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3055 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3058 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3059 __isl_take isl_pw_aff *pwaff2)
3061 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3064 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3065 __isl_take isl_pw_aff *pwaff2)
3067 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3070 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3071 * where the function values are ordered in the same way as "order",
3072 * which returns a set in the shared domain of its two arguments.
3074 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3075 * We first pull back the two functions such that they are defined on
3076 * the domain [A -> B]. Then we apply "order", resulting in a set
3077 * in the space [A -> B]. Finally, we unwrap this set to obtain
3078 * a map in the space A -> B.
3080 static __isl_give isl_map *isl_pw_aff_order_map(
3081 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3082 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3083 __isl_take isl_pw_aff *pa2))
3085 isl_space *space1, *space2;
3086 isl_multi_aff *ma;
3087 isl_set *set;
3089 isl_pw_aff_align_params_bin(&pa1, &pa2);
3090 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3091 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3092 space1 = isl_space_map_from_domain_and_range(space1, space2);
3093 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3094 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3095 ma = isl_multi_aff_range_map(space1);
3096 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3097 set = order(pa1, pa2);
3099 return isl_set_unwrap(set);
3102 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3103 * where the function values are equal.
3105 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3106 __isl_take isl_pw_aff *pa2)
3108 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3111 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3112 * where the function value of "pa1" is less than or equal to
3113 * the function value of "pa2".
3115 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3116 __isl_take isl_pw_aff *pa2)
3118 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3121 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3122 * where the function value of "pa1" is less than the function value of "pa2".
3124 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3125 __isl_take isl_pw_aff *pa2)
3127 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3130 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3131 * where the function value of "pa1" is greater than or equal to
3132 * the function value of "pa2".
3134 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3135 __isl_take isl_pw_aff *pa2)
3137 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3140 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3141 * where the function value of "pa1" is greater than the function value
3142 * of "pa2".
3144 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3145 __isl_take isl_pw_aff *pa2)
3147 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3150 /* Return a set containing those elements in the shared domain
3151 * of the elements of list1 and list2 where each element in list1
3152 * has the relation specified by "fn" with each element in list2.
3154 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3155 __isl_take isl_pw_aff_list *list2,
3156 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3157 __isl_take isl_pw_aff *pwaff2))
3159 int i, j;
3160 isl_ctx *ctx;
3161 isl_set *set;
3163 if (!list1 || !list2)
3164 goto error;
3166 ctx = isl_pw_aff_list_get_ctx(list1);
3167 if (list1->n < 1 || list2->n < 1)
3168 isl_die(ctx, isl_error_invalid,
3169 "list should contain at least one element", goto error);
3171 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3172 for (i = 0; i < list1->n; ++i)
3173 for (j = 0; j < list2->n; ++j) {
3174 isl_set *set_ij;
3176 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3177 isl_pw_aff_copy(list2->p[j]));
3178 set = isl_set_intersect(set, set_ij);
3181 isl_pw_aff_list_free(list1);
3182 isl_pw_aff_list_free(list2);
3183 return set;
3184 error:
3185 isl_pw_aff_list_free(list1);
3186 isl_pw_aff_list_free(list2);
3187 return NULL;
3190 /* Return a set containing those elements in the shared domain
3191 * of the elements of list1 and list2 where each element in list1
3192 * is equal to each element in list2.
3194 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3195 __isl_take isl_pw_aff_list *list2)
3197 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3200 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3201 __isl_take isl_pw_aff_list *list2)
3203 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3206 /* Return a set containing those elements in the shared domain
3207 * of the elements of list1 and list2 where each element in list1
3208 * is less than or equal to each element in list2.
3210 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3211 __isl_take isl_pw_aff_list *list2)
3213 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3216 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3217 __isl_take isl_pw_aff_list *list2)
3219 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3222 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3223 __isl_take isl_pw_aff_list *list2)
3225 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3228 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3229 __isl_take isl_pw_aff_list *list2)
3231 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3235 /* Return a set containing those elements in the shared domain
3236 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3238 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3239 __isl_take isl_pw_aff *pwaff2)
3241 isl_set *set_lt, *set_gt;
3243 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3244 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3245 isl_pw_aff_copy(pwaff2));
3246 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3247 return isl_set_union_disjoint(set_lt, set_gt);
3250 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3251 isl_int v)
3253 int i;
3255 if (isl_int_is_one(v))
3256 return pwaff;
3257 if (!isl_int_is_pos(v))
3258 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3259 "factor needs to be positive",
3260 return isl_pw_aff_free(pwaff));
3261 pwaff = isl_pw_aff_cow(pwaff);
3262 if (!pwaff)
3263 return NULL;
3264 if (pwaff->n == 0)
3265 return pwaff;
3267 for (i = 0; i < pwaff->n; ++i) {
3268 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3269 if (!pwaff->p[i].aff)
3270 return isl_pw_aff_free(pwaff);
3273 return pwaff;
3276 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3278 int i;
3280 pwaff = isl_pw_aff_cow(pwaff);
3281 if (!pwaff)
3282 return NULL;
3283 if (pwaff->n == 0)
3284 return pwaff;
3286 for (i = 0; i < pwaff->n; ++i) {
3287 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3288 if (!pwaff->p[i].aff)
3289 return isl_pw_aff_free(pwaff);
3292 return pwaff;
3295 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3297 int i;
3299 pwaff = isl_pw_aff_cow(pwaff);
3300 if (!pwaff)
3301 return NULL;
3302 if (pwaff->n == 0)
3303 return pwaff;
3305 for (i = 0; i < pwaff->n; ++i) {
3306 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3307 if (!pwaff->p[i].aff)
3308 return isl_pw_aff_free(pwaff);
3311 return pwaff;
3314 /* Assuming that "cond1" and "cond2" are disjoint,
3315 * return an affine expression that is equal to pwaff1 on cond1
3316 * and to pwaff2 on cond2.
3318 static __isl_give isl_pw_aff *isl_pw_aff_select(
3319 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3320 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3322 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3323 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3325 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3328 /* Return an affine expression that is equal to pwaff_true for elements
3329 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3330 * is zero.
3331 * That is, return cond ? pwaff_true : pwaff_false;
3333 * If "cond" involves and NaN, then we conservatively return a NaN
3334 * on its entire domain. In principle, we could consider the pieces
3335 * where it is NaN separately from those where it is not.
3337 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3338 * then only use the domain of "cond" to restrict the domain.
3340 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3341 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3343 isl_set *cond_true, *cond_false;
3344 isl_bool equal;
3346 if (!cond)
3347 goto error;
3348 if (isl_pw_aff_involves_nan(cond)) {
3349 isl_space *space = isl_pw_aff_get_domain_space(cond);
3350 isl_local_space *ls = isl_local_space_from_space(space);
3351 isl_pw_aff_free(cond);
3352 isl_pw_aff_free(pwaff_true);
3353 isl_pw_aff_free(pwaff_false);
3354 return isl_pw_aff_nan_on_domain(ls);
3357 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3358 isl_pw_aff_get_space(pwaff_false));
3359 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3360 isl_pw_aff_get_space(pwaff_true));
3361 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3362 if (equal < 0)
3363 goto error;
3364 if (equal) {
3365 isl_set *dom;
3367 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3368 isl_pw_aff_free(pwaff_false);
3369 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3372 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3373 cond_false = isl_pw_aff_zero_set(cond);
3374 return isl_pw_aff_select(cond_true, pwaff_true,
3375 cond_false, pwaff_false);
3376 error:
3377 isl_pw_aff_free(cond);
3378 isl_pw_aff_free(pwaff_true);
3379 isl_pw_aff_free(pwaff_false);
3380 return NULL;
3383 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3385 int pos;
3387 if (!aff)
3388 return isl_bool_error;
3390 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3391 return isl_bool_ok(pos == -1);
3394 /* Check whether pwaff is a piecewise constant.
3396 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3398 int i;
3400 if (!pwaff)
3401 return isl_bool_error;
3403 for (i = 0; i < pwaff->n; ++i) {
3404 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3405 if (is_cst < 0 || !is_cst)
3406 return is_cst;
3409 return isl_bool_true;
3412 /* Return the product of "aff1" and "aff2".
3414 * If either of the two is NaN, then the result is NaN.
3416 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3418 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3419 __isl_take isl_aff *aff2)
3421 if (!aff1 || !aff2)
3422 goto error;
3424 if (isl_aff_is_nan(aff1)) {
3425 isl_aff_free(aff2);
3426 return aff1;
3428 if (isl_aff_is_nan(aff2)) {
3429 isl_aff_free(aff1);
3430 return aff2;
3433 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3434 return isl_aff_mul(aff2, aff1);
3436 if (!isl_aff_is_cst(aff2))
3437 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3438 "at least one affine expression should be constant",
3439 goto error);
3441 aff1 = isl_aff_cow(aff1);
3442 if (!aff1 || !aff2)
3443 goto error;
3445 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3446 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3448 isl_aff_free(aff2);
3449 return aff1;
3450 error:
3451 isl_aff_free(aff1);
3452 isl_aff_free(aff2);
3453 return NULL;
3456 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3458 * If either of the two is NaN, then the result is NaN.
3459 * A division by zero also results in NaN.
3461 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3462 __isl_take isl_aff *aff2)
3464 isl_bool is_cst, is_zero;
3465 int neg;
3467 if (!aff1 || !aff2)
3468 goto error;
3470 if (isl_aff_is_nan(aff1)) {
3471 isl_aff_free(aff2);
3472 return aff1;
3474 if (isl_aff_is_nan(aff2)) {
3475 isl_aff_free(aff1);
3476 return aff2;
3479 is_cst = isl_aff_is_cst(aff2);
3480 if (is_cst < 0)
3481 goto error;
3482 if (!is_cst)
3483 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3484 "second argument should be a constant", goto error);
3485 is_zero = isl_aff_plain_is_zero(aff2);
3486 if (is_zero < 0)
3487 goto error;
3488 if (is_zero)
3489 return set_nan_free(aff1, aff2);
3491 neg = isl_int_is_neg(aff2->v->el[1]);
3492 if (neg) {
3493 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3494 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3497 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3498 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3500 if (neg) {
3501 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3502 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3505 isl_aff_free(aff2);
3506 return aff1;
3507 error:
3508 isl_aff_free(aff1);
3509 isl_aff_free(aff2);
3510 return NULL;
3513 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3514 __isl_take isl_pw_aff *pwaff2)
3516 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3517 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3520 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3521 __isl_take isl_pw_aff *pwaff2)
3523 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3526 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3527 __isl_take isl_pw_aff *pwaff2)
3529 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3530 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3533 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3535 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3536 __isl_take isl_pw_aff *pa2)
3538 int is_cst;
3540 is_cst = isl_pw_aff_is_cst(pa2);
3541 if (is_cst < 0)
3542 goto error;
3543 if (!is_cst)
3544 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3545 "second argument should be a piecewise constant",
3546 goto error);
3547 isl_pw_aff_align_params_bin(&pa1, &pa2);
3548 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3549 error:
3550 isl_pw_aff_free(pa1);
3551 isl_pw_aff_free(pa2);
3552 return NULL;
3555 /* Compute the quotient of the integer division of "pa1" by "pa2"
3556 * with rounding towards zero.
3557 * "pa2" is assumed to be a piecewise constant.
3559 * In particular, return
3561 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3564 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3565 __isl_take isl_pw_aff *pa2)
3567 int is_cst;
3568 isl_set *cond;
3569 isl_pw_aff *f, *c;
3571 is_cst = isl_pw_aff_is_cst(pa2);
3572 if (is_cst < 0)
3573 goto error;
3574 if (!is_cst)
3575 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3576 "second argument should be a piecewise constant",
3577 goto error);
3579 pa1 = isl_pw_aff_div(pa1, pa2);
3581 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3582 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3583 c = isl_pw_aff_ceil(pa1);
3584 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3585 error:
3586 isl_pw_aff_free(pa1);
3587 isl_pw_aff_free(pa2);
3588 return NULL;
3591 /* Compute the remainder of the integer division of "pa1" by "pa2"
3592 * with rounding towards zero.
3593 * "pa2" is assumed to be a piecewise constant.
3595 * In particular, return
3597 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3600 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3601 __isl_take isl_pw_aff *pa2)
3603 int is_cst;
3604 isl_pw_aff *res;
3606 is_cst = isl_pw_aff_is_cst(pa2);
3607 if (is_cst < 0)
3608 goto error;
3609 if (!is_cst)
3610 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3611 "second argument should be a piecewise constant",
3612 goto error);
3613 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3614 res = isl_pw_aff_mul(pa2, res);
3615 res = isl_pw_aff_sub(pa1, res);
3616 return res;
3617 error:
3618 isl_pw_aff_free(pa1);
3619 isl_pw_aff_free(pa2);
3620 return NULL;
3623 /* Does either of "pa1" or "pa2" involve any NaN2?
3625 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3626 __isl_keep isl_pw_aff *pa2)
3628 isl_bool has_nan;
3630 has_nan = isl_pw_aff_involves_nan(pa1);
3631 if (has_nan < 0 || has_nan)
3632 return has_nan;
3633 return isl_pw_aff_involves_nan(pa2);
3636 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3637 * by a NaN on their shared domain.
3639 * In principle, the result could be refined to only being NaN
3640 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3642 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3643 __isl_take isl_pw_aff *pa2)
3645 isl_local_space *ls;
3646 isl_set *dom;
3647 isl_pw_aff *pa;
3649 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3650 ls = isl_local_space_from_space(isl_set_get_space(dom));
3651 pa = isl_pw_aff_nan_on_domain(ls);
3652 pa = isl_pw_aff_intersect_domain(pa, dom);
3654 return pa;
3657 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3658 __isl_take isl_pw_aff *pwaff2)
3660 isl_set *le;
3661 isl_set *dom;
3663 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3664 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3665 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3666 isl_pw_aff_copy(pwaff2));
3667 dom = isl_set_subtract(dom, isl_set_copy(le));
3668 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3671 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3672 __isl_take isl_pw_aff *pwaff2)
3674 isl_set *ge;
3675 isl_set *dom;
3677 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3678 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3679 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3680 isl_pw_aff_copy(pwaff2));
3681 dom = isl_set_subtract(dom, isl_set_copy(ge));
3682 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3685 /* Return an expression for the minimum (if "max" is not set) or
3686 * the maximum (if "max" is set) of "pa1" and "pa2".
3687 * If either expression involves any NaN, then return a NaN
3688 * on the shared domain as result.
3690 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3691 __isl_take isl_pw_aff *pa2, int max)
3693 isl_bool has_nan;
3695 has_nan = either_involves_nan(pa1, pa2);
3696 if (has_nan < 0)
3697 pa1 = isl_pw_aff_free(pa1);
3698 else if (has_nan)
3699 return replace_by_nan(pa1, pa2);
3701 isl_pw_aff_align_params_bin(&pa1, &pa2);
3702 if (max)
3703 return pw_aff_max(pa1, pa2);
3704 else
3705 return pw_aff_min(pa1, pa2);
3708 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3710 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3711 __isl_take isl_pw_aff *pwaff2)
3713 return pw_aff_min_max(pwaff1, pwaff2, 0);
3716 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3718 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3719 __isl_take isl_pw_aff *pwaff2)
3721 return pw_aff_min_max(pwaff1, pwaff2, 1);
3724 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3725 __isl_take isl_pw_aff_list *list,
3726 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3727 __isl_take isl_pw_aff *pwaff2))
3729 int i;
3730 isl_ctx *ctx;
3731 isl_pw_aff *res;
3733 if (!list)
3734 return NULL;
3736 ctx = isl_pw_aff_list_get_ctx(list);
3737 if (list->n < 1)
3738 isl_die(ctx, isl_error_invalid,
3739 "list should contain at least one element", goto error);
3741 res = isl_pw_aff_copy(list->p[0]);
3742 for (i = 1; i < list->n; ++i)
3743 res = fn(res, isl_pw_aff_copy(list->p[i]));
3745 isl_pw_aff_list_free(list);
3746 return res;
3747 error:
3748 isl_pw_aff_list_free(list);
3749 return NULL;
3752 /* Return an isl_pw_aff that maps each element in the intersection of the
3753 * domains of the elements of list to the minimal corresponding affine
3754 * expression.
3756 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3758 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3761 /* Return an isl_pw_aff that maps each element in the intersection of the
3762 * domains of the elements of list to the maximal corresponding affine
3763 * expression.
3765 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3767 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3770 /* Mark the domains of "pwaff" as rational.
3772 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3774 int i;
3776 pwaff = isl_pw_aff_cow(pwaff);
3777 if (!pwaff)
3778 return NULL;
3779 if (pwaff->n == 0)
3780 return pwaff;
3782 for (i = 0; i < pwaff->n; ++i) {
3783 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3784 if (!pwaff->p[i].set)
3785 return isl_pw_aff_free(pwaff);
3788 return pwaff;
3791 /* Mark the domains of the elements of "list" as rational.
3793 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3794 __isl_take isl_pw_aff_list *list)
3796 int i, n;
3798 if (!list)
3799 return NULL;
3800 if (list->n == 0)
3801 return list;
3803 n = list->n;
3804 for (i = 0; i < n; ++i) {
3805 isl_pw_aff *pa;
3807 pa = isl_pw_aff_list_get_pw_aff(list, i);
3808 pa = isl_pw_aff_set_rational(pa);
3809 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3812 return list;
3815 /* Do the parameters of "aff" match those of "space"?
3817 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3818 __isl_keep isl_space *space)
3820 isl_space *aff_space;
3821 isl_bool match;
3823 if (!aff || !space)
3824 return isl_bool_error;
3826 aff_space = isl_aff_get_domain_space(aff);
3828 match = isl_space_has_equal_params(space, aff_space);
3830 isl_space_free(aff_space);
3831 return match;
3834 /* Check that the domain space of "aff" matches "space".
3836 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3837 __isl_keep isl_space *space)
3839 isl_space *aff_space;
3840 isl_bool match;
3842 if (!aff || !space)
3843 return isl_stat_error;
3845 aff_space = isl_aff_get_domain_space(aff);
3847 match = isl_space_has_equal_params(space, aff_space);
3848 if (match < 0)
3849 goto error;
3850 if (!match)
3851 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3852 "parameters don't match", goto error);
3853 match = isl_space_tuple_is_equal(space, isl_dim_in,
3854 aff_space, isl_dim_set);
3855 if (match < 0)
3856 goto error;
3857 if (!match)
3858 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3859 "domains don't match", goto error);
3860 isl_space_free(aff_space);
3861 return isl_stat_ok;
3862 error:
3863 isl_space_free(aff_space);
3864 return isl_stat_error;
3867 /* Return the shared (universe) domain of the elements of "ma".
3869 * Since an isl_multi_aff (and an isl_aff) is always total,
3870 * the domain is always the universe set in its domain space.
3871 * This is a helper function for use in the generic isl_multi_*_bind.
3873 static __isl_give isl_basic_set *isl_multi_aff_domain(
3874 __isl_take isl_multi_aff *ma)
3876 isl_space *space;
3878 space = isl_multi_aff_get_space(ma);
3879 isl_multi_aff_free(ma);
3881 return isl_basic_set_universe(isl_space_domain(space));
3884 #undef BASE
3885 #define BASE aff
3887 #include <isl_multi_no_explicit_domain.c>
3888 #include <isl_multi_templ.c>
3889 #include <isl_multi_add_constant_templ.c>
3890 #include <isl_multi_apply_set.c>
3891 #include <isl_multi_arith_templ.c>
3892 #include <isl_multi_bind_domain_templ.c>
3893 #include <isl_multi_cmp.c>
3894 #include <isl_multi_dim_id_templ.c>
3895 #include <isl_multi_dims.c>
3896 #include <isl_multi_floor.c>
3897 #include <isl_multi_from_base_templ.c>
3898 #include <isl_multi_identity_templ.c>
3899 #include <isl_multi_insert_domain_templ.c>
3900 #include <isl_multi_locals_templ.c>
3901 #include <isl_multi_move_dims_templ.c>
3902 #include <isl_multi_nan_templ.c>
3903 #include <isl_multi_product_templ.c>
3904 #include <isl_multi_splice_templ.c>
3905 #include <isl_multi_tuple_id_templ.c>
3906 #include <isl_multi_unbind_params_templ.c>
3907 #include <isl_multi_zero_templ.c>
3909 #undef DOMBASE
3910 #define DOMBASE set
3911 #include <isl_multi_gist.c>
3913 #undef DOMBASE
3914 #define DOMBASE basic_set
3915 #include <isl_multi_bind_templ.c>
3917 /* Construct an isl_multi_aff living in "space" that corresponds
3918 * to the affine transformation matrix "mat".
3920 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3921 __isl_take isl_space *space, __isl_take isl_mat *mat)
3923 isl_ctx *ctx;
3924 isl_local_space *ls = NULL;
3925 isl_multi_aff *ma = NULL;
3926 isl_size n_row, n_col, n_out, total;
3927 int i;
3929 if (!space || !mat)
3930 goto error;
3932 ctx = isl_mat_get_ctx(mat);
3934 n_row = isl_mat_rows(mat);
3935 n_col = isl_mat_cols(mat);
3936 n_out = isl_space_dim(space, isl_dim_out);
3937 total = isl_space_dim(space, isl_dim_all);
3938 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3939 goto error;
3940 if (n_row < 1)
3941 isl_die(ctx, isl_error_invalid,
3942 "insufficient number of rows", goto error);
3943 if (n_col < 1)
3944 isl_die(ctx, isl_error_invalid,
3945 "insufficient number of columns", goto error);
3946 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3947 isl_die(ctx, isl_error_invalid,
3948 "dimension mismatch", goto error);
3950 ma = isl_multi_aff_zero(isl_space_copy(space));
3951 space = isl_space_domain(space);
3952 ls = isl_local_space_from_space(isl_space_copy(space));
3954 for (i = 0; i < n_row - 1; ++i) {
3955 isl_vec *v;
3956 isl_aff *aff;
3958 v = isl_vec_alloc(ctx, 1 + n_col);
3959 if (!v)
3960 goto error;
3961 isl_int_set(v->el[0], mat->row[0][0]);
3962 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3963 v = isl_vec_normalize(v);
3964 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3965 ma = isl_multi_aff_set_aff(ma, i, aff);
3968 isl_space_free(space);
3969 isl_local_space_free(ls);
3970 isl_mat_free(mat);
3971 return ma;
3972 error:
3973 isl_space_free(space);
3974 isl_local_space_free(ls);
3975 isl_mat_free(mat);
3976 isl_multi_aff_free(ma);
3977 return NULL;
3980 /* Return the constant terms of the affine expressions of "ma".
3982 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3983 __isl_keep isl_multi_aff *ma)
3985 int i;
3986 isl_size n;
3987 isl_space *space;
3988 isl_multi_val *mv;
3990 n = isl_multi_aff_size(ma);
3991 if (n < 0)
3992 return NULL;
3993 space = isl_space_range(isl_multi_aff_get_space(ma));
3994 space = isl_space_drop_all_params(space);
3995 mv = isl_multi_val_zero(space);
3997 for (i = 0; i < n; ++i) {
3998 isl_aff *aff;
3999 isl_val *val;
4001 aff = isl_multi_aff_get_at(ma, i);
4002 val = isl_aff_get_constant_val(aff);
4003 isl_aff_free(aff);
4004 mv = isl_multi_val_set_at(mv, i, val);
4007 return mv;
4010 /* Remove any internal structure of the domain of "ma".
4011 * If there is any such internal structure in the input,
4012 * then the name of the corresponding space is also removed.
4014 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4015 __isl_take isl_multi_aff *ma)
4017 isl_space *space;
4019 if (!ma)
4020 return NULL;
4022 if (!ma->space->nested[0])
4023 return ma;
4025 space = isl_multi_aff_get_space(ma);
4026 space = isl_space_flatten_domain(space);
4027 ma = isl_multi_aff_reset_space(ma, space);
4029 return ma;
4032 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4033 * of the space to its domain.
4035 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4037 int i;
4038 isl_size n_in;
4039 isl_local_space *ls;
4040 isl_multi_aff *ma;
4042 if (!space)
4043 return NULL;
4044 if (!isl_space_is_map(space))
4045 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4046 "not a map space", goto error);
4048 n_in = isl_space_dim(space, isl_dim_in);
4049 if (n_in < 0)
4050 goto error;
4051 space = isl_space_domain_map(space);
4053 ma = isl_multi_aff_alloc(isl_space_copy(space));
4054 if (n_in == 0) {
4055 isl_space_free(space);
4056 return ma;
4059 space = isl_space_domain(space);
4060 ls = isl_local_space_from_space(space);
4061 for (i = 0; i < n_in; ++i) {
4062 isl_aff *aff;
4064 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4065 isl_dim_set, i);
4066 ma = isl_multi_aff_set_aff(ma, i, aff);
4068 isl_local_space_free(ls);
4069 return ma;
4070 error:
4071 isl_space_free(space);
4072 return NULL;
4075 /* This function performs the same operation as isl_multi_aff_domain_map,
4076 * but is considered as a function on an isl_space when exported.
4078 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4079 __isl_take isl_space *space)
4081 return isl_multi_aff_domain_map(space);
4084 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4085 * of the space to its range.
4087 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4089 int i;
4090 isl_size n_in, n_out;
4091 isl_local_space *ls;
4092 isl_multi_aff *ma;
4094 if (!space)
4095 return NULL;
4096 if (!isl_space_is_map(space))
4097 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4098 "not a map space", goto error);
4100 n_in = isl_space_dim(space, isl_dim_in);
4101 n_out = isl_space_dim(space, isl_dim_out);
4102 if (n_in < 0 || n_out < 0)
4103 goto error;
4104 space = isl_space_range_map(space);
4106 ma = isl_multi_aff_alloc(isl_space_copy(space));
4107 if (n_out == 0) {
4108 isl_space_free(space);
4109 return ma;
4112 space = isl_space_domain(space);
4113 ls = isl_local_space_from_space(space);
4114 for (i = 0; i < n_out; ++i) {
4115 isl_aff *aff;
4117 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4118 isl_dim_set, n_in + i);
4119 ma = isl_multi_aff_set_aff(ma, i, aff);
4121 isl_local_space_free(ls);
4122 return ma;
4123 error:
4124 isl_space_free(space);
4125 return NULL;
4128 /* This function performs the same operation as isl_multi_aff_range_map,
4129 * but is considered as a function on an isl_space when exported.
4131 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4132 __isl_take isl_space *space)
4134 return isl_multi_aff_range_map(space);
4137 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4138 * of the space to its domain.
4140 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4141 __isl_take isl_space *space)
4143 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4146 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4147 * but is considered as a function on an isl_space when exported.
4149 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4150 __isl_take isl_space *space)
4152 return isl_pw_multi_aff_domain_map(space);
4155 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4156 * of the space to its range.
4158 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4159 __isl_take isl_space *space)
4161 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4164 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4165 * but is considered as a function on an isl_space when exported.
4167 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4168 __isl_take isl_space *space)
4170 return isl_pw_multi_aff_range_map(space);
4173 /* Given the space of a set and a range of set dimensions,
4174 * construct an isl_multi_aff that projects out those dimensions.
4176 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4177 __isl_take isl_space *space, enum isl_dim_type type,
4178 unsigned first, unsigned n)
4180 int i;
4181 isl_size dim;
4182 isl_local_space *ls;
4183 isl_multi_aff *ma;
4185 if (!space)
4186 return NULL;
4187 if (!isl_space_is_set(space))
4188 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4189 "expecting set space", goto error);
4190 if (type != isl_dim_set)
4191 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4192 "only set dimensions can be projected out", goto error);
4193 if (isl_space_check_range(space, type, first, n) < 0)
4194 goto error;
4196 dim = isl_space_dim(space, isl_dim_set);
4197 if (dim < 0)
4198 goto error;
4200 space = isl_space_from_domain(space);
4201 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4203 if (dim == n)
4204 return isl_multi_aff_alloc(space);
4206 ma = isl_multi_aff_alloc(isl_space_copy(space));
4207 space = isl_space_domain(space);
4208 ls = isl_local_space_from_space(space);
4210 for (i = 0; i < first; ++i) {
4211 isl_aff *aff;
4213 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4214 isl_dim_set, i);
4215 ma = isl_multi_aff_set_aff(ma, i, aff);
4218 for (i = 0; i < dim - (first + n); ++i) {
4219 isl_aff *aff;
4221 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4222 isl_dim_set, first + n + i);
4223 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4226 isl_local_space_free(ls);
4227 return ma;
4228 error:
4229 isl_space_free(space);
4230 return NULL;
4233 /* Given the space of a set and a range of set dimensions,
4234 * construct an isl_pw_multi_aff that projects out those dimensions.
4236 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4237 __isl_take isl_space *space, enum isl_dim_type type,
4238 unsigned first, unsigned n)
4240 isl_multi_aff *ma;
4242 ma = isl_multi_aff_project_out_map(space, type, first, n);
4243 return isl_pw_multi_aff_from_multi_aff(ma);
4246 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4247 * but is considered as a function on an isl_multi_aff when exported.
4249 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4250 __isl_take isl_multi_aff *ma)
4252 return isl_pw_multi_aff_from_multi_aff(ma);
4255 /* Create a piecewise multi-affine expression in the given space that maps each
4256 * input dimension to the corresponding output dimension.
4258 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4259 __isl_take isl_space *space)
4261 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4264 /* Create a piecewise multi expression that maps elements in the given space
4265 * to themselves.
4267 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4268 __isl_take isl_space *space)
4270 isl_multi_aff *ma;
4272 ma = isl_multi_aff_identity_on_domain_space(space);
4273 return isl_pw_multi_aff_from_multi_aff(ma);
4276 /* This function performs the same operation as
4277 * isl_pw_multi_aff_identity_on_domain_space,
4278 * but is considered as a function on an isl_space when exported.
4280 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4281 __isl_take isl_space *space)
4283 return isl_pw_multi_aff_identity_on_domain_space(space);
4286 /* Exploit the equalities in "eq" to simplify the affine expressions.
4288 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4289 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4291 int i;
4293 maff = isl_multi_aff_cow(maff);
4294 if (!maff || !eq)
4295 goto error;
4297 for (i = 0; i < maff->n; ++i) {
4298 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4299 isl_basic_set_copy(eq));
4300 if (!maff->u.p[i])
4301 goto error;
4304 isl_basic_set_free(eq);
4305 return maff;
4306 error:
4307 isl_basic_set_free(eq);
4308 isl_multi_aff_free(maff);
4309 return NULL;
4312 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4313 isl_int f)
4315 int i;
4317 maff = isl_multi_aff_cow(maff);
4318 if (!maff)
4319 return NULL;
4321 for (i = 0; i < maff->n; ++i) {
4322 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4323 if (!maff->u.p[i])
4324 return isl_multi_aff_free(maff);
4327 return maff;
4330 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4331 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4333 maff1 = isl_multi_aff_add(maff1, maff2);
4334 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4335 return maff1;
4338 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4340 if (!maff)
4341 return isl_bool_error;
4343 return isl_bool_false;
4346 /* Return the set of domain elements where "ma1" is lexicographically
4347 * smaller than or equal to "ma2".
4349 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4350 __isl_take isl_multi_aff *ma2)
4352 return isl_multi_aff_lex_ge_set(ma2, ma1);
4355 /* Return the set of domain elements where "ma1" is lexicographically
4356 * smaller than "ma2".
4358 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4359 __isl_take isl_multi_aff *ma2)
4361 return isl_multi_aff_lex_gt_set(ma2, ma1);
4364 /* Return the set of domain elements where "ma1" is lexicographically
4365 * greater than to "ma2". If "equal" is set, then include the domain
4366 * elements where they are equal.
4367 * Do this for the case where there are no entries.
4368 * In this case, "ma1" cannot be greater than "ma2",
4369 * but it is (greater than or) equal to "ma2".
4371 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4372 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4374 isl_space *space;
4376 space = isl_multi_aff_get_domain_space(ma1);
4378 isl_multi_aff_free(ma1);
4379 isl_multi_aff_free(ma2);
4381 if (equal)
4382 return isl_set_universe(space);
4383 else
4384 return isl_set_empty(space);
4387 /* Return the set where entry "i" of "ma1" and "ma2"
4388 * satisfy the relation prescribed by "cmp".
4390 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4391 __isl_keep isl_multi_aff *ma2, int i,
4392 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4393 __isl_take isl_aff *aff2))
4395 isl_aff *aff1, *aff2;
4397 aff1 = isl_multi_aff_get_at(ma1, i);
4398 aff2 = isl_multi_aff_get_at(ma2, i);
4399 return cmp(aff1, aff2);
4402 /* Return the set of domain elements where "ma1" is lexicographically
4403 * greater than to "ma2". If "equal" is set, then include the domain
4404 * elements where they are equal.
4406 * In particular, for all but the final entry,
4407 * include the set of elements where this entry is strictly greater in "ma1"
4408 * and all previous entries are equal.
4409 * The final entry is also allowed to be equal in the two functions
4410 * if "equal" is set.
4412 * The case where there are no entries is handled separately.
4414 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4415 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4417 int i;
4418 isl_size n;
4419 isl_space *space;
4420 isl_set *res;
4421 isl_set *equal_set;
4422 isl_set *gte;
4424 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4425 goto error;
4426 n = isl_multi_aff_size(ma1);
4427 if (n < 0)
4428 goto error;
4429 if (n == 0)
4430 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4432 space = isl_multi_aff_get_domain_space(ma1);
4433 res = isl_set_empty(isl_space_copy(space));
4434 equal_set = isl_set_universe(space);
4436 for (i = 0; i + 1 < n; ++i) {
4437 isl_bool empty;
4438 isl_set *gt, *eq;
4440 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4441 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4442 res = isl_set_union(res, gt);
4443 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4444 equal_set = isl_set_intersect(equal_set, eq);
4446 empty = isl_set_is_empty(equal_set);
4447 if (empty >= 0 && empty)
4448 break;
4451 if (equal)
4452 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4453 else
4454 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4455 isl_multi_aff_free(ma1);
4456 isl_multi_aff_free(ma2);
4458 gte = isl_set_intersect(gte, equal_set);
4459 return isl_set_union(res, gte);
4460 error:
4461 isl_multi_aff_free(ma1);
4462 isl_multi_aff_free(ma2);
4463 return NULL;
4466 /* Return the set of domain elements where "ma1" is lexicographically
4467 * greater than or equal to "ma2".
4469 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4470 __isl_take isl_multi_aff *ma2)
4472 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4475 /* Return the set of domain elements where "ma1" is lexicographically
4476 * greater than "ma2".
4478 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4479 __isl_take isl_multi_aff *ma2)
4481 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4484 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4486 #undef PW
4487 #define PW isl_pw_multi_aff
4488 #undef BASE
4489 #define BASE multi_aff
4490 #undef EL_IS_ZERO
4491 #define EL_IS_ZERO is_empty
4492 #undef ZERO
4493 #define ZERO empty
4494 #undef IS_ZERO
4495 #define IS_ZERO is_empty
4496 #undef FIELD
4497 #define FIELD maff
4498 #undef DEFAULT_IS_ZERO
4499 #define DEFAULT_IS_ZERO 0
4501 #include <isl_pw_templ.c>
4502 #include <isl_pw_add_constant_multi_val_templ.c>
4503 #include <isl_pw_add_constant_val_templ.c>
4504 #include <isl_pw_bind_domain_templ.c>
4505 #include <isl_pw_insert_dims_templ.c>
4506 #include <isl_pw_insert_domain_templ.c>
4507 #include <isl_pw_locals_templ.c>
4508 #include <isl_pw_move_dims_templ.c>
4509 #include <isl_pw_neg_templ.c>
4510 #include <isl_pw_pullback_templ.c>
4511 #include <isl_pw_range_tuple_id_templ.c>
4512 #include <isl_pw_union_opt.c>
4514 #undef BASE
4515 #define BASE pw_multi_aff
4517 #include <isl_union_multi.c>
4518 #include "isl_union_locals_templ.c"
4519 #include <isl_union_neg.c>
4521 #undef BASE
4522 #define BASE multi_aff
4524 #include <isl_union_pw_templ.c>
4526 /* Generic function for extracting a factor from a product "pma".
4527 * "check_space" checks that the space is that of the right kind of product.
4528 * "space_factor" extracts the factor from the space.
4529 * "multi_aff_factor" extracts the factor from the constituent functions.
4531 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4532 __isl_take isl_pw_multi_aff *pma,
4533 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4534 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4535 __isl_give isl_multi_aff *(*multi_aff_factor)(
4536 __isl_take isl_multi_aff *ma))
4538 int i;
4539 isl_space *space;
4541 if (check_space(pma) < 0)
4542 return isl_pw_multi_aff_free(pma);
4544 space = isl_pw_multi_aff_take_space(pma);
4545 space = space_factor(space);
4547 for (i = 0; pma && i < pma->n; ++i) {
4548 isl_multi_aff *ma;
4550 ma = isl_pw_multi_aff_take_base_at(pma, i);
4551 ma = multi_aff_factor(ma);
4552 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4555 pma = isl_pw_multi_aff_restore_space(pma, space);
4557 return pma;
4560 /* Is the range of "pma" a wrapped relation?
4562 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4563 __isl_keep isl_pw_multi_aff *pma)
4565 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4568 /* Check that the range of "pma" is a product.
4570 static isl_stat pw_multi_aff_check_range_product(
4571 __isl_keep isl_pw_multi_aff *pma)
4573 isl_bool wraps;
4575 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4576 if (wraps < 0)
4577 return isl_stat_error;
4578 if (!wraps)
4579 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4580 "range is not a product", return isl_stat_error);
4581 return isl_stat_ok;
4584 /* Given a function A -> [B -> C], extract the function A -> B.
4586 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4587 __isl_take isl_pw_multi_aff *pma)
4589 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4590 &isl_space_range_factor_domain,
4591 &isl_multi_aff_range_factor_domain);
4594 /* Given a function A -> [B -> C], extract the function A -> C.
4596 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4597 __isl_take isl_pw_multi_aff *pma)
4599 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4600 &isl_space_range_factor_range,
4601 &isl_multi_aff_range_factor_range);
4604 /* Given two piecewise multi affine expressions, return a piecewise
4605 * multi-affine expression defined on the union of the definition domains
4606 * of the inputs that is equal to the lexicographic maximum of the two
4607 * inputs on each cell. If only one of the two inputs is defined on
4608 * a given cell, then it is considered to be the maximum.
4610 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4611 __isl_take isl_pw_multi_aff *pma1,
4612 __isl_take isl_pw_multi_aff *pma2)
4614 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4615 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4616 &isl_multi_aff_lex_ge_set);
4619 /* Given two piecewise multi affine expressions, return a piecewise
4620 * multi-affine expression defined on the union of the definition domains
4621 * of the inputs that is equal to the lexicographic minimum of the two
4622 * inputs on each cell. If only one of the two inputs is defined on
4623 * a given cell, then it is considered to be the minimum.
4625 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4626 __isl_take isl_pw_multi_aff *pma1,
4627 __isl_take isl_pw_multi_aff *pma2)
4629 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4630 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4631 &isl_multi_aff_lex_le_set);
4634 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4635 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4637 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4638 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4639 &isl_multi_aff_add);
4642 /* Subtract "pma2" from "pma1" and return the result.
4644 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4645 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4647 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4648 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4649 &isl_multi_aff_sub);
4652 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4653 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4655 return isl_pw_multi_aff_union_add_(pma1, pma2);
4658 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4659 * with the actual sum on the shared domain and
4660 * the defined expression on the symmetric difference of the domains.
4662 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4663 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4665 return isl_union_pw_aff_union_add_(upa1, upa2);
4668 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4669 * with the actual sum on the shared domain and
4670 * the defined expression on the symmetric difference of the domains.
4672 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4673 __isl_take isl_union_pw_multi_aff *upma1,
4674 __isl_take isl_union_pw_multi_aff *upma2)
4676 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4679 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4680 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4682 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4683 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4685 int i, j, n;
4686 isl_space *space;
4687 isl_pw_multi_aff *res;
4689 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4690 goto error;
4692 n = pma1->n * pma2->n;
4693 space = isl_space_product(isl_space_copy(pma1->dim),
4694 isl_space_copy(pma2->dim));
4695 res = isl_pw_multi_aff_alloc_size(space, n);
4697 for (i = 0; i < pma1->n; ++i) {
4698 for (j = 0; j < pma2->n; ++j) {
4699 isl_set *domain;
4700 isl_multi_aff *ma;
4702 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4703 isl_set_copy(pma2->p[j].set));
4704 ma = isl_multi_aff_product(
4705 isl_multi_aff_copy(pma1->p[i].maff),
4706 isl_multi_aff_copy(pma2->p[j].maff));
4707 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4711 isl_pw_multi_aff_free(pma1);
4712 isl_pw_multi_aff_free(pma2);
4713 return res;
4714 error:
4715 isl_pw_multi_aff_free(pma1);
4716 isl_pw_multi_aff_free(pma2);
4717 return NULL;
4720 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4721 * denominator "denom".
4722 * "denom" is allowed to be negative, in which case the actual denominator
4723 * is -denom and the expressions are added instead.
4725 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4726 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4728 int i, first;
4729 int sign;
4730 isl_int d;
4732 first = isl_seq_first_non_zero(c, n);
4733 if (first == -1)
4734 return aff;
4736 sign = isl_int_sgn(denom);
4737 isl_int_init(d);
4738 isl_int_abs(d, denom);
4739 for (i = first; i < n; ++i) {
4740 isl_aff *aff_i;
4742 if (isl_int_is_zero(c[i]))
4743 continue;
4744 aff_i = isl_multi_aff_get_aff(ma, i);
4745 aff_i = isl_aff_scale(aff_i, c[i]);
4746 aff_i = isl_aff_scale_down(aff_i, d);
4747 if (sign >= 0)
4748 aff = isl_aff_sub(aff, aff_i);
4749 else
4750 aff = isl_aff_add(aff, aff_i);
4752 isl_int_clear(d);
4754 return aff;
4757 /* Extract an affine expression that expresses the output dimension "pos"
4758 * of "bmap" in terms of the parameters and input dimensions from
4759 * equality "eq".
4760 * Note that this expression may involve integer divisions defined
4761 * in terms of parameters and input dimensions.
4762 * The equality may also involve references to earlier (but not later)
4763 * output dimensions. These are replaced by the corresponding elements
4764 * in "ma".
4766 * If the equality is of the form
4768 * f(i) + h(j) + a x + g(i) = 0,
4770 * with f(i) a linear combinations of the parameters and input dimensions,
4771 * g(i) a linear combination of integer divisions defined in terms of the same
4772 * and h(j) a linear combinations of earlier output dimensions,
4773 * then the affine expression is
4775 * (-f(i) - g(i))/a - h(j)/a
4777 * If the equality is of the form
4779 * f(i) + h(j) - a x + g(i) = 0,
4781 * then the affine expression is
4783 * (f(i) + g(i))/a - h(j)/(-a)
4786 * If "div" refers to an integer division (i.e., it is smaller than
4787 * the number of integer divisions), then the equality constraint
4788 * does involve an integer division (the one at position "div") that
4789 * is defined in terms of output dimensions. However, this integer
4790 * division can be eliminated by exploiting a pair of constraints
4791 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4792 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4793 * -l + x >= 0.
4794 * In particular, let
4796 * x = e(i) + m floor(...)
4798 * with e(i) the expression derived above and floor(...) the integer
4799 * division involving output dimensions.
4800 * From
4802 * l <= x <= l + n,
4804 * we have
4806 * 0 <= x - l <= n
4808 * This means
4810 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4811 * = (e(i) - l) mod m
4813 * Therefore,
4815 * x - l = (e(i) - l) mod m
4817 * or
4819 * x = ((e(i) - l) mod m) + l
4821 * The variable "shift" below contains the expression -l, which may
4822 * also involve a linear combination of earlier output dimensions.
4824 static __isl_give isl_aff *extract_aff_from_equality(
4825 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4826 __isl_keep isl_multi_aff *ma)
4828 unsigned o_out;
4829 isl_size n_div, n_out;
4830 isl_ctx *ctx;
4831 isl_local_space *ls;
4832 isl_aff *aff, *shift;
4833 isl_val *mod;
4835 ctx = isl_basic_map_get_ctx(bmap);
4836 ls = isl_basic_map_get_local_space(bmap);
4837 ls = isl_local_space_domain(ls);
4838 aff = isl_aff_alloc(isl_local_space_copy(ls));
4839 if (!aff)
4840 goto error;
4841 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4842 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4843 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4844 if (n_out < 0 || n_div < 0)
4845 goto error;
4846 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4847 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4848 isl_seq_cpy(aff->v->el + 1 + o_out,
4849 bmap->eq[eq] + o_out + n_out, n_div);
4850 } else {
4851 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4852 isl_seq_neg(aff->v->el + 1 + o_out,
4853 bmap->eq[eq] + o_out + n_out, n_div);
4855 if (div < n_div)
4856 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4857 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4858 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4859 bmap->eq[eq][o_out + pos]);
4860 if (div < n_div) {
4861 shift = isl_aff_alloc(isl_local_space_copy(ls));
4862 if (!shift)
4863 goto error;
4864 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4865 isl_seq_cpy(shift->v->el + 1 + o_out,
4866 bmap->ineq[ineq] + o_out + n_out, n_div);
4867 isl_int_set_si(shift->v->el[0], 1);
4868 shift = subtract_initial(shift, ma, pos,
4869 bmap->ineq[ineq] + o_out, ctx->negone);
4870 aff = isl_aff_add(aff, isl_aff_copy(shift));
4871 mod = isl_val_int_from_isl_int(ctx,
4872 bmap->eq[eq][o_out + n_out + div]);
4873 mod = isl_val_abs(mod);
4874 aff = isl_aff_mod_val(aff, mod);
4875 aff = isl_aff_sub(aff, shift);
4878 isl_local_space_free(ls);
4879 return aff;
4880 error:
4881 isl_local_space_free(ls);
4882 isl_aff_free(aff);
4883 return NULL;
4886 /* Given a basic map with output dimensions defined
4887 * in terms of the parameters input dimensions and earlier
4888 * output dimensions using an equality (and possibly a pair on inequalities),
4889 * extract an isl_aff that expresses output dimension "pos" in terms
4890 * of the parameters and input dimensions.
4891 * Note that this expression may involve integer divisions defined
4892 * in terms of parameters and input dimensions.
4893 * "ma" contains the expressions corresponding to earlier output dimensions.
4895 * This function shares some similarities with
4896 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4898 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4899 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4901 int eq, div, ineq;
4902 isl_aff *aff;
4904 if (!bmap)
4905 return NULL;
4906 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4907 if (eq >= bmap->n_eq)
4908 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4909 "unable to find suitable equality", return NULL);
4910 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4912 aff = isl_aff_remove_unused_divs(aff);
4913 return aff;
4916 /* Given a basic map where each output dimension is defined
4917 * in terms of the parameters and input dimensions using an equality,
4918 * extract an isl_multi_aff that expresses the output dimensions in terms
4919 * of the parameters and input dimensions.
4921 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4922 __isl_take isl_basic_map *bmap)
4924 int i;
4925 isl_size n_out;
4926 isl_multi_aff *ma;
4928 if (!bmap)
4929 return NULL;
4931 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4932 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4933 if (n_out < 0)
4934 ma = isl_multi_aff_free(ma);
4936 for (i = 0; i < n_out; ++i) {
4937 isl_aff *aff;
4939 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4940 ma = isl_multi_aff_set_aff(ma, i, aff);
4943 isl_basic_map_free(bmap);
4945 return ma;
4948 /* Given a basic set where each set dimension is defined
4949 * in terms of the parameters using an equality,
4950 * extract an isl_multi_aff that expresses the set dimensions in terms
4951 * of the parameters.
4953 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4954 __isl_take isl_basic_set *bset)
4956 return extract_isl_multi_aff_from_basic_map(bset);
4959 /* Create an isl_pw_multi_aff that is equivalent to
4960 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4961 * The given basic map is such that each output dimension is defined
4962 * in terms of the parameters and input dimensions using an equality.
4964 * Since some applications expect the result of isl_pw_multi_aff_from_map
4965 * to only contain integer affine expressions, we compute the floor
4966 * of the expression before returning.
4968 * Remove all constraints involving local variables without
4969 * an explicit representation (resulting in the removal of those
4970 * local variables) prior to the actual extraction to ensure
4971 * that the local spaces in which the resulting affine expressions
4972 * are created do not contain any unknown local variables.
4973 * Removing such constraints is safe because constraints involving
4974 * unknown local variables are not used to determine whether
4975 * a basic map is obviously single-valued.
4977 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4978 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4980 isl_multi_aff *ma;
4982 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4983 ma = extract_isl_multi_aff_from_basic_map(bmap);
4984 ma = isl_multi_aff_floor(ma);
4985 return isl_pw_multi_aff_alloc(domain, ma);
4988 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4989 * This obviously only works if the input "map" is single-valued.
4990 * If so, we compute the lexicographic minimum of the image in the form
4991 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4992 * to its lexicographic minimum.
4993 * If the input is not single-valued, we produce an error.
4995 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4996 __isl_take isl_map *map)
4998 int i;
4999 int sv;
5000 isl_pw_multi_aff *pma;
5002 sv = isl_map_is_single_valued(map);
5003 if (sv < 0)
5004 goto error;
5005 if (!sv)
5006 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5007 "map is not single-valued", goto error);
5008 map = isl_map_make_disjoint(map);
5009 if (!map)
5010 return NULL;
5012 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5014 for (i = 0; i < map->n; ++i) {
5015 isl_pw_multi_aff *pma_i;
5016 isl_basic_map *bmap;
5017 bmap = isl_basic_map_copy(map->p[i]);
5018 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5019 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5022 isl_map_free(map);
5023 return pma;
5024 error:
5025 isl_map_free(map);
5026 return NULL;
5029 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5030 * taking into account that the output dimension at position "d"
5031 * can be represented as
5033 * x = floor((e(...) + c1) / m)
5035 * given that constraint "i" is of the form
5037 * e(...) + c1 - m x >= 0
5040 * Let "map" be of the form
5042 * A -> B
5044 * We construct a mapping
5046 * A -> [A -> x = floor(...)]
5048 * apply that to the map, obtaining
5050 * [A -> x = floor(...)] -> B
5052 * and equate dimension "d" to x.
5053 * We then compute a isl_pw_multi_aff representation of the resulting map
5054 * and plug in the mapping above.
5056 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5057 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5059 isl_ctx *ctx;
5060 isl_space *space = NULL;
5061 isl_local_space *ls;
5062 isl_multi_aff *ma;
5063 isl_aff *aff;
5064 isl_vec *v;
5065 isl_map *insert;
5066 int offset;
5067 isl_size n;
5068 isl_size n_in;
5069 isl_pw_multi_aff *pma;
5070 isl_bool is_set;
5072 is_set = isl_map_is_set(map);
5073 if (is_set < 0)
5074 goto error;
5076 offset = isl_basic_map_offset(hull, isl_dim_out);
5077 ctx = isl_map_get_ctx(map);
5078 space = isl_space_domain(isl_map_get_space(map));
5079 n_in = isl_space_dim(space, isl_dim_set);
5080 n = isl_space_dim(space, isl_dim_all);
5081 if (n_in < 0 || n < 0)
5082 goto error;
5084 v = isl_vec_alloc(ctx, 1 + 1 + n);
5085 if (v) {
5086 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5087 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5089 isl_basic_map_free(hull);
5091 ls = isl_local_space_from_space(isl_space_copy(space));
5092 aff = isl_aff_alloc_vec(ls, v);
5093 aff = isl_aff_floor(aff);
5094 if (is_set) {
5095 isl_space_free(space);
5096 ma = isl_multi_aff_from_aff(aff);
5097 } else {
5098 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5099 ma = isl_multi_aff_range_product(ma,
5100 isl_multi_aff_from_aff(aff));
5103 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5104 map = isl_map_apply_domain(map, insert);
5105 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5106 pma = isl_pw_multi_aff_from_map(map);
5107 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5109 return pma;
5110 error:
5111 isl_space_free(space);
5112 isl_map_free(map);
5113 isl_basic_map_free(hull);
5114 return NULL;
5117 /* Is constraint "c" of the form
5119 * e(...) + c1 - m x >= 0
5121 * or
5123 * -e(...) + c2 + m x >= 0
5125 * where m > 1 and e only depends on parameters and input dimensions?
5127 * "offset" is the offset of the output dimensions
5128 * "pos" is the position of output dimension x.
5130 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5132 if (isl_int_is_zero(c[offset + d]))
5133 return 0;
5134 if (isl_int_is_one(c[offset + d]))
5135 return 0;
5136 if (isl_int_is_negone(c[offset + d]))
5137 return 0;
5138 if (isl_seq_first_non_zero(c + offset, d) != -1)
5139 return 0;
5140 if (isl_seq_first_non_zero(c + offset + d + 1,
5141 total - (offset + d + 1)) != -1)
5142 return 0;
5143 return 1;
5146 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5148 * As a special case, we first check if there is any pair of constraints,
5149 * shared by all the basic maps in "map" that force a given dimension
5150 * to be equal to the floor of some affine combination of the input dimensions.
5152 * In particular, if we can find two constraints
5154 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5156 * and
5158 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5160 * where m > 1 and e only depends on parameters and input dimensions,
5161 * and such that
5163 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5165 * then we know that we can take
5167 * x = floor((e(...) + c1) / m)
5169 * without having to perform any computation.
5171 * Note that we know that
5173 * c1 + c2 >= 1
5175 * If c1 + c2 were 0, then we would have detected an equality during
5176 * simplification. If c1 + c2 were negative, then we would have detected
5177 * a contradiction.
5179 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5180 __isl_take isl_map *map)
5182 int d;
5183 isl_size dim;
5184 int i, j, n;
5185 int offset;
5186 isl_size total;
5187 isl_int sum;
5188 isl_basic_map *hull;
5190 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5191 dim = isl_map_dim(map, isl_dim_out);
5192 total = isl_basic_map_dim(hull, isl_dim_all);
5193 if (dim < 0 || total < 0)
5194 goto error;
5196 isl_int_init(sum);
5197 offset = isl_basic_map_offset(hull, isl_dim_out);
5198 n = hull->n_ineq;
5199 for (d = 0; d < dim; ++d) {
5200 for (i = 0; i < n; ++i) {
5201 if (!is_potential_div_constraint(hull->ineq[i],
5202 offset, d, 1 + total))
5203 continue;
5204 for (j = i + 1; j < n; ++j) {
5205 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5206 hull->ineq[j] + 1, total))
5207 continue;
5208 isl_int_add(sum, hull->ineq[i][0],
5209 hull->ineq[j][0]);
5210 if (isl_int_abs_lt(sum,
5211 hull->ineq[i][offset + d]))
5212 break;
5215 if (j >= n)
5216 continue;
5217 isl_int_clear(sum);
5218 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5219 j = i;
5220 return pw_multi_aff_from_map_div(map, hull, d, j);
5223 isl_int_clear(sum);
5224 isl_basic_map_free(hull);
5225 return pw_multi_aff_from_map_base(map);
5226 error:
5227 isl_map_free(map);
5228 isl_basic_map_free(hull);
5229 return NULL;
5232 /* Given an affine expression
5234 * [A -> B] -> f(A,B)
5236 * construct an isl_multi_aff
5238 * [A -> B] -> B'
5240 * such that dimension "d" in B' is set to "aff" and the remaining
5241 * dimensions are set equal to the corresponding dimensions in B.
5242 * "n_in" is the dimension of the space A.
5243 * "n_out" is the dimension of the space B.
5245 * If "is_set" is set, then the affine expression is of the form
5247 * [B] -> f(B)
5249 * and we construct an isl_multi_aff
5251 * B -> B'
5253 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5254 unsigned n_in, unsigned n_out, int is_set)
5256 int i;
5257 isl_multi_aff *ma;
5258 isl_space *space, *space2;
5259 isl_local_space *ls;
5261 space = isl_aff_get_domain_space(aff);
5262 ls = isl_local_space_from_space(isl_space_copy(space));
5263 space2 = isl_space_copy(space);
5264 if (!is_set)
5265 space2 = isl_space_range(isl_space_unwrap(space2));
5266 space = isl_space_map_from_domain_and_range(space, space2);
5267 ma = isl_multi_aff_alloc(space);
5268 ma = isl_multi_aff_set_aff(ma, d, aff);
5270 for (i = 0; i < n_out; ++i) {
5271 if (i == d)
5272 continue;
5273 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5274 isl_dim_set, n_in + i);
5275 ma = isl_multi_aff_set_aff(ma, i, aff);
5278 isl_local_space_free(ls);
5280 return ma;
5283 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5284 * taking into account that the dimension at position "d" can be written as
5286 * x = m a + f(..) (1)
5288 * where m is equal to "gcd".
5289 * "i" is the index of the equality in "hull" that defines f(..).
5290 * In particular, the equality is of the form
5292 * f(..) - x + m g(existentials) = 0
5294 * or
5296 * -f(..) + x + m g(existentials) = 0
5298 * We basically plug (1) into "map", resulting in a map with "a"
5299 * in the range instead of "x". The corresponding isl_pw_multi_aff
5300 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5302 * Specifically, given the input map
5304 * A -> B
5306 * We first wrap it into a set
5308 * [A -> B]
5310 * and define (1) on top of the corresponding space, resulting in "aff".
5311 * We use this to create an isl_multi_aff that maps the output position "d"
5312 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5313 * We plug this into the wrapped map, unwrap the result and compute the
5314 * corresponding isl_pw_multi_aff.
5315 * The result is an expression
5317 * A -> T(A)
5319 * We adjust that to
5321 * A -> [A -> T(A)]
5323 * so that we can plug that into "aff", after extending the latter to
5324 * a mapping
5326 * [A -> B] -> B'
5329 * If "map" is actually a set, then there is no "A" space, meaning
5330 * that we do not need to perform any wrapping, and that the result
5331 * of the recursive call is of the form
5333 * [T]
5335 * which is plugged into a mapping of the form
5337 * B -> B'
5339 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5340 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5341 isl_int gcd)
5343 isl_set *set;
5344 isl_space *space;
5345 isl_local_space *ls;
5346 isl_aff *aff;
5347 isl_multi_aff *ma;
5348 isl_pw_multi_aff *pma, *id;
5349 isl_size n_in;
5350 unsigned o_out;
5351 isl_size n_out;
5352 isl_bool is_set;
5354 is_set = isl_map_is_set(map);
5355 if (is_set < 0)
5356 goto error;
5358 n_in = isl_basic_map_dim(hull, isl_dim_in);
5359 n_out = isl_basic_map_dim(hull, isl_dim_out);
5360 if (n_in < 0 || n_out < 0)
5361 goto error;
5362 o_out = isl_basic_map_offset(hull, isl_dim_out);
5364 if (is_set)
5365 set = map;
5366 else
5367 set = isl_map_wrap(map);
5368 space = isl_space_map_from_set(isl_set_get_space(set));
5369 ma = isl_multi_aff_identity(space);
5370 ls = isl_local_space_from_space(isl_set_get_space(set));
5371 aff = isl_aff_alloc(ls);
5372 if (aff) {
5373 isl_int_set_si(aff->v->el[0], 1);
5374 if (isl_int_is_one(hull->eq[i][o_out + d]))
5375 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5376 aff->v->size - 1);
5377 else
5378 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5379 aff->v->size - 1);
5380 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5382 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5383 set = isl_set_preimage_multi_aff(set, ma);
5385 ma = range_map(aff, d, n_in, n_out, is_set);
5387 if (is_set)
5388 map = set;
5389 else
5390 map = isl_set_unwrap(set);
5391 pma = isl_pw_multi_aff_from_map(map);
5393 if (!is_set) {
5394 space = isl_pw_multi_aff_get_domain_space(pma);
5395 space = isl_space_map_from_set(space);
5396 id = isl_pw_multi_aff_identity(space);
5397 pma = isl_pw_multi_aff_range_product(id, pma);
5399 id = isl_pw_multi_aff_from_multi_aff(ma);
5400 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5402 isl_basic_map_free(hull);
5403 return pma;
5404 error:
5405 isl_map_free(map);
5406 isl_basic_map_free(hull);
5407 return NULL;
5410 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5411 * "hull" contains the equalities valid for "map".
5413 * Check if any of the output dimensions is "strided".
5414 * That is, we check if it can be written as
5416 * x = m a + f(..)
5418 * with m greater than 1, a some combination of existentially quantified
5419 * variables and f an expression in the parameters and input dimensions.
5420 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5422 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5423 * special case.
5425 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5426 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5428 int i, j;
5429 isl_size n_out;
5430 unsigned o_out;
5431 isl_size n_div;
5432 unsigned o_div;
5433 isl_int gcd;
5435 n_div = isl_basic_map_dim(hull, isl_dim_div);
5436 n_out = isl_basic_map_dim(hull, isl_dim_out);
5437 if (n_div < 0 || n_out < 0)
5438 goto error;
5440 if (n_div == 0) {
5441 isl_basic_map_free(hull);
5442 return pw_multi_aff_from_map_check_div(map);
5445 isl_int_init(gcd);
5447 o_div = isl_basic_map_offset(hull, isl_dim_div);
5448 o_out = isl_basic_map_offset(hull, isl_dim_out);
5450 for (i = 0; i < n_out; ++i) {
5451 for (j = 0; j < hull->n_eq; ++j) {
5452 isl_int *eq = hull->eq[j];
5453 isl_pw_multi_aff *res;
5455 if (!isl_int_is_one(eq[o_out + i]) &&
5456 !isl_int_is_negone(eq[o_out + i]))
5457 continue;
5458 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5459 continue;
5460 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5461 n_out - (i + 1)) != -1)
5462 continue;
5463 isl_seq_gcd(eq + o_div, n_div, &gcd);
5464 if (isl_int_is_zero(gcd))
5465 continue;
5466 if (isl_int_is_one(gcd))
5467 continue;
5469 res = pw_multi_aff_from_map_stride(map, hull,
5470 i, j, gcd);
5471 isl_int_clear(gcd);
5472 return res;
5476 isl_int_clear(gcd);
5477 isl_basic_map_free(hull);
5478 return pw_multi_aff_from_map_check_div(map);
5479 error:
5480 isl_map_free(map);
5481 isl_basic_map_free(hull);
5482 return NULL;
5485 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5487 * As a special case, we first check if all output dimensions are uniquely
5488 * defined in terms of the parameters and input dimensions over the entire
5489 * domain. If so, we extract the desired isl_pw_multi_aff directly
5490 * from the affine hull of "map" and its domain.
5492 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5493 * special cases.
5495 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5497 isl_bool sv;
5498 isl_size n;
5499 isl_basic_map *hull;
5501 n = isl_map_n_basic_map(map);
5502 if (n < 0)
5503 goto error;
5505 if (n == 1) {
5506 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5507 hull = isl_basic_map_plain_affine_hull(hull);
5508 sv = isl_basic_map_plain_is_single_valued(hull);
5509 if (sv >= 0 && sv)
5510 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5511 hull);
5512 isl_basic_map_free(hull);
5514 map = isl_map_detect_equalities(map);
5515 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5516 sv = isl_basic_map_plain_is_single_valued(hull);
5517 if (sv >= 0 && sv)
5518 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5519 if (sv >= 0)
5520 return pw_multi_aff_from_map_check_strides(map, hull);
5521 isl_basic_map_free(hull);
5522 error:
5523 isl_map_free(map);
5524 return NULL;
5527 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5528 * but is considered as a function on an isl_map when exported.
5530 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5532 return isl_pw_multi_aff_from_map(map);
5535 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5537 return isl_pw_multi_aff_from_map(set);
5540 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5541 * but is considered as a function on an isl_set when exported.
5543 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5545 return isl_pw_multi_aff_from_set(set);
5548 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5549 * add it to *user.
5551 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5553 isl_union_pw_multi_aff **upma = user;
5554 isl_pw_multi_aff *pma;
5556 pma = isl_pw_multi_aff_from_map(map);
5557 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5559 return *upma ? isl_stat_ok : isl_stat_error;
5562 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5563 * domain.
5565 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5566 __isl_take isl_aff *aff)
5568 isl_multi_aff *ma;
5569 isl_pw_multi_aff *pma;
5571 ma = isl_multi_aff_from_aff(aff);
5572 pma = isl_pw_multi_aff_from_multi_aff(ma);
5573 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5576 /* Try and create an isl_union_pw_multi_aff that is equivalent
5577 * to the given isl_union_map.
5578 * The isl_union_map is required to be single-valued in each space.
5579 * Otherwise, an error is produced.
5581 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5582 __isl_take isl_union_map *umap)
5584 isl_space *space;
5585 isl_union_pw_multi_aff *upma;
5587 space = isl_union_map_get_space(umap);
5588 upma = isl_union_pw_multi_aff_empty(space);
5589 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5590 upma = isl_union_pw_multi_aff_free(upma);
5591 isl_union_map_free(umap);
5593 return upma;
5596 /* This function performs the same operation as
5597 * isl_union_pw_multi_aff_from_union_map,
5598 * but is considered as a function on an isl_union_map when exported.
5600 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5601 __isl_take isl_union_map *umap)
5603 return isl_union_pw_multi_aff_from_union_map(umap);
5606 /* Try and create an isl_union_pw_multi_aff that is equivalent
5607 * to the given isl_union_set.
5608 * The isl_union_set is required to be a singleton in each space.
5609 * Otherwise, an error is produced.
5611 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5612 __isl_take isl_union_set *uset)
5614 return isl_union_pw_multi_aff_from_union_map(uset);
5617 /* Return the piecewise affine expression "set ? 1 : 0".
5619 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5621 isl_pw_aff *pa;
5622 isl_space *space = isl_set_get_space(set);
5623 isl_local_space *ls = isl_local_space_from_space(space);
5624 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5625 isl_aff *one = isl_aff_zero_on_domain(ls);
5627 one = isl_aff_add_constant_si(one, 1);
5628 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5629 set = isl_set_complement(set);
5630 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5632 return pa;
5635 /* Plug in "subs" for dimension "type", "pos" of "aff".
5637 * Let i be the dimension to replace and let "subs" be of the form
5639 * f/d
5641 * and "aff" of the form
5643 * (a i + g)/m
5645 * The result is
5647 * (a f + d g')/(m d)
5649 * where g' is the result of plugging in "subs" in each of the integer
5650 * divisions in g.
5652 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5653 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5655 isl_ctx *ctx;
5656 isl_int v;
5657 isl_size n_div;
5659 aff = isl_aff_cow(aff);
5660 if (!aff || !subs)
5661 return isl_aff_free(aff);
5663 ctx = isl_aff_get_ctx(aff);
5664 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5665 isl_die(ctx, isl_error_invalid,
5666 "spaces don't match", return isl_aff_free(aff));
5667 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5668 if (n_div < 0)
5669 return isl_aff_free(aff);
5670 if (n_div != 0)
5671 isl_die(ctx, isl_error_unsupported,
5672 "cannot handle divs yet", return isl_aff_free(aff));
5674 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5675 if (!aff->ls)
5676 return isl_aff_free(aff);
5678 aff->v = isl_vec_cow(aff->v);
5679 if (!aff->v)
5680 return isl_aff_free(aff);
5682 pos += isl_local_space_offset(aff->ls, type);
5684 isl_int_init(v);
5685 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5686 aff->v->size, subs->v->size, v);
5687 isl_int_clear(v);
5689 return aff;
5692 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5693 * expressions in "maff".
5695 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5696 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5697 __isl_keep isl_aff *subs)
5699 int i;
5701 maff = isl_multi_aff_cow(maff);
5702 if (!maff || !subs)
5703 return isl_multi_aff_free(maff);
5705 if (type == isl_dim_in)
5706 type = isl_dim_set;
5708 for (i = 0; i < maff->n; ++i) {
5709 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5710 type, pos, subs);
5711 if (!maff->u.p[i])
5712 return isl_multi_aff_free(maff);
5715 return maff;
5718 /* Plug in "subs" for dimension "type", "pos" of "pma".
5720 * pma is of the form
5722 * A_i(v) -> M_i(v)
5724 * while subs is of the form
5726 * v' = B_j(v) -> S_j
5728 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5729 * has a contribution in the result, in particular
5731 * C_ij(S_j) -> M_i(S_j)
5733 * Note that plugging in S_j in C_ij may also result in an empty set
5734 * and this contribution should simply be discarded.
5736 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5737 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5738 __isl_keep isl_pw_aff *subs)
5740 int i, j, n;
5741 isl_pw_multi_aff *res;
5743 if (!pma || !subs)
5744 return isl_pw_multi_aff_free(pma);
5746 n = pma->n * subs->n;
5747 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5749 for (i = 0; i < pma->n; ++i) {
5750 for (j = 0; j < subs->n; ++j) {
5751 isl_set *common;
5752 isl_multi_aff *res_ij;
5753 int empty;
5755 common = isl_set_intersect(
5756 isl_set_copy(pma->p[i].set),
5757 isl_set_copy(subs->p[j].set));
5758 common = isl_set_substitute(common,
5759 type, pos, subs->p[j].aff);
5760 empty = isl_set_plain_is_empty(common);
5761 if (empty < 0 || empty) {
5762 isl_set_free(common);
5763 if (empty < 0)
5764 goto error;
5765 continue;
5768 res_ij = isl_multi_aff_substitute(
5769 isl_multi_aff_copy(pma->p[i].maff),
5770 type, pos, subs->p[j].aff);
5772 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5776 isl_pw_multi_aff_free(pma);
5777 return res;
5778 error:
5779 isl_pw_multi_aff_free(pma);
5780 isl_pw_multi_aff_free(res);
5781 return NULL;
5784 /* Compute the preimage of a range of dimensions in the affine expression "src"
5785 * under "ma" and put the result in "dst". The number of dimensions in "src"
5786 * that precede the range is given by "n_before". The number of dimensions
5787 * in the range is given by the number of output dimensions of "ma".
5788 * The number of dimensions that follow the range is given by "n_after".
5789 * If "has_denom" is set (to one),
5790 * then "src" and "dst" have an extra initial denominator.
5791 * "n_div_ma" is the number of existentials in "ma"
5792 * "n_div_bset" is the number of existentials in "src"
5793 * The resulting "dst" (which is assumed to have been allocated by
5794 * the caller) contains coefficients for both sets of existentials,
5795 * first those in "ma" and then those in "src".
5796 * f, c1, c2 and g are temporary objects that have been initialized
5797 * by the caller.
5799 * Let src represent the expression
5801 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5803 * and let ma represent the expressions
5805 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5807 * We start out with the following expression for dst:
5809 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5811 * with the multiplication factor f initially equal to 1
5812 * and f \sum_i b_i v_i kept separately.
5813 * For each x_i that we substitute, we multiply the numerator
5814 * (and denominator) of dst by c_1 = m_i and add the numerator
5815 * of the x_i expression multiplied by c_2 = f b_i,
5816 * after removing the common factors of c_1 and c_2.
5817 * The multiplication factor f also needs to be multiplied by c_1
5818 * for the next x_j, j > i.
5820 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5821 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5822 int n_div_ma, int n_div_bmap,
5823 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5825 int i;
5826 isl_size n_param, n_in, n_out;
5827 int o_dst, o_src;
5829 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5830 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5831 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5832 if (n_param < 0 || n_in < 0 || n_out < 0)
5833 return isl_stat_error;
5835 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5836 o_dst = o_src = has_denom + 1 + n_param + n_before;
5837 isl_seq_clr(dst + o_dst, n_in);
5838 o_dst += n_in;
5839 o_src += n_out;
5840 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5841 o_dst += n_after;
5842 o_src += n_after;
5843 isl_seq_clr(dst + o_dst, n_div_ma);
5844 o_dst += n_div_ma;
5845 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5847 isl_int_set_si(f, 1);
5849 for (i = 0; i < n_out; ++i) {
5850 int offset = has_denom + 1 + n_param + n_before + i;
5852 if (isl_int_is_zero(src[offset]))
5853 continue;
5854 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5855 isl_int_mul(c2, f, src[offset]);
5856 isl_int_gcd(g, c1, c2);
5857 isl_int_divexact(c1, c1, g);
5858 isl_int_divexact(c2, c2, g);
5860 isl_int_mul(f, f, c1);
5861 o_dst = has_denom;
5862 o_src = 1;
5863 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5864 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5865 o_dst += 1 + n_param;
5866 o_src += 1 + n_param;
5867 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5868 o_dst += n_before;
5869 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5870 c2, ma->u.p[i]->v->el + o_src, n_in);
5871 o_dst += n_in;
5872 o_src += n_in;
5873 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5874 o_dst += n_after;
5875 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5876 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5877 o_dst += n_div_ma;
5878 o_src += n_div_ma;
5879 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5880 if (has_denom)
5881 isl_int_mul(dst[0], dst[0], c1);
5884 return isl_stat_ok;
5887 /* Compute the pullback of "aff" by the function represented by "ma".
5888 * In other words, plug in "ma" in "aff". The result is an affine expression
5889 * defined over the domain space of "ma".
5891 * If "aff" is represented by
5893 * (a(p) + b x + c(divs))/d
5895 * and ma is represented by
5897 * x = D(p) + F(y) + G(divs')
5899 * then the result is
5901 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5903 * The divs in the local space of the input are similarly adjusted
5904 * through a call to isl_local_space_preimage_multi_aff.
5906 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5907 __isl_take isl_multi_aff *ma)
5909 isl_aff *res = NULL;
5910 isl_local_space *ls;
5911 isl_size n_div_aff, n_div_ma;
5912 isl_int f, c1, c2, g;
5914 ma = isl_multi_aff_align_divs(ma);
5915 if (!aff || !ma)
5916 goto error;
5918 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5919 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5920 if (n_div_aff < 0 || n_div_ma < 0)
5921 goto error;
5923 ls = isl_aff_get_domain_local_space(aff);
5924 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5925 res = isl_aff_alloc(ls);
5926 if (!res)
5927 goto error;
5929 isl_int_init(f);
5930 isl_int_init(c1);
5931 isl_int_init(c2);
5932 isl_int_init(g);
5934 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5935 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5936 res = isl_aff_free(res);
5938 isl_int_clear(f);
5939 isl_int_clear(c1);
5940 isl_int_clear(c2);
5941 isl_int_clear(g);
5943 isl_aff_free(aff);
5944 isl_multi_aff_free(ma);
5945 res = isl_aff_normalize(res);
5946 return res;
5947 error:
5948 isl_aff_free(aff);
5949 isl_multi_aff_free(ma);
5950 isl_aff_free(res);
5951 return NULL;
5954 /* Compute the pullback of "aff1" by the function represented by "aff2".
5955 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5956 * defined over the domain space of "aff1".
5958 * The domain of "aff1" should match the range of "aff2", which means
5959 * that it should be single-dimensional.
5961 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5962 __isl_take isl_aff *aff2)
5964 isl_multi_aff *ma;
5966 ma = isl_multi_aff_from_aff(aff2);
5967 return isl_aff_pullback_multi_aff(aff1, ma);
5970 /* Compute the pullback of "ma1" by the function represented by "ma2".
5971 * In other words, plug in "ma2" in "ma1".
5973 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5974 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5976 int i;
5977 isl_space *space = NULL;
5979 isl_multi_aff_align_params_bin(&ma1, &ma2);
5980 ma2 = isl_multi_aff_align_divs(ma2);
5981 ma1 = isl_multi_aff_cow(ma1);
5982 if (!ma1 || !ma2)
5983 goto error;
5985 space = isl_space_join(isl_multi_aff_get_space(ma2),
5986 isl_multi_aff_get_space(ma1));
5988 for (i = 0; i < ma1->n; ++i) {
5989 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5990 isl_multi_aff_copy(ma2));
5991 if (!ma1->u.p[i])
5992 goto error;
5995 ma1 = isl_multi_aff_reset_space(ma1, space);
5996 isl_multi_aff_free(ma2);
5997 return ma1;
5998 error:
5999 isl_space_free(space);
6000 isl_multi_aff_free(ma2);
6001 isl_multi_aff_free(ma1);
6002 return NULL;
6005 /* Extend the local space of "dst" to include the divs
6006 * in the local space of "src".
6008 * If "src" does not have any divs or if the local spaces of "dst" and
6009 * "src" are the same, then no extension is required.
6011 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6012 __isl_keep isl_aff *src)
6014 isl_ctx *ctx;
6015 isl_size src_n_div, dst_n_div;
6016 int *exp1 = NULL;
6017 int *exp2 = NULL;
6018 isl_bool equal;
6019 isl_mat *div;
6021 if (!src || !dst)
6022 return isl_aff_free(dst);
6024 ctx = isl_aff_get_ctx(src);
6025 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6026 if (equal < 0)
6027 return isl_aff_free(dst);
6028 if (!equal)
6029 isl_die(ctx, isl_error_invalid,
6030 "spaces don't match", goto error);
6032 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6033 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6034 if (src_n_div == 0)
6035 return dst;
6036 equal = isl_local_space_is_equal(src->ls, dst->ls);
6037 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6038 return isl_aff_free(dst);
6039 if (equal)
6040 return dst;
6042 exp1 = isl_alloc_array(ctx, int, src_n_div);
6043 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6044 if (!exp1 || (dst_n_div && !exp2))
6045 goto error;
6047 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6048 dst = isl_aff_expand_divs(dst, div, exp2);
6049 free(exp1);
6050 free(exp2);
6052 return dst;
6053 error:
6054 free(exp1);
6055 free(exp2);
6056 return isl_aff_free(dst);
6059 /* Adjust the local spaces of the affine expressions in "maff"
6060 * such that they all have the save divs.
6062 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6063 __isl_take isl_multi_aff *maff)
6065 int i;
6067 if (!maff)
6068 return NULL;
6069 if (maff->n == 0)
6070 return maff;
6071 maff = isl_multi_aff_cow(maff);
6072 if (!maff)
6073 return NULL;
6075 for (i = 1; i < maff->n; ++i)
6076 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
6077 for (i = 1; i < maff->n; ++i) {
6078 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
6079 if (!maff->u.p[i])
6080 return isl_multi_aff_free(maff);
6083 return maff;
6086 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6088 aff = isl_aff_cow(aff);
6089 if (!aff)
6090 return NULL;
6092 aff->ls = isl_local_space_lift(aff->ls);
6093 if (!aff->ls)
6094 return isl_aff_free(aff);
6096 return aff;
6099 /* Lift "maff" to a space with extra dimensions such that the result
6100 * has no more existentially quantified variables.
6101 * If "ls" is not NULL, then *ls is assigned the local space that lies
6102 * at the basis of the lifting applied to "maff".
6104 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6105 __isl_give isl_local_space **ls)
6107 int i;
6108 isl_space *space;
6109 isl_size n_div;
6111 if (ls)
6112 *ls = NULL;
6114 if (!maff)
6115 return NULL;
6117 if (maff->n == 0) {
6118 if (ls) {
6119 isl_space *space = isl_multi_aff_get_domain_space(maff);
6120 *ls = isl_local_space_from_space(space);
6121 if (!*ls)
6122 return isl_multi_aff_free(maff);
6124 return maff;
6127 maff = isl_multi_aff_cow(maff);
6128 maff = isl_multi_aff_align_divs(maff);
6129 if (!maff)
6130 return NULL;
6132 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
6133 if (n_div < 0)
6134 return isl_multi_aff_free(maff);
6135 space = isl_multi_aff_get_space(maff);
6136 space = isl_space_lift(isl_space_domain(space), n_div);
6137 space = isl_space_extend_domain_with_range(space,
6138 isl_multi_aff_get_space(maff));
6139 if (!space)
6140 return isl_multi_aff_free(maff);
6141 isl_space_free(maff->space);
6142 maff->space = space;
6144 if (ls) {
6145 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
6146 if (!*ls)
6147 return isl_multi_aff_free(maff);
6150 for (i = 0; i < maff->n; ++i) {
6151 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
6152 if (!maff->u.p[i])
6153 goto error;
6156 return maff;
6157 error:
6158 if (ls)
6159 isl_local_space_free(*ls);
6160 return isl_multi_aff_free(maff);
6163 #undef TYPE
6164 #define TYPE isl_pw_multi_aff
6165 static
6166 #include "check_type_range_templ.c"
6168 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6170 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6171 __isl_keep isl_pw_multi_aff *pma, int pos)
6173 int i;
6174 isl_size n_out;
6175 isl_space *space;
6176 isl_pw_aff *pa;
6178 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6179 return NULL;
6181 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6182 if (n_out < 0)
6183 return NULL;
6185 space = isl_pw_multi_aff_get_space(pma);
6186 space = isl_space_drop_dims(space, isl_dim_out,
6187 pos + 1, n_out - pos - 1);
6188 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6190 pa = isl_pw_aff_alloc_size(space, pma->n);
6191 for (i = 0; i < pma->n; ++i) {
6192 isl_aff *aff;
6193 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6194 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6197 return pa;
6200 /* This is an alternative name for the function above.
6202 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6203 __isl_keep isl_pw_multi_aff *pma, int pos)
6205 return isl_pw_multi_aff_get_at(pma, pos);
6208 /* Return an isl_pw_multi_aff with the given "set" as domain and
6209 * an unnamed zero-dimensional range.
6211 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6212 __isl_take isl_set *set)
6214 isl_multi_aff *ma;
6215 isl_space *space;
6217 space = isl_set_get_space(set);
6218 space = isl_space_from_domain(space);
6219 ma = isl_multi_aff_zero(space);
6220 return isl_pw_multi_aff_alloc(set, ma);
6223 /* Add an isl_pw_multi_aff with the given "set" as domain and
6224 * an unnamed zero-dimensional range to *user.
6226 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6227 void *user)
6229 isl_union_pw_multi_aff **upma = user;
6230 isl_pw_multi_aff *pma;
6232 pma = isl_pw_multi_aff_from_domain(set);
6233 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6235 return isl_stat_ok;
6238 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6239 * an unnamed zero-dimensional range.
6241 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6242 __isl_take isl_union_set *uset)
6244 isl_space *space;
6245 isl_union_pw_multi_aff *upma;
6247 if (!uset)
6248 return NULL;
6250 space = isl_union_set_get_space(uset);
6251 upma = isl_union_pw_multi_aff_empty(space);
6253 if (isl_union_set_foreach_set(uset,
6254 &add_pw_multi_aff_from_domain, &upma) < 0)
6255 goto error;
6257 isl_union_set_free(uset);
6258 return upma;
6259 error:
6260 isl_union_set_free(uset);
6261 isl_union_pw_multi_aff_free(upma);
6262 return NULL;
6265 /* Local data for bin_entry and the callback "fn".
6267 struct isl_union_pw_multi_aff_bin_data {
6268 isl_union_pw_multi_aff *upma2;
6269 isl_union_pw_multi_aff *res;
6270 isl_pw_multi_aff *pma;
6271 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6274 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6275 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6277 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6279 struct isl_union_pw_multi_aff_bin_data *data = user;
6280 isl_stat r;
6282 data->pma = pma;
6283 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6284 data->fn, data);
6285 isl_pw_multi_aff_free(pma);
6287 return r;
6290 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6291 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6292 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6293 * as *entry. The callback should adjust data->res if desired.
6295 static __isl_give isl_union_pw_multi_aff *bin_op(
6296 __isl_take isl_union_pw_multi_aff *upma1,
6297 __isl_take isl_union_pw_multi_aff *upma2,
6298 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6300 isl_space *space;
6301 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6303 space = isl_union_pw_multi_aff_get_space(upma2);
6304 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6305 space = isl_union_pw_multi_aff_get_space(upma1);
6306 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6308 if (!upma1 || !upma2)
6309 goto error;
6311 data.upma2 = upma2;
6312 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6313 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6314 &bin_entry, &data) < 0)
6315 goto error;
6317 isl_union_pw_multi_aff_free(upma1);
6318 isl_union_pw_multi_aff_free(upma2);
6319 return data.res;
6320 error:
6321 isl_union_pw_multi_aff_free(upma1);
6322 isl_union_pw_multi_aff_free(upma2);
6323 isl_union_pw_multi_aff_free(data.res);
6324 return NULL;
6327 /* Given two isl_pw_multi_affs A -> B and C -> D,
6328 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6330 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6331 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6333 isl_space *space;
6335 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6336 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6337 isl_pw_multi_aff_get_space(pma2));
6338 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6339 &isl_multi_aff_range_product);
6342 /* Given two isl_pw_multi_affs A -> B and C -> D,
6343 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6345 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6346 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6348 isl_space *space;
6350 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6351 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6352 isl_pw_multi_aff_get_space(pma2));
6353 space = isl_space_flatten_range(space);
6354 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6355 &isl_multi_aff_flat_range_product);
6358 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6359 * to compute some form of range product and add the result to data->res.
6361 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6362 __isl_give isl_pw_multi_aff *(*range_product)(
6363 __isl_take isl_pw_multi_aff *pma1,
6364 __isl_take isl_pw_multi_aff *pma2),
6365 void *user)
6367 struct isl_union_pw_multi_aff_bin_data *data = user;
6368 isl_bool match;
6369 isl_space *space1, *space2;
6371 space1 = isl_pw_multi_aff_peek_space(data->pma);
6372 space2 = isl_pw_multi_aff_peek_space(pma2);
6373 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6374 space2, isl_dim_in);
6375 if (match < 0 || !match) {
6376 isl_pw_multi_aff_free(pma2);
6377 return match < 0 ? isl_stat_error : isl_stat_ok;
6380 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6382 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6384 return isl_stat_ok;
6387 /* If data->pma and "pma2" have the same domain space, then compute
6388 * their flat range product and add the result to data->res.
6390 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6391 void *user)
6393 return gen_range_product_entry(pma2,
6394 &isl_pw_multi_aff_flat_range_product, user);
6397 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6398 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6400 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6401 __isl_take isl_union_pw_multi_aff *upma1,
6402 __isl_take isl_union_pw_multi_aff *upma2)
6404 return bin_op(upma1, upma2, &flat_range_product_entry);
6407 /* If data->pma and "pma2" have the same domain space, then compute
6408 * their range product and add the result to data->res.
6410 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6411 void *user)
6413 return gen_range_product_entry(pma2,
6414 &isl_pw_multi_aff_range_product, user);
6417 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6418 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6420 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6421 __isl_take isl_union_pw_multi_aff *upma1,
6422 __isl_take isl_union_pw_multi_aff *upma2)
6424 return bin_op(upma1, upma2, &range_product_entry);
6427 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6428 * The parameters are assumed to have been aligned.
6430 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6431 * except that it works on two different isl_pw_* types.
6433 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6434 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6435 __isl_take isl_pw_aff *pa)
6437 int i, j, n;
6438 isl_pw_multi_aff *res = NULL;
6440 if (!pma || !pa)
6441 goto error;
6443 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6444 pa->dim, isl_dim_in))
6445 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6446 "domains don't match", goto error);
6447 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6448 goto error;
6450 n = pma->n * pa->n;
6451 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6453 for (i = 0; i < pma->n; ++i) {
6454 for (j = 0; j < pa->n; ++j) {
6455 isl_set *common;
6456 isl_multi_aff *res_ij;
6457 int empty;
6459 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6460 isl_set_copy(pa->p[j].set));
6461 empty = isl_set_plain_is_empty(common);
6462 if (empty < 0 || empty) {
6463 isl_set_free(common);
6464 if (empty < 0)
6465 goto error;
6466 continue;
6469 res_ij = isl_multi_aff_set_aff(
6470 isl_multi_aff_copy(pma->p[i].maff), pos,
6471 isl_aff_copy(pa->p[j].aff));
6472 res_ij = isl_multi_aff_gist(res_ij,
6473 isl_set_copy(common));
6475 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6479 isl_pw_multi_aff_free(pma);
6480 isl_pw_aff_free(pa);
6481 return res;
6482 error:
6483 isl_pw_multi_aff_free(pma);
6484 isl_pw_aff_free(pa);
6485 return isl_pw_multi_aff_free(res);
6488 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6490 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6491 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6492 __isl_take isl_pw_aff *pa)
6494 isl_bool equal_params;
6496 if (!pma || !pa)
6497 goto error;
6498 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6499 if (equal_params < 0)
6500 goto error;
6501 if (equal_params)
6502 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6503 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6504 isl_pw_aff_check_named_params(pa) < 0)
6505 goto error;
6506 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6507 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6508 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6509 error:
6510 isl_pw_multi_aff_free(pma);
6511 isl_pw_aff_free(pa);
6512 return NULL;
6515 /* Do the parameters of "pa" match those of "space"?
6517 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6518 __isl_keep isl_space *space)
6520 isl_space *pa_space;
6521 isl_bool match;
6523 if (!pa || !space)
6524 return isl_bool_error;
6526 pa_space = isl_pw_aff_get_space(pa);
6528 match = isl_space_has_equal_params(space, pa_space);
6530 isl_space_free(pa_space);
6531 return match;
6534 /* Check that the domain space of "pa" matches "space".
6536 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6537 __isl_keep isl_space *space)
6539 isl_space *pa_space;
6540 isl_bool match;
6542 if (!pa || !space)
6543 return isl_stat_error;
6545 pa_space = isl_pw_aff_get_space(pa);
6547 match = isl_space_has_equal_params(space, pa_space);
6548 if (match < 0)
6549 goto error;
6550 if (!match)
6551 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6552 "parameters don't match", goto error);
6553 match = isl_space_tuple_is_equal(space, isl_dim_in,
6554 pa_space, isl_dim_in);
6555 if (match < 0)
6556 goto error;
6557 if (!match)
6558 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6559 "domains don't match", goto error);
6560 isl_space_free(pa_space);
6561 return isl_stat_ok;
6562 error:
6563 isl_space_free(pa_space);
6564 return isl_stat_error;
6567 #undef BASE
6568 #define BASE pw_aff
6569 #undef DOMBASE
6570 #define DOMBASE set
6572 #include <isl_multi_explicit_domain.c>
6573 #include <isl_multi_pw_aff_explicit_domain.c>
6574 #include <isl_multi_templ.c>
6575 #include <isl_multi_add_constant_templ.c>
6576 #include <isl_multi_apply_set.c>
6577 #include <isl_multi_arith_templ.c>
6578 #include <isl_multi_bind_templ.c>
6579 #include <isl_multi_bind_domain_templ.c>
6580 #include <isl_multi_coalesce.c>
6581 #include <isl_multi_domain_templ.c>
6582 #include <isl_multi_dim_id_templ.c>
6583 #include <isl_multi_dims.c>
6584 #include <isl_multi_from_base_templ.c>
6585 #include <isl_multi_gist.c>
6586 #include <isl_multi_hash.c>
6587 #include <isl_multi_identity_templ.c>
6588 #include <isl_multi_align_set.c>
6589 #include <isl_multi_insert_domain_templ.c>
6590 #include <isl_multi_intersect.c>
6591 #include <isl_multi_min_max_templ.c>
6592 #include <isl_multi_move_dims_templ.c>
6593 #include <isl_multi_nan_templ.c>
6594 #include <isl_multi_param_templ.c>
6595 #include <isl_multi_product_templ.c>
6596 #include <isl_multi_splice_templ.c>
6597 #include <isl_multi_tuple_id_templ.c>
6598 #include <isl_multi_union_add_templ.c>
6599 #include <isl_multi_zero_templ.c>
6600 #include <isl_multi_unbind_params_templ.c>
6602 /* Is every element of "mpa" defined over a single universe domain?
6604 isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
6606 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
6609 /* Given that every element of "mpa" is defined over a single universe domain,
6610 * return the corresponding base expressions.
6612 __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
6613 __isl_take isl_multi_pw_aff *mpa)
6615 int i;
6616 isl_size n;
6617 isl_multi_aff *ma;
6619 n = isl_multi_pw_aff_size(mpa);
6620 if (n < 0)
6621 mpa = isl_multi_pw_aff_free(mpa);
6622 ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
6623 for (i = 0; i < n; ++i) {
6624 isl_aff *aff;
6626 aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
6627 ma = isl_multi_aff_set_aff(ma, i, aff);
6629 isl_multi_pw_aff_free(mpa);
6630 return ma;
6633 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6634 * with this explicit domain.
6636 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6637 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6639 isl_set *dom;
6641 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6642 return map;
6644 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6645 map = isl_map_intersect_domain(map, dom);
6647 return map;
6650 /* Are all elements of "mpa" piecewise constants?
6652 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6654 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6657 /* Does "mpa" have a non-trivial explicit domain?
6659 * The explicit domain, if present, is trivial if it represents
6660 * an (obviously) universe set.
6662 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6663 __isl_keep isl_multi_pw_aff *mpa)
6665 if (!mpa)
6666 return isl_bool_error;
6667 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6668 return isl_bool_false;
6669 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6672 #undef BASE
6673 #define BASE set
6675 #include "isl_opt_mpa_templ.c"
6677 /* Compute the minima of the set dimensions as a function of the
6678 * parameters, but independently of the other set dimensions.
6680 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6682 return set_opt_mpa(set, &isl_set_dim_min);
6685 /* Compute the maxima of the set dimensions as a function of the
6686 * parameters, but independently of the other set dimensions.
6688 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6690 return set_opt_mpa(set, &isl_set_dim_max);
6693 #undef BASE
6694 #define BASE map
6696 #include "isl_opt_mpa_templ.c"
6698 /* Compute the minima of the output dimensions as a function of the
6699 * parameters and input dimensions, but independently of
6700 * the other output dimensions.
6702 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6704 return map_opt_mpa(map, &isl_map_dim_min);
6707 /* Compute the maxima of the output dimensions as a function of the
6708 * parameters and input dimensions, but independently of
6709 * the other output dimensions.
6711 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6713 return map_opt_mpa(map, &isl_map_dim_max);
6716 /* Scale the elements of "pma" by the corresponding elements of "mv".
6718 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6719 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6721 int i;
6722 isl_bool equal_params;
6724 pma = isl_pw_multi_aff_cow(pma);
6725 if (!pma || !mv)
6726 goto error;
6727 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6728 mv->space, isl_dim_set))
6729 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6730 "spaces don't match", goto error);
6731 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6732 if (equal_params < 0)
6733 goto error;
6734 if (!equal_params) {
6735 pma = isl_pw_multi_aff_align_params(pma,
6736 isl_multi_val_get_space(mv));
6737 mv = isl_multi_val_align_params(mv,
6738 isl_pw_multi_aff_get_space(pma));
6739 if (!pma || !mv)
6740 goto error;
6743 for (i = 0; i < pma->n; ++i) {
6744 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6745 isl_multi_val_copy(mv));
6746 if (!pma->p[i].maff)
6747 goto error;
6750 isl_multi_val_free(mv);
6751 return pma;
6752 error:
6753 isl_multi_val_free(mv);
6754 isl_pw_multi_aff_free(pma);
6755 return NULL;
6758 /* This function is called for each entry of an isl_union_pw_multi_aff.
6759 * If the space of the entry matches that of data->mv,
6760 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6761 * Otherwise, return an empty isl_pw_multi_aff.
6763 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6764 __isl_take isl_pw_multi_aff *pma, void *user)
6766 isl_multi_val *mv = user;
6768 if (!pma)
6769 return NULL;
6770 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6771 mv->space, isl_dim_set)) {
6772 isl_space *space = isl_pw_multi_aff_get_space(pma);
6773 isl_pw_multi_aff_free(pma);
6774 return isl_pw_multi_aff_empty(space);
6777 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6780 /* Scale the elements of "upma" by the corresponding elements of "mv",
6781 * for those entries that match the space of "mv".
6783 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6784 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6786 struct isl_union_pw_multi_aff_transform_control control = {
6787 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6788 .fn_user = mv,
6791 upma = isl_union_pw_multi_aff_align_params(upma,
6792 isl_multi_val_get_space(mv));
6793 mv = isl_multi_val_align_params(mv,
6794 isl_union_pw_multi_aff_get_space(upma));
6795 if (!upma || !mv)
6796 goto error;
6798 return isl_union_pw_multi_aff_transform(upma, &control);
6800 isl_multi_val_free(mv);
6801 return upma;
6802 error:
6803 isl_multi_val_free(mv);
6804 isl_union_pw_multi_aff_free(upma);
6805 return NULL;
6808 /* Construct and return a piecewise multi affine expression
6809 * in the given space with value zero in each of the output dimensions and
6810 * a universe domain.
6812 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6814 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6817 /* Construct and return a piecewise multi affine expression
6818 * that is equal to the given piecewise affine expression.
6820 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6821 __isl_take isl_pw_aff *pa)
6823 int i;
6824 isl_space *space;
6825 isl_pw_multi_aff *pma;
6827 if (!pa)
6828 return NULL;
6830 space = isl_pw_aff_get_space(pa);
6831 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6833 for (i = 0; i < pa->n; ++i) {
6834 isl_set *set;
6835 isl_multi_aff *ma;
6837 set = isl_set_copy(pa->p[i].set);
6838 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6839 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6842 isl_pw_aff_free(pa);
6843 return pma;
6846 /* Construct and return a piecewise multi affine expression
6847 * that is equal to the given multi piecewise affine expression
6848 * on the shared domain of the piecewise affine expressions,
6849 * in the special case of a 0D multi piecewise affine expression.
6851 * Create a piecewise multi affine expression with the explicit domain of
6852 * the 0D multi piecewise affine expression as domain.
6854 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6855 __isl_take isl_multi_pw_aff *mpa)
6857 isl_space *space;
6858 isl_set *dom;
6859 isl_multi_aff *ma;
6861 space = isl_multi_pw_aff_get_space(mpa);
6862 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6863 isl_multi_pw_aff_free(mpa);
6865 ma = isl_multi_aff_zero(space);
6866 return isl_pw_multi_aff_alloc(dom, ma);
6869 /* Construct and return a piecewise multi affine expression
6870 * that is equal to the given multi piecewise affine expression
6871 * on the shared domain of the piecewise affine expressions.
6873 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6874 __isl_take isl_multi_pw_aff *mpa)
6876 int i;
6877 isl_space *space;
6878 isl_pw_aff *pa;
6879 isl_pw_multi_aff *pma;
6881 if (!mpa)
6882 return NULL;
6884 if (mpa->n == 0)
6885 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6887 space = isl_multi_pw_aff_get_space(mpa);
6888 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6889 pma = isl_pw_multi_aff_from_pw_aff(pa);
6891 for (i = 1; i < mpa->n; ++i) {
6892 isl_pw_multi_aff *pma_i;
6894 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6895 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6896 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6899 pma = isl_pw_multi_aff_reset_space(pma, space);
6901 isl_multi_pw_aff_free(mpa);
6902 return pma;
6905 /* Convenience function that constructs an isl_multi_pw_aff
6906 * directly from an isl_aff.
6908 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6910 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6913 /* Construct and return a multi piecewise affine expression
6914 * that is equal to the given multi affine expression.
6916 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6917 __isl_take isl_multi_aff *ma)
6919 int i;
6920 isl_size n;
6921 isl_multi_pw_aff *mpa;
6923 n = isl_multi_aff_dim(ma, isl_dim_out);
6924 if (n < 0)
6925 ma = isl_multi_aff_free(ma);
6926 if (!ma)
6927 return NULL;
6929 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6931 for (i = 0; i < n; ++i) {
6932 isl_pw_aff *pa;
6934 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6935 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6938 isl_multi_aff_free(ma);
6939 return mpa;
6942 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6943 * but is considered as a function on an isl_multi_aff when exported.
6945 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
6946 __isl_take isl_multi_aff *ma)
6948 return isl_multi_pw_aff_from_multi_aff(ma);
6951 /* Construct and return a multi piecewise affine expression
6952 * that is equal to the given piecewise multi affine expression.
6954 * If the resulting multi piecewise affine expression has
6955 * an explicit domain, then assign it the domain of the input.
6956 * In other cases, the domain is stored in the individual elements.
6958 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6959 __isl_take isl_pw_multi_aff *pma)
6961 int i;
6962 isl_size n;
6963 isl_space *space;
6964 isl_multi_pw_aff *mpa;
6966 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6967 if (n < 0)
6968 pma = isl_pw_multi_aff_free(pma);
6969 space = isl_pw_multi_aff_get_space(pma);
6970 mpa = isl_multi_pw_aff_alloc(space);
6972 for (i = 0; i < n; ++i) {
6973 isl_pw_aff *pa;
6975 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6976 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6978 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6979 isl_set *dom;
6981 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6982 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6985 isl_pw_multi_aff_free(pma);
6986 return mpa;
6989 /* This function performs the same operation as
6990 * isl_multi_pw_aff_from_pw_multi_aff,
6991 * but is considered as a function on an isl_pw_multi_aff when exported.
6993 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
6994 __isl_take isl_pw_multi_aff *pma)
6996 return isl_multi_pw_aff_from_pw_multi_aff(pma);
6999 /* Do "pa1" and "pa2" represent the same function?
7001 * We first check if they are obviously equal.
7002 * If not, we convert them to maps and check if those are equal.
7004 * If "pa1" or "pa2" contain any NaNs, then they are considered
7005 * not to be the same. A NaN is not equal to anything, not even
7006 * to another NaN.
7008 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
7009 __isl_keep isl_pw_aff *pa2)
7011 isl_bool equal;
7012 isl_bool has_nan;
7013 isl_map *map1, *map2;
7015 if (!pa1 || !pa2)
7016 return isl_bool_error;
7018 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
7019 if (equal < 0 || equal)
7020 return equal;
7021 has_nan = either_involves_nan(pa1, pa2);
7022 if (has_nan < 0)
7023 return isl_bool_error;
7024 if (has_nan)
7025 return isl_bool_false;
7027 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
7028 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
7029 equal = isl_map_is_equal(map1, map2);
7030 isl_map_free(map1);
7031 isl_map_free(map2);
7033 return equal;
7036 /* Do "mpa1" and "mpa2" represent the same function?
7038 * Note that we cannot convert the entire isl_multi_pw_aff
7039 * to a map because the domains of the piecewise affine expressions
7040 * may not be the same.
7042 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7043 __isl_keep isl_multi_pw_aff *mpa2)
7045 int i;
7046 isl_bool equal, equal_params;
7048 if (!mpa1 || !mpa2)
7049 return isl_bool_error;
7051 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7052 if (equal_params < 0)
7053 return isl_bool_error;
7054 if (!equal_params) {
7055 if (!isl_space_has_named_params(mpa1->space))
7056 return isl_bool_false;
7057 if (!isl_space_has_named_params(mpa2->space))
7058 return isl_bool_false;
7059 mpa1 = isl_multi_pw_aff_copy(mpa1);
7060 mpa2 = isl_multi_pw_aff_copy(mpa2);
7061 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7062 isl_multi_pw_aff_get_space(mpa2));
7063 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7064 isl_multi_pw_aff_get_space(mpa1));
7065 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7066 isl_multi_pw_aff_free(mpa1);
7067 isl_multi_pw_aff_free(mpa2);
7068 return equal;
7071 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7072 if (equal < 0 || !equal)
7073 return equal;
7075 for (i = 0; i < mpa1->n; ++i) {
7076 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7077 if (equal < 0 || !equal)
7078 return equal;
7081 return isl_bool_true;
7084 /* Do "pma1" and "pma2" represent the same function?
7086 * First check if they are obviously equal.
7087 * If not, then convert them to maps and check if those are equal.
7089 * If "pa1" or "pa2" contain any NaNs, then they are considered
7090 * not to be the same. A NaN is not equal to anything, not even
7091 * to another NaN.
7093 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7094 __isl_keep isl_pw_multi_aff *pma2)
7096 isl_bool equal;
7097 isl_bool has_nan;
7098 isl_map *map1, *map2;
7100 if (!pma1 || !pma2)
7101 return isl_bool_error;
7103 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7104 if (equal < 0 || equal)
7105 return equal;
7106 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7107 if (has_nan >= 0 && !has_nan)
7108 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7109 if (has_nan < 0 || has_nan)
7110 return isl_bool_not(has_nan);
7112 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7113 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7114 equal = isl_map_is_equal(map1, map2);
7115 isl_map_free(map1);
7116 isl_map_free(map2);
7118 return equal;
7121 /* Compute the pullback of "mpa" by the function represented by "ma".
7122 * In other words, plug in "ma" in "mpa".
7124 * The parameters of "mpa" and "ma" are assumed to have been aligned.
7126 * If "mpa" has an explicit domain, then it is this domain
7127 * that needs to undergo a pullback, i.e., a preimage.
7129 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
7130 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7132 int i;
7133 isl_space *space = NULL;
7135 mpa = isl_multi_pw_aff_cow(mpa);
7136 if (!mpa || !ma)
7137 goto error;
7139 space = isl_space_join(isl_multi_aff_get_space(ma),
7140 isl_multi_pw_aff_get_space(mpa));
7141 if (!space)
7142 goto error;
7144 for (i = 0; i < mpa->n; ++i) {
7145 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
7146 isl_multi_aff_copy(ma));
7147 if (!mpa->u.p[i])
7148 goto error;
7150 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7151 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
7152 isl_multi_aff_copy(ma));
7153 if (!mpa->u.dom)
7154 goto error;
7157 isl_multi_aff_free(ma);
7158 isl_space_free(mpa->space);
7159 mpa->space = space;
7160 return mpa;
7161 error:
7162 isl_space_free(space);
7163 isl_multi_pw_aff_free(mpa);
7164 isl_multi_aff_free(ma);
7165 return NULL;
7168 /* Compute the pullback of "mpa" by the function represented by "ma".
7169 * In other words, plug in "ma" in "mpa".
7171 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
7172 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7174 isl_bool equal_params;
7176 if (!mpa || !ma)
7177 goto error;
7178 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
7179 if (equal_params < 0)
7180 goto error;
7181 if (equal_params)
7182 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7183 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
7184 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
7185 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7186 error:
7187 isl_multi_pw_aff_free(mpa);
7188 isl_multi_aff_free(ma);
7189 return NULL;
7192 /* Compute the pullback of "mpa" by the function represented by "pma".
7193 * In other words, plug in "pma" in "mpa".
7195 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7197 * If "mpa" has an explicit domain, then it is this domain
7198 * that needs to undergo a pullback, i.e., a preimage.
7200 static __isl_give isl_multi_pw_aff *
7201 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7202 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7204 int i;
7205 isl_space *space = NULL;
7207 mpa = isl_multi_pw_aff_cow(mpa);
7208 if (!mpa || !pma)
7209 goto error;
7211 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
7212 isl_multi_pw_aff_get_space(mpa));
7214 for (i = 0; i < mpa->n; ++i) {
7215 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7216 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
7217 if (!mpa->u.p[i])
7218 goto error;
7220 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7221 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
7222 isl_pw_multi_aff_copy(pma));
7223 if (!mpa->u.dom)
7224 goto error;
7227 isl_pw_multi_aff_free(pma);
7228 isl_space_free(mpa->space);
7229 mpa->space = space;
7230 return mpa;
7231 error:
7232 isl_space_free(space);
7233 isl_multi_pw_aff_free(mpa);
7234 isl_pw_multi_aff_free(pma);
7235 return NULL;
7238 /* Compute the pullback of "mpa" by the function represented by "pma".
7239 * In other words, plug in "pma" in "mpa".
7241 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
7242 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7244 isl_bool equal_params;
7246 if (!mpa || !pma)
7247 goto error;
7248 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
7249 if (equal_params < 0)
7250 goto error;
7251 if (equal_params)
7252 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7253 mpa = isl_multi_pw_aff_align_params(mpa,
7254 isl_pw_multi_aff_get_space(pma));
7255 pma = isl_pw_multi_aff_align_params(pma,
7256 isl_multi_pw_aff_get_space(mpa));
7257 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7258 error:
7259 isl_multi_pw_aff_free(mpa);
7260 isl_pw_multi_aff_free(pma);
7261 return NULL;
7264 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7265 * with the domain of "aff". The domain of the result is the same
7266 * as that of "mpa".
7267 * "mpa" and "aff" are assumed to have been aligned.
7269 * We first extract the parametric constant from "aff", defined
7270 * over the correct domain.
7271 * Then we add the appropriate combinations of the members of "mpa".
7272 * Finally, we add the integer divisions through recursive calls.
7274 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7275 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7277 int i;
7278 isl_size n_in, n_div, n_mpa_in;
7279 isl_space *space;
7280 isl_val *v;
7281 isl_pw_aff *pa;
7282 isl_aff *tmp;
7284 n_in = isl_aff_dim(aff, isl_dim_in);
7285 n_div = isl_aff_dim(aff, isl_dim_div);
7286 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7287 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7288 goto error;
7290 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7291 tmp = isl_aff_copy(aff);
7292 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7293 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7294 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7295 tmp = isl_aff_reset_domain_space(tmp, space);
7296 pa = isl_pw_aff_from_aff(tmp);
7298 for (i = 0; i < n_in; ++i) {
7299 isl_pw_aff *pa_i;
7301 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7302 continue;
7303 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7304 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7305 pa_i = isl_pw_aff_scale_val(pa_i, v);
7306 pa = isl_pw_aff_add(pa, pa_i);
7309 for (i = 0; i < n_div; ++i) {
7310 isl_aff *div;
7311 isl_pw_aff *pa_i;
7313 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7314 continue;
7315 div = isl_aff_get_div(aff, i);
7316 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7317 isl_multi_pw_aff_copy(mpa), div);
7318 pa_i = isl_pw_aff_floor(pa_i);
7319 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7320 pa_i = isl_pw_aff_scale_val(pa_i, v);
7321 pa = isl_pw_aff_add(pa, pa_i);
7324 isl_multi_pw_aff_free(mpa);
7325 isl_aff_free(aff);
7327 return pa;
7328 error:
7329 isl_multi_pw_aff_free(mpa);
7330 isl_aff_free(aff);
7331 return NULL;
7334 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7335 * with the domain of "aff". The domain of the result is the same
7336 * as that of "mpa".
7338 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7339 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7341 isl_bool equal_params;
7343 if (!aff || !mpa)
7344 goto error;
7345 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7346 if (equal_params < 0)
7347 goto error;
7348 if (equal_params)
7349 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7351 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7352 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7354 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7355 error:
7356 isl_aff_free(aff);
7357 isl_multi_pw_aff_free(mpa);
7358 return NULL;
7361 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7362 * with the domain of "pa". The domain of the result is the same
7363 * as that of "mpa".
7364 * "mpa" and "pa" are assumed to have been aligned.
7366 * We consider each piece in turn. Note that the domains of the
7367 * pieces are assumed to be disjoint and they remain disjoint
7368 * after taking the preimage (over the same function).
7370 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7371 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7373 isl_space *space;
7374 isl_pw_aff *res;
7375 int i;
7377 if (!mpa || !pa)
7378 goto error;
7380 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7381 isl_pw_aff_get_space(pa));
7382 res = isl_pw_aff_empty(space);
7384 for (i = 0; i < pa->n; ++i) {
7385 isl_pw_aff *pa_i;
7386 isl_set *domain;
7388 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7389 isl_multi_pw_aff_copy(mpa),
7390 isl_aff_copy(pa->p[i].aff));
7391 domain = isl_set_copy(pa->p[i].set);
7392 domain = isl_set_preimage_multi_pw_aff(domain,
7393 isl_multi_pw_aff_copy(mpa));
7394 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7395 res = isl_pw_aff_add_disjoint(res, pa_i);
7398 isl_pw_aff_free(pa);
7399 isl_multi_pw_aff_free(mpa);
7400 return res;
7401 error:
7402 isl_pw_aff_free(pa);
7403 isl_multi_pw_aff_free(mpa);
7404 return NULL;
7407 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7408 * with the domain of "pa". The domain of the result is the same
7409 * as that of "mpa".
7411 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7412 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7414 isl_bool equal_params;
7416 if (!pa || !mpa)
7417 goto error;
7418 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7419 if (equal_params < 0)
7420 goto error;
7421 if (equal_params)
7422 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7424 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7425 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7427 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7428 error:
7429 isl_pw_aff_free(pa);
7430 isl_multi_pw_aff_free(mpa);
7431 return NULL;
7434 /* Compute the pullback of "pa" by the function represented by "mpa".
7435 * In other words, plug in "mpa" in "pa".
7436 * "pa" and "mpa" are assumed to have been aligned.
7438 * The pullback is computed by applying "pa" to "mpa".
7440 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7441 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7443 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7446 /* Compute the pullback of "pa" by the function represented by "mpa".
7447 * In other words, plug in "mpa" in "pa".
7449 * The pullback is computed by applying "pa" to "mpa".
7451 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7452 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7454 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7457 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7458 * In other words, plug in "mpa2" in "mpa1".
7460 * We pullback each member of "mpa1" in turn.
7462 * If "mpa1" has an explicit domain, then it is this domain
7463 * that needs to undergo a pullback instead, i.e., a preimage.
7465 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7466 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7468 int i;
7469 isl_space *space = NULL;
7471 isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
7472 mpa1 = isl_multi_pw_aff_cow(mpa1);
7473 if (!mpa1 || !mpa2)
7474 goto error;
7476 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7477 isl_multi_pw_aff_get_space(mpa1));
7479 for (i = 0; i < mpa1->n; ++i) {
7480 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7481 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7482 if (!mpa1->u.p[i])
7483 goto error;
7486 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7487 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7488 isl_multi_pw_aff_copy(mpa2));
7489 if (!mpa1->u.dom)
7490 goto error;
7492 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7494 isl_multi_pw_aff_free(mpa2);
7495 return mpa1;
7496 error:
7497 isl_space_free(space);
7498 isl_multi_pw_aff_free(mpa1);
7499 isl_multi_pw_aff_free(mpa2);
7500 return NULL;
7503 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7504 * of "mpa1" and "mpa2" live in the same space, construct map space
7505 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7506 * with this map space as extract argument.
7508 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7509 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7510 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7511 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7513 int match;
7514 isl_space *space1, *space2;
7515 isl_map *res;
7517 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7518 isl_multi_pw_aff_get_space(mpa2));
7519 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7520 isl_multi_pw_aff_get_space(mpa1));
7521 if (!mpa1 || !mpa2)
7522 goto error;
7523 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7524 mpa2->space, isl_dim_out);
7525 if (match < 0)
7526 goto error;
7527 if (!match)
7528 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7529 "range spaces don't match", goto error);
7530 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7531 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7532 space1 = isl_space_map_from_domain_and_range(space1, space2);
7534 res = order(mpa1, mpa2, space1);
7535 isl_multi_pw_aff_free(mpa1);
7536 isl_multi_pw_aff_free(mpa2);
7537 return res;
7538 error:
7539 isl_multi_pw_aff_free(mpa1);
7540 isl_multi_pw_aff_free(mpa2);
7541 return NULL;
7544 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7545 * where the function values are equal. "space" is the space of the result.
7546 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7548 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7549 * in the sequences are equal.
7551 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7552 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7553 __isl_take isl_space *space)
7555 int i;
7556 isl_size n;
7557 isl_map *res;
7559 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7560 if (n < 0)
7561 space = isl_space_free(space);
7562 res = isl_map_universe(space);
7564 for (i = 0; i < n; ++i) {
7565 isl_pw_aff *pa1, *pa2;
7566 isl_map *map;
7568 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7569 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7570 map = isl_pw_aff_eq_map(pa1, pa2);
7571 res = isl_map_intersect(res, map);
7574 return res;
7577 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7578 * where the function values are equal.
7580 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7581 __isl_take isl_multi_pw_aff *mpa2)
7583 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7584 &isl_multi_pw_aff_eq_map_on_space);
7587 /* Intersect "map" with the result of applying "order"
7588 * on two copies of "mpa".
7590 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7591 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7592 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7593 __isl_take isl_multi_pw_aff *mpa2))
7595 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7598 /* Return the subset of "map" where the domain and the range
7599 * have equal "mpa" values.
7601 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7602 __isl_take isl_multi_pw_aff *mpa)
7604 return isl_map_order_at_multi_pw_aff(map, mpa,
7605 &isl_multi_pw_aff_eq_map);
7608 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7609 * where the function values of "mpa1" lexicographically satisfies
7610 * "strict_base"/"base" compared to that of "mpa2".
7611 * "space" is the space of the result.
7612 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7614 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7615 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7616 * when compared to the i-th element of "mpa2" while all previous elements are
7617 * pairwise equal.
7618 * In particular, if i corresponds to the final elements
7619 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7620 * for other values of i.
7621 * If "base" is a strict order, then "base" and "strict_base" are the same.
7623 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7624 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7625 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7626 __isl_take isl_pw_aff *pa2),
7627 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7628 __isl_take isl_pw_aff *pa2),
7629 __isl_take isl_space *space)
7631 int i;
7632 isl_size n;
7633 isl_map *res, *rest;
7635 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7636 if (n < 0)
7637 space = isl_space_free(space);
7638 res = isl_map_empty(isl_space_copy(space));
7639 rest = isl_map_universe(space);
7641 for (i = 0; i < n; ++i) {
7642 int last;
7643 isl_pw_aff *pa1, *pa2;
7644 isl_map *map;
7646 last = i == n - 1;
7648 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7649 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7650 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7651 map = isl_map_intersect(map, isl_map_copy(rest));
7652 res = isl_map_union(res, map);
7654 if (last)
7655 continue;
7657 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7658 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7659 map = isl_pw_aff_eq_map(pa1, pa2);
7660 rest = isl_map_intersect(rest, map);
7663 isl_map_free(rest);
7664 return res;
7667 #undef ORDER
7668 #define ORDER le
7669 #undef STRICT_ORDER
7670 #define STRICT_ORDER lt
7671 #include "isl_aff_lex_templ.c"
7673 #undef ORDER
7674 #define ORDER lt
7675 #undef STRICT_ORDER
7676 #define STRICT_ORDER lt
7677 #include "isl_aff_lex_templ.c"
7679 #undef ORDER
7680 #define ORDER ge
7681 #undef STRICT_ORDER
7682 #define STRICT_ORDER gt
7683 #include "isl_aff_lex_templ.c"
7685 #undef ORDER
7686 #define ORDER gt
7687 #undef STRICT_ORDER
7688 #define STRICT_ORDER gt
7689 #include "isl_aff_lex_templ.c"
7691 /* Compare two isl_affs.
7693 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7694 * than "aff2" and 0 if they are equal.
7696 * The order is fairly arbitrary. We do consider expressions that only involve
7697 * earlier dimensions as "smaller".
7699 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7701 int cmp;
7702 int last1, last2;
7704 if (aff1 == aff2)
7705 return 0;
7707 if (!aff1)
7708 return -1;
7709 if (!aff2)
7710 return 1;
7712 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7713 if (cmp != 0)
7714 return cmp;
7716 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7717 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7718 if (last1 != last2)
7719 return last1 - last2;
7721 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7724 /* Compare two isl_pw_affs.
7726 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7727 * than "pa2" and 0 if they are equal.
7729 * The order is fairly arbitrary. We do consider expressions that only involve
7730 * earlier dimensions as "smaller".
7732 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7733 __isl_keep isl_pw_aff *pa2)
7735 int i;
7736 int cmp;
7738 if (pa1 == pa2)
7739 return 0;
7741 if (!pa1)
7742 return -1;
7743 if (!pa2)
7744 return 1;
7746 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7747 if (cmp != 0)
7748 return cmp;
7750 if (pa1->n != pa2->n)
7751 return pa1->n - pa2->n;
7753 for (i = 0; i < pa1->n; ++i) {
7754 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7755 if (cmp != 0)
7756 return cmp;
7757 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7758 if (cmp != 0)
7759 return cmp;
7762 return 0;
7765 /* Return a piecewise affine expression that is equal to "v" on "domain".
7767 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7768 __isl_take isl_val *v)
7770 isl_space *space;
7771 isl_local_space *ls;
7772 isl_aff *aff;
7774 space = isl_set_get_space(domain);
7775 ls = isl_local_space_from_space(space);
7776 aff = isl_aff_val_on_domain(ls, v);
7778 return isl_pw_aff_alloc(domain, aff);
7781 /* Return a piecewise affine expression that is equal to the parameter
7782 * with identifier "id" on "domain".
7784 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7785 __isl_take isl_set *domain, __isl_take isl_id *id)
7787 isl_space *space;
7788 isl_aff *aff;
7790 space = isl_set_get_space(domain);
7791 space = isl_space_add_param_id(space, isl_id_copy(id));
7792 domain = isl_set_align_params(domain, isl_space_copy(space));
7793 aff = isl_aff_param_on_domain_space_id(space, id);
7795 return isl_pw_aff_alloc(domain, aff);
7798 /* Return a multi affine expression that is equal to "mv" on domain
7799 * space "space".
7801 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7802 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7804 int i;
7805 isl_size n;
7806 isl_space *space2;
7807 isl_local_space *ls;
7808 isl_multi_aff *ma;
7810 n = isl_multi_val_dim(mv, isl_dim_set);
7811 if (!space || n < 0)
7812 goto error;
7814 space2 = isl_multi_val_get_space(mv);
7815 space2 = isl_space_align_params(space2, isl_space_copy(space));
7816 space = isl_space_align_params(space, isl_space_copy(space2));
7817 space = isl_space_map_from_domain_and_range(space, space2);
7818 ma = isl_multi_aff_alloc(isl_space_copy(space));
7819 ls = isl_local_space_from_space(isl_space_domain(space));
7820 for (i = 0; i < n; ++i) {
7821 isl_val *v;
7822 isl_aff *aff;
7824 v = isl_multi_val_get_val(mv, i);
7825 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7826 ma = isl_multi_aff_set_aff(ma, i, aff);
7828 isl_local_space_free(ls);
7830 isl_multi_val_free(mv);
7831 return ma;
7832 error:
7833 isl_space_free(space);
7834 isl_multi_val_free(mv);
7835 return NULL;
7838 /* This is an alternative name for the function above.
7840 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7841 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7843 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7846 /* This function performs the same operation as
7847 * isl_multi_aff_multi_val_on_domain_space,
7848 * but is considered as a function on an isl_space when exported.
7850 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7851 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7853 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7856 /* Return a piecewise multi-affine expression
7857 * that is equal to "mv" on "domain".
7859 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7860 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7862 isl_space *space;
7863 isl_multi_aff *ma;
7865 space = isl_set_get_space(domain);
7866 ma = isl_multi_aff_multi_val_on_space(space, mv);
7868 return isl_pw_multi_aff_alloc(domain, ma);
7871 /* This function performs the same operation as
7872 * isl_pw_multi_aff_multi_val_on_domain,
7873 * but is considered as a function on an isl_set when exported.
7875 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7876 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7878 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7881 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7882 * mv is the value that should be attained on each domain set
7883 * res collects the results
7885 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7886 isl_multi_val *mv;
7887 isl_union_pw_multi_aff *res;
7890 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7891 * and add it to data->res.
7893 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7894 void *user)
7896 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7897 isl_pw_multi_aff *pma;
7898 isl_multi_val *mv;
7900 mv = isl_multi_val_copy(data->mv);
7901 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7902 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7904 return data->res ? isl_stat_ok : isl_stat_error;
7907 /* Return a union piecewise multi-affine expression
7908 * that is equal to "mv" on "domain".
7910 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7911 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7913 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7914 isl_space *space;
7916 space = isl_union_set_get_space(domain);
7917 data.res = isl_union_pw_multi_aff_empty(space);
7918 data.mv = mv;
7919 if (isl_union_set_foreach_set(domain,
7920 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7921 data.res = isl_union_pw_multi_aff_free(data.res);
7922 isl_union_set_free(domain);
7923 isl_multi_val_free(mv);
7924 return data.res;
7927 /* Compute the pullback of data->pma by the function represented by "pma2",
7928 * provided the spaces match, and add the results to data->res.
7930 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7932 struct isl_union_pw_multi_aff_bin_data *data = user;
7934 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7935 pma2->dim, isl_dim_out)) {
7936 isl_pw_multi_aff_free(pma2);
7937 return isl_stat_ok;
7940 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7941 isl_pw_multi_aff_copy(data->pma), pma2);
7943 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7944 if (!data->res)
7945 return isl_stat_error;
7947 return isl_stat_ok;
7950 /* Compute the pullback of "upma1" by the function represented by "upma2".
7952 __isl_give isl_union_pw_multi_aff *
7953 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7954 __isl_take isl_union_pw_multi_aff *upma1,
7955 __isl_take isl_union_pw_multi_aff *upma2)
7957 return bin_op(upma1, upma2, &pullback_entry);
7960 /* Apply "upma2" to "upma1".
7962 * That is, compute the pullback of "upma2" by "upma1".
7964 __isl_give isl_union_pw_multi_aff *
7965 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7966 __isl_take isl_union_pw_multi_aff *upma1,
7967 __isl_take isl_union_pw_multi_aff *upma2)
7969 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7972 #undef TYPE
7973 #define TYPE isl_pw_multi_aff
7974 static
7975 #include "isl_copy_tuple_id_templ.c"
7977 /* Given a function "pma1" of the form A[B -> C] -> D and
7978 * a function "pma2" of the form E -> B,
7979 * replace the domain of the wrapped relation inside the domain of "pma1"
7980 * by the preimage with respect to "pma2".
7981 * In other words, plug in "pma2" in this nested domain.
7982 * The result is of the form A[E -> C] -> D.
7984 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7985 * plug that into "pma1".
7987 __isl_give isl_pw_multi_aff *
7988 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7989 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
7991 isl_space *pma1_space, *pma2_space;
7992 isl_space *space;
7993 isl_pw_multi_aff *id;
7995 pma1_space = isl_pw_multi_aff_peek_space(pma1);
7996 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7998 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
7999 goto error;
8000 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
8001 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
8002 goto error;
8004 space = isl_space_domain(isl_space_copy(pma1_space));
8005 space = isl_space_range(isl_space_unwrap(space));
8006 id = isl_pw_multi_aff_identity_on_domain_space(space);
8007 pma2 = isl_pw_multi_aff_product(pma2, id);
8009 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
8010 pma1_space, isl_dim_in);
8011 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
8012 pma1_space, isl_dim_in);
8014 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
8015 error:
8016 isl_pw_multi_aff_free(pma1);
8017 isl_pw_multi_aff_free(pma2);
8018 return NULL;
8021 /* If data->pma and "pma2" are such that
8022 * data->pma is of the form A[B -> C] -> D and
8023 * "pma2" is of the form E -> B,
8024 * then replace the domain of the wrapped relation
8025 * inside the domain of data->pma by the preimage with respect to "pma2" and
8026 * add the result to data->res.
8028 static isl_stat preimage_domain_wrapped_domain_entry(
8029 __isl_take isl_pw_multi_aff *pma2, void *user)
8031 struct isl_union_pw_multi_aff_bin_data *data = user;
8032 isl_space *pma1_space, *pma2_space;
8033 isl_bool match;
8035 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
8036 pma2_space = isl_pw_multi_aff_peek_space(pma2);
8038 match = isl_space_domain_is_wrapping(pma1_space);
8039 if (match >= 0 && match)
8040 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
8041 isl_dim_in, pma2_space, isl_dim_out);
8042 if (match < 0 || !match) {
8043 isl_pw_multi_aff_free(pma2);
8044 return match < 0 ? isl_stat_error : isl_stat_ok;
8047 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8048 isl_pw_multi_aff_copy(data->pma), pma2);
8050 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
8052 return isl_stat_non_null(data->res);
8055 /* For each pair of functions A[B -> C] -> D in "upma1" and
8056 * E -> B in "upma2",
8057 * replace the domain of the wrapped relation inside the domain of the first
8058 * by the preimage with respect to the second and collect the results.
8059 * In other words, plug in the second function in this nested domain.
8060 * The results are of the form A[E -> C] -> D.
8062 __isl_give isl_union_pw_multi_aff *
8063 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8064 __isl_take isl_union_pw_multi_aff *upma1,
8065 __isl_take isl_union_pw_multi_aff *upma2)
8067 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
8070 /* Check that the domain space of "upa" matches "space".
8072 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8073 * can in principle never fail since the space "space" is that
8074 * of the isl_multi_union_pw_aff and is a set space such that
8075 * there is no domain space to match.
8077 * We check the parameters and double-check that "space" is
8078 * indeed that of a set.
8080 static isl_stat isl_union_pw_aff_check_match_domain_space(
8081 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8083 isl_space *upa_space;
8084 isl_bool match;
8086 if (!upa || !space)
8087 return isl_stat_error;
8089 match = isl_space_is_set(space);
8090 if (match < 0)
8091 return isl_stat_error;
8092 if (!match)
8093 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8094 "expecting set space", return isl_stat_error);
8096 upa_space = isl_union_pw_aff_get_space(upa);
8097 match = isl_space_has_equal_params(space, upa_space);
8098 if (match < 0)
8099 goto error;
8100 if (!match)
8101 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8102 "parameters don't match", goto error);
8104 isl_space_free(upa_space);
8105 return isl_stat_ok;
8106 error:
8107 isl_space_free(upa_space);
8108 return isl_stat_error;
8111 /* Do the parameters of "upa" match those of "space"?
8113 static isl_bool isl_union_pw_aff_matching_params(
8114 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8116 isl_space *upa_space;
8117 isl_bool match;
8119 if (!upa || !space)
8120 return isl_bool_error;
8122 upa_space = isl_union_pw_aff_get_space(upa);
8124 match = isl_space_has_equal_params(space, upa_space);
8126 isl_space_free(upa_space);
8127 return match;
8130 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8131 * space represents the new parameters.
8132 * res collects the results.
8134 struct isl_union_pw_aff_reset_params_data {
8135 isl_space *space;
8136 isl_union_pw_aff *res;
8139 /* Replace the parameters of "pa" by data->space and
8140 * add the result to data->res.
8142 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8144 struct isl_union_pw_aff_reset_params_data *data = user;
8145 isl_space *space;
8147 space = isl_pw_aff_get_space(pa);
8148 space = isl_space_replace_params(space, data->space);
8149 pa = isl_pw_aff_reset_space(pa, space);
8150 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8152 return data->res ? isl_stat_ok : isl_stat_error;
8155 /* Replace the domain space of "upa" by "space".
8156 * Since a union expression does not have a (single) domain space,
8157 * "space" is necessarily a parameter space.
8159 * Since the order and the names of the parameters determine
8160 * the hash value, we need to create a new hash table.
8162 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8163 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8165 struct isl_union_pw_aff_reset_params_data data = { space };
8166 isl_bool match;
8168 match = isl_union_pw_aff_matching_params(upa, space);
8169 if (match < 0)
8170 upa = isl_union_pw_aff_free(upa);
8171 else if (match) {
8172 isl_space_free(space);
8173 return upa;
8176 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8177 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8178 data.res = isl_union_pw_aff_free(data.res);
8180 isl_union_pw_aff_free(upa);
8181 isl_space_free(space);
8182 return data.res;
8185 /* Return the floor of "pa".
8187 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8189 return isl_pw_aff_floor(pa);
8192 /* Given f, return floor(f).
8194 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8195 __isl_take isl_union_pw_aff *upa)
8197 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8200 /* Compute
8202 * upa mod m = upa - m * floor(upa/m)
8204 * with m an integer value.
8206 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8207 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8209 isl_union_pw_aff *res;
8211 if (!upa || !m)
8212 goto error;
8214 if (!isl_val_is_int(m))
8215 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8216 "expecting integer modulo", goto error);
8217 if (!isl_val_is_pos(m))
8218 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8219 "expecting positive modulo", goto error);
8221 res = isl_union_pw_aff_copy(upa);
8222 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8223 upa = isl_union_pw_aff_floor(upa);
8224 upa = isl_union_pw_aff_scale_val(upa, m);
8225 res = isl_union_pw_aff_sub(res, upa);
8227 return res;
8228 error:
8229 isl_val_free(m);
8230 isl_union_pw_aff_free(upa);
8231 return NULL;
8234 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8235 * pos is the output position that needs to be extracted.
8236 * res collects the results.
8238 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8239 int pos;
8240 isl_union_pw_aff *res;
8243 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8244 * (assuming it has such a dimension) and add it to data->res.
8246 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8248 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8249 isl_size n_out;
8250 isl_pw_aff *pa;
8252 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8253 if (n_out < 0)
8254 return isl_stat_error;
8255 if (data->pos >= n_out) {
8256 isl_pw_multi_aff_free(pma);
8257 return isl_stat_ok;
8260 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8261 isl_pw_multi_aff_free(pma);
8263 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8265 return data->res ? isl_stat_ok : isl_stat_error;
8268 /* Extract an isl_union_pw_aff corresponding to
8269 * output dimension "pos" of "upma".
8271 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8272 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8274 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8275 isl_space *space;
8277 if (!upma)
8278 return NULL;
8280 if (pos < 0)
8281 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8282 "cannot extract at negative position", return NULL);
8284 space = isl_union_pw_multi_aff_get_space(upma);
8285 data.res = isl_union_pw_aff_empty(space);
8286 data.pos = pos;
8287 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8288 &get_union_pw_aff, &data) < 0)
8289 data.res = isl_union_pw_aff_free(data.res);
8291 return data.res;
8294 /* Return a union piecewise affine expression
8295 * that is equal to "aff" on "domain".
8297 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8298 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8300 isl_pw_aff *pa;
8302 pa = isl_pw_aff_from_aff(aff);
8303 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8306 /* Return a union piecewise affine expression
8307 * that is equal to the parameter identified by "id" on "domain".
8309 * Make sure the parameter appears in the space passed to
8310 * isl_aff_param_on_domain_space_id.
8312 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8313 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8315 isl_space *space;
8316 isl_aff *aff;
8318 space = isl_union_set_get_space(domain);
8319 space = isl_space_add_param_id(space, isl_id_copy(id));
8320 aff = isl_aff_param_on_domain_space_id(space, id);
8321 return isl_union_pw_aff_aff_on_domain(domain, aff);
8324 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8325 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8326 * needs to attain.
8327 * "res" collects the results.
8329 struct isl_union_pw_aff_pw_aff_on_domain_data {
8330 isl_pw_aff *pa;
8331 isl_union_pw_aff *res;
8334 /* Construct a piecewise affine expression that is equal to data->pa
8335 * on "domain" and add the result to data->res.
8337 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8339 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8340 isl_pw_aff *pa;
8341 isl_size dim;
8343 pa = isl_pw_aff_copy(data->pa);
8344 dim = isl_set_dim(domain, isl_dim_set);
8345 if (dim < 0)
8346 pa = isl_pw_aff_free(pa);
8347 pa = isl_pw_aff_from_range(pa);
8348 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8349 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8350 pa = isl_pw_aff_intersect_domain(pa, domain);
8351 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8353 return data->res ? isl_stat_ok : isl_stat_error;
8356 /* Return a union piecewise affine expression
8357 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8358 * have been aligned.
8360 * Construct an isl_pw_aff on each of the sets in "domain" and
8361 * collect the results.
8363 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8364 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8366 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8367 isl_space *space;
8369 space = isl_union_set_get_space(domain);
8370 data.res = isl_union_pw_aff_empty(space);
8371 data.pa = pa;
8372 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8373 data.res = isl_union_pw_aff_free(data.res);
8374 isl_union_set_free(domain);
8375 isl_pw_aff_free(pa);
8376 return data.res;
8379 /* Return a union piecewise affine expression
8380 * that is equal to "pa" on "domain".
8382 * Check that "pa" is a parametric expression,
8383 * align the parameters if needed and call
8384 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8386 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8387 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8389 isl_bool is_set;
8390 isl_bool equal_params;
8391 isl_space *domain_space, *pa_space;
8393 pa_space = isl_pw_aff_peek_space(pa);
8394 is_set = isl_space_is_set(pa_space);
8395 if (is_set < 0)
8396 goto error;
8397 if (!is_set)
8398 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8399 "expecting parametric expression", goto error);
8401 domain_space = isl_union_set_get_space(domain);
8402 pa_space = isl_pw_aff_get_space(pa);
8403 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8404 if (equal_params >= 0 && !equal_params) {
8405 isl_space *space;
8407 space = isl_space_align_params(domain_space, pa_space);
8408 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8409 domain = isl_union_set_align_params(domain, space);
8410 } else {
8411 isl_space_free(domain_space);
8412 isl_space_free(pa_space);
8415 if (equal_params < 0)
8416 goto error;
8417 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8418 error:
8419 isl_union_set_free(domain);
8420 isl_pw_aff_free(pa);
8421 return NULL;
8424 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8425 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8426 * "res" collects the results.
8428 struct isl_union_pw_aff_val_on_domain_data {
8429 isl_val *v;
8430 isl_union_pw_aff *res;
8433 /* Construct a piecewise affine expression that is equal to data->v
8434 * on "domain" and add the result to data->res.
8436 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8438 struct isl_union_pw_aff_val_on_domain_data *data = user;
8439 isl_pw_aff *pa;
8440 isl_val *v;
8442 v = isl_val_copy(data->v);
8443 pa = isl_pw_aff_val_on_domain(domain, v);
8444 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8446 return data->res ? isl_stat_ok : isl_stat_error;
8449 /* Return a union piecewise affine expression
8450 * that is equal to "v" on "domain".
8452 * Construct an isl_pw_aff on each of the sets in "domain" and
8453 * collect the results.
8455 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8456 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8458 struct isl_union_pw_aff_val_on_domain_data data;
8459 isl_space *space;
8461 space = isl_union_set_get_space(domain);
8462 data.res = isl_union_pw_aff_empty(space);
8463 data.v = v;
8464 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8465 data.res = isl_union_pw_aff_free(data.res);
8466 isl_union_set_free(domain);
8467 isl_val_free(v);
8468 return data.res;
8471 /* Construct a piecewise multi affine expression
8472 * that is equal to "pa" and add it to upma.
8474 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8475 void *user)
8477 isl_union_pw_multi_aff **upma = user;
8478 isl_pw_multi_aff *pma;
8480 pma = isl_pw_multi_aff_from_pw_aff(pa);
8481 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8483 return *upma ? isl_stat_ok : isl_stat_error;
8486 /* Construct and return a union piecewise multi affine expression
8487 * that is equal to the given union piecewise affine expression.
8489 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8490 __isl_take isl_union_pw_aff *upa)
8492 isl_space *space;
8493 isl_union_pw_multi_aff *upma;
8495 if (!upa)
8496 return NULL;
8498 space = isl_union_pw_aff_get_space(upa);
8499 upma = isl_union_pw_multi_aff_empty(space);
8501 if (isl_union_pw_aff_foreach_pw_aff(upa,
8502 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8503 upma = isl_union_pw_multi_aff_free(upma);
8505 isl_union_pw_aff_free(upa);
8506 return upma;
8509 /* Compute the set of elements in the domain of "pa" where it is zero and
8510 * add this set to "uset".
8512 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8514 isl_union_set **uset = (isl_union_set **)user;
8516 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8518 return *uset ? isl_stat_ok : isl_stat_error;
8521 /* Return a union set containing those elements in the domain
8522 * of "upa" where it is zero.
8524 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8525 __isl_take isl_union_pw_aff *upa)
8527 isl_union_set *zero;
8529 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8530 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8531 zero = isl_union_set_free(zero);
8533 isl_union_pw_aff_free(upa);
8534 return zero;
8537 /* Internal data structure for isl_union_pw_aff_bind_id,
8538 * storing the parameter that needs to be bound and
8539 * the accumulated results.
8541 struct isl_bind_id_data {
8542 isl_id *id;
8543 isl_union_set *bound;
8546 /* Bind the piecewise affine function "pa" to the parameter data->id,
8547 * adding the resulting elements in the domain where the expression
8548 * is equal to the parameter to data->bound.
8550 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8552 struct isl_bind_id_data *data = user;
8553 isl_set *bound;
8555 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8556 data->bound = isl_union_set_add_set(data->bound, bound);
8558 return data->bound ? isl_stat_ok : isl_stat_error;
8561 /* Bind the union piecewise affine function "upa" to the parameter "id",
8562 * returning the elements in the domain where the expression
8563 * is equal to the parameter.
8565 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8566 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8568 struct isl_bind_id_data data = { id };
8570 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8571 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8572 data.bound = isl_union_set_free(data.bound);
8574 isl_union_pw_aff_free(upa);
8575 isl_id_free(id);
8576 return data.bound;
8579 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8580 * upma is the function that is plugged in.
8581 * pa is the current part of the function in which upma is plugged in.
8582 * res collects the results.
8584 struct isl_union_pw_aff_pullback_upma_data {
8585 isl_union_pw_multi_aff *upma;
8586 isl_pw_aff *pa;
8587 isl_union_pw_aff *res;
8590 /* Check if "pma" can be plugged into data->pa.
8591 * If so, perform the pullback and add the result to data->res.
8593 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8595 struct isl_union_pw_aff_pullback_upma_data *data = user;
8596 isl_pw_aff *pa;
8598 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8599 pma->dim, isl_dim_out)) {
8600 isl_pw_multi_aff_free(pma);
8601 return isl_stat_ok;
8604 pa = isl_pw_aff_copy(data->pa);
8605 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8607 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8609 return data->res ? isl_stat_ok : isl_stat_error;
8612 /* Check if any of the elements of data->upma can be plugged into pa,
8613 * add if so add the result to data->res.
8615 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8617 struct isl_union_pw_aff_pullback_upma_data *data = user;
8618 isl_stat r;
8620 data->pa = pa;
8621 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8622 &pa_pb_pma, data);
8623 isl_pw_aff_free(pa);
8625 return r;
8628 /* Compute the pullback of "upa" by the function represented by "upma".
8629 * In other words, plug in "upma" in "upa". The result contains
8630 * expressions defined over the domain space of "upma".
8632 * Run over all pairs of elements in "upa" and "upma", perform
8633 * the pullback when appropriate and collect the results.
8634 * If the hash value were based on the domain space rather than
8635 * the function space, then we could run through all elements
8636 * of "upma" and directly pick out the corresponding element of "upa".
8638 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8639 __isl_take isl_union_pw_aff *upa,
8640 __isl_take isl_union_pw_multi_aff *upma)
8642 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8643 isl_space *space;
8645 space = isl_union_pw_multi_aff_get_space(upma);
8646 upa = isl_union_pw_aff_align_params(upa, space);
8647 space = isl_union_pw_aff_get_space(upa);
8648 upma = isl_union_pw_multi_aff_align_params(upma, space);
8650 if (!upa || !upma)
8651 goto error;
8653 data.upma = upma;
8654 data.res = isl_union_pw_aff_alloc_same_size(upa);
8655 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8656 data.res = isl_union_pw_aff_free(data.res);
8658 isl_union_pw_aff_free(upa);
8659 isl_union_pw_multi_aff_free(upma);
8660 return data.res;
8661 error:
8662 isl_union_pw_aff_free(upa);
8663 isl_union_pw_multi_aff_free(upma);
8664 return NULL;
8667 #undef BASE
8668 #define BASE union_pw_aff
8669 #undef DOMBASE
8670 #define DOMBASE union_set
8672 #include <isl_multi_explicit_domain.c>
8673 #include <isl_multi_union_pw_aff_explicit_domain.c>
8674 #include <isl_multi_templ.c>
8675 #include <isl_multi_apply_set.c>
8676 #include <isl_multi_apply_union_set.c>
8677 #include <isl_multi_arith_templ.c>
8678 #include <isl_multi_bind_templ.c>
8679 #include <isl_multi_coalesce.c>
8680 #include <isl_multi_dim_id_templ.c>
8681 #include <isl_multi_floor.c>
8682 #include <isl_multi_from_base_templ.c>
8683 #include <isl_multi_gist.c>
8684 #include <isl_multi_align_set.c>
8685 #include <isl_multi_align_union_set.c>
8686 #include <isl_multi_intersect.c>
8687 #include <isl_multi_nan_templ.c>
8688 #include <isl_multi_tuple_id_templ.c>
8689 #include <isl_multi_union_add_templ.c>
8690 #include <isl_multi_zero_space_templ.c>
8692 /* Does "mupa" have a non-trivial explicit domain?
8694 * The explicit domain, if present, is trivial if it represents
8695 * an (obviously) universe parameter set.
8697 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8698 __isl_keep isl_multi_union_pw_aff *mupa)
8700 isl_bool is_params, trivial;
8701 isl_set *set;
8703 if (!mupa)
8704 return isl_bool_error;
8705 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8706 return isl_bool_false;
8707 is_params = isl_union_set_is_params(mupa->u.dom);
8708 if (is_params < 0 || !is_params)
8709 return isl_bool_not(is_params);
8710 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8711 trivial = isl_set_plain_is_universe(set);
8712 isl_set_free(set);
8713 return isl_bool_not(trivial);
8716 /* Construct a multiple union piecewise affine expression
8717 * in the given space with value zero in each of the output dimensions.
8719 * Since there is no canonical zero value for
8720 * a union piecewise affine expression, we can only construct
8721 * a zero-dimensional "zero" value.
8723 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8724 __isl_take isl_space *space)
8726 isl_bool params;
8727 isl_size dim;
8729 if (!space)
8730 return NULL;
8732 params = isl_space_is_params(space);
8733 if (params < 0)
8734 goto error;
8735 if (params)
8736 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8737 "expecting proper set space", goto error);
8738 if (!isl_space_is_set(space))
8739 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8740 "expecting set space", goto error);
8741 dim = isl_space_dim(space, isl_dim_out);
8742 if (dim < 0)
8743 goto error;
8744 if (dim != 0)
8745 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8746 "expecting 0D space", goto error);
8748 return isl_multi_union_pw_aff_alloc(space);
8749 error:
8750 isl_space_free(space);
8751 return NULL;
8754 /* Construct and return a multi union piecewise affine expression
8755 * that is equal to the given multi affine expression.
8757 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8758 __isl_take isl_multi_aff *ma)
8760 isl_multi_pw_aff *mpa;
8762 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8763 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8766 /* This function performs the same operation as
8767 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8768 * isl_multi_aff when exported.
8770 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8771 __isl_take isl_multi_aff *ma)
8773 return isl_multi_union_pw_aff_from_multi_aff(ma);
8776 /* Construct and return a multi union piecewise affine expression
8777 * that is equal to the given multi piecewise affine expression.
8779 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8780 __isl_take isl_multi_pw_aff *mpa)
8782 int i;
8783 isl_size n;
8784 isl_space *space;
8785 isl_multi_union_pw_aff *mupa;
8787 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8788 if (n < 0)
8789 mpa = isl_multi_pw_aff_free(mpa);
8790 if (!mpa)
8791 return NULL;
8793 space = isl_multi_pw_aff_get_space(mpa);
8794 space = isl_space_range(space);
8795 mupa = isl_multi_union_pw_aff_alloc(space);
8797 for (i = 0; i < n; ++i) {
8798 isl_pw_aff *pa;
8799 isl_union_pw_aff *upa;
8801 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8802 upa = isl_union_pw_aff_from_pw_aff(pa);
8803 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8806 isl_multi_pw_aff_free(mpa);
8808 return mupa;
8811 /* Extract the range space of "pma" and assign it to *space.
8812 * If *space has already been set (through a previous call to this function),
8813 * then check that the range space is the same.
8815 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8817 isl_space **space = user;
8818 isl_space *pma_space;
8819 isl_bool equal;
8821 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8822 isl_pw_multi_aff_free(pma);
8824 if (!pma_space)
8825 return isl_stat_error;
8826 if (!*space) {
8827 *space = pma_space;
8828 return isl_stat_ok;
8831 equal = isl_space_is_equal(pma_space, *space);
8832 isl_space_free(pma_space);
8834 if (equal < 0)
8835 return isl_stat_error;
8836 if (!equal)
8837 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8838 "range spaces not the same", return isl_stat_error);
8839 return isl_stat_ok;
8842 /* Construct and return a multi union piecewise affine expression
8843 * that is equal to the given union piecewise multi affine expression.
8845 * In order to be able to perform the conversion, the input
8846 * needs to be non-empty and may only involve a single range space.
8848 * If the resulting multi union piecewise affine expression has
8849 * an explicit domain, then assign it the domain of the input.
8850 * In other cases, the domain is stored in the individual elements.
8852 __isl_give isl_multi_union_pw_aff *
8853 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8854 __isl_take isl_union_pw_multi_aff *upma)
8856 isl_space *space = NULL;
8857 isl_multi_union_pw_aff *mupa;
8858 int i;
8859 isl_size n;
8861 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8862 if (n < 0)
8863 goto error;
8864 if (n == 0)
8865 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8866 "cannot extract range space from empty input",
8867 goto error);
8868 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8869 &space) < 0)
8870 goto error;
8872 if (!space)
8873 goto error;
8875 n = isl_space_dim(space, isl_dim_set);
8876 if (n < 0)
8877 space = isl_space_free(space);
8878 mupa = isl_multi_union_pw_aff_alloc(space);
8880 for (i = 0; i < n; ++i) {
8881 isl_union_pw_aff *upa;
8883 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8884 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8886 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8887 isl_union_set *dom;
8888 isl_union_pw_multi_aff *copy;
8890 copy = isl_union_pw_multi_aff_copy(upma);
8891 dom = isl_union_pw_multi_aff_domain(copy);
8892 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8895 isl_union_pw_multi_aff_free(upma);
8896 return mupa;
8897 error:
8898 isl_space_free(space);
8899 isl_union_pw_multi_aff_free(upma);
8900 return NULL;
8903 /* This function performs the same operation as
8904 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8905 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8907 __isl_give isl_multi_union_pw_aff *
8908 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8909 __isl_take isl_union_pw_multi_aff *upma)
8911 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8914 /* Try and create an isl_multi_union_pw_aff that is equivalent
8915 * to the given isl_union_map.
8916 * The isl_union_map is required to be single-valued in each space.
8917 * Moreover, it cannot be empty and all range spaces need to be the same.
8918 * Otherwise, an error is produced.
8920 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8921 __isl_take isl_union_map *umap)
8923 isl_union_pw_multi_aff *upma;
8925 upma = isl_union_pw_multi_aff_from_union_map(umap);
8926 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8929 /* This function performs the same operation as
8930 * isl_multi_union_pw_aff_from_union_map,
8931 * but is considered as a function on an isl_union_map when exported.
8933 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8934 __isl_take isl_union_map *umap)
8936 return isl_multi_union_pw_aff_from_union_map(umap);
8939 /* Return a multiple union piecewise affine expression
8940 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8941 * have been aligned.
8943 * If the resulting multi union piecewise affine expression has
8944 * an explicit domain, then assign it the input domain.
8945 * In other cases, the domain is stored in the individual elements.
8947 static __isl_give isl_multi_union_pw_aff *
8948 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8949 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8951 int i;
8952 isl_size n;
8953 isl_space *space;
8954 isl_multi_union_pw_aff *mupa;
8956 n = isl_multi_val_dim(mv, isl_dim_set);
8957 if (!domain || n < 0)
8958 goto error;
8960 space = isl_multi_val_get_space(mv);
8961 mupa = isl_multi_union_pw_aff_alloc(space);
8962 for (i = 0; i < n; ++i) {
8963 isl_val *v;
8964 isl_union_pw_aff *upa;
8966 v = isl_multi_val_get_val(mv, i);
8967 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8969 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8971 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8972 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8973 isl_union_set_copy(domain));
8975 isl_union_set_free(domain);
8976 isl_multi_val_free(mv);
8977 return mupa;
8978 error:
8979 isl_union_set_free(domain);
8980 isl_multi_val_free(mv);
8981 return NULL;
8984 /* Return a multiple union piecewise affine expression
8985 * that is equal to "mv" on "domain".
8987 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8988 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8990 isl_bool equal_params;
8992 if (!domain || !mv)
8993 goto error;
8994 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8995 if (equal_params < 0)
8996 goto error;
8997 if (equal_params)
8998 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8999 domain, mv);
9000 domain = isl_union_set_align_params(domain,
9001 isl_multi_val_get_space(mv));
9002 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
9003 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
9004 error:
9005 isl_union_set_free(domain);
9006 isl_multi_val_free(mv);
9007 return NULL;
9010 /* Return a multiple union piecewise affine expression
9011 * that is equal to "ma" on "domain".
9013 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
9014 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
9016 isl_pw_multi_aff *pma;
9018 pma = isl_pw_multi_aff_from_multi_aff(ma);
9019 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
9022 /* Return a multiple union piecewise affine expression
9023 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9024 * have been aligned.
9026 * If the resulting multi union piecewise affine expression has
9027 * an explicit domain, then assign it the input domain.
9028 * In other cases, the domain is stored in the individual elements.
9030 static __isl_give isl_multi_union_pw_aff *
9031 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9032 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
9034 int i;
9035 isl_size n;
9036 isl_space *space;
9037 isl_multi_union_pw_aff *mupa;
9039 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
9040 if (!domain || n < 0)
9041 goto error;
9042 space = isl_pw_multi_aff_get_space(pma);
9043 mupa = isl_multi_union_pw_aff_alloc(space);
9044 for (i = 0; i < n; ++i) {
9045 isl_pw_aff *pa;
9046 isl_union_pw_aff *upa;
9048 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9049 upa = isl_union_pw_aff_pw_aff_on_domain(
9050 isl_union_set_copy(domain), pa);
9051 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9053 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9054 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
9055 isl_union_set_copy(domain));
9057 isl_union_set_free(domain);
9058 isl_pw_multi_aff_free(pma);
9059 return mupa;
9060 error:
9061 isl_union_set_free(domain);
9062 isl_pw_multi_aff_free(pma);
9063 return NULL;
9066 /* Return a multiple union piecewise affine expression
9067 * that is equal to "pma" on "domain".
9069 __isl_give isl_multi_union_pw_aff *
9070 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
9071 __isl_take isl_pw_multi_aff *pma)
9073 isl_bool equal_params;
9074 isl_space *space;
9076 space = isl_pw_multi_aff_peek_space(pma);
9077 equal_params = isl_union_set_space_has_equal_params(domain, space);
9078 if (equal_params < 0)
9079 goto error;
9080 if (equal_params)
9081 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9082 domain, pma);
9083 domain = isl_union_set_align_params(domain,
9084 isl_pw_multi_aff_get_space(pma));
9085 pma = isl_pw_multi_aff_align_params(pma,
9086 isl_union_set_get_space(domain));
9087 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
9088 pma);
9089 error:
9090 isl_union_set_free(domain);
9091 isl_pw_multi_aff_free(pma);
9092 return NULL;
9095 /* Return a union set containing those elements in the domains
9096 * of the elements of "mupa" where they are all zero.
9098 * If there are no elements, then simply return the entire domain.
9100 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
9101 __isl_take isl_multi_union_pw_aff *mupa)
9103 int i;
9104 isl_size n;
9105 isl_union_pw_aff *upa;
9106 isl_union_set *zero;
9108 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9109 if (n < 0)
9110 mupa = isl_multi_union_pw_aff_free(mupa);
9111 if (!mupa)
9112 return NULL;
9114 if (n == 0)
9115 return isl_multi_union_pw_aff_domain(mupa);
9117 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9118 zero = isl_union_pw_aff_zero_union_set(upa);
9120 for (i = 1; i < n; ++i) {
9121 isl_union_set *zero_i;
9123 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9124 zero_i = isl_union_pw_aff_zero_union_set(upa);
9126 zero = isl_union_set_intersect(zero, zero_i);
9129 isl_multi_union_pw_aff_free(mupa);
9130 return zero;
9133 /* Construct a union map mapping the shared domain
9134 * of the union piecewise affine expressions to the range of "mupa"
9135 * in the special case of a 0D multi union piecewise affine expression.
9137 * Construct a map between the explicit domain of "mupa" and
9138 * the range space.
9139 * Note that this assumes that the domain consists of explicit elements.
9141 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9142 __isl_take isl_multi_union_pw_aff *mupa)
9144 isl_bool is_params;
9145 isl_space *space;
9146 isl_union_set *dom, *ran;
9148 space = isl_multi_union_pw_aff_get_space(mupa);
9149 dom = isl_multi_union_pw_aff_domain(mupa);
9150 ran = isl_union_set_from_set(isl_set_universe(space));
9152 is_params = isl_union_set_is_params(dom);
9153 if (is_params < 0)
9154 dom = isl_union_set_free(dom);
9155 else if (is_params)
9156 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9157 "cannot create union map from expression without "
9158 "explicit domain elements",
9159 dom = isl_union_set_free(dom));
9161 return isl_union_map_from_domain_and_range(dom, ran);
9164 /* Construct a union map mapping the shared domain
9165 * of the union piecewise affine expressions to the range of "mupa"
9166 * with each dimension in the range equated to the
9167 * corresponding union piecewise affine expression.
9169 * If the input is zero-dimensional, then construct a mapping
9170 * from its explicit domain.
9172 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9173 __isl_take isl_multi_union_pw_aff *mupa)
9175 int i;
9176 isl_size n;
9177 isl_space *space;
9178 isl_union_map *umap;
9179 isl_union_pw_aff *upa;
9181 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9182 if (n < 0)
9183 mupa = isl_multi_union_pw_aff_free(mupa);
9184 if (!mupa)
9185 return NULL;
9187 if (n == 0)
9188 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9190 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9191 umap = isl_union_map_from_union_pw_aff(upa);
9193 for (i = 1; i < n; ++i) {
9194 isl_union_map *umap_i;
9196 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9197 umap_i = isl_union_map_from_union_pw_aff(upa);
9198 umap = isl_union_map_flat_range_product(umap, umap_i);
9201 space = isl_multi_union_pw_aff_get_space(mupa);
9202 umap = isl_union_map_reset_range_space(umap, space);
9204 isl_multi_union_pw_aff_free(mupa);
9205 return umap;
9208 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9209 * "range" is the space from which to set the range space.
9210 * "res" collects the results.
9212 struct isl_union_pw_multi_aff_reset_range_space_data {
9213 isl_space *range;
9214 isl_union_pw_multi_aff *res;
9217 /* Replace the range space of "pma" by the range space of data->range and
9218 * add the result to data->res.
9220 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9222 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9223 isl_space *space;
9225 space = isl_pw_multi_aff_get_space(pma);
9226 space = isl_space_domain(space);
9227 space = isl_space_extend_domain_with_range(space,
9228 isl_space_copy(data->range));
9229 pma = isl_pw_multi_aff_reset_space(pma, space);
9230 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9232 return data->res ? isl_stat_ok : isl_stat_error;
9235 /* Replace the range space of all the piecewise affine expressions in "upma" by
9236 * the range space of "space".
9238 * This assumes that all these expressions have the same output dimension.
9240 * Since the spaces of the expressions change, so do their hash values.
9241 * We therefore need to create a new isl_union_pw_multi_aff.
9242 * Note that the hash value is currently computed based on the entire
9243 * space even though there can only be a single expression with a given
9244 * domain space.
9246 static __isl_give isl_union_pw_multi_aff *
9247 isl_union_pw_multi_aff_reset_range_space(
9248 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9250 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9251 isl_space *space_upma;
9253 space_upma = isl_union_pw_multi_aff_get_space(upma);
9254 data.res = isl_union_pw_multi_aff_empty(space_upma);
9255 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9256 &reset_range_space, &data) < 0)
9257 data.res = isl_union_pw_multi_aff_free(data.res);
9259 isl_space_free(space);
9260 isl_union_pw_multi_aff_free(upma);
9261 return data.res;
9264 /* Construct and return a union piecewise multi affine expression
9265 * that is equal to the given multi union piecewise affine expression,
9266 * in the special case of a 0D multi union piecewise affine expression.
9268 * Construct a union piecewise multi affine expression
9269 * on top of the explicit domain of the input.
9271 __isl_give isl_union_pw_multi_aff *
9272 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9273 __isl_take isl_multi_union_pw_aff *mupa)
9275 isl_space *space;
9276 isl_multi_val *mv;
9277 isl_union_set *domain;
9279 space = isl_multi_union_pw_aff_get_space(mupa);
9280 mv = isl_multi_val_zero(space);
9281 domain = isl_multi_union_pw_aff_domain(mupa);
9282 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9285 /* Construct and return a union piecewise multi affine expression
9286 * that is equal to the given multi union piecewise affine expression.
9288 * If the input is zero-dimensional, then
9289 * construct a union piecewise multi affine expression
9290 * on top of the explicit domain of the input.
9292 __isl_give isl_union_pw_multi_aff *
9293 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9294 __isl_take isl_multi_union_pw_aff *mupa)
9296 int i;
9297 isl_size n;
9298 isl_space *space;
9299 isl_union_pw_multi_aff *upma;
9300 isl_union_pw_aff *upa;
9302 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9303 if (n < 0)
9304 mupa = isl_multi_union_pw_aff_free(mupa);
9305 if (!mupa)
9306 return NULL;
9308 if (n == 0)
9309 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9311 space = isl_multi_union_pw_aff_get_space(mupa);
9312 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9313 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9315 for (i = 1; i < n; ++i) {
9316 isl_union_pw_multi_aff *upma_i;
9318 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9319 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9320 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9323 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9325 isl_multi_union_pw_aff_free(mupa);
9326 return upma;
9329 /* Intersect the range of "mupa" with "range",
9330 * in the special case where "mupa" is 0D.
9332 * Intersect the domain of "mupa" with the constraints on the parameters
9333 * of "range".
9335 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9336 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9338 range = isl_set_params(range);
9339 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9340 return mupa;
9343 /* Intersect the range of "mupa" with "range".
9344 * That is, keep only those domain elements that have a function value
9345 * in "range".
9347 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9348 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9350 isl_union_pw_multi_aff *upma;
9351 isl_union_set *domain;
9352 isl_space *space;
9353 isl_size n;
9354 int match;
9356 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9357 if (n < 0 || !range)
9358 goto error;
9360 space = isl_set_get_space(range);
9361 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9362 space, isl_dim_set);
9363 isl_space_free(space);
9364 if (match < 0)
9365 goto error;
9366 if (!match)
9367 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9368 "space don't match", goto error);
9369 if (n == 0)
9370 return mupa_intersect_range_0D(mupa, range);
9372 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9373 isl_multi_union_pw_aff_copy(mupa));
9374 domain = isl_union_set_from_set(range);
9375 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9376 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9378 return mupa;
9379 error:
9380 isl_multi_union_pw_aff_free(mupa);
9381 isl_set_free(range);
9382 return NULL;
9385 /* Return the shared domain of the elements of "mupa",
9386 * in the special case where "mupa" is zero-dimensional.
9388 * Return the explicit domain of "mupa".
9389 * Note that this domain may be a parameter set, either
9390 * because "mupa" is meant to live in a set space or
9391 * because no explicit domain has been set.
9393 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9394 __isl_take isl_multi_union_pw_aff *mupa)
9396 isl_union_set *dom;
9398 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9399 isl_multi_union_pw_aff_free(mupa);
9401 return dom;
9404 /* Return the shared domain of the elements of "mupa".
9406 * If "mupa" is zero-dimensional, then return its explicit domain.
9408 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9409 __isl_take isl_multi_union_pw_aff *mupa)
9411 int i;
9412 isl_size n;
9413 isl_union_pw_aff *upa;
9414 isl_union_set *dom;
9416 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9417 if (n < 0)
9418 mupa = isl_multi_union_pw_aff_free(mupa);
9419 if (!mupa)
9420 return NULL;
9422 if (n == 0)
9423 return isl_multi_union_pw_aff_domain_0D(mupa);
9425 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9426 dom = isl_union_pw_aff_domain(upa);
9427 for (i = 1; i < n; ++i) {
9428 isl_union_set *dom_i;
9430 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9431 dom_i = isl_union_pw_aff_domain(upa);
9432 dom = isl_union_set_intersect(dom, dom_i);
9435 isl_multi_union_pw_aff_free(mupa);
9436 return dom;
9439 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9440 * In particular, the spaces have been aligned.
9441 * The result is defined over the shared domain of the elements of "mupa"
9443 * We first extract the parametric constant part of "aff" and
9444 * define that over the shared domain.
9445 * Then we iterate over all input dimensions of "aff" and add the corresponding
9446 * multiples of the elements of "mupa".
9447 * Finally, we consider the integer divisions, calling the function
9448 * recursively to obtain an isl_union_pw_aff corresponding to the
9449 * integer division argument.
9451 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9452 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9454 int i;
9455 isl_size n_in, n_div;
9456 isl_union_pw_aff *upa;
9457 isl_union_set *uset;
9458 isl_val *v;
9459 isl_aff *cst;
9461 n_in = isl_aff_dim(aff, isl_dim_in);
9462 n_div = isl_aff_dim(aff, isl_dim_div);
9463 if (n_in < 0 || n_div < 0)
9464 goto error;
9466 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9467 cst = isl_aff_copy(aff);
9468 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9469 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9470 cst = isl_aff_project_domain_on_params(cst);
9471 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9473 for (i = 0; i < n_in; ++i) {
9474 isl_union_pw_aff *upa_i;
9476 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9477 continue;
9478 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9479 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9480 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9481 upa = isl_union_pw_aff_add(upa, upa_i);
9484 for (i = 0; i < n_div; ++i) {
9485 isl_aff *div;
9486 isl_union_pw_aff *upa_i;
9488 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9489 continue;
9490 div = isl_aff_get_div(aff, i);
9491 upa_i = multi_union_pw_aff_apply_aff(
9492 isl_multi_union_pw_aff_copy(mupa), div);
9493 upa_i = isl_union_pw_aff_floor(upa_i);
9494 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9495 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9496 upa = isl_union_pw_aff_add(upa, upa_i);
9499 isl_multi_union_pw_aff_free(mupa);
9500 isl_aff_free(aff);
9502 return upa;
9503 error:
9504 isl_multi_union_pw_aff_free(mupa);
9505 isl_aff_free(aff);
9506 return NULL;
9509 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9510 * with the domain of "aff".
9511 * Furthermore, the dimension of this space needs to be greater than zero.
9512 * The result is defined over the shared domain of the elements of "mupa"
9514 * We perform these checks and then hand over control to
9515 * multi_union_pw_aff_apply_aff.
9517 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9518 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9520 isl_size dim;
9521 isl_space *space1, *space2;
9522 isl_bool equal;
9524 mupa = isl_multi_union_pw_aff_align_params(mupa,
9525 isl_aff_get_space(aff));
9526 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9527 if (!mupa || !aff)
9528 goto error;
9530 space1 = isl_multi_union_pw_aff_get_space(mupa);
9531 space2 = isl_aff_get_domain_space(aff);
9532 equal = isl_space_is_equal(space1, space2);
9533 isl_space_free(space1);
9534 isl_space_free(space2);
9535 if (equal < 0)
9536 goto error;
9537 if (!equal)
9538 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9539 "spaces don't match", goto error);
9540 dim = isl_aff_dim(aff, isl_dim_in);
9541 if (dim < 0)
9542 goto error;
9543 if (dim == 0)
9544 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9545 "cannot determine domains", goto error);
9547 return multi_union_pw_aff_apply_aff(mupa, aff);
9548 error:
9549 isl_multi_union_pw_aff_free(mupa);
9550 isl_aff_free(aff);
9551 return NULL;
9554 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9555 * The space of "mupa" is known to be compatible with the domain of "ma".
9557 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9558 * on the domain of "mupa".
9560 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9561 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9563 isl_union_set *dom;
9565 dom = isl_multi_union_pw_aff_domain(mupa);
9566 ma = isl_multi_aff_project_domain_on_params(ma);
9568 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9571 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9572 * with the domain of "ma".
9573 * The result is defined over the shared domain of the elements of "mupa"
9575 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9576 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9578 isl_space *space1, *space2;
9579 isl_multi_union_pw_aff *res;
9580 isl_bool equal;
9581 int i;
9582 isl_size n_in, n_out;
9584 mupa = isl_multi_union_pw_aff_align_params(mupa,
9585 isl_multi_aff_get_space(ma));
9586 ma = isl_multi_aff_align_params(ma,
9587 isl_multi_union_pw_aff_get_space(mupa));
9588 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9589 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9590 if (!mupa || n_in < 0 || n_out < 0)
9591 goto error;
9593 space1 = isl_multi_union_pw_aff_get_space(mupa);
9594 space2 = isl_multi_aff_get_domain_space(ma);
9595 equal = isl_space_is_equal(space1, space2);
9596 isl_space_free(space1);
9597 isl_space_free(space2);
9598 if (equal < 0)
9599 goto error;
9600 if (!equal)
9601 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9602 "spaces don't match", goto error);
9603 if (n_in == 0)
9604 return mupa_apply_multi_aff_0D(mupa, ma);
9606 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9607 res = isl_multi_union_pw_aff_alloc(space1);
9609 for (i = 0; i < n_out; ++i) {
9610 isl_aff *aff;
9611 isl_union_pw_aff *upa;
9613 aff = isl_multi_aff_get_aff(ma, i);
9614 upa = multi_union_pw_aff_apply_aff(
9615 isl_multi_union_pw_aff_copy(mupa), aff);
9616 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9619 isl_multi_aff_free(ma);
9620 isl_multi_union_pw_aff_free(mupa);
9621 return res;
9622 error:
9623 isl_multi_union_pw_aff_free(mupa);
9624 isl_multi_aff_free(ma);
9625 return NULL;
9628 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9629 * The space of "mupa" is known to be compatible with the domain of "pa".
9631 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9632 * on the domain of "mupa".
9634 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9635 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9637 isl_union_set *dom;
9639 dom = isl_multi_union_pw_aff_domain(mupa);
9640 pa = isl_pw_aff_project_domain_on_params(pa);
9642 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9645 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9646 * with the domain of "pa".
9647 * Furthermore, the dimension of this space needs to be greater than zero.
9648 * The result is defined over the shared domain of the elements of "mupa"
9650 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9651 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9653 int i;
9654 isl_bool equal;
9655 isl_size n_in;
9656 isl_space *space, *space2;
9657 isl_union_pw_aff *upa;
9659 mupa = isl_multi_union_pw_aff_align_params(mupa,
9660 isl_pw_aff_get_space(pa));
9661 pa = isl_pw_aff_align_params(pa,
9662 isl_multi_union_pw_aff_get_space(mupa));
9663 if (!mupa || !pa)
9664 goto error;
9666 space = isl_multi_union_pw_aff_get_space(mupa);
9667 space2 = isl_pw_aff_get_domain_space(pa);
9668 equal = isl_space_is_equal(space, space2);
9669 isl_space_free(space);
9670 isl_space_free(space2);
9671 if (equal < 0)
9672 goto error;
9673 if (!equal)
9674 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9675 "spaces don't match", goto error);
9676 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9677 if (n_in < 0)
9678 goto error;
9679 if (n_in == 0)
9680 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9682 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9683 upa = isl_union_pw_aff_empty(space);
9685 for (i = 0; i < pa->n; ++i) {
9686 isl_aff *aff;
9687 isl_set *domain;
9688 isl_multi_union_pw_aff *mupa_i;
9689 isl_union_pw_aff *upa_i;
9691 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9692 domain = isl_set_copy(pa->p[i].set);
9693 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9694 aff = isl_aff_copy(pa->p[i].aff);
9695 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9696 upa = isl_union_pw_aff_union_add(upa, upa_i);
9699 isl_multi_union_pw_aff_free(mupa);
9700 isl_pw_aff_free(pa);
9701 return upa;
9702 error:
9703 isl_multi_union_pw_aff_free(mupa);
9704 isl_pw_aff_free(pa);
9705 return NULL;
9708 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9709 * The space of "mupa" is known to be compatible with the domain of "pma".
9711 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9712 * on the domain of "mupa".
9714 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9715 __isl_take isl_multi_union_pw_aff *mupa,
9716 __isl_take isl_pw_multi_aff *pma)
9718 isl_union_set *dom;
9720 dom = isl_multi_union_pw_aff_domain(mupa);
9721 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9723 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9726 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9727 * with the domain of "pma".
9728 * The result is defined over the shared domain of the elements of "mupa"
9730 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9731 __isl_take isl_multi_union_pw_aff *mupa,
9732 __isl_take isl_pw_multi_aff *pma)
9734 isl_space *space1, *space2;
9735 isl_multi_union_pw_aff *res;
9736 isl_bool equal;
9737 int i;
9738 isl_size n_in, n_out;
9740 mupa = isl_multi_union_pw_aff_align_params(mupa,
9741 isl_pw_multi_aff_get_space(pma));
9742 pma = isl_pw_multi_aff_align_params(pma,
9743 isl_multi_union_pw_aff_get_space(mupa));
9744 if (!mupa || !pma)
9745 goto error;
9747 space1 = isl_multi_union_pw_aff_get_space(mupa);
9748 space2 = isl_pw_multi_aff_get_domain_space(pma);
9749 equal = isl_space_is_equal(space1, space2);
9750 isl_space_free(space1);
9751 isl_space_free(space2);
9752 if (equal < 0)
9753 goto error;
9754 if (!equal)
9755 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9756 "spaces don't match", goto error);
9757 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9758 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9759 if (n_in < 0 || n_out < 0)
9760 goto error;
9761 if (n_in == 0)
9762 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9764 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9765 res = isl_multi_union_pw_aff_alloc(space1);
9767 for (i = 0; i < n_out; ++i) {
9768 isl_pw_aff *pa;
9769 isl_union_pw_aff *upa;
9771 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9772 upa = isl_multi_union_pw_aff_apply_pw_aff(
9773 isl_multi_union_pw_aff_copy(mupa), pa);
9774 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9777 isl_pw_multi_aff_free(pma);
9778 isl_multi_union_pw_aff_free(mupa);
9779 return res;
9780 error:
9781 isl_multi_union_pw_aff_free(mupa);
9782 isl_pw_multi_aff_free(pma);
9783 return NULL;
9786 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9787 * If the explicit domain only keeps track of constraints on the parameters,
9788 * then only update those constraints.
9790 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9791 __isl_take isl_multi_union_pw_aff *mupa,
9792 __isl_keep isl_union_pw_multi_aff *upma)
9794 isl_bool is_params;
9796 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9797 return isl_multi_union_pw_aff_free(mupa);
9799 mupa = isl_multi_union_pw_aff_cow(mupa);
9800 if (!mupa)
9801 return NULL;
9803 is_params = isl_union_set_is_params(mupa->u.dom);
9804 if (is_params < 0)
9805 return isl_multi_union_pw_aff_free(mupa);
9807 upma = isl_union_pw_multi_aff_copy(upma);
9808 if (is_params)
9809 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9810 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9811 else
9812 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9813 mupa->u.dom, upma);
9814 if (!mupa->u.dom)
9815 return isl_multi_union_pw_aff_free(mupa);
9816 return mupa;
9819 /* Compute the pullback of "mupa" by the function represented by "upma".
9820 * In other words, plug in "upma" in "mupa". The result contains
9821 * expressions defined over the domain space of "upma".
9823 * Run over all elements of "mupa" and plug in "upma" in each of them.
9825 * If "mupa" has an explicit domain, then it is this domain
9826 * that needs to undergo a pullback instead, i.e., a preimage.
9828 __isl_give isl_multi_union_pw_aff *
9829 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9830 __isl_take isl_multi_union_pw_aff *mupa,
9831 __isl_take isl_union_pw_multi_aff *upma)
9833 int i;
9834 isl_size n;
9836 mupa = isl_multi_union_pw_aff_align_params(mupa,
9837 isl_union_pw_multi_aff_get_space(upma));
9838 upma = isl_union_pw_multi_aff_align_params(upma,
9839 isl_multi_union_pw_aff_get_space(mupa));
9840 mupa = isl_multi_union_pw_aff_cow(mupa);
9841 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9842 if (n < 0 || !upma)
9843 goto error;
9845 for (i = 0; i < n; ++i) {
9846 isl_union_pw_aff *upa;
9848 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9849 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9850 isl_union_pw_multi_aff_copy(upma));
9851 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9854 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9855 mupa = preimage_explicit_domain(mupa, upma);
9857 isl_union_pw_multi_aff_free(upma);
9858 return mupa;
9859 error:
9860 isl_multi_union_pw_aff_free(mupa);
9861 isl_union_pw_multi_aff_free(upma);
9862 return NULL;
9865 /* Extract the sequence of elements in "mupa" with domain space "space"
9866 * (ignoring parameters).
9868 * For the elements of "mupa" that are not defined on the specified space,
9869 * the corresponding element in the result is empty.
9871 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9872 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9874 int i;
9875 isl_size n;
9876 isl_space *space_mpa;
9877 isl_multi_pw_aff *mpa;
9879 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9880 if (n < 0 || !space)
9881 goto error;
9883 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9884 space = isl_space_replace_params(space, space_mpa);
9885 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9886 space_mpa);
9887 mpa = isl_multi_pw_aff_alloc(space_mpa);
9889 space = isl_space_from_domain(space);
9890 space = isl_space_add_dims(space, isl_dim_out, 1);
9891 for (i = 0; i < n; ++i) {
9892 isl_union_pw_aff *upa;
9893 isl_pw_aff *pa;
9895 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9896 pa = isl_union_pw_aff_extract_pw_aff(upa,
9897 isl_space_copy(space));
9898 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9899 isl_union_pw_aff_free(upa);
9902 isl_space_free(space);
9903 return mpa;
9904 error:
9905 isl_space_free(space);
9906 return NULL;
9909 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9910 * should modify the base expressions in the input.
9912 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9913 * are taken into account.
9914 * "fn" is applied to each entry in the input.
9916 struct isl_union_pw_multi_aff_un_op_control {
9917 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9918 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9921 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9922 * a second argument) for use as an isl_union_pw_multi_aff_transform
9923 * filter function (which does take a second argument).
9924 * Simply call control->filter without the second argument.
9926 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9927 __isl_take isl_pw_multi_aff *pma, void *user)
9929 struct isl_union_pw_multi_aff_un_op_control *control = user;
9931 return control->filter(pma);
9934 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9935 * a second argument) for use as an isl_union_pw_multi_aff_transform
9936 * base function (which does take a second argument).
9937 * Simply call control->fn without the second argument.
9939 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9940 __isl_take isl_pw_multi_aff *pma, void *user)
9942 struct isl_union_pw_multi_aff_un_op_control *control = user;
9944 return control->fn(pma);
9947 /* Construct an isl_union_pw_multi_aff that is obtained by
9948 * modifying "upma" according to "control".
9950 * isl_union_pw_multi_aff_transform performs essentially
9951 * the same operation, but takes a filter and a callback function
9952 * of a different form (with an extra argument).
9953 * Call isl_union_pw_multi_aff_transform with wrappers
9954 * that remove this extra argument.
9956 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9957 __isl_take isl_union_pw_multi_aff *upma,
9958 struct isl_union_pw_multi_aff_un_op_control *control)
9960 struct isl_union_pw_multi_aff_transform_control t_control = {
9961 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9962 .filter_user = control,
9963 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9964 .fn_user = control,
9967 return isl_union_pw_multi_aff_transform(upma, &t_control);
9970 /* For each function in "upma" of the form A -> [B -> C],
9971 * extract the function A -> B and collect the results.
9973 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9974 __isl_take isl_union_pw_multi_aff *upma)
9976 struct isl_union_pw_multi_aff_un_op_control control = {
9977 .filter = &isl_pw_multi_aff_range_is_wrapping,
9978 .fn = &isl_pw_multi_aff_range_factor_domain,
9980 return isl_union_pw_multi_aff_un_op(upma, &control);
9983 /* For each function in "upma" of the form A -> [B -> C],
9984 * extract the function A -> C and collect the results.
9986 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9987 __isl_take isl_union_pw_multi_aff *upma)
9989 struct isl_union_pw_multi_aff_un_op_control control = {
9990 .filter = &isl_pw_multi_aff_range_is_wrapping,
9991 .fn = &isl_pw_multi_aff_range_factor_range,
9993 return isl_union_pw_multi_aff_un_op(upma, &control);
9996 /* Evaluate the affine function "aff" in the void point "pnt".
9997 * In particular, return the value NaN.
9999 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
10000 __isl_take isl_point *pnt)
10002 isl_ctx *ctx;
10004 ctx = isl_point_get_ctx(pnt);
10005 isl_aff_free(aff);
10006 isl_point_free(pnt);
10007 return isl_val_nan(ctx);
10010 /* Evaluate the affine expression "aff"
10011 * in the coordinates (with denominator) "pnt".
10013 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
10014 __isl_keep isl_vec *pnt)
10016 isl_int n, d;
10017 isl_ctx *ctx;
10018 isl_val *v;
10020 if (!aff || !pnt)
10021 return NULL;
10023 ctx = isl_vec_get_ctx(aff);
10024 isl_int_init(n);
10025 isl_int_init(d);
10026 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
10027 isl_int_mul(d, aff->el[0], pnt->el[0]);
10028 v = isl_val_rat_from_isl_int(ctx, n, d);
10029 v = isl_val_normalize(v);
10030 isl_int_clear(n);
10031 isl_int_clear(d);
10033 return v;
10036 /* Check that the domain space of "aff" is equal to "space".
10038 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
10039 __isl_keep isl_space *space)
10041 isl_bool ok;
10043 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
10044 if (ok < 0)
10045 return isl_stat_error;
10046 if (!ok)
10047 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
10048 "incompatible spaces", return isl_stat_error);
10049 return isl_stat_ok;
10052 /* Evaluate the affine function "aff" in "pnt".
10054 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
10055 __isl_take isl_point *pnt)
10057 isl_bool is_void;
10058 isl_val *v;
10059 isl_local_space *ls;
10061 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
10062 goto error;
10063 is_void = isl_point_is_void(pnt);
10064 if (is_void < 0)
10065 goto error;
10066 if (is_void)
10067 return eval_void(aff, pnt);
10069 ls = isl_aff_get_domain_local_space(aff);
10070 pnt = isl_local_space_lift_point(ls, pnt);
10072 v = eval(aff->v, isl_point_peek_vec(pnt));
10074 isl_aff_free(aff);
10075 isl_point_free(pnt);
10077 return v;
10078 error:
10079 isl_aff_free(aff);
10080 isl_point_free(pnt);
10081 return NULL;