re-export isl_multi_*_from_*_list as isl_space_multi_*
[isl.git] / isl_aff.c
blob0f2b017b20a7c10f9ed98b68e46e1d6d952cf81d
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
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
12 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 * 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_union_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl_local_space_private.h>
26 #include <isl_vec_private.h>
27 #include <isl_mat_private.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl_val_private.h>
33 #include <isl_point_private.h>
34 #include <isl_config.h>
36 #undef EL_BASE
37 #define EL_BASE aff
39 #include <isl_list_templ.c>
41 #undef EL_BASE
42 #define EL_BASE pw_aff
44 #include <isl_list_templ.c>
46 #undef EL_BASE
47 #define EL_BASE pw_multi_aff
49 #include <isl_list_templ.c>
51 #undef EL_BASE
52 #define EL_BASE union_pw_aff
54 #include <isl_list_templ.c>
56 #undef EL_BASE
57 #define EL_BASE union_pw_multi_aff
59 #include <isl_list_templ.c>
61 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
62 __isl_take isl_vec *v)
64 isl_aff *aff;
66 if (!ls || !v)
67 goto error;
69 aff = isl_calloc_type(v->ctx, struct isl_aff);
70 if (!aff)
71 goto error;
73 aff->ref = 1;
74 aff->ls = ls;
75 aff->v = v;
77 return aff;
78 error:
79 isl_local_space_free(ls);
80 isl_vec_free(v);
81 return NULL;
84 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
86 isl_ctx *ctx;
87 isl_vec *v;
88 isl_size total;
90 if (!ls)
91 return NULL;
93 ctx = isl_local_space_get_ctx(ls);
94 if (!isl_local_space_divs_known(ls))
95 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
96 goto error);
97 if (!isl_local_space_is_set(ls))
98 isl_die(ctx, isl_error_invalid,
99 "domain of affine expression should be a set",
100 goto error);
102 total = isl_local_space_dim(ls, isl_dim_all);
103 if (total < 0)
104 goto error;
105 v = isl_vec_alloc(ctx, 1 + 1 + total);
106 return isl_aff_alloc_vec(ls, v);
107 error:
108 isl_local_space_free(ls);
109 return NULL;
112 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
114 if (!aff)
115 return NULL;
117 aff->ref++;
118 return aff;
121 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
123 if (!aff)
124 return NULL;
126 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
127 isl_vec_copy(aff->v));
130 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
132 if (!aff)
133 return NULL;
135 if (aff->ref == 1)
136 return aff;
137 aff->ref--;
138 return isl_aff_dup(aff);
141 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
143 isl_aff *aff;
145 aff = isl_aff_alloc(ls);
146 if (!aff)
147 return NULL;
149 isl_int_set_si(aff->v->el[0], 1);
150 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
152 return aff;
155 /* Return an affine expression that is equal to zero on domain space "space".
157 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
159 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
162 /* This function performs the same operation as isl_aff_zero_on_domain_space,
163 * but is considered as a function on an isl_space when exported.
165 __isl_give isl_aff *isl_space_zero_aff_on_domain(__isl_take isl_space *space)
167 return isl_aff_zero_on_domain_space(space);
170 /* Return a piecewise affine expression defined on the specified domain
171 * that is equal to zero.
173 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
175 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
178 /* Change "aff" into a NaN.
180 * Note that this function gets called from isl_aff_nan_on_domain,
181 * so "aff" may not have been initialized yet.
183 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
185 aff = isl_aff_cow(aff);
186 if (!aff)
187 return NULL;
189 aff->v = isl_vec_clr(aff->v);
190 if (!aff->v)
191 return isl_aff_free(aff);
193 return aff;
196 /* Return an affine expression defined on the specified domain
197 * that represents NaN.
199 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
201 isl_aff *aff;
203 aff = isl_aff_alloc(ls);
204 return isl_aff_set_nan(aff);
207 /* Return an affine expression defined on the specified domain space
208 * that represents NaN.
210 __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
212 return isl_aff_nan_on_domain(isl_local_space_from_space(space));
215 /* Return a piecewise affine expression defined on the specified domain space
216 * that represents NaN.
218 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
219 __isl_take isl_space *space)
221 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
224 /* Return a piecewise affine expression defined on the specified domain
225 * that represents NaN.
227 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
229 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
232 /* Return an affine expression that is equal to "val" on
233 * domain local space "ls".
235 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
236 __isl_take isl_val *val)
238 isl_aff *aff;
240 if (!ls || !val)
241 goto error;
242 if (!isl_val_is_rat(val))
243 isl_die(isl_val_get_ctx(val), isl_error_invalid,
244 "expecting rational value", goto error);
246 aff = isl_aff_alloc(isl_local_space_copy(ls));
247 if (!aff)
248 goto error;
250 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
251 isl_int_set(aff->v->el[1], val->n);
252 isl_int_set(aff->v->el[0], val->d);
254 isl_local_space_free(ls);
255 isl_val_free(val);
256 return aff;
257 error:
258 isl_local_space_free(ls);
259 isl_val_free(val);
260 return NULL;
263 /* Return an affine expression that is equal to "val" on domain space "space".
265 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
266 __isl_take isl_val *val)
268 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
271 /* Return an affine expression that is equal to the specified dimension
272 * in "ls".
274 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
275 enum isl_dim_type type, unsigned pos)
277 isl_space *space;
278 isl_aff *aff;
280 if (!ls)
281 return NULL;
283 space = isl_local_space_get_space(ls);
284 if (!space)
285 goto error;
286 if (isl_space_is_map(space))
287 isl_die(isl_space_get_ctx(space), isl_error_invalid,
288 "expecting (parameter) set space", goto error);
289 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
290 goto error;
292 isl_space_free(space);
293 aff = isl_aff_alloc(ls);
294 if (!aff)
295 return NULL;
297 pos += isl_local_space_offset(aff->ls, type);
299 isl_int_set_si(aff->v->el[0], 1);
300 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
301 isl_int_set_si(aff->v->el[1 + pos], 1);
303 return aff;
304 error:
305 isl_local_space_free(ls);
306 isl_space_free(space);
307 return NULL;
310 /* Return a piecewise affine expression that is equal to
311 * the specified dimension in "ls".
313 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
314 enum isl_dim_type type, unsigned pos)
316 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
319 /* Return an affine expression that is equal to the parameter
320 * in the domain space "space" with identifier "id".
322 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
323 __isl_take isl_space *space, __isl_take isl_id *id)
325 int pos;
326 isl_local_space *ls;
328 if (!space || !id)
329 goto error;
330 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
331 if (pos < 0)
332 isl_die(isl_space_get_ctx(space), isl_error_invalid,
333 "parameter not found in space", goto error);
334 isl_id_free(id);
335 ls = isl_local_space_from_space(space);
336 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
337 error:
338 isl_space_free(space);
339 isl_id_free(id);
340 return NULL;
343 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
345 if (!aff)
346 return NULL;
348 if (--aff->ref > 0)
349 return NULL;
351 isl_local_space_free(aff->ls);
352 isl_vec_free(aff->v);
354 free(aff);
356 return NULL;
359 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
361 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
364 /* Return a hash value that digests "aff".
366 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
368 uint32_t hash, ls_hash, v_hash;
370 if (!aff)
371 return 0;
373 hash = isl_hash_init();
374 ls_hash = isl_local_space_get_hash(aff->ls);
375 isl_hash_hash(hash, ls_hash);
376 v_hash = isl_vec_get_hash(aff->v);
377 isl_hash_hash(hash, v_hash);
379 return hash;
382 /* Return the domain local space of "aff".
384 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
385 __isl_keep isl_aff *aff)
387 return aff ? aff->ls : NULL;
390 /* Return the number of variables of the given type in the domain of "aff".
392 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
394 isl_local_space *ls;
396 ls = isl_aff_peek_domain_local_space(aff);
397 return isl_local_space_dim(ls, type);
400 /* Externally, an isl_aff has a map space, but internally, the
401 * ls field corresponds to the domain of that space.
403 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
405 if (!aff)
406 return isl_size_error;
407 if (type == isl_dim_out)
408 return 1;
409 if (type == isl_dim_in)
410 type = isl_dim_set;
411 return isl_aff_domain_dim(aff, type);
414 /* Return the offset of the first coefficient of type "type" in
415 * the domain of "aff".
417 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
419 isl_local_space *ls;
421 ls = isl_aff_peek_domain_local_space(aff);
422 return isl_local_space_offset(ls, type);
425 /* Return the position of the dimension of the given type and name
426 * in "aff".
427 * Return -1 if no such dimension can be found.
429 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
430 const char *name)
432 if (!aff)
433 return -1;
434 if (type == isl_dim_out)
435 return -1;
436 if (type == isl_dim_in)
437 type = isl_dim_set;
438 return isl_local_space_find_dim_by_name(aff->ls, type, name);
441 /* Return the domain space of "aff".
443 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
445 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
448 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
450 return isl_space_copy(isl_aff_peek_domain_space(aff));
453 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
455 isl_space *space;
456 if (!aff)
457 return NULL;
458 space = isl_local_space_get_space(aff->ls);
459 space = isl_space_from_domain(space);
460 space = isl_space_add_dims(space, isl_dim_out, 1);
461 return space;
464 /* Return a copy of the domain space of "aff".
466 __isl_give isl_local_space *isl_aff_get_domain_local_space(
467 __isl_keep isl_aff *aff)
469 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
472 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
474 isl_local_space *ls;
475 if (!aff)
476 return NULL;
477 ls = isl_local_space_copy(aff->ls);
478 ls = isl_local_space_from_domain(ls);
479 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
480 return ls;
483 /* Return the local space of the domain of "aff".
484 * This may be either a copy or the local space itself
485 * if there is only one reference to "aff".
486 * This allows the local space to be modified inplace
487 * if both the expression and its local space have only a single reference.
488 * The caller is not allowed to modify "aff" between this call and
489 * a subsequent call to isl_aff_restore_domain_local_space.
490 * The only exception is that isl_aff_free can be called instead.
492 __isl_give isl_local_space *isl_aff_take_domain_local_space(
493 __isl_keep isl_aff *aff)
495 isl_local_space *ls;
497 if (!aff)
498 return NULL;
499 if (aff->ref != 1)
500 return isl_aff_get_domain_local_space(aff);
501 ls = aff->ls;
502 aff->ls = NULL;
503 return ls;
506 /* Set the local space of the domain of "aff" to "ls",
507 * where the local space of "aff" may be missing
508 * due to a preceding call to isl_aff_take_domain_local_space.
509 * However, in this case, "aff" only has a single reference and
510 * then the call to isl_aff_cow has no effect.
512 __isl_give isl_aff *isl_aff_restore_domain_local_space(
513 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
515 if (!aff || !ls)
516 goto error;
518 if (aff->ls == ls) {
519 isl_local_space_free(ls);
520 return aff;
523 aff = isl_aff_cow(aff);
524 if (!aff)
525 goto error;
526 isl_local_space_free(aff->ls);
527 aff->ls = ls;
529 return aff;
530 error:
531 isl_aff_free(aff);
532 isl_local_space_free(ls);
533 return NULL;
536 /* Externally, an isl_aff has a map space, but internally, the
537 * ls field corresponds to the domain of that space.
539 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
540 enum isl_dim_type type, unsigned pos)
542 if (!aff)
543 return NULL;
544 if (type == isl_dim_out)
545 return NULL;
546 if (type == isl_dim_in)
547 type = isl_dim_set;
548 return isl_local_space_get_dim_name(aff->ls, type, pos);
551 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
552 __isl_take isl_space *space)
554 aff = isl_aff_cow(aff);
555 if (!aff || !space)
556 goto error;
558 aff->ls = isl_local_space_reset_space(aff->ls, space);
559 if (!aff->ls)
560 return isl_aff_free(aff);
562 return aff;
563 error:
564 isl_aff_free(aff);
565 isl_space_free(space);
566 return NULL;
569 /* Reset the space of "aff". This function is called from isl_pw_templ.c
570 * and doesn't know if the space of an element object is represented
571 * directly or through its domain. It therefore passes along both.
573 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
574 __isl_take isl_space *space, __isl_take isl_space *domain)
576 isl_space_free(space);
577 return isl_aff_reset_domain_space(aff, domain);
580 /* Reorder the coefficients of the affine expression based
581 * on the given reordering.
582 * The reordering r is assumed to have been extended with the local
583 * variables.
585 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
586 __isl_take isl_reordering *r, int n_div)
588 isl_space *space;
589 isl_vec *res;
590 isl_size dim;
591 int i;
593 if (!vec || !r)
594 goto error;
596 space = isl_reordering_peek_space(r);
597 dim = isl_space_dim(space, isl_dim_all);
598 if (dim < 0)
599 goto error;
600 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
601 if (!res)
602 goto error;
603 isl_seq_cpy(res->el, vec->el, 2);
604 isl_seq_clr(res->el + 2, res->size - 2);
605 for (i = 0; i < r->len; ++i)
606 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
608 isl_reordering_free(r);
609 isl_vec_free(vec);
610 return res;
611 error:
612 isl_vec_free(vec);
613 isl_reordering_free(r);
614 return NULL;
617 /* Reorder the dimensions of the domain of "aff" according
618 * to the given reordering.
620 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
621 __isl_take isl_reordering *r)
623 aff = isl_aff_cow(aff);
624 if (!aff)
625 goto error;
627 r = isl_reordering_extend(r, aff->ls->div->n_row);
628 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
629 aff->ls->div->n_row);
630 aff->ls = isl_local_space_realign(aff->ls, r);
632 if (!aff->v || !aff->ls)
633 return isl_aff_free(aff);
635 return aff;
636 error:
637 isl_aff_free(aff);
638 isl_reordering_free(r);
639 return NULL;
642 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
643 __isl_take isl_space *model)
645 isl_bool equal_params;
647 if (!aff || !model)
648 goto error;
650 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
651 if (equal_params < 0)
652 goto error;
653 if (!equal_params) {
654 isl_reordering *exp;
656 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
657 exp = isl_reordering_extend_space(exp,
658 isl_aff_get_domain_space(aff));
659 aff = isl_aff_realign_domain(aff, exp);
662 isl_space_free(model);
663 return aff;
664 error:
665 isl_space_free(model);
666 isl_aff_free(aff);
667 return NULL;
670 #undef TYPE
671 #define TYPE isl_aff
672 #include "isl_unbind_params_templ.c"
674 /* Is "aff" obviously equal to zero?
676 * If the denominator is zero, then "aff" is not equal to zero.
678 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
680 int pos;
682 if (!aff)
683 return isl_bool_error;
685 if (isl_int_is_zero(aff->v->el[0]))
686 return isl_bool_false;
687 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
688 return isl_bool_ok(pos < 0);
691 /* Does "aff" represent NaN?
693 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
695 if (!aff)
696 return isl_bool_error;
698 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
701 /* Are "aff1" and "aff2" obviously equal?
703 * NaN is not equal to anything, not even to another NaN.
705 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
706 __isl_keep isl_aff *aff2)
708 isl_bool equal;
710 if (!aff1 || !aff2)
711 return isl_bool_error;
713 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
714 return isl_bool_false;
716 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
717 if (equal < 0 || !equal)
718 return equal;
720 return isl_vec_is_equal(aff1->v, aff2->v);
723 /* Return the common denominator of "aff" in "v".
725 * We cannot return anything meaningful in case of a NaN.
727 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
729 if (!aff)
730 return isl_stat_error;
731 if (isl_aff_is_nan(aff))
732 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
733 "cannot get denominator of NaN", return isl_stat_error);
734 isl_int_set(*v, aff->v->el[0]);
735 return isl_stat_ok;
738 /* Return the common denominator of "aff".
740 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
742 isl_ctx *ctx;
744 if (!aff)
745 return NULL;
747 ctx = isl_aff_get_ctx(aff);
748 if (isl_aff_is_nan(aff))
749 return isl_val_nan(ctx);
750 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
753 /* Return the constant term of "aff".
755 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
757 isl_ctx *ctx;
758 isl_val *v;
760 if (!aff)
761 return NULL;
763 ctx = isl_aff_get_ctx(aff);
764 if (isl_aff_is_nan(aff))
765 return isl_val_nan(ctx);
766 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
767 return isl_val_normalize(v);
770 /* Return the coefficient of the variable of type "type" at position "pos"
771 * of "aff".
773 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
774 enum isl_dim_type type, int pos)
776 isl_ctx *ctx;
777 isl_val *v;
779 if (!aff)
780 return NULL;
782 ctx = isl_aff_get_ctx(aff);
783 if (type == isl_dim_out)
784 isl_die(ctx, isl_error_invalid,
785 "output/set dimension does not have a coefficient",
786 return NULL);
787 if (type == isl_dim_in)
788 type = isl_dim_set;
790 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
791 return NULL;
793 if (isl_aff_is_nan(aff))
794 return isl_val_nan(ctx);
795 pos += isl_local_space_offset(aff->ls, type);
796 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
797 return isl_val_normalize(v);
800 /* Return the sign of the coefficient of the variable of type "type"
801 * at position "pos" of "aff".
803 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
804 int pos)
806 isl_ctx *ctx;
808 if (!aff)
809 return 0;
811 ctx = isl_aff_get_ctx(aff);
812 if (type == isl_dim_out)
813 isl_die(ctx, isl_error_invalid,
814 "output/set dimension does not have a coefficient",
815 return 0);
816 if (type == isl_dim_in)
817 type = isl_dim_set;
819 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
820 return 0;
822 pos += isl_local_space_offset(aff->ls, type);
823 return isl_int_sgn(aff->v->el[1 + pos]);
826 /* Replace the numerator of the constant term of "aff" by "v".
828 * A NaN is unaffected by this operation.
830 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
832 if (!aff)
833 return NULL;
834 if (isl_aff_is_nan(aff))
835 return aff;
836 aff = isl_aff_cow(aff);
837 if (!aff)
838 return NULL;
840 aff->v = isl_vec_cow(aff->v);
841 if (!aff->v)
842 return isl_aff_free(aff);
844 isl_int_set(aff->v->el[1], v);
846 return aff;
849 /* Replace the constant term of "aff" by "v".
851 * A NaN is unaffected by this operation.
853 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
854 __isl_take isl_val *v)
856 if (!aff || !v)
857 goto error;
859 if (isl_aff_is_nan(aff)) {
860 isl_val_free(v);
861 return aff;
864 if (!isl_val_is_rat(v))
865 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
866 "expecting rational value", goto error);
868 if (isl_int_eq(aff->v->el[1], v->n) &&
869 isl_int_eq(aff->v->el[0], v->d)) {
870 isl_val_free(v);
871 return aff;
874 aff = isl_aff_cow(aff);
875 if (!aff)
876 goto error;
877 aff->v = isl_vec_cow(aff->v);
878 if (!aff->v)
879 goto error;
881 if (isl_int_eq(aff->v->el[0], v->d)) {
882 isl_int_set(aff->v->el[1], v->n);
883 } else if (isl_int_is_one(v->d)) {
884 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
885 } else {
886 isl_seq_scale(aff->v->el + 1,
887 aff->v->el + 1, v->d, aff->v->size - 1);
888 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
889 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
890 aff->v = isl_vec_normalize(aff->v);
891 if (!aff->v)
892 goto error;
895 isl_val_free(v);
896 return aff;
897 error:
898 isl_aff_free(aff);
899 isl_val_free(v);
900 return NULL;
903 /* Add "v" to the constant term of "aff".
905 * A NaN is unaffected by this operation.
907 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
909 if (isl_int_is_zero(v))
910 return aff;
912 if (!aff)
913 return NULL;
914 if (isl_aff_is_nan(aff))
915 return aff;
916 aff = isl_aff_cow(aff);
917 if (!aff)
918 return NULL;
920 aff->v = isl_vec_cow(aff->v);
921 if (!aff->v)
922 return isl_aff_free(aff);
924 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
926 return aff;
929 /* Add "v" to the constant term of "aff",
930 * in case "aff" is a rational expression.
932 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
933 __isl_take isl_val *v)
935 aff = isl_aff_cow(aff);
936 if (!aff)
937 goto error;
939 aff->v = isl_vec_cow(aff->v);
940 if (!aff->v)
941 goto error;
943 if (isl_int_is_one(v->d)) {
944 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
945 } else if (isl_int_eq(aff->v->el[0], v->d)) {
946 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
947 aff->v = isl_vec_normalize(aff->v);
948 if (!aff->v)
949 goto error;
950 } else {
951 isl_seq_scale(aff->v->el + 1,
952 aff->v->el + 1, v->d, aff->v->size - 1);
953 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
954 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
955 aff->v = isl_vec_normalize(aff->v);
956 if (!aff->v)
957 goto error;
960 isl_val_free(v);
961 return aff;
962 error:
963 isl_aff_free(aff);
964 isl_val_free(v);
965 return NULL;
968 /* Return the first argument and free the second.
970 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
971 __isl_take isl_val *v)
973 isl_val_free(v);
974 return aff;
977 /* Replace the first argument by NaN and free the second argument.
979 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
980 __isl_take isl_val *v)
982 isl_val_free(v);
983 return isl_aff_set_nan(aff);
986 /* Add "v" to the constant term of "aff".
988 * A NaN is unaffected by this operation.
989 * Conversely, adding a NaN turns "aff" into a NaN.
991 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
992 __isl_take isl_val *v)
994 isl_bool is_nan, is_zero, is_rat;
996 is_nan = isl_aff_is_nan(aff);
997 is_zero = isl_val_is_zero(v);
998 if (is_nan < 0 || is_zero < 0)
999 goto error;
1000 if (is_nan || is_zero)
1001 return pick_free(aff, v);
1003 is_nan = isl_val_is_nan(v);
1004 is_rat = isl_val_is_rat(v);
1005 if (is_nan < 0 || is_rat < 0)
1006 goto error;
1007 if (is_nan)
1008 return set_nan_free_val(aff, v);
1009 if (!is_rat)
1010 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1011 "expecting rational value or NaN", goto error);
1013 return isl_aff_add_rat_constant_val(aff, v);
1014 error:
1015 isl_aff_free(aff);
1016 isl_val_free(v);
1017 return NULL;
1020 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1022 isl_int t;
1024 isl_int_init(t);
1025 isl_int_set_si(t, v);
1026 aff = isl_aff_add_constant(aff, t);
1027 isl_int_clear(t);
1029 return aff;
1032 /* Add "v" to the numerator of the constant term of "aff".
1034 * A NaN is unaffected by this operation.
1036 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
1038 if (isl_int_is_zero(v))
1039 return aff;
1041 if (!aff)
1042 return NULL;
1043 if (isl_aff_is_nan(aff))
1044 return aff;
1045 aff = isl_aff_cow(aff);
1046 if (!aff)
1047 return NULL;
1049 aff->v = isl_vec_cow(aff->v);
1050 if (!aff->v)
1051 return isl_aff_free(aff);
1053 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1055 return aff;
1058 /* Add "v" to the numerator of the constant term of "aff".
1060 * A NaN is unaffected by this operation.
1062 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1064 isl_int t;
1066 if (v == 0)
1067 return aff;
1069 isl_int_init(t);
1070 isl_int_set_si(t, v);
1071 aff = isl_aff_add_constant_num(aff, t);
1072 isl_int_clear(t);
1074 return aff;
1077 /* Replace the numerator of the constant term of "aff" by "v".
1079 * A NaN is unaffected by this operation.
1081 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1083 if (!aff)
1084 return NULL;
1085 if (isl_aff_is_nan(aff))
1086 return aff;
1087 aff = isl_aff_cow(aff);
1088 if (!aff)
1089 return NULL;
1091 aff->v = isl_vec_cow(aff->v);
1092 if (!aff->v)
1093 return isl_aff_free(aff);
1095 isl_int_set_si(aff->v->el[1], v);
1097 return aff;
1100 /* Replace the numerator of the coefficient of the variable of type "type"
1101 * at position "pos" of "aff" by "v".
1103 * A NaN is unaffected by this operation.
1105 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1106 enum isl_dim_type type, int pos, isl_int v)
1108 if (!aff)
1109 return NULL;
1111 if (type == isl_dim_out)
1112 isl_die(aff->v->ctx, isl_error_invalid,
1113 "output/set dimension does not have a coefficient",
1114 return isl_aff_free(aff));
1115 if (type == isl_dim_in)
1116 type = isl_dim_set;
1118 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1119 return isl_aff_free(aff);
1121 if (isl_aff_is_nan(aff))
1122 return aff;
1123 aff = isl_aff_cow(aff);
1124 if (!aff)
1125 return NULL;
1127 aff->v = isl_vec_cow(aff->v);
1128 if (!aff->v)
1129 return isl_aff_free(aff);
1131 pos += isl_local_space_offset(aff->ls, type);
1132 isl_int_set(aff->v->el[1 + pos], v);
1134 return aff;
1137 /* Replace the numerator of the coefficient of the variable of type "type"
1138 * at position "pos" of "aff" by "v".
1140 * A NaN is unaffected by this operation.
1142 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1143 enum isl_dim_type type, int pos, int v)
1145 if (!aff)
1146 return NULL;
1148 if (type == isl_dim_out)
1149 isl_die(aff->v->ctx, isl_error_invalid,
1150 "output/set dimension does not have a coefficient",
1151 return isl_aff_free(aff));
1152 if (type == isl_dim_in)
1153 type = isl_dim_set;
1155 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1156 return isl_aff_free(aff);
1158 if (isl_aff_is_nan(aff))
1159 return aff;
1160 pos += isl_local_space_offset(aff->ls, type);
1161 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1162 return aff;
1164 aff = isl_aff_cow(aff);
1165 if (!aff)
1166 return NULL;
1168 aff->v = isl_vec_cow(aff->v);
1169 if (!aff->v)
1170 return isl_aff_free(aff);
1172 isl_int_set_si(aff->v->el[1 + pos], v);
1174 return aff;
1177 /* Replace the coefficient of the variable of type "type" at position "pos"
1178 * of "aff" by "v".
1180 * A NaN is unaffected by this operation.
1182 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1183 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1185 if (!aff || !v)
1186 goto error;
1188 if (type == isl_dim_out)
1189 isl_die(aff->v->ctx, isl_error_invalid,
1190 "output/set dimension does not have a coefficient",
1191 goto error);
1192 if (type == isl_dim_in)
1193 type = isl_dim_set;
1195 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1196 return isl_aff_free(aff);
1198 if (isl_aff_is_nan(aff)) {
1199 isl_val_free(v);
1200 return aff;
1202 if (!isl_val_is_rat(v))
1203 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1204 "expecting rational value", goto error);
1206 pos += isl_local_space_offset(aff->ls, type);
1207 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1208 isl_int_eq(aff->v->el[0], v->d)) {
1209 isl_val_free(v);
1210 return aff;
1213 aff = isl_aff_cow(aff);
1214 if (!aff)
1215 goto error;
1216 aff->v = isl_vec_cow(aff->v);
1217 if (!aff->v)
1218 goto error;
1220 if (isl_int_eq(aff->v->el[0], v->d)) {
1221 isl_int_set(aff->v->el[1 + pos], v->n);
1222 } else if (isl_int_is_one(v->d)) {
1223 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1224 } else {
1225 isl_seq_scale(aff->v->el + 1,
1226 aff->v->el + 1, v->d, aff->v->size - 1);
1227 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1228 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1229 aff->v = isl_vec_normalize(aff->v);
1230 if (!aff->v)
1231 goto error;
1234 isl_val_free(v);
1235 return aff;
1236 error:
1237 isl_aff_free(aff);
1238 isl_val_free(v);
1239 return NULL;
1242 /* Add "v" to the coefficient of the variable of type "type"
1243 * at position "pos" of "aff".
1245 * A NaN is unaffected by this operation.
1247 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1248 enum isl_dim_type type, int pos, isl_int v)
1250 if (!aff)
1251 return NULL;
1253 if (type == isl_dim_out)
1254 isl_die(aff->v->ctx, isl_error_invalid,
1255 "output/set dimension does not have a coefficient",
1256 return isl_aff_free(aff));
1257 if (type == isl_dim_in)
1258 type = isl_dim_set;
1260 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1261 return isl_aff_free(aff);
1263 if (isl_aff_is_nan(aff))
1264 return aff;
1265 aff = isl_aff_cow(aff);
1266 if (!aff)
1267 return NULL;
1269 aff->v = isl_vec_cow(aff->v);
1270 if (!aff->v)
1271 return isl_aff_free(aff);
1273 pos += isl_local_space_offset(aff->ls, type);
1274 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1276 return aff;
1279 /* Add "v" to the coefficient of the variable of type "type"
1280 * at position "pos" of "aff".
1282 * A NaN is unaffected by this operation.
1284 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1285 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1287 if (!aff || !v)
1288 goto error;
1290 if (isl_val_is_zero(v)) {
1291 isl_val_free(v);
1292 return aff;
1295 if (type == isl_dim_out)
1296 isl_die(aff->v->ctx, isl_error_invalid,
1297 "output/set dimension does not have a coefficient",
1298 goto error);
1299 if (type == isl_dim_in)
1300 type = isl_dim_set;
1302 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1303 goto error;
1305 if (isl_aff_is_nan(aff)) {
1306 isl_val_free(v);
1307 return aff;
1309 if (!isl_val_is_rat(v))
1310 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1311 "expecting rational value", goto error);
1313 aff = isl_aff_cow(aff);
1314 if (!aff)
1315 goto error;
1317 aff->v = isl_vec_cow(aff->v);
1318 if (!aff->v)
1319 goto error;
1321 pos += isl_local_space_offset(aff->ls, type);
1322 if (isl_int_is_one(v->d)) {
1323 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1324 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1325 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1326 aff->v = isl_vec_normalize(aff->v);
1327 if (!aff->v)
1328 goto error;
1329 } else {
1330 isl_seq_scale(aff->v->el + 1,
1331 aff->v->el + 1, v->d, aff->v->size - 1);
1332 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1333 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1334 aff->v = isl_vec_normalize(aff->v);
1335 if (!aff->v)
1336 goto error;
1339 isl_val_free(v);
1340 return aff;
1341 error:
1342 isl_aff_free(aff);
1343 isl_val_free(v);
1344 return NULL;
1347 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1348 enum isl_dim_type type, int pos, int v)
1350 isl_int t;
1352 isl_int_init(t);
1353 isl_int_set_si(t, v);
1354 aff = isl_aff_add_coefficient(aff, type, pos, t);
1355 isl_int_clear(t);
1357 return aff;
1360 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1362 if (!aff)
1363 return NULL;
1365 return isl_local_space_get_div(aff->ls, pos);
1368 /* Return the negation of "aff".
1370 * As a special case, -NaN = NaN.
1372 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1374 if (!aff)
1375 return NULL;
1376 if (isl_aff_is_nan(aff))
1377 return aff;
1378 aff = isl_aff_cow(aff);
1379 if (!aff)
1380 return NULL;
1381 aff->v = isl_vec_cow(aff->v);
1382 if (!aff->v)
1383 return isl_aff_free(aff);
1385 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1387 return aff;
1390 /* Remove divs from the local space that do not appear in the affine
1391 * expression.
1392 * We currently only remove divs at the end.
1393 * Some intermediate divs may also not appear directly in the affine
1394 * expression, but we would also need to check that no other divs are
1395 * defined in terms of them.
1397 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1399 int pos;
1400 isl_size off;
1401 isl_size n;
1403 n = isl_aff_domain_dim(aff, isl_dim_div);
1404 off = isl_aff_domain_offset(aff, isl_dim_div);
1405 if (n < 0 || off < 0)
1406 return isl_aff_free(aff);
1408 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1409 if (pos == n)
1410 return aff;
1412 aff = isl_aff_cow(aff);
1413 if (!aff)
1414 return NULL;
1416 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1417 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1418 if (!aff->ls || !aff->v)
1419 return isl_aff_free(aff);
1421 return aff;
1424 /* Look for any divs in the aff->ls with a denominator equal to one
1425 * and plug them into the affine expression and any subsequent divs
1426 * that may reference the div.
1428 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1430 int i;
1431 isl_size n;
1432 int len;
1433 isl_int v;
1434 isl_vec *vec;
1435 isl_local_space *ls;
1436 isl_size off;
1438 n = isl_aff_domain_dim(aff, isl_dim_div);
1439 off = isl_aff_domain_offset(aff, isl_dim_div);
1440 if (n < 0 || off < 0)
1441 return isl_aff_free(aff);
1442 len = aff->v->size;
1443 for (i = 0; i < n; ++i) {
1444 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1445 continue;
1446 ls = isl_local_space_copy(aff->ls);
1447 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1448 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1449 vec = isl_vec_copy(aff->v);
1450 vec = isl_vec_cow(vec);
1451 if (!ls || !vec)
1452 goto error;
1454 isl_int_init(v);
1456 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1457 len, len, v);
1459 isl_int_clear(v);
1461 isl_vec_free(aff->v);
1462 aff->v = vec;
1463 isl_local_space_free(aff->ls);
1464 aff->ls = ls;
1467 return aff;
1468 error:
1469 isl_vec_free(vec);
1470 isl_local_space_free(ls);
1471 return isl_aff_free(aff);
1474 /* Look for any divs j that appear with a unit coefficient inside
1475 * the definitions of other divs i and plug them into the definitions
1476 * of the divs i.
1478 * In particular, an expression of the form
1480 * floor((f(..) + floor(g(..)/n))/m)
1482 * is simplified to
1484 * floor((n * f(..) + g(..))/(n * m))
1486 * This simplification is correct because we can move the expression
1487 * f(..) into the inner floor in the original expression to obtain
1489 * floor(floor((n * f(..) + g(..))/n)/m)
1491 * from which we can derive the simplified expression.
1493 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1495 int i, j;
1496 isl_size n;
1497 isl_size off;
1499 n = isl_aff_domain_dim(aff, isl_dim_div);
1500 off = isl_aff_domain_offset(aff, isl_dim_div);
1501 if (n < 0 || off < 0)
1502 return isl_aff_free(aff);
1503 for (i = 1; i < n; ++i) {
1504 for (j = 0; j < i; ++j) {
1505 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1506 continue;
1507 aff->ls = isl_local_space_substitute_seq(aff->ls,
1508 isl_dim_div, j, aff->ls->div->row[j],
1509 aff->v->size, i, 1);
1510 if (!aff->ls)
1511 return isl_aff_free(aff);
1515 return aff;
1518 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1520 * Even though this function is only called on isl_affs with a single
1521 * reference, we are careful to only change aff->v and aff->ls together.
1523 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1525 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1526 isl_local_space *ls;
1527 isl_vec *v;
1529 if (off < 0)
1530 return isl_aff_free(aff);
1532 ls = isl_local_space_copy(aff->ls);
1533 ls = isl_local_space_swap_div(ls, a, b);
1534 v = isl_vec_copy(aff->v);
1535 v = isl_vec_cow(v);
1536 if (!ls || !v)
1537 goto error;
1539 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1540 isl_vec_free(aff->v);
1541 aff->v = v;
1542 isl_local_space_free(aff->ls);
1543 aff->ls = ls;
1545 return aff;
1546 error:
1547 isl_vec_free(v);
1548 isl_local_space_free(ls);
1549 return isl_aff_free(aff);
1552 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1554 * We currently do not actually remove div "b", but simply add its
1555 * coefficient to that of "a" and then zero it out.
1557 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1559 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1561 if (off < 0)
1562 return isl_aff_free(aff);
1564 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1565 return aff;
1567 aff->v = isl_vec_cow(aff->v);
1568 if (!aff->v)
1569 return isl_aff_free(aff);
1571 isl_int_add(aff->v->el[1 + off + a],
1572 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1573 isl_int_set_si(aff->v->el[1 + off + b], 0);
1575 return aff;
1578 /* Sort the divs in the local space of "aff" according to
1579 * the comparison function "cmp_row" in isl_local_space.c,
1580 * combining the coefficients of identical divs.
1582 * Reordering divs does not change the semantics of "aff",
1583 * so there is no need to call isl_aff_cow.
1584 * Moreover, this function is currently only called on isl_affs
1585 * with a single reference.
1587 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1589 isl_size n;
1590 int i, j;
1592 n = isl_aff_dim(aff, isl_dim_div);
1593 if (n < 0)
1594 return isl_aff_free(aff);
1595 for (i = 1; i < n; ++i) {
1596 for (j = i - 1; j >= 0; --j) {
1597 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1598 if (cmp < 0)
1599 break;
1600 if (cmp == 0)
1601 aff = merge_divs(aff, j, j + 1);
1602 else
1603 aff = swap_div(aff, j, j + 1);
1604 if (!aff)
1605 return NULL;
1609 return aff;
1612 /* Normalize the representation of "aff".
1614 * This function should only be called on "new" isl_affs, i.e.,
1615 * with only a single reference. We therefore do not need to
1616 * worry about affecting other instances.
1618 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1620 if (!aff)
1621 return NULL;
1622 aff->v = isl_vec_normalize(aff->v);
1623 if (!aff->v)
1624 return isl_aff_free(aff);
1625 aff = plug_in_integral_divs(aff);
1626 aff = plug_in_unit_divs(aff);
1627 aff = sort_divs(aff);
1628 aff = isl_aff_remove_unused_divs(aff);
1629 return aff;
1632 /* Given f, return floor(f).
1633 * If f is an integer expression, then just return f.
1634 * If f is a constant, then return the constant floor(f).
1635 * Otherwise, if f = g/m, write g = q m + r,
1636 * create a new div d = [r/m] and return the expression q + d.
1637 * The coefficients in r are taken to lie between -m/2 and m/2.
1639 * reduce_div_coefficients performs the same normalization.
1641 * As a special case, floor(NaN) = NaN.
1643 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1645 int i;
1646 int size;
1647 isl_ctx *ctx;
1648 isl_vec *div;
1650 if (!aff)
1651 return NULL;
1653 if (isl_aff_is_nan(aff))
1654 return aff;
1655 if (isl_int_is_one(aff->v->el[0]))
1656 return aff;
1658 aff = isl_aff_cow(aff);
1659 if (!aff)
1660 return NULL;
1662 aff->v = isl_vec_cow(aff->v);
1663 if (!aff->v)
1664 return isl_aff_free(aff);
1666 if (isl_aff_is_cst(aff)) {
1667 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1668 isl_int_set_si(aff->v->el[0], 1);
1669 return aff;
1672 div = isl_vec_copy(aff->v);
1673 div = isl_vec_cow(div);
1674 if (!div)
1675 return isl_aff_free(aff);
1677 ctx = isl_aff_get_ctx(aff);
1678 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1679 for (i = 1; i < aff->v->size; ++i) {
1680 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1681 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1682 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1683 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1684 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1688 aff->ls = isl_local_space_add_div(aff->ls, div);
1689 if (!aff->ls)
1690 return isl_aff_free(aff);
1692 size = aff->v->size;
1693 aff->v = isl_vec_extend(aff->v, size + 1);
1694 if (!aff->v)
1695 return isl_aff_free(aff);
1696 isl_int_set_si(aff->v->el[0], 1);
1697 isl_int_set_si(aff->v->el[size], 1);
1699 aff = isl_aff_normalize(aff);
1701 return aff;
1704 /* Compute
1706 * aff mod m = aff - m * floor(aff/m)
1708 * with m an integer value.
1710 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1711 __isl_take isl_val *m)
1713 isl_aff *res;
1715 if (!aff || !m)
1716 goto error;
1718 if (!isl_val_is_int(m))
1719 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1720 "expecting integer modulo", goto error);
1722 res = isl_aff_copy(aff);
1723 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1724 aff = isl_aff_floor(aff);
1725 aff = isl_aff_scale_val(aff, m);
1726 res = isl_aff_sub(res, aff);
1728 return res;
1729 error:
1730 isl_aff_free(aff);
1731 isl_val_free(m);
1732 return NULL;
1735 /* Compute
1737 * pwaff mod m = pwaff - m * floor(pwaff/m)
1739 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1741 isl_pw_aff *res;
1743 res = isl_pw_aff_copy(pwaff);
1744 pwaff = isl_pw_aff_scale_down(pwaff, m);
1745 pwaff = isl_pw_aff_floor(pwaff);
1746 pwaff = isl_pw_aff_scale(pwaff, m);
1747 res = isl_pw_aff_sub(res, pwaff);
1749 return res;
1752 /* Compute
1754 * pa mod m = pa - m * floor(pa/m)
1756 * with m an integer value.
1758 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1759 __isl_take isl_val *m)
1761 if (!pa || !m)
1762 goto error;
1763 if (!isl_val_is_int(m))
1764 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1765 "expecting integer modulo", goto error);
1766 pa = isl_pw_aff_mod(pa, m->n);
1767 isl_val_free(m);
1768 return pa;
1769 error:
1770 isl_pw_aff_free(pa);
1771 isl_val_free(m);
1772 return NULL;
1775 /* Given f, return ceil(f).
1776 * If f is an integer expression, then just return f.
1777 * Otherwise, let f be the expression
1779 * e/m
1781 * then return
1783 * floor((e + m - 1)/m)
1785 * As a special case, ceil(NaN) = NaN.
1787 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1789 if (!aff)
1790 return NULL;
1792 if (isl_aff_is_nan(aff))
1793 return aff;
1794 if (isl_int_is_one(aff->v->el[0]))
1795 return aff;
1797 aff = isl_aff_cow(aff);
1798 if (!aff)
1799 return NULL;
1800 aff->v = isl_vec_cow(aff->v);
1801 if (!aff->v)
1802 return isl_aff_free(aff);
1804 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1805 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1806 aff = isl_aff_floor(aff);
1808 return aff;
1811 /* Apply the expansion computed by isl_merge_divs.
1812 * The expansion itself is given by "exp" while the resulting
1813 * list of divs is given by "div".
1815 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1816 __isl_take isl_mat *div, int *exp)
1818 isl_size old_n_div;
1819 isl_size new_n_div;
1820 isl_size offset;
1822 aff = isl_aff_cow(aff);
1824 offset = isl_aff_domain_offset(aff, isl_dim_div);
1825 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1826 new_n_div = isl_mat_rows(div);
1827 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1828 goto error;
1830 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1831 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1832 if (!aff->v || !aff->ls)
1833 return isl_aff_free(aff);
1834 return aff;
1835 error:
1836 isl_aff_free(aff);
1837 isl_mat_free(div);
1838 return NULL;
1841 /* Add two affine expressions that live in the same local space.
1843 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1844 __isl_take isl_aff *aff2)
1846 isl_int gcd, f;
1848 aff1 = isl_aff_cow(aff1);
1849 if (!aff1 || !aff2)
1850 goto error;
1852 aff1->v = isl_vec_cow(aff1->v);
1853 if (!aff1->v)
1854 goto error;
1856 isl_int_init(gcd);
1857 isl_int_init(f);
1858 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1859 isl_int_divexact(f, aff2->v->el[0], gcd);
1860 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1861 isl_int_divexact(f, aff1->v->el[0], gcd);
1862 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1863 isl_int_divexact(f, aff2->v->el[0], gcd);
1864 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1865 isl_int_clear(f);
1866 isl_int_clear(gcd);
1868 isl_aff_free(aff2);
1869 aff1 = isl_aff_normalize(aff1);
1870 return aff1;
1871 error:
1872 isl_aff_free(aff1);
1873 isl_aff_free(aff2);
1874 return NULL;
1877 /* Replace one of the arguments by a NaN and free the other one.
1879 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1880 __isl_take isl_aff *aff2)
1882 isl_aff_free(aff2);
1883 return isl_aff_set_nan(aff1);
1886 /* Return the sum of "aff1" and "aff2".
1888 * If either of the two is NaN, then the result is NaN.
1890 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1891 __isl_take isl_aff *aff2)
1893 isl_ctx *ctx;
1894 int *exp1 = NULL;
1895 int *exp2 = NULL;
1896 isl_mat *div;
1897 isl_size n_div1, n_div2;
1899 if (!aff1 || !aff2)
1900 goto error;
1902 ctx = isl_aff_get_ctx(aff1);
1903 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1904 isl_die(ctx, isl_error_invalid,
1905 "spaces don't match", goto error);
1907 if (isl_aff_is_nan(aff1)) {
1908 isl_aff_free(aff2);
1909 return aff1;
1911 if (isl_aff_is_nan(aff2)) {
1912 isl_aff_free(aff1);
1913 return aff2;
1916 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1917 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1918 if (n_div1 < 0 || n_div2 < 0)
1919 goto error;
1920 if (n_div1 == 0 && n_div2 == 0)
1921 return add_expanded(aff1, aff2);
1923 exp1 = isl_alloc_array(ctx, int, n_div1);
1924 exp2 = isl_alloc_array(ctx, int, n_div2);
1925 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1926 goto error;
1928 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1929 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1930 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1931 free(exp1);
1932 free(exp2);
1934 return add_expanded(aff1, aff2);
1935 error:
1936 free(exp1);
1937 free(exp2);
1938 isl_aff_free(aff1);
1939 isl_aff_free(aff2);
1940 return NULL;
1943 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1944 __isl_take isl_aff *aff2)
1946 return isl_aff_add(aff1, isl_aff_neg(aff2));
1949 /* Return the result of scaling "aff" by a factor of "f".
1951 * As a special case, f * NaN = NaN.
1953 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1955 isl_int gcd;
1957 if (!aff)
1958 return NULL;
1959 if (isl_aff_is_nan(aff))
1960 return aff;
1962 if (isl_int_is_one(f))
1963 return aff;
1965 aff = isl_aff_cow(aff);
1966 if (!aff)
1967 return NULL;
1968 aff->v = isl_vec_cow(aff->v);
1969 if (!aff->v)
1970 return isl_aff_free(aff);
1972 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1973 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1974 return aff;
1977 isl_int_init(gcd);
1978 isl_int_gcd(gcd, aff->v->el[0], f);
1979 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1980 isl_int_divexact(gcd, f, gcd);
1981 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1982 isl_int_clear(gcd);
1984 return aff;
1987 /* Multiple "aff" by "v".
1989 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1990 __isl_take isl_val *v)
1992 if (!aff || !v)
1993 goto error;
1995 if (isl_val_is_one(v)) {
1996 isl_val_free(v);
1997 return aff;
2000 if (!isl_val_is_rat(v))
2001 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2002 "expecting rational factor", goto error);
2004 aff = isl_aff_scale(aff, v->n);
2005 aff = isl_aff_scale_down(aff, v->d);
2007 isl_val_free(v);
2008 return aff;
2009 error:
2010 isl_aff_free(aff);
2011 isl_val_free(v);
2012 return NULL;
2015 /* Return the result of scaling "aff" down by a factor of "f".
2017 * As a special case, NaN/f = NaN.
2019 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2021 isl_int gcd;
2023 if (!aff)
2024 return NULL;
2025 if (isl_aff_is_nan(aff))
2026 return aff;
2028 if (isl_int_is_one(f))
2029 return aff;
2031 aff = isl_aff_cow(aff);
2032 if (!aff)
2033 return NULL;
2035 if (isl_int_is_zero(f))
2036 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2037 "cannot scale down by zero", return isl_aff_free(aff));
2039 aff->v = isl_vec_cow(aff->v);
2040 if (!aff->v)
2041 return isl_aff_free(aff);
2043 isl_int_init(gcd);
2044 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2045 isl_int_gcd(gcd, gcd, f);
2046 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2047 isl_int_divexact(gcd, f, gcd);
2048 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2049 isl_int_clear(gcd);
2051 return aff;
2054 /* Divide "aff" by "v".
2056 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2057 __isl_take isl_val *v)
2059 if (!aff || !v)
2060 goto error;
2062 if (isl_val_is_one(v)) {
2063 isl_val_free(v);
2064 return aff;
2067 if (!isl_val_is_rat(v))
2068 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2069 "expecting rational factor", goto error);
2070 if (!isl_val_is_pos(v))
2071 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2072 "factor needs to be positive", goto error);
2074 aff = isl_aff_scale(aff, v->d);
2075 aff = isl_aff_scale_down(aff, v->n);
2077 isl_val_free(v);
2078 return aff;
2079 error:
2080 isl_aff_free(aff);
2081 isl_val_free(v);
2082 return NULL;
2085 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2087 isl_int v;
2089 if (f == 1)
2090 return aff;
2092 isl_int_init(v);
2093 isl_int_set_ui(v, f);
2094 aff = isl_aff_scale_down(aff, v);
2095 isl_int_clear(v);
2097 return aff;
2100 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2101 enum isl_dim_type type, unsigned pos, const char *s)
2103 aff = isl_aff_cow(aff);
2104 if (!aff)
2105 return NULL;
2106 if (type == isl_dim_out)
2107 isl_die(aff->v->ctx, isl_error_invalid,
2108 "cannot set name of output/set dimension",
2109 return isl_aff_free(aff));
2110 if (type == isl_dim_in)
2111 type = isl_dim_set;
2112 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2113 if (!aff->ls)
2114 return isl_aff_free(aff);
2116 return aff;
2119 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2120 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2122 aff = isl_aff_cow(aff);
2123 if (!aff)
2124 goto error;
2125 if (type == isl_dim_out)
2126 isl_die(aff->v->ctx, isl_error_invalid,
2127 "cannot set name of output/set dimension",
2128 goto error);
2129 if (type == isl_dim_in)
2130 type = isl_dim_set;
2131 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2132 if (!aff->ls)
2133 return isl_aff_free(aff);
2135 return aff;
2136 error:
2137 isl_id_free(id);
2138 isl_aff_free(aff);
2139 return NULL;
2142 /* Replace the identifier of the input tuple of "aff" by "id".
2143 * type is currently required to be equal to isl_dim_in
2145 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2146 enum isl_dim_type type, __isl_take isl_id *id)
2148 aff = isl_aff_cow(aff);
2149 if (!aff)
2150 goto error;
2151 if (type != isl_dim_in)
2152 isl_die(aff->v->ctx, isl_error_invalid,
2153 "cannot only set id of input tuple", goto error);
2154 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2155 if (!aff->ls)
2156 return isl_aff_free(aff);
2158 return aff;
2159 error:
2160 isl_id_free(id);
2161 isl_aff_free(aff);
2162 return NULL;
2165 /* Exploit the equalities in "eq" to simplify the affine expression
2166 * and the expressions of the integer divisions in the local space.
2167 * The integer divisions in this local space are assumed to appear
2168 * as regular dimensions in "eq".
2170 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2171 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2173 int i, j;
2174 unsigned o_div;
2175 unsigned n_div;
2177 if (!eq)
2178 goto error;
2179 if (eq->n_eq == 0) {
2180 isl_basic_set_free(eq);
2181 return aff;
2184 aff = isl_aff_cow(aff);
2185 if (!aff)
2186 goto error;
2188 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2189 isl_basic_set_copy(eq));
2190 aff->v = isl_vec_cow(aff->v);
2191 if (!aff->ls || !aff->v)
2192 goto error;
2194 o_div = isl_basic_set_offset(eq, isl_dim_div);
2195 n_div = eq->n_div;
2196 for (i = 0; i < eq->n_eq; ++i) {
2197 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2198 if (j < 0 || j == 0 || j >= o_div)
2199 continue;
2201 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2202 &aff->v->el[0]);
2205 isl_basic_set_free(eq);
2206 aff = isl_aff_normalize(aff);
2207 return aff;
2208 error:
2209 isl_basic_set_free(eq);
2210 isl_aff_free(aff);
2211 return NULL;
2214 /* Exploit the equalities in "eq" to simplify the affine expression
2215 * and the expressions of the integer divisions in the local space.
2217 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2218 __isl_take isl_basic_set *eq)
2220 isl_size n_div;
2222 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2223 if (n_div < 0)
2224 goto error;
2225 if (n_div > 0)
2226 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2227 return isl_aff_substitute_equalities_lifted(aff, eq);
2228 error:
2229 isl_basic_set_free(eq);
2230 isl_aff_free(aff);
2231 return NULL;
2234 /* Look for equalities among the variables shared by context and aff
2235 * and the integer divisions of aff, if any.
2236 * The equalities are then used to eliminate coefficients and/or integer
2237 * divisions from aff.
2239 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2240 __isl_take isl_set *context)
2242 isl_local_space *ls;
2243 isl_basic_set *hull;
2245 ls = isl_aff_get_domain_local_space(aff);
2246 context = isl_local_space_lift_set(ls, context);
2248 hull = isl_set_affine_hull(context);
2249 return isl_aff_substitute_equalities_lifted(aff, hull);
2252 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2253 __isl_take isl_set *context)
2255 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2256 dom_context = isl_set_intersect_params(dom_context, context);
2257 return isl_aff_gist(aff, dom_context);
2260 /* Return a basic set containing those elements in the space
2261 * of aff where it is positive. "rational" should not be set.
2263 * If "aff" is NaN, then it is not positive.
2265 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2266 int rational, void *user)
2268 isl_constraint *ineq;
2269 isl_basic_set *bset;
2270 isl_val *c;
2272 if (!aff)
2273 return NULL;
2274 if (isl_aff_is_nan(aff)) {
2275 isl_space *space = isl_aff_get_domain_space(aff);
2276 isl_aff_free(aff);
2277 return isl_basic_set_empty(space);
2279 if (rational)
2280 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2281 "rational sets not supported", goto error);
2283 ineq = isl_inequality_from_aff(aff);
2284 c = isl_constraint_get_constant_val(ineq);
2285 c = isl_val_sub_ui(c, 1);
2286 ineq = isl_constraint_set_constant_val(ineq, c);
2288 bset = isl_basic_set_from_constraint(ineq);
2289 bset = isl_basic_set_simplify(bset);
2290 return bset;
2291 error:
2292 isl_aff_free(aff);
2293 return NULL;
2296 /* Return a basic set containing those elements in the space
2297 * of aff where it is non-negative.
2298 * If "rational" is set, then return a rational basic set.
2300 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2302 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2303 __isl_take isl_aff *aff, int rational, void *user)
2305 isl_constraint *ineq;
2306 isl_basic_set *bset;
2308 if (!aff)
2309 return NULL;
2310 if (isl_aff_is_nan(aff)) {
2311 isl_space *space = isl_aff_get_domain_space(aff);
2312 isl_aff_free(aff);
2313 return isl_basic_set_empty(space);
2316 ineq = isl_inequality_from_aff(aff);
2318 bset = isl_basic_set_from_constraint(ineq);
2319 if (rational)
2320 bset = isl_basic_set_set_rational(bset);
2321 bset = isl_basic_set_simplify(bset);
2322 return bset;
2325 /* Return a basic set containing those elements in the space
2326 * of aff where it is non-negative.
2328 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2330 return aff_nonneg_basic_set(aff, 0, NULL);
2333 /* Return a basic set containing those elements in the domain space
2334 * of "aff" where it is positive.
2336 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2338 aff = isl_aff_add_constant_num_si(aff, -1);
2339 return isl_aff_nonneg_basic_set(aff);
2342 /* Return a basic set containing those elements in the domain space
2343 * of aff where it is negative.
2345 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2347 aff = isl_aff_neg(aff);
2348 return isl_aff_pos_basic_set(aff);
2351 /* Return a basic set containing those elements in the space
2352 * of aff where it is zero.
2353 * If "rational" is set, then return a rational basic set.
2355 * If "aff" is NaN, then it is not zero.
2357 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2358 int rational, void *user)
2360 isl_constraint *ineq;
2361 isl_basic_set *bset;
2363 if (!aff)
2364 return NULL;
2365 if (isl_aff_is_nan(aff)) {
2366 isl_space *space = isl_aff_get_domain_space(aff);
2367 isl_aff_free(aff);
2368 return isl_basic_set_empty(space);
2371 ineq = isl_equality_from_aff(aff);
2373 bset = isl_basic_set_from_constraint(ineq);
2374 if (rational)
2375 bset = isl_basic_set_set_rational(bset);
2376 bset = isl_basic_set_simplify(bset);
2377 return bset;
2380 /* Return a basic set containing those elements in the space
2381 * of aff where it is zero.
2383 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2385 return aff_zero_basic_set(aff, 0, NULL);
2388 /* Return a basic set containing those elements in the shared space
2389 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2391 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2392 __isl_take isl_aff *aff2)
2394 aff1 = isl_aff_sub(aff1, aff2);
2396 return isl_aff_nonneg_basic_set(aff1);
2399 /* Return a basic set containing those elements in the shared domain space
2400 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2402 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2403 __isl_take isl_aff *aff2)
2405 aff1 = isl_aff_sub(aff1, aff2);
2407 return isl_aff_pos_basic_set(aff1);
2410 /* Return a set containing those elements in the shared space
2411 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2413 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2414 __isl_take isl_aff *aff2)
2416 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2419 /* Return a set containing those elements in the shared domain space
2420 * of aff1 and aff2 where aff1 is greater than aff2.
2422 * If either of the two inputs is NaN, then the result is empty,
2423 * as comparisons with NaN always return false.
2425 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2426 __isl_take isl_aff *aff2)
2428 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2431 /* Return a basic set containing those elements in the shared space
2432 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2434 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2435 __isl_take isl_aff *aff2)
2437 return isl_aff_ge_basic_set(aff2, aff1);
2440 /* Return a basic set containing those elements in the shared domain space
2441 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2443 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2444 __isl_take isl_aff *aff2)
2446 return isl_aff_gt_basic_set(aff2, aff1);
2449 /* Return a set containing those elements in the shared space
2450 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2452 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2453 __isl_take isl_aff *aff2)
2455 return isl_aff_ge_set(aff2, aff1);
2458 /* Return a set containing those elements in the shared domain space
2459 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2461 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2462 __isl_take isl_aff *aff2)
2464 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2467 /* Return a basic set containing those elements in the shared space
2468 * of aff1 and aff2 where aff1 and aff2 are equal.
2470 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2471 __isl_take isl_aff *aff2)
2473 aff1 = isl_aff_sub(aff1, aff2);
2475 return isl_aff_zero_basic_set(aff1);
2478 /* Return a set containing those elements in the shared space
2479 * of aff1 and aff2 where aff1 and aff2 are equal.
2481 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2482 __isl_take isl_aff *aff2)
2484 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2487 /* Return a set containing those elements in the shared domain space
2488 * of aff1 and aff2 where aff1 and aff2 are not equal.
2490 * If either of the two inputs is NaN, then the result is empty,
2491 * as comparisons with NaN always return false.
2493 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2494 __isl_take isl_aff *aff2)
2496 isl_set *set_lt, *set_gt;
2498 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2499 isl_aff_copy(aff2));
2500 set_gt = isl_aff_gt_set(aff1, aff2);
2501 return isl_set_union_disjoint(set_lt, set_gt);
2504 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2505 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2507 aff1 = isl_aff_add(aff1, aff2);
2508 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2509 return aff1;
2512 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2514 if (!aff)
2515 return isl_bool_error;
2517 return isl_bool_false;
2520 #undef TYPE
2521 #define TYPE isl_aff
2522 static
2523 #include "check_type_range_templ.c"
2525 /* Check whether the given affine expression has non-zero coefficient
2526 * for any dimension in the given range or if any of these dimensions
2527 * appear with non-zero coefficients in any of the integer divisions
2528 * involved in the affine expression.
2530 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2531 enum isl_dim_type type, unsigned first, unsigned n)
2533 int i;
2534 int *active = NULL;
2535 isl_bool involves = isl_bool_false;
2537 if (!aff)
2538 return isl_bool_error;
2539 if (n == 0)
2540 return isl_bool_false;
2541 if (isl_aff_check_range(aff, type, first, n) < 0)
2542 return isl_bool_error;
2544 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2545 if (!active)
2546 goto error;
2548 first += isl_local_space_offset(aff->ls, type) - 1;
2549 for (i = 0; i < n; ++i)
2550 if (active[first + i]) {
2551 involves = isl_bool_true;
2552 break;
2555 free(active);
2557 return involves;
2558 error:
2559 free(active);
2560 return isl_bool_error;
2563 /* Does "aff" involve any local variables, i.e., integer divisions?
2565 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2567 isl_size n;
2569 n = isl_aff_dim(aff, isl_dim_div);
2570 if (n < 0)
2571 return isl_bool_error;
2572 return isl_bool_ok(n > 0);
2575 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2576 enum isl_dim_type type, unsigned first, unsigned n)
2578 isl_ctx *ctx;
2580 if (!aff)
2581 return NULL;
2582 if (type == isl_dim_out)
2583 isl_die(aff->v->ctx, isl_error_invalid,
2584 "cannot drop output/set dimension",
2585 return isl_aff_free(aff));
2586 if (type == isl_dim_in)
2587 type = isl_dim_set;
2588 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2589 return aff;
2591 ctx = isl_aff_get_ctx(aff);
2592 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2593 return isl_aff_free(aff);
2595 aff = isl_aff_cow(aff);
2596 if (!aff)
2597 return NULL;
2599 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2600 if (!aff->ls)
2601 return isl_aff_free(aff);
2603 first += 1 + isl_local_space_offset(aff->ls, type);
2604 aff->v = isl_vec_drop_els(aff->v, first, n);
2605 if (!aff->v)
2606 return isl_aff_free(aff);
2608 return aff;
2611 /* Is the domain of "aff" a product?
2613 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2615 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2618 #undef TYPE
2619 #define TYPE isl_aff
2620 #include <isl_domain_factor_templ.c>
2622 /* Project the domain of the affine expression onto its parameter space.
2623 * The affine expression may not involve any of the domain dimensions.
2625 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2627 isl_space *space;
2628 isl_size n;
2630 n = isl_aff_dim(aff, isl_dim_in);
2631 if (n < 0)
2632 return isl_aff_free(aff);
2633 aff = isl_aff_drop_domain(aff, 0, n);
2634 space = isl_aff_get_domain_space(aff);
2635 space = isl_space_params(space);
2636 aff = isl_aff_reset_domain_space(aff, space);
2637 return aff;
2640 /* Convert an affine expression defined over a parameter domain
2641 * into one that is defined over a zero-dimensional set.
2643 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2645 isl_local_space *ls;
2647 ls = isl_aff_take_domain_local_space(aff);
2648 ls = isl_local_space_set_from_params(ls);
2649 aff = isl_aff_restore_domain_local_space(aff, ls);
2651 return aff;
2654 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2655 enum isl_dim_type type, unsigned first, unsigned n)
2657 isl_ctx *ctx;
2659 if (!aff)
2660 return NULL;
2661 if (type == isl_dim_out)
2662 isl_die(aff->v->ctx, isl_error_invalid,
2663 "cannot insert output/set dimensions",
2664 return isl_aff_free(aff));
2665 if (type == isl_dim_in)
2666 type = isl_dim_set;
2667 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2668 return aff;
2670 ctx = isl_aff_get_ctx(aff);
2671 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2672 return isl_aff_free(aff);
2674 aff = isl_aff_cow(aff);
2675 if (!aff)
2676 return NULL;
2678 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2679 if (!aff->ls)
2680 return isl_aff_free(aff);
2682 first += 1 + isl_local_space_offset(aff->ls, type);
2683 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2684 if (!aff->v)
2685 return isl_aff_free(aff);
2687 return aff;
2690 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2691 enum isl_dim_type type, unsigned n)
2693 isl_size pos;
2695 pos = isl_aff_dim(aff, type);
2696 if (pos < 0)
2697 return isl_aff_free(aff);
2699 return isl_aff_insert_dims(aff, type, pos, n);
2702 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2703 * to dimensions of "dst_type" at "dst_pos".
2705 * We only support moving input dimensions to parameters and vice versa.
2707 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2708 enum isl_dim_type dst_type, unsigned dst_pos,
2709 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2711 unsigned g_dst_pos;
2712 unsigned g_src_pos;
2713 isl_size src_off, dst_off;
2715 if (!aff)
2716 return NULL;
2717 if (n == 0 &&
2718 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2719 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2720 return aff;
2722 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2723 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2724 "cannot move output/set dimension",
2725 return isl_aff_free(aff));
2726 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2727 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2728 "cannot move divs", return isl_aff_free(aff));
2729 if (dst_type == isl_dim_in)
2730 dst_type = isl_dim_set;
2731 if (src_type == isl_dim_in)
2732 src_type = isl_dim_set;
2734 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2735 return isl_aff_free(aff);
2736 if (dst_type == src_type)
2737 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2738 "moving dims within the same type not supported",
2739 return isl_aff_free(aff));
2741 aff = isl_aff_cow(aff);
2742 src_off = isl_aff_domain_offset(aff, src_type);
2743 dst_off = isl_aff_domain_offset(aff, dst_type);
2744 if (src_off < 0 || dst_off < 0)
2745 return isl_aff_free(aff);
2747 g_src_pos = 1 + src_off + src_pos;
2748 g_dst_pos = 1 + dst_off + dst_pos;
2749 if (dst_type > src_type)
2750 g_dst_pos -= n;
2752 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2753 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2754 src_type, src_pos, n);
2755 if (!aff->v || !aff->ls)
2756 return isl_aff_free(aff);
2758 aff = sort_divs(aff);
2760 return aff;
2763 /* Return a zero isl_aff in the given space.
2765 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2766 * interface over all piecewise types.
2768 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2770 isl_local_space *ls;
2772 ls = isl_local_space_from_space(isl_space_domain(space));
2773 return isl_aff_zero_on_domain(ls);
2776 #define isl_aff_involves_nan isl_aff_is_nan
2778 #undef PW
2779 #define PW isl_pw_aff
2780 #undef BASE
2781 #define BASE aff
2782 #undef EL_IS_ZERO
2783 #define EL_IS_ZERO is_empty
2784 #undef ZERO
2785 #define ZERO empty
2786 #undef IS_ZERO
2787 #define IS_ZERO is_empty
2788 #undef FIELD
2789 #define FIELD aff
2790 #undef DEFAULT_IS_ZERO
2791 #define DEFAULT_IS_ZERO 0
2793 #include <isl_pw_templ.c>
2794 #include <isl_pw_add_constant_val_templ.c>
2795 #include <isl_pw_bind_domain_templ.c>
2796 #include <isl_pw_eval.c>
2797 #include <isl_pw_hash.c>
2798 #include <isl_pw_insert_dims_templ.c>
2799 #include <isl_pw_insert_domain_templ.c>
2800 #include <isl_pw_move_dims_templ.c>
2801 #include <isl_pw_neg_templ.c>
2802 #include <isl_pw_pullback_templ.c>
2803 #include <isl_pw_sub_templ.c>
2804 #include <isl_pw_union_opt.c>
2806 #undef BASE
2807 #define BASE pw_aff
2809 #include <isl_union_single.c>
2810 #include <isl_union_neg.c>
2812 #undef BASE
2813 #define BASE aff
2815 #include <isl_union_pw_templ.c>
2817 /* Compute a piecewise quasi-affine expression with a domain that
2818 * is the union of those of pwaff1 and pwaff2 and such that on each
2819 * cell, the quasi-affine expression is the maximum of those of pwaff1
2820 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2821 * cell, then the associated expression is the defined one.
2823 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2824 __isl_take isl_pw_aff *pwaff2)
2826 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2827 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2830 /* Compute a piecewise quasi-affine expression with a domain that
2831 * is the union of those of pwaff1 and pwaff2 and such that on each
2832 * cell, the quasi-affine expression is the minimum of those of pwaff1
2833 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2834 * cell, then the associated expression is the defined one.
2836 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2837 __isl_take isl_pw_aff *pwaff2)
2839 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2840 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2843 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2844 __isl_take isl_pw_aff *pwaff2, int max)
2846 if (max)
2847 return isl_pw_aff_union_max(pwaff1, pwaff2);
2848 else
2849 return isl_pw_aff_union_min(pwaff1, pwaff2);
2852 /* Is the domain of "pa" a product?
2854 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2856 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2859 #undef TYPE
2860 #define TYPE isl_pw_aff
2861 #include <isl_domain_factor_templ.c>
2863 /* Return a set containing those elements in the domain
2864 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2865 * does not satisfy "fn" (if complement is 1).
2867 * The pieces with a NaN never belong to the result since
2868 * NaN does not satisfy any property.
2870 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2871 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2872 void *user),
2873 int complement, void *user)
2875 int i;
2876 isl_set *set;
2878 if (!pwaff)
2879 return NULL;
2881 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2883 for (i = 0; i < pwaff->n; ++i) {
2884 isl_basic_set *bset;
2885 isl_set *set_i, *locus;
2886 isl_bool rational;
2888 if (isl_aff_is_nan(pwaff->p[i].aff))
2889 continue;
2891 rational = isl_set_has_rational(pwaff->p[i].set);
2892 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2893 locus = isl_set_from_basic_set(bset);
2894 set_i = isl_set_copy(pwaff->p[i].set);
2895 if (complement)
2896 set_i = isl_set_subtract(set_i, locus);
2897 else
2898 set_i = isl_set_intersect(set_i, locus);
2899 set = isl_set_union_disjoint(set, set_i);
2902 isl_pw_aff_free(pwaff);
2904 return set;
2907 /* Return a set containing those elements in the domain
2908 * of "pa" where it is positive.
2910 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2912 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2915 /* Return a set containing those elements in the domain
2916 * of pwaff where it is non-negative.
2918 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2920 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2923 /* Return a set containing those elements in the domain
2924 * of pwaff where it is zero.
2926 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2928 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2931 /* Return a set containing those elements in the domain
2932 * of pwaff where it is not zero.
2934 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2936 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2939 /* Bind the affine function "aff" to the parameter "id",
2940 * returning the elements in the domain where the affine expression
2941 * is equal to the parameter.
2943 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2944 __isl_take isl_id *id)
2946 isl_space *space;
2947 isl_aff *aff_id;
2949 space = isl_aff_get_domain_space(aff);
2950 space = isl_space_add_param_id(space, isl_id_copy(id));
2952 aff = isl_aff_align_params(aff, isl_space_copy(space));
2953 aff_id = isl_aff_param_on_domain_space_id(space, id);
2955 return isl_aff_eq_basic_set(aff, aff_id);
2958 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2959 * "rational" should not be set.
2961 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2962 int rational, void *user)
2964 isl_id *id = user;
2966 if (!aff)
2967 return NULL;
2968 if (rational)
2969 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2970 "rational binding not supported", goto error);
2971 return isl_aff_bind_id(aff, isl_id_copy(id));
2972 error:
2973 isl_aff_free(aff);
2974 return NULL;
2977 /* Bind the piecewise affine function "pa" to the parameter "id",
2978 * returning the elements in the domain where the expression
2979 * is equal to the parameter.
2981 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2982 __isl_take isl_id *id)
2984 isl_set *bound;
2986 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2987 isl_id_free(id);
2989 return bound;
2992 /* Return a set containing those elements in the shared domain
2993 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2995 * We compute the difference on the shared domain and then construct
2996 * the set of values where this difference is non-negative.
2997 * If strict is set, we first subtract 1 from the difference.
2998 * If equal is set, we only return the elements where pwaff1 and pwaff2
2999 * are equal.
3001 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3002 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3004 isl_set *set1, *set2;
3006 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3007 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3008 set1 = isl_set_intersect(set1, set2);
3009 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3010 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3011 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3013 if (strict) {
3014 isl_space *space = isl_set_get_space(set1);
3015 isl_aff *aff;
3016 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3017 aff = isl_aff_add_constant_si(aff, -1);
3018 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3019 } else
3020 isl_set_free(set1);
3022 if (equal)
3023 return isl_pw_aff_zero_set(pwaff1);
3024 return isl_pw_aff_nonneg_set(pwaff1);
3027 /* Return a set containing those elements in the shared domain
3028 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3030 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3031 __isl_take isl_pw_aff *pwaff2)
3033 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3034 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3037 /* Return a set containing those elements in the shared domain
3038 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3040 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3041 __isl_take isl_pw_aff *pwaff2)
3043 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3044 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3047 /* Return a set containing those elements in the shared domain
3048 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3050 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3051 __isl_take isl_pw_aff *pwaff2)
3053 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3054 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3057 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3058 __isl_take isl_pw_aff *pwaff2)
3060 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3063 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3064 __isl_take isl_pw_aff *pwaff2)
3066 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3069 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3070 * where the function values are ordered in the same way as "order",
3071 * which returns a set in the shared domain of its two arguments.
3073 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3074 * We first pull back the two functions such that they are defined on
3075 * the domain [A -> B]. Then we apply "order", resulting in a set
3076 * in the space [A -> B]. Finally, we unwrap this set to obtain
3077 * a map in the space A -> B.
3079 static __isl_give isl_map *isl_pw_aff_order_map(
3080 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3081 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3082 __isl_take isl_pw_aff *pa2))
3084 isl_space *space1, *space2;
3085 isl_multi_aff *ma;
3086 isl_set *set;
3088 isl_pw_aff_align_params_bin(&pa1, &pa2);
3089 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3090 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3091 space1 = isl_space_map_from_domain_and_range(space1, space2);
3092 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3093 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3094 ma = isl_multi_aff_range_map(space1);
3095 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3096 set = order(pa1, pa2);
3098 return isl_set_unwrap(set);
3101 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3102 * where the function values are equal.
3104 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3105 __isl_take isl_pw_aff *pa2)
3107 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3110 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3111 * where the function value of "pa1" is less than or equal to
3112 * the function value of "pa2".
3114 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3115 __isl_take isl_pw_aff *pa2)
3117 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3120 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3121 * where the function value of "pa1" is less than the function value of "pa2".
3123 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3124 __isl_take isl_pw_aff *pa2)
3126 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3129 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3130 * where the function value of "pa1" is greater than or equal to
3131 * the function value of "pa2".
3133 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3134 __isl_take isl_pw_aff *pa2)
3136 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3139 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3140 * where the function value of "pa1" is greater than the function value
3141 * of "pa2".
3143 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3144 __isl_take isl_pw_aff *pa2)
3146 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3149 /* Return a set containing those elements in the shared domain
3150 * of the elements of list1 and list2 where each element in list1
3151 * has the relation specified by "fn" with each element in list2.
3153 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3154 __isl_take isl_pw_aff_list *list2,
3155 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3156 __isl_take isl_pw_aff *pwaff2))
3158 int i, j;
3159 isl_ctx *ctx;
3160 isl_set *set;
3162 if (!list1 || !list2)
3163 goto error;
3165 ctx = isl_pw_aff_list_get_ctx(list1);
3166 if (list1->n < 1 || list2->n < 1)
3167 isl_die(ctx, isl_error_invalid,
3168 "list should contain at least one element", goto error);
3170 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3171 for (i = 0; i < list1->n; ++i)
3172 for (j = 0; j < list2->n; ++j) {
3173 isl_set *set_ij;
3175 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3176 isl_pw_aff_copy(list2->p[j]));
3177 set = isl_set_intersect(set, set_ij);
3180 isl_pw_aff_list_free(list1);
3181 isl_pw_aff_list_free(list2);
3182 return set;
3183 error:
3184 isl_pw_aff_list_free(list1);
3185 isl_pw_aff_list_free(list2);
3186 return NULL;
3189 /* Return a set containing those elements in the shared domain
3190 * of the elements of list1 and list2 where each element in list1
3191 * is equal to each element in list2.
3193 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3194 __isl_take isl_pw_aff_list *list2)
3196 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3199 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3200 __isl_take isl_pw_aff_list *list2)
3202 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3205 /* Return a set containing those elements in the shared domain
3206 * of the elements of list1 and list2 where each element in list1
3207 * is less than or equal to each element in list2.
3209 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3210 __isl_take isl_pw_aff_list *list2)
3212 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3215 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3216 __isl_take isl_pw_aff_list *list2)
3218 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3221 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3222 __isl_take isl_pw_aff_list *list2)
3224 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3227 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3228 __isl_take isl_pw_aff_list *list2)
3230 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3234 /* Return a set containing those elements in the shared domain
3235 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3237 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3238 __isl_take isl_pw_aff *pwaff2)
3240 isl_set *set_lt, *set_gt;
3242 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3243 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3244 isl_pw_aff_copy(pwaff2));
3245 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3246 return isl_set_union_disjoint(set_lt, set_gt);
3249 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3250 isl_int v)
3252 int i;
3254 if (isl_int_is_one(v))
3255 return pwaff;
3256 if (!isl_int_is_pos(v))
3257 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3258 "factor needs to be positive",
3259 return isl_pw_aff_free(pwaff));
3260 pwaff = isl_pw_aff_cow(pwaff);
3261 if (!pwaff)
3262 return NULL;
3263 if (pwaff->n == 0)
3264 return pwaff;
3266 for (i = 0; i < pwaff->n; ++i) {
3267 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3268 if (!pwaff->p[i].aff)
3269 return isl_pw_aff_free(pwaff);
3272 return pwaff;
3275 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3277 int i;
3279 pwaff = isl_pw_aff_cow(pwaff);
3280 if (!pwaff)
3281 return NULL;
3282 if (pwaff->n == 0)
3283 return pwaff;
3285 for (i = 0; i < pwaff->n; ++i) {
3286 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3287 if (!pwaff->p[i].aff)
3288 return isl_pw_aff_free(pwaff);
3291 return pwaff;
3294 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3296 int i;
3298 pwaff = isl_pw_aff_cow(pwaff);
3299 if (!pwaff)
3300 return NULL;
3301 if (pwaff->n == 0)
3302 return pwaff;
3304 for (i = 0; i < pwaff->n; ++i) {
3305 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3306 if (!pwaff->p[i].aff)
3307 return isl_pw_aff_free(pwaff);
3310 return pwaff;
3313 /* Assuming that "cond1" and "cond2" are disjoint,
3314 * return an affine expression that is equal to pwaff1 on cond1
3315 * and to pwaff2 on cond2.
3317 static __isl_give isl_pw_aff *isl_pw_aff_select(
3318 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3319 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3321 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3322 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3324 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3327 /* Return an affine expression that is equal to pwaff_true for elements
3328 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3329 * is zero.
3330 * That is, return cond ? pwaff_true : pwaff_false;
3332 * If "cond" involves and NaN, then we conservatively return a NaN
3333 * on its entire domain. In principle, we could consider the pieces
3334 * where it is NaN separately from those where it is not.
3336 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3337 * then only use the domain of "cond" to restrict the domain.
3339 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3340 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3342 isl_set *cond_true, *cond_false;
3343 isl_bool equal;
3345 if (!cond)
3346 goto error;
3347 if (isl_pw_aff_involves_nan(cond)) {
3348 isl_space *space = isl_pw_aff_get_domain_space(cond);
3349 isl_local_space *ls = isl_local_space_from_space(space);
3350 isl_pw_aff_free(cond);
3351 isl_pw_aff_free(pwaff_true);
3352 isl_pw_aff_free(pwaff_false);
3353 return isl_pw_aff_nan_on_domain(ls);
3356 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3357 isl_pw_aff_get_space(pwaff_false));
3358 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3359 isl_pw_aff_get_space(pwaff_true));
3360 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3361 if (equal < 0)
3362 goto error;
3363 if (equal) {
3364 isl_set *dom;
3366 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3367 isl_pw_aff_free(pwaff_false);
3368 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3371 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3372 cond_false = isl_pw_aff_zero_set(cond);
3373 return isl_pw_aff_select(cond_true, pwaff_true,
3374 cond_false, pwaff_false);
3375 error:
3376 isl_pw_aff_free(cond);
3377 isl_pw_aff_free(pwaff_true);
3378 isl_pw_aff_free(pwaff_false);
3379 return NULL;
3382 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3384 int pos;
3386 if (!aff)
3387 return isl_bool_error;
3389 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3390 return isl_bool_ok(pos == -1);
3393 /* Check whether pwaff is a piecewise constant.
3395 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3397 int i;
3399 if (!pwaff)
3400 return isl_bool_error;
3402 for (i = 0; i < pwaff->n; ++i) {
3403 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3404 if (is_cst < 0 || !is_cst)
3405 return is_cst;
3408 return isl_bool_true;
3411 /* Return the product of "aff1" and "aff2".
3413 * If either of the two is NaN, then the result is NaN.
3415 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3417 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3418 __isl_take isl_aff *aff2)
3420 if (!aff1 || !aff2)
3421 goto error;
3423 if (isl_aff_is_nan(aff1)) {
3424 isl_aff_free(aff2);
3425 return aff1;
3427 if (isl_aff_is_nan(aff2)) {
3428 isl_aff_free(aff1);
3429 return aff2;
3432 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3433 return isl_aff_mul(aff2, aff1);
3435 if (!isl_aff_is_cst(aff2))
3436 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3437 "at least one affine expression should be constant",
3438 goto error);
3440 aff1 = isl_aff_cow(aff1);
3441 if (!aff1 || !aff2)
3442 goto error;
3444 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3445 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3447 isl_aff_free(aff2);
3448 return aff1;
3449 error:
3450 isl_aff_free(aff1);
3451 isl_aff_free(aff2);
3452 return NULL;
3455 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3457 * If either of the two is NaN, then the result is NaN.
3458 * A division by zero also results in NaN.
3460 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3461 __isl_take isl_aff *aff2)
3463 isl_bool is_cst, is_zero;
3464 int neg;
3466 if (!aff1 || !aff2)
3467 goto error;
3469 if (isl_aff_is_nan(aff1)) {
3470 isl_aff_free(aff2);
3471 return aff1;
3473 if (isl_aff_is_nan(aff2)) {
3474 isl_aff_free(aff1);
3475 return aff2;
3478 is_cst = isl_aff_is_cst(aff2);
3479 if (is_cst < 0)
3480 goto error;
3481 if (!is_cst)
3482 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3483 "second argument should be a constant", goto error);
3484 is_zero = isl_aff_plain_is_zero(aff2);
3485 if (is_zero < 0)
3486 goto error;
3487 if (is_zero)
3488 return set_nan_free(aff1, aff2);
3490 neg = isl_int_is_neg(aff2->v->el[1]);
3491 if (neg) {
3492 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3493 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3496 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3497 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3499 if (neg) {
3500 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3501 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3504 isl_aff_free(aff2);
3505 return aff1;
3506 error:
3507 isl_aff_free(aff1);
3508 isl_aff_free(aff2);
3509 return NULL;
3512 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3513 __isl_take isl_pw_aff *pwaff2)
3515 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3516 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3519 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3520 __isl_take isl_pw_aff *pwaff2)
3522 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3525 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3526 __isl_take isl_pw_aff *pwaff2)
3528 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3529 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3532 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3534 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3535 __isl_take isl_pw_aff *pa2)
3537 int is_cst;
3539 is_cst = isl_pw_aff_is_cst(pa2);
3540 if (is_cst < 0)
3541 goto error;
3542 if (!is_cst)
3543 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3544 "second argument should be a piecewise constant",
3545 goto error);
3546 isl_pw_aff_align_params_bin(&pa1, &pa2);
3547 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3548 error:
3549 isl_pw_aff_free(pa1);
3550 isl_pw_aff_free(pa2);
3551 return NULL;
3554 /* Compute the quotient of the integer division of "pa1" by "pa2"
3555 * with rounding towards zero.
3556 * "pa2" is assumed to be a piecewise constant.
3558 * In particular, return
3560 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3563 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3564 __isl_take isl_pw_aff *pa2)
3566 int is_cst;
3567 isl_set *cond;
3568 isl_pw_aff *f, *c;
3570 is_cst = isl_pw_aff_is_cst(pa2);
3571 if (is_cst < 0)
3572 goto error;
3573 if (!is_cst)
3574 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3575 "second argument should be a piecewise constant",
3576 goto error);
3578 pa1 = isl_pw_aff_div(pa1, pa2);
3580 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3581 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3582 c = isl_pw_aff_ceil(pa1);
3583 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3584 error:
3585 isl_pw_aff_free(pa1);
3586 isl_pw_aff_free(pa2);
3587 return NULL;
3590 /* Compute the remainder of the integer division of "pa1" by "pa2"
3591 * with rounding towards zero.
3592 * "pa2" is assumed to be a piecewise constant.
3594 * In particular, return
3596 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3599 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3600 __isl_take isl_pw_aff *pa2)
3602 int is_cst;
3603 isl_pw_aff *res;
3605 is_cst = isl_pw_aff_is_cst(pa2);
3606 if (is_cst < 0)
3607 goto error;
3608 if (!is_cst)
3609 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3610 "second argument should be a piecewise constant",
3611 goto error);
3612 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3613 res = isl_pw_aff_mul(pa2, res);
3614 res = isl_pw_aff_sub(pa1, res);
3615 return res;
3616 error:
3617 isl_pw_aff_free(pa1);
3618 isl_pw_aff_free(pa2);
3619 return NULL;
3622 /* Does either of "pa1" or "pa2" involve any NaN2?
3624 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3625 __isl_keep isl_pw_aff *pa2)
3627 isl_bool has_nan;
3629 has_nan = isl_pw_aff_involves_nan(pa1);
3630 if (has_nan < 0 || has_nan)
3631 return has_nan;
3632 return isl_pw_aff_involves_nan(pa2);
3635 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3636 * by a NaN on their shared domain.
3638 * In principle, the result could be refined to only being NaN
3639 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3641 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3642 __isl_take isl_pw_aff *pa2)
3644 isl_local_space *ls;
3645 isl_set *dom;
3646 isl_pw_aff *pa;
3648 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3649 ls = isl_local_space_from_space(isl_set_get_space(dom));
3650 pa = isl_pw_aff_nan_on_domain(ls);
3651 pa = isl_pw_aff_intersect_domain(pa, dom);
3653 return pa;
3656 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3657 __isl_take isl_pw_aff *pwaff2)
3659 isl_set *le;
3660 isl_set *dom;
3662 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3663 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3664 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3665 isl_pw_aff_copy(pwaff2));
3666 dom = isl_set_subtract(dom, isl_set_copy(le));
3667 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3670 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3671 __isl_take isl_pw_aff *pwaff2)
3673 isl_set *ge;
3674 isl_set *dom;
3676 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3677 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3678 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3679 isl_pw_aff_copy(pwaff2));
3680 dom = isl_set_subtract(dom, isl_set_copy(ge));
3681 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3684 /* Return an expression for the minimum (if "max" is not set) or
3685 * the maximum (if "max" is set) of "pa1" and "pa2".
3686 * If either expression involves any NaN, then return a NaN
3687 * on the shared domain as result.
3689 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3690 __isl_take isl_pw_aff *pa2, int max)
3692 isl_bool has_nan;
3694 has_nan = either_involves_nan(pa1, pa2);
3695 if (has_nan < 0)
3696 pa1 = isl_pw_aff_free(pa1);
3697 else if (has_nan)
3698 return replace_by_nan(pa1, pa2);
3700 isl_pw_aff_align_params_bin(&pa1, &pa2);
3701 if (max)
3702 return pw_aff_max(pa1, pa2);
3703 else
3704 return pw_aff_min(pa1, pa2);
3707 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3709 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3710 __isl_take isl_pw_aff *pwaff2)
3712 return pw_aff_min_max(pwaff1, pwaff2, 0);
3715 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3717 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3718 __isl_take isl_pw_aff *pwaff2)
3720 return pw_aff_min_max(pwaff1, pwaff2, 1);
3723 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3724 __isl_take isl_pw_aff_list *list,
3725 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3726 __isl_take isl_pw_aff *pwaff2))
3728 int i;
3729 isl_ctx *ctx;
3730 isl_pw_aff *res;
3732 if (!list)
3733 return NULL;
3735 ctx = isl_pw_aff_list_get_ctx(list);
3736 if (list->n < 1)
3737 isl_die(ctx, isl_error_invalid,
3738 "list should contain at least one element", goto error);
3740 res = isl_pw_aff_copy(list->p[0]);
3741 for (i = 1; i < list->n; ++i)
3742 res = fn(res, isl_pw_aff_copy(list->p[i]));
3744 isl_pw_aff_list_free(list);
3745 return res;
3746 error:
3747 isl_pw_aff_list_free(list);
3748 return NULL;
3751 /* Return an isl_pw_aff that maps each element in the intersection of the
3752 * domains of the elements of list to the minimal corresponding affine
3753 * expression.
3755 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3757 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3760 /* Return an isl_pw_aff that maps each element in the intersection of the
3761 * domains of the elements of list to the maximal corresponding affine
3762 * expression.
3764 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3766 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3769 /* Mark the domains of "pwaff" as rational.
3771 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3773 int i;
3775 pwaff = isl_pw_aff_cow(pwaff);
3776 if (!pwaff)
3777 return NULL;
3778 if (pwaff->n == 0)
3779 return pwaff;
3781 for (i = 0; i < pwaff->n; ++i) {
3782 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3783 if (!pwaff->p[i].set)
3784 return isl_pw_aff_free(pwaff);
3787 return pwaff;
3790 /* Mark the domains of the elements of "list" as rational.
3792 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3793 __isl_take isl_pw_aff_list *list)
3795 int i, n;
3797 if (!list)
3798 return NULL;
3799 if (list->n == 0)
3800 return list;
3802 n = list->n;
3803 for (i = 0; i < n; ++i) {
3804 isl_pw_aff *pa;
3806 pa = isl_pw_aff_list_get_pw_aff(list, i);
3807 pa = isl_pw_aff_set_rational(pa);
3808 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3811 return list;
3814 /* Do the parameters of "aff" match those of "space"?
3816 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3817 __isl_keep isl_space *space)
3819 isl_space *aff_space;
3820 isl_bool match;
3822 if (!aff || !space)
3823 return isl_bool_error;
3825 aff_space = isl_aff_get_domain_space(aff);
3827 match = isl_space_has_equal_params(space, aff_space);
3829 isl_space_free(aff_space);
3830 return match;
3833 /* Check that the domain space of "aff" matches "space".
3835 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3836 __isl_keep isl_space *space)
3838 isl_space *aff_space;
3839 isl_bool match;
3841 if (!aff || !space)
3842 return isl_stat_error;
3844 aff_space = isl_aff_get_domain_space(aff);
3846 match = isl_space_has_equal_params(space, aff_space);
3847 if (match < 0)
3848 goto error;
3849 if (!match)
3850 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3851 "parameters don't match", goto error);
3852 match = isl_space_tuple_is_equal(space, isl_dim_in,
3853 aff_space, isl_dim_set);
3854 if (match < 0)
3855 goto error;
3856 if (!match)
3857 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3858 "domains don't match", goto error);
3859 isl_space_free(aff_space);
3860 return isl_stat_ok;
3861 error:
3862 isl_space_free(aff_space);
3863 return isl_stat_error;
3866 /* Return the shared (universe) domain of the elements of "ma".
3868 * Since an isl_multi_aff (and an isl_aff) is always total,
3869 * the domain is always the universe set in its domain space.
3870 * This is a helper function for use in the generic isl_multi_*_bind.
3872 static __isl_give isl_basic_set *isl_multi_aff_domain(
3873 __isl_take isl_multi_aff *ma)
3875 isl_space *space;
3877 space = isl_multi_aff_get_space(ma);
3878 isl_multi_aff_free(ma);
3880 return isl_basic_set_universe(isl_space_domain(space));
3883 #undef BASE
3884 #define BASE aff
3886 #include <isl_multi_no_explicit_domain.c>
3887 #include <isl_multi_templ.c>
3888 #include <isl_multi_add_constant_templ.c>
3889 #include <isl_multi_apply_set.c>
3890 #include <isl_multi_arith_templ.c>
3891 #include <isl_multi_bind_domain_templ.c>
3892 #include <isl_multi_cmp.c>
3893 #include <isl_multi_dim_id_templ.c>
3894 #include <isl_multi_dims.c>
3895 #include <isl_multi_floor.c>
3896 #include <isl_multi_from_base_templ.c>
3897 #include <isl_multi_identity_templ.c>
3898 #include <isl_multi_insert_domain_templ.c>
3899 #include <isl_multi_locals_templ.c>
3900 #include <isl_multi_move_dims_templ.c>
3901 #include <isl_multi_nan_templ.c>
3902 #include <isl_multi_product_templ.c>
3903 #include <isl_multi_splice_templ.c>
3904 #include <isl_multi_tuple_id_templ.c>
3905 #include <isl_multi_unbind_params_templ.c>
3906 #include <isl_multi_zero_templ.c>
3908 #undef DOMBASE
3909 #define DOMBASE set
3910 #include <isl_multi_gist.c>
3912 #undef DOMBASE
3913 #define DOMBASE basic_set
3914 #include <isl_multi_bind_templ.c>
3916 /* Construct an isl_multi_aff living in "space" that corresponds
3917 * to the affine transformation matrix "mat".
3919 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3920 __isl_take isl_space *space, __isl_take isl_mat *mat)
3922 isl_ctx *ctx;
3923 isl_local_space *ls = NULL;
3924 isl_multi_aff *ma = NULL;
3925 isl_size n_row, n_col, n_out, total;
3926 int i;
3928 if (!space || !mat)
3929 goto error;
3931 ctx = isl_mat_get_ctx(mat);
3933 n_row = isl_mat_rows(mat);
3934 n_col = isl_mat_cols(mat);
3935 n_out = isl_space_dim(space, isl_dim_out);
3936 total = isl_space_dim(space, isl_dim_all);
3937 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3938 goto error;
3939 if (n_row < 1)
3940 isl_die(ctx, isl_error_invalid,
3941 "insufficient number of rows", goto error);
3942 if (n_col < 1)
3943 isl_die(ctx, isl_error_invalid,
3944 "insufficient number of columns", goto error);
3945 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3946 isl_die(ctx, isl_error_invalid,
3947 "dimension mismatch", goto error);
3949 ma = isl_multi_aff_zero(isl_space_copy(space));
3950 space = isl_space_domain(space);
3951 ls = isl_local_space_from_space(isl_space_copy(space));
3953 for (i = 0; i < n_row - 1; ++i) {
3954 isl_vec *v;
3955 isl_aff *aff;
3957 v = isl_vec_alloc(ctx, 1 + n_col);
3958 if (!v)
3959 goto error;
3960 isl_int_set(v->el[0], mat->row[0][0]);
3961 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3962 v = isl_vec_normalize(v);
3963 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3964 ma = isl_multi_aff_set_aff(ma, i, aff);
3967 isl_space_free(space);
3968 isl_local_space_free(ls);
3969 isl_mat_free(mat);
3970 return ma;
3971 error:
3972 isl_space_free(space);
3973 isl_local_space_free(ls);
3974 isl_mat_free(mat);
3975 isl_multi_aff_free(ma);
3976 return NULL;
3979 /* Return the constant terms of the affine expressions of "ma".
3981 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3982 __isl_keep isl_multi_aff *ma)
3984 int i;
3985 isl_size n;
3986 isl_space *space;
3987 isl_multi_val *mv;
3989 n = isl_multi_aff_size(ma);
3990 if (n < 0)
3991 return NULL;
3992 space = isl_space_range(isl_multi_aff_get_space(ma));
3993 space = isl_space_drop_all_params(space);
3994 mv = isl_multi_val_zero(space);
3996 for (i = 0; i < n; ++i) {
3997 isl_aff *aff;
3998 isl_val *val;
4000 aff = isl_multi_aff_get_at(ma, i);
4001 val = isl_aff_get_constant_val(aff);
4002 isl_aff_free(aff);
4003 mv = isl_multi_val_set_at(mv, i, val);
4006 return mv;
4009 /* Remove any internal structure of the domain of "ma".
4010 * If there is any such internal structure in the input,
4011 * then the name of the corresponding space is also removed.
4013 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4014 __isl_take isl_multi_aff *ma)
4016 isl_space *space;
4018 if (!ma)
4019 return NULL;
4021 if (!ma->space->nested[0])
4022 return ma;
4024 space = isl_multi_aff_get_space(ma);
4025 space = isl_space_flatten_domain(space);
4026 ma = isl_multi_aff_reset_space(ma, space);
4028 return ma;
4031 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4032 * of the space to its domain.
4034 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4036 int i;
4037 isl_size n_in;
4038 isl_local_space *ls;
4039 isl_multi_aff *ma;
4041 if (!space)
4042 return NULL;
4043 if (!isl_space_is_map(space))
4044 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4045 "not a map space", goto error);
4047 n_in = isl_space_dim(space, isl_dim_in);
4048 if (n_in < 0)
4049 goto error;
4050 space = isl_space_domain_map(space);
4052 ma = isl_multi_aff_alloc(isl_space_copy(space));
4053 if (n_in == 0) {
4054 isl_space_free(space);
4055 return ma;
4058 space = isl_space_domain(space);
4059 ls = isl_local_space_from_space(space);
4060 for (i = 0; i < n_in; ++i) {
4061 isl_aff *aff;
4063 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4064 isl_dim_set, i);
4065 ma = isl_multi_aff_set_aff(ma, i, aff);
4067 isl_local_space_free(ls);
4068 return ma;
4069 error:
4070 isl_space_free(space);
4071 return NULL;
4074 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4075 * of the space to its range.
4077 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4079 int i;
4080 isl_size n_in, n_out;
4081 isl_local_space *ls;
4082 isl_multi_aff *ma;
4084 if (!space)
4085 return NULL;
4086 if (!isl_space_is_map(space))
4087 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4088 "not a map space", goto error);
4090 n_in = isl_space_dim(space, isl_dim_in);
4091 n_out = isl_space_dim(space, isl_dim_out);
4092 if (n_in < 0 || n_out < 0)
4093 goto error;
4094 space = isl_space_range_map(space);
4096 ma = isl_multi_aff_alloc(isl_space_copy(space));
4097 if (n_out == 0) {
4098 isl_space_free(space);
4099 return ma;
4102 space = isl_space_domain(space);
4103 ls = isl_local_space_from_space(space);
4104 for (i = 0; i < n_out; ++i) {
4105 isl_aff *aff;
4107 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4108 isl_dim_set, n_in + i);
4109 ma = isl_multi_aff_set_aff(ma, i, aff);
4111 isl_local_space_free(ls);
4112 return ma;
4113 error:
4114 isl_space_free(space);
4115 return NULL;
4118 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4119 * of the space to its domain.
4121 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4122 __isl_take isl_space *space)
4124 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4127 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4128 * of the space to its range.
4130 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4131 __isl_take isl_space *space)
4133 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4136 /* Given the space of a set and a range of set dimensions,
4137 * construct an isl_multi_aff that projects out those dimensions.
4139 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4140 __isl_take isl_space *space, enum isl_dim_type type,
4141 unsigned first, unsigned n)
4143 int i;
4144 isl_size dim;
4145 isl_local_space *ls;
4146 isl_multi_aff *ma;
4148 if (!space)
4149 return NULL;
4150 if (!isl_space_is_set(space))
4151 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4152 "expecting set space", goto error);
4153 if (type != isl_dim_set)
4154 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4155 "only set dimensions can be projected out", goto error);
4156 if (isl_space_check_range(space, type, first, n) < 0)
4157 goto error;
4159 dim = isl_space_dim(space, isl_dim_set);
4160 if (dim < 0)
4161 goto error;
4163 space = isl_space_from_domain(space);
4164 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4166 if (dim == n)
4167 return isl_multi_aff_alloc(space);
4169 ma = isl_multi_aff_alloc(isl_space_copy(space));
4170 space = isl_space_domain(space);
4171 ls = isl_local_space_from_space(space);
4173 for (i = 0; i < first; ++i) {
4174 isl_aff *aff;
4176 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4177 isl_dim_set, i);
4178 ma = isl_multi_aff_set_aff(ma, i, aff);
4181 for (i = 0; i < dim - (first + n); ++i) {
4182 isl_aff *aff;
4184 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4185 isl_dim_set, first + n + i);
4186 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4189 isl_local_space_free(ls);
4190 return ma;
4191 error:
4192 isl_space_free(space);
4193 return NULL;
4196 /* Given the space of a set and a range of set dimensions,
4197 * construct an isl_pw_multi_aff that projects out those dimensions.
4199 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4200 __isl_take isl_space *space, enum isl_dim_type type,
4201 unsigned first, unsigned n)
4203 isl_multi_aff *ma;
4205 ma = isl_multi_aff_project_out_map(space, type, first, n);
4206 return isl_pw_multi_aff_from_multi_aff(ma);
4209 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4210 * but is considered as a function on an isl_multi_aff when exported.
4212 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4213 __isl_take isl_multi_aff *ma)
4215 return isl_pw_multi_aff_from_multi_aff(ma);
4218 /* Create a piecewise multi-affine expression in the given space that maps each
4219 * input dimension to the corresponding output dimension.
4221 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4222 __isl_take isl_space *space)
4224 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4227 /* Create a piecewise multi expression that maps elements in the given space
4228 * to themselves.
4230 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4231 __isl_take isl_space *space)
4233 isl_multi_aff *ma;
4235 ma = isl_multi_aff_identity_on_domain_space(space);
4236 return isl_pw_multi_aff_from_multi_aff(ma);
4239 /* This function performs the same operation as
4240 * isl_pw_multi_aff_identity_on_domain_space,
4241 * but is considered as a function on an isl_space when exported.
4243 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4244 __isl_take isl_space *space)
4246 return isl_pw_multi_aff_identity_on_domain_space(space);
4249 /* Exploit the equalities in "eq" to simplify the affine expressions.
4251 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4252 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4254 int i;
4256 maff = isl_multi_aff_cow(maff);
4257 if (!maff || !eq)
4258 goto error;
4260 for (i = 0; i < maff->n; ++i) {
4261 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4262 isl_basic_set_copy(eq));
4263 if (!maff->u.p[i])
4264 goto error;
4267 isl_basic_set_free(eq);
4268 return maff;
4269 error:
4270 isl_basic_set_free(eq);
4271 isl_multi_aff_free(maff);
4272 return NULL;
4275 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4276 isl_int f)
4278 int i;
4280 maff = isl_multi_aff_cow(maff);
4281 if (!maff)
4282 return NULL;
4284 for (i = 0; i < maff->n; ++i) {
4285 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4286 if (!maff->u.p[i])
4287 return isl_multi_aff_free(maff);
4290 return maff;
4293 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4294 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4296 maff1 = isl_multi_aff_add(maff1, maff2);
4297 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4298 return maff1;
4301 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4303 if (!maff)
4304 return isl_bool_error;
4306 return isl_bool_false;
4309 /* Return the set of domain elements where "ma1" is lexicographically
4310 * smaller than or equal to "ma2".
4312 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4313 __isl_take isl_multi_aff *ma2)
4315 return isl_multi_aff_lex_ge_set(ma2, ma1);
4318 /* Return the set of domain elements where "ma1" is lexicographically
4319 * smaller than "ma2".
4321 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4322 __isl_take isl_multi_aff *ma2)
4324 return isl_multi_aff_lex_gt_set(ma2, ma1);
4327 /* Return the set of domain elements where "ma1" is lexicographically
4328 * greater than to "ma2". If "equal" is set, then include the domain
4329 * elements where they are equal.
4330 * Do this for the case where there are no entries.
4331 * In this case, "ma1" cannot be greater than "ma2",
4332 * but it is (greater than or) equal to "ma2".
4334 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4335 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4337 isl_space *space;
4339 space = isl_multi_aff_get_domain_space(ma1);
4341 isl_multi_aff_free(ma1);
4342 isl_multi_aff_free(ma2);
4344 if (equal)
4345 return isl_set_universe(space);
4346 else
4347 return isl_set_empty(space);
4350 /* Return the set where entry "i" of "ma1" and "ma2"
4351 * satisfy the relation prescribed by "cmp".
4353 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4354 __isl_keep isl_multi_aff *ma2, int i,
4355 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4356 __isl_take isl_aff *aff2))
4358 isl_aff *aff1, *aff2;
4360 aff1 = isl_multi_aff_get_at(ma1, i);
4361 aff2 = isl_multi_aff_get_at(ma2, i);
4362 return cmp(aff1, aff2);
4365 /* Return the set of domain elements where "ma1" is lexicographically
4366 * greater than to "ma2". If "equal" is set, then include the domain
4367 * elements where they are equal.
4369 * In particular, for all but the final entry,
4370 * include the set of elements where this entry is strictly greater in "ma1"
4371 * and all previous entries are equal.
4372 * The final entry is also allowed to be equal in the two functions
4373 * if "equal" is set.
4375 * The case where there are no entries is handled separately.
4377 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4378 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4380 int i;
4381 isl_size n;
4382 isl_space *space;
4383 isl_set *res;
4384 isl_set *equal_set;
4385 isl_set *gte;
4387 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4388 goto error;
4389 n = isl_multi_aff_size(ma1);
4390 if (n < 0)
4391 goto error;
4392 if (n == 0)
4393 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4395 space = isl_multi_aff_get_domain_space(ma1);
4396 res = isl_set_empty(isl_space_copy(space));
4397 equal_set = isl_set_universe(space);
4399 for (i = 0; i + 1 < n; ++i) {
4400 isl_bool empty;
4401 isl_set *gt, *eq;
4403 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4404 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4405 res = isl_set_union(res, gt);
4406 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4407 equal_set = isl_set_intersect(equal_set, eq);
4409 empty = isl_set_is_empty(equal_set);
4410 if (empty >= 0 && empty)
4411 break;
4414 if (equal)
4415 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4416 else
4417 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4418 isl_multi_aff_free(ma1);
4419 isl_multi_aff_free(ma2);
4421 gte = isl_set_intersect(gte, equal_set);
4422 return isl_set_union(res, gte);
4423 error:
4424 isl_multi_aff_free(ma1);
4425 isl_multi_aff_free(ma2);
4426 return NULL;
4429 /* Return the set of domain elements where "ma1" is lexicographically
4430 * greater than or equal to "ma2".
4432 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4433 __isl_take isl_multi_aff *ma2)
4435 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4438 /* Return the set of domain elements where "ma1" is lexicographically
4439 * greater than "ma2".
4441 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4442 __isl_take isl_multi_aff *ma2)
4444 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4447 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4449 #undef PW
4450 #define PW isl_pw_multi_aff
4451 #undef BASE
4452 #define BASE multi_aff
4453 #undef EL_IS_ZERO
4454 #define EL_IS_ZERO is_empty
4455 #undef ZERO
4456 #define ZERO empty
4457 #undef IS_ZERO
4458 #define IS_ZERO is_empty
4459 #undef FIELD
4460 #define FIELD maff
4461 #undef DEFAULT_IS_ZERO
4462 #define DEFAULT_IS_ZERO 0
4464 #include <isl_pw_templ.c>
4465 #include <isl_pw_add_constant_multi_val_templ.c>
4466 #include <isl_pw_add_constant_val_templ.c>
4467 #include <isl_pw_bind_domain_templ.c>
4468 #include <isl_pw_insert_dims_templ.c>
4469 #include <isl_pw_insert_domain_templ.c>
4470 #include <isl_pw_locals_templ.c>
4471 #include <isl_pw_move_dims_templ.c>
4472 #include <isl_pw_neg_templ.c>
4473 #include <isl_pw_pullback_templ.c>
4474 #include <isl_pw_union_opt.c>
4476 #undef BASE
4477 #define BASE pw_multi_aff
4479 #include <isl_union_multi.c>
4480 #include "isl_union_locals_templ.c"
4481 #include <isl_union_neg.c>
4483 #undef BASE
4484 #define BASE multi_aff
4486 #include <isl_union_pw_templ.c>
4488 /* Generic function for extracting a factor from a product "pma".
4489 * "check_space" checks that the space is that of the right kind of product.
4490 * "space_factor" extracts the factor from the space.
4491 * "multi_aff_factor" extracts the factor from the constituent functions.
4493 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4494 __isl_take isl_pw_multi_aff *pma,
4495 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4496 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4497 __isl_give isl_multi_aff *(*multi_aff_factor)(
4498 __isl_take isl_multi_aff *ma))
4500 int i;
4501 isl_space *space;
4503 if (check_space(pma) < 0)
4504 return isl_pw_multi_aff_free(pma);
4506 space = isl_pw_multi_aff_take_space(pma);
4507 space = space_factor(space);
4509 for (i = 0; pma && i < pma->n; ++i) {
4510 isl_multi_aff *ma;
4512 ma = isl_pw_multi_aff_take_base_at(pma, i);
4513 ma = multi_aff_factor(ma);
4514 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4517 pma = isl_pw_multi_aff_restore_space(pma, space);
4519 return pma;
4522 /* Is the range of "pma" a wrapped relation?
4524 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4525 __isl_keep isl_pw_multi_aff *pma)
4527 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4530 /* Check that the range of "pma" is a product.
4532 static isl_stat pw_multi_aff_check_range_product(
4533 __isl_keep isl_pw_multi_aff *pma)
4535 isl_bool wraps;
4537 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4538 if (wraps < 0)
4539 return isl_stat_error;
4540 if (!wraps)
4541 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4542 "range is not a product", return isl_stat_error);
4543 return isl_stat_ok;
4546 /* Given a function A -> [B -> C], extract the function A -> B.
4548 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4549 __isl_take isl_pw_multi_aff *pma)
4551 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4552 &isl_space_range_factor_domain,
4553 &isl_multi_aff_range_factor_domain);
4556 /* Given a function A -> [B -> C], extract the function A -> C.
4558 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4559 __isl_take isl_pw_multi_aff *pma)
4561 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4562 &isl_space_range_factor_range,
4563 &isl_multi_aff_range_factor_range);
4566 /* Given two piecewise multi affine expressions, return a piecewise
4567 * multi-affine expression defined on the union of the definition domains
4568 * of the inputs that is equal to the lexicographic maximum of the two
4569 * inputs on each cell. If only one of the two inputs is defined on
4570 * a given cell, then it is considered to be the maximum.
4572 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4573 __isl_take isl_pw_multi_aff *pma1,
4574 __isl_take isl_pw_multi_aff *pma2)
4576 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4577 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4578 &isl_multi_aff_lex_ge_set);
4581 /* Given two piecewise multi affine expressions, return a piecewise
4582 * multi-affine expression defined on the union of the definition domains
4583 * of the inputs that is equal to the lexicographic minimum of the two
4584 * inputs on each cell. If only one of the two inputs is defined on
4585 * a given cell, then it is considered to be the minimum.
4587 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4588 __isl_take isl_pw_multi_aff *pma1,
4589 __isl_take isl_pw_multi_aff *pma2)
4591 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4592 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4593 &isl_multi_aff_lex_le_set);
4596 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4597 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4599 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4600 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4601 &isl_multi_aff_add);
4604 /* Subtract "pma2" from "pma1" and return the result.
4606 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4607 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4609 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4610 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4611 &isl_multi_aff_sub);
4614 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4615 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4617 return isl_pw_multi_aff_union_add_(pma1, pma2);
4620 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4621 * with the actual sum on the shared domain and
4622 * the defined expression on the symmetric difference of the domains.
4624 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4625 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4627 return isl_union_pw_aff_union_add_(upa1, upa2);
4630 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4631 * with the actual sum on the shared domain and
4632 * the defined expression on the symmetric difference of the domains.
4634 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4635 __isl_take isl_union_pw_multi_aff *upma1,
4636 __isl_take isl_union_pw_multi_aff *upma2)
4638 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4641 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4642 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4644 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4645 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4647 int i, j, n;
4648 isl_space *space;
4649 isl_pw_multi_aff *res;
4651 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4652 goto error;
4654 n = pma1->n * pma2->n;
4655 space = isl_space_product(isl_space_copy(pma1->dim),
4656 isl_space_copy(pma2->dim));
4657 res = isl_pw_multi_aff_alloc_size(space, n);
4659 for (i = 0; i < pma1->n; ++i) {
4660 for (j = 0; j < pma2->n; ++j) {
4661 isl_set *domain;
4662 isl_multi_aff *ma;
4664 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4665 isl_set_copy(pma2->p[j].set));
4666 ma = isl_multi_aff_product(
4667 isl_multi_aff_copy(pma1->p[i].maff),
4668 isl_multi_aff_copy(pma2->p[j].maff));
4669 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4673 isl_pw_multi_aff_free(pma1);
4674 isl_pw_multi_aff_free(pma2);
4675 return res;
4676 error:
4677 isl_pw_multi_aff_free(pma1);
4678 isl_pw_multi_aff_free(pma2);
4679 return NULL;
4682 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4683 * denominator "denom".
4684 * "denom" is allowed to be negative, in which case the actual denominator
4685 * is -denom and the expressions are added instead.
4687 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4688 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4690 int i, first;
4691 int sign;
4692 isl_int d;
4694 first = isl_seq_first_non_zero(c, n);
4695 if (first == -1)
4696 return aff;
4698 sign = isl_int_sgn(denom);
4699 isl_int_init(d);
4700 isl_int_abs(d, denom);
4701 for (i = first; i < n; ++i) {
4702 isl_aff *aff_i;
4704 if (isl_int_is_zero(c[i]))
4705 continue;
4706 aff_i = isl_multi_aff_get_aff(ma, i);
4707 aff_i = isl_aff_scale(aff_i, c[i]);
4708 aff_i = isl_aff_scale_down(aff_i, d);
4709 if (sign >= 0)
4710 aff = isl_aff_sub(aff, aff_i);
4711 else
4712 aff = isl_aff_add(aff, aff_i);
4714 isl_int_clear(d);
4716 return aff;
4719 /* Extract an affine expression that expresses the output dimension "pos"
4720 * of "bmap" in terms of the parameters and input dimensions from
4721 * equality "eq".
4722 * Note that this expression may involve integer divisions defined
4723 * in terms of parameters and input dimensions.
4724 * The equality may also involve references to earlier (but not later)
4725 * output dimensions. These are replaced by the corresponding elements
4726 * in "ma".
4728 * If the equality is of the form
4730 * f(i) + h(j) + a x + g(i) = 0,
4732 * with f(i) a linear combinations of the parameters and input dimensions,
4733 * g(i) a linear combination of integer divisions defined in terms of the same
4734 * and h(j) a linear combinations of earlier output dimensions,
4735 * then the affine expression is
4737 * (-f(i) - g(i))/a - h(j)/a
4739 * If the equality is of the form
4741 * f(i) + h(j) - a x + g(i) = 0,
4743 * then the affine expression is
4745 * (f(i) + g(i))/a - h(j)/(-a)
4748 * If "div" refers to an integer division (i.e., it is smaller than
4749 * the number of integer divisions), then the equality constraint
4750 * does involve an integer division (the one at position "div") that
4751 * is defined in terms of output dimensions. However, this integer
4752 * division can be eliminated by exploiting a pair of constraints
4753 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4754 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4755 * -l + x >= 0.
4756 * In particular, let
4758 * x = e(i) + m floor(...)
4760 * with e(i) the expression derived above and floor(...) the integer
4761 * division involving output dimensions.
4762 * From
4764 * l <= x <= l + n,
4766 * we have
4768 * 0 <= x - l <= n
4770 * This means
4772 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4773 * = (e(i) - l) mod m
4775 * Therefore,
4777 * x - l = (e(i) - l) mod m
4779 * or
4781 * x = ((e(i) - l) mod m) + l
4783 * The variable "shift" below contains the expression -l, which may
4784 * also involve a linear combination of earlier output dimensions.
4786 static __isl_give isl_aff *extract_aff_from_equality(
4787 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4788 __isl_keep isl_multi_aff *ma)
4790 unsigned o_out;
4791 isl_size n_div, n_out;
4792 isl_ctx *ctx;
4793 isl_local_space *ls;
4794 isl_aff *aff, *shift;
4795 isl_val *mod;
4797 ctx = isl_basic_map_get_ctx(bmap);
4798 ls = isl_basic_map_get_local_space(bmap);
4799 ls = isl_local_space_domain(ls);
4800 aff = isl_aff_alloc(isl_local_space_copy(ls));
4801 if (!aff)
4802 goto error;
4803 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4804 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4805 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4806 if (n_out < 0 || n_div < 0)
4807 goto error;
4808 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4809 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4810 isl_seq_cpy(aff->v->el + 1 + o_out,
4811 bmap->eq[eq] + o_out + n_out, n_div);
4812 } else {
4813 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4814 isl_seq_neg(aff->v->el + 1 + o_out,
4815 bmap->eq[eq] + o_out + n_out, n_div);
4817 if (div < n_div)
4818 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4819 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4820 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4821 bmap->eq[eq][o_out + pos]);
4822 if (div < n_div) {
4823 shift = isl_aff_alloc(isl_local_space_copy(ls));
4824 if (!shift)
4825 goto error;
4826 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4827 isl_seq_cpy(shift->v->el + 1 + o_out,
4828 bmap->ineq[ineq] + o_out + n_out, n_div);
4829 isl_int_set_si(shift->v->el[0], 1);
4830 shift = subtract_initial(shift, ma, pos,
4831 bmap->ineq[ineq] + o_out, ctx->negone);
4832 aff = isl_aff_add(aff, isl_aff_copy(shift));
4833 mod = isl_val_int_from_isl_int(ctx,
4834 bmap->eq[eq][o_out + n_out + div]);
4835 mod = isl_val_abs(mod);
4836 aff = isl_aff_mod_val(aff, mod);
4837 aff = isl_aff_sub(aff, shift);
4840 isl_local_space_free(ls);
4841 return aff;
4842 error:
4843 isl_local_space_free(ls);
4844 isl_aff_free(aff);
4845 return NULL;
4848 /* Given a basic map with output dimensions defined
4849 * in terms of the parameters input dimensions and earlier
4850 * output dimensions using an equality (and possibly a pair on inequalities),
4851 * extract an isl_aff that expresses output dimension "pos" in terms
4852 * of the parameters and input dimensions.
4853 * Note that this expression may involve integer divisions defined
4854 * in terms of parameters and input dimensions.
4855 * "ma" contains the expressions corresponding to earlier output dimensions.
4857 * This function shares some similarities with
4858 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4860 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4861 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4863 int eq, div, ineq;
4864 isl_aff *aff;
4866 if (!bmap)
4867 return NULL;
4868 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4869 if (eq >= bmap->n_eq)
4870 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4871 "unable to find suitable equality", return NULL);
4872 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4874 aff = isl_aff_remove_unused_divs(aff);
4875 return aff;
4878 /* Given a basic map where each output dimension is defined
4879 * in terms of the parameters and input dimensions using an equality,
4880 * extract an isl_multi_aff that expresses the output dimensions in terms
4881 * of the parameters and input dimensions.
4883 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4884 __isl_take isl_basic_map *bmap)
4886 int i;
4887 isl_size n_out;
4888 isl_multi_aff *ma;
4890 if (!bmap)
4891 return NULL;
4893 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4894 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4895 if (n_out < 0)
4896 ma = isl_multi_aff_free(ma);
4898 for (i = 0; i < n_out; ++i) {
4899 isl_aff *aff;
4901 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4902 ma = isl_multi_aff_set_aff(ma, i, aff);
4905 isl_basic_map_free(bmap);
4907 return ma;
4910 /* Given a basic set where each set dimension is defined
4911 * in terms of the parameters using an equality,
4912 * extract an isl_multi_aff that expresses the set dimensions in terms
4913 * of the parameters.
4915 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4916 __isl_take isl_basic_set *bset)
4918 return extract_isl_multi_aff_from_basic_map(bset);
4921 /* Create an isl_pw_multi_aff that is equivalent to
4922 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4923 * The given basic map is such that each output dimension is defined
4924 * in terms of the parameters and input dimensions using an equality.
4926 * Since some applications expect the result of isl_pw_multi_aff_from_map
4927 * to only contain integer affine expressions, we compute the floor
4928 * of the expression before returning.
4930 * Remove all constraints involving local variables without
4931 * an explicit representation (resulting in the removal of those
4932 * local variables) prior to the actual extraction to ensure
4933 * that the local spaces in which the resulting affine expressions
4934 * are created do not contain any unknown local variables.
4935 * Removing such constraints is safe because constraints involving
4936 * unknown local variables are not used to determine whether
4937 * a basic map is obviously single-valued.
4939 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4940 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4942 isl_multi_aff *ma;
4944 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4945 ma = extract_isl_multi_aff_from_basic_map(bmap);
4946 ma = isl_multi_aff_floor(ma);
4947 return isl_pw_multi_aff_alloc(domain, ma);
4950 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4951 * This obviously only works if the input "map" is single-valued.
4952 * If so, we compute the lexicographic minimum of the image in the form
4953 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4954 * to its lexicographic minimum.
4955 * If the input is not single-valued, we produce an error.
4957 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4958 __isl_take isl_map *map)
4960 int i;
4961 int sv;
4962 isl_pw_multi_aff *pma;
4964 sv = isl_map_is_single_valued(map);
4965 if (sv < 0)
4966 goto error;
4967 if (!sv)
4968 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4969 "map is not single-valued", goto error);
4970 map = isl_map_make_disjoint(map);
4971 if (!map)
4972 return NULL;
4974 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4976 for (i = 0; i < map->n; ++i) {
4977 isl_pw_multi_aff *pma_i;
4978 isl_basic_map *bmap;
4979 bmap = isl_basic_map_copy(map->p[i]);
4980 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4981 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4984 isl_map_free(map);
4985 return pma;
4986 error:
4987 isl_map_free(map);
4988 return NULL;
4991 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4992 * taking into account that the output dimension at position "d"
4993 * can be represented as
4995 * x = floor((e(...) + c1) / m)
4997 * given that constraint "i" is of the form
4999 * e(...) + c1 - m x >= 0
5002 * Let "map" be of the form
5004 * A -> B
5006 * We construct a mapping
5008 * A -> [A -> x = floor(...)]
5010 * apply that to the map, obtaining
5012 * [A -> x = floor(...)] -> B
5014 * and equate dimension "d" to x.
5015 * We then compute a isl_pw_multi_aff representation of the resulting map
5016 * and plug in the mapping above.
5018 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5019 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5021 isl_ctx *ctx;
5022 isl_space *space = NULL;
5023 isl_local_space *ls;
5024 isl_multi_aff *ma;
5025 isl_aff *aff;
5026 isl_vec *v;
5027 isl_map *insert;
5028 int offset;
5029 isl_size n;
5030 isl_size n_in;
5031 isl_pw_multi_aff *pma;
5032 isl_bool is_set;
5034 is_set = isl_map_is_set(map);
5035 if (is_set < 0)
5036 goto error;
5038 offset = isl_basic_map_offset(hull, isl_dim_out);
5039 ctx = isl_map_get_ctx(map);
5040 space = isl_space_domain(isl_map_get_space(map));
5041 n_in = isl_space_dim(space, isl_dim_set);
5042 n = isl_space_dim(space, isl_dim_all);
5043 if (n_in < 0 || n < 0)
5044 goto error;
5046 v = isl_vec_alloc(ctx, 1 + 1 + n);
5047 if (v) {
5048 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5049 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5051 isl_basic_map_free(hull);
5053 ls = isl_local_space_from_space(isl_space_copy(space));
5054 aff = isl_aff_alloc_vec(ls, v);
5055 aff = isl_aff_floor(aff);
5056 if (is_set) {
5057 isl_space_free(space);
5058 ma = isl_multi_aff_from_aff(aff);
5059 } else {
5060 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5061 ma = isl_multi_aff_range_product(ma,
5062 isl_multi_aff_from_aff(aff));
5065 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5066 map = isl_map_apply_domain(map, insert);
5067 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5068 pma = isl_pw_multi_aff_from_map(map);
5069 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5071 return pma;
5072 error:
5073 isl_space_free(space);
5074 isl_map_free(map);
5075 isl_basic_map_free(hull);
5076 return NULL;
5079 /* Is constraint "c" of the form
5081 * e(...) + c1 - m x >= 0
5083 * or
5085 * -e(...) + c2 + m x >= 0
5087 * where m > 1 and e only depends on parameters and input dimemnsions?
5089 * "offset" is the offset of the output dimensions
5090 * "pos" is the position of output dimension x.
5092 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5094 if (isl_int_is_zero(c[offset + d]))
5095 return 0;
5096 if (isl_int_is_one(c[offset + d]))
5097 return 0;
5098 if (isl_int_is_negone(c[offset + d]))
5099 return 0;
5100 if (isl_seq_first_non_zero(c + offset, d) != -1)
5101 return 0;
5102 if (isl_seq_first_non_zero(c + offset + d + 1,
5103 total - (offset + d + 1)) != -1)
5104 return 0;
5105 return 1;
5108 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5110 * As a special case, we first check if there is any pair of constraints,
5111 * shared by all the basic maps in "map" that force a given dimension
5112 * to be equal to the floor of some affine combination of the input dimensions.
5114 * In particular, if we can find two constraints
5116 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5118 * and
5120 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5122 * where m > 1 and e only depends on parameters and input dimemnsions,
5123 * and such that
5125 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5127 * then we know that we can take
5129 * x = floor((e(...) + c1) / m)
5131 * without having to perform any computation.
5133 * Note that we know that
5135 * c1 + c2 >= 1
5137 * If c1 + c2 were 0, then we would have detected an equality during
5138 * simplification. If c1 + c2 were negative, then we would have detected
5139 * a contradiction.
5141 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5142 __isl_take isl_map *map)
5144 int d;
5145 isl_size dim;
5146 int i, j, n;
5147 int offset;
5148 isl_size total;
5149 isl_int sum;
5150 isl_basic_map *hull;
5152 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5153 dim = isl_map_dim(map, isl_dim_out);
5154 total = isl_basic_map_dim(hull, isl_dim_all);
5155 if (dim < 0 || total < 0)
5156 goto error;
5158 isl_int_init(sum);
5159 offset = isl_basic_map_offset(hull, isl_dim_out);
5160 n = hull->n_ineq;
5161 for (d = 0; d < dim; ++d) {
5162 for (i = 0; i < n; ++i) {
5163 if (!is_potential_div_constraint(hull->ineq[i],
5164 offset, d, 1 + total))
5165 continue;
5166 for (j = i + 1; j < n; ++j) {
5167 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5168 hull->ineq[j] + 1, total))
5169 continue;
5170 isl_int_add(sum, hull->ineq[i][0],
5171 hull->ineq[j][0]);
5172 if (isl_int_abs_lt(sum,
5173 hull->ineq[i][offset + d]))
5174 break;
5177 if (j >= n)
5178 continue;
5179 isl_int_clear(sum);
5180 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5181 j = i;
5182 return pw_multi_aff_from_map_div(map, hull, d, j);
5185 isl_int_clear(sum);
5186 isl_basic_map_free(hull);
5187 return pw_multi_aff_from_map_base(map);
5188 error:
5189 isl_map_free(map);
5190 isl_basic_map_free(hull);
5191 return NULL;
5194 /* Given an affine expression
5196 * [A -> B] -> f(A,B)
5198 * construct an isl_multi_aff
5200 * [A -> B] -> B'
5202 * such that dimension "d" in B' is set to "aff" and the remaining
5203 * dimensions are set equal to the corresponding dimensions in B.
5204 * "n_in" is the dimension of the space A.
5205 * "n_out" is the dimension of the space B.
5207 * If "is_set" is set, then the affine expression is of the form
5209 * [B] -> f(B)
5211 * and we construct an isl_multi_aff
5213 * B -> B'
5215 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5216 unsigned n_in, unsigned n_out, int is_set)
5218 int i;
5219 isl_multi_aff *ma;
5220 isl_space *space, *space2;
5221 isl_local_space *ls;
5223 space = isl_aff_get_domain_space(aff);
5224 ls = isl_local_space_from_space(isl_space_copy(space));
5225 space2 = isl_space_copy(space);
5226 if (!is_set)
5227 space2 = isl_space_range(isl_space_unwrap(space2));
5228 space = isl_space_map_from_domain_and_range(space, space2);
5229 ma = isl_multi_aff_alloc(space);
5230 ma = isl_multi_aff_set_aff(ma, d, aff);
5232 for (i = 0; i < n_out; ++i) {
5233 if (i == d)
5234 continue;
5235 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5236 isl_dim_set, n_in + i);
5237 ma = isl_multi_aff_set_aff(ma, i, aff);
5240 isl_local_space_free(ls);
5242 return ma;
5245 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5246 * taking into account that the dimension at position "d" can be written as
5248 * x = m a + f(..) (1)
5250 * where m is equal to "gcd".
5251 * "i" is the index of the equality in "hull" that defines f(..).
5252 * In particular, the equality is of the form
5254 * f(..) - x + m g(existentials) = 0
5256 * or
5258 * -f(..) + x + m g(existentials) = 0
5260 * We basically plug (1) into "map", resulting in a map with "a"
5261 * in the range instead of "x". The corresponding isl_pw_multi_aff
5262 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5264 * Specifically, given the input map
5266 * A -> B
5268 * We first wrap it into a set
5270 * [A -> B]
5272 * and define (1) on top of the corresponding space, resulting in "aff".
5273 * We use this to create an isl_multi_aff that maps the output position "d"
5274 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5275 * We plug this into the wrapped map, unwrap the result and compute the
5276 * corresponding isl_pw_multi_aff.
5277 * The result is an expression
5279 * A -> T(A)
5281 * We adjust that to
5283 * A -> [A -> T(A)]
5285 * so that we can plug that into "aff", after extending the latter to
5286 * a mapping
5288 * [A -> B] -> B'
5291 * If "map" is actually a set, then there is no "A" space, meaning
5292 * that we do not need to perform any wrapping, and that the result
5293 * of the recursive call is of the form
5295 * [T]
5297 * which is plugged into a mapping of the form
5299 * B -> B'
5301 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5302 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5303 isl_int gcd)
5305 isl_set *set;
5306 isl_space *space;
5307 isl_local_space *ls;
5308 isl_aff *aff;
5309 isl_multi_aff *ma;
5310 isl_pw_multi_aff *pma, *id;
5311 isl_size n_in;
5312 unsigned o_out;
5313 isl_size n_out;
5314 isl_bool is_set;
5316 is_set = isl_map_is_set(map);
5317 if (is_set < 0)
5318 goto error;
5320 n_in = isl_basic_map_dim(hull, isl_dim_in);
5321 n_out = isl_basic_map_dim(hull, isl_dim_out);
5322 if (n_in < 0 || n_out < 0)
5323 goto error;
5324 o_out = isl_basic_map_offset(hull, isl_dim_out);
5326 if (is_set)
5327 set = map;
5328 else
5329 set = isl_map_wrap(map);
5330 space = isl_space_map_from_set(isl_set_get_space(set));
5331 ma = isl_multi_aff_identity(space);
5332 ls = isl_local_space_from_space(isl_set_get_space(set));
5333 aff = isl_aff_alloc(ls);
5334 if (aff) {
5335 isl_int_set_si(aff->v->el[0], 1);
5336 if (isl_int_is_one(hull->eq[i][o_out + d]))
5337 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5338 aff->v->size - 1);
5339 else
5340 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5341 aff->v->size - 1);
5342 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5344 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5345 set = isl_set_preimage_multi_aff(set, ma);
5347 ma = range_map(aff, d, n_in, n_out, is_set);
5349 if (is_set)
5350 map = set;
5351 else
5352 map = isl_set_unwrap(set);
5353 pma = isl_pw_multi_aff_from_map(map);
5355 if (!is_set) {
5356 space = isl_pw_multi_aff_get_domain_space(pma);
5357 space = isl_space_map_from_set(space);
5358 id = isl_pw_multi_aff_identity(space);
5359 pma = isl_pw_multi_aff_range_product(id, pma);
5361 id = isl_pw_multi_aff_from_multi_aff(ma);
5362 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5364 isl_basic_map_free(hull);
5365 return pma;
5366 error:
5367 isl_map_free(map);
5368 isl_basic_map_free(hull);
5369 return NULL;
5372 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5373 * "hull" contains the equalities valid for "map".
5375 * Check if any of the output dimensions is "strided".
5376 * That is, we check if it can be written as
5378 * x = m a + f(..)
5380 * with m greater than 1, a some combination of existentially quantified
5381 * variables and f an expression in the parameters and input dimensions.
5382 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5384 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5385 * special case.
5387 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5388 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5390 int i, j;
5391 isl_size n_out;
5392 unsigned o_out;
5393 isl_size n_div;
5394 unsigned o_div;
5395 isl_int gcd;
5397 n_div = isl_basic_map_dim(hull, isl_dim_div);
5398 n_out = isl_basic_map_dim(hull, isl_dim_out);
5399 if (n_div < 0 || n_out < 0)
5400 goto error;
5402 if (n_div == 0) {
5403 isl_basic_map_free(hull);
5404 return pw_multi_aff_from_map_check_div(map);
5407 isl_int_init(gcd);
5409 o_div = isl_basic_map_offset(hull, isl_dim_div);
5410 o_out = isl_basic_map_offset(hull, isl_dim_out);
5412 for (i = 0; i < n_out; ++i) {
5413 for (j = 0; j < hull->n_eq; ++j) {
5414 isl_int *eq = hull->eq[j];
5415 isl_pw_multi_aff *res;
5417 if (!isl_int_is_one(eq[o_out + i]) &&
5418 !isl_int_is_negone(eq[o_out + i]))
5419 continue;
5420 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5421 continue;
5422 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5423 n_out - (i + 1)) != -1)
5424 continue;
5425 isl_seq_gcd(eq + o_div, n_div, &gcd);
5426 if (isl_int_is_zero(gcd))
5427 continue;
5428 if (isl_int_is_one(gcd))
5429 continue;
5431 res = pw_multi_aff_from_map_stride(map, hull,
5432 i, j, gcd);
5433 isl_int_clear(gcd);
5434 return res;
5438 isl_int_clear(gcd);
5439 isl_basic_map_free(hull);
5440 return pw_multi_aff_from_map_check_div(map);
5441 error:
5442 isl_map_free(map);
5443 isl_basic_map_free(hull);
5444 return NULL;
5447 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5449 * As a special case, we first check if all output dimensions are uniquely
5450 * defined in terms of the parameters and input dimensions over the entire
5451 * domain. If so, we extract the desired isl_pw_multi_aff directly
5452 * from the affine hull of "map" and its domain.
5454 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5455 * special cases.
5457 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5459 isl_bool sv;
5460 isl_size n;
5461 isl_basic_map *hull;
5463 n = isl_map_n_basic_map(map);
5464 if (n < 0)
5465 goto error;
5467 if (n == 1) {
5468 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5469 hull = isl_basic_map_plain_affine_hull(hull);
5470 sv = isl_basic_map_plain_is_single_valued(hull);
5471 if (sv >= 0 && sv)
5472 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5473 hull);
5474 isl_basic_map_free(hull);
5476 map = isl_map_detect_equalities(map);
5477 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5478 sv = isl_basic_map_plain_is_single_valued(hull);
5479 if (sv >= 0 && sv)
5480 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5481 if (sv >= 0)
5482 return pw_multi_aff_from_map_check_strides(map, hull);
5483 isl_basic_map_free(hull);
5484 error:
5485 isl_map_free(map);
5486 return NULL;
5489 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5490 * but is considered as a function on an isl_map when exported.
5492 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5494 return isl_pw_multi_aff_from_map(map);
5497 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5499 return isl_pw_multi_aff_from_map(set);
5502 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5503 * but is considered as a function on an isl_set when exported.
5505 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5507 return isl_pw_multi_aff_from_set(set);
5510 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5511 * add it to *user.
5513 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5515 isl_union_pw_multi_aff **upma = user;
5516 isl_pw_multi_aff *pma;
5518 pma = isl_pw_multi_aff_from_map(map);
5519 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5521 return *upma ? isl_stat_ok : isl_stat_error;
5524 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5525 * domain.
5527 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5528 __isl_take isl_aff *aff)
5530 isl_multi_aff *ma;
5531 isl_pw_multi_aff *pma;
5533 ma = isl_multi_aff_from_aff(aff);
5534 pma = isl_pw_multi_aff_from_multi_aff(ma);
5535 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5538 /* Try and create an isl_union_pw_multi_aff that is equivalent
5539 * to the given isl_union_map.
5540 * The isl_union_map is required to be single-valued in each space.
5541 * Otherwise, an error is produced.
5543 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5544 __isl_take isl_union_map *umap)
5546 isl_space *space;
5547 isl_union_pw_multi_aff *upma;
5549 space = isl_union_map_get_space(umap);
5550 upma = isl_union_pw_multi_aff_empty(space);
5551 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5552 upma = isl_union_pw_multi_aff_free(upma);
5553 isl_union_map_free(umap);
5555 return upma;
5558 /* This function performs the same operation as
5559 * isl_union_pw_multi_aff_from_union_map,
5560 * but is considered as a function on an isl_union_map when exported.
5562 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5563 __isl_take isl_union_map *umap)
5565 return isl_union_pw_multi_aff_from_union_map(umap);
5568 /* Try and create an isl_union_pw_multi_aff that is equivalent
5569 * to the given isl_union_set.
5570 * The isl_union_set is required to be a singleton in each space.
5571 * Otherwise, an error is produced.
5573 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5574 __isl_take isl_union_set *uset)
5576 return isl_union_pw_multi_aff_from_union_map(uset);
5579 /* Return the piecewise affine expression "set ? 1 : 0".
5581 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5583 isl_pw_aff *pa;
5584 isl_space *space = isl_set_get_space(set);
5585 isl_local_space *ls = isl_local_space_from_space(space);
5586 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5587 isl_aff *one = isl_aff_zero_on_domain(ls);
5589 one = isl_aff_add_constant_si(one, 1);
5590 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5591 set = isl_set_complement(set);
5592 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5594 return pa;
5597 /* Plug in "subs" for dimension "type", "pos" of "aff".
5599 * Let i be the dimension to replace and let "subs" be of the form
5601 * f/d
5603 * and "aff" of the form
5605 * (a i + g)/m
5607 * The result is
5609 * (a f + d g')/(m d)
5611 * where g' is the result of plugging in "subs" in each of the integer
5612 * divisions in g.
5614 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5615 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5617 isl_ctx *ctx;
5618 isl_int v;
5619 isl_size n_div;
5621 aff = isl_aff_cow(aff);
5622 if (!aff || !subs)
5623 return isl_aff_free(aff);
5625 ctx = isl_aff_get_ctx(aff);
5626 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5627 isl_die(ctx, isl_error_invalid,
5628 "spaces don't match", return isl_aff_free(aff));
5629 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5630 if (n_div < 0)
5631 return isl_aff_free(aff);
5632 if (n_div != 0)
5633 isl_die(ctx, isl_error_unsupported,
5634 "cannot handle divs yet", return isl_aff_free(aff));
5636 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5637 if (!aff->ls)
5638 return isl_aff_free(aff);
5640 aff->v = isl_vec_cow(aff->v);
5641 if (!aff->v)
5642 return isl_aff_free(aff);
5644 pos += isl_local_space_offset(aff->ls, type);
5646 isl_int_init(v);
5647 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5648 aff->v->size, subs->v->size, v);
5649 isl_int_clear(v);
5651 return aff;
5654 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5655 * expressions in "maff".
5657 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5658 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5659 __isl_keep isl_aff *subs)
5661 int i;
5663 maff = isl_multi_aff_cow(maff);
5664 if (!maff || !subs)
5665 return isl_multi_aff_free(maff);
5667 if (type == isl_dim_in)
5668 type = isl_dim_set;
5670 for (i = 0; i < maff->n; ++i) {
5671 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5672 type, pos, subs);
5673 if (!maff->u.p[i])
5674 return isl_multi_aff_free(maff);
5677 return maff;
5680 /* Plug in "subs" for dimension "type", "pos" of "pma".
5682 * pma is of the form
5684 * A_i(v) -> M_i(v)
5686 * while subs is of the form
5688 * v' = B_j(v) -> S_j
5690 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5691 * has a contribution in the result, in particular
5693 * C_ij(S_j) -> M_i(S_j)
5695 * Note that plugging in S_j in C_ij may also result in an empty set
5696 * and this contribution should simply be discarded.
5698 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5699 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5700 __isl_keep isl_pw_aff *subs)
5702 int i, j, n;
5703 isl_pw_multi_aff *res;
5705 if (!pma || !subs)
5706 return isl_pw_multi_aff_free(pma);
5708 n = pma->n * subs->n;
5709 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5711 for (i = 0; i < pma->n; ++i) {
5712 for (j = 0; j < subs->n; ++j) {
5713 isl_set *common;
5714 isl_multi_aff *res_ij;
5715 int empty;
5717 common = isl_set_intersect(
5718 isl_set_copy(pma->p[i].set),
5719 isl_set_copy(subs->p[j].set));
5720 common = isl_set_substitute(common,
5721 type, pos, subs->p[j].aff);
5722 empty = isl_set_plain_is_empty(common);
5723 if (empty < 0 || empty) {
5724 isl_set_free(common);
5725 if (empty < 0)
5726 goto error;
5727 continue;
5730 res_ij = isl_multi_aff_substitute(
5731 isl_multi_aff_copy(pma->p[i].maff),
5732 type, pos, subs->p[j].aff);
5734 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5738 isl_pw_multi_aff_free(pma);
5739 return res;
5740 error:
5741 isl_pw_multi_aff_free(pma);
5742 isl_pw_multi_aff_free(res);
5743 return NULL;
5746 /* Compute the preimage of a range of dimensions in the affine expression "src"
5747 * under "ma" and put the result in "dst". The number of dimensions in "src"
5748 * that precede the range is given by "n_before". The number of dimensions
5749 * in the range is given by the number of output dimensions of "ma".
5750 * The number of dimensions that follow the range is given by "n_after".
5751 * If "has_denom" is set (to one),
5752 * then "src" and "dst" have an extra initial denominator.
5753 * "n_div_ma" is the number of existentials in "ma"
5754 * "n_div_bset" is the number of existentials in "src"
5755 * The resulting "dst" (which is assumed to have been allocated by
5756 * the caller) contains coefficients for both sets of existentials,
5757 * first those in "ma" and then those in "src".
5758 * f, c1, c2 and g are temporary objects that have been initialized
5759 * by the caller.
5761 * Let src represent the expression
5763 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5765 * and let ma represent the expressions
5767 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5769 * We start out with the following expression for dst:
5771 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5773 * with the multiplication factor f initially equal to 1
5774 * and f \sum_i b_i v_i kept separately.
5775 * For each x_i that we substitute, we multiply the numerator
5776 * (and denominator) of dst by c_1 = m_i and add the numerator
5777 * of the x_i expression multiplied by c_2 = f b_i,
5778 * after removing the common factors of c_1 and c_2.
5779 * The multiplication factor f also needs to be multiplied by c_1
5780 * for the next x_j, j > i.
5782 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5783 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5784 int n_div_ma, int n_div_bmap,
5785 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5787 int i;
5788 isl_size n_param, n_in, n_out;
5789 int o_dst, o_src;
5791 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5792 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5793 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5794 if (n_param < 0 || n_in < 0 || n_out < 0)
5795 return isl_stat_error;
5797 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5798 o_dst = o_src = has_denom + 1 + n_param + n_before;
5799 isl_seq_clr(dst + o_dst, n_in);
5800 o_dst += n_in;
5801 o_src += n_out;
5802 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5803 o_dst += n_after;
5804 o_src += n_after;
5805 isl_seq_clr(dst + o_dst, n_div_ma);
5806 o_dst += n_div_ma;
5807 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5809 isl_int_set_si(f, 1);
5811 for (i = 0; i < n_out; ++i) {
5812 int offset = has_denom + 1 + n_param + n_before + i;
5814 if (isl_int_is_zero(src[offset]))
5815 continue;
5816 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5817 isl_int_mul(c2, f, src[offset]);
5818 isl_int_gcd(g, c1, c2);
5819 isl_int_divexact(c1, c1, g);
5820 isl_int_divexact(c2, c2, g);
5822 isl_int_mul(f, f, c1);
5823 o_dst = has_denom;
5824 o_src = 1;
5825 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5826 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5827 o_dst += 1 + n_param;
5828 o_src += 1 + n_param;
5829 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5830 o_dst += n_before;
5831 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5832 c2, ma->u.p[i]->v->el + o_src, n_in);
5833 o_dst += n_in;
5834 o_src += n_in;
5835 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5836 o_dst += n_after;
5837 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5838 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5839 o_dst += n_div_ma;
5840 o_src += n_div_ma;
5841 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5842 if (has_denom)
5843 isl_int_mul(dst[0], dst[0], c1);
5846 return isl_stat_ok;
5849 /* Compute the pullback of "aff" by the function represented by "ma".
5850 * In other words, plug in "ma" in "aff". The result is an affine expression
5851 * defined over the domain space of "ma".
5853 * If "aff" is represented by
5855 * (a(p) + b x + c(divs))/d
5857 * and ma is represented by
5859 * x = D(p) + F(y) + G(divs')
5861 * then the result is
5863 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5865 * The divs in the local space of the input are similarly adjusted
5866 * through a call to isl_local_space_preimage_multi_aff.
5868 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5869 __isl_take isl_multi_aff *ma)
5871 isl_aff *res = NULL;
5872 isl_local_space *ls;
5873 isl_size n_div_aff, n_div_ma;
5874 isl_int f, c1, c2, g;
5876 ma = isl_multi_aff_align_divs(ma);
5877 if (!aff || !ma)
5878 goto error;
5880 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5881 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5882 if (n_div_aff < 0 || n_div_ma < 0)
5883 goto error;
5885 ls = isl_aff_get_domain_local_space(aff);
5886 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5887 res = isl_aff_alloc(ls);
5888 if (!res)
5889 goto error;
5891 isl_int_init(f);
5892 isl_int_init(c1);
5893 isl_int_init(c2);
5894 isl_int_init(g);
5896 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5897 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5898 res = isl_aff_free(res);
5900 isl_int_clear(f);
5901 isl_int_clear(c1);
5902 isl_int_clear(c2);
5903 isl_int_clear(g);
5905 isl_aff_free(aff);
5906 isl_multi_aff_free(ma);
5907 res = isl_aff_normalize(res);
5908 return res;
5909 error:
5910 isl_aff_free(aff);
5911 isl_multi_aff_free(ma);
5912 isl_aff_free(res);
5913 return NULL;
5916 /* Compute the pullback of "aff1" by the function represented by "aff2".
5917 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5918 * defined over the domain space of "aff1".
5920 * The domain of "aff1" should match the range of "aff2", which means
5921 * that it should be single-dimensional.
5923 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5924 __isl_take isl_aff *aff2)
5926 isl_multi_aff *ma;
5928 ma = isl_multi_aff_from_aff(aff2);
5929 return isl_aff_pullback_multi_aff(aff1, ma);
5932 /* Compute the pullback of "ma1" by the function represented by "ma2".
5933 * In other words, plug in "ma2" in "ma1".
5935 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5936 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5938 int i;
5939 isl_space *space = NULL;
5941 isl_multi_aff_align_params_bin(&ma1, &ma2);
5942 ma2 = isl_multi_aff_align_divs(ma2);
5943 ma1 = isl_multi_aff_cow(ma1);
5944 if (!ma1 || !ma2)
5945 goto error;
5947 space = isl_space_join(isl_multi_aff_get_space(ma2),
5948 isl_multi_aff_get_space(ma1));
5950 for (i = 0; i < ma1->n; ++i) {
5951 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5952 isl_multi_aff_copy(ma2));
5953 if (!ma1->u.p[i])
5954 goto error;
5957 ma1 = isl_multi_aff_reset_space(ma1, space);
5958 isl_multi_aff_free(ma2);
5959 return ma1;
5960 error:
5961 isl_space_free(space);
5962 isl_multi_aff_free(ma2);
5963 isl_multi_aff_free(ma1);
5964 return NULL;
5967 /* Extend the local space of "dst" to include the divs
5968 * in the local space of "src".
5970 * If "src" does not have any divs or if the local spaces of "dst" and
5971 * "src" are the same, then no extension is required.
5973 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5974 __isl_keep isl_aff *src)
5976 isl_ctx *ctx;
5977 isl_size src_n_div, dst_n_div;
5978 int *exp1 = NULL;
5979 int *exp2 = NULL;
5980 isl_bool equal;
5981 isl_mat *div;
5983 if (!src || !dst)
5984 return isl_aff_free(dst);
5986 ctx = isl_aff_get_ctx(src);
5987 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5988 if (equal < 0)
5989 return isl_aff_free(dst);
5990 if (!equal)
5991 isl_die(ctx, isl_error_invalid,
5992 "spaces don't match", goto error);
5994 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
5995 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
5996 if (src_n_div == 0)
5997 return dst;
5998 equal = isl_local_space_is_equal(src->ls, dst->ls);
5999 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6000 return isl_aff_free(dst);
6001 if (equal)
6002 return dst;
6004 exp1 = isl_alloc_array(ctx, int, src_n_div);
6005 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6006 if (!exp1 || (dst_n_div && !exp2))
6007 goto error;
6009 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6010 dst = isl_aff_expand_divs(dst, div, exp2);
6011 free(exp1);
6012 free(exp2);
6014 return dst;
6015 error:
6016 free(exp1);
6017 free(exp2);
6018 return isl_aff_free(dst);
6021 /* Adjust the local spaces of the affine expressions in "maff"
6022 * such that they all have the save divs.
6024 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6025 __isl_take isl_multi_aff *maff)
6027 int i;
6029 if (!maff)
6030 return NULL;
6031 if (maff->n == 0)
6032 return maff;
6033 maff = isl_multi_aff_cow(maff);
6034 if (!maff)
6035 return NULL;
6037 for (i = 1; i < maff->n; ++i)
6038 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
6039 for (i = 1; i < maff->n; ++i) {
6040 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
6041 if (!maff->u.p[i])
6042 return isl_multi_aff_free(maff);
6045 return maff;
6048 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6050 aff = isl_aff_cow(aff);
6051 if (!aff)
6052 return NULL;
6054 aff->ls = isl_local_space_lift(aff->ls);
6055 if (!aff->ls)
6056 return isl_aff_free(aff);
6058 return aff;
6061 /* Lift "maff" to a space with extra dimensions such that the result
6062 * has no more existentially quantified variables.
6063 * If "ls" is not NULL, then *ls is assigned the local space that lies
6064 * at the basis of the lifting applied to "maff".
6066 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6067 __isl_give isl_local_space **ls)
6069 int i;
6070 isl_space *space;
6071 isl_size n_div;
6073 if (ls)
6074 *ls = NULL;
6076 if (!maff)
6077 return NULL;
6079 if (maff->n == 0) {
6080 if (ls) {
6081 isl_space *space = isl_multi_aff_get_domain_space(maff);
6082 *ls = isl_local_space_from_space(space);
6083 if (!*ls)
6084 return isl_multi_aff_free(maff);
6086 return maff;
6089 maff = isl_multi_aff_cow(maff);
6090 maff = isl_multi_aff_align_divs(maff);
6091 if (!maff)
6092 return NULL;
6094 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
6095 if (n_div < 0)
6096 return isl_multi_aff_free(maff);
6097 space = isl_multi_aff_get_space(maff);
6098 space = isl_space_lift(isl_space_domain(space), n_div);
6099 space = isl_space_extend_domain_with_range(space,
6100 isl_multi_aff_get_space(maff));
6101 if (!space)
6102 return isl_multi_aff_free(maff);
6103 isl_space_free(maff->space);
6104 maff->space = space;
6106 if (ls) {
6107 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
6108 if (!*ls)
6109 return isl_multi_aff_free(maff);
6112 for (i = 0; i < maff->n; ++i) {
6113 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
6114 if (!maff->u.p[i])
6115 goto error;
6118 return maff;
6119 error:
6120 if (ls)
6121 isl_local_space_free(*ls);
6122 return isl_multi_aff_free(maff);
6125 #undef TYPE
6126 #define TYPE isl_pw_multi_aff
6127 static
6128 #include "check_type_range_templ.c"
6130 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6132 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6133 __isl_keep isl_pw_multi_aff *pma, int pos)
6135 int i;
6136 isl_size n_out;
6137 isl_space *space;
6138 isl_pw_aff *pa;
6140 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6141 return NULL;
6143 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6144 if (n_out < 0)
6145 return NULL;
6147 space = isl_pw_multi_aff_get_space(pma);
6148 space = isl_space_drop_dims(space, isl_dim_out,
6149 pos + 1, n_out - pos - 1);
6150 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6152 pa = isl_pw_aff_alloc_size(space, pma->n);
6153 for (i = 0; i < pma->n; ++i) {
6154 isl_aff *aff;
6155 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6156 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6159 return pa;
6162 /* This is an alternative name for the function above.
6164 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6165 __isl_keep isl_pw_multi_aff *pma, int pos)
6167 return isl_pw_multi_aff_get_at(pma, pos);
6170 /* Return an isl_pw_multi_aff with the given "set" as domain and
6171 * an unnamed zero-dimensional range.
6173 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6174 __isl_take isl_set *set)
6176 isl_multi_aff *ma;
6177 isl_space *space;
6179 space = isl_set_get_space(set);
6180 space = isl_space_from_domain(space);
6181 ma = isl_multi_aff_zero(space);
6182 return isl_pw_multi_aff_alloc(set, ma);
6185 /* Add an isl_pw_multi_aff with the given "set" as domain and
6186 * an unnamed zero-dimensional range to *user.
6188 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6189 void *user)
6191 isl_union_pw_multi_aff **upma = user;
6192 isl_pw_multi_aff *pma;
6194 pma = isl_pw_multi_aff_from_domain(set);
6195 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6197 return isl_stat_ok;
6200 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6201 * an unnamed zero-dimensional range.
6203 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6204 __isl_take isl_union_set *uset)
6206 isl_space *space;
6207 isl_union_pw_multi_aff *upma;
6209 if (!uset)
6210 return NULL;
6212 space = isl_union_set_get_space(uset);
6213 upma = isl_union_pw_multi_aff_empty(space);
6215 if (isl_union_set_foreach_set(uset,
6216 &add_pw_multi_aff_from_domain, &upma) < 0)
6217 goto error;
6219 isl_union_set_free(uset);
6220 return upma;
6221 error:
6222 isl_union_set_free(uset);
6223 isl_union_pw_multi_aff_free(upma);
6224 return NULL;
6227 /* Local data for bin_entry and the callback "fn".
6229 struct isl_union_pw_multi_aff_bin_data {
6230 isl_union_pw_multi_aff *upma2;
6231 isl_union_pw_multi_aff *res;
6232 isl_pw_multi_aff *pma;
6233 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6236 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6237 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6239 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6241 struct isl_union_pw_multi_aff_bin_data *data = user;
6242 isl_stat r;
6244 data->pma = pma;
6245 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6246 data->fn, data);
6247 isl_pw_multi_aff_free(pma);
6249 return r;
6252 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6253 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6254 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6255 * as *entry. The callback should adjust data->res if desired.
6257 static __isl_give isl_union_pw_multi_aff *bin_op(
6258 __isl_take isl_union_pw_multi_aff *upma1,
6259 __isl_take isl_union_pw_multi_aff *upma2,
6260 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6262 isl_space *space;
6263 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6265 space = isl_union_pw_multi_aff_get_space(upma2);
6266 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6267 space = isl_union_pw_multi_aff_get_space(upma1);
6268 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6270 if (!upma1 || !upma2)
6271 goto error;
6273 data.upma2 = upma2;
6274 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6275 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6276 &bin_entry, &data) < 0)
6277 goto error;
6279 isl_union_pw_multi_aff_free(upma1);
6280 isl_union_pw_multi_aff_free(upma2);
6281 return data.res;
6282 error:
6283 isl_union_pw_multi_aff_free(upma1);
6284 isl_union_pw_multi_aff_free(upma2);
6285 isl_union_pw_multi_aff_free(data.res);
6286 return NULL;
6289 /* Given two isl_pw_multi_affs A -> B and C -> D,
6290 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6292 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6293 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6295 isl_space *space;
6297 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6298 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6299 isl_pw_multi_aff_get_space(pma2));
6300 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6301 &isl_multi_aff_range_product);
6304 /* Given two isl_pw_multi_affs A -> B and C -> D,
6305 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6307 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6308 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6310 isl_space *space;
6312 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6313 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6314 isl_pw_multi_aff_get_space(pma2));
6315 space = isl_space_flatten_range(space);
6316 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6317 &isl_multi_aff_flat_range_product);
6320 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6321 * to compute some form of range product and add the result to data->res.
6323 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6324 __isl_give isl_pw_multi_aff *(*range_product)(
6325 __isl_take isl_pw_multi_aff *pma1,
6326 __isl_take isl_pw_multi_aff *pma2),
6327 void *user)
6329 struct isl_union_pw_multi_aff_bin_data *data = user;
6330 isl_bool match;
6331 isl_space *space1, *space2;
6333 space1 = isl_pw_multi_aff_peek_space(data->pma);
6334 space2 = isl_pw_multi_aff_peek_space(pma2);
6335 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6336 space2, isl_dim_in);
6337 if (match < 0 || !match) {
6338 isl_pw_multi_aff_free(pma2);
6339 return match < 0 ? isl_stat_error : isl_stat_ok;
6342 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6344 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6346 return isl_stat_ok;
6349 /* If data->pma and "pma2" have the same domain space, then compute
6350 * their flat range product and add the result to data->res.
6352 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6353 void *user)
6355 return gen_range_product_entry(pma2,
6356 &isl_pw_multi_aff_flat_range_product, user);
6359 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6360 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6362 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6363 __isl_take isl_union_pw_multi_aff *upma1,
6364 __isl_take isl_union_pw_multi_aff *upma2)
6366 return bin_op(upma1, upma2, &flat_range_product_entry);
6369 /* If data->pma and "pma2" have the same domain space, then compute
6370 * their range product and add the result to data->res.
6372 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6373 void *user)
6375 return gen_range_product_entry(pma2,
6376 &isl_pw_multi_aff_range_product, user);
6379 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6380 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6382 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6383 __isl_take isl_union_pw_multi_aff *upma1,
6384 __isl_take isl_union_pw_multi_aff *upma2)
6386 return bin_op(upma1, upma2, &range_product_entry);
6389 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6390 * The parameters are assumed to have been aligned.
6392 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6393 * except that it works on two different isl_pw_* types.
6395 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6396 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6397 __isl_take isl_pw_aff *pa)
6399 int i, j, n;
6400 isl_pw_multi_aff *res = NULL;
6402 if (!pma || !pa)
6403 goto error;
6405 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6406 pa->dim, isl_dim_in))
6407 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6408 "domains don't match", goto error);
6409 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6410 goto error;
6412 n = pma->n * pa->n;
6413 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6415 for (i = 0; i < pma->n; ++i) {
6416 for (j = 0; j < pa->n; ++j) {
6417 isl_set *common;
6418 isl_multi_aff *res_ij;
6419 int empty;
6421 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6422 isl_set_copy(pa->p[j].set));
6423 empty = isl_set_plain_is_empty(common);
6424 if (empty < 0 || empty) {
6425 isl_set_free(common);
6426 if (empty < 0)
6427 goto error;
6428 continue;
6431 res_ij = isl_multi_aff_set_aff(
6432 isl_multi_aff_copy(pma->p[i].maff), pos,
6433 isl_aff_copy(pa->p[j].aff));
6434 res_ij = isl_multi_aff_gist(res_ij,
6435 isl_set_copy(common));
6437 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6441 isl_pw_multi_aff_free(pma);
6442 isl_pw_aff_free(pa);
6443 return res;
6444 error:
6445 isl_pw_multi_aff_free(pma);
6446 isl_pw_aff_free(pa);
6447 return isl_pw_multi_aff_free(res);
6450 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6452 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6453 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6454 __isl_take isl_pw_aff *pa)
6456 isl_bool equal_params;
6458 if (!pma || !pa)
6459 goto error;
6460 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6461 if (equal_params < 0)
6462 goto error;
6463 if (equal_params)
6464 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6465 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6466 isl_pw_aff_check_named_params(pa) < 0)
6467 goto error;
6468 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6469 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6470 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6471 error:
6472 isl_pw_multi_aff_free(pma);
6473 isl_pw_aff_free(pa);
6474 return NULL;
6477 /* Do the parameters of "pa" match those of "space"?
6479 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6480 __isl_keep isl_space *space)
6482 isl_space *pa_space;
6483 isl_bool match;
6485 if (!pa || !space)
6486 return isl_bool_error;
6488 pa_space = isl_pw_aff_get_space(pa);
6490 match = isl_space_has_equal_params(space, pa_space);
6492 isl_space_free(pa_space);
6493 return match;
6496 /* Check that the domain space of "pa" matches "space".
6498 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6499 __isl_keep isl_space *space)
6501 isl_space *pa_space;
6502 isl_bool match;
6504 if (!pa || !space)
6505 return isl_stat_error;
6507 pa_space = isl_pw_aff_get_space(pa);
6509 match = isl_space_has_equal_params(space, pa_space);
6510 if (match < 0)
6511 goto error;
6512 if (!match)
6513 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6514 "parameters don't match", goto error);
6515 match = isl_space_tuple_is_equal(space, isl_dim_in,
6516 pa_space, isl_dim_in);
6517 if (match < 0)
6518 goto error;
6519 if (!match)
6520 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6521 "domains don't match", goto error);
6522 isl_space_free(pa_space);
6523 return isl_stat_ok;
6524 error:
6525 isl_space_free(pa_space);
6526 return isl_stat_error;
6529 #undef BASE
6530 #define BASE pw_aff
6531 #undef DOMBASE
6532 #define DOMBASE set
6534 #include <isl_multi_explicit_domain.c>
6535 #include <isl_multi_pw_aff_explicit_domain.c>
6536 #include <isl_multi_templ.c>
6537 #include <isl_multi_add_constant_templ.c>
6538 #include <isl_multi_apply_set.c>
6539 #include <isl_multi_arith_templ.c>
6540 #include <isl_multi_bind_templ.c>
6541 #include <isl_multi_bind_domain_templ.c>
6542 #include <isl_multi_coalesce.c>
6543 #include <isl_multi_domain_templ.c>
6544 #include <isl_multi_dim_id_templ.c>
6545 #include <isl_multi_dims.c>
6546 #include <isl_multi_from_base_templ.c>
6547 #include <isl_multi_gist.c>
6548 #include <isl_multi_hash.c>
6549 #include <isl_multi_identity_templ.c>
6550 #include <isl_multi_align_set.c>
6551 #include <isl_multi_insert_domain_templ.c>
6552 #include <isl_multi_intersect.c>
6553 #include <isl_multi_min_max_templ.c>
6554 #include <isl_multi_move_dims_templ.c>
6555 #include <isl_multi_nan_templ.c>
6556 #include <isl_multi_param_templ.c>
6557 #include <isl_multi_product_templ.c>
6558 #include <isl_multi_splice_templ.c>
6559 #include <isl_multi_tuple_id_templ.c>
6560 #include <isl_multi_union_add_templ.c>
6561 #include <isl_multi_zero_templ.c>
6562 #include <isl_multi_unbind_params_templ.c>
6564 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6565 * with this explicit domain.
6567 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6568 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6570 isl_set *dom;
6572 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6573 return map;
6575 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6576 map = isl_map_intersect_domain(map, dom);
6578 return map;
6581 /* Are all elements of "mpa" piecewise constants?
6583 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6585 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6588 /* Does "mpa" have a non-trivial explicit domain?
6590 * The explicit domain, if present, is trivial if it represents
6591 * an (obviously) universe set.
6593 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6594 __isl_keep isl_multi_pw_aff *mpa)
6596 if (!mpa)
6597 return isl_bool_error;
6598 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6599 return isl_bool_false;
6600 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6603 #undef BASE
6604 #define BASE set
6606 #include "isl_opt_mpa_templ.c"
6608 /* Compute the minima of the set dimensions as a function of the
6609 * parameters, but independently of the other set dimensions.
6611 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6613 return set_opt_mpa(set, &isl_set_dim_min);
6616 /* Compute the maxima of the set dimensions as a function of the
6617 * parameters, but independently of the other set dimensions.
6619 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6621 return set_opt_mpa(set, &isl_set_dim_max);
6624 #undef BASE
6625 #define BASE map
6627 #include "isl_opt_mpa_templ.c"
6629 /* Compute the minima of the output dimensions as a function of the
6630 * parameters and input dimensions, but independently of
6631 * the other output dimensions.
6633 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6635 return map_opt_mpa(map, &isl_map_dim_min);
6638 /* Compute the maxima of the output dimensions as a function of the
6639 * parameters and input dimensions, but independently of
6640 * the other output dimensions.
6642 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6644 return map_opt_mpa(map, &isl_map_dim_max);
6647 /* Scale the elements of "pma" by the corresponding elements of "mv".
6649 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6650 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6652 int i;
6653 isl_bool equal_params;
6655 pma = isl_pw_multi_aff_cow(pma);
6656 if (!pma || !mv)
6657 goto error;
6658 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6659 mv->space, isl_dim_set))
6660 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6661 "spaces don't match", goto error);
6662 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6663 if (equal_params < 0)
6664 goto error;
6665 if (!equal_params) {
6666 pma = isl_pw_multi_aff_align_params(pma,
6667 isl_multi_val_get_space(mv));
6668 mv = isl_multi_val_align_params(mv,
6669 isl_pw_multi_aff_get_space(pma));
6670 if (!pma || !mv)
6671 goto error;
6674 for (i = 0; i < pma->n; ++i) {
6675 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6676 isl_multi_val_copy(mv));
6677 if (!pma->p[i].maff)
6678 goto error;
6681 isl_multi_val_free(mv);
6682 return pma;
6683 error:
6684 isl_multi_val_free(mv);
6685 isl_pw_multi_aff_free(pma);
6686 return NULL;
6689 /* This function is called for each entry of an isl_union_pw_multi_aff.
6690 * If the space of the entry matches that of data->mv,
6691 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6692 * Otherwise, return an empty isl_pw_multi_aff.
6694 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6695 __isl_take isl_pw_multi_aff *pma, void *user)
6697 isl_multi_val *mv = user;
6699 if (!pma)
6700 return NULL;
6701 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6702 mv->space, isl_dim_set)) {
6703 isl_space *space = isl_pw_multi_aff_get_space(pma);
6704 isl_pw_multi_aff_free(pma);
6705 return isl_pw_multi_aff_empty(space);
6708 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6711 /* Scale the elements of "upma" by the corresponding elements of "mv",
6712 * for those entries that match the space of "mv".
6714 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6715 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6717 struct isl_union_pw_multi_aff_transform_control control = {
6718 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6719 .fn_user = mv,
6722 upma = isl_union_pw_multi_aff_align_params(upma,
6723 isl_multi_val_get_space(mv));
6724 mv = isl_multi_val_align_params(mv,
6725 isl_union_pw_multi_aff_get_space(upma));
6726 if (!upma || !mv)
6727 goto error;
6729 return isl_union_pw_multi_aff_transform(upma, &control);
6731 isl_multi_val_free(mv);
6732 return upma;
6733 error:
6734 isl_multi_val_free(mv);
6735 isl_union_pw_multi_aff_free(upma);
6736 return NULL;
6739 /* Construct and return a piecewise multi affine expression
6740 * in the given space with value zero in each of the output dimensions and
6741 * a universe domain.
6743 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6745 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6748 /* Construct and return a piecewise multi affine expression
6749 * that is equal to the given piecewise affine expression.
6751 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6752 __isl_take isl_pw_aff *pa)
6754 int i;
6755 isl_space *space;
6756 isl_pw_multi_aff *pma;
6758 if (!pa)
6759 return NULL;
6761 space = isl_pw_aff_get_space(pa);
6762 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6764 for (i = 0; i < pa->n; ++i) {
6765 isl_set *set;
6766 isl_multi_aff *ma;
6768 set = isl_set_copy(pa->p[i].set);
6769 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6770 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6773 isl_pw_aff_free(pa);
6774 return pma;
6777 /* Construct and return a piecewise multi affine expression
6778 * that is equal to the given multi piecewise affine expression
6779 * on the shared domain of the piecewise affine expressions,
6780 * in the special case of a 0D multi piecewise affine expression.
6782 * Create a piecewise multi affine expression with the explicit domain of
6783 * the 0D multi piecewise affine expression as domain.
6785 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6786 __isl_take isl_multi_pw_aff *mpa)
6788 isl_space *space;
6789 isl_set *dom;
6790 isl_multi_aff *ma;
6792 space = isl_multi_pw_aff_get_space(mpa);
6793 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6794 isl_multi_pw_aff_free(mpa);
6796 ma = isl_multi_aff_zero(space);
6797 return isl_pw_multi_aff_alloc(dom, ma);
6800 /* Construct and return a piecewise multi affine expression
6801 * that is equal to the given multi piecewise affine expression
6802 * on the shared domain of the piecewise affine expressions.
6804 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6805 __isl_take isl_multi_pw_aff *mpa)
6807 int i;
6808 isl_space *space;
6809 isl_pw_aff *pa;
6810 isl_pw_multi_aff *pma;
6812 if (!mpa)
6813 return NULL;
6815 if (mpa->n == 0)
6816 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6818 space = isl_multi_pw_aff_get_space(mpa);
6819 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6820 pma = isl_pw_multi_aff_from_pw_aff(pa);
6822 for (i = 1; i < mpa->n; ++i) {
6823 isl_pw_multi_aff *pma_i;
6825 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6826 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6827 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6830 pma = isl_pw_multi_aff_reset_space(pma, space);
6832 isl_multi_pw_aff_free(mpa);
6833 return pma;
6836 /* Convenience function that constructs an isl_multi_pw_aff
6837 * directly from an isl_aff.
6839 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6841 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6844 /* Construct and return a multi piecewise affine expression
6845 * that is equal to the given multi affine expression.
6847 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6848 __isl_take isl_multi_aff *ma)
6850 int i;
6851 isl_size n;
6852 isl_multi_pw_aff *mpa;
6854 n = isl_multi_aff_dim(ma, isl_dim_out);
6855 if (n < 0)
6856 ma = isl_multi_aff_free(ma);
6857 if (!ma)
6858 return NULL;
6860 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6862 for (i = 0; i < n; ++i) {
6863 isl_pw_aff *pa;
6865 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6866 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6869 isl_multi_aff_free(ma);
6870 return mpa;
6873 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6874 * but is considered as a function on an isl_multi_aff when exported.
6876 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
6877 __isl_take isl_multi_aff *ma)
6879 return isl_multi_pw_aff_from_multi_aff(ma);
6882 /* Construct and return a multi piecewise affine expression
6883 * that is equal to the given piecewise multi affine expression.
6885 * If the resulting multi piecewise affine expression has
6886 * an explicit domain, then assign it the domain of the input.
6887 * In other cases, the domain is stored in the individual elements.
6889 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6890 __isl_take isl_pw_multi_aff *pma)
6892 int i;
6893 isl_size n;
6894 isl_space *space;
6895 isl_multi_pw_aff *mpa;
6897 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6898 if (n < 0)
6899 pma = isl_pw_multi_aff_free(pma);
6900 space = isl_pw_multi_aff_get_space(pma);
6901 mpa = isl_multi_pw_aff_alloc(space);
6903 for (i = 0; i < n; ++i) {
6904 isl_pw_aff *pa;
6906 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6907 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6909 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6910 isl_set *dom;
6912 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6913 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6916 isl_pw_multi_aff_free(pma);
6917 return mpa;
6920 /* This function performs the same operation as
6921 * isl_multi_pw_aff_from_pw_multi_aff,
6922 * but is considered as a function on an isl_pw_multi_aff when exported.
6924 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
6925 __isl_take isl_pw_multi_aff *pma)
6927 return isl_multi_pw_aff_from_pw_multi_aff(pma);
6930 /* Do "pa1" and "pa2" represent the same function?
6932 * We first check if they are obviously equal.
6933 * If not, we convert them to maps and check if those are equal.
6935 * If "pa1" or "pa2" contain any NaNs, then they are considered
6936 * not to be the same. A NaN is not equal to anything, not even
6937 * to another NaN.
6939 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6940 __isl_keep isl_pw_aff *pa2)
6942 isl_bool equal;
6943 isl_bool has_nan;
6944 isl_map *map1, *map2;
6946 if (!pa1 || !pa2)
6947 return isl_bool_error;
6949 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6950 if (equal < 0 || equal)
6951 return equal;
6952 has_nan = either_involves_nan(pa1, pa2);
6953 if (has_nan < 0)
6954 return isl_bool_error;
6955 if (has_nan)
6956 return isl_bool_false;
6958 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6959 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6960 equal = isl_map_is_equal(map1, map2);
6961 isl_map_free(map1);
6962 isl_map_free(map2);
6964 return equal;
6967 /* Do "mpa1" and "mpa2" represent the same function?
6969 * Note that we cannot convert the entire isl_multi_pw_aff
6970 * to a map because the domains of the piecewise affine expressions
6971 * may not be the same.
6973 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6974 __isl_keep isl_multi_pw_aff *mpa2)
6976 int i;
6977 isl_bool equal, equal_params;
6979 if (!mpa1 || !mpa2)
6980 return isl_bool_error;
6982 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6983 if (equal_params < 0)
6984 return isl_bool_error;
6985 if (!equal_params) {
6986 if (!isl_space_has_named_params(mpa1->space))
6987 return isl_bool_false;
6988 if (!isl_space_has_named_params(mpa2->space))
6989 return isl_bool_false;
6990 mpa1 = isl_multi_pw_aff_copy(mpa1);
6991 mpa2 = isl_multi_pw_aff_copy(mpa2);
6992 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6993 isl_multi_pw_aff_get_space(mpa2));
6994 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6995 isl_multi_pw_aff_get_space(mpa1));
6996 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6997 isl_multi_pw_aff_free(mpa1);
6998 isl_multi_pw_aff_free(mpa2);
6999 return equal;
7002 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7003 if (equal < 0 || !equal)
7004 return equal;
7006 for (i = 0; i < mpa1->n; ++i) {
7007 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7008 if (equal < 0 || !equal)
7009 return equal;
7012 return isl_bool_true;
7015 /* Do "pma1" and "pma2" represent the same function?
7017 * First check if they are obviously equal.
7018 * If not, then convert them to maps and check if those are equal.
7020 * If "pa1" or "pa2" contain any NaNs, then they are considered
7021 * not to be the same. A NaN is not equal to anything, not even
7022 * to another NaN.
7024 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7025 __isl_keep isl_pw_multi_aff *pma2)
7027 isl_bool equal;
7028 isl_bool has_nan;
7029 isl_map *map1, *map2;
7031 if (!pma1 || !pma2)
7032 return isl_bool_error;
7034 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7035 if (equal < 0 || equal)
7036 return equal;
7037 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7038 if (has_nan >= 0 && !has_nan)
7039 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7040 if (has_nan < 0 || has_nan)
7041 return isl_bool_not(has_nan);
7043 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7044 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7045 equal = isl_map_is_equal(map1, map2);
7046 isl_map_free(map1);
7047 isl_map_free(map2);
7049 return equal;
7052 /* Compute the pullback of "mpa" by the function represented by "ma".
7053 * In other words, plug in "ma" in "mpa".
7055 * The parameters of "mpa" and "ma" are assumed to have been aligned.
7057 * If "mpa" has an explicit domain, then it is this domain
7058 * that needs to undergo a pullback, i.e., a preimage.
7060 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
7061 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7063 int i;
7064 isl_space *space = NULL;
7066 mpa = isl_multi_pw_aff_cow(mpa);
7067 if (!mpa || !ma)
7068 goto error;
7070 space = isl_space_join(isl_multi_aff_get_space(ma),
7071 isl_multi_pw_aff_get_space(mpa));
7072 if (!space)
7073 goto error;
7075 for (i = 0; i < mpa->n; ++i) {
7076 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
7077 isl_multi_aff_copy(ma));
7078 if (!mpa->u.p[i])
7079 goto error;
7081 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7082 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
7083 isl_multi_aff_copy(ma));
7084 if (!mpa->u.dom)
7085 goto error;
7088 isl_multi_aff_free(ma);
7089 isl_space_free(mpa->space);
7090 mpa->space = space;
7091 return mpa;
7092 error:
7093 isl_space_free(space);
7094 isl_multi_pw_aff_free(mpa);
7095 isl_multi_aff_free(ma);
7096 return NULL;
7099 /* Compute the pullback of "mpa" by the function represented by "ma".
7100 * In other words, plug in "ma" in "mpa".
7102 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
7103 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7105 isl_bool equal_params;
7107 if (!mpa || !ma)
7108 goto error;
7109 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
7110 if (equal_params < 0)
7111 goto error;
7112 if (equal_params)
7113 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7114 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
7115 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
7116 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7117 error:
7118 isl_multi_pw_aff_free(mpa);
7119 isl_multi_aff_free(ma);
7120 return NULL;
7123 /* Compute the pullback of "mpa" by the function represented by "pma".
7124 * In other words, plug in "pma" in "mpa".
7126 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7128 * If "mpa" has an explicit domain, then it is this domain
7129 * that needs to undergo a pullback, i.e., a preimage.
7131 static __isl_give isl_multi_pw_aff *
7132 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7133 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7135 int i;
7136 isl_space *space = NULL;
7138 mpa = isl_multi_pw_aff_cow(mpa);
7139 if (!mpa || !pma)
7140 goto error;
7142 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
7143 isl_multi_pw_aff_get_space(mpa));
7145 for (i = 0; i < mpa->n; ++i) {
7146 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7147 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
7148 if (!mpa->u.p[i])
7149 goto error;
7151 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7152 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
7153 isl_pw_multi_aff_copy(pma));
7154 if (!mpa->u.dom)
7155 goto error;
7158 isl_pw_multi_aff_free(pma);
7159 isl_space_free(mpa->space);
7160 mpa->space = space;
7161 return mpa;
7162 error:
7163 isl_space_free(space);
7164 isl_multi_pw_aff_free(mpa);
7165 isl_pw_multi_aff_free(pma);
7166 return NULL;
7169 /* Compute the pullback of "mpa" by the function represented by "pma".
7170 * In other words, plug in "pma" in "mpa".
7172 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
7173 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7175 isl_bool equal_params;
7177 if (!mpa || !pma)
7178 goto error;
7179 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
7180 if (equal_params < 0)
7181 goto error;
7182 if (equal_params)
7183 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7184 mpa = isl_multi_pw_aff_align_params(mpa,
7185 isl_pw_multi_aff_get_space(pma));
7186 pma = isl_pw_multi_aff_align_params(pma,
7187 isl_multi_pw_aff_get_space(mpa));
7188 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7189 error:
7190 isl_multi_pw_aff_free(mpa);
7191 isl_pw_multi_aff_free(pma);
7192 return NULL;
7195 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7196 * with the domain of "aff". The domain of the result is the same
7197 * as that of "mpa".
7198 * "mpa" and "aff" are assumed to have been aligned.
7200 * We first extract the parametric constant from "aff", defined
7201 * over the correct domain.
7202 * Then we add the appropriate combinations of the members of "mpa".
7203 * Finally, we add the integer divisions through recursive calls.
7205 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7206 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7208 int i;
7209 isl_size n_in, n_div, n_mpa_in;
7210 isl_space *space;
7211 isl_val *v;
7212 isl_pw_aff *pa;
7213 isl_aff *tmp;
7215 n_in = isl_aff_dim(aff, isl_dim_in);
7216 n_div = isl_aff_dim(aff, isl_dim_div);
7217 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7218 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7219 goto error;
7221 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7222 tmp = isl_aff_copy(aff);
7223 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7224 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7225 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7226 tmp = isl_aff_reset_domain_space(tmp, space);
7227 pa = isl_pw_aff_from_aff(tmp);
7229 for (i = 0; i < n_in; ++i) {
7230 isl_pw_aff *pa_i;
7232 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7233 continue;
7234 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7235 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7236 pa_i = isl_pw_aff_scale_val(pa_i, v);
7237 pa = isl_pw_aff_add(pa, pa_i);
7240 for (i = 0; i < n_div; ++i) {
7241 isl_aff *div;
7242 isl_pw_aff *pa_i;
7244 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7245 continue;
7246 div = isl_aff_get_div(aff, i);
7247 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7248 isl_multi_pw_aff_copy(mpa), div);
7249 pa_i = isl_pw_aff_floor(pa_i);
7250 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7251 pa_i = isl_pw_aff_scale_val(pa_i, v);
7252 pa = isl_pw_aff_add(pa, pa_i);
7255 isl_multi_pw_aff_free(mpa);
7256 isl_aff_free(aff);
7258 return pa;
7259 error:
7260 isl_multi_pw_aff_free(mpa);
7261 isl_aff_free(aff);
7262 return NULL;
7265 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7266 * with the domain of "aff". The domain of the result is the same
7267 * as that of "mpa".
7269 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7270 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7272 isl_bool equal_params;
7274 if (!aff || !mpa)
7275 goto error;
7276 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7277 if (equal_params < 0)
7278 goto error;
7279 if (equal_params)
7280 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7282 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7283 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7285 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7286 error:
7287 isl_aff_free(aff);
7288 isl_multi_pw_aff_free(mpa);
7289 return NULL;
7292 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7293 * with the domain of "pa". The domain of the result is the same
7294 * as that of "mpa".
7295 * "mpa" and "pa" are assumed to have been aligned.
7297 * We consider each piece in turn. Note that the domains of the
7298 * pieces are assumed to be disjoint and they remain disjoint
7299 * after taking the preimage (over the same function).
7301 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7302 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7304 isl_space *space;
7305 isl_pw_aff *res;
7306 int i;
7308 if (!mpa || !pa)
7309 goto error;
7311 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7312 isl_pw_aff_get_space(pa));
7313 res = isl_pw_aff_empty(space);
7315 for (i = 0; i < pa->n; ++i) {
7316 isl_pw_aff *pa_i;
7317 isl_set *domain;
7319 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7320 isl_multi_pw_aff_copy(mpa),
7321 isl_aff_copy(pa->p[i].aff));
7322 domain = isl_set_copy(pa->p[i].set);
7323 domain = isl_set_preimage_multi_pw_aff(domain,
7324 isl_multi_pw_aff_copy(mpa));
7325 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7326 res = isl_pw_aff_add_disjoint(res, pa_i);
7329 isl_pw_aff_free(pa);
7330 isl_multi_pw_aff_free(mpa);
7331 return res;
7332 error:
7333 isl_pw_aff_free(pa);
7334 isl_multi_pw_aff_free(mpa);
7335 return NULL;
7338 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7339 * with the domain of "pa". The domain of the result is the same
7340 * as that of "mpa".
7342 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7343 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7345 isl_bool equal_params;
7347 if (!pa || !mpa)
7348 goto error;
7349 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7350 if (equal_params < 0)
7351 goto error;
7352 if (equal_params)
7353 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7355 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7356 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7358 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7359 error:
7360 isl_pw_aff_free(pa);
7361 isl_multi_pw_aff_free(mpa);
7362 return NULL;
7365 /* Compute the pullback of "pa" by the function represented by "mpa".
7366 * In other words, plug in "mpa" in "pa".
7367 * "pa" and "mpa" are assumed to have been aligned.
7369 * The pullback is computed by applying "pa" to "mpa".
7371 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7372 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7374 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7377 /* Compute the pullback of "pa" by the function represented by "mpa".
7378 * In other words, plug in "mpa" in "pa".
7380 * The pullback is computed by applying "pa" to "mpa".
7382 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7383 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7385 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7388 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7389 * In other words, plug in "mpa2" in "mpa1".
7391 * We pullback each member of "mpa1" in turn.
7393 * If "mpa1" has an explicit domain, then it is this domain
7394 * that needs to undergo a pullback instead, i.e., a preimage.
7396 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7397 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7399 int i;
7400 isl_space *space = NULL;
7402 isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
7403 mpa1 = isl_multi_pw_aff_cow(mpa1);
7404 if (!mpa1 || !mpa2)
7405 goto error;
7407 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7408 isl_multi_pw_aff_get_space(mpa1));
7410 for (i = 0; i < mpa1->n; ++i) {
7411 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7412 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7413 if (!mpa1->u.p[i])
7414 goto error;
7417 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7418 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7419 isl_multi_pw_aff_copy(mpa2));
7420 if (!mpa1->u.dom)
7421 goto error;
7423 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7425 isl_multi_pw_aff_free(mpa2);
7426 return mpa1;
7427 error:
7428 isl_space_free(space);
7429 isl_multi_pw_aff_free(mpa1);
7430 isl_multi_pw_aff_free(mpa2);
7431 return NULL;
7434 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7435 * of "mpa1" and "mpa2" live in the same space, construct map space
7436 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7437 * with this map space as extract argument.
7439 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7440 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7441 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7442 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7444 int match;
7445 isl_space *space1, *space2;
7446 isl_map *res;
7448 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7449 isl_multi_pw_aff_get_space(mpa2));
7450 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7451 isl_multi_pw_aff_get_space(mpa1));
7452 if (!mpa1 || !mpa2)
7453 goto error;
7454 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7455 mpa2->space, isl_dim_out);
7456 if (match < 0)
7457 goto error;
7458 if (!match)
7459 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7460 "range spaces don't match", goto error);
7461 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7462 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7463 space1 = isl_space_map_from_domain_and_range(space1, space2);
7465 res = order(mpa1, mpa2, space1);
7466 isl_multi_pw_aff_free(mpa1);
7467 isl_multi_pw_aff_free(mpa2);
7468 return res;
7469 error:
7470 isl_multi_pw_aff_free(mpa1);
7471 isl_multi_pw_aff_free(mpa2);
7472 return NULL;
7475 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7476 * where the function values are equal. "space" is the space of the result.
7477 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7479 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7480 * in the sequences are equal.
7482 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7483 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7484 __isl_take isl_space *space)
7486 int i;
7487 isl_size n;
7488 isl_map *res;
7490 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7491 if (n < 0)
7492 space = isl_space_free(space);
7493 res = isl_map_universe(space);
7495 for (i = 0; i < n; ++i) {
7496 isl_pw_aff *pa1, *pa2;
7497 isl_map *map;
7499 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7500 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7501 map = isl_pw_aff_eq_map(pa1, pa2);
7502 res = isl_map_intersect(res, map);
7505 return res;
7508 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7509 * where the function values are equal.
7511 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7512 __isl_take isl_multi_pw_aff *mpa2)
7514 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7515 &isl_multi_pw_aff_eq_map_on_space);
7518 /* Intersect "map" with the result of applying "order"
7519 * on two copies of "mpa".
7521 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7522 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7523 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7524 __isl_take isl_multi_pw_aff *mpa2))
7526 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7529 /* Return the subset of "map" where the domain and the range
7530 * have equal "mpa" values.
7532 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7533 __isl_take isl_multi_pw_aff *mpa)
7535 return isl_map_order_at_multi_pw_aff(map, mpa,
7536 &isl_multi_pw_aff_eq_map);
7539 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7540 * where the function values of "mpa1" lexicographically satisfies
7541 * "strict_base"/"base" compared to that of "mpa2".
7542 * "space" is the space of the result.
7543 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7545 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7546 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7547 * when compared to the i-th element of "mpa2" while all previous elements are
7548 * pairwise equal.
7549 * In particular, if i corresponds to the final elements
7550 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7551 * for other values of i.
7552 * If "base" is a strict order, then "base" and "strict_base" are the same.
7554 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7555 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7556 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7557 __isl_take isl_pw_aff *pa2),
7558 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7559 __isl_take isl_pw_aff *pa2),
7560 __isl_take isl_space *space)
7562 int i;
7563 isl_size n;
7564 isl_map *res, *rest;
7566 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7567 if (n < 0)
7568 space = isl_space_free(space);
7569 res = isl_map_empty(isl_space_copy(space));
7570 rest = isl_map_universe(space);
7572 for (i = 0; i < n; ++i) {
7573 int last;
7574 isl_pw_aff *pa1, *pa2;
7575 isl_map *map;
7577 last = i == n - 1;
7579 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7580 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7581 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7582 map = isl_map_intersect(map, isl_map_copy(rest));
7583 res = isl_map_union(res, map);
7585 if (last)
7586 continue;
7588 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7589 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7590 map = isl_pw_aff_eq_map(pa1, pa2);
7591 rest = isl_map_intersect(rest, map);
7594 isl_map_free(rest);
7595 return res;
7598 #undef ORDER
7599 #define ORDER le
7600 #undef STRICT_ORDER
7601 #define STRICT_ORDER lt
7602 #include "isl_aff_lex_templ.c"
7604 #undef ORDER
7605 #define ORDER lt
7606 #undef STRICT_ORDER
7607 #define STRICT_ORDER lt
7608 #include "isl_aff_lex_templ.c"
7610 #undef ORDER
7611 #define ORDER ge
7612 #undef STRICT_ORDER
7613 #define STRICT_ORDER gt
7614 #include "isl_aff_lex_templ.c"
7616 #undef ORDER
7617 #define ORDER gt
7618 #undef STRICT_ORDER
7619 #define STRICT_ORDER gt
7620 #include "isl_aff_lex_templ.c"
7622 /* Compare two isl_affs.
7624 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7625 * than "aff2" and 0 if they are equal.
7627 * The order is fairly arbitrary. We do consider expressions that only involve
7628 * earlier dimensions as "smaller".
7630 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7632 int cmp;
7633 int last1, last2;
7635 if (aff1 == aff2)
7636 return 0;
7638 if (!aff1)
7639 return -1;
7640 if (!aff2)
7641 return 1;
7643 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7644 if (cmp != 0)
7645 return cmp;
7647 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7648 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7649 if (last1 != last2)
7650 return last1 - last2;
7652 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7655 /* Compare two isl_pw_affs.
7657 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7658 * than "pa2" and 0 if they are equal.
7660 * The order is fairly arbitrary. We do consider expressions that only involve
7661 * earlier dimensions as "smaller".
7663 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7664 __isl_keep isl_pw_aff *pa2)
7666 int i;
7667 int cmp;
7669 if (pa1 == pa2)
7670 return 0;
7672 if (!pa1)
7673 return -1;
7674 if (!pa2)
7675 return 1;
7677 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7678 if (cmp != 0)
7679 return cmp;
7681 if (pa1->n != pa2->n)
7682 return pa1->n - pa2->n;
7684 for (i = 0; i < pa1->n; ++i) {
7685 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7686 if (cmp != 0)
7687 return cmp;
7688 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7689 if (cmp != 0)
7690 return cmp;
7693 return 0;
7696 /* Return a piecewise affine expression that is equal to "v" on "domain".
7698 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7699 __isl_take isl_val *v)
7701 isl_space *space;
7702 isl_local_space *ls;
7703 isl_aff *aff;
7705 space = isl_set_get_space(domain);
7706 ls = isl_local_space_from_space(space);
7707 aff = isl_aff_val_on_domain(ls, v);
7709 return isl_pw_aff_alloc(domain, aff);
7712 /* Return a piecewise affine expression that is equal to the parameter
7713 * with identifier "id" on "domain".
7715 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7716 __isl_take isl_set *domain, __isl_take isl_id *id)
7718 isl_space *space;
7719 isl_aff *aff;
7721 space = isl_set_get_space(domain);
7722 space = isl_space_add_param_id(space, isl_id_copy(id));
7723 domain = isl_set_align_params(domain, isl_space_copy(space));
7724 aff = isl_aff_param_on_domain_space_id(space, id);
7726 return isl_pw_aff_alloc(domain, aff);
7729 /* Return a multi affine expression that is equal to "mv" on domain
7730 * space "space".
7732 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7733 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7735 int i;
7736 isl_size n;
7737 isl_space *space2;
7738 isl_local_space *ls;
7739 isl_multi_aff *ma;
7741 n = isl_multi_val_dim(mv, isl_dim_set);
7742 if (!space || n < 0)
7743 goto error;
7745 space2 = isl_multi_val_get_space(mv);
7746 space2 = isl_space_align_params(space2, isl_space_copy(space));
7747 space = isl_space_align_params(space, isl_space_copy(space2));
7748 space = isl_space_map_from_domain_and_range(space, space2);
7749 ma = isl_multi_aff_alloc(isl_space_copy(space));
7750 ls = isl_local_space_from_space(isl_space_domain(space));
7751 for (i = 0; i < n; ++i) {
7752 isl_val *v;
7753 isl_aff *aff;
7755 v = isl_multi_val_get_val(mv, i);
7756 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7757 ma = isl_multi_aff_set_aff(ma, i, aff);
7759 isl_local_space_free(ls);
7761 isl_multi_val_free(mv);
7762 return ma;
7763 error:
7764 isl_space_free(space);
7765 isl_multi_val_free(mv);
7766 return NULL;
7769 /* This is an alternative name for the function above.
7771 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7772 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7774 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7777 /* This function performs the same operation as
7778 * isl_multi_aff_multi_val_on_domain_space,
7779 * but is considered as a function on an isl_space when exported.
7781 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7782 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7784 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7787 /* Return a piecewise multi-affine expression
7788 * that is equal to "mv" on "domain".
7790 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7791 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7793 isl_space *space;
7794 isl_multi_aff *ma;
7796 space = isl_set_get_space(domain);
7797 ma = isl_multi_aff_multi_val_on_space(space, mv);
7799 return isl_pw_multi_aff_alloc(domain, ma);
7802 /* This function performs the same operation as
7803 * isl_pw_multi_aff_multi_val_on_domain,
7804 * but is considered as a function on an isl_set when exported.
7806 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7807 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7809 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7812 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7813 * mv is the value that should be attained on each domain set
7814 * res collects the results
7816 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7817 isl_multi_val *mv;
7818 isl_union_pw_multi_aff *res;
7821 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7822 * and add it to data->res.
7824 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7825 void *user)
7827 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7828 isl_pw_multi_aff *pma;
7829 isl_multi_val *mv;
7831 mv = isl_multi_val_copy(data->mv);
7832 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7833 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7835 return data->res ? isl_stat_ok : isl_stat_error;
7838 /* Return a union piecewise multi-affine expression
7839 * that is equal to "mv" on "domain".
7841 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7842 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7844 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7845 isl_space *space;
7847 space = isl_union_set_get_space(domain);
7848 data.res = isl_union_pw_multi_aff_empty(space);
7849 data.mv = mv;
7850 if (isl_union_set_foreach_set(domain,
7851 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7852 data.res = isl_union_pw_multi_aff_free(data.res);
7853 isl_union_set_free(domain);
7854 isl_multi_val_free(mv);
7855 return data.res;
7858 /* Compute the pullback of data->pma by the function represented by "pma2",
7859 * provided the spaces match, and add the results to data->res.
7861 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7863 struct isl_union_pw_multi_aff_bin_data *data = user;
7865 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7866 pma2->dim, isl_dim_out)) {
7867 isl_pw_multi_aff_free(pma2);
7868 return isl_stat_ok;
7871 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7872 isl_pw_multi_aff_copy(data->pma), pma2);
7874 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7875 if (!data->res)
7876 return isl_stat_error;
7878 return isl_stat_ok;
7881 /* Compute the pullback of "upma1" by the function represented by "upma2".
7883 __isl_give isl_union_pw_multi_aff *
7884 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7885 __isl_take isl_union_pw_multi_aff *upma1,
7886 __isl_take isl_union_pw_multi_aff *upma2)
7888 return bin_op(upma1, upma2, &pullback_entry);
7891 /* Apply "upma2" to "upma1".
7893 * That is, compute the pullback of "upma2" by "upma1".
7895 __isl_give isl_union_pw_multi_aff *
7896 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7897 __isl_take isl_union_pw_multi_aff *upma1,
7898 __isl_take isl_union_pw_multi_aff *upma2)
7900 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7903 #undef TYPE
7904 #define TYPE isl_pw_multi_aff
7905 static
7906 #include "isl_copy_tuple_id_templ.c"
7908 /* Given a function "pma1" of the form A[B -> C] -> D and
7909 * a function "pma2" of the form E -> B,
7910 * replace the domain of the wrapped relation inside the domain of "pma1"
7911 * by the preimage with respect to "pma2".
7912 * In other words, plug in "pma2" in this nested domain.
7913 * The result is of the form A[E -> C] -> D.
7915 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7916 * plug that into "pma1".
7918 __isl_give isl_pw_multi_aff *
7919 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7920 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
7922 isl_space *pma1_space, *pma2_space;
7923 isl_space *space;
7924 isl_pw_multi_aff *id;
7926 pma1_space = isl_pw_multi_aff_peek_space(pma1);
7927 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7929 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
7930 goto error;
7931 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
7932 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
7933 goto error;
7935 space = isl_space_domain(isl_space_copy(pma1_space));
7936 space = isl_space_range(isl_space_unwrap(space));
7937 id = isl_pw_multi_aff_identity_on_domain_space(space);
7938 pma2 = isl_pw_multi_aff_product(pma2, id);
7940 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
7941 pma1_space, isl_dim_in);
7942 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
7943 pma1_space, isl_dim_in);
7945 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
7946 error:
7947 isl_pw_multi_aff_free(pma1);
7948 isl_pw_multi_aff_free(pma2);
7949 return NULL;
7952 /* If data->pma and "pma2" are such that
7953 * data->pma is of the form A[B -> C] -> D and
7954 * "pma2" is of the form E -> B,
7955 * then replace the domain of the wrapped relation
7956 * inside the domain of data->pma by the preimage with respect to "pma2" and
7957 * add the result to data->res.
7959 static isl_stat preimage_domain_wrapped_domain_entry(
7960 __isl_take isl_pw_multi_aff *pma2, void *user)
7962 struct isl_union_pw_multi_aff_bin_data *data = user;
7963 isl_space *pma1_space, *pma2_space;
7964 isl_bool match;
7966 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
7967 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7969 match = isl_space_domain_is_wrapping(pma1_space);
7970 if (match >= 0 && match)
7971 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
7972 isl_dim_in, pma2_space, isl_dim_out);
7973 if (match < 0 || !match) {
7974 isl_pw_multi_aff_free(pma2);
7975 return match < 0 ? isl_stat_error : isl_stat_ok;
7978 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7979 isl_pw_multi_aff_copy(data->pma), pma2);
7981 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7983 return isl_stat_non_null(data->res);
7986 /* For each pair of functions A[B -> C] -> D in "upma1" and
7987 * E -> B in "upma2",
7988 * replace the domain of the wrapped relation inside the domain of the first
7989 * by the preimage with respect to the second and collect the results.
7990 * In other words, plug in the second function in this nested domain.
7991 * The results are of the form A[E -> C] -> D.
7993 __isl_give isl_union_pw_multi_aff *
7994 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
7995 __isl_take isl_union_pw_multi_aff *upma1,
7996 __isl_take isl_union_pw_multi_aff *upma2)
7998 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
8001 /* Check that the domain space of "upa" matches "space".
8003 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8004 * can in principle never fail since the space "space" is that
8005 * of the isl_multi_union_pw_aff and is a set space such that
8006 * there is no domain space to match.
8008 * We check the parameters and double-check that "space" is
8009 * indeed that of a set.
8011 static isl_stat isl_union_pw_aff_check_match_domain_space(
8012 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8014 isl_space *upa_space;
8015 isl_bool match;
8017 if (!upa || !space)
8018 return isl_stat_error;
8020 match = isl_space_is_set(space);
8021 if (match < 0)
8022 return isl_stat_error;
8023 if (!match)
8024 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8025 "expecting set space", return isl_stat_error);
8027 upa_space = isl_union_pw_aff_get_space(upa);
8028 match = isl_space_has_equal_params(space, upa_space);
8029 if (match < 0)
8030 goto error;
8031 if (!match)
8032 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8033 "parameters don't match", goto error);
8035 isl_space_free(upa_space);
8036 return isl_stat_ok;
8037 error:
8038 isl_space_free(upa_space);
8039 return isl_stat_error;
8042 /* Do the parameters of "upa" match those of "space"?
8044 static isl_bool isl_union_pw_aff_matching_params(
8045 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8047 isl_space *upa_space;
8048 isl_bool match;
8050 if (!upa || !space)
8051 return isl_bool_error;
8053 upa_space = isl_union_pw_aff_get_space(upa);
8055 match = isl_space_has_equal_params(space, upa_space);
8057 isl_space_free(upa_space);
8058 return match;
8061 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8062 * space represents the new parameters.
8063 * res collects the results.
8065 struct isl_union_pw_aff_reset_params_data {
8066 isl_space *space;
8067 isl_union_pw_aff *res;
8070 /* Replace the parameters of "pa" by data->space and
8071 * add the result to data->res.
8073 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8075 struct isl_union_pw_aff_reset_params_data *data = user;
8076 isl_space *space;
8078 space = isl_pw_aff_get_space(pa);
8079 space = isl_space_replace_params(space, data->space);
8080 pa = isl_pw_aff_reset_space(pa, space);
8081 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8083 return data->res ? isl_stat_ok : isl_stat_error;
8086 /* Replace the domain space of "upa" by "space".
8087 * Since a union expression does not have a (single) domain space,
8088 * "space" is necessarily a parameter space.
8090 * Since the order and the names of the parameters determine
8091 * the hash value, we need to create a new hash table.
8093 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8094 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8096 struct isl_union_pw_aff_reset_params_data data = { space };
8097 isl_bool match;
8099 match = isl_union_pw_aff_matching_params(upa, space);
8100 if (match < 0)
8101 upa = isl_union_pw_aff_free(upa);
8102 else if (match) {
8103 isl_space_free(space);
8104 return upa;
8107 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8108 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8109 data.res = isl_union_pw_aff_free(data.res);
8111 isl_union_pw_aff_free(upa);
8112 isl_space_free(space);
8113 return data.res;
8116 /* Return the floor of "pa".
8118 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8120 return isl_pw_aff_floor(pa);
8123 /* Given f, return floor(f).
8125 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8126 __isl_take isl_union_pw_aff *upa)
8128 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8131 /* Compute
8133 * upa mod m = upa - m * floor(upa/m)
8135 * with m an integer value.
8137 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8138 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8140 isl_union_pw_aff *res;
8142 if (!upa || !m)
8143 goto error;
8145 if (!isl_val_is_int(m))
8146 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8147 "expecting integer modulo", goto error);
8148 if (!isl_val_is_pos(m))
8149 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8150 "expecting positive modulo", goto error);
8152 res = isl_union_pw_aff_copy(upa);
8153 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8154 upa = isl_union_pw_aff_floor(upa);
8155 upa = isl_union_pw_aff_scale_val(upa, m);
8156 res = isl_union_pw_aff_sub(res, upa);
8158 return res;
8159 error:
8160 isl_val_free(m);
8161 isl_union_pw_aff_free(upa);
8162 return NULL;
8165 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8166 * pos is the output position that needs to be extracted.
8167 * res collects the results.
8169 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8170 int pos;
8171 isl_union_pw_aff *res;
8174 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8175 * (assuming it has such a dimension) and add it to data->res.
8177 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8179 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8180 isl_size n_out;
8181 isl_pw_aff *pa;
8183 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8184 if (n_out < 0)
8185 return isl_stat_error;
8186 if (data->pos >= n_out) {
8187 isl_pw_multi_aff_free(pma);
8188 return isl_stat_ok;
8191 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8192 isl_pw_multi_aff_free(pma);
8194 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8196 return data->res ? isl_stat_ok : isl_stat_error;
8199 /* Extract an isl_union_pw_aff corresponding to
8200 * output dimension "pos" of "upma".
8202 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8203 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8205 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8206 isl_space *space;
8208 if (!upma)
8209 return NULL;
8211 if (pos < 0)
8212 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8213 "cannot extract at negative position", return NULL);
8215 space = isl_union_pw_multi_aff_get_space(upma);
8216 data.res = isl_union_pw_aff_empty(space);
8217 data.pos = pos;
8218 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8219 &get_union_pw_aff, &data) < 0)
8220 data.res = isl_union_pw_aff_free(data.res);
8222 return data.res;
8225 /* Return a union piecewise affine expression
8226 * that is equal to "aff" on "domain".
8228 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8229 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8231 isl_pw_aff *pa;
8233 pa = isl_pw_aff_from_aff(aff);
8234 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8237 /* Return a union piecewise affine expression
8238 * that is equal to the parameter identified by "id" on "domain".
8240 * Make sure the parameter appears in the space passed to
8241 * isl_aff_param_on_domain_space_id.
8243 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8244 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8246 isl_space *space;
8247 isl_aff *aff;
8249 space = isl_union_set_get_space(domain);
8250 space = isl_space_add_param_id(space, isl_id_copy(id));
8251 aff = isl_aff_param_on_domain_space_id(space, id);
8252 return isl_union_pw_aff_aff_on_domain(domain, aff);
8255 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8256 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8257 * needs to attain.
8258 * "res" collects the results.
8260 struct isl_union_pw_aff_pw_aff_on_domain_data {
8261 isl_pw_aff *pa;
8262 isl_union_pw_aff *res;
8265 /* Construct a piecewise affine expression that is equal to data->pa
8266 * on "domain" and add the result to data->res.
8268 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8270 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8271 isl_pw_aff *pa;
8272 isl_size dim;
8274 pa = isl_pw_aff_copy(data->pa);
8275 dim = isl_set_dim(domain, isl_dim_set);
8276 if (dim < 0)
8277 pa = isl_pw_aff_free(pa);
8278 pa = isl_pw_aff_from_range(pa);
8279 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8280 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8281 pa = isl_pw_aff_intersect_domain(pa, domain);
8282 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8284 return data->res ? isl_stat_ok : isl_stat_error;
8287 /* Return a union piecewise affine expression
8288 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8289 * have been aligned.
8291 * Construct an isl_pw_aff on each of the sets in "domain" and
8292 * collect the results.
8294 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8295 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8297 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8298 isl_space *space;
8300 space = isl_union_set_get_space(domain);
8301 data.res = isl_union_pw_aff_empty(space);
8302 data.pa = pa;
8303 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8304 data.res = isl_union_pw_aff_free(data.res);
8305 isl_union_set_free(domain);
8306 isl_pw_aff_free(pa);
8307 return data.res;
8310 /* Return a union piecewise affine expression
8311 * that is equal to "pa" on "domain".
8313 * Check that "pa" is a parametric expression,
8314 * align the parameters if needed and call
8315 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8317 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8318 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8320 isl_bool is_set;
8321 isl_bool equal_params;
8322 isl_space *domain_space, *pa_space;
8324 pa_space = isl_pw_aff_peek_space(pa);
8325 is_set = isl_space_is_set(pa_space);
8326 if (is_set < 0)
8327 goto error;
8328 if (!is_set)
8329 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8330 "expecting parametric expression", goto error);
8332 domain_space = isl_union_set_get_space(domain);
8333 pa_space = isl_pw_aff_get_space(pa);
8334 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8335 if (equal_params >= 0 && !equal_params) {
8336 isl_space *space;
8338 space = isl_space_align_params(domain_space, pa_space);
8339 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8340 domain = isl_union_set_align_params(domain, space);
8341 } else {
8342 isl_space_free(domain_space);
8343 isl_space_free(pa_space);
8346 if (equal_params < 0)
8347 goto error;
8348 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8349 error:
8350 isl_union_set_free(domain);
8351 isl_pw_aff_free(pa);
8352 return NULL;
8355 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8356 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8357 * "res" collects the results.
8359 struct isl_union_pw_aff_val_on_domain_data {
8360 isl_val *v;
8361 isl_union_pw_aff *res;
8364 /* Construct a piecewise affine expression that is equal to data->v
8365 * on "domain" and add the result to data->res.
8367 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8369 struct isl_union_pw_aff_val_on_domain_data *data = user;
8370 isl_pw_aff *pa;
8371 isl_val *v;
8373 v = isl_val_copy(data->v);
8374 pa = isl_pw_aff_val_on_domain(domain, v);
8375 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8377 return data->res ? isl_stat_ok : isl_stat_error;
8380 /* Return a union piecewise affine expression
8381 * that is equal to "v" on "domain".
8383 * Construct an isl_pw_aff on each of the sets in "domain" and
8384 * collect the results.
8386 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8387 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8389 struct isl_union_pw_aff_val_on_domain_data data;
8390 isl_space *space;
8392 space = isl_union_set_get_space(domain);
8393 data.res = isl_union_pw_aff_empty(space);
8394 data.v = v;
8395 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8396 data.res = isl_union_pw_aff_free(data.res);
8397 isl_union_set_free(domain);
8398 isl_val_free(v);
8399 return data.res;
8402 /* Construct a piecewise multi affine expression
8403 * that is equal to "pa" and add it to upma.
8405 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8406 void *user)
8408 isl_union_pw_multi_aff **upma = user;
8409 isl_pw_multi_aff *pma;
8411 pma = isl_pw_multi_aff_from_pw_aff(pa);
8412 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8414 return *upma ? isl_stat_ok : isl_stat_error;
8417 /* Construct and return a union piecewise multi affine expression
8418 * that is equal to the given union piecewise affine expression.
8420 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8421 __isl_take isl_union_pw_aff *upa)
8423 isl_space *space;
8424 isl_union_pw_multi_aff *upma;
8426 if (!upa)
8427 return NULL;
8429 space = isl_union_pw_aff_get_space(upa);
8430 upma = isl_union_pw_multi_aff_empty(space);
8432 if (isl_union_pw_aff_foreach_pw_aff(upa,
8433 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8434 upma = isl_union_pw_multi_aff_free(upma);
8436 isl_union_pw_aff_free(upa);
8437 return upma;
8440 /* Compute the set of elements in the domain of "pa" where it is zero and
8441 * add this set to "uset".
8443 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8445 isl_union_set **uset = (isl_union_set **)user;
8447 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8449 return *uset ? isl_stat_ok : isl_stat_error;
8452 /* Return a union set containing those elements in the domain
8453 * of "upa" where it is zero.
8455 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8456 __isl_take isl_union_pw_aff *upa)
8458 isl_union_set *zero;
8460 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8461 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8462 zero = isl_union_set_free(zero);
8464 isl_union_pw_aff_free(upa);
8465 return zero;
8468 /* Internal data structure for isl_union_pw_aff_bind_id,
8469 * storing the parameter that needs to be bound and
8470 * the accumulated results.
8472 struct isl_bind_id_data {
8473 isl_id *id;
8474 isl_union_set *bound;
8477 /* Bind the piecewise affine function "pa" to the parameter data->id,
8478 * adding the resulting elements in the domain where the expression
8479 * is equal to the parameter to data->bound.
8481 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8483 struct isl_bind_id_data *data = user;
8484 isl_set *bound;
8486 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8487 data->bound = isl_union_set_add_set(data->bound, bound);
8489 return data->bound ? isl_stat_ok : isl_stat_error;
8492 /* Bind the union piecewise affine function "upa" to the parameter "id",
8493 * returning the elements in the domain where the expression
8494 * is equal to the parameter.
8496 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8497 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8499 struct isl_bind_id_data data = { id };
8501 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8502 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8503 data.bound = isl_union_set_free(data.bound);
8505 isl_union_pw_aff_free(upa);
8506 isl_id_free(id);
8507 return data.bound;
8510 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8511 * upma is the function that is plugged in.
8512 * pa is the current part of the function in which upma is plugged in.
8513 * res collects the results.
8515 struct isl_union_pw_aff_pullback_upma_data {
8516 isl_union_pw_multi_aff *upma;
8517 isl_pw_aff *pa;
8518 isl_union_pw_aff *res;
8521 /* Check if "pma" can be plugged into data->pa.
8522 * If so, perform the pullback and add the result to data->res.
8524 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8526 struct isl_union_pw_aff_pullback_upma_data *data = user;
8527 isl_pw_aff *pa;
8529 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8530 pma->dim, isl_dim_out)) {
8531 isl_pw_multi_aff_free(pma);
8532 return isl_stat_ok;
8535 pa = isl_pw_aff_copy(data->pa);
8536 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8538 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8540 return data->res ? isl_stat_ok : isl_stat_error;
8543 /* Check if any of the elements of data->upma can be plugged into pa,
8544 * add if so add the result to data->res.
8546 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8548 struct isl_union_pw_aff_pullback_upma_data *data = user;
8549 isl_stat r;
8551 data->pa = pa;
8552 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8553 &pa_pb_pma, data);
8554 isl_pw_aff_free(pa);
8556 return r;
8559 /* Compute the pullback of "upa" by the function represented by "upma".
8560 * In other words, plug in "upma" in "upa". The result contains
8561 * expressions defined over the domain space of "upma".
8563 * Run over all pairs of elements in "upa" and "upma", perform
8564 * the pullback when appropriate and collect the results.
8565 * If the hash value were based on the domain space rather than
8566 * the function space, then we could run through all elements
8567 * of "upma" and directly pick out the corresponding element of "upa".
8569 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8570 __isl_take isl_union_pw_aff *upa,
8571 __isl_take isl_union_pw_multi_aff *upma)
8573 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8574 isl_space *space;
8576 space = isl_union_pw_multi_aff_get_space(upma);
8577 upa = isl_union_pw_aff_align_params(upa, space);
8578 space = isl_union_pw_aff_get_space(upa);
8579 upma = isl_union_pw_multi_aff_align_params(upma, space);
8581 if (!upa || !upma)
8582 goto error;
8584 data.upma = upma;
8585 data.res = isl_union_pw_aff_alloc_same_size(upa);
8586 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8587 data.res = isl_union_pw_aff_free(data.res);
8589 isl_union_pw_aff_free(upa);
8590 isl_union_pw_multi_aff_free(upma);
8591 return data.res;
8592 error:
8593 isl_union_pw_aff_free(upa);
8594 isl_union_pw_multi_aff_free(upma);
8595 return NULL;
8598 #undef BASE
8599 #define BASE union_pw_aff
8600 #undef DOMBASE
8601 #define DOMBASE union_set
8603 #include <isl_multi_explicit_domain.c>
8604 #include <isl_multi_union_pw_aff_explicit_domain.c>
8605 #include <isl_multi_templ.c>
8606 #include <isl_multi_apply_set.c>
8607 #include <isl_multi_apply_union_set.c>
8608 #include <isl_multi_arith_templ.c>
8609 #include <isl_multi_bind_templ.c>
8610 #include <isl_multi_coalesce.c>
8611 #include <isl_multi_dim_id_templ.c>
8612 #include <isl_multi_floor.c>
8613 #include <isl_multi_from_base_templ.c>
8614 #include <isl_multi_gist.c>
8615 #include <isl_multi_align_set.c>
8616 #include <isl_multi_align_union_set.c>
8617 #include <isl_multi_intersect.c>
8618 #include <isl_multi_nan_templ.c>
8619 #include <isl_multi_tuple_id_templ.c>
8620 #include <isl_multi_union_add_templ.c>
8621 #include <isl_multi_zero_space_templ.c>
8623 /* Does "mupa" have a non-trivial explicit domain?
8625 * The explicit domain, if present, is trivial if it represents
8626 * an (obviously) universe parameter set.
8628 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8629 __isl_keep isl_multi_union_pw_aff *mupa)
8631 isl_bool is_params, trivial;
8632 isl_set *set;
8634 if (!mupa)
8635 return isl_bool_error;
8636 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8637 return isl_bool_false;
8638 is_params = isl_union_set_is_params(mupa->u.dom);
8639 if (is_params < 0 || !is_params)
8640 return isl_bool_not(is_params);
8641 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8642 trivial = isl_set_plain_is_universe(set);
8643 isl_set_free(set);
8644 return isl_bool_not(trivial);
8647 /* Construct a multiple union piecewise affine expression
8648 * in the given space with value zero in each of the output dimensions.
8650 * Since there is no canonical zero value for
8651 * a union piecewise affine expression, we can only construct
8652 * a zero-dimensional "zero" value.
8654 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8655 __isl_take isl_space *space)
8657 isl_bool params;
8658 isl_size dim;
8660 if (!space)
8661 return NULL;
8663 params = isl_space_is_params(space);
8664 if (params < 0)
8665 goto error;
8666 if (params)
8667 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8668 "expecting proper set space", goto error);
8669 if (!isl_space_is_set(space))
8670 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8671 "expecting set space", goto error);
8672 dim = isl_space_dim(space, isl_dim_out);
8673 if (dim < 0)
8674 goto error;
8675 if (dim != 0)
8676 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8677 "expecting 0D space", goto error);
8679 return isl_multi_union_pw_aff_alloc(space);
8680 error:
8681 isl_space_free(space);
8682 return NULL;
8685 /* Construct and return a multi union piecewise affine expression
8686 * that is equal to the given multi affine expression.
8688 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8689 __isl_take isl_multi_aff *ma)
8691 isl_multi_pw_aff *mpa;
8693 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8694 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8697 /* This function performs the same operation as
8698 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8699 * isl_multi_aff when exported.
8701 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8702 __isl_take isl_multi_aff *ma)
8704 return isl_multi_union_pw_aff_from_multi_aff(ma);
8707 /* Construct and return a multi union piecewise affine expression
8708 * that is equal to the given multi piecewise affine expression.
8710 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8711 __isl_take isl_multi_pw_aff *mpa)
8713 int i;
8714 isl_size n;
8715 isl_space *space;
8716 isl_multi_union_pw_aff *mupa;
8718 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8719 if (n < 0)
8720 mpa = isl_multi_pw_aff_free(mpa);
8721 if (!mpa)
8722 return NULL;
8724 space = isl_multi_pw_aff_get_space(mpa);
8725 space = isl_space_range(space);
8726 mupa = isl_multi_union_pw_aff_alloc(space);
8728 for (i = 0; i < n; ++i) {
8729 isl_pw_aff *pa;
8730 isl_union_pw_aff *upa;
8732 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8733 upa = isl_union_pw_aff_from_pw_aff(pa);
8734 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8737 isl_multi_pw_aff_free(mpa);
8739 return mupa;
8742 /* Extract the range space of "pma" and assign it to *space.
8743 * If *space has already been set (through a previous call to this function),
8744 * then check that the range space is the same.
8746 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8748 isl_space **space = user;
8749 isl_space *pma_space;
8750 isl_bool equal;
8752 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8753 isl_pw_multi_aff_free(pma);
8755 if (!pma_space)
8756 return isl_stat_error;
8757 if (!*space) {
8758 *space = pma_space;
8759 return isl_stat_ok;
8762 equal = isl_space_is_equal(pma_space, *space);
8763 isl_space_free(pma_space);
8765 if (equal < 0)
8766 return isl_stat_error;
8767 if (!equal)
8768 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8769 "range spaces not the same", return isl_stat_error);
8770 return isl_stat_ok;
8773 /* Construct and return a multi union piecewise affine expression
8774 * that is equal to the given union piecewise multi affine expression.
8776 * In order to be able to perform the conversion, the input
8777 * needs to be non-empty and may only involve a single range space.
8779 * If the resulting multi union piecewise affine expression has
8780 * an explicit domain, then assign it the domain of the input.
8781 * In other cases, the domain is stored in the individual elements.
8783 __isl_give isl_multi_union_pw_aff *
8784 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8785 __isl_take isl_union_pw_multi_aff *upma)
8787 isl_space *space = NULL;
8788 isl_multi_union_pw_aff *mupa;
8789 int i;
8790 isl_size n;
8792 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8793 if (n < 0)
8794 goto error;
8795 if (n == 0)
8796 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8797 "cannot extract range space from empty input",
8798 goto error);
8799 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8800 &space) < 0)
8801 goto error;
8803 if (!space)
8804 goto error;
8806 n = isl_space_dim(space, isl_dim_set);
8807 if (n < 0)
8808 space = isl_space_free(space);
8809 mupa = isl_multi_union_pw_aff_alloc(space);
8811 for (i = 0; i < n; ++i) {
8812 isl_union_pw_aff *upa;
8814 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8815 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8817 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8818 isl_union_set *dom;
8819 isl_union_pw_multi_aff *copy;
8821 copy = isl_union_pw_multi_aff_copy(upma);
8822 dom = isl_union_pw_multi_aff_domain(copy);
8823 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8826 isl_union_pw_multi_aff_free(upma);
8827 return mupa;
8828 error:
8829 isl_space_free(space);
8830 isl_union_pw_multi_aff_free(upma);
8831 return NULL;
8834 /* This function performs the same operation as
8835 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8836 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8838 __isl_give isl_multi_union_pw_aff *
8839 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8840 __isl_take isl_union_pw_multi_aff *upma)
8842 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8845 /* Try and create an isl_multi_union_pw_aff that is equivalent
8846 * to the given isl_union_map.
8847 * The isl_union_map is required to be single-valued in each space.
8848 * Moreover, it cannot be empty and all range spaces need to be the same.
8849 * Otherwise, an error is produced.
8851 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8852 __isl_take isl_union_map *umap)
8854 isl_union_pw_multi_aff *upma;
8856 upma = isl_union_pw_multi_aff_from_union_map(umap);
8857 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8860 /* This function performs the same operation as
8861 * isl_multi_union_pw_aff_from_union_map,
8862 * but is considered as a function on an isl_union_map when exported.
8864 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8865 __isl_take isl_union_map *umap)
8867 return isl_multi_union_pw_aff_from_union_map(umap);
8870 /* Return a multiple union piecewise affine expression
8871 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8872 * have been aligned.
8874 * If the resulting multi union piecewise affine expression has
8875 * an explicit domain, then assign it the input domain.
8876 * In other cases, the domain is stored in the individual elements.
8878 static __isl_give isl_multi_union_pw_aff *
8879 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8880 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8882 int i;
8883 isl_size n;
8884 isl_space *space;
8885 isl_multi_union_pw_aff *mupa;
8887 n = isl_multi_val_dim(mv, isl_dim_set);
8888 if (!domain || n < 0)
8889 goto error;
8891 space = isl_multi_val_get_space(mv);
8892 mupa = isl_multi_union_pw_aff_alloc(space);
8893 for (i = 0; i < n; ++i) {
8894 isl_val *v;
8895 isl_union_pw_aff *upa;
8897 v = isl_multi_val_get_val(mv, i);
8898 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8900 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8902 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8903 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8904 isl_union_set_copy(domain));
8906 isl_union_set_free(domain);
8907 isl_multi_val_free(mv);
8908 return mupa;
8909 error:
8910 isl_union_set_free(domain);
8911 isl_multi_val_free(mv);
8912 return NULL;
8915 /* Return a multiple union piecewise affine expression
8916 * that is equal to "mv" on "domain".
8918 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8919 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8921 isl_bool equal_params;
8923 if (!domain || !mv)
8924 goto error;
8925 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8926 if (equal_params < 0)
8927 goto error;
8928 if (equal_params)
8929 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8930 domain, mv);
8931 domain = isl_union_set_align_params(domain,
8932 isl_multi_val_get_space(mv));
8933 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8934 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8935 error:
8936 isl_union_set_free(domain);
8937 isl_multi_val_free(mv);
8938 return NULL;
8941 /* Return a multiple union piecewise affine expression
8942 * that is equal to "ma" on "domain".
8944 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8945 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8947 isl_pw_multi_aff *pma;
8949 pma = isl_pw_multi_aff_from_multi_aff(ma);
8950 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8953 /* Return a multiple union piecewise affine expression
8954 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8955 * have been aligned.
8957 * If the resulting multi union piecewise affine expression has
8958 * an explicit domain, then assign it the input domain.
8959 * In other cases, the domain is stored in the individual elements.
8961 static __isl_give isl_multi_union_pw_aff *
8962 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8963 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8965 int i;
8966 isl_size n;
8967 isl_space *space;
8968 isl_multi_union_pw_aff *mupa;
8970 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8971 if (!domain || n < 0)
8972 goto error;
8973 space = isl_pw_multi_aff_get_space(pma);
8974 mupa = isl_multi_union_pw_aff_alloc(space);
8975 for (i = 0; i < n; ++i) {
8976 isl_pw_aff *pa;
8977 isl_union_pw_aff *upa;
8979 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8980 upa = isl_union_pw_aff_pw_aff_on_domain(
8981 isl_union_set_copy(domain), pa);
8982 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8984 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8985 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8986 isl_union_set_copy(domain));
8988 isl_union_set_free(domain);
8989 isl_pw_multi_aff_free(pma);
8990 return mupa;
8991 error:
8992 isl_union_set_free(domain);
8993 isl_pw_multi_aff_free(pma);
8994 return NULL;
8997 /* Return a multiple union piecewise affine expression
8998 * that is equal to "pma" on "domain".
9000 __isl_give isl_multi_union_pw_aff *
9001 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
9002 __isl_take isl_pw_multi_aff *pma)
9004 isl_bool equal_params;
9005 isl_space *space;
9007 space = isl_pw_multi_aff_peek_space(pma);
9008 equal_params = isl_union_set_space_has_equal_params(domain, space);
9009 if (equal_params < 0)
9010 goto error;
9011 if (equal_params)
9012 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9013 domain, pma);
9014 domain = isl_union_set_align_params(domain,
9015 isl_pw_multi_aff_get_space(pma));
9016 pma = isl_pw_multi_aff_align_params(pma,
9017 isl_union_set_get_space(domain));
9018 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
9019 pma);
9020 error:
9021 isl_union_set_free(domain);
9022 isl_pw_multi_aff_free(pma);
9023 return NULL;
9026 /* Return a union set containing those elements in the domains
9027 * of the elements of "mupa" where they are all zero.
9029 * If there are no elements, then simply return the entire domain.
9031 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
9032 __isl_take isl_multi_union_pw_aff *mupa)
9034 int i;
9035 isl_size n;
9036 isl_union_pw_aff *upa;
9037 isl_union_set *zero;
9039 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9040 if (n < 0)
9041 mupa = isl_multi_union_pw_aff_free(mupa);
9042 if (!mupa)
9043 return NULL;
9045 if (n == 0)
9046 return isl_multi_union_pw_aff_domain(mupa);
9048 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9049 zero = isl_union_pw_aff_zero_union_set(upa);
9051 for (i = 1; i < n; ++i) {
9052 isl_union_set *zero_i;
9054 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9055 zero_i = isl_union_pw_aff_zero_union_set(upa);
9057 zero = isl_union_set_intersect(zero, zero_i);
9060 isl_multi_union_pw_aff_free(mupa);
9061 return zero;
9064 /* Construct a union map mapping the shared domain
9065 * of the union piecewise affine expressions to the range of "mupa"
9066 * in the special case of a 0D multi union piecewise affine expression.
9068 * Construct a map between the explicit domain of "mupa" and
9069 * the range space.
9070 * Note that this assumes that the domain consists of explicit elements.
9072 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9073 __isl_take isl_multi_union_pw_aff *mupa)
9075 isl_bool is_params;
9076 isl_space *space;
9077 isl_union_set *dom, *ran;
9079 space = isl_multi_union_pw_aff_get_space(mupa);
9080 dom = isl_multi_union_pw_aff_domain(mupa);
9081 ran = isl_union_set_from_set(isl_set_universe(space));
9083 is_params = isl_union_set_is_params(dom);
9084 if (is_params < 0)
9085 dom = isl_union_set_free(dom);
9086 else if (is_params)
9087 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9088 "cannot create union map from expression without "
9089 "explicit domain elements",
9090 dom = isl_union_set_free(dom));
9092 return isl_union_map_from_domain_and_range(dom, ran);
9095 /* Construct a union map mapping the shared domain
9096 * of the union piecewise affine expressions to the range of "mupa"
9097 * with each dimension in the range equated to the
9098 * corresponding union piecewise affine expression.
9100 * If the input is zero-dimensional, then construct a mapping
9101 * from its explicit domain.
9103 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9104 __isl_take isl_multi_union_pw_aff *mupa)
9106 int i;
9107 isl_size n;
9108 isl_space *space;
9109 isl_union_map *umap;
9110 isl_union_pw_aff *upa;
9112 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9113 if (n < 0)
9114 mupa = isl_multi_union_pw_aff_free(mupa);
9115 if (!mupa)
9116 return NULL;
9118 if (n == 0)
9119 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9121 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9122 umap = isl_union_map_from_union_pw_aff(upa);
9124 for (i = 1; i < n; ++i) {
9125 isl_union_map *umap_i;
9127 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9128 umap_i = isl_union_map_from_union_pw_aff(upa);
9129 umap = isl_union_map_flat_range_product(umap, umap_i);
9132 space = isl_multi_union_pw_aff_get_space(mupa);
9133 umap = isl_union_map_reset_range_space(umap, space);
9135 isl_multi_union_pw_aff_free(mupa);
9136 return umap;
9139 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9140 * "range" is the space from which to set the range space.
9141 * "res" collects the results.
9143 struct isl_union_pw_multi_aff_reset_range_space_data {
9144 isl_space *range;
9145 isl_union_pw_multi_aff *res;
9148 /* Replace the range space of "pma" by the range space of data->range and
9149 * add the result to data->res.
9151 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9153 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9154 isl_space *space;
9156 space = isl_pw_multi_aff_get_space(pma);
9157 space = isl_space_domain(space);
9158 space = isl_space_extend_domain_with_range(space,
9159 isl_space_copy(data->range));
9160 pma = isl_pw_multi_aff_reset_space(pma, space);
9161 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9163 return data->res ? isl_stat_ok : isl_stat_error;
9166 /* Replace the range space of all the piecewise affine expressions in "upma" by
9167 * the range space of "space".
9169 * This assumes that all these expressions have the same output dimension.
9171 * Since the spaces of the expressions change, so do their hash values.
9172 * We therefore need to create a new isl_union_pw_multi_aff.
9173 * Note that the hash value is currently computed based on the entire
9174 * space even though there can only be a single expression with a given
9175 * domain space.
9177 static __isl_give isl_union_pw_multi_aff *
9178 isl_union_pw_multi_aff_reset_range_space(
9179 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9181 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9182 isl_space *space_upma;
9184 space_upma = isl_union_pw_multi_aff_get_space(upma);
9185 data.res = isl_union_pw_multi_aff_empty(space_upma);
9186 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9187 &reset_range_space, &data) < 0)
9188 data.res = isl_union_pw_multi_aff_free(data.res);
9190 isl_space_free(space);
9191 isl_union_pw_multi_aff_free(upma);
9192 return data.res;
9195 /* Construct and return a union piecewise multi affine expression
9196 * that is equal to the given multi union piecewise affine expression,
9197 * in the special case of a 0D multi union piecewise affine expression.
9199 * Construct a union piecewise multi affine expression
9200 * on top of the explicit domain of the input.
9202 __isl_give isl_union_pw_multi_aff *
9203 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9204 __isl_take isl_multi_union_pw_aff *mupa)
9206 isl_space *space;
9207 isl_multi_val *mv;
9208 isl_union_set *domain;
9210 space = isl_multi_union_pw_aff_get_space(mupa);
9211 mv = isl_multi_val_zero(space);
9212 domain = isl_multi_union_pw_aff_domain(mupa);
9213 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9216 /* Construct and return a union piecewise multi affine expression
9217 * that is equal to the given multi union piecewise affine expression.
9219 * If the input is zero-dimensional, then
9220 * construct a union piecewise multi affine expression
9221 * on top of the explicit domain of the input.
9223 __isl_give isl_union_pw_multi_aff *
9224 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9225 __isl_take isl_multi_union_pw_aff *mupa)
9227 int i;
9228 isl_size n;
9229 isl_space *space;
9230 isl_union_pw_multi_aff *upma;
9231 isl_union_pw_aff *upa;
9233 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9234 if (n < 0)
9235 mupa = isl_multi_union_pw_aff_free(mupa);
9236 if (!mupa)
9237 return NULL;
9239 if (n == 0)
9240 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9242 space = isl_multi_union_pw_aff_get_space(mupa);
9243 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9244 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9246 for (i = 1; i < n; ++i) {
9247 isl_union_pw_multi_aff *upma_i;
9249 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9250 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9251 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9254 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9256 isl_multi_union_pw_aff_free(mupa);
9257 return upma;
9260 /* Intersect the range of "mupa" with "range",
9261 * in the special case where "mupa" is 0D.
9263 * Intersect the domain of "mupa" with the constraints on the parameters
9264 * of "range".
9266 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9267 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9269 range = isl_set_params(range);
9270 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9271 return mupa;
9274 /* Intersect the range of "mupa" with "range".
9275 * That is, keep only those domain elements that have a function value
9276 * in "range".
9278 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9279 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9281 isl_union_pw_multi_aff *upma;
9282 isl_union_set *domain;
9283 isl_space *space;
9284 isl_size n;
9285 int match;
9287 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9288 if (n < 0 || !range)
9289 goto error;
9291 space = isl_set_get_space(range);
9292 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9293 space, isl_dim_set);
9294 isl_space_free(space);
9295 if (match < 0)
9296 goto error;
9297 if (!match)
9298 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9299 "space don't match", goto error);
9300 if (n == 0)
9301 return mupa_intersect_range_0D(mupa, range);
9303 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9304 isl_multi_union_pw_aff_copy(mupa));
9305 domain = isl_union_set_from_set(range);
9306 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9307 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9309 return mupa;
9310 error:
9311 isl_multi_union_pw_aff_free(mupa);
9312 isl_set_free(range);
9313 return NULL;
9316 /* Return the shared domain of the elements of "mupa",
9317 * in the special case where "mupa" is zero-dimensional.
9319 * Return the explicit domain of "mupa".
9320 * Note that this domain may be a parameter set, either
9321 * because "mupa" is meant to live in a set space or
9322 * because no explicit domain has been set.
9324 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9325 __isl_take isl_multi_union_pw_aff *mupa)
9327 isl_union_set *dom;
9329 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9330 isl_multi_union_pw_aff_free(mupa);
9332 return dom;
9335 /* Return the shared domain of the elements of "mupa".
9337 * If "mupa" is zero-dimensional, then return its explicit domain.
9339 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9340 __isl_take isl_multi_union_pw_aff *mupa)
9342 int i;
9343 isl_size n;
9344 isl_union_pw_aff *upa;
9345 isl_union_set *dom;
9347 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9348 if (n < 0)
9349 mupa = isl_multi_union_pw_aff_free(mupa);
9350 if (!mupa)
9351 return NULL;
9353 if (n == 0)
9354 return isl_multi_union_pw_aff_domain_0D(mupa);
9356 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9357 dom = isl_union_pw_aff_domain(upa);
9358 for (i = 1; i < n; ++i) {
9359 isl_union_set *dom_i;
9361 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9362 dom_i = isl_union_pw_aff_domain(upa);
9363 dom = isl_union_set_intersect(dom, dom_i);
9366 isl_multi_union_pw_aff_free(mupa);
9367 return dom;
9370 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9371 * In particular, the spaces have been aligned.
9372 * The result is defined over the shared domain of the elements of "mupa"
9374 * We first extract the parametric constant part of "aff" and
9375 * define that over the shared domain.
9376 * Then we iterate over all input dimensions of "aff" and add the corresponding
9377 * multiples of the elements of "mupa".
9378 * Finally, we consider the integer divisions, calling the function
9379 * recursively to obtain an isl_union_pw_aff corresponding to the
9380 * integer division argument.
9382 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9383 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9385 int i;
9386 isl_size n_in, n_div;
9387 isl_union_pw_aff *upa;
9388 isl_union_set *uset;
9389 isl_val *v;
9390 isl_aff *cst;
9392 n_in = isl_aff_dim(aff, isl_dim_in);
9393 n_div = isl_aff_dim(aff, isl_dim_div);
9394 if (n_in < 0 || n_div < 0)
9395 goto error;
9397 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9398 cst = isl_aff_copy(aff);
9399 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9400 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9401 cst = isl_aff_project_domain_on_params(cst);
9402 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9404 for (i = 0; i < n_in; ++i) {
9405 isl_union_pw_aff *upa_i;
9407 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9408 continue;
9409 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9410 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9411 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9412 upa = isl_union_pw_aff_add(upa, upa_i);
9415 for (i = 0; i < n_div; ++i) {
9416 isl_aff *div;
9417 isl_union_pw_aff *upa_i;
9419 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9420 continue;
9421 div = isl_aff_get_div(aff, i);
9422 upa_i = multi_union_pw_aff_apply_aff(
9423 isl_multi_union_pw_aff_copy(mupa), div);
9424 upa_i = isl_union_pw_aff_floor(upa_i);
9425 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9426 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9427 upa = isl_union_pw_aff_add(upa, upa_i);
9430 isl_multi_union_pw_aff_free(mupa);
9431 isl_aff_free(aff);
9433 return upa;
9434 error:
9435 isl_multi_union_pw_aff_free(mupa);
9436 isl_aff_free(aff);
9437 return NULL;
9440 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9441 * with the domain of "aff".
9442 * Furthermore, the dimension of this space needs to be greater than zero.
9443 * The result is defined over the shared domain of the elements of "mupa"
9445 * We perform these checks and then hand over control to
9446 * multi_union_pw_aff_apply_aff.
9448 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9449 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9451 isl_size dim;
9452 isl_space *space1, *space2;
9453 isl_bool equal;
9455 mupa = isl_multi_union_pw_aff_align_params(mupa,
9456 isl_aff_get_space(aff));
9457 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9458 if (!mupa || !aff)
9459 goto error;
9461 space1 = isl_multi_union_pw_aff_get_space(mupa);
9462 space2 = isl_aff_get_domain_space(aff);
9463 equal = isl_space_is_equal(space1, space2);
9464 isl_space_free(space1);
9465 isl_space_free(space2);
9466 if (equal < 0)
9467 goto error;
9468 if (!equal)
9469 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9470 "spaces don't match", goto error);
9471 dim = isl_aff_dim(aff, isl_dim_in);
9472 if (dim < 0)
9473 goto error;
9474 if (dim == 0)
9475 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9476 "cannot determine domains", goto error);
9478 return multi_union_pw_aff_apply_aff(mupa, aff);
9479 error:
9480 isl_multi_union_pw_aff_free(mupa);
9481 isl_aff_free(aff);
9482 return NULL;
9485 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9486 * The space of "mupa" is known to be compatible with the domain of "ma".
9488 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9489 * on the domain of "mupa".
9491 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9492 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9494 isl_union_set *dom;
9496 dom = isl_multi_union_pw_aff_domain(mupa);
9497 ma = isl_multi_aff_project_domain_on_params(ma);
9499 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9502 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9503 * with the domain of "ma".
9504 * The result is defined over the shared domain of the elements of "mupa"
9506 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9507 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9509 isl_space *space1, *space2;
9510 isl_multi_union_pw_aff *res;
9511 isl_bool equal;
9512 int i;
9513 isl_size n_in, n_out;
9515 mupa = isl_multi_union_pw_aff_align_params(mupa,
9516 isl_multi_aff_get_space(ma));
9517 ma = isl_multi_aff_align_params(ma,
9518 isl_multi_union_pw_aff_get_space(mupa));
9519 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9520 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9521 if (!mupa || n_in < 0 || n_out < 0)
9522 goto error;
9524 space1 = isl_multi_union_pw_aff_get_space(mupa);
9525 space2 = isl_multi_aff_get_domain_space(ma);
9526 equal = isl_space_is_equal(space1, space2);
9527 isl_space_free(space1);
9528 isl_space_free(space2);
9529 if (equal < 0)
9530 goto error;
9531 if (!equal)
9532 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9533 "spaces don't match", goto error);
9534 if (n_in == 0)
9535 return mupa_apply_multi_aff_0D(mupa, ma);
9537 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9538 res = isl_multi_union_pw_aff_alloc(space1);
9540 for (i = 0; i < n_out; ++i) {
9541 isl_aff *aff;
9542 isl_union_pw_aff *upa;
9544 aff = isl_multi_aff_get_aff(ma, i);
9545 upa = multi_union_pw_aff_apply_aff(
9546 isl_multi_union_pw_aff_copy(mupa), aff);
9547 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9550 isl_multi_aff_free(ma);
9551 isl_multi_union_pw_aff_free(mupa);
9552 return res;
9553 error:
9554 isl_multi_union_pw_aff_free(mupa);
9555 isl_multi_aff_free(ma);
9556 return NULL;
9559 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9560 * The space of "mupa" is known to be compatible with the domain of "pa".
9562 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9563 * on the domain of "mupa".
9565 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9566 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9568 isl_union_set *dom;
9570 dom = isl_multi_union_pw_aff_domain(mupa);
9571 pa = isl_pw_aff_project_domain_on_params(pa);
9573 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9576 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9577 * with the domain of "pa".
9578 * Furthermore, the dimension of this space needs to be greater than zero.
9579 * The result is defined over the shared domain of the elements of "mupa"
9581 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9582 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9584 int i;
9585 isl_bool equal;
9586 isl_size n_in;
9587 isl_space *space, *space2;
9588 isl_union_pw_aff *upa;
9590 mupa = isl_multi_union_pw_aff_align_params(mupa,
9591 isl_pw_aff_get_space(pa));
9592 pa = isl_pw_aff_align_params(pa,
9593 isl_multi_union_pw_aff_get_space(mupa));
9594 if (!mupa || !pa)
9595 goto error;
9597 space = isl_multi_union_pw_aff_get_space(mupa);
9598 space2 = isl_pw_aff_get_domain_space(pa);
9599 equal = isl_space_is_equal(space, space2);
9600 isl_space_free(space);
9601 isl_space_free(space2);
9602 if (equal < 0)
9603 goto error;
9604 if (!equal)
9605 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9606 "spaces don't match", goto error);
9607 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9608 if (n_in < 0)
9609 goto error;
9610 if (n_in == 0)
9611 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9613 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9614 upa = isl_union_pw_aff_empty(space);
9616 for (i = 0; i < pa->n; ++i) {
9617 isl_aff *aff;
9618 isl_set *domain;
9619 isl_multi_union_pw_aff *mupa_i;
9620 isl_union_pw_aff *upa_i;
9622 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9623 domain = isl_set_copy(pa->p[i].set);
9624 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9625 aff = isl_aff_copy(pa->p[i].aff);
9626 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9627 upa = isl_union_pw_aff_union_add(upa, upa_i);
9630 isl_multi_union_pw_aff_free(mupa);
9631 isl_pw_aff_free(pa);
9632 return upa;
9633 error:
9634 isl_multi_union_pw_aff_free(mupa);
9635 isl_pw_aff_free(pa);
9636 return NULL;
9639 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9640 * The space of "mupa" is known to be compatible with the domain of "pma".
9642 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9643 * on the domain of "mupa".
9645 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9646 __isl_take isl_multi_union_pw_aff *mupa,
9647 __isl_take isl_pw_multi_aff *pma)
9649 isl_union_set *dom;
9651 dom = isl_multi_union_pw_aff_domain(mupa);
9652 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9654 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9657 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9658 * with the domain of "pma".
9659 * The result is defined over the shared domain of the elements of "mupa"
9661 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9662 __isl_take isl_multi_union_pw_aff *mupa,
9663 __isl_take isl_pw_multi_aff *pma)
9665 isl_space *space1, *space2;
9666 isl_multi_union_pw_aff *res;
9667 isl_bool equal;
9668 int i;
9669 isl_size n_in, n_out;
9671 mupa = isl_multi_union_pw_aff_align_params(mupa,
9672 isl_pw_multi_aff_get_space(pma));
9673 pma = isl_pw_multi_aff_align_params(pma,
9674 isl_multi_union_pw_aff_get_space(mupa));
9675 if (!mupa || !pma)
9676 goto error;
9678 space1 = isl_multi_union_pw_aff_get_space(mupa);
9679 space2 = isl_pw_multi_aff_get_domain_space(pma);
9680 equal = isl_space_is_equal(space1, space2);
9681 isl_space_free(space1);
9682 isl_space_free(space2);
9683 if (equal < 0)
9684 goto error;
9685 if (!equal)
9686 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9687 "spaces don't match", goto error);
9688 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9689 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9690 if (n_in < 0 || n_out < 0)
9691 goto error;
9692 if (n_in == 0)
9693 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9695 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9696 res = isl_multi_union_pw_aff_alloc(space1);
9698 for (i = 0; i < n_out; ++i) {
9699 isl_pw_aff *pa;
9700 isl_union_pw_aff *upa;
9702 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9703 upa = isl_multi_union_pw_aff_apply_pw_aff(
9704 isl_multi_union_pw_aff_copy(mupa), pa);
9705 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9708 isl_pw_multi_aff_free(pma);
9709 isl_multi_union_pw_aff_free(mupa);
9710 return res;
9711 error:
9712 isl_multi_union_pw_aff_free(mupa);
9713 isl_pw_multi_aff_free(pma);
9714 return NULL;
9717 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9718 * If the explicit domain only keeps track of constraints on the parameters,
9719 * then only update those constraints.
9721 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9722 __isl_take isl_multi_union_pw_aff *mupa,
9723 __isl_keep isl_union_pw_multi_aff *upma)
9725 isl_bool is_params;
9727 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9728 return isl_multi_union_pw_aff_free(mupa);
9730 mupa = isl_multi_union_pw_aff_cow(mupa);
9731 if (!mupa)
9732 return NULL;
9734 is_params = isl_union_set_is_params(mupa->u.dom);
9735 if (is_params < 0)
9736 return isl_multi_union_pw_aff_free(mupa);
9738 upma = isl_union_pw_multi_aff_copy(upma);
9739 if (is_params)
9740 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9741 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9742 else
9743 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9744 mupa->u.dom, upma);
9745 if (!mupa->u.dom)
9746 return isl_multi_union_pw_aff_free(mupa);
9747 return mupa;
9750 /* Compute the pullback of "mupa" by the function represented by "upma".
9751 * In other words, plug in "upma" in "mupa". The result contains
9752 * expressions defined over the domain space of "upma".
9754 * Run over all elements of "mupa" and plug in "upma" in each of them.
9756 * If "mupa" has an explicit domain, then it is this domain
9757 * that needs to undergo a pullback instead, i.e., a preimage.
9759 __isl_give isl_multi_union_pw_aff *
9760 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9761 __isl_take isl_multi_union_pw_aff *mupa,
9762 __isl_take isl_union_pw_multi_aff *upma)
9764 int i;
9765 isl_size n;
9767 mupa = isl_multi_union_pw_aff_align_params(mupa,
9768 isl_union_pw_multi_aff_get_space(upma));
9769 upma = isl_union_pw_multi_aff_align_params(upma,
9770 isl_multi_union_pw_aff_get_space(mupa));
9771 mupa = isl_multi_union_pw_aff_cow(mupa);
9772 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9773 if (n < 0 || !upma)
9774 goto error;
9776 for (i = 0; i < n; ++i) {
9777 isl_union_pw_aff *upa;
9779 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9780 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9781 isl_union_pw_multi_aff_copy(upma));
9782 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9785 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9786 mupa = preimage_explicit_domain(mupa, upma);
9788 isl_union_pw_multi_aff_free(upma);
9789 return mupa;
9790 error:
9791 isl_multi_union_pw_aff_free(mupa);
9792 isl_union_pw_multi_aff_free(upma);
9793 return NULL;
9796 /* Extract the sequence of elements in "mupa" with domain space "space"
9797 * (ignoring parameters).
9799 * For the elements of "mupa" that are not defined on the specified space,
9800 * the corresponding element in the result is empty.
9802 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9803 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9805 int i;
9806 isl_size n;
9807 isl_space *space_mpa;
9808 isl_multi_pw_aff *mpa;
9810 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9811 if (n < 0 || !space)
9812 goto error;
9814 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9815 space = isl_space_replace_params(space, space_mpa);
9816 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9817 space_mpa);
9818 mpa = isl_multi_pw_aff_alloc(space_mpa);
9820 space = isl_space_from_domain(space);
9821 space = isl_space_add_dims(space, isl_dim_out, 1);
9822 for (i = 0; i < n; ++i) {
9823 isl_union_pw_aff *upa;
9824 isl_pw_aff *pa;
9826 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9827 pa = isl_union_pw_aff_extract_pw_aff(upa,
9828 isl_space_copy(space));
9829 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9830 isl_union_pw_aff_free(upa);
9833 isl_space_free(space);
9834 return mpa;
9835 error:
9836 isl_space_free(space);
9837 return NULL;
9840 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9841 * should modify the base expressions in the input.
9843 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9844 * are taken into account.
9845 * "fn" is applied to each entry in the input.
9847 struct isl_union_pw_multi_aff_un_op_control {
9848 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9849 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9852 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9853 * a second argument) for use as an isl_union_pw_multi_aff_transform
9854 * filter function (which does take a second argument).
9855 * Simply call control->filter without the second argument.
9857 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9858 __isl_take isl_pw_multi_aff *pma, void *user)
9860 struct isl_union_pw_multi_aff_un_op_control *control = user;
9862 return control->filter(pma);
9865 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9866 * a second argument) for use as an isl_union_pw_multi_aff_transform
9867 * base function (which does take a second argument).
9868 * Simply call control->fn without the second argument.
9870 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9871 __isl_take isl_pw_multi_aff *pma, void *user)
9873 struct isl_union_pw_multi_aff_un_op_control *control = user;
9875 return control->fn(pma);
9878 /* Construct an isl_union_pw_multi_aff that is obtained by
9879 * modifying "upma" according to "control".
9881 * isl_union_pw_multi_aff_transform performs essentially
9882 * the same operation, but takes a filter and a callback function
9883 * of a different form (with an extra argument).
9884 * Call isl_union_pw_multi_aff_transform with wrappers
9885 * that remove this extra argument.
9887 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9888 __isl_take isl_union_pw_multi_aff *upma,
9889 struct isl_union_pw_multi_aff_un_op_control *control)
9891 struct isl_union_pw_multi_aff_transform_control t_control = {
9892 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9893 .filter_user = control,
9894 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9895 .fn_user = control,
9898 return isl_union_pw_multi_aff_transform(upma, &t_control);
9901 /* For each function in "upma" of the form A -> [B -> C],
9902 * extract the function A -> B and collect the results.
9904 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9905 __isl_take isl_union_pw_multi_aff *upma)
9907 struct isl_union_pw_multi_aff_un_op_control control = {
9908 .filter = &isl_pw_multi_aff_range_is_wrapping,
9909 .fn = &isl_pw_multi_aff_range_factor_domain,
9911 return isl_union_pw_multi_aff_un_op(upma, &control);
9914 /* For each function in "upma" of the form A -> [B -> C],
9915 * extract the function A -> C and collect the results.
9917 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9918 __isl_take isl_union_pw_multi_aff *upma)
9920 struct isl_union_pw_multi_aff_un_op_control control = {
9921 .filter = &isl_pw_multi_aff_range_is_wrapping,
9922 .fn = &isl_pw_multi_aff_range_factor_range,
9924 return isl_union_pw_multi_aff_un_op(upma, &control);
9927 /* Evaluate the affine function "aff" in the void point "pnt".
9928 * In particular, return the value NaN.
9930 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9931 __isl_take isl_point *pnt)
9933 isl_ctx *ctx;
9935 ctx = isl_point_get_ctx(pnt);
9936 isl_aff_free(aff);
9937 isl_point_free(pnt);
9938 return isl_val_nan(ctx);
9941 /* Evaluate the affine expression "aff"
9942 * in the coordinates (with denominator) "pnt".
9944 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9945 __isl_keep isl_vec *pnt)
9947 isl_int n, d;
9948 isl_ctx *ctx;
9949 isl_val *v;
9951 if (!aff || !pnt)
9952 return NULL;
9954 ctx = isl_vec_get_ctx(aff);
9955 isl_int_init(n);
9956 isl_int_init(d);
9957 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9958 isl_int_mul(d, aff->el[0], pnt->el[0]);
9959 v = isl_val_rat_from_isl_int(ctx, n, d);
9960 v = isl_val_normalize(v);
9961 isl_int_clear(n);
9962 isl_int_clear(d);
9964 return v;
9967 /* Check that the domain space of "aff" is equal to "space".
9969 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9970 __isl_keep isl_space *space)
9972 isl_bool ok;
9974 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9975 if (ok < 0)
9976 return isl_stat_error;
9977 if (!ok)
9978 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9979 "incompatible spaces", return isl_stat_error);
9980 return isl_stat_ok;
9983 /* Evaluate the affine function "aff" in "pnt".
9985 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9986 __isl_take isl_point *pnt)
9988 isl_bool is_void;
9989 isl_val *v;
9990 isl_local_space *ls;
9992 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9993 goto error;
9994 is_void = isl_point_is_void(pnt);
9995 if (is_void < 0)
9996 goto error;
9997 if (is_void)
9998 return eval_void(aff, pnt);
10000 ls = isl_aff_get_domain_local_space(aff);
10001 pnt = isl_local_space_lift_point(ls, pnt);
10003 v = eval(aff->v, isl_point_peek_vec(pnt));
10005 isl_aff_free(aff);
10006 isl_point_free(pnt);
10008 return v;
10009 error:
10010 isl_aff_free(aff);
10011 isl_point_free(pnt);
10012 return NULL;