isl_pw_*_eval: rename "pnt_dim" variable to "pnt_space"
[isl.git] / isl_aff.c
blobb27d2d465a27d47aba9c6ddd9e97c10271bfd1e8
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #define ISL_DIM_H
19 #include <isl_map_private.h>
20 #include <isl_union_map_private.h>
21 #include <isl_aff_private.h>
22 #include <isl_space_private.h>
23 #include <isl_local_space_private.h>
24 #include <isl_vec_private.h>
25 #include <isl_mat_private.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl_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_vec *res;
496 int i;
498 if (!vec || !r)
499 goto error;
501 res = isl_vec_alloc(vec->ctx,
502 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
503 if (!res)
504 goto error;
505 isl_seq_cpy(res->el, vec->el, 2);
506 isl_seq_clr(res->el + 2, res->size - 2);
507 for (i = 0; i < r->len; ++i)
508 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
510 isl_reordering_free(r);
511 isl_vec_free(vec);
512 return res;
513 error:
514 isl_vec_free(vec);
515 isl_reordering_free(r);
516 return NULL;
519 /* Reorder the dimensions of the domain of "aff" according
520 * to the given reordering.
522 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
523 __isl_take isl_reordering *r)
525 aff = isl_aff_cow(aff);
526 if (!aff)
527 goto error;
529 r = isl_reordering_extend(r, aff->ls->div->n_row);
530 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
531 aff->ls->div->n_row);
532 aff->ls = isl_local_space_realign(aff->ls, r);
534 if (!aff->v || !aff->ls)
535 return isl_aff_free(aff);
537 return aff;
538 error:
539 isl_aff_free(aff);
540 isl_reordering_free(r);
541 return NULL;
544 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
545 __isl_take isl_space *model)
547 isl_bool equal_params;
549 if (!aff || !model)
550 goto error;
552 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
553 if (equal_params < 0)
554 goto error;
555 if (!equal_params) {
556 isl_reordering *exp;
558 model = isl_space_drop_dims(model, isl_dim_in,
559 0, isl_space_dim(model, isl_dim_in));
560 model = isl_space_drop_dims(model, isl_dim_out,
561 0, isl_space_dim(model, isl_dim_out));
562 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
563 exp = isl_reordering_extend_space(exp,
564 isl_aff_get_domain_space(aff));
565 aff = isl_aff_realign_domain(aff, exp);
568 isl_space_free(model);
569 return aff;
570 error:
571 isl_space_free(model);
572 isl_aff_free(aff);
573 return NULL;
576 /* Is "aff" obviously equal to zero?
578 * If the denominator is zero, then "aff" is not equal to zero.
580 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
582 if (!aff)
583 return isl_bool_error;
585 if (isl_int_is_zero(aff->v->el[0]))
586 return isl_bool_false;
587 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
590 /* Does "aff" represent NaN?
592 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
594 if (!aff)
595 return isl_bool_error;
597 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
600 /* Are "aff1" and "aff2" obviously equal?
602 * NaN is not equal to anything, not even to another NaN.
604 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
605 __isl_keep isl_aff *aff2)
607 isl_bool equal;
609 if (!aff1 || !aff2)
610 return isl_bool_error;
612 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
613 return isl_bool_false;
615 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
616 if (equal < 0 || !equal)
617 return equal;
619 return isl_vec_is_equal(aff1->v, aff2->v);
622 /* Return the common denominator of "aff" in "v".
624 * We cannot return anything meaningful in case of a NaN.
626 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
628 if (!aff)
629 return isl_stat_error;
630 if (isl_aff_is_nan(aff))
631 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
632 "cannot get denominator of NaN", return isl_stat_error);
633 isl_int_set(*v, aff->v->el[0]);
634 return isl_stat_ok;
637 /* Return the common denominator of "aff".
639 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
641 isl_ctx *ctx;
643 if (!aff)
644 return NULL;
646 ctx = isl_aff_get_ctx(aff);
647 if (isl_aff_is_nan(aff))
648 return isl_val_nan(ctx);
649 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
652 /* Return the constant term of "aff".
654 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
656 isl_ctx *ctx;
657 isl_val *v;
659 if (!aff)
660 return NULL;
662 ctx = isl_aff_get_ctx(aff);
663 if (isl_aff_is_nan(aff))
664 return isl_val_nan(ctx);
665 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
666 return isl_val_normalize(v);
669 /* Return the coefficient of the variable of type "type" at position "pos"
670 * of "aff".
672 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
673 enum isl_dim_type type, int pos)
675 isl_ctx *ctx;
676 isl_val *v;
678 if (!aff)
679 return NULL;
681 ctx = isl_aff_get_ctx(aff);
682 if (type == isl_dim_out)
683 isl_die(ctx, isl_error_invalid,
684 "output/set dimension does not have a coefficient",
685 return NULL);
686 if (type == isl_dim_in)
687 type = isl_dim_set;
689 if (pos >= isl_local_space_dim(aff->ls, type))
690 isl_die(ctx, isl_error_invalid,
691 "position out of bounds", return NULL);
693 if (isl_aff_is_nan(aff))
694 return isl_val_nan(ctx);
695 pos += isl_local_space_offset(aff->ls, type);
696 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
697 return isl_val_normalize(v);
700 /* Return the sign of the coefficient of the variable of type "type"
701 * at position "pos" of "aff".
703 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
704 int pos)
706 isl_ctx *ctx;
708 if (!aff)
709 return 0;
711 ctx = isl_aff_get_ctx(aff);
712 if (type == isl_dim_out)
713 isl_die(ctx, isl_error_invalid,
714 "output/set dimension does not have a coefficient",
715 return 0);
716 if (type == isl_dim_in)
717 type = isl_dim_set;
719 if (pos >= isl_local_space_dim(aff->ls, type))
720 isl_die(ctx, isl_error_invalid,
721 "position out of bounds", return 0);
723 pos += isl_local_space_offset(aff->ls, type);
724 return isl_int_sgn(aff->v->el[1 + pos]);
727 /* Replace the numerator of the constant term of "aff" by "v".
729 * A NaN is unaffected by this operation.
731 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
733 if (!aff)
734 return NULL;
735 if (isl_aff_is_nan(aff))
736 return aff;
737 aff = isl_aff_cow(aff);
738 if (!aff)
739 return NULL;
741 aff->v = isl_vec_cow(aff->v);
742 if (!aff->v)
743 return isl_aff_free(aff);
745 isl_int_set(aff->v->el[1], v);
747 return aff;
750 /* Replace the constant term of "aff" by "v".
752 * A NaN is unaffected by this operation.
754 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
755 __isl_take isl_val *v)
757 if (!aff || !v)
758 goto error;
760 if (isl_aff_is_nan(aff)) {
761 isl_val_free(v);
762 return aff;
765 if (!isl_val_is_rat(v))
766 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
767 "expecting rational value", goto error);
769 if (isl_int_eq(aff->v->el[1], v->n) &&
770 isl_int_eq(aff->v->el[0], v->d)) {
771 isl_val_free(v);
772 return aff;
775 aff = isl_aff_cow(aff);
776 if (!aff)
777 goto error;
778 aff->v = isl_vec_cow(aff->v);
779 if (!aff->v)
780 goto error;
782 if (isl_int_eq(aff->v->el[0], v->d)) {
783 isl_int_set(aff->v->el[1], v->n);
784 } else if (isl_int_is_one(v->d)) {
785 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
786 } else {
787 isl_seq_scale(aff->v->el + 1,
788 aff->v->el + 1, v->d, aff->v->size - 1);
789 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
790 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
791 aff->v = isl_vec_normalize(aff->v);
792 if (!aff->v)
793 goto error;
796 isl_val_free(v);
797 return aff;
798 error:
799 isl_aff_free(aff);
800 isl_val_free(v);
801 return NULL;
804 /* Add "v" to the constant term of "aff".
806 * A NaN is unaffected by this operation.
808 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
810 if (isl_int_is_zero(v))
811 return aff;
813 if (!aff)
814 return NULL;
815 if (isl_aff_is_nan(aff))
816 return aff;
817 aff = isl_aff_cow(aff);
818 if (!aff)
819 return NULL;
821 aff->v = isl_vec_cow(aff->v);
822 if (!aff->v)
823 return isl_aff_free(aff);
825 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
827 return aff;
830 /* Add "v" to the constant term of "aff".
832 * A NaN is unaffected by this operation.
834 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
835 __isl_take isl_val *v)
837 if (!aff || !v)
838 goto error;
840 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
841 isl_val_free(v);
842 return aff;
845 if (!isl_val_is_rat(v))
846 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
847 "expecting rational value", goto error);
849 aff = isl_aff_cow(aff);
850 if (!aff)
851 goto error;
853 aff->v = isl_vec_cow(aff->v);
854 if (!aff->v)
855 goto error;
857 if (isl_int_is_one(v->d)) {
858 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
859 } else if (isl_int_eq(aff->v->el[0], v->d)) {
860 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
861 aff->v = isl_vec_normalize(aff->v);
862 if (!aff->v)
863 goto error;
864 } else {
865 isl_seq_scale(aff->v->el + 1,
866 aff->v->el + 1, v->d, aff->v->size - 1);
867 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
868 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
869 aff->v = isl_vec_normalize(aff->v);
870 if (!aff->v)
871 goto error;
874 isl_val_free(v);
875 return aff;
876 error:
877 isl_aff_free(aff);
878 isl_val_free(v);
879 return NULL;
882 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
884 isl_int t;
886 isl_int_init(t);
887 isl_int_set_si(t, v);
888 aff = isl_aff_add_constant(aff, t);
889 isl_int_clear(t);
891 return aff;
894 /* Add "v" to the numerator of the constant term of "aff".
896 * A NaN is unaffected by this operation.
898 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
900 if (isl_int_is_zero(v))
901 return aff;
903 if (!aff)
904 return NULL;
905 if (isl_aff_is_nan(aff))
906 return aff;
907 aff = isl_aff_cow(aff);
908 if (!aff)
909 return NULL;
911 aff->v = isl_vec_cow(aff->v);
912 if (!aff->v)
913 return isl_aff_free(aff);
915 isl_int_add(aff->v->el[1], aff->v->el[1], v);
917 return aff;
920 /* Add "v" to the numerator of the constant term of "aff".
922 * A NaN is unaffected by this operation.
924 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
926 isl_int t;
928 if (v == 0)
929 return aff;
931 isl_int_init(t);
932 isl_int_set_si(t, v);
933 aff = isl_aff_add_constant_num(aff, t);
934 isl_int_clear(t);
936 return aff;
939 /* Replace the numerator of the constant term of "aff" by "v".
941 * A NaN is unaffected by this operation.
943 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
945 if (!aff)
946 return NULL;
947 if (isl_aff_is_nan(aff))
948 return aff;
949 aff = isl_aff_cow(aff);
950 if (!aff)
951 return NULL;
953 aff->v = isl_vec_cow(aff->v);
954 if (!aff->v)
955 return isl_aff_free(aff);
957 isl_int_set_si(aff->v->el[1], v);
959 return aff;
962 /* Replace the numerator of the coefficient of the variable of type "type"
963 * at position "pos" of "aff" by "v".
965 * A NaN is unaffected by this operation.
967 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
968 enum isl_dim_type type, int pos, isl_int v)
970 if (!aff)
971 return NULL;
973 if (type == isl_dim_out)
974 isl_die(aff->v->ctx, isl_error_invalid,
975 "output/set dimension does not have a coefficient",
976 return isl_aff_free(aff));
977 if (type == isl_dim_in)
978 type = isl_dim_set;
980 if (pos >= isl_local_space_dim(aff->ls, type))
981 isl_die(aff->v->ctx, isl_error_invalid,
982 "position out of bounds", return isl_aff_free(aff));
984 if (isl_aff_is_nan(aff))
985 return aff;
986 aff = isl_aff_cow(aff);
987 if (!aff)
988 return NULL;
990 aff->v = isl_vec_cow(aff->v);
991 if (!aff->v)
992 return isl_aff_free(aff);
994 pos += isl_local_space_offset(aff->ls, type);
995 isl_int_set(aff->v->el[1 + pos], v);
997 return aff;
1000 /* Replace the numerator of the coefficient of the variable of type "type"
1001 * at position "pos" of "aff" by "v".
1003 * A NaN is unaffected by this operation.
1005 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1006 enum isl_dim_type type, int pos, int v)
1008 if (!aff)
1009 return NULL;
1011 if (type == isl_dim_out)
1012 isl_die(aff->v->ctx, isl_error_invalid,
1013 "output/set dimension does not have a coefficient",
1014 return isl_aff_free(aff));
1015 if (type == isl_dim_in)
1016 type = isl_dim_set;
1018 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1019 isl_die(aff->v->ctx, isl_error_invalid,
1020 "position out of bounds", return isl_aff_free(aff));
1022 if (isl_aff_is_nan(aff))
1023 return aff;
1024 pos += isl_local_space_offset(aff->ls, type);
1025 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1026 return aff;
1028 aff = isl_aff_cow(aff);
1029 if (!aff)
1030 return NULL;
1032 aff->v = isl_vec_cow(aff->v);
1033 if (!aff->v)
1034 return isl_aff_free(aff);
1036 isl_int_set_si(aff->v->el[1 + pos], v);
1038 return aff;
1041 /* Replace the coefficient of the variable of type "type" at position "pos"
1042 * of "aff" by "v".
1044 * A NaN is unaffected by this operation.
1046 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1047 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1049 if (!aff || !v)
1050 goto error;
1052 if (type == isl_dim_out)
1053 isl_die(aff->v->ctx, isl_error_invalid,
1054 "output/set dimension does not have a coefficient",
1055 goto error);
1056 if (type == isl_dim_in)
1057 type = isl_dim_set;
1059 if (pos >= isl_local_space_dim(aff->ls, type))
1060 isl_die(aff->v->ctx, isl_error_invalid,
1061 "position out of bounds", goto error);
1063 if (isl_aff_is_nan(aff)) {
1064 isl_val_free(v);
1065 return aff;
1067 if (!isl_val_is_rat(v))
1068 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1069 "expecting rational value", goto error);
1071 pos += isl_local_space_offset(aff->ls, type);
1072 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1073 isl_int_eq(aff->v->el[0], v->d)) {
1074 isl_val_free(v);
1075 return aff;
1078 aff = isl_aff_cow(aff);
1079 if (!aff)
1080 goto error;
1081 aff->v = isl_vec_cow(aff->v);
1082 if (!aff->v)
1083 goto error;
1085 if (isl_int_eq(aff->v->el[0], v->d)) {
1086 isl_int_set(aff->v->el[1 + pos], v->n);
1087 } else if (isl_int_is_one(v->d)) {
1088 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1089 } else {
1090 isl_seq_scale(aff->v->el + 1,
1091 aff->v->el + 1, v->d, aff->v->size - 1);
1092 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1093 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1094 aff->v = isl_vec_normalize(aff->v);
1095 if (!aff->v)
1096 goto error;
1099 isl_val_free(v);
1100 return aff;
1101 error:
1102 isl_aff_free(aff);
1103 isl_val_free(v);
1104 return NULL;
1107 /* Add "v" to the coefficient of the variable of type "type"
1108 * at position "pos" of "aff".
1110 * A NaN is unaffected by this operation.
1112 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1113 enum isl_dim_type type, int pos, isl_int v)
1115 if (!aff)
1116 return NULL;
1118 if (type == isl_dim_out)
1119 isl_die(aff->v->ctx, isl_error_invalid,
1120 "output/set dimension does not have a coefficient",
1121 return isl_aff_free(aff));
1122 if (type == isl_dim_in)
1123 type = isl_dim_set;
1125 if (pos >= isl_local_space_dim(aff->ls, type))
1126 isl_die(aff->v->ctx, isl_error_invalid,
1127 "position out of bounds", return isl_aff_free(aff));
1129 if (isl_aff_is_nan(aff))
1130 return aff;
1131 aff = isl_aff_cow(aff);
1132 if (!aff)
1133 return NULL;
1135 aff->v = isl_vec_cow(aff->v);
1136 if (!aff->v)
1137 return isl_aff_free(aff);
1139 pos += isl_local_space_offset(aff->ls, type);
1140 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1142 return aff;
1145 /* Add "v" to the coefficient of the variable of type "type"
1146 * at position "pos" of "aff".
1148 * A NaN is unaffected by this operation.
1150 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1151 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1153 if (!aff || !v)
1154 goto error;
1156 if (isl_val_is_zero(v)) {
1157 isl_val_free(v);
1158 return aff;
1161 if (type == isl_dim_out)
1162 isl_die(aff->v->ctx, isl_error_invalid,
1163 "output/set dimension does not have a coefficient",
1164 goto error);
1165 if (type == isl_dim_in)
1166 type = isl_dim_set;
1168 if (pos >= isl_local_space_dim(aff->ls, type))
1169 isl_die(aff->v->ctx, isl_error_invalid,
1170 "position out of bounds", goto error);
1172 if (isl_aff_is_nan(aff)) {
1173 isl_val_free(v);
1174 return aff;
1176 if (!isl_val_is_rat(v))
1177 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1178 "expecting rational value", goto error);
1180 aff = isl_aff_cow(aff);
1181 if (!aff)
1182 goto error;
1184 aff->v = isl_vec_cow(aff->v);
1185 if (!aff->v)
1186 goto error;
1188 pos += isl_local_space_offset(aff->ls, type);
1189 if (isl_int_is_one(v->d)) {
1190 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1191 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1192 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1193 aff->v = isl_vec_normalize(aff->v);
1194 if (!aff->v)
1195 goto error;
1196 } else {
1197 isl_seq_scale(aff->v->el + 1,
1198 aff->v->el + 1, v->d, aff->v->size - 1);
1199 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1200 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1201 aff->v = isl_vec_normalize(aff->v);
1202 if (!aff->v)
1203 goto error;
1206 isl_val_free(v);
1207 return aff;
1208 error:
1209 isl_aff_free(aff);
1210 isl_val_free(v);
1211 return NULL;
1214 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1215 enum isl_dim_type type, int pos, int v)
1217 isl_int t;
1219 isl_int_init(t);
1220 isl_int_set_si(t, v);
1221 aff = isl_aff_add_coefficient(aff, type, pos, t);
1222 isl_int_clear(t);
1224 return aff;
1227 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1229 if (!aff)
1230 return NULL;
1232 return isl_local_space_get_div(aff->ls, pos);
1235 /* Return the negation of "aff".
1237 * As a special case, -NaN = NaN.
1239 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1241 if (!aff)
1242 return NULL;
1243 if (isl_aff_is_nan(aff))
1244 return aff;
1245 aff = isl_aff_cow(aff);
1246 if (!aff)
1247 return NULL;
1248 aff->v = isl_vec_cow(aff->v);
1249 if (!aff->v)
1250 return isl_aff_free(aff);
1252 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1254 return aff;
1257 /* Remove divs from the local space that do not appear in the affine
1258 * expression.
1259 * We currently only remove divs at the end.
1260 * Some intermediate divs may also not appear directly in the affine
1261 * expression, but we would also need to check that no other divs are
1262 * defined in terms of them.
1264 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1266 int pos;
1267 int off;
1268 int n;
1270 if (!aff)
1271 return NULL;
1273 n = isl_local_space_dim(aff->ls, isl_dim_div);
1274 off = isl_local_space_offset(aff->ls, isl_dim_div);
1276 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1277 if (pos == n)
1278 return aff;
1280 aff = isl_aff_cow(aff);
1281 if (!aff)
1282 return NULL;
1284 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1285 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1286 if (!aff->ls || !aff->v)
1287 return isl_aff_free(aff);
1289 return aff;
1292 /* Look for any divs in the aff->ls with a denominator equal to one
1293 * and plug them into the affine expression and any subsequent divs
1294 * that may reference the div.
1296 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1298 int i, n;
1299 int len;
1300 isl_int v;
1301 isl_vec *vec;
1302 isl_local_space *ls;
1303 unsigned pos;
1305 if (!aff)
1306 return NULL;
1308 n = isl_local_space_dim(aff->ls, isl_dim_div);
1309 len = aff->v->size;
1310 for (i = 0; i < n; ++i) {
1311 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1312 continue;
1313 ls = isl_local_space_copy(aff->ls);
1314 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1315 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1316 vec = isl_vec_copy(aff->v);
1317 vec = isl_vec_cow(vec);
1318 if (!ls || !vec)
1319 goto error;
1321 isl_int_init(v);
1323 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1324 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1325 len, len, v);
1327 isl_int_clear(v);
1329 isl_vec_free(aff->v);
1330 aff->v = vec;
1331 isl_local_space_free(aff->ls);
1332 aff->ls = ls;
1335 return aff;
1336 error:
1337 isl_vec_free(vec);
1338 isl_local_space_free(ls);
1339 return isl_aff_free(aff);
1342 /* Look for any divs j that appear with a unit coefficient inside
1343 * the definitions of other divs i and plug them into the definitions
1344 * of the divs i.
1346 * In particular, an expression of the form
1348 * floor((f(..) + floor(g(..)/n))/m)
1350 * is simplified to
1352 * floor((n * f(..) + g(..))/(n * m))
1354 * This simplification is correct because we can move the expression
1355 * f(..) into the inner floor in the original expression to obtain
1357 * floor(floor((n * f(..) + g(..))/n)/m)
1359 * from which we can derive the simplified expression.
1361 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1363 int i, j, n;
1364 int off;
1366 if (!aff)
1367 return NULL;
1369 n = isl_local_space_dim(aff->ls, isl_dim_div);
1370 off = isl_local_space_offset(aff->ls, isl_dim_div);
1371 for (i = 1; i < n; ++i) {
1372 for (j = 0; j < i; ++j) {
1373 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1374 continue;
1375 aff->ls = isl_local_space_substitute_seq(aff->ls,
1376 isl_dim_div, j, aff->ls->div->row[j],
1377 aff->v->size, i, 1);
1378 if (!aff->ls)
1379 return isl_aff_free(aff);
1383 return aff;
1386 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1388 * Even though this function is only called on isl_affs with a single
1389 * reference, we are careful to only change aff->v and aff->ls together.
1391 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1393 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1394 isl_local_space *ls;
1395 isl_vec *v;
1397 ls = isl_local_space_copy(aff->ls);
1398 ls = isl_local_space_swap_div(ls, a, b);
1399 v = isl_vec_copy(aff->v);
1400 v = isl_vec_cow(v);
1401 if (!ls || !v)
1402 goto error;
1404 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1405 isl_vec_free(aff->v);
1406 aff->v = v;
1407 isl_local_space_free(aff->ls);
1408 aff->ls = ls;
1410 return aff;
1411 error:
1412 isl_vec_free(v);
1413 isl_local_space_free(ls);
1414 return isl_aff_free(aff);
1417 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1419 * We currently do not actually remove div "b", but simply add its
1420 * coefficient to that of "a" and then zero it out.
1422 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1424 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1426 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1427 return aff;
1429 aff->v = isl_vec_cow(aff->v);
1430 if (!aff->v)
1431 return isl_aff_free(aff);
1433 isl_int_add(aff->v->el[1 + off + a],
1434 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1435 isl_int_set_si(aff->v->el[1 + off + b], 0);
1437 return aff;
1440 /* Sort the divs in the local space of "aff" according to
1441 * the comparison function "cmp_row" in isl_local_space.c,
1442 * combining the coefficients of identical divs.
1444 * Reordering divs does not change the semantics of "aff",
1445 * so there is no need to call isl_aff_cow.
1446 * Moreover, this function is currently only called on isl_affs
1447 * with a single reference.
1449 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1451 int i, j, n;
1453 if (!aff)
1454 return NULL;
1456 n = isl_aff_dim(aff, isl_dim_div);
1457 for (i = 1; i < n; ++i) {
1458 for (j = i - 1; j >= 0; --j) {
1459 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1460 if (cmp < 0)
1461 break;
1462 if (cmp == 0)
1463 aff = merge_divs(aff, j, j + 1);
1464 else
1465 aff = swap_div(aff, j, j + 1);
1466 if (!aff)
1467 return NULL;
1471 return aff;
1474 /* Normalize the representation of "aff".
1476 * This function should only be called of "new" isl_affs, i.e.,
1477 * with only a single reference. We therefore do not need to
1478 * worry about affecting other instances.
1480 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1482 if (!aff)
1483 return NULL;
1484 aff->v = isl_vec_normalize(aff->v);
1485 if (!aff->v)
1486 return isl_aff_free(aff);
1487 aff = plug_in_integral_divs(aff);
1488 aff = plug_in_unit_divs(aff);
1489 aff = sort_divs(aff);
1490 aff = isl_aff_remove_unused_divs(aff);
1491 return aff;
1494 /* Given f, return floor(f).
1495 * If f is an integer expression, then just return f.
1496 * If f is a constant, then return the constant floor(f).
1497 * Otherwise, if f = g/m, write g = q m + r,
1498 * create a new div d = [r/m] and return the expression q + d.
1499 * The coefficients in r are taken to lie between -m/2 and m/2.
1501 * reduce_div_coefficients performs the same normalization.
1503 * As a special case, floor(NaN) = NaN.
1505 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1507 int i;
1508 int size;
1509 isl_ctx *ctx;
1510 isl_vec *div;
1512 if (!aff)
1513 return NULL;
1515 if (isl_aff_is_nan(aff))
1516 return aff;
1517 if (isl_int_is_one(aff->v->el[0]))
1518 return aff;
1520 aff = isl_aff_cow(aff);
1521 if (!aff)
1522 return NULL;
1524 aff->v = isl_vec_cow(aff->v);
1525 if (!aff->v)
1526 return isl_aff_free(aff);
1528 if (isl_aff_is_cst(aff)) {
1529 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1530 isl_int_set_si(aff->v->el[0], 1);
1531 return aff;
1534 div = isl_vec_copy(aff->v);
1535 div = isl_vec_cow(div);
1536 if (!div)
1537 return isl_aff_free(aff);
1539 ctx = isl_aff_get_ctx(aff);
1540 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1541 for (i = 1; i < aff->v->size; ++i) {
1542 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1543 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1544 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1545 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1546 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1550 aff->ls = isl_local_space_add_div(aff->ls, div);
1551 if (!aff->ls)
1552 return isl_aff_free(aff);
1554 size = aff->v->size;
1555 aff->v = isl_vec_extend(aff->v, size + 1);
1556 if (!aff->v)
1557 return isl_aff_free(aff);
1558 isl_int_set_si(aff->v->el[0], 1);
1559 isl_int_set_si(aff->v->el[size], 1);
1561 aff = isl_aff_normalize(aff);
1563 return aff;
1566 /* Compute
1568 * aff mod m = aff - m * floor(aff/m)
1570 * with m an integer value.
1572 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1573 __isl_take isl_val *m)
1575 isl_aff *res;
1577 if (!aff || !m)
1578 goto error;
1580 if (!isl_val_is_int(m))
1581 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1582 "expecting integer modulo", goto error);
1584 res = isl_aff_copy(aff);
1585 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1586 aff = isl_aff_floor(aff);
1587 aff = isl_aff_scale_val(aff, m);
1588 res = isl_aff_sub(res, aff);
1590 return res;
1591 error:
1592 isl_aff_free(aff);
1593 isl_val_free(m);
1594 return NULL;
1597 /* Compute
1599 * pwaff mod m = pwaff - m * floor(pwaff/m)
1601 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1603 isl_pw_aff *res;
1605 res = isl_pw_aff_copy(pwaff);
1606 pwaff = isl_pw_aff_scale_down(pwaff, m);
1607 pwaff = isl_pw_aff_floor(pwaff);
1608 pwaff = isl_pw_aff_scale(pwaff, m);
1609 res = isl_pw_aff_sub(res, pwaff);
1611 return res;
1614 /* Compute
1616 * pa mod m = pa - m * floor(pa/m)
1618 * with m an integer value.
1620 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1621 __isl_take isl_val *m)
1623 if (!pa || !m)
1624 goto error;
1625 if (!isl_val_is_int(m))
1626 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1627 "expecting integer modulo", goto error);
1628 pa = isl_pw_aff_mod(pa, m->n);
1629 isl_val_free(m);
1630 return pa;
1631 error:
1632 isl_pw_aff_free(pa);
1633 isl_val_free(m);
1634 return NULL;
1637 /* Given f, return ceil(f).
1638 * If f is an integer expression, then just return f.
1639 * Otherwise, let f be the expression
1641 * e/m
1643 * then return
1645 * floor((e + m - 1)/m)
1647 * As a special case, ceil(NaN) = NaN.
1649 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1651 if (!aff)
1652 return NULL;
1654 if (isl_aff_is_nan(aff))
1655 return aff;
1656 if (isl_int_is_one(aff->v->el[0]))
1657 return aff;
1659 aff = isl_aff_cow(aff);
1660 if (!aff)
1661 return NULL;
1662 aff->v = isl_vec_cow(aff->v);
1663 if (!aff->v)
1664 return isl_aff_free(aff);
1666 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1667 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1668 aff = isl_aff_floor(aff);
1670 return aff;
1673 /* Apply the expansion computed by isl_merge_divs.
1674 * The expansion itself is given by "exp" while the resulting
1675 * list of divs is given by "div".
1677 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1678 __isl_take isl_mat *div, int *exp)
1680 int old_n_div;
1681 int new_n_div;
1682 int offset;
1684 aff = isl_aff_cow(aff);
1685 if (!aff || !div)
1686 goto error;
1688 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1689 new_n_div = isl_mat_rows(div);
1690 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1692 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1693 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1694 if (!aff->v || !aff->ls)
1695 return isl_aff_free(aff);
1696 return aff;
1697 error:
1698 isl_aff_free(aff);
1699 isl_mat_free(div);
1700 return NULL;
1703 /* Add two affine expressions that live in the same local space.
1705 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1706 __isl_take isl_aff *aff2)
1708 isl_int gcd, f;
1710 aff1 = isl_aff_cow(aff1);
1711 if (!aff1 || !aff2)
1712 goto error;
1714 aff1->v = isl_vec_cow(aff1->v);
1715 if (!aff1->v)
1716 goto error;
1718 isl_int_init(gcd);
1719 isl_int_init(f);
1720 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1721 isl_int_divexact(f, aff2->v->el[0], gcd);
1722 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1723 isl_int_divexact(f, aff1->v->el[0], gcd);
1724 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1725 isl_int_divexact(f, aff2->v->el[0], gcd);
1726 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1727 isl_int_clear(f);
1728 isl_int_clear(gcd);
1730 isl_aff_free(aff2);
1731 return aff1;
1732 error:
1733 isl_aff_free(aff1);
1734 isl_aff_free(aff2);
1735 return NULL;
1738 /* Return the sum of "aff1" and "aff2".
1740 * If either of the two is NaN, then the result is NaN.
1742 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1743 __isl_take isl_aff *aff2)
1745 isl_ctx *ctx;
1746 int *exp1 = NULL;
1747 int *exp2 = NULL;
1748 isl_mat *div;
1749 int n_div1, n_div2;
1751 if (!aff1 || !aff2)
1752 goto error;
1754 ctx = isl_aff_get_ctx(aff1);
1755 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1756 isl_die(ctx, isl_error_invalid,
1757 "spaces don't match", goto error);
1759 if (isl_aff_is_nan(aff1)) {
1760 isl_aff_free(aff2);
1761 return aff1;
1763 if (isl_aff_is_nan(aff2)) {
1764 isl_aff_free(aff1);
1765 return aff2;
1768 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1769 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1770 if (n_div1 == 0 && n_div2 == 0)
1771 return add_expanded(aff1, aff2);
1773 exp1 = isl_alloc_array(ctx, int, n_div1);
1774 exp2 = isl_alloc_array(ctx, int, n_div2);
1775 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1776 goto error;
1778 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1779 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1780 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1781 free(exp1);
1782 free(exp2);
1784 return add_expanded(aff1, aff2);
1785 error:
1786 free(exp1);
1787 free(exp2);
1788 isl_aff_free(aff1);
1789 isl_aff_free(aff2);
1790 return NULL;
1793 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1794 __isl_take isl_aff *aff2)
1796 return isl_aff_add(aff1, isl_aff_neg(aff2));
1799 /* Return the result of scaling "aff" by a factor of "f".
1801 * As a special case, f * NaN = NaN.
1803 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1805 isl_int gcd;
1807 if (!aff)
1808 return NULL;
1809 if (isl_aff_is_nan(aff))
1810 return aff;
1812 if (isl_int_is_one(f))
1813 return aff;
1815 aff = isl_aff_cow(aff);
1816 if (!aff)
1817 return NULL;
1818 aff->v = isl_vec_cow(aff->v);
1819 if (!aff->v)
1820 return isl_aff_free(aff);
1822 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1823 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1824 return aff;
1827 isl_int_init(gcd);
1828 isl_int_gcd(gcd, aff->v->el[0], f);
1829 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1830 isl_int_divexact(gcd, f, gcd);
1831 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1832 isl_int_clear(gcd);
1834 return aff;
1837 /* Multiple "aff" by "v".
1839 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1840 __isl_take isl_val *v)
1842 if (!aff || !v)
1843 goto error;
1845 if (isl_val_is_one(v)) {
1846 isl_val_free(v);
1847 return aff;
1850 if (!isl_val_is_rat(v))
1851 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1852 "expecting rational factor", goto error);
1854 aff = isl_aff_scale(aff, v->n);
1855 aff = isl_aff_scale_down(aff, v->d);
1857 isl_val_free(v);
1858 return aff;
1859 error:
1860 isl_aff_free(aff);
1861 isl_val_free(v);
1862 return NULL;
1865 /* Return the result of scaling "aff" down by a factor of "f".
1867 * As a special case, NaN/f = NaN.
1869 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1871 isl_int gcd;
1873 if (!aff)
1874 return NULL;
1875 if (isl_aff_is_nan(aff))
1876 return aff;
1878 if (isl_int_is_one(f))
1879 return aff;
1881 aff = isl_aff_cow(aff);
1882 if (!aff)
1883 return NULL;
1885 if (isl_int_is_zero(f))
1886 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1887 "cannot scale down by zero", return isl_aff_free(aff));
1889 aff->v = isl_vec_cow(aff->v);
1890 if (!aff->v)
1891 return isl_aff_free(aff);
1893 isl_int_init(gcd);
1894 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1895 isl_int_gcd(gcd, gcd, f);
1896 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1897 isl_int_divexact(gcd, f, gcd);
1898 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1899 isl_int_clear(gcd);
1901 return aff;
1904 /* Divide "aff" by "v".
1906 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1907 __isl_take isl_val *v)
1909 if (!aff || !v)
1910 goto error;
1912 if (isl_val_is_one(v)) {
1913 isl_val_free(v);
1914 return aff;
1917 if (!isl_val_is_rat(v))
1918 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1919 "expecting rational factor", goto error);
1920 if (!isl_val_is_pos(v))
1921 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1922 "factor needs to be positive", goto error);
1924 aff = isl_aff_scale(aff, v->d);
1925 aff = isl_aff_scale_down(aff, v->n);
1927 isl_val_free(v);
1928 return aff;
1929 error:
1930 isl_aff_free(aff);
1931 isl_val_free(v);
1932 return NULL;
1935 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1937 isl_int v;
1939 if (f == 1)
1940 return aff;
1942 isl_int_init(v);
1943 isl_int_set_ui(v, f);
1944 aff = isl_aff_scale_down(aff, v);
1945 isl_int_clear(v);
1947 return aff;
1950 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1951 enum isl_dim_type type, unsigned pos, const char *s)
1953 aff = isl_aff_cow(aff);
1954 if (!aff)
1955 return NULL;
1956 if (type == isl_dim_out)
1957 isl_die(aff->v->ctx, isl_error_invalid,
1958 "cannot set name of output/set dimension",
1959 return isl_aff_free(aff));
1960 if (type == isl_dim_in)
1961 type = isl_dim_set;
1962 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1963 if (!aff->ls)
1964 return isl_aff_free(aff);
1966 return aff;
1969 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1970 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1972 aff = isl_aff_cow(aff);
1973 if (!aff)
1974 goto error;
1975 if (type == isl_dim_out)
1976 isl_die(aff->v->ctx, isl_error_invalid,
1977 "cannot set name of output/set dimension",
1978 goto error);
1979 if (type == isl_dim_in)
1980 type = isl_dim_set;
1981 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1982 if (!aff->ls)
1983 return isl_aff_free(aff);
1985 return aff;
1986 error:
1987 isl_id_free(id);
1988 isl_aff_free(aff);
1989 return NULL;
1992 /* Replace the identifier of the input tuple of "aff" by "id".
1993 * type is currently required to be equal to isl_dim_in
1995 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1996 enum isl_dim_type type, __isl_take isl_id *id)
1998 aff = isl_aff_cow(aff);
1999 if (!aff)
2000 goto error;
2001 if (type != isl_dim_out)
2002 isl_die(aff->v->ctx, isl_error_invalid,
2003 "cannot only set id of input tuple", goto error);
2004 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2005 if (!aff->ls)
2006 return isl_aff_free(aff);
2008 return aff;
2009 error:
2010 isl_id_free(id);
2011 isl_aff_free(aff);
2012 return NULL;
2015 /* Exploit the equalities in "eq" to simplify the affine expression
2016 * and the expressions of the integer divisions in the local space.
2017 * The integer divisions in this local space are assumed to appear
2018 * as regular dimensions in "eq".
2020 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2021 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2023 int i, j;
2024 unsigned total;
2025 unsigned n_div;
2027 if (!eq)
2028 goto error;
2029 if (eq->n_eq == 0) {
2030 isl_basic_set_free(eq);
2031 return aff;
2034 aff = isl_aff_cow(aff);
2035 if (!aff)
2036 goto error;
2038 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2039 isl_basic_set_copy(eq));
2040 aff->v = isl_vec_cow(aff->v);
2041 if (!aff->ls || !aff->v)
2042 goto error;
2044 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2045 n_div = eq->n_div;
2046 for (i = 0; i < eq->n_eq; ++i) {
2047 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2048 if (j < 0 || j == 0 || j >= total)
2049 continue;
2051 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2052 &aff->v->el[0]);
2055 isl_basic_set_free(eq);
2056 aff = isl_aff_normalize(aff);
2057 return aff;
2058 error:
2059 isl_basic_set_free(eq);
2060 isl_aff_free(aff);
2061 return NULL;
2064 /* Exploit the equalities in "eq" to simplify the affine expression
2065 * and the expressions of the integer divisions in the local space.
2067 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2068 __isl_take isl_basic_set *eq)
2070 int n_div;
2072 if (!aff || !eq)
2073 goto error;
2074 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2075 if (n_div > 0)
2076 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2077 return isl_aff_substitute_equalities_lifted(aff, eq);
2078 error:
2079 isl_basic_set_free(eq);
2080 isl_aff_free(aff);
2081 return NULL;
2084 /* Look for equalities among the variables shared by context and aff
2085 * and the integer divisions of aff, if any.
2086 * The equalities are then used to eliminate coefficients and/or integer
2087 * divisions from aff.
2089 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2090 __isl_take isl_set *context)
2092 isl_basic_set *hull;
2093 int n_div;
2095 if (!aff)
2096 goto error;
2097 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2098 if (n_div > 0) {
2099 isl_basic_set *bset;
2100 isl_local_space *ls;
2101 context = isl_set_add_dims(context, isl_dim_set, n_div);
2102 ls = isl_aff_get_domain_local_space(aff);
2103 bset = isl_basic_set_from_local_space(ls);
2104 bset = isl_basic_set_lift(bset);
2105 bset = isl_basic_set_flatten(bset);
2106 context = isl_set_intersect(context,
2107 isl_set_from_basic_set(bset));
2110 hull = isl_set_affine_hull(context);
2111 return isl_aff_substitute_equalities_lifted(aff, hull);
2112 error:
2113 isl_aff_free(aff);
2114 isl_set_free(context);
2115 return NULL;
2118 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2119 __isl_take isl_set *context)
2121 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2122 dom_context = isl_set_intersect_params(dom_context, context);
2123 return isl_aff_gist(aff, dom_context);
2126 /* Return a basic set containing those elements in the space
2127 * of aff where it is positive. "rational" should not be set.
2129 * If "aff" is NaN, then it is not positive.
2131 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2132 int rational)
2134 isl_constraint *ineq;
2135 isl_basic_set *bset;
2136 isl_val *c;
2138 if (!aff)
2139 return NULL;
2140 if (isl_aff_is_nan(aff)) {
2141 isl_space *space = isl_aff_get_domain_space(aff);
2142 isl_aff_free(aff);
2143 return isl_basic_set_empty(space);
2145 if (rational)
2146 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2147 "rational sets not supported", goto error);
2149 ineq = isl_inequality_from_aff(aff);
2150 c = isl_constraint_get_constant_val(ineq);
2151 c = isl_val_sub_ui(c, 1);
2152 ineq = isl_constraint_set_constant_val(ineq, c);
2154 bset = isl_basic_set_from_constraint(ineq);
2155 bset = isl_basic_set_simplify(bset);
2156 return bset;
2157 error:
2158 isl_aff_free(aff);
2159 return NULL;
2162 /* Return a basic set containing those elements in the space
2163 * of aff where it is non-negative.
2164 * If "rational" is set, then return a rational basic set.
2166 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2168 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2169 __isl_take isl_aff *aff, int rational)
2171 isl_constraint *ineq;
2172 isl_basic_set *bset;
2174 if (!aff)
2175 return NULL;
2176 if (isl_aff_is_nan(aff)) {
2177 isl_space *space = isl_aff_get_domain_space(aff);
2178 isl_aff_free(aff);
2179 return isl_basic_set_empty(space);
2182 ineq = isl_inequality_from_aff(aff);
2184 bset = isl_basic_set_from_constraint(ineq);
2185 if (rational)
2186 bset = isl_basic_set_set_rational(bset);
2187 bset = isl_basic_set_simplify(bset);
2188 return bset;
2191 /* Return a basic set containing those elements in the space
2192 * of aff where it is non-negative.
2194 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2196 return aff_nonneg_basic_set(aff, 0);
2199 /* Return a basic set containing those elements in the domain space
2200 * of "aff" where it is positive.
2202 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2204 aff = isl_aff_add_constant_num_si(aff, -1);
2205 return isl_aff_nonneg_basic_set(aff);
2208 /* Return a basic set containing those elements in the domain space
2209 * of aff where it is negative.
2211 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2213 aff = isl_aff_neg(aff);
2214 return isl_aff_pos_basic_set(aff);
2217 /* Return a basic set containing those elements in the space
2218 * of aff where it is zero.
2219 * If "rational" is set, then return a rational basic set.
2221 * If "aff" is NaN, then it is not zero.
2223 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2224 int rational)
2226 isl_constraint *ineq;
2227 isl_basic_set *bset;
2229 if (!aff)
2230 return NULL;
2231 if (isl_aff_is_nan(aff)) {
2232 isl_space *space = isl_aff_get_domain_space(aff);
2233 isl_aff_free(aff);
2234 return isl_basic_set_empty(space);
2237 ineq = isl_equality_from_aff(aff);
2239 bset = isl_basic_set_from_constraint(ineq);
2240 if (rational)
2241 bset = isl_basic_set_set_rational(bset);
2242 bset = isl_basic_set_simplify(bset);
2243 return bset;
2246 /* Return a basic set containing those elements in the space
2247 * of aff where it is zero.
2249 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2251 return aff_zero_basic_set(aff, 0);
2254 /* Return a basic set containing those elements in the shared space
2255 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2257 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2258 __isl_take isl_aff *aff2)
2260 aff1 = isl_aff_sub(aff1, aff2);
2262 return isl_aff_nonneg_basic_set(aff1);
2265 /* Return a basic set containing those elements in the shared domain space
2266 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2268 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2269 __isl_take isl_aff *aff2)
2271 aff1 = isl_aff_sub(aff1, aff2);
2273 return isl_aff_pos_basic_set(aff1);
2276 /* Return a set containing those elements in the shared space
2277 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2279 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2280 __isl_take isl_aff *aff2)
2282 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2285 /* Return a set containing those elements in the shared domain space
2286 * of aff1 and aff2 where aff1 is greater than aff2.
2288 * If either of the two inputs is NaN, then the result is empty,
2289 * as comparisons with NaN always return false.
2291 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2292 __isl_take isl_aff *aff2)
2294 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2297 /* Return a basic set containing those elements in the shared space
2298 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2300 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2301 __isl_take isl_aff *aff2)
2303 return isl_aff_ge_basic_set(aff2, aff1);
2306 /* Return a basic set containing those elements in the shared domain space
2307 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2309 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2310 __isl_take isl_aff *aff2)
2312 return isl_aff_gt_basic_set(aff2, aff1);
2315 /* Return a set containing those elements in the shared space
2316 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2318 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2319 __isl_take isl_aff *aff2)
2321 return isl_aff_ge_set(aff2, aff1);
2324 /* Return a set containing those elements in the shared domain space
2325 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2327 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2328 __isl_take isl_aff *aff2)
2330 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2333 /* Return a basic set containing those elements in the shared space
2334 * of aff1 and aff2 where aff1 and aff2 are equal.
2336 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2337 __isl_take isl_aff *aff2)
2339 aff1 = isl_aff_sub(aff1, aff2);
2341 return isl_aff_zero_basic_set(aff1);
2344 /* Return a set containing those elements in the shared space
2345 * of aff1 and aff2 where aff1 and aff2 are equal.
2347 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2348 __isl_take isl_aff *aff2)
2350 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2353 /* Return a set containing those elements in the shared domain space
2354 * of aff1 and aff2 where aff1 and aff2 are not equal.
2356 * If either of the two inputs is NaN, then the result is empty,
2357 * as comparisons with NaN always return false.
2359 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2360 __isl_take isl_aff *aff2)
2362 isl_set *set_lt, *set_gt;
2364 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2365 isl_aff_copy(aff2));
2366 set_gt = isl_aff_gt_set(aff1, aff2);
2367 return isl_set_union_disjoint(set_lt, set_gt);
2370 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2371 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2373 aff1 = isl_aff_add(aff1, aff2);
2374 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2375 return aff1;
2378 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2380 if (!aff)
2381 return -1;
2383 return 0;
2386 /* Check whether the given affine expression has non-zero coefficient
2387 * for any dimension in the given range or if any of these dimensions
2388 * appear with non-zero coefficients in any of the integer divisions
2389 * involved in the affine expression.
2391 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2392 enum isl_dim_type type, unsigned first, unsigned n)
2394 int i;
2395 isl_ctx *ctx;
2396 int *active = NULL;
2397 isl_bool involves = isl_bool_false;
2399 if (!aff)
2400 return isl_bool_error;
2401 if (n == 0)
2402 return isl_bool_false;
2404 ctx = isl_aff_get_ctx(aff);
2405 if (first + n > isl_aff_dim(aff, type))
2406 isl_die(ctx, isl_error_invalid,
2407 "range out of bounds", return isl_bool_error);
2409 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2410 if (!active)
2411 goto error;
2413 first += isl_local_space_offset(aff->ls, type) - 1;
2414 for (i = 0; i < n; ++i)
2415 if (active[first + i]) {
2416 involves = isl_bool_true;
2417 break;
2420 free(active);
2422 return involves;
2423 error:
2424 free(active);
2425 return isl_bool_error;
2428 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2429 enum isl_dim_type type, unsigned first, unsigned n)
2431 isl_ctx *ctx;
2433 if (!aff)
2434 return NULL;
2435 if (type == isl_dim_out)
2436 isl_die(aff->v->ctx, isl_error_invalid,
2437 "cannot drop output/set dimension",
2438 return isl_aff_free(aff));
2439 if (type == isl_dim_in)
2440 type = isl_dim_set;
2441 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2442 return aff;
2444 ctx = isl_aff_get_ctx(aff);
2445 if (first + n > isl_local_space_dim(aff->ls, type))
2446 isl_die(ctx, isl_error_invalid, "range out of bounds",
2447 return isl_aff_free(aff));
2449 aff = isl_aff_cow(aff);
2450 if (!aff)
2451 return NULL;
2453 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2454 if (!aff->ls)
2455 return isl_aff_free(aff);
2457 first += 1 + isl_local_space_offset(aff->ls, type);
2458 aff->v = isl_vec_drop_els(aff->v, first, n);
2459 if (!aff->v)
2460 return isl_aff_free(aff);
2462 return aff;
2465 /* Project the domain of the affine expression onto its parameter space.
2466 * The affine expression may not involve any of the domain dimensions.
2468 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2470 isl_space *space;
2471 unsigned n;
2472 int involves;
2474 n = isl_aff_dim(aff, isl_dim_in);
2475 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2476 if (involves < 0)
2477 return isl_aff_free(aff);
2478 if (involves)
2479 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2480 "affine expression involves some of the domain dimensions",
2481 return isl_aff_free(aff));
2482 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2483 space = isl_aff_get_domain_space(aff);
2484 space = isl_space_params(space);
2485 aff = isl_aff_reset_domain_space(aff, space);
2486 return aff;
2489 /* Convert an affine expression defined over a parameter domain
2490 * into one that is defined over a zero-dimensional set.
2492 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2494 isl_local_space *ls;
2496 ls = isl_aff_take_domain_local_space(aff);
2497 ls = isl_local_space_set_from_params(ls);
2498 aff = isl_aff_restore_domain_local_space(aff, ls);
2500 return aff;
2503 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2504 enum isl_dim_type type, unsigned first, unsigned n)
2506 isl_ctx *ctx;
2508 if (!aff)
2509 return NULL;
2510 if (type == isl_dim_out)
2511 isl_die(aff->v->ctx, isl_error_invalid,
2512 "cannot insert output/set dimensions",
2513 return isl_aff_free(aff));
2514 if (type == isl_dim_in)
2515 type = isl_dim_set;
2516 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2517 return aff;
2519 ctx = isl_aff_get_ctx(aff);
2520 if (first > isl_local_space_dim(aff->ls, type))
2521 isl_die(ctx, isl_error_invalid, "position out of bounds",
2522 return isl_aff_free(aff));
2524 aff = isl_aff_cow(aff);
2525 if (!aff)
2526 return NULL;
2528 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2529 if (!aff->ls)
2530 return isl_aff_free(aff);
2532 first += 1 + isl_local_space_offset(aff->ls, type);
2533 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2534 if (!aff->v)
2535 return isl_aff_free(aff);
2537 return aff;
2540 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2541 enum isl_dim_type type, unsigned n)
2543 unsigned pos;
2545 pos = isl_aff_dim(aff, type);
2547 return isl_aff_insert_dims(aff, type, pos, n);
2550 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2551 enum isl_dim_type type, unsigned n)
2553 unsigned pos;
2555 pos = isl_pw_aff_dim(pwaff, type);
2557 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2560 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2561 * to dimensions of "dst_type" at "dst_pos".
2563 * We only support moving input dimensions to parameters and vice versa.
2565 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2566 enum isl_dim_type dst_type, unsigned dst_pos,
2567 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2569 unsigned g_dst_pos;
2570 unsigned g_src_pos;
2572 if (!aff)
2573 return NULL;
2574 if (n == 0 &&
2575 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2576 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2577 return aff;
2579 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2580 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2581 "cannot move output/set dimension",
2582 return isl_aff_free(aff));
2583 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2584 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2585 "cannot move divs", return isl_aff_free(aff));
2586 if (dst_type == isl_dim_in)
2587 dst_type = isl_dim_set;
2588 if (src_type == isl_dim_in)
2589 src_type = isl_dim_set;
2591 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2592 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2593 "range out of bounds", return isl_aff_free(aff));
2594 if (dst_type == src_type)
2595 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2596 "moving dims within the same type not supported",
2597 return isl_aff_free(aff));
2599 aff = isl_aff_cow(aff);
2600 if (!aff)
2601 return NULL;
2603 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2604 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2605 if (dst_type > src_type)
2606 g_dst_pos -= n;
2608 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2609 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2610 src_type, src_pos, n);
2611 if (!aff->v || !aff->ls)
2612 return isl_aff_free(aff);
2614 aff = sort_divs(aff);
2616 return aff;
2619 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2621 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2622 return isl_pw_aff_alloc(dom, aff);
2625 #define isl_aff_involves_nan isl_aff_is_nan
2627 #undef PW
2628 #define PW isl_pw_aff
2629 #undef EL
2630 #define EL isl_aff
2631 #undef EL_IS_ZERO
2632 #define EL_IS_ZERO is_empty
2633 #undef ZERO
2634 #define ZERO empty
2635 #undef IS_ZERO
2636 #define IS_ZERO is_empty
2637 #undef FIELD
2638 #define FIELD aff
2639 #undef DEFAULT_IS_ZERO
2640 #define DEFAULT_IS_ZERO 0
2642 #define NO_EVAL
2643 #define NO_OPT
2644 #define NO_LIFT
2645 #define NO_MORPH
2647 #include <isl_pw_templ.c>
2648 #include <isl_pw_hash.c>
2649 #include <isl_pw_union_opt.c>
2651 #undef UNION
2652 #define UNION isl_union_pw_aff
2653 #undef PART
2654 #define PART isl_pw_aff
2655 #undef PARTS
2656 #define PARTS pw_aff
2658 #include <isl_union_single.c>
2659 #include <isl_union_neg.c>
2661 static __isl_give isl_set *align_params_pw_pw_set_and(
2662 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2663 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2664 __isl_take isl_pw_aff *pwaff2))
2666 isl_bool equal_params;
2668 if (!pwaff1 || !pwaff2)
2669 goto error;
2670 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2671 if (equal_params < 0)
2672 goto error;
2673 if (equal_params)
2674 return fn(pwaff1, pwaff2);
2675 if (!isl_space_has_named_params(pwaff1->dim) ||
2676 !isl_space_has_named_params(pwaff2->dim))
2677 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2678 "unaligned unnamed parameters", goto error);
2679 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2680 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2681 return fn(pwaff1, pwaff2);
2682 error:
2683 isl_pw_aff_free(pwaff1);
2684 isl_pw_aff_free(pwaff2);
2685 return NULL;
2688 /* Align the parameters of the to isl_pw_aff arguments and
2689 * then apply a function "fn" on them that returns an isl_map.
2691 static __isl_give isl_map *align_params_pw_pw_map_and(
2692 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2693 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2694 __isl_take isl_pw_aff *pa2))
2696 isl_bool equal_params;
2698 if (!pa1 || !pa2)
2699 goto error;
2700 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2701 if (equal_params < 0)
2702 goto error;
2703 if (equal_params)
2704 return fn(pa1, pa2);
2705 if (!isl_space_has_named_params(pa1->dim) ||
2706 !isl_space_has_named_params(pa2->dim))
2707 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2708 "unaligned unnamed parameters", goto error);
2709 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2710 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2711 return fn(pa1, pa2);
2712 error:
2713 isl_pw_aff_free(pa1);
2714 isl_pw_aff_free(pa2);
2715 return NULL;
2718 /* Compute a piecewise quasi-affine expression with a domain that
2719 * is the union of those of pwaff1 and pwaff2 and such that on each
2720 * cell, the quasi-affine expression is the maximum of those of pwaff1
2721 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2722 * cell, then the associated expression is the defined one.
2724 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2725 __isl_take isl_pw_aff *pwaff2)
2727 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2730 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2731 __isl_take isl_pw_aff *pwaff2)
2733 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2734 &pw_aff_union_max);
2737 /* Compute a piecewise quasi-affine expression with a domain that
2738 * is the union of those of pwaff1 and pwaff2 and such that on each
2739 * cell, the quasi-affine expression is the minimum of those of pwaff1
2740 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2741 * cell, then the associated expression is the defined one.
2743 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2744 __isl_take isl_pw_aff *pwaff2)
2746 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2749 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2750 __isl_take isl_pw_aff *pwaff2)
2752 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2753 &pw_aff_union_min);
2756 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2757 __isl_take isl_pw_aff *pwaff2, int max)
2759 if (max)
2760 return isl_pw_aff_union_max(pwaff1, pwaff2);
2761 else
2762 return isl_pw_aff_union_min(pwaff1, pwaff2);
2765 /* Construct a map with as domain the domain of pwaff and
2766 * one-dimensional range corresponding to the affine expressions.
2768 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2770 int i;
2771 isl_space *dim;
2772 isl_map *map;
2774 if (!pwaff)
2775 return NULL;
2777 dim = isl_pw_aff_get_space(pwaff);
2778 map = isl_map_empty(dim);
2780 for (i = 0; i < pwaff->n; ++i) {
2781 isl_basic_map *bmap;
2782 isl_map *map_i;
2784 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2785 map_i = isl_map_from_basic_map(bmap);
2786 map_i = isl_map_intersect_domain(map_i,
2787 isl_set_copy(pwaff->p[i].set));
2788 map = isl_map_union_disjoint(map, map_i);
2791 isl_pw_aff_free(pwaff);
2793 return map;
2796 /* Construct a map with as domain the domain of pwaff and
2797 * one-dimensional range corresponding to the affine expressions.
2799 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2801 if (!pwaff)
2802 return NULL;
2803 if (isl_space_is_set(pwaff->dim))
2804 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2805 "space of input is not a map", goto error);
2806 return map_from_pw_aff(pwaff);
2807 error:
2808 isl_pw_aff_free(pwaff);
2809 return NULL;
2812 /* Construct a one-dimensional set with as parameter domain
2813 * the domain of pwaff and the single set dimension
2814 * corresponding to the affine expressions.
2816 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2818 if (!pwaff)
2819 return NULL;
2820 if (!isl_space_is_set(pwaff->dim))
2821 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2822 "space of input is not a set", goto error);
2823 return map_from_pw_aff(pwaff);
2824 error:
2825 isl_pw_aff_free(pwaff);
2826 return NULL;
2829 /* Return a set containing those elements in the domain
2830 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2831 * does not satisfy "fn" (if complement is 1).
2833 * The pieces with a NaN never belong to the result since
2834 * NaN does not satisfy any property.
2836 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2837 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2838 int complement)
2840 int i;
2841 isl_set *set;
2843 if (!pwaff)
2844 return NULL;
2846 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2848 for (i = 0; i < pwaff->n; ++i) {
2849 isl_basic_set *bset;
2850 isl_set *set_i, *locus;
2851 isl_bool rational;
2853 if (isl_aff_is_nan(pwaff->p[i].aff))
2854 continue;
2856 rational = isl_set_has_rational(pwaff->p[i].set);
2857 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2858 locus = isl_set_from_basic_set(bset);
2859 set_i = isl_set_copy(pwaff->p[i].set);
2860 if (complement)
2861 set_i = isl_set_subtract(set_i, locus);
2862 else
2863 set_i = isl_set_intersect(set_i, locus);
2864 set = isl_set_union_disjoint(set, set_i);
2867 isl_pw_aff_free(pwaff);
2869 return set;
2872 /* Return a set containing those elements in the domain
2873 * of "pa" where it is positive.
2875 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2877 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2880 /* Return a set containing those elements in the domain
2881 * of pwaff where it is non-negative.
2883 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2885 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2888 /* Return a set containing those elements in the domain
2889 * of pwaff where it is zero.
2891 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2893 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2896 /* Return a set containing those elements in the domain
2897 * of pwaff where it is not zero.
2899 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2901 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2904 /* Return a set containing those elements in the shared domain
2905 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2907 * We compute the difference on the shared domain and then construct
2908 * the set of values where this difference is non-negative.
2909 * If strict is set, we first subtract 1 from the difference.
2910 * If equal is set, we only return the elements where pwaff1 and pwaff2
2911 * are equal.
2913 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2914 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2916 isl_set *set1, *set2;
2918 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2919 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2920 set1 = isl_set_intersect(set1, set2);
2921 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2922 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2923 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2925 if (strict) {
2926 isl_space *dim = isl_set_get_space(set1);
2927 isl_aff *aff;
2928 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2929 aff = isl_aff_add_constant_si(aff, -1);
2930 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2931 } else
2932 isl_set_free(set1);
2934 if (equal)
2935 return isl_pw_aff_zero_set(pwaff1);
2936 return isl_pw_aff_nonneg_set(pwaff1);
2939 /* Return a set containing those elements in the shared domain
2940 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2942 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2943 __isl_take isl_pw_aff *pwaff2)
2945 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2948 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2949 __isl_take isl_pw_aff *pwaff2)
2951 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2954 /* Return a set containing those elements in the shared domain
2955 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2957 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2958 __isl_take isl_pw_aff *pwaff2)
2960 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2963 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2964 __isl_take isl_pw_aff *pwaff2)
2966 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2969 /* Return a set containing those elements in the shared domain
2970 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2972 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2973 __isl_take isl_pw_aff *pwaff2)
2975 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2978 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2979 __isl_take isl_pw_aff *pwaff2)
2981 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2984 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2985 __isl_take isl_pw_aff *pwaff2)
2987 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2990 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2991 __isl_take isl_pw_aff *pwaff2)
2993 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2996 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2997 * where the function values are ordered in the same way as "order",
2998 * which returns a set in the shared domain of its two arguments.
2999 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3001 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3002 * We first pull back the two functions such that they are defined on
3003 * the domain [A -> B]. Then we apply "order", resulting in a set
3004 * in the space [A -> B]. Finally, we unwrap this set to obtain
3005 * a map in the space A -> B.
3007 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3008 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3009 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3010 __isl_take isl_pw_aff *pa2))
3012 isl_space *space1, *space2;
3013 isl_multi_aff *ma;
3014 isl_set *set;
3016 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3017 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3018 space1 = isl_space_map_from_domain_and_range(space1, space2);
3019 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3020 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3021 ma = isl_multi_aff_range_map(space1);
3022 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3023 set = order(pa1, pa2);
3025 return isl_set_unwrap(set);
3028 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3029 * where the function values are equal.
3030 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3032 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3033 __isl_take isl_pw_aff *pa2)
3035 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3038 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3039 * where the function values are equal.
3041 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3042 __isl_take isl_pw_aff *pa2)
3044 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3047 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3048 * where the function value of "pa1" is less than the function value of "pa2".
3049 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3051 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3052 __isl_take isl_pw_aff *pa2)
3054 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3057 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3058 * where the function value of "pa1" is less than the function value of "pa2".
3060 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3061 __isl_take isl_pw_aff *pa2)
3063 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3066 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3067 * where the function value of "pa1" is greater than the function value
3068 * of "pa2".
3069 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3071 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3072 __isl_take isl_pw_aff *pa2)
3074 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3077 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3078 * where the function value of "pa1" is greater than the function value
3079 * of "pa2".
3081 __isl_give isl_map *isl_pw_aff_gt_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_gt_map_aligned);
3087 /* Return a set containing those elements in the shared domain
3088 * of the elements of list1 and list2 where each element in list1
3089 * has the relation specified by "fn" with each element in list2.
3091 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3092 __isl_take isl_pw_aff_list *list2,
3093 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3094 __isl_take isl_pw_aff *pwaff2))
3096 int i, j;
3097 isl_ctx *ctx;
3098 isl_set *set;
3100 if (!list1 || !list2)
3101 goto error;
3103 ctx = isl_pw_aff_list_get_ctx(list1);
3104 if (list1->n < 1 || list2->n < 1)
3105 isl_die(ctx, isl_error_invalid,
3106 "list should contain at least one element", goto error);
3108 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3109 for (i = 0; i < list1->n; ++i)
3110 for (j = 0; j < list2->n; ++j) {
3111 isl_set *set_ij;
3113 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3114 isl_pw_aff_copy(list2->p[j]));
3115 set = isl_set_intersect(set, set_ij);
3118 isl_pw_aff_list_free(list1);
3119 isl_pw_aff_list_free(list2);
3120 return set;
3121 error:
3122 isl_pw_aff_list_free(list1);
3123 isl_pw_aff_list_free(list2);
3124 return NULL;
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 * is equal to each element in list2.
3131 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3132 __isl_take isl_pw_aff_list *list2)
3134 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3137 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3138 __isl_take isl_pw_aff_list *list2)
3140 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3143 /* Return a set containing those elements in the shared domain
3144 * of the elements of list1 and list2 where each element in list1
3145 * is less than or equal to each element in list2.
3147 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3148 __isl_take isl_pw_aff_list *list2)
3150 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3153 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3154 __isl_take isl_pw_aff_list *list2)
3156 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3159 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3160 __isl_take isl_pw_aff_list *list2)
3162 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3165 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3166 __isl_take isl_pw_aff_list *list2)
3168 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3172 /* Return a set containing those elements in the shared domain
3173 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3175 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3176 __isl_take isl_pw_aff *pwaff2)
3178 isl_set *set_lt, *set_gt;
3180 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3181 isl_pw_aff_copy(pwaff2));
3182 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3183 return isl_set_union_disjoint(set_lt, set_gt);
3186 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3187 __isl_take isl_pw_aff *pwaff2)
3189 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3192 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3193 isl_int v)
3195 int i;
3197 if (isl_int_is_one(v))
3198 return pwaff;
3199 if (!isl_int_is_pos(v))
3200 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3201 "factor needs to be positive",
3202 return isl_pw_aff_free(pwaff));
3203 pwaff = isl_pw_aff_cow(pwaff);
3204 if (!pwaff)
3205 return NULL;
3206 if (pwaff->n == 0)
3207 return pwaff;
3209 for (i = 0; i < pwaff->n; ++i) {
3210 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3211 if (!pwaff->p[i].aff)
3212 return isl_pw_aff_free(pwaff);
3215 return pwaff;
3218 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3220 int i;
3222 pwaff = isl_pw_aff_cow(pwaff);
3223 if (!pwaff)
3224 return NULL;
3225 if (pwaff->n == 0)
3226 return pwaff;
3228 for (i = 0; i < pwaff->n; ++i) {
3229 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3230 if (!pwaff->p[i].aff)
3231 return isl_pw_aff_free(pwaff);
3234 return pwaff;
3237 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3239 int i;
3241 pwaff = isl_pw_aff_cow(pwaff);
3242 if (!pwaff)
3243 return NULL;
3244 if (pwaff->n == 0)
3245 return pwaff;
3247 for (i = 0; i < pwaff->n; ++i) {
3248 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3249 if (!pwaff->p[i].aff)
3250 return isl_pw_aff_free(pwaff);
3253 return pwaff;
3256 /* Assuming that "cond1" and "cond2" are disjoint,
3257 * return an affine expression that is equal to pwaff1 on cond1
3258 * and to pwaff2 on cond2.
3260 static __isl_give isl_pw_aff *isl_pw_aff_select(
3261 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3262 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3264 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3265 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3267 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3270 /* Return an affine expression that is equal to pwaff_true for elements
3271 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3272 * is zero.
3273 * That is, return cond ? pwaff_true : pwaff_false;
3275 * If "cond" involves and NaN, then we conservatively return a NaN
3276 * on its entire domain. In principle, we could consider the pieces
3277 * where it is NaN separately from those where it is not.
3279 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3280 * then only use the domain of "cond" to restrict the domain.
3282 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3283 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3285 isl_set *cond_true, *cond_false;
3286 isl_bool equal;
3288 if (!cond)
3289 goto error;
3290 if (isl_pw_aff_involves_nan(cond)) {
3291 isl_space *space = isl_pw_aff_get_domain_space(cond);
3292 isl_local_space *ls = isl_local_space_from_space(space);
3293 isl_pw_aff_free(cond);
3294 isl_pw_aff_free(pwaff_true);
3295 isl_pw_aff_free(pwaff_false);
3296 return isl_pw_aff_nan_on_domain(ls);
3299 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3300 isl_pw_aff_get_space(pwaff_false));
3301 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3302 isl_pw_aff_get_space(pwaff_true));
3303 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3304 if (equal < 0)
3305 goto error;
3306 if (equal) {
3307 isl_set *dom;
3309 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3310 isl_pw_aff_free(pwaff_false);
3311 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3314 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3315 cond_false = isl_pw_aff_zero_set(cond);
3316 return isl_pw_aff_select(cond_true, pwaff_true,
3317 cond_false, pwaff_false);
3318 error:
3319 isl_pw_aff_free(cond);
3320 isl_pw_aff_free(pwaff_true);
3321 isl_pw_aff_free(pwaff_false);
3322 return NULL;
3325 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3327 if (!aff)
3328 return isl_bool_error;
3330 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3333 /* Check whether pwaff is a piecewise constant.
3335 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3337 int i;
3339 if (!pwaff)
3340 return isl_bool_error;
3342 for (i = 0; i < pwaff->n; ++i) {
3343 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3344 if (is_cst < 0 || !is_cst)
3345 return is_cst;
3348 return isl_bool_true;
3351 /* Are all elements of "mpa" piecewise constants?
3353 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3355 int i;
3357 if (!mpa)
3358 return isl_bool_error;
3360 for (i = 0; i < mpa->n; ++i) {
3361 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3362 if (is_cst < 0 || !is_cst)
3363 return is_cst;
3366 return isl_bool_true;
3369 /* Return the product of "aff1" and "aff2".
3371 * If either of the two is NaN, then the result is NaN.
3373 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3375 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3376 __isl_take isl_aff *aff2)
3378 if (!aff1 || !aff2)
3379 goto error;
3381 if (isl_aff_is_nan(aff1)) {
3382 isl_aff_free(aff2);
3383 return aff1;
3385 if (isl_aff_is_nan(aff2)) {
3386 isl_aff_free(aff1);
3387 return aff2;
3390 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3391 return isl_aff_mul(aff2, aff1);
3393 if (!isl_aff_is_cst(aff2))
3394 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3395 "at least one affine expression should be constant",
3396 goto error);
3398 aff1 = isl_aff_cow(aff1);
3399 if (!aff1 || !aff2)
3400 goto error;
3402 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3403 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3405 isl_aff_free(aff2);
3406 return aff1;
3407 error:
3408 isl_aff_free(aff1);
3409 isl_aff_free(aff2);
3410 return NULL;
3413 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3415 * If either of the two is NaN, then the result is NaN.
3417 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3418 __isl_take isl_aff *aff2)
3420 int is_cst;
3421 int neg;
3423 if (!aff1 || !aff2)
3424 goto error;
3426 if (isl_aff_is_nan(aff1)) {
3427 isl_aff_free(aff2);
3428 return aff1;
3430 if (isl_aff_is_nan(aff2)) {
3431 isl_aff_free(aff1);
3432 return aff2;
3435 is_cst = isl_aff_is_cst(aff2);
3436 if (is_cst < 0)
3437 goto error;
3438 if (!is_cst)
3439 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3440 "second argument should be a constant", goto error);
3442 if (!aff2)
3443 goto error;
3445 neg = isl_int_is_neg(aff2->v->el[1]);
3446 if (neg) {
3447 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3448 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3451 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3452 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3454 if (neg) {
3455 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3456 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3459 isl_aff_free(aff2);
3460 return aff1;
3461 error:
3462 isl_aff_free(aff1);
3463 isl_aff_free(aff2);
3464 return NULL;
3467 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3468 __isl_take isl_pw_aff *pwaff2)
3470 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3473 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3474 __isl_take isl_pw_aff *pwaff2)
3476 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3479 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3480 __isl_take isl_pw_aff *pwaff2)
3482 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3485 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3486 __isl_take isl_pw_aff *pwaff2)
3488 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3491 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3492 __isl_take isl_pw_aff *pwaff2)
3494 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3497 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3498 __isl_take isl_pw_aff *pa2)
3500 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3503 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3505 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3506 __isl_take isl_pw_aff *pa2)
3508 int is_cst;
3510 is_cst = isl_pw_aff_is_cst(pa2);
3511 if (is_cst < 0)
3512 goto error;
3513 if (!is_cst)
3514 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3515 "second argument should be a piecewise constant",
3516 goto error);
3517 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3518 error:
3519 isl_pw_aff_free(pa1);
3520 isl_pw_aff_free(pa2);
3521 return NULL;
3524 /* Compute the quotient of the integer division of "pa1" by "pa2"
3525 * with rounding towards zero.
3526 * "pa2" is assumed to be a piecewise constant.
3528 * In particular, return
3530 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3533 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3534 __isl_take isl_pw_aff *pa2)
3536 int is_cst;
3537 isl_set *cond;
3538 isl_pw_aff *f, *c;
3540 is_cst = isl_pw_aff_is_cst(pa2);
3541 if (is_cst < 0)
3542 goto error;
3543 if (!is_cst)
3544 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3545 "second argument should be a piecewise constant",
3546 goto error);
3548 pa1 = isl_pw_aff_div(pa1, pa2);
3550 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3551 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3552 c = isl_pw_aff_ceil(pa1);
3553 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3554 error:
3555 isl_pw_aff_free(pa1);
3556 isl_pw_aff_free(pa2);
3557 return NULL;
3560 /* Compute the remainder of the integer division of "pa1" by "pa2"
3561 * with rounding towards zero.
3562 * "pa2" is assumed to be a piecewise constant.
3564 * In particular, return
3566 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3569 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3570 __isl_take isl_pw_aff *pa2)
3572 int is_cst;
3573 isl_pw_aff *res;
3575 is_cst = isl_pw_aff_is_cst(pa2);
3576 if (is_cst < 0)
3577 goto error;
3578 if (!is_cst)
3579 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3580 "second argument should be a piecewise constant",
3581 goto error);
3582 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3583 res = isl_pw_aff_mul(pa2, res);
3584 res = isl_pw_aff_sub(pa1, res);
3585 return res;
3586 error:
3587 isl_pw_aff_free(pa1);
3588 isl_pw_aff_free(pa2);
3589 return NULL;
3592 /* Does either of "pa1" or "pa2" involve any NaN2?
3594 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3595 __isl_keep isl_pw_aff *pa2)
3597 isl_bool has_nan;
3599 has_nan = isl_pw_aff_involves_nan(pa1);
3600 if (has_nan < 0 || has_nan)
3601 return has_nan;
3602 return isl_pw_aff_involves_nan(pa2);
3605 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3606 * by a NaN on their shared domain.
3608 * In principle, the result could be refined to only being NaN
3609 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3611 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3612 __isl_take isl_pw_aff *pa2)
3614 isl_local_space *ls;
3615 isl_set *dom;
3616 isl_pw_aff *pa;
3618 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3619 ls = isl_local_space_from_space(isl_set_get_space(dom));
3620 pa = isl_pw_aff_nan_on_domain(ls);
3621 pa = isl_pw_aff_intersect_domain(pa, dom);
3623 return pa;
3626 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3627 __isl_take isl_pw_aff *pwaff2)
3629 isl_set *le;
3630 isl_set *dom;
3632 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3633 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3634 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3635 isl_pw_aff_copy(pwaff2));
3636 dom = isl_set_subtract(dom, isl_set_copy(le));
3637 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3640 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3641 __isl_take isl_pw_aff *pwaff2)
3643 isl_set *ge;
3644 isl_set *dom;
3646 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3647 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3648 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3649 isl_pw_aff_copy(pwaff2));
3650 dom = isl_set_subtract(dom, isl_set_copy(ge));
3651 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3654 /* Return an expression for the minimum (if "max" is not set) or
3655 * the maximum (if "max" is set) of "pa1" and "pa2".
3656 * If either expression involves any NaN, then return a NaN
3657 * on the shared domain as result.
3659 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3660 __isl_take isl_pw_aff *pa2, int max)
3662 isl_bool has_nan;
3664 has_nan = either_involves_nan(pa1, pa2);
3665 if (has_nan < 0)
3666 pa1 = isl_pw_aff_free(pa1);
3667 else if (has_nan)
3668 return replace_by_nan(pa1, pa2);
3670 if (max)
3671 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3672 else
3673 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3676 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3678 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3679 __isl_take isl_pw_aff *pwaff2)
3681 return pw_aff_min_max(pwaff1, pwaff2, 0);
3684 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3686 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3687 __isl_take isl_pw_aff *pwaff2)
3689 return pw_aff_min_max(pwaff1, pwaff2, 1);
3692 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3693 __isl_take isl_pw_aff_list *list,
3694 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3695 __isl_take isl_pw_aff *pwaff2))
3697 int i;
3698 isl_ctx *ctx;
3699 isl_pw_aff *res;
3701 if (!list)
3702 return NULL;
3704 ctx = isl_pw_aff_list_get_ctx(list);
3705 if (list->n < 1)
3706 isl_die(ctx, isl_error_invalid,
3707 "list should contain at least one element", goto error);
3709 res = isl_pw_aff_copy(list->p[0]);
3710 for (i = 1; i < list->n; ++i)
3711 res = fn(res, isl_pw_aff_copy(list->p[i]));
3713 isl_pw_aff_list_free(list);
3714 return res;
3715 error:
3716 isl_pw_aff_list_free(list);
3717 return NULL;
3720 /* Return an isl_pw_aff that maps each element in the intersection of the
3721 * domains of the elements of list to the minimal corresponding affine
3722 * expression.
3724 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3726 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3729 /* Return an isl_pw_aff that maps each element in the intersection of the
3730 * domains of the elements of list to the maximal corresponding affine
3731 * expression.
3733 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3735 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3738 /* Mark the domains of "pwaff" as rational.
3740 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3742 int i;
3744 pwaff = isl_pw_aff_cow(pwaff);
3745 if (!pwaff)
3746 return NULL;
3747 if (pwaff->n == 0)
3748 return pwaff;
3750 for (i = 0; i < pwaff->n; ++i) {
3751 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3752 if (!pwaff->p[i].set)
3753 return isl_pw_aff_free(pwaff);
3756 return pwaff;
3759 /* Mark the domains of the elements of "list" as rational.
3761 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3762 __isl_take isl_pw_aff_list *list)
3764 int i, n;
3766 if (!list)
3767 return NULL;
3768 if (list->n == 0)
3769 return list;
3771 n = list->n;
3772 for (i = 0; i < n; ++i) {
3773 isl_pw_aff *pa;
3775 pa = isl_pw_aff_list_get_pw_aff(list, i);
3776 pa = isl_pw_aff_set_rational(pa);
3777 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3780 return list;
3783 /* Do the parameters of "aff" match those of "space"?
3785 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3786 __isl_keep isl_space *space)
3788 isl_space *aff_space;
3789 isl_bool match;
3791 if (!aff || !space)
3792 return isl_bool_error;
3794 aff_space = isl_aff_get_domain_space(aff);
3796 match = isl_space_has_equal_params(space, aff_space);
3798 isl_space_free(aff_space);
3799 return match;
3802 /* Check that the domain space of "aff" matches "space".
3804 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3805 __isl_keep isl_space *space)
3807 isl_space *aff_space;
3808 isl_bool match;
3810 if (!aff || !space)
3811 return isl_stat_error;
3813 aff_space = isl_aff_get_domain_space(aff);
3815 match = isl_space_has_equal_params(space, aff_space);
3816 if (match < 0)
3817 goto error;
3818 if (!match)
3819 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3820 "parameters don't match", goto error);
3821 match = isl_space_tuple_is_equal(space, isl_dim_in,
3822 aff_space, isl_dim_set);
3823 if (match < 0)
3824 goto error;
3825 if (!match)
3826 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3827 "domains don't match", goto error);
3828 isl_space_free(aff_space);
3829 return isl_stat_ok;
3830 error:
3831 isl_space_free(aff_space);
3832 return isl_stat_error;
3835 #undef BASE
3836 #define BASE aff
3837 #undef DOMBASE
3838 #define DOMBASE set
3839 #define NO_DOMAIN
3841 #include <isl_multi_no_explicit_domain.c>
3842 #include <isl_multi_templ.c>
3843 #include <isl_multi_apply_set.c>
3844 #include <isl_multi_cmp.c>
3845 #include <isl_multi_dims.c>
3846 #include <isl_multi_floor.c>
3847 #include <isl_multi_gist.c>
3849 #undef NO_DOMAIN
3851 /* Construct an isl_multi_aff living in "space" that corresponds
3852 * to the affine transformation matrix "mat".
3854 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3855 __isl_take isl_space *space, __isl_take isl_mat *mat)
3857 isl_ctx *ctx;
3858 isl_local_space *ls = NULL;
3859 isl_multi_aff *ma = NULL;
3860 int n_row, n_col, n_out, total;
3861 int i;
3863 if (!space || !mat)
3864 goto error;
3866 ctx = isl_mat_get_ctx(mat);
3868 n_row = isl_mat_rows(mat);
3869 n_col = isl_mat_cols(mat);
3870 if (n_row < 1)
3871 isl_die(ctx, isl_error_invalid,
3872 "insufficient number of rows", goto error);
3873 if (n_col < 1)
3874 isl_die(ctx, isl_error_invalid,
3875 "insufficient number of columns", goto error);
3876 n_out = isl_space_dim(space, isl_dim_out);
3877 total = isl_space_dim(space, isl_dim_all);
3878 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3879 isl_die(ctx, isl_error_invalid,
3880 "dimension mismatch", goto error);
3882 ma = isl_multi_aff_zero(isl_space_copy(space));
3883 ls = isl_local_space_from_space(isl_space_domain(space));
3885 for (i = 0; i < n_row - 1; ++i) {
3886 isl_vec *v;
3887 isl_aff *aff;
3889 v = isl_vec_alloc(ctx, 1 + n_col);
3890 if (!v)
3891 goto error;
3892 isl_int_set(v->el[0], mat->row[0][0]);
3893 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3894 v = isl_vec_normalize(v);
3895 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3896 ma = isl_multi_aff_set_aff(ma, i, aff);
3899 isl_local_space_free(ls);
3900 isl_mat_free(mat);
3901 return ma;
3902 error:
3903 isl_local_space_free(ls);
3904 isl_mat_free(mat);
3905 isl_multi_aff_free(ma);
3906 return NULL;
3909 /* Remove any internal structure of the domain of "ma".
3910 * If there is any such internal structure in the input,
3911 * then the name of the corresponding space is also removed.
3913 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3914 __isl_take isl_multi_aff *ma)
3916 isl_space *space;
3918 if (!ma)
3919 return NULL;
3921 if (!ma->space->nested[0])
3922 return ma;
3924 space = isl_multi_aff_get_space(ma);
3925 space = isl_space_flatten_domain(space);
3926 ma = isl_multi_aff_reset_space(ma, space);
3928 return ma;
3931 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3932 * of the space to its domain.
3934 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3936 int i, n_in;
3937 isl_local_space *ls;
3938 isl_multi_aff *ma;
3940 if (!space)
3941 return NULL;
3942 if (!isl_space_is_map(space))
3943 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3944 "not a map space", goto error);
3946 n_in = isl_space_dim(space, isl_dim_in);
3947 space = isl_space_domain_map(space);
3949 ma = isl_multi_aff_alloc(isl_space_copy(space));
3950 if (n_in == 0) {
3951 isl_space_free(space);
3952 return ma;
3955 space = isl_space_domain(space);
3956 ls = isl_local_space_from_space(space);
3957 for (i = 0; i < n_in; ++i) {
3958 isl_aff *aff;
3960 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3961 isl_dim_set, i);
3962 ma = isl_multi_aff_set_aff(ma, i, aff);
3964 isl_local_space_free(ls);
3965 return ma;
3966 error:
3967 isl_space_free(space);
3968 return NULL;
3971 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3972 * of the space to its range.
3974 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3976 int i, n_in, n_out;
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 n_out = isl_space_dim(space, isl_dim_out);
3988 space = isl_space_range_map(space);
3990 ma = isl_multi_aff_alloc(isl_space_copy(space));
3991 if (n_out == 0) {
3992 isl_space_free(space);
3993 return ma;
3996 space = isl_space_domain(space);
3997 ls = isl_local_space_from_space(space);
3998 for (i = 0; i < n_out; ++i) {
3999 isl_aff *aff;
4001 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4002 isl_dim_set, n_in + i);
4003 ma = isl_multi_aff_set_aff(ma, i, aff);
4005 isl_local_space_free(ls);
4006 return ma;
4007 error:
4008 isl_space_free(space);
4009 return NULL;
4012 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4013 * of the space to its range.
4015 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4016 __isl_take isl_space *space)
4018 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4021 /* Given the space of a set and a range of set dimensions,
4022 * construct an isl_multi_aff that projects out those dimensions.
4024 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4025 __isl_take isl_space *space, enum isl_dim_type type,
4026 unsigned first, unsigned n)
4028 int i, dim;
4029 isl_local_space *ls;
4030 isl_multi_aff *ma;
4032 if (!space)
4033 return NULL;
4034 if (!isl_space_is_set(space))
4035 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4036 "expecting set space", goto error);
4037 if (type != isl_dim_set)
4038 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4039 "only set dimensions can be projected out", goto error);
4041 dim = isl_space_dim(space, isl_dim_set);
4042 if (first + n > dim)
4043 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4044 "range out of bounds", goto error);
4046 space = isl_space_from_domain(space);
4047 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4049 if (dim == n)
4050 return isl_multi_aff_alloc(space);
4052 ma = isl_multi_aff_alloc(isl_space_copy(space));
4053 space = isl_space_domain(space);
4054 ls = isl_local_space_from_space(space);
4056 for (i = 0; i < first; ++i) {
4057 isl_aff *aff;
4059 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4060 isl_dim_set, i);
4061 ma = isl_multi_aff_set_aff(ma, i, aff);
4064 for (i = 0; i < dim - (first + n); ++i) {
4065 isl_aff *aff;
4067 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4068 isl_dim_set, first + n + i);
4069 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4072 isl_local_space_free(ls);
4073 return ma;
4074 error:
4075 isl_space_free(space);
4076 return NULL;
4079 /* Given the space of a set and a range of set dimensions,
4080 * construct an isl_pw_multi_aff that projects out those dimensions.
4082 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4083 __isl_take isl_space *space, enum isl_dim_type type,
4084 unsigned first, unsigned n)
4086 isl_multi_aff *ma;
4088 ma = isl_multi_aff_project_out_map(space, type, first, n);
4089 return isl_pw_multi_aff_from_multi_aff(ma);
4092 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4093 * domain.
4095 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4096 __isl_take isl_multi_aff *ma)
4098 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4099 return isl_pw_multi_aff_alloc(dom, ma);
4102 /* Create a piecewise multi-affine expression in the given space that maps each
4103 * input dimension to the corresponding output dimension.
4105 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4106 __isl_take isl_space *space)
4108 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4111 /* Exploit the equalities in "eq" to simplify the affine expressions.
4113 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4114 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4116 int i;
4118 maff = isl_multi_aff_cow(maff);
4119 if (!maff || !eq)
4120 goto error;
4122 for (i = 0; i < maff->n; ++i) {
4123 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4124 isl_basic_set_copy(eq));
4125 if (!maff->u.p[i])
4126 goto error;
4129 isl_basic_set_free(eq);
4130 return maff;
4131 error:
4132 isl_basic_set_free(eq);
4133 isl_multi_aff_free(maff);
4134 return NULL;
4137 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4138 isl_int f)
4140 int i;
4142 maff = isl_multi_aff_cow(maff);
4143 if (!maff)
4144 return NULL;
4146 for (i = 0; i < maff->n; ++i) {
4147 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4148 if (!maff->u.p[i])
4149 return isl_multi_aff_free(maff);
4152 return maff;
4155 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4156 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4158 maff1 = isl_multi_aff_add(maff1, maff2);
4159 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4160 return maff1;
4163 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4165 if (!maff)
4166 return -1;
4168 return 0;
4171 /* Return the set of domain elements where "ma1" is lexicographically
4172 * smaller than or equal to "ma2".
4174 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4175 __isl_take isl_multi_aff *ma2)
4177 return isl_multi_aff_lex_ge_set(ma2, ma1);
4180 /* Return the set of domain elements where "ma1" is lexicographically
4181 * smaller than "ma2".
4183 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4184 __isl_take isl_multi_aff *ma2)
4186 return isl_multi_aff_lex_gt_set(ma2, ma1);
4189 /* Return the set of domain elements where "ma1" and "ma2"
4190 * satisfy "order".
4192 static __isl_give isl_set *isl_multi_aff_order_set(
4193 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4194 __isl_give isl_map *order(__isl_take isl_space *set_space))
4196 isl_space *space;
4197 isl_map *map1, *map2;
4198 isl_map *map, *ge;
4200 map1 = isl_map_from_multi_aff(ma1);
4201 map2 = isl_map_from_multi_aff(ma2);
4202 map = isl_map_range_product(map1, map2);
4203 space = isl_space_range(isl_map_get_space(map));
4204 space = isl_space_domain(isl_space_unwrap(space));
4205 ge = order(space);
4206 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4208 return isl_map_domain(map);
4211 /* Return the set of domain elements where "ma1" is lexicographically
4212 * greater than or equal to "ma2".
4214 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4215 __isl_take isl_multi_aff *ma2)
4217 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4220 /* Return the set of domain elements where "ma1" is lexicographically
4221 * greater than "ma2".
4223 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4224 __isl_take isl_multi_aff *ma2)
4226 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4229 #undef PW
4230 #define PW isl_pw_multi_aff
4231 #undef EL
4232 #define EL isl_multi_aff
4233 #undef EL_IS_ZERO
4234 #define EL_IS_ZERO is_empty
4235 #undef ZERO
4236 #define ZERO empty
4237 #undef IS_ZERO
4238 #define IS_ZERO is_empty
4239 #undef FIELD
4240 #define FIELD maff
4241 #undef DEFAULT_IS_ZERO
4242 #define DEFAULT_IS_ZERO 0
4244 #define NO_SUB
4245 #define NO_EVAL
4246 #define NO_OPT
4247 #define NO_INVOLVES_DIMS
4248 #define NO_INSERT_DIMS
4249 #define NO_LIFT
4250 #define NO_MORPH
4252 #include <isl_pw_templ.c>
4253 #include <isl_pw_union_opt.c>
4255 #undef NO_SUB
4257 #undef UNION
4258 #define UNION isl_union_pw_multi_aff
4259 #undef PART
4260 #define PART isl_pw_multi_aff
4261 #undef PARTS
4262 #define PARTS pw_multi_aff
4264 #include <isl_union_multi.c>
4265 #include <isl_union_neg.c>
4267 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4268 __isl_take isl_pw_multi_aff *pma1,
4269 __isl_take isl_pw_multi_aff *pma2)
4271 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4272 &isl_multi_aff_lex_ge_set);
4275 /* Given two piecewise multi affine expressions, return a piecewise
4276 * multi-affine expression defined on the union of the definition domains
4277 * of the inputs that is equal to the lexicographic maximum of the two
4278 * inputs on each cell. If only one of the two inputs is defined on
4279 * a given cell, then it is considered to be the maximum.
4281 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4282 __isl_take isl_pw_multi_aff *pma1,
4283 __isl_take isl_pw_multi_aff *pma2)
4285 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4286 &pw_multi_aff_union_lexmax);
4289 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4290 __isl_take isl_pw_multi_aff *pma1,
4291 __isl_take isl_pw_multi_aff *pma2)
4293 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4294 &isl_multi_aff_lex_le_set);
4297 /* Given two piecewise multi affine expressions, return a piecewise
4298 * multi-affine expression defined on the union of the definition domains
4299 * of the inputs that is equal to the lexicographic minimum of the two
4300 * inputs on each cell. If only one of the two inputs is defined on
4301 * a given cell, then it is considered to be the minimum.
4303 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4304 __isl_take isl_pw_multi_aff *pma1,
4305 __isl_take isl_pw_multi_aff *pma2)
4307 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4308 &pw_multi_aff_union_lexmin);
4311 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4312 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4314 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4315 &isl_multi_aff_add);
4318 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4319 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4321 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4322 &pw_multi_aff_add);
4325 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4326 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4328 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4329 &isl_multi_aff_sub);
4332 /* Subtract "pma2" from "pma1" and return the result.
4334 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4335 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4337 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4338 &pw_multi_aff_sub);
4341 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4342 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4344 return isl_pw_multi_aff_union_add_(pma1, pma2);
4347 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4348 * with the actual sum on the shared domain and
4349 * the defined expression on the symmetric difference of the domains.
4351 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4352 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4354 return isl_union_pw_aff_union_add_(upa1, upa2);
4357 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4358 * with the actual sum on the shared domain and
4359 * the defined expression on the symmetric difference of the domains.
4361 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4362 __isl_take isl_union_pw_multi_aff *upma1,
4363 __isl_take isl_union_pw_multi_aff *upma2)
4365 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4368 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4369 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4371 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4372 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4374 int i, j, n;
4375 isl_space *space;
4376 isl_pw_multi_aff *res;
4378 if (!pma1 || !pma2)
4379 goto error;
4381 n = pma1->n * pma2->n;
4382 space = isl_space_product(isl_space_copy(pma1->dim),
4383 isl_space_copy(pma2->dim));
4384 res = isl_pw_multi_aff_alloc_size(space, n);
4386 for (i = 0; i < pma1->n; ++i) {
4387 for (j = 0; j < pma2->n; ++j) {
4388 isl_set *domain;
4389 isl_multi_aff *ma;
4391 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4392 isl_set_copy(pma2->p[j].set));
4393 ma = isl_multi_aff_product(
4394 isl_multi_aff_copy(pma1->p[i].maff),
4395 isl_multi_aff_copy(pma2->p[j].maff));
4396 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4400 isl_pw_multi_aff_free(pma1);
4401 isl_pw_multi_aff_free(pma2);
4402 return res;
4403 error:
4404 isl_pw_multi_aff_free(pma1);
4405 isl_pw_multi_aff_free(pma2);
4406 return NULL;
4409 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4410 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4412 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4413 &pw_multi_aff_product);
4416 /* Construct a map mapping the domain of the piecewise multi-affine expression
4417 * to its range, with each dimension in the range equated to the
4418 * corresponding affine expression on its cell.
4420 * If the domain of "pma" is rational, then so is the constructed "map".
4422 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4424 int i;
4425 isl_map *map;
4427 if (!pma)
4428 return NULL;
4430 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4432 for (i = 0; i < pma->n; ++i) {
4433 isl_bool rational;
4434 isl_multi_aff *maff;
4435 isl_basic_map *bmap;
4436 isl_map *map_i;
4438 rational = isl_set_is_rational(pma->p[i].set);
4439 if (rational < 0)
4440 map = isl_map_free(map);
4441 maff = isl_multi_aff_copy(pma->p[i].maff);
4442 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4443 map_i = isl_map_from_basic_map(bmap);
4444 map_i = isl_map_intersect_domain(map_i,
4445 isl_set_copy(pma->p[i].set));
4446 map = isl_map_union_disjoint(map, map_i);
4449 isl_pw_multi_aff_free(pma);
4450 return map;
4453 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4455 if (!pma)
4456 return NULL;
4458 if (!isl_space_is_set(pma->dim))
4459 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4460 "isl_pw_multi_aff cannot be converted into an isl_set",
4461 goto error);
4463 return isl_map_from_pw_multi_aff(pma);
4464 error:
4465 isl_pw_multi_aff_free(pma);
4466 return NULL;
4469 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4470 * denominator "denom".
4471 * "denom" is allowed to be negative, in which case the actual denominator
4472 * is -denom and the expressions are added instead.
4474 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4475 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4477 int i, first;
4478 int sign;
4479 isl_int d;
4481 first = isl_seq_first_non_zero(c, n);
4482 if (first == -1)
4483 return aff;
4485 sign = isl_int_sgn(denom);
4486 isl_int_init(d);
4487 isl_int_abs(d, denom);
4488 for (i = first; i < n; ++i) {
4489 isl_aff *aff_i;
4491 if (isl_int_is_zero(c[i]))
4492 continue;
4493 aff_i = isl_multi_aff_get_aff(ma, i);
4494 aff_i = isl_aff_scale(aff_i, c[i]);
4495 aff_i = isl_aff_scale_down(aff_i, d);
4496 if (sign >= 0)
4497 aff = isl_aff_sub(aff, aff_i);
4498 else
4499 aff = isl_aff_add(aff, aff_i);
4501 isl_int_clear(d);
4503 return aff;
4506 /* Extract an affine expression that expresses the output dimension "pos"
4507 * of "bmap" in terms of the parameters and input dimensions from
4508 * equality "eq".
4509 * Note that this expression may involve integer divisions defined
4510 * in terms of parameters and input dimensions.
4511 * The equality may also involve references to earlier (but not later)
4512 * output dimensions. These are replaced by the corresponding elements
4513 * in "ma".
4515 * If the equality is of the form
4517 * f(i) + h(j) + a x + g(i) = 0,
4519 * with f(i) a linear combinations of the parameters and input dimensions,
4520 * g(i) a linear combination of integer divisions defined in terms of the same
4521 * and h(j) a linear combinations of earlier output dimensions,
4522 * then the affine expression is
4524 * (-f(i) - g(i))/a - h(j)/a
4526 * If the equality is of the form
4528 * f(i) + h(j) - a x + g(i) = 0,
4530 * then the affine expression is
4532 * (f(i) + g(i))/a - h(j)/(-a)
4535 * If "div" refers to an integer division (i.e., it is smaller than
4536 * the number of integer divisions), then the equality constraint
4537 * does involve an integer division (the one at position "div") that
4538 * is defined in terms of output dimensions. However, this integer
4539 * division can be eliminated by exploiting a pair of constraints
4540 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4541 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4542 * -l + x >= 0.
4543 * In particular, let
4545 * x = e(i) + m floor(...)
4547 * with e(i) the expression derived above and floor(...) the integer
4548 * division involving output dimensions.
4549 * From
4551 * l <= x <= l + n,
4553 * we have
4555 * 0 <= x - l <= n
4557 * This means
4559 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4560 * = (e(i) - l) mod m
4562 * Therefore,
4564 * x - l = (e(i) - l) mod m
4566 * or
4568 * x = ((e(i) - l) mod m) + l
4570 * The variable "shift" below contains the expression -l, which may
4571 * also involve a linear combination of earlier output dimensions.
4573 static __isl_give isl_aff *extract_aff_from_equality(
4574 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4575 __isl_keep isl_multi_aff *ma)
4577 unsigned o_out;
4578 unsigned n_div, n_out;
4579 isl_ctx *ctx;
4580 isl_local_space *ls;
4581 isl_aff *aff, *shift;
4582 isl_val *mod;
4584 ctx = isl_basic_map_get_ctx(bmap);
4585 ls = isl_basic_map_get_local_space(bmap);
4586 ls = isl_local_space_domain(ls);
4587 aff = isl_aff_alloc(isl_local_space_copy(ls));
4588 if (!aff)
4589 goto error;
4590 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4591 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4592 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4593 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4594 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4595 isl_seq_cpy(aff->v->el + 1 + o_out,
4596 bmap->eq[eq] + o_out + n_out, n_div);
4597 } else {
4598 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4599 isl_seq_neg(aff->v->el + 1 + o_out,
4600 bmap->eq[eq] + o_out + n_out, n_div);
4602 if (div < n_div)
4603 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4604 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4605 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4606 bmap->eq[eq][o_out + pos]);
4607 if (div < n_div) {
4608 shift = isl_aff_alloc(isl_local_space_copy(ls));
4609 if (!shift)
4610 goto error;
4611 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4612 isl_seq_cpy(shift->v->el + 1 + o_out,
4613 bmap->ineq[ineq] + o_out + n_out, n_div);
4614 isl_int_set_si(shift->v->el[0], 1);
4615 shift = subtract_initial(shift, ma, pos,
4616 bmap->ineq[ineq] + o_out, ctx->negone);
4617 aff = isl_aff_add(aff, isl_aff_copy(shift));
4618 mod = isl_val_int_from_isl_int(ctx,
4619 bmap->eq[eq][o_out + n_out + div]);
4620 mod = isl_val_abs(mod);
4621 aff = isl_aff_mod_val(aff, mod);
4622 aff = isl_aff_sub(aff, shift);
4625 isl_local_space_free(ls);
4626 return aff;
4627 error:
4628 isl_local_space_free(ls);
4629 isl_aff_free(aff);
4630 return NULL;
4633 /* Given a basic map with output dimensions defined
4634 * in terms of the parameters input dimensions and earlier
4635 * output dimensions using an equality (and possibly a pair on inequalities),
4636 * extract an isl_aff that expresses output dimension "pos" in terms
4637 * of the parameters and input dimensions.
4638 * Note that this expression may involve integer divisions defined
4639 * in terms of parameters and input dimensions.
4640 * "ma" contains the expressions corresponding to earlier output dimensions.
4642 * This function shares some similarities with
4643 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4645 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4646 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4648 int eq, div, ineq;
4649 isl_aff *aff;
4651 if (!bmap)
4652 return NULL;
4653 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4654 if (eq >= bmap->n_eq)
4655 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4656 "unable to find suitable equality", return NULL);
4657 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4659 aff = isl_aff_remove_unused_divs(aff);
4660 return aff;
4663 /* Given a basic map where each output dimension is defined
4664 * in terms of the parameters and input dimensions using an equality,
4665 * extract an isl_multi_aff that expresses the output dimensions in terms
4666 * of the parameters and input dimensions.
4668 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4669 __isl_take isl_basic_map *bmap)
4671 int i;
4672 unsigned n_out;
4673 isl_multi_aff *ma;
4675 if (!bmap)
4676 return NULL;
4678 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4679 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4681 for (i = 0; i < n_out; ++i) {
4682 isl_aff *aff;
4684 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4685 ma = isl_multi_aff_set_aff(ma, i, aff);
4688 isl_basic_map_free(bmap);
4690 return ma;
4693 /* Given a basic set where each set dimension is defined
4694 * in terms of the parameters using an equality,
4695 * extract an isl_multi_aff that expresses the set dimensions in terms
4696 * of the parameters.
4698 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4699 __isl_take isl_basic_set *bset)
4701 return extract_isl_multi_aff_from_basic_map(bset);
4704 /* Create an isl_pw_multi_aff that is equivalent to
4705 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4706 * The given basic map is such that each output dimension is defined
4707 * in terms of the parameters and input dimensions using an equality.
4709 * Since some applications expect the result of isl_pw_multi_aff_from_map
4710 * to only contain integer affine expressions, we compute the floor
4711 * of the expression before returning.
4713 * Remove all constraints involving local variables without
4714 * an explicit representation (resulting in the removal of those
4715 * local variables) prior to the actual extraction to ensure
4716 * that the local spaces in which the resulting affine expressions
4717 * are created do not contain any unknown local variables.
4718 * Removing such constraints is safe because constraints involving
4719 * unknown local variables are not used to determine whether
4720 * a basic map is obviously single-valued.
4722 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4723 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4725 isl_multi_aff *ma;
4727 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4728 ma = extract_isl_multi_aff_from_basic_map(bmap);
4729 ma = isl_multi_aff_floor(ma);
4730 return isl_pw_multi_aff_alloc(domain, ma);
4733 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4734 * This obviously only works if the input "map" is single-valued.
4735 * If so, we compute the lexicographic minimum of the image in the form
4736 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4737 * to its lexicographic minimum.
4738 * If the input is not single-valued, we produce an error.
4740 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4741 __isl_take isl_map *map)
4743 int i;
4744 int sv;
4745 isl_pw_multi_aff *pma;
4747 sv = isl_map_is_single_valued(map);
4748 if (sv < 0)
4749 goto error;
4750 if (!sv)
4751 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4752 "map is not single-valued", goto error);
4753 map = isl_map_make_disjoint(map);
4754 if (!map)
4755 return NULL;
4757 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4759 for (i = 0; i < map->n; ++i) {
4760 isl_pw_multi_aff *pma_i;
4761 isl_basic_map *bmap;
4762 bmap = isl_basic_map_copy(map->p[i]);
4763 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4764 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4767 isl_map_free(map);
4768 return pma;
4769 error:
4770 isl_map_free(map);
4771 return NULL;
4774 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4775 * taking into account that the output dimension at position "d"
4776 * can be represented as
4778 * x = floor((e(...) + c1) / m)
4780 * given that constraint "i" is of the form
4782 * e(...) + c1 - m x >= 0
4785 * Let "map" be of the form
4787 * A -> B
4789 * We construct a mapping
4791 * A -> [A -> x = floor(...)]
4793 * apply that to the map, obtaining
4795 * [A -> x = floor(...)] -> B
4797 * and equate dimension "d" to x.
4798 * We then compute a isl_pw_multi_aff representation of the resulting map
4799 * and plug in the mapping above.
4801 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4802 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4804 isl_ctx *ctx;
4805 isl_space *space;
4806 isl_local_space *ls;
4807 isl_multi_aff *ma;
4808 isl_aff *aff;
4809 isl_vec *v;
4810 isl_map *insert;
4811 int offset;
4812 int n;
4813 int n_in;
4814 isl_pw_multi_aff *pma;
4815 isl_bool is_set;
4817 is_set = isl_map_is_set(map);
4818 if (is_set < 0)
4819 goto error;
4821 offset = isl_basic_map_offset(hull, isl_dim_out);
4822 ctx = isl_map_get_ctx(map);
4823 space = isl_space_domain(isl_map_get_space(map));
4824 n_in = isl_space_dim(space, isl_dim_set);
4825 n = isl_space_dim(space, isl_dim_all);
4827 v = isl_vec_alloc(ctx, 1 + 1 + n);
4828 if (v) {
4829 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4830 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4832 isl_basic_map_free(hull);
4834 ls = isl_local_space_from_space(isl_space_copy(space));
4835 aff = isl_aff_alloc_vec(ls, v);
4836 aff = isl_aff_floor(aff);
4837 if (is_set) {
4838 isl_space_free(space);
4839 ma = isl_multi_aff_from_aff(aff);
4840 } else {
4841 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4842 ma = isl_multi_aff_range_product(ma,
4843 isl_multi_aff_from_aff(aff));
4846 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4847 map = isl_map_apply_domain(map, insert);
4848 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4849 pma = isl_pw_multi_aff_from_map(map);
4850 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4852 return pma;
4853 error:
4854 isl_map_free(map);
4855 isl_basic_map_free(hull);
4856 return NULL;
4859 /* Is constraint "c" of the form
4861 * e(...) + c1 - m x >= 0
4863 * or
4865 * -e(...) + c2 + m x >= 0
4867 * where m > 1 and e only depends on parameters and input dimemnsions?
4869 * "offset" is the offset of the output dimensions
4870 * "pos" is the position of output dimension x.
4872 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4874 if (isl_int_is_zero(c[offset + d]))
4875 return 0;
4876 if (isl_int_is_one(c[offset + d]))
4877 return 0;
4878 if (isl_int_is_negone(c[offset + d]))
4879 return 0;
4880 if (isl_seq_first_non_zero(c + offset, d) != -1)
4881 return 0;
4882 if (isl_seq_first_non_zero(c + offset + d + 1,
4883 total - (offset + d + 1)) != -1)
4884 return 0;
4885 return 1;
4888 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4890 * As a special case, we first check if there is any pair of constraints,
4891 * shared by all the basic maps in "map" that force a given dimension
4892 * to be equal to the floor of some affine combination of the input dimensions.
4894 * In particular, if we can find two constraints
4896 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4898 * and
4900 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4902 * where m > 1 and e only depends on parameters and input dimemnsions,
4903 * and such that
4905 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4907 * then we know that we can take
4909 * x = floor((e(...) + c1) / m)
4911 * without having to perform any computation.
4913 * Note that we know that
4915 * c1 + c2 >= 1
4917 * If c1 + c2 were 0, then we would have detected an equality during
4918 * simplification. If c1 + c2 were negative, then we would have detected
4919 * a contradiction.
4921 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4922 __isl_take isl_map *map)
4924 int d, dim;
4925 int i, j, n;
4926 int offset, total;
4927 isl_int sum;
4928 isl_basic_map *hull;
4930 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4931 if (!hull)
4932 goto error;
4934 isl_int_init(sum);
4935 dim = isl_map_dim(map, isl_dim_out);
4936 offset = isl_basic_map_offset(hull, isl_dim_out);
4937 total = 1 + isl_basic_map_total_dim(hull);
4938 n = hull->n_ineq;
4939 for (d = 0; d < dim; ++d) {
4940 for (i = 0; i < n; ++i) {
4941 if (!is_potential_div_constraint(hull->ineq[i],
4942 offset, d, total))
4943 continue;
4944 for (j = i + 1; j < n; ++j) {
4945 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4946 hull->ineq[j] + 1, total - 1))
4947 continue;
4948 isl_int_add(sum, hull->ineq[i][0],
4949 hull->ineq[j][0]);
4950 if (isl_int_abs_lt(sum,
4951 hull->ineq[i][offset + d]))
4952 break;
4955 if (j >= n)
4956 continue;
4957 isl_int_clear(sum);
4958 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4959 j = i;
4960 return pw_multi_aff_from_map_div(map, hull, d, j);
4963 isl_int_clear(sum);
4964 isl_basic_map_free(hull);
4965 return pw_multi_aff_from_map_base(map);
4966 error:
4967 isl_map_free(map);
4968 isl_basic_map_free(hull);
4969 return NULL;
4972 /* Given an affine expression
4974 * [A -> B] -> f(A,B)
4976 * construct an isl_multi_aff
4978 * [A -> B] -> B'
4980 * such that dimension "d" in B' is set to "aff" and the remaining
4981 * dimensions are set equal to the corresponding dimensions in B.
4982 * "n_in" is the dimension of the space A.
4983 * "n_out" is the dimension of the space B.
4985 * If "is_set" is set, then the affine expression is of the form
4987 * [B] -> f(B)
4989 * and we construct an isl_multi_aff
4991 * B -> B'
4993 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4994 unsigned n_in, unsigned n_out, int is_set)
4996 int i;
4997 isl_multi_aff *ma;
4998 isl_space *space, *space2;
4999 isl_local_space *ls;
5001 space = isl_aff_get_domain_space(aff);
5002 ls = isl_local_space_from_space(isl_space_copy(space));
5003 space2 = isl_space_copy(space);
5004 if (!is_set)
5005 space2 = isl_space_range(isl_space_unwrap(space2));
5006 space = isl_space_map_from_domain_and_range(space, space2);
5007 ma = isl_multi_aff_alloc(space);
5008 ma = isl_multi_aff_set_aff(ma, d, aff);
5010 for (i = 0; i < n_out; ++i) {
5011 if (i == d)
5012 continue;
5013 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5014 isl_dim_set, n_in + i);
5015 ma = isl_multi_aff_set_aff(ma, i, aff);
5018 isl_local_space_free(ls);
5020 return ma;
5023 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5024 * taking into account that the dimension at position "d" can be written as
5026 * x = m a + f(..) (1)
5028 * where m is equal to "gcd".
5029 * "i" is the index of the equality in "hull" that defines f(..).
5030 * In particular, the equality is of the form
5032 * f(..) - x + m g(existentials) = 0
5034 * or
5036 * -f(..) + x + m g(existentials) = 0
5038 * We basically plug (1) into "map", resulting in a map with "a"
5039 * in the range instead of "x". The corresponding isl_pw_multi_aff
5040 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5042 * Specifically, given the input map
5044 * A -> B
5046 * We first wrap it into a set
5048 * [A -> B]
5050 * and define (1) on top of the corresponding space, resulting in "aff".
5051 * We use this to create an isl_multi_aff that maps the output position "d"
5052 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5053 * We plug this into the wrapped map, unwrap the result and compute the
5054 * corresponding isl_pw_multi_aff.
5055 * The result is an expression
5057 * A -> T(A)
5059 * We adjust that to
5061 * A -> [A -> T(A)]
5063 * so that we can plug that into "aff", after extending the latter to
5064 * a mapping
5066 * [A -> B] -> B'
5069 * If "map" is actually a set, then there is no "A" space, meaning
5070 * that we do not need to perform any wrapping, and that the result
5071 * of the recursive call is of the form
5073 * [T]
5075 * which is plugged into a mapping of the form
5077 * B -> B'
5079 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5080 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5081 isl_int gcd)
5083 isl_set *set;
5084 isl_space *space;
5085 isl_local_space *ls;
5086 isl_aff *aff;
5087 isl_multi_aff *ma;
5088 isl_pw_multi_aff *pma, *id;
5089 unsigned n_in;
5090 unsigned o_out;
5091 unsigned n_out;
5092 isl_bool is_set;
5094 is_set = isl_map_is_set(map);
5095 if (is_set < 0)
5096 goto error;
5098 n_in = isl_basic_map_dim(hull, isl_dim_in);
5099 n_out = isl_basic_map_dim(hull, isl_dim_out);
5100 o_out = isl_basic_map_offset(hull, isl_dim_out);
5102 if (is_set)
5103 set = map;
5104 else
5105 set = isl_map_wrap(map);
5106 space = isl_space_map_from_set(isl_set_get_space(set));
5107 ma = isl_multi_aff_identity(space);
5108 ls = isl_local_space_from_space(isl_set_get_space(set));
5109 aff = isl_aff_alloc(ls);
5110 if (aff) {
5111 isl_int_set_si(aff->v->el[0], 1);
5112 if (isl_int_is_one(hull->eq[i][o_out + d]))
5113 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5114 aff->v->size - 1);
5115 else
5116 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5117 aff->v->size - 1);
5118 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5120 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5121 set = isl_set_preimage_multi_aff(set, ma);
5123 ma = range_map(aff, d, n_in, n_out, is_set);
5125 if (is_set)
5126 map = set;
5127 else
5128 map = isl_set_unwrap(set);
5129 pma = isl_pw_multi_aff_from_map(map);
5131 if (!is_set) {
5132 space = isl_pw_multi_aff_get_domain_space(pma);
5133 space = isl_space_map_from_set(space);
5134 id = isl_pw_multi_aff_identity(space);
5135 pma = isl_pw_multi_aff_range_product(id, pma);
5137 id = isl_pw_multi_aff_from_multi_aff(ma);
5138 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5140 isl_basic_map_free(hull);
5141 return pma;
5142 error:
5143 isl_map_free(map);
5144 isl_basic_map_free(hull);
5145 return NULL;
5148 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5149 * "hull" contains the equalities valid for "map".
5151 * Check if any of the output dimensions is "strided".
5152 * That is, we check if it can be written as
5154 * x = m a + f(..)
5156 * with m greater than 1, a some combination of existentially quantified
5157 * variables and f an expression in the parameters and input dimensions.
5158 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5160 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5161 * special case.
5163 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5164 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5166 int i, j;
5167 unsigned n_out;
5168 unsigned o_out;
5169 unsigned n_div;
5170 unsigned o_div;
5171 isl_int gcd;
5173 n_div = isl_basic_map_dim(hull, isl_dim_div);
5174 o_div = isl_basic_map_offset(hull, isl_dim_div);
5176 if (n_div == 0) {
5177 isl_basic_map_free(hull);
5178 return pw_multi_aff_from_map_check_div(map);
5181 isl_int_init(gcd);
5183 n_out = isl_basic_map_dim(hull, isl_dim_out);
5184 o_out = isl_basic_map_offset(hull, isl_dim_out);
5186 for (i = 0; i < n_out; ++i) {
5187 for (j = 0; j < hull->n_eq; ++j) {
5188 isl_int *eq = hull->eq[j];
5189 isl_pw_multi_aff *res;
5191 if (!isl_int_is_one(eq[o_out + i]) &&
5192 !isl_int_is_negone(eq[o_out + i]))
5193 continue;
5194 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5195 continue;
5196 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5197 n_out - (i + 1)) != -1)
5198 continue;
5199 isl_seq_gcd(eq + o_div, n_div, &gcd);
5200 if (isl_int_is_zero(gcd))
5201 continue;
5202 if (isl_int_is_one(gcd))
5203 continue;
5205 res = pw_multi_aff_from_map_stride(map, hull,
5206 i, j, gcd);
5207 isl_int_clear(gcd);
5208 return res;
5212 isl_int_clear(gcd);
5213 isl_basic_map_free(hull);
5214 return pw_multi_aff_from_map_check_div(map);
5217 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5219 * As a special case, we first check if all output dimensions are uniquely
5220 * defined in terms of the parameters and input dimensions over the entire
5221 * domain. If so, we extract the desired isl_pw_multi_aff directly
5222 * from the affine hull of "map" and its domain.
5224 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5225 * special cases.
5227 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5229 isl_bool sv;
5230 isl_basic_map *hull;
5232 if (!map)
5233 return NULL;
5235 if (isl_map_n_basic_map(map) == 1) {
5236 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5237 hull = isl_basic_map_plain_affine_hull(hull);
5238 sv = isl_basic_map_plain_is_single_valued(hull);
5239 if (sv >= 0 && sv)
5240 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5241 hull);
5242 isl_basic_map_free(hull);
5244 map = isl_map_detect_equalities(map);
5245 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5246 sv = isl_basic_map_plain_is_single_valued(hull);
5247 if (sv >= 0 && sv)
5248 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5249 if (sv >= 0)
5250 return pw_multi_aff_from_map_check_strides(map, hull);
5251 isl_basic_map_free(hull);
5252 isl_map_free(map);
5253 return NULL;
5256 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5258 return isl_pw_multi_aff_from_map(set);
5261 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5262 * add it to *user.
5264 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5266 isl_union_pw_multi_aff **upma = user;
5267 isl_pw_multi_aff *pma;
5269 pma = isl_pw_multi_aff_from_map(map);
5270 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5272 return *upma ? isl_stat_ok : isl_stat_error;
5275 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5276 * domain.
5278 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5279 __isl_take isl_aff *aff)
5281 isl_multi_aff *ma;
5282 isl_pw_multi_aff *pma;
5284 ma = isl_multi_aff_from_aff(aff);
5285 pma = isl_pw_multi_aff_from_multi_aff(ma);
5286 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5289 /* Try and create an isl_union_pw_multi_aff that is equivalent
5290 * to the given isl_union_map.
5291 * The isl_union_map is required to be single-valued in each space.
5292 * Otherwise, an error is produced.
5294 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5295 __isl_take isl_union_map *umap)
5297 isl_space *space;
5298 isl_union_pw_multi_aff *upma;
5300 space = isl_union_map_get_space(umap);
5301 upma = isl_union_pw_multi_aff_empty(space);
5302 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5303 upma = isl_union_pw_multi_aff_free(upma);
5304 isl_union_map_free(umap);
5306 return upma;
5309 /* Try and create an isl_union_pw_multi_aff that is equivalent
5310 * to the given isl_union_set.
5311 * The isl_union_set is required to be a singleton in each space.
5312 * Otherwise, an error is produced.
5314 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5315 __isl_take isl_union_set *uset)
5317 return isl_union_pw_multi_aff_from_union_map(uset);
5320 /* Return the piecewise affine expression "set ? 1 : 0".
5322 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5324 isl_pw_aff *pa;
5325 isl_space *space = isl_set_get_space(set);
5326 isl_local_space *ls = isl_local_space_from_space(space);
5327 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5328 isl_aff *one = isl_aff_zero_on_domain(ls);
5330 one = isl_aff_add_constant_si(one, 1);
5331 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5332 set = isl_set_complement(set);
5333 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5335 return pa;
5338 /* Plug in "subs" for dimension "type", "pos" of "aff".
5340 * Let i be the dimension to replace and let "subs" be of the form
5342 * f/d
5344 * and "aff" of the form
5346 * (a i + g)/m
5348 * The result is
5350 * (a f + d g')/(m d)
5352 * where g' is the result of plugging in "subs" in each of the integer
5353 * divisions in g.
5355 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5356 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5358 isl_ctx *ctx;
5359 isl_int v;
5361 aff = isl_aff_cow(aff);
5362 if (!aff || !subs)
5363 return isl_aff_free(aff);
5365 ctx = isl_aff_get_ctx(aff);
5366 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5367 isl_die(ctx, isl_error_invalid,
5368 "spaces don't match", return isl_aff_free(aff));
5369 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5370 isl_die(ctx, isl_error_unsupported,
5371 "cannot handle divs yet", return isl_aff_free(aff));
5373 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5374 if (!aff->ls)
5375 return isl_aff_free(aff);
5377 aff->v = isl_vec_cow(aff->v);
5378 if (!aff->v)
5379 return isl_aff_free(aff);
5381 pos += isl_local_space_offset(aff->ls, type);
5383 isl_int_init(v);
5384 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5385 aff->v->size, subs->v->size, v);
5386 isl_int_clear(v);
5388 return aff;
5391 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5392 * expressions in "maff".
5394 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5395 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5396 __isl_keep isl_aff *subs)
5398 int i;
5400 maff = isl_multi_aff_cow(maff);
5401 if (!maff || !subs)
5402 return isl_multi_aff_free(maff);
5404 if (type == isl_dim_in)
5405 type = isl_dim_set;
5407 for (i = 0; i < maff->n; ++i) {
5408 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5409 type, pos, subs);
5410 if (!maff->u.p[i])
5411 return isl_multi_aff_free(maff);
5414 return maff;
5417 /* Plug in "subs" for dimension "type", "pos" of "pma".
5419 * pma is of the form
5421 * A_i(v) -> M_i(v)
5423 * while subs is of the form
5425 * v' = B_j(v) -> S_j
5427 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5428 * has a contribution in the result, in particular
5430 * C_ij(S_j) -> M_i(S_j)
5432 * Note that plugging in S_j in C_ij may also result in an empty set
5433 * and this contribution should simply be discarded.
5435 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5436 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5437 __isl_keep isl_pw_aff *subs)
5439 int i, j, n;
5440 isl_pw_multi_aff *res;
5442 if (!pma || !subs)
5443 return isl_pw_multi_aff_free(pma);
5445 n = pma->n * subs->n;
5446 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5448 for (i = 0; i < pma->n; ++i) {
5449 for (j = 0; j < subs->n; ++j) {
5450 isl_set *common;
5451 isl_multi_aff *res_ij;
5452 int empty;
5454 common = isl_set_intersect(
5455 isl_set_copy(pma->p[i].set),
5456 isl_set_copy(subs->p[j].set));
5457 common = isl_set_substitute(common,
5458 type, pos, subs->p[j].aff);
5459 empty = isl_set_plain_is_empty(common);
5460 if (empty < 0 || empty) {
5461 isl_set_free(common);
5462 if (empty < 0)
5463 goto error;
5464 continue;
5467 res_ij = isl_multi_aff_substitute(
5468 isl_multi_aff_copy(pma->p[i].maff),
5469 type, pos, subs->p[j].aff);
5471 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5475 isl_pw_multi_aff_free(pma);
5476 return res;
5477 error:
5478 isl_pw_multi_aff_free(pma);
5479 isl_pw_multi_aff_free(res);
5480 return NULL;
5483 /* Compute the preimage of a range of dimensions in the affine expression "src"
5484 * under "ma" and put the result in "dst". The number of dimensions in "src"
5485 * that precede the range is given by "n_before". The number of dimensions
5486 * in the range is given by the number of output dimensions of "ma".
5487 * The number of dimensions that follow the range is given by "n_after".
5488 * If "has_denom" is set (to one),
5489 * then "src" and "dst" have an extra initial denominator.
5490 * "n_div_ma" is the number of existentials in "ma"
5491 * "n_div_bset" is the number of existentials in "src"
5492 * The resulting "dst" (which is assumed to have been allocated by
5493 * the caller) contains coefficients for both sets of existentials,
5494 * first those in "ma" and then those in "src".
5495 * f, c1, c2 and g are temporary objects that have been initialized
5496 * by the caller.
5498 * Let src represent the expression
5500 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5502 * and let ma represent the expressions
5504 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5506 * We start out with the following expression for dst:
5508 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5510 * with the multiplication factor f initially equal to 1
5511 * and f \sum_i b_i v_i kept separately.
5512 * For each x_i that we substitute, we multiply the numerator
5513 * (and denominator) of dst by c_1 = m_i and add the numerator
5514 * of the x_i expression multiplied by c_2 = f b_i,
5515 * after removing the common factors of c_1 and c_2.
5516 * The multiplication factor f also needs to be multiplied by c_1
5517 * for the next x_j, j > i.
5519 void isl_seq_preimage(isl_int *dst, isl_int *src,
5520 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5521 int n_div_ma, int n_div_bmap,
5522 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5524 int i;
5525 int n_param, n_in, n_out;
5526 int o_dst, o_src;
5528 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5529 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5530 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5532 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5533 o_dst = o_src = has_denom + 1 + n_param + n_before;
5534 isl_seq_clr(dst + o_dst, n_in);
5535 o_dst += n_in;
5536 o_src += n_out;
5537 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5538 o_dst += n_after;
5539 o_src += n_after;
5540 isl_seq_clr(dst + o_dst, n_div_ma);
5541 o_dst += n_div_ma;
5542 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5544 isl_int_set_si(f, 1);
5546 for (i = 0; i < n_out; ++i) {
5547 int offset = has_denom + 1 + n_param + n_before + i;
5549 if (isl_int_is_zero(src[offset]))
5550 continue;
5551 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5552 isl_int_mul(c2, f, src[offset]);
5553 isl_int_gcd(g, c1, c2);
5554 isl_int_divexact(c1, c1, g);
5555 isl_int_divexact(c2, c2, g);
5557 isl_int_mul(f, f, c1);
5558 o_dst = has_denom;
5559 o_src = 1;
5560 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5561 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5562 o_dst += 1 + n_param;
5563 o_src += 1 + n_param;
5564 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5565 o_dst += n_before;
5566 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5567 c2, ma->u.p[i]->v->el + o_src, n_in);
5568 o_dst += n_in;
5569 o_src += n_in;
5570 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5571 o_dst += n_after;
5572 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5573 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5574 o_dst += n_div_ma;
5575 o_src += n_div_ma;
5576 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5577 if (has_denom)
5578 isl_int_mul(dst[0], dst[0], c1);
5582 /* Compute the pullback of "aff" by the function represented by "ma".
5583 * In other words, plug in "ma" in "aff". The result is an affine expression
5584 * defined over the domain space of "ma".
5586 * If "aff" is represented by
5588 * (a(p) + b x + c(divs))/d
5590 * and ma is represented by
5592 * x = D(p) + F(y) + G(divs')
5594 * then the result is
5596 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5598 * The divs in the local space of the input are similarly adjusted
5599 * through a call to isl_local_space_preimage_multi_aff.
5601 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5602 __isl_take isl_multi_aff *ma)
5604 isl_aff *res = NULL;
5605 isl_local_space *ls;
5606 int n_div_aff, n_div_ma;
5607 isl_int f, c1, c2, g;
5609 ma = isl_multi_aff_align_divs(ma);
5610 if (!aff || !ma)
5611 goto error;
5613 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5614 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5616 ls = isl_aff_get_domain_local_space(aff);
5617 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5618 res = isl_aff_alloc(ls);
5619 if (!res)
5620 goto error;
5622 isl_int_init(f);
5623 isl_int_init(c1);
5624 isl_int_init(c2);
5625 isl_int_init(g);
5627 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5628 f, c1, c2, g, 1);
5630 isl_int_clear(f);
5631 isl_int_clear(c1);
5632 isl_int_clear(c2);
5633 isl_int_clear(g);
5635 isl_aff_free(aff);
5636 isl_multi_aff_free(ma);
5637 res = isl_aff_normalize(res);
5638 return res;
5639 error:
5640 isl_aff_free(aff);
5641 isl_multi_aff_free(ma);
5642 isl_aff_free(res);
5643 return NULL;
5646 /* Compute the pullback of "aff1" by the function represented by "aff2".
5647 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5648 * defined over the domain space of "aff1".
5650 * The domain of "aff1" should match the range of "aff2", which means
5651 * that it should be single-dimensional.
5653 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5654 __isl_take isl_aff *aff2)
5656 isl_multi_aff *ma;
5658 ma = isl_multi_aff_from_aff(aff2);
5659 return isl_aff_pullback_multi_aff(aff1, ma);
5662 /* Compute the pullback of "ma1" by the function represented by "ma2".
5663 * In other words, plug in "ma2" in "ma1".
5665 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5667 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5668 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5670 int i;
5671 isl_space *space = NULL;
5673 ma2 = isl_multi_aff_align_divs(ma2);
5674 ma1 = isl_multi_aff_cow(ma1);
5675 if (!ma1 || !ma2)
5676 goto error;
5678 space = isl_space_join(isl_multi_aff_get_space(ma2),
5679 isl_multi_aff_get_space(ma1));
5681 for (i = 0; i < ma1->n; ++i) {
5682 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5683 isl_multi_aff_copy(ma2));
5684 if (!ma1->u.p[i])
5685 goto error;
5688 ma1 = isl_multi_aff_reset_space(ma1, space);
5689 isl_multi_aff_free(ma2);
5690 return ma1;
5691 error:
5692 isl_space_free(space);
5693 isl_multi_aff_free(ma2);
5694 isl_multi_aff_free(ma1);
5695 return NULL;
5698 /* Compute the pullback of "ma1" by the function represented by "ma2".
5699 * In other words, plug in "ma2" in "ma1".
5701 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5702 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5704 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5705 &isl_multi_aff_pullback_multi_aff_aligned);
5708 /* Extend the local space of "dst" to include the divs
5709 * in the local space of "src".
5711 * If "src" does not have any divs or if the local spaces of "dst" and
5712 * "src" are the same, then no extension is required.
5714 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5715 __isl_keep isl_aff *src)
5717 isl_ctx *ctx;
5718 int src_n_div, dst_n_div;
5719 int *exp1 = NULL;
5720 int *exp2 = NULL;
5721 isl_bool equal;
5722 isl_mat *div;
5724 if (!src || !dst)
5725 return isl_aff_free(dst);
5727 ctx = isl_aff_get_ctx(src);
5728 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5729 if (equal < 0)
5730 return isl_aff_free(dst);
5731 if (!equal)
5732 isl_die(ctx, isl_error_invalid,
5733 "spaces don't match", goto error);
5735 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5736 if (src_n_div == 0)
5737 return dst;
5738 equal = isl_local_space_is_equal(src->ls, dst->ls);
5739 if (equal < 0)
5740 return isl_aff_free(dst);
5741 if (equal)
5742 return dst;
5744 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5745 exp1 = isl_alloc_array(ctx, int, src_n_div);
5746 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5747 if (!exp1 || (dst_n_div && !exp2))
5748 goto error;
5750 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5751 dst = isl_aff_expand_divs(dst, div, exp2);
5752 free(exp1);
5753 free(exp2);
5755 return dst;
5756 error:
5757 free(exp1);
5758 free(exp2);
5759 return isl_aff_free(dst);
5762 /* Adjust the local spaces of the affine expressions in "maff"
5763 * such that they all have the save divs.
5765 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5766 __isl_take isl_multi_aff *maff)
5768 int i;
5770 if (!maff)
5771 return NULL;
5772 if (maff->n == 0)
5773 return maff;
5774 maff = isl_multi_aff_cow(maff);
5775 if (!maff)
5776 return NULL;
5778 for (i = 1; i < maff->n; ++i)
5779 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5780 for (i = 1; i < maff->n; ++i) {
5781 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5782 if (!maff->u.p[i])
5783 return isl_multi_aff_free(maff);
5786 return maff;
5789 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5791 aff = isl_aff_cow(aff);
5792 if (!aff)
5793 return NULL;
5795 aff->ls = isl_local_space_lift(aff->ls);
5796 if (!aff->ls)
5797 return isl_aff_free(aff);
5799 return aff;
5802 /* Lift "maff" to a space with extra dimensions such that the result
5803 * has no more existentially quantified variables.
5804 * If "ls" is not NULL, then *ls is assigned the local space that lies
5805 * at the basis of the lifting applied to "maff".
5807 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5808 __isl_give isl_local_space **ls)
5810 int i;
5811 isl_space *space;
5812 unsigned n_div;
5814 if (ls)
5815 *ls = NULL;
5817 if (!maff)
5818 return NULL;
5820 if (maff->n == 0) {
5821 if (ls) {
5822 isl_space *space = isl_multi_aff_get_domain_space(maff);
5823 *ls = isl_local_space_from_space(space);
5824 if (!*ls)
5825 return isl_multi_aff_free(maff);
5827 return maff;
5830 maff = isl_multi_aff_cow(maff);
5831 maff = isl_multi_aff_align_divs(maff);
5832 if (!maff)
5833 return NULL;
5835 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5836 space = isl_multi_aff_get_space(maff);
5837 space = isl_space_lift(isl_space_domain(space), n_div);
5838 space = isl_space_extend_domain_with_range(space,
5839 isl_multi_aff_get_space(maff));
5840 if (!space)
5841 return isl_multi_aff_free(maff);
5842 isl_space_free(maff->space);
5843 maff->space = space;
5845 if (ls) {
5846 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5847 if (!*ls)
5848 return isl_multi_aff_free(maff);
5851 for (i = 0; i < maff->n; ++i) {
5852 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5853 if (!maff->u.p[i])
5854 goto error;
5857 return maff;
5858 error:
5859 if (ls)
5860 isl_local_space_free(*ls);
5861 return isl_multi_aff_free(maff);
5865 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5867 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5868 __isl_keep isl_pw_multi_aff *pma, int pos)
5870 int i;
5871 int n_out;
5872 isl_space *space;
5873 isl_pw_aff *pa;
5875 if (!pma)
5876 return NULL;
5878 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5879 if (pos < 0 || pos >= n_out)
5880 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5881 "index out of bounds", return NULL);
5883 space = isl_pw_multi_aff_get_space(pma);
5884 space = isl_space_drop_dims(space, isl_dim_out,
5885 pos + 1, n_out - pos - 1);
5886 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5888 pa = isl_pw_aff_alloc_size(space, pma->n);
5889 for (i = 0; i < pma->n; ++i) {
5890 isl_aff *aff;
5891 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5892 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5895 return pa;
5898 /* Return an isl_pw_multi_aff with the given "set" as domain and
5899 * an unnamed zero-dimensional range.
5901 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5902 __isl_take isl_set *set)
5904 isl_multi_aff *ma;
5905 isl_space *space;
5907 space = isl_set_get_space(set);
5908 space = isl_space_from_domain(space);
5909 ma = isl_multi_aff_zero(space);
5910 return isl_pw_multi_aff_alloc(set, ma);
5913 /* Add an isl_pw_multi_aff with the given "set" as domain and
5914 * an unnamed zero-dimensional range to *user.
5916 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5917 void *user)
5919 isl_union_pw_multi_aff **upma = user;
5920 isl_pw_multi_aff *pma;
5922 pma = isl_pw_multi_aff_from_domain(set);
5923 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5925 return isl_stat_ok;
5928 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5929 * an unnamed zero-dimensional range.
5931 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5932 __isl_take isl_union_set *uset)
5934 isl_space *space;
5935 isl_union_pw_multi_aff *upma;
5937 if (!uset)
5938 return NULL;
5940 space = isl_union_set_get_space(uset);
5941 upma = isl_union_pw_multi_aff_empty(space);
5943 if (isl_union_set_foreach_set(uset,
5944 &add_pw_multi_aff_from_domain, &upma) < 0)
5945 goto error;
5947 isl_union_set_free(uset);
5948 return upma;
5949 error:
5950 isl_union_set_free(uset);
5951 isl_union_pw_multi_aff_free(upma);
5952 return NULL;
5955 /* Convert "pma" to an isl_map and add it to *umap.
5957 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5958 void *user)
5960 isl_union_map **umap = user;
5961 isl_map *map;
5963 map = isl_map_from_pw_multi_aff(pma);
5964 *umap = isl_union_map_add_map(*umap, map);
5966 return isl_stat_ok;
5969 /* Construct a union map mapping the domain of the union
5970 * piecewise multi-affine expression to its range, with each dimension
5971 * in the range equated to the corresponding affine expression on its cell.
5973 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5974 __isl_take isl_union_pw_multi_aff *upma)
5976 isl_space *space;
5977 isl_union_map *umap;
5979 if (!upma)
5980 return NULL;
5982 space = isl_union_pw_multi_aff_get_space(upma);
5983 umap = isl_union_map_empty(space);
5985 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5986 &map_from_pw_multi_aff, &umap) < 0)
5987 goto error;
5989 isl_union_pw_multi_aff_free(upma);
5990 return umap;
5991 error:
5992 isl_union_pw_multi_aff_free(upma);
5993 isl_union_map_free(umap);
5994 return NULL;
5997 /* Local data for bin_entry and the callback "fn".
5999 struct isl_union_pw_multi_aff_bin_data {
6000 isl_union_pw_multi_aff *upma2;
6001 isl_union_pw_multi_aff *res;
6002 isl_pw_multi_aff *pma;
6003 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6006 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6007 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6009 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6011 struct isl_union_pw_multi_aff_bin_data *data = user;
6012 isl_stat r;
6014 data->pma = pma;
6015 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6016 data->fn, data);
6017 isl_pw_multi_aff_free(pma);
6019 return r;
6022 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6023 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6024 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6025 * as *entry. The callback should adjust data->res if desired.
6027 static __isl_give isl_union_pw_multi_aff *bin_op(
6028 __isl_take isl_union_pw_multi_aff *upma1,
6029 __isl_take isl_union_pw_multi_aff *upma2,
6030 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6032 isl_space *space;
6033 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6035 space = isl_union_pw_multi_aff_get_space(upma2);
6036 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6037 space = isl_union_pw_multi_aff_get_space(upma1);
6038 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6040 if (!upma1 || !upma2)
6041 goto error;
6043 data.upma2 = upma2;
6044 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6045 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6046 &bin_entry, &data) < 0)
6047 goto error;
6049 isl_union_pw_multi_aff_free(upma1);
6050 isl_union_pw_multi_aff_free(upma2);
6051 return data.res;
6052 error:
6053 isl_union_pw_multi_aff_free(upma1);
6054 isl_union_pw_multi_aff_free(upma2);
6055 isl_union_pw_multi_aff_free(data.res);
6056 return NULL;
6059 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6060 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6062 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6063 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6065 isl_space *space;
6067 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6068 isl_pw_multi_aff_get_space(pma2));
6069 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6070 &isl_multi_aff_range_product);
6073 /* Given two isl_pw_multi_affs A -> B and C -> D,
6074 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6076 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6077 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6079 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6080 &pw_multi_aff_range_product);
6083 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6084 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6086 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6087 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6089 isl_space *space;
6091 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6092 isl_pw_multi_aff_get_space(pma2));
6093 space = isl_space_flatten_range(space);
6094 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6095 &isl_multi_aff_flat_range_product);
6098 /* Given two isl_pw_multi_affs A -> B and C -> D,
6099 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6101 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6102 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6104 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6105 &pw_multi_aff_flat_range_product);
6108 /* If data->pma and "pma2" have the same domain space, then compute
6109 * their flat range product and the result to data->res.
6111 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6112 void *user)
6114 struct isl_union_pw_multi_aff_bin_data *data = user;
6116 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6117 pma2->dim, isl_dim_in)) {
6118 isl_pw_multi_aff_free(pma2);
6119 return isl_stat_ok;
6122 pma2 = isl_pw_multi_aff_flat_range_product(
6123 isl_pw_multi_aff_copy(data->pma), pma2);
6125 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6127 return isl_stat_ok;
6130 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6131 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6133 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6134 __isl_take isl_union_pw_multi_aff *upma1,
6135 __isl_take isl_union_pw_multi_aff *upma2)
6137 return bin_op(upma1, upma2, &flat_range_product_entry);
6140 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6141 * The parameters are assumed to have been aligned.
6143 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6144 * except that it works on two different isl_pw_* types.
6146 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6147 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6148 __isl_take isl_pw_aff *pa)
6150 int i, j, n;
6151 isl_pw_multi_aff *res = NULL;
6153 if (!pma || !pa)
6154 goto error;
6156 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6157 pa->dim, isl_dim_in))
6158 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6159 "domains don't match", goto error);
6160 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6161 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6162 "index out of bounds", goto error);
6164 n = pma->n * pa->n;
6165 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6167 for (i = 0; i < pma->n; ++i) {
6168 for (j = 0; j < pa->n; ++j) {
6169 isl_set *common;
6170 isl_multi_aff *res_ij;
6171 int empty;
6173 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6174 isl_set_copy(pa->p[j].set));
6175 empty = isl_set_plain_is_empty(common);
6176 if (empty < 0 || empty) {
6177 isl_set_free(common);
6178 if (empty < 0)
6179 goto error;
6180 continue;
6183 res_ij = isl_multi_aff_set_aff(
6184 isl_multi_aff_copy(pma->p[i].maff), pos,
6185 isl_aff_copy(pa->p[j].aff));
6186 res_ij = isl_multi_aff_gist(res_ij,
6187 isl_set_copy(common));
6189 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6193 isl_pw_multi_aff_free(pma);
6194 isl_pw_aff_free(pa);
6195 return res;
6196 error:
6197 isl_pw_multi_aff_free(pma);
6198 isl_pw_aff_free(pa);
6199 return isl_pw_multi_aff_free(res);
6202 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6204 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6205 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6206 __isl_take isl_pw_aff *pa)
6208 isl_bool equal_params;
6210 if (!pma || !pa)
6211 goto error;
6212 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6213 if (equal_params < 0)
6214 goto error;
6215 if (equal_params)
6216 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6217 if (!isl_space_has_named_params(pma->dim) ||
6218 !isl_space_has_named_params(pa->dim))
6219 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6220 "unaligned unnamed parameters", goto error);
6221 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6222 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6223 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6224 error:
6225 isl_pw_multi_aff_free(pma);
6226 isl_pw_aff_free(pa);
6227 return NULL;
6230 /* Do the parameters of "pa" match those of "space"?
6232 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6233 __isl_keep isl_space *space)
6235 isl_space *pa_space;
6236 isl_bool match;
6238 if (!pa || !space)
6239 return isl_bool_error;
6241 pa_space = isl_pw_aff_get_space(pa);
6243 match = isl_space_has_equal_params(space, pa_space);
6245 isl_space_free(pa_space);
6246 return match;
6249 /* Check that the domain space of "pa" matches "space".
6251 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6252 __isl_keep isl_space *space)
6254 isl_space *pa_space;
6255 isl_bool match;
6257 if (!pa || !space)
6258 return isl_stat_error;
6260 pa_space = isl_pw_aff_get_space(pa);
6262 match = isl_space_has_equal_params(space, pa_space);
6263 if (match < 0)
6264 goto error;
6265 if (!match)
6266 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6267 "parameters don't match", goto error);
6268 match = isl_space_tuple_is_equal(space, isl_dim_in,
6269 pa_space, isl_dim_in);
6270 if (match < 0)
6271 goto error;
6272 if (!match)
6273 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6274 "domains don't match", goto error);
6275 isl_space_free(pa_space);
6276 return isl_stat_ok;
6277 error:
6278 isl_space_free(pa_space);
6279 return isl_stat_error;
6282 #undef BASE
6283 #define BASE pw_aff
6284 #undef DOMBASE
6285 #define DOMBASE set
6287 #include <isl_multi_explicit_domain.c>
6288 #include <isl_multi_pw_aff_explicit_domain.c>
6289 #include <isl_multi_templ.c>
6290 #include <isl_multi_apply_set.c>
6291 #include <isl_multi_coalesce.c>
6292 #include <isl_multi_dims.c>
6293 #include <isl_multi_gist.c>
6294 #include <isl_multi_hash.c>
6295 #include <isl_multi_align_set.c>
6296 #include <isl_multi_intersect.c>
6298 /* Does "mpa" have a non-trivial explicit domain?
6300 * The explicit domain, if present, is trivial if it represents
6301 * an (obviously) universe set.
6303 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6304 __isl_keep isl_multi_pw_aff *mpa)
6306 if (!mpa)
6307 return isl_bool_error;
6308 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6309 return isl_bool_false;
6310 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6313 /* Scale the elements of "pma" by the corresponding elements of "mv".
6315 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6316 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6318 int i;
6319 isl_bool equal_params;
6321 pma = isl_pw_multi_aff_cow(pma);
6322 if (!pma || !mv)
6323 goto error;
6324 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6325 mv->space, isl_dim_set))
6326 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6327 "spaces don't match", goto error);
6328 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6329 if (equal_params < 0)
6330 goto error;
6331 if (!equal_params) {
6332 pma = isl_pw_multi_aff_align_params(pma,
6333 isl_multi_val_get_space(mv));
6334 mv = isl_multi_val_align_params(mv,
6335 isl_pw_multi_aff_get_space(pma));
6336 if (!pma || !mv)
6337 goto error;
6340 for (i = 0; i < pma->n; ++i) {
6341 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6342 isl_multi_val_copy(mv));
6343 if (!pma->p[i].maff)
6344 goto error;
6347 isl_multi_val_free(mv);
6348 return pma;
6349 error:
6350 isl_multi_val_free(mv);
6351 isl_pw_multi_aff_free(pma);
6352 return NULL;
6355 /* This function is called for each entry of an isl_union_pw_multi_aff.
6356 * If the space of the entry matches that of data->mv,
6357 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6358 * Otherwise, return an empty isl_pw_multi_aff.
6360 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6361 __isl_take isl_pw_multi_aff *pma, void *user)
6363 isl_multi_val *mv = user;
6365 if (!pma)
6366 return NULL;
6367 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6368 mv->space, isl_dim_set)) {
6369 isl_space *space = isl_pw_multi_aff_get_space(pma);
6370 isl_pw_multi_aff_free(pma);
6371 return isl_pw_multi_aff_empty(space);
6374 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6377 /* Scale the elements of "upma" by the corresponding elements of "mv",
6378 * for those entries that match the space of "mv".
6380 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6381 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6383 upma = isl_union_pw_multi_aff_align_params(upma,
6384 isl_multi_val_get_space(mv));
6385 mv = isl_multi_val_align_params(mv,
6386 isl_union_pw_multi_aff_get_space(upma));
6387 if (!upma || !mv)
6388 goto error;
6390 return isl_union_pw_multi_aff_transform(upma,
6391 &union_pw_multi_aff_scale_multi_val_entry, mv);
6393 isl_multi_val_free(mv);
6394 return upma;
6395 error:
6396 isl_multi_val_free(mv);
6397 isl_union_pw_multi_aff_free(upma);
6398 return NULL;
6401 /* Construct and return a piecewise multi affine expression
6402 * in the given space with value zero in each of the output dimensions and
6403 * a universe domain.
6405 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6407 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6410 /* Construct and return a piecewise multi affine expression
6411 * that is equal to the given piecewise affine expression.
6413 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6414 __isl_take isl_pw_aff *pa)
6416 int i;
6417 isl_space *space;
6418 isl_pw_multi_aff *pma;
6420 if (!pa)
6421 return NULL;
6423 space = isl_pw_aff_get_space(pa);
6424 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6426 for (i = 0; i < pa->n; ++i) {
6427 isl_set *set;
6428 isl_multi_aff *ma;
6430 set = isl_set_copy(pa->p[i].set);
6431 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6432 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6435 isl_pw_aff_free(pa);
6436 return pma;
6439 /* Construct a set or map mapping the shared (parameter) domain
6440 * of the piecewise affine expressions to the range of "mpa"
6441 * with each dimension in the range equated to the
6442 * corresponding piecewise affine expression.
6444 static __isl_give isl_map *map_from_multi_pw_aff(
6445 __isl_take isl_multi_pw_aff *mpa)
6447 int i;
6448 isl_space *space;
6449 isl_map *map;
6451 if (!mpa)
6452 return NULL;
6454 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6455 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6456 "invalid space", goto error);
6458 space = isl_multi_pw_aff_get_domain_space(mpa);
6459 map = isl_map_universe(isl_space_from_domain(space));
6461 for (i = 0; i < mpa->n; ++i) {
6462 isl_pw_aff *pa;
6463 isl_map *map_i;
6465 pa = isl_pw_aff_copy(mpa->u.p[i]);
6466 map_i = map_from_pw_aff(pa);
6468 map = isl_map_flat_range_product(map, map_i);
6471 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6473 isl_multi_pw_aff_free(mpa);
6474 return map;
6475 error:
6476 isl_multi_pw_aff_free(mpa);
6477 return NULL;
6480 /* Construct a map mapping the shared domain
6481 * of the piecewise affine expressions to the range of "mpa"
6482 * with each dimension in the range equated to the
6483 * corresponding piecewise affine expression.
6485 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6487 if (!mpa)
6488 return NULL;
6489 if (isl_space_is_set(mpa->space))
6490 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6491 "space of input is not a map", goto error);
6493 return map_from_multi_pw_aff(mpa);
6494 error:
6495 isl_multi_pw_aff_free(mpa);
6496 return NULL;
6499 /* Construct a set mapping the shared parameter domain
6500 * of the piecewise affine expressions to the space of "mpa"
6501 * with each dimension in the range equated to the
6502 * corresponding piecewise affine expression.
6504 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6506 if (!mpa)
6507 return NULL;
6508 if (!isl_space_is_set(mpa->space))
6509 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6510 "space of input is not a set", goto error);
6512 return map_from_multi_pw_aff(mpa);
6513 error:
6514 isl_multi_pw_aff_free(mpa);
6515 return NULL;
6518 /* Construct and return a piecewise multi affine expression
6519 * that is equal to the given multi piecewise affine expression
6520 * on the shared domain of the piecewise affine expressions,
6521 * in the special case of a 0D multi piecewise affine expression.
6523 * Create a piecewise multi affine expression with the explicit domain of
6524 * the 0D multi piecewise affine expression as domain.
6526 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6527 __isl_take isl_multi_pw_aff *mpa)
6529 isl_space *space;
6530 isl_set *dom;
6531 isl_multi_aff *ma;
6533 space = isl_multi_pw_aff_get_space(mpa);
6534 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6535 isl_multi_pw_aff_free(mpa);
6537 ma = isl_multi_aff_zero(space);
6538 return isl_pw_multi_aff_alloc(dom, ma);
6541 /* Construct and return a piecewise multi affine expression
6542 * that is equal to the given multi piecewise affine expression
6543 * on the shared domain of the piecewise affine expressions.
6545 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6546 __isl_take isl_multi_pw_aff *mpa)
6548 int i;
6549 isl_space *space;
6550 isl_pw_aff *pa;
6551 isl_pw_multi_aff *pma;
6553 if (!mpa)
6554 return NULL;
6556 if (mpa->n == 0)
6557 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6559 space = isl_multi_pw_aff_get_space(mpa);
6560 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6561 pma = isl_pw_multi_aff_from_pw_aff(pa);
6563 for (i = 1; i < mpa->n; ++i) {
6564 isl_pw_multi_aff *pma_i;
6566 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6567 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6568 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6571 pma = isl_pw_multi_aff_reset_space(pma, space);
6573 isl_multi_pw_aff_free(mpa);
6574 return pma;
6577 /* Construct and return a multi piecewise affine expression
6578 * that is equal to the given multi affine expression.
6580 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6581 __isl_take isl_multi_aff *ma)
6583 int i, n;
6584 isl_multi_pw_aff *mpa;
6586 if (!ma)
6587 return NULL;
6589 n = isl_multi_aff_dim(ma, isl_dim_out);
6590 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6592 for (i = 0; i < n; ++i) {
6593 isl_pw_aff *pa;
6595 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6596 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6599 isl_multi_aff_free(ma);
6600 return mpa;
6603 /* Construct and return a multi piecewise affine expression
6604 * that is equal to the given piecewise multi affine expression.
6606 * If the resulting multi piecewise affine expression has
6607 * an explicit domain, then assign it the domain of the input.
6608 * In other cases, the domain is stored in the individual elements.
6610 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6611 __isl_take isl_pw_multi_aff *pma)
6613 int i, n;
6614 isl_space *space;
6615 isl_multi_pw_aff *mpa;
6617 if (!pma)
6618 return NULL;
6620 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6621 space = isl_pw_multi_aff_get_space(pma);
6622 mpa = isl_multi_pw_aff_alloc(space);
6624 for (i = 0; i < n; ++i) {
6625 isl_pw_aff *pa;
6627 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6628 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6630 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6631 isl_set *dom;
6633 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6634 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6637 isl_pw_multi_aff_free(pma);
6638 return mpa;
6641 /* Do "pa1" and "pa2" represent the same function?
6643 * We first check if they are obviously equal.
6644 * If not, we convert them to maps and check if those are equal.
6646 * If "pa1" or "pa2" contain any NaNs, then they are considered
6647 * not to be the same. A NaN is not equal to anything, not even
6648 * to another NaN.
6650 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6651 __isl_keep isl_pw_aff *pa2)
6653 isl_bool equal;
6654 isl_bool has_nan;
6655 isl_map *map1, *map2;
6657 if (!pa1 || !pa2)
6658 return isl_bool_error;
6660 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6661 if (equal < 0 || equal)
6662 return equal;
6663 has_nan = either_involves_nan(pa1, pa2);
6664 if (has_nan < 0)
6665 return isl_bool_error;
6666 if (has_nan)
6667 return isl_bool_false;
6669 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6670 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6671 equal = isl_map_is_equal(map1, map2);
6672 isl_map_free(map1);
6673 isl_map_free(map2);
6675 return equal;
6678 /* Do "mpa1" and "mpa2" represent the same function?
6680 * Note that we cannot convert the entire isl_multi_pw_aff
6681 * to a map because the domains of the piecewise affine expressions
6682 * may not be the same.
6684 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6685 __isl_keep isl_multi_pw_aff *mpa2)
6687 int i;
6688 isl_bool equal, equal_params;
6690 if (!mpa1 || !mpa2)
6691 return isl_bool_error;
6693 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6694 if (equal_params < 0)
6695 return isl_bool_error;
6696 if (!equal_params) {
6697 if (!isl_space_has_named_params(mpa1->space))
6698 return isl_bool_false;
6699 if (!isl_space_has_named_params(mpa2->space))
6700 return isl_bool_false;
6701 mpa1 = isl_multi_pw_aff_copy(mpa1);
6702 mpa2 = isl_multi_pw_aff_copy(mpa2);
6703 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6704 isl_multi_pw_aff_get_space(mpa2));
6705 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6706 isl_multi_pw_aff_get_space(mpa1));
6707 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6708 isl_multi_pw_aff_free(mpa1);
6709 isl_multi_pw_aff_free(mpa2);
6710 return equal;
6713 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6714 if (equal < 0 || !equal)
6715 return equal;
6717 for (i = 0; i < mpa1->n; ++i) {
6718 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6719 if (equal < 0 || !equal)
6720 return equal;
6723 return isl_bool_true;
6726 /* Do "pma1" and "pma2" represent the same function?
6728 * First check if they are obviously equal.
6729 * If not, then convert them to maps and check if those are equal.
6731 * If "pa1" or "pa2" contain any NaNs, then they are considered
6732 * not to be the same. A NaN is not equal to anything, not even
6733 * to another NaN.
6735 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6736 __isl_keep isl_pw_multi_aff *pma2)
6738 isl_bool equal;
6739 isl_bool has_nan;
6740 isl_map *map1, *map2;
6742 if (!pma1 || !pma2)
6743 return isl_bool_error;
6745 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6746 if (equal < 0 || equal)
6747 return equal;
6748 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6749 if (has_nan >= 0 && !has_nan)
6750 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6751 if (has_nan < 0 || has_nan)
6752 return isl_bool_not(has_nan);
6754 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6755 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6756 equal = isl_map_is_equal(map1, map2);
6757 isl_map_free(map1);
6758 isl_map_free(map2);
6760 return equal;
6763 /* Compute the pullback of "mpa" by the function represented by "ma".
6764 * In other words, plug in "ma" in "mpa".
6766 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6768 * If "mpa" has an explicit domain, then it is this domain
6769 * that needs to undergo a pullback, i.e., a preimage.
6771 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6772 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6774 int i;
6775 isl_space *space = NULL;
6777 mpa = isl_multi_pw_aff_cow(mpa);
6778 if (!mpa || !ma)
6779 goto error;
6781 space = isl_space_join(isl_multi_aff_get_space(ma),
6782 isl_multi_pw_aff_get_space(mpa));
6783 if (!space)
6784 goto error;
6786 for (i = 0; i < mpa->n; ++i) {
6787 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6788 isl_multi_aff_copy(ma));
6789 if (!mpa->u.p[i])
6790 goto error;
6792 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6793 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6794 isl_multi_aff_copy(ma));
6795 if (!mpa->u.dom)
6796 goto error;
6799 isl_multi_aff_free(ma);
6800 isl_space_free(mpa->space);
6801 mpa->space = space;
6802 return mpa;
6803 error:
6804 isl_space_free(space);
6805 isl_multi_pw_aff_free(mpa);
6806 isl_multi_aff_free(ma);
6807 return NULL;
6810 /* Compute the pullback of "mpa" by the function represented by "ma".
6811 * In other words, plug in "ma" in "mpa".
6813 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6814 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6816 isl_bool equal_params;
6818 if (!mpa || !ma)
6819 goto error;
6820 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6821 if (equal_params < 0)
6822 goto error;
6823 if (equal_params)
6824 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6825 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6826 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6827 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6828 error:
6829 isl_multi_pw_aff_free(mpa);
6830 isl_multi_aff_free(ma);
6831 return NULL;
6834 /* Compute the pullback of "mpa" by the function represented by "pma".
6835 * In other words, plug in "pma" in "mpa".
6837 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6839 * If "mpa" has an explicit domain, then it is this domain
6840 * that needs to undergo a pullback, i.e., a preimage.
6842 static __isl_give isl_multi_pw_aff *
6843 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6844 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6846 int i;
6847 isl_space *space = NULL;
6849 mpa = isl_multi_pw_aff_cow(mpa);
6850 if (!mpa || !pma)
6851 goto error;
6853 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6854 isl_multi_pw_aff_get_space(mpa));
6856 for (i = 0; i < mpa->n; ++i) {
6857 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6858 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6859 if (!mpa->u.p[i])
6860 goto error;
6862 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6863 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6864 isl_pw_multi_aff_copy(pma));
6865 if (!mpa->u.dom)
6866 goto error;
6869 isl_pw_multi_aff_free(pma);
6870 isl_space_free(mpa->space);
6871 mpa->space = space;
6872 return mpa;
6873 error:
6874 isl_space_free(space);
6875 isl_multi_pw_aff_free(mpa);
6876 isl_pw_multi_aff_free(pma);
6877 return NULL;
6880 /* Compute the pullback of "mpa" by the function represented by "pma".
6881 * In other words, plug in "pma" in "mpa".
6883 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6884 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6886 isl_bool equal_params;
6888 if (!mpa || !pma)
6889 goto error;
6890 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6891 if (equal_params < 0)
6892 goto error;
6893 if (equal_params)
6894 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6895 mpa = isl_multi_pw_aff_align_params(mpa,
6896 isl_pw_multi_aff_get_space(pma));
6897 pma = isl_pw_multi_aff_align_params(pma,
6898 isl_multi_pw_aff_get_space(mpa));
6899 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6900 error:
6901 isl_multi_pw_aff_free(mpa);
6902 isl_pw_multi_aff_free(pma);
6903 return NULL;
6906 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6907 * with the domain of "aff". The domain of the result is the same
6908 * as that of "mpa".
6909 * "mpa" and "aff" are assumed to have been aligned.
6911 * We first extract the parametric constant from "aff", defined
6912 * over the correct domain.
6913 * Then we add the appropriate combinations of the members of "mpa".
6914 * Finally, we add the integer divisions through recursive calls.
6916 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6917 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6919 int i, n_in, n_div;
6920 isl_space *space;
6921 isl_val *v;
6922 isl_pw_aff *pa;
6923 isl_aff *tmp;
6925 n_in = isl_aff_dim(aff, isl_dim_in);
6926 n_div = isl_aff_dim(aff, isl_dim_div);
6928 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6929 tmp = isl_aff_copy(aff);
6930 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6931 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6932 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6933 isl_space_dim(space, isl_dim_set));
6934 tmp = isl_aff_reset_domain_space(tmp, space);
6935 pa = isl_pw_aff_from_aff(tmp);
6937 for (i = 0; i < n_in; ++i) {
6938 isl_pw_aff *pa_i;
6940 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6941 continue;
6942 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6943 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6944 pa_i = isl_pw_aff_scale_val(pa_i, v);
6945 pa = isl_pw_aff_add(pa, pa_i);
6948 for (i = 0; i < n_div; ++i) {
6949 isl_aff *div;
6950 isl_pw_aff *pa_i;
6952 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6953 continue;
6954 div = isl_aff_get_div(aff, i);
6955 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6956 isl_multi_pw_aff_copy(mpa), div);
6957 pa_i = isl_pw_aff_floor(pa_i);
6958 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6959 pa_i = isl_pw_aff_scale_val(pa_i, v);
6960 pa = isl_pw_aff_add(pa, pa_i);
6963 isl_multi_pw_aff_free(mpa);
6964 isl_aff_free(aff);
6966 return pa;
6969 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6970 * with the domain of "aff". The domain of the result is the same
6971 * as that of "mpa".
6973 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6974 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6976 isl_bool equal_params;
6978 if (!aff || !mpa)
6979 goto error;
6980 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6981 if (equal_params < 0)
6982 goto error;
6983 if (equal_params)
6984 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6986 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6987 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6989 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6990 error:
6991 isl_aff_free(aff);
6992 isl_multi_pw_aff_free(mpa);
6993 return NULL;
6996 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6997 * with the domain of "pa". The domain of the result is the same
6998 * as that of "mpa".
6999 * "mpa" and "pa" are assumed to have been aligned.
7001 * We consider each piece in turn. Note that the domains of the
7002 * pieces are assumed to be disjoint and they remain disjoint
7003 * after taking the preimage (over the same function).
7005 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7006 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7008 isl_space *space;
7009 isl_pw_aff *res;
7010 int i;
7012 if (!mpa || !pa)
7013 goto error;
7015 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7016 isl_pw_aff_get_space(pa));
7017 res = isl_pw_aff_empty(space);
7019 for (i = 0; i < pa->n; ++i) {
7020 isl_pw_aff *pa_i;
7021 isl_set *domain;
7023 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7024 isl_multi_pw_aff_copy(mpa),
7025 isl_aff_copy(pa->p[i].aff));
7026 domain = isl_set_copy(pa->p[i].set);
7027 domain = isl_set_preimage_multi_pw_aff(domain,
7028 isl_multi_pw_aff_copy(mpa));
7029 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7030 res = isl_pw_aff_add_disjoint(res, pa_i);
7033 isl_pw_aff_free(pa);
7034 isl_multi_pw_aff_free(mpa);
7035 return res;
7036 error:
7037 isl_pw_aff_free(pa);
7038 isl_multi_pw_aff_free(mpa);
7039 return NULL;
7042 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7043 * with the domain of "pa". The domain of the result is the same
7044 * as that of "mpa".
7046 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7047 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7049 isl_bool equal_params;
7051 if (!pa || !mpa)
7052 goto error;
7053 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7054 if (equal_params < 0)
7055 goto error;
7056 if (equal_params)
7057 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7059 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7060 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7062 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7063 error:
7064 isl_pw_aff_free(pa);
7065 isl_multi_pw_aff_free(mpa);
7066 return NULL;
7069 /* Compute the pullback of "pa" by the function represented by "mpa".
7070 * In other words, plug in "mpa" in "pa".
7071 * "pa" and "mpa" are assumed to have been aligned.
7073 * The pullback is computed by applying "pa" to "mpa".
7075 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7076 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7078 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7081 /* Compute the pullback of "pa" by the function represented by "mpa".
7082 * In other words, plug in "mpa" in "pa".
7084 * The pullback is computed by applying "pa" to "mpa".
7086 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7087 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7089 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7092 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7093 * In other words, plug in "mpa2" in "mpa1".
7095 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7097 * We pullback each member of "mpa1" in turn.
7099 * If "mpa1" has an explicit domain, then it is this domain
7100 * that needs to undergo a pullback instead, i.e., a preimage.
7102 static __isl_give isl_multi_pw_aff *
7103 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7104 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7106 int i;
7107 isl_space *space = NULL;
7109 mpa1 = isl_multi_pw_aff_cow(mpa1);
7110 if (!mpa1 || !mpa2)
7111 goto error;
7113 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7114 isl_multi_pw_aff_get_space(mpa1));
7116 for (i = 0; i < mpa1->n; ++i) {
7117 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7118 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7119 if (!mpa1->u.p[i])
7120 goto error;
7123 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7124 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7125 isl_multi_pw_aff_copy(mpa2));
7126 if (!mpa1->u.dom)
7127 goto error;
7129 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7131 isl_multi_pw_aff_free(mpa2);
7132 return mpa1;
7133 error:
7134 isl_space_free(space);
7135 isl_multi_pw_aff_free(mpa1);
7136 isl_multi_pw_aff_free(mpa2);
7137 return NULL;
7140 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7141 * In other words, plug in "mpa2" in "mpa1".
7143 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7144 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7146 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7147 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7150 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7151 * of "mpa1" and "mpa2" live in the same space, construct map space
7152 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7153 * with this map space as extract argument.
7155 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7156 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7157 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7158 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7160 int match;
7161 isl_space *space1, *space2;
7162 isl_map *res;
7164 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7165 isl_multi_pw_aff_get_space(mpa2));
7166 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7167 isl_multi_pw_aff_get_space(mpa1));
7168 if (!mpa1 || !mpa2)
7169 goto error;
7170 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7171 mpa2->space, isl_dim_out);
7172 if (match < 0)
7173 goto error;
7174 if (!match)
7175 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7176 "range spaces don't match", goto error);
7177 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7178 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7179 space1 = isl_space_map_from_domain_and_range(space1, space2);
7181 res = order(mpa1, mpa2, space1);
7182 isl_multi_pw_aff_free(mpa1);
7183 isl_multi_pw_aff_free(mpa2);
7184 return res;
7185 error:
7186 isl_multi_pw_aff_free(mpa1);
7187 isl_multi_pw_aff_free(mpa2);
7188 return NULL;
7191 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7192 * where the function values are equal. "space" is the space of the result.
7193 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7195 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7196 * in the sequences are equal.
7198 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7199 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7200 __isl_take isl_space *space)
7202 int i, n;
7203 isl_map *res;
7205 res = isl_map_universe(space);
7207 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7208 for (i = 0; i < n; ++i) {
7209 isl_pw_aff *pa1, *pa2;
7210 isl_map *map;
7212 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7213 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7214 map = isl_pw_aff_eq_map(pa1, pa2);
7215 res = isl_map_intersect(res, map);
7218 return res;
7221 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7222 * where the function values are equal.
7224 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7225 __isl_take isl_multi_pw_aff *mpa2)
7227 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7228 &isl_multi_pw_aff_eq_map_on_space);
7231 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7232 * where the function values of "mpa1" is lexicographically satisfies "base"
7233 * compared to that of "mpa2". "space" is the space of the result.
7234 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7236 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7237 * if its i-th element satisfies "base" when compared to
7238 * the i-th element of "mpa2" while all previous elements are
7239 * pairwise equal.
7241 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7242 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7243 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7244 __isl_take isl_pw_aff *pa2),
7245 __isl_take isl_space *space)
7247 int i, n;
7248 isl_map *res, *rest;
7250 res = isl_map_empty(isl_space_copy(space));
7251 rest = isl_map_universe(space);
7253 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7254 for (i = 0; i < n; ++i) {
7255 isl_pw_aff *pa1, *pa2;
7256 isl_map *map;
7258 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7259 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7260 map = base(pa1, pa2);
7261 map = isl_map_intersect(map, isl_map_copy(rest));
7262 res = isl_map_union(res, map);
7264 if (i == n - 1)
7265 continue;
7267 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7268 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7269 map = isl_pw_aff_eq_map(pa1, pa2);
7270 rest = isl_map_intersect(rest, map);
7273 isl_map_free(rest);
7274 return res;
7277 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7278 * where the function value of "mpa1" is lexicographically less than that
7279 * of "mpa2". "space" is the space of the result.
7280 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7282 * "mpa1" is less than "mpa2" if its i-th element is smaller
7283 * than the i-th element of "mpa2" while all previous elements are
7284 * pairwise equal.
7286 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7287 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7288 __isl_take isl_space *space)
7290 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7291 &isl_pw_aff_lt_map, space);
7294 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7295 * where the function value of "mpa1" is lexicographically less than that
7296 * of "mpa2".
7298 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7299 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7301 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7302 &isl_multi_pw_aff_lex_lt_map_on_space);
7305 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7306 * where the function value of "mpa1" is lexicographically greater than that
7307 * of "mpa2". "space" is the space of the result.
7308 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7310 * "mpa1" is greater than "mpa2" if its i-th element is greater
7311 * than the i-th element of "mpa2" while all previous elements are
7312 * pairwise equal.
7314 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7315 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7316 __isl_take isl_space *space)
7318 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7319 &isl_pw_aff_gt_map, space);
7322 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7323 * where the function value of "mpa1" is lexicographically greater than that
7324 * of "mpa2".
7326 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7327 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7329 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7330 &isl_multi_pw_aff_lex_gt_map_on_space);
7333 /* Compare two isl_affs.
7335 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7336 * than "aff2" and 0 if they are equal.
7338 * The order is fairly arbitrary. We do consider expressions that only involve
7339 * earlier dimensions as "smaller".
7341 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7343 int cmp;
7344 int last1, last2;
7346 if (aff1 == aff2)
7347 return 0;
7349 if (!aff1)
7350 return -1;
7351 if (!aff2)
7352 return 1;
7354 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7355 if (cmp != 0)
7356 return cmp;
7358 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7359 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7360 if (last1 != last2)
7361 return last1 - last2;
7363 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7366 /* Compare two isl_pw_affs.
7368 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7369 * than "pa2" and 0 if they are equal.
7371 * The order is fairly arbitrary. We do consider expressions that only involve
7372 * earlier dimensions as "smaller".
7374 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7375 __isl_keep isl_pw_aff *pa2)
7377 int i;
7378 int cmp;
7380 if (pa1 == pa2)
7381 return 0;
7383 if (!pa1)
7384 return -1;
7385 if (!pa2)
7386 return 1;
7388 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7389 if (cmp != 0)
7390 return cmp;
7392 if (pa1->n != pa2->n)
7393 return pa1->n - pa2->n;
7395 for (i = 0; i < pa1->n; ++i) {
7396 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7397 if (cmp != 0)
7398 return cmp;
7399 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7400 if (cmp != 0)
7401 return cmp;
7404 return 0;
7407 /* Return a piecewise affine expression that is equal to "v" on "domain".
7409 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7410 __isl_take isl_val *v)
7412 isl_space *space;
7413 isl_local_space *ls;
7414 isl_aff *aff;
7416 space = isl_set_get_space(domain);
7417 ls = isl_local_space_from_space(space);
7418 aff = isl_aff_val_on_domain(ls, v);
7420 return isl_pw_aff_alloc(domain, aff);
7423 /* Return a multi affine expression that is equal to "mv" on domain
7424 * space "space".
7426 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7427 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7429 int i, n;
7430 isl_space *space2;
7431 isl_local_space *ls;
7432 isl_multi_aff *ma;
7434 if (!space || !mv)
7435 goto error;
7437 n = isl_multi_val_dim(mv, isl_dim_set);
7438 space2 = isl_multi_val_get_space(mv);
7439 space2 = isl_space_align_params(space2, isl_space_copy(space));
7440 space = isl_space_align_params(space, isl_space_copy(space2));
7441 space = isl_space_map_from_domain_and_range(space, space2);
7442 ma = isl_multi_aff_alloc(isl_space_copy(space));
7443 ls = isl_local_space_from_space(isl_space_domain(space));
7444 for (i = 0; i < n; ++i) {
7445 isl_val *v;
7446 isl_aff *aff;
7448 v = isl_multi_val_get_val(mv, i);
7449 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7450 ma = isl_multi_aff_set_aff(ma, i, aff);
7452 isl_local_space_free(ls);
7454 isl_multi_val_free(mv);
7455 return ma;
7456 error:
7457 isl_space_free(space);
7458 isl_multi_val_free(mv);
7459 return NULL;
7462 /* Return a piecewise multi-affine expression
7463 * that is equal to "mv" on "domain".
7465 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7466 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7468 isl_space *space;
7469 isl_multi_aff *ma;
7471 space = isl_set_get_space(domain);
7472 ma = isl_multi_aff_multi_val_on_space(space, mv);
7474 return isl_pw_multi_aff_alloc(domain, ma);
7477 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7478 * mv is the value that should be attained on each domain set
7479 * res collects the results
7481 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7482 isl_multi_val *mv;
7483 isl_union_pw_multi_aff *res;
7486 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7487 * and add it to data->res.
7489 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7490 void *user)
7492 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7493 isl_pw_multi_aff *pma;
7494 isl_multi_val *mv;
7496 mv = isl_multi_val_copy(data->mv);
7497 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7498 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7500 return data->res ? isl_stat_ok : isl_stat_error;
7503 /* Return a union piecewise multi-affine expression
7504 * that is equal to "mv" on "domain".
7506 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7507 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7509 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7510 isl_space *space;
7512 space = isl_union_set_get_space(domain);
7513 data.res = isl_union_pw_multi_aff_empty(space);
7514 data.mv = mv;
7515 if (isl_union_set_foreach_set(domain,
7516 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7517 data.res = isl_union_pw_multi_aff_free(data.res);
7518 isl_union_set_free(domain);
7519 isl_multi_val_free(mv);
7520 return data.res;
7523 /* Compute the pullback of data->pma by the function represented by "pma2",
7524 * provided the spaces match, and add the results to data->res.
7526 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7528 struct isl_union_pw_multi_aff_bin_data *data = user;
7530 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7531 pma2->dim, isl_dim_out)) {
7532 isl_pw_multi_aff_free(pma2);
7533 return isl_stat_ok;
7536 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7537 isl_pw_multi_aff_copy(data->pma), pma2);
7539 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7540 if (!data->res)
7541 return isl_stat_error;
7543 return isl_stat_ok;
7546 /* Compute the pullback of "upma1" by the function represented by "upma2".
7548 __isl_give isl_union_pw_multi_aff *
7549 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7550 __isl_take isl_union_pw_multi_aff *upma1,
7551 __isl_take isl_union_pw_multi_aff *upma2)
7553 return bin_op(upma1, upma2, &pullback_entry);
7556 /* Check that the domain space of "upa" matches "space".
7558 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7559 * can in principle never fail since the space "space" is that
7560 * of the isl_multi_union_pw_aff and is a set space such that
7561 * there is no domain space to match.
7563 * We check the parameters and double-check that "space" is
7564 * indeed that of a set.
7566 static isl_stat isl_union_pw_aff_check_match_domain_space(
7567 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7569 isl_space *upa_space;
7570 isl_bool match;
7572 if (!upa || !space)
7573 return isl_stat_error;
7575 match = isl_space_is_set(space);
7576 if (match < 0)
7577 return isl_stat_error;
7578 if (!match)
7579 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7580 "expecting set space", return -1);
7582 upa_space = isl_union_pw_aff_get_space(upa);
7583 match = isl_space_has_equal_params(space, upa_space);
7584 if (match < 0)
7585 goto error;
7586 if (!match)
7587 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7588 "parameters don't match", goto error);
7590 isl_space_free(upa_space);
7591 return isl_stat_ok;
7592 error:
7593 isl_space_free(upa_space);
7594 return isl_stat_error;
7597 /* Do the parameters of "upa" match those of "space"?
7599 static isl_bool isl_union_pw_aff_matching_params(
7600 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7602 isl_space *upa_space;
7603 isl_bool match;
7605 if (!upa || !space)
7606 return isl_bool_error;
7608 upa_space = isl_union_pw_aff_get_space(upa);
7610 match = isl_space_has_equal_params(space, upa_space);
7612 isl_space_free(upa_space);
7613 return match;
7616 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7617 * space represents the new parameters.
7618 * res collects the results.
7620 struct isl_union_pw_aff_reset_params_data {
7621 isl_space *space;
7622 isl_union_pw_aff *res;
7625 /* Replace the parameters of "pa" by data->space and
7626 * add the result to data->res.
7628 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7630 struct isl_union_pw_aff_reset_params_data *data = user;
7631 isl_space *space;
7633 space = isl_pw_aff_get_space(pa);
7634 space = isl_space_replace_params(space, data->space);
7635 pa = isl_pw_aff_reset_space(pa, space);
7636 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7638 return data->res ? isl_stat_ok : isl_stat_error;
7641 /* Replace the domain space of "upa" by "space".
7642 * Since a union expression does not have a (single) domain space,
7643 * "space" is necessarily a parameter space.
7645 * Since the order and the names of the parameters determine
7646 * the hash value, we need to create a new hash table.
7648 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7649 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7651 struct isl_union_pw_aff_reset_params_data data = { space };
7652 isl_bool match;
7654 match = isl_union_pw_aff_matching_params(upa, space);
7655 if (match < 0)
7656 upa = isl_union_pw_aff_free(upa);
7657 else if (match) {
7658 isl_space_free(space);
7659 return upa;
7662 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7663 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7664 data.res = isl_union_pw_aff_free(data.res);
7666 isl_union_pw_aff_free(upa);
7667 isl_space_free(space);
7668 return data.res;
7671 /* Return the floor of "pa".
7673 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7675 return isl_pw_aff_floor(pa);
7678 /* Given f, return floor(f).
7680 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7681 __isl_take isl_union_pw_aff *upa)
7683 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7686 /* Compute
7688 * upa mod m = upa - m * floor(upa/m)
7690 * with m an integer value.
7692 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7693 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7695 isl_union_pw_aff *res;
7697 if (!upa || !m)
7698 goto error;
7700 if (!isl_val_is_int(m))
7701 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7702 "expecting integer modulo", goto error);
7703 if (!isl_val_is_pos(m))
7704 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7705 "expecting positive modulo", goto error);
7707 res = isl_union_pw_aff_copy(upa);
7708 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7709 upa = isl_union_pw_aff_floor(upa);
7710 upa = isl_union_pw_aff_scale_val(upa, m);
7711 res = isl_union_pw_aff_sub(res, upa);
7713 return res;
7714 error:
7715 isl_val_free(m);
7716 isl_union_pw_aff_free(upa);
7717 return NULL;
7720 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7721 * pos is the output position that needs to be extracted.
7722 * res collects the results.
7724 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7725 int pos;
7726 isl_union_pw_aff *res;
7729 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7730 * (assuming it has such a dimension) and add it to data->res.
7732 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7734 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7735 int n_out;
7736 isl_pw_aff *pa;
7738 if (!pma)
7739 return isl_stat_error;
7741 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7742 if (data->pos >= n_out) {
7743 isl_pw_multi_aff_free(pma);
7744 return isl_stat_ok;
7747 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7748 isl_pw_multi_aff_free(pma);
7750 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7752 return data->res ? isl_stat_ok : isl_stat_error;
7755 /* Extract an isl_union_pw_aff corresponding to
7756 * output dimension "pos" of "upma".
7758 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7759 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7761 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7762 isl_space *space;
7764 if (!upma)
7765 return NULL;
7767 if (pos < 0)
7768 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7769 "cannot extract at negative position", return NULL);
7771 space = isl_union_pw_multi_aff_get_space(upma);
7772 data.res = isl_union_pw_aff_empty(space);
7773 data.pos = pos;
7774 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7775 &get_union_pw_aff, &data) < 0)
7776 data.res = isl_union_pw_aff_free(data.res);
7778 return data.res;
7781 /* Return a union piecewise affine expression
7782 * that is equal to "aff" on "domain".
7784 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7785 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7787 isl_pw_aff *pa;
7789 pa = isl_pw_aff_from_aff(aff);
7790 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7793 /* Return a union piecewise affine expression
7794 * that is equal to the parameter identified by "id" on "domain".
7796 * Make sure the parameter appears in the space passed to
7797 * isl_aff_param_on_domain_space_id.
7799 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7800 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7802 isl_space *space;
7803 isl_aff *aff;
7805 space = isl_union_set_get_space(domain);
7806 space = isl_space_add_param_id(space, isl_id_copy(id));
7807 aff = isl_aff_param_on_domain_space_id(space, id);
7808 return isl_union_pw_aff_aff_on_domain(domain, aff);
7811 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7812 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7813 * needs to attain.
7814 * "res" collects the results.
7816 struct isl_union_pw_aff_pw_aff_on_domain_data {
7817 isl_pw_aff *pa;
7818 isl_union_pw_aff *res;
7821 /* Construct a piecewise affine expression that is equal to data->pa
7822 * on "domain" and add the result to data->res.
7824 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7826 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7827 isl_pw_aff *pa;
7828 int dim;
7830 pa = isl_pw_aff_copy(data->pa);
7831 dim = isl_set_dim(domain, isl_dim_set);
7832 pa = isl_pw_aff_from_range(pa);
7833 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7834 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7835 pa = isl_pw_aff_intersect_domain(pa, domain);
7836 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7838 return data->res ? isl_stat_ok : isl_stat_error;
7841 /* Return a union piecewise affine expression
7842 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7843 * have been aligned.
7845 * Construct an isl_pw_aff on each of the sets in "domain" and
7846 * collect the results.
7848 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7849 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7851 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7852 isl_space *space;
7854 space = isl_union_set_get_space(domain);
7855 data.res = isl_union_pw_aff_empty(space);
7856 data.pa = pa;
7857 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7858 data.res = isl_union_pw_aff_free(data.res);
7859 isl_union_set_free(domain);
7860 isl_pw_aff_free(pa);
7861 return data.res;
7864 /* Return a union piecewise affine expression
7865 * that is equal to "pa" on "domain".
7867 * Check that "pa" is a parametric expression,
7868 * align the parameters if needed and call
7869 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7871 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7872 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7874 isl_bool is_set;
7875 isl_bool equal_params;
7876 isl_space *domain_space, *pa_space;
7878 pa_space = isl_pw_aff_peek_space(pa);
7879 is_set = isl_space_is_set(pa_space);
7880 if (is_set < 0)
7881 goto error;
7882 if (!is_set)
7883 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7884 "expecting parametric expression", goto error);
7886 domain_space = isl_union_set_get_space(domain);
7887 pa_space = isl_pw_aff_get_space(pa);
7888 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7889 if (equal_params >= 0 && !equal_params) {
7890 isl_space *space;
7892 space = isl_space_align_params(domain_space, pa_space);
7893 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7894 domain = isl_union_set_align_params(domain, space);
7895 } else {
7896 isl_space_free(domain_space);
7897 isl_space_free(pa_space);
7900 if (equal_params < 0)
7901 goto error;
7902 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7903 error:
7904 isl_union_set_free(domain);
7905 isl_pw_aff_free(pa);
7906 return NULL;
7909 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7910 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7911 * "res" collects the results.
7913 struct isl_union_pw_aff_val_on_domain_data {
7914 isl_val *v;
7915 isl_union_pw_aff *res;
7918 /* Construct a piecewise affine expression that is equal to data->v
7919 * on "domain" and add the result to data->res.
7921 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7923 struct isl_union_pw_aff_val_on_domain_data *data = user;
7924 isl_pw_aff *pa;
7925 isl_val *v;
7927 v = isl_val_copy(data->v);
7928 pa = isl_pw_aff_val_on_domain(domain, v);
7929 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7931 return data->res ? isl_stat_ok : isl_stat_error;
7934 /* Return a union piecewise affine expression
7935 * that is equal to "v" on "domain".
7937 * Construct an isl_pw_aff on each of the sets in "domain" and
7938 * collect the results.
7940 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7941 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7943 struct isl_union_pw_aff_val_on_domain_data data;
7944 isl_space *space;
7946 space = isl_union_set_get_space(domain);
7947 data.res = isl_union_pw_aff_empty(space);
7948 data.v = v;
7949 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7950 data.res = isl_union_pw_aff_free(data.res);
7951 isl_union_set_free(domain);
7952 isl_val_free(v);
7953 return data.res;
7956 /* Construct a piecewise multi affine expression
7957 * that is equal to "pa" and add it to upma.
7959 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7960 void *user)
7962 isl_union_pw_multi_aff **upma = user;
7963 isl_pw_multi_aff *pma;
7965 pma = isl_pw_multi_aff_from_pw_aff(pa);
7966 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7968 return *upma ? isl_stat_ok : isl_stat_error;
7971 /* Construct and return a union piecewise multi affine expression
7972 * that is equal to the given union piecewise affine expression.
7974 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7975 __isl_take isl_union_pw_aff *upa)
7977 isl_space *space;
7978 isl_union_pw_multi_aff *upma;
7980 if (!upa)
7981 return NULL;
7983 space = isl_union_pw_aff_get_space(upa);
7984 upma = isl_union_pw_multi_aff_empty(space);
7986 if (isl_union_pw_aff_foreach_pw_aff(upa,
7987 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7988 upma = isl_union_pw_multi_aff_free(upma);
7990 isl_union_pw_aff_free(upa);
7991 return upma;
7994 /* Compute the set of elements in the domain of "pa" where it is zero and
7995 * add this set to "uset".
7997 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7999 isl_union_set **uset = (isl_union_set **)user;
8001 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8003 return *uset ? isl_stat_ok : isl_stat_error;
8006 /* Return a union set containing those elements in the domain
8007 * of "upa" where it is zero.
8009 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8010 __isl_take isl_union_pw_aff *upa)
8012 isl_union_set *zero;
8014 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8015 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8016 zero = isl_union_set_free(zero);
8018 isl_union_pw_aff_free(upa);
8019 return zero;
8022 /* Convert "pa" to an isl_map and add it to *umap.
8024 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8026 isl_union_map **umap = user;
8027 isl_map *map;
8029 map = isl_map_from_pw_aff(pa);
8030 *umap = isl_union_map_add_map(*umap, map);
8032 return *umap ? isl_stat_ok : isl_stat_error;
8035 /* Construct a union map mapping the domain of the union
8036 * piecewise affine expression to its range, with the single output dimension
8037 * equated to the corresponding affine expressions on their cells.
8039 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8040 __isl_take isl_union_pw_aff *upa)
8042 isl_space *space;
8043 isl_union_map *umap;
8045 if (!upa)
8046 return NULL;
8048 space = isl_union_pw_aff_get_space(upa);
8049 umap = isl_union_map_empty(space);
8051 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8052 &umap) < 0)
8053 umap = isl_union_map_free(umap);
8055 isl_union_pw_aff_free(upa);
8056 return umap;
8059 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8060 * upma is the function that is plugged in.
8061 * pa is the current part of the function in which upma is plugged in.
8062 * res collects the results.
8064 struct isl_union_pw_aff_pullback_upma_data {
8065 isl_union_pw_multi_aff *upma;
8066 isl_pw_aff *pa;
8067 isl_union_pw_aff *res;
8070 /* Check if "pma" can be plugged into data->pa.
8071 * If so, perform the pullback and add the result to data->res.
8073 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8075 struct isl_union_pw_aff_pullback_upma_data *data = user;
8076 isl_pw_aff *pa;
8078 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8079 pma->dim, isl_dim_out)) {
8080 isl_pw_multi_aff_free(pma);
8081 return isl_stat_ok;
8084 pa = isl_pw_aff_copy(data->pa);
8085 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8087 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8089 return data->res ? isl_stat_ok : isl_stat_error;
8092 /* Check if any of the elements of data->upma can be plugged into pa,
8093 * add if so add the result to data->res.
8095 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8097 struct isl_union_pw_aff_pullback_upma_data *data = user;
8098 isl_stat r;
8100 data->pa = pa;
8101 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8102 &pa_pb_pma, data);
8103 isl_pw_aff_free(pa);
8105 return r;
8108 /* Compute the pullback of "upa" by the function represented by "upma".
8109 * In other words, plug in "upma" in "upa". The result contains
8110 * expressions defined over the domain space of "upma".
8112 * Run over all pairs of elements in "upa" and "upma", perform
8113 * the pullback when appropriate and collect the results.
8114 * If the hash value were based on the domain space rather than
8115 * the function space, then we could run through all elements
8116 * of "upma" and directly pick out the corresponding element of "upa".
8118 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8119 __isl_take isl_union_pw_aff *upa,
8120 __isl_take isl_union_pw_multi_aff *upma)
8122 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8123 isl_space *space;
8125 space = isl_union_pw_multi_aff_get_space(upma);
8126 upa = isl_union_pw_aff_align_params(upa, space);
8127 space = isl_union_pw_aff_get_space(upa);
8128 upma = isl_union_pw_multi_aff_align_params(upma, space);
8130 if (!upa || !upma)
8131 goto error;
8133 data.upma = upma;
8134 data.res = isl_union_pw_aff_alloc_same_size(upa);
8135 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8136 data.res = isl_union_pw_aff_free(data.res);
8138 isl_union_pw_aff_free(upa);
8139 isl_union_pw_multi_aff_free(upma);
8140 return data.res;
8141 error:
8142 isl_union_pw_aff_free(upa);
8143 isl_union_pw_multi_aff_free(upma);
8144 return NULL;
8147 #undef BASE
8148 #define BASE union_pw_aff
8149 #undef DOMBASE
8150 #define DOMBASE union_set
8152 #define NO_MOVE_DIMS
8153 #define NO_DOMAIN
8154 #define NO_PRODUCT
8155 #define NO_SPLICE
8156 #define NO_ZERO
8157 #define NO_IDENTITY
8158 #define NO_GIST
8160 #include <isl_multi_explicit_domain.c>
8161 #include <isl_multi_union_pw_aff_explicit_domain.c>
8162 #include <isl_multi_templ.c>
8163 #include <isl_multi_apply_set.c>
8164 #include <isl_multi_apply_union_set.c>
8165 #include <isl_multi_coalesce.c>
8166 #include <isl_multi_floor.c>
8167 #include <isl_multi_gist.c>
8168 #include <isl_multi_align_set.c>
8169 #include <isl_multi_align_union_set.c>
8170 #include <isl_multi_intersect.c>
8172 /* Does "mupa" have a non-trivial explicit domain?
8174 * The explicit domain, if present, is trivial if it represents
8175 * an (obviously) universe parameter set.
8177 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8178 __isl_keep isl_multi_union_pw_aff *mupa)
8180 isl_bool is_params, trivial;
8181 isl_set *set;
8183 if (!mupa)
8184 return isl_bool_error;
8185 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8186 return isl_bool_false;
8187 is_params = isl_union_set_is_params(mupa->u.dom);
8188 if (is_params < 0 || !is_params)
8189 return isl_bool_not(is_params);
8190 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8191 trivial = isl_set_plain_is_universe(set);
8192 isl_set_free(set);
8193 return isl_bool_not(trivial);
8196 /* Construct a multiple union piecewise affine expression
8197 * in the given space with value zero in each of the output dimensions.
8199 * Since there is no canonical zero value for
8200 * a union piecewise affine expression, we can only construct
8201 * a zero-dimensional "zero" value.
8203 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8204 __isl_take isl_space *space)
8206 isl_bool params;
8208 if (!space)
8209 return NULL;
8211 params = isl_space_is_params(space);
8212 if (params < 0)
8213 goto error;
8214 if (params)
8215 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8216 "expecting proper set space", goto error);
8217 if (!isl_space_is_set(space))
8218 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8219 "expecting set space", goto error);
8220 if (isl_space_dim(space , isl_dim_out) != 0)
8221 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8222 "expecting 0D space", goto error);
8224 return isl_multi_union_pw_aff_alloc(space);
8225 error:
8226 isl_space_free(space);
8227 return NULL;
8230 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8231 * with the actual sum on the shared domain and
8232 * the defined expression on the symmetric difference of the domains.
8234 * We simply iterate over the elements in both arguments and
8235 * call isl_union_pw_aff_union_add on each of them, if there is
8236 * at least one element.
8238 * Otherwise, the two expressions have an explicit domain and
8239 * the union of these explicit domains is computed.
8240 * This assumes that the explicit domains are either both in terms
8241 * of specific domains elements or both in terms of parameters.
8242 * However, if one of the expressions does not have any constraints
8243 * on its explicit domain, then this is allowed as well and the result
8244 * is the expression with no constraints on its explicit domain.
8246 static __isl_give isl_multi_union_pw_aff *
8247 isl_multi_union_pw_aff_union_add_aligned(
8248 __isl_take isl_multi_union_pw_aff *mupa1,
8249 __isl_take isl_multi_union_pw_aff *mupa2)
8251 isl_bool has_domain, is_params1, is_params2;
8253 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8254 goto error;
8255 if (mupa1->n > 0)
8256 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8257 &isl_union_pw_aff_union_add);
8258 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8259 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8260 goto error;
8262 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8263 if (has_domain < 0)
8264 goto error;
8265 if (!has_domain) {
8266 isl_multi_union_pw_aff_free(mupa2);
8267 return mupa1;
8269 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8270 if (has_domain < 0)
8271 goto error;
8272 if (!has_domain) {
8273 isl_multi_union_pw_aff_free(mupa1);
8274 return mupa2;
8277 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8278 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8279 if (is_params1 < 0 || is_params2 < 0)
8280 goto error;
8281 if (is_params1 != is_params2)
8282 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8283 isl_error_invalid,
8284 "cannot compute union of concrete domain and "
8285 "parameter constraints", goto error);
8286 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8287 if (!mupa1)
8288 goto error;
8289 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8290 isl_union_set_copy(mupa2->u.dom));
8291 if (!mupa1->u.dom)
8292 goto error;
8293 isl_multi_union_pw_aff_free(mupa2);
8294 return mupa1;
8295 error:
8296 isl_multi_union_pw_aff_free(mupa1);
8297 isl_multi_union_pw_aff_free(mupa2);
8298 return NULL;
8301 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8302 * with the actual sum on the shared domain and
8303 * the defined expression on the symmetric difference of the domains.
8305 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8306 __isl_take isl_multi_union_pw_aff *mupa1,
8307 __isl_take isl_multi_union_pw_aff *mupa2)
8309 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8310 &isl_multi_union_pw_aff_union_add_aligned);
8313 /* Construct and return a multi union piecewise affine expression
8314 * that is equal to the given multi affine expression.
8316 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8317 __isl_take isl_multi_aff *ma)
8319 isl_multi_pw_aff *mpa;
8321 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8322 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8325 /* Construct and return a multi union piecewise affine expression
8326 * that is equal to the given multi piecewise affine expression.
8328 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8329 __isl_take isl_multi_pw_aff *mpa)
8331 int i, n;
8332 isl_space *space;
8333 isl_multi_union_pw_aff *mupa;
8335 if (!mpa)
8336 return NULL;
8338 space = isl_multi_pw_aff_get_space(mpa);
8339 space = isl_space_range(space);
8340 mupa = isl_multi_union_pw_aff_alloc(space);
8342 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8343 for (i = 0; i < n; ++i) {
8344 isl_pw_aff *pa;
8345 isl_union_pw_aff *upa;
8347 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8348 upa = isl_union_pw_aff_from_pw_aff(pa);
8349 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8352 isl_multi_pw_aff_free(mpa);
8354 return mupa;
8357 /* Extract the range space of "pma" and assign it to *space.
8358 * If *space has already been set (through a previous call to this function),
8359 * then check that the range space is the same.
8361 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8363 isl_space **space = user;
8364 isl_space *pma_space;
8365 isl_bool equal;
8367 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8368 isl_pw_multi_aff_free(pma);
8370 if (!pma_space)
8371 return isl_stat_error;
8372 if (!*space) {
8373 *space = pma_space;
8374 return isl_stat_ok;
8377 equal = isl_space_is_equal(pma_space, *space);
8378 isl_space_free(pma_space);
8380 if (equal < 0)
8381 return isl_stat_error;
8382 if (!equal)
8383 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8384 "range spaces not the same", return isl_stat_error);
8385 return isl_stat_ok;
8388 /* Construct and return a multi union piecewise affine expression
8389 * that is equal to the given union piecewise multi affine expression.
8391 * In order to be able to perform the conversion, the input
8392 * needs to be non-empty and may only involve a single range space.
8394 * If the resulting multi union piecewise affine expression has
8395 * an explicit domain, then assign it the domain of the input.
8396 * In other cases, the domain is stored in the individual elements.
8398 __isl_give isl_multi_union_pw_aff *
8399 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8400 __isl_take isl_union_pw_multi_aff *upma)
8402 isl_space *space = NULL;
8403 isl_multi_union_pw_aff *mupa;
8404 int i, n;
8406 if (!upma)
8407 return NULL;
8408 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8409 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8410 "cannot extract range space from empty input",
8411 goto error);
8412 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8413 &space) < 0)
8414 goto error;
8416 if (!space)
8417 goto error;
8419 n = isl_space_dim(space, isl_dim_set);
8420 mupa = isl_multi_union_pw_aff_alloc(space);
8422 for (i = 0; i < n; ++i) {
8423 isl_union_pw_aff *upa;
8425 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8426 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8428 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8429 isl_union_set *dom;
8430 isl_union_pw_multi_aff *copy;
8432 copy = isl_union_pw_multi_aff_copy(upma);
8433 dom = isl_union_pw_multi_aff_domain(copy);
8434 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8437 isl_union_pw_multi_aff_free(upma);
8438 return mupa;
8439 error:
8440 isl_space_free(space);
8441 isl_union_pw_multi_aff_free(upma);
8442 return NULL;
8445 /* Try and create an isl_multi_union_pw_aff that is equivalent
8446 * to the given isl_union_map.
8447 * The isl_union_map is required to be single-valued in each space.
8448 * Moreover, it cannot be empty and all range spaces need to be the same.
8449 * Otherwise, an error is produced.
8451 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8452 __isl_take isl_union_map *umap)
8454 isl_union_pw_multi_aff *upma;
8456 upma = isl_union_pw_multi_aff_from_union_map(umap);
8457 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8460 /* Return a multiple union piecewise affine expression
8461 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8462 * have been aligned.
8464 static __isl_give isl_multi_union_pw_aff *
8465 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8466 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8468 int i, n;
8469 isl_space *space;
8470 isl_multi_union_pw_aff *mupa;
8472 if (!domain || !mv)
8473 goto error;
8475 n = isl_multi_val_dim(mv, isl_dim_set);
8476 space = isl_multi_val_get_space(mv);
8477 mupa = isl_multi_union_pw_aff_alloc(space);
8478 for (i = 0; i < n; ++i) {
8479 isl_val *v;
8480 isl_union_pw_aff *upa;
8482 v = isl_multi_val_get_val(mv, i);
8483 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8485 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8488 isl_union_set_free(domain);
8489 isl_multi_val_free(mv);
8490 return mupa;
8491 error:
8492 isl_union_set_free(domain);
8493 isl_multi_val_free(mv);
8494 return NULL;
8497 /* Return a multiple union piecewise affine expression
8498 * that is equal to "mv" on "domain".
8500 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8501 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8503 isl_bool equal_params;
8505 if (!domain || !mv)
8506 goto error;
8507 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8508 if (equal_params < 0)
8509 goto error;
8510 if (equal_params)
8511 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8512 domain, mv);
8513 domain = isl_union_set_align_params(domain,
8514 isl_multi_val_get_space(mv));
8515 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8516 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8517 error:
8518 isl_union_set_free(domain);
8519 isl_multi_val_free(mv);
8520 return NULL;
8523 /* Return a multiple union piecewise affine expression
8524 * that is equal to "ma" on "domain".
8526 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8527 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8529 isl_pw_multi_aff *pma;
8531 pma = isl_pw_multi_aff_from_multi_aff(ma);
8532 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8535 /* Return a multiple union piecewise affine expression
8536 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8537 * have been aligned.
8539 * If the resulting multi union piecewise affine expression has
8540 * an explicit domain, then assign it the input domain.
8541 * In other cases, the domain is stored in the individual elements.
8543 static __isl_give isl_multi_union_pw_aff *
8544 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8545 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8547 int i, n;
8548 isl_space *space;
8549 isl_multi_union_pw_aff *mupa;
8551 if (!domain || !pma)
8552 goto error;
8554 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8555 space = isl_pw_multi_aff_get_space(pma);
8556 mupa = isl_multi_union_pw_aff_alloc(space);
8557 for (i = 0; i < n; ++i) {
8558 isl_pw_aff *pa;
8559 isl_union_pw_aff *upa;
8561 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8562 upa = isl_union_pw_aff_pw_aff_on_domain(
8563 isl_union_set_copy(domain), pa);
8564 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8566 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8567 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8568 isl_union_set_copy(domain));
8570 isl_union_set_free(domain);
8571 isl_pw_multi_aff_free(pma);
8572 return mupa;
8573 error:
8574 isl_union_set_free(domain);
8575 isl_pw_multi_aff_free(pma);
8576 return NULL;
8579 /* Return a multiple union piecewise affine expression
8580 * that is equal to "pma" on "domain".
8582 __isl_give isl_multi_union_pw_aff *
8583 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8584 __isl_take isl_pw_multi_aff *pma)
8586 isl_bool equal_params;
8587 isl_space *space;
8589 space = isl_pw_multi_aff_peek_space(pma);
8590 equal_params = isl_union_set_space_has_equal_params(domain, space);
8591 if (equal_params < 0)
8592 goto error;
8593 if (equal_params)
8594 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8595 domain, pma);
8596 domain = isl_union_set_align_params(domain,
8597 isl_pw_multi_aff_get_space(pma));
8598 pma = isl_pw_multi_aff_align_params(pma,
8599 isl_union_set_get_space(domain));
8600 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8601 pma);
8602 error:
8603 isl_union_set_free(domain);
8604 isl_pw_multi_aff_free(pma);
8605 return NULL;
8608 /* Return a union set containing those elements in the domains
8609 * of the elements of "mupa" where they are all zero.
8611 * If there are no elements, then simply return the entire domain.
8613 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8614 __isl_take isl_multi_union_pw_aff *mupa)
8616 int i, n;
8617 isl_union_pw_aff *upa;
8618 isl_union_set *zero;
8620 if (!mupa)
8621 return NULL;
8623 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8624 if (n == 0)
8625 return isl_multi_union_pw_aff_domain(mupa);
8627 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8628 zero = isl_union_pw_aff_zero_union_set(upa);
8630 for (i = 1; i < n; ++i) {
8631 isl_union_set *zero_i;
8633 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8634 zero_i = isl_union_pw_aff_zero_union_set(upa);
8636 zero = isl_union_set_intersect(zero, zero_i);
8639 isl_multi_union_pw_aff_free(mupa);
8640 return zero;
8643 /* Construct a union map mapping the shared domain
8644 * of the union piecewise affine expressions to the range of "mupa"
8645 * in the special case of a 0D multi union piecewise affine expression.
8647 * Construct a map between the explicit domain of "mupa" and
8648 * the range space.
8649 * Note that this assumes that the domain consists of explicit elements.
8651 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8652 __isl_take isl_multi_union_pw_aff *mupa)
8654 isl_bool is_params;
8655 isl_space *space;
8656 isl_union_set *dom, *ran;
8658 space = isl_multi_union_pw_aff_get_space(mupa);
8659 dom = isl_multi_union_pw_aff_domain(mupa);
8660 ran = isl_union_set_from_set(isl_set_universe(space));
8662 is_params = isl_union_set_is_params(dom);
8663 if (is_params < 0)
8664 dom = isl_union_set_free(dom);
8665 else if (is_params)
8666 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8667 "cannot create union map from expression without "
8668 "explicit domain elements",
8669 dom = isl_union_set_free(dom));
8671 return isl_union_map_from_domain_and_range(dom, ran);
8674 /* Construct a union map mapping the shared domain
8675 * of the union piecewise affine expressions to the range of "mupa"
8676 * with each dimension in the range equated to the
8677 * corresponding union piecewise affine expression.
8679 * If the input is zero-dimensional, then construct a mapping
8680 * from its explicit domain.
8682 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8683 __isl_take isl_multi_union_pw_aff *mupa)
8685 int i, n;
8686 isl_space *space;
8687 isl_union_map *umap;
8688 isl_union_pw_aff *upa;
8690 if (!mupa)
8691 return NULL;
8693 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8694 if (n == 0)
8695 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8697 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8698 umap = isl_union_map_from_union_pw_aff(upa);
8700 for (i = 1; i < n; ++i) {
8701 isl_union_map *umap_i;
8703 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8704 umap_i = isl_union_map_from_union_pw_aff(upa);
8705 umap = isl_union_map_flat_range_product(umap, umap_i);
8708 space = isl_multi_union_pw_aff_get_space(mupa);
8709 umap = isl_union_map_reset_range_space(umap, space);
8711 isl_multi_union_pw_aff_free(mupa);
8712 return umap;
8715 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8716 * "range" is the space from which to set the range space.
8717 * "res" collects the results.
8719 struct isl_union_pw_multi_aff_reset_range_space_data {
8720 isl_space *range;
8721 isl_union_pw_multi_aff *res;
8724 /* Replace the range space of "pma" by the range space of data->range and
8725 * add the result to data->res.
8727 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8729 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8730 isl_space *space;
8732 space = isl_pw_multi_aff_get_space(pma);
8733 space = isl_space_domain(space);
8734 space = isl_space_extend_domain_with_range(space,
8735 isl_space_copy(data->range));
8736 pma = isl_pw_multi_aff_reset_space(pma, space);
8737 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8739 return data->res ? isl_stat_ok : isl_stat_error;
8742 /* Replace the range space of all the piecewise affine expressions in "upma" by
8743 * the range space of "space".
8745 * This assumes that all these expressions have the same output dimension.
8747 * Since the spaces of the expressions change, so do their hash values.
8748 * We therefore need to create a new isl_union_pw_multi_aff.
8749 * Note that the hash value is currently computed based on the entire
8750 * space even though there can only be a single expression with a given
8751 * domain space.
8753 static __isl_give isl_union_pw_multi_aff *
8754 isl_union_pw_multi_aff_reset_range_space(
8755 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8757 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8758 isl_space *space_upma;
8760 space_upma = isl_union_pw_multi_aff_get_space(upma);
8761 data.res = isl_union_pw_multi_aff_empty(space_upma);
8762 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8763 &reset_range_space, &data) < 0)
8764 data.res = isl_union_pw_multi_aff_free(data.res);
8766 isl_space_free(space);
8767 isl_union_pw_multi_aff_free(upma);
8768 return data.res;
8771 /* Construct and return a union piecewise multi affine expression
8772 * that is equal to the given multi union piecewise affine expression,
8773 * in the special case of a 0D multi union piecewise affine expression.
8775 * Construct a union piecewise multi affine expression
8776 * on top of the explicit domain of the input.
8778 __isl_give isl_union_pw_multi_aff *
8779 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8780 __isl_take isl_multi_union_pw_aff *mupa)
8782 isl_space *space;
8783 isl_multi_val *mv;
8784 isl_union_set *domain;
8786 space = isl_multi_union_pw_aff_get_space(mupa);
8787 mv = isl_multi_val_zero(space);
8788 domain = isl_multi_union_pw_aff_domain(mupa);
8789 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8792 /* Construct and return a union piecewise multi affine expression
8793 * that is equal to the given multi union piecewise affine expression.
8795 * If the input is zero-dimensional, then
8796 * construct a union piecewise multi affine expression
8797 * on top of the explicit domain of the input.
8799 __isl_give isl_union_pw_multi_aff *
8800 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8801 __isl_take isl_multi_union_pw_aff *mupa)
8803 int i, n;
8804 isl_space *space;
8805 isl_union_pw_multi_aff *upma;
8806 isl_union_pw_aff *upa;
8808 if (!mupa)
8809 return NULL;
8811 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8812 if (n == 0)
8813 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8815 space = isl_multi_union_pw_aff_get_space(mupa);
8816 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8817 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8819 for (i = 1; i < n; ++i) {
8820 isl_union_pw_multi_aff *upma_i;
8822 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8823 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8824 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8827 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8829 isl_multi_union_pw_aff_free(mupa);
8830 return upma;
8833 /* Intersect the range of "mupa" with "range",
8834 * in the special case where "mupa" is 0D.
8836 * Intersect the domain of "mupa" with the constraints on the parameters
8837 * of "range".
8839 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8840 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8842 range = isl_set_params(range);
8843 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8844 return mupa;
8847 /* Intersect the range of "mupa" with "range".
8848 * That is, keep only those domain elements that have a function value
8849 * in "range".
8851 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8852 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8854 isl_union_pw_multi_aff *upma;
8855 isl_union_set *domain;
8856 isl_space *space;
8857 int n;
8858 int match;
8860 if (!mupa || !range)
8861 goto error;
8863 space = isl_set_get_space(range);
8864 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8865 space, isl_dim_set);
8866 isl_space_free(space);
8867 if (match < 0)
8868 goto error;
8869 if (!match)
8870 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8871 "space don't match", goto error);
8872 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8873 if (n == 0)
8874 return mupa_intersect_range_0D(mupa, range);
8876 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8877 isl_multi_union_pw_aff_copy(mupa));
8878 domain = isl_union_set_from_set(range);
8879 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8880 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8882 return mupa;
8883 error:
8884 isl_multi_union_pw_aff_free(mupa);
8885 isl_set_free(range);
8886 return NULL;
8889 /* Return the shared domain of the elements of "mupa",
8890 * in the special case where "mupa" is zero-dimensional.
8892 * Return the explicit domain of "mupa".
8893 * Note that this domain may be a parameter set, either
8894 * because "mupa" is meant to live in a set space or
8895 * because no explicit domain has been set.
8897 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8898 __isl_take isl_multi_union_pw_aff *mupa)
8900 isl_union_set *dom;
8902 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8903 isl_multi_union_pw_aff_free(mupa);
8905 return dom;
8908 /* Return the shared domain of the elements of "mupa".
8910 * If "mupa" is zero-dimensional, then return its explicit domain.
8912 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8913 __isl_take isl_multi_union_pw_aff *mupa)
8915 int i, n;
8916 isl_union_pw_aff *upa;
8917 isl_union_set *dom;
8919 if (!mupa)
8920 return NULL;
8922 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8923 if (n == 0)
8924 return isl_multi_union_pw_aff_domain_0D(mupa);
8926 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8927 dom = isl_union_pw_aff_domain(upa);
8928 for (i = 1; i < n; ++i) {
8929 isl_union_set *dom_i;
8931 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8932 dom_i = isl_union_pw_aff_domain(upa);
8933 dom = isl_union_set_intersect(dom, dom_i);
8936 isl_multi_union_pw_aff_free(mupa);
8937 return dom;
8940 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8941 * In particular, the spaces have been aligned.
8942 * The result is defined over the shared domain of the elements of "mupa"
8944 * We first extract the parametric constant part of "aff" and
8945 * define that over the shared domain.
8946 * Then we iterate over all input dimensions of "aff" and add the corresponding
8947 * multiples of the elements of "mupa".
8948 * Finally, we consider the integer divisions, calling the function
8949 * recursively to obtain an isl_union_pw_aff corresponding to the
8950 * integer division argument.
8952 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8953 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8955 int i, n_in, n_div;
8956 isl_union_pw_aff *upa;
8957 isl_union_set *uset;
8958 isl_val *v;
8959 isl_aff *cst;
8961 n_in = isl_aff_dim(aff, isl_dim_in);
8962 n_div = isl_aff_dim(aff, isl_dim_div);
8964 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8965 cst = isl_aff_copy(aff);
8966 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8967 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8968 cst = isl_aff_project_domain_on_params(cst);
8969 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8971 for (i = 0; i < n_in; ++i) {
8972 isl_union_pw_aff *upa_i;
8974 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8975 continue;
8976 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8977 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8978 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8979 upa = isl_union_pw_aff_add(upa, upa_i);
8982 for (i = 0; i < n_div; ++i) {
8983 isl_aff *div;
8984 isl_union_pw_aff *upa_i;
8986 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8987 continue;
8988 div = isl_aff_get_div(aff, i);
8989 upa_i = multi_union_pw_aff_apply_aff(
8990 isl_multi_union_pw_aff_copy(mupa), div);
8991 upa_i = isl_union_pw_aff_floor(upa_i);
8992 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8993 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8994 upa = isl_union_pw_aff_add(upa, upa_i);
8997 isl_multi_union_pw_aff_free(mupa);
8998 isl_aff_free(aff);
9000 return upa;
9003 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9004 * with the domain of "aff".
9005 * Furthermore, the dimension of this space needs to be greater than zero.
9006 * The result is defined over the shared domain of the elements of "mupa"
9008 * We perform these checks and then hand over control to
9009 * multi_union_pw_aff_apply_aff.
9011 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9012 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9014 isl_space *space1, *space2;
9015 int equal;
9017 mupa = isl_multi_union_pw_aff_align_params(mupa,
9018 isl_aff_get_space(aff));
9019 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9020 if (!mupa || !aff)
9021 goto error;
9023 space1 = isl_multi_union_pw_aff_get_space(mupa);
9024 space2 = isl_aff_get_domain_space(aff);
9025 equal = isl_space_is_equal(space1, space2);
9026 isl_space_free(space1);
9027 isl_space_free(space2);
9028 if (equal < 0)
9029 goto error;
9030 if (!equal)
9031 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9032 "spaces don't match", goto error);
9033 if (isl_aff_dim(aff, isl_dim_in) == 0)
9034 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9035 "cannot determine domains", goto error);
9037 return multi_union_pw_aff_apply_aff(mupa, aff);
9038 error:
9039 isl_multi_union_pw_aff_free(mupa);
9040 isl_aff_free(aff);
9041 return NULL;
9044 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9045 * The space of "mupa" is known to be compatible with the domain of "ma".
9047 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9048 * on the domain of "mupa".
9050 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9051 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9053 isl_union_set *dom;
9055 dom = isl_multi_union_pw_aff_domain(mupa);
9056 ma = isl_multi_aff_project_domain_on_params(ma);
9058 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9061 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9062 * with the domain of "ma".
9063 * The result is defined over the shared domain of the elements of "mupa"
9065 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9066 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9068 isl_space *space1, *space2;
9069 isl_multi_union_pw_aff *res;
9070 int equal;
9071 int i, n_out;
9073 mupa = isl_multi_union_pw_aff_align_params(mupa,
9074 isl_multi_aff_get_space(ma));
9075 ma = isl_multi_aff_align_params(ma,
9076 isl_multi_union_pw_aff_get_space(mupa));
9077 if (!mupa || !ma)
9078 goto error;
9080 space1 = isl_multi_union_pw_aff_get_space(mupa);
9081 space2 = isl_multi_aff_get_domain_space(ma);
9082 equal = isl_space_is_equal(space1, space2);
9083 isl_space_free(space1);
9084 isl_space_free(space2);
9085 if (equal < 0)
9086 goto error;
9087 if (!equal)
9088 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9089 "spaces don't match", goto error);
9090 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9091 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9092 return mupa_apply_multi_aff_0D(mupa, ma);
9094 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9095 res = isl_multi_union_pw_aff_alloc(space1);
9097 for (i = 0; i < n_out; ++i) {
9098 isl_aff *aff;
9099 isl_union_pw_aff *upa;
9101 aff = isl_multi_aff_get_aff(ma, i);
9102 upa = multi_union_pw_aff_apply_aff(
9103 isl_multi_union_pw_aff_copy(mupa), aff);
9104 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9107 isl_multi_aff_free(ma);
9108 isl_multi_union_pw_aff_free(mupa);
9109 return res;
9110 error:
9111 isl_multi_union_pw_aff_free(mupa);
9112 isl_multi_aff_free(ma);
9113 return NULL;
9116 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9117 * The space of "mupa" is known to be compatible with the domain of "pa".
9119 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9120 * on the domain of "mupa".
9122 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9123 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9125 isl_union_set *dom;
9127 dom = isl_multi_union_pw_aff_domain(mupa);
9128 pa = isl_pw_aff_project_domain_on_params(pa);
9130 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9133 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9134 * with the domain of "pa".
9135 * Furthermore, the dimension of this space needs to be greater than zero.
9136 * The result is defined over the shared domain of the elements of "mupa"
9138 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9139 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9141 int i;
9142 int equal;
9143 isl_space *space, *space2;
9144 isl_union_pw_aff *upa;
9146 mupa = isl_multi_union_pw_aff_align_params(mupa,
9147 isl_pw_aff_get_space(pa));
9148 pa = isl_pw_aff_align_params(pa,
9149 isl_multi_union_pw_aff_get_space(mupa));
9150 if (!mupa || !pa)
9151 goto error;
9153 space = isl_multi_union_pw_aff_get_space(mupa);
9154 space2 = isl_pw_aff_get_domain_space(pa);
9155 equal = isl_space_is_equal(space, space2);
9156 isl_space_free(space);
9157 isl_space_free(space2);
9158 if (equal < 0)
9159 goto error;
9160 if (!equal)
9161 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9162 "spaces don't match", goto error);
9163 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9164 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9166 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9167 upa = isl_union_pw_aff_empty(space);
9169 for (i = 0; i < pa->n; ++i) {
9170 isl_aff *aff;
9171 isl_set *domain;
9172 isl_multi_union_pw_aff *mupa_i;
9173 isl_union_pw_aff *upa_i;
9175 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9176 domain = isl_set_copy(pa->p[i].set);
9177 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9178 aff = isl_aff_copy(pa->p[i].aff);
9179 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9180 upa = isl_union_pw_aff_union_add(upa, upa_i);
9183 isl_multi_union_pw_aff_free(mupa);
9184 isl_pw_aff_free(pa);
9185 return upa;
9186 error:
9187 isl_multi_union_pw_aff_free(mupa);
9188 isl_pw_aff_free(pa);
9189 return NULL;
9192 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9193 * The space of "mupa" is known to be compatible with the domain of "pma".
9195 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9196 * on the domain of "mupa".
9198 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9199 __isl_take isl_multi_union_pw_aff *mupa,
9200 __isl_take isl_pw_multi_aff *pma)
9202 isl_union_set *dom;
9204 dom = isl_multi_union_pw_aff_domain(mupa);
9205 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9207 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9210 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9211 * with the domain of "pma".
9212 * The result is defined over the shared domain of the elements of "mupa"
9214 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9215 __isl_take isl_multi_union_pw_aff *mupa,
9216 __isl_take isl_pw_multi_aff *pma)
9218 isl_space *space1, *space2;
9219 isl_multi_union_pw_aff *res;
9220 int equal;
9221 int i, n_out;
9223 mupa = isl_multi_union_pw_aff_align_params(mupa,
9224 isl_pw_multi_aff_get_space(pma));
9225 pma = isl_pw_multi_aff_align_params(pma,
9226 isl_multi_union_pw_aff_get_space(mupa));
9227 if (!mupa || !pma)
9228 goto error;
9230 space1 = isl_multi_union_pw_aff_get_space(mupa);
9231 space2 = isl_pw_multi_aff_get_domain_space(pma);
9232 equal = isl_space_is_equal(space1, space2);
9233 isl_space_free(space1);
9234 isl_space_free(space2);
9235 if (equal < 0)
9236 goto error;
9237 if (!equal)
9238 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9239 "spaces don't match", goto error);
9240 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9241 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9242 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9244 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9245 res = isl_multi_union_pw_aff_alloc(space1);
9247 for (i = 0; i < n_out; ++i) {
9248 isl_pw_aff *pa;
9249 isl_union_pw_aff *upa;
9251 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9252 upa = isl_multi_union_pw_aff_apply_pw_aff(
9253 isl_multi_union_pw_aff_copy(mupa), pa);
9254 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9257 isl_pw_multi_aff_free(pma);
9258 isl_multi_union_pw_aff_free(mupa);
9259 return res;
9260 error:
9261 isl_multi_union_pw_aff_free(mupa);
9262 isl_pw_multi_aff_free(pma);
9263 return NULL;
9266 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9267 * If the explicit domain only keeps track of constraints on the parameters,
9268 * then only update those constraints.
9270 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9271 __isl_take isl_multi_union_pw_aff *mupa,
9272 __isl_keep isl_union_pw_multi_aff *upma)
9274 isl_bool is_params;
9276 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9277 return isl_multi_union_pw_aff_free(mupa);
9279 mupa = isl_multi_union_pw_aff_cow(mupa);
9280 if (!mupa)
9281 return NULL;
9283 is_params = isl_union_set_is_params(mupa->u.dom);
9284 if (is_params < 0)
9285 return isl_multi_union_pw_aff_free(mupa);
9287 upma = isl_union_pw_multi_aff_copy(upma);
9288 if (is_params)
9289 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9290 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9291 else
9292 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9293 mupa->u.dom, upma);
9294 if (!mupa->u.dom)
9295 return isl_multi_union_pw_aff_free(mupa);
9296 return mupa;
9299 /* Compute the pullback of "mupa" by the function represented by "upma".
9300 * In other words, plug in "upma" in "mupa". The result contains
9301 * expressions defined over the domain space of "upma".
9303 * Run over all elements of "mupa" and plug in "upma" in each of them.
9305 * If "mupa" has an explicit domain, then it is this domain
9306 * that needs to undergo a pullback instead, i.e., a preimage.
9308 __isl_give isl_multi_union_pw_aff *
9309 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9310 __isl_take isl_multi_union_pw_aff *mupa,
9311 __isl_take isl_union_pw_multi_aff *upma)
9313 int i, n;
9315 mupa = isl_multi_union_pw_aff_align_params(mupa,
9316 isl_union_pw_multi_aff_get_space(upma));
9317 upma = isl_union_pw_multi_aff_align_params(upma,
9318 isl_multi_union_pw_aff_get_space(mupa));
9319 mupa = isl_multi_union_pw_aff_cow(mupa);
9320 if (!mupa || !upma)
9321 goto error;
9323 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9324 for (i = 0; i < n; ++i) {
9325 isl_union_pw_aff *upa;
9327 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9328 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9329 isl_union_pw_multi_aff_copy(upma));
9330 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9333 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9334 mupa = preimage_explicit_domain(mupa, upma);
9336 isl_union_pw_multi_aff_free(upma);
9337 return mupa;
9338 error:
9339 isl_multi_union_pw_aff_free(mupa);
9340 isl_union_pw_multi_aff_free(upma);
9341 return NULL;
9344 /* Extract the sequence of elements in "mupa" with domain space "space"
9345 * (ignoring parameters).
9347 * For the elements of "mupa" that are not defined on the specified space,
9348 * the corresponding element in the result is empty.
9350 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9351 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9353 int i, n;
9354 isl_bool equal_params;
9355 isl_space *space_mpa = NULL;
9356 isl_multi_pw_aff *mpa;
9358 if (!mupa || !space)
9359 goto error;
9361 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9362 equal_params = isl_space_has_equal_params(space_mpa, space);
9363 if (equal_params < 0)
9364 goto error;
9365 if (!equal_params) {
9366 space = isl_space_drop_dims(space, isl_dim_param,
9367 0, isl_space_dim(space, isl_dim_param));
9368 space = isl_space_align_params(space,
9369 isl_space_copy(space_mpa));
9370 if (!space)
9371 goto error;
9373 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9374 space_mpa);
9375 mpa = isl_multi_pw_aff_alloc(space_mpa);
9377 space = isl_space_from_domain(space);
9378 space = isl_space_add_dims(space, isl_dim_out, 1);
9379 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9380 for (i = 0; i < n; ++i) {
9381 isl_union_pw_aff *upa;
9382 isl_pw_aff *pa;
9384 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9385 pa = isl_union_pw_aff_extract_pw_aff(upa,
9386 isl_space_copy(space));
9387 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9388 isl_union_pw_aff_free(upa);
9391 isl_space_free(space);
9392 return mpa;
9393 error:
9394 isl_space_free(space_mpa);
9395 isl_space_free(space);
9396 return NULL;
9399 /* Evaluate the affine function "aff" in the void point "pnt".
9400 * In particular, return the value NaN.
9402 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9403 __isl_take isl_point *pnt)
9405 isl_ctx *ctx;
9407 ctx = isl_point_get_ctx(pnt);
9408 isl_aff_free(aff);
9409 isl_point_free(pnt);
9410 return isl_val_nan(ctx);
9413 /* Evaluate the affine expression "aff"
9414 * in the coordinates (with denominator) "pnt".
9416 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9417 __isl_keep isl_vec *pnt)
9419 isl_int n, d;
9420 isl_ctx *ctx;
9421 isl_val *v;
9423 if (!aff || !pnt)
9424 return NULL;
9426 ctx = isl_vec_get_ctx(aff);
9427 isl_int_init(n);
9428 isl_int_init(d);
9429 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9430 isl_int_mul(d, aff->el[0], pnt->el[0]);
9431 v = isl_val_rat_from_isl_int(ctx, n, d);
9432 v = isl_val_normalize(v);
9433 isl_int_clear(n);
9434 isl_int_clear(d);
9436 return v;
9439 /* Check that the domain space of "aff" is equal to "space".
9441 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9442 __isl_keep isl_space *space)
9444 isl_bool ok;
9446 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9447 if (ok < 0)
9448 return isl_stat_error;
9449 if (!ok)
9450 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9451 "incompatible spaces", return isl_stat_error);
9452 return isl_stat_ok;
9455 /* Evaluate the affine function "aff" in "pnt".
9457 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9458 __isl_take isl_point *pnt)
9460 isl_bool is_void;
9461 isl_val *v;
9462 isl_local_space *ls;
9464 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9465 goto error;
9466 is_void = isl_point_is_void(pnt);
9467 if (is_void < 0)
9468 goto error;
9469 if (is_void)
9470 return eval_void(aff, pnt);
9472 ls = isl_aff_get_domain_local_space(aff);
9473 pnt = isl_local_space_lift_point(ls, pnt);
9475 v = eval(aff->v, isl_point_peek_vec(pnt));
9477 isl_aff_free(aff);
9478 isl_point_free(pnt);
9480 return v;
9481 error:
9482 isl_aff_free(aff);
9483 isl_point_free(pnt);
9484 return NULL;