.gitignore: add piplib
[isl.git] / isl_aff.c
blob3d3ff597085473f35b65fcd801f9dc92a40fa5b1
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
25 #include <isl/id.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE 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 /* Return an affine expression that is equal to the parameter
229 * in the domain space "space" with identifier "id".
231 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
232 __isl_take isl_space *space, __isl_take isl_id *id)
234 int pos;
235 isl_local_space *ls;
237 if (!space || !id)
238 goto error;
239 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
240 if (pos < 0)
241 isl_die(isl_space_get_ctx(space), isl_error_invalid,
242 "parameter not found in space", goto error);
243 isl_id_free(id);
244 ls = isl_local_space_from_space(space);
245 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
246 error:
247 isl_space_free(space);
248 isl_id_free(id);
249 return NULL;
252 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
254 if (!aff)
255 return NULL;
257 aff->ref++;
258 return aff;
261 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
263 if (!aff)
264 return NULL;
266 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
267 isl_vec_copy(aff->v));
270 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
272 if (!aff)
273 return NULL;
275 if (aff->ref == 1)
276 return aff;
277 aff->ref--;
278 return isl_aff_dup(aff);
281 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
283 if (!aff)
284 return NULL;
286 if (--aff->ref > 0)
287 return NULL;
289 isl_local_space_free(aff->ls);
290 isl_vec_free(aff->v);
292 free(aff);
294 return NULL;
297 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
299 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
302 /* Return a hash value that digests "aff".
304 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
306 uint32_t hash, ls_hash, v_hash;
308 if (!aff)
309 return 0;
311 hash = isl_hash_init();
312 ls_hash = isl_local_space_get_hash(aff->ls);
313 isl_hash_hash(hash, ls_hash);
314 v_hash = isl_vec_get_hash(aff->v);
315 isl_hash_hash(hash, v_hash);
317 return hash;
320 /* Externally, an isl_aff has a map space, but internally, the
321 * ls field corresponds to the domain of that space.
323 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
325 if (!aff)
326 return 0;
327 if (type == isl_dim_out)
328 return 1;
329 if (type == isl_dim_in)
330 type = isl_dim_set;
331 return isl_local_space_dim(aff->ls, type);
334 /* Return the position of the dimension of the given type and name
335 * in "aff".
336 * Return -1 if no such dimension can be found.
338 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
339 const char *name)
341 if (!aff)
342 return -1;
343 if (type == isl_dim_out)
344 return -1;
345 if (type == isl_dim_in)
346 type = isl_dim_set;
347 return isl_local_space_find_dim_by_name(aff->ls, type, name);
350 /* Return the domain space of "aff".
352 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
354 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
357 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
359 return isl_space_copy(isl_aff_peek_domain_space(aff));
362 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
364 isl_space *space;
365 if (!aff)
366 return NULL;
367 space = isl_local_space_get_space(aff->ls);
368 space = isl_space_from_domain(space);
369 space = isl_space_add_dims(space, isl_dim_out, 1);
370 return space;
373 __isl_give isl_local_space *isl_aff_get_domain_local_space(
374 __isl_keep isl_aff *aff)
376 return aff ? isl_local_space_copy(aff->ls) : NULL;
379 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
381 isl_local_space *ls;
382 if (!aff)
383 return NULL;
384 ls = isl_local_space_copy(aff->ls);
385 ls = isl_local_space_from_domain(ls);
386 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
387 return ls;
390 /* Return the local space of the domain of "aff".
391 * This may be either a copy or the local space itself
392 * if there is only one reference to "aff".
393 * This allows the local space to be modified inplace
394 * if both the expression and its local space have only a single reference.
395 * The caller is not allowed to modify "aff" between this call and
396 * a subsequent call to isl_aff_restore_domain_local_space.
397 * The only exception is that isl_aff_free can be called instead.
399 __isl_give isl_local_space *isl_aff_take_domain_local_space(
400 __isl_keep isl_aff *aff)
402 isl_local_space *ls;
404 if (!aff)
405 return NULL;
406 if (aff->ref != 1)
407 return isl_aff_get_domain_local_space(aff);
408 ls = aff->ls;
409 aff->ls = NULL;
410 return ls;
413 /* Set the local space of the domain of "aff" to "ls",
414 * where the local space of "aff" may be missing
415 * due to a preceding call to isl_aff_take_domain_local_space.
416 * However, in this case, "aff" only has a single reference and
417 * then the call to isl_aff_cow has no effect.
419 __isl_give isl_aff *isl_aff_restore_domain_local_space(
420 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
422 if (!aff || !ls)
423 goto error;
425 if (aff->ls == ls) {
426 isl_local_space_free(ls);
427 return aff;
430 aff = isl_aff_cow(aff);
431 if (!aff)
432 goto error;
433 isl_local_space_free(aff->ls);
434 aff->ls = ls;
436 return aff;
437 error:
438 isl_aff_free(aff);
439 isl_local_space_free(ls);
440 return NULL;
443 /* Externally, an isl_aff has a map space, but internally, the
444 * ls field corresponds to the domain of that space.
446 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
447 enum isl_dim_type type, unsigned pos)
449 if (!aff)
450 return NULL;
451 if (type == isl_dim_out)
452 return NULL;
453 if (type == isl_dim_in)
454 type = isl_dim_set;
455 return isl_local_space_get_dim_name(aff->ls, type, pos);
458 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
459 __isl_take isl_space *dim)
461 aff = isl_aff_cow(aff);
462 if (!aff || !dim)
463 goto error;
465 aff->ls = isl_local_space_reset_space(aff->ls, dim);
466 if (!aff->ls)
467 return isl_aff_free(aff);
469 return aff;
470 error:
471 isl_aff_free(aff);
472 isl_space_free(dim);
473 return NULL;
476 /* Reset the space of "aff". This function is called from isl_pw_templ.c
477 * and doesn't know if the space of an element object is represented
478 * directly or through its domain. It therefore passes along both.
480 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
481 __isl_take isl_space *space, __isl_take isl_space *domain)
483 isl_space_free(space);
484 return isl_aff_reset_domain_space(aff, domain);
487 /* Reorder the coefficients of the affine expression based
488 * on the given reordering.
489 * The reordering r is assumed to have been extended with the local
490 * variables.
492 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
493 __isl_take isl_reordering *r, int n_div)
495 isl_space *space;
496 isl_vec *res;
497 int i;
499 if (!vec || !r)
500 goto error;
502 space = isl_reordering_peek_space(r);
503 res = isl_vec_alloc(vec->ctx,
504 2 + isl_space_dim(space, isl_dim_all) + n_div);
505 if (!res)
506 goto error;
507 isl_seq_cpy(res->el, vec->el, 2);
508 isl_seq_clr(res->el + 2, res->size - 2);
509 for (i = 0; i < r->len; ++i)
510 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
512 isl_reordering_free(r);
513 isl_vec_free(vec);
514 return res;
515 error:
516 isl_vec_free(vec);
517 isl_reordering_free(r);
518 return NULL;
521 /* Reorder the dimensions of the domain of "aff" according
522 * to the given reordering.
524 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
525 __isl_take isl_reordering *r)
527 aff = isl_aff_cow(aff);
528 if (!aff)
529 goto error;
531 r = isl_reordering_extend(r, aff->ls->div->n_row);
532 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
533 aff->ls->div->n_row);
534 aff->ls = isl_local_space_realign(aff->ls, r);
536 if (!aff->v || !aff->ls)
537 return isl_aff_free(aff);
539 return aff;
540 error:
541 isl_aff_free(aff);
542 isl_reordering_free(r);
543 return NULL;
546 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
547 __isl_take isl_space *model)
549 isl_bool equal_params;
551 if (!aff || !model)
552 goto error;
554 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
555 if (equal_params < 0)
556 goto error;
557 if (!equal_params) {
558 isl_reordering *exp;
560 model = isl_space_drop_dims(model, isl_dim_in,
561 0, isl_space_dim(model, isl_dim_in));
562 model = isl_space_drop_dims(model, isl_dim_out,
563 0, isl_space_dim(model, isl_dim_out));
564 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
565 exp = isl_reordering_extend_space(exp,
566 isl_aff_get_domain_space(aff));
567 aff = isl_aff_realign_domain(aff, exp);
570 isl_space_free(model);
571 return aff;
572 error:
573 isl_space_free(model);
574 isl_aff_free(aff);
575 return NULL;
578 /* Is "aff" obviously equal to zero?
580 * If the denominator is zero, then "aff" is not equal to zero.
582 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
584 if (!aff)
585 return isl_bool_error;
587 if (isl_int_is_zero(aff->v->el[0]))
588 return isl_bool_false;
589 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
592 /* Does "aff" represent NaN?
594 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
596 if (!aff)
597 return isl_bool_error;
599 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
602 /* Are "aff1" and "aff2" obviously equal?
604 * NaN is not equal to anything, not even to another NaN.
606 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
607 __isl_keep isl_aff *aff2)
609 isl_bool equal;
611 if (!aff1 || !aff2)
612 return isl_bool_error;
614 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
615 return isl_bool_false;
617 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
618 if (equal < 0 || !equal)
619 return equal;
621 return isl_vec_is_equal(aff1->v, aff2->v);
624 /* Return the common denominator of "aff" in "v".
626 * We cannot return anything meaningful in case of a NaN.
628 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
630 if (!aff)
631 return isl_stat_error;
632 if (isl_aff_is_nan(aff))
633 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
634 "cannot get denominator of NaN", return isl_stat_error);
635 isl_int_set(*v, aff->v->el[0]);
636 return isl_stat_ok;
639 /* Return the common denominator of "aff".
641 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
643 isl_ctx *ctx;
645 if (!aff)
646 return NULL;
648 ctx = isl_aff_get_ctx(aff);
649 if (isl_aff_is_nan(aff))
650 return isl_val_nan(ctx);
651 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
654 /* Return the constant term of "aff".
656 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
658 isl_ctx *ctx;
659 isl_val *v;
661 if (!aff)
662 return NULL;
664 ctx = isl_aff_get_ctx(aff);
665 if (isl_aff_is_nan(aff))
666 return isl_val_nan(ctx);
667 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
668 return isl_val_normalize(v);
671 /* Return the coefficient of the variable of type "type" at position "pos"
672 * of "aff".
674 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
675 enum isl_dim_type type, int pos)
677 isl_ctx *ctx;
678 isl_val *v;
680 if (!aff)
681 return NULL;
683 ctx = isl_aff_get_ctx(aff);
684 if (type == isl_dim_out)
685 isl_die(ctx, isl_error_invalid,
686 "output/set dimension does not have a coefficient",
687 return NULL);
688 if (type == isl_dim_in)
689 type = isl_dim_set;
691 if (pos >= isl_local_space_dim(aff->ls, type))
692 isl_die(ctx, isl_error_invalid,
693 "position out of bounds", return NULL);
695 if (isl_aff_is_nan(aff))
696 return isl_val_nan(ctx);
697 pos += isl_local_space_offset(aff->ls, type);
698 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
699 return isl_val_normalize(v);
702 /* Return the sign of the coefficient of the variable of type "type"
703 * at position "pos" of "aff".
705 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
706 int pos)
708 isl_ctx *ctx;
710 if (!aff)
711 return 0;
713 ctx = isl_aff_get_ctx(aff);
714 if (type == isl_dim_out)
715 isl_die(ctx, isl_error_invalid,
716 "output/set dimension does not have a coefficient",
717 return 0);
718 if (type == isl_dim_in)
719 type = isl_dim_set;
721 if (pos >= isl_local_space_dim(aff->ls, type))
722 isl_die(ctx, isl_error_invalid,
723 "position out of bounds", return 0);
725 pos += isl_local_space_offset(aff->ls, type);
726 return isl_int_sgn(aff->v->el[1 + pos]);
729 /* Replace the numerator of the constant term of "aff" by "v".
731 * A NaN is unaffected by this operation.
733 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
735 if (!aff)
736 return NULL;
737 if (isl_aff_is_nan(aff))
738 return aff;
739 aff = isl_aff_cow(aff);
740 if (!aff)
741 return NULL;
743 aff->v = isl_vec_cow(aff->v);
744 if (!aff->v)
745 return isl_aff_free(aff);
747 isl_int_set(aff->v->el[1], v);
749 return aff;
752 /* Replace the constant term of "aff" by "v".
754 * A NaN is unaffected by this operation.
756 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
757 __isl_take isl_val *v)
759 if (!aff || !v)
760 goto error;
762 if (isl_aff_is_nan(aff)) {
763 isl_val_free(v);
764 return aff;
767 if (!isl_val_is_rat(v))
768 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
769 "expecting rational value", goto error);
771 if (isl_int_eq(aff->v->el[1], v->n) &&
772 isl_int_eq(aff->v->el[0], v->d)) {
773 isl_val_free(v);
774 return aff;
777 aff = isl_aff_cow(aff);
778 if (!aff)
779 goto error;
780 aff->v = isl_vec_cow(aff->v);
781 if (!aff->v)
782 goto error;
784 if (isl_int_eq(aff->v->el[0], v->d)) {
785 isl_int_set(aff->v->el[1], v->n);
786 } else if (isl_int_is_one(v->d)) {
787 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
788 } else {
789 isl_seq_scale(aff->v->el + 1,
790 aff->v->el + 1, v->d, aff->v->size - 1);
791 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
792 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
793 aff->v = isl_vec_normalize(aff->v);
794 if (!aff->v)
795 goto error;
798 isl_val_free(v);
799 return aff;
800 error:
801 isl_aff_free(aff);
802 isl_val_free(v);
803 return NULL;
806 /* Add "v" to the constant term of "aff".
808 * A NaN is unaffected by this operation.
810 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
812 if (isl_int_is_zero(v))
813 return aff;
815 if (!aff)
816 return NULL;
817 if (isl_aff_is_nan(aff))
818 return aff;
819 aff = isl_aff_cow(aff);
820 if (!aff)
821 return NULL;
823 aff->v = isl_vec_cow(aff->v);
824 if (!aff->v)
825 return isl_aff_free(aff);
827 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
829 return aff;
832 /* Add "v" to the constant term of "aff".
834 * A NaN is unaffected by this operation.
836 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
837 __isl_take isl_val *v)
839 if (!aff || !v)
840 goto error;
842 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
843 isl_val_free(v);
844 return aff;
847 if (!isl_val_is_rat(v))
848 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
849 "expecting rational value", goto error);
851 aff = isl_aff_cow(aff);
852 if (!aff)
853 goto error;
855 aff->v = isl_vec_cow(aff->v);
856 if (!aff->v)
857 goto error;
859 if (isl_int_is_one(v->d)) {
860 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
861 } else if (isl_int_eq(aff->v->el[0], v->d)) {
862 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
863 aff->v = isl_vec_normalize(aff->v);
864 if (!aff->v)
865 goto error;
866 } else {
867 isl_seq_scale(aff->v->el + 1,
868 aff->v->el + 1, v->d, aff->v->size - 1);
869 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
870 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
871 aff->v = isl_vec_normalize(aff->v);
872 if (!aff->v)
873 goto error;
876 isl_val_free(v);
877 return aff;
878 error:
879 isl_aff_free(aff);
880 isl_val_free(v);
881 return NULL;
884 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
886 isl_int t;
888 isl_int_init(t);
889 isl_int_set_si(t, v);
890 aff = isl_aff_add_constant(aff, t);
891 isl_int_clear(t);
893 return aff;
896 /* Add "v" to the numerator of the constant term of "aff".
898 * A NaN is unaffected by this operation.
900 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
902 if (isl_int_is_zero(v))
903 return aff;
905 if (!aff)
906 return NULL;
907 if (isl_aff_is_nan(aff))
908 return aff;
909 aff = isl_aff_cow(aff);
910 if (!aff)
911 return NULL;
913 aff->v = isl_vec_cow(aff->v);
914 if (!aff->v)
915 return isl_aff_free(aff);
917 isl_int_add(aff->v->el[1], aff->v->el[1], v);
919 return aff;
922 /* Add "v" to the numerator of the constant term of "aff".
924 * A NaN is unaffected by this operation.
926 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
928 isl_int t;
930 if (v == 0)
931 return aff;
933 isl_int_init(t);
934 isl_int_set_si(t, v);
935 aff = isl_aff_add_constant_num(aff, t);
936 isl_int_clear(t);
938 return aff;
941 /* Replace the numerator of the constant term of "aff" by "v".
943 * A NaN is unaffected by this operation.
945 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
947 if (!aff)
948 return NULL;
949 if (isl_aff_is_nan(aff))
950 return aff;
951 aff = isl_aff_cow(aff);
952 if (!aff)
953 return NULL;
955 aff->v = isl_vec_cow(aff->v);
956 if (!aff->v)
957 return isl_aff_free(aff);
959 isl_int_set_si(aff->v->el[1], v);
961 return aff;
964 /* Replace the numerator of the coefficient of the variable of type "type"
965 * at position "pos" of "aff" by "v".
967 * A NaN is unaffected by this operation.
969 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
970 enum isl_dim_type type, int pos, isl_int v)
972 if (!aff)
973 return NULL;
975 if (type == isl_dim_out)
976 isl_die(aff->v->ctx, isl_error_invalid,
977 "output/set dimension does not have a coefficient",
978 return isl_aff_free(aff));
979 if (type == isl_dim_in)
980 type = isl_dim_set;
982 if (pos >= isl_local_space_dim(aff->ls, type))
983 isl_die(aff->v->ctx, isl_error_invalid,
984 "position out of bounds", return isl_aff_free(aff));
986 if (isl_aff_is_nan(aff))
987 return aff;
988 aff = isl_aff_cow(aff);
989 if (!aff)
990 return NULL;
992 aff->v = isl_vec_cow(aff->v);
993 if (!aff->v)
994 return isl_aff_free(aff);
996 pos += isl_local_space_offset(aff->ls, type);
997 isl_int_set(aff->v->el[1 + pos], v);
999 return aff;
1002 /* Replace the numerator of the coefficient of the variable of type "type"
1003 * at position "pos" of "aff" by "v".
1005 * A NaN is unaffected by this operation.
1007 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1008 enum isl_dim_type type, int pos, int v)
1010 if (!aff)
1011 return NULL;
1013 if (type == isl_dim_out)
1014 isl_die(aff->v->ctx, isl_error_invalid,
1015 "output/set dimension does not have a coefficient",
1016 return isl_aff_free(aff));
1017 if (type == isl_dim_in)
1018 type = isl_dim_set;
1020 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1021 isl_die(aff->v->ctx, isl_error_invalid,
1022 "position out of bounds", return isl_aff_free(aff));
1024 if (isl_aff_is_nan(aff))
1025 return aff;
1026 pos += isl_local_space_offset(aff->ls, type);
1027 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1028 return aff;
1030 aff = isl_aff_cow(aff);
1031 if (!aff)
1032 return NULL;
1034 aff->v = isl_vec_cow(aff->v);
1035 if (!aff->v)
1036 return isl_aff_free(aff);
1038 isl_int_set_si(aff->v->el[1 + pos], v);
1040 return aff;
1043 /* Replace the coefficient of the variable of type "type" at position "pos"
1044 * of "aff" by "v".
1046 * A NaN is unaffected by this operation.
1048 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1049 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1051 if (!aff || !v)
1052 goto error;
1054 if (type == isl_dim_out)
1055 isl_die(aff->v->ctx, isl_error_invalid,
1056 "output/set dimension does not have a coefficient",
1057 goto error);
1058 if (type == isl_dim_in)
1059 type = isl_dim_set;
1061 if (pos >= isl_local_space_dim(aff->ls, type))
1062 isl_die(aff->v->ctx, isl_error_invalid,
1063 "position out of bounds", goto error);
1065 if (isl_aff_is_nan(aff)) {
1066 isl_val_free(v);
1067 return aff;
1069 if (!isl_val_is_rat(v))
1070 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1071 "expecting rational value", goto error);
1073 pos += isl_local_space_offset(aff->ls, type);
1074 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1075 isl_int_eq(aff->v->el[0], v->d)) {
1076 isl_val_free(v);
1077 return aff;
1080 aff = isl_aff_cow(aff);
1081 if (!aff)
1082 goto error;
1083 aff->v = isl_vec_cow(aff->v);
1084 if (!aff->v)
1085 goto error;
1087 if (isl_int_eq(aff->v->el[0], v->d)) {
1088 isl_int_set(aff->v->el[1 + pos], v->n);
1089 } else if (isl_int_is_one(v->d)) {
1090 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1091 } else {
1092 isl_seq_scale(aff->v->el + 1,
1093 aff->v->el + 1, v->d, aff->v->size - 1);
1094 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1095 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1096 aff->v = isl_vec_normalize(aff->v);
1097 if (!aff->v)
1098 goto error;
1101 isl_val_free(v);
1102 return aff;
1103 error:
1104 isl_aff_free(aff);
1105 isl_val_free(v);
1106 return NULL;
1109 /* Add "v" to the coefficient of the variable of type "type"
1110 * at position "pos" of "aff".
1112 * A NaN is unaffected by this operation.
1114 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1115 enum isl_dim_type type, int pos, isl_int v)
1117 if (!aff)
1118 return NULL;
1120 if (type == isl_dim_out)
1121 isl_die(aff->v->ctx, isl_error_invalid,
1122 "output/set dimension does not have a coefficient",
1123 return isl_aff_free(aff));
1124 if (type == isl_dim_in)
1125 type = isl_dim_set;
1127 if (pos >= isl_local_space_dim(aff->ls, type))
1128 isl_die(aff->v->ctx, isl_error_invalid,
1129 "position out of bounds", return isl_aff_free(aff));
1131 if (isl_aff_is_nan(aff))
1132 return aff;
1133 aff = isl_aff_cow(aff);
1134 if (!aff)
1135 return NULL;
1137 aff->v = isl_vec_cow(aff->v);
1138 if (!aff->v)
1139 return isl_aff_free(aff);
1141 pos += isl_local_space_offset(aff->ls, type);
1142 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1144 return aff;
1147 /* Add "v" to the coefficient of the variable of type "type"
1148 * at position "pos" of "aff".
1150 * A NaN is unaffected by this operation.
1152 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1153 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1155 if (!aff || !v)
1156 goto error;
1158 if (isl_val_is_zero(v)) {
1159 isl_val_free(v);
1160 return aff;
1163 if (type == isl_dim_out)
1164 isl_die(aff->v->ctx, isl_error_invalid,
1165 "output/set dimension does not have a coefficient",
1166 goto error);
1167 if (type == isl_dim_in)
1168 type = isl_dim_set;
1170 if (pos >= isl_local_space_dim(aff->ls, type))
1171 isl_die(aff->v->ctx, isl_error_invalid,
1172 "position out of bounds", goto error);
1174 if (isl_aff_is_nan(aff)) {
1175 isl_val_free(v);
1176 return aff;
1178 if (!isl_val_is_rat(v))
1179 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1180 "expecting rational value", goto error);
1182 aff = isl_aff_cow(aff);
1183 if (!aff)
1184 goto error;
1186 aff->v = isl_vec_cow(aff->v);
1187 if (!aff->v)
1188 goto error;
1190 pos += isl_local_space_offset(aff->ls, type);
1191 if (isl_int_is_one(v->d)) {
1192 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1193 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1194 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1195 aff->v = isl_vec_normalize(aff->v);
1196 if (!aff->v)
1197 goto error;
1198 } else {
1199 isl_seq_scale(aff->v->el + 1,
1200 aff->v->el + 1, v->d, aff->v->size - 1);
1201 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1202 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1203 aff->v = isl_vec_normalize(aff->v);
1204 if (!aff->v)
1205 goto error;
1208 isl_val_free(v);
1209 return aff;
1210 error:
1211 isl_aff_free(aff);
1212 isl_val_free(v);
1213 return NULL;
1216 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1217 enum isl_dim_type type, int pos, int v)
1219 isl_int t;
1221 isl_int_init(t);
1222 isl_int_set_si(t, v);
1223 aff = isl_aff_add_coefficient(aff, type, pos, t);
1224 isl_int_clear(t);
1226 return aff;
1229 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1231 if (!aff)
1232 return NULL;
1234 return isl_local_space_get_div(aff->ls, pos);
1237 /* Return the negation of "aff".
1239 * As a special case, -NaN = NaN.
1241 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1243 if (!aff)
1244 return NULL;
1245 if (isl_aff_is_nan(aff))
1246 return aff;
1247 aff = isl_aff_cow(aff);
1248 if (!aff)
1249 return NULL;
1250 aff->v = isl_vec_cow(aff->v);
1251 if (!aff->v)
1252 return isl_aff_free(aff);
1254 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1256 return aff;
1259 /* Remove divs from the local space that do not appear in the affine
1260 * expression.
1261 * We currently only remove divs at the end.
1262 * Some intermediate divs may also not appear directly in the affine
1263 * expression, but we would also need to check that no other divs are
1264 * defined in terms of them.
1266 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1268 int pos;
1269 int off;
1270 int n;
1272 if (!aff)
1273 return NULL;
1275 n = isl_local_space_dim(aff->ls, isl_dim_div);
1276 off = isl_local_space_offset(aff->ls, isl_dim_div);
1278 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1279 if (pos == n)
1280 return aff;
1282 aff = isl_aff_cow(aff);
1283 if (!aff)
1284 return NULL;
1286 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1287 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1288 if (!aff->ls || !aff->v)
1289 return isl_aff_free(aff);
1291 return aff;
1294 /* Look for any divs in the aff->ls with a denominator equal to one
1295 * and plug them into the affine expression and any subsequent divs
1296 * that may reference the div.
1298 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1300 int i, n;
1301 int len;
1302 isl_int v;
1303 isl_vec *vec;
1304 isl_local_space *ls;
1305 unsigned pos;
1307 if (!aff)
1308 return NULL;
1310 n = isl_local_space_dim(aff->ls, isl_dim_div);
1311 len = aff->v->size;
1312 for (i = 0; i < n; ++i) {
1313 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1314 continue;
1315 ls = isl_local_space_copy(aff->ls);
1316 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1317 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1318 vec = isl_vec_copy(aff->v);
1319 vec = isl_vec_cow(vec);
1320 if (!ls || !vec)
1321 goto error;
1323 isl_int_init(v);
1325 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1326 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1327 len, len, v);
1329 isl_int_clear(v);
1331 isl_vec_free(aff->v);
1332 aff->v = vec;
1333 isl_local_space_free(aff->ls);
1334 aff->ls = ls;
1337 return aff;
1338 error:
1339 isl_vec_free(vec);
1340 isl_local_space_free(ls);
1341 return isl_aff_free(aff);
1344 /* Look for any divs j that appear with a unit coefficient inside
1345 * the definitions of other divs i and plug them into the definitions
1346 * of the divs i.
1348 * In particular, an expression of the form
1350 * floor((f(..) + floor(g(..)/n))/m)
1352 * is simplified to
1354 * floor((n * f(..) + g(..))/(n * m))
1356 * This simplification is correct because we can move the expression
1357 * f(..) into the inner floor in the original expression to obtain
1359 * floor(floor((n * f(..) + g(..))/n)/m)
1361 * from which we can derive the simplified expression.
1363 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1365 int i, j, n;
1366 int off;
1368 if (!aff)
1369 return NULL;
1371 n = isl_local_space_dim(aff->ls, isl_dim_div);
1372 off = isl_local_space_offset(aff->ls, isl_dim_div);
1373 for (i = 1; i < n; ++i) {
1374 for (j = 0; j < i; ++j) {
1375 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1376 continue;
1377 aff->ls = isl_local_space_substitute_seq(aff->ls,
1378 isl_dim_div, j, aff->ls->div->row[j],
1379 aff->v->size, i, 1);
1380 if (!aff->ls)
1381 return isl_aff_free(aff);
1385 return aff;
1388 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1390 * Even though this function is only called on isl_affs with a single
1391 * reference, we are careful to only change aff->v and aff->ls together.
1393 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1395 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1396 isl_local_space *ls;
1397 isl_vec *v;
1399 ls = isl_local_space_copy(aff->ls);
1400 ls = isl_local_space_swap_div(ls, a, b);
1401 v = isl_vec_copy(aff->v);
1402 v = isl_vec_cow(v);
1403 if (!ls || !v)
1404 goto error;
1406 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1407 isl_vec_free(aff->v);
1408 aff->v = v;
1409 isl_local_space_free(aff->ls);
1410 aff->ls = ls;
1412 return aff;
1413 error:
1414 isl_vec_free(v);
1415 isl_local_space_free(ls);
1416 return isl_aff_free(aff);
1419 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1421 * We currently do not actually remove div "b", but simply add its
1422 * coefficient to that of "a" and then zero it out.
1424 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1426 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1428 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1429 return aff;
1431 aff->v = isl_vec_cow(aff->v);
1432 if (!aff->v)
1433 return isl_aff_free(aff);
1435 isl_int_add(aff->v->el[1 + off + a],
1436 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1437 isl_int_set_si(aff->v->el[1 + off + b], 0);
1439 return aff;
1442 /* Sort the divs in the local space of "aff" according to
1443 * the comparison function "cmp_row" in isl_local_space.c,
1444 * combining the coefficients of identical divs.
1446 * Reordering divs does not change the semantics of "aff",
1447 * so there is no need to call isl_aff_cow.
1448 * Moreover, this function is currently only called on isl_affs
1449 * with a single reference.
1451 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1453 int i, j, n;
1455 if (!aff)
1456 return NULL;
1458 n = isl_aff_dim(aff, isl_dim_div);
1459 for (i = 1; i < n; ++i) {
1460 for (j = i - 1; j >= 0; --j) {
1461 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1462 if (cmp < 0)
1463 break;
1464 if (cmp == 0)
1465 aff = merge_divs(aff, j, j + 1);
1466 else
1467 aff = swap_div(aff, j, j + 1);
1468 if (!aff)
1469 return NULL;
1473 return aff;
1476 /* Normalize the representation of "aff".
1478 * This function should only be called of "new" isl_affs, i.e.,
1479 * with only a single reference. We therefore do not need to
1480 * worry about affecting other instances.
1482 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1484 if (!aff)
1485 return NULL;
1486 aff->v = isl_vec_normalize(aff->v);
1487 if (!aff->v)
1488 return isl_aff_free(aff);
1489 aff = plug_in_integral_divs(aff);
1490 aff = plug_in_unit_divs(aff);
1491 aff = sort_divs(aff);
1492 aff = isl_aff_remove_unused_divs(aff);
1493 return aff;
1496 /* Given f, return floor(f).
1497 * If f is an integer expression, then just return f.
1498 * If f is a constant, then return the constant floor(f).
1499 * Otherwise, if f = g/m, write g = q m + r,
1500 * create a new div d = [r/m] and return the expression q + d.
1501 * The coefficients in r are taken to lie between -m/2 and m/2.
1503 * reduce_div_coefficients performs the same normalization.
1505 * As a special case, floor(NaN) = NaN.
1507 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1509 int i;
1510 int size;
1511 isl_ctx *ctx;
1512 isl_vec *div;
1514 if (!aff)
1515 return NULL;
1517 if (isl_aff_is_nan(aff))
1518 return aff;
1519 if (isl_int_is_one(aff->v->el[0]))
1520 return aff;
1522 aff = isl_aff_cow(aff);
1523 if (!aff)
1524 return NULL;
1526 aff->v = isl_vec_cow(aff->v);
1527 if (!aff->v)
1528 return isl_aff_free(aff);
1530 if (isl_aff_is_cst(aff)) {
1531 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1532 isl_int_set_si(aff->v->el[0], 1);
1533 return aff;
1536 div = isl_vec_copy(aff->v);
1537 div = isl_vec_cow(div);
1538 if (!div)
1539 return isl_aff_free(aff);
1541 ctx = isl_aff_get_ctx(aff);
1542 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1543 for (i = 1; i < aff->v->size; ++i) {
1544 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1545 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1546 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1547 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1548 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1552 aff->ls = isl_local_space_add_div(aff->ls, div);
1553 if (!aff->ls)
1554 return isl_aff_free(aff);
1556 size = aff->v->size;
1557 aff->v = isl_vec_extend(aff->v, size + 1);
1558 if (!aff->v)
1559 return isl_aff_free(aff);
1560 isl_int_set_si(aff->v->el[0], 1);
1561 isl_int_set_si(aff->v->el[size], 1);
1563 aff = isl_aff_normalize(aff);
1565 return aff;
1568 /* Compute
1570 * aff mod m = aff - m * floor(aff/m)
1572 * with m an integer value.
1574 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1575 __isl_take isl_val *m)
1577 isl_aff *res;
1579 if (!aff || !m)
1580 goto error;
1582 if (!isl_val_is_int(m))
1583 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1584 "expecting integer modulo", goto error);
1586 res = isl_aff_copy(aff);
1587 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1588 aff = isl_aff_floor(aff);
1589 aff = isl_aff_scale_val(aff, m);
1590 res = isl_aff_sub(res, aff);
1592 return res;
1593 error:
1594 isl_aff_free(aff);
1595 isl_val_free(m);
1596 return NULL;
1599 /* Compute
1601 * pwaff mod m = pwaff - m * floor(pwaff/m)
1603 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1605 isl_pw_aff *res;
1607 res = isl_pw_aff_copy(pwaff);
1608 pwaff = isl_pw_aff_scale_down(pwaff, m);
1609 pwaff = isl_pw_aff_floor(pwaff);
1610 pwaff = isl_pw_aff_scale(pwaff, m);
1611 res = isl_pw_aff_sub(res, pwaff);
1613 return res;
1616 /* Compute
1618 * pa mod m = pa - m * floor(pa/m)
1620 * with m an integer value.
1622 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1623 __isl_take isl_val *m)
1625 if (!pa || !m)
1626 goto error;
1627 if (!isl_val_is_int(m))
1628 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1629 "expecting integer modulo", goto error);
1630 pa = isl_pw_aff_mod(pa, m->n);
1631 isl_val_free(m);
1632 return pa;
1633 error:
1634 isl_pw_aff_free(pa);
1635 isl_val_free(m);
1636 return NULL;
1639 /* Given f, return ceil(f).
1640 * If f is an integer expression, then just return f.
1641 * Otherwise, let f be the expression
1643 * e/m
1645 * then return
1647 * floor((e + m - 1)/m)
1649 * As a special case, ceil(NaN) = NaN.
1651 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1653 if (!aff)
1654 return NULL;
1656 if (isl_aff_is_nan(aff))
1657 return aff;
1658 if (isl_int_is_one(aff->v->el[0]))
1659 return aff;
1661 aff = isl_aff_cow(aff);
1662 if (!aff)
1663 return NULL;
1664 aff->v = isl_vec_cow(aff->v);
1665 if (!aff->v)
1666 return isl_aff_free(aff);
1668 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1669 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1670 aff = isl_aff_floor(aff);
1672 return aff;
1675 /* Apply the expansion computed by isl_merge_divs.
1676 * The expansion itself is given by "exp" while the resulting
1677 * list of divs is given by "div".
1679 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1680 __isl_take isl_mat *div, int *exp)
1682 int old_n_div;
1683 int new_n_div;
1684 int offset;
1686 aff = isl_aff_cow(aff);
1687 if (!aff || !div)
1688 goto error;
1690 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1691 new_n_div = isl_mat_rows(div);
1692 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1694 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1695 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1696 if (!aff->v || !aff->ls)
1697 return isl_aff_free(aff);
1698 return aff;
1699 error:
1700 isl_aff_free(aff);
1701 isl_mat_free(div);
1702 return NULL;
1705 /* Add two affine expressions that live in the same local space.
1707 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1708 __isl_take isl_aff *aff2)
1710 isl_int gcd, f;
1712 aff1 = isl_aff_cow(aff1);
1713 if (!aff1 || !aff2)
1714 goto error;
1716 aff1->v = isl_vec_cow(aff1->v);
1717 if (!aff1->v)
1718 goto error;
1720 isl_int_init(gcd);
1721 isl_int_init(f);
1722 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1723 isl_int_divexact(f, aff2->v->el[0], gcd);
1724 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1725 isl_int_divexact(f, aff1->v->el[0], gcd);
1726 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1727 isl_int_divexact(f, aff2->v->el[0], gcd);
1728 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1729 isl_int_clear(f);
1730 isl_int_clear(gcd);
1732 isl_aff_free(aff2);
1733 return aff1;
1734 error:
1735 isl_aff_free(aff1);
1736 isl_aff_free(aff2);
1737 return NULL;
1740 /* Return the sum of "aff1" and "aff2".
1742 * If either of the two is NaN, then the result is NaN.
1744 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1745 __isl_take isl_aff *aff2)
1747 isl_ctx *ctx;
1748 int *exp1 = NULL;
1749 int *exp2 = NULL;
1750 isl_mat *div;
1751 int n_div1, n_div2;
1753 if (!aff1 || !aff2)
1754 goto error;
1756 ctx = isl_aff_get_ctx(aff1);
1757 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1758 isl_die(ctx, isl_error_invalid,
1759 "spaces don't match", goto error);
1761 if (isl_aff_is_nan(aff1)) {
1762 isl_aff_free(aff2);
1763 return aff1;
1765 if (isl_aff_is_nan(aff2)) {
1766 isl_aff_free(aff1);
1767 return aff2;
1770 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1771 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1772 if (n_div1 == 0 && n_div2 == 0)
1773 return add_expanded(aff1, aff2);
1775 exp1 = isl_alloc_array(ctx, int, n_div1);
1776 exp2 = isl_alloc_array(ctx, int, n_div2);
1777 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1778 goto error;
1780 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1781 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1782 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1783 free(exp1);
1784 free(exp2);
1786 return add_expanded(aff1, aff2);
1787 error:
1788 free(exp1);
1789 free(exp2);
1790 isl_aff_free(aff1);
1791 isl_aff_free(aff2);
1792 return NULL;
1795 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1796 __isl_take isl_aff *aff2)
1798 return isl_aff_add(aff1, isl_aff_neg(aff2));
1801 /* Return the result of scaling "aff" by a factor of "f".
1803 * As a special case, f * NaN = NaN.
1805 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1807 isl_int gcd;
1809 if (!aff)
1810 return NULL;
1811 if (isl_aff_is_nan(aff))
1812 return aff;
1814 if (isl_int_is_one(f))
1815 return aff;
1817 aff = isl_aff_cow(aff);
1818 if (!aff)
1819 return NULL;
1820 aff->v = isl_vec_cow(aff->v);
1821 if (!aff->v)
1822 return isl_aff_free(aff);
1824 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1825 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1826 return aff;
1829 isl_int_init(gcd);
1830 isl_int_gcd(gcd, aff->v->el[0], f);
1831 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1832 isl_int_divexact(gcd, f, gcd);
1833 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1834 isl_int_clear(gcd);
1836 return aff;
1839 /* Multiple "aff" by "v".
1841 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1842 __isl_take isl_val *v)
1844 if (!aff || !v)
1845 goto error;
1847 if (isl_val_is_one(v)) {
1848 isl_val_free(v);
1849 return aff;
1852 if (!isl_val_is_rat(v))
1853 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1854 "expecting rational factor", goto error);
1856 aff = isl_aff_scale(aff, v->n);
1857 aff = isl_aff_scale_down(aff, v->d);
1859 isl_val_free(v);
1860 return aff;
1861 error:
1862 isl_aff_free(aff);
1863 isl_val_free(v);
1864 return NULL;
1867 /* Return the result of scaling "aff" down by a factor of "f".
1869 * As a special case, NaN/f = NaN.
1871 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1873 isl_int gcd;
1875 if (!aff)
1876 return NULL;
1877 if (isl_aff_is_nan(aff))
1878 return aff;
1880 if (isl_int_is_one(f))
1881 return aff;
1883 aff = isl_aff_cow(aff);
1884 if (!aff)
1885 return NULL;
1887 if (isl_int_is_zero(f))
1888 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1889 "cannot scale down by zero", return isl_aff_free(aff));
1891 aff->v = isl_vec_cow(aff->v);
1892 if (!aff->v)
1893 return isl_aff_free(aff);
1895 isl_int_init(gcd);
1896 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1897 isl_int_gcd(gcd, gcd, f);
1898 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1899 isl_int_divexact(gcd, f, gcd);
1900 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1901 isl_int_clear(gcd);
1903 return aff;
1906 /* Divide "aff" by "v".
1908 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1909 __isl_take isl_val *v)
1911 if (!aff || !v)
1912 goto error;
1914 if (isl_val_is_one(v)) {
1915 isl_val_free(v);
1916 return aff;
1919 if (!isl_val_is_rat(v))
1920 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1921 "expecting rational factor", goto error);
1922 if (!isl_val_is_pos(v))
1923 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1924 "factor needs to be positive", goto error);
1926 aff = isl_aff_scale(aff, v->d);
1927 aff = isl_aff_scale_down(aff, v->n);
1929 isl_val_free(v);
1930 return aff;
1931 error:
1932 isl_aff_free(aff);
1933 isl_val_free(v);
1934 return NULL;
1937 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1939 isl_int v;
1941 if (f == 1)
1942 return aff;
1944 isl_int_init(v);
1945 isl_int_set_ui(v, f);
1946 aff = isl_aff_scale_down(aff, v);
1947 isl_int_clear(v);
1949 return aff;
1952 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1953 enum isl_dim_type type, unsigned pos, const char *s)
1955 aff = isl_aff_cow(aff);
1956 if (!aff)
1957 return NULL;
1958 if (type == isl_dim_out)
1959 isl_die(aff->v->ctx, isl_error_invalid,
1960 "cannot set name of output/set dimension",
1961 return isl_aff_free(aff));
1962 if (type == isl_dim_in)
1963 type = isl_dim_set;
1964 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1965 if (!aff->ls)
1966 return isl_aff_free(aff);
1968 return aff;
1971 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1972 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1974 aff = isl_aff_cow(aff);
1975 if (!aff)
1976 goto error;
1977 if (type == isl_dim_out)
1978 isl_die(aff->v->ctx, isl_error_invalid,
1979 "cannot set name of output/set dimension",
1980 goto error);
1981 if (type == isl_dim_in)
1982 type = isl_dim_set;
1983 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1984 if (!aff->ls)
1985 return isl_aff_free(aff);
1987 return aff;
1988 error:
1989 isl_id_free(id);
1990 isl_aff_free(aff);
1991 return NULL;
1994 /* Replace the identifier of the input tuple of "aff" by "id".
1995 * type is currently required to be equal to isl_dim_in
1997 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1998 enum isl_dim_type type, __isl_take isl_id *id)
2000 aff = isl_aff_cow(aff);
2001 if (!aff)
2002 goto error;
2003 if (type != isl_dim_in)
2004 isl_die(aff->v->ctx, isl_error_invalid,
2005 "cannot only set id of input tuple", goto error);
2006 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2007 if (!aff->ls)
2008 return isl_aff_free(aff);
2010 return aff;
2011 error:
2012 isl_id_free(id);
2013 isl_aff_free(aff);
2014 return NULL;
2017 /* Exploit the equalities in "eq" to simplify the affine expression
2018 * and the expressions of the integer divisions in the local space.
2019 * The integer divisions in this local space are assumed to appear
2020 * as regular dimensions in "eq".
2022 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2023 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2025 int i, j;
2026 unsigned total;
2027 unsigned n_div;
2029 if (!eq)
2030 goto error;
2031 if (eq->n_eq == 0) {
2032 isl_basic_set_free(eq);
2033 return aff;
2036 aff = isl_aff_cow(aff);
2037 if (!aff)
2038 goto error;
2040 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2041 isl_basic_set_copy(eq));
2042 aff->v = isl_vec_cow(aff->v);
2043 if (!aff->ls || !aff->v)
2044 goto error;
2046 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2047 n_div = eq->n_div;
2048 for (i = 0; i < eq->n_eq; ++i) {
2049 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2050 if (j < 0 || j == 0 || j >= total)
2051 continue;
2053 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2054 &aff->v->el[0]);
2057 isl_basic_set_free(eq);
2058 aff = isl_aff_normalize(aff);
2059 return aff;
2060 error:
2061 isl_basic_set_free(eq);
2062 isl_aff_free(aff);
2063 return NULL;
2066 /* Exploit the equalities in "eq" to simplify the affine expression
2067 * and the expressions of the integer divisions in the local space.
2069 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2070 __isl_take isl_basic_set *eq)
2072 int n_div;
2074 if (!aff || !eq)
2075 goto error;
2076 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2077 if (n_div > 0)
2078 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2079 return isl_aff_substitute_equalities_lifted(aff, eq);
2080 error:
2081 isl_basic_set_free(eq);
2082 isl_aff_free(aff);
2083 return NULL;
2086 /* Look for equalities among the variables shared by context and aff
2087 * and the integer divisions of aff, if any.
2088 * The equalities are then used to eliminate coefficients and/or integer
2089 * divisions from aff.
2091 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2092 __isl_take isl_set *context)
2094 isl_basic_set *hull;
2095 int n_div;
2097 if (!aff)
2098 goto error;
2099 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2100 if (n_div > 0) {
2101 isl_basic_set *bset;
2102 isl_local_space *ls;
2103 context = isl_set_add_dims(context, isl_dim_set, n_div);
2104 ls = isl_aff_get_domain_local_space(aff);
2105 bset = isl_basic_set_from_local_space(ls);
2106 bset = isl_basic_set_lift(bset);
2107 bset = isl_basic_set_flatten(bset);
2108 context = isl_set_intersect(context,
2109 isl_set_from_basic_set(bset));
2112 hull = isl_set_affine_hull(context);
2113 return isl_aff_substitute_equalities_lifted(aff, hull);
2114 error:
2115 isl_aff_free(aff);
2116 isl_set_free(context);
2117 return NULL;
2120 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2121 __isl_take isl_set *context)
2123 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2124 dom_context = isl_set_intersect_params(dom_context, context);
2125 return isl_aff_gist(aff, dom_context);
2128 /* Return a basic set containing those elements in the space
2129 * of aff where it is positive. "rational" should not be set.
2131 * If "aff" is NaN, then it is not positive.
2133 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2134 int rational)
2136 isl_constraint *ineq;
2137 isl_basic_set *bset;
2138 isl_val *c;
2140 if (!aff)
2141 return NULL;
2142 if (isl_aff_is_nan(aff)) {
2143 isl_space *space = isl_aff_get_domain_space(aff);
2144 isl_aff_free(aff);
2145 return isl_basic_set_empty(space);
2147 if (rational)
2148 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2149 "rational sets not supported", goto error);
2151 ineq = isl_inequality_from_aff(aff);
2152 c = isl_constraint_get_constant_val(ineq);
2153 c = isl_val_sub_ui(c, 1);
2154 ineq = isl_constraint_set_constant_val(ineq, c);
2156 bset = isl_basic_set_from_constraint(ineq);
2157 bset = isl_basic_set_simplify(bset);
2158 return bset;
2159 error:
2160 isl_aff_free(aff);
2161 return NULL;
2164 /* Return a basic set containing those elements in the space
2165 * of aff where it is non-negative.
2166 * If "rational" is set, then return a rational basic set.
2168 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2170 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2171 __isl_take isl_aff *aff, int rational)
2173 isl_constraint *ineq;
2174 isl_basic_set *bset;
2176 if (!aff)
2177 return NULL;
2178 if (isl_aff_is_nan(aff)) {
2179 isl_space *space = isl_aff_get_domain_space(aff);
2180 isl_aff_free(aff);
2181 return isl_basic_set_empty(space);
2184 ineq = isl_inequality_from_aff(aff);
2186 bset = isl_basic_set_from_constraint(ineq);
2187 if (rational)
2188 bset = isl_basic_set_set_rational(bset);
2189 bset = isl_basic_set_simplify(bset);
2190 return bset;
2193 /* Return a basic set containing those elements in the space
2194 * of aff where it is non-negative.
2196 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2198 return aff_nonneg_basic_set(aff, 0);
2201 /* Return a basic set containing those elements in the domain space
2202 * of "aff" where it is positive.
2204 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2206 aff = isl_aff_add_constant_num_si(aff, -1);
2207 return isl_aff_nonneg_basic_set(aff);
2210 /* Return a basic set containing those elements in the domain space
2211 * of aff where it is negative.
2213 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2215 aff = isl_aff_neg(aff);
2216 return isl_aff_pos_basic_set(aff);
2219 /* Return a basic set containing those elements in the space
2220 * of aff where it is zero.
2221 * If "rational" is set, then return a rational basic set.
2223 * If "aff" is NaN, then it is not zero.
2225 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2226 int rational)
2228 isl_constraint *ineq;
2229 isl_basic_set *bset;
2231 if (!aff)
2232 return NULL;
2233 if (isl_aff_is_nan(aff)) {
2234 isl_space *space = isl_aff_get_domain_space(aff);
2235 isl_aff_free(aff);
2236 return isl_basic_set_empty(space);
2239 ineq = isl_equality_from_aff(aff);
2241 bset = isl_basic_set_from_constraint(ineq);
2242 if (rational)
2243 bset = isl_basic_set_set_rational(bset);
2244 bset = isl_basic_set_simplify(bset);
2245 return bset;
2248 /* Return a basic set containing those elements in the space
2249 * of aff where it is zero.
2251 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2253 return aff_zero_basic_set(aff, 0);
2256 /* Return a basic set containing those elements in the shared space
2257 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2259 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2260 __isl_take isl_aff *aff2)
2262 aff1 = isl_aff_sub(aff1, aff2);
2264 return isl_aff_nonneg_basic_set(aff1);
2267 /* Return a basic set containing those elements in the shared domain space
2268 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2270 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2271 __isl_take isl_aff *aff2)
2273 aff1 = isl_aff_sub(aff1, aff2);
2275 return isl_aff_pos_basic_set(aff1);
2278 /* Return a set containing those elements in the shared space
2279 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2281 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2282 __isl_take isl_aff *aff2)
2284 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2287 /* Return a set containing those elements in the shared domain space
2288 * of aff1 and aff2 where aff1 is greater than aff2.
2290 * If either of the two inputs is NaN, then the result is empty,
2291 * as comparisons with NaN always return false.
2293 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2294 __isl_take isl_aff *aff2)
2296 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2299 /* Return a basic set containing those elements in the shared space
2300 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2302 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2303 __isl_take isl_aff *aff2)
2305 return isl_aff_ge_basic_set(aff2, aff1);
2308 /* Return a basic set containing those elements in the shared domain space
2309 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2311 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2312 __isl_take isl_aff *aff2)
2314 return isl_aff_gt_basic_set(aff2, aff1);
2317 /* Return a set containing those elements in the shared space
2318 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2320 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2321 __isl_take isl_aff *aff2)
2323 return isl_aff_ge_set(aff2, aff1);
2326 /* Return a set containing those elements in the shared domain space
2327 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2329 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2330 __isl_take isl_aff *aff2)
2332 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2335 /* Return a basic set containing those elements in the shared space
2336 * of aff1 and aff2 where aff1 and aff2 are equal.
2338 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2339 __isl_take isl_aff *aff2)
2341 aff1 = isl_aff_sub(aff1, aff2);
2343 return isl_aff_zero_basic_set(aff1);
2346 /* Return a set containing those elements in the shared space
2347 * of aff1 and aff2 where aff1 and aff2 are equal.
2349 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2350 __isl_take isl_aff *aff2)
2352 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2355 /* Return a set containing those elements in the shared domain space
2356 * of aff1 and aff2 where aff1 and aff2 are not equal.
2358 * If either of the two inputs is NaN, then the result is empty,
2359 * as comparisons with NaN always return false.
2361 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2362 __isl_take isl_aff *aff2)
2364 isl_set *set_lt, *set_gt;
2366 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2367 isl_aff_copy(aff2));
2368 set_gt = isl_aff_gt_set(aff1, aff2);
2369 return isl_set_union_disjoint(set_lt, set_gt);
2372 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2373 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2375 aff1 = isl_aff_add(aff1, aff2);
2376 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2377 return aff1;
2380 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2382 if (!aff)
2383 return -1;
2385 return 0;
2388 /* Check whether the given affine expression has non-zero coefficient
2389 * for any dimension in the given range or if any of these dimensions
2390 * appear with non-zero coefficients in any of the integer divisions
2391 * involved in the affine expression.
2393 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2394 enum isl_dim_type type, unsigned first, unsigned n)
2396 int i;
2397 isl_ctx *ctx;
2398 int *active = NULL;
2399 isl_bool involves = isl_bool_false;
2401 if (!aff)
2402 return isl_bool_error;
2403 if (n == 0)
2404 return isl_bool_false;
2406 ctx = isl_aff_get_ctx(aff);
2407 if (first + n > isl_aff_dim(aff, type))
2408 isl_die(ctx, isl_error_invalid,
2409 "range out of bounds", return isl_bool_error);
2411 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2412 if (!active)
2413 goto error;
2415 first += isl_local_space_offset(aff->ls, type) - 1;
2416 for (i = 0; i < n; ++i)
2417 if (active[first + i]) {
2418 involves = isl_bool_true;
2419 break;
2422 free(active);
2424 return involves;
2425 error:
2426 free(active);
2427 return isl_bool_error;
2430 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2431 enum isl_dim_type type, unsigned first, unsigned n)
2433 isl_ctx *ctx;
2435 if (!aff)
2436 return NULL;
2437 if (type == isl_dim_out)
2438 isl_die(aff->v->ctx, isl_error_invalid,
2439 "cannot drop output/set dimension",
2440 return isl_aff_free(aff));
2441 if (type == isl_dim_in)
2442 type = isl_dim_set;
2443 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2444 return aff;
2446 ctx = isl_aff_get_ctx(aff);
2447 if (first + n > isl_local_space_dim(aff->ls, type))
2448 isl_die(ctx, isl_error_invalid, "range out of bounds",
2449 return isl_aff_free(aff));
2451 aff = isl_aff_cow(aff);
2452 if (!aff)
2453 return NULL;
2455 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2456 if (!aff->ls)
2457 return isl_aff_free(aff);
2459 first += 1 + isl_local_space_offset(aff->ls, type);
2460 aff->v = isl_vec_drop_els(aff->v, first, n);
2461 if (!aff->v)
2462 return isl_aff_free(aff);
2464 return aff;
2467 /* Drop the "n" domain dimensions starting at "first" from "aff",
2468 * after checking that they do not appear in the affine expression.
2470 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2471 unsigned n)
2473 isl_bool involves;
2475 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2476 if (involves < 0)
2477 return isl_aff_free(aff);
2478 if (involves)
2479 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2480 "affine expression involves some of the domain dimensions",
2481 return isl_aff_free(aff));
2482 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2485 /* Project the domain of the affine expression onto its parameter space.
2486 * The affine expression may not involve any of the domain dimensions.
2488 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2490 isl_space *space;
2491 unsigned n;
2493 n = isl_aff_dim(aff, isl_dim_in);
2494 aff = drop_domain(aff, 0, n);
2495 space = isl_aff_get_domain_space(aff);
2496 space = isl_space_params(space);
2497 aff = isl_aff_reset_domain_space(aff, space);
2498 return aff;
2501 /* Check that the domain of "aff" is a product.
2503 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2505 isl_bool is_product;
2507 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2508 if (is_product < 0)
2509 return isl_stat_error;
2510 if (!is_product)
2511 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2512 "domain is not a product", return isl_stat_error);
2513 return isl_stat_ok;
2516 /* Given an affine function with a domain of the form [A -> B] that
2517 * does not depend on B, return the same function on domain A.
2519 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2521 isl_space *space;
2522 int n, n_in;
2524 if (check_domain_product(aff) < 0)
2525 return isl_aff_free(aff);
2526 space = isl_aff_get_domain_space(aff);
2527 n = isl_space_dim(space, isl_dim_set);
2528 space = isl_space_factor_domain(space);
2529 n_in = isl_space_dim(space, isl_dim_set);
2530 aff = drop_domain(aff, n_in, n - n_in);
2531 aff = isl_aff_reset_domain_space(aff, space);
2532 return aff;
2535 /* Convert an affine expression defined over a parameter domain
2536 * into one that is defined over a zero-dimensional set.
2538 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2540 isl_local_space *ls;
2542 ls = isl_aff_take_domain_local_space(aff);
2543 ls = isl_local_space_set_from_params(ls);
2544 aff = isl_aff_restore_domain_local_space(aff, ls);
2546 return aff;
2549 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2550 enum isl_dim_type type, unsigned first, unsigned n)
2552 isl_ctx *ctx;
2554 if (!aff)
2555 return NULL;
2556 if (type == isl_dim_out)
2557 isl_die(aff->v->ctx, isl_error_invalid,
2558 "cannot insert output/set dimensions",
2559 return isl_aff_free(aff));
2560 if (type == isl_dim_in)
2561 type = isl_dim_set;
2562 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2563 return aff;
2565 ctx = isl_aff_get_ctx(aff);
2566 if (first > isl_local_space_dim(aff->ls, type))
2567 isl_die(ctx, isl_error_invalid, "position out of bounds",
2568 return isl_aff_free(aff));
2570 aff = isl_aff_cow(aff);
2571 if (!aff)
2572 return NULL;
2574 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2575 if (!aff->ls)
2576 return isl_aff_free(aff);
2578 first += 1 + isl_local_space_offset(aff->ls, type);
2579 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2580 if (!aff->v)
2581 return isl_aff_free(aff);
2583 return aff;
2586 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2587 enum isl_dim_type type, unsigned n)
2589 unsigned pos;
2591 pos = isl_aff_dim(aff, type);
2593 return isl_aff_insert_dims(aff, type, pos, n);
2596 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2597 enum isl_dim_type type, unsigned n)
2599 unsigned pos;
2601 pos = isl_pw_aff_dim(pwaff, type);
2603 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2606 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2607 * to dimensions of "dst_type" at "dst_pos".
2609 * We only support moving input dimensions to parameters and vice versa.
2611 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2612 enum isl_dim_type dst_type, unsigned dst_pos,
2613 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2615 unsigned g_dst_pos;
2616 unsigned g_src_pos;
2618 if (!aff)
2619 return NULL;
2620 if (n == 0 &&
2621 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2622 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2623 return aff;
2625 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2626 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2627 "cannot move output/set dimension",
2628 return isl_aff_free(aff));
2629 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2630 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2631 "cannot move divs", return isl_aff_free(aff));
2632 if (dst_type == isl_dim_in)
2633 dst_type = isl_dim_set;
2634 if (src_type == isl_dim_in)
2635 src_type = isl_dim_set;
2637 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2638 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2639 "range out of bounds", return isl_aff_free(aff));
2640 if (dst_type == src_type)
2641 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2642 "moving dims within the same type not supported",
2643 return isl_aff_free(aff));
2645 aff = isl_aff_cow(aff);
2646 if (!aff)
2647 return NULL;
2649 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2650 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2651 if (dst_type > src_type)
2652 g_dst_pos -= n;
2654 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2655 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2656 src_type, src_pos, n);
2657 if (!aff->v || !aff->ls)
2658 return isl_aff_free(aff);
2660 aff = sort_divs(aff);
2662 return aff;
2665 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2667 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2668 return isl_pw_aff_alloc(dom, aff);
2671 #define isl_aff_involves_nan isl_aff_is_nan
2673 #undef PW
2674 #define PW isl_pw_aff
2675 #undef EL
2676 #define EL isl_aff
2677 #undef EL_IS_ZERO
2678 #define EL_IS_ZERO is_empty
2679 #undef ZERO
2680 #define ZERO empty
2681 #undef IS_ZERO
2682 #define IS_ZERO is_empty
2683 #undef FIELD
2684 #define FIELD aff
2685 #undef DEFAULT_IS_ZERO
2686 #define DEFAULT_IS_ZERO 0
2688 #define NO_OPT
2689 #define NO_LIFT
2690 #define NO_MORPH
2692 #include <isl_pw_templ.c>
2693 #include <isl_pw_eval.c>
2694 #include <isl_pw_hash.c>
2695 #include <isl_pw_union_opt.c>
2697 #undef UNION
2698 #define UNION isl_union_pw_aff
2699 #undef PART
2700 #define PART isl_pw_aff
2701 #undef PARTS
2702 #define PARTS pw_aff
2704 #include <isl_union_single.c>
2705 #include <isl_union_neg.c>
2707 static __isl_give isl_set *align_params_pw_pw_set_and(
2708 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2709 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2710 __isl_take isl_pw_aff *pwaff2))
2712 isl_bool equal_params;
2714 if (!pwaff1 || !pwaff2)
2715 goto error;
2716 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2717 if (equal_params < 0)
2718 goto error;
2719 if (equal_params)
2720 return fn(pwaff1, pwaff2);
2721 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2722 isl_pw_aff_check_named_params(pwaff2) < 0)
2723 goto error;
2724 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2725 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2726 return fn(pwaff1, pwaff2);
2727 error:
2728 isl_pw_aff_free(pwaff1);
2729 isl_pw_aff_free(pwaff2);
2730 return NULL;
2733 /* Align the parameters of the to isl_pw_aff arguments and
2734 * then apply a function "fn" on them that returns an isl_map.
2736 static __isl_give isl_map *align_params_pw_pw_map_and(
2737 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2738 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2739 __isl_take isl_pw_aff *pa2))
2741 isl_bool equal_params;
2743 if (!pa1 || !pa2)
2744 goto error;
2745 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2746 if (equal_params < 0)
2747 goto error;
2748 if (equal_params)
2749 return fn(pa1, pa2);
2750 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2751 isl_pw_aff_check_named_params(pa2) < 0)
2752 goto error;
2753 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2754 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2755 return fn(pa1, pa2);
2756 error:
2757 isl_pw_aff_free(pa1);
2758 isl_pw_aff_free(pa2);
2759 return NULL;
2762 /* Compute a piecewise quasi-affine expression with a domain that
2763 * is the union of those of pwaff1 and pwaff2 and such that on each
2764 * cell, the quasi-affine expression is the maximum of those of pwaff1
2765 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2766 * cell, then the associated expression is the defined one.
2768 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2769 __isl_take isl_pw_aff *pwaff2)
2771 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2774 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2775 __isl_take isl_pw_aff *pwaff2)
2777 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2778 &pw_aff_union_max);
2781 /* Compute a piecewise quasi-affine expression with a domain that
2782 * is the union of those of pwaff1 and pwaff2 and such that on each
2783 * cell, the quasi-affine expression is the minimum of those of pwaff1
2784 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2785 * cell, then the associated expression is the defined one.
2787 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2788 __isl_take isl_pw_aff *pwaff2)
2790 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2793 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2794 __isl_take isl_pw_aff *pwaff2)
2796 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2797 &pw_aff_union_min);
2800 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2801 __isl_take isl_pw_aff *pwaff2, int max)
2803 if (max)
2804 return isl_pw_aff_union_max(pwaff1, pwaff2);
2805 else
2806 return isl_pw_aff_union_min(pwaff1, pwaff2);
2809 /* Construct a map with as domain the domain of pwaff and
2810 * one-dimensional range corresponding to the affine expressions.
2812 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2814 int i;
2815 isl_space *dim;
2816 isl_map *map;
2818 if (!pwaff)
2819 return NULL;
2821 dim = isl_pw_aff_get_space(pwaff);
2822 map = isl_map_empty(dim);
2824 for (i = 0; i < pwaff->n; ++i) {
2825 isl_basic_map *bmap;
2826 isl_map *map_i;
2828 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2829 map_i = isl_map_from_basic_map(bmap);
2830 map_i = isl_map_intersect_domain(map_i,
2831 isl_set_copy(pwaff->p[i].set));
2832 map = isl_map_union_disjoint(map, map_i);
2835 isl_pw_aff_free(pwaff);
2837 return map;
2840 /* Construct a map with as domain the domain of pwaff and
2841 * one-dimensional range corresponding to the affine expressions.
2843 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2845 if (!pwaff)
2846 return NULL;
2847 if (isl_space_is_set(pwaff->dim))
2848 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2849 "space of input is not a map", goto error);
2850 return map_from_pw_aff(pwaff);
2851 error:
2852 isl_pw_aff_free(pwaff);
2853 return NULL;
2856 /* Construct a one-dimensional set with as parameter domain
2857 * the domain of pwaff and the single set dimension
2858 * corresponding to the affine expressions.
2860 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2862 if (!pwaff)
2863 return NULL;
2864 if (!isl_space_is_set(pwaff->dim))
2865 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2866 "space of input is not a set", goto error);
2867 return map_from_pw_aff(pwaff);
2868 error:
2869 isl_pw_aff_free(pwaff);
2870 return NULL;
2873 /* Return a set containing those elements in the domain
2874 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2875 * does not satisfy "fn" (if complement is 1).
2877 * The pieces with a NaN never belong to the result since
2878 * NaN does not satisfy any property.
2880 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2881 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2882 int complement)
2884 int i;
2885 isl_set *set;
2887 if (!pwaff)
2888 return NULL;
2890 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2892 for (i = 0; i < pwaff->n; ++i) {
2893 isl_basic_set *bset;
2894 isl_set *set_i, *locus;
2895 isl_bool rational;
2897 if (isl_aff_is_nan(pwaff->p[i].aff))
2898 continue;
2900 rational = isl_set_has_rational(pwaff->p[i].set);
2901 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2902 locus = isl_set_from_basic_set(bset);
2903 set_i = isl_set_copy(pwaff->p[i].set);
2904 if (complement)
2905 set_i = isl_set_subtract(set_i, locus);
2906 else
2907 set_i = isl_set_intersect(set_i, locus);
2908 set = isl_set_union_disjoint(set, set_i);
2911 isl_pw_aff_free(pwaff);
2913 return set;
2916 /* Return a set containing those elements in the domain
2917 * of "pa" where it is positive.
2919 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2921 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2924 /* Return a set containing those elements in the domain
2925 * of pwaff where it is non-negative.
2927 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2929 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2932 /* Return a set containing those elements in the domain
2933 * of pwaff where it is zero.
2935 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2937 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2940 /* Return a set containing those elements in the domain
2941 * of pwaff where it is not zero.
2943 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2945 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2948 /* Return a set containing those elements in the shared domain
2949 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2951 * We compute the difference on the shared domain and then construct
2952 * the set of values where this difference is non-negative.
2953 * If strict is set, we first subtract 1 from the difference.
2954 * If equal is set, we only return the elements where pwaff1 and pwaff2
2955 * are equal.
2957 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2958 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2960 isl_set *set1, *set2;
2962 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2963 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2964 set1 = isl_set_intersect(set1, set2);
2965 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2966 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2967 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2969 if (strict) {
2970 isl_space *dim = isl_set_get_space(set1);
2971 isl_aff *aff;
2972 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2973 aff = isl_aff_add_constant_si(aff, -1);
2974 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2975 } else
2976 isl_set_free(set1);
2978 if (equal)
2979 return isl_pw_aff_zero_set(pwaff1);
2980 return isl_pw_aff_nonneg_set(pwaff1);
2983 /* Return a set containing those elements in the shared domain
2984 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2986 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2987 __isl_take isl_pw_aff *pwaff2)
2989 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2992 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2993 __isl_take isl_pw_aff *pwaff2)
2995 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2998 /* Return a set containing those elements in the shared domain
2999 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
3001 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3002 __isl_take isl_pw_aff *pwaff2)
3004 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3007 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3008 __isl_take isl_pw_aff *pwaff2)
3010 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
3013 /* Return a set containing those elements in the shared domain
3014 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3016 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3017 __isl_take isl_pw_aff *pwaff2)
3019 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3022 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3023 __isl_take isl_pw_aff *pwaff2)
3025 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
3028 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3029 __isl_take isl_pw_aff *pwaff2)
3031 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3034 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3035 __isl_take isl_pw_aff *pwaff2)
3037 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3040 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3041 * where the function values are ordered in the same way as "order",
3042 * which returns a set in the shared domain of its two arguments.
3043 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3045 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3046 * We first pull back the two functions such that they are defined on
3047 * the domain [A -> B]. Then we apply "order", resulting in a set
3048 * in the space [A -> B]. Finally, we unwrap this set to obtain
3049 * a map in the space A -> B.
3051 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3052 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3053 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3054 __isl_take isl_pw_aff *pa2))
3056 isl_space *space1, *space2;
3057 isl_multi_aff *ma;
3058 isl_set *set;
3060 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3061 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3062 space1 = isl_space_map_from_domain_and_range(space1, space2);
3063 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3064 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3065 ma = isl_multi_aff_range_map(space1);
3066 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3067 set = order(pa1, pa2);
3069 return isl_set_unwrap(set);
3072 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3073 * where the function values are equal.
3074 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3076 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3077 __isl_take isl_pw_aff *pa2)
3079 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3082 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3083 * where the function values are equal.
3085 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3086 __isl_take isl_pw_aff *pa2)
3088 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3091 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3092 * where the function value of "pa1" is less than the function value of "pa2".
3093 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3095 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3096 __isl_take isl_pw_aff *pa2)
3098 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3101 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3102 * where the function value of "pa1" is less than the function value of "pa2".
3104 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3105 __isl_take isl_pw_aff *pa2)
3107 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3110 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3111 * where the function value of "pa1" is greater than the function value
3112 * of "pa2".
3113 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3115 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3116 __isl_take isl_pw_aff *pa2)
3118 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3121 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3122 * where the function value of "pa1" is greater than the function value
3123 * of "pa2".
3125 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3126 __isl_take isl_pw_aff *pa2)
3128 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3131 /* Return a set containing those elements in the shared domain
3132 * of the elements of list1 and list2 where each element in list1
3133 * has the relation specified by "fn" with each element in list2.
3135 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3136 __isl_take isl_pw_aff_list *list2,
3137 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3138 __isl_take isl_pw_aff *pwaff2))
3140 int i, j;
3141 isl_ctx *ctx;
3142 isl_set *set;
3144 if (!list1 || !list2)
3145 goto error;
3147 ctx = isl_pw_aff_list_get_ctx(list1);
3148 if (list1->n < 1 || list2->n < 1)
3149 isl_die(ctx, isl_error_invalid,
3150 "list should contain at least one element", goto error);
3152 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3153 for (i = 0; i < list1->n; ++i)
3154 for (j = 0; j < list2->n; ++j) {
3155 isl_set *set_ij;
3157 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3158 isl_pw_aff_copy(list2->p[j]));
3159 set = isl_set_intersect(set, set_ij);
3162 isl_pw_aff_list_free(list1);
3163 isl_pw_aff_list_free(list2);
3164 return set;
3165 error:
3166 isl_pw_aff_list_free(list1);
3167 isl_pw_aff_list_free(list2);
3168 return NULL;
3171 /* Return a set containing those elements in the shared domain
3172 * of the elements of list1 and list2 where each element in list1
3173 * is equal to each element in list2.
3175 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3176 __isl_take isl_pw_aff_list *list2)
3178 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3181 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3182 __isl_take isl_pw_aff_list *list2)
3184 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3187 /* Return a set containing those elements in the shared domain
3188 * of the elements of list1 and list2 where each element in list1
3189 * is less than or equal to each element in list2.
3191 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3192 __isl_take isl_pw_aff_list *list2)
3194 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3197 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3198 __isl_take isl_pw_aff_list *list2)
3200 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3203 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3204 __isl_take isl_pw_aff_list *list2)
3206 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3209 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3210 __isl_take isl_pw_aff_list *list2)
3212 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3216 /* Return a set containing those elements in the shared domain
3217 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3219 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3220 __isl_take isl_pw_aff *pwaff2)
3222 isl_set *set_lt, *set_gt;
3224 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3225 isl_pw_aff_copy(pwaff2));
3226 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3227 return isl_set_union_disjoint(set_lt, set_gt);
3230 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3231 __isl_take isl_pw_aff *pwaff2)
3233 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3236 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3237 isl_int v)
3239 int i;
3241 if (isl_int_is_one(v))
3242 return pwaff;
3243 if (!isl_int_is_pos(v))
3244 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3245 "factor needs to be positive",
3246 return isl_pw_aff_free(pwaff));
3247 pwaff = isl_pw_aff_cow(pwaff);
3248 if (!pwaff)
3249 return NULL;
3250 if (pwaff->n == 0)
3251 return pwaff;
3253 for (i = 0; i < pwaff->n; ++i) {
3254 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3255 if (!pwaff->p[i].aff)
3256 return isl_pw_aff_free(pwaff);
3259 return pwaff;
3262 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3264 int i;
3266 pwaff = isl_pw_aff_cow(pwaff);
3267 if (!pwaff)
3268 return NULL;
3269 if (pwaff->n == 0)
3270 return pwaff;
3272 for (i = 0; i < pwaff->n; ++i) {
3273 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3274 if (!pwaff->p[i].aff)
3275 return isl_pw_aff_free(pwaff);
3278 return pwaff;
3281 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3283 int i;
3285 pwaff = isl_pw_aff_cow(pwaff);
3286 if (!pwaff)
3287 return NULL;
3288 if (pwaff->n == 0)
3289 return pwaff;
3291 for (i = 0; i < pwaff->n; ++i) {
3292 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3293 if (!pwaff->p[i].aff)
3294 return isl_pw_aff_free(pwaff);
3297 return pwaff;
3300 /* Assuming that "cond1" and "cond2" are disjoint,
3301 * return an affine expression that is equal to pwaff1 on cond1
3302 * and to pwaff2 on cond2.
3304 static __isl_give isl_pw_aff *isl_pw_aff_select(
3305 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3306 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3308 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3309 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3311 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3314 /* Return an affine expression that is equal to pwaff_true for elements
3315 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3316 * is zero.
3317 * That is, return cond ? pwaff_true : pwaff_false;
3319 * If "cond" involves and NaN, then we conservatively return a NaN
3320 * on its entire domain. In principle, we could consider the pieces
3321 * where it is NaN separately from those where it is not.
3323 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3324 * then only use the domain of "cond" to restrict the domain.
3326 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3327 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3329 isl_set *cond_true, *cond_false;
3330 isl_bool equal;
3332 if (!cond)
3333 goto error;
3334 if (isl_pw_aff_involves_nan(cond)) {
3335 isl_space *space = isl_pw_aff_get_domain_space(cond);
3336 isl_local_space *ls = isl_local_space_from_space(space);
3337 isl_pw_aff_free(cond);
3338 isl_pw_aff_free(pwaff_true);
3339 isl_pw_aff_free(pwaff_false);
3340 return isl_pw_aff_nan_on_domain(ls);
3343 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3344 isl_pw_aff_get_space(pwaff_false));
3345 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3346 isl_pw_aff_get_space(pwaff_true));
3347 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3348 if (equal < 0)
3349 goto error;
3350 if (equal) {
3351 isl_set *dom;
3353 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3354 isl_pw_aff_free(pwaff_false);
3355 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3358 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3359 cond_false = isl_pw_aff_zero_set(cond);
3360 return isl_pw_aff_select(cond_true, pwaff_true,
3361 cond_false, pwaff_false);
3362 error:
3363 isl_pw_aff_free(cond);
3364 isl_pw_aff_free(pwaff_true);
3365 isl_pw_aff_free(pwaff_false);
3366 return NULL;
3369 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3371 if (!aff)
3372 return isl_bool_error;
3374 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3377 /* Check whether pwaff is a piecewise constant.
3379 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3381 int i;
3383 if (!pwaff)
3384 return isl_bool_error;
3386 for (i = 0; i < pwaff->n; ++i) {
3387 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3388 if (is_cst < 0 || !is_cst)
3389 return is_cst;
3392 return isl_bool_true;
3395 /* Are all elements of "mpa" piecewise constants?
3397 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3399 int i;
3401 if (!mpa)
3402 return isl_bool_error;
3404 for (i = 0; i < mpa->n; ++i) {
3405 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3406 if (is_cst < 0 || !is_cst)
3407 return is_cst;
3410 return isl_bool_true;
3413 /* Return the product of "aff1" and "aff2".
3415 * If either of the two is NaN, then the result is NaN.
3417 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3419 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3420 __isl_take isl_aff *aff2)
3422 if (!aff1 || !aff2)
3423 goto error;
3425 if (isl_aff_is_nan(aff1)) {
3426 isl_aff_free(aff2);
3427 return aff1;
3429 if (isl_aff_is_nan(aff2)) {
3430 isl_aff_free(aff1);
3431 return aff2;
3434 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3435 return isl_aff_mul(aff2, aff1);
3437 if (!isl_aff_is_cst(aff2))
3438 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3439 "at least one affine expression should be constant",
3440 goto error);
3442 aff1 = isl_aff_cow(aff1);
3443 if (!aff1 || !aff2)
3444 goto error;
3446 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3447 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3449 isl_aff_free(aff2);
3450 return aff1;
3451 error:
3452 isl_aff_free(aff1);
3453 isl_aff_free(aff2);
3454 return NULL;
3457 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3459 * If either of the two is NaN, then the result is NaN.
3461 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3462 __isl_take isl_aff *aff2)
3464 int is_cst;
3465 int neg;
3467 if (!aff1 || !aff2)
3468 goto error;
3470 if (isl_aff_is_nan(aff1)) {
3471 isl_aff_free(aff2);
3472 return aff1;
3474 if (isl_aff_is_nan(aff2)) {
3475 isl_aff_free(aff1);
3476 return aff2;
3479 is_cst = isl_aff_is_cst(aff2);
3480 if (is_cst < 0)
3481 goto error;
3482 if (!is_cst)
3483 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3484 "second argument should be a constant", goto error);
3486 if (!aff2)
3487 goto error;
3489 neg = isl_int_is_neg(aff2->v->el[1]);
3490 if (neg) {
3491 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3492 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3495 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3496 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3498 if (neg) {
3499 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3500 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3503 isl_aff_free(aff2);
3504 return aff1;
3505 error:
3506 isl_aff_free(aff1);
3507 isl_aff_free(aff2);
3508 return NULL;
3511 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3512 __isl_take isl_pw_aff *pwaff2)
3514 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3517 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3518 __isl_take isl_pw_aff *pwaff2)
3520 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3523 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3524 __isl_take isl_pw_aff *pwaff2)
3526 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3529 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3530 __isl_take isl_pw_aff *pwaff2)
3532 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3535 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3536 __isl_take isl_pw_aff *pwaff2)
3538 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3541 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3542 __isl_take isl_pw_aff *pa2)
3544 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3547 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3549 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3550 __isl_take isl_pw_aff *pa2)
3552 int is_cst;
3554 is_cst = isl_pw_aff_is_cst(pa2);
3555 if (is_cst < 0)
3556 goto error;
3557 if (!is_cst)
3558 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3559 "second argument should be a piecewise constant",
3560 goto error);
3561 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3562 error:
3563 isl_pw_aff_free(pa1);
3564 isl_pw_aff_free(pa2);
3565 return NULL;
3568 /* Compute the quotient of the integer division of "pa1" by "pa2"
3569 * with rounding towards zero.
3570 * "pa2" is assumed to be a piecewise constant.
3572 * In particular, return
3574 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3577 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3578 __isl_take isl_pw_aff *pa2)
3580 int is_cst;
3581 isl_set *cond;
3582 isl_pw_aff *f, *c;
3584 is_cst = isl_pw_aff_is_cst(pa2);
3585 if (is_cst < 0)
3586 goto error;
3587 if (!is_cst)
3588 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3589 "second argument should be a piecewise constant",
3590 goto error);
3592 pa1 = isl_pw_aff_div(pa1, pa2);
3594 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3595 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3596 c = isl_pw_aff_ceil(pa1);
3597 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3598 error:
3599 isl_pw_aff_free(pa1);
3600 isl_pw_aff_free(pa2);
3601 return NULL;
3604 /* Compute the remainder of the integer division of "pa1" by "pa2"
3605 * with rounding towards zero.
3606 * "pa2" is assumed to be a piecewise constant.
3608 * In particular, return
3610 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3613 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3614 __isl_take isl_pw_aff *pa2)
3616 int is_cst;
3617 isl_pw_aff *res;
3619 is_cst = isl_pw_aff_is_cst(pa2);
3620 if (is_cst < 0)
3621 goto error;
3622 if (!is_cst)
3623 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3624 "second argument should be a piecewise constant",
3625 goto error);
3626 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3627 res = isl_pw_aff_mul(pa2, res);
3628 res = isl_pw_aff_sub(pa1, res);
3629 return res;
3630 error:
3631 isl_pw_aff_free(pa1);
3632 isl_pw_aff_free(pa2);
3633 return NULL;
3636 /* Does either of "pa1" or "pa2" involve any NaN2?
3638 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3639 __isl_keep isl_pw_aff *pa2)
3641 isl_bool has_nan;
3643 has_nan = isl_pw_aff_involves_nan(pa1);
3644 if (has_nan < 0 || has_nan)
3645 return has_nan;
3646 return isl_pw_aff_involves_nan(pa2);
3649 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3650 * by a NaN on their shared domain.
3652 * In principle, the result could be refined to only being NaN
3653 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3655 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3656 __isl_take isl_pw_aff *pa2)
3658 isl_local_space *ls;
3659 isl_set *dom;
3660 isl_pw_aff *pa;
3662 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3663 ls = isl_local_space_from_space(isl_set_get_space(dom));
3664 pa = isl_pw_aff_nan_on_domain(ls);
3665 pa = isl_pw_aff_intersect_domain(pa, dom);
3667 return pa;
3670 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3671 __isl_take isl_pw_aff *pwaff2)
3673 isl_set *le;
3674 isl_set *dom;
3676 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3677 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3678 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3679 isl_pw_aff_copy(pwaff2));
3680 dom = isl_set_subtract(dom, isl_set_copy(le));
3681 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3684 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3685 __isl_take isl_pw_aff *pwaff2)
3687 isl_set *ge;
3688 isl_set *dom;
3690 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3691 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3692 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3693 isl_pw_aff_copy(pwaff2));
3694 dom = isl_set_subtract(dom, isl_set_copy(ge));
3695 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3698 /* Return an expression for the minimum (if "max" is not set) or
3699 * the maximum (if "max" is set) of "pa1" and "pa2".
3700 * If either expression involves any NaN, then return a NaN
3701 * on the shared domain as result.
3703 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3704 __isl_take isl_pw_aff *pa2, int max)
3706 isl_bool has_nan;
3708 has_nan = either_involves_nan(pa1, pa2);
3709 if (has_nan < 0)
3710 pa1 = isl_pw_aff_free(pa1);
3711 else if (has_nan)
3712 return replace_by_nan(pa1, pa2);
3714 if (max)
3715 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3716 else
3717 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3720 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3722 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3723 __isl_take isl_pw_aff *pwaff2)
3725 return pw_aff_min_max(pwaff1, pwaff2, 0);
3728 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3730 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3731 __isl_take isl_pw_aff *pwaff2)
3733 return pw_aff_min_max(pwaff1, pwaff2, 1);
3736 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3737 __isl_take isl_pw_aff_list *list,
3738 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3739 __isl_take isl_pw_aff *pwaff2))
3741 int i;
3742 isl_ctx *ctx;
3743 isl_pw_aff *res;
3745 if (!list)
3746 return NULL;
3748 ctx = isl_pw_aff_list_get_ctx(list);
3749 if (list->n < 1)
3750 isl_die(ctx, isl_error_invalid,
3751 "list should contain at least one element", goto error);
3753 res = isl_pw_aff_copy(list->p[0]);
3754 for (i = 1; i < list->n; ++i)
3755 res = fn(res, isl_pw_aff_copy(list->p[i]));
3757 isl_pw_aff_list_free(list);
3758 return res;
3759 error:
3760 isl_pw_aff_list_free(list);
3761 return NULL;
3764 /* Return an isl_pw_aff that maps each element in the intersection of the
3765 * domains of the elements of list to the minimal corresponding affine
3766 * expression.
3768 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3770 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3773 /* Return an isl_pw_aff that maps each element in the intersection of the
3774 * domains of the elements of list to the maximal corresponding affine
3775 * expression.
3777 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3779 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3782 /* Mark the domains of "pwaff" as rational.
3784 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3786 int i;
3788 pwaff = isl_pw_aff_cow(pwaff);
3789 if (!pwaff)
3790 return NULL;
3791 if (pwaff->n == 0)
3792 return pwaff;
3794 for (i = 0; i < pwaff->n; ++i) {
3795 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3796 if (!pwaff->p[i].set)
3797 return isl_pw_aff_free(pwaff);
3800 return pwaff;
3803 /* Mark the domains of the elements of "list" as rational.
3805 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3806 __isl_take isl_pw_aff_list *list)
3808 int i, n;
3810 if (!list)
3811 return NULL;
3812 if (list->n == 0)
3813 return list;
3815 n = list->n;
3816 for (i = 0; i < n; ++i) {
3817 isl_pw_aff *pa;
3819 pa = isl_pw_aff_list_get_pw_aff(list, i);
3820 pa = isl_pw_aff_set_rational(pa);
3821 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3824 return list;
3827 /* Do the parameters of "aff" match those of "space"?
3829 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3830 __isl_keep isl_space *space)
3832 isl_space *aff_space;
3833 isl_bool match;
3835 if (!aff || !space)
3836 return isl_bool_error;
3838 aff_space = isl_aff_get_domain_space(aff);
3840 match = isl_space_has_equal_params(space, aff_space);
3842 isl_space_free(aff_space);
3843 return match;
3846 /* Check that the domain space of "aff" matches "space".
3848 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3849 __isl_keep isl_space *space)
3851 isl_space *aff_space;
3852 isl_bool match;
3854 if (!aff || !space)
3855 return isl_stat_error;
3857 aff_space = isl_aff_get_domain_space(aff);
3859 match = isl_space_has_equal_params(space, aff_space);
3860 if (match < 0)
3861 goto error;
3862 if (!match)
3863 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3864 "parameters don't match", goto error);
3865 match = isl_space_tuple_is_equal(space, isl_dim_in,
3866 aff_space, isl_dim_set);
3867 if (match < 0)
3868 goto error;
3869 if (!match)
3870 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3871 "domains don't match", goto error);
3872 isl_space_free(aff_space);
3873 return isl_stat_ok;
3874 error:
3875 isl_space_free(aff_space);
3876 return isl_stat_error;
3879 #undef BASE
3880 #define BASE aff
3881 #undef DOMBASE
3882 #define DOMBASE set
3883 #define NO_DOMAIN
3885 #include <isl_multi_no_explicit_domain.c>
3886 #include <isl_multi_templ.c>
3887 #include <isl_multi_apply_set.c>
3888 #include <isl_multi_cmp.c>
3889 #include <isl_multi_dims.c>
3890 #include <isl_multi_floor.c>
3891 #include <isl_multi_gist.c>
3893 #undef NO_DOMAIN
3895 /* Construct an isl_multi_aff living in "space" that corresponds
3896 * to the affine transformation matrix "mat".
3898 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3899 __isl_take isl_space *space, __isl_take isl_mat *mat)
3901 isl_ctx *ctx;
3902 isl_local_space *ls = NULL;
3903 isl_multi_aff *ma = NULL;
3904 int n_row, n_col, n_out, total;
3905 int i;
3907 if (!space || !mat)
3908 goto error;
3910 ctx = isl_mat_get_ctx(mat);
3912 n_row = isl_mat_rows(mat);
3913 n_col = isl_mat_cols(mat);
3914 if (n_row < 1)
3915 isl_die(ctx, isl_error_invalid,
3916 "insufficient number of rows", goto error);
3917 if (n_col < 1)
3918 isl_die(ctx, isl_error_invalid,
3919 "insufficient number of columns", goto error);
3920 n_out = isl_space_dim(space, isl_dim_out);
3921 total = isl_space_dim(space, isl_dim_all);
3922 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3923 isl_die(ctx, isl_error_invalid,
3924 "dimension mismatch", goto error);
3926 ma = isl_multi_aff_zero(isl_space_copy(space));
3927 ls = isl_local_space_from_space(isl_space_domain(space));
3929 for (i = 0; i < n_row - 1; ++i) {
3930 isl_vec *v;
3931 isl_aff *aff;
3933 v = isl_vec_alloc(ctx, 1 + n_col);
3934 if (!v)
3935 goto error;
3936 isl_int_set(v->el[0], mat->row[0][0]);
3937 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3938 v = isl_vec_normalize(v);
3939 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3940 ma = isl_multi_aff_set_aff(ma, i, aff);
3943 isl_local_space_free(ls);
3944 isl_mat_free(mat);
3945 return ma;
3946 error:
3947 isl_local_space_free(ls);
3948 isl_mat_free(mat);
3949 isl_multi_aff_free(ma);
3950 return NULL;
3953 /* Remove any internal structure of the domain of "ma".
3954 * If there is any such internal structure in the input,
3955 * then the name of the corresponding space is also removed.
3957 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3958 __isl_take isl_multi_aff *ma)
3960 isl_space *space;
3962 if (!ma)
3963 return NULL;
3965 if (!ma->space->nested[0])
3966 return ma;
3968 space = isl_multi_aff_get_space(ma);
3969 space = isl_space_flatten_domain(space);
3970 ma = isl_multi_aff_reset_space(ma, space);
3972 return ma;
3975 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3976 * of the space to its domain.
3978 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3980 int i, n_in;
3981 isl_local_space *ls;
3982 isl_multi_aff *ma;
3984 if (!space)
3985 return NULL;
3986 if (!isl_space_is_map(space))
3987 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3988 "not a map space", goto error);
3990 n_in = isl_space_dim(space, isl_dim_in);
3991 space = isl_space_domain_map(space);
3993 ma = isl_multi_aff_alloc(isl_space_copy(space));
3994 if (n_in == 0) {
3995 isl_space_free(space);
3996 return ma;
3999 space = isl_space_domain(space);
4000 ls = isl_local_space_from_space(space);
4001 for (i = 0; i < n_in; ++i) {
4002 isl_aff *aff;
4004 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4005 isl_dim_set, i);
4006 ma = isl_multi_aff_set_aff(ma, i, aff);
4008 isl_local_space_free(ls);
4009 return ma;
4010 error:
4011 isl_space_free(space);
4012 return NULL;
4015 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4016 * of the space to its range.
4018 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4020 int i, n_in, n_out;
4021 isl_local_space *ls;
4022 isl_multi_aff *ma;
4024 if (!space)
4025 return NULL;
4026 if (!isl_space_is_map(space))
4027 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4028 "not a map space", goto error);
4030 n_in = isl_space_dim(space, isl_dim_in);
4031 n_out = isl_space_dim(space, isl_dim_out);
4032 space = isl_space_range_map(space);
4034 ma = isl_multi_aff_alloc(isl_space_copy(space));
4035 if (n_out == 0) {
4036 isl_space_free(space);
4037 return ma;
4040 space = isl_space_domain(space);
4041 ls = isl_local_space_from_space(space);
4042 for (i = 0; i < n_out; ++i) {
4043 isl_aff *aff;
4045 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4046 isl_dim_set, n_in + i);
4047 ma = isl_multi_aff_set_aff(ma, i, aff);
4049 isl_local_space_free(ls);
4050 return ma;
4051 error:
4052 isl_space_free(space);
4053 return NULL;
4056 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4057 * of the space to its range.
4059 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4060 __isl_take isl_space *space)
4062 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4065 /* Given the space of a set and a range of set dimensions,
4066 * construct an isl_multi_aff that projects out those dimensions.
4068 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4069 __isl_take isl_space *space, enum isl_dim_type type,
4070 unsigned first, unsigned n)
4072 int i, dim;
4073 isl_local_space *ls;
4074 isl_multi_aff *ma;
4076 if (!space)
4077 return NULL;
4078 if (!isl_space_is_set(space))
4079 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4080 "expecting set space", goto error);
4081 if (type != isl_dim_set)
4082 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4083 "only set dimensions can be projected out", goto error);
4085 dim = isl_space_dim(space, isl_dim_set);
4086 if (first + n > dim)
4087 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4088 "range out of bounds", goto error);
4090 space = isl_space_from_domain(space);
4091 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4093 if (dim == n)
4094 return isl_multi_aff_alloc(space);
4096 ma = isl_multi_aff_alloc(isl_space_copy(space));
4097 space = isl_space_domain(space);
4098 ls = isl_local_space_from_space(space);
4100 for (i = 0; i < first; ++i) {
4101 isl_aff *aff;
4103 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4104 isl_dim_set, i);
4105 ma = isl_multi_aff_set_aff(ma, i, aff);
4108 for (i = 0; i < dim - (first + n); ++i) {
4109 isl_aff *aff;
4111 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4112 isl_dim_set, first + n + i);
4113 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4116 isl_local_space_free(ls);
4117 return ma;
4118 error:
4119 isl_space_free(space);
4120 return NULL;
4123 /* Given the space of a set and a range of set dimensions,
4124 * construct an isl_pw_multi_aff that projects out those dimensions.
4126 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4127 __isl_take isl_space *space, enum isl_dim_type type,
4128 unsigned first, unsigned n)
4130 isl_multi_aff *ma;
4132 ma = isl_multi_aff_project_out_map(space, type, first, n);
4133 return isl_pw_multi_aff_from_multi_aff(ma);
4136 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4137 * domain.
4139 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4140 __isl_take isl_multi_aff *ma)
4142 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4143 return isl_pw_multi_aff_alloc(dom, ma);
4146 /* Create a piecewise multi-affine expression in the given space that maps each
4147 * input dimension to the corresponding output dimension.
4149 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4150 __isl_take isl_space *space)
4152 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4155 /* Exploit the equalities in "eq" to simplify the affine expressions.
4157 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4158 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4160 int i;
4162 maff = isl_multi_aff_cow(maff);
4163 if (!maff || !eq)
4164 goto error;
4166 for (i = 0; i < maff->n; ++i) {
4167 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4168 isl_basic_set_copy(eq));
4169 if (!maff->u.p[i])
4170 goto error;
4173 isl_basic_set_free(eq);
4174 return maff;
4175 error:
4176 isl_basic_set_free(eq);
4177 isl_multi_aff_free(maff);
4178 return NULL;
4181 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4182 isl_int f)
4184 int i;
4186 maff = isl_multi_aff_cow(maff);
4187 if (!maff)
4188 return NULL;
4190 for (i = 0; i < maff->n; ++i) {
4191 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4192 if (!maff->u.p[i])
4193 return isl_multi_aff_free(maff);
4196 return maff;
4199 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4200 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4202 maff1 = isl_multi_aff_add(maff1, maff2);
4203 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4204 return maff1;
4207 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4209 if (!maff)
4210 return -1;
4212 return 0;
4215 /* Return the set of domain elements where "ma1" is lexicographically
4216 * smaller than or equal to "ma2".
4218 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4219 __isl_take isl_multi_aff *ma2)
4221 return isl_multi_aff_lex_ge_set(ma2, ma1);
4224 /* Return the set of domain elements where "ma1" is lexicographically
4225 * smaller than "ma2".
4227 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4228 __isl_take isl_multi_aff *ma2)
4230 return isl_multi_aff_lex_gt_set(ma2, ma1);
4233 /* Return the set of domain elements where "ma1" and "ma2"
4234 * satisfy "order".
4236 static __isl_give isl_set *isl_multi_aff_order_set(
4237 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4238 __isl_give isl_map *order(__isl_take isl_space *set_space))
4240 isl_space *space;
4241 isl_map *map1, *map2;
4242 isl_map *map, *ge;
4244 map1 = isl_map_from_multi_aff(ma1);
4245 map2 = isl_map_from_multi_aff(ma2);
4246 map = isl_map_range_product(map1, map2);
4247 space = isl_space_range(isl_map_get_space(map));
4248 space = isl_space_domain(isl_space_unwrap(space));
4249 ge = order(space);
4250 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4252 return isl_map_domain(map);
4255 /* Return the set of domain elements where "ma1" is lexicographically
4256 * greater than or equal to "ma2".
4258 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4259 __isl_take isl_multi_aff *ma2)
4261 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4264 /* Return the set of domain elements where "ma1" is lexicographically
4265 * greater than "ma2".
4267 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4268 __isl_take isl_multi_aff *ma2)
4270 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4273 #undef PW
4274 #define PW isl_pw_multi_aff
4275 #undef EL
4276 #define EL isl_multi_aff
4277 #undef EL_IS_ZERO
4278 #define EL_IS_ZERO is_empty
4279 #undef ZERO
4280 #define ZERO empty
4281 #undef IS_ZERO
4282 #define IS_ZERO is_empty
4283 #undef FIELD
4284 #define FIELD maff
4285 #undef DEFAULT_IS_ZERO
4286 #define DEFAULT_IS_ZERO 0
4288 #define NO_SUB
4289 #define NO_OPT
4290 #define NO_INSERT_DIMS
4291 #define NO_LIFT
4292 #define NO_MORPH
4294 #include <isl_pw_templ.c>
4295 #include <isl_pw_union_opt.c>
4297 #undef NO_SUB
4299 #undef UNION
4300 #define UNION isl_union_pw_multi_aff
4301 #undef PART
4302 #define PART isl_pw_multi_aff
4303 #undef PARTS
4304 #define PARTS pw_multi_aff
4306 #include <isl_union_multi.c>
4307 #include <isl_union_neg.c>
4309 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4310 __isl_take isl_pw_multi_aff *pma1,
4311 __isl_take isl_pw_multi_aff *pma2)
4313 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4314 &isl_multi_aff_lex_ge_set);
4317 /* Given two piecewise multi affine expressions, return a piecewise
4318 * multi-affine expression defined on the union of the definition domains
4319 * of the inputs that is equal to the lexicographic maximum of the two
4320 * inputs on each cell. If only one of the two inputs is defined on
4321 * a given cell, then it is considered to be the maximum.
4323 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4324 __isl_take isl_pw_multi_aff *pma1,
4325 __isl_take isl_pw_multi_aff *pma2)
4327 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4328 &pw_multi_aff_union_lexmax);
4331 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4332 __isl_take isl_pw_multi_aff *pma1,
4333 __isl_take isl_pw_multi_aff *pma2)
4335 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4336 &isl_multi_aff_lex_le_set);
4339 /* Given two piecewise multi affine expressions, return a piecewise
4340 * multi-affine expression defined on the union of the definition domains
4341 * of the inputs that is equal to the lexicographic minimum of the two
4342 * inputs on each cell. If only one of the two inputs is defined on
4343 * a given cell, then it is considered to be the minimum.
4345 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4346 __isl_take isl_pw_multi_aff *pma1,
4347 __isl_take isl_pw_multi_aff *pma2)
4349 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4350 &pw_multi_aff_union_lexmin);
4353 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4354 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4356 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4357 &isl_multi_aff_add);
4360 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4361 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4363 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4364 &pw_multi_aff_add);
4367 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4368 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4370 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4371 &isl_multi_aff_sub);
4374 /* Subtract "pma2" from "pma1" and return the result.
4376 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4377 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4379 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4380 &pw_multi_aff_sub);
4383 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4384 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4386 return isl_pw_multi_aff_union_add_(pma1, pma2);
4389 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4390 * with the actual sum on the shared domain and
4391 * the defined expression on the symmetric difference of the domains.
4393 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4394 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4396 return isl_union_pw_aff_union_add_(upa1, upa2);
4399 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4400 * with the actual sum on the shared domain and
4401 * the defined expression on the symmetric difference of the domains.
4403 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4404 __isl_take isl_union_pw_multi_aff *upma1,
4405 __isl_take isl_union_pw_multi_aff *upma2)
4407 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4410 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4411 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4413 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4414 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4416 int i, j, n;
4417 isl_space *space;
4418 isl_pw_multi_aff *res;
4420 if (!pma1 || !pma2)
4421 goto error;
4423 n = pma1->n * pma2->n;
4424 space = isl_space_product(isl_space_copy(pma1->dim),
4425 isl_space_copy(pma2->dim));
4426 res = isl_pw_multi_aff_alloc_size(space, n);
4428 for (i = 0; i < pma1->n; ++i) {
4429 for (j = 0; j < pma2->n; ++j) {
4430 isl_set *domain;
4431 isl_multi_aff *ma;
4433 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4434 isl_set_copy(pma2->p[j].set));
4435 ma = isl_multi_aff_product(
4436 isl_multi_aff_copy(pma1->p[i].maff),
4437 isl_multi_aff_copy(pma2->p[j].maff));
4438 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4442 isl_pw_multi_aff_free(pma1);
4443 isl_pw_multi_aff_free(pma2);
4444 return res;
4445 error:
4446 isl_pw_multi_aff_free(pma1);
4447 isl_pw_multi_aff_free(pma2);
4448 return NULL;
4451 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4452 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4454 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4455 &pw_multi_aff_product);
4458 /* Construct a map mapping the domain of the piecewise multi-affine expression
4459 * to its range, with each dimension in the range equated to the
4460 * corresponding affine expression on its cell.
4462 * If the domain of "pma" is rational, then so is the constructed "map".
4464 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4466 int i;
4467 isl_map *map;
4469 if (!pma)
4470 return NULL;
4472 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4474 for (i = 0; i < pma->n; ++i) {
4475 isl_bool rational;
4476 isl_multi_aff *maff;
4477 isl_basic_map *bmap;
4478 isl_map *map_i;
4480 rational = isl_set_is_rational(pma->p[i].set);
4481 if (rational < 0)
4482 map = isl_map_free(map);
4483 maff = isl_multi_aff_copy(pma->p[i].maff);
4484 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4485 map_i = isl_map_from_basic_map(bmap);
4486 map_i = isl_map_intersect_domain(map_i,
4487 isl_set_copy(pma->p[i].set));
4488 map = isl_map_union_disjoint(map, map_i);
4491 isl_pw_multi_aff_free(pma);
4492 return map;
4495 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4497 if (!pma)
4498 return NULL;
4500 if (!isl_space_is_set(pma->dim))
4501 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4502 "isl_pw_multi_aff cannot be converted into an isl_set",
4503 goto error);
4505 return isl_map_from_pw_multi_aff(pma);
4506 error:
4507 isl_pw_multi_aff_free(pma);
4508 return NULL;
4511 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4512 * denominator "denom".
4513 * "denom" is allowed to be negative, in which case the actual denominator
4514 * is -denom and the expressions are added instead.
4516 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4517 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4519 int i, first;
4520 int sign;
4521 isl_int d;
4523 first = isl_seq_first_non_zero(c, n);
4524 if (first == -1)
4525 return aff;
4527 sign = isl_int_sgn(denom);
4528 isl_int_init(d);
4529 isl_int_abs(d, denom);
4530 for (i = first; i < n; ++i) {
4531 isl_aff *aff_i;
4533 if (isl_int_is_zero(c[i]))
4534 continue;
4535 aff_i = isl_multi_aff_get_aff(ma, i);
4536 aff_i = isl_aff_scale(aff_i, c[i]);
4537 aff_i = isl_aff_scale_down(aff_i, d);
4538 if (sign >= 0)
4539 aff = isl_aff_sub(aff, aff_i);
4540 else
4541 aff = isl_aff_add(aff, aff_i);
4543 isl_int_clear(d);
4545 return aff;
4548 /* Extract an affine expression that expresses the output dimension "pos"
4549 * of "bmap" in terms of the parameters and input dimensions from
4550 * equality "eq".
4551 * Note that this expression may involve integer divisions defined
4552 * in terms of parameters and input dimensions.
4553 * The equality may also involve references to earlier (but not later)
4554 * output dimensions. These are replaced by the corresponding elements
4555 * in "ma".
4557 * If the equality is of the form
4559 * f(i) + h(j) + a x + g(i) = 0,
4561 * with f(i) a linear combinations of the parameters and input dimensions,
4562 * g(i) a linear combination of integer divisions defined in terms of the same
4563 * and h(j) a linear combinations of earlier output dimensions,
4564 * then the affine expression is
4566 * (-f(i) - g(i))/a - h(j)/a
4568 * If the equality is of the form
4570 * f(i) + h(j) - a x + g(i) = 0,
4572 * then the affine expression is
4574 * (f(i) + g(i))/a - h(j)/(-a)
4577 * If "div" refers to an integer division (i.e., it is smaller than
4578 * the number of integer divisions), then the equality constraint
4579 * does involve an integer division (the one at position "div") that
4580 * is defined in terms of output dimensions. However, this integer
4581 * division can be eliminated by exploiting a pair of constraints
4582 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4583 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4584 * -l + x >= 0.
4585 * In particular, let
4587 * x = e(i) + m floor(...)
4589 * with e(i) the expression derived above and floor(...) the integer
4590 * division involving output dimensions.
4591 * From
4593 * l <= x <= l + n,
4595 * we have
4597 * 0 <= x - l <= n
4599 * This means
4601 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4602 * = (e(i) - l) mod m
4604 * Therefore,
4606 * x - l = (e(i) - l) mod m
4608 * or
4610 * x = ((e(i) - l) mod m) + l
4612 * The variable "shift" below contains the expression -l, which may
4613 * also involve a linear combination of earlier output dimensions.
4615 static __isl_give isl_aff *extract_aff_from_equality(
4616 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4617 __isl_keep isl_multi_aff *ma)
4619 unsigned o_out;
4620 unsigned n_div, n_out;
4621 isl_ctx *ctx;
4622 isl_local_space *ls;
4623 isl_aff *aff, *shift;
4624 isl_val *mod;
4626 ctx = isl_basic_map_get_ctx(bmap);
4627 ls = isl_basic_map_get_local_space(bmap);
4628 ls = isl_local_space_domain(ls);
4629 aff = isl_aff_alloc(isl_local_space_copy(ls));
4630 if (!aff)
4631 goto error;
4632 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4633 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4634 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4635 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4636 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4637 isl_seq_cpy(aff->v->el + 1 + o_out,
4638 bmap->eq[eq] + o_out + n_out, n_div);
4639 } else {
4640 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4641 isl_seq_neg(aff->v->el + 1 + o_out,
4642 bmap->eq[eq] + o_out + n_out, n_div);
4644 if (div < n_div)
4645 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4646 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4647 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4648 bmap->eq[eq][o_out + pos]);
4649 if (div < n_div) {
4650 shift = isl_aff_alloc(isl_local_space_copy(ls));
4651 if (!shift)
4652 goto error;
4653 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4654 isl_seq_cpy(shift->v->el + 1 + o_out,
4655 bmap->ineq[ineq] + o_out + n_out, n_div);
4656 isl_int_set_si(shift->v->el[0], 1);
4657 shift = subtract_initial(shift, ma, pos,
4658 bmap->ineq[ineq] + o_out, ctx->negone);
4659 aff = isl_aff_add(aff, isl_aff_copy(shift));
4660 mod = isl_val_int_from_isl_int(ctx,
4661 bmap->eq[eq][o_out + n_out + div]);
4662 mod = isl_val_abs(mod);
4663 aff = isl_aff_mod_val(aff, mod);
4664 aff = isl_aff_sub(aff, shift);
4667 isl_local_space_free(ls);
4668 return aff;
4669 error:
4670 isl_local_space_free(ls);
4671 isl_aff_free(aff);
4672 return NULL;
4675 /* Given a basic map with output dimensions defined
4676 * in terms of the parameters input dimensions and earlier
4677 * output dimensions using an equality (and possibly a pair on inequalities),
4678 * extract an isl_aff that expresses output dimension "pos" in terms
4679 * of the parameters and input dimensions.
4680 * Note that this expression may involve integer divisions defined
4681 * in terms of parameters and input dimensions.
4682 * "ma" contains the expressions corresponding to earlier output dimensions.
4684 * This function shares some similarities with
4685 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4687 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4688 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4690 int eq, div, ineq;
4691 isl_aff *aff;
4693 if (!bmap)
4694 return NULL;
4695 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4696 if (eq >= bmap->n_eq)
4697 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4698 "unable to find suitable equality", return NULL);
4699 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4701 aff = isl_aff_remove_unused_divs(aff);
4702 return aff;
4705 /* Given a basic map where each output dimension is defined
4706 * in terms of the parameters and input dimensions using an equality,
4707 * extract an isl_multi_aff that expresses the output dimensions in terms
4708 * of the parameters and input dimensions.
4710 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4711 __isl_take isl_basic_map *bmap)
4713 int i;
4714 unsigned n_out;
4715 isl_multi_aff *ma;
4717 if (!bmap)
4718 return NULL;
4720 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4721 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4723 for (i = 0; i < n_out; ++i) {
4724 isl_aff *aff;
4726 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4727 ma = isl_multi_aff_set_aff(ma, i, aff);
4730 isl_basic_map_free(bmap);
4732 return ma;
4735 /* Given a basic set where each set dimension is defined
4736 * in terms of the parameters using an equality,
4737 * extract an isl_multi_aff that expresses the set dimensions in terms
4738 * of the parameters.
4740 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4741 __isl_take isl_basic_set *bset)
4743 return extract_isl_multi_aff_from_basic_map(bset);
4746 /* Create an isl_pw_multi_aff that is equivalent to
4747 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4748 * The given basic map is such that each output dimension is defined
4749 * in terms of the parameters and input dimensions using an equality.
4751 * Since some applications expect the result of isl_pw_multi_aff_from_map
4752 * to only contain integer affine expressions, we compute the floor
4753 * of the expression before returning.
4755 * Remove all constraints involving local variables without
4756 * an explicit representation (resulting in the removal of those
4757 * local variables) prior to the actual extraction to ensure
4758 * that the local spaces in which the resulting affine expressions
4759 * are created do not contain any unknown local variables.
4760 * Removing such constraints is safe because constraints involving
4761 * unknown local variables are not used to determine whether
4762 * a basic map is obviously single-valued.
4764 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4765 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4767 isl_multi_aff *ma;
4769 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4770 ma = extract_isl_multi_aff_from_basic_map(bmap);
4771 ma = isl_multi_aff_floor(ma);
4772 return isl_pw_multi_aff_alloc(domain, ma);
4775 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4776 * This obviously only works if the input "map" is single-valued.
4777 * If so, we compute the lexicographic minimum of the image in the form
4778 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4779 * to its lexicographic minimum.
4780 * If the input is not single-valued, we produce an error.
4782 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4783 __isl_take isl_map *map)
4785 int i;
4786 int sv;
4787 isl_pw_multi_aff *pma;
4789 sv = isl_map_is_single_valued(map);
4790 if (sv < 0)
4791 goto error;
4792 if (!sv)
4793 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4794 "map is not single-valued", goto error);
4795 map = isl_map_make_disjoint(map);
4796 if (!map)
4797 return NULL;
4799 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4801 for (i = 0; i < map->n; ++i) {
4802 isl_pw_multi_aff *pma_i;
4803 isl_basic_map *bmap;
4804 bmap = isl_basic_map_copy(map->p[i]);
4805 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4806 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4809 isl_map_free(map);
4810 return pma;
4811 error:
4812 isl_map_free(map);
4813 return NULL;
4816 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4817 * taking into account that the output dimension at position "d"
4818 * can be represented as
4820 * x = floor((e(...) + c1) / m)
4822 * given that constraint "i" is of the form
4824 * e(...) + c1 - m x >= 0
4827 * Let "map" be of the form
4829 * A -> B
4831 * We construct a mapping
4833 * A -> [A -> x = floor(...)]
4835 * apply that to the map, obtaining
4837 * [A -> x = floor(...)] -> B
4839 * and equate dimension "d" to x.
4840 * We then compute a isl_pw_multi_aff representation of the resulting map
4841 * and plug in the mapping above.
4843 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4844 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4846 isl_ctx *ctx;
4847 isl_space *space;
4848 isl_local_space *ls;
4849 isl_multi_aff *ma;
4850 isl_aff *aff;
4851 isl_vec *v;
4852 isl_map *insert;
4853 int offset;
4854 int n;
4855 int n_in;
4856 isl_pw_multi_aff *pma;
4857 isl_bool is_set;
4859 is_set = isl_map_is_set(map);
4860 if (is_set < 0)
4861 goto error;
4863 offset = isl_basic_map_offset(hull, isl_dim_out);
4864 ctx = isl_map_get_ctx(map);
4865 space = isl_space_domain(isl_map_get_space(map));
4866 n_in = isl_space_dim(space, isl_dim_set);
4867 n = isl_space_dim(space, isl_dim_all);
4869 v = isl_vec_alloc(ctx, 1 + 1 + n);
4870 if (v) {
4871 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4872 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4874 isl_basic_map_free(hull);
4876 ls = isl_local_space_from_space(isl_space_copy(space));
4877 aff = isl_aff_alloc_vec(ls, v);
4878 aff = isl_aff_floor(aff);
4879 if (is_set) {
4880 isl_space_free(space);
4881 ma = isl_multi_aff_from_aff(aff);
4882 } else {
4883 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4884 ma = isl_multi_aff_range_product(ma,
4885 isl_multi_aff_from_aff(aff));
4888 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4889 map = isl_map_apply_domain(map, insert);
4890 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4891 pma = isl_pw_multi_aff_from_map(map);
4892 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4894 return pma;
4895 error:
4896 isl_map_free(map);
4897 isl_basic_map_free(hull);
4898 return NULL;
4901 /* Is constraint "c" of the form
4903 * e(...) + c1 - m x >= 0
4905 * or
4907 * -e(...) + c2 + m x >= 0
4909 * where m > 1 and e only depends on parameters and input dimemnsions?
4911 * "offset" is the offset of the output dimensions
4912 * "pos" is the position of output dimension x.
4914 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4916 if (isl_int_is_zero(c[offset + d]))
4917 return 0;
4918 if (isl_int_is_one(c[offset + d]))
4919 return 0;
4920 if (isl_int_is_negone(c[offset + d]))
4921 return 0;
4922 if (isl_seq_first_non_zero(c + offset, d) != -1)
4923 return 0;
4924 if (isl_seq_first_non_zero(c + offset + d + 1,
4925 total - (offset + d + 1)) != -1)
4926 return 0;
4927 return 1;
4930 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4932 * As a special case, we first check if there is any pair of constraints,
4933 * shared by all the basic maps in "map" that force a given dimension
4934 * to be equal to the floor of some affine combination of the input dimensions.
4936 * In particular, if we can find two constraints
4938 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4940 * and
4942 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4944 * where m > 1 and e only depends on parameters and input dimemnsions,
4945 * and such that
4947 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4949 * then we know that we can take
4951 * x = floor((e(...) + c1) / m)
4953 * without having to perform any computation.
4955 * Note that we know that
4957 * c1 + c2 >= 1
4959 * If c1 + c2 were 0, then we would have detected an equality during
4960 * simplification. If c1 + c2 were negative, then we would have detected
4961 * a contradiction.
4963 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4964 __isl_take isl_map *map)
4966 int d, dim;
4967 int i, j, n;
4968 int offset, total;
4969 isl_int sum;
4970 isl_basic_map *hull;
4972 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4973 if (!hull)
4974 goto error;
4976 isl_int_init(sum);
4977 dim = isl_map_dim(map, isl_dim_out);
4978 offset = isl_basic_map_offset(hull, isl_dim_out);
4979 total = 1 + isl_basic_map_total_dim(hull);
4980 n = hull->n_ineq;
4981 for (d = 0; d < dim; ++d) {
4982 for (i = 0; i < n; ++i) {
4983 if (!is_potential_div_constraint(hull->ineq[i],
4984 offset, d, total))
4985 continue;
4986 for (j = i + 1; j < n; ++j) {
4987 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4988 hull->ineq[j] + 1, total - 1))
4989 continue;
4990 isl_int_add(sum, hull->ineq[i][0],
4991 hull->ineq[j][0]);
4992 if (isl_int_abs_lt(sum,
4993 hull->ineq[i][offset + d]))
4994 break;
4997 if (j >= n)
4998 continue;
4999 isl_int_clear(sum);
5000 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5001 j = i;
5002 return pw_multi_aff_from_map_div(map, hull, d, j);
5005 isl_int_clear(sum);
5006 isl_basic_map_free(hull);
5007 return pw_multi_aff_from_map_base(map);
5008 error:
5009 isl_map_free(map);
5010 isl_basic_map_free(hull);
5011 return NULL;
5014 /* Given an affine expression
5016 * [A -> B] -> f(A,B)
5018 * construct an isl_multi_aff
5020 * [A -> B] -> B'
5022 * such that dimension "d" in B' is set to "aff" and the remaining
5023 * dimensions are set equal to the corresponding dimensions in B.
5024 * "n_in" is the dimension of the space A.
5025 * "n_out" is the dimension of the space B.
5027 * If "is_set" is set, then the affine expression is of the form
5029 * [B] -> f(B)
5031 * and we construct an isl_multi_aff
5033 * B -> B'
5035 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5036 unsigned n_in, unsigned n_out, int is_set)
5038 int i;
5039 isl_multi_aff *ma;
5040 isl_space *space, *space2;
5041 isl_local_space *ls;
5043 space = isl_aff_get_domain_space(aff);
5044 ls = isl_local_space_from_space(isl_space_copy(space));
5045 space2 = isl_space_copy(space);
5046 if (!is_set)
5047 space2 = isl_space_range(isl_space_unwrap(space2));
5048 space = isl_space_map_from_domain_and_range(space, space2);
5049 ma = isl_multi_aff_alloc(space);
5050 ma = isl_multi_aff_set_aff(ma, d, aff);
5052 for (i = 0; i < n_out; ++i) {
5053 if (i == d)
5054 continue;
5055 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5056 isl_dim_set, n_in + i);
5057 ma = isl_multi_aff_set_aff(ma, i, aff);
5060 isl_local_space_free(ls);
5062 return ma;
5065 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5066 * taking into account that the dimension at position "d" can be written as
5068 * x = m a + f(..) (1)
5070 * where m is equal to "gcd".
5071 * "i" is the index of the equality in "hull" that defines f(..).
5072 * In particular, the equality is of the form
5074 * f(..) - x + m g(existentials) = 0
5076 * or
5078 * -f(..) + x + m g(existentials) = 0
5080 * We basically plug (1) into "map", resulting in a map with "a"
5081 * in the range instead of "x". The corresponding isl_pw_multi_aff
5082 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5084 * Specifically, given the input map
5086 * A -> B
5088 * We first wrap it into a set
5090 * [A -> B]
5092 * and define (1) on top of the corresponding space, resulting in "aff".
5093 * We use this to create an isl_multi_aff that maps the output position "d"
5094 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5095 * We plug this into the wrapped map, unwrap the result and compute the
5096 * corresponding isl_pw_multi_aff.
5097 * The result is an expression
5099 * A -> T(A)
5101 * We adjust that to
5103 * A -> [A -> T(A)]
5105 * so that we can plug that into "aff", after extending the latter to
5106 * a mapping
5108 * [A -> B] -> B'
5111 * If "map" is actually a set, then there is no "A" space, meaning
5112 * that we do not need to perform any wrapping, and that the result
5113 * of the recursive call is of the form
5115 * [T]
5117 * which is plugged into a mapping of the form
5119 * B -> B'
5121 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5122 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5123 isl_int gcd)
5125 isl_set *set;
5126 isl_space *space;
5127 isl_local_space *ls;
5128 isl_aff *aff;
5129 isl_multi_aff *ma;
5130 isl_pw_multi_aff *pma, *id;
5131 unsigned n_in;
5132 unsigned o_out;
5133 unsigned n_out;
5134 isl_bool is_set;
5136 is_set = isl_map_is_set(map);
5137 if (is_set < 0)
5138 goto error;
5140 n_in = isl_basic_map_dim(hull, isl_dim_in);
5141 n_out = isl_basic_map_dim(hull, isl_dim_out);
5142 o_out = isl_basic_map_offset(hull, isl_dim_out);
5144 if (is_set)
5145 set = map;
5146 else
5147 set = isl_map_wrap(map);
5148 space = isl_space_map_from_set(isl_set_get_space(set));
5149 ma = isl_multi_aff_identity(space);
5150 ls = isl_local_space_from_space(isl_set_get_space(set));
5151 aff = isl_aff_alloc(ls);
5152 if (aff) {
5153 isl_int_set_si(aff->v->el[0], 1);
5154 if (isl_int_is_one(hull->eq[i][o_out + d]))
5155 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5156 aff->v->size - 1);
5157 else
5158 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5159 aff->v->size - 1);
5160 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5162 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5163 set = isl_set_preimage_multi_aff(set, ma);
5165 ma = range_map(aff, d, n_in, n_out, is_set);
5167 if (is_set)
5168 map = set;
5169 else
5170 map = isl_set_unwrap(set);
5171 pma = isl_pw_multi_aff_from_map(map);
5173 if (!is_set) {
5174 space = isl_pw_multi_aff_get_domain_space(pma);
5175 space = isl_space_map_from_set(space);
5176 id = isl_pw_multi_aff_identity(space);
5177 pma = isl_pw_multi_aff_range_product(id, pma);
5179 id = isl_pw_multi_aff_from_multi_aff(ma);
5180 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5182 isl_basic_map_free(hull);
5183 return pma;
5184 error:
5185 isl_map_free(map);
5186 isl_basic_map_free(hull);
5187 return NULL;
5190 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5191 * "hull" contains the equalities valid for "map".
5193 * Check if any of the output dimensions is "strided".
5194 * That is, we check if it can be written as
5196 * x = m a + f(..)
5198 * with m greater than 1, a some combination of existentially quantified
5199 * variables and f an expression in the parameters and input dimensions.
5200 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5202 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5203 * special case.
5205 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5206 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5208 int i, j;
5209 unsigned n_out;
5210 unsigned o_out;
5211 unsigned n_div;
5212 unsigned o_div;
5213 isl_int gcd;
5215 n_div = isl_basic_map_dim(hull, isl_dim_div);
5216 o_div = isl_basic_map_offset(hull, isl_dim_div);
5218 if (n_div == 0) {
5219 isl_basic_map_free(hull);
5220 return pw_multi_aff_from_map_check_div(map);
5223 isl_int_init(gcd);
5225 n_out = isl_basic_map_dim(hull, isl_dim_out);
5226 o_out = isl_basic_map_offset(hull, isl_dim_out);
5228 for (i = 0; i < n_out; ++i) {
5229 for (j = 0; j < hull->n_eq; ++j) {
5230 isl_int *eq = hull->eq[j];
5231 isl_pw_multi_aff *res;
5233 if (!isl_int_is_one(eq[o_out + i]) &&
5234 !isl_int_is_negone(eq[o_out + i]))
5235 continue;
5236 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5237 continue;
5238 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5239 n_out - (i + 1)) != -1)
5240 continue;
5241 isl_seq_gcd(eq + o_div, n_div, &gcd);
5242 if (isl_int_is_zero(gcd))
5243 continue;
5244 if (isl_int_is_one(gcd))
5245 continue;
5247 res = pw_multi_aff_from_map_stride(map, hull,
5248 i, j, gcd);
5249 isl_int_clear(gcd);
5250 return res;
5254 isl_int_clear(gcd);
5255 isl_basic_map_free(hull);
5256 return pw_multi_aff_from_map_check_div(map);
5259 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5261 * As a special case, we first check if all output dimensions are uniquely
5262 * defined in terms of the parameters and input dimensions over the entire
5263 * domain. If so, we extract the desired isl_pw_multi_aff directly
5264 * from the affine hull of "map" and its domain.
5266 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5267 * special cases.
5269 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5271 isl_bool sv;
5272 isl_basic_map *hull;
5274 if (!map)
5275 return NULL;
5277 if (isl_map_n_basic_map(map) == 1) {
5278 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5279 hull = isl_basic_map_plain_affine_hull(hull);
5280 sv = isl_basic_map_plain_is_single_valued(hull);
5281 if (sv >= 0 && sv)
5282 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5283 hull);
5284 isl_basic_map_free(hull);
5286 map = isl_map_detect_equalities(map);
5287 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5288 sv = isl_basic_map_plain_is_single_valued(hull);
5289 if (sv >= 0 && sv)
5290 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5291 if (sv >= 0)
5292 return pw_multi_aff_from_map_check_strides(map, hull);
5293 isl_basic_map_free(hull);
5294 isl_map_free(map);
5295 return NULL;
5298 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5300 return isl_pw_multi_aff_from_map(set);
5303 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5304 * add it to *user.
5306 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5308 isl_union_pw_multi_aff **upma = user;
5309 isl_pw_multi_aff *pma;
5311 pma = isl_pw_multi_aff_from_map(map);
5312 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5314 return *upma ? isl_stat_ok : isl_stat_error;
5317 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5318 * domain.
5320 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5321 __isl_take isl_aff *aff)
5323 isl_multi_aff *ma;
5324 isl_pw_multi_aff *pma;
5326 ma = isl_multi_aff_from_aff(aff);
5327 pma = isl_pw_multi_aff_from_multi_aff(ma);
5328 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5331 /* Try and create an isl_union_pw_multi_aff that is equivalent
5332 * to the given isl_union_map.
5333 * The isl_union_map is required to be single-valued in each space.
5334 * Otherwise, an error is produced.
5336 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5337 __isl_take isl_union_map *umap)
5339 isl_space *space;
5340 isl_union_pw_multi_aff *upma;
5342 space = isl_union_map_get_space(umap);
5343 upma = isl_union_pw_multi_aff_empty(space);
5344 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5345 upma = isl_union_pw_multi_aff_free(upma);
5346 isl_union_map_free(umap);
5348 return upma;
5351 /* Try and create an isl_union_pw_multi_aff that is equivalent
5352 * to the given isl_union_set.
5353 * The isl_union_set is required to be a singleton in each space.
5354 * Otherwise, an error is produced.
5356 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5357 __isl_take isl_union_set *uset)
5359 return isl_union_pw_multi_aff_from_union_map(uset);
5362 /* Return the piecewise affine expression "set ? 1 : 0".
5364 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5366 isl_pw_aff *pa;
5367 isl_space *space = isl_set_get_space(set);
5368 isl_local_space *ls = isl_local_space_from_space(space);
5369 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5370 isl_aff *one = isl_aff_zero_on_domain(ls);
5372 one = isl_aff_add_constant_si(one, 1);
5373 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5374 set = isl_set_complement(set);
5375 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5377 return pa;
5380 /* Plug in "subs" for dimension "type", "pos" of "aff".
5382 * Let i be the dimension to replace and let "subs" be of the form
5384 * f/d
5386 * and "aff" of the form
5388 * (a i + g)/m
5390 * The result is
5392 * (a f + d g')/(m d)
5394 * where g' is the result of plugging in "subs" in each of the integer
5395 * divisions in g.
5397 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5398 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5400 isl_ctx *ctx;
5401 isl_int v;
5403 aff = isl_aff_cow(aff);
5404 if (!aff || !subs)
5405 return isl_aff_free(aff);
5407 ctx = isl_aff_get_ctx(aff);
5408 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5409 isl_die(ctx, isl_error_invalid,
5410 "spaces don't match", return isl_aff_free(aff));
5411 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5412 isl_die(ctx, isl_error_unsupported,
5413 "cannot handle divs yet", return isl_aff_free(aff));
5415 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5416 if (!aff->ls)
5417 return isl_aff_free(aff);
5419 aff->v = isl_vec_cow(aff->v);
5420 if (!aff->v)
5421 return isl_aff_free(aff);
5423 pos += isl_local_space_offset(aff->ls, type);
5425 isl_int_init(v);
5426 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5427 aff->v->size, subs->v->size, v);
5428 isl_int_clear(v);
5430 return aff;
5433 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5434 * expressions in "maff".
5436 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5437 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5438 __isl_keep isl_aff *subs)
5440 int i;
5442 maff = isl_multi_aff_cow(maff);
5443 if (!maff || !subs)
5444 return isl_multi_aff_free(maff);
5446 if (type == isl_dim_in)
5447 type = isl_dim_set;
5449 for (i = 0; i < maff->n; ++i) {
5450 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5451 type, pos, subs);
5452 if (!maff->u.p[i])
5453 return isl_multi_aff_free(maff);
5456 return maff;
5459 /* Plug in "subs" for dimension "type", "pos" of "pma".
5461 * pma is of the form
5463 * A_i(v) -> M_i(v)
5465 * while subs is of the form
5467 * v' = B_j(v) -> S_j
5469 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5470 * has a contribution in the result, in particular
5472 * C_ij(S_j) -> M_i(S_j)
5474 * Note that plugging in S_j in C_ij may also result in an empty set
5475 * and this contribution should simply be discarded.
5477 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5478 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5479 __isl_keep isl_pw_aff *subs)
5481 int i, j, n;
5482 isl_pw_multi_aff *res;
5484 if (!pma || !subs)
5485 return isl_pw_multi_aff_free(pma);
5487 n = pma->n * subs->n;
5488 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5490 for (i = 0; i < pma->n; ++i) {
5491 for (j = 0; j < subs->n; ++j) {
5492 isl_set *common;
5493 isl_multi_aff *res_ij;
5494 int empty;
5496 common = isl_set_intersect(
5497 isl_set_copy(pma->p[i].set),
5498 isl_set_copy(subs->p[j].set));
5499 common = isl_set_substitute(common,
5500 type, pos, subs->p[j].aff);
5501 empty = isl_set_plain_is_empty(common);
5502 if (empty < 0 || empty) {
5503 isl_set_free(common);
5504 if (empty < 0)
5505 goto error;
5506 continue;
5509 res_ij = isl_multi_aff_substitute(
5510 isl_multi_aff_copy(pma->p[i].maff),
5511 type, pos, subs->p[j].aff);
5513 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5517 isl_pw_multi_aff_free(pma);
5518 return res;
5519 error:
5520 isl_pw_multi_aff_free(pma);
5521 isl_pw_multi_aff_free(res);
5522 return NULL;
5525 /* Compute the preimage of a range of dimensions in the affine expression "src"
5526 * under "ma" and put the result in "dst". The number of dimensions in "src"
5527 * that precede the range is given by "n_before". The number of dimensions
5528 * in the range is given by the number of output dimensions of "ma".
5529 * The number of dimensions that follow the range is given by "n_after".
5530 * If "has_denom" is set (to one),
5531 * then "src" and "dst" have an extra initial denominator.
5532 * "n_div_ma" is the number of existentials in "ma"
5533 * "n_div_bset" is the number of existentials in "src"
5534 * The resulting "dst" (which is assumed to have been allocated by
5535 * the caller) contains coefficients for both sets of existentials,
5536 * first those in "ma" and then those in "src".
5537 * f, c1, c2 and g are temporary objects that have been initialized
5538 * by the caller.
5540 * Let src represent the expression
5542 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5544 * and let ma represent the expressions
5546 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5548 * We start out with the following expression for dst:
5550 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5552 * with the multiplication factor f initially equal to 1
5553 * and f \sum_i b_i v_i kept separately.
5554 * For each x_i that we substitute, we multiply the numerator
5555 * (and denominator) of dst by c_1 = m_i and add the numerator
5556 * of the x_i expression multiplied by c_2 = f b_i,
5557 * after removing the common factors of c_1 and c_2.
5558 * The multiplication factor f also needs to be multiplied by c_1
5559 * for the next x_j, j > i.
5561 void isl_seq_preimage(isl_int *dst, isl_int *src,
5562 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5563 int n_div_ma, int n_div_bmap,
5564 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5566 int i;
5567 int n_param, n_in, n_out;
5568 int o_dst, o_src;
5570 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5571 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5572 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5574 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5575 o_dst = o_src = has_denom + 1 + n_param + n_before;
5576 isl_seq_clr(dst + o_dst, n_in);
5577 o_dst += n_in;
5578 o_src += n_out;
5579 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5580 o_dst += n_after;
5581 o_src += n_after;
5582 isl_seq_clr(dst + o_dst, n_div_ma);
5583 o_dst += n_div_ma;
5584 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5586 isl_int_set_si(f, 1);
5588 for (i = 0; i < n_out; ++i) {
5589 int offset = has_denom + 1 + n_param + n_before + i;
5591 if (isl_int_is_zero(src[offset]))
5592 continue;
5593 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5594 isl_int_mul(c2, f, src[offset]);
5595 isl_int_gcd(g, c1, c2);
5596 isl_int_divexact(c1, c1, g);
5597 isl_int_divexact(c2, c2, g);
5599 isl_int_mul(f, f, c1);
5600 o_dst = has_denom;
5601 o_src = 1;
5602 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5603 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5604 o_dst += 1 + n_param;
5605 o_src += 1 + n_param;
5606 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5607 o_dst += n_before;
5608 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5609 c2, ma->u.p[i]->v->el + o_src, n_in);
5610 o_dst += n_in;
5611 o_src += n_in;
5612 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5613 o_dst += n_after;
5614 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5615 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5616 o_dst += n_div_ma;
5617 o_src += n_div_ma;
5618 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5619 if (has_denom)
5620 isl_int_mul(dst[0], dst[0], c1);
5624 /* Compute the pullback of "aff" by the function represented by "ma".
5625 * In other words, plug in "ma" in "aff". The result is an affine expression
5626 * defined over the domain space of "ma".
5628 * If "aff" is represented by
5630 * (a(p) + b x + c(divs))/d
5632 * and ma is represented by
5634 * x = D(p) + F(y) + G(divs')
5636 * then the result is
5638 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5640 * The divs in the local space of the input are similarly adjusted
5641 * through a call to isl_local_space_preimage_multi_aff.
5643 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5644 __isl_take isl_multi_aff *ma)
5646 isl_aff *res = NULL;
5647 isl_local_space *ls;
5648 int n_div_aff, n_div_ma;
5649 isl_int f, c1, c2, g;
5651 ma = isl_multi_aff_align_divs(ma);
5652 if (!aff || !ma)
5653 goto error;
5655 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5656 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5658 ls = isl_aff_get_domain_local_space(aff);
5659 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5660 res = isl_aff_alloc(ls);
5661 if (!res)
5662 goto error;
5664 isl_int_init(f);
5665 isl_int_init(c1);
5666 isl_int_init(c2);
5667 isl_int_init(g);
5669 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5670 f, c1, c2, g, 1);
5672 isl_int_clear(f);
5673 isl_int_clear(c1);
5674 isl_int_clear(c2);
5675 isl_int_clear(g);
5677 isl_aff_free(aff);
5678 isl_multi_aff_free(ma);
5679 res = isl_aff_normalize(res);
5680 return res;
5681 error:
5682 isl_aff_free(aff);
5683 isl_multi_aff_free(ma);
5684 isl_aff_free(res);
5685 return NULL;
5688 /* Compute the pullback of "aff1" by the function represented by "aff2".
5689 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5690 * defined over the domain space of "aff1".
5692 * The domain of "aff1" should match the range of "aff2", which means
5693 * that it should be single-dimensional.
5695 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5696 __isl_take isl_aff *aff2)
5698 isl_multi_aff *ma;
5700 ma = isl_multi_aff_from_aff(aff2);
5701 return isl_aff_pullback_multi_aff(aff1, ma);
5704 /* Compute the pullback of "ma1" by the function represented by "ma2".
5705 * In other words, plug in "ma2" in "ma1".
5707 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5709 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5710 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5712 int i;
5713 isl_space *space = NULL;
5715 ma2 = isl_multi_aff_align_divs(ma2);
5716 ma1 = isl_multi_aff_cow(ma1);
5717 if (!ma1 || !ma2)
5718 goto error;
5720 space = isl_space_join(isl_multi_aff_get_space(ma2),
5721 isl_multi_aff_get_space(ma1));
5723 for (i = 0; i < ma1->n; ++i) {
5724 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5725 isl_multi_aff_copy(ma2));
5726 if (!ma1->u.p[i])
5727 goto error;
5730 ma1 = isl_multi_aff_reset_space(ma1, space);
5731 isl_multi_aff_free(ma2);
5732 return ma1;
5733 error:
5734 isl_space_free(space);
5735 isl_multi_aff_free(ma2);
5736 isl_multi_aff_free(ma1);
5737 return NULL;
5740 /* Compute the pullback of "ma1" by the function represented by "ma2".
5741 * In other words, plug in "ma2" in "ma1".
5743 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5744 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5746 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5747 &isl_multi_aff_pullback_multi_aff_aligned);
5750 /* Extend the local space of "dst" to include the divs
5751 * in the local space of "src".
5753 * If "src" does not have any divs or if the local spaces of "dst" and
5754 * "src" are the same, then no extension is required.
5756 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5757 __isl_keep isl_aff *src)
5759 isl_ctx *ctx;
5760 int src_n_div, dst_n_div;
5761 int *exp1 = NULL;
5762 int *exp2 = NULL;
5763 isl_bool equal;
5764 isl_mat *div;
5766 if (!src || !dst)
5767 return isl_aff_free(dst);
5769 ctx = isl_aff_get_ctx(src);
5770 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5771 if (equal < 0)
5772 return isl_aff_free(dst);
5773 if (!equal)
5774 isl_die(ctx, isl_error_invalid,
5775 "spaces don't match", goto error);
5777 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5778 if (src_n_div == 0)
5779 return dst;
5780 equal = isl_local_space_is_equal(src->ls, dst->ls);
5781 if (equal < 0)
5782 return isl_aff_free(dst);
5783 if (equal)
5784 return dst;
5786 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5787 exp1 = isl_alloc_array(ctx, int, src_n_div);
5788 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5789 if (!exp1 || (dst_n_div && !exp2))
5790 goto error;
5792 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5793 dst = isl_aff_expand_divs(dst, div, exp2);
5794 free(exp1);
5795 free(exp2);
5797 return dst;
5798 error:
5799 free(exp1);
5800 free(exp2);
5801 return isl_aff_free(dst);
5804 /* Adjust the local spaces of the affine expressions in "maff"
5805 * such that they all have the save divs.
5807 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5808 __isl_take isl_multi_aff *maff)
5810 int i;
5812 if (!maff)
5813 return NULL;
5814 if (maff->n == 0)
5815 return maff;
5816 maff = isl_multi_aff_cow(maff);
5817 if (!maff)
5818 return NULL;
5820 for (i = 1; i < maff->n; ++i)
5821 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5822 for (i = 1; i < maff->n; ++i) {
5823 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5824 if (!maff->u.p[i])
5825 return isl_multi_aff_free(maff);
5828 return maff;
5831 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5833 aff = isl_aff_cow(aff);
5834 if (!aff)
5835 return NULL;
5837 aff->ls = isl_local_space_lift(aff->ls);
5838 if (!aff->ls)
5839 return isl_aff_free(aff);
5841 return aff;
5844 /* Lift "maff" to a space with extra dimensions such that the result
5845 * has no more existentially quantified variables.
5846 * If "ls" is not NULL, then *ls is assigned the local space that lies
5847 * at the basis of the lifting applied to "maff".
5849 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5850 __isl_give isl_local_space **ls)
5852 int i;
5853 isl_space *space;
5854 unsigned n_div;
5856 if (ls)
5857 *ls = NULL;
5859 if (!maff)
5860 return NULL;
5862 if (maff->n == 0) {
5863 if (ls) {
5864 isl_space *space = isl_multi_aff_get_domain_space(maff);
5865 *ls = isl_local_space_from_space(space);
5866 if (!*ls)
5867 return isl_multi_aff_free(maff);
5869 return maff;
5872 maff = isl_multi_aff_cow(maff);
5873 maff = isl_multi_aff_align_divs(maff);
5874 if (!maff)
5875 return NULL;
5877 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5878 space = isl_multi_aff_get_space(maff);
5879 space = isl_space_lift(isl_space_domain(space), n_div);
5880 space = isl_space_extend_domain_with_range(space,
5881 isl_multi_aff_get_space(maff));
5882 if (!space)
5883 return isl_multi_aff_free(maff);
5884 isl_space_free(maff->space);
5885 maff->space = space;
5887 if (ls) {
5888 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5889 if (!*ls)
5890 return isl_multi_aff_free(maff);
5893 for (i = 0; i < maff->n; ++i) {
5894 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5895 if (!maff->u.p[i])
5896 goto error;
5899 return maff;
5900 error:
5901 if (ls)
5902 isl_local_space_free(*ls);
5903 return isl_multi_aff_free(maff);
5907 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5909 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5910 __isl_keep isl_pw_multi_aff *pma, int pos)
5912 int i;
5913 int n_out;
5914 isl_space *space;
5915 isl_pw_aff *pa;
5917 if (!pma)
5918 return NULL;
5920 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5921 if (pos < 0 || pos >= n_out)
5922 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5923 "index out of bounds", return NULL);
5925 space = isl_pw_multi_aff_get_space(pma);
5926 space = isl_space_drop_dims(space, isl_dim_out,
5927 pos + 1, n_out - pos - 1);
5928 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5930 pa = isl_pw_aff_alloc_size(space, pma->n);
5931 for (i = 0; i < pma->n; ++i) {
5932 isl_aff *aff;
5933 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5934 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5937 return pa;
5940 /* Return an isl_pw_multi_aff with the given "set" as domain and
5941 * an unnamed zero-dimensional range.
5943 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5944 __isl_take isl_set *set)
5946 isl_multi_aff *ma;
5947 isl_space *space;
5949 space = isl_set_get_space(set);
5950 space = isl_space_from_domain(space);
5951 ma = isl_multi_aff_zero(space);
5952 return isl_pw_multi_aff_alloc(set, ma);
5955 /* Add an isl_pw_multi_aff with the given "set" as domain and
5956 * an unnamed zero-dimensional range to *user.
5958 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5959 void *user)
5961 isl_union_pw_multi_aff **upma = user;
5962 isl_pw_multi_aff *pma;
5964 pma = isl_pw_multi_aff_from_domain(set);
5965 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5967 return isl_stat_ok;
5970 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5971 * an unnamed zero-dimensional range.
5973 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5974 __isl_take isl_union_set *uset)
5976 isl_space *space;
5977 isl_union_pw_multi_aff *upma;
5979 if (!uset)
5980 return NULL;
5982 space = isl_union_set_get_space(uset);
5983 upma = isl_union_pw_multi_aff_empty(space);
5985 if (isl_union_set_foreach_set(uset,
5986 &add_pw_multi_aff_from_domain, &upma) < 0)
5987 goto error;
5989 isl_union_set_free(uset);
5990 return upma;
5991 error:
5992 isl_union_set_free(uset);
5993 isl_union_pw_multi_aff_free(upma);
5994 return NULL;
5997 /* Convert "pma" to an isl_map and add it to *umap.
5999 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
6000 void *user)
6002 isl_union_map **umap = user;
6003 isl_map *map;
6005 map = isl_map_from_pw_multi_aff(pma);
6006 *umap = isl_union_map_add_map(*umap, map);
6008 return isl_stat_ok;
6011 /* Construct a union map mapping the domain of the union
6012 * piecewise multi-affine expression to its range, with each dimension
6013 * in the range equated to the corresponding affine expression on its cell.
6015 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
6016 __isl_take isl_union_pw_multi_aff *upma)
6018 isl_space *space;
6019 isl_union_map *umap;
6021 if (!upma)
6022 return NULL;
6024 space = isl_union_pw_multi_aff_get_space(upma);
6025 umap = isl_union_map_empty(space);
6027 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
6028 &map_from_pw_multi_aff, &umap) < 0)
6029 goto error;
6031 isl_union_pw_multi_aff_free(upma);
6032 return umap;
6033 error:
6034 isl_union_pw_multi_aff_free(upma);
6035 isl_union_map_free(umap);
6036 return NULL;
6039 /* Local data for bin_entry and the callback "fn".
6041 struct isl_union_pw_multi_aff_bin_data {
6042 isl_union_pw_multi_aff *upma2;
6043 isl_union_pw_multi_aff *res;
6044 isl_pw_multi_aff *pma;
6045 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6048 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6049 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6051 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6053 struct isl_union_pw_multi_aff_bin_data *data = user;
6054 isl_stat r;
6056 data->pma = pma;
6057 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6058 data->fn, data);
6059 isl_pw_multi_aff_free(pma);
6061 return r;
6064 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6065 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6066 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6067 * as *entry. The callback should adjust data->res if desired.
6069 static __isl_give isl_union_pw_multi_aff *bin_op(
6070 __isl_take isl_union_pw_multi_aff *upma1,
6071 __isl_take isl_union_pw_multi_aff *upma2,
6072 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6074 isl_space *space;
6075 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6077 space = isl_union_pw_multi_aff_get_space(upma2);
6078 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6079 space = isl_union_pw_multi_aff_get_space(upma1);
6080 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6082 if (!upma1 || !upma2)
6083 goto error;
6085 data.upma2 = upma2;
6086 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6087 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6088 &bin_entry, &data) < 0)
6089 goto error;
6091 isl_union_pw_multi_aff_free(upma1);
6092 isl_union_pw_multi_aff_free(upma2);
6093 return data.res;
6094 error:
6095 isl_union_pw_multi_aff_free(upma1);
6096 isl_union_pw_multi_aff_free(upma2);
6097 isl_union_pw_multi_aff_free(data.res);
6098 return NULL;
6101 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6102 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6104 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6105 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6107 isl_space *space;
6109 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6110 isl_pw_multi_aff_get_space(pma2));
6111 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6112 &isl_multi_aff_range_product);
6115 /* Given two isl_pw_multi_affs A -> B and C -> D,
6116 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6118 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6119 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6121 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6122 &pw_multi_aff_range_product);
6125 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6126 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6128 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6129 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6131 isl_space *space;
6133 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6134 isl_pw_multi_aff_get_space(pma2));
6135 space = isl_space_flatten_range(space);
6136 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6137 &isl_multi_aff_flat_range_product);
6140 /* Given two isl_pw_multi_affs A -> B and C -> D,
6141 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6143 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6144 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6146 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6147 &pw_multi_aff_flat_range_product);
6150 /* If data->pma and "pma2" have the same domain space, then compute
6151 * their flat range product and the result to data->res.
6153 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6154 void *user)
6156 struct isl_union_pw_multi_aff_bin_data *data = user;
6158 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6159 pma2->dim, isl_dim_in)) {
6160 isl_pw_multi_aff_free(pma2);
6161 return isl_stat_ok;
6164 pma2 = isl_pw_multi_aff_flat_range_product(
6165 isl_pw_multi_aff_copy(data->pma), pma2);
6167 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6169 return isl_stat_ok;
6172 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6173 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6175 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6176 __isl_take isl_union_pw_multi_aff *upma1,
6177 __isl_take isl_union_pw_multi_aff *upma2)
6179 return bin_op(upma1, upma2, &flat_range_product_entry);
6182 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6183 * The parameters are assumed to have been aligned.
6185 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6186 * except that it works on two different isl_pw_* types.
6188 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6189 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6190 __isl_take isl_pw_aff *pa)
6192 int i, j, n;
6193 isl_pw_multi_aff *res = NULL;
6195 if (!pma || !pa)
6196 goto error;
6198 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6199 pa->dim, isl_dim_in))
6200 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6201 "domains don't match", goto error);
6202 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6203 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6204 "index out of bounds", goto error);
6206 n = pma->n * pa->n;
6207 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6209 for (i = 0; i < pma->n; ++i) {
6210 for (j = 0; j < pa->n; ++j) {
6211 isl_set *common;
6212 isl_multi_aff *res_ij;
6213 int empty;
6215 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6216 isl_set_copy(pa->p[j].set));
6217 empty = isl_set_plain_is_empty(common);
6218 if (empty < 0 || empty) {
6219 isl_set_free(common);
6220 if (empty < 0)
6221 goto error;
6222 continue;
6225 res_ij = isl_multi_aff_set_aff(
6226 isl_multi_aff_copy(pma->p[i].maff), pos,
6227 isl_aff_copy(pa->p[j].aff));
6228 res_ij = isl_multi_aff_gist(res_ij,
6229 isl_set_copy(common));
6231 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6235 isl_pw_multi_aff_free(pma);
6236 isl_pw_aff_free(pa);
6237 return res;
6238 error:
6239 isl_pw_multi_aff_free(pma);
6240 isl_pw_aff_free(pa);
6241 return isl_pw_multi_aff_free(res);
6244 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6246 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6247 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6248 __isl_take isl_pw_aff *pa)
6250 isl_bool equal_params;
6252 if (!pma || !pa)
6253 goto error;
6254 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6255 if (equal_params < 0)
6256 goto error;
6257 if (equal_params)
6258 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6259 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6260 isl_pw_aff_check_named_params(pa) < 0)
6261 goto error;
6262 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6263 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6264 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6265 error:
6266 isl_pw_multi_aff_free(pma);
6267 isl_pw_aff_free(pa);
6268 return NULL;
6271 /* Do the parameters of "pa" match those of "space"?
6273 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6274 __isl_keep isl_space *space)
6276 isl_space *pa_space;
6277 isl_bool match;
6279 if (!pa || !space)
6280 return isl_bool_error;
6282 pa_space = isl_pw_aff_get_space(pa);
6284 match = isl_space_has_equal_params(space, pa_space);
6286 isl_space_free(pa_space);
6287 return match;
6290 /* Check that the domain space of "pa" matches "space".
6292 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6293 __isl_keep isl_space *space)
6295 isl_space *pa_space;
6296 isl_bool match;
6298 if (!pa || !space)
6299 return isl_stat_error;
6301 pa_space = isl_pw_aff_get_space(pa);
6303 match = isl_space_has_equal_params(space, pa_space);
6304 if (match < 0)
6305 goto error;
6306 if (!match)
6307 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6308 "parameters don't match", goto error);
6309 match = isl_space_tuple_is_equal(space, isl_dim_in,
6310 pa_space, isl_dim_in);
6311 if (match < 0)
6312 goto error;
6313 if (!match)
6314 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6315 "domains don't match", goto error);
6316 isl_space_free(pa_space);
6317 return isl_stat_ok;
6318 error:
6319 isl_space_free(pa_space);
6320 return isl_stat_error;
6323 #undef BASE
6324 #define BASE pw_aff
6325 #undef DOMBASE
6326 #define DOMBASE set
6328 #include <isl_multi_explicit_domain.c>
6329 #include <isl_multi_pw_aff_explicit_domain.c>
6330 #include <isl_multi_templ.c>
6331 #include <isl_multi_apply_set.c>
6332 #include <isl_multi_coalesce.c>
6333 #include <isl_multi_dims.c>
6334 #include <isl_multi_gist.c>
6335 #include <isl_multi_hash.c>
6336 #include <isl_multi_align_set.c>
6337 #include <isl_multi_intersect.c>
6339 /* Does "mpa" have a non-trivial explicit domain?
6341 * The explicit domain, if present, is trivial if it represents
6342 * an (obviously) universe set.
6344 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6345 __isl_keep isl_multi_pw_aff *mpa)
6347 if (!mpa)
6348 return isl_bool_error;
6349 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6350 return isl_bool_false;
6351 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6354 /* Scale the elements of "pma" by the corresponding elements of "mv".
6356 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6357 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6359 int i;
6360 isl_bool equal_params;
6362 pma = isl_pw_multi_aff_cow(pma);
6363 if (!pma || !mv)
6364 goto error;
6365 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6366 mv->space, isl_dim_set))
6367 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6368 "spaces don't match", goto error);
6369 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6370 if (equal_params < 0)
6371 goto error;
6372 if (!equal_params) {
6373 pma = isl_pw_multi_aff_align_params(pma,
6374 isl_multi_val_get_space(mv));
6375 mv = isl_multi_val_align_params(mv,
6376 isl_pw_multi_aff_get_space(pma));
6377 if (!pma || !mv)
6378 goto error;
6381 for (i = 0; i < pma->n; ++i) {
6382 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6383 isl_multi_val_copy(mv));
6384 if (!pma->p[i].maff)
6385 goto error;
6388 isl_multi_val_free(mv);
6389 return pma;
6390 error:
6391 isl_multi_val_free(mv);
6392 isl_pw_multi_aff_free(pma);
6393 return NULL;
6396 /* This function is called for each entry of an isl_union_pw_multi_aff.
6397 * If the space of the entry matches that of data->mv,
6398 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6399 * Otherwise, return an empty isl_pw_multi_aff.
6401 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6402 __isl_take isl_pw_multi_aff *pma, void *user)
6404 isl_multi_val *mv = user;
6406 if (!pma)
6407 return NULL;
6408 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6409 mv->space, isl_dim_set)) {
6410 isl_space *space = isl_pw_multi_aff_get_space(pma);
6411 isl_pw_multi_aff_free(pma);
6412 return isl_pw_multi_aff_empty(space);
6415 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6418 /* Scale the elements of "upma" by the corresponding elements of "mv",
6419 * for those entries that match the space of "mv".
6421 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6422 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6424 upma = isl_union_pw_multi_aff_align_params(upma,
6425 isl_multi_val_get_space(mv));
6426 mv = isl_multi_val_align_params(mv,
6427 isl_union_pw_multi_aff_get_space(upma));
6428 if (!upma || !mv)
6429 goto error;
6431 return isl_union_pw_multi_aff_transform(upma,
6432 &union_pw_multi_aff_scale_multi_val_entry, mv);
6434 isl_multi_val_free(mv);
6435 return upma;
6436 error:
6437 isl_multi_val_free(mv);
6438 isl_union_pw_multi_aff_free(upma);
6439 return NULL;
6442 /* Construct and return a piecewise multi affine expression
6443 * in the given space with value zero in each of the output dimensions and
6444 * a universe domain.
6446 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6448 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6451 /* Construct and return a piecewise multi affine expression
6452 * that is equal to the given piecewise affine expression.
6454 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6455 __isl_take isl_pw_aff *pa)
6457 int i;
6458 isl_space *space;
6459 isl_pw_multi_aff *pma;
6461 if (!pa)
6462 return NULL;
6464 space = isl_pw_aff_get_space(pa);
6465 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6467 for (i = 0; i < pa->n; ++i) {
6468 isl_set *set;
6469 isl_multi_aff *ma;
6471 set = isl_set_copy(pa->p[i].set);
6472 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6473 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6476 isl_pw_aff_free(pa);
6477 return pma;
6480 /* Construct a set or map mapping the shared (parameter) domain
6481 * of the piecewise affine expressions to the range of "mpa"
6482 * with each dimension in the range equated to the
6483 * corresponding piecewise affine expression.
6485 static __isl_give isl_map *map_from_multi_pw_aff(
6486 __isl_take isl_multi_pw_aff *mpa)
6488 int i;
6489 isl_space *space;
6490 isl_map *map;
6492 if (!mpa)
6493 return NULL;
6495 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6496 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6497 "invalid space", goto error);
6499 space = isl_multi_pw_aff_get_domain_space(mpa);
6500 map = isl_map_universe(isl_space_from_domain(space));
6502 for (i = 0; i < mpa->n; ++i) {
6503 isl_pw_aff *pa;
6504 isl_map *map_i;
6506 pa = isl_pw_aff_copy(mpa->u.p[i]);
6507 map_i = map_from_pw_aff(pa);
6509 map = isl_map_flat_range_product(map, map_i);
6512 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6514 isl_multi_pw_aff_free(mpa);
6515 return map;
6516 error:
6517 isl_multi_pw_aff_free(mpa);
6518 return NULL;
6521 /* Construct a map mapping the shared domain
6522 * of the piecewise affine expressions to the range of "mpa"
6523 * with each dimension in the range equated to the
6524 * corresponding piecewise affine expression.
6526 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6528 if (!mpa)
6529 return NULL;
6530 if (isl_space_is_set(mpa->space))
6531 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6532 "space of input is not a map", goto error);
6534 return map_from_multi_pw_aff(mpa);
6535 error:
6536 isl_multi_pw_aff_free(mpa);
6537 return NULL;
6540 /* Construct a set mapping the shared parameter domain
6541 * of the piecewise affine expressions to the space of "mpa"
6542 * with each dimension in the range equated to the
6543 * corresponding piecewise affine expression.
6545 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6547 if (!mpa)
6548 return NULL;
6549 if (!isl_space_is_set(mpa->space))
6550 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6551 "space of input is not a set", goto error);
6553 return map_from_multi_pw_aff(mpa);
6554 error:
6555 isl_multi_pw_aff_free(mpa);
6556 return NULL;
6559 /* Construct and return a piecewise multi affine expression
6560 * that is equal to the given multi piecewise affine expression
6561 * on the shared domain of the piecewise affine expressions,
6562 * in the special case of a 0D multi piecewise affine expression.
6564 * Create a piecewise multi affine expression with the explicit domain of
6565 * the 0D multi piecewise affine expression as domain.
6567 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6568 __isl_take isl_multi_pw_aff *mpa)
6570 isl_space *space;
6571 isl_set *dom;
6572 isl_multi_aff *ma;
6574 space = isl_multi_pw_aff_get_space(mpa);
6575 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6576 isl_multi_pw_aff_free(mpa);
6578 ma = isl_multi_aff_zero(space);
6579 return isl_pw_multi_aff_alloc(dom, ma);
6582 /* Construct and return a piecewise multi affine expression
6583 * that is equal to the given multi piecewise affine expression
6584 * on the shared domain of the piecewise affine expressions.
6586 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6587 __isl_take isl_multi_pw_aff *mpa)
6589 int i;
6590 isl_space *space;
6591 isl_pw_aff *pa;
6592 isl_pw_multi_aff *pma;
6594 if (!mpa)
6595 return NULL;
6597 if (mpa->n == 0)
6598 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6600 space = isl_multi_pw_aff_get_space(mpa);
6601 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6602 pma = isl_pw_multi_aff_from_pw_aff(pa);
6604 for (i = 1; i < mpa->n; ++i) {
6605 isl_pw_multi_aff *pma_i;
6607 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6608 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6609 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6612 pma = isl_pw_multi_aff_reset_space(pma, space);
6614 isl_multi_pw_aff_free(mpa);
6615 return pma;
6618 /* Construct and return a multi piecewise affine expression
6619 * that is equal to the given multi affine expression.
6621 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6622 __isl_take isl_multi_aff *ma)
6624 int i, n;
6625 isl_multi_pw_aff *mpa;
6627 if (!ma)
6628 return NULL;
6630 n = isl_multi_aff_dim(ma, isl_dim_out);
6631 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6633 for (i = 0; i < n; ++i) {
6634 isl_pw_aff *pa;
6636 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6637 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6640 isl_multi_aff_free(ma);
6641 return mpa;
6644 /* Construct and return a multi piecewise affine expression
6645 * that is equal to the given piecewise multi affine expression.
6647 * If the resulting multi piecewise affine expression has
6648 * an explicit domain, then assign it the domain of the input.
6649 * In other cases, the domain is stored in the individual elements.
6651 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6652 __isl_take isl_pw_multi_aff *pma)
6654 int i, n;
6655 isl_space *space;
6656 isl_multi_pw_aff *mpa;
6658 if (!pma)
6659 return NULL;
6661 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6662 space = isl_pw_multi_aff_get_space(pma);
6663 mpa = isl_multi_pw_aff_alloc(space);
6665 for (i = 0; i < n; ++i) {
6666 isl_pw_aff *pa;
6668 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6669 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6671 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6672 isl_set *dom;
6674 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6675 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6678 isl_pw_multi_aff_free(pma);
6679 return mpa;
6682 /* Do "pa1" and "pa2" represent the same function?
6684 * We first check if they are obviously equal.
6685 * If not, we convert them to maps and check if those are equal.
6687 * If "pa1" or "pa2" contain any NaNs, then they are considered
6688 * not to be the same. A NaN is not equal to anything, not even
6689 * to another NaN.
6691 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6692 __isl_keep isl_pw_aff *pa2)
6694 isl_bool equal;
6695 isl_bool has_nan;
6696 isl_map *map1, *map2;
6698 if (!pa1 || !pa2)
6699 return isl_bool_error;
6701 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6702 if (equal < 0 || equal)
6703 return equal;
6704 has_nan = either_involves_nan(pa1, pa2);
6705 if (has_nan < 0)
6706 return isl_bool_error;
6707 if (has_nan)
6708 return isl_bool_false;
6710 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6711 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6712 equal = isl_map_is_equal(map1, map2);
6713 isl_map_free(map1);
6714 isl_map_free(map2);
6716 return equal;
6719 /* Do "mpa1" and "mpa2" represent the same function?
6721 * Note that we cannot convert the entire isl_multi_pw_aff
6722 * to a map because the domains of the piecewise affine expressions
6723 * may not be the same.
6725 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6726 __isl_keep isl_multi_pw_aff *mpa2)
6728 int i;
6729 isl_bool equal, equal_params;
6731 if (!mpa1 || !mpa2)
6732 return isl_bool_error;
6734 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6735 if (equal_params < 0)
6736 return isl_bool_error;
6737 if (!equal_params) {
6738 if (!isl_space_has_named_params(mpa1->space))
6739 return isl_bool_false;
6740 if (!isl_space_has_named_params(mpa2->space))
6741 return isl_bool_false;
6742 mpa1 = isl_multi_pw_aff_copy(mpa1);
6743 mpa2 = isl_multi_pw_aff_copy(mpa2);
6744 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6745 isl_multi_pw_aff_get_space(mpa2));
6746 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6747 isl_multi_pw_aff_get_space(mpa1));
6748 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6749 isl_multi_pw_aff_free(mpa1);
6750 isl_multi_pw_aff_free(mpa2);
6751 return equal;
6754 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6755 if (equal < 0 || !equal)
6756 return equal;
6758 for (i = 0; i < mpa1->n; ++i) {
6759 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6760 if (equal < 0 || !equal)
6761 return equal;
6764 return isl_bool_true;
6767 /* Do "pma1" and "pma2" represent the same function?
6769 * First check if they are obviously equal.
6770 * If not, then convert them to maps and check if those are equal.
6772 * If "pa1" or "pa2" contain any NaNs, then they are considered
6773 * not to be the same. A NaN is not equal to anything, not even
6774 * to another NaN.
6776 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6777 __isl_keep isl_pw_multi_aff *pma2)
6779 isl_bool equal;
6780 isl_bool has_nan;
6781 isl_map *map1, *map2;
6783 if (!pma1 || !pma2)
6784 return isl_bool_error;
6786 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6787 if (equal < 0 || equal)
6788 return equal;
6789 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6790 if (has_nan >= 0 && !has_nan)
6791 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6792 if (has_nan < 0 || has_nan)
6793 return isl_bool_not(has_nan);
6795 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6796 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6797 equal = isl_map_is_equal(map1, map2);
6798 isl_map_free(map1);
6799 isl_map_free(map2);
6801 return equal;
6804 /* Compute the pullback of "mpa" by the function represented by "ma".
6805 * In other words, plug in "ma" in "mpa".
6807 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6809 * If "mpa" has an explicit domain, then it is this domain
6810 * that needs to undergo a pullback, i.e., a preimage.
6812 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6813 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6815 int i;
6816 isl_space *space = NULL;
6818 mpa = isl_multi_pw_aff_cow(mpa);
6819 if (!mpa || !ma)
6820 goto error;
6822 space = isl_space_join(isl_multi_aff_get_space(ma),
6823 isl_multi_pw_aff_get_space(mpa));
6824 if (!space)
6825 goto error;
6827 for (i = 0; i < mpa->n; ++i) {
6828 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6829 isl_multi_aff_copy(ma));
6830 if (!mpa->u.p[i])
6831 goto error;
6833 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6834 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6835 isl_multi_aff_copy(ma));
6836 if (!mpa->u.dom)
6837 goto error;
6840 isl_multi_aff_free(ma);
6841 isl_space_free(mpa->space);
6842 mpa->space = space;
6843 return mpa;
6844 error:
6845 isl_space_free(space);
6846 isl_multi_pw_aff_free(mpa);
6847 isl_multi_aff_free(ma);
6848 return NULL;
6851 /* Compute the pullback of "mpa" by the function represented by "ma".
6852 * In other words, plug in "ma" in "mpa".
6854 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6855 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6857 isl_bool equal_params;
6859 if (!mpa || !ma)
6860 goto error;
6861 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6862 if (equal_params < 0)
6863 goto error;
6864 if (equal_params)
6865 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6866 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6867 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6868 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6869 error:
6870 isl_multi_pw_aff_free(mpa);
6871 isl_multi_aff_free(ma);
6872 return NULL;
6875 /* Compute the pullback of "mpa" by the function represented by "pma".
6876 * In other words, plug in "pma" in "mpa".
6878 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6880 * If "mpa" has an explicit domain, then it is this domain
6881 * that needs to undergo a pullback, i.e., a preimage.
6883 static __isl_give isl_multi_pw_aff *
6884 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6885 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6887 int i;
6888 isl_space *space = NULL;
6890 mpa = isl_multi_pw_aff_cow(mpa);
6891 if (!mpa || !pma)
6892 goto error;
6894 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6895 isl_multi_pw_aff_get_space(mpa));
6897 for (i = 0; i < mpa->n; ++i) {
6898 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6899 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6900 if (!mpa->u.p[i])
6901 goto error;
6903 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6904 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6905 isl_pw_multi_aff_copy(pma));
6906 if (!mpa->u.dom)
6907 goto error;
6910 isl_pw_multi_aff_free(pma);
6911 isl_space_free(mpa->space);
6912 mpa->space = space;
6913 return mpa;
6914 error:
6915 isl_space_free(space);
6916 isl_multi_pw_aff_free(mpa);
6917 isl_pw_multi_aff_free(pma);
6918 return NULL;
6921 /* Compute the pullback of "mpa" by the function represented by "pma".
6922 * In other words, plug in "pma" in "mpa".
6924 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6925 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6927 isl_bool equal_params;
6929 if (!mpa || !pma)
6930 goto error;
6931 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6932 if (equal_params < 0)
6933 goto error;
6934 if (equal_params)
6935 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6936 mpa = isl_multi_pw_aff_align_params(mpa,
6937 isl_pw_multi_aff_get_space(pma));
6938 pma = isl_pw_multi_aff_align_params(pma,
6939 isl_multi_pw_aff_get_space(mpa));
6940 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6941 error:
6942 isl_multi_pw_aff_free(mpa);
6943 isl_pw_multi_aff_free(pma);
6944 return NULL;
6947 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6948 * with the domain of "aff". The domain of the result is the same
6949 * as that of "mpa".
6950 * "mpa" and "aff" are assumed to have been aligned.
6952 * We first extract the parametric constant from "aff", defined
6953 * over the correct domain.
6954 * Then we add the appropriate combinations of the members of "mpa".
6955 * Finally, we add the integer divisions through recursive calls.
6957 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6958 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6960 int i, n_in, n_div;
6961 isl_space *space;
6962 isl_val *v;
6963 isl_pw_aff *pa;
6964 isl_aff *tmp;
6966 n_in = isl_aff_dim(aff, isl_dim_in);
6967 n_div = isl_aff_dim(aff, isl_dim_div);
6969 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6970 tmp = isl_aff_copy(aff);
6971 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6972 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6973 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6974 isl_space_dim(space, isl_dim_set));
6975 tmp = isl_aff_reset_domain_space(tmp, space);
6976 pa = isl_pw_aff_from_aff(tmp);
6978 for (i = 0; i < n_in; ++i) {
6979 isl_pw_aff *pa_i;
6981 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6982 continue;
6983 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6984 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6985 pa_i = isl_pw_aff_scale_val(pa_i, v);
6986 pa = isl_pw_aff_add(pa, pa_i);
6989 for (i = 0; i < n_div; ++i) {
6990 isl_aff *div;
6991 isl_pw_aff *pa_i;
6993 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6994 continue;
6995 div = isl_aff_get_div(aff, i);
6996 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6997 isl_multi_pw_aff_copy(mpa), div);
6998 pa_i = isl_pw_aff_floor(pa_i);
6999 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7000 pa_i = isl_pw_aff_scale_val(pa_i, v);
7001 pa = isl_pw_aff_add(pa, pa_i);
7004 isl_multi_pw_aff_free(mpa);
7005 isl_aff_free(aff);
7007 return pa;
7010 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7011 * with the domain of "aff". The domain of the result is the same
7012 * as that of "mpa".
7014 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7015 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7017 isl_bool equal_params;
7019 if (!aff || !mpa)
7020 goto error;
7021 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7022 if (equal_params < 0)
7023 goto error;
7024 if (equal_params)
7025 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7027 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7028 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7030 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7031 error:
7032 isl_aff_free(aff);
7033 isl_multi_pw_aff_free(mpa);
7034 return NULL;
7037 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7038 * with the domain of "pa". The domain of the result is the same
7039 * as that of "mpa".
7040 * "mpa" and "pa" are assumed to have been aligned.
7042 * We consider each piece in turn. Note that the domains of the
7043 * pieces are assumed to be disjoint and they remain disjoint
7044 * after taking the preimage (over the same function).
7046 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7047 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7049 isl_space *space;
7050 isl_pw_aff *res;
7051 int i;
7053 if (!mpa || !pa)
7054 goto error;
7056 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7057 isl_pw_aff_get_space(pa));
7058 res = isl_pw_aff_empty(space);
7060 for (i = 0; i < pa->n; ++i) {
7061 isl_pw_aff *pa_i;
7062 isl_set *domain;
7064 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7065 isl_multi_pw_aff_copy(mpa),
7066 isl_aff_copy(pa->p[i].aff));
7067 domain = isl_set_copy(pa->p[i].set);
7068 domain = isl_set_preimage_multi_pw_aff(domain,
7069 isl_multi_pw_aff_copy(mpa));
7070 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7071 res = isl_pw_aff_add_disjoint(res, pa_i);
7074 isl_pw_aff_free(pa);
7075 isl_multi_pw_aff_free(mpa);
7076 return res;
7077 error:
7078 isl_pw_aff_free(pa);
7079 isl_multi_pw_aff_free(mpa);
7080 return NULL;
7083 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7084 * with the domain of "pa". The domain of the result is the same
7085 * as that of "mpa".
7087 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7088 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7090 isl_bool equal_params;
7092 if (!pa || !mpa)
7093 goto error;
7094 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7095 if (equal_params < 0)
7096 goto error;
7097 if (equal_params)
7098 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7100 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7101 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7103 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7104 error:
7105 isl_pw_aff_free(pa);
7106 isl_multi_pw_aff_free(mpa);
7107 return NULL;
7110 /* Compute the pullback of "pa" by the function represented by "mpa".
7111 * In other words, plug in "mpa" in "pa".
7112 * "pa" and "mpa" are assumed to have been aligned.
7114 * The pullback is computed by applying "pa" to "mpa".
7116 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7117 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7119 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7122 /* Compute the pullback of "pa" by the function represented by "mpa".
7123 * In other words, plug in "mpa" in "pa".
7125 * The pullback is computed by applying "pa" to "mpa".
7127 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7128 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7130 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7133 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7134 * In other words, plug in "mpa2" in "mpa1".
7136 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7138 * We pullback each member of "mpa1" in turn.
7140 * If "mpa1" has an explicit domain, then it is this domain
7141 * that needs to undergo a pullback instead, i.e., a preimage.
7143 static __isl_give isl_multi_pw_aff *
7144 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7145 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7147 int i;
7148 isl_space *space = NULL;
7150 mpa1 = isl_multi_pw_aff_cow(mpa1);
7151 if (!mpa1 || !mpa2)
7152 goto error;
7154 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7155 isl_multi_pw_aff_get_space(mpa1));
7157 for (i = 0; i < mpa1->n; ++i) {
7158 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7159 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7160 if (!mpa1->u.p[i])
7161 goto error;
7164 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7165 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7166 isl_multi_pw_aff_copy(mpa2));
7167 if (!mpa1->u.dom)
7168 goto error;
7170 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7172 isl_multi_pw_aff_free(mpa2);
7173 return mpa1;
7174 error:
7175 isl_space_free(space);
7176 isl_multi_pw_aff_free(mpa1);
7177 isl_multi_pw_aff_free(mpa2);
7178 return NULL;
7181 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7182 * In other words, plug in "mpa2" in "mpa1".
7184 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7185 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7187 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7188 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7191 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7192 * of "mpa1" and "mpa2" live in the same space, construct map space
7193 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7194 * with this map space as extract argument.
7196 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7197 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7198 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7199 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7201 int match;
7202 isl_space *space1, *space2;
7203 isl_map *res;
7205 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7206 isl_multi_pw_aff_get_space(mpa2));
7207 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7208 isl_multi_pw_aff_get_space(mpa1));
7209 if (!mpa1 || !mpa2)
7210 goto error;
7211 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7212 mpa2->space, isl_dim_out);
7213 if (match < 0)
7214 goto error;
7215 if (!match)
7216 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7217 "range spaces don't match", goto error);
7218 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7219 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7220 space1 = isl_space_map_from_domain_and_range(space1, space2);
7222 res = order(mpa1, mpa2, space1);
7223 isl_multi_pw_aff_free(mpa1);
7224 isl_multi_pw_aff_free(mpa2);
7225 return res;
7226 error:
7227 isl_multi_pw_aff_free(mpa1);
7228 isl_multi_pw_aff_free(mpa2);
7229 return NULL;
7232 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7233 * where the function values are equal. "space" is the space of the result.
7234 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7236 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7237 * in the sequences are equal.
7239 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7240 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7241 __isl_take isl_space *space)
7243 int i, n;
7244 isl_map *res;
7246 res = isl_map_universe(space);
7248 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7249 for (i = 0; i < n; ++i) {
7250 isl_pw_aff *pa1, *pa2;
7251 isl_map *map;
7253 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7254 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7255 map = isl_pw_aff_eq_map(pa1, pa2);
7256 res = isl_map_intersect(res, map);
7259 return res;
7262 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7263 * where the function values are equal.
7265 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7266 __isl_take isl_multi_pw_aff *mpa2)
7268 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7269 &isl_multi_pw_aff_eq_map_on_space);
7272 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7273 * where the function values of "mpa1" is lexicographically satisfies "base"
7274 * compared to that of "mpa2". "space" is the space of the result.
7275 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7277 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7278 * if its i-th element satisfies "base" when compared to
7279 * the i-th element of "mpa2" while all previous elements are
7280 * pairwise equal.
7282 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7283 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7284 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7285 __isl_take isl_pw_aff *pa2),
7286 __isl_take isl_space *space)
7288 int i, n;
7289 isl_map *res, *rest;
7291 res = isl_map_empty(isl_space_copy(space));
7292 rest = isl_map_universe(space);
7294 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7295 for (i = 0; i < n; ++i) {
7296 isl_pw_aff *pa1, *pa2;
7297 isl_map *map;
7299 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7300 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7301 map = base(pa1, pa2);
7302 map = isl_map_intersect(map, isl_map_copy(rest));
7303 res = isl_map_union(res, map);
7305 if (i == n - 1)
7306 continue;
7308 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7309 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7310 map = isl_pw_aff_eq_map(pa1, pa2);
7311 rest = isl_map_intersect(rest, map);
7314 isl_map_free(rest);
7315 return res;
7318 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7319 * where the function value of "mpa1" is lexicographically less than that
7320 * of "mpa2". "space" is the space of the result.
7321 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7323 * "mpa1" is less than "mpa2" if its i-th element is smaller
7324 * than the i-th element of "mpa2" while all previous elements are
7325 * pairwise equal.
7327 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7328 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7329 __isl_take isl_space *space)
7331 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7332 &isl_pw_aff_lt_map, space);
7335 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7336 * where the function value of "mpa1" is lexicographically less than that
7337 * of "mpa2".
7339 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7340 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7342 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7343 &isl_multi_pw_aff_lex_lt_map_on_space);
7346 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7347 * where the function value of "mpa1" is lexicographically greater than that
7348 * of "mpa2". "space" is the space of the result.
7349 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7351 * "mpa1" is greater than "mpa2" if its i-th element is greater
7352 * than the i-th element of "mpa2" while all previous elements are
7353 * pairwise equal.
7355 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7356 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7357 __isl_take isl_space *space)
7359 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7360 &isl_pw_aff_gt_map, space);
7363 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7364 * where the function value of "mpa1" is lexicographically greater than that
7365 * of "mpa2".
7367 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7368 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7370 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7371 &isl_multi_pw_aff_lex_gt_map_on_space);
7374 /* Compare two isl_affs.
7376 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7377 * than "aff2" and 0 if they are equal.
7379 * The order is fairly arbitrary. We do consider expressions that only involve
7380 * earlier dimensions as "smaller".
7382 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7384 int cmp;
7385 int last1, last2;
7387 if (aff1 == aff2)
7388 return 0;
7390 if (!aff1)
7391 return -1;
7392 if (!aff2)
7393 return 1;
7395 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7396 if (cmp != 0)
7397 return cmp;
7399 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7400 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7401 if (last1 != last2)
7402 return last1 - last2;
7404 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7407 /* Compare two isl_pw_affs.
7409 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7410 * than "pa2" and 0 if they are equal.
7412 * The order is fairly arbitrary. We do consider expressions that only involve
7413 * earlier dimensions as "smaller".
7415 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7416 __isl_keep isl_pw_aff *pa2)
7418 int i;
7419 int cmp;
7421 if (pa1 == pa2)
7422 return 0;
7424 if (!pa1)
7425 return -1;
7426 if (!pa2)
7427 return 1;
7429 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7430 if (cmp != 0)
7431 return cmp;
7433 if (pa1->n != pa2->n)
7434 return pa1->n - pa2->n;
7436 for (i = 0; i < pa1->n; ++i) {
7437 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7438 if (cmp != 0)
7439 return cmp;
7440 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7441 if (cmp != 0)
7442 return cmp;
7445 return 0;
7448 /* Return a piecewise affine expression that is equal to "v" on "domain".
7450 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7451 __isl_take isl_val *v)
7453 isl_space *space;
7454 isl_local_space *ls;
7455 isl_aff *aff;
7457 space = isl_set_get_space(domain);
7458 ls = isl_local_space_from_space(space);
7459 aff = isl_aff_val_on_domain(ls, v);
7461 return isl_pw_aff_alloc(domain, aff);
7464 /* Return a multi affine expression that is equal to "mv" on domain
7465 * space "space".
7467 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7468 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7470 int i, n;
7471 isl_space *space2;
7472 isl_local_space *ls;
7473 isl_multi_aff *ma;
7475 if (!space || !mv)
7476 goto error;
7478 n = isl_multi_val_dim(mv, isl_dim_set);
7479 space2 = isl_multi_val_get_space(mv);
7480 space2 = isl_space_align_params(space2, isl_space_copy(space));
7481 space = isl_space_align_params(space, isl_space_copy(space2));
7482 space = isl_space_map_from_domain_and_range(space, space2);
7483 ma = isl_multi_aff_alloc(isl_space_copy(space));
7484 ls = isl_local_space_from_space(isl_space_domain(space));
7485 for (i = 0; i < n; ++i) {
7486 isl_val *v;
7487 isl_aff *aff;
7489 v = isl_multi_val_get_val(mv, i);
7490 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7491 ma = isl_multi_aff_set_aff(ma, i, aff);
7493 isl_local_space_free(ls);
7495 isl_multi_val_free(mv);
7496 return ma;
7497 error:
7498 isl_space_free(space);
7499 isl_multi_val_free(mv);
7500 return NULL;
7503 /* Return a piecewise multi-affine expression
7504 * that is equal to "mv" on "domain".
7506 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7507 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7509 isl_space *space;
7510 isl_multi_aff *ma;
7512 space = isl_set_get_space(domain);
7513 ma = isl_multi_aff_multi_val_on_space(space, mv);
7515 return isl_pw_multi_aff_alloc(domain, ma);
7518 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7519 * mv is the value that should be attained on each domain set
7520 * res collects the results
7522 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7523 isl_multi_val *mv;
7524 isl_union_pw_multi_aff *res;
7527 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7528 * and add it to data->res.
7530 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7531 void *user)
7533 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7534 isl_pw_multi_aff *pma;
7535 isl_multi_val *mv;
7537 mv = isl_multi_val_copy(data->mv);
7538 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7539 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7541 return data->res ? isl_stat_ok : isl_stat_error;
7544 /* Return a union piecewise multi-affine expression
7545 * that is equal to "mv" on "domain".
7547 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7548 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7550 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7551 isl_space *space;
7553 space = isl_union_set_get_space(domain);
7554 data.res = isl_union_pw_multi_aff_empty(space);
7555 data.mv = mv;
7556 if (isl_union_set_foreach_set(domain,
7557 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7558 data.res = isl_union_pw_multi_aff_free(data.res);
7559 isl_union_set_free(domain);
7560 isl_multi_val_free(mv);
7561 return data.res;
7564 /* Compute the pullback of data->pma by the function represented by "pma2",
7565 * provided the spaces match, and add the results to data->res.
7567 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7569 struct isl_union_pw_multi_aff_bin_data *data = user;
7571 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7572 pma2->dim, isl_dim_out)) {
7573 isl_pw_multi_aff_free(pma2);
7574 return isl_stat_ok;
7577 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7578 isl_pw_multi_aff_copy(data->pma), pma2);
7580 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7581 if (!data->res)
7582 return isl_stat_error;
7584 return isl_stat_ok;
7587 /* Compute the pullback of "upma1" by the function represented by "upma2".
7589 __isl_give isl_union_pw_multi_aff *
7590 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7591 __isl_take isl_union_pw_multi_aff *upma1,
7592 __isl_take isl_union_pw_multi_aff *upma2)
7594 return bin_op(upma1, upma2, &pullback_entry);
7597 /* Check that the domain space of "upa" matches "space".
7599 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7600 * can in principle never fail since the space "space" is that
7601 * of the isl_multi_union_pw_aff and is a set space such that
7602 * there is no domain space to match.
7604 * We check the parameters and double-check that "space" is
7605 * indeed that of a set.
7607 static isl_stat isl_union_pw_aff_check_match_domain_space(
7608 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7610 isl_space *upa_space;
7611 isl_bool match;
7613 if (!upa || !space)
7614 return isl_stat_error;
7616 match = isl_space_is_set(space);
7617 if (match < 0)
7618 return isl_stat_error;
7619 if (!match)
7620 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7621 "expecting set space", return -1);
7623 upa_space = isl_union_pw_aff_get_space(upa);
7624 match = isl_space_has_equal_params(space, upa_space);
7625 if (match < 0)
7626 goto error;
7627 if (!match)
7628 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7629 "parameters don't match", goto error);
7631 isl_space_free(upa_space);
7632 return isl_stat_ok;
7633 error:
7634 isl_space_free(upa_space);
7635 return isl_stat_error;
7638 /* Do the parameters of "upa" match those of "space"?
7640 static isl_bool isl_union_pw_aff_matching_params(
7641 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7643 isl_space *upa_space;
7644 isl_bool match;
7646 if (!upa || !space)
7647 return isl_bool_error;
7649 upa_space = isl_union_pw_aff_get_space(upa);
7651 match = isl_space_has_equal_params(space, upa_space);
7653 isl_space_free(upa_space);
7654 return match;
7657 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7658 * space represents the new parameters.
7659 * res collects the results.
7661 struct isl_union_pw_aff_reset_params_data {
7662 isl_space *space;
7663 isl_union_pw_aff *res;
7666 /* Replace the parameters of "pa" by data->space and
7667 * add the result to data->res.
7669 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7671 struct isl_union_pw_aff_reset_params_data *data = user;
7672 isl_space *space;
7674 space = isl_pw_aff_get_space(pa);
7675 space = isl_space_replace_params(space, data->space);
7676 pa = isl_pw_aff_reset_space(pa, space);
7677 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7679 return data->res ? isl_stat_ok : isl_stat_error;
7682 /* Replace the domain space of "upa" by "space".
7683 * Since a union expression does not have a (single) domain space,
7684 * "space" is necessarily a parameter space.
7686 * Since the order and the names of the parameters determine
7687 * the hash value, we need to create a new hash table.
7689 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7690 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7692 struct isl_union_pw_aff_reset_params_data data = { space };
7693 isl_bool match;
7695 match = isl_union_pw_aff_matching_params(upa, space);
7696 if (match < 0)
7697 upa = isl_union_pw_aff_free(upa);
7698 else if (match) {
7699 isl_space_free(space);
7700 return upa;
7703 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7704 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7705 data.res = isl_union_pw_aff_free(data.res);
7707 isl_union_pw_aff_free(upa);
7708 isl_space_free(space);
7709 return data.res;
7712 /* Return the floor of "pa".
7714 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7716 return isl_pw_aff_floor(pa);
7719 /* Given f, return floor(f).
7721 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7722 __isl_take isl_union_pw_aff *upa)
7724 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7727 /* Compute
7729 * upa mod m = upa - m * floor(upa/m)
7731 * with m an integer value.
7733 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7734 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7736 isl_union_pw_aff *res;
7738 if (!upa || !m)
7739 goto error;
7741 if (!isl_val_is_int(m))
7742 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7743 "expecting integer modulo", goto error);
7744 if (!isl_val_is_pos(m))
7745 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7746 "expecting positive modulo", goto error);
7748 res = isl_union_pw_aff_copy(upa);
7749 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7750 upa = isl_union_pw_aff_floor(upa);
7751 upa = isl_union_pw_aff_scale_val(upa, m);
7752 res = isl_union_pw_aff_sub(res, upa);
7754 return res;
7755 error:
7756 isl_val_free(m);
7757 isl_union_pw_aff_free(upa);
7758 return NULL;
7761 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7762 * pos is the output position that needs to be extracted.
7763 * res collects the results.
7765 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7766 int pos;
7767 isl_union_pw_aff *res;
7770 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7771 * (assuming it has such a dimension) and add it to data->res.
7773 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7775 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7776 int n_out;
7777 isl_pw_aff *pa;
7779 if (!pma)
7780 return isl_stat_error;
7782 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7783 if (data->pos >= n_out) {
7784 isl_pw_multi_aff_free(pma);
7785 return isl_stat_ok;
7788 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7789 isl_pw_multi_aff_free(pma);
7791 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7793 return data->res ? isl_stat_ok : isl_stat_error;
7796 /* Extract an isl_union_pw_aff corresponding to
7797 * output dimension "pos" of "upma".
7799 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7800 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7802 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7803 isl_space *space;
7805 if (!upma)
7806 return NULL;
7808 if (pos < 0)
7809 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7810 "cannot extract at negative position", return NULL);
7812 space = isl_union_pw_multi_aff_get_space(upma);
7813 data.res = isl_union_pw_aff_empty(space);
7814 data.pos = pos;
7815 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7816 &get_union_pw_aff, &data) < 0)
7817 data.res = isl_union_pw_aff_free(data.res);
7819 return data.res;
7822 /* Return a union piecewise affine expression
7823 * that is equal to "aff" on "domain".
7825 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7826 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7828 isl_pw_aff *pa;
7830 pa = isl_pw_aff_from_aff(aff);
7831 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7834 /* Return a union piecewise affine expression
7835 * that is equal to the parameter identified by "id" on "domain".
7837 * Make sure the parameter appears in the space passed to
7838 * isl_aff_param_on_domain_space_id.
7840 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7841 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7843 isl_space *space;
7844 isl_aff *aff;
7846 space = isl_union_set_get_space(domain);
7847 space = isl_space_add_param_id(space, isl_id_copy(id));
7848 aff = isl_aff_param_on_domain_space_id(space, id);
7849 return isl_union_pw_aff_aff_on_domain(domain, aff);
7852 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7853 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7854 * needs to attain.
7855 * "res" collects the results.
7857 struct isl_union_pw_aff_pw_aff_on_domain_data {
7858 isl_pw_aff *pa;
7859 isl_union_pw_aff *res;
7862 /* Construct a piecewise affine expression that is equal to data->pa
7863 * on "domain" and add the result to data->res.
7865 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7867 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7868 isl_pw_aff *pa;
7869 int dim;
7871 pa = isl_pw_aff_copy(data->pa);
7872 dim = isl_set_dim(domain, isl_dim_set);
7873 pa = isl_pw_aff_from_range(pa);
7874 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7875 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7876 pa = isl_pw_aff_intersect_domain(pa, domain);
7877 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7879 return data->res ? isl_stat_ok : isl_stat_error;
7882 /* Return a union piecewise affine expression
7883 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7884 * have been aligned.
7886 * Construct an isl_pw_aff on each of the sets in "domain" and
7887 * collect the results.
7889 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7890 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7892 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7893 isl_space *space;
7895 space = isl_union_set_get_space(domain);
7896 data.res = isl_union_pw_aff_empty(space);
7897 data.pa = pa;
7898 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7899 data.res = isl_union_pw_aff_free(data.res);
7900 isl_union_set_free(domain);
7901 isl_pw_aff_free(pa);
7902 return data.res;
7905 /* Return a union piecewise affine expression
7906 * that is equal to "pa" on "domain".
7908 * Check that "pa" is a parametric expression,
7909 * align the parameters if needed and call
7910 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7912 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7913 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7915 isl_bool is_set;
7916 isl_bool equal_params;
7917 isl_space *domain_space, *pa_space;
7919 pa_space = isl_pw_aff_peek_space(pa);
7920 is_set = isl_space_is_set(pa_space);
7921 if (is_set < 0)
7922 goto error;
7923 if (!is_set)
7924 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7925 "expecting parametric expression", goto error);
7927 domain_space = isl_union_set_get_space(domain);
7928 pa_space = isl_pw_aff_get_space(pa);
7929 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7930 if (equal_params >= 0 && !equal_params) {
7931 isl_space *space;
7933 space = isl_space_align_params(domain_space, pa_space);
7934 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7935 domain = isl_union_set_align_params(domain, space);
7936 } else {
7937 isl_space_free(domain_space);
7938 isl_space_free(pa_space);
7941 if (equal_params < 0)
7942 goto error;
7943 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7944 error:
7945 isl_union_set_free(domain);
7946 isl_pw_aff_free(pa);
7947 return NULL;
7950 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7951 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7952 * "res" collects the results.
7954 struct isl_union_pw_aff_val_on_domain_data {
7955 isl_val *v;
7956 isl_union_pw_aff *res;
7959 /* Construct a piecewise affine expression that is equal to data->v
7960 * on "domain" and add the result to data->res.
7962 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7964 struct isl_union_pw_aff_val_on_domain_data *data = user;
7965 isl_pw_aff *pa;
7966 isl_val *v;
7968 v = isl_val_copy(data->v);
7969 pa = isl_pw_aff_val_on_domain(domain, v);
7970 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7972 return data->res ? isl_stat_ok : isl_stat_error;
7975 /* Return a union piecewise affine expression
7976 * that is equal to "v" on "domain".
7978 * Construct an isl_pw_aff on each of the sets in "domain" and
7979 * collect the results.
7981 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7982 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7984 struct isl_union_pw_aff_val_on_domain_data data;
7985 isl_space *space;
7987 space = isl_union_set_get_space(domain);
7988 data.res = isl_union_pw_aff_empty(space);
7989 data.v = v;
7990 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7991 data.res = isl_union_pw_aff_free(data.res);
7992 isl_union_set_free(domain);
7993 isl_val_free(v);
7994 return data.res;
7997 /* Construct a piecewise multi affine expression
7998 * that is equal to "pa" and add it to upma.
8000 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8001 void *user)
8003 isl_union_pw_multi_aff **upma = user;
8004 isl_pw_multi_aff *pma;
8006 pma = isl_pw_multi_aff_from_pw_aff(pa);
8007 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8009 return *upma ? isl_stat_ok : isl_stat_error;
8012 /* Construct and return a union piecewise multi affine expression
8013 * that is equal to the given union piecewise affine expression.
8015 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8016 __isl_take isl_union_pw_aff *upa)
8018 isl_space *space;
8019 isl_union_pw_multi_aff *upma;
8021 if (!upa)
8022 return NULL;
8024 space = isl_union_pw_aff_get_space(upa);
8025 upma = isl_union_pw_multi_aff_empty(space);
8027 if (isl_union_pw_aff_foreach_pw_aff(upa,
8028 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8029 upma = isl_union_pw_multi_aff_free(upma);
8031 isl_union_pw_aff_free(upa);
8032 return upma;
8035 /* Compute the set of elements in the domain of "pa" where it is zero and
8036 * add this set to "uset".
8038 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8040 isl_union_set **uset = (isl_union_set **)user;
8042 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8044 return *uset ? isl_stat_ok : isl_stat_error;
8047 /* Return a union set containing those elements in the domain
8048 * of "upa" where it is zero.
8050 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8051 __isl_take isl_union_pw_aff *upa)
8053 isl_union_set *zero;
8055 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8056 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8057 zero = isl_union_set_free(zero);
8059 isl_union_pw_aff_free(upa);
8060 return zero;
8063 /* Convert "pa" to an isl_map and add it to *umap.
8065 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8067 isl_union_map **umap = user;
8068 isl_map *map;
8070 map = isl_map_from_pw_aff(pa);
8071 *umap = isl_union_map_add_map(*umap, map);
8073 return *umap ? isl_stat_ok : isl_stat_error;
8076 /* Construct a union map mapping the domain of the union
8077 * piecewise affine expression to its range, with the single output dimension
8078 * equated to the corresponding affine expressions on their cells.
8080 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8081 __isl_take isl_union_pw_aff *upa)
8083 isl_space *space;
8084 isl_union_map *umap;
8086 if (!upa)
8087 return NULL;
8089 space = isl_union_pw_aff_get_space(upa);
8090 umap = isl_union_map_empty(space);
8092 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8093 &umap) < 0)
8094 umap = isl_union_map_free(umap);
8096 isl_union_pw_aff_free(upa);
8097 return umap;
8100 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8101 * upma is the function that is plugged in.
8102 * pa is the current part of the function in which upma is plugged in.
8103 * res collects the results.
8105 struct isl_union_pw_aff_pullback_upma_data {
8106 isl_union_pw_multi_aff *upma;
8107 isl_pw_aff *pa;
8108 isl_union_pw_aff *res;
8111 /* Check if "pma" can be plugged into data->pa.
8112 * If so, perform the pullback and add the result to data->res.
8114 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8116 struct isl_union_pw_aff_pullback_upma_data *data = user;
8117 isl_pw_aff *pa;
8119 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8120 pma->dim, isl_dim_out)) {
8121 isl_pw_multi_aff_free(pma);
8122 return isl_stat_ok;
8125 pa = isl_pw_aff_copy(data->pa);
8126 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8128 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8130 return data->res ? isl_stat_ok : isl_stat_error;
8133 /* Check if any of the elements of data->upma can be plugged into pa,
8134 * add if so add the result to data->res.
8136 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8138 struct isl_union_pw_aff_pullback_upma_data *data = user;
8139 isl_stat r;
8141 data->pa = pa;
8142 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8143 &pa_pb_pma, data);
8144 isl_pw_aff_free(pa);
8146 return r;
8149 /* Compute the pullback of "upa" by the function represented by "upma".
8150 * In other words, plug in "upma" in "upa". The result contains
8151 * expressions defined over the domain space of "upma".
8153 * Run over all pairs of elements in "upa" and "upma", perform
8154 * the pullback when appropriate and collect the results.
8155 * If the hash value were based on the domain space rather than
8156 * the function space, then we could run through all elements
8157 * of "upma" and directly pick out the corresponding element of "upa".
8159 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8160 __isl_take isl_union_pw_aff *upa,
8161 __isl_take isl_union_pw_multi_aff *upma)
8163 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8164 isl_space *space;
8166 space = isl_union_pw_multi_aff_get_space(upma);
8167 upa = isl_union_pw_aff_align_params(upa, space);
8168 space = isl_union_pw_aff_get_space(upa);
8169 upma = isl_union_pw_multi_aff_align_params(upma, space);
8171 if (!upa || !upma)
8172 goto error;
8174 data.upma = upma;
8175 data.res = isl_union_pw_aff_alloc_same_size(upa);
8176 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8177 data.res = isl_union_pw_aff_free(data.res);
8179 isl_union_pw_aff_free(upa);
8180 isl_union_pw_multi_aff_free(upma);
8181 return data.res;
8182 error:
8183 isl_union_pw_aff_free(upa);
8184 isl_union_pw_multi_aff_free(upma);
8185 return NULL;
8188 #undef BASE
8189 #define BASE union_pw_aff
8190 #undef DOMBASE
8191 #define DOMBASE union_set
8193 #define NO_MOVE_DIMS
8194 #define NO_DOMAIN
8195 #define NO_PRODUCT
8196 #define NO_SPLICE
8197 #define NO_ZERO
8198 #define NO_IDENTITY
8200 #include <isl_multi_explicit_domain.c>
8201 #include <isl_multi_union_pw_aff_explicit_domain.c>
8202 #include <isl_multi_templ.c>
8203 #include <isl_multi_apply_set.c>
8204 #include <isl_multi_apply_union_set.c>
8205 #include <isl_multi_coalesce.c>
8206 #include <isl_multi_floor.c>
8207 #include <isl_multi_gist.c>
8208 #include <isl_multi_align_set.c>
8209 #include <isl_multi_align_union_set.c>
8210 #include <isl_multi_intersect.c>
8212 /* Does "mupa" have a non-trivial explicit domain?
8214 * The explicit domain, if present, is trivial if it represents
8215 * an (obviously) universe parameter set.
8217 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8218 __isl_keep isl_multi_union_pw_aff *mupa)
8220 isl_bool is_params, trivial;
8221 isl_set *set;
8223 if (!mupa)
8224 return isl_bool_error;
8225 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8226 return isl_bool_false;
8227 is_params = isl_union_set_is_params(mupa->u.dom);
8228 if (is_params < 0 || !is_params)
8229 return isl_bool_not(is_params);
8230 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8231 trivial = isl_set_plain_is_universe(set);
8232 isl_set_free(set);
8233 return isl_bool_not(trivial);
8236 /* Construct a multiple union piecewise affine expression
8237 * in the given space with value zero in each of the output dimensions.
8239 * Since there is no canonical zero value for
8240 * a union piecewise affine expression, we can only construct
8241 * a zero-dimensional "zero" value.
8243 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8244 __isl_take isl_space *space)
8246 isl_bool params;
8248 if (!space)
8249 return NULL;
8251 params = isl_space_is_params(space);
8252 if (params < 0)
8253 goto error;
8254 if (params)
8255 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8256 "expecting proper set space", goto error);
8257 if (!isl_space_is_set(space))
8258 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8259 "expecting set space", goto error);
8260 if (isl_space_dim(space , isl_dim_out) != 0)
8261 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8262 "expecting 0D space", goto error);
8264 return isl_multi_union_pw_aff_alloc(space);
8265 error:
8266 isl_space_free(space);
8267 return NULL;
8270 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8271 * with the actual sum on the shared domain and
8272 * the defined expression on the symmetric difference of the domains.
8274 * We simply iterate over the elements in both arguments and
8275 * call isl_union_pw_aff_union_add on each of them, if there is
8276 * at least one element.
8278 * Otherwise, the two expressions have an explicit domain and
8279 * the union of these explicit domains is computed.
8280 * This assumes that the explicit domains are either both in terms
8281 * of specific domains elements or both in terms of parameters.
8282 * However, if one of the expressions does not have any constraints
8283 * on its explicit domain, then this is allowed as well and the result
8284 * is the expression with no constraints on its explicit domain.
8286 static __isl_give isl_multi_union_pw_aff *
8287 isl_multi_union_pw_aff_union_add_aligned(
8288 __isl_take isl_multi_union_pw_aff *mupa1,
8289 __isl_take isl_multi_union_pw_aff *mupa2)
8291 isl_bool has_domain, is_params1, is_params2;
8293 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8294 goto error;
8295 if (mupa1->n > 0)
8296 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8297 &isl_union_pw_aff_union_add);
8298 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8299 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8300 goto error;
8302 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8303 if (has_domain < 0)
8304 goto error;
8305 if (!has_domain) {
8306 isl_multi_union_pw_aff_free(mupa2);
8307 return mupa1;
8309 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8310 if (has_domain < 0)
8311 goto error;
8312 if (!has_domain) {
8313 isl_multi_union_pw_aff_free(mupa1);
8314 return mupa2;
8317 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8318 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8319 if (is_params1 < 0 || is_params2 < 0)
8320 goto error;
8321 if (is_params1 != is_params2)
8322 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8323 isl_error_invalid,
8324 "cannot compute union of concrete domain and "
8325 "parameter constraints", goto error);
8326 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8327 if (!mupa1)
8328 goto error;
8329 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8330 isl_union_set_copy(mupa2->u.dom));
8331 if (!mupa1->u.dom)
8332 goto error;
8333 isl_multi_union_pw_aff_free(mupa2);
8334 return mupa1;
8335 error:
8336 isl_multi_union_pw_aff_free(mupa1);
8337 isl_multi_union_pw_aff_free(mupa2);
8338 return NULL;
8341 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8342 * with the actual sum on the shared domain and
8343 * the defined expression on the symmetric difference of the domains.
8345 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8346 __isl_take isl_multi_union_pw_aff *mupa1,
8347 __isl_take isl_multi_union_pw_aff *mupa2)
8349 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8350 &isl_multi_union_pw_aff_union_add_aligned);
8353 /* Construct and return a multi union piecewise affine expression
8354 * that is equal to the given multi affine expression.
8356 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8357 __isl_take isl_multi_aff *ma)
8359 isl_multi_pw_aff *mpa;
8361 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8362 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8365 /* Construct and return a multi union piecewise affine expression
8366 * that is equal to the given multi piecewise affine expression.
8368 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8369 __isl_take isl_multi_pw_aff *mpa)
8371 int i, n;
8372 isl_space *space;
8373 isl_multi_union_pw_aff *mupa;
8375 if (!mpa)
8376 return NULL;
8378 space = isl_multi_pw_aff_get_space(mpa);
8379 space = isl_space_range(space);
8380 mupa = isl_multi_union_pw_aff_alloc(space);
8382 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8383 for (i = 0; i < n; ++i) {
8384 isl_pw_aff *pa;
8385 isl_union_pw_aff *upa;
8387 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8388 upa = isl_union_pw_aff_from_pw_aff(pa);
8389 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8392 isl_multi_pw_aff_free(mpa);
8394 return mupa;
8397 /* Extract the range space of "pma" and assign it to *space.
8398 * If *space has already been set (through a previous call to this function),
8399 * then check that the range space is the same.
8401 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8403 isl_space **space = user;
8404 isl_space *pma_space;
8405 isl_bool equal;
8407 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8408 isl_pw_multi_aff_free(pma);
8410 if (!pma_space)
8411 return isl_stat_error;
8412 if (!*space) {
8413 *space = pma_space;
8414 return isl_stat_ok;
8417 equal = isl_space_is_equal(pma_space, *space);
8418 isl_space_free(pma_space);
8420 if (equal < 0)
8421 return isl_stat_error;
8422 if (!equal)
8423 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8424 "range spaces not the same", return isl_stat_error);
8425 return isl_stat_ok;
8428 /* Construct and return a multi union piecewise affine expression
8429 * that is equal to the given union piecewise multi affine expression.
8431 * In order to be able to perform the conversion, the input
8432 * needs to be non-empty and may only involve a single range space.
8434 * If the resulting multi union piecewise affine expression has
8435 * an explicit domain, then assign it the domain of the input.
8436 * In other cases, the domain is stored in the individual elements.
8438 __isl_give isl_multi_union_pw_aff *
8439 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8440 __isl_take isl_union_pw_multi_aff *upma)
8442 isl_space *space = NULL;
8443 isl_multi_union_pw_aff *mupa;
8444 int i, n;
8446 if (!upma)
8447 return NULL;
8448 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8449 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8450 "cannot extract range space from empty input",
8451 goto error);
8452 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8453 &space) < 0)
8454 goto error;
8456 if (!space)
8457 goto error;
8459 n = isl_space_dim(space, isl_dim_set);
8460 mupa = isl_multi_union_pw_aff_alloc(space);
8462 for (i = 0; i < n; ++i) {
8463 isl_union_pw_aff *upa;
8465 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8466 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8468 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8469 isl_union_set *dom;
8470 isl_union_pw_multi_aff *copy;
8472 copy = isl_union_pw_multi_aff_copy(upma);
8473 dom = isl_union_pw_multi_aff_domain(copy);
8474 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8477 isl_union_pw_multi_aff_free(upma);
8478 return mupa;
8479 error:
8480 isl_space_free(space);
8481 isl_union_pw_multi_aff_free(upma);
8482 return NULL;
8485 /* Try and create an isl_multi_union_pw_aff that is equivalent
8486 * to the given isl_union_map.
8487 * The isl_union_map is required to be single-valued in each space.
8488 * Moreover, it cannot be empty and all range spaces need to be the same.
8489 * Otherwise, an error is produced.
8491 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8492 __isl_take isl_union_map *umap)
8494 isl_union_pw_multi_aff *upma;
8496 upma = isl_union_pw_multi_aff_from_union_map(umap);
8497 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8500 /* Return a multiple union piecewise affine expression
8501 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8502 * have been aligned.
8504 * If the resulting multi union piecewise affine expression has
8505 * an explicit domain, then assign it the input domain.
8506 * In other cases, the domain is stored in the individual elements.
8508 static __isl_give isl_multi_union_pw_aff *
8509 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8510 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8512 int i, n;
8513 isl_space *space;
8514 isl_multi_union_pw_aff *mupa;
8516 if (!domain || !mv)
8517 goto error;
8519 n = isl_multi_val_dim(mv, isl_dim_set);
8520 space = isl_multi_val_get_space(mv);
8521 mupa = isl_multi_union_pw_aff_alloc(space);
8522 for (i = 0; i < n; ++i) {
8523 isl_val *v;
8524 isl_union_pw_aff *upa;
8526 v = isl_multi_val_get_val(mv, i);
8527 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8529 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8531 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8532 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8533 isl_union_set_copy(domain));
8535 isl_union_set_free(domain);
8536 isl_multi_val_free(mv);
8537 return mupa;
8538 error:
8539 isl_union_set_free(domain);
8540 isl_multi_val_free(mv);
8541 return NULL;
8544 /* Return a multiple union piecewise affine expression
8545 * that is equal to "mv" on "domain".
8547 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8548 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8550 isl_bool equal_params;
8552 if (!domain || !mv)
8553 goto error;
8554 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8555 if (equal_params < 0)
8556 goto error;
8557 if (equal_params)
8558 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8559 domain, mv);
8560 domain = isl_union_set_align_params(domain,
8561 isl_multi_val_get_space(mv));
8562 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8563 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8564 error:
8565 isl_union_set_free(domain);
8566 isl_multi_val_free(mv);
8567 return NULL;
8570 /* Return a multiple union piecewise affine expression
8571 * that is equal to "ma" on "domain".
8573 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8574 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8576 isl_pw_multi_aff *pma;
8578 pma = isl_pw_multi_aff_from_multi_aff(ma);
8579 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8582 /* Return a multiple union piecewise affine expression
8583 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8584 * have been aligned.
8586 * If the resulting multi union piecewise affine expression has
8587 * an explicit domain, then assign it the input domain.
8588 * In other cases, the domain is stored in the individual elements.
8590 static __isl_give isl_multi_union_pw_aff *
8591 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8592 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8594 int i, n;
8595 isl_space *space;
8596 isl_multi_union_pw_aff *mupa;
8598 if (!domain || !pma)
8599 goto error;
8601 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8602 space = isl_pw_multi_aff_get_space(pma);
8603 mupa = isl_multi_union_pw_aff_alloc(space);
8604 for (i = 0; i < n; ++i) {
8605 isl_pw_aff *pa;
8606 isl_union_pw_aff *upa;
8608 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8609 upa = isl_union_pw_aff_pw_aff_on_domain(
8610 isl_union_set_copy(domain), pa);
8611 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8613 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8614 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8615 isl_union_set_copy(domain));
8617 isl_union_set_free(domain);
8618 isl_pw_multi_aff_free(pma);
8619 return mupa;
8620 error:
8621 isl_union_set_free(domain);
8622 isl_pw_multi_aff_free(pma);
8623 return NULL;
8626 /* Return a multiple union piecewise affine expression
8627 * that is equal to "pma" on "domain".
8629 __isl_give isl_multi_union_pw_aff *
8630 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8631 __isl_take isl_pw_multi_aff *pma)
8633 isl_bool equal_params;
8634 isl_space *space;
8636 space = isl_pw_multi_aff_peek_space(pma);
8637 equal_params = isl_union_set_space_has_equal_params(domain, space);
8638 if (equal_params < 0)
8639 goto error;
8640 if (equal_params)
8641 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8642 domain, pma);
8643 domain = isl_union_set_align_params(domain,
8644 isl_pw_multi_aff_get_space(pma));
8645 pma = isl_pw_multi_aff_align_params(pma,
8646 isl_union_set_get_space(domain));
8647 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8648 pma);
8649 error:
8650 isl_union_set_free(domain);
8651 isl_pw_multi_aff_free(pma);
8652 return NULL;
8655 /* Return a union set containing those elements in the domains
8656 * of the elements of "mupa" where they are all zero.
8658 * If there are no elements, then simply return the entire domain.
8660 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8661 __isl_take isl_multi_union_pw_aff *mupa)
8663 int i, n;
8664 isl_union_pw_aff *upa;
8665 isl_union_set *zero;
8667 if (!mupa)
8668 return NULL;
8670 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8671 if (n == 0)
8672 return isl_multi_union_pw_aff_domain(mupa);
8674 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8675 zero = isl_union_pw_aff_zero_union_set(upa);
8677 for (i = 1; i < n; ++i) {
8678 isl_union_set *zero_i;
8680 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8681 zero_i = isl_union_pw_aff_zero_union_set(upa);
8683 zero = isl_union_set_intersect(zero, zero_i);
8686 isl_multi_union_pw_aff_free(mupa);
8687 return zero;
8690 /* Construct a union map mapping the shared domain
8691 * of the union piecewise affine expressions to the range of "mupa"
8692 * in the special case of a 0D multi union piecewise affine expression.
8694 * Construct a map between the explicit domain of "mupa" and
8695 * the range space.
8696 * Note that this assumes that the domain consists of explicit elements.
8698 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8699 __isl_take isl_multi_union_pw_aff *mupa)
8701 isl_bool is_params;
8702 isl_space *space;
8703 isl_union_set *dom, *ran;
8705 space = isl_multi_union_pw_aff_get_space(mupa);
8706 dom = isl_multi_union_pw_aff_domain(mupa);
8707 ran = isl_union_set_from_set(isl_set_universe(space));
8709 is_params = isl_union_set_is_params(dom);
8710 if (is_params < 0)
8711 dom = isl_union_set_free(dom);
8712 else if (is_params)
8713 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8714 "cannot create union map from expression without "
8715 "explicit domain elements",
8716 dom = isl_union_set_free(dom));
8718 return isl_union_map_from_domain_and_range(dom, ran);
8721 /* Construct a union map mapping the shared domain
8722 * of the union piecewise affine expressions to the range of "mupa"
8723 * with each dimension in the range equated to the
8724 * corresponding union piecewise affine expression.
8726 * If the input is zero-dimensional, then construct a mapping
8727 * from its explicit domain.
8729 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8730 __isl_take isl_multi_union_pw_aff *mupa)
8732 int i, n;
8733 isl_space *space;
8734 isl_union_map *umap;
8735 isl_union_pw_aff *upa;
8737 if (!mupa)
8738 return NULL;
8740 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8741 if (n == 0)
8742 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8744 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8745 umap = isl_union_map_from_union_pw_aff(upa);
8747 for (i = 1; i < n; ++i) {
8748 isl_union_map *umap_i;
8750 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8751 umap_i = isl_union_map_from_union_pw_aff(upa);
8752 umap = isl_union_map_flat_range_product(umap, umap_i);
8755 space = isl_multi_union_pw_aff_get_space(mupa);
8756 umap = isl_union_map_reset_range_space(umap, space);
8758 isl_multi_union_pw_aff_free(mupa);
8759 return umap;
8762 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8763 * "range" is the space from which to set the range space.
8764 * "res" collects the results.
8766 struct isl_union_pw_multi_aff_reset_range_space_data {
8767 isl_space *range;
8768 isl_union_pw_multi_aff *res;
8771 /* Replace the range space of "pma" by the range space of data->range and
8772 * add the result to data->res.
8774 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8776 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8777 isl_space *space;
8779 space = isl_pw_multi_aff_get_space(pma);
8780 space = isl_space_domain(space);
8781 space = isl_space_extend_domain_with_range(space,
8782 isl_space_copy(data->range));
8783 pma = isl_pw_multi_aff_reset_space(pma, space);
8784 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8786 return data->res ? isl_stat_ok : isl_stat_error;
8789 /* Replace the range space of all the piecewise affine expressions in "upma" by
8790 * the range space of "space".
8792 * This assumes that all these expressions have the same output dimension.
8794 * Since the spaces of the expressions change, so do their hash values.
8795 * We therefore need to create a new isl_union_pw_multi_aff.
8796 * Note that the hash value is currently computed based on the entire
8797 * space even though there can only be a single expression with a given
8798 * domain space.
8800 static __isl_give isl_union_pw_multi_aff *
8801 isl_union_pw_multi_aff_reset_range_space(
8802 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8804 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8805 isl_space *space_upma;
8807 space_upma = isl_union_pw_multi_aff_get_space(upma);
8808 data.res = isl_union_pw_multi_aff_empty(space_upma);
8809 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8810 &reset_range_space, &data) < 0)
8811 data.res = isl_union_pw_multi_aff_free(data.res);
8813 isl_space_free(space);
8814 isl_union_pw_multi_aff_free(upma);
8815 return data.res;
8818 /* Construct and return a union piecewise multi affine expression
8819 * that is equal to the given multi union piecewise affine expression,
8820 * in the special case of a 0D multi union piecewise affine expression.
8822 * Construct a union piecewise multi affine expression
8823 * on top of the explicit domain of the input.
8825 __isl_give isl_union_pw_multi_aff *
8826 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8827 __isl_take isl_multi_union_pw_aff *mupa)
8829 isl_space *space;
8830 isl_multi_val *mv;
8831 isl_union_set *domain;
8833 space = isl_multi_union_pw_aff_get_space(mupa);
8834 mv = isl_multi_val_zero(space);
8835 domain = isl_multi_union_pw_aff_domain(mupa);
8836 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8839 /* Construct and return a union piecewise multi affine expression
8840 * that is equal to the given multi union piecewise affine expression.
8842 * If the input is zero-dimensional, then
8843 * construct a union piecewise multi affine expression
8844 * on top of the explicit domain of the input.
8846 __isl_give isl_union_pw_multi_aff *
8847 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8848 __isl_take isl_multi_union_pw_aff *mupa)
8850 int i, n;
8851 isl_space *space;
8852 isl_union_pw_multi_aff *upma;
8853 isl_union_pw_aff *upa;
8855 if (!mupa)
8856 return NULL;
8858 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8859 if (n == 0)
8860 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8862 space = isl_multi_union_pw_aff_get_space(mupa);
8863 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8864 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8866 for (i = 1; i < n; ++i) {
8867 isl_union_pw_multi_aff *upma_i;
8869 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8870 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8871 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8874 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8876 isl_multi_union_pw_aff_free(mupa);
8877 return upma;
8880 /* Intersect the range of "mupa" with "range",
8881 * in the special case where "mupa" is 0D.
8883 * Intersect the domain of "mupa" with the constraints on the parameters
8884 * of "range".
8886 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8887 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8889 range = isl_set_params(range);
8890 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8891 return mupa;
8894 /* Intersect the range of "mupa" with "range".
8895 * That is, keep only those domain elements that have a function value
8896 * in "range".
8898 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8899 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8901 isl_union_pw_multi_aff *upma;
8902 isl_union_set *domain;
8903 isl_space *space;
8904 int n;
8905 int match;
8907 if (!mupa || !range)
8908 goto error;
8910 space = isl_set_get_space(range);
8911 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8912 space, isl_dim_set);
8913 isl_space_free(space);
8914 if (match < 0)
8915 goto error;
8916 if (!match)
8917 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8918 "space don't match", goto error);
8919 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8920 if (n == 0)
8921 return mupa_intersect_range_0D(mupa, range);
8923 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8924 isl_multi_union_pw_aff_copy(mupa));
8925 domain = isl_union_set_from_set(range);
8926 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8927 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8929 return mupa;
8930 error:
8931 isl_multi_union_pw_aff_free(mupa);
8932 isl_set_free(range);
8933 return NULL;
8936 /* Return the shared domain of the elements of "mupa",
8937 * in the special case where "mupa" is zero-dimensional.
8939 * Return the explicit domain of "mupa".
8940 * Note that this domain may be a parameter set, either
8941 * because "mupa" is meant to live in a set space or
8942 * because no explicit domain has been set.
8944 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8945 __isl_take isl_multi_union_pw_aff *mupa)
8947 isl_union_set *dom;
8949 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8950 isl_multi_union_pw_aff_free(mupa);
8952 return dom;
8955 /* Return the shared domain of the elements of "mupa".
8957 * If "mupa" is zero-dimensional, then return its explicit domain.
8959 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8960 __isl_take isl_multi_union_pw_aff *mupa)
8962 int i, n;
8963 isl_union_pw_aff *upa;
8964 isl_union_set *dom;
8966 if (!mupa)
8967 return NULL;
8969 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8970 if (n == 0)
8971 return isl_multi_union_pw_aff_domain_0D(mupa);
8973 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8974 dom = isl_union_pw_aff_domain(upa);
8975 for (i = 1; i < n; ++i) {
8976 isl_union_set *dom_i;
8978 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8979 dom_i = isl_union_pw_aff_domain(upa);
8980 dom = isl_union_set_intersect(dom, dom_i);
8983 isl_multi_union_pw_aff_free(mupa);
8984 return dom;
8987 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8988 * In particular, the spaces have been aligned.
8989 * The result is defined over the shared domain of the elements of "mupa"
8991 * We first extract the parametric constant part of "aff" and
8992 * define that over the shared domain.
8993 * Then we iterate over all input dimensions of "aff" and add the corresponding
8994 * multiples of the elements of "mupa".
8995 * Finally, we consider the integer divisions, calling the function
8996 * recursively to obtain an isl_union_pw_aff corresponding to the
8997 * integer division argument.
8999 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9000 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9002 int i, n_in, n_div;
9003 isl_union_pw_aff *upa;
9004 isl_union_set *uset;
9005 isl_val *v;
9006 isl_aff *cst;
9008 n_in = isl_aff_dim(aff, isl_dim_in);
9009 n_div = isl_aff_dim(aff, isl_dim_div);
9011 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9012 cst = isl_aff_copy(aff);
9013 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9014 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9015 cst = isl_aff_project_domain_on_params(cst);
9016 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9018 for (i = 0; i < n_in; ++i) {
9019 isl_union_pw_aff *upa_i;
9021 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9022 continue;
9023 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9024 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9025 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9026 upa = isl_union_pw_aff_add(upa, upa_i);
9029 for (i = 0; i < n_div; ++i) {
9030 isl_aff *div;
9031 isl_union_pw_aff *upa_i;
9033 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9034 continue;
9035 div = isl_aff_get_div(aff, i);
9036 upa_i = multi_union_pw_aff_apply_aff(
9037 isl_multi_union_pw_aff_copy(mupa), div);
9038 upa_i = isl_union_pw_aff_floor(upa_i);
9039 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9040 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9041 upa = isl_union_pw_aff_add(upa, upa_i);
9044 isl_multi_union_pw_aff_free(mupa);
9045 isl_aff_free(aff);
9047 return upa;
9050 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9051 * with the domain of "aff".
9052 * Furthermore, the dimension of this space needs to be greater than zero.
9053 * The result is defined over the shared domain of the elements of "mupa"
9055 * We perform these checks and then hand over control to
9056 * multi_union_pw_aff_apply_aff.
9058 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9059 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9061 isl_space *space1, *space2;
9062 int equal;
9064 mupa = isl_multi_union_pw_aff_align_params(mupa,
9065 isl_aff_get_space(aff));
9066 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9067 if (!mupa || !aff)
9068 goto error;
9070 space1 = isl_multi_union_pw_aff_get_space(mupa);
9071 space2 = isl_aff_get_domain_space(aff);
9072 equal = isl_space_is_equal(space1, space2);
9073 isl_space_free(space1);
9074 isl_space_free(space2);
9075 if (equal < 0)
9076 goto error;
9077 if (!equal)
9078 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9079 "spaces don't match", goto error);
9080 if (isl_aff_dim(aff, isl_dim_in) == 0)
9081 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9082 "cannot determine domains", goto error);
9084 return multi_union_pw_aff_apply_aff(mupa, aff);
9085 error:
9086 isl_multi_union_pw_aff_free(mupa);
9087 isl_aff_free(aff);
9088 return NULL;
9091 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9092 * The space of "mupa" is known to be compatible with the domain of "ma".
9094 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9095 * on the domain of "mupa".
9097 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9098 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9100 isl_union_set *dom;
9102 dom = isl_multi_union_pw_aff_domain(mupa);
9103 ma = isl_multi_aff_project_domain_on_params(ma);
9105 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9108 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9109 * with the domain of "ma".
9110 * The result is defined over the shared domain of the elements of "mupa"
9112 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9113 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9115 isl_space *space1, *space2;
9116 isl_multi_union_pw_aff *res;
9117 int equal;
9118 int i, n_out;
9120 mupa = isl_multi_union_pw_aff_align_params(mupa,
9121 isl_multi_aff_get_space(ma));
9122 ma = isl_multi_aff_align_params(ma,
9123 isl_multi_union_pw_aff_get_space(mupa));
9124 if (!mupa || !ma)
9125 goto error;
9127 space1 = isl_multi_union_pw_aff_get_space(mupa);
9128 space2 = isl_multi_aff_get_domain_space(ma);
9129 equal = isl_space_is_equal(space1, space2);
9130 isl_space_free(space1);
9131 isl_space_free(space2);
9132 if (equal < 0)
9133 goto error;
9134 if (!equal)
9135 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9136 "spaces don't match", goto error);
9137 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9138 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9139 return mupa_apply_multi_aff_0D(mupa, ma);
9141 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9142 res = isl_multi_union_pw_aff_alloc(space1);
9144 for (i = 0; i < n_out; ++i) {
9145 isl_aff *aff;
9146 isl_union_pw_aff *upa;
9148 aff = isl_multi_aff_get_aff(ma, i);
9149 upa = multi_union_pw_aff_apply_aff(
9150 isl_multi_union_pw_aff_copy(mupa), aff);
9151 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9154 isl_multi_aff_free(ma);
9155 isl_multi_union_pw_aff_free(mupa);
9156 return res;
9157 error:
9158 isl_multi_union_pw_aff_free(mupa);
9159 isl_multi_aff_free(ma);
9160 return NULL;
9163 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9164 * The space of "mupa" is known to be compatible with the domain of "pa".
9166 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9167 * on the domain of "mupa".
9169 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9170 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9172 isl_union_set *dom;
9174 dom = isl_multi_union_pw_aff_domain(mupa);
9175 pa = isl_pw_aff_project_domain_on_params(pa);
9177 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9180 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9181 * with the domain of "pa".
9182 * Furthermore, the dimension of this space needs to be greater than zero.
9183 * The result is defined over the shared domain of the elements of "mupa"
9185 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9186 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9188 int i;
9189 int equal;
9190 isl_space *space, *space2;
9191 isl_union_pw_aff *upa;
9193 mupa = isl_multi_union_pw_aff_align_params(mupa,
9194 isl_pw_aff_get_space(pa));
9195 pa = isl_pw_aff_align_params(pa,
9196 isl_multi_union_pw_aff_get_space(mupa));
9197 if (!mupa || !pa)
9198 goto error;
9200 space = isl_multi_union_pw_aff_get_space(mupa);
9201 space2 = isl_pw_aff_get_domain_space(pa);
9202 equal = isl_space_is_equal(space, space2);
9203 isl_space_free(space);
9204 isl_space_free(space2);
9205 if (equal < 0)
9206 goto error;
9207 if (!equal)
9208 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9209 "spaces don't match", goto error);
9210 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9211 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9213 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9214 upa = isl_union_pw_aff_empty(space);
9216 for (i = 0; i < pa->n; ++i) {
9217 isl_aff *aff;
9218 isl_set *domain;
9219 isl_multi_union_pw_aff *mupa_i;
9220 isl_union_pw_aff *upa_i;
9222 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9223 domain = isl_set_copy(pa->p[i].set);
9224 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9225 aff = isl_aff_copy(pa->p[i].aff);
9226 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9227 upa = isl_union_pw_aff_union_add(upa, upa_i);
9230 isl_multi_union_pw_aff_free(mupa);
9231 isl_pw_aff_free(pa);
9232 return upa;
9233 error:
9234 isl_multi_union_pw_aff_free(mupa);
9235 isl_pw_aff_free(pa);
9236 return NULL;
9239 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9240 * The space of "mupa" is known to be compatible with the domain of "pma".
9242 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9243 * on the domain of "mupa".
9245 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9246 __isl_take isl_multi_union_pw_aff *mupa,
9247 __isl_take isl_pw_multi_aff *pma)
9249 isl_union_set *dom;
9251 dom = isl_multi_union_pw_aff_domain(mupa);
9252 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9254 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9257 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9258 * with the domain of "pma".
9259 * The result is defined over the shared domain of the elements of "mupa"
9261 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9262 __isl_take isl_multi_union_pw_aff *mupa,
9263 __isl_take isl_pw_multi_aff *pma)
9265 isl_space *space1, *space2;
9266 isl_multi_union_pw_aff *res;
9267 int equal;
9268 int i, n_out;
9270 mupa = isl_multi_union_pw_aff_align_params(mupa,
9271 isl_pw_multi_aff_get_space(pma));
9272 pma = isl_pw_multi_aff_align_params(pma,
9273 isl_multi_union_pw_aff_get_space(mupa));
9274 if (!mupa || !pma)
9275 goto error;
9277 space1 = isl_multi_union_pw_aff_get_space(mupa);
9278 space2 = isl_pw_multi_aff_get_domain_space(pma);
9279 equal = isl_space_is_equal(space1, space2);
9280 isl_space_free(space1);
9281 isl_space_free(space2);
9282 if (equal < 0)
9283 goto error;
9284 if (!equal)
9285 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9286 "spaces don't match", goto error);
9287 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9288 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9289 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9291 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9292 res = isl_multi_union_pw_aff_alloc(space1);
9294 for (i = 0; i < n_out; ++i) {
9295 isl_pw_aff *pa;
9296 isl_union_pw_aff *upa;
9298 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9299 upa = isl_multi_union_pw_aff_apply_pw_aff(
9300 isl_multi_union_pw_aff_copy(mupa), pa);
9301 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9304 isl_pw_multi_aff_free(pma);
9305 isl_multi_union_pw_aff_free(mupa);
9306 return res;
9307 error:
9308 isl_multi_union_pw_aff_free(mupa);
9309 isl_pw_multi_aff_free(pma);
9310 return NULL;
9313 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9314 * If the explicit domain only keeps track of constraints on the parameters,
9315 * then only update those constraints.
9317 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9318 __isl_take isl_multi_union_pw_aff *mupa,
9319 __isl_keep isl_union_pw_multi_aff *upma)
9321 isl_bool is_params;
9323 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9324 return isl_multi_union_pw_aff_free(mupa);
9326 mupa = isl_multi_union_pw_aff_cow(mupa);
9327 if (!mupa)
9328 return NULL;
9330 is_params = isl_union_set_is_params(mupa->u.dom);
9331 if (is_params < 0)
9332 return isl_multi_union_pw_aff_free(mupa);
9334 upma = isl_union_pw_multi_aff_copy(upma);
9335 if (is_params)
9336 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9337 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9338 else
9339 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9340 mupa->u.dom, upma);
9341 if (!mupa->u.dom)
9342 return isl_multi_union_pw_aff_free(mupa);
9343 return mupa;
9346 /* Compute the pullback of "mupa" by the function represented by "upma".
9347 * In other words, plug in "upma" in "mupa". The result contains
9348 * expressions defined over the domain space of "upma".
9350 * Run over all elements of "mupa" and plug in "upma" in each of them.
9352 * If "mupa" has an explicit domain, then it is this domain
9353 * that needs to undergo a pullback instead, i.e., a preimage.
9355 __isl_give isl_multi_union_pw_aff *
9356 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9357 __isl_take isl_multi_union_pw_aff *mupa,
9358 __isl_take isl_union_pw_multi_aff *upma)
9360 int i, n;
9362 mupa = isl_multi_union_pw_aff_align_params(mupa,
9363 isl_union_pw_multi_aff_get_space(upma));
9364 upma = isl_union_pw_multi_aff_align_params(upma,
9365 isl_multi_union_pw_aff_get_space(mupa));
9366 mupa = isl_multi_union_pw_aff_cow(mupa);
9367 if (!mupa || !upma)
9368 goto error;
9370 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9371 for (i = 0; i < n; ++i) {
9372 isl_union_pw_aff *upa;
9374 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9375 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9376 isl_union_pw_multi_aff_copy(upma));
9377 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9380 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9381 mupa = preimage_explicit_domain(mupa, upma);
9383 isl_union_pw_multi_aff_free(upma);
9384 return mupa;
9385 error:
9386 isl_multi_union_pw_aff_free(mupa);
9387 isl_union_pw_multi_aff_free(upma);
9388 return NULL;
9391 /* Extract the sequence of elements in "mupa" with domain space "space"
9392 * (ignoring parameters).
9394 * For the elements of "mupa" that are not defined on the specified space,
9395 * the corresponding element in the result is empty.
9397 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9398 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9400 int i, n;
9401 isl_space *space_mpa;
9402 isl_multi_pw_aff *mpa;
9404 if (!mupa || !space)
9405 goto error;
9407 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9408 space = isl_space_replace_params(space, space_mpa);
9409 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9410 space_mpa);
9411 mpa = isl_multi_pw_aff_alloc(space_mpa);
9413 space = isl_space_from_domain(space);
9414 space = isl_space_add_dims(space, isl_dim_out, 1);
9415 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9416 for (i = 0; i < n; ++i) {
9417 isl_union_pw_aff *upa;
9418 isl_pw_aff *pa;
9420 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9421 pa = isl_union_pw_aff_extract_pw_aff(upa,
9422 isl_space_copy(space));
9423 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9424 isl_union_pw_aff_free(upa);
9427 isl_space_free(space);
9428 return mpa;
9429 error:
9430 isl_space_free(space);
9431 return NULL;
9434 /* Evaluate the affine function "aff" in the void point "pnt".
9435 * In particular, return the value NaN.
9437 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9438 __isl_take isl_point *pnt)
9440 isl_ctx *ctx;
9442 ctx = isl_point_get_ctx(pnt);
9443 isl_aff_free(aff);
9444 isl_point_free(pnt);
9445 return isl_val_nan(ctx);
9448 /* Evaluate the affine expression "aff"
9449 * in the coordinates (with denominator) "pnt".
9451 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9452 __isl_keep isl_vec *pnt)
9454 isl_int n, d;
9455 isl_ctx *ctx;
9456 isl_val *v;
9458 if (!aff || !pnt)
9459 return NULL;
9461 ctx = isl_vec_get_ctx(aff);
9462 isl_int_init(n);
9463 isl_int_init(d);
9464 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9465 isl_int_mul(d, aff->el[0], pnt->el[0]);
9466 v = isl_val_rat_from_isl_int(ctx, n, d);
9467 v = isl_val_normalize(v);
9468 isl_int_clear(n);
9469 isl_int_clear(d);
9471 return v;
9474 /* Check that the domain space of "aff" is equal to "space".
9476 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9477 __isl_keep isl_space *space)
9479 isl_bool ok;
9481 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9482 if (ok < 0)
9483 return isl_stat_error;
9484 if (!ok)
9485 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9486 "incompatible spaces", return isl_stat_error);
9487 return isl_stat_ok;
9490 /* Evaluate the affine function "aff" in "pnt".
9492 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9493 __isl_take isl_point *pnt)
9495 isl_bool is_void;
9496 isl_val *v;
9497 isl_local_space *ls;
9499 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9500 goto error;
9501 is_void = isl_point_is_void(pnt);
9502 if (is_void < 0)
9503 goto error;
9504 if (is_void)
9505 return eval_void(aff, pnt);
9507 ls = isl_aff_get_domain_local_space(aff);
9508 pnt = isl_local_space_lift_point(ls, pnt);
9510 v = eval(aff->v, isl_point_peek_vec(pnt));
9512 isl_aff_free(aff);
9513 isl_point_free(pnt);
9515 return v;
9516 error:
9517 isl_aff_free(aff);
9518 isl_point_free(pnt);
9519 return NULL;