isl_test.c: test_coalesce_special2: return isl_stat
[isl.git] / isl_aff.c
blob5a160daab037bdd8f60ec2f1d6be50127cf8ef18
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 /* This function performs the same operation as isl_multi_aff_domain_map,
4075 * but is considered as a function on an isl_space when exported.
4077 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4078 __isl_take isl_space *space)
4080 return isl_multi_aff_domain_map(space);
4083 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4084 * of the space to its range.
4086 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4088 int i;
4089 isl_size n_in, n_out;
4090 isl_local_space *ls;
4091 isl_multi_aff *ma;
4093 if (!space)
4094 return NULL;
4095 if (!isl_space_is_map(space))
4096 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4097 "not a map space", goto error);
4099 n_in = isl_space_dim(space, isl_dim_in);
4100 n_out = isl_space_dim(space, isl_dim_out);
4101 if (n_in < 0 || n_out < 0)
4102 goto error;
4103 space = isl_space_range_map(space);
4105 ma = isl_multi_aff_alloc(isl_space_copy(space));
4106 if (n_out == 0) {
4107 isl_space_free(space);
4108 return ma;
4111 space = isl_space_domain(space);
4112 ls = isl_local_space_from_space(space);
4113 for (i = 0; i < n_out; ++i) {
4114 isl_aff *aff;
4116 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4117 isl_dim_set, n_in + i);
4118 ma = isl_multi_aff_set_aff(ma, i, aff);
4120 isl_local_space_free(ls);
4121 return ma;
4122 error:
4123 isl_space_free(space);
4124 return NULL;
4127 /* This function performs the same operation as isl_multi_aff_range_map,
4128 * but is considered as a function on an isl_space when exported.
4130 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4131 __isl_take isl_space *space)
4133 return isl_multi_aff_range_map(space);
4136 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4137 * of the space to its domain.
4139 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4140 __isl_take isl_space *space)
4142 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4145 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4146 * but is considered as a function on an isl_space when exported.
4148 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4149 __isl_take isl_space *space)
4151 return isl_pw_multi_aff_domain_map(space);
4154 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4155 * of the space to its range.
4157 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4158 __isl_take isl_space *space)
4160 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4163 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4164 * but is considered as a function on an isl_space when exported.
4166 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4167 __isl_take isl_space *space)
4169 return isl_pw_multi_aff_range_map(space);
4172 /* Given the space of a set and a range of set dimensions,
4173 * construct an isl_multi_aff that projects out those dimensions.
4175 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4176 __isl_take isl_space *space, enum isl_dim_type type,
4177 unsigned first, unsigned n)
4179 int i;
4180 isl_size dim;
4181 isl_local_space *ls;
4182 isl_multi_aff *ma;
4184 if (!space)
4185 return NULL;
4186 if (!isl_space_is_set(space))
4187 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4188 "expecting set space", goto error);
4189 if (type != isl_dim_set)
4190 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4191 "only set dimensions can be projected out", goto error);
4192 if (isl_space_check_range(space, type, first, n) < 0)
4193 goto error;
4195 dim = isl_space_dim(space, isl_dim_set);
4196 if (dim < 0)
4197 goto error;
4199 space = isl_space_from_domain(space);
4200 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4202 if (dim == n)
4203 return isl_multi_aff_alloc(space);
4205 ma = isl_multi_aff_alloc(isl_space_copy(space));
4206 space = isl_space_domain(space);
4207 ls = isl_local_space_from_space(space);
4209 for (i = 0; i < first; ++i) {
4210 isl_aff *aff;
4212 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4213 isl_dim_set, i);
4214 ma = isl_multi_aff_set_aff(ma, i, aff);
4217 for (i = 0; i < dim - (first + n); ++i) {
4218 isl_aff *aff;
4220 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4221 isl_dim_set, first + n + i);
4222 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4225 isl_local_space_free(ls);
4226 return ma;
4227 error:
4228 isl_space_free(space);
4229 return NULL;
4232 /* Given the space of a set and a range of set dimensions,
4233 * construct an isl_pw_multi_aff that projects out those dimensions.
4235 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4236 __isl_take isl_space *space, enum isl_dim_type type,
4237 unsigned first, unsigned n)
4239 isl_multi_aff *ma;
4241 ma = isl_multi_aff_project_out_map(space, type, first, n);
4242 return isl_pw_multi_aff_from_multi_aff(ma);
4245 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4246 * but is considered as a function on an isl_multi_aff when exported.
4248 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4249 __isl_take isl_multi_aff *ma)
4251 return isl_pw_multi_aff_from_multi_aff(ma);
4254 /* Create a piecewise multi-affine expression in the given space that maps each
4255 * input dimension to the corresponding output dimension.
4257 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4258 __isl_take isl_space *space)
4260 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4263 /* Create a piecewise multi expression that maps elements in the given space
4264 * to themselves.
4266 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4267 __isl_take isl_space *space)
4269 isl_multi_aff *ma;
4271 ma = isl_multi_aff_identity_on_domain_space(space);
4272 return isl_pw_multi_aff_from_multi_aff(ma);
4275 /* This function performs the same operation as
4276 * isl_pw_multi_aff_identity_on_domain_space,
4277 * but is considered as a function on an isl_space when exported.
4279 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4280 __isl_take isl_space *space)
4282 return isl_pw_multi_aff_identity_on_domain_space(space);
4285 /* Exploit the equalities in "eq" to simplify the affine expressions.
4287 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4288 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4290 int i;
4292 maff = isl_multi_aff_cow(maff);
4293 if (!maff || !eq)
4294 goto error;
4296 for (i = 0; i < maff->n; ++i) {
4297 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4298 isl_basic_set_copy(eq));
4299 if (!maff->u.p[i])
4300 goto error;
4303 isl_basic_set_free(eq);
4304 return maff;
4305 error:
4306 isl_basic_set_free(eq);
4307 isl_multi_aff_free(maff);
4308 return NULL;
4311 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4312 isl_int f)
4314 int i;
4316 maff = isl_multi_aff_cow(maff);
4317 if (!maff)
4318 return NULL;
4320 for (i = 0; i < maff->n; ++i) {
4321 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4322 if (!maff->u.p[i])
4323 return isl_multi_aff_free(maff);
4326 return maff;
4329 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4330 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4332 maff1 = isl_multi_aff_add(maff1, maff2);
4333 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4334 return maff1;
4337 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4339 if (!maff)
4340 return isl_bool_error;
4342 return isl_bool_false;
4345 /* Return the set of domain elements where "ma1" is lexicographically
4346 * smaller than or equal to "ma2".
4348 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4349 __isl_take isl_multi_aff *ma2)
4351 return isl_multi_aff_lex_ge_set(ma2, ma1);
4354 /* Return the set of domain elements where "ma1" is lexicographically
4355 * smaller than "ma2".
4357 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4358 __isl_take isl_multi_aff *ma2)
4360 return isl_multi_aff_lex_gt_set(ma2, ma1);
4363 /* Return the set of domain elements where "ma1" is lexicographically
4364 * greater than to "ma2". If "equal" is set, then include the domain
4365 * elements where they are equal.
4366 * Do this for the case where there are no entries.
4367 * In this case, "ma1" cannot be greater than "ma2",
4368 * but it is (greater than or) equal to "ma2".
4370 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4371 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4373 isl_space *space;
4375 space = isl_multi_aff_get_domain_space(ma1);
4377 isl_multi_aff_free(ma1);
4378 isl_multi_aff_free(ma2);
4380 if (equal)
4381 return isl_set_universe(space);
4382 else
4383 return isl_set_empty(space);
4386 /* Return the set where entry "i" of "ma1" and "ma2"
4387 * satisfy the relation prescribed by "cmp".
4389 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4390 __isl_keep isl_multi_aff *ma2, int i,
4391 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4392 __isl_take isl_aff *aff2))
4394 isl_aff *aff1, *aff2;
4396 aff1 = isl_multi_aff_get_at(ma1, i);
4397 aff2 = isl_multi_aff_get_at(ma2, i);
4398 return cmp(aff1, aff2);
4401 /* Return the set of domain elements where "ma1" is lexicographically
4402 * greater than to "ma2". If "equal" is set, then include the domain
4403 * elements where they are equal.
4405 * In particular, for all but the final entry,
4406 * include the set of elements where this entry is strictly greater in "ma1"
4407 * and all previous entries are equal.
4408 * The final entry is also allowed to be equal in the two functions
4409 * if "equal" is set.
4411 * The case where there are no entries is handled separately.
4413 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4414 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4416 int i;
4417 isl_size n;
4418 isl_space *space;
4419 isl_set *res;
4420 isl_set *equal_set;
4421 isl_set *gte;
4423 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4424 goto error;
4425 n = isl_multi_aff_size(ma1);
4426 if (n < 0)
4427 goto error;
4428 if (n == 0)
4429 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4431 space = isl_multi_aff_get_domain_space(ma1);
4432 res = isl_set_empty(isl_space_copy(space));
4433 equal_set = isl_set_universe(space);
4435 for (i = 0; i + 1 < n; ++i) {
4436 isl_bool empty;
4437 isl_set *gt, *eq;
4439 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4440 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4441 res = isl_set_union(res, gt);
4442 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4443 equal_set = isl_set_intersect(equal_set, eq);
4445 empty = isl_set_is_empty(equal_set);
4446 if (empty >= 0 && empty)
4447 break;
4450 if (equal)
4451 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4452 else
4453 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4454 isl_multi_aff_free(ma1);
4455 isl_multi_aff_free(ma2);
4457 gte = isl_set_intersect(gte, equal_set);
4458 return isl_set_union(res, gte);
4459 error:
4460 isl_multi_aff_free(ma1);
4461 isl_multi_aff_free(ma2);
4462 return NULL;
4465 /* Return the set of domain elements where "ma1" is lexicographically
4466 * greater than or equal to "ma2".
4468 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4469 __isl_take isl_multi_aff *ma2)
4471 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4474 /* Return the set of domain elements where "ma1" is lexicographically
4475 * greater than "ma2".
4477 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4478 __isl_take isl_multi_aff *ma2)
4480 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4483 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4485 #undef PW
4486 #define PW isl_pw_multi_aff
4487 #undef BASE
4488 #define BASE multi_aff
4489 #undef EL_IS_ZERO
4490 #define EL_IS_ZERO is_empty
4491 #undef ZERO
4492 #define ZERO empty
4493 #undef IS_ZERO
4494 #define IS_ZERO is_empty
4495 #undef FIELD
4496 #define FIELD maff
4497 #undef DEFAULT_IS_ZERO
4498 #define DEFAULT_IS_ZERO 0
4500 #include <isl_pw_templ.c>
4501 #include <isl_pw_add_constant_multi_val_templ.c>
4502 #include <isl_pw_add_constant_val_templ.c>
4503 #include <isl_pw_bind_domain_templ.c>
4504 #include <isl_pw_insert_dims_templ.c>
4505 #include <isl_pw_insert_domain_templ.c>
4506 #include <isl_pw_locals_templ.c>
4507 #include <isl_pw_move_dims_templ.c>
4508 #include <isl_pw_neg_templ.c>
4509 #include <isl_pw_pullback_templ.c>
4510 #include <isl_pw_range_tuple_id_templ.c>
4511 #include <isl_pw_union_opt.c>
4513 #undef BASE
4514 #define BASE pw_multi_aff
4516 #include <isl_union_multi.c>
4517 #include "isl_union_locals_templ.c"
4518 #include <isl_union_neg.c>
4520 #undef BASE
4521 #define BASE multi_aff
4523 #include <isl_union_pw_templ.c>
4525 /* Generic function for extracting a factor from a product "pma".
4526 * "check_space" checks that the space is that of the right kind of product.
4527 * "space_factor" extracts the factor from the space.
4528 * "multi_aff_factor" extracts the factor from the constituent functions.
4530 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4531 __isl_take isl_pw_multi_aff *pma,
4532 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4533 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4534 __isl_give isl_multi_aff *(*multi_aff_factor)(
4535 __isl_take isl_multi_aff *ma))
4537 int i;
4538 isl_space *space;
4540 if (check_space(pma) < 0)
4541 return isl_pw_multi_aff_free(pma);
4543 space = isl_pw_multi_aff_take_space(pma);
4544 space = space_factor(space);
4546 for (i = 0; pma && i < pma->n; ++i) {
4547 isl_multi_aff *ma;
4549 ma = isl_pw_multi_aff_take_base_at(pma, i);
4550 ma = multi_aff_factor(ma);
4551 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4554 pma = isl_pw_multi_aff_restore_space(pma, space);
4556 return pma;
4559 /* Is the range of "pma" a wrapped relation?
4561 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4562 __isl_keep isl_pw_multi_aff *pma)
4564 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4567 /* Check that the range of "pma" is a product.
4569 static isl_stat pw_multi_aff_check_range_product(
4570 __isl_keep isl_pw_multi_aff *pma)
4572 isl_bool wraps;
4574 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4575 if (wraps < 0)
4576 return isl_stat_error;
4577 if (!wraps)
4578 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4579 "range is not a product", return isl_stat_error);
4580 return isl_stat_ok;
4583 /* Given a function A -> [B -> C], extract the function A -> B.
4585 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4586 __isl_take isl_pw_multi_aff *pma)
4588 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4589 &isl_space_range_factor_domain,
4590 &isl_multi_aff_range_factor_domain);
4593 /* Given a function A -> [B -> C], extract the function A -> C.
4595 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4596 __isl_take isl_pw_multi_aff *pma)
4598 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4599 &isl_space_range_factor_range,
4600 &isl_multi_aff_range_factor_range);
4603 /* Given two piecewise multi affine expressions, return a piecewise
4604 * multi-affine expression defined on the union of the definition domains
4605 * of the inputs that is equal to the lexicographic maximum of the two
4606 * inputs on each cell. If only one of the two inputs is defined on
4607 * a given cell, then it is considered to be the maximum.
4609 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4610 __isl_take isl_pw_multi_aff *pma1,
4611 __isl_take isl_pw_multi_aff *pma2)
4613 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4614 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4615 &isl_multi_aff_lex_ge_set);
4618 /* Given two piecewise multi affine expressions, return a piecewise
4619 * multi-affine expression defined on the union of the definition domains
4620 * of the inputs that is equal to the lexicographic minimum of the two
4621 * inputs on each cell. If only one of the two inputs is defined on
4622 * a given cell, then it is considered to be the minimum.
4624 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4625 __isl_take isl_pw_multi_aff *pma1,
4626 __isl_take isl_pw_multi_aff *pma2)
4628 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4629 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4630 &isl_multi_aff_lex_le_set);
4633 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4634 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4636 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4637 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4638 &isl_multi_aff_add);
4641 /* Subtract "pma2" from "pma1" and return the result.
4643 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4644 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4646 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4647 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4648 &isl_multi_aff_sub);
4651 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4652 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4654 return isl_pw_multi_aff_union_add_(pma1, pma2);
4657 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4658 * with the actual sum on the shared domain and
4659 * the defined expression on the symmetric difference of the domains.
4661 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4662 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4664 return isl_union_pw_aff_union_add_(upa1, upa2);
4667 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4668 * with the actual sum on the shared domain and
4669 * the defined expression on the symmetric difference of the domains.
4671 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4672 __isl_take isl_union_pw_multi_aff *upma1,
4673 __isl_take isl_union_pw_multi_aff *upma2)
4675 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4678 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4679 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4681 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4682 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4684 int i, j, n;
4685 isl_space *space;
4686 isl_pw_multi_aff *res;
4688 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4689 goto error;
4691 n = pma1->n * pma2->n;
4692 space = isl_space_product(isl_space_copy(pma1->dim),
4693 isl_space_copy(pma2->dim));
4694 res = isl_pw_multi_aff_alloc_size(space, n);
4696 for (i = 0; i < pma1->n; ++i) {
4697 for (j = 0; j < pma2->n; ++j) {
4698 isl_set *domain;
4699 isl_multi_aff *ma;
4701 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4702 isl_set_copy(pma2->p[j].set));
4703 ma = isl_multi_aff_product(
4704 isl_multi_aff_copy(pma1->p[i].maff),
4705 isl_multi_aff_copy(pma2->p[j].maff));
4706 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4710 isl_pw_multi_aff_free(pma1);
4711 isl_pw_multi_aff_free(pma2);
4712 return res;
4713 error:
4714 isl_pw_multi_aff_free(pma1);
4715 isl_pw_multi_aff_free(pma2);
4716 return NULL;
4719 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4720 * denominator "denom".
4721 * "denom" is allowed to be negative, in which case the actual denominator
4722 * is -denom and the expressions are added instead.
4724 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4725 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4727 int i, first;
4728 int sign;
4729 isl_int d;
4731 first = isl_seq_first_non_zero(c, n);
4732 if (first == -1)
4733 return aff;
4735 sign = isl_int_sgn(denom);
4736 isl_int_init(d);
4737 isl_int_abs(d, denom);
4738 for (i = first; i < n; ++i) {
4739 isl_aff *aff_i;
4741 if (isl_int_is_zero(c[i]))
4742 continue;
4743 aff_i = isl_multi_aff_get_aff(ma, i);
4744 aff_i = isl_aff_scale(aff_i, c[i]);
4745 aff_i = isl_aff_scale_down(aff_i, d);
4746 if (sign >= 0)
4747 aff = isl_aff_sub(aff, aff_i);
4748 else
4749 aff = isl_aff_add(aff, aff_i);
4751 isl_int_clear(d);
4753 return aff;
4756 /* Extract an affine expression that expresses the output dimension "pos"
4757 * of "bmap" in terms of the parameters and input dimensions from
4758 * equality "eq".
4759 * Note that this expression may involve integer divisions defined
4760 * in terms of parameters and input dimensions.
4761 * The equality may also involve references to earlier (but not later)
4762 * output dimensions. These are replaced by the corresponding elements
4763 * in "ma".
4765 * If the equality is of the form
4767 * f(i) + h(j) + a x + g(i) = 0,
4769 * with f(i) a linear combinations of the parameters and input dimensions,
4770 * g(i) a linear combination of integer divisions defined in terms of the same
4771 * and h(j) a linear combinations of earlier output dimensions,
4772 * then the affine expression is
4774 * (-f(i) - g(i))/a - h(j)/a
4776 * If the equality is of the form
4778 * f(i) + h(j) - a x + g(i) = 0,
4780 * then the affine expression is
4782 * (f(i) + g(i))/a - h(j)/(-a)
4785 * If "div" refers to an integer division (i.e., it is smaller than
4786 * the number of integer divisions), then the equality constraint
4787 * does involve an integer division (the one at position "div") that
4788 * is defined in terms of output dimensions. However, this integer
4789 * division can be eliminated by exploiting a pair of constraints
4790 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4791 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4792 * -l + x >= 0.
4793 * In particular, let
4795 * x = e(i) + m floor(...)
4797 * with e(i) the expression derived above and floor(...) the integer
4798 * division involving output dimensions.
4799 * From
4801 * l <= x <= l + n,
4803 * we have
4805 * 0 <= x - l <= n
4807 * This means
4809 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4810 * = (e(i) - l) mod m
4812 * Therefore,
4814 * x - l = (e(i) - l) mod m
4816 * or
4818 * x = ((e(i) - l) mod m) + l
4820 * The variable "shift" below contains the expression -l, which may
4821 * also involve a linear combination of earlier output dimensions.
4823 static __isl_give isl_aff *extract_aff_from_equality(
4824 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4825 __isl_keep isl_multi_aff *ma)
4827 unsigned o_out;
4828 isl_size n_div, n_out;
4829 isl_ctx *ctx;
4830 isl_local_space *ls;
4831 isl_aff *aff, *shift;
4832 isl_val *mod;
4834 ctx = isl_basic_map_get_ctx(bmap);
4835 ls = isl_basic_map_get_local_space(bmap);
4836 ls = isl_local_space_domain(ls);
4837 aff = isl_aff_alloc(isl_local_space_copy(ls));
4838 if (!aff)
4839 goto error;
4840 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4841 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4842 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4843 if (n_out < 0 || n_div < 0)
4844 goto error;
4845 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4846 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4847 isl_seq_cpy(aff->v->el + 1 + o_out,
4848 bmap->eq[eq] + o_out + n_out, n_div);
4849 } else {
4850 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4851 isl_seq_neg(aff->v->el + 1 + o_out,
4852 bmap->eq[eq] + o_out + n_out, n_div);
4854 if (div < n_div)
4855 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4856 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4857 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4858 bmap->eq[eq][o_out + pos]);
4859 if (div < n_div) {
4860 shift = isl_aff_alloc(isl_local_space_copy(ls));
4861 if (!shift)
4862 goto error;
4863 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4864 isl_seq_cpy(shift->v->el + 1 + o_out,
4865 bmap->ineq[ineq] + o_out + n_out, n_div);
4866 isl_int_set_si(shift->v->el[0], 1);
4867 shift = subtract_initial(shift, ma, pos,
4868 bmap->ineq[ineq] + o_out, ctx->negone);
4869 aff = isl_aff_add(aff, isl_aff_copy(shift));
4870 mod = isl_val_int_from_isl_int(ctx,
4871 bmap->eq[eq][o_out + n_out + div]);
4872 mod = isl_val_abs(mod);
4873 aff = isl_aff_mod_val(aff, mod);
4874 aff = isl_aff_sub(aff, shift);
4877 isl_local_space_free(ls);
4878 return aff;
4879 error:
4880 isl_local_space_free(ls);
4881 isl_aff_free(aff);
4882 return NULL;
4885 /* Given a basic map with output dimensions defined
4886 * in terms of the parameters input dimensions and earlier
4887 * output dimensions using an equality (and possibly a pair on inequalities),
4888 * extract an isl_aff that expresses output dimension "pos" in terms
4889 * of the parameters and input dimensions.
4890 * Note that this expression may involve integer divisions defined
4891 * in terms of parameters and input dimensions.
4892 * "ma" contains the expressions corresponding to earlier output dimensions.
4894 * This function shares some similarities with
4895 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4897 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4898 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4900 int eq, div, ineq;
4901 isl_aff *aff;
4903 if (!bmap)
4904 return NULL;
4905 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4906 if (eq >= bmap->n_eq)
4907 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4908 "unable to find suitable equality", return NULL);
4909 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4911 aff = isl_aff_remove_unused_divs(aff);
4912 return aff;
4915 /* Given a basic map where each output dimension is defined
4916 * in terms of the parameters and input dimensions using an equality,
4917 * extract an isl_multi_aff that expresses the output dimensions in terms
4918 * of the parameters and input dimensions.
4920 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4921 __isl_take isl_basic_map *bmap)
4923 int i;
4924 isl_size n_out;
4925 isl_multi_aff *ma;
4927 if (!bmap)
4928 return NULL;
4930 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4931 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4932 if (n_out < 0)
4933 ma = isl_multi_aff_free(ma);
4935 for (i = 0; i < n_out; ++i) {
4936 isl_aff *aff;
4938 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4939 ma = isl_multi_aff_set_aff(ma, i, aff);
4942 isl_basic_map_free(bmap);
4944 return ma;
4947 /* Given a basic set where each set dimension is defined
4948 * in terms of the parameters using an equality,
4949 * extract an isl_multi_aff that expresses the set dimensions in terms
4950 * of the parameters.
4952 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4953 __isl_take isl_basic_set *bset)
4955 return extract_isl_multi_aff_from_basic_map(bset);
4958 /* Create an isl_pw_multi_aff that is equivalent to
4959 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4960 * The given basic map is such that each output dimension is defined
4961 * in terms of the parameters and input dimensions using an equality.
4963 * Since some applications expect the result of isl_pw_multi_aff_from_map
4964 * to only contain integer affine expressions, we compute the floor
4965 * of the expression before returning.
4967 * Remove all constraints involving local variables without
4968 * an explicit representation (resulting in the removal of those
4969 * local variables) prior to the actual extraction to ensure
4970 * that the local spaces in which the resulting affine expressions
4971 * are created do not contain any unknown local variables.
4972 * Removing such constraints is safe because constraints involving
4973 * unknown local variables are not used to determine whether
4974 * a basic map is obviously single-valued.
4976 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4977 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4979 isl_multi_aff *ma;
4981 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4982 ma = extract_isl_multi_aff_from_basic_map(bmap);
4983 ma = isl_multi_aff_floor(ma);
4984 return isl_pw_multi_aff_alloc(domain, ma);
4987 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4988 * This obviously only works if the input "map" is single-valued.
4989 * If so, we compute the lexicographic minimum of the image in the form
4990 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4991 * to its lexicographic minimum.
4992 * If the input is not single-valued, we produce an error.
4994 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4995 __isl_take isl_map *map)
4997 int i;
4998 int sv;
4999 isl_pw_multi_aff *pma;
5001 sv = isl_map_is_single_valued(map);
5002 if (sv < 0)
5003 goto error;
5004 if (!sv)
5005 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5006 "map is not single-valued", goto error);
5007 map = isl_map_make_disjoint(map);
5008 if (!map)
5009 return NULL;
5011 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5013 for (i = 0; i < map->n; ++i) {
5014 isl_pw_multi_aff *pma_i;
5015 isl_basic_map *bmap;
5016 bmap = isl_basic_map_copy(map->p[i]);
5017 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5018 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5021 isl_map_free(map);
5022 return pma;
5023 error:
5024 isl_map_free(map);
5025 return NULL;
5028 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5029 * taking into account that the output dimension at position "d"
5030 * can be represented as
5032 * x = floor((e(...) + c1) / m)
5034 * given that constraint "i" is of the form
5036 * e(...) + c1 - m x >= 0
5039 * Let "map" be of the form
5041 * A -> B
5043 * We construct a mapping
5045 * A -> [A -> x = floor(...)]
5047 * apply that to the map, obtaining
5049 * [A -> x = floor(...)] -> B
5051 * and equate dimension "d" to x.
5052 * We then compute a isl_pw_multi_aff representation of the resulting map
5053 * and plug in the mapping above.
5055 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5056 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5058 isl_ctx *ctx;
5059 isl_space *space = NULL;
5060 isl_local_space *ls;
5061 isl_multi_aff *ma;
5062 isl_aff *aff;
5063 isl_vec *v;
5064 isl_map *insert;
5065 int offset;
5066 isl_size n;
5067 isl_size n_in;
5068 isl_pw_multi_aff *pma;
5069 isl_bool is_set;
5071 is_set = isl_map_is_set(map);
5072 if (is_set < 0)
5073 goto error;
5075 offset = isl_basic_map_offset(hull, isl_dim_out);
5076 ctx = isl_map_get_ctx(map);
5077 space = isl_space_domain(isl_map_get_space(map));
5078 n_in = isl_space_dim(space, isl_dim_set);
5079 n = isl_space_dim(space, isl_dim_all);
5080 if (n_in < 0 || n < 0)
5081 goto error;
5083 v = isl_vec_alloc(ctx, 1 + 1 + n);
5084 if (v) {
5085 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5086 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5088 isl_basic_map_free(hull);
5090 ls = isl_local_space_from_space(isl_space_copy(space));
5091 aff = isl_aff_alloc_vec(ls, v);
5092 aff = isl_aff_floor(aff);
5093 if (is_set) {
5094 isl_space_free(space);
5095 ma = isl_multi_aff_from_aff(aff);
5096 } else {
5097 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5098 ma = isl_multi_aff_range_product(ma,
5099 isl_multi_aff_from_aff(aff));
5102 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5103 map = isl_map_apply_domain(map, insert);
5104 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5105 pma = isl_pw_multi_aff_from_map(map);
5106 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5108 return pma;
5109 error:
5110 isl_space_free(space);
5111 isl_map_free(map);
5112 isl_basic_map_free(hull);
5113 return NULL;
5116 /* Is constraint "c" of the form
5118 * e(...) + c1 - m x >= 0
5120 * or
5122 * -e(...) + c2 + m x >= 0
5124 * where m > 1 and e only depends on parameters and input dimemnsions?
5126 * "offset" is the offset of the output dimensions
5127 * "pos" is the position of output dimension x.
5129 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5131 if (isl_int_is_zero(c[offset + d]))
5132 return 0;
5133 if (isl_int_is_one(c[offset + d]))
5134 return 0;
5135 if (isl_int_is_negone(c[offset + d]))
5136 return 0;
5137 if (isl_seq_first_non_zero(c + offset, d) != -1)
5138 return 0;
5139 if (isl_seq_first_non_zero(c + offset + d + 1,
5140 total - (offset + d + 1)) != -1)
5141 return 0;
5142 return 1;
5145 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5147 * As a special case, we first check if there is any pair of constraints,
5148 * shared by all the basic maps in "map" that force a given dimension
5149 * to be equal to the floor of some affine combination of the input dimensions.
5151 * In particular, if we can find two constraints
5153 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5155 * and
5157 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5159 * where m > 1 and e only depends on parameters and input dimemnsions,
5160 * and such that
5162 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5164 * then we know that we can take
5166 * x = floor((e(...) + c1) / m)
5168 * without having to perform any computation.
5170 * Note that we know that
5172 * c1 + c2 >= 1
5174 * If c1 + c2 were 0, then we would have detected an equality during
5175 * simplification. If c1 + c2 were negative, then we would have detected
5176 * a contradiction.
5178 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5179 __isl_take isl_map *map)
5181 int d;
5182 isl_size dim;
5183 int i, j, n;
5184 int offset;
5185 isl_size total;
5186 isl_int sum;
5187 isl_basic_map *hull;
5189 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5190 dim = isl_map_dim(map, isl_dim_out);
5191 total = isl_basic_map_dim(hull, isl_dim_all);
5192 if (dim < 0 || total < 0)
5193 goto error;
5195 isl_int_init(sum);
5196 offset = isl_basic_map_offset(hull, isl_dim_out);
5197 n = hull->n_ineq;
5198 for (d = 0; d < dim; ++d) {
5199 for (i = 0; i < n; ++i) {
5200 if (!is_potential_div_constraint(hull->ineq[i],
5201 offset, d, 1 + total))
5202 continue;
5203 for (j = i + 1; j < n; ++j) {
5204 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5205 hull->ineq[j] + 1, total))
5206 continue;
5207 isl_int_add(sum, hull->ineq[i][0],
5208 hull->ineq[j][0]);
5209 if (isl_int_abs_lt(sum,
5210 hull->ineq[i][offset + d]))
5211 break;
5214 if (j >= n)
5215 continue;
5216 isl_int_clear(sum);
5217 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5218 j = i;
5219 return pw_multi_aff_from_map_div(map, hull, d, j);
5222 isl_int_clear(sum);
5223 isl_basic_map_free(hull);
5224 return pw_multi_aff_from_map_base(map);
5225 error:
5226 isl_map_free(map);
5227 isl_basic_map_free(hull);
5228 return NULL;
5231 /* Given an affine expression
5233 * [A -> B] -> f(A,B)
5235 * construct an isl_multi_aff
5237 * [A -> B] -> B'
5239 * such that dimension "d" in B' is set to "aff" and the remaining
5240 * dimensions are set equal to the corresponding dimensions in B.
5241 * "n_in" is the dimension of the space A.
5242 * "n_out" is the dimension of the space B.
5244 * If "is_set" is set, then the affine expression is of the form
5246 * [B] -> f(B)
5248 * and we construct an isl_multi_aff
5250 * B -> B'
5252 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5253 unsigned n_in, unsigned n_out, int is_set)
5255 int i;
5256 isl_multi_aff *ma;
5257 isl_space *space, *space2;
5258 isl_local_space *ls;
5260 space = isl_aff_get_domain_space(aff);
5261 ls = isl_local_space_from_space(isl_space_copy(space));
5262 space2 = isl_space_copy(space);
5263 if (!is_set)
5264 space2 = isl_space_range(isl_space_unwrap(space2));
5265 space = isl_space_map_from_domain_and_range(space, space2);
5266 ma = isl_multi_aff_alloc(space);
5267 ma = isl_multi_aff_set_aff(ma, d, aff);
5269 for (i = 0; i < n_out; ++i) {
5270 if (i == d)
5271 continue;
5272 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5273 isl_dim_set, n_in + i);
5274 ma = isl_multi_aff_set_aff(ma, i, aff);
5277 isl_local_space_free(ls);
5279 return ma;
5282 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5283 * taking into account that the dimension at position "d" can be written as
5285 * x = m a + f(..) (1)
5287 * where m is equal to "gcd".
5288 * "i" is the index of the equality in "hull" that defines f(..).
5289 * In particular, the equality is of the form
5291 * f(..) - x + m g(existentials) = 0
5293 * or
5295 * -f(..) + x + m g(existentials) = 0
5297 * We basically plug (1) into "map", resulting in a map with "a"
5298 * in the range instead of "x". The corresponding isl_pw_multi_aff
5299 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5301 * Specifically, given the input map
5303 * A -> B
5305 * We first wrap it into a set
5307 * [A -> B]
5309 * and define (1) on top of the corresponding space, resulting in "aff".
5310 * We use this to create an isl_multi_aff that maps the output position "d"
5311 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5312 * We plug this into the wrapped map, unwrap the result and compute the
5313 * corresponding isl_pw_multi_aff.
5314 * The result is an expression
5316 * A -> T(A)
5318 * We adjust that to
5320 * A -> [A -> T(A)]
5322 * so that we can plug that into "aff", after extending the latter to
5323 * a mapping
5325 * [A -> B] -> B'
5328 * If "map" is actually a set, then there is no "A" space, meaning
5329 * that we do not need to perform any wrapping, and that the result
5330 * of the recursive call is of the form
5332 * [T]
5334 * which is plugged into a mapping of the form
5336 * B -> B'
5338 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5339 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5340 isl_int gcd)
5342 isl_set *set;
5343 isl_space *space;
5344 isl_local_space *ls;
5345 isl_aff *aff;
5346 isl_multi_aff *ma;
5347 isl_pw_multi_aff *pma, *id;
5348 isl_size n_in;
5349 unsigned o_out;
5350 isl_size n_out;
5351 isl_bool is_set;
5353 is_set = isl_map_is_set(map);
5354 if (is_set < 0)
5355 goto error;
5357 n_in = isl_basic_map_dim(hull, isl_dim_in);
5358 n_out = isl_basic_map_dim(hull, isl_dim_out);
5359 if (n_in < 0 || n_out < 0)
5360 goto error;
5361 o_out = isl_basic_map_offset(hull, isl_dim_out);
5363 if (is_set)
5364 set = map;
5365 else
5366 set = isl_map_wrap(map);
5367 space = isl_space_map_from_set(isl_set_get_space(set));
5368 ma = isl_multi_aff_identity(space);
5369 ls = isl_local_space_from_space(isl_set_get_space(set));
5370 aff = isl_aff_alloc(ls);
5371 if (aff) {
5372 isl_int_set_si(aff->v->el[0], 1);
5373 if (isl_int_is_one(hull->eq[i][o_out + d]))
5374 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5375 aff->v->size - 1);
5376 else
5377 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5378 aff->v->size - 1);
5379 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5381 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5382 set = isl_set_preimage_multi_aff(set, ma);
5384 ma = range_map(aff, d, n_in, n_out, is_set);
5386 if (is_set)
5387 map = set;
5388 else
5389 map = isl_set_unwrap(set);
5390 pma = isl_pw_multi_aff_from_map(map);
5392 if (!is_set) {
5393 space = isl_pw_multi_aff_get_domain_space(pma);
5394 space = isl_space_map_from_set(space);
5395 id = isl_pw_multi_aff_identity(space);
5396 pma = isl_pw_multi_aff_range_product(id, pma);
5398 id = isl_pw_multi_aff_from_multi_aff(ma);
5399 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5401 isl_basic_map_free(hull);
5402 return pma;
5403 error:
5404 isl_map_free(map);
5405 isl_basic_map_free(hull);
5406 return NULL;
5409 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5410 * "hull" contains the equalities valid for "map".
5412 * Check if any of the output dimensions is "strided".
5413 * That is, we check if it can be written as
5415 * x = m a + f(..)
5417 * with m greater than 1, a some combination of existentially quantified
5418 * variables and f an expression in the parameters and input dimensions.
5419 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5421 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5422 * special case.
5424 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5425 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5427 int i, j;
5428 isl_size n_out;
5429 unsigned o_out;
5430 isl_size n_div;
5431 unsigned o_div;
5432 isl_int gcd;
5434 n_div = isl_basic_map_dim(hull, isl_dim_div);
5435 n_out = isl_basic_map_dim(hull, isl_dim_out);
5436 if (n_div < 0 || n_out < 0)
5437 goto error;
5439 if (n_div == 0) {
5440 isl_basic_map_free(hull);
5441 return pw_multi_aff_from_map_check_div(map);
5444 isl_int_init(gcd);
5446 o_div = isl_basic_map_offset(hull, isl_dim_div);
5447 o_out = isl_basic_map_offset(hull, isl_dim_out);
5449 for (i = 0; i < n_out; ++i) {
5450 for (j = 0; j < hull->n_eq; ++j) {
5451 isl_int *eq = hull->eq[j];
5452 isl_pw_multi_aff *res;
5454 if (!isl_int_is_one(eq[o_out + i]) &&
5455 !isl_int_is_negone(eq[o_out + i]))
5456 continue;
5457 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5458 continue;
5459 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5460 n_out - (i + 1)) != -1)
5461 continue;
5462 isl_seq_gcd(eq + o_div, n_div, &gcd);
5463 if (isl_int_is_zero(gcd))
5464 continue;
5465 if (isl_int_is_one(gcd))
5466 continue;
5468 res = pw_multi_aff_from_map_stride(map, hull,
5469 i, j, gcd);
5470 isl_int_clear(gcd);
5471 return res;
5475 isl_int_clear(gcd);
5476 isl_basic_map_free(hull);
5477 return pw_multi_aff_from_map_check_div(map);
5478 error:
5479 isl_map_free(map);
5480 isl_basic_map_free(hull);
5481 return NULL;
5484 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5486 * As a special case, we first check if all output dimensions are uniquely
5487 * defined in terms of the parameters and input dimensions over the entire
5488 * domain. If so, we extract the desired isl_pw_multi_aff directly
5489 * from the affine hull of "map" and its domain.
5491 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5492 * special cases.
5494 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5496 isl_bool sv;
5497 isl_size n;
5498 isl_basic_map *hull;
5500 n = isl_map_n_basic_map(map);
5501 if (n < 0)
5502 goto error;
5504 if (n == 1) {
5505 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5506 hull = isl_basic_map_plain_affine_hull(hull);
5507 sv = isl_basic_map_plain_is_single_valued(hull);
5508 if (sv >= 0 && sv)
5509 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5510 hull);
5511 isl_basic_map_free(hull);
5513 map = isl_map_detect_equalities(map);
5514 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5515 sv = isl_basic_map_plain_is_single_valued(hull);
5516 if (sv >= 0 && sv)
5517 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5518 if (sv >= 0)
5519 return pw_multi_aff_from_map_check_strides(map, hull);
5520 isl_basic_map_free(hull);
5521 error:
5522 isl_map_free(map);
5523 return NULL;
5526 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5527 * but is considered as a function on an isl_map when exported.
5529 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5531 return isl_pw_multi_aff_from_map(map);
5534 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5536 return isl_pw_multi_aff_from_map(set);
5539 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5540 * but is considered as a function on an isl_set when exported.
5542 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5544 return isl_pw_multi_aff_from_set(set);
5547 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5548 * add it to *user.
5550 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5552 isl_union_pw_multi_aff **upma = user;
5553 isl_pw_multi_aff *pma;
5555 pma = isl_pw_multi_aff_from_map(map);
5556 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5558 return *upma ? isl_stat_ok : isl_stat_error;
5561 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5562 * domain.
5564 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5565 __isl_take isl_aff *aff)
5567 isl_multi_aff *ma;
5568 isl_pw_multi_aff *pma;
5570 ma = isl_multi_aff_from_aff(aff);
5571 pma = isl_pw_multi_aff_from_multi_aff(ma);
5572 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5575 /* Try and create an isl_union_pw_multi_aff that is equivalent
5576 * to the given isl_union_map.
5577 * The isl_union_map is required to be single-valued in each space.
5578 * Otherwise, an error is produced.
5580 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5581 __isl_take isl_union_map *umap)
5583 isl_space *space;
5584 isl_union_pw_multi_aff *upma;
5586 space = isl_union_map_get_space(umap);
5587 upma = isl_union_pw_multi_aff_empty(space);
5588 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5589 upma = isl_union_pw_multi_aff_free(upma);
5590 isl_union_map_free(umap);
5592 return upma;
5595 /* This function performs the same operation as
5596 * isl_union_pw_multi_aff_from_union_map,
5597 * but is considered as a function on an isl_union_map when exported.
5599 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5600 __isl_take isl_union_map *umap)
5602 return isl_union_pw_multi_aff_from_union_map(umap);
5605 /* Try and create an isl_union_pw_multi_aff that is equivalent
5606 * to the given isl_union_set.
5607 * The isl_union_set is required to be a singleton in each space.
5608 * Otherwise, an error is produced.
5610 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5611 __isl_take isl_union_set *uset)
5613 return isl_union_pw_multi_aff_from_union_map(uset);
5616 /* Return the piecewise affine expression "set ? 1 : 0".
5618 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5620 isl_pw_aff *pa;
5621 isl_space *space = isl_set_get_space(set);
5622 isl_local_space *ls = isl_local_space_from_space(space);
5623 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5624 isl_aff *one = isl_aff_zero_on_domain(ls);
5626 one = isl_aff_add_constant_si(one, 1);
5627 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5628 set = isl_set_complement(set);
5629 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5631 return pa;
5634 /* Plug in "subs" for dimension "type", "pos" of "aff".
5636 * Let i be the dimension to replace and let "subs" be of the form
5638 * f/d
5640 * and "aff" of the form
5642 * (a i + g)/m
5644 * The result is
5646 * (a f + d g')/(m d)
5648 * where g' is the result of plugging in "subs" in each of the integer
5649 * divisions in g.
5651 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5652 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5654 isl_ctx *ctx;
5655 isl_int v;
5656 isl_size n_div;
5658 aff = isl_aff_cow(aff);
5659 if (!aff || !subs)
5660 return isl_aff_free(aff);
5662 ctx = isl_aff_get_ctx(aff);
5663 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5664 isl_die(ctx, isl_error_invalid,
5665 "spaces don't match", return isl_aff_free(aff));
5666 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5667 if (n_div < 0)
5668 return isl_aff_free(aff);
5669 if (n_div != 0)
5670 isl_die(ctx, isl_error_unsupported,
5671 "cannot handle divs yet", return isl_aff_free(aff));
5673 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5674 if (!aff->ls)
5675 return isl_aff_free(aff);
5677 aff->v = isl_vec_cow(aff->v);
5678 if (!aff->v)
5679 return isl_aff_free(aff);
5681 pos += isl_local_space_offset(aff->ls, type);
5683 isl_int_init(v);
5684 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5685 aff->v->size, subs->v->size, v);
5686 isl_int_clear(v);
5688 return aff;
5691 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5692 * expressions in "maff".
5694 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5695 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5696 __isl_keep isl_aff *subs)
5698 int i;
5700 maff = isl_multi_aff_cow(maff);
5701 if (!maff || !subs)
5702 return isl_multi_aff_free(maff);
5704 if (type == isl_dim_in)
5705 type = isl_dim_set;
5707 for (i = 0; i < maff->n; ++i) {
5708 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5709 type, pos, subs);
5710 if (!maff->u.p[i])
5711 return isl_multi_aff_free(maff);
5714 return maff;
5717 /* Plug in "subs" for dimension "type", "pos" of "pma".
5719 * pma is of the form
5721 * A_i(v) -> M_i(v)
5723 * while subs is of the form
5725 * v' = B_j(v) -> S_j
5727 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5728 * has a contribution in the result, in particular
5730 * C_ij(S_j) -> M_i(S_j)
5732 * Note that plugging in S_j in C_ij may also result in an empty set
5733 * and this contribution should simply be discarded.
5735 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5736 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5737 __isl_keep isl_pw_aff *subs)
5739 int i, j, n;
5740 isl_pw_multi_aff *res;
5742 if (!pma || !subs)
5743 return isl_pw_multi_aff_free(pma);
5745 n = pma->n * subs->n;
5746 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5748 for (i = 0; i < pma->n; ++i) {
5749 for (j = 0; j < subs->n; ++j) {
5750 isl_set *common;
5751 isl_multi_aff *res_ij;
5752 int empty;
5754 common = isl_set_intersect(
5755 isl_set_copy(pma->p[i].set),
5756 isl_set_copy(subs->p[j].set));
5757 common = isl_set_substitute(common,
5758 type, pos, subs->p[j].aff);
5759 empty = isl_set_plain_is_empty(common);
5760 if (empty < 0 || empty) {
5761 isl_set_free(common);
5762 if (empty < 0)
5763 goto error;
5764 continue;
5767 res_ij = isl_multi_aff_substitute(
5768 isl_multi_aff_copy(pma->p[i].maff),
5769 type, pos, subs->p[j].aff);
5771 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5775 isl_pw_multi_aff_free(pma);
5776 return res;
5777 error:
5778 isl_pw_multi_aff_free(pma);
5779 isl_pw_multi_aff_free(res);
5780 return NULL;
5783 /* Compute the preimage of a range of dimensions in the affine expression "src"
5784 * under "ma" and put the result in "dst". The number of dimensions in "src"
5785 * that precede the range is given by "n_before". The number of dimensions
5786 * in the range is given by the number of output dimensions of "ma".
5787 * The number of dimensions that follow the range is given by "n_after".
5788 * If "has_denom" is set (to one),
5789 * then "src" and "dst" have an extra initial denominator.
5790 * "n_div_ma" is the number of existentials in "ma"
5791 * "n_div_bset" is the number of existentials in "src"
5792 * The resulting "dst" (which is assumed to have been allocated by
5793 * the caller) contains coefficients for both sets of existentials,
5794 * first those in "ma" and then those in "src".
5795 * f, c1, c2 and g are temporary objects that have been initialized
5796 * by the caller.
5798 * Let src represent the expression
5800 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5802 * and let ma represent the expressions
5804 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5806 * We start out with the following expression for dst:
5808 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5810 * with the multiplication factor f initially equal to 1
5811 * and f \sum_i b_i v_i kept separately.
5812 * For each x_i that we substitute, we multiply the numerator
5813 * (and denominator) of dst by c_1 = m_i and add the numerator
5814 * of the x_i expression multiplied by c_2 = f b_i,
5815 * after removing the common factors of c_1 and c_2.
5816 * The multiplication factor f also needs to be multiplied by c_1
5817 * for the next x_j, j > i.
5819 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5820 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5821 int n_div_ma, int n_div_bmap,
5822 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5824 int i;
5825 isl_size n_param, n_in, n_out;
5826 int o_dst, o_src;
5828 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5829 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5830 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5831 if (n_param < 0 || n_in < 0 || n_out < 0)
5832 return isl_stat_error;
5834 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5835 o_dst = o_src = has_denom + 1 + n_param + n_before;
5836 isl_seq_clr(dst + o_dst, n_in);
5837 o_dst += n_in;
5838 o_src += n_out;
5839 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5840 o_dst += n_after;
5841 o_src += n_after;
5842 isl_seq_clr(dst + o_dst, n_div_ma);
5843 o_dst += n_div_ma;
5844 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5846 isl_int_set_si(f, 1);
5848 for (i = 0; i < n_out; ++i) {
5849 int offset = has_denom + 1 + n_param + n_before + i;
5851 if (isl_int_is_zero(src[offset]))
5852 continue;
5853 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5854 isl_int_mul(c2, f, src[offset]);
5855 isl_int_gcd(g, c1, c2);
5856 isl_int_divexact(c1, c1, g);
5857 isl_int_divexact(c2, c2, g);
5859 isl_int_mul(f, f, c1);
5860 o_dst = has_denom;
5861 o_src = 1;
5862 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5863 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5864 o_dst += 1 + n_param;
5865 o_src += 1 + n_param;
5866 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5867 o_dst += n_before;
5868 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5869 c2, ma->u.p[i]->v->el + o_src, n_in);
5870 o_dst += n_in;
5871 o_src += n_in;
5872 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5873 o_dst += n_after;
5874 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5875 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5876 o_dst += n_div_ma;
5877 o_src += n_div_ma;
5878 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5879 if (has_denom)
5880 isl_int_mul(dst[0], dst[0], c1);
5883 return isl_stat_ok;
5886 /* Compute the pullback of "aff" by the function represented by "ma".
5887 * In other words, plug in "ma" in "aff". The result is an affine expression
5888 * defined over the domain space of "ma".
5890 * If "aff" is represented by
5892 * (a(p) + b x + c(divs))/d
5894 * and ma is represented by
5896 * x = D(p) + F(y) + G(divs')
5898 * then the result is
5900 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5902 * The divs in the local space of the input are similarly adjusted
5903 * through a call to isl_local_space_preimage_multi_aff.
5905 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5906 __isl_take isl_multi_aff *ma)
5908 isl_aff *res = NULL;
5909 isl_local_space *ls;
5910 isl_size n_div_aff, n_div_ma;
5911 isl_int f, c1, c2, g;
5913 ma = isl_multi_aff_align_divs(ma);
5914 if (!aff || !ma)
5915 goto error;
5917 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5918 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5919 if (n_div_aff < 0 || n_div_ma < 0)
5920 goto error;
5922 ls = isl_aff_get_domain_local_space(aff);
5923 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5924 res = isl_aff_alloc(ls);
5925 if (!res)
5926 goto error;
5928 isl_int_init(f);
5929 isl_int_init(c1);
5930 isl_int_init(c2);
5931 isl_int_init(g);
5933 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5934 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5935 res = isl_aff_free(res);
5937 isl_int_clear(f);
5938 isl_int_clear(c1);
5939 isl_int_clear(c2);
5940 isl_int_clear(g);
5942 isl_aff_free(aff);
5943 isl_multi_aff_free(ma);
5944 res = isl_aff_normalize(res);
5945 return res;
5946 error:
5947 isl_aff_free(aff);
5948 isl_multi_aff_free(ma);
5949 isl_aff_free(res);
5950 return NULL;
5953 /* Compute the pullback of "aff1" by the function represented by "aff2".
5954 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5955 * defined over the domain space of "aff1".
5957 * The domain of "aff1" should match the range of "aff2", which means
5958 * that it should be single-dimensional.
5960 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5961 __isl_take isl_aff *aff2)
5963 isl_multi_aff *ma;
5965 ma = isl_multi_aff_from_aff(aff2);
5966 return isl_aff_pullback_multi_aff(aff1, ma);
5969 /* Compute the pullback of "ma1" by the function represented by "ma2".
5970 * In other words, plug in "ma2" in "ma1".
5972 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5973 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5975 int i;
5976 isl_space *space = NULL;
5978 isl_multi_aff_align_params_bin(&ma1, &ma2);
5979 ma2 = isl_multi_aff_align_divs(ma2);
5980 ma1 = isl_multi_aff_cow(ma1);
5981 if (!ma1 || !ma2)
5982 goto error;
5984 space = isl_space_join(isl_multi_aff_get_space(ma2),
5985 isl_multi_aff_get_space(ma1));
5987 for (i = 0; i < ma1->n; ++i) {
5988 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5989 isl_multi_aff_copy(ma2));
5990 if (!ma1->u.p[i])
5991 goto error;
5994 ma1 = isl_multi_aff_reset_space(ma1, space);
5995 isl_multi_aff_free(ma2);
5996 return ma1;
5997 error:
5998 isl_space_free(space);
5999 isl_multi_aff_free(ma2);
6000 isl_multi_aff_free(ma1);
6001 return NULL;
6004 /* Extend the local space of "dst" to include the divs
6005 * in the local space of "src".
6007 * If "src" does not have any divs or if the local spaces of "dst" and
6008 * "src" are the same, then no extension is required.
6010 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6011 __isl_keep isl_aff *src)
6013 isl_ctx *ctx;
6014 isl_size src_n_div, dst_n_div;
6015 int *exp1 = NULL;
6016 int *exp2 = NULL;
6017 isl_bool equal;
6018 isl_mat *div;
6020 if (!src || !dst)
6021 return isl_aff_free(dst);
6023 ctx = isl_aff_get_ctx(src);
6024 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6025 if (equal < 0)
6026 return isl_aff_free(dst);
6027 if (!equal)
6028 isl_die(ctx, isl_error_invalid,
6029 "spaces don't match", goto error);
6031 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6032 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6033 if (src_n_div == 0)
6034 return dst;
6035 equal = isl_local_space_is_equal(src->ls, dst->ls);
6036 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6037 return isl_aff_free(dst);
6038 if (equal)
6039 return dst;
6041 exp1 = isl_alloc_array(ctx, int, src_n_div);
6042 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6043 if (!exp1 || (dst_n_div && !exp2))
6044 goto error;
6046 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6047 dst = isl_aff_expand_divs(dst, div, exp2);
6048 free(exp1);
6049 free(exp2);
6051 return dst;
6052 error:
6053 free(exp1);
6054 free(exp2);
6055 return isl_aff_free(dst);
6058 /* Adjust the local spaces of the affine expressions in "maff"
6059 * such that they all have the save divs.
6061 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6062 __isl_take isl_multi_aff *maff)
6064 int i;
6066 if (!maff)
6067 return NULL;
6068 if (maff->n == 0)
6069 return maff;
6070 maff = isl_multi_aff_cow(maff);
6071 if (!maff)
6072 return NULL;
6074 for (i = 1; i < maff->n; ++i)
6075 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
6076 for (i = 1; i < maff->n; ++i) {
6077 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
6078 if (!maff->u.p[i])
6079 return isl_multi_aff_free(maff);
6082 return maff;
6085 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6087 aff = isl_aff_cow(aff);
6088 if (!aff)
6089 return NULL;
6091 aff->ls = isl_local_space_lift(aff->ls);
6092 if (!aff->ls)
6093 return isl_aff_free(aff);
6095 return aff;
6098 /* Lift "maff" to a space with extra dimensions such that the result
6099 * has no more existentially quantified variables.
6100 * If "ls" is not NULL, then *ls is assigned the local space that lies
6101 * at the basis of the lifting applied to "maff".
6103 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6104 __isl_give isl_local_space **ls)
6106 int i;
6107 isl_space *space;
6108 isl_size n_div;
6110 if (ls)
6111 *ls = NULL;
6113 if (!maff)
6114 return NULL;
6116 if (maff->n == 0) {
6117 if (ls) {
6118 isl_space *space = isl_multi_aff_get_domain_space(maff);
6119 *ls = isl_local_space_from_space(space);
6120 if (!*ls)
6121 return isl_multi_aff_free(maff);
6123 return maff;
6126 maff = isl_multi_aff_cow(maff);
6127 maff = isl_multi_aff_align_divs(maff);
6128 if (!maff)
6129 return NULL;
6131 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
6132 if (n_div < 0)
6133 return isl_multi_aff_free(maff);
6134 space = isl_multi_aff_get_space(maff);
6135 space = isl_space_lift(isl_space_domain(space), n_div);
6136 space = isl_space_extend_domain_with_range(space,
6137 isl_multi_aff_get_space(maff));
6138 if (!space)
6139 return isl_multi_aff_free(maff);
6140 isl_space_free(maff->space);
6141 maff->space = space;
6143 if (ls) {
6144 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
6145 if (!*ls)
6146 return isl_multi_aff_free(maff);
6149 for (i = 0; i < maff->n; ++i) {
6150 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
6151 if (!maff->u.p[i])
6152 goto error;
6155 return maff;
6156 error:
6157 if (ls)
6158 isl_local_space_free(*ls);
6159 return isl_multi_aff_free(maff);
6162 #undef TYPE
6163 #define TYPE isl_pw_multi_aff
6164 static
6165 #include "check_type_range_templ.c"
6167 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6169 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6170 __isl_keep isl_pw_multi_aff *pma, int pos)
6172 int i;
6173 isl_size n_out;
6174 isl_space *space;
6175 isl_pw_aff *pa;
6177 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6178 return NULL;
6180 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6181 if (n_out < 0)
6182 return NULL;
6184 space = isl_pw_multi_aff_get_space(pma);
6185 space = isl_space_drop_dims(space, isl_dim_out,
6186 pos + 1, n_out - pos - 1);
6187 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6189 pa = isl_pw_aff_alloc_size(space, pma->n);
6190 for (i = 0; i < pma->n; ++i) {
6191 isl_aff *aff;
6192 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6193 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6196 return pa;
6199 /* This is an alternative name for the function above.
6201 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6202 __isl_keep isl_pw_multi_aff *pma, int pos)
6204 return isl_pw_multi_aff_get_at(pma, pos);
6207 /* Return an isl_pw_multi_aff with the given "set" as domain and
6208 * an unnamed zero-dimensional range.
6210 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6211 __isl_take isl_set *set)
6213 isl_multi_aff *ma;
6214 isl_space *space;
6216 space = isl_set_get_space(set);
6217 space = isl_space_from_domain(space);
6218 ma = isl_multi_aff_zero(space);
6219 return isl_pw_multi_aff_alloc(set, ma);
6222 /* Add an isl_pw_multi_aff with the given "set" as domain and
6223 * an unnamed zero-dimensional range to *user.
6225 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6226 void *user)
6228 isl_union_pw_multi_aff **upma = user;
6229 isl_pw_multi_aff *pma;
6231 pma = isl_pw_multi_aff_from_domain(set);
6232 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6234 return isl_stat_ok;
6237 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6238 * an unnamed zero-dimensional range.
6240 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6241 __isl_take isl_union_set *uset)
6243 isl_space *space;
6244 isl_union_pw_multi_aff *upma;
6246 if (!uset)
6247 return NULL;
6249 space = isl_union_set_get_space(uset);
6250 upma = isl_union_pw_multi_aff_empty(space);
6252 if (isl_union_set_foreach_set(uset,
6253 &add_pw_multi_aff_from_domain, &upma) < 0)
6254 goto error;
6256 isl_union_set_free(uset);
6257 return upma;
6258 error:
6259 isl_union_set_free(uset);
6260 isl_union_pw_multi_aff_free(upma);
6261 return NULL;
6264 /* Local data for bin_entry and the callback "fn".
6266 struct isl_union_pw_multi_aff_bin_data {
6267 isl_union_pw_multi_aff *upma2;
6268 isl_union_pw_multi_aff *res;
6269 isl_pw_multi_aff *pma;
6270 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6273 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6274 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6276 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6278 struct isl_union_pw_multi_aff_bin_data *data = user;
6279 isl_stat r;
6281 data->pma = pma;
6282 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6283 data->fn, data);
6284 isl_pw_multi_aff_free(pma);
6286 return r;
6289 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6290 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6291 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6292 * as *entry. The callback should adjust data->res if desired.
6294 static __isl_give isl_union_pw_multi_aff *bin_op(
6295 __isl_take isl_union_pw_multi_aff *upma1,
6296 __isl_take isl_union_pw_multi_aff *upma2,
6297 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6299 isl_space *space;
6300 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6302 space = isl_union_pw_multi_aff_get_space(upma2);
6303 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6304 space = isl_union_pw_multi_aff_get_space(upma1);
6305 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6307 if (!upma1 || !upma2)
6308 goto error;
6310 data.upma2 = upma2;
6311 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6312 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6313 &bin_entry, &data) < 0)
6314 goto error;
6316 isl_union_pw_multi_aff_free(upma1);
6317 isl_union_pw_multi_aff_free(upma2);
6318 return data.res;
6319 error:
6320 isl_union_pw_multi_aff_free(upma1);
6321 isl_union_pw_multi_aff_free(upma2);
6322 isl_union_pw_multi_aff_free(data.res);
6323 return NULL;
6326 /* Given two isl_pw_multi_affs A -> B and C -> D,
6327 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6329 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6330 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6332 isl_space *space;
6334 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6335 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6336 isl_pw_multi_aff_get_space(pma2));
6337 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6338 &isl_multi_aff_range_product);
6341 /* Given two isl_pw_multi_affs A -> B and C -> D,
6342 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6344 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6345 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6347 isl_space *space;
6349 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6350 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6351 isl_pw_multi_aff_get_space(pma2));
6352 space = isl_space_flatten_range(space);
6353 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6354 &isl_multi_aff_flat_range_product);
6357 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6358 * to compute some form of range product and add the result to data->res.
6360 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6361 __isl_give isl_pw_multi_aff *(*range_product)(
6362 __isl_take isl_pw_multi_aff *pma1,
6363 __isl_take isl_pw_multi_aff *pma2),
6364 void *user)
6366 struct isl_union_pw_multi_aff_bin_data *data = user;
6367 isl_bool match;
6368 isl_space *space1, *space2;
6370 space1 = isl_pw_multi_aff_peek_space(data->pma);
6371 space2 = isl_pw_multi_aff_peek_space(pma2);
6372 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6373 space2, isl_dim_in);
6374 if (match < 0 || !match) {
6375 isl_pw_multi_aff_free(pma2);
6376 return match < 0 ? isl_stat_error : isl_stat_ok;
6379 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6381 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6383 return isl_stat_ok;
6386 /* If data->pma and "pma2" have the same domain space, then compute
6387 * their flat range product and add the result to data->res.
6389 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6390 void *user)
6392 return gen_range_product_entry(pma2,
6393 &isl_pw_multi_aff_flat_range_product, user);
6396 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6397 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6399 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6400 __isl_take isl_union_pw_multi_aff *upma1,
6401 __isl_take isl_union_pw_multi_aff *upma2)
6403 return bin_op(upma1, upma2, &flat_range_product_entry);
6406 /* If data->pma and "pma2" have the same domain space, then compute
6407 * their range product and add the result to data->res.
6409 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6410 void *user)
6412 return gen_range_product_entry(pma2,
6413 &isl_pw_multi_aff_range_product, user);
6416 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6417 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6419 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6420 __isl_take isl_union_pw_multi_aff *upma1,
6421 __isl_take isl_union_pw_multi_aff *upma2)
6423 return bin_op(upma1, upma2, &range_product_entry);
6426 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6427 * The parameters are assumed to have been aligned.
6429 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6430 * except that it works on two different isl_pw_* types.
6432 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6433 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6434 __isl_take isl_pw_aff *pa)
6436 int i, j, n;
6437 isl_pw_multi_aff *res = NULL;
6439 if (!pma || !pa)
6440 goto error;
6442 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6443 pa->dim, isl_dim_in))
6444 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6445 "domains don't match", goto error);
6446 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6447 goto error;
6449 n = pma->n * pa->n;
6450 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6452 for (i = 0; i < pma->n; ++i) {
6453 for (j = 0; j < pa->n; ++j) {
6454 isl_set *common;
6455 isl_multi_aff *res_ij;
6456 int empty;
6458 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6459 isl_set_copy(pa->p[j].set));
6460 empty = isl_set_plain_is_empty(common);
6461 if (empty < 0 || empty) {
6462 isl_set_free(common);
6463 if (empty < 0)
6464 goto error;
6465 continue;
6468 res_ij = isl_multi_aff_set_aff(
6469 isl_multi_aff_copy(pma->p[i].maff), pos,
6470 isl_aff_copy(pa->p[j].aff));
6471 res_ij = isl_multi_aff_gist(res_ij,
6472 isl_set_copy(common));
6474 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6478 isl_pw_multi_aff_free(pma);
6479 isl_pw_aff_free(pa);
6480 return res;
6481 error:
6482 isl_pw_multi_aff_free(pma);
6483 isl_pw_aff_free(pa);
6484 return isl_pw_multi_aff_free(res);
6487 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6489 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6490 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6491 __isl_take isl_pw_aff *pa)
6493 isl_bool equal_params;
6495 if (!pma || !pa)
6496 goto error;
6497 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6498 if (equal_params < 0)
6499 goto error;
6500 if (equal_params)
6501 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6502 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6503 isl_pw_aff_check_named_params(pa) < 0)
6504 goto error;
6505 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6506 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6507 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6508 error:
6509 isl_pw_multi_aff_free(pma);
6510 isl_pw_aff_free(pa);
6511 return NULL;
6514 /* Do the parameters of "pa" match those of "space"?
6516 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6517 __isl_keep isl_space *space)
6519 isl_space *pa_space;
6520 isl_bool match;
6522 if (!pa || !space)
6523 return isl_bool_error;
6525 pa_space = isl_pw_aff_get_space(pa);
6527 match = isl_space_has_equal_params(space, pa_space);
6529 isl_space_free(pa_space);
6530 return match;
6533 /* Check that the domain space of "pa" matches "space".
6535 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6536 __isl_keep isl_space *space)
6538 isl_space *pa_space;
6539 isl_bool match;
6541 if (!pa || !space)
6542 return isl_stat_error;
6544 pa_space = isl_pw_aff_get_space(pa);
6546 match = isl_space_has_equal_params(space, pa_space);
6547 if (match < 0)
6548 goto error;
6549 if (!match)
6550 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6551 "parameters don't match", goto error);
6552 match = isl_space_tuple_is_equal(space, isl_dim_in,
6553 pa_space, isl_dim_in);
6554 if (match < 0)
6555 goto error;
6556 if (!match)
6557 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6558 "domains don't match", goto error);
6559 isl_space_free(pa_space);
6560 return isl_stat_ok;
6561 error:
6562 isl_space_free(pa_space);
6563 return isl_stat_error;
6566 #undef BASE
6567 #define BASE pw_aff
6568 #undef DOMBASE
6569 #define DOMBASE set
6571 #include <isl_multi_explicit_domain.c>
6572 #include <isl_multi_pw_aff_explicit_domain.c>
6573 #include <isl_multi_templ.c>
6574 #include <isl_multi_add_constant_templ.c>
6575 #include <isl_multi_apply_set.c>
6576 #include <isl_multi_arith_templ.c>
6577 #include <isl_multi_bind_templ.c>
6578 #include <isl_multi_bind_domain_templ.c>
6579 #include <isl_multi_coalesce.c>
6580 #include <isl_multi_domain_templ.c>
6581 #include <isl_multi_dim_id_templ.c>
6582 #include <isl_multi_dims.c>
6583 #include <isl_multi_from_base_templ.c>
6584 #include <isl_multi_gist.c>
6585 #include <isl_multi_hash.c>
6586 #include <isl_multi_identity_templ.c>
6587 #include <isl_multi_align_set.c>
6588 #include <isl_multi_insert_domain_templ.c>
6589 #include <isl_multi_intersect.c>
6590 #include <isl_multi_min_max_templ.c>
6591 #include <isl_multi_move_dims_templ.c>
6592 #include <isl_multi_nan_templ.c>
6593 #include <isl_multi_param_templ.c>
6594 #include <isl_multi_product_templ.c>
6595 #include <isl_multi_splice_templ.c>
6596 #include <isl_multi_tuple_id_templ.c>
6597 #include <isl_multi_union_add_templ.c>
6598 #include <isl_multi_zero_templ.c>
6599 #include <isl_multi_unbind_params_templ.c>
6601 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6602 * with this explicit domain.
6604 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6605 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6607 isl_set *dom;
6609 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6610 return map;
6612 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6613 map = isl_map_intersect_domain(map, dom);
6615 return map;
6618 /* Are all elements of "mpa" piecewise constants?
6620 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6622 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6625 /* Does "mpa" have a non-trivial explicit domain?
6627 * The explicit domain, if present, is trivial if it represents
6628 * an (obviously) universe set.
6630 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6631 __isl_keep isl_multi_pw_aff *mpa)
6633 if (!mpa)
6634 return isl_bool_error;
6635 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6636 return isl_bool_false;
6637 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6640 #undef BASE
6641 #define BASE set
6643 #include "isl_opt_mpa_templ.c"
6645 /* Compute the minima of the set dimensions as a function of the
6646 * parameters, but independently of the other set dimensions.
6648 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6650 return set_opt_mpa(set, &isl_set_dim_min);
6653 /* Compute the maxima of the set dimensions as a function of the
6654 * parameters, but independently of the other set dimensions.
6656 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6658 return set_opt_mpa(set, &isl_set_dim_max);
6661 #undef BASE
6662 #define BASE map
6664 #include "isl_opt_mpa_templ.c"
6666 /* Compute the minima of the output dimensions as a function of the
6667 * parameters and input dimensions, but independently of
6668 * the other output dimensions.
6670 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6672 return map_opt_mpa(map, &isl_map_dim_min);
6675 /* Compute the maxima of the output dimensions as a function of the
6676 * parameters and input dimensions, but independently of
6677 * the other output dimensions.
6679 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6681 return map_opt_mpa(map, &isl_map_dim_max);
6684 /* Scale the elements of "pma" by the corresponding elements of "mv".
6686 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6687 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6689 int i;
6690 isl_bool equal_params;
6692 pma = isl_pw_multi_aff_cow(pma);
6693 if (!pma || !mv)
6694 goto error;
6695 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6696 mv->space, isl_dim_set))
6697 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6698 "spaces don't match", goto error);
6699 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6700 if (equal_params < 0)
6701 goto error;
6702 if (!equal_params) {
6703 pma = isl_pw_multi_aff_align_params(pma,
6704 isl_multi_val_get_space(mv));
6705 mv = isl_multi_val_align_params(mv,
6706 isl_pw_multi_aff_get_space(pma));
6707 if (!pma || !mv)
6708 goto error;
6711 for (i = 0; i < pma->n; ++i) {
6712 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6713 isl_multi_val_copy(mv));
6714 if (!pma->p[i].maff)
6715 goto error;
6718 isl_multi_val_free(mv);
6719 return pma;
6720 error:
6721 isl_multi_val_free(mv);
6722 isl_pw_multi_aff_free(pma);
6723 return NULL;
6726 /* This function is called for each entry of an isl_union_pw_multi_aff.
6727 * If the space of the entry matches that of data->mv,
6728 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6729 * Otherwise, return an empty isl_pw_multi_aff.
6731 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6732 __isl_take isl_pw_multi_aff *pma, void *user)
6734 isl_multi_val *mv = user;
6736 if (!pma)
6737 return NULL;
6738 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6739 mv->space, isl_dim_set)) {
6740 isl_space *space = isl_pw_multi_aff_get_space(pma);
6741 isl_pw_multi_aff_free(pma);
6742 return isl_pw_multi_aff_empty(space);
6745 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6748 /* Scale the elements of "upma" by the corresponding elements of "mv",
6749 * for those entries that match the space of "mv".
6751 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6752 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6754 struct isl_union_pw_multi_aff_transform_control control = {
6755 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6756 .fn_user = mv,
6759 upma = isl_union_pw_multi_aff_align_params(upma,
6760 isl_multi_val_get_space(mv));
6761 mv = isl_multi_val_align_params(mv,
6762 isl_union_pw_multi_aff_get_space(upma));
6763 if (!upma || !mv)
6764 goto error;
6766 return isl_union_pw_multi_aff_transform(upma, &control);
6768 isl_multi_val_free(mv);
6769 return upma;
6770 error:
6771 isl_multi_val_free(mv);
6772 isl_union_pw_multi_aff_free(upma);
6773 return NULL;
6776 /* Construct and return a piecewise multi affine expression
6777 * in the given space with value zero in each of the output dimensions and
6778 * a universe domain.
6780 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6782 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6785 /* Construct and return a piecewise multi affine expression
6786 * that is equal to the given piecewise affine expression.
6788 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6789 __isl_take isl_pw_aff *pa)
6791 int i;
6792 isl_space *space;
6793 isl_pw_multi_aff *pma;
6795 if (!pa)
6796 return NULL;
6798 space = isl_pw_aff_get_space(pa);
6799 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6801 for (i = 0; i < pa->n; ++i) {
6802 isl_set *set;
6803 isl_multi_aff *ma;
6805 set = isl_set_copy(pa->p[i].set);
6806 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6807 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6810 isl_pw_aff_free(pa);
6811 return pma;
6814 /* Construct and return a piecewise multi affine expression
6815 * that is equal to the given multi piecewise affine expression
6816 * on the shared domain of the piecewise affine expressions,
6817 * in the special case of a 0D multi piecewise affine expression.
6819 * Create a piecewise multi affine expression with the explicit domain of
6820 * the 0D multi piecewise affine expression as domain.
6822 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6823 __isl_take isl_multi_pw_aff *mpa)
6825 isl_space *space;
6826 isl_set *dom;
6827 isl_multi_aff *ma;
6829 space = isl_multi_pw_aff_get_space(mpa);
6830 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6831 isl_multi_pw_aff_free(mpa);
6833 ma = isl_multi_aff_zero(space);
6834 return isl_pw_multi_aff_alloc(dom, ma);
6837 /* Construct and return a piecewise multi affine expression
6838 * that is equal to the given multi piecewise affine expression
6839 * on the shared domain of the piecewise affine expressions.
6841 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6842 __isl_take isl_multi_pw_aff *mpa)
6844 int i;
6845 isl_space *space;
6846 isl_pw_aff *pa;
6847 isl_pw_multi_aff *pma;
6849 if (!mpa)
6850 return NULL;
6852 if (mpa->n == 0)
6853 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6855 space = isl_multi_pw_aff_get_space(mpa);
6856 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6857 pma = isl_pw_multi_aff_from_pw_aff(pa);
6859 for (i = 1; i < mpa->n; ++i) {
6860 isl_pw_multi_aff *pma_i;
6862 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6863 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6864 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6867 pma = isl_pw_multi_aff_reset_space(pma, space);
6869 isl_multi_pw_aff_free(mpa);
6870 return pma;
6873 /* Convenience function that constructs an isl_multi_pw_aff
6874 * directly from an isl_aff.
6876 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6878 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6881 /* Construct and return a multi piecewise affine expression
6882 * that is equal to the given multi affine expression.
6884 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6885 __isl_take isl_multi_aff *ma)
6887 int i;
6888 isl_size n;
6889 isl_multi_pw_aff *mpa;
6891 n = isl_multi_aff_dim(ma, isl_dim_out);
6892 if (n < 0)
6893 ma = isl_multi_aff_free(ma);
6894 if (!ma)
6895 return NULL;
6897 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6899 for (i = 0; i < n; ++i) {
6900 isl_pw_aff *pa;
6902 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6903 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6906 isl_multi_aff_free(ma);
6907 return mpa;
6910 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6911 * but is considered as a function on an isl_multi_aff when exported.
6913 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
6914 __isl_take isl_multi_aff *ma)
6916 return isl_multi_pw_aff_from_multi_aff(ma);
6919 /* Construct and return a multi piecewise affine expression
6920 * that is equal to the given piecewise multi affine expression.
6922 * If the resulting multi piecewise affine expression has
6923 * an explicit domain, then assign it the domain of the input.
6924 * In other cases, the domain is stored in the individual elements.
6926 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6927 __isl_take isl_pw_multi_aff *pma)
6929 int i;
6930 isl_size n;
6931 isl_space *space;
6932 isl_multi_pw_aff *mpa;
6934 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6935 if (n < 0)
6936 pma = isl_pw_multi_aff_free(pma);
6937 space = isl_pw_multi_aff_get_space(pma);
6938 mpa = isl_multi_pw_aff_alloc(space);
6940 for (i = 0; i < n; ++i) {
6941 isl_pw_aff *pa;
6943 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6944 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6946 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6947 isl_set *dom;
6949 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6950 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6953 isl_pw_multi_aff_free(pma);
6954 return mpa;
6957 /* This function performs the same operation as
6958 * isl_multi_pw_aff_from_pw_multi_aff,
6959 * but is considered as a function on an isl_pw_multi_aff when exported.
6961 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
6962 __isl_take isl_pw_multi_aff *pma)
6964 return isl_multi_pw_aff_from_pw_multi_aff(pma);
6967 /* Do "pa1" and "pa2" represent the same function?
6969 * We first check if they are obviously equal.
6970 * If not, we convert them to maps and check if those are equal.
6972 * If "pa1" or "pa2" contain any NaNs, then they are considered
6973 * not to be the same. A NaN is not equal to anything, not even
6974 * to another NaN.
6976 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6977 __isl_keep isl_pw_aff *pa2)
6979 isl_bool equal;
6980 isl_bool has_nan;
6981 isl_map *map1, *map2;
6983 if (!pa1 || !pa2)
6984 return isl_bool_error;
6986 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6987 if (equal < 0 || equal)
6988 return equal;
6989 has_nan = either_involves_nan(pa1, pa2);
6990 if (has_nan < 0)
6991 return isl_bool_error;
6992 if (has_nan)
6993 return isl_bool_false;
6995 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6996 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6997 equal = isl_map_is_equal(map1, map2);
6998 isl_map_free(map1);
6999 isl_map_free(map2);
7001 return equal;
7004 /* Do "mpa1" and "mpa2" represent the same function?
7006 * Note that we cannot convert the entire isl_multi_pw_aff
7007 * to a map because the domains of the piecewise affine expressions
7008 * may not be the same.
7010 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7011 __isl_keep isl_multi_pw_aff *mpa2)
7013 int i;
7014 isl_bool equal, equal_params;
7016 if (!mpa1 || !mpa2)
7017 return isl_bool_error;
7019 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7020 if (equal_params < 0)
7021 return isl_bool_error;
7022 if (!equal_params) {
7023 if (!isl_space_has_named_params(mpa1->space))
7024 return isl_bool_false;
7025 if (!isl_space_has_named_params(mpa2->space))
7026 return isl_bool_false;
7027 mpa1 = isl_multi_pw_aff_copy(mpa1);
7028 mpa2 = isl_multi_pw_aff_copy(mpa2);
7029 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7030 isl_multi_pw_aff_get_space(mpa2));
7031 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7032 isl_multi_pw_aff_get_space(mpa1));
7033 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7034 isl_multi_pw_aff_free(mpa1);
7035 isl_multi_pw_aff_free(mpa2);
7036 return equal;
7039 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7040 if (equal < 0 || !equal)
7041 return equal;
7043 for (i = 0; i < mpa1->n; ++i) {
7044 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7045 if (equal < 0 || !equal)
7046 return equal;
7049 return isl_bool_true;
7052 /* Do "pma1" and "pma2" represent the same function?
7054 * First check if they are obviously equal.
7055 * If not, then convert them to maps and check if those are equal.
7057 * If "pa1" or "pa2" contain any NaNs, then they are considered
7058 * not to be the same. A NaN is not equal to anything, not even
7059 * to another NaN.
7061 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7062 __isl_keep isl_pw_multi_aff *pma2)
7064 isl_bool equal;
7065 isl_bool has_nan;
7066 isl_map *map1, *map2;
7068 if (!pma1 || !pma2)
7069 return isl_bool_error;
7071 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7072 if (equal < 0 || equal)
7073 return equal;
7074 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7075 if (has_nan >= 0 && !has_nan)
7076 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7077 if (has_nan < 0 || has_nan)
7078 return isl_bool_not(has_nan);
7080 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7081 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7082 equal = isl_map_is_equal(map1, map2);
7083 isl_map_free(map1);
7084 isl_map_free(map2);
7086 return equal;
7089 /* Compute the pullback of "mpa" by the function represented by "ma".
7090 * In other words, plug in "ma" in "mpa".
7092 * The parameters of "mpa" and "ma" are assumed to have been aligned.
7094 * If "mpa" has an explicit domain, then it is this domain
7095 * that needs to undergo a pullback, i.e., a preimage.
7097 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
7098 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7100 int i;
7101 isl_space *space = NULL;
7103 mpa = isl_multi_pw_aff_cow(mpa);
7104 if (!mpa || !ma)
7105 goto error;
7107 space = isl_space_join(isl_multi_aff_get_space(ma),
7108 isl_multi_pw_aff_get_space(mpa));
7109 if (!space)
7110 goto error;
7112 for (i = 0; i < mpa->n; ++i) {
7113 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
7114 isl_multi_aff_copy(ma));
7115 if (!mpa->u.p[i])
7116 goto error;
7118 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7119 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
7120 isl_multi_aff_copy(ma));
7121 if (!mpa->u.dom)
7122 goto error;
7125 isl_multi_aff_free(ma);
7126 isl_space_free(mpa->space);
7127 mpa->space = space;
7128 return mpa;
7129 error:
7130 isl_space_free(space);
7131 isl_multi_pw_aff_free(mpa);
7132 isl_multi_aff_free(ma);
7133 return NULL;
7136 /* Compute the pullback of "mpa" by the function represented by "ma".
7137 * In other words, plug in "ma" in "mpa".
7139 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
7140 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7142 isl_bool equal_params;
7144 if (!mpa || !ma)
7145 goto error;
7146 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
7147 if (equal_params < 0)
7148 goto error;
7149 if (equal_params)
7150 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7151 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
7152 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
7153 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7154 error:
7155 isl_multi_pw_aff_free(mpa);
7156 isl_multi_aff_free(ma);
7157 return NULL;
7160 /* Compute the pullback of "mpa" by the function represented by "pma".
7161 * In other words, plug in "pma" in "mpa".
7163 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7165 * If "mpa" has an explicit domain, then it is this domain
7166 * that needs to undergo a pullback, i.e., a preimage.
7168 static __isl_give isl_multi_pw_aff *
7169 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7170 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7172 int i;
7173 isl_space *space = NULL;
7175 mpa = isl_multi_pw_aff_cow(mpa);
7176 if (!mpa || !pma)
7177 goto error;
7179 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
7180 isl_multi_pw_aff_get_space(mpa));
7182 for (i = 0; i < mpa->n; ++i) {
7183 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7184 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
7185 if (!mpa->u.p[i])
7186 goto error;
7188 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7189 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
7190 isl_pw_multi_aff_copy(pma));
7191 if (!mpa->u.dom)
7192 goto error;
7195 isl_pw_multi_aff_free(pma);
7196 isl_space_free(mpa->space);
7197 mpa->space = space;
7198 return mpa;
7199 error:
7200 isl_space_free(space);
7201 isl_multi_pw_aff_free(mpa);
7202 isl_pw_multi_aff_free(pma);
7203 return NULL;
7206 /* Compute the pullback of "mpa" by the function represented by "pma".
7207 * In other words, plug in "pma" in "mpa".
7209 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
7210 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7212 isl_bool equal_params;
7214 if (!mpa || !pma)
7215 goto error;
7216 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
7217 if (equal_params < 0)
7218 goto error;
7219 if (equal_params)
7220 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7221 mpa = isl_multi_pw_aff_align_params(mpa,
7222 isl_pw_multi_aff_get_space(pma));
7223 pma = isl_pw_multi_aff_align_params(pma,
7224 isl_multi_pw_aff_get_space(mpa));
7225 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7226 error:
7227 isl_multi_pw_aff_free(mpa);
7228 isl_pw_multi_aff_free(pma);
7229 return NULL;
7232 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7233 * with the domain of "aff". The domain of the result is the same
7234 * as that of "mpa".
7235 * "mpa" and "aff" are assumed to have been aligned.
7237 * We first extract the parametric constant from "aff", defined
7238 * over the correct domain.
7239 * Then we add the appropriate combinations of the members of "mpa".
7240 * Finally, we add the integer divisions through recursive calls.
7242 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7243 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7245 int i;
7246 isl_size n_in, n_div, n_mpa_in;
7247 isl_space *space;
7248 isl_val *v;
7249 isl_pw_aff *pa;
7250 isl_aff *tmp;
7252 n_in = isl_aff_dim(aff, isl_dim_in);
7253 n_div = isl_aff_dim(aff, isl_dim_div);
7254 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7255 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7256 goto error;
7258 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7259 tmp = isl_aff_copy(aff);
7260 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7261 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7262 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7263 tmp = isl_aff_reset_domain_space(tmp, space);
7264 pa = isl_pw_aff_from_aff(tmp);
7266 for (i = 0; i < n_in; ++i) {
7267 isl_pw_aff *pa_i;
7269 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7270 continue;
7271 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7272 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7273 pa_i = isl_pw_aff_scale_val(pa_i, v);
7274 pa = isl_pw_aff_add(pa, pa_i);
7277 for (i = 0; i < n_div; ++i) {
7278 isl_aff *div;
7279 isl_pw_aff *pa_i;
7281 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7282 continue;
7283 div = isl_aff_get_div(aff, i);
7284 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7285 isl_multi_pw_aff_copy(mpa), div);
7286 pa_i = isl_pw_aff_floor(pa_i);
7287 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7288 pa_i = isl_pw_aff_scale_val(pa_i, v);
7289 pa = isl_pw_aff_add(pa, pa_i);
7292 isl_multi_pw_aff_free(mpa);
7293 isl_aff_free(aff);
7295 return pa;
7296 error:
7297 isl_multi_pw_aff_free(mpa);
7298 isl_aff_free(aff);
7299 return NULL;
7302 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7303 * with the domain of "aff". The domain of the result is the same
7304 * as that of "mpa".
7306 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7307 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7309 isl_bool equal_params;
7311 if (!aff || !mpa)
7312 goto error;
7313 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7314 if (equal_params < 0)
7315 goto error;
7316 if (equal_params)
7317 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7319 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7320 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7322 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7323 error:
7324 isl_aff_free(aff);
7325 isl_multi_pw_aff_free(mpa);
7326 return NULL;
7329 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7330 * with the domain of "pa". The domain of the result is the same
7331 * as that of "mpa".
7332 * "mpa" and "pa" are assumed to have been aligned.
7334 * We consider each piece in turn. Note that the domains of the
7335 * pieces are assumed to be disjoint and they remain disjoint
7336 * after taking the preimage (over the same function).
7338 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7339 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7341 isl_space *space;
7342 isl_pw_aff *res;
7343 int i;
7345 if (!mpa || !pa)
7346 goto error;
7348 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7349 isl_pw_aff_get_space(pa));
7350 res = isl_pw_aff_empty(space);
7352 for (i = 0; i < pa->n; ++i) {
7353 isl_pw_aff *pa_i;
7354 isl_set *domain;
7356 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7357 isl_multi_pw_aff_copy(mpa),
7358 isl_aff_copy(pa->p[i].aff));
7359 domain = isl_set_copy(pa->p[i].set);
7360 domain = isl_set_preimage_multi_pw_aff(domain,
7361 isl_multi_pw_aff_copy(mpa));
7362 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7363 res = isl_pw_aff_add_disjoint(res, pa_i);
7366 isl_pw_aff_free(pa);
7367 isl_multi_pw_aff_free(mpa);
7368 return res;
7369 error:
7370 isl_pw_aff_free(pa);
7371 isl_multi_pw_aff_free(mpa);
7372 return NULL;
7375 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7376 * with the domain of "pa". The domain of the result is the same
7377 * as that of "mpa".
7379 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7380 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7382 isl_bool equal_params;
7384 if (!pa || !mpa)
7385 goto error;
7386 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7387 if (equal_params < 0)
7388 goto error;
7389 if (equal_params)
7390 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7392 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7393 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7395 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7396 error:
7397 isl_pw_aff_free(pa);
7398 isl_multi_pw_aff_free(mpa);
7399 return NULL;
7402 /* Compute the pullback of "pa" by the function represented by "mpa".
7403 * In other words, plug in "mpa" in "pa".
7404 * "pa" and "mpa" are assumed to have been aligned.
7406 * The pullback is computed by applying "pa" to "mpa".
7408 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7409 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7411 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7414 /* Compute the pullback of "pa" by the function represented by "mpa".
7415 * In other words, plug in "mpa" in "pa".
7417 * The pullback is computed by applying "pa" to "mpa".
7419 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7420 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7422 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7425 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7426 * In other words, plug in "mpa2" in "mpa1".
7428 * We pullback each member of "mpa1" in turn.
7430 * If "mpa1" has an explicit domain, then it is this domain
7431 * that needs to undergo a pullback instead, i.e., a preimage.
7433 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7434 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7436 int i;
7437 isl_space *space = NULL;
7439 isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
7440 mpa1 = isl_multi_pw_aff_cow(mpa1);
7441 if (!mpa1 || !mpa2)
7442 goto error;
7444 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7445 isl_multi_pw_aff_get_space(mpa1));
7447 for (i = 0; i < mpa1->n; ++i) {
7448 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7449 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7450 if (!mpa1->u.p[i])
7451 goto error;
7454 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7455 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7456 isl_multi_pw_aff_copy(mpa2));
7457 if (!mpa1->u.dom)
7458 goto error;
7460 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7462 isl_multi_pw_aff_free(mpa2);
7463 return mpa1;
7464 error:
7465 isl_space_free(space);
7466 isl_multi_pw_aff_free(mpa1);
7467 isl_multi_pw_aff_free(mpa2);
7468 return NULL;
7471 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7472 * of "mpa1" and "mpa2" live in the same space, construct map space
7473 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7474 * with this map space as extract argument.
7476 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7477 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7478 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7479 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7481 int match;
7482 isl_space *space1, *space2;
7483 isl_map *res;
7485 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7486 isl_multi_pw_aff_get_space(mpa2));
7487 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7488 isl_multi_pw_aff_get_space(mpa1));
7489 if (!mpa1 || !mpa2)
7490 goto error;
7491 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7492 mpa2->space, isl_dim_out);
7493 if (match < 0)
7494 goto error;
7495 if (!match)
7496 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7497 "range spaces don't match", goto error);
7498 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7499 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7500 space1 = isl_space_map_from_domain_and_range(space1, space2);
7502 res = order(mpa1, mpa2, space1);
7503 isl_multi_pw_aff_free(mpa1);
7504 isl_multi_pw_aff_free(mpa2);
7505 return res;
7506 error:
7507 isl_multi_pw_aff_free(mpa1);
7508 isl_multi_pw_aff_free(mpa2);
7509 return NULL;
7512 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7513 * where the function values are equal. "space" is the space of the result.
7514 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7516 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7517 * in the sequences are equal.
7519 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7520 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7521 __isl_take isl_space *space)
7523 int i;
7524 isl_size n;
7525 isl_map *res;
7527 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7528 if (n < 0)
7529 space = isl_space_free(space);
7530 res = isl_map_universe(space);
7532 for (i = 0; i < n; ++i) {
7533 isl_pw_aff *pa1, *pa2;
7534 isl_map *map;
7536 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7537 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7538 map = isl_pw_aff_eq_map(pa1, pa2);
7539 res = isl_map_intersect(res, map);
7542 return res;
7545 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7546 * where the function values are equal.
7548 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7549 __isl_take isl_multi_pw_aff *mpa2)
7551 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7552 &isl_multi_pw_aff_eq_map_on_space);
7555 /* Intersect "map" with the result of applying "order"
7556 * on two copies of "mpa".
7558 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7559 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7560 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7561 __isl_take isl_multi_pw_aff *mpa2))
7563 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7566 /* Return the subset of "map" where the domain and the range
7567 * have equal "mpa" values.
7569 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7570 __isl_take isl_multi_pw_aff *mpa)
7572 return isl_map_order_at_multi_pw_aff(map, mpa,
7573 &isl_multi_pw_aff_eq_map);
7576 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7577 * where the function values of "mpa1" lexicographically satisfies
7578 * "strict_base"/"base" compared to that of "mpa2".
7579 * "space" is the space of the result.
7580 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7582 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7583 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7584 * when compared to the i-th element of "mpa2" while all previous elements are
7585 * pairwise equal.
7586 * In particular, if i corresponds to the final elements
7587 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7588 * for other values of i.
7589 * If "base" is a strict order, then "base" and "strict_base" are the same.
7591 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7592 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7593 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7594 __isl_take isl_pw_aff *pa2),
7595 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7596 __isl_take isl_pw_aff *pa2),
7597 __isl_take isl_space *space)
7599 int i;
7600 isl_size n;
7601 isl_map *res, *rest;
7603 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7604 if (n < 0)
7605 space = isl_space_free(space);
7606 res = isl_map_empty(isl_space_copy(space));
7607 rest = isl_map_universe(space);
7609 for (i = 0; i < n; ++i) {
7610 int last;
7611 isl_pw_aff *pa1, *pa2;
7612 isl_map *map;
7614 last = i == n - 1;
7616 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7617 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7618 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7619 map = isl_map_intersect(map, isl_map_copy(rest));
7620 res = isl_map_union(res, map);
7622 if (last)
7623 continue;
7625 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7626 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7627 map = isl_pw_aff_eq_map(pa1, pa2);
7628 rest = isl_map_intersect(rest, map);
7631 isl_map_free(rest);
7632 return res;
7635 #undef ORDER
7636 #define ORDER le
7637 #undef STRICT_ORDER
7638 #define STRICT_ORDER lt
7639 #include "isl_aff_lex_templ.c"
7641 #undef ORDER
7642 #define ORDER lt
7643 #undef STRICT_ORDER
7644 #define STRICT_ORDER lt
7645 #include "isl_aff_lex_templ.c"
7647 #undef ORDER
7648 #define ORDER ge
7649 #undef STRICT_ORDER
7650 #define STRICT_ORDER gt
7651 #include "isl_aff_lex_templ.c"
7653 #undef ORDER
7654 #define ORDER gt
7655 #undef STRICT_ORDER
7656 #define STRICT_ORDER gt
7657 #include "isl_aff_lex_templ.c"
7659 /* Compare two isl_affs.
7661 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7662 * than "aff2" and 0 if they are equal.
7664 * The order is fairly arbitrary. We do consider expressions that only involve
7665 * earlier dimensions as "smaller".
7667 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7669 int cmp;
7670 int last1, last2;
7672 if (aff1 == aff2)
7673 return 0;
7675 if (!aff1)
7676 return -1;
7677 if (!aff2)
7678 return 1;
7680 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7681 if (cmp != 0)
7682 return cmp;
7684 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7685 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7686 if (last1 != last2)
7687 return last1 - last2;
7689 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7692 /* Compare two isl_pw_affs.
7694 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7695 * than "pa2" and 0 if they are equal.
7697 * The order is fairly arbitrary. We do consider expressions that only involve
7698 * earlier dimensions as "smaller".
7700 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7701 __isl_keep isl_pw_aff *pa2)
7703 int i;
7704 int cmp;
7706 if (pa1 == pa2)
7707 return 0;
7709 if (!pa1)
7710 return -1;
7711 if (!pa2)
7712 return 1;
7714 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7715 if (cmp != 0)
7716 return cmp;
7718 if (pa1->n != pa2->n)
7719 return pa1->n - pa2->n;
7721 for (i = 0; i < pa1->n; ++i) {
7722 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7723 if (cmp != 0)
7724 return cmp;
7725 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7726 if (cmp != 0)
7727 return cmp;
7730 return 0;
7733 /* Return a piecewise affine expression that is equal to "v" on "domain".
7735 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7736 __isl_take isl_val *v)
7738 isl_space *space;
7739 isl_local_space *ls;
7740 isl_aff *aff;
7742 space = isl_set_get_space(domain);
7743 ls = isl_local_space_from_space(space);
7744 aff = isl_aff_val_on_domain(ls, v);
7746 return isl_pw_aff_alloc(domain, aff);
7749 /* Return a piecewise affine expression that is equal to the parameter
7750 * with identifier "id" on "domain".
7752 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7753 __isl_take isl_set *domain, __isl_take isl_id *id)
7755 isl_space *space;
7756 isl_aff *aff;
7758 space = isl_set_get_space(domain);
7759 space = isl_space_add_param_id(space, isl_id_copy(id));
7760 domain = isl_set_align_params(domain, isl_space_copy(space));
7761 aff = isl_aff_param_on_domain_space_id(space, id);
7763 return isl_pw_aff_alloc(domain, aff);
7766 /* Return a multi affine expression that is equal to "mv" on domain
7767 * space "space".
7769 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7770 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7772 int i;
7773 isl_size n;
7774 isl_space *space2;
7775 isl_local_space *ls;
7776 isl_multi_aff *ma;
7778 n = isl_multi_val_dim(mv, isl_dim_set);
7779 if (!space || n < 0)
7780 goto error;
7782 space2 = isl_multi_val_get_space(mv);
7783 space2 = isl_space_align_params(space2, isl_space_copy(space));
7784 space = isl_space_align_params(space, isl_space_copy(space2));
7785 space = isl_space_map_from_domain_and_range(space, space2);
7786 ma = isl_multi_aff_alloc(isl_space_copy(space));
7787 ls = isl_local_space_from_space(isl_space_domain(space));
7788 for (i = 0; i < n; ++i) {
7789 isl_val *v;
7790 isl_aff *aff;
7792 v = isl_multi_val_get_val(mv, i);
7793 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7794 ma = isl_multi_aff_set_aff(ma, i, aff);
7796 isl_local_space_free(ls);
7798 isl_multi_val_free(mv);
7799 return ma;
7800 error:
7801 isl_space_free(space);
7802 isl_multi_val_free(mv);
7803 return NULL;
7806 /* This is an alternative name for the function above.
7808 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7809 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7811 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7814 /* This function performs the same operation as
7815 * isl_multi_aff_multi_val_on_domain_space,
7816 * but is considered as a function on an isl_space when exported.
7818 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7819 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7821 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7824 /* Return a piecewise multi-affine expression
7825 * that is equal to "mv" on "domain".
7827 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7828 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7830 isl_space *space;
7831 isl_multi_aff *ma;
7833 space = isl_set_get_space(domain);
7834 ma = isl_multi_aff_multi_val_on_space(space, mv);
7836 return isl_pw_multi_aff_alloc(domain, ma);
7839 /* This function performs the same operation as
7840 * isl_pw_multi_aff_multi_val_on_domain,
7841 * but is considered as a function on an isl_set when exported.
7843 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7844 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7846 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7849 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7850 * mv is the value that should be attained on each domain set
7851 * res collects the results
7853 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7854 isl_multi_val *mv;
7855 isl_union_pw_multi_aff *res;
7858 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7859 * and add it to data->res.
7861 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7862 void *user)
7864 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7865 isl_pw_multi_aff *pma;
7866 isl_multi_val *mv;
7868 mv = isl_multi_val_copy(data->mv);
7869 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7870 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7872 return data->res ? isl_stat_ok : isl_stat_error;
7875 /* Return a union piecewise multi-affine expression
7876 * that is equal to "mv" on "domain".
7878 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7879 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7881 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7882 isl_space *space;
7884 space = isl_union_set_get_space(domain);
7885 data.res = isl_union_pw_multi_aff_empty(space);
7886 data.mv = mv;
7887 if (isl_union_set_foreach_set(domain,
7888 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7889 data.res = isl_union_pw_multi_aff_free(data.res);
7890 isl_union_set_free(domain);
7891 isl_multi_val_free(mv);
7892 return data.res;
7895 /* Compute the pullback of data->pma by the function represented by "pma2",
7896 * provided the spaces match, and add the results to data->res.
7898 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7900 struct isl_union_pw_multi_aff_bin_data *data = user;
7902 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7903 pma2->dim, isl_dim_out)) {
7904 isl_pw_multi_aff_free(pma2);
7905 return isl_stat_ok;
7908 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7909 isl_pw_multi_aff_copy(data->pma), pma2);
7911 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7912 if (!data->res)
7913 return isl_stat_error;
7915 return isl_stat_ok;
7918 /* Compute the pullback of "upma1" by the function represented by "upma2".
7920 __isl_give isl_union_pw_multi_aff *
7921 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7922 __isl_take isl_union_pw_multi_aff *upma1,
7923 __isl_take isl_union_pw_multi_aff *upma2)
7925 return bin_op(upma1, upma2, &pullback_entry);
7928 /* Apply "upma2" to "upma1".
7930 * That is, compute the pullback of "upma2" by "upma1".
7932 __isl_give isl_union_pw_multi_aff *
7933 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7934 __isl_take isl_union_pw_multi_aff *upma1,
7935 __isl_take isl_union_pw_multi_aff *upma2)
7937 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7940 #undef TYPE
7941 #define TYPE isl_pw_multi_aff
7942 static
7943 #include "isl_copy_tuple_id_templ.c"
7945 /* Given a function "pma1" of the form A[B -> C] -> D and
7946 * a function "pma2" of the form E -> B,
7947 * replace the domain of the wrapped relation inside the domain of "pma1"
7948 * by the preimage with respect to "pma2".
7949 * In other words, plug in "pma2" in this nested domain.
7950 * The result is of the form A[E -> C] -> D.
7952 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7953 * plug that into "pma1".
7955 __isl_give isl_pw_multi_aff *
7956 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7957 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
7959 isl_space *pma1_space, *pma2_space;
7960 isl_space *space;
7961 isl_pw_multi_aff *id;
7963 pma1_space = isl_pw_multi_aff_peek_space(pma1);
7964 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7966 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
7967 goto error;
7968 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
7969 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
7970 goto error;
7972 space = isl_space_domain(isl_space_copy(pma1_space));
7973 space = isl_space_range(isl_space_unwrap(space));
7974 id = isl_pw_multi_aff_identity_on_domain_space(space);
7975 pma2 = isl_pw_multi_aff_product(pma2, id);
7977 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
7978 pma1_space, isl_dim_in);
7979 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
7980 pma1_space, isl_dim_in);
7982 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
7983 error:
7984 isl_pw_multi_aff_free(pma1);
7985 isl_pw_multi_aff_free(pma2);
7986 return NULL;
7989 /* If data->pma and "pma2" are such that
7990 * data->pma is of the form A[B -> C] -> D and
7991 * "pma2" is of the form E -> B,
7992 * then replace the domain of the wrapped relation
7993 * inside the domain of data->pma by the preimage with respect to "pma2" and
7994 * add the result to data->res.
7996 static isl_stat preimage_domain_wrapped_domain_entry(
7997 __isl_take isl_pw_multi_aff *pma2, void *user)
7999 struct isl_union_pw_multi_aff_bin_data *data = user;
8000 isl_space *pma1_space, *pma2_space;
8001 isl_bool match;
8003 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
8004 pma2_space = isl_pw_multi_aff_peek_space(pma2);
8006 match = isl_space_domain_is_wrapping(pma1_space);
8007 if (match >= 0 && match)
8008 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
8009 isl_dim_in, pma2_space, isl_dim_out);
8010 if (match < 0 || !match) {
8011 isl_pw_multi_aff_free(pma2);
8012 return match < 0 ? isl_stat_error : isl_stat_ok;
8015 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8016 isl_pw_multi_aff_copy(data->pma), pma2);
8018 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
8020 return isl_stat_non_null(data->res);
8023 /* For each pair of functions A[B -> C] -> D in "upma1" and
8024 * E -> B in "upma2",
8025 * replace the domain of the wrapped relation inside the domain of the first
8026 * by the preimage with respect to the second and collect the results.
8027 * In other words, plug in the second function in this nested domain.
8028 * The results are of the form A[E -> C] -> D.
8030 __isl_give isl_union_pw_multi_aff *
8031 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8032 __isl_take isl_union_pw_multi_aff *upma1,
8033 __isl_take isl_union_pw_multi_aff *upma2)
8035 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
8038 /* Check that the domain space of "upa" matches "space".
8040 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8041 * can in principle never fail since the space "space" is that
8042 * of the isl_multi_union_pw_aff and is a set space such that
8043 * there is no domain space to match.
8045 * We check the parameters and double-check that "space" is
8046 * indeed that of a set.
8048 static isl_stat isl_union_pw_aff_check_match_domain_space(
8049 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8051 isl_space *upa_space;
8052 isl_bool match;
8054 if (!upa || !space)
8055 return isl_stat_error;
8057 match = isl_space_is_set(space);
8058 if (match < 0)
8059 return isl_stat_error;
8060 if (!match)
8061 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8062 "expecting set space", return isl_stat_error);
8064 upa_space = isl_union_pw_aff_get_space(upa);
8065 match = isl_space_has_equal_params(space, upa_space);
8066 if (match < 0)
8067 goto error;
8068 if (!match)
8069 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8070 "parameters don't match", goto error);
8072 isl_space_free(upa_space);
8073 return isl_stat_ok;
8074 error:
8075 isl_space_free(upa_space);
8076 return isl_stat_error;
8079 /* Do the parameters of "upa" match those of "space"?
8081 static isl_bool isl_union_pw_aff_matching_params(
8082 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8084 isl_space *upa_space;
8085 isl_bool match;
8087 if (!upa || !space)
8088 return isl_bool_error;
8090 upa_space = isl_union_pw_aff_get_space(upa);
8092 match = isl_space_has_equal_params(space, upa_space);
8094 isl_space_free(upa_space);
8095 return match;
8098 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8099 * space represents the new parameters.
8100 * res collects the results.
8102 struct isl_union_pw_aff_reset_params_data {
8103 isl_space *space;
8104 isl_union_pw_aff *res;
8107 /* Replace the parameters of "pa" by data->space and
8108 * add the result to data->res.
8110 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8112 struct isl_union_pw_aff_reset_params_data *data = user;
8113 isl_space *space;
8115 space = isl_pw_aff_get_space(pa);
8116 space = isl_space_replace_params(space, data->space);
8117 pa = isl_pw_aff_reset_space(pa, space);
8118 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8120 return data->res ? isl_stat_ok : isl_stat_error;
8123 /* Replace the domain space of "upa" by "space".
8124 * Since a union expression does not have a (single) domain space,
8125 * "space" is necessarily a parameter space.
8127 * Since the order and the names of the parameters determine
8128 * the hash value, we need to create a new hash table.
8130 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8131 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8133 struct isl_union_pw_aff_reset_params_data data = { space };
8134 isl_bool match;
8136 match = isl_union_pw_aff_matching_params(upa, space);
8137 if (match < 0)
8138 upa = isl_union_pw_aff_free(upa);
8139 else if (match) {
8140 isl_space_free(space);
8141 return upa;
8144 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8145 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8146 data.res = isl_union_pw_aff_free(data.res);
8148 isl_union_pw_aff_free(upa);
8149 isl_space_free(space);
8150 return data.res;
8153 /* Return the floor of "pa".
8155 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8157 return isl_pw_aff_floor(pa);
8160 /* Given f, return floor(f).
8162 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8163 __isl_take isl_union_pw_aff *upa)
8165 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8168 /* Compute
8170 * upa mod m = upa - m * floor(upa/m)
8172 * with m an integer value.
8174 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8175 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8177 isl_union_pw_aff *res;
8179 if (!upa || !m)
8180 goto error;
8182 if (!isl_val_is_int(m))
8183 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8184 "expecting integer modulo", goto error);
8185 if (!isl_val_is_pos(m))
8186 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8187 "expecting positive modulo", goto error);
8189 res = isl_union_pw_aff_copy(upa);
8190 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8191 upa = isl_union_pw_aff_floor(upa);
8192 upa = isl_union_pw_aff_scale_val(upa, m);
8193 res = isl_union_pw_aff_sub(res, upa);
8195 return res;
8196 error:
8197 isl_val_free(m);
8198 isl_union_pw_aff_free(upa);
8199 return NULL;
8202 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8203 * pos is the output position that needs to be extracted.
8204 * res collects the results.
8206 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8207 int pos;
8208 isl_union_pw_aff *res;
8211 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8212 * (assuming it has such a dimension) and add it to data->res.
8214 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8216 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8217 isl_size n_out;
8218 isl_pw_aff *pa;
8220 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8221 if (n_out < 0)
8222 return isl_stat_error;
8223 if (data->pos >= n_out) {
8224 isl_pw_multi_aff_free(pma);
8225 return isl_stat_ok;
8228 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8229 isl_pw_multi_aff_free(pma);
8231 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8233 return data->res ? isl_stat_ok : isl_stat_error;
8236 /* Extract an isl_union_pw_aff corresponding to
8237 * output dimension "pos" of "upma".
8239 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8240 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8242 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8243 isl_space *space;
8245 if (!upma)
8246 return NULL;
8248 if (pos < 0)
8249 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8250 "cannot extract at negative position", return NULL);
8252 space = isl_union_pw_multi_aff_get_space(upma);
8253 data.res = isl_union_pw_aff_empty(space);
8254 data.pos = pos;
8255 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8256 &get_union_pw_aff, &data) < 0)
8257 data.res = isl_union_pw_aff_free(data.res);
8259 return data.res;
8262 /* Return a union piecewise affine expression
8263 * that is equal to "aff" on "domain".
8265 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8266 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8268 isl_pw_aff *pa;
8270 pa = isl_pw_aff_from_aff(aff);
8271 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8274 /* Return a union piecewise affine expression
8275 * that is equal to the parameter identified by "id" on "domain".
8277 * Make sure the parameter appears in the space passed to
8278 * isl_aff_param_on_domain_space_id.
8280 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8281 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8283 isl_space *space;
8284 isl_aff *aff;
8286 space = isl_union_set_get_space(domain);
8287 space = isl_space_add_param_id(space, isl_id_copy(id));
8288 aff = isl_aff_param_on_domain_space_id(space, id);
8289 return isl_union_pw_aff_aff_on_domain(domain, aff);
8292 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8293 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8294 * needs to attain.
8295 * "res" collects the results.
8297 struct isl_union_pw_aff_pw_aff_on_domain_data {
8298 isl_pw_aff *pa;
8299 isl_union_pw_aff *res;
8302 /* Construct a piecewise affine expression that is equal to data->pa
8303 * on "domain" and add the result to data->res.
8305 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8307 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8308 isl_pw_aff *pa;
8309 isl_size dim;
8311 pa = isl_pw_aff_copy(data->pa);
8312 dim = isl_set_dim(domain, isl_dim_set);
8313 if (dim < 0)
8314 pa = isl_pw_aff_free(pa);
8315 pa = isl_pw_aff_from_range(pa);
8316 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8317 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8318 pa = isl_pw_aff_intersect_domain(pa, domain);
8319 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8321 return data->res ? isl_stat_ok : isl_stat_error;
8324 /* Return a union piecewise affine expression
8325 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8326 * have been aligned.
8328 * Construct an isl_pw_aff on each of the sets in "domain" and
8329 * collect the results.
8331 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8332 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8334 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8335 isl_space *space;
8337 space = isl_union_set_get_space(domain);
8338 data.res = isl_union_pw_aff_empty(space);
8339 data.pa = pa;
8340 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8341 data.res = isl_union_pw_aff_free(data.res);
8342 isl_union_set_free(domain);
8343 isl_pw_aff_free(pa);
8344 return data.res;
8347 /* Return a union piecewise affine expression
8348 * that is equal to "pa" on "domain".
8350 * Check that "pa" is a parametric expression,
8351 * align the parameters if needed and call
8352 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8354 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8355 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8357 isl_bool is_set;
8358 isl_bool equal_params;
8359 isl_space *domain_space, *pa_space;
8361 pa_space = isl_pw_aff_peek_space(pa);
8362 is_set = isl_space_is_set(pa_space);
8363 if (is_set < 0)
8364 goto error;
8365 if (!is_set)
8366 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8367 "expecting parametric expression", goto error);
8369 domain_space = isl_union_set_get_space(domain);
8370 pa_space = isl_pw_aff_get_space(pa);
8371 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8372 if (equal_params >= 0 && !equal_params) {
8373 isl_space *space;
8375 space = isl_space_align_params(domain_space, pa_space);
8376 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8377 domain = isl_union_set_align_params(domain, space);
8378 } else {
8379 isl_space_free(domain_space);
8380 isl_space_free(pa_space);
8383 if (equal_params < 0)
8384 goto error;
8385 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8386 error:
8387 isl_union_set_free(domain);
8388 isl_pw_aff_free(pa);
8389 return NULL;
8392 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8393 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8394 * "res" collects the results.
8396 struct isl_union_pw_aff_val_on_domain_data {
8397 isl_val *v;
8398 isl_union_pw_aff *res;
8401 /* Construct a piecewise affine expression that is equal to data->v
8402 * on "domain" and add the result to data->res.
8404 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8406 struct isl_union_pw_aff_val_on_domain_data *data = user;
8407 isl_pw_aff *pa;
8408 isl_val *v;
8410 v = isl_val_copy(data->v);
8411 pa = isl_pw_aff_val_on_domain(domain, v);
8412 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8414 return data->res ? isl_stat_ok : isl_stat_error;
8417 /* Return a union piecewise affine expression
8418 * that is equal to "v" on "domain".
8420 * Construct an isl_pw_aff on each of the sets in "domain" and
8421 * collect the results.
8423 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8424 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8426 struct isl_union_pw_aff_val_on_domain_data data;
8427 isl_space *space;
8429 space = isl_union_set_get_space(domain);
8430 data.res = isl_union_pw_aff_empty(space);
8431 data.v = v;
8432 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8433 data.res = isl_union_pw_aff_free(data.res);
8434 isl_union_set_free(domain);
8435 isl_val_free(v);
8436 return data.res;
8439 /* Construct a piecewise multi affine expression
8440 * that is equal to "pa" and add it to upma.
8442 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8443 void *user)
8445 isl_union_pw_multi_aff **upma = user;
8446 isl_pw_multi_aff *pma;
8448 pma = isl_pw_multi_aff_from_pw_aff(pa);
8449 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8451 return *upma ? isl_stat_ok : isl_stat_error;
8454 /* Construct and return a union piecewise multi affine expression
8455 * that is equal to the given union piecewise affine expression.
8457 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8458 __isl_take isl_union_pw_aff *upa)
8460 isl_space *space;
8461 isl_union_pw_multi_aff *upma;
8463 if (!upa)
8464 return NULL;
8466 space = isl_union_pw_aff_get_space(upa);
8467 upma = isl_union_pw_multi_aff_empty(space);
8469 if (isl_union_pw_aff_foreach_pw_aff(upa,
8470 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8471 upma = isl_union_pw_multi_aff_free(upma);
8473 isl_union_pw_aff_free(upa);
8474 return upma;
8477 /* Compute the set of elements in the domain of "pa" where it is zero and
8478 * add this set to "uset".
8480 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8482 isl_union_set **uset = (isl_union_set **)user;
8484 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8486 return *uset ? isl_stat_ok : isl_stat_error;
8489 /* Return a union set containing those elements in the domain
8490 * of "upa" where it is zero.
8492 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8493 __isl_take isl_union_pw_aff *upa)
8495 isl_union_set *zero;
8497 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8498 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8499 zero = isl_union_set_free(zero);
8501 isl_union_pw_aff_free(upa);
8502 return zero;
8505 /* Internal data structure for isl_union_pw_aff_bind_id,
8506 * storing the parameter that needs to be bound and
8507 * the accumulated results.
8509 struct isl_bind_id_data {
8510 isl_id *id;
8511 isl_union_set *bound;
8514 /* Bind the piecewise affine function "pa" to the parameter data->id,
8515 * adding the resulting elements in the domain where the expression
8516 * is equal to the parameter to data->bound.
8518 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8520 struct isl_bind_id_data *data = user;
8521 isl_set *bound;
8523 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8524 data->bound = isl_union_set_add_set(data->bound, bound);
8526 return data->bound ? isl_stat_ok : isl_stat_error;
8529 /* Bind the union piecewise affine function "upa" to the parameter "id",
8530 * returning the elements in the domain where the expression
8531 * is equal to the parameter.
8533 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8534 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8536 struct isl_bind_id_data data = { id };
8538 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8539 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8540 data.bound = isl_union_set_free(data.bound);
8542 isl_union_pw_aff_free(upa);
8543 isl_id_free(id);
8544 return data.bound;
8547 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8548 * upma is the function that is plugged in.
8549 * pa is the current part of the function in which upma is plugged in.
8550 * res collects the results.
8552 struct isl_union_pw_aff_pullback_upma_data {
8553 isl_union_pw_multi_aff *upma;
8554 isl_pw_aff *pa;
8555 isl_union_pw_aff *res;
8558 /* Check if "pma" can be plugged into data->pa.
8559 * If so, perform the pullback and add the result to data->res.
8561 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8563 struct isl_union_pw_aff_pullback_upma_data *data = user;
8564 isl_pw_aff *pa;
8566 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8567 pma->dim, isl_dim_out)) {
8568 isl_pw_multi_aff_free(pma);
8569 return isl_stat_ok;
8572 pa = isl_pw_aff_copy(data->pa);
8573 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8575 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8577 return data->res ? isl_stat_ok : isl_stat_error;
8580 /* Check if any of the elements of data->upma can be plugged into pa,
8581 * add if so add the result to data->res.
8583 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8585 struct isl_union_pw_aff_pullback_upma_data *data = user;
8586 isl_stat r;
8588 data->pa = pa;
8589 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8590 &pa_pb_pma, data);
8591 isl_pw_aff_free(pa);
8593 return r;
8596 /* Compute the pullback of "upa" by the function represented by "upma".
8597 * In other words, plug in "upma" in "upa". The result contains
8598 * expressions defined over the domain space of "upma".
8600 * Run over all pairs of elements in "upa" and "upma", perform
8601 * the pullback when appropriate and collect the results.
8602 * If the hash value were based on the domain space rather than
8603 * the function space, then we could run through all elements
8604 * of "upma" and directly pick out the corresponding element of "upa".
8606 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8607 __isl_take isl_union_pw_aff *upa,
8608 __isl_take isl_union_pw_multi_aff *upma)
8610 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8611 isl_space *space;
8613 space = isl_union_pw_multi_aff_get_space(upma);
8614 upa = isl_union_pw_aff_align_params(upa, space);
8615 space = isl_union_pw_aff_get_space(upa);
8616 upma = isl_union_pw_multi_aff_align_params(upma, space);
8618 if (!upa || !upma)
8619 goto error;
8621 data.upma = upma;
8622 data.res = isl_union_pw_aff_alloc_same_size(upa);
8623 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8624 data.res = isl_union_pw_aff_free(data.res);
8626 isl_union_pw_aff_free(upa);
8627 isl_union_pw_multi_aff_free(upma);
8628 return data.res;
8629 error:
8630 isl_union_pw_aff_free(upa);
8631 isl_union_pw_multi_aff_free(upma);
8632 return NULL;
8635 #undef BASE
8636 #define BASE union_pw_aff
8637 #undef DOMBASE
8638 #define DOMBASE union_set
8640 #include <isl_multi_explicit_domain.c>
8641 #include <isl_multi_union_pw_aff_explicit_domain.c>
8642 #include <isl_multi_templ.c>
8643 #include <isl_multi_apply_set.c>
8644 #include <isl_multi_apply_union_set.c>
8645 #include <isl_multi_arith_templ.c>
8646 #include <isl_multi_bind_templ.c>
8647 #include <isl_multi_coalesce.c>
8648 #include <isl_multi_dim_id_templ.c>
8649 #include <isl_multi_floor.c>
8650 #include <isl_multi_from_base_templ.c>
8651 #include <isl_multi_gist.c>
8652 #include <isl_multi_align_set.c>
8653 #include <isl_multi_align_union_set.c>
8654 #include <isl_multi_intersect.c>
8655 #include <isl_multi_nan_templ.c>
8656 #include <isl_multi_tuple_id_templ.c>
8657 #include <isl_multi_union_add_templ.c>
8658 #include <isl_multi_zero_space_templ.c>
8660 /* Does "mupa" have a non-trivial explicit domain?
8662 * The explicit domain, if present, is trivial if it represents
8663 * an (obviously) universe parameter set.
8665 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8666 __isl_keep isl_multi_union_pw_aff *mupa)
8668 isl_bool is_params, trivial;
8669 isl_set *set;
8671 if (!mupa)
8672 return isl_bool_error;
8673 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8674 return isl_bool_false;
8675 is_params = isl_union_set_is_params(mupa->u.dom);
8676 if (is_params < 0 || !is_params)
8677 return isl_bool_not(is_params);
8678 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8679 trivial = isl_set_plain_is_universe(set);
8680 isl_set_free(set);
8681 return isl_bool_not(trivial);
8684 /* Construct a multiple union piecewise affine expression
8685 * in the given space with value zero in each of the output dimensions.
8687 * Since there is no canonical zero value for
8688 * a union piecewise affine expression, we can only construct
8689 * a zero-dimensional "zero" value.
8691 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8692 __isl_take isl_space *space)
8694 isl_bool params;
8695 isl_size dim;
8697 if (!space)
8698 return NULL;
8700 params = isl_space_is_params(space);
8701 if (params < 0)
8702 goto error;
8703 if (params)
8704 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8705 "expecting proper set space", goto error);
8706 if (!isl_space_is_set(space))
8707 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8708 "expecting set space", goto error);
8709 dim = isl_space_dim(space, isl_dim_out);
8710 if (dim < 0)
8711 goto error;
8712 if (dim != 0)
8713 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8714 "expecting 0D space", goto error);
8716 return isl_multi_union_pw_aff_alloc(space);
8717 error:
8718 isl_space_free(space);
8719 return NULL;
8722 /* Construct and return a multi union piecewise affine expression
8723 * that is equal to the given multi affine expression.
8725 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8726 __isl_take isl_multi_aff *ma)
8728 isl_multi_pw_aff *mpa;
8730 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8731 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8734 /* This function performs the same operation as
8735 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8736 * isl_multi_aff when exported.
8738 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8739 __isl_take isl_multi_aff *ma)
8741 return isl_multi_union_pw_aff_from_multi_aff(ma);
8744 /* Construct and return a multi union piecewise affine expression
8745 * that is equal to the given multi piecewise affine expression.
8747 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8748 __isl_take isl_multi_pw_aff *mpa)
8750 int i;
8751 isl_size n;
8752 isl_space *space;
8753 isl_multi_union_pw_aff *mupa;
8755 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8756 if (n < 0)
8757 mpa = isl_multi_pw_aff_free(mpa);
8758 if (!mpa)
8759 return NULL;
8761 space = isl_multi_pw_aff_get_space(mpa);
8762 space = isl_space_range(space);
8763 mupa = isl_multi_union_pw_aff_alloc(space);
8765 for (i = 0; i < n; ++i) {
8766 isl_pw_aff *pa;
8767 isl_union_pw_aff *upa;
8769 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8770 upa = isl_union_pw_aff_from_pw_aff(pa);
8771 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8774 isl_multi_pw_aff_free(mpa);
8776 return mupa;
8779 /* Extract the range space of "pma" and assign it to *space.
8780 * If *space has already been set (through a previous call to this function),
8781 * then check that the range space is the same.
8783 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8785 isl_space **space = user;
8786 isl_space *pma_space;
8787 isl_bool equal;
8789 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8790 isl_pw_multi_aff_free(pma);
8792 if (!pma_space)
8793 return isl_stat_error;
8794 if (!*space) {
8795 *space = pma_space;
8796 return isl_stat_ok;
8799 equal = isl_space_is_equal(pma_space, *space);
8800 isl_space_free(pma_space);
8802 if (equal < 0)
8803 return isl_stat_error;
8804 if (!equal)
8805 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8806 "range spaces not the same", return isl_stat_error);
8807 return isl_stat_ok;
8810 /* Construct and return a multi union piecewise affine expression
8811 * that is equal to the given union piecewise multi affine expression.
8813 * In order to be able to perform the conversion, the input
8814 * needs to be non-empty and may only involve a single range space.
8816 * If the resulting multi union piecewise affine expression has
8817 * an explicit domain, then assign it the domain of the input.
8818 * In other cases, the domain is stored in the individual elements.
8820 __isl_give isl_multi_union_pw_aff *
8821 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8822 __isl_take isl_union_pw_multi_aff *upma)
8824 isl_space *space = NULL;
8825 isl_multi_union_pw_aff *mupa;
8826 int i;
8827 isl_size n;
8829 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8830 if (n < 0)
8831 goto error;
8832 if (n == 0)
8833 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8834 "cannot extract range space from empty input",
8835 goto error);
8836 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8837 &space) < 0)
8838 goto error;
8840 if (!space)
8841 goto error;
8843 n = isl_space_dim(space, isl_dim_set);
8844 if (n < 0)
8845 space = isl_space_free(space);
8846 mupa = isl_multi_union_pw_aff_alloc(space);
8848 for (i = 0; i < n; ++i) {
8849 isl_union_pw_aff *upa;
8851 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8852 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8854 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8855 isl_union_set *dom;
8856 isl_union_pw_multi_aff *copy;
8858 copy = isl_union_pw_multi_aff_copy(upma);
8859 dom = isl_union_pw_multi_aff_domain(copy);
8860 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8863 isl_union_pw_multi_aff_free(upma);
8864 return mupa;
8865 error:
8866 isl_space_free(space);
8867 isl_union_pw_multi_aff_free(upma);
8868 return NULL;
8871 /* This function performs the same operation as
8872 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8873 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8875 __isl_give isl_multi_union_pw_aff *
8876 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8877 __isl_take isl_union_pw_multi_aff *upma)
8879 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8882 /* Try and create an isl_multi_union_pw_aff that is equivalent
8883 * to the given isl_union_map.
8884 * The isl_union_map is required to be single-valued in each space.
8885 * Moreover, it cannot be empty and all range spaces need to be the same.
8886 * Otherwise, an error is produced.
8888 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8889 __isl_take isl_union_map *umap)
8891 isl_union_pw_multi_aff *upma;
8893 upma = isl_union_pw_multi_aff_from_union_map(umap);
8894 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8897 /* This function performs the same operation as
8898 * isl_multi_union_pw_aff_from_union_map,
8899 * but is considered as a function on an isl_union_map when exported.
8901 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8902 __isl_take isl_union_map *umap)
8904 return isl_multi_union_pw_aff_from_union_map(umap);
8907 /* Return a multiple union piecewise affine expression
8908 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8909 * have been aligned.
8911 * If the resulting multi union piecewise affine expression has
8912 * an explicit domain, then assign it the input domain.
8913 * In other cases, the domain is stored in the individual elements.
8915 static __isl_give isl_multi_union_pw_aff *
8916 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8917 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8919 int i;
8920 isl_size n;
8921 isl_space *space;
8922 isl_multi_union_pw_aff *mupa;
8924 n = isl_multi_val_dim(mv, isl_dim_set);
8925 if (!domain || n < 0)
8926 goto error;
8928 space = isl_multi_val_get_space(mv);
8929 mupa = isl_multi_union_pw_aff_alloc(space);
8930 for (i = 0; i < n; ++i) {
8931 isl_val *v;
8932 isl_union_pw_aff *upa;
8934 v = isl_multi_val_get_val(mv, i);
8935 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8937 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8939 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8940 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8941 isl_union_set_copy(domain));
8943 isl_union_set_free(domain);
8944 isl_multi_val_free(mv);
8945 return mupa;
8946 error:
8947 isl_union_set_free(domain);
8948 isl_multi_val_free(mv);
8949 return NULL;
8952 /* Return a multiple union piecewise affine expression
8953 * that is equal to "mv" on "domain".
8955 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8956 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8958 isl_bool equal_params;
8960 if (!domain || !mv)
8961 goto error;
8962 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8963 if (equal_params < 0)
8964 goto error;
8965 if (equal_params)
8966 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8967 domain, mv);
8968 domain = isl_union_set_align_params(domain,
8969 isl_multi_val_get_space(mv));
8970 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8971 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8972 error:
8973 isl_union_set_free(domain);
8974 isl_multi_val_free(mv);
8975 return NULL;
8978 /* Return a multiple union piecewise affine expression
8979 * that is equal to "ma" on "domain".
8981 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8982 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8984 isl_pw_multi_aff *pma;
8986 pma = isl_pw_multi_aff_from_multi_aff(ma);
8987 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8990 /* Return a multiple union piecewise affine expression
8991 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8992 * have been aligned.
8994 * If the resulting multi union piecewise affine expression has
8995 * an explicit domain, then assign it the input domain.
8996 * In other cases, the domain is stored in the individual elements.
8998 static __isl_give isl_multi_union_pw_aff *
8999 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9000 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
9002 int i;
9003 isl_size n;
9004 isl_space *space;
9005 isl_multi_union_pw_aff *mupa;
9007 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
9008 if (!domain || n < 0)
9009 goto error;
9010 space = isl_pw_multi_aff_get_space(pma);
9011 mupa = isl_multi_union_pw_aff_alloc(space);
9012 for (i = 0; i < n; ++i) {
9013 isl_pw_aff *pa;
9014 isl_union_pw_aff *upa;
9016 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9017 upa = isl_union_pw_aff_pw_aff_on_domain(
9018 isl_union_set_copy(domain), pa);
9019 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9021 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9022 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
9023 isl_union_set_copy(domain));
9025 isl_union_set_free(domain);
9026 isl_pw_multi_aff_free(pma);
9027 return mupa;
9028 error:
9029 isl_union_set_free(domain);
9030 isl_pw_multi_aff_free(pma);
9031 return NULL;
9034 /* Return a multiple union piecewise affine expression
9035 * that is equal to "pma" on "domain".
9037 __isl_give isl_multi_union_pw_aff *
9038 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
9039 __isl_take isl_pw_multi_aff *pma)
9041 isl_bool equal_params;
9042 isl_space *space;
9044 space = isl_pw_multi_aff_peek_space(pma);
9045 equal_params = isl_union_set_space_has_equal_params(domain, space);
9046 if (equal_params < 0)
9047 goto error;
9048 if (equal_params)
9049 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9050 domain, pma);
9051 domain = isl_union_set_align_params(domain,
9052 isl_pw_multi_aff_get_space(pma));
9053 pma = isl_pw_multi_aff_align_params(pma,
9054 isl_union_set_get_space(domain));
9055 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
9056 pma);
9057 error:
9058 isl_union_set_free(domain);
9059 isl_pw_multi_aff_free(pma);
9060 return NULL;
9063 /* Return a union set containing those elements in the domains
9064 * of the elements of "mupa" where they are all zero.
9066 * If there are no elements, then simply return the entire domain.
9068 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
9069 __isl_take isl_multi_union_pw_aff *mupa)
9071 int i;
9072 isl_size n;
9073 isl_union_pw_aff *upa;
9074 isl_union_set *zero;
9076 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9077 if (n < 0)
9078 mupa = isl_multi_union_pw_aff_free(mupa);
9079 if (!mupa)
9080 return NULL;
9082 if (n == 0)
9083 return isl_multi_union_pw_aff_domain(mupa);
9085 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9086 zero = isl_union_pw_aff_zero_union_set(upa);
9088 for (i = 1; i < n; ++i) {
9089 isl_union_set *zero_i;
9091 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9092 zero_i = isl_union_pw_aff_zero_union_set(upa);
9094 zero = isl_union_set_intersect(zero, zero_i);
9097 isl_multi_union_pw_aff_free(mupa);
9098 return zero;
9101 /* Construct a union map mapping the shared domain
9102 * of the union piecewise affine expressions to the range of "mupa"
9103 * in the special case of a 0D multi union piecewise affine expression.
9105 * Construct a map between the explicit domain of "mupa" and
9106 * the range space.
9107 * Note that this assumes that the domain consists of explicit elements.
9109 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9110 __isl_take isl_multi_union_pw_aff *mupa)
9112 isl_bool is_params;
9113 isl_space *space;
9114 isl_union_set *dom, *ran;
9116 space = isl_multi_union_pw_aff_get_space(mupa);
9117 dom = isl_multi_union_pw_aff_domain(mupa);
9118 ran = isl_union_set_from_set(isl_set_universe(space));
9120 is_params = isl_union_set_is_params(dom);
9121 if (is_params < 0)
9122 dom = isl_union_set_free(dom);
9123 else if (is_params)
9124 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9125 "cannot create union map from expression without "
9126 "explicit domain elements",
9127 dom = isl_union_set_free(dom));
9129 return isl_union_map_from_domain_and_range(dom, ran);
9132 /* Construct a union map mapping the shared domain
9133 * of the union piecewise affine expressions to the range of "mupa"
9134 * with each dimension in the range equated to the
9135 * corresponding union piecewise affine expression.
9137 * If the input is zero-dimensional, then construct a mapping
9138 * from its explicit domain.
9140 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9141 __isl_take isl_multi_union_pw_aff *mupa)
9143 int i;
9144 isl_size n;
9145 isl_space *space;
9146 isl_union_map *umap;
9147 isl_union_pw_aff *upa;
9149 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9150 if (n < 0)
9151 mupa = isl_multi_union_pw_aff_free(mupa);
9152 if (!mupa)
9153 return NULL;
9155 if (n == 0)
9156 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9158 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9159 umap = isl_union_map_from_union_pw_aff(upa);
9161 for (i = 1; i < n; ++i) {
9162 isl_union_map *umap_i;
9164 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9165 umap_i = isl_union_map_from_union_pw_aff(upa);
9166 umap = isl_union_map_flat_range_product(umap, umap_i);
9169 space = isl_multi_union_pw_aff_get_space(mupa);
9170 umap = isl_union_map_reset_range_space(umap, space);
9172 isl_multi_union_pw_aff_free(mupa);
9173 return umap;
9176 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9177 * "range" is the space from which to set the range space.
9178 * "res" collects the results.
9180 struct isl_union_pw_multi_aff_reset_range_space_data {
9181 isl_space *range;
9182 isl_union_pw_multi_aff *res;
9185 /* Replace the range space of "pma" by the range space of data->range and
9186 * add the result to data->res.
9188 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9190 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9191 isl_space *space;
9193 space = isl_pw_multi_aff_get_space(pma);
9194 space = isl_space_domain(space);
9195 space = isl_space_extend_domain_with_range(space,
9196 isl_space_copy(data->range));
9197 pma = isl_pw_multi_aff_reset_space(pma, space);
9198 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9200 return data->res ? isl_stat_ok : isl_stat_error;
9203 /* Replace the range space of all the piecewise affine expressions in "upma" by
9204 * the range space of "space".
9206 * This assumes that all these expressions have the same output dimension.
9208 * Since the spaces of the expressions change, so do their hash values.
9209 * We therefore need to create a new isl_union_pw_multi_aff.
9210 * Note that the hash value is currently computed based on the entire
9211 * space even though there can only be a single expression with a given
9212 * domain space.
9214 static __isl_give isl_union_pw_multi_aff *
9215 isl_union_pw_multi_aff_reset_range_space(
9216 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9218 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9219 isl_space *space_upma;
9221 space_upma = isl_union_pw_multi_aff_get_space(upma);
9222 data.res = isl_union_pw_multi_aff_empty(space_upma);
9223 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9224 &reset_range_space, &data) < 0)
9225 data.res = isl_union_pw_multi_aff_free(data.res);
9227 isl_space_free(space);
9228 isl_union_pw_multi_aff_free(upma);
9229 return data.res;
9232 /* Construct and return a union piecewise multi affine expression
9233 * that is equal to the given multi union piecewise affine expression,
9234 * in the special case of a 0D multi union piecewise affine expression.
9236 * Construct a union piecewise multi affine expression
9237 * on top of the explicit domain of the input.
9239 __isl_give isl_union_pw_multi_aff *
9240 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9241 __isl_take isl_multi_union_pw_aff *mupa)
9243 isl_space *space;
9244 isl_multi_val *mv;
9245 isl_union_set *domain;
9247 space = isl_multi_union_pw_aff_get_space(mupa);
9248 mv = isl_multi_val_zero(space);
9249 domain = isl_multi_union_pw_aff_domain(mupa);
9250 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9253 /* Construct and return a union piecewise multi affine expression
9254 * that is equal to the given multi union piecewise affine expression.
9256 * If the input is zero-dimensional, then
9257 * construct a union piecewise multi affine expression
9258 * on top of the explicit domain of the input.
9260 __isl_give isl_union_pw_multi_aff *
9261 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9262 __isl_take isl_multi_union_pw_aff *mupa)
9264 int i;
9265 isl_size n;
9266 isl_space *space;
9267 isl_union_pw_multi_aff *upma;
9268 isl_union_pw_aff *upa;
9270 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9271 if (n < 0)
9272 mupa = isl_multi_union_pw_aff_free(mupa);
9273 if (!mupa)
9274 return NULL;
9276 if (n == 0)
9277 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9279 space = isl_multi_union_pw_aff_get_space(mupa);
9280 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9281 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9283 for (i = 1; i < n; ++i) {
9284 isl_union_pw_multi_aff *upma_i;
9286 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9287 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9288 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9291 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9293 isl_multi_union_pw_aff_free(mupa);
9294 return upma;
9297 /* Intersect the range of "mupa" with "range",
9298 * in the special case where "mupa" is 0D.
9300 * Intersect the domain of "mupa" with the constraints on the parameters
9301 * of "range".
9303 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9304 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9306 range = isl_set_params(range);
9307 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9308 return mupa;
9311 /* Intersect the range of "mupa" with "range".
9312 * That is, keep only those domain elements that have a function value
9313 * in "range".
9315 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9316 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9318 isl_union_pw_multi_aff *upma;
9319 isl_union_set *domain;
9320 isl_space *space;
9321 isl_size n;
9322 int match;
9324 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9325 if (n < 0 || !range)
9326 goto error;
9328 space = isl_set_get_space(range);
9329 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9330 space, isl_dim_set);
9331 isl_space_free(space);
9332 if (match < 0)
9333 goto error;
9334 if (!match)
9335 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9336 "space don't match", goto error);
9337 if (n == 0)
9338 return mupa_intersect_range_0D(mupa, range);
9340 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9341 isl_multi_union_pw_aff_copy(mupa));
9342 domain = isl_union_set_from_set(range);
9343 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9344 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9346 return mupa;
9347 error:
9348 isl_multi_union_pw_aff_free(mupa);
9349 isl_set_free(range);
9350 return NULL;
9353 /* Return the shared domain of the elements of "mupa",
9354 * in the special case where "mupa" is zero-dimensional.
9356 * Return the explicit domain of "mupa".
9357 * Note that this domain may be a parameter set, either
9358 * because "mupa" is meant to live in a set space or
9359 * because no explicit domain has been set.
9361 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9362 __isl_take isl_multi_union_pw_aff *mupa)
9364 isl_union_set *dom;
9366 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9367 isl_multi_union_pw_aff_free(mupa);
9369 return dom;
9372 /* Return the shared domain of the elements of "mupa".
9374 * If "mupa" is zero-dimensional, then return its explicit domain.
9376 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9377 __isl_take isl_multi_union_pw_aff *mupa)
9379 int i;
9380 isl_size n;
9381 isl_union_pw_aff *upa;
9382 isl_union_set *dom;
9384 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9385 if (n < 0)
9386 mupa = isl_multi_union_pw_aff_free(mupa);
9387 if (!mupa)
9388 return NULL;
9390 if (n == 0)
9391 return isl_multi_union_pw_aff_domain_0D(mupa);
9393 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9394 dom = isl_union_pw_aff_domain(upa);
9395 for (i = 1; i < n; ++i) {
9396 isl_union_set *dom_i;
9398 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9399 dom_i = isl_union_pw_aff_domain(upa);
9400 dom = isl_union_set_intersect(dom, dom_i);
9403 isl_multi_union_pw_aff_free(mupa);
9404 return dom;
9407 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9408 * In particular, the spaces have been aligned.
9409 * The result is defined over the shared domain of the elements of "mupa"
9411 * We first extract the parametric constant part of "aff" and
9412 * define that over the shared domain.
9413 * Then we iterate over all input dimensions of "aff" and add the corresponding
9414 * multiples of the elements of "mupa".
9415 * Finally, we consider the integer divisions, calling the function
9416 * recursively to obtain an isl_union_pw_aff corresponding to the
9417 * integer division argument.
9419 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9420 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9422 int i;
9423 isl_size n_in, n_div;
9424 isl_union_pw_aff *upa;
9425 isl_union_set *uset;
9426 isl_val *v;
9427 isl_aff *cst;
9429 n_in = isl_aff_dim(aff, isl_dim_in);
9430 n_div = isl_aff_dim(aff, isl_dim_div);
9431 if (n_in < 0 || n_div < 0)
9432 goto error;
9434 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9435 cst = isl_aff_copy(aff);
9436 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9437 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9438 cst = isl_aff_project_domain_on_params(cst);
9439 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9441 for (i = 0; i < n_in; ++i) {
9442 isl_union_pw_aff *upa_i;
9444 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9445 continue;
9446 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9447 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9448 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9449 upa = isl_union_pw_aff_add(upa, upa_i);
9452 for (i = 0; i < n_div; ++i) {
9453 isl_aff *div;
9454 isl_union_pw_aff *upa_i;
9456 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9457 continue;
9458 div = isl_aff_get_div(aff, i);
9459 upa_i = multi_union_pw_aff_apply_aff(
9460 isl_multi_union_pw_aff_copy(mupa), div);
9461 upa_i = isl_union_pw_aff_floor(upa_i);
9462 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9463 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9464 upa = isl_union_pw_aff_add(upa, upa_i);
9467 isl_multi_union_pw_aff_free(mupa);
9468 isl_aff_free(aff);
9470 return upa;
9471 error:
9472 isl_multi_union_pw_aff_free(mupa);
9473 isl_aff_free(aff);
9474 return NULL;
9477 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9478 * with the domain of "aff".
9479 * Furthermore, the dimension of this space needs to be greater than zero.
9480 * The result is defined over the shared domain of the elements of "mupa"
9482 * We perform these checks and then hand over control to
9483 * multi_union_pw_aff_apply_aff.
9485 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9486 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9488 isl_size dim;
9489 isl_space *space1, *space2;
9490 isl_bool equal;
9492 mupa = isl_multi_union_pw_aff_align_params(mupa,
9493 isl_aff_get_space(aff));
9494 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9495 if (!mupa || !aff)
9496 goto error;
9498 space1 = isl_multi_union_pw_aff_get_space(mupa);
9499 space2 = isl_aff_get_domain_space(aff);
9500 equal = isl_space_is_equal(space1, space2);
9501 isl_space_free(space1);
9502 isl_space_free(space2);
9503 if (equal < 0)
9504 goto error;
9505 if (!equal)
9506 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9507 "spaces don't match", goto error);
9508 dim = isl_aff_dim(aff, isl_dim_in);
9509 if (dim < 0)
9510 goto error;
9511 if (dim == 0)
9512 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9513 "cannot determine domains", goto error);
9515 return multi_union_pw_aff_apply_aff(mupa, aff);
9516 error:
9517 isl_multi_union_pw_aff_free(mupa);
9518 isl_aff_free(aff);
9519 return NULL;
9522 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9523 * The space of "mupa" is known to be compatible with the domain of "ma".
9525 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9526 * on the domain of "mupa".
9528 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9529 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9531 isl_union_set *dom;
9533 dom = isl_multi_union_pw_aff_domain(mupa);
9534 ma = isl_multi_aff_project_domain_on_params(ma);
9536 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9539 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9540 * with the domain of "ma".
9541 * The result is defined over the shared domain of the elements of "mupa"
9543 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9544 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9546 isl_space *space1, *space2;
9547 isl_multi_union_pw_aff *res;
9548 isl_bool equal;
9549 int i;
9550 isl_size n_in, n_out;
9552 mupa = isl_multi_union_pw_aff_align_params(mupa,
9553 isl_multi_aff_get_space(ma));
9554 ma = isl_multi_aff_align_params(ma,
9555 isl_multi_union_pw_aff_get_space(mupa));
9556 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9557 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9558 if (!mupa || n_in < 0 || n_out < 0)
9559 goto error;
9561 space1 = isl_multi_union_pw_aff_get_space(mupa);
9562 space2 = isl_multi_aff_get_domain_space(ma);
9563 equal = isl_space_is_equal(space1, space2);
9564 isl_space_free(space1);
9565 isl_space_free(space2);
9566 if (equal < 0)
9567 goto error;
9568 if (!equal)
9569 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9570 "spaces don't match", goto error);
9571 if (n_in == 0)
9572 return mupa_apply_multi_aff_0D(mupa, ma);
9574 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9575 res = isl_multi_union_pw_aff_alloc(space1);
9577 for (i = 0; i < n_out; ++i) {
9578 isl_aff *aff;
9579 isl_union_pw_aff *upa;
9581 aff = isl_multi_aff_get_aff(ma, i);
9582 upa = multi_union_pw_aff_apply_aff(
9583 isl_multi_union_pw_aff_copy(mupa), aff);
9584 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9587 isl_multi_aff_free(ma);
9588 isl_multi_union_pw_aff_free(mupa);
9589 return res;
9590 error:
9591 isl_multi_union_pw_aff_free(mupa);
9592 isl_multi_aff_free(ma);
9593 return NULL;
9596 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9597 * The space of "mupa" is known to be compatible with the domain of "pa".
9599 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9600 * on the domain of "mupa".
9602 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9603 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9605 isl_union_set *dom;
9607 dom = isl_multi_union_pw_aff_domain(mupa);
9608 pa = isl_pw_aff_project_domain_on_params(pa);
9610 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9613 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9614 * with the domain of "pa".
9615 * Furthermore, the dimension of this space needs to be greater than zero.
9616 * The result is defined over the shared domain of the elements of "mupa"
9618 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9619 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9621 int i;
9622 isl_bool equal;
9623 isl_size n_in;
9624 isl_space *space, *space2;
9625 isl_union_pw_aff *upa;
9627 mupa = isl_multi_union_pw_aff_align_params(mupa,
9628 isl_pw_aff_get_space(pa));
9629 pa = isl_pw_aff_align_params(pa,
9630 isl_multi_union_pw_aff_get_space(mupa));
9631 if (!mupa || !pa)
9632 goto error;
9634 space = isl_multi_union_pw_aff_get_space(mupa);
9635 space2 = isl_pw_aff_get_domain_space(pa);
9636 equal = isl_space_is_equal(space, space2);
9637 isl_space_free(space);
9638 isl_space_free(space2);
9639 if (equal < 0)
9640 goto error;
9641 if (!equal)
9642 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9643 "spaces don't match", goto error);
9644 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9645 if (n_in < 0)
9646 goto error;
9647 if (n_in == 0)
9648 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9650 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9651 upa = isl_union_pw_aff_empty(space);
9653 for (i = 0; i < pa->n; ++i) {
9654 isl_aff *aff;
9655 isl_set *domain;
9656 isl_multi_union_pw_aff *mupa_i;
9657 isl_union_pw_aff *upa_i;
9659 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9660 domain = isl_set_copy(pa->p[i].set);
9661 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9662 aff = isl_aff_copy(pa->p[i].aff);
9663 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9664 upa = isl_union_pw_aff_union_add(upa, upa_i);
9667 isl_multi_union_pw_aff_free(mupa);
9668 isl_pw_aff_free(pa);
9669 return upa;
9670 error:
9671 isl_multi_union_pw_aff_free(mupa);
9672 isl_pw_aff_free(pa);
9673 return NULL;
9676 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9677 * The space of "mupa" is known to be compatible with the domain of "pma".
9679 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9680 * on the domain of "mupa".
9682 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9683 __isl_take isl_multi_union_pw_aff *mupa,
9684 __isl_take isl_pw_multi_aff *pma)
9686 isl_union_set *dom;
9688 dom = isl_multi_union_pw_aff_domain(mupa);
9689 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9691 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9694 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9695 * with the domain of "pma".
9696 * The result is defined over the shared domain of the elements of "mupa"
9698 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9699 __isl_take isl_multi_union_pw_aff *mupa,
9700 __isl_take isl_pw_multi_aff *pma)
9702 isl_space *space1, *space2;
9703 isl_multi_union_pw_aff *res;
9704 isl_bool equal;
9705 int i;
9706 isl_size n_in, n_out;
9708 mupa = isl_multi_union_pw_aff_align_params(mupa,
9709 isl_pw_multi_aff_get_space(pma));
9710 pma = isl_pw_multi_aff_align_params(pma,
9711 isl_multi_union_pw_aff_get_space(mupa));
9712 if (!mupa || !pma)
9713 goto error;
9715 space1 = isl_multi_union_pw_aff_get_space(mupa);
9716 space2 = isl_pw_multi_aff_get_domain_space(pma);
9717 equal = isl_space_is_equal(space1, space2);
9718 isl_space_free(space1);
9719 isl_space_free(space2);
9720 if (equal < 0)
9721 goto error;
9722 if (!equal)
9723 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9724 "spaces don't match", goto error);
9725 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9726 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9727 if (n_in < 0 || n_out < 0)
9728 goto error;
9729 if (n_in == 0)
9730 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9732 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9733 res = isl_multi_union_pw_aff_alloc(space1);
9735 for (i = 0; i < n_out; ++i) {
9736 isl_pw_aff *pa;
9737 isl_union_pw_aff *upa;
9739 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9740 upa = isl_multi_union_pw_aff_apply_pw_aff(
9741 isl_multi_union_pw_aff_copy(mupa), pa);
9742 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9745 isl_pw_multi_aff_free(pma);
9746 isl_multi_union_pw_aff_free(mupa);
9747 return res;
9748 error:
9749 isl_multi_union_pw_aff_free(mupa);
9750 isl_pw_multi_aff_free(pma);
9751 return NULL;
9754 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9755 * If the explicit domain only keeps track of constraints on the parameters,
9756 * then only update those constraints.
9758 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9759 __isl_take isl_multi_union_pw_aff *mupa,
9760 __isl_keep isl_union_pw_multi_aff *upma)
9762 isl_bool is_params;
9764 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9765 return isl_multi_union_pw_aff_free(mupa);
9767 mupa = isl_multi_union_pw_aff_cow(mupa);
9768 if (!mupa)
9769 return NULL;
9771 is_params = isl_union_set_is_params(mupa->u.dom);
9772 if (is_params < 0)
9773 return isl_multi_union_pw_aff_free(mupa);
9775 upma = isl_union_pw_multi_aff_copy(upma);
9776 if (is_params)
9777 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9778 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9779 else
9780 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9781 mupa->u.dom, upma);
9782 if (!mupa->u.dom)
9783 return isl_multi_union_pw_aff_free(mupa);
9784 return mupa;
9787 /* Compute the pullback of "mupa" by the function represented by "upma".
9788 * In other words, plug in "upma" in "mupa". The result contains
9789 * expressions defined over the domain space of "upma".
9791 * Run over all elements of "mupa" and plug in "upma" in each of them.
9793 * If "mupa" has an explicit domain, then it is this domain
9794 * that needs to undergo a pullback instead, i.e., a preimage.
9796 __isl_give isl_multi_union_pw_aff *
9797 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9798 __isl_take isl_multi_union_pw_aff *mupa,
9799 __isl_take isl_union_pw_multi_aff *upma)
9801 int i;
9802 isl_size n;
9804 mupa = isl_multi_union_pw_aff_align_params(mupa,
9805 isl_union_pw_multi_aff_get_space(upma));
9806 upma = isl_union_pw_multi_aff_align_params(upma,
9807 isl_multi_union_pw_aff_get_space(mupa));
9808 mupa = isl_multi_union_pw_aff_cow(mupa);
9809 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9810 if (n < 0 || !upma)
9811 goto error;
9813 for (i = 0; i < n; ++i) {
9814 isl_union_pw_aff *upa;
9816 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9817 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9818 isl_union_pw_multi_aff_copy(upma));
9819 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9822 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9823 mupa = preimage_explicit_domain(mupa, upma);
9825 isl_union_pw_multi_aff_free(upma);
9826 return mupa;
9827 error:
9828 isl_multi_union_pw_aff_free(mupa);
9829 isl_union_pw_multi_aff_free(upma);
9830 return NULL;
9833 /* Extract the sequence of elements in "mupa" with domain space "space"
9834 * (ignoring parameters).
9836 * For the elements of "mupa" that are not defined on the specified space,
9837 * the corresponding element in the result is empty.
9839 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9840 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9842 int i;
9843 isl_size n;
9844 isl_space *space_mpa;
9845 isl_multi_pw_aff *mpa;
9847 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9848 if (n < 0 || !space)
9849 goto error;
9851 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9852 space = isl_space_replace_params(space, space_mpa);
9853 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9854 space_mpa);
9855 mpa = isl_multi_pw_aff_alloc(space_mpa);
9857 space = isl_space_from_domain(space);
9858 space = isl_space_add_dims(space, isl_dim_out, 1);
9859 for (i = 0; i < n; ++i) {
9860 isl_union_pw_aff *upa;
9861 isl_pw_aff *pa;
9863 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9864 pa = isl_union_pw_aff_extract_pw_aff(upa,
9865 isl_space_copy(space));
9866 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9867 isl_union_pw_aff_free(upa);
9870 isl_space_free(space);
9871 return mpa;
9872 error:
9873 isl_space_free(space);
9874 return NULL;
9877 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9878 * should modify the base expressions in the input.
9880 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9881 * are taken into account.
9882 * "fn" is applied to each entry in the input.
9884 struct isl_union_pw_multi_aff_un_op_control {
9885 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9886 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9889 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9890 * a second argument) for use as an isl_union_pw_multi_aff_transform
9891 * filter function (which does take a second argument).
9892 * Simply call control->filter without the second argument.
9894 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9895 __isl_take isl_pw_multi_aff *pma, void *user)
9897 struct isl_union_pw_multi_aff_un_op_control *control = user;
9899 return control->filter(pma);
9902 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9903 * a second argument) for use as an isl_union_pw_multi_aff_transform
9904 * base function (which does take a second argument).
9905 * Simply call control->fn without the second argument.
9907 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9908 __isl_take isl_pw_multi_aff *pma, void *user)
9910 struct isl_union_pw_multi_aff_un_op_control *control = user;
9912 return control->fn(pma);
9915 /* Construct an isl_union_pw_multi_aff that is obtained by
9916 * modifying "upma" according to "control".
9918 * isl_union_pw_multi_aff_transform performs essentially
9919 * the same operation, but takes a filter and a callback function
9920 * of a different form (with an extra argument).
9921 * Call isl_union_pw_multi_aff_transform with wrappers
9922 * that remove this extra argument.
9924 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9925 __isl_take isl_union_pw_multi_aff *upma,
9926 struct isl_union_pw_multi_aff_un_op_control *control)
9928 struct isl_union_pw_multi_aff_transform_control t_control = {
9929 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9930 .filter_user = control,
9931 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9932 .fn_user = control,
9935 return isl_union_pw_multi_aff_transform(upma, &t_control);
9938 /* For each function in "upma" of the form A -> [B -> C],
9939 * extract the function A -> B and collect the results.
9941 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9942 __isl_take isl_union_pw_multi_aff *upma)
9944 struct isl_union_pw_multi_aff_un_op_control control = {
9945 .filter = &isl_pw_multi_aff_range_is_wrapping,
9946 .fn = &isl_pw_multi_aff_range_factor_domain,
9948 return isl_union_pw_multi_aff_un_op(upma, &control);
9951 /* For each function in "upma" of the form A -> [B -> C],
9952 * extract the function A -> C and collect the results.
9954 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9955 __isl_take isl_union_pw_multi_aff *upma)
9957 struct isl_union_pw_multi_aff_un_op_control control = {
9958 .filter = &isl_pw_multi_aff_range_is_wrapping,
9959 .fn = &isl_pw_multi_aff_range_factor_range,
9961 return isl_union_pw_multi_aff_un_op(upma, &control);
9964 /* Evaluate the affine function "aff" in the void point "pnt".
9965 * In particular, return the value NaN.
9967 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9968 __isl_take isl_point *pnt)
9970 isl_ctx *ctx;
9972 ctx = isl_point_get_ctx(pnt);
9973 isl_aff_free(aff);
9974 isl_point_free(pnt);
9975 return isl_val_nan(ctx);
9978 /* Evaluate the affine expression "aff"
9979 * in the coordinates (with denominator) "pnt".
9981 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9982 __isl_keep isl_vec *pnt)
9984 isl_int n, d;
9985 isl_ctx *ctx;
9986 isl_val *v;
9988 if (!aff || !pnt)
9989 return NULL;
9991 ctx = isl_vec_get_ctx(aff);
9992 isl_int_init(n);
9993 isl_int_init(d);
9994 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9995 isl_int_mul(d, aff->el[0], pnt->el[0]);
9996 v = isl_val_rat_from_isl_int(ctx, n, d);
9997 v = isl_val_normalize(v);
9998 isl_int_clear(n);
9999 isl_int_clear(d);
10001 return v;
10004 /* Check that the domain space of "aff" is equal to "space".
10006 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
10007 __isl_keep isl_space *space)
10009 isl_bool ok;
10011 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
10012 if (ok < 0)
10013 return isl_stat_error;
10014 if (!ok)
10015 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
10016 "incompatible spaces", return isl_stat_error);
10017 return isl_stat_ok;
10020 /* Evaluate the affine function "aff" in "pnt".
10022 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
10023 __isl_take isl_point *pnt)
10025 isl_bool is_void;
10026 isl_val *v;
10027 isl_local_space *ls;
10029 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
10030 goto error;
10031 is_void = isl_point_is_void(pnt);
10032 if (is_void < 0)
10033 goto error;
10034 if (is_void)
10035 return eval_void(aff, pnt);
10037 ls = isl_aff_get_domain_local_space(aff);
10038 pnt = isl_local_space_lift_point(ls, pnt);
10040 v = eval(aff->v, isl_point_peek_vec(pnt));
10042 isl_aff_free(aff);
10043 isl_point_free(pnt);
10045 return v;
10046 error:
10047 isl_aff_free(aff);
10048 isl_point_free(pnt);
10049 return NULL;