isl_scheduler.c: fix whitespace
[isl.git] / isl_aff.c
blob2a41d011300da44e5a711d3fc44615af23c64292
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 reodering.
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 isl_seq_cpy(res->el, vec->el, 2);
420 isl_seq_clr(res->el + 2, res->size - 2);
421 for (i = 0; i < r->len; ++i)
422 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
424 isl_reordering_free(r);
425 isl_vec_free(vec);
426 return res;
427 error:
428 isl_vec_free(vec);
429 isl_reordering_free(r);
430 return NULL;
433 /* Reorder the dimensions of the domain of "aff" according
434 * to the given reordering.
436 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
437 __isl_take isl_reordering *r)
439 aff = isl_aff_cow(aff);
440 if (!aff)
441 goto error;
443 r = isl_reordering_extend(r, aff->ls->div->n_row);
444 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
445 aff->ls->div->n_row);
446 aff->ls = isl_local_space_realign(aff->ls, r);
448 if (!aff->v || !aff->ls)
449 return isl_aff_free(aff);
451 return aff;
452 error:
453 isl_aff_free(aff);
454 isl_reordering_free(r);
455 return NULL;
458 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
459 __isl_take isl_space *model)
461 if (!aff || !model)
462 goto error;
464 if (!isl_space_match(aff->ls->dim, isl_dim_param,
465 model, isl_dim_param)) {
466 isl_reordering *exp;
468 model = isl_space_drop_dims(model, isl_dim_in,
469 0, isl_space_dim(model, isl_dim_in));
470 model = isl_space_drop_dims(model, isl_dim_out,
471 0, isl_space_dim(model, isl_dim_out));
472 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
473 exp = isl_reordering_extend_space(exp,
474 isl_aff_get_domain_space(aff));
475 aff = isl_aff_realign_domain(aff, exp);
478 isl_space_free(model);
479 return aff;
480 error:
481 isl_space_free(model);
482 isl_aff_free(aff);
483 return NULL;
486 /* Is "aff" obviously equal to zero?
488 * If the denominator is zero, then "aff" is not equal to zero.
490 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
492 if (!aff)
493 return isl_bool_error;
495 if (isl_int_is_zero(aff->v->el[0]))
496 return isl_bool_false;
497 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
500 /* Does "aff" represent NaN?
502 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
504 if (!aff)
505 return isl_bool_error;
507 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
510 /* Does "pa" involve any NaNs?
512 isl_bool isl_pw_aff_involves_nan(__isl_keep isl_pw_aff *pa)
514 int i;
516 if (!pa)
517 return isl_bool_error;
518 if (pa->n == 0)
519 return isl_bool_false;
521 for (i = 0; i < pa->n; ++i) {
522 isl_bool is_nan = isl_aff_is_nan(pa->p[i].aff);
523 if (is_nan < 0 || is_nan)
524 return is_nan;
527 return isl_bool_false;
530 /* Are "aff1" and "aff2" obviously equal?
532 * NaN is not equal to anything, not even to another NaN.
534 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
535 __isl_keep isl_aff *aff2)
537 isl_bool equal;
539 if (!aff1 || !aff2)
540 return isl_bool_error;
542 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
543 return isl_bool_false;
545 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
546 if (equal < 0 || !equal)
547 return equal;
549 return isl_vec_is_equal(aff1->v, aff2->v);
552 /* Return the common denominator of "aff" in "v".
554 * We cannot return anything meaningful in case of a NaN.
556 int isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
558 if (!aff)
559 return -1;
560 if (isl_aff_is_nan(aff))
561 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
562 "cannot get denominator of NaN", return -1);
563 isl_int_set(*v, aff->v->el[0]);
564 return 0;
567 /* Return the common denominator of "aff".
569 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
571 isl_ctx *ctx;
573 if (!aff)
574 return NULL;
576 ctx = isl_aff_get_ctx(aff);
577 if (isl_aff_is_nan(aff))
578 return isl_val_nan(ctx);
579 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
582 /* Return the constant term of "aff" in "v".
584 * We cannot return anything meaningful in case of a NaN.
586 int isl_aff_get_constant(__isl_keep isl_aff *aff, isl_int *v)
588 if (!aff)
589 return -1;
590 if (isl_aff_is_nan(aff))
591 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
592 "cannot get constant term of NaN", return -1);
593 isl_int_set(*v, aff->v->el[1]);
594 return 0;
597 /* Return the constant term of "aff".
599 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
601 isl_ctx *ctx;
602 isl_val *v;
604 if (!aff)
605 return NULL;
607 ctx = isl_aff_get_ctx(aff);
608 if (isl_aff_is_nan(aff))
609 return isl_val_nan(ctx);
610 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
611 return isl_val_normalize(v);
614 /* Return the coefficient of the variable of type "type" at position "pos"
615 * of "aff" in "v".
617 * We cannot return anything meaningful in case of a NaN.
619 int isl_aff_get_coefficient(__isl_keep isl_aff *aff,
620 enum isl_dim_type type, int pos, isl_int *v)
622 if (!aff)
623 return -1;
625 if (type == isl_dim_out)
626 isl_die(aff->v->ctx, isl_error_invalid,
627 "output/set dimension does not have a coefficient",
628 return -1);
629 if (type == isl_dim_in)
630 type = isl_dim_set;
632 if (pos >= isl_local_space_dim(aff->ls, type))
633 isl_die(aff->v->ctx, isl_error_invalid,
634 "position out of bounds", return -1);
636 if (isl_aff_is_nan(aff))
637 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
638 "cannot get coefficient of NaN", return -1);
639 pos += isl_local_space_offset(aff->ls, type);
640 isl_int_set(*v, aff->v->el[1 + pos]);
642 return 0;
645 /* Return the coefficient of the variable of type "type" at position "pos"
646 * of "aff".
648 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
649 enum isl_dim_type type, int pos)
651 isl_ctx *ctx;
652 isl_val *v;
654 if (!aff)
655 return NULL;
657 ctx = isl_aff_get_ctx(aff);
658 if (type == isl_dim_out)
659 isl_die(ctx, isl_error_invalid,
660 "output/set dimension does not have a coefficient",
661 return NULL);
662 if (type == isl_dim_in)
663 type = isl_dim_set;
665 if (pos >= isl_local_space_dim(aff->ls, type))
666 isl_die(ctx, isl_error_invalid,
667 "position out of bounds", return NULL);
669 if (isl_aff_is_nan(aff))
670 return isl_val_nan(ctx);
671 pos += isl_local_space_offset(aff->ls, type);
672 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
673 return isl_val_normalize(v);
676 /* Return the sign of the coefficient of the variable of type "type"
677 * at position "pos" of "aff".
679 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
680 int pos)
682 isl_ctx *ctx;
684 if (!aff)
685 return 0;
687 ctx = isl_aff_get_ctx(aff);
688 if (type == isl_dim_out)
689 isl_die(ctx, isl_error_invalid,
690 "output/set dimension does not have a coefficient",
691 return 0);
692 if (type == isl_dim_in)
693 type = isl_dim_set;
695 if (pos >= isl_local_space_dim(aff->ls, type))
696 isl_die(ctx, isl_error_invalid,
697 "position out of bounds", return 0);
699 pos += isl_local_space_offset(aff->ls, type);
700 return isl_int_sgn(aff->v->el[1 + pos]);
703 /* Replace the denominator of "aff" by "v".
705 * A NaN is unaffected by this operation.
707 __isl_give isl_aff *isl_aff_set_denominator(__isl_take isl_aff *aff, isl_int v)
709 if (!aff)
710 return NULL;
711 if (isl_aff_is_nan(aff))
712 return aff;
713 aff = isl_aff_cow(aff);
714 if (!aff)
715 return NULL;
717 aff->v = isl_vec_cow(aff->v);
718 if (!aff->v)
719 return isl_aff_free(aff);
721 isl_int_set(aff->v->el[0], v);
723 return aff;
726 /* Replace the numerator of the constant term of "aff" by "v".
728 * A NaN is unaffected by this operation.
730 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
732 if (!aff)
733 return NULL;
734 if (isl_aff_is_nan(aff))
735 return aff;
736 aff = isl_aff_cow(aff);
737 if (!aff)
738 return NULL;
740 aff->v = isl_vec_cow(aff->v);
741 if (!aff->v)
742 return isl_aff_free(aff);
744 isl_int_set(aff->v->el[1], v);
746 return aff;
749 /* Replace the constant term of "aff" by "v".
751 * A NaN is unaffected by this operation.
753 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
754 __isl_take isl_val *v)
756 if (!aff || !v)
757 goto error;
759 if (isl_aff_is_nan(aff)) {
760 isl_val_free(v);
761 return aff;
764 if (!isl_val_is_rat(v))
765 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
766 "expecting rational value", goto error);
768 if (isl_int_eq(aff->v->el[1], v->n) &&
769 isl_int_eq(aff->v->el[0], v->d)) {
770 isl_val_free(v);
771 return aff;
774 aff = isl_aff_cow(aff);
775 if (!aff)
776 goto error;
777 aff->v = isl_vec_cow(aff->v);
778 if (!aff->v)
779 goto error;
781 if (isl_int_eq(aff->v->el[0], v->d)) {
782 isl_int_set(aff->v->el[1], v->n);
783 } else if (isl_int_is_one(v->d)) {
784 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
785 } else {
786 isl_seq_scale(aff->v->el + 1,
787 aff->v->el + 1, v->d, aff->v->size - 1);
788 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
789 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
790 aff->v = isl_vec_normalize(aff->v);
791 if (!aff->v)
792 goto error;
795 isl_val_free(v);
796 return aff;
797 error:
798 isl_aff_free(aff);
799 isl_val_free(v);
800 return NULL;
803 /* Add "v" to the constant term of "aff".
805 * A NaN is unaffected by this operation.
807 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
809 if (isl_int_is_zero(v))
810 return aff;
812 if (!aff)
813 return NULL;
814 if (isl_aff_is_nan(aff))
815 return aff;
816 aff = isl_aff_cow(aff);
817 if (!aff)
818 return NULL;
820 aff->v = isl_vec_cow(aff->v);
821 if (!aff->v)
822 return isl_aff_free(aff);
824 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
826 return aff;
829 /* Add "v" to the constant term of "aff".
831 * A NaN is unaffected by this operation.
833 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
834 __isl_take isl_val *v)
836 if (!aff || !v)
837 goto error;
839 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
840 isl_val_free(v);
841 return aff;
844 if (!isl_val_is_rat(v))
845 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
846 "expecting rational value", goto error);
848 aff = isl_aff_cow(aff);
849 if (!aff)
850 goto error;
852 aff->v = isl_vec_cow(aff->v);
853 if (!aff->v)
854 goto error;
856 if (isl_int_is_one(v->d)) {
857 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
858 } else if (isl_int_eq(aff->v->el[0], v->d)) {
859 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
860 aff->v = isl_vec_normalize(aff->v);
861 if (!aff->v)
862 goto error;
863 } else {
864 isl_seq_scale(aff->v->el + 1,
865 aff->v->el + 1, v->d, aff->v->size - 1);
866 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
867 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
868 aff->v = isl_vec_normalize(aff->v);
869 if (!aff->v)
870 goto error;
873 isl_val_free(v);
874 return aff;
875 error:
876 isl_aff_free(aff);
877 isl_val_free(v);
878 return NULL;
881 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
883 isl_int t;
885 isl_int_init(t);
886 isl_int_set_si(t, v);
887 aff = isl_aff_add_constant(aff, t);
888 isl_int_clear(t);
890 return aff;
893 /* Add "v" to the numerator of the constant term of "aff".
895 * A NaN is unaffected by this operation.
897 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
899 if (isl_int_is_zero(v))
900 return aff;
902 if (!aff)
903 return NULL;
904 if (isl_aff_is_nan(aff))
905 return aff;
906 aff = isl_aff_cow(aff);
907 if (!aff)
908 return NULL;
910 aff->v = isl_vec_cow(aff->v);
911 if (!aff->v)
912 return isl_aff_free(aff);
914 isl_int_add(aff->v->el[1], aff->v->el[1], v);
916 return aff;
919 /* Add "v" to the numerator of the constant term of "aff".
921 * A NaN is unaffected by this operation.
923 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
925 isl_int t;
927 if (v == 0)
928 return aff;
930 isl_int_init(t);
931 isl_int_set_si(t, v);
932 aff = isl_aff_add_constant_num(aff, t);
933 isl_int_clear(t);
935 return aff;
938 /* Replace the numerator of the constant term of "aff" by "v".
940 * A NaN is unaffected by this operation.
942 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
944 if (!aff)
945 return NULL;
946 if (isl_aff_is_nan(aff))
947 return aff;
948 aff = isl_aff_cow(aff);
949 if (!aff)
950 return NULL;
952 aff->v = isl_vec_cow(aff->v);
953 if (!aff->v)
954 return isl_aff_free(aff);
956 isl_int_set_si(aff->v->el[1], v);
958 return aff;
961 /* Replace the numerator of the coefficient of the variable of type "type"
962 * at position "pos" of "aff" by "v".
964 * A NaN is unaffected by this operation.
966 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
967 enum isl_dim_type type, int pos, isl_int v)
969 if (!aff)
970 return NULL;
972 if (type == isl_dim_out)
973 isl_die(aff->v->ctx, isl_error_invalid,
974 "output/set dimension does not have a coefficient",
975 return isl_aff_free(aff));
976 if (type == isl_dim_in)
977 type = isl_dim_set;
979 if (pos >= isl_local_space_dim(aff->ls, type))
980 isl_die(aff->v->ctx, isl_error_invalid,
981 "position out of bounds", return isl_aff_free(aff));
983 if (isl_aff_is_nan(aff))
984 return aff;
985 aff = isl_aff_cow(aff);
986 if (!aff)
987 return NULL;
989 aff->v = isl_vec_cow(aff->v);
990 if (!aff->v)
991 return isl_aff_free(aff);
993 pos += isl_local_space_offset(aff->ls, type);
994 isl_int_set(aff->v->el[1 + pos], v);
996 return aff;
999 /* Replace the numerator of the coefficient of the variable of type "type"
1000 * at position "pos" of "aff" by "v".
1002 * A NaN is unaffected by this operation.
1004 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1005 enum isl_dim_type type, int pos, int v)
1007 if (!aff)
1008 return NULL;
1010 if (type == isl_dim_out)
1011 isl_die(aff->v->ctx, isl_error_invalid,
1012 "output/set dimension does not have a coefficient",
1013 return isl_aff_free(aff));
1014 if (type == isl_dim_in)
1015 type = isl_dim_set;
1017 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1018 isl_die(aff->v->ctx, isl_error_invalid,
1019 "position out of bounds", return isl_aff_free(aff));
1021 if (isl_aff_is_nan(aff))
1022 return aff;
1023 pos += isl_local_space_offset(aff->ls, type);
1024 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1025 return aff;
1027 aff = isl_aff_cow(aff);
1028 if (!aff)
1029 return NULL;
1031 aff->v = isl_vec_cow(aff->v);
1032 if (!aff->v)
1033 return isl_aff_free(aff);
1035 isl_int_set_si(aff->v->el[1 + pos], v);
1037 return aff;
1040 /* Replace the coefficient of the variable of type "type" at position "pos"
1041 * of "aff" by "v".
1043 * A NaN is unaffected by this operation.
1045 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1046 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1048 if (!aff || !v)
1049 goto error;
1051 if (type == isl_dim_out)
1052 isl_die(aff->v->ctx, isl_error_invalid,
1053 "output/set dimension does not have a coefficient",
1054 goto error);
1055 if (type == isl_dim_in)
1056 type = isl_dim_set;
1058 if (pos >= isl_local_space_dim(aff->ls, type))
1059 isl_die(aff->v->ctx, isl_error_invalid,
1060 "position out of bounds", goto error);
1062 if (isl_aff_is_nan(aff)) {
1063 isl_val_free(v);
1064 return aff;
1066 if (!isl_val_is_rat(v))
1067 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1068 "expecting rational value", goto error);
1070 pos += isl_local_space_offset(aff->ls, type);
1071 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1072 isl_int_eq(aff->v->el[0], v->d)) {
1073 isl_val_free(v);
1074 return aff;
1077 aff = isl_aff_cow(aff);
1078 if (!aff)
1079 goto error;
1080 aff->v = isl_vec_cow(aff->v);
1081 if (!aff->v)
1082 goto error;
1084 if (isl_int_eq(aff->v->el[0], v->d)) {
1085 isl_int_set(aff->v->el[1 + pos], v->n);
1086 } else if (isl_int_is_one(v->d)) {
1087 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1088 } else {
1089 isl_seq_scale(aff->v->el + 1,
1090 aff->v->el + 1, v->d, aff->v->size - 1);
1091 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1092 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1093 aff->v = isl_vec_normalize(aff->v);
1094 if (!aff->v)
1095 goto error;
1098 isl_val_free(v);
1099 return aff;
1100 error:
1101 isl_aff_free(aff);
1102 isl_val_free(v);
1103 return NULL;
1106 /* Add "v" to the coefficient of the variable of type "type"
1107 * at position "pos" of "aff".
1109 * A NaN is unaffected by this operation.
1111 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1112 enum isl_dim_type type, int pos, isl_int v)
1114 if (!aff)
1115 return NULL;
1117 if (type == isl_dim_out)
1118 isl_die(aff->v->ctx, isl_error_invalid,
1119 "output/set dimension does not have a coefficient",
1120 return isl_aff_free(aff));
1121 if (type == isl_dim_in)
1122 type = isl_dim_set;
1124 if (pos >= isl_local_space_dim(aff->ls, type))
1125 isl_die(aff->v->ctx, isl_error_invalid,
1126 "position out of bounds", return isl_aff_free(aff));
1128 if (isl_aff_is_nan(aff))
1129 return aff;
1130 aff = isl_aff_cow(aff);
1131 if (!aff)
1132 return NULL;
1134 aff->v = isl_vec_cow(aff->v);
1135 if (!aff->v)
1136 return isl_aff_free(aff);
1138 pos += isl_local_space_offset(aff->ls, type);
1139 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1141 return aff;
1144 /* Add "v" to the coefficient of the variable of type "type"
1145 * at position "pos" of "aff".
1147 * A NaN is unaffected by this operation.
1149 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1150 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1152 if (!aff || !v)
1153 goto error;
1155 if (isl_val_is_zero(v)) {
1156 isl_val_free(v);
1157 return aff;
1160 if (type == isl_dim_out)
1161 isl_die(aff->v->ctx, isl_error_invalid,
1162 "output/set dimension does not have a coefficient",
1163 goto error);
1164 if (type == isl_dim_in)
1165 type = isl_dim_set;
1167 if (pos >= isl_local_space_dim(aff->ls, type))
1168 isl_die(aff->v->ctx, isl_error_invalid,
1169 "position out of bounds", goto error);
1171 if (isl_aff_is_nan(aff)) {
1172 isl_val_free(v);
1173 return aff;
1175 if (!isl_val_is_rat(v))
1176 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1177 "expecting rational value", goto error);
1179 aff = isl_aff_cow(aff);
1180 if (!aff)
1181 goto error;
1183 aff->v = isl_vec_cow(aff->v);
1184 if (!aff->v)
1185 goto error;
1187 pos += isl_local_space_offset(aff->ls, type);
1188 if (isl_int_is_one(v->d)) {
1189 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1190 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1191 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1192 aff->v = isl_vec_normalize(aff->v);
1193 if (!aff->v)
1194 goto error;
1195 } else {
1196 isl_seq_scale(aff->v->el + 1,
1197 aff->v->el + 1, v->d, aff->v->size - 1);
1198 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1199 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1200 aff->v = isl_vec_normalize(aff->v);
1201 if (!aff->v)
1202 goto error;
1205 isl_val_free(v);
1206 return aff;
1207 error:
1208 isl_aff_free(aff);
1209 isl_val_free(v);
1210 return NULL;
1213 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1214 enum isl_dim_type type, int pos, int v)
1216 isl_int t;
1218 isl_int_init(t);
1219 isl_int_set_si(t, v);
1220 aff = isl_aff_add_coefficient(aff, type, pos, t);
1221 isl_int_clear(t);
1223 return aff;
1226 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1228 if (!aff)
1229 return NULL;
1231 return isl_local_space_get_div(aff->ls, pos);
1234 /* Return the negation of "aff".
1236 * As a special case, -NaN = NaN.
1238 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1240 if (!aff)
1241 return NULL;
1242 if (isl_aff_is_nan(aff))
1243 return aff;
1244 aff = isl_aff_cow(aff);
1245 if (!aff)
1246 return NULL;
1247 aff->v = isl_vec_cow(aff->v);
1248 if (!aff->v)
1249 return isl_aff_free(aff);
1251 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1253 return aff;
1256 /* Remove divs from the local space that do not appear in the affine
1257 * expression.
1258 * We currently only remove divs at the end.
1259 * Some intermediate divs may also not appear directly in the affine
1260 * expression, but we would also need to check that no other divs are
1261 * defined in terms of them.
1263 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1265 int pos;
1266 int off;
1267 int n;
1269 if (!aff)
1270 return NULL;
1272 n = isl_local_space_dim(aff->ls, isl_dim_div);
1273 off = isl_local_space_offset(aff->ls, isl_dim_div);
1275 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1276 if (pos == n)
1277 return aff;
1279 aff = isl_aff_cow(aff);
1280 if (!aff)
1281 return NULL;
1283 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1284 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1285 if (!aff->ls || !aff->v)
1286 return isl_aff_free(aff);
1288 return aff;
1291 /* Given two affine expressions "p" of length p_len (including the
1292 * denominator and the constant term) and "subs" of length subs_len,
1293 * plug in "subs" for the variable at position "pos".
1294 * The variables of "subs" and "p" are assumed to match up to subs_len,
1295 * but "p" may have additional variables.
1296 * "v" is an initialized isl_int that can be used internally.
1298 * In particular, if "p" represents the expression
1300 * (a i + g)/m
1302 * with i the variable at position "pos" and "subs" represents the expression
1304 * f/d
1306 * then the result represents the expression
1308 * (a f + d g)/(m d)
1311 void isl_seq_substitute(isl_int *p, int pos, isl_int *subs,
1312 int p_len, int subs_len, isl_int v)
1314 isl_int_set(v, p[1 + pos]);
1315 isl_int_set_si(p[1 + pos], 0);
1316 isl_seq_combine(p + 1, subs[0], p + 1, v, subs + 1, subs_len - 1);
1317 isl_seq_scale(p + subs_len, p + subs_len, subs[0], p_len - subs_len);
1318 isl_int_mul(p[0], p[0], subs[0]);
1321 /* Look for any divs in the aff->ls with a denominator equal to one
1322 * and plug them into the affine expression and any subsequent divs
1323 * that may reference the div.
1325 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1327 int i, n;
1328 int len;
1329 isl_int v;
1330 isl_vec *vec;
1331 isl_local_space *ls;
1332 unsigned pos;
1334 if (!aff)
1335 return NULL;
1337 n = isl_local_space_dim(aff->ls, isl_dim_div);
1338 len = aff->v->size;
1339 for (i = 0; i < n; ++i) {
1340 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1341 continue;
1342 ls = isl_local_space_copy(aff->ls);
1343 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1344 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1345 vec = isl_vec_copy(aff->v);
1346 vec = isl_vec_cow(vec);
1347 if (!ls || !vec)
1348 goto error;
1350 isl_int_init(v);
1352 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1353 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1354 len, len, v);
1356 isl_int_clear(v);
1358 isl_vec_free(aff->v);
1359 aff->v = vec;
1360 isl_local_space_free(aff->ls);
1361 aff->ls = ls;
1364 return aff;
1365 error:
1366 isl_vec_free(vec);
1367 isl_local_space_free(ls);
1368 return isl_aff_free(aff);
1371 /* Look for any divs j that appear with a unit coefficient inside
1372 * the definitions of other divs i and plug them into the definitions
1373 * of the divs i.
1375 * In particular, an expression of the form
1377 * floor((f(..) + floor(g(..)/n))/m)
1379 * is simplified to
1381 * floor((n * f(..) + g(..))/(n * m))
1383 * This simplification is correct because we can move the expression
1384 * f(..) into the inner floor in the original expression to obtain
1386 * floor(floor((n * f(..) + g(..))/n)/m)
1388 * from which we can derive the simplified expression.
1390 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1392 int i, j, n;
1393 int off;
1395 if (!aff)
1396 return NULL;
1398 n = isl_local_space_dim(aff->ls, isl_dim_div);
1399 off = isl_local_space_offset(aff->ls, isl_dim_div);
1400 for (i = 1; i < n; ++i) {
1401 for (j = 0; j < i; ++j) {
1402 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1403 continue;
1404 aff->ls = isl_local_space_substitute_seq(aff->ls,
1405 isl_dim_div, j, aff->ls->div->row[j],
1406 aff->v->size, i, 1);
1407 if (!aff->ls)
1408 return isl_aff_free(aff);
1412 return aff;
1415 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1417 * Even though this function is only called on isl_affs with a single
1418 * reference, we are careful to only change aff->v and aff->ls together.
1420 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1422 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1423 isl_local_space *ls;
1424 isl_vec *v;
1426 ls = isl_local_space_copy(aff->ls);
1427 ls = isl_local_space_swap_div(ls, a, b);
1428 v = isl_vec_copy(aff->v);
1429 v = isl_vec_cow(v);
1430 if (!ls || !v)
1431 goto error;
1433 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1434 isl_vec_free(aff->v);
1435 aff->v = v;
1436 isl_local_space_free(aff->ls);
1437 aff->ls = ls;
1439 return aff;
1440 error:
1441 isl_vec_free(v);
1442 isl_local_space_free(ls);
1443 return isl_aff_free(aff);
1446 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1448 * We currently do not actually remove div "b", but simply add its
1449 * coefficient to that of "a" and then zero it out.
1451 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1453 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1455 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1456 return aff;
1458 aff->v = isl_vec_cow(aff->v);
1459 if (!aff->v)
1460 return isl_aff_free(aff);
1462 isl_int_add(aff->v->el[1 + off + a],
1463 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1464 isl_int_set_si(aff->v->el[1 + off + b], 0);
1466 return aff;
1469 /* Sort the divs in the local space of "aff" according to
1470 * the comparison function "cmp_row" in isl_local_space.c,
1471 * combining the coefficients of identical divs.
1473 * Reordering divs does not change the semantics of "aff",
1474 * so there is no need to call isl_aff_cow.
1475 * Moreover, this function is currently only called on isl_affs
1476 * with a single reference.
1478 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1480 int i, j, n;
1482 if (!aff)
1483 return NULL;
1485 n = isl_aff_dim(aff, isl_dim_div);
1486 for (i = 1; i < n; ++i) {
1487 for (j = i - 1; j >= 0; --j) {
1488 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1489 if (cmp < 0)
1490 break;
1491 if (cmp == 0)
1492 aff = merge_divs(aff, j, j + 1);
1493 else
1494 aff = swap_div(aff, j, j + 1);
1495 if (!aff)
1496 return NULL;
1500 return aff;
1503 /* Normalize the representation of "aff".
1505 * This function should only be called of "new" isl_affs, i.e.,
1506 * with only a single reference. We therefore do not need to
1507 * worry about affecting other instances.
1509 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1511 if (!aff)
1512 return NULL;
1513 aff->v = isl_vec_normalize(aff->v);
1514 if (!aff->v)
1515 return isl_aff_free(aff);
1516 aff = plug_in_integral_divs(aff);
1517 aff = plug_in_unit_divs(aff);
1518 aff = sort_divs(aff);
1519 aff = isl_aff_remove_unused_divs(aff);
1520 return aff;
1523 /* Given f, return floor(f).
1524 * If f is an integer expression, then just return f.
1525 * If f is a constant, then return the constant floor(f).
1526 * Otherwise, if f = g/m, write g = q m + r,
1527 * create a new div d = [r/m] and return the expression q + d.
1528 * The coefficients in r are taken to lie between -m/2 and m/2.
1530 * As a special case, floor(NaN) = NaN.
1532 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1534 int i;
1535 int size;
1536 isl_ctx *ctx;
1537 isl_vec *div;
1539 if (!aff)
1540 return NULL;
1542 if (isl_aff_is_nan(aff))
1543 return aff;
1544 if (isl_int_is_one(aff->v->el[0]))
1545 return aff;
1547 aff = isl_aff_cow(aff);
1548 if (!aff)
1549 return NULL;
1551 aff->v = isl_vec_cow(aff->v);
1552 if (!aff->v)
1553 return isl_aff_free(aff);
1555 if (isl_aff_is_cst(aff)) {
1556 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1557 isl_int_set_si(aff->v->el[0], 1);
1558 return aff;
1561 div = isl_vec_copy(aff->v);
1562 div = isl_vec_cow(div);
1563 if (!div)
1564 return isl_aff_free(aff);
1566 ctx = isl_aff_get_ctx(aff);
1567 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1568 for (i = 1; i < aff->v->size; ++i) {
1569 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1570 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1571 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1572 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1573 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1577 aff->ls = isl_local_space_add_div(aff->ls, div);
1578 if (!aff->ls)
1579 return isl_aff_free(aff);
1581 size = aff->v->size;
1582 aff->v = isl_vec_extend(aff->v, size + 1);
1583 if (!aff->v)
1584 return isl_aff_free(aff);
1585 isl_int_set_si(aff->v->el[0], 1);
1586 isl_int_set_si(aff->v->el[size], 1);
1588 aff = isl_aff_normalize(aff);
1590 return aff;
1593 /* Compute
1595 * aff mod m = aff - m * floor(aff/m)
1597 __isl_give isl_aff *isl_aff_mod(__isl_take isl_aff *aff, isl_int m)
1599 isl_aff *res;
1601 res = isl_aff_copy(aff);
1602 aff = isl_aff_scale_down(aff, m);
1603 aff = isl_aff_floor(aff);
1604 aff = isl_aff_scale(aff, m);
1605 res = isl_aff_sub(res, aff);
1607 return res;
1610 /* Compute
1612 * aff mod m = aff - m * floor(aff/m)
1614 * with m an integer value.
1616 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1617 __isl_take isl_val *m)
1619 isl_aff *res;
1621 if (!aff || !m)
1622 goto error;
1624 if (!isl_val_is_int(m))
1625 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1626 "expecting integer modulo", goto error);
1628 res = isl_aff_copy(aff);
1629 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1630 aff = isl_aff_floor(aff);
1631 aff = isl_aff_scale_val(aff, m);
1632 res = isl_aff_sub(res, aff);
1634 return res;
1635 error:
1636 isl_aff_free(aff);
1637 isl_val_free(m);
1638 return NULL;
1641 /* Compute
1643 * pwaff mod m = pwaff - m * floor(pwaff/m)
1645 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1647 isl_pw_aff *res;
1649 res = isl_pw_aff_copy(pwaff);
1650 pwaff = isl_pw_aff_scale_down(pwaff, m);
1651 pwaff = isl_pw_aff_floor(pwaff);
1652 pwaff = isl_pw_aff_scale(pwaff, m);
1653 res = isl_pw_aff_sub(res, pwaff);
1655 return res;
1658 /* Compute
1660 * pa mod m = pa - m * floor(pa/m)
1662 * with m an integer value.
1664 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1665 __isl_take isl_val *m)
1667 if (!pa || !m)
1668 goto error;
1669 if (!isl_val_is_int(m))
1670 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1671 "expecting integer modulo", goto error);
1672 pa = isl_pw_aff_mod(pa, m->n);
1673 isl_val_free(m);
1674 return pa;
1675 error:
1676 isl_pw_aff_free(pa);
1677 isl_val_free(m);
1678 return NULL;
1681 /* Given f, return ceil(f).
1682 * If f is an integer expression, then just return f.
1683 * Otherwise, let f be the expression
1685 * e/m
1687 * then return
1689 * floor((e + m - 1)/m)
1691 * As a special case, ceil(NaN) = NaN.
1693 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1695 if (!aff)
1696 return NULL;
1698 if (isl_aff_is_nan(aff))
1699 return aff;
1700 if (isl_int_is_one(aff->v->el[0]))
1701 return aff;
1703 aff = isl_aff_cow(aff);
1704 if (!aff)
1705 return NULL;
1706 aff->v = isl_vec_cow(aff->v);
1707 if (!aff->v)
1708 return isl_aff_free(aff);
1710 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1711 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1712 aff = isl_aff_floor(aff);
1714 return aff;
1717 /* Apply the expansion computed by isl_merge_divs.
1718 * The expansion itself is given by "exp" while the resulting
1719 * list of divs is given by "div".
1721 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1722 __isl_take isl_mat *div, int *exp)
1724 int i, j;
1725 int old_n_div;
1726 int new_n_div;
1727 int offset;
1729 aff = isl_aff_cow(aff);
1730 if (!aff || !div)
1731 goto error;
1733 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1734 new_n_div = isl_mat_rows(div);
1735 if (new_n_div < old_n_div)
1736 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
1737 "not an expansion", goto error);
1739 aff->v = isl_vec_extend(aff->v, aff->v->size + new_n_div - old_n_div);
1740 if (!aff->v)
1741 goto error;
1743 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1744 j = old_n_div - 1;
1745 for (i = new_n_div - 1; i >= 0; --i) {
1746 if (j >= 0 && exp[j] == i) {
1747 if (i != j)
1748 isl_int_swap(aff->v->el[offset + i],
1749 aff->v->el[offset + j]);
1750 j--;
1751 } else
1752 isl_int_set_si(aff->v->el[offset + i], 0);
1755 aff->ls = isl_local_space_replace_divs(aff->ls, isl_mat_copy(div));
1756 if (!aff->ls)
1757 goto error;
1758 isl_mat_free(div);
1759 return aff;
1760 error:
1761 isl_aff_free(aff);
1762 isl_mat_free(div);
1763 return NULL;
1766 /* Add two affine expressions that live in the same local space.
1768 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1769 __isl_take isl_aff *aff2)
1771 isl_int gcd, f;
1773 aff1 = isl_aff_cow(aff1);
1774 if (!aff1 || !aff2)
1775 goto error;
1777 aff1->v = isl_vec_cow(aff1->v);
1778 if (!aff1->v)
1779 goto error;
1781 isl_int_init(gcd);
1782 isl_int_init(f);
1783 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1784 isl_int_divexact(f, aff2->v->el[0], gcd);
1785 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1786 isl_int_divexact(f, aff1->v->el[0], gcd);
1787 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1788 isl_int_divexact(f, aff2->v->el[0], gcd);
1789 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1790 isl_int_clear(f);
1791 isl_int_clear(gcd);
1793 isl_aff_free(aff2);
1794 return aff1;
1795 error:
1796 isl_aff_free(aff1);
1797 isl_aff_free(aff2);
1798 return NULL;
1801 /* Return the sum of "aff1" and "aff2".
1803 * If either of the two is NaN, then the result is NaN.
1805 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1806 __isl_take isl_aff *aff2)
1808 isl_ctx *ctx;
1809 int *exp1 = NULL;
1810 int *exp2 = NULL;
1811 isl_mat *div;
1812 int n_div1, n_div2;
1814 if (!aff1 || !aff2)
1815 goto error;
1817 ctx = isl_aff_get_ctx(aff1);
1818 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1819 isl_die(ctx, isl_error_invalid,
1820 "spaces don't match", goto error);
1822 if (isl_aff_is_nan(aff1)) {
1823 isl_aff_free(aff2);
1824 return aff1;
1826 if (isl_aff_is_nan(aff2)) {
1827 isl_aff_free(aff1);
1828 return aff2;
1831 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1832 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1833 if (n_div1 == 0 && n_div2 == 0)
1834 return add_expanded(aff1, aff2);
1836 exp1 = isl_alloc_array(ctx, int, n_div1);
1837 exp2 = isl_alloc_array(ctx, int, n_div2);
1838 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1839 goto error;
1841 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1842 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1843 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1844 free(exp1);
1845 free(exp2);
1847 return add_expanded(aff1, aff2);
1848 error:
1849 free(exp1);
1850 free(exp2);
1851 isl_aff_free(aff1);
1852 isl_aff_free(aff2);
1853 return NULL;
1856 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1857 __isl_take isl_aff *aff2)
1859 return isl_aff_add(aff1, isl_aff_neg(aff2));
1862 /* Return the result of scaling "aff" by a factor of "f".
1864 * As a special case, f * NaN = NaN.
1866 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1868 isl_int gcd;
1870 if (!aff)
1871 return NULL;
1872 if (isl_aff_is_nan(aff))
1873 return aff;
1875 if (isl_int_is_one(f))
1876 return aff;
1878 aff = isl_aff_cow(aff);
1879 if (!aff)
1880 return NULL;
1881 aff->v = isl_vec_cow(aff->v);
1882 if (!aff->v)
1883 return isl_aff_free(aff);
1885 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1886 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1887 return aff;
1890 isl_int_init(gcd);
1891 isl_int_gcd(gcd, aff->v->el[0], f);
1892 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1893 isl_int_divexact(gcd, f, gcd);
1894 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1895 isl_int_clear(gcd);
1897 return aff;
1900 /* Multiple "aff" by "v".
1902 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1903 __isl_take isl_val *v)
1905 if (!aff || !v)
1906 goto error;
1908 if (isl_val_is_one(v)) {
1909 isl_val_free(v);
1910 return aff;
1913 if (!isl_val_is_rat(v))
1914 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1915 "expecting rational factor", goto error);
1917 aff = isl_aff_scale(aff, v->n);
1918 aff = isl_aff_scale_down(aff, v->d);
1920 isl_val_free(v);
1921 return aff;
1922 error:
1923 isl_aff_free(aff);
1924 isl_val_free(v);
1925 return NULL;
1928 /* Return the result of scaling "aff" down by a factor of "f".
1930 * As a special case, NaN/f = NaN.
1932 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1934 isl_int gcd;
1936 if (!aff)
1937 return NULL;
1938 if (isl_aff_is_nan(aff))
1939 return aff;
1941 if (isl_int_is_one(f))
1942 return aff;
1944 aff = isl_aff_cow(aff);
1945 if (!aff)
1946 return NULL;
1948 if (isl_int_is_zero(f))
1949 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1950 "cannot scale down by zero", return isl_aff_free(aff));
1952 aff->v = isl_vec_cow(aff->v);
1953 if (!aff->v)
1954 return isl_aff_free(aff);
1956 isl_int_init(gcd);
1957 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1958 isl_int_gcd(gcd, gcd, f);
1959 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1960 isl_int_divexact(gcd, f, gcd);
1961 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1962 isl_int_clear(gcd);
1964 return aff;
1967 /* Divide "aff" by "v".
1969 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1970 __isl_take isl_val *v)
1972 if (!aff || !v)
1973 goto error;
1975 if (isl_val_is_one(v)) {
1976 isl_val_free(v);
1977 return aff;
1980 if (!isl_val_is_rat(v))
1981 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1982 "expecting rational factor", goto error);
1983 if (!isl_val_is_pos(v))
1984 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1985 "factor needs to be positive", goto error);
1987 aff = isl_aff_scale(aff, v->d);
1988 aff = isl_aff_scale_down(aff, v->n);
1990 isl_val_free(v);
1991 return aff;
1992 error:
1993 isl_aff_free(aff);
1994 isl_val_free(v);
1995 return NULL;
1998 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2000 isl_int v;
2002 if (f == 1)
2003 return aff;
2005 isl_int_init(v);
2006 isl_int_set_ui(v, f);
2007 aff = isl_aff_scale_down(aff, v);
2008 isl_int_clear(v);
2010 return aff;
2013 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2014 enum isl_dim_type type, unsigned pos, const char *s)
2016 aff = isl_aff_cow(aff);
2017 if (!aff)
2018 return NULL;
2019 if (type == isl_dim_out)
2020 isl_die(aff->v->ctx, isl_error_invalid,
2021 "cannot set name of output/set dimension",
2022 return isl_aff_free(aff));
2023 if (type == isl_dim_in)
2024 type = isl_dim_set;
2025 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2026 if (!aff->ls)
2027 return isl_aff_free(aff);
2029 return aff;
2032 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2033 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2035 aff = isl_aff_cow(aff);
2036 if (!aff)
2037 goto error;
2038 if (type == isl_dim_out)
2039 isl_die(aff->v->ctx, isl_error_invalid,
2040 "cannot set name of output/set dimension",
2041 goto error);
2042 if (type == isl_dim_in)
2043 type = isl_dim_set;
2044 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2045 if (!aff->ls)
2046 return isl_aff_free(aff);
2048 return aff;
2049 error:
2050 isl_id_free(id);
2051 isl_aff_free(aff);
2052 return NULL;
2055 /* Replace the identifier of the input tuple of "aff" by "id".
2056 * type is currently required to be equal to isl_dim_in
2058 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2059 enum isl_dim_type type, __isl_take isl_id *id)
2061 aff = isl_aff_cow(aff);
2062 if (!aff)
2063 goto error;
2064 if (type != isl_dim_out)
2065 isl_die(aff->v->ctx, isl_error_invalid,
2066 "cannot only set id of input tuple", goto error);
2067 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2068 if (!aff->ls)
2069 return isl_aff_free(aff);
2071 return aff;
2072 error:
2073 isl_id_free(id);
2074 isl_aff_free(aff);
2075 return NULL;
2078 /* Exploit the equalities in "eq" to simplify the affine expression
2079 * and the expressions of the integer divisions in the local space.
2080 * The integer divisions in this local space are assumed to appear
2081 * as regular dimensions in "eq".
2083 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2084 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2086 int i, j;
2087 unsigned total;
2088 unsigned n_div;
2090 if (!eq)
2091 goto error;
2092 if (eq->n_eq == 0) {
2093 isl_basic_set_free(eq);
2094 return aff;
2097 aff = isl_aff_cow(aff);
2098 if (!aff)
2099 goto error;
2101 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2102 isl_basic_set_copy(eq));
2103 aff->v = isl_vec_cow(aff->v);
2104 if (!aff->ls || !aff->v)
2105 goto error;
2107 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2108 n_div = eq->n_div;
2109 for (i = 0; i < eq->n_eq; ++i) {
2110 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2111 if (j < 0 || j == 0 || j >= total)
2112 continue;
2114 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2115 &aff->v->el[0]);
2118 isl_basic_set_free(eq);
2119 aff = isl_aff_normalize(aff);
2120 return aff;
2121 error:
2122 isl_basic_set_free(eq);
2123 isl_aff_free(aff);
2124 return NULL;
2127 /* Exploit the equalities in "eq" to simplify the affine expression
2128 * and the expressions of the integer divisions in the local space.
2130 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2131 __isl_take isl_basic_set *eq)
2133 int n_div;
2135 if (!aff || !eq)
2136 goto error;
2137 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2138 if (n_div > 0)
2139 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2140 return isl_aff_substitute_equalities_lifted(aff, eq);
2141 error:
2142 isl_basic_set_free(eq);
2143 isl_aff_free(aff);
2144 return NULL;
2147 /* Look for equalities among the variables shared by context and aff
2148 * and the integer divisions of aff, if any.
2149 * The equalities are then used to eliminate coefficients and/or integer
2150 * divisions from aff.
2152 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2153 __isl_take isl_set *context)
2155 isl_basic_set *hull;
2156 int n_div;
2158 if (!aff)
2159 goto error;
2160 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2161 if (n_div > 0) {
2162 isl_basic_set *bset;
2163 isl_local_space *ls;
2164 context = isl_set_add_dims(context, isl_dim_set, n_div);
2165 ls = isl_aff_get_domain_local_space(aff);
2166 bset = isl_basic_set_from_local_space(ls);
2167 bset = isl_basic_set_lift(bset);
2168 bset = isl_basic_set_flatten(bset);
2169 context = isl_set_intersect(context,
2170 isl_set_from_basic_set(bset));
2173 hull = isl_set_affine_hull(context);
2174 return isl_aff_substitute_equalities_lifted(aff, hull);
2175 error:
2176 isl_aff_free(aff);
2177 isl_set_free(context);
2178 return NULL;
2181 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2182 __isl_take isl_set *context)
2184 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2185 dom_context = isl_set_intersect_params(dom_context, context);
2186 return isl_aff_gist(aff, dom_context);
2189 /* Return a basic set containing those elements in the space
2190 * of aff where it is positive. "rational" should not be set.
2192 * If "aff" is NaN, then it is not positive.
2194 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2195 int rational)
2197 isl_constraint *ineq;
2198 isl_basic_set *bset;
2199 isl_val *c;
2201 if (!aff)
2202 return NULL;
2203 if (isl_aff_is_nan(aff)) {
2204 isl_space *space = isl_aff_get_domain_space(aff);
2205 isl_aff_free(aff);
2206 return isl_basic_set_empty(space);
2208 if (rational)
2209 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2210 "rational sets not supported", goto error);
2212 ineq = isl_inequality_from_aff(aff);
2213 c = isl_constraint_get_constant_val(ineq);
2214 c = isl_val_sub_ui(c, 1);
2215 ineq = isl_constraint_set_constant_val(ineq, c);
2217 bset = isl_basic_set_from_constraint(ineq);
2218 bset = isl_basic_set_simplify(bset);
2219 return bset;
2220 error:
2221 isl_aff_free(aff);
2222 return NULL;
2225 /* Return a basic set containing those elements in the space
2226 * of aff where it is non-negative.
2227 * If "rational" is set, then return a rational basic set.
2229 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2231 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2232 __isl_take isl_aff *aff, int rational)
2234 isl_constraint *ineq;
2235 isl_basic_set *bset;
2237 if (!aff)
2238 return NULL;
2239 if (isl_aff_is_nan(aff)) {
2240 isl_space *space = isl_aff_get_domain_space(aff);
2241 isl_aff_free(aff);
2242 return isl_basic_set_empty(space);
2245 ineq = isl_inequality_from_aff(aff);
2247 bset = isl_basic_set_from_constraint(ineq);
2248 if (rational)
2249 bset = isl_basic_set_set_rational(bset);
2250 bset = isl_basic_set_simplify(bset);
2251 return bset;
2254 /* Return a basic set containing those elements in the space
2255 * of aff where it is non-negative.
2257 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2259 return aff_nonneg_basic_set(aff, 0);
2262 /* Return a basic set containing those elements in the domain space
2263 * of aff where it is negative.
2265 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2267 aff = isl_aff_neg(aff);
2268 aff = isl_aff_add_constant_num_si(aff, -1);
2269 return isl_aff_nonneg_basic_set(aff);
2272 /* Return a basic set containing those elements in the space
2273 * of aff where it is zero.
2274 * If "rational" is set, then return a rational basic set.
2276 * If "aff" is NaN, then it is not zero.
2278 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2279 int rational)
2281 isl_constraint *ineq;
2282 isl_basic_set *bset;
2284 if (!aff)
2285 return NULL;
2286 if (isl_aff_is_nan(aff)) {
2287 isl_space *space = isl_aff_get_domain_space(aff);
2288 isl_aff_free(aff);
2289 return isl_basic_set_empty(space);
2292 ineq = isl_equality_from_aff(aff);
2294 bset = isl_basic_set_from_constraint(ineq);
2295 if (rational)
2296 bset = isl_basic_set_set_rational(bset);
2297 bset = isl_basic_set_simplify(bset);
2298 return bset;
2301 /* Return a basic set containing those elements in the space
2302 * of aff where it is zero.
2304 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2306 return aff_zero_basic_set(aff, 0);
2309 /* Return a basic set containing those elements in the shared space
2310 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2312 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2313 __isl_take isl_aff *aff2)
2315 aff1 = isl_aff_sub(aff1, aff2);
2317 return isl_aff_nonneg_basic_set(aff1);
2320 /* Return a set containing those elements in the shared space
2321 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2323 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2324 __isl_take isl_aff *aff2)
2326 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2329 /* Return a basic set containing those elements in the shared space
2330 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2332 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2333 __isl_take isl_aff *aff2)
2335 return isl_aff_ge_basic_set(aff2, aff1);
2338 /* Return a set containing those elements in the shared space
2339 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2341 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2342 __isl_take isl_aff *aff2)
2344 return isl_aff_ge_set(aff2, aff1);
2347 /* Return a basic set containing those elements in the shared space
2348 * of aff1 and aff2 where aff1 and aff2 are equal.
2350 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2351 __isl_take isl_aff *aff2)
2353 aff1 = isl_aff_sub(aff1, aff2);
2355 return isl_aff_zero_basic_set(aff1);
2358 /* Return a set containing those elements in the shared space
2359 * of aff1 and aff2 where aff1 and aff2 are equal.
2361 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2362 __isl_take isl_aff *aff2)
2364 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2367 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2368 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2370 aff1 = isl_aff_add(aff1, aff2);
2371 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2372 return aff1;
2375 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2377 if (!aff)
2378 return -1;
2380 return 0;
2383 /* Check whether the given affine expression has non-zero coefficient
2384 * for any dimension in the given range or if any of these dimensions
2385 * appear with non-zero coefficients in any of the integer divisions
2386 * involved in the affine expression.
2388 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2389 enum isl_dim_type type, unsigned first, unsigned n)
2391 int i;
2392 isl_ctx *ctx;
2393 int *active = NULL;
2394 isl_bool involves = isl_bool_false;
2396 if (!aff)
2397 return isl_bool_error;
2398 if (n == 0)
2399 return isl_bool_false;
2401 ctx = isl_aff_get_ctx(aff);
2402 if (first + n > isl_aff_dim(aff, type))
2403 isl_die(ctx, isl_error_invalid,
2404 "range out of bounds", return isl_bool_error);
2406 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2407 if (!active)
2408 goto error;
2410 first += isl_local_space_offset(aff->ls, type) - 1;
2411 for (i = 0; i < n; ++i)
2412 if (active[first + i]) {
2413 involves = isl_bool_true;
2414 break;
2417 free(active);
2419 return involves;
2420 error:
2421 free(active);
2422 return isl_bool_error;
2425 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2426 enum isl_dim_type type, unsigned first, unsigned n)
2428 isl_ctx *ctx;
2430 if (!aff)
2431 return NULL;
2432 if (type == isl_dim_out)
2433 isl_die(aff->v->ctx, isl_error_invalid,
2434 "cannot drop output/set dimension",
2435 return isl_aff_free(aff));
2436 if (type == isl_dim_in)
2437 type = isl_dim_set;
2438 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2439 return aff;
2441 ctx = isl_aff_get_ctx(aff);
2442 if (first + n > isl_local_space_dim(aff->ls, type))
2443 isl_die(ctx, isl_error_invalid, "range out of bounds",
2444 return isl_aff_free(aff));
2446 aff = isl_aff_cow(aff);
2447 if (!aff)
2448 return NULL;
2450 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2451 if (!aff->ls)
2452 return isl_aff_free(aff);
2454 first += 1 + isl_local_space_offset(aff->ls, type);
2455 aff->v = isl_vec_drop_els(aff->v, first, n);
2456 if (!aff->v)
2457 return isl_aff_free(aff);
2459 return aff;
2462 /* Project the domain of the affine expression onto its parameter space.
2463 * The affine expression may not involve any of the domain dimensions.
2465 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2467 isl_space *space;
2468 unsigned n;
2469 int involves;
2471 n = isl_aff_dim(aff, isl_dim_in);
2472 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2473 if (involves < 0)
2474 return isl_aff_free(aff);
2475 if (involves)
2476 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2477 "affine expression involves some of the domain dimensions",
2478 return isl_aff_free(aff));
2479 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2480 space = isl_aff_get_domain_space(aff);
2481 space = isl_space_params(space);
2482 aff = isl_aff_reset_domain_space(aff, space);
2483 return aff;
2486 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2487 enum isl_dim_type type, unsigned first, unsigned n)
2489 isl_ctx *ctx;
2491 if (!aff)
2492 return NULL;
2493 if (type == isl_dim_out)
2494 isl_die(aff->v->ctx, isl_error_invalid,
2495 "cannot insert output/set dimensions",
2496 return isl_aff_free(aff));
2497 if (type == isl_dim_in)
2498 type = isl_dim_set;
2499 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2500 return aff;
2502 ctx = isl_aff_get_ctx(aff);
2503 if (first > isl_local_space_dim(aff->ls, type))
2504 isl_die(ctx, isl_error_invalid, "position out of bounds",
2505 return isl_aff_free(aff));
2507 aff = isl_aff_cow(aff);
2508 if (!aff)
2509 return NULL;
2511 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2512 if (!aff->ls)
2513 return isl_aff_free(aff);
2515 first += 1 + isl_local_space_offset(aff->ls, type);
2516 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2517 if (!aff->v)
2518 return isl_aff_free(aff);
2520 return aff;
2523 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2524 enum isl_dim_type type, unsigned n)
2526 unsigned pos;
2528 pos = isl_aff_dim(aff, type);
2530 return isl_aff_insert_dims(aff, type, pos, n);
2533 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2534 enum isl_dim_type type, unsigned n)
2536 unsigned pos;
2538 pos = isl_pw_aff_dim(pwaff, type);
2540 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2543 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2544 * to dimensions of "dst_type" at "dst_pos".
2546 * We only support moving input dimensions to parameters and vice versa.
2548 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2549 enum isl_dim_type dst_type, unsigned dst_pos,
2550 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2552 unsigned g_dst_pos;
2553 unsigned g_src_pos;
2555 if (!aff)
2556 return NULL;
2557 if (n == 0 &&
2558 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2559 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2560 return aff;
2562 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2563 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2564 "cannot move output/set dimension",
2565 return isl_aff_free(aff));
2566 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2567 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2568 "cannot move divs", return isl_aff_free(aff));
2569 if (dst_type == isl_dim_in)
2570 dst_type = isl_dim_set;
2571 if (src_type == isl_dim_in)
2572 src_type = isl_dim_set;
2574 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2575 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2576 "range out of bounds", return isl_aff_free(aff));
2577 if (dst_type == src_type)
2578 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2579 "moving dims within the same type not supported",
2580 return isl_aff_free(aff));
2582 aff = isl_aff_cow(aff);
2583 if (!aff)
2584 return NULL;
2586 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2587 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2588 if (dst_type > src_type)
2589 g_dst_pos -= n;
2591 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2592 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2593 src_type, src_pos, n);
2594 if (!aff->v || !aff->ls)
2595 return isl_aff_free(aff);
2597 aff = sort_divs(aff);
2599 return aff;
2602 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2604 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2605 return isl_pw_aff_alloc(dom, aff);
2608 #undef PW
2609 #define PW isl_pw_aff
2610 #undef EL
2611 #define EL isl_aff
2612 #undef EL_IS_ZERO
2613 #define EL_IS_ZERO is_empty
2614 #undef ZERO
2615 #define ZERO empty
2616 #undef IS_ZERO
2617 #define IS_ZERO is_empty
2618 #undef FIELD
2619 #define FIELD aff
2620 #undef DEFAULT_IS_ZERO
2621 #define DEFAULT_IS_ZERO 0
2623 #define NO_EVAL
2624 #define NO_OPT
2625 #define NO_LIFT
2626 #define NO_MORPH
2628 #include <isl_pw_templ.c>
2629 #include <isl_pw_hash.c>
2630 #include <isl_pw_union_opt.c>
2632 #undef UNION
2633 #define UNION isl_union_pw_aff
2634 #undef PART
2635 #define PART isl_pw_aff
2636 #undef PARTS
2637 #define PARTS pw_aff
2639 #include <isl_union_single.c>
2640 #include <isl_union_neg.c>
2642 static __isl_give isl_set *align_params_pw_pw_set_and(
2643 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2644 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2645 __isl_take isl_pw_aff *pwaff2))
2647 if (!pwaff1 || !pwaff2)
2648 goto error;
2649 if (isl_space_match(pwaff1->dim, isl_dim_param,
2650 pwaff2->dim, isl_dim_param))
2651 return fn(pwaff1, pwaff2);
2652 if (!isl_space_has_named_params(pwaff1->dim) ||
2653 !isl_space_has_named_params(pwaff2->dim))
2654 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2655 "unaligned unnamed parameters", goto error);
2656 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2657 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2658 return fn(pwaff1, pwaff2);
2659 error:
2660 isl_pw_aff_free(pwaff1);
2661 isl_pw_aff_free(pwaff2);
2662 return NULL;
2665 /* Align the parameters of the to isl_pw_aff arguments and
2666 * then apply a function "fn" on them that returns an isl_map.
2668 static __isl_give isl_map *align_params_pw_pw_map_and(
2669 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2670 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2671 __isl_take isl_pw_aff *pa2))
2673 if (!pa1 || !pa2)
2674 goto error;
2675 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2676 return fn(pa1, pa2);
2677 if (!isl_space_has_named_params(pa1->dim) ||
2678 !isl_space_has_named_params(pa2->dim))
2679 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2680 "unaligned unnamed parameters", goto error);
2681 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2682 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2683 return fn(pa1, pa2);
2684 error:
2685 isl_pw_aff_free(pa1);
2686 isl_pw_aff_free(pa2);
2687 return NULL;
2690 /* Compute a piecewise quasi-affine expression with a domain that
2691 * is the union of those of pwaff1 and pwaff2 and such that on each
2692 * cell, the quasi-affine expression is the maximum of those of pwaff1
2693 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2694 * cell, then the associated expression is the defined one.
2696 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2697 __isl_take isl_pw_aff *pwaff2)
2699 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2702 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2703 __isl_take isl_pw_aff *pwaff2)
2705 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2706 &pw_aff_union_max);
2709 /* Compute a piecewise quasi-affine expression with a domain that
2710 * is the union of those of pwaff1 and pwaff2 and such that on each
2711 * cell, the quasi-affine expression is the minimum of those of pwaff1
2712 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2713 * cell, then the associated expression is the defined one.
2715 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2716 __isl_take isl_pw_aff *pwaff2)
2718 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2721 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2722 __isl_take isl_pw_aff *pwaff2)
2724 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2725 &pw_aff_union_min);
2728 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2729 __isl_take isl_pw_aff *pwaff2, int max)
2731 if (max)
2732 return isl_pw_aff_union_max(pwaff1, pwaff2);
2733 else
2734 return isl_pw_aff_union_min(pwaff1, pwaff2);
2737 /* Construct a map with as domain the domain of pwaff and
2738 * one-dimensional range corresponding to the affine expressions.
2740 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2742 int i;
2743 isl_space *dim;
2744 isl_map *map;
2746 if (!pwaff)
2747 return NULL;
2749 dim = isl_pw_aff_get_space(pwaff);
2750 map = isl_map_empty(dim);
2752 for (i = 0; i < pwaff->n; ++i) {
2753 isl_basic_map *bmap;
2754 isl_map *map_i;
2756 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2757 map_i = isl_map_from_basic_map(bmap);
2758 map_i = isl_map_intersect_domain(map_i,
2759 isl_set_copy(pwaff->p[i].set));
2760 map = isl_map_union_disjoint(map, map_i);
2763 isl_pw_aff_free(pwaff);
2765 return map;
2768 /* Construct a map with as domain the domain of pwaff and
2769 * one-dimensional range corresponding to the affine expressions.
2771 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2773 if (!pwaff)
2774 return NULL;
2775 if (isl_space_is_set(pwaff->dim))
2776 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2777 "space of input is not a map", goto error);
2778 return map_from_pw_aff(pwaff);
2779 error:
2780 isl_pw_aff_free(pwaff);
2781 return NULL;
2784 /* Construct a one-dimensional set with as parameter domain
2785 * the domain of pwaff and the single set dimension
2786 * corresponding to the affine expressions.
2788 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2790 if (!pwaff)
2791 return NULL;
2792 if (!isl_space_is_set(pwaff->dim))
2793 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2794 "space of input is not a set", goto error);
2795 return map_from_pw_aff(pwaff);
2796 error:
2797 isl_pw_aff_free(pwaff);
2798 return NULL;
2801 /* Return a set containing those elements in the domain
2802 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2803 * does not satisfy "fn" (if complement is 1).
2805 * The pieces with a NaN never belong to the result since
2806 * NaN does not satisfy any property.
2808 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2809 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2810 int complement)
2812 int i;
2813 isl_set *set;
2815 if (!pwaff)
2816 return NULL;
2818 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2820 for (i = 0; i < pwaff->n; ++i) {
2821 isl_basic_set *bset;
2822 isl_set *set_i, *locus;
2823 int rational;
2825 if (isl_aff_is_nan(pwaff->p[i].aff))
2826 continue;
2828 rational = isl_set_has_rational(pwaff->p[i].set);
2829 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2830 locus = isl_set_from_basic_set(bset);
2831 set_i = isl_set_copy(pwaff->p[i].set);
2832 if (complement)
2833 set_i = isl_set_subtract(set_i, locus);
2834 else
2835 set_i = isl_set_intersect(set_i, locus);
2836 set = isl_set_union_disjoint(set, set_i);
2839 isl_pw_aff_free(pwaff);
2841 return set;
2844 /* Return a set containing those elements in the domain
2845 * of "pa" where it is positive.
2847 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2849 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2852 /* Return a set containing those elements in the domain
2853 * of pwaff where it is non-negative.
2855 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2857 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2860 /* Return a set containing those elements in the domain
2861 * of pwaff where it is zero.
2863 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2865 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2868 /* Return a set containing those elements in the domain
2869 * of pwaff where it is not zero.
2871 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2873 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2876 /* Return a set containing those elements in the shared domain
2877 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2879 * We compute the difference on the shared domain and then construct
2880 * the set of values where this difference is non-negative.
2881 * If strict is set, we first subtract 1 from the difference.
2882 * If equal is set, we only return the elements where pwaff1 and pwaff2
2883 * are equal.
2885 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2886 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2888 isl_set *set1, *set2;
2890 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2891 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2892 set1 = isl_set_intersect(set1, set2);
2893 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2894 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2895 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2897 if (strict) {
2898 isl_space *dim = isl_set_get_space(set1);
2899 isl_aff *aff;
2900 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2901 aff = isl_aff_add_constant_si(aff, -1);
2902 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2903 } else
2904 isl_set_free(set1);
2906 if (equal)
2907 return isl_pw_aff_zero_set(pwaff1);
2908 return isl_pw_aff_nonneg_set(pwaff1);
2911 /* Return a set containing those elements in the shared domain
2912 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2914 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2915 __isl_take isl_pw_aff *pwaff2)
2917 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2920 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2921 __isl_take isl_pw_aff *pwaff2)
2923 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2926 /* Return a set containing those elements in the shared domain
2927 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2929 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2930 __isl_take isl_pw_aff *pwaff2)
2932 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2935 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2936 __isl_take isl_pw_aff *pwaff2)
2938 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2941 /* Return a set containing those elements in the shared domain
2942 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2944 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2945 __isl_take isl_pw_aff *pwaff2)
2947 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2950 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2951 __isl_take isl_pw_aff *pwaff2)
2953 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2956 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2957 __isl_take isl_pw_aff *pwaff2)
2959 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2962 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2963 __isl_take isl_pw_aff *pwaff2)
2965 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2968 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2969 * where the function values are ordered in the same way as "order",
2970 * which returns a set in the shared domain of its two arguments.
2971 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2973 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2974 * We first pull back the two functions such that they are defined on
2975 * the domain [A -> B]. Then we apply "order", resulting in a set
2976 * in the space [A -> B]. Finally, we unwrap this set to obtain
2977 * a map in the space A -> B.
2979 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2980 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2981 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2982 __isl_take isl_pw_aff *pa2))
2984 isl_space *space1, *space2;
2985 isl_multi_aff *ma;
2986 isl_set *set;
2988 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2989 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2990 space1 = isl_space_map_from_domain_and_range(space1, space2);
2991 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2992 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2993 ma = isl_multi_aff_range_map(space1);
2994 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2995 set = order(pa1, pa2);
2997 return isl_set_unwrap(set);
3000 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3001 * where the function values are equal.
3002 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3004 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3005 __isl_take isl_pw_aff *pa2)
3007 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3010 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3011 * where the function values are equal.
3013 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3014 __isl_take isl_pw_aff *pa2)
3016 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3019 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3020 * where the function value of "pa1" is less than the function value of "pa2".
3021 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3023 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3024 __isl_take isl_pw_aff *pa2)
3026 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3029 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3030 * where the function value of "pa1" is less than the function value of "pa2".
3032 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3033 __isl_take isl_pw_aff *pa2)
3035 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3038 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3039 * where the function value of "pa1" is greater than the function value
3040 * of "pa2".
3041 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3043 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3044 __isl_take isl_pw_aff *pa2)
3046 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3049 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3050 * where the function value of "pa1" is greater than the function value
3051 * of "pa2".
3053 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3054 __isl_take isl_pw_aff *pa2)
3056 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3059 /* Return a set containing those elements in the shared domain
3060 * of the elements of list1 and list2 where each element in list1
3061 * has the relation specified by "fn" with each element in list2.
3063 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3064 __isl_take isl_pw_aff_list *list2,
3065 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3066 __isl_take isl_pw_aff *pwaff2))
3068 int i, j;
3069 isl_ctx *ctx;
3070 isl_set *set;
3072 if (!list1 || !list2)
3073 goto error;
3075 ctx = isl_pw_aff_list_get_ctx(list1);
3076 if (list1->n < 1 || list2->n < 1)
3077 isl_die(ctx, isl_error_invalid,
3078 "list should contain at least one element", goto error);
3080 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3081 for (i = 0; i < list1->n; ++i)
3082 for (j = 0; j < list2->n; ++j) {
3083 isl_set *set_ij;
3085 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3086 isl_pw_aff_copy(list2->p[j]));
3087 set = isl_set_intersect(set, set_ij);
3090 isl_pw_aff_list_free(list1);
3091 isl_pw_aff_list_free(list2);
3092 return set;
3093 error:
3094 isl_pw_aff_list_free(list1);
3095 isl_pw_aff_list_free(list2);
3096 return NULL;
3099 /* Return a set containing those elements in the shared domain
3100 * of the elements of list1 and list2 where each element in list1
3101 * is equal to each element in list2.
3103 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3104 __isl_take isl_pw_aff_list *list2)
3106 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3109 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3110 __isl_take isl_pw_aff_list *list2)
3112 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3115 /* Return a set containing those elements in the shared domain
3116 * of the elements of list1 and list2 where each element in list1
3117 * is less than or equal to each element in list2.
3119 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3120 __isl_take isl_pw_aff_list *list2)
3122 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3125 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3126 __isl_take isl_pw_aff_list *list2)
3128 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3131 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3132 __isl_take isl_pw_aff_list *list2)
3134 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3137 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3138 __isl_take isl_pw_aff_list *list2)
3140 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3144 /* Return a set containing those elements in the shared domain
3145 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3147 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3148 __isl_take isl_pw_aff *pwaff2)
3150 isl_set *set_lt, *set_gt;
3152 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3153 isl_pw_aff_copy(pwaff2));
3154 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3155 return isl_set_union_disjoint(set_lt, set_gt);
3158 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3159 __isl_take isl_pw_aff *pwaff2)
3161 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3164 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3165 isl_int v)
3167 int i;
3169 if (isl_int_is_one(v))
3170 return pwaff;
3171 if (!isl_int_is_pos(v))
3172 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3173 "factor needs to be positive",
3174 return isl_pw_aff_free(pwaff));
3175 pwaff = isl_pw_aff_cow(pwaff);
3176 if (!pwaff)
3177 return NULL;
3178 if (pwaff->n == 0)
3179 return pwaff;
3181 for (i = 0; i < pwaff->n; ++i) {
3182 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3183 if (!pwaff->p[i].aff)
3184 return isl_pw_aff_free(pwaff);
3187 return pwaff;
3190 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3192 int i;
3194 pwaff = isl_pw_aff_cow(pwaff);
3195 if (!pwaff)
3196 return NULL;
3197 if (pwaff->n == 0)
3198 return pwaff;
3200 for (i = 0; i < pwaff->n; ++i) {
3201 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3202 if (!pwaff->p[i].aff)
3203 return isl_pw_aff_free(pwaff);
3206 return pwaff;
3209 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3211 int i;
3213 pwaff = isl_pw_aff_cow(pwaff);
3214 if (!pwaff)
3215 return NULL;
3216 if (pwaff->n == 0)
3217 return pwaff;
3219 for (i = 0; i < pwaff->n; ++i) {
3220 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3221 if (!pwaff->p[i].aff)
3222 return isl_pw_aff_free(pwaff);
3225 return pwaff;
3228 /* Assuming that "cond1" and "cond2" are disjoint,
3229 * return an affine expression that is equal to pwaff1 on cond1
3230 * and to pwaff2 on cond2.
3232 static __isl_give isl_pw_aff *isl_pw_aff_select(
3233 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3234 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3236 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3237 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3239 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3242 /* Return an affine expression that is equal to pwaff_true for elements
3243 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3244 * is zero.
3245 * That is, return cond ? pwaff_true : pwaff_false;
3247 * If "cond" involves and NaN, then we conservatively return a NaN
3248 * on its entire domain. In principle, we could consider the pieces
3249 * where it is NaN separately from those where it is not.
3251 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3252 * then only use the domain of "cond" to restrict the domain.
3254 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3255 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3257 isl_set *cond_true, *cond_false;
3258 isl_bool equal;
3260 if (!cond)
3261 goto error;
3262 if (isl_pw_aff_involves_nan(cond)) {
3263 isl_space *space = isl_pw_aff_get_domain_space(cond);
3264 isl_local_space *ls = isl_local_space_from_space(space);
3265 isl_pw_aff_free(cond);
3266 isl_pw_aff_free(pwaff_true);
3267 isl_pw_aff_free(pwaff_false);
3268 return isl_pw_aff_nan_on_domain(ls);
3271 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3272 isl_pw_aff_get_space(pwaff_false));
3273 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3274 isl_pw_aff_get_space(pwaff_true));
3275 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3276 if (equal < 0)
3277 goto error;
3278 if (equal) {
3279 isl_set *dom;
3281 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3282 isl_pw_aff_free(pwaff_false);
3283 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3286 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3287 cond_false = isl_pw_aff_zero_set(cond);
3288 return isl_pw_aff_select(cond_true, pwaff_true,
3289 cond_false, pwaff_false);
3290 error:
3291 isl_pw_aff_free(cond);
3292 isl_pw_aff_free(pwaff_true);
3293 isl_pw_aff_free(pwaff_false);
3294 return NULL;
3297 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3299 if (!aff)
3300 return isl_bool_error;
3302 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3305 /* Check whether pwaff is a piecewise constant.
3307 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3309 int i;
3311 if (!pwaff)
3312 return isl_bool_error;
3314 for (i = 0; i < pwaff->n; ++i) {
3315 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3316 if (is_cst < 0 || !is_cst)
3317 return is_cst;
3320 return isl_bool_true;
3323 /* Are all elements of "mpa" piecewise constants?
3325 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3327 int i;
3329 if (!mpa)
3330 return isl_bool_error;
3332 for (i = 0; i < mpa->n; ++i) {
3333 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3334 if (is_cst < 0 || !is_cst)
3335 return is_cst;
3338 return isl_bool_true;
3341 /* Return the product of "aff1" and "aff2".
3343 * If either of the two is NaN, then the result is NaN.
3345 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3347 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3348 __isl_take isl_aff *aff2)
3350 if (!aff1 || !aff2)
3351 goto error;
3353 if (isl_aff_is_nan(aff1)) {
3354 isl_aff_free(aff2);
3355 return aff1;
3357 if (isl_aff_is_nan(aff2)) {
3358 isl_aff_free(aff1);
3359 return aff2;
3362 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3363 return isl_aff_mul(aff2, aff1);
3365 if (!isl_aff_is_cst(aff2))
3366 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3367 "at least one affine expression should be constant",
3368 goto error);
3370 aff1 = isl_aff_cow(aff1);
3371 if (!aff1 || !aff2)
3372 goto error;
3374 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3375 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3377 isl_aff_free(aff2);
3378 return aff1;
3379 error:
3380 isl_aff_free(aff1);
3381 isl_aff_free(aff2);
3382 return NULL;
3385 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3387 * If either of the two is NaN, then the result is NaN.
3389 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3390 __isl_take isl_aff *aff2)
3392 int is_cst;
3393 int neg;
3395 if (!aff1 || !aff2)
3396 goto error;
3398 if (isl_aff_is_nan(aff1)) {
3399 isl_aff_free(aff2);
3400 return aff1;
3402 if (isl_aff_is_nan(aff2)) {
3403 isl_aff_free(aff1);
3404 return aff2;
3407 is_cst = isl_aff_is_cst(aff2);
3408 if (is_cst < 0)
3409 goto error;
3410 if (!is_cst)
3411 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3412 "second argument should be a constant", goto error);
3414 if (!aff2)
3415 goto error;
3417 neg = isl_int_is_neg(aff2->v->el[1]);
3418 if (neg) {
3419 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3420 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3423 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3424 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3426 if (neg) {
3427 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3428 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3431 isl_aff_free(aff2);
3432 return aff1;
3433 error:
3434 isl_aff_free(aff1);
3435 isl_aff_free(aff2);
3436 return NULL;
3439 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3440 __isl_take isl_pw_aff *pwaff2)
3442 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3445 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3446 __isl_take isl_pw_aff *pwaff2)
3448 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3451 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3452 __isl_take isl_pw_aff *pwaff2)
3454 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3457 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3458 __isl_take isl_pw_aff *pwaff2)
3460 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3463 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3464 __isl_take isl_pw_aff *pwaff2)
3466 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3469 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3470 __isl_take isl_pw_aff *pa2)
3472 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3475 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3477 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3478 __isl_take isl_pw_aff *pa2)
3480 int is_cst;
3482 is_cst = isl_pw_aff_is_cst(pa2);
3483 if (is_cst < 0)
3484 goto error;
3485 if (!is_cst)
3486 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3487 "second argument should be a piecewise constant",
3488 goto error);
3489 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3490 error:
3491 isl_pw_aff_free(pa1);
3492 isl_pw_aff_free(pa2);
3493 return NULL;
3496 /* Compute the quotient of the integer division of "pa1" by "pa2"
3497 * with rounding towards zero.
3498 * "pa2" is assumed to be a piecewise constant.
3500 * In particular, return
3502 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3505 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3506 __isl_take isl_pw_aff *pa2)
3508 int is_cst;
3509 isl_set *cond;
3510 isl_pw_aff *f, *c;
3512 is_cst = isl_pw_aff_is_cst(pa2);
3513 if (is_cst < 0)
3514 goto error;
3515 if (!is_cst)
3516 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3517 "second argument should be a piecewise constant",
3518 goto error);
3520 pa1 = isl_pw_aff_div(pa1, pa2);
3522 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3523 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3524 c = isl_pw_aff_ceil(pa1);
3525 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3526 error:
3527 isl_pw_aff_free(pa1);
3528 isl_pw_aff_free(pa2);
3529 return NULL;
3532 /* Compute the remainder of the integer division of "pa1" by "pa2"
3533 * with rounding towards zero.
3534 * "pa2" is assumed to be a piecewise constant.
3536 * In particular, return
3538 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3541 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3542 __isl_take isl_pw_aff *pa2)
3544 int is_cst;
3545 isl_pw_aff *res;
3547 is_cst = isl_pw_aff_is_cst(pa2);
3548 if (is_cst < 0)
3549 goto error;
3550 if (!is_cst)
3551 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3552 "second argument should be a piecewise constant",
3553 goto error);
3554 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3555 res = isl_pw_aff_mul(pa2, res);
3556 res = isl_pw_aff_sub(pa1, res);
3557 return res;
3558 error:
3559 isl_pw_aff_free(pa1);
3560 isl_pw_aff_free(pa2);
3561 return NULL;
3564 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3565 __isl_take isl_pw_aff *pwaff2)
3567 isl_set *le;
3568 isl_set *dom;
3570 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3571 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3572 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3573 isl_pw_aff_copy(pwaff2));
3574 dom = isl_set_subtract(dom, isl_set_copy(le));
3575 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3578 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3579 __isl_take isl_pw_aff *pwaff2)
3581 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3584 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3585 __isl_take isl_pw_aff *pwaff2)
3587 isl_set *ge;
3588 isl_set *dom;
3590 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3591 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3592 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3593 isl_pw_aff_copy(pwaff2));
3594 dom = isl_set_subtract(dom, isl_set_copy(ge));
3595 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3598 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3599 __isl_take isl_pw_aff *pwaff2)
3601 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3604 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3605 __isl_take isl_pw_aff_list *list,
3606 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3607 __isl_take isl_pw_aff *pwaff2))
3609 int i;
3610 isl_ctx *ctx;
3611 isl_pw_aff *res;
3613 if (!list)
3614 return NULL;
3616 ctx = isl_pw_aff_list_get_ctx(list);
3617 if (list->n < 1)
3618 isl_die(ctx, isl_error_invalid,
3619 "list should contain at least one element", goto error);
3621 res = isl_pw_aff_copy(list->p[0]);
3622 for (i = 1; i < list->n; ++i)
3623 res = fn(res, isl_pw_aff_copy(list->p[i]));
3625 isl_pw_aff_list_free(list);
3626 return res;
3627 error:
3628 isl_pw_aff_list_free(list);
3629 return NULL;
3632 /* Return an isl_pw_aff that maps each element in the intersection of the
3633 * domains of the elements of list to the minimal corresponding affine
3634 * expression.
3636 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3638 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3641 /* Return an isl_pw_aff that maps each element in the intersection of the
3642 * domains of the elements of list to the maximal corresponding affine
3643 * expression.
3645 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3647 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3650 /* Mark the domains of "pwaff" as rational.
3652 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3654 int i;
3656 pwaff = isl_pw_aff_cow(pwaff);
3657 if (!pwaff)
3658 return NULL;
3659 if (pwaff->n == 0)
3660 return pwaff;
3662 for (i = 0; i < pwaff->n; ++i) {
3663 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3664 if (!pwaff->p[i].set)
3665 return isl_pw_aff_free(pwaff);
3668 return pwaff;
3671 /* Mark the domains of the elements of "list" as rational.
3673 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3674 __isl_take isl_pw_aff_list *list)
3676 int i, n;
3678 if (!list)
3679 return NULL;
3680 if (list->n == 0)
3681 return list;
3683 n = list->n;
3684 for (i = 0; i < n; ++i) {
3685 isl_pw_aff *pa;
3687 pa = isl_pw_aff_list_get_pw_aff(list, i);
3688 pa = isl_pw_aff_set_rational(pa);
3689 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3692 return list;
3695 /* Do the parameters of "aff" match those of "space"?
3697 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3698 __isl_keep isl_space *space)
3700 isl_space *aff_space;
3701 int match;
3703 if (!aff || !space)
3704 return -1;
3706 aff_space = isl_aff_get_domain_space(aff);
3708 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3710 isl_space_free(aff_space);
3711 return match;
3714 /* Check that the domain space of "aff" matches "space".
3716 * Return 0 on success and -1 on error.
3718 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3719 __isl_keep isl_space *space)
3721 isl_space *aff_space;
3722 int match;
3724 if (!aff || !space)
3725 return -1;
3727 aff_space = isl_aff_get_domain_space(aff);
3729 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3730 if (match < 0)
3731 goto error;
3732 if (!match)
3733 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3734 "parameters don't match", goto error);
3735 match = isl_space_tuple_is_equal(space, isl_dim_in,
3736 aff_space, isl_dim_set);
3737 if (match < 0)
3738 goto error;
3739 if (!match)
3740 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3741 "domains don't match", goto error);
3742 isl_space_free(aff_space);
3743 return 0;
3744 error:
3745 isl_space_free(aff_space);
3746 return -1;
3749 #undef BASE
3750 #define BASE aff
3751 #undef DOMBASE
3752 #define DOMBASE set
3753 #define NO_DOMAIN
3755 #include <isl_multi_templ.c>
3756 #include <isl_multi_apply_set.c>
3757 #include <isl_multi_cmp.c>
3758 #include <isl_multi_floor.c>
3759 #include <isl_multi_gist.c>
3761 #undef NO_DOMAIN
3763 /* Remove any internal structure of the domain of "ma".
3764 * If there is any such internal structure in the input,
3765 * then the name of the corresponding space is also removed.
3767 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3768 __isl_take isl_multi_aff *ma)
3770 isl_space *space;
3772 if (!ma)
3773 return NULL;
3775 if (!ma->space->nested[0])
3776 return ma;
3778 space = isl_multi_aff_get_space(ma);
3779 space = isl_space_flatten_domain(space);
3780 ma = isl_multi_aff_reset_space(ma, space);
3782 return ma;
3785 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3786 * of the space to its domain.
3788 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3790 int i, n_in;
3791 isl_local_space *ls;
3792 isl_multi_aff *ma;
3794 if (!space)
3795 return NULL;
3796 if (!isl_space_is_map(space))
3797 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3798 "not a map space", goto error);
3800 n_in = isl_space_dim(space, isl_dim_in);
3801 space = isl_space_domain_map(space);
3803 ma = isl_multi_aff_alloc(isl_space_copy(space));
3804 if (n_in == 0) {
3805 isl_space_free(space);
3806 return ma;
3809 space = isl_space_domain(space);
3810 ls = isl_local_space_from_space(space);
3811 for (i = 0; i < n_in; ++i) {
3812 isl_aff *aff;
3814 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3815 isl_dim_set, i);
3816 ma = isl_multi_aff_set_aff(ma, i, aff);
3818 isl_local_space_free(ls);
3819 return ma;
3820 error:
3821 isl_space_free(space);
3822 return NULL;
3825 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3826 * of the space to its range.
3828 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3830 int i, n_in, n_out;
3831 isl_local_space *ls;
3832 isl_multi_aff *ma;
3834 if (!space)
3835 return NULL;
3836 if (!isl_space_is_map(space))
3837 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3838 "not a map space", goto error);
3840 n_in = isl_space_dim(space, isl_dim_in);
3841 n_out = isl_space_dim(space, isl_dim_out);
3842 space = isl_space_range_map(space);
3844 ma = isl_multi_aff_alloc(isl_space_copy(space));
3845 if (n_out == 0) {
3846 isl_space_free(space);
3847 return ma;
3850 space = isl_space_domain(space);
3851 ls = isl_local_space_from_space(space);
3852 for (i = 0; i < n_out; ++i) {
3853 isl_aff *aff;
3855 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3856 isl_dim_set, n_in + i);
3857 ma = isl_multi_aff_set_aff(ma, i, aff);
3859 isl_local_space_free(ls);
3860 return ma;
3861 error:
3862 isl_space_free(space);
3863 return NULL;
3866 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3867 * of the space to its range.
3869 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3870 __isl_take isl_space *space)
3872 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3875 /* Given the space of a set and a range of set dimensions,
3876 * construct an isl_multi_aff that projects out those dimensions.
3878 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3879 __isl_take isl_space *space, enum isl_dim_type type,
3880 unsigned first, unsigned n)
3882 int i, dim;
3883 isl_local_space *ls;
3884 isl_multi_aff *ma;
3886 if (!space)
3887 return NULL;
3888 if (!isl_space_is_set(space))
3889 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3890 "expecting set space", goto error);
3891 if (type != isl_dim_set)
3892 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3893 "only set dimensions can be projected out", goto error);
3895 dim = isl_space_dim(space, isl_dim_set);
3896 if (first + n > dim)
3897 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3898 "range out of bounds", goto error);
3900 space = isl_space_from_domain(space);
3901 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3903 if (dim == n)
3904 return isl_multi_aff_alloc(space);
3906 ma = isl_multi_aff_alloc(isl_space_copy(space));
3907 space = isl_space_domain(space);
3908 ls = isl_local_space_from_space(space);
3910 for (i = 0; i < first; ++i) {
3911 isl_aff *aff;
3913 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3914 isl_dim_set, i);
3915 ma = isl_multi_aff_set_aff(ma, i, aff);
3918 for (i = 0; i < dim - (first + n); ++i) {
3919 isl_aff *aff;
3921 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3922 isl_dim_set, first + n + i);
3923 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3926 isl_local_space_free(ls);
3927 return ma;
3928 error:
3929 isl_space_free(space);
3930 return NULL;
3933 /* Given the space of a set and a range of set dimensions,
3934 * construct an isl_pw_multi_aff that projects out those dimensions.
3936 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3937 __isl_take isl_space *space, enum isl_dim_type type,
3938 unsigned first, unsigned n)
3940 isl_multi_aff *ma;
3942 ma = isl_multi_aff_project_out_map(space, type, first, n);
3943 return isl_pw_multi_aff_from_multi_aff(ma);
3946 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3947 * domain.
3949 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3950 __isl_take isl_multi_aff *ma)
3952 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3953 return isl_pw_multi_aff_alloc(dom, ma);
3956 /* Create a piecewise multi-affine expression in the given space that maps each
3957 * input dimension to the corresponding output dimension.
3959 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3960 __isl_take isl_space *space)
3962 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3965 /* Exploit the equalities in "eq" to simplify the affine expressions.
3967 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3968 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3970 int i;
3972 maff = isl_multi_aff_cow(maff);
3973 if (!maff || !eq)
3974 goto error;
3976 for (i = 0; i < maff->n; ++i) {
3977 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3978 isl_basic_set_copy(eq));
3979 if (!maff->p[i])
3980 goto error;
3983 isl_basic_set_free(eq);
3984 return maff;
3985 error:
3986 isl_basic_set_free(eq);
3987 isl_multi_aff_free(maff);
3988 return NULL;
3991 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3992 isl_int f)
3994 int i;
3996 maff = isl_multi_aff_cow(maff);
3997 if (!maff)
3998 return NULL;
4000 for (i = 0; i < maff->n; ++i) {
4001 maff->p[i] = isl_aff_scale(maff->p[i], f);
4002 if (!maff->p[i])
4003 return isl_multi_aff_free(maff);
4006 return maff;
4009 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4010 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4012 maff1 = isl_multi_aff_add(maff1, maff2);
4013 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4014 return maff1;
4017 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4019 if (!maff)
4020 return -1;
4022 return 0;
4025 /* Return the set of domain elements where "ma1" is lexicographically
4026 * smaller than or equal to "ma2".
4028 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4029 __isl_take isl_multi_aff *ma2)
4031 return isl_multi_aff_lex_ge_set(ma2, ma1);
4034 /* Return the set of domain elements where "ma1" is lexicographically
4035 * smaller than "ma2".
4037 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4038 __isl_take isl_multi_aff *ma2)
4040 return isl_multi_aff_lex_gt_set(ma2, ma1);
4043 /* Return the set of domain elements where "ma1" and "ma2"
4044 * satisfy "order".
4046 static __isl_give isl_set *isl_multi_aff_order_set(
4047 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4048 __isl_give isl_map *order(__isl_take isl_space *set_space))
4050 isl_space *space;
4051 isl_map *map1, *map2;
4052 isl_map *map, *ge;
4054 map1 = isl_map_from_multi_aff(ma1);
4055 map2 = isl_map_from_multi_aff(ma2);
4056 map = isl_map_range_product(map1, map2);
4057 space = isl_space_range(isl_map_get_space(map));
4058 space = isl_space_domain(isl_space_unwrap(space));
4059 ge = order(space);
4060 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4062 return isl_map_domain(map);
4065 /* Return the set of domain elements where "ma1" is lexicographically
4066 * greater than or equal to "ma2".
4068 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4069 __isl_take isl_multi_aff *ma2)
4071 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4074 /* Return the set of domain elements where "ma1" is lexicographically
4075 * greater than "ma2".
4077 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4078 __isl_take isl_multi_aff *ma2)
4080 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4083 #undef PW
4084 #define PW isl_pw_multi_aff
4085 #undef EL
4086 #define EL isl_multi_aff
4087 #undef EL_IS_ZERO
4088 #define EL_IS_ZERO is_empty
4089 #undef ZERO
4090 #define ZERO empty
4091 #undef IS_ZERO
4092 #define IS_ZERO is_empty
4093 #undef FIELD
4094 #define FIELD maff
4095 #undef DEFAULT_IS_ZERO
4096 #define DEFAULT_IS_ZERO 0
4098 #define NO_SUB
4099 #define NO_EVAL
4100 #define NO_OPT
4101 #define NO_INVOLVES_DIMS
4102 #define NO_INSERT_DIMS
4103 #define NO_LIFT
4104 #define NO_MORPH
4106 #include <isl_pw_templ.c>
4107 #include <isl_pw_union_opt.c>
4109 #undef NO_SUB
4111 #undef UNION
4112 #define UNION isl_union_pw_multi_aff
4113 #undef PART
4114 #define PART isl_pw_multi_aff
4115 #undef PARTS
4116 #define PARTS pw_multi_aff
4118 #include <isl_union_multi.c>
4119 #include <isl_union_neg.c>
4121 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4122 __isl_take isl_pw_multi_aff *pma1,
4123 __isl_take isl_pw_multi_aff *pma2)
4125 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4126 &isl_multi_aff_lex_ge_set);
4129 /* Given two piecewise multi affine expressions, return a piecewise
4130 * multi-affine expression defined on the union of the definition domains
4131 * of the inputs that is equal to the lexicographic maximum of the two
4132 * inputs on each cell. If only one of the two inputs is defined on
4133 * a given cell, then it is considered to be the maximum.
4135 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4136 __isl_take isl_pw_multi_aff *pma1,
4137 __isl_take isl_pw_multi_aff *pma2)
4139 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4140 &pw_multi_aff_union_lexmax);
4143 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4144 __isl_take isl_pw_multi_aff *pma1,
4145 __isl_take isl_pw_multi_aff *pma2)
4147 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4148 &isl_multi_aff_lex_le_set);
4151 /* Given two piecewise multi affine expressions, return a piecewise
4152 * multi-affine expression defined on the union of the definition domains
4153 * of the inputs that is equal to the lexicographic minimum of the two
4154 * inputs on each cell. If only one of the two inputs is defined on
4155 * a given cell, then it is considered to be the minimum.
4157 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4158 __isl_take isl_pw_multi_aff *pma1,
4159 __isl_take isl_pw_multi_aff *pma2)
4161 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4162 &pw_multi_aff_union_lexmin);
4165 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4166 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4168 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4169 &isl_multi_aff_add);
4172 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4173 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4175 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4176 &pw_multi_aff_add);
4179 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4180 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4182 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4183 &isl_multi_aff_sub);
4186 /* Subtract "pma2" from "pma1" and return the result.
4188 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4189 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4191 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4192 &pw_multi_aff_sub);
4195 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4196 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4198 return isl_pw_multi_aff_union_add_(pma1, pma2);
4201 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4202 * with the actual sum on the shared domain and
4203 * the defined expression on the symmetric difference of the domains.
4205 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4206 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4208 return isl_union_pw_aff_union_add_(upa1, upa2);
4211 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4212 * with the actual sum on the shared domain and
4213 * the defined expression on the symmetric difference of the domains.
4215 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4216 __isl_take isl_union_pw_multi_aff *upma1,
4217 __isl_take isl_union_pw_multi_aff *upma2)
4219 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4222 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4223 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4225 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4226 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4228 int i, j, n;
4229 isl_space *space;
4230 isl_pw_multi_aff *res;
4232 if (!pma1 || !pma2)
4233 goto error;
4235 n = pma1->n * pma2->n;
4236 space = isl_space_product(isl_space_copy(pma1->dim),
4237 isl_space_copy(pma2->dim));
4238 res = isl_pw_multi_aff_alloc_size(space, n);
4240 for (i = 0; i < pma1->n; ++i) {
4241 for (j = 0; j < pma2->n; ++j) {
4242 isl_set *domain;
4243 isl_multi_aff *ma;
4245 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4246 isl_set_copy(pma2->p[j].set));
4247 ma = isl_multi_aff_product(
4248 isl_multi_aff_copy(pma1->p[i].maff),
4249 isl_multi_aff_copy(pma2->p[j].maff));
4250 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4254 isl_pw_multi_aff_free(pma1);
4255 isl_pw_multi_aff_free(pma2);
4256 return res;
4257 error:
4258 isl_pw_multi_aff_free(pma1);
4259 isl_pw_multi_aff_free(pma2);
4260 return NULL;
4263 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4264 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4266 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4267 &pw_multi_aff_product);
4270 /* Construct a map mapping the domain of the piecewise multi-affine expression
4271 * to its range, with each dimension in the range equated to the
4272 * corresponding affine expression on its cell.
4274 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4276 int i;
4277 isl_map *map;
4279 if (!pma)
4280 return NULL;
4282 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4284 for (i = 0; i < pma->n; ++i) {
4285 isl_multi_aff *maff;
4286 isl_basic_map *bmap;
4287 isl_map *map_i;
4289 maff = isl_multi_aff_copy(pma->p[i].maff);
4290 bmap = isl_basic_map_from_multi_aff(maff);
4291 map_i = isl_map_from_basic_map(bmap);
4292 map_i = isl_map_intersect_domain(map_i,
4293 isl_set_copy(pma->p[i].set));
4294 map = isl_map_union_disjoint(map, map_i);
4297 isl_pw_multi_aff_free(pma);
4298 return map;
4301 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4303 if (!pma)
4304 return NULL;
4306 if (!isl_space_is_set(pma->dim))
4307 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4308 "isl_pw_multi_aff cannot be converted into an isl_set",
4309 goto error);
4311 return isl_map_from_pw_multi_aff(pma);
4312 error:
4313 isl_pw_multi_aff_free(pma);
4314 return NULL;
4317 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4318 * denominator "denom".
4319 * "denom" is allowed to be negative, in which case the actual denominator
4320 * is -denom and the expressions are added instead.
4322 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4323 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4325 int i, first;
4326 int sign;
4327 isl_int d;
4329 first = isl_seq_first_non_zero(c, n);
4330 if (first == -1)
4331 return aff;
4333 sign = isl_int_sgn(denom);
4334 isl_int_init(d);
4335 isl_int_abs(d, denom);
4336 for (i = first; i < n; ++i) {
4337 isl_aff *aff_i;
4339 if (isl_int_is_zero(c[i]))
4340 continue;
4341 aff_i = isl_multi_aff_get_aff(ma, i);
4342 aff_i = isl_aff_scale(aff_i, c[i]);
4343 aff_i = isl_aff_scale_down(aff_i, d);
4344 if (sign >= 0)
4345 aff = isl_aff_sub(aff, aff_i);
4346 else
4347 aff = isl_aff_add(aff, aff_i);
4349 isl_int_clear(d);
4351 return aff;
4354 /* Extract an affine expression that expresses the output dimension "pos"
4355 * of "bmap" in terms of the parameters and input dimensions from
4356 * equality "eq".
4357 * Note that this expression may involve integer divisions defined
4358 * in terms of parameters and input dimensions.
4359 * The equality may also involve references to earlier (but not later)
4360 * output dimensions. These are replaced by the corresponding elements
4361 * in "ma".
4363 * If the equality is of the form
4365 * f(i) + h(j) + a x + g(i) = 0,
4367 * with f(i) a linear combinations of the parameters and input dimensions,
4368 * g(i) a linear combination of integer divisions defined in terms of the same
4369 * and h(j) a linear combinations of earlier output dimensions,
4370 * then the affine expression is
4372 * (-f(i) - g(i))/a - h(j)/a
4374 * If the equality is of the form
4376 * f(i) + h(j) - a x + g(i) = 0,
4378 * then the affine expression is
4380 * (f(i) + g(i))/a - h(j)/(-a)
4383 * If "div" refers to an integer division (i.e., it is smaller than
4384 * the number of integer divisions), then the equality constraint
4385 * does involve an integer division (the one at position "div") that
4386 * is defined in terms of output dimensions. However, this integer
4387 * division can be eliminated by exploiting a pair of constraints
4388 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4389 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4390 * -l + x >= 0.
4391 * In particular, let
4393 * x = e(i) + m floor(...)
4395 * with e(i) the expression derived above and floor(...) the integer
4396 * division involving output dimensions.
4397 * From
4399 * l <= x <= l + n,
4401 * we have
4403 * 0 <= x - l <= n
4405 * This means
4407 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4408 * = (e(i) - l) mod m
4410 * Therefore,
4412 * x - l = (e(i) - l) mod m
4414 * or
4416 * x = ((e(i) - l) mod m) + l
4418 * The variable "shift" below contains the expression -l, which may
4419 * also involve a linear combination of earlier output dimensions.
4421 static __isl_give isl_aff *extract_aff_from_equality(
4422 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4423 __isl_keep isl_multi_aff *ma)
4425 unsigned o_out;
4426 unsigned n_div, n_out;
4427 isl_ctx *ctx;
4428 isl_local_space *ls;
4429 isl_aff *aff, *shift;
4430 isl_val *mod;
4432 ctx = isl_basic_map_get_ctx(bmap);
4433 ls = isl_basic_map_get_local_space(bmap);
4434 ls = isl_local_space_domain(ls);
4435 aff = isl_aff_alloc(isl_local_space_copy(ls));
4436 if (!aff)
4437 goto error;
4438 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4439 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4440 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4441 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4442 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4443 isl_seq_cpy(aff->v->el + 1 + o_out,
4444 bmap->eq[eq] + o_out + n_out, n_div);
4445 } else {
4446 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4447 isl_seq_neg(aff->v->el + 1 + o_out,
4448 bmap->eq[eq] + o_out + n_out, n_div);
4450 if (div < n_div)
4451 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4452 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4453 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4454 bmap->eq[eq][o_out + pos]);
4455 if (div < n_div) {
4456 shift = isl_aff_alloc(isl_local_space_copy(ls));
4457 if (!shift)
4458 goto error;
4459 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4460 isl_seq_cpy(shift->v->el + 1 + o_out,
4461 bmap->ineq[ineq] + o_out + n_out, n_div);
4462 isl_int_set_si(shift->v->el[0], 1);
4463 shift = subtract_initial(shift, ma, pos,
4464 bmap->ineq[ineq] + o_out, ctx->negone);
4465 aff = isl_aff_add(aff, isl_aff_copy(shift));
4466 mod = isl_val_int_from_isl_int(ctx,
4467 bmap->eq[eq][o_out + n_out + div]);
4468 mod = isl_val_abs(mod);
4469 aff = isl_aff_mod_val(aff, mod);
4470 aff = isl_aff_sub(aff, shift);
4473 isl_local_space_free(ls);
4474 return aff;
4475 error:
4476 isl_local_space_free(ls);
4477 isl_aff_free(aff);
4478 return NULL;
4481 /* Given a basic map with output dimensions defined
4482 * in terms of the parameters input dimensions and earlier
4483 * output dimensions using an equality (and possibly a pair on inequalities),
4484 * extract an isl_aff that expresses output dimension "pos" in terms
4485 * of the parameters and input dimensions.
4486 * Note that this expression may involve integer divisions defined
4487 * in terms of parameters and input dimensions.
4488 * "ma" contains the expressions corresponding to earlier output dimensions.
4490 * This function shares some similarities with
4491 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4493 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4494 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4496 int eq, div, ineq;
4497 isl_aff *aff;
4499 if (!bmap)
4500 return NULL;
4501 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4502 if (eq >= bmap->n_eq)
4503 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4504 "unable to find suitable equality", return NULL);
4505 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4507 aff = isl_aff_remove_unused_divs(aff);
4508 return aff;
4511 /* Given a basic map where each output dimension is defined
4512 * in terms of the parameters and input dimensions using an equality,
4513 * extract an isl_multi_aff that expresses the output dimensions in terms
4514 * of the parameters and input dimensions.
4516 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4517 __isl_take isl_basic_map *bmap)
4519 int i;
4520 unsigned n_out;
4521 isl_multi_aff *ma;
4523 if (!bmap)
4524 return NULL;
4526 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4527 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4529 for (i = 0; i < n_out; ++i) {
4530 isl_aff *aff;
4532 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4533 ma = isl_multi_aff_set_aff(ma, i, aff);
4536 isl_basic_map_free(bmap);
4538 return ma;
4541 /* Given a basic set where each set dimension is defined
4542 * in terms of the parameters using an equality,
4543 * extract an isl_multi_aff that expresses the set dimensions in terms
4544 * of the parameters.
4546 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4547 __isl_take isl_basic_set *bset)
4549 return extract_isl_multi_aff_from_basic_map(bset);
4552 /* Create an isl_pw_multi_aff that is equivalent to
4553 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4554 * The given basic map is such that each output dimension is defined
4555 * in terms of the parameters and input dimensions using an equality.
4557 * Since some applications expect the result of isl_pw_multi_aff_from_map
4558 * to only contain integer affine expressions, we compute the floor
4559 * of the expression before returning.
4561 * Remove all constraints involving local variables without
4562 * an explicit representation (resulting in the removal of those
4563 * local variables) prior to the actual extraction to ensure
4564 * that the local spaces in which the resulting affine expressions
4565 * are created do not contain any unknown local variables.
4566 * Removing such constraints is safe because constraints involving
4567 * unknown local variables are not used to determine whether
4568 * a basic map is obviously single-valued.
4570 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4571 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4573 isl_multi_aff *ma;
4575 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4576 ma = extract_isl_multi_aff_from_basic_map(bmap);
4577 ma = isl_multi_aff_floor(ma);
4578 return isl_pw_multi_aff_alloc(domain, ma);
4581 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4582 * This obviously only works if the input "map" is single-valued.
4583 * If so, we compute the lexicographic minimum of the image in the form
4584 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4585 * to its lexicographic minimum.
4586 * If the input is not single-valued, we produce an error.
4588 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4589 __isl_take isl_map *map)
4591 int i;
4592 int sv;
4593 isl_pw_multi_aff *pma;
4595 sv = isl_map_is_single_valued(map);
4596 if (sv < 0)
4597 goto error;
4598 if (!sv)
4599 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4600 "map is not single-valued", goto error);
4601 map = isl_map_make_disjoint(map);
4602 if (!map)
4603 return NULL;
4605 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4607 for (i = 0; i < map->n; ++i) {
4608 isl_pw_multi_aff *pma_i;
4609 isl_basic_map *bmap;
4610 bmap = isl_basic_map_copy(map->p[i]);
4611 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4612 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4615 isl_map_free(map);
4616 return pma;
4617 error:
4618 isl_map_free(map);
4619 return NULL;
4622 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4623 * taking into account that the output dimension at position "d"
4624 * can be represented as
4626 * x = floor((e(...) + c1) / m)
4628 * given that constraint "i" is of the form
4630 * e(...) + c1 - m x >= 0
4633 * Let "map" be of the form
4635 * A -> B
4637 * We construct a mapping
4639 * A -> [A -> x = floor(...)]
4641 * apply that to the map, obtaining
4643 * [A -> x = floor(...)] -> B
4645 * and equate dimension "d" to x.
4646 * We then compute a isl_pw_multi_aff representation of the resulting map
4647 * and plug in the mapping above.
4649 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4650 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4652 isl_ctx *ctx;
4653 isl_space *space;
4654 isl_local_space *ls;
4655 isl_multi_aff *ma;
4656 isl_aff *aff;
4657 isl_vec *v;
4658 isl_map *insert;
4659 int offset;
4660 int n;
4661 int n_in;
4662 isl_pw_multi_aff *pma;
4663 int is_set;
4665 is_set = isl_map_is_set(map);
4667 offset = isl_basic_map_offset(hull, isl_dim_out);
4668 ctx = isl_map_get_ctx(map);
4669 space = isl_space_domain(isl_map_get_space(map));
4670 n_in = isl_space_dim(space, isl_dim_set);
4671 n = isl_space_dim(space, isl_dim_all);
4673 v = isl_vec_alloc(ctx, 1 + 1 + n);
4674 if (v) {
4675 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4676 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4678 isl_basic_map_free(hull);
4680 ls = isl_local_space_from_space(isl_space_copy(space));
4681 aff = isl_aff_alloc_vec(ls, v);
4682 aff = isl_aff_floor(aff);
4683 if (is_set) {
4684 isl_space_free(space);
4685 ma = isl_multi_aff_from_aff(aff);
4686 } else {
4687 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4688 ma = isl_multi_aff_range_product(ma,
4689 isl_multi_aff_from_aff(aff));
4692 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4693 map = isl_map_apply_domain(map, insert);
4694 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4695 pma = isl_pw_multi_aff_from_map(map);
4696 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4698 return pma;
4701 /* Is constraint "c" of the form
4703 * e(...) + c1 - m x >= 0
4705 * or
4707 * -e(...) + c2 + m x >= 0
4709 * where m > 1 and e only depends on parameters and input dimemnsions?
4711 * "offset" is the offset of the output dimensions
4712 * "pos" is the position of output dimension x.
4714 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4716 if (isl_int_is_zero(c[offset + d]))
4717 return 0;
4718 if (isl_int_is_one(c[offset + d]))
4719 return 0;
4720 if (isl_int_is_negone(c[offset + d]))
4721 return 0;
4722 if (isl_seq_first_non_zero(c + offset, d) != -1)
4723 return 0;
4724 if (isl_seq_first_non_zero(c + offset + d + 1,
4725 total - (offset + d + 1)) != -1)
4726 return 0;
4727 return 1;
4730 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4732 * As a special case, we first check if there is any pair of constraints,
4733 * shared by all the basic maps in "map" that force a given dimension
4734 * to be equal to the floor of some affine combination of the input dimensions.
4736 * In particular, if we can find two constraints
4738 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4740 * and
4742 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4744 * where m > 1 and e only depends on parameters and input dimemnsions,
4745 * and such that
4747 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4749 * then we know that we can take
4751 * x = floor((e(...) + c1) / m)
4753 * without having to perform any computation.
4755 * Note that we know that
4757 * c1 + c2 >= 1
4759 * If c1 + c2 were 0, then we would have detected an equality during
4760 * simplification. If c1 + c2 were negative, then we would have detected
4761 * a contradiction.
4763 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4764 __isl_take isl_map *map)
4766 int d, dim;
4767 int i, j, n;
4768 int offset, total;
4769 isl_int sum;
4770 isl_basic_map *hull;
4772 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4773 if (!hull)
4774 goto error;
4776 isl_int_init(sum);
4777 dim = isl_map_dim(map, isl_dim_out);
4778 offset = isl_basic_map_offset(hull, isl_dim_out);
4779 total = 1 + isl_basic_map_total_dim(hull);
4780 n = hull->n_ineq;
4781 for (d = 0; d < dim; ++d) {
4782 for (i = 0; i < n; ++i) {
4783 if (!is_potential_div_constraint(hull->ineq[i],
4784 offset, d, total))
4785 continue;
4786 for (j = i + 1; j < n; ++j) {
4787 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4788 hull->ineq[j] + 1, total - 1))
4789 continue;
4790 isl_int_add(sum, hull->ineq[i][0],
4791 hull->ineq[j][0]);
4792 if (isl_int_abs_lt(sum,
4793 hull->ineq[i][offset + d]))
4794 break;
4797 if (j >= n)
4798 continue;
4799 isl_int_clear(sum);
4800 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4801 j = i;
4802 return pw_multi_aff_from_map_div(map, hull, d, j);
4805 isl_int_clear(sum);
4806 isl_basic_map_free(hull);
4807 return pw_multi_aff_from_map_base(map);
4808 error:
4809 isl_map_free(map);
4810 isl_basic_map_free(hull);
4811 return NULL;
4814 /* Given an affine expression
4816 * [A -> B] -> f(A,B)
4818 * construct an isl_multi_aff
4820 * [A -> B] -> B'
4822 * such that dimension "d" in B' is set to "aff" and the remaining
4823 * dimensions are set equal to the corresponding dimensions in B.
4824 * "n_in" is the dimension of the space A.
4825 * "n_out" is the dimension of the space B.
4827 * If "is_set" is set, then the affine expression is of the form
4829 * [B] -> f(B)
4831 * and we construct an isl_multi_aff
4833 * B -> B'
4835 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4836 unsigned n_in, unsigned n_out, int is_set)
4838 int i;
4839 isl_multi_aff *ma;
4840 isl_space *space, *space2;
4841 isl_local_space *ls;
4843 space = isl_aff_get_domain_space(aff);
4844 ls = isl_local_space_from_space(isl_space_copy(space));
4845 space2 = isl_space_copy(space);
4846 if (!is_set)
4847 space2 = isl_space_range(isl_space_unwrap(space2));
4848 space = isl_space_map_from_domain_and_range(space, space2);
4849 ma = isl_multi_aff_alloc(space);
4850 ma = isl_multi_aff_set_aff(ma, d, aff);
4852 for (i = 0; i < n_out; ++i) {
4853 if (i == d)
4854 continue;
4855 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4856 isl_dim_set, n_in + i);
4857 ma = isl_multi_aff_set_aff(ma, i, aff);
4860 isl_local_space_free(ls);
4862 return ma;
4865 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4866 * taking into account that the dimension at position "d" can be written as
4868 * x = m a + f(..) (1)
4870 * where m is equal to "gcd".
4871 * "i" is the index of the equality in "hull" that defines f(..).
4872 * In particular, the equality is of the form
4874 * f(..) - x + m g(existentials) = 0
4876 * or
4878 * -f(..) + x + m g(existentials) = 0
4880 * We basically plug (1) into "map", resulting in a map with "a"
4881 * in the range instead of "x". The corresponding isl_pw_multi_aff
4882 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4884 * Specifically, given the input map
4886 * A -> B
4888 * We first wrap it into a set
4890 * [A -> B]
4892 * and define (1) on top of the corresponding space, resulting in "aff".
4893 * We use this to create an isl_multi_aff that maps the output position "d"
4894 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4895 * We plug this into the wrapped map, unwrap the result and compute the
4896 * corresponding isl_pw_multi_aff.
4897 * The result is an expression
4899 * A -> T(A)
4901 * We adjust that to
4903 * A -> [A -> T(A)]
4905 * so that we can plug that into "aff", after extending the latter to
4906 * a mapping
4908 * [A -> B] -> B'
4911 * If "map" is actually a set, then there is no "A" space, meaning
4912 * that we do not need to perform any wrapping, and that the result
4913 * of the recursive call is of the form
4915 * [T]
4917 * which is plugged into a mapping of the form
4919 * B -> B'
4921 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4922 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4923 isl_int gcd)
4925 isl_set *set;
4926 isl_space *space;
4927 isl_local_space *ls;
4928 isl_aff *aff;
4929 isl_multi_aff *ma;
4930 isl_pw_multi_aff *pma, *id;
4931 unsigned n_in;
4932 unsigned o_out;
4933 unsigned n_out;
4934 int is_set;
4936 is_set = isl_map_is_set(map);
4938 n_in = isl_basic_map_dim(hull, isl_dim_in);
4939 n_out = isl_basic_map_dim(hull, isl_dim_out);
4940 o_out = isl_basic_map_offset(hull, isl_dim_out);
4942 if (is_set)
4943 set = map;
4944 else
4945 set = isl_map_wrap(map);
4946 space = isl_space_map_from_set(isl_set_get_space(set));
4947 ma = isl_multi_aff_identity(space);
4948 ls = isl_local_space_from_space(isl_set_get_space(set));
4949 aff = isl_aff_alloc(ls);
4950 if (aff) {
4951 isl_int_set_si(aff->v->el[0], 1);
4952 if (isl_int_is_one(hull->eq[i][o_out + d]))
4953 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4954 aff->v->size - 1);
4955 else
4956 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4957 aff->v->size - 1);
4958 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4960 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4961 set = isl_set_preimage_multi_aff(set, ma);
4963 ma = range_map(aff, d, n_in, n_out, is_set);
4965 if (is_set)
4966 map = set;
4967 else
4968 map = isl_set_unwrap(set);
4969 pma = isl_pw_multi_aff_from_map(map);
4971 if (!is_set) {
4972 space = isl_pw_multi_aff_get_domain_space(pma);
4973 space = isl_space_map_from_set(space);
4974 id = isl_pw_multi_aff_identity(space);
4975 pma = isl_pw_multi_aff_range_product(id, pma);
4977 id = isl_pw_multi_aff_from_multi_aff(ma);
4978 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4980 isl_basic_map_free(hull);
4981 return pma;
4984 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4986 * As a special case, we first check if all output dimensions are uniquely
4987 * defined in terms of the parameters and input dimensions over the entire
4988 * domain. If so, we extract the desired isl_pw_multi_aff directly
4989 * from the affine hull of "map" and its domain.
4991 * Otherwise, we check if any of the output dimensions is "strided".
4992 * That is, we check if can be written as
4994 * x = m a + f(..)
4996 * with m greater than 1, a some combination of existentially quantified
4997 * variables and f an expression in the parameters and input dimensions.
4998 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5000 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5001 * special case.
5003 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5005 int i, j;
5006 isl_bool sv;
5007 isl_basic_map *hull;
5008 unsigned n_out;
5009 unsigned o_out;
5010 unsigned n_div;
5011 unsigned o_div;
5012 isl_int gcd;
5014 if (!map)
5015 return NULL;
5017 map = isl_map_detect_equalities(map);
5018 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5019 sv = isl_basic_map_plain_is_single_valued(hull);
5020 if (sv >= 0 && sv)
5021 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5022 if (sv < 0)
5023 hull = isl_basic_map_free(hull);
5024 if (!hull)
5025 goto error;
5027 n_div = isl_basic_map_dim(hull, isl_dim_div);
5028 o_div = isl_basic_map_offset(hull, isl_dim_div);
5030 if (n_div == 0) {
5031 isl_basic_map_free(hull);
5032 return pw_multi_aff_from_map_check_div(map);
5035 isl_int_init(gcd);
5037 n_out = isl_basic_map_dim(hull, isl_dim_out);
5038 o_out = isl_basic_map_offset(hull, isl_dim_out);
5040 for (i = 0; i < n_out; ++i) {
5041 for (j = 0; j < hull->n_eq; ++j) {
5042 isl_int *eq = hull->eq[j];
5043 isl_pw_multi_aff *res;
5045 if (!isl_int_is_one(eq[o_out + i]) &&
5046 !isl_int_is_negone(eq[o_out + i]))
5047 continue;
5048 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5049 continue;
5050 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5051 n_out - (i + 1)) != -1)
5052 continue;
5053 isl_seq_gcd(eq + o_div, n_div, &gcd);
5054 if (isl_int_is_zero(gcd))
5055 continue;
5056 if (isl_int_is_one(gcd))
5057 continue;
5059 res = pw_multi_aff_from_map_stride(map, hull,
5060 i, j, gcd);
5061 isl_int_clear(gcd);
5062 return res;
5066 isl_int_clear(gcd);
5067 isl_basic_map_free(hull);
5068 return pw_multi_aff_from_map_check_div(map);
5069 error:
5070 isl_map_free(map);
5071 return NULL;
5074 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5076 return isl_pw_multi_aff_from_map(set);
5079 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5080 * add it to *user.
5082 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5084 isl_union_pw_multi_aff **upma = user;
5085 isl_pw_multi_aff *pma;
5087 pma = isl_pw_multi_aff_from_map(map);
5088 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5090 return *upma ? isl_stat_ok : isl_stat_error;
5093 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5094 * domain.
5096 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5097 __isl_take isl_aff *aff)
5099 isl_multi_aff *ma;
5100 isl_pw_multi_aff *pma;
5102 ma = isl_multi_aff_from_aff(aff);
5103 pma = isl_pw_multi_aff_from_multi_aff(ma);
5104 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5107 /* Try and create an isl_union_pw_multi_aff that is equivalent
5108 * to the given isl_union_map.
5109 * The isl_union_map is required to be single-valued in each space.
5110 * Otherwise, an error is produced.
5112 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5113 __isl_take isl_union_map *umap)
5115 isl_space *space;
5116 isl_union_pw_multi_aff *upma;
5118 space = isl_union_map_get_space(umap);
5119 upma = isl_union_pw_multi_aff_empty(space);
5120 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5121 upma = isl_union_pw_multi_aff_free(upma);
5122 isl_union_map_free(umap);
5124 return upma;
5127 /* Try and create an isl_union_pw_multi_aff that is equivalent
5128 * to the given isl_union_set.
5129 * The isl_union_set is required to be a singleton in each space.
5130 * Otherwise, an error is produced.
5132 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5133 __isl_take isl_union_set *uset)
5135 return isl_union_pw_multi_aff_from_union_map(uset);
5138 /* Return the piecewise affine expression "set ? 1 : 0".
5140 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5142 isl_pw_aff *pa;
5143 isl_space *space = isl_set_get_space(set);
5144 isl_local_space *ls = isl_local_space_from_space(space);
5145 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5146 isl_aff *one = isl_aff_zero_on_domain(ls);
5148 one = isl_aff_add_constant_si(one, 1);
5149 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5150 set = isl_set_complement(set);
5151 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5153 return pa;
5156 /* Plug in "subs" for dimension "type", "pos" of "aff".
5158 * Let i be the dimension to replace and let "subs" be of the form
5160 * f/d
5162 * and "aff" of the form
5164 * (a i + g)/m
5166 * The result is
5168 * (a f + d g')/(m d)
5170 * where g' is the result of plugging in "subs" in each of the integer
5171 * divisions in g.
5173 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5174 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5176 isl_ctx *ctx;
5177 isl_int v;
5179 aff = isl_aff_cow(aff);
5180 if (!aff || !subs)
5181 return isl_aff_free(aff);
5183 ctx = isl_aff_get_ctx(aff);
5184 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5185 isl_die(ctx, isl_error_invalid,
5186 "spaces don't match", return isl_aff_free(aff));
5187 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5188 isl_die(ctx, isl_error_unsupported,
5189 "cannot handle divs yet", return isl_aff_free(aff));
5191 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5192 if (!aff->ls)
5193 return isl_aff_free(aff);
5195 aff->v = isl_vec_cow(aff->v);
5196 if (!aff->v)
5197 return isl_aff_free(aff);
5199 pos += isl_local_space_offset(aff->ls, type);
5201 isl_int_init(v);
5202 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5203 aff->v->size, subs->v->size, v);
5204 isl_int_clear(v);
5206 return aff;
5209 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5210 * expressions in "maff".
5212 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5213 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5214 __isl_keep isl_aff *subs)
5216 int i;
5218 maff = isl_multi_aff_cow(maff);
5219 if (!maff || !subs)
5220 return isl_multi_aff_free(maff);
5222 if (type == isl_dim_in)
5223 type = isl_dim_set;
5225 for (i = 0; i < maff->n; ++i) {
5226 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5227 if (!maff->p[i])
5228 return isl_multi_aff_free(maff);
5231 return maff;
5234 /* Plug in "subs" for dimension "type", "pos" of "pma".
5236 * pma is of the form
5238 * A_i(v) -> M_i(v)
5240 * while subs is of the form
5242 * v' = B_j(v) -> S_j
5244 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5245 * has a contribution in the result, in particular
5247 * C_ij(S_j) -> M_i(S_j)
5249 * Note that plugging in S_j in C_ij may also result in an empty set
5250 * and this contribution should simply be discarded.
5252 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5253 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5254 __isl_keep isl_pw_aff *subs)
5256 int i, j, n;
5257 isl_pw_multi_aff *res;
5259 if (!pma || !subs)
5260 return isl_pw_multi_aff_free(pma);
5262 n = pma->n * subs->n;
5263 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5265 for (i = 0; i < pma->n; ++i) {
5266 for (j = 0; j < subs->n; ++j) {
5267 isl_set *common;
5268 isl_multi_aff *res_ij;
5269 int empty;
5271 common = isl_set_intersect(
5272 isl_set_copy(pma->p[i].set),
5273 isl_set_copy(subs->p[j].set));
5274 common = isl_set_substitute(common,
5275 type, pos, subs->p[j].aff);
5276 empty = isl_set_plain_is_empty(common);
5277 if (empty < 0 || empty) {
5278 isl_set_free(common);
5279 if (empty < 0)
5280 goto error;
5281 continue;
5284 res_ij = isl_multi_aff_substitute(
5285 isl_multi_aff_copy(pma->p[i].maff),
5286 type, pos, subs->p[j].aff);
5288 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5292 isl_pw_multi_aff_free(pma);
5293 return res;
5294 error:
5295 isl_pw_multi_aff_free(pma);
5296 isl_pw_multi_aff_free(res);
5297 return NULL;
5300 /* Compute the preimage of a range of dimensions in the affine expression "src"
5301 * under "ma" and put the result in "dst". The number of dimensions in "src"
5302 * that precede the range is given by "n_before". The number of dimensions
5303 * in the range is given by the number of output dimensions of "ma".
5304 * The number of dimensions that follow the range is given by "n_after".
5305 * If "has_denom" is set (to one),
5306 * then "src" and "dst" have an extra initial denominator.
5307 * "n_div_ma" is the number of existentials in "ma"
5308 * "n_div_bset" is the number of existentials in "src"
5309 * The resulting "dst" (which is assumed to have been allocated by
5310 * the caller) contains coefficients for both sets of existentials,
5311 * first those in "ma" and then those in "src".
5312 * f, c1, c2 and g are temporary objects that have been initialized
5313 * by the caller.
5315 * Let src represent the expression
5317 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5319 * and let ma represent the expressions
5321 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5323 * We start out with the following expression for dst:
5325 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5327 * with the multiplication factor f initially equal to 1
5328 * and f \sum_i b_i v_i kept separately.
5329 * For each x_i that we substitute, we multiply the numerator
5330 * (and denominator) of dst by c_1 = m_i and add the numerator
5331 * of the x_i expression multiplied by c_2 = f b_i,
5332 * after removing the common factors of c_1 and c_2.
5333 * The multiplication factor f also needs to be multiplied by c_1
5334 * for the next x_j, j > i.
5336 void isl_seq_preimage(isl_int *dst, isl_int *src,
5337 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5338 int n_div_ma, int n_div_bmap,
5339 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5341 int i;
5342 int n_param, n_in, n_out;
5343 int o_dst, o_src;
5345 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5346 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5347 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5349 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5350 o_dst = o_src = has_denom + 1 + n_param + n_before;
5351 isl_seq_clr(dst + o_dst, n_in);
5352 o_dst += n_in;
5353 o_src += n_out;
5354 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5355 o_dst += n_after;
5356 o_src += n_after;
5357 isl_seq_clr(dst + o_dst, n_div_ma);
5358 o_dst += n_div_ma;
5359 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5361 isl_int_set_si(f, 1);
5363 for (i = 0; i < n_out; ++i) {
5364 int offset = has_denom + 1 + n_param + n_before + i;
5366 if (isl_int_is_zero(src[offset]))
5367 continue;
5368 isl_int_set(c1, ma->p[i]->v->el[0]);
5369 isl_int_mul(c2, f, src[offset]);
5370 isl_int_gcd(g, c1, c2);
5371 isl_int_divexact(c1, c1, g);
5372 isl_int_divexact(c2, c2, g);
5374 isl_int_mul(f, f, c1);
5375 o_dst = has_denom;
5376 o_src = 1;
5377 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5378 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5379 o_dst += 1 + n_param;
5380 o_src += 1 + n_param;
5381 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5382 o_dst += n_before;
5383 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5384 c2, ma->p[i]->v->el + o_src, n_in);
5385 o_dst += n_in;
5386 o_src += n_in;
5387 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5388 o_dst += n_after;
5389 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5390 c2, ma->p[i]->v->el + o_src, n_div_ma);
5391 o_dst += n_div_ma;
5392 o_src += n_div_ma;
5393 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5394 if (has_denom)
5395 isl_int_mul(dst[0], dst[0], c1);
5399 /* Compute the pullback of "aff" by the function represented by "ma".
5400 * In other words, plug in "ma" in "aff". The result is an affine expression
5401 * defined over the domain space of "ma".
5403 * If "aff" is represented by
5405 * (a(p) + b x + c(divs))/d
5407 * and ma is represented by
5409 * x = D(p) + F(y) + G(divs')
5411 * then the result is
5413 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5415 * The divs in the local space of the input are similarly adjusted
5416 * through a call to isl_local_space_preimage_multi_aff.
5418 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5419 __isl_take isl_multi_aff *ma)
5421 isl_aff *res = NULL;
5422 isl_local_space *ls;
5423 int n_div_aff, n_div_ma;
5424 isl_int f, c1, c2, g;
5426 ma = isl_multi_aff_align_divs(ma);
5427 if (!aff || !ma)
5428 goto error;
5430 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5431 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5433 ls = isl_aff_get_domain_local_space(aff);
5434 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5435 res = isl_aff_alloc(ls);
5436 if (!res)
5437 goto error;
5439 isl_int_init(f);
5440 isl_int_init(c1);
5441 isl_int_init(c2);
5442 isl_int_init(g);
5444 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5445 f, c1, c2, g, 1);
5447 isl_int_clear(f);
5448 isl_int_clear(c1);
5449 isl_int_clear(c2);
5450 isl_int_clear(g);
5452 isl_aff_free(aff);
5453 isl_multi_aff_free(ma);
5454 res = isl_aff_normalize(res);
5455 return res;
5456 error:
5457 isl_aff_free(aff);
5458 isl_multi_aff_free(ma);
5459 isl_aff_free(res);
5460 return NULL;
5463 /* Compute the pullback of "aff1" by the function represented by "aff2".
5464 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5465 * defined over the domain space of "aff1".
5467 * The domain of "aff1" should match the range of "aff2", which means
5468 * that it should be single-dimensional.
5470 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5471 __isl_take isl_aff *aff2)
5473 isl_multi_aff *ma;
5475 ma = isl_multi_aff_from_aff(aff2);
5476 return isl_aff_pullback_multi_aff(aff1, ma);
5479 /* Compute the pullback of "ma1" by the function represented by "ma2".
5480 * In other words, plug in "ma2" in "ma1".
5482 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5484 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5485 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5487 int i;
5488 isl_space *space = NULL;
5490 ma2 = isl_multi_aff_align_divs(ma2);
5491 ma1 = isl_multi_aff_cow(ma1);
5492 if (!ma1 || !ma2)
5493 goto error;
5495 space = isl_space_join(isl_multi_aff_get_space(ma2),
5496 isl_multi_aff_get_space(ma1));
5498 for (i = 0; i < ma1->n; ++i) {
5499 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5500 isl_multi_aff_copy(ma2));
5501 if (!ma1->p[i])
5502 goto error;
5505 ma1 = isl_multi_aff_reset_space(ma1, space);
5506 isl_multi_aff_free(ma2);
5507 return ma1;
5508 error:
5509 isl_space_free(space);
5510 isl_multi_aff_free(ma2);
5511 isl_multi_aff_free(ma1);
5512 return NULL;
5515 /* Compute the pullback of "ma1" by the function represented by "ma2".
5516 * In other words, plug in "ma2" in "ma1".
5518 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5519 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5521 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5522 &isl_multi_aff_pullback_multi_aff_aligned);
5525 /* Extend the local space of "dst" to include the divs
5526 * in the local space of "src".
5528 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5529 __isl_keep isl_aff *src)
5531 isl_ctx *ctx;
5532 int *exp1 = NULL;
5533 int *exp2 = NULL;
5534 isl_mat *div;
5536 if (!src || !dst)
5537 return isl_aff_free(dst);
5539 ctx = isl_aff_get_ctx(src);
5540 if (!isl_space_is_equal(src->ls->dim, dst->ls->dim))
5541 isl_die(ctx, isl_error_invalid,
5542 "spaces don't match", goto error);
5544 if (src->ls->div->n_row == 0)
5545 return dst;
5547 exp1 = isl_alloc_array(ctx, int, src->ls->div->n_row);
5548 exp2 = isl_alloc_array(ctx, int, dst->ls->div->n_row);
5549 if (!exp1 || (dst->ls->div->n_row && !exp2))
5550 goto error;
5552 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5553 dst = isl_aff_expand_divs(dst, div, exp2);
5554 free(exp1);
5555 free(exp2);
5557 return dst;
5558 error:
5559 free(exp1);
5560 free(exp2);
5561 return isl_aff_free(dst);
5564 /* Adjust the local spaces of the affine expressions in "maff"
5565 * such that they all have the save divs.
5567 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5568 __isl_take isl_multi_aff *maff)
5570 int i;
5572 if (!maff)
5573 return NULL;
5574 if (maff->n == 0)
5575 return maff;
5576 maff = isl_multi_aff_cow(maff);
5577 if (!maff)
5578 return NULL;
5580 for (i = 1; i < maff->n; ++i)
5581 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5582 for (i = 1; i < maff->n; ++i) {
5583 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5584 if (!maff->p[i])
5585 return isl_multi_aff_free(maff);
5588 return maff;
5591 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5593 aff = isl_aff_cow(aff);
5594 if (!aff)
5595 return NULL;
5597 aff->ls = isl_local_space_lift(aff->ls);
5598 if (!aff->ls)
5599 return isl_aff_free(aff);
5601 return aff;
5604 /* Lift "maff" to a space with extra dimensions such that the result
5605 * has no more existentially quantified variables.
5606 * If "ls" is not NULL, then *ls is assigned the local space that lies
5607 * at the basis of the lifting applied to "maff".
5609 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5610 __isl_give isl_local_space **ls)
5612 int i;
5613 isl_space *space;
5614 unsigned n_div;
5616 if (ls)
5617 *ls = NULL;
5619 if (!maff)
5620 return NULL;
5622 if (maff->n == 0) {
5623 if (ls) {
5624 isl_space *space = isl_multi_aff_get_domain_space(maff);
5625 *ls = isl_local_space_from_space(space);
5626 if (!*ls)
5627 return isl_multi_aff_free(maff);
5629 return maff;
5632 maff = isl_multi_aff_cow(maff);
5633 maff = isl_multi_aff_align_divs(maff);
5634 if (!maff)
5635 return NULL;
5637 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5638 space = isl_multi_aff_get_space(maff);
5639 space = isl_space_lift(isl_space_domain(space), n_div);
5640 space = isl_space_extend_domain_with_range(space,
5641 isl_multi_aff_get_space(maff));
5642 if (!space)
5643 return isl_multi_aff_free(maff);
5644 isl_space_free(maff->space);
5645 maff->space = space;
5647 if (ls) {
5648 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5649 if (!*ls)
5650 return isl_multi_aff_free(maff);
5653 for (i = 0; i < maff->n; ++i) {
5654 maff->p[i] = isl_aff_lift(maff->p[i]);
5655 if (!maff->p[i])
5656 goto error;
5659 return maff;
5660 error:
5661 if (ls)
5662 isl_local_space_free(*ls);
5663 return isl_multi_aff_free(maff);
5667 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5669 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5670 __isl_keep isl_pw_multi_aff *pma, int pos)
5672 int i;
5673 int n_out;
5674 isl_space *space;
5675 isl_pw_aff *pa;
5677 if (!pma)
5678 return NULL;
5680 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5681 if (pos < 0 || pos >= n_out)
5682 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5683 "index out of bounds", return NULL);
5685 space = isl_pw_multi_aff_get_space(pma);
5686 space = isl_space_drop_dims(space, isl_dim_out,
5687 pos + 1, n_out - pos - 1);
5688 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5690 pa = isl_pw_aff_alloc_size(space, pma->n);
5691 for (i = 0; i < pma->n; ++i) {
5692 isl_aff *aff;
5693 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5694 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5697 return pa;
5700 /* Return an isl_pw_multi_aff with the given "set" as domain and
5701 * an unnamed zero-dimensional range.
5703 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5704 __isl_take isl_set *set)
5706 isl_multi_aff *ma;
5707 isl_space *space;
5709 space = isl_set_get_space(set);
5710 space = isl_space_from_domain(space);
5711 ma = isl_multi_aff_zero(space);
5712 return isl_pw_multi_aff_alloc(set, ma);
5715 /* Add an isl_pw_multi_aff with the given "set" as domain and
5716 * an unnamed zero-dimensional range to *user.
5718 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5719 void *user)
5721 isl_union_pw_multi_aff **upma = user;
5722 isl_pw_multi_aff *pma;
5724 pma = isl_pw_multi_aff_from_domain(set);
5725 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5727 return isl_stat_ok;
5730 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5731 * an unnamed zero-dimensional range.
5733 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5734 __isl_take isl_union_set *uset)
5736 isl_space *space;
5737 isl_union_pw_multi_aff *upma;
5739 if (!uset)
5740 return NULL;
5742 space = isl_union_set_get_space(uset);
5743 upma = isl_union_pw_multi_aff_empty(space);
5745 if (isl_union_set_foreach_set(uset,
5746 &add_pw_multi_aff_from_domain, &upma) < 0)
5747 goto error;
5749 isl_union_set_free(uset);
5750 return upma;
5751 error:
5752 isl_union_set_free(uset);
5753 isl_union_pw_multi_aff_free(upma);
5754 return NULL;
5757 /* Convert "pma" to an isl_map and add it to *umap.
5759 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5760 void *user)
5762 isl_union_map **umap = user;
5763 isl_map *map;
5765 map = isl_map_from_pw_multi_aff(pma);
5766 *umap = isl_union_map_add_map(*umap, map);
5768 return isl_stat_ok;
5771 /* Construct a union map mapping the domain of the union
5772 * piecewise multi-affine expression to its range, with each dimension
5773 * in the range equated to the corresponding affine expression on its cell.
5775 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5776 __isl_take isl_union_pw_multi_aff *upma)
5778 isl_space *space;
5779 isl_union_map *umap;
5781 if (!upma)
5782 return NULL;
5784 space = isl_union_pw_multi_aff_get_space(upma);
5785 umap = isl_union_map_empty(space);
5787 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5788 &map_from_pw_multi_aff, &umap) < 0)
5789 goto error;
5791 isl_union_pw_multi_aff_free(upma);
5792 return umap;
5793 error:
5794 isl_union_pw_multi_aff_free(upma);
5795 isl_union_map_free(umap);
5796 return NULL;
5799 /* Local data for bin_entry and the callback "fn".
5801 struct isl_union_pw_multi_aff_bin_data {
5802 isl_union_pw_multi_aff *upma2;
5803 isl_union_pw_multi_aff *res;
5804 isl_pw_multi_aff *pma;
5805 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5808 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5809 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5811 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5813 struct isl_union_pw_multi_aff_bin_data *data = user;
5814 isl_stat r;
5816 data->pma = pma;
5817 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5818 data->fn, data);
5819 isl_pw_multi_aff_free(pma);
5821 return r;
5824 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5825 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5826 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5827 * as *entry. The callback should adjust data->res if desired.
5829 static __isl_give isl_union_pw_multi_aff *bin_op(
5830 __isl_take isl_union_pw_multi_aff *upma1,
5831 __isl_take isl_union_pw_multi_aff *upma2,
5832 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5834 isl_space *space;
5835 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5837 space = isl_union_pw_multi_aff_get_space(upma2);
5838 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5839 space = isl_union_pw_multi_aff_get_space(upma1);
5840 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5842 if (!upma1 || !upma2)
5843 goto error;
5845 data.upma2 = upma2;
5846 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5847 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5848 &bin_entry, &data) < 0)
5849 goto error;
5851 isl_union_pw_multi_aff_free(upma1);
5852 isl_union_pw_multi_aff_free(upma2);
5853 return data.res;
5854 error:
5855 isl_union_pw_multi_aff_free(upma1);
5856 isl_union_pw_multi_aff_free(upma2);
5857 isl_union_pw_multi_aff_free(data.res);
5858 return NULL;
5861 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5862 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5864 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5865 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5867 isl_space *space;
5869 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5870 isl_pw_multi_aff_get_space(pma2));
5871 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5872 &isl_multi_aff_range_product);
5875 /* Given two isl_pw_multi_affs A -> B and C -> D,
5876 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5878 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5879 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5881 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5882 &pw_multi_aff_range_product);
5885 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5886 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5888 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5889 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5891 isl_space *space;
5893 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5894 isl_pw_multi_aff_get_space(pma2));
5895 space = isl_space_flatten_range(space);
5896 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5897 &isl_multi_aff_flat_range_product);
5900 /* Given two isl_pw_multi_affs A -> B and C -> D,
5901 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5903 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5904 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5906 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5907 &pw_multi_aff_flat_range_product);
5910 /* If data->pma and "pma2" have the same domain space, then compute
5911 * their flat range product and the result to data->res.
5913 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
5914 void *user)
5916 struct isl_union_pw_multi_aff_bin_data *data = user;
5918 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5919 pma2->dim, isl_dim_in)) {
5920 isl_pw_multi_aff_free(pma2);
5921 return isl_stat_ok;
5924 pma2 = isl_pw_multi_aff_flat_range_product(
5925 isl_pw_multi_aff_copy(data->pma), pma2);
5927 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5929 return isl_stat_ok;
5932 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5933 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5935 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5936 __isl_take isl_union_pw_multi_aff *upma1,
5937 __isl_take isl_union_pw_multi_aff *upma2)
5939 return bin_op(upma1, upma2, &flat_range_product_entry);
5942 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5943 * The parameters are assumed to have been aligned.
5945 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5946 * except that it works on two different isl_pw_* types.
5948 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5949 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5950 __isl_take isl_pw_aff *pa)
5952 int i, j, n;
5953 isl_pw_multi_aff *res = NULL;
5955 if (!pma || !pa)
5956 goto error;
5958 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5959 pa->dim, isl_dim_in))
5960 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5961 "domains don't match", goto error);
5962 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5963 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5964 "index out of bounds", goto error);
5966 n = pma->n * pa->n;
5967 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5969 for (i = 0; i < pma->n; ++i) {
5970 for (j = 0; j < pa->n; ++j) {
5971 isl_set *common;
5972 isl_multi_aff *res_ij;
5973 int empty;
5975 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5976 isl_set_copy(pa->p[j].set));
5977 empty = isl_set_plain_is_empty(common);
5978 if (empty < 0 || empty) {
5979 isl_set_free(common);
5980 if (empty < 0)
5981 goto error;
5982 continue;
5985 res_ij = isl_multi_aff_set_aff(
5986 isl_multi_aff_copy(pma->p[i].maff), pos,
5987 isl_aff_copy(pa->p[j].aff));
5988 res_ij = isl_multi_aff_gist(res_ij,
5989 isl_set_copy(common));
5991 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5995 isl_pw_multi_aff_free(pma);
5996 isl_pw_aff_free(pa);
5997 return res;
5998 error:
5999 isl_pw_multi_aff_free(pma);
6000 isl_pw_aff_free(pa);
6001 return isl_pw_multi_aff_free(res);
6004 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6006 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6007 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6008 __isl_take isl_pw_aff *pa)
6010 if (!pma || !pa)
6011 goto error;
6012 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
6013 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6014 if (!isl_space_has_named_params(pma->dim) ||
6015 !isl_space_has_named_params(pa->dim))
6016 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6017 "unaligned unnamed parameters", goto error);
6018 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6019 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6020 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6021 error:
6022 isl_pw_multi_aff_free(pma);
6023 isl_pw_aff_free(pa);
6024 return NULL;
6027 /* Do the parameters of "pa" match those of "space"?
6029 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6030 __isl_keep isl_space *space)
6032 isl_space *pa_space;
6033 int match;
6035 if (!pa || !space)
6036 return -1;
6038 pa_space = isl_pw_aff_get_space(pa);
6040 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6042 isl_space_free(pa_space);
6043 return match;
6046 /* Check that the domain space of "pa" matches "space".
6048 * Return 0 on success and -1 on error.
6050 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6051 __isl_keep isl_space *space)
6053 isl_space *pa_space;
6054 int match;
6056 if (!pa || !space)
6057 return -1;
6059 pa_space = isl_pw_aff_get_space(pa);
6061 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6062 if (match < 0)
6063 goto error;
6064 if (!match)
6065 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6066 "parameters don't match", goto error);
6067 match = isl_space_tuple_is_equal(space, isl_dim_in,
6068 pa_space, isl_dim_in);
6069 if (match < 0)
6070 goto error;
6071 if (!match)
6072 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6073 "domains don't match", goto error);
6074 isl_space_free(pa_space);
6075 return 0;
6076 error:
6077 isl_space_free(pa_space);
6078 return -1;
6081 #undef BASE
6082 #define BASE pw_aff
6083 #undef DOMBASE
6084 #define DOMBASE set
6086 #include <isl_multi_templ.c>
6087 #include <isl_multi_apply_set.c>
6088 #include <isl_multi_coalesce.c>
6089 #include <isl_multi_gist.c>
6090 #include <isl_multi_hash.c>
6091 #include <isl_multi_intersect.c>
6093 /* Scale the elements of "pma" by the corresponding elements of "mv".
6095 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6096 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6098 int i;
6100 pma = isl_pw_multi_aff_cow(pma);
6101 if (!pma || !mv)
6102 goto error;
6103 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6104 mv->space, isl_dim_set))
6105 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6106 "spaces don't match", goto error);
6107 if (!isl_space_match(pma->dim, isl_dim_param,
6108 mv->space, isl_dim_param)) {
6109 pma = isl_pw_multi_aff_align_params(pma,
6110 isl_multi_val_get_space(mv));
6111 mv = isl_multi_val_align_params(mv,
6112 isl_pw_multi_aff_get_space(pma));
6113 if (!pma || !mv)
6114 goto error;
6117 for (i = 0; i < pma->n; ++i) {
6118 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6119 isl_multi_val_copy(mv));
6120 if (!pma->p[i].maff)
6121 goto error;
6124 isl_multi_val_free(mv);
6125 return pma;
6126 error:
6127 isl_multi_val_free(mv);
6128 isl_pw_multi_aff_free(pma);
6129 return NULL;
6132 /* This function is called for each entry of an isl_union_pw_multi_aff.
6133 * If the space of the entry matches that of data->mv,
6134 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6135 * Otherwise, return an empty isl_pw_multi_aff.
6137 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6138 __isl_take isl_pw_multi_aff *pma, void *user)
6140 isl_multi_val *mv = user;
6142 if (!pma)
6143 return NULL;
6144 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6145 mv->space, isl_dim_set)) {
6146 isl_space *space = isl_pw_multi_aff_get_space(pma);
6147 isl_pw_multi_aff_free(pma);
6148 return isl_pw_multi_aff_empty(space);
6151 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6154 /* Scale the elements of "upma" by the corresponding elements of "mv",
6155 * for those entries that match the space of "mv".
6157 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6158 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6160 upma = isl_union_pw_multi_aff_align_params(upma,
6161 isl_multi_val_get_space(mv));
6162 mv = isl_multi_val_align_params(mv,
6163 isl_union_pw_multi_aff_get_space(upma));
6164 if (!upma || !mv)
6165 goto error;
6167 return isl_union_pw_multi_aff_transform(upma,
6168 &union_pw_multi_aff_scale_multi_val_entry, mv);
6170 isl_multi_val_free(mv);
6171 return upma;
6172 error:
6173 isl_multi_val_free(mv);
6174 isl_union_pw_multi_aff_free(upma);
6175 return NULL;
6178 /* Construct and return a piecewise multi affine expression
6179 * in the given space with value zero in each of the output dimensions and
6180 * a universe domain.
6182 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6184 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6187 /* Construct and return a piecewise multi affine expression
6188 * that is equal to the given piecewise affine expression.
6190 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6191 __isl_take isl_pw_aff *pa)
6193 int i;
6194 isl_space *space;
6195 isl_pw_multi_aff *pma;
6197 if (!pa)
6198 return NULL;
6200 space = isl_pw_aff_get_space(pa);
6201 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6203 for (i = 0; i < pa->n; ++i) {
6204 isl_set *set;
6205 isl_multi_aff *ma;
6207 set = isl_set_copy(pa->p[i].set);
6208 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6209 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6212 isl_pw_aff_free(pa);
6213 return pma;
6216 /* Construct a set or map mapping the shared (parameter) domain
6217 * of the piecewise affine expressions to the range of "mpa"
6218 * with each dimension in the range equated to the
6219 * corresponding piecewise affine expression.
6221 static __isl_give isl_map *map_from_multi_pw_aff(
6222 __isl_take isl_multi_pw_aff *mpa)
6224 int i;
6225 isl_space *space;
6226 isl_map *map;
6228 if (!mpa)
6229 return NULL;
6231 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6232 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6233 "invalid space", goto error);
6235 space = isl_multi_pw_aff_get_domain_space(mpa);
6236 map = isl_map_universe(isl_space_from_domain(space));
6238 for (i = 0; i < mpa->n; ++i) {
6239 isl_pw_aff *pa;
6240 isl_map *map_i;
6242 pa = isl_pw_aff_copy(mpa->p[i]);
6243 map_i = map_from_pw_aff(pa);
6245 map = isl_map_flat_range_product(map, map_i);
6248 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6250 isl_multi_pw_aff_free(mpa);
6251 return map;
6252 error:
6253 isl_multi_pw_aff_free(mpa);
6254 return NULL;
6257 /* Construct a map mapping the shared domain
6258 * of the piecewise affine expressions to the range of "mpa"
6259 * with each dimension in the range equated to the
6260 * corresponding piecewise affine expression.
6262 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6264 if (!mpa)
6265 return NULL;
6266 if (isl_space_is_set(mpa->space))
6267 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6268 "space of input is not a map", goto error);
6270 return map_from_multi_pw_aff(mpa);
6271 error:
6272 isl_multi_pw_aff_free(mpa);
6273 return NULL;
6276 /* Construct a set mapping the shared parameter domain
6277 * of the piecewise affine expressions to the space of "mpa"
6278 * with each dimension in the range equated to the
6279 * corresponding piecewise affine expression.
6281 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6283 if (!mpa)
6284 return NULL;
6285 if (!isl_space_is_set(mpa->space))
6286 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6287 "space of input is not a set", goto error);
6289 return map_from_multi_pw_aff(mpa);
6290 error:
6291 isl_multi_pw_aff_free(mpa);
6292 return NULL;
6295 /* Construct and return a piecewise multi affine expression
6296 * that is equal to the given multi piecewise affine expression
6297 * on the shared domain of the piecewise affine expressions.
6299 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6300 __isl_take isl_multi_pw_aff *mpa)
6302 int i;
6303 isl_space *space;
6304 isl_pw_aff *pa;
6305 isl_pw_multi_aff *pma;
6307 if (!mpa)
6308 return NULL;
6310 space = isl_multi_pw_aff_get_space(mpa);
6312 if (mpa->n == 0) {
6313 isl_multi_pw_aff_free(mpa);
6314 return isl_pw_multi_aff_zero(space);
6317 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6318 pma = isl_pw_multi_aff_from_pw_aff(pa);
6320 for (i = 1; i < mpa->n; ++i) {
6321 isl_pw_multi_aff *pma_i;
6323 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6324 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6325 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6328 pma = isl_pw_multi_aff_reset_space(pma, space);
6330 isl_multi_pw_aff_free(mpa);
6331 return pma;
6334 /* Construct and return a multi piecewise affine expression
6335 * that is equal to the given multi affine expression.
6337 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6338 __isl_take isl_multi_aff *ma)
6340 int i, n;
6341 isl_multi_pw_aff *mpa;
6343 if (!ma)
6344 return NULL;
6346 n = isl_multi_aff_dim(ma, isl_dim_out);
6347 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6349 for (i = 0; i < n; ++i) {
6350 isl_pw_aff *pa;
6352 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6353 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6356 isl_multi_aff_free(ma);
6357 return mpa;
6360 /* Construct and return a multi piecewise affine expression
6361 * that is equal to the given piecewise multi affine expression.
6363 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6364 __isl_take isl_pw_multi_aff *pma)
6366 int i, n;
6367 isl_space *space;
6368 isl_multi_pw_aff *mpa;
6370 if (!pma)
6371 return NULL;
6373 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6374 space = isl_pw_multi_aff_get_space(pma);
6375 mpa = isl_multi_pw_aff_alloc(space);
6377 for (i = 0; i < n; ++i) {
6378 isl_pw_aff *pa;
6380 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6381 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6384 isl_pw_multi_aff_free(pma);
6385 return mpa;
6388 /* Do "pa1" and "pa2" represent the same function?
6390 * We first check if they are obviously equal.
6391 * If not, we convert them to maps and check if those are equal.
6393 * If "pa1" or "pa2" contain any NaNs, then they are considered
6394 * not to be the same. A NaN is not equal to anything, not even
6395 * to another NaN.
6397 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6399 int equal;
6400 isl_bool has_nan;
6401 isl_map *map1, *map2;
6403 if (!pa1 || !pa2)
6404 return -1;
6406 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6407 if (equal < 0 || equal)
6408 return equal;
6409 has_nan = isl_pw_aff_involves_nan(pa1);
6410 if (has_nan >= 0 && !has_nan)
6411 has_nan = isl_pw_aff_involves_nan(pa2);
6412 if (has_nan < 0)
6413 return -1;
6414 if (has_nan)
6415 return 0;
6417 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6418 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6419 equal = isl_map_is_equal(map1, map2);
6420 isl_map_free(map1);
6421 isl_map_free(map2);
6423 return equal;
6426 /* Do "mpa1" and "mpa2" represent the same function?
6428 * Note that we cannot convert the entire isl_multi_pw_aff
6429 * to a map because the domains of the piecewise affine expressions
6430 * may not be the same.
6432 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6433 __isl_keep isl_multi_pw_aff *mpa2)
6435 int i;
6436 isl_bool equal;
6438 if (!mpa1 || !mpa2)
6439 return isl_bool_error;
6441 if (!isl_space_match(mpa1->space, isl_dim_param,
6442 mpa2->space, isl_dim_param)) {
6443 if (!isl_space_has_named_params(mpa1->space))
6444 return isl_bool_false;
6445 if (!isl_space_has_named_params(mpa2->space))
6446 return isl_bool_false;
6447 mpa1 = isl_multi_pw_aff_copy(mpa1);
6448 mpa2 = isl_multi_pw_aff_copy(mpa2);
6449 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6450 isl_multi_pw_aff_get_space(mpa2));
6451 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6452 isl_multi_pw_aff_get_space(mpa1));
6453 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6454 isl_multi_pw_aff_free(mpa1);
6455 isl_multi_pw_aff_free(mpa2);
6456 return equal;
6459 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6460 if (equal < 0 || !equal)
6461 return equal;
6463 for (i = 0; i < mpa1->n; ++i) {
6464 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6465 if (equal < 0 || !equal)
6466 return equal;
6469 return isl_bool_true;
6472 /* Compute the pullback of "mpa" by the function represented by "ma".
6473 * In other words, plug in "ma" in "mpa".
6475 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6477 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6478 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6480 int i;
6481 isl_space *space = NULL;
6483 mpa = isl_multi_pw_aff_cow(mpa);
6484 if (!mpa || !ma)
6485 goto error;
6487 space = isl_space_join(isl_multi_aff_get_space(ma),
6488 isl_multi_pw_aff_get_space(mpa));
6489 if (!space)
6490 goto error;
6492 for (i = 0; i < mpa->n; ++i) {
6493 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6494 isl_multi_aff_copy(ma));
6495 if (!mpa->p[i])
6496 goto error;
6499 isl_multi_aff_free(ma);
6500 isl_space_free(mpa->space);
6501 mpa->space = space;
6502 return mpa;
6503 error:
6504 isl_space_free(space);
6505 isl_multi_pw_aff_free(mpa);
6506 isl_multi_aff_free(ma);
6507 return NULL;
6510 /* Compute the pullback of "mpa" by the function represented by "ma".
6511 * In other words, plug in "ma" in "mpa".
6513 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6514 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6516 if (!mpa || !ma)
6517 goto error;
6518 if (isl_space_match(mpa->space, isl_dim_param,
6519 ma->space, isl_dim_param))
6520 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6521 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6522 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6523 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6524 error:
6525 isl_multi_pw_aff_free(mpa);
6526 isl_multi_aff_free(ma);
6527 return NULL;
6530 /* Compute the pullback of "mpa" by the function represented by "pma".
6531 * In other words, plug in "pma" in "mpa".
6533 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6535 static __isl_give isl_multi_pw_aff *
6536 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6537 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6539 int i;
6540 isl_space *space = NULL;
6542 mpa = isl_multi_pw_aff_cow(mpa);
6543 if (!mpa || !pma)
6544 goto error;
6546 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6547 isl_multi_pw_aff_get_space(mpa));
6549 for (i = 0; i < mpa->n; ++i) {
6550 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6551 isl_pw_multi_aff_copy(pma));
6552 if (!mpa->p[i])
6553 goto error;
6556 isl_pw_multi_aff_free(pma);
6557 isl_space_free(mpa->space);
6558 mpa->space = space;
6559 return mpa;
6560 error:
6561 isl_space_free(space);
6562 isl_multi_pw_aff_free(mpa);
6563 isl_pw_multi_aff_free(pma);
6564 return NULL;
6567 /* Compute the pullback of "mpa" by the function represented by "pma".
6568 * In other words, plug in "pma" in "mpa".
6570 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6571 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6573 if (!mpa || !pma)
6574 goto error;
6575 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6576 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6577 mpa = isl_multi_pw_aff_align_params(mpa,
6578 isl_pw_multi_aff_get_space(pma));
6579 pma = isl_pw_multi_aff_align_params(pma,
6580 isl_multi_pw_aff_get_space(mpa));
6581 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6582 error:
6583 isl_multi_pw_aff_free(mpa);
6584 isl_pw_multi_aff_free(pma);
6585 return NULL;
6588 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6589 * with the domain of "aff". The domain of the result is the same
6590 * as that of "mpa".
6591 * "mpa" and "aff" are assumed to have been aligned.
6593 * We first extract the parametric constant from "aff", defined
6594 * over the correct domain.
6595 * Then we add the appropriate combinations of the members of "mpa".
6596 * Finally, we add the integer divisions through recursive calls.
6598 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6599 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6601 int i, n_in, n_div;
6602 isl_space *space;
6603 isl_val *v;
6604 isl_pw_aff *pa;
6605 isl_aff *tmp;
6607 n_in = isl_aff_dim(aff, isl_dim_in);
6608 n_div = isl_aff_dim(aff, isl_dim_div);
6610 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6611 tmp = isl_aff_copy(aff);
6612 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6613 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6614 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6615 isl_space_dim(space, isl_dim_set));
6616 tmp = isl_aff_reset_domain_space(tmp, space);
6617 pa = isl_pw_aff_from_aff(tmp);
6619 for (i = 0; i < n_in; ++i) {
6620 isl_pw_aff *pa_i;
6622 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6623 continue;
6624 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6625 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6626 pa_i = isl_pw_aff_scale_val(pa_i, v);
6627 pa = isl_pw_aff_add(pa, pa_i);
6630 for (i = 0; i < n_div; ++i) {
6631 isl_aff *div;
6632 isl_pw_aff *pa_i;
6634 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6635 continue;
6636 div = isl_aff_get_div(aff, i);
6637 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6638 isl_multi_pw_aff_copy(mpa), div);
6639 pa_i = isl_pw_aff_floor(pa_i);
6640 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6641 pa_i = isl_pw_aff_scale_val(pa_i, v);
6642 pa = isl_pw_aff_add(pa, pa_i);
6645 isl_multi_pw_aff_free(mpa);
6646 isl_aff_free(aff);
6648 return pa;
6651 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6652 * with the domain of "aff". The domain of the result is the same
6653 * as that of "mpa".
6655 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6656 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6658 if (!aff || !mpa)
6659 goto error;
6660 if (isl_space_match(aff->ls->dim, isl_dim_param,
6661 mpa->space, isl_dim_param))
6662 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6664 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6665 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6667 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6668 error:
6669 isl_aff_free(aff);
6670 isl_multi_pw_aff_free(mpa);
6671 return NULL;
6674 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6675 * with the domain of "pa". The domain of the result is the same
6676 * as that of "mpa".
6677 * "mpa" and "pa" are assumed to have been aligned.
6679 * We consider each piece in turn. Note that the domains of the
6680 * pieces are assumed to be disjoint and they remain disjoint
6681 * after taking the preimage (over the same function).
6683 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6684 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6686 isl_space *space;
6687 isl_pw_aff *res;
6688 int i;
6690 if (!mpa || !pa)
6691 goto error;
6693 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6694 isl_pw_aff_get_space(pa));
6695 res = isl_pw_aff_empty(space);
6697 for (i = 0; i < pa->n; ++i) {
6698 isl_pw_aff *pa_i;
6699 isl_set *domain;
6701 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6702 isl_multi_pw_aff_copy(mpa),
6703 isl_aff_copy(pa->p[i].aff));
6704 domain = isl_set_copy(pa->p[i].set);
6705 domain = isl_set_preimage_multi_pw_aff(domain,
6706 isl_multi_pw_aff_copy(mpa));
6707 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6708 res = isl_pw_aff_add_disjoint(res, pa_i);
6711 isl_pw_aff_free(pa);
6712 isl_multi_pw_aff_free(mpa);
6713 return res;
6714 error:
6715 isl_pw_aff_free(pa);
6716 isl_multi_pw_aff_free(mpa);
6717 return NULL;
6720 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6721 * with the domain of "pa". The domain of the result is the same
6722 * as that of "mpa".
6724 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6725 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6727 if (!pa || !mpa)
6728 goto error;
6729 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6730 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6732 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6733 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6735 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6736 error:
6737 isl_pw_aff_free(pa);
6738 isl_multi_pw_aff_free(mpa);
6739 return NULL;
6742 /* Compute the pullback of "pa" by the function represented by "mpa".
6743 * In other words, plug in "mpa" in "pa".
6744 * "pa" and "mpa" are assumed to have been aligned.
6746 * The pullback is computed by applying "pa" to "mpa".
6748 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6749 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6751 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6754 /* Compute the pullback of "pa" by the function represented by "mpa".
6755 * In other words, plug in "mpa" in "pa".
6757 * The pullback is computed by applying "pa" to "mpa".
6759 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6760 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6762 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6765 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6766 * In other words, plug in "mpa2" in "mpa1".
6768 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6770 * We pullback each member of "mpa1" in turn.
6772 static __isl_give isl_multi_pw_aff *
6773 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6774 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6776 int i;
6777 isl_space *space = NULL;
6779 mpa1 = isl_multi_pw_aff_cow(mpa1);
6780 if (!mpa1 || !mpa2)
6781 goto error;
6783 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6784 isl_multi_pw_aff_get_space(mpa1));
6786 for (i = 0; i < mpa1->n; ++i) {
6787 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6788 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6789 if (!mpa1->p[i])
6790 goto error;
6793 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6795 isl_multi_pw_aff_free(mpa2);
6796 return mpa1;
6797 error:
6798 isl_space_free(space);
6799 isl_multi_pw_aff_free(mpa1);
6800 isl_multi_pw_aff_free(mpa2);
6801 return NULL;
6804 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6805 * In other words, plug in "mpa2" in "mpa1".
6807 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6808 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6810 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6811 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6814 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6815 * of "mpa1" and "mpa2" live in the same space, construct map space
6816 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6817 * with this map space as extract argument.
6819 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6820 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6821 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6822 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6824 int match;
6825 isl_space *space1, *space2;
6826 isl_map *res;
6828 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6829 isl_multi_pw_aff_get_space(mpa2));
6830 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6831 isl_multi_pw_aff_get_space(mpa1));
6832 if (!mpa1 || !mpa2)
6833 goto error;
6834 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6835 mpa2->space, isl_dim_out);
6836 if (match < 0)
6837 goto error;
6838 if (!match)
6839 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6840 "range spaces don't match", goto error);
6841 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6842 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6843 space1 = isl_space_map_from_domain_and_range(space1, space2);
6845 res = order(mpa1, mpa2, space1);
6846 isl_multi_pw_aff_free(mpa1);
6847 isl_multi_pw_aff_free(mpa2);
6848 return res;
6849 error:
6850 isl_multi_pw_aff_free(mpa1);
6851 isl_multi_pw_aff_free(mpa2);
6852 return NULL;
6855 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6856 * where the function values are equal. "space" is the space of the result.
6857 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6859 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6860 * in the sequences are equal.
6862 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6863 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6864 __isl_take isl_space *space)
6866 int i, n;
6867 isl_map *res;
6869 res = isl_map_universe(space);
6871 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6872 for (i = 0; i < n; ++i) {
6873 isl_pw_aff *pa1, *pa2;
6874 isl_map *map;
6876 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6877 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6878 map = isl_pw_aff_eq_map(pa1, pa2);
6879 res = isl_map_intersect(res, map);
6882 return res;
6885 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6886 * where the function values are equal.
6888 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6889 __isl_take isl_multi_pw_aff *mpa2)
6891 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6892 &isl_multi_pw_aff_eq_map_on_space);
6895 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6896 * where the function values of "mpa1" is lexicographically satisfies "base"
6897 * compared to that of "mpa2". "space" is the space of the result.
6898 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6900 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6901 * if its i-th element satisfies "base" when compared to
6902 * the i-th element of "mpa2" while all previous elements are
6903 * pairwise equal.
6905 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6906 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6907 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6908 __isl_take isl_pw_aff *pa2),
6909 __isl_take isl_space *space)
6911 int i, n;
6912 isl_map *res, *rest;
6914 res = isl_map_empty(isl_space_copy(space));
6915 rest = isl_map_universe(space);
6917 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6918 for (i = 0; i < n; ++i) {
6919 isl_pw_aff *pa1, *pa2;
6920 isl_map *map;
6922 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6923 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6924 map = base(pa1, pa2);
6925 map = isl_map_intersect(map, isl_map_copy(rest));
6926 res = isl_map_union(res, map);
6928 if (i == n - 1)
6929 continue;
6931 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6932 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6933 map = isl_pw_aff_eq_map(pa1, pa2);
6934 rest = isl_map_intersect(rest, map);
6937 isl_map_free(rest);
6938 return res;
6941 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6942 * where the function value of "mpa1" is lexicographically less than that
6943 * of "mpa2". "space" is the space of the result.
6944 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6946 * "mpa1" is less than "mpa2" if its i-th element is smaller
6947 * than the i-th element of "mpa2" while all previous elements are
6948 * pairwise equal.
6950 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
6951 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6952 __isl_take isl_space *space)
6954 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6955 &isl_pw_aff_lt_map, space);
6958 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6959 * where the function value of "mpa1" is lexicographically less than that
6960 * of "mpa2".
6962 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
6963 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6965 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6966 &isl_multi_pw_aff_lex_lt_map_on_space);
6969 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6970 * where the function value of "mpa1" is lexicographically greater than that
6971 * of "mpa2". "space" is the space of the result.
6972 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6974 * "mpa1" is greater than "mpa2" if its i-th element is greater
6975 * than the i-th element of "mpa2" while all previous elements are
6976 * pairwise equal.
6978 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
6979 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6980 __isl_take isl_space *space)
6982 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6983 &isl_pw_aff_gt_map, space);
6986 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6987 * where the function value of "mpa1" is lexicographically greater than that
6988 * of "mpa2".
6990 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
6991 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6993 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6994 &isl_multi_pw_aff_lex_gt_map_on_space);
6997 /* Compare two isl_affs.
6999 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7000 * than "aff2" and 0 if they are equal.
7002 * The order is fairly arbitrary. We do consider expressions that only involve
7003 * earlier dimensions as "smaller".
7005 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7007 int cmp;
7008 int last1, last2;
7010 if (aff1 == aff2)
7011 return 0;
7013 if (!aff1)
7014 return -1;
7015 if (!aff2)
7016 return 1;
7018 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7019 if (cmp != 0)
7020 return cmp;
7022 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7023 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7024 if (last1 != last2)
7025 return last1 - last2;
7027 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7030 /* Compare two isl_pw_affs.
7032 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7033 * than "pa2" and 0 if they are equal.
7035 * The order is fairly arbitrary. We do consider expressions that only involve
7036 * earlier dimensions as "smaller".
7038 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7039 __isl_keep isl_pw_aff *pa2)
7041 int i;
7042 int cmp;
7044 if (pa1 == pa2)
7045 return 0;
7047 if (!pa1)
7048 return -1;
7049 if (!pa2)
7050 return 1;
7052 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7053 if (cmp != 0)
7054 return cmp;
7056 if (pa1->n != pa2->n)
7057 return pa1->n - pa2->n;
7059 for (i = 0; i < pa1->n; ++i) {
7060 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7061 if (cmp != 0)
7062 return cmp;
7063 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7064 if (cmp != 0)
7065 return cmp;
7068 return 0;
7071 /* Return a piecewise affine expression that is equal to "v" on "domain".
7073 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7074 __isl_take isl_val *v)
7076 isl_space *space;
7077 isl_local_space *ls;
7078 isl_aff *aff;
7080 space = isl_set_get_space(domain);
7081 ls = isl_local_space_from_space(space);
7082 aff = isl_aff_val_on_domain(ls, v);
7084 return isl_pw_aff_alloc(domain, aff);
7087 /* Return a multi affine expression that is equal to "mv" on domain
7088 * space "space".
7090 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7091 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7093 int i, n;
7094 isl_space *space2;
7095 isl_local_space *ls;
7096 isl_multi_aff *ma;
7098 if (!space || !mv)
7099 goto error;
7101 n = isl_multi_val_dim(mv, isl_dim_set);
7102 space2 = isl_multi_val_get_space(mv);
7103 space2 = isl_space_align_params(space2, isl_space_copy(space));
7104 space = isl_space_align_params(space, isl_space_copy(space2));
7105 space = isl_space_map_from_domain_and_range(space, space2);
7106 ma = isl_multi_aff_alloc(isl_space_copy(space));
7107 ls = isl_local_space_from_space(isl_space_domain(space));
7108 for (i = 0; i < n; ++i) {
7109 isl_val *v;
7110 isl_aff *aff;
7112 v = isl_multi_val_get_val(mv, i);
7113 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7114 ma = isl_multi_aff_set_aff(ma, i, aff);
7116 isl_local_space_free(ls);
7118 isl_multi_val_free(mv);
7119 return ma;
7120 error:
7121 isl_space_free(space);
7122 isl_multi_val_free(mv);
7123 return NULL;
7126 /* Return a piecewise multi-affine expression
7127 * that is equal to "mv" on "domain".
7129 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7130 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7132 isl_space *space;
7133 isl_multi_aff *ma;
7135 space = isl_set_get_space(domain);
7136 ma = isl_multi_aff_multi_val_on_space(space, mv);
7138 return isl_pw_multi_aff_alloc(domain, ma);
7141 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7142 * mv is the value that should be attained on each domain set
7143 * res collects the results
7145 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7146 isl_multi_val *mv;
7147 isl_union_pw_multi_aff *res;
7150 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7151 * and add it to data->res.
7153 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7154 void *user)
7156 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7157 isl_pw_multi_aff *pma;
7158 isl_multi_val *mv;
7160 mv = isl_multi_val_copy(data->mv);
7161 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7162 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7164 return data->res ? isl_stat_ok : isl_stat_error;
7167 /* Return a union piecewise multi-affine expression
7168 * that is equal to "mv" on "domain".
7170 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7171 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7173 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7174 isl_space *space;
7176 space = isl_union_set_get_space(domain);
7177 data.res = isl_union_pw_multi_aff_empty(space);
7178 data.mv = mv;
7179 if (isl_union_set_foreach_set(domain,
7180 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7181 data.res = isl_union_pw_multi_aff_free(data.res);
7182 isl_union_set_free(domain);
7183 isl_multi_val_free(mv);
7184 return data.res;
7187 /* Compute the pullback of data->pma by the function represented by "pma2",
7188 * provided the spaces match, and add the results to data->res.
7190 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7192 struct isl_union_pw_multi_aff_bin_data *data = user;
7194 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7195 pma2->dim, isl_dim_out)) {
7196 isl_pw_multi_aff_free(pma2);
7197 return isl_stat_ok;
7200 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7201 isl_pw_multi_aff_copy(data->pma), pma2);
7203 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7204 if (!data->res)
7205 return isl_stat_error;
7207 return isl_stat_ok;
7210 /* Compute the pullback of "upma1" by the function represented by "upma2".
7212 __isl_give isl_union_pw_multi_aff *
7213 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7214 __isl_take isl_union_pw_multi_aff *upma1,
7215 __isl_take isl_union_pw_multi_aff *upma2)
7217 return bin_op(upma1, upma2, &pullback_entry);
7220 /* Check that the domain space of "upa" matches "space".
7222 * Return 0 on success and -1 on error.
7224 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7225 * can in principle never fail since the space "space" is that
7226 * of the isl_multi_union_pw_aff and is a set space such that
7227 * there is no domain space to match.
7229 * We check the parameters and double-check that "space" is
7230 * indeed that of a set.
7232 static int isl_union_pw_aff_check_match_domain_space(
7233 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7235 isl_space *upa_space;
7236 int match;
7238 if (!upa || !space)
7239 return -1;
7241 match = isl_space_is_set(space);
7242 if (match < 0)
7243 return -1;
7244 if (!match)
7245 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7246 "expecting set space", return -1);
7248 upa_space = isl_union_pw_aff_get_space(upa);
7249 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7250 if (match < 0)
7251 goto error;
7252 if (!match)
7253 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7254 "parameters don't match", goto error);
7256 isl_space_free(upa_space);
7257 return 0;
7258 error:
7259 isl_space_free(upa_space);
7260 return -1;
7263 /* Do the parameters of "upa" match those of "space"?
7265 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7266 __isl_keep isl_space *space)
7268 isl_space *upa_space;
7269 int match;
7271 if (!upa || !space)
7272 return -1;
7274 upa_space = isl_union_pw_aff_get_space(upa);
7276 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7278 isl_space_free(upa_space);
7279 return match;
7282 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7283 * space represents the new parameters.
7284 * res collects the results.
7286 struct isl_union_pw_aff_reset_params_data {
7287 isl_space *space;
7288 isl_union_pw_aff *res;
7291 /* Replace the parameters of "pa" by data->space and
7292 * add the result to data->res.
7294 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7296 struct isl_union_pw_aff_reset_params_data *data = user;
7297 isl_space *space;
7299 space = isl_pw_aff_get_space(pa);
7300 space = isl_space_replace(space, isl_dim_param, data->space);
7301 pa = isl_pw_aff_reset_space(pa, space);
7302 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7304 return data->res ? isl_stat_ok : isl_stat_error;
7307 /* Replace the domain space of "upa" by "space".
7308 * Since a union expression does not have a (single) domain space,
7309 * "space" is necessarily a parameter space.
7311 * Since the order and the names of the parameters determine
7312 * the hash value, we need to create a new hash table.
7314 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7315 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7317 struct isl_union_pw_aff_reset_params_data data = { space };
7318 int match;
7320 match = isl_union_pw_aff_matching_params(upa, space);
7321 if (match < 0)
7322 upa = isl_union_pw_aff_free(upa);
7323 else if (match) {
7324 isl_space_free(space);
7325 return upa;
7328 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7329 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7330 data.res = isl_union_pw_aff_free(data.res);
7332 isl_union_pw_aff_free(upa);
7333 isl_space_free(space);
7334 return data.res;
7337 /* Return the floor of "pa".
7339 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7341 return isl_pw_aff_floor(pa);
7344 /* Given f, return floor(f).
7346 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7347 __isl_take isl_union_pw_aff *upa)
7349 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7352 /* Compute
7354 * upa mod m = upa - m * floor(upa/m)
7356 * with m an integer value.
7358 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7359 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7361 isl_union_pw_aff *res;
7363 if (!upa || !m)
7364 goto error;
7366 if (!isl_val_is_int(m))
7367 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7368 "expecting integer modulo", goto error);
7369 if (!isl_val_is_pos(m))
7370 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7371 "expecting positive modulo", goto error);
7373 res = isl_union_pw_aff_copy(upa);
7374 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7375 upa = isl_union_pw_aff_floor(upa);
7376 upa = isl_union_pw_aff_scale_val(upa, m);
7377 res = isl_union_pw_aff_sub(res, upa);
7379 return res;
7380 error:
7381 isl_val_free(m);
7382 isl_union_pw_aff_free(upa);
7383 return NULL;
7386 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7387 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7388 * needs to attain.
7389 * "res" collects the results.
7391 struct isl_union_pw_aff_aff_on_domain_data {
7392 isl_aff *aff;
7393 isl_union_pw_aff *res;
7396 /* Construct a piecewise affine expression that is equal to data->aff
7397 * on "domain" and add the result to data->res.
7399 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7401 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7402 isl_pw_aff *pa;
7403 isl_aff *aff;
7404 int dim;
7406 aff = isl_aff_copy(data->aff);
7407 dim = isl_set_dim(domain, isl_dim_set);
7408 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7409 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7410 pa = isl_pw_aff_alloc(domain, aff);
7411 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7413 return data->res ? isl_stat_ok : isl_stat_error;
7416 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7417 * pos is the output position that needs to be extracted.
7418 * res collects the results.
7420 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7421 int pos;
7422 isl_union_pw_aff *res;
7425 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7426 * (assuming it has such a dimension) and add it to data->res.
7428 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7430 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7431 int n_out;
7432 isl_pw_aff *pa;
7434 if (!pma)
7435 return isl_stat_error;
7437 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7438 if (data->pos >= n_out) {
7439 isl_pw_multi_aff_free(pma);
7440 return isl_stat_ok;
7443 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7444 isl_pw_multi_aff_free(pma);
7446 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7448 return data->res ? isl_stat_ok : isl_stat_error;
7451 /* Extract an isl_union_pw_aff corresponding to
7452 * output dimension "pos" of "upma".
7454 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7455 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7457 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7458 isl_space *space;
7460 if (!upma)
7461 return NULL;
7463 if (pos < 0)
7464 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7465 "cannot extract at negative position", return NULL);
7467 space = isl_union_pw_multi_aff_get_space(upma);
7468 data.res = isl_union_pw_aff_empty(space);
7469 data.pos = pos;
7470 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7471 &get_union_pw_aff, &data) < 0)
7472 data.res = isl_union_pw_aff_free(data.res);
7474 return data.res;
7477 /* Return a union piecewise affine expression
7478 * that is equal to "aff" on "domain".
7480 * Construct an isl_pw_aff on each of the sets in "domain" and
7481 * collect the results.
7483 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7484 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7486 struct isl_union_pw_aff_aff_on_domain_data data;
7487 isl_space *space;
7489 if (!domain || !aff)
7490 goto error;
7491 if (!isl_local_space_is_params(aff->ls))
7492 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7493 "expecting parametric expression", goto error);
7495 space = isl_union_set_get_space(domain);
7496 data.res = isl_union_pw_aff_empty(space);
7497 data.aff = aff;
7498 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7499 data.res = isl_union_pw_aff_free(data.res);
7500 isl_union_set_free(domain);
7501 isl_aff_free(aff);
7502 return data.res;
7503 error:
7504 isl_union_set_free(domain);
7505 isl_aff_free(aff);
7506 return NULL;
7509 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7510 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7511 * "res" collects the results.
7513 struct isl_union_pw_aff_val_on_domain_data {
7514 isl_val *v;
7515 isl_union_pw_aff *res;
7518 /* Construct a piecewise affine expression that is equal to data->v
7519 * on "domain" and add the result to data->res.
7521 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7523 struct isl_union_pw_aff_val_on_domain_data *data = user;
7524 isl_pw_aff *pa;
7525 isl_val *v;
7527 v = isl_val_copy(data->v);
7528 pa = isl_pw_aff_val_on_domain(domain, v);
7529 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7531 return data->res ? isl_stat_ok : isl_stat_error;
7534 /* Return a union piecewise affine expression
7535 * that is equal to "v" on "domain".
7537 * Construct an isl_pw_aff on each of the sets in "domain" and
7538 * collect the results.
7540 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7541 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7543 struct isl_union_pw_aff_val_on_domain_data data;
7544 isl_space *space;
7546 space = isl_union_set_get_space(domain);
7547 data.res = isl_union_pw_aff_empty(space);
7548 data.v = v;
7549 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7550 data.res = isl_union_pw_aff_free(data.res);
7551 isl_union_set_free(domain);
7552 isl_val_free(v);
7553 return data.res;
7556 /* Construct a piecewise multi affine expression
7557 * that is equal to "pa" and add it to upma.
7559 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7560 void *user)
7562 isl_union_pw_multi_aff **upma = user;
7563 isl_pw_multi_aff *pma;
7565 pma = isl_pw_multi_aff_from_pw_aff(pa);
7566 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7568 return *upma ? isl_stat_ok : isl_stat_error;
7571 /* Construct and return a union piecewise multi affine expression
7572 * that is equal to the given union piecewise affine expression.
7574 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7575 __isl_take isl_union_pw_aff *upa)
7577 isl_space *space;
7578 isl_union_pw_multi_aff *upma;
7580 if (!upa)
7581 return NULL;
7583 space = isl_union_pw_aff_get_space(upa);
7584 upma = isl_union_pw_multi_aff_empty(space);
7586 if (isl_union_pw_aff_foreach_pw_aff(upa,
7587 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7588 upma = isl_union_pw_multi_aff_free(upma);
7590 isl_union_pw_aff_free(upa);
7591 return upma;
7594 /* Compute the set of elements in the domain of "pa" where it is zero and
7595 * add this set to "uset".
7597 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7599 isl_union_set **uset = (isl_union_set **)user;
7601 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7603 return *uset ? isl_stat_ok : isl_stat_error;
7606 /* Return a union set containing those elements in the domain
7607 * of "upa" where it is zero.
7609 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7610 __isl_take isl_union_pw_aff *upa)
7612 isl_union_set *zero;
7614 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7615 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7616 zero = isl_union_set_free(zero);
7618 isl_union_pw_aff_free(upa);
7619 return zero;
7622 /* Convert "pa" to an isl_map and add it to *umap.
7624 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7626 isl_union_map **umap = user;
7627 isl_map *map;
7629 map = isl_map_from_pw_aff(pa);
7630 *umap = isl_union_map_add_map(*umap, map);
7632 return *umap ? isl_stat_ok : isl_stat_error;
7635 /* Construct a union map mapping the domain of the union
7636 * piecewise affine expression to its range, with the single output dimension
7637 * equated to the corresponding affine expressions on their cells.
7639 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7640 __isl_take isl_union_pw_aff *upa)
7642 isl_space *space;
7643 isl_union_map *umap;
7645 if (!upa)
7646 return NULL;
7648 space = isl_union_pw_aff_get_space(upa);
7649 umap = isl_union_map_empty(space);
7651 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7652 &umap) < 0)
7653 umap = isl_union_map_free(umap);
7655 isl_union_pw_aff_free(upa);
7656 return umap;
7659 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7660 * upma is the function that is plugged in.
7661 * pa is the current part of the function in which upma is plugged in.
7662 * res collects the results.
7664 struct isl_union_pw_aff_pullback_upma_data {
7665 isl_union_pw_multi_aff *upma;
7666 isl_pw_aff *pa;
7667 isl_union_pw_aff *res;
7670 /* Check if "pma" can be plugged into data->pa.
7671 * If so, perform the pullback and add the result to data->res.
7673 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7675 struct isl_union_pw_aff_pullback_upma_data *data = user;
7676 isl_pw_aff *pa;
7678 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7679 pma->dim, isl_dim_out)) {
7680 isl_pw_multi_aff_free(pma);
7681 return isl_stat_ok;
7684 pa = isl_pw_aff_copy(data->pa);
7685 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7687 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7689 return data->res ? isl_stat_ok : isl_stat_error;
7692 /* Check if any of the elements of data->upma can be plugged into pa,
7693 * add if so add the result to data->res.
7695 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7697 struct isl_union_pw_aff_pullback_upma_data *data = user;
7698 isl_stat r;
7700 data->pa = pa;
7701 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7702 &pa_pb_pma, data);
7703 isl_pw_aff_free(pa);
7705 return r;
7708 /* Compute the pullback of "upa" by the function represented by "upma".
7709 * In other words, plug in "upma" in "upa". The result contains
7710 * expressions defined over the domain space of "upma".
7712 * Run over all pairs of elements in "upa" and "upma", perform
7713 * the pullback when appropriate and collect the results.
7714 * If the hash value were based on the domain space rather than
7715 * the function space, then we could run through all elements
7716 * of "upma" and directly pick out the corresponding element of "upa".
7718 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7719 __isl_take isl_union_pw_aff *upa,
7720 __isl_take isl_union_pw_multi_aff *upma)
7722 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7723 isl_space *space;
7725 space = isl_union_pw_multi_aff_get_space(upma);
7726 upa = isl_union_pw_aff_align_params(upa, space);
7727 space = isl_union_pw_aff_get_space(upa);
7728 upma = isl_union_pw_multi_aff_align_params(upma, space);
7730 if (!upa || !upma)
7731 goto error;
7733 data.upma = upma;
7734 data.res = isl_union_pw_aff_alloc_same_size(upa);
7735 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7736 data.res = isl_union_pw_aff_free(data.res);
7738 isl_union_pw_aff_free(upa);
7739 isl_union_pw_multi_aff_free(upma);
7740 return data.res;
7741 error:
7742 isl_union_pw_aff_free(upa);
7743 isl_union_pw_multi_aff_free(upma);
7744 return NULL;
7747 #undef BASE
7748 #define BASE union_pw_aff
7749 #undef DOMBASE
7750 #define DOMBASE union_set
7752 #define NO_MOVE_DIMS
7753 #define NO_DIMS
7754 #define NO_DOMAIN
7755 #define NO_PRODUCT
7756 #define NO_SPLICE
7757 #define NO_ZERO
7758 #define NO_IDENTITY
7759 #define NO_GIST
7761 #include <isl_multi_templ.c>
7762 #include <isl_multi_apply_set.c>
7763 #include <isl_multi_apply_union_set.c>
7764 #include <isl_multi_coalesce.c>
7765 #include <isl_multi_floor.c>
7766 #include <isl_multi_gist.c>
7767 #include <isl_multi_intersect.c>
7769 /* Construct a multiple union piecewise affine expression
7770 * in the given space with value zero in each of the output dimensions.
7772 * Since there is no canonical zero value for
7773 * a union piecewise affine expression, we can only construct
7774 * zero-dimensional "zero" value.
7776 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7777 __isl_take isl_space *space)
7779 if (!space)
7780 return NULL;
7782 if (!isl_space_is_set(space))
7783 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7784 "expecting set space", goto error);
7785 if (isl_space_dim(space , isl_dim_out) != 0)
7786 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7787 "expecting 0D space", goto error);
7789 return isl_multi_union_pw_aff_alloc(space);
7790 error:
7791 isl_space_free(space);
7792 return NULL;
7795 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7796 * with the actual sum on the shared domain and
7797 * the defined expression on the symmetric difference of the domains.
7799 * We simply iterate over the elements in both arguments and
7800 * call isl_union_pw_aff_union_add on each of them.
7802 static __isl_give isl_multi_union_pw_aff *
7803 isl_multi_union_pw_aff_union_add_aligned(
7804 __isl_take isl_multi_union_pw_aff *mupa1,
7805 __isl_take isl_multi_union_pw_aff *mupa2)
7807 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7808 &isl_union_pw_aff_union_add);
7811 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7812 * with the actual sum on the shared domain and
7813 * the defined expression on the symmetric difference of the domains.
7815 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7816 __isl_take isl_multi_union_pw_aff *mupa1,
7817 __isl_take isl_multi_union_pw_aff *mupa2)
7819 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7820 &isl_multi_union_pw_aff_union_add_aligned);
7823 /* Construct and return a multi union piecewise affine expression
7824 * that is equal to the given multi affine expression.
7826 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7827 __isl_take isl_multi_aff *ma)
7829 isl_multi_pw_aff *mpa;
7831 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7832 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7835 /* Construct and return a multi union piecewise affine expression
7836 * that is equal to the given multi piecewise affine expression.
7838 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7839 __isl_take isl_multi_pw_aff *mpa)
7841 int i, n;
7842 isl_space *space;
7843 isl_multi_union_pw_aff *mupa;
7845 if (!mpa)
7846 return NULL;
7848 space = isl_multi_pw_aff_get_space(mpa);
7849 space = isl_space_range(space);
7850 mupa = isl_multi_union_pw_aff_alloc(space);
7852 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7853 for (i = 0; i < n; ++i) {
7854 isl_pw_aff *pa;
7855 isl_union_pw_aff *upa;
7857 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7858 upa = isl_union_pw_aff_from_pw_aff(pa);
7859 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7862 isl_multi_pw_aff_free(mpa);
7864 return mupa;
7867 /* Extract the range space of "pma" and assign it to *space.
7868 * If *space has already been set (through a previous call to this function),
7869 * then check that the range space is the same.
7871 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7873 isl_space **space = user;
7874 isl_space *pma_space;
7875 isl_bool equal;
7877 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7878 isl_pw_multi_aff_free(pma);
7880 if (!pma_space)
7881 return isl_stat_error;
7882 if (!*space) {
7883 *space = pma_space;
7884 return isl_stat_ok;
7887 equal = isl_space_is_equal(pma_space, *space);
7888 isl_space_free(pma_space);
7890 if (equal < 0)
7891 return isl_stat_error;
7892 if (!equal)
7893 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7894 "range spaces not the same", return isl_stat_error);
7895 return isl_stat_ok;
7898 /* Construct and return a multi union piecewise affine expression
7899 * that is equal to the given union piecewise multi affine expression.
7901 * In order to be able to perform the conversion, the input
7902 * needs to be non-empty and may only involve a single range space.
7904 __isl_give isl_multi_union_pw_aff *
7905 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7906 __isl_take isl_union_pw_multi_aff *upma)
7908 isl_space *space = NULL;
7909 isl_multi_union_pw_aff *mupa;
7910 int i, n;
7912 if (!upma)
7913 return NULL;
7914 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
7915 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7916 "cannot extract range space from empty input",
7917 goto error);
7918 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
7919 &space) < 0)
7920 goto error;
7922 if (!space)
7923 goto error;
7925 n = isl_space_dim(space, isl_dim_set);
7926 mupa = isl_multi_union_pw_aff_alloc(space);
7928 for (i = 0; i < n; ++i) {
7929 isl_union_pw_aff *upa;
7931 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
7932 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7935 isl_union_pw_multi_aff_free(upma);
7936 return mupa;
7937 error:
7938 isl_space_free(space);
7939 isl_union_pw_multi_aff_free(upma);
7940 return NULL;
7943 /* Try and create an isl_multi_union_pw_aff that is equivalent
7944 * to the given isl_union_map.
7945 * The isl_union_map is required to be single-valued in each space.
7946 * Moreover, it cannot be empty and all range spaces need to be the same.
7947 * Otherwise, an error is produced.
7949 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
7950 __isl_take isl_union_map *umap)
7952 isl_union_pw_multi_aff *upma;
7954 upma = isl_union_pw_multi_aff_from_union_map(umap);
7955 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
7958 /* Return a multiple union piecewise affine expression
7959 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7960 * have been aligned.
7962 static __isl_give isl_multi_union_pw_aff *
7963 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7964 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7966 int i, n;
7967 isl_space *space;
7968 isl_multi_union_pw_aff *mupa;
7970 if (!domain || !mv)
7971 goto error;
7973 n = isl_multi_val_dim(mv, isl_dim_set);
7974 space = isl_multi_val_get_space(mv);
7975 mupa = isl_multi_union_pw_aff_alloc(space);
7976 for (i = 0; i < n; ++i) {
7977 isl_val *v;
7978 isl_union_pw_aff *upa;
7980 v = isl_multi_val_get_val(mv, i);
7981 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
7983 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7986 isl_union_set_free(domain);
7987 isl_multi_val_free(mv);
7988 return mupa;
7989 error:
7990 isl_union_set_free(domain);
7991 isl_multi_val_free(mv);
7992 return NULL;
7995 /* Return a multiple union piecewise affine expression
7996 * that is equal to "mv" on "domain".
7998 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
7999 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8001 if (!domain || !mv)
8002 goto error;
8003 if (isl_space_match(domain->dim, isl_dim_param,
8004 mv->space, isl_dim_param))
8005 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8006 domain, mv);
8007 domain = isl_union_set_align_params(domain,
8008 isl_multi_val_get_space(mv));
8009 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8010 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8011 error:
8012 isl_union_set_free(domain);
8013 isl_multi_val_free(mv);
8014 return NULL;
8017 /* Return a multiple union piecewise affine expression
8018 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8019 * have been aligned.
8021 static __isl_give isl_multi_union_pw_aff *
8022 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8023 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8025 int i, n;
8026 isl_space *space;
8027 isl_multi_union_pw_aff *mupa;
8029 if (!domain || !ma)
8030 goto error;
8032 n = isl_multi_aff_dim(ma, isl_dim_set);
8033 space = isl_multi_aff_get_space(ma);
8034 mupa = isl_multi_union_pw_aff_alloc(space);
8035 for (i = 0; i < n; ++i) {
8036 isl_aff *aff;
8037 isl_union_pw_aff *upa;
8039 aff = isl_multi_aff_get_aff(ma, i);
8040 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8041 aff);
8042 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8045 isl_union_set_free(domain);
8046 isl_multi_aff_free(ma);
8047 return mupa;
8048 error:
8049 isl_union_set_free(domain);
8050 isl_multi_aff_free(ma);
8051 return NULL;
8054 /* Return a multiple union piecewise affine expression
8055 * that is equal to "ma" on "domain".
8057 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8058 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8060 if (!domain || !ma)
8061 goto error;
8062 if (isl_space_match(domain->dim, isl_dim_param,
8063 ma->space, isl_dim_param))
8064 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8065 domain, ma);
8066 domain = isl_union_set_align_params(domain,
8067 isl_multi_aff_get_space(ma));
8068 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8069 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8070 error:
8071 isl_union_set_free(domain);
8072 isl_multi_aff_free(ma);
8073 return NULL;
8076 /* Return a union set containing those elements in the domains
8077 * of the elements of "mupa" where they are all zero.
8079 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8080 __isl_take isl_multi_union_pw_aff *mupa)
8082 int i, n;
8083 isl_union_pw_aff *upa;
8084 isl_union_set *zero;
8086 if (!mupa)
8087 return NULL;
8089 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8090 if (n == 0)
8091 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8092 "cannot determine zero set "
8093 "of zero-dimensional function", goto error);
8095 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8096 zero = isl_union_pw_aff_zero_union_set(upa);
8098 for (i = 1; i < n; ++i) {
8099 isl_union_set *zero_i;
8101 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8102 zero_i = isl_union_pw_aff_zero_union_set(upa);
8104 zero = isl_union_set_intersect(zero, zero_i);
8107 isl_multi_union_pw_aff_free(mupa);
8108 return zero;
8109 error:
8110 isl_multi_union_pw_aff_free(mupa);
8111 return NULL;
8114 /* Construct a union map mapping the shared domain
8115 * of the union piecewise affine expressions to the range of "mupa"
8116 * with each dimension in the range equated to the
8117 * corresponding union piecewise affine expression.
8119 * The input cannot be zero-dimensional as there is
8120 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8122 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8123 __isl_take isl_multi_union_pw_aff *mupa)
8125 int i, n;
8126 isl_space *space;
8127 isl_union_map *umap;
8128 isl_union_pw_aff *upa;
8130 if (!mupa)
8131 return NULL;
8133 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8134 if (n == 0)
8135 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8136 "cannot determine domain of zero-dimensional "
8137 "isl_multi_union_pw_aff", goto error);
8139 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8140 umap = isl_union_map_from_union_pw_aff(upa);
8142 for (i = 1; i < n; ++i) {
8143 isl_union_map *umap_i;
8145 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8146 umap_i = isl_union_map_from_union_pw_aff(upa);
8147 umap = isl_union_map_flat_range_product(umap, umap_i);
8150 space = isl_multi_union_pw_aff_get_space(mupa);
8151 umap = isl_union_map_reset_range_space(umap, space);
8153 isl_multi_union_pw_aff_free(mupa);
8154 return umap;
8155 error:
8156 isl_multi_union_pw_aff_free(mupa);
8157 return NULL;
8160 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8161 * "range" is the space from which to set the range space.
8162 * "res" collects the results.
8164 struct isl_union_pw_multi_aff_reset_range_space_data {
8165 isl_space *range;
8166 isl_union_pw_multi_aff *res;
8169 /* Replace the range space of "pma" by the range space of data->range and
8170 * add the result to data->res.
8172 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8174 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8175 isl_space *space;
8177 space = isl_pw_multi_aff_get_space(pma);
8178 space = isl_space_domain(space);
8179 space = isl_space_extend_domain_with_range(space,
8180 isl_space_copy(data->range));
8181 pma = isl_pw_multi_aff_reset_space(pma, space);
8182 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8184 return data->res ? isl_stat_ok : isl_stat_error;
8187 /* Replace the range space of all the piecewise affine expressions in "upma" by
8188 * the range space of "space".
8190 * This assumes that all these expressions have the same output dimension.
8192 * Since the spaces of the expressions change, so do their hash values.
8193 * We therefore need to create a new isl_union_pw_multi_aff.
8194 * Note that the hash value is currently computed based on the entire
8195 * space even though there can only be a single expression with a given
8196 * domain space.
8198 static __isl_give isl_union_pw_multi_aff *
8199 isl_union_pw_multi_aff_reset_range_space(
8200 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8202 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8203 isl_space *space_upma;
8205 space_upma = isl_union_pw_multi_aff_get_space(upma);
8206 data.res = isl_union_pw_multi_aff_empty(space_upma);
8207 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8208 &reset_range_space, &data) < 0)
8209 data.res = isl_union_pw_multi_aff_free(data.res);
8211 isl_space_free(space);
8212 isl_union_pw_multi_aff_free(upma);
8213 return data.res;
8216 /* Construct and return a union piecewise multi affine expression
8217 * that is equal to the given multi union piecewise affine expression.
8219 * In order to be able to perform the conversion, the input
8220 * needs to have a least one output dimension.
8222 __isl_give isl_union_pw_multi_aff *
8223 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8224 __isl_take isl_multi_union_pw_aff *mupa)
8226 int i, n;
8227 isl_space *space;
8228 isl_union_pw_multi_aff *upma;
8229 isl_union_pw_aff *upa;
8231 if (!mupa)
8232 return NULL;
8234 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8235 if (n == 0)
8236 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8237 "cannot determine domain of zero-dimensional "
8238 "isl_multi_union_pw_aff", goto error);
8240 space = isl_multi_union_pw_aff_get_space(mupa);
8241 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8242 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8244 for (i = 1; i < n; ++i) {
8245 isl_union_pw_multi_aff *upma_i;
8247 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8248 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8249 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8252 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8254 isl_multi_union_pw_aff_free(mupa);
8255 return upma;
8256 error:
8257 isl_multi_union_pw_aff_free(mupa);
8258 return NULL;
8261 /* Intersect the range of "mupa" with "range".
8262 * That is, keep only those domain elements that have a function value
8263 * in "range".
8265 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8266 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8268 isl_union_pw_multi_aff *upma;
8269 isl_union_set *domain;
8270 isl_space *space;
8271 int n;
8272 int match;
8274 if (!mupa || !range)
8275 goto error;
8277 space = isl_set_get_space(range);
8278 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8279 space, isl_dim_set);
8280 isl_space_free(space);
8281 if (match < 0)
8282 goto error;
8283 if (!match)
8284 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8285 "space don't match", goto error);
8286 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8287 if (n == 0)
8288 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8289 "cannot intersect range of zero-dimensional "
8290 "isl_multi_union_pw_aff", goto error);
8292 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8293 isl_multi_union_pw_aff_copy(mupa));
8294 domain = isl_union_set_from_set(range);
8295 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8296 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8298 return mupa;
8299 error:
8300 isl_multi_union_pw_aff_free(mupa);
8301 isl_set_free(range);
8302 return NULL;
8305 /* Return the shared domain of the elements of "mupa".
8307 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8308 __isl_take isl_multi_union_pw_aff *mupa)
8310 int i, n;
8311 isl_union_pw_aff *upa;
8312 isl_union_set *dom;
8314 if (!mupa)
8315 return NULL;
8317 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8318 if (n == 0)
8319 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8320 "cannot determine domain", goto error);
8322 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8323 dom = isl_union_pw_aff_domain(upa);
8324 for (i = 1; i < n; ++i) {
8325 isl_union_set *dom_i;
8327 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8328 dom_i = isl_union_pw_aff_domain(upa);
8329 dom = isl_union_set_intersect(dom, dom_i);
8332 isl_multi_union_pw_aff_free(mupa);
8333 return dom;
8334 error:
8335 isl_multi_union_pw_aff_free(mupa);
8336 return NULL;
8339 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8340 * In particular, the spaces have been aligned.
8341 * The result is defined over the shared domain of the elements of "mupa"
8343 * We first extract the parametric constant part of "aff" and
8344 * define that over the shared domain.
8345 * Then we iterate over all input dimensions of "aff" and add the corresponding
8346 * multiples of the elements of "mupa".
8347 * Finally, we consider the integer divisions, calling the function
8348 * recursively to obtain an isl_union_pw_aff corresponding to the
8349 * integer division argument.
8351 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8352 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8354 int i, n_in, n_div;
8355 isl_union_pw_aff *upa;
8356 isl_union_set *uset;
8357 isl_val *v;
8358 isl_aff *cst;
8360 n_in = isl_aff_dim(aff, isl_dim_in);
8361 n_div = isl_aff_dim(aff, isl_dim_div);
8363 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8364 cst = isl_aff_copy(aff);
8365 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8366 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8367 cst = isl_aff_project_domain_on_params(cst);
8368 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8370 for (i = 0; i < n_in; ++i) {
8371 isl_union_pw_aff *upa_i;
8373 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8374 continue;
8375 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8376 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8377 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8378 upa = isl_union_pw_aff_add(upa, upa_i);
8381 for (i = 0; i < n_div; ++i) {
8382 isl_aff *div;
8383 isl_union_pw_aff *upa_i;
8385 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8386 continue;
8387 div = isl_aff_get_div(aff, i);
8388 upa_i = multi_union_pw_aff_apply_aff(
8389 isl_multi_union_pw_aff_copy(mupa), div);
8390 upa_i = isl_union_pw_aff_floor(upa_i);
8391 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8392 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8393 upa = isl_union_pw_aff_add(upa, upa_i);
8396 isl_multi_union_pw_aff_free(mupa);
8397 isl_aff_free(aff);
8399 return upa;
8402 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8403 * with the domain of "aff".
8404 * Furthermore, the dimension of this space needs to be greater than zero.
8405 * The result is defined over the shared domain of the elements of "mupa"
8407 * We perform these checks and then hand over control to
8408 * multi_union_pw_aff_apply_aff.
8410 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8411 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8413 isl_space *space1, *space2;
8414 int equal;
8416 mupa = isl_multi_union_pw_aff_align_params(mupa,
8417 isl_aff_get_space(aff));
8418 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8419 if (!mupa || !aff)
8420 goto error;
8422 space1 = isl_multi_union_pw_aff_get_space(mupa);
8423 space2 = isl_aff_get_domain_space(aff);
8424 equal = isl_space_is_equal(space1, space2);
8425 isl_space_free(space1);
8426 isl_space_free(space2);
8427 if (equal < 0)
8428 goto error;
8429 if (!equal)
8430 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8431 "spaces don't match", goto error);
8432 if (isl_aff_dim(aff, isl_dim_in) == 0)
8433 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8434 "cannot determine domains", goto error);
8436 return multi_union_pw_aff_apply_aff(mupa, aff);
8437 error:
8438 isl_multi_union_pw_aff_free(mupa);
8439 isl_aff_free(aff);
8440 return NULL;
8443 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8444 * with the domain of "ma".
8445 * Furthermore, the dimension of this space needs to be greater than zero,
8446 * unless the dimension of the target space of "ma" is also zero.
8447 * The result is defined over the shared domain of the elements of "mupa"
8449 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8450 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8452 isl_space *space1, *space2;
8453 isl_multi_union_pw_aff *res;
8454 int equal;
8455 int i, n_out;
8457 mupa = isl_multi_union_pw_aff_align_params(mupa,
8458 isl_multi_aff_get_space(ma));
8459 ma = isl_multi_aff_align_params(ma,
8460 isl_multi_union_pw_aff_get_space(mupa));
8461 if (!mupa || !ma)
8462 goto error;
8464 space1 = isl_multi_union_pw_aff_get_space(mupa);
8465 space2 = isl_multi_aff_get_domain_space(ma);
8466 equal = isl_space_is_equal(space1, space2);
8467 isl_space_free(space1);
8468 isl_space_free(space2);
8469 if (equal < 0)
8470 goto error;
8471 if (!equal)
8472 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8473 "spaces don't match", goto error);
8474 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8475 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8476 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8477 "cannot determine domains", goto error);
8479 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8480 res = isl_multi_union_pw_aff_alloc(space1);
8482 for (i = 0; i < n_out; ++i) {
8483 isl_aff *aff;
8484 isl_union_pw_aff *upa;
8486 aff = isl_multi_aff_get_aff(ma, i);
8487 upa = multi_union_pw_aff_apply_aff(
8488 isl_multi_union_pw_aff_copy(mupa), aff);
8489 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8492 isl_multi_aff_free(ma);
8493 isl_multi_union_pw_aff_free(mupa);
8494 return res;
8495 error:
8496 isl_multi_union_pw_aff_free(mupa);
8497 isl_multi_aff_free(ma);
8498 return NULL;
8501 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8502 * with the domain of "pa".
8503 * Furthermore, the dimension of this space needs to be greater than zero.
8504 * The result is defined over the shared domain of the elements of "mupa"
8506 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8507 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8509 int i;
8510 int equal;
8511 isl_space *space, *space2;
8512 isl_union_pw_aff *upa;
8514 mupa = isl_multi_union_pw_aff_align_params(mupa,
8515 isl_pw_aff_get_space(pa));
8516 pa = isl_pw_aff_align_params(pa,
8517 isl_multi_union_pw_aff_get_space(mupa));
8518 if (!mupa || !pa)
8519 goto error;
8521 space = isl_multi_union_pw_aff_get_space(mupa);
8522 space2 = isl_pw_aff_get_domain_space(pa);
8523 equal = isl_space_is_equal(space, space2);
8524 isl_space_free(space);
8525 isl_space_free(space2);
8526 if (equal < 0)
8527 goto error;
8528 if (!equal)
8529 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8530 "spaces don't match", goto error);
8531 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8532 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8533 "cannot determine domains", goto error);
8535 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8536 upa = isl_union_pw_aff_empty(space);
8538 for (i = 0; i < pa->n; ++i) {
8539 isl_aff *aff;
8540 isl_set *domain;
8541 isl_multi_union_pw_aff *mupa_i;
8542 isl_union_pw_aff *upa_i;
8544 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8545 domain = isl_set_copy(pa->p[i].set);
8546 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8547 aff = isl_aff_copy(pa->p[i].aff);
8548 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8549 upa = isl_union_pw_aff_union_add(upa, upa_i);
8552 isl_multi_union_pw_aff_free(mupa);
8553 isl_pw_aff_free(pa);
8554 return upa;
8555 error:
8556 isl_multi_union_pw_aff_free(mupa);
8557 isl_pw_aff_free(pa);
8558 return NULL;
8561 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8562 * with the domain of "pma".
8563 * Furthermore, the dimension of this space needs to be greater than zero,
8564 * unless the dimension of the target space of "pma" is also zero.
8565 * The result is defined over the shared domain of the elements of "mupa"
8567 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8568 __isl_take isl_multi_union_pw_aff *mupa,
8569 __isl_take isl_pw_multi_aff *pma)
8571 isl_space *space1, *space2;
8572 isl_multi_union_pw_aff *res;
8573 int equal;
8574 int i, n_out;
8576 mupa = isl_multi_union_pw_aff_align_params(mupa,
8577 isl_pw_multi_aff_get_space(pma));
8578 pma = isl_pw_multi_aff_align_params(pma,
8579 isl_multi_union_pw_aff_get_space(mupa));
8580 if (!mupa || !pma)
8581 goto error;
8583 space1 = isl_multi_union_pw_aff_get_space(mupa);
8584 space2 = isl_pw_multi_aff_get_domain_space(pma);
8585 equal = isl_space_is_equal(space1, space2);
8586 isl_space_free(space1);
8587 isl_space_free(space2);
8588 if (equal < 0)
8589 goto error;
8590 if (!equal)
8591 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8592 "spaces don't match", goto error);
8593 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8594 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8595 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8596 "cannot determine domains", goto error);
8598 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8599 res = isl_multi_union_pw_aff_alloc(space1);
8601 for (i = 0; i < n_out; ++i) {
8602 isl_pw_aff *pa;
8603 isl_union_pw_aff *upa;
8605 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8606 upa = isl_multi_union_pw_aff_apply_pw_aff(
8607 isl_multi_union_pw_aff_copy(mupa), pa);
8608 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8611 isl_pw_multi_aff_free(pma);
8612 isl_multi_union_pw_aff_free(mupa);
8613 return res;
8614 error:
8615 isl_multi_union_pw_aff_free(mupa);
8616 isl_pw_multi_aff_free(pma);
8617 return NULL;
8620 /* Compute the pullback of "mupa" by the function represented by "upma".
8621 * In other words, plug in "upma" in "mupa". The result contains
8622 * expressions defined over the domain space of "upma".
8624 * Run over all elements of "mupa" and plug in "upma" in each of them.
8626 __isl_give isl_multi_union_pw_aff *
8627 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8628 __isl_take isl_multi_union_pw_aff *mupa,
8629 __isl_take isl_union_pw_multi_aff *upma)
8631 int i, n;
8633 mupa = isl_multi_union_pw_aff_align_params(mupa,
8634 isl_union_pw_multi_aff_get_space(upma));
8635 upma = isl_union_pw_multi_aff_align_params(upma,
8636 isl_multi_union_pw_aff_get_space(mupa));
8637 if (!mupa || !upma)
8638 goto error;
8640 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8641 for (i = 0; i < n; ++i) {
8642 isl_union_pw_aff *upa;
8644 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8645 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8646 isl_union_pw_multi_aff_copy(upma));
8647 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8650 isl_union_pw_multi_aff_free(upma);
8651 return mupa;
8652 error:
8653 isl_multi_union_pw_aff_free(mupa);
8654 isl_union_pw_multi_aff_free(upma);
8655 return NULL;
8658 /* Extract the sequence of elements in "mupa" with domain space "space"
8659 * (ignoring parameters).
8661 * For the elements of "mupa" that are not defined on the specified space,
8662 * the corresponding element in the result is empty.
8664 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8665 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8667 int i, n;
8668 isl_space *space_mpa = NULL;
8669 isl_multi_pw_aff *mpa;
8671 if (!mupa || !space)
8672 goto error;
8674 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8675 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8676 space = isl_space_drop_dims(space, isl_dim_param,
8677 0, isl_space_dim(space, isl_dim_param));
8678 space = isl_space_align_params(space,
8679 isl_space_copy(space_mpa));
8680 if (!space)
8681 goto error;
8683 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8684 space_mpa);
8685 mpa = isl_multi_pw_aff_alloc(space_mpa);
8687 space = isl_space_from_domain(space);
8688 space = isl_space_add_dims(space, isl_dim_out, 1);
8689 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8690 for (i = 0; i < n; ++i) {
8691 isl_union_pw_aff *upa;
8692 isl_pw_aff *pa;
8694 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8695 pa = isl_union_pw_aff_extract_pw_aff(upa,
8696 isl_space_copy(space));
8697 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8698 isl_union_pw_aff_free(upa);
8701 isl_space_free(space);
8702 return mpa;
8703 error:
8704 isl_space_free(space_mpa);
8705 isl_space_free(space);
8706 return NULL;