isl_aff_drop_dims: drop unused local variable
[isl.git] / isl_aff.c
blob6137ce5ac53dd995135e681f5cef13f4866b0990
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 if (!aff)
2594 return NULL;
2595 if (type == isl_dim_out)
2596 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2597 "cannot drop output/set dimension",
2598 return isl_aff_free(aff));
2599 if (type == isl_dim_in)
2600 type = isl_dim_set;
2601 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2602 return aff;
2604 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2605 return isl_aff_free(aff);
2607 aff = isl_aff_cow(aff);
2608 if (!aff)
2609 return NULL;
2611 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2612 if (!aff->ls)
2613 return isl_aff_free(aff);
2615 first += 1 + isl_local_space_offset(aff->ls, type);
2616 aff->v = isl_vec_drop_els(aff->v, first, n);
2617 if (!aff->v)
2618 return isl_aff_free(aff);
2620 return aff;
2623 /* Is the domain of "aff" a product?
2625 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2627 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2630 #undef TYPE
2631 #define TYPE isl_aff
2632 #include <isl_domain_factor_templ.c>
2634 /* Project the domain of the affine expression onto its parameter space.
2635 * The affine expression may not involve any of the domain dimensions.
2637 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2639 isl_space *space;
2640 isl_size n;
2642 n = isl_aff_dim(aff, isl_dim_in);
2643 if (n < 0)
2644 return isl_aff_free(aff);
2645 aff = isl_aff_drop_domain(aff, 0, n);
2646 space = isl_aff_get_domain_space(aff);
2647 space = isl_space_params(space);
2648 aff = isl_aff_reset_domain_space(aff, space);
2649 return aff;
2652 /* Convert an affine expression defined over a parameter domain
2653 * into one that is defined over a zero-dimensional set.
2655 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2657 isl_local_space *ls;
2659 ls = isl_aff_take_domain_local_space(aff);
2660 ls = isl_local_space_set_from_params(ls);
2661 aff = isl_aff_restore_domain_local_space(aff, ls);
2663 return aff;
2666 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2667 enum isl_dim_type type, unsigned first, unsigned n)
2669 isl_ctx *ctx;
2671 if (!aff)
2672 return NULL;
2673 if (type == isl_dim_out)
2674 isl_die(aff->v->ctx, isl_error_invalid,
2675 "cannot insert output/set dimensions",
2676 return isl_aff_free(aff));
2677 if (type == isl_dim_in)
2678 type = isl_dim_set;
2679 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2680 return aff;
2682 ctx = isl_aff_get_ctx(aff);
2683 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2684 return isl_aff_free(aff);
2686 aff = isl_aff_cow(aff);
2687 if (!aff)
2688 return NULL;
2690 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2691 if (!aff->ls)
2692 return isl_aff_free(aff);
2694 first += 1 + isl_local_space_offset(aff->ls, type);
2695 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2696 if (!aff->v)
2697 return isl_aff_free(aff);
2699 return aff;
2702 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2703 enum isl_dim_type type, unsigned n)
2705 isl_size pos;
2707 pos = isl_aff_dim(aff, type);
2708 if (pos < 0)
2709 return isl_aff_free(aff);
2711 return isl_aff_insert_dims(aff, type, pos, n);
2714 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2715 * to dimensions of "dst_type" at "dst_pos".
2717 * We only support moving input dimensions to parameters and vice versa.
2719 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2720 enum isl_dim_type dst_type, unsigned dst_pos,
2721 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2723 unsigned g_dst_pos;
2724 unsigned g_src_pos;
2725 isl_size src_off, dst_off;
2727 if (!aff)
2728 return NULL;
2729 if (n == 0 &&
2730 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2731 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2732 return aff;
2734 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2735 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2736 "cannot move output/set dimension",
2737 return isl_aff_free(aff));
2738 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2739 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2740 "cannot move divs", return isl_aff_free(aff));
2741 if (dst_type == isl_dim_in)
2742 dst_type = isl_dim_set;
2743 if (src_type == isl_dim_in)
2744 src_type = isl_dim_set;
2746 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2747 return isl_aff_free(aff);
2748 if (dst_type == src_type)
2749 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2750 "moving dims within the same type not supported",
2751 return isl_aff_free(aff));
2753 aff = isl_aff_cow(aff);
2754 src_off = isl_aff_domain_offset(aff, src_type);
2755 dst_off = isl_aff_domain_offset(aff, dst_type);
2756 if (src_off < 0 || dst_off < 0)
2757 return isl_aff_free(aff);
2759 g_src_pos = 1 + src_off + src_pos;
2760 g_dst_pos = 1 + dst_off + dst_pos;
2761 if (dst_type > src_type)
2762 g_dst_pos -= n;
2764 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2765 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2766 src_type, src_pos, n);
2767 if (!aff->v || !aff->ls)
2768 return isl_aff_free(aff);
2770 aff = sort_divs(aff);
2772 return aff;
2775 /* Return a zero isl_aff in the given space.
2777 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2778 * interface over all piecewise types.
2780 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2782 isl_local_space *ls;
2784 ls = isl_local_space_from_space(isl_space_domain(space));
2785 return isl_aff_zero_on_domain(ls);
2788 #define isl_aff_involves_nan isl_aff_is_nan
2790 #undef PW
2791 #define PW isl_pw_aff
2792 #undef BASE
2793 #define BASE aff
2794 #undef EL_IS_ZERO
2795 #define EL_IS_ZERO is_empty
2796 #undef ZERO
2797 #define ZERO empty
2798 #undef IS_ZERO
2799 #define IS_ZERO is_empty
2800 #undef FIELD
2801 #define FIELD aff
2802 #undef DEFAULT_IS_ZERO
2803 #define DEFAULT_IS_ZERO 0
2805 #include <isl_pw_templ.c>
2806 #include <isl_pw_un_op_templ.c>
2807 #include <isl_pw_add_constant_val_templ.c>
2808 #include <isl_pw_bind_domain_templ.c>
2809 #include <isl_pw_eval.c>
2810 #include <isl_pw_hash.c>
2811 #include <isl_pw_insert_dims_templ.c>
2812 #include <isl_pw_insert_domain_templ.c>
2813 #include <isl_pw_move_dims_templ.c>
2814 #include <isl_pw_neg_templ.c>
2815 #include <isl_pw_pullback_templ.c>
2816 #include <isl_pw_sub_templ.c>
2817 #include <isl_pw_union_opt.c>
2819 #undef BASE
2820 #define BASE pw_aff
2822 #include <isl_union_single.c>
2823 #include <isl_union_neg.c>
2825 #undef BASE
2826 #define BASE aff
2828 #include <isl_union_pw_templ.c>
2830 /* Compute a piecewise quasi-affine expression with a domain that
2831 * is the union of those of pwaff1 and pwaff2 and such that on each
2832 * cell, the quasi-affine expression is the maximum of those of pwaff1
2833 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2834 * cell, then the associated expression is the defined one.
2836 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2837 __isl_take isl_pw_aff *pwaff2)
2839 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2840 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2843 /* Compute a piecewise quasi-affine expression with a domain that
2844 * is the union of those of pwaff1 and pwaff2 and such that on each
2845 * cell, the quasi-affine expression is the minimum of those of pwaff1
2846 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2847 * cell, then the associated expression is the defined one.
2849 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2850 __isl_take isl_pw_aff *pwaff2)
2852 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2853 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2856 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2857 __isl_take isl_pw_aff *pwaff2, int max)
2859 if (max)
2860 return isl_pw_aff_union_max(pwaff1, pwaff2);
2861 else
2862 return isl_pw_aff_union_min(pwaff1, pwaff2);
2865 /* Is the domain of "pa" a product?
2867 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2869 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2872 #undef TYPE
2873 #define TYPE isl_pw_aff
2874 #include <isl_domain_factor_templ.c>
2876 /* Return a set containing those elements in the domain
2877 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2878 * does not satisfy "fn" (if complement is 1).
2880 * The pieces with a NaN never belong to the result since
2881 * NaN does not satisfy any property.
2883 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2884 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2885 void *user),
2886 int complement, void *user)
2888 int i;
2889 isl_set *set;
2891 if (!pwaff)
2892 return NULL;
2894 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2896 for (i = 0; i < pwaff->n; ++i) {
2897 isl_basic_set *bset;
2898 isl_set *set_i, *locus;
2899 isl_bool rational;
2901 if (isl_aff_is_nan(pwaff->p[i].aff))
2902 continue;
2904 rational = isl_set_has_rational(pwaff->p[i].set);
2905 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2906 locus = isl_set_from_basic_set(bset);
2907 set_i = isl_set_copy(pwaff->p[i].set);
2908 if (complement)
2909 set_i = isl_set_subtract(set_i, locus);
2910 else
2911 set_i = isl_set_intersect(set_i, locus);
2912 set = isl_set_union_disjoint(set, set_i);
2915 isl_pw_aff_free(pwaff);
2917 return set;
2920 /* Return a set containing those elements in the domain
2921 * of "pa" where it is positive.
2923 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2925 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2928 /* Return a set containing those elements in the domain
2929 * of pwaff where it is non-negative.
2931 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2933 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2936 /* Return a set containing those elements in the domain
2937 * of pwaff where it is zero.
2939 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2941 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2944 /* Return a set containing those elements in the domain
2945 * of pwaff where it is not zero.
2947 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2949 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2952 /* Bind the affine function "aff" to the parameter "id",
2953 * returning the elements in the domain where the affine expression
2954 * is equal to the parameter.
2956 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2957 __isl_take isl_id *id)
2959 isl_space *space;
2960 isl_aff *aff_id;
2962 space = isl_aff_get_domain_space(aff);
2963 space = isl_space_add_param_id(space, isl_id_copy(id));
2965 aff = isl_aff_align_params(aff, isl_space_copy(space));
2966 aff_id = isl_aff_param_on_domain_space_id(space, id);
2968 return isl_aff_eq_basic_set(aff, aff_id);
2971 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2972 * "rational" should not be set.
2974 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2975 int rational, void *user)
2977 isl_id *id = user;
2979 if (!aff)
2980 return NULL;
2981 if (rational)
2982 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2983 "rational binding not supported", goto error);
2984 return isl_aff_bind_id(aff, isl_id_copy(id));
2985 error:
2986 isl_aff_free(aff);
2987 return NULL;
2990 /* Bind the piecewise affine function "pa" to the parameter "id",
2991 * returning the elements in the domain where the expression
2992 * is equal to the parameter.
2994 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2995 __isl_take isl_id *id)
2997 isl_set *bound;
2999 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
3000 isl_id_free(id);
3002 return bound;
3005 /* Return a set containing those elements in the shared domain
3006 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3008 * We compute the difference on the shared domain and then construct
3009 * the set of values where this difference is non-negative.
3010 * If strict is set, we first subtract 1 from the difference.
3011 * If equal is set, we only return the elements where pwaff1 and pwaff2
3012 * are equal.
3014 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3015 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3017 isl_set *set1, *set2;
3019 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3020 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3021 set1 = isl_set_intersect(set1, set2);
3022 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3023 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3024 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3026 if (strict) {
3027 isl_space *space = isl_set_get_space(set1);
3028 isl_aff *aff;
3029 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3030 aff = isl_aff_add_constant_si(aff, -1);
3031 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3032 } else
3033 isl_set_free(set1);
3035 if (equal)
3036 return isl_pw_aff_zero_set(pwaff1);
3037 return isl_pw_aff_nonneg_set(pwaff1);
3040 /* Return a set containing those elements in the shared domain
3041 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3043 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3044 __isl_take isl_pw_aff *pwaff2)
3046 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3047 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3050 /* Return a set containing those elements in the shared domain
3051 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3053 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3054 __isl_take isl_pw_aff *pwaff2)
3056 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3057 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3060 /* Return a set containing those elements in the shared domain
3061 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3063 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3064 __isl_take isl_pw_aff *pwaff2)
3066 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3067 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3070 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3071 __isl_take isl_pw_aff *pwaff2)
3073 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3076 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3077 __isl_take isl_pw_aff *pwaff2)
3079 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3082 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3083 * where the function values are ordered in the same way as "order",
3084 * which returns a set in the shared domain of its two arguments.
3086 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3087 * We first pull back the two functions such that they are defined on
3088 * the domain [A -> B]. Then we apply "order", resulting in a set
3089 * in the space [A -> B]. Finally, we unwrap this set to obtain
3090 * a map in the space A -> B.
3092 static __isl_give isl_map *isl_pw_aff_order_map(
3093 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3094 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3095 __isl_take isl_pw_aff *pa2))
3097 isl_space *space1, *space2;
3098 isl_multi_aff *ma;
3099 isl_set *set;
3101 isl_pw_aff_align_params_bin(&pa1, &pa2);
3102 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3103 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3104 space1 = isl_space_map_from_domain_and_range(space1, space2);
3105 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3106 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3107 ma = isl_multi_aff_range_map(space1);
3108 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3109 set = order(pa1, pa2);
3111 return isl_set_unwrap(set);
3114 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3115 * where the function values are equal.
3117 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3118 __isl_take isl_pw_aff *pa2)
3120 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3123 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3124 * where the function value of "pa1" is less than or equal to
3125 * the function value of "pa2".
3127 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3128 __isl_take isl_pw_aff *pa2)
3130 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3133 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3134 * where the function value of "pa1" is less than the function value of "pa2".
3136 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3137 __isl_take isl_pw_aff *pa2)
3139 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3142 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3143 * where the function value of "pa1" is greater than or equal to
3144 * the function value of "pa2".
3146 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3147 __isl_take isl_pw_aff *pa2)
3149 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3152 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3153 * where the function value of "pa1" is greater than the function value
3154 * of "pa2".
3156 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3157 __isl_take isl_pw_aff *pa2)
3159 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3162 /* Return a set containing those elements in the shared domain
3163 * of the elements of list1 and list2 where each element in list1
3164 * has the relation specified by "fn" with each element in list2.
3166 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3167 __isl_take isl_pw_aff_list *list2,
3168 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3169 __isl_take isl_pw_aff *pwaff2))
3171 int i, j;
3172 isl_ctx *ctx;
3173 isl_set *set;
3175 if (!list1 || !list2)
3176 goto error;
3178 ctx = isl_pw_aff_list_get_ctx(list1);
3179 if (list1->n < 1 || list2->n < 1)
3180 isl_die(ctx, isl_error_invalid,
3181 "list should contain at least one element", goto error);
3183 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3184 for (i = 0; i < list1->n; ++i)
3185 for (j = 0; j < list2->n; ++j) {
3186 isl_set *set_ij;
3188 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3189 isl_pw_aff_copy(list2->p[j]));
3190 set = isl_set_intersect(set, set_ij);
3193 isl_pw_aff_list_free(list1);
3194 isl_pw_aff_list_free(list2);
3195 return set;
3196 error:
3197 isl_pw_aff_list_free(list1);
3198 isl_pw_aff_list_free(list2);
3199 return NULL;
3202 /* Return a set containing those elements in the shared domain
3203 * of the elements of list1 and list2 where each element in list1
3204 * is equal to each element in list2.
3206 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3207 __isl_take isl_pw_aff_list *list2)
3209 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3212 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3213 __isl_take isl_pw_aff_list *list2)
3215 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3218 /* Return a set containing those elements in the shared domain
3219 * of the elements of list1 and list2 where each element in list1
3220 * is less than or equal to each element in list2.
3222 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3223 __isl_take isl_pw_aff_list *list2)
3225 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3228 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3229 __isl_take isl_pw_aff_list *list2)
3231 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3234 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3235 __isl_take isl_pw_aff_list *list2)
3237 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3240 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3241 __isl_take isl_pw_aff_list *list2)
3243 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3247 /* Return a set containing those elements in the shared domain
3248 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3250 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3251 __isl_take isl_pw_aff *pwaff2)
3253 isl_set *set_lt, *set_gt;
3255 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3256 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3257 isl_pw_aff_copy(pwaff2));
3258 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3259 return isl_set_union_disjoint(set_lt, set_gt);
3262 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3263 isl_int v)
3265 int i;
3267 if (isl_int_is_one(v))
3268 return pwaff;
3269 if (!isl_int_is_pos(v))
3270 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3271 "factor needs to be positive",
3272 return isl_pw_aff_free(pwaff));
3273 pwaff = isl_pw_aff_cow(pwaff);
3274 if (!pwaff)
3275 return NULL;
3276 if (pwaff->n == 0)
3277 return pwaff;
3279 for (i = 0; i < pwaff->n; ++i) {
3280 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3281 if (!pwaff->p[i].aff)
3282 return isl_pw_aff_free(pwaff);
3285 return pwaff;
3288 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3290 return isl_pw_aff_un_op(pwaff, &isl_aff_floor);
3293 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3295 return isl_pw_aff_un_op(pwaff, &isl_aff_ceil);
3298 /* Assuming that "cond1" and "cond2" are disjoint,
3299 * return an affine expression that is equal to pwaff1 on cond1
3300 * and to pwaff2 on cond2.
3302 static __isl_give isl_pw_aff *isl_pw_aff_select(
3303 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3304 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3306 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3307 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3309 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3312 /* Return an affine expression that is equal to pwaff_true for elements
3313 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3314 * is zero.
3315 * That is, return cond ? pwaff_true : pwaff_false;
3317 * If "cond" involves and NaN, then we conservatively return a NaN
3318 * on its entire domain. In principle, we could consider the pieces
3319 * where it is NaN separately from those where it is not.
3321 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3322 * then only use the domain of "cond" to restrict the domain.
3324 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3325 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3327 isl_set *cond_true, *cond_false;
3328 isl_bool equal;
3330 if (!cond)
3331 goto error;
3332 if (isl_pw_aff_involves_nan(cond)) {
3333 isl_space *space = isl_pw_aff_get_domain_space(cond);
3334 isl_local_space *ls = isl_local_space_from_space(space);
3335 isl_pw_aff_free(cond);
3336 isl_pw_aff_free(pwaff_true);
3337 isl_pw_aff_free(pwaff_false);
3338 return isl_pw_aff_nan_on_domain(ls);
3341 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3342 isl_pw_aff_get_space(pwaff_false));
3343 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3344 isl_pw_aff_get_space(pwaff_true));
3345 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3346 if (equal < 0)
3347 goto error;
3348 if (equal) {
3349 isl_set *dom;
3351 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3352 isl_pw_aff_free(pwaff_false);
3353 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3356 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3357 cond_false = isl_pw_aff_zero_set(cond);
3358 return isl_pw_aff_select(cond_true, pwaff_true,
3359 cond_false, pwaff_false);
3360 error:
3361 isl_pw_aff_free(cond);
3362 isl_pw_aff_free(pwaff_true);
3363 isl_pw_aff_free(pwaff_false);
3364 return NULL;
3367 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3369 int pos;
3371 if (!aff)
3372 return isl_bool_error;
3374 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3375 return isl_bool_ok(pos == -1);
3378 /* Check whether pwaff is a piecewise constant.
3380 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3382 int i;
3384 if (!pwaff)
3385 return isl_bool_error;
3387 for (i = 0; i < pwaff->n; ++i) {
3388 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3389 if (is_cst < 0 || !is_cst)
3390 return is_cst;
3393 return isl_bool_true;
3396 /* Return the product of "aff1" and "aff2".
3398 * If either of the two is NaN, then the result is NaN.
3400 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3402 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3403 __isl_take isl_aff *aff2)
3405 if (!aff1 || !aff2)
3406 goto error;
3408 if (isl_aff_is_nan(aff1)) {
3409 isl_aff_free(aff2);
3410 return aff1;
3412 if (isl_aff_is_nan(aff2)) {
3413 isl_aff_free(aff1);
3414 return aff2;
3417 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3418 return isl_aff_mul(aff2, aff1);
3420 if (!isl_aff_is_cst(aff2))
3421 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3422 "at least one affine expression should be constant",
3423 goto error);
3425 aff1 = isl_aff_cow(aff1);
3426 if (!aff1 || !aff2)
3427 goto error;
3429 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3430 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3432 isl_aff_free(aff2);
3433 return aff1;
3434 error:
3435 isl_aff_free(aff1);
3436 isl_aff_free(aff2);
3437 return NULL;
3440 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3442 * If either of the two is NaN, then the result is NaN.
3443 * A division by zero also results in NaN.
3445 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3446 __isl_take isl_aff *aff2)
3448 isl_bool is_cst, is_zero;
3449 int neg;
3451 if (!aff1 || !aff2)
3452 goto error;
3454 if (isl_aff_is_nan(aff1)) {
3455 isl_aff_free(aff2);
3456 return aff1;
3458 if (isl_aff_is_nan(aff2)) {
3459 isl_aff_free(aff1);
3460 return aff2;
3463 is_cst = isl_aff_is_cst(aff2);
3464 if (is_cst < 0)
3465 goto error;
3466 if (!is_cst)
3467 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3468 "second argument should be a constant", goto error);
3469 is_zero = isl_aff_plain_is_zero(aff2);
3470 if (is_zero < 0)
3471 goto error;
3472 if (is_zero)
3473 return set_nan_free(aff1, aff2);
3475 neg = isl_int_is_neg(aff2->v->el[1]);
3476 if (neg) {
3477 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3478 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3481 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3482 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3484 if (neg) {
3485 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3486 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3489 isl_aff_free(aff2);
3490 return aff1;
3491 error:
3492 isl_aff_free(aff1);
3493 isl_aff_free(aff2);
3494 return NULL;
3497 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3498 __isl_take isl_pw_aff *pwaff2)
3500 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3501 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3504 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3505 __isl_take isl_pw_aff *pwaff2)
3507 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3510 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3511 __isl_take isl_pw_aff *pwaff2)
3513 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3514 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3517 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3519 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3520 __isl_take isl_pw_aff *pa2)
3522 int is_cst;
3524 is_cst = isl_pw_aff_is_cst(pa2);
3525 if (is_cst < 0)
3526 goto error;
3527 if (!is_cst)
3528 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3529 "second argument should be a piecewise constant",
3530 goto error);
3531 isl_pw_aff_align_params_bin(&pa1, &pa2);
3532 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3533 error:
3534 isl_pw_aff_free(pa1);
3535 isl_pw_aff_free(pa2);
3536 return NULL;
3539 /* Compute the quotient of the integer division of "pa1" by "pa2"
3540 * with rounding towards zero.
3541 * "pa2" is assumed to be a piecewise constant.
3543 * In particular, return
3545 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3548 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3549 __isl_take isl_pw_aff *pa2)
3551 int is_cst;
3552 isl_set *cond;
3553 isl_pw_aff *f, *c;
3555 is_cst = isl_pw_aff_is_cst(pa2);
3556 if (is_cst < 0)
3557 goto error;
3558 if (!is_cst)
3559 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3560 "second argument should be a piecewise constant",
3561 goto error);
3563 pa1 = isl_pw_aff_div(pa1, pa2);
3565 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3566 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3567 c = isl_pw_aff_ceil(pa1);
3568 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3569 error:
3570 isl_pw_aff_free(pa1);
3571 isl_pw_aff_free(pa2);
3572 return NULL;
3575 /* Compute the remainder of the integer division of "pa1" by "pa2"
3576 * with rounding towards zero.
3577 * "pa2" is assumed to be a piecewise constant.
3579 * In particular, return
3581 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3584 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3585 __isl_take isl_pw_aff *pa2)
3587 int is_cst;
3588 isl_pw_aff *res;
3590 is_cst = isl_pw_aff_is_cst(pa2);
3591 if (is_cst < 0)
3592 goto error;
3593 if (!is_cst)
3594 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3595 "second argument should be a piecewise constant",
3596 goto error);
3597 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3598 res = isl_pw_aff_mul(pa2, res);
3599 res = isl_pw_aff_sub(pa1, res);
3600 return res;
3601 error:
3602 isl_pw_aff_free(pa1);
3603 isl_pw_aff_free(pa2);
3604 return NULL;
3607 /* Does either of "pa1" or "pa2" involve any NaN2?
3609 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3610 __isl_keep isl_pw_aff *pa2)
3612 isl_bool has_nan;
3614 has_nan = isl_pw_aff_involves_nan(pa1);
3615 if (has_nan < 0 || has_nan)
3616 return has_nan;
3617 return isl_pw_aff_involves_nan(pa2);
3620 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3621 * by a NaN on their shared domain.
3623 * In principle, the result could be refined to only being NaN
3624 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3626 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3627 __isl_take isl_pw_aff *pa2)
3629 isl_local_space *ls;
3630 isl_set *dom;
3631 isl_pw_aff *pa;
3633 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3634 ls = isl_local_space_from_space(isl_set_get_space(dom));
3635 pa = isl_pw_aff_nan_on_domain(ls);
3636 pa = isl_pw_aff_intersect_domain(pa, dom);
3638 return pa;
3641 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3642 __isl_take isl_pw_aff *pwaff2)
3644 isl_set *le;
3645 isl_set *dom;
3647 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3648 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3649 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3650 isl_pw_aff_copy(pwaff2));
3651 dom = isl_set_subtract(dom, isl_set_copy(le));
3652 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3655 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3656 __isl_take isl_pw_aff *pwaff2)
3658 isl_set *ge;
3659 isl_set *dom;
3661 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3662 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3663 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3664 isl_pw_aff_copy(pwaff2));
3665 dom = isl_set_subtract(dom, isl_set_copy(ge));
3666 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3669 /* Return an expression for the minimum (if "max" is not set) or
3670 * the maximum (if "max" is set) of "pa1" and "pa2".
3671 * If either expression involves any NaN, then return a NaN
3672 * on the shared domain as result.
3674 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3675 __isl_take isl_pw_aff *pa2, int max)
3677 isl_bool has_nan;
3679 has_nan = either_involves_nan(pa1, pa2);
3680 if (has_nan < 0)
3681 pa1 = isl_pw_aff_free(pa1);
3682 else if (has_nan)
3683 return replace_by_nan(pa1, pa2);
3685 isl_pw_aff_align_params_bin(&pa1, &pa2);
3686 if (max)
3687 return pw_aff_max(pa1, pa2);
3688 else
3689 return pw_aff_min(pa1, pa2);
3692 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3694 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3695 __isl_take isl_pw_aff *pwaff2)
3697 return pw_aff_min_max(pwaff1, pwaff2, 0);
3700 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3702 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3703 __isl_take isl_pw_aff *pwaff2)
3705 return pw_aff_min_max(pwaff1, pwaff2, 1);
3708 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3709 __isl_take isl_pw_aff_list *list,
3710 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3711 __isl_take isl_pw_aff *pwaff2))
3713 int i;
3714 isl_ctx *ctx;
3715 isl_pw_aff *res;
3717 if (!list)
3718 return NULL;
3720 ctx = isl_pw_aff_list_get_ctx(list);
3721 if (list->n < 1)
3722 isl_die(ctx, isl_error_invalid,
3723 "list should contain at least one element", goto error);
3725 res = isl_pw_aff_copy(list->p[0]);
3726 for (i = 1; i < list->n; ++i)
3727 res = fn(res, isl_pw_aff_copy(list->p[i]));
3729 isl_pw_aff_list_free(list);
3730 return res;
3731 error:
3732 isl_pw_aff_list_free(list);
3733 return NULL;
3736 /* Return an isl_pw_aff that maps each element in the intersection of the
3737 * domains of the elements of list to the minimal corresponding affine
3738 * expression.
3740 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3742 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3745 /* Return an isl_pw_aff that maps each element in the intersection of the
3746 * domains of the elements of list to the maximal corresponding affine
3747 * expression.
3749 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3751 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3754 /* Mark the domains of "pwaff" as rational.
3756 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3758 int i;
3760 pwaff = isl_pw_aff_cow(pwaff);
3761 if (!pwaff)
3762 return NULL;
3763 if (pwaff->n == 0)
3764 return pwaff;
3766 for (i = 0; i < pwaff->n; ++i) {
3767 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3768 if (!pwaff->p[i].set)
3769 return isl_pw_aff_free(pwaff);
3772 return pwaff;
3775 /* Mark the domains of the elements of "list" as rational.
3777 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3778 __isl_take isl_pw_aff_list *list)
3780 int i, n;
3782 if (!list)
3783 return NULL;
3784 if (list->n == 0)
3785 return list;
3787 n = list->n;
3788 for (i = 0; i < n; ++i) {
3789 isl_pw_aff *pa;
3791 pa = isl_pw_aff_list_get_pw_aff(list, i);
3792 pa = isl_pw_aff_set_rational(pa);
3793 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3796 return list;
3799 /* Do the parameters of "aff" match those of "space"?
3801 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3802 __isl_keep isl_space *space)
3804 isl_space *aff_space;
3805 isl_bool match;
3807 if (!aff || !space)
3808 return isl_bool_error;
3810 aff_space = isl_aff_get_domain_space(aff);
3812 match = isl_space_has_equal_params(space, aff_space);
3814 isl_space_free(aff_space);
3815 return match;
3818 /* Check that the domain space of "aff" matches "space".
3820 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3821 __isl_keep isl_space *space)
3823 isl_space *aff_space;
3824 isl_bool match;
3826 if (!aff || !space)
3827 return isl_stat_error;
3829 aff_space = isl_aff_get_domain_space(aff);
3831 match = isl_space_has_equal_params(space, aff_space);
3832 if (match < 0)
3833 goto error;
3834 if (!match)
3835 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3836 "parameters don't match", goto error);
3837 match = isl_space_tuple_is_equal(space, isl_dim_in,
3838 aff_space, isl_dim_set);
3839 if (match < 0)
3840 goto error;
3841 if (!match)
3842 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3843 "domains don't match", goto error);
3844 isl_space_free(aff_space);
3845 return isl_stat_ok;
3846 error:
3847 isl_space_free(aff_space);
3848 return isl_stat_error;
3851 /* Return the shared (universe) domain of the elements of "ma".
3853 * Since an isl_multi_aff (and an isl_aff) is always total,
3854 * the domain is always the universe set in its domain space.
3855 * This is a helper function for use in the generic isl_multi_*_bind.
3857 static __isl_give isl_basic_set *isl_multi_aff_domain(
3858 __isl_take isl_multi_aff *ma)
3860 isl_space *space;
3862 space = isl_multi_aff_get_space(ma);
3863 isl_multi_aff_free(ma);
3865 return isl_basic_set_universe(isl_space_domain(space));
3868 #undef BASE
3869 #define BASE aff
3871 #include <isl_multi_no_explicit_domain.c>
3872 #include <isl_multi_templ.c>
3873 #include <isl_multi_un_op_templ.c>
3874 #include <isl_multi_bin_val_templ.c>
3875 #include <isl_multi_add_constant_templ.c>
3876 #include <isl_multi_apply_set.c>
3877 #include <isl_multi_arith_templ.c>
3878 #include <isl_multi_bind_domain_templ.c>
3879 #include <isl_multi_cmp.c>
3880 #include <isl_multi_dim_id_templ.c>
3881 #include <isl_multi_dims.c>
3882 #include <isl_multi_floor.c>
3883 #include <isl_multi_from_base_templ.c>
3884 #include <isl_multi_identity_templ.c>
3885 #include <isl_multi_insert_domain_templ.c>
3886 #include <isl_multi_locals_templ.c>
3887 #include <isl_multi_move_dims_templ.c>
3888 #include <isl_multi_nan_templ.c>
3889 #include <isl_multi_product_templ.c>
3890 #include <isl_multi_splice_templ.c>
3891 #include <isl_multi_tuple_id_templ.c>
3892 #include <isl_multi_unbind_params_templ.c>
3893 #include <isl_multi_zero_templ.c>
3895 #undef DOMBASE
3896 #define DOMBASE set
3897 #include <isl_multi_gist.c>
3899 #undef DOMBASE
3900 #define DOMBASE basic_set
3901 #include <isl_multi_bind_templ.c>
3903 /* Construct an isl_multi_aff living in "space" that corresponds
3904 * to the affine transformation matrix "mat".
3906 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3907 __isl_take isl_space *space, __isl_take isl_mat *mat)
3909 isl_ctx *ctx;
3910 isl_local_space *ls = NULL;
3911 isl_multi_aff *ma = NULL;
3912 isl_size n_row, n_col, n_out, total;
3913 int i;
3915 if (!space || !mat)
3916 goto error;
3918 ctx = isl_mat_get_ctx(mat);
3920 n_row = isl_mat_rows(mat);
3921 n_col = isl_mat_cols(mat);
3922 n_out = isl_space_dim(space, isl_dim_out);
3923 total = isl_space_dim(space, isl_dim_all);
3924 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3925 goto error;
3926 if (n_row < 1)
3927 isl_die(ctx, isl_error_invalid,
3928 "insufficient number of rows", goto error);
3929 if (n_col < 1)
3930 isl_die(ctx, isl_error_invalid,
3931 "insufficient number of columns", goto error);
3932 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3933 isl_die(ctx, isl_error_invalid,
3934 "dimension mismatch", goto error);
3936 ma = isl_multi_aff_zero(isl_space_copy(space));
3937 space = isl_space_domain(space);
3938 ls = isl_local_space_from_space(isl_space_copy(space));
3940 for (i = 0; i < n_row - 1; ++i) {
3941 isl_vec *v;
3942 isl_aff *aff;
3944 v = isl_vec_alloc(ctx, 1 + n_col);
3945 if (!v)
3946 goto error;
3947 isl_int_set(v->el[0], mat->row[0][0]);
3948 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3949 v = isl_vec_normalize(v);
3950 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3951 ma = isl_multi_aff_set_aff(ma, i, aff);
3954 isl_space_free(space);
3955 isl_local_space_free(ls);
3956 isl_mat_free(mat);
3957 return ma;
3958 error:
3959 isl_space_free(space);
3960 isl_local_space_free(ls);
3961 isl_mat_free(mat);
3962 isl_multi_aff_free(ma);
3963 return NULL;
3966 /* Return the constant terms of the affine expressions of "ma".
3968 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3969 __isl_keep isl_multi_aff *ma)
3971 int i;
3972 isl_size n;
3973 isl_space *space;
3974 isl_multi_val *mv;
3976 n = isl_multi_aff_size(ma);
3977 if (n < 0)
3978 return NULL;
3979 space = isl_space_range(isl_multi_aff_get_space(ma));
3980 space = isl_space_drop_all_params(space);
3981 mv = isl_multi_val_zero(space);
3983 for (i = 0; i < n; ++i) {
3984 isl_aff *aff;
3985 isl_val *val;
3987 aff = isl_multi_aff_get_at(ma, i);
3988 val = isl_aff_get_constant_val(aff);
3989 isl_aff_free(aff);
3990 mv = isl_multi_val_set_at(mv, i, val);
3993 return mv;
3996 /* Remove any internal structure of the domain of "ma".
3997 * If there is any such internal structure in the input,
3998 * then the name of the corresponding space is also removed.
4000 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4001 __isl_take isl_multi_aff *ma)
4003 isl_space *space;
4005 if (!ma)
4006 return NULL;
4008 if (!ma->space->nested[0])
4009 return ma;
4011 space = isl_multi_aff_get_space(ma);
4012 space = isl_space_flatten_domain(space);
4013 ma = isl_multi_aff_reset_space(ma, space);
4015 return ma;
4018 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4019 * of the space to its domain.
4021 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4023 int i;
4024 isl_size n_in;
4025 isl_local_space *ls;
4026 isl_multi_aff *ma;
4028 if (!space)
4029 return NULL;
4030 if (!isl_space_is_map(space))
4031 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4032 "not a map space", goto error);
4034 n_in = isl_space_dim(space, isl_dim_in);
4035 if (n_in < 0)
4036 goto error;
4037 space = isl_space_domain_map(space);
4039 ma = isl_multi_aff_alloc(isl_space_copy(space));
4040 if (n_in == 0) {
4041 isl_space_free(space);
4042 return ma;
4045 space = isl_space_domain(space);
4046 ls = isl_local_space_from_space(space);
4047 for (i = 0; i < n_in; ++i) {
4048 isl_aff *aff;
4050 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4051 isl_dim_set, i);
4052 ma = isl_multi_aff_set_aff(ma, i, aff);
4054 isl_local_space_free(ls);
4055 return ma;
4056 error:
4057 isl_space_free(space);
4058 return NULL;
4061 /* This function performs the same operation as isl_multi_aff_domain_map,
4062 * but is considered as a function on an isl_space when exported.
4064 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4065 __isl_take isl_space *space)
4067 return isl_multi_aff_domain_map(space);
4070 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4071 * of the space to its range.
4073 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4075 int i;
4076 isl_size n_in, n_out;
4077 isl_local_space *ls;
4078 isl_multi_aff *ma;
4080 if (!space)
4081 return NULL;
4082 if (!isl_space_is_map(space))
4083 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4084 "not a map space", goto error);
4086 n_in = isl_space_dim(space, isl_dim_in);
4087 n_out = isl_space_dim(space, isl_dim_out);
4088 if (n_in < 0 || n_out < 0)
4089 goto error;
4090 space = isl_space_range_map(space);
4092 ma = isl_multi_aff_alloc(isl_space_copy(space));
4093 if (n_out == 0) {
4094 isl_space_free(space);
4095 return ma;
4098 space = isl_space_domain(space);
4099 ls = isl_local_space_from_space(space);
4100 for (i = 0; i < n_out; ++i) {
4101 isl_aff *aff;
4103 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4104 isl_dim_set, n_in + i);
4105 ma = isl_multi_aff_set_aff(ma, i, aff);
4107 isl_local_space_free(ls);
4108 return ma;
4109 error:
4110 isl_space_free(space);
4111 return NULL;
4114 /* This function performs the same operation as isl_multi_aff_range_map,
4115 * but is considered as a function on an isl_space when exported.
4117 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4118 __isl_take isl_space *space)
4120 return isl_multi_aff_range_map(space);
4123 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4124 * of the space to its domain.
4126 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4127 __isl_take isl_space *space)
4129 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4132 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4133 * but is considered as a function on an isl_space when exported.
4135 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4136 __isl_take isl_space *space)
4138 return isl_pw_multi_aff_domain_map(space);
4141 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4142 * of the space to its range.
4144 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4145 __isl_take isl_space *space)
4147 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4150 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4151 * but is considered as a function on an isl_space when exported.
4153 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4154 __isl_take isl_space *space)
4156 return isl_pw_multi_aff_range_map(space);
4159 /* Given the space of a set and a range of set dimensions,
4160 * construct an isl_multi_aff that projects out those dimensions.
4162 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4163 __isl_take isl_space *space, enum isl_dim_type type,
4164 unsigned first, unsigned n)
4166 int i;
4167 isl_size dim;
4168 isl_local_space *ls;
4169 isl_multi_aff *ma;
4171 if (!space)
4172 return NULL;
4173 if (!isl_space_is_set(space))
4174 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4175 "expecting set space", goto error);
4176 if (type != isl_dim_set)
4177 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4178 "only set dimensions can be projected out", goto error);
4179 if (isl_space_check_range(space, type, first, n) < 0)
4180 goto error;
4182 dim = isl_space_dim(space, isl_dim_set);
4183 if (dim < 0)
4184 goto error;
4186 space = isl_space_from_domain(space);
4187 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4189 if (dim == n)
4190 return isl_multi_aff_alloc(space);
4192 ma = isl_multi_aff_alloc(isl_space_copy(space));
4193 space = isl_space_domain(space);
4194 ls = isl_local_space_from_space(space);
4196 for (i = 0; i < first; ++i) {
4197 isl_aff *aff;
4199 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4200 isl_dim_set, i);
4201 ma = isl_multi_aff_set_aff(ma, i, aff);
4204 for (i = 0; i < dim - (first + n); ++i) {
4205 isl_aff *aff;
4207 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4208 isl_dim_set, first + n + i);
4209 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4212 isl_local_space_free(ls);
4213 return ma;
4214 error:
4215 isl_space_free(space);
4216 return NULL;
4219 /* Given the space of a set and a range of set dimensions,
4220 * construct an isl_pw_multi_aff that projects out those dimensions.
4222 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4223 __isl_take isl_space *space, enum isl_dim_type type,
4224 unsigned first, unsigned n)
4226 isl_multi_aff *ma;
4228 ma = isl_multi_aff_project_out_map(space, type, first, n);
4229 return isl_pw_multi_aff_from_multi_aff(ma);
4232 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4233 * but is considered as a function on an isl_multi_aff when exported.
4235 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4236 __isl_take isl_multi_aff *ma)
4238 return isl_pw_multi_aff_from_multi_aff(ma);
4241 /* Create a piecewise multi-affine expression in the given space that maps each
4242 * input dimension to the corresponding output dimension.
4244 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4245 __isl_take isl_space *space)
4247 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4250 /* Create a piecewise multi expression that maps elements in the given space
4251 * to themselves.
4253 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4254 __isl_take isl_space *space)
4256 isl_multi_aff *ma;
4258 ma = isl_multi_aff_identity_on_domain_space(space);
4259 return isl_pw_multi_aff_from_multi_aff(ma);
4262 /* This function performs the same operation as
4263 * isl_pw_multi_aff_identity_on_domain_space,
4264 * but is considered as a function on an isl_space when exported.
4266 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4267 __isl_take isl_space *space)
4269 return isl_pw_multi_aff_identity_on_domain_space(space);
4272 /* Exploit the equalities in "eq" to simplify the affine expressions.
4274 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4275 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4277 isl_size n;
4278 int i;
4280 n = isl_multi_aff_size(maff);
4281 if (n < 0 || !eq)
4282 goto error;
4284 for (i = 0; i < n; ++i) {
4285 isl_aff *aff;
4287 aff = isl_multi_aff_take_at(maff, i);
4288 aff = isl_aff_substitute_equalities(aff,
4289 isl_basic_set_copy(eq));
4290 maff = isl_multi_aff_restore_at(maff, i, aff);
4293 isl_basic_set_free(eq);
4294 return maff;
4295 error:
4296 isl_basic_set_free(eq);
4297 isl_multi_aff_free(maff);
4298 return NULL;
4301 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4302 isl_int f)
4304 isl_size n;
4305 int i;
4307 n = isl_multi_aff_size(maff);
4308 if (n < 0)
4309 return isl_multi_aff_free(maff);
4311 for (i = 0; i < n; ++i) {
4312 isl_aff *aff;
4314 aff = isl_multi_aff_take_at(maff, i);
4315 aff = isl_aff_scale(aff, f);
4316 maff = isl_multi_aff_restore_at(maff, i, aff);
4319 return maff;
4322 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4323 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4325 maff1 = isl_multi_aff_add(maff1, maff2);
4326 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4327 return maff1;
4330 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4332 if (!maff)
4333 return isl_bool_error;
4335 return isl_bool_false;
4338 /* Return the set of domain elements where "ma1" is lexicographically
4339 * smaller than or equal to "ma2".
4341 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4342 __isl_take isl_multi_aff *ma2)
4344 return isl_multi_aff_lex_ge_set(ma2, ma1);
4347 /* Return the set of domain elements where "ma1" is lexicographically
4348 * smaller than "ma2".
4350 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4351 __isl_take isl_multi_aff *ma2)
4353 return isl_multi_aff_lex_gt_set(ma2, ma1);
4356 /* Return the set of domain elements where "ma1" is lexicographically
4357 * greater than to "ma2". If "equal" is set, then include the domain
4358 * elements where they are equal.
4359 * Do this for the case where there are no entries.
4360 * In this case, "ma1" cannot be greater than "ma2",
4361 * but it is (greater than or) equal to "ma2".
4363 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4364 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4366 isl_space *space;
4368 space = isl_multi_aff_get_domain_space(ma1);
4370 isl_multi_aff_free(ma1);
4371 isl_multi_aff_free(ma2);
4373 if (equal)
4374 return isl_set_universe(space);
4375 else
4376 return isl_set_empty(space);
4379 /* Return the set where entry "i" of "ma1" and "ma2"
4380 * satisfy the relation prescribed by "cmp".
4382 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4383 __isl_keep isl_multi_aff *ma2, int i,
4384 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4385 __isl_take isl_aff *aff2))
4387 isl_aff *aff1, *aff2;
4389 aff1 = isl_multi_aff_get_at(ma1, i);
4390 aff2 = isl_multi_aff_get_at(ma2, i);
4391 return cmp(aff1, aff2);
4394 /* Return the set of domain elements where "ma1" is lexicographically
4395 * greater than to "ma2". If "equal" is set, then include the domain
4396 * elements where they are equal.
4398 * In particular, for all but the final entry,
4399 * include the set of elements where this entry is strictly greater in "ma1"
4400 * and all previous entries are equal.
4401 * The final entry is also allowed to be equal in the two functions
4402 * if "equal" is set.
4404 * The case where there are no entries is handled separately.
4406 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4407 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4409 int i;
4410 isl_size n;
4411 isl_space *space;
4412 isl_set *res;
4413 isl_set *equal_set;
4414 isl_set *gte;
4416 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4417 goto error;
4418 n = isl_multi_aff_size(ma1);
4419 if (n < 0)
4420 goto error;
4421 if (n == 0)
4422 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4424 space = isl_multi_aff_get_domain_space(ma1);
4425 res = isl_set_empty(isl_space_copy(space));
4426 equal_set = isl_set_universe(space);
4428 for (i = 0; i + 1 < n; ++i) {
4429 isl_bool empty;
4430 isl_set *gt, *eq;
4432 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4433 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4434 res = isl_set_union(res, gt);
4435 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4436 equal_set = isl_set_intersect(equal_set, eq);
4438 empty = isl_set_is_empty(equal_set);
4439 if (empty >= 0 && empty)
4440 break;
4443 if (equal)
4444 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4445 else
4446 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4447 isl_multi_aff_free(ma1);
4448 isl_multi_aff_free(ma2);
4450 gte = isl_set_intersect(gte, equal_set);
4451 return isl_set_union(res, gte);
4452 error:
4453 isl_multi_aff_free(ma1);
4454 isl_multi_aff_free(ma2);
4455 return NULL;
4458 /* Return the set of domain elements where "ma1" is lexicographically
4459 * greater than or equal to "ma2".
4461 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4462 __isl_take isl_multi_aff *ma2)
4464 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4467 /* Return the set of domain elements where "ma1" is lexicographically
4468 * greater than "ma2".
4470 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4471 __isl_take isl_multi_aff *ma2)
4473 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4476 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4478 #undef PW
4479 #define PW isl_pw_multi_aff
4480 #undef BASE
4481 #define BASE multi_aff
4482 #undef EL_IS_ZERO
4483 #define EL_IS_ZERO is_empty
4484 #undef ZERO
4485 #define ZERO empty
4486 #undef IS_ZERO
4487 #define IS_ZERO is_empty
4488 #undef FIELD
4489 #define FIELD maff
4490 #undef DEFAULT_IS_ZERO
4491 #define DEFAULT_IS_ZERO 0
4493 #include <isl_pw_templ.c>
4494 #include <isl_pw_un_op_templ.c>
4495 #include <isl_pw_add_constant_multi_val_templ.c>
4496 #include <isl_pw_add_constant_val_templ.c>
4497 #include <isl_pw_bind_domain_templ.c>
4498 #include <isl_pw_insert_dims_templ.c>
4499 #include <isl_pw_insert_domain_templ.c>
4500 #include <isl_pw_locals_templ.c>
4501 #include <isl_pw_move_dims_templ.c>
4502 #include <isl_pw_neg_templ.c>
4503 #include <isl_pw_pullback_templ.c>
4504 #include <isl_pw_range_tuple_id_templ.c>
4505 #include <isl_pw_union_opt.c>
4507 #undef BASE
4508 #define BASE pw_multi_aff
4510 #include <isl_union_multi.c>
4511 #include "isl_union_locals_templ.c"
4512 #include <isl_union_neg.c>
4514 #undef BASE
4515 #define BASE multi_aff
4517 #include <isl_union_pw_templ.c>
4519 /* Generic function for extracting a factor from a product "pma".
4520 * "check_space" checks that the space is that of the right kind of product.
4521 * "space_factor" extracts the factor from the space.
4522 * "multi_aff_factor" extracts the factor from the constituent functions.
4524 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4525 __isl_take isl_pw_multi_aff *pma,
4526 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4527 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4528 __isl_give isl_multi_aff *(*multi_aff_factor)(
4529 __isl_take isl_multi_aff *ma))
4531 int i;
4532 isl_space *space;
4534 if (check_space(pma) < 0)
4535 return isl_pw_multi_aff_free(pma);
4537 space = isl_pw_multi_aff_take_space(pma);
4538 space = space_factor(space);
4540 for (i = 0; pma && i < pma->n; ++i) {
4541 isl_multi_aff *ma;
4543 ma = isl_pw_multi_aff_take_base_at(pma, i);
4544 ma = multi_aff_factor(ma);
4545 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4548 pma = isl_pw_multi_aff_restore_space(pma, space);
4550 return pma;
4553 /* Is the range of "pma" a wrapped relation?
4555 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4556 __isl_keep isl_pw_multi_aff *pma)
4558 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4561 /* Check that the range of "pma" is a product.
4563 static isl_stat pw_multi_aff_check_range_product(
4564 __isl_keep isl_pw_multi_aff *pma)
4566 isl_bool wraps;
4568 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4569 if (wraps < 0)
4570 return isl_stat_error;
4571 if (!wraps)
4572 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4573 "range is not a product", return isl_stat_error);
4574 return isl_stat_ok;
4577 /* Given a function A -> [B -> C], extract the function A -> B.
4579 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4580 __isl_take isl_pw_multi_aff *pma)
4582 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4583 &isl_space_range_factor_domain,
4584 &isl_multi_aff_range_factor_domain);
4587 /* Given a function A -> [B -> C], extract the function A -> C.
4589 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4590 __isl_take isl_pw_multi_aff *pma)
4592 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4593 &isl_space_range_factor_range,
4594 &isl_multi_aff_range_factor_range);
4597 /* Given two piecewise multi affine expressions, return a piecewise
4598 * multi-affine expression defined on the union of the definition domains
4599 * of the inputs that is equal to the lexicographic maximum of the two
4600 * inputs on each cell. If only one of the two inputs is defined on
4601 * a given cell, then it is considered to be the maximum.
4603 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4604 __isl_take isl_pw_multi_aff *pma1,
4605 __isl_take isl_pw_multi_aff *pma2)
4607 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4608 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4609 &isl_multi_aff_lex_ge_set);
4612 /* Given two piecewise multi affine expressions, return a piecewise
4613 * multi-affine expression defined on the union of the definition domains
4614 * of the inputs that is equal to the lexicographic minimum of the two
4615 * inputs on each cell. If only one of the two inputs is defined on
4616 * a given cell, then it is considered to be the minimum.
4618 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4619 __isl_take isl_pw_multi_aff *pma1,
4620 __isl_take isl_pw_multi_aff *pma2)
4622 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4623 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4624 &isl_multi_aff_lex_le_set);
4627 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4628 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4630 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4631 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4632 &isl_multi_aff_add);
4635 /* Subtract "pma2" from "pma1" and return the result.
4637 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4638 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4640 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4641 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4642 &isl_multi_aff_sub);
4645 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4646 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4648 return isl_pw_multi_aff_union_add_(pma1, pma2);
4651 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4652 * with the actual sum on the shared domain and
4653 * the defined expression on the symmetric difference of the domains.
4655 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4656 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4658 return isl_union_pw_aff_union_add_(upa1, upa2);
4661 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4662 * with the actual sum on the shared domain and
4663 * the defined expression on the symmetric difference of the domains.
4665 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4666 __isl_take isl_union_pw_multi_aff *upma1,
4667 __isl_take isl_union_pw_multi_aff *upma2)
4669 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4672 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4673 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4675 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4676 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4678 int i, j, n;
4679 isl_space *space;
4680 isl_pw_multi_aff *res;
4682 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4683 goto error;
4685 n = pma1->n * pma2->n;
4686 space = isl_space_product(isl_space_copy(pma1->dim),
4687 isl_space_copy(pma2->dim));
4688 res = isl_pw_multi_aff_alloc_size(space, n);
4690 for (i = 0; i < pma1->n; ++i) {
4691 for (j = 0; j < pma2->n; ++j) {
4692 isl_set *domain;
4693 isl_multi_aff *ma;
4695 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4696 isl_set_copy(pma2->p[j].set));
4697 ma = isl_multi_aff_product(
4698 isl_multi_aff_copy(pma1->p[i].maff),
4699 isl_multi_aff_copy(pma2->p[j].maff));
4700 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4704 isl_pw_multi_aff_free(pma1);
4705 isl_pw_multi_aff_free(pma2);
4706 return res;
4707 error:
4708 isl_pw_multi_aff_free(pma1);
4709 isl_pw_multi_aff_free(pma2);
4710 return NULL;
4713 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4714 * denominator "denom".
4715 * "denom" is allowed to be negative, in which case the actual denominator
4716 * is -denom and the expressions are added instead.
4718 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4719 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4721 int i, first;
4722 int sign;
4723 isl_int d;
4725 first = isl_seq_first_non_zero(c, n);
4726 if (first == -1)
4727 return aff;
4729 sign = isl_int_sgn(denom);
4730 isl_int_init(d);
4731 isl_int_abs(d, denom);
4732 for (i = first; i < n; ++i) {
4733 isl_aff *aff_i;
4735 if (isl_int_is_zero(c[i]))
4736 continue;
4737 aff_i = isl_multi_aff_get_aff(ma, i);
4738 aff_i = isl_aff_scale(aff_i, c[i]);
4739 aff_i = isl_aff_scale_down(aff_i, d);
4740 if (sign >= 0)
4741 aff = isl_aff_sub(aff, aff_i);
4742 else
4743 aff = isl_aff_add(aff, aff_i);
4745 isl_int_clear(d);
4747 return aff;
4750 /* Extract an affine expression that expresses the output dimension "pos"
4751 * of "bmap" in terms of the parameters and input dimensions from
4752 * equality "eq".
4753 * Note that this expression may involve integer divisions defined
4754 * in terms of parameters and input dimensions.
4755 * The equality may also involve references to earlier (but not later)
4756 * output dimensions. These are replaced by the corresponding elements
4757 * in "ma".
4759 * If the equality is of the form
4761 * f(i) + h(j) + a x + g(i) = 0,
4763 * with f(i) a linear combinations of the parameters and input dimensions,
4764 * g(i) a linear combination of integer divisions defined in terms of the same
4765 * and h(j) a linear combinations of earlier output dimensions,
4766 * then the affine expression is
4768 * (-f(i) - g(i))/a - h(j)/a
4770 * If the equality is of the form
4772 * f(i) + h(j) - a x + g(i) = 0,
4774 * then the affine expression is
4776 * (f(i) + g(i))/a - h(j)/(-a)
4779 * If "div" refers to an integer division (i.e., it is smaller than
4780 * the number of integer divisions), then the equality constraint
4781 * does involve an integer division (the one at position "div") that
4782 * is defined in terms of output dimensions. However, this integer
4783 * division can be eliminated by exploiting a pair of constraints
4784 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4785 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4786 * -l + x >= 0.
4787 * In particular, let
4789 * x = e(i) + m floor(...)
4791 * with e(i) the expression derived above and floor(...) the integer
4792 * division involving output dimensions.
4793 * From
4795 * l <= x <= l + n,
4797 * we have
4799 * 0 <= x - l <= n
4801 * This means
4803 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4804 * = (e(i) - l) mod m
4806 * Therefore,
4808 * x - l = (e(i) - l) mod m
4810 * or
4812 * x = ((e(i) - l) mod m) + l
4814 * The variable "shift" below contains the expression -l, which may
4815 * also involve a linear combination of earlier output dimensions.
4817 static __isl_give isl_aff *extract_aff_from_equality(
4818 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4819 __isl_keep isl_multi_aff *ma)
4821 unsigned o_out;
4822 isl_size n_div, n_out;
4823 isl_ctx *ctx;
4824 isl_local_space *ls;
4825 isl_aff *aff, *shift;
4826 isl_val *mod;
4828 ctx = isl_basic_map_get_ctx(bmap);
4829 ls = isl_basic_map_get_local_space(bmap);
4830 ls = isl_local_space_domain(ls);
4831 aff = isl_aff_alloc(isl_local_space_copy(ls));
4832 if (!aff)
4833 goto error;
4834 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4835 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4836 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4837 if (n_out < 0 || n_div < 0)
4838 goto error;
4839 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4840 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4841 isl_seq_cpy(aff->v->el + 1 + o_out,
4842 bmap->eq[eq] + o_out + n_out, n_div);
4843 } else {
4844 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4845 isl_seq_neg(aff->v->el + 1 + o_out,
4846 bmap->eq[eq] + o_out + n_out, n_div);
4848 if (div < n_div)
4849 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4850 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4851 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4852 bmap->eq[eq][o_out + pos]);
4853 if (div < n_div) {
4854 shift = isl_aff_alloc(isl_local_space_copy(ls));
4855 if (!shift)
4856 goto error;
4857 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4858 isl_seq_cpy(shift->v->el + 1 + o_out,
4859 bmap->ineq[ineq] + o_out + n_out, n_div);
4860 isl_int_set_si(shift->v->el[0], 1);
4861 shift = subtract_initial(shift, ma, pos,
4862 bmap->ineq[ineq] + o_out, ctx->negone);
4863 aff = isl_aff_add(aff, isl_aff_copy(shift));
4864 mod = isl_val_int_from_isl_int(ctx,
4865 bmap->eq[eq][o_out + n_out + div]);
4866 mod = isl_val_abs(mod);
4867 aff = isl_aff_mod_val(aff, mod);
4868 aff = isl_aff_sub(aff, shift);
4871 isl_local_space_free(ls);
4872 return aff;
4873 error:
4874 isl_local_space_free(ls);
4875 isl_aff_free(aff);
4876 return NULL;
4879 /* Given a basic map with output dimensions defined
4880 * in terms of the parameters input dimensions and earlier
4881 * output dimensions using an equality (and possibly a pair on inequalities),
4882 * extract an isl_aff that expresses output dimension "pos" in terms
4883 * of the parameters and input dimensions.
4884 * Note that this expression may involve integer divisions defined
4885 * in terms of parameters and input dimensions.
4886 * "ma" contains the expressions corresponding to earlier output dimensions.
4888 * This function shares some similarities with
4889 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4891 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4892 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4894 int eq, div, ineq;
4895 isl_aff *aff;
4897 if (!bmap)
4898 return NULL;
4899 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4900 if (eq >= bmap->n_eq)
4901 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4902 "unable to find suitable equality", return NULL);
4903 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4905 aff = isl_aff_remove_unused_divs(aff);
4906 return aff;
4909 /* Given a basic map where each output dimension is defined
4910 * in terms of the parameters and input dimensions using an equality,
4911 * extract an isl_multi_aff that expresses the output dimensions in terms
4912 * of the parameters and input dimensions.
4914 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4915 __isl_take isl_basic_map *bmap)
4917 int i;
4918 isl_size n_out;
4919 isl_multi_aff *ma;
4921 if (!bmap)
4922 return NULL;
4924 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4925 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4926 if (n_out < 0)
4927 ma = isl_multi_aff_free(ma);
4929 for (i = 0; i < n_out; ++i) {
4930 isl_aff *aff;
4932 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4933 ma = isl_multi_aff_set_aff(ma, i, aff);
4936 isl_basic_map_free(bmap);
4938 return ma;
4941 /* Given a basic set where each set dimension is defined
4942 * in terms of the parameters using an equality,
4943 * extract an isl_multi_aff that expresses the set dimensions in terms
4944 * of the parameters.
4946 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4947 __isl_take isl_basic_set *bset)
4949 return extract_isl_multi_aff_from_basic_map(bset);
4952 /* Create an isl_pw_multi_aff that is equivalent to
4953 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4954 * The given basic map is such that each output dimension is defined
4955 * in terms of the parameters and input dimensions using an equality.
4957 * Since some applications expect the result of isl_pw_multi_aff_from_map
4958 * to only contain integer affine expressions, we compute the floor
4959 * of the expression before returning.
4961 * Remove all constraints involving local variables without
4962 * an explicit representation (resulting in the removal of those
4963 * local variables) prior to the actual extraction to ensure
4964 * that the local spaces in which the resulting affine expressions
4965 * are created do not contain any unknown local variables.
4966 * Removing such constraints is safe because constraints involving
4967 * unknown local variables are not used to determine whether
4968 * a basic map is obviously single-valued.
4970 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4971 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4973 isl_multi_aff *ma;
4975 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4976 ma = extract_isl_multi_aff_from_basic_map(bmap);
4977 ma = isl_multi_aff_floor(ma);
4978 return isl_pw_multi_aff_alloc(domain, ma);
4981 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4982 * This obviously only works if the input "map" is single-valued.
4983 * If so, we compute the lexicographic minimum of the image in the form
4984 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4985 * to its lexicographic minimum.
4986 * If the input is not single-valued, we produce an error.
4988 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4989 __isl_take isl_map *map)
4991 int i;
4992 int sv;
4993 isl_pw_multi_aff *pma;
4995 sv = isl_map_is_single_valued(map);
4996 if (sv < 0)
4997 goto error;
4998 if (!sv)
4999 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5000 "map is not single-valued", goto error);
5001 map = isl_map_make_disjoint(map);
5002 if (!map)
5003 return NULL;
5005 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5007 for (i = 0; i < map->n; ++i) {
5008 isl_pw_multi_aff *pma_i;
5009 isl_basic_map *bmap;
5010 bmap = isl_basic_map_copy(map->p[i]);
5011 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5012 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5015 isl_map_free(map);
5016 return pma;
5017 error:
5018 isl_map_free(map);
5019 return NULL;
5022 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5023 * taking into account that the output dimension at position "d"
5024 * can be represented as
5026 * x = floor((e(...) + c1) / m)
5028 * given that constraint "i" is of the form
5030 * e(...) + c1 - m x >= 0
5033 * Let "map" be of the form
5035 * A -> B
5037 * We construct a mapping
5039 * A -> [A -> x = floor(...)]
5041 * apply that to the map, obtaining
5043 * [A -> x = floor(...)] -> B
5045 * and equate dimension "d" to x.
5046 * We then compute a isl_pw_multi_aff representation of the resulting map
5047 * and plug in the mapping above.
5049 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5050 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5052 isl_ctx *ctx;
5053 isl_space *space = NULL;
5054 isl_local_space *ls;
5055 isl_multi_aff *ma;
5056 isl_aff *aff;
5057 isl_vec *v;
5058 isl_map *insert;
5059 int offset;
5060 isl_size n;
5061 isl_size n_in;
5062 isl_pw_multi_aff *pma;
5063 isl_bool is_set;
5065 is_set = isl_map_is_set(map);
5066 if (is_set < 0)
5067 goto error;
5069 offset = isl_basic_map_offset(hull, isl_dim_out);
5070 ctx = isl_map_get_ctx(map);
5071 space = isl_space_domain(isl_map_get_space(map));
5072 n_in = isl_space_dim(space, isl_dim_set);
5073 n = isl_space_dim(space, isl_dim_all);
5074 if (n_in < 0 || n < 0)
5075 goto error;
5077 v = isl_vec_alloc(ctx, 1 + 1 + n);
5078 if (v) {
5079 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5080 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5082 isl_basic_map_free(hull);
5084 ls = isl_local_space_from_space(isl_space_copy(space));
5085 aff = isl_aff_alloc_vec(ls, v);
5086 aff = isl_aff_floor(aff);
5087 if (is_set) {
5088 isl_space_free(space);
5089 ma = isl_multi_aff_from_aff(aff);
5090 } else {
5091 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5092 ma = isl_multi_aff_range_product(ma,
5093 isl_multi_aff_from_aff(aff));
5096 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5097 map = isl_map_apply_domain(map, insert);
5098 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5099 pma = isl_pw_multi_aff_from_map(map);
5100 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5102 return pma;
5103 error:
5104 isl_space_free(space);
5105 isl_map_free(map);
5106 isl_basic_map_free(hull);
5107 return NULL;
5110 /* Is constraint "c" of the form
5112 * e(...) + c1 - m x >= 0
5114 * or
5116 * -e(...) + c2 + m x >= 0
5118 * where m > 1 and e only depends on parameters and input dimensions?
5120 * "offset" is the offset of the output dimensions
5121 * "pos" is the position of output dimension x.
5123 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5125 if (isl_int_is_zero(c[offset + d]))
5126 return 0;
5127 if (isl_int_is_one(c[offset + d]))
5128 return 0;
5129 if (isl_int_is_negone(c[offset + d]))
5130 return 0;
5131 if (isl_seq_first_non_zero(c + offset, d) != -1)
5132 return 0;
5133 if (isl_seq_first_non_zero(c + offset + d + 1,
5134 total - (offset + d + 1)) != -1)
5135 return 0;
5136 return 1;
5139 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5141 * As a special case, we first check if there is any pair of constraints,
5142 * shared by all the basic maps in "map" that force a given dimension
5143 * to be equal to the floor of some affine combination of the input dimensions.
5145 * In particular, if we can find two constraints
5147 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5149 * and
5151 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5153 * where m > 1 and e only depends on parameters and input dimensions,
5154 * and such that
5156 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5158 * then we know that we can take
5160 * x = floor((e(...) + c1) / m)
5162 * without having to perform any computation.
5164 * Note that we know that
5166 * c1 + c2 >= 1
5168 * If c1 + c2 were 0, then we would have detected an equality during
5169 * simplification. If c1 + c2 were negative, then we would have detected
5170 * a contradiction.
5172 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5173 __isl_take isl_map *map)
5175 int d;
5176 isl_size dim;
5177 int i, j, n;
5178 int offset;
5179 isl_size total;
5180 isl_int sum;
5181 isl_basic_map *hull;
5183 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5184 dim = isl_map_dim(map, isl_dim_out);
5185 total = isl_basic_map_dim(hull, isl_dim_all);
5186 if (dim < 0 || total < 0)
5187 goto error;
5189 isl_int_init(sum);
5190 offset = isl_basic_map_offset(hull, isl_dim_out);
5191 n = hull->n_ineq;
5192 for (d = 0; d < dim; ++d) {
5193 for (i = 0; i < n; ++i) {
5194 if (!is_potential_div_constraint(hull->ineq[i],
5195 offset, d, 1 + total))
5196 continue;
5197 for (j = i + 1; j < n; ++j) {
5198 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5199 hull->ineq[j] + 1, total))
5200 continue;
5201 isl_int_add(sum, hull->ineq[i][0],
5202 hull->ineq[j][0]);
5203 if (isl_int_abs_lt(sum,
5204 hull->ineq[i][offset + d]))
5205 break;
5208 if (j >= n)
5209 continue;
5210 isl_int_clear(sum);
5211 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5212 j = i;
5213 return pw_multi_aff_from_map_div(map, hull, d, j);
5216 isl_int_clear(sum);
5217 isl_basic_map_free(hull);
5218 return pw_multi_aff_from_map_base(map);
5219 error:
5220 isl_map_free(map);
5221 isl_basic_map_free(hull);
5222 return NULL;
5225 /* Given an affine expression
5227 * [A -> B] -> f(A,B)
5229 * construct an isl_multi_aff
5231 * [A -> B] -> B'
5233 * such that dimension "d" in B' is set to "aff" and the remaining
5234 * dimensions are set equal to the corresponding dimensions in B.
5235 * "n_in" is the dimension of the space A.
5236 * "n_out" is the dimension of the space B.
5238 * If "is_set" is set, then the affine expression is of the form
5240 * [B] -> f(B)
5242 * and we construct an isl_multi_aff
5244 * B -> B'
5246 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5247 unsigned n_in, unsigned n_out, int is_set)
5249 int i;
5250 isl_multi_aff *ma;
5251 isl_space *space, *space2;
5252 isl_local_space *ls;
5254 space = isl_aff_get_domain_space(aff);
5255 ls = isl_local_space_from_space(isl_space_copy(space));
5256 space2 = isl_space_copy(space);
5257 if (!is_set)
5258 space2 = isl_space_range(isl_space_unwrap(space2));
5259 space = isl_space_map_from_domain_and_range(space, space2);
5260 ma = isl_multi_aff_alloc(space);
5261 ma = isl_multi_aff_set_aff(ma, d, aff);
5263 for (i = 0; i < n_out; ++i) {
5264 if (i == d)
5265 continue;
5266 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5267 isl_dim_set, n_in + i);
5268 ma = isl_multi_aff_set_aff(ma, i, aff);
5271 isl_local_space_free(ls);
5273 return ma;
5276 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5277 * taking into account that the dimension at position "d" can be written as
5279 * x = m a + f(..) (1)
5281 * where m is equal to "gcd".
5282 * "i" is the index of the equality in "hull" that defines f(..).
5283 * In particular, the equality is of the form
5285 * f(..) - x + m g(existentials) = 0
5287 * or
5289 * -f(..) + x + m g(existentials) = 0
5291 * We basically plug (1) into "map", resulting in a map with "a"
5292 * in the range instead of "x". The corresponding isl_pw_multi_aff
5293 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5295 * Specifically, given the input map
5297 * A -> B
5299 * We first wrap it into a set
5301 * [A -> B]
5303 * and define (1) on top of the corresponding space, resulting in "aff".
5304 * We use this to create an isl_multi_aff that maps the output position "d"
5305 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5306 * We plug this into the wrapped map, unwrap the result and compute the
5307 * corresponding isl_pw_multi_aff.
5308 * The result is an expression
5310 * A -> T(A)
5312 * We adjust that to
5314 * A -> [A -> T(A)]
5316 * so that we can plug that into "aff", after extending the latter to
5317 * a mapping
5319 * [A -> B] -> B'
5322 * If "map" is actually a set, then there is no "A" space, meaning
5323 * that we do not need to perform any wrapping, and that the result
5324 * of the recursive call is of the form
5326 * [T]
5328 * which is plugged into a mapping of the form
5330 * B -> B'
5332 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5333 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5334 isl_int gcd)
5336 isl_set *set;
5337 isl_space *space;
5338 isl_local_space *ls;
5339 isl_aff *aff;
5340 isl_multi_aff *ma;
5341 isl_pw_multi_aff *pma, *id;
5342 isl_size n_in;
5343 unsigned o_out;
5344 isl_size n_out;
5345 isl_bool is_set;
5347 is_set = isl_map_is_set(map);
5348 if (is_set < 0)
5349 goto error;
5351 n_in = isl_basic_map_dim(hull, isl_dim_in);
5352 n_out = isl_basic_map_dim(hull, isl_dim_out);
5353 if (n_in < 0 || n_out < 0)
5354 goto error;
5355 o_out = isl_basic_map_offset(hull, isl_dim_out);
5357 if (is_set)
5358 set = map;
5359 else
5360 set = isl_map_wrap(map);
5361 space = isl_space_map_from_set(isl_set_get_space(set));
5362 ma = isl_multi_aff_identity(space);
5363 ls = isl_local_space_from_space(isl_set_get_space(set));
5364 aff = isl_aff_alloc(ls);
5365 if (aff) {
5366 isl_int_set_si(aff->v->el[0], 1);
5367 if (isl_int_is_one(hull->eq[i][o_out + d]))
5368 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5369 aff->v->size - 1);
5370 else
5371 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5372 aff->v->size - 1);
5373 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5375 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5376 set = isl_set_preimage_multi_aff(set, ma);
5378 ma = range_map(aff, d, n_in, n_out, is_set);
5380 if (is_set)
5381 map = set;
5382 else
5383 map = isl_set_unwrap(set);
5384 pma = isl_pw_multi_aff_from_map(map);
5386 if (!is_set) {
5387 space = isl_pw_multi_aff_get_domain_space(pma);
5388 space = isl_space_map_from_set(space);
5389 id = isl_pw_multi_aff_identity(space);
5390 pma = isl_pw_multi_aff_range_product(id, pma);
5392 id = isl_pw_multi_aff_from_multi_aff(ma);
5393 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5395 isl_basic_map_free(hull);
5396 return pma;
5397 error:
5398 isl_map_free(map);
5399 isl_basic_map_free(hull);
5400 return NULL;
5403 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5404 * "hull" contains the equalities valid for "map".
5406 * Check if any of the output dimensions is "strided".
5407 * That is, we check if it can be written as
5409 * x = m a + f(..)
5411 * with m greater than 1, a some combination of existentially quantified
5412 * variables and f an expression in the parameters and input dimensions.
5413 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5415 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5416 * special case.
5418 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5419 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5421 int i, j;
5422 isl_size n_out;
5423 unsigned o_out;
5424 isl_size n_div;
5425 unsigned o_div;
5426 isl_int gcd;
5428 n_div = isl_basic_map_dim(hull, isl_dim_div);
5429 n_out = isl_basic_map_dim(hull, isl_dim_out);
5430 if (n_div < 0 || n_out < 0)
5431 goto error;
5433 if (n_div == 0) {
5434 isl_basic_map_free(hull);
5435 return pw_multi_aff_from_map_check_div(map);
5438 isl_int_init(gcd);
5440 o_div = isl_basic_map_offset(hull, isl_dim_div);
5441 o_out = isl_basic_map_offset(hull, isl_dim_out);
5443 for (i = 0; i < n_out; ++i) {
5444 for (j = 0; j < hull->n_eq; ++j) {
5445 isl_int *eq = hull->eq[j];
5446 isl_pw_multi_aff *res;
5448 if (!isl_int_is_one(eq[o_out + i]) &&
5449 !isl_int_is_negone(eq[o_out + i]))
5450 continue;
5451 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5452 continue;
5453 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5454 n_out - (i + 1)) != -1)
5455 continue;
5456 isl_seq_gcd(eq + o_div, n_div, &gcd);
5457 if (isl_int_is_zero(gcd))
5458 continue;
5459 if (isl_int_is_one(gcd))
5460 continue;
5462 res = pw_multi_aff_from_map_stride(map, hull,
5463 i, j, gcd);
5464 isl_int_clear(gcd);
5465 return res;
5469 isl_int_clear(gcd);
5470 isl_basic_map_free(hull);
5471 return pw_multi_aff_from_map_check_div(map);
5472 error:
5473 isl_map_free(map);
5474 isl_basic_map_free(hull);
5475 return NULL;
5478 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5480 * As a special case, we first check if all output dimensions are uniquely
5481 * defined in terms of the parameters and input dimensions over the entire
5482 * domain. If so, we extract the desired isl_pw_multi_aff directly
5483 * from the affine hull of "map" and its domain.
5485 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5486 * special cases.
5488 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5490 isl_bool sv;
5491 isl_size n;
5492 isl_basic_map *hull;
5494 n = isl_map_n_basic_map(map);
5495 if (n < 0)
5496 goto error;
5498 if (n == 1) {
5499 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5500 hull = isl_basic_map_plain_affine_hull(hull);
5501 sv = isl_basic_map_plain_is_single_valued(hull);
5502 if (sv >= 0 && sv)
5503 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5504 hull);
5505 isl_basic_map_free(hull);
5507 map = isl_map_detect_equalities(map);
5508 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5509 sv = isl_basic_map_plain_is_single_valued(hull);
5510 if (sv >= 0 && sv)
5511 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5512 if (sv >= 0)
5513 return pw_multi_aff_from_map_check_strides(map, hull);
5514 isl_basic_map_free(hull);
5515 error:
5516 isl_map_free(map);
5517 return NULL;
5520 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5521 * but is considered as a function on an isl_map when exported.
5523 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5525 return isl_pw_multi_aff_from_map(map);
5528 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5530 return isl_pw_multi_aff_from_map(set);
5533 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5534 * but is considered as a function on an isl_set when exported.
5536 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5538 return isl_pw_multi_aff_from_set(set);
5541 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5542 * add it to *user.
5544 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5546 isl_union_pw_multi_aff **upma = user;
5547 isl_pw_multi_aff *pma;
5549 pma = isl_pw_multi_aff_from_map(map);
5550 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5552 return *upma ? isl_stat_ok : isl_stat_error;
5555 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5556 * domain.
5558 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5559 __isl_take isl_aff *aff)
5561 isl_multi_aff *ma;
5562 isl_pw_multi_aff *pma;
5564 ma = isl_multi_aff_from_aff(aff);
5565 pma = isl_pw_multi_aff_from_multi_aff(ma);
5566 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5569 /* Try and create an isl_union_pw_multi_aff that is equivalent
5570 * to the given isl_union_map.
5571 * The isl_union_map is required to be single-valued in each space.
5572 * Otherwise, an error is produced.
5574 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5575 __isl_take isl_union_map *umap)
5577 isl_space *space;
5578 isl_union_pw_multi_aff *upma;
5580 space = isl_union_map_get_space(umap);
5581 upma = isl_union_pw_multi_aff_empty(space);
5582 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5583 upma = isl_union_pw_multi_aff_free(upma);
5584 isl_union_map_free(umap);
5586 return upma;
5589 /* This function performs the same operation as
5590 * isl_union_pw_multi_aff_from_union_map,
5591 * but is considered as a function on an isl_union_map when exported.
5593 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5594 __isl_take isl_union_map *umap)
5596 return isl_union_pw_multi_aff_from_union_map(umap);
5599 /* Try and create an isl_union_pw_multi_aff that is equivalent
5600 * to the given isl_union_set.
5601 * The isl_union_set is required to be a singleton in each space.
5602 * Otherwise, an error is produced.
5604 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5605 __isl_take isl_union_set *uset)
5607 return isl_union_pw_multi_aff_from_union_map(uset);
5610 /* Return the piecewise affine expression "set ? 1 : 0".
5612 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5614 isl_pw_aff *pa;
5615 isl_space *space = isl_set_get_space(set);
5616 isl_local_space *ls = isl_local_space_from_space(space);
5617 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5618 isl_aff *one = isl_aff_zero_on_domain(ls);
5620 one = isl_aff_add_constant_si(one, 1);
5621 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5622 set = isl_set_complement(set);
5623 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5625 return pa;
5628 /* Plug in "subs" for dimension "type", "pos" of "aff".
5630 * Let i be the dimension to replace and let "subs" be of the form
5632 * f/d
5634 * and "aff" of the form
5636 * (a i + g)/m
5638 * The result is
5640 * (a f + d g')/(m d)
5642 * where g' is the result of plugging in "subs" in each of the integer
5643 * divisions in g.
5645 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5646 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5648 isl_ctx *ctx;
5649 isl_int v;
5650 isl_size n_div;
5652 aff = isl_aff_cow(aff);
5653 if (!aff || !subs)
5654 return isl_aff_free(aff);
5656 ctx = isl_aff_get_ctx(aff);
5657 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5658 isl_die(ctx, isl_error_invalid,
5659 "spaces don't match", return isl_aff_free(aff));
5660 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5661 if (n_div < 0)
5662 return isl_aff_free(aff);
5663 if (n_div != 0)
5664 isl_die(ctx, isl_error_unsupported,
5665 "cannot handle divs yet", return isl_aff_free(aff));
5667 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5668 if (!aff->ls)
5669 return isl_aff_free(aff);
5671 aff->v = isl_vec_cow(aff->v);
5672 if (!aff->v)
5673 return isl_aff_free(aff);
5675 pos += isl_local_space_offset(aff->ls, type);
5677 isl_int_init(v);
5678 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5679 aff->v->size, subs->v->size, v);
5680 isl_int_clear(v);
5682 return aff;
5685 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5686 * expressions in "maff".
5688 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5689 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5690 __isl_keep isl_aff *subs)
5692 isl_size n;
5693 int i;
5695 n = isl_multi_aff_size(maff);
5696 if (n < 0 || !subs)
5697 return isl_multi_aff_free(maff);
5699 if (type == isl_dim_in)
5700 type = isl_dim_set;
5702 for (i = 0; i < n; ++i) {
5703 isl_aff *aff;
5705 aff = isl_multi_aff_take_at(maff, i);
5706 aff = isl_aff_substitute(aff, type, pos, subs);
5707 maff = isl_multi_aff_restore_at(maff, i, aff);
5710 return maff;
5713 /* Plug in "subs" for input dimension "pos" of "pma".
5715 * pma is of the form
5717 * A_i(v) -> M_i(v)
5719 * while subs is of the form
5721 * v' = B_j(v) -> S_j
5723 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5724 * has a contribution in the result, in particular
5726 * C_ij(S_j) -> M_i(S_j)
5728 * Note that plugging in S_j in C_ij may also result in an empty set
5729 * and this contribution should simply be discarded.
5731 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5732 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5733 __isl_keep isl_pw_aff *subs)
5735 int i, j, n;
5736 isl_pw_multi_aff *res;
5738 if (!pma || !subs)
5739 return isl_pw_multi_aff_free(pma);
5741 n = pma->n * subs->n;
5742 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5744 for (i = 0; i < pma->n; ++i) {
5745 for (j = 0; j < subs->n; ++j) {
5746 isl_set *common;
5747 isl_multi_aff *res_ij;
5748 int empty;
5750 common = isl_set_intersect(
5751 isl_set_copy(pma->p[i].set),
5752 isl_set_copy(subs->p[j].set));
5753 common = isl_set_substitute(common,
5754 pos, subs->p[j].aff);
5755 empty = isl_set_plain_is_empty(common);
5756 if (empty < 0 || empty) {
5757 isl_set_free(common);
5758 if (empty < 0)
5759 goto error;
5760 continue;
5763 res_ij = isl_multi_aff_substitute(
5764 isl_multi_aff_copy(pma->p[i].maff),
5765 isl_dim_in, pos, subs->p[j].aff);
5767 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5771 isl_pw_multi_aff_free(pma);
5772 return res;
5773 error:
5774 isl_pw_multi_aff_free(pma);
5775 isl_pw_multi_aff_free(res);
5776 return NULL;
5779 /* Compute the preimage of a range of dimensions in the affine expression "src"
5780 * under "ma" and put the result in "dst". The number of dimensions in "src"
5781 * that precede the range is given by "n_before". The number of dimensions
5782 * in the range is given by the number of output dimensions of "ma".
5783 * The number of dimensions that follow the range is given by "n_after".
5784 * If "has_denom" is set (to one),
5785 * then "src" and "dst" have an extra initial denominator.
5786 * "n_div_ma" is the number of existentials in "ma"
5787 * "n_div_bset" is the number of existentials in "src"
5788 * The resulting "dst" (which is assumed to have been allocated by
5789 * the caller) contains coefficients for both sets of existentials,
5790 * first those in "ma" and then those in "src".
5791 * f, c1, c2 and g are temporary objects that have been initialized
5792 * by the caller.
5794 * Let src represent the expression
5796 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5798 * and let ma represent the expressions
5800 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5802 * We start out with the following expression for dst:
5804 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5806 * with the multiplication factor f initially equal to 1
5807 * and f \sum_i b_i v_i kept separately.
5808 * For each x_i that we substitute, we multiply the numerator
5809 * (and denominator) of dst by c_1 = m_i and add the numerator
5810 * of the x_i expression multiplied by c_2 = f b_i,
5811 * after removing the common factors of c_1 and c_2.
5812 * The multiplication factor f also needs to be multiplied by c_1
5813 * for the next x_j, j > i.
5815 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5816 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5817 int n_div_ma, int n_div_bmap,
5818 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5820 int i;
5821 isl_size n_param, n_in, n_out;
5822 int o_dst, o_src;
5824 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5825 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5826 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5827 if (n_param < 0 || n_in < 0 || n_out < 0)
5828 return isl_stat_error;
5830 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5831 o_dst = o_src = has_denom + 1 + n_param + n_before;
5832 isl_seq_clr(dst + o_dst, n_in);
5833 o_dst += n_in;
5834 o_src += n_out;
5835 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5836 o_dst += n_after;
5837 o_src += n_after;
5838 isl_seq_clr(dst + o_dst, n_div_ma);
5839 o_dst += n_div_ma;
5840 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5842 isl_int_set_si(f, 1);
5844 for (i = 0; i < n_out; ++i) {
5845 int offset = has_denom + 1 + n_param + n_before + i;
5847 if (isl_int_is_zero(src[offset]))
5848 continue;
5849 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5850 isl_int_mul(c2, f, src[offset]);
5851 isl_int_gcd(g, c1, c2);
5852 isl_int_divexact(c1, c1, g);
5853 isl_int_divexact(c2, c2, g);
5855 isl_int_mul(f, f, c1);
5856 o_dst = has_denom;
5857 o_src = 1;
5858 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5859 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5860 o_dst += 1 + n_param;
5861 o_src += 1 + n_param;
5862 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5863 o_dst += n_before;
5864 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5865 c2, ma->u.p[i]->v->el + o_src, n_in);
5866 o_dst += n_in;
5867 o_src += n_in;
5868 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5869 o_dst += n_after;
5870 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5871 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5872 o_dst += n_div_ma;
5873 o_src += n_div_ma;
5874 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5875 if (has_denom)
5876 isl_int_mul(dst[0], dst[0], c1);
5879 return isl_stat_ok;
5882 /* Compute the pullback of "aff" by the function represented by "ma".
5883 * In other words, plug in "ma" in "aff". The result is an affine expression
5884 * defined over the domain space of "ma".
5886 * If "aff" is represented by
5888 * (a(p) + b x + c(divs))/d
5890 * and ma is represented by
5892 * x = D(p) + F(y) + G(divs')
5894 * then the result is
5896 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5898 * The divs in the local space of the input are similarly adjusted
5899 * through a call to isl_local_space_preimage_multi_aff.
5901 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5902 __isl_take isl_multi_aff *ma)
5904 isl_aff *res = NULL;
5905 isl_local_space *ls;
5906 isl_size n_div_aff, n_div_ma;
5907 isl_int f, c1, c2, g;
5909 ma = isl_multi_aff_align_divs(ma);
5910 if (!aff || !ma)
5911 goto error;
5913 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5914 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5915 if (n_div_aff < 0 || n_div_ma < 0)
5916 goto error;
5918 ls = isl_aff_get_domain_local_space(aff);
5919 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5920 res = isl_aff_alloc(ls);
5921 if (!res)
5922 goto error;
5924 isl_int_init(f);
5925 isl_int_init(c1);
5926 isl_int_init(c2);
5927 isl_int_init(g);
5929 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5930 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5931 res = isl_aff_free(res);
5933 isl_int_clear(f);
5934 isl_int_clear(c1);
5935 isl_int_clear(c2);
5936 isl_int_clear(g);
5938 isl_aff_free(aff);
5939 isl_multi_aff_free(ma);
5940 res = isl_aff_normalize(res);
5941 return res;
5942 error:
5943 isl_aff_free(aff);
5944 isl_multi_aff_free(ma);
5945 isl_aff_free(res);
5946 return NULL;
5949 /* Compute the pullback of "aff1" by the function represented by "aff2".
5950 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5951 * defined over the domain space of "aff1".
5953 * The domain of "aff1" should match the range of "aff2", which means
5954 * that it should be single-dimensional.
5956 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5957 __isl_take isl_aff *aff2)
5959 isl_multi_aff *ma;
5961 ma = isl_multi_aff_from_aff(aff2);
5962 return isl_aff_pullback_multi_aff(aff1, ma);
5965 /* Compute the pullback of "ma1" by the function represented by "ma2".
5966 * In other words, plug in "ma2" in "ma1".
5968 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5969 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5971 int i;
5972 isl_size n;
5973 isl_space *space = NULL;
5975 isl_multi_aff_align_params_bin(&ma1, &ma2);
5976 ma2 = isl_multi_aff_align_divs(ma2);
5977 n = isl_multi_aff_size(ma1);
5978 if (n < 0 || !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 < n; ++i) {
5985 isl_aff *aff;
5987 aff = isl_multi_aff_take_at(ma1, i);
5988 aff = isl_aff_pullback_multi_aff(aff, isl_multi_aff_copy(ma2));
5989 ma1 = isl_multi_aff_restore_at(ma1, i, aff);
5992 ma1 = isl_multi_aff_reset_space(ma1, space);
5993 isl_multi_aff_free(ma2);
5994 return ma1;
5995 error:
5996 isl_space_free(space);
5997 isl_multi_aff_free(ma2);
5998 isl_multi_aff_free(ma1);
5999 return NULL;
6002 /* Extend the local space of "dst" to include the divs
6003 * in the local space of "src".
6005 * If "src" does not have any divs or if the local spaces of "dst" and
6006 * "src" are the same, then no extension is required.
6008 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6009 __isl_keep isl_aff *src)
6011 isl_ctx *ctx;
6012 isl_size src_n_div, dst_n_div;
6013 int *exp1 = NULL;
6014 int *exp2 = NULL;
6015 isl_bool equal;
6016 isl_mat *div;
6018 if (!src || !dst)
6019 return isl_aff_free(dst);
6021 ctx = isl_aff_get_ctx(src);
6022 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6023 if (equal < 0)
6024 return isl_aff_free(dst);
6025 if (!equal)
6026 isl_die(ctx, isl_error_invalid,
6027 "spaces don't match", goto error);
6029 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6030 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6031 if (src_n_div == 0)
6032 return dst;
6033 equal = isl_local_space_is_equal(src->ls, dst->ls);
6034 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6035 return isl_aff_free(dst);
6036 if (equal)
6037 return dst;
6039 exp1 = isl_alloc_array(ctx, int, src_n_div);
6040 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6041 if (!exp1 || (dst_n_div && !exp2))
6042 goto error;
6044 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6045 dst = isl_aff_expand_divs(dst, div, exp2);
6046 free(exp1);
6047 free(exp2);
6049 return dst;
6050 error:
6051 free(exp1);
6052 free(exp2);
6053 return isl_aff_free(dst);
6056 /* Adjust the local spaces of the affine expressions in "maff"
6057 * such that they all have the save divs.
6059 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6060 __isl_take isl_multi_aff *maff)
6062 isl_aff *aff_0;
6063 isl_size n;
6064 int i;
6066 n = isl_multi_aff_size(maff);
6067 if (n < 0)
6068 return isl_multi_aff_free(maff);
6069 if (n <= 1)
6070 return maff;
6072 aff_0 = isl_multi_aff_take_at(maff, 0);
6073 for (i = 1; i < n; ++i) {
6074 isl_aff *aff_i;
6076 aff_i = isl_multi_aff_peek_at(maff, i);
6077 aff_0 = isl_aff_align_divs(aff_0, aff_i);
6079 maff = isl_multi_aff_restore_at(maff, 0, aff_0);
6081 aff_0 = isl_multi_aff_peek_at(maff, 0);
6082 for (i = 1; i < n; ++i) {
6083 isl_aff *aff_i;
6085 aff_i = isl_multi_aff_take_at(maff, i);
6086 aff_i = isl_aff_align_divs(aff_i, aff_0);
6087 maff = isl_multi_aff_restore_at(maff, i, aff_i);
6090 return maff;
6093 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6095 aff = isl_aff_cow(aff);
6096 if (!aff)
6097 return NULL;
6099 aff->ls = isl_local_space_lift(aff->ls);
6100 if (!aff->ls)
6101 return isl_aff_free(aff);
6103 return aff;
6106 /* Lift "maff" to a space with extra dimensions such that the result
6107 * has no more existentially quantified variables.
6108 * If "ls" is not NULL, then *ls is assigned the local space that lies
6109 * at the basis of the lifting applied to "maff".
6111 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6112 __isl_give isl_local_space **ls)
6114 int i;
6115 isl_space *space;
6116 isl_aff *aff;
6117 isl_size n, n_div;
6119 if (ls)
6120 *ls = NULL;
6122 n = isl_multi_aff_size(maff);
6123 if (n < 0)
6124 return isl_multi_aff_free(maff);
6126 if (n == 0) {
6127 if (ls) {
6128 isl_space *space = isl_multi_aff_get_domain_space(maff);
6129 *ls = isl_local_space_from_space(space);
6130 if (!*ls)
6131 return isl_multi_aff_free(maff);
6133 return maff;
6136 maff = isl_multi_aff_align_divs(maff);
6138 aff = isl_multi_aff_peek_at(maff, 0);
6139 n_div = isl_aff_dim(aff, isl_dim_div);
6140 if (n_div < 0)
6141 return isl_multi_aff_free(maff);
6142 space = isl_multi_aff_get_space(maff);
6143 space = isl_space_lift(isl_space_domain(space), n_div);
6144 space = isl_space_extend_domain_with_range(space,
6145 isl_multi_aff_get_space(maff));
6146 maff = isl_multi_aff_restore_space(maff, space);
6148 if (ls) {
6149 aff = isl_multi_aff_peek_at(maff, 0);
6150 *ls = isl_aff_get_domain_local_space(aff);
6151 if (!*ls)
6152 return isl_multi_aff_free(maff);
6155 for (i = 0; i < n; ++i) {
6156 aff = isl_multi_aff_take_at(maff, i);
6157 aff = isl_aff_lift(aff);
6158 maff = isl_multi_aff_restore_at(maff, i, aff);
6161 return maff;
6164 #undef TYPE
6165 #define TYPE isl_pw_multi_aff
6166 static
6167 #include "check_type_range_templ.c"
6169 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6171 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6172 __isl_keep isl_pw_multi_aff *pma, int pos)
6174 int i;
6175 isl_size n_out;
6176 isl_space *space;
6177 isl_pw_aff *pa;
6179 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6180 return NULL;
6182 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6183 if (n_out < 0)
6184 return NULL;
6186 space = isl_pw_multi_aff_get_space(pma);
6187 space = isl_space_drop_dims(space, isl_dim_out,
6188 pos + 1, n_out - pos - 1);
6189 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6191 pa = isl_pw_aff_alloc_size(space, pma->n);
6192 for (i = 0; i < pma->n; ++i) {
6193 isl_aff *aff;
6194 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6195 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6198 return pa;
6201 /* This is an alternative name for the function above.
6203 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6204 __isl_keep isl_pw_multi_aff *pma, int pos)
6206 return isl_pw_multi_aff_get_at(pma, pos);
6209 /* Return an isl_pw_multi_aff with the given "set" as domain and
6210 * an unnamed zero-dimensional range.
6212 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6213 __isl_take isl_set *set)
6215 isl_multi_aff *ma;
6216 isl_space *space;
6218 space = isl_set_get_space(set);
6219 space = isl_space_from_domain(space);
6220 ma = isl_multi_aff_zero(space);
6221 return isl_pw_multi_aff_alloc(set, ma);
6224 /* Add an isl_pw_multi_aff with the given "set" as domain and
6225 * an unnamed zero-dimensional range to *user.
6227 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6228 void *user)
6230 isl_union_pw_multi_aff **upma = user;
6231 isl_pw_multi_aff *pma;
6233 pma = isl_pw_multi_aff_from_domain(set);
6234 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6236 return isl_stat_ok;
6239 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6240 * an unnamed zero-dimensional range.
6242 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6243 __isl_take isl_union_set *uset)
6245 isl_space *space;
6246 isl_union_pw_multi_aff *upma;
6248 if (!uset)
6249 return NULL;
6251 space = isl_union_set_get_space(uset);
6252 upma = isl_union_pw_multi_aff_empty(space);
6254 if (isl_union_set_foreach_set(uset,
6255 &add_pw_multi_aff_from_domain, &upma) < 0)
6256 goto error;
6258 isl_union_set_free(uset);
6259 return upma;
6260 error:
6261 isl_union_set_free(uset);
6262 isl_union_pw_multi_aff_free(upma);
6263 return NULL;
6266 /* Local data for bin_entry and the callback "fn".
6268 struct isl_union_pw_multi_aff_bin_data {
6269 isl_union_pw_multi_aff *upma2;
6270 isl_union_pw_multi_aff *res;
6271 isl_pw_multi_aff *pma;
6272 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6275 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6276 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6278 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6280 struct isl_union_pw_multi_aff_bin_data *data = user;
6281 isl_stat r;
6283 data->pma = pma;
6284 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6285 data->fn, data);
6286 isl_pw_multi_aff_free(pma);
6288 return r;
6291 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6292 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6293 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6294 * as *entry. The callback should adjust data->res if desired.
6296 static __isl_give isl_union_pw_multi_aff *bin_op(
6297 __isl_take isl_union_pw_multi_aff *upma1,
6298 __isl_take isl_union_pw_multi_aff *upma2,
6299 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6301 isl_space *space;
6302 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6304 space = isl_union_pw_multi_aff_get_space(upma2);
6305 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6306 space = isl_union_pw_multi_aff_get_space(upma1);
6307 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6309 if (!upma1 || !upma2)
6310 goto error;
6312 data.upma2 = upma2;
6313 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6314 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6315 &bin_entry, &data) < 0)
6316 goto error;
6318 isl_union_pw_multi_aff_free(upma1);
6319 isl_union_pw_multi_aff_free(upma2);
6320 return data.res;
6321 error:
6322 isl_union_pw_multi_aff_free(upma1);
6323 isl_union_pw_multi_aff_free(upma2);
6324 isl_union_pw_multi_aff_free(data.res);
6325 return NULL;
6328 /* Given two isl_pw_multi_affs A -> B and C -> D,
6329 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6331 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6332 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6334 isl_space *space;
6336 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6337 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6338 isl_pw_multi_aff_get_space(pma2));
6339 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6340 &isl_multi_aff_range_product);
6343 /* Given two isl_pw_multi_affs A -> B and C -> D,
6344 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6346 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6347 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6349 isl_space *space;
6351 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6352 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6353 isl_pw_multi_aff_get_space(pma2));
6354 space = isl_space_flatten_range(space);
6355 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6356 &isl_multi_aff_flat_range_product);
6359 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6360 * to compute some form of range product and add the result to data->res.
6362 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6363 __isl_give isl_pw_multi_aff *(*range_product)(
6364 __isl_take isl_pw_multi_aff *pma1,
6365 __isl_take isl_pw_multi_aff *pma2),
6366 void *user)
6368 struct isl_union_pw_multi_aff_bin_data *data = user;
6369 isl_bool match;
6370 isl_space *space1, *space2;
6372 space1 = isl_pw_multi_aff_peek_space(data->pma);
6373 space2 = isl_pw_multi_aff_peek_space(pma2);
6374 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6375 space2, isl_dim_in);
6376 if (match < 0 || !match) {
6377 isl_pw_multi_aff_free(pma2);
6378 return match < 0 ? isl_stat_error : isl_stat_ok;
6381 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6383 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6385 return isl_stat_ok;
6388 /* If data->pma and "pma2" have the same domain space, then compute
6389 * their flat range product and add the result to data->res.
6391 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6392 void *user)
6394 return gen_range_product_entry(pma2,
6395 &isl_pw_multi_aff_flat_range_product, user);
6398 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6399 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6401 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6402 __isl_take isl_union_pw_multi_aff *upma1,
6403 __isl_take isl_union_pw_multi_aff *upma2)
6405 return bin_op(upma1, upma2, &flat_range_product_entry);
6408 /* If data->pma and "pma2" have the same domain space, then compute
6409 * their range product and add the result to data->res.
6411 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6412 void *user)
6414 return gen_range_product_entry(pma2,
6415 &isl_pw_multi_aff_range_product, user);
6418 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6419 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6421 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6422 __isl_take isl_union_pw_multi_aff *upma1,
6423 __isl_take isl_union_pw_multi_aff *upma2)
6425 return bin_op(upma1, upma2, &range_product_entry);
6428 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6429 * The parameters are assumed to have been aligned.
6431 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6432 * except that it works on two different isl_pw_* types.
6434 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6435 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6436 __isl_take isl_pw_aff *pa)
6438 int i, j, n;
6439 isl_pw_multi_aff *res = NULL;
6441 if (!pma || !pa)
6442 goto error;
6444 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6445 pa->dim, isl_dim_in))
6446 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6447 "domains don't match", goto error);
6448 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6449 goto error;
6451 n = pma->n * pa->n;
6452 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6454 for (i = 0; i < pma->n; ++i) {
6455 for (j = 0; j < pa->n; ++j) {
6456 isl_set *common;
6457 isl_multi_aff *res_ij;
6458 int empty;
6460 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6461 isl_set_copy(pa->p[j].set));
6462 empty = isl_set_plain_is_empty(common);
6463 if (empty < 0 || empty) {
6464 isl_set_free(common);
6465 if (empty < 0)
6466 goto error;
6467 continue;
6470 res_ij = isl_multi_aff_set_aff(
6471 isl_multi_aff_copy(pma->p[i].maff), pos,
6472 isl_aff_copy(pa->p[j].aff));
6473 res_ij = isl_multi_aff_gist(res_ij,
6474 isl_set_copy(common));
6476 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6480 isl_pw_multi_aff_free(pma);
6481 isl_pw_aff_free(pa);
6482 return res;
6483 error:
6484 isl_pw_multi_aff_free(pma);
6485 isl_pw_aff_free(pa);
6486 return isl_pw_multi_aff_free(res);
6489 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6491 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6492 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6493 __isl_take isl_pw_aff *pa)
6495 isl_bool equal_params;
6497 if (!pma || !pa)
6498 goto error;
6499 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6500 if (equal_params < 0)
6501 goto error;
6502 if (equal_params)
6503 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6504 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6505 isl_pw_aff_check_named_params(pa) < 0)
6506 goto error;
6507 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6508 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6509 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6510 error:
6511 isl_pw_multi_aff_free(pma);
6512 isl_pw_aff_free(pa);
6513 return NULL;
6516 /* Do the parameters of "pa" match those of "space"?
6518 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6519 __isl_keep isl_space *space)
6521 isl_space *pa_space;
6522 isl_bool match;
6524 if (!pa || !space)
6525 return isl_bool_error;
6527 pa_space = isl_pw_aff_get_space(pa);
6529 match = isl_space_has_equal_params(space, pa_space);
6531 isl_space_free(pa_space);
6532 return match;
6535 /* Check that the domain space of "pa" matches "space".
6537 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6538 __isl_keep isl_space *space)
6540 isl_space *pa_space;
6541 isl_bool match;
6543 if (!pa || !space)
6544 return isl_stat_error;
6546 pa_space = isl_pw_aff_get_space(pa);
6548 match = isl_space_has_equal_params(space, pa_space);
6549 if (match < 0)
6550 goto error;
6551 if (!match)
6552 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6553 "parameters don't match", goto error);
6554 match = isl_space_tuple_is_equal(space, isl_dim_in,
6555 pa_space, isl_dim_in);
6556 if (match < 0)
6557 goto error;
6558 if (!match)
6559 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6560 "domains don't match", goto error);
6561 isl_space_free(pa_space);
6562 return isl_stat_ok;
6563 error:
6564 isl_space_free(pa_space);
6565 return isl_stat_error;
6568 #undef BASE
6569 #define BASE pw_aff
6570 #undef DOMBASE
6571 #define DOMBASE set
6573 #include <isl_multi_explicit_domain.c>
6574 #include <isl_multi_pw_aff_explicit_domain.c>
6575 #include <isl_multi_templ.c>
6576 #include <isl_multi_un_op_templ.c>
6577 #include <isl_multi_bin_val_templ.c>
6578 #include <isl_multi_add_constant_templ.c>
6579 #include <isl_multi_apply_set.c>
6580 #include <isl_multi_arith_templ.c>
6581 #include <isl_multi_bind_templ.c>
6582 #include <isl_multi_bind_domain_templ.c>
6583 #include <isl_multi_coalesce.c>
6584 #include <isl_multi_domain_templ.c>
6585 #include <isl_multi_dim_id_templ.c>
6586 #include <isl_multi_dims.c>
6587 #include <isl_multi_from_base_templ.c>
6588 #include <isl_multi_gist.c>
6589 #include <isl_multi_hash.c>
6590 #include <isl_multi_identity_templ.c>
6591 #include <isl_multi_align_set.c>
6592 #include <isl_multi_insert_domain_templ.c>
6593 #include <isl_multi_intersect.c>
6594 #include <isl_multi_min_max_templ.c>
6595 #include <isl_multi_move_dims_templ.c>
6596 #include <isl_multi_nan_templ.c>
6597 #include <isl_multi_param_templ.c>
6598 #include <isl_multi_product_templ.c>
6599 #include <isl_multi_splice_templ.c>
6600 #include <isl_multi_tuple_id_templ.c>
6601 #include <isl_multi_union_add_templ.c>
6602 #include <isl_multi_zero_templ.c>
6603 #include <isl_multi_unbind_params_templ.c>
6605 /* Is every element of "mpa" defined over a single universe domain?
6607 isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
6609 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
6612 /* Given that every element of "mpa" is defined over a single universe domain,
6613 * return the corresponding base expressions.
6615 __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
6616 __isl_take isl_multi_pw_aff *mpa)
6618 int i;
6619 isl_size n;
6620 isl_multi_aff *ma;
6622 n = isl_multi_pw_aff_size(mpa);
6623 if (n < 0)
6624 mpa = isl_multi_pw_aff_free(mpa);
6625 ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
6626 for (i = 0; i < n; ++i) {
6627 isl_aff *aff;
6629 aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
6630 ma = isl_multi_aff_set_aff(ma, i, aff);
6632 isl_multi_pw_aff_free(mpa);
6633 return ma;
6636 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6637 * with this explicit domain.
6639 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6640 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6642 isl_set *dom;
6644 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6645 return map;
6647 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6648 map = isl_map_intersect_domain(map, dom);
6650 return map;
6653 /* Are all elements of "mpa" piecewise constants?
6655 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6657 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6660 /* Does "mpa" have a non-trivial explicit domain?
6662 * The explicit domain, if present, is trivial if it represents
6663 * an (obviously) universe set.
6665 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6666 __isl_keep isl_multi_pw_aff *mpa)
6668 if (!mpa)
6669 return isl_bool_error;
6670 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6671 return isl_bool_false;
6672 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6675 #undef BASE
6676 #define BASE set
6678 #include "isl_opt_mpa_templ.c"
6680 /* Compute the minima of the set dimensions as a function of the
6681 * parameters, but independently of the other set dimensions.
6683 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6685 return set_opt_mpa(set, &isl_set_dim_min);
6688 /* Compute the maxima of the set dimensions as a function of the
6689 * parameters, but independently of the other set dimensions.
6691 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6693 return set_opt_mpa(set, &isl_set_dim_max);
6696 #undef BASE
6697 #define BASE map
6699 #include "isl_opt_mpa_templ.c"
6701 /* Compute the minima of the output dimensions as a function of the
6702 * parameters and input dimensions, but independently of
6703 * the other output dimensions.
6705 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6707 return map_opt_mpa(map, &isl_map_dim_min);
6710 /* Compute the maxima of the output dimensions as a function of the
6711 * parameters and input dimensions, but independently of
6712 * the other output dimensions.
6714 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6716 return map_opt_mpa(map, &isl_map_dim_max);
6719 #undef TYPE
6720 #define TYPE isl_pw_multi_aff
6721 #include "isl_type_check_match_range_multi_val.c"
6723 /* Apply "fn" to the base expressions of "pma" and "mv".
6725 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_op_multi_val(
6726 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv,
6727 __isl_give isl_multi_aff *(*fn)(__isl_take isl_multi_aff *ma,
6728 __isl_take isl_multi_val *mv))
6730 int i;
6731 isl_size n;
6733 if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
6734 goto error;
6736 n = isl_pw_multi_aff_n_piece(pma);
6737 if (n < 0)
6738 goto error;
6740 for (i = 0; i < n; ++i) {
6741 isl_multi_aff *ma;
6743 ma = isl_pw_multi_aff_take_base_at(pma, i);
6744 ma = fn(ma, isl_multi_val_copy(mv));
6745 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
6748 isl_multi_val_free(mv);
6749 return pma;
6750 error:
6751 isl_multi_val_free(mv);
6752 isl_pw_multi_aff_free(pma);
6753 return NULL;
6756 /* Scale the elements of "pma" by the corresponding elements of "mv".
6758 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6759 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6761 return isl_pw_multi_aff_op_multi_val(pma, mv,
6762 &isl_multi_aff_scale_multi_val);
6765 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6767 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_multi_val(
6768 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6770 return isl_pw_multi_aff_op_multi_val(pma, mv,
6771 &isl_multi_aff_scale_down_multi_val);
6774 /* This function is called for each entry of an isl_union_pw_multi_aff.
6775 * If the space of the entry matches that of data->mv,
6776 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6777 * Otherwise, return an empty isl_pw_multi_aff.
6779 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6780 __isl_take isl_pw_multi_aff *pma, void *user)
6782 isl_bool equal;
6783 isl_multi_val *mv = user;
6785 equal = isl_pw_multi_aff_match_range_multi_val(pma, mv);
6786 if (equal < 0)
6787 return isl_pw_multi_aff_free(pma);
6788 if (!equal) {
6789 isl_space *space = isl_pw_multi_aff_get_space(pma);
6790 isl_pw_multi_aff_free(pma);
6791 return isl_pw_multi_aff_empty(space);
6794 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6797 /* Scale the elements of "upma" by the corresponding elements of "mv",
6798 * for those entries that match the space of "mv".
6800 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6801 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6803 struct isl_union_pw_multi_aff_transform_control control = {
6804 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6805 .fn_user = mv,
6808 upma = isl_union_pw_multi_aff_align_params(upma,
6809 isl_multi_val_get_space(mv));
6810 mv = isl_multi_val_align_params(mv,
6811 isl_union_pw_multi_aff_get_space(upma));
6812 if (!upma || !mv)
6813 goto error;
6815 return isl_union_pw_multi_aff_transform(upma, &control);
6817 isl_multi_val_free(mv);
6818 return upma;
6819 error:
6820 isl_multi_val_free(mv);
6821 isl_union_pw_multi_aff_free(upma);
6822 return NULL;
6825 /* Construct and return a piecewise multi affine expression
6826 * in the given space with value zero in each of the output dimensions and
6827 * a universe domain.
6829 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6831 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6834 /* Construct and return a piecewise multi affine expression
6835 * that is equal to the given piecewise affine expression.
6837 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6838 __isl_take isl_pw_aff *pa)
6840 int i;
6841 isl_space *space;
6842 isl_pw_multi_aff *pma;
6844 if (!pa)
6845 return NULL;
6847 space = isl_pw_aff_get_space(pa);
6848 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6850 for (i = 0; i < pa->n; ++i) {
6851 isl_set *set;
6852 isl_multi_aff *ma;
6854 set = isl_set_copy(pa->p[i].set);
6855 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6856 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6859 isl_pw_aff_free(pa);
6860 return pma;
6863 /* Construct and return a piecewise multi affine expression
6864 * that is equal to the given multi piecewise affine expression
6865 * on the shared domain of the piecewise affine expressions,
6866 * in the special case of a 0D multi piecewise affine expression.
6868 * Create a piecewise multi affine expression with the explicit domain of
6869 * the 0D multi piecewise affine expression as domain.
6871 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6872 __isl_take isl_multi_pw_aff *mpa)
6874 isl_space *space;
6875 isl_set *dom;
6876 isl_multi_aff *ma;
6878 space = isl_multi_pw_aff_get_space(mpa);
6879 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6880 isl_multi_pw_aff_free(mpa);
6882 ma = isl_multi_aff_zero(space);
6883 return isl_pw_multi_aff_alloc(dom, ma);
6886 /* Construct and return a piecewise multi affine expression
6887 * that is equal to the given multi piecewise affine expression
6888 * on the shared domain of the piecewise affine expressions.
6890 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6891 __isl_take isl_multi_pw_aff *mpa)
6893 int i;
6894 isl_space *space;
6895 isl_pw_aff *pa;
6896 isl_pw_multi_aff *pma;
6898 if (!mpa)
6899 return NULL;
6901 if (mpa->n == 0)
6902 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6904 space = isl_multi_pw_aff_get_space(mpa);
6905 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6906 pma = isl_pw_multi_aff_from_pw_aff(pa);
6908 for (i = 1; i < mpa->n; ++i) {
6909 isl_pw_multi_aff *pma_i;
6911 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6912 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6913 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6916 pma = isl_pw_multi_aff_reset_space(pma, space);
6918 isl_multi_pw_aff_free(mpa);
6919 return pma;
6922 /* Convenience function that constructs an isl_multi_pw_aff
6923 * directly from an isl_aff.
6925 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6927 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6930 /* Construct and return a multi piecewise affine expression
6931 * that is equal to the given multi affine expression.
6933 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6934 __isl_take isl_multi_aff *ma)
6936 int i;
6937 isl_size n;
6938 isl_multi_pw_aff *mpa;
6940 n = isl_multi_aff_dim(ma, isl_dim_out);
6941 if (n < 0)
6942 ma = isl_multi_aff_free(ma);
6943 if (!ma)
6944 return NULL;
6946 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6948 for (i = 0; i < n; ++i) {
6949 isl_pw_aff *pa;
6951 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6952 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6955 isl_multi_aff_free(ma);
6956 return mpa;
6959 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6960 * but is considered as a function on an isl_multi_aff when exported.
6962 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
6963 __isl_take isl_multi_aff *ma)
6965 return isl_multi_pw_aff_from_multi_aff(ma);
6968 /* Construct and return a multi piecewise affine expression
6969 * that is equal to the given piecewise multi affine expression.
6971 * If the resulting multi piecewise affine expression has
6972 * an explicit domain, then assign it the domain of the input.
6973 * In other cases, the domain is stored in the individual elements.
6975 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6976 __isl_take isl_pw_multi_aff *pma)
6978 int i;
6979 isl_size n;
6980 isl_space *space;
6981 isl_multi_pw_aff *mpa;
6983 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6984 if (n < 0)
6985 pma = isl_pw_multi_aff_free(pma);
6986 space = isl_pw_multi_aff_get_space(pma);
6987 mpa = isl_multi_pw_aff_alloc(space);
6989 for (i = 0; i < n; ++i) {
6990 isl_pw_aff *pa;
6992 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6993 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6995 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6996 isl_set *dom;
6998 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6999 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
7002 isl_pw_multi_aff_free(pma);
7003 return mpa;
7006 /* This function performs the same operation as
7007 * isl_multi_pw_aff_from_pw_multi_aff,
7008 * but is considered as a function on an isl_pw_multi_aff when exported.
7010 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
7011 __isl_take isl_pw_multi_aff *pma)
7013 return isl_multi_pw_aff_from_pw_multi_aff(pma);
7016 /* Do "pa1" and "pa2" represent the same function?
7018 * We first check if they are obviously equal.
7019 * If not, we convert them to maps and check if those are equal.
7021 * If "pa1" or "pa2" contain any NaNs, then they are considered
7022 * not to be the same. A NaN is not equal to anything, not even
7023 * to another NaN.
7025 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
7026 __isl_keep isl_pw_aff *pa2)
7028 isl_bool equal;
7029 isl_bool has_nan;
7030 isl_map *map1, *map2;
7032 if (!pa1 || !pa2)
7033 return isl_bool_error;
7035 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
7036 if (equal < 0 || equal)
7037 return equal;
7038 has_nan = either_involves_nan(pa1, pa2);
7039 if (has_nan < 0)
7040 return isl_bool_error;
7041 if (has_nan)
7042 return isl_bool_false;
7044 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
7045 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
7046 equal = isl_map_is_equal(map1, map2);
7047 isl_map_free(map1);
7048 isl_map_free(map2);
7050 return equal;
7053 /* Do "mpa1" and "mpa2" represent the same function?
7055 * Note that we cannot convert the entire isl_multi_pw_aff
7056 * to a map because the domains of the piecewise affine expressions
7057 * may not be the same.
7059 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7060 __isl_keep isl_multi_pw_aff *mpa2)
7062 int i;
7063 isl_bool equal, equal_params;
7065 if (!mpa1 || !mpa2)
7066 return isl_bool_error;
7068 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7069 if (equal_params < 0)
7070 return isl_bool_error;
7071 if (!equal_params) {
7072 if (!isl_space_has_named_params(mpa1->space))
7073 return isl_bool_false;
7074 if (!isl_space_has_named_params(mpa2->space))
7075 return isl_bool_false;
7076 mpa1 = isl_multi_pw_aff_copy(mpa1);
7077 mpa2 = isl_multi_pw_aff_copy(mpa2);
7078 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7079 isl_multi_pw_aff_get_space(mpa2));
7080 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7081 isl_multi_pw_aff_get_space(mpa1));
7082 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7083 isl_multi_pw_aff_free(mpa1);
7084 isl_multi_pw_aff_free(mpa2);
7085 return equal;
7088 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7089 if (equal < 0 || !equal)
7090 return equal;
7092 for (i = 0; i < mpa1->n; ++i) {
7093 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7094 if (equal < 0 || !equal)
7095 return equal;
7098 return isl_bool_true;
7101 /* Do "pma1" and "pma2" represent the same function?
7103 * First check if they are obviously equal.
7104 * If not, then convert them to maps and check if those are equal.
7106 * If "pa1" or "pa2" contain any NaNs, then they are considered
7107 * not to be the same. A NaN is not equal to anything, not even
7108 * to another NaN.
7110 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7111 __isl_keep isl_pw_multi_aff *pma2)
7113 isl_bool equal;
7114 isl_bool has_nan;
7115 isl_map *map1, *map2;
7117 if (!pma1 || !pma2)
7118 return isl_bool_error;
7120 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7121 if (equal < 0 || equal)
7122 return equal;
7123 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7124 if (has_nan >= 0 && !has_nan)
7125 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7126 if (has_nan < 0 || has_nan)
7127 return isl_bool_not(has_nan);
7129 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7130 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7131 equal = isl_map_is_equal(map1, map2);
7132 isl_map_free(map1);
7133 isl_map_free(map2);
7135 return equal;
7138 #undef BASE
7139 #define BASE multi_aff
7141 #include "isl_multi_pw_aff_pullback_templ.c"
7143 #undef BASE
7144 #define BASE pw_multi_aff
7146 #include "isl_multi_pw_aff_pullback_templ.c"
7148 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7149 * with the domain of "aff". The domain of the result is the same
7150 * as that of "mpa".
7151 * "mpa" and "aff" are assumed to have been aligned.
7153 * We first extract the parametric constant from "aff", defined
7154 * over the correct domain.
7155 * Then we add the appropriate combinations of the members of "mpa".
7156 * Finally, we add the integer divisions through recursive calls.
7158 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7159 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7161 int i;
7162 isl_size n_in, n_div, n_mpa_in;
7163 isl_space *space;
7164 isl_val *v;
7165 isl_pw_aff *pa;
7166 isl_aff *tmp;
7168 n_in = isl_aff_dim(aff, isl_dim_in);
7169 n_div = isl_aff_dim(aff, isl_dim_div);
7170 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7171 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7172 goto error;
7174 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7175 tmp = isl_aff_copy(aff);
7176 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7177 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7178 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7179 tmp = isl_aff_reset_domain_space(tmp, space);
7180 pa = isl_pw_aff_from_aff(tmp);
7182 for (i = 0; i < n_in; ++i) {
7183 isl_pw_aff *pa_i;
7185 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7186 continue;
7187 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7188 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7189 pa_i = isl_pw_aff_scale_val(pa_i, v);
7190 pa = isl_pw_aff_add(pa, pa_i);
7193 for (i = 0; i < n_div; ++i) {
7194 isl_aff *div;
7195 isl_pw_aff *pa_i;
7197 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7198 continue;
7199 div = isl_aff_get_div(aff, i);
7200 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7201 isl_multi_pw_aff_copy(mpa), div);
7202 pa_i = isl_pw_aff_floor(pa_i);
7203 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7204 pa_i = isl_pw_aff_scale_val(pa_i, v);
7205 pa = isl_pw_aff_add(pa, pa_i);
7208 isl_multi_pw_aff_free(mpa);
7209 isl_aff_free(aff);
7211 return pa;
7212 error:
7213 isl_multi_pw_aff_free(mpa);
7214 isl_aff_free(aff);
7215 return NULL;
7218 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7219 * with the domain of "aff". The domain of the result is the same
7220 * as that of "mpa".
7222 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7223 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7225 isl_bool equal_params;
7227 if (!aff || !mpa)
7228 goto error;
7229 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7230 if (equal_params < 0)
7231 goto error;
7232 if (equal_params)
7233 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7235 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7236 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7238 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7239 error:
7240 isl_aff_free(aff);
7241 isl_multi_pw_aff_free(mpa);
7242 return NULL;
7245 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7246 * with the domain of "pa". The domain of the result is the same
7247 * as that of "mpa".
7248 * "mpa" and "pa" are assumed to have been aligned.
7250 * We consider each piece in turn. Note that the domains of the
7251 * pieces are assumed to be disjoint and they remain disjoint
7252 * after taking the preimage (over the same function).
7254 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7255 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7257 isl_space *space;
7258 isl_pw_aff *res;
7259 int i;
7261 if (!mpa || !pa)
7262 goto error;
7264 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7265 isl_pw_aff_get_space(pa));
7266 res = isl_pw_aff_empty(space);
7268 for (i = 0; i < pa->n; ++i) {
7269 isl_pw_aff *pa_i;
7270 isl_set *domain;
7272 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7273 isl_multi_pw_aff_copy(mpa),
7274 isl_aff_copy(pa->p[i].aff));
7275 domain = isl_set_copy(pa->p[i].set);
7276 domain = isl_set_preimage_multi_pw_aff(domain,
7277 isl_multi_pw_aff_copy(mpa));
7278 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7279 res = isl_pw_aff_add_disjoint(res, pa_i);
7282 isl_pw_aff_free(pa);
7283 isl_multi_pw_aff_free(mpa);
7284 return res;
7285 error:
7286 isl_pw_aff_free(pa);
7287 isl_multi_pw_aff_free(mpa);
7288 return NULL;
7291 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7292 * with the domain of "pa". The domain of the result is the same
7293 * as that of "mpa".
7295 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7296 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7298 isl_bool equal_params;
7300 if (!pa || !mpa)
7301 goto error;
7302 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7303 if (equal_params < 0)
7304 goto error;
7305 if (equal_params)
7306 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7308 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7309 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7311 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7312 error:
7313 isl_pw_aff_free(pa);
7314 isl_multi_pw_aff_free(mpa);
7315 return NULL;
7318 /* Compute the pullback of "pa" by the function represented by "mpa".
7319 * In other words, plug in "mpa" in "pa".
7321 * The pullback is computed by applying "pa" to "mpa".
7323 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7324 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7326 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7329 #undef BASE
7330 #define BASE multi_pw_aff
7332 #include "isl_multi_pw_aff_pullback_templ.c"
7334 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7335 * of "mpa1" and "mpa2" live in the same space, construct map space
7336 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7337 * with this map space as extract argument.
7339 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7340 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7341 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7342 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7344 int match;
7345 isl_space *space1, *space2;
7346 isl_map *res;
7348 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7349 isl_multi_pw_aff_get_space(mpa2));
7350 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7351 isl_multi_pw_aff_get_space(mpa1));
7352 if (!mpa1 || !mpa2)
7353 goto error;
7354 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7355 mpa2->space, isl_dim_out);
7356 if (match < 0)
7357 goto error;
7358 if (!match)
7359 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7360 "range spaces don't match", goto error);
7361 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7362 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7363 space1 = isl_space_map_from_domain_and_range(space1, space2);
7365 res = order(mpa1, mpa2, space1);
7366 isl_multi_pw_aff_free(mpa1);
7367 isl_multi_pw_aff_free(mpa2);
7368 return res;
7369 error:
7370 isl_multi_pw_aff_free(mpa1);
7371 isl_multi_pw_aff_free(mpa2);
7372 return NULL;
7375 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7376 * where the function values are equal. "space" is the space of the result.
7377 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7379 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7380 * in the sequences are equal.
7382 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7383 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7384 __isl_take isl_space *space)
7386 int i;
7387 isl_size n;
7388 isl_map *res;
7390 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7391 if (n < 0)
7392 space = isl_space_free(space);
7393 res = isl_map_universe(space);
7395 for (i = 0; i < n; ++i) {
7396 isl_pw_aff *pa1, *pa2;
7397 isl_map *map;
7399 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7400 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7401 map = isl_pw_aff_eq_map(pa1, pa2);
7402 res = isl_map_intersect(res, map);
7405 return res;
7408 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7409 * where the function values are equal.
7411 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7412 __isl_take isl_multi_pw_aff *mpa2)
7414 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7415 &isl_multi_pw_aff_eq_map_on_space);
7418 /* Intersect "map" with the result of applying "order"
7419 * on two copies of "mpa".
7421 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7422 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7423 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7424 __isl_take isl_multi_pw_aff *mpa2))
7426 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7429 /* Return the subset of "map" where the domain and the range
7430 * have equal "mpa" values.
7432 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7433 __isl_take isl_multi_pw_aff *mpa)
7435 return isl_map_order_at_multi_pw_aff(map, mpa,
7436 &isl_multi_pw_aff_eq_map);
7439 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7440 * where the function values of "mpa1" lexicographically satisfies
7441 * "strict_base"/"base" compared to that of "mpa2".
7442 * "space" is the space of the result.
7443 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7445 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7446 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7447 * when compared to the i-th element of "mpa2" while all previous elements are
7448 * pairwise equal.
7449 * In particular, if i corresponds to the final elements
7450 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7451 * for other values of i.
7452 * If "base" is a strict order, then "base" and "strict_base" are the same.
7454 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7455 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7456 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7457 __isl_take isl_pw_aff *pa2),
7458 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7459 __isl_take isl_pw_aff *pa2),
7460 __isl_take isl_space *space)
7462 int i;
7463 isl_size n;
7464 isl_map *res, *rest;
7466 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7467 if (n < 0)
7468 space = isl_space_free(space);
7469 res = isl_map_empty(isl_space_copy(space));
7470 rest = isl_map_universe(space);
7472 for (i = 0; i < n; ++i) {
7473 int last;
7474 isl_pw_aff *pa1, *pa2;
7475 isl_map *map;
7477 last = i == n - 1;
7479 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7480 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7481 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7482 map = isl_map_intersect(map, isl_map_copy(rest));
7483 res = isl_map_union(res, map);
7485 if (last)
7486 continue;
7488 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7489 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7490 map = isl_pw_aff_eq_map(pa1, pa2);
7491 rest = isl_map_intersect(rest, map);
7494 isl_map_free(rest);
7495 return res;
7498 #undef ORDER
7499 #define ORDER le
7500 #undef STRICT_ORDER
7501 #define STRICT_ORDER lt
7502 #include "isl_aff_lex_templ.c"
7504 #undef ORDER
7505 #define ORDER lt
7506 #undef STRICT_ORDER
7507 #define STRICT_ORDER lt
7508 #include "isl_aff_lex_templ.c"
7510 #undef ORDER
7511 #define ORDER ge
7512 #undef STRICT_ORDER
7513 #define STRICT_ORDER gt
7514 #include "isl_aff_lex_templ.c"
7516 #undef ORDER
7517 #define ORDER gt
7518 #undef STRICT_ORDER
7519 #define STRICT_ORDER gt
7520 #include "isl_aff_lex_templ.c"
7522 /* Compare two isl_affs.
7524 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7525 * than "aff2" and 0 if they are equal.
7527 * The order is fairly arbitrary. We do consider expressions that only involve
7528 * earlier dimensions as "smaller".
7530 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7532 int cmp;
7533 int last1, last2;
7535 if (aff1 == aff2)
7536 return 0;
7538 if (!aff1)
7539 return -1;
7540 if (!aff2)
7541 return 1;
7543 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7544 if (cmp != 0)
7545 return cmp;
7547 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7548 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7549 if (last1 != last2)
7550 return last1 - last2;
7552 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7555 /* Compare two isl_pw_affs.
7557 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7558 * than "pa2" and 0 if they are equal.
7560 * The order is fairly arbitrary. We do consider expressions that only involve
7561 * earlier dimensions as "smaller".
7563 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7564 __isl_keep isl_pw_aff *pa2)
7566 int i;
7567 int cmp;
7569 if (pa1 == pa2)
7570 return 0;
7572 if (!pa1)
7573 return -1;
7574 if (!pa2)
7575 return 1;
7577 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7578 if (cmp != 0)
7579 return cmp;
7581 if (pa1->n != pa2->n)
7582 return pa1->n - pa2->n;
7584 for (i = 0; i < pa1->n; ++i) {
7585 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7586 if (cmp != 0)
7587 return cmp;
7588 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7589 if (cmp != 0)
7590 return cmp;
7593 return 0;
7596 /* Return a piecewise affine expression that is equal to "v" on "domain".
7598 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7599 __isl_take isl_val *v)
7601 isl_space *space;
7602 isl_local_space *ls;
7603 isl_aff *aff;
7605 space = isl_set_get_space(domain);
7606 ls = isl_local_space_from_space(space);
7607 aff = isl_aff_val_on_domain(ls, v);
7609 return isl_pw_aff_alloc(domain, aff);
7612 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7613 * but is considered as a function on an isl_set when exported.
7615 __isl_give isl_pw_aff *isl_set_pw_aff_on_domain_val(__isl_take isl_set *domain,
7616 __isl_take isl_val *v)
7618 return isl_pw_aff_val_on_domain(domain, v);
7621 /* Return a piecewise affine expression that is equal to the parameter
7622 * with identifier "id" on "domain".
7624 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7625 __isl_take isl_set *domain, __isl_take isl_id *id)
7627 isl_space *space;
7628 isl_aff *aff;
7630 space = isl_set_get_space(domain);
7631 space = isl_space_add_param_id(space, isl_id_copy(id));
7632 domain = isl_set_align_params(domain, isl_space_copy(space));
7633 aff = isl_aff_param_on_domain_space_id(space, id);
7635 return isl_pw_aff_alloc(domain, aff);
7638 /* This function performs the same operation as
7639 * isl_pw_aff_param_on_domain_id,
7640 * but is considered as a function on an isl_set when exported.
7642 __isl_give isl_pw_aff *isl_set_param_pw_aff_on_domain_id(
7643 __isl_take isl_set *domain, __isl_take isl_id *id)
7645 return isl_pw_aff_param_on_domain_id(domain, id);
7648 /* Return a multi affine expression that is equal to "mv" on domain
7649 * space "space".
7651 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7652 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7654 int i;
7655 isl_size n;
7656 isl_space *space2;
7657 isl_local_space *ls;
7658 isl_multi_aff *ma;
7660 n = isl_multi_val_dim(mv, isl_dim_set);
7661 if (!space || n < 0)
7662 goto error;
7664 space2 = isl_multi_val_get_space(mv);
7665 space2 = isl_space_align_params(space2, isl_space_copy(space));
7666 space = isl_space_align_params(space, isl_space_copy(space2));
7667 space = isl_space_map_from_domain_and_range(space, space2);
7668 ma = isl_multi_aff_alloc(isl_space_copy(space));
7669 ls = isl_local_space_from_space(isl_space_domain(space));
7670 for (i = 0; i < n; ++i) {
7671 isl_val *v;
7672 isl_aff *aff;
7674 v = isl_multi_val_get_val(mv, i);
7675 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7676 ma = isl_multi_aff_set_aff(ma, i, aff);
7678 isl_local_space_free(ls);
7680 isl_multi_val_free(mv);
7681 return ma;
7682 error:
7683 isl_space_free(space);
7684 isl_multi_val_free(mv);
7685 return NULL;
7688 /* This is an alternative name for the function above.
7690 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7691 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7693 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7696 /* This function performs the same operation as
7697 * isl_multi_aff_multi_val_on_domain_space,
7698 * but is considered as a function on an isl_space when exported.
7700 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7701 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7703 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7706 /* Return a piecewise multi-affine expression
7707 * that is equal to "mv" on "domain".
7709 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7710 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7712 isl_space *space;
7713 isl_multi_aff *ma;
7715 space = isl_set_get_space(domain);
7716 ma = isl_multi_aff_multi_val_on_space(space, mv);
7718 return isl_pw_multi_aff_alloc(domain, ma);
7721 /* This function performs the same operation as
7722 * isl_pw_multi_aff_multi_val_on_domain,
7723 * but is considered as a function on an isl_set when exported.
7725 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7726 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7728 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7731 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7732 * mv is the value that should be attained on each domain set
7733 * res collects the results
7735 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7736 isl_multi_val *mv;
7737 isl_union_pw_multi_aff *res;
7740 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7741 * and add it to data->res.
7743 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7744 void *user)
7746 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7747 isl_pw_multi_aff *pma;
7748 isl_multi_val *mv;
7750 mv = isl_multi_val_copy(data->mv);
7751 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7752 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7754 return data->res ? isl_stat_ok : isl_stat_error;
7757 /* Return a union piecewise multi-affine expression
7758 * that is equal to "mv" on "domain".
7760 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7761 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7763 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7764 isl_space *space;
7766 space = isl_union_set_get_space(domain);
7767 data.res = isl_union_pw_multi_aff_empty(space);
7768 data.mv = mv;
7769 if (isl_union_set_foreach_set(domain,
7770 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7771 data.res = isl_union_pw_multi_aff_free(data.res);
7772 isl_union_set_free(domain);
7773 isl_multi_val_free(mv);
7774 return data.res;
7777 /* Compute the pullback of data->pma by the function represented by "pma2",
7778 * provided the spaces match, and add the results to data->res.
7780 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7782 struct isl_union_pw_multi_aff_bin_data *data = user;
7784 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7785 pma2->dim, isl_dim_out)) {
7786 isl_pw_multi_aff_free(pma2);
7787 return isl_stat_ok;
7790 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7791 isl_pw_multi_aff_copy(data->pma), pma2);
7793 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7794 if (!data->res)
7795 return isl_stat_error;
7797 return isl_stat_ok;
7800 /* Compute the pullback of "upma1" by the function represented by "upma2".
7802 __isl_give isl_union_pw_multi_aff *
7803 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7804 __isl_take isl_union_pw_multi_aff *upma1,
7805 __isl_take isl_union_pw_multi_aff *upma2)
7807 return bin_op(upma1, upma2, &pullback_entry);
7810 /* Apply "upma2" to "upma1".
7812 * That is, compute the pullback of "upma2" by "upma1".
7814 __isl_give isl_union_pw_multi_aff *
7815 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7816 __isl_take isl_union_pw_multi_aff *upma1,
7817 __isl_take isl_union_pw_multi_aff *upma2)
7819 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7822 #undef TYPE
7823 #define TYPE isl_pw_multi_aff
7824 static
7825 #include "isl_copy_tuple_id_templ.c"
7827 /* Given a function "pma1" of the form A[B -> C] -> D and
7828 * a function "pma2" of the form E -> B,
7829 * replace the domain of the wrapped relation inside the domain of "pma1"
7830 * by the preimage with respect to "pma2".
7831 * In other words, plug in "pma2" in this nested domain.
7832 * The result is of the form A[E -> C] -> D.
7834 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7835 * plug that into "pma1".
7837 __isl_give isl_pw_multi_aff *
7838 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7839 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
7841 isl_space *pma1_space, *pma2_space;
7842 isl_space *space;
7843 isl_pw_multi_aff *id;
7845 pma1_space = isl_pw_multi_aff_peek_space(pma1);
7846 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7848 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
7849 goto error;
7850 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
7851 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
7852 goto error;
7854 space = isl_space_domain(isl_space_copy(pma1_space));
7855 space = isl_space_range(isl_space_unwrap(space));
7856 id = isl_pw_multi_aff_identity_on_domain_space(space);
7857 pma2 = isl_pw_multi_aff_product(pma2, id);
7859 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
7860 pma1_space, isl_dim_in);
7861 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
7862 pma1_space, isl_dim_in);
7864 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
7865 error:
7866 isl_pw_multi_aff_free(pma1);
7867 isl_pw_multi_aff_free(pma2);
7868 return NULL;
7871 /* If data->pma and "pma2" are such that
7872 * data->pma is of the form A[B -> C] -> D and
7873 * "pma2" is of the form E -> B,
7874 * then replace the domain of the wrapped relation
7875 * inside the domain of data->pma by the preimage with respect to "pma2" and
7876 * add the result to data->res.
7878 static isl_stat preimage_domain_wrapped_domain_entry(
7879 __isl_take isl_pw_multi_aff *pma2, void *user)
7881 struct isl_union_pw_multi_aff_bin_data *data = user;
7882 isl_space *pma1_space, *pma2_space;
7883 isl_bool match;
7885 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
7886 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7888 match = isl_space_domain_is_wrapping(pma1_space);
7889 if (match >= 0 && match)
7890 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
7891 isl_dim_in, pma2_space, isl_dim_out);
7892 if (match < 0 || !match) {
7893 isl_pw_multi_aff_free(pma2);
7894 return match < 0 ? isl_stat_error : isl_stat_ok;
7897 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7898 isl_pw_multi_aff_copy(data->pma), pma2);
7900 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7902 return isl_stat_non_null(data->res);
7905 /* For each pair of functions A[B -> C] -> D in "upma1" and
7906 * E -> B in "upma2",
7907 * replace the domain of the wrapped relation inside the domain of the first
7908 * by the preimage with respect to the second and collect the results.
7909 * In other words, plug in the second function in this nested domain.
7910 * The results are of the form A[E -> C] -> D.
7912 __isl_give isl_union_pw_multi_aff *
7913 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
7914 __isl_take isl_union_pw_multi_aff *upma1,
7915 __isl_take isl_union_pw_multi_aff *upma2)
7917 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
7920 /* Check that the domain space of "upa" matches "space".
7922 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7923 * can in principle never fail since the space "space" is that
7924 * of the isl_multi_union_pw_aff and is a set space such that
7925 * there is no domain space to match.
7927 * We check the parameters and double-check that "space" is
7928 * indeed that of a set.
7930 static isl_stat isl_union_pw_aff_check_match_domain_space(
7931 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7933 isl_space *upa_space;
7934 isl_bool match;
7936 if (!upa || !space)
7937 return isl_stat_error;
7939 match = isl_space_is_set(space);
7940 if (match < 0)
7941 return isl_stat_error;
7942 if (!match)
7943 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7944 "expecting set space", return isl_stat_error);
7946 upa_space = isl_union_pw_aff_get_space(upa);
7947 match = isl_space_has_equal_params(space, upa_space);
7948 if (match < 0)
7949 goto error;
7950 if (!match)
7951 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7952 "parameters don't match", goto error);
7954 isl_space_free(upa_space);
7955 return isl_stat_ok;
7956 error:
7957 isl_space_free(upa_space);
7958 return isl_stat_error;
7961 /* Do the parameters of "upa" match those of "space"?
7963 static isl_bool isl_union_pw_aff_matching_params(
7964 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7966 isl_space *upa_space;
7967 isl_bool match;
7969 if (!upa || !space)
7970 return isl_bool_error;
7972 upa_space = isl_union_pw_aff_get_space(upa);
7974 match = isl_space_has_equal_params(space, upa_space);
7976 isl_space_free(upa_space);
7977 return match;
7980 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7981 * space represents the new parameters.
7982 * res collects the results.
7984 struct isl_union_pw_aff_reset_params_data {
7985 isl_space *space;
7986 isl_union_pw_aff *res;
7989 /* Replace the parameters of "pa" by data->space and
7990 * add the result to data->res.
7992 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7994 struct isl_union_pw_aff_reset_params_data *data = user;
7995 isl_space *space;
7997 space = isl_pw_aff_get_space(pa);
7998 space = isl_space_replace_params(space, data->space);
7999 pa = isl_pw_aff_reset_space(pa, space);
8000 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8002 return data->res ? isl_stat_ok : isl_stat_error;
8005 /* Replace the domain space of "upa" by "space".
8006 * Since a union expression does not have a (single) domain space,
8007 * "space" is necessarily a parameter space.
8009 * Since the order and the names of the parameters determine
8010 * the hash value, we need to create a new hash table.
8012 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8013 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8015 struct isl_union_pw_aff_reset_params_data data = { space };
8016 isl_bool match;
8018 match = isl_union_pw_aff_matching_params(upa, space);
8019 if (match < 0)
8020 upa = isl_union_pw_aff_free(upa);
8021 else if (match) {
8022 isl_space_free(space);
8023 return upa;
8026 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8027 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8028 data.res = isl_union_pw_aff_free(data.res);
8030 isl_union_pw_aff_free(upa);
8031 isl_space_free(space);
8032 return data.res;
8035 /* Return the floor of "pa".
8037 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8039 return isl_pw_aff_floor(pa);
8042 /* Given f, return floor(f).
8044 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8045 __isl_take isl_union_pw_aff *upa)
8047 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8050 /* Compute
8052 * upa mod m = upa - m * floor(upa/m)
8054 * with m an integer value.
8056 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8057 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8059 isl_union_pw_aff *res;
8061 if (!upa || !m)
8062 goto error;
8064 if (!isl_val_is_int(m))
8065 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8066 "expecting integer modulo", goto error);
8067 if (!isl_val_is_pos(m))
8068 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8069 "expecting positive modulo", goto error);
8071 res = isl_union_pw_aff_copy(upa);
8072 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8073 upa = isl_union_pw_aff_floor(upa);
8074 upa = isl_union_pw_aff_scale_val(upa, m);
8075 res = isl_union_pw_aff_sub(res, upa);
8077 return res;
8078 error:
8079 isl_val_free(m);
8080 isl_union_pw_aff_free(upa);
8081 return NULL;
8084 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8085 * pos is the output position that needs to be extracted.
8086 * res collects the results.
8088 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8089 int pos;
8090 isl_union_pw_aff *res;
8093 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8094 * (assuming it has such a dimension) and add it to data->res.
8096 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8098 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8099 isl_size n_out;
8100 isl_pw_aff *pa;
8102 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8103 if (n_out < 0)
8104 return isl_stat_error;
8105 if (data->pos >= n_out) {
8106 isl_pw_multi_aff_free(pma);
8107 return isl_stat_ok;
8110 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8111 isl_pw_multi_aff_free(pma);
8113 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8115 return data->res ? isl_stat_ok : isl_stat_error;
8118 /* Extract an isl_union_pw_aff corresponding to
8119 * output dimension "pos" of "upma".
8121 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8122 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8124 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8125 isl_space *space;
8127 if (!upma)
8128 return NULL;
8130 if (pos < 0)
8131 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8132 "cannot extract at negative position", return NULL);
8134 space = isl_union_pw_multi_aff_get_space(upma);
8135 data.res = isl_union_pw_aff_empty(space);
8136 data.pos = pos;
8137 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8138 &get_union_pw_aff, &data) < 0)
8139 data.res = isl_union_pw_aff_free(data.res);
8141 return data.res;
8144 /* Return a union piecewise affine expression
8145 * that is equal to "aff" on "domain".
8147 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8148 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8150 isl_pw_aff *pa;
8152 pa = isl_pw_aff_from_aff(aff);
8153 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8156 /* Return a union piecewise affine expression
8157 * that is equal to the parameter identified by "id" on "domain".
8159 * Make sure the parameter appears in the space passed to
8160 * isl_aff_param_on_domain_space_id.
8162 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8163 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8165 isl_space *space;
8166 isl_aff *aff;
8168 space = isl_union_set_get_space(domain);
8169 space = isl_space_add_param_id(space, isl_id_copy(id));
8170 aff = isl_aff_param_on_domain_space_id(space, id);
8171 return isl_union_pw_aff_aff_on_domain(domain, aff);
8174 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8175 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8176 * needs to attain.
8177 * "res" collects the results.
8179 struct isl_union_pw_aff_pw_aff_on_domain_data {
8180 isl_pw_aff *pa;
8181 isl_union_pw_aff *res;
8184 /* Construct a piecewise affine expression that is equal to data->pa
8185 * on "domain" and add the result to data->res.
8187 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8189 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8190 isl_pw_aff *pa;
8191 isl_size dim;
8193 pa = isl_pw_aff_copy(data->pa);
8194 dim = isl_set_dim(domain, isl_dim_set);
8195 if (dim < 0)
8196 pa = isl_pw_aff_free(pa);
8197 pa = isl_pw_aff_from_range(pa);
8198 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8199 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8200 pa = isl_pw_aff_intersect_domain(pa, domain);
8201 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8203 return data->res ? isl_stat_ok : isl_stat_error;
8206 /* Return a union piecewise affine expression
8207 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8208 * have been aligned.
8210 * Construct an isl_pw_aff on each of the sets in "domain" and
8211 * collect the results.
8213 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8214 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8216 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8217 isl_space *space;
8219 space = isl_union_set_get_space(domain);
8220 data.res = isl_union_pw_aff_empty(space);
8221 data.pa = pa;
8222 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8223 data.res = isl_union_pw_aff_free(data.res);
8224 isl_union_set_free(domain);
8225 isl_pw_aff_free(pa);
8226 return data.res;
8229 /* Return a union piecewise affine expression
8230 * that is equal to "pa" on "domain".
8232 * Check that "pa" is a parametric expression,
8233 * align the parameters if needed and call
8234 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8236 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8237 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8239 isl_bool is_set;
8240 isl_bool equal_params;
8241 isl_space *domain_space, *pa_space;
8243 pa_space = isl_pw_aff_peek_space(pa);
8244 is_set = isl_space_is_set(pa_space);
8245 if (is_set < 0)
8246 goto error;
8247 if (!is_set)
8248 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8249 "expecting parametric expression", goto error);
8251 domain_space = isl_union_set_get_space(domain);
8252 pa_space = isl_pw_aff_get_space(pa);
8253 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8254 if (equal_params >= 0 && !equal_params) {
8255 isl_space *space;
8257 space = isl_space_align_params(domain_space, pa_space);
8258 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8259 domain = isl_union_set_align_params(domain, space);
8260 } else {
8261 isl_space_free(domain_space);
8262 isl_space_free(pa_space);
8265 if (equal_params < 0)
8266 goto error;
8267 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8268 error:
8269 isl_union_set_free(domain);
8270 isl_pw_aff_free(pa);
8271 return NULL;
8274 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8275 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8276 * "res" collects the results.
8278 struct isl_union_pw_aff_val_on_domain_data {
8279 isl_val *v;
8280 isl_union_pw_aff *res;
8283 /* Construct a piecewise affine expression that is equal to data->v
8284 * on "domain" and add the result to data->res.
8286 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8288 struct isl_union_pw_aff_val_on_domain_data *data = user;
8289 isl_pw_aff *pa;
8290 isl_val *v;
8292 v = isl_val_copy(data->v);
8293 pa = isl_pw_aff_val_on_domain(domain, v);
8294 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8296 return data->res ? isl_stat_ok : isl_stat_error;
8299 /* Return a union piecewise affine expression
8300 * that is equal to "v" on "domain".
8302 * Construct an isl_pw_aff on each of the sets in "domain" and
8303 * collect the results.
8305 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8306 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8308 struct isl_union_pw_aff_val_on_domain_data data;
8309 isl_space *space;
8311 space = isl_union_set_get_space(domain);
8312 data.res = isl_union_pw_aff_empty(space);
8313 data.v = v;
8314 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8315 data.res = isl_union_pw_aff_free(data.res);
8316 isl_union_set_free(domain);
8317 isl_val_free(v);
8318 return data.res;
8321 /* Construct a piecewise multi affine expression
8322 * that is equal to "pa" and add it to upma.
8324 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8325 void *user)
8327 isl_union_pw_multi_aff **upma = user;
8328 isl_pw_multi_aff *pma;
8330 pma = isl_pw_multi_aff_from_pw_aff(pa);
8331 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8333 return *upma ? isl_stat_ok : isl_stat_error;
8336 /* Construct and return a union piecewise multi affine expression
8337 * that is equal to the given union piecewise affine expression.
8339 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8340 __isl_take isl_union_pw_aff *upa)
8342 isl_space *space;
8343 isl_union_pw_multi_aff *upma;
8345 if (!upa)
8346 return NULL;
8348 space = isl_union_pw_aff_get_space(upa);
8349 upma = isl_union_pw_multi_aff_empty(space);
8351 if (isl_union_pw_aff_foreach_pw_aff(upa,
8352 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8353 upma = isl_union_pw_multi_aff_free(upma);
8355 isl_union_pw_aff_free(upa);
8356 return upma;
8359 /* Compute the set of elements in the domain of "pa" where it is zero and
8360 * add this set to "uset".
8362 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8364 isl_union_set **uset = (isl_union_set **)user;
8366 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8368 return *uset ? isl_stat_ok : isl_stat_error;
8371 /* Return a union set containing those elements in the domain
8372 * of "upa" where it is zero.
8374 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8375 __isl_take isl_union_pw_aff *upa)
8377 isl_union_set *zero;
8379 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8380 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8381 zero = isl_union_set_free(zero);
8383 isl_union_pw_aff_free(upa);
8384 return zero;
8387 /* Internal data structure for isl_union_pw_aff_bind_id,
8388 * storing the parameter that needs to be bound and
8389 * the accumulated results.
8391 struct isl_bind_id_data {
8392 isl_id *id;
8393 isl_union_set *bound;
8396 /* Bind the piecewise affine function "pa" to the parameter data->id,
8397 * adding the resulting elements in the domain where the expression
8398 * is equal to the parameter to data->bound.
8400 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8402 struct isl_bind_id_data *data = user;
8403 isl_set *bound;
8405 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8406 data->bound = isl_union_set_add_set(data->bound, bound);
8408 return data->bound ? isl_stat_ok : isl_stat_error;
8411 /* Bind the union piecewise affine function "upa" to the parameter "id",
8412 * returning the elements in the domain where the expression
8413 * is equal to the parameter.
8415 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8416 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8418 struct isl_bind_id_data data = { id };
8420 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8421 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8422 data.bound = isl_union_set_free(data.bound);
8424 isl_union_pw_aff_free(upa);
8425 isl_id_free(id);
8426 return data.bound;
8429 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8430 * upma is the function that is plugged in.
8431 * pa is the current part of the function in which upma is plugged in.
8432 * res collects the results.
8434 struct isl_union_pw_aff_pullback_upma_data {
8435 isl_union_pw_multi_aff *upma;
8436 isl_pw_aff *pa;
8437 isl_union_pw_aff *res;
8440 /* Check if "pma" can be plugged into data->pa.
8441 * If so, perform the pullback and add the result to data->res.
8443 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8445 struct isl_union_pw_aff_pullback_upma_data *data = user;
8446 isl_pw_aff *pa;
8448 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8449 pma->dim, isl_dim_out)) {
8450 isl_pw_multi_aff_free(pma);
8451 return isl_stat_ok;
8454 pa = isl_pw_aff_copy(data->pa);
8455 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8457 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8459 return data->res ? isl_stat_ok : isl_stat_error;
8462 /* Check if any of the elements of data->upma can be plugged into pa,
8463 * add if so add the result to data->res.
8465 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8467 struct isl_union_pw_aff_pullback_upma_data *data = user;
8468 isl_stat r;
8470 data->pa = pa;
8471 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8472 &pa_pb_pma, data);
8473 isl_pw_aff_free(pa);
8475 return r;
8478 /* Compute the pullback of "upa" by the function represented by "upma".
8479 * In other words, plug in "upma" in "upa". The result contains
8480 * expressions defined over the domain space of "upma".
8482 * Run over all pairs of elements in "upa" and "upma", perform
8483 * the pullback when appropriate and collect the results.
8484 * If the hash value were based on the domain space rather than
8485 * the function space, then we could run through all elements
8486 * of "upma" and directly pick out the corresponding element of "upa".
8488 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8489 __isl_take isl_union_pw_aff *upa,
8490 __isl_take isl_union_pw_multi_aff *upma)
8492 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8493 isl_space *space;
8495 space = isl_union_pw_multi_aff_get_space(upma);
8496 upa = isl_union_pw_aff_align_params(upa, space);
8497 space = isl_union_pw_aff_get_space(upa);
8498 upma = isl_union_pw_multi_aff_align_params(upma, space);
8500 if (!upa || !upma)
8501 goto error;
8503 data.upma = upma;
8504 data.res = isl_union_pw_aff_alloc_same_size(upa);
8505 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8506 data.res = isl_union_pw_aff_free(data.res);
8508 isl_union_pw_aff_free(upa);
8509 isl_union_pw_multi_aff_free(upma);
8510 return data.res;
8511 error:
8512 isl_union_pw_aff_free(upa);
8513 isl_union_pw_multi_aff_free(upma);
8514 return NULL;
8517 #undef BASE
8518 #define BASE union_pw_aff
8519 #undef DOMBASE
8520 #define DOMBASE union_set
8522 #include <isl_multi_explicit_domain.c>
8523 #include <isl_multi_union_pw_aff_explicit_domain.c>
8524 #include <isl_multi_templ.c>
8525 #include <isl_multi_un_op_templ.c>
8526 #include <isl_multi_bin_val_templ.c>
8527 #include <isl_multi_apply_set.c>
8528 #include <isl_multi_apply_union_set.c>
8529 #include <isl_multi_arith_templ.c>
8530 #include <isl_multi_bind_templ.c>
8531 #include <isl_multi_coalesce.c>
8532 #include <isl_multi_dim_id_templ.c>
8533 #include <isl_multi_floor.c>
8534 #include <isl_multi_from_base_templ.c>
8535 #include <isl_multi_gist.c>
8536 #include <isl_multi_align_set.c>
8537 #include <isl_multi_align_union_set.c>
8538 #include <isl_multi_intersect.c>
8539 #include <isl_multi_nan_templ.c>
8540 #include <isl_multi_tuple_id_templ.c>
8541 #include <isl_multi_union_add_templ.c>
8542 #include <isl_multi_zero_space_templ.c>
8544 /* Does "mupa" have a non-trivial explicit domain?
8546 * The explicit domain, if present, is trivial if it represents
8547 * an (obviously) universe parameter set.
8549 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8550 __isl_keep isl_multi_union_pw_aff *mupa)
8552 isl_bool is_params, trivial;
8553 isl_set *set;
8555 if (!mupa)
8556 return isl_bool_error;
8557 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8558 return isl_bool_false;
8559 is_params = isl_union_set_is_params(mupa->u.dom);
8560 if (is_params < 0 || !is_params)
8561 return isl_bool_not(is_params);
8562 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8563 trivial = isl_set_plain_is_universe(set);
8564 isl_set_free(set);
8565 return isl_bool_not(trivial);
8568 /* Construct a multiple union piecewise affine expression
8569 * in the given space with value zero in each of the output dimensions.
8571 * Since there is no canonical zero value for
8572 * a union piecewise affine expression, we can only construct
8573 * a zero-dimensional "zero" value.
8575 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8576 __isl_take isl_space *space)
8578 isl_bool params;
8579 isl_size dim;
8581 if (!space)
8582 return NULL;
8584 params = isl_space_is_params(space);
8585 if (params < 0)
8586 goto error;
8587 if (params)
8588 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8589 "expecting proper set space", goto error);
8590 if (!isl_space_is_set(space))
8591 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8592 "expecting set space", goto error);
8593 dim = isl_space_dim(space, isl_dim_out);
8594 if (dim < 0)
8595 goto error;
8596 if (dim != 0)
8597 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8598 "expecting 0D space", goto error);
8600 return isl_multi_union_pw_aff_alloc(space);
8601 error:
8602 isl_space_free(space);
8603 return NULL;
8606 /* Construct and return a multi union piecewise affine expression
8607 * that is equal to the given multi affine expression.
8609 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8610 __isl_take isl_multi_aff *ma)
8612 isl_multi_pw_aff *mpa;
8614 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8615 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8618 /* This function performs the same operation as
8619 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8620 * isl_multi_aff when exported.
8622 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8623 __isl_take isl_multi_aff *ma)
8625 return isl_multi_union_pw_aff_from_multi_aff(ma);
8628 /* Construct and return a multi union piecewise affine expression
8629 * that is equal to the given multi piecewise affine expression.
8631 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8632 __isl_take isl_multi_pw_aff *mpa)
8634 int i;
8635 isl_size n;
8636 isl_space *space;
8637 isl_multi_union_pw_aff *mupa;
8639 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8640 if (n < 0)
8641 mpa = isl_multi_pw_aff_free(mpa);
8642 if (!mpa)
8643 return NULL;
8645 space = isl_multi_pw_aff_get_space(mpa);
8646 space = isl_space_range(space);
8647 mupa = isl_multi_union_pw_aff_alloc(space);
8649 for (i = 0; i < n; ++i) {
8650 isl_pw_aff *pa;
8651 isl_union_pw_aff *upa;
8653 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8654 upa = isl_union_pw_aff_from_pw_aff(pa);
8655 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8658 isl_multi_pw_aff_free(mpa);
8660 return mupa;
8663 /* Extract the range space of "pma" and assign it to *space.
8664 * If *space has already been set (through a previous call to this function),
8665 * then check that the range space is the same.
8667 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8669 isl_space **space = user;
8670 isl_space *pma_space;
8671 isl_bool equal;
8673 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8674 isl_pw_multi_aff_free(pma);
8676 if (!pma_space)
8677 return isl_stat_error;
8678 if (!*space) {
8679 *space = pma_space;
8680 return isl_stat_ok;
8683 equal = isl_space_is_equal(pma_space, *space);
8684 isl_space_free(pma_space);
8686 if (equal < 0)
8687 return isl_stat_error;
8688 if (!equal)
8689 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8690 "range spaces not the same", return isl_stat_error);
8691 return isl_stat_ok;
8694 /* Construct and return a multi union piecewise affine expression
8695 * that is equal to the given union piecewise multi affine expression.
8697 * In order to be able to perform the conversion, the input
8698 * needs to be non-empty and may only involve a single range space.
8700 * If the resulting multi union piecewise affine expression has
8701 * an explicit domain, then assign it the domain of the input.
8702 * In other cases, the domain is stored in the individual elements.
8704 __isl_give isl_multi_union_pw_aff *
8705 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8706 __isl_take isl_union_pw_multi_aff *upma)
8708 isl_space *space = NULL;
8709 isl_multi_union_pw_aff *mupa;
8710 int i;
8711 isl_size n;
8713 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8714 if (n < 0)
8715 goto error;
8716 if (n == 0)
8717 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8718 "cannot extract range space from empty input",
8719 goto error);
8720 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8721 &space) < 0)
8722 goto error;
8724 if (!space)
8725 goto error;
8727 n = isl_space_dim(space, isl_dim_set);
8728 if (n < 0)
8729 space = isl_space_free(space);
8730 mupa = isl_multi_union_pw_aff_alloc(space);
8732 for (i = 0; i < n; ++i) {
8733 isl_union_pw_aff *upa;
8735 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8736 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8738 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8739 isl_union_set *dom;
8740 isl_union_pw_multi_aff *copy;
8742 copy = isl_union_pw_multi_aff_copy(upma);
8743 dom = isl_union_pw_multi_aff_domain(copy);
8744 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8747 isl_union_pw_multi_aff_free(upma);
8748 return mupa;
8749 error:
8750 isl_space_free(space);
8751 isl_union_pw_multi_aff_free(upma);
8752 return NULL;
8755 /* This function performs the same operation as
8756 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8757 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8759 __isl_give isl_multi_union_pw_aff *
8760 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8761 __isl_take isl_union_pw_multi_aff *upma)
8763 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8766 /* Try and create an isl_multi_union_pw_aff that is equivalent
8767 * to the given isl_union_map.
8768 * The isl_union_map is required to be single-valued in each space.
8769 * Moreover, it cannot be empty and all range spaces need to be the same.
8770 * Otherwise, an error is produced.
8772 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8773 __isl_take isl_union_map *umap)
8775 isl_union_pw_multi_aff *upma;
8777 upma = isl_union_pw_multi_aff_from_union_map(umap);
8778 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8781 /* This function performs the same operation as
8782 * isl_multi_union_pw_aff_from_union_map,
8783 * but is considered as a function on an isl_union_map when exported.
8785 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8786 __isl_take isl_union_map *umap)
8788 return isl_multi_union_pw_aff_from_union_map(umap);
8791 /* Return a multiple union piecewise affine expression
8792 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8793 * have been aligned.
8795 * If the resulting multi union piecewise affine expression has
8796 * an explicit domain, then assign it the input domain.
8797 * In other cases, the domain is stored in the individual elements.
8799 static __isl_give isl_multi_union_pw_aff *
8800 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8801 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8803 int i;
8804 isl_size n;
8805 isl_space *space;
8806 isl_multi_union_pw_aff *mupa;
8808 n = isl_multi_val_dim(mv, isl_dim_set);
8809 if (!domain || n < 0)
8810 goto error;
8812 space = isl_multi_val_get_space(mv);
8813 mupa = isl_multi_union_pw_aff_alloc(space);
8814 for (i = 0; i < n; ++i) {
8815 isl_val *v;
8816 isl_union_pw_aff *upa;
8818 v = isl_multi_val_get_val(mv, i);
8819 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8821 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8823 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8824 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8825 isl_union_set_copy(domain));
8827 isl_union_set_free(domain);
8828 isl_multi_val_free(mv);
8829 return mupa;
8830 error:
8831 isl_union_set_free(domain);
8832 isl_multi_val_free(mv);
8833 return NULL;
8836 /* Return a multiple union piecewise affine expression
8837 * that is equal to "mv" on "domain".
8839 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8840 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8842 isl_bool equal_params;
8844 if (!domain || !mv)
8845 goto error;
8846 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8847 if (equal_params < 0)
8848 goto error;
8849 if (equal_params)
8850 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8851 domain, mv);
8852 domain = isl_union_set_align_params(domain,
8853 isl_multi_val_get_space(mv));
8854 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8855 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8856 error:
8857 isl_union_set_free(domain);
8858 isl_multi_val_free(mv);
8859 return NULL;
8862 /* Return a multiple union piecewise affine expression
8863 * that is equal to "ma" on "domain".
8865 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8866 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8868 isl_pw_multi_aff *pma;
8870 pma = isl_pw_multi_aff_from_multi_aff(ma);
8871 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8874 /* Return a multiple union piecewise affine expression
8875 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8876 * have been aligned.
8878 * If the resulting multi union piecewise affine expression has
8879 * an explicit domain, then assign it the input domain.
8880 * In other cases, the domain is stored in the individual elements.
8882 static __isl_give isl_multi_union_pw_aff *
8883 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8884 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8886 int i;
8887 isl_size n;
8888 isl_space *space;
8889 isl_multi_union_pw_aff *mupa;
8891 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8892 if (!domain || n < 0)
8893 goto error;
8894 space = isl_pw_multi_aff_get_space(pma);
8895 mupa = isl_multi_union_pw_aff_alloc(space);
8896 for (i = 0; i < n; ++i) {
8897 isl_pw_aff *pa;
8898 isl_union_pw_aff *upa;
8900 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8901 upa = isl_union_pw_aff_pw_aff_on_domain(
8902 isl_union_set_copy(domain), pa);
8903 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8905 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8906 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8907 isl_union_set_copy(domain));
8909 isl_union_set_free(domain);
8910 isl_pw_multi_aff_free(pma);
8911 return mupa;
8912 error:
8913 isl_union_set_free(domain);
8914 isl_pw_multi_aff_free(pma);
8915 return NULL;
8918 /* Return a multiple union piecewise affine expression
8919 * that is equal to "pma" on "domain".
8921 __isl_give isl_multi_union_pw_aff *
8922 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8923 __isl_take isl_pw_multi_aff *pma)
8925 isl_bool equal_params;
8926 isl_space *space;
8928 space = isl_pw_multi_aff_peek_space(pma);
8929 equal_params = isl_union_set_space_has_equal_params(domain, space);
8930 if (equal_params < 0)
8931 goto error;
8932 if (equal_params)
8933 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8934 domain, pma);
8935 domain = isl_union_set_align_params(domain,
8936 isl_pw_multi_aff_get_space(pma));
8937 pma = isl_pw_multi_aff_align_params(pma,
8938 isl_union_set_get_space(domain));
8939 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8940 pma);
8941 error:
8942 isl_union_set_free(domain);
8943 isl_pw_multi_aff_free(pma);
8944 return NULL;
8947 /* Return a union set containing those elements in the domains
8948 * of the elements of "mupa" where they are all zero.
8950 * If there are no elements, then simply return the entire domain.
8952 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8953 __isl_take isl_multi_union_pw_aff *mupa)
8955 int i;
8956 isl_size n;
8957 isl_union_pw_aff *upa;
8958 isl_union_set *zero;
8960 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8961 if (n < 0)
8962 mupa = isl_multi_union_pw_aff_free(mupa);
8963 if (!mupa)
8964 return NULL;
8966 if (n == 0)
8967 return isl_multi_union_pw_aff_domain(mupa);
8969 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8970 zero = isl_union_pw_aff_zero_union_set(upa);
8972 for (i = 1; i < n; ++i) {
8973 isl_union_set *zero_i;
8975 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8976 zero_i = isl_union_pw_aff_zero_union_set(upa);
8978 zero = isl_union_set_intersect(zero, zero_i);
8981 isl_multi_union_pw_aff_free(mupa);
8982 return zero;
8985 /* Construct a union map mapping the shared domain
8986 * of the union piecewise affine expressions to the range of "mupa"
8987 * in the special case of a 0D multi union piecewise affine expression.
8989 * Construct a map between the explicit domain of "mupa" and
8990 * the range space.
8991 * Note that this assumes that the domain consists of explicit elements.
8993 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8994 __isl_take isl_multi_union_pw_aff *mupa)
8996 isl_bool is_params;
8997 isl_space *space;
8998 isl_union_set *dom, *ran;
9000 space = isl_multi_union_pw_aff_get_space(mupa);
9001 dom = isl_multi_union_pw_aff_domain(mupa);
9002 ran = isl_union_set_from_set(isl_set_universe(space));
9004 is_params = isl_union_set_is_params(dom);
9005 if (is_params < 0)
9006 dom = isl_union_set_free(dom);
9007 else if (is_params)
9008 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9009 "cannot create union map from expression without "
9010 "explicit domain elements",
9011 dom = isl_union_set_free(dom));
9013 return isl_union_map_from_domain_and_range(dom, ran);
9016 /* Construct a union map mapping the shared domain
9017 * of the union piecewise affine expressions to the range of "mupa"
9018 * with each dimension in the range equated to the
9019 * corresponding union piecewise affine expression.
9021 * If the input is zero-dimensional, then construct a mapping
9022 * from its explicit domain.
9024 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9025 __isl_take isl_multi_union_pw_aff *mupa)
9027 int i;
9028 isl_size n;
9029 isl_space *space;
9030 isl_union_map *umap;
9031 isl_union_pw_aff *upa;
9033 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9034 if (n < 0)
9035 mupa = isl_multi_union_pw_aff_free(mupa);
9036 if (!mupa)
9037 return NULL;
9039 if (n == 0)
9040 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9042 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9043 umap = isl_union_map_from_union_pw_aff(upa);
9045 for (i = 1; i < n; ++i) {
9046 isl_union_map *umap_i;
9048 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9049 umap_i = isl_union_map_from_union_pw_aff(upa);
9050 umap = isl_union_map_flat_range_product(umap, umap_i);
9053 space = isl_multi_union_pw_aff_get_space(mupa);
9054 umap = isl_union_map_reset_range_space(umap, space);
9056 isl_multi_union_pw_aff_free(mupa);
9057 return umap;
9060 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9061 * "range" is the space from which to set the range space.
9062 * "res" collects the results.
9064 struct isl_union_pw_multi_aff_reset_range_space_data {
9065 isl_space *range;
9066 isl_union_pw_multi_aff *res;
9069 /* Replace the range space of "pma" by the range space of data->range and
9070 * add the result to data->res.
9072 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9074 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9075 isl_space *space;
9077 space = isl_pw_multi_aff_get_space(pma);
9078 space = isl_space_domain(space);
9079 space = isl_space_extend_domain_with_range(space,
9080 isl_space_copy(data->range));
9081 pma = isl_pw_multi_aff_reset_space(pma, space);
9082 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9084 return data->res ? isl_stat_ok : isl_stat_error;
9087 /* Replace the range space of all the piecewise affine expressions in "upma" by
9088 * the range space of "space".
9090 * This assumes that all these expressions have the same output dimension.
9092 * Since the spaces of the expressions change, so do their hash values.
9093 * We therefore need to create a new isl_union_pw_multi_aff.
9094 * Note that the hash value is currently computed based on the entire
9095 * space even though there can only be a single expression with a given
9096 * domain space.
9098 static __isl_give isl_union_pw_multi_aff *
9099 isl_union_pw_multi_aff_reset_range_space(
9100 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9102 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9103 isl_space *space_upma;
9105 space_upma = isl_union_pw_multi_aff_get_space(upma);
9106 data.res = isl_union_pw_multi_aff_empty(space_upma);
9107 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9108 &reset_range_space, &data) < 0)
9109 data.res = isl_union_pw_multi_aff_free(data.res);
9111 isl_space_free(space);
9112 isl_union_pw_multi_aff_free(upma);
9113 return data.res;
9116 /* Construct and return a union piecewise multi affine expression
9117 * that is equal to the given multi union piecewise affine expression,
9118 * in the special case of a 0D multi union piecewise affine expression.
9120 * Construct a union piecewise multi affine expression
9121 * on top of the explicit domain of the input.
9123 __isl_give isl_union_pw_multi_aff *
9124 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9125 __isl_take isl_multi_union_pw_aff *mupa)
9127 isl_space *space;
9128 isl_multi_val *mv;
9129 isl_union_set *domain;
9131 space = isl_multi_union_pw_aff_get_space(mupa);
9132 mv = isl_multi_val_zero(space);
9133 domain = isl_multi_union_pw_aff_domain(mupa);
9134 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9137 /* Construct and return a union piecewise multi affine expression
9138 * that is equal to the given multi union piecewise affine expression.
9140 * If the input is zero-dimensional, then
9141 * construct a union piecewise multi affine expression
9142 * on top of the explicit domain of the input.
9144 __isl_give isl_union_pw_multi_aff *
9145 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9146 __isl_take isl_multi_union_pw_aff *mupa)
9148 int i;
9149 isl_size n;
9150 isl_space *space;
9151 isl_union_pw_multi_aff *upma;
9152 isl_union_pw_aff *upa;
9154 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9155 if (n < 0)
9156 mupa = isl_multi_union_pw_aff_free(mupa);
9157 if (!mupa)
9158 return NULL;
9160 if (n == 0)
9161 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9163 space = isl_multi_union_pw_aff_get_space(mupa);
9164 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9165 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9167 for (i = 1; i < n; ++i) {
9168 isl_union_pw_multi_aff *upma_i;
9170 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9171 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9172 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9175 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9177 isl_multi_union_pw_aff_free(mupa);
9178 return upma;
9181 /* Intersect the range of "mupa" with "range",
9182 * in the special case where "mupa" is 0D.
9184 * Intersect the domain of "mupa" with the constraints on the parameters
9185 * of "range".
9187 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9188 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9190 range = isl_set_params(range);
9191 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9192 return mupa;
9195 /* Intersect the range of "mupa" with "range".
9196 * That is, keep only those domain elements that have a function value
9197 * in "range".
9199 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9200 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9202 isl_union_pw_multi_aff *upma;
9203 isl_union_set *domain;
9204 isl_space *space;
9205 isl_size n;
9206 int match;
9208 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9209 if (n < 0 || !range)
9210 goto error;
9212 space = isl_set_get_space(range);
9213 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9214 space, isl_dim_set);
9215 isl_space_free(space);
9216 if (match < 0)
9217 goto error;
9218 if (!match)
9219 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9220 "space don't match", goto error);
9221 if (n == 0)
9222 return mupa_intersect_range_0D(mupa, range);
9224 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9225 isl_multi_union_pw_aff_copy(mupa));
9226 domain = isl_union_set_from_set(range);
9227 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9228 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9230 return mupa;
9231 error:
9232 isl_multi_union_pw_aff_free(mupa);
9233 isl_set_free(range);
9234 return NULL;
9237 /* Return the shared domain of the elements of "mupa",
9238 * in the special case where "mupa" is zero-dimensional.
9240 * Return the explicit domain of "mupa".
9241 * Note that this domain may be a parameter set, either
9242 * because "mupa" is meant to live in a set space or
9243 * because no explicit domain has been set.
9245 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9246 __isl_take isl_multi_union_pw_aff *mupa)
9248 isl_union_set *dom;
9250 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9251 isl_multi_union_pw_aff_free(mupa);
9253 return dom;
9256 /* Return the shared domain of the elements of "mupa".
9258 * If "mupa" is zero-dimensional, then return its explicit domain.
9260 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9261 __isl_take isl_multi_union_pw_aff *mupa)
9263 int i;
9264 isl_size n;
9265 isl_union_pw_aff *upa;
9266 isl_union_set *dom;
9268 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9269 if (n < 0)
9270 mupa = isl_multi_union_pw_aff_free(mupa);
9271 if (!mupa)
9272 return NULL;
9274 if (n == 0)
9275 return isl_multi_union_pw_aff_domain_0D(mupa);
9277 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9278 dom = isl_union_pw_aff_domain(upa);
9279 for (i = 1; i < n; ++i) {
9280 isl_union_set *dom_i;
9282 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9283 dom_i = isl_union_pw_aff_domain(upa);
9284 dom = isl_union_set_intersect(dom, dom_i);
9287 isl_multi_union_pw_aff_free(mupa);
9288 return dom;
9291 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9292 * In particular, the spaces have been aligned.
9293 * The result is defined over the shared domain of the elements of "mupa"
9295 * We first extract the parametric constant part of "aff" and
9296 * define that over the shared domain.
9297 * Then we iterate over all input dimensions of "aff" and add the corresponding
9298 * multiples of the elements of "mupa".
9299 * Finally, we consider the integer divisions, calling the function
9300 * recursively to obtain an isl_union_pw_aff corresponding to the
9301 * integer division argument.
9303 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9304 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9306 int i;
9307 isl_size n_in, n_div;
9308 isl_union_pw_aff *upa;
9309 isl_union_set *uset;
9310 isl_val *v;
9311 isl_aff *cst;
9313 n_in = isl_aff_dim(aff, isl_dim_in);
9314 n_div = isl_aff_dim(aff, isl_dim_div);
9315 if (n_in < 0 || n_div < 0)
9316 goto error;
9318 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9319 cst = isl_aff_copy(aff);
9320 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9321 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9322 cst = isl_aff_project_domain_on_params(cst);
9323 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9325 for (i = 0; i < n_in; ++i) {
9326 isl_union_pw_aff *upa_i;
9328 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9329 continue;
9330 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9331 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9332 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9333 upa = isl_union_pw_aff_add(upa, upa_i);
9336 for (i = 0; i < n_div; ++i) {
9337 isl_aff *div;
9338 isl_union_pw_aff *upa_i;
9340 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9341 continue;
9342 div = isl_aff_get_div(aff, i);
9343 upa_i = multi_union_pw_aff_apply_aff(
9344 isl_multi_union_pw_aff_copy(mupa), div);
9345 upa_i = isl_union_pw_aff_floor(upa_i);
9346 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9347 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9348 upa = isl_union_pw_aff_add(upa, upa_i);
9351 isl_multi_union_pw_aff_free(mupa);
9352 isl_aff_free(aff);
9354 return upa;
9355 error:
9356 isl_multi_union_pw_aff_free(mupa);
9357 isl_aff_free(aff);
9358 return NULL;
9361 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9362 * with the domain of "aff".
9363 * Furthermore, the dimension of this space needs to be greater than zero.
9364 * The result is defined over the shared domain of the elements of "mupa"
9366 * We perform these checks and then hand over control to
9367 * multi_union_pw_aff_apply_aff.
9369 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9370 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9372 isl_size dim;
9373 isl_space *space1, *space2;
9374 isl_bool equal;
9376 mupa = isl_multi_union_pw_aff_align_params(mupa,
9377 isl_aff_get_space(aff));
9378 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9379 if (!mupa || !aff)
9380 goto error;
9382 space1 = isl_multi_union_pw_aff_get_space(mupa);
9383 space2 = isl_aff_get_domain_space(aff);
9384 equal = isl_space_is_equal(space1, space2);
9385 isl_space_free(space1);
9386 isl_space_free(space2);
9387 if (equal < 0)
9388 goto error;
9389 if (!equal)
9390 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9391 "spaces don't match", goto error);
9392 dim = isl_aff_dim(aff, isl_dim_in);
9393 if (dim < 0)
9394 goto error;
9395 if (dim == 0)
9396 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9397 "cannot determine domains", goto error);
9399 return multi_union_pw_aff_apply_aff(mupa, aff);
9400 error:
9401 isl_multi_union_pw_aff_free(mupa);
9402 isl_aff_free(aff);
9403 return NULL;
9406 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9407 * The space of "mupa" is known to be compatible with the domain of "ma".
9409 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9410 * on the domain of "mupa".
9412 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9413 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9415 isl_union_set *dom;
9417 dom = isl_multi_union_pw_aff_domain(mupa);
9418 ma = isl_multi_aff_project_domain_on_params(ma);
9420 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9423 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9424 * with the domain of "ma".
9425 * The result is defined over the shared domain of the elements of "mupa"
9427 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9428 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9430 isl_space *space1, *space2;
9431 isl_multi_union_pw_aff *res;
9432 isl_bool equal;
9433 int i;
9434 isl_size n_in, n_out;
9436 mupa = isl_multi_union_pw_aff_align_params(mupa,
9437 isl_multi_aff_get_space(ma));
9438 ma = isl_multi_aff_align_params(ma,
9439 isl_multi_union_pw_aff_get_space(mupa));
9440 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9441 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9442 if (!mupa || n_in < 0 || n_out < 0)
9443 goto error;
9445 space1 = isl_multi_union_pw_aff_get_space(mupa);
9446 space2 = isl_multi_aff_get_domain_space(ma);
9447 equal = isl_space_is_equal(space1, space2);
9448 isl_space_free(space1);
9449 isl_space_free(space2);
9450 if (equal < 0)
9451 goto error;
9452 if (!equal)
9453 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9454 "spaces don't match", goto error);
9455 if (n_in == 0)
9456 return mupa_apply_multi_aff_0D(mupa, ma);
9458 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9459 res = isl_multi_union_pw_aff_alloc(space1);
9461 for (i = 0; i < n_out; ++i) {
9462 isl_aff *aff;
9463 isl_union_pw_aff *upa;
9465 aff = isl_multi_aff_get_aff(ma, i);
9466 upa = multi_union_pw_aff_apply_aff(
9467 isl_multi_union_pw_aff_copy(mupa), aff);
9468 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9471 isl_multi_aff_free(ma);
9472 isl_multi_union_pw_aff_free(mupa);
9473 return res;
9474 error:
9475 isl_multi_union_pw_aff_free(mupa);
9476 isl_multi_aff_free(ma);
9477 return NULL;
9480 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9481 * The space of "mupa" is known to be compatible with the domain of "pa".
9483 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9484 * on the domain of "mupa".
9486 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9487 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9489 isl_union_set *dom;
9491 dom = isl_multi_union_pw_aff_domain(mupa);
9492 pa = isl_pw_aff_project_domain_on_params(pa);
9494 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9497 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9498 * with the domain of "pa".
9499 * Furthermore, the dimension of this space needs to be greater than zero.
9500 * The result is defined over the shared domain of the elements of "mupa"
9502 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9503 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9505 int i;
9506 isl_bool equal;
9507 isl_size n_in;
9508 isl_space *space, *space2;
9509 isl_union_pw_aff *upa;
9511 mupa = isl_multi_union_pw_aff_align_params(mupa,
9512 isl_pw_aff_get_space(pa));
9513 pa = isl_pw_aff_align_params(pa,
9514 isl_multi_union_pw_aff_get_space(mupa));
9515 if (!mupa || !pa)
9516 goto error;
9518 space = isl_multi_union_pw_aff_get_space(mupa);
9519 space2 = isl_pw_aff_get_domain_space(pa);
9520 equal = isl_space_is_equal(space, space2);
9521 isl_space_free(space);
9522 isl_space_free(space2);
9523 if (equal < 0)
9524 goto error;
9525 if (!equal)
9526 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9527 "spaces don't match", goto error);
9528 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9529 if (n_in < 0)
9530 goto error;
9531 if (n_in == 0)
9532 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9534 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9535 upa = isl_union_pw_aff_empty(space);
9537 for (i = 0; i < pa->n; ++i) {
9538 isl_aff *aff;
9539 isl_set *domain;
9540 isl_multi_union_pw_aff *mupa_i;
9541 isl_union_pw_aff *upa_i;
9543 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9544 domain = isl_set_copy(pa->p[i].set);
9545 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9546 aff = isl_aff_copy(pa->p[i].aff);
9547 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9548 upa = isl_union_pw_aff_union_add(upa, upa_i);
9551 isl_multi_union_pw_aff_free(mupa);
9552 isl_pw_aff_free(pa);
9553 return upa;
9554 error:
9555 isl_multi_union_pw_aff_free(mupa);
9556 isl_pw_aff_free(pa);
9557 return NULL;
9560 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9561 * The space of "mupa" is known to be compatible with the domain of "pma".
9563 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9564 * on the domain of "mupa".
9566 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9567 __isl_take isl_multi_union_pw_aff *mupa,
9568 __isl_take isl_pw_multi_aff *pma)
9570 isl_union_set *dom;
9572 dom = isl_multi_union_pw_aff_domain(mupa);
9573 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9575 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9578 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9579 * with the domain of "pma".
9580 * The result is defined over the shared domain of the elements of "mupa"
9582 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9583 __isl_take isl_multi_union_pw_aff *mupa,
9584 __isl_take isl_pw_multi_aff *pma)
9586 isl_space *space1, *space2;
9587 isl_multi_union_pw_aff *res;
9588 isl_bool equal;
9589 int i;
9590 isl_size n_in, n_out;
9592 mupa = isl_multi_union_pw_aff_align_params(mupa,
9593 isl_pw_multi_aff_get_space(pma));
9594 pma = isl_pw_multi_aff_align_params(pma,
9595 isl_multi_union_pw_aff_get_space(mupa));
9596 if (!mupa || !pma)
9597 goto error;
9599 space1 = isl_multi_union_pw_aff_get_space(mupa);
9600 space2 = isl_pw_multi_aff_get_domain_space(pma);
9601 equal = isl_space_is_equal(space1, space2);
9602 isl_space_free(space1);
9603 isl_space_free(space2);
9604 if (equal < 0)
9605 goto error;
9606 if (!equal)
9607 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9608 "spaces don't match", goto error);
9609 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9610 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9611 if (n_in < 0 || n_out < 0)
9612 goto error;
9613 if (n_in == 0)
9614 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9616 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9617 res = isl_multi_union_pw_aff_alloc(space1);
9619 for (i = 0; i < n_out; ++i) {
9620 isl_pw_aff *pa;
9621 isl_union_pw_aff *upa;
9623 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9624 upa = isl_multi_union_pw_aff_apply_pw_aff(
9625 isl_multi_union_pw_aff_copy(mupa), pa);
9626 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9629 isl_pw_multi_aff_free(pma);
9630 isl_multi_union_pw_aff_free(mupa);
9631 return res;
9632 error:
9633 isl_multi_union_pw_aff_free(mupa);
9634 isl_pw_multi_aff_free(pma);
9635 return NULL;
9638 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9639 * If the explicit domain only keeps track of constraints on the parameters,
9640 * then only update those constraints.
9642 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9643 __isl_take isl_multi_union_pw_aff *mupa,
9644 __isl_keep isl_union_pw_multi_aff *upma)
9646 isl_bool is_params;
9648 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9649 return isl_multi_union_pw_aff_free(mupa);
9651 mupa = isl_multi_union_pw_aff_cow(mupa);
9652 if (!mupa)
9653 return NULL;
9655 is_params = isl_union_set_is_params(mupa->u.dom);
9656 if (is_params < 0)
9657 return isl_multi_union_pw_aff_free(mupa);
9659 upma = isl_union_pw_multi_aff_copy(upma);
9660 if (is_params)
9661 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9662 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9663 else
9664 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9665 mupa->u.dom, upma);
9666 if (!mupa->u.dom)
9667 return isl_multi_union_pw_aff_free(mupa);
9668 return mupa;
9671 /* Compute the pullback of "mupa" by the function represented by "upma".
9672 * In other words, plug in "upma" in "mupa". The result contains
9673 * expressions defined over the domain space of "upma".
9675 * Run over all elements of "mupa" and plug in "upma" in each of them.
9677 * If "mupa" has an explicit domain, then it is this domain
9678 * that needs to undergo a pullback instead, i.e., a preimage.
9680 __isl_give isl_multi_union_pw_aff *
9681 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9682 __isl_take isl_multi_union_pw_aff *mupa,
9683 __isl_take isl_union_pw_multi_aff *upma)
9685 int i;
9686 isl_size n;
9688 mupa = isl_multi_union_pw_aff_align_params(mupa,
9689 isl_union_pw_multi_aff_get_space(upma));
9690 upma = isl_union_pw_multi_aff_align_params(upma,
9691 isl_multi_union_pw_aff_get_space(mupa));
9692 mupa = isl_multi_union_pw_aff_cow(mupa);
9693 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9694 if (n < 0 || !upma)
9695 goto error;
9697 for (i = 0; i < n; ++i) {
9698 isl_union_pw_aff *upa;
9700 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9701 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9702 isl_union_pw_multi_aff_copy(upma));
9703 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9706 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9707 mupa = preimage_explicit_domain(mupa, upma);
9709 isl_union_pw_multi_aff_free(upma);
9710 return mupa;
9711 error:
9712 isl_multi_union_pw_aff_free(mupa);
9713 isl_union_pw_multi_aff_free(upma);
9714 return NULL;
9717 /* Extract the sequence of elements in "mupa" with domain space "space"
9718 * (ignoring parameters).
9720 * For the elements of "mupa" that are not defined on the specified space,
9721 * the corresponding element in the result is empty.
9723 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9724 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9726 int i;
9727 isl_size n;
9728 isl_space *space_mpa;
9729 isl_multi_pw_aff *mpa;
9731 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9732 if (n < 0 || !space)
9733 goto error;
9735 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9736 space = isl_space_replace_params(space, space_mpa);
9737 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9738 space_mpa);
9739 mpa = isl_multi_pw_aff_alloc(space_mpa);
9741 space = isl_space_from_domain(space);
9742 space = isl_space_add_dims(space, isl_dim_out, 1);
9743 for (i = 0; i < n; ++i) {
9744 isl_union_pw_aff *upa;
9745 isl_pw_aff *pa;
9747 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9748 pa = isl_union_pw_aff_extract_pw_aff(upa,
9749 isl_space_copy(space));
9750 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9751 isl_union_pw_aff_free(upa);
9754 isl_space_free(space);
9755 return mpa;
9756 error:
9757 isl_space_free(space);
9758 return NULL;
9761 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9762 * should modify the base expressions in the input.
9764 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9765 * are taken into account.
9766 * "fn" is applied to each entry in the input.
9768 struct isl_union_pw_multi_aff_un_op_control {
9769 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9770 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9773 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9774 * a second argument) for use as an isl_union_pw_multi_aff_transform
9775 * filter function (which does take a second argument).
9776 * Simply call control->filter without the second argument.
9778 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9779 __isl_take isl_pw_multi_aff *pma, void *user)
9781 struct isl_union_pw_multi_aff_un_op_control *control = user;
9783 return control->filter(pma);
9786 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9787 * a second argument) for use as an isl_union_pw_multi_aff_transform
9788 * base function (which does take a second argument).
9789 * Simply call control->fn without the second argument.
9791 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9792 __isl_take isl_pw_multi_aff *pma, void *user)
9794 struct isl_union_pw_multi_aff_un_op_control *control = user;
9796 return control->fn(pma);
9799 /* Construct an isl_union_pw_multi_aff that is obtained by
9800 * modifying "upma" according to "control".
9802 * isl_union_pw_multi_aff_transform performs essentially
9803 * the same operation, but takes a filter and a callback function
9804 * of a different form (with an extra argument).
9805 * Call isl_union_pw_multi_aff_transform with wrappers
9806 * that remove this extra argument.
9808 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9809 __isl_take isl_union_pw_multi_aff *upma,
9810 struct isl_union_pw_multi_aff_un_op_control *control)
9812 struct isl_union_pw_multi_aff_transform_control t_control = {
9813 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9814 .filter_user = control,
9815 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9816 .fn_user = control,
9819 return isl_union_pw_multi_aff_transform(upma, &t_control);
9822 /* For each function in "upma" of the form A -> [B -> C],
9823 * extract the function A -> B and collect the results.
9825 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9826 __isl_take isl_union_pw_multi_aff *upma)
9828 struct isl_union_pw_multi_aff_un_op_control control = {
9829 .filter = &isl_pw_multi_aff_range_is_wrapping,
9830 .fn = &isl_pw_multi_aff_range_factor_domain,
9832 return isl_union_pw_multi_aff_un_op(upma, &control);
9835 /* For each function in "upma" of the form A -> [B -> C],
9836 * extract the function A -> C and collect the results.
9838 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9839 __isl_take isl_union_pw_multi_aff *upma)
9841 struct isl_union_pw_multi_aff_un_op_control control = {
9842 .filter = &isl_pw_multi_aff_range_is_wrapping,
9843 .fn = &isl_pw_multi_aff_range_factor_range,
9845 return isl_union_pw_multi_aff_un_op(upma, &control);
9848 /* Evaluate the affine function "aff" in the void point "pnt".
9849 * In particular, return the value NaN.
9851 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9852 __isl_take isl_point *pnt)
9854 isl_ctx *ctx;
9856 ctx = isl_point_get_ctx(pnt);
9857 isl_aff_free(aff);
9858 isl_point_free(pnt);
9859 return isl_val_nan(ctx);
9862 /* Evaluate the affine expression "aff"
9863 * in the coordinates (with denominator) "pnt".
9865 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9866 __isl_keep isl_vec *pnt)
9868 isl_int n, d;
9869 isl_ctx *ctx;
9870 isl_val *v;
9872 if (!aff || !pnt)
9873 return NULL;
9875 ctx = isl_vec_get_ctx(aff);
9876 isl_int_init(n);
9877 isl_int_init(d);
9878 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9879 isl_int_mul(d, aff->el[0], pnt->el[0]);
9880 v = isl_val_rat_from_isl_int(ctx, n, d);
9881 v = isl_val_normalize(v);
9882 isl_int_clear(n);
9883 isl_int_clear(d);
9885 return v;
9888 /* Check that the domain space of "aff" is equal to "space".
9890 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9891 __isl_keep isl_space *space)
9893 isl_bool ok;
9895 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9896 if (ok < 0)
9897 return isl_stat_error;
9898 if (!ok)
9899 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9900 "incompatible spaces", return isl_stat_error);
9901 return isl_stat_ok;
9904 /* Evaluate the affine function "aff" in "pnt".
9906 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9907 __isl_take isl_point *pnt)
9909 isl_bool is_void;
9910 isl_val *v;
9911 isl_local_space *ls;
9913 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9914 goto error;
9915 is_void = isl_point_is_void(pnt);
9916 if (is_void < 0)
9917 goto error;
9918 if (is_void)
9919 return eval_void(aff, pnt);
9921 ls = isl_aff_get_domain_local_space(aff);
9922 pnt = isl_local_space_lift_point(ls, pnt);
9924 v = eval(aff->v, isl_point_peek_vec(pnt));
9926 isl_aff_free(aff);
9927 isl_point_free(pnt);
9929 return v;
9930 error:
9931 isl_aff_free(aff);
9932 isl_point_free(pnt);
9933 return NULL;