isl_set_get_stride: remove local variables from local space of offset
[isl.git] / isl_aff.c
blobda28a65523e26e906cc1348832e4e511ce6e8ac5
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
25 #include <isl/id.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
53 #undef BASE
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
59 __isl_take isl_vec *v)
61 isl_aff *aff;
63 if (!ls || !v)
64 goto error;
66 aff = isl_calloc_type(v->ctx, struct isl_aff);
67 if (!aff)
68 goto error;
70 aff->ref = 1;
71 aff->ls = ls;
72 aff->v = v;
74 return aff;
75 error:
76 isl_local_space_free(ls);
77 isl_vec_free(v);
78 return NULL;
81 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
83 isl_ctx *ctx;
84 isl_vec *v;
85 unsigned total;
87 if (!ls)
88 return NULL;
90 ctx = isl_local_space_get_ctx(ls);
91 if (!isl_local_space_divs_known(ls))
92 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
93 goto error);
94 if (!isl_local_space_is_set(ls))
95 isl_die(ctx, isl_error_invalid,
96 "domain of affine expression should be a set",
97 goto error);
99 total = isl_local_space_dim(ls, isl_dim_all);
100 v = isl_vec_alloc(ctx, 1 + 1 + total);
101 return isl_aff_alloc_vec(ls, v);
102 error:
103 isl_local_space_free(ls);
104 return NULL;
107 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
109 isl_aff *aff;
111 aff = isl_aff_alloc(ls);
112 if (!aff)
113 return NULL;
115 isl_int_set_si(aff->v->el[0], 1);
116 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
118 return aff;
121 /* Return a piecewise affine expression defined on the specified domain
122 * that is equal to zero.
124 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
126 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
129 /* Return an affine expression defined on the specified domain
130 * that represents NaN.
132 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
134 isl_aff *aff;
136 aff = isl_aff_alloc(ls);
137 if (!aff)
138 return NULL;
140 isl_seq_clr(aff->v->el, aff->v->size);
142 return aff;
145 /* Return a piecewise affine expression defined on the specified domain
146 * that represents NaN.
148 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
150 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
153 /* Return an affine expression that is equal to "val" on
154 * domain local space "ls".
156 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
157 __isl_take isl_val *val)
159 isl_aff *aff;
161 if (!ls || !val)
162 goto error;
163 if (!isl_val_is_rat(val))
164 isl_die(isl_val_get_ctx(val), isl_error_invalid,
165 "expecting rational value", goto error);
167 aff = isl_aff_alloc(isl_local_space_copy(ls));
168 if (!aff)
169 goto error;
171 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
172 isl_int_set(aff->v->el[1], val->n);
173 isl_int_set(aff->v->el[0], val->d);
175 isl_local_space_free(ls);
176 isl_val_free(val);
177 return aff;
178 error:
179 isl_local_space_free(ls);
180 isl_val_free(val);
181 return NULL;
184 /* Return an affine expression that is equal to the specified dimension
185 * in "ls".
187 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
188 enum isl_dim_type type, unsigned pos)
190 isl_space *space;
191 isl_aff *aff;
193 if (!ls)
194 return NULL;
196 space = isl_local_space_get_space(ls);
197 if (!space)
198 goto error;
199 if (isl_space_is_map(space))
200 isl_die(isl_space_get_ctx(space), isl_error_invalid,
201 "expecting (parameter) set space", goto error);
202 if (pos >= isl_local_space_dim(ls, type))
203 isl_die(isl_space_get_ctx(space), isl_error_invalid,
204 "position out of bounds", goto error);
206 isl_space_free(space);
207 aff = isl_aff_alloc(ls);
208 if (!aff)
209 return NULL;
211 pos += isl_local_space_offset(aff->ls, type);
213 isl_int_set_si(aff->v->el[0], 1);
214 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
215 isl_int_set_si(aff->v->el[1 + pos], 1);
217 return aff;
218 error:
219 isl_local_space_free(ls);
220 isl_space_free(space);
221 return NULL;
224 /* Return a piecewise affine expression that is equal to
225 * the specified dimension in "ls".
227 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
228 enum isl_dim_type type, unsigned pos)
230 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
233 /* Return an affine expression that is equal to the parameter
234 * in the domain space "space" with identifier "id".
236 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
237 __isl_take isl_space *space, __isl_take isl_id *id)
239 int pos;
240 isl_local_space *ls;
242 if (!space || !id)
243 goto error;
244 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
245 if (pos < 0)
246 isl_die(isl_space_get_ctx(space), isl_error_invalid,
247 "parameter not found in space", goto error);
248 isl_id_free(id);
249 ls = isl_local_space_from_space(space);
250 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
251 error:
252 isl_space_free(space);
253 isl_id_free(id);
254 return NULL;
257 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
259 if (!aff)
260 return NULL;
262 aff->ref++;
263 return aff;
266 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
268 if (!aff)
269 return NULL;
271 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
272 isl_vec_copy(aff->v));
275 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
277 if (!aff)
278 return NULL;
280 if (aff->ref == 1)
281 return aff;
282 aff->ref--;
283 return isl_aff_dup(aff);
286 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
288 if (!aff)
289 return NULL;
291 if (--aff->ref > 0)
292 return NULL;
294 isl_local_space_free(aff->ls);
295 isl_vec_free(aff->v);
297 free(aff);
299 return NULL;
302 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
304 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
307 /* Return a hash value that digests "aff".
309 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
311 uint32_t hash, ls_hash, v_hash;
313 if (!aff)
314 return 0;
316 hash = isl_hash_init();
317 ls_hash = isl_local_space_get_hash(aff->ls);
318 isl_hash_hash(hash, ls_hash);
319 v_hash = isl_vec_get_hash(aff->v);
320 isl_hash_hash(hash, v_hash);
322 return hash;
325 /* Externally, an isl_aff has a map space, but internally, the
326 * ls field corresponds to the domain of that space.
328 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
330 if (!aff)
331 return 0;
332 if (type == isl_dim_out)
333 return 1;
334 if (type == isl_dim_in)
335 type = isl_dim_set;
336 return isl_local_space_dim(aff->ls, type);
339 /* Return the position of the dimension of the given type and name
340 * in "aff".
341 * Return -1 if no such dimension can be found.
343 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
344 const char *name)
346 if (!aff)
347 return -1;
348 if (type == isl_dim_out)
349 return -1;
350 if (type == isl_dim_in)
351 type = isl_dim_set;
352 return isl_local_space_find_dim_by_name(aff->ls, type, name);
355 /* Return the domain space of "aff".
357 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
359 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
362 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
364 return isl_space_copy(isl_aff_peek_domain_space(aff));
367 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
369 isl_space *space;
370 if (!aff)
371 return NULL;
372 space = isl_local_space_get_space(aff->ls);
373 space = isl_space_from_domain(space);
374 space = isl_space_add_dims(space, isl_dim_out, 1);
375 return space;
378 __isl_give isl_local_space *isl_aff_get_domain_local_space(
379 __isl_keep isl_aff *aff)
381 return aff ? isl_local_space_copy(aff->ls) : NULL;
384 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
386 isl_local_space *ls;
387 if (!aff)
388 return NULL;
389 ls = isl_local_space_copy(aff->ls);
390 ls = isl_local_space_from_domain(ls);
391 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
392 return ls;
395 /* Return the local space of the domain of "aff".
396 * This may be either a copy or the local space itself
397 * if there is only one reference to "aff".
398 * This allows the local space to be modified inplace
399 * if both the expression and its local space have only a single reference.
400 * The caller is not allowed to modify "aff" between this call and
401 * a subsequent call to isl_aff_restore_domain_local_space.
402 * The only exception is that isl_aff_free can be called instead.
404 __isl_give isl_local_space *isl_aff_take_domain_local_space(
405 __isl_keep isl_aff *aff)
407 isl_local_space *ls;
409 if (!aff)
410 return NULL;
411 if (aff->ref != 1)
412 return isl_aff_get_domain_local_space(aff);
413 ls = aff->ls;
414 aff->ls = NULL;
415 return ls;
418 /* Set the local space of the domain of "aff" to "ls",
419 * where the local space of "aff" may be missing
420 * due to a preceding call to isl_aff_take_domain_local_space.
421 * However, in this case, "aff" only has a single reference and
422 * then the call to isl_aff_cow has no effect.
424 __isl_give isl_aff *isl_aff_restore_domain_local_space(
425 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
427 if (!aff || !ls)
428 goto error;
430 if (aff->ls == ls) {
431 isl_local_space_free(ls);
432 return aff;
435 aff = isl_aff_cow(aff);
436 if (!aff)
437 goto error;
438 isl_local_space_free(aff->ls);
439 aff->ls = ls;
441 return aff;
442 error:
443 isl_aff_free(aff);
444 isl_local_space_free(ls);
445 return NULL;
448 /* Externally, an isl_aff has a map space, but internally, the
449 * ls field corresponds to the domain of that space.
451 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
452 enum isl_dim_type type, unsigned pos)
454 if (!aff)
455 return NULL;
456 if (type == isl_dim_out)
457 return NULL;
458 if (type == isl_dim_in)
459 type = isl_dim_set;
460 return isl_local_space_get_dim_name(aff->ls, type, pos);
463 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
464 __isl_take isl_space *dim)
466 aff = isl_aff_cow(aff);
467 if (!aff || !dim)
468 goto error;
470 aff->ls = isl_local_space_reset_space(aff->ls, dim);
471 if (!aff->ls)
472 return isl_aff_free(aff);
474 return aff;
475 error:
476 isl_aff_free(aff);
477 isl_space_free(dim);
478 return NULL;
481 /* Reset the space of "aff". This function is called from isl_pw_templ.c
482 * and doesn't know if the space of an element object is represented
483 * directly or through its domain. It therefore passes along both.
485 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
486 __isl_take isl_space *space, __isl_take isl_space *domain)
488 isl_space_free(space);
489 return isl_aff_reset_domain_space(aff, domain);
492 /* Reorder the coefficients of the affine expression based
493 * on the given reordering.
494 * The reordering r is assumed to have been extended with the local
495 * variables.
497 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
498 __isl_take isl_reordering *r, int n_div)
500 isl_space *space;
501 isl_vec *res;
502 int i;
504 if (!vec || !r)
505 goto error;
507 space = isl_reordering_peek_space(r);
508 res = isl_vec_alloc(vec->ctx,
509 2 + isl_space_dim(space, isl_dim_all) + n_div);
510 if (!res)
511 goto error;
512 isl_seq_cpy(res->el, vec->el, 2);
513 isl_seq_clr(res->el + 2, res->size - 2);
514 for (i = 0; i < r->len; ++i)
515 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
517 isl_reordering_free(r);
518 isl_vec_free(vec);
519 return res;
520 error:
521 isl_vec_free(vec);
522 isl_reordering_free(r);
523 return NULL;
526 /* Reorder the dimensions of the domain of "aff" according
527 * to the given reordering.
529 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
530 __isl_take isl_reordering *r)
532 aff = isl_aff_cow(aff);
533 if (!aff)
534 goto error;
536 r = isl_reordering_extend(r, aff->ls->div->n_row);
537 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
538 aff->ls->div->n_row);
539 aff->ls = isl_local_space_realign(aff->ls, r);
541 if (!aff->v || !aff->ls)
542 return isl_aff_free(aff);
544 return aff;
545 error:
546 isl_aff_free(aff);
547 isl_reordering_free(r);
548 return NULL;
551 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
552 __isl_take isl_space *model)
554 isl_bool equal_params;
556 if (!aff || !model)
557 goto error;
559 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
560 if (equal_params < 0)
561 goto error;
562 if (!equal_params) {
563 isl_reordering *exp;
565 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
566 exp = isl_reordering_extend_space(exp,
567 isl_aff_get_domain_space(aff));
568 aff = isl_aff_realign_domain(aff, exp);
571 isl_space_free(model);
572 return aff;
573 error:
574 isl_space_free(model);
575 isl_aff_free(aff);
576 return NULL;
579 /* Is "aff" obviously equal to zero?
581 * If the denominator is zero, then "aff" is not equal to zero.
583 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
585 if (!aff)
586 return isl_bool_error;
588 if (isl_int_is_zero(aff->v->el[0]))
589 return isl_bool_false;
590 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
593 /* Does "aff" represent NaN?
595 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
597 if (!aff)
598 return isl_bool_error;
600 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
603 /* Are "aff1" and "aff2" obviously equal?
605 * NaN is not equal to anything, not even to another NaN.
607 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
608 __isl_keep isl_aff *aff2)
610 isl_bool equal;
612 if (!aff1 || !aff2)
613 return isl_bool_error;
615 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
616 return isl_bool_false;
618 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
619 if (equal < 0 || !equal)
620 return equal;
622 return isl_vec_is_equal(aff1->v, aff2->v);
625 /* Return the common denominator of "aff" in "v".
627 * We cannot return anything meaningful in case of a NaN.
629 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
631 if (!aff)
632 return isl_stat_error;
633 if (isl_aff_is_nan(aff))
634 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
635 "cannot get denominator of NaN", return isl_stat_error);
636 isl_int_set(*v, aff->v->el[0]);
637 return isl_stat_ok;
640 /* Return the common denominator of "aff".
642 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
644 isl_ctx *ctx;
646 if (!aff)
647 return NULL;
649 ctx = isl_aff_get_ctx(aff);
650 if (isl_aff_is_nan(aff))
651 return isl_val_nan(ctx);
652 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
655 /* Return the constant term of "aff".
657 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
659 isl_ctx *ctx;
660 isl_val *v;
662 if (!aff)
663 return NULL;
665 ctx = isl_aff_get_ctx(aff);
666 if (isl_aff_is_nan(aff))
667 return isl_val_nan(ctx);
668 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
669 return isl_val_normalize(v);
672 /* Return the coefficient of the variable of type "type" at position "pos"
673 * of "aff".
675 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
676 enum isl_dim_type type, int pos)
678 isl_ctx *ctx;
679 isl_val *v;
681 if (!aff)
682 return NULL;
684 ctx = isl_aff_get_ctx(aff);
685 if (type == isl_dim_out)
686 isl_die(ctx, isl_error_invalid,
687 "output/set dimension does not have a coefficient",
688 return NULL);
689 if (type == isl_dim_in)
690 type = isl_dim_set;
692 if (pos >= isl_local_space_dim(aff->ls, type))
693 isl_die(ctx, isl_error_invalid,
694 "position out of bounds", return NULL);
696 if (isl_aff_is_nan(aff))
697 return isl_val_nan(ctx);
698 pos += isl_local_space_offset(aff->ls, type);
699 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
700 return isl_val_normalize(v);
703 /* Return the sign of the coefficient of the variable of type "type"
704 * at position "pos" of "aff".
706 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
707 int pos)
709 isl_ctx *ctx;
711 if (!aff)
712 return 0;
714 ctx = isl_aff_get_ctx(aff);
715 if (type == isl_dim_out)
716 isl_die(ctx, isl_error_invalid,
717 "output/set dimension does not have a coefficient",
718 return 0);
719 if (type == isl_dim_in)
720 type = isl_dim_set;
722 if (pos >= isl_local_space_dim(aff->ls, type))
723 isl_die(ctx, isl_error_invalid,
724 "position out of bounds", return 0);
726 pos += isl_local_space_offset(aff->ls, type);
727 return isl_int_sgn(aff->v->el[1 + pos]);
730 /* Replace the numerator of the constant term of "aff" by "v".
732 * A NaN is unaffected by this operation.
734 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
736 if (!aff)
737 return NULL;
738 if (isl_aff_is_nan(aff))
739 return aff;
740 aff = isl_aff_cow(aff);
741 if (!aff)
742 return NULL;
744 aff->v = isl_vec_cow(aff->v);
745 if (!aff->v)
746 return isl_aff_free(aff);
748 isl_int_set(aff->v->el[1], v);
750 return aff;
753 /* Replace the constant term of "aff" by "v".
755 * A NaN is unaffected by this operation.
757 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
758 __isl_take isl_val *v)
760 if (!aff || !v)
761 goto error;
763 if (isl_aff_is_nan(aff)) {
764 isl_val_free(v);
765 return aff;
768 if (!isl_val_is_rat(v))
769 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
770 "expecting rational value", goto error);
772 if (isl_int_eq(aff->v->el[1], v->n) &&
773 isl_int_eq(aff->v->el[0], v->d)) {
774 isl_val_free(v);
775 return aff;
778 aff = isl_aff_cow(aff);
779 if (!aff)
780 goto error;
781 aff->v = isl_vec_cow(aff->v);
782 if (!aff->v)
783 goto error;
785 if (isl_int_eq(aff->v->el[0], v->d)) {
786 isl_int_set(aff->v->el[1], v->n);
787 } else if (isl_int_is_one(v->d)) {
788 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
789 } else {
790 isl_seq_scale(aff->v->el + 1,
791 aff->v->el + 1, v->d, aff->v->size - 1);
792 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
793 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
794 aff->v = isl_vec_normalize(aff->v);
795 if (!aff->v)
796 goto error;
799 isl_val_free(v);
800 return aff;
801 error:
802 isl_aff_free(aff);
803 isl_val_free(v);
804 return NULL;
807 /* Add "v" to the constant term of "aff".
809 * A NaN is unaffected by this operation.
811 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
813 if (isl_int_is_zero(v))
814 return aff;
816 if (!aff)
817 return NULL;
818 if (isl_aff_is_nan(aff))
819 return aff;
820 aff = isl_aff_cow(aff);
821 if (!aff)
822 return NULL;
824 aff->v = isl_vec_cow(aff->v);
825 if (!aff->v)
826 return isl_aff_free(aff);
828 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
830 return aff;
833 /* Add "v" to the constant term of "aff".
835 * A NaN is unaffected by this operation.
837 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
838 __isl_take isl_val *v)
840 if (!aff || !v)
841 goto error;
843 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
844 isl_val_free(v);
845 return aff;
848 if (!isl_val_is_rat(v))
849 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
850 "expecting rational value", goto error);
852 aff = isl_aff_cow(aff);
853 if (!aff)
854 goto error;
856 aff->v = isl_vec_cow(aff->v);
857 if (!aff->v)
858 goto error;
860 if (isl_int_is_one(v->d)) {
861 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
862 } else if (isl_int_eq(aff->v->el[0], v->d)) {
863 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
864 aff->v = isl_vec_normalize(aff->v);
865 if (!aff->v)
866 goto error;
867 } else {
868 isl_seq_scale(aff->v->el + 1,
869 aff->v->el + 1, v->d, aff->v->size - 1);
870 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
871 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
872 aff->v = isl_vec_normalize(aff->v);
873 if (!aff->v)
874 goto error;
877 isl_val_free(v);
878 return aff;
879 error:
880 isl_aff_free(aff);
881 isl_val_free(v);
882 return NULL;
885 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
887 isl_int t;
889 isl_int_init(t);
890 isl_int_set_si(t, v);
891 aff = isl_aff_add_constant(aff, t);
892 isl_int_clear(t);
894 return aff;
897 /* Add "v" to the numerator of the constant term of "aff".
899 * A NaN is unaffected by this operation.
901 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
903 if (isl_int_is_zero(v))
904 return aff;
906 if (!aff)
907 return NULL;
908 if (isl_aff_is_nan(aff))
909 return aff;
910 aff = isl_aff_cow(aff);
911 if (!aff)
912 return NULL;
914 aff->v = isl_vec_cow(aff->v);
915 if (!aff->v)
916 return isl_aff_free(aff);
918 isl_int_add(aff->v->el[1], aff->v->el[1], v);
920 return aff;
923 /* Add "v" to the numerator of the constant term of "aff".
925 * A NaN is unaffected by this operation.
927 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
929 isl_int t;
931 if (v == 0)
932 return aff;
934 isl_int_init(t);
935 isl_int_set_si(t, v);
936 aff = isl_aff_add_constant_num(aff, t);
937 isl_int_clear(t);
939 return aff;
942 /* Replace the numerator of the constant term of "aff" by "v".
944 * A NaN is unaffected by this operation.
946 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
948 if (!aff)
949 return NULL;
950 if (isl_aff_is_nan(aff))
951 return aff;
952 aff = isl_aff_cow(aff);
953 if (!aff)
954 return NULL;
956 aff->v = isl_vec_cow(aff->v);
957 if (!aff->v)
958 return isl_aff_free(aff);
960 isl_int_set_si(aff->v->el[1], v);
962 return aff;
965 /* Replace the numerator of the coefficient of the variable of type "type"
966 * at position "pos" of "aff" by "v".
968 * A NaN is unaffected by this operation.
970 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
971 enum isl_dim_type type, int pos, isl_int v)
973 if (!aff)
974 return NULL;
976 if (type == isl_dim_out)
977 isl_die(aff->v->ctx, isl_error_invalid,
978 "output/set dimension does not have a coefficient",
979 return isl_aff_free(aff));
980 if (type == isl_dim_in)
981 type = isl_dim_set;
983 if (pos >= isl_local_space_dim(aff->ls, type))
984 isl_die(aff->v->ctx, isl_error_invalid,
985 "position out of bounds", return isl_aff_free(aff));
987 if (isl_aff_is_nan(aff))
988 return aff;
989 aff = isl_aff_cow(aff);
990 if (!aff)
991 return NULL;
993 aff->v = isl_vec_cow(aff->v);
994 if (!aff->v)
995 return isl_aff_free(aff);
997 pos += isl_local_space_offset(aff->ls, type);
998 isl_int_set(aff->v->el[1 + pos], v);
1000 return aff;
1003 /* Replace the numerator of the coefficient of the variable of type "type"
1004 * at position "pos" of "aff" by "v".
1006 * A NaN is unaffected by this operation.
1008 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1009 enum isl_dim_type type, int pos, int v)
1011 if (!aff)
1012 return NULL;
1014 if (type == isl_dim_out)
1015 isl_die(aff->v->ctx, isl_error_invalid,
1016 "output/set dimension does not have a coefficient",
1017 return isl_aff_free(aff));
1018 if (type == isl_dim_in)
1019 type = isl_dim_set;
1021 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1022 isl_die(aff->v->ctx, isl_error_invalid,
1023 "position out of bounds", return isl_aff_free(aff));
1025 if (isl_aff_is_nan(aff))
1026 return aff;
1027 pos += isl_local_space_offset(aff->ls, type);
1028 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1029 return aff;
1031 aff = isl_aff_cow(aff);
1032 if (!aff)
1033 return NULL;
1035 aff->v = isl_vec_cow(aff->v);
1036 if (!aff->v)
1037 return isl_aff_free(aff);
1039 isl_int_set_si(aff->v->el[1 + pos], v);
1041 return aff;
1044 /* Replace the coefficient of the variable of type "type" at position "pos"
1045 * of "aff" by "v".
1047 * A NaN is unaffected by this operation.
1049 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1050 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1052 if (!aff || !v)
1053 goto error;
1055 if (type == isl_dim_out)
1056 isl_die(aff->v->ctx, isl_error_invalid,
1057 "output/set dimension does not have a coefficient",
1058 goto error);
1059 if (type == isl_dim_in)
1060 type = isl_dim_set;
1062 if (pos >= isl_local_space_dim(aff->ls, type))
1063 isl_die(aff->v->ctx, isl_error_invalid,
1064 "position out of bounds", goto error);
1066 if (isl_aff_is_nan(aff)) {
1067 isl_val_free(v);
1068 return aff;
1070 if (!isl_val_is_rat(v))
1071 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1072 "expecting rational value", goto error);
1074 pos += isl_local_space_offset(aff->ls, type);
1075 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1076 isl_int_eq(aff->v->el[0], v->d)) {
1077 isl_val_free(v);
1078 return aff;
1081 aff = isl_aff_cow(aff);
1082 if (!aff)
1083 goto error;
1084 aff->v = isl_vec_cow(aff->v);
1085 if (!aff->v)
1086 goto error;
1088 if (isl_int_eq(aff->v->el[0], v->d)) {
1089 isl_int_set(aff->v->el[1 + pos], v->n);
1090 } else if (isl_int_is_one(v->d)) {
1091 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1092 } else {
1093 isl_seq_scale(aff->v->el + 1,
1094 aff->v->el + 1, v->d, aff->v->size - 1);
1095 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1096 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1097 aff->v = isl_vec_normalize(aff->v);
1098 if (!aff->v)
1099 goto error;
1102 isl_val_free(v);
1103 return aff;
1104 error:
1105 isl_aff_free(aff);
1106 isl_val_free(v);
1107 return NULL;
1110 /* Add "v" to the coefficient of the variable of type "type"
1111 * at position "pos" of "aff".
1113 * A NaN is unaffected by this operation.
1115 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1116 enum isl_dim_type type, int pos, isl_int v)
1118 if (!aff)
1119 return NULL;
1121 if (type == isl_dim_out)
1122 isl_die(aff->v->ctx, isl_error_invalid,
1123 "output/set dimension does not have a coefficient",
1124 return isl_aff_free(aff));
1125 if (type == isl_dim_in)
1126 type = isl_dim_set;
1128 if (pos >= isl_local_space_dim(aff->ls, type))
1129 isl_die(aff->v->ctx, isl_error_invalid,
1130 "position out of bounds", return isl_aff_free(aff));
1132 if (isl_aff_is_nan(aff))
1133 return aff;
1134 aff = isl_aff_cow(aff);
1135 if (!aff)
1136 return NULL;
1138 aff->v = isl_vec_cow(aff->v);
1139 if (!aff->v)
1140 return isl_aff_free(aff);
1142 pos += isl_local_space_offset(aff->ls, type);
1143 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1145 return aff;
1148 /* Add "v" to the coefficient of the variable of type "type"
1149 * at position "pos" of "aff".
1151 * A NaN is unaffected by this operation.
1153 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1154 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1156 if (!aff || !v)
1157 goto error;
1159 if (isl_val_is_zero(v)) {
1160 isl_val_free(v);
1161 return aff;
1164 if (type == isl_dim_out)
1165 isl_die(aff->v->ctx, isl_error_invalid,
1166 "output/set dimension does not have a coefficient",
1167 goto error);
1168 if (type == isl_dim_in)
1169 type = isl_dim_set;
1171 if (pos >= isl_local_space_dim(aff->ls, type))
1172 isl_die(aff->v->ctx, isl_error_invalid,
1173 "position out of bounds", goto error);
1175 if (isl_aff_is_nan(aff)) {
1176 isl_val_free(v);
1177 return aff;
1179 if (!isl_val_is_rat(v))
1180 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1181 "expecting rational value", goto error);
1183 aff = isl_aff_cow(aff);
1184 if (!aff)
1185 goto error;
1187 aff->v = isl_vec_cow(aff->v);
1188 if (!aff->v)
1189 goto error;
1191 pos += isl_local_space_offset(aff->ls, type);
1192 if (isl_int_is_one(v->d)) {
1193 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1194 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1195 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1196 aff->v = isl_vec_normalize(aff->v);
1197 if (!aff->v)
1198 goto error;
1199 } else {
1200 isl_seq_scale(aff->v->el + 1,
1201 aff->v->el + 1, v->d, aff->v->size - 1);
1202 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1203 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1204 aff->v = isl_vec_normalize(aff->v);
1205 if (!aff->v)
1206 goto error;
1209 isl_val_free(v);
1210 return aff;
1211 error:
1212 isl_aff_free(aff);
1213 isl_val_free(v);
1214 return NULL;
1217 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1218 enum isl_dim_type type, int pos, int v)
1220 isl_int t;
1222 isl_int_init(t);
1223 isl_int_set_si(t, v);
1224 aff = isl_aff_add_coefficient(aff, type, pos, t);
1225 isl_int_clear(t);
1227 return aff;
1230 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1232 if (!aff)
1233 return NULL;
1235 return isl_local_space_get_div(aff->ls, pos);
1238 /* Return the negation of "aff".
1240 * As a special case, -NaN = NaN.
1242 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1244 if (!aff)
1245 return NULL;
1246 if (isl_aff_is_nan(aff))
1247 return aff;
1248 aff = isl_aff_cow(aff);
1249 if (!aff)
1250 return NULL;
1251 aff->v = isl_vec_cow(aff->v);
1252 if (!aff->v)
1253 return isl_aff_free(aff);
1255 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1257 return aff;
1260 /* Remove divs from the local space that do not appear in the affine
1261 * expression.
1262 * We currently only remove divs at the end.
1263 * Some intermediate divs may also not appear directly in the affine
1264 * expression, but we would also need to check that no other divs are
1265 * defined in terms of them.
1267 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1269 int pos;
1270 int off;
1271 int n;
1273 if (!aff)
1274 return NULL;
1276 n = isl_local_space_dim(aff->ls, isl_dim_div);
1277 off = isl_local_space_offset(aff->ls, isl_dim_div);
1279 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1280 if (pos == n)
1281 return aff;
1283 aff = isl_aff_cow(aff);
1284 if (!aff)
1285 return NULL;
1287 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1288 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1289 if (!aff->ls || !aff->v)
1290 return isl_aff_free(aff);
1292 return aff;
1295 /* Look for any divs in the aff->ls with a denominator equal to one
1296 * and plug them into the affine expression and any subsequent divs
1297 * that may reference the div.
1299 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1301 int i, n;
1302 int len;
1303 isl_int v;
1304 isl_vec *vec;
1305 isl_local_space *ls;
1306 unsigned pos;
1308 if (!aff)
1309 return NULL;
1311 n = isl_local_space_dim(aff->ls, isl_dim_div);
1312 len = aff->v->size;
1313 for (i = 0; i < n; ++i) {
1314 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1315 continue;
1316 ls = isl_local_space_copy(aff->ls);
1317 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1318 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1319 vec = isl_vec_copy(aff->v);
1320 vec = isl_vec_cow(vec);
1321 if (!ls || !vec)
1322 goto error;
1324 isl_int_init(v);
1326 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1327 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1328 len, len, v);
1330 isl_int_clear(v);
1332 isl_vec_free(aff->v);
1333 aff->v = vec;
1334 isl_local_space_free(aff->ls);
1335 aff->ls = ls;
1338 return aff;
1339 error:
1340 isl_vec_free(vec);
1341 isl_local_space_free(ls);
1342 return isl_aff_free(aff);
1345 /* Look for any divs j that appear with a unit coefficient inside
1346 * the definitions of other divs i and plug them into the definitions
1347 * of the divs i.
1349 * In particular, an expression of the form
1351 * floor((f(..) + floor(g(..)/n))/m)
1353 * is simplified to
1355 * floor((n * f(..) + g(..))/(n * m))
1357 * This simplification is correct because we can move the expression
1358 * f(..) into the inner floor in the original expression to obtain
1360 * floor(floor((n * f(..) + g(..))/n)/m)
1362 * from which we can derive the simplified expression.
1364 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1366 int i, j, n;
1367 int off;
1369 if (!aff)
1370 return NULL;
1372 n = isl_local_space_dim(aff->ls, isl_dim_div);
1373 off = isl_local_space_offset(aff->ls, isl_dim_div);
1374 for (i = 1; i < n; ++i) {
1375 for (j = 0; j < i; ++j) {
1376 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1377 continue;
1378 aff->ls = isl_local_space_substitute_seq(aff->ls,
1379 isl_dim_div, j, aff->ls->div->row[j],
1380 aff->v->size, i, 1);
1381 if (!aff->ls)
1382 return isl_aff_free(aff);
1386 return aff;
1389 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1391 * Even though this function is only called on isl_affs with a single
1392 * reference, we are careful to only change aff->v and aff->ls together.
1394 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1396 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1397 isl_local_space *ls;
1398 isl_vec *v;
1400 ls = isl_local_space_copy(aff->ls);
1401 ls = isl_local_space_swap_div(ls, a, b);
1402 v = isl_vec_copy(aff->v);
1403 v = isl_vec_cow(v);
1404 if (!ls || !v)
1405 goto error;
1407 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1408 isl_vec_free(aff->v);
1409 aff->v = v;
1410 isl_local_space_free(aff->ls);
1411 aff->ls = ls;
1413 return aff;
1414 error:
1415 isl_vec_free(v);
1416 isl_local_space_free(ls);
1417 return isl_aff_free(aff);
1420 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1422 * We currently do not actually remove div "b", but simply add its
1423 * coefficient to that of "a" and then zero it out.
1425 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1427 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1429 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1430 return aff;
1432 aff->v = isl_vec_cow(aff->v);
1433 if (!aff->v)
1434 return isl_aff_free(aff);
1436 isl_int_add(aff->v->el[1 + off + a],
1437 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1438 isl_int_set_si(aff->v->el[1 + off + b], 0);
1440 return aff;
1443 /* Sort the divs in the local space of "aff" according to
1444 * the comparison function "cmp_row" in isl_local_space.c,
1445 * combining the coefficients of identical divs.
1447 * Reordering divs does not change the semantics of "aff",
1448 * so there is no need to call isl_aff_cow.
1449 * Moreover, this function is currently only called on isl_affs
1450 * with a single reference.
1452 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1454 int i, j, n;
1456 if (!aff)
1457 return NULL;
1459 n = isl_aff_dim(aff, isl_dim_div);
1460 for (i = 1; i < n; ++i) {
1461 for (j = i - 1; j >= 0; --j) {
1462 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1463 if (cmp < 0)
1464 break;
1465 if (cmp == 0)
1466 aff = merge_divs(aff, j, j + 1);
1467 else
1468 aff = swap_div(aff, j, j + 1);
1469 if (!aff)
1470 return NULL;
1474 return aff;
1477 /* Normalize the representation of "aff".
1479 * This function should only be called of "new" isl_affs, i.e.,
1480 * with only a single reference. We therefore do not need to
1481 * worry about affecting other instances.
1483 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1485 if (!aff)
1486 return NULL;
1487 aff->v = isl_vec_normalize(aff->v);
1488 if (!aff->v)
1489 return isl_aff_free(aff);
1490 aff = plug_in_integral_divs(aff);
1491 aff = plug_in_unit_divs(aff);
1492 aff = sort_divs(aff);
1493 aff = isl_aff_remove_unused_divs(aff);
1494 return aff;
1497 /* Given f, return floor(f).
1498 * If f is an integer expression, then just return f.
1499 * If f is a constant, then return the constant floor(f).
1500 * Otherwise, if f = g/m, write g = q m + r,
1501 * create a new div d = [r/m] and return the expression q + d.
1502 * The coefficients in r are taken to lie between -m/2 and m/2.
1504 * reduce_div_coefficients performs the same normalization.
1506 * As a special case, floor(NaN) = NaN.
1508 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1510 int i;
1511 int size;
1512 isl_ctx *ctx;
1513 isl_vec *div;
1515 if (!aff)
1516 return NULL;
1518 if (isl_aff_is_nan(aff))
1519 return aff;
1520 if (isl_int_is_one(aff->v->el[0]))
1521 return aff;
1523 aff = isl_aff_cow(aff);
1524 if (!aff)
1525 return NULL;
1527 aff->v = isl_vec_cow(aff->v);
1528 if (!aff->v)
1529 return isl_aff_free(aff);
1531 if (isl_aff_is_cst(aff)) {
1532 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1533 isl_int_set_si(aff->v->el[0], 1);
1534 return aff;
1537 div = isl_vec_copy(aff->v);
1538 div = isl_vec_cow(div);
1539 if (!div)
1540 return isl_aff_free(aff);
1542 ctx = isl_aff_get_ctx(aff);
1543 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1544 for (i = 1; i < aff->v->size; ++i) {
1545 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1546 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1547 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1548 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1549 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1553 aff->ls = isl_local_space_add_div(aff->ls, div);
1554 if (!aff->ls)
1555 return isl_aff_free(aff);
1557 size = aff->v->size;
1558 aff->v = isl_vec_extend(aff->v, size + 1);
1559 if (!aff->v)
1560 return isl_aff_free(aff);
1561 isl_int_set_si(aff->v->el[0], 1);
1562 isl_int_set_si(aff->v->el[size], 1);
1564 aff = isl_aff_normalize(aff);
1566 return aff;
1569 /* Compute
1571 * aff mod m = aff - m * floor(aff/m)
1573 * with m an integer value.
1575 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1576 __isl_take isl_val *m)
1578 isl_aff *res;
1580 if (!aff || !m)
1581 goto error;
1583 if (!isl_val_is_int(m))
1584 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1585 "expecting integer modulo", goto error);
1587 res = isl_aff_copy(aff);
1588 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1589 aff = isl_aff_floor(aff);
1590 aff = isl_aff_scale_val(aff, m);
1591 res = isl_aff_sub(res, aff);
1593 return res;
1594 error:
1595 isl_aff_free(aff);
1596 isl_val_free(m);
1597 return NULL;
1600 /* Compute
1602 * pwaff mod m = pwaff - m * floor(pwaff/m)
1604 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1606 isl_pw_aff *res;
1608 res = isl_pw_aff_copy(pwaff);
1609 pwaff = isl_pw_aff_scale_down(pwaff, m);
1610 pwaff = isl_pw_aff_floor(pwaff);
1611 pwaff = isl_pw_aff_scale(pwaff, m);
1612 res = isl_pw_aff_sub(res, pwaff);
1614 return res;
1617 /* Compute
1619 * pa mod m = pa - m * floor(pa/m)
1621 * with m an integer value.
1623 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1624 __isl_take isl_val *m)
1626 if (!pa || !m)
1627 goto error;
1628 if (!isl_val_is_int(m))
1629 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1630 "expecting integer modulo", goto error);
1631 pa = isl_pw_aff_mod(pa, m->n);
1632 isl_val_free(m);
1633 return pa;
1634 error:
1635 isl_pw_aff_free(pa);
1636 isl_val_free(m);
1637 return NULL;
1640 /* Given f, return ceil(f).
1641 * If f is an integer expression, then just return f.
1642 * Otherwise, let f be the expression
1644 * e/m
1646 * then return
1648 * floor((e + m - 1)/m)
1650 * As a special case, ceil(NaN) = NaN.
1652 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1654 if (!aff)
1655 return NULL;
1657 if (isl_aff_is_nan(aff))
1658 return aff;
1659 if (isl_int_is_one(aff->v->el[0]))
1660 return aff;
1662 aff = isl_aff_cow(aff);
1663 if (!aff)
1664 return NULL;
1665 aff->v = isl_vec_cow(aff->v);
1666 if (!aff->v)
1667 return isl_aff_free(aff);
1669 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1670 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1671 aff = isl_aff_floor(aff);
1673 return aff;
1676 /* Apply the expansion computed by isl_merge_divs.
1677 * The expansion itself is given by "exp" while the resulting
1678 * list of divs is given by "div".
1680 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1681 __isl_take isl_mat *div, int *exp)
1683 int old_n_div;
1684 int new_n_div;
1685 int offset;
1687 aff = isl_aff_cow(aff);
1688 if (!aff || !div)
1689 goto error;
1691 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1692 new_n_div = isl_mat_rows(div);
1693 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1695 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1696 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1697 if (!aff->v || !aff->ls)
1698 return isl_aff_free(aff);
1699 return aff;
1700 error:
1701 isl_aff_free(aff);
1702 isl_mat_free(div);
1703 return NULL;
1706 /* Add two affine expressions that live in the same local space.
1708 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1709 __isl_take isl_aff *aff2)
1711 isl_int gcd, f;
1713 aff1 = isl_aff_cow(aff1);
1714 if (!aff1 || !aff2)
1715 goto error;
1717 aff1->v = isl_vec_cow(aff1->v);
1718 if (!aff1->v)
1719 goto error;
1721 isl_int_init(gcd);
1722 isl_int_init(f);
1723 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1724 isl_int_divexact(f, aff2->v->el[0], gcd);
1725 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1726 isl_int_divexact(f, aff1->v->el[0], gcd);
1727 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1728 isl_int_divexact(f, aff2->v->el[0], gcd);
1729 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1730 isl_int_clear(f);
1731 isl_int_clear(gcd);
1733 isl_aff_free(aff2);
1734 return aff1;
1735 error:
1736 isl_aff_free(aff1);
1737 isl_aff_free(aff2);
1738 return NULL;
1741 /* Return the sum of "aff1" and "aff2".
1743 * If either of the two is NaN, then the result is NaN.
1745 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1746 __isl_take isl_aff *aff2)
1748 isl_ctx *ctx;
1749 int *exp1 = NULL;
1750 int *exp2 = NULL;
1751 isl_mat *div;
1752 int n_div1, n_div2;
1754 if (!aff1 || !aff2)
1755 goto error;
1757 ctx = isl_aff_get_ctx(aff1);
1758 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1759 isl_die(ctx, isl_error_invalid,
1760 "spaces don't match", goto error);
1762 if (isl_aff_is_nan(aff1)) {
1763 isl_aff_free(aff2);
1764 return aff1;
1766 if (isl_aff_is_nan(aff2)) {
1767 isl_aff_free(aff1);
1768 return aff2;
1771 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1772 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1773 if (n_div1 == 0 && n_div2 == 0)
1774 return add_expanded(aff1, aff2);
1776 exp1 = isl_alloc_array(ctx, int, n_div1);
1777 exp2 = isl_alloc_array(ctx, int, n_div2);
1778 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1779 goto error;
1781 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1782 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1783 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1784 free(exp1);
1785 free(exp2);
1787 return add_expanded(aff1, aff2);
1788 error:
1789 free(exp1);
1790 free(exp2);
1791 isl_aff_free(aff1);
1792 isl_aff_free(aff2);
1793 return NULL;
1796 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1797 __isl_take isl_aff *aff2)
1799 return isl_aff_add(aff1, isl_aff_neg(aff2));
1802 /* Return the result of scaling "aff" by a factor of "f".
1804 * As a special case, f * NaN = NaN.
1806 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1808 isl_int gcd;
1810 if (!aff)
1811 return NULL;
1812 if (isl_aff_is_nan(aff))
1813 return aff;
1815 if (isl_int_is_one(f))
1816 return aff;
1818 aff = isl_aff_cow(aff);
1819 if (!aff)
1820 return NULL;
1821 aff->v = isl_vec_cow(aff->v);
1822 if (!aff->v)
1823 return isl_aff_free(aff);
1825 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1826 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1827 return aff;
1830 isl_int_init(gcd);
1831 isl_int_gcd(gcd, aff->v->el[0], f);
1832 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1833 isl_int_divexact(gcd, f, gcd);
1834 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1835 isl_int_clear(gcd);
1837 return aff;
1840 /* Multiple "aff" by "v".
1842 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1843 __isl_take isl_val *v)
1845 if (!aff || !v)
1846 goto error;
1848 if (isl_val_is_one(v)) {
1849 isl_val_free(v);
1850 return aff;
1853 if (!isl_val_is_rat(v))
1854 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1855 "expecting rational factor", goto error);
1857 aff = isl_aff_scale(aff, v->n);
1858 aff = isl_aff_scale_down(aff, v->d);
1860 isl_val_free(v);
1861 return aff;
1862 error:
1863 isl_aff_free(aff);
1864 isl_val_free(v);
1865 return NULL;
1868 /* Return the result of scaling "aff" down by a factor of "f".
1870 * As a special case, NaN/f = NaN.
1872 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1874 isl_int gcd;
1876 if (!aff)
1877 return NULL;
1878 if (isl_aff_is_nan(aff))
1879 return aff;
1881 if (isl_int_is_one(f))
1882 return aff;
1884 aff = isl_aff_cow(aff);
1885 if (!aff)
1886 return NULL;
1888 if (isl_int_is_zero(f))
1889 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1890 "cannot scale down by zero", return isl_aff_free(aff));
1892 aff->v = isl_vec_cow(aff->v);
1893 if (!aff->v)
1894 return isl_aff_free(aff);
1896 isl_int_init(gcd);
1897 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1898 isl_int_gcd(gcd, gcd, f);
1899 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1900 isl_int_divexact(gcd, f, gcd);
1901 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1902 isl_int_clear(gcd);
1904 return aff;
1907 /* Divide "aff" by "v".
1909 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1910 __isl_take isl_val *v)
1912 if (!aff || !v)
1913 goto error;
1915 if (isl_val_is_one(v)) {
1916 isl_val_free(v);
1917 return aff;
1920 if (!isl_val_is_rat(v))
1921 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1922 "expecting rational factor", goto error);
1923 if (!isl_val_is_pos(v))
1924 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1925 "factor needs to be positive", goto error);
1927 aff = isl_aff_scale(aff, v->d);
1928 aff = isl_aff_scale_down(aff, v->n);
1930 isl_val_free(v);
1931 return aff;
1932 error:
1933 isl_aff_free(aff);
1934 isl_val_free(v);
1935 return NULL;
1938 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1940 isl_int v;
1942 if (f == 1)
1943 return aff;
1945 isl_int_init(v);
1946 isl_int_set_ui(v, f);
1947 aff = isl_aff_scale_down(aff, v);
1948 isl_int_clear(v);
1950 return aff;
1953 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1954 enum isl_dim_type type, unsigned pos, const char *s)
1956 aff = isl_aff_cow(aff);
1957 if (!aff)
1958 return NULL;
1959 if (type == isl_dim_out)
1960 isl_die(aff->v->ctx, isl_error_invalid,
1961 "cannot set name of output/set dimension",
1962 return isl_aff_free(aff));
1963 if (type == isl_dim_in)
1964 type = isl_dim_set;
1965 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1966 if (!aff->ls)
1967 return isl_aff_free(aff);
1969 return aff;
1972 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1973 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1975 aff = isl_aff_cow(aff);
1976 if (!aff)
1977 goto error;
1978 if (type == isl_dim_out)
1979 isl_die(aff->v->ctx, isl_error_invalid,
1980 "cannot set name of output/set dimension",
1981 goto error);
1982 if (type == isl_dim_in)
1983 type = isl_dim_set;
1984 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1985 if (!aff->ls)
1986 return isl_aff_free(aff);
1988 return aff;
1989 error:
1990 isl_id_free(id);
1991 isl_aff_free(aff);
1992 return NULL;
1995 /* Replace the identifier of the input tuple of "aff" by "id".
1996 * type is currently required to be equal to isl_dim_in
1998 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1999 enum isl_dim_type type, __isl_take isl_id *id)
2001 aff = isl_aff_cow(aff);
2002 if (!aff)
2003 goto error;
2004 if (type != isl_dim_in)
2005 isl_die(aff->v->ctx, isl_error_invalid,
2006 "cannot only set id of input tuple", goto error);
2007 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2008 if (!aff->ls)
2009 return isl_aff_free(aff);
2011 return aff;
2012 error:
2013 isl_id_free(id);
2014 isl_aff_free(aff);
2015 return NULL;
2018 /* Exploit the equalities in "eq" to simplify the affine expression
2019 * and the expressions of the integer divisions in the local space.
2020 * The integer divisions in this local space are assumed to appear
2021 * as regular dimensions in "eq".
2023 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2024 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2026 int i, j;
2027 unsigned total;
2028 unsigned n_div;
2030 if (!eq)
2031 goto error;
2032 if (eq->n_eq == 0) {
2033 isl_basic_set_free(eq);
2034 return aff;
2037 aff = isl_aff_cow(aff);
2038 if (!aff)
2039 goto error;
2041 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2042 isl_basic_set_copy(eq));
2043 aff->v = isl_vec_cow(aff->v);
2044 if (!aff->ls || !aff->v)
2045 goto error;
2047 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2048 n_div = eq->n_div;
2049 for (i = 0; i < eq->n_eq; ++i) {
2050 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2051 if (j < 0 || j == 0 || j >= total)
2052 continue;
2054 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2055 &aff->v->el[0]);
2058 isl_basic_set_free(eq);
2059 aff = isl_aff_normalize(aff);
2060 return aff;
2061 error:
2062 isl_basic_set_free(eq);
2063 isl_aff_free(aff);
2064 return NULL;
2067 /* Exploit the equalities in "eq" to simplify the affine expression
2068 * and the expressions of the integer divisions in the local space.
2070 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2071 __isl_take isl_basic_set *eq)
2073 int n_div;
2075 if (!aff || !eq)
2076 goto error;
2077 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2078 if (n_div > 0)
2079 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2080 return isl_aff_substitute_equalities_lifted(aff, eq);
2081 error:
2082 isl_basic_set_free(eq);
2083 isl_aff_free(aff);
2084 return NULL;
2087 /* Look for equalities among the variables shared by context and aff
2088 * and the integer divisions of aff, if any.
2089 * The equalities are then used to eliminate coefficients and/or integer
2090 * divisions from aff.
2092 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2093 __isl_take isl_set *context)
2095 isl_basic_set *hull;
2096 int n_div;
2098 if (!aff)
2099 goto error;
2100 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2101 if (n_div > 0) {
2102 isl_basic_set *bset;
2103 isl_local_space *ls;
2104 context = isl_set_add_dims(context, isl_dim_set, n_div);
2105 ls = isl_aff_get_domain_local_space(aff);
2106 bset = isl_basic_set_from_local_space(ls);
2107 bset = isl_basic_set_lift(bset);
2108 bset = isl_basic_set_flatten(bset);
2109 context = isl_set_intersect(context,
2110 isl_set_from_basic_set(bset));
2113 hull = isl_set_affine_hull(context);
2114 return isl_aff_substitute_equalities_lifted(aff, hull);
2115 error:
2116 isl_aff_free(aff);
2117 isl_set_free(context);
2118 return NULL;
2121 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2122 __isl_take isl_set *context)
2124 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2125 dom_context = isl_set_intersect_params(dom_context, context);
2126 return isl_aff_gist(aff, dom_context);
2129 /* Return a basic set containing those elements in the space
2130 * of aff where it is positive. "rational" should not be set.
2132 * If "aff" is NaN, then it is not positive.
2134 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2135 int rational)
2137 isl_constraint *ineq;
2138 isl_basic_set *bset;
2139 isl_val *c;
2141 if (!aff)
2142 return NULL;
2143 if (isl_aff_is_nan(aff)) {
2144 isl_space *space = isl_aff_get_domain_space(aff);
2145 isl_aff_free(aff);
2146 return isl_basic_set_empty(space);
2148 if (rational)
2149 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2150 "rational sets not supported", goto error);
2152 ineq = isl_inequality_from_aff(aff);
2153 c = isl_constraint_get_constant_val(ineq);
2154 c = isl_val_sub_ui(c, 1);
2155 ineq = isl_constraint_set_constant_val(ineq, c);
2157 bset = isl_basic_set_from_constraint(ineq);
2158 bset = isl_basic_set_simplify(bset);
2159 return bset;
2160 error:
2161 isl_aff_free(aff);
2162 return NULL;
2165 /* Return a basic set containing those elements in the space
2166 * of aff where it is non-negative.
2167 * If "rational" is set, then return a rational basic set.
2169 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2171 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2172 __isl_take isl_aff *aff, int rational)
2174 isl_constraint *ineq;
2175 isl_basic_set *bset;
2177 if (!aff)
2178 return NULL;
2179 if (isl_aff_is_nan(aff)) {
2180 isl_space *space = isl_aff_get_domain_space(aff);
2181 isl_aff_free(aff);
2182 return isl_basic_set_empty(space);
2185 ineq = isl_inequality_from_aff(aff);
2187 bset = isl_basic_set_from_constraint(ineq);
2188 if (rational)
2189 bset = isl_basic_set_set_rational(bset);
2190 bset = isl_basic_set_simplify(bset);
2191 return bset;
2194 /* Return a basic set containing those elements in the space
2195 * of aff where it is non-negative.
2197 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2199 return aff_nonneg_basic_set(aff, 0);
2202 /* Return a basic set containing those elements in the domain space
2203 * of "aff" where it is positive.
2205 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2207 aff = isl_aff_add_constant_num_si(aff, -1);
2208 return isl_aff_nonneg_basic_set(aff);
2211 /* Return a basic set containing those elements in the domain space
2212 * of aff where it is negative.
2214 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2216 aff = isl_aff_neg(aff);
2217 return isl_aff_pos_basic_set(aff);
2220 /* Return a basic set containing those elements in the space
2221 * of aff where it is zero.
2222 * If "rational" is set, then return a rational basic set.
2224 * If "aff" is NaN, then it is not zero.
2226 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2227 int rational)
2229 isl_constraint *ineq;
2230 isl_basic_set *bset;
2232 if (!aff)
2233 return NULL;
2234 if (isl_aff_is_nan(aff)) {
2235 isl_space *space = isl_aff_get_domain_space(aff);
2236 isl_aff_free(aff);
2237 return isl_basic_set_empty(space);
2240 ineq = isl_equality_from_aff(aff);
2242 bset = isl_basic_set_from_constraint(ineq);
2243 if (rational)
2244 bset = isl_basic_set_set_rational(bset);
2245 bset = isl_basic_set_simplify(bset);
2246 return bset;
2249 /* Return a basic set containing those elements in the space
2250 * of aff where it is zero.
2252 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2254 return aff_zero_basic_set(aff, 0);
2257 /* Return a basic set containing those elements in the shared space
2258 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2260 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2261 __isl_take isl_aff *aff2)
2263 aff1 = isl_aff_sub(aff1, aff2);
2265 return isl_aff_nonneg_basic_set(aff1);
2268 /* Return a basic set containing those elements in the shared domain space
2269 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2271 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2272 __isl_take isl_aff *aff2)
2274 aff1 = isl_aff_sub(aff1, aff2);
2276 return isl_aff_pos_basic_set(aff1);
2279 /* Return a set containing those elements in the shared space
2280 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2282 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2283 __isl_take isl_aff *aff2)
2285 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2288 /* Return a set containing those elements in the shared domain space
2289 * of aff1 and aff2 where aff1 is greater than aff2.
2291 * If either of the two inputs is NaN, then the result is empty,
2292 * as comparisons with NaN always return false.
2294 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2295 __isl_take isl_aff *aff2)
2297 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2300 /* Return a basic set containing those elements in the shared space
2301 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2303 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2304 __isl_take isl_aff *aff2)
2306 return isl_aff_ge_basic_set(aff2, aff1);
2309 /* Return a basic set containing those elements in the shared domain space
2310 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2312 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2313 __isl_take isl_aff *aff2)
2315 return isl_aff_gt_basic_set(aff2, aff1);
2318 /* Return a set containing those elements in the shared space
2319 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2321 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2322 __isl_take isl_aff *aff2)
2324 return isl_aff_ge_set(aff2, aff1);
2327 /* Return a set containing those elements in the shared domain space
2328 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2330 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2331 __isl_take isl_aff *aff2)
2333 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2336 /* Return a basic set containing those elements in the shared space
2337 * of aff1 and aff2 where aff1 and aff2 are equal.
2339 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2340 __isl_take isl_aff *aff2)
2342 aff1 = isl_aff_sub(aff1, aff2);
2344 return isl_aff_zero_basic_set(aff1);
2347 /* Return a set containing those elements in the shared space
2348 * of aff1 and aff2 where aff1 and aff2 are equal.
2350 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2351 __isl_take isl_aff *aff2)
2353 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2356 /* Return a set containing those elements in the shared domain space
2357 * of aff1 and aff2 where aff1 and aff2 are not equal.
2359 * If either of the two inputs is NaN, then the result is empty,
2360 * as comparisons with NaN always return false.
2362 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2363 __isl_take isl_aff *aff2)
2365 isl_set *set_lt, *set_gt;
2367 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2368 isl_aff_copy(aff2));
2369 set_gt = isl_aff_gt_set(aff1, aff2);
2370 return isl_set_union_disjoint(set_lt, set_gt);
2373 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2374 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2376 aff1 = isl_aff_add(aff1, aff2);
2377 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2378 return aff1;
2381 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2383 if (!aff)
2384 return -1;
2386 return 0;
2389 /* Check whether the given affine expression has non-zero coefficient
2390 * for any dimension in the given range or if any of these dimensions
2391 * appear with non-zero coefficients in any of the integer divisions
2392 * involved in the affine expression.
2394 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2395 enum isl_dim_type type, unsigned first, unsigned n)
2397 int i;
2398 isl_ctx *ctx;
2399 int *active = NULL;
2400 isl_bool involves = isl_bool_false;
2402 if (!aff)
2403 return isl_bool_error;
2404 if (n == 0)
2405 return isl_bool_false;
2407 ctx = isl_aff_get_ctx(aff);
2408 if (first + n > isl_aff_dim(aff, type))
2409 isl_die(ctx, isl_error_invalid,
2410 "range out of bounds", return isl_bool_error);
2412 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2413 if (!active)
2414 goto error;
2416 first += isl_local_space_offset(aff->ls, type) - 1;
2417 for (i = 0; i < n; ++i)
2418 if (active[first + i]) {
2419 involves = isl_bool_true;
2420 break;
2423 free(active);
2425 return involves;
2426 error:
2427 free(active);
2428 return isl_bool_error;
2431 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2432 enum isl_dim_type type, unsigned first, unsigned n)
2434 isl_ctx *ctx;
2436 if (!aff)
2437 return NULL;
2438 if (type == isl_dim_out)
2439 isl_die(aff->v->ctx, isl_error_invalid,
2440 "cannot drop output/set dimension",
2441 return isl_aff_free(aff));
2442 if (type == isl_dim_in)
2443 type = isl_dim_set;
2444 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2445 return aff;
2447 ctx = isl_aff_get_ctx(aff);
2448 if (first + n > isl_local_space_dim(aff->ls, type))
2449 isl_die(ctx, isl_error_invalid, "range out of bounds",
2450 return isl_aff_free(aff));
2452 aff = isl_aff_cow(aff);
2453 if (!aff)
2454 return NULL;
2456 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2457 if (!aff->ls)
2458 return isl_aff_free(aff);
2460 first += 1 + isl_local_space_offset(aff->ls, type);
2461 aff->v = isl_vec_drop_els(aff->v, first, n);
2462 if (!aff->v)
2463 return isl_aff_free(aff);
2465 return aff;
2468 /* Drop the "n" domain dimensions starting at "first" from "aff",
2469 * after checking that they do not appear in the affine expression.
2471 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2472 unsigned n)
2474 isl_bool involves;
2476 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2477 if (involves < 0)
2478 return isl_aff_free(aff);
2479 if (involves)
2480 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2481 "affine expression involves some of the domain dimensions",
2482 return isl_aff_free(aff));
2483 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2486 /* Project the domain of the affine expression onto its parameter space.
2487 * The affine expression may not involve any of the domain dimensions.
2489 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2491 isl_space *space;
2492 unsigned n;
2494 n = isl_aff_dim(aff, isl_dim_in);
2495 aff = drop_domain(aff, 0, n);
2496 space = isl_aff_get_domain_space(aff);
2497 space = isl_space_params(space);
2498 aff = isl_aff_reset_domain_space(aff, space);
2499 return aff;
2502 /* Check that the domain of "aff" is a product.
2504 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2506 isl_bool is_product;
2508 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2509 if (is_product < 0)
2510 return isl_stat_error;
2511 if (!is_product)
2512 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2513 "domain is not a product", return isl_stat_error);
2514 return isl_stat_ok;
2517 /* Given an affine function with a domain of the form [A -> B] that
2518 * does not depend on B, return the same function on domain A.
2520 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2522 isl_space *space;
2523 int n, n_in;
2525 if (check_domain_product(aff) < 0)
2526 return isl_aff_free(aff);
2527 space = isl_aff_get_domain_space(aff);
2528 n = isl_space_dim(space, isl_dim_set);
2529 space = isl_space_factor_domain(space);
2530 n_in = isl_space_dim(space, isl_dim_set);
2531 aff = drop_domain(aff, n_in, n - n_in);
2532 aff = isl_aff_reset_domain_space(aff, space);
2533 return aff;
2536 /* Convert an affine expression defined over a parameter domain
2537 * into one that is defined over a zero-dimensional set.
2539 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2541 isl_local_space *ls;
2543 ls = isl_aff_take_domain_local_space(aff);
2544 ls = isl_local_space_set_from_params(ls);
2545 aff = isl_aff_restore_domain_local_space(aff, ls);
2547 return aff;
2550 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2551 enum isl_dim_type type, unsigned first, unsigned n)
2553 isl_ctx *ctx;
2555 if (!aff)
2556 return NULL;
2557 if (type == isl_dim_out)
2558 isl_die(aff->v->ctx, isl_error_invalid,
2559 "cannot insert output/set dimensions",
2560 return isl_aff_free(aff));
2561 if (type == isl_dim_in)
2562 type = isl_dim_set;
2563 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2564 return aff;
2566 ctx = isl_aff_get_ctx(aff);
2567 if (first > isl_local_space_dim(aff->ls, type))
2568 isl_die(ctx, isl_error_invalid, "position out of bounds",
2569 return isl_aff_free(aff));
2571 aff = isl_aff_cow(aff);
2572 if (!aff)
2573 return NULL;
2575 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2576 if (!aff->ls)
2577 return isl_aff_free(aff);
2579 first += 1 + isl_local_space_offset(aff->ls, type);
2580 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2581 if (!aff->v)
2582 return isl_aff_free(aff);
2584 return aff;
2587 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2588 enum isl_dim_type type, unsigned n)
2590 unsigned pos;
2592 pos = isl_aff_dim(aff, type);
2594 return isl_aff_insert_dims(aff, type, pos, n);
2597 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2598 enum isl_dim_type type, unsigned n)
2600 unsigned pos;
2602 pos = isl_pw_aff_dim(pwaff, type);
2604 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2607 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2608 * to dimensions of "dst_type" at "dst_pos".
2610 * We only support moving input dimensions to parameters and vice versa.
2612 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2613 enum isl_dim_type dst_type, unsigned dst_pos,
2614 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2616 unsigned g_dst_pos;
2617 unsigned g_src_pos;
2619 if (!aff)
2620 return NULL;
2621 if (n == 0 &&
2622 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2623 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2624 return aff;
2626 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2627 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2628 "cannot move output/set dimension",
2629 return isl_aff_free(aff));
2630 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2631 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2632 "cannot move divs", return isl_aff_free(aff));
2633 if (dst_type == isl_dim_in)
2634 dst_type = isl_dim_set;
2635 if (src_type == isl_dim_in)
2636 src_type = isl_dim_set;
2638 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2639 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2640 "range out of bounds", return isl_aff_free(aff));
2641 if (dst_type == src_type)
2642 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2643 "moving dims within the same type not supported",
2644 return isl_aff_free(aff));
2646 aff = isl_aff_cow(aff);
2647 if (!aff)
2648 return NULL;
2650 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2651 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2652 if (dst_type > src_type)
2653 g_dst_pos -= n;
2655 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2656 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2657 src_type, src_pos, n);
2658 if (!aff->v || !aff->ls)
2659 return isl_aff_free(aff);
2661 aff = sort_divs(aff);
2663 return aff;
2666 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2668 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2669 return isl_pw_aff_alloc(dom, aff);
2672 #define isl_aff_involves_nan isl_aff_is_nan
2674 #undef PW
2675 #define PW isl_pw_aff
2676 #undef EL
2677 #define EL isl_aff
2678 #undef EL_IS_ZERO
2679 #define EL_IS_ZERO is_empty
2680 #undef ZERO
2681 #define ZERO empty
2682 #undef IS_ZERO
2683 #define IS_ZERO is_empty
2684 #undef FIELD
2685 #define FIELD aff
2686 #undef DEFAULT_IS_ZERO
2687 #define DEFAULT_IS_ZERO 0
2689 #define NO_OPT
2690 #define NO_LIFT
2691 #define NO_MORPH
2693 #include <isl_pw_templ.c>
2694 #include <isl_pw_eval.c>
2695 #include <isl_pw_hash.c>
2696 #include <isl_pw_union_opt.c>
2698 #undef BASE
2699 #define BASE pw_aff
2701 #include <isl_union_single.c>
2702 #include <isl_union_neg.c>
2704 static __isl_give isl_set *align_params_pw_pw_set_and(
2705 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2706 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2707 __isl_take isl_pw_aff *pwaff2))
2709 isl_bool equal_params;
2711 if (!pwaff1 || !pwaff2)
2712 goto error;
2713 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2714 if (equal_params < 0)
2715 goto error;
2716 if (equal_params)
2717 return fn(pwaff1, pwaff2);
2718 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2719 isl_pw_aff_check_named_params(pwaff2) < 0)
2720 goto error;
2721 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2722 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2723 return fn(pwaff1, pwaff2);
2724 error:
2725 isl_pw_aff_free(pwaff1);
2726 isl_pw_aff_free(pwaff2);
2727 return NULL;
2730 /* Align the parameters of the to isl_pw_aff arguments and
2731 * then apply a function "fn" on them that returns an isl_map.
2733 static __isl_give isl_map *align_params_pw_pw_map_and(
2734 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2735 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2736 __isl_take isl_pw_aff *pa2))
2738 isl_bool equal_params;
2740 if (!pa1 || !pa2)
2741 goto error;
2742 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2743 if (equal_params < 0)
2744 goto error;
2745 if (equal_params)
2746 return fn(pa1, pa2);
2747 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2748 isl_pw_aff_check_named_params(pa2) < 0)
2749 goto error;
2750 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2751 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2752 return fn(pa1, pa2);
2753 error:
2754 isl_pw_aff_free(pa1);
2755 isl_pw_aff_free(pa2);
2756 return NULL;
2759 /* Compute a piecewise quasi-affine expression with a domain that
2760 * is the union of those of pwaff1 and pwaff2 and such that on each
2761 * cell, the quasi-affine expression is the maximum of those of pwaff1
2762 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2763 * cell, then the associated expression is the defined one.
2765 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2766 __isl_take isl_pw_aff *pwaff2)
2768 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2771 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2772 __isl_take isl_pw_aff *pwaff2)
2774 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2775 &pw_aff_union_max);
2778 /* Compute a piecewise quasi-affine expression with a domain that
2779 * is the union of those of pwaff1 and pwaff2 and such that on each
2780 * cell, the quasi-affine expression is the minimum of those of pwaff1
2781 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2782 * cell, then the associated expression is the defined one.
2784 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2785 __isl_take isl_pw_aff *pwaff2)
2787 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2790 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2791 __isl_take isl_pw_aff *pwaff2)
2793 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2794 &pw_aff_union_min);
2797 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2798 __isl_take isl_pw_aff *pwaff2, int max)
2800 if (max)
2801 return isl_pw_aff_union_max(pwaff1, pwaff2);
2802 else
2803 return isl_pw_aff_union_min(pwaff1, pwaff2);
2806 /* Construct a map with as domain the domain of pwaff and
2807 * one-dimensional range corresponding to the affine expressions.
2809 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2811 int i;
2812 isl_space *dim;
2813 isl_map *map;
2815 if (!pwaff)
2816 return NULL;
2818 dim = isl_pw_aff_get_space(pwaff);
2819 map = isl_map_empty(dim);
2821 for (i = 0; i < pwaff->n; ++i) {
2822 isl_basic_map *bmap;
2823 isl_map *map_i;
2825 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2826 map_i = isl_map_from_basic_map(bmap);
2827 map_i = isl_map_intersect_domain(map_i,
2828 isl_set_copy(pwaff->p[i].set));
2829 map = isl_map_union_disjoint(map, map_i);
2832 isl_pw_aff_free(pwaff);
2834 return map;
2837 /* Construct a map with as domain the domain of pwaff and
2838 * one-dimensional range corresponding to the affine expressions.
2840 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2842 if (!pwaff)
2843 return NULL;
2844 if (isl_space_is_set(pwaff->dim))
2845 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2846 "space of input is not a map", goto error);
2847 return map_from_pw_aff(pwaff);
2848 error:
2849 isl_pw_aff_free(pwaff);
2850 return NULL;
2853 /* Construct a one-dimensional set with as parameter domain
2854 * the domain of pwaff and the single set dimension
2855 * corresponding to the affine expressions.
2857 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2859 if (!pwaff)
2860 return NULL;
2861 if (!isl_space_is_set(pwaff->dim))
2862 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2863 "space of input is not a set", goto error);
2864 return map_from_pw_aff(pwaff);
2865 error:
2866 isl_pw_aff_free(pwaff);
2867 return NULL;
2870 /* Return a set containing those elements in the domain
2871 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2872 * does not satisfy "fn" (if complement is 1).
2874 * The pieces with a NaN never belong to the result since
2875 * NaN does not satisfy any property.
2877 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2878 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2879 int complement)
2881 int i;
2882 isl_set *set;
2884 if (!pwaff)
2885 return NULL;
2887 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2889 for (i = 0; i < pwaff->n; ++i) {
2890 isl_basic_set *bset;
2891 isl_set *set_i, *locus;
2892 isl_bool rational;
2894 if (isl_aff_is_nan(pwaff->p[i].aff))
2895 continue;
2897 rational = isl_set_has_rational(pwaff->p[i].set);
2898 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2899 locus = isl_set_from_basic_set(bset);
2900 set_i = isl_set_copy(pwaff->p[i].set);
2901 if (complement)
2902 set_i = isl_set_subtract(set_i, locus);
2903 else
2904 set_i = isl_set_intersect(set_i, locus);
2905 set = isl_set_union_disjoint(set, set_i);
2908 isl_pw_aff_free(pwaff);
2910 return set;
2913 /* Return a set containing those elements in the domain
2914 * of "pa" where it is positive.
2916 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2918 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2921 /* Return a set containing those elements in the domain
2922 * of pwaff where it is non-negative.
2924 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2926 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2929 /* Return a set containing those elements in the domain
2930 * of pwaff where it is zero.
2932 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2934 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2937 /* Return a set containing those elements in the domain
2938 * of pwaff where it is not zero.
2940 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2942 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2945 /* Return a set containing those elements in the shared domain
2946 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2948 * We compute the difference on the shared domain and then construct
2949 * the set of values where this difference is non-negative.
2950 * If strict is set, we first subtract 1 from the difference.
2951 * If equal is set, we only return the elements where pwaff1 and pwaff2
2952 * are equal.
2954 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2955 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2957 isl_set *set1, *set2;
2959 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2960 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2961 set1 = isl_set_intersect(set1, set2);
2962 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2963 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2964 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2966 if (strict) {
2967 isl_space *dim = isl_set_get_space(set1);
2968 isl_aff *aff;
2969 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2970 aff = isl_aff_add_constant_si(aff, -1);
2971 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2972 } else
2973 isl_set_free(set1);
2975 if (equal)
2976 return isl_pw_aff_zero_set(pwaff1);
2977 return isl_pw_aff_nonneg_set(pwaff1);
2980 /* Return a set containing those elements in the shared domain
2981 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2983 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2984 __isl_take isl_pw_aff *pwaff2)
2986 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2989 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2990 __isl_take isl_pw_aff *pwaff2)
2992 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2995 /* Return a set containing those elements in the shared domain
2996 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2998 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2999 __isl_take isl_pw_aff *pwaff2)
3001 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3004 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3005 __isl_take isl_pw_aff *pwaff2)
3007 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
3010 /* Return a set containing those elements in the shared domain
3011 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3013 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3014 __isl_take isl_pw_aff *pwaff2)
3016 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3019 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3020 __isl_take isl_pw_aff *pwaff2)
3022 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
3025 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3026 __isl_take isl_pw_aff *pwaff2)
3028 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3031 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3032 __isl_take isl_pw_aff *pwaff2)
3034 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3037 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3038 * where the function values are ordered in the same way as "order",
3039 * which returns a set in the shared domain of its two arguments.
3040 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3042 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3043 * We first pull back the two functions such that they are defined on
3044 * the domain [A -> B]. Then we apply "order", resulting in a set
3045 * in the space [A -> B]. Finally, we unwrap this set to obtain
3046 * a map in the space A -> B.
3048 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3049 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3050 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3051 __isl_take isl_pw_aff *pa2))
3053 isl_space *space1, *space2;
3054 isl_multi_aff *ma;
3055 isl_set *set;
3057 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3058 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3059 space1 = isl_space_map_from_domain_and_range(space1, space2);
3060 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3061 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3062 ma = isl_multi_aff_range_map(space1);
3063 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3064 set = order(pa1, pa2);
3066 return isl_set_unwrap(set);
3069 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3070 * where the function values are equal.
3071 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3073 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3074 __isl_take isl_pw_aff *pa2)
3076 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3079 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3080 * where the function values are equal.
3082 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3083 __isl_take isl_pw_aff *pa2)
3085 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3088 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3089 * where the function value of "pa1" is less than the function value of "pa2".
3090 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3092 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3093 __isl_take isl_pw_aff *pa2)
3095 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3098 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3099 * where the function value of "pa1" is less than the function value of "pa2".
3101 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3102 __isl_take isl_pw_aff *pa2)
3104 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3107 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3108 * where the function value of "pa1" is greater than the function value
3109 * of "pa2".
3110 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3112 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3113 __isl_take isl_pw_aff *pa2)
3115 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3118 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3119 * where the function value of "pa1" is greater than the function value
3120 * of "pa2".
3122 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3123 __isl_take isl_pw_aff *pa2)
3125 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3128 /* Return a set containing those elements in the shared domain
3129 * of the elements of list1 and list2 where each element in list1
3130 * has the relation specified by "fn" with each element in list2.
3132 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3133 __isl_take isl_pw_aff_list *list2,
3134 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3135 __isl_take isl_pw_aff *pwaff2))
3137 int i, j;
3138 isl_ctx *ctx;
3139 isl_set *set;
3141 if (!list1 || !list2)
3142 goto error;
3144 ctx = isl_pw_aff_list_get_ctx(list1);
3145 if (list1->n < 1 || list2->n < 1)
3146 isl_die(ctx, isl_error_invalid,
3147 "list should contain at least one element", goto error);
3149 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3150 for (i = 0; i < list1->n; ++i)
3151 for (j = 0; j < list2->n; ++j) {
3152 isl_set *set_ij;
3154 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3155 isl_pw_aff_copy(list2->p[j]));
3156 set = isl_set_intersect(set, set_ij);
3159 isl_pw_aff_list_free(list1);
3160 isl_pw_aff_list_free(list2);
3161 return set;
3162 error:
3163 isl_pw_aff_list_free(list1);
3164 isl_pw_aff_list_free(list2);
3165 return NULL;
3168 /* Return a set containing those elements in the shared domain
3169 * of the elements of list1 and list2 where each element in list1
3170 * is equal to each element in list2.
3172 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3173 __isl_take isl_pw_aff_list *list2)
3175 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3178 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3179 __isl_take isl_pw_aff_list *list2)
3181 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3184 /* Return a set containing those elements in the shared domain
3185 * of the elements of list1 and list2 where each element in list1
3186 * is less than or equal to each element in list2.
3188 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3189 __isl_take isl_pw_aff_list *list2)
3191 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3194 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3195 __isl_take isl_pw_aff_list *list2)
3197 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3200 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3201 __isl_take isl_pw_aff_list *list2)
3203 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3206 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3207 __isl_take isl_pw_aff_list *list2)
3209 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3213 /* Return a set containing those elements in the shared domain
3214 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3216 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3217 __isl_take isl_pw_aff *pwaff2)
3219 isl_set *set_lt, *set_gt;
3221 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3222 isl_pw_aff_copy(pwaff2));
3223 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3224 return isl_set_union_disjoint(set_lt, set_gt);
3227 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3228 __isl_take isl_pw_aff *pwaff2)
3230 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3233 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3234 isl_int v)
3236 int i;
3238 if (isl_int_is_one(v))
3239 return pwaff;
3240 if (!isl_int_is_pos(v))
3241 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3242 "factor needs to be positive",
3243 return isl_pw_aff_free(pwaff));
3244 pwaff = isl_pw_aff_cow(pwaff);
3245 if (!pwaff)
3246 return NULL;
3247 if (pwaff->n == 0)
3248 return pwaff;
3250 for (i = 0; i < pwaff->n; ++i) {
3251 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3252 if (!pwaff->p[i].aff)
3253 return isl_pw_aff_free(pwaff);
3256 return pwaff;
3259 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3261 int i;
3263 pwaff = isl_pw_aff_cow(pwaff);
3264 if (!pwaff)
3265 return NULL;
3266 if (pwaff->n == 0)
3267 return pwaff;
3269 for (i = 0; i < pwaff->n; ++i) {
3270 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3271 if (!pwaff->p[i].aff)
3272 return isl_pw_aff_free(pwaff);
3275 return pwaff;
3278 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3280 int i;
3282 pwaff = isl_pw_aff_cow(pwaff);
3283 if (!pwaff)
3284 return NULL;
3285 if (pwaff->n == 0)
3286 return pwaff;
3288 for (i = 0; i < pwaff->n; ++i) {
3289 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3290 if (!pwaff->p[i].aff)
3291 return isl_pw_aff_free(pwaff);
3294 return pwaff;
3297 /* Assuming that "cond1" and "cond2" are disjoint,
3298 * return an affine expression that is equal to pwaff1 on cond1
3299 * and to pwaff2 on cond2.
3301 static __isl_give isl_pw_aff *isl_pw_aff_select(
3302 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3303 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3305 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3306 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3308 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3311 /* Return an affine expression that is equal to pwaff_true for elements
3312 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3313 * is zero.
3314 * That is, return cond ? pwaff_true : pwaff_false;
3316 * If "cond" involves and NaN, then we conservatively return a NaN
3317 * on its entire domain. In principle, we could consider the pieces
3318 * where it is NaN separately from those where it is not.
3320 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3321 * then only use the domain of "cond" to restrict the domain.
3323 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3324 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3326 isl_set *cond_true, *cond_false;
3327 isl_bool equal;
3329 if (!cond)
3330 goto error;
3331 if (isl_pw_aff_involves_nan(cond)) {
3332 isl_space *space = isl_pw_aff_get_domain_space(cond);
3333 isl_local_space *ls = isl_local_space_from_space(space);
3334 isl_pw_aff_free(cond);
3335 isl_pw_aff_free(pwaff_true);
3336 isl_pw_aff_free(pwaff_false);
3337 return isl_pw_aff_nan_on_domain(ls);
3340 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3341 isl_pw_aff_get_space(pwaff_false));
3342 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3343 isl_pw_aff_get_space(pwaff_true));
3344 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3345 if (equal < 0)
3346 goto error;
3347 if (equal) {
3348 isl_set *dom;
3350 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3351 isl_pw_aff_free(pwaff_false);
3352 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3355 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3356 cond_false = isl_pw_aff_zero_set(cond);
3357 return isl_pw_aff_select(cond_true, pwaff_true,
3358 cond_false, pwaff_false);
3359 error:
3360 isl_pw_aff_free(cond);
3361 isl_pw_aff_free(pwaff_true);
3362 isl_pw_aff_free(pwaff_false);
3363 return NULL;
3366 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3368 if (!aff)
3369 return isl_bool_error;
3371 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3374 /* Check whether pwaff is a piecewise constant.
3376 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3378 int i;
3380 if (!pwaff)
3381 return isl_bool_error;
3383 for (i = 0; i < pwaff->n; ++i) {
3384 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3385 if (is_cst < 0 || !is_cst)
3386 return is_cst;
3389 return isl_bool_true;
3392 /* Are all elements of "mpa" piecewise constants?
3394 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3396 int i;
3398 if (!mpa)
3399 return isl_bool_error;
3401 for (i = 0; i < mpa->n; ++i) {
3402 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3403 if (is_cst < 0 || !is_cst)
3404 return is_cst;
3407 return isl_bool_true;
3410 /* Return the product of "aff1" and "aff2".
3412 * If either of the two is NaN, then the result is NaN.
3414 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3416 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3417 __isl_take isl_aff *aff2)
3419 if (!aff1 || !aff2)
3420 goto error;
3422 if (isl_aff_is_nan(aff1)) {
3423 isl_aff_free(aff2);
3424 return aff1;
3426 if (isl_aff_is_nan(aff2)) {
3427 isl_aff_free(aff1);
3428 return aff2;
3431 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3432 return isl_aff_mul(aff2, aff1);
3434 if (!isl_aff_is_cst(aff2))
3435 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3436 "at least one affine expression should be constant",
3437 goto error);
3439 aff1 = isl_aff_cow(aff1);
3440 if (!aff1 || !aff2)
3441 goto error;
3443 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3444 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3446 isl_aff_free(aff2);
3447 return aff1;
3448 error:
3449 isl_aff_free(aff1);
3450 isl_aff_free(aff2);
3451 return NULL;
3454 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3456 * If either of the two is NaN, then the result is NaN.
3458 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3459 __isl_take isl_aff *aff2)
3461 int is_cst;
3462 int neg;
3464 if (!aff1 || !aff2)
3465 goto error;
3467 if (isl_aff_is_nan(aff1)) {
3468 isl_aff_free(aff2);
3469 return aff1;
3471 if (isl_aff_is_nan(aff2)) {
3472 isl_aff_free(aff1);
3473 return aff2;
3476 is_cst = isl_aff_is_cst(aff2);
3477 if (is_cst < 0)
3478 goto error;
3479 if (!is_cst)
3480 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3481 "second argument should be a constant", goto error);
3483 if (!aff2)
3484 goto error;
3486 neg = isl_int_is_neg(aff2->v->el[1]);
3487 if (neg) {
3488 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3489 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3492 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3493 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3495 if (neg) {
3496 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3497 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3500 isl_aff_free(aff2);
3501 return aff1;
3502 error:
3503 isl_aff_free(aff1);
3504 isl_aff_free(aff2);
3505 return NULL;
3508 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3509 __isl_take isl_pw_aff *pwaff2)
3511 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3514 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3515 __isl_take isl_pw_aff *pwaff2)
3517 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3520 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3521 __isl_take isl_pw_aff *pwaff2)
3523 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3526 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3527 __isl_take isl_pw_aff *pwaff2)
3529 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3532 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3533 __isl_take isl_pw_aff *pwaff2)
3535 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3538 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3539 __isl_take isl_pw_aff *pa2)
3541 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
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 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3559 error:
3560 isl_pw_aff_free(pa1);
3561 isl_pw_aff_free(pa2);
3562 return NULL;
3565 /* Compute the quotient of the integer division of "pa1" by "pa2"
3566 * with rounding towards zero.
3567 * "pa2" is assumed to be a piecewise constant.
3569 * In particular, return
3571 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3574 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3575 __isl_take isl_pw_aff *pa2)
3577 int is_cst;
3578 isl_set *cond;
3579 isl_pw_aff *f, *c;
3581 is_cst = isl_pw_aff_is_cst(pa2);
3582 if (is_cst < 0)
3583 goto error;
3584 if (!is_cst)
3585 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3586 "second argument should be a piecewise constant",
3587 goto error);
3589 pa1 = isl_pw_aff_div(pa1, pa2);
3591 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3592 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3593 c = isl_pw_aff_ceil(pa1);
3594 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3595 error:
3596 isl_pw_aff_free(pa1);
3597 isl_pw_aff_free(pa2);
3598 return NULL;
3601 /* Compute the remainder of the integer division of "pa1" by "pa2"
3602 * with rounding towards zero.
3603 * "pa2" is assumed to be a piecewise constant.
3605 * In particular, return
3607 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3610 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3611 __isl_take isl_pw_aff *pa2)
3613 int is_cst;
3614 isl_pw_aff *res;
3616 is_cst = isl_pw_aff_is_cst(pa2);
3617 if (is_cst < 0)
3618 goto error;
3619 if (!is_cst)
3620 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3621 "second argument should be a piecewise constant",
3622 goto error);
3623 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3624 res = isl_pw_aff_mul(pa2, res);
3625 res = isl_pw_aff_sub(pa1, res);
3626 return res;
3627 error:
3628 isl_pw_aff_free(pa1);
3629 isl_pw_aff_free(pa2);
3630 return NULL;
3633 /* Does either of "pa1" or "pa2" involve any NaN2?
3635 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3636 __isl_keep isl_pw_aff *pa2)
3638 isl_bool has_nan;
3640 has_nan = isl_pw_aff_involves_nan(pa1);
3641 if (has_nan < 0 || has_nan)
3642 return has_nan;
3643 return isl_pw_aff_involves_nan(pa2);
3646 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3647 * by a NaN on their shared domain.
3649 * In principle, the result could be refined to only being NaN
3650 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3652 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3653 __isl_take isl_pw_aff *pa2)
3655 isl_local_space *ls;
3656 isl_set *dom;
3657 isl_pw_aff *pa;
3659 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3660 ls = isl_local_space_from_space(isl_set_get_space(dom));
3661 pa = isl_pw_aff_nan_on_domain(ls);
3662 pa = isl_pw_aff_intersect_domain(pa, dom);
3664 return pa;
3667 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3668 __isl_take isl_pw_aff *pwaff2)
3670 isl_set *le;
3671 isl_set *dom;
3673 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3674 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3675 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3676 isl_pw_aff_copy(pwaff2));
3677 dom = isl_set_subtract(dom, isl_set_copy(le));
3678 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3681 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3682 __isl_take isl_pw_aff *pwaff2)
3684 isl_set *ge;
3685 isl_set *dom;
3687 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3688 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3689 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3690 isl_pw_aff_copy(pwaff2));
3691 dom = isl_set_subtract(dom, isl_set_copy(ge));
3692 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3695 /* Return an expression for the minimum (if "max" is not set) or
3696 * the maximum (if "max" is set) of "pa1" and "pa2".
3697 * If either expression involves any NaN, then return a NaN
3698 * on the shared domain as result.
3700 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3701 __isl_take isl_pw_aff *pa2, int max)
3703 isl_bool has_nan;
3705 has_nan = either_involves_nan(pa1, pa2);
3706 if (has_nan < 0)
3707 pa1 = isl_pw_aff_free(pa1);
3708 else if (has_nan)
3709 return replace_by_nan(pa1, pa2);
3711 if (max)
3712 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3713 else
3714 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3717 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3719 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3720 __isl_take isl_pw_aff *pwaff2)
3722 return pw_aff_min_max(pwaff1, pwaff2, 0);
3725 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3727 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3728 __isl_take isl_pw_aff *pwaff2)
3730 return pw_aff_min_max(pwaff1, pwaff2, 1);
3733 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3734 __isl_take isl_pw_aff_list *list,
3735 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3736 __isl_take isl_pw_aff *pwaff2))
3738 int i;
3739 isl_ctx *ctx;
3740 isl_pw_aff *res;
3742 if (!list)
3743 return NULL;
3745 ctx = isl_pw_aff_list_get_ctx(list);
3746 if (list->n < 1)
3747 isl_die(ctx, isl_error_invalid,
3748 "list should contain at least one element", goto error);
3750 res = isl_pw_aff_copy(list->p[0]);
3751 for (i = 1; i < list->n; ++i)
3752 res = fn(res, isl_pw_aff_copy(list->p[i]));
3754 isl_pw_aff_list_free(list);
3755 return res;
3756 error:
3757 isl_pw_aff_list_free(list);
3758 return NULL;
3761 /* Return an isl_pw_aff that maps each element in the intersection of the
3762 * domains of the elements of list to the minimal corresponding affine
3763 * expression.
3765 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3767 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3770 /* Return an isl_pw_aff that maps each element in the intersection of the
3771 * domains of the elements of list to the maximal corresponding affine
3772 * expression.
3774 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3776 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3779 /* Mark the domains of "pwaff" as rational.
3781 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3783 int i;
3785 pwaff = isl_pw_aff_cow(pwaff);
3786 if (!pwaff)
3787 return NULL;
3788 if (pwaff->n == 0)
3789 return pwaff;
3791 for (i = 0; i < pwaff->n; ++i) {
3792 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3793 if (!pwaff->p[i].set)
3794 return isl_pw_aff_free(pwaff);
3797 return pwaff;
3800 /* Mark the domains of the elements of "list" as rational.
3802 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3803 __isl_take isl_pw_aff_list *list)
3805 int i, n;
3807 if (!list)
3808 return NULL;
3809 if (list->n == 0)
3810 return list;
3812 n = list->n;
3813 for (i = 0; i < n; ++i) {
3814 isl_pw_aff *pa;
3816 pa = isl_pw_aff_list_get_pw_aff(list, i);
3817 pa = isl_pw_aff_set_rational(pa);
3818 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3821 return list;
3824 /* Do the parameters of "aff" match those of "space"?
3826 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3827 __isl_keep isl_space *space)
3829 isl_space *aff_space;
3830 isl_bool match;
3832 if (!aff || !space)
3833 return isl_bool_error;
3835 aff_space = isl_aff_get_domain_space(aff);
3837 match = isl_space_has_equal_params(space, aff_space);
3839 isl_space_free(aff_space);
3840 return match;
3843 /* Check that the domain space of "aff" matches "space".
3845 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3846 __isl_keep isl_space *space)
3848 isl_space *aff_space;
3849 isl_bool match;
3851 if (!aff || !space)
3852 return isl_stat_error;
3854 aff_space = isl_aff_get_domain_space(aff);
3856 match = isl_space_has_equal_params(space, aff_space);
3857 if (match < 0)
3858 goto error;
3859 if (!match)
3860 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3861 "parameters don't match", goto error);
3862 match = isl_space_tuple_is_equal(space, isl_dim_in,
3863 aff_space, isl_dim_set);
3864 if (match < 0)
3865 goto error;
3866 if (!match)
3867 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3868 "domains don't match", goto error);
3869 isl_space_free(aff_space);
3870 return isl_stat_ok;
3871 error:
3872 isl_space_free(aff_space);
3873 return isl_stat_error;
3876 #undef BASE
3877 #define BASE aff
3878 #undef DOMBASE
3879 #define DOMBASE set
3880 #define NO_DOMAIN
3882 #include <isl_multi_no_explicit_domain.c>
3883 #include <isl_multi_templ.c>
3884 #include <isl_multi_apply_set.c>
3885 #include <isl_multi_cmp.c>
3886 #include <isl_multi_dims.c>
3887 #include <isl_multi_floor.c>
3888 #include <isl_multi_gist.c>
3890 #undef NO_DOMAIN
3892 /* Construct an isl_multi_aff living in "space" that corresponds
3893 * to the affine transformation matrix "mat".
3895 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3896 __isl_take isl_space *space, __isl_take isl_mat *mat)
3898 isl_ctx *ctx;
3899 isl_local_space *ls = NULL;
3900 isl_multi_aff *ma = NULL;
3901 int n_row, n_col, n_out, total;
3902 int i;
3904 if (!space || !mat)
3905 goto error;
3907 ctx = isl_mat_get_ctx(mat);
3909 n_row = isl_mat_rows(mat);
3910 n_col = isl_mat_cols(mat);
3911 if (n_row < 1)
3912 isl_die(ctx, isl_error_invalid,
3913 "insufficient number of rows", goto error);
3914 if (n_col < 1)
3915 isl_die(ctx, isl_error_invalid,
3916 "insufficient number of columns", goto error);
3917 n_out = isl_space_dim(space, isl_dim_out);
3918 total = isl_space_dim(space, isl_dim_all);
3919 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3920 isl_die(ctx, isl_error_invalid,
3921 "dimension mismatch", goto error);
3923 ma = isl_multi_aff_zero(isl_space_copy(space));
3924 ls = isl_local_space_from_space(isl_space_domain(space));
3926 for (i = 0; i < n_row - 1; ++i) {
3927 isl_vec *v;
3928 isl_aff *aff;
3930 v = isl_vec_alloc(ctx, 1 + n_col);
3931 if (!v)
3932 goto error;
3933 isl_int_set(v->el[0], mat->row[0][0]);
3934 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3935 v = isl_vec_normalize(v);
3936 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3937 ma = isl_multi_aff_set_aff(ma, i, aff);
3940 isl_local_space_free(ls);
3941 isl_mat_free(mat);
3942 return ma;
3943 error:
3944 isl_local_space_free(ls);
3945 isl_mat_free(mat);
3946 isl_multi_aff_free(ma);
3947 return NULL;
3950 /* Remove any internal structure of the domain of "ma".
3951 * If there is any such internal structure in the input,
3952 * then the name of the corresponding space is also removed.
3954 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3955 __isl_take isl_multi_aff *ma)
3957 isl_space *space;
3959 if (!ma)
3960 return NULL;
3962 if (!ma->space->nested[0])
3963 return ma;
3965 space = isl_multi_aff_get_space(ma);
3966 space = isl_space_flatten_domain(space);
3967 ma = isl_multi_aff_reset_space(ma, space);
3969 return ma;
3972 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3973 * of the space to its domain.
3975 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3977 int i, n_in;
3978 isl_local_space *ls;
3979 isl_multi_aff *ma;
3981 if (!space)
3982 return NULL;
3983 if (!isl_space_is_map(space))
3984 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3985 "not a map space", goto error);
3987 n_in = isl_space_dim(space, isl_dim_in);
3988 space = isl_space_domain_map(space);
3990 ma = isl_multi_aff_alloc(isl_space_copy(space));
3991 if (n_in == 0) {
3992 isl_space_free(space);
3993 return ma;
3996 space = isl_space_domain(space);
3997 ls = isl_local_space_from_space(space);
3998 for (i = 0; i < n_in; ++i) {
3999 isl_aff *aff;
4001 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4002 isl_dim_set, i);
4003 ma = isl_multi_aff_set_aff(ma, i, aff);
4005 isl_local_space_free(ls);
4006 return ma;
4007 error:
4008 isl_space_free(space);
4009 return NULL;
4012 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4013 * of the space to its range.
4015 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4017 int i, n_in, n_out;
4018 isl_local_space *ls;
4019 isl_multi_aff *ma;
4021 if (!space)
4022 return NULL;
4023 if (!isl_space_is_map(space))
4024 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4025 "not a map space", goto error);
4027 n_in = isl_space_dim(space, isl_dim_in);
4028 n_out = isl_space_dim(space, isl_dim_out);
4029 space = isl_space_range_map(space);
4031 ma = isl_multi_aff_alloc(isl_space_copy(space));
4032 if (n_out == 0) {
4033 isl_space_free(space);
4034 return ma;
4037 space = isl_space_domain(space);
4038 ls = isl_local_space_from_space(space);
4039 for (i = 0; i < n_out; ++i) {
4040 isl_aff *aff;
4042 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4043 isl_dim_set, n_in + i);
4044 ma = isl_multi_aff_set_aff(ma, i, aff);
4046 isl_local_space_free(ls);
4047 return ma;
4048 error:
4049 isl_space_free(space);
4050 return NULL;
4053 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4054 * of the space to its range.
4056 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4057 __isl_take isl_space *space)
4059 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4062 /* Given the space of a set and a range of set dimensions,
4063 * construct an isl_multi_aff that projects out those dimensions.
4065 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4066 __isl_take isl_space *space, enum isl_dim_type type,
4067 unsigned first, unsigned n)
4069 int i, dim;
4070 isl_local_space *ls;
4071 isl_multi_aff *ma;
4073 if (!space)
4074 return NULL;
4075 if (!isl_space_is_set(space))
4076 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4077 "expecting set space", goto error);
4078 if (type != isl_dim_set)
4079 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4080 "only set dimensions can be projected out", goto error);
4082 dim = isl_space_dim(space, isl_dim_set);
4083 if (first + n > dim)
4084 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4085 "range out of bounds", goto error);
4087 space = isl_space_from_domain(space);
4088 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4090 if (dim == n)
4091 return isl_multi_aff_alloc(space);
4093 ma = isl_multi_aff_alloc(isl_space_copy(space));
4094 space = isl_space_domain(space);
4095 ls = isl_local_space_from_space(space);
4097 for (i = 0; i < first; ++i) {
4098 isl_aff *aff;
4100 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4101 isl_dim_set, i);
4102 ma = isl_multi_aff_set_aff(ma, i, aff);
4105 for (i = 0; i < dim - (first + n); ++i) {
4106 isl_aff *aff;
4108 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4109 isl_dim_set, first + n + i);
4110 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4113 isl_local_space_free(ls);
4114 return ma;
4115 error:
4116 isl_space_free(space);
4117 return NULL;
4120 /* Given the space of a set and a range of set dimensions,
4121 * construct an isl_pw_multi_aff that projects out those dimensions.
4123 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4124 __isl_take isl_space *space, enum isl_dim_type type,
4125 unsigned first, unsigned n)
4127 isl_multi_aff *ma;
4129 ma = isl_multi_aff_project_out_map(space, type, first, n);
4130 return isl_pw_multi_aff_from_multi_aff(ma);
4133 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4134 * domain.
4136 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4137 __isl_take isl_multi_aff *ma)
4139 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4140 return isl_pw_multi_aff_alloc(dom, ma);
4143 /* Create a piecewise multi-affine expression in the given space that maps each
4144 * input dimension to the corresponding output dimension.
4146 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4147 __isl_take isl_space *space)
4149 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4152 /* Exploit the equalities in "eq" to simplify the affine expressions.
4154 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4155 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4157 int i;
4159 maff = isl_multi_aff_cow(maff);
4160 if (!maff || !eq)
4161 goto error;
4163 for (i = 0; i < maff->n; ++i) {
4164 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4165 isl_basic_set_copy(eq));
4166 if (!maff->u.p[i])
4167 goto error;
4170 isl_basic_set_free(eq);
4171 return maff;
4172 error:
4173 isl_basic_set_free(eq);
4174 isl_multi_aff_free(maff);
4175 return NULL;
4178 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4179 isl_int f)
4181 int i;
4183 maff = isl_multi_aff_cow(maff);
4184 if (!maff)
4185 return NULL;
4187 for (i = 0; i < maff->n; ++i) {
4188 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4189 if (!maff->u.p[i])
4190 return isl_multi_aff_free(maff);
4193 return maff;
4196 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4197 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4199 maff1 = isl_multi_aff_add(maff1, maff2);
4200 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4201 return maff1;
4204 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4206 if (!maff)
4207 return -1;
4209 return 0;
4212 /* Return the set of domain elements where "ma1" is lexicographically
4213 * smaller than or equal to "ma2".
4215 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4216 __isl_take isl_multi_aff *ma2)
4218 return isl_multi_aff_lex_ge_set(ma2, ma1);
4221 /* Return the set of domain elements where "ma1" is lexicographically
4222 * smaller than "ma2".
4224 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4225 __isl_take isl_multi_aff *ma2)
4227 return isl_multi_aff_lex_gt_set(ma2, ma1);
4230 /* Return the set of domain elements where "ma1" and "ma2"
4231 * satisfy "order".
4233 static __isl_give isl_set *isl_multi_aff_order_set(
4234 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4235 __isl_give isl_map *order(__isl_take isl_space *set_space))
4237 isl_space *space;
4238 isl_map *map1, *map2;
4239 isl_map *map, *ge;
4241 map1 = isl_map_from_multi_aff(ma1);
4242 map2 = isl_map_from_multi_aff(ma2);
4243 map = isl_map_range_product(map1, map2);
4244 space = isl_space_range(isl_map_get_space(map));
4245 space = isl_space_domain(isl_space_unwrap(space));
4246 ge = order(space);
4247 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4249 return isl_map_domain(map);
4252 /* Return the set of domain elements where "ma1" is lexicographically
4253 * greater than or equal to "ma2".
4255 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4256 __isl_take isl_multi_aff *ma2)
4258 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4261 /* Return the set of domain elements where "ma1" is lexicographically
4262 * greater than "ma2".
4264 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4265 __isl_take isl_multi_aff *ma2)
4267 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4270 #undef PW
4271 #define PW isl_pw_multi_aff
4272 #undef EL
4273 #define EL isl_multi_aff
4274 #undef EL_IS_ZERO
4275 #define EL_IS_ZERO is_empty
4276 #undef ZERO
4277 #define ZERO empty
4278 #undef IS_ZERO
4279 #define IS_ZERO is_empty
4280 #undef FIELD
4281 #define FIELD maff
4282 #undef DEFAULT_IS_ZERO
4283 #define DEFAULT_IS_ZERO 0
4285 #define NO_SUB
4286 #define NO_OPT
4287 #define NO_INSERT_DIMS
4288 #define NO_LIFT
4289 #define NO_MORPH
4291 #include <isl_pw_templ.c>
4292 #include <isl_pw_union_opt.c>
4294 #undef NO_SUB
4296 #undef BASE
4297 #define BASE pw_multi_aff
4299 #include <isl_union_multi.c>
4300 #include <isl_union_neg.c>
4302 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4303 __isl_take isl_pw_multi_aff *pma1,
4304 __isl_take isl_pw_multi_aff *pma2)
4306 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4307 &isl_multi_aff_lex_ge_set);
4310 /* Given two piecewise multi affine expressions, return a piecewise
4311 * multi-affine expression defined on the union of the definition domains
4312 * of the inputs that is equal to the lexicographic maximum of the two
4313 * inputs on each cell. If only one of the two inputs is defined on
4314 * a given cell, then it is considered to be the maximum.
4316 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4317 __isl_take isl_pw_multi_aff *pma1,
4318 __isl_take isl_pw_multi_aff *pma2)
4320 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4321 &pw_multi_aff_union_lexmax);
4324 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4325 __isl_take isl_pw_multi_aff *pma1,
4326 __isl_take isl_pw_multi_aff *pma2)
4328 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4329 &isl_multi_aff_lex_le_set);
4332 /* Given two piecewise multi affine expressions, return a piecewise
4333 * multi-affine expression defined on the union of the definition domains
4334 * of the inputs that is equal to the lexicographic minimum of the two
4335 * inputs on each cell. If only one of the two inputs is defined on
4336 * a given cell, then it is considered to be the minimum.
4338 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4339 __isl_take isl_pw_multi_aff *pma1,
4340 __isl_take isl_pw_multi_aff *pma2)
4342 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4343 &pw_multi_aff_union_lexmin);
4346 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4347 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4349 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4350 &isl_multi_aff_add);
4353 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4354 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4356 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4357 &pw_multi_aff_add);
4360 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4361 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4363 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4364 &isl_multi_aff_sub);
4367 /* Subtract "pma2" from "pma1" and return the result.
4369 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4370 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4372 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4373 &pw_multi_aff_sub);
4376 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4377 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4379 return isl_pw_multi_aff_union_add_(pma1, pma2);
4382 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4383 * with the actual sum on the shared domain and
4384 * the defined expression on the symmetric difference of the domains.
4386 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4387 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4389 return isl_union_pw_aff_union_add_(upa1, upa2);
4392 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4393 * with the actual sum on the shared domain and
4394 * the defined expression on the symmetric difference of the domains.
4396 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4397 __isl_take isl_union_pw_multi_aff *upma1,
4398 __isl_take isl_union_pw_multi_aff *upma2)
4400 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4403 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4404 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4406 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4407 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4409 int i, j, n;
4410 isl_space *space;
4411 isl_pw_multi_aff *res;
4413 if (!pma1 || !pma2)
4414 goto error;
4416 n = pma1->n * pma2->n;
4417 space = isl_space_product(isl_space_copy(pma1->dim),
4418 isl_space_copy(pma2->dim));
4419 res = isl_pw_multi_aff_alloc_size(space, n);
4421 for (i = 0; i < pma1->n; ++i) {
4422 for (j = 0; j < pma2->n; ++j) {
4423 isl_set *domain;
4424 isl_multi_aff *ma;
4426 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4427 isl_set_copy(pma2->p[j].set));
4428 ma = isl_multi_aff_product(
4429 isl_multi_aff_copy(pma1->p[i].maff),
4430 isl_multi_aff_copy(pma2->p[j].maff));
4431 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4435 isl_pw_multi_aff_free(pma1);
4436 isl_pw_multi_aff_free(pma2);
4437 return res;
4438 error:
4439 isl_pw_multi_aff_free(pma1);
4440 isl_pw_multi_aff_free(pma2);
4441 return NULL;
4444 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4445 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4447 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4448 &pw_multi_aff_product);
4451 /* Construct a map mapping the domain of the piecewise multi-affine expression
4452 * to its range, with each dimension in the range equated to the
4453 * corresponding affine expression on its cell.
4455 * If the domain of "pma" is rational, then so is the constructed "map".
4457 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4459 int i;
4460 isl_map *map;
4462 if (!pma)
4463 return NULL;
4465 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4467 for (i = 0; i < pma->n; ++i) {
4468 isl_bool rational;
4469 isl_multi_aff *maff;
4470 isl_basic_map *bmap;
4471 isl_map *map_i;
4473 rational = isl_set_is_rational(pma->p[i].set);
4474 if (rational < 0)
4475 map = isl_map_free(map);
4476 maff = isl_multi_aff_copy(pma->p[i].maff);
4477 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4478 map_i = isl_map_from_basic_map(bmap);
4479 map_i = isl_map_intersect_domain(map_i,
4480 isl_set_copy(pma->p[i].set));
4481 map = isl_map_union_disjoint(map, map_i);
4484 isl_pw_multi_aff_free(pma);
4485 return map;
4488 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4490 if (!pma)
4491 return NULL;
4493 if (!isl_space_is_set(pma->dim))
4494 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4495 "isl_pw_multi_aff cannot be converted into an isl_set",
4496 goto error);
4498 return isl_map_from_pw_multi_aff(pma);
4499 error:
4500 isl_pw_multi_aff_free(pma);
4501 return NULL;
4504 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4505 * denominator "denom".
4506 * "denom" is allowed to be negative, in which case the actual denominator
4507 * is -denom and the expressions are added instead.
4509 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4510 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4512 int i, first;
4513 int sign;
4514 isl_int d;
4516 first = isl_seq_first_non_zero(c, n);
4517 if (first == -1)
4518 return aff;
4520 sign = isl_int_sgn(denom);
4521 isl_int_init(d);
4522 isl_int_abs(d, denom);
4523 for (i = first; i < n; ++i) {
4524 isl_aff *aff_i;
4526 if (isl_int_is_zero(c[i]))
4527 continue;
4528 aff_i = isl_multi_aff_get_aff(ma, i);
4529 aff_i = isl_aff_scale(aff_i, c[i]);
4530 aff_i = isl_aff_scale_down(aff_i, d);
4531 if (sign >= 0)
4532 aff = isl_aff_sub(aff, aff_i);
4533 else
4534 aff = isl_aff_add(aff, aff_i);
4536 isl_int_clear(d);
4538 return aff;
4541 /* Extract an affine expression that expresses the output dimension "pos"
4542 * of "bmap" in terms of the parameters and input dimensions from
4543 * equality "eq".
4544 * Note that this expression may involve integer divisions defined
4545 * in terms of parameters and input dimensions.
4546 * The equality may also involve references to earlier (but not later)
4547 * output dimensions. These are replaced by the corresponding elements
4548 * in "ma".
4550 * If the equality is of the form
4552 * f(i) + h(j) + a x + g(i) = 0,
4554 * with f(i) a linear combinations of the parameters and input dimensions,
4555 * g(i) a linear combination of integer divisions defined in terms of the same
4556 * and h(j) a linear combinations of earlier output dimensions,
4557 * then the affine expression is
4559 * (-f(i) - g(i))/a - h(j)/a
4561 * If the equality is of the form
4563 * f(i) + h(j) - a x + g(i) = 0,
4565 * then the affine expression is
4567 * (f(i) + g(i))/a - h(j)/(-a)
4570 * If "div" refers to an integer division (i.e., it is smaller than
4571 * the number of integer divisions), then the equality constraint
4572 * does involve an integer division (the one at position "div") that
4573 * is defined in terms of output dimensions. However, this integer
4574 * division can be eliminated by exploiting a pair of constraints
4575 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4576 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4577 * -l + x >= 0.
4578 * In particular, let
4580 * x = e(i) + m floor(...)
4582 * with e(i) the expression derived above and floor(...) the integer
4583 * division involving output dimensions.
4584 * From
4586 * l <= x <= l + n,
4588 * we have
4590 * 0 <= x - l <= n
4592 * This means
4594 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4595 * = (e(i) - l) mod m
4597 * Therefore,
4599 * x - l = (e(i) - l) mod m
4601 * or
4603 * x = ((e(i) - l) mod m) + l
4605 * The variable "shift" below contains the expression -l, which may
4606 * also involve a linear combination of earlier output dimensions.
4608 static __isl_give isl_aff *extract_aff_from_equality(
4609 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4610 __isl_keep isl_multi_aff *ma)
4612 unsigned o_out;
4613 unsigned n_div, n_out;
4614 isl_ctx *ctx;
4615 isl_local_space *ls;
4616 isl_aff *aff, *shift;
4617 isl_val *mod;
4619 ctx = isl_basic_map_get_ctx(bmap);
4620 ls = isl_basic_map_get_local_space(bmap);
4621 ls = isl_local_space_domain(ls);
4622 aff = isl_aff_alloc(isl_local_space_copy(ls));
4623 if (!aff)
4624 goto error;
4625 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4626 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4627 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4628 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4629 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4630 isl_seq_cpy(aff->v->el + 1 + o_out,
4631 bmap->eq[eq] + o_out + n_out, n_div);
4632 } else {
4633 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4634 isl_seq_neg(aff->v->el + 1 + o_out,
4635 bmap->eq[eq] + o_out + n_out, n_div);
4637 if (div < n_div)
4638 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4639 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4640 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4641 bmap->eq[eq][o_out + pos]);
4642 if (div < n_div) {
4643 shift = isl_aff_alloc(isl_local_space_copy(ls));
4644 if (!shift)
4645 goto error;
4646 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4647 isl_seq_cpy(shift->v->el + 1 + o_out,
4648 bmap->ineq[ineq] + o_out + n_out, n_div);
4649 isl_int_set_si(shift->v->el[0], 1);
4650 shift = subtract_initial(shift, ma, pos,
4651 bmap->ineq[ineq] + o_out, ctx->negone);
4652 aff = isl_aff_add(aff, isl_aff_copy(shift));
4653 mod = isl_val_int_from_isl_int(ctx,
4654 bmap->eq[eq][o_out + n_out + div]);
4655 mod = isl_val_abs(mod);
4656 aff = isl_aff_mod_val(aff, mod);
4657 aff = isl_aff_sub(aff, shift);
4660 isl_local_space_free(ls);
4661 return aff;
4662 error:
4663 isl_local_space_free(ls);
4664 isl_aff_free(aff);
4665 return NULL;
4668 /* Given a basic map with output dimensions defined
4669 * in terms of the parameters input dimensions and earlier
4670 * output dimensions using an equality (and possibly a pair on inequalities),
4671 * extract an isl_aff that expresses output dimension "pos" in terms
4672 * of the parameters and input dimensions.
4673 * Note that this expression may involve integer divisions defined
4674 * in terms of parameters and input dimensions.
4675 * "ma" contains the expressions corresponding to earlier output dimensions.
4677 * This function shares some similarities with
4678 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4680 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4681 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4683 int eq, div, ineq;
4684 isl_aff *aff;
4686 if (!bmap)
4687 return NULL;
4688 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4689 if (eq >= bmap->n_eq)
4690 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4691 "unable to find suitable equality", return NULL);
4692 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4694 aff = isl_aff_remove_unused_divs(aff);
4695 return aff;
4698 /* Given a basic map where each output dimension is defined
4699 * in terms of the parameters and input dimensions using an equality,
4700 * extract an isl_multi_aff that expresses the output dimensions in terms
4701 * of the parameters and input dimensions.
4703 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4704 __isl_take isl_basic_map *bmap)
4706 int i;
4707 unsigned n_out;
4708 isl_multi_aff *ma;
4710 if (!bmap)
4711 return NULL;
4713 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4714 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4716 for (i = 0; i < n_out; ++i) {
4717 isl_aff *aff;
4719 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4720 ma = isl_multi_aff_set_aff(ma, i, aff);
4723 isl_basic_map_free(bmap);
4725 return ma;
4728 /* Given a basic set where each set dimension is defined
4729 * in terms of the parameters using an equality,
4730 * extract an isl_multi_aff that expresses the set dimensions in terms
4731 * of the parameters.
4733 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4734 __isl_take isl_basic_set *bset)
4736 return extract_isl_multi_aff_from_basic_map(bset);
4739 /* Create an isl_pw_multi_aff that is equivalent to
4740 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4741 * The given basic map is such that each output dimension is defined
4742 * in terms of the parameters and input dimensions using an equality.
4744 * Since some applications expect the result of isl_pw_multi_aff_from_map
4745 * to only contain integer affine expressions, we compute the floor
4746 * of the expression before returning.
4748 * Remove all constraints involving local variables without
4749 * an explicit representation (resulting in the removal of those
4750 * local variables) prior to the actual extraction to ensure
4751 * that the local spaces in which the resulting affine expressions
4752 * are created do not contain any unknown local variables.
4753 * Removing such constraints is safe because constraints involving
4754 * unknown local variables are not used to determine whether
4755 * a basic map is obviously single-valued.
4757 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4758 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4760 isl_multi_aff *ma;
4762 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4763 ma = extract_isl_multi_aff_from_basic_map(bmap);
4764 ma = isl_multi_aff_floor(ma);
4765 return isl_pw_multi_aff_alloc(domain, ma);
4768 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4769 * This obviously only works if the input "map" is single-valued.
4770 * If so, we compute the lexicographic minimum of the image in the form
4771 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4772 * to its lexicographic minimum.
4773 * If the input is not single-valued, we produce an error.
4775 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4776 __isl_take isl_map *map)
4778 int i;
4779 int sv;
4780 isl_pw_multi_aff *pma;
4782 sv = isl_map_is_single_valued(map);
4783 if (sv < 0)
4784 goto error;
4785 if (!sv)
4786 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4787 "map is not single-valued", goto error);
4788 map = isl_map_make_disjoint(map);
4789 if (!map)
4790 return NULL;
4792 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4794 for (i = 0; i < map->n; ++i) {
4795 isl_pw_multi_aff *pma_i;
4796 isl_basic_map *bmap;
4797 bmap = isl_basic_map_copy(map->p[i]);
4798 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4799 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4802 isl_map_free(map);
4803 return pma;
4804 error:
4805 isl_map_free(map);
4806 return NULL;
4809 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4810 * taking into account that the output dimension at position "d"
4811 * can be represented as
4813 * x = floor((e(...) + c1) / m)
4815 * given that constraint "i" is of the form
4817 * e(...) + c1 - m x >= 0
4820 * Let "map" be of the form
4822 * A -> B
4824 * We construct a mapping
4826 * A -> [A -> x = floor(...)]
4828 * apply that to the map, obtaining
4830 * [A -> x = floor(...)] -> B
4832 * and equate dimension "d" to x.
4833 * We then compute a isl_pw_multi_aff representation of the resulting map
4834 * and plug in the mapping above.
4836 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4837 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4839 isl_ctx *ctx;
4840 isl_space *space;
4841 isl_local_space *ls;
4842 isl_multi_aff *ma;
4843 isl_aff *aff;
4844 isl_vec *v;
4845 isl_map *insert;
4846 int offset;
4847 int n;
4848 int n_in;
4849 isl_pw_multi_aff *pma;
4850 isl_bool is_set;
4852 is_set = isl_map_is_set(map);
4853 if (is_set < 0)
4854 goto error;
4856 offset = isl_basic_map_offset(hull, isl_dim_out);
4857 ctx = isl_map_get_ctx(map);
4858 space = isl_space_domain(isl_map_get_space(map));
4859 n_in = isl_space_dim(space, isl_dim_set);
4860 n = isl_space_dim(space, isl_dim_all);
4862 v = isl_vec_alloc(ctx, 1 + 1 + n);
4863 if (v) {
4864 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4865 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4867 isl_basic_map_free(hull);
4869 ls = isl_local_space_from_space(isl_space_copy(space));
4870 aff = isl_aff_alloc_vec(ls, v);
4871 aff = isl_aff_floor(aff);
4872 if (is_set) {
4873 isl_space_free(space);
4874 ma = isl_multi_aff_from_aff(aff);
4875 } else {
4876 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4877 ma = isl_multi_aff_range_product(ma,
4878 isl_multi_aff_from_aff(aff));
4881 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4882 map = isl_map_apply_domain(map, insert);
4883 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4884 pma = isl_pw_multi_aff_from_map(map);
4885 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4887 return pma;
4888 error:
4889 isl_map_free(map);
4890 isl_basic_map_free(hull);
4891 return NULL;
4894 /* Is constraint "c" of the form
4896 * e(...) + c1 - m x >= 0
4898 * or
4900 * -e(...) + c2 + m x >= 0
4902 * where m > 1 and e only depends on parameters and input dimemnsions?
4904 * "offset" is the offset of the output dimensions
4905 * "pos" is the position of output dimension x.
4907 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4909 if (isl_int_is_zero(c[offset + d]))
4910 return 0;
4911 if (isl_int_is_one(c[offset + d]))
4912 return 0;
4913 if (isl_int_is_negone(c[offset + d]))
4914 return 0;
4915 if (isl_seq_first_non_zero(c + offset, d) != -1)
4916 return 0;
4917 if (isl_seq_first_non_zero(c + offset + d + 1,
4918 total - (offset + d + 1)) != -1)
4919 return 0;
4920 return 1;
4923 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4925 * As a special case, we first check if there is any pair of constraints,
4926 * shared by all the basic maps in "map" that force a given dimension
4927 * to be equal to the floor of some affine combination of the input dimensions.
4929 * In particular, if we can find two constraints
4931 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4933 * and
4935 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4937 * where m > 1 and e only depends on parameters and input dimemnsions,
4938 * and such that
4940 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4942 * then we know that we can take
4944 * x = floor((e(...) + c1) / m)
4946 * without having to perform any computation.
4948 * Note that we know that
4950 * c1 + c2 >= 1
4952 * If c1 + c2 were 0, then we would have detected an equality during
4953 * simplification. If c1 + c2 were negative, then we would have detected
4954 * a contradiction.
4956 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4957 __isl_take isl_map *map)
4959 int d, dim;
4960 int i, j, n;
4961 int offset, total;
4962 isl_int sum;
4963 isl_basic_map *hull;
4965 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4966 if (!hull)
4967 goto error;
4969 isl_int_init(sum);
4970 dim = isl_map_dim(map, isl_dim_out);
4971 offset = isl_basic_map_offset(hull, isl_dim_out);
4972 total = 1 + isl_basic_map_total_dim(hull);
4973 n = hull->n_ineq;
4974 for (d = 0; d < dim; ++d) {
4975 for (i = 0; i < n; ++i) {
4976 if (!is_potential_div_constraint(hull->ineq[i],
4977 offset, d, total))
4978 continue;
4979 for (j = i + 1; j < n; ++j) {
4980 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4981 hull->ineq[j] + 1, total - 1))
4982 continue;
4983 isl_int_add(sum, hull->ineq[i][0],
4984 hull->ineq[j][0]);
4985 if (isl_int_abs_lt(sum,
4986 hull->ineq[i][offset + d]))
4987 break;
4990 if (j >= n)
4991 continue;
4992 isl_int_clear(sum);
4993 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4994 j = i;
4995 return pw_multi_aff_from_map_div(map, hull, d, j);
4998 isl_int_clear(sum);
4999 isl_basic_map_free(hull);
5000 return pw_multi_aff_from_map_base(map);
5001 error:
5002 isl_map_free(map);
5003 isl_basic_map_free(hull);
5004 return NULL;
5007 /* Given an affine expression
5009 * [A -> B] -> f(A,B)
5011 * construct an isl_multi_aff
5013 * [A -> B] -> B'
5015 * such that dimension "d" in B' is set to "aff" and the remaining
5016 * dimensions are set equal to the corresponding dimensions in B.
5017 * "n_in" is the dimension of the space A.
5018 * "n_out" is the dimension of the space B.
5020 * If "is_set" is set, then the affine expression is of the form
5022 * [B] -> f(B)
5024 * and we construct an isl_multi_aff
5026 * B -> B'
5028 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5029 unsigned n_in, unsigned n_out, int is_set)
5031 int i;
5032 isl_multi_aff *ma;
5033 isl_space *space, *space2;
5034 isl_local_space *ls;
5036 space = isl_aff_get_domain_space(aff);
5037 ls = isl_local_space_from_space(isl_space_copy(space));
5038 space2 = isl_space_copy(space);
5039 if (!is_set)
5040 space2 = isl_space_range(isl_space_unwrap(space2));
5041 space = isl_space_map_from_domain_and_range(space, space2);
5042 ma = isl_multi_aff_alloc(space);
5043 ma = isl_multi_aff_set_aff(ma, d, aff);
5045 for (i = 0; i < n_out; ++i) {
5046 if (i == d)
5047 continue;
5048 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5049 isl_dim_set, n_in + i);
5050 ma = isl_multi_aff_set_aff(ma, i, aff);
5053 isl_local_space_free(ls);
5055 return ma;
5058 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5059 * taking into account that the dimension at position "d" can be written as
5061 * x = m a + f(..) (1)
5063 * where m is equal to "gcd".
5064 * "i" is the index of the equality in "hull" that defines f(..).
5065 * In particular, the equality is of the form
5067 * f(..) - x + m g(existentials) = 0
5069 * or
5071 * -f(..) + x + m g(existentials) = 0
5073 * We basically plug (1) into "map", resulting in a map with "a"
5074 * in the range instead of "x". The corresponding isl_pw_multi_aff
5075 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5077 * Specifically, given the input map
5079 * A -> B
5081 * We first wrap it into a set
5083 * [A -> B]
5085 * and define (1) on top of the corresponding space, resulting in "aff".
5086 * We use this to create an isl_multi_aff that maps the output position "d"
5087 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5088 * We plug this into the wrapped map, unwrap the result and compute the
5089 * corresponding isl_pw_multi_aff.
5090 * The result is an expression
5092 * A -> T(A)
5094 * We adjust that to
5096 * A -> [A -> T(A)]
5098 * so that we can plug that into "aff", after extending the latter to
5099 * a mapping
5101 * [A -> B] -> B'
5104 * If "map" is actually a set, then there is no "A" space, meaning
5105 * that we do not need to perform any wrapping, and that the result
5106 * of the recursive call is of the form
5108 * [T]
5110 * which is plugged into a mapping of the form
5112 * B -> B'
5114 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5115 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5116 isl_int gcd)
5118 isl_set *set;
5119 isl_space *space;
5120 isl_local_space *ls;
5121 isl_aff *aff;
5122 isl_multi_aff *ma;
5123 isl_pw_multi_aff *pma, *id;
5124 unsigned n_in;
5125 unsigned o_out;
5126 unsigned n_out;
5127 isl_bool is_set;
5129 is_set = isl_map_is_set(map);
5130 if (is_set < 0)
5131 goto error;
5133 n_in = isl_basic_map_dim(hull, isl_dim_in);
5134 n_out = isl_basic_map_dim(hull, isl_dim_out);
5135 o_out = isl_basic_map_offset(hull, isl_dim_out);
5137 if (is_set)
5138 set = map;
5139 else
5140 set = isl_map_wrap(map);
5141 space = isl_space_map_from_set(isl_set_get_space(set));
5142 ma = isl_multi_aff_identity(space);
5143 ls = isl_local_space_from_space(isl_set_get_space(set));
5144 aff = isl_aff_alloc(ls);
5145 if (aff) {
5146 isl_int_set_si(aff->v->el[0], 1);
5147 if (isl_int_is_one(hull->eq[i][o_out + d]))
5148 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5149 aff->v->size - 1);
5150 else
5151 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5152 aff->v->size - 1);
5153 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5155 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5156 set = isl_set_preimage_multi_aff(set, ma);
5158 ma = range_map(aff, d, n_in, n_out, is_set);
5160 if (is_set)
5161 map = set;
5162 else
5163 map = isl_set_unwrap(set);
5164 pma = isl_pw_multi_aff_from_map(map);
5166 if (!is_set) {
5167 space = isl_pw_multi_aff_get_domain_space(pma);
5168 space = isl_space_map_from_set(space);
5169 id = isl_pw_multi_aff_identity(space);
5170 pma = isl_pw_multi_aff_range_product(id, pma);
5172 id = isl_pw_multi_aff_from_multi_aff(ma);
5173 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5175 isl_basic_map_free(hull);
5176 return pma;
5177 error:
5178 isl_map_free(map);
5179 isl_basic_map_free(hull);
5180 return NULL;
5183 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5184 * "hull" contains the equalities valid for "map".
5186 * Check if any of the output dimensions is "strided".
5187 * That is, we check if it can be written as
5189 * x = m a + f(..)
5191 * with m greater than 1, a some combination of existentially quantified
5192 * variables and f an expression in the parameters and input dimensions.
5193 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5195 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5196 * special case.
5198 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5199 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5201 int i, j;
5202 unsigned n_out;
5203 unsigned o_out;
5204 unsigned n_div;
5205 unsigned o_div;
5206 isl_int gcd;
5208 n_div = isl_basic_map_dim(hull, isl_dim_div);
5209 o_div = isl_basic_map_offset(hull, isl_dim_div);
5211 if (n_div == 0) {
5212 isl_basic_map_free(hull);
5213 return pw_multi_aff_from_map_check_div(map);
5216 isl_int_init(gcd);
5218 n_out = isl_basic_map_dim(hull, isl_dim_out);
5219 o_out = isl_basic_map_offset(hull, isl_dim_out);
5221 for (i = 0; i < n_out; ++i) {
5222 for (j = 0; j < hull->n_eq; ++j) {
5223 isl_int *eq = hull->eq[j];
5224 isl_pw_multi_aff *res;
5226 if (!isl_int_is_one(eq[o_out + i]) &&
5227 !isl_int_is_negone(eq[o_out + i]))
5228 continue;
5229 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5230 continue;
5231 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5232 n_out - (i + 1)) != -1)
5233 continue;
5234 isl_seq_gcd(eq + o_div, n_div, &gcd);
5235 if (isl_int_is_zero(gcd))
5236 continue;
5237 if (isl_int_is_one(gcd))
5238 continue;
5240 res = pw_multi_aff_from_map_stride(map, hull,
5241 i, j, gcd);
5242 isl_int_clear(gcd);
5243 return res;
5247 isl_int_clear(gcd);
5248 isl_basic_map_free(hull);
5249 return pw_multi_aff_from_map_check_div(map);
5252 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5254 * As a special case, we first check if all output dimensions are uniquely
5255 * defined in terms of the parameters and input dimensions over the entire
5256 * domain. If so, we extract the desired isl_pw_multi_aff directly
5257 * from the affine hull of "map" and its domain.
5259 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5260 * special cases.
5262 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5264 isl_bool sv;
5265 isl_basic_map *hull;
5267 if (!map)
5268 return NULL;
5270 if (isl_map_n_basic_map(map) == 1) {
5271 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5272 hull = isl_basic_map_plain_affine_hull(hull);
5273 sv = isl_basic_map_plain_is_single_valued(hull);
5274 if (sv >= 0 && sv)
5275 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5276 hull);
5277 isl_basic_map_free(hull);
5279 map = isl_map_detect_equalities(map);
5280 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5281 sv = isl_basic_map_plain_is_single_valued(hull);
5282 if (sv >= 0 && sv)
5283 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5284 if (sv >= 0)
5285 return pw_multi_aff_from_map_check_strides(map, hull);
5286 isl_basic_map_free(hull);
5287 isl_map_free(map);
5288 return NULL;
5291 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5293 return isl_pw_multi_aff_from_map(set);
5296 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5297 * add it to *user.
5299 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5301 isl_union_pw_multi_aff **upma = user;
5302 isl_pw_multi_aff *pma;
5304 pma = isl_pw_multi_aff_from_map(map);
5305 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5307 return *upma ? isl_stat_ok : isl_stat_error;
5310 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5311 * domain.
5313 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5314 __isl_take isl_aff *aff)
5316 isl_multi_aff *ma;
5317 isl_pw_multi_aff *pma;
5319 ma = isl_multi_aff_from_aff(aff);
5320 pma = isl_pw_multi_aff_from_multi_aff(ma);
5321 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5324 /* Try and create an isl_union_pw_multi_aff that is equivalent
5325 * to the given isl_union_map.
5326 * The isl_union_map is required to be single-valued in each space.
5327 * Otherwise, an error is produced.
5329 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5330 __isl_take isl_union_map *umap)
5332 isl_space *space;
5333 isl_union_pw_multi_aff *upma;
5335 space = isl_union_map_get_space(umap);
5336 upma = isl_union_pw_multi_aff_empty(space);
5337 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5338 upma = isl_union_pw_multi_aff_free(upma);
5339 isl_union_map_free(umap);
5341 return upma;
5344 /* Try and create an isl_union_pw_multi_aff that is equivalent
5345 * to the given isl_union_set.
5346 * The isl_union_set is required to be a singleton in each space.
5347 * Otherwise, an error is produced.
5349 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5350 __isl_take isl_union_set *uset)
5352 return isl_union_pw_multi_aff_from_union_map(uset);
5355 /* Return the piecewise affine expression "set ? 1 : 0".
5357 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5359 isl_pw_aff *pa;
5360 isl_space *space = isl_set_get_space(set);
5361 isl_local_space *ls = isl_local_space_from_space(space);
5362 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5363 isl_aff *one = isl_aff_zero_on_domain(ls);
5365 one = isl_aff_add_constant_si(one, 1);
5366 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5367 set = isl_set_complement(set);
5368 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5370 return pa;
5373 /* Plug in "subs" for dimension "type", "pos" of "aff".
5375 * Let i be the dimension to replace and let "subs" be of the form
5377 * f/d
5379 * and "aff" of the form
5381 * (a i + g)/m
5383 * The result is
5385 * (a f + d g')/(m d)
5387 * where g' is the result of plugging in "subs" in each of the integer
5388 * divisions in g.
5390 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5391 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5393 isl_ctx *ctx;
5394 isl_int v;
5396 aff = isl_aff_cow(aff);
5397 if (!aff || !subs)
5398 return isl_aff_free(aff);
5400 ctx = isl_aff_get_ctx(aff);
5401 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5402 isl_die(ctx, isl_error_invalid,
5403 "spaces don't match", return isl_aff_free(aff));
5404 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5405 isl_die(ctx, isl_error_unsupported,
5406 "cannot handle divs yet", return isl_aff_free(aff));
5408 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5409 if (!aff->ls)
5410 return isl_aff_free(aff);
5412 aff->v = isl_vec_cow(aff->v);
5413 if (!aff->v)
5414 return isl_aff_free(aff);
5416 pos += isl_local_space_offset(aff->ls, type);
5418 isl_int_init(v);
5419 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5420 aff->v->size, subs->v->size, v);
5421 isl_int_clear(v);
5423 return aff;
5426 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5427 * expressions in "maff".
5429 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5430 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5431 __isl_keep isl_aff *subs)
5433 int i;
5435 maff = isl_multi_aff_cow(maff);
5436 if (!maff || !subs)
5437 return isl_multi_aff_free(maff);
5439 if (type == isl_dim_in)
5440 type = isl_dim_set;
5442 for (i = 0; i < maff->n; ++i) {
5443 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5444 type, pos, subs);
5445 if (!maff->u.p[i])
5446 return isl_multi_aff_free(maff);
5449 return maff;
5452 /* Plug in "subs" for dimension "type", "pos" of "pma".
5454 * pma is of the form
5456 * A_i(v) -> M_i(v)
5458 * while subs is of the form
5460 * v' = B_j(v) -> S_j
5462 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5463 * has a contribution in the result, in particular
5465 * C_ij(S_j) -> M_i(S_j)
5467 * Note that plugging in S_j in C_ij may also result in an empty set
5468 * and this contribution should simply be discarded.
5470 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5471 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5472 __isl_keep isl_pw_aff *subs)
5474 int i, j, n;
5475 isl_pw_multi_aff *res;
5477 if (!pma || !subs)
5478 return isl_pw_multi_aff_free(pma);
5480 n = pma->n * subs->n;
5481 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5483 for (i = 0; i < pma->n; ++i) {
5484 for (j = 0; j < subs->n; ++j) {
5485 isl_set *common;
5486 isl_multi_aff *res_ij;
5487 int empty;
5489 common = isl_set_intersect(
5490 isl_set_copy(pma->p[i].set),
5491 isl_set_copy(subs->p[j].set));
5492 common = isl_set_substitute(common,
5493 type, pos, subs->p[j].aff);
5494 empty = isl_set_plain_is_empty(common);
5495 if (empty < 0 || empty) {
5496 isl_set_free(common);
5497 if (empty < 0)
5498 goto error;
5499 continue;
5502 res_ij = isl_multi_aff_substitute(
5503 isl_multi_aff_copy(pma->p[i].maff),
5504 type, pos, subs->p[j].aff);
5506 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5510 isl_pw_multi_aff_free(pma);
5511 return res;
5512 error:
5513 isl_pw_multi_aff_free(pma);
5514 isl_pw_multi_aff_free(res);
5515 return NULL;
5518 /* Compute the preimage of a range of dimensions in the affine expression "src"
5519 * under "ma" and put the result in "dst". The number of dimensions in "src"
5520 * that precede the range is given by "n_before". The number of dimensions
5521 * in the range is given by the number of output dimensions of "ma".
5522 * The number of dimensions that follow the range is given by "n_after".
5523 * If "has_denom" is set (to one),
5524 * then "src" and "dst" have an extra initial denominator.
5525 * "n_div_ma" is the number of existentials in "ma"
5526 * "n_div_bset" is the number of existentials in "src"
5527 * The resulting "dst" (which is assumed to have been allocated by
5528 * the caller) contains coefficients for both sets of existentials,
5529 * first those in "ma" and then those in "src".
5530 * f, c1, c2 and g are temporary objects that have been initialized
5531 * by the caller.
5533 * Let src represent the expression
5535 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5537 * and let ma represent the expressions
5539 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5541 * We start out with the following expression for dst:
5543 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5545 * with the multiplication factor f initially equal to 1
5546 * and f \sum_i b_i v_i kept separately.
5547 * For each x_i that we substitute, we multiply the numerator
5548 * (and denominator) of dst by c_1 = m_i and add the numerator
5549 * of the x_i expression multiplied by c_2 = f b_i,
5550 * after removing the common factors of c_1 and c_2.
5551 * The multiplication factor f also needs to be multiplied by c_1
5552 * for the next x_j, j > i.
5554 void isl_seq_preimage(isl_int *dst, isl_int *src,
5555 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5556 int n_div_ma, int n_div_bmap,
5557 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5559 int i;
5560 int n_param, n_in, n_out;
5561 int o_dst, o_src;
5563 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5564 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5565 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5567 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5568 o_dst = o_src = has_denom + 1 + n_param + n_before;
5569 isl_seq_clr(dst + o_dst, n_in);
5570 o_dst += n_in;
5571 o_src += n_out;
5572 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5573 o_dst += n_after;
5574 o_src += n_after;
5575 isl_seq_clr(dst + o_dst, n_div_ma);
5576 o_dst += n_div_ma;
5577 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5579 isl_int_set_si(f, 1);
5581 for (i = 0; i < n_out; ++i) {
5582 int offset = has_denom + 1 + n_param + n_before + i;
5584 if (isl_int_is_zero(src[offset]))
5585 continue;
5586 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5587 isl_int_mul(c2, f, src[offset]);
5588 isl_int_gcd(g, c1, c2);
5589 isl_int_divexact(c1, c1, g);
5590 isl_int_divexact(c2, c2, g);
5592 isl_int_mul(f, f, c1);
5593 o_dst = has_denom;
5594 o_src = 1;
5595 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5596 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5597 o_dst += 1 + n_param;
5598 o_src += 1 + n_param;
5599 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5600 o_dst += n_before;
5601 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5602 c2, ma->u.p[i]->v->el + o_src, n_in);
5603 o_dst += n_in;
5604 o_src += n_in;
5605 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5606 o_dst += n_after;
5607 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5608 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5609 o_dst += n_div_ma;
5610 o_src += n_div_ma;
5611 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5612 if (has_denom)
5613 isl_int_mul(dst[0], dst[0], c1);
5617 /* Compute the pullback of "aff" by the function represented by "ma".
5618 * In other words, plug in "ma" in "aff". The result is an affine expression
5619 * defined over the domain space of "ma".
5621 * If "aff" is represented by
5623 * (a(p) + b x + c(divs))/d
5625 * and ma is represented by
5627 * x = D(p) + F(y) + G(divs')
5629 * then the result is
5631 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5633 * The divs in the local space of the input are similarly adjusted
5634 * through a call to isl_local_space_preimage_multi_aff.
5636 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5637 __isl_take isl_multi_aff *ma)
5639 isl_aff *res = NULL;
5640 isl_local_space *ls;
5641 int n_div_aff, n_div_ma;
5642 isl_int f, c1, c2, g;
5644 ma = isl_multi_aff_align_divs(ma);
5645 if (!aff || !ma)
5646 goto error;
5648 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5649 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5651 ls = isl_aff_get_domain_local_space(aff);
5652 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5653 res = isl_aff_alloc(ls);
5654 if (!res)
5655 goto error;
5657 isl_int_init(f);
5658 isl_int_init(c1);
5659 isl_int_init(c2);
5660 isl_int_init(g);
5662 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5663 f, c1, c2, g, 1);
5665 isl_int_clear(f);
5666 isl_int_clear(c1);
5667 isl_int_clear(c2);
5668 isl_int_clear(g);
5670 isl_aff_free(aff);
5671 isl_multi_aff_free(ma);
5672 res = isl_aff_normalize(res);
5673 return res;
5674 error:
5675 isl_aff_free(aff);
5676 isl_multi_aff_free(ma);
5677 isl_aff_free(res);
5678 return NULL;
5681 /* Compute the pullback of "aff1" by the function represented by "aff2".
5682 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5683 * defined over the domain space of "aff1".
5685 * The domain of "aff1" should match the range of "aff2", which means
5686 * that it should be single-dimensional.
5688 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5689 __isl_take isl_aff *aff2)
5691 isl_multi_aff *ma;
5693 ma = isl_multi_aff_from_aff(aff2);
5694 return isl_aff_pullback_multi_aff(aff1, ma);
5697 /* Compute the pullback of "ma1" by the function represented by "ma2".
5698 * In other words, plug in "ma2" in "ma1".
5700 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5702 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5703 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5705 int i;
5706 isl_space *space = NULL;
5708 ma2 = isl_multi_aff_align_divs(ma2);
5709 ma1 = isl_multi_aff_cow(ma1);
5710 if (!ma1 || !ma2)
5711 goto error;
5713 space = isl_space_join(isl_multi_aff_get_space(ma2),
5714 isl_multi_aff_get_space(ma1));
5716 for (i = 0; i < ma1->n; ++i) {
5717 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5718 isl_multi_aff_copy(ma2));
5719 if (!ma1->u.p[i])
5720 goto error;
5723 ma1 = isl_multi_aff_reset_space(ma1, space);
5724 isl_multi_aff_free(ma2);
5725 return ma1;
5726 error:
5727 isl_space_free(space);
5728 isl_multi_aff_free(ma2);
5729 isl_multi_aff_free(ma1);
5730 return NULL;
5733 /* Compute the pullback of "ma1" by the function represented by "ma2".
5734 * In other words, plug in "ma2" in "ma1".
5736 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5737 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5739 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5740 &isl_multi_aff_pullback_multi_aff_aligned);
5743 /* Extend the local space of "dst" to include the divs
5744 * in the local space of "src".
5746 * If "src" does not have any divs or if the local spaces of "dst" and
5747 * "src" are the same, then no extension is required.
5749 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5750 __isl_keep isl_aff *src)
5752 isl_ctx *ctx;
5753 int src_n_div, dst_n_div;
5754 int *exp1 = NULL;
5755 int *exp2 = NULL;
5756 isl_bool equal;
5757 isl_mat *div;
5759 if (!src || !dst)
5760 return isl_aff_free(dst);
5762 ctx = isl_aff_get_ctx(src);
5763 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5764 if (equal < 0)
5765 return isl_aff_free(dst);
5766 if (!equal)
5767 isl_die(ctx, isl_error_invalid,
5768 "spaces don't match", goto error);
5770 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5771 if (src_n_div == 0)
5772 return dst;
5773 equal = isl_local_space_is_equal(src->ls, dst->ls);
5774 if (equal < 0)
5775 return isl_aff_free(dst);
5776 if (equal)
5777 return dst;
5779 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5780 exp1 = isl_alloc_array(ctx, int, src_n_div);
5781 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5782 if (!exp1 || (dst_n_div && !exp2))
5783 goto error;
5785 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5786 dst = isl_aff_expand_divs(dst, div, exp2);
5787 free(exp1);
5788 free(exp2);
5790 return dst;
5791 error:
5792 free(exp1);
5793 free(exp2);
5794 return isl_aff_free(dst);
5797 /* Adjust the local spaces of the affine expressions in "maff"
5798 * such that they all have the save divs.
5800 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5801 __isl_take isl_multi_aff *maff)
5803 int i;
5805 if (!maff)
5806 return NULL;
5807 if (maff->n == 0)
5808 return maff;
5809 maff = isl_multi_aff_cow(maff);
5810 if (!maff)
5811 return NULL;
5813 for (i = 1; i < maff->n; ++i)
5814 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5815 for (i = 1; i < maff->n; ++i) {
5816 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5817 if (!maff->u.p[i])
5818 return isl_multi_aff_free(maff);
5821 return maff;
5824 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5826 aff = isl_aff_cow(aff);
5827 if (!aff)
5828 return NULL;
5830 aff->ls = isl_local_space_lift(aff->ls);
5831 if (!aff->ls)
5832 return isl_aff_free(aff);
5834 return aff;
5837 /* Lift "maff" to a space with extra dimensions such that the result
5838 * has no more existentially quantified variables.
5839 * If "ls" is not NULL, then *ls is assigned the local space that lies
5840 * at the basis of the lifting applied to "maff".
5842 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5843 __isl_give isl_local_space **ls)
5845 int i;
5846 isl_space *space;
5847 unsigned n_div;
5849 if (ls)
5850 *ls = NULL;
5852 if (!maff)
5853 return NULL;
5855 if (maff->n == 0) {
5856 if (ls) {
5857 isl_space *space = isl_multi_aff_get_domain_space(maff);
5858 *ls = isl_local_space_from_space(space);
5859 if (!*ls)
5860 return isl_multi_aff_free(maff);
5862 return maff;
5865 maff = isl_multi_aff_cow(maff);
5866 maff = isl_multi_aff_align_divs(maff);
5867 if (!maff)
5868 return NULL;
5870 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5871 space = isl_multi_aff_get_space(maff);
5872 space = isl_space_lift(isl_space_domain(space), n_div);
5873 space = isl_space_extend_domain_with_range(space,
5874 isl_multi_aff_get_space(maff));
5875 if (!space)
5876 return isl_multi_aff_free(maff);
5877 isl_space_free(maff->space);
5878 maff->space = space;
5880 if (ls) {
5881 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5882 if (!*ls)
5883 return isl_multi_aff_free(maff);
5886 for (i = 0; i < maff->n; ++i) {
5887 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5888 if (!maff->u.p[i])
5889 goto error;
5892 return maff;
5893 error:
5894 if (ls)
5895 isl_local_space_free(*ls);
5896 return isl_multi_aff_free(maff);
5900 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5902 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5903 __isl_keep isl_pw_multi_aff *pma, int pos)
5905 int i;
5906 int n_out;
5907 isl_space *space;
5908 isl_pw_aff *pa;
5910 if (!pma)
5911 return NULL;
5913 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5914 if (pos < 0 || pos >= n_out)
5915 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5916 "index out of bounds", return NULL);
5918 space = isl_pw_multi_aff_get_space(pma);
5919 space = isl_space_drop_dims(space, isl_dim_out,
5920 pos + 1, n_out - pos - 1);
5921 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5923 pa = isl_pw_aff_alloc_size(space, pma->n);
5924 for (i = 0; i < pma->n; ++i) {
5925 isl_aff *aff;
5926 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5927 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5930 return pa;
5933 /* Return an isl_pw_multi_aff with the given "set" as domain and
5934 * an unnamed zero-dimensional range.
5936 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5937 __isl_take isl_set *set)
5939 isl_multi_aff *ma;
5940 isl_space *space;
5942 space = isl_set_get_space(set);
5943 space = isl_space_from_domain(space);
5944 ma = isl_multi_aff_zero(space);
5945 return isl_pw_multi_aff_alloc(set, ma);
5948 /* Add an isl_pw_multi_aff with the given "set" as domain and
5949 * an unnamed zero-dimensional range to *user.
5951 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5952 void *user)
5954 isl_union_pw_multi_aff **upma = user;
5955 isl_pw_multi_aff *pma;
5957 pma = isl_pw_multi_aff_from_domain(set);
5958 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5960 return isl_stat_ok;
5963 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5964 * an unnamed zero-dimensional range.
5966 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5967 __isl_take isl_union_set *uset)
5969 isl_space *space;
5970 isl_union_pw_multi_aff *upma;
5972 if (!uset)
5973 return NULL;
5975 space = isl_union_set_get_space(uset);
5976 upma = isl_union_pw_multi_aff_empty(space);
5978 if (isl_union_set_foreach_set(uset,
5979 &add_pw_multi_aff_from_domain, &upma) < 0)
5980 goto error;
5982 isl_union_set_free(uset);
5983 return upma;
5984 error:
5985 isl_union_set_free(uset);
5986 isl_union_pw_multi_aff_free(upma);
5987 return NULL;
5990 /* Convert "pma" to an isl_map and add it to *umap.
5992 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5993 void *user)
5995 isl_union_map **umap = user;
5996 isl_map *map;
5998 map = isl_map_from_pw_multi_aff(pma);
5999 *umap = isl_union_map_add_map(*umap, map);
6001 return isl_stat_ok;
6004 /* Construct a union map mapping the domain of the union
6005 * piecewise multi-affine expression to its range, with each dimension
6006 * in the range equated to the corresponding affine expression on its cell.
6008 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
6009 __isl_take isl_union_pw_multi_aff *upma)
6011 isl_space *space;
6012 isl_union_map *umap;
6014 if (!upma)
6015 return NULL;
6017 space = isl_union_pw_multi_aff_get_space(upma);
6018 umap = isl_union_map_empty(space);
6020 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
6021 &map_from_pw_multi_aff, &umap) < 0)
6022 goto error;
6024 isl_union_pw_multi_aff_free(upma);
6025 return umap;
6026 error:
6027 isl_union_pw_multi_aff_free(upma);
6028 isl_union_map_free(umap);
6029 return NULL;
6032 /* Local data for bin_entry and the callback "fn".
6034 struct isl_union_pw_multi_aff_bin_data {
6035 isl_union_pw_multi_aff *upma2;
6036 isl_union_pw_multi_aff *res;
6037 isl_pw_multi_aff *pma;
6038 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6041 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6042 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6044 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6046 struct isl_union_pw_multi_aff_bin_data *data = user;
6047 isl_stat r;
6049 data->pma = pma;
6050 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6051 data->fn, data);
6052 isl_pw_multi_aff_free(pma);
6054 return r;
6057 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6058 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6059 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6060 * as *entry. The callback should adjust data->res if desired.
6062 static __isl_give isl_union_pw_multi_aff *bin_op(
6063 __isl_take isl_union_pw_multi_aff *upma1,
6064 __isl_take isl_union_pw_multi_aff *upma2,
6065 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6067 isl_space *space;
6068 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6070 space = isl_union_pw_multi_aff_get_space(upma2);
6071 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6072 space = isl_union_pw_multi_aff_get_space(upma1);
6073 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6075 if (!upma1 || !upma2)
6076 goto error;
6078 data.upma2 = upma2;
6079 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6080 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6081 &bin_entry, &data) < 0)
6082 goto error;
6084 isl_union_pw_multi_aff_free(upma1);
6085 isl_union_pw_multi_aff_free(upma2);
6086 return data.res;
6087 error:
6088 isl_union_pw_multi_aff_free(upma1);
6089 isl_union_pw_multi_aff_free(upma2);
6090 isl_union_pw_multi_aff_free(data.res);
6091 return NULL;
6094 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6095 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6097 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6098 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6100 isl_space *space;
6102 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6103 isl_pw_multi_aff_get_space(pma2));
6104 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6105 &isl_multi_aff_range_product);
6108 /* Given two isl_pw_multi_affs A -> B and C -> D,
6109 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6111 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6112 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6114 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6115 &pw_multi_aff_range_product);
6118 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6119 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6121 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6122 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6124 isl_space *space;
6126 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6127 isl_pw_multi_aff_get_space(pma2));
6128 space = isl_space_flatten_range(space);
6129 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6130 &isl_multi_aff_flat_range_product);
6133 /* Given two isl_pw_multi_affs A -> B and C -> D,
6134 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6136 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6137 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6139 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6140 &pw_multi_aff_flat_range_product);
6143 /* If data->pma and "pma2" have the same domain space, then compute
6144 * their flat range product and the result to data->res.
6146 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6147 void *user)
6149 struct isl_union_pw_multi_aff_bin_data *data = user;
6151 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6152 pma2->dim, isl_dim_in)) {
6153 isl_pw_multi_aff_free(pma2);
6154 return isl_stat_ok;
6157 pma2 = isl_pw_multi_aff_flat_range_product(
6158 isl_pw_multi_aff_copy(data->pma), pma2);
6160 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6162 return isl_stat_ok;
6165 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6166 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6168 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6169 __isl_take isl_union_pw_multi_aff *upma1,
6170 __isl_take isl_union_pw_multi_aff *upma2)
6172 return bin_op(upma1, upma2, &flat_range_product_entry);
6175 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6176 * The parameters are assumed to have been aligned.
6178 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6179 * except that it works on two different isl_pw_* types.
6181 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6182 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6183 __isl_take isl_pw_aff *pa)
6185 int i, j, n;
6186 isl_pw_multi_aff *res = NULL;
6188 if (!pma || !pa)
6189 goto error;
6191 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6192 pa->dim, isl_dim_in))
6193 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6194 "domains don't match", goto error);
6195 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6196 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6197 "index out of bounds", goto error);
6199 n = pma->n * pa->n;
6200 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6202 for (i = 0; i < pma->n; ++i) {
6203 for (j = 0; j < pa->n; ++j) {
6204 isl_set *common;
6205 isl_multi_aff *res_ij;
6206 int empty;
6208 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6209 isl_set_copy(pa->p[j].set));
6210 empty = isl_set_plain_is_empty(common);
6211 if (empty < 0 || empty) {
6212 isl_set_free(common);
6213 if (empty < 0)
6214 goto error;
6215 continue;
6218 res_ij = isl_multi_aff_set_aff(
6219 isl_multi_aff_copy(pma->p[i].maff), pos,
6220 isl_aff_copy(pa->p[j].aff));
6221 res_ij = isl_multi_aff_gist(res_ij,
6222 isl_set_copy(common));
6224 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6228 isl_pw_multi_aff_free(pma);
6229 isl_pw_aff_free(pa);
6230 return res;
6231 error:
6232 isl_pw_multi_aff_free(pma);
6233 isl_pw_aff_free(pa);
6234 return isl_pw_multi_aff_free(res);
6237 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6239 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6240 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6241 __isl_take isl_pw_aff *pa)
6243 isl_bool equal_params;
6245 if (!pma || !pa)
6246 goto error;
6247 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6248 if (equal_params < 0)
6249 goto error;
6250 if (equal_params)
6251 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6252 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6253 isl_pw_aff_check_named_params(pa) < 0)
6254 goto error;
6255 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6256 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6257 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6258 error:
6259 isl_pw_multi_aff_free(pma);
6260 isl_pw_aff_free(pa);
6261 return NULL;
6264 /* Do the parameters of "pa" match those of "space"?
6266 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6267 __isl_keep isl_space *space)
6269 isl_space *pa_space;
6270 isl_bool match;
6272 if (!pa || !space)
6273 return isl_bool_error;
6275 pa_space = isl_pw_aff_get_space(pa);
6277 match = isl_space_has_equal_params(space, pa_space);
6279 isl_space_free(pa_space);
6280 return match;
6283 /* Check that the domain space of "pa" matches "space".
6285 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6286 __isl_keep isl_space *space)
6288 isl_space *pa_space;
6289 isl_bool match;
6291 if (!pa || !space)
6292 return isl_stat_error;
6294 pa_space = isl_pw_aff_get_space(pa);
6296 match = isl_space_has_equal_params(space, pa_space);
6297 if (match < 0)
6298 goto error;
6299 if (!match)
6300 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6301 "parameters don't match", goto error);
6302 match = isl_space_tuple_is_equal(space, isl_dim_in,
6303 pa_space, isl_dim_in);
6304 if (match < 0)
6305 goto error;
6306 if (!match)
6307 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6308 "domains don't match", goto error);
6309 isl_space_free(pa_space);
6310 return isl_stat_ok;
6311 error:
6312 isl_space_free(pa_space);
6313 return isl_stat_error;
6316 #undef BASE
6317 #define BASE pw_aff
6318 #undef DOMBASE
6319 #define DOMBASE set
6321 #include <isl_multi_explicit_domain.c>
6322 #include <isl_multi_pw_aff_explicit_domain.c>
6323 #include <isl_multi_templ.c>
6324 #include <isl_multi_apply_set.c>
6325 #include <isl_multi_coalesce.c>
6326 #include <isl_multi_dims.c>
6327 #include <isl_multi_gist.c>
6328 #include <isl_multi_hash.c>
6329 #include <isl_multi_align_set.c>
6330 #include <isl_multi_intersect.c>
6332 /* Does "mpa" have a non-trivial explicit domain?
6334 * The explicit domain, if present, is trivial if it represents
6335 * an (obviously) universe set.
6337 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6338 __isl_keep isl_multi_pw_aff *mpa)
6340 if (!mpa)
6341 return isl_bool_error;
6342 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6343 return isl_bool_false;
6344 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6347 /* Scale the elements of "pma" by the corresponding elements of "mv".
6349 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6350 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6352 int i;
6353 isl_bool equal_params;
6355 pma = isl_pw_multi_aff_cow(pma);
6356 if (!pma || !mv)
6357 goto error;
6358 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6359 mv->space, isl_dim_set))
6360 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6361 "spaces don't match", goto error);
6362 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6363 if (equal_params < 0)
6364 goto error;
6365 if (!equal_params) {
6366 pma = isl_pw_multi_aff_align_params(pma,
6367 isl_multi_val_get_space(mv));
6368 mv = isl_multi_val_align_params(mv,
6369 isl_pw_multi_aff_get_space(pma));
6370 if (!pma || !mv)
6371 goto error;
6374 for (i = 0; i < pma->n; ++i) {
6375 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6376 isl_multi_val_copy(mv));
6377 if (!pma->p[i].maff)
6378 goto error;
6381 isl_multi_val_free(mv);
6382 return pma;
6383 error:
6384 isl_multi_val_free(mv);
6385 isl_pw_multi_aff_free(pma);
6386 return NULL;
6389 /* This function is called for each entry of an isl_union_pw_multi_aff.
6390 * If the space of the entry matches that of data->mv,
6391 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6392 * Otherwise, return an empty isl_pw_multi_aff.
6394 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6395 __isl_take isl_pw_multi_aff *pma, void *user)
6397 isl_multi_val *mv = user;
6399 if (!pma)
6400 return NULL;
6401 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6402 mv->space, isl_dim_set)) {
6403 isl_space *space = isl_pw_multi_aff_get_space(pma);
6404 isl_pw_multi_aff_free(pma);
6405 return isl_pw_multi_aff_empty(space);
6408 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6411 /* Scale the elements of "upma" by the corresponding elements of "mv",
6412 * for those entries that match the space of "mv".
6414 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6415 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6417 upma = isl_union_pw_multi_aff_align_params(upma,
6418 isl_multi_val_get_space(mv));
6419 mv = isl_multi_val_align_params(mv,
6420 isl_union_pw_multi_aff_get_space(upma));
6421 if (!upma || !mv)
6422 goto error;
6424 return isl_union_pw_multi_aff_transform(upma,
6425 &union_pw_multi_aff_scale_multi_val_entry, mv);
6427 isl_multi_val_free(mv);
6428 return upma;
6429 error:
6430 isl_multi_val_free(mv);
6431 isl_union_pw_multi_aff_free(upma);
6432 return NULL;
6435 /* Construct and return a piecewise multi affine expression
6436 * in the given space with value zero in each of the output dimensions and
6437 * a universe domain.
6439 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6441 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6444 /* Construct and return a piecewise multi affine expression
6445 * that is equal to the given piecewise affine expression.
6447 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6448 __isl_take isl_pw_aff *pa)
6450 int i;
6451 isl_space *space;
6452 isl_pw_multi_aff *pma;
6454 if (!pa)
6455 return NULL;
6457 space = isl_pw_aff_get_space(pa);
6458 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6460 for (i = 0; i < pa->n; ++i) {
6461 isl_set *set;
6462 isl_multi_aff *ma;
6464 set = isl_set_copy(pa->p[i].set);
6465 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6466 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6469 isl_pw_aff_free(pa);
6470 return pma;
6473 /* Construct a set or map mapping the shared (parameter) domain
6474 * of the piecewise affine expressions to the range of "mpa"
6475 * with each dimension in the range equated to the
6476 * corresponding piecewise affine expression.
6478 static __isl_give isl_map *map_from_multi_pw_aff(
6479 __isl_take isl_multi_pw_aff *mpa)
6481 int i;
6482 isl_space *space;
6483 isl_map *map;
6485 if (!mpa)
6486 return NULL;
6488 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6489 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6490 "invalid space", goto error);
6492 space = isl_multi_pw_aff_get_domain_space(mpa);
6493 map = isl_map_universe(isl_space_from_domain(space));
6495 for (i = 0; i < mpa->n; ++i) {
6496 isl_pw_aff *pa;
6497 isl_map *map_i;
6499 pa = isl_pw_aff_copy(mpa->u.p[i]);
6500 map_i = map_from_pw_aff(pa);
6502 map = isl_map_flat_range_product(map, map_i);
6505 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6507 isl_multi_pw_aff_free(mpa);
6508 return map;
6509 error:
6510 isl_multi_pw_aff_free(mpa);
6511 return NULL;
6514 /* Construct a map mapping the shared domain
6515 * of the piecewise affine expressions to the range of "mpa"
6516 * with each dimension in the range equated to the
6517 * corresponding piecewise affine expression.
6519 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6521 if (!mpa)
6522 return NULL;
6523 if (isl_space_is_set(mpa->space))
6524 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6525 "space of input is not a map", goto error);
6527 return map_from_multi_pw_aff(mpa);
6528 error:
6529 isl_multi_pw_aff_free(mpa);
6530 return NULL;
6533 /* Construct a set mapping the shared parameter domain
6534 * of the piecewise affine expressions to the space of "mpa"
6535 * with each dimension in the range equated to the
6536 * corresponding piecewise affine expression.
6538 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6540 if (!mpa)
6541 return NULL;
6542 if (!isl_space_is_set(mpa->space))
6543 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6544 "space of input is not a set", goto error);
6546 return map_from_multi_pw_aff(mpa);
6547 error:
6548 isl_multi_pw_aff_free(mpa);
6549 return NULL;
6552 /* Construct and return a piecewise multi affine expression
6553 * that is equal to the given multi piecewise affine expression
6554 * on the shared domain of the piecewise affine expressions,
6555 * in the special case of a 0D multi piecewise affine expression.
6557 * Create a piecewise multi affine expression with the explicit domain of
6558 * the 0D multi piecewise affine expression as domain.
6560 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6561 __isl_take isl_multi_pw_aff *mpa)
6563 isl_space *space;
6564 isl_set *dom;
6565 isl_multi_aff *ma;
6567 space = isl_multi_pw_aff_get_space(mpa);
6568 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6569 isl_multi_pw_aff_free(mpa);
6571 ma = isl_multi_aff_zero(space);
6572 return isl_pw_multi_aff_alloc(dom, ma);
6575 /* Construct and return a piecewise multi affine expression
6576 * that is equal to the given multi piecewise affine expression
6577 * on the shared domain of the piecewise affine expressions.
6579 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6580 __isl_take isl_multi_pw_aff *mpa)
6582 int i;
6583 isl_space *space;
6584 isl_pw_aff *pa;
6585 isl_pw_multi_aff *pma;
6587 if (!mpa)
6588 return NULL;
6590 if (mpa->n == 0)
6591 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6593 space = isl_multi_pw_aff_get_space(mpa);
6594 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6595 pma = isl_pw_multi_aff_from_pw_aff(pa);
6597 for (i = 1; i < mpa->n; ++i) {
6598 isl_pw_multi_aff *pma_i;
6600 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6601 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6602 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6605 pma = isl_pw_multi_aff_reset_space(pma, space);
6607 isl_multi_pw_aff_free(mpa);
6608 return pma;
6611 /* Construct and return a multi piecewise affine expression
6612 * that is equal to the given multi affine expression.
6614 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6615 __isl_take isl_multi_aff *ma)
6617 int i, n;
6618 isl_multi_pw_aff *mpa;
6620 if (!ma)
6621 return NULL;
6623 n = isl_multi_aff_dim(ma, isl_dim_out);
6624 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6626 for (i = 0; i < n; ++i) {
6627 isl_pw_aff *pa;
6629 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6630 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6633 isl_multi_aff_free(ma);
6634 return mpa;
6637 /* Construct and return a multi piecewise affine expression
6638 * that is equal to the given piecewise multi affine expression.
6640 * If the resulting multi piecewise affine expression has
6641 * an explicit domain, then assign it the domain of the input.
6642 * In other cases, the domain is stored in the individual elements.
6644 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6645 __isl_take isl_pw_multi_aff *pma)
6647 int i, n;
6648 isl_space *space;
6649 isl_multi_pw_aff *mpa;
6651 if (!pma)
6652 return NULL;
6654 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6655 space = isl_pw_multi_aff_get_space(pma);
6656 mpa = isl_multi_pw_aff_alloc(space);
6658 for (i = 0; i < n; ++i) {
6659 isl_pw_aff *pa;
6661 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6662 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6664 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6665 isl_set *dom;
6667 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6668 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6671 isl_pw_multi_aff_free(pma);
6672 return mpa;
6675 /* Do "pa1" and "pa2" represent the same function?
6677 * We first check if they are obviously equal.
6678 * If not, we convert them to maps and check if those are equal.
6680 * If "pa1" or "pa2" contain any NaNs, then they are considered
6681 * not to be the same. A NaN is not equal to anything, not even
6682 * to another NaN.
6684 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6685 __isl_keep isl_pw_aff *pa2)
6687 isl_bool equal;
6688 isl_bool has_nan;
6689 isl_map *map1, *map2;
6691 if (!pa1 || !pa2)
6692 return isl_bool_error;
6694 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6695 if (equal < 0 || equal)
6696 return equal;
6697 has_nan = either_involves_nan(pa1, pa2);
6698 if (has_nan < 0)
6699 return isl_bool_error;
6700 if (has_nan)
6701 return isl_bool_false;
6703 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6704 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6705 equal = isl_map_is_equal(map1, map2);
6706 isl_map_free(map1);
6707 isl_map_free(map2);
6709 return equal;
6712 /* Do "mpa1" and "mpa2" represent the same function?
6714 * Note that we cannot convert the entire isl_multi_pw_aff
6715 * to a map because the domains of the piecewise affine expressions
6716 * may not be the same.
6718 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6719 __isl_keep isl_multi_pw_aff *mpa2)
6721 int i;
6722 isl_bool equal, equal_params;
6724 if (!mpa1 || !mpa2)
6725 return isl_bool_error;
6727 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6728 if (equal_params < 0)
6729 return isl_bool_error;
6730 if (!equal_params) {
6731 if (!isl_space_has_named_params(mpa1->space))
6732 return isl_bool_false;
6733 if (!isl_space_has_named_params(mpa2->space))
6734 return isl_bool_false;
6735 mpa1 = isl_multi_pw_aff_copy(mpa1);
6736 mpa2 = isl_multi_pw_aff_copy(mpa2);
6737 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6738 isl_multi_pw_aff_get_space(mpa2));
6739 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6740 isl_multi_pw_aff_get_space(mpa1));
6741 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6742 isl_multi_pw_aff_free(mpa1);
6743 isl_multi_pw_aff_free(mpa2);
6744 return equal;
6747 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6748 if (equal < 0 || !equal)
6749 return equal;
6751 for (i = 0; i < mpa1->n; ++i) {
6752 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6753 if (equal < 0 || !equal)
6754 return equal;
6757 return isl_bool_true;
6760 /* Do "pma1" and "pma2" represent the same function?
6762 * First check if they are obviously equal.
6763 * If not, then convert them to maps and check if those are equal.
6765 * If "pa1" or "pa2" contain any NaNs, then they are considered
6766 * not to be the same. A NaN is not equal to anything, not even
6767 * to another NaN.
6769 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6770 __isl_keep isl_pw_multi_aff *pma2)
6772 isl_bool equal;
6773 isl_bool has_nan;
6774 isl_map *map1, *map2;
6776 if (!pma1 || !pma2)
6777 return isl_bool_error;
6779 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6780 if (equal < 0 || equal)
6781 return equal;
6782 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6783 if (has_nan >= 0 && !has_nan)
6784 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6785 if (has_nan < 0 || has_nan)
6786 return isl_bool_not(has_nan);
6788 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6789 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6790 equal = isl_map_is_equal(map1, map2);
6791 isl_map_free(map1);
6792 isl_map_free(map2);
6794 return equal;
6797 /* Compute the pullback of "mpa" by the function represented by "ma".
6798 * In other words, plug in "ma" in "mpa".
6800 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6802 * If "mpa" has an explicit domain, then it is this domain
6803 * that needs to undergo a pullback, i.e., a preimage.
6805 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6806 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6808 int i;
6809 isl_space *space = NULL;
6811 mpa = isl_multi_pw_aff_cow(mpa);
6812 if (!mpa || !ma)
6813 goto error;
6815 space = isl_space_join(isl_multi_aff_get_space(ma),
6816 isl_multi_pw_aff_get_space(mpa));
6817 if (!space)
6818 goto error;
6820 for (i = 0; i < mpa->n; ++i) {
6821 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6822 isl_multi_aff_copy(ma));
6823 if (!mpa->u.p[i])
6824 goto error;
6826 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6827 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6828 isl_multi_aff_copy(ma));
6829 if (!mpa->u.dom)
6830 goto error;
6833 isl_multi_aff_free(ma);
6834 isl_space_free(mpa->space);
6835 mpa->space = space;
6836 return mpa;
6837 error:
6838 isl_space_free(space);
6839 isl_multi_pw_aff_free(mpa);
6840 isl_multi_aff_free(ma);
6841 return NULL;
6844 /* Compute the pullback of "mpa" by the function represented by "ma".
6845 * In other words, plug in "ma" in "mpa".
6847 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6848 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6850 isl_bool equal_params;
6852 if (!mpa || !ma)
6853 goto error;
6854 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6855 if (equal_params < 0)
6856 goto error;
6857 if (equal_params)
6858 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6859 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6860 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6861 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6862 error:
6863 isl_multi_pw_aff_free(mpa);
6864 isl_multi_aff_free(ma);
6865 return NULL;
6868 /* Compute the pullback of "mpa" by the function represented by "pma".
6869 * In other words, plug in "pma" in "mpa".
6871 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6873 * If "mpa" has an explicit domain, then it is this domain
6874 * that needs to undergo a pullback, i.e., a preimage.
6876 static __isl_give isl_multi_pw_aff *
6877 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6878 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6880 int i;
6881 isl_space *space = NULL;
6883 mpa = isl_multi_pw_aff_cow(mpa);
6884 if (!mpa || !pma)
6885 goto error;
6887 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6888 isl_multi_pw_aff_get_space(mpa));
6890 for (i = 0; i < mpa->n; ++i) {
6891 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6892 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6893 if (!mpa->u.p[i])
6894 goto error;
6896 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6897 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6898 isl_pw_multi_aff_copy(pma));
6899 if (!mpa->u.dom)
6900 goto error;
6903 isl_pw_multi_aff_free(pma);
6904 isl_space_free(mpa->space);
6905 mpa->space = space;
6906 return mpa;
6907 error:
6908 isl_space_free(space);
6909 isl_multi_pw_aff_free(mpa);
6910 isl_pw_multi_aff_free(pma);
6911 return NULL;
6914 /* Compute the pullback of "mpa" by the function represented by "pma".
6915 * In other words, plug in "pma" in "mpa".
6917 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6918 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6920 isl_bool equal_params;
6922 if (!mpa || !pma)
6923 goto error;
6924 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6925 if (equal_params < 0)
6926 goto error;
6927 if (equal_params)
6928 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6929 mpa = isl_multi_pw_aff_align_params(mpa,
6930 isl_pw_multi_aff_get_space(pma));
6931 pma = isl_pw_multi_aff_align_params(pma,
6932 isl_multi_pw_aff_get_space(mpa));
6933 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6934 error:
6935 isl_multi_pw_aff_free(mpa);
6936 isl_pw_multi_aff_free(pma);
6937 return NULL;
6940 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6941 * with the domain of "aff". The domain of the result is the same
6942 * as that of "mpa".
6943 * "mpa" and "aff" are assumed to have been aligned.
6945 * We first extract the parametric constant from "aff", defined
6946 * over the correct domain.
6947 * Then we add the appropriate combinations of the members of "mpa".
6948 * Finally, we add the integer divisions through recursive calls.
6950 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6951 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6953 int i, n_in, n_div;
6954 isl_space *space;
6955 isl_val *v;
6956 isl_pw_aff *pa;
6957 isl_aff *tmp;
6959 n_in = isl_aff_dim(aff, isl_dim_in);
6960 n_div = isl_aff_dim(aff, isl_dim_div);
6962 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6963 tmp = isl_aff_copy(aff);
6964 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6965 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6966 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6967 isl_space_dim(space, isl_dim_set));
6968 tmp = isl_aff_reset_domain_space(tmp, space);
6969 pa = isl_pw_aff_from_aff(tmp);
6971 for (i = 0; i < n_in; ++i) {
6972 isl_pw_aff *pa_i;
6974 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6975 continue;
6976 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6977 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6978 pa_i = isl_pw_aff_scale_val(pa_i, v);
6979 pa = isl_pw_aff_add(pa, pa_i);
6982 for (i = 0; i < n_div; ++i) {
6983 isl_aff *div;
6984 isl_pw_aff *pa_i;
6986 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6987 continue;
6988 div = isl_aff_get_div(aff, i);
6989 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6990 isl_multi_pw_aff_copy(mpa), div);
6991 pa_i = isl_pw_aff_floor(pa_i);
6992 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6993 pa_i = isl_pw_aff_scale_val(pa_i, v);
6994 pa = isl_pw_aff_add(pa, pa_i);
6997 isl_multi_pw_aff_free(mpa);
6998 isl_aff_free(aff);
7000 return pa;
7003 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7004 * with the domain of "aff". The domain of the result is the same
7005 * as that of "mpa".
7007 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7008 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7010 isl_bool equal_params;
7012 if (!aff || !mpa)
7013 goto error;
7014 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7015 if (equal_params < 0)
7016 goto error;
7017 if (equal_params)
7018 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7020 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7021 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7023 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7024 error:
7025 isl_aff_free(aff);
7026 isl_multi_pw_aff_free(mpa);
7027 return NULL;
7030 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7031 * with the domain of "pa". The domain of the result is the same
7032 * as that of "mpa".
7033 * "mpa" and "pa" are assumed to have been aligned.
7035 * We consider each piece in turn. Note that the domains of the
7036 * pieces are assumed to be disjoint and they remain disjoint
7037 * after taking the preimage (over the same function).
7039 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7040 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7042 isl_space *space;
7043 isl_pw_aff *res;
7044 int i;
7046 if (!mpa || !pa)
7047 goto error;
7049 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7050 isl_pw_aff_get_space(pa));
7051 res = isl_pw_aff_empty(space);
7053 for (i = 0; i < pa->n; ++i) {
7054 isl_pw_aff *pa_i;
7055 isl_set *domain;
7057 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7058 isl_multi_pw_aff_copy(mpa),
7059 isl_aff_copy(pa->p[i].aff));
7060 domain = isl_set_copy(pa->p[i].set);
7061 domain = isl_set_preimage_multi_pw_aff(domain,
7062 isl_multi_pw_aff_copy(mpa));
7063 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7064 res = isl_pw_aff_add_disjoint(res, pa_i);
7067 isl_pw_aff_free(pa);
7068 isl_multi_pw_aff_free(mpa);
7069 return res;
7070 error:
7071 isl_pw_aff_free(pa);
7072 isl_multi_pw_aff_free(mpa);
7073 return NULL;
7076 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7077 * with the domain of "pa". The domain of the result is the same
7078 * as that of "mpa".
7080 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7081 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7083 isl_bool equal_params;
7085 if (!pa || !mpa)
7086 goto error;
7087 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7088 if (equal_params < 0)
7089 goto error;
7090 if (equal_params)
7091 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7093 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7094 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7096 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7097 error:
7098 isl_pw_aff_free(pa);
7099 isl_multi_pw_aff_free(mpa);
7100 return NULL;
7103 /* Compute the pullback of "pa" by the function represented by "mpa".
7104 * In other words, plug in "mpa" in "pa".
7105 * "pa" and "mpa" are assumed to have been aligned.
7107 * The pullback is computed by applying "pa" to "mpa".
7109 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7110 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7112 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7115 /* Compute the pullback of "pa" by the function represented by "mpa".
7116 * In other words, plug in "mpa" in "pa".
7118 * The pullback is computed by applying "pa" to "mpa".
7120 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7121 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7123 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7126 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7127 * In other words, plug in "mpa2" in "mpa1".
7129 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7131 * We pullback each member of "mpa1" in turn.
7133 * If "mpa1" has an explicit domain, then it is this domain
7134 * that needs to undergo a pullback instead, i.e., a preimage.
7136 static __isl_give isl_multi_pw_aff *
7137 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7138 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7140 int i;
7141 isl_space *space = NULL;
7143 mpa1 = isl_multi_pw_aff_cow(mpa1);
7144 if (!mpa1 || !mpa2)
7145 goto error;
7147 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7148 isl_multi_pw_aff_get_space(mpa1));
7150 for (i = 0; i < mpa1->n; ++i) {
7151 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7152 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7153 if (!mpa1->u.p[i])
7154 goto error;
7157 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7158 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7159 isl_multi_pw_aff_copy(mpa2));
7160 if (!mpa1->u.dom)
7161 goto error;
7163 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7165 isl_multi_pw_aff_free(mpa2);
7166 return mpa1;
7167 error:
7168 isl_space_free(space);
7169 isl_multi_pw_aff_free(mpa1);
7170 isl_multi_pw_aff_free(mpa2);
7171 return NULL;
7174 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7175 * In other words, plug in "mpa2" in "mpa1".
7177 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7178 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7180 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7181 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7184 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7185 * of "mpa1" and "mpa2" live in the same space, construct map space
7186 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7187 * with this map space as extract argument.
7189 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7190 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7191 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7192 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7194 int match;
7195 isl_space *space1, *space2;
7196 isl_map *res;
7198 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7199 isl_multi_pw_aff_get_space(mpa2));
7200 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7201 isl_multi_pw_aff_get_space(mpa1));
7202 if (!mpa1 || !mpa2)
7203 goto error;
7204 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7205 mpa2->space, isl_dim_out);
7206 if (match < 0)
7207 goto error;
7208 if (!match)
7209 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7210 "range spaces don't match", goto error);
7211 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7212 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7213 space1 = isl_space_map_from_domain_and_range(space1, space2);
7215 res = order(mpa1, mpa2, space1);
7216 isl_multi_pw_aff_free(mpa1);
7217 isl_multi_pw_aff_free(mpa2);
7218 return res;
7219 error:
7220 isl_multi_pw_aff_free(mpa1);
7221 isl_multi_pw_aff_free(mpa2);
7222 return NULL;
7225 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7226 * where the function values are equal. "space" is the space of the result.
7227 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7229 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7230 * in the sequences are equal.
7232 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7233 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7234 __isl_take isl_space *space)
7236 int i, n;
7237 isl_map *res;
7239 res = isl_map_universe(space);
7241 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7242 for (i = 0; i < n; ++i) {
7243 isl_pw_aff *pa1, *pa2;
7244 isl_map *map;
7246 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7247 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7248 map = isl_pw_aff_eq_map(pa1, pa2);
7249 res = isl_map_intersect(res, map);
7252 return res;
7255 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7256 * where the function values are equal.
7258 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7259 __isl_take isl_multi_pw_aff *mpa2)
7261 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7262 &isl_multi_pw_aff_eq_map_on_space);
7265 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7266 * where the function values of "mpa1" is lexicographically satisfies "base"
7267 * compared to that of "mpa2". "space" is the space of the result.
7268 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7270 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7271 * if its i-th element satisfies "base" when compared to
7272 * the i-th element of "mpa2" while all previous elements are
7273 * pairwise equal.
7275 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7276 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7277 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7278 __isl_take isl_pw_aff *pa2),
7279 __isl_take isl_space *space)
7281 int i, n;
7282 isl_map *res, *rest;
7284 res = isl_map_empty(isl_space_copy(space));
7285 rest = isl_map_universe(space);
7287 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7288 for (i = 0; i < n; ++i) {
7289 isl_pw_aff *pa1, *pa2;
7290 isl_map *map;
7292 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7293 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7294 map = base(pa1, pa2);
7295 map = isl_map_intersect(map, isl_map_copy(rest));
7296 res = isl_map_union(res, map);
7298 if (i == n - 1)
7299 continue;
7301 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7302 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7303 map = isl_pw_aff_eq_map(pa1, pa2);
7304 rest = isl_map_intersect(rest, map);
7307 isl_map_free(rest);
7308 return res;
7311 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7312 * where the function value of "mpa1" is lexicographically less than that
7313 * of "mpa2". "space" is the space of the result.
7314 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7316 * "mpa1" is less than "mpa2" if its i-th element is smaller
7317 * than the i-th element of "mpa2" while all previous elements are
7318 * pairwise equal.
7320 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7321 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7322 __isl_take isl_space *space)
7324 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7325 &isl_pw_aff_lt_map, space);
7328 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7329 * where the function value of "mpa1" is lexicographically less than that
7330 * of "mpa2".
7332 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7333 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7335 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7336 &isl_multi_pw_aff_lex_lt_map_on_space);
7339 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7340 * where the function value of "mpa1" is lexicographically greater than that
7341 * of "mpa2". "space" is the space of the result.
7342 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7344 * "mpa1" is greater than "mpa2" if its i-th element is greater
7345 * than the i-th element of "mpa2" while all previous elements are
7346 * pairwise equal.
7348 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7349 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7350 __isl_take isl_space *space)
7352 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7353 &isl_pw_aff_gt_map, space);
7356 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7357 * where the function value of "mpa1" is lexicographically greater than that
7358 * of "mpa2".
7360 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7361 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7363 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7364 &isl_multi_pw_aff_lex_gt_map_on_space);
7367 /* Compare two isl_affs.
7369 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7370 * than "aff2" and 0 if they are equal.
7372 * The order is fairly arbitrary. We do consider expressions that only involve
7373 * earlier dimensions as "smaller".
7375 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7377 int cmp;
7378 int last1, last2;
7380 if (aff1 == aff2)
7381 return 0;
7383 if (!aff1)
7384 return -1;
7385 if (!aff2)
7386 return 1;
7388 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7389 if (cmp != 0)
7390 return cmp;
7392 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7393 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7394 if (last1 != last2)
7395 return last1 - last2;
7397 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7400 /* Compare two isl_pw_affs.
7402 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7403 * than "pa2" and 0 if they are equal.
7405 * The order is fairly arbitrary. We do consider expressions that only involve
7406 * earlier dimensions as "smaller".
7408 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7409 __isl_keep isl_pw_aff *pa2)
7411 int i;
7412 int cmp;
7414 if (pa1 == pa2)
7415 return 0;
7417 if (!pa1)
7418 return -1;
7419 if (!pa2)
7420 return 1;
7422 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7423 if (cmp != 0)
7424 return cmp;
7426 if (pa1->n != pa2->n)
7427 return pa1->n - pa2->n;
7429 for (i = 0; i < pa1->n; ++i) {
7430 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7431 if (cmp != 0)
7432 return cmp;
7433 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7434 if (cmp != 0)
7435 return cmp;
7438 return 0;
7441 /* Return a piecewise affine expression that is equal to "v" on "domain".
7443 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7444 __isl_take isl_val *v)
7446 isl_space *space;
7447 isl_local_space *ls;
7448 isl_aff *aff;
7450 space = isl_set_get_space(domain);
7451 ls = isl_local_space_from_space(space);
7452 aff = isl_aff_val_on_domain(ls, v);
7454 return isl_pw_aff_alloc(domain, aff);
7457 /* Return a multi affine expression that is equal to "mv" on domain
7458 * space "space".
7460 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7461 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7463 int i, n;
7464 isl_space *space2;
7465 isl_local_space *ls;
7466 isl_multi_aff *ma;
7468 if (!space || !mv)
7469 goto error;
7471 n = isl_multi_val_dim(mv, isl_dim_set);
7472 space2 = isl_multi_val_get_space(mv);
7473 space2 = isl_space_align_params(space2, isl_space_copy(space));
7474 space = isl_space_align_params(space, isl_space_copy(space2));
7475 space = isl_space_map_from_domain_and_range(space, space2);
7476 ma = isl_multi_aff_alloc(isl_space_copy(space));
7477 ls = isl_local_space_from_space(isl_space_domain(space));
7478 for (i = 0; i < n; ++i) {
7479 isl_val *v;
7480 isl_aff *aff;
7482 v = isl_multi_val_get_val(mv, i);
7483 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7484 ma = isl_multi_aff_set_aff(ma, i, aff);
7486 isl_local_space_free(ls);
7488 isl_multi_val_free(mv);
7489 return ma;
7490 error:
7491 isl_space_free(space);
7492 isl_multi_val_free(mv);
7493 return NULL;
7496 /* Return a piecewise multi-affine expression
7497 * that is equal to "mv" on "domain".
7499 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7500 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7502 isl_space *space;
7503 isl_multi_aff *ma;
7505 space = isl_set_get_space(domain);
7506 ma = isl_multi_aff_multi_val_on_space(space, mv);
7508 return isl_pw_multi_aff_alloc(domain, ma);
7511 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7512 * mv is the value that should be attained on each domain set
7513 * res collects the results
7515 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7516 isl_multi_val *mv;
7517 isl_union_pw_multi_aff *res;
7520 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7521 * and add it to data->res.
7523 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7524 void *user)
7526 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7527 isl_pw_multi_aff *pma;
7528 isl_multi_val *mv;
7530 mv = isl_multi_val_copy(data->mv);
7531 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7532 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7534 return data->res ? isl_stat_ok : isl_stat_error;
7537 /* Return a union piecewise multi-affine expression
7538 * that is equal to "mv" on "domain".
7540 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7541 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7543 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7544 isl_space *space;
7546 space = isl_union_set_get_space(domain);
7547 data.res = isl_union_pw_multi_aff_empty(space);
7548 data.mv = mv;
7549 if (isl_union_set_foreach_set(domain,
7550 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7551 data.res = isl_union_pw_multi_aff_free(data.res);
7552 isl_union_set_free(domain);
7553 isl_multi_val_free(mv);
7554 return data.res;
7557 /* Compute the pullback of data->pma by the function represented by "pma2",
7558 * provided the spaces match, and add the results to data->res.
7560 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7562 struct isl_union_pw_multi_aff_bin_data *data = user;
7564 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7565 pma2->dim, isl_dim_out)) {
7566 isl_pw_multi_aff_free(pma2);
7567 return isl_stat_ok;
7570 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7571 isl_pw_multi_aff_copy(data->pma), pma2);
7573 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7574 if (!data->res)
7575 return isl_stat_error;
7577 return isl_stat_ok;
7580 /* Compute the pullback of "upma1" by the function represented by "upma2".
7582 __isl_give isl_union_pw_multi_aff *
7583 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7584 __isl_take isl_union_pw_multi_aff *upma1,
7585 __isl_take isl_union_pw_multi_aff *upma2)
7587 return bin_op(upma1, upma2, &pullback_entry);
7590 /* Check that the domain space of "upa" matches "space".
7592 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7593 * can in principle never fail since the space "space" is that
7594 * of the isl_multi_union_pw_aff and is a set space such that
7595 * there is no domain space to match.
7597 * We check the parameters and double-check that "space" is
7598 * indeed that of a set.
7600 static isl_stat isl_union_pw_aff_check_match_domain_space(
7601 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7603 isl_space *upa_space;
7604 isl_bool match;
7606 if (!upa || !space)
7607 return isl_stat_error;
7609 match = isl_space_is_set(space);
7610 if (match < 0)
7611 return isl_stat_error;
7612 if (!match)
7613 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7614 "expecting set space", return isl_stat_error);
7616 upa_space = isl_union_pw_aff_get_space(upa);
7617 match = isl_space_has_equal_params(space, upa_space);
7618 if (match < 0)
7619 goto error;
7620 if (!match)
7621 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7622 "parameters don't match", goto error);
7624 isl_space_free(upa_space);
7625 return isl_stat_ok;
7626 error:
7627 isl_space_free(upa_space);
7628 return isl_stat_error;
7631 /* Do the parameters of "upa" match those of "space"?
7633 static isl_bool isl_union_pw_aff_matching_params(
7634 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7636 isl_space *upa_space;
7637 isl_bool match;
7639 if (!upa || !space)
7640 return isl_bool_error;
7642 upa_space = isl_union_pw_aff_get_space(upa);
7644 match = isl_space_has_equal_params(space, upa_space);
7646 isl_space_free(upa_space);
7647 return match;
7650 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7651 * space represents the new parameters.
7652 * res collects the results.
7654 struct isl_union_pw_aff_reset_params_data {
7655 isl_space *space;
7656 isl_union_pw_aff *res;
7659 /* Replace the parameters of "pa" by data->space and
7660 * add the result to data->res.
7662 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7664 struct isl_union_pw_aff_reset_params_data *data = user;
7665 isl_space *space;
7667 space = isl_pw_aff_get_space(pa);
7668 space = isl_space_replace_params(space, data->space);
7669 pa = isl_pw_aff_reset_space(pa, space);
7670 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7672 return data->res ? isl_stat_ok : isl_stat_error;
7675 /* Replace the domain space of "upa" by "space".
7676 * Since a union expression does not have a (single) domain space,
7677 * "space" is necessarily a parameter space.
7679 * Since the order and the names of the parameters determine
7680 * the hash value, we need to create a new hash table.
7682 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7683 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7685 struct isl_union_pw_aff_reset_params_data data = { space };
7686 isl_bool match;
7688 match = isl_union_pw_aff_matching_params(upa, space);
7689 if (match < 0)
7690 upa = isl_union_pw_aff_free(upa);
7691 else if (match) {
7692 isl_space_free(space);
7693 return upa;
7696 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7697 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7698 data.res = isl_union_pw_aff_free(data.res);
7700 isl_union_pw_aff_free(upa);
7701 isl_space_free(space);
7702 return data.res;
7705 /* Return the floor of "pa".
7707 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7709 return isl_pw_aff_floor(pa);
7712 /* Given f, return floor(f).
7714 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7715 __isl_take isl_union_pw_aff *upa)
7717 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7720 /* Compute
7722 * upa mod m = upa - m * floor(upa/m)
7724 * with m an integer value.
7726 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7727 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7729 isl_union_pw_aff *res;
7731 if (!upa || !m)
7732 goto error;
7734 if (!isl_val_is_int(m))
7735 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7736 "expecting integer modulo", goto error);
7737 if (!isl_val_is_pos(m))
7738 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7739 "expecting positive modulo", goto error);
7741 res = isl_union_pw_aff_copy(upa);
7742 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7743 upa = isl_union_pw_aff_floor(upa);
7744 upa = isl_union_pw_aff_scale_val(upa, m);
7745 res = isl_union_pw_aff_sub(res, upa);
7747 return res;
7748 error:
7749 isl_val_free(m);
7750 isl_union_pw_aff_free(upa);
7751 return NULL;
7754 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7755 * pos is the output position that needs to be extracted.
7756 * res collects the results.
7758 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7759 int pos;
7760 isl_union_pw_aff *res;
7763 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7764 * (assuming it has such a dimension) and add it to data->res.
7766 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7768 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7769 int n_out;
7770 isl_pw_aff *pa;
7772 if (!pma)
7773 return isl_stat_error;
7775 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7776 if (data->pos >= n_out) {
7777 isl_pw_multi_aff_free(pma);
7778 return isl_stat_ok;
7781 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7782 isl_pw_multi_aff_free(pma);
7784 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7786 return data->res ? isl_stat_ok : isl_stat_error;
7789 /* Extract an isl_union_pw_aff corresponding to
7790 * output dimension "pos" of "upma".
7792 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7793 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7795 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7796 isl_space *space;
7798 if (!upma)
7799 return NULL;
7801 if (pos < 0)
7802 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7803 "cannot extract at negative position", return NULL);
7805 space = isl_union_pw_multi_aff_get_space(upma);
7806 data.res = isl_union_pw_aff_empty(space);
7807 data.pos = pos;
7808 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7809 &get_union_pw_aff, &data) < 0)
7810 data.res = isl_union_pw_aff_free(data.res);
7812 return data.res;
7815 /* Return a union piecewise affine expression
7816 * that is equal to "aff" on "domain".
7818 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7819 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7821 isl_pw_aff *pa;
7823 pa = isl_pw_aff_from_aff(aff);
7824 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7827 /* Return a union piecewise affine expression
7828 * that is equal to the parameter identified by "id" on "domain".
7830 * Make sure the parameter appears in the space passed to
7831 * isl_aff_param_on_domain_space_id.
7833 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7834 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7836 isl_space *space;
7837 isl_aff *aff;
7839 space = isl_union_set_get_space(domain);
7840 space = isl_space_add_param_id(space, isl_id_copy(id));
7841 aff = isl_aff_param_on_domain_space_id(space, id);
7842 return isl_union_pw_aff_aff_on_domain(domain, aff);
7845 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7846 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7847 * needs to attain.
7848 * "res" collects the results.
7850 struct isl_union_pw_aff_pw_aff_on_domain_data {
7851 isl_pw_aff *pa;
7852 isl_union_pw_aff *res;
7855 /* Construct a piecewise affine expression that is equal to data->pa
7856 * on "domain" and add the result to data->res.
7858 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7860 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7861 isl_pw_aff *pa;
7862 int dim;
7864 pa = isl_pw_aff_copy(data->pa);
7865 dim = isl_set_dim(domain, isl_dim_set);
7866 pa = isl_pw_aff_from_range(pa);
7867 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7868 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7869 pa = isl_pw_aff_intersect_domain(pa, domain);
7870 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7872 return data->res ? isl_stat_ok : isl_stat_error;
7875 /* Return a union piecewise affine expression
7876 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7877 * have been aligned.
7879 * Construct an isl_pw_aff on each of the sets in "domain" and
7880 * collect the results.
7882 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7883 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7885 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7886 isl_space *space;
7888 space = isl_union_set_get_space(domain);
7889 data.res = isl_union_pw_aff_empty(space);
7890 data.pa = pa;
7891 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7892 data.res = isl_union_pw_aff_free(data.res);
7893 isl_union_set_free(domain);
7894 isl_pw_aff_free(pa);
7895 return data.res;
7898 /* Return a union piecewise affine expression
7899 * that is equal to "pa" on "domain".
7901 * Check that "pa" is a parametric expression,
7902 * align the parameters if needed and call
7903 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7905 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7906 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7908 isl_bool is_set;
7909 isl_bool equal_params;
7910 isl_space *domain_space, *pa_space;
7912 pa_space = isl_pw_aff_peek_space(pa);
7913 is_set = isl_space_is_set(pa_space);
7914 if (is_set < 0)
7915 goto error;
7916 if (!is_set)
7917 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7918 "expecting parametric expression", goto error);
7920 domain_space = isl_union_set_get_space(domain);
7921 pa_space = isl_pw_aff_get_space(pa);
7922 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7923 if (equal_params >= 0 && !equal_params) {
7924 isl_space *space;
7926 space = isl_space_align_params(domain_space, pa_space);
7927 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7928 domain = isl_union_set_align_params(domain, space);
7929 } else {
7930 isl_space_free(domain_space);
7931 isl_space_free(pa_space);
7934 if (equal_params < 0)
7935 goto error;
7936 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7937 error:
7938 isl_union_set_free(domain);
7939 isl_pw_aff_free(pa);
7940 return NULL;
7943 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7944 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7945 * "res" collects the results.
7947 struct isl_union_pw_aff_val_on_domain_data {
7948 isl_val *v;
7949 isl_union_pw_aff *res;
7952 /* Construct a piecewise affine expression that is equal to data->v
7953 * on "domain" and add the result to data->res.
7955 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7957 struct isl_union_pw_aff_val_on_domain_data *data = user;
7958 isl_pw_aff *pa;
7959 isl_val *v;
7961 v = isl_val_copy(data->v);
7962 pa = isl_pw_aff_val_on_domain(domain, v);
7963 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7965 return data->res ? isl_stat_ok : isl_stat_error;
7968 /* Return a union piecewise affine expression
7969 * that is equal to "v" on "domain".
7971 * Construct an isl_pw_aff on each of the sets in "domain" and
7972 * collect the results.
7974 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7975 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7977 struct isl_union_pw_aff_val_on_domain_data data;
7978 isl_space *space;
7980 space = isl_union_set_get_space(domain);
7981 data.res = isl_union_pw_aff_empty(space);
7982 data.v = v;
7983 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7984 data.res = isl_union_pw_aff_free(data.res);
7985 isl_union_set_free(domain);
7986 isl_val_free(v);
7987 return data.res;
7990 /* Construct a piecewise multi affine expression
7991 * that is equal to "pa" and add it to upma.
7993 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7994 void *user)
7996 isl_union_pw_multi_aff **upma = user;
7997 isl_pw_multi_aff *pma;
7999 pma = isl_pw_multi_aff_from_pw_aff(pa);
8000 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8002 return *upma ? isl_stat_ok : isl_stat_error;
8005 /* Construct and return a union piecewise multi affine expression
8006 * that is equal to the given union piecewise affine expression.
8008 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8009 __isl_take isl_union_pw_aff *upa)
8011 isl_space *space;
8012 isl_union_pw_multi_aff *upma;
8014 if (!upa)
8015 return NULL;
8017 space = isl_union_pw_aff_get_space(upa);
8018 upma = isl_union_pw_multi_aff_empty(space);
8020 if (isl_union_pw_aff_foreach_pw_aff(upa,
8021 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8022 upma = isl_union_pw_multi_aff_free(upma);
8024 isl_union_pw_aff_free(upa);
8025 return upma;
8028 /* Compute the set of elements in the domain of "pa" where it is zero and
8029 * add this set to "uset".
8031 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8033 isl_union_set **uset = (isl_union_set **)user;
8035 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8037 return *uset ? isl_stat_ok : isl_stat_error;
8040 /* Return a union set containing those elements in the domain
8041 * of "upa" where it is zero.
8043 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8044 __isl_take isl_union_pw_aff *upa)
8046 isl_union_set *zero;
8048 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8049 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8050 zero = isl_union_set_free(zero);
8052 isl_union_pw_aff_free(upa);
8053 return zero;
8056 /* Convert "pa" to an isl_map and add it to *umap.
8058 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8060 isl_union_map **umap = user;
8061 isl_map *map;
8063 map = isl_map_from_pw_aff(pa);
8064 *umap = isl_union_map_add_map(*umap, map);
8066 return *umap ? isl_stat_ok : isl_stat_error;
8069 /* Construct a union map mapping the domain of the union
8070 * piecewise affine expression to its range, with the single output dimension
8071 * equated to the corresponding affine expressions on their cells.
8073 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8074 __isl_take isl_union_pw_aff *upa)
8076 isl_space *space;
8077 isl_union_map *umap;
8079 if (!upa)
8080 return NULL;
8082 space = isl_union_pw_aff_get_space(upa);
8083 umap = isl_union_map_empty(space);
8085 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8086 &umap) < 0)
8087 umap = isl_union_map_free(umap);
8089 isl_union_pw_aff_free(upa);
8090 return umap;
8093 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8094 * upma is the function that is plugged in.
8095 * pa is the current part of the function in which upma is plugged in.
8096 * res collects the results.
8098 struct isl_union_pw_aff_pullback_upma_data {
8099 isl_union_pw_multi_aff *upma;
8100 isl_pw_aff *pa;
8101 isl_union_pw_aff *res;
8104 /* Check if "pma" can be plugged into data->pa.
8105 * If so, perform the pullback and add the result to data->res.
8107 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8109 struct isl_union_pw_aff_pullback_upma_data *data = user;
8110 isl_pw_aff *pa;
8112 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8113 pma->dim, isl_dim_out)) {
8114 isl_pw_multi_aff_free(pma);
8115 return isl_stat_ok;
8118 pa = isl_pw_aff_copy(data->pa);
8119 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8121 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8123 return data->res ? isl_stat_ok : isl_stat_error;
8126 /* Check if any of the elements of data->upma can be plugged into pa,
8127 * add if so add the result to data->res.
8129 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8131 struct isl_union_pw_aff_pullback_upma_data *data = user;
8132 isl_stat r;
8134 data->pa = pa;
8135 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8136 &pa_pb_pma, data);
8137 isl_pw_aff_free(pa);
8139 return r;
8142 /* Compute the pullback of "upa" by the function represented by "upma".
8143 * In other words, plug in "upma" in "upa". The result contains
8144 * expressions defined over the domain space of "upma".
8146 * Run over all pairs of elements in "upa" and "upma", perform
8147 * the pullback when appropriate and collect the results.
8148 * If the hash value were based on the domain space rather than
8149 * the function space, then we could run through all elements
8150 * of "upma" and directly pick out the corresponding element of "upa".
8152 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8153 __isl_take isl_union_pw_aff *upa,
8154 __isl_take isl_union_pw_multi_aff *upma)
8156 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8157 isl_space *space;
8159 space = isl_union_pw_multi_aff_get_space(upma);
8160 upa = isl_union_pw_aff_align_params(upa, space);
8161 space = isl_union_pw_aff_get_space(upa);
8162 upma = isl_union_pw_multi_aff_align_params(upma, space);
8164 if (!upa || !upma)
8165 goto error;
8167 data.upma = upma;
8168 data.res = isl_union_pw_aff_alloc_same_size(upa);
8169 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8170 data.res = isl_union_pw_aff_free(data.res);
8172 isl_union_pw_aff_free(upa);
8173 isl_union_pw_multi_aff_free(upma);
8174 return data.res;
8175 error:
8176 isl_union_pw_aff_free(upa);
8177 isl_union_pw_multi_aff_free(upma);
8178 return NULL;
8181 #undef BASE
8182 #define BASE union_pw_aff
8183 #undef DOMBASE
8184 #define DOMBASE union_set
8186 #define NO_MOVE_DIMS
8187 #define NO_DOMAIN
8188 #define NO_PRODUCT
8189 #define NO_SPLICE
8190 #define NO_ZERO
8191 #define NO_IDENTITY
8193 #include <isl_multi_explicit_domain.c>
8194 #include <isl_multi_union_pw_aff_explicit_domain.c>
8195 #include <isl_multi_templ.c>
8196 #include <isl_multi_apply_set.c>
8197 #include <isl_multi_apply_union_set.c>
8198 #include <isl_multi_coalesce.c>
8199 #include <isl_multi_floor.c>
8200 #include <isl_multi_gist.c>
8201 #include <isl_multi_align_set.c>
8202 #include <isl_multi_align_union_set.c>
8203 #include <isl_multi_intersect.c>
8205 /* Does "mupa" have a non-trivial explicit domain?
8207 * The explicit domain, if present, is trivial if it represents
8208 * an (obviously) universe parameter set.
8210 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8211 __isl_keep isl_multi_union_pw_aff *mupa)
8213 isl_bool is_params, trivial;
8214 isl_set *set;
8216 if (!mupa)
8217 return isl_bool_error;
8218 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8219 return isl_bool_false;
8220 is_params = isl_union_set_is_params(mupa->u.dom);
8221 if (is_params < 0 || !is_params)
8222 return isl_bool_not(is_params);
8223 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8224 trivial = isl_set_plain_is_universe(set);
8225 isl_set_free(set);
8226 return isl_bool_not(trivial);
8229 /* Construct a multiple union piecewise affine expression
8230 * in the given space with value zero in each of the output dimensions.
8232 * Since there is no canonical zero value for
8233 * a union piecewise affine expression, we can only construct
8234 * a zero-dimensional "zero" value.
8236 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8237 __isl_take isl_space *space)
8239 isl_bool params;
8241 if (!space)
8242 return NULL;
8244 params = isl_space_is_params(space);
8245 if (params < 0)
8246 goto error;
8247 if (params)
8248 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8249 "expecting proper set space", goto error);
8250 if (!isl_space_is_set(space))
8251 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8252 "expecting set space", goto error);
8253 if (isl_space_dim(space , isl_dim_out) != 0)
8254 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8255 "expecting 0D space", goto error);
8257 return isl_multi_union_pw_aff_alloc(space);
8258 error:
8259 isl_space_free(space);
8260 return NULL;
8263 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8264 * with the actual sum on the shared domain and
8265 * the defined expression on the symmetric difference of the domains.
8267 * We simply iterate over the elements in both arguments and
8268 * call isl_union_pw_aff_union_add on each of them, if there is
8269 * at least one element.
8271 * Otherwise, the two expressions have an explicit domain and
8272 * the union of these explicit domains is computed.
8273 * This assumes that the explicit domains are either both in terms
8274 * of specific domains elements or both in terms of parameters.
8275 * However, if one of the expressions does not have any constraints
8276 * on its explicit domain, then this is allowed as well and the result
8277 * is the expression with no constraints on its explicit domain.
8279 static __isl_give isl_multi_union_pw_aff *
8280 isl_multi_union_pw_aff_union_add_aligned(
8281 __isl_take isl_multi_union_pw_aff *mupa1,
8282 __isl_take isl_multi_union_pw_aff *mupa2)
8284 isl_bool has_domain, is_params1, is_params2;
8286 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8287 goto error;
8288 if (mupa1->n > 0)
8289 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8290 &isl_union_pw_aff_union_add);
8291 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8292 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8293 goto error;
8295 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8296 if (has_domain < 0)
8297 goto error;
8298 if (!has_domain) {
8299 isl_multi_union_pw_aff_free(mupa2);
8300 return mupa1;
8302 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8303 if (has_domain < 0)
8304 goto error;
8305 if (!has_domain) {
8306 isl_multi_union_pw_aff_free(mupa1);
8307 return mupa2;
8310 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8311 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8312 if (is_params1 < 0 || is_params2 < 0)
8313 goto error;
8314 if (is_params1 != is_params2)
8315 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8316 isl_error_invalid,
8317 "cannot compute union of concrete domain and "
8318 "parameter constraints", goto error);
8319 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8320 if (!mupa1)
8321 goto error;
8322 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8323 isl_union_set_copy(mupa2->u.dom));
8324 if (!mupa1->u.dom)
8325 goto error;
8326 isl_multi_union_pw_aff_free(mupa2);
8327 return mupa1;
8328 error:
8329 isl_multi_union_pw_aff_free(mupa1);
8330 isl_multi_union_pw_aff_free(mupa2);
8331 return NULL;
8334 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8335 * with the actual sum on the shared domain and
8336 * the defined expression on the symmetric difference of the domains.
8338 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8339 __isl_take isl_multi_union_pw_aff *mupa1,
8340 __isl_take isl_multi_union_pw_aff *mupa2)
8342 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8343 &isl_multi_union_pw_aff_union_add_aligned);
8346 /* Construct and return a multi union piecewise affine expression
8347 * that is equal to the given multi affine expression.
8349 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8350 __isl_take isl_multi_aff *ma)
8352 isl_multi_pw_aff *mpa;
8354 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8355 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8358 /* Construct and return a multi union piecewise affine expression
8359 * that is equal to the given multi piecewise affine expression.
8361 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8362 __isl_take isl_multi_pw_aff *mpa)
8364 int i, n;
8365 isl_space *space;
8366 isl_multi_union_pw_aff *mupa;
8368 if (!mpa)
8369 return NULL;
8371 space = isl_multi_pw_aff_get_space(mpa);
8372 space = isl_space_range(space);
8373 mupa = isl_multi_union_pw_aff_alloc(space);
8375 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8376 for (i = 0; i < n; ++i) {
8377 isl_pw_aff *pa;
8378 isl_union_pw_aff *upa;
8380 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8381 upa = isl_union_pw_aff_from_pw_aff(pa);
8382 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8385 isl_multi_pw_aff_free(mpa);
8387 return mupa;
8390 /* Extract the range space of "pma" and assign it to *space.
8391 * If *space has already been set (through a previous call to this function),
8392 * then check that the range space is the same.
8394 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8396 isl_space **space = user;
8397 isl_space *pma_space;
8398 isl_bool equal;
8400 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8401 isl_pw_multi_aff_free(pma);
8403 if (!pma_space)
8404 return isl_stat_error;
8405 if (!*space) {
8406 *space = pma_space;
8407 return isl_stat_ok;
8410 equal = isl_space_is_equal(pma_space, *space);
8411 isl_space_free(pma_space);
8413 if (equal < 0)
8414 return isl_stat_error;
8415 if (!equal)
8416 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8417 "range spaces not the same", return isl_stat_error);
8418 return isl_stat_ok;
8421 /* Construct and return a multi union piecewise affine expression
8422 * that is equal to the given union piecewise multi affine expression.
8424 * In order to be able to perform the conversion, the input
8425 * needs to be non-empty and may only involve a single range space.
8427 * If the resulting multi union piecewise affine expression has
8428 * an explicit domain, then assign it the domain of the input.
8429 * In other cases, the domain is stored in the individual elements.
8431 __isl_give isl_multi_union_pw_aff *
8432 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8433 __isl_take isl_union_pw_multi_aff *upma)
8435 isl_space *space = NULL;
8436 isl_multi_union_pw_aff *mupa;
8437 int i, n;
8439 if (!upma)
8440 return NULL;
8441 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8442 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8443 "cannot extract range space from empty input",
8444 goto error);
8445 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8446 &space) < 0)
8447 goto error;
8449 if (!space)
8450 goto error;
8452 n = isl_space_dim(space, isl_dim_set);
8453 mupa = isl_multi_union_pw_aff_alloc(space);
8455 for (i = 0; i < n; ++i) {
8456 isl_union_pw_aff *upa;
8458 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8459 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8461 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8462 isl_union_set *dom;
8463 isl_union_pw_multi_aff *copy;
8465 copy = isl_union_pw_multi_aff_copy(upma);
8466 dom = isl_union_pw_multi_aff_domain(copy);
8467 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8470 isl_union_pw_multi_aff_free(upma);
8471 return mupa;
8472 error:
8473 isl_space_free(space);
8474 isl_union_pw_multi_aff_free(upma);
8475 return NULL;
8478 /* Try and create an isl_multi_union_pw_aff that is equivalent
8479 * to the given isl_union_map.
8480 * The isl_union_map is required to be single-valued in each space.
8481 * Moreover, it cannot be empty and all range spaces need to be the same.
8482 * Otherwise, an error is produced.
8484 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8485 __isl_take isl_union_map *umap)
8487 isl_union_pw_multi_aff *upma;
8489 upma = isl_union_pw_multi_aff_from_union_map(umap);
8490 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8493 /* Return a multiple union piecewise affine expression
8494 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8495 * have been aligned.
8497 * If the resulting multi union piecewise affine expression has
8498 * an explicit domain, then assign it the input domain.
8499 * In other cases, the domain is stored in the individual elements.
8501 static __isl_give isl_multi_union_pw_aff *
8502 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8503 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8505 int i, n;
8506 isl_space *space;
8507 isl_multi_union_pw_aff *mupa;
8509 if (!domain || !mv)
8510 goto error;
8512 n = isl_multi_val_dim(mv, isl_dim_set);
8513 space = isl_multi_val_get_space(mv);
8514 mupa = isl_multi_union_pw_aff_alloc(space);
8515 for (i = 0; i < n; ++i) {
8516 isl_val *v;
8517 isl_union_pw_aff *upa;
8519 v = isl_multi_val_get_val(mv, i);
8520 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8522 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8524 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8525 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8526 isl_union_set_copy(domain));
8528 isl_union_set_free(domain);
8529 isl_multi_val_free(mv);
8530 return mupa;
8531 error:
8532 isl_union_set_free(domain);
8533 isl_multi_val_free(mv);
8534 return NULL;
8537 /* Return a multiple union piecewise affine expression
8538 * that is equal to "mv" on "domain".
8540 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8541 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8543 isl_bool equal_params;
8545 if (!domain || !mv)
8546 goto error;
8547 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8548 if (equal_params < 0)
8549 goto error;
8550 if (equal_params)
8551 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8552 domain, mv);
8553 domain = isl_union_set_align_params(domain,
8554 isl_multi_val_get_space(mv));
8555 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8556 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8557 error:
8558 isl_union_set_free(domain);
8559 isl_multi_val_free(mv);
8560 return NULL;
8563 /* Return a multiple union piecewise affine expression
8564 * that is equal to "ma" on "domain".
8566 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8567 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8569 isl_pw_multi_aff *pma;
8571 pma = isl_pw_multi_aff_from_multi_aff(ma);
8572 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8575 /* Return a multiple union piecewise affine expression
8576 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8577 * have been aligned.
8579 * If the resulting multi union piecewise affine expression has
8580 * an explicit domain, then assign it the input domain.
8581 * In other cases, the domain is stored in the individual elements.
8583 static __isl_give isl_multi_union_pw_aff *
8584 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8585 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8587 int i, n;
8588 isl_space *space;
8589 isl_multi_union_pw_aff *mupa;
8591 if (!domain || !pma)
8592 goto error;
8594 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8595 space = isl_pw_multi_aff_get_space(pma);
8596 mupa = isl_multi_union_pw_aff_alloc(space);
8597 for (i = 0; i < n; ++i) {
8598 isl_pw_aff *pa;
8599 isl_union_pw_aff *upa;
8601 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8602 upa = isl_union_pw_aff_pw_aff_on_domain(
8603 isl_union_set_copy(domain), pa);
8604 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8606 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8607 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8608 isl_union_set_copy(domain));
8610 isl_union_set_free(domain);
8611 isl_pw_multi_aff_free(pma);
8612 return mupa;
8613 error:
8614 isl_union_set_free(domain);
8615 isl_pw_multi_aff_free(pma);
8616 return NULL;
8619 /* Return a multiple union piecewise affine expression
8620 * that is equal to "pma" on "domain".
8622 __isl_give isl_multi_union_pw_aff *
8623 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8624 __isl_take isl_pw_multi_aff *pma)
8626 isl_bool equal_params;
8627 isl_space *space;
8629 space = isl_pw_multi_aff_peek_space(pma);
8630 equal_params = isl_union_set_space_has_equal_params(domain, space);
8631 if (equal_params < 0)
8632 goto error;
8633 if (equal_params)
8634 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8635 domain, pma);
8636 domain = isl_union_set_align_params(domain,
8637 isl_pw_multi_aff_get_space(pma));
8638 pma = isl_pw_multi_aff_align_params(pma,
8639 isl_union_set_get_space(domain));
8640 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8641 pma);
8642 error:
8643 isl_union_set_free(domain);
8644 isl_pw_multi_aff_free(pma);
8645 return NULL;
8648 /* Return a union set containing those elements in the domains
8649 * of the elements of "mupa" where they are all zero.
8651 * If there are no elements, then simply return the entire domain.
8653 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8654 __isl_take isl_multi_union_pw_aff *mupa)
8656 int i, n;
8657 isl_union_pw_aff *upa;
8658 isl_union_set *zero;
8660 if (!mupa)
8661 return NULL;
8663 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8664 if (n == 0)
8665 return isl_multi_union_pw_aff_domain(mupa);
8667 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8668 zero = isl_union_pw_aff_zero_union_set(upa);
8670 for (i = 1; i < n; ++i) {
8671 isl_union_set *zero_i;
8673 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8674 zero_i = isl_union_pw_aff_zero_union_set(upa);
8676 zero = isl_union_set_intersect(zero, zero_i);
8679 isl_multi_union_pw_aff_free(mupa);
8680 return zero;
8683 /* Construct a union map mapping the shared domain
8684 * of the union piecewise affine expressions to the range of "mupa"
8685 * in the special case of a 0D multi union piecewise affine expression.
8687 * Construct a map between the explicit domain of "mupa" and
8688 * the range space.
8689 * Note that this assumes that the domain consists of explicit elements.
8691 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8692 __isl_take isl_multi_union_pw_aff *mupa)
8694 isl_bool is_params;
8695 isl_space *space;
8696 isl_union_set *dom, *ran;
8698 space = isl_multi_union_pw_aff_get_space(mupa);
8699 dom = isl_multi_union_pw_aff_domain(mupa);
8700 ran = isl_union_set_from_set(isl_set_universe(space));
8702 is_params = isl_union_set_is_params(dom);
8703 if (is_params < 0)
8704 dom = isl_union_set_free(dom);
8705 else if (is_params)
8706 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8707 "cannot create union map from expression without "
8708 "explicit domain elements",
8709 dom = isl_union_set_free(dom));
8711 return isl_union_map_from_domain_and_range(dom, ran);
8714 /* Construct a union map mapping the shared domain
8715 * of the union piecewise affine expressions to the range of "mupa"
8716 * with each dimension in the range equated to the
8717 * corresponding union piecewise affine expression.
8719 * If the input is zero-dimensional, then construct a mapping
8720 * from its explicit domain.
8722 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8723 __isl_take isl_multi_union_pw_aff *mupa)
8725 int i, n;
8726 isl_space *space;
8727 isl_union_map *umap;
8728 isl_union_pw_aff *upa;
8730 if (!mupa)
8731 return NULL;
8733 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8734 if (n == 0)
8735 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8737 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8738 umap = isl_union_map_from_union_pw_aff(upa);
8740 for (i = 1; i < n; ++i) {
8741 isl_union_map *umap_i;
8743 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8744 umap_i = isl_union_map_from_union_pw_aff(upa);
8745 umap = isl_union_map_flat_range_product(umap, umap_i);
8748 space = isl_multi_union_pw_aff_get_space(mupa);
8749 umap = isl_union_map_reset_range_space(umap, space);
8751 isl_multi_union_pw_aff_free(mupa);
8752 return umap;
8755 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8756 * "range" is the space from which to set the range space.
8757 * "res" collects the results.
8759 struct isl_union_pw_multi_aff_reset_range_space_data {
8760 isl_space *range;
8761 isl_union_pw_multi_aff *res;
8764 /* Replace the range space of "pma" by the range space of data->range and
8765 * add the result to data->res.
8767 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8769 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8770 isl_space *space;
8772 space = isl_pw_multi_aff_get_space(pma);
8773 space = isl_space_domain(space);
8774 space = isl_space_extend_domain_with_range(space,
8775 isl_space_copy(data->range));
8776 pma = isl_pw_multi_aff_reset_space(pma, space);
8777 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8779 return data->res ? isl_stat_ok : isl_stat_error;
8782 /* Replace the range space of all the piecewise affine expressions in "upma" by
8783 * the range space of "space".
8785 * This assumes that all these expressions have the same output dimension.
8787 * Since the spaces of the expressions change, so do their hash values.
8788 * We therefore need to create a new isl_union_pw_multi_aff.
8789 * Note that the hash value is currently computed based on the entire
8790 * space even though there can only be a single expression with a given
8791 * domain space.
8793 static __isl_give isl_union_pw_multi_aff *
8794 isl_union_pw_multi_aff_reset_range_space(
8795 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8797 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8798 isl_space *space_upma;
8800 space_upma = isl_union_pw_multi_aff_get_space(upma);
8801 data.res = isl_union_pw_multi_aff_empty(space_upma);
8802 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8803 &reset_range_space, &data) < 0)
8804 data.res = isl_union_pw_multi_aff_free(data.res);
8806 isl_space_free(space);
8807 isl_union_pw_multi_aff_free(upma);
8808 return data.res;
8811 /* Construct and return a union piecewise multi affine expression
8812 * that is equal to the given multi union piecewise affine expression,
8813 * in the special case of a 0D multi union piecewise affine expression.
8815 * Construct a union piecewise multi affine expression
8816 * on top of the explicit domain of the input.
8818 __isl_give isl_union_pw_multi_aff *
8819 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8820 __isl_take isl_multi_union_pw_aff *mupa)
8822 isl_space *space;
8823 isl_multi_val *mv;
8824 isl_union_set *domain;
8826 space = isl_multi_union_pw_aff_get_space(mupa);
8827 mv = isl_multi_val_zero(space);
8828 domain = isl_multi_union_pw_aff_domain(mupa);
8829 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8832 /* Construct and return a union piecewise multi affine expression
8833 * that is equal to the given multi union piecewise affine expression.
8835 * If the input is zero-dimensional, then
8836 * construct a union piecewise multi affine expression
8837 * on top of the explicit domain of the input.
8839 __isl_give isl_union_pw_multi_aff *
8840 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8841 __isl_take isl_multi_union_pw_aff *mupa)
8843 int i, n;
8844 isl_space *space;
8845 isl_union_pw_multi_aff *upma;
8846 isl_union_pw_aff *upa;
8848 if (!mupa)
8849 return NULL;
8851 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8852 if (n == 0)
8853 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8855 space = isl_multi_union_pw_aff_get_space(mupa);
8856 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8857 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8859 for (i = 1; i < n; ++i) {
8860 isl_union_pw_multi_aff *upma_i;
8862 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8863 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8864 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8867 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8869 isl_multi_union_pw_aff_free(mupa);
8870 return upma;
8873 /* Intersect the range of "mupa" with "range",
8874 * in the special case where "mupa" is 0D.
8876 * Intersect the domain of "mupa" with the constraints on the parameters
8877 * of "range".
8879 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8880 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8882 range = isl_set_params(range);
8883 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8884 return mupa;
8887 /* Intersect the range of "mupa" with "range".
8888 * That is, keep only those domain elements that have a function value
8889 * in "range".
8891 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8892 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8894 isl_union_pw_multi_aff *upma;
8895 isl_union_set *domain;
8896 isl_space *space;
8897 int n;
8898 int match;
8900 if (!mupa || !range)
8901 goto error;
8903 space = isl_set_get_space(range);
8904 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8905 space, isl_dim_set);
8906 isl_space_free(space);
8907 if (match < 0)
8908 goto error;
8909 if (!match)
8910 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8911 "space don't match", goto error);
8912 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8913 if (n == 0)
8914 return mupa_intersect_range_0D(mupa, range);
8916 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8917 isl_multi_union_pw_aff_copy(mupa));
8918 domain = isl_union_set_from_set(range);
8919 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8920 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8922 return mupa;
8923 error:
8924 isl_multi_union_pw_aff_free(mupa);
8925 isl_set_free(range);
8926 return NULL;
8929 /* Return the shared domain of the elements of "mupa",
8930 * in the special case where "mupa" is zero-dimensional.
8932 * Return the explicit domain of "mupa".
8933 * Note that this domain may be a parameter set, either
8934 * because "mupa" is meant to live in a set space or
8935 * because no explicit domain has been set.
8937 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8938 __isl_take isl_multi_union_pw_aff *mupa)
8940 isl_union_set *dom;
8942 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8943 isl_multi_union_pw_aff_free(mupa);
8945 return dom;
8948 /* Return the shared domain of the elements of "mupa".
8950 * If "mupa" is zero-dimensional, then return its explicit domain.
8952 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8953 __isl_take isl_multi_union_pw_aff *mupa)
8955 int i, n;
8956 isl_union_pw_aff *upa;
8957 isl_union_set *dom;
8959 if (!mupa)
8960 return NULL;
8962 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8963 if (n == 0)
8964 return isl_multi_union_pw_aff_domain_0D(mupa);
8966 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8967 dom = isl_union_pw_aff_domain(upa);
8968 for (i = 1; i < n; ++i) {
8969 isl_union_set *dom_i;
8971 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8972 dom_i = isl_union_pw_aff_domain(upa);
8973 dom = isl_union_set_intersect(dom, dom_i);
8976 isl_multi_union_pw_aff_free(mupa);
8977 return dom;
8980 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8981 * In particular, the spaces have been aligned.
8982 * The result is defined over the shared domain of the elements of "mupa"
8984 * We first extract the parametric constant part of "aff" and
8985 * define that over the shared domain.
8986 * Then we iterate over all input dimensions of "aff" and add the corresponding
8987 * multiples of the elements of "mupa".
8988 * Finally, we consider the integer divisions, calling the function
8989 * recursively to obtain an isl_union_pw_aff corresponding to the
8990 * integer division argument.
8992 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8993 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8995 int i, n_in, n_div;
8996 isl_union_pw_aff *upa;
8997 isl_union_set *uset;
8998 isl_val *v;
8999 isl_aff *cst;
9001 n_in = isl_aff_dim(aff, isl_dim_in);
9002 n_div = isl_aff_dim(aff, isl_dim_div);
9004 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9005 cst = isl_aff_copy(aff);
9006 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9007 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9008 cst = isl_aff_project_domain_on_params(cst);
9009 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9011 for (i = 0; i < n_in; ++i) {
9012 isl_union_pw_aff *upa_i;
9014 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9015 continue;
9016 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9017 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9018 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9019 upa = isl_union_pw_aff_add(upa, upa_i);
9022 for (i = 0; i < n_div; ++i) {
9023 isl_aff *div;
9024 isl_union_pw_aff *upa_i;
9026 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9027 continue;
9028 div = isl_aff_get_div(aff, i);
9029 upa_i = multi_union_pw_aff_apply_aff(
9030 isl_multi_union_pw_aff_copy(mupa), div);
9031 upa_i = isl_union_pw_aff_floor(upa_i);
9032 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9033 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9034 upa = isl_union_pw_aff_add(upa, upa_i);
9037 isl_multi_union_pw_aff_free(mupa);
9038 isl_aff_free(aff);
9040 return upa;
9043 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9044 * with the domain of "aff".
9045 * Furthermore, the dimension of this space needs to be greater than zero.
9046 * The result is defined over the shared domain of the elements of "mupa"
9048 * We perform these checks and then hand over control to
9049 * multi_union_pw_aff_apply_aff.
9051 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9052 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9054 isl_space *space1, *space2;
9055 int equal;
9057 mupa = isl_multi_union_pw_aff_align_params(mupa,
9058 isl_aff_get_space(aff));
9059 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9060 if (!mupa || !aff)
9061 goto error;
9063 space1 = isl_multi_union_pw_aff_get_space(mupa);
9064 space2 = isl_aff_get_domain_space(aff);
9065 equal = isl_space_is_equal(space1, space2);
9066 isl_space_free(space1);
9067 isl_space_free(space2);
9068 if (equal < 0)
9069 goto error;
9070 if (!equal)
9071 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9072 "spaces don't match", goto error);
9073 if (isl_aff_dim(aff, isl_dim_in) == 0)
9074 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9075 "cannot determine domains", goto error);
9077 return multi_union_pw_aff_apply_aff(mupa, aff);
9078 error:
9079 isl_multi_union_pw_aff_free(mupa);
9080 isl_aff_free(aff);
9081 return NULL;
9084 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9085 * The space of "mupa" is known to be compatible with the domain of "ma".
9087 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9088 * on the domain of "mupa".
9090 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9091 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9093 isl_union_set *dom;
9095 dom = isl_multi_union_pw_aff_domain(mupa);
9096 ma = isl_multi_aff_project_domain_on_params(ma);
9098 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9101 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9102 * with the domain of "ma".
9103 * The result is defined over the shared domain of the elements of "mupa"
9105 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9106 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9108 isl_space *space1, *space2;
9109 isl_multi_union_pw_aff *res;
9110 int equal;
9111 int i, n_out;
9113 mupa = isl_multi_union_pw_aff_align_params(mupa,
9114 isl_multi_aff_get_space(ma));
9115 ma = isl_multi_aff_align_params(ma,
9116 isl_multi_union_pw_aff_get_space(mupa));
9117 if (!mupa || !ma)
9118 goto error;
9120 space1 = isl_multi_union_pw_aff_get_space(mupa);
9121 space2 = isl_multi_aff_get_domain_space(ma);
9122 equal = isl_space_is_equal(space1, space2);
9123 isl_space_free(space1);
9124 isl_space_free(space2);
9125 if (equal < 0)
9126 goto error;
9127 if (!equal)
9128 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9129 "spaces don't match", goto error);
9130 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9131 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9132 return mupa_apply_multi_aff_0D(mupa, ma);
9134 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9135 res = isl_multi_union_pw_aff_alloc(space1);
9137 for (i = 0; i < n_out; ++i) {
9138 isl_aff *aff;
9139 isl_union_pw_aff *upa;
9141 aff = isl_multi_aff_get_aff(ma, i);
9142 upa = multi_union_pw_aff_apply_aff(
9143 isl_multi_union_pw_aff_copy(mupa), aff);
9144 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9147 isl_multi_aff_free(ma);
9148 isl_multi_union_pw_aff_free(mupa);
9149 return res;
9150 error:
9151 isl_multi_union_pw_aff_free(mupa);
9152 isl_multi_aff_free(ma);
9153 return NULL;
9156 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9157 * The space of "mupa" is known to be compatible with the domain of "pa".
9159 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9160 * on the domain of "mupa".
9162 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9163 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9165 isl_union_set *dom;
9167 dom = isl_multi_union_pw_aff_domain(mupa);
9168 pa = isl_pw_aff_project_domain_on_params(pa);
9170 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9173 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9174 * with the domain of "pa".
9175 * Furthermore, the dimension of this space needs to be greater than zero.
9176 * The result is defined over the shared domain of the elements of "mupa"
9178 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9179 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9181 int i;
9182 int equal;
9183 isl_space *space, *space2;
9184 isl_union_pw_aff *upa;
9186 mupa = isl_multi_union_pw_aff_align_params(mupa,
9187 isl_pw_aff_get_space(pa));
9188 pa = isl_pw_aff_align_params(pa,
9189 isl_multi_union_pw_aff_get_space(mupa));
9190 if (!mupa || !pa)
9191 goto error;
9193 space = isl_multi_union_pw_aff_get_space(mupa);
9194 space2 = isl_pw_aff_get_domain_space(pa);
9195 equal = isl_space_is_equal(space, space2);
9196 isl_space_free(space);
9197 isl_space_free(space2);
9198 if (equal < 0)
9199 goto error;
9200 if (!equal)
9201 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9202 "spaces don't match", goto error);
9203 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9204 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9206 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9207 upa = isl_union_pw_aff_empty(space);
9209 for (i = 0; i < pa->n; ++i) {
9210 isl_aff *aff;
9211 isl_set *domain;
9212 isl_multi_union_pw_aff *mupa_i;
9213 isl_union_pw_aff *upa_i;
9215 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9216 domain = isl_set_copy(pa->p[i].set);
9217 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9218 aff = isl_aff_copy(pa->p[i].aff);
9219 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9220 upa = isl_union_pw_aff_union_add(upa, upa_i);
9223 isl_multi_union_pw_aff_free(mupa);
9224 isl_pw_aff_free(pa);
9225 return upa;
9226 error:
9227 isl_multi_union_pw_aff_free(mupa);
9228 isl_pw_aff_free(pa);
9229 return NULL;
9232 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9233 * The space of "mupa" is known to be compatible with the domain of "pma".
9235 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9236 * on the domain of "mupa".
9238 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9239 __isl_take isl_multi_union_pw_aff *mupa,
9240 __isl_take isl_pw_multi_aff *pma)
9242 isl_union_set *dom;
9244 dom = isl_multi_union_pw_aff_domain(mupa);
9245 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9247 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9250 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9251 * with the domain of "pma".
9252 * The result is defined over the shared domain of the elements of "mupa"
9254 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9255 __isl_take isl_multi_union_pw_aff *mupa,
9256 __isl_take isl_pw_multi_aff *pma)
9258 isl_space *space1, *space2;
9259 isl_multi_union_pw_aff *res;
9260 int equal;
9261 int i, n_out;
9263 mupa = isl_multi_union_pw_aff_align_params(mupa,
9264 isl_pw_multi_aff_get_space(pma));
9265 pma = isl_pw_multi_aff_align_params(pma,
9266 isl_multi_union_pw_aff_get_space(mupa));
9267 if (!mupa || !pma)
9268 goto error;
9270 space1 = isl_multi_union_pw_aff_get_space(mupa);
9271 space2 = isl_pw_multi_aff_get_domain_space(pma);
9272 equal = isl_space_is_equal(space1, space2);
9273 isl_space_free(space1);
9274 isl_space_free(space2);
9275 if (equal < 0)
9276 goto error;
9277 if (!equal)
9278 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9279 "spaces don't match", goto error);
9280 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9281 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9282 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9284 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9285 res = isl_multi_union_pw_aff_alloc(space1);
9287 for (i = 0; i < n_out; ++i) {
9288 isl_pw_aff *pa;
9289 isl_union_pw_aff *upa;
9291 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9292 upa = isl_multi_union_pw_aff_apply_pw_aff(
9293 isl_multi_union_pw_aff_copy(mupa), pa);
9294 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9297 isl_pw_multi_aff_free(pma);
9298 isl_multi_union_pw_aff_free(mupa);
9299 return res;
9300 error:
9301 isl_multi_union_pw_aff_free(mupa);
9302 isl_pw_multi_aff_free(pma);
9303 return NULL;
9306 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9307 * If the explicit domain only keeps track of constraints on the parameters,
9308 * then only update those constraints.
9310 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9311 __isl_take isl_multi_union_pw_aff *mupa,
9312 __isl_keep isl_union_pw_multi_aff *upma)
9314 isl_bool is_params;
9316 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9317 return isl_multi_union_pw_aff_free(mupa);
9319 mupa = isl_multi_union_pw_aff_cow(mupa);
9320 if (!mupa)
9321 return NULL;
9323 is_params = isl_union_set_is_params(mupa->u.dom);
9324 if (is_params < 0)
9325 return isl_multi_union_pw_aff_free(mupa);
9327 upma = isl_union_pw_multi_aff_copy(upma);
9328 if (is_params)
9329 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9330 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9331 else
9332 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9333 mupa->u.dom, upma);
9334 if (!mupa->u.dom)
9335 return isl_multi_union_pw_aff_free(mupa);
9336 return mupa;
9339 /* Compute the pullback of "mupa" by the function represented by "upma".
9340 * In other words, plug in "upma" in "mupa". The result contains
9341 * expressions defined over the domain space of "upma".
9343 * Run over all elements of "mupa" and plug in "upma" in each of them.
9345 * If "mupa" has an explicit domain, then it is this domain
9346 * that needs to undergo a pullback instead, i.e., a preimage.
9348 __isl_give isl_multi_union_pw_aff *
9349 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9350 __isl_take isl_multi_union_pw_aff *mupa,
9351 __isl_take isl_union_pw_multi_aff *upma)
9353 int i, n;
9355 mupa = isl_multi_union_pw_aff_align_params(mupa,
9356 isl_union_pw_multi_aff_get_space(upma));
9357 upma = isl_union_pw_multi_aff_align_params(upma,
9358 isl_multi_union_pw_aff_get_space(mupa));
9359 mupa = isl_multi_union_pw_aff_cow(mupa);
9360 if (!mupa || !upma)
9361 goto error;
9363 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9364 for (i = 0; i < n; ++i) {
9365 isl_union_pw_aff *upa;
9367 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9368 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9369 isl_union_pw_multi_aff_copy(upma));
9370 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9373 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9374 mupa = preimage_explicit_domain(mupa, upma);
9376 isl_union_pw_multi_aff_free(upma);
9377 return mupa;
9378 error:
9379 isl_multi_union_pw_aff_free(mupa);
9380 isl_union_pw_multi_aff_free(upma);
9381 return NULL;
9384 /* Extract the sequence of elements in "mupa" with domain space "space"
9385 * (ignoring parameters).
9387 * For the elements of "mupa" that are not defined on the specified space,
9388 * the corresponding element in the result is empty.
9390 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9391 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9393 int i, n;
9394 isl_space *space_mpa;
9395 isl_multi_pw_aff *mpa;
9397 if (!mupa || !space)
9398 goto error;
9400 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9401 space = isl_space_replace_params(space, space_mpa);
9402 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9403 space_mpa);
9404 mpa = isl_multi_pw_aff_alloc(space_mpa);
9406 space = isl_space_from_domain(space);
9407 space = isl_space_add_dims(space, isl_dim_out, 1);
9408 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9409 for (i = 0; i < n; ++i) {
9410 isl_union_pw_aff *upa;
9411 isl_pw_aff *pa;
9413 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9414 pa = isl_union_pw_aff_extract_pw_aff(upa,
9415 isl_space_copy(space));
9416 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9417 isl_union_pw_aff_free(upa);
9420 isl_space_free(space);
9421 return mpa;
9422 error:
9423 isl_space_free(space);
9424 return NULL;
9427 /* Evaluate the affine function "aff" in the void point "pnt".
9428 * In particular, return the value NaN.
9430 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9431 __isl_take isl_point *pnt)
9433 isl_ctx *ctx;
9435 ctx = isl_point_get_ctx(pnt);
9436 isl_aff_free(aff);
9437 isl_point_free(pnt);
9438 return isl_val_nan(ctx);
9441 /* Evaluate the affine expression "aff"
9442 * in the coordinates (with denominator) "pnt".
9444 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9445 __isl_keep isl_vec *pnt)
9447 isl_int n, d;
9448 isl_ctx *ctx;
9449 isl_val *v;
9451 if (!aff || !pnt)
9452 return NULL;
9454 ctx = isl_vec_get_ctx(aff);
9455 isl_int_init(n);
9456 isl_int_init(d);
9457 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9458 isl_int_mul(d, aff->el[0], pnt->el[0]);
9459 v = isl_val_rat_from_isl_int(ctx, n, d);
9460 v = isl_val_normalize(v);
9461 isl_int_clear(n);
9462 isl_int_clear(d);
9464 return v;
9467 /* Check that the domain space of "aff" is equal to "space".
9469 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9470 __isl_keep isl_space *space)
9472 isl_bool ok;
9474 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9475 if (ok < 0)
9476 return isl_stat_error;
9477 if (!ok)
9478 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9479 "incompatible spaces", return isl_stat_error);
9480 return isl_stat_ok;
9483 /* Evaluate the affine function "aff" in "pnt".
9485 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9486 __isl_take isl_point *pnt)
9488 isl_bool is_void;
9489 isl_val *v;
9490 isl_local_space *ls;
9492 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9493 goto error;
9494 is_void = isl_point_is_void(pnt);
9495 if (is_void < 0)
9496 goto error;
9497 if (is_void)
9498 return eval_void(aff, pnt);
9500 ls = isl_aff_get_domain_local_space(aff);
9501 pnt = isl_local_space_lift_point(ls, pnt);
9503 v = eval(aff->v, isl_point_peek_vec(pnt));
9505 isl_aff_free(aff);
9506 isl_point_free(pnt);
9508 return v;
9509 error:
9510 isl_aff_free(aff);
9511 isl_point_free(pnt);
9512 return NULL;