isl_polynomial.c: use isl_stat enum instead of plain integers
[isl.git] / isl_aff.c
blob3e0ddd27591ebc0f004ef464a14f1c0bf3da0c27
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 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
561 exp = isl_reordering_extend_space(exp,
562 isl_aff_get_domain_space(aff));
563 aff = isl_aff_realign_domain(aff, exp);
566 isl_space_free(model);
567 return aff;
568 error:
569 isl_space_free(model);
570 isl_aff_free(aff);
571 return NULL;
574 /* Is "aff" obviously equal to zero?
576 * If the denominator is zero, then "aff" is not equal to zero.
578 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
580 if (!aff)
581 return isl_bool_error;
583 if (isl_int_is_zero(aff->v->el[0]))
584 return isl_bool_false;
585 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
588 /* Does "aff" represent NaN?
590 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
592 if (!aff)
593 return isl_bool_error;
595 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
598 /* Are "aff1" and "aff2" obviously equal?
600 * NaN is not equal to anything, not even to another NaN.
602 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
603 __isl_keep isl_aff *aff2)
605 isl_bool equal;
607 if (!aff1 || !aff2)
608 return isl_bool_error;
610 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
611 return isl_bool_false;
613 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
614 if (equal < 0 || !equal)
615 return equal;
617 return isl_vec_is_equal(aff1->v, aff2->v);
620 /* Return the common denominator of "aff" in "v".
622 * We cannot return anything meaningful in case of a NaN.
624 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
626 if (!aff)
627 return isl_stat_error;
628 if (isl_aff_is_nan(aff))
629 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
630 "cannot get denominator of NaN", return isl_stat_error);
631 isl_int_set(*v, aff->v->el[0]);
632 return isl_stat_ok;
635 /* Return the common denominator of "aff".
637 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
639 isl_ctx *ctx;
641 if (!aff)
642 return NULL;
644 ctx = isl_aff_get_ctx(aff);
645 if (isl_aff_is_nan(aff))
646 return isl_val_nan(ctx);
647 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
650 /* Return the constant term of "aff".
652 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
654 isl_ctx *ctx;
655 isl_val *v;
657 if (!aff)
658 return NULL;
660 ctx = isl_aff_get_ctx(aff);
661 if (isl_aff_is_nan(aff))
662 return isl_val_nan(ctx);
663 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
664 return isl_val_normalize(v);
667 /* Return the coefficient of the variable of type "type" at position "pos"
668 * of "aff".
670 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
671 enum isl_dim_type type, int pos)
673 isl_ctx *ctx;
674 isl_val *v;
676 if (!aff)
677 return NULL;
679 ctx = isl_aff_get_ctx(aff);
680 if (type == isl_dim_out)
681 isl_die(ctx, isl_error_invalid,
682 "output/set dimension does not have a coefficient",
683 return NULL);
684 if (type == isl_dim_in)
685 type = isl_dim_set;
687 if (pos >= isl_local_space_dim(aff->ls, type))
688 isl_die(ctx, isl_error_invalid,
689 "position out of bounds", return NULL);
691 if (isl_aff_is_nan(aff))
692 return isl_val_nan(ctx);
693 pos += isl_local_space_offset(aff->ls, type);
694 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
695 return isl_val_normalize(v);
698 /* Return the sign of the coefficient of the variable of type "type"
699 * at position "pos" of "aff".
701 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
702 int pos)
704 isl_ctx *ctx;
706 if (!aff)
707 return 0;
709 ctx = isl_aff_get_ctx(aff);
710 if (type == isl_dim_out)
711 isl_die(ctx, isl_error_invalid,
712 "output/set dimension does not have a coefficient",
713 return 0);
714 if (type == isl_dim_in)
715 type = isl_dim_set;
717 if (pos >= isl_local_space_dim(aff->ls, type))
718 isl_die(ctx, isl_error_invalid,
719 "position out of bounds", return 0);
721 pos += isl_local_space_offset(aff->ls, type);
722 return isl_int_sgn(aff->v->el[1 + pos]);
725 /* Replace the numerator of the constant term of "aff" by "v".
727 * A NaN is unaffected by this operation.
729 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
731 if (!aff)
732 return NULL;
733 if (isl_aff_is_nan(aff))
734 return aff;
735 aff = isl_aff_cow(aff);
736 if (!aff)
737 return NULL;
739 aff->v = isl_vec_cow(aff->v);
740 if (!aff->v)
741 return isl_aff_free(aff);
743 isl_int_set(aff->v->el[1], v);
745 return aff;
748 /* Replace the constant term of "aff" by "v".
750 * A NaN is unaffected by this operation.
752 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
753 __isl_take isl_val *v)
755 if (!aff || !v)
756 goto error;
758 if (isl_aff_is_nan(aff)) {
759 isl_val_free(v);
760 return aff;
763 if (!isl_val_is_rat(v))
764 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
765 "expecting rational value", goto error);
767 if (isl_int_eq(aff->v->el[1], v->n) &&
768 isl_int_eq(aff->v->el[0], v->d)) {
769 isl_val_free(v);
770 return aff;
773 aff = isl_aff_cow(aff);
774 if (!aff)
775 goto error;
776 aff->v = isl_vec_cow(aff->v);
777 if (!aff->v)
778 goto error;
780 if (isl_int_eq(aff->v->el[0], v->d)) {
781 isl_int_set(aff->v->el[1], v->n);
782 } else if (isl_int_is_one(v->d)) {
783 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
784 } else {
785 isl_seq_scale(aff->v->el + 1,
786 aff->v->el + 1, v->d, aff->v->size - 1);
787 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
788 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
789 aff->v = isl_vec_normalize(aff->v);
790 if (!aff->v)
791 goto error;
794 isl_val_free(v);
795 return aff;
796 error:
797 isl_aff_free(aff);
798 isl_val_free(v);
799 return NULL;
802 /* Add "v" to the constant term of "aff".
804 * A NaN is unaffected by this operation.
806 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
808 if (isl_int_is_zero(v))
809 return aff;
811 if (!aff)
812 return NULL;
813 if (isl_aff_is_nan(aff))
814 return aff;
815 aff = isl_aff_cow(aff);
816 if (!aff)
817 return NULL;
819 aff->v = isl_vec_cow(aff->v);
820 if (!aff->v)
821 return isl_aff_free(aff);
823 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
825 return aff;
828 /* Add "v" to the constant term of "aff".
830 * A NaN is unaffected by this operation.
832 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
833 __isl_take isl_val *v)
835 if (!aff || !v)
836 goto error;
838 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
839 isl_val_free(v);
840 return aff;
843 if (!isl_val_is_rat(v))
844 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
845 "expecting rational value", goto error);
847 aff = isl_aff_cow(aff);
848 if (!aff)
849 goto error;
851 aff->v = isl_vec_cow(aff->v);
852 if (!aff->v)
853 goto error;
855 if (isl_int_is_one(v->d)) {
856 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
857 } else if (isl_int_eq(aff->v->el[0], v->d)) {
858 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
859 aff->v = isl_vec_normalize(aff->v);
860 if (!aff->v)
861 goto error;
862 } else {
863 isl_seq_scale(aff->v->el + 1,
864 aff->v->el + 1, v->d, aff->v->size - 1);
865 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
866 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
867 aff->v = isl_vec_normalize(aff->v);
868 if (!aff->v)
869 goto error;
872 isl_val_free(v);
873 return aff;
874 error:
875 isl_aff_free(aff);
876 isl_val_free(v);
877 return NULL;
880 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
882 isl_int t;
884 isl_int_init(t);
885 isl_int_set_si(t, v);
886 aff = isl_aff_add_constant(aff, t);
887 isl_int_clear(t);
889 return aff;
892 /* Add "v" to the numerator of the constant term of "aff".
894 * A NaN is unaffected by this operation.
896 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
898 if (isl_int_is_zero(v))
899 return aff;
901 if (!aff)
902 return NULL;
903 if (isl_aff_is_nan(aff))
904 return aff;
905 aff = isl_aff_cow(aff);
906 if (!aff)
907 return NULL;
909 aff->v = isl_vec_cow(aff->v);
910 if (!aff->v)
911 return isl_aff_free(aff);
913 isl_int_add(aff->v->el[1], aff->v->el[1], v);
915 return aff;
918 /* Add "v" to the numerator of the constant term of "aff".
920 * A NaN is unaffected by this operation.
922 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
924 isl_int t;
926 if (v == 0)
927 return aff;
929 isl_int_init(t);
930 isl_int_set_si(t, v);
931 aff = isl_aff_add_constant_num(aff, t);
932 isl_int_clear(t);
934 return aff;
937 /* Replace the numerator of the constant term of "aff" by "v".
939 * A NaN is unaffected by this operation.
941 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
943 if (!aff)
944 return NULL;
945 if (isl_aff_is_nan(aff))
946 return aff;
947 aff = isl_aff_cow(aff);
948 if (!aff)
949 return NULL;
951 aff->v = isl_vec_cow(aff->v);
952 if (!aff->v)
953 return isl_aff_free(aff);
955 isl_int_set_si(aff->v->el[1], v);
957 return aff;
960 /* Replace the numerator of the coefficient of the variable of type "type"
961 * at position "pos" of "aff" by "v".
963 * A NaN is unaffected by this operation.
965 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
966 enum isl_dim_type type, int pos, isl_int v)
968 if (!aff)
969 return NULL;
971 if (type == isl_dim_out)
972 isl_die(aff->v->ctx, isl_error_invalid,
973 "output/set dimension does not have a coefficient",
974 return isl_aff_free(aff));
975 if (type == isl_dim_in)
976 type = isl_dim_set;
978 if (pos >= isl_local_space_dim(aff->ls, type))
979 isl_die(aff->v->ctx, isl_error_invalid,
980 "position out of bounds", return isl_aff_free(aff));
982 if (isl_aff_is_nan(aff))
983 return aff;
984 aff = isl_aff_cow(aff);
985 if (!aff)
986 return NULL;
988 aff->v = isl_vec_cow(aff->v);
989 if (!aff->v)
990 return isl_aff_free(aff);
992 pos += isl_local_space_offset(aff->ls, type);
993 isl_int_set(aff->v->el[1 + pos], v);
995 return aff;
998 /* Replace the numerator of the coefficient of the variable of type "type"
999 * at position "pos" of "aff" by "v".
1001 * A NaN is unaffected by this operation.
1003 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1004 enum isl_dim_type type, int pos, int v)
1006 if (!aff)
1007 return NULL;
1009 if (type == isl_dim_out)
1010 isl_die(aff->v->ctx, isl_error_invalid,
1011 "output/set dimension does not have a coefficient",
1012 return isl_aff_free(aff));
1013 if (type == isl_dim_in)
1014 type = isl_dim_set;
1016 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1017 isl_die(aff->v->ctx, isl_error_invalid,
1018 "position out of bounds", return isl_aff_free(aff));
1020 if (isl_aff_is_nan(aff))
1021 return aff;
1022 pos += isl_local_space_offset(aff->ls, type);
1023 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1024 return aff;
1026 aff = isl_aff_cow(aff);
1027 if (!aff)
1028 return NULL;
1030 aff->v = isl_vec_cow(aff->v);
1031 if (!aff->v)
1032 return isl_aff_free(aff);
1034 isl_int_set_si(aff->v->el[1 + pos], v);
1036 return aff;
1039 /* Replace the coefficient of the variable of type "type" at position "pos"
1040 * of "aff" by "v".
1042 * A NaN is unaffected by this operation.
1044 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1045 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1047 if (!aff || !v)
1048 goto error;
1050 if (type == isl_dim_out)
1051 isl_die(aff->v->ctx, isl_error_invalid,
1052 "output/set dimension does not have a coefficient",
1053 goto error);
1054 if (type == isl_dim_in)
1055 type = isl_dim_set;
1057 if (pos >= isl_local_space_dim(aff->ls, type))
1058 isl_die(aff->v->ctx, isl_error_invalid,
1059 "position out of bounds", goto error);
1061 if (isl_aff_is_nan(aff)) {
1062 isl_val_free(v);
1063 return aff;
1065 if (!isl_val_is_rat(v))
1066 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1067 "expecting rational value", goto error);
1069 pos += isl_local_space_offset(aff->ls, type);
1070 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1071 isl_int_eq(aff->v->el[0], v->d)) {
1072 isl_val_free(v);
1073 return aff;
1076 aff = isl_aff_cow(aff);
1077 if (!aff)
1078 goto error;
1079 aff->v = isl_vec_cow(aff->v);
1080 if (!aff->v)
1081 goto error;
1083 if (isl_int_eq(aff->v->el[0], v->d)) {
1084 isl_int_set(aff->v->el[1 + pos], v->n);
1085 } else if (isl_int_is_one(v->d)) {
1086 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1087 } else {
1088 isl_seq_scale(aff->v->el + 1,
1089 aff->v->el + 1, v->d, aff->v->size - 1);
1090 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1091 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1092 aff->v = isl_vec_normalize(aff->v);
1093 if (!aff->v)
1094 goto error;
1097 isl_val_free(v);
1098 return aff;
1099 error:
1100 isl_aff_free(aff);
1101 isl_val_free(v);
1102 return NULL;
1105 /* Add "v" to the coefficient of the variable of type "type"
1106 * at position "pos" of "aff".
1108 * A NaN is unaffected by this operation.
1110 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1111 enum isl_dim_type type, int pos, isl_int v)
1113 if (!aff)
1114 return NULL;
1116 if (type == isl_dim_out)
1117 isl_die(aff->v->ctx, isl_error_invalid,
1118 "output/set dimension does not have a coefficient",
1119 return isl_aff_free(aff));
1120 if (type == isl_dim_in)
1121 type = isl_dim_set;
1123 if (pos >= isl_local_space_dim(aff->ls, type))
1124 isl_die(aff->v->ctx, isl_error_invalid,
1125 "position out of bounds", return isl_aff_free(aff));
1127 if (isl_aff_is_nan(aff))
1128 return aff;
1129 aff = isl_aff_cow(aff);
1130 if (!aff)
1131 return NULL;
1133 aff->v = isl_vec_cow(aff->v);
1134 if (!aff->v)
1135 return isl_aff_free(aff);
1137 pos += isl_local_space_offset(aff->ls, type);
1138 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1140 return aff;
1143 /* Add "v" to the coefficient of the variable of type "type"
1144 * at position "pos" of "aff".
1146 * A NaN is unaffected by this operation.
1148 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1149 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1151 if (!aff || !v)
1152 goto error;
1154 if (isl_val_is_zero(v)) {
1155 isl_val_free(v);
1156 return aff;
1159 if (type == isl_dim_out)
1160 isl_die(aff->v->ctx, isl_error_invalid,
1161 "output/set dimension does not have a coefficient",
1162 goto error);
1163 if (type == isl_dim_in)
1164 type = isl_dim_set;
1166 if (pos >= isl_local_space_dim(aff->ls, type))
1167 isl_die(aff->v->ctx, isl_error_invalid,
1168 "position out of bounds", goto error);
1170 if (isl_aff_is_nan(aff)) {
1171 isl_val_free(v);
1172 return aff;
1174 if (!isl_val_is_rat(v))
1175 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1176 "expecting rational value", goto error);
1178 aff = isl_aff_cow(aff);
1179 if (!aff)
1180 goto error;
1182 aff->v = isl_vec_cow(aff->v);
1183 if (!aff->v)
1184 goto error;
1186 pos += isl_local_space_offset(aff->ls, type);
1187 if (isl_int_is_one(v->d)) {
1188 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1189 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1190 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1191 aff->v = isl_vec_normalize(aff->v);
1192 if (!aff->v)
1193 goto error;
1194 } else {
1195 isl_seq_scale(aff->v->el + 1,
1196 aff->v->el + 1, v->d, aff->v->size - 1);
1197 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1198 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1199 aff->v = isl_vec_normalize(aff->v);
1200 if (!aff->v)
1201 goto error;
1204 isl_val_free(v);
1205 return aff;
1206 error:
1207 isl_aff_free(aff);
1208 isl_val_free(v);
1209 return NULL;
1212 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1213 enum isl_dim_type type, int pos, int v)
1215 isl_int t;
1217 isl_int_init(t);
1218 isl_int_set_si(t, v);
1219 aff = isl_aff_add_coefficient(aff, type, pos, t);
1220 isl_int_clear(t);
1222 return aff;
1225 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1227 if (!aff)
1228 return NULL;
1230 return isl_local_space_get_div(aff->ls, pos);
1233 /* Return the negation of "aff".
1235 * As a special case, -NaN = NaN.
1237 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1239 if (!aff)
1240 return NULL;
1241 if (isl_aff_is_nan(aff))
1242 return aff;
1243 aff = isl_aff_cow(aff);
1244 if (!aff)
1245 return NULL;
1246 aff->v = isl_vec_cow(aff->v);
1247 if (!aff->v)
1248 return isl_aff_free(aff);
1250 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1252 return aff;
1255 /* Remove divs from the local space that do not appear in the affine
1256 * expression.
1257 * We currently only remove divs at the end.
1258 * Some intermediate divs may also not appear directly in the affine
1259 * expression, but we would also need to check that no other divs are
1260 * defined in terms of them.
1262 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1264 int pos;
1265 int off;
1266 int n;
1268 if (!aff)
1269 return NULL;
1271 n = isl_local_space_dim(aff->ls, isl_dim_div);
1272 off = isl_local_space_offset(aff->ls, isl_dim_div);
1274 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1275 if (pos == n)
1276 return aff;
1278 aff = isl_aff_cow(aff);
1279 if (!aff)
1280 return NULL;
1282 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1283 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1284 if (!aff->ls || !aff->v)
1285 return isl_aff_free(aff);
1287 return aff;
1290 /* Look for any divs in the aff->ls with a denominator equal to one
1291 * and plug them into the affine expression and any subsequent divs
1292 * that may reference the div.
1294 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1296 int i, n;
1297 int len;
1298 isl_int v;
1299 isl_vec *vec;
1300 isl_local_space *ls;
1301 unsigned pos;
1303 if (!aff)
1304 return NULL;
1306 n = isl_local_space_dim(aff->ls, isl_dim_div);
1307 len = aff->v->size;
1308 for (i = 0; i < n; ++i) {
1309 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1310 continue;
1311 ls = isl_local_space_copy(aff->ls);
1312 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1313 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1314 vec = isl_vec_copy(aff->v);
1315 vec = isl_vec_cow(vec);
1316 if (!ls || !vec)
1317 goto error;
1319 isl_int_init(v);
1321 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1322 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1323 len, len, v);
1325 isl_int_clear(v);
1327 isl_vec_free(aff->v);
1328 aff->v = vec;
1329 isl_local_space_free(aff->ls);
1330 aff->ls = ls;
1333 return aff;
1334 error:
1335 isl_vec_free(vec);
1336 isl_local_space_free(ls);
1337 return isl_aff_free(aff);
1340 /* Look for any divs j that appear with a unit coefficient inside
1341 * the definitions of other divs i and plug them into the definitions
1342 * of the divs i.
1344 * In particular, an expression of the form
1346 * floor((f(..) + floor(g(..)/n))/m)
1348 * is simplified to
1350 * floor((n * f(..) + g(..))/(n * m))
1352 * This simplification is correct because we can move the expression
1353 * f(..) into the inner floor in the original expression to obtain
1355 * floor(floor((n * f(..) + g(..))/n)/m)
1357 * from which we can derive the simplified expression.
1359 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1361 int i, j, n;
1362 int off;
1364 if (!aff)
1365 return NULL;
1367 n = isl_local_space_dim(aff->ls, isl_dim_div);
1368 off = isl_local_space_offset(aff->ls, isl_dim_div);
1369 for (i = 1; i < n; ++i) {
1370 for (j = 0; j < i; ++j) {
1371 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1372 continue;
1373 aff->ls = isl_local_space_substitute_seq(aff->ls,
1374 isl_dim_div, j, aff->ls->div->row[j],
1375 aff->v->size, i, 1);
1376 if (!aff->ls)
1377 return isl_aff_free(aff);
1381 return aff;
1384 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1386 * Even though this function is only called on isl_affs with a single
1387 * reference, we are careful to only change aff->v and aff->ls together.
1389 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1391 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1392 isl_local_space *ls;
1393 isl_vec *v;
1395 ls = isl_local_space_copy(aff->ls);
1396 ls = isl_local_space_swap_div(ls, a, b);
1397 v = isl_vec_copy(aff->v);
1398 v = isl_vec_cow(v);
1399 if (!ls || !v)
1400 goto error;
1402 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1403 isl_vec_free(aff->v);
1404 aff->v = v;
1405 isl_local_space_free(aff->ls);
1406 aff->ls = ls;
1408 return aff;
1409 error:
1410 isl_vec_free(v);
1411 isl_local_space_free(ls);
1412 return isl_aff_free(aff);
1415 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1417 * We currently do not actually remove div "b", but simply add its
1418 * coefficient to that of "a" and then zero it out.
1420 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1422 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1424 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1425 return aff;
1427 aff->v = isl_vec_cow(aff->v);
1428 if (!aff->v)
1429 return isl_aff_free(aff);
1431 isl_int_add(aff->v->el[1 + off + a],
1432 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1433 isl_int_set_si(aff->v->el[1 + off + b], 0);
1435 return aff;
1438 /* Sort the divs in the local space of "aff" according to
1439 * the comparison function "cmp_row" in isl_local_space.c,
1440 * combining the coefficients of identical divs.
1442 * Reordering divs does not change the semantics of "aff",
1443 * so there is no need to call isl_aff_cow.
1444 * Moreover, this function is currently only called on isl_affs
1445 * with a single reference.
1447 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1449 int i, j, n;
1451 if (!aff)
1452 return NULL;
1454 n = isl_aff_dim(aff, isl_dim_div);
1455 for (i = 1; i < n; ++i) {
1456 for (j = i - 1; j >= 0; --j) {
1457 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1458 if (cmp < 0)
1459 break;
1460 if (cmp == 0)
1461 aff = merge_divs(aff, j, j + 1);
1462 else
1463 aff = swap_div(aff, j, j + 1);
1464 if (!aff)
1465 return NULL;
1469 return aff;
1472 /* Normalize the representation of "aff".
1474 * This function should only be called of "new" isl_affs, i.e.,
1475 * with only a single reference. We therefore do not need to
1476 * worry about affecting other instances.
1478 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1480 if (!aff)
1481 return NULL;
1482 aff->v = isl_vec_normalize(aff->v);
1483 if (!aff->v)
1484 return isl_aff_free(aff);
1485 aff = plug_in_integral_divs(aff);
1486 aff = plug_in_unit_divs(aff);
1487 aff = sort_divs(aff);
1488 aff = isl_aff_remove_unused_divs(aff);
1489 return aff;
1492 /* Given f, return floor(f).
1493 * If f is an integer expression, then just return f.
1494 * If f is a constant, then return the constant floor(f).
1495 * Otherwise, if f = g/m, write g = q m + r,
1496 * create a new div d = [r/m] and return the expression q + d.
1497 * The coefficients in r are taken to lie between -m/2 and m/2.
1499 * reduce_div_coefficients performs the same normalization.
1501 * As a special case, floor(NaN) = NaN.
1503 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1505 int i;
1506 int size;
1507 isl_ctx *ctx;
1508 isl_vec *div;
1510 if (!aff)
1511 return NULL;
1513 if (isl_aff_is_nan(aff))
1514 return aff;
1515 if (isl_int_is_one(aff->v->el[0]))
1516 return aff;
1518 aff = isl_aff_cow(aff);
1519 if (!aff)
1520 return NULL;
1522 aff->v = isl_vec_cow(aff->v);
1523 if (!aff->v)
1524 return isl_aff_free(aff);
1526 if (isl_aff_is_cst(aff)) {
1527 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1528 isl_int_set_si(aff->v->el[0], 1);
1529 return aff;
1532 div = isl_vec_copy(aff->v);
1533 div = isl_vec_cow(div);
1534 if (!div)
1535 return isl_aff_free(aff);
1537 ctx = isl_aff_get_ctx(aff);
1538 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1539 for (i = 1; i < aff->v->size; ++i) {
1540 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1541 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1542 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1543 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1544 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1548 aff->ls = isl_local_space_add_div(aff->ls, div);
1549 if (!aff->ls)
1550 return isl_aff_free(aff);
1552 size = aff->v->size;
1553 aff->v = isl_vec_extend(aff->v, size + 1);
1554 if (!aff->v)
1555 return isl_aff_free(aff);
1556 isl_int_set_si(aff->v->el[0], 1);
1557 isl_int_set_si(aff->v->el[size], 1);
1559 aff = isl_aff_normalize(aff);
1561 return aff;
1564 /* Compute
1566 * aff mod m = aff - m * floor(aff/m)
1568 * with m an integer value.
1570 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1571 __isl_take isl_val *m)
1573 isl_aff *res;
1575 if (!aff || !m)
1576 goto error;
1578 if (!isl_val_is_int(m))
1579 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1580 "expecting integer modulo", goto error);
1582 res = isl_aff_copy(aff);
1583 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1584 aff = isl_aff_floor(aff);
1585 aff = isl_aff_scale_val(aff, m);
1586 res = isl_aff_sub(res, aff);
1588 return res;
1589 error:
1590 isl_aff_free(aff);
1591 isl_val_free(m);
1592 return NULL;
1595 /* Compute
1597 * pwaff mod m = pwaff - m * floor(pwaff/m)
1599 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1601 isl_pw_aff *res;
1603 res = isl_pw_aff_copy(pwaff);
1604 pwaff = isl_pw_aff_scale_down(pwaff, m);
1605 pwaff = isl_pw_aff_floor(pwaff);
1606 pwaff = isl_pw_aff_scale(pwaff, m);
1607 res = isl_pw_aff_sub(res, pwaff);
1609 return res;
1612 /* Compute
1614 * pa mod m = pa - m * floor(pa/m)
1616 * with m an integer value.
1618 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1619 __isl_take isl_val *m)
1621 if (!pa || !m)
1622 goto error;
1623 if (!isl_val_is_int(m))
1624 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1625 "expecting integer modulo", goto error);
1626 pa = isl_pw_aff_mod(pa, m->n);
1627 isl_val_free(m);
1628 return pa;
1629 error:
1630 isl_pw_aff_free(pa);
1631 isl_val_free(m);
1632 return NULL;
1635 /* Given f, return ceil(f).
1636 * If f is an integer expression, then just return f.
1637 * Otherwise, let f be the expression
1639 * e/m
1641 * then return
1643 * floor((e + m - 1)/m)
1645 * As a special case, ceil(NaN) = NaN.
1647 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1649 if (!aff)
1650 return NULL;
1652 if (isl_aff_is_nan(aff))
1653 return aff;
1654 if (isl_int_is_one(aff->v->el[0]))
1655 return aff;
1657 aff = isl_aff_cow(aff);
1658 if (!aff)
1659 return NULL;
1660 aff->v = isl_vec_cow(aff->v);
1661 if (!aff->v)
1662 return isl_aff_free(aff);
1664 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1665 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1666 aff = isl_aff_floor(aff);
1668 return aff;
1671 /* Apply the expansion computed by isl_merge_divs.
1672 * The expansion itself is given by "exp" while the resulting
1673 * list of divs is given by "div".
1675 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1676 __isl_take isl_mat *div, int *exp)
1678 int old_n_div;
1679 int new_n_div;
1680 int offset;
1682 aff = isl_aff_cow(aff);
1683 if (!aff || !div)
1684 goto error;
1686 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1687 new_n_div = isl_mat_rows(div);
1688 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1690 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1691 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1692 if (!aff->v || !aff->ls)
1693 return isl_aff_free(aff);
1694 return aff;
1695 error:
1696 isl_aff_free(aff);
1697 isl_mat_free(div);
1698 return NULL;
1701 /* Add two affine expressions that live in the same local space.
1703 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1704 __isl_take isl_aff *aff2)
1706 isl_int gcd, f;
1708 aff1 = isl_aff_cow(aff1);
1709 if (!aff1 || !aff2)
1710 goto error;
1712 aff1->v = isl_vec_cow(aff1->v);
1713 if (!aff1->v)
1714 goto error;
1716 isl_int_init(gcd);
1717 isl_int_init(f);
1718 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1719 isl_int_divexact(f, aff2->v->el[0], gcd);
1720 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1721 isl_int_divexact(f, aff1->v->el[0], gcd);
1722 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1723 isl_int_divexact(f, aff2->v->el[0], gcd);
1724 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1725 isl_int_clear(f);
1726 isl_int_clear(gcd);
1728 isl_aff_free(aff2);
1729 return aff1;
1730 error:
1731 isl_aff_free(aff1);
1732 isl_aff_free(aff2);
1733 return NULL;
1736 /* Return the sum of "aff1" and "aff2".
1738 * If either of the two is NaN, then the result is NaN.
1740 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1741 __isl_take isl_aff *aff2)
1743 isl_ctx *ctx;
1744 int *exp1 = NULL;
1745 int *exp2 = NULL;
1746 isl_mat *div;
1747 int n_div1, n_div2;
1749 if (!aff1 || !aff2)
1750 goto error;
1752 ctx = isl_aff_get_ctx(aff1);
1753 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1754 isl_die(ctx, isl_error_invalid,
1755 "spaces don't match", goto error);
1757 if (isl_aff_is_nan(aff1)) {
1758 isl_aff_free(aff2);
1759 return aff1;
1761 if (isl_aff_is_nan(aff2)) {
1762 isl_aff_free(aff1);
1763 return aff2;
1766 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1767 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1768 if (n_div1 == 0 && n_div2 == 0)
1769 return add_expanded(aff1, aff2);
1771 exp1 = isl_alloc_array(ctx, int, n_div1);
1772 exp2 = isl_alloc_array(ctx, int, n_div2);
1773 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1774 goto error;
1776 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1777 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1778 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1779 free(exp1);
1780 free(exp2);
1782 return add_expanded(aff1, aff2);
1783 error:
1784 free(exp1);
1785 free(exp2);
1786 isl_aff_free(aff1);
1787 isl_aff_free(aff2);
1788 return NULL;
1791 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1792 __isl_take isl_aff *aff2)
1794 return isl_aff_add(aff1, isl_aff_neg(aff2));
1797 /* Return the result of scaling "aff" by a factor of "f".
1799 * As a special case, f * NaN = NaN.
1801 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1803 isl_int gcd;
1805 if (!aff)
1806 return NULL;
1807 if (isl_aff_is_nan(aff))
1808 return aff;
1810 if (isl_int_is_one(f))
1811 return aff;
1813 aff = isl_aff_cow(aff);
1814 if (!aff)
1815 return NULL;
1816 aff->v = isl_vec_cow(aff->v);
1817 if (!aff->v)
1818 return isl_aff_free(aff);
1820 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1821 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1822 return aff;
1825 isl_int_init(gcd);
1826 isl_int_gcd(gcd, aff->v->el[0], f);
1827 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1828 isl_int_divexact(gcd, f, gcd);
1829 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1830 isl_int_clear(gcd);
1832 return aff;
1835 /* Multiple "aff" by "v".
1837 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1838 __isl_take isl_val *v)
1840 if (!aff || !v)
1841 goto error;
1843 if (isl_val_is_one(v)) {
1844 isl_val_free(v);
1845 return aff;
1848 if (!isl_val_is_rat(v))
1849 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1850 "expecting rational factor", goto error);
1852 aff = isl_aff_scale(aff, v->n);
1853 aff = isl_aff_scale_down(aff, v->d);
1855 isl_val_free(v);
1856 return aff;
1857 error:
1858 isl_aff_free(aff);
1859 isl_val_free(v);
1860 return NULL;
1863 /* Return the result of scaling "aff" down by a factor of "f".
1865 * As a special case, NaN/f = NaN.
1867 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1869 isl_int gcd;
1871 if (!aff)
1872 return NULL;
1873 if (isl_aff_is_nan(aff))
1874 return aff;
1876 if (isl_int_is_one(f))
1877 return aff;
1879 aff = isl_aff_cow(aff);
1880 if (!aff)
1881 return NULL;
1883 if (isl_int_is_zero(f))
1884 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1885 "cannot scale down by zero", return isl_aff_free(aff));
1887 aff->v = isl_vec_cow(aff->v);
1888 if (!aff->v)
1889 return isl_aff_free(aff);
1891 isl_int_init(gcd);
1892 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1893 isl_int_gcd(gcd, gcd, f);
1894 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1895 isl_int_divexact(gcd, f, gcd);
1896 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1897 isl_int_clear(gcd);
1899 return aff;
1902 /* Divide "aff" by "v".
1904 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1905 __isl_take isl_val *v)
1907 if (!aff || !v)
1908 goto error;
1910 if (isl_val_is_one(v)) {
1911 isl_val_free(v);
1912 return aff;
1915 if (!isl_val_is_rat(v))
1916 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1917 "expecting rational factor", goto error);
1918 if (!isl_val_is_pos(v))
1919 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1920 "factor needs to be positive", goto error);
1922 aff = isl_aff_scale(aff, v->d);
1923 aff = isl_aff_scale_down(aff, v->n);
1925 isl_val_free(v);
1926 return aff;
1927 error:
1928 isl_aff_free(aff);
1929 isl_val_free(v);
1930 return NULL;
1933 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1935 isl_int v;
1937 if (f == 1)
1938 return aff;
1940 isl_int_init(v);
1941 isl_int_set_ui(v, f);
1942 aff = isl_aff_scale_down(aff, v);
1943 isl_int_clear(v);
1945 return aff;
1948 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1949 enum isl_dim_type type, unsigned pos, const char *s)
1951 aff = isl_aff_cow(aff);
1952 if (!aff)
1953 return NULL;
1954 if (type == isl_dim_out)
1955 isl_die(aff->v->ctx, isl_error_invalid,
1956 "cannot set name of output/set dimension",
1957 return isl_aff_free(aff));
1958 if (type == isl_dim_in)
1959 type = isl_dim_set;
1960 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1961 if (!aff->ls)
1962 return isl_aff_free(aff);
1964 return aff;
1967 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1968 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1970 aff = isl_aff_cow(aff);
1971 if (!aff)
1972 goto error;
1973 if (type == isl_dim_out)
1974 isl_die(aff->v->ctx, isl_error_invalid,
1975 "cannot set name of output/set dimension",
1976 goto error);
1977 if (type == isl_dim_in)
1978 type = isl_dim_set;
1979 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1980 if (!aff->ls)
1981 return isl_aff_free(aff);
1983 return aff;
1984 error:
1985 isl_id_free(id);
1986 isl_aff_free(aff);
1987 return NULL;
1990 /* Replace the identifier of the input tuple of "aff" by "id".
1991 * type is currently required to be equal to isl_dim_in
1993 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1994 enum isl_dim_type type, __isl_take isl_id *id)
1996 aff = isl_aff_cow(aff);
1997 if (!aff)
1998 goto error;
1999 if (type != isl_dim_in)
2000 isl_die(aff->v->ctx, isl_error_invalid,
2001 "cannot only set id of input tuple", goto error);
2002 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2003 if (!aff->ls)
2004 return isl_aff_free(aff);
2006 return aff;
2007 error:
2008 isl_id_free(id);
2009 isl_aff_free(aff);
2010 return NULL;
2013 /* Exploit the equalities in "eq" to simplify the affine expression
2014 * and the expressions of the integer divisions in the local space.
2015 * The integer divisions in this local space are assumed to appear
2016 * as regular dimensions in "eq".
2018 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2019 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2021 int i, j;
2022 unsigned total;
2023 unsigned n_div;
2025 if (!eq)
2026 goto error;
2027 if (eq->n_eq == 0) {
2028 isl_basic_set_free(eq);
2029 return aff;
2032 aff = isl_aff_cow(aff);
2033 if (!aff)
2034 goto error;
2036 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2037 isl_basic_set_copy(eq));
2038 aff->v = isl_vec_cow(aff->v);
2039 if (!aff->ls || !aff->v)
2040 goto error;
2042 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2043 n_div = eq->n_div;
2044 for (i = 0; i < eq->n_eq; ++i) {
2045 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2046 if (j < 0 || j == 0 || j >= total)
2047 continue;
2049 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2050 &aff->v->el[0]);
2053 isl_basic_set_free(eq);
2054 aff = isl_aff_normalize(aff);
2055 return aff;
2056 error:
2057 isl_basic_set_free(eq);
2058 isl_aff_free(aff);
2059 return NULL;
2062 /* Exploit the equalities in "eq" to simplify the affine expression
2063 * and the expressions of the integer divisions in the local space.
2065 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2066 __isl_take isl_basic_set *eq)
2068 int n_div;
2070 if (!aff || !eq)
2071 goto error;
2072 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2073 if (n_div > 0)
2074 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2075 return isl_aff_substitute_equalities_lifted(aff, eq);
2076 error:
2077 isl_basic_set_free(eq);
2078 isl_aff_free(aff);
2079 return NULL;
2082 /* Look for equalities among the variables shared by context and aff
2083 * and the integer divisions of aff, if any.
2084 * The equalities are then used to eliminate coefficients and/or integer
2085 * divisions from aff.
2087 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2088 __isl_take isl_set *context)
2090 isl_basic_set *hull;
2091 int n_div;
2093 if (!aff)
2094 goto error;
2095 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2096 if (n_div > 0) {
2097 isl_basic_set *bset;
2098 isl_local_space *ls;
2099 context = isl_set_add_dims(context, isl_dim_set, n_div);
2100 ls = isl_aff_get_domain_local_space(aff);
2101 bset = isl_basic_set_from_local_space(ls);
2102 bset = isl_basic_set_lift(bset);
2103 bset = isl_basic_set_flatten(bset);
2104 context = isl_set_intersect(context,
2105 isl_set_from_basic_set(bset));
2108 hull = isl_set_affine_hull(context);
2109 return isl_aff_substitute_equalities_lifted(aff, hull);
2110 error:
2111 isl_aff_free(aff);
2112 isl_set_free(context);
2113 return NULL;
2116 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2117 __isl_take isl_set *context)
2119 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2120 dom_context = isl_set_intersect_params(dom_context, context);
2121 return isl_aff_gist(aff, dom_context);
2124 /* Return a basic set containing those elements in the space
2125 * of aff where it is positive. "rational" should not be set.
2127 * If "aff" is NaN, then it is not positive.
2129 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2130 int rational)
2132 isl_constraint *ineq;
2133 isl_basic_set *bset;
2134 isl_val *c;
2136 if (!aff)
2137 return NULL;
2138 if (isl_aff_is_nan(aff)) {
2139 isl_space *space = isl_aff_get_domain_space(aff);
2140 isl_aff_free(aff);
2141 return isl_basic_set_empty(space);
2143 if (rational)
2144 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2145 "rational sets not supported", goto error);
2147 ineq = isl_inequality_from_aff(aff);
2148 c = isl_constraint_get_constant_val(ineq);
2149 c = isl_val_sub_ui(c, 1);
2150 ineq = isl_constraint_set_constant_val(ineq, c);
2152 bset = isl_basic_set_from_constraint(ineq);
2153 bset = isl_basic_set_simplify(bset);
2154 return bset;
2155 error:
2156 isl_aff_free(aff);
2157 return NULL;
2160 /* Return a basic set containing those elements in the space
2161 * of aff where it is non-negative.
2162 * If "rational" is set, then return a rational basic set.
2164 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2166 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2167 __isl_take isl_aff *aff, int rational)
2169 isl_constraint *ineq;
2170 isl_basic_set *bset;
2172 if (!aff)
2173 return NULL;
2174 if (isl_aff_is_nan(aff)) {
2175 isl_space *space = isl_aff_get_domain_space(aff);
2176 isl_aff_free(aff);
2177 return isl_basic_set_empty(space);
2180 ineq = isl_inequality_from_aff(aff);
2182 bset = isl_basic_set_from_constraint(ineq);
2183 if (rational)
2184 bset = isl_basic_set_set_rational(bset);
2185 bset = isl_basic_set_simplify(bset);
2186 return bset;
2189 /* Return a basic set containing those elements in the space
2190 * of aff where it is non-negative.
2192 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2194 return aff_nonneg_basic_set(aff, 0);
2197 /* Return a basic set containing those elements in the domain space
2198 * of "aff" where it is positive.
2200 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2202 aff = isl_aff_add_constant_num_si(aff, -1);
2203 return isl_aff_nonneg_basic_set(aff);
2206 /* Return a basic set containing those elements in the domain space
2207 * of aff where it is negative.
2209 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2211 aff = isl_aff_neg(aff);
2212 return isl_aff_pos_basic_set(aff);
2215 /* Return a basic set containing those elements in the space
2216 * of aff where it is zero.
2217 * If "rational" is set, then return a rational basic set.
2219 * If "aff" is NaN, then it is not zero.
2221 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2222 int rational)
2224 isl_constraint *ineq;
2225 isl_basic_set *bset;
2227 if (!aff)
2228 return NULL;
2229 if (isl_aff_is_nan(aff)) {
2230 isl_space *space = isl_aff_get_domain_space(aff);
2231 isl_aff_free(aff);
2232 return isl_basic_set_empty(space);
2235 ineq = isl_equality_from_aff(aff);
2237 bset = isl_basic_set_from_constraint(ineq);
2238 if (rational)
2239 bset = isl_basic_set_set_rational(bset);
2240 bset = isl_basic_set_simplify(bset);
2241 return bset;
2244 /* Return a basic set containing those elements in the space
2245 * of aff where it is zero.
2247 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2249 return aff_zero_basic_set(aff, 0);
2252 /* Return a basic set containing those elements in the shared space
2253 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2255 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2256 __isl_take isl_aff *aff2)
2258 aff1 = isl_aff_sub(aff1, aff2);
2260 return isl_aff_nonneg_basic_set(aff1);
2263 /* Return a basic set containing those elements in the shared domain space
2264 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2266 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2267 __isl_take isl_aff *aff2)
2269 aff1 = isl_aff_sub(aff1, aff2);
2271 return isl_aff_pos_basic_set(aff1);
2274 /* Return a set containing those elements in the shared space
2275 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2277 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2278 __isl_take isl_aff *aff2)
2280 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2283 /* Return a set containing those elements in the shared domain space
2284 * of aff1 and aff2 where aff1 is greater than aff2.
2286 * If either of the two inputs is NaN, then the result is empty,
2287 * as comparisons with NaN always return false.
2289 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2290 __isl_take isl_aff *aff2)
2292 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2295 /* Return a basic set containing those elements in the shared space
2296 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2298 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2299 __isl_take isl_aff *aff2)
2301 return isl_aff_ge_basic_set(aff2, aff1);
2304 /* Return a basic set containing those elements in the shared domain space
2305 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2307 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2308 __isl_take isl_aff *aff2)
2310 return isl_aff_gt_basic_set(aff2, aff1);
2313 /* Return a set containing those elements in the shared space
2314 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2316 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2317 __isl_take isl_aff *aff2)
2319 return isl_aff_ge_set(aff2, aff1);
2322 /* Return a set containing those elements in the shared domain space
2323 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2325 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2326 __isl_take isl_aff *aff2)
2328 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2331 /* Return a basic set containing those elements in the shared space
2332 * of aff1 and aff2 where aff1 and aff2 are equal.
2334 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2335 __isl_take isl_aff *aff2)
2337 aff1 = isl_aff_sub(aff1, aff2);
2339 return isl_aff_zero_basic_set(aff1);
2342 /* Return a set containing those elements in the shared space
2343 * of aff1 and aff2 where aff1 and aff2 are equal.
2345 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2346 __isl_take isl_aff *aff2)
2348 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2351 /* Return a set containing those elements in the shared domain space
2352 * of aff1 and aff2 where aff1 and aff2 are not equal.
2354 * If either of the two inputs is NaN, then the result is empty,
2355 * as comparisons with NaN always return false.
2357 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2358 __isl_take isl_aff *aff2)
2360 isl_set *set_lt, *set_gt;
2362 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2363 isl_aff_copy(aff2));
2364 set_gt = isl_aff_gt_set(aff1, aff2);
2365 return isl_set_union_disjoint(set_lt, set_gt);
2368 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2369 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2371 aff1 = isl_aff_add(aff1, aff2);
2372 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2373 return aff1;
2376 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2378 if (!aff)
2379 return -1;
2381 return 0;
2384 /* Check whether the given affine expression has non-zero coefficient
2385 * for any dimension in the given range or if any of these dimensions
2386 * appear with non-zero coefficients in any of the integer divisions
2387 * involved in the affine expression.
2389 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2390 enum isl_dim_type type, unsigned first, unsigned n)
2392 int i;
2393 isl_ctx *ctx;
2394 int *active = NULL;
2395 isl_bool involves = isl_bool_false;
2397 if (!aff)
2398 return isl_bool_error;
2399 if (n == 0)
2400 return isl_bool_false;
2402 ctx = isl_aff_get_ctx(aff);
2403 if (first + n > isl_aff_dim(aff, type))
2404 isl_die(ctx, isl_error_invalid,
2405 "range out of bounds", return isl_bool_error);
2407 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2408 if (!active)
2409 goto error;
2411 first += isl_local_space_offset(aff->ls, type) - 1;
2412 for (i = 0; i < n; ++i)
2413 if (active[first + i]) {
2414 involves = isl_bool_true;
2415 break;
2418 free(active);
2420 return involves;
2421 error:
2422 free(active);
2423 return isl_bool_error;
2426 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2427 enum isl_dim_type type, unsigned first, unsigned n)
2429 isl_ctx *ctx;
2431 if (!aff)
2432 return NULL;
2433 if (type == isl_dim_out)
2434 isl_die(aff->v->ctx, isl_error_invalid,
2435 "cannot drop output/set dimension",
2436 return isl_aff_free(aff));
2437 if (type == isl_dim_in)
2438 type = isl_dim_set;
2439 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2440 return aff;
2442 ctx = isl_aff_get_ctx(aff);
2443 if (first + n > isl_local_space_dim(aff->ls, type))
2444 isl_die(ctx, isl_error_invalid, "range out of bounds",
2445 return isl_aff_free(aff));
2447 aff = isl_aff_cow(aff);
2448 if (!aff)
2449 return NULL;
2451 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2452 if (!aff->ls)
2453 return isl_aff_free(aff);
2455 first += 1 + isl_local_space_offset(aff->ls, type);
2456 aff->v = isl_vec_drop_els(aff->v, first, n);
2457 if (!aff->v)
2458 return isl_aff_free(aff);
2460 return aff;
2463 /* Drop the "n" domain dimensions starting at "first" from "aff",
2464 * after checking that they do not appear in the affine expression.
2466 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2467 unsigned n)
2469 isl_bool involves;
2471 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2472 if (involves < 0)
2473 return isl_aff_free(aff);
2474 if (involves)
2475 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2476 "affine expression involves some of the domain dimensions",
2477 return isl_aff_free(aff));
2478 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2481 /* Project the domain of the affine expression onto its parameter space.
2482 * The affine expression may not involve any of the domain dimensions.
2484 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2486 isl_space *space;
2487 unsigned n;
2489 n = isl_aff_dim(aff, isl_dim_in);
2490 aff = drop_domain(aff, 0, n);
2491 space = isl_aff_get_domain_space(aff);
2492 space = isl_space_params(space);
2493 aff = isl_aff_reset_domain_space(aff, space);
2494 return aff;
2497 /* Check that the domain of "aff" is a product.
2499 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2501 isl_bool is_product;
2503 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2504 if (is_product < 0)
2505 return isl_stat_error;
2506 if (!is_product)
2507 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2508 "domain is not a product", return isl_stat_error);
2509 return isl_stat_ok;
2512 /* Given an affine function with a domain of the form [A -> B] that
2513 * does not depend on B, return the same function on domain A.
2515 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2517 isl_space *space;
2518 int n, n_in;
2520 if (check_domain_product(aff) < 0)
2521 return isl_aff_free(aff);
2522 space = isl_aff_get_domain_space(aff);
2523 n = isl_space_dim(space, isl_dim_set);
2524 space = isl_space_factor_domain(space);
2525 n_in = isl_space_dim(space, isl_dim_set);
2526 aff = drop_domain(aff, n_in, n - n_in);
2527 aff = isl_aff_reset_domain_space(aff, space);
2528 return aff;
2531 /* Convert an affine expression defined over a parameter domain
2532 * into one that is defined over a zero-dimensional set.
2534 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2536 isl_local_space *ls;
2538 ls = isl_aff_take_domain_local_space(aff);
2539 ls = isl_local_space_set_from_params(ls);
2540 aff = isl_aff_restore_domain_local_space(aff, ls);
2542 return aff;
2545 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2546 enum isl_dim_type type, unsigned first, unsigned n)
2548 isl_ctx *ctx;
2550 if (!aff)
2551 return NULL;
2552 if (type == isl_dim_out)
2553 isl_die(aff->v->ctx, isl_error_invalid,
2554 "cannot insert output/set dimensions",
2555 return isl_aff_free(aff));
2556 if (type == isl_dim_in)
2557 type = isl_dim_set;
2558 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2559 return aff;
2561 ctx = isl_aff_get_ctx(aff);
2562 if (first > isl_local_space_dim(aff->ls, type))
2563 isl_die(ctx, isl_error_invalid, "position out of bounds",
2564 return isl_aff_free(aff));
2566 aff = isl_aff_cow(aff);
2567 if (!aff)
2568 return NULL;
2570 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2571 if (!aff->ls)
2572 return isl_aff_free(aff);
2574 first += 1 + isl_local_space_offset(aff->ls, type);
2575 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2576 if (!aff->v)
2577 return isl_aff_free(aff);
2579 return aff;
2582 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2583 enum isl_dim_type type, unsigned n)
2585 unsigned pos;
2587 pos = isl_aff_dim(aff, type);
2589 return isl_aff_insert_dims(aff, type, pos, n);
2592 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2593 enum isl_dim_type type, unsigned n)
2595 unsigned pos;
2597 pos = isl_pw_aff_dim(pwaff, type);
2599 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2602 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2603 * to dimensions of "dst_type" at "dst_pos".
2605 * We only support moving input dimensions to parameters and vice versa.
2607 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2608 enum isl_dim_type dst_type, unsigned dst_pos,
2609 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2611 unsigned g_dst_pos;
2612 unsigned g_src_pos;
2614 if (!aff)
2615 return NULL;
2616 if (n == 0 &&
2617 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2618 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2619 return aff;
2621 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2622 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2623 "cannot move output/set dimension",
2624 return isl_aff_free(aff));
2625 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2626 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2627 "cannot move divs", return isl_aff_free(aff));
2628 if (dst_type == isl_dim_in)
2629 dst_type = isl_dim_set;
2630 if (src_type == isl_dim_in)
2631 src_type = isl_dim_set;
2633 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2634 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2635 "range out of bounds", return isl_aff_free(aff));
2636 if (dst_type == src_type)
2637 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2638 "moving dims within the same type not supported",
2639 return isl_aff_free(aff));
2641 aff = isl_aff_cow(aff);
2642 if (!aff)
2643 return NULL;
2645 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2646 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2647 if (dst_type > src_type)
2648 g_dst_pos -= n;
2650 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2651 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2652 src_type, src_pos, n);
2653 if (!aff->v || !aff->ls)
2654 return isl_aff_free(aff);
2656 aff = sort_divs(aff);
2658 return aff;
2661 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2663 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2664 return isl_pw_aff_alloc(dom, aff);
2667 #define isl_aff_involves_nan isl_aff_is_nan
2669 #undef PW
2670 #define PW isl_pw_aff
2671 #undef EL
2672 #define EL isl_aff
2673 #undef EL_IS_ZERO
2674 #define EL_IS_ZERO is_empty
2675 #undef ZERO
2676 #define ZERO empty
2677 #undef IS_ZERO
2678 #define IS_ZERO is_empty
2679 #undef FIELD
2680 #define FIELD aff
2681 #undef DEFAULT_IS_ZERO
2682 #define DEFAULT_IS_ZERO 0
2684 #define NO_OPT
2685 #define NO_LIFT
2686 #define NO_MORPH
2688 #include <isl_pw_templ.c>
2689 #include <isl_pw_eval.c>
2690 #include <isl_pw_hash.c>
2691 #include <isl_pw_union_opt.c>
2693 #undef UNION
2694 #define UNION isl_union_pw_aff
2695 #undef PART
2696 #define PART isl_pw_aff
2697 #undef PARTS
2698 #define PARTS pw_aff
2700 #include <isl_union_single.c>
2701 #include <isl_union_neg.c>
2703 static __isl_give isl_set *align_params_pw_pw_set_and(
2704 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2705 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2706 __isl_take isl_pw_aff *pwaff2))
2708 isl_bool equal_params;
2710 if (!pwaff1 || !pwaff2)
2711 goto error;
2712 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2713 if (equal_params < 0)
2714 goto error;
2715 if (equal_params)
2716 return fn(pwaff1, pwaff2);
2717 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2718 isl_pw_aff_check_named_params(pwaff2) < 0)
2719 goto error;
2720 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2721 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2722 return fn(pwaff1, pwaff2);
2723 error:
2724 isl_pw_aff_free(pwaff1);
2725 isl_pw_aff_free(pwaff2);
2726 return NULL;
2729 /* Align the parameters of the to isl_pw_aff arguments and
2730 * then apply a function "fn" on them that returns an isl_map.
2732 static __isl_give isl_map *align_params_pw_pw_map_and(
2733 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2734 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2735 __isl_take isl_pw_aff *pa2))
2737 isl_bool equal_params;
2739 if (!pa1 || !pa2)
2740 goto error;
2741 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2742 if (equal_params < 0)
2743 goto error;
2744 if (equal_params)
2745 return fn(pa1, pa2);
2746 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2747 isl_pw_aff_check_named_params(pa2) < 0)
2748 goto error;
2749 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2750 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2751 return fn(pa1, pa2);
2752 error:
2753 isl_pw_aff_free(pa1);
2754 isl_pw_aff_free(pa2);
2755 return NULL;
2758 /* Compute a piecewise quasi-affine expression with a domain that
2759 * is the union of those of pwaff1 and pwaff2 and such that on each
2760 * cell, the quasi-affine expression is the maximum of those of pwaff1
2761 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2762 * cell, then the associated expression is the defined one.
2764 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2765 __isl_take isl_pw_aff *pwaff2)
2767 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2770 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2771 __isl_take isl_pw_aff *pwaff2)
2773 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2774 &pw_aff_union_max);
2777 /* Compute a piecewise quasi-affine expression with a domain that
2778 * is the union of those of pwaff1 and pwaff2 and such that on each
2779 * cell, the quasi-affine expression is the minimum of those of pwaff1
2780 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2781 * cell, then the associated expression is the defined one.
2783 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2784 __isl_take isl_pw_aff *pwaff2)
2786 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2789 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2790 __isl_take isl_pw_aff *pwaff2)
2792 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2793 &pw_aff_union_min);
2796 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2797 __isl_take isl_pw_aff *pwaff2, int max)
2799 if (max)
2800 return isl_pw_aff_union_max(pwaff1, pwaff2);
2801 else
2802 return isl_pw_aff_union_min(pwaff1, pwaff2);
2805 /* Construct a map with as domain the domain of pwaff and
2806 * one-dimensional range corresponding to the affine expressions.
2808 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2810 int i;
2811 isl_space *dim;
2812 isl_map *map;
2814 if (!pwaff)
2815 return NULL;
2817 dim = isl_pw_aff_get_space(pwaff);
2818 map = isl_map_empty(dim);
2820 for (i = 0; i < pwaff->n; ++i) {
2821 isl_basic_map *bmap;
2822 isl_map *map_i;
2824 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2825 map_i = isl_map_from_basic_map(bmap);
2826 map_i = isl_map_intersect_domain(map_i,
2827 isl_set_copy(pwaff->p[i].set));
2828 map = isl_map_union_disjoint(map, map_i);
2831 isl_pw_aff_free(pwaff);
2833 return map;
2836 /* Construct a map with as domain the domain of pwaff and
2837 * one-dimensional range corresponding to the affine expressions.
2839 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2841 if (!pwaff)
2842 return NULL;
2843 if (isl_space_is_set(pwaff->dim))
2844 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2845 "space of input is not a map", goto error);
2846 return map_from_pw_aff(pwaff);
2847 error:
2848 isl_pw_aff_free(pwaff);
2849 return NULL;
2852 /* Construct a one-dimensional set with as parameter domain
2853 * the domain of pwaff and the single set dimension
2854 * corresponding to the affine expressions.
2856 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2858 if (!pwaff)
2859 return NULL;
2860 if (!isl_space_is_set(pwaff->dim))
2861 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2862 "space of input is not a set", goto error);
2863 return map_from_pw_aff(pwaff);
2864 error:
2865 isl_pw_aff_free(pwaff);
2866 return NULL;
2869 /* Return a set containing those elements in the domain
2870 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2871 * does not satisfy "fn" (if complement is 1).
2873 * The pieces with a NaN never belong to the result since
2874 * NaN does not satisfy any property.
2876 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2877 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2878 int complement)
2880 int i;
2881 isl_set *set;
2883 if (!pwaff)
2884 return NULL;
2886 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2888 for (i = 0; i < pwaff->n; ++i) {
2889 isl_basic_set *bset;
2890 isl_set *set_i, *locus;
2891 isl_bool rational;
2893 if (isl_aff_is_nan(pwaff->p[i].aff))
2894 continue;
2896 rational = isl_set_has_rational(pwaff->p[i].set);
2897 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2898 locus = isl_set_from_basic_set(bset);
2899 set_i = isl_set_copy(pwaff->p[i].set);
2900 if (complement)
2901 set_i = isl_set_subtract(set_i, locus);
2902 else
2903 set_i = isl_set_intersect(set_i, locus);
2904 set = isl_set_union_disjoint(set, set_i);
2907 isl_pw_aff_free(pwaff);
2909 return set;
2912 /* Return a set containing those elements in the domain
2913 * of "pa" where it is positive.
2915 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2917 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2920 /* Return a set containing those elements in the domain
2921 * of pwaff where it is non-negative.
2923 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2925 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2928 /* Return a set containing those elements in the domain
2929 * of pwaff where it is zero.
2931 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2933 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2936 /* Return a set containing those elements in the domain
2937 * of pwaff where it is not zero.
2939 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2941 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2944 /* Return a set containing those elements in the shared domain
2945 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2947 * We compute the difference on the shared domain and then construct
2948 * the set of values where this difference is non-negative.
2949 * If strict is set, we first subtract 1 from the difference.
2950 * If equal is set, we only return the elements where pwaff1 and pwaff2
2951 * are equal.
2953 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2954 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2956 isl_set *set1, *set2;
2958 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2959 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2960 set1 = isl_set_intersect(set1, set2);
2961 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2962 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2963 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2965 if (strict) {
2966 isl_space *dim = isl_set_get_space(set1);
2967 isl_aff *aff;
2968 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2969 aff = isl_aff_add_constant_si(aff, -1);
2970 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2971 } else
2972 isl_set_free(set1);
2974 if (equal)
2975 return isl_pw_aff_zero_set(pwaff1);
2976 return isl_pw_aff_nonneg_set(pwaff1);
2979 /* Return a set containing those elements in the shared domain
2980 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2982 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2983 __isl_take isl_pw_aff *pwaff2)
2985 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2988 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2989 __isl_take isl_pw_aff *pwaff2)
2991 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2994 /* Return a set containing those elements in the shared domain
2995 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2997 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2998 __isl_take isl_pw_aff *pwaff2)
3000 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
3003 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
3004 __isl_take isl_pw_aff *pwaff2)
3006 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
3009 /* Return a set containing those elements in the shared domain
3010 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
3012 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3013 __isl_take isl_pw_aff *pwaff2)
3015 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3018 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
3019 __isl_take isl_pw_aff *pwaff2)
3021 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
3024 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3025 __isl_take isl_pw_aff *pwaff2)
3027 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3030 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3031 __isl_take isl_pw_aff *pwaff2)
3033 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3036 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3037 * where the function values are ordered in the same way as "order",
3038 * which returns a set in the shared domain of its two arguments.
3039 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3041 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3042 * We first pull back the two functions such that they are defined on
3043 * the domain [A -> B]. Then we apply "order", resulting in a set
3044 * in the space [A -> B]. Finally, we unwrap this set to obtain
3045 * a map in the space A -> B.
3047 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3048 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3049 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3050 __isl_take isl_pw_aff *pa2))
3052 isl_space *space1, *space2;
3053 isl_multi_aff *ma;
3054 isl_set *set;
3056 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3057 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3058 space1 = isl_space_map_from_domain_and_range(space1, space2);
3059 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3060 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3061 ma = isl_multi_aff_range_map(space1);
3062 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3063 set = order(pa1, pa2);
3065 return isl_set_unwrap(set);
3068 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3069 * where the function values are equal.
3070 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3072 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3073 __isl_take isl_pw_aff *pa2)
3075 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3078 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3079 * where the function values are equal.
3081 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3082 __isl_take isl_pw_aff *pa2)
3084 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3087 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3088 * where the function value of "pa1" is less than the function value of "pa2".
3089 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3091 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3092 __isl_take isl_pw_aff *pa2)
3094 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3097 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3098 * where the function value of "pa1" is less than the function value of "pa2".
3100 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3101 __isl_take isl_pw_aff *pa2)
3103 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3106 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3107 * where the function value of "pa1" is greater than the function value
3108 * of "pa2".
3109 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3111 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3112 __isl_take isl_pw_aff *pa2)
3114 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3117 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3118 * where the function value of "pa1" is greater than the function value
3119 * of "pa2".
3121 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3122 __isl_take isl_pw_aff *pa2)
3124 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3127 /* Return a set containing those elements in the shared domain
3128 * of the elements of list1 and list2 where each element in list1
3129 * has the relation specified by "fn" with each element in list2.
3131 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3132 __isl_take isl_pw_aff_list *list2,
3133 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3134 __isl_take isl_pw_aff *pwaff2))
3136 int i, j;
3137 isl_ctx *ctx;
3138 isl_set *set;
3140 if (!list1 || !list2)
3141 goto error;
3143 ctx = isl_pw_aff_list_get_ctx(list1);
3144 if (list1->n < 1 || list2->n < 1)
3145 isl_die(ctx, isl_error_invalid,
3146 "list should contain at least one element", goto error);
3148 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3149 for (i = 0; i < list1->n; ++i)
3150 for (j = 0; j < list2->n; ++j) {
3151 isl_set *set_ij;
3153 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3154 isl_pw_aff_copy(list2->p[j]));
3155 set = isl_set_intersect(set, set_ij);
3158 isl_pw_aff_list_free(list1);
3159 isl_pw_aff_list_free(list2);
3160 return set;
3161 error:
3162 isl_pw_aff_list_free(list1);
3163 isl_pw_aff_list_free(list2);
3164 return NULL;
3167 /* Return a set containing those elements in the shared domain
3168 * of the elements of list1 and list2 where each element in list1
3169 * is equal to each element in list2.
3171 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3172 __isl_take isl_pw_aff_list *list2)
3174 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3177 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3178 __isl_take isl_pw_aff_list *list2)
3180 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3183 /* Return a set containing those elements in the shared domain
3184 * of the elements of list1 and list2 where each element in list1
3185 * is less than or equal to each element in list2.
3187 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3188 __isl_take isl_pw_aff_list *list2)
3190 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3193 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3194 __isl_take isl_pw_aff_list *list2)
3196 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3199 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3200 __isl_take isl_pw_aff_list *list2)
3202 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3205 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3206 __isl_take isl_pw_aff_list *list2)
3208 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3212 /* Return a set containing those elements in the shared domain
3213 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3215 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3216 __isl_take isl_pw_aff *pwaff2)
3218 isl_set *set_lt, *set_gt;
3220 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3221 isl_pw_aff_copy(pwaff2));
3222 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3223 return isl_set_union_disjoint(set_lt, set_gt);
3226 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3227 __isl_take isl_pw_aff *pwaff2)
3229 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3232 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3233 isl_int v)
3235 int i;
3237 if (isl_int_is_one(v))
3238 return pwaff;
3239 if (!isl_int_is_pos(v))
3240 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3241 "factor needs to be positive",
3242 return isl_pw_aff_free(pwaff));
3243 pwaff = isl_pw_aff_cow(pwaff);
3244 if (!pwaff)
3245 return NULL;
3246 if (pwaff->n == 0)
3247 return pwaff;
3249 for (i = 0; i < pwaff->n; ++i) {
3250 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3251 if (!pwaff->p[i].aff)
3252 return isl_pw_aff_free(pwaff);
3255 return pwaff;
3258 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3260 int i;
3262 pwaff = isl_pw_aff_cow(pwaff);
3263 if (!pwaff)
3264 return NULL;
3265 if (pwaff->n == 0)
3266 return pwaff;
3268 for (i = 0; i < pwaff->n; ++i) {
3269 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3270 if (!pwaff->p[i].aff)
3271 return isl_pw_aff_free(pwaff);
3274 return pwaff;
3277 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3279 int i;
3281 pwaff = isl_pw_aff_cow(pwaff);
3282 if (!pwaff)
3283 return NULL;
3284 if (pwaff->n == 0)
3285 return pwaff;
3287 for (i = 0; i < pwaff->n; ++i) {
3288 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3289 if (!pwaff->p[i].aff)
3290 return isl_pw_aff_free(pwaff);
3293 return pwaff;
3296 /* Assuming that "cond1" and "cond2" are disjoint,
3297 * return an affine expression that is equal to pwaff1 on cond1
3298 * and to pwaff2 on cond2.
3300 static __isl_give isl_pw_aff *isl_pw_aff_select(
3301 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3302 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3304 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3305 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3307 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3310 /* Return an affine expression that is equal to pwaff_true for elements
3311 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3312 * is zero.
3313 * That is, return cond ? pwaff_true : pwaff_false;
3315 * If "cond" involves and NaN, then we conservatively return a NaN
3316 * on its entire domain. In principle, we could consider the pieces
3317 * where it is NaN separately from those where it is not.
3319 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3320 * then only use the domain of "cond" to restrict the domain.
3322 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3323 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3325 isl_set *cond_true, *cond_false;
3326 isl_bool equal;
3328 if (!cond)
3329 goto error;
3330 if (isl_pw_aff_involves_nan(cond)) {
3331 isl_space *space = isl_pw_aff_get_domain_space(cond);
3332 isl_local_space *ls = isl_local_space_from_space(space);
3333 isl_pw_aff_free(cond);
3334 isl_pw_aff_free(pwaff_true);
3335 isl_pw_aff_free(pwaff_false);
3336 return isl_pw_aff_nan_on_domain(ls);
3339 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3340 isl_pw_aff_get_space(pwaff_false));
3341 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3342 isl_pw_aff_get_space(pwaff_true));
3343 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3344 if (equal < 0)
3345 goto error;
3346 if (equal) {
3347 isl_set *dom;
3349 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3350 isl_pw_aff_free(pwaff_false);
3351 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3354 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3355 cond_false = isl_pw_aff_zero_set(cond);
3356 return isl_pw_aff_select(cond_true, pwaff_true,
3357 cond_false, pwaff_false);
3358 error:
3359 isl_pw_aff_free(cond);
3360 isl_pw_aff_free(pwaff_true);
3361 isl_pw_aff_free(pwaff_false);
3362 return NULL;
3365 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3367 if (!aff)
3368 return isl_bool_error;
3370 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3373 /* Check whether pwaff is a piecewise constant.
3375 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3377 int i;
3379 if (!pwaff)
3380 return isl_bool_error;
3382 for (i = 0; i < pwaff->n; ++i) {
3383 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3384 if (is_cst < 0 || !is_cst)
3385 return is_cst;
3388 return isl_bool_true;
3391 /* Are all elements of "mpa" piecewise constants?
3393 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3395 int i;
3397 if (!mpa)
3398 return isl_bool_error;
3400 for (i = 0; i < mpa->n; ++i) {
3401 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3402 if (is_cst < 0 || !is_cst)
3403 return is_cst;
3406 return isl_bool_true;
3409 /* Return the product of "aff1" and "aff2".
3411 * If either of the two is NaN, then the result is NaN.
3413 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3415 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3416 __isl_take isl_aff *aff2)
3418 if (!aff1 || !aff2)
3419 goto error;
3421 if (isl_aff_is_nan(aff1)) {
3422 isl_aff_free(aff2);
3423 return aff1;
3425 if (isl_aff_is_nan(aff2)) {
3426 isl_aff_free(aff1);
3427 return aff2;
3430 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3431 return isl_aff_mul(aff2, aff1);
3433 if (!isl_aff_is_cst(aff2))
3434 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3435 "at least one affine expression should be constant",
3436 goto error);
3438 aff1 = isl_aff_cow(aff1);
3439 if (!aff1 || !aff2)
3440 goto error;
3442 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3443 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3445 isl_aff_free(aff2);
3446 return aff1;
3447 error:
3448 isl_aff_free(aff1);
3449 isl_aff_free(aff2);
3450 return NULL;
3453 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3455 * If either of the two is NaN, then the result is NaN.
3457 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3458 __isl_take isl_aff *aff2)
3460 int is_cst;
3461 int neg;
3463 if (!aff1 || !aff2)
3464 goto error;
3466 if (isl_aff_is_nan(aff1)) {
3467 isl_aff_free(aff2);
3468 return aff1;
3470 if (isl_aff_is_nan(aff2)) {
3471 isl_aff_free(aff1);
3472 return aff2;
3475 is_cst = isl_aff_is_cst(aff2);
3476 if (is_cst < 0)
3477 goto error;
3478 if (!is_cst)
3479 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3480 "second argument should be a constant", goto error);
3482 if (!aff2)
3483 goto error;
3485 neg = isl_int_is_neg(aff2->v->el[1]);
3486 if (neg) {
3487 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3488 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3491 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3492 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3494 if (neg) {
3495 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3496 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3499 isl_aff_free(aff2);
3500 return aff1;
3501 error:
3502 isl_aff_free(aff1);
3503 isl_aff_free(aff2);
3504 return NULL;
3507 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3508 __isl_take isl_pw_aff *pwaff2)
3510 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3513 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3514 __isl_take isl_pw_aff *pwaff2)
3516 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3519 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3520 __isl_take isl_pw_aff *pwaff2)
3522 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3525 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3526 __isl_take isl_pw_aff *pwaff2)
3528 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3531 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3532 __isl_take isl_pw_aff *pwaff2)
3534 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3537 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3538 __isl_take isl_pw_aff *pa2)
3540 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3543 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3545 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3546 __isl_take isl_pw_aff *pa2)
3548 int is_cst;
3550 is_cst = isl_pw_aff_is_cst(pa2);
3551 if (is_cst < 0)
3552 goto error;
3553 if (!is_cst)
3554 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3555 "second argument should be a piecewise constant",
3556 goto error);
3557 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3558 error:
3559 isl_pw_aff_free(pa1);
3560 isl_pw_aff_free(pa2);
3561 return NULL;
3564 /* Compute the quotient of the integer division of "pa1" by "pa2"
3565 * with rounding towards zero.
3566 * "pa2" is assumed to be a piecewise constant.
3568 * In particular, return
3570 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3573 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3574 __isl_take isl_pw_aff *pa2)
3576 int is_cst;
3577 isl_set *cond;
3578 isl_pw_aff *f, *c;
3580 is_cst = isl_pw_aff_is_cst(pa2);
3581 if (is_cst < 0)
3582 goto error;
3583 if (!is_cst)
3584 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3585 "second argument should be a piecewise constant",
3586 goto error);
3588 pa1 = isl_pw_aff_div(pa1, pa2);
3590 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3591 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3592 c = isl_pw_aff_ceil(pa1);
3593 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3594 error:
3595 isl_pw_aff_free(pa1);
3596 isl_pw_aff_free(pa2);
3597 return NULL;
3600 /* Compute the remainder of the integer division of "pa1" by "pa2"
3601 * with rounding towards zero.
3602 * "pa2" is assumed to be a piecewise constant.
3604 * In particular, return
3606 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3609 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3610 __isl_take isl_pw_aff *pa2)
3612 int is_cst;
3613 isl_pw_aff *res;
3615 is_cst = isl_pw_aff_is_cst(pa2);
3616 if (is_cst < 0)
3617 goto error;
3618 if (!is_cst)
3619 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3620 "second argument should be a piecewise constant",
3621 goto error);
3622 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3623 res = isl_pw_aff_mul(pa2, res);
3624 res = isl_pw_aff_sub(pa1, res);
3625 return res;
3626 error:
3627 isl_pw_aff_free(pa1);
3628 isl_pw_aff_free(pa2);
3629 return NULL;
3632 /* Does either of "pa1" or "pa2" involve any NaN2?
3634 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3635 __isl_keep isl_pw_aff *pa2)
3637 isl_bool has_nan;
3639 has_nan = isl_pw_aff_involves_nan(pa1);
3640 if (has_nan < 0 || has_nan)
3641 return has_nan;
3642 return isl_pw_aff_involves_nan(pa2);
3645 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3646 * by a NaN on their shared domain.
3648 * In principle, the result could be refined to only being NaN
3649 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3651 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3652 __isl_take isl_pw_aff *pa2)
3654 isl_local_space *ls;
3655 isl_set *dom;
3656 isl_pw_aff *pa;
3658 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3659 ls = isl_local_space_from_space(isl_set_get_space(dom));
3660 pa = isl_pw_aff_nan_on_domain(ls);
3661 pa = isl_pw_aff_intersect_domain(pa, dom);
3663 return pa;
3666 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3667 __isl_take isl_pw_aff *pwaff2)
3669 isl_set *le;
3670 isl_set *dom;
3672 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3673 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3674 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3675 isl_pw_aff_copy(pwaff2));
3676 dom = isl_set_subtract(dom, isl_set_copy(le));
3677 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3680 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3681 __isl_take isl_pw_aff *pwaff2)
3683 isl_set *ge;
3684 isl_set *dom;
3686 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3687 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3688 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3689 isl_pw_aff_copy(pwaff2));
3690 dom = isl_set_subtract(dom, isl_set_copy(ge));
3691 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3694 /* Return an expression for the minimum (if "max" is not set) or
3695 * the maximum (if "max" is set) of "pa1" and "pa2".
3696 * If either expression involves any NaN, then return a NaN
3697 * on the shared domain as result.
3699 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3700 __isl_take isl_pw_aff *pa2, int max)
3702 isl_bool has_nan;
3704 has_nan = either_involves_nan(pa1, pa2);
3705 if (has_nan < 0)
3706 pa1 = isl_pw_aff_free(pa1);
3707 else if (has_nan)
3708 return replace_by_nan(pa1, pa2);
3710 if (max)
3711 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3712 else
3713 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3716 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3718 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3719 __isl_take isl_pw_aff *pwaff2)
3721 return pw_aff_min_max(pwaff1, pwaff2, 0);
3724 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3726 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3727 __isl_take isl_pw_aff *pwaff2)
3729 return pw_aff_min_max(pwaff1, pwaff2, 1);
3732 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3733 __isl_take isl_pw_aff_list *list,
3734 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3735 __isl_take isl_pw_aff *pwaff2))
3737 int i;
3738 isl_ctx *ctx;
3739 isl_pw_aff *res;
3741 if (!list)
3742 return NULL;
3744 ctx = isl_pw_aff_list_get_ctx(list);
3745 if (list->n < 1)
3746 isl_die(ctx, isl_error_invalid,
3747 "list should contain at least one element", goto error);
3749 res = isl_pw_aff_copy(list->p[0]);
3750 for (i = 1; i < list->n; ++i)
3751 res = fn(res, isl_pw_aff_copy(list->p[i]));
3753 isl_pw_aff_list_free(list);
3754 return res;
3755 error:
3756 isl_pw_aff_list_free(list);
3757 return NULL;
3760 /* Return an isl_pw_aff that maps each element in the intersection of the
3761 * domains of the elements of list to the minimal corresponding affine
3762 * expression.
3764 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3766 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3769 /* Return an isl_pw_aff that maps each element in the intersection of the
3770 * domains of the elements of list to the maximal corresponding affine
3771 * expression.
3773 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3775 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3778 /* Mark the domains of "pwaff" as rational.
3780 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3782 int i;
3784 pwaff = isl_pw_aff_cow(pwaff);
3785 if (!pwaff)
3786 return NULL;
3787 if (pwaff->n == 0)
3788 return pwaff;
3790 for (i = 0; i < pwaff->n; ++i) {
3791 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3792 if (!pwaff->p[i].set)
3793 return isl_pw_aff_free(pwaff);
3796 return pwaff;
3799 /* Mark the domains of the elements of "list" as rational.
3801 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3802 __isl_take isl_pw_aff_list *list)
3804 int i, n;
3806 if (!list)
3807 return NULL;
3808 if (list->n == 0)
3809 return list;
3811 n = list->n;
3812 for (i = 0; i < n; ++i) {
3813 isl_pw_aff *pa;
3815 pa = isl_pw_aff_list_get_pw_aff(list, i);
3816 pa = isl_pw_aff_set_rational(pa);
3817 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3820 return list;
3823 /* Do the parameters of "aff" match those of "space"?
3825 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3826 __isl_keep isl_space *space)
3828 isl_space *aff_space;
3829 isl_bool match;
3831 if (!aff || !space)
3832 return isl_bool_error;
3834 aff_space = isl_aff_get_domain_space(aff);
3836 match = isl_space_has_equal_params(space, aff_space);
3838 isl_space_free(aff_space);
3839 return match;
3842 /* Check that the domain space of "aff" matches "space".
3844 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3845 __isl_keep isl_space *space)
3847 isl_space *aff_space;
3848 isl_bool match;
3850 if (!aff || !space)
3851 return isl_stat_error;
3853 aff_space = isl_aff_get_domain_space(aff);
3855 match = isl_space_has_equal_params(space, aff_space);
3856 if (match < 0)
3857 goto error;
3858 if (!match)
3859 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3860 "parameters don't match", goto error);
3861 match = isl_space_tuple_is_equal(space, isl_dim_in,
3862 aff_space, isl_dim_set);
3863 if (match < 0)
3864 goto error;
3865 if (!match)
3866 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3867 "domains don't match", goto error);
3868 isl_space_free(aff_space);
3869 return isl_stat_ok;
3870 error:
3871 isl_space_free(aff_space);
3872 return isl_stat_error;
3875 #undef BASE
3876 #define BASE aff
3877 #undef DOMBASE
3878 #define DOMBASE set
3879 #define NO_DOMAIN
3881 #include <isl_multi_no_explicit_domain.c>
3882 #include <isl_multi_templ.c>
3883 #include <isl_multi_apply_set.c>
3884 #include <isl_multi_cmp.c>
3885 #include <isl_multi_dims.c>
3886 #include <isl_multi_floor.c>
3887 #include <isl_multi_gist.c>
3889 #undef NO_DOMAIN
3891 /* Construct an isl_multi_aff living in "space" that corresponds
3892 * to the affine transformation matrix "mat".
3894 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3895 __isl_take isl_space *space, __isl_take isl_mat *mat)
3897 isl_ctx *ctx;
3898 isl_local_space *ls = NULL;
3899 isl_multi_aff *ma = NULL;
3900 int n_row, n_col, n_out, total;
3901 int i;
3903 if (!space || !mat)
3904 goto error;
3906 ctx = isl_mat_get_ctx(mat);
3908 n_row = isl_mat_rows(mat);
3909 n_col = isl_mat_cols(mat);
3910 if (n_row < 1)
3911 isl_die(ctx, isl_error_invalid,
3912 "insufficient number of rows", goto error);
3913 if (n_col < 1)
3914 isl_die(ctx, isl_error_invalid,
3915 "insufficient number of columns", goto error);
3916 n_out = isl_space_dim(space, isl_dim_out);
3917 total = isl_space_dim(space, isl_dim_all);
3918 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3919 isl_die(ctx, isl_error_invalid,
3920 "dimension mismatch", goto error);
3922 ma = isl_multi_aff_zero(isl_space_copy(space));
3923 ls = isl_local_space_from_space(isl_space_domain(space));
3925 for (i = 0; i < n_row - 1; ++i) {
3926 isl_vec *v;
3927 isl_aff *aff;
3929 v = isl_vec_alloc(ctx, 1 + n_col);
3930 if (!v)
3931 goto error;
3932 isl_int_set(v->el[0], mat->row[0][0]);
3933 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3934 v = isl_vec_normalize(v);
3935 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3936 ma = isl_multi_aff_set_aff(ma, i, aff);
3939 isl_local_space_free(ls);
3940 isl_mat_free(mat);
3941 return ma;
3942 error:
3943 isl_local_space_free(ls);
3944 isl_mat_free(mat);
3945 isl_multi_aff_free(ma);
3946 return NULL;
3949 /* Remove any internal structure of the domain of "ma".
3950 * If there is any such internal structure in the input,
3951 * then the name of the corresponding space is also removed.
3953 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3954 __isl_take isl_multi_aff *ma)
3956 isl_space *space;
3958 if (!ma)
3959 return NULL;
3961 if (!ma->space->nested[0])
3962 return ma;
3964 space = isl_multi_aff_get_space(ma);
3965 space = isl_space_flatten_domain(space);
3966 ma = isl_multi_aff_reset_space(ma, space);
3968 return ma;
3971 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3972 * of the space to its domain.
3974 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3976 int i, n_in;
3977 isl_local_space *ls;
3978 isl_multi_aff *ma;
3980 if (!space)
3981 return NULL;
3982 if (!isl_space_is_map(space))
3983 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3984 "not a map space", goto error);
3986 n_in = isl_space_dim(space, isl_dim_in);
3987 space = isl_space_domain_map(space);
3989 ma = isl_multi_aff_alloc(isl_space_copy(space));
3990 if (n_in == 0) {
3991 isl_space_free(space);
3992 return ma;
3995 space = isl_space_domain(space);
3996 ls = isl_local_space_from_space(space);
3997 for (i = 0; i < n_in; ++i) {
3998 isl_aff *aff;
4000 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4001 isl_dim_set, i);
4002 ma = isl_multi_aff_set_aff(ma, i, aff);
4004 isl_local_space_free(ls);
4005 return ma;
4006 error:
4007 isl_space_free(space);
4008 return NULL;
4011 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4012 * of the space to its range.
4014 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4016 int i, n_in, n_out;
4017 isl_local_space *ls;
4018 isl_multi_aff *ma;
4020 if (!space)
4021 return NULL;
4022 if (!isl_space_is_map(space))
4023 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4024 "not a map space", goto error);
4026 n_in = isl_space_dim(space, isl_dim_in);
4027 n_out = isl_space_dim(space, isl_dim_out);
4028 space = isl_space_range_map(space);
4030 ma = isl_multi_aff_alloc(isl_space_copy(space));
4031 if (n_out == 0) {
4032 isl_space_free(space);
4033 return ma;
4036 space = isl_space_domain(space);
4037 ls = isl_local_space_from_space(space);
4038 for (i = 0; i < n_out; ++i) {
4039 isl_aff *aff;
4041 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4042 isl_dim_set, n_in + i);
4043 ma = isl_multi_aff_set_aff(ma, i, aff);
4045 isl_local_space_free(ls);
4046 return ma;
4047 error:
4048 isl_space_free(space);
4049 return NULL;
4052 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4053 * of the space to its range.
4055 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4056 __isl_take isl_space *space)
4058 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4061 /* Given the space of a set and a range of set dimensions,
4062 * construct an isl_multi_aff that projects out those dimensions.
4064 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4065 __isl_take isl_space *space, enum isl_dim_type type,
4066 unsigned first, unsigned n)
4068 int i, dim;
4069 isl_local_space *ls;
4070 isl_multi_aff *ma;
4072 if (!space)
4073 return NULL;
4074 if (!isl_space_is_set(space))
4075 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4076 "expecting set space", goto error);
4077 if (type != isl_dim_set)
4078 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4079 "only set dimensions can be projected out", goto error);
4081 dim = isl_space_dim(space, isl_dim_set);
4082 if (first + n > dim)
4083 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4084 "range out of bounds", goto error);
4086 space = isl_space_from_domain(space);
4087 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4089 if (dim == n)
4090 return isl_multi_aff_alloc(space);
4092 ma = isl_multi_aff_alloc(isl_space_copy(space));
4093 space = isl_space_domain(space);
4094 ls = isl_local_space_from_space(space);
4096 for (i = 0; i < first; ++i) {
4097 isl_aff *aff;
4099 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4100 isl_dim_set, i);
4101 ma = isl_multi_aff_set_aff(ma, i, aff);
4104 for (i = 0; i < dim - (first + n); ++i) {
4105 isl_aff *aff;
4107 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4108 isl_dim_set, first + n + i);
4109 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4112 isl_local_space_free(ls);
4113 return ma;
4114 error:
4115 isl_space_free(space);
4116 return NULL;
4119 /* Given the space of a set and a range of set dimensions,
4120 * construct an isl_pw_multi_aff that projects out those dimensions.
4122 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4123 __isl_take isl_space *space, enum isl_dim_type type,
4124 unsigned first, unsigned n)
4126 isl_multi_aff *ma;
4128 ma = isl_multi_aff_project_out_map(space, type, first, n);
4129 return isl_pw_multi_aff_from_multi_aff(ma);
4132 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4133 * domain.
4135 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4136 __isl_take isl_multi_aff *ma)
4138 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4139 return isl_pw_multi_aff_alloc(dom, ma);
4142 /* Create a piecewise multi-affine expression in the given space that maps each
4143 * input dimension to the corresponding output dimension.
4145 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4146 __isl_take isl_space *space)
4148 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4151 /* Exploit the equalities in "eq" to simplify the affine expressions.
4153 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4154 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4156 int i;
4158 maff = isl_multi_aff_cow(maff);
4159 if (!maff || !eq)
4160 goto error;
4162 for (i = 0; i < maff->n; ++i) {
4163 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4164 isl_basic_set_copy(eq));
4165 if (!maff->u.p[i])
4166 goto error;
4169 isl_basic_set_free(eq);
4170 return maff;
4171 error:
4172 isl_basic_set_free(eq);
4173 isl_multi_aff_free(maff);
4174 return NULL;
4177 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4178 isl_int f)
4180 int i;
4182 maff = isl_multi_aff_cow(maff);
4183 if (!maff)
4184 return NULL;
4186 for (i = 0; i < maff->n; ++i) {
4187 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4188 if (!maff->u.p[i])
4189 return isl_multi_aff_free(maff);
4192 return maff;
4195 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4196 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4198 maff1 = isl_multi_aff_add(maff1, maff2);
4199 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4200 return maff1;
4203 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4205 if (!maff)
4206 return -1;
4208 return 0;
4211 /* Return the set of domain elements where "ma1" is lexicographically
4212 * smaller than or equal to "ma2".
4214 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4215 __isl_take isl_multi_aff *ma2)
4217 return isl_multi_aff_lex_ge_set(ma2, ma1);
4220 /* Return the set of domain elements where "ma1" is lexicographically
4221 * smaller than "ma2".
4223 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4224 __isl_take isl_multi_aff *ma2)
4226 return isl_multi_aff_lex_gt_set(ma2, ma1);
4229 /* Return the set of domain elements where "ma1" and "ma2"
4230 * satisfy "order".
4232 static __isl_give isl_set *isl_multi_aff_order_set(
4233 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4234 __isl_give isl_map *order(__isl_take isl_space *set_space))
4236 isl_space *space;
4237 isl_map *map1, *map2;
4238 isl_map *map, *ge;
4240 map1 = isl_map_from_multi_aff(ma1);
4241 map2 = isl_map_from_multi_aff(ma2);
4242 map = isl_map_range_product(map1, map2);
4243 space = isl_space_range(isl_map_get_space(map));
4244 space = isl_space_domain(isl_space_unwrap(space));
4245 ge = order(space);
4246 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4248 return isl_map_domain(map);
4251 /* Return the set of domain elements where "ma1" is lexicographically
4252 * greater than or equal to "ma2".
4254 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4255 __isl_take isl_multi_aff *ma2)
4257 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4260 /* Return the set of domain elements where "ma1" is lexicographically
4261 * greater than "ma2".
4263 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4264 __isl_take isl_multi_aff *ma2)
4266 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4269 #undef PW
4270 #define PW isl_pw_multi_aff
4271 #undef EL
4272 #define EL isl_multi_aff
4273 #undef EL_IS_ZERO
4274 #define EL_IS_ZERO is_empty
4275 #undef ZERO
4276 #define ZERO empty
4277 #undef IS_ZERO
4278 #define IS_ZERO is_empty
4279 #undef FIELD
4280 #define FIELD maff
4281 #undef DEFAULT_IS_ZERO
4282 #define DEFAULT_IS_ZERO 0
4284 #define NO_SUB
4285 #define NO_OPT
4286 #define NO_INSERT_DIMS
4287 #define NO_LIFT
4288 #define NO_MORPH
4290 #include <isl_pw_templ.c>
4291 #include <isl_pw_union_opt.c>
4293 #undef NO_SUB
4295 #undef UNION
4296 #define UNION isl_union_pw_multi_aff
4297 #undef PART
4298 #define PART isl_pw_multi_aff
4299 #undef PARTS
4300 #define PARTS pw_multi_aff
4302 #include <isl_union_multi.c>
4303 #include <isl_union_neg.c>
4305 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4306 __isl_take isl_pw_multi_aff *pma1,
4307 __isl_take isl_pw_multi_aff *pma2)
4309 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4310 &isl_multi_aff_lex_ge_set);
4313 /* Given two piecewise multi affine expressions, return a piecewise
4314 * multi-affine expression defined on the union of the definition domains
4315 * of the inputs that is equal to the lexicographic maximum of the two
4316 * inputs on each cell. If only one of the two inputs is defined on
4317 * a given cell, then it is considered to be the maximum.
4319 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4320 __isl_take isl_pw_multi_aff *pma1,
4321 __isl_take isl_pw_multi_aff *pma2)
4323 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4324 &pw_multi_aff_union_lexmax);
4327 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4328 __isl_take isl_pw_multi_aff *pma1,
4329 __isl_take isl_pw_multi_aff *pma2)
4331 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4332 &isl_multi_aff_lex_le_set);
4335 /* Given two piecewise multi affine expressions, return a piecewise
4336 * multi-affine expression defined on the union of the definition domains
4337 * of the inputs that is equal to the lexicographic minimum of the two
4338 * inputs on each cell. If only one of the two inputs is defined on
4339 * a given cell, then it is considered to be the minimum.
4341 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4342 __isl_take isl_pw_multi_aff *pma1,
4343 __isl_take isl_pw_multi_aff *pma2)
4345 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4346 &pw_multi_aff_union_lexmin);
4349 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4350 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4352 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4353 &isl_multi_aff_add);
4356 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4357 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4359 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4360 &pw_multi_aff_add);
4363 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4364 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4366 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4367 &isl_multi_aff_sub);
4370 /* Subtract "pma2" from "pma1" and return the result.
4372 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4373 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4375 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4376 &pw_multi_aff_sub);
4379 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4380 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4382 return isl_pw_multi_aff_union_add_(pma1, pma2);
4385 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4386 * with the actual sum on the shared domain and
4387 * the defined expression on the symmetric difference of the domains.
4389 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4390 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4392 return isl_union_pw_aff_union_add_(upa1, upa2);
4395 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4396 * with the actual sum on the shared domain and
4397 * the defined expression on the symmetric difference of the domains.
4399 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4400 __isl_take isl_union_pw_multi_aff *upma1,
4401 __isl_take isl_union_pw_multi_aff *upma2)
4403 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4406 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4407 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4409 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4410 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4412 int i, j, n;
4413 isl_space *space;
4414 isl_pw_multi_aff *res;
4416 if (!pma1 || !pma2)
4417 goto error;
4419 n = pma1->n * pma2->n;
4420 space = isl_space_product(isl_space_copy(pma1->dim),
4421 isl_space_copy(pma2->dim));
4422 res = isl_pw_multi_aff_alloc_size(space, n);
4424 for (i = 0; i < pma1->n; ++i) {
4425 for (j = 0; j < pma2->n; ++j) {
4426 isl_set *domain;
4427 isl_multi_aff *ma;
4429 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4430 isl_set_copy(pma2->p[j].set));
4431 ma = isl_multi_aff_product(
4432 isl_multi_aff_copy(pma1->p[i].maff),
4433 isl_multi_aff_copy(pma2->p[j].maff));
4434 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4438 isl_pw_multi_aff_free(pma1);
4439 isl_pw_multi_aff_free(pma2);
4440 return res;
4441 error:
4442 isl_pw_multi_aff_free(pma1);
4443 isl_pw_multi_aff_free(pma2);
4444 return NULL;
4447 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4448 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4450 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4451 &pw_multi_aff_product);
4454 /* Construct a map mapping the domain of the piecewise multi-affine expression
4455 * to its range, with each dimension in the range equated to the
4456 * corresponding affine expression on its cell.
4458 * If the domain of "pma" is rational, then so is the constructed "map".
4460 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4462 int i;
4463 isl_map *map;
4465 if (!pma)
4466 return NULL;
4468 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4470 for (i = 0; i < pma->n; ++i) {
4471 isl_bool rational;
4472 isl_multi_aff *maff;
4473 isl_basic_map *bmap;
4474 isl_map *map_i;
4476 rational = isl_set_is_rational(pma->p[i].set);
4477 if (rational < 0)
4478 map = isl_map_free(map);
4479 maff = isl_multi_aff_copy(pma->p[i].maff);
4480 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4481 map_i = isl_map_from_basic_map(bmap);
4482 map_i = isl_map_intersect_domain(map_i,
4483 isl_set_copy(pma->p[i].set));
4484 map = isl_map_union_disjoint(map, map_i);
4487 isl_pw_multi_aff_free(pma);
4488 return map;
4491 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4493 if (!pma)
4494 return NULL;
4496 if (!isl_space_is_set(pma->dim))
4497 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4498 "isl_pw_multi_aff cannot be converted into an isl_set",
4499 goto error);
4501 return isl_map_from_pw_multi_aff(pma);
4502 error:
4503 isl_pw_multi_aff_free(pma);
4504 return NULL;
4507 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4508 * denominator "denom".
4509 * "denom" is allowed to be negative, in which case the actual denominator
4510 * is -denom and the expressions are added instead.
4512 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4513 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4515 int i, first;
4516 int sign;
4517 isl_int d;
4519 first = isl_seq_first_non_zero(c, n);
4520 if (first == -1)
4521 return aff;
4523 sign = isl_int_sgn(denom);
4524 isl_int_init(d);
4525 isl_int_abs(d, denom);
4526 for (i = first; i < n; ++i) {
4527 isl_aff *aff_i;
4529 if (isl_int_is_zero(c[i]))
4530 continue;
4531 aff_i = isl_multi_aff_get_aff(ma, i);
4532 aff_i = isl_aff_scale(aff_i, c[i]);
4533 aff_i = isl_aff_scale_down(aff_i, d);
4534 if (sign >= 0)
4535 aff = isl_aff_sub(aff, aff_i);
4536 else
4537 aff = isl_aff_add(aff, aff_i);
4539 isl_int_clear(d);
4541 return aff;
4544 /* Extract an affine expression that expresses the output dimension "pos"
4545 * of "bmap" in terms of the parameters and input dimensions from
4546 * equality "eq".
4547 * Note that this expression may involve integer divisions defined
4548 * in terms of parameters and input dimensions.
4549 * The equality may also involve references to earlier (but not later)
4550 * output dimensions. These are replaced by the corresponding elements
4551 * in "ma".
4553 * If the equality is of the form
4555 * f(i) + h(j) + a x + g(i) = 0,
4557 * with f(i) a linear combinations of the parameters and input dimensions,
4558 * g(i) a linear combination of integer divisions defined in terms of the same
4559 * and h(j) a linear combinations of earlier output dimensions,
4560 * then the affine expression is
4562 * (-f(i) - g(i))/a - h(j)/a
4564 * If the equality is of the form
4566 * f(i) + h(j) - a x + g(i) = 0,
4568 * then the affine expression is
4570 * (f(i) + g(i))/a - h(j)/(-a)
4573 * If "div" refers to an integer division (i.e., it is smaller than
4574 * the number of integer divisions), then the equality constraint
4575 * does involve an integer division (the one at position "div") that
4576 * is defined in terms of output dimensions. However, this integer
4577 * division can be eliminated by exploiting a pair of constraints
4578 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4579 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4580 * -l + x >= 0.
4581 * In particular, let
4583 * x = e(i) + m floor(...)
4585 * with e(i) the expression derived above and floor(...) the integer
4586 * division involving output dimensions.
4587 * From
4589 * l <= x <= l + n,
4591 * we have
4593 * 0 <= x - l <= n
4595 * This means
4597 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4598 * = (e(i) - l) mod m
4600 * Therefore,
4602 * x - l = (e(i) - l) mod m
4604 * or
4606 * x = ((e(i) - l) mod m) + l
4608 * The variable "shift" below contains the expression -l, which may
4609 * also involve a linear combination of earlier output dimensions.
4611 static __isl_give isl_aff *extract_aff_from_equality(
4612 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4613 __isl_keep isl_multi_aff *ma)
4615 unsigned o_out;
4616 unsigned n_div, n_out;
4617 isl_ctx *ctx;
4618 isl_local_space *ls;
4619 isl_aff *aff, *shift;
4620 isl_val *mod;
4622 ctx = isl_basic_map_get_ctx(bmap);
4623 ls = isl_basic_map_get_local_space(bmap);
4624 ls = isl_local_space_domain(ls);
4625 aff = isl_aff_alloc(isl_local_space_copy(ls));
4626 if (!aff)
4627 goto error;
4628 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4629 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4630 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4631 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4632 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4633 isl_seq_cpy(aff->v->el + 1 + o_out,
4634 bmap->eq[eq] + o_out + n_out, n_div);
4635 } else {
4636 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4637 isl_seq_neg(aff->v->el + 1 + o_out,
4638 bmap->eq[eq] + o_out + n_out, n_div);
4640 if (div < n_div)
4641 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4642 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4643 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4644 bmap->eq[eq][o_out + pos]);
4645 if (div < n_div) {
4646 shift = isl_aff_alloc(isl_local_space_copy(ls));
4647 if (!shift)
4648 goto error;
4649 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4650 isl_seq_cpy(shift->v->el + 1 + o_out,
4651 bmap->ineq[ineq] + o_out + n_out, n_div);
4652 isl_int_set_si(shift->v->el[0], 1);
4653 shift = subtract_initial(shift, ma, pos,
4654 bmap->ineq[ineq] + o_out, ctx->negone);
4655 aff = isl_aff_add(aff, isl_aff_copy(shift));
4656 mod = isl_val_int_from_isl_int(ctx,
4657 bmap->eq[eq][o_out + n_out + div]);
4658 mod = isl_val_abs(mod);
4659 aff = isl_aff_mod_val(aff, mod);
4660 aff = isl_aff_sub(aff, shift);
4663 isl_local_space_free(ls);
4664 return aff;
4665 error:
4666 isl_local_space_free(ls);
4667 isl_aff_free(aff);
4668 return NULL;
4671 /* Given a basic map with output dimensions defined
4672 * in terms of the parameters input dimensions and earlier
4673 * output dimensions using an equality (and possibly a pair on inequalities),
4674 * extract an isl_aff that expresses output dimension "pos" in terms
4675 * of the parameters and input dimensions.
4676 * Note that this expression may involve integer divisions defined
4677 * in terms of parameters and input dimensions.
4678 * "ma" contains the expressions corresponding to earlier output dimensions.
4680 * This function shares some similarities with
4681 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4683 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4684 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4686 int eq, div, ineq;
4687 isl_aff *aff;
4689 if (!bmap)
4690 return NULL;
4691 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4692 if (eq >= bmap->n_eq)
4693 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4694 "unable to find suitable equality", return NULL);
4695 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4697 aff = isl_aff_remove_unused_divs(aff);
4698 return aff;
4701 /* Given a basic map where each output dimension is defined
4702 * in terms of the parameters and input dimensions using an equality,
4703 * extract an isl_multi_aff that expresses the output dimensions in terms
4704 * of the parameters and input dimensions.
4706 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4707 __isl_take isl_basic_map *bmap)
4709 int i;
4710 unsigned n_out;
4711 isl_multi_aff *ma;
4713 if (!bmap)
4714 return NULL;
4716 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4717 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4719 for (i = 0; i < n_out; ++i) {
4720 isl_aff *aff;
4722 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4723 ma = isl_multi_aff_set_aff(ma, i, aff);
4726 isl_basic_map_free(bmap);
4728 return ma;
4731 /* Given a basic set where each set dimension is defined
4732 * in terms of the parameters using an equality,
4733 * extract an isl_multi_aff that expresses the set dimensions in terms
4734 * of the parameters.
4736 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4737 __isl_take isl_basic_set *bset)
4739 return extract_isl_multi_aff_from_basic_map(bset);
4742 /* Create an isl_pw_multi_aff that is equivalent to
4743 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4744 * The given basic map is such that each output dimension is defined
4745 * in terms of the parameters and input dimensions using an equality.
4747 * Since some applications expect the result of isl_pw_multi_aff_from_map
4748 * to only contain integer affine expressions, we compute the floor
4749 * of the expression before returning.
4751 * Remove all constraints involving local variables without
4752 * an explicit representation (resulting in the removal of those
4753 * local variables) prior to the actual extraction to ensure
4754 * that the local spaces in which the resulting affine expressions
4755 * are created do not contain any unknown local variables.
4756 * Removing such constraints is safe because constraints involving
4757 * unknown local variables are not used to determine whether
4758 * a basic map is obviously single-valued.
4760 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4761 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4763 isl_multi_aff *ma;
4765 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4766 ma = extract_isl_multi_aff_from_basic_map(bmap);
4767 ma = isl_multi_aff_floor(ma);
4768 return isl_pw_multi_aff_alloc(domain, ma);
4771 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4772 * This obviously only works if the input "map" is single-valued.
4773 * If so, we compute the lexicographic minimum of the image in the form
4774 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4775 * to its lexicographic minimum.
4776 * If the input is not single-valued, we produce an error.
4778 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4779 __isl_take isl_map *map)
4781 int i;
4782 int sv;
4783 isl_pw_multi_aff *pma;
4785 sv = isl_map_is_single_valued(map);
4786 if (sv < 0)
4787 goto error;
4788 if (!sv)
4789 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4790 "map is not single-valued", goto error);
4791 map = isl_map_make_disjoint(map);
4792 if (!map)
4793 return NULL;
4795 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4797 for (i = 0; i < map->n; ++i) {
4798 isl_pw_multi_aff *pma_i;
4799 isl_basic_map *bmap;
4800 bmap = isl_basic_map_copy(map->p[i]);
4801 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4802 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4805 isl_map_free(map);
4806 return pma;
4807 error:
4808 isl_map_free(map);
4809 return NULL;
4812 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4813 * taking into account that the output dimension at position "d"
4814 * can be represented as
4816 * x = floor((e(...) + c1) / m)
4818 * given that constraint "i" is of the form
4820 * e(...) + c1 - m x >= 0
4823 * Let "map" be of the form
4825 * A -> B
4827 * We construct a mapping
4829 * A -> [A -> x = floor(...)]
4831 * apply that to the map, obtaining
4833 * [A -> x = floor(...)] -> B
4835 * and equate dimension "d" to x.
4836 * We then compute a isl_pw_multi_aff representation of the resulting map
4837 * and plug in the mapping above.
4839 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4840 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4842 isl_ctx *ctx;
4843 isl_space *space;
4844 isl_local_space *ls;
4845 isl_multi_aff *ma;
4846 isl_aff *aff;
4847 isl_vec *v;
4848 isl_map *insert;
4849 int offset;
4850 int n;
4851 int n_in;
4852 isl_pw_multi_aff *pma;
4853 isl_bool is_set;
4855 is_set = isl_map_is_set(map);
4856 if (is_set < 0)
4857 goto error;
4859 offset = isl_basic_map_offset(hull, isl_dim_out);
4860 ctx = isl_map_get_ctx(map);
4861 space = isl_space_domain(isl_map_get_space(map));
4862 n_in = isl_space_dim(space, isl_dim_set);
4863 n = isl_space_dim(space, isl_dim_all);
4865 v = isl_vec_alloc(ctx, 1 + 1 + n);
4866 if (v) {
4867 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4868 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4870 isl_basic_map_free(hull);
4872 ls = isl_local_space_from_space(isl_space_copy(space));
4873 aff = isl_aff_alloc_vec(ls, v);
4874 aff = isl_aff_floor(aff);
4875 if (is_set) {
4876 isl_space_free(space);
4877 ma = isl_multi_aff_from_aff(aff);
4878 } else {
4879 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4880 ma = isl_multi_aff_range_product(ma,
4881 isl_multi_aff_from_aff(aff));
4884 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4885 map = isl_map_apply_domain(map, insert);
4886 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4887 pma = isl_pw_multi_aff_from_map(map);
4888 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4890 return pma;
4891 error:
4892 isl_map_free(map);
4893 isl_basic_map_free(hull);
4894 return NULL;
4897 /* Is constraint "c" of the form
4899 * e(...) + c1 - m x >= 0
4901 * or
4903 * -e(...) + c2 + m x >= 0
4905 * where m > 1 and e only depends on parameters and input dimemnsions?
4907 * "offset" is the offset of the output dimensions
4908 * "pos" is the position of output dimension x.
4910 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4912 if (isl_int_is_zero(c[offset + d]))
4913 return 0;
4914 if (isl_int_is_one(c[offset + d]))
4915 return 0;
4916 if (isl_int_is_negone(c[offset + d]))
4917 return 0;
4918 if (isl_seq_first_non_zero(c + offset, d) != -1)
4919 return 0;
4920 if (isl_seq_first_non_zero(c + offset + d + 1,
4921 total - (offset + d + 1)) != -1)
4922 return 0;
4923 return 1;
4926 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4928 * As a special case, we first check if there is any pair of constraints,
4929 * shared by all the basic maps in "map" that force a given dimension
4930 * to be equal to the floor of some affine combination of the input dimensions.
4932 * In particular, if we can find two constraints
4934 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4936 * and
4938 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4940 * where m > 1 and e only depends on parameters and input dimemnsions,
4941 * and such that
4943 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4945 * then we know that we can take
4947 * x = floor((e(...) + c1) / m)
4949 * without having to perform any computation.
4951 * Note that we know that
4953 * c1 + c2 >= 1
4955 * If c1 + c2 were 0, then we would have detected an equality during
4956 * simplification. If c1 + c2 were negative, then we would have detected
4957 * a contradiction.
4959 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4960 __isl_take isl_map *map)
4962 int d, dim;
4963 int i, j, n;
4964 int offset, total;
4965 isl_int sum;
4966 isl_basic_map *hull;
4968 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4969 if (!hull)
4970 goto error;
4972 isl_int_init(sum);
4973 dim = isl_map_dim(map, isl_dim_out);
4974 offset = isl_basic_map_offset(hull, isl_dim_out);
4975 total = 1 + isl_basic_map_total_dim(hull);
4976 n = hull->n_ineq;
4977 for (d = 0; d < dim; ++d) {
4978 for (i = 0; i < n; ++i) {
4979 if (!is_potential_div_constraint(hull->ineq[i],
4980 offset, d, total))
4981 continue;
4982 for (j = i + 1; j < n; ++j) {
4983 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4984 hull->ineq[j] + 1, total - 1))
4985 continue;
4986 isl_int_add(sum, hull->ineq[i][0],
4987 hull->ineq[j][0]);
4988 if (isl_int_abs_lt(sum,
4989 hull->ineq[i][offset + d]))
4990 break;
4993 if (j >= n)
4994 continue;
4995 isl_int_clear(sum);
4996 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4997 j = i;
4998 return pw_multi_aff_from_map_div(map, hull, d, j);
5001 isl_int_clear(sum);
5002 isl_basic_map_free(hull);
5003 return pw_multi_aff_from_map_base(map);
5004 error:
5005 isl_map_free(map);
5006 isl_basic_map_free(hull);
5007 return NULL;
5010 /* Given an affine expression
5012 * [A -> B] -> f(A,B)
5014 * construct an isl_multi_aff
5016 * [A -> B] -> B'
5018 * such that dimension "d" in B' is set to "aff" and the remaining
5019 * dimensions are set equal to the corresponding dimensions in B.
5020 * "n_in" is the dimension of the space A.
5021 * "n_out" is the dimension of the space B.
5023 * If "is_set" is set, then the affine expression is of the form
5025 * [B] -> f(B)
5027 * and we construct an isl_multi_aff
5029 * B -> B'
5031 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5032 unsigned n_in, unsigned n_out, int is_set)
5034 int i;
5035 isl_multi_aff *ma;
5036 isl_space *space, *space2;
5037 isl_local_space *ls;
5039 space = isl_aff_get_domain_space(aff);
5040 ls = isl_local_space_from_space(isl_space_copy(space));
5041 space2 = isl_space_copy(space);
5042 if (!is_set)
5043 space2 = isl_space_range(isl_space_unwrap(space2));
5044 space = isl_space_map_from_domain_and_range(space, space2);
5045 ma = isl_multi_aff_alloc(space);
5046 ma = isl_multi_aff_set_aff(ma, d, aff);
5048 for (i = 0; i < n_out; ++i) {
5049 if (i == d)
5050 continue;
5051 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5052 isl_dim_set, n_in + i);
5053 ma = isl_multi_aff_set_aff(ma, i, aff);
5056 isl_local_space_free(ls);
5058 return ma;
5061 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5062 * taking into account that the dimension at position "d" can be written as
5064 * x = m a + f(..) (1)
5066 * where m is equal to "gcd".
5067 * "i" is the index of the equality in "hull" that defines f(..).
5068 * In particular, the equality is of the form
5070 * f(..) - x + m g(existentials) = 0
5072 * or
5074 * -f(..) + x + m g(existentials) = 0
5076 * We basically plug (1) into "map", resulting in a map with "a"
5077 * in the range instead of "x". The corresponding isl_pw_multi_aff
5078 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5080 * Specifically, given the input map
5082 * A -> B
5084 * We first wrap it into a set
5086 * [A -> B]
5088 * and define (1) on top of the corresponding space, resulting in "aff".
5089 * We use this to create an isl_multi_aff that maps the output position "d"
5090 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5091 * We plug this into the wrapped map, unwrap the result and compute the
5092 * corresponding isl_pw_multi_aff.
5093 * The result is an expression
5095 * A -> T(A)
5097 * We adjust that to
5099 * A -> [A -> T(A)]
5101 * so that we can plug that into "aff", after extending the latter to
5102 * a mapping
5104 * [A -> B] -> B'
5107 * If "map" is actually a set, then there is no "A" space, meaning
5108 * that we do not need to perform any wrapping, and that the result
5109 * of the recursive call is of the form
5111 * [T]
5113 * which is plugged into a mapping of the form
5115 * B -> B'
5117 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5118 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5119 isl_int gcd)
5121 isl_set *set;
5122 isl_space *space;
5123 isl_local_space *ls;
5124 isl_aff *aff;
5125 isl_multi_aff *ma;
5126 isl_pw_multi_aff *pma, *id;
5127 unsigned n_in;
5128 unsigned o_out;
5129 unsigned n_out;
5130 isl_bool is_set;
5132 is_set = isl_map_is_set(map);
5133 if (is_set < 0)
5134 goto error;
5136 n_in = isl_basic_map_dim(hull, isl_dim_in);
5137 n_out = isl_basic_map_dim(hull, isl_dim_out);
5138 o_out = isl_basic_map_offset(hull, isl_dim_out);
5140 if (is_set)
5141 set = map;
5142 else
5143 set = isl_map_wrap(map);
5144 space = isl_space_map_from_set(isl_set_get_space(set));
5145 ma = isl_multi_aff_identity(space);
5146 ls = isl_local_space_from_space(isl_set_get_space(set));
5147 aff = isl_aff_alloc(ls);
5148 if (aff) {
5149 isl_int_set_si(aff->v->el[0], 1);
5150 if (isl_int_is_one(hull->eq[i][o_out + d]))
5151 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5152 aff->v->size - 1);
5153 else
5154 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5155 aff->v->size - 1);
5156 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5158 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5159 set = isl_set_preimage_multi_aff(set, ma);
5161 ma = range_map(aff, d, n_in, n_out, is_set);
5163 if (is_set)
5164 map = set;
5165 else
5166 map = isl_set_unwrap(set);
5167 pma = isl_pw_multi_aff_from_map(map);
5169 if (!is_set) {
5170 space = isl_pw_multi_aff_get_domain_space(pma);
5171 space = isl_space_map_from_set(space);
5172 id = isl_pw_multi_aff_identity(space);
5173 pma = isl_pw_multi_aff_range_product(id, pma);
5175 id = isl_pw_multi_aff_from_multi_aff(ma);
5176 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5178 isl_basic_map_free(hull);
5179 return pma;
5180 error:
5181 isl_map_free(map);
5182 isl_basic_map_free(hull);
5183 return NULL;
5186 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5187 * "hull" contains the equalities valid for "map".
5189 * Check if any of the output dimensions is "strided".
5190 * That is, we check if it can be written as
5192 * x = m a + f(..)
5194 * with m greater than 1, a some combination of existentially quantified
5195 * variables and f an expression in the parameters and input dimensions.
5196 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5198 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5199 * special case.
5201 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5202 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5204 int i, j;
5205 unsigned n_out;
5206 unsigned o_out;
5207 unsigned n_div;
5208 unsigned o_div;
5209 isl_int gcd;
5211 n_div = isl_basic_map_dim(hull, isl_dim_div);
5212 o_div = isl_basic_map_offset(hull, isl_dim_div);
5214 if (n_div == 0) {
5215 isl_basic_map_free(hull);
5216 return pw_multi_aff_from_map_check_div(map);
5219 isl_int_init(gcd);
5221 n_out = isl_basic_map_dim(hull, isl_dim_out);
5222 o_out = isl_basic_map_offset(hull, isl_dim_out);
5224 for (i = 0; i < n_out; ++i) {
5225 for (j = 0; j < hull->n_eq; ++j) {
5226 isl_int *eq = hull->eq[j];
5227 isl_pw_multi_aff *res;
5229 if (!isl_int_is_one(eq[o_out + i]) &&
5230 !isl_int_is_negone(eq[o_out + i]))
5231 continue;
5232 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5233 continue;
5234 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5235 n_out - (i + 1)) != -1)
5236 continue;
5237 isl_seq_gcd(eq + o_div, n_div, &gcd);
5238 if (isl_int_is_zero(gcd))
5239 continue;
5240 if (isl_int_is_one(gcd))
5241 continue;
5243 res = pw_multi_aff_from_map_stride(map, hull,
5244 i, j, gcd);
5245 isl_int_clear(gcd);
5246 return res;
5250 isl_int_clear(gcd);
5251 isl_basic_map_free(hull);
5252 return pw_multi_aff_from_map_check_div(map);
5255 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5257 * As a special case, we first check if all output dimensions are uniquely
5258 * defined in terms of the parameters and input dimensions over the entire
5259 * domain. If so, we extract the desired isl_pw_multi_aff directly
5260 * from the affine hull of "map" and its domain.
5262 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5263 * special cases.
5265 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5267 isl_bool sv;
5268 isl_basic_map *hull;
5270 if (!map)
5271 return NULL;
5273 if (isl_map_n_basic_map(map) == 1) {
5274 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5275 hull = isl_basic_map_plain_affine_hull(hull);
5276 sv = isl_basic_map_plain_is_single_valued(hull);
5277 if (sv >= 0 && sv)
5278 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5279 hull);
5280 isl_basic_map_free(hull);
5282 map = isl_map_detect_equalities(map);
5283 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5284 sv = isl_basic_map_plain_is_single_valued(hull);
5285 if (sv >= 0 && sv)
5286 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5287 if (sv >= 0)
5288 return pw_multi_aff_from_map_check_strides(map, hull);
5289 isl_basic_map_free(hull);
5290 isl_map_free(map);
5291 return NULL;
5294 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5296 return isl_pw_multi_aff_from_map(set);
5299 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5300 * add it to *user.
5302 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5304 isl_union_pw_multi_aff **upma = user;
5305 isl_pw_multi_aff *pma;
5307 pma = isl_pw_multi_aff_from_map(map);
5308 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5310 return *upma ? isl_stat_ok : isl_stat_error;
5313 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5314 * domain.
5316 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5317 __isl_take isl_aff *aff)
5319 isl_multi_aff *ma;
5320 isl_pw_multi_aff *pma;
5322 ma = isl_multi_aff_from_aff(aff);
5323 pma = isl_pw_multi_aff_from_multi_aff(ma);
5324 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5327 /* Try and create an isl_union_pw_multi_aff that is equivalent
5328 * to the given isl_union_map.
5329 * The isl_union_map is required to be single-valued in each space.
5330 * Otherwise, an error is produced.
5332 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5333 __isl_take isl_union_map *umap)
5335 isl_space *space;
5336 isl_union_pw_multi_aff *upma;
5338 space = isl_union_map_get_space(umap);
5339 upma = isl_union_pw_multi_aff_empty(space);
5340 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5341 upma = isl_union_pw_multi_aff_free(upma);
5342 isl_union_map_free(umap);
5344 return upma;
5347 /* Try and create an isl_union_pw_multi_aff that is equivalent
5348 * to the given isl_union_set.
5349 * The isl_union_set is required to be a singleton in each space.
5350 * Otherwise, an error is produced.
5352 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5353 __isl_take isl_union_set *uset)
5355 return isl_union_pw_multi_aff_from_union_map(uset);
5358 /* Return the piecewise affine expression "set ? 1 : 0".
5360 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5362 isl_pw_aff *pa;
5363 isl_space *space = isl_set_get_space(set);
5364 isl_local_space *ls = isl_local_space_from_space(space);
5365 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5366 isl_aff *one = isl_aff_zero_on_domain(ls);
5368 one = isl_aff_add_constant_si(one, 1);
5369 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5370 set = isl_set_complement(set);
5371 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5373 return pa;
5376 /* Plug in "subs" for dimension "type", "pos" of "aff".
5378 * Let i be the dimension to replace and let "subs" be of the form
5380 * f/d
5382 * and "aff" of the form
5384 * (a i + g)/m
5386 * The result is
5388 * (a f + d g')/(m d)
5390 * where g' is the result of plugging in "subs" in each of the integer
5391 * divisions in g.
5393 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5394 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5396 isl_ctx *ctx;
5397 isl_int v;
5399 aff = isl_aff_cow(aff);
5400 if (!aff || !subs)
5401 return isl_aff_free(aff);
5403 ctx = isl_aff_get_ctx(aff);
5404 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5405 isl_die(ctx, isl_error_invalid,
5406 "spaces don't match", return isl_aff_free(aff));
5407 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5408 isl_die(ctx, isl_error_unsupported,
5409 "cannot handle divs yet", return isl_aff_free(aff));
5411 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5412 if (!aff->ls)
5413 return isl_aff_free(aff);
5415 aff->v = isl_vec_cow(aff->v);
5416 if (!aff->v)
5417 return isl_aff_free(aff);
5419 pos += isl_local_space_offset(aff->ls, type);
5421 isl_int_init(v);
5422 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5423 aff->v->size, subs->v->size, v);
5424 isl_int_clear(v);
5426 return aff;
5429 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5430 * expressions in "maff".
5432 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5433 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5434 __isl_keep isl_aff *subs)
5436 int i;
5438 maff = isl_multi_aff_cow(maff);
5439 if (!maff || !subs)
5440 return isl_multi_aff_free(maff);
5442 if (type == isl_dim_in)
5443 type = isl_dim_set;
5445 for (i = 0; i < maff->n; ++i) {
5446 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5447 type, pos, subs);
5448 if (!maff->u.p[i])
5449 return isl_multi_aff_free(maff);
5452 return maff;
5455 /* Plug in "subs" for dimension "type", "pos" of "pma".
5457 * pma is of the form
5459 * A_i(v) -> M_i(v)
5461 * while subs is of the form
5463 * v' = B_j(v) -> S_j
5465 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5466 * has a contribution in the result, in particular
5468 * C_ij(S_j) -> M_i(S_j)
5470 * Note that plugging in S_j in C_ij may also result in an empty set
5471 * and this contribution should simply be discarded.
5473 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5474 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5475 __isl_keep isl_pw_aff *subs)
5477 int i, j, n;
5478 isl_pw_multi_aff *res;
5480 if (!pma || !subs)
5481 return isl_pw_multi_aff_free(pma);
5483 n = pma->n * subs->n;
5484 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5486 for (i = 0; i < pma->n; ++i) {
5487 for (j = 0; j < subs->n; ++j) {
5488 isl_set *common;
5489 isl_multi_aff *res_ij;
5490 int empty;
5492 common = isl_set_intersect(
5493 isl_set_copy(pma->p[i].set),
5494 isl_set_copy(subs->p[j].set));
5495 common = isl_set_substitute(common,
5496 type, pos, subs->p[j].aff);
5497 empty = isl_set_plain_is_empty(common);
5498 if (empty < 0 || empty) {
5499 isl_set_free(common);
5500 if (empty < 0)
5501 goto error;
5502 continue;
5505 res_ij = isl_multi_aff_substitute(
5506 isl_multi_aff_copy(pma->p[i].maff),
5507 type, pos, subs->p[j].aff);
5509 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5513 isl_pw_multi_aff_free(pma);
5514 return res;
5515 error:
5516 isl_pw_multi_aff_free(pma);
5517 isl_pw_multi_aff_free(res);
5518 return NULL;
5521 /* Compute the preimage of a range of dimensions in the affine expression "src"
5522 * under "ma" and put the result in "dst". The number of dimensions in "src"
5523 * that precede the range is given by "n_before". The number of dimensions
5524 * in the range is given by the number of output dimensions of "ma".
5525 * The number of dimensions that follow the range is given by "n_after".
5526 * If "has_denom" is set (to one),
5527 * then "src" and "dst" have an extra initial denominator.
5528 * "n_div_ma" is the number of existentials in "ma"
5529 * "n_div_bset" is the number of existentials in "src"
5530 * The resulting "dst" (which is assumed to have been allocated by
5531 * the caller) contains coefficients for both sets of existentials,
5532 * first those in "ma" and then those in "src".
5533 * f, c1, c2 and g are temporary objects that have been initialized
5534 * by the caller.
5536 * Let src represent the expression
5538 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5540 * and let ma represent the expressions
5542 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5544 * We start out with the following expression for dst:
5546 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5548 * with the multiplication factor f initially equal to 1
5549 * and f \sum_i b_i v_i kept separately.
5550 * For each x_i that we substitute, we multiply the numerator
5551 * (and denominator) of dst by c_1 = m_i and add the numerator
5552 * of the x_i expression multiplied by c_2 = f b_i,
5553 * after removing the common factors of c_1 and c_2.
5554 * The multiplication factor f also needs to be multiplied by c_1
5555 * for the next x_j, j > i.
5557 void isl_seq_preimage(isl_int *dst, isl_int *src,
5558 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5559 int n_div_ma, int n_div_bmap,
5560 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5562 int i;
5563 int n_param, n_in, n_out;
5564 int o_dst, o_src;
5566 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5567 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5568 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5570 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5571 o_dst = o_src = has_denom + 1 + n_param + n_before;
5572 isl_seq_clr(dst + o_dst, n_in);
5573 o_dst += n_in;
5574 o_src += n_out;
5575 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5576 o_dst += n_after;
5577 o_src += n_after;
5578 isl_seq_clr(dst + o_dst, n_div_ma);
5579 o_dst += n_div_ma;
5580 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5582 isl_int_set_si(f, 1);
5584 for (i = 0; i < n_out; ++i) {
5585 int offset = has_denom + 1 + n_param + n_before + i;
5587 if (isl_int_is_zero(src[offset]))
5588 continue;
5589 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5590 isl_int_mul(c2, f, src[offset]);
5591 isl_int_gcd(g, c1, c2);
5592 isl_int_divexact(c1, c1, g);
5593 isl_int_divexact(c2, c2, g);
5595 isl_int_mul(f, f, c1);
5596 o_dst = has_denom;
5597 o_src = 1;
5598 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5599 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5600 o_dst += 1 + n_param;
5601 o_src += 1 + n_param;
5602 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5603 o_dst += n_before;
5604 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5605 c2, ma->u.p[i]->v->el + o_src, n_in);
5606 o_dst += n_in;
5607 o_src += n_in;
5608 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5609 o_dst += n_after;
5610 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5611 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5612 o_dst += n_div_ma;
5613 o_src += n_div_ma;
5614 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5615 if (has_denom)
5616 isl_int_mul(dst[0], dst[0], c1);
5620 /* Compute the pullback of "aff" by the function represented by "ma".
5621 * In other words, plug in "ma" in "aff". The result is an affine expression
5622 * defined over the domain space of "ma".
5624 * If "aff" is represented by
5626 * (a(p) + b x + c(divs))/d
5628 * and ma is represented by
5630 * x = D(p) + F(y) + G(divs')
5632 * then the result is
5634 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5636 * The divs in the local space of the input are similarly adjusted
5637 * through a call to isl_local_space_preimage_multi_aff.
5639 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5640 __isl_take isl_multi_aff *ma)
5642 isl_aff *res = NULL;
5643 isl_local_space *ls;
5644 int n_div_aff, n_div_ma;
5645 isl_int f, c1, c2, g;
5647 ma = isl_multi_aff_align_divs(ma);
5648 if (!aff || !ma)
5649 goto error;
5651 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5652 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5654 ls = isl_aff_get_domain_local_space(aff);
5655 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5656 res = isl_aff_alloc(ls);
5657 if (!res)
5658 goto error;
5660 isl_int_init(f);
5661 isl_int_init(c1);
5662 isl_int_init(c2);
5663 isl_int_init(g);
5665 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5666 f, c1, c2, g, 1);
5668 isl_int_clear(f);
5669 isl_int_clear(c1);
5670 isl_int_clear(c2);
5671 isl_int_clear(g);
5673 isl_aff_free(aff);
5674 isl_multi_aff_free(ma);
5675 res = isl_aff_normalize(res);
5676 return res;
5677 error:
5678 isl_aff_free(aff);
5679 isl_multi_aff_free(ma);
5680 isl_aff_free(res);
5681 return NULL;
5684 /* Compute the pullback of "aff1" by the function represented by "aff2".
5685 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5686 * defined over the domain space of "aff1".
5688 * The domain of "aff1" should match the range of "aff2", which means
5689 * that it should be single-dimensional.
5691 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5692 __isl_take isl_aff *aff2)
5694 isl_multi_aff *ma;
5696 ma = isl_multi_aff_from_aff(aff2);
5697 return isl_aff_pullback_multi_aff(aff1, ma);
5700 /* Compute the pullback of "ma1" by the function represented by "ma2".
5701 * In other words, plug in "ma2" in "ma1".
5703 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5705 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5706 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5708 int i;
5709 isl_space *space = NULL;
5711 ma2 = isl_multi_aff_align_divs(ma2);
5712 ma1 = isl_multi_aff_cow(ma1);
5713 if (!ma1 || !ma2)
5714 goto error;
5716 space = isl_space_join(isl_multi_aff_get_space(ma2),
5717 isl_multi_aff_get_space(ma1));
5719 for (i = 0; i < ma1->n; ++i) {
5720 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5721 isl_multi_aff_copy(ma2));
5722 if (!ma1->u.p[i])
5723 goto error;
5726 ma1 = isl_multi_aff_reset_space(ma1, space);
5727 isl_multi_aff_free(ma2);
5728 return ma1;
5729 error:
5730 isl_space_free(space);
5731 isl_multi_aff_free(ma2);
5732 isl_multi_aff_free(ma1);
5733 return NULL;
5736 /* Compute the pullback of "ma1" by the function represented by "ma2".
5737 * In other words, plug in "ma2" in "ma1".
5739 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5740 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5742 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5743 &isl_multi_aff_pullback_multi_aff_aligned);
5746 /* Extend the local space of "dst" to include the divs
5747 * in the local space of "src".
5749 * If "src" does not have any divs or if the local spaces of "dst" and
5750 * "src" are the same, then no extension is required.
5752 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5753 __isl_keep isl_aff *src)
5755 isl_ctx *ctx;
5756 int src_n_div, dst_n_div;
5757 int *exp1 = NULL;
5758 int *exp2 = NULL;
5759 isl_bool equal;
5760 isl_mat *div;
5762 if (!src || !dst)
5763 return isl_aff_free(dst);
5765 ctx = isl_aff_get_ctx(src);
5766 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5767 if (equal < 0)
5768 return isl_aff_free(dst);
5769 if (!equal)
5770 isl_die(ctx, isl_error_invalid,
5771 "spaces don't match", goto error);
5773 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5774 if (src_n_div == 0)
5775 return dst;
5776 equal = isl_local_space_is_equal(src->ls, dst->ls);
5777 if (equal < 0)
5778 return isl_aff_free(dst);
5779 if (equal)
5780 return dst;
5782 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5783 exp1 = isl_alloc_array(ctx, int, src_n_div);
5784 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5785 if (!exp1 || (dst_n_div && !exp2))
5786 goto error;
5788 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5789 dst = isl_aff_expand_divs(dst, div, exp2);
5790 free(exp1);
5791 free(exp2);
5793 return dst;
5794 error:
5795 free(exp1);
5796 free(exp2);
5797 return isl_aff_free(dst);
5800 /* Adjust the local spaces of the affine expressions in "maff"
5801 * such that they all have the save divs.
5803 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5804 __isl_take isl_multi_aff *maff)
5806 int i;
5808 if (!maff)
5809 return NULL;
5810 if (maff->n == 0)
5811 return maff;
5812 maff = isl_multi_aff_cow(maff);
5813 if (!maff)
5814 return NULL;
5816 for (i = 1; i < maff->n; ++i)
5817 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5818 for (i = 1; i < maff->n; ++i) {
5819 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5820 if (!maff->u.p[i])
5821 return isl_multi_aff_free(maff);
5824 return maff;
5827 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5829 aff = isl_aff_cow(aff);
5830 if (!aff)
5831 return NULL;
5833 aff->ls = isl_local_space_lift(aff->ls);
5834 if (!aff->ls)
5835 return isl_aff_free(aff);
5837 return aff;
5840 /* Lift "maff" to a space with extra dimensions such that the result
5841 * has no more existentially quantified variables.
5842 * If "ls" is not NULL, then *ls is assigned the local space that lies
5843 * at the basis of the lifting applied to "maff".
5845 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5846 __isl_give isl_local_space **ls)
5848 int i;
5849 isl_space *space;
5850 unsigned n_div;
5852 if (ls)
5853 *ls = NULL;
5855 if (!maff)
5856 return NULL;
5858 if (maff->n == 0) {
5859 if (ls) {
5860 isl_space *space = isl_multi_aff_get_domain_space(maff);
5861 *ls = isl_local_space_from_space(space);
5862 if (!*ls)
5863 return isl_multi_aff_free(maff);
5865 return maff;
5868 maff = isl_multi_aff_cow(maff);
5869 maff = isl_multi_aff_align_divs(maff);
5870 if (!maff)
5871 return NULL;
5873 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5874 space = isl_multi_aff_get_space(maff);
5875 space = isl_space_lift(isl_space_domain(space), n_div);
5876 space = isl_space_extend_domain_with_range(space,
5877 isl_multi_aff_get_space(maff));
5878 if (!space)
5879 return isl_multi_aff_free(maff);
5880 isl_space_free(maff->space);
5881 maff->space = space;
5883 if (ls) {
5884 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5885 if (!*ls)
5886 return isl_multi_aff_free(maff);
5889 for (i = 0; i < maff->n; ++i) {
5890 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5891 if (!maff->u.p[i])
5892 goto error;
5895 return maff;
5896 error:
5897 if (ls)
5898 isl_local_space_free(*ls);
5899 return isl_multi_aff_free(maff);
5903 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5905 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5906 __isl_keep isl_pw_multi_aff *pma, int pos)
5908 int i;
5909 int n_out;
5910 isl_space *space;
5911 isl_pw_aff *pa;
5913 if (!pma)
5914 return NULL;
5916 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5917 if (pos < 0 || pos >= n_out)
5918 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5919 "index out of bounds", return NULL);
5921 space = isl_pw_multi_aff_get_space(pma);
5922 space = isl_space_drop_dims(space, isl_dim_out,
5923 pos + 1, n_out - pos - 1);
5924 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5926 pa = isl_pw_aff_alloc_size(space, pma->n);
5927 for (i = 0; i < pma->n; ++i) {
5928 isl_aff *aff;
5929 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5930 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5933 return pa;
5936 /* Return an isl_pw_multi_aff with the given "set" as domain and
5937 * an unnamed zero-dimensional range.
5939 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5940 __isl_take isl_set *set)
5942 isl_multi_aff *ma;
5943 isl_space *space;
5945 space = isl_set_get_space(set);
5946 space = isl_space_from_domain(space);
5947 ma = isl_multi_aff_zero(space);
5948 return isl_pw_multi_aff_alloc(set, ma);
5951 /* Add an isl_pw_multi_aff with the given "set" as domain and
5952 * an unnamed zero-dimensional range to *user.
5954 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5955 void *user)
5957 isl_union_pw_multi_aff **upma = user;
5958 isl_pw_multi_aff *pma;
5960 pma = isl_pw_multi_aff_from_domain(set);
5961 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5963 return isl_stat_ok;
5966 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5967 * an unnamed zero-dimensional range.
5969 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5970 __isl_take isl_union_set *uset)
5972 isl_space *space;
5973 isl_union_pw_multi_aff *upma;
5975 if (!uset)
5976 return NULL;
5978 space = isl_union_set_get_space(uset);
5979 upma = isl_union_pw_multi_aff_empty(space);
5981 if (isl_union_set_foreach_set(uset,
5982 &add_pw_multi_aff_from_domain, &upma) < 0)
5983 goto error;
5985 isl_union_set_free(uset);
5986 return upma;
5987 error:
5988 isl_union_set_free(uset);
5989 isl_union_pw_multi_aff_free(upma);
5990 return NULL;
5993 /* Convert "pma" to an isl_map and add it to *umap.
5995 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5996 void *user)
5998 isl_union_map **umap = user;
5999 isl_map *map;
6001 map = isl_map_from_pw_multi_aff(pma);
6002 *umap = isl_union_map_add_map(*umap, map);
6004 return isl_stat_ok;
6007 /* Construct a union map mapping the domain of the union
6008 * piecewise multi-affine expression to its range, with each dimension
6009 * in the range equated to the corresponding affine expression on its cell.
6011 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
6012 __isl_take isl_union_pw_multi_aff *upma)
6014 isl_space *space;
6015 isl_union_map *umap;
6017 if (!upma)
6018 return NULL;
6020 space = isl_union_pw_multi_aff_get_space(upma);
6021 umap = isl_union_map_empty(space);
6023 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
6024 &map_from_pw_multi_aff, &umap) < 0)
6025 goto error;
6027 isl_union_pw_multi_aff_free(upma);
6028 return umap;
6029 error:
6030 isl_union_pw_multi_aff_free(upma);
6031 isl_union_map_free(umap);
6032 return NULL;
6035 /* Local data for bin_entry and the callback "fn".
6037 struct isl_union_pw_multi_aff_bin_data {
6038 isl_union_pw_multi_aff *upma2;
6039 isl_union_pw_multi_aff *res;
6040 isl_pw_multi_aff *pma;
6041 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6044 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6045 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6047 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6049 struct isl_union_pw_multi_aff_bin_data *data = user;
6050 isl_stat r;
6052 data->pma = pma;
6053 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6054 data->fn, data);
6055 isl_pw_multi_aff_free(pma);
6057 return r;
6060 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6061 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6062 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6063 * as *entry. The callback should adjust data->res if desired.
6065 static __isl_give isl_union_pw_multi_aff *bin_op(
6066 __isl_take isl_union_pw_multi_aff *upma1,
6067 __isl_take isl_union_pw_multi_aff *upma2,
6068 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6070 isl_space *space;
6071 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6073 space = isl_union_pw_multi_aff_get_space(upma2);
6074 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6075 space = isl_union_pw_multi_aff_get_space(upma1);
6076 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6078 if (!upma1 || !upma2)
6079 goto error;
6081 data.upma2 = upma2;
6082 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6083 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6084 &bin_entry, &data) < 0)
6085 goto error;
6087 isl_union_pw_multi_aff_free(upma1);
6088 isl_union_pw_multi_aff_free(upma2);
6089 return data.res;
6090 error:
6091 isl_union_pw_multi_aff_free(upma1);
6092 isl_union_pw_multi_aff_free(upma2);
6093 isl_union_pw_multi_aff_free(data.res);
6094 return NULL;
6097 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6098 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6100 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6101 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6103 isl_space *space;
6105 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6106 isl_pw_multi_aff_get_space(pma2));
6107 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6108 &isl_multi_aff_range_product);
6111 /* Given two isl_pw_multi_affs A -> B and C -> D,
6112 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6114 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6115 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6117 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6118 &pw_multi_aff_range_product);
6121 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6122 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6124 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6125 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6127 isl_space *space;
6129 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6130 isl_pw_multi_aff_get_space(pma2));
6131 space = isl_space_flatten_range(space);
6132 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6133 &isl_multi_aff_flat_range_product);
6136 /* Given two isl_pw_multi_affs A -> B and C -> D,
6137 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6139 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6140 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6142 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6143 &pw_multi_aff_flat_range_product);
6146 /* If data->pma and "pma2" have the same domain space, then compute
6147 * their flat range product and the result to data->res.
6149 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6150 void *user)
6152 struct isl_union_pw_multi_aff_bin_data *data = user;
6154 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6155 pma2->dim, isl_dim_in)) {
6156 isl_pw_multi_aff_free(pma2);
6157 return isl_stat_ok;
6160 pma2 = isl_pw_multi_aff_flat_range_product(
6161 isl_pw_multi_aff_copy(data->pma), pma2);
6163 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6165 return isl_stat_ok;
6168 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6169 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6171 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6172 __isl_take isl_union_pw_multi_aff *upma1,
6173 __isl_take isl_union_pw_multi_aff *upma2)
6175 return bin_op(upma1, upma2, &flat_range_product_entry);
6178 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6179 * The parameters are assumed to have been aligned.
6181 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6182 * except that it works on two different isl_pw_* types.
6184 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6185 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6186 __isl_take isl_pw_aff *pa)
6188 int i, j, n;
6189 isl_pw_multi_aff *res = NULL;
6191 if (!pma || !pa)
6192 goto error;
6194 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6195 pa->dim, isl_dim_in))
6196 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6197 "domains don't match", goto error);
6198 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6199 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6200 "index out of bounds", goto error);
6202 n = pma->n * pa->n;
6203 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6205 for (i = 0; i < pma->n; ++i) {
6206 for (j = 0; j < pa->n; ++j) {
6207 isl_set *common;
6208 isl_multi_aff *res_ij;
6209 int empty;
6211 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6212 isl_set_copy(pa->p[j].set));
6213 empty = isl_set_plain_is_empty(common);
6214 if (empty < 0 || empty) {
6215 isl_set_free(common);
6216 if (empty < 0)
6217 goto error;
6218 continue;
6221 res_ij = isl_multi_aff_set_aff(
6222 isl_multi_aff_copy(pma->p[i].maff), pos,
6223 isl_aff_copy(pa->p[j].aff));
6224 res_ij = isl_multi_aff_gist(res_ij,
6225 isl_set_copy(common));
6227 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6231 isl_pw_multi_aff_free(pma);
6232 isl_pw_aff_free(pa);
6233 return res;
6234 error:
6235 isl_pw_multi_aff_free(pma);
6236 isl_pw_aff_free(pa);
6237 return isl_pw_multi_aff_free(res);
6240 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6242 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6243 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6244 __isl_take isl_pw_aff *pa)
6246 isl_bool equal_params;
6248 if (!pma || !pa)
6249 goto error;
6250 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6251 if (equal_params < 0)
6252 goto error;
6253 if (equal_params)
6254 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6255 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6256 isl_pw_aff_check_named_params(pa) < 0)
6257 goto error;
6258 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6259 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6260 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6261 error:
6262 isl_pw_multi_aff_free(pma);
6263 isl_pw_aff_free(pa);
6264 return NULL;
6267 /* Do the parameters of "pa" match those of "space"?
6269 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6270 __isl_keep isl_space *space)
6272 isl_space *pa_space;
6273 isl_bool match;
6275 if (!pa || !space)
6276 return isl_bool_error;
6278 pa_space = isl_pw_aff_get_space(pa);
6280 match = isl_space_has_equal_params(space, pa_space);
6282 isl_space_free(pa_space);
6283 return match;
6286 /* Check that the domain space of "pa" matches "space".
6288 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6289 __isl_keep isl_space *space)
6291 isl_space *pa_space;
6292 isl_bool match;
6294 if (!pa || !space)
6295 return isl_stat_error;
6297 pa_space = isl_pw_aff_get_space(pa);
6299 match = isl_space_has_equal_params(space, pa_space);
6300 if (match < 0)
6301 goto error;
6302 if (!match)
6303 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6304 "parameters don't match", goto error);
6305 match = isl_space_tuple_is_equal(space, isl_dim_in,
6306 pa_space, isl_dim_in);
6307 if (match < 0)
6308 goto error;
6309 if (!match)
6310 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6311 "domains don't match", goto error);
6312 isl_space_free(pa_space);
6313 return isl_stat_ok;
6314 error:
6315 isl_space_free(pa_space);
6316 return isl_stat_error;
6319 #undef BASE
6320 #define BASE pw_aff
6321 #undef DOMBASE
6322 #define DOMBASE set
6324 #include <isl_multi_explicit_domain.c>
6325 #include <isl_multi_pw_aff_explicit_domain.c>
6326 #include <isl_multi_templ.c>
6327 #include <isl_multi_apply_set.c>
6328 #include <isl_multi_coalesce.c>
6329 #include <isl_multi_dims.c>
6330 #include <isl_multi_gist.c>
6331 #include <isl_multi_hash.c>
6332 #include <isl_multi_align_set.c>
6333 #include <isl_multi_intersect.c>
6335 /* Does "mpa" have a non-trivial explicit domain?
6337 * The explicit domain, if present, is trivial if it represents
6338 * an (obviously) universe set.
6340 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6341 __isl_keep isl_multi_pw_aff *mpa)
6343 if (!mpa)
6344 return isl_bool_error;
6345 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6346 return isl_bool_false;
6347 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6350 /* Scale the elements of "pma" by the corresponding elements of "mv".
6352 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6353 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6355 int i;
6356 isl_bool equal_params;
6358 pma = isl_pw_multi_aff_cow(pma);
6359 if (!pma || !mv)
6360 goto error;
6361 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6362 mv->space, isl_dim_set))
6363 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6364 "spaces don't match", goto error);
6365 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6366 if (equal_params < 0)
6367 goto error;
6368 if (!equal_params) {
6369 pma = isl_pw_multi_aff_align_params(pma,
6370 isl_multi_val_get_space(mv));
6371 mv = isl_multi_val_align_params(mv,
6372 isl_pw_multi_aff_get_space(pma));
6373 if (!pma || !mv)
6374 goto error;
6377 for (i = 0; i < pma->n; ++i) {
6378 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6379 isl_multi_val_copy(mv));
6380 if (!pma->p[i].maff)
6381 goto error;
6384 isl_multi_val_free(mv);
6385 return pma;
6386 error:
6387 isl_multi_val_free(mv);
6388 isl_pw_multi_aff_free(pma);
6389 return NULL;
6392 /* This function is called for each entry of an isl_union_pw_multi_aff.
6393 * If the space of the entry matches that of data->mv,
6394 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6395 * Otherwise, return an empty isl_pw_multi_aff.
6397 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6398 __isl_take isl_pw_multi_aff *pma, void *user)
6400 isl_multi_val *mv = user;
6402 if (!pma)
6403 return NULL;
6404 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6405 mv->space, isl_dim_set)) {
6406 isl_space *space = isl_pw_multi_aff_get_space(pma);
6407 isl_pw_multi_aff_free(pma);
6408 return isl_pw_multi_aff_empty(space);
6411 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6414 /* Scale the elements of "upma" by the corresponding elements of "mv",
6415 * for those entries that match the space of "mv".
6417 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6418 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6420 upma = isl_union_pw_multi_aff_align_params(upma,
6421 isl_multi_val_get_space(mv));
6422 mv = isl_multi_val_align_params(mv,
6423 isl_union_pw_multi_aff_get_space(upma));
6424 if (!upma || !mv)
6425 goto error;
6427 return isl_union_pw_multi_aff_transform(upma,
6428 &union_pw_multi_aff_scale_multi_val_entry, mv);
6430 isl_multi_val_free(mv);
6431 return upma;
6432 error:
6433 isl_multi_val_free(mv);
6434 isl_union_pw_multi_aff_free(upma);
6435 return NULL;
6438 /* Construct and return a piecewise multi affine expression
6439 * in the given space with value zero in each of the output dimensions and
6440 * a universe domain.
6442 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6444 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6447 /* Construct and return a piecewise multi affine expression
6448 * that is equal to the given piecewise affine expression.
6450 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6451 __isl_take isl_pw_aff *pa)
6453 int i;
6454 isl_space *space;
6455 isl_pw_multi_aff *pma;
6457 if (!pa)
6458 return NULL;
6460 space = isl_pw_aff_get_space(pa);
6461 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6463 for (i = 0; i < pa->n; ++i) {
6464 isl_set *set;
6465 isl_multi_aff *ma;
6467 set = isl_set_copy(pa->p[i].set);
6468 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6469 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6472 isl_pw_aff_free(pa);
6473 return pma;
6476 /* Construct a set or map mapping the shared (parameter) domain
6477 * of the piecewise affine expressions to the range of "mpa"
6478 * with each dimension in the range equated to the
6479 * corresponding piecewise affine expression.
6481 static __isl_give isl_map *map_from_multi_pw_aff(
6482 __isl_take isl_multi_pw_aff *mpa)
6484 int i;
6485 isl_space *space;
6486 isl_map *map;
6488 if (!mpa)
6489 return NULL;
6491 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6492 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6493 "invalid space", goto error);
6495 space = isl_multi_pw_aff_get_domain_space(mpa);
6496 map = isl_map_universe(isl_space_from_domain(space));
6498 for (i = 0; i < mpa->n; ++i) {
6499 isl_pw_aff *pa;
6500 isl_map *map_i;
6502 pa = isl_pw_aff_copy(mpa->u.p[i]);
6503 map_i = map_from_pw_aff(pa);
6505 map = isl_map_flat_range_product(map, map_i);
6508 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6510 isl_multi_pw_aff_free(mpa);
6511 return map;
6512 error:
6513 isl_multi_pw_aff_free(mpa);
6514 return NULL;
6517 /* Construct a map mapping the shared domain
6518 * of the piecewise affine expressions to the range of "mpa"
6519 * with each dimension in the range equated to the
6520 * corresponding piecewise affine expression.
6522 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6524 if (!mpa)
6525 return NULL;
6526 if (isl_space_is_set(mpa->space))
6527 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6528 "space of input is not a map", goto error);
6530 return map_from_multi_pw_aff(mpa);
6531 error:
6532 isl_multi_pw_aff_free(mpa);
6533 return NULL;
6536 /* Construct a set mapping the shared parameter domain
6537 * of the piecewise affine expressions to the space of "mpa"
6538 * with each dimension in the range equated to the
6539 * corresponding piecewise affine expression.
6541 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6543 if (!mpa)
6544 return NULL;
6545 if (!isl_space_is_set(mpa->space))
6546 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6547 "space of input is not a set", goto error);
6549 return map_from_multi_pw_aff(mpa);
6550 error:
6551 isl_multi_pw_aff_free(mpa);
6552 return NULL;
6555 /* Construct and return a piecewise multi affine expression
6556 * that is equal to the given multi piecewise affine expression
6557 * on the shared domain of the piecewise affine expressions,
6558 * in the special case of a 0D multi piecewise affine expression.
6560 * Create a piecewise multi affine expression with the explicit domain of
6561 * the 0D multi piecewise affine expression as domain.
6563 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6564 __isl_take isl_multi_pw_aff *mpa)
6566 isl_space *space;
6567 isl_set *dom;
6568 isl_multi_aff *ma;
6570 space = isl_multi_pw_aff_get_space(mpa);
6571 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6572 isl_multi_pw_aff_free(mpa);
6574 ma = isl_multi_aff_zero(space);
6575 return isl_pw_multi_aff_alloc(dom, ma);
6578 /* Construct and return a piecewise multi affine expression
6579 * that is equal to the given multi piecewise affine expression
6580 * on the shared domain of the piecewise affine expressions.
6582 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6583 __isl_take isl_multi_pw_aff *mpa)
6585 int i;
6586 isl_space *space;
6587 isl_pw_aff *pa;
6588 isl_pw_multi_aff *pma;
6590 if (!mpa)
6591 return NULL;
6593 if (mpa->n == 0)
6594 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6596 space = isl_multi_pw_aff_get_space(mpa);
6597 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6598 pma = isl_pw_multi_aff_from_pw_aff(pa);
6600 for (i = 1; i < mpa->n; ++i) {
6601 isl_pw_multi_aff *pma_i;
6603 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6604 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6605 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6608 pma = isl_pw_multi_aff_reset_space(pma, space);
6610 isl_multi_pw_aff_free(mpa);
6611 return pma;
6614 /* Construct and return a multi piecewise affine expression
6615 * that is equal to the given multi affine expression.
6617 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6618 __isl_take isl_multi_aff *ma)
6620 int i, n;
6621 isl_multi_pw_aff *mpa;
6623 if (!ma)
6624 return NULL;
6626 n = isl_multi_aff_dim(ma, isl_dim_out);
6627 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6629 for (i = 0; i < n; ++i) {
6630 isl_pw_aff *pa;
6632 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6633 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6636 isl_multi_aff_free(ma);
6637 return mpa;
6640 /* Construct and return a multi piecewise affine expression
6641 * that is equal to the given piecewise multi affine expression.
6643 * If the resulting multi piecewise affine expression has
6644 * an explicit domain, then assign it the domain of the input.
6645 * In other cases, the domain is stored in the individual elements.
6647 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6648 __isl_take isl_pw_multi_aff *pma)
6650 int i, n;
6651 isl_space *space;
6652 isl_multi_pw_aff *mpa;
6654 if (!pma)
6655 return NULL;
6657 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6658 space = isl_pw_multi_aff_get_space(pma);
6659 mpa = isl_multi_pw_aff_alloc(space);
6661 for (i = 0; i < n; ++i) {
6662 isl_pw_aff *pa;
6664 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6665 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6667 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6668 isl_set *dom;
6670 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6671 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6674 isl_pw_multi_aff_free(pma);
6675 return mpa;
6678 /* Do "pa1" and "pa2" represent the same function?
6680 * We first check if they are obviously equal.
6681 * If not, we convert them to maps and check if those are equal.
6683 * If "pa1" or "pa2" contain any NaNs, then they are considered
6684 * not to be the same. A NaN is not equal to anything, not even
6685 * to another NaN.
6687 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6688 __isl_keep isl_pw_aff *pa2)
6690 isl_bool equal;
6691 isl_bool has_nan;
6692 isl_map *map1, *map2;
6694 if (!pa1 || !pa2)
6695 return isl_bool_error;
6697 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6698 if (equal < 0 || equal)
6699 return equal;
6700 has_nan = either_involves_nan(pa1, pa2);
6701 if (has_nan < 0)
6702 return isl_bool_error;
6703 if (has_nan)
6704 return isl_bool_false;
6706 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6707 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6708 equal = isl_map_is_equal(map1, map2);
6709 isl_map_free(map1);
6710 isl_map_free(map2);
6712 return equal;
6715 /* Do "mpa1" and "mpa2" represent the same function?
6717 * Note that we cannot convert the entire isl_multi_pw_aff
6718 * to a map because the domains of the piecewise affine expressions
6719 * may not be the same.
6721 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6722 __isl_keep isl_multi_pw_aff *mpa2)
6724 int i;
6725 isl_bool equal, equal_params;
6727 if (!mpa1 || !mpa2)
6728 return isl_bool_error;
6730 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6731 if (equal_params < 0)
6732 return isl_bool_error;
6733 if (!equal_params) {
6734 if (!isl_space_has_named_params(mpa1->space))
6735 return isl_bool_false;
6736 if (!isl_space_has_named_params(mpa2->space))
6737 return isl_bool_false;
6738 mpa1 = isl_multi_pw_aff_copy(mpa1);
6739 mpa2 = isl_multi_pw_aff_copy(mpa2);
6740 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6741 isl_multi_pw_aff_get_space(mpa2));
6742 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6743 isl_multi_pw_aff_get_space(mpa1));
6744 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6745 isl_multi_pw_aff_free(mpa1);
6746 isl_multi_pw_aff_free(mpa2);
6747 return equal;
6750 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6751 if (equal < 0 || !equal)
6752 return equal;
6754 for (i = 0; i < mpa1->n; ++i) {
6755 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6756 if (equal < 0 || !equal)
6757 return equal;
6760 return isl_bool_true;
6763 /* Do "pma1" and "pma2" represent the same function?
6765 * First check if they are obviously equal.
6766 * If not, then convert them to maps and check if those are equal.
6768 * If "pa1" or "pa2" contain any NaNs, then they are considered
6769 * not to be the same. A NaN is not equal to anything, not even
6770 * to another NaN.
6772 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6773 __isl_keep isl_pw_multi_aff *pma2)
6775 isl_bool equal;
6776 isl_bool has_nan;
6777 isl_map *map1, *map2;
6779 if (!pma1 || !pma2)
6780 return isl_bool_error;
6782 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6783 if (equal < 0 || equal)
6784 return equal;
6785 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6786 if (has_nan >= 0 && !has_nan)
6787 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6788 if (has_nan < 0 || has_nan)
6789 return isl_bool_not(has_nan);
6791 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6792 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6793 equal = isl_map_is_equal(map1, map2);
6794 isl_map_free(map1);
6795 isl_map_free(map2);
6797 return equal;
6800 /* Compute the pullback of "mpa" by the function represented by "ma".
6801 * In other words, plug in "ma" in "mpa".
6803 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6805 * If "mpa" has an explicit domain, then it is this domain
6806 * that needs to undergo a pullback, i.e., a preimage.
6808 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6809 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6811 int i;
6812 isl_space *space = NULL;
6814 mpa = isl_multi_pw_aff_cow(mpa);
6815 if (!mpa || !ma)
6816 goto error;
6818 space = isl_space_join(isl_multi_aff_get_space(ma),
6819 isl_multi_pw_aff_get_space(mpa));
6820 if (!space)
6821 goto error;
6823 for (i = 0; i < mpa->n; ++i) {
6824 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6825 isl_multi_aff_copy(ma));
6826 if (!mpa->u.p[i])
6827 goto error;
6829 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6830 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6831 isl_multi_aff_copy(ma));
6832 if (!mpa->u.dom)
6833 goto error;
6836 isl_multi_aff_free(ma);
6837 isl_space_free(mpa->space);
6838 mpa->space = space;
6839 return mpa;
6840 error:
6841 isl_space_free(space);
6842 isl_multi_pw_aff_free(mpa);
6843 isl_multi_aff_free(ma);
6844 return NULL;
6847 /* Compute the pullback of "mpa" by the function represented by "ma".
6848 * In other words, plug in "ma" in "mpa".
6850 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6851 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6853 isl_bool equal_params;
6855 if (!mpa || !ma)
6856 goto error;
6857 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6858 if (equal_params < 0)
6859 goto error;
6860 if (equal_params)
6861 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6862 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6863 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6864 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6865 error:
6866 isl_multi_pw_aff_free(mpa);
6867 isl_multi_aff_free(ma);
6868 return NULL;
6871 /* Compute the pullback of "mpa" by the function represented by "pma".
6872 * In other words, plug in "pma" in "mpa".
6874 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6876 * If "mpa" has an explicit domain, then it is this domain
6877 * that needs to undergo a pullback, i.e., a preimage.
6879 static __isl_give isl_multi_pw_aff *
6880 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6881 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6883 int i;
6884 isl_space *space = NULL;
6886 mpa = isl_multi_pw_aff_cow(mpa);
6887 if (!mpa || !pma)
6888 goto error;
6890 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6891 isl_multi_pw_aff_get_space(mpa));
6893 for (i = 0; i < mpa->n; ++i) {
6894 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6895 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6896 if (!mpa->u.p[i])
6897 goto error;
6899 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6900 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6901 isl_pw_multi_aff_copy(pma));
6902 if (!mpa->u.dom)
6903 goto error;
6906 isl_pw_multi_aff_free(pma);
6907 isl_space_free(mpa->space);
6908 mpa->space = space;
6909 return mpa;
6910 error:
6911 isl_space_free(space);
6912 isl_multi_pw_aff_free(mpa);
6913 isl_pw_multi_aff_free(pma);
6914 return NULL;
6917 /* Compute the pullback of "mpa" by the function represented by "pma".
6918 * In other words, plug in "pma" in "mpa".
6920 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6921 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6923 isl_bool equal_params;
6925 if (!mpa || !pma)
6926 goto error;
6927 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6928 if (equal_params < 0)
6929 goto error;
6930 if (equal_params)
6931 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6932 mpa = isl_multi_pw_aff_align_params(mpa,
6933 isl_pw_multi_aff_get_space(pma));
6934 pma = isl_pw_multi_aff_align_params(pma,
6935 isl_multi_pw_aff_get_space(mpa));
6936 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6937 error:
6938 isl_multi_pw_aff_free(mpa);
6939 isl_pw_multi_aff_free(pma);
6940 return NULL;
6943 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6944 * with the domain of "aff". The domain of the result is the same
6945 * as that of "mpa".
6946 * "mpa" and "aff" are assumed to have been aligned.
6948 * We first extract the parametric constant from "aff", defined
6949 * over the correct domain.
6950 * Then we add the appropriate combinations of the members of "mpa".
6951 * Finally, we add the integer divisions through recursive calls.
6953 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6954 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6956 int i, n_in, n_div;
6957 isl_space *space;
6958 isl_val *v;
6959 isl_pw_aff *pa;
6960 isl_aff *tmp;
6962 n_in = isl_aff_dim(aff, isl_dim_in);
6963 n_div = isl_aff_dim(aff, isl_dim_div);
6965 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6966 tmp = isl_aff_copy(aff);
6967 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6968 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6969 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6970 isl_space_dim(space, isl_dim_set));
6971 tmp = isl_aff_reset_domain_space(tmp, space);
6972 pa = isl_pw_aff_from_aff(tmp);
6974 for (i = 0; i < n_in; ++i) {
6975 isl_pw_aff *pa_i;
6977 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6978 continue;
6979 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6980 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6981 pa_i = isl_pw_aff_scale_val(pa_i, v);
6982 pa = isl_pw_aff_add(pa, pa_i);
6985 for (i = 0; i < n_div; ++i) {
6986 isl_aff *div;
6987 isl_pw_aff *pa_i;
6989 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6990 continue;
6991 div = isl_aff_get_div(aff, i);
6992 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6993 isl_multi_pw_aff_copy(mpa), div);
6994 pa_i = isl_pw_aff_floor(pa_i);
6995 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6996 pa_i = isl_pw_aff_scale_val(pa_i, v);
6997 pa = isl_pw_aff_add(pa, pa_i);
7000 isl_multi_pw_aff_free(mpa);
7001 isl_aff_free(aff);
7003 return pa;
7006 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7007 * with the domain of "aff". The domain of the result is the same
7008 * as that of "mpa".
7010 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7011 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7013 isl_bool equal_params;
7015 if (!aff || !mpa)
7016 goto error;
7017 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7018 if (equal_params < 0)
7019 goto error;
7020 if (equal_params)
7021 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7023 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7024 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7026 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7027 error:
7028 isl_aff_free(aff);
7029 isl_multi_pw_aff_free(mpa);
7030 return NULL;
7033 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7034 * with the domain of "pa". The domain of the result is the same
7035 * as that of "mpa".
7036 * "mpa" and "pa" are assumed to have been aligned.
7038 * We consider each piece in turn. Note that the domains of the
7039 * pieces are assumed to be disjoint and they remain disjoint
7040 * after taking the preimage (over the same function).
7042 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7043 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7045 isl_space *space;
7046 isl_pw_aff *res;
7047 int i;
7049 if (!mpa || !pa)
7050 goto error;
7052 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7053 isl_pw_aff_get_space(pa));
7054 res = isl_pw_aff_empty(space);
7056 for (i = 0; i < pa->n; ++i) {
7057 isl_pw_aff *pa_i;
7058 isl_set *domain;
7060 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7061 isl_multi_pw_aff_copy(mpa),
7062 isl_aff_copy(pa->p[i].aff));
7063 domain = isl_set_copy(pa->p[i].set);
7064 domain = isl_set_preimage_multi_pw_aff(domain,
7065 isl_multi_pw_aff_copy(mpa));
7066 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7067 res = isl_pw_aff_add_disjoint(res, pa_i);
7070 isl_pw_aff_free(pa);
7071 isl_multi_pw_aff_free(mpa);
7072 return res;
7073 error:
7074 isl_pw_aff_free(pa);
7075 isl_multi_pw_aff_free(mpa);
7076 return NULL;
7079 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7080 * with the domain of "pa". The domain of the result is the same
7081 * as that of "mpa".
7083 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7084 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7086 isl_bool equal_params;
7088 if (!pa || !mpa)
7089 goto error;
7090 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7091 if (equal_params < 0)
7092 goto error;
7093 if (equal_params)
7094 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7096 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7097 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7099 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7100 error:
7101 isl_pw_aff_free(pa);
7102 isl_multi_pw_aff_free(mpa);
7103 return NULL;
7106 /* Compute the pullback of "pa" by the function represented by "mpa".
7107 * In other words, plug in "mpa" in "pa".
7108 * "pa" and "mpa" are assumed to have been aligned.
7110 * The pullback is computed by applying "pa" to "mpa".
7112 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7113 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7115 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7118 /* Compute the pullback of "pa" by the function represented by "mpa".
7119 * In other words, plug in "mpa" in "pa".
7121 * The pullback is computed by applying "pa" to "mpa".
7123 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7124 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7126 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7129 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7130 * In other words, plug in "mpa2" in "mpa1".
7132 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7134 * We pullback each member of "mpa1" in turn.
7136 * If "mpa1" has an explicit domain, then it is this domain
7137 * that needs to undergo a pullback instead, i.e., a preimage.
7139 static __isl_give isl_multi_pw_aff *
7140 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7141 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7143 int i;
7144 isl_space *space = NULL;
7146 mpa1 = isl_multi_pw_aff_cow(mpa1);
7147 if (!mpa1 || !mpa2)
7148 goto error;
7150 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7151 isl_multi_pw_aff_get_space(mpa1));
7153 for (i = 0; i < mpa1->n; ++i) {
7154 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7155 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7156 if (!mpa1->u.p[i])
7157 goto error;
7160 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7161 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7162 isl_multi_pw_aff_copy(mpa2));
7163 if (!mpa1->u.dom)
7164 goto error;
7166 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7168 isl_multi_pw_aff_free(mpa2);
7169 return mpa1;
7170 error:
7171 isl_space_free(space);
7172 isl_multi_pw_aff_free(mpa1);
7173 isl_multi_pw_aff_free(mpa2);
7174 return NULL;
7177 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7178 * In other words, plug in "mpa2" in "mpa1".
7180 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7181 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7183 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7184 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7187 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7188 * of "mpa1" and "mpa2" live in the same space, construct map space
7189 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7190 * with this map space as extract argument.
7192 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7193 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7194 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7195 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7197 int match;
7198 isl_space *space1, *space2;
7199 isl_map *res;
7201 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7202 isl_multi_pw_aff_get_space(mpa2));
7203 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7204 isl_multi_pw_aff_get_space(mpa1));
7205 if (!mpa1 || !mpa2)
7206 goto error;
7207 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7208 mpa2->space, isl_dim_out);
7209 if (match < 0)
7210 goto error;
7211 if (!match)
7212 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7213 "range spaces don't match", goto error);
7214 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7215 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7216 space1 = isl_space_map_from_domain_and_range(space1, space2);
7218 res = order(mpa1, mpa2, space1);
7219 isl_multi_pw_aff_free(mpa1);
7220 isl_multi_pw_aff_free(mpa2);
7221 return res;
7222 error:
7223 isl_multi_pw_aff_free(mpa1);
7224 isl_multi_pw_aff_free(mpa2);
7225 return NULL;
7228 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7229 * where the function values are equal. "space" is the space of the result.
7230 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7232 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7233 * in the sequences are equal.
7235 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7236 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7237 __isl_take isl_space *space)
7239 int i, n;
7240 isl_map *res;
7242 res = isl_map_universe(space);
7244 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7245 for (i = 0; i < n; ++i) {
7246 isl_pw_aff *pa1, *pa2;
7247 isl_map *map;
7249 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7250 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7251 map = isl_pw_aff_eq_map(pa1, pa2);
7252 res = isl_map_intersect(res, map);
7255 return res;
7258 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7259 * where the function values are equal.
7261 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7262 __isl_take isl_multi_pw_aff *mpa2)
7264 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7265 &isl_multi_pw_aff_eq_map_on_space);
7268 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7269 * where the function values of "mpa1" is lexicographically satisfies "base"
7270 * compared to that of "mpa2". "space" is the space of the result.
7271 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7273 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7274 * if its i-th element satisfies "base" when compared to
7275 * the i-th element of "mpa2" while all previous elements are
7276 * pairwise equal.
7278 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7279 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7280 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7281 __isl_take isl_pw_aff *pa2),
7282 __isl_take isl_space *space)
7284 int i, n;
7285 isl_map *res, *rest;
7287 res = isl_map_empty(isl_space_copy(space));
7288 rest = isl_map_universe(space);
7290 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7291 for (i = 0; i < n; ++i) {
7292 isl_pw_aff *pa1, *pa2;
7293 isl_map *map;
7295 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7296 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7297 map = base(pa1, pa2);
7298 map = isl_map_intersect(map, isl_map_copy(rest));
7299 res = isl_map_union(res, map);
7301 if (i == n - 1)
7302 continue;
7304 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7305 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7306 map = isl_pw_aff_eq_map(pa1, pa2);
7307 rest = isl_map_intersect(rest, map);
7310 isl_map_free(rest);
7311 return res;
7314 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7315 * where the function value of "mpa1" is lexicographically less than that
7316 * of "mpa2". "space" is the space of the result.
7317 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7319 * "mpa1" is less than "mpa2" if its i-th element is smaller
7320 * than the i-th element of "mpa2" while all previous elements are
7321 * pairwise equal.
7323 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7324 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7325 __isl_take isl_space *space)
7327 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7328 &isl_pw_aff_lt_map, space);
7331 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7332 * where the function value of "mpa1" is lexicographically less than that
7333 * of "mpa2".
7335 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7336 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7338 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7339 &isl_multi_pw_aff_lex_lt_map_on_space);
7342 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7343 * where the function value of "mpa1" is lexicographically greater than that
7344 * of "mpa2". "space" is the space of the result.
7345 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7347 * "mpa1" is greater than "mpa2" if its i-th element is greater
7348 * than the i-th element of "mpa2" while all previous elements are
7349 * pairwise equal.
7351 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7352 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7353 __isl_take isl_space *space)
7355 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7356 &isl_pw_aff_gt_map, space);
7359 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7360 * where the function value of "mpa1" is lexicographically greater than that
7361 * of "mpa2".
7363 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7364 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7366 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7367 &isl_multi_pw_aff_lex_gt_map_on_space);
7370 /* Compare two isl_affs.
7372 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7373 * than "aff2" and 0 if they are equal.
7375 * The order is fairly arbitrary. We do consider expressions that only involve
7376 * earlier dimensions as "smaller".
7378 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7380 int cmp;
7381 int last1, last2;
7383 if (aff1 == aff2)
7384 return 0;
7386 if (!aff1)
7387 return -1;
7388 if (!aff2)
7389 return 1;
7391 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7392 if (cmp != 0)
7393 return cmp;
7395 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7396 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7397 if (last1 != last2)
7398 return last1 - last2;
7400 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7403 /* Compare two isl_pw_affs.
7405 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7406 * than "pa2" and 0 if they are equal.
7408 * The order is fairly arbitrary. We do consider expressions that only involve
7409 * earlier dimensions as "smaller".
7411 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7412 __isl_keep isl_pw_aff *pa2)
7414 int i;
7415 int cmp;
7417 if (pa1 == pa2)
7418 return 0;
7420 if (!pa1)
7421 return -1;
7422 if (!pa2)
7423 return 1;
7425 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7426 if (cmp != 0)
7427 return cmp;
7429 if (pa1->n != pa2->n)
7430 return pa1->n - pa2->n;
7432 for (i = 0; i < pa1->n; ++i) {
7433 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7434 if (cmp != 0)
7435 return cmp;
7436 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7437 if (cmp != 0)
7438 return cmp;
7441 return 0;
7444 /* Return a piecewise affine expression that is equal to "v" on "domain".
7446 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7447 __isl_take isl_val *v)
7449 isl_space *space;
7450 isl_local_space *ls;
7451 isl_aff *aff;
7453 space = isl_set_get_space(domain);
7454 ls = isl_local_space_from_space(space);
7455 aff = isl_aff_val_on_domain(ls, v);
7457 return isl_pw_aff_alloc(domain, aff);
7460 /* Return a multi affine expression that is equal to "mv" on domain
7461 * space "space".
7463 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7464 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7466 int i, n;
7467 isl_space *space2;
7468 isl_local_space *ls;
7469 isl_multi_aff *ma;
7471 if (!space || !mv)
7472 goto error;
7474 n = isl_multi_val_dim(mv, isl_dim_set);
7475 space2 = isl_multi_val_get_space(mv);
7476 space2 = isl_space_align_params(space2, isl_space_copy(space));
7477 space = isl_space_align_params(space, isl_space_copy(space2));
7478 space = isl_space_map_from_domain_and_range(space, space2);
7479 ma = isl_multi_aff_alloc(isl_space_copy(space));
7480 ls = isl_local_space_from_space(isl_space_domain(space));
7481 for (i = 0; i < n; ++i) {
7482 isl_val *v;
7483 isl_aff *aff;
7485 v = isl_multi_val_get_val(mv, i);
7486 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7487 ma = isl_multi_aff_set_aff(ma, i, aff);
7489 isl_local_space_free(ls);
7491 isl_multi_val_free(mv);
7492 return ma;
7493 error:
7494 isl_space_free(space);
7495 isl_multi_val_free(mv);
7496 return NULL;
7499 /* Return a piecewise multi-affine expression
7500 * that is equal to "mv" on "domain".
7502 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7503 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7505 isl_space *space;
7506 isl_multi_aff *ma;
7508 space = isl_set_get_space(domain);
7509 ma = isl_multi_aff_multi_val_on_space(space, mv);
7511 return isl_pw_multi_aff_alloc(domain, ma);
7514 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7515 * mv is the value that should be attained on each domain set
7516 * res collects the results
7518 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7519 isl_multi_val *mv;
7520 isl_union_pw_multi_aff *res;
7523 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7524 * and add it to data->res.
7526 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7527 void *user)
7529 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7530 isl_pw_multi_aff *pma;
7531 isl_multi_val *mv;
7533 mv = isl_multi_val_copy(data->mv);
7534 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7535 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7537 return data->res ? isl_stat_ok : isl_stat_error;
7540 /* Return a union piecewise multi-affine expression
7541 * that is equal to "mv" on "domain".
7543 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7544 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7546 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7547 isl_space *space;
7549 space = isl_union_set_get_space(domain);
7550 data.res = isl_union_pw_multi_aff_empty(space);
7551 data.mv = mv;
7552 if (isl_union_set_foreach_set(domain,
7553 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7554 data.res = isl_union_pw_multi_aff_free(data.res);
7555 isl_union_set_free(domain);
7556 isl_multi_val_free(mv);
7557 return data.res;
7560 /* Compute the pullback of data->pma by the function represented by "pma2",
7561 * provided the spaces match, and add the results to data->res.
7563 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7565 struct isl_union_pw_multi_aff_bin_data *data = user;
7567 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7568 pma2->dim, isl_dim_out)) {
7569 isl_pw_multi_aff_free(pma2);
7570 return isl_stat_ok;
7573 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7574 isl_pw_multi_aff_copy(data->pma), pma2);
7576 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7577 if (!data->res)
7578 return isl_stat_error;
7580 return isl_stat_ok;
7583 /* Compute the pullback of "upma1" by the function represented by "upma2".
7585 __isl_give isl_union_pw_multi_aff *
7586 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7587 __isl_take isl_union_pw_multi_aff *upma1,
7588 __isl_take isl_union_pw_multi_aff *upma2)
7590 return bin_op(upma1, upma2, &pullback_entry);
7593 /* Check that the domain space of "upa" matches "space".
7595 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7596 * can in principle never fail since the space "space" is that
7597 * of the isl_multi_union_pw_aff and is a set space such that
7598 * there is no domain space to match.
7600 * We check the parameters and double-check that "space" is
7601 * indeed that of a set.
7603 static isl_stat isl_union_pw_aff_check_match_domain_space(
7604 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7606 isl_space *upa_space;
7607 isl_bool match;
7609 if (!upa || !space)
7610 return isl_stat_error;
7612 match = isl_space_is_set(space);
7613 if (match < 0)
7614 return isl_stat_error;
7615 if (!match)
7616 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7617 "expecting set space", return isl_stat_error);
7619 upa_space = isl_union_pw_aff_get_space(upa);
7620 match = isl_space_has_equal_params(space, upa_space);
7621 if (match < 0)
7622 goto error;
7623 if (!match)
7624 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7625 "parameters don't match", goto error);
7627 isl_space_free(upa_space);
7628 return isl_stat_ok;
7629 error:
7630 isl_space_free(upa_space);
7631 return isl_stat_error;
7634 /* Do the parameters of "upa" match those of "space"?
7636 static isl_bool isl_union_pw_aff_matching_params(
7637 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7639 isl_space *upa_space;
7640 isl_bool match;
7642 if (!upa || !space)
7643 return isl_bool_error;
7645 upa_space = isl_union_pw_aff_get_space(upa);
7647 match = isl_space_has_equal_params(space, upa_space);
7649 isl_space_free(upa_space);
7650 return match;
7653 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7654 * space represents the new parameters.
7655 * res collects the results.
7657 struct isl_union_pw_aff_reset_params_data {
7658 isl_space *space;
7659 isl_union_pw_aff *res;
7662 /* Replace the parameters of "pa" by data->space and
7663 * add the result to data->res.
7665 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7667 struct isl_union_pw_aff_reset_params_data *data = user;
7668 isl_space *space;
7670 space = isl_pw_aff_get_space(pa);
7671 space = isl_space_replace_params(space, data->space);
7672 pa = isl_pw_aff_reset_space(pa, space);
7673 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7675 return data->res ? isl_stat_ok : isl_stat_error;
7678 /* Replace the domain space of "upa" by "space".
7679 * Since a union expression does not have a (single) domain space,
7680 * "space" is necessarily a parameter space.
7682 * Since the order and the names of the parameters determine
7683 * the hash value, we need to create a new hash table.
7685 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7686 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7688 struct isl_union_pw_aff_reset_params_data data = { space };
7689 isl_bool match;
7691 match = isl_union_pw_aff_matching_params(upa, space);
7692 if (match < 0)
7693 upa = isl_union_pw_aff_free(upa);
7694 else if (match) {
7695 isl_space_free(space);
7696 return upa;
7699 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7700 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7701 data.res = isl_union_pw_aff_free(data.res);
7703 isl_union_pw_aff_free(upa);
7704 isl_space_free(space);
7705 return data.res;
7708 /* Return the floor of "pa".
7710 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7712 return isl_pw_aff_floor(pa);
7715 /* Given f, return floor(f).
7717 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7718 __isl_take isl_union_pw_aff *upa)
7720 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7723 /* Compute
7725 * upa mod m = upa - m * floor(upa/m)
7727 * with m an integer value.
7729 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7730 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7732 isl_union_pw_aff *res;
7734 if (!upa || !m)
7735 goto error;
7737 if (!isl_val_is_int(m))
7738 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7739 "expecting integer modulo", goto error);
7740 if (!isl_val_is_pos(m))
7741 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7742 "expecting positive modulo", goto error);
7744 res = isl_union_pw_aff_copy(upa);
7745 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7746 upa = isl_union_pw_aff_floor(upa);
7747 upa = isl_union_pw_aff_scale_val(upa, m);
7748 res = isl_union_pw_aff_sub(res, upa);
7750 return res;
7751 error:
7752 isl_val_free(m);
7753 isl_union_pw_aff_free(upa);
7754 return NULL;
7757 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7758 * pos is the output position that needs to be extracted.
7759 * res collects the results.
7761 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7762 int pos;
7763 isl_union_pw_aff *res;
7766 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7767 * (assuming it has such a dimension) and add it to data->res.
7769 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7771 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7772 int n_out;
7773 isl_pw_aff *pa;
7775 if (!pma)
7776 return isl_stat_error;
7778 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7779 if (data->pos >= n_out) {
7780 isl_pw_multi_aff_free(pma);
7781 return isl_stat_ok;
7784 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7785 isl_pw_multi_aff_free(pma);
7787 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7789 return data->res ? isl_stat_ok : isl_stat_error;
7792 /* Extract an isl_union_pw_aff corresponding to
7793 * output dimension "pos" of "upma".
7795 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7796 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7798 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7799 isl_space *space;
7801 if (!upma)
7802 return NULL;
7804 if (pos < 0)
7805 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7806 "cannot extract at negative position", return NULL);
7808 space = isl_union_pw_multi_aff_get_space(upma);
7809 data.res = isl_union_pw_aff_empty(space);
7810 data.pos = pos;
7811 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7812 &get_union_pw_aff, &data) < 0)
7813 data.res = isl_union_pw_aff_free(data.res);
7815 return data.res;
7818 /* Return a union piecewise affine expression
7819 * that is equal to "aff" on "domain".
7821 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7822 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7824 isl_pw_aff *pa;
7826 pa = isl_pw_aff_from_aff(aff);
7827 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7830 /* Return a union piecewise affine expression
7831 * that is equal to the parameter identified by "id" on "domain".
7833 * Make sure the parameter appears in the space passed to
7834 * isl_aff_param_on_domain_space_id.
7836 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7837 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7839 isl_space *space;
7840 isl_aff *aff;
7842 space = isl_union_set_get_space(domain);
7843 space = isl_space_add_param_id(space, isl_id_copy(id));
7844 aff = isl_aff_param_on_domain_space_id(space, id);
7845 return isl_union_pw_aff_aff_on_domain(domain, aff);
7848 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7849 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7850 * needs to attain.
7851 * "res" collects the results.
7853 struct isl_union_pw_aff_pw_aff_on_domain_data {
7854 isl_pw_aff *pa;
7855 isl_union_pw_aff *res;
7858 /* Construct a piecewise affine expression that is equal to data->pa
7859 * on "domain" and add the result to data->res.
7861 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7863 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7864 isl_pw_aff *pa;
7865 int dim;
7867 pa = isl_pw_aff_copy(data->pa);
7868 dim = isl_set_dim(domain, isl_dim_set);
7869 pa = isl_pw_aff_from_range(pa);
7870 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7871 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7872 pa = isl_pw_aff_intersect_domain(pa, domain);
7873 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7875 return data->res ? isl_stat_ok : isl_stat_error;
7878 /* Return a union piecewise affine expression
7879 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7880 * have been aligned.
7882 * Construct an isl_pw_aff on each of the sets in "domain" and
7883 * collect the results.
7885 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7886 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7888 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7889 isl_space *space;
7891 space = isl_union_set_get_space(domain);
7892 data.res = isl_union_pw_aff_empty(space);
7893 data.pa = pa;
7894 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7895 data.res = isl_union_pw_aff_free(data.res);
7896 isl_union_set_free(domain);
7897 isl_pw_aff_free(pa);
7898 return data.res;
7901 /* Return a union piecewise affine expression
7902 * that is equal to "pa" on "domain".
7904 * Check that "pa" is a parametric expression,
7905 * align the parameters if needed and call
7906 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7908 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7909 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7911 isl_bool is_set;
7912 isl_bool equal_params;
7913 isl_space *domain_space, *pa_space;
7915 pa_space = isl_pw_aff_peek_space(pa);
7916 is_set = isl_space_is_set(pa_space);
7917 if (is_set < 0)
7918 goto error;
7919 if (!is_set)
7920 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7921 "expecting parametric expression", goto error);
7923 domain_space = isl_union_set_get_space(domain);
7924 pa_space = isl_pw_aff_get_space(pa);
7925 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7926 if (equal_params >= 0 && !equal_params) {
7927 isl_space *space;
7929 space = isl_space_align_params(domain_space, pa_space);
7930 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7931 domain = isl_union_set_align_params(domain, space);
7932 } else {
7933 isl_space_free(domain_space);
7934 isl_space_free(pa_space);
7937 if (equal_params < 0)
7938 goto error;
7939 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7940 error:
7941 isl_union_set_free(domain);
7942 isl_pw_aff_free(pa);
7943 return NULL;
7946 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7947 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7948 * "res" collects the results.
7950 struct isl_union_pw_aff_val_on_domain_data {
7951 isl_val *v;
7952 isl_union_pw_aff *res;
7955 /* Construct a piecewise affine expression that is equal to data->v
7956 * on "domain" and add the result to data->res.
7958 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7960 struct isl_union_pw_aff_val_on_domain_data *data = user;
7961 isl_pw_aff *pa;
7962 isl_val *v;
7964 v = isl_val_copy(data->v);
7965 pa = isl_pw_aff_val_on_domain(domain, v);
7966 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7968 return data->res ? isl_stat_ok : isl_stat_error;
7971 /* Return a union piecewise affine expression
7972 * that is equal to "v" on "domain".
7974 * Construct an isl_pw_aff on each of the sets in "domain" and
7975 * collect the results.
7977 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7978 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7980 struct isl_union_pw_aff_val_on_domain_data data;
7981 isl_space *space;
7983 space = isl_union_set_get_space(domain);
7984 data.res = isl_union_pw_aff_empty(space);
7985 data.v = v;
7986 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7987 data.res = isl_union_pw_aff_free(data.res);
7988 isl_union_set_free(domain);
7989 isl_val_free(v);
7990 return data.res;
7993 /* Construct a piecewise multi affine expression
7994 * that is equal to "pa" and add it to upma.
7996 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7997 void *user)
7999 isl_union_pw_multi_aff **upma = user;
8000 isl_pw_multi_aff *pma;
8002 pma = isl_pw_multi_aff_from_pw_aff(pa);
8003 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8005 return *upma ? isl_stat_ok : isl_stat_error;
8008 /* Construct and return a union piecewise multi affine expression
8009 * that is equal to the given union piecewise affine expression.
8011 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8012 __isl_take isl_union_pw_aff *upa)
8014 isl_space *space;
8015 isl_union_pw_multi_aff *upma;
8017 if (!upa)
8018 return NULL;
8020 space = isl_union_pw_aff_get_space(upa);
8021 upma = isl_union_pw_multi_aff_empty(space);
8023 if (isl_union_pw_aff_foreach_pw_aff(upa,
8024 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8025 upma = isl_union_pw_multi_aff_free(upma);
8027 isl_union_pw_aff_free(upa);
8028 return upma;
8031 /* Compute the set of elements in the domain of "pa" where it is zero and
8032 * add this set to "uset".
8034 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8036 isl_union_set **uset = (isl_union_set **)user;
8038 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8040 return *uset ? isl_stat_ok : isl_stat_error;
8043 /* Return a union set containing those elements in the domain
8044 * of "upa" where it is zero.
8046 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8047 __isl_take isl_union_pw_aff *upa)
8049 isl_union_set *zero;
8051 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8052 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8053 zero = isl_union_set_free(zero);
8055 isl_union_pw_aff_free(upa);
8056 return zero;
8059 /* Convert "pa" to an isl_map and add it to *umap.
8061 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8063 isl_union_map **umap = user;
8064 isl_map *map;
8066 map = isl_map_from_pw_aff(pa);
8067 *umap = isl_union_map_add_map(*umap, map);
8069 return *umap ? isl_stat_ok : isl_stat_error;
8072 /* Construct a union map mapping the domain of the union
8073 * piecewise affine expression to its range, with the single output dimension
8074 * equated to the corresponding affine expressions on their cells.
8076 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8077 __isl_take isl_union_pw_aff *upa)
8079 isl_space *space;
8080 isl_union_map *umap;
8082 if (!upa)
8083 return NULL;
8085 space = isl_union_pw_aff_get_space(upa);
8086 umap = isl_union_map_empty(space);
8088 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8089 &umap) < 0)
8090 umap = isl_union_map_free(umap);
8092 isl_union_pw_aff_free(upa);
8093 return umap;
8096 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8097 * upma is the function that is plugged in.
8098 * pa is the current part of the function in which upma is plugged in.
8099 * res collects the results.
8101 struct isl_union_pw_aff_pullback_upma_data {
8102 isl_union_pw_multi_aff *upma;
8103 isl_pw_aff *pa;
8104 isl_union_pw_aff *res;
8107 /* Check if "pma" can be plugged into data->pa.
8108 * If so, perform the pullback and add the result to data->res.
8110 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8112 struct isl_union_pw_aff_pullback_upma_data *data = user;
8113 isl_pw_aff *pa;
8115 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8116 pma->dim, isl_dim_out)) {
8117 isl_pw_multi_aff_free(pma);
8118 return isl_stat_ok;
8121 pa = isl_pw_aff_copy(data->pa);
8122 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8124 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8126 return data->res ? isl_stat_ok : isl_stat_error;
8129 /* Check if any of the elements of data->upma can be plugged into pa,
8130 * add if so add the result to data->res.
8132 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8134 struct isl_union_pw_aff_pullback_upma_data *data = user;
8135 isl_stat r;
8137 data->pa = pa;
8138 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8139 &pa_pb_pma, data);
8140 isl_pw_aff_free(pa);
8142 return r;
8145 /* Compute the pullback of "upa" by the function represented by "upma".
8146 * In other words, plug in "upma" in "upa". The result contains
8147 * expressions defined over the domain space of "upma".
8149 * Run over all pairs of elements in "upa" and "upma", perform
8150 * the pullback when appropriate and collect the results.
8151 * If the hash value were based on the domain space rather than
8152 * the function space, then we could run through all elements
8153 * of "upma" and directly pick out the corresponding element of "upa".
8155 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8156 __isl_take isl_union_pw_aff *upa,
8157 __isl_take isl_union_pw_multi_aff *upma)
8159 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8160 isl_space *space;
8162 space = isl_union_pw_multi_aff_get_space(upma);
8163 upa = isl_union_pw_aff_align_params(upa, space);
8164 space = isl_union_pw_aff_get_space(upa);
8165 upma = isl_union_pw_multi_aff_align_params(upma, space);
8167 if (!upa || !upma)
8168 goto error;
8170 data.upma = upma;
8171 data.res = isl_union_pw_aff_alloc_same_size(upa);
8172 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8173 data.res = isl_union_pw_aff_free(data.res);
8175 isl_union_pw_aff_free(upa);
8176 isl_union_pw_multi_aff_free(upma);
8177 return data.res;
8178 error:
8179 isl_union_pw_aff_free(upa);
8180 isl_union_pw_multi_aff_free(upma);
8181 return NULL;
8184 #undef BASE
8185 #define BASE union_pw_aff
8186 #undef DOMBASE
8187 #define DOMBASE union_set
8189 #define NO_MOVE_DIMS
8190 #define NO_DOMAIN
8191 #define NO_PRODUCT
8192 #define NO_SPLICE
8193 #define NO_ZERO
8194 #define NO_IDENTITY
8196 #include <isl_multi_explicit_domain.c>
8197 #include <isl_multi_union_pw_aff_explicit_domain.c>
8198 #include <isl_multi_templ.c>
8199 #include <isl_multi_apply_set.c>
8200 #include <isl_multi_apply_union_set.c>
8201 #include <isl_multi_coalesce.c>
8202 #include <isl_multi_floor.c>
8203 #include <isl_multi_gist.c>
8204 #include <isl_multi_align_set.c>
8205 #include <isl_multi_align_union_set.c>
8206 #include <isl_multi_intersect.c>
8208 /* Does "mupa" have a non-trivial explicit domain?
8210 * The explicit domain, if present, is trivial if it represents
8211 * an (obviously) universe parameter set.
8213 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8214 __isl_keep isl_multi_union_pw_aff *mupa)
8216 isl_bool is_params, trivial;
8217 isl_set *set;
8219 if (!mupa)
8220 return isl_bool_error;
8221 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8222 return isl_bool_false;
8223 is_params = isl_union_set_is_params(mupa->u.dom);
8224 if (is_params < 0 || !is_params)
8225 return isl_bool_not(is_params);
8226 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8227 trivial = isl_set_plain_is_universe(set);
8228 isl_set_free(set);
8229 return isl_bool_not(trivial);
8232 /* Construct a multiple union piecewise affine expression
8233 * in the given space with value zero in each of the output dimensions.
8235 * Since there is no canonical zero value for
8236 * a union piecewise affine expression, we can only construct
8237 * a zero-dimensional "zero" value.
8239 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8240 __isl_take isl_space *space)
8242 isl_bool params;
8244 if (!space)
8245 return NULL;
8247 params = isl_space_is_params(space);
8248 if (params < 0)
8249 goto error;
8250 if (params)
8251 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8252 "expecting proper set space", goto error);
8253 if (!isl_space_is_set(space))
8254 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8255 "expecting set space", goto error);
8256 if (isl_space_dim(space , isl_dim_out) != 0)
8257 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8258 "expecting 0D space", goto error);
8260 return isl_multi_union_pw_aff_alloc(space);
8261 error:
8262 isl_space_free(space);
8263 return NULL;
8266 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8267 * with the actual sum on the shared domain and
8268 * the defined expression on the symmetric difference of the domains.
8270 * We simply iterate over the elements in both arguments and
8271 * call isl_union_pw_aff_union_add on each of them, if there is
8272 * at least one element.
8274 * Otherwise, the two expressions have an explicit domain and
8275 * the union of these explicit domains is computed.
8276 * This assumes that the explicit domains are either both in terms
8277 * of specific domains elements or both in terms of parameters.
8278 * However, if one of the expressions does not have any constraints
8279 * on its explicit domain, then this is allowed as well and the result
8280 * is the expression with no constraints on its explicit domain.
8282 static __isl_give isl_multi_union_pw_aff *
8283 isl_multi_union_pw_aff_union_add_aligned(
8284 __isl_take isl_multi_union_pw_aff *mupa1,
8285 __isl_take isl_multi_union_pw_aff *mupa2)
8287 isl_bool has_domain, is_params1, is_params2;
8289 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8290 goto error;
8291 if (mupa1->n > 0)
8292 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8293 &isl_union_pw_aff_union_add);
8294 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8295 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8296 goto error;
8298 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8299 if (has_domain < 0)
8300 goto error;
8301 if (!has_domain) {
8302 isl_multi_union_pw_aff_free(mupa2);
8303 return mupa1;
8305 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8306 if (has_domain < 0)
8307 goto error;
8308 if (!has_domain) {
8309 isl_multi_union_pw_aff_free(mupa1);
8310 return mupa2;
8313 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8314 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8315 if (is_params1 < 0 || is_params2 < 0)
8316 goto error;
8317 if (is_params1 != is_params2)
8318 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8319 isl_error_invalid,
8320 "cannot compute union of concrete domain and "
8321 "parameter constraints", goto error);
8322 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8323 if (!mupa1)
8324 goto error;
8325 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8326 isl_union_set_copy(mupa2->u.dom));
8327 if (!mupa1->u.dom)
8328 goto error;
8329 isl_multi_union_pw_aff_free(mupa2);
8330 return mupa1;
8331 error:
8332 isl_multi_union_pw_aff_free(mupa1);
8333 isl_multi_union_pw_aff_free(mupa2);
8334 return NULL;
8337 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8338 * with the actual sum on the shared domain and
8339 * the defined expression on the symmetric difference of the domains.
8341 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8342 __isl_take isl_multi_union_pw_aff *mupa1,
8343 __isl_take isl_multi_union_pw_aff *mupa2)
8345 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8346 &isl_multi_union_pw_aff_union_add_aligned);
8349 /* Construct and return a multi union piecewise affine expression
8350 * that is equal to the given multi affine expression.
8352 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8353 __isl_take isl_multi_aff *ma)
8355 isl_multi_pw_aff *mpa;
8357 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8358 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8361 /* Construct and return a multi union piecewise affine expression
8362 * that is equal to the given multi piecewise affine expression.
8364 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8365 __isl_take isl_multi_pw_aff *mpa)
8367 int i, n;
8368 isl_space *space;
8369 isl_multi_union_pw_aff *mupa;
8371 if (!mpa)
8372 return NULL;
8374 space = isl_multi_pw_aff_get_space(mpa);
8375 space = isl_space_range(space);
8376 mupa = isl_multi_union_pw_aff_alloc(space);
8378 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8379 for (i = 0; i < n; ++i) {
8380 isl_pw_aff *pa;
8381 isl_union_pw_aff *upa;
8383 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8384 upa = isl_union_pw_aff_from_pw_aff(pa);
8385 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8388 isl_multi_pw_aff_free(mpa);
8390 return mupa;
8393 /* Extract the range space of "pma" and assign it to *space.
8394 * If *space has already been set (through a previous call to this function),
8395 * then check that the range space is the same.
8397 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8399 isl_space **space = user;
8400 isl_space *pma_space;
8401 isl_bool equal;
8403 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8404 isl_pw_multi_aff_free(pma);
8406 if (!pma_space)
8407 return isl_stat_error;
8408 if (!*space) {
8409 *space = pma_space;
8410 return isl_stat_ok;
8413 equal = isl_space_is_equal(pma_space, *space);
8414 isl_space_free(pma_space);
8416 if (equal < 0)
8417 return isl_stat_error;
8418 if (!equal)
8419 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8420 "range spaces not the same", return isl_stat_error);
8421 return isl_stat_ok;
8424 /* Construct and return a multi union piecewise affine expression
8425 * that is equal to the given union piecewise multi affine expression.
8427 * In order to be able to perform the conversion, the input
8428 * needs to be non-empty and may only involve a single range space.
8430 * If the resulting multi union piecewise affine expression has
8431 * an explicit domain, then assign it the domain of the input.
8432 * In other cases, the domain is stored in the individual elements.
8434 __isl_give isl_multi_union_pw_aff *
8435 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8436 __isl_take isl_union_pw_multi_aff *upma)
8438 isl_space *space = NULL;
8439 isl_multi_union_pw_aff *mupa;
8440 int i, n;
8442 if (!upma)
8443 return NULL;
8444 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8445 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8446 "cannot extract range space from empty input",
8447 goto error);
8448 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8449 &space) < 0)
8450 goto error;
8452 if (!space)
8453 goto error;
8455 n = isl_space_dim(space, isl_dim_set);
8456 mupa = isl_multi_union_pw_aff_alloc(space);
8458 for (i = 0; i < n; ++i) {
8459 isl_union_pw_aff *upa;
8461 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8462 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8464 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8465 isl_union_set *dom;
8466 isl_union_pw_multi_aff *copy;
8468 copy = isl_union_pw_multi_aff_copy(upma);
8469 dom = isl_union_pw_multi_aff_domain(copy);
8470 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8473 isl_union_pw_multi_aff_free(upma);
8474 return mupa;
8475 error:
8476 isl_space_free(space);
8477 isl_union_pw_multi_aff_free(upma);
8478 return NULL;
8481 /* Try and create an isl_multi_union_pw_aff that is equivalent
8482 * to the given isl_union_map.
8483 * The isl_union_map is required to be single-valued in each space.
8484 * Moreover, it cannot be empty and all range spaces need to be the same.
8485 * Otherwise, an error is produced.
8487 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8488 __isl_take isl_union_map *umap)
8490 isl_union_pw_multi_aff *upma;
8492 upma = isl_union_pw_multi_aff_from_union_map(umap);
8493 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8496 /* Return a multiple union piecewise affine expression
8497 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8498 * have been aligned.
8500 * If the resulting multi union piecewise affine expression has
8501 * an explicit domain, then assign it the input domain.
8502 * In other cases, the domain is stored in the individual elements.
8504 static __isl_give isl_multi_union_pw_aff *
8505 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8506 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8508 int i, n;
8509 isl_space *space;
8510 isl_multi_union_pw_aff *mupa;
8512 if (!domain || !mv)
8513 goto error;
8515 n = isl_multi_val_dim(mv, isl_dim_set);
8516 space = isl_multi_val_get_space(mv);
8517 mupa = isl_multi_union_pw_aff_alloc(space);
8518 for (i = 0; i < n; ++i) {
8519 isl_val *v;
8520 isl_union_pw_aff *upa;
8522 v = isl_multi_val_get_val(mv, i);
8523 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8525 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8527 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8528 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8529 isl_union_set_copy(domain));
8531 isl_union_set_free(domain);
8532 isl_multi_val_free(mv);
8533 return mupa;
8534 error:
8535 isl_union_set_free(domain);
8536 isl_multi_val_free(mv);
8537 return NULL;
8540 /* Return a multiple union piecewise affine expression
8541 * that is equal to "mv" on "domain".
8543 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8544 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8546 isl_bool equal_params;
8548 if (!domain || !mv)
8549 goto error;
8550 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8551 if (equal_params < 0)
8552 goto error;
8553 if (equal_params)
8554 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8555 domain, mv);
8556 domain = isl_union_set_align_params(domain,
8557 isl_multi_val_get_space(mv));
8558 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8559 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8560 error:
8561 isl_union_set_free(domain);
8562 isl_multi_val_free(mv);
8563 return NULL;
8566 /* Return a multiple union piecewise affine expression
8567 * that is equal to "ma" on "domain".
8569 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8570 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8572 isl_pw_multi_aff *pma;
8574 pma = isl_pw_multi_aff_from_multi_aff(ma);
8575 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8578 /* Return a multiple union piecewise affine expression
8579 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8580 * have been aligned.
8582 * If the resulting multi union piecewise affine expression has
8583 * an explicit domain, then assign it the input domain.
8584 * In other cases, the domain is stored in the individual elements.
8586 static __isl_give isl_multi_union_pw_aff *
8587 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8588 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8590 int i, n;
8591 isl_space *space;
8592 isl_multi_union_pw_aff *mupa;
8594 if (!domain || !pma)
8595 goto error;
8597 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8598 space = isl_pw_multi_aff_get_space(pma);
8599 mupa = isl_multi_union_pw_aff_alloc(space);
8600 for (i = 0; i < n; ++i) {
8601 isl_pw_aff *pa;
8602 isl_union_pw_aff *upa;
8604 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8605 upa = isl_union_pw_aff_pw_aff_on_domain(
8606 isl_union_set_copy(domain), pa);
8607 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8609 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8610 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8611 isl_union_set_copy(domain));
8613 isl_union_set_free(domain);
8614 isl_pw_multi_aff_free(pma);
8615 return mupa;
8616 error:
8617 isl_union_set_free(domain);
8618 isl_pw_multi_aff_free(pma);
8619 return NULL;
8622 /* Return a multiple union piecewise affine expression
8623 * that is equal to "pma" on "domain".
8625 __isl_give isl_multi_union_pw_aff *
8626 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8627 __isl_take isl_pw_multi_aff *pma)
8629 isl_bool equal_params;
8630 isl_space *space;
8632 space = isl_pw_multi_aff_peek_space(pma);
8633 equal_params = isl_union_set_space_has_equal_params(domain, space);
8634 if (equal_params < 0)
8635 goto error;
8636 if (equal_params)
8637 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8638 domain, pma);
8639 domain = isl_union_set_align_params(domain,
8640 isl_pw_multi_aff_get_space(pma));
8641 pma = isl_pw_multi_aff_align_params(pma,
8642 isl_union_set_get_space(domain));
8643 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8644 pma);
8645 error:
8646 isl_union_set_free(domain);
8647 isl_pw_multi_aff_free(pma);
8648 return NULL;
8651 /* Return a union set containing those elements in the domains
8652 * of the elements of "mupa" where they are all zero.
8654 * If there are no elements, then simply return the entire domain.
8656 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8657 __isl_take isl_multi_union_pw_aff *mupa)
8659 int i, n;
8660 isl_union_pw_aff *upa;
8661 isl_union_set *zero;
8663 if (!mupa)
8664 return NULL;
8666 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8667 if (n == 0)
8668 return isl_multi_union_pw_aff_domain(mupa);
8670 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8671 zero = isl_union_pw_aff_zero_union_set(upa);
8673 for (i = 1; i < n; ++i) {
8674 isl_union_set *zero_i;
8676 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8677 zero_i = isl_union_pw_aff_zero_union_set(upa);
8679 zero = isl_union_set_intersect(zero, zero_i);
8682 isl_multi_union_pw_aff_free(mupa);
8683 return zero;
8686 /* Construct a union map mapping the shared domain
8687 * of the union piecewise affine expressions to the range of "mupa"
8688 * in the special case of a 0D multi union piecewise affine expression.
8690 * Construct a map between the explicit domain of "mupa" and
8691 * the range space.
8692 * Note that this assumes that the domain consists of explicit elements.
8694 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8695 __isl_take isl_multi_union_pw_aff *mupa)
8697 isl_bool is_params;
8698 isl_space *space;
8699 isl_union_set *dom, *ran;
8701 space = isl_multi_union_pw_aff_get_space(mupa);
8702 dom = isl_multi_union_pw_aff_domain(mupa);
8703 ran = isl_union_set_from_set(isl_set_universe(space));
8705 is_params = isl_union_set_is_params(dom);
8706 if (is_params < 0)
8707 dom = isl_union_set_free(dom);
8708 else if (is_params)
8709 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8710 "cannot create union map from expression without "
8711 "explicit domain elements",
8712 dom = isl_union_set_free(dom));
8714 return isl_union_map_from_domain_and_range(dom, ran);
8717 /* Construct a union map mapping the shared domain
8718 * of the union piecewise affine expressions to the range of "mupa"
8719 * with each dimension in the range equated to the
8720 * corresponding union piecewise affine expression.
8722 * If the input is zero-dimensional, then construct a mapping
8723 * from its explicit domain.
8725 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8726 __isl_take isl_multi_union_pw_aff *mupa)
8728 int i, n;
8729 isl_space *space;
8730 isl_union_map *umap;
8731 isl_union_pw_aff *upa;
8733 if (!mupa)
8734 return NULL;
8736 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8737 if (n == 0)
8738 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8740 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8741 umap = isl_union_map_from_union_pw_aff(upa);
8743 for (i = 1; i < n; ++i) {
8744 isl_union_map *umap_i;
8746 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8747 umap_i = isl_union_map_from_union_pw_aff(upa);
8748 umap = isl_union_map_flat_range_product(umap, umap_i);
8751 space = isl_multi_union_pw_aff_get_space(mupa);
8752 umap = isl_union_map_reset_range_space(umap, space);
8754 isl_multi_union_pw_aff_free(mupa);
8755 return umap;
8758 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8759 * "range" is the space from which to set the range space.
8760 * "res" collects the results.
8762 struct isl_union_pw_multi_aff_reset_range_space_data {
8763 isl_space *range;
8764 isl_union_pw_multi_aff *res;
8767 /* Replace the range space of "pma" by the range space of data->range and
8768 * add the result to data->res.
8770 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8772 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8773 isl_space *space;
8775 space = isl_pw_multi_aff_get_space(pma);
8776 space = isl_space_domain(space);
8777 space = isl_space_extend_domain_with_range(space,
8778 isl_space_copy(data->range));
8779 pma = isl_pw_multi_aff_reset_space(pma, space);
8780 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8782 return data->res ? isl_stat_ok : isl_stat_error;
8785 /* Replace the range space of all the piecewise affine expressions in "upma" by
8786 * the range space of "space".
8788 * This assumes that all these expressions have the same output dimension.
8790 * Since the spaces of the expressions change, so do their hash values.
8791 * We therefore need to create a new isl_union_pw_multi_aff.
8792 * Note that the hash value is currently computed based on the entire
8793 * space even though there can only be a single expression with a given
8794 * domain space.
8796 static __isl_give isl_union_pw_multi_aff *
8797 isl_union_pw_multi_aff_reset_range_space(
8798 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8800 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8801 isl_space *space_upma;
8803 space_upma = isl_union_pw_multi_aff_get_space(upma);
8804 data.res = isl_union_pw_multi_aff_empty(space_upma);
8805 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8806 &reset_range_space, &data) < 0)
8807 data.res = isl_union_pw_multi_aff_free(data.res);
8809 isl_space_free(space);
8810 isl_union_pw_multi_aff_free(upma);
8811 return data.res;
8814 /* Construct and return a union piecewise multi affine expression
8815 * that is equal to the given multi union piecewise affine expression,
8816 * in the special case of a 0D multi union piecewise affine expression.
8818 * Construct a union piecewise multi affine expression
8819 * on top of the explicit domain of the input.
8821 __isl_give isl_union_pw_multi_aff *
8822 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8823 __isl_take isl_multi_union_pw_aff *mupa)
8825 isl_space *space;
8826 isl_multi_val *mv;
8827 isl_union_set *domain;
8829 space = isl_multi_union_pw_aff_get_space(mupa);
8830 mv = isl_multi_val_zero(space);
8831 domain = isl_multi_union_pw_aff_domain(mupa);
8832 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8835 /* Construct and return a union piecewise multi affine expression
8836 * that is equal to the given multi union piecewise affine expression.
8838 * If the input is zero-dimensional, then
8839 * construct a union piecewise multi affine expression
8840 * on top of the explicit domain of the input.
8842 __isl_give isl_union_pw_multi_aff *
8843 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8844 __isl_take isl_multi_union_pw_aff *mupa)
8846 int i, n;
8847 isl_space *space;
8848 isl_union_pw_multi_aff *upma;
8849 isl_union_pw_aff *upa;
8851 if (!mupa)
8852 return NULL;
8854 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8855 if (n == 0)
8856 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8858 space = isl_multi_union_pw_aff_get_space(mupa);
8859 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8860 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8862 for (i = 1; i < n; ++i) {
8863 isl_union_pw_multi_aff *upma_i;
8865 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8866 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8867 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8870 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8872 isl_multi_union_pw_aff_free(mupa);
8873 return upma;
8876 /* Intersect the range of "mupa" with "range",
8877 * in the special case where "mupa" is 0D.
8879 * Intersect the domain of "mupa" with the constraints on the parameters
8880 * of "range".
8882 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8883 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8885 range = isl_set_params(range);
8886 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8887 return mupa;
8890 /* Intersect the range of "mupa" with "range".
8891 * That is, keep only those domain elements that have a function value
8892 * in "range".
8894 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8895 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8897 isl_union_pw_multi_aff *upma;
8898 isl_union_set *domain;
8899 isl_space *space;
8900 int n;
8901 int match;
8903 if (!mupa || !range)
8904 goto error;
8906 space = isl_set_get_space(range);
8907 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8908 space, isl_dim_set);
8909 isl_space_free(space);
8910 if (match < 0)
8911 goto error;
8912 if (!match)
8913 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8914 "space don't match", goto error);
8915 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8916 if (n == 0)
8917 return mupa_intersect_range_0D(mupa, range);
8919 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8920 isl_multi_union_pw_aff_copy(mupa));
8921 domain = isl_union_set_from_set(range);
8922 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8923 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8925 return mupa;
8926 error:
8927 isl_multi_union_pw_aff_free(mupa);
8928 isl_set_free(range);
8929 return NULL;
8932 /* Return the shared domain of the elements of "mupa",
8933 * in the special case where "mupa" is zero-dimensional.
8935 * Return the explicit domain of "mupa".
8936 * Note that this domain may be a parameter set, either
8937 * because "mupa" is meant to live in a set space or
8938 * because no explicit domain has been set.
8940 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8941 __isl_take isl_multi_union_pw_aff *mupa)
8943 isl_union_set *dom;
8945 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8946 isl_multi_union_pw_aff_free(mupa);
8948 return dom;
8951 /* Return the shared domain of the elements of "mupa".
8953 * If "mupa" is zero-dimensional, then return its explicit domain.
8955 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8956 __isl_take isl_multi_union_pw_aff *mupa)
8958 int i, n;
8959 isl_union_pw_aff *upa;
8960 isl_union_set *dom;
8962 if (!mupa)
8963 return NULL;
8965 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8966 if (n == 0)
8967 return isl_multi_union_pw_aff_domain_0D(mupa);
8969 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8970 dom = isl_union_pw_aff_domain(upa);
8971 for (i = 1; i < n; ++i) {
8972 isl_union_set *dom_i;
8974 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8975 dom_i = isl_union_pw_aff_domain(upa);
8976 dom = isl_union_set_intersect(dom, dom_i);
8979 isl_multi_union_pw_aff_free(mupa);
8980 return dom;
8983 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8984 * In particular, the spaces have been aligned.
8985 * The result is defined over the shared domain of the elements of "mupa"
8987 * We first extract the parametric constant part of "aff" and
8988 * define that over the shared domain.
8989 * Then we iterate over all input dimensions of "aff" and add the corresponding
8990 * multiples of the elements of "mupa".
8991 * Finally, we consider the integer divisions, calling the function
8992 * recursively to obtain an isl_union_pw_aff corresponding to the
8993 * integer division argument.
8995 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8996 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8998 int i, n_in, n_div;
8999 isl_union_pw_aff *upa;
9000 isl_union_set *uset;
9001 isl_val *v;
9002 isl_aff *cst;
9004 n_in = isl_aff_dim(aff, isl_dim_in);
9005 n_div = isl_aff_dim(aff, isl_dim_div);
9007 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9008 cst = isl_aff_copy(aff);
9009 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9010 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9011 cst = isl_aff_project_domain_on_params(cst);
9012 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9014 for (i = 0; i < n_in; ++i) {
9015 isl_union_pw_aff *upa_i;
9017 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9018 continue;
9019 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9020 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9021 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9022 upa = isl_union_pw_aff_add(upa, upa_i);
9025 for (i = 0; i < n_div; ++i) {
9026 isl_aff *div;
9027 isl_union_pw_aff *upa_i;
9029 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9030 continue;
9031 div = isl_aff_get_div(aff, i);
9032 upa_i = multi_union_pw_aff_apply_aff(
9033 isl_multi_union_pw_aff_copy(mupa), div);
9034 upa_i = isl_union_pw_aff_floor(upa_i);
9035 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9036 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9037 upa = isl_union_pw_aff_add(upa, upa_i);
9040 isl_multi_union_pw_aff_free(mupa);
9041 isl_aff_free(aff);
9043 return upa;
9046 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9047 * with the domain of "aff".
9048 * Furthermore, the dimension of this space needs to be greater than zero.
9049 * The result is defined over the shared domain of the elements of "mupa"
9051 * We perform these checks and then hand over control to
9052 * multi_union_pw_aff_apply_aff.
9054 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9055 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9057 isl_space *space1, *space2;
9058 int equal;
9060 mupa = isl_multi_union_pw_aff_align_params(mupa,
9061 isl_aff_get_space(aff));
9062 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9063 if (!mupa || !aff)
9064 goto error;
9066 space1 = isl_multi_union_pw_aff_get_space(mupa);
9067 space2 = isl_aff_get_domain_space(aff);
9068 equal = isl_space_is_equal(space1, space2);
9069 isl_space_free(space1);
9070 isl_space_free(space2);
9071 if (equal < 0)
9072 goto error;
9073 if (!equal)
9074 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9075 "spaces don't match", goto error);
9076 if (isl_aff_dim(aff, isl_dim_in) == 0)
9077 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9078 "cannot determine domains", goto error);
9080 return multi_union_pw_aff_apply_aff(mupa, aff);
9081 error:
9082 isl_multi_union_pw_aff_free(mupa);
9083 isl_aff_free(aff);
9084 return NULL;
9087 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9088 * The space of "mupa" is known to be compatible with the domain of "ma".
9090 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9091 * on the domain of "mupa".
9093 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9094 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9096 isl_union_set *dom;
9098 dom = isl_multi_union_pw_aff_domain(mupa);
9099 ma = isl_multi_aff_project_domain_on_params(ma);
9101 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9104 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9105 * with the domain of "ma".
9106 * The result is defined over the shared domain of the elements of "mupa"
9108 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9109 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9111 isl_space *space1, *space2;
9112 isl_multi_union_pw_aff *res;
9113 int equal;
9114 int i, n_out;
9116 mupa = isl_multi_union_pw_aff_align_params(mupa,
9117 isl_multi_aff_get_space(ma));
9118 ma = isl_multi_aff_align_params(ma,
9119 isl_multi_union_pw_aff_get_space(mupa));
9120 if (!mupa || !ma)
9121 goto error;
9123 space1 = isl_multi_union_pw_aff_get_space(mupa);
9124 space2 = isl_multi_aff_get_domain_space(ma);
9125 equal = isl_space_is_equal(space1, space2);
9126 isl_space_free(space1);
9127 isl_space_free(space2);
9128 if (equal < 0)
9129 goto error;
9130 if (!equal)
9131 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9132 "spaces don't match", goto error);
9133 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9134 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9135 return mupa_apply_multi_aff_0D(mupa, ma);
9137 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9138 res = isl_multi_union_pw_aff_alloc(space1);
9140 for (i = 0; i < n_out; ++i) {
9141 isl_aff *aff;
9142 isl_union_pw_aff *upa;
9144 aff = isl_multi_aff_get_aff(ma, i);
9145 upa = multi_union_pw_aff_apply_aff(
9146 isl_multi_union_pw_aff_copy(mupa), aff);
9147 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9150 isl_multi_aff_free(ma);
9151 isl_multi_union_pw_aff_free(mupa);
9152 return res;
9153 error:
9154 isl_multi_union_pw_aff_free(mupa);
9155 isl_multi_aff_free(ma);
9156 return NULL;
9159 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9160 * The space of "mupa" is known to be compatible with the domain of "pa".
9162 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9163 * on the domain of "mupa".
9165 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9166 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9168 isl_union_set *dom;
9170 dom = isl_multi_union_pw_aff_domain(mupa);
9171 pa = isl_pw_aff_project_domain_on_params(pa);
9173 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9176 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9177 * with the domain of "pa".
9178 * Furthermore, the dimension of this space needs to be greater than zero.
9179 * The result is defined over the shared domain of the elements of "mupa"
9181 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9182 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9184 int i;
9185 int equal;
9186 isl_space *space, *space2;
9187 isl_union_pw_aff *upa;
9189 mupa = isl_multi_union_pw_aff_align_params(mupa,
9190 isl_pw_aff_get_space(pa));
9191 pa = isl_pw_aff_align_params(pa,
9192 isl_multi_union_pw_aff_get_space(mupa));
9193 if (!mupa || !pa)
9194 goto error;
9196 space = isl_multi_union_pw_aff_get_space(mupa);
9197 space2 = isl_pw_aff_get_domain_space(pa);
9198 equal = isl_space_is_equal(space, space2);
9199 isl_space_free(space);
9200 isl_space_free(space2);
9201 if (equal < 0)
9202 goto error;
9203 if (!equal)
9204 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9205 "spaces don't match", goto error);
9206 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9207 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9209 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9210 upa = isl_union_pw_aff_empty(space);
9212 for (i = 0; i < pa->n; ++i) {
9213 isl_aff *aff;
9214 isl_set *domain;
9215 isl_multi_union_pw_aff *mupa_i;
9216 isl_union_pw_aff *upa_i;
9218 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9219 domain = isl_set_copy(pa->p[i].set);
9220 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9221 aff = isl_aff_copy(pa->p[i].aff);
9222 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9223 upa = isl_union_pw_aff_union_add(upa, upa_i);
9226 isl_multi_union_pw_aff_free(mupa);
9227 isl_pw_aff_free(pa);
9228 return upa;
9229 error:
9230 isl_multi_union_pw_aff_free(mupa);
9231 isl_pw_aff_free(pa);
9232 return NULL;
9235 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9236 * The space of "mupa" is known to be compatible with the domain of "pma".
9238 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9239 * on the domain of "mupa".
9241 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9242 __isl_take isl_multi_union_pw_aff *mupa,
9243 __isl_take isl_pw_multi_aff *pma)
9245 isl_union_set *dom;
9247 dom = isl_multi_union_pw_aff_domain(mupa);
9248 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9250 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9253 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9254 * with the domain of "pma".
9255 * The result is defined over the shared domain of the elements of "mupa"
9257 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9258 __isl_take isl_multi_union_pw_aff *mupa,
9259 __isl_take isl_pw_multi_aff *pma)
9261 isl_space *space1, *space2;
9262 isl_multi_union_pw_aff *res;
9263 int equal;
9264 int i, n_out;
9266 mupa = isl_multi_union_pw_aff_align_params(mupa,
9267 isl_pw_multi_aff_get_space(pma));
9268 pma = isl_pw_multi_aff_align_params(pma,
9269 isl_multi_union_pw_aff_get_space(mupa));
9270 if (!mupa || !pma)
9271 goto error;
9273 space1 = isl_multi_union_pw_aff_get_space(mupa);
9274 space2 = isl_pw_multi_aff_get_domain_space(pma);
9275 equal = isl_space_is_equal(space1, space2);
9276 isl_space_free(space1);
9277 isl_space_free(space2);
9278 if (equal < 0)
9279 goto error;
9280 if (!equal)
9281 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9282 "spaces don't match", goto error);
9283 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9284 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9285 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9287 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9288 res = isl_multi_union_pw_aff_alloc(space1);
9290 for (i = 0; i < n_out; ++i) {
9291 isl_pw_aff *pa;
9292 isl_union_pw_aff *upa;
9294 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9295 upa = isl_multi_union_pw_aff_apply_pw_aff(
9296 isl_multi_union_pw_aff_copy(mupa), pa);
9297 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9300 isl_pw_multi_aff_free(pma);
9301 isl_multi_union_pw_aff_free(mupa);
9302 return res;
9303 error:
9304 isl_multi_union_pw_aff_free(mupa);
9305 isl_pw_multi_aff_free(pma);
9306 return NULL;
9309 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9310 * If the explicit domain only keeps track of constraints on the parameters,
9311 * then only update those constraints.
9313 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9314 __isl_take isl_multi_union_pw_aff *mupa,
9315 __isl_keep isl_union_pw_multi_aff *upma)
9317 isl_bool is_params;
9319 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9320 return isl_multi_union_pw_aff_free(mupa);
9322 mupa = isl_multi_union_pw_aff_cow(mupa);
9323 if (!mupa)
9324 return NULL;
9326 is_params = isl_union_set_is_params(mupa->u.dom);
9327 if (is_params < 0)
9328 return isl_multi_union_pw_aff_free(mupa);
9330 upma = isl_union_pw_multi_aff_copy(upma);
9331 if (is_params)
9332 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9333 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9334 else
9335 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9336 mupa->u.dom, upma);
9337 if (!mupa->u.dom)
9338 return isl_multi_union_pw_aff_free(mupa);
9339 return mupa;
9342 /* Compute the pullback of "mupa" by the function represented by "upma".
9343 * In other words, plug in "upma" in "mupa". The result contains
9344 * expressions defined over the domain space of "upma".
9346 * Run over all elements of "mupa" and plug in "upma" in each of them.
9348 * If "mupa" has an explicit domain, then it is this domain
9349 * that needs to undergo a pullback instead, i.e., a preimage.
9351 __isl_give isl_multi_union_pw_aff *
9352 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9353 __isl_take isl_multi_union_pw_aff *mupa,
9354 __isl_take isl_union_pw_multi_aff *upma)
9356 int i, n;
9358 mupa = isl_multi_union_pw_aff_align_params(mupa,
9359 isl_union_pw_multi_aff_get_space(upma));
9360 upma = isl_union_pw_multi_aff_align_params(upma,
9361 isl_multi_union_pw_aff_get_space(mupa));
9362 mupa = isl_multi_union_pw_aff_cow(mupa);
9363 if (!mupa || !upma)
9364 goto error;
9366 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9367 for (i = 0; i < n; ++i) {
9368 isl_union_pw_aff *upa;
9370 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9371 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9372 isl_union_pw_multi_aff_copy(upma));
9373 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9376 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9377 mupa = preimage_explicit_domain(mupa, upma);
9379 isl_union_pw_multi_aff_free(upma);
9380 return mupa;
9381 error:
9382 isl_multi_union_pw_aff_free(mupa);
9383 isl_union_pw_multi_aff_free(upma);
9384 return NULL;
9387 /* Extract the sequence of elements in "mupa" with domain space "space"
9388 * (ignoring parameters).
9390 * For the elements of "mupa" that are not defined on the specified space,
9391 * the corresponding element in the result is empty.
9393 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9394 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9396 int i, n;
9397 isl_space *space_mpa;
9398 isl_multi_pw_aff *mpa;
9400 if (!mupa || !space)
9401 goto error;
9403 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9404 space = isl_space_replace_params(space, space_mpa);
9405 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9406 space_mpa);
9407 mpa = isl_multi_pw_aff_alloc(space_mpa);
9409 space = isl_space_from_domain(space);
9410 space = isl_space_add_dims(space, isl_dim_out, 1);
9411 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9412 for (i = 0; i < n; ++i) {
9413 isl_union_pw_aff *upa;
9414 isl_pw_aff *pa;
9416 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9417 pa = isl_union_pw_aff_extract_pw_aff(upa,
9418 isl_space_copy(space));
9419 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9420 isl_union_pw_aff_free(upa);
9423 isl_space_free(space);
9424 return mpa;
9425 error:
9426 isl_space_free(space);
9427 return NULL;
9430 /* Evaluate the affine function "aff" in the void point "pnt".
9431 * In particular, return the value NaN.
9433 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9434 __isl_take isl_point *pnt)
9436 isl_ctx *ctx;
9438 ctx = isl_point_get_ctx(pnt);
9439 isl_aff_free(aff);
9440 isl_point_free(pnt);
9441 return isl_val_nan(ctx);
9444 /* Evaluate the affine expression "aff"
9445 * in the coordinates (with denominator) "pnt".
9447 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9448 __isl_keep isl_vec *pnt)
9450 isl_int n, d;
9451 isl_ctx *ctx;
9452 isl_val *v;
9454 if (!aff || !pnt)
9455 return NULL;
9457 ctx = isl_vec_get_ctx(aff);
9458 isl_int_init(n);
9459 isl_int_init(d);
9460 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9461 isl_int_mul(d, aff->el[0], pnt->el[0]);
9462 v = isl_val_rat_from_isl_int(ctx, n, d);
9463 v = isl_val_normalize(v);
9464 isl_int_clear(n);
9465 isl_int_clear(d);
9467 return v;
9470 /* Check that the domain space of "aff" is equal to "space".
9472 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9473 __isl_keep isl_space *space)
9475 isl_bool ok;
9477 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9478 if (ok < 0)
9479 return isl_stat_error;
9480 if (!ok)
9481 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9482 "incompatible spaces", return isl_stat_error);
9483 return isl_stat_ok;
9486 /* Evaluate the affine function "aff" in "pnt".
9488 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9489 __isl_take isl_point *pnt)
9491 isl_bool is_void;
9492 isl_val *v;
9493 isl_local_space *ls;
9495 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9496 goto error;
9497 is_void = isl_point_is_void(pnt);
9498 if (is_void < 0)
9499 goto error;
9500 if (is_void)
9501 return eval_void(aff, pnt);
9503 ls = isl_aff_get_domain_local_space(aff);
9504 pnt = isl_local_space_lift_point(ls, pnt);
9506 v = eval(aff->v, isl_point_peek_vec(pnt));
9508 isl_aff_free(aff);
9509 isl_point_free(pnt);
9511 return v;
9512 error:
9513 isl_aff_free(aff);
9514 isl_point_free(pnt);
9515 return NULL;