add missing annotations to isl_set_preimage
[isl.git] / isl_aff.c
blob61ef61eb3658071d627a27546ffd88a86a410916
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 #define ISL_DIM_H
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.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/deprecated/aff_int.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 union_pw_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_multi_aff
51 #include <isl_list_templ.c>
53 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
54 __isl_take isl_vec *v)
56 isl_aff *aff;
58 if (!ls || !v)
59 goto error;
61 aff = isl_calloc_type(v->ctx, struct isl_aff);
62 if (!aff)
63 goto error;
65 aff->ref = 1;
66 aff->ls = ls;
67 aff->v = v;
69 return aff;
70 error:
71 isl_local_space_free(ls);
72 isl_vec_free(v);
73 return NULL;
76 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
78 isl_ctx *ctx;
79 isl_vec *v;
80 unsigned total;
82 if (!ls)
83 return NULL;
85 ctx = isl_local_space_get_ctx(ls);
86 if (!isl_local_space_divs_known(ls))
87 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
88 goto error);
89 if (!isl_local_space_is_set(ls))
90 isl_die(ctx, isl_error_invalid,
91 "domain of affine expression should be a set",
92 goto error);
94 total = isl_local_space_dim(ls, isl_dim_all);
95 v = isl_vec_alloc(ctx, 1 + 1 + total);
96 return isl_aff_alloc_vec(ls, v);
97 error:
98 isl_local_space_free(ls);
99 return NULL;
102 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
104 isl_aff *aff;
106 aff = isl_aff_alloc(ls);
107 if (!aff)
108 return NULL;
110 isl_int_set_si(aff->v->el[0], 1);
111 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
113 return aff;
116 /* Return a piecewise affine expression defined on the specified domain
117 * that is equal to zero.
119 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
121 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
124 /* Return an affine expression defined on the specified domain
125 * that represents NaN.
127 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
129 isl_aff *aff;
131 aff = isl_aff_alloc(ls);
132 if (!aff)
133 return NULL;
135 isl_seq_clr(aff->v->el, aff->v->size);
137 return aff;
140 /* Return a piecewise affine expression defined on the specified domain
141 * that represents NaN.
143 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
145 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
148 /* Return an affine expression that is equal to "val" on
149 * domain local space "ls".
151 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
152 __isl_take isl_val *val)
154 isl_aff *aff;
156 if (!ls || !val)
157 goto error;
158 if (!isl_val_is_rat(val))
159 isl_die(isl_val_get_ctx(val), isl_error_invalid,
160 "expecting rational value", goto error);
162 aff = isl_aff_alloc(isl_local_space_copy(ls));
163 if (!aff)
164 goto error;
166 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
167 isl_int_set(aff->v->el[1], val->n);
168 isl_int_set(aff->v->el[0], val->d);
170 isl_local_space_free(ls);
171 isl_val_free(val);
172 return aff;
173 error:
174 isl_local_space_free(ls);
175 isl_val_free(val);
176 return NULL;
179 /* Return an affine expression that is equal to the specified dimension
180 * in "ls".
182 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
183 enum isl_dim_type type, unsigned pos)
185 isl_space *space;
186 isl_aff *aff;
188 if (!ls)
189 return NULL;
191 space = isl_local_space_get_space(ls);
192 if (!space)
193 goto error;
194 if (isl_space_is_map(space))
195 isl_die(isl_space_get_ctx(space), isl_error_invalid,
196 "expecting (parameter) set space", goto error);
197 if (pos >= isl_local_space_dim(ls, type))
198 isl_die(isl_space_get_ctx(space), isl_error_invalid,
199 "position out of bounds", goto error);
201 isl_space_free(space);
202 aff = isl_aff_alloc(ls);
203 if (!aff)
204 return NULL;
206 pos += isl_local_space_offset(aff->ls, type);
208 isl_int_set_si(aff->v->el[0], 1);
209 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
210 isl_int_set_si(aff->v->el[1 + pos], 1);
212 return aff;
213 error:
214 isl_local_space_free(ls);
215 isl_space_free(space);
216 return NULL;
219 /* Return a piecewise affine expression that is equal to
220 * the specified dimension in "ls".
222 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
223 enum isl_dim_type type, unsigned pos)
225 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
228 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
230 if (!aff)
231 return NULL;
233 aff->ref++;
234 return aff;
237 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
239 if (!aff)
240 return NULL;
242 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
243 isl_vec_copy(aff->v));
246 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
248 if (!aff)
249 return NULL;
251 if (aff->ref == 1)
252 return aff;
253 aff->ref--;
254 return isl_aff_dup(aff);
257 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
259 if (!aff)
260 return NULL;
262 if (--aff->ref > 0)
263 return NULL;
265 isl_local_space_free(aff->ls);
266 isl_vec_free(aff->v);
268 free(aff);
270 return NULL;
273 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
275 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
278 /* Return a hash value that digests "aff".
280 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
282 uint32_t hash, ls_hash, v_hash;
284 if (!aff)
285 return 0;
287 hash = isl_hash_init();
288 ls_hash = isl_local_space_get_hash(aff->ls);
289 isl_hash_hash(hash, ls_hash);
290 v_hash = isl_vec_get_hash(aff->v);
291 isl_hash_hash(hash, v_hash);
293 return hash;
296 /* Externally, an isl_aff has a map space, but internally, the
297 * ls field corresponds to the domain of that space.
299 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
301 if (!aff)
302 return 0;
303 if (type == isl_dim_out)
304 return 1;
305 if (type == isl_dim_in)
306 type = isl_dim_set;
307 return isl_local_space_dim(aff->ls, type);
310 /* Return the position of the dimension of the given type and name
311 * in "aff".
312 * Return -1 if no such dimension can be found.
314 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
315 const char *name)
317 if (!aff)
318 return -1;
319 if (type == isl_dim_out)
320 return -1;
321 if (type == isl_dim_in)
322 type = isl_dim_set;
323 return isl_local_space_find_dim_by_name(aff->ls, type, name);
326 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
328 return aff ? isl_local_space_get_space(aff->ls) : NULL;
331 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
333 isl_space *space;
334 if (!aff)
335 return NULL;
336 space = isl_local_space_get_space(aff->ls);
337 space = isl_space_from_domain(space);
338 space = isl_space_add_dims(space, isl_dim_out, 1);
339 return space;
342 __isl_give isl_local_space *isl_aff_get_domain_local_space(
343 __isl_keep isl_aff *aff)
345 return aff ? isl_local_space_copy(aff->ls) : NULL;
348 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
350 isl_local_space *ls;
351 if (!aff)
352 return NULL;
353 ls = isl_local_space_copy(aff->ls);
354 ls = isl_local_space_from_domain(ls);
355 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
356 return ls;
359 /* Externally, an isl_aff has a map space, but internally, the
360 * ls field corresponds to the domain of that space.
362 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
363 enum isl_dim_type type, unsigned pos)
365 if (!aff)
366 return NULL;
367 if (type == isl_dim_out)
368 return NULL;
369 if (type == isl_dim_in)
370 type = isl_dim_set;
371 return isl_local_space_get_dim_name(aff->ls, type, pos);
374 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
375 __isl_take isl_space *dim)
377 aff = isl_aff_cow(aff);
378 if (!aff || !dim)
379 goto error;
381 aff->ls = isl_local_space_reset_space(aff->ls, dim);
382 if (!aff->ls)
383 return isl_aff_free(aff);
385 return aff;
386 error:
387 isl_aff_free(aff);
388 isl_space_free(dim);
389 return NULL;
392 /* Reset the space of "aff". This function is called from isl_pw_templ.c
393 * and doesn't know if the space of an element object is represented
394 * directly or through its domain. It therefore passes along both.
396 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
397 __isl_take isl_space *space, __isl_take isl_space *domain)
399 isl_space_free(space);
400 return isl_aff_reset_domain_space(aff, domain);
403 /* Reorder the coefficients of the affine expression based
404 * on the given reordering.
405 * The reordering r is assumed to have been extended with the local
406 * variables.
408 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
409 __isl_take isl_reordering *r, int n_div)
411 isl_vec *res;
412 int i;
414 if (!vec || !r)
415 goto error;
417 res = isl_vec_alloc(vec->ctx,
418 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
419 if (!res)
420 goto error;
421 isl_seq_cpy(res->el, vec->el, 2);
422 isl_seq_clr(res->el + 2, res->size - 2);
423 for (i = 0; i < r->len; ++i)
424 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
426 isl_reordering_free(r);
427 isl_vec_free(vec);
428 return res;
429 error:
430 isl_vec_free(vec);
431 isl_reordering_free(r);
432 return NULL;
435 /* Reorder the dimensions of the domain of "aff" according
436 * to the given reordering.
438 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
439 __isl_take isl_reordering *r)
441 aff = isl_aff_cow(aff);
442 if (!aff)
443 goto error;
445 r = isl_reordering_extend(r, aff->ls->div->n_row);
446 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
447 aff->ls->div->n_row);
448 aff->ls = isl_local_space_realign(aff->ls, r);
450 if (!aff->v || !aff->ls)
451 return isl_aff_free(aff);
453 return aff;
454 error:
455 isl_aff_free(aff);
456 isl_reordering_free(r);
457 return NULL;
460 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
461 __isl_take isl_space *model)
463 isl_bool equal_params;
465 if (!aff || !model)
466 goto error;
468 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
469 if (equal_params < 0)
470 goto error;
471 if (!equal_params) {
472 isl_reordering *exp;
474 model = isl_space_drop_dims(model, isl_dim_in,
475 0, isl_space_dim(model, isl_dim_in));
476 model = isl_space_drop_dims(model, isl_dim_out,
477 0, isl_space_dim(model, isl_dim_out));
478 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
479 exp = isl_reordering_extend_space(exp,
480 isl_aff_get_domain_space(aff));
481 aff = isl_aff_realign_domain(aff, exp);
484 isl_space_free(model);
485 return aff;
486 error:
487 isl_space_free(model);
488 isl_aff_free(aff);
489 return NULL;
492 /* Is "aff" obviously equal to zero?
494 * If the denominator is zero, then "aff" is not equal to zero.
496 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
498 if (!aff)
499 return isl_bool_error;
501 if (isl_int_is_zero(aff->v->el[0]))
502 return isl_bool_false;
503 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
506 /* Does "aff" represent NaN?
508 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
510 if (!aff)
511 return isl_bool_error;
513 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
516 /* Are "aff1" and "aff2" obviously equal?
518 * NaN is not equal to anything, not even to another NaN.
520 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
521 __isl_keep isl_aff *aff2)
523 isl_bool equal;
525 if (!aff1 || !aff2)
526 return isl_bool_error;
528 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
529 return isl_bool_false;
531 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
532 if (equal < 0 || !equal)
533 return equal;
535 return isl_vec_is_equal(aff1->v, aff2->v);
538 /* Return the common denominator of "aff" in "v".
540 * We cannot return anything meaningful in case of a NaN.
542 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
544 if (!aff)
545 return isl_stat_error;
546 if (isl_aff_is_nan(aff))
547 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
548 "cannot get denominator of NaN", return isl_stat_error);
549 isl_int_set(*v, aff->v->el[0]);
550 return isl_stat_ok;
553 /* Return the common denominator of "aff".
555 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
557 isl_ctx *ctx;
559 if (!aff)
560 return NULL;
562 ctx = isl_aff_get_ctx(aff);
563 if (isl_aff_is_nan(aff))
564 return isl_val_nan(ctx);
565 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
568 /* Return the constant term of "aff" in "v".
570 * We cannot return anything meaningful in case of a NaN.
572 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
574 if (!aff)
575 return -1;
576 if (isl_aff_is_nan(aff))
577 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
578 "cannot get constant term of NaN", return -1);
579 isl_int_set(*v, aff->v->el[1]);
580 return 0;
583 /* Return the constant term of "aff".
585 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
587 isl_ctx *ctx;
588 isl_val *v;
590 if (!aff)
591 return NULL;
593 ctx = isl_aff_get_ctx(aff);
594 if (isl_aff_is_nan(aff))
595 return isl_val_nan(ctx);
596 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
597 return isl_val_normalize(v);
600 /* Return the coefficient of the variable of type "type" at position "pos"
601 * of "aff" in "v".
603 * We cannot return anything meaningful in case of a NaN.
605 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
606 enum isl_dim_type type, int pos, isl_int *v)
608 if (!aff)
609 return -1;
611 if (type == isl_dim_out)
612 isl_die(aff->v->ctx, isl_error_invalid,
613 "output/set dimension does not have a coefficient",
614 return -1);
615 if (type == isl_dim_in)
616 type = isl_dim_set;
618 if (pos >= isl_local_space_dim(aff->ls, type))
619 isl_die(aff->v->ctx, isl_error_invalid,
620 "position out of bounds", return -1);
622 if (isl_aff_is_nan(aff))
623 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
624 "cannot get coefficient of NaN", return -1);
625 pos += isl_local_space_offset(aff->ls, type);
626 isl_int_set(*v, aff->v->el[1 + pos]);
628 return 0;
631 /* Return the coefficient of the variable of type "type" at position "pos"
632 * of "aff".
634 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
635 enum isl_dim_type type, int pos)
637 isl_ctx *ctx;
638 isl_val *v;
640 if (!aff)
641 return NULL;
643 ctx = isl_aff_get_ctx(aff);
644 if (type == isl_dim_out)
645 isl_die(ctx, isl_error_invalid,
646 "output/set dimension does not have a coefficient",
647 return NULL);
648 if (type == isl_dim_in)
649 type = isl_dim_set;
651 if (pos >= isl_local_space_dim(aff->ls, type))
652 isl_die(ctx, isl_error_invalid,
653 "position out of bounds", return NULL);
655 if (isl_aff_is_nan(aff))
656 return isl_val_nan(ctx);
657 pos += isl_local_space_offset(aff->ls, type);
658 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
659 return isl_val_normalize(v);
662 /* Return the sign of the coefficient of the variable of type "type"
663 * at position "pos" of "aff".
665 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
666 int pos)
668 isl_ctx *ctx;
670 if (!aff)
671 return 0;
673 ctx = isl_aff_get_ctx(aff);
674 if (type == isl_dim_out)
675 isl_die(ctx, isl_error_invalid,
676 "output/set dimension does not have a coefficient",
677 return 0);
678 if (type == isl_dim_in)
679 type = isl_dim_set;
681 if (pos >= isl_local_space_dim(aff->ls, type))
682 isl_die(ctx, isl_error_invalid,
683 "position out of bounds", return 0);
685 pos += isl_local_space_offset(aff->ls, type);
686 return isl_int_sgn(aff->v->el[1 + pos]);
689 /* Replace the denominator of "aff" by "v".
691 * A NaN is unaffected by this operation.
693 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
695 if (!aff)
696 return NULL;
697 if (isl_aff_is_nan(aff))
698 return aff;
699 aff = isl_aff_cow(aff);
700 if (!aff)
701 return NULL;
703 aff->v = isl_vec_cow(aff->v);
704 if (!aff->v)
705 return isl_aff_free(aff);
707 isl_int_set(aff->v->el[0], v);
709 return aff;
712 /* Replace the numerator of the constant term of "aff" by "v".
714 * A NaN is unaffected by this operation.
716 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
718 if (!aff)
719 return NULL;
720 if (isl_aff_is_nan(aff))
721 return aff;
722 aff = isl_aff_cow(aff);
723 if (!aff)
724 return NULL;
726 aff->v = isl_vec_cow(aff->v);
727 if (!aff->v)
728 return isl_aff_free(aff);
730 isl_int_set(aff->v->el[1], v);
732 return aff;
735 /* Replace the constant term of "aff" by "v".
737 * A NaN is unaffected by this operation.
739 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
740 __isl_take isl_val *v)
742 if (!aff || !v)
743 goto error;
745 if (isl_aff_is_nan(aff)) {
746 isl_val_free(v);
747 return aff;
750 if (!isl_val_is_rat(v))
751 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
752 "expecting rational value", goto error);
754 if (isl_int_eq(aff->v->el[1], v->n) &&
755 isl_int_eq(aff->v->el[0], v->d)) {
756 isl_val_free(v);
757 return aff;
760 aff = isl_aff_cow(aff);
761 if (!aff)
762 goto error;
763 aff->v = isl_vec_cow(aff->v);
764 if (!aff->v)
765 goto error;
767 if (isl_int_eq(aff->v->el[0], v->d)) {
768 isl_int_set(aff->v->el[1], v->n);
769 } else if (isl_int_is_one(v->d)) {
770 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
771 } else {
772 isl_seq_scale(aff->v->el + 1,
773 aff->v->el + 1, v->d, aff->v->size - 1);
774 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
775 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
776 aff->v = isl_vec_normalize(aff->v);
777 if (!aff->v)
778 goto error;
781 isl_val_free(v);
782 return aff;
783 error:
784 isl_aff_free(aff);
785 isl_val_free(v);
786 return NULL;
789 /* Add "v" to the constant term of "aff".
791 * A NaN is unaffected by this operation.
793 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
795 if (isl_int_is_zero(v))
796 return aff;
798 if (!aff)
799 return NULL;
800 if (isl_aff_is_nan(aff))
801 return aff;
802 aff = isl_aff_cow(aff);
803 if (!aff)
804 return NULL;
806 aff->v = isl_vec_cow(aff->v);
807 if (!aff->v)
808 return isl_aff_free(aff);
810 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
812 return aff;
815 /* Add "v" to the constant term of "aff".
817 * A NaN is unaffected by this operation.
819 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
820 __isl_take isl_val *v)
822 if (!aff || !v)
823 goto error;
825 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
826 isl_val_free(v);
827 return aff;
830 if (!isl_val_is_rat(v))
831 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
832 "expecting rational value", goto error);
834 aff = isl_aff_cow(aff);
835 if (!aff)
836 goto error;
838 aff->v = isl_vec_cow(aff->v);
839 if (!aff->v)
840 goto error;
842 if (isl_int_is_one(v->d)) {
843 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
844 } else if (isl_int_eq(aff->v->el[0], v->d)) {
845 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
846 aff->v = isl_vec_normalize(aff->v);
847 if (!aff->v)
848 goto error;
849 } else {
850 isl_seq_scale(aff->v->el + 1,
851 aff->v->el + 1, v->d, aff->v->size - 1);
852 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
853 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
854 aff->v = isl_vec_normalize(aff->v);
855 if (!aff->v)
856 goto error;
859 isl_val_free(v);
860 return aff;
861 error:
862 isl_aff_free(aff);
863 isl_val_free(v);
864 return NULL;
867 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
869 isl_int t;
871 isl_int_init(t);
872 isl_int_set_si(t, v);
873 aff = isl_aff_add_constant(aff, t);
874 isl_int_clear(t);
876 return aff;
879 /* Add "v" to the numerator of the constant term of "aff".
881 * A NaN is unaffected by this operation.
883 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
885 if (isl_int_is_zero(v))
886 return aff;
888 if (!aff)
889 return NULL;
890 if (isl_aff_is_nan(aff))
891 return aff;
892 aff = isl_aff_cow(aff);
893 if (!aff)
894 return NULL;
896 aff->v = isl_vec_cow(aff->v);
897 if (!aff->v)
898 return isl_aff_free(aff);
900 isl_int_add(aff->v->el[1], aff->v->el[1], v);
902 return aff;
905 /* Add "v" to the numerator of the constant term of "aff".
907 * A NaN is unaffected by this operation.
909 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
911 isl_int t;
913 if (v == 0)
914 return aff;
916 isl_int_init(t);
917 isl_int_set_si(t, v);
918 aff = isl_aff_add_constant_num(aff, t);
919 isl_int_clear(t);
921 return aff;
924 /* Replace the numerator of the constant term of "aff" by "v".
926 * A NaN is unaffected by this operation.
928 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
930 if (!aff)
931 return NULL;
932 if (isl_aff_is_nan(aff))
933 return aff;
934 aff = isl_aff_cow(aff);
935 if (!aff)
936 return NULL;
938 aff->v = isl_vec_cow(aff->v);
939 if (!aff->v)
940 return isl_aff_free(aff);
942 isl_int_set_si(aff->v->el[1], v);
944 return aff;
947 /* Replace the numerator of the coefficient of the variable of type "type"
948 * at position "pos" of "aff" by "v".
950 * A NaN is unaffected by this operation.
952 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
953 enum isl_dim_type type, int pos, isl_int v)
955 if (!aff)
956 return NULL;
958 if (type == isl_dim_out)
959 isl_die(aff->v->ctx, isl_error_invalid,
960 "output/set dimension does not have a coefficient",
961 return isl_aff_free(aff));
962 if (type == isl_dim_in)
963 type = isl_dim_set;
965 if (pos >= isl_local_space_dim(aff->ls, type))
966 isl_die(aff->v->ctx, isl_error_invalid,
967 "position out of bounds", return isl_aff_free(aff));
969 if (isl_aff_is_nan(aff))
970 return aff;
971 aff = isl_aff_cow(aff);
972 if (!aff)
973 return NULL;
975 aff->v = isl_vec_cow(aff->v);
976 if (!aff->v)
977 return isl_aff_free(aff);
979 pos += isl_local_space_offset(aff->ls, type);
980 isl_int_set(aff->v->el[1 + pos], v);
982 return aff;
985 /* Replace the numerator of the coefficient of the variable of type "type"
986 * at position "pos" of "aff" by "v".
988 * A NaN is unaffected by this operation.
990 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
991 enum isl_dim_type type, int pos, int v)
993 if (!aff)
994 return NULL;
996 if (type == isl_dim_out)
997 isl_die(aff->v->ctx, isl_error_invalid,
998 "output/set dimension does not have a coefficient",
999 return isl_aff_free(aff));
1000 if (type == isl_dim_in)
1001 type = isl_dim_set;
1003 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1004 isl_die(aff->v->ctx, isl_error_invalid,
1005 "position out of bounds", return isl_aff_free(aff));
1007 if (isl_aff_is_nan(aff))
1008 return aff;
1009 pos += isl_local_space_offset(aff->ls, type);
1010 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1011 return aff;
1013 aff = isl_aff_cow(aff);
1014 if (!aff)
1015 return NULL;
1017 aff->v = isl_vec_cow(aff->v);
1018 if (!aff->v)
1019 return isl_aff_free(aff);
1021 isl_int_set_si(aff->v->el[1 + pos], v);
1023 return aff;
1026 /* Replace the coefficient of the variable of type "type" at position "pos"
1027 * of "aff" by "v".
1029 * A NaN is unaffected by this operation.
1031 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1032 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1034 if (!aff || !v)
1035 goto error;
1037 if (type == isl_dim_out)
1038 isl_die(aff->v->ctx, isl_error_invalid,
1039 "output/set dimension does not have a coefficient",
1040 goto error);
1041 if (type == isl_dim_in)
1042 type = isl_dim_set;
1044 if (pos >= isl_local_space_dim(aff->ls, type))
1045 isl_die(aff->v->ctx, isl_error_invalid,
1046 "position out of bounds", goto error);
1048 if (isl_aff_is_nan(aff)) {
1049 isl_val_free(v);
1050 return aff;
1052 if (!isl_val_is_rat(v))
1053 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1054 "expecting rational value", goto error);
1056 pos += isl_local_space_offset(aff->ls, type);
1057 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1058 isl_int_eq(aff->v->el[0], v->d)) {
1059 isl_val_free(v);
1060 return aff;
1063 aff = isl_aff_cow(aff);
1064 if (!aff)
1065 goto error;
1066 aff->v = isl_vec_cow(aff->v);
1067 if (!aff->v)
1068 goto error;
1070 if (isl_int_eq(aff->v->el[0], v->d)) {
1071 isl_int_set(aff->v->el[1 + pos], v->n);
1072 } else if (isl_int_is_one(v->d)) {
1073 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1074 } else {
1075 isl_seq_scale(aff->v->el + 1,
1076 aff->v->el + 1, v->d, aff->v->size - 1);
1077 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1078 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1079 aff->v = isl_vec_normalize(aff->v);
1080 if (!aff->v)
1081 goto error;
1084 isl_val_free(v);
1085 return aff;
1086 error:
1087 isl_aff_free(aff);
1088 isl_val_free(v);
1089 return NULL;
1092 /* Add "v" to the coefficient of the variable of type "type"
1093 * at position "pos" of "aff".
1095 * A NaN is unaffected by this operation.
1097 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1098 enum isl_dim_type type, int pos, isl_int v)
1100 if (!aff)
1101 return NULL;
1103 if (type == isl_dim_out)
1104 isl_die(aff->v->ctx, isl_error_invalid,
1105 "output/set dimension does not have a coefficient",
1106 return isl_aff_free(aff));
1107 if (type == isl_dim_in)
1108 type = isl_dim_set;
1110 if (pos >= isl_local_space_dim(aff->ls, type))
1111 isl_die(aff->v->ctx, isl_error_invalid,
1112 "position out of bounds", return isl_aff_free(aff));
1114 if (isl_aff_is_nan(aff))
1115 return aff;
1116 aff = isl_aff_cow(aff);
1117 if (!aff)
1118 return NULL;
1120 aff->v = isl_vec_cow(aff->v);
1121 if (!aff->v)
1122 return isl_aff_free(aff);
1124 pos += isl_local_space_offset(aff->ls, type);
1125 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1127 return aff;
1130 /* Add "v" to the coefficient of the variable of type "type"
1131 * at position "pos" of "aff".
1133 * A NaN is unaffected by this operation.
1135 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1136 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1138 if (!aff || !v)
1139 goto error;
1141 if (isl_val_is_zero(v)) {
1142 isl_val_free(v);
1143 return aff;
1146 if (type == isl_dim_out)
1147 isl_die(aff->v->ctx, isl_error_invalid,
1148 "output/set dimension does not have a coefficient",
1149 goto error);
1150 if (type == isl_dim_in)
1151 type = isl_dim_set;
1153 if (pos >= isl_local_space_dim(aff->ls, type))
1154 isl_die(aff->v->ctx, isl_error_invalid,
1155 "position out of bounds", goto error);
1157 if (isl_aff_is_nan(aff)) {
1158 isl_val_free(v);
1159 return aff;
1161 if (!isl_val_is_rat(v))
1162 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1163 "expecting rational value", goto error);
1165 aff = isl_aff_cow(aff);
1166 if (!aff)
1167 goto error;
1169 aff->v = isl_vec_cow(aff->v);
1170 if (!aff->v)
1171 goto error;
1173 pos += isl_local_space_offset(aff->ls, type);
1174 if (isl_int_is_one(v->d)) {
1175 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1176 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1177 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1178 aff->v = isl_vec_normalize(aff->v);
1179 if (!aff->v)
1180 goto error;
1181 } else {
1182 isl_seq_scale(aff->v->el + 1,
1183 aff->v->el + 1, v->d, aff->v->size - 1);
1184 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1185 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1186 aff->v = isl_vec_normalize(aff->v);
1187 if (!aff->v)
1188 goto error;
1191 isl_val_free(v);
1192 return aff;
1193 error:
1194 isl_aff_free(aff);
1195 isl_val_free(v);
1196 return NULL;
1199 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1200 enum isl_dim_type type, int pos, int v)
1202 isl_int t;
1204 isl_int_init(t);
1205 isl_int_set_si(t, v);
1206 aff = isl_aff_add_coefficient(aff, type, pos, t);
1207 isl_int_clear(t);
1209 return aff;
1212 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1214 if (!aff)
1215 return NULL;
1217 return isl_local_space_get_div(aff->ls, pos);
1220 /* Return the negation of "aff".
1222 * As a special case, -NaN = NaN.
1224 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1226 if (!aff)
1227 return NULL;
1228 if (isl_aff_is_nan(aff))
1229 return aff;
1230 aff = isl_aff_cow(aff);
1231 if (!aff)
1232 return NULL;
1233 aff->v = isl_vec_cow(aff->v);
1234 if (!aff->v)
1235 return isl_aff_free(aff);
1237 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1239 return aff;
1242 /* Remove divs from the local space that do not appear in the affine
1243 * expression.
1244 * We currently only remove divs at the end.
1245 * Some intermediate divs may also not appear directly in the affine
1246 * expression, but we would also need to check that no other divs are
1247 * defined in terms of them.
1249 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1251 int pos;
1252 int off;
1253 int n;
1255 if (!aff)
1256 return NULL;
1258 n = isl_local_space_dim(aff->ls, isl_dim_div);
1259 off = isl_local_space_offset(aff->ls, isl_dim_div);
1261 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1262 if (pos == n)
1263 return aff;
1265 aff = isl_aff_cow(aff);
1266 if (!aff)
1267 return NULL;
1269 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1270 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1271 if (!aff->ls || !aff->v)
1272 return isl_aff_free(aff);
1274 return aff;
1277 /* Look for any divs in the aff->ls with a denominator equal to one
1278 * and plug them into the affine expression and any subsequent divs
1279 * that may reference the div.
1281 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1283 int i, n;
1284 int len;
1285 isl_int v;
1286 isl_vec *vec;
1287 isl_local_space *ls;
1288 unsigned pos;
1290 if (!aff)
1291 return NULL;
1293 n = isl_local_space_dim(aff->ls, isl_dim_div);
1294 len = aff->v->size;
1295 for (i = 0; i < n; ++i) {
1296 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1297 continue;
1298 ls = isl_local_space_copy(aff->ls);
1299 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1300 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1301 vec = isl_vec_copy(aff->v);
1302 vec = isl_vec_cow(vec);
1303 if (!ls || !vec)
1304 goto error;
1306 isl_int_init(v);
1308 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1309 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1310 len, len, v);
1312 isl_int_clear(v);
1314 isl_vec_free(aff->v);
1315 aff->v = vec;
1316 isl_local_space_free(aff->ls);
1317 aff->ls = ls;
1320 return aff;
1321 error:
1322 isl_vec_free(vec);
1323 isl_local_space_free(ls);
1324 return isl_aff_free(aff);
1327 /* Look for any divs j that appear with a unit coefficient inside
1328 * the definitions of other divs i and plug them into the definitions
1329 * of the divs i.
1331 * In particular, an expression of the form
1333 * floor((f(..) + floor(g(..)/n))/m)
1335 * is simplified to
1337 * floor((n * f(..) + g(..))/(n * m))
1339 * This simplification is correct because we can move the expression
1340 * f(..) into the inner floor in the original expression to obtain
1342 * floor(floor((n * f(..) + g(..))/n)/m)
1344 * from which we can derive the simplified expression.
1346 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1348 int i, j, n;
1349 int off;
1351 if (!aff)
1352 return NULL;
1354 n = isl_local_space_dim(aff->ls, isl_dim_div);
1355 off = isl_local_space_offset(aff->ls, isl_dim_div);
1356 for (i = 1; i < n; ++i) {
1357 for (j = 0; j < i; ++j) {
1358 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1359 continue;
1360 aff->ls = isl_local_space_substitute_seq(aff->ls,
1361 isl_dim_div, j, aff->ls->div->row[j],
1362 aff->v->size, i, 1);
1363 if (!aff->ls)
1364 return isl_aff_free(aff);
1368 return aff;
1371 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1373 * Even though this function is only called on isl_affs with a single
1374 * reference, we are careful to only change aff->v and aff->ls together.
1376 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1378 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1379 isl_local_space *ls;
1380 isl_vec *v;
1382 ls = isl_local_space_copy(aff->ls);
1383 ls = isl_local_space_swap_div(ls, a, b);
1384 v = isl_vec_copy(aff->v);
1385 v = isl_vec_cow(v);
1386 if (!ls || !v)
1387 goto error;
1389 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1390 isl_vec_free(aff->v);
1391 aff->v = v;
1392 isl_local_space_free(aff->ls);
1393 aff->ls = ls;
1395 return aff;
1396 error:
1397 isl_vec_free(v);
1398 isl_local_space_free(ls);
1399 return isl_aff_free(aff);
1402 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1404 * We currently do not actually remove div "b", but simply add its
1405 * coefficient to that of "a" and then zero it out.
1407 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1409 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1411 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1412 return aff;
1414 aff->v = isl_vec_cow(aff->v);
1415 if (!aff->v)
1416 return isl_aff_free(aff);
1418 isl_int_add(aff->v->el[1 + off + a],
1419 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1420 isl_int_set_si(aff->v->el[1 + off + b], 0);
1422 return aff;
1425 /* Sort the divs in the local space of "aff" according to
1426 * the comparison function "cmp_row" in isl_local_space.c,
1427 * combining the coefficients of identical divs.
1429 * Reordering divs does not change the semantics of "aff",
1430 * so there is no need to call isl_aff_cow.
1431 * Moreover, this function is currently only called on isl_affs
1432 * with a single reference.
1434 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1436 int i, j, n;
1438 if (!aff)
1439 return NULL;
1441 n = isl_aff_dim(aff, isl_dim_div);
1442 for (i = 1; i < n; ++i) {
1443 for (j = i - 1; j >= 0; --j) {
1444 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1445 if (cmp < 0)
1446 break;
1447 if (cmp == 0)
1448 aff = merge_divs(aff, j, j + 1);
1449 else
1450 aff = swap_div(aff, j, j + 1);
1451 if (!aff)
1452 return NULL;
1456 return aff;
1459 /* Normalize the representation of "aff".
1461 * This function should only be called of "new" isl_affs, i.e.,
1462 * with only a single reference. We therefore do not need to
1463 * worry about affecting other instances.
1465 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1467 if (!aff)
1468 return NULL;
1469 aff->v = isl_vec_normalize(aff->v);
1470 if (!aff->v)
1471 return isl_aff_free(aff);
1472 aff = plug_in_integral_divs(aff);
1473 aff = plug_in_unit_divs(aff);
1474 aff = sort_divs(aff);
1475 aff = isl_aff_remove_unused_divs(aff);
1476 return aff;
1479 /* Given f, return floor(f).
1480 * If f is an integer expression, then just return f.
1481 * If f is a constant, then return the constant floor(f).
1482 * Otherwise, if f = g/m, write g = q m + r,
1483 * create a new div d = [r/m] and return the expression q + d.
1484 * The coefficients in r are taken to lie between -m/2 and m/2.
1486 * reduce_div_coefficients performs the same normalization.
1488 * As a special case, floor(NaN) = NaN.
1490 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1492 int i;
1493 int size;
1494 isl_ctx *ctx;
1495 isl_vec *div;
1497 if (!aff)
1498 return NULL;
1500 if (isl_aff_is_nan(aff))
1501 return aff;
1502 if (isl_int_is_one(aff->v->el[0]))
1503 return aff;
1505 aff = isl_aff_cow(aff);
1506 if (!aff)
1507 return NULL;
1509 aff->v = isl_vec_cow(aff->v);
1510 if (!aff->v)
1511 return isl_aff_free(aff);
1513 if (isl_aff_is_cst(aff)) {
1514 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1515 isl_int_set_si(aff->v->el[0], 1);
1516 return aff;
1519 div = isl_vec_copy(aff->v);
1520 div = isl_vec_cow(div);
1521 if (!div)
1522 return isl_aff_free(aff);
1524 ctx = isl_aff_get_ctx(aff);
1525 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1526 for (i = 1; i < aff->v->size; ++i) {
1527 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1528 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1529 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1530 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1531 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1535 aff->ls = isl_local_space_add_div(aff->ls, div);
1536 if (!aff->ls)
1537 return isl_aff_free(aff);
1539 size = aff->v->size;
1540 aff->v = isl_vec_extend(aff->v, size + 1);
1541 if (!aff->v)
1542 return isl_aff_free(aff);
1543 isl_int_set_si(aff->v->el[0], 1);
1544 isl_int_set_si(aff->v->el[size], 1);
1546 aff = isl_aff_normalize(aff);
1548 return aff;
1551 /* Compute
1553 * aff mod m = aff - m * floor(aff/m)
1555 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1557 isl_aff *res;
1559 res = isl_aff_copy(aff);
1560 aff = isl_aff_scale_down(aff, m);
1561 aff = isl_aff_floor(aff);
1562 aff = isl_aff_scale(aff, m);
1563 res = isl_aff_sub(res, aff);
1565 return res;
1568 /* Compute
1570 * aff mod m = aff - m * floor(aff/m)
1572 * with m an integer value.
1574 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1575 __isl_take isl_val *m)
1577 isl_aff *res;
1579 if (!aff || !m)
1580 goto error;
1582 if (!isl_val_is_int(m))
1583 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1584 "expecting integer modulo", goto error);
1586 res = isl_aff_copy(aff);
1587 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1588 aff = isl_aff_floor(aff);
1589 aff = isl_aff_scale_val(aff, m);
1590 res = isl_aff_sub(res, aff);
1592 return res;
1593 error:
1594 isl_aff_free(aff);
1595 isl_val_free(m);
1596 return NULL;
1599 /* Compute
1601 * pwaff mod m = pwaff - m * floor(pwaff/m)
1603 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1605 isl_pw_aff *res;
1607 res = isl_pw_aff_copy(pwaff);
1608 pwaff = isl_pw_aff_scale_down(pwaff, m);
1609 pwaff = isl_pw_aff_floor(pwaff);
1610 pwaff = isl_pw_aff_scale(pwaff, m);
1611 res = isl_pw_aff_sub(res, pwaff);
1613 return res;
1616 /* Compute
1618 * pa mod m = pa - m * floor(pa/m)
1620 * with m an integer value.
1622 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1623 __isl_take isl_val *m)
1625 if (!pa || !m)
1626 goto error;
1627 if (!isl_val_is_int(m))
1628 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1629 "expecting integer modulo", goto error);
1630 pa = isl_pw_aff_mod(pa, m->n);
1631 isl_val_free(m);
1632 return pa;
1633 error:
1634 isl_pw_aff_free(pa);
1635 isl_val_free(m);
1636 return NULL;
1639 /* Given f, return ceil(f).
1640 * If f is an integer expression, then just return f.
1641 * Otherwise, let f be the expression
1643 * e/m
1645 * then return
1647 * floor((e + m - 1)/m)
1649 * As a special case, ceil(NaN) = NaN.
1651 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1653 if (!aff)
1654 return NULL;
1656 if (isl_aff_is_nan(aff))
1657 return aff;
1658 if (isl_int_is_one(aff->v->el[0]))
1659 return aff;
1661 aff = isl_aff_cow(aff);
1662 if (!aff)
1663 return NULL;
1664 aff->v = isl_vec_cow(aff->v);
1665 if (!aff->v)
1666 return isl_aff_free(aff);
1668 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1669 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1670 aff = isl_aff_floor(aff);
1672 return aff;
1675 /* Apply the expansion computed by isl_merge_divs.
1676 * The expansion itself is given by "exp" while the resulting
1677 * list of divs is given by "div".
1679 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1680 __isl_take isl_mat *div, int *exp)
1682 int old_n_div;
1683 int new_n_div;
1684 int offset;
1686 aff = isl_aff_cow(aff);
1687 if (!aff || !div)
1688 goto error;
1690 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1691 new_n_div = isl_mat_rows(div);
1692 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1694 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1695 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1696 if (!aff->v || !aff->ls)
1697 return isl_aff_free(aff);
1698 return aff;
1699 error:
1700 isl_aff_free(aff);
1701 isl_mat_free(div);
1702 return NULL;
1705 /* Add two affine expressions that live in the same local space.
1707 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1708 __isl_take isl_aff *aff2)
1710 isl_int gcd, f;
1712 aff1 = isl_aff_cow(aff1);
1713 if (!aff1 || !aff2)
1714 goto error;
1716 aff1->v = isl_vec_cow(aff1->v);
1717 if (!aff1->v)
1718 goto error;
1720 isl_int_init(gcd);
1721 isl_int_init(f);
1722 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1723 isl_int_divexact(f, aff2->v->el[0], gcd);
1724 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1725 isl_int_divexact(f, aff1->v->el[0], gcd);
1726 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1727 isl_int_divexact(f, aff2->v->el[0], gcd);
1728 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1729 isl_int_clear(f);
1730 isl_int_clear(gcd);
1732 isl_aff_free(aff2);
1733 return aff1;
1734 error:
1735 isl_aff_free(aff1);
1736 isl_aff_free(aff2);
1737 return NULL;
1740 /* Return the sum of "aff1" and "aff2".
1742 * If either of the two is NaN, then the result is NaN.
1744 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1745 __isl_take isl_aff *aff2)
1747 isl_ctx *ctx;
1748 int *exp1 = NULL;
1749 int *exp2 = NULL;
1750 isl_mat *div;
1751 int n_div1, n_div2;
1753 if (!aff1 || !aff2)
1754 goto error;
1756 ctx = isl_aff_get_ctx(aff1);
1757 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1758 isl_die(ctx, isl_error_invalid,
1759 "spaces don't match", goto error);
1761 if (isl_aff_is_nan(aff1)) {
1762 isl_aff_free(aff2);
1763 return aff1;
1765 if (isl_aff_is_nan(aff2)) {
1766 isl_aff_free(aff1);
1767 return aff2;
1770 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1771 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1772 if (n_div1 == 0 && n_div2 == 0)
1773 return add_expanded(aff1, aff2);
1775 exp1 = isl_alloc_array(ctx, int, n_div1);
1776 exp2 = isl_alloc_array(ctx, int, n_div2);
1777 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1778 goto error;
1780 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1781 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1782 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1783 free(exp1);
1784 free(exp2);
1786 return add_expanded(aff1, aff2);
1787 error:
1788 free(exp1);
1789 free(exp2);
1790 isl_aff_free(aff1);
1791 isl_aff_free(aff2);
1792 return NULL;
1795 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1796 __isl_take isl_aff *aff2)
1798 return isl_aff_add(aff1, isl_aff_neg(aff2));
1801 /* Return the result of scaling "aff" by a factor of "f".
1803 * As a special case, f * NaN = NaN.
1805 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1807 isl_int gcd;
1809 if (!aff)
1810 return NULL;
1811 if (isl_aff_is_nan(aff))
1812 return aff;
1814 if (isl_int_is_one(f))
1815 return aff;
1817 aff = isl_aff_cow(aff);
1818 if (!aff)
1819 return NULL;
1820 aff->v = isl_vec_cow(aff->v);
1821 if (!aff->v)
1822 return isl_aff_free(aff);
1824 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1825 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1826 return aff;
1829 isl_int_init(gcd);
1830 isl_int_gcd(gcd, aff->v->el[0], f);
1831 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1832 isl_int_divexact(gcd, f, gcd);
1833 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1834 isl_int_clear(gcd);
1836 return aff;
1839 /* Multiple "aff" by "v".
1841 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1842 __isl_take isl_val *v)
1844 if (!aff || !v)
1845 goto error;
1847 if (isl_val_is_one(v)) {
1848 isl_val_free(v);
1849 return aff;
1852 if (!isl_val_is_rat(v))
1853 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1854 "expecting rational factor", goto error);
1856 aff = isl_aff_scale(aff, v->n);
1857 aff = isl_aff_scale_down(aff, v->d);
1859 isl_val_free(v);
1860 return aff;
1861 error:
1862 isl_aff_free(aff);
1863 isl_val_free(v);
1864 return NULL;
1867 /* Return the result of scaling "aff" down by a factor of "f".
1869 * As a special case, NaN/f = NaN.
1871 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1873 isl_int gcd;
1875 if (!aff)
1876 return NULL;
1877 if (isl_aff_is_nan(aff))
1878 return aff;
1880 if (isl_int_is_one(f))
1881 return aff;
1883 aff = isl_aff_cow(aff);
1884 if (!aff)
1885 return NULL;
1887 if (isl_int_is_zero(f))
1888 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1889 "cannot scale down by zero", return isl_aff_free(aff));
1891 aff->v = isl_vec_cow(aff->v);
1892 if (!aff->v)
1893 return isl_aff_free(aff);
1895 isl_int_init(gcd);
1896 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1897 isl_int_gcd(gcd, gcd, f);
1898 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1899 isl_int_divexact(gcd, f, gcd);
1900 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1901 isl_int_clear(gcd);
1903 return aff;
1906 /* Divide "aff" by "v".
1908 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1909 __isl_take isl_val *v)
1911 if (!aff || !v)
1912 goto error;
1914 if (isl_val_is_one(v)) {
1915 isl_val_free(v);
1916 return aff;
1919 if (!isl_val_is_rat(v))
1920 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1921 "expecting rational factor", goto error);
1922 if (!isl_val_is_pos(v))
1923 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1924 "factor needs to be positive", goto error);
1926 aff = isl_aff_scale(aff, v->d);
1927 aff = isl_aff_scale_down(aff, v->n);
1929 isl_val_free(v);
1930 return aff;
1931 error:
1932 isl_aff_free(aff);
1933 isl_val_free(v);
1934 return NULL;
1937 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1939 isl_int v;
1941 if (f == 1)
1942 return aff;
1944 isl_int_init(v);
1945 isl_int_set_ui(v, f);
1946 aff = isl_aff_scale_down(aff, v);
1947 isl_int_clear(v);
1949 return aff;
1952 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1953 enum isl_dim_type type, unsigned pos, const char *s)
1955 aff = isl_aff_cow(aff);
1956 if (!aff)
1957 return NULL;
1958 if (type == isl_dim_out)
1959 isl_die(aff->v->ctx, isl_error_invalid,
1960 "cannot set name of output/set dimension",
1961 return isl_aff_free(aff));
1962 if (type == isl_dim_in)
1963 type = isl_dim_set;
1964 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1965 if (!aff->ls)
1966 return isl_aff_free(aff);
1968 return aff;
1971 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1972 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1974 aff = isl_aff_cow(aff);
1975 if (!aff)
1976 goto error;
1977 if (type == isl_dim_out)
1978 isl_die(aff->v->ctx, isl_error_invalid,
1979 "cannot set name of output/set dimension",
1980 goto error);
1981 if (type == isl_dim_in)
1982 type = isl_dim_set;
1983 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1984 if (!aff->ls)
1985 return isl_aff_free(aff);
1987 return aff;
1988 error:
1989 isl_id_free(id);
1990 isl_aff_free(aff);
1991 return NULL;
1994 /* Replace the identifier of the input tuple of "aff" by "id".
1995 * type is currently required to be equal to isl_dim_in
1997 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1998 enum isl_dim_type type, __isl_take isl_id *id)
2000 aff = isl_aff_cow(aff);
2001 if (!aff)
2002 goto error;
2003 if (type != isl_dim_out)
2004 isl_die(aff->v->ctx, isl_error_invalid,
2005 "cannot only set id of input tuple", goto error);
2006 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2007 if (!aff->ls)
2008 return isl_aff_free(aff);
2010 return aff;
2011 error:
2012 isl_id_free(id);
2013 isl_aff_free(aff);
2014 return NULL;
2017 /* Exploit the equalities in "eq" to simplify the affine expression
2018 * and the expressions of the integer divisions in the local space.
2019 * The integer divisions in this local space are assumed to appear
2020 * as regular dimensions in "eq".
2022 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2023 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2025 int i, j;
2026 unsigned total;
2027 unsigned n_div;
2029 if (!eq)
2030 goto error;
2031 if (eq->n_eq == 0) {
2032 isl_basic_set_free(eq);
2033 return aff;
2036 aff = isl_aff_cow(aff);
2037 if (!aff)
2038 goto error;
2040 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2041 isl_basic_set_copy(eq));
2042 aff->v = isl_vec_cow(aff->v);
2043 if (!aff->ls || !aff->v)
2044 goto error;
2046 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2047 n_div = eq->n_div;
2048 for (i = 0; i < eq->n_eq; ++i) {
2049 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2050 if (j < 0 || j == 0 || j >= total)
2051 continue;
2053 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2054 &aff->v->el[0]);
2057 isl_basic_set_free(eq);
2058 aff = isl_aff_normalize(aff);
2059 return aff;
2060 error:
2061 isl_basic_set_free(eq);
2062 isl_aff_free(aff);
2063 return NULL;
2066 /* Exploit the equalities in "eq" to simplify the affine expression
2067 * and the expressions of the integer divisions in the local space.
2069 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2070 __isl_take isl_basic_set *eq)
2072 int n_div;
2074 if (!aff || !eq)
2075 goto error;
2076 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2077 if (n_div > 0)
2078 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2079 return isl_aff_substitute_equalities_lifted(aff, eq);
2080 error:
2081 isl_basic_set_free(eq);
2082 isl_aff_free(aff);
2083 return NULL;
2086 /* Look for equalities among the variables shared by context and aff
2087 * and the integer divisions of aff, if any.
2088 * The equalities are then used to eliminate coefficients and/or integer
2089 * divisions from aff.
2091 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2092 __isl_take isl_set *context)
2094 isl_basic_set *hull;
2095 int n_div;
2097 if (!aff)
2098 goto error;
2099 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2100 if (n_div > 0) {
2101 isl_basic_set *bset;
2102 isl_local_space *ls;
2103 context = isl_set_add_dims(context, isl_dim_set, n_div);
2104 ls = isl_aff_get_domain_local_space(aff);
2105 bset = isl_basic_set_from_local_space(ls);
2106 bset = isl_basic_set_lift(bset);
2107 bset = isl_basic_set_flatten(bset);
2108 context = isl_set_intersect(context,
2109 isl_set_from_basic_set(bset));
2112 hull = isl_set_affine_hull(context);
2113 return isl_aff_substitute_equalities_lifted(aff, hull);
2114 error:
2115 isl_aff_free(aff);
2116 isl_set_free(context);
2117 return NULL;
2120 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2121 __isl_take isl_set *context)
2123 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2124 dom_context = isl_set_intersect_params(dom_context, context);
2125 return isl_aff_gist(aff, dom_context);
2128 /* Return a basic set containing those elements in the space
2129 * of aff where it is positive. "rational" should not be set.
2131 * If "aff" is NaN, then it is not positive.
2133 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2134 int rational)
2136 isl_constraint *ineq;
2137 isl_basic_set *bset;
2138 isl_val *c;
2140 if (!aff)
2141 return NULL;
2142 if (isl_aff_is_nan(aff)) {
2143 isl_space *space = isl_aff_get_domain_space(aff);
2144 isl_aff_free(aff);
2145 return isl_basic_set_empty(space);
2147 if (rational)
2148 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2149 "rational sets not supported", goto error);
2151 ineq = isl_inequality_from_aff(aff);
2152 c = isl_constraint_get_constant_val(ineq);
2153 c = isl_val_sub_ui(c, 1);
2154 ineq = isl_constraint_set_constant_val(ineq, c);
2156 bset = isl_basic_set_from_constraint(ineq);
2157 bset = isl_basic_set_simplify(bset);
2158 return bset;
2159 error:
2160 isl_aff_free(aff);
2161 return NULL;
2164 /* Return a basic set containing those elements in the space
2165 * of aff where it is non-negative.
2166 * If "rational" is set, then return a rational basic set.
2168 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2170 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2171 __isl_take isl_aff *aff, int rational)
2173 isl_constraint *ineq;
2174 isl_basic_set *bset;
2176 if (!aff)
2177 return NULL;
2178 if (isl_aff_is_nan(aff)) {
2179 isl_space *space = isl_aff_get_domain_space(aff);
2180 isl_aff_free(aff);
2181 return isl_basic_set_empty(space);
2184 ineq = isl_inequality_from_aff(aff);
2186 bset = isl_basic_set_from_constraint(ineq);
2187 if (rational)
2188 bset = isl_basic_set_set_rational(bset);
2189 bset = isl_basic_set_simplify(bset);
2190 return bset;
2193 /* Return a basic set containing those elements in the space
2194 * of aff where it is non-negative.
2196 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2198 return aff_nonneg_basic_set(aff, 0);
2201 /* Return a basic set containing those elements in the domain space
2202 * of "aff" where it is positive.
2204 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2206 aff = isl_aff_add_constant_num_si(aff, -1);
2207 return isl_aff_nonneg_basic_set(aff);
2210 /* Return a basic set containing those elements in the domain space
2211 * of aff where it is negative.
2213 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2215 aff = isl_aff_neg(aff);
2216 return isl_aff_pos_basic_set(aff);
2219 /* Return a basic set containing those elements in the space
2220 * of aff where it is zero.
2221 * If "rational" is set, then return a rational basic set.
2223 * If "aff" is NaN, then it is not zero.
2225 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2226 int rational)
2228 isl_constraint *ineq;
2229 isl_basic_set *bset;
2231 if (!aff)
2232 return NULL;
2233 if (isl_aff_is_nan(aff)) {
2234 isl_space *space = isl_aff_get_domain_space(aff);
2235 isl_aff_free(aff);
2236 return isl_basic_set_empty(space);
2239 ineq = isl_equality_from_aff(aff);
2241 bset = isl_basic_set_from_constraint(ineq);
2242 if (rational)
2243 bset = isl_basic_set_set_rational(bset);
2244 bset = isl_basic_set_simplify(bset);
2245 return bset;
2248 /* Return a basic set containing those elements in the space
2249 * of aff where it is zero.
2251 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2253 return aff_zero_basic_set(aff, 0);
2256 /* Return a basic set containing those elements in the shared space
2257 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2259 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2260 __isl_take isl_aff *aff2)
2262 aff1 = isl_aff_sub(aff1, aff2);
2264 return isl_aff_nonneg_basic_set(aff1);
2267 /* Return a basic set containing those elements in the shared domain space
2268 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2270 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2271 __isl_take isl_aff *aff2)
2273 aff1 = isl_aff_sub(aff1, aff2);
2275 return isl_aff_pos_basic_set(aff1);
2278 /* Return a set containing those elements in the shared space
2279 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2281 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2282 __isl_take isl_aff *aff2)
2284 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2287 /* Return a basic set containing those elements in the shared space
2288 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2290 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2291 __isl_take isl_aff *aff2)
2293 return isl_aff_ge_basic_set(aff2, aff1);
2296 /* Return a basic set containing those elements in the shared domain space
2297 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2299 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2300 __isl_take isl_aff *aff2)
2302 return isl_aff_gt_basic_set(aff2, aff1);
2305 /* Return a set containing those elements in the shared space
2306 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2308 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2309 __isl_take isl_aff *aff2)
2311 return isl_aff_ge_set(aff2, aff1);
2314 /* Return a set containing those elements in the shared domain space
2315 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2317 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2318 __isl_take isl_aff *aff2)
2320 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2323 /* Return a basic set containing those elements in the shared space
2324 * of aff1 and aff2 where aff1 and aff2 are equal.
2326 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2327 __isl_take isl_aff *aff2)
2329 aff1 = isl_aff_sub(aff1, aff2);
2331 return isl_aff_zero_basic_set(aff1);
2334 /* Return a set containing those elements in the shared space
2335 * of aff1 and aff2 where aff1 and aff2 are equal.
2337 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2338 __isl_take isl_aff *aff2)
2340 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2343 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2344 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2346 aff1 = isl_aff_add(aff1, aff2);
2347 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2348 return aff1;
2351 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2353 if (!aff)
2354 return -1;
2356 return 0;
2359 /* Check whether the given affine expression has non-zero coefficient
2360 * for any dimension in the given range or if any of these dimensions
2361 * appear with non-zero coefficients in any of the integer divisions
2362 * involved in the affine expression.
2364 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2365 enum isl_dim_type type, unsigned first, unsigned n)
2367 int i;
2368 isl_ctx *ctx;
2369 int *active = NULL;
2370 isl_bool involves = isl_bool_false;
2372 if (!aff)
2373 return isl_bool_error;
2374 if (n == 0)
2375 return isl_bool_false;
2377 ctx = isl_aff_get_ctx(aff);
2378 if (first + n > isl_aff_dim(aff, type))
2379 isl_die(ctx, isl_error_invalid,
2380 "range out of bounds", return isl_bool_error);
2382 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2383 if (!active)
2384 goto error;
2386 first += isl_local_space_offset(aff->ls, type) - 1;
2387 for (i = 0; i < n; ++i)
2388 if (active[first + i]) {
2389 involves = isl_bool_true;
2390 break;
2393 free(active);
2395 return involves;
2396 error:
2397 free(active);
2398 return isl_bool_error;
2401 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2402 enum isl_dim_type type, unsigned first, unsigned n)
2404 isl_ctx *ctx;
2406 if (!aff)
2407 return NULL;
2408 if (type == isl_dim_out)
2409 isl_die(aff->v->ctx, isl_error_invalid,
2410 "cannot drop output/set dimension",
2411 return isl_aff_free(aff));
2412 if (type == isl_dim_in)
2413 type = isl_dim_set;
2414 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2415 return aff;
2417 ctx = isl_aff_get_ctx(aff);
2418 if (first + n > isl_local_space_dim(aff->ls, type))
2419 isl_die(ctx, isl_error_invalid, "range out of bounds",
2420 return isl_aff_free(aff));
2422 aff = isl_aff_cow(aff);
2423 if (!aff)
2424 return NULL;
2426 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2427 if (!aff->ls)
2428 return isl_aff_free(aff);
2430 first += 1 + isl_local_space_offset(aff->ls, type);
2431 aff->v = isl_vec_drop_els(aff->v, first, n);
2432 if (!aff->v)
2433 return isl_aff_free(aff);
2435 return aff;
2438 /* Project the domain of the affine expression onto its parameter space.
2439 * The affine expression may not involve any of the domain dimensions.
2441 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2443 isl_space *space;
2444 unsigned n;
2445 int involves;
2447 n = isl_aff_dim(aff, isl_dim_in);
2448 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2449 if (involves < 0)
2450 return isl_aff_free(aff);
2451 if (involves)
2452 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2453 "affine expression involves some of the domain dimensions",
2454 return isl_aff_free(aff));
2455 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2456 space = isl_aff_get_domain_space(aff);
2457 space = isl_space_params(space);
2458 aff = isl_aff_reset_domain_space(aff, space);
2459 return aff;
2462 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2463 enum isl_dim_type type, unsigned first, unsigned n)
2465 isl_ctx *ctx;
2467 if (!aff)
2468 return NULL;
2469 if (type == isl_dim_out)
2470 isl_die(aff->v->ctx, isl_error_invalid,
2471 "cannot insert output/set dimensions",
2472 return isl_aff_free(aff));
2473 if (type == isl_dim_in)
2474 type = isl_dim_set;
2475 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2476 return aff;
2478 ctx = isl_aff_get_ctx(aff);
2479 if (first > isl_local_space_dim(aff->ls, type))
2480 isl_die(ctx, isl_error_invalid, "position out of bounds",
2481 return isl_aff_free(aff));
2483 aff = isl_aff_cow(aff);
2484 if (!aff)
2485 return NULL;
2487 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2488 if (!aff->ls)
2489 return isl_aff_free(aff);
2491 first += 1 + isl_local_space_offset(aff->ls, type);
2492 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2493 if (!aff->v)
2494 return isl_aff_free(aff);
2496 return aff;
2499 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2500 enum isl_dim_type type, unsigned n)
2502 unsigned pos;
2504 pos = isl_aff_dim(aff, type);
2506 return isl_aff_insert_dims(aff, type, pos, n);
2509 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2510 enum isl_dim_type type, unsigned n)
2512 unsigned pos;
2514 pos = isl_pw_aff_dim(pwaff, type);
2516 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2519 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2520 * to dimensions of "dst_type" at "dst_pos".
2522 * We only support moving input dimensions to parameters and vice versa.
2524 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2525 enum isl_dim_type dst_type, unsigned dst_pos,
2526 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2528 unsigned g_dst_pos;
2529 unsigned g_src_pos;
2531 if (!aff)
2532 return NULL;
2533 if (n == 0 &&
2534 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2535 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2536 return aff;
2538 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2539 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2540 "cannot move output/set dimension",
2541 return isl_aff_free(aff));
2542 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2543 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2544 "cannot move divs", return isl_aff_free(aff));
2545 if (dst_type == isl_dim_in)
2546 dst_type = isl_dim_set;
2547 if (src_type == isl_dim_in)
2548 src_type = isl_dim_set;
2550 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2551 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2552 "range out of bounds", return isl_aff_free(aff));
2553 if (dst_type == src_type)
2554 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2555 "moving dims within the same type not supported",
2556 return isl_aff_free(aff));
2558 aff = isl_aff_cow(aff);
2559 if (!aff)
2560 return NULL;
2562 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2563 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2564 if (dst_type > src_type)
2565 g_dst_pos -= n;
2567 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2568 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2569 src_type, src_pos, n);
2570 if (!aff->v || !aff->ls)
2571 return isl_aff_free(aff);
2573 aff = sort_divs(aff);
2575 return aff;
2578 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2580 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2581 return isl_pw_aff_alloc(dom, aff);
2584 #define isl_aff_involves_nan isl_aff_is_nan
2586 #undef PW
2587 #define PW isl_pw_aff
2588 #undef EL
2589 #define EL isl_aff
2590 #undef EL_IS_ZERO
2591 #define EL_IS_ZERO is_empty
2592 #undef ZERO
2593 #define ZERO empty
2594 #undef IS_ZERO
2595 #define IS_ZERO is_empty
2596 #undef FIELD
2597 #define FIELD aff
2598 #undef DEFAULT_IS_ZERO
2599 #define DEFAULT_IS_ZERO 0
2601 #define NO_EVAL
2602 #define NO_OPT
2603 #define NO_LIFT
2604 #define NO_MORPH
2606 #include <isl_pw_templ.c>
2607 #include <isl_pw_hash.c>
2608 #include <isl_pw_union_opt.c>
2610 #undef UNION
2611 #define UNION isl_union_pw_aff
2612 #undef PART
2613 #define PART isl_pw_aff
2614 #undef PARTS
2615 #define PARTS pw_aff
2617 #include <isl_union_single.c>
2618 #include <isl_union_neg.c>
2620 static __isl_give isl_set *align_params_pw_pw_set_and(
2621 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2622 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2623 __isl_take isl_pw_aff *pwaff2))
2625 isl_bool equal_params;
2627 if (!pwaff1 || !pwaff2)
2628 goto error;
2629 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2630 if (equal_params < 0)
2631 goto error;
2632 if (equal_params)
2633 return fn(pwaff1, pwaff2);
2634 if (!isl_space_has_named_params(pwaff1->dim) ||
2635 !isl_space_has_named_params(pwaff2->dim))
2636 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2637 "unaligned unnamed parameters", goto error);
2638 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2639 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2640 return fn(pwaff1, pwaff2);
2641 error:
2642 isl_pw_aff_free(pwaff1);
2643 isl_pw_aff_free(pwaff2);
2644 return NULL;
2647 /* Align the parameters of the to isl_pw_aff arguments and
2648 * then apply a function "fn" on them that returns an isl_map.
2650 static __isl_give isl_map *align_params_pw_pw_map_and(
2651 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2652 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2653 __isl_take isl_pw_aff *pa2))
2655 isl_bool equal_params;
2657 if (!pa1 || !pa2)
2658 goto error;
2659 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2660 if (equal_params < 0)
2661 goto error;
2662 if (equal_params)
2663 return fn(pa1, pa2);
2664 if (!isl_space_has_named_params(pa1->dim) ||
2665 !isl_space_has_named_params(pa2->dim))
2666 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2667 "unaligned unnamed parameters", goto error);
2668 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2669 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2670 return fn(pa1, pa2);
2671 error:
2672 isl_pw_aff_free(pa1);
2673 isl_pw_aff_free(pa2);
2674 return NULL;
2677 /* Compute a piecewise quasi-affine expression with a domain that
2678 * is the union of those of pwaff1 and pwaff2 and such that on each
2679 * cell, the quasi-affine expression is the maximum of those of pwaff1
2680 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2681 * cell, then the associated expression is the defined one.
2683 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2684 __isl_take isl_pw_aff *pwaff2)
2686 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2689 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2690 __isl_take isl_pw_aff *pwaff2)
2692 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2693 &pw_aff_union_max);
2696 /* Compute a piecewise quasi-affine expression with a domain that
2697 * is the union of those of pwaff1 and pwaff2 and such that on each
2698 * cell, the quasi-affine expression is the minimum of those of pwaff1
2699 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2700 * cell, then the associated expression is the defined one.
2702 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2703 __isl_take isl_pw_aff *pwaff2)
2705 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2708 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2709 __isl_take isl_pw_aff *pwaff2)
2711 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2712 &pw_aff_union_min);
2715 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2716 __isl_take isl_pw_aff *pwaff2, int max)
2718 if (max)
2719 return isl_pw_aff_union_max(pwaff1, pwaff2);
2720 else
2721 return isl_pw_aff_union_min(pwaff1, pwaff2);
2724 /* Construct a map with as domain the domain of pwaff and
2725 * one-dimensional range corresponding to the affine expressions.
2727 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2729 int i;
2730 isl_space *dim;
2731 isl_map *map;
2733 if (!pwaff)
2734 return NULL;
2736 dim = isl_pw_aff_get_space(pwaff);
2737 map = isl_map_empty(dim);
2739 for (i = 0; i < pwaff->n; ++i) {
2740 isl_basic_map *bmap;
2741 isl_map *map_i;
2743 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2744 map_i = isl_map_from_basic_map(bmap);
2745 map_i = isl_map_intersect_domain(map_i,
2746 isl_set_copy(pwaff->p[i].set));
2747 map = isl_map_union_disjoint(map, map_i);
2750 isl_pw_aff_free(pwaff);
2752 return map;
2755 /* Construct a map with as domain the domain of pwaff and
2756 * one-dimensional range corresponding to the affine expressions.
2758 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2760 if (!pwaff)
2761 return NULL;
2762 if (isl_space_is_set(pwaff->dim))
2763 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2764 "space of input is not a map", goto error);
2765 return map_from_pw_aff(pwaff);
2766 error:
2767 isl_pw_aff_free(pwaff);
2768 return NULL;
2771 /* Construct a one-dimensional set with as parameter domain
2772 * the domain of pwaff and the single set dimension
2773 * corresponding to the affine expressions.
2775 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2777 if (!pwaff)
2778 return NULL;
2779 if (!isl_space_is_set(pwaff->dim))
2780 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2781 "space of input is not a set", goto error);
2782 return map_from_pw_aff(pwaff);
2783 error:
2784 isl_pw_aff_free(pwaff);
2785 return NULL;
2788 /* Return a set containing those elements in the domain
2789 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2790 * does not satisfy "fn" (if complement is 1).
2792 * The pieces with a NaN never belong to the result since
2793 * NaN does not satisfy any property.
2795 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2796 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2797 int complement)
2799 int i;
2800 isl_set *set;
2802 if (!pwaff)
2803 return NULL;
2805 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2807 for (i = 0; i < pwaff->n; ++i) {
2808 isl_basic_set *bset;
2809 isl_set *set_i, *locus;
2810 isl_bool rational;
2812 if (isl_aff_is_nan(pwaff->p[i].aff))
2813 continue;
2815 rational = isl_set_has_rational(pwaff->p[i].set);
2816 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2817 locus = isl_set_from_basic_set(bset);
2818 set_i = isl_set_copy(pwaff->p[i].set);
2819 if (complement)
2820 set_i = isl_set_subtract(set_i, locus);
2821 else
2822 set_i = isl_set_intersect(set_i, locus);
2823 set = isl_set_union_disjoint(set, set_i);
2826 isl_pw_aff_free(pwaff);
2828 return set;
2831 /* Return a set containing those elements in the domain
2832 * of "pa" where it is positive.
2834 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2836 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2839 /* Return a set containing those elements in the domain
2840 * of pwaff where it is non-negative.
2842 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2844 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2847 /* Return a set containing those elements in the domain
2848 * of pwaff where it is zero.
2850 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2852 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2855 /* Return a set containing those elements in the domain
2856 * of pwaff where it is not zero.
2858 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2860 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2863 /* Return a set containing those elements in the shared domain
2864 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2866 * We compute the difference on the shared domain and then construct
2867 * the set of values where this difference is non-negative.
2868 * If strict is set, we first subtract 1 from the difference.
2869 * If equal is set, we only return the elements where pwaff1 and pwaff2
2870 * are equal.
2872 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2873 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2875 isl_set *set1, *set2;
2877 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2878 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2879 set1 = isl_set_intersect(set1, set2);
2880 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2881 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2882 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2884 if (strict) {
2885 isl_space *dim = isl_set_get_space(set1);
2886 isl_aff *aff;
2887 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2888 aff = isl_aff_add_constant_si(aff, -1);
2889 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2890 } else
2891 isl_set_free(set1);
2893 if (equal)
2894 return isl_pw_aff_zero_set(pwaff1);
2895 return isl_pw_aff_nonneg_set(pwaff1);
2898 /* Return a set containing those elements in the shared domain
2899 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2901 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2902 __isl_take isl_pw_aff *pwaff2)
2904 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2907 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2908 __isl_take isl_pw_aff *pwaff2)
2910 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2913 /* Return a set containing those elements in the shared domain
2914 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2916 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2917 __isl_take isl_pw_aff *pwaff2)
2919 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2922 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2923 __isl_take isl_pw_aff *pwaff2)
2925 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2928 /* Return a set containing those elements in the shared domain
2929 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2931 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2932 __isl_take isl_pw_aff *pwaff2)
2934 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2937 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2938 __isl_take isl_pw_aff *pwaff2)
2940 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2943 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2944 __isl_take isl_pw_aff *pwaff2)
2946 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2949 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2950 __isl_take isl_pw_aff *pwaff2)
2952 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2955 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2956 * where the function values are ordered in the same way as "order",
2957 * which returns a set in the shared domain of its two arguments.
2958 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2960 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2961 * We first pull back the two functions such that they are defined on
2962 * the domain [A -> B]. Then we apply "order", resulting in a set
2963 * in the space [A -> B]. Finally, we unwrap this set to obtain
2964 * a map in the space A -> B.
2966 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2967 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2968 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2969 __isl_take isl_pw_aff *pa2))
2971 isl_space *space1, *space2;
2972 isl_multi_aff *ma;
2973 isl_set *set;
2975 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2976 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2977 space1 = isl_space_map_from_domain_and_range(space1, space2);
2978 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2979 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2980 ma = isl_multi_aff_range_map(space1);
2981 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2982 set = order(pa1, pa2);
2984 return isl_set_unwrap(set);
2987 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2988 * where the function values are equal.
2989 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2991 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
2992 __isl_take isl_pw_aff *pa2)
2994 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
2997 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2998 * where the function values are equal.
3000 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3001 __isl_take isl_pw_aff *pa2)
3003 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3006 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3007 * where the function value of "pa1" is less than the function value of "pa2".
3008 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3010 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3011 __isl_take isl_pw_aff *pa2)
3013 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3016 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3017 * where the function value of "pa1" is less than the function value of "pa2".
3019 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3020 __isl_take isl_pw_aff *pa2)
3022 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3025 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3026 * where the function value of "pa1" is greater than the function value
3027 * of "pa2".
3028 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3030 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3031 __isl_take isl_pw_aff *pa2)
3033 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3036 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3037 * where the function value of "pa1" is greater than the function value
3038 * of "pa2".
3040 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3041 __isl_take isl_pw_aff *pa2)
3043 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3046 /* Return a set containing those elements in the shared domain
3047 * of the elements of list1 and list2 where each element in list1
3048 * has the relation specified by "fn" with each element in list2.
3050 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3051 __isl_take isl_pw_aff_list *list2,
3052 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3053 __isl_take isl_pw_aff *pwaff2))
3055 int i, j;
3056 isl_ctx *ctx;
3057 isl_set *set;
3059 if (!list1 || !list2)
3060 goto error;
3062 ctx = isl_pw_aff_list_get_ctx(list1);
3063 if (list1->n < 1 || list2->n < 1)
3064 isl_die(ctx, isl_error_invalid,
3065 "list should contain at least one element", goto error);
3067 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3068 for (i = 0; i < list1->n; ++i)
3069 for (j = 0; j < list2->n; ++j) {
3070 isl_set *set_ij;
3072 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3073 isl_pw_aff_copy(list2->p[j]));
3074 set = isl_set_intersect(set, set_ij);
3077 isl_pw_aff_list_free(list1);
3078 isl_pw_aff_list_free(list2);
3079 return set;
3080 error:
3081 isl_pw_aff_list_free(list1);
3082 isl_pw_aff_list_free(list2);
3083 return NULL;
3086 /* Return a set containing those elements in the shared domain
3087 * of the elements of list1 and list2 where each element in list1
3088 * is equal to each element in list2.
3090 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3091 __isl_take isl_pw_aff_list *list2)
3093 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3096 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3097 __isl_take isl_pw_aff_list *list2)
3099 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3102 /* Return a set containing those elements in the shared domain
3103 * of the elements of list1 and list2 where each element in list1
3104 * is less than or equal to each element in list2.
3106 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3107 __isl_take isl_pw_aff_list *list2)
3109 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3112 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3113 __isl_take isl_pw_aff_list *list2)
3115 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3118 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3119 __isl_take isl_pw_aff_list *list2)
3121 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3124 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3125 __isl_take isl_pw_aff_list *list2)
3127 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3131 /* Return a set containing those elements in the shared domain
3132 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3134 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3135 __isl_take isl_pw_aff *pwaff2)
3137 isl_set *set_lt, *set_gt;
3139 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3140 isl_pw_aff_copy(pwaff2));
3141 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3142 return isl_set_union_disjoint(set_lt, set_gt);
3145 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3146 __isl_take isl_pw_aff *pwaff2)
3148 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3151 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3152 isl_int v)
3154 int i;
3156 if (isl_int_is_one(v))
3157 return pwaff;
3158 if (!isl_int_is_pos(v))
3159 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3160 "factor needs to be positive",
3161 return isl_pw_aff_free(pwaff));
3162 pwaff = isl_pw_aff_cow(pwaff);
3163 if (!pwaff)
3164 return NULL;
3165 if (pwaff->n == 0)
3166 return pwaff;
3168 for (i = 0; i < pwaff->n; ++i) {
3169 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3170 if (!pwaff->p[i].aff)
3171 return isl_pw_aff_free(pwaff);
3174 return pwaff;
3177 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3179 int i;
3181 pwaff = isl_pw_aff_cow(pwaff);
3182 if (!pwaff)
3183 return NULL;
3184 if (pwaff->n == 0)
3185 return pwaff;
3187 for (i = 0; i < pwaff->n; ++i) {
3188 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3189 if (!pwaff->p[i].aff)
3190 return isl_pw_aff_free(pwaff);
3193 return pwaff;
3196 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3198 int i;
3200 pwaff = isl_pw_aff_cow(pwaff);
3201 if (!pwaff)
3202 return NULL;
3203 if (pwaff->n == 0)
3204 return pwaff;
3206 for (i = 0; i < pwaff->n; ++i) {
3207 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3208 if (!pwaff->p[i].aff)
3209 return isl_pw_aff_free(pwaff);
3212 return pwaff;
3215 /* Assuming that "cond1" and "cond2" are disjoint,
3216 * return an affine expression that is equal to pwaff1 on cond1
3217 * and to pwaff2 on cond2.
3219 static __isl_give isl_pw_aff *isl_pw_aff_select(
3220 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3221 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3223 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3224 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3226 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3229 /* Return an affine expression that is equal to pwaff_true for elements
3230 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3231 * is zero.
3232 * That is, return cond ? pwaff_true : pwaff_false;
3234 * If "cond" involves and NaN, then we conservatively return a NaN
3235 * on its entire domain. In principle, we could consider the pieces
3236 * where it is NaN separately from those where it is not.
3238 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3239 * then only use the domain of "cond" to restrict the domain.
3241 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3242 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3244 isl_set *cond_true, *cond_false;
3245 isl_bool equal;
3247 if (!cond)
3248 goto error;
3249 if (isl_pw_aff_involves_nan(cond)) {
3250 isl_space *space = isl_pw_aff_get_domain_space(cond);
3251 isl_local_space *ls = isl_local_space_from_space(space);
3252 isl_pw_aff_free(cond);
3253 isl_pw_aff_free(pwaff_true);
3254 isl_pw_aff_free(pwaff_false);
3255 return isl_pw_aff_nan_on_domain(ls);
3258 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3259 isl_pw_aff_get_space(pwaff_false));
3260 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3261 isl_pw_aff_get_space(pwaff_true));
3262 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3263 if (equal < 0)
3264 goto error;
3265 if (equal) {
3266 isl_set *dom;
3268 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3269 isl_pw_aff_free(pwaff_false);
3270 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3273 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3274 cond_false = isl_pw_aff_zero_set(cond);
3275 return isl_pw_aff_select(cond_true, pwaff_true,
3276 cond_false, pwaff_false);
3277 error:
3278 isl_pw_aff_free(cond);
3279 isl_pw_aff_free(pwaff_true);
3280 isl_pw_aff_free(pwaff_false);
3281 return NULL;
3284 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3286 if (!aff)
3287 return isl_bool_error;
3289 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3292 /* Check whether pwaff is a piecewise constant.
3294 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3296 int i;
3298 if (!pwaff)
3299 return isl_bool_error;
3301 for (i = 0; i < pwaff->n; ++i) {
3302 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3303 if (is_cst < 0 || !is_cst)
3304 return is_cst;
3307 return isl_bool_true;
3310 /* Are all elements of "mpa" piecewise constants?
3312 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3314 int i;
3316 if (!mpa)
3317 return isl_bool_error;
3319 for (i = 0; i < mpa->n; ++i) {
3320 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3321 if (is_cst < 0 || !is_cst)
3322 return is_cst;
3325 return isl_bool_true;
3328 /* Return the product of "aff1" and "aff2".
3330 * If either of the two is NaN, then the result is NaN.
3332 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3334 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3335 __isl_take isl_aff *aff2)
3337 if (!aff1 || !aff2)
3338 goto error;
3340 if (isl_aff_is_nan(aff1)) {
3341 isl_aff_free(aff2);
3342 return aff1;
3344 if (isl_aff_is_nan(aff2)) {
3345 isl_aff_free(aff1);
3346 return aff2;
3349 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3350 return isl_aff_mul(aff2, aff1);
3352 if (!isl_aff_is_cst(aff2))
3353 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3354 "at least one affine expression should be constant",
3355 goto error);
3357 aff1 = isl_aff_cow(aff1);
3358 if (!aff1 || !aff2)
3359 goto error;
3361 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3362 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3364 isl_aff_free(aff2);
3365 return aff1;
3366 error:
3367 isl_aff_free(aff1);
3368 isl_aff_free(aff2);
3369 return NULL;
3372 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3374 * If either of the two is NaN, then the result is NaN.
3376 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3377 __isl_take isl_aff *aff2)
3379 int is_cst;
3380 int neg;
3382 if (!aff1 || !aff2)
3383 goto error;
3385 if (isl_aff_is_nan(aff1)) {
3386 isl_aff_free(aff2);
3387 return aff1;
3389 if (isl_aff_is_nan(aff2)) {
3390 isl_aff_free(aff1);
3391 return aff2;
3394 is_cst = isl_aff_is_cst(aff2);
3395 if (is_cst < 0)
3396 goto error;
3397 if (!is_cst)
3398 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3399 "second argument should be a constant", goto error);
3401 if (!aff2)
3402 goto error;
3404 neg = isl_int_is_neg(aff2->v->el[1]);
3405 if (neg) {
3406 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3407 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3410 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3411 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3413 if (neg) {
3414 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3415 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3418 isl_aff_free(aff2);
3419 return aff1;
3420 error:
3421 isl_aff_free(aff1);
3422 isl_aff_free(aff2);
3423 return NULL;
3426 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3427 __isl_take isl_pw_aff *pwaff2)
3429 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3432 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3433 __isl_take isl_pw_aff *pwaff2)
3435 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3438 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3439 __isl_take isl_pw_aff *pwaff2)
3441 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3444 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3445 __isl_take isl_pw_aff *pwaff2)
3447 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3450 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3451 __isl_take isl_pw_aff *pwaff2)
3453 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3456 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3457 __isl_take isl_pw_aff *pa2)
3459 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3462 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3464 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3465 __isl_take isl_pw_aff *pa2)
3467 int is_cst;
3469 is_cst = isl_pw_aff_is_cst(pa2);
3470 if (is_cst < 0)
3471 goto error;
3472 if (!is_cst)
3473 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3474 "second argument should be a piecewise constant",
3475 goto error);
3476 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3477 error:
3478 isl_pw_aff_free(pa1);
3479 isl_pw_aff_free(pa2);
3480 return NULL;
3483 /* Compute the quotient of the integer division of "pa1" by "pa2"
3484 * with rounding towards zero.
3485 * "pa2" is assumed to be a piecewise constant.
3487 * In particular, return
3489 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3492 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3493 __isl_take isl_pw_aff *pa2)
3495 int is_cst;
3496 isl_set *cond;
3497 isl_pw_aff *f, *c;
3499 is_cst = isl_pw_aff_is_cst(pa2);
3500 if (is_cst < 0)
3501 goto error;
3502 if (!is_cst)
3503 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3504 "second argument should be a piecewise constant",
3505 goto error);
3507 pa1 = isl_pw_aff_div(pa1, pa2);
3509 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3510 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3511 c = isl_pw_aff_ceil(pa1);
3512 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3513 error:
3514 isl_pw_aff_free(pa1);
3515 isl_pw_aff_free(pa2);
3516 return NULL;
3519 /* Compute the remainder of the integer division of "pa1" by "pa2"
3520 * with rounding towards zero.
3521 * "pa2" is assumed to be a piecewise constant.
3523 * In particular, return
3525 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3528 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3529 __isl_take isl_pw_aff *pa2)
3531 int is_cst;
3532 isl_pw_aff *res;
3534 is_cst = isl_pw_aff_is_cst(pa2);
3535 if (is_cst < 0)
3536 goto error;
3537 if (!is_cst)
3538 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3539 "second argument should be a piecewise constant",
3540 goto error);
3541 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3542 res = isl_pw_aff_mul(pa2, res);
3543 res = isl_pw_aff_sub(pa1, res);
3544 return res;
3545 error:
3546 isl_pw_aff_free(pa1);
3547 isl_pw_aff_free(pa2);
3548 return NULL;
3551 /* Does either of "pa1" or "pa2" involve any NaN2?
3553 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3554 __isl_keep isl_pw_aff *pa2)
3556 isl_bool has_nan;
3558 has_nan = isl_pw_aff_involves_nan(pa1);
3559 if (has_nan < 0 || has_nan)
3560 return has_nan;
3561 return isl_pw_aff_involves_nan(pa2);
3564 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3565 * by a NaN on their shared domain.
3567 * In principle, the result could be refined to only being NaN
3568 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3570 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3571 __isl_take isl_pw_aff *pa2)
3573 isl_local_space *ls;
3574 isl_set *dom;
3575 isl_pw_aff *pa;
3577 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3578 ls = isl_local_space_from_space(isl_set_get_space(dom));
3579 pa = isl_pw_aff_nan_on_domain(ls);
3580 pa = isl_pw_aff_intersect_domain(pa, dom);
3582 return pa;
3585 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3586 __isl_take isl_pw_aff *pwaff2)
3588 isl_set *le;
3589 isl_set *dom;
3591 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3592 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3593 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3594 isl_pw_aff_copy(pwaff2));
3595 dom = isl_set_subtract(dom, isl_set_copy(le));
3596 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3599 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3600 __isl_take isl_pw_aff *pwaff2)
3602 isl_set *ge;
3603 isl_set *dom;
3605 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3606 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3607 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3608 isl_pw_aff_copy(pwaff2));
3609 dom = isl_set_subtract(dom, isl_set_copy(ge));
3610 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3613 /* Return an expression for the minimum (if "max" is not set) or
3614 * the maximum (if "max" is set) of "pa1" and "pa2".
3615 * If either expression involves any NaN, then return a NaN
3616 * on the shared domain as result.
3618 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3619 __isl_take isl_pw_aff *pa2, int max)
3621 isl_bool has_nan;
3623 has_nan = either_involves_nan(pa1, pa2);
3624 if (has_nan < 0)
3625 pa1 = isl_pw_aff_free(pa1);
3626 else if (has_nan)
3627 return replace_by_nan(pa1, pa2);
3629 if (max)
3630 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3631 else
3632 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3635 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3637 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3638 __isl_take isl_pw_aff *pwaff2)
3640 return pw_aff_min_max(pwaff1, pwaff2, 0);
3643 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3645 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3646 __isl_take isl_pw_aff *pwaff2)
3648 return pw_aff_min_max(pwaff1, pwaff2, 1);
3651 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3652 __isl_take isl_pw_aff_list *list,
3653 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3654 __isl_take isl_pw_aff *pwaff2))
3656 int i;
3657 isl_ctx *ctx;
3658 isl_pw_aff *res;
3660 if (!list)
3661 return NULL;
3663 ctx = isl_pw_aff_list_get_ctx(list);
3664 if (list->n < 1)
3665 isl_die(ctx, isl_error_invalid,
3666 "list should contain at least one element", goto error);
3668 res = isl_pw_aff_copy(list->p[0]);
3669 for (i = 1; i < list->n; ++i)
3670 res = fn(res, isl_pw_aff_copy(list->p[i]));
3672 isl_pw_aff_list_free(list);
3673 return res;
3674 error:
3675 isl_pw_aff_list_free(list);
3676 return NULL;
3679 /* Return an isl_pw_aff that maps each element in the intersection of the
3680 * domains of the elements of list to the minimal corresponding affine
3681 * expression.
3683 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3685 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3688 /* Return an isl_pw_aff that maps each element in the intersection of the
3689 * domains of the elements of list to the maximal corresponding affine
3690 * expression.
3692 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3694 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3697 /* Mark the domains of "pwaff" as rational.
3699 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3701 int i;
3703 pwaff = isl_pw_aff_cow(pwaff);
3704 if (!pwaff)
3705 return NULL;
3706 if (pwaff->n == 0)
3707 return pwaff;
3709 for (i = 0; i < pwaff->n; ++i) {
3710 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3711 if (!pwaff->p[i].set)
3712 return isl_pw_aff_free(pwaff);
3715 return pwaff;
3718 /* Mark the domains of the elements of "list" as rational.
3720 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3721 __isl_take isl_pw_aff_list *list)
3723 int i, n;
3725 if (!list)
3726 return NULL;
3727 if (list->n == 0)
3728 return list;
3730 n = list->n;
3731 for (i = 0; i < n; ++i) {
3732 isl_pw_aff *pa;
3734 pa = isl_pw_aff_list_get_pw_aff(list, i);
3735 pa = isl_pw_aff_set_rational(pa);
3736 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3739 return list;
3742 /* Do the parameters of "aff" match those of "space"?
3744 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3745 __isl_keep isl_space *space)
3747 isl_space *aff_space;
3748 isl_bool match;
3750 if (!aff || !space)
3751 return isl_bool_error;
3753 aff_space = isl_aff_get_domain_space(aff);
3755 match = isl_space_has_equal_params(space, aff_space);
3757 isl_space_free(aff_space);
3758 return match;
3761 /* Check that the domain space of "aff" matches "space".
3763 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3764 __isl_keep isl_space *space)
3766 isl_space *aff_space;
3767 isl_bool match;
3769 if (!aff || !space)
3770 return isl_stat_error;
3772 aff_space = isl_aff_get_domain_space(aff);
3774 match = isl_space_has_equal_params(space, aff_space);
3775 if (match < 0)
3776 goto error;
3777 if (!match)
3778 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3779 "parameters don't match", goto error);
3780 match = isl_space_tuple_is_equal(space, isl_dim_in,
3781 aff_space, isl_dim_set);
3782 if (match < 0)
3783 goto error;
3784 if (!match)
3785 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3786 "domains don't match", goto error);
3787 isl_space_free(aff_space);
3788 return isl_stat_ok;
3789 error:
3790 isl_space_free(aff_space);
3791 return isl_stat_error;
3794 #undef BASE
3795 #define BASE aff
3796 #undef DOMBASE
3797 #define DOMBASE set
3798 #define NO_DOMAIN
3800 #include <isl_multi_templ.c>
3801 #include <isl_multi_apply_set.c>
3802 #include <isl_multi_cmp.c>
3803 #include <isl_multi_floor.c>
3804 #include <isl_multi_gist.c>
3806 #undef NO_DOMAIN
3808 /* Construct an isl_multi_aff living in "space" that corresponds
3809 * to the affine transformation matrix "mat".
3811 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3812 __isl_take isl_space *space, __isl_take isl_mat *mat)
3814 isl_ctx *ctx;
3815 isl_local_space *ls = NULL;
3816 isl_multi_aff *ma = NULL;
3817 int n_row, n_col, n_out, total;
3818 int i;
3820 if (!space || !mat)
3821 goto error;
3823 ctx = isl_mat_get_ctx(mat);
3825 n_row = isl_mat_rows(mat);
3826 n_col = isl_mat_cols(mat);
3827 if (n_row < 1)
3828 isl_die(ctx, isl_error_invalid,
3829 "insufficient number of rows", goto error);
3830 if (n_col < 1)
3831 isl_die(ctx, isl_error_invalid,
3832 "insufficient number of columns", goto error);
3833 n_out = isl_space_dim(space, isl_dim_out);
3834 total = isl_space_dim(space, isl_dim_all);
3835 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3836 isl_die(ctx, isl_error_invalid,
3837 "dimension mismatch", goto error);
3839 ma = isl_multi_aff_zero(isl_space_copy(space));
3840 ls = isl_local_space_from_space(isl_space_domain(space));
3842 for (i = 0; i < n_row - 1; ++i) {
3843 isl_vec *v;
3844 isl_aff *aff;
3846 v = isl_vec_alloc(ctx, 1 + n_col);
3847 if (!v)
3848 goto error;
3849 isl_int_set(v->el[0], mat->row[0][0]);
3850 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3851 v = isl_vec_normalize(v);
3852 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3853 ma = isl_multi_aff_set_aff(ma, i, aff);
3856 isl_local_space_free(ls);
3857 isl_mat_free(mat);
3858 return ma;
3859 error:
3860 isl_local_space_free(ls);
3861 isl_mat_free(mat);
3862 isl_multi_aff_free(ma);
3863 return NULL;
3866 /* Remove any internal structure of the domain of "ma".
3867 * If there is any such internal structure in the input,
3868 * then the name of the corresponding space is also removed.
3870 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3871 __isl_take isl_multi_aff *ma)
3873 isl_space *space;
3875 if (!ma)
3876 return NULL;
3878 if (!ma->space->nested[0])
3879 return ma;
3881 space = isl_multi_aff_get_space(ma);
3882 space = isl_space_flatten_domain(space);
3883 ma = isl_multi_aff_reset_space(ma, space);
3885 return ma;
3888 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3889 * of the space to its domain.
3891 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3893 int i, n_in;
3894 isl_local_space *ls;
3895 isl_multi_aff *ma;
3897 if (!space)
3898 return NULL;
3899 if (!isl_space_is_map(space))
3900 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3901 "not a map space", goto error);
3903 n_in = isl_space_dim(space, isl_dim_in);
3904 space = isl_space_domain_map(space);
3906 ma = isl_multi_aff_alloc(isl_space_copy(space));
3907 if (n_in == 0) {
3908 isl_space_free(space);
3909 return ma;
3912 space = isl_space_domain(space);
3913 ls = isl_local_space_from_space(space);
3914 for (i = 0; i < n_in; ++i) {
3915 isl_aff *aff;
3917 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3918 isl_dim_set, i);
3919 ma = isl_multi_aff_set_aff(ma, i, aff);
3921 isl_local_space_free(ls);
3922 return ma;
3923 error:
3924 isl_space_free(space);
3925 return NULL;
3928 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3929 * of the space to its range.
3931 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3933 int i, n_in, n_out;
3934 isl_local_space *ls;
3935 isl_multi_aff *ma;
3937 if (!space)
3938 return NULL;
3939 if (!isl_space_is_map(space))
3940 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3941 "not a map space", goto error);
3943 n_in = isl_space_dim(space, isl_dim_in);
3944 n_out = isl_space_dim(space, isl_dim_out);
3945 space = isl_space_range_map(space);
3947 ma = isl_multi_aff_alloc(isl_space_copy(space));
3948 if (n_out == 0) {
3949 isl_space_free(space);
3950 return ma;
3953 space = isl_space_domain(space);
3954 ls = isl_local_space_from_space(space);
3955 for (i = 0; i < n_out; ++i) {
3956 isl_aff *aff;
3958 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3959 isl_dim_set, n_in + i);
3960 ma = isl_multi_aff_set_aff(ma, i, aff);
3962 isl_local_space_free(ls);
3963 return ma;
3964 error:
3965 isl_space_free(space);
3966 return NULL;
3969 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3970 * of the space to its range.
3972 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3973 __isl_take isl_space *space)
3975 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3978 /* Given the space of a set and a range of set dimensions,
3979 * construct an isl_multi_aff that projects out those dimensions.
3981 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3982 __isl_take isl_space *space, enum isl_dim_type type,
3983 unsigned first, unsigned n)
3985 int i, dim;
3986 isl_local_space *ls;
3987 isl_multi_aff *ma;
3989 if (!space)
3990 return NULL;
3991 if (!isl_space_is_set(space))
3992 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3993 "expecting set space", goto error);
3994 if (type != isl_dim_set)
3995 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3996 "only set dimensions can be projected out", goto error);
3998 dim = isl_space_dim(space, isl_dim_set);
3999 if (first + n > dim)
4000 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4001 "range out of bounds", goto error);
4003 space = isl_space_from_domain(space);
4004 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4006 if (dim == n)
4007 return isl_multi_aff_alloc(space);
4009 ma = isl_multi_aff_alloc(isl_space_copy(space));
4010 space = isl_space_domain(space);
4011 ls = isl_local_space_from_space(space);
4013 for (i = 0; i < first; ++i) {
4014 isl_aff *aff;
4016 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4017 isl_dim_set, i);
4018 ma = isl_multi_aff_set_aff(ma, i, aff);
4021 for (i = 0; i < dim - (first + n); ++i) {
4022 isl_aff *aff;
4024 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4025 isl_dim_set, first + n + i);
4026 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4029 isl_local_space_free(ls);
4030 return ma;
4031 error:
4032 isl_space_free(space);
4033 return NULL;
4036 /* Given the space of a set and a range of set dimensions,
4037 * construct an isl_pw_multi_aff that projects out those dimensions.
4039 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4040 __isl_take isl_space *space, enum isl_dim_type type,
4041 unsigned first, unsigned n)
4043 isl_multi_aff *ma;
4045 ma = isl_multi_aff_project_out_map(space, type, first, n);
4046 return isl_pw_multi_aff_from_multi_aff(ma);
4049 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4050 * domain.
4052 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4053 __isl_take isl_multi_aff *ma)
4055 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4056 return isl_pw_multi_aff_alloc(dom, ma);
4059 /* Create a piecewise multi-affine expression in the given space that maps each
4060 * input dimension to the corresponding output dimension.
4062 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4063 __isl_take isl_space *space)
4065 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4068 /* Exploit the equalities in "eq" to simplify the affine expressions.
4070 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4071 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4073 int i;
4075 maff = isl_multi_aff_cow(maff);
4076 if (!maff || !eq)
4077 goto error;
4079 for (i = 0; i < maff->n; ++i) {
4080 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
4081 isl_basic_set_copy(eq));
4082 if (!maff->p[i])
4083 goto error;
4086 isl_basic_set_free(eq);
4087 return maff;
4088 error:
4089 isl_basic_set_free(eq);
4090 isl_multi_aff_free(maff);
4091 return NULL;
4094 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4095 isl_int f)
4097 int i;
4099 maff = isl_multi_aff_cow(maff);
4100 if (!maff)
4101 return NULL;
4103 for (i = 0; i < maff->n; ++i) {
4104 maff->p[i] = isl_aff_scale(maff->p[i], f);
4105 if (!maff->p[i])
4106 return isl_multi_aff_free(maff);
4109 return maff;
4112 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4113 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4115 maff1 = isl_multi_aff_add(maff1, maff2);
4116 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4117 return maff1;
4120 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4122 if (!maff)
4123 return -1;
4125 return 0;
4128 /* Return the set of domain elements where "ma1" is lexicographically
4129 * smaller than or equal to "ma2".
4131 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4132 __isl_take isl_multi_aff *ma2)
4134 return isl_multi_aff_lex_ge_set(ma2, ma1);
4137 /* Return the set of domain elements where "ma1" is lexicographically
4138 * smaller than "ma2".
4140 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4141 __isl_take isl_multi_aff *ma2)
4143 return isl_multi_aff_lex_gt_set(ma2, ma1);
4146 /* Return the set of domain elements where "ma1" and "ma2"
4147 * satisfy "order".
4149 static __isl_give isl_set *isl_multi_aff_order_set(
4150 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4151 __isl_give isl_map *order(__isl_take isl_space *set_space))
4153 isl_space *space;
4154 isl_map *map1, *map2;
4155 isl_map *map, *ge;
4157 map1 = isl_map_from_multi_aff(ma1);
4158 map2 = isl_map_from_multi_aff(ma2);
4159 map = isl_map_range_product(map1, map2);
4160 space = isl_space_range(isl_map_get_space(map));
4161 space = isl_space_domain(isl_space_unwrap(space));
4162 ge = order(space);
4163 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4165 return isl_map_domain(map);
4168 /* Return the set of domain elements where "ma1" is lexicographically
4169 * greater than or equal to "ma2".
4171 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4172 __isl_take isl_multi_aff *ma2)
4174 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4177 /* Return the set of domain elements where "ma1" is lexicographically
4178 * greater than "ma2".
4180 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4181 __isl_take isl_multi_aff *ma2)
4183 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4186 #undef PW
4187 #define PW isl_pw_multi_aff
4188 #undef EL
4189 #define EL isl_multi_aff
4190 #undef EL_IS_ZERO
4191 #define EL_IS_ZERO is_empty
4192 #undef ZERO
4193 #define ZERO empty
4194 #undef IS_ZERO
4195 #define IS_ZERO is_empty
4196 #undef FIELD
4197 #define FIELD maff
4198 #undef DEFAULT_IS_ZERO
4199 #define DEFAULT_IS_ZERO 0
4201 #define NO_SUB
4202 #define NO_EVAL
4203 #define NO_OPT
4204 #define NO_INVOLVES_DIMS
4205 #define NO_INSERT_DIMS
4206 #define NO_LIFT
4207 #define NO_MORPH
4209 #include <isl_pw_templ.c>
4210 #include <isl_pw_union_opt.c>
4212 #undef NO_SUB
4214 #undef UNION
4215 #define UNION isl_union_pw_multi_aff
4216 #undef PART
4217 #define PART isl_pw_multi_aff
4218 #undef PARTS
4219 #define PARTS pw_multi_aff
4221 #include <isl_union_multi.c>
4222 #include <isl_union_neg.c>
4224 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4225 __isl_take isl_pw_multi_aff *pma1,
4226 __isl_take isl_pw_multi_aff *pma2)
4228 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4229 &isl_multi_aff_lex_ge_set);
4232 /* Given two piecewise multi affine expressions, return a piecewise
4233 * multi-affine expression defined on the union of the definition domains
4234 * of the inputs that is equal to the lexicographic maximum of the two
4235 * inputs on each cell. If only one of the two inputs is defined on
4236 * a given cell, then it is considered to be the maximum.
4238 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4239 __isl_take isl_pw_multi_aff *pma1,
4240 __isl_take isl_pw_multi_aff *pma2)
4242 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4243 &pw_multi_aff_union_lexmax);
4246 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4247 __isl_take isl_pw_multi_aff *pma1,
4248 __isl_take isl_pw_multi_aff *pma2)
4250 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4251 &isl_multi_aff_lex_le_set);
4254 /* Given two piecewise multi affine expressions, return a piecewise
4255 * multi-affine expression defined on the union of the definition domains
4256 * of the inputs that is equal to the lexicographic minimum of the two
4257 * inputs on each cell. If only one of the two inputs is defined on
4258 * a given cell, then it is considered to be the minimum.
4260 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4261 __isl_take isl_pw_multi_aff *pma1,
4262 __isl_take isl_pw_multi_aff *pma2)
4264 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4265 &pw_multi_aff_union_lexmin);
4268 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4269 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4271 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4272 &isl_multi_aff_add);
4275 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4276 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4278 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4279 &pw_multi_aff_add);
4282 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4283 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4285 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4286 &isl_multi_aff_sub);
4289 /* Subtract "pma2" from "pma1" and return the result.
4291 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4292 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4294 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4295 &pw_multi_aff_sub);
4298 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4299 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4301 return isl_pw_multi_aff_union_add_(pma1, pma2);
4304 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4305 * with the actual sum on the shared domain and
4306 * the defined expression on the symmetric difference of the domains.
4308 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4309 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4311 return isl_union_pw_aff_union_add_(upa1, upa2);
4314 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4315 * with the actual sum on the shared domain and
4316 * the defined expression on the symmetric difference of the domains.
4318 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4319 __isl_take isl_union_pw_multi_aff *upma1,
4320 __isl_take isl_union_pw_multi_aff *upma2)
4322 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4325 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4326 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4328 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4329 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4331 int i, j, n;
4332 isl_space *space;
4333 isl_pw_multi_aff *res;
4335 if (!pma1 || !pma2)
4336 goto error;
4338 n = pma1->n * pma2->n;
4339 space = isl_space_product(isl_space_copy(pma1->dim),
4340 isl_space_copy(pma2->dim));
4341 res = isl_pw_multi_aff_alloc_size(space, n);
4343 for (i = 0; i < pma1->n; ++i) {
4344 for (j = 0; j < pma2->n; ++j) {
4345 isl_set *domain;
4346 isl_multi_aff *ma;
4348 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4349 isl_set_copy(pma2->p[j].set));
4350 ma = isl_multi_aff_product(
4351 isl_multi_aff_copy(pma1->p[i].maff),
4352 isl_multi_aff_copy(pma2->p[j].maff));
4353 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4357 isl_pw_multi_aff_free(pma1);
4358 isl_pw_multi_aff_free(pma2);
4359 return res;
4360 error:
4361 isl_pw_multi_aff_free(pma1);
4362 isl_pw_multi_aff_free(pma2);
4363 return NULL;
4366 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4367 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4369 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4370 &pw_multi_aff_product);
4373 /* Construct a map mapping the domain of the piecewise multi-affine expression
4374 * to its range, with each dimension in the range equated to the
4375 * corresponding affine expression on its cell.
4377 * If the domain of "pma" is rational, then so is the constructed "map".
4379 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4381 int i;
4382 isl_map *map;
4384 if (!pma)
4385 return NULL;
4387 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4389 for (i = 0; i < pma->n; ++i) {
4390 isl_bool rational;
4391 isl_multi_aff *maff;
4392 isl_basic_map *bmap;
4393 isl_map *map_i;
4395 rational = isl_set_is_rational(pma->p[i].set);
4396 if (rational < 0)
4397 map = isl_map_free(map);
4398 maff = isl_multi_aff_copy(pma->p[i].maff);
4399 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4400 map_i = isl_map_from_basic_map(bmap);
4401 map_i = isl_map_intersect_domain(map_i,
4402 isl_set_copy(pma->p[i].set));
4403 map = isl_map_union_disjoint(map, map_i);
4406 isl_pw_multi_aff_free(pma);
4407 return map;
4410 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4412 if (!pma)
4413 return NULL;
4415 if (!isl_space_is_set(pma->dim))
4416 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4417 "isl_pw_multi_aff cannot be converted into an isl_set",
4418 goto error);
4420 return isl_map_from_pw_multi_aff(pma);
4421 error:
4422 isl_pw_multi_aff_free(pma);
4423 return NULL;
4426 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4427 * denominator "denom".
4428 * "denom" is allowed to be negative, in which case the actual denominator
4429 * is -denom and the expressions are added instead.
4431 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4432 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4434 int i, first;
4435 int sign;
4436 isl_int d;
4438 first = isl_seq_first_non_zero(c, n);
4439 if (first == -1)
4440 return aff;
4442 sign = isl_int_sgn(denom);
4443 isl_int_init(d);
4444 isl_int_abs(d, denom);
4445 for (i = first; i < n; ++i) {
4446 isl_aff *aff_i;
4448 if (isl_int_is_zero(c[i]))
4449 continue;
4450 aff_i = isl_multi_aff_get_aff(ma, i);
4451 aff_i = isl_aff_scale(aff_i, c[i]);
4452 aff_i = isl_aff_scale_down(aff_i, d);
4453 if (sign >= 0)
4454 aff = isl_aff_sub(aff, aff_i);
4455 else
4456 aff = isl_aff_add(aff, aff_i);
4458 isl_int_clear(d);
4460 return aff;
4463 /* Extract an affine expression that expresses the output dimension "pos"
4464 * of "bmap" in terms of the parameters and input dimensions from
4465 * equality "eq".
4466 * Note that this expression may involve integer divisions defined
4467 * in terms of parameters and input dimensions.
4468 * The equality may also involve references to earlier (but not later)
4469 * output dimensions. These are replaced by the corresponding elements
4470 * in "ma".
4472 * If the equality is of the form
4474 * f(i) + h(j) + a x + g(i) = 0,
4476 * with f(i) a linear combinations of the parameters and input dimensions,
4477 * g(i) a linear combination of integer divisions defined in terms of the same
4478 * and h(j) a linear combinations of earlier output dimensions,
4479 * then the affine expression is
4481 * (-f(i) - g(i))/a - h(j)/a
4483 * If the equality is of the form
4485 * f(i) + h(j) - a x + g(i) = 0,
4487 * then the affine expression is
4489 * (f(i) + g(i))/a - h(j)/(-a)
4492 * If "div" refers to an integer division (i.e., it is smaller than
4493 * the number of integer divisions), then the equality constraint
4494 * does involve an integer division (the one at position "div") that
4495 * is defined in terms of output dimensions. However, this integer
4496 * division can be eliminated by exploiting a pair of constraints
4497 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4498 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4499 * -l + x >= 0.
4500 * In particular, let
4502 * x = e(i) + m floor(...)
4504 * with e(i) the expression derived above and floor(...) the integer
4505 * division involving output dimensions.
4506 * From
4508 * l <= x <= l + n,
4510 * we have
4512 * 0 <= x - l <= n
4514 * This means
4516 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4517 * = (e(i) - l) mod m
4519 * Therefore,
4521 * x - l = (e(i) - l) mod m
4523 * or
4525 * x = ((e(i) - l) mod m) + l
4527 * The variable "shift" below contains the expression -l, which may
4528 * also involve a linear combination of earlier output dimensions.
4530 static __isl_give isl_aff *extract_aff_from_equality(
4531 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4532 __isl_keep isl_multi_aff *ma)
4534 unsigned o_out;
4535 unsigned n_div, n_out;
4536 isl_ctx *ctx;
4537 isl_local_space *ls;
4538 isl_aff *aff, *shift;
4539 isl_val *mod;
4541 ctx = isl_basic_map_get_ctx(bmap);
4542 ls = isl_basic_map_get_local_space(bmap);
4543 ls = isl_local_space_domain(ls);
4544 aff = isl_aff_alloc(isl_local_space_copy(ls));
4545 if (!aff)
4546 goto error;
4547 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4548 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4549 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4550 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4551 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4552 isl_seq_cpy(aff->v->el + 1 + o_out,
4553 bmap->eq[eq] + o_out + n_out, n_div);
4554 } else {
4555 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4556 isl_seq_neg(aff->v->el + 1 + o_out,
4557 bmap->eq[eq] + o_out + n_out, n_div);
4559 if (div < n_div)
4560 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4561 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4562 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4563 bmap->eq[eq][o_out + pos]);
4564 if (div < n_div) {
4565 shift = isl_aff_alloc(isl_local_space_copy(ls));
4566 if (!shift)
4567 goto error;
4568 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4569 isl_seq_cpy(shift->v->el + 1 + o_out,
4570 bmap->ineq[ineq] + o_out + n_out, n_div);
4571 isl_int_set_si(shift->v->el[0], 1);
4572 shift = subtract_initial(shift, ma, pos,
4573 bmap->ineq[ineq] + o_out, ctx->negone);
4574 aff = isl_aff_add(aff, isl_aff_copy(shift));
4575 mod = isl_val_int_from_isl_int(ctx,
4576 bmap->eq[eq][o_out + n_out + div]);
4577 mod = isl_val_abs(mod);
4578 aff = isl_aff_mod_val(aff, mod);
4579 aff = isl_aff_sub(aff, shift);
4582 isl_local_space_free(ls);
4583 return aff;
4584 error:
4585 isl_local_space_free(ls);
4586 isl_aff_free(aff);
4587 return NULL;
4590 /* Given a basic map with output dimensions defined
4591 * in terms of the parameters input dimensions and earlier
4592 * output dimensions using an equality (and possibly a pair on inequalities),
4593 * extract an isl_aff that expresses output dimension "pos" in terms
4594 * of the parameters and input dimensions.
4595 * Note that this expression may involve integer divisions defined
4596 * in terms of parameters and input dimensions.
4597 * "ma" contains the expressions corresponding to earlier output dimensions.
4599 * This function shares some similarities with
4600 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4602 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4603 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4605 int eq, div, ineq;
4606 isl_aff *aff;
4608 if (!bmap)
4609 return NULL;
4610 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4611 if (eq >= bmap->n_eq)
4612 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4613 "unable to find suitable equality", return NULL);
4614 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4616 aff = isl_aff_remove_unused_divs(aff);
4617 return aff;
4620 /* Given a basic map where each output dimension is defined
4621 * in terms of the parameters and input dimensions using an equality,
4622 * extract an isl_multi_aff that expresses the output dimensions in terms
4623 * of the parameters and input dimensions.
4625 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4626 __isl_take isl_basic_map *bmap)
4628 int i;
4629 unsigned n_out;
4630 isl_multi_aff *ma;
4632 if (!bmap)
4633 return NULL;
4635 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4636 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4638 for (i = 0; i < n_out; ++i) {
4639 isl_aff *aff;
4641 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4642 ma = isl_multi_aff_set_aff(ma, i, aff);
4645 isl_basic_map_free(bmap);
4647 return ma;
4650 /* Given a basic set where each set dimension is defined
4651 * in terms of the parameters using an equality,
4652 * extract an isl_multi_aff that expresses the set dimensions in terms
4653 * of the parameters.
4655 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4656 __isl_take isl_basic_set *bset)
4658 return extract_isl_multi_aff_from_basic_map(bset);
4661 /* Create an isl_pw_multi_aff that is equivalent to
4662 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4663 * The given basic map is such that each output dimension is defined
4664 * in terms of the parameters and input dimensions using an equality.
4666 * Since some applications expect the result of isl_pw_multi_aff_from_map
4667 * to only contain integer affine expressions, we compute the floor
4668 * of the expression before returning.
4670 * Remove all constraints involving local variables without
4671 * an explicit representation (resulting in the removal of those
4672 * local variables) prior to the actual extraction to ensure
4673 * that the local spaces in which the resulting affine expressions
4674 * are created do not contain any unknown local variables.
4675 * Removing such constraints is safe because constraints involving
4676 * unknown local variables are not used to determine whether
4677 * a basic map is obviously single-valued.
4679 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4680 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4682 isl_multi_aff *ma;
4684 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4685 ma = extract_isl_multi_aff_from_basic_map(bmap);
4686 ma = isl_multi_aff_floor(ma);
4687 return isl_pw_multi_aff_alloc(domain, ma);
4690 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4691 * This obviously only works if the input "map" is single-valued.
4692 * If so, we compute the lexicographic minimum of the image in the form
4693 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4694 * to its lexicographic minimum.
4695 * If the input is not single-valued, we produce an error.
4697 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4698 __isl_take isl_map *map)
4700 int i;
4701 int sv;
4702 isl_pw_multi_aff *pma;
4704 sv = isl_map_is_single_valued(map);
4705 if (sv < 0)
4706 goto error;
4707 if (!sv)
4708 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4709 "map is not single-valued", goto error);
4710 map = isl_map_make_disjoint(map);
4711 if (!map)
4712 return NULL;
4714 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4716 for (i = 0; i < map->n; ++i) {
4717 isl_pw_multi_aff *pma_i;
4718 isl_basic_map *bmap;
4719 bmap = isl_basic_map_copy(map->p[i]);
4720 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4721 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4724 isl_map_free(map);
4725 return pma;
4726 error:
4727 isl_map_free(map);
4728 return NULL;
4731 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4732 * taking into account that the output dimension at position "d"
4733 * can be represented as
4735 * x = floor((e(...) + c1) / m)
4737 * given that constraint "i" is of the form
4739 * e(...) + c1 - m x >= 0
4742 * Let "map" be of the form
4744 * A -> B
4746 * We construct a mapping
4748 * A -> [A -> x = floor(...)]
4750 * apply that to the map, obtaining
4752 * [A -> x = floor(...)] -> B
4754 * and equate dimension "d" to x.
4755 * We then compute a isl_pw_multi_aff representation of the resulting map
4756 * and plug in the mapping above.
4758 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4759 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4761 isl_ctx *ctx;
4762 isl_space *space;
4763 isl_local_space *ls;
4764 isl_multi_aff *ma;
4765 isl_aff *aff;
4766 isl_vec *v;
4767 isl_map *insert;
4768 int offset;
4769 int n;
4770 int n_in;
4771 isl_pw_multi_aff *pma;
4772 isl_bool is_set;
4774 is_set = isl_map_is_set(map);
4775 if (is_set < 0)
4776 goto error;
4778 offset = isl_basic_map_offset(hull, isl_dim_out);
4779 ctx = isl_map_get_ctx(map);
4780 space = isl_space_domain(isl_map_get_space(map));
4781 n_in = isl_space_dim(space, isl_dim_set);
4782 n = isl_space_dim(space, isl_dim_all);
4784 v = isl_vec_alloc(ctx, 1 + 1 + n);
4785 if (v) {
4786 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4787 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4789 isl_basic_map_free(hull);
4791 ls = isl_local_space_from_space(isl_space_copy(space));
4792 aff = isl_aff_alloc_vec(ls, v);
4793 aff = isl_aff_floor(aff);
4794 if (is_set) {
4795 isl_space_free(space);
4796 ma = isl_multi_aff_from_aff(aff);
4797 } else {
4798 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4799 ma = isl_multi_aff_range_product(ma,
4800 isl_multi_aff_from_aff(aff));
4803 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4804 map = isl_map_apply_domain(map, insert);
4805 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4806 pma = isl_pw_multi_aff_from_map(map);
4807 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4809 return pma;
4810 error:
4811 isl_map_free(map);
4812 isl_basic_map_free(hull);
4813 return NULL;
4816 /* Is constraint "c" of the form
4818 * e(...) + c1 - m x >= 0
4820 * or
4822 * -e(...) + c2 + m x >= 0
4824 * where m > 1 and e only depends on parameters and input dimemnsions?
4826 * "offset" is the offset of the output dimensions
4827 * "pos" is the position of output dimension x.
4829 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4831 if (isl_int_is_zero(c[offset + d]))
4832 return 0;
4833 if (isl_int_is_one(c[offset + d]))
4834 return 0;
4835 if (isl_int_is_negone(c[offset + d]))
4836 return 0;
4837 if (isl_seq_first_non_zero(c + offset, d) != -1)
4838 return 0;
4839 if (isl_seq_first_non_zero(c + offset + d + 1,
4840 total - (offset + d + 1)) != -1)
4841 return 0;
4842 return 1;
4845 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4847 * As a special case, we first check if there is any pair of constraints,
4848 * shared by all the basic maps in "map" that force a given dimension
4849 * to be equal to the floor of some affine combination of the input dimensions.
4851 * In particular, if we can find two constraints
4853 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4855 * and
4857 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4859 * where m > 1 and e only depends on parameters and input dimemnsions,
4860 * and such that
4862 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4864 * then we know that we can take
4866 * x = floor((e(...) + c1) / m)
4868 * without having to perform any computation.
4870 * Note that we know that
4872 * c1 + c2 >= 1
4874 * If c1 + c2 were 0, then we would have detected an equality during
4875 * simplification. If c1 + c2 were negative, then we would have detected
4876 * a contradiction.
4878 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4879 __isl_take isl_map *map)
4881 int d, dim;
4882 int i, j, n;
4883 int offset, total;
4884 isl_int sum;
4885 isl_basic_map *hull;
4887 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4888 if (!hull)
4889 goto error;
4891 isl_int_init(sum);
4892 dim = isl_map_dim(map, isl_dim_out);
4893 offset = isl_basic_map_offset(hull, isl_dim_out);
4894 total = 1 + isl_basic_map_total_dim(hull);
4895 n = hull->n_ineq;
4896 for (d = 0; d < dim; ++d) {
4897 for (i = 0; i < n; ++i) {
4898 if (!is_potential_div_constraint(hull->ineq[i],
4899 offset, d, total))
4900 continue;
4901 for (j = i + 1; j < n; ++j) {
4902 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4903 hull->ineq[j] + 1, total - 1))
4904 continue;
4905 isl_int_add(sum, hull->ineq[i][0],
4906 hull->ineq[j][0]);
4907 if (isl_int_abs_lt(sum,
4908 hull->ineq[i][offset + d]))
4909 break;
4912 if (j >= n)
4913 continue;
4914 isl_int_clear(sum);
4915 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4916 j = i;
4917 return pw_multi_aff_from_map_div(map, hull, d, j);
4920 isl_int_clear(sum);
4921 isl_basic_map_free(hull);
4922 return pw_multi_aff_from_map_base(map);
4923 error:
4924 isl_map_free(map);
4925 isl_basic_map_free(hull);
4926 return NULL;
4929 /* Given an affine expression
4931 * [A -> B] -> f(A,B)
4933 * construct an isl_multi_aff
4935 * [A -> B] -> B'
4937 * such that dimension "d" in B' is set to "aff" and the remaining
4938 * dimensions are set equal to the corresponding dimensions in B.
4939 * "n_in" is the dimension of the space A.
4940 * "n_out" is the dimension of the space B.
4942 * If "is_set" is set, then the affine expression is of the form
4944 * [B] -> f(B)
4946 * and we construct an isl_multi_aff
4948 * B -> B'
4950 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4951 unsigned n_in, unsigned n_out, int is_set)
4953 int i;
4954 isl_multi_aff *ma;
4955 isl_space *space, *space2;
4956 isl_local_space *ls;
4958 space = isl_aff_get_domain_space(aff);
4959 ls = isl_local_space_from_space(isl_space_copy(space));
4960 space2 = isl_space_copy(space);
4961 if (!is_set)
4962 space2 = isl_space_range(isl_space_unwrap(space2));
4963 space = isl_space_map_from_domain_and_range(space, space2);
4964 ma = isl_multi_aff_alloc(space);
4965 ma = isl_multi_aff_set_aff(ma, d, aff);
4967 for (i = 0; i < n_out; ++i) {
4968 if (i == d)
4969 continue;
4970 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4971 isl_dim_set, n_in + i);
4972 ma = isl_multi_aff_set_aff(ma, i, aff);
4975 isl_local_space_free(ls);
4977 return ma;
4980 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4981 * taking into account that the dimension at position "d" can be written as
4983 * x = m a + f(..) (1)
4985 * where m is equal to "gcd".
4986 * "i" is the index of the equality in "hull" that defines f(..).
4987 * In particular, the equality is of the form
4989 * f(..) - x + m g(existentials) = 0
4991 * or
4993 * -f(..) + x + m g(existentials) = 0
4995 * We basically plug (1) into "map", resulting in a map with "a"
4996 * in the range instead of "x". The corresponding isl_pw_multi_aff
4997 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4999 * Specifically, given the input map
5001 * A -> B
5003 * We first wrap it into a set
5005 * [A -> B]
5007 * and define (1) on top of the corresponding space, resulting in "aff".
5008 * We use this to create an isl_multi_aff that maps the output position "d"
5009 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5010 * We plug this into the wrapped map, unwrap the result and compute the
5011 * corresponding isl_pw_multi_aff.
5012 * The result is an expression
5014 * A -> T(A)
5016 * We adjust that to
5018 * A -> [A -> T(A)]
5020 * so that we can plug that into "aff", after extending the latter to
5021 * a mapping
5023 * [A -> B] -> B'
5026 * If "map" is actually a set, then there is no "A" space, meaning
5027 * that we do not need to perform any wrapping, and that the result
5028 * of the recursive call is of the form
5030 * [T]
5032 * which is plugged into a mapping of the form
5034 * B -> B'
5036 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5037 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5038 isl_int gcd)
5040 isl_set *set;
5041 isl_space *space;
5042 isl_local_space *ls;
5043 isl_aff *aff;
5044 isl_multi_aff *ma;
5045 isl_pw_multi_aff *pma, *id;
5046 unsigned n_in;
5047 unsigned o_out;
5048 unsigned n_out;
5049 isl_bool is_set;
5051 is_set = isl_map_is_set(map);
5052 if (is_set < 0)
5053 goto error;
5055 n_in = isl_basic_map_dim(hull, isl_dim_in);
5056 n_out = isl_basic_map_dim(hull, isl_dim_out);
5057 o_out = isl_basic_map_offset(hull, isl_dim_out);
5059 if (is_set)
5060 set = map;
5061 else
5062 set = isl_map_wrap(map);
5063 space = isl_space_map_from_set(isl_set_get_space(set));
5064 ma = isl_multi_aff_identity(space);
5065 ls = isl_local_space_from_space(isl_set_get_space(set));
5066 aff = isl_aff_alloc(ls);
5067 if (aff) {
5068 isl_int_set_si(aff->v->el[0], 1);
5069 if (isl_int_is_one(hull->eq[i][o_out + d]))
5070 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5071 aff->v->size - 1);
5072 else
5073 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5074 aff->v->size - 1);
5075 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5077 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5078 set = isl_set_preimage_multi_aff(set, ma);
5080 ma = range_map(aff, d, n_in, n_out, is_set);
5082 if (is_set)
5083 map = set;
5084 else
5085 map = isl_set_unwrap(set);
5086 pma = isl_pw_multi_aff_from_map(map);
5088 if (!is_set) {
5089 space = isl_pw_multi_aff_get_domain_space(pma);
5090 space = isl_space_map_from_set(space);
5091 id = isl_pw_multi_aff_identity(space);
5092 pma = isl_pw_multi_aff_range_product(id, pma);
5094 id = isl_pw_multi_aff_from_multi_aff(ma);
5095 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5097 isl_basic_map_free(hull);
5098 return pma;
5099 error:
5100 isl_map_free(map);
5101 isl_basic_map_free(hull);
5102 return NULL;
5105 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5106 * "hull" contains the equalities valid for "map".
5108 * Check if any of the output dimensions is "strided".
5109 * That is, we check if it can be written as
5111 * x = m a + f(..)
5113 * with m greater than 1, a some combination of existentially quantified
5114 * variables and f an expression in the parameters and input dimensions.
5115 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5117 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5118 * special case.
5120 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5121 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5123 int i, j;
5124 unsigned n_out;
5125 unsigned o_out;
5126 unsigned n_div;
5127 unsigned o_div;
5128 isl_int gcd;
5130 n_div = isl_basic_map_dim(hull, isl_dim_div);
5131 o_div = isl_basic_map_offset(hull, isl_dim_div);
5133 if (n_div == 0) {
5134 isl_basic_map_free(hull);
5135 return pw_multi_aff_from_map_check_div(map);
5138 isl_int_init(gcd);
5140 n_out = isl_basic_map_dim(hull, isl_dim_out);
5141 o_out = isl_basic_map_offset(hull, isl_dim_out);
5143 for (i = 0; i < n_out; ++i) {
5144 for (j = 0; j < hull->n_eq; ++j) {
5145 isl_int *eq = hull->eq[j];
5146 isl_pw_multi_aff *res;
5148 if (!isl_int_is_one(eq[o_out + i]) &&
5149 !isl_int_is_negone(eq[o_out + i]))
5150 continue;
5151 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5152 continue;
5153 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5154 n_out - (i + 1)) != -1)
5155 continue;
5156 isl_seq_gcd(eq + o_div, n_div, &gcd);
5157 if (isl_int_is_zero(gcd))
5158 continue;
5159 if (isl_int_is_one(gcd))
5160 continue;
5162 res = pw_multi_aff_from_map_stride(map, hull,
5163 i, j, gcd);
5164 isl_int_clear(gcd);
5165 return res;
5169 isl_int_clear(gcd);
5170 isl_basic_map_free(hull);
5171 return pw_multi_aff_from_map_check_div(map);
5174 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5176 * As a special case, we first check if all output dimensions are uniquely
5177 * defined in terms of the parameters and input dimensions over the entire
5178 * domain. If so, we extract the desired isl_pw_multi_aff directly
5179 * from the affine hull of "map" and its domain.
5181 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5182 * special cases.
5184 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5186 isl_bool sv;
5187 isl_basic_map *hull;
5189 if (!map)
5190 return NULL;
5192 if (isl_map_n_basic_map(map) == 1) {
5193 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5194 hull = isl_basic_map_plain_affine_hull(hull);
5195 sv = isl_basic_map_plain_is_single_valued(hull);
5196 if (sv >= 0 && sv)
5197 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5198 hull);
5199 isl_basic_map_free(hull);
5201 map = isl_map_detect_equalities(map);
5202 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5203 sv = isl_basic_map_plain_is_single_valued(hull);
5204 if (sv >= 0 && sv)
5205 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5206 if (sv >= 0)
5207 return pw_multi_aff_from_map_check_strides(map, hull);
5208 isl_basic_map_free(hull);
5209 isl_map_free(map);
5210 return NULL;
5213 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5215 return isl_pw_multi_aff_from_map(set);
5218 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5219 * add it to *user.
5221 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5223 isl_union_pw_multi_aff **upma = user;
5224 isl_pw_multi_aff *pma;
5226 pma = isl_pw_multi_aff_from_map(map);
5227 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5229 return *upma ? isl_stat_ok : isl_stat_error;
5232 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5233 * domain.
5235 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5236 __isl_take isl_aff *aff)
5238 isl_multi_aff *ma;
5239 isl_pw_multi_aff *pma;
5241 ma = isl_multi_aff_from_aff(aff);
5242 pma = isl_pw_multi_aff_from_multi_aff(ma);
5243 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5246 /* Try and create an isl_union_pw_multi_aff that is equivalent
5247 * to the given isl_union_map.
5248 * The isl_union_map is required to be single-valued in each space.
5249 * Otherwise, an error is produced.
5251 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5252 __isl_take isl_union_map *umap)
5254 isl_space *space;
5255 isl_union_pw_multi_aff *upma;
5257 space = isl_union_map_get_space(umap);
5258 upma = isl_union_pw_multi_aff_empty(space);
5259 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5260 upma = isl_union_pw_multi_aff_free(upma);
5261 isl_union_map_free(umap);
5263 return upma;
5266 /* Try and create an isl_union_pw_multi_aff that is equivalent
5267 * to the given isl_union_set.
5268 * The isl_union_set is required to be a singleton in each space.
5269 * Otherwise, an error is produced.
5271 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5272 __isl_take isl_union_set *uset)
5274 return isl_union_pw_multi_aff_from_union_map(uset);
5277 /* Return the piecewise affine expression "set ? 1 : 0".
5279 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5281 isl_pw_aff *pa;
5282 isl_space *space = isl_set_get_space(set);
5283 isl_local_space *ls = isl_local_space_from_space(space);
5284 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5285 isl_aff *one = isl_aff_zero_on_domain(ls);
5287 one = isl_aff_add_constant_si(one, 1);
5288 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5289 set = isl_set_complement(set);
5290 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5292 return pa;
5295 /* Plug in "subs" for dimension "type", "pos" of "aff".
5297 * Let i be the dimension to replace and let "subs" be of the form
5299 * f/d
5301 * and "aff" of the form
5303 * (a i + g)/m
5305 * The result is
5307 * (a f + d g')/(m d)
5309 * where g' is the result of plugging in "subs" in each of the integer
5310 * divisions in g.
5312 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5313 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5315 isl_ctx *ctx;
5316 isl_int v;
5318 aff = isl_aff_cow(aff);
5319 if (!aff || !subs)
5320 return isl_aff_free(aff);
5322 ctx = isl_aff_get_ctx(aff);
5323 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5324 isl_die(ctx, isl_error_invalid,
5325 "spaces don't match", return isl_aff_free(aff));
5326 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5327 isl_die(ctx, isl_error_unsupported,
5328 "cannot handle divs yet", return isl_aff_free(aff));
5330 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5331 if (!aff->ls)
5332 return isl_aff_free(aff);
5334 aff->v = isl_vec_cow(aff->v);
5335 if (!aff->v)
5336 return isl_aff_free(aff);
5338 pos += isl_local_space_offset(aff->ls, type);
5340 isl_int_init(v);
5341 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5342 aff->v->size, subs->v->size, v);
5343 isl_int_clear(v);
5345 return aff;
5348 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5349 * expressions in "maff".
5351 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5352 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5353 __isl_keep isl_aff *subs)
5355 int i;
5357 maff = isl_multi_aff_cow(maff);
5358 if (!maff || !subs)
5359 return isl_multi_aff_free(maff);
5361 if (type == isl_dim_in)
5362 type = isl_dim_set;
5364 for (i = 0; i < maff->n; ++i) {
5365 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5366 if (!maff->p[i])
5367 return isl_multi_aff_free(maff);
5370 return maff;
5373 /* Plug in "subs" for dimension "type", "pos" of "pma".
5375 * pma is of the form
5377 * A_i(v) -> M_i(v)
5379 * while subs is of the form
5381 * v' = B_j(v) -> S_j
5383 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5384 * has a contribution in the result, in particular
5386 * C_ij(S_j) -> M_i(S_j)
5388 * Note that plugging in S_j in C_ij may also result in an empty set
5389 * and this contribution should simply be discarded.
5391 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5392 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5393 __isl_keep isl_pw_aff *subs)
5395 int i, j, n;
5396 isl_pw_multi_aff *res;
5398 if (!pma || !subs)
5399 return isl_pw_multi_aff_free(pma);
5401 n = pma->n * subs->n;
5402 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5404 for (i = 0; i < pma->n; ++i) {
5405 for (j = 0; j < subs->n; ++j) {
5406 isl_set *common;
5407 isl_multi_aff *res_ij;
5408 int empty;
5410 common = isl_set_intersect(
5411 isl_set_copy(pma->p[i].set),
5412 isl_set_copy(subs->p[j].set));
5413 common = isl_set_substitute(common,
5414 type, pos, subs->p[j].aff);
5415 empty = isl_set_plain_is_empty(common);
5416 if (empty < 0 || empty) {
5417 isl_set_free(common);
5418 if (empty < 0)
5419 goto error;
5420 continue;
5423 res_ij = isl_multi_aff_substitute(
5424 isl_multi_aff_copy(pma->p[i].maff),
5425 type, pos, subs->p[j].aff);
5427 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5431 isl_pw_multi_aff_free(pma);
5432 return res;
5433 error:
5434 isl_pw_multi_aff_free(pma);
5435 isl_pw_multi_aff_free(res);
5436 return NULL;
5439 /* Compute the preimage of a range of dimensions in the affine expression "src"
5440 * under "ma" and put the result in "dst". The number of dimensions in "src"
5441 * that precede the range is given by "n_before". The number of dimensions
5442 * in the range is given by the number of output dimensions of "ma".
5443 * The number of dimensions that follow the range is given by "n_after".
5444 * If "has_denom" is set (to one),
5445 * then "src" and "dst" have an extra initial denominator.
5446 * "n_div_ma" is the number of existentials in "ma"
5447 * "n_div_bset" is the number of existentials in "src"
5448 * The resulting "dst" (which is assumed to have been allocated by
5449 * the caller) contains coefficients for both sets of existentials,
5450 * first those in "ma" and then those in "src".
5451 * f, c1, c2 and g are temporary objects that have been initialized
5452 * by the caller.
5454 * Let src represent the expression
5456 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5458 * and let ma represent the expressions
5460 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5462 * We start out with the following expression for dst:
5464 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5466 * with the multiplication factor f initially equal to 1
5467 * and f \sum_i b_i v_i kept separately.
5468 * For each x_i that we substitute, we multiply the numerator
5469 * (and denominator) of dst by c_1 = m_i and add the numerator
5470 * of the x_i expression multiplied by c_2 = f b_i,
5471 * after removing the common factors of c_1 and c_2.
5472 * The multiplication factor f also needs to be multiplied by c_1
5473 * for the next x_j, j > i.
5475 void isl_seq_preimage(isl_int *dst, isl_int *src,
5476 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5477 int n_div_ma, int n_div_bmap,
5478 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5480 int i;
5481 int n_param, n_in, n_out;
5482 int o_dst, o_src;
5484 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5485 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5486 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5488 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5489 o_dst = o_src = has_denom + 1 + n_param + n_before;
5490 isl_seq_clr(dst + o_dst, n_in);
5491 o_dst += n_in;
5492 o_src += n_out;
5493 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5494 o_dst += n_after;
5495 o_src += n_after;
5496 isl_seq_clr(dst + o_dst, n_div_ma);
5497 o_dst += n_div_ma;
5498 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5500 isl_int_set_si(f, 1);
5502 for (i = 0; i < n_out; ++i) {
5503 int offset = has_denom + 1 + n_param + n_before + i;
5505 if (isl_int_is_zero(src[offset]))
5506 continue;
5507 isl_int_set(c1, ma->p[i]->v->el[0]);
5508 isl_int_mul(c2, f, src[offset]);
5509 isl_int_gcd(g, c1, c2);
5510 isl_int_divexact(c1, c1, g);
5511 isl_int_divexact(c2, c2, g);
5513 isl_int_mul(f, f, c1);
5514 o_dst = has_denom;
5515 o_src = 1;
5516 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5517 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5518 o_dst += 1 + n_param;
5519 o_src += 1 + n_param;
5520 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5521 o_dst += n_before;
5522 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5523 c2, ma->p[i]->v->el + o_src, n_in);
5524 o_dst += n_in;
5525 o_src += n_in;
5526 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5527 o_dst += n_after;
5528 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5529 c2, ma->p[i]->v->el + o_src, n_div_ma);
5530 o_dst += n_div_ma;
5531 o_src += n_div_ma;
5532 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5533 if (has_denom)
5534 isl_int_mul(dst[0], dst[0], c1);
5538 /* Compute the pullback of "aff" by the function represented by "ma".
5539 * In other words, plug in "ma" in "aff". The result is an affine expression
5540 * defined over the domain space of "ma".
5542 * If "aff" is represented by
5544 * (a(p) + b x + c(divs))/d
5546 * and ma is represented by
5548 * x = D(p) + F(y) + G(divs')
5550 * then the result is
5552 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5554 * The divs in the local space of the input are similarly adjusted
5555 * through a call to isl_local_space_preimage_multi_aff.
5557 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5558 __isl_take isl_multi_aff *ma)
5560 isl_aff *res = NULL;
5561 isl_local_space *ls;
5562 int n_div_aff, n_div_ma;
5563 isl_int f, c1, c2, g;
5565 ma = isl_multi_aff_align_divs(ma);
5566 if (!aff || !ma)
5567 goto error;
5569 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5570 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5572 ls = isl_aff_get_domain_local_space(aff);
5573 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5574 res = isl_aff_alloc(ls);
5575 if (!res)
5576 goto error;
5578 isl_int_init(f);
5579 isl_int_init(c1);
5580 isl_int_init(c2);
5581 isl_int_init(g);
5583 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5584 f, c1, c2, g, 1);
5586 isl_int_clear(f);
5587 isl_int_clear(c1);
5588 isl_int_clear(c2);
5589 isl_int_clear(g);
5591 isl_aff_free(aff);
5592 isl_multi_aff_free(ma);
5593 res = isl_aff_normalize(res);
5594 return res;
5595 error:
5596 isl_aff_free(aff);
5597 isl_multi_aff_free(ma);
5598 isl_aff_free(res);
5599 return NULL;
5602 /* Compute the pullback of "aff1" by the function represented by "aff2".
5603 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5604 * defined over the domain space of "aff1".
5606 * The domain of "aff1" should match the range of "aff2", which means
5607 * that it should be single-dimensional.
5609 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5610 __isl_take isl_aff *aff2)
5612 isl_multi_aff *ma;
5614 ma = isl_multi_aff_from_aff(aff2);
5615 return isl_aff_pullback_multi_aff(aff1, ma);
5618 /* Compute the pullback of "ma1" by the function represented by "ma2".
5619 * In other words, plug in "ma2" in "ma1".
5621 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5623 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5624 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5626 int i;
5627 isl_space *space = NULL;
5629 ma2 = isl_multi_aff_align_divs(ma2);
5630 ma1 = isl_multi_aff_cow(ma1);
5631 if (!ma1 || !ma2)
5632 goto error;
5634 space = isl_space_join(isl_multi_aff_get_space(ma2),
5635 isl_multi_aff_get_space(ma1));
5637 for (i = 0; i < ma1->n; ++i) {
5638 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5639 isl_multi_aff_copy(ma2));
5640 if (!ma1->p[i])
5641 goto error;
5644 ma1 = isl_multi_aff_reset_space(ma1, space);
5645 isl_multi_aff_free(ma2);
5646 return ma1;
5647 error:
5648 isl_space_free(space);
5649 isl_multi_aff_free(ma2);
5650 isl_multi_aff_free(ma1);
5651 return NULL;
5654 /* Compute the pullback of "ma1" by the function represented by "ma2".
5655 * In other words, plug in "ma2" in "ma1".
5657 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5658 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5660 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5661 &isl_multi_aff_pullback_multi_aff_aligned);
5664 /* Extend the local space of "dst" to include the divs
5665 * in the local space of "src".
5667 * If "src" does not have any divs or if the local spaces of "dst" and
5668 * "src" are the same, then no extension is required.
5670 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5671 __isl_keep isl_aff *src)
5673 isl_ctx *ctx;
5674 int src_n_div, dst_n_div;
5675 int *exp1 = NULL;
5676 int *exp2 = NULL;
5677 isl_bool equal;
5678 isl_mat *div;
5680 if (!src || !dst)
5681 return isl_aff_free(dst);
5683 ctx = isl_aff_get_ctx(src);
5684 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5685 if (equal < 0)
5686 return isl_aff_free(dst);
5687 if (!equal)
5688 isl_die(ctx, isl_error_invalid,
5689 "spaces don't match", goto error);
5691 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5692 if (src_n_div == 0)
5693 return dst;
5694 equal = isl_local_space_is_equal(src->ls, dst->ls);
5695 if (equal < 0)
5696 return isl_aff_free(dst);
5697 if (equal)
5698 return dst;
5700 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5701 exp1 = isl_alloc_array(ctx, int, src_n_div);
5702 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5703 if (!exp1 || (dst_n_div && !exp2))
5704 goto error;
5706 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5707 dst = isl_aff_expand_divs(dst, div, exp2);
5708 free(exp1);
5709 free(exp2);
5711 return dst;
5712 error:
5713 free(exp1);
5714 free(exp2);
5715 return isl_aff_free(dst);
5718 /* Adjust the local spaces of the affine expressions in "maff"
5719 * such that they all have the save divs.
5721 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5722 __isl_take isl_multi_aff *maff)
5724 int i;
5726 if (!maff)
5727 return NULL;
5728 if (maff->n == 0)
5729 return maff;
5730 maff = isl_multi_aff_cow(maff);
5731 if (!maff)
5732 return NULL;
5734 for (i = 1; i < maff->n; ++i)
5735 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5736 for (i = 1; i < maff->n; ++i) {
5737 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5738 if (!maff->p[i])
5739 return isl_multi_aff_free(maff);
5742 return maff;
5745 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5747 aff = isl_aff_cow(aff);
5748 if (!aff)
5749 return NULL;
5751 aff->ls = isl_local_space_lift(aff->ls);
5752 if (!aff->ls)
5753 return isl_aff_free(aff);
5755 return aff;
5758 /* Lift "maff" to a space with extra dimensions such that the result
5759 * has no more existentially quantified variables.
5760 * If "ls" is not NULL, then *ls is assigned the local space that lies
5761 * at the basis of the lifting applied to "maff".
5763 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5764 __isl_give isl_local_space **ls)
5766 int i;
5767 isl_space *space;
5768 unsigned n_div;
5770 if (ls)
5771 *ls = NULL;
5773 if (!maff)
5774 return NULL;
5776 if (maff->n == 0) {
5777 if (ls) {
5778 isl_space *space = isl_multi_aff_get_domain_space(maff);
5779 *ls = isl_local_space_from_space(space);
5780 if (!*ls)
5781 return isl_multi_aff_free(maff);
5783 return maff;
5786 maff = isl_multi_aff_cow(maff);
5787 maff = isl_multi_aff_align_divs(maff);
5788 if (!maff)
5789 return NULL;
5791 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5792 space = isl_multi_aff_get_space(maff);
5793 space = isl_space_lift(isl_space_domain(space), n_div);
5794 space = isl_space_extend_domain_with_range(space,
5795 isl_multi_aff_get_space(maff));
5796 if (!space)
5797 return isl_multi_aff_free(maff);
5798 isl_space_free(maff->space);
5799 maff->space = space;
5801 if (ls) {
5802 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5803 if (!*ls)
5804 return isl_multi_aff_free(maff);
5807 for (i = 0; i < maff->n; ++i) {
5808 maff->p[i] = isl_aff_lift(maff->p[i]);
5809 if (!maff->p[i])
5810 goto error;
5813 return maff;
5814 error:
5815 if (ls)
5816 isl_local_space_free(*ls);
5817 return isl_multi_aff_free(maff);
5821 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5823 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5824 __isl_keep isl_pw_multi_aff *pma, int pos)
5826 int i;
5827 int n_out;
5828 isl_space *space;
5829 isl_pw_aff *pa;
5831 if (!pma)
5832 return NULL;
5834 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5835 if (pos < 0 || pos >= n_out)
5836 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5837 "index out of bounds", return NULL);
5839 space = isl_pw_multi_aff_get_space(pma);
5840 space = isl_space_drop_dims(space, isl_dim_out,
5841 pos + 1, n_out - pos - 1);
5842 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5844 pa = isl_pw_aff_alloc_size(space, pma->n);
5845 for (i = 0; i < pma->n; ++i) {
5846 isl_aff *aff;
5847 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5848 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5851 return pa;
5854 /* Return an isl_pw_multi_aff with the given "set" as domain and
5855 * an unnamed zero-dimensional range.
5857 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5858 __isl_take isl_set *set)
5860 isl_multi_aff *ma;
5861 isl_space *space;
5863 space = isl_set_get_space(set);
5864 space = isl_space_from_domain(space);
5865 ma = isl_multi_aff_zero(space);
5866 return isl_pw_multi_aff_alloc(set, ma);
5869 /* Add an isl_pw_multi_aff with the given "set" as domain and
5870 * an unnamed zero-dimensional range to *user.
5872 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5873 void *user)
5875 isl_union_pw_multi_aff **upma = user;
5876 isl_pw_multi_aff *pma;
5878 pma = isl_pw_multi_aff_from_domain(set);
5879 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5881 return isl_stat_ok;
5884 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5885 * an unnamed zero-dimensional range.
5887 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5888 __isl_take isl_union_set *uset)
5890 isl_space *space;
5891 isl_union_pw_multi_aff *upma;
5893 if (!uset)
5894 return NULL;
5896 space = isl_union_set_get_space(uset);
5897 upma = isl_union_pw_multi_aff_empty(space);
5899 if (isl_union_set_foreach_set(uset,
5900 &add_pw_multi_aff_from_domain, &upma) < 0)
5901 goto error;
5903 isl_union_set_free(uset);
5904 return upma;
5905 error:
5906 isl_union_set_free(uset);
5907 isl_union_pw_multi_aff_free(upma);
5908 return NULL;
5911 /* Convert "pma" to an isl_map and add it to *umap.
5913 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5914 void *user)
5916 isl_union_map **umap = user;
5917 isl_map *map;
5919 map = isl_map_from_pw_multi_aff(pma);
5920 *umap = isl_union_map_add_map(*umap, map);
5922 return isl_stat_ok;
5925 /* Construct a union map mapping the domain of the union
5926 * piecewise multi-affine expression to its range, with each dimension
5927 * in the range equated to the corresponding affine expression on its cell.
5929 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5930 __isl_take isl_union_pw_multi_aff *upma)
5932 isl_space *space;
5933 isl_union_map *umap;
5935 if (!upma)
5936 return NULL;
5938 space = isl_union_pw_multi_aff_get_space(upma);
5939 umap = isl_union_map_empty(space);
5941 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5942 &map_from_pw_multi_aff, &umap) < 0)
5943 goto error;
5945 isl_union_pw_multi_aff_free(upma);
5946 return umap;
5947 error:
5948 isl_union_pw_multi_aff_free(upma);
5949 isl_union_map_free(umap);
5950 return NULL;
5953 /* Local data for bin_entry and the callback "fn".
5955 struct isl_union_pw_multi_aff_bin_data {
5956 isl_union_pw_multi_aff *upma2;
5957 isl_union_pw_multi_aff *res;
5958 isl_pw_multi_aff *pma;
5959 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5962 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5963 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5965 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5967 struct isl_union_pw_multi_aff_bin_data *data = user;
5968 isl_stat r;
5970 data->pma = pma;
5971 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5972 data->fn, data);
5973 isl_pw_multi_aff_free(pma);
5975 return r;
5978 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5979 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5980 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5981 * as *entry. The callback should adjust data->res if desired.
5983 static __isl_give isl_union_pw_multi_aff *bin_op(
5984 __isl_take isl_union_pw_multi_aff *upma1,
5985 __isl_take isl_union_pw_multi_aff *upma2,
5986 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5988 isl_space *space;
5989 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5991 space = isl_union_pw_multi_aff_get_space(upma2);
5992 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5993 space = isl_union_pw_multi_aff_get_space(upma1);
5994 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5996 if (!upma1 || !upma2)
5997 goto error;
5999 data.upma2 = upma2;
6000 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6001 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6002 &bin_entry, &data) < 0)
6003 goto error;
6005 isl_union_pw_multi_aff_free(upma1);
6006 isl_union_pw_multi_aff_free(upma2);
6007 return data.res;
6008 error:
6009 isl_union_pw_multi_aff_free(upma1);
6010 isl_union_pw_multi_aff_free(upma2);
6011 isl_union_pw_multi_aff_free(data.res);
6012 return NULL;
6015 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6016 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6018 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6019 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6021 isl_space *space;
6023 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6024 isl_pw_multi_aff_get_space(pma2));
6025 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6026 &isl_multi_aff_range_product);
6029 /* Given two isl_pw_multi_affs A -> B and C -> D,
6030 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6032 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6033 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6035 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6036 &pw_multi_aff_range_product);
6039 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6040 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6042 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6043 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6045 isl_space *space;
6047 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6048 isl_pw_multi_aff_get_space(pma2));
6049 space = isl_space_flatten_range(space);
6050 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6051 &isl_multi_aff_flat_range_product);
6054 /* Given two isl_pw_multi_affs A -> B and C -> D,
6055 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6057 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6058 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6060 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6061 &pw_multi_aff_flat_range_product);
6064 /* If data->pma and "pma2" have the same domain space, then compute
6065 * their flat range product and the result to data->res.
6067 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6068 void *user)
6070 struct isl_union_pw_multi_aff_bin_data *data = user;
6072 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6073 pma2->dim, isl_dim_in)) {
6074 isl_pw_multi_aff_free(pma2);
6075 return isl_stat_ok;
6078 pma2 = isl_pw_multi_aff_flat_range_product(
6079 isl_pw_multi_aff_copy(data->pma), pma2);
6081 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6083 return isl_stat_ok;
6086 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6087 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6089 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6090 __isl_take isl_union_pw_multi_aff *upma1,
6091 __isl_take isl_union_pw_multi_aff *upma2)
6093 return bin_op(upma1, upma2, &flat_range_product_entry);
6096 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6097 * The parameters are assumed to have been aligned.
6099 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6100 * except that it works on two different isl_pw_* types.
6102 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6103 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6104 __isl_take isl_pw_aff *pa)
6106 int i, j, n;
6107 isl_pw_multi_aff *res = NULL;
6109 if (!pma || !pa)
6110 goto error;
6112 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6113 pa->dim, isl_dim_in))
6114 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6115 "domains don't match", goto error);
6116 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6117 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6118 "index out of bounds", goto error);
6120 n = pma->n * pa->n;
6121 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6123 for (i = 0; i < pma->n; ++i) {
6124 for (j = 0; j < pa->n; ++j) {
6125 isl_set *common;
6126 isl_multi_aff *res_ij;
6127 int empty;
6129 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6130 isl_set_copy(pa->p[j].set));
6131 empty = isl_set_plain_is_empty(common);
6132 if (empty < 0 || empty) {
6133 isl_set_free(common);
6134 if (empty < 0)
6135 goto error;
6136 continue;
6139 res_ij = isl_multi_aff_set_aff(
6140 isl_multi_aff_copy(pma->p[i].maff), pos,
6141 isl_aff_copy(pa->p[j].aff));
6142 res_ij = isl_multi_aff_gist(res_ij,
6143 isl_set_copy(common));
6145 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6149 isl_pw_multi_aff_free(pma);
6150 isl_pw_aff_free(pa);
6151 return res;
6152 error:
6153 isl_pw_multi_aff_free(pma);
6154 isl_pw_aff_free(pa);
6155 return isl_pw_multi_aff_free(res);
6158 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6160 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6161 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6162 __isl_take isl_pw_aff *pa)
6164 isl_bool equal_params;
6166 if (!pma || !pa)
6167 goto error;
6168 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6169 if (equal_params < 0)
6170 goto error;
6171 if (equal_params)
6172 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6173 if (!isl_space_has_named_params(pma->dim) ||
6174 !isl_space_has_named_params(pa->dim))
6175 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6176 "unaligned unnamed parameters", goto error);
6177 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6178 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6179 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6180 error:
6181 isl_pw_multi_aff_free(pma);
6182 isl_pw_aff_free(pa);
6183 return NULL;
6186 /* Do the parameters of "pa" match those of "space"?
6188 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6189 __isl_keep isl_space *space)
6191 isl_space *pa_space;
6192 isl_bool match;
6194 if (!pa || !space)
6195 return isl_bool_error;
6197 pa_space = isl_pw_aff_get_space(pa);
6199 match = isl_space_has_equal_params(space, pa_space);
6201 isl_space_free(pa_space);
6202 return match;
6205 /* Check that the domain space of "pa" matches "space".
6207 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6208 __isl_keep isl_space *space)
6210 isl_space *pa_space;
6211 isl_bool match;
6213 if (!pa || !space)
6214 return isl_stat_error;
6216 pa_space = isl_pw_aff_get_space(pa);
6218 match = isl_space_has_equal_params(space, pa_space);
6219 if (match < 0)
6220 goto error;
6221 if (!match)
6222 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6223 "parameters don't match", goto error);
6224 match = isl_space_tuple_is_equal(space, isl_dim_in,
6225 pa_space, isl_dim_in);
6226 if (match < 0)
6227 goto error;
6228 if (!match)
6229 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6230 "domains don't match", goto error);
6231 isl_space_free(pa_space);
6232 return isl_stat_ok;
6233 error:
6234 isl_space_free(pa_space);
6235 return isl_stat_error;
6238 #undef BASE
6239 #define BASE pw_aff
6240 #undef DOMBASE
6241 #define DOMBASE set
6243 #include <isl_multi_templ.c>
6244 #include <isl_multi_apply_set.c>
6245 #include <isl_multi_coalesce.c>
6246 #include <isl_multi_gist.c>
6247 #include <isl_multi_hash.c>
6248 #include <isl_multi_intersect.c>
6250 /* Scale the elements of "pma" by the corresponding elements of "mv".
6252 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6253 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6255 int i;
6256 isl_bool equal_params;
6258 pma = isl_pw_multi_aff_cow(pma);
6259 if (!pma || !mv)
6260 goto error;
6261 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6262 mv->space, isl_dim_set))
6263 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6264 "spaces don't match", goto error);
6265 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6266 if (equal_params < 0)
6267 goto error;
6268 if (!equal_params) {
6269 pma = isl_pw_multi_aff_align_params(pma,
6270 isl_multi_val_get_space(mv));
6271 mv = isl_multi_val_align_params(mv,
6272 isl_pw_multi_aff_get_space(pma));
6273 if (!pma || !mv)
6274 goto error;
6277 for (i = 0; i < pma->n; ++i) {
6278 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6279 isl_multi_val_copy(mv));
6280 if (!pma->p[i].maff)
6281 goto error;
6284 isl_multi_val_free(mv);
6285 return pma;
6286 error:
6287 isl_multi_val_free(mv);
6288 isl_pw_multi_aff_free(pma);
6289 return NULL;
6292 /* This function is called for each entry of an isl_union_pw_multi_aff.
6293 * If the space of the entry matches that of data->mv,
6294 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6295 * Otherwise, return an empty isl_pw_multi_aff.
6297 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6298 __isl_take isl_pw_multi_aff *pma, void *user)
6300 isl_multi_val *mv = user;
6302 if (!pma)
6303 return NULL;
6304 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6305 mv->space, isl_dim_set)) {
6306 isl_space *space = isl_pw_multi_aff_get_space(pma);
6307 isl_pw_multi_aff_free(pma);
6308 return isl_pw_multi_aff_empty(space);
6311 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6314 /* Scale the elements of "upma" by the corresponding elements of "mv",
6315 * for those entries that match the space of "mv".
6317 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6318 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6320 upma = isl_union_pw_multi_aff_align_params(upma,
6321 isl_multi_val_get_space(mv));
6322 mv = isl_multi_val_align_params(mv,
6323 isl_union_pw_multi_aff_get_space(upma));
6324 if (!upma || !mv)
6325 goto error;
6327 return isl_union_pw_multi_aff_transform(upma,
6328 &union_pw_multi_aff_scale_multi_val_entry, mv);
6330 isl_multi_val_free(mv);
6331 return upma;
6332 error:
6333 isl_multi_val_free(mv);
6334 isl_union_pw_multi_aff_free(upma);
6335 return NULL;
6338 /* Construct and return a piecewise multi affine expression
6339 * in the given space with value zero in each of the output dimensions and
6340 * a universe domain.
6342 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6344 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6347 /* Construct and return a piecewise multi affine expression
6348 * that is equal to the given piecewise affine expression.
6350 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6351 __isl_take isl_pw_aff *pa)
6353 int i;
6354 isl_space *space;
6355 isl_pw_multi_aff *pma;
6357 if (!pa)
6358 return NULL;
6360 space = isl_pw_aff_get_space(pa);
6361 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6363 for (i = 0; i < pa->n; ++i) {
6364 isl_set *set;
6365 isl_multi_aff *ma;
6367 set = isl_set_copy(pa->p[i].set);
6368 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6369 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6372 isl_pw_aff_free(pa);
6373 return pma;
6376 /* Construct a set or map mapping the shared (parameter) domain
6377 * of the piecewise affine expressions to the range of "mpa"
6378 * with each dimension in the range equated to the
6379 * corresponding piecewise affine expression.
6381 static __isl_give isl_map *map_from_multi_pw_aff(
6382 __isl_take isl_multi_pw_aff *mpa)
6384 int i;
6385 isl_space *space;
6386 isl_map *map;
6388 if (!mpa)
6389 return NULL;
6391 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6392 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6393 "invalid space", goto error);
6395 space = isl_multi_pw_aff_get_domain_space(mpa);
6396 map = isl_map_universe(isl_space_from_domain(space));
6398 for (i = 0; i < mpa->n; ++i) {
6399 isl_pw_aff *pa;
6400 isl_map *map_i;
6402 pa = isl_pw_aff_copy(mpa->p[i]);
6403 map_i = map_from_pw_aff(pa);
6405 map = isl_map_flat_range_product(map, map_i);
6408 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6410 isl_multi_pw_aff_free(mpa);
6411 return map;
6412 error:
6413 isl_multi_pw_aff_free(mpa);
6414 return NULL;
6417 /* Construct a map mapping the shared domain
6418 * of the piecewise affine expressions to the range of "mpa"
6419 * with each dimension in the range equated to the
6420 * corresponding piecewise affine expression.
6422 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6424 if (!mpa)
6425 return NULL;
6426 if (isl_space_is_set(mpa->space))
6427 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6428 "space of input is not a map", goto error);
6430 return map_from_multi_pw_aff(mpa);
6431 error:
6432 isl_multi_pw_aff_free(mpa);
6433 return NULL;
6436 /* Construct a set mapping the shared parameter domain
6437 * of the piecewise affine expressions to the space of "mpa"
6438 * with each dimension in the range equated to the
6439 * corresponding piecewise affine expression.
6441 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6443 if (!mpa)
6444 return NULL;
6445 if (!isl_space_is_set(mpa->space))
6446 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6447 "space of input is not a set", goto error);
6449 return map_from_multi_pw_aff(mpa);
6450 error:
6451 isl_multi_pw_aff_free(mpa);
6452 return NULL;
6455 /* Construct and return a piecewise multi affine expression
6456 * that is equal to the given multi piecewise affine expression
6457 * on the shared domain of the piecewise affine expressions.
6459 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6460 __isl_take isl_multi_pw_aff *mpa)
6462 int i;
6463 isl_space *space;
6464 isl_pw_aff *pa;
6465 isl_pw_multi_aff *pma;
6467 if (!mpa)
6468 return NULL;
6470 space = isl_multi_pw_aff_get_space(mpa);
6472 if (mpa->n == 0) {
6473 isl_multi_pw_aff_free(mpa);
6474 return isl_pw_multi_aff_zero(space);
6477 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6478 pma = isl_pw_multi_aff_from_pw_aff(pa);
6480 for (i = 1; i < mpa->n; ++i) {
6481 isl_pw_multi_aff *pma_i;
6483 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6484 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6485 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6488 pma = isl_pw_multi_aff_reset_space(pma, space);
6490 isl_multi_pw_aff_free(mpa);
6491 return pma;
6494 /* Construct and return a multi piecewise affine expression
6495 * that is equal to the given multi affine expression.
6497 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6498 __isl_take isl_multi_aff *ma)
6500 int i, n;
6501 isl_multi_pw_aff *mpa;
6503 if (!ma)
6504 return NULL;
6506 n = isl_multi_aff_dim(ma, isl_dim_out);
6507 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6509 for (i = 0; i < n; ++i) {
6510 isl_pw_aff *pa;
6512 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6513 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6516 isl_multi_aff_free(ma);
6517 return mpa;
6520 /* Construct and return a multi piecewise affine expression
6521 * that is equal to the given piecewise multi affine expression.
6523 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6524 __isl_take isl_pw_multi_aff *pma)
6526 int i, n;
6527 isl_space *space;
6528 isl_multi_pw_aff *mpa;
6530 if (!pma)
6531 return NULL;
6533 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6534 space = isl_pw_multi_aff_get_space(pma);
6535 mpa = isl_multi_pw_aff_alloc(space);
6537 for (i = 0; i < n; ++i) {
6538 isl_pw_aff *pa;
6540 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6541 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6544 isl_pw_multi_aff_free(pma);
6545 return mpa;
6548 /* Do "pa1" and "pa2" represent the same function?
6550 * We first check if they are obviously equal.
6551 * If not, we convert them to maps and check if those are equal.
6553 * If "pa1" or "pa2" contain any NaNs, then they are considered
6554 * not to be the same. A NaN is not equal to anything, not even
6555 * to another NaN.
6557 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6558 __isl_keep isl_pw_aff *pa2)
6560 isl_bool equal;
6561 isl_bool has_nan;
6562 isl_map *map1, *map2;
6564 if (!pa1 || !pa2)
6565 return isl_bool_error;
6567 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6568 if (equal < 0 || equal)
6569 return equal;
6570 has_nan = either_involves_nan(pa1, pa2);
6571 if (has_nan < 0)
6572 return isl_bool_error;
6573 if (has_nan)
6574 return isl_bool_false;
6576 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6577 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6578 equal = isl_map_is_equal(map1, map2);
6579 isl_map_free(map1);
6580 isl_map_free(map2);
6582 return equal;
6585 /* Do "mpa1" and "mpa2" represent the same function?
6587 * Note that we cannot convert the entire isl_multi_pw_aff
6588 * to a map because the domains of the piecewise affine expressions
6589 * may not be the same.
6591 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6592 __isl_keep isl_multi_pw_aff *mpa2)
6594 int i;
6595 isl_bool equal, equal_params;
6597 if (!mpa1 || !mpa2)
6598 return isl_bool_error;
6600 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6601 if (equal_params < 0)
6602 return isl_bool_error;
6603 if (!equal_params) {
6604 if (!isl_space_has_named_params(mpa1->space))
6605 return isl_bool_false;
6606 if (!isl_space_has_named_params(mpa2->space))
6607 return isl_bool_false;
6608 mpa1 = isl_multi_pw_aff_copy(mpa1);
6609 mpa2 = isl_multi_pw_aff_copy(mpa2);
6610 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6611 isl_multi_pw_aff_get_space(mpa2));
6612 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6613 isl_multi_pw_aff_get_space(mpa1));
6614 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6615 isl_multi_pw_aff_free(mpa1);
6616 isl_multi_pw_aff_free(mpa2);
6617 return equal;
6620 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6621 if (equal < 0 || !equal)
6622 return equal;
6624 for (i = 0; i < mpa1->n; ++i) {
6625 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6626 if (equal < 0 || !equal)
6627 return equal;
6630 return isl_bool_true;
6633 /* Do "pma1" and "pma2" represent the same function?
6635 * First check if they are obviously equal.
6636 * If not, then convert them to maps and check if those are equal.
6638 * If "pa1" or "pa2" contain any NaNs, then they are considered
6639 * not to be the same. A NaN is not equal to anything, not even
6640 * to another NaN.
6642 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6643 __isl_keep isl_pw_multi_aff *pma2)
6645 isl_bool equal;
6646 isl_bool has_nan;
6647 isl_map *map1, *map2;
6649 if (!pma1 || !pma2)
6650 return isl_bool_error;
6652 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6653 if (equal < 0 || equal)
6654 return equal;
6655 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6656 if (has_nan >= 0 && !has_nan)
6657 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6658 if (has_nan < 0 || has_nan)
6659 return isl_bool_not(has_nan);
6661 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6662 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6663 equal = isl_map_is_equal(map1, map2);
6664 isl_map_free(map1);
6665 isl_map_free(map2);
6667 return equal;
6670 /* Compute the pullback of "mpa" by the function represented by "ma".
6671 * In other words, plug in "ma" in "mpa".
6673 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6675 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6676 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6678 int i;
6679 isl_space *space = NULL;
6681 mpa = isl_multi_pw_aff_cow(mpa);
6682 if (!mpa || !ma)
6683 goto error;
6685 space = isl_space_join(isl_multi_aff_get_space(ma),
6686 isl_multi_pw_aff_get_space(mpa));
6687 if (!space)
6688 goto error;
6690 for (i = 0; i < mpa->n; ++i) {
6691 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6692 isl_multi_aff_copy(ma));
6693 if (!mpa->p[i])
6694 goto error;
6697 isl_multi_aff_free(ma);
6698 isl_space_free(mpa->space);
6699 mpa->space = space;
6700 return mpa;
6701 error:
6702 isl_space_free(space);
6703 isl_multi_pw_aff_free(mpa);
6704 isl_multi_aff_free(ma);
6705 return NULL;
6708 /* Compute the pullback of "mpa" by the function represented by "ma".
6709 * In other words, plug in "ma" in "mpa".
6711 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6712 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6714 isl_bool equal_params;
6716 if (!mpa || !ma)
6717 goto error;
6718 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6719 if (equal_params < 0)
6720 goto error;
6721 if (equal_params)
6722 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6723 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6724 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6725 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6726 error:
6727 isl_multi_pw_aff_free(mpa);
6728 isl_multi_aff_free(ma);
6729 return NULL;
6732 /* Compute the pullback of "mpa" by the function represented by "pma".
6733 * In other words, plug in "pma" in "mpa".
6735 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6737 static __isl_give isl_multi_pw_aff *
6738 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6739 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6741 int i;
6742 isl_space *space = NULL;
6744 mpa = isl_multi_pw_aff_cow(mpa);
6745 if (!mpa || !pma)
6746 goto error;
6748 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6749 isl_multi_pw_aff_get_space(mpa));
6751 for (i = 0; i < mpa->n; ++i) {
6752 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6753 isl_pw_multi_aff_copy(pma));
6754 if (!mpa->p[i])
6755 goto error;
6758 isl_pw_multi_aff_free(pma);
6759 isl_space_free(mpa->space);
6760 mpa->space = space;
6761 return mpa;
6762 error:
6763 isl_space_free(space);
6764 isl_multi_pw_aff_free(mpa);
6765 isl_pw_multi_aff_free(pma);
6766 return NULL;
6769 /* Compute the pullback of "mpa" by the function represented by "pma".
6770 * In other words, plug in "pma" in "mpa".
6772 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6773 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6775 isl_bool equal_params;
6777 if (!mpa || !pma)
6778 goto error;
6779 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6780 if (equal_params < 0)
6781 goto error;
6782 if (equal_params)
6783 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6784 mpa = isl_multi_pw_aff_align_params(mpa,
6785 isl_pw_multi_aff_get_space(pma));
6786 pma = isl_pw_multi_aff_align_params(pma,
6787 isl_multi_pw_aff_get_space(mpa));
6788 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6789 error:
6790 isl_multi_pw_aff_free(mpa);
6791 isl_pw_multi_aff_free(pma);
6792 return NULL;
6795 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6796 * with the domain of "aff". The domain of the result is the same
6797 * as that of "mpa".
6798 * "mpa" and "aff" are assumed to have been aligned.
6800 * We first extract the parametric constant from "aff", defined
6801 * over the correct domain.
6802 * Then we add the appropriate combinations of the members of "mpa".
6803 * Finally, we add the integer divisions through recursive calls.
6805 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6806 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6808 int i, n_in, n_div;
6809 isl_space *space;
6810 isl_val *v;
6811 isl_pw_aff *pa;
6812 isl_aff *tmp;
6814 n_in = isl_aff_dim(aff, isl_dim_in);
6815 n_div = isl_aff_dim(aff, isl_dim_div);
6817 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6818 tmp = isl_aff_copy(aff);
6819 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6820 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6821 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6822 isl_space_dim(space, isl_dim_set));
6823 tmp = isl_aff_reset_domain_space(tmp, space);
6824 pa = isl_pw_aff_from_aff(tmp);
6826 for (i = 0; i < n_in; ++i) {
6827 isl_pw_aff *pa_i;
6829 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6830 continue;
6831 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6832 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6833 pa_i = isl_pw_aff_scale_val(pa_i, v);
6834 pa = isl_pw_aff_add(pa, pa_i);
6837 for (i = 0; i < n_div; ++i) {
6838 isl_aff *div;
6839 isl_pw_aff *pa_i;
6841 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6842 continue;
6843 div = isl_aff_get_div(aff, i);
6844 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6845 isl_multi_pw_aff_copy(mpa), div);
6846 pa_i = isl_pw_aff_floor(pa_i);
6847 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6848 pa_i = isl_pw_aff_scale_val(pa_i, v);
6849 pa = isl_pw_aff_add(pa, pa_i);
6852 isl_multi_pw_aff_free(mpa);
6853 isl_aff_free(aff);
6855 return pa;
6858 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6859 * with the domain of "aff". The domain of the result is the same
6860 * as that of "mpa".
6862 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6863 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6865 isl_bool equal_params;
6867 if (!aff || !mpa)
6868 goto error;
6869 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6870 if (equal_params < 0)
6871 goto error;
6872 if (equal_params)
6873 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6875 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6876 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6878 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6879 error:
6880 isl_aff_free(aff);
6881 isl_multi_pw_aff_free(mpa);
6882 return NULL;
6885 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6886 * with the domain of "pa". The domain of the result is the same
6887 * as that of "mpa".
6888 * "mpa" and "pa" are assumed to have been aligned.
6890 * We consider each piece in turn. Note that the domains of the
6891 * pieces are assumed to be disjoint and they remain disjoint
6892 * after taking the preimage (over the same function).
6894 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6895 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6897 isl_space *space;
6898 isl_pw_aff *res;
6899 int i;
6901 if (!mpa || !pa)
6902 goto error;
6904 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6905 isl_pw_aff_get_space(pa));
6906 res = isl_pw_aff_empty(space);
6908 for (i = 0; i < pa->n; ++i) {
6909 isl_pw_aff *pa_i;
6910 isl_set *domain;
6912 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6913 isl_multi_pw_aff_copy(mpa),
6914 isl_aff_copy(pa->p[i].aff));
6915 domain = isl_set_copy(pa->p[i].set);
6916 domain = isl_set_preimage_multi_pw_aff(domain,
6917 isl_multi_pw_aff_copy(mpa));
6918 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6919 res = isl_pw_aff_add_disjoint(res, pa_i);
6922 isl_pw_aff_free(pa);
6923 isl_multi_pw_aff_free(mpa);
6924 return res;
6925 error:
6926 isl_pw_aff_free(pa);
6927 isl_multi_pw_aff_free(mpa);
6928 return NULL;
6931 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6932 * with the domain of "pa". The domain of the result is the same
6933 * as that of "mpa".
6935 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6936 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6938 isl_bool equal_params;
6940 if (!pa || !mpa)
6941 goto error;
6942 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6943 if (equal_params < 0)
6944 goto error;
6945 if (equal_params)
6946 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6948 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6949 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6951 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6952 error:
6953 isl_pw_aff_free(pa);
6954 isl_multi_pw_aff_free(mpa);
6955 return NULL;
6958 /* Compute the pullback of "pa" by the function represented by "mpa".
6959 * In other words, plug in "mpa" in "pa".
6960 * "pa" and "mpa" are assumed to have been aligned.
6962 * The pullback is computed by applying "pa" to "mpa".
6964 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6965 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6967 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6970 /* Compute the pullback of "pa" by the function represented by "mpa".
6971 * In other words, plug in "mpa" in "pa".
6973 * The pullback is computed by applying "pa" to "mpa".
6975 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6976 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6978 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6981 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6982 * In other words, plug in "mpa2" in "mpa1".
6984 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6986 * We pullback each member of "mpa1" in turn.
6988 static __isl_give isl_multi_pw_aff *
6989 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6990 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6992 int i;
6993 isl_space *space = NULL;
6995 mpa1 = isl_multi_pw_aff_cow(mpa1);
6996 if (!mpa1 || !mpa2)
6997 goto error;
6999 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7000 isl_multi_pw_aff_get_space(mpa1));
7002 for (i = 0; i < mpa1->n; ++i) {
7003 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7004 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
7005 if (!mpa1->p[i])
7006 goto error;
7009 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7011 isl_multi_pw_aff_free(mpa2);
7012 return mpa1;
7013 error:
7014 isl_space_free(space);
7015 isl_multi_pw_aff_free(mpa1);
7016 isl_multi_pw_aff_free(mpa2);
7017 return NULL;
7020 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7021 * In other words, plug in "mpa2" in "mpa1".
7023 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7024 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7026 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7027 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7030 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7031 * of "mpa1" and "mpa2" live in the same space, construct map space
7032 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7033 * with this map space as extract argument.
7035 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7036 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7037 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7038 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7040 int match;
7041 isl_space *space1, *space2;
7042 isl_map *res;
7044 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7045 isl_multi_pw_aff_get_space(mpa2));
7046 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7047 isl_multi_pw_aff_get_space(mpa1));
7048 if (!mpa1 || !mpa2)
7049 goto error;
7050 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7051 mpa2->space, isl_dim_out);
7052 if (match < 0)
7053 goto error;
7054 if (!match)
7055 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7056 "range spaces don't match", goto error);
7057 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7058 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7059 space1 = isl_space_map_from_domain_and_range(space1, space2);
7061 res = order(mpa1, mpa2, space1);
7062 isl_multi_pw_aff_free(mpa1);
7063 isl_multi_pw_aff_free(mpa2);
7064 return res;
7065 error:
7066 isl_multi_pw_aff_free(mpa1);
7067 isl_multi_pw_aff_free(mpa2);
7068 return NULL;
7071 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7072 * where the function values are equal. "space" is the space of the result.
7073 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7075 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7076 * in the sequences are equal.
7078 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7079 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7080 __isl_take isl_space *space)
7082 int i, n;
7083 isl_map *res;
7085 res = isl_map_universe(space);
7087 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7088 for (i = 0; i < n; ++i) {
7089 isl_pw_aff *pa1, *pa2;
7090 isl_map *map;
7092 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7093 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7094 map = isl_pw_aff_eq_map(pa1, pa2);
7095 res = isl_map_intersect(res, map);
7098 return res;
7101 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7102 * where the function values are equal.
7104 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7105 __isl_take isl_multi_pw_aff *mpa2)
7107 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7108 &isl_multi_pw_aff_eq_map_on_space);
7111 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7112 * where the function values of "mpa1" is lexicographically satisfies "base"
7113 * compared to that of "mpa2". "space" is the space of the result.
7114 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7116 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7117 * if its i-th element satisfies "base" when compared to
7118 * the i-th element of "mpa2" while all previous elements are
7119 * pairwise equal.
7121 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7122 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7123 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7124 __isl_take isl_pw_aff *pa2),
7125 __isl_take isl_space *space)
7127 int i, n;
7128 isl_map *res, *rest;
7130 res = isl_map_empty(isl_space_copy(space));
7131 rest = isl_map_universe(space);
7133 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7134 for (i = 0; i < n; ++i) {
7135 isl_pw_aff *pa1, *pa2;
7136 isl_map *map;
7138 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7139 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7140 map = base(pa1, pa2);
7141 map = isl_map_intersect(map, isl_map_copy(rest));
7142 res = isl_map_union(res, map);
7144 if (i == n - 1)
7145 continue;
7147 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7148 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7149 map = isl_pw_aff_eq_map(pa1, pa2);
7150 rest = isl_map_intersect(rest, map);
7153 isl_map_free(rest);
7154 return res;
7157 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7158 * where the function value of "mpa1" is lexicographically less than that
7159 * of "mpa2". "space" is the space of the result.
7160 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7162 * "mpa1" is less than "mpa2" if its i-th element is smaller
7163 * than the i-th element of "mpa2" while all previous elements are
7164 * pairwise equal.
7166 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7167 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7168 __isl_take isl_space *space)
7170 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7171 &isl_pw_aff_lt_map, space);
7174 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7175 * where the function value of "mpa1" is lexicographically less than that
7176 * of "mpa2".
7178 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7179 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7181 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7182 &isl_multi_pw_aff_lex_lt_map_on_space);
7185 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7186 * where the function value of "mpa1" is lexicographically greater than that
7187 * of "mpa2". "space" is the space of the result.
7188 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7190 * "mpa1" is greater than "mpa2" if its i-th element is greater
7191 * than the i-th element of "mpa2" while all previous elements are
7192 * pairwise equal.
7194 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7195 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7196 __isl_take isl_space *space)
7198 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7199 &isl_pw_aff_gt_map, space);
7202 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7203 * where the function value of "mpa1" is lexicographically greater than that
7204 * of "mpa2".
7206 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7207 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7209 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7210 &isl_multi_pw_aff_lex_gt_map_on_space);
7213 /* Compare two isl_affs.
7215 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7216 * than "aff2" and 0 if they are equal.
7218 * The order is fairly arbitrary. We do consider expressions that only involve
7219 * earlier dimensions as "smaller".
7221 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7223 int cmp;
7224 int last1, last2;
7226 if (aff1 == aff2)
7227 return 0;
7229 if (!aff1)
7230 return -1;
7231 if (!aff2)
7232 return 1;
7234 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7235 if (cmp != 0)
7236 return cmp;
7238 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7239 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7240 if (last1 != last2)
7241 return last1 - last2;
7243 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7246 /* Compare two isl_pw_affs.
7248 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7249 * than "pa2" and 0 if they are equal.
7251 * The order is fairly arbitrary. We do consider expressions that only involve
7252 * earlier dimensions as "smaller".
7254 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7255 __isl_keep isl_pw_aff *pa2)
7257 int i;
7258 int cmp;
7260 if (pa1 == pa2)
7261 return 0;
7263 if (!pa1)
7264 return -1;
7265 if (!pa2)
7266 return 1;
7268 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7269 if (cmp != 0)
7270 return cmp;
7272 if (pa1->n != pa2->n)
7273 return pa1->n - pa2->n;
7275 for (i = 0; i < pa1->n; ++i) {
7276 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7277 if (cmp != 0)
7278 return cmp;
7279 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7280 if (cmp != 0)
7281 return cmp;
7284 return 0;
7287 /* Return a piecewise affine expression that is equal to "v" on "domain".
7289 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7290 __isl_take isl_val *v)
7292 isl_space *space;
7293 isl_local_space *ls;
7294 isl_aff *aff;
7296 space = isl_set_get_space(domain);
7297 ls = isl_local_space_from_space(space);
7298 aff = isl_aff_val_on_domain(ls, v);
7300 return isl_pw_aff_alloc(domain, aff);
7303 /* Return a multi affine expression that is equal to "mv" on domain
7304 * space "space".
7306 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7307 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7309 int i, n;
7310 isl_space *space2;
7311 isl_local_space *ls;
7312 isl_multi_aff *ma;
7314 if (!space || !mv)
7315 goto error;
7317 n = isl_multi_val_dim(mv, isl_dim_set);
7318 space2 = isl_multi_val_get_space(mv);
7319 space2 = isl_space_align_params(space2, isl_space_copy(space));
7320 space = isl_space_align_params(space, isl_space_copy(space2));
7321 space = isl_space_map_from_domain_and_range(space, space2);
7322 ma = isl_multi_aff_alloc(isl_space_copy(space));
7323 ls = isl_local_space_from_space(isl_space_domain(space));
7324 for (i = 0; i < n; ++i) {
7325 isl_val *v;
7326 isl_aff *aff;
7328 v = isl_multi_val_get_val(mv, i);
7329 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7330 ma = isl_multi_aff_set_aff(ma, i, aff);
7332 isl_local_space_free(ls);
7334 isl_multi_val_free(mv);
7335 return ma;
7336 error:
7337 isl_space_free(space);
7338 isl_multi_val_free(mv);
7339 return NULL;
7342 /* Return a piecewise multi-affine expression
7343 * that is equal to "mv" on "domain".
7345 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7346 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7348 isl_space *space;
7349 isl_multi_aff *ma;
7351 space = isl_set_get_space(domain);
7352 ma = isl_multi_aff_multi_val_on_space(space, mv);
7354 return isl_pw_multi_aff_alloc(domain, ma);
7357 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7358 * mv is the value that should be attained on each domain set
7359 * res collects the results
7361 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7362 isl_multi_val *mv;
7363 isl_union_pw_multi_aff *res;
7366 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7367 * and add it to data->res.
7369 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7370 void *user)
7372 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7373 isl_pw_multi_aff *pma;
7374 isl_multi_val *mv;
7376 mv = isl_multi_val_copy(data->mv);
7377 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7378 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7380 return data->res ? isl_stat_ok : isl_stat_error;
7383 /* Return a union piecewise multi-affine expression
7384 * that is equal to "mv" on "domain".
7386 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7387 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7389 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7390 isl_space *space;
7392 space = isl_union_set_get_space(domain);
7393 data.res = isl_union_pw_multi_aff_empty(space);
7394 data.mv = mv;
7395 if (isl_union_set_foreach_set(domain,
7396 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7397 data.res = isl_union_pw_multi_aff_free(data.res);
7398 isl_union_set_free(domain);
7399 isl_multi_val_free(mv);
7400 return data.res;
7403 /* Compute the pullback of data->pma by the function represented by "pma2",
7404 * provided the spaces match, and add the results to data->res.
7406 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7408 struct isl_union_pw_multi_aff_bin_data *data = user;
7410 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7411 pma2->dim, isl_dim_out)) {
7412 isl_pw_multi_aff_free(pma2);
7413 return isl_stat_ok;
7416 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7417 isl_pw_multi_aff_copy(data->pma), pma2);
7419 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7420 if (!data->res)
7421 return isl_stat_error;
7423 return isl_stat_ok;
7426 /* Compute the pullback of "upma1" by the function represented by "upma2".
7428 __isl_give isl_union_pw_multi_aff *
7429 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7430 __isl_take isl_union_pw_multi_aff *upma1,
7431 __isl_take isl_union_pw_multi_aff *upma2)
7433 return bin_op(upma1, upma2, &pullback_entry);
7436 /* Check that the domain space of "upa" matches "space".
7438 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7439 * can in principle never fail since the space "space" is that
7440 * of the isl_multi_union_pw_aff and is a set space such that
7441 * there is no domain space to match.
7443 * We check the parameters and double-check that "space" is
7444 * indeed that of a set.
7446 static isl_stat isl_union_pw_aff_check_match_domain_space(
7447 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7449 isl_space *upa_space;
7450 isl_bool match;
7452 if (!upa || !space)
7453 return isl_stat_error;
7455 match = isl_space_is_set(space);
7456 if (match < 0)
7457 return isl_stat_error;
7458 if (!match)
7459 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7460 "expecting set space", return -1);
7462 upa_space = isl_union_pw_aff_get_space(upa);
7463 match = isl_space_has_equal_params(space, upa_space);
7464 if (match < 0)
7465 goto error;
7466 if (!match)
7467 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7468 "parameters don't match", goto error);
7470 isl_space_free(upa_space);
7471 return isl_stat_ok;
7472 error:
7473 isl_space_free(upa_space);
7474 return isl_stat_error;
7477 /* Do the parameters of "upa" match those of "space"?
7479 static isl_bool isl_union_pw_aff_matching_params(
7480 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7482 isl_space *upa_space;
7483 isl_bool match;
7485 if (!upa || !space)
7486 return isl_bool_error;
7488 upa_space = isl_union_pw_aff_get_space(upa);
7490 match = isl_space_has_equal_params(space, upa_space);
7492 isl_space_free(upa_space);
7493 return match;
7496 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7497 * space represents the new parameters.
7498 * res collects the results.
7500 struct isl_union_pw_aff_reset_params_data {
7501 isl_space *space;
7502 isl_union_pw_aff *res;
7505 /* Replace the parameters of "pa" by data->space and
7506 * add the result to data->res.
7508 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7510 struct isl_union_pw_aff_reset_params_data *data = user;
7511 isl_space *space;
7513 space = isl_pw_aff_get_space(pa);
7514 space = isl_space_replace(space, isl_dim_param, data->space);
7515 pa = isl_pw_aff_reset_space(pa, space);
7516 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7518 return data->res ? isl_stat_ok : isl_stat_error;
7521 /* Replace the domain space of "upa" by "space".
7522 * Since a union expression does not have a (single) domain space,
7523 * "space" is necessarily a parameter space.
7525 * Since the order and the names of the parameters determine
7526 * the hash value, we need to create a new hash table.
7528 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7529 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7531 struct isl_union_pw_aff_reset_params_data data = { space };
7532 isl_bool match;
7534 match = isl_union_pw_aff_matching_params(upa, space);
7535 if (match < 0)
7536 upa = isl_union_pw_aff_free(upa);
7537 else if (match) {
7538 isl_space_free(space);
7539 return upa;
7542 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7543 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7544 data.res = isl_union_pw_aff_free(data.res);
7546 isl_union_pw_aff_free(upa);
7547 isl_space_free(space);
7548 return data.res;
7551 /* Return the floor of "pa".
7553 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7555 return isl_pw_aff_floor(pa);
7558 /* Given f, return floor(f).
7560 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7561 __isl_take isl_union_pw_aff *upa)
7563 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7566 /* Compute
7568 * upa mod m = upa - m * floor(upa/m)
7570 * with m an integer value.
7572 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7573 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7575 isl_union_pw_aff *res;
7577 if (!upa || !m)
7578 goto error;
7580 if (!isl_val_is_int(m))
7581 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7582 "expecting integer modulo", goto error);
7583 if (!isl_val_is_pos(m))
7584 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7585 "expecting positive modulo", goto error);
7587 res = isl_union_pw_aff_copy(upa);
7588 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7589 upa = isl_union_pw_aff_floor(upa);
7590 upa = isl_union_pw_aff_scale_val(upa, m);
7591 res = isl_union_pw_aff_sub(res, upa);
7593 return res;
7594 error:
7595 isl_val_free(m);
7596 isl_union_pw_aff_free(upa);
7597 return NULL;
7600 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7601 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7602 * needs to attain.
7603 * "res" collects the results.
7605 struct isl_union_pw_aff_aff_on_domain_data {
7606 isl_aff *aff;
7607 isl_union_pw_aff *res;
7610 /* Construct a piecewise affine expression that is equal to data->aff
7611 * on "domain" and add the result to data->res.
7613 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7615 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7616 isl_pw_aff *pa;
7617 isl_aff *aff;
7618 int dim;
7620 aff = isl_aff_copy(data->aff);
7621 dim = isl_set_dim(domain, isl_dim_set);
7622 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7623 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7624 pa = isl_pw_aff_alloc(domain, aff);
7625 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7627 return data->res ? isl_stat_ok : isl_stat_error;
7630 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7631 * pos is the output position that needs to be extracted.
7632 * res collects the results.
7634 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7635 int pos;
7636 isl_union_pw_aff *res;
7639 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7640 * (assuming it has such a dimension) and add it to data->res.
7642 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7644 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7645 int n_out;
7646 isl_pw_aff *pa;
7648 if (!pma)
7649 return isl_stat_error;
7651 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7652 if (data->pos >= n_out) {
7653 isl_pw_multi_aff_free(pma);
7654 return isl_stat_ok;
7657 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7658 isl_pw_multi_aff_free(pma);
7660 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7662 return data->res ? isl_stat_ok : isl_stat_error;
7665 /* Extract an isl_union_pw_aff corresponding to
7666 * output dimension "pos" of "upma".
7668 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7669 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7671 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7672 isl_space *space;
7674 if (!upma)
7675 return NULL;
7677 if (pos < 0)
7678 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7679 "cannot extract at negative position", return NULL);
7681 space = isl_union_pw_multi_aff_get_space(upma);
7682 data.res = isl_union_pw_aff_empty(space);
7683 data.pos = pos;
7684 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7685 &get_union_pw_aff, &data) < 0)
7686 data.res = isl_union_pw_aff_free(data.res);
7688 return data.res;
7691 /* Return a union piecewise affine expression
7692 * that is equal to "aff" on "domain".
7694 * Construct an isl_pw_aff on each of the sets in "domain" and
7695 * collect the results.
7697 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7698 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7700 struct isl_union_pw_aff_aff_on_domain_data data;
7701 isl_space *space;
7703 if (!domain || !aff)
7704 goto error;
7705 if (!isl_local_space_is_params(aff->ls))
7706 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7707 "expecting parametric expression", goto error);
7709 space = isl_union_set_get_space(domain);
7710 data.res = isl_union_pw_aff_empty(space);
7711 data.aff = aff;
7712 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7713 data.res = isl_union_pw_aff_free(data.res);
7714 isl_union_set_free(domain);
7715 isl_aff_free(aff);
7716 return data.res;
7717 error:
7718 isl_union_set_free(domain);
7719 isl_aff_free(aff);
7720 return NULL;
7723 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7724 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7725 * "res" collects the results.
7727 struct isl_union_pw_aff_val_on_domain_data {
7728 isl_val *v;
7729 isl_union_pw_aff *res;
7732 /* Construct a piecewise affine expression that is equal to data->v
7733 * on "domain" and add the result to data->res.
7735 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7737 struct isl_union_pw_aff_val_on_domain_data *data = user;
7738 isl_pw_aff *pa;
7739 isl_val *v;
7741 v = isl_val_copy(data->v);
7742 pa = isl_pw_aff_val_on_domain(domain, v);
7743 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7745 return data->res ? isl_stat_ok : isl_stat_error;
7748 /* Return a union piecewise affine expression
7749 * that is equal to "v" on "domain".
7751 * Construct an isl_pw_aff on each of the sets in "domain" and
7752 * collect the results.
7754 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7755 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7757 struct isl_union_pw_aff_val_on_domain_data data;
7758 isl_space *space;
7760 space = isl_union_set_get_space(domain);
7761 data.res = isl_union_pw_aff_empty(space);
7762 data.v = v;
7763 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7764 data.res = isl_union_pw_aff_free(data.res);
7765 isl_union_set_free(domain);
7766 isl_val_free(v);
7767 return data.res;
7770 /* Construct a piecewise multi affine expression
7771 * that is equal to "pa" and add it to upma.
7773 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7774 void *user)
7776 isl_union_pw_multi_aff **upma = user;
7777 isl_pw_multi_aff *pma;
7779 pma = isl_pw_multi_aff_from_pw_aff(pa);
7780 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7782 return *upma ? isl_stat_ok : isl_stat_error;
7785 /* Construct and return a union piecewise multi affine expression
7786 * that is equal to the given union piecewise affine expression.
7788 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7789 __isl_take isl_union_pw_aff *upa)
7791 isl_space *space;
7792 isl_union_pw_multi_aff *upma;
7794 if (!upa)
7795 return NULL;
7797 space = isl_union_pw_aff_get_space(upa);
7798 upma = isl_union_pw_multi_aff_empty(space);
7800 if (isl_union_pw_aff_foreach_pw_aff(upa,
7801 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7802 upma = isl_union_pw_multi_aff_free(upma);
7804 isl_union_pw_aff_free(upa);
7805 return upma;
7808 /* Compute the set of elements in the domain of "pa" where it is zero and
7809 * add this set to "uset".
7811 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7813 isl_union_set **uset = (isl_union_set **)user;
7815 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7817 return *uset ? isl_stat_ok : isl_stat_error;
7820 /* Return a union set containing those elements in the domain
7821 * of "upa" where it is zero.
7823 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7824 __isl_take isl_union_pw_aff *upa)
7826 isl_union_set *zero;
7828 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7829 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7830 zero = isl_union_set_free(zero);
7832 isl_union_pw_aff_free(upa);
7833 return zero;
7836 /* Convert "pa" to an isl_map and add it to *umap.
7838 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7840 isl_union_map **umap = user;
7841 isl_map *map;
7843 map = isl_map_from_pw_aff(pa);
7844 *umap = isl_union_map_add_map(*umap, map);
7846 return *umap ? isl_stat_ok : isl_stat_error;
7849 /* Construct a union map mapping the domain of the union
7850 * piecewise affine expression to its range, with the single output dimension
7851 * equated to the corresponding affine expressions on their cells.
7853 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7854 __isl_take isl_union_pw_aff *upa)
7856 isl_space *space;
7857 isl_union_map *umap;
7859 if (!upa)
7860 return NULL;
7862 space = isl_union_pw_aff_get_space(upa);
7863 umap = isl_union_map_empty(space);
7865 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7866 &umap) < 0)
7867 umap = isl_union_map_free(umap);
7869 isl_union_pw_aff_free(upa);
7870 return umap;
7873 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7874 * upma is the function that is plugged in.
7875 * pa is the current part of the function in which upma is plugged in.
7876 * res collects the results.
7878 struct isl_union_pw_aff_pullback_upma_data {
7879 isl_union_pw_multi_aff *upma;
7880 isl_pw_aff *pa;
7881 isl_union_pw_aff *res;
7884 /* Check if "pma" can be plugged into data->pa.
7885 * If so, perform the pullback and add the result to data->res.
7887 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7889 struct isl_union_pw_aff_pullback_upma_data *data = user;
7890 isl_pw_aff *pa;
7892 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7893 pma->dim, isl_dim_out)) {
7894 isl_pw_multi_aff_free(pma);
7895 return isl_stat_ok;
7898 pa = isl_pw_aff_copy(data->pa);
7899 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7901 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7903 return data->res ? isl_stat_ok : isl_stat_error;
7906 /* Check if any of the elements of data->upma can be plugged into pa,
7907 * add if so add the result to data->res.
7909 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7911 struct isl_union_pw_aff_pullback_upma_data *data = user;
7912 isl_stat r;
7914 data->pa = pa;
7915 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7916 &pa_pb_pma, data);
7917 isl_pw_aff_free(pa);
7919 return r;
7922 /* Compute the pullback of "upa" by the function represented by "upma".
7923 * In other words, plug in "upma" in "upa". The result contains
7924 * expressions defined over the domain space of "upma".
7926 * Run over all pairs of elements in "upa" and "upma", perform
7927 * the pullback when appropriate and collect the results.
7928 * If the hash value were based on the domain space rather than
7929 * the function space, then we could run through all elements
7930 * of "upma" and directly pick out the corresponding element of "upa".
7932 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7933 __isl_take isl_union_pw_aff *upa,
7934 __isl_take isl_union_pw_multi_aff *upma)
7936 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7937 isl_space *space;
7939 space = isl_union_pw_multi_aff_get_space(upma);
7940 upa = isl_union_pw_aff_align_params(upa, space);
7941 space = isl_union_pw_aff_get_space(upa);
7942 upma = isl_union_pw_multi_aff_align_params(upma, space);
7944 if (!upa || !upma)
7945 goto error;
7947 data.upma = upma;
7948 data.res = isl_union_pw_aff_alloc_same_size(upa);
7949 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7950 data.res = isl_union_pw_aff_free(data.res);
7952 isl_union_pw_aff_free(upa);
7953 isl_union_pw_multi_aff_free(upma);
7954 return data.res;
7955 error:
7956 isl_union_pw_aff_free(upa);
7957 isl_union_pw_multi_aff_free(upma);
7958 return NULL;
7961 #undef BASE
7962 #define BASE union_pw_aff
7963 #undef DOMBASE
7964 #define DOMBASE union_set
7966 #define NO_MOVE_DIMS
7967 #define NO_DIMS
7968 #define NO_DOMAIN
7969 #define NO_PRODUCT
7970 #define NO_SPLICE
7971 #define NO_ZERO
7972 #define NO_IDENTITY
7973 #define NO_GIST
7975 #include <isl_multi_templ.c>
7976 #include <isl_multi_apply_set.c>
7977 #include <isl_multi_apply_union_set.c>
7978 #include <isl_multi_coalesce.c>
7979 #include <isl_multi_floor.c>
7980 #include <isl_multi_gist.c>
7981 #include <isl_multi_intersect.c>
7983 /* Construct a multiple union piecewise affine expression
7984 * in the given space with value zero in each of the output dimensions.
7986 * Since there is no canonical zero value for
7987 * a union piecewise affine expression, we can only construct
7988 * zero-dimensional "zero" value.
7990 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7991 __isl_take isl_space *space)
7993 if (!space)
7994 return NULL;
7996 if (!isl_space_is_set(space))
7997 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7998 "expecting set space", goto error);
7999 if (isl_space_dim(space , isl_dim_out) != 0)
8000 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8001 "expecting 0D space", goto error);
8003 return isl_multi_union_pw_aff_alloc(space);
8004 error:
8005 isl_space_free(space);
8006 return NULL;
8009 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8010 * with the actual sum on the shared domain and
8011 * the defined expression on the symmetric difference of the domains.
8013 * We simply iterate over the elements in both arguments and
8014 * call isl_union_pw_aff_union_add on each of them.
8016 static __isl_give isl_multi_union_pw_aff *
8017 isl_multi_union_pw_aff_union_add_aligned(
8018 __isl_take isl_multi_union_pw_aff *mupa1,
8019 __isl_take isl_multi_union_pw_aff *mupa2)
8021 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8022 &isl_union_pw_aff_union_add);
8025 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8026 * with the actual sum on the shared domain and
8027 * the defined expression on the symmetric difference of the domains.
8029 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8030 __isl_take isl_multi_union_pw_aff *mupa1,
8031 __isl_take isl_multi_union_pw_aff *mupa2)
8033 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8034 &isl_multi_union_pw_aff_union_add_aligned);
8037 /* Construct and return a multi union piecewise affine expression
8038 * that is equal to the given multi affine expression.
8040 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8041 __isl_take isl_multi_aff *ma)
8043 isl_multi_pw_aff *mpa;
8045 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8046 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8049 /* Construct and return a multi union piecewise affine expression
8050 * that is equal to the given multi piecewise affine expression.
8052 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8053 __isl_take isl_multi_pw_aff *mpa)
8055 int i, n;
8056 isl_space *space;
8057 isl_multi_union_pw_aff *mupa;
8059 if (!mpa)
8060 return NULL;
8062 space = isl_multi_pw_aff_get_space(mpa);
8063 space = isl_space_range(space);
8064 mupa = isl_multi_union_pw_aff_alloc(space);
8066 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8067 for (i = 0; i < n; ++i) {
8068 isl_pw_aff *pa;
8069 isl_union_pw_aff *upa;
8071 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8072 upa = isl_union_pw_aff_from_pw_aff(pa);
8073 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8076 isl_multi_pw_aff_free(mpa);
8078 return mupa;
8081 /* Extract the range space of "pma" and assign it to *space.
8082 * If *space has already been set (through a previous call to this function),
8083 * then check that the range space is the same.
8085 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8087 isl_space **space = user;
8088 isl_space *pma_space;
8089 isl_bool equal;
8091 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8092 isl_pw_multi_aff_free(pma);
8094 if (!pma_space)
8095 return isl_stat_error;
8096 if (!*space) {
8097 *space = pma_space;
8098 return isl_stat_ok;
8101 equal = isl_space_is_equal(pma_space, *space);
8102 isl_space_free(pma_space);
8104 if (equal < 0)
8105 return isl_stat_error;
8106 if (!equal)
8107 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8108 "range spaces not the same", return isl_stat_error);
8109 return isl_stat_ok;
8112 /* Construct and return a multi union piecewise affine expression
8113 * that is equal to the given union piecewise multi affine expression.
8115 * In order to be able to perform the conversion, the input
8116 * needs to be non-empty and may only involve a single range space.
8118 __isl_give isl_multi_union_pw_aff *
8119 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8120 __isl_take isl_union_pw_multi_aff *upma)
8122 isl_space *space = NULL;
8123 isl_multi_union_pw_aff *mupa;
8124 int i, n;
8126 if (!upma)
8127 return NULL;
8128 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8129 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8130 "cannot extract range space from empty input",
8131 goto error);
8132 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8133 &space) < 0)
8134 goto error;
8136 if (!space)
8137 goto error;
8139 n = isl_space_dim(space, isl_dim_set);
8140 mupa = isl_multi_union_pw_aff_alloc(space);
8142 for (i = 0; i < n; ++i) {
8143 isl_union_pw_aff *upa;
8145 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8146 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8149 isl_union_pw_multi_aff_free(upma);
8150 return mupa;
8151 error:
8152 isl_space_free(space);
8153 isl_union_pw_multi_aff_free(upma);
8154 return NULL;
8157 /* Try and create an isl_multi_union_pw_aff that is equivalent
8158 * to the given isl_union_map.
8159 * The isl_union_map is required to be single-valued in each space.
8160 * Moreover, it cannot be empty and all range spaces need to be the same.
8161 * Otherwise, an error is produced.
8163 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8164 __isl_take isl_union_map *umap)
8166 isl_union_pw_multi_aff *upma;
8168 upma = isl_union_pw_multi_aff_from_union_map(umap);
8169 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8172 /* Return a multiple union piecewise affine expression
8173 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8174 * have been aligned.
8176 static __isl_give isl_multi_union_pw_aff *
8177 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8178 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8180 int i, n;
8181 isl_space *space;
8182 isl_multi_union_pw_aff *mupa;
8184 if (!domain || !mv)
8185 goto error;
8187 n = isl_multi_val_dim(mv, isl_dim_set);
8188 space = isl_multi_val_get_space(mv);
8189 mupa = isl_multi_union_pw_aff_alloc(space);
8190 for (i = 0; i < n; ++i) {
8191 isl_val *v;
8192 isl_union_pw_aff *upa;
8194 v = isl_multi_val_get_val(mv, i);
8195 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8197 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8200 isl_union_set_free(domain);
8201 isl_multi_val_free(mv);
8202 return mupa;
8203 error:
8204 isl_union_set_free(domain);
8205 isl_multi_val_free(mv);
8206 return NULL;
8209 /* Return a multiple union piecewise affine expression
8210 * that is equal to "mv" on "domain".
8212 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8213 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8215 isl_bool equal_params;
8217 if (!domain || !mv)
8218 goto error;
8219 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8220 if (equal_params < 0)
8221 goto error;
8222 if (equal_params)
8223 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8224 domain, mv);
8225 domain = isl_union_set_align_params(domain,
8226 isl_multi_val_get_space(mv));
8227 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8228 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8229 error:
8230 isl_union_set_free(domain);
8231 isl_multi_val_free(mv);
8232 return NULL;
8235 /* Return a multiple union piecewise affine expression
8236 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8237 * have been aligned.
8239 static __isl_give isl_multi_union_pw_aff *
8240 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8241 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8243 int i, n;
8244 isl_space *space;
8245 isl_multi_union_pw_aff *mupa;
8247 if (!domain || !ma)
8248 goto error;
8250 n = isl_multi_aff_dim(ma, isl_dim_set);
8251 space = isl_multi_aff_get_space(ma);
8252 mupa = isl_multi_union_pw_aff_alloc(space);
8253 for (i = 0; i < n; ++i) {
8254 isl_aff *aff;
8255 isl_union_pw_aff *upa;
8257 aff = isl_multi_aff_get_aff(ma, i);
8258 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8259 aff);
8260 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8263 isl_union_set_free(domain);
8264 isl_multi_aff_free(ma);
8265 return mupa;
8266 error:
8267 isl_union_set_free(domain);
8268 isl_multi_aff_free(ma);
8269 return NULL;
8272 /* Return a multiple union piecewise affine expression
8273 * that is equal to "ma" on "domain".
8275 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8276 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8278 isl_bool equal_params;
8280 if (!domain || !ma)
8281 goto error;
8282 equal_params = isl_space_has_equal_params(domain->dim, ma->space);
8283 if (equal_params < 0)
8284 goto error;
8285 if (equal_params)
8286 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8287 domain, ma);
8288 domain = isl_union_set_align_params(domain,
8289 isl_multi_aff_get_space(ma));
8290 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8291 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8292 error:
8293 isl_union_set_free(domain);
8294 isl_multi_aff_free(ma);
8295 return NULL;
8298 /* Return a union set containing those elements in the domains
8299 * of the elements of "mupa" where they are all zero.
8301 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8302 __isl_take isl_multi_union_pw_aff *mupa)
8304 int i, n;
8305 isl_union_pw_aff *upa;
8306 isl_union_set *zero;
8308 if (!mupa)
8309 return NULL;
8311 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8312 if (n == 0)
8313 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8314 "cannot determine zero set "
8315 "of zero-dimensional function", goto error);
8317 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8318 zero = isl_union_pw_aff_zero_union_set(upa);
8320 for (i = 1; i < n; ++i) {
8321 isl_union_set *zero_i;
8323 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8324 zero_i = isl_union_pw_aff_zero_union_set(upa);
8326 zero = isl_union_set_intersect(zero, zero_i);
8329 isl_multi_union_pw_aff_free(mupa);
8330 return zero;
8331 error:
8332 isl_multi_union_pw_aff_free(mupa);
8333 return NULL;
8336 /* Construct a union map mapping the shared domain
8337 * of the union piecewise affine expressions to the range of "mupa"
8338 * with each dimension in the range equated to the
8339 * corresponding union piecewise affine expression.
8341 * The input cannot be zero-dimensional as there is
8342 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8344 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8345 __isl_take isl_multi_union_pw_aff *mupa)
8347 int i, n;
8348 isl_space *space;
8349 isl_union_map *umap;
8350 isl_union_pw_aff *upa;
8352 if (!mupa)
8353 return NULL;
8355 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8356 if (n == 0)
8357 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8358 "cannot determine domain of zero-dimensional "
8359 "isl_multi_union_pw_aff", goto error);
8361 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8362 umap = isl_union_map_from_union_pw_aff(upa);
8364 for (i = 1; i < n; ++i) {
8365 isl_union_map *umap_i;
8367 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8368 umap_i = isl_union_map_from_union_pw_aff(upa);
8369 umap = isl_union_map_flat_range_product(umap, umap_i);
8372 space = isl_multi_union_pw_aff_get_space(mupa);
8373 umap = isl_union_map_reset_range_space(umap, space);
8375 isl_multi_union_pw_aff_free(mupa);
8376 return umap;
8377 error:
8378 isl_multi_union_pw_aff_free(mupa);
8379 return NULL;
8382 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8383 * "range" is the space from which to set the range space.
8384 * "res" collects the results.
8386 struct isl_union_pw_multi_aff_reset_range_space_data {
8387 isl_space *range;
8388 isl_union_pw_multi_aff *res;
8391 /* Replace the range space of "pma" by the range space of data->range and
8392 * add the result to data->res.
8394 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8396 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8397 isl_space *space;
8399 space = isl_pw_multi_aff_get_space(pma);
8400 space = isl_space_domain(space);
8401 space = isl_space_extend_domain_with_range(space,
8402 isl_space_copy(data->range));
8403 pma = isl_pw_multi_aff_reset_space(pma, space);
8404 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8406 return data->res ? isl_stat_ok : isl_stat_error;
8409 /* Replace the range space of all the piecewise affine expressions in "upma" by
8410 * the range space of "space".
8412 * This assumes that all these expressions have the same output dimension.
8414 * Since the spaces of the expressions change, so do their hash values.
8415 * We therefore need to create a new isl_union_pw_multi_aff.
8416 * Note that the hash value is currently computed based on the entire
8417 * space even though there can only be a single expression with a given
8418 * domain space.
8420 static __isl_give isl_union_pw_multi_aff *
8421 isl_union_pw_multi_aff_reset_range_space(
8422 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8424 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8425 isl_space *space_upma;
8427 space_upma = isl_union_pw_multi_aff_get_space(upma);
8428 data.res = isl_union_pw_multi_aff_empty(space_upma);
8429 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8430 &reset_range_space, &data) < 0)
8431 data.res = isl_union_pw_multi_aff_free(data.res);
8433 isl_space_free(space);
8434 isl_union_pw_multi_aff_free(upma);
8435 return data.res;
8438 /* Construct and return a union piecewise multi affine expression
8439 * that is equal to the given multi union piecewise affine expression.
8441 * In order to be able to perform the conversion, the input
8442 * needs to have a least one output dimension.
8444 __isl_give isl_union_pw_multi_aff *
8445 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8446 __isl_take isl_multi_union_pw_aff *mupa)
8448 int i, n;
8449 isl_space *space;
8450 isl_union_pw_multi_aff *upma;
8451 isl_union_pw_aff *upa;
8453 if (!mupa)
8454 return NULL;
8456 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8457 if (n == 0)
8458 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8459 "cannot determine domain of zero-dimensional "
8460 "isl_multi_union_pw_aff", goto error);
8462 space = isl_multi_union_pw_aff_get_space(mupa);
8463 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8464 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8466 for (i = 1; i < n; ++i) {
8467 isl_union_pw_multi_aff *upma_i;
8469 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8470 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8471 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8474 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8476 isl_multi_union_pw_aff_free(mupa);
8477 return upma;
8478 error:
8479 isl_multi_union_pw_aff_free(mupa);
8480 return NULL;
8483 /* Intersect the range of "mupa" with "range".
8484 * That is, keep only those domain elements that have a function value
8485 * in "range".
8487 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8488 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8490 isl_union_pw_multi_aff *upma;
8491 isl_union_set *domain;
8492 isl_space *space;
8493 int n;
8494 int match;
8496 if (!mupa || !range)
8497 goto error;
8499 space = isl_set_get_space(range);
8500 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8501 space, isl_dim_set);
8502 isl_space_free(space);
8503 if (match < 0)
8504 goto error;
8505 if (!match)
8506 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8507 "space don't match", goto error);
8508 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8509 if (n == 0)
8510 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8511 "cannot intersect range of zero-dimensional "
8512 "isl_multi_union_pw_aff", goto error);
8514 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8515 isl_multi_union_pw_aff_copy(mupa));
8516 domain = isl_union_set_from_set(range);
8517 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8518 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8520 return mupa;
8521 error:
8522 isl_multi_union_pw_aff_free(mupa);
8523 isl_set_free(range);
8524 return NULL;
8527 /* Return the shared domain of the elements of "mupa".
8529 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8530 __isl_take isl_multi_union_pw_aff *mupa)
8532 int i, n;
8533 isl_union_pw_aff *upa;
8534 isl_union_set *dom;
8536 if (!mupa)
8537 return NULL;
8539 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8540 if (n == 0)
8541 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8542 "cannot determine domain", goto error);
8544 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8545 dom = isl_union_pw_aff_domain(upa);
8546 for (i = 1; i < n; ++i) {
8547 isl_union_set *dom_i;
8549 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8550 dom_i = isl_union_pw_aff_domain(upa);
8551 dom = isl_union_set_intersect(dom, dom_i);
8554 isl_multi_union_pw_aff_free(mupa);
8555 return dom;
8556 error:
8557 isl_multi_union_pw_aff_free(mupa);
8558 return NULL;
8561 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8562 * In particular, the spaces have been aligned.
8563 * The result is defined over the shared domain of the elements of "mupa"
8565 * We first extract the parametric constant part of "aff" and
8566 * define that over the shared domain.
8567 * Then we iterate over all input dimensions of "aff" and add the corresponding
8568 * multiples of the elements of "mupa".
8569 * Finally, we consider the integer divisions, calling the function
8570 * recursively to obtain an isl_union_pw_aff corresponding to the
8571 * integer division argument.
8573 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8574 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8576 int i, n_in, n_div;
8577 isl_union_pw_aff *upa;
8578 isl_union_set *uset;
8579 isl_val *v;
8580 isl_aff *cst;
8582 n_in = isl_aff_dim(aff, isl_dim_in);
8583 n_div = isl_aff_dim(aff, isl_dim_div);
8585 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8586 cst = isl_aff_copy(aff);
8587 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8588 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8589 cst = isl_aff_project_domain_on_params(cst);
8590 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8592 for (i = 0; i < n_in; ++i) {
8593 isl_union_pw_aff *upa_i;
8595 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8596 continue;
8597 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8598 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8599 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8600 upa = isl_union_pw_aff_add(upa, upa_i);
8603 for (i = 0; i < n_div; ++i) {
8604 isl_aff *div;
8605 isl_union_pw_aff *upa_i;
8607 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8608 continue;
8609 div = isl_aff_get_div(aff, i);
8610 upa_i = multi_union_pw_aff_apply_aff(
8611 isl_multi_union_pw_aff_copy(mupa), div);
8612 upa_i = isl_union_pw_aff_floor(upa_i);
8613 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8614 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8615 upa = isl_union_pw_aff_add(upa, upa_i);
8618 isl_multi_union_pw_aff_free(mupa);
8619 isl_aff_free(aff);
8621 return upa;
8624 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8625 * with the domain of "aff".
8626 * Furthermore, the dimension of this space needs to be greater than zero.
8627 * The result is defined over the shared domain of the elements of "mupa"
8629 * We perform these checks and then hand over control to
8630 * multi_union_pw_aff_apply_aff.
8632 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8633 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8635 isl_space *space1, *space2;
8636 int equal;
8638 mupa = isl_multi_union_pw_aff_align_params(mupa,
8639 isl_aff_get_space(aff));
8640 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8641 if (!mupa || !aff)
8642 goto error;
8644 space1 = isl_multi_union_pw_aff_get_space(mupa);
8645 space2 = isl_aff_get_domain_space(aff);
8646 equal = isl_space_is_equal(space1, space2);
8647 isl_space_free(space1);
8648 isl_space_free(space2);
8649 if (equal < 0)
8650 goto error;
8651 if (!equal)
8652 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8653 "spaces don't match", goto error);
8654 if (isl_aff_dim(aff, isl_dim_in) == 0)
8655 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8656 "cannot determine domains", goto error);
8658 return multi_union_pw_aff_apply_aff(mupa, aff);
8659 error:
8660 isl_multi_union_pw_aff_free(mupa);
8661 isl_aff_free(aff);
8662 return NULL;
8665 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8666 * with the domain of "ma".
8667 * Furthermore, the dimension of this space needs to be greater than zero,
8668 * unless the dimension of the target space of "ma" is also zero.
8669 * The result is defined over the shared domain of the elements of "mupa"
8671 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8672 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8674 isl_space *space1, *space2;
8675 isl_multi_union_pw_aff *res;
8676 int equal;
8677 int i, n_out;
8679 mupa = isl_multi_union_pw_aff_align_params(mupa,
8680 isl_multi_aff_get_space(ma));
8681 ma = isl_multi_aff_align_params(ma,
8682 isl_multi_union_pw_aff_get_space(mupa));
8683 if (!mupa || !ma)
8684 goto error;
8686 space1 = isl_multi_union_pw_aff_get_space(mupa);
8687 space2 = isl_multi_aff_get_domain_space(ma);
8688 equal = isl_space_is_equal(space1, space2);
8689 isl_space_free(space1);
8690 isl_space_free(space2);
8691 if (equal < 0)
8692 goto error;
8693 if (!equal)
8694 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8695 "spaces don't match", goto error);
8696 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8697 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8698 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8699 "cannot determine domains", goto error);
8701 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8702 res = isl_multi_union_pw_aff_alloc(space1);
8704 for (i = 0; i < n_out; ++i) {
8705 isl_aff *aff;
8706 isl_union_pw_aff *upa;
8708 aff = isl_multi_aff_get_aff(ma, i);
8709 upa = multi_union_pw_aff_apply_aff(
8710 isl_multi_union_pw_aff_copy(mupa), aff);
8711 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8714 isl_multi_aff_free(ma);
8715 isl_multi_union_pw_aff_free(mupa);
8716 return res;
8717 error:
8718 isl_multi_union_pw_aff_free(mupa);
8719 isl_multi_aff_free(ma);
8720 return NULL;
8723 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8724 * with the domain of "pa".
8725 * Furthermore, the dimension of this space needs to be greater than zero.
8726 * The result is defined over the shared domain of the elements of "mupa"
8728 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8729 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8731 int i;
8732 int equal;
8733 isl_space *space, *space2;
8734 isl_union_pw_aff *upa;
8736 mupa = isl_multi_union_pw_aff_align_params(mupa,
8737 isl_pw_aff_get_space(pa));
8738 pa = isl_pw_aff_align_params(pa,
8739 isl_multi_union_pw_aff_get_space(mupa));
8740 if (!mupa || !pa)
8741 goto error;
8743 space = isl_multi_union_pw_aff_get_space(mupa);
8744 space2 = isl_pw_aff_get_domain_space(pa);
8745 equal = isl_space_is_equal(space, space2);
8746 isl_space_free(space);
8747 isl_space_free(space2);
8748 if (equal < 0)
8749 goto error;
8750 if (!equal)
8751 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8752 "spaces don't match", goto error);
8753 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8754 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8755 "cannot determine domains", goto error);
8757 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8758 upa = isl_union_pw_aff_empty(space);
8760 for (i = 0; i < pa->n; ++i) {
8761 isl_aff *aff;
8762 isl_set *domain;
8763 isl_multi_union_pw_aff *mupa_i;
8764 isl_union_pw_aff *upa_i;
8766 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8767 domain = isl_set_copy(pa->p[i].set);
8768 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8769 aff = isl_aff_copy(pa->p[i].aff);
8770 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8771 upa = isl_union_pw_aff_union_add(upa, upa_i);
8774 isl_multi_union_pw_aff_free(mupa);
8775 isl_pw_aff_free(pa);
8776 return upa;
8777 error:
8778 isl_multi_union_pw_aff_free(mupa);
8779 isl_pw_aff_free(pa);
8780 return NULL;
8783 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8784 * with the domain of "pma".
8785 * Furthermore, the dimension of this space needs to be greater than zero,
8786 * unless the dimension of the target space of "pma" is also zero.
8787 * The result is defined over the shared domain of the elements of "mupa"
8789 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8790 __isl_take isl_multi_union_pw_aff *mupa,
8791 __isl_take isl_pw_multi_aff *pma)
8793 isl_space *space1, *space2;
8794 isl_multi_union_pw_aff *res;
8795 int equal;
8796 int i, n_out;
8798 mupa = isl_multi_union_pw_aff_align_params(mupa,
8799 isl_pw_multi_aff_get_space(pma));
8800 pma = isl_pw_multi_aff_align_params(pma,
8801 isl_multi_union_pw_aff_get_space(mupa));
8802 if (!mupa || !pma)
8803 goto error;
8805 space1 = isl_multi_union_pw_aff_get_space(mupa);
8806 space2 = isl_pw_multi_aff_get_domain_space(pma);
8807 equal = isl_space_is_equal(space1, space2);
8808 isl_space_free(space1);
8809 isl_space_free(space2);
8810 if (equal < 0)
8811 goto error;
8812 if (!equal)
8813 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8814 "spaces don't match", goto error);
8815 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8816 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8817 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8818 "cannot determine domains", goto error);
8820 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8821 res = isl_multi_union_pw_aff_alloc(space1);
8823 for (i = 0; i < n_out; ++i) {
8824 isl_pw_aff *pa;
8825 isl_union_pw_aff *upa;
8827 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8828 upa = isl_multi_union_pw_aff_apply_pw_aff(
8829 isl_multi_union_pw_aff_copy(mupa), pa);
8830 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8833 isl_pw_multi_aff_free(pma);
8834 isl_multi_union_pw_aff_free(mupa);
8835 return res;
8836 error:
8837 isl_multi_union_pw_aff_free(mupa);
8838 isl_pw_multi_aff_free(pma);
8839 return NULL;
8842 /* Compute the pullback of "mupa" by the function represented by "upma".
8843 * In other words, plug in "upma" in "mupa". The result contains
8844 * expressions defined over the domain space of "upma".
8846 * Run over all elements of "mupa" and plug in "upma" in each of them.
8848 __isl_give isl_multi_union_pw_aff *
8849 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8850 __isl_take isl_multi_union_pw_aff *mupa,
8851 __isl_take isl_union_pw_multi_aff *upma)
8853 int i, n;
8855 mupa = isl_multi_union_pw_aff_align_params(mupa,
8856 isl_union_pw_multi_aff_get_space(upma));
8857 upma = isl_union_pw_multi_aff_align_params(upma,
8858 isl_multi_union_pw_aff_get_space(mupa));
8859 if (!mupa || !upma)
8860 goto error;
8862 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8863 for (i = 0; i < n; ++i) {
8864 isl_union_pw_aff *upa;
8866 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8867 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8868 isl_union_pw_multi_aff_copy(upma));
8869 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8872 isl_union_pw_multi_aff_free(upma);
8873 return mupa;
8874 error:
8875 isl_multi_union_pw_aff_free(mupa);
8876 isl_union_pw_multi_aff_free(upma);
8877 return NULL;
8880 /* Extract the sequence of elements in "mupa" with domain space "space"
8881 * (ignoring parameters).
8883 * For the elements of "mupa" that are not defined on the specified space,
8884 * the corresponding element in the result is empty.
8886 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8887 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8889 int i, n;
8890 isl_bool equal_params;
8891 isl_space *space_mpa = NULL;
8892 isl_multi_pw_aff *mpa;
8894 if (!mupa || !space)
8895 goto error;
8897 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8898 equal_params = isl_space_has_equal_params(space_mpa, space);
8899 if (equal_params < 0)
8900 goto error;
8901 if (!equal_params) {
8902 space = isl_space_drop_dims(space, isl_dim_param,
8903 0, isl_space_dim(space, isl_dim_param));
8904 space = isl_space_align_params(space,
8905 isl_space_copy(space_mpa));
8906 if (!space)
8907 goto error;
8909 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8910 space_mpa);
8911 mpa = isl_multi_pw_aff_alloc(space_mpa);
8913 space = isl_space_from_domain(space);
8914 space = isl_space_add_dims(space, isl_dim_out, 1);
8915 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8916 for (i = 0; i < n; ++i) {
8917 isl_union_pw_aff *upa;
8918 isl_pw_aff *pa;
8920 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8921 pa = isl_union_pw_aff_extract_pw_aff(upa,
8922 isl_space_copy(space));
8923 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8924 isl_union_pw_aff_free(upa);
8927 isl_space_free(space);
8928 return mpa;
8929 error:
8930 isl_space_free(space_mpa);
8931 isl_space_free(space);
8932 return NULL;