doc: update latest release of clang
[isl.git] / isl_aff.c
blob7aa73f780ffc5ef341c39838e7a3d918877da2b9
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 * Note that the encoding for the special value NaN
267 * is the same in isl_val and isl_aff, so this does not need
268 * to be treated in any special way.
270 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
271 __isl_take isl_val *val)
273 isl_aff *aff;
275 if (!ls || !val)
276 goto error;
277 if (!isl_val_is_rat(val) && !isl_val_is_nan(val))
278 isl_die(isl_val_get_ctx(val), isl_error_invalid,
279 "expecting rational value or NaN", goto error);
281 aff = isl_aff_alloc(isl_local_space_copy(ls));
282 if (!aff)
283 goto error;
285 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
286 isl_int_set(aff->v->el[1], val->n);
287 isl_int_set(aff->v->el[0], val->d);
289 isl_local_space_free(ls);
290 isl_val_free(val);
291 return aff;
292 error:
293 isl_local_space_free(ls);
294 isl_val_free(val);
295 return NULL;
298 /* Return an affine expression that is equal to "val" on domain space "space".
300 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
301 __isl_take isl_val *val)
303 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
306 /* Return an affine expression that is equal to the specified dimension
307 * in "ls".
309 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
310 enum isl_dim_type type, unsigned pos)
312 isl_space *space;
313 isl_aff *aff;
315 if (!ls)
316 return NULL;
318 space = isl_local_space_get_space(ls);
319 if (!space)
320 goto error;
321 if (isl_space_is_map(space))
322 isl_die(isl_space_get_ctx(space), isl_error_invalid,
323 "expecting (parameter) set space", goto error);
324 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
325 goto error;
327 isl_space_free(space);
328 aff = isl_aff_alloc(ls);
329 if (!aff)
330 return NULL;
332 pos += isl_local_space_offset(aff->ls, type);
334 isl_int_set_si(aff->v->el[0], 1);
335 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
336 isl_int_set_si(aff->v->el[1 + pos], 1);
338 return aff;
339 error:
340 isl_local_space_free(ls);
341 isl_space_free(space);
342 return NULL;
345 /* Return a piecewise affine expression that is equal to
346 * the specified dimension in "ls".
348 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
349 enum isl_dim_type type, unsigned pos)
351 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
354 /* Return an affine expression that is equal to the parameter
355 * in the domain space "space" with identifier "id".
357 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
358 __isl_take isl_space *space, __isl_take isl_id *id)
360 int pos;
361 isl_local_space *ls;
363 if (!space || !id)
364 goto error;
365 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
366 if (pos < 0)
367 isl_die(isl_space_get_ctx(space), isl_error_invalid,
368 "parameter not found in space", goto error);
369 isl_id_free(id);
370 ls = isl_local_space_from_space(space);
371 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
372 error:
373 isl_space_free(space);
374 isl_id_free(id);
375 return NULL;
378 /* This function performs the same operation as
379 * isl_aff_param_on_domain_space_id,
380 * but is considered as a function on an isl_space when exported.
382 __isl_give isl_aff *isl_space_param_aff_on_domain_id(
383 __isl_take isl_space *space, __isl_take isl_id *id)
385 return isl_aff_param_on_domain_space_id(space, id);
388 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
390 if (!aff)
391 return NULL;
393 if (--aff->ref > 0)
394 return NULL;
396 isl_local_space_free(aff->ls);
397 isl_vec_free(aff->v);
399 free(aff);
401 return NULL;
404 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
406 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
409 /* Return a hash value that digests "aff".
411 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
413 uint32_t hash, ls_hash, v_hash;
415 if (!aff)
416 return 0;
418 hash = isl_hash_init();
419 ls_hash = isl_local_space_get_hash(aff->ls);
420 isl_hash_hash(hash, ls_hash);
421 v_hash = isl_vec_get_hash(aff->v);
422 isl_hash_hash(hash, v_hash);
424 return hash;
427 /* Return the domain local space of "aff".
429 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
430 __isl_keep isl_aff *aff)
432 return aff ? aff->ls : NULL;
435 /* Return the number of variables of the given type in the domain of "aff".
437 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
439 isl_local_space *ls;
441 ls = isl_aff_peek_domain_local_space(aff);
442 return isl_local_space_dim(ls, type);
445 /* Externally, an isl_aff has a map space, but internally, the
446 * ls field corresponds to the domain of that space.
448 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
450 if (!aff)
451 return isl_size_error;
452 if (type == isl_dim_out)
453 return 1;
454 if (type == isl_dim_in)
455 type = isl_dim_set;
456 return isl_aff_domain_dim(aff, type);
459 /* Return the offset of the first coefficient of type "type" in
460 * the domain of "aff".
462 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
464 isl_local_space *ls;
466 ls = isl_aff_peek_domain_local_space(aff);
467 return isl_local_space_offset(ls, type);
470 /* Return the position of the dimension of the given type and name
471 * in "aff".
472 * Return -1 if no such dimension can be found.
474 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
475 const char *name)
477 if (!aff)
478 return -1;
479 if (type == isl_dim_out)
480 return -1;
481 if (type == isl_dim_in)
482 type = isl_dim_set;
483 return isl_local_space_find_dim_by_name(aff->ls, type, name);
486 /* Return the domain space of "aff".
488 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
490 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
493 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
495 return isl_space_copy(isl_aff_peek_domain_space(aff));
498 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
500 isl_space *space;
501 if (!aff)
502 return NULL;
503 space = isl_local_space_get_space(aff->ls);
504 space = isl_space_from_domain(space);
505 space = isl_space_add_dims(space, isl_dim_out, 1);
506 return space;
509 /* Return a copy of the domain space of "aff".
511 __isl_give isl_local_space *isl_aff_get_domain_local_space(
512 __isl_keep isl_aff *aff)
514 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
517 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
519 isl_local_space *ls;
520 if (!aff)
521 return NULL;
522 ls = isl_local_space_copy(aff->ls);
523 ls = isl_local_space_from_domain(ls);
524 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
525 return ls;
528 /* Return the local space of the domain of "aff".
529 * This may be either a copy or the local space itself
530 * if there is only one reference to "aff".
531 * This allows the local space to be modified inplace
532 * if both the expression and its local space have only a single reference.
533 * The caller is not allowed to modify "aff" between this call and
534 * a subsequent call to isl_aff_restore_domain_local_space.
535 * The only exception is that isl_aff_free can be called instead.
537 __isl_give isl_local_space *isl_aff_take_domain_local_space(
538 __isl_keep isl_aff *aff)
540 isl_local_space *ls;
542 if (!aff)
543 return NULL;
544 if (aff->ref != 1)
545 return isl_aff_get_domain_local_space(aff);
546 ls = aff->ls;
547 aff->ls = NULL;
548 return ls;
551 /* Set the local space of the domain of "aff" to "ls",
552 * where the local space of "aff" may be missing
553 * due to a preceding call to isl_aff_take_domain_local_space.
554 * However, in this case, "aff" only has a single reference and
555 * then the call to isl_aff_cow has no effect.
557 __isl_give isl_aff *isl_aff_restore_domain_local_space(
558 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
560 if (!aff || !ls)
561 goto error;
563 if (aff->ls == ls) {
564 isl_local_space_free(ls);
565 return aff;
568 aff = isl_aff_cow(aff);
569 if (!aff)
570 goto error;
571 isl_local_space_free(aff->ls);
572 aff->ls = ls;
574 return aff;
575 error:
576 isl_aff_free(aff);
577 isl_local_space_free(ls);
578 return NULL;
581 /* Externally, an isl_aff has a map space, but internally, the
582 * ls field corresponds to the domain of that space.
584 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
585 enum isl_dim_type type, unsigned pos)
587 if (!aff)
588 return NULL;
589 if (type == isl_dim_out)
590 return NULL;
591 if (type == isl_dim_in)
592 type = isl_dim_set;
593 return isl_local_space_get_dim_name(aff->ls, type, pos);
596 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
597 __isl_take isl_space *space)
599 aff = isl_aff_cow(aff);
600 if (!aff || !space)
601 goto error;
603 aff->ls = isl_local_space_reset_space(aff->ls, space);
604 if (!aff->ls)
605 return isl_aff_free(aff);
607 return aff;
608 error:
609 isl_aff_free(aff);
610 isl_space_free(space);
611 return NULL;
614 /* Reset the space of "aff". This function is called from isl_pw_templ.c
615 * and doesn't know if the space of an element object is represented
616 * directly or through its domain. It therefore passes along both.
618 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
619 __isl_take isl_space *space, __isl_take isl_space *domain)
621 isl_space_free(space);
622 return isl_aff_reset_domain_space(aff, domain);
625 /* Reorder the coefficients of the affine expression based
626 * on the given reordering.
627 * The reordering r is assumed to have been extended with the local
628 * variables.
630 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
631 __isl_take isl_reordering *r, int n_div)
633 isl_space *space;
634 isl_vec *res;
635 isl_size dim;
636 int i;
638 if (!vec || !r)
639 goto error;
641 space = isl_reordering_peek_space(r);
642 dim = isl_space_dim(space, isl_dim_all);
643 if (dim < 0)
644 goto error;
645 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
646 if (!res)
647 goto error;
648 isl_seq_cpy(res->el, vec->el, 2);
649 isl_seq_clr(res->el + 2, res->size - 2);
650 for (i = 0; i < r->len; ++i)
651 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
653 isl_reordering_free(r);
654 isl_vec_free(vec);
655 return res;
656 error:
657 isl_vec_free(vec);
658 isl_reordering_free(r);
659 return NULL;
662 /* Reorder the dimensions of the domain of "aff" according
663 * to the given reordering.
665 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
666 __isl_take isl_reordering *r)
668 aff = isl_aff_cow(aff);
669 if (!aff)
670 goto error;
672 r = isl_reordering_extend(r, aff->ls->div->n_row);
673 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
674 aff->ls->div->n_row);
675 aff->ls = isl_local_space_realign(aff->ls, r);
677 if (!aff->v || !aff->ls)
678 return isl_aff_free(aff);
680 return aff;
681 error:
682 isl_aff_free(aff);
683 isl_reordering_free(r);
684 return NULL;
687 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
688 __isl_take isl_space *model)
690 isl_bool equal_params;
692 if (!aff || !model)
693 goto error;
695 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
696 if (equal_params < 0)
697 goto error;
698 if (!equal_params) {
699 isl_reordering *exp;
701 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
702 exp = isl_reordering_extend_space(exp,
703 isl_aff_get_domain_space(aff));
704 aff = isl_aff_realign_domain(aff, exp);
707 isl_space_free(model);
708 return aff;
709 error:
710 isl_space_free(model);
711 isl_aff_free(aff);
712 return NULL;
715 #undef TYPE
716 #define TYPE isl_aff
717 #include "isl_unbind_params_templ.c"
719 /* Is "aff" obviously equal to zero?
721 * If the denominator is zero, then "aff" is not equal to zero.
723 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
725 int pos;
727 if (!aff)
728 return isl_bool_error;
730 if (isl_int_is_zero(aff->v->el[0]))
731 return isl_bool_false;
732 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
733 return isl_bool_ok(pos < 0);
736 /* Does "aff" represent NaN?
738 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
740 if (!aff)
741 return isl_bool_error;
743 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
746 /* Are "aff1" and "aff2" obviously equal?
748 * NaN is not equal to anything, not even to another NaN.
750 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
751 __isl_keep isl_aff *aff2)
753 isl_bool equal;
755 if (!aff1 || !aff2)
756 return isl_bool_error;
758 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
759 return isl_bool_false;
761 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
762 if (equal < 0 || !equal)
763 return equal;
765 return isl_vec_is_equal(aff1->v, aff2->v);
768 /* Return the common denominator of "aff" in "v".
770 * We cannot return anything meaningful in case of a NaN.
772 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
774 if (!aff)
775 return isl_stat_error;
776 if (isl_aff_is_nan(aff))
777 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
778 "cannot get denominator of NaN", return isl_stat_error);
779 isl_int_set(*v, aff->v->el[0]);
780 return isl_stat_ok;
783 /* Return the common denominator of "aff".
785 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
787 isl_ctx *ctx;
789 if (!aff)
790 return NULL;
792 ctx = isl_aff_get_ctx(aff);
793 if (isl_aff_is_nan(aff))
794 return isl_val_nan(ctx);
795 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
798 /* Return the constant term of "aff".
800 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
802 isl_ctx *ctx;
803 isl_val *v;
805 if (!aff)
806 return NULL;
808 ctx = isl_aff_get_ctx(aff);
809 if (isl_aff_is_nan(aff))
810 return isl_val_nan(ctx);
811 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
812 return isl_val_normalize(v);
815 /* Return the coefficient of the variable of type "type" at position "pos"
816 * of "aff".
818 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
819 enum isl_dim_type type, int pos)
821 isl_ctx *ctx;
822 isl_val *v;
824 if (!aff)
825 return NULL;
827 ctx = isl_aff_get_ctx(aff);
828 if (type == isl_dim_out)
829 isl_die(ctx, isl_error_invalid,
830 "output/set dimension does not have a coefficient",
831 return NULL);
832 if (type == isl_dim_in)
833 type = isl_dim_set;
835 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
836 return NULL;
838 if (isl_aff_is_nan(aff))
839 return isl_val_nan(ctx);
840 pos += isl_local_space_offset(aff->ls, type);
841 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
842 return isl_val_normalize(v);
845 /* Return the sign of the coefficient of the variable of type "type"
846 * at position "pos" of "aff".
848 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
849 int pos)
851 isl_ctx *ctx;
853 if (!aff)
854 return 0;
856 ctx = isl_aff_get_ctx(aff);
857 if (type == isl_dim_out)
858 isl_die(ctx, isl_error_invalid,
859 "output/set dimension does not have a coefficient",
860 return 0);
861 if (type == isl_dim_in)
862 type = isl_dim_set;
864 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
865 return 0;
867 pos += isl_local_space_offset(aff->ls, type);
868 return isl_int_sgn(aff->v->el[1 + pos]);
871 /* Replace the numerator of the constant term of "aff" by "v".
873 * A NaN is unaffected by this operation.
875 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
877 if (!aff)
878 return NULL;
879 if (isl_aff_is_nan(aff))
880 return aff;
881 aff = isl_aff_cow(aff);
882 if (!aff)
883 return NULL;
885 aff->v = isl_vec_cow(aff->v);
886 if (!aff->v)
887 return isl_aff_free(aff);
889 isl_int_set(aff->v->el[1], v);
891 return aff;
894 /* Replace the constant term of "aff" by "v".
896 * A NaN is unaffected by this operation.
898 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
899 __isl_take isl_val *v)
901 if (!aff || !v)
902 goto error;
904 if (isl_aff_is_nan(aff)) {
905 isl_val_free(v);
906 return aff;
909 if (!isl_val_is_rat(v))
910 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
911 "expecting rational value", goto error);
913 if (isl_int_eq(aff->v->el[1], v->n) &&
914 isl_int_eq(aff->v->el[0], v->d)) {
915 isl_val_free(v);
916 return aff;
919 aff = isl_aff_cow(aff);
920 if (!aff)
921 goto error;
922 aff->v = isl_vec_cow(aff->v);
923 if (!aff->v)
924 goto error;
926 if (isl_int_eq(aff->v->el[0], v->d)) {
927 isl_int_set(aff->v->el[1], v->n);
928 } else if (isl_int_is_one(v->d)) {
929 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
930 } else {
931 isl_seq_scale(aff->v->el + 1,
932 aff->v->el + 1, v->d, aff->v->size - 1);
933 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
934 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
935 aff->v = isl_vec_normalize(aff->v);
936 if (!aff->v)
937 goto error;
940 isl_val_free(v);
941 return aff;
942 error:
943 isl_aff_free(aff);
944 isl_val_free(v);
945 return NULL;
948 /* Add "v" to the constant term of "aff".
950 * A NaN is unaffected by this operation.
952 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
954 if (isl_int_is_zero(v))
955 return aff;
957 if (!aff)
958 return NULL;
959 if (isl_aff_is_nan(aff))
960 return aff;
961 aff = isl_aff_cow(aff);
962 if (!aff)
963 return NULL;
965 aff->v = isl_vec_cow(aff->v);
966 if (!aff->v)
967 return isl_aff_free(aff);
969 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
971 return aff;
974 /* Add "v" to the constant term of "aff",
975 * in case "aff" is a rational expression.
977 static __isl_give isl_aff *isl_aff_add_rat_constant_val(__isl_take isl_aff *aff,
978 __isl_take isl_val *v)
980 aff = isl_aff_cow(aff);
981 if (!aff)
982 goto error;
984 aff->v = isl_vec_cow(aff->v);
985 if (!aff->v)
986 goto error;
988 if (isl_int_is_one(v->d)) {
989 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
990 } else if (isl_int_eq(aff->v->el[0], v->d)) {
991 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
992 aff->v = isl_vec_normalize(aff->v);
993 if (!aff->v)
994 goto error;
995 } else {
996 isl_seq_scale(aff->v->el + 1,
997 aff->v->el + 1, v->d, aff->v->size - 1);
998 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
999 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1000 aff->v = isl_vec_normalize(aff->v);
1001 if (!aff->v)
1002 goto error;
1005 isl_val_free(v);
1006 return aff;
1007 error:
1008 isl_aff_free(aff);
1009 isl_val_free(v);
1010 return NULL;
1013 /* Return the first argument and free the second.
1015 static __isl_give isl_aff *pick_free(__isl_take isl_aff *aff,
1016 __isl_take isl_val *v)
1018 isl_val_free(v);
1019 return aff;
1022 /* Replace the first argument by NaN and free the second argument.
1024 static __isl_give isl_aff *set_nan_free_val(__isl_take isl_aff *aff,
1025 __isl_take isl_val *v)
1027 isl_val_free(v);
1028 return isl_aff_set_nan(aff);
1031 /* Add "v" to the constant term of "aff".
1033 * A NaN is unaffected by this operation.
1034 * Conversely, adding a NaN turns "aff" into a NaN.
1036 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
1037 __isl_take isl_val *v)
1039 isl_bool is_nan, is_zero, is_rat;
1041 is_nan = isl_aff_is_nan(aff);
1042 is_zero = isl_val_is_zero(v);
1043 if (is_nan < 0 || is_zero < 0)
1044 goto error;
1045 if (is_nan || is_zero)
1046 return pick_free(aff, v);
1048 is_nan = isl_val_is_nan(v);
1049 is_rat = isl_val_is_rat(v);
1050 if (is_nan < 0 || is_rat < 0)
1051 goto error;
1052 if (is_nan)
1053 return set_nan_free_val(aff, v);
1054 if (!is_rat)
1055 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1056 "expecting rational value or NaN", goto error);
1058 return isl_aff_add_rat_constant_val(aff, v);
1059 error:
1060 isl_aff_free(aff);
1061 isl_val_free(v);
1062 return NULL;
1065 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
1067 isl_int t;
1069 isl_int_init(t);
1070 isl_int_set_si(t, v);
1071 aff = isl_aff_add_constant(aff, t);
1072 isl_int_clear(t);
1074 return aff;
1077 /* Add "v" to the numerator of the constant term of "aff".
1079 * A NaN is unaffected by this operation.
1081 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
1083 if (isl_int_is_zero(v))
1084 return aff;
1086 if (!aff)
1087 return NULL;
1088 if (isl_aff_is_nan(aff))
1089 return aff;
1090 aff = isl_aff_cow(aff);
1091 if (!aff)
1092 return NULL;
1094 aff->v = isl_vec_cow(aff->v);
1095 if (!aff->v)
1096 return isl_aff_free(aff);
1098 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1100 return aff;
1103 /* Add "v" to the numerator of the constant term of "aff".
1105 * A NaN is unaffected by this operation.
1107 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1109 isl_int t;
1111 if (v == 0)
1112 return aff;
1114 isl_int_init(t);
1115 isl_int_set_si(t, v);
1116 aff = isl_aff_add_constant_num(aff, t);
1117 isl_int_clear(t);
1119 return aff;
1122 /* Replace the numerator of the constant term of "aff" by "v".
1124 * A NaN is unaffected by this operation.
1126 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1128 if (!aff)
1129 return NULL;
1130 if (isl_aff_is_nan(aff))
1131 return aff;
1132 aff = isl_aff_cow(aff);
1133 if (!aff)
1134 return NULL;
1136 aff->v = isl_vec_cow(aff->v);
1137 if (!aff->v)
1138 return isl_aff_free(aff);
1140 isl_int_set_si(aff->v->el[1], v);
1142 return aff;
1145 /* Replace the numerator of the coefficient of the variable of type "type"
1146 * at position "pos" of "aff" by "v".
1148 * A NaN is unaffected by this operation.
1150 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1151 enum isl_dim_type type, int pos, isl_int v)
1153 if (!aff)
1154 return NULL;
1156 if (type == isl_dim_out)
1157 isl_die(aff->v->ctx, isl_error_invalid,
1158 "output/set dimension does not have a coefficient",
1159 return isl_aff_free(aff));
1160 if (type == isl_dim_in)
1161 type = isl_dim_set;
1163 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1164 return isl_aff_free(aff);
1166 if (isl_aff_is_nan(aff))
1167 return aff;
1168 aff = isl_aff_cow(aff);
1169 if (!aff)
1170 return NULL;
1172 aff->v = isl_vec_cow(aff->v);
1173 if (!aff->v)
1174 return isl_aff_free(aff);
1176 pos += isl_local_space_offset(aff->ls, type);
1177 isl_int_set(aff->v->el[1 + pos], v);
1179 return aff;
1182 /* Replace the numerator of the coefficient of the variable of type "type"
1183 * at position "pos" of "aff" by "v".
1185 * A NaN is unaffected by this operation.
1187 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1188 enum isl_dim_type type, int pos, int v)
1190 if (!aff)
1191 return NULL;
1193 if (type == isl_dim_out)
1194 isl_die(aff->v->ctx, isl_error_invalid,
1195 "output/set dimension does not have a coefficient",
1196 return isl_aff_free(aff));
1197 if (type == isl_dim_in)
1198 type = isl_dim_set;
1200 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1201 return isl_aff_free(aff);
1203 if (isl_aff_is_nan(aff))
1204 return aff;
1205 pos += isl_local_space_offset(aff->ls, type);
1206 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1207 return aff;
1209 aff = isl_aff_cow(aff);
1210 if (!aff)
1211 return NULL;
1213 aff->v = isl_vec_cow(aff->v);
1214 if (!aff->v)
1215 return isl_aff_free(aff);
1217 isl_int_set_si(aff->v->el[1 + pos], v);
1219 return aff;
1222 /* Replace the coefficient of the variable of type "type" at position "pos"
1223 * of "aff" by "v".
1225 * A NaN is unaffected by this operation.
1227 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1228 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1230 if (!aff || !v)
1231 goto error;
1233 if (type == isl_dim_out)
1234 isl_die(aff->v->ctx, isl_error_invalid,
1235 "output/set dimension does not have a coefficient",
1236 goto error);
1237 if (type == isl_dim_in)
1238 type = isl_dim_set;
1240 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1241 return isl_aff_free(aff);
1243 if (isl_aff_is_nan(aff)) {
1244 isl_val_free(v);
1245 return aff;
1247 if (!isl_val_is_rat(v))
1248 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1249 "expecting rational value", goto error);
1251 pos += isl_local_space_offset(aff->ls, type);
1252 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1253 isl_int_eq(aff->v->el[0], v->d)) {
1254 isl_val_free(v);
1255 return aff;
1258 aff = isl_aff_cow(aff);
1259 if (!aff)
1260 goto error;
1261 aff->v = isl_vec_cow(aff->v);
1262 if (!aff->v)
1263 goto error;
1265 if (isl_int_eq(aff->v->el[0], v->d)) {
1266 isl_int_set(aff->v->el[1 + pos], v->n);
1267 } else if (isl_int_is_one(v->d)) {
1268 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1269 } else {
1270 isl_seq_scale(aff->v->el + 1,
1271 aff->v->el + 1, v->d, aff->v->size - 1);
1272 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1273 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1274 aff->v = isl_vec_normalize(aff->v);
1275 if (!aff->v)
1276 goto error;
1279 isl_val_free(v);
1280 return aff;
1281 error:
1282 isl_aff_free(aff);
1283 isl_val_free(v);
1284 return NULL;
1287 /* Add "v" to the coefficient of the variable of type "type"
1288 * at position "pos" of "aff".
1290 * A NaN is unaffected by this operation.
1292 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1293 enum isl_dim_type type, int pos, isl_int v)
1295 if (!aff)
1296 return NULL;
1298 if (type == isl_dim_out)
1299 isl_die(aff->v->ctx, isl_error_invalid,
1300 "output/set dimension does not have a coefficient",
1301 return isl_aff_free(aff));
1302 if (type == isl_dim_in)
1303 type = isl_dim_set;
1305 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1306 return isl_aff_free(aff);
1308 if (isl_aff_is_nan(aff))
1309 return aff;
1310 aff = isl_aff_cow(aff);
1311 if (!aff)
1312 return NULL;
1314 aff->v = isl_vec_cow(aff->v);
1315 if (!aff->v)
1316 return isl_aff_free(aff);
1318 pos += isl_local_space_offset(aff->ls, type);
1319 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1321 return aff;
1324 /* Add "v" to the coefficient of the variable of type "type"
1325 * at position "pos" of "aff".
1327 * A NaN is unaffected by this operation.
1329 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1330 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1332 if (!aff || !v)
1333 goto error;
1335 if (isl_val_is_zero(v)) {
1336 isl_val_free(v);
1337 return aff;
1340 if (type == isl_dim_out)
1341 isl_die(aff->v->ctx, isl_error_invalid,
1342 "output/set dimension does not have a coefficient",
1343 goto error);
1344 if (type == isl_dim_in)
1345 type = isl_dim_set;
1347 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1348 goto error;
1350 if (isl_aff_is_nan(aff)) {
1351 isl_val_free(v);
1352 return aff;
1354 if (!isl_val_is_rat(v))
1355 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1356 "expecting rational value", goto error);
1358 aff = isl_aff_cow(aff);
1359 if (!aff)
1360 goto error;
1362 aff->v = isl_vec_cow(aff->v);
1363 if (!aff->v)
1364 goto error;
1366 pos += isl_local_space_offset(aff->ls, type);
1367 if (isl_int_is_one(v->d)) {
1368 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1369 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1370 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1371 aff->v = isl_vec_normalize(aff->v);
1372 if (!aff->v)
1373 goto error;
1374 } else {
1375 isl_seq_scale(aff->v->el + 1,
1376 aff->v->el + 1, v->d, aff->v->size - 1);
1377 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1378 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1379 aff->v = isl_vec_normalize(aff->v);
1380 if (!aff->v)
1381 goto error;
1384 isl_val_free(v);
1385 return aff;
1386 error:
1387 isl_aff_free(aff);
1388 isl_val_free(v);
1389 return NULL;
1392 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1393 enum isl_dim_type type, int pos, int v)
1395 isl_int t;
1397 isl_int_init(t);
1398 isl_int_set_si(t, v);
1399 aff = isl_aff_add_coefficient(aff, type, pos, t);
1400 isl_int_clear(t);
1402 return aff;
1405 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1407 if (!aff)
1408 return NULL;
1410 return isl_local_space_get_div(aff->ls, pos);
1413 /* Return the negation of "aff".
1415 * As a special case, -NaN = NaN.
1417 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1419 if (!aff)
1420 return NULL;
1421 if (isl_aff_is_nan(aff))
1422 return aff;
1423 aff = isl_aff_cow(aff);
1424 if (!aff)
1425 return NULL;
1426 aff->v = isl_vec_cow(aff->v);
1427 if (!aff->v)
1428 return isl_aff_free(aff);
1430 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1432 return aff;
1435 /* Remove divs from the local space that do not appear in the affine
1436 * expression.
1437 * We currently only remove divs at the end.
1438 * Some intermediate divs may also not appear directly in the affine
1439 * expression, but we would also need to check that no other divs are
1440 * defined in terms of them.
1442 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1444 int pos;
1445 isl_size off;
1446 isl_size n;
1448 n = isl_aff_domain_dim(aff, isl_dim_div);
1449 off = isl_aff_domain_offset(aff, isl_dim_div);
1450 if (n < 0 || off < 0)
1451 return isl_aff_free(aff);
1453 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1454 if (pos == n)
1455 return aff;
1457 aff = isl_aff_cow(aff);
1458 if (!aff)
1459 return NULL;
1461 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1462 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1463 if (!aff->ls || !aff->v)
1464 return isl_aff_free(aff);
1466 return aff;
1469 /* Look for any divs in the aff->ls with a denominator equal to one
1470 * and plug them into the affine expression and any subsequent divs
1471 * that may reference the div.
1473 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1475 int i;
1476 isl_size n;
1477 int len;
1478 isl_int v;
1479 isl_vec *vec;
1480 isl_local_space *ls;
1481 isl_size off;
1483 n = isl_aff_domain_dim(aff, isl_dim_div);
1484 off = isl_aff_domain_offset(aff, isl_dim_div);
1485 if (n < 0 || off < 0)
1486 return isl_aff_free(aff);
1487 len = aff->v->size;
1488 for (i = 0; i < n; ++i) {
1489 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1490 continue;
1491 ls = isl_local_space_copy(aff->ls);
1492 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1493 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1494 vec = isl_vec_copy(aff->v);
1495 vec = isl_vec_cow(vec);
1496 if (!ls || !vec)
1497 goto error;
1499 isl_int_init(v);
1501 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1502 len, len, v);
1504 isl_int_clear(v);
1506 isl_vec_free(aff->v);
1507 aff->v = vec;
1508 isl_local_space_free(aff->ls);
1509 aff->ls = ls;
1512 return aff;
1513 error:
1514 isl_vec_free(vec);
1515 isl_local_space_free(ls);
1516 return isl_aff_free(aff);
1519 /* Look for any divs j that appear with a unit coefficient inside
1520 * the definitions of other divs i and plug them into the definitions
1521 * of the divs i.
1523 * In particular, an expression of the form
1525 * floor((f(..) + floor(g(..)/n))/m)
1527 * is simplified to
1529 * floor((n * f(..) + g(..))/(n * m))
1531 * This simplification is correct because we can move the expression
1532 * f(..) into the inner floor in the original expression to obtain
1534 * floor(floor((n * f(..) + g(..))/n)/m)
1536 * from which we can derive the simplified expression.
1538 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1540 int i, j;
1541 isl_size n;
1542 isl_size off;
1544 n = isl_aff_domain_dim(aff, isl_dim_div);
1545 off = isl_aff_domain_offset(aff, isl_dim_div);
1546 if (n < 0 || off < 0)
1547 return isl_aff_free(aff);
1548 for (i = 1; i < n; ++i) {
1549 for (j = 0; j < i; ++j) {
1550 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1551 continue;
1552 aff->ls = isl_local_space_substitute_seq(aff->ls,
1553 isl_dim_div, j, aff->ls->div->row[j],
1554 aff->v->size, i, 1);
1555 if (!aff->ls)
1556 return isl_aff_free(aff);
1560 return aff;
1563 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1565 * Even though this function is only called on isl_affs with a single
1566 * reference, we are careful to only change aff->v and aff->ls together.
1568 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1570 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1571 isl_local_space *ls;
1572 isl_vec *v;
1574 if (off < 0)
1575 return isl_aff_free(aff);
1577 ls = isl_local_space_copy(aff->ls);
1578 ls = isl_local_space_swap_div(ls, a, b);
1579 v = isl_vec_copy(aff->v);
1580 v = isl_vec_cow(v);
1581 if (!ls || !v)
1582 goto error;
1584 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1585 isl_vec_free(aff->v);
1586 aff->v = v;
1587 isl_local_space_free(aff->ls);
1588 aff->ls = ls;
1590 return aff;
1591 error:
1592 isl_vec_free(v);
1593 isl_local_space_free(ls);
1594 return isl_aff_free(aff);
1597 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1599 * We currently do not actually remove div "b", but simply add its
1600 * coefficient to that of "a" and then zero it out.
1602 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1604 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1606 if (off < 0)
1607 return isl_aff_free(aff);
1609 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1610 return aff;
1612 aff->v = isl_vec_cow(aff->v);
1613 if (!aff->v)
1614 return isl_aff_free(aff);
1616 isl_int_add(aff->v->el[1 + off + a],
1617 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1618 isl_int_set_si(aff->v->el[1 + off + b], 0);
1620 return aff;
1623 /* Sort the divs in the local space of "aff" according to
1624 * the comparison function "cmp_row" in isl_local_space.c,
1625 * combining the coefficients of identical divs.
1627 * Reordering divs does not change the semantics of "aff",
1628 * so there is no need to call isl_aff_cow.
1629 * Moreover, this function is currently only called on isl_affs
1630 * with a single reference.
1632 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1634 isl_size n;
1635 int i, j;
1637 n = isl_aff_dim(aff, isl_dim_div);
1638 if (n < 0)
1639 return isl_aff_free(aff);
1640 for (i = 1; i < n; ++i) {
1641 for (j = i - 1; j >= 0; --j) {
1642 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1643 if (cmp < 0)
1644 break;
1645 if (cmp == 0)
1646 aff = merge_divs(aff, j, j + 1);
1647 else
1648 aff = swap_div(aff, j, j + 1);
1649 if (!aff)
1650 return NULL;
1654 return aff;
1657 /* Normalize the representation of "aff".
1659 * This function should only be called on "new" isl_affs, i.e.,
1660 * with only a single reference. We therefore do not need to
1661 * worry about affecting other instances.
1663 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1665 if (!aff)
1666 return NULL;
1667 aff->v = isl_vec_normalize(aff->v);
1668 if (!aff->v)
1669 return isl_aff_free(aff);
1670 aff = plug_in_integral_divs(aff);
1671 aff = plug_in_unit_divs(aff);
1672 aff = sort_divs(aff);
1673 aff = isl_aff_remove_unused_divs(aff);
1674 return aff;
1677 /* Given f, return floor(f).
1678 * If f is an integer expression, then just return f.
1679 * If f is a constant, then return the constant floor(f).
1680 * Otherwise, if f = g/m, write g = q m + r,
1681 * create a new div d = [r/m] and return the expression q + d.
1682 * The coefficients in r are taken to lie between -m/2 and m/2.
1684 * reduce_div_coefficients performs the same normalization.
1686 * As a special case, floor(NaN) = NaN.
1688 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1690 int i;
1691 int size;
1692 isl_ctx *ctx;
1693 isl_vec *div;
1695 if (!aff)
1696 return NULL;
1698 if (isl_aff_is_nan(aff))
1699 return aff;
1700 if (isl_int_is_one(aff->v->el[0]))
1701 return aff;
1703 aff = isl_aff_cow(aff);
1704 if (!aff)
1705 return NULL;
1707 aff->v = isl_vec_cow(aff->v);
1708 if (!aff->v)
1709 return isl_aff_free(aff);
1711 if (isl_aff_is_cst(aff)) {
1712 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1713 isl_int_set_si(aff->v->el[0], 1);
1714 return aff;
1717 div = isl_vec_copy(aff->v);
1718 div = isl_vec_cow(div);
1719 if (!div)
1720 return isl_aff_free(aff);
1722 ctx = isl_aff_get_ctx(aff);
1723 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1724 for (i = 1; i < aff->v->size; ++i) {
1725 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1726 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1727 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1728 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1729 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1733 aff->ls = isl_local_space_add_div(aff->ls, div);
1734 if (!aff->ls)
1735 return isl_aff_free(aff);
1737 size = aff->v->size;
1738 aff->v = isl_vec_extend(aff->v, size + 1);
1739 if (!aff->v)
1740 return isl_aff_free(aff);
1741 isl_int_set_si(aff->v->el[0], 1);
1742 isl_int_set_si(aff->v->el[size], 1);
1744 aff = isl_aff_normalize(aff);
1746 return aff;
1749 /* Compute
1751 * aff mod m = aff - m * floor(aff/m)
1753 * with m an integer value.
1755 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1756 __isl_take isl_val *m)
1758 isl_aff *res;
1760 if (!aff || !m)
1761 goto error;
1763 if (!isl_val_is_int(m))
1764 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1765 "expecting integer modulo", goto error);
1767 res = isl_aff_copy(aff);
1768 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1769 aff = isl_aff_floor(aff);
1770 aff = isl_aff_scale_val(aff, m);
1771 res = isl_aff_sub(res, aff);
1773 return res;
1774 error:
1775 isl_aff_free(aff);
1776 isl_val_free(m);
1777 return NULL;
1780 /* Compute
1782 * pwaff mod m = pwaff - m * floor(pwaff/m)
1784 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1786 isl_pw_aff *res;
1788 res = isl_pw_aff_copy(pwaff);
1789 pwaff = isl_pw_aff_scale_down(pwaff, m);
1790 pwaff = isl_pw_aff_floor(pwaff);
1791 pwaff = isl_pw_aff_scale(pwaff, m);
1792 res = isl_pw_aff_sub(res, pwaff);
1794 return res;
1797 /* Compute
1799 * pa mod m = pa - m * floor(pa/m)
1801 * with m an integer value.
1803 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1804 __isl_take isl_val *m)
1806 if (!pa || !m)
1807 goto error;
1808 if (!isl_val_is_int(m))
1809 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1810 "expecting integer modulo", goto error);
1811 pa = isl_pw_aff_mod(pa, m->n);
1812 isl_val_free(m);
1813 return pa;
1814 error:
1815 isl_pw_aff_free(pa);
1816 isl_val_free(m);
1817 return NULL;
1820 /* Given f, return ceil(f).
1821 * If f is an integer expression, then just return f.
1822 * Otherwise, let f be the expression
1824 * e/m
1826 * then return
1828 * floor((e + m - 1)/m)
1830 * As a special case, ceil(NaN) = NaN.
1832 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1834 if (!aff)
1835 return NULL;
1837 if (isl_aff_is_nan(aff))
1838 return aff;
1839 if (isl_int_is_one(aff->v->el[0]))
1840 return aff;
1842 aff = isl_aff_cow(aff);
1843 if (!aff)
1844 return NULL;
1845 aff->v = isl_vec_cow(aff->v);
1846 if (!aff->v)
1847 return isl_aff_free(aff);
1849 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1850 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1851 aff = isl_aff_floor(aff);
1853 return aff;
1856 /* Apply the expansion computed by isl_merge_divs.
1857 * The expansion itself is given by "exp" while the resulting
1858 * list of divs is given by "div".
1860 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1861 __isl_take isl_mat *div, int *exp)
1863 isl_size old_n_div;
1864 isl_size new_n_div;
1865 isl_size offset;
1867 aff = isl_aff_cow(aff);
1869 offset = isl_aff_domain_offset(aff, isl_dim_div);
1870 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1871 new_n_div = isl_mat_rows(div);
1872 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1873 goto error;
1875 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1876 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1877 if (!aff->v || !aff->ls)
1878 return isl_aff_free(aff);
1879 return aff;
1880 error:
1881 isl_aff_free(aff);
1882 isl_mat_free(div);
1883 return NULL;
1886 /* Add two affine expressions that live in the same local space.
1888 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1889 __isl_take isl_aff *aff2)
1891 isl_int gcd, f;
1893 aff1 = isl_aff_cow(aff1);
1894 if (!aff1 || !aff2)
1895 goto error;
1897 aff1->v = isl_vec_cow(aff1->v);
1898 if (!aff1->v)
1899 goto error;
1901 isl_int_init(gcd);
1902 isl_int_init(f);
1903 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1904 isl_int_divexact(f, aff2->v->el[0], gcd);
1905 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1906 isl_int_divexact(f, aff1->v->el[0], gcd);
1907 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1908 isl_int_divexact(f, aff2->v->el[0], gcd);
1909 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1910 isl_int_clear(f);
1911 isl_int_clear(gcd);
1913 isl_aff_free(aff2);
1914 aff1 = isl_aff_normalize(aff1);
1915 return aff1;
1916 error:
1917 isl_aff_free(aff1);
1918 isl_aff_free(aff2);
1919 return NULL;
1922 /* Replace one of the arguments by a NaN and free the other one.
1924 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1925 __isl_take isl_aff *aff2)
1927 isl_aff_free(aff2);
1928 return isl_aff_set_nan(aff1);
1931 /* Return the sum of "aff1" and "aff2".
1933 * If either of the two is NaN, then the result is NaN.
1935 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1936 __isl_take isl_aff *aff2)
1938 isl_ctx *ctx;
1939 int *exp1 = NULL;
1940 int *exp2 = NULL;
1941 isl_mat *div;
1942 isl_size n_div1, n_div2;
1944 if (!aff1 || !aff2)
1945 goto error;
1947 ctx = isl_aff_get_ctx(aff1);
1948 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1949 isl_die(ctx, isl_error_invalid,
1950 "spaces don't match", goto error);
1952 if (isl_aff_is_nan(aff1)) {
1953 isl_aff_free(aff2);
1954 return aff1;
1956 if (isl_aff_is_nan(aff2)) {
1957 isl_aff_free(aff1);
1958 return aff2;
1961 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1962 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1963 if (n_div1 < 0 || n_div2 < 0)
1964 goto error;
1965 if (n_div1 == 0 && n_div2 == 0)
1966 return add_expanded(aff1, aff2);
1968 exp1 = isl_alloc_array(ctx, int, n_div1);
1969 exp2 = isl_alloc_array(ctx, int, n_div2);
1970 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1971 goto error;
1973 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1974 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1975 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1976 free(exp1);
1977 free(exp2);
1979 return add_expanded(aff1, aff2);
1980 error:
1981 free(exp1);
1982 free(exp2);
1983 isl_aff_free(aff1);
1984 isl_aff_free(aff2);
1985 return NULL;
1988 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1989 __isl_take isl_aff *aff2)
1991 return isl_aff_add(aff1, isl_aff_neg(aff2));
1994 /* Return the result of scaling "aff" by a factor of "f".
1996 * As a special case, f * NaN = NaN.
1998 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
2000 isl_int gcd;
2002 if (!aff)
2003 return NULL;
2004 if (isl_aff_is_nan(aff))
2005 return aff;
2007 if (isl_int_is_one(f))
2008 return aff;
2010 aff = isl_aff_cow(aff);
2011 if (!aff)
2012 return NULL;
2013 aff->v = isl_vec_cow(aff->v);
2014 if (!aff->v)
2015 return isl_aff_free(aff);
2017 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
2018 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
2019 return aff;
2022 isl_int_init(gcd);
2023 isl_int_gcd(gcd, aff->v->el[0], f);
2024 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
2025 isl_int_divexact(gcd, f, gcd);
2026 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2027 isl_int_clear(gcd);
2029 return aff;
2032 /* Multiple "aff" by "v".
2034 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
2035 __isl_take isl_val *v)
2037 if (!aff || !v)
2038 goto error;
2040 if (isl_val_is_one(v)) {
2041 isl_val_free(v);
2042 return aff;
2045 if (!isl_val_is_rat(v))
2046 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2047 "expecting rational factor", goto error);
2049 aff = isl_aff_scale(aff, v->n);
2050 aff = isl_aff_scale_down(aff, v->d);
2052 isl_val_free(v);
2053 return aff;
2054 error:
2055 isl_aff_free(aff);
2056 isl_val_free(v);
2057 return NULL;
2060 /* Return the result of scaling "aff" down by a factor of "f".
2062 * As a special case, NaN/f = NaN.
2064 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
2066 isl_int gcd;
2068 if (!aff)
2069 return NULL;
2070 if (isl_aff_is_nan(aff))
2071 return aff;
2073 if (isl_int_is_one(f))
2074 return aff;
2076 aff = isl_aff_cow(aff);
2077 if (!aff)
2078 return NULL;
2080 if (isl_int_is_zero(f))
2081 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2082 "cannot scale down by zero", return isl_aff_free(aff));
2084 aff->v = isl_vec_cow(aff->v);
2085 if (!aff->v)
2086 return isl_aff_free(aff);
2088 isl_int_init(gcd);
2089 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
2090 isl_int_gcd(gcd, gcd, f);
2091 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
2092 isl_int_divexact(gcd, f, gcd);
2093 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
2094 isl_int_clear(gcd);
2096 return aff;
2099 /* Divide "aff" by "v".
2101 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
2102 __isl_take isl_val *v)
2104 if (!aff || !v)
2105 goto error;
2107 if (isl_val_is_one(v)) {
2108 isl_val_free(v);
2109 return aff;
2112 if (!isl_val_is_rat(v))
2113 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2114 "expecting rational factor", goto error);
2115 if (!isl_val_is_pos(v))
2116 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2117 "factor needs to be positive", goto error);
2119 aff = isl_aff_scale(aff, v->d);
2120 aff = isl_aff_scale_down(aff, v->n);
2122 isl_val_free(v);
2123 return aff;
2124 error:
2125 isl_aff_free(aff);
2126 isl_val_free(v);
2127 return NULL;
2130 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2132 isl_int v;
2134 if (f == 1)
2135 return aff;
2137 isl_int_init(v);
2138 isl_int_set_ui(v, f);
2139 aff = isl_aff_scale_down(aff, v);
2140 isl_int_clear(v);
2142 return aff;
2145 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2146 enum isl_dim_type type, unsigned pos, const char *s)
2148 aff = isl_aff_cow(aff);
2149 if (!aff)
2150 return NULL;
2151 if (type == isl_dim_out)
2152 isl_die(aff->v->ctx, isl_error_invalid,
2153 "cannot set name of output/set dimension",
2154 return isl_aff_free(aff));
2155 if (type == isl_dim_in)
2156 type = isl_dim_set;
2157 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2158 if (!aff->ls)
2159 return isl_aff_free(aff);
2161 return aff;
2164 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2165 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2167 aff = isl_aff_cow(aff);
2168 if (!aff)
2169 goto error;
2170 if (type == isl_dim_out)
2171 isl_die(aff->v->ctx, isl_error_invalid,
2172 "cannot set name of output/set dimension",
2173 goto error);
2174 if (type == isl_dim_in)
2175 type = isl_dim_set;
2176 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2177 if (!aff->ls)
2178 return isl_aff_free(aff);
2180 return aff;
2181 error:
2182 isl_id_free(id);
2183 isl_aff_free(aff);
2184 return NULL;
2187 /* Replace the identifier of the input tuple of "aff" by "id".
2188 * type is currently required to be equal to isl_dim_in
2190 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2191 enum isl_dim_type type, __isl_take isl_id *id)
2193 aff = isl_aff_cow(aff);
2194 if (!aff)
2195 goto error;
2196 if (type != isl_dim_in)
2197 isl_die(aff->v->ctx, isl_error_invalid,
2198 "cannot only set id of input tuple", goto error);
2199 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2200 if (!aff->ls)
2201 return isl_aff_free(aff);
2203 return aff;
2204 error:
2205 isl_id_free(id);
2206 isl_aff_free(aff);
2207 return NULL;
2210 /* Exploit the equalities in "eq" to simplify the affine expression
2211 * and the expressions of the integer divisions in the local space.
2212 * The integer divisions in this local space are assumed to appear
2213 * as regular dimensions in "eq".
2215 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2216 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2218 int i, j;
2219 unsigned o_div;
2220 unsigned n_div;
2222 if (!eq)
2223 goto error;
2224 if (eq->n_eq == 0) {
2225 isl_basic_set_free(eq);
2226 return aff;
2229 aff = isl_aff_cow(aff);
2230 if (!aff)
2231 goto error;
2233 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2234 isl_basic_set_copy(eq));
2235 aff->v = isl_vec_cow(aff->v);
2236 if (!aff->ls || !aff->v)
2237 goto error;
2239 o_div = isl_basic_set_offset(eq, isl_dim_div);
2240 n_div = eq->n_div;
2241 for (i = 0; i < eq->n_eq; ++i) {
2242 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2243 if (j < 0 || j == 0 || j >= o_div)
2244 continue;
2246 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2247 &aff->v->el[0]);
2250 isl_basic_set_free(eq);
2251 aff = isl_aff_normalize(aff);
2252 return aff;
2253 error:
2254 isl_basic_set_free(eq);
2255 isl_aff_free(aff);
2256 return NULL;
2259 /* Exploit the equalities in "eq" to simplify the affine expression
2260 * and the expressions of the integer divisions in the local space.
2262 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2263 __isl_take isl_basic_set *eq)
2265 isl_size n_div;
2267 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2268 if (n_div < 0)
2269 goto error;
2270 if (n_div > 0)
2271 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2272 return isl_aff_substitute_equalities_lifted(aff, eq);
2273 error:
2274 isl_basic_set_free(eq);
2275 isl_aff_free(aff);
2276 return NULL;
2279 /* Look for equalities among the variables shared by context and aff
2280 * and the integer divisions of aff, if any.
2281 * The equalities are then used to eliminate coefficients and/or integer
2282 * divisions from aff.
2284 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2285 __isl_take isl_set *context)
2287 isl_local_space *ls;
2288 isl_basic_set *hull;
2290 ls = isl_aff_get_domain_local_space(aff);
2291 context = isl_local_space_lift_set(ls, context);
2293 hull = isl_set_affine_hull(context);
2294 return isl_aff_substitute_equalities_lifted(aff, hull);
2297 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2298 __isl_take isl_set *context)
2300 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2301 dom_context = isl_set_intersect_params(dom_context, context);
2302 return isl_aff_gist(aff, dom_context);
2305 /* Return a basic set containing those elements in the space
2306 * of aff where it is positive. "rational" should not be set.
2308 * If "aff" is NaN, then it is not positive.
2310 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2311 int rational, void *user)
2313 isl_constraint *ineq;
2314 isl_basic_set *bset;
2315 isl_val *c;
2317 if (!aff)
2318 return NULL;
2319 if (isl_aff_is_nan(aff)) {
2320 isl_space *space = isl_aff_get_domain_space(aff);
2321 isl_aff_free(aff);
2322 return isl_basic_set_empty(space);
2324 if (rational)
2325 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2326 "rational sets not supported", goto error);
2328 ineq = isl_inequality_from_aff(aff);
2329 c = isl_constraint_get_constant_val(ineq);
2330 c = isl_val_sub_ui(c, 1);
2331 ineq = isl_constraint_set_constant_val(ineq, c);
2333 bset = isl_basic_set_from_constraint(ineq);
2334 bset = isl_basic_set_simplify(bset);
2335 return bset;
2336 error:
2337 isl_aff_free(aff);
2338 return NULL;
2341 /* Return a basic set containing those elements in the space
2342 * of aff where it is non-negative.
2343 * If "rational" is set, then return a rational basic set.
2345 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2347 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2348 __isl_take isl_aff *aff, int rational, void *user)
2350 isl_constraint *ineq;
2351 isl_basic_set *bset;
2353 if (!aff)
2354 return NULL;
2355 if (isl_aff_is_nan(aff)) {
2356 isl_space *space = isl_aff_get_domain_space(aff);
2357 isl_aff_free(aff);
2358 return isl_basic_set_empty(space);
2361 ineq = isl_inequality_from_aff(aff);
2363 bset = isl_basic_set_from_constraint(ineq);
2364 if (rational)
2365 bset = isl_basic_set_set_rational(bset);
2366 bset = isl_basic_set_simplify(bset);
2367 return bset;
2370 /* Return a basic set containing those elements in the space
2371 * of aff where it is non-negative.
2373 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2375 return aff_nonneg_basic_set(aff, 0, NULL);
2378 /* Return a basic set containing those elements in the domain space
2379 * of "aff" where it is positive.
2381 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2383 aff = isl_aff_add_constant_num_si(aff, -1);
2384 return isl_aff_nonneg_basic_set(aff);
2387 /* Return a basic set containing those elements in the domain space
2388 * of aff where it is negative.
2390 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2392 aff = isl_aff_neg(aff);
2393 return isl_aff_pos_basic_set(aff);
2396 /* Return a basic set containing those elements in the space
2397 * of aff where it is zero.
2398 * If "rational" is set, then return a rational basic set.
2400 * If "aff" is NaN, then it is not zero.
2402 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2403 int rational, void *user)
2405 isl_constraint *ineq;
2406 isl_basic_set *bset;
2408 if (!aff)
2409 return NULL;
2410 if (isl_aff_is_nan(aff)) {
2411 isl_space *space = isl_aff_get_domain_space(aff);
2412 isl_aff_free(aff);
2413 return isl_basic_set_empty(space);
2416 ineq = isl_equality_from_aff(aff);
2418 bset = isl_basic_set_from_constraint(ineq);
2419 if (rational)
2420 bset = isl_basic_set_set_rational(bset);
2421 bset = isl_basic_set_simplify(bset);
2422 return bset;
2425 /* Return a basic set containing those elements in the space
2426 * of aff where it is zero.
2428 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2430 return aff_zero_basic_set(aff, 0, NULL);
2433 /* Return a basic set containing those elements in the shared space
2434 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2436 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2437 __isl_take isl_aff *aff2)
2439 aff1 = isl_aff_sub(aff1, aff2);
2441 return isl_aff_nonneg_basic_set(aff1);
2444 /* Return a basic set containing those elements in the shared domain space
2445 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2447 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2448 __isl_take isl_aff *aff2)
2450 aff1 = isl_aff_sub(aff1, aff2);
2452 return isl_aff_pos_basic_set(aff1);
2455 /* Return a set containing those elements in the shared space
2456 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2458 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2459 __isl_take isl_aff *aff2)
2461 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2464 /* Return a set containing those elements in the shared domain space
2465 * of aff1 and aff2 where aff1 is greater than aff2.
2467 * If either of the two inputs is NaN, then the result is empty,
2468 * as comparisons with NaN always return false.
2470 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2471 __isl_take isl_aff *aff2)
2473 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2476 /* Return a basic set containing those elements in the shared space
2477 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2479 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2480 __isl_take isl_aff *aff2)
2482 return isl_aff_ge_basic_set(aff2, aff1);
2485 /* Return a basic set containing those elements in the shared domain space
2486 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2488 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2489 __isl_take isl_aff *aff2)
2491 return isl_aff_gt_basic_set(aff2, aff1);
2494 /* Return a set containing those elements in the shared space
2495 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2497 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2498 __isl_take isl_aff *aff2)
2500 return isl_aff_ge_set(aff2, aff1);
2503 /* Return a set containing those elements in the shared domain space
2504 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2506 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2507 __isl_take isl_aff *aff2)
2509 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2512 /* Return a basic set containing those elements in the shared space
2513 * of aff1 and aff2 where aff1 and aff2 are equal.
2515 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2516 __isl_take isl_aff *aff2)
2518 aff1 = isl_aff_sub(aff1, aff2);
2520 return isl_aff_zero_basic_set(aff1);
2523 /* Return a set containing those elements in the shared space
2524 * of aff1 and aff2 where aff1 and aff2 are equal.
2526 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2527 __isl_take isl_aff *aff2)
2529 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2532 /* Return a set containing those elements in the shared domain space
2533 * of aff1 and aff2 where aff1 and aff2 are not equal.
2535 * If either of the two inputs is NaN, then the result is empty,
2536 * as comparisons with NaN always return false.
2538 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2539 __isl_take isl_aff *aff2)
2541 isl_set *set_lt, *set_gt;
2543 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2544 isl_aff_copy(aff2));
2545 set_gt = isl_aff_gt_set(aff1, aff2);
2546 return isl_set_union_disjoint(set_lt, set_gt);
2549 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2550 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2552 aff1 = isl_aff_add(aff1, aff2);
2553 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2554 return aff1;
2557 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2559 if (!aff)
2560 return isl_bool_error;
2562 return isl_bool_false;
2565 #undef TYPE
2566 #define TYPE isl_aff
2567 static
2568 #include "check_type_range_templ.c"
2570 /* Check whether the given affine expression has non-zero coefficient
2571 * for any dimension in the given range or if any of these dimensions
2572 * appear with non-zero coefficients in any of the integer divisions
2573 * involved in the affine expression.
2575 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2576 enum isl_dim_type type, unsigned first, unsigned n)
2578 int i;
2579 int *active = NULL;
2580 isl_bool involves = isl_bool_false;
2582 if (!aff)
2583 return isl_bool_error;
2584 if (n == 0)
2585 return isl_bool_false;
2586 if (isl_aff_check_range(aff, type, first, n) < 0)
2587 return isl_bool_error;
2589 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2590 if (!active)
2591 goto error;
2593 first += isl_local_space_offset(aff->ls, type) - 1;
2594 for (i = 0; i < n; ++i)
2595 if (active[first + i]) {
2596 involves = isl_bool_true;
2597 break;
2600 free(active);
2602 return involves;
2603 error:
2604 free(active);
2605 return isl_bool_error;
2608 /* Does "aff" involve any local variables, i.e., integer divisions?
2610 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2612 isl_size n;
2614 n = isl_aff_dim(aff, isl_dim_div);
2615 if (n < 0)
2616 return isl_bool_error;
2617 return isl_bool_ok(n > 0);
2620 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2621 enum isl_dim_type type, unsigned first, unsigned n)
2623 if (!aff)
2624 return NULL;
2625 if (type == isl_dim_out)
2626 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2627 "cannot drop output/set dimension",
2628 return isl_aff_free(aff));
2629 if (type == isl_dim_in)
2630 type = isl_dim_set;
2631 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2632 return aff;
2634 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2635 return isl_aff_free(aff);
2637 aff = isl_aff_cow(aff);
2638 if (!aff)
2639 return NULL;
2641 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2642 if (!aff->ls)
2643 return isl_aff_free(aff);
2645 first += 1 + isl_local_space_offset(aff->ls, type);
2646 aff->v = isl_vec_drop_els(aff->v, first, n);
2647 if (!aff->v)
2648 return isl_aff_free(aff);
2650 return aff;
2653 /* Is the domain of "aff" a product?
2655 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2657 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2660 #undef TYPE
2661 #define TYPE isl_aff
2662 #include <isl_domain_factor_templ.c>
2664 /* Project the domain of the affine expression onto its parameter space.
2665 * The affine expression may not involve any of the domain dimensions.
2667 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2669 isl_space *space;
2670 isl_size n;
2672 n = isl_aff_dim(aff, isl_dim_in);
2673 if (n < 0)
2674 return isl_aff_free(aff);
2675 aff = isl_aff_drop_domain(aff, 0, n);
2676 space = isl_aff_get_domain_space(aff);
2677 space = isl_space_params(space);
2678 aff = isl_aff_reset_domain_space(aff, space);
2679 return aff;
2682 /* Convert an affine expression defined over a parameter domain
2683 * into one that is defined over a zero-dimensional set.
2685 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2687 isl_local_space *ls;
2689 ls = isl_aff_take_domain_local_space(aff);
2690 ls = isl_local_space_set_from_params(ls);
2691 aff = isl_aff_restore_domain_local_space(aff, ls);
2693 return aff;
2696 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2697 enum isl_dim_type type, unsigned first, unsigned n)
2699 if (!aff)
2700 return NULL;
2701 if (type == isl_dim_out)
2702 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2703 "cannot insert output/set dimensions",
2704 return isl_aff_free(aff));
2705 if (type == isl_dim_in)
2706 type = isl_dim_set;
2707 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2708 return aff;
2710 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2711 return isl_aff_free(aff);
2713 aff = isl_aff_cow(aff);
2714 if (!aff)
2715 return NULL;
2717 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2718 if (!aff->ls)
2719 return isl_aff_free(aff);
2721 first += 1 + isl_local_space_offset(aff->ls, type);
2722 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2723 if (!aff->v)
2724 return isl_aff_free(aff);
2726 return aff;
2729 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2730 enum isl_dim_type type, unsigned n)
2732 isl_size pos;
2734 pos = isl_aff_dim(aff, type);
2735 if (pos < 0)
2736 return isl_aff_free(aff);
2738 return isl_aff_insert_dims(aff, type, pos, n);
2741 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2742 * to dimensions of "dst_type" at "dst_pos".
2744 * We only support moving input dimensions to parameters and vice versa.
2746 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2747 enum isl_dim_type dst_type, unsigned dst_pos,
2748 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2750 unsigned g_dst_pos;
2751 unsigned g_src_pos;
2752 isl_size src_off, dst_off;
2754 if (!aff)
2755 return NULL;
2756 if (n == 0 &&
2757 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2758 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2759 return aff;
2761 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2762 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2763 "cannot move output/set dimension",
2764 return isl_aff_free(aff));
2765 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2766 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2767 "cannot move divs", return isl_aff_free(aff));
2768 if (dst_type == isl_dim_in)
2769 dst_type = isl_dim_set;
2770 if (src_type == isl_dim_in)
2771 src_type = isl_dim_set;
2773 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2774 return isl_aff_free(aff);
2775 if (dst_type == src_type)
2776 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2777 "moving dims within the same type not supported",
2778 return isl_aff_free(aff));
2780 aff = isl_aff_cow(aff);
2781 src_off = isl_aff_domain_offset(aff, src_type);
2782 dst_off = isl_aff_domain_offset(aff, dst_type);
2783 if (src_off < 0 || dst_off < 0)
2784 return isl_aff_free(aff);
2786 g_src_pos = 1 + src_off + src_pos;
2787 g_dst_pos = 1 + dst_off + dst_pos;
2788 if (dst_type > src_type)
2789 g_dst_pos -= n;
2791 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2792 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2793 src_type, src_pos, n);
2794 if (!aff->v || !aff->ls)
2795 return isl_aff_free(aff);
2797 aff = sort_divs(aff);
2799 return aff;
2802 /* Return a zero isl_aff in the given space.
2804 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2805 * interface over all piecewise types.
2807 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2809 isl_local_space *ls;
2811 ls = isl_local_space_from_space(isl_space_domain(space));
2812 return isl_aff_zero_on_domain(ls);
2815 #define isl_aff_involves_nan isl_aff_is_nan
2817 #undef PW
2818 #define PW isl_pw_aff
2819 #undef BASE
2820 #define BASE aff
2821 #undef EL_IS_ZERO
2822 #define EL_IS_ZERO is_empty
2823 #undef ZERO
2824 #define ZERO empty
2825 #undef IS_ZERO
2826 #define IS_ZERO is_empty
2827 #undef FIELD
2828 #define FIELD aff
2829 #undef DEFAULT_IS_ZERO
2830 #define DEFAULT_IS_ZERO 0
2832 #include <isl_pw_templ.c>
2833 #include <isl_pw_un_op_templ.c>
2834 #include <isl_pw_add_constant_val_templ.c>
2835 #include <isl_pw_bind_domain_templ.c>
2836 #include <isl_pw_eval.c>
2837 #include <isl_pw_hash.c>
2838 #include <isl_pw_insert_dims_templ.c>
2839 #include <isl_pw_insert_domain_templ.c>
2840 #include <isl_pw_move_dims_templ.c>
2841 #include <isl_pw_neg_templ.c>
2842 #include <isl_pw_pullback_templ.c>
2843 #include <isl_pw_sub_templ.c>
2844 #include <isl_pw_union_opt.c>
2846 #undef BASE
2847 #define BASE pw_aff
2849 #include <isl_union_single.c>
2850 #include <isl_union_neg.c>
2852 #undef BASE
2853 #define BASE aff
2855 #include <isl_union_pw_templ.c>
2857 /* Compute a piecewise quasi-affine expression with a domain that
2858 * is the union of those of pwaff1 and pwaff2 and such that on each
2859 * cell, the quasi-affine expression is the maximum of those of pwaff1
2860 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2861 * cell, then the associated expression is the defined one.
2863 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2864 __isl_take isl_pw_aff *pwaff2)
2866 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2867 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2870 /* Compute a piecewise quasi-affine expression with a domain that
2871 * is the union of those of pwaff1 and pwaff2 and such that on each
2872 * cell, the quasi-affine expression is the minimum of those of pwaff1
2873 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2874 * cell, then the associated expression is the defined one.
2876 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2877 __isl_take isl_pw_aff *pwaff2)
2879 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2880 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2883 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2884 __isl_take isl_pw_aff *pwaff2, int max)
2886 if (max)
2887 return isl_pw_aff_union_max(pwaff1, pwaff2);
2888 else
2889 return isl_pw_aff_union_min(pwaff1, pwaff2);
2892 /* Is the domain of "pa" a product?
2894 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2896 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2899 #undef TYPE
2900 #define TYPE isl_pw_aff
2901 #include <isl_domain_factor_templ.c>
2903 /* Return a set containing those elements in the domain
2904 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2905 * does not satisfy "fn" (if complement is 1).
2907 * The pieces with a NaN never belong to the result since
2908 * NaN does not satisfy any property.
2910 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2911 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2912 void *user),
2913 int complement, void *user)
2915 int i;
2916 isl_set *set;
2918 if (!pwaff)
2919 return NULL;
2921 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2923 for (i = 0; i < pwaff->n; ++i) {
2924 isl_basic_set *bset;
2925 isl_set *set_i, *locus;
2926 isl_bool rational;
2928 if (isl_aff_is_nan(pwaff->p[i].aff))
2929 continue;
2931 rational = isl_set_has_rational(pwaff->p[i].set);
2932 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2933 locus = isl_set_from_basic_set(bset);
2934 set_i = isl_set_copy(pwaff->p[i].set);
2935 if (complement)
2936 set_i = isl_set_subtract(set_i, locus);
2937 else
2938 set_i = isl_set_intersect(set_i, locus);
2939 set = isl_set_union_disjoint(set, set_i);
2942 isl_pw_aff_free(pwaff);
2944 return set;
2947 /* Return a set containing those elements in the domain
2948 * of "pa" where it is positive.
2950 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2952 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2955 /* Return a set containing those elements in the domain
2956 * of pwaff where it is non-negative.
2958 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2960 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2963 /* Return a set containing those elements in the domain
2964 * of pwaff where it is zero.
2966 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2968 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2971 /* Return a set containing those elements in the domain
2972 * of pwaff where it is not zero.
2974 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2976 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2979 /* Bind the affine function "aff" to the parameter "id",
2980 * returning the elements in the domain where the affine expression
2981 * is equal to the parameter.
2983 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2984 __isl_take isl_id *id)
2986 isl_space *space;
2987 isl_aff *aff_id;
2989 space = isl_aff_get_domain_space(aff);
2990 space = isl_space_add_param_id(space, isl_id_copy(id));
2992 aff = isl_aff_align_params(aff, isl_space_copy(space));
2993 aff_id = isl_aff_param_on_domain_space_id(space, id);
2995 return isl_aff_eq_basic_set(aff, aff_id);
2998 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2999 * "rational" should not be set.
3001 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
3002 int rational, void *user)
3004 isl_id *id = user;
3006 if (!aff)
3007 return NULL;
3008 if (rational)
3009 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
3010 "rational binding not supported", goto error);
3011 return isl_aff_bind_id(aff, isl_id_copy(id));
3012 error:
3013 isl_aff_free(aff);
3014 return NULL;
3017 /* Bind the piecewise affine function "pa" to the parameter "id",
3018 * returning the elements in the domain where the expression
3019 * is equal to the parameter.
3021 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
3022 __isl_take isl_id *id)
3024 isl_set *bound;
3026 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
3027 isl_id_free(id);
3029 return bound;
3032 /* Return a set containing those elements in the shared domain
3033 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
3035 * We compute the difference on the shared domain and then construct
3036 * the set of values where this difference is non-negative.
3037 * If strict is set, we first subtract 1 from the difference.
3038 * If equal is set, we only return the elements where pwaff1 and pwaff2
3039 * are equal.
3041 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
3042 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
3044 isl_set *set1, *set2;
3046 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
3047 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
3048 set1 = isl_set_intersect(set1, set2);
3049 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
3050 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
3051 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
3053 if (strict) {
3054 isl_space *space = isl_set_get_space(set1);
3055 isl_aff *aff;
3056 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
3057 aff = isl_aff_add_constant_si(aff, -1);
3058 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
3059 } else
3060 isl_set_free(set1);
3062 if (equal)
3063 return isl_pw_aff_zero_set(pwaff1);
3064 return isl_pw_aff_nonneg_set(pwaff1);
3067 /* Return a set containing those elements in the shared domain
3068 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
3070 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
3071 __isl_take isl_pw_aff *pwaff2)
3073 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3074 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
3077 /* Return a set containing those elements in the shared domain
3078 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3080 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3081 __isl_take isl_pw_aff *pwaff2)
3083 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3084 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3087 /* Return a set containing those elements in the shared domain
3088 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3090 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3091 __isl_take isl_pw_aff *pwaff2)
3093 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3094 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3097 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3098 __isl_take isl_pw_aff *pwaff2)
3100 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3103 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3104 __isl_take isl_pw_aff *pwaff2)
3106 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3109 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3110 * where the function values are ordered in the same way as "order",
3111 * which returns a set in the shared domain of its two arguments.
3113 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3114 * We first pull back the two functions such that they are defined on
3115 * the domain [A -> B]. Then we apply "order", resulting in a set
3116 * in the space [A -> B]. Finally, we unwrap this set to obtain
3117 * a map in the space A -> B.
3119 static __isl_give isl_map *isl_pw_aff_order_map(
3120 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3121 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3122 __isl_take isl_pw_aff *pa2))
3124 isl_space *space1, *space2;
3125 isl_multi_aff *ma;
3126 isl_set *set;
3128 isl_pw_aff_align_params_bin(&pa1, &pa2);
3129 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3130 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3131 space1 = isl_space_map_from_domain_and_range(space1, space2);
3132 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3133 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3134 ma = isl_multi_aff_range_map(space1);
3135 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3136 set = order(pa1, pa2);
3138 return isl_set_unwrap(set);
3141 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3142 * where the function values are equal.
3144 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3145 __isl_take isl_pw_aff *pa2)
3147 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3150 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3151 * where the function value of "pa1" is less than or equal to
3152 * the function value of "pa2".
3154 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3155 __isl_take isl_pw_aff *pa2)
3157 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3160 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3161 * where the function value of "pa1" is less than the function value of "pa2".
3163 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3164 __isl_take isl_pw_aff *pa2)
3166 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3169 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3170 * where the function value of "pa1" is greater than or equal to
3171 * the function value of "pa2".
3173 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3174 __isl_take isl_pw_aff *pa2)
3176 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3179 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3180 * where the function value of "pa1" is greater than the function value
3181 * of "pa2".
3183 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3184 __isl_take isl_pw_aff *pa2)
3186 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3189 /* Return a set containing those elements in the shared domain
3190 * of the elements of list1 and list2 where each element in list1
3191 * has the relation specified by "fn" with each element in list2.
3193 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3194 __isl_take isl_pw_aff_list *list2,
3195 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3196 __isl_take isl_pw_aff *pwaff2))
3198 int i, j;
3199 isl_ctx *ctx;
3200 isl_set *set;
3202 if (!list1 || !list2)
3203 goto error;
3205 ctx = isl_pw_aff_list_get_ctx(list1);
3206 if (list1->n < 1 || list2->n < 1)
3207 isl_die(ctx, isl_error_invalid,
3208 "list should contain at least one element", goto error);
3210 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3211 for (i = 0; i < list1->n; ++i)
3212 for (j = 0; j < list2->n; ++j) {
3213 isl_set *set_ij;
3215 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3216 isl_pw_aff_copy(list2->p[j]));
3217 set = isl_set_intersect(set, set_ij);
3220 isl_pw_aff_list_free(list1);
3221 isl_pw_aff_list_free(list2);
3222 return set;
3223 error:
3224 isl_pw_aff_list_free(list1);
3225 isl_pw_aff_list_free(list2);
3226 return NULL;
3229 /* Return a set containing those elements in the shared domain
3230 * of the elements of list1 and list2 where each element in list1
3231 * is equal to each element in list2.
3233 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3234 __isl_take isl_pw_aff_list *list2)
3236 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3239 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3240 __isl_take isl_pw_aff_list *list2)
3242 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3245 /* Return a set containing those elements in the shared domain
3246 * of the elements of list1 and list2 where each element in list1
3247 * is less than or equal to each element in list2.
3249 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3250 __isl_take isl_pw_aff_list *list2)
3252 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3255 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3256 __isl_take isl_pw_aff_list *list2)
3258 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3261 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3262 __isl_take isl_pw_aff_list *list2)
3264 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3267 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3268 __isl_take isl_pw_aff_list *list2)
3270 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3274 /* Return a set containing those elements in the shared domain
3275 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3277 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3278 __isl_take isl_pw_aff *pwaff2)
3280 isl_set *set_lt, *set_gt;
3282 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3283 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3284 isl_pw_aff_copy(pwaff2));
3285 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3286 return isl_set_union_disjoint(set_lt, set_gt);
3289 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3290 isl_int v)
3292 int i;
3294 if (isl_int_is_one(v))
3295 return pwaff;
3296 if (!isl_int_is_pos(v))
3297 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3298 "factor needs to be positive",
3299 return isl_pw_aff_free(pwaff));
3300 pwaff = isl_pw_aff_cow(pwaff);
3301 if (!pwaff)
3302 return NULL;
3303 if (pwaff->n == 0)
3304 return pwaff;
3306 for (i = 0; i < pwaff->n; ++i) {
3307 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3308 if (!pwaff->p[i].aff)
3309 return isl_pw_aff_free(pwaff);
3312 return pwaff;
3315 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3317 return isl_pw_aff_un_op(pwaff, &isl_aff_floor);
3320 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3322 return isl_pw_aff_un_op(pwaff, &isl_aff_ceil);
3325 /* Assuming that "cond1" and "cond2" are disjoint,
3326 * return an affine expression that is equal to pwaff1 on cond1
3327 * and to pwaff2 on cond2.
3329 static __isl_give isl_pw_aff *isl_pw_aff_select(
3330 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3331 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3333 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3334 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3336 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3339 /* Return an affine expression that is equal to pwaff_true for elements
3340 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3341 * is zero.
3342 * That is, return cond ? pwaff_true : pwaff_false;
3344 * If "cond" involves and NaN, then we conservatively return a NaN
3345 * on its entire domain. In principle, we could consider the pieces
3346 * where it is NaN separately from those where it is not.
3348 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3349 * then only use the domain of "cond" to restrict the domain.
3351 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3352 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3354 isl_set *cond_true, *cond_false;
3355 isl_bool equal;
3357 if (!cond)
3358 goto error;
3359 if (isl_pw_aff_involves_nan(cond)) {
3360 isl_space *space = isl_pw_aff_get_domain_space(cond);
3361 isl_local_space *ls = isl_local_space_from_space(space);
3362 isl_pw_aff_free(cond);
3363 isl_pw_aff_free(pwaff_true);
3364 isl_pw_aff_free(pwaff_false);
3365 return isl_pw_aff_nan_on_domain(ls);
3368 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3369 isl_pw_aff_get_space(pwaff_false));
3370 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3371 isl_pw_aff_get_space(pwaff_true));
3372 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3373 if (equal < 0)
3374 goto error;
3375 if (equal) {
3376 isl_set *dom;
3378 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3379 isl_pw_aff_free(pwaff_false);
3380 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3383 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3384 cond_false = isl_pw_aff_zero_set(cond);
3385 return isl_pw_aff_select(cond_true, pwaff_true,
3386 cond_false, pwaff_false);
3387 error:
3388 isl_pw_aff_free(cond);
3389 isl_pw_aff_free(pwaff_true);
3390 isl_pw_aff_free(pwaff_false);
3391 return NULL;
3394 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3396 int pos;
3398 if (!aff)
3399 return isl_bool_error;
3401 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3402 return isl_bool_ok(pos == -1);
3405 /* Check whether pwaff is a piecewise constant.
3407 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3409 int i;
3411 if (!pwaff)
3412 return isl_bool_error;
3414 for (i = 0; i < pwaff->n; ++i) {
3415 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3416 if (is_cst < 0 || !is_cst)
3417 return is_cst;
3420 return isl_bool_true;
3423 /* Return the product of "aff1" and "aff2".
3425 * If either of the two is NaN, then the result is NaN.
3427 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3429 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3430 __isl_take isl_aff *aff2)
3432 if (!aff1 || !aff2)
3433 goto error;
3435 if (isl_aff_is_nan(aff1)) {
3436 isl_aff_free(aff2);
3437 return aff1;
3439 if (isl_aff_is_nan(aff2)) {
3440 isl_aff_free(aff1);
3441 return aff2;
3444 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3445 return isl_aff_mul(aff2, aff1);
3447 if (!isl_aff_is_cst(aff2))
3448 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3449 "at least one affine expression should be constant",
3450 goto error);
3452 aff1 = isl_aff_cow(aff1);
3453 if (!aff1 || !aff2)
3454 goto error;
3456 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3457 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3459 isl_aff_free(aff2);
3460 return aff1;
3461 error:
3462 isl_aff_free(aff1);
3463 isl_aff_free(aff2);
3464 return NULL;
3467 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3469 * If either of the two is NaN, then the result is NaN.
3470 * A division by zero also results in NaN.
3472 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3473 __isl_take isl_aff *aff2)
3475 isl_bool is_cst, is_zero;
3476 int neg;
3478 if (!aff1 || !aff2)
3479 goto error;
3481 if (isl_aff_is_nan(aff1)) {
3482 isl_aff_free(aff2);
3483 return aff1;
3485 if (isl_aff_is_nan(aff2)) {
3486 isl_aff_free(aff1);
3487 return aff2;
3490 is_cst = isl_aff_is_cst(aff2);
3491 if (is_cst < 0)
3492 goto error;
3493 if (!is_cst)
3494 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3495 "second argument should be a constant", goto error);
3496 is_zero = isl_aff_plain_is_zero(aff2);
3497 if (is_zero < 0)
3498 goto error;
3499 if (is_zero)
3500 return set_nan_free(aff1, aff2);
3502 neg = isl_int_is_neg(aff2->v->el[1]);
3503 if (neg) {
3504 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3505 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3508 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3509 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3511 if (neg) {
3512 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3513 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3516 isl_aff_free(aff2);
3517 return aff1;
3518 error:
3519 isl_aff_free(aff1);
3520 isl_aff_free(aff2);
3521 return NULL;
3524 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3525 __isl_take isl_pw_aff *pwaff2)
3527 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3528 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3531 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3532 __isl_take isl_pw_aff *pwaff2)
3534 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3537 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3538 __isl_take isl_pw_aff *pwaff2)
3540 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3541 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3544 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3546 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3547 __isl_take isl_pw_aff *pa2)
3549 int is_cst;
3551 is_cst = isl_pw_aff_is_cst(pa2);
3552 if (is_cst < 0)
3553 goto error;
3554 if (!is_cst)
3555 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3556 "second argument should be a piecewise constant",
3557 goto error);
3558 isl_pw_aff_align_params_bin(&pa1, &pa2);
3559 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3560 error:
3561 isl_pw_aff_free(pa1);
3562 isl_pw_aff_free(pa2);
3563 return NULL;
3566 /* Compute the quotient of the integer division of "pa1" by "pa2"
3567 * with rounding towards zero.
3568 * "pa2" is assumed to be a piecewise constant.
3570 * In particular, return
3572 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3575 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3576 __isl_take isl_pw_aff *pa2)
3578 int is_cst;
3579 isl_set *cond;
3580 isl_pw_aff *f, *c;
3582 is_cst = isl_pw_aff_is_cst(pa2);
3583 if (is_cst < 0)
3584 goto error;
3585 if (!is_cst)
3586 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3587 "second argument should be a piecewise constant",
3588 goto error);
3590 pa1 = isl_pw_aff_div(pa1, pa2);
3592 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3593 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3594 c = isl_pw_aff_ceil(pa1);
3595 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3596 error:
3597 isl_pw_aff_free(pa1);
3598 isl_pw_aff_free(pa2);
3599 return NULL;
3602 /* Compute the remainder of the integer division of "pa1" by "pa2"
3603 * with rounding towards zero.
3604 * "pa2" is assumed to be a piecewise constant.
3606 * In particular, return
3608 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3611 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3612 __isl_take isl_pw_aff *pa2)
3614 int is_cst;
3615 isl_pw_aff *res;
3617 is_cst = isl_pw_aff_is_cst(pa2);
3618 if (is_cst < 0)
3619 goto error;
3620 if (!is_cst)
3621 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3622 "second argument should be a piecewise constant",
3623 goto error);
3624 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3625 res = isl_pw_aff_mul(pa2, res);
3626 res = isl_pw_aff_sub(pa1, res);
3627 return res;
3628 error:
3629 isl_pw_aff_free(pa1);
3630 isl_pw_aff_free(pa2);
3631 return NULL;
3634 /* Does either of "pa1" or "pa2" involve any NaN?
3636 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3637 __isl_keep isl_pw_aff *pa2)
3639 isl_bool has_nan;
3641 has_nan = isl_pw_aff_involves_nan(pa1);
3642 if (has_nan < 0 || has_nan)
3643 return has_nan;
3644 return isl_pw_aff_involves_nan(pa2);
3647 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3648 * by a NaN on their shared domain.
3650 * In principle, the result could be refined to only being NaN
3651 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3653 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3654 __isl_take isl_pw_aff *pa2)
3656 isl_local_space *ls;
3657 isl_set *dom;
3658 isl_pw_aff *pa;
3660 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3661 ls = isl_local_space_from_space(isl_set_get_space(dom));
3662 pa = isl_pw_aff_nan_on_domain(ls);
3663 pa = isl_pw_aff_intersect_domain(pa, dom);
3665 return pa;
3668 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3669 __isl_take isl_pw_aff *pwaff2)
3671 isl_set *le;
3672 isl_set *dom;
3674 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3675 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3676 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3677 isl_pw_aff_copy(pwaff2));
3678 dom = isl_set_subtract(dom, isl_set_copy(le));
3679 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3682 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3683 __isl_take isl_pw_aff *pwaff2)
3685 isl_set *ge;
3686 isl_set *dom;
3688 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3689 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3690 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3691 isl_pw_aff_copy(pwaff2));
3692 dom = isl_set_subtract(dom, isl_set_copy(ge));
3693 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3696 /* Return an expression for the minimum (if "max" is not set) or
3697 * the maximum (if "max" is set) of "pa1" and "pa2".
3698 * If either expression involves any NaN, then return a NaN
3699 * on the shared domain as result.
3701 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3702 __isl_take isl_pw_aff *pa2, int max)
3704 isl_bool has_nan;
3706 has_nan = either_involves_nan(pa1, pa2);
3707 if (has_nan < 0)
3708 pa1 = isl_pw_aff_free(pa1);
3709 else if (has_nan)
3710 return replace_by_nan(pa1, pa2);
3712 isl_pw_aff_align_params_bin(&pa1, &pa2);
3713 if (max)
3714 return pw_aff_max(pa1, pa2);
3715 else
3716 return pw_aff_min(pa1, pa2);
3719 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3721 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3722 __isl_take isl_pw_aff *pwaff2)
3724 return pw_aff_min_max(pwaff1, pwaff2, 0);
3727 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3729 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3730 __isl_take isl_pw_aff *pwaff2)
3732 return pw_aff_min_max(pwaff1, pwaff2, 1);
3735 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3736 __isl_take isl_pw_aff_list *list,
3737 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3738 __isl_take isl_pw_aff *pwaff2))
3740 int i;
3741 isl_ctx *ctx;
3742 isl_pw_aff *res;
3744 if (!list)
3745 return NULL;
3747 ctx = isl_pw_aff_list_get_ctx(list);
3748 if (list->n < 1)
3749 isl_die(ctx, isl_error_invalid,
3750 "list should contain at least one element", goto error);
3752 res = isl_pw_aff_copy(list->p[0]);
3753 for (i = 1; i < list->n; ++i)
3754 res = fn(res, isl_pw_aff_copy(list->p[i]));
3756 isl_pw_aff_list_free(list);
3757 return res;
3758 error:
3759 isl_pw_aff_list_free(list);
3760 return NULL;
3763 /* Return an isl_pw_aff that maps each element in the intersection of the
3764 * domains of the elements of list to the minimal corresponding affine
3765 * expression.
3767 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3769 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3772 /* Return an isl_pw_aff that maps each element in the intersection of the
3773 * domains of the elements of list to the maximal corresponding affine
3774 * expression.
3776 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3778 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3781 /* Mark the domains of "pwaff" as rational.
3783 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3785 int i;
3787 pwaff = isl_pw_aff_cow(pwaff);
3788 if (!pwaff)
3789 return NULL;
3790 if (pwaff->n == 0)
3791 return pwaff;
3793 for (i = 0; i < pwaff->n; ++i) {
3794 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3795 if (!pwaff->p[i].set)
3796 return isl_pw_aff_free(pwaff);
3799 return pwaff;
3802 /* Mark the domains of the elements of "list" as rational.
3804 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3805 __isl_take isl_pw_aff_list *list)
3807 int i, n;
3809 if (!list)
3810 return NULL;
3811 if (list->n == 0)
3812 return list;
3814 n = list->n;
3815 for (i = 0; i < n; ++i) {
3816 isl_pw_aff *pa;
3818 pa = isl_pw_aff_list_get_pw_aff(list, i);
3819 pa = isl_pw_aff_set_rational(pa);
3820 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3823 return list;
3826 /* Do the parameters of "aff" match those of "space"?
3828 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3829 __isl_keep isl_space *space)
3831 isl_space *aff_space;
3832 isl_bool match;
3834 if (!aff || !space)
3835 return isl_bool_error;
3837 aff_space = isl_aff_get_domain_space(aff);
3839 match = isl_space_has_equal_params(space, aff_space);
3841 isl_space_free(aff_space);
3842 return match;
3845 /* Check that the domain space of "aff" matches "space".
3847 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3848 __isl_keep isl_space *space)
3850 isl_space *aff_space;
3851 isl_bool match;
3853 if (!aff || !space)
3854 return isl_stat_error;
3856 aff_space = isl_aff_get_domain_space(aff);
3858 match = isl_space_has_equal_params(space, aff_space);
3859 if (match < 0)
3860 goto error;
3861 if (!match)
3862 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3863 "parameters don't match", goto error);
3864 match = isl_space_tuple_is_equal(space, isl_dim_in,
3865 aff_space, isl_dim_set);
3866 if (match < 0)
3867 goto error;
3868 if (!match)
3869 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3870 "domains don't match", goto error);
3871 isl_space_free(aff_space);
3872 return isl_stat_ok;
3873 error:
3874 isl_space_free(aff_space);
3875 return isl_stat_error;
3878 /* Return the shared (universe) domain of the elements of "ma".
3880 * Since an isl_multi_aff (and an isl_aff) is always total,
3881 * the domain is always the universe set in its domain space.
3882 * This is a helper function for use in the generic isl_multi_*_bind.
3884 static __isl_give isl_basic_set *isl_multi_aff_domain(
3885 __isl_take isl_multi_aff *ma)
3887 isl_space *space;
3889 space = isl_multi_aff_get_space(ma);
3890 isl_multi_aff_free(ma);
3892 return isl_basic_set_universe(isl_space_domain(space));
3895 #undef BASE
3896 #define BASE aff
3898 #include <isl_multi_no_explicit_domain.c>
3899 #include <isl_multi_templ.c>
3900 #include <isl_multi_un_op_templ.c>
3901 #include <isl_multi_bin_val_templ.c>
3902 #include <isl_multi_add_constant_templ.c>
3903 #include <isl_multi_apply_set.c>
3904 #include <isl_multi_arith_templ.c>
3905 #include <isl_multi_bind_domain_templ.c>
3906 #include <isl_multi_cmp.c>
3907 #include <isl_multi_dim_id_templ.c>
3908 #include <isl_multi_dims.c>
3909 #include <isl_multi_floor.c>
3910 #include <isl_multi_from_base_templ.c>
3911 #include <isl_multi_identity_templ.c>
3912 #include <isl_multi_insert_domain_templ.c>
3913 #include <isl_multi_locals_templ.c>
3914 #include <isl_multi_move_dims_templ.c>
3915 #include <isl_multi_nan_templ.c>
3916 #include <isl_multi_product_templ.c>
3917 #include <isl_multi_splice_templ.c>
3918 #include <isl_multi_tuple_id_templ.c>
3919 #include <isl_multi_unbind_params_templ.c>
3920 #include <isl_multi_zero_templ.c>
3922 #undef DOMBASE
3923 #define DOMBASE set
3924 #include <isl_multi_gist.c>
3926 #undef DOMBASE
3927 #define DOMBASE basic_set
3928 #include <isl_multi_bind_templ.c>
3930 /* Construct an isl_multi_aff living in "space" that corresponds
3931 * to the affine transformation matrix "mat".
3933 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3934 __isl_take isl_space *space, __isl_take isl_mat *mat)
3936 isl_ctx *ctx;
3937 isl_local_space *ls = NULL;
3938 isl_multi_aff *ma = NULL;
3939 isl_size n_row, n_col, n_out, total;
3940 int i;
3942 if (!space || !mat)
3943 goto error;
3945 ctx = isl_mat_get_ctx(mat);
3947 n_row = isl_mat_rows(mat);
3948 n_col = isl_mat_cols(mat);
3949 n_out = isl_space_dim(space, isl_dim_out);
3950 total = isl_space_dim(space, isl_dim_all);
3951 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3952 goto error;
3953 if (n_row < 1)
3954 isl_die(ctx, isl_error_invalid,
3955 "insufficient number of rows", goto error);
3956 if (n_col < 1)
3957 isl_die(ctx, isl_error_invalid,
3958 "insufficient number of columns", goto error);
3959 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3960 isl_die(ctx, isl_error_invalid,
3961 "dimension mismatch", goto error);
3963 ma = isl_multi_aff_zero(isl_space_copy(space));
3964 space = isl_space_domain(space);
3965 ls = isl_local_space_from_space(isl_space_copy(space));
3967 for (i = 0; i < n_row - 1; ++i) {
3968 isl_vec *v;
3969 isl_aff *aff;
3971 v = isl_vec_alloc(ctx, 1 + n_col);
3972 if (!v)
3973 goto error;
3974 isl_int_set(v->el[0], mat->row[0][0]);
3975 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3976 v = isl_vec_normalize(v);
3977 aff = isl_aff_alloc_vec_validated(isl_local_space_copy(ls), v);
3978 ma = isl_multi_aff_set_aff(ma, i, aff);
3981 isl_space_free(space);
3982 isl_local_space_free(ls);
3983 isl_mat_free(mat);
3984 return ma;
3985 error:
3986 isl_space_free(space);
3987 isl_local_space_free(ls);
3988 isl_mat_free(mat);
3989 isl_multi_aff_free(ma);
3990 return NULL;
3993 /* Return the constant terms of the affine expressions of "ma".
3995 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3996 __isl_keep isl_multi_aff *ma)
3998 int i;
3999 isl_size n;
4000 isl_space *space;
4001 isl_multi_val *mv;
4003 n = isl_multi_aff_size(ma);
4004 if (n < 0)
4005 return NULL;
4006 space = isl_space_range(isl_multi_aff_get_space(ma));
4007 space = isl_space_drop_all_params(space);
4008 mv = isl_multi_val_zero(space);
4010 for (i = 0; i < n; ++i) {
4011 isl_aff *aff;
4012 isl_val *val;
4014 aff = isl_multi_aff_get_at(ma, i);
4015 val = isl_aff_get_constant_val(aff);
4016 isl_aff_free(aff);
4017 mv = isl_multi_val_set_at(mv, i, val);
4020 return mv;
4023 /* Remove any internal structure of the domain of "ma".
4024 * If there is any such internal structure in the input,
4025 * then the name of the corresponding space is also removed.
4027 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
4028 __isl_take isl_multi_aff *ma)
4030 isl_space *space;
4032 if (!ma)
4033 return NULL;
4035 if (!ma->space->nested[0])
4036 return ma;
4038 space = isl_multi_aff_get_space(ma);
4039 space = isl_space_flatten_domain(space);
4040 ma = isl_multi_aff_reset_space(ma, space);
4042 return ma;
4045 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4046 * of the space to its domain.
4048 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
4050 int i;
4051 isl_size n_in;
4052 isl_local_space *ls;
4053 isl_multi_aff *ma;
4055 if (!space)
4056 return NULL;
4057 if (!isl_space_is_map(space))
4058 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4059 "not a map space", goto error);
4061 n_in = isl_space_dim(space, isl_dim_in);
4062 if (n_in < 0)
4063 goto error;
4064 space = isl_space_domain_map(space);
4066 ma = isl_multi_aff_alloc(isl_space_copy(space));
4067 if (n_in == 0) {
4068 isl_space_free(space);
4069 return ma;
4072 space = isl_space_domain(space);
4073 ls = isl_local_space_from_space(space);
4074 for (i = 0; i < n_in; ++i) {
4075 isl_aff *aff;
4077 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4078 isl_dim_set, i);
4079 ma = isl_multi_aff_set_aff(ma, i, aff);
4081 isl_local_space_free(ls);
4082 return ma;
4083 error:
4084 isl_space_free(space);
4085 return NULL;
4088 /* This function performs the same operation as isl_multi_aff_domain_map,
4089 * but is considered as a function on an isl_space when exported.
4091 __isl_give isl_multi_aff *isl_space_domain_map_multi_aff(
4092 __isl_take isl_space *space)
4094 return isl_multi_aff_domain_map(space);
4097 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4098 * of the space to its range.
4100 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4102 int i;
4103 isl_size n_in, n_out;
4104 isl_local_space *ls;
4105 isl_multi_aff *ma;
4107 if (!space)
4108 return NULL;
4109 if (!isl_space_is_map(space))
4110 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4111 "not a map space", goto error);
4113 n_in = isl_space_dim(space, isl_dim_in);
4114 n_out = isl_space_dim(space, isl_dim_out);
4115 if (n_in < 0 || n_out < 0)
4116 goto error;
4117 space = isl_space_range_map(space);
4119 ma = isl_multi_aff_alloc(isl_space_copy(space));
4120 if (n_out == 0) {
4121 isl_space_free(space);
4122 return ma;
4125 space = isl_space_domain(space);
4126 ls = isl_local_space_from_space(space);
4127 for (i = 0; i < n_out; ++i) {
4128 isl_aff *aff;
4130 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4131 isl_dim_set, n_in + i);
4132 ma = isl_multi_aff_set_aff(ma, i, aff);
4134 isl_local_space_free(ls);
4135 return ma;
4136 error:
4137 isl_space_free(space);
4138 return NULL;
4141 /* This function performs the same operation as isl_multi_aff_range_map,
4142 * but is considered as a function on an isl_space when exported.
4144 __isl_give isl_multi_aff *isl_space_range_map_multi_aff(
4145 __isl_take isl_space *space)
4147 return isl_multi_aff_range_map(space);
4150 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4151 * of the space to its domain.
4153 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4154 __isl_take isl_space *space)
4156 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4159 /* This function performs the same operation as isl_pw_multi_aff_domain_map,
4160 * but is considered as a function on an isl_space when exported.
4162 __isl_give isl_pw_multi_aff *isl_space_domain_map_pw_multi_aff(
4163 __isl_take isl_space *space)
4165 return isl_pw_multi_aff_domain_map(space);
4168 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4169 * of the space to its range.
4171 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4172 __isl_take isl_space *space)
4174 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4177 /* This function performs the same operation as isl_pw_multi_aff_range_map,
4178 * but is considered as a function on an isl_space when exported.
4180 __isl_give isl_pw_multi_aff *isl_space_range_map_pw_multi_aff(
4181 __isl_take isl_space *space)
4183 return isl_pw_multi_aff_range_map(space);
4186 /* Given the space of a set and a range of set dimensions,
4187 * construct an isl_multi_aff that projects out those dimensions.
4189 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4190 __isl_take isl_space *space, enum isl_dim_type type,
4191 unsigned first, unsigned n)
4193 int i;
4194 isl_size dim;
4195 isl_local_space *ls;
4196 isl_multi_aff *ma;
4198 if (!space)
4199 return NULL;
4200 if (!isl_space_is_set(space))
4201 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4202 "expecting set space", goto error);
4203 if (type != isl_dim_set)
4204 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4205 "only set dimensions can be projected out", goto error);
4206 if (isl_space_check_range(space, type, first, n) < 0)
4207 goto error;
4209 dim = isl_space_dim(space, isl_dim_set);
4210 if (dim < 0)
4211 goto error;
4213 space = isl_space_from_domain(space);
4214 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4216 if (dim == n)
4217 return isl_multi_aff_alloc(space);
4219 ma = isl_multi_aff_alloc(isl_space_copy(space));
4220 space = isl_space_domain(space);
4221 ls = isl_local_space_from_space(space);
4223 for (i = 0; i < first; ++i) {
4224 isl_aff *aff;
4226 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4227 isl_dim_set, i);
4228 ma = isl_multi_aff_set_aff(ma, i, aff);
4231 for (i = 0; i < dim - (first + n); ++i) {
4232 isl_aff *aff;
4234 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4235 isl_dim_set, first + n + i);
4236 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4239 isl_local_space_free(ls);
4240 return ma;
4241 error:
4242 isl_space_free(space);
4243 return NULL;
4246 /* Given the space of a set and a range of set dimensions,
4247 * construct an isl_pw_multi_aff that projects out those dimensions.
4249 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4250 __isl_take isl_space *space, enum isl_dim_type type,
4251 unsigned first, unsigned n)
4253 isl_multi_aff *ma;
4255 ma = isl_multi_aff_project_out_map(space, type, first, n);
4256 return isl_pw_multi_aff_from_multi_aff(ma);
4259 /* This function performs the same operation as isl_pw_multi_aff_from_multi_aff,
4260 * but is considered as a function on an isl_multi_aff when exported.
4262 __isl_give isl_pw_multi_aff *isl_multi_aff_to_pw_multi_aff(
4263 __isl_take isl_multi_aff *ma)
4265 return isl_pw_multi_aff_from_multi_aff(ma);
4268 /* Create a piecewise multi-affine expression in the given space that maps each
4269 * input dimension to the corresponding output dimension.
4271 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4272 __isl_take isl_space *space)
4274 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4277 /* Create a piecewise multi expression that maps elements in the given space
4278 * to themselves.
4280 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity_on_domain_space(
4281 __isl_take isl_space *space)
4283 isl_multi_aff *ma;
4285 ma = isl_multi_aff_identity_on_domain_space(space);
4286 return isl_pw_multi_aff_from_multi_aff(ma);
4289 /* This function performs the same operation as
4290 * isl_pw_multi_aff_identity_on_domain_space,
4291 * but is considered as a function on an isl_space when exported.
4293 __isl_give isl_pw_multi_aff *isl_space_identity_pw_multi_aff_on_domain(
4294 __isl_take isl_space *space)
4296 return isl_pw_multi_aff_identity_on_domain_space(space);
4299 /* Exploit the equalities in "eq" to simplify the affine expressions.
4301 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4302 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4304 isl_size n;
4305 int i;
4307 n = isl_multi_aff_size(maff);
4308 if (n < 0 || !eq)
4309 goto error;
4311 for (i = 0; i < n; ++i) {
4312 isl_aff *aff;
4314 aff = isl_multi_aff_take_at(maff, i);
4315 aff = isl_aff_substitute_equalities(aff,
4316 isl_basic_set_copy(eq));
4317 maff = isl_multi_aff_restore_at(maff, i, aff);
4320 isl_basic_set_free(eq);
4321 return maff;
4322 error:
4323 isl_basic_set_free(eq);
4324 isl_multi_aff_free(maff);
4325 return NULL;
4328 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4329 isl_int f)
4331 isl_size n;
4332 int i;
4334 n = isl_multi_aff_size(maff);
4335 if (n < 0)
4336 return isl_multi_aff_free(maff);
4338 for (i = 0; i < n; ++i) {
4339 isl_aff *aff;
4341 aff = isl_multi_aff_take_at(maff, i);
4342 aff = isl_aff_scale(aff, f);
4343 maff = isl_multi_aff_restore_at(maff, i, aff);
4346 return maff;
4349 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4350 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4352 maff1 = isl_multi_aff_add(maff1, maff2);
4353 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4354 return maff1;
4357 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4359 if (!maff)
4360 return isl_bool_error;
4362 return isl_bool_false;
4365 /* Return the set of domain elements where "ma1" is lexicographically
4366 * smaller than or equal to "ma2".
4368 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4369 __isl_take isl_multi_aff *ma2)
4371 return isl_multi_aff_lex_ge_set(ma2, ma1);
4374 /* Return the set of domain elements where "ma1" is lexicographically
4375 * smaller than "ma2".
4377 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4378 __isl_take isl_multi_aff *ma2)
4380 return isl_multi_aff_lex_gt_set(ma2, ma1);
4383 /* Return the set of domain elements where "ma1" is lexicographically
4384 * greater than to "ma2". If "equal" is set, then include the domain
4385 * elements where they are equal.
4386 * Do this for the case where there are no entries.
4387 * In this case, "ma1" cannot be greater than "ma2",
4388 * but it is (greater than or) equal to "ma2".
4390 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4391 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4393 isl_space *space;
4395 space = isl_multi_aff_get_domain_space(ma1);
4397 isl_multi_aff_free(ma1);
4398 isl_multi_aff_free(ma2);
4400 if (equal)
4401 return isl_set_universe(space);
4402 else
4403 return isl_set_empty(space);
4406 /* Return the set where entry "i" of "ma1" and "ma2"
4407 * satisfy the relation prescribed by "cmp".
4409 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4410 __isl_keep isl_multi_aff *ma2, int i,
4411 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4412 __isl_take isl_aff *aff2))
4414 isl_aff *aff1, *aff2;
4416 aff1 = isl_multi_aff_get_at(ma1, i);
4417 aff2 = isl_multi_aff_get_at(ma2, i);
4418 return cmp(aff1, aff2);
4421 /* Return the set of domain elements where "ma1" is lexicographically
4422 * greater than to "ma2". If "equal" is set, then include the domain
4423 * elements where they are equal.
4425 * In particular, for all but the final entry,
4426 * include the set of elements where this entry is strictly greater in "ma1"
4427 * and all previous entries are equal.
4428 * The final entry is also allowed to be equal in the two functions
4429 * if "equal" is set.
4431 * The case where there are no entries is handled separately.
4433 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4434 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4436 int i;
4437 isl_size n;
4438 isl_space *space;
4439 isl_set *res;
4440 isl_set *equal_set;
4441 isl_set *gte;
4443 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4444 goto error;
4445 n = isl_multi_aff_size(ma1);
4446 if (n < 0)
4447 goto error;
4448 if (n == 0)
4449 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4451 space = isl_multi_aff_get_domain_space(ma1);
4452 res = isl_set_empty(isl_space_copy(space));
4453 equal_set = isl_set_universe(space);
4455 for (i = 0; i + 1 < n; ++i) {
4456 isl_bool empty;
4457 isl_set *gt, *eq;
4459 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4460 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4461 res = isl_set_union(res, gt);
4462 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4463 equal_set = isl_set_intersect(equal_set, eq);
4465 empty = isl_set_is_empty(equal_set);
4466 if (empty >= 0 && empty)
4467 break;
4470 if (equal)
4471 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4472 else
4473 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4474 isl_multi_aff_free(ma1);
4475 isl_multi_aff_free(ma2);
4477 gte = isl_set_intersect(gte, equal_set);
4478 return isl_set_union(res, gte);
4479 error:
4480 isl_multi_aff_free(ma1);
4481 isl_multi_aff_free(ma2);
4482 return NULL;
4485 /* Return the set of domain elements where "ma1" is lexicographically
4486 * greater than or equal to "ma2".
4488 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4489 __isl_take isl_multi_aff *ma2)
4491 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4494 /* Return the set of domain elements where "ma1" is lexicographically
4495 * greater than "ma2".
4497 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4498 __isl_take isl_multi_aff *ma2)
4500 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4503 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4505 #undef PW
4506 #define PW isl_pw_multi_aff
4507 #undef BASE
4508 #define BASE multi_aff
4509 #undef EL_IS_ZERO
4510 #define EL_IS_ZERO is_empty
4511 #undef ZERO
4512 #define ZERO empty
4513 #undef IS_ZERO
4514 #define IS_ZERO is_empty
4515 #undef FIELD
4516 #define FIELD maff
4517 #undef DEFAULT_IS_ZERO
4518 #define DEFAULT_IS_ZERO 0
4520 #include <isl_pw_templ.c>
4521 #include <isl_pw_un_op_templ.c>
4522 #include <isl_pw_add_constant_multi_val_templ.c>
4523 #include <isl_pw_add_constant_val_templ.c>
4524 #include <isl_pw_bind_domain_templ.c>
4525 #include <isl_pw_insert_dims_templ.c>
4526 #include <isl_pw_insert_domain_templ.c>
4527 #include <isl_pw_locals_templ.c>
4528 #include <isl_pw_move_dims_templ.c>
4529 #include <isl_pw_neg_templ.c>
4530 #include <isl_pw_pullback_templ.c>
4531 #include <isl_pw_range_tuple_id_templ.c>
4532 #include <isl_pw_union_opt.c>
4534 #undef BASE
4535 #define BASE pw_multi_aff
4537 #include <isl_union_multi.c>
4538 #include "isl_union_locals_templ.c"
4539 #include <isl_union_neg.c>
4541 #undef BASE
4542 #define BASE multi_aff
4544 #include <isl_union_pw_templ.c>
4546 /* Generic function for extracting a factor from a product "pma".
4547 * "check_space" checks that the space is that of the right kind of product.
4548 * "space_factor" extracts the factor from the space.
4549 * "multi_aff_factor" extracts the factor from the constituent functions.
4551 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4552 __isl_take isl_pw_multi_aff *pma,
4553 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4554 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4555 __isl_give isl_multi_aff *(*multi_aff_factor)(
4556 __isl_take isl_multi_aff *ma))
4558 int i;
4559 isl_space *space;
4561 if (check_space(pma) < 0)
4562 return isl_pw_multi_aff_free(pma);
4564 space = isl_pw_multi_aff_take_space(pma);
4565 space = space_factor(space);
4567 for (i = 0; pma && i < pma->n; ++i) {
4568 isl_multi_aff *ma;
4570 ma = isl_pw_multi_aff_take_base_at(pma, i);
4571 ma = multi_aff_factor(ma);
4572 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4575 pma = isl_pw_multi_aff_restore_space(pma, space);
4577 return pma;
4580 /* Is the range of "pma" a wrapped relation?
4582 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4583 __isl_keep isl_pw_multi_aff *pma)
4585 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4588 /* Check that the range of "pma" is a product.
4590 static isl_stat pw_multi_aff_check_range_product(
4591 __isl_keep isl_pw_multi_aff *pma)
4593 isl_bool wraps;
4595 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4596 if (wraps < 0)
4597 return isl_stat_error;
4598 if (!wraps)
4599 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4600 "range is not a product", return isl_stat_error);
4601 return isl_stat_ok;
4604 /* Given a function A -> [B -> C], extract the function A -> B.
4606 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4607 __isl_take isl_pw_multi_aff *pma)
4609 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4610 &isl_space_range_factor_domain,
4611 &isl_multi_aff_range_factor_domain);
4614 /* Given a function A -> [B -> C], extract the function A -> C.
4616 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4617 __isl_take isl_pw_multi_aff *pma)
4619 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4620 &isl_space_range_factor_range,
4621 &isl_multi_aff_range_factor_range);
4624 /* Given two piecewise multi affine expressions, return a piecewise
4625 * multi-affine expression defined on the union of the definition domains
4626 * of the inputs that is equal to the lexicographic maximum of the two
4627 * inputs on each cell. If only one of the two inputs is defined on
4628 * a given cell, then it is considered to be the maximum.
4630 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4631 __isl_take isl_pw_multi_aff *pma1,
4632 __isl_take isl_pw_multi_aff *pma2)
4634 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4635 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4636 &isl_multi_aff_lex_ge_set);
4639 /* Given two piecewise multi affine expressions, return a piecewise
4640 * multi-affine expression defined on the union of the definition domains
4641 * of the inputs that is equal to the lexicographic minimum of the two
4642 * inputs on each cell. If only one of the two inputs is defined on
4643 * a given cell, then it is considered to be the minimum.
4645 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4646 __isl_take isl_pw_multi_aff *pma1,
4647 __isl_take isl_pw_multi_aff *pma2)
4649 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4650 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4651 &isl_multi_aff_lex_le_set);
4654 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4655 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4657 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4658 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4659 &isl_multi_aff_add);
4662 /* Subtract "pma2" from "pma1" and return the result.
4664 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4665 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4667 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4668 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4669 &isl_multi_aff_sub);
4672 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4673 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4675 return isl_pw_multi_aff_union_add_(pma1, pma2);
4678 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4679 * with the actual sum on the shared domain and
4680 * the defined expression on the symmetric difference of the domains.
4682 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4683 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4685 return isl_union_pw_aff_union_add_(upa1, upa2);
4688 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4689 * with the actual sum on the shared domain and
4690 * the defined expression on the symmetric difference of the domains.
4692 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4693 __isl_take isl_union_pw_multi_aff *upma1,
4694 __isl_take isl_union_pw_multi_aff *upma2)
4696 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4699 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4700 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4702 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4703 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4705 int i, j, n;
4706 isl_space *space;
4707 isl_pw_multi_aff *res;
4709 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4710 goto error;
4712 n = pma1->n * pma2->n;
4713 space = isl_space_product(isl_space_copy(pma1->dim),
4714 isl_space_copy(pma2->dim));
4715 res = isl_pw_multi_aff_alloc_size(space, n);
4717 for (i = 0; i < pma1->n; ++i) {
4718 for (j = 0; j < pma2->n; ++j) {
4719 isl_set *domain;
4720 isl_multi_aff *ma;
4722 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4723 isl_set_copy(pma2->p[j].set));
4724 ma = isl_multi_aff_product(
4725 isl_multi_aff_copy(pma1->p[i].maff),
4726 isl_multi_aff_copy(pma2->p[j].maff));
4727 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4731 isl_pw_multi_aff_free(pma1);
4732 isl_pw_multi_aff_free(pma2);
4733 return res;
4734 error:
4735 isl_pw_multi_aff_free(pma1);
4736 isl_pw_multi_aff_free(pma2);
4737 return NULL;
4740 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4741 * denominator "denom".
4742 * "denom" is allowed to be negative, in which case the actual denominator
4743 * is -denom and the expressions are added instead.
4745 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4746 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4748 int i, first;
4749 int sign;
4750 isl_int d;
4752 first = isl_seq_first_non_zero(c, n);
4753 if (first == -1)
4754 return aff;
4756 sign = isl_int_sgn(denom);
4757 isl_int_init(d);
4758 isl_int_abs(d, denom);
4759 for (i = first; i < n; ++i) {
4760 isl_aff *aff_i;
4762 if (isl_int_is_zero(c[i]))
4763 continue;
4764 aff_i = isl_multi_aff_get_aff(ma, i);
4765 aff_i = isl_aff_scale(aff_i, c[i]);
4766 aff_i = isl_aff_scale_down(aff_i, d);
4767 if (sign >= 0)
4768 aff = isl_aff_sub(aff, aff_i);
4769 else
4770 aff = isl_aff_add(aff, aff_i);
4772 isl_int_clear(d);
4774 return aff;
4777 /* Extract an affine expression that expresses the output dimension "pos"
4778 * of "bmap" in terms of the parameters and input dimensions from
4779 * equality "eq".
4780 * Note that this expression may involve integer divisions defined
4781 * in terms of parameters and input dimensions.
4782 * The equality may also involve references to earlier (but not later)
4783 * output dimensions. These are replaced by the corresponding elements
4784 * in "ma".
4786 * If the equality is of the form
4788 * f(i) + h(j) + a x + g(i) = 0,
4790 * with f(i) a linear combinations of the parameters and input dimensions,
4791 * g(i) a linear combination of integer divisions defined in terms of the same
4792 * and h(j) a linear combinations of earlier output dimensions,
4793 * then the affine expression is
4795 * (-f(i) - g(i))/a - h(j)/a
4797 * If the equality is of the form
4799 * f(i) + h(j) - a x + g(i) = 0,
4801 * then the affine expression is
4803 * (f(i) + g(i))/a - h(j)/(-a)
4806 * If "div" refers to an integer division (i.e., it is smaller than
4807 * the number of integer divisions), then the equality constraint
4808 * does involve an integer division (the one at position "div") that
4809 * is defined in terms of output dimensions. However, this integer
4810 * division can be eliminated by exploiting a pair of constraints
4811 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4812 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4813 * -l + x >= 0.
4814 * In particular, let
4816 * x = e(i) + m floor(...)
4818 * with e(i) the expression derived above and floor(...) the integer
4819 * division involving output dimensions.
4820 * From
4822 * l <= x <= l + n,
4824 * we have
4826 * 0 <= x - l <= n
4828 * This means
4830 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4831 * = (e(i) - l) mod m
4833 * Therefore,
4835 * x - l = (e(i) - l) mod m
4837 * or
4839 * x = ((e(i) - l) mod m) + l
4841 * The variable "shift" below contains the expression -l, which may
4842 * also involve a linear combination of earlier output dimensions.
4844 static __isl_give isl_aff *extract_aff_from_equality(
4845 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4846 __isl_keep isl_multi_aff *ma)
4848 unsigned o_out;
4849 isl_size n_div, n_out;
4850 isl_ctx *ctx;
4851 isl_local_space *ls;
4852 isl_aff *aff, *shift;
4853 isl_val *mod;
4855 ctx = isl_basic_map_get_ctx(bmap);
4856 ls = isl_basic_map_get_local_space(bmap);
4857 ls = isl_local_space_domain(ls);
4858 aff = isl_aff_alloc(isl_local_space_copy(ls));
4859 if (!aff)
4860 goto error;
4861 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4862 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4863 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4864 if (n_out < 0 || n_div < 0)
4865 goto error;
4866 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4867 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4868 isl_seq_cpy(aff->v->el + 1 + o_out,
4869 bmap->eq[eq] + o_out + n_out, n_div);
4870 } else {
4871 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4872 isl_seq_neg(aff->v->el + 1 + o_out,
4873 bmap->eq[eq] + o_out + n_out, n_div);
4875 if (div < n_div)
4876 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4877 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4878 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4879 bmap->eq[eq][o_out + pos]);
4880 if (div < n_div) {
4881 shift = isl_aff_alloc(isl_local_space_copy(ls));
4882 if (!shift)
4883 goto error;
4884 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4885 isl_seq_cpy(shift->v->el + 1 + o_out,
4886 bmap->ineq[ineq] + o_out + n_out, n_div);
4887 isl_int_set_si(shift->v->el[0], 1);
4888 shift = subtract_initial(shift, ma, pos,
4889 bmap->ineq[ineq] + o_out, ctx->negone);
4890 aff = isl_aff_add(aff, isl_aff_copy(shift));
4891 mod = isl_val_int_from_isl_int(ctx,
4892 bmap->eq[eq][o_out + n_out + div]);
4893 mod = isl_val_abs(mod);
4894 aff = isl_aff_mod_val(aff, mod);
4895 aff = isl_aff_sub(aff, shift);
4898 isl_local_space_free(ls);
4899 return aff;
4900 error:
4901 isl_local_space_free(ls);
4902 isl_aff_free(aff);
4903 return NULL;
4906 /* Given a basic map with output dimensions defined
4907 * in terms of the parameters input dimensions and earlier
4908 * output dimensions using an equality (and possibly a pair on inequalities),
4909 * extract an isl_aff that expresses output dimension "pos" in terms
4910 * of the parameters and input dimensions.
4911 * Note that this expression may involve integer divisions defined
4912 * in terms of parameters and input dimensions.
4913 * "ma" contains the expressions corresponding to earlier output dimensions.
4915 * This function shares some similarities with
4916 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4918 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4919 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4921 int eq, div, ineq;
4922 isl_aff *aff;
4924 if (!bmap)
4925 return NULL;
4926 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4927 if (eq >= bmap->n_eq)
4928 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4929 "unable to find suitable equality", return NULL);
4930 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4932 aff = isl_aff_remove_unused_divs(aff);
4933 return aff;
4936 /* Given a basic map where each output dimension is defined
4937 * in terms of the parameters and input dimensions using an equality,
4938 * extract an isl_multi_aff that expresses the output dimensions in terms
4939 * of the parameters and input dimensions.
4941 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4942 __isl_take isl_basic_map *bmap)
4944 int i;
4945 isl_size n_out;
4946 isl_multi_aff *ma;
4948 if (!bmap)
4949 return NULL;
4951 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4952 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4953 if (n_out < 0)
4954 ma = isl_multi_aff_free(ma);
4956 for (i = 0; i < n_out; ++i) {
4957 isl_aff *aff;
4959 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4960 ma = isl_multi_aff_set_aff(ma, i, aff);
4963 isl_basic_map_free(bmap);
4965 return ma;
4968 /* Given a basic set where each set dimension is defined
4969 * in terms of the parameters using an equality,
4970 * extract an isl_multi_aff that expresses the set dimensions in terms
4971 * of the parameters.
4973 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4974 __isl_take isl_basic_set *bset)
4976 return extract_isl_multi_aff_from_basic_map(bset);
4979 /* Create an isl_pw_multi_aff that is equivalent to
4980 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4981 * The given basic map is such that each output dimension is defined
4982 * in terms of the parameters and input dimensions using an equality.
4984 * Since some applications expect the result of isl_pw_multi_aff_from_map
4985 * to only contain integer affine expressions, we compute the floor
4986 * of the expression before returning.
4988 * Remove all constraints involving local variables without
4989 * an explicit representation (resulting in the removal of those
4990 * local variables) prior to the actual extraction to ensure
4991 * that the local spaces in which the resulting affine expressions
4992 * are created do not contain any unknown local variables.
4993 * Removing such constraints is safe because constraints involving
4994 * unknown local variables are not used to determine whether
4995 * a basic map is obviously single-valued.
4997 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4998 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
5000 isl_multi_aff *ma;
5002 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
5003 ma = extract_isl_multi_aff_from_basic_map(bmap);
5004 ma = isl_multi_aff_floor(ma);
5005 return isl_pw_multi_aff_alloc(domain, ma);
5008 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5009 * This obviously only works if the input "map" is single-valued.
5010 * If so, we compute the lexicographic minimum of the image in the form
5011 * of an isl_pw_multi_aff. Since the image is unique, it is equal
5012 * to its lexicographic minimum.
5013 * If the input is not single-valued, we produce an error.
5015 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
5016 __isl_take isl_map *map)
5018 int i;
5019 int sv;
5020 isl_pw_multi_aff *pma;
5022 sv = isl_map_is_single_valued(map);
5023 if (sv < 0)
5024 goto error;
5025 if (!sv)
5026 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5027 "map is not single-valued", goto error);
5028 map = isl_map_make_disjoint(map);
5029 if (!map)
5030 return NULL;
5032 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
5034 for (i = 0; i < map->n; ++i) {
5035 isl_pw_multi_aff *pma_i;
5036 isl_basic_map *bmap;
5037 bmap = isl_basic_map_copy(map->p[i]);
5038 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
5039 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
5042 isl_map_free(map);
5043 return pma;
5044 error:
5045 isl_map_free(map);
5046 return NULL;
5049 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5050 * taking into account that the output dimension at position "d"
5051 * can be represented as
5053 * x = floor((e(...) + c1) / m)
5055 * given that constraint "i" is of the form
5057 * e(...) + c1 - m x >= 0
5060 * Let "map" be of the form
5062 * A -> B
5064 * We construct a mapping
5066 * A -> [A -> x = floor(...)]
5068 * apply that to the map, obtaining
5070 * [A -> x = floor(...)] -> B
5072 * and equate dimension "d" to x.
5073 * We then compute a isl_pw_multi_aff representation of the resulting map
5074 * and plug in the mapping above.
5076 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
5077 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
5079 isl_ctx *ctx;
5080 isl_space *space = NULL;
5081 isl_local_space *ls;
5082 isl_multi_aff *ma;
5083 isl_aff *aff;
5084 isl_vec *v;
5085 isl_map *insert;
5086 int offset;
5087 isl_size n;
5088 isl_size n_in;
5089 isl_pw_multi_aff *pma;
5090 isl_bool is_set;
5092 is_set = isl_map_is_set(map);
5093 if (is_set < 0)
5094 goto error;
5096 offset = isl_basic_map_offset(hull, isl_dim_out);
5097 ctx = isl_map_get_ctx(map);
5098 space = isl_space_domain(isl_map_get_space(map));
5099 n_in = isl_space_dim(space, isl_dim_set);
5100 n = isl_space_dim(space, isl_dim_all);
5101 if (n_in < 0 || n < 0)
5102 goto error;
5104 v = isl_vec_alloc(ctx, 1 + 1 + n);
5105 if (v) {
5106 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
5107 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
5109 isl_basic_map_free(hull);
5111 ls = isl_local_space_from_space(isl_space_copy(space));
5112 aff = isl_aff_alloc_vec_validated(ls, v);
5113 aff = isl_aff_floor(aff);
5114 if (is_set) {
5115 isl_space_free(space);
5116 ma = isl_multi_aff_from_aff(aff);
5117 } else {
5118 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
5119 ma = isl_multi_aff_range_product(ma,
5120 isl_multi_aff_from_aff(aff));
5123 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
5124 map = isl_map_apply_domain(map, insert);
5125 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
5126 pma = isl_pw_multi_aff_from_map(map);
5127 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
5129 return pma;
5130 error:
5131 isl_space_free(space);
5132 isl_map_free(map);
5133 isl_basic_map_free(hull);
5134 return NULL;
5137 /* Is constraint "c" of the form
5139 * e(...) + c1 - m x >= 0
5141 * or
5143 * -e(...) + c2 + m x >= 0
5145 * where m > 1 and e only depends on parameters and input dimensions?
5147 * "offset" is the offset of the output dimensions
5148 * "pos" is the position of output dimension x.
5150 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
5152 if (isl_int_is_zero(c[offset + d]))
5153 return 0;
5154 if (isl_int_is_one(c[offset + d]))
5155 return 0;
5156 if (isl_int_is_negone(c[offset + d]))
5157 return 0;
5158 if (isl_seq_first_non_zero(c + offset, d) != -1)
5159 return 0;
5160 if (isl_seq_first_non_zero(c + offset + d + 1,
5161 total - (offset + d + 1)) != -1)
5162 return 0;
5163 return 1;
5166 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5168 * As a special case, we first check if there is any pair of constraints,
5169 * shared by all the basic maps in "map" that force a given dimension
5170 * to be equal to the floor of some affine combination of the input dimensions.
5172 * In particular, if we can find two constraints
5174 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5176 * and
5178 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5180 * where m > 1 and e only depends on parameters and input dimensions,
5181 * and such that
5183 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5185 * then we know that we can take
5187 * x = floor((e(...) + c1) / m)
5189 * without having to perform any computation.
5191 * Note that we know that
5193 * c1 + c2 >= 1
5195 * If c1 + c2 were 0, then we would have detected an equality during
5196 * simplification. If c1 + c2 were negative, then we would have detected
5197 * a contradiction.
5199 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5200 __isl_take isl_map *map)
5202 int d;
5203 isl_size dim;
5204 int i, j, n;
5205 int offset;
5206 isl_size total;
5207 isl_int sum;
5208 isl_basic_map *hull;
5210 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5211 dim = isl_map_dim(map, isl_dim_out);
5212 total = isl_basic_map_dim(hull, isl_dim_all);
5213 if (dim < 0 || total < 0)
5214 goto error;
5216 isl_int_init(sum);
5217 offset = isl_basic_map_offset(hull, isl_dim_out);
5218 n = hull->n_ineq;
5219 for (d = 0; d < dim; ++d) {
5220 for (i = 0; i < n; ++i) {
5221 if (!is_potential_div_constraint(hull->ineq[i],
5222 offset, d, 1 + total))
5223 continue;
5224 for (j = i + 1; j < n; ++j) {
5225 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5226 hull->ineq[j] + 1, total))
5227 continue;
5228 isl_int_add(sum, hull->ineq[i][0],
5229 hull->ineq[j][0]);
5230 if (isl_int_abs_lt(sum,
5231 hull->ineq[i][offset + d]))
5232 break;
5235 if (j >= n)
5236 continue;
5237 isl_int_clear(sum);
5238 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5239 j = i;
5240 return pw_multi_aff_from_map_div(map, hull, d, j);
5243 isl_int_clear(sum);
5244 isl_basic_map_free(hull);
5245 return pw_multi_aff_from_map_base(map);
5246 error:
5247 isl_map_free(map);
5248 isl_basic_map_free(hull);
5249 return NULL;
5252 /* Given an affine expression
5254 * [A -> B] -> f(A,B)
5256 * construct an isl_multi_aff
5258 * [A -> B] -> B'
5260 * such that dimension "d" in B' is set to "aff" and the remaining
5261 * dimensions are set equal to the corresponding dimensions in B.
5262 * "n_in" is the dimension of the space A.
5263 * "n_out" is the dimension of the space B.
5265 * If "is_set" is set, then the affine expression is of the form
5267 * [B] -> f(B)
5269 * and we construct an isl_multi_aff
5271 * B -> B'
5273 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5274 unsigned n_in, unsigned n_out, int is_set)
5276 int i;
5277 isl_multi_aff *ma;
5278 isl_space *space, *space2;
5279 isl_local_space *ls;
5281 space = isl_aff_get_domain_space(aff);
5282 ls = isl_local_space_from_space(isl_space_copy(space));
5283 space2 = isl_space_copy(space);
5284 if (!is_set)
5285 space2 = isl_space_range(isl_space_unwrap(space2));
5286 space = isl_space_map_from_domain_and_range(space, space2);
5287 ma = isl_multi_aff_alloc(space);
5288 ma = isl_multi_aff_set_aff(ma, d, aff);
5290 for (i = 0; i < n_out; ++i) {
5291 if (i == d)
5292 continue;
5293 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5294 isl_dim_set, n_in + i);
5295 ma = isl_multi_aff_set_aff(ma, i, aff);
5298 isl_local_space_free(ls);
5300 return ma;
5303 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5304 * taking into account that the dimension at position "d" can be written as
5306 * x = m a + f(..) (1)
5308 * where m is equal to "gcd".
5309 * "i" is the index of the equality in "hull" that defines f(..).
5310 * In particular, the equality is of the form
5312 * f(..) - x + m g(existentials) = 0
5314 * or
5316 * -f(..) + x + m g(existentials) = 0
5318 * We basically plug (1) into "map", resulting in a map with "a"
5319 * in the range instead of "x". The corresponding isl_pw_multi_aff
5320 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5322 * Specifically, given the input map
5324 * A -> B
5326 * We first wrap it into a set
5328 * [A -> B]
5330 * and define (1) on top of the corresponding space, resulting in "aff".
5331 * We use this to create an isl_multi_aff that maps the output position "d"
5332 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5333 * We plug this into the wrapped map, unwrap the result and compute the
5334 * corresponding isl_pw_multi_aff.
5335 * The result is an expression
5337 * A -> T(A)
5339 * We adjust that to
5341 * A -> [A -> T(A)]
5343 * so that we can plug that into "aff", after extending the latter to
5344 * a mapping
5346 * [A -> B] -> B'
5349 * If "map" is actually a set, then there is no "A" space, meaning
5350 * that we do not need to perform any wrapping, and that the result
5351 * of the recursive call is of the form
5353 * [T]
5355 * which is plugged into a mapping of the form
5357 * B -> B'
5359 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5360 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5361 isl_int gcd)
5363 isl_set *set;
5364 isl_space *space;
5365 isl_local_space *ls;
5366 isl_aff *aff;
5367 isl_multi_aff *ma;
5368 isl_pw_multi_aff *pma, *id;
5369 isl_size n_in;
5370 unsigned o_out;
5371 isl_size n_out;
5372 isl_bool is_set;
5374 is_set = isl_map_is_set(map);
5375 if (is_set < 0)
5376 goto error;
5378 n_in = isl_basic_map_dim(hull, isl_dim_in);
5379 n_out = isl_basic_map_dim(hull, isl_dim_out);
5380 if (n_in < 0 || n_out < 0)
5381 goto error;
5382 o_out = isl_basic_map_offset(hull, isl_dim_out);
5384 if (is_set)
5385 set = map;
5386 else
5387 set = isl_map_wrap(map);
5388 space = isl_space_map_from_set(isl_set_get_space(set));
5389 ma = isl_multi_aff_identity(space);
5390 ls = isl_local_space_from_space(isl_set_get_space(set));
5391 aff = isl_aff_alloc(ls);
5392 if (aff) {
5393 isl_int_set_si(aff->v->el[0], 1);
5394 if (isl_int_is_one(hull->eq[i][o_out + d]))
5395 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5396 aff->v->size - 1);
5397 else
5398 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5399 aff->v->size - 1);
5400 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5402 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5403 set = isl_set_preimage_multi_aff(set, ma);
5405 ma = range_map(aff, d, n_in, n_out, is_set);
5407 if (is_set)
5408 map = set;
5409 else
5410 map = isl_set_unwrap(set);
5411 pma = isl_pw_multi_aff_from_map(map);
5413 if (!is_set) {
5414 space = isl_pw_multi_aff_get_domain_space(pma);
5415 space = isl_space_map_from_set(space);
5416 id = isl_pw_multi_aff_identity(space);
5417 pma = isl_pw_multi_aff_range_product(id, pma);
5419 id = isl_pw_multi_aff_from_multi_aff(ma);
5420 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5422 isl_basic_map_free(hull);
5423 return pma;
5424 error:
5425 isl_map_free(map);
5426 isl_basic_map_free(hull);
5427 return NULL;
5430 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5431 * "hull" contains the equalities valid for "map".
5433 * Check if any of the output dimensions is "strided".
5434 * That is, we check if it can be written as
5436 * x = m a + f(..)
5438 * with m greater than 1, a some combination of existentially quantified
5439 * variables and f an expression in the parameters and input dimensions.
5440 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5442 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5443 * special case.
5445 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5446 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5448 int i, j;
5449 isl_size n_out;
5450 unsigned o_out;
5451 isl_size n_div;
5452 unsigned o_div;
5453 isl_int gcd;
5455 n_div = isl_basic_map_dim(hull, isl_dim_div);
5456 n_out = isl_basic_map_dim(hull, isl_dim_out);
5457 if (n_div < 0 || n_out < 0)
5458 goto error;
5460 if (n_div == 0) {
5461 isl_basic_map_free(hull);
5462 return pw_multi_aff_from_map_check_div(map);
5465 isl_int_init(gcd);
5467 o_div = isl_basic_map_offset(hull, isl_dim_div);
5468 o_out = isl_basic_map_offset(hull, isl_dim_out);
5470 for (i = 0; i < n_out; ++i) {
5471 for (j = 0; j < hull->n_eq; ++j) {
5472 isl_int *eq = hull->eq[j];
5473 isl_pw_multi_aff *res;
5475 if (!isl_int_is_one(eq[o_out + i]) &&
5476 !isl_int_is_negone(eq[o_out + i]))
5477 continue;
5478 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5479 continue;
5480 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5481 n_out - (i + 1)) != -1)
5482 continue;
5483 isl_seq_gcd(eq + o_div, n_div, &gcd);
5484 if (isl_int_is_zero(gcd))
5485 continue;
5486 if (isl_int_is_one(gcd))
5487 continue;
5489 res = pw_multi_aff_from_map_stride(map, hull,
5490 i, j, gcd);
5491 isl_int_clear(gcd);
5492 return res;
5496 isl_int_clear(gcd);
5497 isl_basic_map_free(hull);
5498 return pw_multi_aff_from_map_check_div(map);
5499 error:
5500 isl_map_free(map);
5501 isl_basic_map_free(hull);
5502 return NULL;
5505 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5507 * As a special case, we first check if all output dimensions are uniquely
5508 * defined in terms of the parameters and input dimensions over the entire
5509 * domain. If so, we extract the desired isl_pw_multi_aff directly
5510 * from the affine hull of "map" and its domain.
5512 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5513 * special cases.
5515 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5517 isl_bool sv;
5518 isl_size n;
5519 isl_basic_map *hull;
5521 n = isl_map_n_basic_map(map);
5522 if (n < 0)
5523 goto error;
5525 if (n == 1) {
5526 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5527 hull = isl_basic_map_plain_affine_hull(hull);
5528 sv = isl_basic_map_plain_is_single_valued(hull);
5529 if (sv >= 0 && sv)
5530 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5531 hull);
5532 isl_basic_map_free(hull);
5534 map = isl_map_detect_equalities(map);
5535 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5536 sv = isl_basic_map_plain_is_single_valued(hull);
5537 if (sv >= 0 && sv)
5538 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5539 if (sv >= 0)
5540 return pw_multi_aff_from_map_check_strides(map, hull);
5541 isl_basic_map_free(hull);
5542 error:
5543 isl_map_free(map);
5544 return NULL;
5547 /* This function performs the same operation as isl_pw_multi_aff_from_map,
5548 * but is considered as a function on an isl_map when exported.
5550 __isl_give isl_pw_multi_aff *isl_map_as_pw_multi_aff(__isl_take isl_map *map)
5552 return isl_pw_multi_aff_from_map(map);
5555 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5557 return isl_pw_multi_aff_from_map(set);
5560 /* This function performs the same operation as isl_pw_multi_aff_from_set,
5561 * but is considered as a function on an isl_set when exported.
5563 __isl_give isl_pw_multi_aff *isl_set_as_pw_multi_aff(__isl_take isl_set *set)
5565 return isl_pw_multi_aff_from_set(set);
5568 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5569 * add it to *user.
5571 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5573 isl_union_pw_multi_aff **upma = user;
5574 isl_pw_multi_aff *pma;
5576 pma = isl_pw_multi_aff_from_map(map);
5577 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5579 return *upma ? isl_stat_ok : isl_stat_error;
5582 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5583 * domain.
5585 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5586 __isl_take isl_aff *aff)
5588 isl_multi_aff *ma;
5589 isl_pw_multi_aff *pma;
5591 ma = isl_multi_aff_from_aff(aff);
5592 pma = isl_pw_multi_aff_from_multi_aff(ma);
5593 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5596 /* Try and create an isl_union_pw_multi_aff that is equivalent
5597 * to the given isl_union_map.
5598 * The isl_union_map is required to be single-valued in each space.
5599 * Otherwise, an error is produced.
5601 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5602 __isl_take isl_union_map *umap)
5604 isl_space *space;
5605 isl_union_pw_multi_aff *upma;
5607 space = isl_union_map_get_space(umap);
5608 upma = isl_union_pw_multi_aff_empty(space);
5609 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5610 upma = isl_union_pw_multi_aff_free(upma);
5611 isl_union_map_free(umap);
5613 return upma;
5616 /* This function performs the same operation as
5617 * isl_union_pw_multi_aff_from_union_map,
5618 * but is considered as a function on an isl_union_map when exported.
5620 __isl_give isl_union_pw_multi_aff *isl_union_map_as_union_pw_multi_aff(
5621 __isl_take isl_union_map *umap)
5623 return isl_union_pw_multi_aff_from_union_map(umap);
5626 /* Try and create an isl_union_pw_multi_aff that is equivalent
5627 * to the given isl_union_set.
5628 * The isl_union_set is required to be a singleton in each space.
5629 * Otherwise, an error is produced.
5631 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5632 __isl_take isl_union_set *uset)
5634 return isl_union_pw_multi_aff_from_union_map(uset);
5637 /* Return the piecewise affine expression "set ? 1 : 0".
5639 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5641 isl_pw_aff *pa;
5642 isl_space *space = isl_set_get_space(set);
5643 isl_local_space *ls = isl_local_space_from_space(space);
5644 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5645 isl_aff *one = isl_aff_zero_on_domain(ls);
5647 one = isl_aff_add_constant_si(one, 1);
5648 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5649 set = isl_set_complement(set);
5650 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5652 return pa;
5655 /* Plug in "subs" for dimension "type", "pos" of "aff".
5657 * Let i be the dimension to replace and let "subs" be of the form
5659 * f/d
5661 * and "aff" of the form
5663 * (a i + g)/m
5665 * The result is
5667 * (a f + d g')/(m d)
5669 * where g' is the result of plugging in "subs" in each of the integer
5670 * divisions in g.
5672 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5673 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5675 isl_ctx *ctx;
5676 isl_int v;
5677 isl_size n_div;
5679 aff = isl_aff_cow(aff);
5680 if (!aff || !subs)
5681 return isl_aff_free(aff);
5683 ctx = isl_aff_get_ctx(aff);
5684 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5685 isl_die(ctx, isl_error_invalid,
5686 "spaces don't match", return isl_aff_free(aff));
5687 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5688 if (n_div < 0)
5689 return isl_aff_free(aff);
5690 if (n_div != 0)
5691 isl_die(ctx, isl_error_unsupported,
5692 "cannot handle divs yet", return isl_aff_free(aff));
5694 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5695 if (!aff->ls)
5696 return isl_aff_free(aff);
5698 aff->v = isl_vec_cow(aff->v);
5699 if (!aff->v)
5700 return isl_aff_free(aff);
5702 pos += isl_local_space_offset(aff->ls, type);
5704 isl_int_init(v);
5705 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5706 aff->v->size, subs->v->size, v);
5707 isl_int_clear(v);
5709 return aff;
5712 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5713 * expressions in "maff".
5715 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5716 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5717 __isl_keep isl_aff *subs)
5719 isl_size n;
5720 int i;
5722 n = isl_multi_aff_size(maff);
5723 if (n < 0 || !subs)
5724 return isl_multi_aff_free(maff);
5726 if (type == isl_dim_in)
5727 type = isl_dim_set;
5729 for (i = 0; i < n; ++i) {
5730 isl_aff *aff;
5732 aff = isl_multi_aff_take_at(maff, i);
5733 aff = isl_aff_substitute(aff, type, pos, subs);
5734 maff = isl_multi_aff_restore_at(maff, i, aff);
5737 return maff;
5740 /* Plug in "subs" for input dimension "pos" of "pma".
5742 * pma is of the form
5744 * A_i(v) -> M_i(v)
5746 * while subs is of the form
5748 * v' = B_j(v) -> S_j
5750 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5751 * has a contribution in the result, in particular
5753 * C_ij(S_j) -> M_i(S_j)
5755 * Note that plugging in S_j in C_ij may also result in an empty set
5756 * and this contribution should simply be discarded.
5758 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5759 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5760 __isl_keep isl_pw_aff *subs)
5762 int i, j, n;
5763 isl_pw_multi_aff *res;
5765 if (!pma || !subs)
5766 return isl_pw_multi_aff_free(pma);
5768 n = pma->n * subs->n;
5769 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5771 for (i = 0; i < pma->n; ++i) {
5772 for (j = 0; j < subs->n; ++j) {
5773 isl_set *common;
5774 isl_multi_aff *res_ij;
5775 int empty;
5777 common = isl_set_intersect(
5778 isl_set_copy(pma->p[i].set),
5779 isl_set_copy(subs->p[j].set));
5780 common = isl_set_substitute(common,
5781 pos, subs->p[j].aff);
5782 empty = isl_set_plain_is_empty(common);
5783 if (empty < 0 || empty) {
5784 isl_set_free(common);
5785 if (empty < 0)
5786 goto error;
5787 continue;
5790 res_ij = isl_multi_aff_substitute(
5791 isl_multi_aff_copy(pma->p[i].maff),
5792 isl_dim_in, pos, subs->p[j].aff);
5794 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5798 isl_pw_multi_aff_free(pma);
5799 return res;
5800 error:
5801 isl_pw_multi_aff_free(pma);
5802 isl_pw_multi_aff_free(res);
5803 return NULL;
5806 /* Compute the preimage of a range of dimensions in the affine expression "src"
5807 * under "ma" and put the result in "dst". The number of dimensions in "src"
5808 * that precede the range is given by "n_before". The number of dimensions
5809 * in the range is given by the number of output dimensions of "ma".
5810 * The number of dimensions that follow the range is given by "n_after".
5811 * If "has_denom" is set (to one),
5812 * then "src" and "dst" have an extra initial denominator.
5813 * "n_div_ma" is the number of existentials in "ma"
5814 * "n_div_bset" is the number of existentials in "src"
5815 * The resulting "dst" (which is assumed to have been allocated by
5816 * the caller) contains coefficients for both sets of existentials,
5817 * first those in "ma" and then those in "src".
5818 * f, c1, c2 and g are temporary objects that have been initialized
5819 * by the caller.
5821 * Let src represent the expression
5823 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5825 * and let ma represent the expressions
5827 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5829 * We start out with the following expression for dst:
5831 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5833 * with the multiplication factor f initially equal to 1
5834 * and f \sum_i b_i v_i kept separately.
5835 * For each x_i that we substitute, we multiply the numerator
5836 * (and denominator) of dst by c_1 = m_i and add the numerator
5837 * of the x_i expression multiplied by c_2 = f b_i,
5838 * after removing the common factors of c_1 and c_2.
5839 * The multiplication factor f also needs to be multiplied by c_1
5840 * for the next x_j, j > i.
5842 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5843 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5844 int n_div_ma, int n_div_bmap,
5845 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5847 int i;
5848 isl_size n_param, n_in, n_out;
5849 int o_dst, o_src;
5851 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5852 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5853 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5854 if (n_param < 0 || n_in < 0 || n_out < 0)
5855 return isl_stat_error;
5857 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5858 o_dst = o_src = has_denom + 1 + n_param + n_before;
5859 isl_seq_clr(dst + o_dst, n_in);
5860 o_dst += n_in;
5861 o_src += n_out;
5862 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5863 o_dst += n_after;
5864 o_src += n_after;
5865 isl_seq_clr(dst + o_dst, n_div_ma);
5866 o_dst += n_div_ma;
5867 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5869 isl_int_set_si(f, 1);
5871 for (i = 0; i < n_out; ++i) {
5872 int offset = has_denom + 1 + n_param + n_before + i;
5874 if (isl_int_is_zero(src[offset]))
5875 continue;
5876 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5877 isl_int_mul(c2, f, src[offset]);
5878 isl_int_gcd(g, c1, c2);
5879 isl_int_divexact(c1, c1, g);
5880 isl_int_divexact(c2, c2, g);
5882 isl_int_mul(f, f, c1);
5883 o_dst = has_denom;
5884 o_src = 1;
5885 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5886 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5887 o_dst += 1 + n_param;
5888 o_src += 1 + n_param;
5889 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5890 o_dst += n_before;
5891 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5892 c2, ma->u.p[i]->v->el + o_src, n_in);
5893 o_dst += n_in;
5894 o_src += n_in;
5895 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5896 o_dst += n_after;
5897 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5898 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5899 o_dst += n_div_ma;
5900 o_src += n_div_ma;
5901 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5902 if (has_denom)
5903 isl_int_mul(dst[0], dst[0], c1);
5906 return isl_stat_ok;
5909 /* Compute the pullback of "aff" by the function represented by "ma".
5910 * In other words, plug in "ma" in "aff". The result is an affine expression
5911 * defined over the domain space of "ma".
5913 * If "aff" is represented by
5915 * (a(p) + b x + c(divs))/d
5917 * and ma is represented by
5919 * x = D(p) + F(y) + G(divs')
5921 * then the result is
5923 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5925 * The divs in the local space of the input are similarly adjusted
5926 * through a call to isl_local_space_preimage_multi_aff.
5928 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5929 __isl_take isl_multi_aff *ma)
5931 isl_aff *res = NULL;
5932 isl_local_space *ls;
5933 isl_size n_div_aff, n_div_ma;
5934 isl_int f, c1, c2, g;
5936 ma = isl_multi_aff_align_divs(ma);
5937 if (!aff || !ma)
5938 goto error;
5940 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5941 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5942 if (n_div_aff < 0 || n_div_ma < 0)
5943 goto error;
5945 ls = isl_aff_get_domain_local_space(aff);
5946 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5947 res = isl_aff_alloc(ls);
5948 if (!res)
5949 goto error;
5951 isl_int_init(f);
5952 isl_int_init(c1);
5953 isl_int_init(c2);
5954 isl_int_init(g);
5956 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5957 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5958 res = isl_aff_free(res);
5960 isl_int_clear(f);
5961 isl_int_clear(c1);
5962 isl_int_clear(c2);
5963 isl_int_clear(g);
5965 isl_aff_free(aff);
5966 isl_multi_aff_free(ma);
5967 res = isl_aff_normalize(res);
5968 return res;
5969 error:
5970 isl_aff_free(aff);
5971 isl_multi_aff_free(ma);
5972 isl_aff_free(res);
5973 return NULL;
5976 /* Compute the pullback of "aff1" by the function represented by "aff2".
5977 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5978 * defined over the domain space of "aff1".
5980 * The domain of "aff1" should match the range of "aff2", which means
5981 * that it should be single-dimensional.
5983 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5984 __isl_take isl_aff *aff2)
5986 isl_multi_aff *ma;
5988 ma = isl_multi_aff_from_aff(aff2);
5989 return isl_aff_pullback_multi_aff(aff1, ma);
5992 /* Compute the pullback of "ma1" by the function represented by "ma2".
5993 * In other words, plug in "ma2" in "ma1".
5995 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5996 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5998 int i;
5999 isl_size n;
6000 isl_space *space = NULL;
6002 isl_multi_aff_align_params_bin(&ma1, &ma2);
6003 ma2 = isl_multi_aff_align_divs(ma2);
6004 n = isl_multi_aff_size(ma1);
6005 if (n < 0 || !ma2)
6006 goto error;
6008 space = isl_space_join(isl_multi_aff_get_space(ma2),
6009 isl_multi_aff_get_space(ma1));
6011 for (i = 0; i < n; ++i) {
6012 isl_aff *aff;
6014 aff = isl_multi_aff_take_at(ma1, i);
6015 aff = isl_aff_pullback_multi_aff(aff, isl_multi_aff_copy(ma2));
6016 ma1 = isl_multi_aff_restore_at(ma1, i, aff);
6019 ma1 = isl_multi_aff_reset_space(ma1, space);
6020 isl_multi_aff_free(ma2);
6021 return ma1;
6022 error:
6023 isl_space_free(space);
6024 isl_multi_aff_free(ma2);
6025 isl_multi_aff_free(ma1);
6026 return NULL;
6029 /* Extend the local space of "dst" to include the divs
6030 * in the local space of "src".
6032 * If "src" does not have any divs or if the local spaces of "dst" and
6033 * "src" are the same, then no extension is required.
6035 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
6036 __isl_keep isl_aff *src)
6038 isl_ctx *ctx;
6039 isl_size src_n_div, dst_n_div;
6040 int *exp1 = NULL;
6041 int *exp2 = NULL;
6042 isl_bool equal;
6043 isl_mat *div;
6045 if (!src || !dst)
6046 return isl_aff_free(dst);
6048 ctx = isl_aff_get_ctx(src);
6049 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
6050 if (equal < 0)
6051 return isl_aff_free(dst);
6052 if (!equal)
6053 isl_die(ctx, isl_error_invalid,
6054 "spaces don't match", goto error);
6056 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
6057 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
6058 if (src_n_div == 0)
6059 return dst;
6060 equal = isl_local_space_is_equal(src->ls, dst->ls);
6061 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
6062 return isl_aff_free(dst);
6063 if (equal)
6064 return dst;
6066 exp1 = isl_alloc_array(ctx, int, src_n_div);
6067 exp2 = isl_alloc_array(ctx, int, dst_n_div);
6068 if (!exp1 || (dst_n_div && !exp2))
6069 goto error;
6071 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
6072 dst = isl_aff_expand_divs(dst, div, exp2);
6073 free(exp1);
6074 free(exp2);
6076 return dst;
6077 error:
6078 free(exp1);
6079 free(exp2);
6080 return isl_aff_free(dst);
6083 /* Adjust the local spaces of the affine expressions in "maff"
6084 * such that they all have the save divs.
6086 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
6087 __isl_take isl_multi_aff *maff)
6089 isl_aff *aff_0;
6090 isl_size n;
6091 int i;
6093 n = isl_multi_aff_size(maff);
6094 if (n < 0)
6095 return isl_multi_aff_free(maff);
6096 if (n <= 1)
6097 return maff;
6099 aff_0 = isl_multi_aff_take_at(maff, 0);
6100 for (i = 1; i < n; ++i) {
6101 isl_aff *aff_i;
6103 aff_i = isl_multi_aff_peek_at(maff, i);
6104 aff_0 = isl_aff_align_divs(aff_0, aff_i);
6106 maff = isl_multi_aff_restore_at(maff, 0, aff_0);
6108 aff_0 = isl_multi_aff_peek_at(maff, 0);
6109 for (i = 1; i < n; ++i) {
6110 isl_aff *aff_i;
6112 aff_i = isl_multi_aff_take_at(maff, i);
6113 aff_i = isl_aff_align_divs(aff_i, aff_0);
6114 maff = isl_multi_aff_restore_at(maff, i, aff_i);
6117 return maff;
6120 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
6122 aff = isl_aff_cow(aff);
6123 if (!aff)
6124 return NULL;
6126 aff->ls = isl_local_space_lift(aff->ls);
6127 if (!aff->ls)
6128 return isl_aff_free(aff);
6130 return aff;
6133 /* Lift "maff" to a space with extra dimensions such that the result
6134 * has no more existentially quantified variables.
6135 * If "ls" is not NULL, then *ls is assigned the local space that lies
6136 * at the basis of the lifting applied to "maff".
6138 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
6139 __isl_give isl_local_space **ls)
6141 int i;
6142 isl_space *space;
6143 isl_aff *aff;
6144 isl_size n, n_div;
6146 if (ls)
6147 *ls = NULL;
6149 n = isl_multi_aff_size(maff);
6150 if (n < 0)
6151 return isl_multi_aff_free(maff);
6153 if (n == 0) {
6154 if (ls) {
6155 isl_space *space = isl_multi_aff_get_domain_space(maff);
6156 *ls = isl_local_space_from_space(space);
6157 if (!*ls)
6158 return isl_multi_aff_free(maff);
6160 return maff;
6163 maff = isl_multi_aff_align_divs(maff);
6165 aff = isl_multi_aff_peek_at(maff, 0);
6166 n_div = isl_aff_dim(aff, isl_dim_div);
6167 if (n_div < 0)
6168 return isl_multi_aff_free(maff);
6169 space = isl_multi_aff_get_space(maff);
6170 space = isl_space_lift(isl_space_domain(space), n_div);
6171 space = isl_space_extend_domain_with_range(space,
6172 isl_multi_aff_get_space(maff));
6173 maff = isl_multi_aff_restore_space(maff, space);
6175 if (ls) {
6176 aff = isl_multi_aff_peek_at(maff, 0);
6177 *ls = isl_aff_get_domain_local_space(aff);
6178 if (!*ls)
6179 return isl_multi_aff_free(maff);
6182 for (i = 0; i < n; ++i) {
6183 aff = isl_multi_aff_take_at(maff, i);
6184 aff = isl_aff_lift(aff);
6185 maff = isl_multi_aff_restore_at(maff, i, aff);
6188 return maff;
6191 #undef TYPE
6192 #define TYPE isl_pw_multi_aff
6193 static
6194 #include "check_type_range_templ.c"
6196 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6198 __isl_give isl_pw_aff *isl_pw_multi_aff_get_at(
6199 __isl_keep isl_pw_multi_aff *pma, int pos)
6201 int i;
6202 isl_size n_out;
6203 isl_space *space;
6204 isl_pw_aff *pa;
6206 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6207 return NULL;
6209 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6210 if (n_out < 0)
6211 return NULL;
6213 space = isl_pw_multi_aff_get_space(pma);
6214 space = isl_space_drop_dims(space, isl_dim_out,
6215 pos + 1, n_out - pos - 1);
6216 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6218 pa = isl_pw_aff_alloc_size(space, pma->n);
6219 for (i = 0; i < pma->n; ++i) {
6220 isl_aff *aff;
6221 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6222 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6225 return pa;
6228 /* This is an alternative name for the function above.
6230 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6231 __isl_keep isl_pw_multi_aff *pma, int pos)
6233 return isl_pw_multi_aff_get_at(pma, pos);
6236 /* Return an isl_pw_multi_aff with the given "set" as domain and
6237 * an unnamed zero-dimensional range.
6239 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6240 __isl_take isl_set *set)
6242 isl_multi_aff *ma;
6243 isl_space *space;
6245 space = isl_set_get_space(set);
6246 space = isl_space_from_domain(space);
6247 ma = isl_multi_aff_zero(space);
6248 return isl_pw_multi_aff_alloc(set, ma);
6251 /* Add an isl_pw_multi_aff with the given "set" as domain and
6252 * an unnamed zero-dimensional range to *user.
6254 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6255 void *user)
6257 isl_union_pw_multi_aff **upma = user;
6258 isl_pw_multi_aff *pma;
6260 pma = isl_pw_multi_aff_from_domain(set);
6261 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6263 return isl_stat_ok;
6266 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6267 * an unnamed zero-dimensional range.
6269 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6270 __isl_take isl_union_set *uset)
6272 isl_space *space;
6273 isl_union_pw_multi_aff *upma;
6275 if (!uset)
6276 return NULL;
6278 space = isl_union_set_get_space(uset);
6279 upma = isl_union_pw_multi_aff_empty(space);
6281 if (isl_union_set_foreach_set(uset,
6282 &add_pw_multi_aff_from_domain, &upma) < 0)
6283 goto error;
6285 isl_union_set_free(uset);
6286 return upma;
6287 error:
6288 isl_union_set_free(uset);
6289 isl_union_pw_multi_aff_free(upma);
6290 return NULL;
6293 /* Local data for bin_entry and the callback "fn".
6295 struct isl_union_pw_multi_aff_bin_data {
6296 isl_union_pw_multi_aff *upma2;
6297 isl_union_pw_multi_aff *res;
6298 isl_pw_multi_aff *pma;
6299 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6302 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6303 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6305 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6307 struct isl_union_pw_multi_aff_bin_data *data = user;
6308 isl_stat r;
6310 data->pma = pma;
6311 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6312 data->fn, data);
6313 isl_pw_multi_aff_free(pma);
6315 return r;
6318 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6319 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6320 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6321 * as *entry. The callback should adjust data->res if desired.
6323 static __isl_give isl_union_pw_multi_aff *bin_op(
6324 __isl_take isl_union_pw_multi_aff *upma1,
6325 __isl_take isl_union_pw_multi_aff *upma2,
6326 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6328 isl_space *space;
6329 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6331 space = isl_union_pw_multi_aff_get_space(upma2);
6332 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6333 space = isl_union_pw_multi_aff_get_space(upma1);
6334 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6336 if (!upma1 || !upma2)
6337 goto error;
6339 data.upma2 = upma2;
6340 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6341 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6342 &bin_entry, &data) < 0)
6343 goto error;
6345 isl_union_pw_multi_aff_free(upma1);
6346 isl_union_pw_multi_aff_free(upma2);
6347 return data.res;
6348 error:
6349 isl_union_pw_multi_aff_free(upma1);
6350 isl_union_pw_multi_aff_free(upma2);
6351 isl_union_pw_multi_aff_free(data.res);
6352 return NULL;
6355 /* Given two isl_pw_multi_affs A -> B and C -> D,
6356 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6358 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6359 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6361 isl_space *space;
6363 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6364 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6365 isl_pw_multi_aff_get_space(pma2));
6366 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6367 &isl_multi_aff_range_product);
6370 /* Given two isl_pw_multi_affs A -> B and C -> D,
6371 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6373 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6374 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6376 isl_space *space;
6378 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6379 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6380 isl_pw_multi_aff_get_space(pma2));
6381 space = isl_space_flatten_range(space);
6382 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6383 &isl_multi_aff_flat_range_product);
6386 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6387 * to compute some form of range product and add the result to data->res.
6389 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6390 __isl_give isl_pw_multi_aff *(*range_product)(
6391 __isl_take isl_pw_multi_aff *pma1,
6392 __isl_take isl_pw_multi_aff *pma2),
6393 void *user)
6395 struct isl_union_pw_multi_aff_bin_data *data = user;
6396 isl_bool match;
6397 isl_space *space1, *space2;
6399 space1 = isl_pw_multi_aff_peek_space(data->pma);
6400 space2 = isl_pw_multi_aff_peek_space(pma2);
6401 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6402 space2, isl_dim_in);
6403 if (match < 0 || !match) {
6404 isl_pw_multi_aff_free(pma2);
6405 return match < 0 ? isl_stat_error : isl_stat_ok;
6408 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6410 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6412 return isl_stat_ok;
6415 /* If data->pma and "pma2" have the same domain space, then compute
6416 * their flat range product and add the result to data->res.
6418 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6419 void *user)
6421 return gen_range_product_entry(pma2,
6422 &isl_pw_multi_aff_flat_range_product, user);
6425 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6426 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6428 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6429 __isl_take isl_union_pw_multi_aff *upma1,
6430 __isl_take isl_union_pw_multi_aff *upma2)
6432 return bin_op(upma1, upma2, &flat_range_product_entry);
6435 /* If data->pma and "pma2" have the same domain space, then compute
6436 * their range product and add the result to data->res.
6438 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6439 void *user)
6441 return gen_range_product_entry(pma2,
6442 &isl_pw_multi_aff_range_product, user);
6445 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6446 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6448 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6449 __isl_take isl_union_pw_multi_aff *upma1,
6450 __isl_take isl_union_pw_multi_aff *upma2)
6452 return bin_op(upma1, upma2, &range_product_entry);
6455 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6456 * The parameters are assumed to have been aligned.
6458 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6459 * except that it works on two different isl_pw_* types.
6461 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6462 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6463 __isl_take isl_pw_aff *pa)
6465 int i, j, n;
6466 isl_pw_multi_aff *res = NULL;
6468 if (!pma || !pa)
6469 goto error;
6471 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6472 pa->dim, isl_dim_in))
6473 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6474 "domains don't match", goto error);
6475 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6476 goto error;
6478 n = pma->n * pa->n;
6479 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6481 for (i = 0; i < pma->n; ++i) {
6482 for (j = 0; j < pa->n; ++j) {
6483 isl_set *common;
6484 isl_multi_aff *res_ij;
6485 int empty;
6487 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6488 isl_set_copy(pa->p[j].set));
6489 empty = isl_set_plain_is_empty(common);
6490 if (empty < 0 || empty) {
6491 isl_set_free(common);
6492 if (empty < 0)
6493 goto error;
6494 continue;
6497 res_ij = isl_multi_aff_set_aff(
6498 isl_multi_aff_copy(pma->p[i].maff), pos,
6499 isl_aff_copy(pa->p[j].aff));
6500 res_ij = isl_multi_aff_gist(res_ij,
6501 isl_set_copy(common));
6503 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6507 isl_pw_multi_aff_free(pma);
6508 isl_pw_aff_free(pa);
6509 return res;
6510 error:
6511 isl_pw_multi_aff_free(pma);
6512 isl_pw_aff_free(pa);
6513 return isl_pw_multi_aff_free(res);
6516 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6518 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6519 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6520 __isl_take isl_pw_aff *pa)
6522 isl_bool equal_params;
6524 if (!pma || !pa)
6525 goto error;
6526 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6527 if (equal_params < 0)
6528 goto error;
6529 if (equal_params)
6530 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6531 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6532 isl_pw_aff_check_named_params(pa) < 0)
6533 goto error;
6534 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6535 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6536 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6537 error:
6538 isl_pw_multi_aff_free(pma);
6539 isl_pw_aff_free(pa);
6540 return NULL;
6543 /* Do the parameters of "pa" match those of "space"?
6545 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6546 __isl_keep isl_space *space)
6548 isl_space *pa_space;
6549 isl_bool match;
6551 if (!pa || !space)
6552 return isl_bool_error;
6554 pa_space = isl_pw_aff_get_space(pa);
6556 match = isl_space_has_equal_params(space, pa_space);
6558 isl_space_free(pa_space);
6559 return match;
6562 /* Check that the domain space of "pa" matches "space".
6564 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6565 __isl_keep isl_space *space)
6567 isl_space *pa_space;
6568 isl_bool match;
6570 if (!pa || !space)
6571 return isl_stat_error;
6573 pa_space = isl_pw_aff_get_space(pa);
6575 match = isl_space_has_equal_params(space, pa_space);
6576 if (match < 0)
6577 goto error;
6578 if (!match)
6579 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6580 "parameters don't match", goto error);
6581 match = isl_space_tuple_is_equal(space, isl_dim_in,
6582 pa_space, isl_dim_in);
6583 if (match < 0)
6584 goto error;
6585 if (!match)
6586 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6587 "domains don't match", goto error);
6588 isl_space_free(pa_space);
6589 return isl_stat_ok;
6590 error:
6591 isl_space_free(pa_space);
6592 return isl_stat_error;
6595 #undef BASE
6596 #define BASE pw_aff
6597 #undef DOMBASE
6598 #define DOMBASE set
6600 #include <isl_multi_explicit_domain.c>
6601 #include <isl_multi_pw_aff_explicit_domain.c>
6602 #include <isl_multi_templ.c>
6603 #include <isl_multi_un_op_templ.c>
6604 #include <isl_multi_bin_val_templ.c>
6605 #include <isl_multi_add_constant_templ.c>
6606 #include <isl_multi_apply_set.c>
6607 #include <isl_multi_arith_templ.c>
6608 #include <isl_multi_bind_templ.c>
6609 #include <isl_multi_bind_domain_templ.c>
6610 #include <isl_multi_coalesce.c>
6611 #include <isl_multi_domain_templ.c>
6612 #include <isl_multi_dim_id_templ.c>
6613 #include <isl_multi_dims.c>
6614 #include <isl_multi_from_base_templ.c>
6615 #include <isl_multi_gist.c>
6616 #include <isl_multi_hash.c>
6617 #include <isl_multi_identity_templ.c>
6618 #include <isl_multi_align_set.c>
6619 #include <isl_multi_insert_domain_templ.c>
6620 #include <isl_multi_intersect.c>
6621 #include <isl_multi_min_max_templ.c>
6622 #include <isl_multi_move_dims_templ.c>
6623 #include <isl_multi_nan_templ.c>
6624 #include <isl_multi_param_templ.c>
6625 #include <isl_multi_product_templ.c>
6626 #include <isl_multi_splice_templ.c>
6627 #include <isl_multi_tuple_id_templ.c>
6628 #include <isl_multi_union_add_templ.c>
6629 #include <isl_multi_zero_templ.c>
6630 #include <isl_multi_unbind_params_templ.c>
6632 /* Is every element of "mpa" defined over a single universe domain?
6634 isl_bool isl_multi_pw_aff_isa_multi_aff(__isl_keep isl_multi_pw_aff *mpa)
6636 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_isa_aff);
6639 /* Given that every element of "mpa" is defined over a single universe domain,
6640 * return the corresponding base expressions.
6642 __isl_give isl_multi_aff *isl_multi_pw_aff_as_multi_aff(
6643 __isl_take isl_multi_pw_aff *mpa)
6645 int i;
6646 isl_size n;
6647 isl_multi_aff *ma;
6649 n = isl_multi_pw_aff_size(mpa);
6650 if (n < 0)
6651 mpa = isl_multi_pw_aff_free(mpa);
6652 ma = isl_multi_aff_alloc(isl_multi_pw_aff_get_space(mpa));
6653 for (i = 0; i < n; ++i) {
6654 isl_aff *aff;
6656 aff = isl_pw_aff_as_aff(isl_multi_pw_aff_get_at(mpa, i));
6657 ma = isl_multi_aff_set_aff(ma, i, aff);
6659 isl_multi_pw_aff_free(mpa);
6660 return ma;
6663 /* If "mpa" has an explicit domain, then intersect the domain of "map"
6664 * with this explicit domain.
6666 __isl_give isl_map *isl_map_intersect_multi_pw_aff_explicit_domain(
6667 __isl_take isl_map *map, __isl_keep isl_multi_pw_aff *mpa)
6669 isl_set *dom;
6671 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6672 return map;
6674 dom = isl_multi_pw_aff_domain(isl_multi_pw_aff_copy(mpa));
6675 map = isl_map_intersect_domain(map, dom);
6677 return map;
6680 /* Are all elements of "mpa" piecewise constants?
6682 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6684 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6687 /* Does "mpa" have a non-trivial explicit domain?
6689 * The explicit domain, if present, is trivial if it represents
6690 * an (obviously) universe set.
6692 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6693 __isl_keep isl_multi_pw_aff *mpa)
6695 if (!mpa)
6696 return isl_bool_error;
6697 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6698 return isl_bool_false;
6699 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6702 #undef BASE
6703 #define BASE set
6705 #include "isl_opt_mpa_templ.c"
6707 /* Compute the minima of the set dimensions as a function of the
6708 * parameters, but independently of the other set dimensions.
6710 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6712 return set_opt_mpa(set, &isl_set_dim_min);
6715 /* Compute the maxima of the set dimensions as a function of the
6716 * parameters, but independently of the other set dimensions.
6718 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6720 return set_opt_mpa(set, &isl_set_dim_max);
6723 #undef BASE
6724 #define BASE map
6726 #include "isl_opt_mpa_templ.c"
6728 /* Compute the minima of the output dimensions as a function of the
6729 * parameters and input dimensions, but independently of
6730 * the other output dimensions.
6732 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6734 return map_opt_mpa(map, &isl_map_dim_min);
6737 /* Compute the maxima of the output dimensions as a function of the
6738 * parameters and input dimensions, but independently of
6739 * the other output dimensions.
6741 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6743 return map_opt_mpa(map, &isl_map_dim_max);
6746 #undef TYPE
6747 #define TYPE isl_pw_multi_aff
6748 #include "isl_type_check_match_range_multi_val.c"
6750 /* Apply "fn" to the base expressions of "pma" and "mv".
6752 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_op_multi_val(
6753 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv,
6754 __isl_give isl_multi_aff *(*fn)(__isl_take isl_multi_aff *ma,
6755 __isl_take isl_multi_val *mv))
6757 int i;
6758 isl_size n;
6760 if (isl_pw_multi_aff_check_match_range_multi_val(pma, mv) < 0)
6761 goto error;
6763 n = isl_pw_multi_aff_n_piece(pma);
6764 if (n < 0)
6765 goto error;
6767 for (i = 0; i < n; ++i) {
6768 isl_multi_aff *ma;
6770 ma = isl_pw_multi_aff_take_base_at(pma, i);
6771 ma = fn(ma, isl_multi_val_copy(mv));
6772 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
6775 isl_multi_val_free(mv);
6776 return pma;
6777 error:
6778 isl_multi_val_free(mv);
6779 isl_pw_multi_aff_free(pma);
6780 return NULL;
6783 /* Scale the elements of "pma" by the corresponding elements of "mv".
6785 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6786 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6788 return isl_pw_multi_aff_op_multi_val(pma, mv,
6789 &isl_multi_aff_scale_multi_val);
6792 /* Scale the elements of "pma" down by the corresponding elements of "mv".
6794 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_down_multi_val(
6795 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6797 return isl_pw_multi_aff_op_multi_val(pma, mv,
6798 &isl_multi_aff_scale_down_multi_val);
6801 /* This function is called for each entry of an isl_union_pw_multi_aff.
6802 * If the space of the entry matches that of data->mv,
6803 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6804 * Otherwise, return an empty isl_pw_multi_aff.
6806 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6807 __isl_take isl_pw_multi_aff *pma, void *user)
6809 isl_bool equal;
6810 isl_multi_val *mv = user;
6812 equal = isl_pw_multi_aff_match_range_multi_val(pma, mv);
6813 if (equal < 0)
6814 return isl_pw_multi_aff_free(pma);
6815 if (!equal) {
6816 isl_space *space = isl_pw_multi_aff_get_space(pma);
6817 isl_pw_multi_aff_free(pma);
6818 return isl_pw_multi_aff_empty(space);
6821 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6824 /* Scale the elements of "upma" by the corresponding elements of "mv",
6825 * for those entries that match the space of "mv".
6827 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6828 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6830 struct isl_union_pw_multi_aff_transform_control control = {
6831 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6832 .fn_user = mv,
6835 upma = isl_union_pw_multi_aff_align_params(upma,
6836 isl_multi_val_get_space(mv));
6837 mv = isl_multi_val_align_params(mv,
6838 isl_union_pw_multi_aff_get_space(upma));
6839 if (!upma || !mv)
6840 goto error;
6842 return isl_union_pw_multi_aff_transform(upma, &control);
6844 isl_multi_val_free(mv);
6845 return upma;
6846 error:
6847 isl_multi_val_free(mv);
6848 isl_union_pw_multi_aff_free(upma);
6849 return NULL;
6852 /* Construct and return a piecewise multi affine expression
6853 * in the given space with value zero in each of the output dimensions and
6854 * a universe domain.
6856 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6858 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6861 /* Construct and return a piecewise multi affine expression
6862 * that is equal to the given piecewise affine expression.
6864 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6865 __isl_take isl_pw_aff *pa)
6867 int i;
6868 isl_space *space;
6869 isl_pw_multi_aff *pma;
6871 if (!pa)
6872 return NULL;
6874 space = isl_pw_aff_get_space(pa);
6875 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6877 for (i = 0; i < pa->n; ++i) {
6878 isl_set *set;
6879 isl_multi_aff *ma;
6881 set = isl_set_copy(pa->p[i].set);
6882 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6883 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6886 isl_pw_aff_free(pa);
6887 return pma;
6890 /* Construct and return a piecewise multi affine expression
6891 * that is equal to the given multi piecewise affine expression
6892 * on the shared domain of the piecewise affine expressions,
6893 * in the special case of a 0D multi piecewise affine expression.
6895 * Create a piecewise multi affine expression with the explicit domain of
6896 * the 0D multi piecewise affine expression as domain.
6898 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6899 __isl_take isl_multi_pw_aff *mpa)
6901 isl_space *space;
6902 isl_set *dom;
6903 isl_multi_aff *ma;
6905 space = isl_multi_pw_aff_get_space(mpa);
6906 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6907 isl_multi_pw_aff_free(mpa);
6909 ma = isl_multi_aff_zero(space);
6910 return isl_pw_multi_aff_alloc(dom, ma);
6913 /* Construct and return a piecewise multi affine expression
6914 * that is equal to the given multi piecewise affine expression
6915 * on the shared domain of the piecewise affine expressions.
6917 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6918 __isl_take isl_multi_pw_aff *mpa)
6920 int i;
6921 isl_space *space;
6922 isl_pw_aff *pa;
6923 isl_pw_multi_aff *pma;
6925 if (!mpa)
6926 return NULL;
6928 if (mpa->n == 0)
6929 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6931 space = isl_multi_pw_aff_get_space(mpa);
6932 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6933 pma = isl_pw_multi_aff_from_pw_aff(pa);
6935 for (i = 1; i < mpa->n; ++i) {
6936 isl_pw_multi_aff *pma_i;
6938 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6939 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6940 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6943 pma = isl_pw_multi_aff_reset_space(pma, space);
6945 isl_multi_pw_aff_free(mpa);
6946 return pma;
6949 /* Convenience function that constructs an isl_multi_pw_aff
6950 * directly from an isl_aff.
6952 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6954 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6957 /* Construct and return a multi piecewise affine expression
6958 * that is equal to the given multi affine expression.
6960 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6961 __isl_take isl_multi_aff *ma)
6963 int i;
6964 isl_size n;
6965 isl_multi_pw_aff *mpa;
6967 n = isl_multi_aff_dim(ma, isl_dim_out);
6968 if (n < 0)
6969 ma = isl_multi_aff_free(ma);
6970 if (!ma)
6971 return NULL;
6973 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6975 for (i = 0; i < n; ++i) {
6976 isl_pw_aff *pa;
6978 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6979 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6982 isl_multi_aff_free(ma);
6983 return mpa;
6986 /* This function performs the same operation as isl_multi_pw_aff_from_multi_aff,
6987 * but is considered as a function on an isl_multi_aff when exported.
6989 __isl_give isl_multi_pw_aff *isl_multi_aff_to_multi_pw_aff(
6990 __isl_take isl_multi_aff *ma)
6992 return isl_multi_pw_aff_from_multi_aff(ma);
6995 /* Construct and return a multi piecewise affine expression
6996 * that is equal to the given piecewise multi affine expression.
6998 * If the resulting multi piecewise affine expression has
6999 * an explicit domain, then assign it the domain of the input.
7000 * In other cases, the domain is stored in the individual elements.
7002 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
7003 __isl_take isl_pw_multi_aff *pma)
7005 int i;
7006 isl_size n;
7007 isl_space *space;
7008 isl_multi_pw_aff *mpa;
7010 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
7011 if (n < 0)
7012 pma = isl_pw_multi_aff_free(pma);
7013 space = isl_pw_multi_aff_get_space(pma);
7014 mpa = isl_multi_pw_aff_alloc(space);
7016 for (i = 0; i < n; ++i) {
7017 isl_pw_aff *pa;
7019 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
7020 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
7022 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
7023 isl_set *dom;
7025 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
7026 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
7029 isl_pw_multi_aff_free(pma);
7030 return mpa;
7033 /* This function performs the same operation as
7034 * isl_multi_pw_aff_from_pw_multi_aff,
7035 * but is considered as a function on an isl_pw_multi_aff when exported.
7037 __isl_give isl_multi_pw_aff *isl_pw_multi_aff_to_multi_pw_aff(
7038 __isl_take isl_pw_multi_aff *pma)
7040 return isl_multi_pw_aff_from_pw_multi_aff(pma);
7043 /* Do "pa1" and "pa2" represent the same function?
7045 * We first check if they are obviously equal.
7046 * If not, we convert them to maps and check if those are equal.
7048 * If "pa1" or "pa2" contain any NaNs, then they are considered
7049 * not to be the same. A NaN is not equal to anything, not even
7050 * to another NaN.
7052 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
7053 __isl_keep isl_pw_aff *pa2)
7055 isl_bool equal;
7056 isl_bool has_nan;
7057 isl_map *map1, *map2;
7059 if (!pa1 || !pa2)
7060 return isl_bool_error;
7062 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
7063 if (equal < 0 || equal)
7064 return equal;
7065 has_nan = either_involves_nan(pa1, pa2);
7066 if (has_nan < 0)
7067 return isl_bool_error;
7068 if (has_nan)
7069 return isl_bool_false;
7071 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
7072 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
7073 equal = isl_map_is_equal(map1, map2);
7074 isl_map_free(map1);
7075 isl_map_free(map2);
7077 return equal;
7080 /* Do "mpa1" and "mpa2" represent the same function?
7082 * Note that we cannot convert the entire isl_multi_pw_aff
7083 * to a map because the domains of the piecewise affine expressions
7084 * may not be the same.
7086 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
7087 __isl_keep isl_multi_pw_aff *mpa2)
7089 int i;
7090 isl_bool equal, equal_params;
7092 if (!mpa1 || !mpa2)
7093 return isl_bool_error;
7095 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
7096 if (equal_params < 0)
7097 return isl_bool_error;
7098 if (!equal_params) {
7099 if (!isl_space_has_named_params(mpa1->space))
7100 return isl_bool_false;
7101 if (!isl_space_has_named_params(mpa2->space))
7102 return isl_bool_false;
7103 mpa1 = isl_multi_pw_aff_copy(mpa1);
7104 mpa2 = isl_multi_pw_aff_copy(mpa2);
7105 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7106 isl_multi_pw_aff_get_space(mpa2));
7107 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7108 isl_multi_pw_aff_get_space(mpa1));
7109 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
7110 isl_multi_pw_aff_free(mpa1);
7111 isl_multi_pw_aff_free(mpa2);
7112 return equal;
7115 equal = isl_space_is_equal(mpa1->space, mpa2->space);
7116 if (equal < 0 || !equal)
7117 return equal;
7119 for (i = 0; i < mpa1->n; ++i) {
7120 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
7121 if (equal < 0 || !equal)
7122 return equal;
7125 return isl_bool_true;
7128 /* Do "pma1" and "pma2" represent the same function?
7130 * First check if they are obviously equal.
7131 * If not, then convert them to maps and check if those are equal.
7133 * If "pa1" or "pa2" contain any NaNs, then they are considered
7134 * not to be the same. A NaN is not equal to anything, not even
7135 * to another NaN.
7137 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
7138 __isl_keep isl_pw_multi_aff *pma2)
7140 isl_bool equal;
7141 isl_bool has_nan;
7142 isl_map *map1, *map2;
7144 if (!pma1 || !pma2)
7145 return isl_bool_error;
7147 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
7148 if (equal < 0 || equal)
7149 return equal;
7150 has_nan = isl_pw_multi_aff_involves_nan(pma1);
7151 if (has_nan >= 0 && !has_nan)
7152 has_nan = isl_pw_multi_aff_involves_nan(pma2);
7153 if (has_nan < 0 || has_nan)
7154 return isl_bool_not(has_nan);
7156 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
7157 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
7158 equal = isl_map_is_equal(map1, map2);
7159 isl_map_free(map1);
7160 isl_map_free(map2);
7162 return equal;
7165 #undef BASE
7166 #define BASE multi_aff
7168 #include "isl_multi_pw_aff_pullback_templ.c"
7170 #undef BASE
7171 #define BASE pw_multi_aff
7173 #include "isl_multi_pw_aff_pullback_templ.c"
7175 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7176 * with the domain of "aff". The domain of the result is the same
7177 * as that of "mpa".
7178 * "mpa" and "aff" are assumed to have been aligned.
7180 * We first extract the parametric constant from "aff", defined
7181 * over the correct domain.
7182 * Then we add the appropriate combinations of the members of "mpa".
7183 * Finally, we add the integer divisions through recursive calls.
7185 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7186 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7188 int i;
7189 isl_size n_in, n_div, n_mpa_in;
7190 isl_space *space;
7191 isl_val *v;
7192 isl_pw_aff *pa;
7193 isl_aff *tmp;
7195 n_in = isl_aff_dim(aff, isl_dim_in);
7196 n_div = isl_aff_dim(aff, isl_dim_div);
7197 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7198 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7199 goto error;
7201 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7202 tmp = isl_aff_copy(aff);
7203 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7204 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7205 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7206 tmp = isl_aff_reset_domain_space(tmp, space);
7207 pa = isl_pw_aff_from_aff(tmp);
7209 for (i = 0; i < n_in; ++i) {
7210 isl_pw_aff *pa_i;
7212 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7213 continue;
7214 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7215 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7216 pa_i = isl_pw_aff_scale_val(pa_i, v);
7217 pa = isl_pw_aff_add(pa, pa_i);
7220 for (i = 0; i < n_div; ++i) {
7221 isl_aff *div;
7222 isl_pw_aff *pa_i;
7224 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7225 continue;
7226 div = isl_aff_get_div(aff, i);
7227 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7228 isl_multi_pw_aff_copy(mpa), div);
7229 pa_i = isl_pw_aff_floor(pa_i);
7230 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7231 pa_i = isl_pw_aff_scale_val(pa_i, v);
7232 pa = isl_pw_aff_add(pa, pa_i);
7235 isl_multi_pw_aff_free(mpa);
7236 isl_aff_free(aff);
7238 return pa;
7239 error:
7240 isl_multi_pw_aff_free(mpa);
7241 isl_aff_free(aff);
7242 return NULL;
7245 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7246 * with the domain of "aff". The domain of the result is the same
7247 * as that of "mpa".
7249 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7250 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7252 isl_bool equal_params;
7254 if (!aff || !mpa)
7255 goto error;
7256 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7257 if (equal_params < 0)
7258 goto error;
7259 if (equal_params)
7260 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7262 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7263 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7265 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7266 error:
7267 isl_aff_free(aff);
7268 isl_multi_pw_aff_free(mpa);
7269 return NULL;
7272 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7273 * with the domain of "pa". The domain of the result is the same
7274 * as that of "mpa".
7275 * "mpa" and "pa" are assumed to have been aligned.
7277 * We consider each piece in turn. Note that the domains of the
7278 * pieces are assumed to be disjoint and they remain disjoint
7279 * after taking the preimage (over the same function).
7281 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7282 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7284 isl_space *space;
7285 isl_pw_aff *res;
7286 int i;
7288 if (!mpa || !pa)
7289 goto error;
7291 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7292 isl_pw_aff_get_space(pa));
7293 res = isl_pw_aff_empty(space);
7295 for (i = 0; i < pa->n; ++i) {
7296 isl_pw_aff *pa_i;
7297 isl_set *domain;
7299 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7300 isl_multi_pw_aff_copy(mpa),
7301 isl_aff_copy(pa->p[i].aff));
7302 domain = isl_set_copy(pa->p[i].set);
7303 domain = isl_set_preimage_multi_pw_aff(domain,
7304 isl_multi_pw_aff_copy(mpa));
7305 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7306 res = isl_pw_aff_add_disjoint(res, pa_i);
7309 isl_pw_aff_free(pa);
7310 isl_multi_pw_aff_free(mpa);
7311 return res;
7312 error:
7313 isl_pw_aff_free(pa);
7314 isl_multi_pw_aff_free(mpa);
7315 return NULL;
7318 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7319 * with the domain of "pa". The domain of the result is the same
7320 * as that of "mpa".
7322 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7323 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7325 isl_bool equal_params;
7327 if (!pa || !mpa)
7328 goto error;
7329 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7330 if (equal_params < 0)
7331 goto error;
7332 if (equal_params)
7333 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7335 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7336 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7338 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7339 error:
7340 isl_pw_aff_free(pa);
7341 isl_multi_pw_aff_free(mpa);
7342 return NULL;
7345 /* Compute the pullback of "pa" by the function represented by "mpa".
7346 * In other words, plug in "mpa" in "pa".
7348 * The pullback is computed by applying "pa" to "mpa".
7350 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7351 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7353 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7356 #undef BASE
7357 #define BASE multi_pw_aff
7359 #include "isl_multi_pw_aff_pullback_templ.c"
7361 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7362 * of "mpa1" and "mpa2" live in the same space, construct map space
7363 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7364 * with this map space as extract argument.
7366 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7367 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7368 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7369 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7371 int match;
7372 isl_space *space1, *space2;
7373 isl_map *res;
7375 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7376 isl_multi_pw_aff_get_space(mpa2));
7377 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7378 isl_multi_pw_aff_get_space(mpa1));
7379 if (!mpa1 || !mpa2)
7380 goto error;
7381 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7382 mpa2->space, isl_dim_out);
7383 if (match < 0)
7384 goto error;
7385 if (!match)
7386 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7387 "range spaces don't match", goto error);
7388 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7389 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7390 space1 = isl_space_map_from_domain_and_range(space1, space2);
7392 res = order(mpa1, mpa2, space1);
7393 isl_multi_pw_aff_free(mpa1);
7394 isl_multi_pw_aff_free(mpa2);
7395 return res;
7396 error:
7397 isl_multi_pw_aff_free(mpa1);
7398 isl_multi_pw_aff_free(mpa2);
7399 return NULL;
7402 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7403 * where the function values are equal. "space" is the space of the result.
7404 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7406 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7407 * in the sequences are equal.
7409 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7410 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7411 __isl_take isl_space *space)
7413 int i;
7414 isl_size n;
7415 isl_map *res;
7417 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7418 if (n < 0)
7419 space = isl_space_free(space);
7420 res = isl_map_universe(space);
7422 for (i = 0; i < n; ++i) {
7423 isl_pw_aff *pa1, *pa2;
7424 isl_map *map;
7426 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7427 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7428 map = isl_pw_aff_eq_map(pa1, pa2);
7429 res = isl_map_intersect(res, map);
7432 return res;
7435 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7436 * where the function values are equal.
7438 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7439 __isl_take isl_multi_pw_aff *mpa2)
7441 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7442 &isl_multi_pw_aff_eq_map_on_space);
7445 /* Intersect "map" with the result of applying "order"
7446 * on two copies of "mpa".
7448 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7449 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7450 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7451 __isl_take isl_multi_pw_aff *mpa2))
7453 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7456 /* Return the subset of "map" where the domain and the range
7457 * have equal "mpa" values.
7459 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7460 __isl_take isl_multi_pw_aff *mpa)
7462 return isl_map_order_at_multi_pw_aff(map, mpa,
7463 &isl_multi_pw_aff_eq_map);
7466 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7467 * where the function values of "mpa1" lexicographically satisfies
7468 * "strict_base"/"base" compared to that of "mpa2".
7469 * "space" is the space of the result.
7470 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7472 * "mpa1" lexicographically satisfies "strict_base"/"base" compared to "mpa2"
7473 * if, for some i, the i-th element of "mpa1" satisfies "strict_base"/"base"
7474 * when compared to the i-th element of "mpa2" while all previous elements are
7475 * pairwise equal.
7476 * In particular, if i corresponds to the final elements
7477 * then they need to satisfy "base", while "strict_base" needs to be satisfied
7478 * for other values of i.
7479 * If "base" is a strict order, then "base" and "strict_base" are the same.
7481 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7482 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7483 __isl_give isl_map *(*strict_base)(__isl_take isl_pw_aff *pa1,
7484 __isl_take isl_pw_aff *pa2),
7485 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7486 __isl_take isl_pw_aff *pa2),
7487 __isl_take isl_space *space)
7489 int i;
7490 isl_size n;
7491 isl_map *res, *rest;
7493 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7494 if (n < 0)
7495 space = isl_space_free(space);
7496 res = isl_map_empty(isl_space_copy(space));
7497 rest = isl_map_universe(space);
7499 for (i = 0; i < n; ++i) {
7500 int last;
7501 isl_pw_aff *pa1, *pa2;
7502 isl_map *map;
7504 last = i == n - 1;
7506 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7507 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7508 map = last ? base(pa1, pa2) : strict_base(pa1, pa2);
7509 map = isl_map_intersect(map, isl_map_copy(rest));
7510 res = isl_map_union(res, map);
7512 if (last)
7513 continue;
7515 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7516 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7517 map = isl_pw_aff_eq_map(pa1, pa2);
7518 rest = isl_map_intersect(rest, map);
7521 isl_map_free(rest);
7522 return res;
7525 #undef ORDER
7526 #define ORDER le
7527 #undef STRICT_ORDER
7528 #define STRICT_ORDER lt
7529 #include "isl_aff_lex_templ.c"
7531 #undef ORDER
7532 #define ORDER lt
7533 #undef STRICT_ORDER
7534 #define STRICT_ORDER lt
7535 #include "isl_aff_lex_templ.c"
7537 #undef ORDER
7538 #define ORDER ge
7539 #undef STRICT_ORDER
7540 #define STRICT_ORDER gt
7541 #include "isl_aff_lex_templ.c"
7543 #undef ORDER
7544 #define ORDER gt
7545 #undef STRICT_ORDER
7546 #define STRICT_ORDER gt
7547 #include "isl_aff_lex_templ.c"
7549 /* Compare two isl_affs.
7551 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7552 * than "aff2" and 0 if they are equal.
7554 * The order is fairly arbitrary. We do consider expressions that only involve
7555 * earlier dimensions as "smaller".
7557 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7559 int cmp;
7560 int last1, last2;
7562 if (aff1 == aff2)
7563 return 0;
7565 if (!aff1)
7566 return -1;
7567 if (!aff2)
7568 return 1;
7570 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7571 if (cmp != 0)
7572 return cmp;
7574 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7575 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7576 if (last1 != last2)
7577 return last1 - last2;
7579 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7582 /* Compare two isl_pw_affs.
7584 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7585 * than "pa2" and 0 if they are equal.
7587 * The order is fairly arbitrary. We do consider expressions that only involve
7588 * earlier dimensions as "smaller".
7590 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7591 __isl_keep isl_pw_aff *pa2)
7593 int i;
7594 int cmp;
7596 if (pa1 == pa2)
7597 return 0;
7599 if (!pa1)
7600 return -1;
7601 if (!pa2)
7602 return 1;
7604 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7605 if (cmp != 0)
7606 return cmp;
7608 if (pa1->n != pa2->n)
7609 return pa1->n - pa2->n;
7611 for (i = 0; i < pa1->n; ++i) {
7612 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7613 if (cmp != 0)
7614 return cmp;
7615 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7616 if (cmp != 0)
7617 return cmp;
7620 return 0;
7623 /* Return a piecewise affine expression that is equal to "v" on "domain".
7625 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7626 __isl_take isl_val *v)
7628 isl_space *space;
7629 isl_local_space *ls;
7630 isl_aff *aff;
7632 space = isl_set_get_space(domain);
7633 ls = isl_local_space_from_space(space);
7634 aff = isl_aff_val_on_domain(ls, v);
7636 return isl_pw_aff_alloc(domain, aff);
7639 /* This function performs the same operation as isl_pw_aff_val_on_domain,
7640 * but is considered as a function on an isl_set when exported.
7642 __isl_give isl_pw_aff *isl_set_pw_aff_on_domain_val(__isl_take isl_set *domain,
7643 __isl_take isl_val *v)
7645 return isl_pw_aff_val_on_domain(domain, v);
7648 /* Return a piecewise affine expression that is equal to the parameter
7649 * with identifier "id" on "domain".
7651 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7652 __isl_take isl_set *domain, __isl_take isl_id *id)
7654 isl_space *space;
7655 isl_aff *aff;
7657 space = isl_set_get_space(domain);
7658 space = isl_space_add_param_id(space, isl_id_copy(id));
7659 domain = isl_set_align_params(domain, isl_space_copy(space));
7660 aff = isl_aff_param_on_domain_space_id(space, id);
7662 return isl_pw_aff_alloc(domain, aff);
7665 /* This function performs the same operation as
7666 * isl_pw_aff_param_on_domain_id,
7667 * but is considered as a function on an isl_set when exported.
7669 __isl_give isl_pw_aff *isl_set_param_pw_aff_on_domain_id(
7670 __isl_take isl_set *domain, __isl_take isl_id *id)
7672 return isl_pw_aff_param_on_domain_id(domain, id);
7675 /* Return a multi affine expression that is equal to "mv" on domain
7676 * space "space".
7678 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_domain_space(
7679 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7681 int i;
7682 isl_size n;
7683 isl_space *space2;
7684 isl_local_space *ls;
7685 isl_multi_aff *ma;
7687 n = isl_multi_val_dim(mv, isl_dim_set);
7688 if (!space || n < 0)
7689 goto error;
7691 space2 = isl_multi_val_get_space(mv);
7692 space2 = isl_space_align_params(space2, isl_space_copy(space));
7693 space = isl_space_align_params(space, isl_space_copy(space2));
7694 space = isl_space_map_from_domain_and_range(space, space2);
7695 ma = isl_multi_aff_alloc(isl_space_copy(space));
7696 ls = isl_local_space_from_space(isl_space_domain(space));
7697 for (i = 0; i < n; ++i) {
7698 isl_val *v;
7699 isl_aff *aff;
7701 v = isl_multi_val_get_val(mv, i);
7702 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7703 ma = isl_multi_aff_set_aff(ma, i, aff);
7705 isl_local_space_free(ls);
7707 isl_multi_val_free(mv);
7708 return ma;
7709 error:
7710 isl_space_free(space);
7711 isl_multi_val_free(mv);
7712 return NULL;
7715 /* This is an alternative name for the function above.
7717 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7718 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7720 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7723 /* This function performs the same operation as
7724 * isl_multi_aff_multi_val_on_domain_space,
7725 * but is considered as a function on an isl_space when exported.
7727 __isl_give isl_multi_aff *isl_space_multi_aff_on_domain_multi_val(
7728 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7730 return isl_multi_aff_multi_val_on_domain_space(space, mv);
7733 /* Return a piecewise multi-affine expression
7734 * that is equal to "mv" on "domain".
7736 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7737 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7739 isl_space *space;
7740 isl_multi_aff *ma;
7742 space = isl_set_get_space(domain);
7743 ma = isl_multi_aff_multi_val_on_space(space, mv);
7745 return isl_pw_multi_aff_alloc(domain, ma);
7748 /* This function performs the same operation as
7749 * isl_pw_multi_aff_multi_val_on_domain,
7750 * but is considered as a function on an isl_set when exported.
7752 __isl_give isl_pw_multi_aff *isl_set_pw_multi_aff_on_domain_multi_val(
7753 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7755 return isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7758 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7759 * mv is the value that should be attained on each domain set
7760 * res collects the results
7762 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7763 isl_multi_val *mv;
7764 isl_union_pw_multi_aff *res;
7767 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7768 * and add it to data->res.
7770 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7771 void *user)
7773 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7774 isl_pw_multi_aff *pma;
7775 isl_multi_val *mv;
7777 mv = isl_multi_val_copy(data->mv);
7778 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7779 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7781 return data->res ? isl_stat_ok : isl_stat_error;
7784 /* Return a union piecewise multi-affine expression
7785 * that is equal to "mv" on "domain".
7787 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7788 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7790 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7791 isl_space *space;
7793 space = isl_union_set_get_space(domain);
7794 data.res = isl_union_pw_multi_aff_empty(space);
7795 data.mv = mv;
7796 if (isl_union_set_foreach_set(domain,
7797 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7798 data.res = isl_union_pw_multi_aff_free(data.res);
7799 isl_union_set_free(domain);
7800 isl_multi_val_free(mv);
7801 return data.res;
7804 /* Compute the pullback of data->pma by the function represented by "pma2",
7805 * provided the spaces match, and add the results to data->res.
7807 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7809 struct isl_union_pw_multi_aff_bin_data *data = user;
7811 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7812 pma2->dim, isl_dim_out)) {
7813 isl_pw_multi_aff_free(pma2);
7814 return isl_stat_ok;
7817 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7818 isl_pw_multi_aff_copy(data->pma), pma2);
7820 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7821 if (!data->res)
7822 return isl_stat_error;
7824 return isl_stat_ok;
7827 /* Compute the pullback of "upma1" by the function represented by "upma2".
7829 __isl_give isl_union_pw_multi_aff *
7830 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7831 __isl_take isl_union_pw_multi_aff *upma1,
7832 __isl_take isl_union_pw_multi_aff *upma2)
7834 return bin_op(upma1, upma2, &pullback_entry);
7837 /* Apply "upma2" to "upma1".
7839 * That is, compute the pullback of "upma2" by "upma1".
7841 __isl_give isl_union_pw_multi_aff *
7842 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7843 __isl_take isl_union_pw_multi_aff *upma1,
7844 __isl_take isl_union_pw_multi_aff *upma2)
7846 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7849 #undef TYPE
7850 #define TYPE isl_pw_multi_aff
7851 static
7852 #include "isl_copy_tuple_id_templ.c"
7854 /* Given a function "pma1" of the form A[B -> C] -> D and
7855 * a function "pma2" of the form E -> B,
7856 * replace the domain of the wrapped relation inside the domain of "pma1"
7857 * by the preimage with respect to "pma2".
7858 * In other words, plug in "pma2" in this nested domain.
7859 * The result is of the form A[E -> C] -> D.
7861 * In particular, extend E -> B to A[E -> C] -> A[B -> C] and
7862 * plug that into "pma1".
7864 __isl_give isl_pw_multi_aff *
7865 isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7866 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
7868 isl_space *pma1_space, *pma2_space;
7869 isl_space *space;
7870 isl_pw_multi_aff *id;
7872 pma1_space = isl_pw_multi_aff_peek_space(pma1);
7873 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7875 if (isl_space_check_domain_is_wrapping(pma1_space) < 0)
7876 goto error;
7877 if (isl_space_check_wrapped_tuple_is_equal(pma1_space,
7878 isl_dim_in, isl_dim_in, pma2_space, isl_dim_out) < 0)
7879 goto error;
7881 space = isl_space_domain(isl_space_copy(pma1_space));
7882 space = isl_space_range(isl_space_unwrap(space));
7883 id = isl_pw_multi_aff_identity_on_domain_space(space);
7884 pma2 = isl_pw_multi_aff_product(pma2, id);
7886 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_in,
7887 pma1_space, isl_dim_in);
7888 pma2 = isl_pw_multi_aff_copy_tuple_id(pma2, isl_dim_out,
7889 pma1_space, isl_dim_in);
7891 return isl_pw_multi_aff_pullback_pw_multi_aff(pma1, pma2);
7892 error:
7893 isl_pw_multi_aff_free(pma1);
7894 isl_pw_multi_aff_free(pma2);
7895 return NULL;
7898 /* If data->pma and "pma2" are such that
7899 * data->pma is of the form A[B -> C] -> D and
7900 * "pma2" is of the form E -> B,
7901 * then replace the domain of the wrapped relation
7902 * inside the domain of data->pma by the preimage with respect to "pma2" and
7903 * add the result to data->res.
7905 static isl_stat preimage_domain_wrapped_domain_entry(
7906 __isl_take isl_pw_multi_aff *pma2, void *user)
7908 struct isl_union_pw_multi_aff_bin_data *data = user;
7909 isl_space *pma1_space, *pma2_space;
7910 isl_bool match;
7912 pma1_space = isl_pw_multi_aff_peek_space(data->pma);
7913 pma2_space = isl_pw_multi_aff_peek_space(pma2);
7915 match = isl_space_domain_is_wrapping(pma1_space);
7916 if (match >= 0 && match)
7917 match = isl_space_wrapped_tuple_is_equal(pma1_space, isl_dim_in,
7918 isl_dim_in, pma2_space, isl_dim_out);
7919 if (match < 0 || !match) {
7920 isl_pw_multi_aff_free(pma2);
7921 return match < 0 ? isl_stat_error : isl_stat_ok;
7924 pma2 = isl_pw_multi_aff_preimage_domain_wrapped_domain_pw_multi_aff(
7925 isl_pw_multi_aff_copy(data->pma), pma2);
7927 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7929 return isl_stat_non_null(data->res);
7932 /* For each pair of functions A[B -> C] -> D in "upma1" and
7933 * E -> B in "upma2",
7934 * replace the domain of the wrapped relation inside the domain of the first
7935 * by the preimage with respect to the second and collect the results.
7936 * In other words, plug in the second function in this nested domain.
7937 * The results are of the form A[E -> C] -> D.
7939 __isl_give isl_union_pw_multi_aff *
7940 isl_union_pw_multi_aff_preimage_domain_wrapped_domain_union_pw_multi_aff(
7941 __isl_take isl_union_pw_multi_aff *upma1,
7942 __isl_take isl_union_pw_multi_aff *upma2)
7944 return bin_op(upma1, upma2, &preimage_domain_wrapped_domain_entry);
7947 /* Check that the domain space of "upa" matches "space".
7949 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7950 * can in principle never fail since the space "space" is that
7951 * of the isl_multi_union_pw_aff and is a set space such that
7952 * there is no domain space to match.
7954 * We check the parameters and double-check that "space" is
7955 * indeed that of a set.
7957 static isl_stat isl_union_pw_aff_check_match_domain_space(
7958 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7960 isl_space *upa_space;
7961 isl_bool match;
7963 if (!upa || !space)
7964 return isl_stat_error;
7966 match = isl_space_is_set(space);
7967 if (match < 0)
7968 return isl_stat_error;
7969 if (!match)
7970 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7971 "expecting set space", return isl_stat_error);
7973 upa_space = isl_union_pw_aff_get_space(upa);
7974 match = isl_space_has_equal_params(space, upa_space);
7975 if (match < 0)
7976 goto error;
7977 if (!match)
7978 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7979 "parameters don't match", goto error);
7981 isl_space_free(upa_space);
7982 return isl_stat_ok;
7983 error:
7984 isl_space_free(upa_space);
7985 return isl_stat_error;
7988 /* Do the parameters of "upa" match those of "space"?
7990 static isl_bool isl_union_pw_aff_matching_params(
7991 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7993 isl_space *upa_space;
7994 isl_bool match;
7996 if (!upa || !space)
7997 return isl_bool_error;
7999 upa_space = isl_union_pw_aff_get_space(upa);
8001 match = isl_space_has_equal_params(space, upa_space);
8003 isl_space_free(upa_space);
8004 return match;
8007 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
8008 * space represents the new parameters.
8009 * res collects the results.
8011 struct isl_union_pw_aff_reset_params_data {
8012 isl_space *space;
8013 isl_union_pw_aff *res;
8016 /* Replace the parameters of "pa" by data->space and
8017 * add the result to data->res.
8019 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
8021 struct isl_union_pw_aff_reset_params_data *data = user;
8022 isl_space *space;
8024 space = isl_pw_aff_get_space(pa);
8025 space = isl_space_replace_params(space, data->space);
8026 pa = isl_pw_aff_reset_space(pa, space);
8027 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8029 return data->res ? isl_stat_ok : isl_stat_error;
8032 /* Replace the domain space of "upa" by "space".
8033 * Since a union expression does not have a (single) domain space,
8034 * "space" is necessarily a parameter space.
8036 * Since the order and the names of the parameters determine
8037 * the hash value, we need to create a new hash table.
8039 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
8040 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
8042 struct isl_union_pw_aff_reset_params_data data = { space };
8043 isl_bool match;
8045 match = isl_union_pw_aff_matching_params(upa, space);
8046 if (match < 0)
8047 upa = isl_union_pw_aff_free(upa);
8048 else if (match) {
8049 isl_space_free(space);
8050 return upa;
8053 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
8054 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
8055 data.res = isl_union_pw_aff_free(data.res);
8057 isl_union_pw_aff_free(upa);
8058 isl_space_free(space);
8059 return data.res;
8062 /* Return the floor of "pa".
8064 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
8066 return isl_pw_aff_floor(pa);
8069 /* Given f, return floor(f).
8071 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
8072 __isl_take isl_union_pw_aff *upa)
8074 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
8077 /* Compute
8079 * upa mod m = upa - m * floor(upa/m)
8081 * with m an integer value.
8083 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
8084 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
8086 isl_union_pw_aff *res;
8088 if (!upa || !m)
8089 goto error;
8091 if (!isl_val_is_int(m))
8092 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8093 "expecting integer modulo", goto error);
8094 if (!isl_val_is_pos(m))
8095 isl_die(isl_val_get_ctx(m), isl_error_invalid,
8096 "expecting positive modulo", goto error);
8098 res = isl_union_pw_aff_copy(upa);
8099 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
8100 upa = isl_union_pw_aff_floor(upa);
8101 upa = isl_union_pw_aff_scale_val(upa, m);
8102 res = isl_union_pw_aff_sub(res, upa);
8104 return res;
8105 error:
8106 isl_val_free(m);
8107 isl_union_pw_aff_free(upa);
8108 return NULL;
8111 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
8112 * pos is the output position that needs to be extracted.
8113 * res collects the results.
8115 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
8116 int pos;
8117 isl_union_pw_aff *res;
8120 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
8121 * (assuming it has such a dimension) and add it to data->res.
8123 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
8125 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
8126 isl_size n_out;
8127 isl_pw_aff *pa;
8129 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8130 if (n_out < 0)
8131 return isl_stat_error;
8132 if (data->pos >= n_out) {
8133 isl_pw_multi_aff_free(pma);
8134 return isl_stat_ok;
8137 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
8138 isl_pw_multi_aff_free(pma);
8140 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8142 return data->res ? isl_stat_ok : isl_stat_error;
8145 /* Extract an isl_union_pw_aff corresponding to
8146 * output dimension "pos" of "upma".
8148 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
8149 __isl_keep isl_union_pw_multi_aff *upma, int pos)
8151 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
8152 isl_space *space;
8154 if (!upma)
8155 return NULL;
8157 if (pos < 0)
8158 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8159 "cannot extract at negative position", return NULL);
8161 space = isl_union_pw_multi_aff_get_space(upma);
8162 data.res = isl_union_pw_aff_empty(space);
8163 data.pos = pos;
8164 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8165 &get_union_pw_aff, &data) < 0)
8166 data.res = isl_union_pw_aff_free(data.res);
8168 return data.res;
8171 /* Return a union piecewise affine expression
8172 * that is equal to "aff" on "domain".
8174 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
8175 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
8177 isl_pw_aff *pa;
8179 pa = isl_pw_aff_from_aff(aff);
8180 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
8183 /* Return a union piecewise affine expression
8184 * that is equal to the parameter identified by "id" on "domain".
8186 * Make sure the parameter appears in the space passed to
8187 * isl_aff_param_on_domain_space_id.
8189 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
8190 __isl_take isl_union_set *domain, __isl_take isl_id *id)
8192 isl_space *space;
8193 isl_aff *aff;
8195 space = isl_union_set_get_space(domain);
8196 space = isl_space_add_param_id(space, isl_id_copy(id));
8197 aff = isl_aff_param_on_domain_space_id(space, id);
8198 return isl_union_pw_aff_aff_on_domain(domain, aff);
8201 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
8202 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
8203 * needs to attain.
8204 * "res" collects the results.
8206 struct isl_union_pw_aff_pw_aff_on_domain_data {
8207 isl_pw_aff *pa;
8208 isl_union_pw_aff *res;
8211 /* Construct a piecewise affine expression that is equal to data->pa
8212 * on "domain" and add the result to data->res.
8214 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
8216 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
8217 isl_pw_aff *pa;
8218 isl_size dim;
8220 pa = isl_pw_aff_copy(data->pa);
8221 dim = isl_set_dim(domain, isl_dim_set);
8222 if (dim < 0)
8223 pa = isl_pw_aff_free(pa);
8224 pa = isl_pw_aff_from_range(pa);
8225 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
8226 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
8227 pa = isl_pw_aff_intersect_domain(pa, domain);
8228 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8230 return data->res ? isl_stat_ok : isl_stat_error;
8233 /* Return a union piecewise affine expression
8234 * that is equal to "pa" on "domain", assuming "domain" and "pa"
8235 * have been aligned.
8237 * Construct an isl_pw_aff on each of the sets in "domain" and
8238 * collect the results.
8240 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
8241 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8243 struct isl_union_pw_aff_pw_aff_on_domain_data data;
8244 isl_space *space;
8246 space = isl_union_set_get_space(domain);
8247 data.res = isl_union_pw_aff_empty(space);
8248 data.pa = pa;
8249 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
8250 data.res = isl_union_pw_aff_free(data.res);
8251 isl_union_set_free(domain);
8252 isl_pw_aff_free(pa);
8253 return data.res;
8256 /* Return a union piecewise affine expression
8257 * that is equal to "pa" on "domain".
8259 * Check that "pa" is a parametric expression,
8260 * align the parameters if needed and call
8261 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8263 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8264 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8266 isl_bool is_set;
8267 isl_bool equal_params;
8268 isl_space *domain_space, *pa_space;
8270 pa_space = isl_pw_aff_peek_space(pa);
8271 is_set = isl_space_is_set(pa_space);
8272 if (is_set < 0)
8273 goto error;
8274 if (!is_set)
8275 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8276 "expecting parametric expression", goto error);
8278 domain_space = isl_union_set_get_space(domain);
8279 pa_space = isl_pw_aff_get_space(pa);
8280 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8281 if (equal_params >= 0 && !equal_params) {
8282 isl_space *space;
8284 space = isl_space_align_params(domain_space, pa_space);
8285 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8286 domain = isl_union_set_align_params(domain, space);
8287 } else {
8288 isl_space_free(domain_space);
8289 isl_space_free(pa_space);
8292 if (equal_params < 0)
8293 goto error;
8294 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8295 error:
8296 isl_union_set_free(domain);
8297 isl_pw_aff_free(pa);
8298 return NULL;
8301 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8302 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8303 * "res" collects the results.
8305 struct isl_union_pw_aff_val_on_domain_data {
8306 isl_val *v;
8307 isl_union_pw_aff *res;
8310 /* Construct a piecewise affine expression that is equal to data->v
8311 * on "domain" and add the result to data->res.
8313 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8315 struct isl_union_pw_aff_val_on_domain_data *data = user;
8316 isl_pw_aff *pa;
8317 isl_val *v;
8319 v = isl_val_copy(data->v);
8320 pa = isl_pw_aff_val_on_domain(domain, v);
8321 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8323 return data->res ? isl_stat_ok : isl_stat_error;
8326 /* Return a union piecewise affine expression
8327 * that is equal to "v" on "domain".
8329 * Construct an isl_pw_aff on each of the sets in "domain" and
8330 * collect the results.
8332 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8333 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8335 struct isl_union_pw_aff_val_on_domain_data data;
8336 isl_space *space;
8338 space = isl_union_set_get_space(domain);
8339 data.res = isl_union_pw_aff_empty(space);
8340 data.v = v;
8341 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8342 data.res = isl_union_pw_aff_free(data.res);
8343 isl_union_set_free(domain);
8344 isl_val_free(v);
8345 return data.res;
8348 /* Construct a piecewise multi affine expression
8349 * that is equal to "pa" and add it to upma.
8351 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8352 void *user)
8354 isl_union_pw_multi_aff **upma = user;
8355 isl_pw_multi_aff *pma;
8357 pma = isl_pw_multi_aff_from_pw_aff(pa);
8358 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8360 return *upma ? isl_stat_ok : isl_stat_error;
8363 /* Construct and return a union piecewise multi affine expression
8364 * that is equal to the given union piecewise affine expression.
8366 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8367 __isl_take isl_union_pw_aff *upa)
8369 isl_space *space;
8370 isl_union_pw_multi_aff *upma;
8372 if (!upa)
8373 return NULL;
8375 space = isl_union_pw_aff_get_space(upa);
8376 upma = isl_union_pw_multi_aff_empty(space);
8378 if (isl_union_pw_aff_foreach_pw_aff(upa,
8379 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8380 upma = isl_union_pw_multi_aff_free(upma);
8382 isl_union_pw_aff_free(upa);
8383 return upma;
8386 /* Compute the set of elements in the domain of "pa" where it is zero and
8387 * add this set to "uset".
8389 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8391 isl_union_set **uset = (isl_union_set **)user;
8393 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8395 return *uset ? isl_stat_ok : isl_stat_error;
8398 /* Return a union set containing those elements in the domain
8399 * of "upa" where it is zero.
8401 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8402 __isl_take isl_union_pw_aff *upa)
8404 isl_union_set *zero;
8406 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8407 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8408 zero = isl_union_set_free(zero);
8410 isl_union_pw_aff_free(upa);
8411 return zero;
8414 /* Internal data structure for isl_union_pw_aff_bind_id,
8415 * storing the parameter that needs to be bound and
8416 * the accumulated results.
8418 struct isl_bind_id_data {
8419 isl_id *id;
8420 isl_union_set *bound;
8423 /* Bind the piecewise affine function "pa" to the parameter data->id,
8424 * adding the resulting elements in the domain where the expression
8425 * is equal to the parameter to data->bound.
8427 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8429 struct isl_bind_id_data *data = user;
8430 isl_set *bound;
8432 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8433 data->bound = isl_union_set_add_set(data->bound, bound);
8435 return data->bound ? isl_stat_ok : isl_stat_error;
8438 /* Bind the union piecewise affine function "upa" to the parameter "id",
8439 * returning the elements in the domain where the expression
8440 * is equal to the parameter.
8442 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8443 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8445 struct isl_bind_id_data data = { id };
8447 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8448 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8449 data.bound = isl_union_set_free(data.bound);
8451 isl_union_pw_aff_free(upa);
8452 isl_id_free(id);
8453 return data.bound;
8456 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8457 * upma is the function that is plugged in.
8458 * pa is the current part of the function in which upma is plugged in.
8459 * res collects the results.
8461 struct isl_union_pw_aff_pullback_upma_data {
8462 isl_union_pw_multi_aff *upma;
8463 isl_pw_aff *pa;
8464 isl_union_pw_aff *res;
8467 /* Check if "pma" can be plugged into data->pa.
8468 * If so, perform the pullback and add the result to data->res.
8470 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8472 struct isl_union_pw_aff_pullback_upma_data *data = user;
8473 isl_pw_aff *pa;
8475 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8476 pma->dim, isl_dim_out)) {
8477 isl_pw_multi_aff_free(pma);
8478 return isl_stat_ok;
8481 pa = isl_pw_aff_copy(data->pa);
8482 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8484 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8486 return data->res ? isl_stat_ok : isl_stat_error;
8489 /* Check if any of the elements of data->upma can be plugged into pa,
8490 * add if so add the result to data->res.
8492 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8494 struct isl_union_pw_aff_pullback_upma_data *data = user;
8495 isl_stat r;
8497 data->pa = pa;
8498 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8499 &pa_pb_pma, data);
8500 isl_pw_aff_free(pa);
8502 return r;
8505 /* Compute the pullback of "upa" by the function represented by "upma".
8506 * In other words, plug in "upma" in "upa". The result contains
8507 * expressions defined over the domain space of "upma".
8509 * Run over all pairs of elements in "upa" and "upma", perform
8510 * the pullback when appropriate and collect the results.
8511 * If the hash value were based on the domain space rather than
8512 * the function space, then we could run through all elements
8513 * of "upma" and directly pick out the corresponding element of "upa".
8515 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8516 __isl_take isl_union_pw_aff *upa,
8517 __isl_take isl_union_pw_multi_aff *upma)
8519 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8520 isl_space *space;
8522 space = isl_union_pw_multi_aff_get_space(upma);
8523 upa = isl_union_pw_aff_align_params(upa, space);
8524 space = isl_union_pw_aff_get_space(upa);
8525 upma = isl_union_pw_multi_aff_align_params(upma, space);
8527 if (!upa || !upma)
8528 goto error;
8530 data.upma = upma;
8531 data.res = isl_union_pw_aff_alloc_same_size(upa);
8532 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8533 data.res = isl_union_pw_aff_free(data.res);
8535 isl_union_pw_aff_free(upa);
8536 isl_union_pw_multi_aff_free(upma);
8537 return data.res;
8538 error:
8539 isl_union_pw_aff_free(upa);
8540 isl_union_pw_multi_aff_free(upma);
8541 return NULL;
8544 #undef BASE
8545 #define BASE union_pw_aff
8546 #undef DOMBASE
8547 #define DOMBASE union_set
8549 #include <isl_multi_explicit_domain.c>
8550 #include <isl_multi_union_pw_aff_explicit_domain.c>
8551 #include <isl_multi_templ.c>
8552 #include <isl_multi_un_op_templ.c>
8553 #include <isl_multi_bin_val_templ.c>
8554 #include <isl_multi_apply_set.c>
8555 #include <isl_multi_apply_union_set.c>
8556 #include <isl_multi_arith_templ.c>
8557 #include <isl_multi_bind_templ.c>
8558 #include <isl_multi_coalesce.c>
8559 #include <isl_multi_dim_id_templ.c>
8560 #include <isl_multi_floor.c>
8561 #include <isl_multi_from_base_templ.c>
8562 #include <isl_multi_gist.c>
8563 #include <isl_multi_align_set.c>
8564 #include <isl_multi_align_union_set.c>
8565 #include <isl_multi_intersect.c>
8566 #include <isl_multi_nan_templ.c>
8567 #include <isl_multi_tuple_id_templ.c>
8568 #include <isl_multi_union_add_templ.c>
8569 #include <isl_multi_zero_space_templ.c>
8571 /* Does "mupa" have a non-trivial explicit domain?
8573 * The explicit domain, if present, is trivial if it represents
8574 * an (obviously) universe parameter set.
8576 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8577 __isl_keep isl_multi_union_pw_aff *mupa)
8579 isl_bool is_params, trivial;
8580 isl_set *set;
8582 if (!mupa)
8583 return isl_bool_error;
8584 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8585 return isl_bool_false;
8586 is_params = isl_union_set_is_params(mupa->u.dom);
8587 if (is_params < 0 || !is_params)
8588 return isl_bool_not(is_params);
8589 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8590 trivial = isl_set_plain_is_universe(set);
8591 isl_set_free(set);
8592 return isl_bool_not(trivial);
8595 /* Construct a multiple union piecewise affine expression
8596 * in the given space with value zero in each of the output dimensions.
8598 * Since there is no canonical zero value for
8599 * a union piecewise affine expression, we can only construct
8600 * a zero-dimensional "zero" value.
8602 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8603 __isl_take isl_space *space)
8605 isl_bool params;
8606 isl_size dim;
8608 if (!space)
8609 return NULL;
8611 params = isl_space_is_params(space);
8612 if (params < 0)
8613 goto error;
8614 if (params)
8615 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8616 "expecting proper set space", goto error);
8617 if (!isl_space_is_set(space))
8618 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8619 "expecting set space", goto error);
8620 dim = isl_space_dim(space, isl_dim_out);
8621 if (dim < 0)
8622 goto error;
8623 if (dim != 0)
8624 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8625 "expecting 0D space", goto error);
8627 return isl_multi_union_pw_aff_alloc(space);
8628 error:
8629 isl_space_free(space);
8630 return NULL;
8633 /* Construct and return a multi union piecewise affine expression
8634 * that is equal to the given multi affine expression.
8636 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8637 __isl_take isl_multi_aff *ma)
8639 isl_multi_pw_aff *mpa;
8641 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8642 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8645 /* This function performs the same operation as
8646 * isl_multi_union_pw_aff_from_multi_aff, but is considered as a function on an
8647 * isl_multi_aff when exported.
8649 __isl_give isl_multi_union_pw_aff *isl_multi_aff_to_multi_union_pw_aff(
8650 __isl_take isl_multi_aff *ma)
8652 return isl_multi_union_pw_aff_from_multi_aff(ma);
8655 /* Construct and return a multi union piecewise affine expression
8656 * that is equal to the given multi piecewise affine expression.
8658 * If the resulting multi union piecewise affine expression has
8659 * an explicit domain, then assign it the domain of the input.
8660 * In other cases, the domain is stored in the individual elements.
8662 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8663 __isl_take isl_multi_pw_aff *mpa)
8665 int i;
8666 isl_size n;
8667 isl_space *space;
8668 isl_multi_union_pw_aff *mupa;
8670 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8671 if (n < 0)
8672 mpa = isl_multi_pw_aff_free(mpa);
8673 if (!mpa)
8674 return NULL;
8676 space = isl_multi_pw_aff_get_space(mpa);
8677 space = isl_space_range(space);
8678 mupa = isl_multi_union_pw_aff_alloc(space);
8680 for (i = 0; i < n; ++i) {
8681 isl_pw_aff *pa;
8682 isl_union_pw_aff *upa;
8684 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8685 upa = isl_union_pw_aff_from_pw_aff(pa);
8686 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8688 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8689 isl_union_set *dom;
8690 isl_multi_pw_aff *copy;
8692 copy = isl_multi_pw_aff_copy(mpa);
8693 dom = isl_union_set_from_set(isl_multi_pw_aff_domain(copy));
8694 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8697 isl_multi_pw_aff_free(mpa);
8699 return mupa;
8702 /* Extract the range space of "pma" and assign it to *space.
8703 * If *space has already been set (through a previous call to this function),
8704 * then check that the range space is the same.
8706 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8708 isl_space **space = user;
8709 isl_space *pma_space;
8710 isl_bool equal;
8712 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8713 isl_pw_multi_aff_free(pma);
8715 if (!pma_space)
8716 return isl_stat_error;
8717 if (!*space) {
8718 *space = pma_space;
8719 return isl_stat_ok;
8722 equal = isl_space_is_equal(pma_space, *space);
8723 isl_space_free(pma_space);
8725 if (equal < 0)
8726 return isl_stat_error;
8727 if (!equal)
8728 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8729 "range spaces not the same", return isl_stat_error);
8730 return isl_stat_ok;
8733 /* Construct and return a multi union piecewise affine expression
8734 * that is equal to the given union piecewise multi affine expression.
8736 * In order to be able to perform the conversion, the input
8737 * needs to be non-empty and may only involve a single range space.
8739 * If the resulting multi union piecewise affine expression has
8740 * an explicit domain, then assign it the domain of the input.
8741 * In other cases, the domain is stored in the individual elements.
8743 __isl_give isl_multi_union_pw_aff *
8744 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8745 __isl_take isl_union_pw_multi_aff *upma)
8747 isl_space *space = NULL;
8748 isl_multi_union_pw_aff *mupa;
8749 int i;
8750 isl_size n;
8752 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8753 if (n < 0)
8754 goto error;
8755 if (n == 0)
8756 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8757 "cannot extract range space from empty input",
8758 goto error);
8759 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8760 &space) < 0)
8761 goto error;
8763 if (!space)
8764 goto error;
8766 n = isl_space_dim(space, isl_dim_set);
8767 if (n < 0)
8768 space = isl_space_free(space);
8769 mupa = isl_multi_union_pw_aff_alloc(space);
8771 for (i = 0; i < n; ++i) {
8772 isl_union_pw_aff *upa;
8774 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8775 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8777 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8778 isl_union_set *dom;
8779 isl_union_pw_multi_aff *copy;
8781 copy = isl_union_pw_multi_aff_copy(upma);
8782 dom = isl_union_pw_multi_aff_domain(copy);
8783 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8786 isl_union_pw_multi_aff_free(upma);
8787 return mupa;
8788 error:
8789 isl_space_free(space);
8790 isl_union_pw_multi_aff_free(upma);
8791 return NULL;
8794 /* This function performs the same operation as
8795 * isl_multi_union_pw_aff_from_union_pw_multi_aff,
8796 * but is considered as a function on an isl_union_pw_multi_aff when exported.
8798 __isl_give isl_multi_union_pw_aff *
8799 isl_union_pw_multi_aff_as_multi_union_pw_aff(
8800 __isl_take isl_union_pw_multi_aff *upma)
8802 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8805 /* Try and create an isl_multi_union_pw_aff that is equivalent
8806 * to the given isl_union_map.
8807 * The isl_union_map is required to be single-valued in each space.
8808 * Moreover, it cannot be empty and all range spaces need to be the same.
8809 * Otherwise, an error is produced.
8811 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8812 __isl_take isl_union_map *umap)
8814 isl_union_pw_multi_aff *upma;
8816 upma = isl_union_pw_multi_aff_from_union_map(umap);
8817 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8820 /* This function performs the same operation as
8821 * isl_multi_union_pw_aff_from_union_map,
8822 * but is considered as a function on an isl_union_map when exported.
8824 __isl_give isl_multi_union_pw_aff *isl_union_map_as_multi_union_pw_aff(
8825 __isl_take isl_union_map *umap)
8827 return isl_multi_union_pw_aff_from_union_map(umap);
8830 /* Return a multiple union piecewise affine expression
8831 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8832 * have been aligned.
8834 * If the resulting multi union piecewise affine expression has
8835 * an explicit domain, then assign it the input domain.
8836 * In other cases, the domain is stored in the individual elements.
8838 static __isl_give isl_multi_union_pw_aff *
8839 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8840 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8842 int i;
8843 isl_size n;
8844 isl_space *space;
8845 isl_multi_union_pw_aff *mupa;
8847 n = isl_multi_val_dim(mv, isl_dim_set);
8848 if (!domain || n < 0)
8849 goto error;
8851 space = isl_multi_val_get_space(mv);
8852 mupa = isl_multi_union_pw_aff_alloc(space);
8853 for (i = 0; i < n; ++i) {
8854 isl_val *v;
8855 isl_union_pw_aff *upa;
8857 v = isl_multi_val_get_val(mv, i);
8858 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8860 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8862 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8863 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8864 isl_union_set_copy(domain));
8866 isl_union_set_free(domain);
8867 isl_multi_val_free(mv);
8868 return mupa;
8869 error:
8870 isl_union_set_free(domain);
8871 isl_multi_val_free(mv);
8872 return NULL;
8875 /* Return a multiple union piecewise affine expression
8876 * that is equal to "mv" on "domain".
8878 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8879 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8881 isl_bool equal_params;
8883 if (!domain || !mv)
8884 goto error;
8885 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8886 if (equal_params < 0)
8887 goto error;
8888 if (equal_params)
8889 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8890 domain, mv);
8891 domain = isl_union_set_align_params(domain,
8892 isl_multi_val_get_space(mv));
8893 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8894 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8895 error:
8896 isl_union_set_free(domain);
8897 isl_multi_val_free(mv);
8898 return NULL;
8901 /* Return a multiple union piecewise affine expression
8902 * that is equal to "ma" on "domain".
8904 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8905 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8907 isl_pw_multi_aff *pma;
8909 pma = isl_pw_multi_aff_from_multi_aff(ma);
8910 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8913 /* Return a multiple union piecewise affine expression
8914 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8915 * have been aligned.
8917 * If the resulting multi union piecewise affine expression has
8918 * an explicit domain, then assign it the input domain.
8919 * In other cases, the domain is stored in the individual elements.
8921 static __isl_give isl_multi_union_pw_aff *
8922 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8923 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8925 int i;
8926 isl_size n;
8927 isl_space *space;
8928 isl_multi_union_pw_aff *mupa;
8930 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8931 if (!domain || n < 0)
8932 goto error;
8933 space = isl_pw_multi_aff_get_space(pma);
8934 mupa = isl_multi_union_pw_aff_alloc(space);
8935 for (i = 0; i < n; ++i) {
8936 isl_pw_aff *pa;
8937 isl_union_pw_aff *upa;
8939 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8940 upa = isl_union_pw_aff_pw_aff_on_domain(
8941 isl_union_set_copy(domain), pa);
8942 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8944 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8945 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8946 isl_union_set_copy(domain));
8948 isl_union_set_free(domain);
8949 isl_pw_multi_aff_free(pma);
8950 return mupa;
8951 error:
8952 isl_union_set_free(domain);
8953 isl_pw_multi_aff_free(pma);
8954 return NULL;
8957 /* Return a multiple union piecewise affine expression
8958 * that is equal to "pma" on "domain".
8960 __isl_give isl_multi_union_pw_aff *
8961 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8962 __isl_take isl_pw_multi_aff *pma)
8964 isl_bool equal_params;
8965 isl_space *space;
8967 space = isl_pw_multi_aff_peek_space(pma);
8968 equal_params = isl_union_set_space_has_equal_params(domain, space);
8969 if (equal_params < 0)
8970 goto error;
8971 if (equal_params)
8972 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8973 domain, pma);
8974 domain = isl_union_set_align_params(domain,
8975 isl_pw_multi_aff_get_space(pma));
8976 pma = isl_pw_multi_aff_align_params(pma,
8977 isl_union_set_get_space(domain));
8978 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8979 pma);
8980 error:
8981 isl_union_set_free(domain);
8982 isl_pw_multi_aff_free(pma);
8983 return NULL;
8986 /* Return a union set containing those elements in the domains
8987 * of the elements of "mupa" where they are all zero.
8989 * If there are no elements, then simply return the entire domain.
8991 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8992 __isl_take isl_multi_union_pw_aff *mupa)
8994 int i;
8995 isl_size n;
8996 isl_union_pw_aff *upa;
8997 isl_union_set *zero;
8999 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9000 if (n < 0)
9001 mupa = isl_multi_union_pw_aff_free(mupa);
9002 if (!mupa)
9003 return NULL;
9005 if (n == 0)
9006 return isl_multi_union_pw_aff_domain(mupa);
9008 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9009 zero = isl_union_pw_aff_zero_union_set(upa);
9011 for (i = 1; i < n; ++i) {
9012 isl_union_set *zero_i;
9014 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9015 zero_i = isl_union_pw_aff_zero_union_set(upa);
9017 zero = isl_union_set_intersect(zero, zero_i);
9020 isl_multi_union_pw_aff_free(mupa);
9021 return zero;
9024 /* Construct a union map mapping the shared domain
9025 * of the union piecewise affine expressions to the range of "mupa"
9026 * in the special case of a 0D multi union piecewise affine expression.
9028 * Construct a map between the explicit domain of "mupa" and
9029 * the range space.
9030 * Note that this assumes that the domain consists of explicit elements.
9032 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
9033 __isl_take isl_multi_union_pw_aff *mupa)
9035 isl_bool is_params;
9036 isl_space *space;
9037 isl_union_set *dom, *ran;
9039 space = isl_multi_union_pw_aff_get_space(mupa);
9040 dom = isl_multi_union_pw_aff_domain(mupa);
9041 ran = isl_union_set_from_set(isl_set_universe(space));
9043 is_params = isl_union_set_is_params(dom);
9044 if (is_params < 0)
9045 dom = isl_union_set_free(dom);
9046 else if (is_params)
9047 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
9048 "cannot create union map from expression without "
9049 "explicit domain elements",
9050 dom = isl_union_set_free(dom));
9052 return isl_union_map_from_domain_and_range(dom, ran);
9055 /* Construct a union map mapping the shared domain
9056 * of the union piecewise affine expressions to the range of "mupa"
9057 * with each dimension in the range equated to the
9058 * corresponding union piecewise affine expression.
9060 * If the input is zero-dimensional, then construct a mapping
9061 * from its explicit domain.
9063 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
9064 __isl_take isl_multi_union_pw_aff *mupa)
9066 int i;
9067 isl_size n;
9068 isl_space *space;
9069 isl_union_map *umap;
9070 isl_union_pw_aff *upa;
9072 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9073 if (n < 0)
9074 mupa = isl_multi_union_pw_aff_free(mupa);
9075 if (!mupa)
9076 return NULL;
9078 if (n == 0)
9079 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
9081 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9082 umap = isl_union_map_from_union_pw_aff(upa);
9084 for (i = 1; i < n; ++i) {
9085 isl_union_map *umap_i;
9087 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9088 umap_i = isl_union_map_from_union_pw_aff(upa);
9089 umap = isl_union_map_flat_range_product(umap, umap_i);
9092 space = isl_multi_union_pw_aff_get_space(mupa);
9093 umap = isl_union_map_reset_range_space(umap, space);
9095 isl_multi_union_pw_aff_free(mupa);
9096 return umap;
9099 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
9100 * "range" is the space from which to set the range space.
9101 * "res" collects the results.
9103 struct isl_union_pw_multi_aff_reset_range_space_data {
9104 isl_space *range;
9105 isl_union_pw_multi_aff *res;
9108 /* Replace the range space of "pma" by the range space of data->range and
9109 * add the result to data->res.
9111 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
9113 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
9114 isl_space *space;
9116 space = isl_pw_multi_aff_get_space(pma);
9117 space = isl_space_domain(space);
9118 space = isl_space_extend_domain_with_range(space,
9119 isl_space_copy(data->range));
9120 pma = isl_pw_multi_aff_reset_space(pma, space);
9121 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
9123 return data->res ? isl_stat_ok : isl_stat_error;
9126 /* Replace the range space of all the piecewise affine expressions in "upma" by
9127 * the range space of "space".
9129 * This assumes that all these expressions have the same output dimension.
9131 * Since the spaces of the expressions change, so do their hash values.
9132 * We therefore need to create a new isl_union_pw_multi_aff.
9133 * Note that the hash value is currently computed based on the entire
9134 * space even though there can only be a single expression with a given
9135 * domain space.
9137 static __isl_give isl_union_pw_multi_aff *
9138 isl_union_pw_multi_aff_reset_range_space(
9139 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
9141 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
9142 isl_space *space_upma;
9144 space_upma = isl_union_pw_multi_aff_get_space(upma);
9145 data.res = isl_union_pw_multi_aff_empty(space_upma);
9146 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
9147 &reset_range_space, &data) < 0)
9148 data.res = isl_union_pw_multi_aff_free(data.res);
9150 isl_space_free(space);
9151 isl_union_pw_multi_aff_free(upma);
9152 return data.res;
9155 /* Construct and return a union piecewise multi affine expression
9156 * that is equal to the given multi union piecewise affine expression,
9157 * in the special case of a 0D multi union piecewise affine expression.
9159 * Construct a union piecewise multi affine expression
9160 * on top of the explicit domain of the input.
9162 __isl_give isl_union_pw_multi_aff *
9163 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
9164 __isl_take isl_multi_union_pw_aff *mupa)
9166 isl_space *space;
9167 isl_multi_val *mv;
9168 isl_union_set *domain;
9170 space = isl_multi_union_pw_aff_get_space(mupa);
9171 mv = isl_multi_val_zero(space);
9172 domain = isl_multi_union_pw_aff_domain(mupa);
9173 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
9176 /* Construct and return a union piecewise multi affine expression
9177 * that is equal to the given multi union piecewise affine expression.
9179 * If the input is zero-dimensional, then
9180 * construct a union piecewise multi affine expression
9181 * on top of the explicit domain of the input.
9183 __isl_give isl_union_pw_multi_aff *
9184 isl_union_pw_multi_aff_from_multi_union_pw_aff(
9185 __isl_take isl_multi_union_pw_aff *mupa)
9187 int i;
9188 isl_size n;
9189 isl_space *space;
9190 isl_union_pw_multi_aff *upma;
9191 isl_union_pw_aff *upa;
9193 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9194 if (n < 0)
9195 mupa = isl_multi_union_pw_aff_free(mupa);
9196 if (!mupa)
9197 return NULL;
9199 if (n == 0)
9200 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
9202 space = isl_multi_union_pw_aff_get_space(mupa);
9203 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9204 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9206 for (i = 1; i < n; ++i) {
9207 isl_union_pw_multi_aff *upma_i;
9209 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9210 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
9211 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
9214 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
9216 isl_multi_union_pw_aff_free(mupa);
9217 return upma;
9220 /* Intersect the range of "mupa" with "range",
9221 * in the special case where "mupa" is 0D.
9223 * Intersect the domain of "mupa" with the constraints on the parameters
9224 * of "range".
9226 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
9227 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9229 range = isl_set_params(range);
9230 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
9231 return mupa;
9234 /* Intersect the range of "mupa" with "range".
9235 * That is, keep only those domain elements that have a function value
9236 * in "range".
9238 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
9239 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
9241 isl_union_pw_multi_aff *upma;
9242 isl_union_set *domain;
9243 isl_space *space;
9244 isl_size n;
9245 int match;
9247 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9248 if (n < 0 || !range)
9249 goto error;
9251 space = isl_set_get_space(range);
9252 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
9253 space, isl_dim_set);
9254 isl_space_free(space);
9255 if (match < 0)
9256 goto error;
9257 if (!match)
9258 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
9259 "space don't match", goto error);
9260 if (n == 0)
9261 return mupa_intersect_range_0D(mupa, range);
9263 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
9264 isl_multi_union_pw_aff_copy(mupa));
9265 domain = isl_union_set_from_set(range);
9266 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
9267 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
9269 return mupa;
9270 error:
9271 isl_multi_union_pw_aff_free(mupa);
9272 isl_set_free(range);
9273 return NULL;
9276 /* Return the shared domain of the elements of "mupa",
9277 * in the special case where "mupa" is zero-dimensional.
9279 * Return the explicit domain of "mupa".
9280 * Note that this domain may be a parameter set, either
9281 * because "mupa" is meant to live in a set space or
9282 * because no explicit domain has been set.
9284 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
9285 __isl_take isl_multi_union_pw_aff *mupa)
9287 isl_union_set *dom;
9289 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
9290 isl_multi_union_pw_aff_free(mupa);
9292 return dom;
9295 /* Return the shared domain of the elements of "mupa".
9297 * If "mupa" is zero-dimensional, then return its explicit domain.
9299 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
9300 __isl_take isl_multi_union_pw_aff *mupa)
9302 int i;
9303 isl_size n;
9304 isl_union_pw_aff *upa;
9305 isl_union_set *dom;
9307 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9308 if (n < 0)
9309 mupa = isl_multi_union_pw_aff_free(mupa);
9310 if (!mupa)
9311 return NULL;
9313 if (n == 0)
9314 return isl_multi_union_pw_aff_domain_0D(mupa);
9316 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9317 dom = isl_union_pw_aff_domain(upa);
9318 for (i = 1; i < n; ++i) {
9319 isl_union_set *dom_i;
9321 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9322 dom_i = isl_union_pw_aff_domain(upa);
9323 dom = isl_union_set_intersect(dom, dom_i);
9326 isl_multi_union_pw_aff_free(mupa);
9327 return dom;
9330 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9331 * In particular, the spaces have been aligned.
9332 * The result is defined over the shared domain of the elements of "mupa"
9334 * We first extract the parametric constant part of "aff" and
9335 * define that over the shared domain.
9336 * Then we iterate over all input dimensions of "aff" and add the corresponding
9337 * multiples of the elements of "mupa".
9338 * Finally, we consider the integer divisions, calling the function
9339 * recursively to obtain an isl_union_pw_aff corresponding to the
9340 * integer division argument.
9342 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9343 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9345 int i;
9346 isl_size n_in, n_div;
9347 isl_union_pw_aff *upa;
9348 isl_union_set *uset;
9349 isl_val *v;
9350 isl_aff *cst;
9352 n_in = isl_aff_dim(aff, isl_dim_in);
9353 n_div = isl_aff_dim(aff, isl_dim_div);
9354 if (n_in < 0 || n_div < 0)
9355 goto error;
9357 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9358 cst = isl_aff_copy(aff);
9359 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9360 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9361 cst = isl_aff_project_domain_on_params(cst);
9362 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9364 for (i = 0; i < n_in; ++i) {
9365 isl_union_pw_aff *upa_i;
9367 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9368 continue;
9369 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9370 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9371 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9372 upa = isl_union_pw_aff_add(upa, upa_i);
9375 for (i = 0; i < n_div; ++i) {
9376 isl_aff *div;
9377 isl_union_pw_aff *upa_i;
9379 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9380 continue;
9381 div = isl_aff_get_div(aff, i);
9382 upa_i = multi_union_pw_aff_apply_aff(
9383 isl_multi_union_pw_aff_copy(mupa), div);
9384 upa_i = isl_union_pw_aff_floor(upa_i);
9385 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9386 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9387 upa = isl_union_pw_aff_add(upa, upa_i);
9390 isl_multi_union_pw_aff_free(mupa);
9391 isl_aff_free(aff);
9393 return upa;
9394 error:
9395 isl_multi_union_pw_aff_free(mupa);
9396 isl_aff_free(aff);
9397 return NULL;
9400 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9401 * with the domain of "aff".
9402 * Furthermore, the dimension of this space needs to be greater than zero.
9403 * The result is defined over the shared domain of the elements of "mupa"
9405 * We perform these checks and then hand over control to
9406 * multi_union_pw_aff_apply_aff.
9408 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9409 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9411 isl_size dim;
9412 isl_space *space1, *space2;
9413 isl_bool equal;
9415 mupa = isl_multi_union_pw_aff_align_params(mupa,
9416 isl_aff_get_space(aff));
9417 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9418 if (!mupa || !aff)
9419 goto error;
9421 space1 = isl_multi_union_pw_aff_get_space(mupa);
9422 space2 = isl_aff_get_domain_space(aff);
9423 equal = isl_space_is_equal(space1, space2);
9424 isl_space_free(space1);
9425 isl_space_free(space2);
9426 if (equal < 0)
9427 goto error;
9428 if (!equal)
9429 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9430 "spaces don't match", goto error);
9431 dim = isl_aff_dim(aff, isl_dim_in);
9432 if (dim < 0)
9433 goto error;
9434 if (dim == 0)
9435 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9436 "cannot determine domains", goto error);
9438 return multi_union_pw_aff_apply_aff(mupa, aff);
9439 error:
9440 isl_multi_union_pw_aff_free(mupa);
9441 isl_aff_free(aff);
9442 return NULL;
9445 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9446 * The space of "mupa" is known to be compatible with the domain of "ma".
9448 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9449 * on the domain of "mupa".
9451 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9452 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9454 isl_union_set *dom;
9456 dom = isl_multi_union_pw_aff_domain(mupa);
9457 ma = isl_multi_aff_project_domain_on_params(ma);
9459 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9462 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9463 * with the domain of "ma".
9464 * The result is defined over the shared domain of the elements of "mupa"
9466 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9467 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9469 isl_space *space1, *space2;
9470 isl_multi_union_pw_aff *res;
9471 isl_bool equal;
9472 int i;
9473 isl_size n_in, n_out;
9475 mupa = isl_multi_union_pw_aff_align_params(mupa,
9476 isl_multi_aff_get_space(ma));
9477 ma = isl_multi_aff_align_params(ma,
9478 isl_multi_union_pw_aff_get_space(mupa));
9479 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9480 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9481 if (!mupa || n_in < 0 || n_out < 0)
9482 goto error;
9484 space1 = isl_multi_union_pw_aff_get_space(mupa);
9485 space2 = isl_multi_aff_get_domain_space(ma);
9486 equal = isl_space_is_equal(space1, space2);
9487 isl_space_free(space1);
9488 isl_space_free(space2);
9489 if (equal < 0)
9490 goto error;
9491 if (!equal)
9492 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9493 "spaces don't match", goto error);
9494 if (n_in == 0)
9495 return mupa_apply_multi_aff_0D(mupa, ma);
9497 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9498 res = isl_multi_union_pw_aff_alloc(space1);
9500 for (i = 0; i < n_out; ++i) {
9501 isl_aff *aff;
9502 isl_union_pw_aff *upa;
9504 aff = isl_multi_aff_get_aff(ma, i);
9505 upa = multi_union_pw_aff_apply_aff(
9506 isl_multi_union_pw_aff_copy(mupa), aff);
9507 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9510 isl_multi_aff_free(ma);
9511 isl_multi_union_pw_aff_free(mupa);
9512 return res;
9513 error:
9514 isl_multi_union_pw_aff_free(mupa);
9515 isl_multi_aff_free(ma);
9516 return NULL;
9519 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9520 * The space of "mupa" is known to be compatible with the domain of "pa".
9522 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9523 * on the domain of "mupa".
9525 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9526 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9528 isl_union_set *dom;
9530 dom = isl_multi_union_pw_aff_domain(mupa);
9531 pa = isl_pw_aff_project_domain_on_params(pa);
9533 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9536 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9537 * with the domain of "pa".
9538 * Furthermore, the dimension of this space needs to be greater than zero.
9539 * The result is defined over the shared domain of the elements of "mupa"
9541 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9542 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9544 int i;
9545 isl_bool equal;
9546 isl_size n_in;
9547 isl_space *space, *space2;
9548 isl_union_pw_aff *upa;
9550 mupa = isl_multi_union_pw_aff_align_params(mupa,
9551 isl_pw_aff_get_space(pa));
9552 pa = isl_pw_aff_align_params(pa,
9553 isl_multi_union_pw_aff_get_space(mupa));
9554 if (!mupa || !pa)
9555 goto error;
9557 space = isl_multi_union_pw_aff_get_space(mupa);
9558 space2 = isl_pw_aff_get_domain_space(pa);
9559 equal = isl_space_is_equal(space, space2);
9560 isl_space_free(space);
9561 isl_space_free(space2);
9562 if (equal < 0)
9563 goto error;
9564 if (!equal)
9565 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9566 "spaces don't match", goto error);
9567 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9568 if (n_in < 0)
9569 goto error;
9570 if (n_in == 0)
9571 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9573 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9574 upa = isl_union_pw_aff_empty(space);
9576 for (i = 0; i < pa->n; ++i) {
9577 isl_aff *aff;
9578 isl_set *domain;
9579 isl_multi_union_pw_aff *mupa_i;
9580 isl_union_pw_aff *upa_i;
9582 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9583 domain = isl_set_copy(pa->p[i].set);
9584 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9585 aff = isl_aff_copy(pa->p[i].aff);
9586 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9587 upa = isl_union_pw_aff_union_add(upa, upa_i);
9590 isl_multi_union_pw_aff_free(mupa);
9591 isl_pw_aff_free(pa);
9592 return upa;
9593 error:
9594 isl_multi_union_pw_aff_free(mupa);
9595 isl_pw_aff_free(pa);
9596 return NULL;
9599 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9600 * The space of "mupa" is known to be compatible with the domain of "pma".
9602 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9603 * on the domain of "mupa".
9605 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9606 __isl_take isl_multi_union_pw_aff *mupa,
9607 __isl_take isl_pw_multi_aff *pma)
9609 isl_union_set *dom;
9611 dom = isl_multi_union_pw_aff_domain(mupa);
9612 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9614 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9617 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9618 * with the domain of "pma".
9619 * The result is defined over the shared domain of the elements of "mupa"
9621 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9622 __isl_take isl_multi_union_pw_aff *mupa,
9623 __isl_take isl_pw_multi_aff *pma)
9625 isl_space *space1, *space2;
9626 isl_multi_union_pw_aff *res;
9627 isl_bool equal;
9628 int i;
9629 isl_size n_in, n_out;
9631 mupa = isl_multi_union_pw_aff_align_params(mupa,
9632 isl_pw_multi_aff_get_space(pma));
9633 pma = isl_pw_multi_aff_align_params(pma,
9634 isl_multi_union_pw_aff_get_space(mupa));
9635 if (!mupa || !pma)
9636 goto error;
9638 space1 = isl_multi_union_pw_aff_get_space(mupa);
9639 space2 = isl_pw_multi_aff_get_domain_space(pma);
9640 equal = isl_space_is_equal(space1, space2);
9641 isl_space_free(space1);
9642 isl_space_free(space2);
9643 if (equal < 0)
9644 goto error;
9645 if (!equal)
9646 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9647 "spaces don't match", goto error);
9648 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9649 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9650 if (n_in < 0 || n_out < 0)
9651 goto error;
9652 if (n_in == 0)
9653 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9655 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9656 res = isl_multi_union_pw_aff_alloc(space1);
9658 for (i = 0; i < n_out; ++i) {
9659 isl_pw_aff *pa;
9660 isl_union_pw_aff *upa;
9662 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9663 upa = isl_multi_union_pw_aff_apply_pw_aff(
9664 isl_multi_union_pw_aff_copy(mupa), pa);
9665 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9668 isl_pw_multi_aff_free(pma);
9669 isl_multi_union_pw_aff_free(mupa);
9670 return res;
9671 error:
9672 isl_multi_union_pw_aff_free(mupa);
9673 isl_pw_multi_aff_free(pma);
9674 return NULL;
9677 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9678 * If the explicit domain only keeps track of constraints on the parameters,
9679 * then only update those constraints.
9681 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9682 __isl_take isl_multi_union_pw_aff *mupa,
9683 __isl_keep isl_union_pw_multi_aff *upma)
9685 isl_bool is_params;
9687 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9688 return isl_multi_union_pw_aff_free(mupa);
9690 mupa = isl_multi_union_pw_aff_cow(mupa);
9691 if (!mupa)
9692 return NULL;
9694 is_params = isl_union_set_is_params(mupa->u.dom);
9695 if (is_params < 0)
9696 return isl_multi_union_pw_aff_free(mupa);
9698 upma = isl_union_pw_multi_aff_copy(upma);
9699 if (is_params)
9700 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9701 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9702 else
9703 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9704 mupa->u.dom, upma);
9705 if (!mupa->u.dom)
9706 return isl_multi_union_pw_aff_free(mupa);
9707 return mupa;
9710 /* Compute the pullback of "mupa" by the function represented by "upma".
9711 * In other words, plug in "upma" in "mupa". The result contains
9712 * expressions defined over the domain space of "upma".
9714 * Run over all elements of "mupa" and plug in "upma" in each of them.
9716 * If "mupa" has an explicit domain, then it is this domain
9717 * that needs to undergo a pullback instead, i.e., a preimage.
9719 __isl_give isl_multi_union_pw_aff *
9720 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9721 __isl_take isl_multi_union_pw_aff *mupa,
9722 __isl_take isl_union_pw_multi_aff *upma)
9724 int i;
9725 isl_size n;
9727 mupa = isl_multi_union_pw_aff_align_params(mupa,
9728 isl_union_pw_multi_aff_get_space(upma));
9729 upma = isl_union_pw_multi_aff_align_params(upma,
9730 isl_multi_union_pw_aff_get_space(mupa));
9731 mupa = isl_multi_union_pw_aff_cow(mupa);
9732 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9733 if (n < 0 || !upma)
9734 goto error;
9736 for (i = 0; i < n; ++i) {
9737 isl_union_pw_aff *upa;
9739 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9740 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9741 isl_union_pw_multi_aff_copy(upma));
9742 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9745 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9746 mupa = preimage_explicit_domain(mupa, upma);
9748 isl_union_pw_multi_aff_free(upma);
9749 return mupa;
9750 error:
9751 isl_multi_union_pw_aff_free(mupa);
9752 isl_union_pw_multi_aff_free(upma);
9753 return NULL;
9756 /* Extract the sequence of elements in "mupa" with domain space "space"
9757 * (ignoring parameters).
9759 * For the elements of "mupa" that are not defined on the specified space,
9760 * the corresponding element in the result is empty.
9762 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9763 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9765 int i;
9766 isl_size n;
9767 isl_space *space_mpa;
9768 isl_multi_pw_aff *mpa;
9770 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9771 if (n < 0 || !space)
9772 goto error;
9774 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9775 space = isl_space_replace_params(space, space_mpa);
9776 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9777 space_mpa);
9778 mpa = isl_multi_pw_aff_alloc(space_mpa);
9780 space = isl_space_from_domain(space);
9781 space = isl_space_add_dims(space, isl_dim_out, 1);
9782 for (i = 0; i < n; ++i) {
9783 isl_union_pw_aff *upa;
9784 isl_pw_aff *pa;
9786 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9787 pa = isl_union_pw_aff_extract_pw_aff(upa,
9788 isl_space_copy(space));
9789 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9790 isl_union_pw_aff_free(upa);
9793 isl_space_free(space);
9794 return mpa;
9795 error:
9796 isl_space_free(space);
9797 return NULL;
9800 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9801 * should modify the base expressions in the input.
9803 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9804 * are taken into account.
9805 * "fn" is applied to each entry in the input.
9807 struct isl_union_pw_multi_aff_un_op_control {
9808 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9809 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9812 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9813 * a second argument) for use as an isl_union_pw_multi_aff_transform
9814 * filter function (which does take a second argument).
9815 * Simply call control->filter without the second argument.
9817 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9818 __isl_take isl_pw_multi_aff *pma, void *user)
9820 struct isl_union_pw_multi_aff_un_op_control *control = user;
9822 return control->filter(pma);
9825 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9826 * a second argument) for use as an isl_union_pw_multi_aff_transform
9827 * base function (which does take a second argument).
9828 * Simply call control->fn without the second argument.
9830 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9831 __isl_take isl_pw_multi_aff *pma, void *user)
9833 struct isl_union_pw_multi_aff_un_op_control *control = user;
9835 return control->fn(pma);
9838 /* Construct an isl_union_pw_multi_aff that is obtained by
9839 * modifying "upma" according to "control".
9841 * isl_union_pw_multi_aff_transform performs essentially
9842 * the same operation, but takes a filter and a callback function
9843 * of a different form (with an extra argument).
9844 * Call isl_union_pw_multi_aff_transform with wrappers
9845 * that remove this extra argument.
9847 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9848 __isl_take isl_union_pw_multi_aff *upma,
9849 struct isl_union_pw_multi_aff_un_op_control *control)
9851 struct isl_union_pw_multi_aff_transform_control t_control = {
9852 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9853 .filter_user = control,
9854 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9855 .fn_user = control,
9858 return isl_union_pw_multi_aff_transform(upma, &t_control);
9861 /* For each function in "upma" of the form A -> [B -> C],
9862 * extract the function A -> B and collect the results.
9864 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9865 __isl_take isl_union_pw_multi_aff *upma)
9867 struct isl_union_pw_multi_aff_un_op_control control = {
9868 .filter = &isl_pw_multi_aff_range_is_wrapping,
9869 .fn = &isl_pw_multi_aff_range_factor_domain,
9871 return isl_union_pw_multi_aff_un_op(upma, &control);
9874 /* For each function in "upma" of the form A -> [B -> C],
9875 * extract the function A -> C and collect the results.
9877 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9878 __isl_take isl_union_pw_multi_aff *upma)
9880 struct isl_union_pw_multi_aff_un_op_control control = {
9881 .filter = &isl_pw_multi_aff_range_is_wrapping,
9882 .fn = &isl_pw_multi_aff_range_factor_range,
9884 return isl_union_pw_multi_aff_un_op(upma, &control);
9887 /* Evaluate the affine function "aff" in the void point "pnt".
9888 * In particular, return the value NaN.
9890 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9891 __isl_take isl_point *pnt)
9893 isl_ctx *ctx;
9895 ctx = isl_point_get_ctx(pnt);
9896 isl_aff_free(aff);
9897 isl_point_free(pnt);
9898 return isl_val_nan(ctx);
9901 /* Evaluate the affine expression "aff"
9902 * in the coordinates (with denominator) "pnt".
9904 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9905 __isl_keep isl_vec *pnt)
9907 isl_int n, d;
9908 isl_ctx *ctx;
9909 isl_val *v;
9911 if (!aff || !pnt)
9912 return NULL;
9914 ctx = isl_vec_get_ctx(aff);
9915 isl_int_init(n);
9916 isl_int_init(d);
9917 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9918 isl_int_mul(d, aff->el[0], pnt->el[0]);
9919 v = isl_val_rat_from_isl_int(ctx, n, d);
9920 v = isl_val_normalize(v);
9921 isl_int_clear(n);
9922 isl_int_clear(d);
9924 return v;
9927 /* Check that the domain space of "aff" is equal to "space".
9929 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9930 __isl_keep isl_space *space)
9932 isl_bool ok;
9934 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9935 if (ok < 0)
9936 return isl_stat_error;
9937 if (!ok)
9938 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9939 "incompatible spaces", return isl_stat_error);
9940 return isl_stat_ok;
9943 /* Evaluate the affine function "aff" in "pnt".
9945 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9946 __isl_take isl_point *pnt)
9948 isl_bool is_void;
9949 isl_val *v;
9950 isl_local_space *ls;
9952 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9953 goto error;
9954 is_void = isl_point_is_void(pnt);
9955 if (is_void < 0)
9956 goto error;
9957 if (is_void)
9958 return eval_void(aff, pnt);
9960 ls = isl_aff_get_domain_local_space(aff);
9961 pnt = isl_local_space_lift_point(ls, pnt);
9963 v = eval(aff->v, isl_point_peek_vec(pnt));
9965 isl_aff_free(aff);
9966 isl_point_free(pnt);
9968 return v;
9969 error:
9970 isl_aff_free(aff);
9971 isl_point_free(pnt);
9972 return NULL;