isl_multi_aff_substitute: use isl_multi_aff_size
[isl.git] / isl_aff.c
blob8268d40152e86b6c85252fe4054e7b7b979e46eb
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018,2020 Cerebras Systems
8 * Copyright 2021 Sven Verdoolaege
10 * Use of this software is governed by the MIT license
12 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
13 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
14 * 91893 Orsay, France
15 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
16 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
17 * B.P. 105 - 78153 Le Chesnay, France
18 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
21 #include <isl_ctx_private.h>
22 #include <isl_map_private.h>
23 #include <isl_union_map_private.h>
24 #include <isl_aff_private.h>
25 #include <isl_space_private.h>
26 #include <isl_local_space_private.h>
27 #include <isl_vec_private.h>
28 #include <isl_mat_private.h>
29 #include <isl_id_private.h>
30 #include <isl/constraint.h>
31 #include <isl_seq.h>
32 #include <isl/set.h>
33 #include <isl_val_private.h>
34 #include <isl_point_private.h>
35 #include <isl_config.h>
37 #undef EL_BASE
38 #define EL_BASE aff
40 #include <isl_list_templ.c>
41 #include <isl_list_read_templ.c>
43 #undef EL_BASE
44 #define EL_BASE pw_aff
46 #include <isl_list_templ.c>
47 #include <isl_list_read_templ.c>
49 #undef EL_BASE
50 #define EL_BASE pw_multi_aff
52 #include <isl_list_templ.c>
53 #include <isl_list_read_templ.c>
55 #undef EL_BASE
56 #define EL_BASE union_pw_aff
58 #include <isl_list_templ.c>
59 #include <isl_list_read_templ.c>
61 #undef EL_BASE
62 #define EL_BASE union_pw_multi_aff
64 #include <isl_list_templ.c>
66 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
67 __isl_take isl_vec *v)
69 isl_aff *aff;
71 if (!ls || !v)
72 goto error;
74 aff = isl_calloc_type(v->ctx, struct isl_aff);
75 if (!aff)
76 goto error;
78 aff->ref = 1;
79 aff->ls = ls;
80 aff->v = v;
82 return aff;
83 error:
84 isl_local_space_free(ls);
85 isl_vec_free(v);
86 return NULL;
89 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
91 isl_ctx *ctx;
92 isl_vec *v;
93 isl_size total;
95 if (!ls)
96 return NULL;
98 ctx = isl_local_space_get_ctx(ls);
99 if (!isl_local_space_divs_known(ls))
100 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
101 goto error);
102 if (!isl_local_space_is_set(ls))
103 isl_die(ctx, isl_error_invalid,
104 "domain of affine expression should be a set",
105 goto error);
107 total = isl_local_space_dim(ls, isl_dim_all);
108 if (total < 0)
109 goto error;
110 v = isl_vec_alloc(ctx, 1 + 1 + total);
111 return isl_aff_alloc_vec(ls, v);
112 error:
113 isl_local_space_free(ls);
114 return NULL;
117 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
119 if (!aff)
120 return NULL;
122 aff->ref++;
123 return aff;
126 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
128 if (!aff)
129 return NULL;
131 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
132 isl_vec_copy(aff->v));
135 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
137 if (!aff)
138 return NULL;
140 if (aff->ref == 1)
141 return aff;
142 aff->ref--;
143 return isl_aff_dup(aff);
146 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
148 isl_aff *aff;
150 aff = isl_aff_alloc(ls);
151 if (!aff)
152 return NULL;
154 isl_int_set_si(aff->v->el[0], 1);
155 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
157 return aff;
160 /* Return an affine expression that is equal to zero on domain space "space".
162 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
164 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
167 /* This function performs the same operation as isl_aff_zero_on_domain_space,
168 * but is considered as a function on an isl_space when exported.
170 __isl_give isl_aff *isl_space_zero_aff_on_domain(__isl_take isl_space *space)
172 return isl_aff_zero_on_domain_space(space);
175 /* Return a piecewise affine expression defined on the specified domain
176 * that is equal to zero.
178 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
180 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
183 /* Change "aff" into a NaN.
185 * Note that this function gets called from isl_aff_nan_on_domain,
186 * so "aff" may not have been initialized yet.
188 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
190 aff = isl_aff_cow(aff);
191 if (!aff)
192 return NULL;
194 aff->v = isl_vec_clr(aff->v);
195 if (!aff->v)
196 return isl_aff_free(aff);
198 return aff;
201 /* Return an affine expression defined on the specified domain
202 * that represents NaN.
204 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
206 isl_aff *aff;
208 aff = isl_aff_alloc(ls);
209 return isl_aff_set_nan(aff);
212 /* Return an affine expression defined on the specified domain space
213 * that represents NaN.
215 __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
217 return isl_aff_nan_on_domain(isl_local_space_from_space(space));
220 /* Return a piecewise affine expression defined on the specified domain space
221 * that represents NaN.
223 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
224 __isl_take isl_space *space)
226 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
229 /* Return a piecewise affine expression defined on the specified domain
230 * that represents NaN.
232 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
234 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
237 /* Return an affine expression that is equal to "val" on
238 * domain local space "ls".
240 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
241 __isl_take isl_val *val)
243 isl_aff *aff;
245 if (!ls || !val)
246 goto error;
247 if (!isl_val_is_rat(val))
248 isl_die(isl_val_get_ctx(val), isl_error_invalid,
249 "expecting rational value", goto error);
251 aff = isl_aff_alloc(isl_local_space_copy(ls));
252 if (!aff)
253 goto error;
255 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
256 isl_int_set(aff->v->el[1], val->n);
257 isl_int_set(aff->v->el[0], val->d);
259 isl_local_space_free(ls);
260 isl_val_free(val);
261 return aff;
262 error:
263 isl_local_space_free(ls);
264 isl_val_free(val);
265 return NULL;
268 /* Return an affine expression that is equal to "val" on domain space "space".
270 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
271 __isl_take isl_val *val)
273 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
276 /* Return an affine expression that is equal to the specified dimension
277 * in "ls".
279 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
280 enum isl_dim_type type, unsigned pos)
282 isl_space *space;
283 isl_aff *aff;
285 if (!ls)
286 return NULL;
288 space = isl_local_space_get_space(ls);
289 if (!space)
290 goto error;
291 if (isl_space_is_map(space))
292 isl_die(isl_space_get_ctx(space), isl_error_invalid,
293 "expecting (parameter) set space", goto error);
294 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
295 goto error;
297 isl_space_free(space);
298 aff = isl_aff_alloc(ls);
299 if (!aff)
300 return NULL;
302 pos += isl_local_space_offset(aff->ls, type);
304 isl_int_set_si(aff->v->el[0], 1);
305 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
306 isl_int_set_si(aff->v->el[1 + pos], 1);
308 return aff;
309 error:
310 isl_local_space_free(ls);
311 isl_space_free(space);
312 return NULL;
315 /* Return a piecewise affine expression that is equal to
316 * the specified dimension in "ls".
318 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
319 enum isl_dim_type type, unsigned pos)
321 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
324 /* Return an affine expression that is equal to the parameter
325 * in the domain space "space" with identifier "id".
327 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
328 __isl_take isl_space *space, __isl_take isl_id *id)
330 int pos;
331 isl_local_space *ls;
333 if (!space || !id)
334 goto error;
335 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
336 if (pos < 0)
337 isl_die(isl_space_get_ctx(space), isl_error_invalid,
338 "parameter not found in space", goto error);
339 isl_id_free(id);
340 ls = isl_local_space_from_space(space);
341 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
342 error:
343 isl_space_free(space);
344 isl_id_free(id);
345 return NULL;
348 /* This function performs the same operation as
349 * isl_aff_param_on_domain_space_id,
350 * but is considered as a function on an isl_space when exported.
352 __isl_give isl_aff *isl_space_param_aff_on_domain_id(
353 __isl_take isl_space *space, __isl_take isl_id *id)
355 return isl_aff_param_on_domain_space_id(space, id);
358 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
360 if (!aff)
361 return NULL;
363 if (--aff->ref > 0)
364 return NULL;
366 isl_local_space_free(aff->ls);
367 isl_vec_free(aff->v);
369 free(aff);
371 return NULL;
374 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
376 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
379 /* Return a hash value that digests "aff".
381 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
383 uint32_t hash, ls_hash, v_hash;
385 if (!aff)
386 return 0;
388 hash = isl_hash_init();
389 ls_hash = isl_local_space_get_hash(aff->ls);
390 isl_hash_hash(hash, ls_hash);
391 v_hash = isl_vec_get_hash(aff->v);
392 isl_hash_hash(hash, v_hash);
394 return hash;
397 /* Return the domain local space of "aff".
399 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
400 __isl_keep isl_aff *aff)
402 return aff ? aff->ls : NULL;
405 /* Return the number of variables of the given type in the domain of "aff".
407 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
409 isl_local_space *ls;
411 ls = isl_aff_peek_domain_local_space(aff);
412 return isl_local_space_dim(ls, type);
415 /* Externally, an isl_aff has a map space, but internally, the
416 * ls field corresponds to the domain of that space.
418 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
420 if (!aff)
421 return isl_size_error;
422 if (type == isl_dim_out)
423 return 1;
424 if (type == isl_dim_in)
425 type = isl_dim_set;
426 return isl_aff_domain_dim(aff, type);
429 /* Return the offset of the first coefficient of type "type" in
430 * the domain of "aff".
432 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
434 isl_local_space *ls;
436 ls = isl_aff_peek_domain_local_space(aff);
437 return isl_local_space_offset(ls, type);
440 /* Return the position of the dimension of the given type and name
441 * in "aff".
442 * Return -1 if no such dimension can be found.
444 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
445 const char *name)
447 if (!aff)
448 return -1;
449 if (type == isl_dim_out)
450 return -1;
451 if (type == isl_dim_in)
452 type = isl_dim_set;
453 return isl_local_space_find_dim_by_name(aff->ls, type, name);
456 /* Return the domain space of "aff".
458 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
460 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
463 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
465 return isl_space_copy(isl_aff_peek_domain_space(aff));
468 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
470 isl_space *space;
471 if (!aff)
472 return NULL;
473 space = isl_local_space_get_space(aff->ls);
474 space = isl_space_from_domain(space);
475 space = isl_space_add_dims(space, isl_dim_out, 1);
476 return space;
479 /* Return a copy of the domain space of "aff".
481 __isl_give isl_local_space *isl_aff_get_domain_local_space(
482 __isl_keep isl_aff *aff)
484 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
487 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
489 isl_local_space *ls;
490 if (!aff)
491 return NULL;
492 ls = isl_local_space_copy(aff->ls);
493 ls = isl_local_space_from_domain(ls);
494 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
495 return ls;
498 /* Return the local space of the domain of "aff".
499 * This may be either a copy or the local space itself
500 * if there is only one reference to "aff".
501 * This allows the local space to be modified inplace
502 * if both the expression and its local space have only a single reference.
503 * The caller is not allowed to modify "aff" between this call and
504 * a subsequent call to isl_aff_restore_domain_local_space.
505 * The only exception is that isl_aff_free can be called instead.
507 __isl_give isl_local_space *isl_aff_take_domain_local_space(
508 __isl_keep isl_aff *aff)
510 isl_local_space *ls;
512 if (!aff)
513 return NULL;
514 if (aff->ref != 1)
515 return isl_aff_get_domain_local_space(aff);
516 ls = aff->ls;
517 aff->ls = NULL;
518 return ls;
521 /* Set the local space of the domain of "aff" to "ls",
522 * where the local space of "aff" may be missing
523 * due to a preceding call to isl_aff_take_domain_local_space.
524 * However, in this case, "aff" only has a single reference and
525 * then the call to isl_aff_cow has no effect.
527 __isl_give isl_aff *isl_aff_restore_domain_local_space(
528 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
530 if (!aff || !ls)
531 goto error;
533 if (aff->ls == ls) {
534 isl_local_space_free(ls);
535 return aff;
538 aff = isl_aff_cow(aff);
539 if (!aff)
540 goto error;
541 isl_local_space_free(aff->ls);
542 aff->ls = ls;
544 return aff;
545 error:
546 isl_aff_free(aff);
547 isl_local_space_free(ls);
548 return NULL;
551 /* Externally, an isl_aff has a map space, but internally, the
552 * ls field corresponds to the domain of that space.
554 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
555 enum isl_dim_type type, unsigned pos)
557 if (!aff)
558 return NULL;
559 if (type == isl_dim_out)
560 return NULL;
561 if (type == isl_dim_in)
562 type = isl_dim_set;
563 return isl_local_space_get_dim_name(aff->ls, type, pos);
566 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
567 __isl_take isl_space *space)
569 aff = isl_aff_cow(aff);
570 if (!aff || !space)
571 goto error;
573 aff->ls = isl_local_space_reset_space(aff->ls, space);
574 if (!aff->ls)
575 return isl_aff_free(aff);
577 return aff;
578 error:
579 isl_aff_free(aff);
580 isl_space_free(space);
581 return NULL;
584 /* Reset the space of "aff". This function is called from isl_pw_templ.c
585 * and doesn't know if the space of an element object is represented
586 * directly or through its domain. It therefore passes along both.
588 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
589 __isl_take isl_space *space, __isl_take isl_space *domain)
591 isl_space_free(space);
592 return isl_aff_reset_domain_space(aff, domain);
595 /* Reorder the coefficients of the affine expression based
596 * on the given reordering.
597 * The reordering r is assumed to have been extended with the local
598 * variables.
600 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
601 __isl_take isl_reordering *r, int n_div)
603 isl_space *space;
604 isl_vec *res;
605 isl_size dim;
606 int i;
608 if (!vec || !r)
609 goto error;
611 space = isl_reordering_peek_space(r);
612 dim = isl_space_dim(space, isl_dim_all);
613 if (dim < 0)
614 goto error;
615 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
616 if (!res)
617 goto error;
618 isl_seq_cpy(res->el, vec->el, 2);
619 isl_seq_clr(res->el + 2, res->size - 2);
620 for (i = 0; i < r->len; ++i)
621 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
623 isl_reordering_free(r);
624 isl_vec_free(vec);
625 return res;
626 error:
627 isl_vec_free(vec);
628 isl_reordering_free(r);
629 return NULL;
632 /* Reorder the dimensions of the domain of "aff" according
633 * to the given reordering.
635 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
636 __isl_take isl_reordering *r)
638 aff = isl_aff_cow(aff);
639 if (!aff)
640 goto error;
642 r = isl_reordering_extend(r, aff->ls->div->n_row);
643 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
644 aff->ls->div->n_row);
645 aff->ls = isl_local_space_realign(aff->ls, r);
647 if (!aff->v || !aff->ls)
648 return isl_aff_free(aff);
650 return aff;
651 error:
652 isl_aff_free(aff);
653 isl_reordering_free(r);
654 return NULL;
657 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
658 __isl_take isl_space *model)
660 isl_bool equal_params;
662 if (!aff || !model)
663 goto error;
665 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
666 if (equal_params < 0)
667 goto error;
668 if (!equal_params) {
669 isl_reordering *exp;
671 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
672 exp = isl_reordering_extend_space(exp,
673 isl_aff_get_domain_space(aff));
674 aff = isl_aff_realign_domain(aff, exp);
677 isl_space_free(model);
678 return aff;
679 error:
680 isl_space_free(model);
681 isl_aff_free(aff);
682 return NULL;
685 #undef TYPE
686 #define TYPE isl_aff
687 #include "isl_unbind_params_templ.c"
689 /* Is "aff" obviously equal to zero?
691 * If the denominator is zero, then "aff" is not equal to zero.
693 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
695 int pos;
697 if (!aff)
698 return isl_bool_error;
700 if (isl_int_is_zero(aff->v->el[0]))
701 return isl_bool_false;
702 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
703 return isl_bool_ok(pos < 0);
706 /* Does "aff" represent NaN?
708 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
710 if (!aff)
711 return isl_bool_error;
713 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
716 /* Are "aff1" and "aff2" obviously equal?
718 * NaN is not equal to anything, not even to another NaN.
720 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
721 __isl_keep isl_aff *aff2)
723 isl_bool equal;
725 if (!aff1 || !aff2)
726 return isl_bool_error;
728 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
729 return isl_bool_false;
731 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
732 if (equal < 0 || !equal)
733 return equal;
735 return isl_vec_is_equal(aff1->v, aff2->v);
738 /* Return the common denominator of "aff" in "v".
740 * We cannot return anything meaningful in case of a NaN.
742 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
744 if (!aff)
745 return isl_stat_error;
746 if (isl_aff_is_nan(aff))
747 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
748 "cannot get denominator of NaN", return isl_stat_error);
749 isl_int_set(*v, aff->v->el[0]);
750 return isl_stat_ok;
753 /* Return the common denominator of "aff".
755 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
757 isl_ctx *ctx;
759 if (!aff)
760 return NULL;
762 ctx = isl_aff_get_ctx(aff);
763 if (isl_aff_is_nan(aff))
764 return isl_val_nan(ctx);
765 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
768 /* Return the constant term of "aff".
770 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
772 isl_ctx *ctx;
773 isl_val *v;
775 if (!aff)
776 return NULL;
778 ctx = isl_aff_get_ctx(aff);
779 if (isl_aff_is_nan(aff))
780 return isl_val_nan(ctx);
781 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
782 return isl_val_normalize(v);
785 /* Return the coefficient of the variable of type "type" at position "pos"
786 * of "aff".
788 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
789 enum isl_dim_type type, int pos)
791 isl_ctx *ctx;
792 isl_val *v;
794 if (!aff)
795 return NULL;
797 ctx = isl_aff_get_ctx(aff);
798 if (type == isl_dim_out)
799 isl_die(ctx, isl_error_invalid,
800 "output/set dimension does not have a coefficient",
801 return NULL);
802 if (type == isl_dim_in)
803 type = isl_dim_set;
805 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
806 return NULL;
808 if (isl_aff_is_nan(aff))
809 return isl_val_nan(ctx);
810 pos += isl_local_space_offset(aff->ls, type);
811 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
812 return isl_val_normalize(v);
815 /* Return the sign of the coefficient of the variable of type "type"
816 * at position "pos" of "aff".
818 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
819 int pos)
821 isl_ctx *ctx;
823 if (!aff)
824 return 0;
826 ctx = isl_aff_get_ctx(aff);
827 if (type == isl_dim_out)
828 isl_die(ctx, isl_error_invalid,
829 "output/set dimension does not have a coefficient",
830 return 0);
831 if (type == isl_dim_in)
832 type = isl_dim_set;
834 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
835 return 0;
837 pos += isl_local_space_offset(aff->ls, type);
838 return isl_int_sgn(aff->v->el[1 + pos]);
841 /* Replace the numerator of the constant term of "aff" by "v".
843 * A NaN is unaffected by this operation.
845 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
847 if (!aff)
848 return NULL;
849 if (isl_aff_is_nan(aff))
850 return aff;
851 aff = isl_aff_cow(aff);
852 if (!aff)
853 return NULL;
855 aff->v = isl_vec_cow(aff->v);
856 if (!aff->v)
857 return isl_aff_free(aff);
859 isl_int_set(aff->v->el[1], v);
861 return aff;
864 /* Replace the constant term of "aff" by "v".
866 * A NaN is unaffected by this operation.
868 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
869 __isl_take isl_val *v)
871 if (!aff || !v)
872 goto error;
874 if (isl_aff_is_nan(aff)) {
875 isl_val_free(v);
876 return aff;
879 if (!isl_val_is_rat(v))
880 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
881 "expecting rational value", goto error);
883 if (isl_int_eq(aff->v->el[1], v->n) &&
884 isl_int_eq(aff->v->el[0], v->d)) {
885 isl_val_free(v);
886 return aff;
889 aff = isl_aff_cow(aff);
890 if (!aff)
891 goto error;
892 aff->v = isl_vec_cow(aff->v);
893 if (!aff->v)
894 goto error;
896 if (isl_int_eq(aff->v->el[0], v->d)) {
897 isl_int_set(aff->v->el[1], v->n);
898 } else if (isl_int_is_one(v->d)) {
899 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
900 } else {
901 isl_seq_scale(aff->v->el + 1,
902 aff->v->el + 1, v->d, aff->v->size - 1);
903 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
904 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
905 aff->v = isl_vec_normalize(aff->v);
906 if (!aff->v)
907 goto error;
910 isl_val_free(v);
911 return aff;
912 error:
913 isl_aff_free(aff);
914 isl_val_free(v);
915 return NULL;
918 /* Add "v" to the constant term of "aff".
920 * A NaN is unaffected by this operation.
922 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
924 if (isl_int_is_zero(v))
925 return aff;
927 if (!aff)
928 return NULL;
929 if (isl_aff_is_nan(aff))
930 return aff;
931 aff = isl_aff_cow(aff);
932 if (!aff)
933 return NULL;
935 aff->v = isl_vec_cow(aff->v);
936 if (!aff->v)
937 return isl_aff_free(aff);
939 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
941 return aff;
944 /* Add "v" to the constant term of "aff",
945 * in case "aff" is a rational expression.
947 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
948 __isl_take isl_val *v)
950 aff = isl_aff_cow(aff);
951 if (!aff)
952 goto error;
954 aff->v = isl_vec_cow(aff->v);
955 if (!aff->v)
956 goto error;
958 if (isl_int_is_one(v->d)) {
959 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
960 } else if (isl_int_eq(aff->v->el[0], v->d)) {
961 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
962 aff->v = isl_vec_normalize(aff->v);
963 if (!aff->v)
964 goto error;
965 } else {
966 isl_seq_scale(aff->v->el + 1,
967 aff->v->el + 1, v->d, aff->v->size - 1);
968 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
969 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
970 aff->v = isl_vec_normalize(aff->v);
971 if (!aff->v)
972 goto error;
975 isl_val_free(v);
976 return aff;
977 error:
978 isl_aff_free(aff);
979 isl_val_free(v);
980 return NULL;
983 /* Return the first argument and free the second.
985 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
986 __isl_take isl_val *v)
988 isl_val_free(v);
989 return aff;
992 /* Replace the first argument by NaN and free the second argument.
994 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
995 __isl_take isl_val *v)
997 isl_val_free(v);
998 return isl_aff_set_nan(aff);
1001 /* Add "v" to the constant term of "aff".
1003 * A NaN is unaffected by this operation.
1004 * Conversely, adding a NaN turns "aff" into a NaN.
1006 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
1007 __isl_take isl_val *v)
1009 isl_bool is_nan, is_zero, is_rat;
1011 is_nan = isl_aff_is_nan(aff);
1012 is_zero = isl_val_is_zero(v);
1013 if (is_nan < 0 || is_zero < 0)
1014 goto error;
1015 if (is_nan || is_zero)
1016 return pick_free(aff, v);
1018 is_nan = isl_val_is_nan(v);
1019 is_rat = isl_val_is_rat(v);
1020 if (is_nan < 0 || is_rat < 0)
1021 goto error;
1022 if (is_nan)
1023 return set_nan_free_val(aff, v);
1024 if (!is_rat)
1025 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1026 "expecting rational value or NaN", goto error);
1028 return isl_aff_add_rat_constant_val(aff, v);
1029 error:
1030 isl_aff_free(aff);
1031 isl_val_free(v);
1032 return NULL;
1035 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1037 isl_int t;
1039 isl_int_init(t);
1040 isl_int_set_si(t, v);
1041 aff = isl_aff_add_constant(aff, t);
1042 isl_int_clear(t);
1044 return aff;
1047 /* Add "v" to the numerator of the constant term of "aff".
1049 * A NaN is unaffected by this operation.
1051 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
1053 if (isl_int_is_zero(v))
1054 return aff;
1056 if (!aff)
1057 return NULL;
1058 if (isl_aff_is_nan(aff))
1059 return aff;
1060 aff = isl_aff_cow(aff);
1061 if (!aff)
1062 return NULL;
1064 aff->v = isl_vec_cow(aff->v);
1065 if (!aff->v)
1066 return isl_aff_free(aff);
1068 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1070 return aff;
1073 /* Add "v" to the numerator of the constant term of "aff".
1075 * A NaN is unaffected by this operation.
1077 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1079 isl_int t;
1081 if (v == 0)
1082 return aff;
1084 isl_int_init(t);
1085 isl_int_set_si(t, v);
1086 aff = isl_aff_add_constant_num(aff, t);
1087 isl_int_clear(t);
1089 return aff;
1092 /* Replace the numerator of the constant term of "aff" by "v".
1094 * A NaN is unaffected by this operation.
1096 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1098 if (!aff)
1099 return NULL;
1100 if (isl_aff_is_nan(aff))
1101 return aff;
1102 aff = isl_aff_cow(aff);
1103 if (!aff)
1104 return NULL;
1106 aff->v = isl_vec_cow(aff->v);
1107 if (!aff->v)
1108 return isl_aff_free(aff);
1110 isl_int_set_si(aff->v->el[1], v);
1112 return aff;
1115 /* Replace the numerator of the coefficient of the variable of type "type"
1116 * at position "pos" of "aff" by "v".
1118 * A NaN is unaffected by this operation.
1120 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1121 enum isl_dim_type type, int pos, isl_int v)
1123 if (!aff)
1124 return NULL;
1126 if (type == isl_dim_out)
1127 isl_die(aff->v->ctx, isl_error_invalid,
1128 "output/set dimension does not have a coefficient",
1129 return isl_aff_free(aff));
1130 if (type == isl_dim_in)
1131 type = isl_dim_set;
1133 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1134 return isl_aff_free(aff);
1136 if (isl_aff_is_nan(aff))
1137 return aff;
1138 aff = isl_aff_cow(aff);
1139 if (!aff)
1140 return NULL;
1142 aff->v = isl_vec_cow(aff->v);
1143 if (!aff->v)
1144 return isl_aff_free(aff);
1146 pos += isl_local_space_offset(aff->ls, type);
1147 isl_int_set(aff->v->el[1 + pos], v);
1149 return aff;
1152 /* Replace the numerator of the coefficient of the variable of type "type"
1153 * at position "pos" of "aff" by "v".
1155 * A NaN is unaffected by this operation.
1157 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1158 enum isl_dim_type type, int pos, int v)
1160 if (!aff)
1161 return NULL;
1163 if (type == isl_dim_out)
1164 isl_die(aff->v->ctx, isl_error_invalid,
1165 "output/set dimension does not have a coefficient",
1166 return isl_aff_free(aff));
1167 if (type == isl_dim_in)
1168 type = isl_dim_set;
1170 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1171 return isl_aff_free(aff);
1173 if (isl_aff_is_nan(aff))
1174 return aff;
1175 pos += isl_local_space_offset(aff->ls, type);
1176 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1177 return aff;
1179 aff = isl_aff_cow(aff);
1180 if (!aff)
1181 return NULL;
1183 aff->v = isl_vec_cow(aff->v);
1184 if (!aff->v)
1185 return isl_aff_free(aff);
1187 isl_int_set_si(aff->v->el[1 + pos], v);
1189 return aff;
1192 /* Replace the coefficient of the variable of type "type" at position "pos"
1193 * of "aff" by "v".
1195 * A NaN is unaffected by this operation.
1197 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1198 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1200 if (!aff || !v)
1201 goto error;
1203 if (type == isl_dim_out)
1204 isl_die(aff->v->ctx, isl_error_invalid,
1205 "output/set dimension does not have a coefficient",
1206 goto error);
1207 if (type == isl_dim_in)
1208 type = isl_dim_set;
1210 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1211 return isl_aff_free(aff);
1213 if (isl_aff_is_nan(aff)) {
1214 isl_val_free(v);
1215 return aff;
1217 if (!isl_val_is_rat(v))
1218 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1219 "expecting rational value", goto error);
1221 pos += isl_local_space_offset(aff->ls, type);
1222 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1223 isl_int_eq(aff->v->el[0], v->d)) {
1224 isl_val_free(v);
1225 return aff;
1228 aff = isl_aff_cow(aff);
1229 if (!aff)
1230 goto error;
1231 aff->v = isl_vec_cow(aff->v);
1232 if (!aff->v)
1233 goto error;
1235 if (isl_int_eq(aff->v->el[0], v->d)) {
1236 isl_int_set(aff->v->el[1 + pos], v->n);
1237 } else if (isl_int_is_one(v->d)) {
1238 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1239 } else {
1240 isl_seq_scale(aff->v->el + 1,
1241 aff->v->el + 1, v->d, aff->v->size - 1);
1242 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1243 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1244 aff->v = isl_vec_normalize(aff->v);
1245 if (!aff->v)
1246 goto error;
1249 isl_val_free(v);
1250 return aff;
1251 error:
1252 isl_aff_free(aff);
1253 isl_val_free(v);
1254 return NULL;
1257 /* Add "v" to the coefficient of the variable of type "type"
1258 * at position "pos" of "aff".
1260 * A NaN is unaffected by this operation.
1262 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1263 enum isl_dim_type type, int pos, isl_int v)
1265 if (!aff)
1266 return NULL;
1268 if (type == isl_dim_out)
1269 isl_die(aff->v->ctx, isl_error_invalid,
1270 "output/set dimension does not have a coefficient",
1271 return isl_aff_free(aff));
1272 if (type == isl_dim_in)
1273 type = isl_dim_set;
1275 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1276 return isl_aff_free(aff);
1278 if (isl_aff_is_nan(aff))
1279 return aff;
1280 aff = isl_aff_cow(aff);
1281 if (!aff)
1282 return NULL;
1284 aff->v = isl_vec_cow(aff->v);
1285 if (!aff->v)
1286 return isl_aff_free(aff);
1288 pos += isl_local_space_offset(aff->ls, type);
1289 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1291 return aff;
1294 /* Add "v" to the coefficient of the variable of type "type"
1295 * at position "pos" of "aff".
1297 * A NaN is unaffected by this operation.
1299 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1300 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1302 if (!aff || !v)
1303 goto error;
1305 if (isl_val_is_zero(v)) {
1306 isl_val_free(v);
1307 return aff;
1310 if (type == isl_dim_out)
1311 isl_die(aff->v->ctx, isl_error_invalid,
1312 "output/set dimension does not have a coefficient",
1313 goto error);
1314 if (type == isl_dim_in)
1315 type = isl_dim_set;
1317 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1318 goto error;
1320 if (isl_aff_is_nan(aff)) {
1321 isl_val_free(v);
1322 return aff;
1324 if (!isl_val_is_rat(v))
1325 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1326 "expecting rational value", goto error);
1328 aff = isl_aff_cow(aff);
1329 if (!aff)
1330 goto error;
1332 aff->v = isl_vec_cow(aff->v);
1333 if (!aff->v)
1334 goto error;
1336 pos += isl_local_space_offset(aff->ls, type);
1337 if (isl_int_is_one(v->d)) {
1338 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1339 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1340 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1341 aff->v = isl_vec_normalize(aff->v);
1342 if (!aff->v)
1343 goto error;
1344 } else {
1345 isl_seq_scale(aff->v->el + 1,
1346 aff->v->el + 1, v->d, aff->v->size - 1);
1347 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1348 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1349 aff->v = isl_vec_normalize(aff->v);
1350 if (!aff->v)
1351 goto error;
1354 isl_val_free(v);
1355 return aff;
1356 error:
1357 isl_aff_free(aff);
1358 isl_val_free(v);
1359 return NULL;
1362 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1363 enum isl_dim_type type, int pos, int v)
1365 isl_int t;
1367 isl_int_init(t);
1368 isl_int_set_si(t, v);
1369 aff = isl_aff_add_coefficient(aff, type, pos, t);
1370 isl_int_clear(t);
1372 return aff;
1375 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1377 if (!aff)
1378 return NULL;
1380 return isl_local_space_get_div(aff->ls, pos);
1383 /* Return the negation of "aff".
1385 * As a special case, -NaN = NaN.
1387 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1389 if (!aff)
1390 return NULL;
1391 if (isl_aff_is_nan(aff))
1392 return aff;
1393 aff = isl_aff_cow(aff);
1394 if (!aff)
1395 return NULL;
1396 aff->v = isl_vec_cow(aff->v);
1397 if (!aff->v)
1398 return isl_aff_free(aff);
1400 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1402 return aff;
1405 /* Remove divs from the local space that do not appear in the affine
1406 * expression.
1407 * We currently only remove divs at the end.
1408 * Some intermediate divs may also not appear directly in the affine
1409 * expression, but we would also need to check that no other divs are
1410 * defined in terms of them.
1412 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1414 int pos;
1415 isl_size off;
1416 isl_size n;
1418 n = isl_aff_domain_dim(aff, isl_dim_div);
1419 off = isl_aff_domain_offset(aff, isl_dim_div);
1420 if (n < 0 || off < 0)
1421 return isl_aff_free(aff);
1423 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1424 if (pos == n)
1425 return aff;
1427 aff = isl_aff_cow(aff);
1428 if (!aff)
1429 return NULL;
1431 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1432 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1433 if (!aff->ls || !aff->v)
1434 return isl_aff_free(aff);
1436 return aff;
1439 /* Look for any divs in the aff->ls with a denominator equal to one
1440 * and plug them into the affine expression and any subsequent divs
1441 * that may reference the div.
1443 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1445 int i;
1446 isl_size n;
1447 int len;
1448 isl_int v;
1449 isl_vec *vec;
1450 isl_local_space *ls;
1451 isl_size off;
1453 n = isl_aff_domain_dim(aff, isl_dim_div);
1454 off = isl_aff_domain_offset(aff, isl_dim_div);
1455 if (n < 0 || off < 0)
1456 return isl_aff_free(aff);
1457 len = aff->v->size;
1458 for (i = 0; i < n; ++i) {
1459 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1460 continue;
1461 ls = isl_local_space_copy(aff->ls);
1462 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1463 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1464 vec = isl_vec_copy(aff->v);
1465 vec = isl_vec_cow(vec);
1466 if (!ls || !vec)
1467 goto error;
1469 isl_int_init(v);
1471 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1472 len, len, v);
1474 isl_int_clear(v);
1476 isl_vec_free(aff->v);
1477 aff->v = vec;
1478 isl_local_space_free(aff->ls);
1479 aff->ls = ls;
1482 return aff;
1483 error:
1484 isl_vec_free(vec);
1485 isl_local_space_free(ls);
1486 return isl_aff_free(aff);
1489 /* Look for any divs j that appear with a unit coefficient inside
1490 * the definitions of other divs i and plug them into the definitions
1491 * of the divs i.
1493 * In particular, an expression of the form
1495 * floor((f(..) + floor(g(..)/n))/m)
1497 * is simplified to
1499 * floor((n * f(..) + g(..))/(n * m))
1501 * This simplification is correct because we can move the expression
1502 * f(..) into the inner floor in the original expression to obtain
1504 * floor(floor((n * f(..) + g(..))/n)/m)
1506 * from which we can derive the simplified expression.
1508 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1510 int i, j;
1511 isl_size n;
1512 isl_size off;
1514 n = isl_aff_domain_dim(aff, isl_dim_div);
1515 off = isl_aff_domain_offset(aff, isl_dim_div);
1516 if (n < 0 || off < 0)
1517 return isl_aff_free(aff);
1518 for (i = 1; i < n; ++i) {
1519 for (j = 0; j < i; ++j) {
1520 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1521 continue;
1522 aff->ls = isl_local_space_substitute_seq(aff->ls,
1523 isl_dim_div, j, aff->ls->div->row[j],
1524 aff->v->size, i, 1);
1525 if (!aff->ls)
1526 return isl_aff_free(aff);
1530 return aff;
1533 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1535 * Even though this function is only called on isl_affs with a single
1536 * reference, we are careful to only change aff->v and aff->ls together.
1538 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1540 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1541 isl_local_space *ls;
1542 isl_vec *v;
1544 if (off < 0)
1545 return isl_aff_free(aff);
1547 ls = isl_local_space_copy(aff->ls);
1548 ls = isl_local_space_swap_div(ls, a, b);
1549 v = isl_vec_copy(aff->v);
1550 v = isl_vec_cow(v);
1551 if (!ls || !v)
1552 goto error;
1554 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1555 isl_vec_free(aff->v);
1556 aff->v = v;
1557 isl_local_space_free(aff->ls);
1558 aff->ls = ls;
1560 return aff;
1561 error:
1562 isl_vec_free(v);
1563 isl_local_space_free(ls);
1564 return isl_aff_free(aff);
1567 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1569 * We currently do not actually remove div "b", but simply add its
1570 * coefficient to that of "a" and then zero it out.
1572 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1574 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1576 if (off < 0)
1577 return isl_aff_free(aff);
1579 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1580 return aff;
1582 aff->v = isl_vec_cow(aff->v);
1583 if (!aff->v)
1584 return isl_aff_free(aff);
1586 isl_int_add(aff->v->el[1 + off + a],
1587 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1588 isl_int_set_si(aff->v->el[1 + off + b], 0);
1590 return aff;
1593 /* Sort the divs in the local space of "aff" according to
1594 * the comparison function "cmp_row" in isl_local_space.c,
1595 * combining the coefficients of identical divs.
1597 * Reordering divs does not change the semantics of "aff",
1598 * so there is no need to call isl_aff_cow.
1599 * Moreover, this function is currently only called on isl_affs
1600 * with a single reference.
1602 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1604 isl_size n;
1605 int i, j;
1607 n = isl_aff_dim(aff, isl_dim_div);
1608 if (n < 0)
1609 return isl_aff_free(aff);
1610 for (i = 1; i < n; ++i) {
1611 for (j = i - 1; j >= 0; --j) {
1612 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1613 if (cmp < 0)
1614 break;
1615 if (cmp == 0)
1616 aff = merge_divs(aff, j, j + 1);
1617 else
1618 aff = swap_div(aff, j, j + 1);
1619 if (!aff)
1620 return NULL;
1624 return aff;
1627 /* Normalize the representation of "aff".
1629 * This function should only be called on "new" isl_affs, i.e.,
1630 * with only a single reference. We therefore do not need to
1631 * worry about affecting other instances.
1633 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1635 if (!aff)
1636 return NULL;
1637 aff->v = isl_vec_normalize(aff->v);
1638 if (!aff->v)
1639 return isl_aff_free(aff);
1640 aff = plug_in_integral_divs(aff);
1641 aff = plug_in_unit_divs(aff);
1642 aff = sort_divs(aff);
1643 aff = isl_aff_remove_unused_divs(aff);
1644 return aff;
1647 /* Given f, return floor(f).
1648 * If f is an integer expression, then just return f.
1649 * If f is a constant, then return the constant floor(f).
1650 * Otherwise, if f = g/m, write g = q m + r,
1651 * create a new div d = [r/m] and return the expression q + d.
1652 * The coefficients in r are taken to lie between -m/2 and m/2.
1654 * reduce_div_coefficients performs the same normalization.
1656 * As a special case, floor(NaN) = NaN.
1658 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1660 int i;
1661 int size;
1662 isl_ctx *ctx;
1663 isl_vec *div;
1665 if (!aff)
1666 return NULL;
1668 if (isl_aff_is_nan(aff))
1669 return aff;
1670 if (isl_int_is_one(aff->v->el[0]))
1671 return aff;
1673 aff = isl_aff_cow(aff);
1674 if (!aff)
1675 return NULL;
1677 aff->v = isl_vec_cow(aff->v);
1678 if (!aff->v)
1679 return isl_aff_free(aff);
1681 if (isl_aff_is_cst(aff)) {
1682 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1683 isl_int_set_si(aff->v->el[0], 1);
1684 return aff;
1687 div = isl_vec_copy(aff->v);
1688 div = isl_vec_cow(div);
1689 if (!div)
1690 return isl_aff_free(aff);
1692 ctx = isl_aff_get_ctx(aff);
1693 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1694 for (i = 1; i < aff->v->size; ++i) {
1695 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1696 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1697 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1698 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1699 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1703 aff->ls = isl_local_space_add_div(aff->ls, div);
1704 if (!aff->ls)
1705 return isl_aff_free(aff);
1707 size = aff->v->size;
1708 aff->v = isl_vec_extend(aff->v, size + 1);
1709 if (!aff->v)
1710 return isl_aff_free(aff);
1711 isl_int_set_si(aff->v->el[0], 1);
1712 isl_int_set_si(aff->v->el[size], 1);
1714 aff = isl_aff_normalize(aff);
1716 return aff;
1719 /* Compute
1721 * aff mod m = aff - m * floor(aff/m)
1723 * with m an integer value.
1725 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1726 __isl_take isl_val *m)
1728 isl_aff *res;
1730 if (!aff || !m)
1731 goto error;
1733 if (!isl_val_is_int(m))
1734 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1735 "expecting integer modulo", goto error);
1737 res = isl_aff_copy(aff);
1738 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1739 aff = isl_aff_floor(aff);
1740 aff = isl_aff_scale_val(aff, m);
1741 res = isl_aff_sub(res, aff);
1743 return res;
1744 error:
1745 isl_aff_free(aff);
1746 isl_val_free(m);
1747 return NULL;
1750 /* Compute
1752 * pwaff mod m = pwaff - m * floor(pwaff/m)
1754 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1756 isl_pw_aff *res;
1758 res = isl_pw_aff_copy(pwaff);
1759 pwaff = isl_pw_aff_scale_down(pwaff, m);
1760 pwaff = isl_pw_aff_floor(pwaff);
1761 pwaff = isl_pw_aff_scale(pwaff, m);
1762 res = isl_pw_aff_sub(res, pwaff);
1764 return res;
1767 /* Compute
1769 * pa mod m = pa - m * floor(pa/m)
1771 * with m an integer value.
1773 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1774 __isl_take isl_val *m)
1776 if (!pa || !m)
1777 goto error;
1778 if (!isl_val_is_int(m))
1779 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1780 "expecting integer modulo", goto error);
1781 pa = isl_pw_aff_mod(pa, m->n);
1782 isl_val_free(m);
1783 return pa;
1784 error:
1785 isl_pw_aff_free(pa);
1786 isl_val_free(m);
1787 return NULL;
1790 /* Given f, return ceil(f).
1791 * If f is an integer expression, then just return f.
1792 * Otherwise, let f be the expression
1794 * e/m
1796 * then return
1798 * floor((e + m - 1)/m)
1800 * As a special case, ceil(NaN) = NaN.
1802 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1804 if (!aff)
1805 return NULL;
1807 if (isl_aff_is_nan(aff))
1808 return aff;
1809 if (isl_int_is_one(aff->v->el[0]))
1810 return aff;
1812 aff = isl_aff_cow(aff);
1813 if (!aff)
1814 return NULL;
1815 aff->v = isl_vec_cow(aff->v);
1816 if (!aff->v)
1817 return isl_aff_free(aff);
1819 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1820 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1821 aff = isl_aff_floor(aff);
1823 return aff;
1826 /* Apply the expansion computed by isl_merge_divs.
1827 * The expansion itself is given by "exp" while the resulting
1828 * list of divs is given by "div".
1830 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1831 __isl_take isl_mat *div, int *exp)
1833 isl_size old_n_div;
1834 isl_size new_n_div;
1835 isl_size offset;
1837 aff = isl_aff_cow(aff);
1839 offset = isl_aff_domain_offset(aff, isl_dim_div);
1840 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1841 new_n_div = isl_mat_rows(div);
1842 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1843 goto error;
1845 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1846 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1847 if (!aff->v || !aff->ls)
1848 return isl_aff_free(aff);
1849 return aff;
1850 error:
1851 isl_aff_free(aff);
1852 isl_mat_free(div);
1853 return NULL;
1856 /* Add two affine expressions that live in the same local space.
1858 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1859 __isl_take isl_aff *aff2)
1861 isl_int gcd, f;
1863 aff1 = isl_aff_cow(aff1);
1864 if (!aff1 || !aff2)
1865 goto error;
1867 aff1->v = isl_vec_cow(aff1->v);
1868 if (!aff1->v)
1869 goto error;
1871 isl_int_init(gcd);
1872 isl_int_init(f);
1873 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1874 isl_int_divexact(f, aff2->v->el[0], gcd);
1875 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1876 isl_int_divexact(f, aff1->v->el[0], gcd);
1877 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1878 isl_int_divexact(f, aff2->v->el[0], gcd);
1879 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1880 isl_int_clear(f);
1881 isl_int_clear(gcd);
1883 isl_aff_free(aff2);
1884 aff1 = isl_aff_normalize(aff1);
1885 return aff1;
1886 error:
1887 isl_aff_free(aff1);
1888 isl_aff_free(aff2);
1889 return NULL;
1892 /* Replace one of the arguments by a NaN and free the other one.
1894 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1895 __isl_take isl_aff *aff2)
1897 isl_aff_free(aff2);
1898 return isl_aff_set_nan(aff1);
1901 /* Return the sum of "aff1" and "aff2".
1903 * If either of the two is NaN, then the result is NaN.
1905 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1906 __isl_take isl_aff *aff2)
1908 isl_ctx *ctx;
1909 int *exp1 = NULL;
1910 int *exp2 = NULL;
1911 isl_mat *div;
1912 isl_size n_div1, n_div2;
1914 if (!aff1 || !aff2)
1915 goto error;
1917 ctx = isl_aff_get_ctx(aff1);
1918 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1919 isl_die(ctx, isl_error_invalid,
1920 "spaces don't match", goto error);
1922 if (isl_aff_is_nan(aff1)) {
1923 isl_aff_free(aff2);
1924 return aff1;
1926 if (isl_aff_is_nan(aff2)) {
1927 isl_aff_free(aff1);
1928 return aff2;
1931 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1932 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1933 if (n_div1 < 0 || n_div2 < 0)
1934 goto error;
1935 if (n_div1 == 0 && n_div2 == 0)
1936 return add_expanded(aff1, aff2);
1938 exp1 = isl_alloc_array(ctx, int, n_div1);
1939 exp2 = isl_alloc_array(ctx, int, n_div2);
1940 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1941 goto error;
1943 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1944 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1945 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1946 free(exp1);
1947 free(exp2);
1949 return add_expanded(aff1, aff2);
1950 error:
1951 free(exp1);
1952 free(exp2);
1953 isl_aff_free(aff1);
1954 isl_aff_free(aff2);
1955 return NULL;
1958 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1959 __isl_take isl_aff *aff2)
1961 return isl_aff_add(aff1, isl_aff_neg(aff2));
1964 /* Return the result of scaling "aff" by a factor of "f".
1966 * As a special case, f * NaN = NaN.
1968 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1970 isl_int gcd;
1972 if (!aff)
1973 return NULL;
1974 if (isl_aff_is_nan(aff))
1975 return aff;
1977 if (isl_int_is_one(f))
1978 return aff;
1980 aff = isl_aff_cow(aff);
1981 if (!aff)
1982 return NULL;
1983 aff->v = isl_vec_cow(aff->v);
1984 if (!aff->v)
1985 return isl_aff_free(aff);
1987 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1988 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1989 return aff;
1992 isl_int_init(gcd);
1993 isl_int_gcd(gcd, aff->v->el[0], f);
1994 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1995 isl_int_divexact(gcd, f, gcd);
1996 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1997 isl_int_clear(gcd);
1999 return aff;
2002 /* Multiple "aff" by "v".
2004 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
2005 __isl_take isl_val *v)
2007 if (!aff || !v)
2008 goto error;
2010 if (isl_val_is_one(v)) {
2011 isl_val_free(v);
2012 return aff;
2015 if (!isl_val_is_rat(v))
2016 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2017 "expecting rational factor", goto error);
2019 aff = isl_aff_scale(aff, v->n);
2020 aff = isl_aff_scale_down(aff, v->d);
2022 isl_val_free(v);
2023 return aff;
2024 error:
2025 isl_aff_free(aff);
2026 isl_val_free(v);
2027 return NULL;
2030 /* Return the result of scaling "aff" down by a factor of "f".
2032 * As a special case, NaN/f = NaN.
2034 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2036 isl_int gcd;
2038 if (!aff)
2039 return NULL;
2040 if (isl_aff_is_nan(aff))
2041 return aff;
2043 if (isl_int_is_one(f))
2044 return aff;
2046 aff = isl_aff_cow(aff);
2047 if (!aff)
2048 return NULL;
2050 if (isl_int_is_zero(f))
2051 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2052 "cannot scale down by zero", return isl_aff_free(aff));
2054 aff->v = isl_vec_cow(aff->v);
2055 if (!aff->v)
2056 return isl_aff_free(aff);
2058 isl_int_init(gcd);
2059 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2060 isl_int_gcd(gcd, gcd, f);
2061 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2062 isl_int_divexact(gcd, f, gcd);
2063 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2064 isl_int_clear(gcd);
2066 return aff;
2069 /* Divide "aff" by "v".
2071 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2072 __isl_take isl_val *v)
2074 if (!aff || !v)
2075 goto error;
2077 if (isl_val_is_one(v)) {
2078 isl_val_free(v);
2079 return aff;
2082 if (!isl_val_is_rat(v))
2083 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2084 "expecting rational factor", goto error);
2085 if (!isl_val_is_pos(v))
2086 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2087 "factor needs to be positive", goto error);
2089 aff = isl_aff_scale(aff, v->d);
2090 aff = isl_aff_scale_down(aff, v->n);
2092 isl_val_free(v);
2093 return aff;
2094 error:
2095 isl_aff_free(aff);
2096 isl_val_free(v);
2097 return NULL;
2100 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2102 isl_int v;
2104 if (f == 1)
2105 return aff;
2107 isl_int_init(v);
2108 isl_int_set_ui(v, f);
2109 aff = isl_aff_scale_down(aff, v);
2110 isl_int_clear(v);
2112 return aff;
2115 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2116 enum isl_dim_type type, unsigned pos, const char *s)
2118 aff = isl_aff_cow(aff);
2119 if (!aff)
2120 return NULL;
2121 if (type == isl_dim_out)
2122 isl_die(aff->v->ctx, isl_error_invalid,
2123 "cannot set name of output/set dimension",
2124 return isl_aff_free(aff));
2125 if (type == isl_dim_in)
2126 type = isl_dim_set;
2127 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2128 if (!aff->ls)
2129 return isl_aff_free(aff);
2131 return aff;
2134 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2135 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2137 aff = isl_aff_cow(aff);
2138 if (!aff)
2139 goto error;
2140 if (type == isl_dim_out)
2141 isl_die(aff->v->ctx, isl_error_invalid,
2142 "cannot set name of output/set dimension",
2143 goto error);
2144 if (type == isl_dim_in)
2145 type = isl_dim_set;
2146 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2147 if (!aff->ls)
2148 return isl_aff_free(aff);
2150 return aff;
2151 error:
2152 isl_id_free(id);
2153 isl_aff_free(aff);
2154 return NULL;
2157 /* Replace the identifier of the input tuple of "aff" by "id".
2158 * type is currently required to be equal to isl_dim_in
2160 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2161 enum isl_dim_type type, __isl_take isl_id *id)
2163 aff = isl_aff_cow(aff);
2164 if (!aff)
2165 goto error;
2166 if (type != isl_dim_in)
2167 isl_die(aff->v->ctx, isl_error_invalid,
2168 "cannot only set id of input tuple", goto error);
2169 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2170 if (!aff->ls)
2171 return isl_aff_free(aff);
2173 return aff;
2174 error:
2175 isl_id_free(id);
2176 isl_aff_free(aff);
2177 return NULL;
2180 /* Exploit the equalities in "eq" to simplify the affine expression
2181 * and the expressions of the integer divisions in the local space.
2182 * The integer divisions in this local space are assumed to appear
2183 * as regular dimensions in "eq".
2185 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2186 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2188 int i, j;
2189 unsigned o_div;
2190 unsigned n_div;
2192 if (!eq)
2193 goto error;
2194 if (eq->n_eq == 0) {
2195 isl_basic_set_free(eq);
2196 return aff;
2199 aff = isl_aff_cow(aff);
2200 if (!aff)
2201 goto error;
2203 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2204 isl_basic_set_copy(eq));
2205 aff->v = isl_vec_cow(aff->v);
2206 if (!aff->ls || !aff->v)
2207 goto error;
2209 o_div = isl_basic_set_offset(eq, isl_dim_div);
2210 n_div = eq->n_div;
2211 for (i = 0; i < eq->n_eq; ++i) {
2212 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2213 if (j < 0 || j == 0 || j >= o_div)
2214 continue;
2216 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2217 &aff->v->el[0]);
2220 isl_basic_set_free(eq);
2221 aff = isl_aff_normalize(aff);
2222 return aff;
2223 error:
2224 isl_basic_set_free(eq);
2225 isl_aff_free(aff);
2226 return NULL;
2229 /* Exploit the equalities in "eq" to simplify the affine expression
2230 * and the expressions of the integer divisions in the local space.
2232 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2233 __isl_take isl_basic_set *eq)
2235 isl_size n_div;
2237 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2238 if (n_div < 0)
2239 goto error;
2240 if (n_div > 0)
2241 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2242 return isl_aff_substitute_equalities_lifted(aff, eq);
2243 error:
2244 isl_basic_set_free(eq);
2245 isl_aff_free(aff);
2246 return NULL;
2249 /* Look for equalities among the variables shared by context and aff
2250 * and the integer divisions of aff, if any.
2251 * The equalities are then used to eliminate coefficients and/or integer
2252 * divisions from aff.
2254 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2255 __isl_take isl_set *context)
2257 isl_local_space *ls;
2258 isl_basic_set *hull;
2260 ls = isl_aff_get_domain_local_space(aff);
2261 context = isl_local_space_lift_set(ls, context);
2263 hull = isl_set_affine_hull(context);
2264 return isl_aff_substitute_equalities_lifted(aff, hull);
2267 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2268 __isl_take isl_set *context)
2270 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2271 dom_context = isl_set_intersect_params(dom_context, context);
2272 return isl_aff_gist(aff, dom_context);
2275 /* Return a basic set containing those elements in the space
2276 * of aff where it is positive. "rational" should not be set.
2278 * If "aff" is NaN, then it is not positive.
2280 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2281 int rational, void *user)
2283 isl_constraint *ineq;
2284 isl_basic_set *bset;
2285 isl_val *c;
2287 if (!aff)
2288 return NULL;
2289 if (isl_aff_is_nan(aff)) {
2290 isl_space *space = isl_aff_get_domain_space(aff);
2291 isl_aff_free(aff);
2292 return isl_basic_set_empty(space);
2294 if (rational)
2295 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2296 "rational sets not supported", goto error);
2298 ineq = isl_inequality_from_aff(aff);
2299 c = isl_constraint_get_constant_val(ineq);
2300 c = isl_val_sub_ui(c, 1);
2301 ineq = isl_constraint_set_constant_val(ineq, c);
2303 bset = isl_basic_set_from_constraint(ineq);
2304 bset = isl_basic_set_simplify(bset);
2305 return bset;
2306 error:
2307 isl_aff_free(aff);
2308 return NULL;
2311 /* Return a basic set containing those elements in the space
2312 * of aff where it is non-negative.
2313 * If "rational" is set, then return a rational basic set.
2315 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2317 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2318 __isl_take isl_aff *aff, int rational, void *user)
2320 isl_constraint *ineq;
2321 isl_basic_set *bset;
2323 if (!aff)
2324 return NULL;
2325 if (isl_aff_is_nan(aff)) {
2326 isl_space *space = isl_aff_get_domain_space(aff);
2327 isl_aff_free(aff);
2328 return isl_basic_set_empty(space);
2331 ineq = isl_inequality_from_aff(aff);
2333 bset = isl_basic_set_from_constraint(ineq);
2334 if (rational)
2335 bset = isl_basic_set_set_rational(bset);
2336 bset = isl_basic_set_simplify(bset);
2337 return bset;
2340 /* Return a basic set containing those elements in the space
2341 * of aff where it is non-negative.
2343 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2345 return aff_nonneg_basic_set(aff, 0, NULL);
2348 /* Return a basic set containing those elements in the domain space
2349 * of "aff" where it is positive.
2351 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2353 aff = isl_aff_add_constant_num_si(aff, -1);
2354 return isl_aff_nonneg_basic_set(aff);
2357 /* Return a basic set containing those elements in the domain space
2358 * of aff where it is negative.
2360 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2362 aff = isl_aff_neg(aff);
2363 return isl_aff_pos_basic_set(aff);
2366 /* Return a basic set containing those elements in the space
2367 * of aff where it is zero.
2368 * If "rational" is set, then return a rational basic set.
2370 * If "aff" is NaN, then it is not zero.
2372 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2373 int rational, void *user)
2375 isl_constraint *ineq;
2376 isl_basic_set *bset;
2378 if (!aff)
2379 return NULL;
2380 if (isl_aff_is_nan(aff)) {
2381 isl_space *space = isl_aff_get_domain_space(aff);
2382 isl_aff_free(aff);
2383 return isl_basic_set_empty(space);
2386 ineq = isl_equality_from_aff(aff);
2388 bset = isl_basic_set_from_constraint(ineq);
2389 if (rational)
2390 bset = isl_basic_set_set_rational(bset);
2391 bset = isl_basic_set_simplify(bset);
2392 return bset;
2395 /* Return a basic set containing those elements in the space
2396 * of aff where it is zero.
2398 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2400 return aff_zero_basic_set(aff, 0, NULL);
2403 /* Return a basic set containing those elements in the shared space
2404 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2406 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2407 __isl_take isl_aff *aff2)
2409 aff1 = isl_aff_sub(aff1, aff2);
2411 return isl_aff_nonneg_basic_set(aff1);
2414 /* Return a basic set containing those elements in the shared domain space
2415 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2417 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2418 __isl_take isl_aff *aff2)
2420 aff1 = isl_aff_sub(aff1, aff2);
2422 return isl_aff_pos_basic_set(aff1);
2425 /* Return a set containing those elements in the shared space
2426 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2428 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2429 __isl_take isl_aff *aff2)
2431 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2434 /* Return a set containing those elements in the shared domain space
2435 * of aff1 and aff2 where aff1 is greater than aff2.
2437 * If either of the two inputs is NaN, then the result is empty,
2438 * as comparisons with NaN always return false.
2440 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2441 __isl_take isl_aff *aff2)
2443 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2446 /* Return a basic set containing those elements in the shared space
2447 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2449 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2450 __isl_take isl_aff *aff2)
2452 return isl_aff_ge_basic_set(aff2, aff1);
2455 /* Return a basic set containing those elements in the shared domain space
2456 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2458 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2459 __isl_take isl_aff *aff2)
2461 return isl_aff_gt_basic_set(aff2, aff1);
2464 /* Return a set containing those elements in the shared space
2465 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2467 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2468 __isl_take isl_aff *aff2)
2470 return isl_aff_ge_set(aff2, aff1);
2473 /* Return a set containing those elements in the shared domain space
2474 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2476 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2477 __isl_take isl_aff *aff2)
2479 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2482 /* Return a basic set containing those elements in the shared space
2483 * of aff1 and aff2 where aff1 and aff2 are equal.
2485 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2486 __isl_take isl_aff *aff2)
2488 aff1 = isl_aff_sub(aff1, aff2);
2490 return isl_aff_zero_basic_set(aff1);
2493 /* Return a set containing those elements in the shared space
2494 * of aff1 and aff2 where aff1 and aff2 are equal.
2496 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2497 __isl_take isl_aff *aff2)
2499 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2502 /* Return a set containing those elements in the shared domain space
2503 * of aff1 and aff2 where aff1 and aff2 are not equal.
2505 * If either of the two inputs is NaN, then the result is empty,
2506 * as comparisons with NaN always return false.
2508 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2509 __isl_take isl_aff *aff2)
2511 isl_set *set_lt, *set_gt;
2513 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2514 isl_aff_copy(aff2));
2515 set_gt = isl_aff_gt_set(aff1, aff2);
2516 return isl_set_union_disjoint(set_lt, set_gt);
2519 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2520 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2522 aff1 = isl_aff_add(aff1, aff2);
2523 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2524 return aff1;
2527 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2529 if (!aff)
2530 return isl_bool_error;
2532 return isl_bool_false;
2535 #undef TYPE
2536 #define TYPE isl_aff
2537 static
2538 #include "check_type_range_templ.c"
2540 /* Check whether the given affine expression has non-zero coefficient
2541 * for any dimension in the given range or if any of these dimensions
2542 * appear with non-zero coefficients in any of the integer divisions
2543 * involved in the affine expression.
2545 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2546 enum isl_dim_type type, unsigned first, unsigned n)
2548 int i;
2549 int *active = NULL;
2550 isl_bool involves = isl_bool_false;
2552 if (!aff)
2553 return isl_bool_error;
2554 if (n == 0)
2555 return isl_bool_false;
2556 if (isl_aff_check_range(aff, type, first, n) < 0)
2557 return isl_bool_error;
2559 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2560 if (!active)
2561 goto error;
2563 first += isl_local_space_offset(aff->ls, type) - 1;
2564 for (i = 0; i < n; ++i)
2565 if (active[first + i]) {
2566 involves = isl_bool_true;
2567 break;
2570 free(active);
2572 return involves;
2573 error:
2574 free(active);
2575 return isl_bool_error;
2578 /* Does "aff" involve any local variables, i.e., integer divisions?
2580 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2582 isl_size n;
2584 n = isl_aff_dim(aff, isl_dim_div);
2585 if (n < 0)
2586 return isl_bool_error;
2587 return isl_bool_ok(n > 0);
2590 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2591 enum isl_dim_type type, unsigned first, unsigned n)
2593 isl_ctx *ctx;
2595 if (!aff)
2596 return NULL;
2597 if (type == isl_dim_out)
2598 isl_die(aff->v->ctx, isl_error_invalid,
2599 "cannot drop output/set dimension",
2600 return isl_aff_free(aff));
2601 if (type == isl_dim_in)
2602 type = isl_dim_set;
2603 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2604 return aff;
2606 ctx = isl_aff_get_ctx(aff);
2607 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2608 return isl_aff_free(aff);
2610 aff = isl_aff_cow(aff);
2611 if (!aff)
2612 return NULL;
2614 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2615 if (!aff->ls)
2616 return isl_aff_free(aff);
2618 first += 1 + isl_local_space_offset(aff->ls, type);
2619 aff->v = isl_vec_drop_els(aff->v, first, n);
2620 if (!aff->v)
2621 return isl_aff_free(aff);
2623 return aff;
2626 /* Is the domain of "aff" a product?
2628 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2630 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2633 #undef TYPE
2634 #define TYPE isl_aff
2635 #include <isl_domain_factor_templ.c>
2637 /* Project the domain of the affine expression onto its parameter space.
2638 * The affine expression may not involve any of the domain dimensions.
2640 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2642 isl_space *space;
2643 isl_size n;
2645 n = isl_aff_dim(aff, isl_dim_in);
2646 if (n < 0)
2647 return isl_aff_free(aff);
2648 aff = isl_aff_drop_domain(aff, 0, n);
2649 space = isl_aff_get_domain_space(aff);
2650 space = isl_space_params(space);
2651 aff = isl_aff_reset_domain_space(aff, space);
2652 return aff;
2655 /* Convert an affine expression defined over a parameter domain
2656 * into one that is defined over a zero-dimensional set.
2658 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2660 isl_local_space *ls;
2662 ls = isl_aff_take_domain_local_space(aff);
2663 ls = isl_local_space_set_from_params(ls);
2664 aff = isl_aff_restore_domain_local_space(aff, ls);
2666 return aff;
2669 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2670 enum isl_dim_type type, unsigned first, unsigned n)
2672 isl_ctx *ctx;
2674 if (!aff)
2675 return NULL;
2676 if (type == isl_dim_out)
2677 isl_die(aff->v->ctx, isl_error_invalid,
2678 "cannot insert output/set dimensions",
2679 return isl_aff_free(aff));
2680 if (type == isl_dim_in)
2681 type = isl_dim_set;
2682 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2683 return aff;
2685 ctx = isl_aff_get_ctx(aff);
2686 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2687 return isl_aff_free(aff);
2689 aff = isl_aff_cow(aff);
2690 if (!aff)
2691 return NULL;
2693 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2694 if (!aff->ls)
2695 return isl_aff_free(aff);
2697 first += 1 + isl_local_space_offset(aff->ls, type);
2698 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2699 if (!aff->v)
2700 return isl_aff_free(aff);
2702 return aff;
2705 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2706 enum isl_dim_type type, unsigned n)
2708 isl_size pos;
2710 pos = isl_aff_dim(aff, type);
2711 if (pos < 0)
2712 return isl_aff_free(aff);
2714 return isl_aff_insert_dims(aff, type, pos, n);
2717 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2718 * to dimensions of "dst_type" at "dst_pos".
2720 * We only support moving input dimensions to parameters and vice versa.
2722 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2723 enum isl_dim_type dst_type, unsigned dst_pos,
2724 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2726 unsigned g_dst_pos;
2727 unsigned g_src_pos;
2728 isl_size src_off, dst_off;
2730 if (!aff)
2731 return NULL;
2732 if (n == 0 &&
2733 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2734 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2735 return aff;
2737 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2738 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2739 "cannot move output/set dimension",
2740 return isl_aff_free(aff));
2741 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2742 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2743 "cannot move divs", return isl_aff_free(aff));
2744 if (dst_type == isl_dim_in)
2745 dst_type = isl_dim_set;
2746 if (src_type == isl_dim_in)
2747 src_type = isl_dim_set;
2749 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2750 return isl_aff_free(aff);
2751 if (dst_type == src_type)
2752 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2753 "moving dims within the same type not supported",
2754 return isl_aff_free(aff));
2756 aff = isl_aff_cow(aff);
2757 src_off = isl_aff_domain_offset(aff, src_type);
2758 dst_off = isl_aff_domain_offset(aff, dst_type);
2759 if (src_off < 0 || dst_off < 0)
2760 return isl_aff_free(aff);
2762 g_src_pos = 1 + src_off + src_pos;
2763 g_dst_pos = 1 + dst_off + dst_pos;
2764 if (dst_type > src_type)
2765 g_dst_pos -= n;
2767 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2768 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2769 src_type, src_pos, n);
2770 if (!aff->v || !aff->ls)
2771 return isl_aff_free(aff);
2773 aff = sort_divs(aff);
2775 return aff;
2778 /* Return a zero isl_aff in the given space.
2780 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2781 * interface over all piecewise types.
2783 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2785 isl_local_space *ls;
2787 ls = isl_local_space_from_space(isl_space_domain(space));
2788 return isl_aff_zero_on_domain(ls);
2791 #define isl_aff_involves_nan isl_aff_is_nan
2793 #undef PW
2794 #define PW isl_pw_aff
2795 #undef BASE
2796 #define BASE aff
2797 #undef EL_IS_ZERO
2798 #define EL_IS_ZERO is_empty
2799 #undef ZERO
2800 #define ZERO empty
2801 #undef IS_ZERO
2802 #define IS_ZERO is_empty
2803 #undef FIELD
2804 #define FIELD aff
2805 #undef DEFAULT_IS_ZERO
2806 #define DEFAULT_IS_ZERO 0
2808 #include <isl_pw_templ.c>
2809 #include <isl_pw_un_op_templ.c>
2810 #include <isl_pw_add_constant_val_templ.c>
2811 #include <isl_pw_bind_domain_templ.c>
2812 #include <isl_pw_eval.c>
2813 #include <isl_pw_hash.c>
2814 #include <isl_pw_insert_dims_templ.c>
2815 #include <isl_pw_insert_domain_templ.c>
2816 #include <isl_pw_move_dims_templ.c>
2817 #include <isl_pw_neg_templ.c>
2818 #include <isl_pw_pullback_templ.c>
2819 #include <isl_pw_sub_templ.c>
2820 #include <isl_pw_union_opt.c>
2822 #undef BASE
2823 #define BASE pw_aff
2825 #include <isl_union_single.c>
2826 #include <isl_union_neg.c>
2828 #undef BASE
2829 #define BASE aff
2831 #include <isl_union_pw_templ.c>
2833 /* Compute a piecewise quasi-affine expression with a domain that
2834 * is the union of those of pwaff1 and pwaff2 and such that on each
2835 * cell, the quasi-affine expression is the maximum of those of pwaff1
2836 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2837 * cell, then the associated expression is the defined one.
2839 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2840 __isl_take isl_pw_aff *pwaff2)
2842 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2843 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2846 /* Compute a piecewise quasi-affine expression with a domain that
2847 * is the union of those of pwaff1 and pwaff2 and such that on each
2848 * cell, the quasi-affine expression is the minimum of those of pwaff1
2849 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2850 * cell, then the associated expression is the defined one.
2852 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2853 __isl_take isl_pw_aff *pwaff2)
2855 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2856 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2859 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2860 __isl_take isl_pw_aff *pwaff2, int max)
2862 if (max)
2863 return isl_pw_aff_union_max(pwaff1, pwaff2);
2864 else
2865 return isl_pw_aff_union_min(pwaff1, pwaff2);
2868 /* Is the domain of "pa" a product?
2870 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2872 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2875 #undef TYPE
2876 #define TYPE isl_pw_aff
2877 #include <isl_domain_factor_templ.c>
2879 /* Return a set containing those elements in the domain
2880 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2881 * does not satisfy "fn" (if complement is 1).
2883 * The pieces with a NaN never belong to the result since
2884 * NaN does not satisfy any property.
2886 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2887 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2888 void *user),
2889 int complement, void *user)
2891 int i;
2892 isl_set *set;
2894 if (!pwaff)
2895 return NULL;
2897 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2899 for (i = 0; i < pwaff->n; ++i) {
2900 isl_basic_set *bset;
2901 isl_set *set_i, *locus;
2902 isl_bool rational;
2904 if (isl_aff_is_nan(pwaff->p[i].aff))
2905 continue;
2907 rational = isl_set_has_rational(pwaff->p[i].set);
2908 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2909 locus = isl_set_from_basic_set(bset);
2910 set_i = isl_set_copy(pwaff->p[i].set);
2911 if (complement)
2912 set_i = isl_set_subtract(set_i, locus);
2913 else
2914 set_i = isl_set_intersect(set_i, locus);
2915 set = isl_set_union_disjoint(set, set_i);
2918 isl_pw_aff_free(pwaff);
2920 return set;
2923 /* Return a set containing those elements in the domain
2924 * of "pa" where it is positive.
2926 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2928 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2931 /* Return a set containing those elements in the domain
2932 * of pwaff where it is non-negative.
2934 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2936 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2939 /* Return a set containing those elements in the domain
2940 * of pwaff where it is zero.
2942 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2944 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2947 /* Return a set containing those elements in the domain
2948 * of pwaff where it is not zero.
2950 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2952 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2955 /* Bind the affine function "aff" to the parameter "id",
2956 * returning the elements in the domain where the affine expression
2957 * is equal to the parameter.
2959 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2960 __isl_take isl_id *id)
2962 isl_space *space;
2963 isl_aff *aff_id;
2965 space = isl_aff_get_domain_space(aff);
2966 space = isl_space_add_param_id(space, isl_id_copy(id));
2968 aff = isl_aff_align_params(aff, isl_space_copy(space));
2969 aff_id = isl_aff_param_on_domain_space_id(space, id);
2971 return isl_aff_eq_basic_set(aff, aff_id);
2974 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2975 * "rational" should not be set.
2977 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2978 int rational, void *user)
2980 isl_id *id = user;
2982 if (!aff)
2983 return NULL;
2984 if (rational)
2985 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2986 "rational binding not supported", goto error);
2987 return isl_aff_bind_id(aff, isl_id_copy(id));
2988 error:
2989 isl_aff_free(aff);
2990 return NULL;
2993 /* Bind the piecewise affine function "pa" to the parameter "id",
2994 * returning the elements in the domain where the expression
2995 * is equal to the parameter.
2997 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2998 __isl_take isl_id *id)
3000 isl_set *bound;
3002 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
3003 isl_id_free(id);
3005 return bound;
3008 /* Return a set containing those elements in the shared domain
3009 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3011 * We compute the difference on the shared domain and then construct
3012 * the set of values where this difference is non-negative.
3013 * If strict is set, we first subtract 1 from the difference.
3014 * If equal is set, we only return the elements where pwaff1 and pwaff2
3015 * are equal.
3017 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3018 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3020 isl_set *set1, *set2;
3022 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3023 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3024 set1 = isl_set_intersect(set1, set2);
3025 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3026 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3027 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3029 if (strict) {
3030 isl_space *space = isl_set_get_space(set1);
3031 isl_aff *aff;
3032 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3033 aff = isl_aff_add_constant_si(aff, -1);
3034 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3035 } else
3036 isl_set_free(set1);
3038 if (equal)
3039 return isl_pw_aff_zero_set(pwaff1);
3040 return isl_pw_aff_nonneg_set(pwaff1);
3043 /* Return a set containing those elements in the shared domain
3044 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3046 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3047 __isl_take isl_pw_aff *pwaff2)
3049 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3050 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3053 /* Return a set containing those elements in the shared domain
3054 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3056 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3057 __isl_take isl_pw_aff *pwaff2)
3059 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3060 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3063 /* Return a set containing those elements in the shared domain
3064 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3066 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3067 __isl_take isl_pw_aff *pwaff2)
3069 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3070 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3073 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3074 __isl_take isl_pw_aff *pwaff2)
3076 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3079 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3080 __isl_take isl_pw_aff *pwaff2)
3082 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3085 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3086 * where the function values are ordered in the same way as "order",
3087 * which returns a set in the shared domain of its two arguments.
3089 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3090 * We first pull back the two functions such that they are defined on
3091 * the domain [A -> B]. Then we apply "order", resulting in a set
3092 * in the space [A -> B]. Finally, we unwrap this set to obtain
3093 * a map in the space A -> B.
3095 static __isl_give isl_map *isl_pw_aff_order_map(
3096 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3097 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3098 __isl_take isl_pw_aff *pa2))
3100 isl_space *space1, *space2;
3101 isl_multi_aff *ma;
3102 isl_set *set;
3104 isl_pw_aff_align_params_bin(&pa1, &pa2);
3105 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3106 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3107 space1 = isl_space_map_from_domain_and_range(space1, space2);
3108 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3109 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3110 ma = isl_multi_aff_range_map(space1);
3111 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3112 set = order(pa1, pa2);
3114 return isl_set_unwrap(set);
3117 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3118 * where the function values are equal.
3120 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3121 __isl_take isl_pw_aff *pa2)
3123 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3126 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3127 * where the function value of "pa1" is less than or equal to
3128 * the function value of "pa2".
3130 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3131 __isl_take isl_pw_aff *pa2)
3133 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3136 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3137 * where the function value of "pa1" is less than the function value of "pa2".
3139 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3140 __isl_take isl_pw_aff *pa2)
3142 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3145 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3146 * where the function value of "pa1" is greater than or equal to
3147 * the function value of "pa2".
3149 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3150 __isl_take isl_pw_aff *pa2)
3152 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3155 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3156 * where the function value of "pa1" is greater than the function value
3157 * of "pa2".
3159 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3160 __isl_take isl_pw_aff *pa2)
3162 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3165 /* Return a set containing those elements in the shared domain
3166 * of the elements of list1 and list2 where each element in list1
3167 * has the relation specified by "fn" with each element in list2.
3169 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3170 __isl_take isl_pw_aff_list *list2,
3171 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3172 __isl_take isl_pw_aff *pwaff2))
3174 int i, j;
3175 isl_ctx *ctx;
3176 isl_set *set;
3178 if (!list1 || !list2)
3179 goto error;
3181 ctx = isl_pw_aff_list_get_ctx(list1);
3182 if (list1->n < 1 || list2->n < 1)
3183 isl_die(ctx, isl_error_invalid,
3184 "list should contain at least one element", goto error);
3186 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3187 for (i = 0; i < list1->n; ++i)
3188 for (j = 0; j < list2->n; ++j) {
3189 isl_set *set_ij;
3191 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3192 isl_pw_aff_copy(list2->p[j]));
3193 set = isl_set_intersect(set, set_ij);
3196 isl_pw_aff_list_free(list1);
3197 isl_pw_aff_list_free(list2);
3198 return set;
3199 error:
3200 isl_pw_aff_list_free(list1);
3201 isl_pw_aff_list_free(list2);
3202 return NULL;
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 equal to each element in list2.
3209 __isl_give isl_set *isl_pw_aff_list_eq_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_eq_set);
3215 __isl_give isl_set *isl_pw_aff_list_ne_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_ne_set);
3221 /* Return a set containing those elements in the shared domain
3222 * of the elements of list1 and list2 where each element in list1
3223 * is less than or equal to each element in list2.
3225 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3226 __isl_take isl_pw_aff_list *list2)
3228 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3231 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3232 __isl_take isl_pw_aff_list *list2)
3234 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3237 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3238 __isl_take isl_pw_aff_list *list2)
3240 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3243 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3244 __isl_take isl_pw_aff_list *list2)
3246 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3250 /* Return a set containing those elements in the shared domain
3251 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3253 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3254 __isl_take isl_pw_aff *pwaff2)
3256 isl_set *set_lt, *set_gt;
3258 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3259 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3260 isl_pw_aff_copy(pwaff2));
3261 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3262 return isl_set_union_disjoint(set_lt, set_gt);
3265 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3266 isl_int v)
3268 int i;
3270 if (isl_int_is_one(v))
3271 return pwaff;
3272 if (!isl_int_is_pos(v))
3273 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3274 "factor needs to be positive",
3275 return isl_pw_aff_free(pwaff));
3276 pwaff = isl_pw_aff_cow(pwaff);
3277 if (!pwaff)
3278 return NULL;
3279 if (pwaff->n == 0)
3280 return pwaff;
3282 for (i = 0; i < pwaff->n; ++i) {
3283 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3284 if (!pwaff->p[i].aff)
3285 return isl_pw_aff_free(pwaff);
3288 return pwaff;
3291 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3293 return isl_pw_aff_un_op(pwaff, &isl_aff_floor);
3296 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3298 return isl_pw_aff_un_op(pwaff, &isl_aff_ceil);
3301 /* Assuming that "cond1" and "cond2" are disjoint,
3302 * return an affine expression that is equal to pwaff1 on cond1
3303 * and to pwaff2 on cond2.
3305 static __isl_give isl_pw_aff *isl_pw_aff_select(
3306 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3307 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3309 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3310 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3312 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3315 /* Return an affine expression that is equal to pwaff_true for elements
3316 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3317 * is zero.
3318 * That is, return cond ? pwaff_true : pwaff_false;
3320 * If "cond" involves and NaN, then we conservatively return a NaN
3321 * on its entire domain. In principle, we could consider the pieces
3322 * where it is NaN separately from those where it is not.
3324 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3325 * then only use the domain of "cond" to restrict the domain.
3327 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3328 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3330 isl_set *cond_true, *cond_false;
3331 isl_bool equal;
3333 if (!cond)
3334 goto error;
3335 if (isl_pw_aff_involves_nan(cond)) {
3336 isl_space *space = isl_pw_aff_get_domain_space(cond);
3337 isl_local_space *ls = isl_local_space_from_space(space);
3338 isl_pw_aff_free(cond);
3339 isl_pw_aff_free(pwaff_true);
3340 isl_pw_aff_free(pwaff_false);
3341 return isl_pw_aff_nan_on_domain(ls);
3344 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3345 isl_pw_aff_get_space(pwaff_false));
3346 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3347 isl_pw_aff_get_space(pwaff_true));
3348 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3349 if (equal < 0)
3350 goto error;
3351 if (equal) {
3352 isl_set *dom;
3354 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3355 isl_pw_aff_free(pwaff_false);
3356 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3359 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3360 cond_false = isl_pw_aff_zero_set(cond);
3361 return isl_pw_aff_select(cond_true, pwaff_true,
3362 cond_false, pwaff_false);
3363 error:
3364 isl_pw_aff_free(cond);
3365 isl_pw_aff_free(pwaff_true);
3366 isl_pw_aff_free(pwaff_false);
3367 return NULL;
3370 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3372 int pos;
3374 if (!aff)
3375 return isl_bool_error;
3377 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3378 return isl_bool_ok(pos == -1);
3381 /* Check whether pwaff is a piecewise constant.
3383 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3385 int i;
3387 if (!pwaff)
3388 return isl_bool_error;
3390 for (i = 0; i < pwaff->n; ++i) {
3391 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3392 if (is_cst < 0 || !is_cst)
3393 return is_cst;
3396 return isl_bool_true;
3399 /* Return the product of "aff1" and "aff2".
3401 * If either of the two is NaN, then the result is NaN.
3403 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3405 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3406 __isl_take isl_aff *aff2)
3408 if (!aff1 || !aff2)
3409 goto error;
3411 if (isl_aff_is_nan(aff1)) {
3412 isl_aff_free(aff2);
3413 return aff1;
3415 if (isl_aff_is_nan(aff2)) {
3416 isl_aff_free(aff1);
3417 return aff2;
3420 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3421 return isl_aff_mul(aff2, aff1);
3423 if (!isl_aff_is_cst(aff2))
3424 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3425 "at least one affine expression should be constant",
3426 goto error);
3428 aff1 = isl_aff_cow(aff1);
3429 if (!aff1 || !aff2)
3430 goto error;
3432 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3433 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3435 isl_aff_free(aff2);
3436 return aff1;
3437 error:
3438 isl_aff_free(aff1);
3439 isl_aff_free(aff2);
3440 return NULL;
3443 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3445 * If either of the two is NaN, then the result is NaN.
3446 * A division by zero also results in NaN.
3448 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3449 __isl_take isl_aff *aff2)
3451 isl_bool is_cst, is_zero;
3452 int neg;
3454 if (!aff1 || !aff2)
3455 goto error;
3457 if (isl_aff_is_nan(aff1)) {
3458 isl_aff_free(aff2);
3459 return aff1;
3461 if (isl_aff_is_nan(aff2)) {
3462 isl_aff_free(aff1);
3463 return aff2;
3466 is_cst = isl_aff_is_cst(aff2);
3467 if (is_cst < 0)
3468 goto error;
3469 if (!is_cst)
3470 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3471 "second argument should be a constant", goto error);
3472 is_zero = isl_aff_plain_is_zero(aff2);
3473 if (is_zero < 0)
3474 goto error;
3475 if (is_zero)
3476 return set_nan_free(aff1, aff2);
3478 neg = isl_int_is_neg(aff2->v->el[1]);
3479 if (neg) {
3480 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3481 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3484 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3485 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3487 if (neg) {
3488 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3489 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3492 isl_aff_free(aff2);
3493 return aff1;
3494 error:
3495 isl_aff_free(aff1);
3496 isl_aff_free(aff2);
3497 return NULL;
3500 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3501 __isl_take isl_pw_aff *pwaff2)
3503 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3504 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3507 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3508 __isl_take isl_pw_aff *pwaff2)
3510 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3513 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3514 __isl_take isl_pw_aff *pwaff2)
3516 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3517 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3520 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3522 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3523 __isl_take isl_pw_aff *pa2)
3525 int is_cst;
3527 is_cst = isl_pw_aff_is_cst(pa2);
3528 if (is_cst < 0)
3529 goto error;
3530 if (!is_cst)
3531 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3532 "second argument should be a piecewise constant",
3533 goto error);
3534 isl_pw_aff_align_params_bin(&pa1, &pa2);
3535 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3536 error:
3537 isl_pw_aff_free(pa1);
3538 isl_pw_aff_free(pa2);
3539 return NULL;
3542 /* Compute the quotient of the integer division of "pa1" by "pa2"
3543 * with rounding towards zero.
3544 * "pa2" is assumed to be a piecewise constant.
3546 * In particular, return
3548 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3551 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3552 __isl_take isl_pw_aff *pa2)
3554 int is_cst;
3555 isl_set *cond;
3556 isl_pw_aff *f, *c;
3558 is_cst = isl_pw_aff_is_cst(pa2);
3559 if (is_cst < 0)
3560 goto error;
3561 if (!is_cst)
3562 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3563 "second argument should be a piecewise constant",
3564 goto error);
3566 pa1 = isl_pw_aff_div(pa1, pa2);
3568 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3569 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3570 c = isl_pw_aff_ceil(pa1);
3571 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3572 error:
3573 isl_pw_aff_free(pa1);
3574 isl_pw_aff_free(pa2);
3575 return NULL;
3578 /* Compute the remainder of the integer division of "pa1" by "pa2"
3579 * with rounding towards zero.
3580 * "pa2" is assumed to be a piecewise constant.
3582 * In particular, return
3584 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3587 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3588 __isl_take isl_pw_aff *pa2)
3590 int is_cst;
3591 isl_pw_aff *res;
3593 is_cst = isl_pw_aff_is_cst(pa2);
3594 if (is_cst < 0)
3595 goto error;
3596 if (!is_cst)
3597 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3598 "second argument should be a piecewise constant",
3599 goto error);
3600 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3601 res = isl_pw_aff_mul(pa2, res);
3602 res = isl_pw_aff_sub(pa1, res);
3603 return res;
3604 error:
3605 isl_pw_aff_free(pa1);
3606 isl_pw_aff_free(pa2);
3607 return NULL;
3610 /* Does either of "pa1" or "pa2" involve any NaN2?
3612 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3613 __isl_keep isl_pw_aff *pa2)
3615 isl_bool has_nan;
3617 has_nan = isl_pw_aff_involves_nan(pa1);
3618 if (has_nan < 0 || has_nan)
3619 return has_nan;
3620 return isl_pw_aff_involves_nan(pa2);
3623 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3624 * by a NaN on their shared domain.
3626 * In principle, the result could be refined to only being NaN
3627 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3629 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3630 __isl_take isl_pw_aff *pa2)
3632 isl_local_space *ls;
3633 isl_set *dom;
3634 isl_pw_aff *pa;
3636 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3637 ls = isl_local_space_from_space(isl_set_get_space(dom));
3638 pa = isl_pw_aff_nan_on_domain(ls);
3639 pa = isl_pw_aff_intersect_domain(pa, dom);
3641 return pa;
3644 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3645 __isl_take isl_pw_aff *pwaff2)
3647 isl_set *le;
3648 isl_set *dom;
3650 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3651 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3652 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3653 isl_pw_aff_copy(pwaff2));
3654 dom = isl_set_subtract(dom, isl_set_copy(le));
3655 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3658 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3659 __isl_take isl_pw_aff *pwaff2)
3661 isl_set *ge;
3662 isl_set *dom;
3664 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3665 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3666 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3667 isl_pw_aff_copy(pwaff2));
3668 dom = isl_set_subtract(dom, isl_set_copy(ge));
3669 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3672 /* Return an expression for the minimum (if "max" is not set) or
3673 * the maximum (if "max" is set) of "pa1" and "pa2".
3674 * If either expression involves any NaN, then return a NaN
3675 * on the shared domain as result.
3677 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3678 __isl_take isl_pw_aff *pa2, int max)
3680 isl_bool has_nan;
3682 has_nan = either_involves_nan(pa1, pa2);
3683 if (has_nan < 0)
3684 pa1 = isl_pw_aff_free(pa1);
3685 else if (has_nan)
3686 return replace_by_nan(pa1, pa2);
3688 isl_pw_aff_align_params_bin(&pa1, &pa2);
3689 if (max)
3690 return pw_aff_max(pa1, pa2);
3691 else
3692 return pw_aff_min(pa1, pa2);
3695 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3697 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3698 __isl_take isl_pw_aff *pwaff2)
3700 return pw_aff_min_max(pwaff1, pwaff2, 0);
3703 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3705 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3706 __isl_take isl_pw_aff *pwaff2)
3708 return pw_aff_min_max(pwaff1, pwaff2, 1);
3711 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3712 __isl_take isl_pw_aff_list *list,
3713 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3714 __isl_take isl_pw_aff *pwaff2))
3716 int i;
3717 isl_ctx *ctx;
3718 isl_pw_aff *res;
3720 if (!list)
3721 return NULL;
3723 ctx = isl_pw_aff_list_get_ctx(list);
3724 if (list->n < 1)
3725 isl_die(ctx, isl_error_invalid,
3726 "list should contain at least one element", goto error);
3728 res = isl_pw_aff_copy(list->p[0]);
3729 for (i = 1; i < list->n; ++i)
3730 res = fn(res, isl_pw_aff_copy(list->p[i]));
3732 isl_pw_aff_list_free(list);
3733 return res;
3734 error:
3735 isl_pw_aff_list_free(list);
3736 return NULL;
3739 /* Return an isl_pw_aff that maps each element in the intersection of the
3740 * domains of the elements of list to the minimal corresponding affine
3741 * expression.
3743 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3745 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3748 /* Return an isl_pw_aff that maps each element in the intersection of the
3749 * domains of the elements of list to the maximal corresponding affine
3750 * expression.
3752 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3754 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3757 /* Mark the domains of "pwaff" as rational.
3759 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3761 int i;
3763 pwaff = isl_pw_aff_cow(pwaff);
3764 if (!pwaff)
3765 return NULL;
3766 if (pwaff->n == 0)
3767 return pwaff;
3769 for (i = 0; i < pwaff->n; ++i) {
3770 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3771 if (!pwaff->p[i].set)
3772 return isl_pw_aff_free(pwaff);
3775 return pwaff;
3778 /* Mark the domains of the elements of "list" as rational.
3780 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3781 __isl_take isl_pw_aff_list *list)
3783 int i, n;
3785 if (!list)
3786 return NULL;
3787 if (list->n == 0)
3788 return list;
3790 n = list->n;
3791 for (i = 0; i < n; ++i) {
3792 isl_pw_aff *pa;
3794 pa = isl_pw_aff_list_get_pw_aff(list, i);
3795 pa = isl_pw_aff_set_rational(pa);
3796 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3799 return list;
3802 /* Do the parameters of "aff" match those of "space"?
3804 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3805 __isl_keep isl_space *space)
3807 isl_space *aff_space;
3808 isl_bool match;
3810 if (!aff || !space)
3811 return isl_bool_error;
3813 aff_space = isl_aff_get_domain_space(aff);
3815 match = isl_space_has_equal_params(space, aff_space);
3817 isl_space_free(aff_space);
3818 return match;
3821 /* Check that the domain space of "aff" matches "space".
3823 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3824 __isl_keep isl_space *space)
3826 isl_space *aff_space;
3827 isl_bool match;
3829 if (!aff || !space)
3830 return isl_stat_error;
3832 aff_space = isl_aff_get_domain_space(aff);
3834 match = isl_space_has_equal_params(space, aff_space);
3835 if (match < 0)
3836 goto error;
3837 if (!match)
3838 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3839 "parameters don't match", goto error);
3840 match = isl_space_tuple_is_equal(space, isl_dim_in,
3841 aff_space, isl_dim_set);
3842 if (match < 0)
3843 goto error;
3844 if (!match)
3845 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3846 "domains don't match", goto error);
3847 isl_space_free(aff_space);
3848 return isl_stat_ok;
3849 error:
3850 isl_space_free(aff_space);
3851 return isl_stat_error;
3854 /* Return the shared (universe) domain of the elements of "ma".
3856 * Since an isl_multi_aff (and an isl_aff) is always total,
3857 * the domain is always the universe set in its domain space.
3858 * This is a helper function for use in the generic isl_multi_*_bind.
3860 static __isl_give isl_basic_set *isl_multi_aff_domain(
3861 __isl_take isl_multi_aff *ma)
3863 isl_space *space;
3865 space = isl_multi_aff_get_space(ma);
3866 isl_multi_aff_free(ma);
3868 return isl_basic_set_universe(isl_space_domain(space));
3871 #undef BASE
3872 #define BASE aff
3874 #include <isl_multi_no_explicit_domain.c>
3875 #include <isl_multi_templ.c>
3876 #include <isl_multi_un_op_templ.c>
3877 #include <isl_multi_bin_val_templ.c>
3878 #include <isl_multi_add_constant_templ.c>
3879 #include <isl_multi_apply_set.c>
3880 #include <isl_multi_arith_templ.c>
3881 #include <isl_multi_bind_domain_templ.c>
3882 #include <isl_multi_cmp.c>
3883 #include <isl_multi_dim_id_templ.c>
3884 #include <isl_multi_dims.c>
3885 #include <isl_multi_floor.c>
3886 #include <isl_multi_from_base_templ.c>
3887 #include <isl_multi_identity_templ.c>
3888 #include <isl_multi_insert_domain_templ.c>
3889 #include <isl_multi_locals_templ.c>
3890 #include <isl_multi_move_dims_templ.c>
3891 #include <isl_multi_nan_templ.c>
3892 #include <isl_multi_product_templ.c>
3893 #include <isl_multi_splice_templ.c>
3894 #include <isl_multi_tuple_id_templ.c>
3895 #include <isl_multi_unbind_params_templ.c>
3896 #include <isl_multi_zero_templ.c>
3898 #undef DOMBASE
3899 #define DOMBASE set
3900 #include <isl_multi_gist.c>
3902 #undef DOMBASE
3903 #define DOMBASE basic_set
3904 #include <isl_multi_bind_templ.c>
3906 /* Construct an isl_multi_aff living in "space" that corresponds
3907 * to the affine transformation matrix "mat".
3909 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3910 __isl_take isl_space *space, __isl_take isl_mat *mat)
3912 isl_ctx *ctx;
3913 isl_local_space *ls = NULL;
3914 isl_multi_aff *ma = NULL;
3915 isl_size n_row, n_col, n_out, total;
3916 int i;
3918 if (!space || !mat)
3919 goto error;
3921 ctx = isl_mat_get_ctx(mat);
3923 n_row = isl_mat_rows(mat);
3924 n_col = isl_mat_cols(mat);
3925 n_out = isl_space_dim(space, isl_dim_out);
3926 total = isl_space_dim(space, isl_dim_all);
3927 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3928 goto error;
3929 if (n_row < 1)
3930 isl_die(ctx, isl_error_invalid,
3931 "insufficient number of rows", goto error);
3932 if (n_col < 1)
3933 isl_die(ctx, isl_error_invalid,
3934 "insufficient number of columns", goto error);
3935 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3936 isl_die(ctx, isl_error_invalid,
3937 "dimension mismatch", goto error);
3939 ma = isl_multi_aff_zero(isl_space_copy(space));
3940 space = isl_space_domain(space);
3941 ls = isl_local_space_from_space(isl_space_copy(space));
3943 for (i = 0; i < n_row - 1; ++i) {
3944 isl_vec *v;
3945 isl_aff *aff;
3947 v = isl_vec_alloc(ctx, 1 + n_col);
3948 if (!v)
3949 goto error;
3950 isl_int_set(v->el[0], mat->row[0][0]);
3951 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3952 v = isl_vec_normalize(v);
3953 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3954 ma = isl_multi_aff_set_aff(ma, i, aff);
3957 isl_space_free(space);
3958 isl_local_space_free(ls);
3959 isl_mat_free(mat);
3960 return ma;
3961 error:
3962 isl_space_free(space);
3963 isl_local_space_free(ls);
3964 isl_mat_free(mat);
3965 isl_multi_aff_free(ma);
3966 return NULL;
3969 /* Return the constant terms of the affine expressions of "ma".
3971 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3972 __isl_keep isl_multi_aff *ma)
3974 int i;
3975 isl_size n;
3976 isl_space *space;
3977 isl_multi_val *mv;
3979 n = isl_multi_aff_size(ma);
3980 if (n < 0)
3981 return NULL;
3982 space = isl_space_range(isl_multi_aff_get_space(ma));
3983 space = isl_space_drop_all_params(space);
3984 mv = isl_multi_val_zero(space);
3986 for (i = 0; i < n; ++i) {
3987 isl_aff *aff;
3988 isl_val *val;
3990 aff = isl_multi_aff_get_at(ma, i);
3991 val = isl_aff_get_constant_val(aff);
3992 isl_aff_free(aff);
3993 mv = isl_multi_val_set_at(mv, i, val);
3996 return mv;
3999 /* Remove any internal structure of the domain of "ma".
4000 * If there is any such internal structure in the input,
4001 * then the name of the corresponding space is also removed.
4003 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4004 __isl_take isl_multi_aff *ma)
4006 isl_space *space;
4008 if (!ma)
4009 return NULL;
4011 if (!ma->space->nested[0])
4012 return ma;
4014 space = isl_multi_aff_get_space(ma);
4015 space = isl_space_flatten_domain(space);
4016 ma = isl_multi_aff_reset_space(ma, space);
4018 return ma;
4021 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4022 * of the space to its domain.
4024 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4026 int i;
4027 isl_size n_in;
4028 isl_local_space *ls;
4029 isl_multi_aff *ma;
4031 if (!space)
4032 return NULL;
4033 if (!isl_space_is_map(space))
4034 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4035 "not a map space", goto error);
4037 n_in = isl_space_dim(space, isl_dim_in);
4038 if (n_in < 0)
4039 goto error;
4040 space = isl_space_domain_map(space);
4042 ma = isl_multi_aff_alloc(isl_space_copy(space));
4043 if (n_in == 0) {
4044 isl_space_free(space);
4045 return ma;
4048 space = isl_space_domain(space);
4049 ls = isl_local_space_from_space(space);
4050 for (i = 0; i < n_in; ++i) {
4051 isl_aff *aff;
4053 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4054 isl_dim_set, i);
4055 ma = isl_multi_aff_set_aff(ma, i, aff);
4057 isl_local_space_free(ls);
4058 return ma;
4059 error:
4060 isl_space_free(space);
4061 return NULL;
4064 /* This function performs the same operation as isl_multi_aff_domain_map,
4065 * but is considered as a function on an isl_space when exported.
4067 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4068 __isl_take isl_space *space)
4070 return isl_multi_aff_domain_map(space);
4073 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4074 * of the space to its range.
4076 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4078 int i;
4079 isl_size n_in, n_out;
4080 isl_local_space *ls;
4081 isl_multi_aff *ma;
4083 if (!space)
4084 return NULL;
4085 if (!isl_space_is_map(space))
4086 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4087 "not a map space", goto error);
4089 n_in = isl_space_dim(space, isl_dim_in);
4090 n_out = isl_space_dim(space, isl_dim_out);
4091 if (n_in < 0 || n_out < 0)
4092 goto error;
4093 space = isl_space_range_map(space);
4095 ma = isl_multi_aff_alloc(isl_space_copy(space));
4096 if (n_out == 0) {
4097 isl_space_free(space);
4098 return ma;
4101 space = isl_space_domain(space);
4102 ls = isl_local_space_from_space(space);
4103 for (i = 0; i < n_out; ++i) {
4104 isl_aff *aff;
4106 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4107 isl_dim_set, n_in + i);
4108 ma = isl_multi_aff_set_aff(ma, i, aff);
4110 isl_local_space_free(ls);
4111 return ma;
4112 error:
4113 isl_space_free(space);
4114 return NULL;
4117 /* This function performs the same operation as isl_multi_aff_range_map,
4118 * but is considered as a function on an isl_space when exported.
4120 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4121 __isl_take isl_space *space)
4123 return isl_multi_aff_range_map(space);
4126 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4127 * of the space to its domain.
4129 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4130 __isl_take isl_space *space)
4132 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4135 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4136 * but is considered as a function on an isl_space when exported.
4138 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4139 __isl_take isl_space *space)
4141 return isl_pw_multi_aff_domain_map(space);
4144 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4145 * of the space to its range.
4147 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4148 __isl_take isl_space *space)
4150 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4153 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4154 * but is considered as a function on an isl_space when exported.
4156 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4157 __isl_take isl_space *space)
4159 return isl_pw_multi_aff_range_map(space);
4162 /* Given the space of a set and a range of set dimensions,
4163 * construct an isl_multi_aff that projects out those dimensions.
4165 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4166 __isl_take isl_space *space, enum isl_dim_type type,
4167 unsigned first, unsigned n)
4169 int i;
4170 isl_size dim;
4171 isl_local_space *ls;
4172 isl_multi_aff *ma;
4174 if (!space)
4175 return NULL;
4176 if (!isl_space_is_set(space))
4177 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4178 "expecting set space", goto error);
4179 if (type != isl_dim_set)
4180 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4181 "only set dimensions can be projected out", goto error);
4182 if (isl_space_check_range(space, type, first, n) < 0)
4183 goto error;
4185 dim = isl_space_dim(space, isl_dim_set);
4186 if (dim < 0)
4187 goto error;
4189 space = isl_space_from_domain(space);
4190 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4192 if (dim == n)
4193 return isl_multi_aff_alloc(space);
4195 ma = isl_multi_aff_alloc(isl_space_copy(space));
4196 space = isl_space_domain(space);
4197 ls = isl_local_space_from_space(space);
4199 for (i = 0; i < first; ++i) {
4200 isl_aff *aff;
4202 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4203 isl_dim_set, i);
4204 ma = isl_multi_aff_set_aff(ma, i, aff);
4207 for (i = 0; i < dim - (first + n); ++i) {
4208 isl_aff *aff;
4210 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4211 isl_dim_set, first + n + i);
4212 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4215 isl_local_space_free(ls);
4216 return ma;
4217 error:
4218 isl_space_free(space);
4219 return NULL;
4222 /* Given the space of a set and a range of set dimensions,
4223 * construct an isl_pw_multi_aff that projects out those dimensions.
4225 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4226 __isl_take isl_space *space, enum isl_dim_type type,
4227 unsigned first, unsigned n)
4229 isl_multi_aff *ma;
4231 ma = isl_multi_aff_project_out_map(space, type, first, n);
4232 return isl_pw_multi_aff_from_multi_aff(ma);
4235 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4236 * but is considered as a function on an isl_multi_aff when exported.
4238 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4239 __isl_take isl_multi_aff *ma)
4241 return isl_pw_multi_aff_from_multi_aff(ma);
4244 /* Create a piecewise multi-affine expression in the given space that maps each
4245 * input dimension to the corresponding output dimension.
4247 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4248 __isl_take isl_space *space)
4250 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4253 /* Create a piecewise multi expression that maps elements in the given space
4254 * to themselves.
4256 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4257 __isl_take isl_space *space)
4259 isl_multi_aff *ma;
4261 ma = isl_multi_aff_identity_on_domain_space(space);
4262 return isl_pw_multi_aff_from_multi_aff(ma);
4265 /* This function performs the same operation as
4266 * isl_pw_multi_aff_identity_on_domain_space,
4267 * but is considered as a function on an isl_space when exported.
4269 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4270 __isl_take isl_space *space)
4272 return isl_pw_multi_aff_identity_on_domain_space(space);
4275 /* Exploit the equalities in "eq" to simplify the affine expressions.
4277 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4278 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4280 isl_size n;
4281 int i;
4283 maff = isl_multi_aff_cow(maff);
4284 n = isl_multi_aff_size(maff);
4285 if (n < 0 || !eq)
4286 goto error;
4288 for (i = 0; i < n; ++i) {
4289 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4290 isl_basic_set_copy(eq));
4291 if (!maff->u.p[i])
4292 goto error;
4295 isl_basic_set_free(eq);
4296 return maff;
4297 error:
4298 isl_basic_set_free(eq);
4299 isl_multi_aff_free(maff);
4300 return NULL;
4303 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4304 isl_int f)
4306 isl_size n;
4307 int i;
4309 maff = isl_multi_aff_cow(maff);
4310 n = isl_multi_aff_size(maff);
4311 if (n < 0)
4312 return isl_multi_aff_free(maff);
4314 for (i = 0; i < n; ++i) {
4315 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4316 if (!maff->u.p[i])
4317 return isl_multi_aff_free(maff);
4320 return maff;
4323 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4324 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4326 maff1 = isl_multi_aff_add(maff1, maff2);
4327 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4328 return maff1;
4331 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4333 if (!maff)
4334 return isl_bool_error;
4336 return isl_bool_false;
4339 /* Return the set of domain elements where "ma1" is lexicographically
4340 * smaller than or equal to "ma2".
4342 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4343 __isl_take isl_multi_aff *ma2)
4345 return isl_multi_aff_lex_ge_set(ma2, ma1);
4348 /* Return the set of domain elements where "ma1" is lexicographically
4349 * smaller than "ma2".
4351 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4352 __isl_take isl_multi_aff *ma2)
4354 return isl_multi_aff_lex_gt_set(ma2, ma1);
4357 /* Return the set of domain elements where "ma1" is lexicographically
4358 * greater than to "ma2". If "equal" is set, then include the domain
4359 * elements where they are equal.
4360 * Do this for the case where there are no entries.
4361 * In this case, "ma1" cannot be greater than "ma2",
4362 * but it is (greater than or) equal to "ma2".
4364 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4365 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4367 isl_space *space;
4369 space = isl_multi_aff_get_domain_space(ma1);
4371 isl_multi_aff_free(ma1);
4372 isl_multi_aff_free(ma2);
4374 if (equal)
4375 return isl_set_universe(space);
4376 else
4377 return isl_set_empty(space);
4380 /* Return the set where entry "i" of "ma1" and "ma2"
4381 * satisfy the relation prescribed by "cmp".
4383 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4384 __isl_keep isl_multi_aff *ma2, int i,
4385 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4386 __isl_take isl_aff *aff2))
4388 isl_aff *aff1, *aff2;
4390 aff1 = isl_multi_aff_get_at(ma1, i);
4391 aff2 = isl_multi_aff_get_at(ma2, i);
4392 return cmp(aff1, aff2);
4395 /* Return the set of domain elements where "ma1" is lexicographically
4396 * greater than to "ma2". If "equal" is set, then include the domain
4397 * elements where they are equal.
4399 * In particular, for all but the final entry,
4400 * include the set of elements where this entry is strictly greater in "ma1"
4401 * and all previous entries are equal.
4402 * The final entry is also allowed to be equal in the two functions
4403 * if "equal" is set.
4405 * The case where there are no entries is handled separately.
4407 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4408 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4410 int i;
4411 isl_size n;
4412 isl_space *space;
4413 isl_set *res;
4414 isl_set *equal_set;
4415 isl_set *gte;
4417 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4418 goto error;
4419 n = isl_multi_aff_size(ma1);
4420 if (n < 0)
4421 goto error;
4422 if (n == 0)
4423 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4425 space = isl_multi_aff_get_domain_space(ma1);
4426 res = isl_set_empty(isl_space_copy(space));
4427 equal_set = isl_set_universe(space);
4429 for (i = 0; i + 1 < n; ++i) {
4430 isl_bool empty;
4431 isl_set *gt, *eq;
4433 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4434 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4435 res = isl_set_union(res, gt);
4436 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4437 equal_set = isl_set_intersect(equal_set, eq);
4439 empty = isl_set_is_empty(equal_set);
4440 if (empty >= 0 && empty)
4441 break;
4444 if (equal)
4445 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4446 else
4447 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4448 isl_multi_aff_free(ma1);
4449 isl_multi_aff_free(ma2);
4451 gte = isl_set_intersect(gte, equal_set);
4452 return isl_set_union(res, gte);
4453 error:
4454 isl_multi_aff_free(ma1);
4455 isl_multi_aff_free(ma2);
4456 return NULL;
4459 /* Return the set of domain elements where "ma1" is lexicographically
4460 * greater than or equal to "ma2".
4462 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4463 __isl_take isl_multi_aff *ma2)
4465 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4468 /* Return the set of domain elements where "ma1" is lexicographically
4469 * greater than "ma2".
4471 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4472 __isl_take isl_multi_aff *ma2)
4474 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4477 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4479 #undef PW
4480 #define PW isl_pw_multi_aff
4481 #undef BASE
4482 #define BASE multi_aff
4483 #undef EL_IS_ZERO
4484 #define EL_IS_ZERO is_empty
4485 #undef ZERO
4486 #define ZERO empty
4487 #undef IS_ZERO
4488 #define IS_ZERO is_empty
4489 #undef FIELD
4490 #define FIELD maff
4491 #undef DEFAULT_IS_ZERO
4492 #define DEFAULT_IS_ZERO 0
4494 #include <isl_pw_templ.c>
4495 #include <isl_pw_un_op_templ.c>
4496 #include <isl_pw_add_constant_multi_val_templ.c>
4497 #include <isl_pw_add_constant_val_templ.c>
4498 #include <isl_pw_bind_domain_templ.c>
4499 #include <isl_pw_insert_dims_templ.c>
4500 #include <isl_pw_insert_domain_templ.c>
4501 #include <isl_pw_locals_templ.c>
4502 #include <isl_pw_move_dims_templ.c>
4503 #include <isl_pw_neg_templ.c>
4504 #include <isl_pw_pullback_templ.c>
4505 #include <isl_pw_range_tuple_id_templ.c>
4506 #include <isl_pw_union_opt.c>
4508 #undef BASE
4509 #define BASE pw_multi_aff
4511 #include <isl_union_multi.c>
4512 #include "isl_union_locals_templ.c"
4513 #include <isl_union_neg.c>
4515 #undef BASE
4516 #define BASE multi_aff
4518 #include <isl_union_pw_templ.c>
4520 /* Generic function for extracting a factor from a product "pma".
4521 * "check_space" checks that the space is that of the right kind of product.
4522 * "space_factor" extracts the factor from the space.
4523 * "multi_aff_factor" extracts the factor from the constituent functions.
4525 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4526 __isl_take isl_pw_multi_aff *pma,
4527 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4528 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4529 __isl_give isl_multi_aff *(*multi_aff_factor)(
4530 __isl_take isl_multi_aff *ma))
4532 int i;
4533 isl_space *space;
4535 if (check_space(pma) < 0)
4536 return isl_pw_multi_aff_free(pma);
4538 space = isl_pw_multi_aff_take_space(pma);
4539 space = space_factor(space);
4541 for (i = 0; pma && i < pma->n; ++i) {
4542 isl_multi_aff *ma;
4544 ma = isl_pw_multi_aff_take_base_at(pma, i);
4545 ma = multi_aff_factor(ma);
4546 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4549 pma = isl_pw_multi_aff_restore_space(pma, space);
4551 return pma;
4554 /* Is the range of "pma" a wrapped relation?
4556 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4557 __isl_keep isl_pw_multi_aff *pma)
4559 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4562 /* Check that the range of "pma" is a product.
4564 static isl_stat pw_multi_aff_check_range_product(
4565 __isl_keep isl_pw_multi_aff *pma)
4567 isl_bool wraps;
4569 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4570 if (wraps < 0)
4571 return isl_stat_error;
4572 if (!wraps)
4573 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4574 "range is not a product", return isl_stat_error);
4575 return isl_stat_ok;
4578 /* Given a function A -> [B -> C], extract the function A -> B.
4580 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4581 __isl_take isl_pw_multi_aff *pma)
4583 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4584 &isl_space_range_factor_domain,
4585 &isl_multi_aff_range_factor_domain);
4588 /* Given a function A -> [B -> C], extract the function A -> C.
4590 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4591 __isl_take isl_pw_multi_aff *pma)
4593 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4594 &isl_space_range_factor_range,
4595 &isl_multi_aff_range_factor_range);
4598 /* Given two piecewise multi affine expressions, return a piecewise
4599 * multi-affine expression defined on the union of the definition domains
4600 * of the inputs that is equal to the lexicographic maximum of the two
4601 * inputs on each cell. If only one of the two inputs is defined on
4602 * a given cell, then it is considered to be the maximum.
4604 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4605 __isl_take isl_pw_multi_aff *pma1,
4606 __isl_take isl_pw_multi_aff *pma2)
4608 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4609 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4610 &isl_multi_aff_lex_ge_set);
4613 /* Given two piecewise multi affine expressions, return a piecewise
4614 * multi-affine expression defined on the union of the definition domains
4615 * of the inputs that is equal to the lexicographic minimum of the two
4616 * inputs on each cell. If only one of the two inputs is defined on
4617 * a given cell, then it is considered to be the minimum.
4619 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4620 __isl_take isl_pw_multi_aff *pma1,
4621 __isl_take isl_pw_multi_aff *pma2)
4623 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4624 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4625 &isl_multi_aff_lex_le_set);
4628 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4629 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4631 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4632 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4633 &isl_multi_aff_add);
4636 /* Subtract "pma2" from "pma1" and return the result.
4638 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4639 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4641 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4642 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4643 &isl_multi_aff_sub);
4646 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4647 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4649 return isl_pw_multi_aff_union_add_(pma1, pma2);
4652 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4653 * with the actual sum on the shared domain and
4654 * the defined expression on the symmetric difference of the domains.
4656 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4657 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4659 return isl_union_pw_aff_union_add_(upa1, upa2);
4662 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4663 * with the actual sum on the shared domain and
4664 * the defined expression on the symmetric difference of the domains.
4666 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4667 __isl_take isl_union_pw_multi_aff *upma1,
4668 __isl_take isl_union_pw_multi_aff *upma2)
4670 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4673 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4674 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4676 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4677 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4679 int i, j, n;
4680 isl_space *space;
4681 isl_pw_multi_aff *res;
4683 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4684 goto error;
4686 n = pma1->n * pma2->n;
4687 space = isl_space_product(isl_space_copy(pma1->dim),
4688 isl_space_copy(pma2->dim));
4689 res = isl_pw_multi_aff_alloc_size(space, n);
4691 for (i = 0; i < pma1->n; ++i) {
4692 for (j = 0; j < pma2->n; ++j) {
4693 isl_set *domain;
4694 isl_multi_aff *ma;
4696 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4697 isl_set_copy(pma2->p[j].set));
4698 ma = isl_multi_aff_product(
4699 isl_multi_aff_copy(pma1->p[i].maff),
4700 isl_multi_aff_copy(pma2->p[j].maff));
4701 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4705 isl_pw_multi_aff_free(pma1);
4706 isl_pw_multi_aff_free(pma2);
4707 return res;
4708 error:
4709 isl_pw_multi_aff_free(pma1);
4710 isl_pw_multi_aff_free(pma2);
4711 return NULL;
4714 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4715 * denominator "denom".
4716 * "denom" is allowed to be negative, in which case the actual denominator
4717 * is -denom and the expressions are added instead.
4719 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4720 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4722 int i, first;
4723 int sign;
4724 isl_int d;
4726 first = isl_seq_first_non_zero(c, n);
4727 if (first == -1)
4728 return aff;
4730 sign = isl_int_sgn(denom);
4731 isl_int_init(d);
4732 isl_int_abs(d, denom);
4733 for (i = first; i < n; ++i) {
4734 isl_aff *aff_i;
4736 if (isl_int_is_zero(c[i]))
4737 continue;
4738 aff_i = isl_multi_aff_get_aff(ma, i);
4739 aff_i = isl_aff_scale(aff_i, c[i]);
4740 aff_i = isl_aff_scale_down(aff_i, d);
4741 if (sign >= 0)
4742 aff = isl_aff_sub(aff, aff_i);
4743 else
4744 aff = isl_aff_add(aff, aff_i);
4746 isl_int_clear(d);
4748 return aff;
4751 /* Extract an affine expression that expresses the output dimension "pos"
4752 * of "bmap" in terms of the parameters and input dimensions from
4753 * equality "eq".
4754 * Note that this expression may involve integer divisions defined
4755 * in terms of parameters and input dimensions.
4756 * The equality may also involve references to earlier (but not later)
4757 * output dimensions. These are replaced by the corresponding elements
4758 * in "ma".
4760 * If the equality is of the form
4762 * f(i) + h(j) + a x + g(i) = 0,
4764 * with f(i) a linear combinations of the parameters and input dimensions,
4765 * g(i) a linear combination of integer divisions defined in terms of the same
4766 * and h(j) a linear combinations of earlier output dimensions,
4767 * then the affine expression is
4769 * (-f(i) - g(i))/a - h(j)/a
4771 * If the equality is of the form
4773 * f(i) + h(j) - a x + g(i) = 0,
4775 * then the affine expression is
4777 * (f(i) + g(i))/a - h(j)/(-a)
4780 * If "div" refers to an integer division (i.e., it is smaller than
4781 * the number of integer divisions), then the equality constraint
4782 * does involve an integer division (the one at position "div") that
4783 * is defined in terms of output dimensions. However, this integer
4784 * division can be eliminated by exploiting a pair of constraints
4785 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4786 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4787 * -l + x >= 0.
4788 * In particular, let
4790 * x = e(i) + m floor(...)
4792 * with e(i) the expression derived above and floor(...) the integer
4793 * division involving output dimensions.
4794 * From
4796 * l <= x <= l + n,
4798 * we have
4800 * 0 <= x - l <= n
4802 * This means
4804 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4805 * = (e(i) - l) mod m
4807 * Therefore,
4809 * x - l = (e(i) - l) mod m
4811 * or
4813 * x = ((e(i) - l) mod m) + l
4815 * The variable "shift" below contains the expression -l, which may
4816 * also involve a linear combination of earlier output dimensions.
4818 static __isl_give isl_aff *extract_aff_from_equality(
4819 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4820 __isl_keep isl_multi_aff *ma)
4822 unsigned o_out;
4823 isl_size n_div, n_out;
4824 isl_ctx *ctx;
4825 isl_local_space *ls;
4826 isl_aff *aff, *shift;
4827 isl_val *mod;
4829 ctx = isl_basic_map_get_ctx(bmap);
4830 ls = isl_basic_map_get_local_space(bmap);
4831 ls = isl_local_space_domain(ls);
4832 aff = isl_aff_alloc(isl_local_space_copy(ls));
4833 if (!aff)
4834 goto error;
4835 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4836 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4837 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4838 if (n_out < 0 || n_div < 0)
4839 goto error;
4840 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4841 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4842 isl_seq_cpy(aff->v->el + 1 + o_out,
4843 bmap->eq[eq] + o_out + n_out, n_div);
4844 } else {
4845 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4846 isl_seq_neg(aff->v->el + 1 + o_out,
4847 bmap->eq[eq] + o_out + n_out, n_div);
4849 if (div < n_div)
4850 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4851 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4852 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4853 bmap->eq[eq][o_out + pos]);
4854 if (div < n_div) {
4855 shift = isl_aff_alloc(isl_local_space_copy(ls));
4856 if (!shift)
4857 goto error;
4858 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4859 isl_seq_cpy(shift->v->el + 1 + o_out,
4860 bmap->ineq[ineq] + o_out + n_out, n_div);
4861 isl_int_set_si(shift->v->el[0], 1);
4862 shift = subtract_initial(shift, ma, pos,
4863 bmap->ineq[ineq] + o_out, ctx->negone);
4864 aff = isl_aff_add(aff, isl_aff_copy(shift));
4865 mod = isl_val_int_from_isl_int(ctx,
4866 bmap->eq[eq][o_out + n_out + div]);
4867 mod = isl_val_abs(mod);
4868 aff = isl_aff_mod_val(aff, mod);
4869 aff = isl_aff_sub(aff, shift);
4872 isl_local_space_free(ls);
4873 return aff;
4874 error:
4875 isl_local_space_free(ls);
4876 isl_aff_free(aff);
4877 return NULL;
4880 /* Given a basic map with output dimensions defined
4881 * in terms of the parameters input dimensions and earlier
4882 * output dimensions using an equality (and possibly a pair on inequalities),
4883 * extract an isl_aff that expresses output dimension "pos" in terms
4884 * of the parameters and input dimensions.
4885 * Note that this expression may involve integer divisions defined
4886 * in terms of parameters and input dimensions.
4887 * "ma" contains the expressions corresponding to earlier output dimensions.
4889 * This function shares some similarities with
4890 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4892 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4893 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4895 int eq, div, ineq;
4896 isl_aff *aff;
4898 if (!bmap)
4899 return NULL;
4900 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4901 if (eq >= bmap->n_eq)
4902 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4903 "unable to find suitable equality", return NULL);
4904 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4906 aff = isl_aff_remove_unused_divs(aff);
4907 return aff;
4910 /* Given a basic map where each output dimension is defined
4911 * in terms of the parameters and input dimensions using an equality,
4912 * extract an isl_multi_aff that expresses the output dimensions in terms
4913 * of the parameters and input dimensions.
4915 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4916 __isl_take isl_basic_map *bmap)
4918 int i;
4919 isl_size n_out;
4920 isl_multi_aff *ma;
4922 if (!bmap)
4923 return NULL;
4925 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4926 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4927 if (n_out < 0)
4928 ma = isl_multi_aff_free(ma);
4930 for (i = 0; i < n_out; ++i) {
4931 isl_aff *aff;
4933 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4934 ma = isl_multi_aff_set_aff(ma, i, aff);
4937 isl_basic_map_free(bmap);
4939 return ma;
4942 /* Given a basic set where each set dimension is defined
4943 * in terms of the parameters using an equality,
4944 * extract an isl_multi_aff that expresses the set dimensions in terms
4945 * of the parameters.
4947 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4948 __isl_take isl_basic_set *bset)
4950 return extract_isl_multi_aff_from_basic_map(bset);
4953 /* Create an isl_pw_multi_aff that is equivalent to
4954 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4955 * The given basic map is such that each output dimension is defined
4956 * in terms of the parameters and input dimensions using an equality.
4958 * Since some applications expect the result of isl_pw_multi_aff_from_map
4959 * to only contain integer affine expressions, we compute the floor
4960 * of the expression before returning.
4962 * Remove all constraints involving local variables without
4963 * an explicit representation (resulting in the removal of those
4964 * local variables) prior to the actual extraction to ensure
4965 * that the local spaces in which the resulting affine expressions
4966 * are created do not contain any unknown local variables.
4967 * Removing such constraints is safe because constraints involving
4968 * unknown local variables are not used to determine whether
4969 * a basic map is obviously single-valued.
4971 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4972 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4974 isl_multi_aff *ma;
4976 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4977 ma = extract_isl_multi_aff_from_basic_map(bmap);
4978 ma = isl_multi_aff_floor(ma);
4979 return isl_pw_multi_aff_alloc(domain, ma);
4982 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4983 * This obviously only works if the input "map" is single-valued.
4984 * If so, we compute the lexicographic minimum of the image in the form
4985 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4986 * to its lexicographic minimum.
4987 * If the input is not single-valued, we produce an error.
4989 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4990 __isl_take isl_map *map)
4992 int i;
4993 int sv;
4994 isl_pw_multi_aff *pma;
4996 sv = isl_map_is_single_valued(map);
4997 if (sv < 0)
4998 goto error;
4999 if (!sv)
5000 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5001 "map is not single-valued", goto error);
5002 map = isl_map_make_disjoint(map);
5003 if (!map)
5004 return NULL;
5006 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5008 for (i = 0; i < map->n; ++i) {
5009 isl_pw_multi_aff *pma_i;
5010 isl_basic_map *bmap;
5011 bmap = isl_basic_map_copy(map->p[i]);
5012 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5013 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5016 isl_map_free(map);
5017 return pma;
5018 error:
5019 isl_map_free(map);
5020 return NULL;
5023 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5024 * taking into account that the output dimension at position "d"
5025 * can be represented as
5027 * x = floor((e(...) + c1) / m)
5029 * given that constraint "i" is of the form
5031 * e(...) + c1 - m x >= 0
5034 * Let "map" be of the form
5036 * A -> B
5038 * We construct a mapping
5040 * A -> [A -> x = floor(...)]
5042 * apply that to the map, obtaining
5044 * [A -> x = floor(...)] -> B
5046 * and equate dimension "d" to x.
5047 * We then compute a isl_pw_multi_aff representation of the resulting map
5048 * and plug in the mapping above.
5050 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5051 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5053 isl_ctx *ctx;
5054 isl_space *space = NULL;
5055 isl_local_space *ls;
5056 isl_multi_aff *ma;
5057 isl_aff *aff;
5058 isl_vec *v;
5059 isl_map *insert;
5060 int offset;
5061 isl_size n;
5062 isl_size n_in;
5063 isl_pw_multi_aff *pma;
5064 isl_bool is_set;
5066 is_set = isl_map_is_set(map);
5067 if (is_set < 0)
5068 goto error;
5070 offset = isl_basic_map_offset(hull, isl_dim_out);
5071 ctx = isl_map_get_ctx(map);
5072 space = isl_space_domain(isl_map_get_space(map));
5073 n_in = isl_space_dim(space, isl_dim_set);
5074 n = isl_space_dim(space, isl_dim_all);
5075 if (n_in < 0 || n < 0)
5076 goto error;
5078 v = isl_vec_alloc(ctx, 1 + 1 + n);
5079 if (v) {
5080 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5081 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5083 isl_basic_map_free(hull);
5085 ls = isl_local_space_from_space(isl_space_copy(space));
5086 aff = isl_aff_alloc_vec(ls, v);
5087 aff = isl_aff_floor(aff);
5088 if (is_set) {
5089 isl_space_free(space);
5090 ma = isl_multi_aff_from_aff(aff);
5091 } else {
5092 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5093 ma = isl_multi_aff_range_product(ma,
5094 isl_multi_aff_from_aff(aff));
5097 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5098 map = isl_map_apply_domain(map, insert);
5099 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5100 pma = isl_pw_multi_aff_from_map(map);
5101 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5103 return pma;
5104 error:
5105 isl_space_free(space);
5106 isl_map_free(map);
5107 isl_basic_map_free(hull);
5108 return NULL;
5111 /* Is constraint "c" of the form
5113 * e(...) + c1 - m x >= 0
5115 * or
5117 * -e(...) + c2 + m x >= 0
5119 * where m > 1 and e only depends on parameters and input dimensions?
5121 * "offset" is the offset of the output dimensions
5122 * "pos" is the position of output dimension x.
5124 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5126 if (isl_int_is_zero(c[offset + d]))
5127 return 0;
5128 if (isl_int_is_one(c[offset + d]))
5129 return 0;
5130 if (isl_int_is_negone(c[offset + d]))
5131 return 0;
5132 if (isl_seq_first_non_zero(c + offset, d) != -1)
5133 return 0;
5134 if (isl_seq_first_non_zero(c + offset + d + 1,
5135 total - (offset + d + 1)) != -1)
5136 return 0;
5137 return 1;
5140 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5142 * As a special case, we first check if there is any pair of constraints,
5143 * shared by all the basic maps in "map" that force a given dimension
5144 * to be equal to the floor of some affine combination of the input dimensions.
5146 * In particular, if we can find two constraints
5148 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5150 * and
5152 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5154 * where m > 1 and e only depends on parameters and input dimensions,
5155 * and such that
5157 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5159 * then we know that we can take
5161 * x = floor((e(...) + c1) / m)
5163 * without having to perform any computation.
5165 * Note that we know that
5167 * c1 + c2 >= 1
5169 * If c1 + c2 were 0, then we would have detected an equality during
5170 * simplification. If c1 + c2 were negative, then we would have detected
5171 * a contradiction.
5173 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5174 __isl_take isl_map *map)
5176 int d;
5177 isl_size dim;
5178 int i, j, n;
5179 int offset;
5180 isl_size total;
5181 isl_int sum;
5182 isl_basic_map *hull;
5184 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5185 dim = isl_map_dim(map, isl_dim_out);
5186 total = isl_basic_map_dim(hull, isl_dim_all);
5187 if (dim < 0 || total < 0)
5188 goto error;
5190 isl_int_init(sum);
5191 offset = isl_basic_map_offset(hull, isl_dim_out);
5192 n = hull->n_ineq;
5193 for (d = 0; d < dim; ++d) {
5194 for (i = 0; i < n; ++i) {
5195 if (!is_potential_div_constraint(hull->ineq[i],
5196 offset, d, 1 + total))
5197 continue;
5198 for (j = i + 1; j < n; ++j) {
5199 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5200 hull->ineq[j] + 1, total))
5201 continue;
5202 isl_int_add(sum, hull->ineq[i][0],
5203 hull->ineq[j][0]);
5204 if (isl_int_abs_lt(sum,
5205 hull->ineq[i][offset + d]))
5206 break;
5209 if (j >= n)
5210 continue;
5211 isl_int_clear(sum);
5212 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5213 j = i;
5214 return pw_multi_aff_from_map_div(map, hull, d, j);
5217 isl_int_clear(sum);
5218 isl_basic_map_free(hull);
5219 return pw_multi_aff_from_map_base(map);
5220 error:
5221 isl_map_free(map);
5222 isl_basic_map_free(hull);
5223 return NULL;
5226 /* Given an affine expression
5228 * [A -> B] -> f(A,B)
5230 * construct an isl_multi_aff
5232 * [A -> B] -> B'
5234 * such that dimension "d" in B' is set to "aff" and the remaining
5235 * dimensions are set equal to the corresponding dimensions in B.
5236 * "n_in" is the dimension of the space A.
5237 * "n_out" is the dimension of the space B.
5239 * If "is_set" is set, then the affine expression is of the form
5241 * [B] -> f(B)
5243 * and we construct an isl_multi_aff
5245 * B -> B'
5247 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5248 unsigned n_in, unsigned n_out, int is_set)
5250 int i;
5251 isl_multi_aff *ma;
5252 isl_space *space, *space2;
5253 isl_local_space *ls;
5255 space = isl_aff_get_domain_space(aff);
5256 ls = isl_local_space_from_space(isl_space_copy(space));
5257 space2 = isl_space_copy(space);
5258 if (!is_set)
5259 space2 = isl_space_range(isl_space_unwrap(space2));
5260 space = isl_space_map_from_domain_and_range(space, space2);
5261 ma = isl_multi_aff_alloc(space);
5262 ma = isl_multi_aff_set_aff(ma, d, aff);
5264 for (i = 0; i < n_out; ++i) {
5265 if (i == d)
5266 continue;
5267 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5268 isl_dim_set, n_in + i);
5269 ma = isl_multi_aff_set_aff(ma, i, aff);
5272 isl_local_space_free(ls);
5274 return ma;
5277 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5278 * taking into account that the dimension at position "d" can be written as
5280 * x = m a + f(..) (1)
5282 * where m is equal to "gcd".
5283 * "i" is the index of the equality in "hull" that defines f(..).
5284 * In particular, the equality is of the form
5286 * f(..) - x + m g(existentials) = 0
5288 * or
5290 * -f(..) + x + m g(existentials) = 0
5292 * We basically plug (1) into "map", resulting in a map with "a"
5293 * in the range instead of "x". The corresponding isl_pw_multi_aff
5294 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5296 * Specifically, given the input map
5298 * A -> B
5300 * We first wrap it into a set
5302 * [A -> B]
5304 * and define (1) on top of the corresponding space, resulting in "aff".
5305 * We use this to create an isl_multi_aff that maps the output position "d"
5306 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5307 * We plug this into the wrapped map, unwrap the result and compute the
5308 * corresponding isl_pw_multi_aff.
5309 * The result is an expression
5311 * A -> T(A)
5313 * We adjust that to
5315 * A -> [A -> T(A)]
5317 * so that we can plug that into "aff", after extending the latter to
5318 * a mapping
5320 * [A -> B] -> B'
5323 * If "map" is actually a set, then there is no "A" space, meaning
5324 * that we do not need to perform any wrapping, and that the result
5325 * of the recursive call is of the form
5327 * [T]
5329 * which is plugged into a mapping of the form
5331 * B -> B'
5333 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5334 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5335 isl_int gcd)
5337 isl_set *set;
5338 isl_space *space;
5339 isl_local_space *ls;
5340 isl_aff *aff;
5341 isl_multi_aff *ma;
5342 isl_pw_multi_aff *pma, *id;
5343 isl_size n_in;
5344 unsigned o_out;
5345 isl_size n_out;
5346 isl_bool is_set;
5348 is_set = isl_map_is_set(map);
5349 if (is_set < 0)
5350 goto error;
5352 n_in = isl_basic_map_dim(hull, isl_dim_in);
5353 n_out = isl_basic_map_dim(hull, isl_dim_out);
5354 if (n_in < 0 || n_out < 0)
5355 goto error;
5356 o_out = isl_basic_map_offset(hull, isl_dim_out);
5358 if (is_set)
5359 set = map;
5360 else
5361 set = isl_map_wrap(map);
5362 space = isl_space_map_from_set(isl_set_get_space(set));
5363 ma = isl_multi_aff_identity(space);
5364 ls = isl_local_space_from_space(isl_set_get_space(set));
5365 aff = isl_aff_alloc(ls);
5366 if (aff) {
5367 isl_int_set_si(aff->v->el[0], 1);
5368 if (isl_int_is_one(hull->eq[i][o_out + d]))
5369 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5370 aff->v->size - 1);
5371 else
5372 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5373 aff->v->size - 1);
5374 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5376 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5377 set = isl_set_preimage_multi_aff(set, ma);
5379 ma = range_map(aff, d, n_in, n_out, is_set);
5381 if (is_set)
5382 map = set;
5383 else
5384 map = isl_set_unwrap(set);
5385 pma = isl_pw_multi_aff_from_map(map);
5387 if (!is_set) {
5388 space = isl_pw_multi_aff_get_domain_space(pma);
5389 space = isl_space_map_from_set(space);
5390 id = isl_pw_multi_aff_identity(space);
5391 pma = isl_pw_multi_aff_range_product(id, pma);
5393 id = isl_pw_multi_aff_from_multi_aff(ma);
5394 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5396 isl_basic_map_free(hull);
5397 return pma;
5398 error:
5399 isl_map_free(map);
5400 isl_basic_map_free(hull);
5401 return NULL;
5404 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5405 * "hull" contains the equalities valid for "map".
5407 * Check if any of the output dimensions is "strided".
5408 * That is, we check if it can be written as
5410 * x = m a + f(..)
5412 * with m greater than 1, a some combination of existentially quantified
5413 * variables and f an expression in the parameters and input dimensions.
5414 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5416 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5417 * special case.
5419 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5420 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5422 int i, j;
5423 isl_size n_out;
5424 unsigned o_out;
5425 isl_size n_div;
5426 unsigned o_div;
5427 isl_int gcd;
5429 n_div = isl_basic_map_dim(hull, isl_dim_div);
5430 n_out = isl_basic_map_dim(hull, isl_dim_out);
5431 if (n_div < 0 || n_out < 0)
5432 goto error;
5434 if (n_div == 0) {
5435 isl_basic_map_free(hull);
5436 return pw_multi_aff_from_map_check_div(map);
5439 isl_int_init(gcd);
5441 o_div = isl_basic_map_offset(hull, isl_dim_div);
5442 o_out = isl_basic_map_offset(hull, isl_dim_out);
5444 for (i = 0; i < n_out; ++i) {
5445 for (j = 0; j < hull->n_eq; ++j) {
5446 isl_int *eq = hull->eq[j];
5447 isl_pw_multi_aff *res;
5449 if (!isl_int_is_one(eq[o_out + i]) &&
5450 !isl_int_is_negone(eq[o_out + i]))
5451 continue;
5452 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5453 continue;
5454 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5455 n_out - (i + 1)) != -1)
5456 continue;
5457 isl_seq_gcd(eq + o_div, n_div, &gcd);
5458 if (isl_int_is_zero(gcd))
5459 continue;
5460 if (isl_int_is_one(gcd))
5461 continue;
5463 res = pw_multi_aff_from_map_stride(map, hull,
5464 i, j, gcd);
5465 isl_int_clear(gcd);
5466 return res;
5470 isl_int_clear(gcd);
5471 isl_basic_map_free(hull);
5472 return pw_multi_aff_from_map_check_div(map);
5473 error:
5474 isl_map_free(map);
5475 isl_basic_map_free(hull);
5476 return NULL;
5479 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5481 * As a special case, we first check if all output dimensions are uniquely
5482 * defined in terms of the parameters and input dimensions over the entire
5483 * domain. If so, we extract the desired isl_pw_multi_aff directly
5484 * from the affine hull of "map" and its domain.
5486 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5487 * special cases.
5489 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5491 isl_bool sv;
5492 isl_size n;
5493 isl_basic_map *hull;
5495 n = isl_map_n_basic_map(map);
5496 if (n < 0)
5497 goto error;
5499 if (n == 1) {
5500 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5501 hull = isl_basic_map_plain_affine_hull(hull);
5502 sv = isl_basic_map_plain_is_single_valued(hull);
5503 if (sv >= 0 && sv)
5504 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5505 hull);
5506 isl_basic_map_free(hull);
5508 map = isl_map_detect_equalities(map);
5509 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5510 sv = isl_basic_map_plain_is_single_valued(hull);
5511 if (sv >= 0 && sv)
5512 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5513 if (sv >= 0)
5514 return pw_multi_aff_from_map_check_strides(map, hull);
5515 isl_basic_map_free(hull);
5516 error:
5517 isl_map_free(map);
5518 return NULL;
5521 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5522 * but is considered as a function on an isl_map when exported.
5524 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5526 return isl_pw_multi_aff_from_map(map);
5529 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5531 return isl_pw_multi_aff_from_map(set);
5534 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5535 * but is considered as a function on an isl_set when exported.
5537 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5539 return isl_pw_multi_aff_from_set(set);
5542 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5543 * add it to *user.
5545 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5547 isl_union_pw_multi_aff **upma = user;
5548 isl_pw_multi_aff *pma;
5550 pma = isl_pw_multi_aff_from_map(map);
5551 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5553 return *upma ? isl_stat_ok : isl_stat_error;
5556 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5557 * domain.
5559 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5560 __isl_take isl_aff *aff)
5562 isl_multi_aff *ma;
5563 isl_pw_multi_aff *pma;
5565 ma = isl_multi_aff_from_aff(aff);
5566 pma = isl_pw_multi_aff_from_multi_aff(ma);
5567 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5570 /* Try and create an isl_union_pw_multi_aff that is equivalent
5571 * to the given isl_union_map.
5572 * The isl_union_map is required to be single-valued in each space.
5573 * Otherwise, an error is produced.
5575 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5576 __isl_take isl_union_map *umap)
5578 isl_space *space;
5579 isl_union_pw_multi_aff *upma;
5581 space = isl_union_map_get_space(umap);
5582 upma = isl_union_pw_multi_aff_empty(space);
5583 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5584 upma = isl_union_pw_multi_aff_free(upma);
5585 isl_union_map_free(umap);
5587 return upma;
5590 /* This function performs the same operation as
5591 * isl_union_pw_multi_aff_from_union_map,
5592 * but is considered as a function on an isl_union_map when exported.
5594 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5595 __isl_take isl_union_map *umap)
5597 return isl_union_pw_multi_aff_from_union_map(umap);
5600 /* Try and create an isl_union_pw_multi_aff that is equivalent
5601 * to the given isl_union_set.
5602 * The isl_union_set is required to be a singleton in each space.
5603 * Otherwise, an error is produced.
5605 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5606 __isl_take isl_union_set *uset)
5608 return isl_union_pw_multi_aff_from_union_map(uset);
5611 /* Return the piecewise affine expression "set ? 1 : 0".
5613 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5615 isl_pw_aff *pa;
5616 isl_space *space = isl_set_get_space(set);
5617 isl_local_space *ls = isl_local_space_from_space(space);
5618 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5619 isl_aff *one = isl_aff_zero_on_domain(ls);
5621 one = isl_aff_add_constant_si(one, 1);
5622 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5623 set = isl_set_complement(set);
5624 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5626 return pa;
5629 /* Plug in "subs" for dimension "type", "pos" of "aff".
5631 * Let i be the dimension to replace and let "subs" be of the form
5633 * f/d
5635 * and "aff" of the form
5637 * (a i + g)/m
5639 * The result is
5641 * (a f + d g')/(m d)
5643 * where g' is the result of plugging in "subs" in each of the integer
5644 * divisions in g.
5646 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5647 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5649 isl_ctx *ctx;
5650 isl_int v;
5651 isl_size n_div;
5653 aff = isl_aff_cow(aff);
5654 if (!aff || !subs)
5655 return isl_aff_free(aff);
5657 ctx = isl_aff_get_ctx(aff);
5658 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5659 isl_die(ctx, isl_error_invalid,
5660 "spaces don't match", return isl_aff_free(aff));
5661 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5662 if (n_div < 0)
5663 return isl_aff_free(aff);
5664 if (n_div != 0)
5665 isl_die(ctx, isl_error_unsupported,
5666 "cannot handle divs yet", return isl_aff_free(aff));
5668 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5669 if (!aff->ls)
5670 return isl_aff_free(aff);
5672 aff->v = isl_vec_cow(aff->v);
5673 if (!aff->v)
5674 return isl_aff_free(aff);
5676 pos += isl_local_space_offset(aff->ls, type);
5678 isl_int_init(v);
5679 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5680 aff->v->size, subs->v->size, v);
5681 isl_int_clear(v);
5683 return aff;
5686 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5687 * expressions in "maff".
5689 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5690 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5691 __isl_keep isl_aff *subs)
5693 isl_size n;
5694 int i;
5696 maff = isl_multi_aff_cow(maff);
5697 n = isl_multi_aff_size(maff);
5698 if (n < 0 || !subs)
5699 return isl_multi_aff_free(maff);
5701 if (type == isl_dim_in)
5702 type = isl_dim_set;
5704 for (i = 0; i < n; ++i) {
5705 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5706 type, pos, subs);
5707 if (!maff->u.p[i])
5708 return isl_multi_aff_free(maff);
5711 return maff;
5714 /* Plug in "subs" for input dimension "pos" of "pma".
5716 * pma is of the form
5718 * A_i(v) -> M_i(v)
5720 * while subs is of the form
5722 * v' = B_j(v) -> S_j
5724 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5725 * has a contribution in the result, in particular
5727 * C_ij(S_j) -> M_i(S_j)
5729 * Note that plugging in S_j in C_ij may also result in an empty set
5730 * and this contribution should simply be discarded.
5732 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5733 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5734 __isl_keep isl_pw_aff *subs)
5736 int i, j, n;
5737 isl_pw_multi_aff *res;
5739 if (!pma || !subs)
5740 return isl_pw_multi_aff_free(pma);
5742 n = pma->n * subs->n;
5743 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5745 for (i = 0; i < pma->n; ++i) {
5746 for (j = 0; j < subs->n; ++j) {
5747 isl_set *common;
5748 isl_multi_aff *res_ij;
5749 int empty;
5751 common = isl_set_intersect(
5752 isl_set_copy(pma->p[i].set),
5753 isl_set_copy(subs->p[j].set));
5754 common = isl_set_substitute(common,
5755 pos, subs->p[j].aff);
5756 empty = isl_set_plain_is_empty(common);
5757 if (empty < 0 || empty) {
5758 isl_set_free(common);
5759 if (empty < 0)
5760 goto error;
5761 continue;
5764 res_ij = isl_multi_aff_substitute(
5765 isl_multi_aff_copy(pma->p[i].maff),
5766 isl_dim_in, pos, subs->p[j].aff);
5768 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5772 isl_pw_multi_aff_free(pma);
5773 return res;
5774 error:
5775 isl_pw_multi_aff_free(pma);
5776 isl_pw_multi_aff_free(res);
5777 return NULL;
5780 /* Compute the preimage of a range of dimensions in the affine expression "src"
5781 * under "ma" and put the result in "dst". The number of dimensions in "src"
5782 * that precede the range is given by "n_before". The number of dimensions
5783 * in the range is given by the number of output dimensions of "ma".
5784 * The number of dimensions that follow the range is given by "n_after".
5785 * If "has_denom" is set (to one),
5786 * then "src" and "dst" have an extra initial denominator.
5787 * "n_div_ma" is the number of existentials in "ma"
5788 * "n_div_bset" is the number of existentials in "src"
5789 * The resulting "dst" (which is assumed to have been allocated by
5790 * the caller) contains coefficients for both sets of existentials,
5791 * first those in "ma" and then those in "src".
5792 * f, c1, c2 and g are temporary objects that have been initialized
5793 * by the caller.
5795 * Let src represent the expression
5797 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5799 * and let ma represent the expressions
5801 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5803 * We start out with the following expression for dst:
5805 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5807 * with the multiplication factor f initially equal to 1
5808 * and f \sum_i b_i v_i kept separately.
5809 * For each x_i that we substitute, we multiply the numerator
5810 * (and denominator) of dst by c_1 = m_i and add the numerator
5811 * of the x_i expression multiplied by c_2 = f b_i,
5812 * after removing the common factors of c_1 and c_2.
5813 * The multiplication factor f also needs to be multiplied by c_1
5814 * for the next x_j, j > i.
5816 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5817 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5818 int n_div_ma, int n_div_bmap,
5819 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5821 int i;
5822 isl_size n_param, n_in, n_out;
5823 int o_dst, o_src;
5825 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5826 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5827 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5828 if (n_param < 0 || n_in < 0 || n_out < 0)
5829 return isl_stat_error;
5831 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5832 o_dst = o_src = has_denom + 1 + n_param + n_before;
5833 isl_seq_clr(dst + o_dst, n_in);
5834 o_dst += n_in;
5835 o_src += n_out;
5836 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5837 o_dst += n_after;
5838 o_src += n_after;
5839 isl_seq_clr(dst + o_dst, n_div_ma);
5840 o_dst += n_div_ma;
5841 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5843 isl_int_set_si(f, 1);
5845 for (i = 0; i < n_out; ++i) {
5846 int offset = has_denom + 1 + n_param + n_before + i;
5848 if (isl_int_is_zero(src[offset]))
5849 continue;
5850 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5851 isl_int_mul(c2, f, src[offset]);
5852 isl_int_gcd(g, c1, c2);
5853 isl_int_divexact(c1, c1, g);
5854 isl_int_divexact(c2, c2, g);
5856 isl_int_mul(f, f, c1);
5857 o_dst = has_denom;
5858 o_src = 1;
5859 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5860 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5861 o_dst += 1 + n_param;
5862 o_src += 1 + n_param;
5863 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5864 o_dst += n_before;
5865 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5866 c2, ma->u.p[i]->v->el + o_src, n_in);
5867 o_dst += n_in;
5868 o_src += n_in;
5869 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5870 o_dst += n_after;
5871 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5872 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5873 o_dst += n_div_ma;
5874 o_src += n_div_ma;
5875 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5876 if (has_denom)
5877 isl_int_mul(dst[0], dst[0], c1);
5880 return isl_stat_ok;
5883 /* Compute the pullback of "aff" by the function represented by "ma".
5884 * In other words, plug in "ma" in "aff". The result is an affine expression
5885 * defined over the domain space of "ma".
5887 * If "aff" is represented by
5889 * (a(p) + b x + c(divs))/d
5891 * and ma is represented by
5893 * x = D(p) + F(y) + G(divs')
5895 * then the result is
5897 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5899 * The divs in the local space of the input are similarly adjusted
5900 * through a call to isl_local_space_preimage_multi_aff.
5902 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5903 __isl_take isl_multi_aff *ma)
5905 isl_aff *res = NULL;
5906 isl_local_space *ls;
5907 isl_size n_div_aff, n_div_ma;
5908 isl_int f, c1, c2, g;
5910 ma = isl_multi_aff_align_divs(ma);
5911 if (!aff || !ma)
5912 goto error;
5914 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5915 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5916 if (n_div_aff < 0 || n_div_ma < 0)
5917 goto error;
5919 ls = isl_aff_get_domain_local_space(aff);
5920 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5921 res = isl_aff_alloc(ls);
5922 if (!res)
5923 goto error;
5925 isl_int_init(f);
5926 isl_int_init(c1);
5927 isl_int_init(c2);
5928 isl_int_init(g);
5930 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5931 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5932 res = isl_aff_free(res);
5934 isl_int_clear(f);
5935 isl_int_clear(c1);
5936 isl_int_clear(c2);
5937 isl_int_clear(g);
5939 isl_aff_free(aff);
5940 isl_multi_aff_free(ma);
5941 res = isl_aff_normalize(res);
5942 return res;
5943 error:
5944 isl_aff_free(aff);
5945 isl_multi_aff_free(ma);
5946 isl_aff_free(res);
5947 return NULL;
5950 /* Compute the pullback of "aff1" by the function represented by "aff2".
5951 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5952 * defined over the domain space of "aff1".
5954 * The domain of "aff1" should match the range of "aff2", which means
5955 * that it should be single-dimensional.
5957 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5958 __isl_take isl_aff *aff2)
5960 isl_multi_aff *ma;
5962 ma = isl_multi_aff_from_aff(aff2);
5963 return isl_aff_pullback_multi_aff(aff1, ma);
5966 /* Compute the pullback of "ma1" by the function represented by "ma2".
5967 * In other words, plug in "ma2" in "ma1".
5969 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5970 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5972 int i;
5973 isl_space *space = NULL;
5975 isl_multi_aff_align_params_bin(&ma1, &ma2);
5976 ma2 = isl_multi_aff_align_divs(ma2);
5977 ma1 = isl_multi_aff_cow(ma1);
5978 if (!ma1 || !ma2)
5979 goto error;
5981 space = isl_space_join(isl_multi_aff_get_space(ma2),
5982 isl_multi_aff_get_space(ma1));
5984 for (i = 0; i < ma1->n; ++i) {
5985 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5986 isl_multi_aff_copy(ma2));
5987 if (!ma1->u.p[i])
5988 goto error;
5991 ma1 = isl_multi_aff_reset_space(ma1, space);
5992 isl_multi_aff_free(ma2);
5993 return ma1;
5994 error:
5995 isl_space_free(space);
5996 isl_multi_aff_free(ma2);
5997 isl_multi_aff_free(ma1);
5998 return NULL;
6001 /* Extend the local space of "dst" to include the divs
6002 * in the local space of "src".
6004 * If "src" does not have any divs or if the local spaces of "dst" and
6005 * "src" are the same, then no extension is required.
6007 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6008 __isl_keep isl_aff *src)
6010 isl_ctx *ctx;
6011 isl_size src_n_div, dst_n_div;
6012 int *exp1 = NULL;
6013 int *exp2 = NULL;
6014 isl_bool equal;
6015 isl_mat *div;
6017 if (!src || !dst)
6018 return isl_aff_free(dst);
6020 ctx = isl_aff_get_ctx(src);
6021 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6022 if (equal < 0)
6023 return isl_aff_free(dst);
6024 if (!equal)
6025 isl_die(ctx, isl_error_invalid,
6026 "spaces don't match", goto error);
6028 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6029 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6030 if (src_n_div == 0)
6031 return dst;
6032 equal = isl_local_space_is_equal(src->ls, dst->ls);
6033 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6034 return isl_aff_free(dst);
6035 if (equal)
6036 return dst;
6038 exp1 = isl_alloc_array(ctx, int, src_n_div);
6039 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6040 if (!exp1 || (dst_n_div && !exp2))
6041 goto error;
6043 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6044 dst = isl_aff_expand_divs(dst, div, exp2);
6045 free(exp1);
6046 free(exp2);
6048 return dst;
6049 error:
6050 free(exp1);
6051 free(exp2);
6052 return isl_aff_free(dst);
6055 /* Adjust the local spaces of the affine expressions in "maff"
6056 * such that they all have the save divs.
6058 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6059 __isl_take isl_multi_aff *maff)
6061 int i;
6063 if (!maff)
6064 return NULL;
6065 if (maff->n == 0)
6066 return maff;
6067 maff = isl_multi_aff_cow(maff);
6068 if (!maff)
6069 return NULL;
6071 for (i = 1; i < maff->n; ++i)
6072 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
6073 for (i = 1; i < maff->n; ++i) {
6074 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
6075 if (!maff->u.p[i])
6076 return isl_multi_aff_free(maff);
6079 return maff;
6082 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6084 aff = isl_aff_cow(aff);
6085 if (!aff)
6086 return NULL;
6088 aff->ls = isl_local_space_lift(aff->ls);
6089 if (!aff->ls)
6090 return isl_aff_free(aff);
6092 return aff;
6095 /* Lift "maff" to a space with extra dimensions such that the result
6096 * has no more existentially quantified variables.
6097 * If "ls" is not NULL, then *ls is assigned the local space that lies
6098 * at the basis of the lifting applied to "maff".
6100 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6101 __isl_give isl_local_space **ls)
6103 int i;
6104 isl_space *space;
6105 isl_size n_div;
6107 if (ls)
6108 *ls = NULL;
6110 if (!maff)
6111 return NULL;
6113 if (maff->n == 0) {
6114 if (ls) {
6115 isl_space *space = isl_multi_aff_get_domain_space(maff);
6116 *ls = isl_local_space_from_space(space);
6117 if (!*ls)
6118 return isl_multi_aff_free(maff);
6120 return maff;
6123 maff = isl_multi_aff_cow(maff);
6124 maff = isl_multi_aff_align_divs(maff);
6125 if (!maff)
6126 return NULL;
6128 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
6129 if (n_div < 0)
6130 return isl_multi_aff_free(maff);
6131 space = isl_multi_aff_get_space(maff);
6132 space = isl_space_lift(isl_space_domain(space), n_div);
6133 space = isl_space_extend_domain_with_range(space,
6134 isl_multi_aff_get_space(maff));
6135 if (!space)
6136 return isl_multi_aff_free(maff);
6137 isl_space_free(maff->space);
6138 maff->space = space;
6140 if (ls) {
6141 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
6142 if (!*ls)
6143 return isl_multi_aff_free(maff);
6146 for (i = 0; i < maff->n; ++i) {
6147 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
6148 if (!maff->u.p[i])
6149 goto error;
6152 return maff;
6153 error:
6154 if (ls)
6155 isl_local_space_free(*ls);
6156 return isl_multi_aff_free(maff);
6159 #undef TYPE
6160 #define TYPE isl_pw_multi_aff
6161 static
6162 #include "check_type_range_templ.c"
6164 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6166 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6167 __isl_keep isl_pw_multi_aff *pma, int pos)
6169 int i;
6170 isl_size n_out;
6171 isl_space *space;
6172 isl_pw_aff *pa;
6174 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6175 return NULL;
6177 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6178 if (n_out < 0)
6179 return NULL;
6181 space = isl_pw_multi_aff_get_space(pma);
6182 space = isl_space_drop_dims(space, isl_dim_out,
6183 pos + 1, n_out - pos - 1);
6184 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6186 pa = isl_pw_aff_alloc_size(space, pma->n);
6187 for (i = 0; i < pma->n; ++i) {
6188 isl_aff *aff;
6189 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6190 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6193 return pa;
6196 /* This is an alternative name for the function above.
6198 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6199 __isl_keep isl_pw_multi_aff *pma, int pos)
6201 return isl_pw_multi_aff_get_at(pma, pos);
6204 /* Return an isl_pw_multi_aff with the given "set" as domain and
6205 * an unnamed zero-dimensional range.
6207 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6208 __isl_take isl_set *set)
6210 isl_multi_aff *ma;
6211 isl_space *space;
6213 space = isl_set_get_space(set);
6214 space = isl_space_from_domain(space);
6215 ma = isl_multi_aff_zero(space);
6216 return isl_pw_multi_aff_alloc(set, ma);
6219 /* Add an isl_pw_multi_aff with the given "set" as domain and
6220 * an unnamed zero-dimensional range to *user.
6222 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6223 void *user)
6225 isl_union_pw_multi_aff **upma = user;
6226 isl_pw_multi_aff *pma;
6228 pma = isl_pw_multi_aff_from_domain(set);
6229 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6231 return isl_stat_ok;
6234 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6235 * an unnamed zero-dimensional range.
6237 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6238 __isl_take isl_union_set *uset)
6240 isl_space *space;
6241 isl_union_pw_multi_aff *upma;
6243 if (!uset)
6244 return NULL;
6246 space = isl_union_set_get_space(uset);
6247 upma = isl_union_pw_multi_aff_empty(space);
6249 if (isl_union_set_foreach_set(uset,
6250 &add_pw_multi_aff_from_domain, &upma) < 0)
6251 goto error;
6253 isl_union_set_free(uset);
6254 return upma;
6255 error:
6256 isl_union_set_free(uset);
6257 isl_union_pw_multi_aff_free(upma);
6258 return NULL;
6261 /* Local data for bin_entry and the callback "fn".
6263 struct isl_union_pw_multi_aff_bin_data {
6264 isl_union_pw_multi_aff *upma2;
6265 isl_union_pw_multi_aff *res;
6266 isl_pw_multi_aff *pma;
6267 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6270 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6271 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6273 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6275 struct isl_union_pw_multi_aff_bin_data *data = user;
6276 isl_stat r;
6278 data->pma = pma;
6279 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6280 data->fn, data);
6281 isl_pw_multi_aff_free(pma);
6283 return r;
6286 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6287 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6288 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6289 * as *entry. The callback should adjust data->res if desired.
6291 static __isl_give isl_union_pw_multi_aff *bin_op(
6292 __isl_take isl_union_pw_multi_aff *upma1,
6293 __isl_take isl_union_pw_multi_aff *upma2,
6294 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6296 isl_space *space;
6297 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6299 space = isl_union_pw_multi_aff_get_space(upma2);
6300 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6301 space = isl_union_pw_multi_aff_get_space(upma1);
6302 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6304 if (!upma1 || !upma2)
6305 goto error;
6307 data.upma2 = upma2;
6308 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6309 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6310 &bin_entry, &data) < 0)
6311 goto error;
6313 isl_union_pw_multi_aff_free(upma1);
6314 isl_union_pw_multi_aff_free(upma2);
6315 return data.res;
6316 error:
6317 isl_union_pw_multi_aff_free(upma1);
6318 isl_union_pw_multi_aff_free(upma2);
6319 isl_union_pw_multi_aff_free(data.res);
6320 return NULL;
6323 /* Given two isl_pw_multi_affs A -> B and C -> D,
6324 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6326 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6327 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6329 isl_space *space;
6331 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6332 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6333 isl_pw_multi_aff_get_space(pma2));
6334 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6335 &isl_multi_aff_range_product);
6338 /* Given two isl_pw_multi_affs A -> B and C -> D,
6339 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6341 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6342 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6344 isl_space *space;
6346 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6347 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6348 isl_pw_multi_aff_get_space(pma2));
6349 space = isl_space_flatten_range(space);
6350 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6351 &isl_multi_aff_flat_range_product);
6354 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6355 * to compute some form of range product and add the result to data->res.
6357 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6358 __isl_give isl_pw_multi_aff *(*range_product)(
6359 __isl_take isl_pw_multi_aff *pma1,
6360 __isl_take isl_pw_multi_aff *pma2),
6361 void *user)
6363 struct isl_union_pw_multi_aff_bin_data *data = user;
6364 isl_bool match;
6365 isl_space *space1, *space2;
6367 space1 = isl_pw_multi_aff_peek_space(data->pma);
6368 space2 = isl_pw_multi_aff_peek_space(pma2);
6369 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6370 space2, isl_dim_in);
6371 if (match < 0 || !match) {
6372 isl_pw_multi_aff_free(pma2);
6373 return match < 0 ? isl_stat_error : isl_stat_ok;
6376 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6378 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6380 return isl_stat_ok;
6383 /* If data->pma and "pma2" have the same domain space, then compute
6384 * their flat range product and add the result to data->res.
6386 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6387 void *user)
6389 return gen_range_product_entry(pma2,
6390 &isl_pw_multi_aff_flat_range_product, user);
6393 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6394 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6396 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6397 __isl_take isl_union_pw_multi_aff *upma1,
6398 __isl_take isl_union_pw_multi_aff *upma2)
6400 return bin_op(upma1, upma2, &flat_range_product_entry);
6403 /* If data->pma and "pma2" have the same domain space, then compute
6404 * their range product and add the result to data->res.
6406 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6407 void *user)
6409 return gen_range_product_entry(pma2,
6410 &isl_pw_multi_aff_range_product, user);
6413 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6414 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6416 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6417 __isl_take isl_union_pw_multi_aff *upma1,
6418 __isl_take isl_union_pw_multi_aff *upma2)
6420 return bin_op(upma1, upma2, &range_product_entry);
6423 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6424 * The parameters are assumed to have been aligned.
6426 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6427 * except that it works on two different isl_pw_* types.
6429 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6430 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6431 __isl_take isl_pw_aff *pa)
6433 int i, j, n;
6434 isl_pw_multi_aff *res = NULL;
6436 if (!pma || !pa)
6437 goto error;
6439 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6440 pa->dim, isl_dim_in))
6441 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6442 "domains don't match", goto error);
6443 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6444 goto error;
6446 n = pma->n * pa->n;
6447 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6449 for (i = 0; i < pma->n; ++i) {
6450 for (j = 0; j < pa->n; ++j) {
6451 isl_set *common;
6452 isl_multi_aff *res_ij;
6453 int empty;
6455 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6456 isl_set_copy(pa->p[j].set));
6457 empty = isl_set_plain_is_empty(common);
6458 if (empty < 0 || empty) {
6459 isl_set_free(common);
6460 if (empty < 0)
6461 goto error;
6462 continue;
6465 res_ij = isl_multi_aff_set_aff(
6466 isl_multi_aff_copy(pma->p[i].maff), pos,
6467 isl_aff_copy(pa->p[j].aff));
6468 res_ij = isl_multi_aff_gist(res_ij,
6469 isl_set_copy(common));
6471 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6475 isl_pw_multi_aff_free(pma);
6476 isl_pw_aff_free(pa);
6477 return res;
6478 error:
6479 isl_pw_multi_aff_free(pma);
6480 isl_pw_aff_free(pa);
6481 return isl_pw_multi_aff_free(res);
6484 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6486 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6487 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6488 __isl_take isl_pw_aff *pa)
6490 isl_bool equal_params;
6492 if (!pma || !pa)
6493 goto error;
6494 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6495 if (equal_params < 0)
6496 goto error;
6497 if (equal_params)
6498 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6499 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6500 isl_pw_aff_check_named_params(pa) < 0)
6501 goto error;
6502 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6503 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6504 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6505 error:
6506 isl_pw_multi_aff_free(pma);
6507 isl_pw_aff_free(pa);
6508 return NULL;
6511 /* Do the parameters of "pa" match those of "space"?
6513 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6514 __isl_keep isl_space *space)
6516 isl_space *pa_space;
6517 isl_bool match;
6519 if (!pa || !space)
6520 return isl_bool_error;
6522 pa_space = isl_pw_aff_get_space(pa);
6524 match = isl_space_has_equal_params(space, pa_space);
6526 isl_space_free(pa_space);
6527 return match;
6530 /* Check that the domain space of "pa" matches "space".
6532 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6533 __isl_keep isl_space *space)
6535 isl_space *pa_space;
6536 isl_bool match;
6538 if (!pa || !space)
6539 return isl_stat_error;
6541 pa_space = isl_pw_aff_get_space(pa);
6543 match = isl_space_has_equal_params(space, pa_space);
6544 if (match < 0)
6545 goto error;
6546 if (!match)
6547 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6548 "parameters don't match", goto error);
6549 match = isl_space_tuple_is_equal(space, isl_dim_in,
6550 pa_space, isl_dim_in);
6551 if (match < 0)
6552 goto error;
6553 if (!match)
6554 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6555 "domains don't match", goto error);
6556 isl_space_free(pa_space);
6557 return isl_stat_ok;
6558 error:
6559 isl_space_free(pa_space);
6560 return isl_stat_error;
6563 #undef BASE
6564 #define BASE pw_aff
6565 #undef DOMBASE
6566 #define DOMBASE set
6568 #include <isl_multi_explicit_domain.c>
6569 #include <isl_multi_pw_aff_explicit_domain.c>
6570 #include <isl_multi_templ.c>
6571 #include <isl_multi_un_op_templ.c>
6572 #include <isl_multi_bin_val_templ.c>
6573 #include <isl_multi_add_constant_templ.c>
6574 #include <isl_multi_apply_set.c>
6575 #include <isl_multi_arith_templ.c>
6576 #include <isl_multi_bind_templ.c>
6577 #include <isl_multi_bind_domain_templ.c>
6578 #include <isl_multi_coalesce.c>
6579 #include <isl_multi_domain_templ.c>
6580 #include <isl_multi_dim_id_templ.c>
6581 #include <isl_multi_dims.c>
6582 #include <isl_multi_from_base_templ.c>
6583 #include <isl_multi_gist.c>
6584 #include <isl_multi_hash.c>
6585 #include <isl_multi_identity_templ.c>
6586 #include <isl_multi_align_set.c>
6587 #include <isl_multi_insert_domain_templ.c>
6588 #include <isl_multi_intersect.c>
6589 #include <isl_multi_min_max_templ.c>
6590 #include <isl_multi_move_dims_templ.c>
6591 #include <isl_multi_nan_templ.c>
6592 #include <isl_multi_param_templ.c>
6593 #include <isl_multi_product_templ.c>
6594 #include <isl_multi_splice_templ.c>
6595 #include <isl_multi_tuple_id_templ.c>
6596 #include <isl_multi_union_add_templ.c>
6597 #include <isl_multi_zero_templ.c>
6598 #include <isl_multi_unbind_params_templ.c>
6600 /* Is every element of "mpa" defined over a single universe domain?
6602 isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
6604 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
6607 /* Given that every element of "mpa" is defined over a single universe domain,
6608 * return the corresponding base expressions.
6610 __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
6611 __isl_take isl_multi_pw_aff *mpa)
6613 int i;
6614 isl_size n;
6615 isl_multi_aff *ma;
6617 n = isl_multi_pw_aff_size(mpa);
6618 if (n < 0)
6619 mpa = isl_multi_pw_aff_free(mpa);
6620 ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
6621 for (i = 0; i < n; ++i) {
6622 isl_aff *aff;
6624 aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
6625 ma = isl_multi_aff_set_aff(ma, i, aff);
6627 isl_multi_pw_aff_free(mpa);
6628 return ma;
6631 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6632 * with this explicit domain.
6634 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6635 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6637 isl_set *dom;
6639 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6640 return map;
6642 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6643 map = isl_map_intersect_domain(map, dom);
6645 return map;
6648 /* Are all elements of "mpa" piecewise constants?
6650 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6652 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6655 /* Does "mpa" have a non-trivial explicit domain?
6657 * The explicit domain, if present, is trivial if it represents
6658 * an (obviously) universe set.
6660 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6661 __isl_keep isl_multi_pw_aff *mpa)
6663 if (!mpa)
6664 return isl_bool_error;
6665 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6666 return isl_bool_false;
6667 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6670 #undef BASE
6671 #define BASE set
6673 #include "isl_opt_mpa_templ.c"
6675 /* Compute the minima of the set dimensions as a function of the
6676 * parameters, but independently of the other set dimensions.
6678 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6680 return set_opt_mpa(set, &isl_set_dim_min);
6683 /* Compute the maxima of the set dimensions as a function of the
6684 * parameters, but independently of the other set dimensions.
6686 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6688 return set_opt_mpa(set, &isl_set_dim_max);
6691 #undef BASE
6692 #define BASE map
6694 #include "isl_opt_mpa_templ.c"
6696 /* Compute the minima of the output dimensions as a function of the
6697 * parameters and input dimensions, but independently of
6698 * the other output dimensions.
6700 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6702 return map_opt_mpa(map, &isl_map_dim_min);
6705 /* Compute the maxima of the output dimensions as a function of the
6706 * parameters and input dimensions, but independently of
6707 * the other output dimensions.
6709 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6711 return map_opt_mpa(map, &isl_map_dim_max);
6714 #undef TYPE
6715 #define TYPE isl_pw_multi_aff
6716 #include "isl_type_check_match_range_multi_val.c"
6718 /* Apply "fn" to the base expressions of "pma" and "mv".
6720 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_op_multi_val(
6721 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv,
6722 __isl_give isl_multi_aff *(*fn)(__isl_take isl_multi_aff *ma,
6723 __isl_take isl_multi_val *mv))
6725 int i;
6726 isl_size n;
6728 if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
6729 goto error;
6731 n = isl_pw_multi_aff_n_piece(pma);
6732 if (n < 0)
6733 goto error;
6735 for (i = 0; i < n; ++i) {
6736 isl_multi_aff *ma;
6738 ma = isl_pw_multi_aff_take_base_at(pma, i);
6739 ma = fn(ma, isl_multi_val_copy(mv));
6740 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
6743 isl_multi_val_free(mv);
6744 return pma;
6745 error:
6746 isl_multi_val_free(mv);
6747 isl_pw_multi_aff_free(pma);
6748 return NULL;
6751 /* Scale the elements of "pma" by the corresponding elements of "mv".
6753 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6754 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6756 return isl_pw_multi_aff_op_multi_val(pma, mv,
6757 &isl_multi_aff_scale_multi_val);
6760 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6762 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_multi_val(
6763 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6765 return isl_pw_multi_aff_op_multi_val(pma, mv,
6766 &isl_multi_aff_scale_down_multi_val);
6769 /* This function is called for each entry of an isl_union_pw_multi_aff.
6770 * If the space of the entry matches that of data->mv,
6771 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6772 * Otherwise, return an empty isl_pw_multi_aff.
6774 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6775 __isl_take isl_pw_multi_aff *pma, void *user)
6777 isl_bool equal;
6778 isl_multi_val *mv = user;
6780 equal = isl_pw_multi_aff_match_range_multi_val(pma, mv);
6781 if (equal < 0)
6782 return isl_pw_multi_aff_free(pma);
6783 if (!equal) {
6784 isl_space *space = isl_pw_multi_aff_get_space(pma);
6785 isl_pw_multi_aff_free(pma);
6786 return isl_pw_multi_aff_empty(space);
6789 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6792 /* Scale the elements of "upma" by the corresponding elements of "mv",
6793 * for those entries that match the space of "mv".
6795 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6796 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6798 struct isl_union_pw_multi_aff_transform_control control = {
6799 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6800 .fn_user = mv,
6803 upma = isl_union_pw_multi_aff_align_params(upma,
6804 isl_multi_val_get_space(mv));
6805 mv = isl_multi_val_align_params(mv,
6806 isl_union_pw_multi_aff_get_space(upma));
6807 if (!upma || !mv)
6808 goto error;
6810 return isl_union_pw_multi_aff_transform(upma, &control);
6812 isl_multi_val_free(mv);
6813 return upma;
6814 error:
6815 isl_multi_val_free(mv);
6816 isl_union_pw_multi_aff_free(upma);
6817 return NULL;
6820 /* Construct and return a piecewise multi affine expression
6821 * in the given space with value zero in each of the output dimensions and
6822 * a universe domain.
6824 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6826 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6829 /* Construct and return a piecewise multi affine expression
6830 * that is equal to the given piecewise affine expression.
6832 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6833 __isl_take isl_pw_aff *pa)
6835 int i;
6836 isl_space *space;
6837 isl_pw_multi_aff *pma;
6839 if (!pa)
6840 return NULL;
6842 space = isl_pw_aff_get_space(pa);
6843 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6845 for (i = 0; i < pa->n; ++i) {
6846 isl_set *set;
6847 isl_multi_aff *ma;
6849 set = isl_set_copy(pa->p[i].set);
6850 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6851 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6854 isl_pw_aff_free(pa);
6855 return pma;
6858 /* Construct and return a piecewise multi affine expression
6859 * that is equal to the given multi piecewise affine expression
6860 * on the shared domain of the piecewise affine expressions,
6861 * in the special case of a 0D multi piecewise affine expression.
6863 * Create a piecewise multi affine expression with the explicit domain of
6864 * the 0D multi piecewise affine expression as domain.
6866 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6867 __isl_take isl_multi_pw_aff *mpa)
6869 isl_space *space;
6870 isl_set *dom;
6871 isl_multi_aff *ma;
6873 space = isl_multi_pw_aff_get_space(mpa);
6874 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6875 isl_multi_pw_aff_free(mpa);
6877 ma = isl_multi_aff_zero(space);
6878 return isl_pw_multi_aff_alloc(dom, ma);
6881 /* Construct and return a piecewise multi affine expression
6882 * that is equal to the given multi piecewise affine expression
6883 * on the shared domain of the piecewise affine expressions.
6885 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6886 __isl_take isl_multi_pw_aff *mpa)
6888 int i;
6889 isl_space *space;
6890 isl_pw_aff *pa;
6891 isl_pw_multi_aff *pma;
6893 if (!mpa)
6894 return NULL;
6896 if (mpa->n == 0)
6897 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6899 space = isl_multi_pw_aff_get_space(mpa);
6900 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6901 pma = isl_pw_multi_aff_from_pw_aff(pa);
6903 for (i = 1; i < mpa->n; ++i) {
6904 isl_pw_multi_aff *pma_i;
6906 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6907 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6908 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6911 pma = isl_pw_multi_aff_reset_space(pma, space);
6913 isl_multi_pw_aff_free(mpa);
6914 return pma;
6917 /* Convenience function that constructs an isl_multi_pw_aff
6918 * directly from an isl_aff.
6920 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6922 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6925 /* Construct and return a multi piecewise affine expression
6926 * that is equal to the given multi affine expression.
6928 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6929 __isl_take isl_multi_aff *ma)
6931 int i;
6932 isl_size n;
6933 isl_multi_pw_aff *mpa;
6935 n = isl_multi_aff_dim(ma, isl_dim_out);
6936 if (n < 0)
6937 ma = isl_multi_aff_free(ma);
6938 if (!ma)
6939 return NULL;
6941 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6943 for (i = 0; i < n; ++i) {
6944 isl_pw_aff *pa;
6946 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6947 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6950 isl_multi_aff_free(ma);
6951 return mpa;
6954 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6955 * but is considered as a function on an isl_multi_aff when exported.
6957 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
6958 __isl_take isl_multi_aff *ma)
6960 return isl_multi_pw_aff_from_multi_aff(ma);
6963 /* Construct and return a multi piecewise affine expression
6964 * that is equal to the given piecewise multi affine expression.
6966 * If the resulting multi piecewise affine expression has
6967 * an explicit domain, then assign it the domain of the input.
6968 * In other cases, the domain is stored in the individual elements.
6970 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6971 __isl_take isl_pw_multi_aff *pma)
6973 int i;
6974 isl_size n;
6975 isl_space *space;
6976 isl_multi_pw_aff *mpa;
6978 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6979 if (n < 0)
6980 pma = isl_pw_multi_aff_free(pma);
6981 space = isl_pw_multi_aff_get_space(pma);
6982 mpa = isl_multi_pw_aff_alloc(space);
6984 for (i = 0; i < n; ++i) {
6985 isl_pw_aff *pa;
6987 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6988 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6990 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6991 isl_set *dom;
6993 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6994 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6997 isl_pw_multi_aff_free(pma);
6998 return mpa;
7001 /* This function performs the same operation as
7002 * isl_multi_pw_aff_from_pw_multi_aff,
7003 * but is considered as a function on an isl_pw_multi_aff when exported.
7005 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
7006 __isl_take isl_pw_multi_aff *pma)
7008 return isl_multi_pw_aff_from_pw_multi_aff(pma);
7011 /* Do "pa1" and "pa2" represent the same function?
7013 * We first check if they are obviously equal.
7014 * If not, we convert them to maps and check if those are equal.
7016 * If "pa1" or "pa2" contain any NaNs, then they are considered
7017 * not to be the same. A NaN is not equal to anything, not even
7018 * to another NaN.
7020 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
7021 __isl_keep isl_pw_aff *pa2)
7023 isl_bool equal;
7024 isl_bool has_nan;
7025 isl_map *map1, *map2;
7027 if (!pa1 || !pa2)
7028 return isl_bool_error;
7030 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
7031 if (equal < 0 || equal)
7032 return equal;
7033 has_nan = either_involves_nan(pa1, pa2);
7034 if (has_nan < 0)
7035 return isl_bool_error;
7036 if (has_nan)
7037 return isl_bool_false;
7039 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
7040 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
7041 equal = isl_map_is_equal(map1, map2);
7042 isl_map_free(map1);
7043 isl_map_free(map2);
7045 return equal;
7048 /* Do "mpa1" and "mpa2" represent the same function?
7050 * Note that we cannot convert the entire isl_multi_pw_aff
7051 * to a map because the domains of the piecewise affine expressions
7052 * may not be the same.
7054 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7055 __isl_keep isl_multi_pw_aff *mpa2)
7057 int i;
7058 isl_bool equal, equal_params;
7060 if (!mpa1 || !mpa2)
7061 return isl_bool_error;
7063 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7064 if (equal_params < 0)
7065 return isl_bool_error;
7066 if (!equal_params) {
7067 if (!isl_space_has_named_params(mpa1->space))
7068 return isl_bool_false;
7069 if (!isl_space_has_named_params(mpa2->space))
7070 return isl_bool_false;
7071 mpa1 = isl_multi_pw_aff_copy(mpa1);
7072 mpa2 = isl_multi_pw_aff_copy(mpa2);
7073 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7074 isl_multi_pw_aff_get_space(mpa2));
7075 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7076 isl_multi_pw_aff_get_space(mpa1));
7077 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7078 isl_multi_pw_aff_free(mpa1);
7079 isl_multi_pw_aff_free(mpa2);
7080 return equal;
7083 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7084 if (equal < 0 || !equal)
7085 return equal;
7087 for (i = 0; i < mpa1->n; ++i) {
7088 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7089 if (equal < 0 || !equal)
7090 return equal;
7093 return isl_bool_true;
7096 /* Do "pma1" and "pma2" represent the same function?
7098 * First check if they are obviously equal.
7099 * If not, then convert them to maps and check if those are equal.
7101 * If "pa1" or "pa2" contain any NaNs, then they are considered
7102 * not to be the same. A NaN is not equal to anything, not even
7103 * to another NaN.
7105 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7106 __isl_keep isl_pw_multi_aff *pma2)
7108 isl_bool equal;
7109 isl_bool has_nan;
7110 isl_map *map1, *map2;
7112 if (!pma1 || !pma2)
7113 return isl_bool_error;
7115 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7116 if (equal < 0 || equal)
7117 return equal;
7118 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7119 if (has_nan >= 0 && !has_nan)
7120 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7121 if (has_nan < 0 || has_nan)
7122 return isl_bool_not(has_nan);
7124 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7125 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7126 equal = isl_map_is_equal(map1, map2);
7127 isl_map_free(map1);
7128 isl_map_free(map2);
7130 return equal;
7133 /* Compute the pullback of "mpa" by the function represented by "ma".
7134 * In other words, plug in "ma" in "mpa".
7136 * The parameters of "mpa" and "ma" are assumed to have been aligned.
7138 * If "mpa" has an explicit domain, then it is this domain
7139 * that needs to undergo a pullback, i.e., a preimage.
7141 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
7142 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7144 int i;
7145 isl_space *space = NULL;
7147 mpa = isl_multi_pw_aff_cow(mpa);
7148 if (!mpa || !ma)
7149 goto error;
7151 space = isl_space_join(isl_multi_aff_get_space(ma),
7152 isl_multi_pw_aff_get_space(mpa));
7153 if (!space)
7154 goto error;
7156 for (i = 0; i < mpa->n; ++i) {
7157 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
7158 isl_multi_aff_copy(ma));
7159 if (!mpa->u.p[i])
7160 goto error;
7162 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7163 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
7164 isl_multi_aff_copy(ma));
7165 if (!mpa->u.dom)
7166 goto error;
7169 isl_multi_aff_free(ma);
7170 isl_space_free(mpa->space);
7171 mpa->space = space;
7172 return mpa;
7173 error:
7174 isl_space_free(space);
7175 isl_multi_pw_aff_free(mpa);
7176 isl_multi_aff_free(ma);
7177 return NULL;
7180 /* Compute the pullback of "mpa" by the function represented by "ma".
7181 * In other words, plug in "ma" in "mpa".
7183 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
7184 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
7186 isl_bool equal_params;
7188 if (!mpa || !ma)
7189 goto error;
7190 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
7191 if (equal_params < 0)
7192 goto error;
7193 if (equal_params)
7194 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7195 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
7196 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
7197 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
7198 error:
7199 isl_multi_pw_aff_free(mpa);
7200 isl_multi_aff_free(ma);
7201 return NULL;
7204 /* Compute the pullback of "mpa" by the function represented by "pma".
7205 * In other words, plug in "pma" in "mpa".
7207 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
7209 * If "mpa" has an explicit domain, then it is this domain
7210 * that needs to undergo a pullback, i.e., a preimage.
7212 static __isl_give isl_multi_pw_aff *
7213 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
7214 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7216 int i;
7217 isl_space *space = NULL;
7219 mpa = isl_multi_pw_aff_cow(mpa);
7220 if (!mpa || !pma)
7221 goto error;
7223 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
7224 isl_multi_pw_aff_get_space(mpa));
7226 for (i = 0; i < mpa->n; ++i) {
7227 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
7228 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
7229 if (!mpa->u.p[i])
7230 goto error;
7232 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7233 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
7234 isl_pw_multi_aff_copy(pma));
7235 if (!mpa->u.dom)
7236 goto error;
7239 isl_pw_multi_aff_free(pma);
7240 isl_space_free(mpa->space);
7241 mpa->space = space;
7242 return mpa;
7243 error:
7244 isl_space_free(space);
7245 isl_multi_pw_aff_free(mpa);
7246 isl_pw_multi_aff_free(pma);
7247 return NULL;
7250 /* Compute the pullback of "mpa" by the function represented by "pma".
7251 * In other words, plug in "pma" in "mpa".
7253 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
7254 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7256 isl_bool equal_params;
7258 if (!mpa || !pma)
7259 goto error;
7260 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
7261 if (equal_params < 0)
7262 goto error;
7263 if (equal_params)
7264 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7265 mpa = isl_multi_pw_aff_align_params(mpa,
7266 isl_pw_multi_aff_get_space(pma));
7267 pma = isl_pw_multi_aff_align_params(pma,
7268 isl_multi_pw_aff_get_space(mpa));
7269 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7270 error:
7271 isl_multi_pw_aff_free(mpa);
7272 isl_pw_multi_aff_free(pma);
7273 return NULL;
7276 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7277 * with the domain of "aff". The domain of the result is the same
7278 * as that of "mpa".
7279 * "mpa" and "aff" are assumed to have been aligned.
7281 * We first extract the parametric constant from "aff", defined
7282 * over the correct domain.
7283 * Then we add the appropriate combinations of the members of "mpa".
7284 * Finally, we add the integer divisions through recursive calls.
7286 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7287 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7289 int i;
7290 isl_size n_in, n_div, n_mpa_in;
7291 isl_space *space;
7292 isl_val *v;
7293 isl_pw_aff *pa;
7294 isl_aff *tmp;
7296 n_in = isl_aff_dim(aff, isl_dim_in);
7297 n_div = isl_aff_dim(aff, isl_dim_div);
7298 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7299 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7300 goto error;
7302 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7303 tmp = isl_aff_copy(aff);
7304 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7305 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7306 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7307 tmp = isl_aff_reset_domain_space(tmp, space);
7308 pa = isl_pw_aff_from_aff(tmp);
7310 for (i = 0; i < n_in; ++i) {
7311 isl_pw_aff *pa_i;
7313 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7314 continue;
7315 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7316 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7317 pa_i = isl_pw_aff_scale_val(pa_i, v);
7318 pa = isl_pw_aff_add(pa, pa_i);
7321 for (i = 0; i < n_div; ++i) {
7322 isl_aff *div;
7323 isl_pw_aff *pa_i;
7325 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7326 continue;
7327 div = isl_aff_get_div(aff, i);
7328 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7329 isl_multi_pw_aff_copy(mpa), div);
7330 pa_i = isl_pw_aff_floor(pa_i);
7331 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7332 pa_i = isl_pw_aff_scale_val(pa_i, v);
7333 pa = isl_pw_aff_add(pa, pa_i);
7336 isl_multi_pw_aff_free(mpa);
7337 isl_aff_free(aff);
7339 return pa;
7340 error:
7341 isl_multi_pw_aff_free(mpa);
7342 isl_aff_free(aff);
7343 return NULL;
7346 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7347 * with the domain of "aff". The domain of the result is the same
7348 * as that of "mpa".
7350 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7351 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7353 isl_bool equal_params;
7355 if (!aff || !mpa)
7356 goto error;
7357 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7358 if (equal_params < 0)
7359 goto error;
7360 if (equal_params)
7361 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7363 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7364 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7366 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7367 error:
7368 isl_aff_free(aff);
7369 isl_multi_pw_aff_free(mpa);
7370 return NULL;
7373 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7374 * with the domain of "pa". The domain of the result is the same
7375 * as that of "mpa".
7376 * "mpa" and "pa" are assumed to have been aligned.
7378 * We consider each piece in turn. Note that the domains of the
7379 * pieces are assumed to be disjoint and they remain disjoint
7380 * after taking the preimage (over the same function).
7382 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7383 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7385 isl_space *space;
7386 isl_pw_aff *res;
7387 int i;
7389 if (!mpa || !pa)
7390 goto error;
7392 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7393 isl_pw_aff_get_space(pa));
7394 res = isl_pw_aff_empty(space);
7396 for (i = 0; i < pa->n; ++i) {
7397 isl_pw_aff *pa_i;
7398 isl_set *domain;
7400 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7401 isl_multi_pw_aff_copy(mpa),
7402 isl_aff_copy(pa->p[i].aff));
7403 domain = isl_set_copy(pa->p[i].set);
7404 domain = isl_set_preimage_multi_pw_aff(domain,
7405 isl_multi_pw_aff_copy(mpa));
7406 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7407 res = isl_pw_aff_add_disjoint(res, pa_i);
7410 isl_pw_aff_free(pa);
7411 isl_multi_pw_aff_free(mpa);
7412 return res;
7413 error:
7414 isl_pw_aff_free(pa);
7415 isl_multi_pw_aff_free(mpa);
7416 return NULL;
7419 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7420 * with the domain of "pa". The domain of the result is the same
7421 * as that of "mpa".
7423 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7424 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7426 isl_bool equal_params;
7428 if (!pa || !mpa)
7429 goto error;
7430 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7431 if (equal_params < 0)
7432 goto error;
7433 if (equal_params)
7434 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7436 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7437 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7439 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7440 error:
7441 isl_pw_aff_free(pa);
7442 isl_multi_pw_aff_free(mpa);
7443 return NULL;
7446 /* Compute the pullback of "pa" by the function represented by "mpa".
7447 * In other words, plug in "mpa" in "pa".
7448 * "pa" and "mpa" are assumed to have been aligned.
7450 * The pullback is computed by applying "pa" to "mpa".
7452 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7453 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7455 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7458 /* Compute the pullback of "pa" by the function represented by "mpa".
7459 * In other words, plug in "mpa" in "pa".
7461 * The pullback is computed by applying "pa" to "mpa".
7463 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7464 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7466 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7469 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7470 * In other words, plug in "mpa2" in "mpa1".
7472 * We pullback each member of "mpa1" in turn.
7474 * If "mpa1" has an explicit domain, then it is this domain
7475 * that needs to undergo a pullback instead, i.e., a preimage.
7477 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7478 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7480 int i;
7481 isl_space *space = NULL;
7483 isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
7484 mpa1 = isl_multi_pw_aff_cow(mpa1);
7485 if (!mpa1 || !mpa2)
7486 goto error;
7488 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7489 isl_multi_pw_aff_get_space(mpa1));
7491 for (i = 0; i < mpa1->n; ++i) {
7492 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7493 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7494 if (!mpa1->u.p[i])
7495 goto error;
7498 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7499 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7500 isl_multi_pw_aff_copy(mpa2));
7501 if (!mpa1->u.dom)
7502 goto error;
7504 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7506 isl_multi_pw_aff_free(mpa2);
7507 return mpa1;
7508 error:
7509 isl_space_free(space);
7510 isl_multi_pw_aff_free(mpa1);
7511 isl_multi_pw_aff_free(mpa2);
7512 return NULL;
7515 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7516 * of "mpa1" and "mpa2" live in the same space, construct map space
7517 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7518 * with this map space as extract argument.
7520 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7521 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7522 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7523 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7525 int match;
7526 isl_space *space1, *space2;
7527 isl_map *res;
7529 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7530 isl_multi_pw_aff_get_space(mpa2));
7531 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7532 isl_multi_pw_aff_get_space(mpa1));
7533 if (!mpa1 || !mpa2)
7534 goto error;
7535 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7536 mpa2->space, isl_dim_out);
7537 if (match < 0)
7538 goto error;
7539 if (!match)
7540 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7541 "range spaces don't match", goto error);
7542 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7543 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7544 space1 = isl_space_map_from_domain_and_range(space1, space2);
7546 res = order(mpa1, mpa2, space1);
7547 isl_multi_pw_aff_free(mpa1);
7548 isl_multi_pw_aff_free(mpa2);
7549 return res;
7550 error:
7551 isl_multi_pw_aff_free(mpa1);
7552 isl_multi_pw_aff_free(mpa2);
7553 return NULL;
7556 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7557 * where the function values are equal. "space" is the space of the result.
7558 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7560 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7561 * in the sequences are equal.
7563 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7564 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7565 __isl_take isl_space *space)
7567 int i;
7568 isl_size n;
7569 isl_map *res;
7571 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7572 if (n < 0)
7573 space = isl_space_free(space);
7574 res = isl_map_universe(space);
7576 for (i = 0; i < n; ++i) {
7577 isl_pw_aff *pa1, *pa2;
7578 isl_map *map;
7580 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7581 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7582 map = isl_pw_aff_eq_map(pa1, pa2);
7583 res = isl_map_intersect(res, map);
7586 return res;
7589 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7590 * where the function values are equal.
7592 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7593 __isl_take isl_multi_pw_aff *mpa2)
7595 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7596 &isl_multi_pw_aff_eq_map_on_space);
7599 /* Intersect "map" with the result of applying "order"
7600 * on two copies of "mpa".
7602 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7603 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7604 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7605 __isl_take isl_multi_pw_aff *mpa2))
7607 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7610 /* Return the subset of "map" where the domain and the range
7611 * have equal "mpa" values.
7613 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7614 __isl_take isl_multi_pw_aff *mpa)
7616 return isl_map_order_at_multi_pw_aff(map, mpa,
7617 &isl_multi_pw_aff_eq_map);
7620 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7621 * where the function values of "mpa1" lexicographically satisfies
7622 * "strict_base"/"base" compared to that of "mpa2".
7623 * "space" is the space of the result.
7624 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7626 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7627 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7628 * when compared to the i-th element of "mpa2" while all previous elements are
7629 * pairwise equal.
7630 * In particular, if i corresponds to the final elements
7631 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7632 * for other values of i.
7633 * If "base" is a strict order, then "base" and "strict_base" are the same.
7635 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7636 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7637 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7638 __isl_take isl_pw_aff *pa2),
7639 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7640 __isl_take isl_pw_aff *pa2),
7641 __isl_take isl_space *space)
7643 int i;
7644 isl_size n;
7645 isl_map *res, *rest;
7647 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7648 if (n < 0)
7649 space = isl_space_free(space);
7650 res = isl_map_empty(isl_space_copy(space));
7651 rest = isl_map_universe(space);
7653 for (i = 0; i < n; ++i) {
7654 int last;
7655 isl_pw_aff *pa1, *pa2;
7656 isl_map *map;
7658 last = i == n - 1;
7660 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7661 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7662 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7663 map = isl_map_intersect(map, isl_map_copy(rest));
7664 res = isl_map_union(res, map);
7666 if (last)
7667 continue;
7669 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7670 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7671 map = isl_pw_aff_eq_map(pa1, pa2);
7672 rest = isl_map_intersect(rest, map);
7675 isl_map_free(rest);
7676 return res;
7679 #undef ORDER
7680 #define ORDER le
7681 #undef STRICT_ORDER
7682 #define STRICT_ORDER lt
7683 #include "isl_aff_lex_templ.c"
7685 #undef ORDER
7686 #define ORDER lt
7687 #undef STRICT_ORDER
7688 #define STRICT_ORDER lt
7689 #include "isl_aff_lex_templ.c"
7691 #undef ORDER
7692 #define ORDER ge
7693 #undef STRICT_ORDER
7694 #define STRICT_ORDER gt
7695 #include "isl_aff_lex_templ.c"
7697 #undef ORDER
7698 #define ORDER gt
7699 #undef STRICT_ORDER
7700 #define STRICT_ORDER gt
7701 #include "isl_aff_lex_templ.c"
7703 /* Compare two isl_affs.
7705 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7706 * than "aff2" and 0 if they are equal.
7708 * The order is fairly arbitrary. We do consider expressions that only involve
7709 * earlier dimensions as "smaller".
7711 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7713 int cmp;
7714 int last1, last2;
7716 if (aff1 == aff2)
7717 return 0;
7719 if (!aff1)
7720 return -1;
7721 if (!aff2)
7722 return 1;
7724 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7725 if (cmp != 0)
7726 return cmp;
7728 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7729 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7730 if (last1 != last2)
7731 return last1 - last2;
7733 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7736 /* Compare two isl_pw_affs.
7738 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7739 * than "pa2" and 0 if they are equal.
7741 * The order is fairly arbitrary. We do consider expressions that only involve
7742 * earlier dimensions as "smaller".
7744 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7745 __isl_keep isl_pw_aff *pa2)
7747 int i;
7748 int cmp;
7750 if (pa1 == pa2)
7751 return 0;
7753 if (!pa1)
7754 return -1;
7755 if (!pa2)
7756 return 1;
7758 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7759 if (cmp != 0)
7760 return cmp;
7762 if (pa1->n != pa2->n)
7763 return pa1->n - pa2->n;
7765 for (i = 0; i < pa1->n; ++i) {
7766 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7767 if (cmp != 0)
7768 return cmp;
7769 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7770 if (cmp != 0)
7771 return cmp;
7774 return 0;
7777 /* Return a piecewise affine expression that is equal to "v" on "domain".
7779 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7780 __isl_take isl_val *v)
7782 isl_space *space;
7783 isl_local_space *ls;
7784 isl_aff *aff;
7786 space = isl_set_get_space(domain);
7787 ls = isl_local_space_from_space(space);
7788 aff = isl_aff_val_on_domain(ls, v);
7790 return isl_pw_aff_alloc(domain, aff);
7793 /* Return a piecewise affine expression that is equal to the parameter
7794 * with identifier "id" on "domain".
7796 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7797 __isl_take isl_set *domain, __isl_take isl_id *id)
7799 isl_space *space;
7800 isl_aff *aff;
7802 space = isl_set_get_space(domain);
7803 space = isl_space_add_param_id(space, isl_id_copy(id));
7804 domain = isl_set_align_params(domain, isl_space_copy(space));
7805 aff = isl_aff_param_on_domain_space_id(space, id);
7807 return isl_pw_aff_alloc(domain, aff);
7810 /* Return a multi affine expression that is equal to "mv" on domain
7811 * space "space".
7813 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7814 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7816 int i;
7817 isl_size n;
7818 isl_space *space2;
7819 isl_local_space *ls;
7820 isl_multi_aff *ma;
7822 n = isl_multi_val_dim(mv, isl_dim_set);
7823 if (!space || n < 0)
7824 goto error;
7826 space2 = isl_multi_val_get_space(mv);
7827 space2 = isl_space_align_params(space2, isl_space_copy(space));
7828 space = isl_space_align_params(space, isl_space_copy(space2));
7829 space = isl_space_map_from_domain_and_range(space, space2);
7830 ma = isl_multi_aff_alloc(isl_space_copy(space));
7831 ls = isl_local_space_from_space(isl_space_domain(space));
7832 for (i = 0; i < n; ++i) {
7833 isl_val *v;
7834 isl_aff *aff;
7836 v = isl_multi_val_get_val(mv, i);
7837 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7838 ma = isl_multi_aff_set_aff(ma, i, aff);
7840 isl_local_space_free(ls);
7842 isl_multi_val_free(mv);
7843 return ma;
7844 error:
7845 isl_space_free(space);
7846 isl_multi_val_free(mv);
7847 return NULL;
7850 /* This is an alternative name for the function above.
7852 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7853 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7855 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7858 /* This function performs the same operation as
7859 * isl_multi_aff_multi_val_on_domain_space,
7860 * but is considered as a function on an isl_space when exported.
7862 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7863 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7865 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7868 /* Return a piecewise multi-affine expression
7869 * that is equal to "mv" on "domain".
7871 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7872 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7874 isl_space *space;
7875 isl_multi_aff *ma;
7877 space = isl_set_get_space(domain);
7878 ma = isl_multi_aff_multi_val_on_space(space, mv);
7880 return isl_pw_multi_aff_alloc(domain, ma);
7883 /* This function performs the same operation as
7884 * isl_pw_multi_aff_multi_val_on_domain,
7885 * but is considered as a function on an isl_set when exported.
7887 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7888 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7890 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7893 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7894 * mv is the value that should be attained on each domain set
7895 * res collects the results
7897 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7898 isl_multi_val *mv;
7899 isl_union_pw_multi_aff *res;
7902 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7903 * and add it to data->res.
7905 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7906 void *user)
7908 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7909 isl_pw_multi_aff *pma;
7910 isl_multi_val *mv;
7912 mv = isl_multi_val_copy(data->mv);
7913 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7914 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7916 return data->res ? isl_stat_ok : isl_stat_error;
7919 /* Return a union piecewise multi-affine expression
7920 * that is equal to "mv" on "domain".
7922 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7923 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7925 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7926 isl_space *space;
7928 space = isl_union_set_get_space(domain);
7929 data.res = isl_union_pw_multi_aff_empty(space);
7930 data.mv = mv;
7931 if (isl_union_set_foreach_set(domain,
7932 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7933 data.res = isl_union_pw_multi_aff_free(data.res);
7934 isl_union_set_free(domain);
7935 isl_multi_val_free(mv);
7936 return data.res;
7939 /* Compute the pullback of data->pma by the function represented by "pma2",
7940 * provided the spaces match, and add the results to data->res.
7942 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7944 struct isl_union_pw_multi_aff_bin_data *data = user;
7946 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7947 pma2->dim, isl_dim_out)) {
7948 isl_pw_multi_aff_free(pma2);
7949 return isl_stat_ok;
7952 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7953 isl_pw_multi_aff_copy(data->pma), pma2);
7955 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7956 if (!data->res)
7957 return isl_stat_error;
7959 return isl_stat_ok;
7962 /* Compute the pullback of "upma1" by the function represented by "upma2".
7964 __isl_give isl_union_pw_multi_aff *
7965 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7966 __isl_take isl_union_pw_multi_aff *upma1,
7967 __isl_take isl_union_pw_multi_aff *upma2)
7969 return bin_op(upma1, upma2, &pullback_entry);
7972 /* Apply "upma2" to "upma1".
7974 * That is, compute the pullback of "upma2" by "upma1".
7976 __isl_give isl_union_pw_multi_aff *
7977 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7978 __isl_take isl_union_pw_multi_aff *upma1,
7979 __isl_take isl_union_pw_multi_aff *upma2)
7981 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7984 #undef TYPE
7985 #define TYPE isl_pw_multi_aff
7986 static
7987 #include "isl_copy_tuple_id_templ.c"
7989 /* Given a function "pma1" of the form A[B -> C] -> D and
7990 * a function "pma2" of the form E -> B,
7991 * replace the domain of the wrapped relation inside the domain of "pma1"
7992 * by the preimage with respect to "pma2".
7993 * In other words, plug in "pma2" in this nested domain.
7994 * The result is of the form A[E -> C] -> D.
7996 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7997 * plug that into "pma1".
7999 __isl_give isl_pw_multi_aff *
8000 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8001 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
8003 isl_space *pma1_space, *pma2_space;
8004 isl_space *space;
8005 isl_pw_multi_aff *id;
8007 pma1_space = isl_pw_multi_aff_peek_space(pma1);
8008 pma2_space = isl_pw_multi_aff_peek_space(pma2);
8010 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
8011 goto error;
8012 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
8013 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
8014 goto error;
8016 space = isl_space_domain(isl_space_copy(pma1_space));
8017 space = isl_space_range(isl_space_unwrap(space));
8018 id = isl_pw_multi_aff_identity_on_domain_space(space);
8019 pma2 = isl_pw_multi_aff_product(pma2, id);
8021 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
8022 pma1_space, isl_dim_in);
8023 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
8024 pma1_space, isl_dim_in);
8026 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
8027 error:
8028 isl_pw_multi_aff_free(pma1);
8029 isl_pw_multi_aff_free(pma2);
8030 return NULL;
8033 /* If data->pma and "pma2" are such that
8034 * data->pma is of the form A[B -> C] -> D and
8035 * "pma2" is of the form E -> B,
8036 * then replace the domain of the wrapped relation
8037 * inside the domain of data->pma by the preimage with respect to "pma2" and
8038 * add the result to data->res.
8040 static isl_stat preimage_domain_wrapped_domain_entry(
8041 __isl_take isl_pw_multi_aff *pma2, void *user)
8043 struct isl_union_pw_multi_aff_bin_data *data = user;
8044 isl_space *pma1_space, *pma2_space;
8045 isl_bool match;
8047 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
8048 pma2_space = isl_pw_multi_aff_peek_space(pma2);
8050 match = isl_space_domain_is_wrapping(pma1_space);
8051 if (match >= 0 && match)
8052 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
8053 isl_dim_in, pma2_space, isl_dim_out);
8054 if (match < 0 || !match) {
8055 isl_pw_multi_aff_free(pma2);
8056 return match < 0 ? isl_stat_error : isl_stat_ok;
8059 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
8060 isl_pw_multi_aff_copy(data->pma), pma2);
8062 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
8064 return isl_stat_non_null(data->res);
8067 /* For each pair of functions A[B -> C] -> D in "upma1" and
8068 * E -> B in "upma2",
8069 * replace the domain of the wrapped relation inside the domain of the first
8070 * by the preimage with respect to the second and collect the results.
8071 * In other words, plug in the second function in this nested domain.
8072 * The results are of the form A[E -> C] -> D.
8074 __isl_give isl_union_pw_multi_aff *
8075 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
8076 __isl_take isl_union_pw_multi_aff *upma1,
8077 __isl_take isl_union_pw_multi_aff *upma2)
8079 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
8082 /* Check that the domain space of "upa" matches "space".
8084 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
8085 * can in principle never fail since the space "space" is that
8086 * of the isl_multi_union_pw_aff and is a set space such that
8087 * there is no domain space to match.
8089 * We check the parameters and double-check that "space" is
8090 * indeed that of a set.
8092 static isl_stat isl_union_pw_aff_check_match_domain_space(
8093 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8095 isl_space *upa_space;
8096 isl_bool match;
8098 if (!upa || !space)
8099 return isl_stat_error;
8101 match = isl_space_is_set(space);
8102 if (match < 0)
8103 return isl_stat_error;
8104 if (!match)
8105 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8106 "expecting set space", return isl_stat_error);
8108 upa_space = isl_union_pw_aff_get_space(upa);
8109 match = isl_space_has_equal_params(space, upa_space);
8110 if (match < 0)
8111 goto error;
8112 if (!match)
8113 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8114 "parameters don't match", goto error);
8116 isl_space_free(upa_space);
8117 return isl_stat_ok;
8118 error:
8119 isl_space_free(upa_space);
8120 return isl_stat_error;
8123 /* Do the parameters of "upa" match those of "space"?
8125 static isl_bool isl_union_pw_aff_matching_params(
8126 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
8128 isl_space *upa_space;
8129 isl_bool match;
8131 if (!upa || !space)
8132 return isl_bool_error;
8134 upa_space = isl_union_pw_aff_get_space(upa);
8136 match = isl_space_has_equal_params(space, upa_space);
8138 isl_space_free(upa_space);
8139 return match;
8142 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8143 * space represents the new parameters.
8144 * res collects the results.
8146 struct isl_union_pw_aff_reset_params_data {
8147 isl_space *space;
8148 isl_union_pw_aff *res;
8151 /* Replace the parameters of "pa" by data->space and
8152 * add the result to data->res.
8154 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8156 struct isl_union_pw_aff_reset_params_data *data = user;
8157 isl_space *space;
8159 space = isl_pw_aff_get_space(pa);
8160 space = isl_space_replace_params(space, data->space);
8161 pa = isl_pw_aff_reset_space(pa, space);
8162 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8164 return data->res ? isl_stat_ok : isl_stat_error;
8167 /* Replace the domain space of "upa" by "space".
8168 * Since a union expression does not have a (single) domain space,
8169 * "space" is necessarily a parameter space.
8171 * Since the order and the names of the parameters determine
8172 * the hash value, we need to create a new hash table.
8174 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8175 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8177 struct isl_union_pw_aff_reset_params_data data = { space };
8178 isl_bool match;
8180 match = isl_union_pw_aff_matching_params(upa, space);
8181 if (match < 0)
8182 upa = isl_union_pw_aff_free(upa);
8183 else if (match) {
8184 isl_space_free(space);
8185 return upa;
8188 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8189 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8190 data.res = isl_union_pw_aff_free(data.res);
8192 isl_union_pw_aff_free(upa);
8193 isl_space_free(space);
8194 return data.res;
8197 /* Return the floor of "pa".
8199 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8201 return isl_pw_aff_floor(pa);
8204 /* Given f, return floor(f).
8206 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8207 __isl_take isl_union_pw_aff *upa)
8209 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8212 /* Compute
8214 * upa mod m = upa - m * floor(upa/m)
8216 * with m an integer value.
8218 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8219 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8221 isl_union_pw_aff *res;
8223 if (!upa || !m)
8224 goto error;
8226 if (!isl_val_is_int(m))
8227 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8228 "expecting integer modulo", goto error);
8229 if (!isl_val_is_pos(m))
8230 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8231 "expecting positive modulo", goto error);
8233 res = isl_union_pw_aff_copy(upa);
8234 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8235 upa = isl_union_pw_aff_floor(upa);
8236 upa = isl_union_pw_aff_scale_val(upa, m);
8237 res = isl_union_pw_aff_sub(res, upa);
8239 return res;
8240 error:
8241 isl_val_free(m);
8242 isl_union_pw_aff_free(upa);
8243 return NULL;
8246 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8247 * pos is the output position that needs to be extracted.
8248 * res collects the results.
8250 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8251 int pos;
8252 isl_union_pw_aff *res;
8255 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8256 * (assuming it has such a dimension) and add it to data->res.
8258 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8260 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8261 isl_size n_out;
8262 isl_pw_aff *pa;
8264 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8265 if (n_out < 0)
8266 return isl_stat_error;
8267 if (data->pos >= n_out) {
8268 isl_pw_multi_aff_free(pma);
8269 return isl_stat_ok;
8272 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8273 isl_pw_multi_aff_free(pma);
8275 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8277 return data->res ? isl_stat_ok : isl_stat_error;
8280 /* Extract an isl_union_pw_aff corresponding to
8281 * output dimension "pos" of "upma".
8283 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8284 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8286 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8287 isl_space *space;
8289 if (!upma)
8290 return NULL;
8292 if (pos < 0)
8293 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8294 "cannot extract at negative position", return NULL);
8296 space = isl_union_pw_multi_aff_get_space(upma);
8297 data.res = isl_union_pw_aff_empty(space);
8298 data.pos = pos;
8299 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8300 &get_union_pw_aff, &data) < 0)
8301 data.res = isl_union_pw_aff_free(data.res);
8303 return data.res;
8306 /* Return a union piecewise affine expression
8307 * that is equal to "aff" on "domain".
8309 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8310 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8312 isl_pw_aff *pa;
8314 pa = isl_pw_aff_from_aff(aff);
8315 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8318 /* Return a union piecewise affine expression
8319 * that is equal to the parameter identified by "id" on "domain".
8321 * Make sure the parameter appears in the space passed to
8322 * isl_aff_param_on_domain_space_id.
8324 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8325 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8327 isl_space *space;
8328 isl_aff *aff;
8330 space = isl_union_set_get_space(domain);
8331 space = isl_space_add_param_id(space, isl_id_copy(id));
8332 aff = isl_aff_param_on_domain_space_id(space, id);
8333 return isl_union_pw_aff_aff_on_domain(domain, aff);
8336 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8337 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8338 * needs to attain.
8339 * "res" collects the results.
8341 struct isl_union_pw_aff_pw_aff_on_domain_data {
8342 isl_pw_aff *pa;
8343 isl_union_pw_aff *res;
8346 /* Construct a piecewise affine expression that is equal to data->pa
8347 * on "domain" and add the result to data->res.
8349 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8351 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8352 isl_pw_aff *pa;
8353 isl_size dim;
8355 pa = isl_pw_aff_copy(data->pa);
8356 dim = isl_set_dim(domain, isl_dim_set);
8357 if (dim < 0)
8358 pa = isl_pw_aff_free(pa);
8359 pa = isl_pw_aff_from_range(pa);
8360 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8361 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8362 pa = isl_pw_aff_intersect_domain(pa, domain);
8363 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8365 return data->res ? isl_stat_ok : isl_stat_error;
8368 /* Return a union piecewise affine expression
8369 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8370 * have been aligned.
8372 * Construct an isl_pw_aff on each of the sets in "domain" and
8373 * collect the results.
8375 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8376 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8378 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8379 isl_space *space;
8381 space = isl_union_set_get_space(domain);
8382 data.res = isl_union_pw_aff_empty(space);
8383 data.pa = pa;
8384 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8385 data.res = isl_union_pw_aff_free(data.res);
8386 isl_union_set_free(domain);
8387 isl_pw_aff_free(pa);
8388 return data.res;
8391 /* Return a union piecewise affine expression
8392 * that is equal to "pa" on "domain".
8394 * Check that "pa" is a parametric expression,
8395 * align the parameters if needed and call
8396 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8398 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8399 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8401 isl_bool is_set;
8402 isl_bool equal_params;
8403 isl_space *domain_space, *pa_space;
8405 pa_space = isl_pw_aff_peek_space(pa);
8406 is_set = isl_space_is_set(pa_space);
8407 if (is_set < 0)
8408 goto error;
8409 if (!is_set)
8410 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8411 "expecting parametric expression", goto error);
8413 domain_space = isl_union_set_get_space(domain);
8414 pa_space = isl_pw_aff_get_space(pa);
8415 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8416 if (equal_params >= 0 && !equal_params) {
8417 isl_space *space;
8419 space = isl_space_align_params(domain_space, pa_space);
8420 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8421 domain = isl_union_set_align_params(domain, space);
8422 } else {
8423 isl_space_free(domain_space);
8424 isl_space_free(pa_space);
8427 if (equal_params < 0)
8428 goto error;
8429 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8430 error:
8431 isl_union_set_free(domain);
8432 isl_pw_aff_free(pa);
8433 return NULL;
8436 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8437 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8438 * "res" collects the results.
8440 struct isl_union_pw_aff_val_on_domain_data {
8441 isl_val *v;
8442 isl_union_pw_aff *res;
8445 /* Construct a piecewise affine expression that is equal to data->v
8446 * on "domain" and add the result to data->res.
8448 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8450 struct isl_union_pw_aff_val_on_domain_data *data = user;
8451 isl_pw_aff *pa;
8452 isl_val *v;
8454 v = isl_val_copy(data->v);
8455 pa = isl_pw_aff_val_on_domain(domain, v);
8456 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8458 return data->res ? isl_stat_ok : isl_stat_error;
8461 /* Return a union piecewise affine expression
8462 * that is equal to "v" on "domain".
8464 * Construct an isl_pw_aff on each of the sets in "domain" and
8465 * collect the results.
8467 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8468 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8470 struct isl_union_pw_aff_val_on_domain_data data;
8471 isl_space *space;
8473 space = isl_union_set_get_space(domain);
8474 data.res = isl_union_pw_aff_empty(space);
8475 data.v = v;
8476 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8477 data.res = isl_union_pw_aff_free(data.res);
8478 isl_union_set_free(domain);
8479 isl_val_free(v);
8480 return data.res;
8483 /* Construct a piecewise multi affine expression
8484 * that is equal to "pa" and add it to upma.
8486 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8487 void *user)
8489 isl_union_pw_multi_aff **upma = user;
8490 isl_pw_multi_aff *pma;
8492 pma = isl_pw_multi_aff_from_pw_aff(pa);
8493 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8495 return *upma ? isl_stat_ok : isl_stat_error;
8498 /* Construct and return a union piecewise multi affine expression
8499 * that is equal to the given union piecewise affine expression.
8501 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8502 __isl_take isl_union_pw_aff *upa)
8504 isl_space *space;
8505 isl_union_pw_multi_aff *upma;
8507 if (!upa)
8508 return NULL;
8510 space = isl_union_pw_aff_get_space(upa);
8511 upma = isl_union_pw_multi_aff_empty(space);
8513 if (isl_union_pw_aff_foreach_pw_aff(upa,
8514 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8515 upma = isl_union_pw_multi_aff_free(upma);
8517 isl_union_pw_aff_free(upa);
8518 return upma;
8521 /* Compute the set of elements in the domain of "pa" where it is zero and
8522 * add this set to "uset".
8524 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8526 isl_union_set **uset = (isl_union_set **)user;
8528 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8530 return *uset ? isl_stat_ok : isl_stat_error;
8533 /* Return a union set containing those elements in the domain
8534 * of "upa" where it is zero.
8536 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8537 __isl_take isl_union_pw_aff *upa)
8539 isl_union_set *zero;
8541 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8542 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8543 zero = isl_union_set_free(zero);
8545 isl_union_pw_aff_free(upa);
8546 return zero;
8549 /* Internal data structure for isl_union_pw_aff_bind_id,
8550 * storing the parameter that needs to be bound and
8551 * the accumulated results.
8553 struct isl_bind_id_data {
8554 isl_id *id;
8555 isl_union_set *bound;
8558 /* Bind the piecewise affine function "pa" to the parameter data->id,
8559 * adding the resulting elements in the domain where the expression
8560 * is equal to the parameter to data->bound.
8562 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8564 struct isl_bind_id_data *data = user;
8565 isl_set *bound;
8567 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8568 data->bound = isl_union_set_add_set(data->bound, bound);
8570 return data->bound ? isl_stat_ok : isl_stat_error;
8573 /* Bind the union piecewise affine function "upa" to the parameter "id",
8574 * returning the elements in the domain where the expression
8575 * is equal to the parameter.
8577 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8578 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8580 struct isl_bind_id_data data = { id };
8582 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8583 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8584 data.bound = isl_union_set_free(data.bound);
8586 isl_union_pw_aff_free(upa);
8587 isl_id_free(id);
8588 return data.bound;
8591 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8592 * upma is the function that is plugged in.
8593 * pa is the current part of the function in which upma is plugged in.
8594 * res collects the results.
8596 struct isl_union_pw_aff_pullback_upma_data {
8597 isl_union_pw_multi_aff *upma;
8598 isl_pw_aff *pa;
8599 isl_union_pw_aff *res;
8602 /* Check if "pma" can be plugged into data->pa.
8603 * If so, perform the pullback and add the result to data->res.
8605 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8607 struct isl_union_pw_aff_pullback_upma_data *data = user;
8608 isl_pw_aff *pa;
8610 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8611 pma->dim, isl_dim_out)) {
8612 isl_pw_multi_aff_free(pma);
8613 return isl_stat_ok;
8616 pa = isl_pw_aff_copy(data->pa);
8617 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8619 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8621 return data->res ? isl_stat_ok : isl_stat_error;
8624 /* Check if any of the elements of data->upma can be plugged into pa,
8625 * add if so add the result to data->res.
8627 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8629 struct isl_union_pw_aff_pullback_upma_data *data = user;
8630 isl_stat r;
8632 data->pa = pa;
8633 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8634 &pa_pb_pma, data);
8635 isl_pw_aff_free(pa);
8637 return r;
8640 /* Compute the pullback of "upa" by the function represented by "upma".
8641 * In other words, plug in "upma" in "upa". The result contains
8642 * expressions defined over the domain space of "upma".
8644 * Run over all pairs of elements in "upa" and "upma", perform
8645 * the pullback when appropriate and collect the results.
8646 * If the hash value were based on the domain space rather than
8647 * the function space, then we could run through all elements
8648 * of "upma" and directly pick out the corresponding element of "upa".
8650 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8651 __isl_take isl_union_pw_aff *upa,
8652 __isl_take isl_union_pw_multi_aff *upma)
8654 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8655 isl_space *space;
8657 space = isl_union_pw_multi_aff_get_space(upma);
8658 upa = isl_union_pw_aff_align_params(upa, space);
8659 space = isl_union_pw_aff_get_space(upa);
8660 upma = isl_union_pw_multi_aff_align_params(upma, space);
8662 if (!upa || !upma)
8663 goto error;
8665 data.upma = upma;
8666 data.res = isl_union_pw_aff_alloc_same_size(upa);
8667 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8668 data.res = isl_union_pw_aff_free(data.res);
8670 isl_union_pw_aff_free(upa);
8671 isl_union_pw_multi_aff_free(upma);
8672 return data.res;
8673 error:
8674 isl_union_pw_aff_free(upa);
8675 isl_union_pw_multi_aff_free(upma);
8676 return NULL;
8679 #undef BASE
8680 #define BASE union_pw_aff
8681 #undef DOMBASE
8682 #define DOMBASE union_set
8684 #include <isl_multi_explicit_domain.c>
8685 #include <isl_multi_union_pw_aff_explicit_domain.c>
8686 #include <isl_multi_templ.c>
8687 #include <isl_multi_un_op_templ.c>
8688 #include <isl_multi_bin_val_templ.c>
8689 #include <isl_multi_apply_set.c>
8690 #include <isl_multi_apply_union_set.c>
8691 #include <isl_multi_arith_templ.c>
8692 #include <isl_multi_bind_templ.c>
8693 #include <isl_multi_coalesce.c>
8694 #include <isl_multi_dim_id_templ.c>
8695 #include <isl_multi_floor.c>
8696 #include <isl_multi_from_base_templ.c>
8697 #include <isl_multi_gist.c>
8698 #include <isl_multi_align_set.c>
8699 #include <isl_multi_align_union_set.c>
8700 #include <isl_multi_intersect.c>
8701 #include <isl_multi_nan_templ.c>
8702 #include <isl_multi_tuple_id_templ.c>
8703 #include <isl_multi_union_add_templ.c>
8704 #include <isl_multi_zero_space_templ.c>
8706 /* Does "mupa" have a non-trivial explicit domain?
8708 * The explicit domain, if present, is trivial if it represents
8709 * an (obviously) universe parameter set.
8711 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8712 __isl_keep isl_multi_union_pw_aff *mupa)
8714 isl_bool is_params, trivial;
8715 isl_set *set;
8717 if (!mupa)
8718 return isl_bool_error;
8719 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8720 return isl_bool_false;
8721 is_params = isl_union_set_is_params(mupa->u.dom);
8722 if (is_params < 0 || !is_params)
8723 return isl_bool_not(is_params);
8724 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8725 trivial = isl_set_plain_is_universe(set);
8726 isl_set_free(set);
8727 return isl_bool_not(trivial);
8730 /* Construct a multiple union piecewise affine expression
8731 * in the given space with value zero in each of the output dimensions.
8733 * Since there is no canonical zero value for
8734 * a union piecewise affine expression, we can only construct
8735 * a zero-dimensional "zero" value.
8737 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8738 __isl_take isl_space *space)
8740 isl_bool params;
8741 isl_size dim;
8743 if (!space)
8744 return NULL;
8746 params = isl_space_is_params(space);
8747 if (params < 0)
8748 goto error;
8749 if (params)
8750 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8751 "expecting proper set space", goto error);
8752 if (!isl_space_is_set(space))
8753 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8754 "expecting set space", goto error);
8755 dim = isl_space_dim(space, isl_dim_out);
8756 if (dim < 0)
8757 goto error;
8758 if (dim != 0)
8759 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8760 "expecting 0D space", goto error);
8762 return isl_multi_union_pw_aff_alloc(space);
8763 error:
8764 isl_space_free(space);
8765 return NULL;
8768 /* Construct and return a multi union piecewise affine expression
8769 * that is equal to the given multi affine expression.
8771 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8772 __isl_take isl_multi_aff *ma)
8774 isl_multi_pw_aff *mpa;
8776 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8777 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8780 /* This function performs the same operation as
8781 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8782 * isl_multi_aff when exported.
8784 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8785 __isl_take isl_multi_aff *ma)
8787 return isl_multi_union_pw_aff_from_multi_aff(ma);
8790 /* Construct and return a multi union piecewise affine expression
8791 * that is equal to the given multi piecewise affine expression.
8793 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8794 __isl_take isl_multi_pw_aff *mpa)
8796 int i;
8797 isl_size n;
8798 isl_space *space;
8799 isl_multi_union_pw_aff *mupa;
8801 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8802 if (n < 0)
8803 mpa = isl_multi_pw_aff_free(mpa);
8804 if (!mpa)
8805 return NULL;
8807 space = isl_multi_pw_aff_get_space(mpa);
8808 space = isl_space_range(space);
8809 mupa = isl_multi_union_pw_aff_alloc(space);
8811 for (i = 0; i < n; ++i) {
8812 isl_pw_aff *pa;
8813 isl_union_pw_aff *upa;
8815 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8816 upa = isl_union_pw_aff_from_pw_aff(pa);
8817 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8820 isl_multi_pw_aff_free(mpa);
8822 return mupa;
8825 /* Extract the range space of "pma" and assign it to *space.
8826 * If *space has already been set (through a previous call to this function),
8827 * then check that the range space is the same.
8829 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8831 isl_space **space = user;
8832 isl_space *pma_space;
8833 isl_bool equal;
8835 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8836 isl_pw_multi_aff_free(pma);
8838 if (!pma_space)
8839 return isl_stat_error;
8840 if (!*space) {
8841 *space = pma_space;
8842 return isl_stat_ok;
8845 equal = isl_space_is_equal(pma_space, *space);
8846 isl_space_free(pma_space);
8848 if (equal < 0)
8849 return isl_stat_error;
8850 if (!equal)
8851 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8852 "range spaces not the same", return isl_stat_error);
8853 return isl_stat_ok;
8856 /* Construct and return a multi union piecewise affine expression
8857 * that is equal to the given union piecewise multi affine expression.
8859 * In order to be able to perform the conversion, the input
8860 * needs to be non-empty and may only involve a single range space.
8862 * If the resulting multi union piecewise affine expression has
8863 * an explicit domain, then assign it the domain of the input.
8864 * In other cases, the domain is stored in the individual elements.
8866 __isl_give isl_multi_union_pw_aff *
8867 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8868 __isl_take isl_union_pw_multi_aff *upma)
8870 isl_space *space = NULL;
8871 isl_multi_union_pw_aff *mupa;
8872 int i;
8873 isl_size n;
8875 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8876 if (n < 0)
8877 goto error;
8878 if (n == 0)
8879 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8880 "cannot extract range space from empty input",
8881 goto error);
8882 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8883 &space) < 0)
8884 goto error;
8886 if (!space)
8887 goto error;
8889 n = isl_space_dim(space, isl_dim_set);
8890 if (n < 0)
8891 space = isl_space_free(space);
8892 mupa = isl_multi_union_pw_aff_alloc(space);
8894 for (i = 0; i < n; ++i) {
8895 isl_union_pw_aff *upa;
8897 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8898 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8900 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8901 isl_union_set *dom;
8902 isl_union_pw_multi_aff *copy;
8904 copy = isl_union_pw_multi_aff_copy(upma);
8905 dom = isl_union_pw_multi_aff_domain(copy);
8906 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8909 isl_union_pw_multi_aff_free(upma);
8910 return mupa;
8911 error:
8912 isl_space_free(space);
8913 isl_union_pw_multi_aff_free(upma);
8914 return NULL;
8917 /* This function performs the same operation as
8918 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8919 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8921 __isl_give isl_multi_union_pw_aff *
8922 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8923 __isl_take isl_union_pw_multi_aff *upma)
8925 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8928 /* Try and create an isl_multi_union_pw_aff that is equivalent
8929 * to the given isl_union_map.
8930 * The isl_union_map is required to be single-valued in each space.
8931 * Moreover, it cannot be empty and all range spaces need to be the same.
8932 * Otherwise, an error is produced.
8934 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8935 __isl_take isl_union_map *umap)
8937 isl_union_pw_multi_aff *upma;
8939 upma = isl_union_pw_multi_aff_from_union_map(umap);
8940 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8943 /* This function performs the same operation as
8944 * isl_multi_union_pw_aff_from_union_map,
8945 * but is considered as a function on an isl_union_map when exported.
8947 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8948 __isl_take isl_union_map *umap)
8950 return isl_multi_union_pw_aff_from_union_map(umap);
8953 /* Return a multiple union piecewise affine expression
8954 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8955 * have been aligned.
8957 * If the resulting multi union piecewise affine expression has
8958 * an explicit domain, then assign it the input domain.
8959 * In other cases, the domain is stored in the individual elements.
8961 static __isl_give isl_multi_union_pw_aff *
8962 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8963 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8965 int i;
8966 isl_size n;
8967 isl_space *space;
8968 isl_multi_union_pw_aff *mupa;
8970 n = isl_multi_val_dim(mv, isl_dim_set);
8971 if (!domain || n < 0)
8972 goto error;
8974 space = isl_multi_val_get_space(mv);
8975 mupa = isl_multi_union_pw_aff_alloc(space);
8976 for (i = 0; i < n; ++i) {
8977 isl_val *v;
8978 isl_union_pw_aff *upa;
8980 v = isl_multi_val_get_val(mv, i);
8981 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8983 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8985 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8986 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8987 isl_union_set_copy(domain));
8989 isl_union_set_free(domain);
8990 isl_multi_val_free(mv);
8991 return mupa;
8992 error:
8993 isl_union_set_free(domain);
8994 isl_multi_val_free(mv);
8995 return NULL;
8998 /* Return a multiple union piecewise affine expression
8999 * that is equal to "mv" on "domain".
9001 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
9002 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
9004 isl_bool equal_params;
9006 if (!domain || !mv)
9007 goto error;
9008 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
9009 if (equal_params < 0)
9010 goto error;
9011 if (equal_params)
9012 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
9013 domain, mv);
9014 domain = isl_union_set_align_params(domain,
9015 isl_multi_val_get_space(mv));
9016 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
9017 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
9018 error:
9019 isl_union_set_free(domain);
9020 isl_multi_val_free(mv);
9021 return NULL;
9024 /* Return a multiple union piecewise affine expression
9025 * that is equal to "ma" on "domain".
9027 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
9028 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
9030 isl_pw_multi_aff *pma;
9032 pma = isl_pw_multi_aff_from_multi_aff(ma);
9033 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
9036 /* Return a multiple union piecewise affine expression
9037 * that is equal to "pma" on "domain", assuming "domain" and "pma"
9038 * have been aligned.
9040 * If the resulting multi union piecewise affine expression has
9041 * an explicit domain, then assign it the input domain.
9042 * In other cases, the domain is stored in the individual elements.
9044 static __isl_give isl_multi_union_pw_aff *
9045 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9046 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
9048 int i;
9049 isl_size n;
9050 isl_space *space;
9051 isl_multi_union_pw_aff *mupa;
9053 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
9054 if (!domain || n < 0)
9055 goto error;
9056 space = isl_pw_multi_aff_get_space(pma);
9057 mupa = isl_multi_union_pw_aff_alloc(space);
9058 for (i = 0; i < n; ++i) {
9059 isl_pw_aff *pa;
9060 isl_union_pw_aff *upa;
9062 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9063 upa = isl_union_pw_aff_pw_aff_on_domain(
9064 isl_union_set_copy(domain), pa);
9065 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9067 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9068 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
9069 isl_union_set_copy(domain));
9071 isl_union_set_free(domain);
9072 isl_pw_multi_aff_free(pma);
9073 return mupa;
9074 error:
9075 isl_union_set_free(domain);
9076 isl_pw_multi_aff_free(pma);
9077 return NULL;
9080 /* Return a multiple union piecewise affine expression
9081 * that is equal to "pma" on "domain".
9083 __isl_give isl_multi_union_pw_aff *
9084 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
9085 __isl_take isl_pw_multi_aff *pma)
9087 isl_bool equal_params;
9088 isl_space *space;
9090 space = isl_pw_multi_aff_peek_space(pma);
9091 equal_params = isl_union_set_space_has_equal_params(domain, space);
9092 if (equal_params < 0)
9093 goto error;
9094 if (equal_params)
9095 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
9096 domain, pma);
9097 domain = isl_union_set_align_params(domain,
9098 isl_pw_multi_aff_get_space(pma));
9099 pma = isl_pw_multi_aff_align_params(pma,
9100 isl_union_set_get_space(domain));
9101 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
9102 pma);
9103 error:
9104 isl_union_set_free(domain);
9105 isl_pw_multi_aff_free(pma);
9106 return NULL;
9109 /* Return a union set containing those elements in the domains
9110 * of the elements of "mupa" where they are all zero.
9112 * If there are no elements, then simply return the entire domain.
9114 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
9115 __isl_take isl_multi_union_pw_aff *mupa)
9117 int i;
9118 isl_size n;
9119 isl_union_pw_aff *upa;
9120 isl_union_set *zero;
9122 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9123 if (n < 0)
9124 mupa = isl_multi_union_pw_aff_free(mupa);
9125 if (!mupa)
9126 return NULL;
9128 if (n == 0)
9129 return isl_multi_union_pw_aff_domain(mupa);
9131 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9132 zero = isl_union_pw_aff_zero_union_set(upa);
9134 for (i = 1; i < n; ++i) {
9135 isl_union_set *zero_i;
9137 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9138 zero_i = isl_union_pw_aff_zero_union_set(upa);
9140 zero = isl_union_set_intersect(zero, zero_i);
9143 isl_multi_union_pw_aff_free(mupa);
9144 return zero;
9147 /* Construct a union map mapping the shared domain
9148 * of the union piecewise affine expressions to the range of "mupa"
9149 * in the special case of a 0D multi union piecewise affine expression.
9151 * Construct a map between the explicit domain of "mupa" and
9152 * the range space.
9153 * Note that this assumes that the domain consists of explicit elements.
9155 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9156 __isl_take isl_multi_union_pw_aff *mupa)
9158 isl_bool is_params;
9159 isl_space *space;
9160 isl_union_set *dom, *ran;
9162 space = isl_multi_union_pw_aff_get_space(mupa);
9163 dom = isl_multi_union_pw_aff_domain(mupa);
9164 ran = isl_union_set_from_set(isl_set_universe(space));
9166 is_params = isl_union_set_is_params(dom);
9167 if (is_params < 0)
9168 dom = isl_union_set_free(dom);
9169 else if (is_params)
9170 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9171 "cannot create union map from expression without "
9172 "explicit domain elements",
9173 dom = isl_union_set_free(dom));
9175 return isl_union_map_from_domain_and_range(dom, ran);
9178 /* Construct a union map mapping the shared domain
9179 * of the union piecewise affine expressions to the range of "mupa"
9180 * with each dimension in the range equated to the
9181 * corresponding union piecewise affine expression.
9183 * If the input is zero-dimensional, then construct a mapping
9184 * from its explicit domain.
9186 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9187 __isl_take isl_multi_union_pw_aff *mupa)
9189 int i;
9190 isl_size n;
9191 isl_space *space;
9192 isl_union_map *umap;
9193 isl_union_pw_aff *upa;
9195 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9196 if (n < 0)
9197 mupa = isl_multi_union_pw_aff_free(mupa);
9198 if (!mupa)
9199 return NULL;
9201 if (n == 0)
9202 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9204 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9205 umap = isl_union_map_from_union_pw_aff(upa);
9207 for (i = 1; i < n; ++i) {
9208 isl_union_map *umap_i;
9210 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9211 umap_i = isl_union_map_from_union_pw_aff(upa);
9212 umap = isl_union_map_flat_range_product(umap, umap_i);
9215 space = isl_multi_union_pw_aff_get_space(mupa);
9216 umap = isl_union_map_reset_range_space(umap, space);
9218 isl_multi_union_pw_aff_free(mupa);
9219 return umap;
9222 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9223 * "range" is the space from which to set the range space.
9224 * "res" collects the results.
9226 struct isl_union_pw_multi_aff_reset_range_space_data {
9227 isl_space *range;
9228 isl_union_pw_multi_aff *res;
9231 /* Replace the range space of "pma" by the range space of data->range and
9232 * add the result to data->res.
9234 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9236 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9237 isl_space *space;
9239 space = isl_pw_multi_aff_get_space(pma);
9240 space = isl_space_domain(space);
9241 space = isl_space_extend_domain_with_range(space,
9242 isl_space_copy(data->range));
9243 pma = isl_pw_multi_aff_reset_space(pma, space);
9244 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9246 return data->res ? isl_stat_ok : isl_stat_error;
9249 /* Replace the range space of all the piecewise affine expressions in "upma" by
9250 * the range space of "space".
9252 * This assumes that all these expressions have the same output dimension.
9254 * Since the spaces of the expressions change, so do their hash values.
9255 * We therefore need to create a new isl_union_pw_multi_aff.
9256 * Note that the hash value is currently computed based on the entire
9257 * space even though there can only be a single expression with a given
9258 * domain space.
9260 static __isl_give isl_union_pw_multi_aff *
9261 isl_union_pw_multi_aff_reset_range_space(
9262 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9264 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9265 isl_space *space_upma;
9267 space_upma = isl_union_pw_multi_aff_get_space(upma);
9268 data.res = isl_union_pw_multi_aff_empty(space_upma);
9269 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9270 &reset_range_space, &data) < 0)
9271 data.res = isl_union_pw_multi_aff_free(data.res);
9273 isl_space_free(space);
9274 isl_union_pw_multi_aff_free(upma);
9275 return data.res;
9278 /* Construct and return a union piecewise multi affine expression
9279 * that is equal to the given multi union piecewise affine expression,
9280 * in the special case of a 0D multi union piecewise affine expression.
9282 * Construct a union piecewise multi affine expression
9283 * on top of the explicit domain of the input.
9285 __isl_give isl_union_pw_multi_aff *
9286 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9287 __isl_take isl_multi_union_pw_aff *mupa)
9289 isl_space *space;
9290 isl_multi_val *mv;
9291 isl_union_set *domain;
9293 space = isl_multi_union_pw_aff_get_space(mupa);
9294 mv = isl_multi_val_zero(space);
9295 domain = isl_multi_union_pw_aff_domain(mupa);
9296 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9299 /* Construct and return a union piecewise multi affine expression
9300 * that is equal to the given multi union piecewise affine expression.
9302 * If the input is zero-dimensional, then
9303 * construct a union piecewise multi affine expression
9304 * on top of the explicit domain of the input.
9306 __isl_give isl_union_pw_multi_aff *
9307 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9308 __isl_take isl_multi_union_pw_aff *mupa)
9310 int i;
9311 isl_size n;
9312 isl_space *space;
9313 isl_union_pw_multi_aff *upma;
9314 isl_union_pw_aff *upa;
9316 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9317 if (n < 0)
9318 mupa = isl_multi_union_pw_aff_free(mupa);
9319 if (!mupa)
9320 return NULL;
9322 if (n == 0)
9323 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9325 space = isl_multi_union_pw_aff_get_space(mupa);
9326 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9327 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9329 for (i = 1; i < n; ++i) {
9330 isl_union_pw_multi_aff *upma_i;
9332 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9333 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9334 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9337 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9339 isl_multi_union_pw_aff_free(mupa);
9340 return upma;
9343 /* Intersect the range of "mupa" with "range",
9344 * in the special case where "mupa" is 0D.
9346 * Intersect the domain of "mupa" with the constraints on the parameters
9347 * of "range".
9349 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9350 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9352 range = isl_set_params(range);
9353 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9354 return mupa;
9357 /* Intersect the range of "mupa" with "range".
9358 * That is, keep only those domain elements that have a function value
9359 * in "range".
9361 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9362 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9364 isl_union_pw_multi_aff *upma;
9365 isl_union_set *domain;
9366 isl_space *space;
9367 isl_size n;
9368 int match;
9370 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9371 if (n < 0 || !range)
9372 goto error;
9374 space = isl_set_get_space(range);
9375 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9376 space, isl_dim_set);
9377 isl_space_free(space);
9378 if (match < 0)
9379 goto error;
9380 if (!match)
9381 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9382 "space don't match", goto error);
9383 if (n == 0)
9384 return mupa_intersect_range_0D(mupa, range);
9386 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9387 isl_multi_union_pw_aff_copy(mupa));
9388 domain = isl_union_set_from_set(range);
9389 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9390 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9392 return mupa;
9393 error:
9394 isl_multi_union_pw_aff_free(mupa);
9395 isl_set_free(range);
9396 return NULL;
9399 /* Return the shared domain of the elements of "mupa",
9400 * in the special case where "mupa" is zero-dimensional.
9402 * Return the explicit domain of "mupa".
9403 * Note that this domain may be a parameter set, either
9404 * because "mupa" is meant to live in a set space or
9405 * because no explicit domain has been set.
9407 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9408 __isl_take isl_multi_union_pw_aff *mupa)
9410 isl_union_set *dom;
9412 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9413 isl_multi_union_pw_aff_free(mupa);
9415 return dom;
9418 /* Return the shared domain of the elements of "mupa".
9420 * If "mupa" is zero-dimensional, then return its explicit domain.
9422 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9423 __isl_take isl_multi_union_pw_aff *mupa)
9425 int i;
9426 isl_size n;
9427 isl_union_pw_aff *upa;
9428 isl_union_set *dom;
9430 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9431 if (n < 0)
9432 mupa = isl_multi_union_pw_aff_free(mupa);
9433 if (!mupa)
9434 return NULL;
9436 if (n == 0)
9437 return isl_multi_union_pw_aff_domain_0D(mupa);
9439 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9440 dom = isl_union_pw_aff_domain(upa);
9441 for (i = 1; i < n; ++i) {
9442 isl_union_set *dom_i;
9444 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9445 dom_i = isl_union_pw_aff_domain(upa);
9446 dom = isl_union_set_intersect(dom, dom_i);
9449 isl_multi_union_pw_aff_free(mupa);
9450 return dom;
9453 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9454 * In particular, the spaces have been aligned.
9455 * The result is defined over the shared domain of the elements of "mupa"
9457 * We first extract the parametric constant part of "aff" and
9458 * define that over the shared domain.
9459 * Then we iterate over all input dimensions of "aff" and add the corresponding
9460 * multiples of the elements of "mupa".
9461 * Finally, we consider the integer divisions, calling the function
9462 * recursively to obtain an isl_union_pw_aff corresponding to the
9463 * integer division argument.
9465 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9466 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9468 int i;
9469 isl_size n_in, n_div;
9470 isl_union_pw_aff *upa;
9471 isl_union_set *uset;
9472 isl_val *v;
9473 isl_aff *cst;
9475 n_in = isl_aff_dim(aff, isl_dim_in);
9476 n_div = isl_aff_dim(aff, isl_dim_div);
9477 if (n_in < 0 || n_div < 0)
9478 goto error;
9480 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9481 cst = isl_aff_copy(aff);
9482 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9483 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9484 cst = isl_aff_project_domain_on_params(cst);
9485 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9487 for (i = 0; i < n_in; ++i) {
9488 isl_union_pw_aff *upa_i;
9490 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9491 continue;
9492 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9493 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9494 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9495 upa = isl_union_pw_aff_add(upa, upa_i);
9498 for (i = 0; i < n_div; ++i) {
9499 isl_aff *div;
9500 isl_union_pw_aff *upa_i;
9502 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9503 continue;
9504 div = isl_aff_get_div(aff, i);
9505 upa_i = multi_union_pw_aff_apply_aff(
9506 isl_multi_union_pw_aff_copy(mupa), div);
9507 upa_i = isl_union_pw_aff_floor(upa_i);
9508 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9509 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9510 upa = isl_union_pw_aff_add(upa, upa_i);
9513 isl_multi_union_pw_aff_free(mupa);
9514 isl_aff_free(aff);
9516 return upa;
9517 error:
9518 isl_multi_union_pw_aff_free(mupa);
9519 isl_aff_free(aff);
9520 return NULL;
9523 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9524 * with the domain of "aff".
9525 * Furthermore, the dimension of this space needs to be greater than zero.
9526 * The result is defined over the shared domain of the elements of "mupa"
9528 * We perform these checks and then hand over control to
9529 * multi_union_pw_aff_apply_aff.
9531 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9532 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9534 isl_size dim;
9535 isl_space *space1, *space2;
9536 isl_bool equal;
9538 mupa = isl_multi_union_pw_aff_align_params(mupa,
9539 isl_aff_get_space(aff));
9540 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9541 if (!mupa || !aff)
9542 goto error;
9544 space1 = isl_multi_union_pw_aff_get_space(mupa);
9545 space2 = isl_aff_get_domain_space(aff);
9546 equal = isl_space_is_equal(space1, space2);
9547 isl_space_free(space1);
9548 isl_space_free(space2);
9549 if (equal < 0)
9550 goto error;
9551 if (!equal)
9552 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9553 "spaces don't match", goto error);
9554 dim = isl_aff_dim(aff, isl_dim_in);
9555 if (dim < 0)
9556 goto error;
9557 if (dim == 0)
9558 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9559 "cannot determine domains", goto error);
9561 return multi_union_pw_aff_apply_aff(mupa, aff);
9562 error:
9563 isl_multi_union_pw_aff_free(mupa);
9564 isl_aff_free(aff);
9565 return NULL;
9568 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9569 * The space of "mupa" is known to be compatible with the domain of "ma".
9571 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9572 * on the domain of "mupa".
9574 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9575 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9577 isl_union_set *dom;
9579 dom = isl_multi_union_pw_aff_domain(mupa);
9580 ma = isl_multi_aff_project_domain_on_params(ma);
9582 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9585 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9586 * with the domain of "ma".
9587 * The result is defined over the shared domain of the elements of "mupa"
9589 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9590 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9592 isl_space *space1, *space2;
9593 isl_multi_union_pw_aff *res;
9594 isl_bool equal;
9595 int i;
9596 isl_size n_in, n_out;
9598 mupa = isl_multi_union_pw_aff_align_params(mupa,
9599 isl_multi_aff_get_space(ma));
9600 ma = isl_multi_aff_align_params(ma,
9601 isl_multi_union_pw_aff_get_space(mupa));
9602 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9603 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9604 if (!mupa || n_in < 0 || n_out < 0)
9605 goto error;
9607 space1 = isl_multi_union_pw_aff_get_space(mupa);
9608 space2 = isl_multi_aff_get_domain_space(ma);
9609 equal = isl_space_is_equal(space1, space2);
9610 isl_space_free(space1);
9611 isl_space_free(space2);
9612 if (equal < 0)
9613 goto error;
9614 if (!equal)
9615 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9616 "spaces don't match", goto error);
9617 if (n_in == 0)
9618 return mupa_apply_multi_aff_0D(mupa, ma);
9620 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9621 res = isl_multi_union_pw_aff_alloc(space1);
9623 for (i = 0; i < n_out; ++i) {
9624 isl_aff *aff;
9625 isl_union_pw_aff *upa;
9627 aff = isl_multi_aff_get_aff(ma, i);
9628 upa = multi_union_pw_aff_apply_aff(
9629 isl_multi_union_pw_aff_copy(mupa), aff);
9630 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9633 isl_multi_aff_free(ma);
9634 isl_multi_union_pw_aff_free(mupa);
9635 return res;
9636 error:
9637 isl_multi_union_pw_aff_free(mupa);
9638 isl_multi_aff_free(ma);
9639 return NULL;
9642 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9643 * The space of "mupa" is known to be compatible with the domain of "pa".
9645 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9646 * on the domain of "mupa".
9648 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9649 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9651 isl_union_set *dom;
9653 dom = isl_multi_union_pw_aff_domain(mupa);
9654 pa = isl_pw_aff_project_domain_on_params(pa);
9656 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9659 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9660 * with the domain of "pa".
9661 * Furthermore, the dimension of this space needs to be greater than zero.
9662 * The result is defined over the shared domain of the elements of "mupa"
9664 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9665 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9667 int i;
9668 isl_bool equal;
9669 isl_size n_in;
9670 isl_space *space, *space2;
9671 isl_union_pw_aff *upa;
9673 mupa = isl_multi_union_pw_aff_align_params(mupa,
9674 isl_pw_aff_get_space(pa));
9675 pa = isl_pw_aff_align_params(pa,
9676 isl_multi_union_pw_aff_get_space(mupa));
9677 if (!mupa || !pa)
9678 goto error;
9680 space = isl_multi_union_pw_aff_get_space(mupa);
9681 space2 = isl_pw_aff_get_domain_space(pa);
9682 equal = isl_space_is_equal(space, space2);
9683 isl_space_free(space);
9684 isl_space_free(space2);
9685 if (equal < 0)
9686 goto error;
9687 if (!equal)
9688 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9689 "spaces don't match", goto error);
9690 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9691 if (n_in < 0)
9692 goto error;
9693 if (n_in == 0)
9694 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9696 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9697 upa = isl_union_pw_aff_empty(space);
9699 for (i = 0; i < pa->n; ++i) {
9700 isl_aff *aff;
9701 isl_set *domain;
9702 isl_multi_union_pw_aff *mupa_i;
9703 isl_union_pw_aff *upa_i;
9705 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9706 domain = isl_set_copy(pa->p[i].set);
9707 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9708 aff = isl_aff_copy(pa->p[i].aff);
9709 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9710 upa = isl_union_pw_aff_union_add(upa, upa_i);
9713 isl_multi_union_pw_aff_free(mupa);
9714 isl_pw_aff_free(pa);
9715 return upa;
9716 error:
9717 isl_multi_union_pw_aff_free(mupa);
9718 isl_pw_aff_free(pa);
9719 return NULL;
9722 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9723 * The space of "mupa" is known to be compatible with the domain of "pma".
9725 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9726 * on the domain of "mupa".
9728 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9729 __isl_take isl_multi_union_pw_aff *mupa,
9730 __isl_take isl_pw_multi_aff *pma)
9732 isl_union_set *dom;
9734 dom = isl_multi_union_pw_aff_domain(mupa);
9735 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9737 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9740 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9741 * with the domain of "pma".
9742 * The result is defined over the shared domain of the elements of "mupa"
9744 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9745 __isl_take isl_multi_union_pw_aff *mupa,
9746 __isl_take isl_pw_multi_aff *pma)
9748 isl_space *space1, *space2;
9749 isl_multi_union_pw_aff *res;
9750 isl_bool equal;
9751 int i;
9752 isl_size n_in, n_out;
9754 mupa = isl_multi_union_pw_aff_align_params(mupa,
9755 isl_pw_multi_aff_get_space(pma));
9756 pma = isl_pw_multi_aff_align_params(pma,
9757 isl_multi_union_pw_aff_get_space(mupa));
9758 if (!mupa || !pma)
9759 goto error;
9761 space1 = isl_multi_union_pw_aff_get_space(mupa);
9762 space2 = isl_pw_multi_aff_get_domain_space(pma);
9763 equal = isl_space_is_equal(space1, space2);
9764 isl_space_free(space1);
9765 isl_space_free(space2);
9766 if (equal < 0)
9767 goto error;
9768 if (!equal)
9769 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9770 "spaces don't match", goto error);
9771 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9772 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9773 if (n_in < 0 || n_out < 0)
9774 goto error;
9775 if (n_in == 0)
9776 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9778 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9779 res = isl_multi_union_pw_aff_alloc(space1);
9781 for (i = 0; i < n_out; ++i) {
9782 isl_pw_aff *pa;
9783 isl_union_pw_aff *upa;
9785 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9786 upa = isl_multi_union_pw_aff_apply_pw_aff(
9787 isl_multi_union_pw_aff_copy(mupa), pa);
9788 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9791 isl_pw_multi_aff_free(pma);
9792 isl_multi_union_pw_aff_free(mupa);
9793 return res;
9794 error:
9795 isl_multi_union_pw_aff_free(mupa);
9796 isl_pw_multi_aff_free(pma);
9797 return NULL;
9800 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9801 * If the explicit domain only keeps track of constraints on the parameters,
9802 * then only update those constraints.
9804 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9805 __isl_take isl_multi_union_pw_aff *mupa,
9806 __isl_keep isl_union_pw_multi_aff *upma)
9808 isl_bool is_params;
9810 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9811 return isl_multi_union_pw_aff_free(mupa);
9813 mupa = isl_multi_union_pw_aff_cow(mupa);
9814 if (!mupa)
9815 return NULL;
9817 is_params = isl_union_set_is_params(mupa->u.dom);
9818 if (is_params < 0)
9819 return isl_multi_union_pw_aff_free(mupa);
9821 upma = isl_union_pw_multi_aff_copy(upma);
9822 if (is_params)
9823 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9824 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9825 else
9826 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9827 mupa->u.dom, upma);
9828 if (!mupa->u.dom)
9829 return isl_multi_union_pw_aff_free(mupa);
9830 return mupa;
9833 /* Compute the pullback of "mupa" by the function represented by "upma".
9834 * In other words, plug in "upma" in "mupa". The result contains
9835 * expressions defined over the domain space of "upma".
9837 * Run over all elements of "mupa" and plug in "upma" in each of them.
9839 * If "mupa" has an explicit domain, then it is this domain
9840 * that needs to undergo a pullback instead, i.e., a preimage.
9842 __isl_give isl_multi_union_pw_aff *
9843 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9844 __isl_take isl_multi_union_pw_aff *mupa,
9845 __isl_take isl_union_pw_multi_aff *upma)
9847 int i;
9848 isl_size n;
9850 mupa = isl_multi_union_pw_aff_align_params(mupa,
9851 isl_union_pw_multi_aff_get_space(upma));
9852 upma = isl_union_pw_multi_aff_align_params(upma,
9853 isl_multi_union_pw_aff_get_space(mupa));
9854 mupa = isl_multi_union_pw_aff_cow(mupa);
9855 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9856 if (n < 0 || !upma)
9857 goto error;
9859 for (i = 0; i < n; ++i) {
9860 isl_union_pw_aff *upa;
9862 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9863 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9864 isl_union_pw_multi_aff_copy(upma));
9865 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9868 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9869 mupa = preimage_explicit_domain(mupa, upma);
9871 isl_union_pw_multi_aff_free(upma);
9872 return mupa;
9873 error:
9874 isl_multi_union_pw_aff_free(mupa);
9875 isl_union_pw_multi_aff_free(upma);
9876 return NULL;
9879 /* Extract the sequence of elements in "mupa" with domain space "space"
9880 * (ignoring parameters).
9882 * For the elements of "mupa" that are not defined on the specified space,
9883 * the corresponding element in the result is empty.
9885 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9886 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9888 int i;
9889 isl_size n;
9890 isl_space *space_mpa;
9891 isl_multi_pw_aff *mpa;
9893 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9894 if (n < 0 || !space)
9895 goto error;
9897 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9898 space = isl_space_replace_params(space, space_mpa);
9899 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9900 space_mpa);
9901 mpa = isl_multi_pw_aff_alloc(space_mpa);
9903 space = isl_space_from_domain(space);
9904 space = isl_space_add_dims(space, isl_dim_out, 1);
9905 for (i = 0; i < n; ++i) {
9906 isl_union_pw_aff *upa;
9907 isl_pw_aff *pa;
9909 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9910 pa = isl_union_pw_aff_extract_pw_aff(upa,
9911 isl_space_copy(space));
9912 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9913 isl_union_pw_aff_free(upa);
9916 isl_space_free(space);
9917 return mpa;
9918 error:
9919 isl_space_free(space);
9920 return NULL;
9923 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9924 * should modify the base expressions in the input.
9926 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9927 * are taken into account.
9928 * "fn" is applied to each entry in the input.
9930 struct isl_union_pw_multi_aff_un_op_control {
9931 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9932 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9935 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9936 * a second argument) for use as an isl_union_pw_multi_aff_transform
9937 * filter function (which does take a second argument).
9938 * Simply call control->filter without the second argument.
9940 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9941 __isl_take isl_pw_multi_aff *pma, void *user)
9943 struct isl_union_pw_multi_aff_un_op_control *control = user;
9945 return control->filter(pma);
9948 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9949 * a second argument) for use as an isl_union_pw_multi_aff_transform
9950 * base function (which does take a second argument).
9951 * Simply call control->fn without the second argument.
9953 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9954 __isl_take isl_pw_multi_aff *pma, void *user)
9956 struct isl_union_pw_multi_aff_un_op_control *control = user;
9958 return control->fn(pma);
9961 /* Construct an isl_union_pw_multi_aff that is obtained by
9962 * modifying "upma" according to "control".
9964 * isl_union_pw_multi_aff_transform performs essentially
9965 * the same operation, but takes a filter and a callback function
9966 * of a different form (with an extra argument).
9967 * Call isl_union_pw_multi_aff_transform with wrappers
9968 * that remove this extra argument.
9970 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9971 __isl_take isl_union_pw_multi_aff *upma,
9972 struct isl_union_pw_multi_aff_un_op_control *control)
9974 struct isl_union_pw_multi_aff_transform_control t_control = {
9975 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9976 .filter_user = control,
9977 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9978 .fn_user = control,
9981 return isl_union_pw_multi_aff_transform(upma, &t_control);
9984 /* For each function in "upma" of the form A -> [B -> C],
9985 * extract the function A -> B and collect the results.
9987 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9988 __isl_take isl_union_pw_multi_aff *upma)
9990 struct isl_union_pw_multi_aff_un_op_control control = {
9991 .filter = &isl_pw_multi_aff_range_is_wrapping,
9992 .fn = &isl_pw_multi_aff_range_factor_domain,
9994 return isl_union_pw_multi_aff_un_op(upma, &control);
9997 /* For each function in "upma" of the form A -> [B -> C],
9998 * extract the function A -> C and collect the results.
10000 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
10001 __isl_take isl_union_pw_multi_aff *upma)
10003 struct isl_union_pw_multi_aff_un_op_control control = {
10004 .filter = &isl_pw_multi_aff_range_is_wrapping,
10005 .fn = &isl_pw_multi_aff_range_factor_range,
10007 return isl_union_pw_multi_aff_un_op(upma, &control);
10010 /* Evaluate the affine function "aff" in the void point "pnt".
10011 * In particular, return the value NaN.
10013 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
10014 __isl_take isl_point *pnt)
10016 isl_ctx *ctx;
10018 ctx = isl_point_get_ctx(pnt);
10019 isl_aff_free(aff);
10020 isl_point_free(pnt);
10021 return isl_val_nan(ctx);
10024 /* Evaluate the affine expression "aff"
10025 * in the coordinates (with denominator) "pnt".
10027 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
10028 __isl_keep isl_vec *pnt)
10030 isl_int n, d;
10031 isl_ctx *ctx;
10032 isl_val *v;
10034 if (!aff || !pnt)
10035 return NULL;
10037 ctx = isl_vec_get_ctx(aff);
10038 isl_int_init(n);
10039 isl_int_init(d);
10040 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
10041 isl_int_mul(d, aff->el[0], pnt->el[0]);
10042 v = isl_val_rat_from_isl_int(ctx, n, d);
10043 v = isl_val_normalize(v);
10044 isl_int_clear(n);
10045 isl_int_clear(d);
10047 return v;
10050 /* Check that the domain space of "aff" is equal to "space".
10052 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
10053 __isl_keep isl_space *space)
10055 isl_bool ok;
10057 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
10058 if (ok < 0)
10059 return isl_stat_error;
10060 if (!ok)
10061 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
10062 "incompatible spaces", return isl_stat_error);
10063 return isl_stat_ok;
10066 /* Evaluate the affine function "aff" in "pnt".
10068 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
10069 __isl_take isl_point *pnt)
10071 isl_bool is_void;
10072 isl_val *v;
10073 isl_local_space *ls;
10075 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
10076 goto error;
10077 is_void = isl_point_is_void(pnt);
10078 if (is_void < 0)
10079 goto error;
10080 if (is_void)
10081 return eval_void(aff, pnt);
10083 ls = isl_aff_get_domain_local_space(aff);
10084 pnt = isl_local_space_lift_point(ls, pnt);
10086 v = eval(aff->v, isl_point_peek_vec(pnt));
10088 isl_aff_free(aff);
10089 isl_point_free(pnt);
10091 return v;
10092 error:
10093 isl_aff_free(aff);
10094 isl_point_free(pnt);
10095 return NULL;