isl_schedule_node.c: is_disjoint_extension: return isl_bool
[isl.git] / isl_aff.c
blobed977f8eaa3242de33826179fcf84bd93d12fc44
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 /* Construct an isl_aff from the given domain local space "ls" and
67 * coefficients "v", where the local space is known to be valid
68 * for an affine expression.
70 static __isl_give isl_aff *isl_aff_alloc_vec_validated(
71 __isl_take isl_local_space *ls, __isl_take isl_vec *v)
73 isl_aff *aff;
75 if (!ls || !v)
76 goto error;
78 aff = isl_calloc_type(v->ctx, struct isl_aff);
79 if (!aff)
80 goto error;
82 aff->ref = 1;
83 aff->ls = ls;
84 aff->v = v;
86 return aff;
87 error:
88 isl_local_space_free(ls);
89 isl_vec_free(v);
90 return NULL;
93 /* Construct an isl_aff from the given domain local space "ls" and
94 * coefficients "v".
96 * First check that "ls" is a valid domain local space
97 * for an affine expression.
99 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
100 __isl_take isl_vec *v)
102 isl_ctx *ctx;
104 if (!ls)
105 return NULL;
107 ctx = isl_local_space_get_ctx(ls);
108 if (!isl_local_space_divs_known(ls))
109 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
110 goto error);
111 if (!isl_local_space_is_set(ls))
112 isl_die(ctx, isl_error_invalid,
113 "domain of affine expression should be a set",
114 goto error);
115 return isl_aff_alloc_vec_validated(ls, v);
116 error:
117 isl_local_space_free(ls);
118 isl_vec_free(v);
119 return NULL;
122 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
124 isl_ctx *ctx;
125 isl_vec *v;
126 isl_size total;
128 if (!ls)
129 return NULL;
131 ctx = isl_local_space_get_ctx(ls);
133 total = isl_local_space_dim(ls, isl_dim_all);
134 if (total < 0)
135 goto error;
136 v = isl_vec_alloc(ctx, 1 + 1 + total);
137 return isl_aff_alloc_vec(ls, v);
138 error:
139 isl_local_space_free(ls);
140 return NULL;
143 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
145 if (!aff)
146 return NULL;
148 aff->ref++;
149 return aff;
152 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
154 if (!aff)
155 return NULL;
157 return isl_aff_alloc_vec_validated(isl_local_space_copy(aff->ls),
158 isl_vec_copy(aff->v));
161 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
163 if (!aff)
164 return NULL;
166 if (aff->ref == 1)
167 return aff;
168 aff->ref--;
169 return isl_aff_dup(aff);
172 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
174 isl_aff *aff;
176 aff = isl_aff_alloc(ls);
177 if (!aff)
178 return NULL;
180 isl_int_set_si(aff->v->el[0], 1);
181 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
183 return aff;
186 /* Return an affine expression that is equal to zero on domain space "space".
188 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
190 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
193 /* This function performs the same operation as isl_aff_zero_on_domain_space,
194 * but is considered as a function on an isl_space when exported.
196 __isl_give isl_aff *isl_space_zero_aff_on_domain(__isl_take isl_space *space)
198 return isl_aff_zero_on_domain_space(space);
201 /* Return a piecewise affine expression defined on the specified domain
202 * that is equal to zero.
204 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
206 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
209 /* Change "aff" into a NaN.
211 * Note that this function gets called from isl_aff_nan_on_domain,
212 * so "aff" may not have been initialized yet.
214 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
216 aff = isl_aff_cow(aff);
217 if (!aff)
218 return NULL;
220 aff->v = isl_vec_clr(aff->v);
221 if (!aff->v)
222 return isl_aff_free(aff);
224 return aff;
227 /* Return an affine expression defined on the specified domain
228 * that represents NaN.
230 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
232 isl_aff *aff;
234 aff = isl_aff_alloc(ls);
235 return isl_aff_set_nan(aff);
238 /* Return an affine expression defined on the specified domain space
239 * that represents NaN.
241 __isl_give isl_aff *isl_aff_nan_on_domain_space(__isl_take isl_space *space)
243 return isl_aff_nan_on_domain(isl_local_space_from_space(space));
246 /* Return a piecewise affine expression defined on the specified domain space
247 * that represents NaN.
249 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain_space(
250 __isl_take isl_space *space)
252 return isl_pw_aff_from_aff(isl_aff_nan_on_domain_space(space));
255 /* Return a piecewise affine expression defined on the specified domain
256 * that represents NaN.
258 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
260 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
263 /* Return an affine expression that is equal to "val" on
264 * domain local space "ls".
266 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
267 __isl_take isl_val *val)
269 isl_aff *aff;
271 if (!ls || !val)
272 goto error;
273 if (!isl_val_is_rat(val))
274 isl_die(isl_val_get_ctx(val), isl_error_invalid,
275 "expecting rational value", goto error);
277 aff = isl_aff_alloc(isl_local_space_copy(ls));
278 if (!aff)
279 goto error;
281 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
282 isl_int_set(aff->v->el[1], val->n);
283 isl_int_set(aff->v->el[0], val->d);
285 isl_local_space_free(ls);
286 isl_val_free(val);
287 return aff;
288 error:
289 isl_local_space_free(ls);
290 isl_val_free(val);
291 return NULL;
294 /* Return an affine expression that is equal to "val" on domain space "space".
296 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
297 __isl_take isl_val *val)
299 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
302 /* Return an affine expression that is equal to the specified dimension
303 * in "ls".
305 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
306 enum isl_dim_type type, unsigned pos)
308 isl_space *space;
309 isl_aff *aff;
311 if (!ls)
312 return NULL;
314 space = isl_local_space_get_space(ls);
315 if (!space)
316 goto error;
317 if (isl_space_is_map(space))
318 isl_die(isl_space_get_ctx(space), isl_error_invalid,
319 "expecting (parameter) set space", goto error);
320 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
321 goto error;
323 isl_space_free(space);
324 aff = isl_aff_alloc(ls);
325 if (!aff)
326 return NULL;
328 pos += isl_local_space_offset(aff->ls, type);
330 isl_int_set_si(aff->v->el[0], 1);
331 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
332 isl_int_set_si(aff->v->el[1 + pos], 1);
334 return aff;
335 error:
336 isl_local_space_free(ls);
337 isl_space_free(space);
338 return NULL;
341 /* Return a piecewise affine expression that is equal to
342 * the specified dimension in "ls".
344 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
345 enum isl_dim_type type, unsigned pos)
347 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
350 /* Return an affine expression that is equal to the parameter
351 * in the domain space "space" with identifier "id".
353 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
354 __isl_take isl_space *space, __isl_take isl_id *id)
356 int pos;
357 isl_local_space *ls;
359 if (!space || !id)
360 goto error;
361 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
362 if (pos < 0)
363 isl_die(isl_space_get_ctx(space), isl_error_invalid,
364 "parameter not found in space", goto error);
365 isl_id_free(id);
366 ls = isl_local_space_from_space(space);
367 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
368 error:
369 isl_space_free(space);
370 isl_id_free(id);
371 return NULL;
374 /* This function performs the same operation as
375 * isl_aff_param_on_domain_space_id,
376 * but is considered as a function on an isl_space when exported.
378 __isl_give isl_aff *isl_space_param_aff_on_domain_id(
379 __isl_take isl_space *space, __isl_take isl_id *id)
381 return isl_aff_param_on_domain_space_id(space, id);
384 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
386 if (!aff)
387 return NULL;
389 if (--aff->ref > 0)
390 return NULL;
392 isl_local_space_free(aff->ls);
393 isl_vec_free(aff->v);
395 free(aff);
397 return NULL;
400 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
402 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
405 /* Return a hash value that digests "aff".
407 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
409 uint32_t hash, ls_hash, v_hash;
411 if (!aff)
412 return 0;
414 hash = isl_hash_init();
415 ls_hash = isl_local_space_get_hash(aff->ls);
416 isl_hash_hash(hash, ls_hash);
417 v_hash = isl_vec_get_hash(aff->v);
418 isl_hash_hash(hash, v_hash);
420 return hash;
423 /* Return the domain local space of "aff".
425 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
426 __isl_keep isl_aff *aff)
428 return aff ? aff->ls : NULL;
431 /* Return the number of variables of the given type in the domain of "aff".
433 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
435 isl_local_space *ls;
437 ls = isl_aff_peek_domain_local_space(aff);
438 return isl_local_space_dim(ls, type);
441 /* Externally, an isl_aff has a map space, but internally, the
442 * ls field corresponds to the domain of that space.
444 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
446 if (!aff)
447 return isl_size_error;
448 if (type == isl_dim_out)
449 return 1;
450 if (type == isl_dim_in)
451 type = isl_dim_set;
452 return isl_aff_domain_dim(aff, type);
455 /* Return the offset of the first coefficient of type "type" in
456 * the domain of "aff".
458 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
460 isl_local_space *ls;
462 ls = isl_aff_peek_domain_local_space(aff);
463 return isl_local_space_offset(ls, type);
466 /* Return the position of the dimension of the given type and name
467 * in "aff".
468 * Return -1 if no such dimension can be found.
470 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
471 const char *name)
473 if (!aff)
474 return -1;
475 if (type == isl_dim_out)
476 return -1;
477 if (type == isl_dim_in)
478 type = isl_dim_set;
479 return isl_local_space_find_dim_by_name(aff->ls, type, name);
482 /* Return the domain space of "aff".
484 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
486 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
489 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
491 return isl_space_copy(isl_aff_peek_domain_space(aff));
494 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
496 isl_space *space;
497 if (!aff)
498 return NULL;
499 space = isl_local_space_get_space(aff->ls);
500 space = isl_space_from_domain(space);
501 space = isl_space_add_dims(space, isl_dim_out, 1);
502 return space;
505 /* Return a copy of the domain space of "aff".
507 __isl_give isl_local_space *isl_aff_get_domain_local_space(
508 __isl_keep isl_aff *aff)
510 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
513 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
515 isl_local_space *ls;
516 if (!aff)
517 return NULL;
518 ls = isl_local_space_copy(aff->ls);
519 ls = isl_local_space_from_domain(ls);
520 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
521 return ls;
524 /* Return the local space of the domain of "aff".
525 * This may be either a copy or the local space itself
526 * if there is only one reference to "aff".
527 * This allows the local space to be modified inplace
528 * if both the expression and its local space have only a single reference.
529 * The caller is not allowed to modify "aff" between this call and
530 * a subsequent call to isl_aff_restore_domain_local_space.
531 * The only exception is that isl_aff_free can be called instead.
533 __isl_give isl_local_space *isl_aff_take_domain_local_space(
534 __isl_keep isl_aff *aff)
536 isl_local_space *ls;
538 if (!aff)
539 return NULL;
540 if (aff->ref != 1)
541 return isl_aff_get_domain_local_space(aff);
542 ls = aff->ls;
543 aff->ls = NULL;
544 return ls;
547 /* Set the local space of the domain of "aff" to "ls",
548 * where the local space of "aff" may be missing
549 * due to a preceding call to isl_aff_take_domain_local_space.
550 * However, in this case, "aff" only has a single reference and
551 * then the call to isl_aff_cow has no effect.
553 __isl_give isl_aff *isl_aff_restore_domain_local_space(
554 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
556 if (!aff || !ls)
557 goto error;
559 if (aff->ls == ls) {
560 isl_local_space_free(ls);
561 return aff;
564 aff = isl_aff_cow(aff);
565 if (!aff)
566 goto error;
567 isl_local_space_free(aff->ls);
568 aff->ls = ls;
570 return aff;
571 error:
572 isl_aff_free(aff);
573 isl_local_space_free(ls);
574 return NULL;
577 /* Externally, an isl_aff has a map space, but internally, the
578 * ls field corresponds to the domain of that space.
580 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
581 enum isl_dim_type type, unsigned pos)
583 if (!aff)
584 return NULL;
585 if (type == isl_dim_out)
586 return NULL;
587 if (type == isl_dim_in)
588 type = isl_dim_set;
589 return isl_local_space_get_dim_name(aff->ls, type, pos);
592 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
593 __isl_take isl_space *space)
595 aff = isl_aff_cow(aff);
596 if (!aff || !space)
597 goto error;
599 aff->ls = isl_local_space_reset_space(aff->ls, space);
600 if (!aff->ls)
601 return isl_aff_free(aff);
603 return aff;
604 error:
605 isl_aff_free(aff);
606 isl_space_free(space);
607 return NULL;
610 /* Reset the space of "aff". This function is called from isl_pw_templ.c
611 * and doesn't know if the space of an element object is represented
612 * directly or through its domain. It therefore passes along both.
614 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
615 __isl_take isl_space *space, __isl_take isl_space *domain)
617 isl_space_free(space);
618 return isl_aff_reset_domain_space(aff, domain);
621 /* Reorder the coefficients of the affine expression based
622 * on the given reordering.
623 * The reordering r is assumed to have been extended with the local
624 * variables.
626 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
627 __isl_take isl_reordering *r, int n_div)
629 isl_space *space;
630 isl_vec *res;
631 isl_size dim;
632 int i;
634 if (!vec || !r)
635 goto error;
637 space = isl_reordering_peek_space(r);
638 dim = isl_space_dim(space, isl_dim_all);
639 if (dim < 0)
640 goto error;
641 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
642 if (!res)
643 goto error;
644 isl_seq_cpy(res->el, vec->el, 2);
645 isl_seq_clr(res->el + 2, res->size - 2);
646 for (i = 0; i < r->len; ++i)
647 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
649 isl_reordering_free(r);
650 isl_vec_free(vec);
651 return res;
652 error:
653 isl_vec_free(vec);
654 isl_reordering_free(r);
655 return NULL;
658 /* Reorder the dimensions of the domain of "aff" according
659 * to the given reordering.
661 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
662 __isl_take isl_reordering *r)
664 aff = isl_aff_cow(aff);
665 if (!aff)
666 goto error;
668 r = isl_reordering_extend(r, aff->ls->div->n_row);
669 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
670 aff->ls->div->n_row);
671 aff->ls = isl_local_space_realign(aff->ls, r);
673 if (!aff->v || !aff->ls)
674 return isl_aff_free(aff);
676 return aff;
677 error:
678 isl_aff_free(aff);
679 isl_reordering_free(r);
680 return NULL;
683 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
684 __isl_take isl_space *model)
686 isl_bool equal_params;
688 if (!aff || !model)
689 goto error;
691 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
692 if (equal_params < 0)
693 goto error;
694 if (!equal_params) {
695 isl_reordering *exp;
697 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
698 exp = isl_reordering_extend_space(exp,
699 isl_aff_get_domain_space(aff));
700 aff = isl_aff_realign_domain(aff, exp);
703 isl_space_free(model);
704 return aff;
705 error:
706 isl_space_free(model);
707 isl_aff_free(aff);
708 return NULL;
711 #undef TYPE
712 #define TYPE isl_aff
713 #include "isl_unbind_params_templ.c"
715 /* Is "aff" obviously equal to zero?
717 * If the denominator is zero, then "aff" is not equal to zero.
719 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
721 int pos;
723 if (!aff)
724 return isl_bool_error;
726 if (isl_int_is_zero(aff->v->el[0]))
727 return isl_bool_false;
728 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
729 return isl_bool_ok(pos < 0);
732 /* Does "aff" represent NaN?
734 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
736 if (!aff)
737 return isl_bool_error;
739 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
742 /* Are "aff1" and "aff2" obviously equal?
744 * NaN is not equal to anything, not even to another NaN.
746 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
747 __isl_keep isl_aff *aff2)
749 isl_bool equal;
751 if (!aff1 || !aff2)
752 return isl_bool_error;
754 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
755 return isl_bool_false;
757 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
758 if (equal < 0 || !equal)
759 return equal;
761 return isl_vec_is_equal(aff1->v, aff2->v);
764 /* Return the common denominator of "aff" in "v".
766 * We cannot return anything meaningful in case of a NaN.
768 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
770 if (!aff)
771 return isl_stat_error;
772 if (isl_aff_is_nan(aff))
773 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
774 "cannot get denominator of NaN", return isl_stat_error);
775 isl_int_set(*v, aff->v->el[0]);
776 return isl_stat_ok;
779 /* Return the common denominator of "aff".
781 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
783 isl_ctx *ctx;
785 if (!aff)
786 return NULL;
788 ctx = isl_aff_get_ctx(aff);
789 if (isl_aff_is_nan(aff))
790 return isl_val_nan(ctx);
791 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
794 /* Return the constant term of "aff".
796 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
798 isl_ctx *ctx;
799 isl_val *v;
801 if (!aff)
802 return NULL;
804 ctx = isl_aff_get_ctx(aff);
805 if (isl_aff_is_nan(aff))
806 return isl_val_nan(ctx);
807 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
808 return isl_val_normalize(v);
811 /* Return the coefficient of the variable of type "type" at position "pos"
812 * of "aff".
814 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
815 enum isl_dim_type type, int pos)
817 isl_ctx *ctx;
818 isl_val *v;
820 if (!aff)
821 return NULL;
823 ctx = isl_aff_get_ctx(aff);
824 if (type == isl_dim_out)
825 isl_die(ctx, isl_error_invalid,
826 "output/set dimension does not have a coefficient",
827 return NULL);
828 if (type == isl_dim_in)
829 type = isl_dim_set;
831 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
832 return NULL;
834 if (isl_aff_is_nan(aff))
835 return isl_val_nan(ctx);
836 pos += isl_local_space_offset(aff->ls, type);
837 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
838 return isl_val_normalize(v);
841 /* Return the sign of the coefficient of the variable of type "type"
842 * at position "pos" of "aff".
844 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
845 int pos)
847 isl_ctx *ctx;
849 if (!aff)
850 return 0;
852 ctx = isl_aff_get_ctx(aff);
853 if (type == isl_dim_out)
854 isl_die(ctx, isl_error_invalid,
855 "output/set dimension does not have a coefficient",
856 return 0);
857 if (type == isl_dim_in)
858 type = isl_dim_set;
860 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
861 return 0;
863 pos += isl_local_space_offset(aff->ls, type);
864 return isl_int_sgn(aff->v->el[1 + pos]);
867 /* Replace the numerator of the constant term of "aff" by "v".
869 * A NaN is unaffected by this operation.
871 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
873 if (!aff)
874 return NULL;
875 if (isl_aff_is_nan(aff))
876 return aff;
877 aff = isl_aff_cow(aff);
878 if (!aff)
879 return NULL;
881 aff->v = isl_vec_cow(aff->v);
882 if (!aff->v)
883 return isl_aff_free(aff);
885 isl_int_set(aff->v->el[1], v);
887 return aff;
890 /* Replace the constant term of "aff" by "v".
892 * A NaN is unaffected by this operation.
894 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
895 __isl_take isl_val *v)
897 if (!aff || !v)
898 goto error;
900 if (isl_aff_is_nan(aff)) {
901 isl_val_free(v);
902 return aff;
905 if (!isl_val_is_rat(v))
906 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
907 "expecting rational value", goto error);
909 if (isl_int_eq(aff->v->el[1], v->n) &&
910 isl_int_eq(aff->v->el[0], v->d)) {
911 isl_val_free(v);
912 return aff;
915 aff = isl_aff_cow(aff);
916 if (!aff)
917 goto error;
918 aff->v = isl_vec_cow(aff->v);
919 if (!aff->v)
920 goto error;
922 if (isl_int_eq(aff->v->el[0], v->d)) {
923 isl_int_set(aff->v->el[1], v->n);
924 } else if (isl_int_is_one(v->d)) {
925 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
926 } else {
927 isl_seq_scale(aff->v->el + 1,
928 aff->v->el + 1, v->d, aff->v->size - 1);
929 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
930 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
931 aff->v = isl_vec_normalize(aff->v);
932 if (!aff->v)
933 goto error;
936 isl_val_free(v);
937 return aff;
938 error:
939 isl_aff_free(aff);
940 isl_val_free(v);
941 return NULL;
944 /* Add "v" to the constant term of "aff".
946 * A NaN is unaffected by this operation.
948 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
950 if (isl_int_is_zero(v))
951 return aff;
953 if (!aff)
954 return NULL;
955 if (isl_aff_is_nan(aff))
956 return aff;
957 aff = isl_aff_cow(aff);
958 if (!aff)
959 return NULL;
961 aff->v = isl_vec_cow(aff->v);
962 if (!aff->v)
963 return isl_aff_free(aff);
965 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
967 return aff;
970 /* Add "v" to the constant term of "aff",
971 * in case "aff" is a rational expression.
973 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
974 __isl_take isl_val *v)
976 aff = isl_aff_cow(aff);
977 if (!aff)
978 goto error;
980 aff->v = isl_vec_cow(aff->v);
981 if (!aff->v)
982 goto error;
984 if (isl_int_is_one(v->d)) {
985 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
986 } else if (isl_int_eq(aff->v->el[0], v->d)) {
987 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
988 aff->v = isl_vec_normalize(aff->v);
989 if (!aff->v)
990 goto error;
991 } else {
992 isl_seq_scale(aff->v->el + 1,
993 aff->v->el + 1, v->d, aff->v->size - 1);
994 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
995 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
996 aff->v = isl_vec_normalize(aff->v);
997 if (!aff->v)
998 goto error;
1001 isl_val_free(v);
1002 return aff;
1003 error:
1004 isl_aff_free(aff);
1005 isl_val_free(v);
1006 return NULL;
1009 /* Return the first argument and free the second.
1011 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
1012 __isl_take isl_val *v)
1014 isl_val_free(v);
1015 return aff;
1018 /* Replace the first argument by NaN and free the second argument.
1020 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
1021 __isl_take isl_val *v)
1023 isl_val_free(v);
1024 return isl_aff_set_nan(aff);
1027 /* Add "v" to the constant term of "aff".
1029 * A NaN is unaffected by this operation.
1030 * Conversely, adding a NaN turns "aff" into a NaN.
1032 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
1033 __isl_take isl_val *v)
1035 isl_bool is_nan, is_zero, is_rat;
1037 is_nan = isl_aff_is_nan(aff);
1038 is_zero = isl_val_is_zero(v);
1039 if (is_nan < 0 || is_zero < 0)
1040 goto error;
1041 if (is_nan || is_zero)
1042 return pick_free(aff, v);
1044 is_nan = isl_val_is_nan(v);
1045 is_rat = isl_val_is_rat(v);
1046 if (is_nan < 0 || is_rat < 0)
1047 goto error;
1048 if (is_nan)
1049 return set_nan_free_val(aff, v);
1050 if (!is_rat)
1051 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1052 "expecting rational value or NaN", goto error);
1054 return isl_aff_add_rat_constant_val(aff, v);
1055 error:
1056 isl_aff_free(aff);
1057 isl_val_free(v);
1058 return NULL;
1061 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1063 isl_int t;
1065 isl_int_init(t);
1066 isl_int_set_si(t, v);
1067 aff = isl_aff_add_constant(aff, t);
1068 isl_int_clear(t);
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(__isl_take isl_aff *aff, isl_int v)
1079 if (isl_int_is_zero(v))
1080 return aff;
1082 if (!aff)
1083 return NULL;
1084 if (isl_aff_is_nan(aff))
1085 return aff;
1086 aff = isl_aff_cow(aff);
1087 if (!aff)
1088 return NULL;
1090 aff->v = isl_vec_cow(aff->v);
1091 if (!aff->v)
1092 return isl_aff_free(aff);
1094 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1096 return aff;
1099 /* Add "v" to the numerator of the constant term of "aff".
1101 * A NaN is unaffected by this operation.
1103 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1105 isl_int t;
1107 if (v == 0)
1108 return aff;
1110 isl_int_init(t);
1111 isl_int_set_si(t, v);
1112 aff = isl_aff_add_constant_num(aff, t);
1113 isl_int_clear(t);
1115 return aff;
1118 /* Replace the numerator of the constant term of "aff" by "v".
1120 * A NaN is unaffected by this operation.
1122 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1124 if (!aff)
1125 return NULL;
1126 if (isl_aff_is_nan(aff))
1127 return aff;
1128 aff = isl_aff_cow(aff);
1129 if (!aff)
1130 return NULL;
1132 aff->v = isl_vec_cow(aff->v);
1133 if (!aff->v)
1134 return isl_aff_free(aff);
1136 isl_int_set_si(aff->v->el[1], v);
1138 return aff;
1141 /* Replace the numerator of the coefficient of the variable of type "type"
1142 * at position "pos" of "aff" by "v".
1144 * A NaN is unaffected by this operation.
1146 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1147 enum isl_dim_type type, int pos, isl_int v)
1149 if (!aff)
1150 return NULL;
1152 if (type == isl_dim_out)
1153 isl_die(aff->v->ctx, isl_error_invalid,
1154 "output/set dimension does not have a coefficient",
1155 return isl_aff_free(aff));
1156 if (type == isl_dim_in)
1157 type = isl_dim_set;
1159 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1160 return isl_aff_free(aff);
1162 if (isl_aff_is_nan(aff))
1163 return aff;
1164 aff = isl_aff_cow(aff);
1165 if (!aff)
1166 return NULL;
1168 aff->v = isl_vec_cow(aff->v);
1169 if (!aff->v)
1170 return isl_aff_free(aff);
1172 pos += isl_local_space_offset(aff->ls, type);
1173 isl_int_set(aff->v->el[1 + pos], v);
1175 return aff;
1178 /* Replace the numerator of the coefficient of the variable of type "type"
1179 * at position "pos" of "aff" by "v".
1181 * A NaN is unaffected by this operation.
1183 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1184 enum isl_dim_type type, int pos, int v)
1186 if (!aff)
1187 return NULL;
1189 if (type == isl_dim_out)
1190 isl_die(aff->v->ctx, isl_error_invalid,
1191 "output/set dimension does not have a coefficient",
1192 return isl_aff_free(aff));
1193 if (type == isl_dim_in)
1194 type = isl_dim_set;
1196 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1197 return isl_aff_free(aff);
1199 if (isl_aff_is_nan(aff))
1200 return aff;
1201 pos += isl_local_space_offset(aff->ls, type);
1202 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1203 return aff;
1205 aff = isl_aff_cow(aff);
1206 if (!aff)
1207 return NULL;
1209 aff->v = isl_vec_cow(aff->v);
1210 if (!aff->v)
1211 return isl_aff_free(aff);
1213 isl_int_set_si(aff->v->el[1 + pos], v);
1215 return aff;
1218 /* Replace the coefficient of the variable of type "type" at position "pos"
1219 * of "aff" by "v".
1221 * A NaN is unaffected by this operation.
1223 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1224 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1226 if (!aff || !v)
1227 goto error;
1229 if (type == isl_dim_out)
1230 isl_die(aff->v->ctx, isl_error_invalid,
1231 "output/set dimension does not have a coefficient",
1232 goto error);
1233 if (type == isl_dim_in)
1234 type = isl_dim_set;
1236 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1237 return isl_aff_free(aff);
1239 if (isl_aff_is_nan(aff)) {
1240 isl_val_free(v);
1241 return aff;
1243 if (!isl_val_is_rat(v))
1244 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1245 "expecting rational value", goto error);
1247 pos += isl_local_space_offset(aff->ls, type);
1248 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1249 isl_int_eq(aff->v->el[0], v->d)) {
1250 isl_val_free(v);
1251 return aff;
1254 aff = isl_aff_cow(aff);
1255 if (!aff)
1256 goto error;
1257 aff->v = isl_vec_cow(aff->v);
1258 if (!aff->v)
1259 goto error;
1261 if (isl_int_eq(aff->v->el[0], v->d)) {
1262 isl_int_set(aff->v->el[1 + pos], v->n);
1263 } else if (isl_int_is_one(v->d)) {
1264 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1265 } else {
1266 isl_seq_scale(aff->v->el + 1,
1267 aff->v->el + 1, v->d, aff->v->size - 1);
1268 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1269 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1270 aff->v = isl_vec_normalize(aff->v);
1271 if (!aff->v)
1272 goto error;
1275 isl_val_free(v);
1276 return aff;
1277 error:
1278 isl_aff_free(aff);
1279 isl_val_free(v);
1280 return NULL;
1283 /* Add "v" to the coefficient of the variable of type "type"
1284 * at position "pos" of "aff".
1286 * A NaN is unaffected by this operation.
1288 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1289 enum isl_dim_type type, int pos, isl_int v)
1291 if (!aff)
1292 return NULL;
1294 if (type == isl_dim_out)
1295 isl_die(aff->v->ctx, isl_error_invalid,
1296 "output/set dimension does not have a coefficient",
1297 return isl_aff_free(aff));
1298 if (type == isl_dim_in)
1299 type = isl_dim_set;
1301 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1302 return isl_aff_free(aff);
1304 if (isl_aff_is_nan(aff))
1305 return aff;
1306 aff = isl_aff_cow(aff);
1307 if (!aff)
1308 return NULL;
1310 aff->v = isl_vec_cow(aff->v);
1311 if (!aff->v)
1312 return isl_aff_free(aff);
1314 pos += isl_local_space_offset(aff->ls, type);
1315 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1317 return aff;
1320 /* Add "v" to the coefficient of the variable of type "type"
1321 * at position "pos" of "aff".
1323 * A NaN is unaffected by this operation.
1325 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1326 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1328 if (!aff || !v)
1329 goto error;
1331 if (isl_val_is_zero(v)) {
1332 isl_val_free(v);
1333 return aff;
1336 if (type == isl_dim_out)
1337 isl_die(aff->v->ctx, isl_error_invalid,
1338 "output/set dimension does not have a coefficient",
1339 goto error);
1340 if (type == isl_dim_in)
1341 type = isl_dim_set;
1343 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1344 goto error;
1346 if (isl_aff_is_nan(aff)) {
1347 isl_val_free(v);
1348 return aff;
1350 if (!isl_val_is_rat(v))
1351 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1352 "expecting rational value", goto error);
1354 aff = isl_aff_cow(aff);
1355 if (!aff)
1356 goto error;
1358 aff->v = isl_vec_cow(aff->v);
1359 if (!aff->v)
1360 goto error;
1362 pos += isl_local_space_offset(aff->ls, type);
1363 if (isl_int_is_one(v->d)) {
1364 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1365 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1366 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1367 aff->v = isl_vec_normalize(aff->v);
1368 if (!aff->v)
1369 goto error;
1370 } else {
1371 isl_seq_scale(aff->v->el + 1,
1372 aff->v->el + 1, v->d, aff->v->size - 1);
1373 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1374 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1375 aff->v = isl_vec_normalize(aff->v);
1376 if (!aff->v)
1377 goto error;
1380 isl_val_free(v);
1381 return aff;
1382 error:
1383 isl_aff_free(aff);
1384 isl_val_free(v);
1385 return NULL;
1388 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1389 enum isl_dim_type type, int pos, int v)
1391 isl_int t;
1393 isl_int_init(t);
1394 isl_int_set_si(t, v);
1395 aff = isl_aff_add_coefficient(aff, type, pos, t);
1396 isl_int_clear(t);
1398 return aff;
1401 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1403 if (!aff)
1404 return NULL;
1406 return isl_local_space_get_div(aff->ls, pos);
1409 /* Return the negation of "aff".
1411 * As a special case, -NaN = NaN.
1413 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1415 if (!aff)
1416 return NULL;
1417 if (isl_aff_is_nan(aff))
1418 return aff;
1419 aff = isl_aff_cow(aff);
1420 if (!aff)
1421 return NULL;
1422 aff->v = isl_vec_cow(aff->v);
1423 if (!aff->v)
1424 return isl_aff_free(aff);
1426 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1428 return aff;
1431 /* Remove divs from the local space that do not appear in the affine
1432 * expression.
1433 * We currently only remove divs at the end.
1434 * Some intermediate divs may also not appear directly in the affine
1435 * expression, but we would also need to check that no other divs are
1436 * defined in terms of them.
1438 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1440 int pos;
1441 isl_size off;
1442 isl_size n;
1444 n = isl_aff_domain_dim(aff, isl_dim_div);
1445 off = isl_aff_domain_offset(aff, isl_dim_div);
1446 if (n < 0 || off < 0)
1447 return isl_aff_free(aff);
1449 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1450 if (pos == n)
1451 return aff;
1453 aff = isl_aff_cow(aff);
1454 if (!aff)
1455 return NULL;
1457 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1458 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1459 if (!aff->ls || !aff->v)
1460 return isl_aff_free(aff);
1462 return aff;
1465 /* Look for any divs in the aff->ls with a denominator equal to one
1466 * and plug them into the affine expression and any subsequent divs
1467 * that may reference the div.
1469 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1471 int i;
1472 isl_size n;
1473 int len;
1474 isl_int v;
1475 isl_vec *vec;
1476 isl_local_space *ls;
1477 isl_size off;
1479 n = isl_aff_domain_dim(aff, isl_dim_div);
1480 off = isl_aff_domain_offset(aff, isl_dim_div);
1481 if (n < 0 || off < 0)
1482 return isl_aff_free(aff);
1483 len = aff->v->size;
1484 for (i = 0; i < n; ++i) {
1485 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1486 continue;
1487 ls = isl_local_space_copy(aff->ls);
1488 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1489 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1490 vec = isl_vec_copy(aff->v);
1491 vec = isl_vec_cow(vec);
1492 if (!ls || !vec)
1493 goto error;
1495 isl_int_init(v);
1497 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1498 len, len, v);
1500 isl_int_clear(v);
1502 isl_vec_free(aff->v);
1503 aff->v = vec;
1504 isl_local_space_free(aff->ls);
1505 aff->ls = ls;
1508 return aff;
1509 error:
1510 isl_vec_free(vec);
1511 isl_local_space_free(ls);
1512 return isl_aff_free(aff);
1515 /* Look for any divs j that appear with a unit coefficient inside
1516 * the definitions of other divs i and plug them into the definitions
1517 * of the divs i.
1519 * In particular, an expression of the form
1521 * floor((f(..) + floor(g(..)/n))/m)
1523 * is simplified to
1525 * floor((n * f(..) + g(..))/(n * m))
1527 * This simplification is correct because we can move the expression
1528 * f(..) into the inner floor in the original expression to obtain
1530 * floor(floor((n * f(..) + g(..))/n)/m)
1532 * from which we can derive the simplified expression.
1534 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1536 int i, j;
1537 isl_size n;
1538 isl_size off;
1540 n = isl_aff_domain_dim(aff, isl_dim_div);
1541 off = isl_aff_domain_offset(aff, isl_dim_div);
1542 if (n < 0 || off < 0)
1543 return isl_aff_free(aff);
1544 for (i = 1; i < n; ++i) {
1545 for (j = 0; j < i; ++j) {
1546 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1547 continue;
1548 aff->ls = isl_local_space_substitute_seq(aff->ls,
1549 isl_dim_div, j, aff->ls->div->row[j],
1550 aff->v->size, i, 1);
1551 if (!aff->ls)
1552 return isl_aff_free(aff);
1556 return aff;
1559 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1561 * Even though this function is only called on isl_affs with a single
1562 * reference, we are careful to only change aff->v and aff->ls together.
1564 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1566 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1567 isl_local_space *ls;
1568 isl_vec *v;
1570 if (off < 0)
1571 return isl_aff_free(aff);
1573 ls = isl_local_space_copy(aff->ls);
1574 ls = isl_local_space_swap_div(ls, a, b);
1575 v = isl_vec_copy(aff->v);
1576 v = isl_vec_cow(v);
1577 if (!ls || !v)
1578 goto error;
1580 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1581 isl_vec_free(aff->v);
1582 aff->v = v;
1583 isl_local_space_free(aff->ls);
1584 aff->ls = ls;
1586 return aff;
1587 error:
1588 isl_vec_free(v);
1589 isl_local_space_free(ls);
1590 return isl_aff_free(aff);
1593 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1595 * We currently do not actually remove div "b", but simply add its
1596 * coefficient to that of "a" and then zero it out.
1598 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1600 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1602 if (off < 0)
1603 return isl_aff_free(aff);
1605 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1606 return aff;
1608 aff->v = isl_vec_cow(aff->v);
1609 if (!aff->v)
1610 return isl_aff_free(aff);
1612 isl_int_add(aff->v->el[1 + off + a],
1613 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1614 isl_int_set_si(aff->v->el[1 + off + b], 0);
1616 return aff;
1619 /* Sort the divs in the local space of "aff" according to
1620 * the comparison function "cmp_row" in isl_local_space.c,
1621 * combining the coefficients of identical divs.
1623 * Reordering divs does not change the semantics of "aff",
1624 * so there is no need to call isl_aff_cow.
1625 * Moreover, this function is currently only called on isl_affs
1626 * with a single reference.
1628 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1630 isl_size n;
1631 int i, j;
1633 n = isl_aff_dim(aff, isl_dim_div);
1634 if (n < 0)
1635 return isl_aff_free(aff);
1636 for (i = 1; i < n; ++i) {
1637 for (j = i - 1; j >= 0; --j) {
1638 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1639 if (cmp < 0)
1640 break;
1641 if (cmp == 0)
1642 aff = merge_divs(aff, j, j + 1);
1643 else
1644 aff = swap_div(aff, j, j + 1);
1645 if (!aff)
1646 return NULL;
1650 return aff;
1653 /* Normalize the representation of "aff".
1655 * This function should only be called on "new" isl_affs, i.e.,
1656 * with only a single reference. We therefore do not need to
1657 * worry about affecting other instances.
1659 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1661 if (!aff)
1662 return NULL;
1663 aff->v = isl_vec_normalize(aff->v);
1664 if (!aff->v)
1665 return isl_aff_free(aff);
1666 aff = plug_in_integral_divs(aff);
1667 aff = plug_in_unit_divs(aff);
1668 aff = sort_divs(aff);
1669 aff = isl_aff_remove_unused_divs(aff);
1670 return aff;
1673 /* Given f, return floor(f).
1674 * If f is an integer expression, then just return f.
1675 * If f is a constant, then return the constant floor(f).
1676 * Otherwise, if f = g/m, write g = q m + r,
1677 * create a new div d = [r/m] and return the expression q + d.
1678 * The coefficients in r are taken to lie between -m/2 and m/2.
1680 * reduce_div_coefficients performs the same normalization.
1682 * As a special case, floor(NaN) = NaN.
1684 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1686 int i;
1687 int size;
1688 isl_ctx *ctx;
1689 isl_vec *div;
1691 if (!aff)
1692 return NULL;
1694 if (isl_aff_is_nan(aff))
1695 return aff;
1696 if (isl_int_is_one(aff->v->el[0]))
1697 return aff;
1699 aff = isl_aff_cow(aff);
1700 if (!aff)
1701 return NULL;
1703 aff->v = isl_vec_cow(aff->v);
1704 if (!aff->v)
1705 return isl_aff_free(aff);
1707 if (isl_aff_is_cst(aff)) {
1708 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1709 isl_int_set_si(aff->v->el[0], 1);
1710 return aff;
1713 div = isl_vec_copy(aff->v);
1714 div = isl_vec_cow(div);
1715 if (!div)
1716 return isl_aff_free(aff);
1718 ctx = isl_aff_get_ctx(aff);
1719 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1720 for (i = 1; i < aff->v->size; ++i) {
1721 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1722 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1723 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1724 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1725 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1729 aff->ls = isl_local_space_add_div(aff->ls, div);
1730 if (!aff->ls)
1731 return isl_aff_free(aff);
1733 size = aff->v->size;
1734 aff->v = isl_vec_extend(aff->v, size + 1);
1735 if (!aff->v)
1736 return isl_aff_free(aff);
1737 isl_int_set_si(aff->v->el[0], 1);
1738 isl_int_set_si(aff->v->el[size], 1);
1740 aff = isl_aff_normalize(aff);
1742 return aff;
1745 /* Compute
1747 * aff mod m = aff - m * floor(aff/m)
1749 * with m an integer value.
1751 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1752 __isl_take isl_val *m)
1754 isl_aff *res;
1756 if (!aff || !m)
1757 goto error;
1759 if (!isl_val_is_int(m))
1760 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1761 "expecting integer modulo", goto error);
1763 res = isl_aff_copy(aff);
1764 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1765 aff = isl_aff_floor(aff);
1766 aff = isl_aff_scale_val(aff, m);
1767 res = isl_aff_sub(res, aff);
1769 return res;
1770 error:
1771 isl_aff_free(aff);
1772 isl_val_free(m);
1773 return NULL;
1776 /* Compute
1778 * pwaff mod m = pwaff - m * floor(pwaff/m)
1780 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1782 isl_pw_aff *res;
1784 res = isl_pw_aff_copy(pwaff);
1785 pwaff = isl_pw_aff_scale_down(pwaff, m);
1786 pwaff = isl_pw_aff_floor(pwaff);
1787 pwaff = isl_pw_aff_scale(pwaff, m);
1788 res = isl_pw_aff_sub(res, pwaff);
1790 return res;
1793 /* Compute
1795 * pa mod m = pa - m * floor(pa/m)
1797 * with m an integer value.
1799 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1800 __isl_take isl_val *m)
1802 if (!pa || !m)
1803 goto error;
1804 if (!isl_val_is_int(m))
1805 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1806 "expecting integer modulo", goto error);
1807 pa = isl_pw_aff_mod(pa, m->n);
1808 isl_val_free(m);
1809 return pa;
1810 error:
1811 isl_pw_aff_free(pa);
1812 isl_val_free(m);
1813 return NULL;
1816 /* Given f, return ceil(f).
1817 * If f is an integer expression, then just return f.
1818 * Otherwise, let f be the expression
1820 * e/m
1822 * then return
1824 * floor((e + m - 1)/m)
1826 * As a special case, ceil(NaN) = NaN.
1828 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1830 if (!aff)
1831 return NULL;
1833 if (isl_aff_is_nan(aff))
1834 return aff;
1835 if (isl_int_is_one(aff->v->el[0]))
1836 return aff;
1838 aff = isl_aff_cow(aff);
1839 if (!aff)
1840 return NULL;
1841 aff->v = isl_vec_cow(aff->v);
1842 if (!aff->v)
1843 return isl_aff_free(aff);
1845 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1846 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1847 aff = isl_aff_floor(aff);
1849 return aff;
1852 /* Apply the expansion computed by isl_merge_divs.
1853 * The expansion itself is given by "exp" while the resulting
1854 * list of divs is given by "div".
1856 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1857 __isl_take isl_mat *div, int *exp)
1859 isl_size old_n_div;
1860 isl_size new_n_div;
1861 isl_size offset;
1863 aff = isl_aff_cow(aff);
1865 offset = isl_aff_domain_offset(aff, isl_dim_div);
1866 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1867 new_n_div = isl_mat_rows(div);
1868 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1869 goto error;
1871 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1872 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1873 if (!aff->v || !aff->ls)
1874 return isl_aff_free(aff);
1875 return aff;
1876 error:
1877 isl_aff_free(aff);
1878 isl_mat_free(div);
1879 return NULL;
1882 /* Add two affine expressions that live in the same local space.
1884 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1885 __isl_take isl_aff *aff2)
1887 isl_int gcd, f;
1889 aff1 = isl_aff_cow(aff1);
1890 if (!aff1 || !aff2)
1891 goto error;
1893 aff1->v = isl_vec_cow(aff1->v);
1894 if (!aff1->v)
1895 goto error;
1897 isl_int_init(gcd);
1898 isl_int_init(f);
1899 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1900 isl_int_divexact(f, aff2->v->el[0], gcd);
1901 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1902 isl_int_divexact(f, aff1->v->el[0], gcd);
1903 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1904 isl_int_divexact(f, aff2->v->el[0], gcd);
1905 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1906 isl_int_clear(f);
1907 isl_int_clear(gcd);
1909 isl_aff_free(aff2);
1910 aff1 = isl_aff_normalize(aff1);
1911 return aff1;
1912 error:
1913 isl_aff_free(aff1);
1914 isl_aff_free(aff2);
1915 return NULL;
1918 /* Replace one of the arguments by a NaN and free the other one.
1920 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1921 __isl_take isl_aff *aff2)
1923 isl_aff_free(aff2);
1924 return isl_aff_set_nan(aff1);
1927 /* Return the sum of "aff1" and "aff2".
1929 * If either of the two is NaN, then the result is NaN.
1931 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1932 __isl_take isl_aff *aff2)
1934 isl_ctx *ctx;
1935 int *exp1 = NULL;
1936 int *exp2 = NULL;
1937 isl_mat *div;
1938 isl_size n_div1, n_div2;
1940 if (!aff1 || !aff2)
1941 goto error;
1943 ctx = isl_aff_get_ctx(aff1);
1944 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1945 isl_die(ctx, isl_error_invalid,
1946 "spaces don't match", goto error);
1948 if (isl_aff_is_nan(aff1)) {
1949 isl_aff_free(aff2);
1950 return aff1;
1952 if (isl_aff_is_nan(aff2)) {
1953 isl_aff_free(aff1);
1954 return aff2;
1957 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1958 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1959 if (n_div1 < 0 || n_div2 < 0)
1960 goto error;
1961 if (n_div1 == 0 && n_div2 == 0)
1962 return add_expanded(aff1, aff2);
1964 exp1 = isl_alloc_array(ctx, int, n_div1);
1965 exp2 = isl_alloc_array(ctx, int, n_div2);
1966 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1967 goto error;
1969 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1970 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1971 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1972 free(exp1);
1973 free(exp2);
1975 return add_expanded(aff1, aff2);
1976 error:
1977 free(exp1);
1978 free(exp2);
1979 isl_aff_free(aff1);
1980 isl_aff_free(aff2);
1981 return NULL;
1984 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1985 __isl_take isl_aff *aff2)
1987 return isl_aff_add(aff1, isl_aff_neg(aff2));
1990 /* Return the result of scaling "aff" by a factor of "f".
1992 * As a special case, f * NaN = NaN.
1994 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1996 isl_int gcd;
1998 if (!aff)
1999 return NULL;
2000 if (isl_aff_is_nan(aff))
2001 return aff;
2003 if (isl_int_is_one(f))
2004 return aff;
2006 aff = isl_aff_cow(aff);
2007 if (!aff)
2008 return NULL;
2009 aff->v = isl_vec_cow(aff->v);
2010 if (!aff->v)
2011 return isl_aff_free(aff);
2013 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
2014 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
2015 return aff;
2018 isl_int_init(gcd);
2019 isl_int_gcd(gcd, aff->v->el[0], f);
2020 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
2021 isl_int_divexact(gcd, f, gcd);
2022 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2023 isl_int_clear(gcd);
2025 return aff;
2028 /* Multiple "aff" by "v".
2030 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
2031 __isl_take isl_val *v)
2033 if (!aff || !v)
2034 goto error;
2036 if (isl_val_is_one(v)) {
2037 isl_val_free(v);
2038 return aff;
2041 if (!isl_val_is_rat(v))
2042 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2043 "expecting rational factor", goto error);
2045 aff = isl_aff_scale(aff, v->n);
2046 aff = isl_aff_scale_down(aff, v->d);
2048 isl_val_free(v);
2049 return aff;
2050 error:
2051 isl_aff_free(aff);
2052 isl_val_free(v);
2053 return NULL;
2056 /* Return the result of scaling "aff" down by a factor of "f".
2058 * As a special case, NaN/f = NaN.
2060 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2062 isl_int gcd;
2064 if (!aff)
2065 return NULL;
2066 if (isl_aff_is_nan(aff))
2067 return aff;
2069 if (isl_int_is_one(f))
2070 return aff;
2072 aff = isl_aff_cow(aff);
2073 if (!aff)
2074 return NULL;
2076 if (isl_int_is_zero(f))
2077 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2078 "cannot scale down by zero", return isl_aff_free(aff));
2080 aff->v = isl_vec_cow(aff->v);
2081 if (!aff->v)
2082 return isl_aff_free(aff);
2084 isl_int_init(gcd);
2085 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2086 isl_int_gcd(gcd, gcd, f);
2087 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2088 isl_int_divexact(gcd, f, gcd);
2089 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2090 isl_int_clear(gcd);
2092 return aff;
2095 /* Divide "aff" by "v".
2097 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2098 __isl_take isl_val *v)
2100 if (!aff || !v)
2101 goto error;
2103 if (isl_val_is_one(v)) {
2104 isl_val_free(v);
2105 return aff;
2108 if (!isl_val_is_rat(v))
2109 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2110 "expecting rational factor", goto error);
2111 if (!isl_val_is_pos(v))
2112 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2113 "factor needs to be positive", goto error);
2115 aff = isl_aff_scale(aff, v->d);
2116 aff = isl_aff_scale_down(aff, v->n);
2118 isl_val_free(v);
2119 return aff;
2120 error:
2121 isl_aff_free(aff);
2122 isl_val_free(v);
2123 return NULL;
2126 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2128 isl_int v;
2130 if (f == 1)
2131 return aff;
2133 isl_int_init(v);
2134 isl_int_set_ui(v, f);
2135 aff = isl_aff_scale_down(aff, v);
2136 isl_int_clear(v);
2138 return aff;
2141 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2142 enum isl_dim_type type, unsigned pos, const char *s)
2144 aff = isl_aff_cow(aff);
2145 if (!aff)
2146 return NULL;
2147 if (type == isl_dim_out)
2148 isl_die(aff->v->ctx, isl_error_invalid,
2149 "cannot set name of output/set dimension",
2150 return isl_aff_free(aff));
2151 if (type == isl_dim_in)
2152 type = isl_dim_set;
2153 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2154 if (!aff->ls)
2155 return isl_aff_free(aff);
2157 return aff;
2160 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2161 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2163 aff = isl_aff_cow(aff);
2164 if (!aff)
2165 goto error;
2166 if (type == isl_dim_out)
2167 isl_die(aff->v->ctx, isl_error_invalid,
2168 "cannot set name of output/set dimension",
2169 goto error);
2170 if (type == isl_dim_in)
2171 type = isl_dim_set;
2172 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2173 if (!aff->ls)
2174 return isl_aff_free(aff);
2176 return aff;
2177 error:
2178 isl_id_free(id);
2179 isl_aff_free(aff);
2180 return NULL;
2183 /* Replace the identifier of the input tuple of "aff" by "id".
2184 * type is currently required to be equal to isl_dim_in
2186 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2187 enum isl_dim_type type, __isl_take isl_id *id)
2189 aff = isl_aff_cow(aff);
2190 if (!aff)
2191 goto error;
2192 if (type != isl_dim_in)
2193 isl_die(aff->v->ctx, isl_error_invalid,
2194 "cannot only set id of input tuple", goto error);
2195 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2196 if (!aff->ls)
2197 return isl_aff_free(aff);
2199 return aff;
2200 error:
2201 isl_id_free(id);
2202 isl_aff_free(aff);
2203 return NULL;
2206 /* Exploit the equalities in "eq" to simplify the affine expression
2207 * and the expressions of the integer divisions in the local space.
2208 * The integer divisions in this local space are assumed to appear
2209 * as regular dimensions in "eq".
2211 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2212 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2214 int i, j;
2215 unsigned o_div;
2216 unsigned n_div;
2218 if (!eq)
2219 goto error;
2220 if (eq->n_eq == 0) {
2221 isl_basic_set_free(eq);
2222 return aff;
2225 aff = isl_aff_cow(aff);
2226 if (!aff)
2227 goto error;
2229 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2230 isl_basic_set_copy(eq));
2231 aff->v = isl_vec_cow(aff->v);
2232 if (!aff->ls || !aff->v)
2233 goto error;
2235 o_div = isl_basic_set_offset(eq, isl_dim_div);
2236 n_div = eq->n_div;
2237 for (i = 0; i < eq->n_eq; ++i) {
2238 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2239 if (j < 0 || j == 0 || j >= o_div)
2240 continue;
2242 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2243 &aff->v->el[0]);
2246 isl_basic_set_free(eq);
2247 aff = isl_aff_normalize(aff);
2248 return aff;
2249 error:
2250 isl_basic_set_free(eq);
2251 isl_aff_free(aff);
2252 return NULL;
2255 /* Exploit the equalities in "eq" to simplify the affine expression
2256 * and the expressions of the integer divisions in the local space.
2258 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2259 __isl_take isl_basic_set *eq)
2261 isl_size n_div;
2263 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2264 if (n_div < 0)
2265 goto error;
2266 if (n_div > 0)
2267 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2268 return isl_aff_substitute_equalities_lifted(aff, eq);
2269 error:
2270 isl_basic_set_free(eq);
2271 isl_aff_free(aff);
2272 return NULL;
2275 /* Look for equalities among the variables shared by context and aff
2276 * and the integer divisions of aff, if any.
2277 * The equalities are then used to eliminate coefficients and/or integer
2278 * divisions from aff.
2280 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2281 __isl_take isl_set *context)
2283 isl_local_space *ls;
2284 isl_basic_set *hull;
2286 ls = isl_aff_get_domain_local_space(aff);
2287 context = isl_local_space_lift_set(ls, context);
2289 hull = isl_set_affine_hull(context);
2290 return isl_aff_substitute_equalities_lifted(aff, hull);
2293 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2294 __isl_take isl_set *context)
2296 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2297 dom_context = isl_set_intersect_params(dom_context, context);
2298 return isl_aff_gist(aff, dom_context);
2301 /* Return a basic set containing those elements in the space
2302 * of aff where it is positive. "rational" should not be set.
2304 * If "aff" is NaN, then it is not positive.
2306 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2307 int rational, void *user)
2309 isl_constraint *ineq;
2310 isl_basic_set *bset;
2311 isl_val *c;
2313 if (!aff)
2314 return NULL;
2315 if (isl_aff_is_nan(aff)) {
2316 isl_space *space = isl_aff_get_domain_space(aff);
2317 isl_aff_free(aff);
2318 return isl_basic_set_empty(space);
2320 if (rational)
2321 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2322 "rational sets not supported", goto error);
2324 ineq = isl_inequality_from_aff(aff);
2325 c = isl_constraint_get_constant_val(ineq);
2326 c = isl_val_sub_ui(c, 1);
2327 ineq = isl_constraint_set_constant_val(ineq, c);
2329 bset = isl_basic_set_from_constraint(ineq);
2330 bset = isl_basic_set_simplify(bset);
2331 return bset;
2332 error:
2333 isl_aff_free(aff);
2334 return NULL;
2337 /* Return a basic set containing those elements in the space
2338 * of aff where it is non-negative.
2339 * If "rational" is set, then return a rational basic set.
2341 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2343 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2344 __isl_take isl_aff *aff, int rational, void *user)
2346 isl_constraint *ineq;
2347 isl_basic_set *bset;
2349 if (!aff)
2350 return NULL;
2351 if (isl_aff_is_nan(aff)) {
2352 isl_space *space = isl_aff_get_domain_space(aff);
2353 isl_aff_free(aff);
2354 return isl_basic_set_empty(space);
2357 ineq = isl_inequality_from_aff(aff);
2359 bset = isl_basic_set_from_constraint(ineq);
2360 if (rational)
2361 bset = isl_basic_set_set_rational(bset);
2362 bset = isl_basic_set_simplify(bset);
2363 return bset;
2366 /* Return a basic set containing those elements in the space
2367 * of aff where it is non-negative.
2369 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2371 return aff_nonneg_basic_set(aff, 0, NULL);
2374 /* Return a basic set containing those elements in the domain space
2375 * of "aff" where it is positive.
2377 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2379 aff = isl_aff_add_constant_num_si(aff, -1);
2380 return isl_aff_nonneg_basic_set(aff);
2383 /* Return a basic set containing those elements in the domain space
2384 * of aff where it is negative.
2386 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2388 aff = isl_aff_neg(aff);
2389 return isl_aff_pos_basic_set(aff);
2392 /* Return a basic set containing those elements in the space
2393 * of aff where it is zero.
2394 * If "rational" is set, then return a rational basic set.
2396 * If "aff" is NaN, then it is not zero.
2398 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2399 int rational, void *user)
2401 isl_constraint *ineq;
2402 isl_basic_set *bset;
2404 if (!aff)
2405 return NULL;
2406 if (isl_aff_is_nan(aff)) {
2407 isl_space *space = isl_aff_get_domain_space(aff);
2408 isl_aff_free(aff);
2409 return isl_basic_set_empty(space);
2412 ineq = isl_equality_from_aff(aff);
2414 bset = isl_basic_set_from_constraint(ineq);
2415 if (rational)
2416 bset = isl_basic_set_set_rational(bset);
2417 bset = isl_basic_set_simplify(bset);
2418 return bset;
2421 /* Return a basic set containing those elements in the space
2422 * of aff where it is zero.
2424 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2426 return aff_zero_basic_set(aff, 0, NULL);
2429 /* Return a basic set containing those elements in the shared space
2430 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2432 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2433 __isl_take isl_aff *aff2)
2435 aff1 = isl_aff_sub(aff1, aff2);
2437 return isl_aff_nonneg_basic_set(aff1);
2440 /* Return a basic set containing those elements in the shared domain space
2441 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2443 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2444 __isl_take isl_aff *aff2)
2446 aff1 = isl_aff_sub(aff1, aff2);
2448 return isl_aff_pos_basic_set(aff1);
2451 /* Return a set containing those elements in the shared space
2452 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2454 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2455 __isl_take isl_aff *aff2)
2457 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2460 /* Return a set containing those elements in the shared domain space
2461 * of aff1 and aff2 where aff1 is greater than aff2.
2463 * If either of the two inputs is NaN, then the result is empty,
2464 * as comparisons with NaN always return false.
2466 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2467 __isl_take isl_aff *aff2)
2469 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2472 /* Return a basic set containing those elements in the shared space
2473 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2475 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2476 __isl_take isl_aff *aff2)
2478 return isl_aff_ge_basic_set(aff2, aff1);
2481 /* Return a basic set containing those elements in the shared domain space
2482 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2484 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2485 __isl_take isl_aff *aff2)
2487 return isl_aff_gt_basic_set(aff2, aff1);
2490 /* Return a set containing those elements in the shared space
2491 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2493 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2494 __isl_take isl_aff *aff2)
2496 return isl_aff_ge_set(aff2, aff1);
2499 /* Return a set containing those elements in the shared domain space
2500 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2502 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2503 __isl_take isl_aff *aff2)
2505 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2508 /* Return a basic set containing those elements in the shared space
2509 * of aff1 and aff2 where aff1 and aff2 are equal.
2511 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2512 __isl_take isl_aff *aff2)
2514 aff1 = isl_aff_sub(aff1, aff2);
2516 return isl_aff_zero_basic_set(aff1);
2519 /* Return a set containing those elements in the shared space
2520 * of aff1 and aff2 where aff1 and aff2 are equal.
2522 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2523 __isl_take isl_aff *aff2)
2525 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2528 /* Return a set containing those elements in the shared domain space
2529 * of aff1 and aff2 where aff1 and aff2 are not equal.
2531 * If either of the two inputs is NaN, then the result is empty,
2532 * as comparisons with NaN always return false.
2534 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2535 __isl_take isl_aff *aff2)
2537 isl_set *set_lt, *set_gt;
2539 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2540 isl_aff_copy(aff2));
2541 set_gt = isl_aff_gt_set(aff1, aff2);
2542 return isl_set_union_disjoint(set_lt, set_gt);
2545 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2546 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2548 aff1 = isl_aff_add(aff1, aff2);
2549 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2550 return aff1;
2553 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2555 if (!aff)
2556 return isl_bool_error;
2558 return isl_bool_false;
2561 #undef TYPE
2562 #define TYPE isl_aff
2563 static
2564 #include "check_type_range_templ.c"
2566 /* Check whether the given affine expression has non-zero coefficient
2567 * for any dimension in the given range or if any of these dimensions
2568 * appear with non-zero coefficients in any of the integer divisions
2569 * involved in the affine expression.
2571 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2572 enum isl_dim_type type, unsigned first, unsigned n)
2574 int i;
2575 int *active = NULL;
2576 isl_bool involves = isl_bool_false;
2578 if (!aff)
2579 return isl_bool_error;
2580 if (n == 0)
2581 return isl_bool_false;
2582 if (isl_aff_check_range(aff, type, first, n) < 0)
2583 return isl_bool_error;
2585 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2586 if (!active)
2587 goto error;
2589 first += isl_local_space_offset(aff->ls, type) - 1;
2590 for (i = 0; i < n; ++i)
2591 if (active[first + i]) {
2592 involves = isl_bool_true;
2593 break;
2596 free(active);
2598 return involves;
2599 error:
2600 free(active);
2601 return isl_bool_error;
2604 /* Does "aff" involve any local variables, i.e., integer divisions?
2606 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2608 isl_size n;
2610 n = isl_aff_dim(aff, isl_dim_div);
2611 if (n < 0)
2612 return isl_bool_error;
2613 return isl_bool_ok(n > 0);
2616 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2617 enum isl_dim_type type, unsigned first, unsigned n)
2619 if (!aff)
2620 return NULL;
2621 if (type == isl_dim_out)
2622 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2623 "cannot drop output/set dimension",
2624 return isl_aff_free(aff));
2625 if (type == isl_dim_in)
2626 type = isl_dim_set;
2627 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2628 return aff;
2630 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2631 return isl_aff_free(aff);
2633 aff = isl_aff_cow(aff);
2634 if (!aff)
2635 return NULL;
2637 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2638 if (!aff->ls)
2639 return isl_aff_free(aff);
2641 first += 1 + isl_local_space_offset(aff->ls, type);
2642 aff->v = isl_vec_drop_els(aff->v, first, n);
2643 if (!aff->v)
2644 return isl_aff_free(aff);
2646 return aff;
2649 /* Is the domain of "aff" a product?
2651 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2653 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2656 #undef TYPE
2657 #define TYPE isl_aff
2658 #include <isl_domain_factor_templ.c>
2660 /* Project the domain of the affine expression onto its parameter space.
2661 * The affine expression may not involve any of the domain dimensions.
2663 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2665 isl_space *space;
2666 isl_size n;
2668 n = isl_aff_dim(aff, isl_dim_in);
2669 if (n < 0)
2670 return isl_aff_free(aff);
2671 aff = isl_aff_drop_domain(aff, 0, n);
2672 space = isl_aff_get_domain_space(aff);
2673 space = isl_space_params(space);
2674 aff = isl_aff_reset_domain_space(aff, space);
2675 return aff;
2678 /* Convert an affine expression defined over a parameter domain
2679 * into one that is defined over a zero-dimensional set.
2681 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2683 isl_local_space *ls;
2685 ls = isl_aff_take_domain_local_space(aff);
2686 ls = isl_local_space_set_from_params(ls);
2687 aff = isl_aff_restore_domain_local_space(aff, ls);
2689 return aff;
2692 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2693 enum isl_dim_type type, unsigned first, unsigned n)
2695 if (!aff)
2696 return NULL;
2697 if (type == isl_dim_out)
2698 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2699 "cannot insert output/set dimensions",
2700 return isl_aff_free(aff));
2701 if (type == isl_dim_in)
2702 type = isl_dim_set;
2703 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2704 return aff;
2706 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2707 return isl_aff_free(aff);
2709 aff = isl_aff_cow(aff);
2710 if (!aff)
2711 return NULL;
2713 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2714 if (!aff->ls)
2715 return isl_aff_free(aff);
2717 first += 1 + isl_local_space_offset(aff->ls, type);
2718 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2719 if (!aff->v)
2720 return isl_aff_free(aff);
2722 return aff;
2725 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2726 enum isl_dim_type type, unsigned n)
2728 isl_size pos;
2730 pos = isl_aff_dim(aff, type);
2731 if (pos < 0)
2732 return isl_aff_free(aff);
2734 return isl_aff_insert_dims(aff, type, pos, n);
2737 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2738 * to dimensions of "dst_type" at "dst_pos".
2740 * We only support moving input dimensions to parameters and vice versa.
2742 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2743 enum isl_dim_type dst_type, unsigned dst_pos,
2744 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2746 unsigned g_dst_pos;
2747 unsigned g_src_pos;
2748 isl_size src_off, dst_off;
2750 if (!aff)
2751 return NULL;
2752 if (n == 0 &&
2753 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2754 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2755 return aff;
2757 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2758 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2759 "cannot move output/set dimension",
2760 return isl_aff_free(aff));
2761 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2762 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2763 "cannot move divs", return isl_aff_free(aff));
2764 if (dst_type == isl_dim_in)
2765 dst_type = isl_dim_set;
2766 if (src_type == isl_dim_in)
2767 src_type = isl_dim_set;
2769 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2770 return isl_aff_free(aff);
2771 if (dst_type == src_type)
2772 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2773 "moving dims within the same type not supported",
2774 return isl_aff_free(aff));
2776 aff = isl_aff_cow(aff);
2777 src_off = isl_aff_domain_offset(aff, src_type);
2778 dst_off = isl_aff_domain_offset(aff, dst_type);
2779 if (src_off < 0 || dst_off < 0)
2780 return isl_aff_free(aff);
2782 g_src_pos = 1 + src_off + src_pos;
2783 g_dst_pos = 1 + dst_off + dst_pos;
2784 if (dst_type > src_type)
2785 g_dst_pos -= n;
2787 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2788 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2789 src_type, src_pos, n);
2790 if (!aff->v || !aff->ls)
2791 return isl_aff_free(aff);
2793 aff = sort_divs(aff);
2795 return aff;
2798 /* Return a zero isl_aff in the given space.
2800 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2801 * interface over all piecewise types.
2803 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2805 isl_local_space *ls;
2807 ls = isl_local_space_from_space(isl_space_domain(space));
2808 return isl_aff_zero_on_domain(ls);
2811 #define isl_aff_involves_nan isl_aff_is_nan
2813 #undef PW
2814 #define PW isl_pw_aff
2815 #undef BASE
2816 #define BASE aff
2817 #undef EL_IS_ZERO
2818 #define EL_IS_ZERO is_empty
2819 #undef ZERO
2820 #define ZERO empty
2821 #undef IS_ZERO
2822 #define IS_ZERO is_empty
2823 #undef FIELD
2824 #define FIELD aff
2825 #undef DEFAULT_IS_ZERO
2826 #define DEFAULT_IS_ZERO 0
2828 #include <isl_pw_templ.c>
2829 #include <isl_pw_un_op_templ.c>
2830 #include <isl_pw_add_constant_val_templ.c>
2831 #include <isl_pw_bind_domain_templ.c>
2832 #include <isl_pw_eval.c>
2833 #include <isl_pw_hash.c>
2834 #include <isl_pw_insert_dims_templ.c>
2835 #include <isl_pw_insert_domain_templ.c>
2836 #include <isl_pw_move_dims_templ.c>
2837 #include <isl_pw_neg_templ.c>
2838 #include <isl_pw_pullback_templ.c>
2839 #include <isl_pw_sub_templ.c>
2840 #include <isl_pw_union_opt.c>
2842 #undef BASE
2843 #define BASE pw_aff
2845 #include <isl_union_single.c>
2846 #include <isl_union_neg.c>
2848 #undef BASE
2849 #define BASE aff
2851 #include <isl_union_pw_templ.c>
2853 /* Compute a piecewise quasi-affine expression with a domain that
2854 * is the union of those of pwaff1 and pwaff2 and such that on each
2855 * cell, the quasi-affine expression is the maximum of those of pwaff1
2856 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2857 * cell, then the associated expression is the defined one.
2859 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2860 __isl_take isl_pw_aff *pwaff2)
2862 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2863 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2866 /* Compute a piecewise quasi-affine expression with a domain that
2867 * is the union of those of pwaff1 and pwaff2 and such that on each
2868 * cell, the quasi-affine expression is the minimum of those of pwaff1
2869 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2870 * cell, then the associated expression is the defined one.
2872 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2873 __isl_take isl_pw_aff *pwaff2)
2875 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2876 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2879 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2880 __isl_take isl_pw_aff *pwaff2, int max)
2882 if (max)
2883 return isl_pw_aff_union_max(pwaff1, pwaff2);
2884 else
2885 return isl_pw_aff_union_min(pwaff1, pwaff2);
2888 /* Is the domain of "pa" a product?
2890 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2892 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2895 #undef TYPE
2896 #define TYPE isl_pw_aff
2897 #include <isl_domain_factor_templ.c>
2899 /* Return a set containing those elements in the domain
2900 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2901 * does not satisfy "fn" (if complement is 1).
2903 * The pieces with a NaN never belong to the result since
2904 * NaN does not satisfy any property.
2906 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2907 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2908 void *user),
2909 int complement, void *user)
2911 int i;
2912 isl_set *set;
2914 if (!pwaff)
2915 return NULL;
2917 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2919 for (i = 0; i < pwaff->n; ++i) {
2920 isl_basic_set *bset;
2921 isl_set *set_i, *locus;
2922 isl_bool rational;
2924 if (isl_aff_is_nan(pwaff->p[i].aff))
2925 continue;
2927 rational = isl_set_has_rational(pwaff->p[i].set);
2928 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2929 locus = isl_set_from_basic_set(bset);
2930 set_i = isl_set_copy(pwaff->p[i].set);
2931 if (complement)
2932 set_i = isl_set_subtract(set_i, locus);
2933 else
2934 set_i = isl_set_intersect(set_i, locus);
2935 set = isl_set_union_disjoint(set, set_i);
2938 isl_pw_aff_free(pwaff);
2940 return set;
2943 /* Return a set containing those elements in the domain
2944 * of "pa" where it is positive.
2946 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2948 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2951 /* Return a set containing those elements in the domain
2952 * of pwaff where it is non-negative.
2954 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2956 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2959 /* Return a set containing those elements in the domain
2960 * of pwaff where it is zero.
2962 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2964 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2967 /* Return a set containing those elements in the domain
2968 * of pwaff where it is not zero.
2970 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2972 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2975 /* Bind the affine function "aff" to the parameter "id",
2976 * returning the elements in the domain where the affine expression
2977 * is equal to the parameter.
2979 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2980 __isl_take isl_id *id)
2982 isl_space *space;
2983 isl_aff *aff_id;
2985 space = isl_aff_get_domain_space(aff);
2986 space = isl_space_add_param_id(space, isl_id_copy(id));
2988 aff = isl_aff_align_params(aff, isl_space_copy(space));
2989 aff_id = isl_aff_param_on_domain_space_id(space, id);
2991 return isl_aff_eq_basic_set(aff, aff_id);
2994 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2995 * "rational" should not be set.
2997 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2998 int rational, void *user)
3000 isl_id *id = user;
3002 if (!aff)
3003 return NULL;
3004 if (rational)
3005 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
3006 "rational binding not supported", goto error);
3007 return isl_aff_bind_id(aff, isl_id_copy(id));
3008 error:
3009 isl_aff_free(aff);
3010 return NULL;
3013 /* Bind the piecewise affine function "pa" to the parameter "id",
3014 * returning the elements in the domain where the expression
3015 * is equal to the parameter.
3017 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
3018 __isl_take isl_id *id)
3020 isl_set *bound;
3022 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
3023 isl_id_free(id);
3025 return bound;
3028 /* Return a set containing those elements in the shared domain
3029 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3031 * We compute the difference on the shared domain and then construct
3032 * the set of values where this difference is non-negative.
3033 * If strict is set, we first subtract 1 from the difference.
3034 * If equal is set, we only return the elements where pwaff1 and pwaff2
3035 * are equal.
3037 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3038 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3040 isl_set *set1, *set2;
3042 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3043 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3044 set1 = isl_set_intersect(set1, set2);
3045 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3046 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3047 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3049 if (strict) {
3050 isl_space *space = isl_set_get_space(set1);
3051 isl_aff *aff;
3052 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3053 aff = isl_aff_add_constant_si(aff, -1);
3054 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3055 } else
3056 isl_set_free(set1);
3058 if (equal)
3059 return isl_pw_aff_zero_set(pwaff1);
3060 return isl_pw_aff_nonneg_set(pwaff1);
3063 /* Return a set containing those elements in the shared domain
3064 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3066 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3067 __isl_take isl_pw_aff *pwaff2)
3069 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3070 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3073 /* Return a set containing those elements in the shared domain
3074 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3076 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3077 __isl_take isl_pw_aff *pwaff2)
3079 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3080 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3083 /* Return a set containing those elements in the shared domain
3084 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3086 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3087 __isl_take isl_pw_aff *pwaff2)
3089 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3090 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3093 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3094 __isl_take isl_pw_aff *pwaff2)
3096 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3099 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3100 __isl_take isl_pw_aff *pwaff2)
3102 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3105 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3106 * where the function values are ordered in the same way as "order",
3107 * which returns a set in the shared domain of its two arguments.
3109 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3110 * We first pull back the two functions such that they are defined on
3111 * the domain [A -> B]. Then we apply "order", resulting in a set
3112 * in the space [A -> B]. Finally, we unwrap this set to obtain
3113 * a map in the space A -> B.
3115 static __isl_give isl_map *isl_pw_aff_order_map(
3116 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3117 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3118 __isl_take isl_pw_aff *pa2))
3120 isl_space *space1, *space2;
3121 isl_multi_aff *ma;
3122 isl_set *set;
3124 isl_pw_aff_align_params_bin(&pa1, &pa2);
3125 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3126 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3127 space1 = isl_space_map_from_domain_and_range(space1, space2);
3128 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3129 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3130 ma = isl_multi_aff_range_map(space1);
3131 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3132 set = order(pa1, pa2);
3134 return isl_set_unwrap(set);
3137 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3138 * where the function values are equal.
3140 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3141 __isl_take isl_pw_aff *pa2)
3143 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3146 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3147 * where the function value of "pa1" is less than or equal to
3148 * the function value of "pa2".
3150 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3151 __isl_take isl_pw_aff *pa2)
3153 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3156 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3157 * where the function value of "pa1" is less than the function value of "pa2".
3159 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3160 __isl_take isl_pw_aff *pa2)
3162 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3165 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3166 * where the function value of "pa1" is greater than or equal to
3167 * the function value of "pa2".
3169 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3170 __isl_take isl_pw_aff *pa2)
3172 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3175 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3176 * where the function value of "pa1" is greater than the function value
3177 * of "pa2".
3179 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3180 __isl_take isl_pw_aff *pa2)
3182 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3185 /* Return a set containing those elements in the shared domain
3186 * of the elements of list1 and list2 where each element in list1
3187 * has the relation specified by "fn" with each element in list2.
3189 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3190 __isl_take isl_pw_aff_list *list2,
3191 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3192 __isl_take isl_pw_aff *pwaff2))
3194 int i, j;
3195 isl_ctx *ctx;
3196 isl_set *set;
3198 if (!list1 || !list2)
3199 goto error;
3201 ctx = isl_pw_aff_list_get_ctx(list1);
3202 if (list1->n < 1 || list2->n < 1)
3203 isl_die(ctx, isl_error_invalid,
3204 "list should contain at least one element", goto error);
3206 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3207 for (i = 0; i < list1->n; ++i)
3208 for (j = 0; j < list2->n; ++j) {
3209 isl_set *set_ij;
3211 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3212 isl_pw_aff_copy(list2->p[j]));
3213 set = isl_set_intersect(set, set_ij);
3216 isl_pw_aff_list_free(list1);
3217 isl_pw_aff_list_free(list2);
3218 return set;
3219 error:
3220 isl_pw_aff_list_free(list1);
3221 isl_pw_aff_list_free(list2);
3222 return NULL;
3225 /* Return a set containing those elements in the shared domain
3226 * of the elements of list1 and list2 where each element in list1
3227 * is equal to each element in list2.
3229 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3230 __isl_take isl_pw_aff_list *list2)
3232 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3235 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3236 __isl_take isl_pw_aff_list *list2)
3238 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3241 /* Return a set containing those elements in the shared domain
3242 * of the elements of list1 and list2 where each element in list1
3243 * is less than or equal to each element in list2.
3245 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3246 __isl_take isl_pw_aff_list *list2)
3248 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3251 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3252 __isl_take isl_pw_aff_list *list2)
3254 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3257 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3258 __isl_take isl_pw_aff_list *list2)
3260 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3263 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3264 __isl_take isl_pw_aff_list *list2)
3266 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3270 /* Return a set containing those elements in the shared domain
3271 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3273 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3274 __isl_take isl_pw_aff *pwaff2)
3276 isl_set *set_lt, *set_gt;
3278 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3279 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3280 isl_pw_aff_copy(pwaff2));
3281 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3282 return isl_set_union_disjoint(set_lt, set_gt);
3285 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3286 isl_int v)
3288 int i;
3290 if (isl_int_is_one(v))
3291 return pwaff;
3292 if (!isl_int_is_pos(v))
3293 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3294 "factor needs to be positive",
3295 return isl_pw_aff_free(pwaff));
3296 pwaff = isl_pw_aff_cow(pwaff);
3297 if (!pwaff)
3298 return NULL;
3299 if (pwaff->n == 0)
3300 return pwaff;
3302 for (i = 0; i < pwaff->n; ++i) {
3303 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3304 if (!pwaff->p[i].aff)
3305 return isl_pw_aff_free(pwaff);
3308 return pwaff;
3311 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3313 return isl_pw_aff_un_op(pwaff, &isl_aff_floor);
3316 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3318 return isl_pw_aff_un_op(pwaff, &isl_aff_ceil);
3321 /* Assuming that "cond1" and "cond2" are disjoint,
3322 * return an affine expression that is equal to pwaff1 on cond1
3323 * and to pwaff2 on cond2.
3325 static __isl_give isl_pw_aff *isl_pw_aff_select(
3326 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3327 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3329 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3330 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3332 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3335 /* Return an affine expression that is equal to pwaff_true for elements
3336 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3337 * is zero.
3338 * That is, return cond ? pwaff_true : pwaff_false;
3340 * If "cond" involves and NaN, then we conservatively return a NaN
3341 * on its entire domain. In principle, we could consider the pieces
3342 * where it is NaN separately from those where it is not.
3344 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3345 * then only use the domain of "cond" to restrict the domain.
3347 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3348 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3350 isl_set *cond_true, *cond_false;
3351 isl_bool equal;
3353 if (!cond)
3354 goto error;
3355 if (isl_pw_aff_involves_nan(cond)) {
3356 isl_space *space = isl_pw_aff_get_domain_space(cond);
3357 isl_local_space *ls = isl_local_space_from_space(space);
3358 isl_pw_aff_free(cond);
3359 isl_pw_aff_free(pwaff_true);
3360 isl_pw_aff_free(pwaff_false);
3361 return isl_pw_aff_nan_on_domain(ls);
3364 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3365 isl_pw_aff_get_space(pwaff_false));
3366 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3367 isl_pw_aff_get_space(pwaff_true));
3368 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3369 if (equal < 0)
3370 goto error;
3371 if (equal) {
3372 isl_set *dom;
3374 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3375 isl_pw_aff_free(pwaff_false);
3376 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3379 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3380 cond_false = isl_pw_aff_zero_set(cond);
3381 return isl_pw_aff_select(cond_true, pwaff_true,
3382 cond_false, pwaff_false);
3383 error:
3384 isl_pw_aff_free(cond);
3385 isl_pw_aff_free(pwaff_true);
3386 isl_pw_aff_free(pwaff_false);
3387 return NULL;
3390 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3392 int pos;
3394 if (!aff)
3395 return isl_bool_error;
3397 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3398 return isl_bool_ok(pos == -1);
3401 /* Check whether pwaff is a piecewise constant.
3403 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3405 int i;
3407 if (!pwaff)
3408 return isl_bool_error;
3410 for (i = 0; i < pwaff->n; ++i) {
3411 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3412 if (is_cst < 0 || !is_cst)
3413 return is_cst;
3416 return isl_bool_true;
3419 /* Return the product of "aff1" and "aff2".
3421 * If either of the two is NaN, then the result is NaN.
3423 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3425 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3426 __isl_take isl_aff *aff2)
3428 if (!aff1 || !aff2)
3429 goto error;
3431 if (isl_aff_is_nan(aff1)) {
3432 isl_aff_free(aff2);
3433 return aff1;
3435 if (isl_aff_is_nan(aff2)) {
3436 isl_aff_free(aff1);
3437 return aff2;
3440 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3441 return isl_aff_mul(aff2, aff1);
3443 if (!isl_aff_is_cst(aff2))
3444 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3445 "at least one affine expression should be constant",
3446 goto error);
3448 aff1 = isl_aff_cow(aff1);
3449 if (!aff1 || !aff2)
3450 goto error;
3452 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3453 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3455 isl_aff_free(aff2);
3456 return aff1;
3457 error:
3458 isl_aff_free(aff1);
3459 isl_aff_free(aff2);
3460 return NULL;
3463 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3465 * If either of the two is NaN, then the result is NaN.
3466 * A division by zero also results in NaN.
3468 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3469 __isl_take isl_aff *aff2)
3471 isl_bool is_cst, is_zero;
3472 int neg;
3474 if (!aff1 || !aff2)
3475 goto error;
3477 if (isl_aff_is_nan(aff1)) {
3478 isl_aff_free(aff2);
3479 return aff1;
3481 if (isl_aff_is_nan(aff2)) {
3482 isl_aff_free(aff1);
3483 return aff2;
3486 is_cst = isl_aff_is_cst(aff2);
3487 if (is_cst < 0)
3488 goto error;
3489 if (!is_cst)
3490 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3491 "second argument should be a constant", goto error);
3492 is_zero = isl_aff_plain_is_zero(aff2);
3493 if (is_zero < 0)
3494 goto error;
3495 if (is_zero)
3496 return set_nan_free(aff1, aff2);
3498 neg = isl_int_is_neg(aff2->v->el[1]);
3499 if (neg) {
3500 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3501 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3504 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3505 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3507 if (neg) {
3508 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3509 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3512 isl_aff_free(aff2);
3513 return aff1;
3514 error:
3515 isl_aff_free(aff1);
3516 isl_aff_free(aff2);
3517 return NULL;
3520 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3521 __isl_take isl_pw_aff *pwaff2)
3523 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3524 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3527 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3528 __isl_take isl_pw_aff *pwaff2)
3530 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3533 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3534 __isl_take isl_pw_aff *pwaff2)
3536 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3537 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3540 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3542 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3543 __isl_take isl_pw_aff *pa2)
3545 int is_cst;
3547 is_cst = isl_pw_aff_is_cst(pa2);
3548 if (is_cst < 0)
3549 goto error;
3550 if (!is_cst)
3551 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3552 "second argument should be a piecewise constant",
3553 goto error);
3554 isl_pw_aff_align_params_bin(&pa1, &pa2);
3555 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3556 error:
3557 isl_pw_aff_free(pa1);
3558 isl_pw_aff_free(pa2);
3559 return NULL;
3562 /* Compute the quotient of the integer division of "pa1" by "pa2"
3563 * with rounding towards zero.
3564 * "pa2" is assumed to be a piecewise constant.
3566 * In particular, return
3568 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3571 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3572 __isl_take isl_pw_aff *pa2)
3574 int is_cst;
3575 isl_set *cond;
3576 isl_pw_aff *f, *c;
3578 is_cst = isl_pw_aff_is_cst(pa2);
3579 if (is_cst < 0)
3580 goto error;
3581 if (!is_cst)
3582 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3583 "second argument should be a piecewise constant",
3584 goto error);
3586 pa1 = isl_pw_aff_div(pa1, pa2);
3588 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3589 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3590 c = isl_pw_aff_ceil(pa1);
3591 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3592 error:
3593 isl_pw_aff_free(pa1);
3594 isl_pw_aff_free(pa2);
3595 return NULL;
3598 /* Compute the remainder of the integer division of "pa1" by "pa2"
3599 * with rounding towards zero.
3600 * "pa2" is assumed to be a piecewise constant.
3602 * In particular, return
3604 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3607 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3608 __isl_take isl_pw_aff *pa2)
3610 int is_cst;
3611 isl_pw_aff *res;
3613 is_cst = isl_pw_aff_is_cst(pa2);
3614 if (is_cst < 0)
3615 goto error;
3616 if (!is_cst)
3617 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3618 "second argument should be a piecewise constant",
3619 goto error);
3620 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3621 res = isl_pw_aff_mul(pa2, res);
3622 res = isl_pw_aff_sub(pa1, res);
3623 return res;
3624 error:
3625 isl_pw_aff_free(pa1);
3626 isl_pw_aff_free(pa2);
3627 return NULL;
3630 /* Does either of "pa1" or "pa2" involve any NaN2?
3632 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3633 __isl_keep isl_pw_aff *pa2)
3635 isl_bool has_nan;
3637 has_nan = isl_pw_aff_involves_nan(pa1);
3638 if (has_nan < 0 || has_nan)
3639 return has_nan;
3640 return isl_pw_aff_involves_nan(pa2);
3643 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3644 * by a NaN on their shared domain.
3646 * In principle, the result could be refined to only being NaN
3647 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3649 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3650 __isl_take isl_pw_aff *pa2)
3652 isl_local_space *ls;
3653 isl_set *dom;
3654 isl_pw_aff *pa;
3656 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3657 ls = isl_local_space_from_space(isl_set_get_space(dom));
3658 pa = isl_pw_aff_nan_on_domain(ls);
3659 pa = isl_pw_aff_intersect_domain(pa, dom);
3661 return pa;
3664 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3665 __isl_take isl_pw_aff *pwaff2)
3667 isl_set *le;
3668 isl_set *dom;
3670 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3671 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3672 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3673 isl_pw_aff_copy(pwaff2));
3674 dom = isl_set_subtract(dom, isl_set_copy(le));
3675 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3678 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3679 __isl_take isl_pw_aff *pwaff2)
3681 isl_set *ge;
3682 isl_set *dom;
3684 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3685 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3686 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3687 isl_pw_aff_copy(pwaff2));
3688 dom = isl_set_subtract(dom, isl_set_copy(ge));
3689 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3692 /* Return an expression for the minimum (if "max" is not set) or
3693 * the maximum (if "max" is set) of "pa1" and "pa2".
3694 * If either expression involves any NaN, then return a NaN
3695 * on the shared domain as result.
3697 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3698 __isl_take isl_pw_aff *pa2, int max)
3700 isl_bool has_nan;
3702 has_nan = either_involves_nan(pa1, pa2);
3703 if (has_nan < 0)
3704 pa1 = isl_pw_aff_free(pa1);
3705 else if (has_nan)
3706 return replace_by_nan(pa1, pa2);
3708 isl_pw_aff_align_params_bin(&pa1, &pa2);
3709 if (max)
3710 return pw_aff_max(pa1, pa2);
3711 else
3712 return pw_aff_min(pa1, pa2);
3715 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3717 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3718 __isl_take isl_pw_aff *pwaff2)
3720 return pw_aff_min_max(pwaff1, pwaff2, 0);
3723 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3725 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3726 __isl_take isl_pw_aff *pwaff2)
3728 return pw_aff_min_max(pwaff1, pwaff2, 1);
3731 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3732 __isl_take isl_pw_aff_list *list,
3733 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3734 __isl_take isl_pw_aff *pwaff2))
3736 int i;
3737 isl_ctx *ctx;
3738 isl_pw_aff *res;
3740 if (!list)
3741 return NULL;
3743 ctx = isl_pw_aff_list_get_ctx(list);
3744 if (list->n < 1)
3745 isl_die(ctx, isl_error_invalid,
3746 "list should contain at least one element", goto error);
3748 res = isl_pw_aff_copy(list->p[0]);
3749 for (i = 1; i < list->n; ++i)
3750 res = fn(res, isl_pw_aff_copy(list->p[i]));
3752 isl_pw_aff_list_free(list);
3753 return res;
3754 error:
3755 isl_pw_aff_list_free(list);
3756 return NULL;
3759 /* Return an isl_pw_aff that maps each element in the intersection of the
3760 * domains of the elements of list to the minimal corresponding affine
3761 * expression.
3763 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3765 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3768 /* Return an isl_pw_aff that maps each element in the intersection of the
3769 * domains of the elements of list to the maximal corresponding affine
3770 * expression.
3772 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3774 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3777 /* Mark the domains of "pwaff" as rational.
3779 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3781 int i;
3783 pwaff = isl_pw_aff_cow(pwaff);
3784 if (!pwaff)
3785 return NULL;
3786 if (pwaff->n == 0)
3787 return pwaff;
3789 for (i = 0; i < pwaff->n; ++i) {
3790 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3791 if (!pwaff->p[i].set)
3792 return isl_pw_aff_free(pwaff);
3795 return pwaff;
3798 /* Mark the domains of the elements of "list" as rational.
3800 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3801 __isl_take isl_pw_aff_list *list)
3803 int i, n;
3805 if (!list)
3806 return NULL;
3807 if (list->n == 0)
3808 return list;
3810 n = list->n;
3811 for (i = 0; i < n; ++i) {
3812 isl_pw_aff *pa;
3814 pa = isl_pw_aff_list_get_pw_aff(list, i);
3815 pa = isl_pw_aff_set_rational(pa);
3816 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3819 return list;
3822 /* Do the parameters of "aff" match those of "space"?
3824 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3825 __isl_keep isl_space *space)
3827 isl_space *aff_space;
3828 isl_bool match;
3830 if (!aff || !space)
3831 return isl_bool_error;
3833 aff_space = isl_aff_get_domain_space(aff);
3835 match = isl_space_has_equal_params(space, aff_space);
3837 isl_space_free(aff_space);
3838 return match;
3841 /* Check that the domain space of "aff" matches "space".
3843 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3844 __isl_keep isl_space *space)
3846 isl_space *aff_space;
3847 isl_bool match;
3849 if (!aff || !space)
3850 return isl_stat_error;
3852 aff_space = isl_aff_get_domain_space(aff);
3854 match = isl_space_has_equal_params(space, aff_space);
3855 if (match < 0)
3856 goto error;
3857 if (!match)
3858 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3859 "parameters don't match", goto error);
3860 match = isl_space_tuple_is_equal(space, isl_dim_in,
3861 aff_space, isl_dim_set);
3862 if (match < 0)
3863 goto error;
3864 if (!match)
3865 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3866 "domains don't match", goto error);
3867 isl_space_free(aff_space);
3868 return isl_stat_ok;
3869 error:
3870 isl_space_free(aff_space);
3871 return isl_stat_error;
3874 /* Return the shared (universe) domain of the elements of "ma".
3876 * Since an isl_multi_aff (and an isl_aff) is always total,
3877 * the domain is always the universe set in its domain space.
3878 * This is a helper function for use in the generic isl_multi_*_bind.
3880 static __isl_give isl_basic_set *isl_multi_aff_domain(
3881 __isl_take isl_multi_aff *ma)
3883 isl_space *space;
3885 space = isl_multi_aff_get_space(ma);
3886 isl_multi_aff_free(ma);
3888 return isl_basic_set_universe(isl_space_domain(space));
3891 #undef BASE
3892 #define BASE aff
3894 #include <isl_multi_no_explicit_domain.c>
3895 #include <isl_multi_templ.c>
3896 #include <isl_multi_un_op_templ.c>
3897 #include <isl_multi_bin_val_templ.c>
3898 #include <isl_multi_add_constant_templ.c>
3899 #include <isl_multi_apply_set.c>
3900 #include <isl_multi_arith_templ.c>
3901 #include <isl_multi_bind_domain_templ.c>
3902 #include <isl_multi_cmp.c>
3903 #include <isl_multi_dim_id_templ.c>
3904 #include <isl_multi_dims.c>
3905 #include <isl_multi_floor.c>
3906 #include <isl_multi_from_base_templ.c>
3907 #include <isl_multi_identity_templ.c>
3908 #include <isl_multi_insert_domain_templ.c>
3909 #include <isl_multi_locals_templ.c>
3910 #include <isl_multi_move_dims_templ.c>
3911 #include <isl_multi_nan_templ.c>
3912 #include <isl_multi_product_templ.c>
3913 #include <isl_multi_splice_templ.c>
3914 #include <isl_multi_tuple_id_templ.c>
3915 #include <isl_multi_unbind_params_templ.c>
3916 #include <isl_multi_zero_templ.c>
3918 #undef DOMBASE
3919 #define DOMBASE set
3920 #include <isl_multi_gist.c>
3922 #undef DOMBASE
3923 #define DOMBASE basic_set
3924 #include <isl_multi_bind_templ.c>
3926 /* Construct an isl_multi_aff living in "space" that corresponds
3927 * to the affine transformation matrix "mat".
3929 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3930 __isl_take isl_space *space, __isl_take isl_mat *mat)
3932 isl_ctx *ctx;
3933 isl_local_space *ls = NULL;
3934 isl_multi_aff *ma = NULL;
3935 isl_size n_row, n_col, n_out, total;
3936 int i;
3938 if (!space || !mat)
3939 goto error;
3941 ctx = isl_mat_get_ctx(mat);
3943 n_row = isl_mat_rows(mat);
3944 n_col = isl_mat_cols(mat);
3945 n_out = isl_space_dim(space, isl_dim_out);
3946 total = isl_space_dim(space, isl_dim_all);
3947 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3948 goto error;
3949 if (n_row < 1)
3950 isl_die(ctx, isl_error_invalid,
3951 "insufficient number of rows", goto error);
3952 if (n_col < 1)
3953 isl_die(ctx, isl_error_invalid,
3954 "insufficient number of columns", goto error);
3955 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3956 isl_die(ctx, isl_error_invalid,
3957 "dimension mismatch", goto error);
3959 ma = isl_multi_aff_zero(isl_space_copy(space));
3960 space = isl_space_domain(space);
3961 ls = isl_local_space_from_space(isl_space_copy(space));
3963 for (i = 0; i < n_row - 1; ++i) {
3964 isl_vec *v;
3965 isl_aff *aff;
3967 v = isl_vec_alloc(ctx, 1 + n_col);
3968 if (!v)
3969 goto error;
3970 isl_int_set(v->el[0], mat->row[0][0]);
3971 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3972 v = isl_vec_normalize(v);
3973 aff = isl_aff_alloc_vec_validated(isl_local_space_copy(ls), v);
3974 ma = isl_multi_aff_set_aff(ma, i, aff);
3977 isl_space_free(space);
3978 isl_local_space_free(ls);
3979 isl_mat_free(mat);
3980 return ma;
3981 error:
3982 isl_space_free(space);
3983 isl_local_space_free(ls);
3984 isl_mat_free(mat);
3985 isl_multi_aff_free(ma);
3986 return NULL;
3989 /* Return the constant terms of the affine expressions of "ma".
3991 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3992 __isl_keep isl_multi_aff *ma)
3994 int i;
3995 isl_size n;
3996 isl_space *space;
3997 isl_multi_val *mv;
3999 n = isl_multi_aff_size(ma);
4000 if (n < 0)
4001 return NULL;
4002 space = isl_space_range(isl_multi_aff_get_space(ma));
4003 space = isl_space_drop_all_params(space);
4004 mv = isl_multi_val_zero(space);
4006 for (i = 0; i < n; ++i) {
4007 isl_aff *aff;
4008 isl_val *val;
4010 aff = isl_multi_aff_get_at(ma, i);
4011 val = isl_aff_get_constant_val(aff);
4012 isl_aff_free(aff);
4013 mv = isl_multi_val_set_at(mv, i, val);
4016 return mv;
4019 /* Remove any internal structure of the domain of "ma".
4020 * If there is any such internal structure in the input,
4021 * then the name of the corresponding space is also removed.
4023 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4024 __isl_take isl_multi_aff *ma)
4026 isl_space *space;
4028 if (!ma)
4029 return NULL;
4031 if (!ma->space->nested[0])
4032 return ma;
4034 space = isl_multi_aff_get_space(ma);
4035 space = isl_space_flatten_domain(space);
4036 ma = isl_multi_aff_reset_space(ma, space);
4038 return ma;
4041 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4042 * of the space to its domain.
4044 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4046 int i;
4047 isl_size n_in;
4048 isl_local_space *ls;
4049 isl_multi_aff *ma;
4051 if (!space)
4052 return NULL;
4053 if (!isl_space_is_map(space))
4054 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4055 "not a map space", goto error);
4057 n_in = isl_space_dim(space, isl_dim_in);
4058 if (n_in < 0)
4059 goto error;
4060 space = isl_space_domain_map(space);
4062 ma = isl_multi_aff_alloc(isl_space_copy(space));
4063 if (n_in == 0) {
4064 isl_space_free(space);
4065 return ma;
4068 space = isl_space_domain(space);
4069 ls = isl_local_space_from_space(space);
4070 for (i = 0; i < n_in; ++i) {
4071 isl_aff *aff;
4073 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4074 isl_dim_set, i);
4075 ma = isl_multi_aff_set_aff(ma, i, aff);
4077 isl_local_space_free(ls);
4078 return ma;
4079 error:
4080 isl_space_free(space);
4081 return NULL;
4084 /* This function performs the same operation as isl_multi_aff_domain_map,
4085 * but is considered as a function on an isl_space when exported.
4087 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4088 __isl_take isl_space *space)
4090 return isl_multi_aff_domain_map(space);
4093 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4094 * of the space to its range.
4096 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4098 int i;
4099 isl_size n_in, n_out;
4100 isl_local_space *ls;
4101 isl_multi_aff *ma;
4103 if (!space)
4104 return NULL;
4105 if (!isl_space_is_map(space))
4106 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4107 "not a map space", goto error);
4109 n_in = isl_space_dim(space, isl_dim_in);
4110 n_out = isl_space_dim(space, isl_dim_out);
4111 if (n_in < 0 || n_out < 0)
4112 goto error;
4113 space = isl_space_range_map(space);
4115 ma = isl_multi_aff_alloc(isl_space_copy(space));
4116 if (n_out == 0) {
4117 isl_space_free(space);
4118 return ma;
4121 space = isl_space_domain(space);
4122 ls = isl_local_space_from_space(space);
4123 for (i = 0; i < n_out; ++i) {
4124 isl_aff *aff;
4126 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4127 isl_dim_set, n_in + i);
4128 ma = isl_multi_aff_set_aff(ma, i, aff);
4130 isl_local_space_free(ls);
4131 return ma;
4132 error:
4133 isl_space_free(space);
4134 return NULL;
4137 /* This function performs the same operation as isl_multi_aff_range_map,
4138 * but is considered as a function on an isl_space when exported.
4140 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4141 __isl_take isl_space *space)
4143 return isl_multi_aff_range_map(space);
4146 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4147 * of the space to its domain.
4149 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4150 __isl_take isl_space *space)
4152 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4155 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4156 * but is considered as a function on an isl_space when exported.
4158 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4159 __isl_take isl_space *space)
4161 return isl_pw_multi_aff_domain_map(space);
4164 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4165 * of the space to its range.
4167 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4168 __isl_take isl_space *space)
4170 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4173 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4174 * but is considered as a function on an isl_space when exported.
4176 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4177 __isl_take isl_space *space)
4179 return isl_pw_multi_aff_range_map(space);
4182 /* Given the space of a set and a range of set dimensions,
4183 * construct an isl_multi_aff that projects out those dimensions.
4185 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4186 __isl_take isl_space *space, enum isl_dim_type type,
4187 unsigned first, unsigned n)
4189 int i;
4190 isl_size dim;
4191 isl_local_space *ls;
4192 isl_multi_aff *ma;
4194 if (!space)
4195 return NULL;
4196 if (!isl_space_is_set(space))
4197 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4198 "expecting set space", goto error);
4199 if (type != isl_dim_set)
4200 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4201 "only set dimensions can be projected out", goto error);
4202 if (isl_space_check_range(space, type, first, n) < 0)
4203 goto error;
4205 dim = isl_space_dim(space, isl_dim_set);
4206 if (dim < 0)
4207 goto error;
4209 space = isl_space_from_domain(space);
4210 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4212 if (dim == n)
4213 return isl_multi_aff_alloc(space);
4215 ma = isl_multi_aff_alloc(isl_space_copy(space));
4216 space = isl_space_domain(space);
4217 ls = isl_local_space_from_space(space);
4219 for (i = 0; i < first; ++i) {
4220 isl_aff *aff;
4222 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4223 isl_dim_set, i);
4224 ma = isl_multi_aff_set_aff(ma, i, aff);
4227 for (i = 0; i < dim - (first + n); ++i) {
4228 isl_aff *aff;
4230 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4231 isl_dim_set, first + n + i);
4232 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4235 isl_local_space_free(ls);
4236 return ma;
4237 error:
4238 isl_space_free(space);
4239 return NULL;
4242 /* Given the space of a set and a range of set dimensions,
4243 * construct an isl_pw_multi_aff that projects out those dimensions.
4245 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4246 __isl_take isl_space *space, enum isl_dim_type type,
4247 unsigned first, unsigned n)
4249 isl_multi_aff *ma;
4251 ma = isl_multi_aff_project_out_map(space, type, first, n);
4252 return isl_pw_multi_aff_from_multi_aff(ma);
4255 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4256 * but is considered as a function on an isl_multi_aff when exported.
4258 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4259 __isl_take isl_multi_aff *ma)
4261 return isl_pw_multi_aff_from_multi_aff(ma);
4264 /* Create a piecewise multi-affine expression in the given space that maps each
4265 * input dimension to the corresponding output dimension.
4267 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4268 __isl_take isl_space *space)
4270 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4273 /* Create a piecewise multi expression that maps elements in the given space
4274 * to themselves.
4276 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4277 __isl_take isl_space *space)
4279 isl_multi_aff *ma;
4281 ma = isl_multi_aff_identity_on_domain_space(space);
4282 return isl_pw_multi_aff_from_multi_aff(ma);
4285 /* This function performs the same operation as
4286 * isl_pw_multi_aff_identity_on_domain_space,
4287 * but is considered as a function on an isl_space when exported.
4289 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4290 __isl_take isl_space *space)
4292 return isl_pw_multi_aff_identity_on_domain_space(space);
4295 /* Exploit the equalities in "eq" to simplify the affine expressions.
4297 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4298 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4300 isl_size n;
4301 int i;
4303 n = isl_multi_aff_size(maff);
4304 if (n < 0 || !eq)
4305 goto error;
4307 for (i = 0; i < n; ++i) {
4308 isl_aff *aff;
4310 aff = isl_multi_aff_take_at(maff, i);
4311 aff = isl_aff_substitute_equalities(aff,
4312 isl_basic_set_copy(eq));
4313 maff = isl_multi_aff_restore_at(maff, i, aff);
4316 isl_basic_set_free(eq);
4317 return maff;
4318 error:
4319 isl_basic_set_free(eq);
4320 isl_multi_aff_free(maff);
4321 return NULL;
4324 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4325 isl_int f)
4327 isl_size n;
4328 int i;
4330 n = isl_multi_aff_size(maff);
4331 if (n < 0)
4332 return isl_multi_aff_free(maff);
4334 for (i = 0; i < n; ++i) {
4335 isl_aff *aff;
4337 aff = isl_multi_aff_take_at(maff, i);
4338 aff = isl_aff_scale(aff, f);
4339 maff = isl_multi_aff_restore_at(maff, i, aff);
4342 return maff;
4345 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4346 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4348 maff1 = isl_multi_aff_add(maff1, maff2);
4349 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4350 return maff1;
4353 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4355 if (!maff)
4356 return isl_bool_error;
4358 return isl_bool_false;
4361 /* Return the set of domain elements where "ma1" is lexicographically
4362 * smaller than or equal to "ma2".
4364 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4365 __isl_take isl_multi_aff *ma2)
4367 return isl_multi_aff_lex_ge_set(ma2, ma1);
4370 /* Return the set of domain elements where "ma1" is lexicographically
4371 * smaller than "ma2".
4373 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4374 __isl_take isl_multi_aff *ma2)
4376 return isl_multi_aff_lex_gt_set(ma2, ma1);
4379 /* Return the set of domain elements where "ma1" is lexicographically
4380 * greater than to "ma2". If "equal" is set, then include the domain
4381 * elements where they are equal.
4382 * Do this for the case where there are no entries.
4383 * In this case, "ma1" cannot be greater than "ma2",
4384 * but it is (greater than or) equal to "ma2".
4386 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4387 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4389 isl_space *space;
4391 space = isl_multi_aff_get_domain_space(ma1);
4393 isl_multi_aff_free(ma1);
4394 isl_multi_aff_free(ma2);
4396 if (equal)
4397 return isl_set_universe(space);
4398 else
4399 return isl_set_empty(space);
4402 /* Return the set where entry "i" of "ma1" and "ma2"
4403 * satisfy the relation prescribed by "cmp".
4405 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4406 __isl_keep isl_multi_aff *ma2, int i,
4407 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4408 __isl_take isl_aff *aff2))
4410 isl_aff *aff1, *aff2;
4412 aff1 = isl_multi_aff_get_at(ma1, i);
4413 aff2 = isl_multi_aff_get_at(ma2, i);
4414 return cmp(aff1, aff2);
4417 /* Return the set of domain elements where "ma1" is lexicographically
4418 * greater than to "ma2". If "equal" is set, then include the domain
4419 * elements where they are equal.
4421 * In particular, for all but the final entry,
4422 * include the set of elements where this entry is strictly greater in "ma1"
4423 * and all previous entries are equal.
4424 * The final entry is also allowed to be equal in the two functions
4425 * if "equal" is set.
4427 * The case where there are no entries is handled separately.
4429 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4430 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4432 int i;
4433 isl_size n;
4434 isl_space *space;
4435 isl_set *res;
4436 isl_set *equal_set;
4437 isl_set *gte;
4439 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4440 goto error;
4441 n = isl_multi_aff_size(ma1);
4442 if (n < 0)
4443 goto error;
4444 if (n == 0)
4445 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4447 space = isl_multi_aff_get_domain_space(ma1);
4448 res = isl_set_empty(isl_space_copy(space));
4449 equal_set = isl_set_universe(space);
4451 for (i = 0; i + 1 < n; ++i) {
4452 isl_bool empty;
4453 isl_set *gt, *eq;
4455 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4456 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4457 res = isl_set_union(res, gt);
4458 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4459 equal_set = isl_set_intersect(equal_set, eq);
4461 empty = isl_set_is_empty(equal_set);
4462 if (empty >= 0 && empty)
4463 break;
4466 if (equal)
4467 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4468 else
4469 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4470 isl_multi_aff_free(ma1);
4471 isl_multi_aff_free(ma2);
4473 gte = isl_set_intersect(gte, equal_set);
4474 return isl_set_union(res, gte);
4475 error:
4476 isl_multi_aff_free(ma1);
4477 isl_multi_aff_free(ma2);
4478 return NULL;
4481 /* Return the set of domain elements where "ma1" is lexicographically
4482 * greater than or equal to "ma2".
4484 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4485 __isl_take isl_multi_aff *ma2)
4487 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4490 /* Return the set of domain elements where "ma1" is lexicographically
4491 * greater than "ma2".
4493 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4494 __isl_take isl_multi_aff *ma2)
4496 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4499 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4501 #undef PW
4502 #define PW isl_pw_multi_aff
4503 #undef BASE
4504 #define BASE multi_aff
4505 #undef EL_IS_ZERO
4506 #define EL_IS_ZERO is_empty
4507 #undef ZERO
4508 #define ZERO empty
4509 #undef IS_ZERO
4510 #define IS_ZERO is_empty
4511 #undef FIELD
4512 #define FIELD maff
4513 #undef DEFAULT_IS_ZERO
4514 #define DEFAULT_IS_ZERO 0
4516 #include <isl_pw_templ.c>
4517 #include <isl_pw_un_op_templ.c>
4518 #include <isl_pw_add_constant_multi_val_templ.c>
4519 #include <isl_pw_add_constant_val_templ.c>
4520 #include <isl_pw_bind_domain_templ.c>
4521 #include <isl_pw_insert_dims_templ.c>
4522 #include <isl_pw_insert_domain_templ.c>
4523 #include <isl_pw_locals_templ.c>
4524 #include <isl_pw_move_dims_templ.c>
4525 #include <isl_pw_neg_templ.c>
4526 #include <isl_pw_pullback_templ.c>
4527 #include <isl_pw_range_tuple_id_templ.c>
4528 #include <isl_pw_union_opt.c>
4530 #undef BASE
4531 #define BASE pw_multi_aff
4533 #include <isl_union_multi.c>
4534 #include "isl_union_locals_templ.c"
4535 #include <isl_union_neg.c>
4537 #undef BASE
4538 #define BASE multi_aff
4540 #include <isl_union_pw_templ.c>
4542 /* Generic function for extracting a factor from a product "pma".
4543 * "check_space" checks that the space is that of the right kind of product.
4544 * "space_factor" extracts the factor from the space.
4545 * "multi_aff_factor" extracts the factor from the constituent functions.
4547 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4548 __isl_take isl_pw_multi_aff *pma,
4549 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4550 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4551 __isl_give isl_multi_aff *(*multi_aff_factor)(
4552 __isl_take isl_multi_aff *ma))
4554 int i;
4555 isl_space *space;
4557 if (check_space(pma) < 0)
4558 return isl_pw_multi_aff_free(pma);
4560 space = isl_pw_multi_aff_take_space(pma);
4561 space = space_factor(space);
4563 for (i = 0; pma && i < pma->n; ++i) {
4564 isl_multi_aff *ma;
4566 ma = isl_pw_multi_aff_take_base_at(pma, i);
4567 ma = multi_aff_factor(ma);
4568 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4571 pma = isl_pw_multi_aff_restore_space(pma, space);
4573 return pma;
4576 /* Is the range of "pma" a wrapped relation?
4578 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4579 __isl_keep isl_pw_multi_aff *pma)
4581 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4584 /* Check that the range of "pma" is a product.
4586 static isl_stat pw_multi_aff_check_range_product(
4587 __isl_keep isl_pw_multi_aff *pma)
4589 isl_bool wraps;
4591 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4592 if (wraps < 0)
4593 return isl_stat_error;
4594 if (!wraps)
4595 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4596 "range is not a product", return isl_stat_error);
4597 return isl_stat_ok;
4600 /* Given a function A -> [B -> C], extract the function A -> B.
4602 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4603 __isl_take isl_pw_multi_aff *pma)
4605 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4606 &isl_space_range_factor_domain,
4607 &isl_multi_aff_range_factor_domain);
4610 /* Given a function A -> [B -> C], extract the function A -> C.
4612 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4613 __isl_take isl_pw_multi_aff *pma)
4615 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4616 &isl_space_range_factor_range,
4617 &isl_multi_aff_range_factor_range);
4620 /* Given two piecewise multi affine expressions, return a piecewise
4621 * multi-affine expression defined on the union of the definition domains
4622 * of the inputs that is equal to the lexicographic maximum of the two
4623 * inputs on each cell. If only one of the two inputs is defined on
4624 * a given cell, then it is considered to be the maximum.
4626 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4627 __isl_take isl_pw_multi_aff *pma1,
4628 __isl_take isl_pw_multi_aff *pma2)
4630 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4631 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4632 &isl_multi_aff_lex_ge_set);
4635 /* Given two piecewise multi affine expressions, return a piecewise
4636 * multi-affine expression defined on the union of the definition domains
4637 * of the inputs that is equal to the lexicographic minimum of the two
4638 * inputs on each cell. If only one of the two inputs is defined on
4639 * a given cell, then it is considered to be the minimum.
4641 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4642 __isl_take isl_pw_multi_aff *pma1,
4643 __isl_take isl_pw_multi_aff *pma2)
4645 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4646 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4647 &isl_multi_aff_lex_le_set);
4650 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4651 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4653 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4654 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4655 &isl_multi_aff_add);
4658 /* Subtract "pma2" from "pma1" and return the result.
4660 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4661 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4663 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4664 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4665 &isl_multi_aff_sub);
4668 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4669 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4671 return isl_pw_multi_aff_union_add_(pma1, pma2);
4674 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4675 * with the actual sum on the shared domain and
4676 * the defined expression on the symmetric difference of the domains.
4678 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4679 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4681 return isl_union_pw_aff_union_add_(upa1, upa2);
4684 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4685 * with the actual sum on the shared domain and
4686 * the defined expression on the symmetric difference of the domains.
4688 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4689 __isl_take isl_union_pw_multi_aff *upma1,
4690 __isl_take isl_union_pw_multi_aff *upma2)
4692 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4695 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4696 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4698 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4699 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4701 int i, j, n;
4702 isl_space *space;
4703 isl_pw_multi_aff *res;
4705 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4706 goto error;
4708 n = pma1->n * pma2->n;
4709 space = isl_space_product(isl_space_copy(pma1->dim),
4710 isl_space_copy(pma2->dim));
4711 res = isl_pw_multi_aff_alloc_size(space, n);
4713 for (i = 0; i < pma1->n; ++i) {
4714 for (j = 0; j < pma2->n; ++j) {
4715 isl_set *domain;
4716 isl_multi_aff *ma;
4718 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4719 isl_set_copy(pma2->p[j].set));
4720 ma = isl_multi_aff_product(
4721 isl_multi_aff_copy(pma1->p[i].maff),
4722 isl_multi_aff_copy(pma2->p[j].maff));
4723 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4727 isl_pw_multi_aff_free(pma1);
4728 isl_pw_multi_aff_free(pma2);
4729 return res;
4730 error:
4731 isl_pw_multi_aff_free(pma1);
4732 isl_pw_multi_aff_free(pma2);
4733 return NULL;
4736 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4737 * denominator "denom".
4738 * "denom" is allowed to be negative, in which case the actual denominator
4739 * is -denom and the expressions are added instead.
4741 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4742 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4744 int i, first;
4745 int sign;
4746 isl_int d;
4748 first = isl_seq_first_non_zero(c, n);
4749 if (first == -1)
4750 return aff;
4752 sign = isl_int_sgn(denom);
4753 isl_int_init(d);
4754 isl_int_abs(d, denom);
4755 for (i = first; i < n; ++i) {
4756 isl_aff *aff_i;
4758 if (isl_int_is_zero(c[i]))
4759 continue;
4760 aff_i = isl_multi_aff_get_aff(ma, i);
4761 aff_i = isl_aff_scale(aff_i, c[i]);
4762 aff_i = isl_aff_scale_down(aff_i, d);
4763 if (sign >= 0)
4764 aff = isl_aff_sub(aff, aff_i);
4765 else
4766 aff = isl_aff_add(aff, aff_i);
4768 isl_int_clear(d);
4770 return aff;
4773 /* Extract an affine expression that expresses the output dimension "pos"
4774 * of "bmap" in terms of the parameters and input dimensions from
4775 * equality "eq".
4776 * Note that this expression may involve integer divisions defined
4777 * in terms of parameters and input dimensions.
4778 * The equality may also involve references to earlier (but not later)
4779 * output dimensions. These are replaced by the corresponding elements
4780 * in "ma".
4782 * If the equality is of the form
4784 * f(i) + h(j) + a x + g(i) = 0,
4786 * with f(i) a linear combinations of the parameters and input dimensions,
4787 * g(i) a linear combination of integer divisions defined in terms of the same
4788 * and h(j) a linear combinations of earlier output dimensions,
4789 * then the affine expression is
4791 * (-f(i) - g(i))/a - h(j)/a
4793 * If the equality is of the form
4795 * f(i) + h(j) - a x + g(i) = 0,
4797 * then the affine expression is
4799 * (f(i) + g(i))/a - h(j)/(-a)
4802 * If "div" refers to an integer division (i.e., it is smaller than
4803 * the number of integer divisions), then the equality constraint
4804 * does involve an integer division (the one at position "div") that
4805 * is defined in terms of output dimensions. However, this integer
4806 * division can be eliminated by exploiting a pair of constraints
4807 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4808 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4809 * -l + x >= 0.
4810 * In particular, let
4812 * x = e(i) + m floor(...)
4814 * with e(i) the expression derived above and floor(...) the integer
4815 * division involving output dimensions.
4816 * From
4818 * l <= x <= l + n,
4820 * we have
4822 * 0 <= x - l <= n
4824 * This means
4826 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4827 * = (e(i) - l) mod m
4829 * Therefore,
4831 * x - l = (e(i) - l) mod m
4833 * or
4835 * x = ((e(i) - l) mod m) + l
4837 * The variable "shift" below contains the expression -l, which may
4838 * also involve a linear combination of earlier output dimensions.
4840 static __isl_give isl_aff *extract_aff_from_equality(
4841 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4842 __isl_keep isl_multi_aff *ma)
4844 unsigned o_out;
4845 isl_size n_div, n_out;
4846 isl_ctx *ctx;
4847 isl_local_space *ls;
4848 isl_aff *aff, *shift;
4849 isl_val *mod;
4851 ctx = isl_basic_map_get_ctx(bmap);
4852 ls = isl_basic_map_get_local_space(bmap);
4853 ls = isl_local_space_domain(ls);
4854 aff = isl_aff_alloc(isl_local_space_copy(ls));
4855 if (!aff)
4856 goto error;
4857 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4858 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4859 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4860 if (n_out < 0 || n_div < 0)
4861 goto error;
4862 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4863 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4864 isl_seq_cpy(aff->v->el + 1 + o_out,
4865 bmap->eq[eq] + o_out + n_out, n_div);
4866 } else {
4867 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4868 isl_seq_neg(aff->v->el + 1 + o_out,
4869 bmap->eq[eq] + o_out + n_out, n_div);
4871 if (div < n_div)
4872 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4873 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4874 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4875 bmap->eq[eq][o_out + pos]);
4876 if (div < n_div) {
4877 shift = isl_aff_alloc(isl_local_space_copy(ls));
4878 if (!shift)
4879 goto error;
4880 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4881 isl_seq_cpy(shift->v->el + 1 + o_out,
4882 bmap->ineq[ineq] + o_out + n_out, n_div);
4883 isl_int_set_si(shift->v->el[0], 1);
4884 shift = subtract_initial(shift, ma, pos,
4885 bmap->ineq[ineq] + o_out, ctx->negone);
4886 aff = isl_aff_add(aff, isl_aff_copy(shift));
4887 mod = isl_val_int_from_isl_int(ctx,
4888 bmap->eq[eq][o_out + n_out + div]);
4889 mod = isl_val_abs(mod);
4890 aff = isl_aff_mod_val(aff, mod);
4891 aff = isl_aff_sub(aff, shift);
4894 isl_local_space_free(ls);
4895 return aff;
4896 error:
4897 isl_local_space_free(ls);
4898 isl_aff_free(aff);
4899 return NULL;
4902 /* Given a basic map with output dimensions defined
4903 * in terms of the parameters input dimensions and earlier
4904 * output dimensions using an equality (and possibly a pair on inequalities),
4905 * extract an isl_aff that expresses output dimension "pos" in terms
4906 * of the parameters and input dimensions.
4907 * Note that this expression may involve integer divisions defined
4908 * in terms of parameters and input dimensions.
4909 * "ma" contains the expressions corresponding to earlier output dimensions.
4911 * This function shares some similarities with
4912 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4914 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4915 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4917 int eq, div, ineq;
4918 isl_aff *aff;
4920 if (!bmap)
4921 return NULL;
4922 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4923 if (eq >= bmap->n_eq)
4924 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4925 "unable to find suitable equality", return NULL);
4926 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4928 aff = isl_aff_remove_unused_divs(aff);
4929 return aff;
4932 /* Given a basic map where each output dimension is defined
4933 * in terms of the parameters and input dimensions using an equality,
4934 * extract an isl_multi_aff that expresses the output dimensions in terms
4935 * of the parameters and input dimensions.
4937 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4938 __isl_take isl_basic_map *bmap)
4940 int i;
4941 isl_size n_out;
4942 isl_multi_aff *ma;
4944 if (!bmap)
4945 return NULL;
4947 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4948 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4949 if (n_out < 0)
4950 ma = isl_multi_aff_free(ma);
4952 for (i = 0; i < n_out; ++i) {
4953 isl_aff *aff;
4955 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4956 ma = isl_multi_aff_set_aff(ma, i, aff);
4959 isl_basic_map_free(bmap);
4961 return ma;
4964 /* Given a basic set where each set dimension is defined
4965 * in terms of the parameters using an equality,
4966 * extract an isl_multi_aff that expresses the set dimensions in terms
4967 * of the parameters.
4969 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4970 __isl_take isl_basic_set *bset)
4972 return extract_isl_multi_aff_from_basic_map(bset);
4975 /* Create an isl_pw_multi_aff that is equivalent to
4976 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4977 * The given basic map is such that each output dimension is defined
4978 * in terms of the parameters and input dimensions using an equality.
4980 * Since some applications expect the result of isl_pw_multi_aff_from_map
4981 * to only contain integer affine expressions, we compute the floor
4982 * of the expression before returning.
4984 * Remove all constraints involving local variables without
4985 * an explicit representation (resulting in the removal of those
4986 * local variables) prior to the actual extraction to ensure
4987 * that the local spaces in which the resulting affine expressions
4988 * are created do not contain any unknown local variables.
4989 * Removing such constraints is safe because constraints involving
4990 * unknown local variables are not used to determine whether
4991 * a basic map is obviously single-valued.
4993 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4994 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4996 isl_multi_aff *ma;
4998 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4999 ma = extract_isl_multi_aff_from_basic_map(bmap);
5000 ma = isl_multi_aff_floor(ma);
5001 return isl_pw_multi_aff_alloc(domain, ma);
5004 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5005 * This obviously only works if the input "map" is single-valued.
5006 * If so, we compute the lexicographic minimum of the image in the form
5007 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5008 * to its lexicographic minimum.
5009 * If the input is not single-valued, we produce an error.
5011 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
5012 __isl_take isl_map *map)
5014 int i;
5015 int sv;
5016 isl_pw_multi_aff *pma;
5018 sv = isl_map_is_single_valued(map);
5019 if (sv < 0)
5020 goto error;
5021 if (!sv)
5022 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5023 "map is not single-valued", goto error);
5024 map = isl_map_make_disjoint(map);
5025 if (!map)
5026 return NULL;
5028 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5030 for (i = 0; i < map->n; ++i) {
5031 isl_pw_multi_aff *pma_i;
5032 isl_basic_map *bmap;
5033 bmap = isl_basic_map_copy(map->p[i]);
5034 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5035 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5038 isl_map_free(map);
5039 return pma;
5040 error:
5041 isl_map_free(map);
5042 return NULL;
5045 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5046 * taking into account that the output dimension at position "d"
5047 * can be represented as
5049 * x = floor((e(...) + c1) / m)
5051 * given that constraint "i" is of the form
5053 * e(...) + c1 - m x >= 0
5056 * Let "map" be of the form
5058 * A -> B
5060 * We construct a mapping
5062 * A -> [A -> x = floor(...)]
5064 * apply that to the map, obtaining
5066 * [A -> x = floor(...)] -> B
5068 * and equate dimension "d" to x.
5069 * We then compute a isl_pw_multi_aff representation of the resulting map
5070 * and plug in the mapping above.
5072 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5073 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5075 isl_ctx *ctx;
5076 isl_space *space = NULL;
5077 isl_local_space *ls;
5078 isl_multi_aff *ma;
5079 isl_aff *aff;
5080 isl_vec *v;
5081 isl_map *insert;
5082 int offset;
5083 isl_size n;
5084 isl_size n_in;
5085 isl_pw_multi_aff *pma;
5086 isl_bool is_set;
5088 is_set = isl_map_is_set(map);
5089 if (is_set < 0)
5090 goto error;
5092 offset = isl_basic_map_offset(hull, isl_dim_out);
5093 ctx = isl_map_get_ctx(map);
5094 space = isl_space_domain(isl_map_get_space(map));
5095 n_in = isl_space_dim(space, isl_dim_set);
5096 n = isl_space_dim(space, isl_dim_all);
5097 if (n_in < 0 || n < 0)
5098 goto error;
5100 v = isl_vec_alloc(ctx, 1 + 1 + n);
5101 if (v) {
5102 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5103 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5105 isl_basic_map_free(hull);
5107 ls = isl_local_space_from_space(isl_space_copy(space));
5108 aff = isl_aff_alloc_vec_validated(ls, v);
5109 aff = isl_aff_floor(aff);
5110 if (is_set) {
5111 isl_space_free(space);
5112 ma = isl_multi_aff_from_aff(aff);
5113 } else {
5114 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5115 ma = isl_multi_aff_range_product(ma,
5116 isl_multi_aff_from_aff(aff));
5119 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5120 map = isl_map_apply_domain(map, insert);
5121 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5122 pma = isl_pw_multi_aff_from_map(map);
5123 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5125 return pma;
5126 error:
5127 isl_space_free(space);
5128 isl_map_free(map);
5129 isl_basic_map_free(hull);
5130 return NULL;
5133 /* Is constraint "c" of the form
5135 * e(...) + c1 - m x >= 0
5137 * or
5139 * -e(...) + c2 + m x >= 0
5141 * where m > 1 and e only depends on parameters and input dimensions?
5143 * "offset" is the offset of the output dimensions
5144 * "pos" is the position of output dimension x.
5146 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5148 if (isl_int_is_zero(c[offset + d]))
5149 return 0;
5150 if (isl_int_is_one(c[offset + d]))
5151 return 0;
5152 if (isl_int_is_negone(c[offset + d]))
5153 return 0;
5154 if (isl_seq_first_non_zero(c + offset, d) != -1)
5155 return 0;
5156 if (isl_seq_first_non_zero(c + offset + d + 1,
5157 total - (offset + d + 1)) != -1)
5158 return 0;
5159 return 1;
5162 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5164 * As a special case, we first check if there is any pair of constraints,
5165 * shared by all the basic maps in "map" that force a given dimension
5166 * to be equal to the floor of some affine combination of the input dimensions.
5168 * In particular, if we can find two constraints
5170 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5172 * and
5174 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5176 * where m > 1 and e only depends on parameters and input dimensions,
5177 * and such that
5179 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5181 * then we know that we can take
5183 * x = floor((e(...) + c1) / m)
5185 * without having to perform any computation.
5187 * Note that we know that
5189 * c1 + c2 >= 1
5191 * If c1 + c2 were 0, then we would have detected an equality during
5192 * simplification. If c1 + c2 were negative, then we would have detected
5193 * a contradiction.
5195 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5196 __isl_take isl_map *map)
5198 int d;
5199 isl_size dim;
5200 int i, j, n;
5201 int offset;
5202 isl_size total;
5203 isl_int sum;
5204 isl_basic_map *hull;
5206 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5207 dim = isl_map_dim(map, isl_dim_out);
5208 total = isl_basic_map_dim(hull, isl_dim_all);
5209 if (dim < 0 || total < 0)
5210 goto error;
5212 isl_int_init(sum);
5213 offset = isl_basic_map_offset(hull, isl_dim_out);
5214 n = hull->n_ineq;
5215 for (d = 0; d < dim; ++d) {
5216 for (i = 0; i < n; ++i) {
5217 if (!is_potential_div_constraint(hull->ineq[i],
5218 offset, d, 1 + total))
5219 continue;
5220 for (j = i + 1; j < n; ++j) {
5221 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5222 hull->ineq[j] + 1, total))
5223 continue;
5224 isl_int_add(sum, hull->ineq[i][0],
5225 hull->ineq[j][0]);
5226 if (isl_int_abs_lt(sum,
5227 hull->ineq[i][offset + d]))
5228 break;
5231 if (j >= n)
5232 continue;
5233 isl_int_clear(sum);
5234 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5235 j = i;
5236 return pw_multi_aff_from_map_div(map, hull, d, j);
5239 isl_int_clear(sum);
5240 isl_basic_map_free(hull);
5241 return pw_multi_aff_from_map_base(map);
5242 error:
5243 isl_map_free(map);
5244 isl_basic_map_free(hull);
5245 return NULL;
5248 /* Given an affine expression
5250 * [A -> B] -> f(A,B)
5252 * construct an isl_multi_aff
5254 * [A -> B] -> B'
5256 * such that dimension "d" in B' is set to "aff" and the remaining
5257 * dimensions are set equal to the corresponding dimensions in B.
5258 * "n_in" is the dimension of the space A.
5259 * "n_out" is the dimension of the space B.
5261 * If "is_set" is set, then the affine expression is of the form
5263 * [B] -> f(B)
5265 * and we construct an isl_multi_aff
5267 * B -> B'
5269 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5270 unsigned n_in, unsigned n_out, int is_set)
5272 int i;
5273 isl_multi_aff *ma;
5274 isl_space *space, *space2;
5275 isl_local_space *ls;
5277 space = isl_aff_get_domain_space(aff);
5278 ls = isl_local_space_from_space(isl_space_copy(space));
5279 space2 = isl_space_copy(space);
5280 if (!is_set)
5281 space2 = isl_space_range(isl_space_unwrap(space2));
5282 space = isl_space_map_from_domain_and_range(space, space2);
5283 ma = isl_multi_aff_alloc(space);
5284 ma = isl_multi_aff_set_aff(ma, d, aff);
5286 for (i = 0; i < n_out; ++i) {
5287 if (i == d)
5288 continue;
5289 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5290 isl_dim_set, n_in + i);
5291 ma = isl_multi_aff_set_aff(ma, i, aff);
5294 isl_local_space_free(ls);
5296 return ma;
5299 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5300 * taking into account that the dimension at position "d" can be written as
5302 * x = m a + f(..) (1)
5304 * where m is equal to "gcd".
5305 * "i" is the index of the equality in "hull" that defines f(..).
5306 * In particular, the equality is of the form
5308 * f(..) - x + m g(existentials) = 0
5310 * or
5312 * -f(..) + x + m g(existentials) = 0
5314 * We basically plug (1) into "map", resulting in a map with "a"
5315 * in the range instead of "x". The corresponding isl_pw_multi_aff
5316 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5318 * Specifically, given the input map
5320 * A -> B
5322 * We first wrap it into a set
5324 * [A -> B]
5326 * and define (1) on top of the corresponding space, resulting in "aff".
5327 * We use this to create an isl_multi_aff that maps the output position "d"
5328 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5329 * We plug this into the wrapped map, unwrap the result and compute the
5330 * corresponding isl_pw_multi_aff.
5331 * The result is an expression
5333 * A -> T(A)
5335 * We adjust that to
5337 * A -> [A -> T(A)]
5339 * so that we can plug that into "aff", after extending the latter to
5340 * a mapping
5342 * [A -> B] -> B'
5345 * If "map" is actually a set, then there is no "A" space, meaning
5346 * that we do not need to perform any wrapping, and that the result
5347 * of the recursive call is of the form
5349 * [T]
5351 * which is plugged into a mapping of the form
5353 * B -> B'
5355 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5356 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5357 isl_int gcd)
5359 isl_set *set;
5360 isl_space *space;
5361 isl_local_space *ls;
5362 isl_aff *aff;
5363 isl_multi_aff *ma;
5364 isl_pw_multi_aff *pma, *id;
5365 isl_size n_in;
5366 unsigned o_out;
5367 isl_size n_out;
5368 isl_bool is_set;
5370 is_set = isl_map_is_set(map);
5371 if (is_set < 0)
5372 goto error;
5374 n_in = isl_basic_map_dim(hull, isl_dim_in);
5375 n_out = isl_basic_map_dim(hull, isl_dim_out);
5376 if (n_in < 0 || n_out < 0)
5377 goto error;
5378 o_out = isl_basic_map_offset(hull, isl_dim_out);
5380 if (is_set)
5381 set = map;
5382 else
5383 set = isl_map_wrap(map);
5384 space = isl_space_map_from_set(isl_set_get_space(set));
5385 ma = isl_multi_aff_identity(space);
5386 ls = isl_local_space_from_space(isl_set_get_space(set));
5387 aff = isl_aff_alloc(ls);
5388 if (aff) {
5389 isl_int_set_si(aff->v->el[0], 1);
5390 if (isl_int_is_one(hull->eq[i][o_out + d]))
5391 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5392 aff->v->size - 1);
5393 else
5394 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5395 aff->v->size - 1);
5396 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5398 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5399 set = isl_set_preimage_multi_aff(set, ma);
5401 ma = range_map(aff, d, n_in, n_out, is_set);
5403 if (is_set)
5404 map = set;
5405 else
5406 map = isl_set_unwrap(set);
5407 pma = isl_pw_multi_aff_from_map(map);
5409 if (!is_set) {
5410 space = isl_pw_multi_aff_get_domain_space(pma);
5411 space = isl_space_map_from_set(space);
5412 id = isl_pw_multi_aff_identity(space);
5413 pma = isl_pw_multi_aff_range_product(id, pma);
5415 id = isl_pw_multi_aff_from_multi_aff(ma);
5416 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5418 isl_basic_map_free(hull);
5419 return pma;
5420 error:
5421 isl_map_free(map);
5422 isl_basic_map_free(hull);
5423 return NULL;
5426 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5427 * "hull" contains the equalities valid for "map".
5429 * Check if any of the output dimensions is "strided".
5430 * That is, we check if it can be written as
5432 * x = m a + f(..)
5434 * with m greater than 1, a some combination of existentially quantified
5435 * variables and f an expression in the parameters and input dimensions.
5436 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5438 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5439 * special case.
5441 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5442 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5444 int i, j;
5445 isl_size n_out;
5446 unsigned o_out;
5447 isl_size n_div;
5448 unsigned o_div;
5449 isl_int gcd;
5451 n_div = isl_basic_map_dim(hull, isl_dim_div);
5452 n_out = isl_basic_map_dim(hull, isl_dim_out);
5453 if (n_div < 0 || n_out < 0)
5454 goto error;
5456 if (n_div == 0) {
5457 isl_basic_map_free(hull);
5458 return pw_multi_aff_from_map_check_div(map);
5461 isl_int_init(gcd);
5463 o_div = isl_basic_map_offset(hull, isl_dim_div);
5464 o_out = isl_basic_map_offset(hull, isl_dim_out);
5466 for (i = 0; i < n_out; ++i) {
5467 for (j = 0; j < hull->n_eq; ++j) {
5468 isl_int *eq = hull->eq[j];
5469 isl_pw_multi_aff *res;
5471 if (!isl_int_is_one(eq[o_out + i]) &&
5472 !isl_int_is_negone(eq[o_out + i]))
5473 continue;
5474 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5475 continue;
5476 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5477 n_out - (i + 1)) != -1)
5478 continue;
5479 isl_seq_gcd(eq + o_div, n_div, &gcd);
5480 if (isl_int_is_zero(gcd))
5481 continue;
5482 if (isl_int_is_one(gcd))
5483 continue;
5485 res = pw_multi_aff_from_map_stride(map, hull,
5486 i, j, gcd);
5487 isl_int_clear(gcd);
5488 return res;
5492 isl_int_clear(gcd);
5493 isl_basic_map_free(hull);
5494 return pw_multi_aff_from_map_check_div(map);
5495 error:
5496 isl_map_free(map);
5497 isl_basic_map_free(hull);
5498 return NULL;
5501 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5503 * As a special case, we first check if all output dimensions are uniquely
5504 * defined in terms of the parameters and input dimensions over the entire
5505 * domain. If so, we extract the desired isl_pw_multi_aff directly
5506 * from the affine hull of "map" and its domain.
5508 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5509 * special cases.
5511 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5513 isl_bool sv;
5514 isl_size n;
5515 isl_basic_map *hull;
5517 n = isl_map_n_basic_map(map);
5518 if (n < 0)
5519 goto error;
5521 if (n == 1) {
5522 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5523 hull = isl_basic_map_plain_affine_hull(hull);
5524 sv = isl_basic_map_plain_is_single_valued(hull);
5525 if (sv >= 0 && sv)
5526 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5527 hull);
5528 isl_basic_map_free(hull);
5530 map = isl_map_detect_equalities(map);
5531 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5532 sv = isl_basic_map_plain_is_single_valued(hull);
5533 if (sv >= 0 && sv)
5534 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5535 if (sv >= 0)
5536 return pw_multi_aff_from_map_check_strides(map, hull);
5537 isl_basic_map_free(hull);
5538 error:
5539 isl_map_free(map);
5540 return NULL;
5543 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5544 * but is considered as a function on an isl_map when exported.
5546 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5548 return isl_pw_multi_aff_from_map(map);
5551 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5553 return isl_pw_multi_aff_from_map(set);
5556 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5557 * but is considered as a function on an isl_set when exported.
5559 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5561 return isl_pw_multi_aff_from_set(set);
5564 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5565 * add it to *user.
5567 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5569 isl_union_pw_multi_aff **upma = user;
5570 isl_pw_multi_aff *pma;
5572 pma = isl_pw_multi_aff_from_map(map);
5573 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5575 return *upma ? isl_stat_ok : isl_stat_error;
5578 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5579 * domain.
5581 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5582 __isl_take isl_aff *aff)
5584 isl_multi_aff *ma;
5585 isl_pw_multi_aff *pma;
5587 ma = isl_multi_aff_from_aff(aff);
5588 pma = isl_pw_multi_aff_from_multi_aff(ma);
5589 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5592 /* Try and create an isl_union_pw_multi_aff that is equivalent
5593 * to the given isl_union_map.
5594 * The isl_union_map is required to be single-valued in each space.
5595 * Otherwise, an error is produced.
5597 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5598 __isl_take isl_union_map *umap)
5600 isl_space *space;
5601 isl_union_pw_multi_aff *upma;
5603 space = isl_union_map_get_space(umap);
5604 upma = isl_union_pw_multi_aff_empty(space);
5605 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5606 upma = isl_union_pw_multi_aff_free(upma);
5607 isl_union_map_free(umap);
5609 return upma;
5612 /* This function performs the same operation as
5613 * isl_union_pw_multi_aff_from_union_map,
5614 * but is considered as a function on an isl_union_map when exported.
5616 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5617 __isl_take isl_union_map *umap)
5619 return isl_union_pw_multi_aff_from_union_map(umap);
5622 /* Try and create an isl_union_pw_multi_aff that is equivalent
5623 * to the given isl_union_set.
5624 * The isl_union_set is required to be a singleton in each space.
5625 * Otherwise, an error is produced.
5627 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5628 __isl_take isl_union_set *uset)
5630 return isl_union_pw_multi_aff_from_union_map(uset);
5633 /* Return the piecewise affine expression "set ? 1 : 0".
5635 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5637 isl_pw_aff *pa;
5638 isl_space *space = isl_set_get_space(set);
5639 isl_local_space *ls = isl_local_space_from_space(space);
5640 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5641 isl_aff *one = isl_aff_zero_on_domain(ls);
5643 one = isl_aff_add_constant_si(one, 1);
5644 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5645 set = isl_set_complement(set);
5646 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5648 return pa;
5651 /* Plug in "subs" for dimension "type", "pos" of "aff".
5653 * Let i be the dimension to replace and let "subs" be of the form
5655 * f/d
5657 * and "aff" of the form
5659 * (a i + g)/m
5661 * The result is
5663 * (a f + d g')/(m d)
5665 * where g' is the result of plugging in "subs" in each of the integer
5666 * divisions in g.
5668 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5669 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5671 isl_ctx *ctx;
5672 isl_int v;
5673 isl_size n_div;
5675 aff = isl_aff_cow(aff);
5676 if (!aff || !subs)
5677 return isl_aff_free(aff);
5679 ctx = isl_aff_get_ctx(aff);
5680 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5681 isl_die(ctx, isl_error_invalid,
5682 "spaces don't match", return isl_aff_free(aff));
5683 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5684 if (n_div < 0)
5685 return isl_aff_free(aff);
5686 if (n_div != 0)
5687 isl_die(ctx, isl_error_unsupported,
5688 "cannot handle divs yet", return isl_aff_free(aff));
5690 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5691 if (!aff->ls)
5692 return isl_aff_free(aff);
5694 aff->v = isl_vec_cow(aff->v);
5695 if (!aff->v)
5696 return isl_aff_free(aff);
5698 pos += isl_local_space_offset(aff->ls, type);
5700 isl_int_init(v);
5701 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5702 aff->v->size, subs->v->size, v);
5703 isl_int_clear(v);
5705 return aff;
5708 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5709 * expressions in "maff".
5711 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5712 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5713 __isl_keep isl_aff *subs)
5715 isl_size n;
5716 int i;
5718 n = isl_multi_aff_size(maff);
5719 if (n < 0 || !subs)
5720 return isl_multi_aff_free(maff);
5722 if (type == isl_dim_in)
5723 type = isl_dim_set;
5725 for (i = 0; i < n; ++i) {
5726 isl_aff *aff;
5728 aff = isl_multi_aff_take_at(maff, i);
5729 aff = isl_aff_substitute(aff, type, pos, subs);
5730 maff = isl_multi_aff_restore_at(maff, i, aff);
5733 return maff;
5736 /* Plug in "subs" for input dimension "pos" of "pma".
5738 * pma is of the form
5740 * A_i(v) -> M_i(v)
5742 * while subs is of the form
5744 * v' = B_j(v) -> S_j
5746 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5747 * has a contribution in the result, in particular
5749 * C_ij(S_j) -> M_i(S_j)
5751 * Note that plugging in S_j in C_ij may also result in an empty set
5752 * and this contribution should simply be discarded.
5754 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5755 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5756 __isl_keep isl_pw_aff *subs)
5758 int i, j, n;
5759 isl_pw_multi_aff *res;
5761 if (!pma || !subs)
5762 return isl_pw_multi_aff_free(pma);
5764 n = pma->n * subs->n;
5765 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5767 for (i = 0; i < pma->n; ++i) {
5768 for (j = 0; j < subs->n; ++j) {
5769 isl_set *common;
5770 isl_multi_aff *res_ij;
5771 int empty;
5773 common = isl_set_intersect(
5774 isl_set_copy(pma->p[i].set),
5775 isl_set_copy(subs->p[j].set));
5776 common = isl_set_substitute(common,
5777 pos, subs->p[j].aff);
5778 empty = isl_set_plain_is_empty(common);
5779 if (empty < 0 || empty) {
5780 isl_set_free(common);
5781 if (empty < 0)
5782 goto error;
5783 continue;
5786 res_ij = isl_multi_aff_substitute(
5787 isl_multi_aff_copy(pma->p[i].maff),
5788 isl_dim_in, pos, subs->p[j].aff);
5790 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5794 isl_pw_multi_aff_free(pma);
5795 return res;
5796 error:
5797 isl_pw_multi_aff_free(pma);
5798 isl_pw_multi_aff_free(res);
5799 return NULL;
5802 /* Compute the preimage of a range of dimensions in the affine expression "src"
5803 * under "ma" and put the result in "dst". The number of dimensions in "src"
5804 * that precede the range is given by "n_before". The number of dimensions
5805 * in the range is given by the number of output dimensions of "ma".
5806 * The number of dimensions that follow the range is given by "n_after".
5807 * If "has_denom" is set (to one),
5808 * then "src" and "dst" have an extra initial denominator.
5809 * "n_div_ma" is the number of existentials in "ma"
5810 * "n_div_bset" is the number of existentials in "src"
5811 * The resulting "dst" (which is assumed to have been allocated by
5812 * the caller) contains coefficients for both sets of existentials,
5813 * first those in "ma" and then those in "src".
5814 * f, c1, c2 and g are temporary objects that have been initialized
5815 * by the caller.
5817 * Let src represent the expression
5819 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5821 * and let ma represent the expressions
5823 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5825 * We start out with the following expression for dst:
5827 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5829 * with the multiplication factor f initially equal to 1
5830 * and f \sum_i b_i v_i kept separately.
5831 * For each x_i that we substitute, we multiply the numerator
5832 * (and denominator) of dst by c_1 = m_i and add the numerator
5833 * of the x_i expression multiplied by c_2 = f b_i,
5834 * after removing the common factors of c_1 and c_2.
5835 * The multiplication factor f also needs to be multiplied by c_1
5836 * for the next x_j, j > i.
5838 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5839 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5840 int n_div_ma, int n_div_bmap,
5841 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5843 int i;
5844 isl_size n_param, n_in, n_out;
5845 int o_dst, o_src;
5847 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5848 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5849 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5850 if (n_param < 0 || n_in < 0 || n_out < 0)
5851 return isl_stat_error;
5853 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5854 o_dst = o_src = has_denom + 1 + n_param + n_before;
5855 isl_seq_clr(dst + o_dst, n_in);
5856 o_dst += n_in;
5857 o_src += n_out;
5858 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5859 o_dst += n_after;
5860 o_src += n_after;
5861 isl_seq_clr(dst + o_dst, n_div_ma);
5862 o_dst += n_div_ma;
5863 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5865 isl_int_set_si(f, 1);
5867 for (i = 0; i < n_out; ++i) {
5868 int offset = has_denom + 1 + n_param + n_before + i;
5870 if (isl_int_is_zero(src[offset]))
5871 continue;
5872 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5873 isl_int_mul(c2, f, src[offset]);
5874 isl_int_gcd(g, c1, c2);
5875 isl_int_divexact(c1, c1, g);
5876 isl_int_divexact(c2, c2, g);
5878 isl_int_mul(f, f, c1);
5879 o_dst = has_denom;
5880 o_src = 1;
5881 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5882 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5883 o_dst += 1 + n_param;
5884 o_src += 1 + n_param;
5885 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5886 o_dst += n_before;
5887 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5888 c2, ma->u.p[i]->v->el + o_src, n_in);
5889 o_dst += n_in;
5890 o_src += n_in;
5891 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5892 o_dst += n_after;
5893 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5894 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5895 o_dst += n_div_ma;
5896 o_src += n_div_ma;
5897 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5898 if (has_denom)
5899 isl_int_mul(dst[0], dst[0], c1);
5902 return isl_stat_ok;
5905 /* Compute the pullback of "aff" by the function represented by "ma".
5906 * In other words, plug in "ma" in "aff". The result is an affine expression
5907 * defined over the domain space of "ma".
5909 * If "aff" is represented by
5911 * (a(p) + b x + c(divs))/d
5913 * and ma is represented by
5915 * x = D(p) + F(y) + G(divs')
5917 * then the result is
5919 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5921 * The divs in the local space of the input are similarly adjusted
5922 * through a call to isl_local_space_preimage_multi_aff.
5924 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5925 __isl_take isl_multi_aff *ma)
5927 isl_aff *res = NULL;
5928 isl_local_space *ls;
5929 isl_size n_div_aff, n_div_ma;
5930 isl_int f, c1, c2, g;
5932 ma = isl_multi_aff_align_divs(ma);
5933 if (!aff || !ma)
5934 goto error;
5936 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5937 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5938 if (n_div_aff < 0 || n_div_ma < 0)
5939 goto error;
5941 ls = isl_aff_get_domain_local_space(aff);
5942 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5943 res = isl_aff_alloc(ls);
5944 if (!res)
5945 goto error;
5947 isl_int_init(f);
5948 isl_int_init(c1);
5949 isl_int_init(c2);
5950 isl_int_init(g);
5952 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5953 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5954 res = isl_aff_free(res);
5956 isl_int_clear(f);
5957 isl_int_clear(c1);
5958 isl_int_clear(c2);
5959 isl_int_clear(g);
5961 isl_aff_free(aff);
5962 isl_multi_aff_free(ma);
5963 res = isl_aff_normalize(res);
5964 return res;
5965 error:
5966 isl_aff_free(aff);
5967 isl_multi_aff_free(ma);
5968 isl_aff_free(res);
5969 return NULL;
5972 /* Compute the pullback of "aff1" by the function represented by "aff2".
5973 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5974 * defined over the domain space of "aff1".
5976 * The domain of "aff1" should match the range of "aff2", which means
5977 * that it should be single-dimensional.
5979 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5980 __isl_take isl_aff *aff2)
5982 isl_multi_aff *ma;
5984 ma = isl_multi_aff_from_aff(aff2);
5985 return isl_aff_pullback_multi_aff(aff1, ma);
5988 /* Compute the pullback of "ma1" by the function represented by "ma2".
5989 * In other words, plug in "ma2" in "ma1".
5991 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5992 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5994 int i;
5995 isl_size n;
5996 isl_space *space = NULL;
5998 isl_multi_aff_align_params_bin(&ma1, &ma2);
5999 ma2 = isl_multi_aff_align_divs(ma2);
6000 n = isl_multi_aff_size(ma1);
6001 if (n < 0 || !ma2)
6002 goto error;
6004 space = isl_space_join(isl_multi_aff_get_space(ma2),
6005 isl_multi_aff_get_space(ma1));
6007 for (i = 0; i < n; ++i) {
6008 isl_aff *aff;
6010 aff = isl_multi_aff_take_at(ma1, i);
6011 aff = isl_aff_pullback_multi_aff(aff, isl_multi_aff_copy(ma2));
6012 ma1 = isl_multi_aff_restore_at(ma1, i, aff);
6015 ma1 = isl_multi_aff_reset_space(ma1, space);
6016 isl_multi_aff_free(ma2);
6017 return ma1;
6018 error:
6019 isl_space_free(space);
6020 isl_multi_aff_free(ma2);
6021 isl_multi_aff_free(ma1);
6022 return NULL;
6025 /* Extend the local space of "dst" to include the divs
6026 * in the local space of "src".
6028 * If "src" does not have any divs or if the local spaces of "dst" and
6029 * "src" are the same, then no extension is required.
6031 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6032 __isl_keep isl_aff *src)
6034 isl_ctx *ctx;
6035 isl_size src_n_div, dst_n_div;
6036 int *exp1 = NULL;
6037 int *exp2 = NULL;
6038 isl_bool equal;
6039 isl_mat *div;
6041 if (!src || !dst)
6042 return isl_aff_free(dst);
6044 ctx = isl_aff_get_ctx(src);
6045 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6046 if (equal < 0)
6047 return isl_aff_free(dst);
6048 if (!equal)
6049 isl_die(ctx, isl_error_invalid,
6050 "spaces don't match", goto error);
6052 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6053 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6054 if (src_n_div == 0)
6055 return dst;
6056 equal = isl_local_space_is_equal(src->ls, dst->ls);
6057 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6058 return isl_aff_free(dst);
6059 if (equal)
6060 return dst;
6062 exp1 = isl_alloc_array(ctx, int, src_n_div);
6063 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6064 if (!exp1 || (dst_n_div && !exp2))
6065 goto error;
6067 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6068 dst = isl_aff_expand_divs(dst, div, exp2);
6069 free(exp1);
6070 free(exp2);
6072 return dst;
6073 error:
6074 free(exp1);
6075 free(exp2);
6076 return isl_aff_free(dst);
6079 /* Adjust the local spaces of the affine expressions in "maff"
6080 * such that they all have the save divs.
6082 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6083 __isl_take isl_multi_aff *maff)
6085 isl_aff *aff_0;
6086 isl_size n;
6087 int i;
6089 n = isl_multi_aff_size(maff);
6090 if (n < 0)
6091 return isl_multi_aff_free(maff);
6092 if (n <= 1)
6093 return maff;
6095 aff_0 = isl_multi_aff_take_at(maff, 0);
6096 for (i = 1; i < n; ++i) {
6097 isl_aff *aff_i;
6099 aff_i = isl_multi_aff_peek_at(maff, i);
6100 aff_0 = isl_aff_align_divs(aff_0, aff_i);
6102 maff = isl_multi_aff_restore_at(maff, 0, aff_0);
6104 aff_0 = isl_multi_aff_peek_at(maff, 0);
6105 for (i = 1; i < n; ++i) {
6106 isl_aff *aff_i;
6108 aff_i = isl_multi_aff_take_at(maff, i);
6109 aff_i = isl_aff_align_divs(aff_i, aff_0);
6110 maff = isl_multi_aff_restore_at(maff, i, aff_i);
6113 return maff;
6116 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6118 aff = isl_aff_cow(aff);
6119 if (!aff)
6120 return NULL;
6122 aff->ls = isl_local_space_lift(aff->ls);
6123 if (!aff->ls)
6124 return isl_aff_free(aff);
6126 return aff;
6129 /* Lift "maff" to a space with extra dimensions such that the result
6130 * has no more existentially quantified variables.
6131 * If "ls" is not NULL, then *ls is assigned the local space that lies
6132 * at the basis of the lifting applied to "maff".
6134 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6135 __isl_give isl_local_space **ls)
6137 int i;
6138 isl_space *space;
6139 isl_aff *aff;
6140 isl_size n, n_div;
6142 if (ls)
6143 *ls = NULL;
6145 n = isl_multi_aff_size(maff);
6146 if (n < 0)
6147 return isl_multi_aff_free(maff);
6149 if (n == 0) {
6150 if (ls) {
6151 isl_space *space = isl_multi_aff_get_domain_space(maff);
6152 *ls = isl_local_space_from_space(space);
6153 if (!*ls)
6154 return isl_multi_aff_free(maff);
6156 return maff;
6159 maff = isl_multi_aff_align_divs(maff);
6161 aff = isl_multi_aff_peek_at(maff, 0);
6162 n_div = isl_aff_dim(aff, isl_dim_div);
6163 if (n_div < 0)
6164 return isl_multi_aff_free(maff);
6165 space = isl_multi_aff_get_space(maff);
6166 space = isl_space_lift(isl_space_domain(space), n_div);
6167 space = isl_space_extend_domain_with_range(space,
6168 isl_multi_aff_get_space(maff));
6169 maff = isl_multi_aff_restore_space(maff, space);
6171 if (ls) {
6172 aff = isl_multi_aff_peek_at(maff, 0);
6173 *ls = isl_aff_get_domain_local_space(aff);
6174 if (!*ls)
6175 return isl_multi_aff_free(maff);
6178 for (i = 0; i < n; ++i) {
6179 aff = isl_multi_aff_take_at(maff, i);
6180 aff = isl_aff_lift(aff);
6181 maff = isl_multi_aff_restore_at(maff, i, aff);
6184 return maff;
6187 #undef TYPE
6188 #define TYPE isl_pw_multi_aff
6189 static
6190 #include "check_type_range_templ.c"
6192 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6194 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6195 __isl_keep isl_pw_multi_aff *pma, int pos)
6197 int i;
6198 isl_size n_out;
6199 isl_space *space;
6200 isl_pw_aff *pa;
6202 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6203 return NULL;
6205 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6206 if (n_out < 0)
6207 return NULL;
6209 space = isl_pw_multi_aff_get_space(pma);
6210 space = isl_space_drop_dims(space, isl_dim_out,
6211 pos + 1, n_out - pos - 1);
6212 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6214 pa = isl_pw_aff_alloc_size(space, pma->n);
6215 for (i = 0; i < pma->n; ++i) {
6216 isl_aff *aff;
6217 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6218 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6221 return pa;
6224 /* This is an alternative name for the function above.
6226 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6227 __isl_keep isl_pw_multi_aff *pma, int pos)
6229 return isl_pw_multi_aff_get_at(pma, pos);
6232 /* Return an isl_pw_multi_aff with the given "set" as domain and
6233 * an unnamed zero-dimensional range.
6235 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6236 __isl_take isl_set *set)
6238 isl_multi_aff *ma;
6239 isl_space *space;
6241 space = isl_set_get_space(set);
6242 space = isl_space_from_domain(space);
6243 ma = isl_multi_aff_zero(space);
6244 return isl_pw_multi_aff_alloc(set, ma);
6247 /* Add an isl_pw_multi_aff with the given "set" as domain and
6248 * an unnamed zero-dimensional range to *user.
6250 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6251 void *user)
6253 isl_union_pw_multi_aff **upma = user;
6254 isl_pw_multi_aff *pma;
6256 pma = isl_pw_multi_aff_from_domain(set);
6257 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6259 return isl_stat_ok;
6262 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6263 * an unnamed zero-dimensional range.
6265 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6266 __isl_take isl_union_set *uset)
6268 isl_space *space;
6269 isl_union_pw_multi_aff *upma;
6271 if (!uset)
6272 return NULL;
6274 space = isl_union_set_get_space(uset);
6275 upma = isl_union_pw_multi_aff_empty(space);
6277 if (isl_union_set_foreach_set(uset,
6278 &add_pw_multi_aff_from_domain, &upma) < 0)
6279 goto error;
6281 isl_union_set_free(uset);
6282 return upma;
6283 error:
6284 isl_union_set_free(uset);
6285 isl_union_pw_multi_aff_free(upma);
6286 return NULL;
6289 /* Local data for bin_entry and the callback "fn".
6291 struct isl_union_pw_multi_aff_bin_data {
6292 isl_union_pw_multi_aff *upma2;
6293 isl_union_pw_multi_aff *res;
6294 isl_pw_multi_aff *pma;
6295 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6298 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6299 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6301 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6303 struct isl_union_pw_multi_aff_bin_data *data = user;
6304 isl_stat r;
6306 data->pma = pma;
6307 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6308 data->fn, data);
6309 isl_pw_multi_aff_free(pma);
6311 return r;
6314 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6315 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6316 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6317 * as *entry. The callback should adjust data->res if desired.
6319 static __isl_give isl_union_pw_multi_aff *bin_op(
6320 __isl_take isl_union_pw_multi_aff *upma1,
6321 __isl_take isl_union_pw_multi_aff *upma2,
6322 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6324 isl_space *space;
6325 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6327 space = isl_union_pw_multi_aff_get_space(upma2);
6328 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6329 space = isl_union_pw_multi_aff_get_space(upma1);
6330 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6332 if (!upma1 || !upma2)
6333 goto error;
6335 data.upma2 = upma2;
6336 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6337 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6338 &bin_entry, &data) < 0)
6339 goto error;
6341 isl_union_pw_multi_aff_free(upma1);
6342 isl_union_pw_multi_aff_free(upma2);
6343 return data.res;
6344 error:
6345 isl_union_pw_multi_aff_free(upma1);
6346 isl_union_pw_multi_aff_free(upma2);
6347 isl_union_pw_multi_aff_free(data.res);
6348 return NULL;
6351 /* Given two isl_pw_multi_affs A -> B and C -> D,
6352 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6354 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6355 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6357 isl_space *space;
6359 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6360 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6361 isl_pw_multi_aff_get_space(pma2));
6362 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6363 &isl_multi_aff_range_product);
6366 /* Given two isl_pw_multi_affs A -> B and C -> D,
6367 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6369 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6370 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6372 isl_space *space;
6374 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6375 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6376 isl_pw_multi_aff_get_space(pma2));
6377 space = isl_space_flatten_range(space);
6378 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6379 &isl_multi_aff_flat_range_product);
6382 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6383 * to compute some form of range product and add the result to data->res.
6385 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6386 __isl_give isl_pw_multi_aff *(*range_product)(
6387 __isl_take isl_pw_multi_aff *pma1,
6388 __isl_take isl_pw_multi_aff *pma2),
6389 void *user)
6391 struct isl_union_pw_multi_aff_bin_data *data = user;
6392 isl_bool match;
6393 isl_space *space1, *space2;
6395 space1 = isl_pw_multi_aff_peek_space(data->pma);
6396 space2 = isl_pw_multi_aff_peek_space(pma2);
6397 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6398 space2, isl_dim_in);
6399 if (match < 0 || !match) {
6400 isl_pw_multi_aff_free(pma2);
6401 return match < 0 ? isl_stat_error : isl_stat_ok;
6404 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6406 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6408 return isl_stat_ok;
6411 /* If data->pma and "pma2" have the same domain space, then compute
6412 * their flat range product and add the result to data->res.
6414 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6415 void *user)
6417 return gen_range_product_entry(pma2,
6418 &isl_pw_multi_aff_flat_range_product, user);
6421 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6422 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6424 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6425 __isl_take isl_union_pw_multi_aff *upma1,
6426 __isl_take isl_union_pw_multi_aff *upma2)
6428 return bin_op(upma1, upma2, &flat_range_product_entry);
6431 /* If data->pma and "pma2" have the same domain space, then compute
6432 * their range product and add the result to data->res.
6434 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6435 void *user)
6437 return gen_range_product_entry(pma2,
6438 &isl_pw_multi_aff_range_product, user);
6441 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6442 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6444 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6445 __isl_take isl_union_pw_multi_aff *upma1,
6446 __isl_take isl_union_pw_multi_aff *upma2)
6448 return bin_op(upma1, upma2, &range_product_entry);
6451 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6452 * The parameters are assumed to have been aligned.
6454 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6455 * except that it works on two different isl_pw_* types.
6457 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6458 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6459 __isl_take isl_pw_aff *pa)
6461 int i, j, n;
6462 isl_pw_multi_aff *res = NULL;
6464 if (!pma || !pa)
6465 goto error;
6467 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6468 pa->dim, isl_dim_in))
6469 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6470 "domains don't match", goto error);
6471 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6472 goto error;
6474 n = pma->n * pa->n;
6475 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6477 for (i = 0; i < pma->n; ++i) {
6478 for (j = 0; j < pa->n; ++j) {
6479 isl_set *common;
6480 isl_multi_aff *res_ij;
6481 int empty;
6483 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6484 isl_set_copy(pa->p[j].set));
6485 empty = isl_set_plain_is_empty(common);
6486 if (empty < 0 || empty) {
6487 isl_set_free(common);
6488 if (empty < 0)
6489 goto error;
6490 continue;
6493 res_ij = isl_multi_aff_set_aff(
6494 isl_multi_aff_copy(pma->p[i].maff), pos,
6495 isl_aff_copy(pa->p[j].aff));
6496 res_ij = isl_multi_aff_gist(res_ij,
6497 isl_set_copy(common));
6499 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6503 isl_pw_multi_aff_free(pma);
6504 isl_pw_aff_free(pa);
6505 return res;
6506 error:
6507 isl_pw_multi_aff_free(pma);
6508 isl_pw_aff_free(pa);
6509 return isl_pw_multi_aff_free(res);
6512 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6514 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6515 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6516 __isl_take isl_pw_aff *pa)
6518 isl_bool equal_params;
6520 if (!pma || !pa)
6521 goto error;
6522 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6523 if (equal_params < 0)
6524 goto error;
6525 if (equal_params)
6526 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6527 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6528 isl_pw_aff_check_named_params(pa) < 0)
6529 goto error;
6530 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6531 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6532 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6533 error:
6534 isl_pw_multi_aff_free(pma);
6535 isl_pw_aff_free(pa);
6536 return NULL;
6539 /* Do the parameters of "pa" match those of "space"?
6541 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6542 __isl_keep isl_space *space)
6544 isl_space *pa_space;
6545 isl_bool match;
6547 if (!pa || !space)
6548 return isl_bool_error;
6550 pa_space = isl_pw_aff_get_space(pa);
6552 match = isl_space_has_equal_params(space, pa_space);
6554 isl_space_free(pa_space);
6555 return match;
6558 /* Check that the domain space of "pa" matches "space".
6560 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6561 __isl_keep isl_space *space)
6563 isl_space *pa_space;
6564 isl_bool match;
6566 if (!pa || !space)
6567 return isl_stat_error;
6569 pa_space = isl_pw_aff_get_space(pa);
6571 match = isl_space_has_equal_params(space, pa_space);
6572 if (match < 0)
6573 goto error;
6574 if (!match)
6575 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6576 "parameters don't match", goto error);
6577 match = isl_space_tuple_is_equal(space, isl_dim_in,
6578 pa_space, isl_dim_in);
6579 if (match < 0)
6580 goto error;
6581 if (!match)
6582 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6583 "domains don't match", goto error);
6584 isl_space_free(pa_space);
6585 return isl_stat_ok;
6586 error:
6587 isl_space_free(pa_space);
6588 return isl_stat_error;
6591 #undef BASE
6592 #define BASE pw_aff
6593 #undef DOMBASE
6594 #define DOMBASE set
6596 #include <isl_multi_explicit_domain.c>
6597 #include <isl_multi_pw_aff_explicit_domain.c>
6598 #include <isl_multi_templ.c>
6599 #include <isl_multi_un_op_templ.c>
6600 #include <isl_multi_bin_val_templ.c>
6601 #include <isl_multi_add_constant_templ.c>
6602 #include <isl_multi_apply_set.c>
6603 #include <isl_multi_arith_templ.c>
6604 #include <isl_multi_bind_templ.c>
6605 #include <isl_multi_bind_domain_templ.c>
6606 #include <isl_multi_coalesce.c>
6607 #include <isl_multi_domain_templ.c>
6608 #include <isl_multi_dim_id_templ.c>
6609 #include <isl_multi_dims.c>
6610 #include <isl_multi_from_base_templ.c>
6611 #include <isl_multi_gist.c>
6612 #include <isl_multi_hash.c>
6613 #include <isl_multi_identity_templ.c>
6614 #include <isl_multi_align_set.c>
6615 #include <isl_multi_insert_domain_templ.c>
6616 #include <isl_multi_intersect.c>
6617 #include <isl_multi_min_max_templ.c>
6618 #include <isl_multi_move_dims_templ.c>
6619 #include <isl_multi_nan_templ.c>
6620 #include <isl_multi_param_templ.c>
6621 #include <isl_multi_product_templ.c>
6622 #include <isl_multi_splice_templ.c>
6623 #include <isl_multi_tuple_id_templ.c>
6624 #include <isl_multi_union_add_templ.c>
6625 #include <isl_multi_zero_templ.c>
6626 #include <isl_multi_unbind_params_templ.c>
6628 /* Is every element of "mpa" defined over a single universe domain?
6630 isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
6632 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
6635 /* Given that every element of "mpa" is defined over a single universe domain,
6636 * return the corresponding base expressions.
6638 __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
6639 __isl_take isl_multi_pw_aff *mpa)
6641 int i;
6642 isl_size n;
6643 isl_multi_aff *ma;
6645 n = isl_multi_pw_aff_size(mpa);
6646 if (n < 0)
6647 mpa = isl_multi_pw_aff_free(mpa);
6648 ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
6649 for (i = 0; i < n; ++i) {
6650 isl_aff *aff;
6652 aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
6653 ma = isl_multi_aff_set_aff(ma, i, aff);
6655 isl_multi_pw_aff_free(mpa);
6656 return ma;
6659 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6660 * with this explicit domain.
6662 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6663 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6665 isl_set *dom;
6667 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6668 return map;
6670 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6671 map = isl_map_intersect_domain(map, dom);
6673 return map;
6676 /* Are all elements of "mpa" piecewise constants?
6678 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6680 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6683 /* Does "mpa" have a non-trivial explicit domain?
6685 * The explicit domain, if present, is trivial if it represents
6686 * an (obviously) universe set.
6688 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6689 __isl_keep isl_multi_pw_aff *mpa)
6691 if (!mpa)
6692 return isl_bool_error;
6693 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6694 return isl_bool_false;
6695 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6698 #undef BASE
6699 #define BASE set
6701 #include "isl_opt_mpa_templ.c"
6703 /* Compute the minima of the set dimensions as a function of the
6704 * parameters, but independently of the other set dimensions.
6706 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6708 return set_opt_mpa(set, &isl_set_dim_min);
6711 /* Compute the maxima of the set dimensions as a function of the
6712 * parameters, but independently of the other set dimensions.
6714 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6716 return set_opt_mpa(set, &isl_set_dim_max);
6719 #undef BASE
6720 #define BASE map
6722 #include "isl_opt_mpa_templ.c"
6724 /* Compute the minima of the output dimensions as a function of the
6725 * parameters and input dimensions, but independently of
6726 * the other output dimensions.
6728 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6730 return map_opt_mpa(map, &isl_map_dim_min);
6733 /* Compute the maxima of the output dimensions as a function of the
6734 * parameters and input dimensions, but independently of
6735 * the other output dimensions.
6737 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6739 return map_opt_mpa(map, &isl_map_dim_max);
6742 #undef TYPE
6743 #define TYPE isl_pw_multi_aff
6744 #include "isl_type_check_match_range_multi_val.c"
6746 /* Apply "fn" to the base expressions of "pma" and "mv".
6748 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_op_multi_val(
6749 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv,
6750 __isl_give isl_multi_aff *(*fn)(__isl_take isl_multi_aff *ma,
6751 __isl_take isl_multi_val *mv))
6753 int i;
6754 isl_size n;
6756 if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
6757 goto error;
6759 n = isl_pw_multi_aff_n_piece(pma);
6760 if (n < 0)
6761 goto error;
6763 for (i = 0; i < n; ++i) {
6764 isl_multi_aff *ma;
6766 ma = isl_pw_multi_aff_take_base_at(pma, i);
6767 ma = fn(ma, isl_multi_val_copy(mv));
6768 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
6771 isl_multi_val_free(mv);
6772 return pma;
6773 error:
6774 isl_multi_val_free(mv);
6775 isl_pw_multi_aff_free(pma);
6776 return NULL;
6779 /* Scale the elements of "pma" by the corresponding elements of "mv".
6781 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6782 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6784 return isl_pw_multi_aff_op_multi_val(pma, mv,
6785 &isl_multi_aff_scale_multi_val);
6788 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6790 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_multi_val(
6791 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6793 return isl_pw_multi_aff_op_multi_val(pma, mv,
6794 &isl_multi_aff_scale_down_multi_val);
6797 /* This function is called for each entry of an isl_union_pw_multi_aff.
6798 * If the space of the entry matches that of data->mv,
6799 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6800 * Otherwise, return an empty isl_pw_multi_aff.
6802 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6803 __isl_take isl_pw_multi_aff *pma, void *user)
6805 isl_bool equal;
6806 isl_multi_val *mv = user;
6808 equal = isl_pw_multi_aff_match_range_multi_val(pma, mv);
6809 if (equal < 0)
6810 return isl_pw_multi_aff_free(pma);
6811 if (!equal) {
6812 isl_space *space = isl_pw_multi_aff_get_space(pma);
6813 isl_pw_multi_aff_free(pma);
6814 return isl_pw_multi_aff_empty(space);
6817 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6820 /* Scale the elements of "upma" by the corresponding elements of "mv",
6821 * for those entries that match the space of "mv".
6823 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6824 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6826 struct isl_union_pw_multi_aff_transform_control control = {
6827 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6828 .fn_user = mv,
6831 upma = isl_union_pw_multi_aff_align_params(upma,
6832 isl_multi_val_get_space(mv));
6833 mv = isl_multi_val_align_params(mv,
6834 isl_union_pw_multi_aff_get_space(upma));
6835 if (!upma || !mv)
6836 goto error;
6838 return isl_union_pw_multi_aff_transform(upma, &control);
6840 isl_multi_val_free(mv);
6841 return upma;
6842 error:
6843 isl_multi_val_free(mv);
6844 isl_union_pw_multi_aff_free(upma);
6845 return NULL;
6848 /* Construct and return a piecewise multi affine expression
6849 * in the given space with value zero in each of the output dimensions and
6850 * a universe domain.
6852 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6854 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6857 /* Construct and return a piecewise multi affine expression
6858 * that is equal to the given piecewise affine expression.
6860 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6861 __isl_take isl_pw_aff *pa)
6863 int i;
6864 isl_space *space;
6865 isl_pw_multi_aff *pma;
6867 if (!pa)
6868 return NULL;
6870 space = isl_pw_aff_get_space(pa);
6871 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6873 for (i = 0; i < pa->n; ++i) {
6874 isl_set *set;
6875 isl_multi_aff *ma;
6877 set = isl_set_copy(pa->p[i].set);
6878 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6879 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6882 isl_pw_aff_free(pa);
6883 return pma;
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,
6889 * in the special case of a 0D multi piecewise affine expression.
6891 * Create a piecewise multi affine expression with the explicit domain of
6892 * the 0D multi piecewise affine expression as domain.
6894 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6895 __isl_take isl_multi_pw_aff *mpa)
6897 isl_space *space;
6898 isl_set *dom;
6899 isl_multi_aff *ma;
6901 space = isl_multi_pw_aff_get_space(mpa);
6902 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6903 isl_multi_pw_aff_free(mpa);
6905 ma = isl_multi_aff_zero(space);
6906 return isl_pw_multi_aff_alloc(dom, ma);
6909 /* Construct and return a piecewise multi affine expression
6910 * that is equal to the given multi piecewise affine expression
6911 * on the shared domain of the piecewise affine expressions.
6913 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6914 __isl_take isl_multi_pw_aff *mpa)
6916 int i;
6917 isl_space *space;
6918 isl_pw_aff *pa;
6919 isl_pw_multi_aff *pma;
6921 if (!mpa)
6922 return NULL;
6924 if (mpa->n == 0)
6925 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6927 space = isl_multi_pw_aff_get_space(mpa);
6928 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6929 pma = isl_pw_multi_aff_from_pw_aff(pa);
6931 for (i = 1; i < mpa->n; ++i) {
6932 isl_pw_multi_aff *pma_i;
6934 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6935 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6936 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6939 pma = isl_pw_multi_aff_reset_space(pma, space);
6941 isl_multi_pw_aff_free(mpa);
6942 return pma;
6945 /* Convenience function that constructs an isl_multi_pw_aff
6946 * directly from an isl_aff.
6948 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6950 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6953 /* Construct and return a multi piecewise affine expression
6954 * that is equal to the given multi affine expression.
6956 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6957 __isl_take isl_multi_aff *ma)
6959 int i;
6960 isl_size n;
6961 isl_multi_pw_aff *mpa;
6963 n = isl_multi_aff_dim(ma, isl_dim_out);
6964 if (n < 0)
6965 ma = isl_multi_aff_free(ma);
6966 if (!ma)
6967 return NULL;
6969 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6971 for (i = 0; i < n; ++i) {
6972 isl_pw_aff *pa;
6974 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6975 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6978 isl_multi_aff_free(ma);
6979 return mpa;
6982 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6983 * but is considered as a function on an isl_multi_aff when exported.
6985 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
6986 __isl_take isl_multi_aff *ma)
6988 return isl_multi_pw_aff_from_multi_aff(ma);
6991 /* Construct and return a multi piecewise affine expression
6992 * that is equal to the given piecewise multi affine expression.
6994 * If the resulting multi piecewise affine expression has
6995 * an explicit domain, then assign it the domain of the input.
6996 * In other cases, the domain is stored in the individual elements.
6998 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6999 __isl_take isl_pw_multi_aff *pma)
7001 int i;
7002 isl_size n;
7003 isl_space *space;
7004 isl_multi_pw_aff *mpa;
7006 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
7007 if (n < 0)
7008 pma = isl_pw_multi_aff_free(pma);
7009 space = isl_pw_multi_aff_get_space(pma);
7010 mpa = isl_multi_pw_aff_alloc(space);
7012 for (i = 0; i < n; ++i) {
7013 isl_pw_aff *pa;
7015 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
7016 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
7018 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7019 isl_set *dom;
7021 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
7022 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
7025 isl_pw_multi_aff_free(pma);
7026 return mpa;
7029 /* This function performs the same operation as
7030 * isl_multi_pw_aff_from_pw_multi_aff,
7031 * but is considered as a function on an isl_pw_multi_aff when exported.
7033 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
7034 __isl_take isl_pw_multi_aff *pma)
7036 return isl_multi_pw_aff_from_pw_multi_aff(pma);
7039 /* Do "pa1" and "pa2" represent the same function?
7041 * We first check if they are obviously equal.
7042 * If not, we convert them to maps and check if those are equal.
7044 * If "pa1" or "pa2" contain any NaNs, then they are considered
7045 * not to be the same. A NaN is not equal to anything, not even
7046 * to another NaN.
7048 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
7049 __isl_keep isl_pw_aff *pa2)
7051 isl_bool equal;
7052 isl_bool has_nan;
7053 isl_map *map1, *map2;
7055 if (!pa1 || !pa2)
7056 return isl_bool_error;
7058 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
7059 if (equal < 0 || equal)
7060 return equal;
7061 has_nan = either_involves_nan(pa1, pa2);
7062 if (has_nan < 0)
7063 return isl_bool_error;
7064 if (has_nan)
7065 return isl_bool_false;
7067 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
7068 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
7069 equal = isl_map_is_equal(map1, map2);
7070 isl_map_free(map1);
7071 isl_map_free(map2);
7073 return equal;
7076 /* Do "mpa1" and "mpa2" represent the same function?
7078 * Note that we cannot convert the entire isl_multi_pw_aff
7079 * to a map because the domains of the piecewise affine expressions
7080 * may not be the same.
7082 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7083 __isl_keep isl_multi_pw_aff *mpa2)
7085 int i;
7086 isl_bool equal, equal_params;
7088 if (!mpa1 || !mpa2)
7089 return isl_bool_error;
7091 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7092 if (equal_params < 0)
7093 return isl_bool_error;
7094 if (!equal_params) {
7095 if (!isl_space_has_named_params(mpa1->space))
7096 return isl_bool_false;
7097 if (!isl_space_has_named_params(mpa2->space))
7098 return isl_bool_false;
7099 mpa1 = isl_multi_pw_aff_copy(mpa1);
7100 mpa2 = isl_multi_pw_aff_copy(mpa2);
7101 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7102 isl_multi_pw_aff_get_space(mpa2));
7103 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7104 isl_multi_pw_aff_get_space(mpa1));
7105 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7106 isl_multi_pw_aff_free(mpa1);
7107 isl_multi_pw_aff_free(mpa2);
7108 return equal;
7111 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7112 if (equal < 0 || !equal)
7113 return equal;
7115 for (i = 0; i < mpa1->n; ++i) {
7116 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7117 if (equal < 0 || !equal)
7118 return equal;
7121 return isl_bool_true;
7124 /* Do "pma1" and "pma2" represent the same function?
7126 * First check if they are obviously equal.
7127 * If not, then convert them to maps and check if those are equal.
7129 * If "pa1" or "pa2" contain any NaNs, then they are considered
7130 * not to be the same. A NaN is not equal to anything, not even
7131 * to another NaN.
7133 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7134 __isl_keep isl_pw_multi_aff *pma2)
7136 isl_bool equal;
7137 isl_bool has_nan;
7138 isl_map *map1, *map2;
7140 if (!pma1 || !pma2)
7141 return isl_bool_error;
7143 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7144 if (equal < 0 || equal)
7145 return equal;
7146 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7147 if (has_nan >= 0 && !has_nan)
7148 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7149 if (has_nan < 0 || has_nan)
7150 return isl_bool_not(has_nan);
7152 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7153 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7154 equal = isl_map_is_equal(map1, map2);
7155 isl_map_free(map1);
7156 isl_map_free(map2);
7158 return equal;
7161 #undef BASE
7162 #define BASE multi_aff
7164 #include "isl_multi_pw_aff_pullback_templ.c"
7166 #undef BASE
7167 #define BASE pw_multi_aff
7169 #include "isl_multi_pw_aff_pullback_templ.c"
7171 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7172 * with the domain of "aff". The domain of the result is the same
7173 * as that of "mpa".
7174 * "mpa" and "aff" are assumed to have been aligned.
7176 * We first extract the parametric constant from "aff", defined
7177 * over the correct domain.
7178 * Then we add the appropriate combinations of the members of "mpa".
7179 * Finally, we add the integer divisions through recursive calls.
7181 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7182 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7184 int i;
7185 isl_size n_in, n_div, n_mpa_in;
7186 isl_space *space;
7187 isl_val *v;
7188 isl_pw_aff *pa;
7189 isl_aff *tmp;
7191 n_in = isl_aff_dim(aff, isl_dim_in);
7192 n_div = isl_aff_dim(aff, isl_dim_div);
7193 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7194 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7195 goto error;
7197 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7198 tmp = isl_aff_copy(aff);
7199 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7200 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7201 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7202 tmp = isl_aff_reset_domain_space(tmp, space);
7203 pa = isl_pw_aff_from_aff(tmp);
7205 for (i = 0; i < n_in; ++i) {
7206 isl_pw_aff *pa_i;
7208 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7209 continue;
7210 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7211 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7212 pa_i = isl_pw_aff_scale_val(pa_i, v);
7213 pa = isl_pw_aff_add(pa, pa_i);
7216 for (i = 0; i < n_div; ++i) {
7217 isl_aff *div;
7218 isl_pw_aff *pa_i;
7220 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7221 continue;
7222 div = isl_aff_get_div(aff, i);
7223 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7224 isl_multi_pw_aff_copy(mpa), div);
7225 pa_i = isl_pw_aff_floor(pa_i);
7226 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7227 pa_i = isl_pw_aff_scale_val(pa_i, v);
7228 pa = isl_pw_aff_add(pa, pa_i);
7231 isl_multi_pw_aff_free(mpa);
7232 isl_aff_free(aff);
7234 return pa;
7235 error:
7236 isl_multi_pw_aff_free(mpa);
7237 isl_aff_free(aff);
7238 return NULL;
7241 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7242 * with the domain of "aff". The domain of the result is the same
7243 * as that of "mpa".
7245 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7246 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7248 isl_bool equal_params;
7250 if (!aff || !mpa)
7251 goto error;
7252 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7253 if (equal_params < 0)
7254 goto error;
7255 if (equal_params)
7256 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7258 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7259 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7261 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7262 error:
7263 isl_aff_free(aff);
7264 isl_multi_pw_aff_free(mpa);
7265 return NULL;
7268 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7269 * with the domain of "pa". The domain of the result is the same
7270 * as that of "mpa".
7271 * "mpa" and "pa" are assumed to have been aligned.
7273 * We consider each piece in turn. Note that the domains of the
7274 * pieces are assumed to be disjoint and they remain disjoint
7275 * after taking the preimage (over the same function).
7277 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7278 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7280 isl_space *space;
7281 isl_pw_aff *res;
7282 int i;
7284 if (!mpa || !pa)
7285 goto error;
7287 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7288 isl_pw_aff_get_space(pa));
7289 res = isl_pw_aff_empty(space);
7291 for (i = 0; i < pa->n; ++i) {
7292 isl_pw_aff *pa_i;
7293 isl_set *domain;
7295 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7296 isl_multi_pw_aff_copy(mpa),
7297 isl_aff_copy(pa->p[i].aff));
7298 domain = isl_set_copy(pa->p[i].set);
7299 domain = isl_set_preimage_multi_pw_aff(domain,
7300 isl_multi_pw_aff_copy(mpa));
7301 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7302 res = isl_pw_aff_add_disjoint(res, pa_i);
7305 isl_pw_aff_free(pa);
7306 isl_multi_pw_aff_free(mpa);
7307 return res;
7308 error:
7309 isl_pw_aff_free(pa);
7310 isl_multi_pw_aff_free(mpa);
7311 return NULL;
7314 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7315 * with the domain of "pa". The domain of the result is the same
7316 * as that of "mpa".
7318 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7319 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7321 isl_bool equal_params;
7323 if (!pa || !mpa)
7324 goto error;
7325 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7326 if (equal_params < 0)
7327 goto error;
7328 if (equal_params)
7329 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7331 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7332 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7334 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7335 error:
7336 isl_pw_aff_free(pa);
7337 isl_multi_pw_aff_free(mpa);
7338 return NULL;
7341 /* Compute the pullback of "pa" by the function represented by "mpa".
7342 * In other words, plug in "mpa" in "pa".
7344 * The pullback is computed by applying "pa" to "mpa".
7346 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7347 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7349 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7352 #undef BASE
7353 #define BASE multi_pw_aff
7355 #include "isl_multi_pw_aff_pullback_templ.c"
7357 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7358 * of "mpa1" and "mpa2" live in the same space, construct map space
7359 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7360 * with this map space as extract argument.
7362 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7363 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7364 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7365 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7367 int match;
7368 isl_space *space1, *space2;
7369 isl_map *res;
7371 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7372 isl_multi_pw_aff_get_space(mpa2));
7373 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7374 isl_multi_pw_aff_get_space(mpa1));
7375 if (!mpa1 || !mpa2)
7376 goto error;
7377 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7378 mpa2->space, isl_dim_out);
7379 if (match < 0)
7380 goto error;
7381 if (!match)
7382 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7383 "range spaces don't match", goto error);
7384 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7385 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7386 space1 = isl_space_map_from_domain_and_range(space1, space2);
7388 res = order(mpa1, mpa2, space1);
7389 isl_multi_pw_aff_free(mpa1);
7390 isl_multi_pw_aff_free(mpa2);
7391 return res;
7392 error:
7393 isl_multi_pw_aff_free(mpa1);
7394 isl_multi_pw_aff_free(mpa2);
7395 return NULL;
7398 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7399 * where the function values are equal. "space" is the space of the result.
7400 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7402 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7403 * in the sequences are equal.
7405 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7406 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7407 __isl_take isl_space *space)
7409 int i;
7410 isl_size n;
7411 isl_map *res;
7413 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7414 if (n < 0)
7415 space = isl_space_free(space);
7416 res = isl_map_universe(space);
7418 for (i = 0; i < n; ++i) {
7419 isl_pw_aff *pa1, *pa2;
7420 isl_map *map;
7422 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7423 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7424 map = isl_pw_aff_eq_map(pa1, pa2);
7425 res = isl_map_intersect(res, map);
7428 return res;
7431 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7432 * where the function values are equal.
7434 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7435 __isl_take isl_multi_pw_aff *mpa2)
7437 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7438 &isl_multi_pw_aff_eq_map_on_space);
7441 /* Intersect "map" with the result of applying "order"
7442 * on two copies of "mpa".
7444 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7445 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7446 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7447 __isl_take isl_multi_pw_aff *mpa2))
7449 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7452 /* Return the subset of "map" where the domain and the range
7453 * have equal "mpa" values.
7455 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7456 __isl_take isl_multi_pw_aff *mpa)
7458 return isl_map_order_at_multi_pw_aff(map, mpa,
7459 &isl_multi_pw_aff_eq_map);
7462 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7463 * where the function values of "mpa1" lexicographically satisfies
7464 * "strict_base"/"base" compared to that of "mpa2".
7465 * "space" is the space of the result.
7466 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7468 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7469 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7470 * when compared to the i-th element of "mpa2" while all previous elements are
7471 * pairwise equal.
7472 * In particular, if i corresponds to the final elements
7473 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7474 * for other values of i.
7475 * If "base" is a strict order, then "base" and "strict_base" are the same.
7477 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7478 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7479 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7480 __isl_take isl_pw_aff *pa2),
7481 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7482 __isl_take isl_pw_aff *pa2),
7483 __isl_take isl_space *space)
7485 int i;
7486 isl_size n;
7487 isl_map *res, *rest;
7489 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7490 if (n < 0)
7491 space = isl_space_free(space);
7492 res = isl_map_empty(isl_space_copy(space));
7493 rest = isl_map_universe(space);
7495 for (i = 0; i < n; ++i) {
7496 int last;
7497 isl_pw_aff *pa1, *pa2;
7498 isl_map *map;
7500 last = i == n - 1;
7502 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7503 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7504 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7505 map = isl_map_intersect(map, isl_map_copy(rest));
7506 res = isl_map_union(res, map);
7508 if (last)
7509 continue;
7511 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7512 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7513 map = isl_pw_aff_eq_map(pa1, pa2);
7514 rest = isl_map_intersect(rest, map);
7517 isl_map_free(rest);
7518 return res;
7521 #undef ORDER
7522 #define ORDER le
7523 #undef STRICT_ORDER
7524 #define STRICT_ORDER lt
7525 #include "isl_aff_lex_templ.c"
7527 #undef ORDER
7528 #define ORDER lt
7529 #undef STRICT_ORDER
7530 #define STRICT_ORDER lt
7531 #include "isl_aff_lex_templ.c"
7533 #undef ORDER
7534 #define ORDER ge
7535 #undef STRICT_ORDER
7536 #define STRICT_ORDER gt
7537 #include "isl_aff_lex_templ.c"
7539 #undef ORDER
7540 #define ORDER gt
7541 #undef STRICT_ORDER
7542 #define STRICT_ORDER gt
7543 #include "isl_aff_lex_templ.c"
7545 /* Compare two isl_affs.
7547 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7548 * than "aff2" and 0 if they are equal.
7550 * The order is fairly arbitrary. We do consider expressions that only involve
7551 * earlier dimensions as "smaller".
7553 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7555 int cmp;
7556 int last1, last2;
7558 if (aff1 == aff2)
7559 return 0;
7561 if (!aff1)
7562 return -1;
7563 if (!aff2)
7564 return 1;
7566 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7567 if (cmp != 0)
7568 return cmp;
7570 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7571 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7572 if (last1 != last2)
7573 return last1 - last2;
7575 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7578 /* Compare two isl_pw_affs.
7580 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7581 * than "pa2" and 0 if they are equal.
7583 * The order is fairly arbitrary. We do consider expressions that only involve
7584 * earlier dimensions as "smaller".
7586 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7587 __isl_keep isl_pw_aff *pa2)
7589 int i;
7590 int cmp;
7592 if (pa1 == pa2)
7593 return 0;
7595 if (!pa1)
7596 return -1;
7597 if (!pa2)
7598 return 1;
7600 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7601 if (cmp != 0)
7602 return cmp;
7604 if (pa1->n != pa2->n)
7605 return pa1->n - pa2->n;
7607 for (i = 0; i < pa1->n; ++i) {
7608 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7609 if (cmp != 0)
7610 return cmp;
7611 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7612 if (cmp != 0)
7613 return cmp;
7616 return 0;
7619 /* Return a piecewise affine expression that is equal to "v" on "domain".
7621 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7622 __isl_take isl_val *v)
7624 isl_space *space;
7625 isl_local_space *ls;
7626 isl_aff *aff;
7628 space = isl_set_get_space(domain);
7629 ls = isl_local_space_from_space(space);
7630 aff = isl_aff_val_on_domain(ls, v);
7632 return isl_pw_aff_alloc(domain, aff);
7635 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7636 * but is considered as a function on an isl_set when exported.
7638 __isl_give isl_pw_aff *isl_set_pw_aff_on_domain_val(__isl_take isl_set *domain,
7639 __isl_take isl_val *v)
7641 return isl_pw_aff_val_on_domain(domain, v);
7644 /* Return a piecewise affine expression that is equal to the parameter
7645 * with identifier "id" on "domain".
7647 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7648 __isl_take isl_set *domain, __isl_take isl_id *id)
7650 isl_space *space;
7651 isl_aff *aff;
7653 space = isl_set_get_space(domain);
7654 space = isl_space_add_param_id(space, isl_id_copy(id));
7655 domain = isl_set_align_params(domain, isl_space_copy(space));
7656 aff = isl_aff_param_on_domain_space_id(space, id);
7658 return isl_pw_aff_alloc(domain, aff);
7661 /* This function performs the same operation as
7662 * isl_pw_aff_param_on_domain_id,
7663 * but is considered as a function on an isl_set when exported.
7665 __isl_give isl_pw_aff *isl_set_param_pw_aff_on_domain_id(
7666 __isl_take isl_set *domain, __isl_take isl_id *id)
7668 return isl_pw_aff_param_on_domain_id(domain, id);
7671 /* Return a multi affine expression that is equal to "mv" on domain
7672 * space "space".
7674 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7675 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7677 int i;
7678 isl_size n;
7679 isl_space *space2;
7680 isl_local_space *ls;
7681 isl_multi_aff *ma;
7683 n = isl_multi_val_dim(mv, isl_dim_set);
7684 if (!space || n < 0)
7685 goto error;
7687 space2 = isl_multi_val_get_space(mv);
7688 space2 = isl_space_align_params(space2, isl_space_copy(space));
7689 space = isl_space_align_params(space, isl_space_copy(space2));
7690 space = isl_space_map_from_domain_and_range(space, space2);
7691 ma = isl_multi_aff_alloc(isl_space_copy(space));
7692 ls = isl_local_space_from_space(isl_space_domain(space));
7693 for (i = 0; i < n; ++i) {
7694 isl_val *v;
7695 isl_aff *aff;
7697 v = isl_multi_val_get_val(mv, i);
7698 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7699 ma = isl_multi_aff_set_aff(ma, i, aff);
7701 isl_local_space_free(ls);
7703 isl_multi_val_free(mv);
7704 return ma;
7705 error:
7706 isl_space_free(space);
7707 isl_multi_val_free(mv);
7708 return NULL;
7711 /* This is an alternative name for the function above.
7713 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7714 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7716 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7719 /* This function performs the same operation as
7720 * isl_multi_aff_multi_val_on_domain_space,
7721 * but is considered as a function on an isl_space when exported.
7723 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7724 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7726 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7729 /* Return a piecewise multi-affine expression
7730 * that is equal to "mv" on "domain".
7732 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7733 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7735 isl_space *space;
7736 isl_multi_aff *ma;
7738 space = isl_set_get_space(domain);
7739 ma = isl_multi_aff_multi_val_on_space(space, mv);
7741 return isl_pw_multi_aff_alloc(domain, ma);
7744 /* This function performs the same operation as
7745 * isl_pw_multi_aff_multi_val_on_domain,
7746 * but is considered as a function on an isl_set when exported.
7748 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7749 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7751 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7754 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7755 * mv is the value that should be attained on each domain set
7756 * res collects the results
7758 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7759 isl_multi_val *mv;
7760 isl_union_pw_multi_aff *res;
7763 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7764 * and add it to data->res.
7766 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7767 void *user)
7769 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7770 isl_pw_multi_aff *pma;
7771 isl_multi_val *mv;
7773 mv = isl_multi_val_copy(data->mv);
7774 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7775 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7777 return data->res ? isl_stat_ok : isl_stat_error;
7780 /* Return a union piecewise multi-affine expression
7781 * that is equal to "mv" on "domain".
7783 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7784 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7786 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7787 isl_space *space;
7789 space = isl_union_set_get_space(domain);
7790 data.res = isl_union_pw_multi_aff_empty(space);
7791 data.mv = mv;
7792 if (isl_union_set_foreach_set(domain,
7793 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7794 data.res = isl_union_pw_multi_aff_free(data.res);
7795 isl_union_set_free(domain);
7796 isl_multi_val_free(mv);
7797 return data.res;
7800 /* Compute the pullback of data->pma by the function represented by "pma2",
7801 * provided the spaces match, and add the results to data->res.
7803 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7805 struct isl_union_pw_multi_aff_bin_data *data = user;
7807 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7808 pma2->dim, isl_dim_out)) {
7809 isl_pw_multi_aff_free(pma2);
7810 return isl_stat_ok;
7813 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7814 isl_pw_multi_aff_copy(data->pma), pma2);
7816 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7817 if (!data->res)
7818 return isl_stat_error;
7820 return isl_stat_ok;
7823 /* Compute the pullback of "upma1" by the function represented by "upma2".
7825 __isl_give isl_union_pw_multi_aff *
7826 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7827 __isl_take isl_union_pw_multi_aff *upma1,
7828 __isl_take isl_union_pw_multi_aff *upma2)
7830 return bin_op(upma1, upma2, &pullback_entry);
7833 /* Apply "upma2" to "upma1".
7835 * That is, compute the pullback of "upma2" by "upma1".
7837 __isl_give isl_union_pw_multi_aff *
7838 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7839 __isl_take isl_union_pw_multi_aff *upma1,
7840 __isl_take isl_union_pw_multi_aff *upma2)
7842 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7845 #undef TYPE
7846 #define TYPE isl_pw_multi_aff
7847 static
7848 #include "isl_copy_tuple_id_templ.c"
7850 /* Given a function "pma1" of the form A[B -> C] -> D and
7851 * a function "pma2" of the form E -> B,
7852 * replace the domain of the wrapped relation inside the domain of "pma1"
7853 * by the preimage with respect to "pma2".
7854 * In other words, plug in "pma2" in this nested domain.
7855 * The result is of the form A[E -> C] -> D.
7857 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7858 * plug that into "pma1".
7860 __isl_give isl_pw_multi_aff *
7861 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7862 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
7864 isl_space *pma1_space, *pma2_space;
7865 isl_space *space;
7866 isl_pw_multi_aff *id;
7868 pma1_space = isl_pw_multi_aff_peek_space(pma1);
7869 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7871 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
7872 goto error;
7873 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
7874 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
7875 goto error;
7877 space = isl_space_domain(isl_space_copy(pma1_space));
7878 space = isl_space_range(isl_space_unwrap(space));
7879 id = isl_pw_multi_aff_identity_on_domain_space(space);
7880 pma2 = isl_pw_multi_aff_product(pma2, id);
7882 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
7883 pma1_space, isl_dim_in);
7884 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
7885 pma1_space, isl_dim_in);
7887 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
7888 error:
7889 isl_pw_multi_aff_free(pma1);
7890 isl_pw_multi_aff_free(pma2);
7891 return NULL;
7894 /* If data->pma and "pma2" are such that
7895 * data->pma is of the form A[B -> C] -> D and
7896 * "pma2" is of the form E -> B,
7897 * then replace the domain of the wrapped relation
7898 * inside the domain of data->pma by the preimage with respect to "pma2" and
7899 * add the result to data->res.
7901 static isl_stat preimage_domain_wrapped_domain_entry(
7902 __isl_take isl_pw_multi_aff *pma2, void *user)
7904 struct isl_union_pw_multi_aff_bin_data *data = user;
7905 isl_space *pma1_space, *pma2_space;
7906 isl_bool match;
7908 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
7909 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7911 match = isl_space_domain_is_wrapping(pma1_space);
7912 if (match >= 0 && match)
7913 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
7914 isl_dim_in, pma2_space, isl_dim_out);
7915 if (match < 0 || !match) {
7916 isl_pw_multi_aff_free(pma2);
7917 return match < 0 ? isl_stat_error : isl_stat_ok;
7920 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7921 isl_pw_multi_aff_copy(data->pma), pma2);
7923 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7925 return isl_stat_non_null(data->res);
7928 /* For each pair of functions A[B -> C] -> D in "upma1" and
7929 * E -> B in "upma2",
7930 * replace the domain of the wrapped relation inside the domain of the first
7931 * by the preimage with respect to the second and collect the results.
7932 * In other words, plug in the second function in this nested domain.
7933 * The results are of the form A[E -> C] -> D.
7935 __isl_give isl_union_pw_multi_aff *
7936 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
7937 __isl_take isl_union_pw_multi_aff *upma1,
7938 __isl_take isl_union_pw_multi_aff *upma2)
7940 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
7943 /* Check that the domain space of "upa" matches "space".
7945 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7946 * can in principle never fail since the space "space" is that
7947 * of the isl_multi_union_pw_aff and is a set space such that
7948 * there is no domain space to match.
7950 * We check the parameters and double-check that "space" is
7951 * indeed that of a set.
7953 static isl_stat isl_union_pw_aff_check_match_domain_space(
7954 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7956 isl_space *upa_space;
7957 isl_bool match;
7959 if (!upa || !space)
7960 return isl_stat_error;
7962 match = isl_space_is_set(space);
7963 if (match < 0)
7964 return isl_stat_error;
7965 if (!match)
7966 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7967 "expecting set space", return isl_stat_error);
7969 upa_space = isl_union_pw_aff_get_space(upa);
7970 match = isl_space_has_equal_params(space, upa_space);
7971 if (match < 0)
7972 goto error;
7973 if (!match)
7974 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7975 "parameters don't match", goto error);
7977 isl_space_free(upa_space);
7978 return isl_stat_ok;
7979 error:
7980 isl_space_free(upa_space);
7981 return isl_stat_error;
7984 /* Do the parameters of "upa" match those of "space"?
7986 static isl_bool isl_union_pw_aff_matching_params(
7987 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7989 isl_space *upa_space;
7990 isl_bool match;
7992 if (!upa || !space)
7993 return isl_bool_error;
7995 upa_space = isl_union_pw_aff_get_space(upa);
7997 match = isl_space_has_equal_params(space, upa_space);
7999 isl_space_free(upa_space);
8000 return match;
8003 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8004 * space represents the new parameters.
8005 * res collects the results.
8007 struct isl_union_pw_aff_reset_params_data {
8008 isl_space *space;
8009 isl_union_pw_aff *res;
8012 /* Replace the parameters of "pa" by data->space and
8013 * add the result to data->res.
8015 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8017 struct isl_union_pw_aff_reset_params_data *data = user;
8018 isl_space *space;
8020 space = isl_pw_aff_get_space(pa);
8021 space = isl_space_replace_params(space, data->space);
8022 pa = isl_pw_aff_reset_space(pa, space);
8023 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8025 return data->res ? isl_stat_ok : isl_stat_error;
8028 /* Replace the domain space of "upa" by "space".
8029 * Since a union expression does not have a (single) domain space,
8030 * "space" is necessarily a parameter space.
8032 * Since the order and the names of the parameters determine
8033 * the hash value, we need to create a new hash table.
8035 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8036 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8038 struct isl_union_pw_aff_reset_params_data data = { space };
8039 isl_bool match;
8041 match = isl_union_pw_aff_matching_params(upa, space);
8042 if (match < 0)
8043 upa = isl_union_pw_aff_free(upa);
8044 else if (match) {
8045 isl_space_free(space);
8046 return upa;
8049 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8050 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8051 data.res = isl_union_pw_aff_free(data.res);
8053 isl_union_pw_aff_free(upa);
8054 isl_space_free(space);
8055 return data.res;
8058 /* Return the floor of "pa".
8060 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8062 return isl_pw_aff_floor(pa);
8065 /* Given f, return floor(f).
8067 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8068 __isl_take isl_union_pw_aff *upa)
8070 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8073 /* Compute
8075 * upa mod m = upa - m * floor(upa/m)
8077 * with m an integer value.
8079 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8080 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8082 isl_union_pw_aff *res;
8084 if (!upa || !m)
8085 goto error;
8087 if (!isl_val_is_int(m))
8088 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8089 "expecting integer modulo", goto error);
8090 if (!isl_val_is_pos(m))
8091 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8092 "expecting positive modulo", goto error);
8094 res = isl_union_pw_aff_copy(upa);
8095 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8096 upa = isl_union_pw_aff_floor(upa);
8097 upa = isl_union_pw_aff_scale_val(upa, m);
8098 res = isl_union_pw_aff_sub(res, upa);
8100 return res;
8101 error:
8102 isl_val_free(m);
8103 isl_union_pw_aff_free(upa);
8104 return NULL;
8107 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8108 * pos is the output position that needs to be extracted.
8109 * res collects the results.
8111 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8112 int pos;
8113 isl_union_pw_aff *res;
8116 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8117 * (assuming it has such a dimension) and add it to data->res.
8119 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8121 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8122 isl_size n_out;
8123 isl_pw_aff *pa;
8125 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8126 if (n_out < 0)
8127 return isl_stat_error;
8128 if (data->pos >= n_out) {
8129 isl_pw_multi_aff_free(pma);
8130 return isl_stat_ok;
8133 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8134 isl_pw_multi_aff_free(pma);
8136 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8138 return data->res ? isl_stat_ok : isl_stat_error;
8141 /* Extract an isl_union_pw_aff corresponding to
8142 * output dimension "pos" of "upma".
8144 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8145 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8147 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8148 isl_space *space;
8150 if (!upma)
8151 return NULL;
8153 if (pos < 0)
8154 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8155 "cannot extract at negative position", return NULL);
8157 space = isl_union_pw_multi_aff_get_space(upma);
8158 data.res = isl_union_pw_aff_empty(space);
8159 data.pos = pos;
8160 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8161 &get_union_pw_aff, &data) < 0)
8162 data.res = isl_union_pw_aff_free(data.res);
8164 return data.res;
8167 /* Return a union piecewise affine expression
8168 * that is equal to "aff" on "domain".
8170 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8171 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8173 isl_pw_aff *pa;
8175 pa = isl_pw_aff_from_aff(aff);
8176 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8179 /* Return a union piecewise affine expression
8180 * that is equal to the parameter identified by "id" on "domain".
8182 * Make sure the parameter appears in the space passed to
8183 * isl_aff_param_on_domain_space_id.
8185 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8186 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8188 isl_space *space;
8189 isl_aff *aff;
8191 space = isl_union_set_get_space(domain);
8192 space = isl_space_add_param_id(space, isl_id_copy(id));
8193 aff = isl_aff_param_on_domain_space_id(space, id);
8194 return isl_union_pw_aff_aff_on_domain(domain, aff);
8197 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8198 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8199 * needs to attain.
8200 * "res" collects the results.
8202 struct isl_union_pw_aff_pw_aff_on_domain_data {
8203 isl_pw_aff *pa;
8204 isl_union_pw_aff *res;
8207 /* Construct a piecewise affine expression that is equal to data->pa
8208 * on "domain" and add the result to data->res.
8210 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8212 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8213 isl_pw_aff *pa;
8214 isl_size dim;
8216 pa = isl_pw_aff_copy(data->pa);
8217 dim = isl_set_dim(domain, isl_dim_set);
8218 if (dim < 0)
8219 pa = isl_pw_aff_free(pa);
8220 pa = isl_pw_aff_from_range(pa);
8221 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8222 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8223 pa = isl_pw_aff_intersect_domain(pa, domain);
8224 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8226 return data->res ? isl_stat_ok : isl_stat_error;
8229 /* Return a union piecewise affine expression
8230 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8231 * have been aligned.
8233 * Construct an isl_pw_aff on each of the sets in "domain" and
8234 * collect the results.
8236 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8237 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8239 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8240 isl_space *space;
8242 space = isl_union_set_get_space(domain);
8243 data.res = isl_union_pw_aff_empty(space);
8244 data.pa = pa;
8245 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8246 data.res = isl_union_pw_aff_free(data.res);
8247 isl_union_set_free(domain);
8248 isl_pw_aff_free(pa);
8249 return data.res;
8252 /* Return a union piecewise affine expression
8253 * that is equal to "pa" on "domain".
8255 * Check that "pa" is a parametric expression,
8256 * align the parameters if needed and call
8257 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8259 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8260 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8262 isl_bool is_set;
8263 isl_bool equal_params;
8264 isl_space *domain_space, *pa_space;
8266 pa_space = isl_pw_aff_peek_space(pa);
8267 is_set = isl_space_is_set(pa_space);
8268 if (is_set < 0)
8269 goto error;
8270 if (!is_set)
8271 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8272 "expecting parametric expression", goto error);
8274 domain_space = isl_union_set_get_space(domain);
8275 pa_space = isl_pw_aff_get_space(pa);
8276 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8277 if (equal_params >= 0 && !equal_params) {
8278 isl_space *space;
8280 space = isl_space_align_params(domain_space, pa_space);
8281 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8282 domain = isl_union_set_align_params(domain, space);
8283 } else {
8284 isl_space_free(domain_space);
8285 isl_space_free(pa_space);
8288 if (equal_params < 0)
8289 goto error;
8290 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8291 error:
8292 isl_union_set_free(domain);
8293 isl_pw_aff_free(pa);
8294 return NULL;
8297 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8298 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8299 * "res" collects the results.
8301 struct isl_union_pw_aff_val_on_domain_data {
8302 isl_val *v;
8303 isl_union_pw_aff *res;
8306 /* Construct a piecewise affine expression that is equal to data->v
8307 * on "domain" and add the result to data->res.
8309 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8311 struct isl_union_pw_aff_val_on_domain_data *data = user;
8312 isl_pw_aff *pa;
8313 isl_val *v;
8315 v = isl_val_copy(data->v);
8316 pa = isl_pw_aff_val_on_domain(domain, v);
8317 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8319 return data->res ? isl_stat_ok : isl_stat_error;
8322 /* Return a union piecewise affine expression
8323 * that is equal to "v" on "domain".
8325 * Construct an isl_pw_aff on each of the sets in "domain" and
8326 * collect the results.
8328 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8329 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8331 struct isl_union_pw_aff_val_on_domain_data data;
8332 isl_space *space;
8334 space = isl_union_set_get_space(domain);
8335 data.res = isl_union_pw_aff_empty(space);
8336 data.v = v;
8337 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8338 data.res = isl_union_pw_aff_free(data.res);
8339 isl_union_set_free(domain);
8340 isl_val_free(v);
8341 return data.res;
8344 /* Construct a piecewise multi affine expression
8345 * that is equal to "pa" and add it to upma.
8347 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8348 void *user)
8350 isl_union_pw_multi_aff **upma = user;
8351 isl_pw_multi_aff *pma;
8353 pma = isl_pw_multi_aff_from_pw_aff(pa);
8354 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8356 return *upma ? isl_stat_ok : isl_stat_error;
8359 /* Construct and return a union piecewise multi affine expression
8360 * that is equal to the given union piecewise affine expression.
8362 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8363 __isl_take isl_union_pw_aff *upa)
8365 isl_space *space;
8366 isl_union_pw_multi_aff *upma;
8368 if (!upa)
8369 return NULL;
8371 space = isl_union_pw_aff_get_space(upa);
8372 upma = isl_union_pw_multi_aff_empty(space);
8374 if (isl_union_pw_aff_foreach_pw_aff(upa,
8375 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8376 upma = isl_union_pw_multi_aff_free(upma);
8378 isl_union_pw_aff_free(upa);
8379 return upma;
8382 /* Compute the set of elements in the domain of "pa" where it is zero and
8383 * add this set to "uset".
8385 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8387 isl_union_set **uset = (isl_union_set **)user;
8389 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8391 return *uset ? isl_stat_ok : isl_stat_error;
8394 /* Return a union set containing those elements in the domain
8395 * of "upa" where it is zero.
8397 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8398 __isl_take isl_union_pw_aff *upa)
8400 isl_union_set *zero;
8402 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8403 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8404 zero = isl_union_set_free(zero);
8406 isl_union_pw_aff_free(upa);
8407 return zero;
8410 /* Internal data structure for isl_union_pw_aff_bind_id,
8411 * storing the parameter that needs to be bound and
8412 * the accumulated results.
8414 struct isl_bind_id_data {
8415 isl_id *id;
8416 isl_union_set *bound;
8419 /* Bind the piecewise affine function "pa" to the parameter data->id,
8420 * adding the resulting elements in the domain where the expression
8421 * is equal to the parameter to data->bound.
8423 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8425 struct isl_bind_id_data *data = user;
8426 isl_set *bound;
8428 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8429 data->bound = isl_union_set_add_set(data->bound, bound);
8431 return data->bound ? isl_stat_ok : isl_stat_error;
8434 /* Bind the union piecewise affine function "upa" to the parameter "id",
8435 * returning the elements in the domain where the expression
8436 * is equal to the parameter.
8438 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8439 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8441 struct isl_bind_id_data data = { id };
8443 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8444 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8445 data.bound = isl_union_set_free(data.bound);
8447 isl_union_pw_aff_free(upa);
8448 isl_id_free(id);
8449 return data.bound;
8452 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8453 * upma is the function that is plugged in.
8454 * pa is the current part of the function in which upma is plugged in.
8455 * res collects the results.
8457 struct isl_union_pw_aff_pullback_upma_data {
8458 isl_union_pw_multi_aff *upma;
8459 isl_pw_aff *pa;
8460 isl_union_pw_aff *res;
8463 /* Check if "pma" can be plugged into data->pa.
8464 * If so, perform the pullback and add the result to data->res.
8466 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8468 struct isl_union_pw_aff_pullback_upma_data *data = user;
8469 isl_pw_aff *pa;
8471 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8472 pma->dim, isl_dim_out)) {
8473 isl_pw_multi_aff_free(pma);
8474 return isl_stat_ok;
8477 pa = isl_pw_aff_copy(data->pa);
8478 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8480 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8482 return data->res ? isl_stat_ok : isl_stat_error;
8485 /* Check if any of the elements of data->upma can be plugged into pa,
8486 * add if so add the result to data->res.
8488 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8490 struct isl_union_pw_aff_pullback_upma_data *data = user;
8491 isl_stat r;
8493 data->pa = pa;
8494 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8495 &pa_pb_pma, data);
8496 isl_pw_aff_free(pa);
8498 return r;
8501 /* Compute the pullback of "upa" by the function represented by "upma".
8502 * In other words, plug in "upma" in "upa". The result contains
8503 * expressions defined over the domain space of "upma".
8505 * Run over all pairs of elements in "upa" and "upma", perform
8506 * the pullback when appropriate and collect the results.
8507 * If the hash value were based on the domain space rather than
8508 * the function space, then we could run through all elements
8509 * of "upma" and directly pick out the corresponding element of "upa".
8511 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8512 __isl_take isl_union_pw_aff *upa,
8513 __isl_take isl_union_pw_multi_aff *upma)
8515 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8516 isl_space *space;
8518 space = isl_union_pw_multi_aff_get_space(upma);
8519 upa = isl_union_pw_aff_align_params(upa, space);
8520 space = isl_union_pw_aff_get_space(upa);
8521 upma = isl_union_pw_multi_aff_align_params(upma, space);
8523 if (!upa || !upma)
8524 goto error;
8526 data.upma = upma;
8527 data.res = isl_union_pw_aff_alloc_same_size(upa);
8528 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8529 data.res = isl_union_pw_aff_free(data.res);
8531 isl_union_pw_aff_free(upa);
8532 isl_union_pw_multi_aff_free(upma);
8533 return data.res;
8534 error:
8535 isl_union_pw_aff_free(upa);
8536 isl_union_pw_multi_aff_free(upma);
8537 return NULL;
8540 #undef BASE
8541 #define BASE union_pw_aff
8542 #undef DOMBASE
8543 #define DOMBASE union_set
8545 #include <isl_multi_explicit_domain.c>
8546 #include <isl_multi_union_pw_aff_explicit_domain.c>
8547 #include <isl_multi_templ.c>
8548 #include <isl_multi_un_op_templ.c>
8549 #include <isl_multi_bin_val_templ.c>
8550 #include <isl_multi_apply_set.c>
8551 #include <isl_multi_apply_union_set.c>
8552 #include <isl_multi_arith_templ.c>
8553 #include <isl_multi_bind_templ.c>
8554 #include <isl_multi_coalesce.c>
8555 #include <isl_multi_dim_id_templ.c>
8556 #include <isl_multi_floor.c>
8557 #include <isl_multi_from_base_templ.c>
8558 #include <isl_multi_gist.c>
8559 #include <isl_multi_align_set.c>
8560 #include <isl_multi_align_union_set.c>
8561 #include <isl_multi_intersect.c>
8562 #include <isl_multi_nan_templ.c>
8563 #include <isl_multi_tuple_id_templ.c>
8564 #include <isl_multi_union_add_templ.c>
8565 #include <isl_multi_zero_space_templ.c>
8567 /* Does "mupa" have a non-trivial explicit domain?
8569 * The explicit domain, if present, is trivial if it represents
8570 * an (obviously) universe parameter set.
8572 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8573 __isl_keep isl_multi_union_pw_aff *mupa)
8575 isl_bool is_params, trivial;
8576 isl_set *set;
8578 if (!mupa)
8579 return isl_bool_error;
8580 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8581 return isl_bool_false;
8582 is_params = isl_union_set_is_params(mupa->u.dom);
8583 if (is_params < 0 || !is_params)
8584 return isl_bool_not(is_params);
8585 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8586 trivial = isl_set_plain_is_universe(set);
8587 isl_set_free(set);
8588 return isl_bool_not(trivial);
8591 /* Construct a multiple union piecewise affine expression
8592 * in the given space with value zero in each of the output dimensions.
8594 * Since there is no canonical zero value for
8595 * a union piecewise affine expression, we can only construct
8596 * a zero-dimensional "zero" value.
8598 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8599 __isl_take isl_space *space)
8601 isl_bool params;
8602 isl_size dim;
8604 if (!space)
8605 return NULL;
8607 params = isl_space_is_params(space);
8608 if (params < 0)
8609 goto error;
8610 if (params)
8611 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8612 "expecting proper set space", goto error);
8613 if (!isl_space_is_set(space))
8614 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8615 "expecting set space", goto error);
8616 dim = isl_space_dim(space, isl_dim_out);
8617 if (dim < 0)
8618 goto error;
8619 if (dim != 0)
8620 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8621 "expecting 0D space", goto error);
8623 return isl_multi_union_pw_aff_alloc(space);
8624 error:
8625 isl_space_free(space);
8626 return NULL;
8629 /* Construct and return a multi union piecewise affine expression
8630 * that is equal to the given multi affine expression.
8632 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8633 __isl_take isl_multi_aff *ma)
8635 isl_multi_pw_aff *mpa;
8637 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8638 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8641 /* This function performs the same operation as
8642 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8643 * isl_multi_aff when exported.
8645 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8646 __isl_take isl_multi_aff *ma)
8648 return isl_multi_union_pw_aff_from_multi_aff(ma);
8651 /* Construct and return a multi union piecewise affine expression
8652 * that is equal to the given multi piecewise affine expression.
8654 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8655 __isl_take isl_multi_pw_aff *mpa)
8657 int i;
8658 isl_size n;
8659 isl_space *space;
8660 isl_multi_union_pw_aff *mupa;
8662 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8663 if (n < 0)
8664 mpa = isl_multi_pw_aff_free(mpa);
8665 if (!mpa)
8666 return NULL;
8668 space = isl_multi_pw_aff_get_space(mpa);
8669 space = isl_space_range(space);
8670 mupa = isl_multi_union_pw_aff_alloc(space);
8672 for (i = 0; i < n; ++i) {
8673 isl_pw_aff *pa;
8674 isl_union_pw_aff *upa;
8676 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8677 upa = isl_union_pw_aff_from_pw_aff(pa);
8678 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8681 isl_multi_pw_aff_free(mpa);
8683 return mupa;
8686 /* Extract the range space of "pma" and assign it to *space.
8687 * If *space has already been set (through a previous call to this function),
8688 * then check that the range space is the same.
8690 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8692 isl_space **space = user;
8693 isl_space *pma_space;
8694 isl_bool equal;
8696 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8697 isl_pw_multi_aff_free(pma);
8699 if (!pma_space)
8700 return isl_stat_error;
8701 if (!*space) {
8702 *space = pma_space;
8703 return isl_stat_ok;
8706 equal = isl_space_is_equal(pma_space, *space);
8707 isl_space_free(pma_space);
8709 if (equal < 0)
8710 return isl_stat_error;
8711 if (!equal)
8712 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8713 "range spaces not the same", return isl_stat_error);
8714 return isl_stat_ok;
8717 /* Construct and return a multi union piecewise affine expression
8718 * that is equal to the given union piecewise multi affine expression.
8720 * In order to be able to perform the conversion, the input
8721 * needs to be non-empty and may only involve a single range space.
8723 * If the resulting multi union piecewise affine expression has
8724 * an explicit domain, then assign it the domain of the input.
8725 * In other cases, the domain is stored in the individual elements.
8727 __isl_give isl_multi_union_pw_aff *
8728 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8729 __isl_take isl_union_pw_multi_aff *upma)
8731 isl_space *space = NULL;
8732 isl_multi_union_pw_aff *mupa;
8733 int i;
8734 isl_size n;
8736 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8737 if (n < 0)
8738 goto error;
8739 if (n == 0)
8740 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8741 "cannot extract range space from empty input",
8742 goto error);
8743 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8744 &space) < 0)
8745 goto error;
8747 if (!space)
8748 goto error;
8750 n = isl_space_dim(space, isl_dim_set);
8751 if (n < 0)
8752 space = isl_space_free(space);
8753 mupa = isl_multi_union_pw_aff_alloc(space);
8755 for (i = 0; i < n; ++i) {
8756 isl_union_pw_aff *upa;
8758 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8759 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8761 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8762 isl_union_set *dom;
8763 isl_union_pw_multi_aff *copy;
8765 copy = isl_union_pw_multi_aff_copy(upma);
8766 dom = isl_union_pw_multi_aff_domain(copy);
8767 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8770 isl_union_pw_multi_aff_free(upma);
8771 return mupa;
8772 error:
8773 isl_space_free(space);
8774 isl_union_pw_multi_aff_free(upma);
8775 return NULL;
8778 /* This function performs the same operation as
8779 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8780 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8782 __isl_give isl_multi_union_pw_aff *
8783 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8784 __isl_take isl_union_pw_multi_aff *upma)
8786 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8789 /* Try and create an isl_multi_union_pw_aff that is equivalent
8790 * to the given isl_union_map.
8791 * The isl_union_map is required to be single-valued in each space.
8792 * Moreover, it cannot be empty and all range spaces need to be the same.
8793 * Otherwise, an error is produced.
8795 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8796 __isl_take isl_union_map *umap)
8798 isl_union_pw_multi_aff *upma;
8800 upma = isl_union_pw_multi_aff_from_union_map(umap);
8801 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8804 /* This function performs the same operation as
8805 * isl_multi_union_pw_aff_from_union_map,
8806 * but is considered as a function on an isl_union_map when exported.
8808 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8809 __isl_take isl_union_map *umap)
8811 return isl_multi_union_pw_aff_from_union_map(umap);
8814 /* Return a multiple union piecewise affine expression
8815 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8816 * have been aligned.
8818 * If the resulting multi union piecewise affine expression has
8819 * an explicit domain, then assign it the input domain.
8820 * In other cases, the domain is stored in the individual elements.
8822 static __isl_give isl_multi_union_pw_aff *
8823 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8824 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8826 int i;
8827 isl_size n;
8828 isl_space *space;
8829 isl_multi_union_pw_aff *mupa;
8831 n = isl_multi_val_dim(mv, isl_dim_set);
8832 if (!domain || n < 0)
8833 goto error;
8835 space = isl_multi_val_get_space(mv);
8836 mupa = isl_multi_union_pw_aff_alloc(space);
8837 for (i = 0; i < n; ++i) {
8838 isl_val *v;
8839 isl_union_pw_aff *upa;
8841 v = isl_multi_val_get_val(mv, i);
8842 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8844 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8846 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8847 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8848 isl_union_set_copy(domain));
8850 isl_union_set_free(domain);
8851 isl_multi_val_free(mv);
8852 return mupa;
8853 error:
8854 isl_union_set_free(domain);
8855 isl_multi_val_free(mv);
8856 return NULL;
8859 /* Return a multiple union piecewise affine expression
8860 * that is equal to "mv" on "domain".
8862 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8863 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8865 isl_bool equal_params;
8867 if (!domain || !mv)
8868 goto error;
8869 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8870 if (equal_params < 0)
8871 goto error;
8872 if (equal_params)
8873 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8874 domain, mv);
8875 domain = isl_union_set_align_params(domain,
8876 isl_multi_val_get_space(mv));
8877 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8878 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8879 error:
8880 isl_union_set_free(domain);
8881 isl_multi_val_free(mv);
8882 return NULL;
8885 /* Return a multiple union piecewise affine expression
8886 * that is equal to "ma" on "domain".
8888 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8889 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8891 isl_pw_multi_aff *pma;
8893 pma = isl_pw_multi_aff_from_multi_aff(ma);
8894 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8897 /* Return a multiple union piecewise affine expression
8898 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8899 * have been aligned.
8901 * If the resulting multi union piecewise affine expression has
8902 * an explicit domain, then assign it the input domain.
8903 * In other cases, the domain is stored in the individual elements.
8905 static __isl_give isl_multi_union_pw_aff *
8906 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8907 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8909 int i;
8910 isl_size n;
8911 isl_space *space;
8912 isl_multi_union_pw_aff *mupa;
8914 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8915 if (!domain || n < 0)
8916 goto error;
8917 space = isl_pw_multi_aff_get_space(pma);
8918 mupa = isl_multi_union_pw_aff_alloc(space);
8919 for (i = 0; i < n; ++i) {
8920 isl_pw_aff *pa;
8921 isl_union_pw_aff *upa;
8923 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8924 upa = isl_union_pw_aff_pw_aff_on_domain(
8925 isl_union_set_copy(domain), pa);
8926 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8928 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8929 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8930 isl_union_set_copy(domain));
8932 isl_union_set_free(domain);
8933 isl_pw_multi_aff_free(pma);
8934 return mupa;
8935 error:
8936 isl_union_set_free(domain);
8937 isl_pw_multi_aff_free(pma);
8938 return NULL;
8941 /* Return a multiple union piecewise affine expression
8942 * that is equal to "pma" on "domain".
8944 __isl_give isl_multi_union_pw_aff *
8945 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8946 __isl_take isl_pw_multi_aff *pma)
8948 isl_bool equal_params;
8949 isl_space *space;
8951 space = isl_pw_multi_aff_peek_space(pma);
8952 equal_params = isl_union_set_space_has_equal_params(domain, space);
8953 if (equal_params < 0)
8954 goto error;
8955 if (equal_params)
8956 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8957 domain, pma);
8958 domain = isl_union_set_align_params(domain,
8959 isl_pw_multi_aff_get_space(pma));
8960 pma = isl_pw_multi_aff_align_params(pma,
8961 isl_union_set_get_space(domain));
8962 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8963 pma);
8964 error:
8965 isl_union_set_free(domain);
8966 isl_pw_multi_aff_free(pma);
8967 return NULL;
8970 /* Return a union set containing those elements in the domains
8971 * of the elements of "mupa" where they are all zero.
8973 * If there are no elements, then simply return the entire domain.
8975 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8976 __isl_take isl_multi_union_pw_aff *mupa)
8978 int i;
8979 isl_size n;
8980 isl_union_pw_aff *upa;
8981 isl_union_set *zero;
8983 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8984 if (n < 0)
8985 mupa = isl_multi_union_pw_aff_free(mupa);
8986 if (!mupa)
8987 return NULL;
8989 if (n == 0)
8990 return isl_multi_union_pw_aff_domain(mupa);
8992 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8993 zero = isl_union_pw_aff_zero_union_set(upa);
8995 for (i = 1; i < n; ++i) {
8996 isl_union_set *zero_i;
8998 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8999 zero_i = isl_union_pw_aff_zero_union_set(upa);
9001 zero = isl_union_set_intersect(zero, zero_i);
9004 isl_multi_union_pw_aff_free(mupa);
9005 return zero;
9008 /* Construct a union map mapping the shared domain
9009 * of the union piecewise affine expressions to the range of "mupa"
9010 * in the special case of a 0D multi union piecewise affine expression.
9012 * Construct a map between the explicit domain of "mupa" and
9013 * the range space.
9014 * Note that this assumes that the domain consists of explicit elements.
9016 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9017 __isl_take isl_multi_union_pw_aff *mupa)
9019 isl_bool is_params;
9020 isl_space *space;
9021 isl_union_set *dom, *ran;
9023 space = isl_multi_union_pw_aff_get_space(mupa);
9024 dom = isl_multi_union_pw_aff_domain(mupa);
9025 ran = isl_union_set_from_set(isl_set_universe(space));
9027 is_params = isl_union_set_is_params(dom);
9028 if (is_params < 0)
9029 dom = isl_union_set_free(dom);
9030 else if (is_params)
9031 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9032 "cannot create union map from expression without "
9033 "explicit domain elements",
9034 dom = isl_union_set_free(dom));
9036 return isl_union_map_from_domain_and_range(dom, ran);
9039 /* Construct a union map mapping the shared domain
9040 * of the union piecewise affine expressions to the range of "mupa"
9041 * with each dimension in the range equated to the
9042 * corresponding union piecewise affine expression.
9044 * If the input is zero-dimensional, then construct a mapping
9045 * from its explicit domain.
9047 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9048 __isl_take isl_multi_union_pw_aff *mupa)
9050 int i;
9051 isl_size n;
9052 isl_space *space;
9053 isl_union_map *umap;
9054 isl_union_pw_aff *upa;
9056 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9057 if (n < 0)
9058 mupa = isl_multi_union_pw_aff_free(mupa);
9059 if (!mupa)
9060 return NULL;
9062 if (n == 0)
9063 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9065 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9066 umap = isl_union_map_from_union_pw_aff(upa);
9068 for (i = 1; i < n; ++i) {
9069 isl_union_map *umap_i;
9071 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9072 umap_i = isl_union_map_from_union_pw_aff(upa);
9073 umap = isl_union_map_flat_range_product(umap, umap_i);
9076 space = isl_multi_union_pw_aff_get_space(mupa);
9077 umap = isl_union_map_reset_range_space(umap, space);
9079 isl_multi_union_pw_aff_free(mupa);
9080 return umap;
9083 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9084 * "range" is the space from which to set the range space.
9085 * "res" collects the results.
9087 struct isl_union_pw_multi_aff_reset_range_space_data {
9088 isl_space *range;
9089 isl_union_pw_multi_aff *res;
9092 /* Replace the range space of "pma" by the range space of data->range and
9093 * add the result to data->res.
9095 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9097 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9098 isl_space *space;
9100 space = isl_pw_multi_aff_get_space(pma);
9101 space = isl_space_domain(space);
9102 space = isl_space_extend_domain_with_range(space,
9103 isl_space_copy(data->range));
9104 pma = isl_pw_multi_aff_reset_space(pma, space);
9105 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9107 return data->res ? isl_stat_ok : isl_stat_error;
9110 /* Replace the range space of all the piecewise affine expressions in "upma" by
9111 * the range space of "space".
9113 * This assumes that all these expressions have the same output dimension.
9115 * Since the spaces of the expressions change, so do their hash values.
9116 * We therefore need to create a new isl_union_pw_multi_aff.
9117 * Note that the hash value is currently computed based on the entire
9118 * space even though there can only be a single expression with a given
9119 * domain space.
9121 static __isl_give isl_union_pw_multi_aff *
9122 isl_union_pw_multi_aff_reset_range_space(
9123 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9125 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9126 isl_space *space_upma;
9128 space_upma = isl_union_pw_multi_aff_get_space(upma);
9129 data.res = isl_union_pw_multi_aff_empty(space_upma);
9130 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9131 &reset_range_space, &data) < 0)
9132 data.res = isl_union_pw_multi_aff_free(data.res);
9134 isl_space_free(space);
9135 isl_union_pw_multi_aff_free(upma);
9136 return data.res;
9139 /* Construct and return a union piecewise multi affine expression
9140 * that is equal to the given multi union piecewise affine expression,
9141 * in the special case of a 0D multi union piecewise affine expression.
9143 * Construct a union piecewise multi affine expression
9144 * on top of the explicit domain of the input.
9146 __isl_give isl_union_pw_multi_aff *
9147 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9148 __isl_take isl_multi_union_pw_aff *mupa)
9150 isl_space *space;
9151 isl_multi_val *mv;
9152 isl_union_set *domain;
9154 space = isl_multi_union_pw_aff_get_space(mupa);
9155 mv = isl_multi_val_zero(space);
9156 domain = isl_multi_union_pw_aff_domain(mupa);
9157 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9160 /* Construct and return a union piecewise multi affine expression
9161 * that is equal to the given multi union piecewise affine expression.
9163 * If the input is zero-dimensional, then
9164 * construct a union piecewise multi affine expression
9165 * on top of the explicit domain of the input.
9167 __isl_give isl_union_pw_multi_aff *
9168 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9169 __isl_take isl_multi_union_pw_aff *mupa)
9171 int i;
9172 isl_size n;
9173 isl_space *space;
9174 isl_union_pw_multi_aff *upma;
9175 isl_union_pw_aff *upa;
9177 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9178 if (n < 0)
9179 mupa = isl_multi_union_pw_aff_free(mupa);
9180 if (!mupa)
9181 return NULL;
9183 if (n == 0)
9184 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9186 space = isl_multi_union_pw_aff_get_space(mupa);
9187 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9188 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9190 for (i = 1; i < n; ++i) {
9191 isl_union_pw_multi_aff *upma_i;
9193 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9194 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9195 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9198 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9200 isl_multi_union_pw_aff_free(mupa);
9201 return upma;
9204 /* Intersect the range of "mupa" with "range",
9205 * in the special case where "mupa" is 0D.
9207 * Intersect the domain of "mupa" with the constraints on the parameters
9208 * of "range".
9210 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9211 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9213 range = isl_set_params(range);
9214 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9215 return mupa;
9218 /* Intersect the range of "mupa" with "range".
9219 * That is, keep only those domain elements that have a function value
9220 * in "range".
9222 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9223 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9225 isl_union_pw_multi_aff *upma;
9226 isl_union_set *domain;
9227 isl_space *space;
9228 isl_size n;
9229 int match;
9231 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9232 if (n < 0 || !range)
9233 goto error;
9235 space = isl_set_get_space(range);
9236 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9237 space, isl_dim_set);
9238 isl_space_free(space);
9239 if (match < 0)
9240 goto error;
9241 if (!match)
9242 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9243 "space don't match", goto error);
9244 if (n == 0)
9245 return mupa_intersect_range_0D(mupa, range);
9247 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9248 isl_multi_union_pw_aff_copy(mupa));
9249 domain = isl_union_set_from_set(range);
9250 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9251 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9253 return mupa;
9254 error:
9255 isl_multi_union_pw_aff_free(mupa);
9256 isl_set_free(range);
9257 return NULL;
9260 /* Return the shared domain of the elements of "mupa",
9261 * in the special case where "mupa" is zero-dimensional.
9263 * Return the explicit domain of "mupa".
9264 * Note that this domain may be a parameter set, either
9265 * because "mupa" is meant to live in a set space or
9266 * because no explicit domain has been set.
9268 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9269 __isl_take isl_multi_union_pw_aff *mupa)
9271 isl_union_set *dom;
9273 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9274 isl_multi_union_pw_aff_free(mupa);
9276 return dom;
9279 /* Return the shared domain of the elements of "mupa".
9281 * If "mupa" is zero-dimensional, then return its explicit domain.
9283 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9284 __isl_take isl_multi_union_pw_aff *mupa)
9286 int i;
9287 isl_size n;
9288 isl_union_pw_aff *upa;
9289 isl_union_set *dom;
9291 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9292 if (n < 0)
9293 mupa = isl_multi_union_pw_aff_free(mupa);
9294 if (!mupa)
9295 return NULL;
9297 if (n == 0)
9298 return isl_multi_union_pw_aff_domain_0D(mupa);
9300 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9301 dom = isl_union_pw_aff_domain(upa);
9302 for (i = 1; i < n; ++i) {
9303 isl_union_set *dom_i;
9305 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9306 dom_i = isl_union_pw_aff_domain(upa);
9307 dom = isl_union_set_intersect(dom, dom_i);
9310 isl_multi_union_pw_aff_free(mupa);
9311 return dom;
9314 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9315 * In particular, the spaces have been aligned.
9316 * The result is defined over the shared domain of the elements of "mupa"
9318 * We first extract the parametric constant part of "aff" and
9319 * define that over the shared domain.
9320 * Then we iterate over all input dimensions of "aff" and add the corresponding
9321 * multiples of the elements of "mupa".
9322 * Finally, we consider the integer divisions, calling the function
9323 * recursively to obtain an isl_union_pw_aff corresponding to the
9324 * integer division argument.
9326 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9327 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9329 int i;
9330 isl_size n_in, n_div;
9331 isl_union_pw_aff *upa;
9332 isl_union_set *uset;
9333 isl_val *v;
9334 isl_aff *cst;
9336 n_in = isl_aff_dim(aff, isl_dim_in);
9337 n_div = isl_aff_dim(aff, isl_dim_div);
9338 if (n_in < 0 || n_div < 0)
9339 goto error;
9341 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9342 cst = isl_aff_copy(aff);
9343 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9344 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9345 cst = isl_aff_project_domain_on_params(cst);
9346 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9348 for (i = 0; i < n_in; ++i) {
9349 isl_union_pw_aff *upa_i;
9351 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9352 continue;
9353 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9354 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9355 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9356 upa = isl_union_pw_aff_add(upa, upa_i);
9359 for (i = 0; i < n_div; ++i) {
9360 isl_aff *div;
9361 isl_union_pw_aff *upa_i;
9363 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9364 continue;
9365 div = isl_aff_get_div(aff, i);
9366 upa_i = multi_union_pw_aff_apply_aff(
9367 isl_multi_union_pw_aff_copy(mupa), div);
9368 upa_i = isl_union_pw_aff_floor(upa_i);
9369 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9370 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9371 upa = isl_union_pw_aff_add(upa, upa_i);
9374 isl_multi_union_pw_aff_free(mupa);
9375 isl_aff_free(aff);
9377 return upa;
9378 error:
9379 isl_multi_union_pw_aff_free(mupa);
9380 isl_aff_free(aff);
9381 return NULL;
9384 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9385 * with the domain of "aff".
9386 * Furthermore, the dimension of this space needs to be greater than zero.
9387 * The result is defined over the shared domain of the elements of "mupa"
9389 * We perform these checks and then hand over control to
9390 * multi_union_pw_aff_apply_aff.
9392 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9393 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9395 isl_size dim;
9396 isl_space *space1, *space2;
9397 isl_bool equal;
9399 mupa = isl_multi_union_pw_aff_align_params(mupa,
9400 isl_aff_get_space(aff));
9401 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9402 if (!mupa || !aff)
9403 goto error;
9405 space1 = isl_multi_union_pw_aff_get_space(mupa);
9406 space2 = isl_aff_get_domain_space(aff);
9407 equal = isl_space_is_equal(space1, space2);
9408 isl_space_free(space1);
9409 isl_space_free(space2);
9410 if (equal < 0)
9411 goto error;
9412 if (!equal)
9413 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9414 "spaces don't match", goto error);
9415 dim = isl_aff_dim(aff, isl_dim_in);
9416 if (dim < 0)
9417 goto error;
9418 if (dim == 0)
9419 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9420 "cannot determine domains", goto error);
9422 return multi_union_pw_aff_apply_aff(mupa, aff);
9423 error:
9424 isl_multi_union_pw_aff_free(mupa);
9425 isl_aff_free(aff);
9426 return NULL;
9429 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9430 * The space of "mupa" is known to be compatible with the domain of "ma".
9432 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9433 * on the domain of "mupa".
9435 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9436 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9438 isl_union_set *dom;
9440 dom = isl_multi_union_pw_aff_domain(mupa);
9441 ma = isl_multi_aff_project_domain_on_params(ma);
9443 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9446 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9447 * with the domain of "ma".
9448 * The result is defined over the shared domain of the elements of "mupa"
9450 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9451 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9453 isl_space *space1, *space2;
9454 isl_multi_union_pw_aff *res;
9455 isl_bool equal;
9456 int i;
9457 isl_size n_in, n_out;
9459 mupa = isl_multi_union_pw_aff_align_params(mupa,
9460 isl_multi_aff_get_space(ma));
9461 ma = isl_multi_aff_align_params(ma,
9462 isl_multi_union_pw_aff_get_space(mupa));
9463 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9464 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9465 if (!mupa || n_in < 0 || n_out < 0)
9466 goto error;
9468 space1 = isl_multi_union_pw_aff_get_space(mupa);
9469 space2 = isl_multi_aff_get_domain_space(ma);
9470 equal = isl_space_is_equal(space1, space2);
9471 isl_space_free(space1);
9472 isl_space_free(space2);
9473 if (equal < 0)
9474 goto error;
9475 if (!equal)
9476 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9477 "spaces don't match", goto error);
9478 if (n_in == 0)
9479 return mupa_apply_multi_aff_0D(mupa, ma);
9481 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9482 res = isl_multi_union_pw_aff_alloc(space1);
9484 for (i = 0; i < n_out; ++i) {
9485 isl_aff *aff;
9486 isl_union_pw_aff *upa;
9488 aff = isl_multi_aff_get_aff(ma, i);
9489 upa = multi_union_pw_aff_apply_aff(
9490 isl_multi_union_pw_aff_copy(mupa), aff);
9491 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9494 isl_multi_aff_free(ma);
9495 isl_multi_union_pw_aff_free(mupa);
9496 return res;
9497 error:
9498 isl_multi_union_pw_aff_free(mupa);
9499 isl_multi_aff_free(ma);
9500 return NULL;
9503 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9504 * The space of "mupa" is known to be compatible with the domain of "pa".
9506 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9507 * on the domain of "mupa".
9509 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9510 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9512 isl_union_set *dom;
9514 dom = isl_multi_union_pw_aff_domain(mupa);
9515 pa = isl_pw_aff_project_domain_on_params(pa);
9517 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9520 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9521 * with the domain of "pa".
9522 * Furthermore, the dimension of this space needs to be greater than zero.
9523 * The result is defined over the shared domain of the elements of "mupa"
9525 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9526 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9528 int i;
9529 isl_bool equal;
9530 isl_size n_in;
9531 isl_space *space, *space2;
9532 isl_union_pw_aff *upa;
9534 mupa = isl_multi_union_pw_aff_align_params(mupa,
9535 isl_pw_aff_get_space(pa));
9536 pa = isl_pw_aff_align_params(pa,
9537 isl_multi_union_pw_aff_get_space(mupa));
9538 if (!mupa || !pa)
9539 goto error;
9541 space = isl_multi_union_pw_aff_get_space(mupa);
9542 space2 = isl_pw_aff_get_domain_space(pa);
9543 equal = isl_space_is_equal(space, space2);
9544 isl_space_free(space);
9545 isl_space_free(space2);
9546 if (equal < 0)
9547 goto error;
9548 if (!equal)
9549 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9550 "spaces don't match", goto error);
9551 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9552 if (n_in < 0)
9553 goto error;
9554 if (n_in == 0)
9555 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9557 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9558 upa = isl_union_pw_aff_empty(space);
9560 for (i = 0; i < pa->n; ++i) {
9561 isl_aff *aff;
9562 isl_set *domain;
9563 isl_multi_union_pw_aff *mupa_i;
9564 isl_union_pw_aff *upa_i;
9566 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9567 domain = isl_set_copy(pa->p[i].set);
9568 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9569 aff = isl_aff_copy(pa->p[i].aff);
9570 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9571 upa = isl_union_pw_aff_union_add(upa, upa_i);
9574 isl_multi_union_pw_aff_free(mupa);
9575 isl_pw_aff_free(pa);
9576 return upa;
9577 error:
9578 isl_multi_union_pw_aff_free(mupa);
9579 isl_pw_aff_free(pa);
9580 return NULL;
9583 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9584 * The space of "mupa" is known to be compatible with the domain of "pma".
9586 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9587 * on the domain of "mupa".
9589 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9590 __isl_take isl_multi_union_pw_aff *mupa,
9591 __isl_take isl_pw_multi_aff *pma)
9593 isl_union_set *dom;
9595 dom = isl_multi_union_pw_aff_domain(mupa);
9596 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9598 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9601 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9602 * with the domain of "pma".
9603 * The result is defined over the shared domain of the elements of "mupa"
9605 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9606 __isl_take isl_multi_union_pw_aff *mupa,
9607 __isl_take isl_pw_multi_aff *pma)
9609 isl_space *space1, *space2;
9610 isl_multi_union_pw_aff *res;
9611 isl_bool equal;
9612 int i;
9613 isl_size n_in, n_out;
9615 mupa = isl_multi_union_pw_aff_align_params(mupa,
9616 isl_pw_multi_aff_get_space(pma));
9617 pma = isl_pw_multi_aff_align_params(pma,
9618 isl_multi_union_pw_aff_get_space(mupa));
9619 if (!mupa || !pma)
9620 goto error;
9622 space1 = isl_multi_union_pw_aff_get_space(mupa);
9623 space2 = isl_pw_multi_aff_get_domain_space(pma);
9624 equal = isl_space_is_equal(space1, space2);
9625 isl_space_free(space1);
9626 isl_space_free(space2);
9627 if (equal < 0)
9628 goto error;
9629 if (!equal)
9630 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9631 "spaces don't match", goto error);
9632 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9633 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9634 if (n_in < 0 || n_out < 0)
9635 goto error;
9636 if (n_in == 0)
9637 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9639 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9640 res = isl_multi_union_pw_aff_alloc(space1);
9642 for (i = 0; i < n_out; ++i) {
9643 isl_pw_aff *pa;
9644 isl_union_pw_aff *upa;
9646 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9647 upa = isl_multi_union_pw_aff_apply_pw_aff(
9648 isl_multi_union_pw_aff_copy(mupa), pa);
9649 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9652 isl_pw_multi_aff_free(pma);
9653 isl_multi_union_pw_aff_free(mupa);
9654 return res;
9655 error:
9656 isl_multi_union_pw_aff_free(mupa);
9657 isl_pw_multi_aff_free(pma);
9658 return NULL;
9661 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9662 * If the explicit domain only keeps track of constraints on the parameters,
9663 * then only update those constraints.
9665 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9666 __isl_take isl_multi_union_pw_aff *mupa,
9667 __isl_keep isl_union_pw_multi_aff *upma)
9669 isl_bool is_params;
9671 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9672 return isl_multi_union_pw_aff_free(mupa);
9674 mupa = isl_multi_union_pw_aff_cow(mupa);
9675 if (!mupa)
9676 return NULL;
9678 is_params = isl_union_set_is_params(mupa->u.dom);
9679 if (is_params < 0)
9680 return isl_multi_union_pw_aff_free(mupa);
9682 upma = isl_union_pw_multi_aff_copy(upma);
9683 if (is_params)
9684 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9685 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9686 else
9687 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9688 mupa->u.dom, upma);
9689 if (!mupa->u.dom)
9690 return isl_multi_union_pw_aff_free(mupa);
9691 return mupa;
9694 /* Compute the pullback of "mupa" by the function represented by "upma".
9695 * In other words, plug in "upma" in "mupa". The result contains
9696 * expressions defined over the domain space of "upma".
9698 * Run over all elements of "mupa" and plug in "upma" in each of them.
9700 * If "mupa" has an explicit domain, then it is this domain
9701 * that needs to undergo a pullback instead, i.e., a preimage.
9703 __isl_give isl_multi_union_pw_aff *
9704 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9705 __isl_take isl_multi_union_pw_aff *mupa,
9706 __isl_take isl_union_pw_multi_aff *upma)
9708 int i;
9709 isl_size n;
9711 mupa = isl_multi_union_pw_aff_align_params(mupa,
9712 isl_union_pw_multi_aff_get_space(upma));
9713 upma = isl_union_pw_multi_aff_align_params(upma,
9714 isl_multi_union_pw_aff_get_space(mupa));
9715 mupa = isl_multi_union_pw_aff_cow(mupa);
9716 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9717 if (n < 0 || !upma)
9718 goto error;
9720 for (i = 0; i < n; ++i) {
9721 isl_union_pw_aff *upa;
9723 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9724 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9725 isl_union_pw_multi_aff_copy(upma));
9726 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9729 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9730 mupa = preimage_explicit_domain(mupa, upma);
9732 isl_union_pw_multi_aff_free(upma);
9733 return mupa;
9734 error:
9735 isl_multi_union_pw_aff_free(mupa);
9736 isl_union_pw_multi_aff_free(upma);
9737 return NULL;
9740 /* Extract the sequence of elements in "mupa" with domain space "space"
9741 * (ignoring parameters).
9743 * For the elements of "mupa" that are not defined on the specified space,
9744 * the corresponding element in the result is empty.
9746 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9747 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9749 int i;
9750 isl_size n;
9751 isl_space *space_mpa;
9752 isl_multi_pw_aff *mpa;
9754 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9755 if (n < 0 || !space)
9756 goto error;
9758 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9759 space = isl_space_replace_params(space, space_mpa);
9760 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9761 space_mpa);
9762 mpa = isl_multi_pw_aff_alloc(space_mpa);
9764 space = isl_space_from_domain(space);
9765 space = isl_space_add_dims(space, isl_dim_out, 1);
9766 for (i = 0; i < n; ++i) {
9767 isl_union_pw_aff *upa;
9768 isl_pw_aff *pa;
9770 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9771 pa = isl_union_pw_aff_extract_pw_aff(upa,
9772 isl_space_copy(space));
9773 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9774 isl_union_pw_aff_free(upa);
9777 isl_space_free(space);
9778 return mpa;
9779 error:
9780 isl_space_free(space);
9781 return NULL;
9784 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9785 * should modify the base expressions in the input.
9787 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9788 * are taken into account.
9789 * "fn" is applied to each entry in the input.
9791 struct isl_union_pw_multi_aff_un_op_control {
9792 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9793 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9796 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9797 * a second argument) for use as an isl_union_pw_multi_aff_transform
9798 * filter function (which does take a second argument).
9799 * Simply call control->filter without the second argument.
9801 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9802 __isl_take isl_pw_multi_aff *pma, void *user)
9804 struct isl_union_pw_multi_aff_un_op_control *control = user;
9806 return control->filter(pma);
9809 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9810 * a second argument) for use as an isl_union_pw_multi_aff_transform
9811 * base function (which does take a second argument).
9812 * Simply call control->fn without the second argument.
9814 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9815 __isl_take isl_pw_multi_aff *pma, void *user)
9817 struct isl_union_pw_multi_aff_un_op_control *control = user;
9819 return control->fn(pma);
9822 /* Construct an isl_union_pw_multi_aff that is obtained by
9823 * modifying "upma" according to "control".
9825 * isl_union_pw_multi_aff_transform performs essentially
9826 * the same operation, but takes a filter and a callback function
9827 * of a different form (with an extra argument).
9828 * Call isl_union_pw_multi_aff_transform with wrappers
9829 * that remove this extra argument.
9831 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9832 __isl_take isl_union_pw_multi_aff *upma,
9833 struct isl_union_pw_multi_aff_un_op_control *control)
9835 struct isl_union_pw_multi_aff_transform_control t_control = {
9836 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9837 .filter_user = control,
9838 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9839 .fn_user = control,
9842 return isl_union_pw_multi_aff_transform(upma, &t_control);
9845 /* For each function in "upma" of the form A -> [B -> C],
9846 * extract the function A -> B and collect the results.
9848 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9849 __isl_take isl_union_pw_multi_aff *upma)
9851 struct isl_union_pw_multi_aff_un_op_control control = {
9852 .filter = &isl_pw_multi_aff_range_is_wrapping,
9853 .fn = &isl_pw_multi_aff_range_factor_domain,
9855 return isl_union_pw_multi_aff_un_op(upma, &control);
9858 /* For each function in "upma" of the form A -> [B -> C],
9859 * extract the function A -> C and collect the results.
9861 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9862 __isl_take isl_union_pw_multi_aff *upma)
9864 struct isl_union_pw_multi_aff_un_op_control control = {
9865 .filter = &isl_pw_multi_aff_range_is_wrapping,
9866 .fn = &isl_pw_multi_aff_range_factor_range,
9868 return isl_union_pw_multi_aff_un_op(upma, &control);
9871 /* Evaluate the affine function "aff" in the void point "pnt".
9872 * In particular, return the value NaN.
9874 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9875 __isl_take isl_point *pnt)
9877 isl_ctx *ctx;
9879 ctx = isl_point_get_ctx(pnt);
9880 isl_aff_free(aff);
9881 isl_point_free(pnt);
9882 return isl_val_nan(ctx);
9885 /* Evaluate the affine expression "aff"
9886 * in the coordinates (with denominator) "pnt".
9888 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9889 __isl_keep isl_vec *pnt)
9891 isl_int n, d;
9892 isl_ctx *ctx;
9893 isl_val *v;
9895 if (!aff || !pnt)
9896 return NULL;
9898 ctx = isl_vec_get_ctx(aff);
9899 isl_int_init(n);
9900 isl_int_init(d);
9901 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9902 isl_int_mul(d, aff->el[0], pnt->el[0]);
9903 v = isl_val_rat_from_isl_int(ctx, n, d);
9904 v = isl_val_normalize(v);
9905 isl_int_clear(n);
9906 isl_int_clear(d);
9908 return v;
9911 /* Check that the domain space of "aff" is equal to "space".
9913 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9914 __isl_keep isl_space *space)
9916 isl_bool ok;
9918 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9919 if (ok < 0)
9920 return isl_stat_error;
9921 if (!ok)
9922 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9923 "incompatible spaces", return isl_stat_error);
9924 return isl_stat_ok;
9927 /* Evaluate the affine function "aff" in "pnt".
9929 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9930 __isl_take isl_point *pnt)
9932 isl_bool is_void;
9933 isl_val *v;
9934 isl_local_space *ls;
9936 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9937 goto error;
9938 is_void = isl_point_is_void(pnt);
9939 if (is_void < 0)
9940 goto error;
9941 if (is_void)
9942 return eval_void(aff, pnt);
9944 ls = isl_aff_get_domain_local_space(aff);
9945 pnt = isl_local_space_lift_point(ls, pnt);
9947 v = eval(aff->v, isl_point_peek_vec(pnt));
9949 isl_aff_free(aff);
9950 isl_point_free(pnt);
9952 return v;
9953 error:
9954 isl_aff_free(aff);
9955 isl_point_free(pnt);
9956 return NULL;