add isl_schedule_constraints_read_from_str
[isl.git] / isl_aff.c
blob3be95ffece741f89f50e0000f7c3bca06e3ee48e
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 old_n_div;
1725 int new_n_div;
1726 int offset;
1728 aff = isl_aff_cow(aff);
1729 if (!aff || !div)
1730 goto error;
1732 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1733 new_n_div = isl_mat_rows(div);
1734 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1736 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1737 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1738 if (!aff->v || !aff->ls)
1739 return isl_aff_free(aff);
1740 return aff;
1741 error:
1742 isl_aff_free(aff);
1743 isl_mat_free(div);
1744 return NULL;
1747 /* Add two affine expressions that live in the same local space.
1749 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1750 __isl_take isl_aff *aff2)
1752 isl_int gcd, f;
1754 aff1 = isl_aff_cow(aff1);
1755 if (!aff1 || !aff2)
1756 goto error;
1758 aff1->v = isl_vec_cow(aff1->v);
1759 if (!aff1->v)
1760 goto error;
1762 isl_int_init(gcd);
1763 isl_int_init(f);
1764 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1765 isl_int_divexact(f, aff2->v->el[0], gcd);
1766 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1767 isl_int_divexact(f, aff1->v->el[0], gcd);
1768 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1769 isl_int_divexact(f, aff2->v->el[0], gcd);
1770 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1771 isl_int_clear(f);
1772 isl_int_clear(gcd);
1774 isl_aff_free(aff2);
1775 return aff1;
1776 error:
1777 isl_aff_free(aff1);
1778 isl_aff_free(aff2);
1779 return NULL;
1782 /* Return the sum of "aff1" and "aff2".
1784 * If either of the two is NaN, then the result is NaN.
1786 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1787 __isl_take isl_aff *aff2)
1789 isl_ctx *ctx;
1790 int *exp1 = NULL;
1791 int *exp2 = NULL;
1792 isl_mat *div;
1793 int n_div1, n_div2;
1795 if (!aff1 || !aff2)
1796 goto error;
1798 ctx = isl_aff_get_ctx(aff1);
1799 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1800 isl_die(ctx, isl_error_invalid,
1801 "spaces don't match", goto error);
1803 if (isl_aff_is_nan(aff1)) {
1804 isl_aff_free(aff2);
1805 return aff1;
1807 if (isl_aff_is_nan(aff2)) {
1808 isl_aff_free(aff1);
1809 return aff2;
1812 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1813 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1814 if (n_div1 == 0 && n_div2 == 0)
1815 return add_expanded(aff1, aff2);
1817 exp1 = isl_alloc_array(ctx, int, n_div1);
1818 exp2 = isl_alloc_array(ctx, int, n_div2);
1819 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1820 goto error;
1822 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1823 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1824 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1825 free(exp1);
1826 free(exp2);
1828 return add_expanded(aff1, aff2);
1829 error:
1830 free(exp1);
1831 free(exp2);
1832 isl_aff_free(aff1);
1833 isl_aff_free(aff2);
1834 return NULL;
1837 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1838 __isl_take isl_aff *aff2)
1840 return isl_aff_add(aff1, isl_aff_neg(aff2));
1843 /* Return the result of scaling "aff" by a factor of "f".
1845 * As a special case, f * NaN = NaN.
1847 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1849 isl_int gcd;
1851 if (!aff)
1852 return NULL;
1853 if (isl_aff_is_nan(aff))
1854 return aff;
1856 if (isl_int_is_one(f))
1857 return aff;
1859 aff = isl_aff_cow(aff);
1860 if (!aff)
1861 return NULL;
1862 aff->v = isl_vec_cow(aff->v);
1863 if (!aff->v)
1864 return isl_aff_free(aff);
1866 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1867 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1868 return aff;
1871 isl_int_init(gcd);
1872 isl_int_gcd(gcd, aff->v->el[0], f);
1873 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1874 isl_int_divexact(gcd, f, gcd);
1875 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1876 isl_int_clear(gcd);
1878 return aff;
1881 /* Multiple "aff" by "v".
1883 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1884 __isl_take isl_val *v)
1886 if (!aff || !v)
1887 goto error;
1889 if (isl_val_is_one(v)) {
1890 isl_val_free(v);
1891 return aff;
1894 if (!isl_val_is_rat(v))
1895 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1896 "expecting rational factor", goto error);
1898 aff = isl_aff_scale(aff, v->n);
1899 aff = isl_aff_scale_down(aff, v->d);
1901 isl_val_free(v);
1902 return aff;
1903 error:
1904 isl_aff_free(aff);
1905 isl_val_free(v);
1906 return NULL;
1909 /* Return the result of scaling "aff" down by a factor of "f".
1911 * As a special case, NaN/f = NaN.
1913 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1915 isl_int gcd;
1917 if (!aff)
1918 return NULL;
1919 if (isl_aff_is_nan(aff))
1920 return aff;
1922 if (isl_int_is_one(f))
1923 return aff;
1925 aff = isl_aff_cow(aff);
1926 if (!aff)
1927 return NULL;
1929 if (isl_int_is_zero(f))
1930 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1931 "cannot scale down by zero", return isl_aff_free(aff));
1933 aff->v = isl_vec_cow(aff->v);
1934 if (!aff->v)
1935 return isl_aff_free(aff);
1937 isl_int_init(gcd);
1938 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1939 isl_int_gcd(gcd, gcd, f);
1940 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1941 isl_int_divexact(gcd, f, gcd);
1942 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1943 isl_int_clear(gcd);
1945 return aff;
1948 /* Divide "aff" by "v".
1950 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1951 __isl_take isl_val *v)
1953 if (!aff || !v)
1954 goto error;
1956 if (isl_val_is_one(v)) {
1957 isl_val_free(v);
1958 return aff;
1961 if (!isl_val_is_rat(v))
1962 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1963 "expecting rational factor", goto error);
1964 if (!isl_val_is_pos(v))
1965 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1966 "factor needs to be positive", goto error);
1968 aff = isl_aff_scale(aff, v->d);
1969 aff = isl_aff_scale_down(aff, v->n);
1971 isl_val_free(v);
1972 return aff;
1973 error:
1974 isl_aff_free(aff);
1975 isl_val_free(v);
1976 return NULL;
1979 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1981 isl_int v;
1983 if (f == 1)
1984 return aff;
1986 isl_int_init(v);
1987 isl_int_set_ui(v, f);
1988 aff = isl_aff_scale_down(aff, v);
1989 isl_int_clear(v);
1991 return aff;
1994 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1995 enum isl_dim_type type, unsigned pos, const char *s)
1997 aff = isl_aff_cow(aff);
1998 if (!aff)
1999 return NULL;
2000 if (type == isl_dim_out)
2001 isl_die(aff->v->ctx, isl_error_invalid,
2002 "cannot set name of output/set dimension",
2003 return isl_aff_free(aff));
2004 if (type == isl_dim_in)
2005 type = isl_dim_set;
2006 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2007 if (!aff->ls)
2008 return isl_aff_free(aff);
2010 return aff;
2013 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2014 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2016 aff = isl_aff_cow(aff);
2017 if (!aff)
2018 goto error;
2019 if (type == isl_dim_out)
2020 isl_die(aff->v->ctx, isl_error_invalid,
2021 "cannot set name of output/set dimension",
2022 goto error);
2023 if (type == isl_dim_in)
2024 type = isl_dim_set;
2025 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2026 if (!aff->ls)
2027 return isl_aff_free(aff);
2029 return aff;
2030 error:
2031 isl_id_free(id);
2032 isl_aff_free(aff);
2033 return NULL;
2036 /* Replace the identifier of the input tuple of "aff" by "id".
2037 * type is currently required to be equal to isl_dim_in
2039 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2040 enum isl_dim_type type, __isl_take isl_id *id)
2042 aff = isl_aff_cow(aff);
2043 if (!aff)
2044 goto error;
2045 if (type != isl_dim_out)
2046 isl_die(aff->v->ctx, isl_error_invalid,
2047 "cannot only set id of input tuple", goto error);
2048 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2049 if (!aff->ls)
2050 return isl_aff_free(aff);
2052 return aff;
2053 error:
2054 isl_id_free(id);
2055 isl_aff_free(aff);
2056 return NULL;
2059 /* Exploit the equalities in "eq" to simplify the affine expression
2060 * and the expressions of the integer divisions in the local space.
2061 * The integer divisions in this local space are assumed to appear
2062 * as regular dimensions in "eq".
2064 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2065 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2067 int i, j;
2068 unsigned total;
2069 unsigned n_div;
2071 if (!eq)
2072 goto error;
2073 if (eq->n_eq == 0) {
2074 isl_basic_set_free(eq);
2075 return aff;
2078 aff = isl_aff_cow(aff);
2079 if (!aff)
2080 goto error;
2082 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2083 isl_basic_set_copy(eq));
2084 aff->v = isl_vec_cow(aff->v);
2085 if (!aff->ls || !aff->v)
2086 goto error;
2088 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2089 n_div = eq->n_div;
2090 for (i = 0; i < eq->n_eq; ++i) {
2091 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2092 if (j < 0 || j == 0 || j >= total)
2093 continue;
2095 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2096 &aff->v->el[0]);
2099 isl_basic_set_free(eq);
2100 aff = isl_aff_normalize(aff);
2101 return aff;
2102 error:
2103 isl_basic_set_free(eq);
2104 isl_aff_free(aff);
2105 return NULL;
2108 /* Exploit the equalities in "eq" to simplify the affine expression
2109 * and the expressions of the integer divisions in the local space.
2111 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2112 __isl_take isl_basic_set *eq)
2114 int n_div;
2116 if (!aff || !eq)
2117 goto error;
2118 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2119 if (n_div > 0)
2120 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2121 return isl_aff_substitute_equalities_lifted(aff, eq);
2122 error:
2123 isl_basic_set_free(eq);
2124 isl_aff_free(aff);
2125 return NULL;
2128 /* Look for equalities among the variables shared by context and aff
2129 * and the integer divisions of aff, if any.
2130 * The equalities are then used to eliminate coefficients and/or integer
2131 * divisions from aff.
2133 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2134 __isl_take isl_set *context)
2136 isl_basic_set *hull;
2137 int n_div;
2139 if (!aff)
2140 goto error;
2141 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2142 if (n_div > 0) {
2143 isl_basic_set *bset;
2144 isl_local_space *ls;
2145 context = isl_set_add_dims(context, isl_dim_set, n_div);
2146 ls = isl_aff_get_domain_local_space(aff);
2147 bset = isl_basic_set_from_local_space(ls);
2148 bset = isl_basic_set_lift(bset);
2149 bset = isl_basic_set_flatten(bset);
2150 context = isl_set_intersect(context,
2151 isl_set_from_basic_set(bset));
2154 hull = isl_set_affine_hull(context);
2155 return isl_aff_substitute_equalities_lifted(aff, hull);
2156 error:
2157 isl_aff_free(aff);
2158 isl_set_free(context);
2159 return NULL;
2162 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2163 __isl_take isl_set *context)
2165 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2166 dom_context = isl_set_intersect_params(dom_context, context);
2167 return isl_aff_gist(aff, dom_context);
2170 /* Return a basic set containing those elements in the space
2171 * of aff where it is positive. "rational" should not be set.
2173 * If "aff" is NaN, then it is not positive.
2175 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2176 int rational)
2178 isl_constraint *ineq;
2179 isl_basic_set *bset;
2180 isl_val *c;
2182 if (!aff)
2183 return NULL;
2184 if (isl_aff_is_nan(aff)) {
2185 isl_space *space = isl_aff_get_domain_space(aff);
2186 isl_aff_free(aff);
2187 return isl_basic_set_empty(space);
2189 if (rational)
2190 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2191 "rational sets not supported", goto error);
2193 ineq = isl_inequality_from_aff(aff);
2194 c = isl_constraint_get_constant_val(ineq);
2195 c = isl_val_sub_ui(c, 1);
2196 ineq = isl_constraint_set_constant_val(ineq, c);
2198 bset = isl_basic_set_from_constraint(ineq);
2199 bset = isl_basic_set_simplify(bset);
2200 return bset;
2201 error:
2202 isl_aff_free(aff);
2203 return NULL;
2206 /* Return a basic set containing those elements in the space
2207 * of aff where it is non-negative.
2208 * If "rational" is set, then return a rational basic set.
2210 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2212 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2213 __isl_take isl_aff *aff, int rational)
2215 isl_constraint *ineq;
2216 isl_basic_set *bset;
2218 if (!aff)
2219 return NULL;
2220 if (isl_aff_is_nan(aff)) {
2221 isl_space *space = isl_aff_get_domain_space(aff);
2222 isl_aff_free(aff);
2223 return isl_basic_set_empty(space);
2226 ineq = isl_inequality_from_aff(aff);
2228 bset = isl_basic_set_from_constraint(ineq);
2229 if (rational)
2230 bset = isl_basic_set_set_rational(bset);
2231 bset = isl_basic_set_simplify(bset);
2232 return bset;
2235 /* Return a basic set containing those elements in the space
2236 * of aff where it is non-negative.
2238 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2240 return aff_nonneg_basic_set(aff, 0);
2243 /* Return a basic set containing those elements in the domain space
2244 * of aff where it is negative.
2246 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2248 aff = isl_aff_neg(aff);
2249 aff = isl_aff_add_constant_num_si(aff, -1);
2250 return isl_aff_nonneg_basic_set(aff);
2253 /* Return a basic set containing those elements in the space
2254 * of aff where it is zero.
2255 * If "rational" is set, then return a rational basic set.
2257 * If "aff" is NaN, then it is not zero.
2259 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2260 int rational)
2262 isl_constraint *ineq;
2263 isl_basic_set *bset;
2265 if (!aff)
2266 return NULL;
2267 if (isl_aff_is_nan(aff)) {
2268 isl_space *space = isl_aff_get_domain_space(aff);
2269 isl_aff_free(aff);
2270 return isl_basic_set_empty(space);
2273 ineq = isl_equality_from_aff(aff);
2275 bset = isl_basic_set_from_constraint(ineq);
2276 if (rational)
2277 bset = isl_basic_set_set_rational(bset);
2278 bset = isl_basic_set_simplify(bset);
2279 return bset;
2282 /* Return a basic set containing those elements in the space
2283 * of aff where it is zero.
2285 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2287 return aff_zero_basic_set(aff, 0);
2290 /* Return a basic set containing those elements in the shared space
2291 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2293 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2294 __isl_take isl_aff *aff2)
2296 aff1 = isl_aff_sub(aff1, aff2);
2298 return isl_aff_nonneg_basic_set(aff1);
2301 /* Return a set containing those elements in the shared space
2302 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2304 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2305 __isl_take isl_aff *aff2)
2307 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2310 /* Return a basic set containing those elements in the shared space
2311 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2313 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2314 __isl_take isl_aff *aff2)
2316 return isl_aff_ge_basic_set(aff2, aff1);
2319 /* Return a set containing those elements in the shared space
2320 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2322 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2323 __isl_take isl_aff *aff2)
2325 return isl_aff_ge_set(aff2, aff1);
2328 /* Return a basic set containing those elements in the shared space
2329 * of aff1 and aff2 where aff1 and aff2 are equal.
2331 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2332 __isl_take isl_aff *aff2)
2334 aff1 = isl_aff_sub(aff1, aff2);
2336 return isl_aff_zero_basic_set(aff1);
2339 /* Return a set containing those elements in the shared space
2340 * of aff1 and aff2 where aff1 and aff2 are equal.
2342 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2343 __isl_take isl_aff *aff2)
2345 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2348 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2349 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2351 aff1 = isl_aff_add(aff1, aff2);
2352 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2353 return aff1;
2356 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2358 if (!aff)
2359 return -1;
2361 return 0;
2364 /* Check whether the given affine expression has non-zero coefficient
2365 * for any dimension in the given range or if any of these dimensions
2366 * appear with non-zero coefficients in any of the integer divisions
2367 * involved in the affine expression.
2369 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2370 enum isl_dim_type type, unsigned first, unsigned n)
2372 int i;
2373 isl_ctx *ctx;
2374 int *active = NULL;
2375 isl_bool involves = isl_bool_false;
2377 if (!aff)
2378 return isl_bool_error;
2379 if (n == 0)
2380 return isl_bool_false;
2382 ctx = isl_aff_get_ctx(aff);
2383 if (first + n > isl_aff_dim(aff, type))
2384 isl_die(ctx, isl_error_invalid,
2385 "range out of bounds", return isl_bool_error);
2387 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2388 if (!active)
2389 goto error;
2391 first += isl_local_space_offset(aff->ls, type) - 1;
2392 for (i = 0; i < n; ++i)
2393 if (active[first + i]) {
2394 involves = isl_bool_true;
2395 break;
2398 free(active);
2400 return involves;
2401 error:
2402 free(active);
2403 return isl_bool_error;
2406 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2407 enum isl_dim_type type, unsigned first, unsigned n)
2409 isl_ctx *ctx;
2411 if (!aff)
2412 return NULL;
2413 if (type == isl_dim_out)
2414 isl_die(aff->v->ctx, isl_error_invalid,
2415 "cannot drop output/set dimension",
2416 return isl_aff_free(aff));
2417 if (type == isl_dim_in)
2418 type = isl_dim_set;
2419 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2420 return aff;
2422 ctx = isl_aff_get_ctx(aff);
2423 if (first + n > isl_local_space_dim(aff->ls, type))
2424 isl_die(ctx, isl_error_invalid, "range out of bounds",
2425 return isl_aff_free(aff));
2427 aff = isl_aff_cow(aff);
2428 if (!aff)
2429 return NULL;
2431 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2432 if (!aff->ls)
2433 return isl_aff_free(aff);
2435 first += 1 + isl_local_space_offset(aff->ls, type);
2436 aff->v = isl_vec_drop_els(aff->v, first, n);
2437 if (!aff->v)
2438 return isl_aff_free(aff);
2440 return aff;
2443 /* Project the domain of the affine expression onto its parameter space.
2444 * The affine expression may not involve any of the domain dimensions.
2446 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2448 isl_space *space;
2449 unsigned n;
2450 int involves;
2452 n = isl_aff_dim(aff, isl_dim_in);
2453 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2454 if (involves < 0)
2455 return isl_aff_free(aff);
2456 if (involves)
2457 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2458 "affine expression involves some of the domain dimensions",
2459 return isl_aff_free(aff));
2460 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2461 space = isl_aff_get_domain_space(aff);
2462 space = isl_space_params(space);
2463 aff = isl_aff_reset_domain_space(aff, space);
2464 return aff;
2467 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2468 enum isl_dim_type type, unsigned first, unsigned n)
2470 isl_ctx *ctx;
2472 if (!aff)
2473 return NULL;
2474 if (type == isl_dim_out)
2475 isl_die(aff->v->ctx, isl_error_invalid,
2476 "cannot insert output/set dimensions",
2477 return isl_aff_free(aff));
2478 if (type == isl_dim_in)
2479 type = isl_dim_set;
2480 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2481 return aff;
2483 ctx = isl_aff_get_ctx(aff);
2484 if (first > isl_local_space_dim(aff->ls, type))
2485 isl_die(ctx, isl_error_invalid, "position out of bounds",
2486 return isl_aff_free(aff));
2488 aff = isl_aff_cow(aff);
2489 if (!aff)
2490 return NULL;
2492 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2493 if (!aff->ls)
2494 return isl_aff_free(aff);
2496 first += 1 + isl_local_space_offset(aff->ls, type);
2497 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2498 if (!aff->v)
2499 return isl_aff_free(aff);
2501 return aff;
2504 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2505 enum isl_dim_type type, unsigned n)
2507 unsigned pos;
2509 pos = isl_aff_dim(aff, type);
2511 return isl_aff_insert_dims(aff, type, pos, n);
2514 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2515 enum isl_dim_type type, unsigned n)
2517 unsigned pos;
2519 pos = isl_pw_aff_dim(pwaff, type);
2521 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2524 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2525 * to dimensions of "dst_type" at "dst_pos".
2527 * We only support moving input dimensions to parameters and vice versa.
2529 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2530 enum isl_dim_type dst_type, unsigned dst_pos,
2531 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2533 unsigned g_dst_pos;
2534 unsigned g_src_pos;
2536 if (!aff)
2537 return NULL;
2538 if (n == 0 &&
2539 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2540 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2541 return aff;
2543 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2544 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2545 "cannot move output/set dimension",
2546 return isl_aff_free(aff));
2547 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2548 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2549 "cannot move divs", return isl_aff_free(aff));
2550 if (dst_type == isl_dim_in)
2551 dst_type = isl_dim_set;
2552 if (src_type == isl_dim_in)
2553 src_type = isl_dim_set;
2555 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2556 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2557 "range out of bounds", return isl_aff_free(aff));
2558 if (dst_type == src_type)
2559 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2560 "moving dims within the same type not supported",
2561 return isl_aff_free(aff));
2563 aff = isl_aff_cow(aff);
2564 if (!aff)
2565 return NULL;
2567 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2568 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2569 if (dst_type > src_type)
2570 g_dst_pos -= n;
2572 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2573 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2574 src_type, src_pos, n);
2575 if (!aff->v || !aff->ls)
2576 return isl_aff_free(aff);
2578 aff = sort_divs(aff);
2580 return aff;
2583 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2585 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2586 return isl_pw_aff_alloc(dom, aff);
2589 #undef PW
2590 #define PW isl_pw_aff
2591 #undef EL
2592 #define EL isl_aff
2593 #undef EL_IS_ZERO
2594 #define EL_IS_ZERO is_empty
2595 #undef ZERO
2596 #define ZERO empty
2597 #undef IS_ZERO
2598 #define IS_ZERO is_empty
2599 #undef FIELD
2600 #define FIELD aff
2601 #undef DEFAULT_IS_ZERO
2602 #define DEFAULT_IS_ZERO 0
2604 #define NO_EVAL
2605 #define NO_OPT
2606 #define NO_LIFT
2607 #define NO_MORPH
2609 #include <isl_pw_templ.c>
2610 #include <isl_pw_hash.c>
2611 #include <isl_pw_union_opt.c>
2613 #undef UNION
2614 #define UNION isl_union_pw_aff
2615 #undef PART
2616 #define PART isl_pw_aff
2617 #undef PARTS
2618 #define PARTS pw_aff
2620 #include <isl_union_single.c>
2621 #include <isl_union_neg.c>
2623 static __isl_give isl_set *align_params_pw_pw_set_and(
2624 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2625 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2626 __isl_take isl_pw_aff *pwaff2))
2628 if (!pwaff1 || !pwaff2)
2629 goto error;
2630 if (isl_space_match(pwaff1->dim, isl_dim_param,
2631 pwaff2->dim, isl_dim_param))
2632 return fn(pwaff1, pwaff2);
2633 if (!isl_space_has_named_params(pwaff1->dim) ||
2634 !isl_space_has_named_params(pwaff2->dim))
2635 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2636 "unaligned unnamed parameters", goto error);
2637 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2638 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2639 return fn(pwaff1, pwaff2);
2640 error:
2641 isl_pw_aff_free(pwaff1);
2642 isl_pw_aff_free(pwaff2);
2643 return NULL;
2646 /* Align the parameters of the to isl_pw_aff arguments and
2647 * then apply a function "fn" on them that returns an isl_map.
2649 static __isl_give isl_map *align_params_pw_pw_map_and(
2650 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2651 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2652 __isl_take isl_pw_aff *pa2))
2654 if (!pa1 || !pa2)
2655 goto error;
2656 if (isl_space_match(pa1->dim, isl_dim_param, pa2->dim, isl_dim_param))
2657 return fn(pa1, pa2);
2658 if (!isl_space_has_named_params(pa1->dim) ||
2659 !isl_space_has_named_params(pa2->dim))
2660 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2661 "unaligned unnamed parameters", goto error);
2662 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2663 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2664 return fn(pa1, pa2);
2665 error:
2666 isl_pw_aff_free(pa1);
2667 isl_pw_aff_free(pa2);
2668 return NULL;
2671 /* Compute a piecewise quasi-affine expression with a domain that
2672 * is the union of those of pwaff1 and pwaff2 and such that on each
2673 * cell, the quasi-affine expression is the maximum of those of pwaff1
2674 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2675 * cell, then the associated expression is the defined one.
2677 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2678 __isl_take isl_pw_aff *pwaff2)
2680 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2683 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2684 __isl_take isl_pw_aff *pwaff2)
2686 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2687 &pw_aff_union_max);
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 minimum 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_min(__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_le_set);
2702 __isl_give isl_pw_aff *isl_pw_aff_union_min(__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_min);
2709 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2710 __isl_take isl_pw_aff *pwaff2, int max)
2712 if (max)
2713 return isl_pw_aff_union_max(pwaff1, pwaff2);
2714 else
2715 return isl_pw_aff_union_min(pwaff1, pwaff2);
2718 /* Construct a map with as domain the domain of pwaff and
2719 * one-dimensional range corresponding to the affine expressions.
2721 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2723 int i;
2724 isl_space *dim;
2725 isl_map *map;
2727 if (!pwaff)
2728 return NULL;
2730 dim = isl_pw_aff_get_space(pwaff);
2731 map = isl_map_empty(dim);
2733 for (i = 0; i < pwaff->n; ++i) {
2734 isl_basic_map *bmap;
2735 isl_map *map_i;
2737 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2738 map_i = isl_map_from_basic_map(bmap);
2739 map_i = isl_map_intersect_domain(map_i,
2740 isl_set_copy(pwaff->p[i].set));
2741 map = isl_map_union_disjoint(map, map_i);
2744 isl_pw_aff_free(pwaff);
2746 return map;
2749 /* Construct a map with as domain the domain of pwaff and
2750 * one-dimensional range corresponding to the affine expressions.
2752 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2754 if (!pwaff)
2755 return NULL;
2756 if (isl_space_is_set(pwaff->dim))
2757 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2758 "space of input is not a map", goto error);
2759 return map_from_pw_aff(pwaff);
2760 error:
2761 isl_pw_aff_free(pwaff);
2762 return NULL;
2765 /* Construct a one-dimensional set with as parameter domain
2766 * the domain of pwaff and the single set dimension
2767 * corresponding to the affine expressions.
2769 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2771 if (!pwaff)
2772 return NULL;
2773 if (!isl_space_is_set(pwaff->dim))
2774 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2775 "space of input is not a set", goto error);
2776 return map_from_pw_aff(pwaff);
2777 error:
2778 isl_pw_aff_free(pwaff);
2779 return NULL;
2782 /* Return a set containing those elements in the domain
2783 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2784 * does not satisfy "fn" (if complement is 1).
2786 * The pieces with a NaN never belong to the result since
2787 * NaN does not satisfy any property.
2789 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2790 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2791 int complement)
2793 int i;
2794 isl_set *set;
2796 if (!pwaff)
2797 return NULL;
2799 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2801 for (i = 0; i < pwaff->n; ++i) {
2802 isl_basic_set *bset;
2803 isl_set *set_i, *locus;
2804 int rational;
2806 if (isl_aff_is_nan(pwaff->p[i].aff))
2807 continue;
2809 rational = isl_set_has_rational(pwaff->p[i].set);
2810 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2811 locus = isl_set_from_basic_set(bset);
2812 set_i = isl_set_copy(pwaff->p[i].set);
2813 if (complement)
2814 set_i = isl_set_subtract(set_i, locus);
2815 else
2816 set_i = isl_set_intersect(set_i, locus);
2817 set = isl_set_union_disjoint(set, set_i);
2820 isl_pw_aff_free(pwaff);
2822 return set;
2825 /* Return a set containing those elements in the domain
2826 * of "pa" where it is positive.
2828 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2830 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2833 /* Return a set containing those elements in the domain
2834 * of pwaff where it is non-negative.
2836 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2838 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2841 /* Return a set containing those elements in the domain
2842 * of pwaff where it is zero.
2844 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2846 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2849 /* Return a set containing those elements in the domain
2850 * of pwaff where it is not zero.
2852 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2854 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2857 /* Return a set containing those elements in the shared domain
2858 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2860 * We compute the difference on the shared domain and then construct
2861 * the set of values where this difference is non-negative.
2862 * If strict is set, we first subtract 1 from the difference.
2863 * If equal is set, we only return the elements where pwaff1 and pwaff2
2864 * are equal.
2866 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2867 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2869 isl_set *set1, *set2;
2871 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2872 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2873 set1 = isl_set_intersect(set1, set2);
2874 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2875 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2876 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2878 if (strict) {
2879 isl_space *dim = isl_set_get_space(set1);
2880 isl_aff *aff;
2881 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2882 aff = isl_aff_add_constant_si(aff, -1);
2883 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2884 } else
2885 isl_set_free(set1);
2887 if (equal)
2888 return isl_pw_aff_zero_set(pwaff1);
2889 return isl_pw_aff_nonneg_set(pwaff1);
2892 /* Return a set containing those elements in the shared domain
2893 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2895 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2896 __isl_take isl_pw_aff *pwaff2)
2898 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2901 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2902 __isl_take isl_pw_aff *pwaff2)
2904 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2907 /* Return a set containing those elements in the shared domain
2908 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2910 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2911 __isl_take isl_pw_aff *pwaff2)
2913 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2916 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2917 __isl_take isl_pw_aff *pwaff2)
2919 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2922 /* Return a set containing those elements in the shared domain
2923 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2925 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2926 __isl_take isl_pw_aff *pwaff2)
2928 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2931 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2932 __isl_take isl_pw_aff *pwaff2)
2934 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2937 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2938 __isl_take isl_pw_aff *pwaff2)
2940 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2943 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2944 __isl_take isl_pw_aff *pwaff2)
2946 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2949 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2950 * where the function values are ordered in the same way as "order",
2951 * which returns a set in the shared domain of its two arguments.
2952 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2954 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2955 * We first pull back the two functions such that they are defined on
2956 * the domain [A -> B]. Then we apply "order", resulting in a set
2957 * in the space [A -> B]. Finally, we unwrap this set to obtain
2958 * a map in the space A -> B.
2960 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2961 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2962 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2963 __isl_take isl_pw_aff *pa2))
2965 isl_space *space1, *space2;
2966 isl_multi_aff *ma;
2967 isl_set *set;
2969 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2970 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2971 space1 = isl_space_map_from_domain_and_range(space1, space2);
2972 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2973 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
2974 ma = isl_multi_aff_range_map(space1);
2975 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
2976 set = order(pa1, pa2);
2978 return isl_set_unwrap(set);
2981 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2982 * where the function values are equal.
2983 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2985 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
2986 __isl_take isl_pw_aff *pa2)
2988 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
2991 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2992 * where the function values are equal.
2994 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
2995 __isl_take isl_pw_aff *pa2)
2997 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3000 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3001 * where the function value of "pa1" is less than the function value of "pa2".
3002 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3004 static __isl_give isl_map *isl_pw_aff_lt_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_lt_set);
3010 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3011 * where the function value of "pa1" is less than the function value of "pa2".
3013 __isl_give isl_map *isl_pw_aff_lt_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_lt_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 greater than the function value
3021 * of "pa2".
3022 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3024 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3025 __isl_take isl_pw_aff *pa2)
3027 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3030 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3031 * where the function value of "pa1" is greater than the function value
3032 * of "pa2".
3034 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3035 __isl_take isl_pw_aff *pa2)
3037 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3040 /* Return a set containing those elements in the shared domain
3041 * of the elements of list1 and list2 where each element in list1
3042 * has the relation specified by "fn" with each element in list2.
3044 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3045 __isl_take isl_pw_aff_list *list2,
3046 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3047 __isl_take isl_pw_aff *pwaff2))
3049 int i, j;
3050 isl_ctx *ctx;
3051 isl_set *set;
3053 if (!list1 || !list2)
3054 goto error;
3056 ctx = isl_pw_aff_list_get_ctx(list1);
3057 if (list1->n < 1 || list2->n < 1)
3058 isl_die(ctx, isl_error_invalid,
3059 "list should contain at least one element", goto error);
3061 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3062 for (i = 0; i < list1->n; ++i)
3063 for (j = 0; j < list2->n; ++j) {
3064 isl_set *set_ij;
3066 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3067 isl_pw_aff_copy(list2->p[j]));
3068 set = isl_set_intersect(set, set_ij);
3071 isl_pw_aff_list_free(list1);
3072 isl_pw_aff_list_free(list2);
3073 return set;
3074 error:
3075 isl_pw_aff_list_free(list1);
3076 isl_pw_aff_list_free(list2);
3077 return NULL;
3080 /* Return a set containing those elements in the shared domain
3081 * of the elements of list1 and list2 where each element in list1
3082 * is equal to each element in list2.
3084 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3085 __isl_take isl_pw_aff_list *list2)
3087 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3090 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3091 __isl_take isl_pw_aff_list *list2)
3093 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3096 /* Return a set containing those elements in the shared domain
3097 * of the elements of list1 and list2 where each element in list1
3098 * is less than or equal to each element in list2.
3100 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3101 __isl_take isl_pw_aff_list *list2)
3103 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3106 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3107 __isl_take isl_pw_aff_list *list2)
3109 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3112 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3113 __isl_take isl_pw_aff_list *list2)
3115 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3118 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3119 __isl_take isl_pw_aff_list *list2)
3121 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3125 /* Return a set containing those elements in the shared domain
3126 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3128 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3129 __isl_take isl_pw_aff *pwaff2)
3131 isl_set *set_lt, *set_gt;
3133 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3134 isl_pw_aff_copy(pwaff2));
3135 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3136 return isl_set_union_disjoint(set_lt, set_gt);
3139 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3140 __isl_take isl_pw_aff *pwaff2)
3142 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3145 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3146 isl_int v)
3148 int i;
3150 if (isl_int_is_one(v))
3151 return pwaff;
3152 if (!isl_int_is_pos(v))
3153 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3154 "factor needs to be positive",
3155 return isl_pw_aff_free(pwaff));
3156 pwaff = isl_pw_aff_cow(pwaff);
3157 if (!pwaff)
3158 return NULL;
3159 if (pwaff->n == 0)
3160 return pwaff;
3162 for (i = 0; i < pwaff->n; ++i) {
3163 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3164 if (!pwaff->p[i].aff)
3165 return isl_pw_aff_free(pwaff);
3168 return pwaff;
3171 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3173 int i;
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_floor(pwaff->p[i].aff);
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_ceil(__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_ceil(pwaff->p[i].aff);
3202 if (!pwaff->p[i].aff)
3203 return isl_pw_aff_free(pwaff);
3206 return pwaff;
3209 /* Assuming that "cond1" and "cond2" are disjoint,
3210 * return an affine expression that is equal to pwaff1 on cond1
3211 * and to pwaff2 on cond2.
3213 static __isl_give isl_pw_aff *isl_pw_aff_select(
3214 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3215 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3217 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3218 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3220 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3223 /* Return an affine expression that is equal to pwaff_true for elements
3224 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3225 * is zero.
3226 * That is, return cond ? pwaff_true : pwaff_false;
3228 * If "cond" involves and NaN, then we conservatively return a NaN
3229 * on its entire domain. In principle, we could consider the pieces
3230 * where it is NaN separately from those where it is not.
3232 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3233 * then only use the domain of "cond" to restrict the domain.
3235 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3236 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3238 isl_set *cond_true, *cond_false;
3239 isl_bool equal;
3241 if (!cond)
3242 goto error;
3243 if (isl_pw_aff_involves_nan(cond)) {
3244 isl_space *space = isl_pw_aff_get_domain_space(cond);
3245 isl_local_space *ls = isl_local_space_from_space(space);
3246 isl_pw_aff_free(cond);
3247 isl_pw_aff_free(pwaff_true);
3248 isl_pw_aff_free(pwaff_false);
3249 return isl_pw_aff_nan_on_domain(ls);
3252 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3253 isl_pw_aff_get_space(pwaff_false));
3254 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3255 isl_pw_aff_get_space(pwaff_true));
3256 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3257 if (equal < 0)
3258 goto error;
3259 if (equal) {
3260 isl_set *dom;
3262 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3263 isl_pw_aff_free(pwaff_false);
3264 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3267 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3268 cond_false = isl_pw_aff_zero_set(cond);
3269 return isl_pw_aff_select(cond_true, pwaff_true,
3270 cond_false, pwaff_false);
3271 error:
3272 isl_pw_aff_free(cond);
3273 isl_pw_aff_free(pwaff_true);
3274 isl_pw_aff_free(pwaff_false);
3275 return NULL;
3278 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3280 if (!aff)
3281 return isl_bool_error;
3283 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3286 /* Check whether pwaff is a piecewise constant.
3288 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3290 int i;
3292 if (!pwaff)
3293 return isl_bool_error;
3295 for (i = 0; i < pwaff->n; ++i) {
3296 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3297 if (is_cst < 0 || !is_cst)
3298 return is_cst;
3301 return isl_bool_true;
3304 /* Are all elements of "mpa" piecewise constants?
3306 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3308 int i;
3310 if (!mpa)
3311 return isl_bool_error;
3313 for (i = 0; i < mpa->n; ++i) {
3314 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3315 if (is_cst < 0 || !is_cst)
3316 return is_cst;
3319 return isl_bool_true;
3322 /* Return the product of "aff1" and "aff2".
3324 * If either of the two is NaN, then the result is NaN.
3326 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3328 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3329 __isl_take isl_aff *aff2)
3331 if (!aff1 || !aff2)
3332 goto error;
3334 if (isl_aff_is_nan(aff1)) {
3335 isl_aff_free(aff2);
3336 return aff1;
3338 if (isl_aff_is_nan(aff2)) {
3339 isl_aff_free(aff1);
3340 return aff2;
3343 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3344 return isl_aff_mul(aff2, aff1);
3346 if (!isl_aff_is_cst(aff2))
3347 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3348 "at least one affine expression should be constant",
3349 goto error);
3351 aff1 = isl_aff_cow(aff1);
3352 if (!aff1 || !aff2)
3353 goto error;
3355 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3356 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3358 isl_aff_free(aff2);
3359 return aff1;
3360 error:
3361 isl_aff_free(aff1);
3362 isl_aff_free(aff2);
3363 return NULL;
3366 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3368 * If either of the two is NaN, then the result is NaN.
3370 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3371 __isl_take isl_aff *aff2)
3373 int is_cst;
3374 int neg;
3376 if (!aff1 || !aff2)
3377 goto error;
3379 if (isl_aff_is_nan(aff1)) {
3380 isl_aff_free(aff2);
3381 return aff1;
3383 if (isl_aff_is_nan(aff2)) {
3384 isl_aff_free(aff1);
3385 return aff2;
3388 is_cst = isl_aff_is_cst(aff2);
3389 if (is_cst < 0)
3390 goto error;
3391 if (!is_cst)
3392 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3393 "second argument should be a constant", goto error);
3395 if (!aff2)
3396 goto error;
3398 neg = isl_int_is_neg(aff2->v->el[1]);
3399 if (neg) {
3400 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3401 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3404 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3405 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3407 if (neg) {
3408 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3409 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3412 isl_aff_free(aff2);
3413 return aff1;
3414 error:
3415 isl_aff_free(aff1);
3416 isl_aff_free(aff2);
3417 return NULL;
3420 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3421 __isl_take isl_pw_aff *pwaff2)
3423 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3426 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3427 __isl_take isl_pw_aff *pwaff2)
3429 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3432 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3433 __isl_take isl_pw_aff *pwaff2)
3435 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3438 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3439 __isl_take isl_pw_aff *pwaff2)
3441 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3444 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3445 __isl_take isl_pw_aff *pwaff2)
3447 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3450 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3451 __isl_take isl_pw_aff *pa2)
3453 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3456 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3458 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3459 __isl_take isl_pw_aff *pa2)
3461 int is_cst;
3463 is_cst = isl_pw_aff_is_cst(pa2);
3464 if (is_cst < 0)
3465 goto error;
3466 if (!is_cst)
3467 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3468 "second argument should be a piecewise constant",
3469 goto error);
3470 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3471 error:
3472 isl_pw_aff_free(pa1);
3473 isl_pw_aff_free(pa2);
3474 return NULL;
3477 /* Compute the quotient of the integer division of "pa1" by "pa2"
3478 * with rounding towards zero.
3479 * "pa2" is assumed to be a piecewise constant.
3481 * In particular, return
3483 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3486 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3487 __isl_take isl_pw_aff *pa2)
3489 int is_cst;
3490 isl_set *cond;
3491 isl_pw_aff *f, *c;
3493 is_cst = isl_pw_aff_is_cst(pa2);
3494 if (is_cst < 0)
3495 goto error;
3496 if (!is_cst)
3497 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3498 "second argument should be a piecewise constant",
3499 goto error);
3501 pa1 = isl_pw_aff_div(pa1, pa2);
3503 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3504 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3505 c = isl_pw_aff_ceil(pa1);
3506 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3507 error:
3508 isl_pw_aff_free(pa1);
3509 isl_pw_aff_free(pa2);
3510 return NULL;
3513 /* Compute the remainder of the integer division of "pa1" by "pa2"
3514 * with rounding towards zero.
3515 * "pa2" is assumed to be a piecewise constant.
3517 * In particular, return
3519 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3522 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3523 __isl_take isl_pw_aff *pa2)
3525 int is_cst;
3526 isl_pw_aff *res;
3528 is_cst = isl_pw_aff_is_cst(pa2);
3529 if (is_cst < 0)
3530 goto error;
3531 if (!is_cst)
3532 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3533 "second argument should be a piecewise constant",
3534 goto error);
3535 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3536 res = isl_pw_aff_mul(pa2, res);
3537 res = isl_pw_aff_sub(pa1, res);
3538 return res;
3539 error:
3540 isl_pw_aff_free(pa1);
3541 isl_pw_aff_free(pa2);
3542 return NULL;
3545 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3546 __isl_take isl_pw_aff *pwaff2)
3548 isl_set *le;
3549 isl_set *dom;
3551 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3552 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3553 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3554 isl_pw_aff_copy(pwaff2));
3555 dom = isl_set_subtract(dom, isl_set_copy(le));
3556 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3559 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3560 __isl_take isl_pw_aff *pwaff2)
3562 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_min);
3565 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3566 __isl_take isl_pw_aff *pwaff2)
3568 isl_set *ge;
3569 isl_set *dom;
3571 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3572 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3573 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3574 isl_pw_aff_copy(pwaff2));
3575 dom = isl_set_subtract(dom, isl_set_copy(ge));
3576 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3579 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3580 __isl_take isl_pw_aff *pwaff2)
3582 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_max);
3585 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3586 __isl_take isl_pw_aff_list *list,
3587 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3588 __isl_take isl_pw_aff *pwaff2))
3590 int i;
3591 isl_ctx *ctx;
3592 isl_pw_aff *res;
3594 if (!list)
3595 return NULL;
3597 ctx = isl_pw_aff_list_get_ctx(list);
3598 if (list->n < 1)
3599 isl_die(ctx, isl_error_invalid,
3600 "list should contain at least one element", goto error);
3602 res = isl_pw_aff_copy(list->p[0]);
3603 for (i = 1; i < list->n; ++i)
3604 res = fn(res, isl_pw_aff_copy(list->p[i]));
3606 isl_pw_aff_list_free(list);
3607 return res;
3608 error:
3609 isl_pw_aff_list_free(list);
3610 return NULL;
3613 /* Return an isl_pw_aff that maps each element in the intersection of the
3614 * domains of the elements of list to the minimal corresponding affine
3615 * expression.
3617 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3619 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3622 /* Return an isl_pw_aff that maps each element in the intersection of the
3623 * domains of the elements of list to the maximal corresponding affine
3624 * expression.
3626 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3628 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3631 /* Mark the domains of "pwaff" as rational.
3633 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3635 int i;
3637 pwaff = isl_pw_aff_cow(pwaff);
3638 if (!pwaff)
3639 return NULL;
3640 if (pwaff->n == 0)
3641 return pwaff;
3643 for (i = 0; i < pwaff->n; ++i) {
3644 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3645 if (!pwaff->p[i].set)
3646 return isl_pw_aff_free(pwaff);
3649 return pwaff;
3652 /* Mark the domains of the elements of "list" as rational.
3654 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3655 __isl_take isl_pw_aff_list *list)
3657 int i, n;
3659 if (!list)
3660 return NULL;
3661 if (list->n == 0)
3662 return list;
3664 n = list->n;
3665 for (i = 0; i < n; ++i) {
3666 isl_pw_aff *pa;
3668 pa = isl_pw_aff_list_get_pw_aff(list, i);
3669 pa = isl_pw_aff_set_rational(pa);
3670 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3673 return list;
3676 /* Do the parameters of "aff" match those of "space"?
3678 int isl_aff_matching_params(__isl_keep isl_aff *aff,
3679 __isl_keep isl_space *space)
3681 isl_space *aff_space;
3682 int match;
3684 if (!aff || !space)
3685 return -1;
3687 aff_space = isl_aff_get_domain_space(aff);
3689 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3691 isl_space_free(aff_space);
3692 return match;
3695 /* Check that the domain space of "aff" matches "space".
3697 * Return 0 on success and -1 on error.
3699 int isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3700 __isl_keep isl_space *space)
3702 isl_space *aff_space;
3703 int match;
3705 if (!aff || !space)
3706 return -1;
3708 aff_space = isl_aff_get_domain_space(aff);
3710 match = isl_space_match(space, isl_dim_param, aff_space, isl_dim_param);
3711 if (match < 0)
3712 goto error;
3713 if (!match)
3714 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3715 "parameters don't match", goto error);
3716 match = isl_space_tuple_is_equal(space, isl_dim_in,
3717 aff_space, isl_dim_set);
3718 if (match < 0)
3719 goto error;
3720 if (!match)
3721 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3722 "domains don't match", goto error);
3723 isl_space_free(aff_space);
3724 return 0;
3725 error:
3726 isl_space_free(aff_space);
3727 return -1;
3730 #undef BASE
3731 #define BASE aff
3732 #undef DOMBASE
3733 #define DOMBASE set
3734 #define NO_DOMAIN
3736 #include <isl_multi_templ.c>
3737 #include <isl_multi_apply_set.c>
3738 #include <isl_multi_cmp.c>
3739 #include <isl_multi_floor.c>
3740 #include <isl_multi_gist.c>
3742 #undef NO_DOMAIN
3744 /* Remove any internal structure of the domain of "ma".
3745 * If there is any such internal structure in the input,
3746 * then the name of the corresponding space is also removed.
3748 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3749 __isl_take isl_multi_aff *ma)
3751 isl_space *space;
3753 if (!ma)
3754 return NULL;
3756 if (!ma->space->nested[0])
3757 return ma;
3759 space = isl_multi_aff_get_space(ma);
3760 space = isl_space_flatten_domain(space);
3761 ma = isl_multi_aff_reset_space(ma, space);
3763 return ma;
3766 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3767 * of the space to its domain.
3769 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3771 int i, n_in;
3772 isl_local_space *ls;
3773 isl_multi_aff *ma;
3775 if (!space)
3776 return NULL;
3777 if (!isl_space_is_map(space))
3778 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3779 "not a map space", goto error);
3781 n_in = isl_space_dim(space, isl_dim_in);
3782 space = isl_space_domain_map(space);
3784 ma = isl_multi_aff_alloc(isl_space_copy(space));
3785 if (n_in == 0) {
3786 isl_space_free(space);
3787 return ma;
3790 space = isl_space_domain(space);
3791 ls = isl_local_space_from_space(space);
3792 for (i = 0; i < n_in; ++i) {
3793 isl_aff *aff;
3795 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3796 isl_dim_set, i);
3797 ma = isl_multi_aff_set_aff(ma, i, aff);
3799 isl_local_space_free(ls);
3800 return ma;
3801 error:
3802 isl_space_free(space);
3803 return NULL;
3806 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3807 * of the space to its range.
3809 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3811 int i, n_in, n_out;
3812 isl_local_space *ls;
3813 isl_multi_aff *ma;
3815 if (!space)
3816 return NULL;
3817 if (!isl_space_is_map(space))
3818 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3819 "not a map space", goto error);
3821 n_in = isl_space_dim(space, isl_dim_in);
3822 n_out = isl_space_dim(space, isl_dim_out);
3823 space = isl_space_range_map(space);
3825 ma = isl_multi_aff_alloc(isl_space_copy(space));
3826 if (n_out == 0) {
3827 isl_space_free(space);
3828 return ma;
3831 space = isl_space_domain(space);
3832 ls = isl_local_space_from_space(space);
3833 for (i = 0; i < n_out; ++i) {
3834 isl_aff *aff;
3836 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3837 isl_dim_set, n_in + i);
3838 ma = isl_multi_aff_set_aff(ma, i, aff);
3840 isl_local_space_free(ls);
3841 return ma;
3842 error:
3843 isl_space_free(space);
3844 return NULL;
3847 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
3848 * of the space to its range.
3850 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
3851 __isl_take isl_space *space)
3853 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
3856 /* Given the space of a set and a range of set dimensions,
3857 * construct an isl_multi_aff that projects out those dimensions.
3859 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
3860 __isl_take isl_space *space, enum isl_dim_type type,
3861 unsigned first, unsigned n)
3863 int i, dim;
3864 isl_local_space *ls;
3865 isl_multi_aff *ma;
3867 if (!space)
3868 return NULL;
3869 if (!isl_space_is_set(space))
3870 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
3871 "expecting set space", goto error);
3872 if (type != isl_dim_set)
3873 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3874 "only set dimensions can be projected out", goto error);
3876 dim = isl_space_dim(space, isl_dim_set);
3877 if (first + n > dim)
3878 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3879 "range out of bounds", goto error);
3881 space = isl_space_from_domain(space);
3882 space = isl_space_add_dims(space, isl_dim_out, dim - n);
3884 if (dim == n)
3885 return isl_multi_aff_alloc(space);
3887 ma = isl_multi_aff_alloc(isl_space_copy(space));
3888 space = isl_space_domain(space);
3889 ls = isl_local_space_from_space(space);
3891 for (i = 0; i < first; ++i) {
3892 isl_aff *aff;
3894 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3895 isl_dim_set, i);
3896 ma = isl_multi_aff_set_aff(ma, i, aff);
3899 for (i = 0; i < dim - (first + n); ++i) {
3900 isl_aff *aff;
3902 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3903 isl_dim_set, first + n + i);
3904 ma = isl_multi_aff_set_aff(ma, first + i, aff);
3907 isl_local_space_free(ls);
3908 return ma;
3909 error:
3910 isl_space_free(space);
3911 return NULL;
3914 /* Given the space of a set and a range of set dimensions,
3915 * construct an isl_pw_multi_aff that projects out those dimensions.
3917 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
3918 __isl_take isl_space *space, enum isl_dim_type type,
3919 unsigned first, unsigned n)
3921 isl_multi_aff *ma;
3923 ma = isl_multi_aff_project_out_map(space, type, first, n);
3924 return isl_pw_multi_aff_from_multi_aff(ma);
3927 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
3928 * domain.
3930 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
3931 __isl_take isl_multi_aff *ma)
3933 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
3934 return isl_pw_multi_aff_alloc(dom, ma);
3937 /* Create a piecewise multi-affine expression in the given space that maps each
3938 * input dimension to the corresponding output dimension.
3940 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
3941 __isl_take isl_space *space)
3943 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
3946 /* Exploit the equalities in "eq" to simplify the affine expressions.
3948 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
3949 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
3951 int i;
3953 maff = isl_multi_aff_cow(maff);
3954 if (!maff || !eq)
3955 goto error;
3957 for (i = 0; i < maff->n; ++i) {
3958 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
3959 isl_basic_set_copy(eq));
3960 if (!maff->p[i])
3961 goto error;
3964 isl_basic_set_free(eq);
3965 return maff;
3966 error:
3967 isl_basic_set_free(eq);
3968 isl_multi_aff_free(maff);
3969 return NULL;
3972 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
3973 isl_int f)
3975 int i;
3977 maff = isl_multi_aff_cow(maff);
3978 if (!maff)
3979 return NULL;
3981 for (i = 0; i < maff->n; ++i) {
3982 maff->p[i] = isl_aff_scale(maff->p[i], f);
3983 if (!maff->p[i])
3984 return isl_multi_aff_free(maff);
3987 return maff;
3990 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
3991 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
3993 maff1 = isl_multi_aff_add(maff1, maff2);
3994 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
3995 return maff1;
3998 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4000 if (!maff)
4001 return -1;
4003 return 0;
4006 /* Return the set of domain elements where "ma1" is lexicographically
4007 * smaller than or equal to "ma2".
4009 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4010 __isl_take isl_multi_aff *ma2)
4012 return isl_multi_aff_lex_ge_set(ma2, ma1);
4015 /* Return the set of domain elements where "ma1" is lexicographically
4016 * smaller than "ma2".
4018 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4019 __isl_take isl_multi_aff *ma2)
4021 return isl_multi_aff_lex_gt_set(ma2, ma1);
4024 /* Return the set of domain elements where "ma1" and "ma2"
4025 * satisfy "order".
4027 static __isl_give isl_set *isl_multi_aff_order_set(
4028 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4029 __isl_give isl_map *order(__isl_take isl_space *set_space))
4031 isl_space *space;
4032 isl_map *map1, *map2;
4033 isl_map *map, *ge;
4035 map1 = isl_map_from_multi_aff(ma1);
4036 map2 = isl_map_from_multi_aff(ma2);
4037 map = isl_map_range_product(map1, map2);
4038 space = isl_space_range(isl_map_get_space(map));
4039 space = isl_space_domain(isl_space_unwrap(space));
4040 ge = order(space);
4041 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4043 return isl_map_domain(map);
4046 /* Return the set of domain elements where "ma1" is lexicographically
4047 * greater than or equal to "ma2".
4049 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4050 __isl_take isl_multi_aff *ma2)
4052 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4055 /* Return the set of domain elements where "ma1" is lexicographically
4056 * greater than "ma2".
4058 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4059 __isl_take isl_multi_aff *ma2)
4061 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4064 #undef PW
4065 #define PW isl_pw_multi_aff
4066 #undef EL
4067 #define EL isl_multi_aff
4068 #undef EL_IS_ZERO
4069 #define EL_IS_ZERO is_empty
4070 #undef ZERO
4071 #define ZERO empty
4072 #undef IS_ZERO
4073 #define IS_ZERO is_empty
4074 #undef FIELD
4075 #define FIELD maff
4076 #undef DEFAULT_IS_ZERO
4077 #define DEFAULT_IS_ZERO 0
4079 #define NO_SUB
4080 #define NO_EVAL
4081 #define NO_OPT
4082 #define NO_INVOLVES_DIMS
4083 #define NO_INSERT_DIMS
4084 #define NO_LIFT
4085 #define NO_MORPH
4087 #include <isl_pw_templ.c>
4088 #include <isl_pw_union_opt.c>
4090 #undef NO_SUB
4092 #undef UNION
4093 #define UNION isl_union_pw_multi_aff
4094 #undef PART
4095 #define PART isl_pw_multi_aff
4096 #undef PARTS
4097 #define PARTS pw_multi_aff
4099 #include <isl_union_multi.c>
4100 #include <isl_union_neg.c>
4102 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4103 __isl_take isl_pw_multi_aff *pma1,
4104 __isl_take isl_pw_multi_aff *pma2)
4106 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4107 &isl_multi_aff_lex_ge_set);
4110 /* Given two piecewise multi affine expressions, return a piecewise
4111 * multi-affine expression defined on the union of the definition domains
4112 * of the inputs that is equal to the lexicographic maximum of the two
4113 * inputs on each cell. If only one of the two inputs is defined on
4114 * a given cell, then it is considered to be the maximum.
4116 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4117 __isl_take isl_pw_multi_aff *pma1,
4118 __isl_take isl_pw_multi_aff *pma2)
4120 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4121 &pw_multi_aff_union_lexmax);
4124 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4125 __isl_take isl_pw_multi_aff *pma1,
4126 __isl_take isl_pw_multi_aff *pma2)
4128 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4129 &isl_multi_aff_lex_le_set);
4132 /* Given two piecewise multi affine expressions, return a piecewise
4133 * multi-affine expression defined on the union of the definition domains
4134 * of the inputs that is equal to the lexicographic minimum of the two
4135 * inputs on each cell. If only one of the two inputs is defined on
4136 * a given cell, then it is considered to be the minimum.
4138 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4139 __isl_take isl_pw_multi_aff *pma1,
4140 __isl_take isl_pw_multi_aff *pma2)
4142 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4143 &pw_multi_aff_union_lexmin);
4146 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4147 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4149 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4150 &isl_multi_aff_add);
4153 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4154 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4156 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4157 &pw_multi_aff_add);
4160 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4161 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4163 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4164 &isl_multi_aff_sub);
4167 /* Subtract "pma2" from "pma1" and return the result.
4169 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4170 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4172 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4173 &pw_multi_aff_sub);
4176 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4177 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4179 return isl_pw_multi_aff_union_add_(pma1, pma2);
4182 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4183 * with the actual sum on the shared domain and
4184 * the defined expression on the symmetric difference of the domains.
4186 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4187 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4189 return isl_union_pw_aff_union_add_(upa1, upa2);
4192 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4193 * with the actual sum on the shared domain and
4194 * the defined expression on the symmetric difference of the domains.
4196 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4197 __isl_take isl_union_pw_multi_aff *upma1,
4198 __isl_take isl_union_pw_multi_aff *upma2)
4200 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4203 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4204 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4206 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4207 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4209 int i, j, n;
4210 isl_space *space;
4211 isl_pw_multi_aff *res;
4213 if (!pma1 || !pma2)
4214 goto error;
4216 n = pma1->n * pma2->n;
4217 space = isl_space_product(isl_space_copy(pma1->dim),
4218 isl_space_copy(pma2->dim));
4219 res = isl_pw_multi_aff_alloc_size(space, n);
4221 for (i = 0; i < pma1->n; ++i) {
4222 for (j = 0; j < pma2->n; ++j) {
4223 isl_set *domain;
4224 isl_multi_aff *ma;
4226 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4227 isl_set_copy(pma2->p[j].set));
4228 ma = isl_multi_aff_product(
4229 isl_multi_aff_copy(pma1->p[i].maff),
4230 isl_multi_aff_copy(pma2->p[j].maff));
4231 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4235 isl_pw_multi_aff_free(pma1);
4236 isl_pw_multi_aff_free(pma2);
4237 return res;
4238 error:
4239 isl_pw_multi_aff_free(pma1);
4240 isl_pw_multi_aff_free(pma2);
4241 return NULL;
4244 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4245 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4247 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4248 &pw_multi_aff_product);
4251 /* Construct a map mapping the domain of the piecewise multi-affine expression
4252 * to its range, with each dimension in the range equated to the
4253 * corresponding affine expression on its cell.
4255 * If the domain of "pma" is rational, then so is the constructed "map".
4257 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4259 int i;
4260 isl_map *map;
4262 if (!pma)
4263 return NULL;
4265 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4267 for (i = 0; i < pma->n; ++i) {
4268 isl_bool rational;
4269 isl_multi_aff *maff;
4270 isl_basic_map *bmap;
4271 isl_map *map_i;
4273 rational = isl_set_is_rational(pma->p[i].set);
4274 if (rational < 0)
4275 map = isl_map_free(map);
4276 maff = isl_multi_aff_copy(pma->p[i].maff);
4277 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4278 map_i = isl_map_from_basic_map(bmap);
4279 map_i = isl_map_intersect_domain(map_i,
4280 isl_set_copy(pma->p[i].set));
4281 map = isl_map_union_disjoint(map, map_i);
4284 isl_pw_multi_aff_free(pma);
4285 return map;
4288 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4290 if (!pma)
4291 return NULL;
4293 if (!isl_space_is_set(pma->dim))
4294 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4295 "isl_pw_multi_aff cannot be converted into an isl_set",
4296 goto error);
4298 return isl_map_from_pw_multi_aff(pma);
4299 error:
4300 isl_pw_multi_aff_free(pma);
4301 return NULL;
4304 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4305 * denominator "denom".
4306 * "denom" is allowed to be negative, in which case the actual denominator
4307 * is -denom and the expressions are added instead.
4309 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4310 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4312 int i, first;
4313 int sign;
4314 isl_int d;
4316 first = isl_seq_first_non_zero(c, n);
4317 if (first == -1)
4318 return aff;
4320 sign = isl_int_sgn(denom);
4321 isl_int_init(d);
4322 isl_int_abs(d, denom);
4323 for (i = first; i < n; ++i) {
4324 isl_aff *aff_i;
4326 if (isl_int_is_zero(c[i]))
4327 continue;
4328 aff_i = isl_multi_aff_get_aff(ma, i);
4329 aff_i = isl_aff_scale(aff_i, c[i]);
4330 aff_i = isl_aff_scale_down(aff_i, d);
4331 if (sign >= 0)
4332 aff = isl_aff_sub(aff, aff_i);
4333 else
4334 aff = isl_aff_add(aff, aff_i);
4336 isl_int_clear(d);
4338 return aff;
4341 /* Extract an affine expression that expresses the output dimension "pos"
4342 * of "bmap" in terms of the parameters and input dimensions from
4343 * equality "eq".
4344 * Note that this expression may involve integer divisions defined
4345 * in terms of parameters and input dimensions.
4346 * The equality may also involve references to earlier (but not later)
4347 * output dimensions. These are replaced by the corresponding elements
4348 * in "ma".
4350 * If the equality is of the form
4352 * f(i) + h(j) + a x + g(i) = 0,
4354 * with f(i) a linear combinations of the parameters and input dimensions,
4355 * g(i) a linear combination of integer divisions defined in terms of the same
4356 * and h(j) a linear combinations of earlier output dimensions,
4357 * then the affine expression is
4359 * (-f(i) - g(i))/a - h(j)/a
4361 * If the equality is of the form
4363 * f(i) + h(j) - a x + g(i) = 0,
4365 * then the affine expression is
4367 * (f(i) + g(i))/a - h(j)/(-a)
4370 * If "div" refers to an integer division (i.e., it is smaller than
4371 * the number of integer divisions), then the equality constraint
4372 * does involve an integer division (the one at position "div") that
4373 * is defined in terms of output dimensions. However, this integer
4374 * division can be eliminated by exploiting a pair of constraints
4375 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4376 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4377 * -l + x >= 0.
4378 * In particular, let
4380 * x = e(i) + m floor(...)
4382 * with e(i) the expression derived above and floor(...) the integer
4383 * division involving output dimensions.
4384 * From
4386 * l <= x <= l + n,
4388 * we have
4390 * 0 <= x - l <= n
4392 * This means
4394 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4395 * = (e(i) - l) mod m
4397 * Therefore,
4399 * x - l = (e(i) - l) mod m
4401 * or
4403 * x = ((e(i) - l) mod m) + l
4405 * The variable "shift" below contains the expression -l, which may
4406 * also involve a linear combination of earlier output dimensions.
4408 static __isl_give isl_aff *extract_aff_from_equality(
4409 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4410 __isl_keep isl_multi_aff *ma)
4412 unsigned o_out;
4413 unsigned n_div, n_out;
4414 isl_ctx *ctx;
4415 isl_local_space *ls;
4416 isl_aff *aff, *shift;
4417 isl_val *mod;
4419 ctx = isl_basic_map_get_ctx(bmap);
4420 ls = isl_basic_map_get_local_space(bmap);
4421 ls = isl_local_space_domain(ls);
4422 aff = isl_aff_alloc(isl_local_space_copy(ls));
4423 if (!aff)
4424 goto error;
4425 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4426 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4427 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4428 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4429 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4430 isl_seq_cpy(aff->v->el + 1 + o_out,
4431 bmap->eq[eq] + o_out + n_out, n_div);
4432 } else {
4433 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4434 isl_seq_neg(aff->v->el + 1 + o_out,
4435 bmap->eq[eq] + o_out + n_out, n_div);
4437 if (div < n_div)
4438 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4439 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4440 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4441 bmap->eq[eq][o_out + pos]);
4442 if (div < n_div) {
4443 shift = isl_aff_alloc(isl_local_space_copy(ls));
4444 if (!shift)
4445 goto error;
4446 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4447 isl_seq_cpy(shift->v->el + 1 + o_out,
4448 bmap->ineq[ineq] + o_out + n_out, n_div);
4449 isl_int_set_si(shift->v->el[0], 1);
4450 shift = subtract_initial(shift, ma, pos,
4451 bmap->ineq[ineq] + o_out, ctx->negone);
4452 aff = isl_aff_add(aff, isl_aff_copy(shift));
4453 mod = isl_val_int_from_isl_int(ctx,
4454 bmap->eq[eq][o_out + n_out + div]);
4455 mod = isl_val_abs(mod);
4456 aff = isl_aff_mod_val(aff, mod);
4457 aff = isl_aff_sub(aff, shift);
4460 isl_local_space_free(ls);
4461 return aff;
4462 error:
4463 isl_local_space_free(ls);
4464 isl_aff_free(aff);
4465 return NULL;
4468 /* Given a basic map with output dimensions defined
4469 * in terms of the parameters input dimensions and earlier
4470 * output dimensions using an equality (and possibly a pair on inequalities),
4471 * extract an isl_aff that expresses output dimension "pos" in terms
4472 * of the parameters and input dimensions.
4473 * Note that this expression may involve integer divisions defined
4474 * in terms of parameters and input dimensions.
4475 * "ma" contains the expressions corresponding to earlier output dimensions.
4477 * This function shares some similarities with
4478 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4480 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4481 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4483 int eq, div, ineq;
4484 isl_aff *aff;
4486 if (!bmap)
4487 return NULL;
4488 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4489 if (eq >= bmap->n_eq)
4490 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4491 "unable to find suitable equality", return NULL);
4492 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4494 aff = isl_aff_remove_unused_divs(aff);
4495 return aff;
4498 /* Given a basic map where each output dimension is defined
4499 * in terms of the parameters and input dimensions using an equality,
4500 * extract an isl_multi_aff that expresses the output dimensions in terms
4501 * of the parameters and input dimensions.
4503 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4504 __isl_take isl_basic_map *bmap)
4506 int i;
4507 unsigned n_out;
4508 isl_multi_aff *ma;
4510 if (!bmap)
4511 return NULL;
4513 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4514 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4516 for (i = 0; i < n_out; ++i) {
4517 isl_aff *aff;
4519 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4520 ma = isl_multi_aff_set_aff(ma, i, aff);
4523 isl_basic_map_free(bmap);
4525 return ma;
4528 /* Given a basic set where each set dimension is defined
4529 * in terms of the parameters using an equality,
4530 * extract an isl_multi_aff that expresses the set dimensions in terms
4531 * of the parameters.
4533 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4534 __isl_take isl_basic_set *bset)
4536 return extract_isl_multi_aff_from_basic_map(bset);
4539 /* Create an isl_pw_multi_aff that is equivalent to
4540 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4541 * The given basic map is such that each output dimension is defined
4542 * in terms of the parameters and input dimensions using an equality.
4544 * Since some applications expect the result of isl_pw_multi_aff_from_map
4545 * to only contain integer affine expressions, we compute the floor
4546 * of the expression before returning.
4548 * Remove all constraints involving local variables without
4549 * an explicit representation (resulting in the removal of those
4550 * local variables) prior to the actual extraction to ensure
4551 * that the local spaces in which the resulting affine expressions
4552 * are created do not contain any unknown local variables.
4553 * Removing such constraints is safe because constraints involving
4554 * unknown local variables are not used to determine whether
4555 * a basic map is obviously single-valued.
4557 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4558 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4560 isl_multi_aff *ma;
4562 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4563 ma = extract_isl_multi_aff_from_basic_map(bmap);
4564 ma = isl_multi_aff_floor(ma);
4565 return isl_pw_multi_aff_alloc(domain, ma);
4568 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4569 * This obviously only works if the input "map" is single-valued.
4570 * If so, we compute the lexicographic minimum of the image in the form
4571 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4572 * to its lexicographic minimum.
4573 * If the input is not single-valued, we produce an error.
4575 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4576 __isl_take isl_map *map)
4578 int i;
4579 int sv;
4580 isl_pw_multi_aff *pma;
4582 sv = isl_map_is_single_valued(map);
4583 if (sv < 0)
4584 goto error;
4585 if (!sv)
4586 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4587 "map is not single-valued", goto error);
4588 map = isl_map_make_disjoint(map);
4589 if (!map)
4590 return NULL;
4592 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4594 for (i = 0; i < map->n; ++i) {
4595 isl_pw_multi_aff *pma_i;
4596 isl_basic_map *bmap;
4597 bmap = isl_basic_map_copy(map->p[i]);
4598 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4599 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4602 isl_map_free(map);
4603 return pma;
4604 error:
4605 isl_map_free(map);
4606 return NULL;
4609 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4610 * taking into account that the output dimension at position "d"
4611 * can be represented as
4613 * x = floor((e(...) + c1) / m)
4615 * given that constraint "i" is of the form
4617 * e(...) + c1 - m x >= 0
4620 * Let "map" be of the form
4622 * A -> B
4624 * We construct a mapping
4626 * A -> [A -> x = floor(...)]
4628 * apply that to the map, obtaining
4630 * [A -> x = floor(...)] -> B
4632 * and equate dimension "d" to x.
4633 * We then compute a isl_pw_multi_aff representation of the resulting map
4634 * and plug in the mapping above.
4636 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4637 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4639 isl_ctx *ctx;
4640 isl_space *space;
4641 isl_local_space *ls;
4642 isl_multi_aff *ma;
4643 isl_aff *aff;
4644 isl_vec *v;
4645 isl_map *insert;
4646 int offset;
4647 int n;
4648 int n_in;
4649 isl_pw_multi_aff *pma;
4650 int is_set;
4652 is_set = isl_map_is_set(map);
4654 offset = isl_basic_map_offset(hull, isl_dim_out);
4655 ctx = isl_map_get_ctx(map);
4656 space = isl_space_domain(isl_map_get_space(map));
4657 n_in = isl_space_dim(space, isl_dim_set);
4658 n = isl_space_dim(space, isl_dim_all);
4660 v = isl_vec_alloc(ctx, 1 + 1 + n);
4661 if (v) {
4662 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4663 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4665 isl_basic_map_free(hull);
4667 ls = isl_local_space_from_space(isl_space_copy(space));
4668 aff = isl_aff_alloc_vec(ls, v);
4669 aff = isl_aff_floor(aff);
4670 if (is_set) {
4671 isl_space_free(space);
4672 ma = isl_multi_aff_from_aff(aff);
4673 } else {
4674 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4675 ma = isl_multi_aff_range_product(ma,
4676 isl_multi_aff_from_aff(aff));
4679 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4680 map = isl_map_apply_domain(map, insert);
4681 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4682 pma = isl_pw_multi_aff_from_map(map);
4683 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4685 return pma;
4688 /* Is constraint "c" of the form
4690 * e(...) + c1 - m x >= 0
4692 * or
4694 * -e(...) + c2 + m x >= 0
4696 * where m > 1 and e only depends on parameters and input dimemnsions?
4698 * "offset" is the offset of the output dimensions
4699 * "pos" is the position of output dimension x.
4701 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4703 if (isl_int_is_zero(c[offset + d]))
4704 return 0;
4705 if (isl_int_is_one(c[offset + d]))
4706 return 0;
4707 if (isl_int_is_negone(c[offset + d]))
4708 return 0;
4709 if (isl_seq_first_non_zero(c + offset, d) != -1)
4710 return 0;
4711 if (isl_seq_first_non_zero(c + offset + d + 1,
4712 total - (offset + d + 1)) != -1)
4713 return 0;
4714 return 1;
4717 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4719 * As a special case, we first check if there is any pair of constraints,
4720 * shared by all the basic maps in "map" that force a given dimension
4721 * to be equal to the floor of some affine combination of the input dimensions.
4723 * In particular, if we can find two constraints
4725 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4727 * and
4729 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4731 * where m > 1 and e only depends on parameters and input dimemnsions,
4732 * and such that
4734 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4736 * then we know that we can take
4738 * x = floor((e(...) + c1) / m)
4740 * without having to perform any computation.
4742 * Note that we know that
4744 * c1 + c2 >= 1
4746 * If c1 + c2 were 0, then we would have detected an equality during
4747 * simplification. If c1 + c2 were negative, then we would have detected
4748 * a contradiction.
4750 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4751 __isl_take isl_map *map)
4753 int d, dim;
4754 int i, j, n;
4755 int offset, total;
4756 isl_int sum;
4757 isl_basic_map *hull;
4759 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4760 if (!hull)
4761 goto error;
4763 isl_int_init(sum);
4764 dim = isl_map_dim(map, isl_dim_out);
4765 offset = isl_basic_map_offset(hull, isl_dim_out);
4766 total = 1 + isl_basic_map_total_dim(hull);
4767 n = hull->n_ineq;
4768 for (d = 0; d < dim; ++d) {
4769 for (i = 0; i < n; ++i) {
4770 if (!is_potential_div_constraint(hull->ineq[i],
4771 offset, d, total))
4772 continue;
4773 for (j = i + 1; j < n; ++j) {
4774 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4775 hull->ineq[j] + 1, total - 1))
4776 continue;
4777 isl_int_add(sum, hull->ineq[i][0],
4778 hull->ineq[j][0]);
4779 if (isl_int_abs_lt(sum,
4780 hull->ineq[i][offset + d]))
4781 break;
4784 if (j >= n)
4785 continue;
4786 isl_int_clear(sum);
4787 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4788 j = i;
4789 return pw_multi_aff_from_map_div(map, hull, d, j);
4792 isl_int_clear(sum);
4793 isl_basic_map_free(hull);
4794 return pw_multi_aff_from_map_base(map);
4795 error:
4796 isl_map_free(map);
4797 isl_basic_map_free(hull);
4798 return NULL;
4801 /* Given an affine expression
4803 * [A -> B] -> f(A,B)
4805 * construct an isl_multi_aff
4807 * [A -> B] -> B'
4809 * such that dimension "d" in B' is set to "aff" and the remaining
4810 * dimensions are set equal to the corresponding dimensions in B.
4811 * "n_in" is the dimension of the space A.
4812 * "n_out" is the dimension of the space B.
4814 * If "is_set" is set, then the affine expression is of the form
4816 * [B] -> f(B)
4818 * and we construct an isl_multi_aff
4820 * B -> B'
4822 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4823 unsigned n_in, unsigned n_out, int is_set)
4825 int i;
4826 isl_multi_aff *ma;
4827 isl_space *space, *space2;
4828 isl_local_space *ls;
4830 space = isl_aff_get_domain_space(aff);
4831 ls = isl_local_space_from_space(isl_space_copy(space));
4832 space2 = isl_space_copy(space);
4833 if (!is_set)
4834 space2 = isl_space_range(isl_space_unwrap(space2));
4835 space = isl_space_map_from_domain_and_range(space, space2);
4836 ma = isl_multi_aff_alloc(space);
4837 ma = isl_multi_aff_set_aff(ma, d, aff);
4839 for (i = 0; i < n_out; ++i) {
4840 if (i == d)
4841 continue;
4842 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4843 isl_dim_set, n_in + i);
4844 ma = isl_multi_aff_set_aff(ma, i, aff);
4847 isl_local_space_free(ls);
4849 return ma;
4852 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4853 * taking into account that the dimension at position "d" can be written as
4855 * x = m a + f(..) (1)
4857 * where m is equal to "gcd".
4858 * "i" is the index of the equality in "hull" that defines f(..).
4859 * In particular, the equality is of the form
4861 * f(..) - x + m g(existentials) = 0
4863 * or
4865 * -f(..) + x + m g(existentials) = 0
4867 * We basically plug (1) into "map", resulting in a map with "a"
4868 * in the range instead of "x". The corresponding isl_pw_multi_aff
4869 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4871 * Specifically, given the input map
4873 * A -> B
4875 * We first wrap it into a set
4877 * [A -> B]
4879 * and define (1) on top of the corresponding space, resulting in "aff".
4880 * We use this to create an isl_multi_aff that maps the output position "d"
4881 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4882 * We plug this into the wrapped map, unwrap the result and compute the
4883 * corresponding isl_pw_multi_aff.
4884 * The result is an expression
4886 * A -> T(A)
4888 * We adjust that to
4890 * A -> [A -> T(A)]
4892 * so that we can plug that into "aff", after extending the latter to
4893 * a mapping
4895 * [A -> B] -> B'
4898 * If "map" is actually a set, then there is no "A" space, meaning
4899 * that we do not need to perform any wrapping, and that the result
4900 * of the recursive call is of the form
4902 * [T]
4904 * which is plugged into a mapping of the form
4906 * B -> B'
4908 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
4909 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
4910 isl_int gcd)
4912 isl_set *set;
4913 isl_space *space;
4914 isl_local_space *ls;
4915 isl_aff *aff;
4916 isl_multi_aff *ma;
4917 isl_pw_multi_aff *pma, *id;
4918 unsigned n_in;
4919 unsigned o_out;
4920 unsigned n_out;
4921 int is_set;
4923 is_set = isl_map_is_set(map);
4925 n_in = isl_basic_map_dim(hull, isl_dim_in);
4926 n_out = isl_basic_map_dim(hull, isl_dim_out);
4927 o_out = isl_basic_map_offset(hull, isl_dim_out);
4929 if (is_set)
4930 set = map;
4931 else
4932 set = isl_map_wrap(map);
4933 space = isl_space_map_from_set(isl_set_get_space(set));
4934 ma = isl_multi_aff_identity(space);
4935 ls = isl_local_space_from_space(isl_set_get_space(set));
4936 aff = isl_aff_alloc(ls);
4937 if (aff) {
4938 isl_int_set_si(aff->v->el[0], 1);
4939 if (isl_int_is_one(hull->eq[i][o_out + d]))
4940 isl_seq_neg(aff->v->el + 1, hull->eq[i],
4941 aff->v->size - 1);
4942 else
4943 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
4944 aff->v->size - 1);
4945 isl_int_set(aff->v->el[1 + o_out + d], gcd);
4947 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
4948 set = isl_set_preimage_multi_aff(set, ma);
4950 ma = range_map(aff, d, n_in, n_out, is_set);
4952 if (is_set)
4953 map = set;
4954 else
4955 map = isl_set_unwrap(set);
4956 pma = isl_pw_multi_aff_from_map(map);
4958 if (!is_set) {
4959 space = isl_pw_multi_aff_get_domain_space(pma);
4960 space = isl_space_map_from_set(space);
4961 id = isl_pw_multi_aff_identity(space);
4962 pma = isl_pw_multi_aff_range_product(id, pma);
4964 id = isl_pw_multi_aff_from_multi_aff(ma);
4965 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
4967 isl_basic_map_free(hull);
4968 return pma;
4971 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4972 * "hull" contains the equalities valid for "map".
4974 * Check if any of the output dimensions is "strided".
4975 * That is, we check if it can be written as
4977 * x = m a + f(..)
4979 * with m greater than 1, a some combination of existentially quantified
4980 * variables and f an expression in the parameters and input dimensions.
4981 * If so, we remove the stride in pw_multi_aff_from_map_stride.
4983 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
4984 * special case.
4986 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
4987 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
4989 int i, j;
4990 unsigned n_out;
4991 unsigned o_out;
4992 unsigned n_div;
4993 unsigned o_div;
4994 isl_int gcd;
4996 n_div = isl_basic_map_dim(hull, isl_dim_div);
4997 o_div = isl_basic_map_offset(hull, isl_dim_div);
4999 if (n_div == 0) {
5000 isl_basic_map_free(hull);
5001 return pw_multi_aff_from_map_check_div(map);
5004 isl_int_init(gcd);
5006 n_out = isl_basic_map_dim(hull, isl_dim_out);
5007 o_out = isl_basic_map_offset(hull, isl_dim_out);
5009 for (i = 0; i < n_out; ++i) {
5010 for (j = 0; j < hull->n_eq; ++j) {
5011 isl_int *eq = hull->eq[j];
5012 isl_pw_multi_aff *res;
5014 if (!isl_int_is_one(eq[o_out + i]) &&
5015 !isl_int_is_negone(eq[o_out + i]))
5016 continue;
5017 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5018 continue;
5019 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5020 n_out - (i + 1)) != -1)
5021 continue;
5022 isl_seq_gcd(eq + o_div, n_div, &gcd);
5023 if (isl_int_is_zero(gcd))
5024 continue;
5025 if (isl_int_is_one(gcd))
5026 continue;
5028 res = pw_multi_aff_from_map_stride(map, hull,
5029 i, j, gcd);
5030 isl_int_clear(gcd);
5031 return res;
5035 isl_int_clear(gcd);
5036 isl_basic_map_free(hull);
5037 return pw_multi_aff_from_map_check_div(map);
5040 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5042 * As a special case, we first check if all output dimensions are uniquely
5043 * defined in terms of the parameters and input dimensions over the entire
5044 * domain. If so, we extract the desired isl_pw_multi_aff directly
5045 * from the affine hull of "map" and its domain.
5047 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5048 * special cases.
5050 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5052 isl_bool sv;
5053 isl_basic_map *hull;
5055 if (!map)
5056 return NULL;
5058 if (isl_map_n_basic_map(map) == 1) {
5059 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5060 hull = isl_basic_map_plain_affine_hull(hull);
5061 sv = isl_basic_map_plain_is_single_valued(hull);
5062 if (sv >= 0 && sv)
5063 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5064 hull);
5065 isl_basic_map_free(hull);
5067 map = isl_map_detect_equalities(map);
5068 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5069 sv = isl_basic_map_plain_is_single_valued(hull);
5070 if (sv >= 0 && sv)
5071 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5072 if (sv >= 0)
5073 return pw_multi_aff_from_map_check_strides(map, hull);
5074 isl_basic_map_free(hull);
5075 isl_map_free(map);
5076 return NULL;
5079 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5081 return isl_pw_multi_aff_from_map(set);
5084 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5085 * add it to *user.
5087 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5089 isl_union_pw_multi_aff **upma = user;
5090 isl_pw_multi_aff *pma;
5092 pma = isl_pw_multi_aff_from_map(map);
5093 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5095 return *upma ? isl_stat_ok : isl_stat_error;
5098 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5099 * domain.
5101 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5102 __isl_take isl_aff *aff)
5104 isl_multi_aff *ma;
5105 isl_pw_multi_aff *pma;
5107 ma = isl_multi_aff_from_aff(aff);
5108 pma = isl_pw_multi_aff_from_multi_aff(ma);
5109 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5112 /* Try and create an isl_union_pw_multi_aff that is equivalent
5113 * to the given isl_union_map.
5114 * The isl_union_map is required to be single-valued in each space.
5115 * Otherwise, an error is produced.
5117 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5118 __isl_take isl_union_map *umap)
5120 isl_space *space;
5121 isl_union_pw_multi_aff *upma;
5123 space = isl_union_map_get_space(umap);
5124 upma = isl_union_pw_multi_aff_empty(space);
5125 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5126 upma = isl_union_pw_multi_aff_free(upma);
5127 isl_union_map_free(umap);
5129 return upma;
5132 /* Try and create an isl_union_pw_multi_aff that is equivalent
5133 * to the given isl_union_set.
5134 * The isl_union_set is required to be a singleton in each space.
5135 * Otherwise, an error is produced.
5137 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5138 __isl_take isl_union_set *uset)
5140 return isl_union_pw_multi_aff_from_union_map(uset);
5143 /* Return the piecewise affine expression "set ? 1 : 0".
5145 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5147 isl_pw_aff *pa;
5148 isl_space *space = isl_set_get_space(set);
5149 isl_local_space *ls = isl_local_space_from_space(space);
5150 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5151 isl_aff *one = isl_aff_zero_on_domain(ls);
5153 one = isl_aff_add_constant_si(one, 1);
5154 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5155 set = isl_set_complement(set);
5156 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5158 return pa;
5161 /* Plug in "subs" for dimension "type", "pos" of "aff".
5163 * Let i be the dimension to replace and let "subs" be of the form
5165 * f/d
5167 * and "aff" of the form
5169 * (a i + g)/m
5171 * The result is
5173 * (a f + d g')/(m d)
5175 * where g' is the result of plugging in "subs" in each of the integer
5176 * divisions in g.
5178 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5179 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5181 isl_ctx *ctx;
5182 isl_int v;
5184 aff = isl_aff_cow(aff);
5185 if (!aff || !subs)
5186 return isl_aff_free(aff);
5188 ctx = isl_aff_get_ctx(aff);
5189 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5190 isl_die(ctx, isl_error_invalid,
5191 "spaces don't match", return isl_aff_free(aff));
5192 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5193 isl_die(ctx, isl_error_unsupported,
5194 "cannot handle divs yet", return isl_aff_free(aff));
5196 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5197 if (!aff->ls)
5198 return isl_aff_free(aff);
5200 aff->v = isl_vec_cow(aff->v);
5201 if (!aff->v)
5202 return isl_aff_free(aff);
5204 pos += isl_local_space_offset(aff->ls, type);
5206 isl_int_init(v);
5207 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5208 aff->v->size, subs->v->size, v);
5209 isl_int_clear(v);
5211 return aff;
5214 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5215 * expressions in "maff".
5217 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5218 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5219 __isl_keep isl_aff *subs)
5221 int i;
5223 maff = isl_multi_aff_cow(maff);
5224 if (!maff || !subs)
5225 return isl_multi_aff_free(maff);
5227 if (type == isl_dim_in)
5228 type = isl_dim_set;
5230 for (i = 0; i < maff->n; ++i) {
5231 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5232 if (!maff->p[i])
5233 return isl_multi_aff_free(maff);
5236 return maff;
5239 /* Plug in "subs" for dimension "type", "pos" of "pma".
5241 * pma is of the form
5243 * A_i(v) -> M_i(v)
5245 * while subs is of the form
5247 * v' = B_j(v) -> S_j
5249 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5250 * has a contribution in the result, in particular
5252 * C_ij(S_j) -> M_i(S_j)
5254 * Note that plugging in S_j in C_ij may also result in an empty set
5255 * and this contribution should simply be discarded.
5257 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5258 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5259 __isl_keep isl_pw_aff *subs)
5261 int i, j, n;
5262 isl_pw_multi_aff *res;
5264 if (!pma || !subs)
5265 return isl_pw_multi_aff_free(pma);
5267 n = pma->n * subs->n;
5268 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5270 for (i = 0; i < pma->n; ++i) {
5271 for (j = 0; j < subs->n; ++j) {
5272 isl_set *common;
5273 isl_multi_aff *res_ij;
5274 int empty;
5276 common = isl_set_intersect(
5277 isl_set_copy(pma->p[i].set),
5278 isl_set_copy(subs->p[j].set));
5279 common = isl_set_substitute(common,
5280 type, pos, subs->p[j].aff);
5281 empty = isl_set_plain_is_empty(common);
5282 if (empty < 0 || empty) {
5283 isl_set_free(common);
5284 if (empty < 0)
5285 goto error;
5286 continue;
5289 res_ij = isl_multi_aff_substitute(
5290 isl_multi_aff_copy(pma->p[i].maff),
5291 type, pos, subs->p[j].aff);
5293 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5297 isl_pw_multi_aff_free(pma);
5298 return res;
5299 error:
5300 isl_pw_multi_aff_free(pma);
5301 isl_pw_multi_aff_free(res);
5302 return NULL;
5305 /* Compute the preimage of a range of dimensions in the affine expression "src"
5306 * under "ma" and put the result in "dst". The number of dimensions in "src"
5307 * that precede the range is given by "n_before". The number of dimensions
5308 * in the range is given by the number of output dimensions of "ma".
5309 * The number of dimensions that follow the range is given by "n_after".
5310 * If "has_denom" is set (to one),
5311 * then "src" and "dst" have an extra initial denominator.
5312 * "n_div_ma" is the number of existentials in "ma"
5313 * "n_div_bset" is the number of existentials in "src"
5314 * The resulting "dst" (which is assumed to have been allocated by
5315 * the caller) contains coefficients for both sets of existentials,
5316 * first those in "ma" and then those in "src".
5317 * f, c1, c2 and g are temporary objects that have been initialized
5318 * by the caller.
5320 * Let src represent the expression
5322 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5324 * and let ma represent the expressions
5326 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5328 * We start out with the following expression for dst:
5330 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5332 * with the multiplication factor f initially equal to 1
5333 * and f \sum_i b_i v_i kept separately.
5334 * For each x_i that we substitute, we multiply the numerator
5335 * (and denominator) of dst by c_1 = m_i and add the numerator
5336 * of the x_i expression multiplied by c_2 = f b_i,
5337 * after removing the common factors of c_1 and c_2.
5338 * The multiplication factor f also needs to be multiplied by c_1
5339 * for the next x_j, j > i.
5341 void isl_seq_preimage(isl_int *dst, isl_int *src,
5342 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5343 int n_div_ma, int n_div_bmap,
5344 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5346 int i;
5347 int n_param, n_in, n_out;
5348 int o_dst, o_src;
5350 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5351 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5352 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5354 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5355 o_dst = o_src = has_denom + 1 + n_param + n_before;
5356 isl_seq_clr(dst + o_dst, n_in);
5357 o_dst += n_in;
5358 o_src += n_out;
5359 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5360 o_dst += n_after;
5361 o_src += n_after;
5362 isl_seq_clr(dst + o_dst, n_div_ma);
5363 o_dst += n_div_ma;
5364 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5366 isl_int_set_si(f, 1);
5368 for (i = 0; i < n_out; ++i) {
5369 int offset = has_denom + 1 + n_param + n_before + i;
5371 if (isl_int_is_zero(src[offset]))
5372 continue;
5373 isl_int_set(c1, ma->p[i]->v->el[0]);
5374 isl_int_mul(c2, f, src[offset]);
5375 isl_int_gcd(g, c1, c2);
5376 isl_int_divexact(c1, c1, g);
5377 isl_int_divexact(c2, c2, g);
5379 isl_int_mul(f, f, c1);
5380 o_dst = has_denom;
5381 o_src = 1;
5382 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5383 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5384 o_dst += 1 + n_param;
5385 o_src += 1 + n_param;
5386 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5387 o_dst += n_before;
5388 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5389 c2, ma->p[i]->v->el + o_src, n_in);
5390 o_dst += n_in;
5391 o_src += n_in;
5392 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5393 o_dst += n_after;
5394 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5395 c2, ma->p[i]->v->el + o_src, n_div_ma);
5396 o_dst += n_div_ma;
5397 o_src += n_div_ma;
5398 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5399 if (has_denom)
5400 isl_int_mul(dst[0], dst[0], c1);
5404 /* Compute the pullback of "aff" by the function represented by "ma".
5405 * In other words, plug in "ma" in "aff". The result is an affine expression
5406 * defined over the domain space of "ma".
5408 * If "aff" is represented by
5410 * (a(p) + b x + c(divs))/d
5412 * and ma is represented by
5414 * x = D(p) + F(y) + G(divs')
5416 * then the result is
5418 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5420 * The divs in the local space of the input are similarly adjusted
5421 * through a call to isl_local_space_preimage_multi_aff.
5423 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5424 __isl_take isl_multi_aff *ma)
5426 isl_aff *res = NULL;
5427 isl_local_space *ls;
5428 int n_div_aff, n_div_ma;
5429 isl_int f, c1, c2, g;
5431 ma = isl_multi_aff_align_divs(ma);
5432 if (!aff || !ma)
5433 goto error;
5435 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5436 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5438 ls = isl_aff_get_domain_local_space(aff);
5439 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5440 res = isl_aff_alloc(ls);
5441 if (!res)
5442 goto error;
5444 isl_int_init(f);
5445 isl_int_init(c1);
5446 isl_int_init(c2);
5447 isl_int_init(g);
5449 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5450 f, c1, c2, g, 1);
5452 isl_int_clear(f);
5453 isl_int_clear(c1);
5454 isl_int_clear(c2);
5455 isl_int_clear(g);
5457 isl_aff_free(aff);
5458 isl_multi_aff_free(ma);
5459 res = isl_aff_normalize(res);
5460 return res;
5461 error:
5462 isl_aff_free(aff);
5463 isl_multi_aff_free(ma);
5464 isl_aff_free(res);
5465 return NULL;
5468 /* Compute the pullback of "aff1" by the function represented by "aff2".
5469 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5470 * defined over the domain space of "aff1".
5472 * The domain of "aff1" should match the range of "aff2", which means
5473 * that it should be single-dimensional.
5475 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5476 __isl_take isl_aff *aff2)
5478 isl_multi_aff *ma;
5480 ma = isl_multi_aff_from_aff(aff2);
5481 return isl_aff_pullback_multi_aff(aff1, ma);
5484 /* Compute the pullback of "ma1" by the function represented by "ma2".
5485 * In other words, plug in "ma2" in "ma1".
5487 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5489 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5490 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5492 int i;
5493 isl_space *space = NULL;
5495 ma2 = isl_multi_aff_align_divs(ma2);
5496 ma1 = isl_multi_aff_cow(ma1);
5497 if (!ma1 || !ma2)
5498 goto error;
5500 space = isl_space_join(isl_multi_aff_get_space(ma2),
5501 isl_multi_aff_get_space(ma1));
5503 for (i = 0; i < ma1->n; ++i) {
5504 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5505 isl_multi_aff_copy(ma2));
5506 if (!ma1->p[i])
5507 goto error;
5510 ma1 = isl_multi_aff_reset_space(ma1, space);
5511 isl_multi_aff_free(ma2);
5512 return ma1;
5513 error:
5514 isl_space_free(space);
5515 isl_multi_aff_free(ma2);
5516 isl_multi_aff_free(ma1);
5517 return NULL;
5520 /* Compute the pullback of "ma1" by the function represented by "ma2".
5521 * In other words, plug in "ma2" in "ma1".
5523 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5524 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5526 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5527 &isl_multi_aff_pullback_multi_aff_aligned);
5530 /* Extend the local space of "dst" to include the divs
5531 * in the local space of "src".
5533 * If "src" does not have any divs or if the local spaces of "dst" and
5534 * "src" are the same, then no extension is required.
5536 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5537 __isl_keep isl_aff *src)
5539 isl_ctx *ctx;
5540 int src_n_div, dst_n_div;
5541 int *exp1 = NULL;
5542 int *exp2 = NULL;
5543 isl_bool equal;
5544 isl_mat *div;
5546 if (!src || !dst)
5547 return isl_aff_free(dst);
5549 ctx = isl_aff_get_ctx(src);
5550 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5551 if (equal < 0)
5552 return isl_aff_free(dst);
5553 if (!equal)
5554 isl_die(ctx, isl_error_invalid,
5555 "spaces don't match", goto error);
5557 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5558 if (src_n_div == 0)
5559 return dst;
5560 equal = isl_local_space_is_equal(src->ls, dst->ls);
5561 if (equal < 0)
5562 return isl_aff_free(dst);
5563 if (equal)
5564 return dst;
5566 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5567 exp1 = isl_alloc_array(ctx, int, src_n_div);
5568 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5569 if (!exp1 || (dst_n_div && !exp2))
5570 goto error;
5572 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5573 dst = isl_aff_expand_divs(dst, div, exp2);
5574 free(exp1);
5575 free(exp2);
5577 return dst;
5578 error:
5579 free(exp1);
5580 free(exp2);
5581 return isl_aff_free(dst);
5584 /* Adjust the local spaces of the affine expressions in "maff"
5585 * such that they all have the save divs.
5587 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5588 __isl_take isl_multi_aff *maff)
5590 int i;
5592 if (!maff)
5593 return NULL;
5594 if (maff->n == 0)
5595 return maff;
5596 maff = isl_multi_aff_cow(maff);
5597 if (!maff)
5598 return NULL;
5600 for (i = 1; i < maff->n; ++i)
5601 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5602 for (i = 1; i < maff->n; ++i) {
5603 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5604 if (!maff->p[i])
5605 return isl_multi_aff_free(maff);
5608 return maff;
5611 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5613 aff = isl_aff_cow(aff);
5614 if (!aff)
5615 return NULL;
5617 aff->ls = isl_local_space_lift(aff->ls);
5618 if (!aff->ls)
5619 return isl_aff_free(aff);
5621 return aff;
5624 /* Lift "maff" to a space with extra dimensions such that the result
5625 * has no more existentially quantified variables.
5626 * If "ls" is not NULL, then *ls is assigned the local space that lies
5627 * at the basis of the lifting applied to "maff".
5629 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5630 __isl_give isl_local_space **ls)
5632 int i;
5633 isl_space *space;
5634 unsigned n_div;
5636 if (ls)
5637 *ls = NULL;
5639 if (!maff)
5640 return NULL;
5642 if (maff->n == 0) {
5643 if (ls) {
5644 isl_space *space = isl_multi_aff_get_domain_space(maff);
5645 *ls = isl_local_space_from_space(space);
5646 if (!*ls)
5647 return isl_multi_aff_free(maff);
5649 return maff;
5652 maff = isl_multi_aff_cow(maff);
5653 maff = isl_multi_aff_align_divs(maff);
5654 if (!maff)
5655 return NULL;
5657 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5658 space = isl_multi_aff_get_space(maff);
5659 space = isl_space_lift(isl_space_domain(space), n_div);
5660 space = isl_space_extend_domain_with_range(space,
5661 isl_multi_aff_get_space(maff));
5662 if (!space)
5663 return isl_multi_aff_free(maff);
5664 isl_space_free(maff->space);
5665 maff->space = space;
5667 if (ls) {
5668 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5669 if (!*ls)
5670 return isl_multi_aff_free(maff);
5673 for (i = 0; i < maff->n; ++i) {
5674 maff->p[i] = isl_aff_lift(maff->p[i]);
5675 if (!maff->p[i])
5676 goto error;
5679 return maff;
5680 error:
5681 if (ls)
5682 isl_local_space_free(*ls);
5683 return isl_multi_aff_free(maff);
5687 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5689 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5690 __isl_keep isl_pw_multi_aff *pma, int pos)
5692 int i;
5693 int n_out;
5694 isl_space *space;
5695 isl_pw_aff *pa;
5697 if (!pma)
5698 return NULL;
5700 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5701 if (pos < 0 || pos >= n_out)
5702 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5703 "index out of bounds", return NULL);
5705 space = isl_pw_multi_aff_get_space(pma);
5706 space = isl_space_drop_dims(space, isl_dim_out,
5707 pos + 1, n_out - pos - 1);
5708 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5710 pa = isl_pw_aff_alloc_size(space, pma->n);
5711 for (i = 0; i < pma->n; ++i) {
5712 isl_aff *aff;
5713 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5714 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5717 return pa;
5720 /* Return an isl_pw_multi_aff with the given "set" as domain and
5721 * an unnamed zero-dimensional range.
5723 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5724 __isl_take isl_set *set)
5726 isl_multi_aff *ma;
5727 isl_space *space;
5729 space = isl_set_get_space(set);
5730 space = isl_space_from_domain(space);
5731 ma = isl_multi_aff_zero(space);
5732 return isl_pw_multi_aff_alloc(set, ma);
5735 /* Add an isl_pw_multi_aff with the given "set" as domain and
5736 * an unnamed zero-dimensional range to *user.
5738 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5739 void *user)
5741 isl_union_pw_multi_aff **upma = user;
5742 isl_pw_multi_aff *pma;
5744 pma = isl_pw_multi_aff_from_domain(set);
5745 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5747 return isl_stat_ok;
5750 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5751 * an unnamed zero-dimensional range.
5753 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5754 __isl_take isl_union_set *uset)
5756 isl_space *space;
5757 isl_union_pw_multi_aff *upma;
5759 if (!uset)
5760 return NULL;
5762 space = isl_union_set_get_space(uset);
5763 upma = isl_union_pw_multi_aff_empty(space);
5765 if (isl_union_set_foreach_set(uset,
5766 &add_pw_multi_aff_from_domain, &upma) < 0)
5767 goto error;
5769 isl_union_set_free(uset);
5770 return upma;
5771 error:
5772 isl_union_set_free(uset);
5773 isl_union_pw_multi_aff_free(upma);
5774 return NULL;
5777 /* Convert "pma" to an isl_map and add it to *umap.
5779 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5780 void *user)
5782 isl_union_map **umap = user;
5783 isl_map *map;
5785 map = isl_map_from_pw_multi_aff(pma);
5786 *umap = isl_union_map_add_map(*umap, map);
5788 return isl_stat_ok;
5791 /* Construct a union map mapping the domain of the union
5792 * piecewise multi-affine expression to its range, with each dimension
5793 * in the range equated to the corresponding affine expression on its cell.
5795 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5796 __isl_take isl_union_pw_multi_aff *upma)
5798 isl_space *space;
5799 isl_union_map *umap;
5801 if (!upma)
5802 return NULL;
5804 space = isl_union_pw_multi_aff_get_space(upma);
5805 umap = isl_union_map_empty(space);
5807 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5808 &map_from_pw_multi_aff, &umap) < 0)
5809 goto error;
5811 isl_union_pw_multi_aff_free(upma);
5812 return umap;
5813 error:
5814 isl_union_pw_multi_aff_free(upma);
5815 isl_union_map_free(umap);
5816 return NULL;
5819 /* Local data for bin_entry and the callback "fn".
5821 struct isl_union_pw_multi_aff_bin_data {
5822 isl_union_pw_multi_aff *upma2;
5823 isl_union_pw_multi_aff *res;
5824 isl_pw_multi_aff *pma;
5825 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5828 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5829 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5831 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5833 struct isl_union_pw_multi_aff_bin_data *data = user;
5834 isl_stat r;
5836 data->pma = pma;
5837 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5838 data->fn, data);
5839 isl_pw_multi_aff_free(pma);
5841 return r;
5844 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5845 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5846 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5847 * as *entry. The callback should adjust data->res if desired.
5849 static __isl_give isl_union_pw_multi_aff *bin_op(
5850 __isl_take isl_union_pw_multi_aff *upma1,
5851 __isl_take isl_union_pw_multi_aff *upma2,
5852 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5854 isl_space *space;
5855 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5857 space = isl_union_pw_multi_aff_get_space(upma2);
5858 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5859 space = isl_union_pw_multi_aff_get_space(upma1);
5860 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5862 if (!upma1 || !upma2)
5863 goto error;
5865 data.upma2 = upma2;
5866 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5867 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5868 &bin_entry, &data) < 0)
5869 goto error;
5871 isl_union_pw_multi_aff_free(upma1);
5872 isl_union_pw_multi_aff_free(upma2);
5873 return data.res;
5874 error:
5875 isl_union_pw_multi_aff_free(upma1);
5876 isl_union_pw_multi_aff_free(upma2);
5877 isl_union_pw_multi_aff_free(data.res);
5878 return NULL;
5881 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5882 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5884 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5885 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5887 isl_space *space;
5889 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5890 isl_pw_multi_aff_get_space(pma2));
5891 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5892 &isl_multi_aff_range_product);
5895 /* Given two isl_pw_multi_affs A -> B and C -> D,
5896 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5898 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
5899 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5901 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5902 &pw_multi_aff_range_product);
5905 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5906 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5908 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
5909 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5911 isl_space *space;
5913 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5914 isl_pw_multi_aff_get_space(pma2));
5915 space = isl_space_flatten_range(space);
5916 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5917 &isl_multi_aff_flat_range_product);
5920 /* Given two isl_pw_multi_affs A -> B and C -> D,
5921 * construct an isl_pw_multi_aff (A * C) -> (B, D).
5923 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
5924 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5926 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
5927 &pw_multi_aff_flat_range_product);
5930 /* If data->pma and "pma2" have the same domain space, then compute
5931 * their flat range product and the result to data->res.
5933 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
5934 void *user)
5936 struct isl_union_pw_multi_aff_bin_data *data = user;
5938 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
5939 pma2->dim, isl_dim_in)) {
5940 isl_pw_multi_aff_free(pma2);
5941 return isl_stat_ok;
5944 pma2 = isl_pw_multi_aff_flat_range_product(
5945 isl_pw_multi_aff_copy(data->pma), pma2);
5947 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
5949 return isl_stat_ok;
5952 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
5953 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
5955 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
5956 __isl_take isl_union_pw_multi_aff *upma1,
5957 __isl_take isl_union_pw_multi_aff *upma2)
5959 return bin_op(upma1, upma2, &flat_range_product_entry);
5962 /* Replace the affine expressions at position "pos" in "pma" by "pa".
5963 * The parameters are assumed to have been aligned.
5965 * The implementation essentially performs an isl_pw_*_on_shared_domain,
5966 * except that it works on two different isl_pw_* types.
5968 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
5969 __isl_take isl_pw_multi_aff *pma, unsigned pos,
5970 __isl_take isl_pw_aff *pa)
5972 int i, j, n;
5973 isl_pw_multi_aff *res = NULL;
5975 if (!pma || !pa)
5976 goto error;
5978 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
5979 pa->dim, isl_dim_in))
5980 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5981 "domains don't match", goto error);
5982 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
5983 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5984 "index out of bounds", goto error);
5986 n = pma->n * pa->n;
5987 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
5989 for (i = 0; i < pma->n; ++i) {
5990 for (j = 0; j < pa->n; ++j) {
5991 isl_set *common;
5992 isl_multi_aff *res_ij;
5993 int empty;
5995 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
5996 isl_set_copy(pa->p[j].set));
5997 empty = isl_set_plain_is_empty(common);
5998 if (empty < 0 || empty) {
5999 isl_set_free(common);
6000 if (empty < 0)
6001 goto error;
6002 continue;
6005 res_ij = isl_multi_aff_set_aff(
6006 isl_multi_aff_copy(pma->p[i].maff), pos,
6007 isl_aff_copy(pa->p[j].aff));
6008 res_ij = isl_multi_aff_gist(res_ij,
6009 isl_set_copy(common));
6011 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6015 isl_pw_multi_aff_free(pma);
6016 isl_pw_aff_free(pa);
6017 return res;
6018 error:
6019 isl_pw_multi_aff_free(pma);
6020 isl_pw_aff_free(pa);
6021 return isl_pw_multi_aff_free(res);
6024 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6026 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6027 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6028 __isl_take isl_pw_aff *pa)
6030 if (!pma || !pa)
6031 goto error;
6032 if (isl_space_match(pma->dim, isl_dim_param, pa->dim, isl_dim_param))
6033 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6034 if (!isl_space_has_named_params(pma->dim) ||
6035 !isl_space_has_named_params(pa->dim))
6036 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6037 "unaligned unnamed parameters", goto error);
6038 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6039 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6040 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6041 error:
6042 isl_pw_multi_aff_free(pma);
6043 isl_pw_aff_free(pa);
6044 return NULL;
6047 /* Do the parameters of "pa" match those of "space"?
6049 int isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6050 __isl_keep isl_space *space)
6052 isl_space *pa_space;
6053 int match;
6055 if (!pa || !space)
6056 return -1;
6058 pa_space = isl_pw_aff_get_space(pa);
6060 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6062 isl_space_free(pa_space);
6063 return match;
6066 /* Check that the domain space of "pa" matches "space".
6068 * Return 0 on success and -1 on error.
6070 int isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6071 __isl_keep isl_space *space)
6073 isl_space *pa_space;
6074 int match;
6076 if (!pa || !space)
6077 return -1;
6079 pa_space = isl_pw_aff_get_space(pa);
6081 match = isl_space_match(space, isl_dim_param, pa_space, isl_dim_param);
6082 if (match < 0)
6083 goto error;
6084 if (!match)
6085 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6086 "parameters don't match", goto error);
6087 match = isl_space_tuple_is_equal(space, isl_dim_in,
6088 pa_space, isl_dim_in);
6089 if (match < 0)
6090 goto error;
6091 if (!match)
6092 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6093 "domains don't match", goto error);
6094 isl_space_free(pa_space);
6095 return 0;
6096 error:
6097 isl_space_free(pa_space);
6098 return -1;
6101 #undef BASE
6102 #define BASE pw_aff
6103 #undef DOMBASE
6104 #define DOMBASE set
6106 #include <isl_multi_templ.c>
6107 #include <isl_multi_apply_set.c>
6108 #include <isl_multi_coalesce.c>
6109 #include <isl_multi_gist.c>
6110 #include <isl_multi_hash.c>
6111 #include <isl_multi_intersect.c>
6113 /* Scale the elements of "pma" by the corresponding elements of "mv".
6115 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6116 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6118 int i;
6120 pma = isl_pw_multi_aff_cow(pma);
6121 if (!pma || !mv)
6122 goto error;
6123 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6124 mv->space, isl_dim_set))
6125 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6126 "spaces don't match", goto error);
6127 if (!isl_space_match(pma->dim, isl_dim_param,
6128 mv->space, isl_dim_param)) {
6129 pma = isl_pw_multi_aff_align_params(pma,
6130 isl_multi_val_get_space(mv));
6131 mv = isl_multi_val_align_params(mv,
6132 isl_pw_multi_aff_get_space(pma));
6133 if (!pma || !mv)
6134 goto error;
6137 for (i = 0; i < pma->n; ++i) {
6138 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6139 isl_multi_val_copy(mv));
6140 if (!pma->p[i].maff)
6141 goto error;
6144 isl_multi_val_free(mv);
6145 return pma;
6146 error:
6147 isl_multi_val_free(mv);
6148 isl_pw_multi_aff_free(pma);
6149 return NULL;
6152 /* This function is called for each entry of an isl_union_pw_multi_aff.
6153 * If the space of the entry matches that of data->mv,
6154 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6155 * Otherwise, return an empty isl_pw_multi_aff.
6157 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6158 __isl_take isl_pw_multi_aff *pma, void *user)
6160 isl_multi_val *mv = user;
6162 if (!pma)
6163 return NULL;
6164 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6165 mv->space, isl_dim_set)) {
6166 isl_space *space = isl_pw_multi_aff_get_space(pma);
6167 isl_pw_multi_aff_free(pma);
6168 return isl_pw_multi_aff_empty(space);
6171 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6174 /* Scale the elements of "upma" by the corresponding elements of "mv",
6175 * for those entries that match the space of "mv".
6177 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6178 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6180 upma = isl_union_pw_multi_aff_align_params(upma,
6181 isl_multi_val_get_space(mv));
6182 mv = isl_multi_val_align_params(mv,
6183 isl_union_pw_multi_aff_get_space(upma));
6184 if (!upma || !mv)
6185 goto error;
6187 return isl_union_pw_multi_aff_transform(upma,
6188 &union_pw_multi_aff_scale_multi_val_entry, mv);
6190 isl_multi_val_free(mv);
6191 return upma;
6192 error:
6193 isl_multi_val_free(mv);
6194 isl_union_pw_multi_aff_free(upma);
6195 return NULL;
6198 /* Construct and return a piecewise multi affine expression
6199 * in the given space with value zero in each of the output dimensions and
6200 * a universe domain.
6202 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6204 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6207 /* Construct and return a piecewise multi affine expression
6208 * that is equal to the given piecewise affine expression.
6210 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6211 __isl_take isl_pw_aff *pa)
6213 int i;
6214 isl_space *space;
6215 isl_pw_multi_aff *pma;
6217 if (!pa)
6218 return NULL;
6220 space = isl_pw_aff_get_space(pa);
6221 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6223 for (i = 0; i < pa->n; ++i) {
6224 isl_set *set;
6225 isl_multi_aff *ma;
6227 set = isl_set_copy(pa->p[i].set);
6228 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6229 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6232 isl_pw_aff_free(pa);
6233 return pma;
6236 /* Construct a set or map mapping the shared (parameter) domain
6237 * of the piecewise affine expressions to the range of "mpa"
6238 * with each dimension in the range equated to the
6239 * corresponding piecewise affine expression.
6241 static __isl_give isl_map *map_from_multi_pw_aff(
6242 __isl_take isl_multi_pw_aff *mpa)
6244 int i;
6245 isl_space *space;
6246 isl_map *map;
6248 if (!mpa)
6249 return NULL;
6251 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6252 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6253 "invalid space", goto error);
6255 space = isl_multi_pw_aff_get_domain_space(mpa);
6256 map = isl_map_universe(isl_space_from_domain(space));
6258 for (i = 0; i < mpa->n; ++i) {
6259 isl_pw_aff *pa;
6260 isl_map *map_i;
6262 pa = isl_pw_aff_copy(mpa->p[i]);
6263 map_i = map_from_pw_aff(pa);
6265 map = isl_map_flat_range_product(map, map_i);
6268 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6270 isl_multi_pw_aff_free(mpa);
6271 return map;
6272 error:
6273 isl_multi_pw_aff_free(mpa);
6274 return NULL;
6277 /* Construct a map mapping the shared domain
6278 * of the piecewise affine expressions to the range of "mpa"
6279 * with each dimension in the range equated to the
6280 * corresponding piecewise affine expression.
6282 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6284 if (!mpa)
6285 return NULL;
6286 if (isl_space_is_set(mpa->space))
6287 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6288 "space of input is not a map", goto error);
6290 return map_from_multi_pw_aff(mpa);
6291 error:
6292 isl_multi_pw_aff_free(mpa);
6293 return NULL;
6296 /* Construct a set mapping the shared parameter domain
6297 * of the piecewise affine expressions to the space of "mpa"
6298 * with each dimension in the range equated to the
6299 * corresponding piecewise affine expression.
6301 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6303 if (!mpa)
6304 return NULL;
6305 if (!isl_space_is_set(mpa->space))
6306 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6307 "space of input is not a set", goto error);
6309 return map_from_multi_pw_aff(mpa);
6310 error:
6311 isl_multi_pw_aff_free(mpa);
6312 return NULL;
6315 /* Construct and return a piecewise multi affine expression
6316 * that is equal to the given multi piecewise affine expression
6317 * on the shared domain of the piecewise affine expressions.
6319 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6320 __isl_take isl_multi_pw_aff *mpa)
6322 int i;
6323 isl_space *space;
6324 isl_pw_aff *pa;
6325 isl_pw_multi_aff *pma;
6327 if (!mpa)
6328 return NULL;
6330 space = isl_multi_pw_aff_get_space(mpa);
6332 if (mpa->n == 0) {
6333 isl_multi_pw_aff_free(mpa);
6334 return isl_pw_multi_aff_zero(space);
6337 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6338 pma = isl_pw_multi_aff_from_pw_aff(pa);
6340 for (i = 1; i < mpa->n; ++i) {
6341 isl_pw_multi_aff *pma_i;
6343 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6344 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6345 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6348 pma = isl_pw_multi_aff_reset_space(pma, space);
6350 isl_multi_pw_aff_free(mpa);
6351 return pma;
6354 /* Construct and return a multi piecewise affine expression
6355 * that is equal to the given multi affine expression.
6357 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6358 __isl_take isl_multi_aff *ma)
6360 int i, n;
6361 isl_multi_pw_aff *mpa;
6363 if (!ma)
6364 return NULL;
6366 n = isl_multi_aff_dim(ma, isl_dim_out);
6367 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6369 for (i = 0; i < n; ++i) {
6370 isl_pw_aff *pa;
6372 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6373 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6376 isl_multi_aff_free(ma);
6377 return mpa;
6380 /* Construct and return a multi piecewise affine expression
6381 * that is equal to the given piecewise multi affine expression.
6383 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6384 __isl_take isl_pw_multi_aff *pma)
6386 int i, n;
6387 isl_space *space;
6388 isl_multi_pw_aff *mpa;
6390 if (!pma)
6391 return NULL;
6393 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6394 space = isl_pw_multi_aff_get_space(pma);
6395 mpa = isl_multi_pw_aff_alloc(space);
6397 for (i = 0; i < n; ++i) {
6398 isl_pw_aff *pa;
6400 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6401 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6404 isl_pw_multi_aff_free(pma);
6405 return mpa;
6408 /* Do "pa1" and "pa2" represent the same function?
6410 * We first check if they are obviously equal.
6411 * If not, we convert them to maps and check if those are equal.
6413 * If "pa1" or "pa2" contain any NaNs, then they are considered
6414 * not to be the same. A NaN is not equal to anything, not even
6415 * to another NaN.
6417 int isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1, __isl_keep isl_pw_aff *pa2)
6419 int equal;
6420 isl_bool has_nan;
6421 isl_map *map1, *map2;
6423 if (!pa1 || !pa2)
6424 return -1;
6426 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6427 if (equal < 0 || equal)
6428 return equal;
6429 has_nan = isl_pw_aff_involves_nan(pa1);
6430 if (has_nan >= 0 && !has_nan)
6431 has_nan = isl_pw_aff_involves_nan(pa2);
6432 if (has_nan < 0)
6433 return -1;
6434 if (has_nan)
6435 return 0;
6437 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6438 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6439 equal = isl_map_is_equal(map1, map2);
6440 isl_map_free(map1);
6441 isl_map_free(map2);
6443 return equal;
6446 /* Do "mpa1" and "mpa2" represent the same function?
6448 * Note that we cannot convert the entire isl_multi_pw_aff
6449 * to a map because the domains of the piecewise affine expressions
6450 * may not be the same.
6452 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6453 __isl_keep isl_multi_pw_aff *mpa2)
6455 int i;
6456 isl_bool equal;
6458 if (!mpa1 || !mpa2)
6459 return isl_bool_error;
6461 if (!isl_space_match(mpa1->space, isl_dim_param,
6462 mpa2->space, isl_dim_param)) {
6463 if (!isl_space_has_named_params(mpa1->space))
6464 return isl_bool_false;
6465 if (!isl_space_has_named_params(mpa2->space))
6466 return isl_bool_false;
6467 mpa1 = isl_multi_pw_aff_copy(mpa1);
6468 mpa2 = isl_multi_pw_aff_copy(mpa2);
6469 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6470 isl_multi_pw_aff_get_space(mpa2));
6471 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6472 isl_multi_pw_aff_get_space(mpa1));
6473 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6474 isl_multi_pw_aff_free(mpa1);
6475 isl_multi_pw_aff_free(mpa2);
6476 return equal;
6479 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6480 if (equal < 0 || !equal)
6481 return equal;
6483 for (i = 0; i < mpa1->n; ++i) {
6484 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6485 if (equal < 0 || !equal)
6486 return equal;
6489 return isl_bool_true;
6492 /* Compute the pullback of "mpa" by the function represented by "ma".
6493 * In other words, plug in "ma" in "mpa".
6495 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6497 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6498 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6500 int i;
6501 isl_space *space = NULL;
6503 mpa = isl_multi_pw_aff_cow(mpa);
6504 if (!mpa || !ma)
6505 goto error;
6507 space = isl_space_join(isl_multi_aff_get_space(ma),
6508 isl_multi_pw_aff_get_space(mpa));
6509 if (!space)
6510 goto error;
6512 for (i = 0; i < mpa->n; ++i) {
6513 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6514 isl_multi_aff_copy(ma));
6515 if (!mpa->p[i])
6516 goto error;
6519 isl_multi_aff_free(ma);
6520 isl_space_free(mpa->space);
6521 mpa->space = space;
6522 return mpa;
6523 error:
6524 isl_space_free(space);
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 "ma".
6531 * In other words, plug in "ma" in "mpa".
6533 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6534 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6536 if (!mpa || !ma)
6537 goto error;
6538 if (isl_space_match(mpa->space, isl_dim_param,
6539 ma->space, isl_dim_param))
6540 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6541 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6542 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6543 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6544 error:
6545 isl_multi_pw_aff_free(mpa);
6546 isl_multi_aff_free(ma);
6547 return NULL;
6550 /* Compute the pullback of "mpa" by the function represented by "pma".
6551 * In other words, plug in "pma" in "mpa".
6553 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6555 static __isl_give isl_multi_pw_aff *
6556 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6557 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6559 int i;
6560 isl_space *space = NULL;
6562 mpa = isl_multi_pw_aff_cow(mpa);
6563 if (!mpa || !pma)
6564 goto error;
6566 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6567 isl_multi_pw_aff_get_space(mpa));
6569 for (i = 0; i < mpa->n; ++i) {
6570 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6571 isl_pw_multi_aff_copy(pma));
6572 if (!mpa->p[i])
6573 goto error;
6576 isl_pw_multi_aff_free(pma);
6577 isl_space_free(mpa->space);
6578 mpa->space = space;
6579 return mpa;
6580 error:
6581 isl_space_free(space);
6582 isl_multi_pw_aff_free(mpa);
6583 isl_pw_multi_aff_free(pma);
6584 return NULL;
6587 /* Compute the pullback of "mpa" by the function represented by "pma".
6588 * In other words, plug in "pma" in "mpa".
6590 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6591 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6593 if (!mpa || !pma)
6594 goto error;
6595 if (isl_space_match(mpa->space, isl_dim_param, pma->dim, isl_dim_param))
6596 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6597 mpa = isl_multi_pw_aff_align_params(mpa,
6598 isl_pw_multi_aff_get_space(pma));
6599 pma = isl_pw_multi_aff_align_params(pma,
6600 isl_multi_pw_aff_get_space(mpa));
6601 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6602 error:
6603 isl_multi_pw_aff_free(mpa);
6604 isl_pw_multi_aff_free(pma);
6605 return NULL;
6608 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6609 * with the domain of "aff". The domain of the result is the same
6610 * as that of "mpa".
6611 * "mpa" and "aff" are assumed to have been aligned.
6613 * We first extract the parametric constant from "aff", defined
6614 * over the correct domain.
6615 * Then we add the appropriate combinations of the members of "mpa".
6616 * Finally, we add the integer divisions through recursive calls.
6618 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6619 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6621 int i, n_in, n_div;
6622 isl_space *space;
6623 isl_val *v;
6624 isl_pw_aff *pa;
6625 isl_aff *tmp;
6627 n_in = isl_aff_dim(aff, isl_dim_in);
6628 n_div = isl_aff_dim(aff, isl_dim_div);
6630 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6631 tmp = isl_aff_copy(aff);
6632 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6633 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6634 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6635 isl_space_dim(space, isl_dim_set));
6636 tmp = isl_aff_reset_domain_space(tmp, space);
6637 pa = isl_pw_aff_from_aff(tmp);
6639 for (i = 0; i < n_in; ++i) {
6640 isl_pw_aff *pa_i;
6642 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6643 continue;
6644 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6645 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6646 pa_i = isl_pw_aff_scale_val(pa_i, v);
6647 pa = isl_pw_aff_add(pa, pa_i);
6650 for (i = 0; i < n_div; ++i) {
6651 isl_aff *div;
6652 isl_pw_aff *pa_i;
6654 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6655 continue;
6656 div = isl_aff_get_div(aff, i);
6657 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6658 isl_multi_pw_aff_copy(mpa), div);
6659 pa_i = isl_pw_aff_floor(pa_i);
6660 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6661 pa_i = isl_pw_aff_scale_val(pa_i, v);
6662 pa = isl_pw_aff_add(pa, pa_i);
6665 isl_multi_pw_aff_free(mpa);
6666 isl_aff_free(aff);
6668 return pa;
6671 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6672 * with the domain of "aff". The domain of the result is the same
6673 * as that of "mpa".
6675 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6676 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6678 if (!aff || !mpa)
6679 goto error;
6680 if (isl_space_match(aff->ls->dim, isl_dim_param,
6681 mpa->space, isl_dim_param))
6682 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6684 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6685 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6687 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6688 error:
6689 isl_aff_free(aff);
6690 isl_multi_pw_aff_free(mpa);
6691 return NULL;
6694 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6695 * with the domain of "pa". The domain of the result is the same
6696 * as that of "mpa".
6697 * "mpa" and "pa" are assumed to have been aligned.
6699 * We consider each piece in turn. Note that the domains of the
6700 * pieces are assumed to be disjoint and they remain disjoint
6701 * after taking the preimage (over the same function).
6703 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6704 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6706 isl_space *space;
6707 isl_pw_aff *res;
6708 int i;
6710 if (!mpa || !pa)
6711 goto error;
6713 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6714 isl_pw_aff_get_space(pa));
6715 res = isl_pw_aff_empty(space);
6717 for (i = 0; i < pa->n; ++i) {
6718 isl_pw_aff *pa_i;
6719 isl_set *domain;
6721 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6722 isl_multi_pw_aff_copy(mpa),
6723 isl_aff_copy(pa->p[i].aff));
6724 domain = isl_set_copy(pa->p[i].set);
6725 domain = isl_set_preimage_multi_pw_aff(domain,
6726 isl_multi_pw_aff_copy(mpa));
6727 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6728 res = isl_pw_aff_add_disjoint(res, pa_i);
6731 isl_pw_aff_free(pa);
6732 isl_multi_pw_aff_free(mpa);
6733 return res;
6734 error:
6735 isl_pw_aff_free(pa);
6736 isl_multi_pw_aff_free(mpa);
6737 return NULL;
6740 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6741 * with the domain of "pa". The domain of the result is the same
6742 * as that of "mpa".
6744 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6745 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6747 if (!pa || !mpa)
6748 goto error;
6749 if (isl_space_match(pa->dim, isl_dim_param, mpa->space, isl_dim_param))
6750 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6752 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6753 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6755 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6756 error:
6757 isl_pw_aff_free(pa);
6758 isl_multi_pw_aff_free(mpa);
6759 return NULL;
6762 /* Compute the pullback of "pa" by the function represented by "mpa".
6763 * In other words, plug in "mpa" in "pa".
6764 * "pa" and "mpa" are assumed to have been aligned.
6766 * The pullback is computed by applying "pa" to "mpa".
6768 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6769 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6771 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6774 /* Compute the pullback of "pa" by the function represented by "mpa".
6775 * In other words, plug in "mpa" in "pa".
6777 * The pullback is computed by applying "pa" to "mpa".
6779 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6780 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6782 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6785 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6786 * In other words, plug in "mpa2" in "mpa1".
6788 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6790 * We pullback each member of "mpa1" in turn.
6792 static __isl_give isl_multi_pw_aff *
6793 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6794 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6796 int i;
6797 isl_space *space = NULL;
6799 mpa1 = isl_multi_pw_aff_cow(mpa1);
6800 if (!mpa1 || !mpa2)
6801 goto error;
6803 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6804 isl_multi_pw_aff_get_space(mpa1));
6806 for (i = 0; i < mpa1->n; ++i) {
6807 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6808 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
6809 if (!mpa1->p[i])
6810 goto error;
6813 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6815 isl_multi_pw_aff_free(mpa2);
6816 return mpa1;
6817 error:
6818 isl_space_free(space);
6819 isl_multi_pw_aff_free(mpa1);
6820 isl_multi_pw_aff_free(mpa2);
6821 return NULL;
6824 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6825 * In other words, plug in "mpa2" in "mpa1".
6827 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
6828 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6830 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
6831 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
6834 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
6835 * of "mpa1" and "mpa2" live in the same space, construct map space
6836 * between the domain spaces of "mpa1" and "mpa2" and call "order"
6837 * with this map space as extract argument.
6839 static __isl_give isl_map *isl_multi_pw_aff_order_map(
6840 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6841 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
6842 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
6844 int match;
6845 isl_space *space1, *space2;
6846 isl_map *res;
6848 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6849 isl_multi_pw_aff_get_space(mpa2));
6850 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6851 isl_multi_pw_aff_get_space(mpa1));
6852 if (!mpa1 || !mpa2)
6853 goto error;
6854 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
6855 mpa2->space, isl_dim_out);
6856 if (match < 0)
6857 goto error;
6858 if (!match)
6859 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
6860 "range spaces don't match", goto error);
6861 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
6862 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
6863 space1 = isl_space_map_from_domain_and_range(space1, space2);
6865 res = order(mpa1, mpa2, space1);
6866 isl_multi_pw_aff_free(mpa1);
6867 isl_multi_pw_aff_free(mpa2);
6868 return res;
6869 error:
6870 isl_multi_pw_aff_free(mpa1);
6871 isl_multi_pw_aff_free(mpa2);
6872 return NULL;
6875 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6876 * where the function values are equal. "space" is the space of the result.
6877 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6879 * "mpa1" and "mpa2" are equal when each of the pairs of elements
6880 * in the sequences are equal.
6882 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
6883 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
6884 __isl_take isl_space *space)
6886 int i, n;
6887 isl_map *res;
6889 res = isl_map_universe(space);
6891 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6892 for (i = 0; i < n; ++i) {
6893 isl_pw_aff *pa1, *pa2;
6894 isl_map *map;
6896 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6897 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6898 map = isl_pw_aff_eq_map(pa1, pa2);
6899 res = isl_map_intersect(res, map);
6902 return res;
6905 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6906 * where the function values are equal.
6908 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
6909 __isl_take isl_multi_pw_aff *mpa2)
6911 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6912 &isl_multi_pw_aff_eq_map_on_space);
6915 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6916 * where the function values of "mpa1" is lexicographically satisfies "base"
6917 * compared to that of "mpa2". "space" is the space of the result.
6918 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6920 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
6921 * if its i-th element satisfies "base" when compared to
6922 * the i-th element of "mpa2" while all previous elements are
6923 * pairwise equal.
6925 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
6926 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6927 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
6928 __isl_take isl_pw_aff *pa2),
6929 __isl_take isl_space *space)
6931 int i, n;
6932 isl_map *res, *rest;
6934 res = isl_map_empty(isl_space_copy(space));
6935 rest = isl_map_universe(space);
6937 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
6938 for (i = 0; i < n; ++i) {
6939 isl_pw_aff *pa1, *pa2;
6940 isl_map *map;
6942 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6943 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6944 map = base(pa1, pa2);
6945 map = isl_map_intersect(map, isl_map_copy(rest));
6946 res = isl_map_union(res, map);
6948 if (i == n - 1)
6949 continue;
6951 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
6952 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
6953 map = isl_pw_aff_eq_map(pa1, pa2);
6954 rest = isl_map_intersect(rest, map);
6957 isl_map_free(rest);
6958 return res;
6961 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6962 * where the function value of "mpa1" is lexicographically less than that
6963 * of "mpa2". "space" is the space of the result.
6964 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6966 * "mpa1" is less than "mpa2" if its i-th element is smaller
6967 * than the i-th element of "mpa2" while all previous elements are
6968 * pairwise equal.
6970 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
6971 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
6972 __isl_take isl_space *space)
6974 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
6975 &isl_pw_aff_lt_map, space);
6978 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6979 * where the function value of "mpa1" is lexicographically less than that
6980 * of "mpa2".
6982 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
6983 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6985 return isl_multi_pw_aff_order_map(mpa1, mpa2,
6986 &isl_multi_pw_aff_lex_lt_map_on_space);
6989 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
6990 * where the function value of "mpa1" is lexicographically greater than that
6991 * of "mpa2". "space" is the space of the result.
6992 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6994 * "mpa1" is greater than "mpa2" if its i-th element is greater
6995 * than the i-th element of "mpa2" while all previous elements are
6996 * pairwise equal.
6998 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
6999 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7000 __isl_take isl_space *space)
7002 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7003 &isl_pw_aff_gt_map, space);
7006 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7007 * where the function value of "mpa1" is lexicographically greater than that
7008 * of "mpa2".
7010 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7011 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7013 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7014 &isl_multi_pw_aff_lex_gt_map_on_space);
7017 /* Compare two isl_affs.
7019 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7020 * than "aff2" and 0 if they are equal.
7022 * The order is fairly arbitrary. We do consider expressions that only involve
7023 * earlier dimensions as "smaller".
7025 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7027 int cmp;
7028 int last1, last2;
7030 if (aff1 == aff2)
7031 return 0;
7033 if (!aff1)
7034 return -1;
7035 if (!aff2)
7036 return 1;
7038 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7039 if (cmp != 0)
7040 return cmp;
7042 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7043 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7044 if (last1 != last2)
7045 return last1 - last2;
7047 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7050 /* Compare two isl_pw_affs.
7052 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7053 * than "pa2" and 0 if they are equal.
7055 * The order is fairly arbitrary. We do consider expressions that only involve
7056 * earlier dimensions as "smaller".
7058 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7059 __isl_keep isl_pw_aff *pa2)
7061 int i;
7062 int cmp;
7064 if (pa1 == pa2)
7065 return 0;
7067 if (!pa1)
7068 return -1;
7069 if (!pa2)
7070 return 1;
7072 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7073 if (cmp != 0)
7074 return cmp;
7076 if (pa1->n != pa2->n)
7077 return pa1->n - pa2->n;
7079 for (i = 0; i < pa1->n; ++i) {
7080 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7081 if (cmp != 0)
7082 return cmp;
7083 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7084 if (cmp != 0)
7085 return cmp;
7088 return 0;
7091 /* Return a piecewise affine expression that is equal to "v" on "domain".
7093 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7094 __isl_take isl_val *v)
7096 isl_space *space;
7097 isl_local_space *ls;
7098 isl_aff *aff;
7100 space = isl_set_get_space(domain);
7101 ls = isl_local_space_from_space(space);
7102 aff = isl_aff_val_on_domain(ls, v);
7104 return isl_pw_aff_alloc(domain, aff);
7107 /* Return a multi affine expression that is equal to "mv" on domain
7108 * space "space".
7110 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7111 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7113 int i, n;
7114 isl_space *space2;
7115 isl_local_space *ls;
7116 isl_multi_aff *ma;
7118 if (!space || !mv)
7119 goto error;
7121 n = isl_multi_val_dim(mv, isl_dim_set);
7122 space2 = isl_multi_val_get_space(mv);
7123 space2 = isl_space_align_params(space2, isl_space_copy(space));
7124 space = isl_space_align_params(space, isl_space_copy(space2));
7125 space = isl_space_map_from_domain_and_range(space, space2);
7126 ma = isl_multi_aff_alloc(isl_space_copy(space));
7127 ls = isl_local_space_from_space(isl_space_domain(space));
7128 for (i = 0; i < n; ++i) {
7129 isl_val *v;
7130 isl_aff *aff;
7132 v = isl_multi_val_get_val(mv, i);
7133 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7134 ma = isl_multi_aff_set_aff(ma, i, aff);
7136 isl_local_space_free(ls);
7138 isl_multi_val_free(mv);
7139 return ma;
7140 error:
7141 isl_space_free(space);
7142 isl_multi_val_free(mv);
7143 return NULL;
7146 /* Return a piecewise multi-affine expression
7147 * that is equal to "mv" on "domain".
7149 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7150 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7152 isl_space *space;
7153 isl_multi_aff *ma;
7155 space = isl_set_get_space(domain);
7156 ma = isl_multi_aff_multi_val_on_space(space, mv);
7158 return isl_pw_multi_aff_alloc(domain, ma);
7161 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7162 * mv is the value that should be attained on each domain set
7163 * res collects the results
7165 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7166 isl_multi_val *mv;
7167 isl_union_pw_multi_aff *res;
7170 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7171 * and add it to data->res.
7173 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7174 void *user)
7176 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7177 isl_pw_multi_aff *pma;
7178 isl_multi_val *mv;
7180 mv = isl_multi_val_copy(data->mv);
7181 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7182 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7184 return data->res ? isl_stat_ok : isl_stat_error;
7187 /* Return a union piecewise multi-affine expression
7188 * that is equal to "mv" on "domain".
7190 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7191 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7193 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7194 isl_space *space;
7196 space = isl_union_set_get_space(domain);
7197 data.res = isl_union_pw_multi_aff_empty(space);
7198 data.mv = mv;
7199 if (isl_union_set_foreach_set(domain,
7200 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7201 data.res = isl_union_pw_multi_aff_free(data.res);
7202 isl_union_set_free(domain);
7203 isl_multi_val_free(mv);
7204 return data.res;
7207 /* Compute the pullback of data->pma by the function represented by "pma2",
7208 * provided the spaces match, and add the results to data->res.
7210 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7212 struct isl_union_pw_multi_aff_bin_data *data = user;
7214 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7215 pma2->dim, isl_dim_out)) {
7216 isl_pw_multi_aff_free(pma2);
7217 return isl_stat_ok;
7220 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7221 isl_pw_multi_aff_copy(data->pma), pma2);
7223 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7224 if (!data->res)
7225 return isl_stat_error;
7227 return isl_stat_ok;
7230 /* Compute the pullback of "upma1" by the function represented by "upma2".
7232 __isl_give isl_union_pw_multi_aff *
7233 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7234 __isl_take isl_union_pw_multi_aff *upma1,
7235 __isl_take isl_union_pw_multi_aff *upma2)
7237 return bin_op(upma1, upma2, &pullback_entry);
7240 /* Check that the domain space of "upa" matches "space".
7242 * Return 0 on success and -1 on error.
7244 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7245 * can in principle never fail since the space "space" is that
7246 * of the isl_multi_union_pw_aff and is a set space such that
7247 * there is no domain space to match.
7249 * We check the parameters and double-check that "space" is
7250 * indeed that of a set.
7252 static int isl_union_pw_aff_check_match_domain_space(
7253 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7255 isl_space *upa_space;
7256 int match;
7258 if (!upa || !space)
7259 return -1;
7261 match = isl_space_is_set(space);
7262 if (match < 0)
7263 return -1;
7264 if (!match)
7265 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7266 "expecting set space", return -1);
7268 upa_space = isl_union_pw_aff_get_space(upa);
7269 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7270 if (match < 0)
7271 goto error;
7272 if (!match)
7273 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7274 "parameters don't match", goto error);
7276 isl_space_free(upa_space);
7277 return 0;
7278 error:
7279 isl_space_free(upa_space);
7280 return -1;
7283 /* Do the parameters of "upa" match those of "space"?
7285 static int isl_union_pw_aff_matching_params(__isl_keep isl_union_pw_aff *upa,
7286 __isl_keep isl_space *space)
7288 isl_space *upa_space;
7289 int match;
7291 if (!upa || !space)
7292 return -1;
7294 upa_space = isl_union_pw_aff_get_space(upa);
7296 match = isl_space_match(space, isl_dim_param, upa_space, isl_dim_param);
7298 isl_space_free(upa_space);
7299 return match;
7302 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7303 * space represents the new parameters.
7304 * res collects the results.
7306 struct isl_union_pw_aff_reset_params_data {
7307 isl_space *space;
7308 isl_union_pw_aff *res;
7311 /* Replace the parameters of "pa" by data->space and
7312 * add the result to data->res.
7314 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7316 struct isl_union_pw_aff_reset_params_data *data = user;
7317 isl_space *space;
7319 space = isl_pw_aff_get_space(pa);
7320 space = isl_space_replace(space, isl_dim_param, data->space);
7321 pa = isl_pw_aff_reset_space(pa, space);
7322 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7324 return data->res ? isl_stat_ok : isl_stat_error;
7327 /* Replace the domain space of "upa" by "space".
7328 * Since a union expression does not have a (single) domain space,
7329 * "space" is necessarily a parameter space.
7331 * Since the order and the names of the parameters determine
7332 * the hash value, we need to create a new hash table.
7334 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7335 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7337 struct isl_union_pw_aff_reset_params_data data = { space };
7338 int match;
7340 match = isl_union_pw_aff_matching_params(upa, space);
7341 if (match < 0)
7342 upa = isl_union_pw_aff_free(upa);
7343 else if (match) {
7344 isl_space_free(space);
7345 return upa;
7348 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7349 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7350 data.res = isl_union_pw_aff_free(data.res);
7352 isl_union_pw_aff_free(upa);
7353 isl_space_free(space);
7354 return data.res;
7357 /* Return the floor of "pa".
7359 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7361 return isl_pw_aff_floor(pa);
7364 /* Given f, return floor(f).
7366 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7367 __isl_take isl_union_pw_aff *upa)
7369 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7372 /* Compute
7374 * upa mod m = upa - m * floor(upa/m)
7376 * with m an integer value.
7378 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7379 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7381 isl_union_pw_aff *res;
7383 if (!upa || !m)
7384 goto error;
7386 if (!isl_val_is_int(m))
7387 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7388 "expecting integer modulo", goto error);
7389 if (!isl_val_is_pos(m))
7390 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7391 "expecting positive modulo", goto error);
7393 res = isl_union_pw_aff_copy(upa);
7394 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7395 upa = isl_union_pw_aff_floor(upa);
7396 upa = isl_union_pw_aff_scale_val(upa, m);
7397 res = isl_union_pw_aff_sub(res, upa);
7399 return res;
7400 error:
7401 isl_val_free(m);
7402 isl_union_pw_aff_free(upa);
7403 return NULL;
7406 /* Internal data structure for isl_union_pw_aff_aff_on_domain.
7407 * "aff" is the symbolic value that the resulting isl_union_pw_aff
7408 * needs to attain.
7409 * "res" collects the results.
7411 struct isl_union_pw_aff_aff_on_domain_data {
7412 isl_aff *aff;
7413 isl_union_pw_aff *res;
7416 /* Construct a piecewise affine expression that is equal to data->aff
7417 * on "domain" and add the result to data->res.
7419 static isl_stat pw_aff_aff_on_domain(__isl_take isl_set *domain, void *user)
7421 struct isl_union_pw_aff_aff_on_domain_data *data = user;
7422 isl_pw_aff *pa;
7423 isl_aff *aff;
7424 int dim;
7426 aff = isl_aff_copy(data->aff);
7427 dim = isl_set_dim(domain, isl_dim_set);
7428 aff = isl_aff_add_dims(aff, isl_dim_in, dim);
7429 aff = isl_aff_reset_domain_space(aff, isl_set_get_space(domain));
7430 pa = isl_pw_aff_alloc(domain, aff);
7431 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7433 return data->res ? isl_stat_ok : isl_stat_error;
7436 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7437 * pos is the output position that needs to be extracted.
7438 * res collects the results.
7440 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7441 int pos;
7442 isl_union_pw_aff *res;
7445 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7446 * (assuming it has such a dimension) and add it to data->res.
7448 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7450 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7451 int n_out;
7452 isl_pw_aff *pa;
7454 if (!pma)
7455 return isl_stat_error;
7457 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7458 if (data->pos >= n_out) {
7459 isl_pw_multi_aff_free(pma);
7460 return isl_stat_ok;
7463 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7464 isl_pw_multi_aff_free(pma);
7466 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7468 return data->res ? isl_stat_ok : isl_stat_error;
7471 /* Extract an isl_union_pw_aff corresponding to
7472 * output dimension "pos" of "upma".
7474 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7475 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7477 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7478 isl_space *space;
7480 if (!upma)
7481 return NULL;
7483 if (pos < 0)
7484 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7485 "cannot extract at negative position", return NULL);
7487 space = isl_union_pw_multi_aff_get_space(upma);
7488 data.res = isl_union_pw_aff_empty(space);
7489 data.pos = pos;
7490 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7491 &get_union_pw_aff, &data) < 0)
7492 data.res = isl_union_pw_aff_free(data.res);
7494 return data.res;
7497 /* Return a union piecewise affine expression
7498 * that is equal to "aff" on "domain".
7500 * Construct an isl_pw_aff on each of the sets in "domain" and
7501 * collect the results.
7503 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7504 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7506 struct isl_union_pw_aff_aff_on_domain_data data;
7507 isl_space *space;
7509 if (!domain || !aff)
7510 goto error;
7511 if (!isl_local_space_is_params(aff->ls))
7512 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
7513 "expecting parametric expression", goto error);
7515 space = isl_union_set_get_space(domain);
7516 data.res = isl_union_pw_aff_empty(space);
7517 data.aff = aff;
7518 if (isl_union_set_foreach_set(domain, &pw_aff_aff_on_domain, &data) < 0)
7519 data.res = isl_union_pw_aff_free(data.res);
7520 isl_union_set_free(domain);
7521 isl_aff_free(aff);
7522 return data.res;
7523 error:
7524 isl_union_set_free(domain);
7525 isl_aff_free(aff);
7526 return NULL;
7529 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7530 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7531 * "res" collects the results.
7533 struct isl_union_pw_aff_val_on_domain_data {
7534 isl_val *v;
7535 isl_union_pw_aff *res;
7538 /* Construct a piecewise affine expression that is equal to data->v
7539 * on "domain" and add the result to data->res.
7541 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7543 struct isl_union_pw_aff_val_on_domain_data *data = user;
7544 isl_pw_aff *pa;
7545 isl_val *v;
7547 v = isl_val_copy(data->v);
7548 pa = isl_pw_aff_val_on_domain(domain, v);
7549 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7551 return data->res ? isl_stat_ok : isl_stat_error;
7554 /* Return a union piecewise affine expression
7555 * that is equal to "v" on "domain".
7557 * Construct an isl_pw_aff on each of the sets in "domain" and
7558 * collect the results.
7560 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7561 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7563 struct isl_union_pw_aff_val_on_domain_data data;
7564 isl_space *space;
7566 space = isl_union_set_get_space(domain);
7567 data.res = isl_union_pw_aff_empty(space);
7568 data.v = v;
7569 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7570 data.res = isl_union_pw_aff_free(data.res);
7571 isl_union_set_free(domain);
7572 isl_val_free(v);
7573 return data.res;
7576 /* Construct a piecewise multi affine expression
7577 * that is equal to "pa" and add it to upma.
7579 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7580 void *user)
7582 isl_union_pw_multi_aff **upma = user;
7583 isl_pw_multi_aff *pma;
7585 pma = isl_pw_multi_aff_from_pw_aff(pa);
7586 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7588 return *upma ? isl_stat_ok : isl_stat_error;
7591 /* Construct and return a union piecewise multi affine expression
7592 * that is equal to the given union piecewise affine expression.
7594 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7595 __isl_take isl_union_pw_aff *upa)
7597 isl_space *space;
7598 isl_union_pw_multi_aff *upma;
7600 if (!upa)
7601 return NULL;
7603 space = isl_union_pw_aff_get_space(upa);
7604 upma = isl_union_pw_multi_aff_empty(space);
7606 if (isl_union_pw_aff_foreach_pw_aff(upa,
7607 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7608 upma = isl_union_pw_multi_aff_free(upma);
7610 isl_union_pw_aff_free(upa);
7611 return upma;
7614 /* Compute the set of elements in the domain of "pa" where it is zero and
7615 * add this set to "uset".
7617 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7619 isl_union_set **uset = (isl_union_set **)user;
7621 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7623 return *uset ? isl_stat_ok : isl_stat_error;
7626 /* Return a union set containing those elements in the domain
7627 * of "upa" where it is zero.
7629 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7630 __isl_take isl_union_pw_aff *upa)
7632 isl_union_set *zero;
7634 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7635 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7636 zero = isl_union_set_free(zero);
7638 isl_union_pw_aff_free(upa);
7639 return zero;
7642 /* Convert "pa" to an isl_map and add it to *umap.
7644 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7646 isl_union_map **umap = user;
7647 isl_map *map;
7649 map = isl_map_from_pw_aff(pa);
7650 *umap = isl_union_map_add_map(*umap, map);
7652 return *umap ? isl_stat_ok : isl_stat_error;
7655 /* Construct a union map mapping the domain of the union
7656 * piecewise affine expression to its range, with the single output dimension
7657 * equated to the corresponding affine expressions on their cells.
7659 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7660 __isl_take isl_union_pw_aff *upa)
7662 isl_space *space;
7663 isl_union_map *umap;
7665 if (!upa)
7666 return NULL;
7668 space = isl_union_pw_aff_get_space(upa);
7669 umap = isl_union_map_empty(space);
7671 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7672 &umap) < 0)
7673 umap = isl_union_map_free(umap);
7675 isl_union_pw_aff_free(upa);
7676 return umap;
7679 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7680 * upma is the function that is plugged in.
7681 * pa is the current part of the function in which upma is plugged in.
7682 * res collects the results.
7684 struct isl_union_pw_aff_pullback_upma_data {
7685 isl_union_pw_multi_aff *upma;
7686 isl_pw_aff *pa;
7687 isl_union_pw_aff *res;
7690 /* Check if "pma" can be plugged into data->pa.
7691 * If so, perform the pullback and add the result to data->res.
7693 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7695 struct isl_union_pw_aff_pullback_upma_data *data = user;
7696 isl_pw_aff *pa;
7698 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7699 pma->dim, isl_dim_out)) {
7700 isl_pw_multi_aff_free(pma);
7701 return isl_stat_ok;
7704 pa = isl_pw_aff_copy(data->pa);
7705 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7707 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7709 return data->res ? isl_stat_ok : isl_stat_error;
7712 /* Check if any of the elements of data->upma can be plugged into pa,
7713 * add if so add the result to data->res.
7715 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7717 struct isl_union_pw_aff_pullback_upma_data *data = user;
7718 isl_stat r;
7720 data->pa = pa;
7721 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7722 &pa_pb_pma, data);
7723 isl_pw_aff_free(pa);
7725 return r;
7728 /* Compute the pullback of "upa" by the function represented by "upma".
7729 * In other words, plug in "upma" in "upa". The result contains
7730 * expressions defined over the domain space of "upma".
7732 * Run over all pairs of elements in "upa" and "upma", perform
7733 * the pullback when appropriate and collect the results.
7734 * If the hash value were based on the domain space rather than
7735 * the function space, then we could run through all elements
7736 * of "upma" and directly pick out the corresponding element of "upa".
7738 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7739 __isl_take isl_union_pw_aff *upa,
7740 __isl_take isl_union_pw_multi_aff *upma)
7742 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7743 isl_space *space;
7745 space = isl_union_pw_multi_aff_get_space(upma);
7746 upa = isl_union_pw_aff_align_params(upa, space);
7747 space = isl_union_pw_aff_get_space(upa);
7748 upma = isl_union_pw_multi_aff_align_params(upma, space);
7750 if (!upa || !upma)
7751 goto error;
7753 data.upma = upma;
7754 data.res = isl_union_pw_aff_alloc_same_size(upa);
7755 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7756 data.res = isl_union_pw_aff_free(data.res);
7758 isl_union_pw_aff_free(upa);
7759 isl_union_pw_multi_aff_free(upma);
7760 return data.res;
7761 error:
7762 isl_union_pw_aff_free(upa);
7763 isl_union_pw_multi_aff_free(upma);
7764 return NULL;
7767 #undef BASE
7768 #define BASE union_pw_aff
7769 #undef DOMBASE
7770 #define DOMBASE union_set
7772 #define NO_MOVE_DIMS
7773 #define NO_DIMS
7774 #define NO_DOMAIN
7775 #define NO_PRODUCT
7776 #define NO_SPLICE
7777 #define NO_ZERO
7778 #define NO_IDENTITY
7779 #define NO_GIST
7781 #include <isl_multi_templ.c>
7782 #include <isl_multi_apply_set.c>
7783 #include <isl_multi_apply_union_set.c>
7784 #include <isl_multi_coalesce.c>
7785 #include <isl_multi_floor.c>
7786 #include <isl_multi_gist.c>
7787 #include <isl_multi_intersect.c>
7789 /* Construct a multiple union piecewise affine expression
7790 * in the given space with value zero in each of the output dimensions.
7792 * Since there is no canonical zero value for
7793 * a union piecewise affine expression, we can only construct
7794 * zero-dimensional "zero" value.
7796 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
7797 __isl_take isl_space *space)
7799 if (!space)
7800 return NULL;
7802 if (!isl_space_is_set(space))
7803 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7804 "expecting set space", goto error);
7805 if (isl_space_dim(space , isl_dim_out) != 0)
7806 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7807 "expecting 0D space", goto error);
7809 return isl_multi_union_pw_aff_alloc(space);
7810 error:
7811 isl_space_free(space);
7812 return NULL;
7815 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7816 * with the actual sum on the shared domain and
7817 * the defined expression on the symmetric difference of the domains.
7819 * We simply iterate over the elements in both arguments and
7820 * call isl_union_pw_aff_union_add on each of them.
7822 static __isl_give isl_multi_union_pw_aff *
7823 isl_multi_union_pw_aff_union_add_aligned(
7824 __isl_take isl_multi_union_pw_aff *mupa1,
7825 __isl_take isl_multi_union_pw_aff *mupa2)
7827 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
7828 &isl_union_pw_aff_union_add);
7831 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
7832 * with the actual sum on the shared domain and
7833 * the defined expression on the symmetric difference of the domains.
7835 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
7836 __isl_take isl_multi_union_pw_aff *mupa1,
7837 __isl_take isl_multi_union_pw_aff *mupa2)
7839 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
7840 &isl_multi_union_pw_aff_union_add_aligned);
7843 /* Construct and return a multi union piecewise affine expression
7844 * that is equal to the given multi affine expression.
7846 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
7847 __isl_take isl_multi_aff *ma)
7849 isl_multi_pw_aff *mpa;
7851 mpa = isl_multi_pw_aff_from_multi_aff(ma);
7852 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
7855 /* Construct and return a multi union piecewise affine expression
7856 * that is equal to the given multi piecewise affine expression.
7858 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
7859 __isl_take isl_multi_pw_aff *mpa)
7861 int i, n;
7862 isl_space *space;
7863 isl_multi_union_pw_aff *mupa;
7865 if (!mpa)
7866 return NULL;
7868 space = isl_multi_pw_aff_get_space(mpa);
7869 space = isl_space_range(space);
7870 mupa = isl_multi_union_pw_aff_alloc(space);
7872 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
7873 for (i = 0; i < n; ++i) {
7874 isl_pw_aff *pa;
7875 isl_union_pw_aff *upa;
7877 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
7878 upa = isl_union_pw_aff_from_pw_aff(pa);
7879 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7882 isl_multi_pw_aff_free(mpa);
7884 return mupa;
7887 /* Extract the range space of "pma" and assign it to *space.
7888 * If *space has already been set (through a previous call to this function),
7889 * then check that the range space is the same.
7891 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
7893 isl_space **space = user;
7894 isl_space *pma_space;
7895 isl_bool equal;
7897 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
7898 isl_pw_multi_aff_free(pma);
7900 if (!pma_space)
7901 return isl_stat_error;
7902 if (!*space) {
7903 *space = pma_space;
7904 return isl_stat_ok;
7907 equal = isl_space_is_equal(pma_space, *space);
7908 isl_space_free(pma_space);
7910 if (equal < 0)
7911 return isl_stat_error;
7912 if (!equal)
7913 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
7914 "range spaces not the same", return isl_stat_error);
7915 return isl_stat_ok;
7918 /* Construct and return a multi union piecewise affine expression
7919 * that is equal to the given union piecewise multi affine expression.
7921 * In order to be able to perform the conversion, the input
7922 * needs to be non-empty and may only involve a single range space.
7924 __isl_give isl_multi_union_pw_aff *
7925 isl_multi_union_pw_aff_from_union_pw_multi_aff(
7926 __isl_take isl_union_pw_multi_aff *upma)
7928 isl_space *space = NULL;
7929 isl_multi_union_pw_aff *mupa;
7930 int i, n;
7932 if (!upma)
7933 return NULL;
7934 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
7935 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7936 "cannot extract range space from empty input",
7937 goto error);
7938 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
7939 &space) < 0)
7940 goto error;
7942 if (!space)
7943 goto error;
7945 n = isl_space_dim(space, isl_dim_set);
7946 mupa = isl_multi_union_pw_aff_alloc(space);
7948 for (i = 0; i < n; ++i) {
7949 isl_union_pw_aff *upa;
7951 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
7952 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
7955 isl_union_pw_multi_aff_free(upma);
7956 return mupa;
7957 error:
7958 isl_space_free(space);
7959 isl_union_pw_multi_aff_free(upma);
7960 return NULL;
7963 /* Try and create an isl_multi_union_pw_aff that is equivalent
7964 * to the given isl_union_map.
7965 * The isl_union_map is required to be single-valued in each space.
7966 * Moreover, it cannot be empty and all range spaces need to be the same.
7967 * Otherwise, an error is produced.
7969 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
7970 __isl_take isl_union_map *umap)
7972 isl_union_pw_multi_aff *upma;
7974 upma = isl_union_pw_multi_aff_from_union_map(umap);
7975 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
7978 /* Return a multiple union piecewise affine expression
7979 * that is equal to "mv" on "domain", assuming "domain" and "mv"
7980 * have been aligned.
7982 static __isl_give isl_multi_union_pw_aff *
7983 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
7984 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7986 int i, n;
7987 isl_space *space;
7988 isl_multi_union_pw_aff *mupa;
7990 if (!domain || !mv)
7991 goto error;
7993 n = isl_multi_val_dim(mv, isl_dim_set);
7994 space = isl_multi_val_get_space(mv);
7995 mupa = isl_multi_union_pw_aff_alloc(space);
7996 for (i = 0; i < n; ++i) {
7997 isl_val *v;
7998 isl_union_pw_aff *upa;
8000 v = isl_multi_val_get_val(mv, i);
8001 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8003 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8006 isl_union_set_free(domain);
8007 isl_multi_val_free(mv);
8008 return mupa;
8009 error:
8010 isl_union_set_free(domain);
8011 isl_multi_val_free(mv);
8012 return NULL;
8015 /* Return a multiple union piecewise affine expression
8016 * that is equal to "mv" on "domain".
8018 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8019 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8021 if (!domain || !mv)
8022 goto error;
8023 if (isl_space_match(domain->dim, isl_dim_param,
8024 mv->space, isl_dim_param))
8025 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8026 domain, mv);
8027 domain = isl_union_set_align_params(domain,
8028 isl_multi_val_get_space(mv));
8029 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8030 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8031 error:
8032 isl_union_set_free(domain);
8033 isl_multi_val_free(mv);
8034 return NULL;
8037 /* Return a multiple union piecewise affine expression
8038 * that is equal to "ma" on "domain", assuming "domain" and "ma"
8039 * have been aligned.
8041 static __isl_give isl_multi_union_pw_aff *
8042 isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8043 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8045 int i, n;
8046 isl_space *space;
8047 isl_multi_union_pw_aff *mupa;
8049 if (!domain || !ma)
8050 goto error;
8052 n = isl_multi_aff_dim(ma, isl_dim_set);
8053 space = isl_multi_aff_get_space(ma);
8054 mupa = isl_multi_union_pw_aff_alloc(space);
8055 for (i = 0; i < n; ++i) {
8056 isl_aff *aff;
8057 isl_union_pw_aff *upa;
8059 aff = isl_multi_aff_get_aff(ma, i);
8060 upa = isl_union_pw_aff_aff_on_domain(isl_union_set_copy(domain),
8061 aff);
8062 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8065 isl_union_set_free(domain);
8066 isl_multi_aff_free(ma);
8067 return mupa;
8068 error:
8069 isl_union_set_free(domain);
8070 isl_multi_aff_free(ma);
8071 return NULL;
8074 /* Return a multiple union piecewise affine expression
8075 * that is equal to "ma" on "domain".
8077 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8078 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8080 if (!domain || !ma)
8081 goto error;
8082 if (isl_space_match(domain->dim, isl_dim_param,
8083 ma->space, isl_dim_param))
8084 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(
8085 domain, ma);
8086 domain = isl_union_set_align_params(domain,
8087 isl_multi_aff_get_space(ma));
8088 ma = isl_multi_aff_align_params(ma, isl_union_set_get_space(domain));
8089 return isl_multi_union_pw_aff_multi_aff_on_domain_aligned(domain, ma);
8090 error:
8091 isl_union_set_free(domain);
8092 isl_multi_aff_free(ma);
8093 return NULL;
8096 /* Return a union set containing those elements in the domains
8097 * of the elements of "mupa" where they are all zero.
8099 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8100 __isl_take isl_multi_union_pw_aff *mupa)
8102 int i, n;
8103 isl_union_pw_aff *upa;
8104 isl_union_set *zero;
8106 if (!mupa)
8107 return NULL;
8109 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8110 if (n == 0)
8111 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8112 "cannot determine zero set "
8113 "of zero-dimensional function", goto error);
8115 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8116 zero = isl_union_pw_aff_zero_union_set(upa);
8118 for (i = 1; i < n; ++i) {
8119 isl_union_set *zero_i;
8121 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8122 zero_i = isl_union_pw_aff_zero_union_set(upa);
8124 zero = isl_union_set_intersect(zero, zero_i);
8127 isl_multi_union_pw_aff_free(mupa);
8128 return zero;
8129 error:
8130 isl_multi_union_pw_aff_free(mupa);
8131 return NULL;
8134 /* Construct a union map mapping the shared domain
8135 * of the union piecewise affine expressions to the range of "mupa"
8136 * with each dimension in the range equated to the
8137 * corresponding union piecewise affine expression.
8139 * The input cannot be zero-dimensional as there is
8140 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8142 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8143 __isl_take isl_multi_union_pw_aff *mupa)
8145 int i, n;
8146 isl_space *space;
8147 isl_union_map *umap;
8148 isl_union_pw_aff *upa;
8150 if (!mupa)
8151 return NULL;
8153 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8154 if (n == 0)
8155 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8156 "cannot determine domain of zero-dimensional "
8157 "isl_multi_union_pw_aff", goto error);
8159 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8160 umap = isl_union_map_from_union_pw_aff(upa);
8162 for (i = 1; i < n; ++i) {
8163 isl_union_map *umap_i;
8165 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8166 umap_i = isl_union_map_from_union_pw_aff(upa);
8167 umap = isl_union_map_flat_range_product(umap, umap_i);
8170 space = isl_multi_union_pw_aff_get_space(mupa);
8171 umap = isl_union_map_reset_range_space(umap, space);
8173 isl_multi_union_pw_aff_free(mupa);
8174 return umap;
8175 error:
8176 isl_multi_union_pw_aff_free(mupa);
8177 return NULL;
8180 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8181 * "range" is the space from which to set the range space.
8182 * "res" collects the results.
8184 struct isl_union_pw_multi_aff_reset_range_space_data {
8185 isl_space *range;
8186 isl_union_pw_multi_aff *res;
8189 /* Replace the range space of "pma" by the range space of data->range and
8190 * add the result to data->res.
8192 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8194 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8195 isl_space *space;
8197 space = isl_pw_multi_aff_get_space(pma);
8198 space = isl_space_domain(space);
8199 space = isl_space_extend_domain_with_range(space,
8200 isl_space_copy(data->range));
8201 pma = isl_pw_multi_aff_reset_space(pma, space);
8202 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8204 return data->res ? isl_stat_ok : isl_stat_error;
8207 /* Replace the range space of all the piecewise affine expressions in "upma" by
8208 * the range space of "space".
8210 * This assumes that all these expressions have the same output dimension.
8212 * Since the spaces of the expressions change, so do their hash values.
8213 * We therefore need to create a new isl_union_pw_multi_aff.
8214 * Note that the hash value is currently computed based on the entire
8215 * space even though there can only be a single expression with a given
8216 * domain space.
8218 static __isl_give isl_union_pw_multi_aff *
8219 isl_union_pw_multi_aff_reset_range_space(
8220 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8222 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8223 isl_space *space_upma;
8225 space_upma = isl_union_pw_multi_aff_get_space(upma);
8226 data.res = isl_union_pw_multi_aff_empty(space_upma);
8227 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8228 &reset_range_space, &data) < 0)
8229 data.res = isl_union_pw_multi_aff_free(data.res);
8231 isl_space_free(space);
8232 isl_union_pw_multi_aff_free(upma);
8233 return data.res;
8236 /* Construct and return a union piecewise multi affine expression
8237 * that is equal to the given multi union piecewise affine expression.
8239 * In order to be able to perform the conversion, the input
8240 * needs to have a least one output dimension.
8242 __isl_give isl_union_pw_multi_aff *
8243 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8244 __isl_take isl_multi_union_pw_aff *mupa)
8246 int i, n;
8247 isl_space *space;
8248 isl_union_pw_multi_aff *upma;
8249 isl_union_pw_aff *upa;
8251 if (!mupa)
8252 return NULL;
8254 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8255 if (n == 0)
8256 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8257 "cannot determine domain of zero-dimensional "
8258 "isl_multi_union_pw_aff", goto error);
8260 space = isl_multi_union_pw_aff_get_space(mupa);
8261 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8262 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8264 for (i = 1; i < n; ++i) {
8265 isl_union_pw_multi_aff *upma_i;
8267 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8268 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8269 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8272 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8274 isl_multi_union_pw_aff_free(mupa);
8275 return upma;
8276 error:
8277 isl_multi_union_pw_aff_free(mupa);
8278 return NULL;
8281 /* Intersect the range of "mupa" with "range".
8282 * That is, keep only those domain elements that have a function value
8283 * in "range".
8285 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8286 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8288 isl_union_pw_multi_aff *upma;
8289 isl_union_set *domain;
8290 isl_space *space;
8291 int n;
8292 int match;
8294 if (!mupa || !range)
8295 goto error;
8297 space = isl_set_get_space(range);
8298 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8299 space, isl_dim_set);
8300 isl_space_free(space);
8301 if (match < 0)
8302 goto error;
8303 if (!match)
8304 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8305 "space don't match", goto error);
8306 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8307 if (n == 0)
8308 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8309 "cannot intersect range of zero-dimensional "
8310 "isl_multi_union_pw_aff", goto error);
8312 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8313 isl_multi_union_pw_aff_copy(mupa));
8314 domain = isl_union_set_from_set(range);
8315 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8316 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8318 return mupa;
8319 error:
8320 isl_multi_union_pw_aff_free(mupa);
8321 isl_set_free(range);
8322 return NULL;
8325 /* Return the shared domain of the elements of "mupa".
8327 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8328 __isl_take isl_multi_union_pw_aff *mupa)
8330 int i, n;
8331 isl_union_pw_aff *upa;
8332 isl_union_set *dom;
8334 if (!mupa)
8335 return NULL;
8337 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8338 if (n == 0)
8339 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8340 "cannot determine domain", goto error);
8342 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8343 dom = isl_union_pw_aff_domain(upa);
8344 for (i = 1; i < n; ++i) {
8345 isl_union_set *dom_i;
8347 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8348 dom_i = isl_union_pw_aff_domain(upa);
8349 dom = isl_union_set_intersect(dom, dom_i);
8352 isl_multi_union_pw_aff_free(mupa);
8353 return dom;
8354 error:
8355 isl_multi_union_pw_aff_free(mupa);
8356 return NULL;
8359 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8360 * In particular, the spaces have been aligned.
8361 * The result is defined over the shared domain of the elements of "mupa"
8363 * We first extract the parametric constant part of "aff" and
8364 * define that over the shared domain.
8365 * Then we iterate over all input dimensions of "aff" and add the corresponding
8366 * multiples of the elements of "mupa".
8367 * Finally, we consider the integer divisions, calling the function
8368 * recursively to obtain an isl_union_pw_aff corresponding to the
8369 * integer division argument.
8371 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8372 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8374 int i, n_in, n_div;
8375 isl_union_pw_aff *upa;
8376 isl_union_set *uset;
8377 isl_val *v;
8378 isl_aff *cst;
8380 n_in = isl_aff_dim(aff, isl_dim_in);
8381 n_div = isl_aff_dim(aff, isl_dim_div);
8383 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8384 cst = isl_aff_copy(aff);
8385 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8386 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8387 cst = isl_aff_project_domain_on_params(cst);
8388 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8390 for (i = 0; i < n_in; ++i) {
8391 isl_union_pw_aff *upa_i;
8393 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8394 continue;
8395 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8396 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8397 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8398 upa = isl_union_pw_aff_add(upa, upa_i);
8401 for (i = 0; i < n_div; ++i) {
8402 isl_aff *div;
8403 isl_union_pw_aff *upa_i;
8405 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8406 continue;
8407 div = isl_aff_get_div(aff, i);
8408 upa_i = multi_union_pw_aff_apply_aff(
8409 isl_multi_union_pw_aff_copy(mupa), div);
8410 upa_i = isl_union_pw_aff_floor(upa_i);
8411 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8412 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8413 upa = isl_union_pw_aff_add(upa, upa_i);
8416 isl_multi_union_pw_aff_free(mupa);
8417 isl_aff_free(aff);
8419 return upa;
8422 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8423 * with the domain of "aff".
8424 * Furthermore, the dimension of this space needs to be greater than zero.
8425 * The result is defined over the shared domain of the elements of "mupa"
8427 * We perform these checks and then hand over control to
8428 * multi_union_pw_aff_apply_aff.
8430 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8431 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8433 isl_space *space1, *space2;
8434 int equal;
8436 mupa = isl_multi_union_pw_aff_align_params(mupa,
8437 isl_aff_get_space(aff));
8438 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8439 if (!mupa || !aff)
8440 goto error;
8442 space1 = isl_multi_union_pw_aff_get_space(mupa);
8443 space2 = isl_aff_get_domain_space(aff);
8444 equal = isl_space_is_equal(space1, space2);
8445 isl_space_free(space1);
8446 isl_space_free(space2);
8447 if (equal < 0)
8448 goto error;
8449 if (!equal)
8450 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8451 "spaces don't match", goto error);
8452 if (isl_aff_dim(aff, isl_dim_in) == 0)
8453 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8454 "cannot determine domains", goto error);
8456 return multi_union_pw_aff_apply_aff(mupa, aff);
8457 error:
8458 isl_multi_union_pw_aff_free(mupa);
8459 isl_aff_free(aff);
8460 return NULL;
8463 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8464 * with the domain of "ma".
8465 * Furthermore, the dimension of this space needs to be greater than zero,
8466 * unless the dimension of the target space of "ma" is also zero.
8467 * The result is defined over the shared domain of the elements of "mupa"
8469 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8470 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8472 isl_space *space1, *space2;
8473 isl_multi_union_pw_aff *res;
8474 int equal;
8475 int i, n_out;
8477 mupa = isl_multi_union_pw_aff_align_params(mupa,
8478 isl_multi_aff_get_space(ma));
8479 ma = isl_multi_aff_align_params(ma,
8480 isl_multi_union_pw_aff_get_space(mupa));
8481 if (!mupa || !ma)
8482 goto error;
8484 space1 = isl_multi_union_pw_aff_get_space(mupa);
8485 space2 = isl_multi_aff_get_domain_space(ma);
8486 equal = isl_space_is_equal(space1, space2);
8487 isl_space_free(space1);
8488 isl_space_free(space2);
8489 if (equal < 0)
8490 goto error;
8491 if (!equal)
8492 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8493 "spaces don't match", goto error);
8494 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8495 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8496 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8497 "cannot determine domains", goto error);
8499 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8500 res = isl_multi_union_pw_aff_alloc(space1);
8502 for (i = 0; i < n_out; ++i) {
8503 isl_aff *aff;
8504 isl_union_pw_aff *upa;
8506 aff = isl_multi_aff_get_aff(ma, i);
8507 upa = multi_union_pw_aff_apply_aff(
8508 isl_multi_union_pw_aff_copy(mupa), aff);
8509 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8512 isl_multi_aff_free(ma);
8513 isl_multi_union_pw_aff_free(mupa);
8514 return res;
8515 error:
8516 isl_multi_union_pw_aff_free(mupa);
8517 isl_multi_aff_free(ma);
8518 return NULL;
8521 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8522 * with the domain of "pa".
8523 * Furthermore, the dimension of this space needs to be greater than zero.
8524 * The result is defined over the shared domain of the elements of "mupa"
8526 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8527 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8529 int i;
8530 int equal;
8531 isl_space *space, *space2;
8532 isl_union_pw_aff *upa;
8534 mupa = isl_multi_union_pw_aff_align_params(mupa,
8535 isl_pw_aff_get_space(pa));
8536 pa = isl_pw_aff_align_params(pa,
8537 isl_multi_union_pw_aff_get_space(mupa));
8538 if (!mupa || !pa)
8539 goto error;
8541 space = isl_multi_union_pw_aff_get_space(mupa);
8542 space2 = isl_pw_aff_get_domain_space(pa);
8543 equal = isl_space_is_equal(space, space2);
8544 isl_space_free(space);
8545 isl_space_free(space2);
8546 if (equal < 0)
8547 goto error;
8548 if (!equal)
8549 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8550 "spaces don't match", goto error);
8551 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8552 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8553 "cannot determine domains", goto error);
8555 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8556 upa = isl_union_pw_aff_empty(space);
8558 for (i = 0; i < pa->n; ++i) {
8559 isl_aff *aff;
8560 isl_set *domain;
8561 isl_multi_union_pw_aff *mupa_i;
8562 isl_union_pw_aff *upa_i;
8564 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8565 domain = isl_set_copy(pa->p[i].set);
8566 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8567 aff = isl_aff_copy(pa->p[i].aff);
8568 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8569 upa = isl_union_pw_aff_union_add(upa, upa_i);
8572 isl_multi_union_pw_aff_free(mupa);
8573 isl_pw_aff_free(pa);
8574 return upa;
8575 error:
8576 isl_multi_union_pw_aff_free(mupa);
8577 isl_pw_aff_free(pa);
8578 return NULL;
8581 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8582 * with the domain of "pma".
8583 * Furthermore, the dimension of this space needs to be greater than zero,
8584 * unless the dimension of the target space of "pma" is also zero.
8585 * The result is defined over the shared domain of the elements of "mupa"
8587 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8588 __isl_take isl_multi_union_pw_aff *mupa,
8589 __isl_take isl_pw_multi_aff *pma)
8591 isl_space *space1, *space2;
8592 isl_multi_union_pw_aff *res;
8593 int equal;
8594 int i, n_out;
8596 mupa = isl_multi_union_pw_aff_align_params(mupa,
8597 isl_pw_multi_aff_get_space(pma));
8598 pma = isl_pw_multi_aff_align_params(pma,
8599 isl_multi_union_pw_aff_get_space(mupa));
8600 if (!mupa || !pma)
8601 goto error;
8603 space1 = isl_multi_union_pw_aff_get_space(mupa);
8604 space2 = isl_pw_multi_aff_get_domain_space(pma);
8605 equal = isl_space_is_equal(space1, space2);
8606 isl_space_free(space1);
8607 isl_space_free(space2);
8608 if (equal < 0)
8609 goto error;
8610 if (!equal)
8611 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8612 "spaces don't match", goto error);
8613 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8614 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8615 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8616 "cannot determine domains", goto error);
8618 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8619 res = isl_multi_union_pw_aff_alloc(space1);
8621 for (i = 0; i < n_out; ++i) {
8622 isl_pw_aff *pa;
8623 isl_union_pw_aff *upa;
8625 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8626 upa = isl_multi_union_pw_aff_apply_pw_aff(
8627 isl_multi_union_pw_aff_copy(mupa), pa);
8628 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8631 isl_pw_multi_aff_free(pma);
8632 isl_multi_union_pw_aff_free(mupa);
8633 return res;
8634 error:
8635 isl_multi_union_pw_aff_free(mupa);
8636 isl_pw_multi_aff_free(pma);
8637 return NULL;
8640 /* Compute the pullback of "mupa" by the function represented by "upma".
8641 * In other words, plug in "upma" in "mupa". The result contains
8642 * expressions defined over the domain space of "upma".
8644 * Run over all elements of "mupa" and plug in "upma" in each of them.
8646 __isl_give isl_multi_union_pw_aff *
8647 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8648 __isl_take isl_multi_union_pw_aff *mupa,
8649 __isl_take isl_union_pw_multi_aff *upma)
8651 int i, n;
8653 mupa = isl_multi_union_pw_aff_align_params(mupa,
8654 isl_union_pw_multi_aff_get_space(upma));
8655 upma = isl_union_pw_multi_aff_align_params(upma,
8656 isl_multi_union_pw_aff_get_space(mupa));
8657 if (!mupa || !upma)
8658 goto error;
8660 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8661 for (i = 0; i < n; ++i) {
8662 isl_union_pw_aff *upa;
8664 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8665 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8666 isl_union_pw_multi_aff_copy(upma));
8667 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8670 isl_union_pw_multi_aff_free(upma);
8671 return mupa;
8672 error:
8673 isl_multi_union_pw_aff_free(mupa);
8674 isl_union_pw_multi_aff_free(upma);
8675 return NULL;
8678 /* Extract the sequence of elements in "mupa" with domain space "space"
8679 * (ignoring parameters).
8681 * For the elements of "mupa" that are not defined on the specified space,
8682 * the corresponding element in the result is empty.
8684 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
8685 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
8687 int i, n;
8688 isl_space *space_mpa = NULL;
8689 isl_multi_pw_aff *mpa;
8691 if (!mupa || !space)
8692 goto error;
8694 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
8695 if (!isl_space_match(space_mpa, isl_dim_param, space, isl_dim_param)) {
8696 space = isl_space_drop_dims(space, isl_dim_param,
8697 0, isl_space_dim(space, isl_dim_param));
8698 space = isl_space_align_params(space,
8699 isl_space_copy(space_mpa));
8700 if (!space)
8701 goto error;
8703 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
8704 space_mpa);
8705 mpa = isl_multi_pw_aff_alloc(space_mpa);
8707 space = isl_space_from_domain(space);
8708 space = isl_space_add_dims(space, isl_dim_out, 1);
8709 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8710 for (i = 0; i < n; ++i) {
8711 isl_union_pw_aff *upa;
8712 isl_pw_aff *pa;
8714 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8715 pa = isl_union_pw_aff_extract_pw_aff(upa,
8716 isl_space_copy(space));
8717 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
8718 isl_union_pw_aff_free(upa);
8721 isl_space_free(space);
8722 return mpa;
8723 error:
8724 isl_space_free(space_mpa);
8725 isl_space_free(space);
8726 return NULL;