isl_pw_templ.c: extract out isl_pw_eval.c
[isl.git] / isl_aff.c
blob56ca1d366b1068a3e54afa2625e992ef0dd750e2
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_OPT
2643 #define NO_LIFT
2644 #define NO_MORPH
2646 #include <isl_pw_templ.c>
2647 #include <isl_pw_hash.c>
2648 #include <isl_pw_union_opt.c>
2650 #undef UNION
2651 #define UNION isl_union_pw_aff
2652 #undef PART
2653 #define PART isl_pw_aff
2654 #undef PARTS
2655 #define PARTS pw_aff
2657 #include <isl_union_single.c>
2658 #include <isl_union_neg.c>
2660 static __isl_give isl_set *align_params_pw_pw_set_and(
2661 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2662 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2663 __isl_take isl_pw_aff *pwaff2))
2665 isl_bool equal_params;
2667 if (!pwaff1 || !pwaff2)
2668 goto error;
2669 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2670 if (equal_params < 0)
2671 goto error;
2672 if (equal_params)
2673 return fn(pwaff1, pwaff2);
2674 if (!isl_space_has_named_params(pwaff1->dim) ||
2675 !isl_space_has_named_params(pwaff2->dim))
2676 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2677 "unaligned unnamed parameters", goto error);
2678 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2679 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2680 return fn(pwaff1, pwaff2);
2681 error:
2682 isl_pw_aff_free(pwaff1);
2683 isl_pw_aff_free(pwaff2);
2684 return NULL;
2687 /* Align the parameters of the to isl_pw_aff arguments and
2688 * then apply a function "fn" on them that returns an isl_map.
2690 static __isl_give isl_map *align_params_pw_pw_map_and(
2691 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2692 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2693 __isl_take isl_pw_aff *pa2))
2695 isl_bool equal_params;
2697 if (!pa1 || !pa2)
2698 goto error;
2699 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2700 if (equal_params < 0)
2701 goto error;
2702 if (equal_params)
2703 return fn(pa1, pa2);
2704 if (!isl_space_has_named_params(pa1->dim) ||
2705 !isl_space_has_named_params(pa2->dim))
2706 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2707 "unaligned unnamed parameters", goto error);
2708 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2709 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2710 return fn(pa1, pa2);
2711 error:
2712 isl_pw_aff_free(pa1);
2713 isl_pw_aff_free(pa2);
2714 return NULL;
2717 /* Compute a piecewise quasi-affine expression with a domain that
2718 * is the union of those of pwaff1 and pwaff2 and such that on each
2719 * cell, the quasi-affine expression is the maximum of those of pwaff1
2720 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2721 * cell, then the associated expression is the defined one.
2723 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2724 __isl_take isl_pw_aff *pwaff2)
2726 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2729 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2730 __isl_take isl_pw_aff *pwaff2)
2732 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2733 &pw_aff_union_max);
2736 /* Compute a piecewise quasi-affine expression with a domain that
2737 * is the union of those of pwaff1 and pwaff2 and such that on each
2738 * cell, the quasi-affine expression is the minimum of those of pwaff1
2739 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2740 * cell, then the associated expression is the defined one.
2742 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2743 __isl_take isl_pw_aff *pwaff2)
2745 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2748 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2749 __isl_take isl_pw_aff *pwaff2)
2751 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2752 &pw_aff_union_min);
2755 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2756 __isl_take isl_pw_aff *pwaff2, int max)
2758 if (max)
2759 return isl_pw_aff_union_max(pwaff1, pwaff2);
2760 else
2761 return isl_pw_aff_union_min(pwaff1, pwaff2);
2764 /* Construct a map with as domain the domain of pwaff and
2765 * one-dimensional range corresponding to the affine expressions.
2767 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2769 int i;
2770 isl_space *dim;
2771 isl_map *map;
2773 if (!pwaff)
2774 return NULL;
2776 dim = isl_pw_aff_get_space(pwaff);
2777 map = isl_map_empty(dim);
2779 for (i = 0; i < pwaff->n; ++i) {
2780 isl_basic_map *bmap;
2781 isl_map *map_i;
2783 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2784 map_i = isl_map_from_basic_map(bmap);
2785 map_i = isl_map_intersect_domain(map_i,
2786 isl_set_copy(pwaff->p[i].set));
2787 map = isl_map_union_disjoint(map, map_i);
2790 isl_pw_aff_free(pwaff);
2792 return map;
2795 /* Construct a map with as domain the domain of pwaff and
2796 * one-dimensional range corresponding to the affine expressions.
2798 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2800 if (!pwaff)
2801 return NULL;
2802 if (isl_space_is_set(pwaff->dim))
2803 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2804 "space of input is not a map", goto error);
2805 return map_from_pw_aff(pwaff);
2806 error:
2807 isl_pw_aff_free(pwaff);
2808 return NULL;
2811 /* Construct a one-dimensional set with as parameter domain
2812 * the domain of pwaff and the single set dimension
2813 * corresponding to the affine expressions.
2815 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2817 if (!pwaff)
2818 return NULL;
2819 if (!isl_space_is_set(pwaff->dim))
2820 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2821 "space of input is not a set", goto error);
2822 return map_from_pw_aff(pwaff);
2823 error:
2824 isl_pw_aff_free(pwaff);
2825 return NULL;
2828 /* Return a set containing those elements in the domain
2829 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2830 * does not satisfy "fn" (if complement is 1).
2832 * The pieces with a NaN never belong to the result since
2833 * NaN does not satisfy any property.
2835 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2836 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2837 int complement)
2839 int i;
2840 isl_set *set;
2842 if (!pwaff)
2843 return NULL;
2845 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2847 for (i = 0; i < pwaff->n; ++i) {
2848 isl_basic_set *bset;
2849 isl_set *set_i, *locus;
2850 isl_bool rational;
2852 if (isl_aff_is_nan(pwaff->p[i].aff))
2853 continue;
2855 rational = isl_set_has_rational(pwaff->p[i].set);
2856 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2857 locus = isl_set_from_basic_set(bset);
2858 set_i = isl_set_copy(pwaff->p[i].set);
2859 if (complement)
2860 set_i = isl_set_subtract(set_i, locus);
2861 else
2862 set_i = isl_set_intersect(set_i, locus);
2863 set = isl_set_union_disjoint(set, set_i);
2866 isl_pw_aff_free(pwaff);
2868 return set;
2871 /* Return a set containing those elements in the domain
2872 * of "pa" where it is positive.
2874 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2876 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2879 /* Return a set containing those elements in the domain
2880 * of pwaff where it is non-negative.
2882 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2884 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2887 /* Return a set containing those elements in the domain
2888 * of pwaff where it is zero.
2890 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2892 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2895 /* Return a set containing those elements in the domain
2896 * of pwaff where it is not zero.
2898 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2900 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2903 /* Return a set containing those elements in the shared domain
2904 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2906 * We compute the difference on the shared domain and then construct
2907 * the set of values where this difference is non-negative.
2908 * If strict is set, we first subtract 1 from the difference.
2909 * If equal is set, we only return the elements where pwaff1 and pwaff2
2910 * are equal.
2912 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2913 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2915 isl_set *set1, *set2;
2917 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2918 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2919 set1 = isl_set_intersect(set1, set2);
2920 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2921 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2922 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2924 if (strict) {
2925 isl_space *dim = isl_set_get_space(set1);
2926 isl_aff *aff;
2927 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2928 aff = isl_aff_add_constant_si(aff, -1);
2929 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2930 } else
2931 isl_set_free(set1);
2933 if (equal)
2934 return isl_pw_aff_zero_set(pwaff1);
2935 return isl_pw_aff_nonneg_set(pwaff1);
2938 /* Return a set containing those elements in the shared domain
2939 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2941 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2942 __isl_take isl_pw_aff *pwaff2)
2944 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2947 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2948 __isl_take isl_pw_aff *pwaff2)
2950 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2953 /* Return a set containing those elements in the shared domain
2954 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2956 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2957 __isl_take isl_pw_aff *pwaff2)
2959 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2962 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2963 __isl_take isl_pw_aff *pwaff2)
2965 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2968 /* Return a set containing those elements in the shared domain
2969 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2971 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2972 __isl_take isl_pw_aff *pwaff2)
2974 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2977 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2978 __isl_take isl_pw_aff *pwaff2)
2980 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2983 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2984 __isl_take isl_pw_aff *pwaff2)
2986 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2989 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2990 __isl_take isl_pw_aff *pwaff2)
2992 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2995 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2996 * where the function values are ordered in the same way as "order",
2997 * which returns a set in the shared domain of its two arguments.
2998 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3000 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3001 * We first pull back the two functions such that they are defined on
3002 * the domain [A -> B]. Then we apply "order", resulting in a set
3003 * in the space [A -> B]. Finally, we unwrap this set to obtain
3004 * a map in the space A -> B.
3006 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3007 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3008 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3009 __isl_take isl_pw_aff *pa2))
3011 isl_space *space1, *space2;
3012 isl_multi_aff *ma;
3013 isl_set *set;
3015 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3016 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3017 space1 = isl_space_map_from_domain_and_range(space1, space2);
3018 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3019 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3020 ma = isl_multi_aff_range_map(space1);
3021 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3022 set = order(pa1, pa2);
3024 return isl_set_unwrap(set);
3027 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3028 * where the function values are equal.
3029 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3031 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3032 __isl_take isl_pw_aff *pa2)
3034 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3037 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3038 * where the function values are equal.
3040 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3041 __isl_take isl_pw_aff *pa2)
3043 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3046 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3047 * where the function value of "pa1" is less than the function value of "pa2".
3048 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3050 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3051 __isl_take isl_pw_aff *pa2)
3053 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3056 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3057 * where the function value of "pa1" is less than the function value of "pa2".
3059 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3060 __isl_take isl_pw_aff *pa2)
3062 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3065 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3066 * where the function value of "pa1" is greater than the function value
3067 * of "pa2".
3068 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3070 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3071 __isl_take isl_pw_aff *pa2)
3073 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3076 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3077 * where the function value of "pa1" is greater than the function value
3078 * of "pa2".
3080 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3081 __isl_take isl_pw_aff *pa2)
3083 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3086 /* Return a set containing those elements in the shared domain
3087 * of the elements of list1 and list2 where each element in list1
3088 * has the relation specified by "fn" with each element in list2.
3090 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3091 __isl_take isl_pw_aff_list *list2,
3092 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3093 __isl_take isl_pw_aff *pwaff2))
3095 int i, j;
3096 isl_ctx *ctx;
3097 isl_set *set;
3099 if (!list1 || !list2)
3100 goto error;
3102 ctx = isl_pw_aff_list_get_ctx(list1);
3103 if (list1->n < 1 || list2->n < 1)
3104 isl_die(ctx, isl_error_invalid,
3105 "list should contain at least one element", goto error);
3107 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3108 for (i = 0; i < list1->n; ++i)
3109 for (j = 0; j < list2->n; ++j) {
3110 isl_set *set_ij;
3112 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3113 isl_pw_aff_copy(list2->p[j]));
3114 set = isl_set_intersect(set, set_ij);
3117 isl_pw_aff_list_free(list1);
3118 isl_pw_aff_list_free(list2);
3119 return set;
3120 error:
3121 isl_pw_aff_list_free(list1);
3122 isl_pw_aff_list_free(list2);
3123 return NULL;
3126 /* Return a set containing those elements in the shared domain
3127 * of the elements of list1 and list2 where each element in list1
3128 * is equal to each element in list2.
3130 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3131 __isl_take isl_pw_aff_list *list2)
3133 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3136 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3137 __isl_take isl_pw_aff_list *list2)
3139 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3142 /* Return a set containing those elements in the shared domain
3143 * of the elements of list1 and list2 where each element in list1
3144 * is less than or equal to each element in list2.
3146 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3147 __isl_take isl_pw_aff_list *list2)
3149 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3152 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3153 __isl_take isl_pw_aff_list *list2)
3155 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3158 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3159 __isl_take isl_pw_aff_list *list2)
3161 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3164 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3165 __isl_take isl_pw_aff_list *list2)
3167 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3171 /* Return a set containing those elements in the shared domain
3172 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3174 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3175 __isl_take isl_pw_aff *pwaff2)
3177 isl_set *set_lt, *set_gt;
3179 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3180 isl_pw_aff_copy(pwaff2));
3181 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3182 return isl_set_union_disjoint(set_lt, set_gt);
3185 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3186 __isl_take isl_pw_aff *pwaff2)
3188 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3191 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3192 isl_int v)
3194 int i;
3196 if (isl_int_is_one(v))
3197 return pwaff;
3198 if (!isl_int_is_pos(v))
3199 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3200 "factor needs to be positive",
3201 return isl_pw_aff_free(pwaff));
3202 pwaff = isl_pw_aff_cow(pwaff);
3203 if (!pwaff)
3204 return NULL;
3205 if (pwaff->n == 0)
3206 return pwaff;
3208 for (i = 0; i < pwaff->n; ++i) {
3209 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3210 if (!pwaff->p[i].aff)
3211 return isl_pw_aff_free(pwaff);
3214 return pwaff;
3217 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3219 int i;
3221 pwaff = isl_pw_aff_cow(pwaff);
3222 if (!pwaff)
3223 return NULL;
3224 if (pwaff->n == 0)
3225 return pwaff;
3227 for (i = 0; i < pwaff->n; ++i) {
3228 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3229 if (!pwaff->p[i].aff)
3230 return isl_pw_aff_free(pwaff);
3233 return pwaff;
3236 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3238 int i;
3240 pwaff = isl_pw_aff_cow(pwaff);
3241 if (!pwaff)
3242 return NULL;
3243 if (pwaff->n == 0)
3244 return pwaff;
3246 for (i = 0; i < pwaff->n; ++i) {
3247 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3248 if (!pwaff->p[i].aff)
3249 return isl_pw_aff_free(pwaff);
3252 return pwaff;
3255 /* Assuming that "cond1" and "cond2" are disjoint,
3256 * return an affine expression that is equal to pwaff1 on cond1
3257 * and to pwaff2 on cond2.
3259 static __isl_give isl_pw_aff *isl_pw_aff_select(
3260 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3261 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3263 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3264 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3266 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3269 /* Return an affine expression that is equal to pwaff_true for elements
3270 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3271 * is zero.
3272 * That is, return cond ? pwaff_true : pwaff_false;
3274 * If "cond" involves and NaN, then we conservatively return a NaN
3275 * on its entire domain. In principle, we could consider the pieces
3276 * where it is NaN separately from those where it is not.
3278 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3279 * then only use the domain of "cond" to restrict the domain.
3281 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3282 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3284 isl_set *cond_true, *cond_false;
3285 isl_bool equal;
3287 if (!cond)
3288 goto error;
3289 if (isl_pw_aff_involves_nan(cond)) {
3290 isl_space *space = isl_pw_aff_get_domain_space(cond);
3291 isl_local_space *ls = isl_local_space_from_space(space);
3292 isl_pw_aff_free(cond);
3293 isl_pw_aff_free(pwaff_true);
3294 isl_pw_aff_free(pwaff_false);
3295 return isl_pw_aff_nan_on_domain(ls);
3298 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3299 isl_pw_aff_get_space(pwaff_false));
3300 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3301 isl_pw_aff_get_space(pwaff_true));
3302 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3303 if (equal < 0)
3304 goto error;
3305 if (equal) {
3306 isl_set *dom;
3308 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3309 isl_pw_aff_free(pwaff_false);
3310 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3313 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3314 cond_false = isl_pw_aff_zero_set(cond);
3315 return isl_pw_aff_select(cond_true, pwaff_true,
3316 cond_false, pwaff_false);
3317 error:
3318 isl_pw_aff_free(cond);
3319 isl_pw_aff_free(pwaff_true);
3320 isl_pw_aff_free(pwaff_false);
3321 return NULL;
3324 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3326 if (!aff)
3327 return isl_bool_error;
3329 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3332 /* Check whether pwaff is a piecewise constant.
3334 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3336 int i;
3338 if (!pwaff)
3339 return isl_bool_error;
3341 for (i = 0; i < pwaff->n; ++i) {
3342 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3343 if (is_cst < 0 || !is_cst)
3344 return is_cst;
3347 return isl_bool_true;
3350 /* Are all elements of "mpa" piecewise constants?
3352 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3354 int i;
3356 if (!mpa)
3357 return isl_bool_error;
3359 for (i = 0; i < mpa->n; ++i) {
3360 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3361 if (is_cst < 0 || !is_cst)
3362 return is_cst;
3365 return isl_bool_true;
3368 /* Return the product of "aff1" and "aff2".
3370 * If either of the two is NaN, then the result is NaN.
3372 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3374 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3375 __isl_take isl_aff *aff2)
3377 if (!aff1 || !aff2)
3378 goto error;
3380 if (isl_aff_is_nan(aff1)) {
3381 isl_aff_free(aff2);
3382 return aff1;
3384 if (isl_aff_is_nan(aff2)) {
3385 isl_aff_free(aff1);
3386 return aff2;
3389 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3390 return isl_aff_mul(aff2, aff1);
3392 if (!isl_aff_is_cst(aff2))
3393 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3394 "at least one affine expression should be constant",
3395 goto error);
3397 aff1 = isl_aff_cow(aff1);
3398 if (!aff1 || !aff2)
3399 goto error;
3401 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3402 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3404 isl_aff_free(aff2);
3405 return aff1;
3406 error:
3407 isl_aff_free(aff1);
3408 isl_aff_free(aff2);
3409 return NULL;
3412 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3414 * If either of the two is NaN, then the result is NaN.
3416 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3417 __isl_take isl_aff *aff2)
3419 int is_cst;
3420 int neg;
3422 if (!aff1 || !aff2)
3423 goto error;
3425 if (isl_aff_is_nan(aff1)) {
3426 isl_aff_free(aff2);
3427 return aff1;
3429 if (isl_aff_is_nan(aff2)) {
3430 isl_aff_free(aff1);
3431 return aff2;
3434 is_cst = isl_aff_is_cst(aff2);
3435 if (is_cst < 0)
3436 goto error;
3437 if (!is_cst)
3438 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3439 "second argument should be a constant", goto error);
3441 if (!aff2)
3442 goto error;
3444 neg = isl_int_is_neg(aff2->v->el[1]);
3445 if (neg) {
3446 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3447 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3450 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3451 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3453 if (neg) {
3454 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3455 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3458 isl_aff_free(aff2);
3459 return aff1;
3460 error:
3461 isl_aff_free(aff1);
3462 isl_aff_free(aff2);
3463 return NULL;
3466 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3467 __isl_take isl_pw_aff *pwaff2)
3469 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3472 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3473 __isl_take isl_pw_aff *pwaff2)
3475 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3478 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3479 __isl_take isl_pw_aff *pwaff2)
3481 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3484 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3485 __isl_take isl_pw_aff *pwaff2)
3487 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3490 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3491 __isl_take isl_pw_aff *pwaff2)
3493 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3496 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3497 __isl_take isl_pw_aff *pa2)
3499 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3502 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3504 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3505 __isl_take isl_pw_aff *pa2)
3507 int is_cst;
3509 is_cst = isl_pw_aff_is_cst(pa2);
3510 if (is_cst < 0)
3511 goto error;
3512 if (!is_cst)
3513 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3514 "second argument should be a piecewise constant",
3515 goto error);
3516 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3517 error:
3518 isl_pw_aff_free(pa1);
3519 isl_pw_aff_free(pa2);
3520 return NULL;
3523 /* Compute the quotient of the integer division of "pa1" by "pa2"
3524 * with rounding towards zero.
3525 * "pa2" is assumed to be a piecewise constant.
3527 * In particular, return
3529 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3532 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3533 __isl_take isl_pw_aff *pa2)
3535 int is_cst;
3536 isl_set *cond;
3537 isl_pw_aff *f, *c;
3539 is_cst = isl_pw_aff_is_cst(pa2);
3540 if (is_cst < 0)
3541 goto error;
3542 if (!is_cst)
3543 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3544 "second argument should be a piecewise constant",
3545 goto error);
3547 pa1 = isl_pw_aff_div(pa1, pa2);
3549 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3550 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3551 c = isl_pw_aff_ceil(pa1);
3552 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3553 error:
3554 isl_pw_aff_free(pa1);
3555 isl_pw_aff_free(pa2);
3556 return NULL;
3559 /* Compute the remainder of the integer division of "pa1" by "pa2"
3560 * with rounding towards zero.
3561 * "pa2" is assumed to be a piecewise constant.
3563 * In particular, return
3565 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3568 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3569 __isl_take isl_pw_aff *pa2)
3571 int is_cst;
3572 isl_pw_aff *res;
3574 is_cst = isl_pw_aff_is_cst(pa2);
3575 if (is_cst < 0)
3576 goto error;
3577 if (!is_cst)
3578 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3579 "second argument should be a piecewise constant",
3580 goto error);
3581 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3582 res = isl_pw_aff_mul(pa2, res);
3583 res = isl_pw_aff_sub(pa1, res);
3584 return res;
3585 error:
3586 isl_pw_aff_free(pa1);
3587 isl_pw_aff_free(pa2);
3588 return NULL;
3591 /* Does either of "pa1" or "pa2" involve any NaN2?
3593 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3594 __isl_keep isl_pw_aff *pa2)
3596 isl_bool has_nan;
3598 has_nan = isl_pw_aff_involves_nan(pa1);
3599 if (has_nan < 0 || has_nan)
3600 return has_nan;
3601 return isl_pw_aff_involves_nan(pa2);
3604 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3605 * by a NaN on their shared domain.
3607 * In principle, the result could be refined to only being NaN
3608 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3610 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3611 __isl_take isl_pw_aff *pa2)
3613 isl_local_space *ls;
3614 isl_set *dom;
3615 isl_pw_aff *pa;
3617 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3618 ls = isl_local_space_from_space(isl_set_get_space(dom));
3619 pa = isl_pw_aff_nan_on_domain(ls);
3620 pa = isl_pw_aff_intersect_domain(pa, dom);
3622 return pa;
3625 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3626 __isl_take isl_pw_aff *pwaff2)
3628 isl_set *le;
3629 isl_set *dom;
3631 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3632 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3633 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3634 isl_pw_aff_copy(pwaff2));
3635 dom = isl_set_subtract(dom, isl_set_copy(le));
3636 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3639 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3640 __isl_take isl_pw_aff *pwaff2)
3642 isl_set *ge;
3643 isl_set *dom;
3645 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3646 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3647 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3648 isl_pw_aff_copy(pwaff2));
3649 dom = isl_set_subtract(dom, isl_set_copy(ge));
3650 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3653 /* Return an expression for the minimum (if "max" is not set) or
3654 * the maximum (if "max" is set) of "pa1" and "pa2".
3655 * If either expression involves any NaN, then return a NaN
3656 * on the shared domain as result.
3658 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3659 __isl_take isl_pw_aff *pa2, int max)
3661 isl_bool has_nan;
3663 has_nan = either_involves_nan(pa1, pa2);
3664 if (has_nan < 0)
3665 pa1 = isl_pw_aff_free(pa1);
3666 else if (has_nan)
3667 return replace_by_nan(pa1, pa2);
3669 if (max)
3670 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3671 else
3672 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3675 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3677 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3678 __isl_take isl_pw_aff *pwaff2)
3680 return pw_aff_min_max(pwaff1, pwaff2, 0);
3683 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3685 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3686 __isl_take isl_pw_aff *pwaff2)
3688 return pw_aff_min_max(pwaff1, pwaff2, 1);
3691 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3692 __isl_take isl_pw_aff_list *list,
3693 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3694 __isl_take isl_pw_aff *pwaff2))
3696 int i;
3697 isl_ctx *ctx;
3698 isl_pw_aff *res;
3700 if (!list)
3701 return NULL;
3703 ctx = isl_pw_aff_list_get_ctx(list);
3704 if (list->n < 1)
3705 isl_die(ctx, isl_error_invalid,
3706 "list should contain at least one element", goto error);
3708 res = isl_pw_aff_copy(list->p[0]);
3709 for (i = 1; i < list->n; ++i)
3710 res = fn(res, isl_pw_aff_copy(list->p[i]));
3712 isl_pw_aff_list_free(list);
3713 return res;
3714 error:
3715 isl_pw_aff_list_free(list);
3716 return NULL;
3719 /* Return an isl_pw_aff that maps each element in the intersection of the
3720 * domains of the elements of list to the minimal corresponding affine
3721 * expression.
3723 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3725 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3728 /* Return an isl_pw_aff that maps each element in the intersection of the
3729 * domains of the elements of list to the maximal corresponding affine
3730 * expression.
3732 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3734 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3737 /* Mark the domains of "pwaff" as rational.
3739 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3741 int i;
3743 pwaff = isl_pw_aff_cow(pwaff);
3744 if (!pwaff)
3745 return NULL;
3746 if (pwaff->n == 0)
3747 return pwaff;
3749 for (i = 0; i < pwaff->n; ++i) {
3750 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3751 if (!pwaff->p[i].set)
3752 return isl_pw_aff_free(pwaff);
3755 return pwaff;
3758 /* Mark the domains of the elements of "list" as rational.
3760 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3761 __isl_take isl_pw_aff_list *list)
3763 int i, n;
3765 if (!list)
3766 return NULL;
3767 if (list->n == 0)
3768 return list;
3770 n = list->n;
3771 for (i = 0; i < n; ++i) {
3772 isl_pw_aff *pa;
3774 pa = isl_pw_aff_list_get_pw_aff(list, i);
3775 pa = isl_pw_aff_set_rational(pa);
3776 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3779 return list;
3782 /* Do the parameters of "aff" match those of "space"?
3784 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3785 __isl_keep isl_space *space)
3787 isl_space *aff_space;
3788 isl_bool match;
3790 if (!aff || !space)
3791 return isl_bool_error;
3793 aff_space = isl_aff_get_domain_space(aff);
3795 match = isl_space_has_equal_params(space, aff_space);
3797 isl_space_free(aff_space);
3798 return match;
3801 /* Check that the domain space of "aff" matches "space".
3803 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3804 __isl_keep isl_space *space)
3806 isl_space *aff_space;
3807 isl_bool match;
3809 if (!aff || !space)
3810 return isl_stat_error;
3812 aff_space = isl_aff_get_domain_space(aff);
3814 match = isl_space_has_equal_params(space, aff_space);
3815 if (match < 0)
3816 goto error;
3817 if (!match)
3818 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3819 "parameters don't match", goto error);
3820 match = isl_space_tuple_is_equal(space, isl_dim_in,
3821 aff_space, isl_dim_set);
3822 if (match < 0)
3823 goto error;
3824 if (!match)
3825 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3826 "domains don't match", goto error);
3827 isl_space_free(aff_space);
3828 return isl_stat_ok;
3829 error:
3830 isl_space_free(aff_space);
3831 return isl_stat_error;
3834 #undef BASE
3835 #define BASE aff
3836 #undef DOMBASE
3837 #define DOMBASE set
3838 #define NO_DOMAIN
3840 #include <isl_multi_no_explicit_domain.c>
3841 #include <isl_multi_templ.c>
3842 #include <isl_multi_apply_set.c>
3843 #include <isl_multi_cmp.c>
3844 #include <isl_multi_dims.c>
3845 #include <isl_multi_floor.c>
3846 #include <isl_multi_gist.c>
3848 #undef NO_DOMAIN
3850 /* Construct an isl_multi_aff living in "space" that corresponds
3851 * to the affine transformation matrix "mat".
3853 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3854 __isl_take isl_space *space, __isl_take isl_mat *mat)
3856 isl_ctx *ctx;
3857 isl_local_space *ls = NULL;
3858 isl_multi_aff *ma = NULL;
3859 int n_row, n_col, n_out, total;
3860 int i;
3862 if (!space || !mat)
3863 goto error;
3865 ctx = isl_mat_get_ctx(mat);
3867 n_row = isl_mat_rows(mat);
3868 n_col = isl_mat_cols(mat);
3869 if (n_row < 1)
3870 isl_die(ctx, isl_error_invalid,
3871 "insufficient number of rows", goto error);
3872 if (n_col < 1)
3873 isl_die(ctx, isl_error_invalid,
3874 "insufficient number of columns", goto error);
3875 n_out = isl_space_dim(space, isl_dim_out);
3876 total = isl_space_dim(space, isl_dim_all);
3877 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3878 isl_die(ctx, isl_error_invalid,
3879 "dimension mismatch", goto error);
3881 ma = isl_multi_aff_zero(isl_space_copy(space));
3882 ls = isl_local_space_from_space(isl_space_domain(space));
3884 for (i = 0; i < n_row - 1; ++i) {
3885 isl_vec *v;
3886 isl_aff *aff;
3888 v = isl_vec_alloc(ctx, 1 + n_col);
3889 if (!v)
3890 goto error;
3891 isl_int_set(v->el[0], mat->row[0][0]);
3892 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3893 v = isl_vec_normalize(v);
3894 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3895 ma = isl_multi_aff_set_aff(ma, i, aff);
3898 isl_local_space_free(ls);
3899 isl_mat_free(mat);
3900 return ma;
3901 error:
3902 isl_local_space_free(ls);
3903 isl_mat_free(mat);
3904 isl_multi_aff_free(ma);
3905 return NULL;
3908 /* Remove any internal structure of the domain of "ma".
3909 * If there is any such internal structure in the input,
3910 * then the name of the corresponding space is also removed.
3912 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3913 __isl_take isl_multi_aff *ma)
3915 isl_space *space;
3917 if (!ma)
3918 return NULL;
3920 if (!ma->space->nested[0])
3921 return ma;
3923 space = isl_multi_aff_get_space(ma);
3924 space = isl_space_flatten_domain(space);
3925 ma = isl_multi_aff_reset_space(ma, space);
3927 return ma;
3930 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3931 * of the space to its domain.
3933 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3935 int i, n_in;
3936 isl_local_space *ls;
3937 isl_multi_aff *ma;
3939 if (!space)
3940 return NULL;
3941 if (!isl_space_is_map(space))
3942 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3943 "not a map space", goto error);
3945 n_in = isl_space_dim(space, isl_dim_in);
3946 space = isl_space_domain_map(space);
3948 ma = isl_multi_aff_alloc(isl_space_copy(space));
3949 if (n_in == 0) {
3950 isl_space_free(space);
3951 return ma;
3954 space = isl_space_domain(space);
3955 ls = isl_local_space_from_space(space);
3956 for (i = 0; i < n_in; ++i) {
3957 isl_aff *aff;
3959 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3960 isl_dim_set, i);
3961 ma = isl_multi_aff_set_aff(ma, i, aff);
3963 isl_local_space_free(ls);
3964 return ma;
3965 error:
3966 isl_space_free(space);
3967 return NULL;
3970 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3971 * of the space to its range.
3973 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3975 int i, n_in, n_out;
3976 isl_local_space *ls;
3977 isl_multi_aff *ma;
3979 if (!space)
3980 return NULL;
3981 if (!isl_space_is_map(space))
3982 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3983 "not a map space", goto error);
3985 n_in = isl_space_dim(space, isl_dim_in);
3986 n_out = isl_space_dim(space, isl_dim_out);
3987 space = isl_space_range_map(space);
3989 ma = isl_multi_aff_alloc(isl_space_copy(space));
3990 if (n_out == 0) {
3991 isl_space_free(space);
3992 return ma;
3995 space = isl_space_domain(space);
3996 ls = isl_local_space_from_space(space);
3997 for (i = 0; i < n_out; ++i) {
3998 isl_aff *aff;
4000 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4001 isl_dim_set, n_in + i);
4002 ma = isl_multi_aff_set_aff(ma, i, aff);
4004 isl_local_space_free(ls);
4005 return ma;
4006 error:
4007 isl_space_free(space);
4008 return NULL;
4011 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4012 * of the space to its range.
4014 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4015 __isl_take isl_space *space)
4017 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4020 /* Given the space of a set and a range of set dimensions,
4021 * construct an isl_multi_aff that projects out those dimensions.
4023 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4024 __isl_take isl_space *space, enum isl_dim_type type,
4025 unsigned first, unsigned n)
4027 int i, dim;
4028 isl_local_space *ls;
4029 isl_multi_aff *ma;
4031 if (!space)
4032 return NULL;
4033 if (!isl_space_is_set(space))
4034 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4035 "expecting set space", goto error);
4036 if (type != isl_dim_set)
4037 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4038 "only set dimensions can be projected out", goto error);
4040 dim = isl_space_dim(space, isl_dim_set);
4041 if (first + n > dim)
4042 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4043 "range out of bounds", goto error);
4045 space = isl_space_from_domain(space);
4046 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4048 if (dim == n)
4049 return isl_multi_aff_alloc(space);
4051 ma = isl_multi_aff_alloc(isl_space_copy(space));
4052 space = isl_space_domain(space);
4053 ls = isl_local_space_from_space(space);
4055 for (i = 0; i < first; ++i) {
4056 isl_aff *aff;
4058 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4059 isl_dim_set, i);
4060 ma = isl_multi_aff_set_aff(ma, i, aff);
4063 for (i = 0; i < dim - (first + n); ++i) {
4064 isl_aff *aff;
4066 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4067 isl_dim_set, first + n + i);
4068 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4071 isl_local_space_free(ls);
4072 return ma;
4073 error:
4074 isl_space_free(space);
4075 return NULL;
4078 /* Given the space of a set and a range of set dimensions,
4079 * construct an isl_pw_multi_aff that projects out those dimensions.
4081 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4082 __isl_take isl_space *space, enum isl_dim_type type,
4083 unsigned first, unsigned n)
4085 isl_multi_aff *ma;
4087 ma = isl_multi_aff_project_out_map(space, type, first, n);
4088 return isl_pw_multi_aff_from_multi_aff(ma);
4091 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4092 * domain.
4094 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4095 __isl_take isl_multi_aff *ma)
4097 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4098 return isl_pw_multi_aff_alloc(dom, ma);
4101 /* Create a piecewise multi-affine expression in the given space that maps each
4102 * input dimension to the corresponding output dimension.
4104 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4105 __isl_take isl_space *space)
4107 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4110 /* Exploit the equalities in "eq" to simplify the affine expressions.
4112 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4113 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4115 int i;
4117 maff = isl_multi_aff_cow(maff);
4118 if (!maff || !eq)
4119 goto error;
4121 for (i = 0; i < maff->n; ++i) {
4122 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4123 isl_basic_set_copy(eq));
4124 if (!maff->u.p[i])
4125 goto error;
4128 isl_basic_set_free(eq);
4129 return maff;
4130 error:
4131 isl_basic_set_free(eq);
4132 isl_multi_aff_free(maff);
4133 return NULL;
4136 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4137 isl_int f)
4139 int i;
4141 maff = isl_multi_aff_cow(maff);
4142 if (!maff)
4143 return NULL;
4145 for (i = 0; i < maff->n; ++i) {
4146 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4147 if (!maff->u.p[i])
4148 return isl_multi_aff_free(maff);
4151 return maff;
4154 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4155 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4157 maff1 = isl_multi_aff_add(maff1, maff2);
4158 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4159 return maff1;
4162 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4164 if (!maff)
4165 return -1;
4167 return 0;
4170 /* Return the set of domain elements where "ma1" is lexicographically
4171 * smaller than or equal to "ma2".
4173 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4174 __isl_take isl_multi_aff *ma2)
4176 return isl_multi_aff_lex_ge_set(ma2, ma1);
4179 /* Return the set of domain elements where "ma1" is lexicographically
4180 * smaller than "ma2".
4182 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4183 __isl_take isl_multi_aff *ma2)
4185 return isl_multi_aff_lex_gt_set(ma2, ma1);
4188 /* Return the set of domain elements where "ma1" and "ma2"
4189 * satisfy "order".
4191 static __isl_give isl_set *isl_multi_aff_order_set(
4192 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4193 __isl_give isl_map *order(__isl_take isl_space *set_space))
4195 isl_space *space;
4196 isl_map *map1, *map2;
4197 isl_map *map, *ge;
4199 map1 = isl_map_from_multi_aff(ma1);
4200 map2 = isl_map_from_multi_aff(ma2);
4201 map = isl_map_range_product(map1, map2);
4202 space = isl_space_range(isl_map_get_space(map));
4203 space = isl_space_domain(isl_space_unwrap(space));
4204 ge = order(space);
4205 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4207 return isl_map_domain(map);
4210 /* Return the set of domain elements where "ma1" is lexicographically
4211 * greater than or equal to "ma2".
4213 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4214 __isl_take isl_multi_aff *ma2)
4216 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4219 /* Return the set of domain elements where "ma1" is lexicographically
4220 * greater than "ma2".
4222 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4223 __isl_take isl_multi_aff *ma2)
4225 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4228 #undef PW
4229 #define PW isl_pw_multi_aff
4230 #undef EL
4231 #define EL isl_multi_aff
4232 #undef EL_IS_ZERO
4233 #define EL_IS_ZERO is_empty
4234 #undef ZERO
4235 #define ZERO empty
4236 #undef IS_ZERO
4237 #define IS_ZERO is_empty
4238 #undef FIELD
4239 #define FIELD maff
4240 #undef DEFAULT_IS_ZERO
4241 #define DEFAULT_IS_ZERO 0
4243 #define NO_SUB
4244 #define NO_OPT
4245 #define NO_INVOLVES_DIMS
4246 #define NO_INSERT_DIMS
4247 #define NO_LIFT
4248 #define NO_MORPH
4250 #include <isl_pw_templ.c>
4251 #include <isl_pw_union_opt.c>
4253 #undef NO_SUB
4255 #undef UNION
4256 #define UNION isl_union_pw_multi_aff
4257 #undef PART
4258 #define PART isl_pw_multi_aff
4259 #undef PARTS
4260 #define PARTS pw_multi_aff
4262 #include <isl_union_multi.c>
4263 #include <isl_union_neg.c>
4265 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4266 __isl_take isl_pw_multi_aff *pma1,
4267 __isl_take isl_pw_multi_aff *pma2)
4269 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4270 &isl_multi_aff_lex_ge_set);
4273 /* Given two piecewise multi affine expressions, return a piecewise
4274 * multi-affine expression defined on the union of the definition domains
4275 * of the inputs that is equal to the lexicographic maximum of the two
4276 * inputs on each cell. If only one of the two inputs is defined on
4277 * a given cell, then it is considered to be the maximum.
4279 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4280 __isl_take isl_pw_multi_aff *pma1,
4281 __isl_take isl_pw_multi_aff *pma2)
4283 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4284 &pw_multi_aff_union_lexmax);
4287 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4288 __isl_take isl_pw_multi_aff *pma1,
4289 __isl_take isl_pw_multi_aff *pma2)
4291 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4292 &isl_multi_aff_lex_le_set);
4295 /* Given two piecewise multi affine expressions, return a piecewise
4296 * multi-affine expression defined on the union of the definition domains
4297 * of the inputs that is equal to the lexicographic minimum of the two
4298 * inputs on each cell. If only one of the two inputs is defined on
4299 * a given cell, then it is considered to be the minimum.
4301 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4302 __isl_take isl_pw_multi_aff *pma1,
4303 __isl_take isl_pw_multi_aff *pma2)
4305 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4306 &pw_multi_aff_union_lexmin);
4309 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4310 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4312 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4313 &isl_multi_aff_add);
4316 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4317 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4319 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4320 &pw_multi_aff_add);
4323 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4324 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4326 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4327 &isl_multi_aff_sub);
4330 /* Subtract "pma2" from "pma1" and return the result.
4332 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4333 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4335 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4336 &pw_multi_aff_sub);
4339 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4340 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4342 return isl_pw_multi_aff_union_add_(pma1, pma2);
4345 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4346 * with the actual sum on the shared domain and
4347 * the defined expression on the symmetric difference of the domains.
4349 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4350 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4352 return isl_union_pw_aff_union_add_(upa1, upa2);
4355 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4356 * with the actual sum on the shared domain and
4357 * the defined expression on the symmetric difference of the domains.
4359 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4360 __isl_take isl_union_pw_multi_aff *upma1,
4361 __isl_take isl_union_pw_multi_aff *upma2)
4363 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4366 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4367 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4369 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4370 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4372 int i, j, n;
4373 isl_space *space;
4374 isl_pw_multi_aff *res;
4376 if (!pma1 || !pma2)
4377 goto error;
4379 n = pma1->n * pma2->n;
4380 space = isl_space_product(isl_space_copy(pma1->dim),
4381 isl_space_copy(pma2->dim));
4382 res = isl_pw_multi_aff_alloc_size(space, n);
4384 for (i = 0; i < pma1->n; ++i) {
4385 for (j = 0; j < pma2->n; ++j) {
4386 isl_set *domain;
4387 isl_multi_aff *ma;
4389 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4390 isl_set_copy(pma2->p[j].set));
4391 ma = isl_multi_aff_product(
4392 isl_multi_aff_copy(pma1->p[i].maff),
4393 isl_multi_aff_copy(pma2->p[j].maff));
4394 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4398 isl_pw_multi_aff_free(pma1);
4399 isl_pw_multi_aff_free(pma2);
4400 return res;
4401 error:
4402 isl_pw_multi_aff_free(pma1);
4403 isl_pw_multi_aff_free(pma2);
4404 return NULL;
4407 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4408 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4410 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4411 &pw_multi_aff_product);
4414 /* Construct a map mapping the domain of the piecewise multi-affine expression
4415 * to its range, with each dimension in the range equated to the
4416 * corresponding affine expression on its cell.
4418 * If the domain of "pma" is rational, then so is the constructed "map".
4420 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4422 int i;
4423 isl_map *map;
4425 if (!pma)
4426 return NULL;
4428 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4430 for (i = 0; i < pma->n; ++i) {
4431 isl_bool rational;
4432 isl_multi_aff *maff;
4433 isl_basic_map *bmap;
4434 isl_map *map_i;
4436 rational = isl_set_is_rational(pma->p[i].set);
4437 if (rational < 0)
4438 map = isl_map_free(map);
4439 maff = isl_multi_aff_copy(pma->p[i].maff);
4440 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4441 map_i = isl_map_from_basic_map(bmap);
4442 map_i = isl_map_intersect_domain(map_i,
4443 isl_set_copy(pma->p[i].set));
4444 map = isl_map_union_disjoint(map, map_i);
4447 isl_pw_multi_aff_free(pma);
4448 return map;
4451 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4453 if (!pma)
4454 return NULL;
4456 if (!isl_space_is_set(pma->dim))
4457 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4458 "isl_pw_multi_aff cannot be converted into an isl_set",
4459 goto error);
4461 return isl_map_from_pw_multi_aff(pma);
4462 error:
4463 isl_pw_multi_aff_free(pma);
4464 return NULL;
4467 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4468 * denominator "denom".
4469 * "denom" is allowed to be negative, in which case the actual denominator
4470 * is -denom and the expressions are added instead.
4472 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4473 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4475 int i, first;
4476 int sign;
4477 isl_int d;
4479 first = isl_seq_first_non_zero(c, n);
4480 if (first == -1)
4481 return aff;
4483 sign = isl_int_sgn(denom);
4484 isl_int_init(d);
4485 isl_int_abs(d, denom);
4486 for (i = first; i < n; ++i) {
4487 isl_aff *aff_i;
4489 if (isl_int_is_zero(c[i]))
4490 continue;
4491 aff_i = isl_multi_aff_get_aff(ma, i);
4492 aff_i = isl_aff_scale(aff_i, c[i]);
4493 aff_i = isl_aff_scale_down(aff_i, d);
4494 if (sign >= 0)
4495 aff = isl_aff_sub(aff, aff_i);
4496 else
4497 aff = isl_aff_add(aff, aff_i);
4499 isl_int_clear(d);
4501 return aff;
4504 /* Extract an affine expression that expresses the output dimension "pos"
4505 * of "bmap" in terms of the parameters and input dimensions from
4506 * equality "eq".
4507 * Note that this expression may involve integer divisions defined
4508 * in terms of parameters and input dimensions.
4509 * The equality may also involve references to earlier (but not later)
4510 * output dimensions. These are replaced by the corresponding elements
4511 * in "ma".
4513 * If the equality is of the form
4515 * f(i) + h(j) + a x + g(i) = 0,
4517 * with f(i) a linear combinations of the parameters and input dimensions,
4518 * g(i) a linear combination of integer divisions defined in terms of the same
4519 * and h(j) a linear combinations of earlier output dimensions,
4520 * then the affine expression is
4522 * (-f(i) - g(i))/a - h(j)/a
4524 * If the equality is of the form
4526 * f(i) + h(j) - a x + g(i) = 0,
4528 * then the affine expression is
4530 * (f(i) + g(i))/a - h(j)/(-a)
4533 * If "div" refers to an integer division (i.e., it is smaller than
4534 * the number of integer divisions), then the equality constraint
4535 * does involve an integer division (the one at position "div") that
4536 * is defined in terms of output dimensions. However, this integer
4537 * division can be eliminated by exploiting a pair of constraints
4538 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4539 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4540 * -l + x >= 0.
4541 * In particular, let
4543 * x = e(i) + m floor(...)
4545 * with e(i) the expression derived above and floor(...) the integer
4546 * division involving output dimensions.
4547 * From
4549 * l <= x <= l + n,
4551 * we have
4553 * 0 <= x - l <= n
4555 * This means
4557 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4558 * = (e(i) - l) mod m
4560 * Therefore,
4562 * x - l = (e(i) - l) mod m
4564 * or
4566 * x = ((e(i) - l) mod m) + l
4568 * The variable "shift" below contains the expression -l, which may
4569 * also involve a linear combination of earlier output dimensions.
4571 static __isl_give isl_aff *extract_aff_from_equality(
4572 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4573 __isl_keep isl_multi_aff *ma)
4575 unsigned o_out;
4576 unsigned n_div, n_out;
4577 isl_ctx *ctx;
4578 isl_local_space *ls;
4579 isl_aff *aff, *shift;
4580 isl_val *mod;
4582 ctx = isl_basic_map_get_ctx(bmap);
4583 ls = isl_basic_map_get_local_space(bmap);
4584 ls = isl_local_space_domain(ls);
4585 aff = isl_aff_alloc(isl_local_space_copy(ls));
4586 if (!aff)
4587 goto error;
4588 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4589 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4590 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4591 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4592 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4593 isl_seq_cpy(aff->v->el + 1 + o_out,
4594 bmap->eq[eq] + o_out + n_out, n_div);
4595 } else {
4596 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4597 isl_seq_neg(aff->v->el + 1 + o_out,
4598 bmap->eq[eq] + o_out + n_out, n_div);
4600 if (div < n_div)
4601 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4602 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4603 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4604 bmap->eq[eq][o_out + pos]);
4605 if (div < n_div) {
4606 shift = isl_aff_alloc(isl_local_space_copy(ls));
4607 if (!shift)
4608 goto error;
4609 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4610 isl_seq_cpy(shift->v->el + 1 + o_out,
4611 bmap->ineq[ineq] + o_out + n_out, n_div);
4612 isl_int_set_si(shift->v->el[0], 1);
4613 shift = subtract_initial(shift, ma, pos,
4614 bmap->ineq[ineq] + o_out, ctx->negone);
4615 aff = isl_aff_add(aff, isl_aff_copy(shift));
4616 mod = isl_val_int_from_isl_int(ctx,
4617 bmap->eq[eq][o_out + n_out + div]);
4618 mod = isl_val_abs(mod);
4619 aff = isl_aff_mod_val(aff, mod);
4620 aff = isl_aff_sub(aff, shift);
4623 isl_local_space_free(ls);
4624 return aff;
4625 error:
4626 isl_local_space_free(ls);
4627 isl_aff_free(aff);
4628 return NULL;
4631 /* Given a basic map with output dimensions defined
4632 * in terms of the parameters input dimensions and earlier
4633 * output dimensions using an equality (and possibly a pair on inequalities),
4634 * extract an isl_aff that expresses output dimension "pos" in terms
4635 * of the parameters and input dimensions.
4636 * Note that this expression may involve integer divisions defined
4637 * in terms of parameters and input dimensions.
4638 * "ma" contains the expressions corresponding to earlier output dimensions.
4640 * This function shares some similarities with
4641 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4643 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4644 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4646 int eq, div, ineq;
4647 isl_aff *aff;
4649 if (!bmap)
4650 return NULL;
4651 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4652 if (eq >= bmap->n_eq)
4653 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4654 "unable to find suitable equality", return NULL);
4655 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4657 aff = isl_aff_remove_unused_divs(aff);
4658 return aff;
4661 /* Given a basic map where each output dimension is defined
4662 * in terms of the parameters and input dimensions using an equality,
4663 * extract an isl_multi_aff that expresses the output dimensions in terms
4664 * of the parameters and input dimensions.
4666 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4667 __isl_take isl_basic_map *bmap)
4669 int i;
4670 unsigned n_out;
4671 isl_multi_aff *ma;
4673 if (!bmap)
4674 return NULL;
4676 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4677 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4679 for (i = 0; i < n_out; ++i) {
4680 isl_aff *aff;
4682 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4683 ma = isl_multi_aff_set_aff(ma, i, aff);
4686 isl_basic_map_free(bmap);
4688 return ma;
4691 /* Given a basic set where each set dimension is defined
4692 * in terms of the parameters using an equality,
4693 * extract an isl_multi_aff that expresses the set dimensions in terms
4694 * of the parameters.
4696 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4697 __isl_take isl_basic_set *bset)
4699 return extract_isl_multi_aff_from_basic_map(bset);
4702 /* Create an isl_pw_multi_aff that is equivalent to
4703 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4704 * The given basic map is such that each output dimension is defined
4705 * in terms of the parameters and input dimensions using an equality.
4707 * Since some applications expect the result of isl_pw_multi_aff_from_map
4708 * to only contain integer affine expressions, we compute the floor
4709 * of the expression before returning.
4711 * Remove all constraints involving local variables without
4712 * an explicit representation (resulting in the removal of those
4713 * local variables) prior to the actual extraction to ensure
4714 * that the local spaces in which the resulting affine expressions
4715 * are created do not contain any unknown local variables.
4716 * Removing such constraints is safe because constraints involving
4717 * unknown local variables are not used to determine whether
4718 * a basic map is obviously single-valued.
4720 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4721 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4723 isl_multi_aff *ma;
4725 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4726 ma = extract_isl_multi_aff_from_basic_map(bmap);
4727 ma = isl_multi_aff_floor(ma);
4728 return isl_pw_multi_aff_alloc(domain, ma);
4731 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4732 * This obviously only works if the input "map" is single-valued.
4733 * If so, we compute the lexicographic minimum of the image in the form
4734 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4735 * to its lexicographic minimum.
4736 * If the input is not single-valued, we produce an error.
4738 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4739 __isl_take isl_map *map)
4741 int i;
4742 int sv;
4743 isl_pw_multi_aff *pma;
4745 sv = isl_map_is_single_valued(map);
4746 if (sv < 0)
4747 goto error;
4748 if (!sv)
4749 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4750 "map is not single-valued", goto error);
4751 map = isl_map_make_disjoint(map);
4752 if (!map)
4753 return NULL;
4755 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4757 for (i = 0; i < map->n; ++i) {
4758 isl_pw_multi_aff *pma_i;
4759 isl_basic_map *bmap;
4760 bmap = isl_basic_map_copy(map->p[i]);
4761 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4762 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4765 isl_map_free(map);
4766 return pma;
4767 error:
4768 isl_map_free(map);
4769 return NULL;
4772 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4773 * taking into account that the output dimension at position "d"
4774 * can be represented as
4776 * x = floor((e(...) + c1) / m)
4778 * given that constraint "i" is of the form
4780 * e(...) + c1 - m x >= 0
4783 * Let "map" be of the form
4785 * A -> B
4787 * We construct a mapping
4789 * A -> [A -> x = floor(...)]
4791 * apply that to the map, obtaining
4793 * [A -> x = floor(...)] -> B
4795 * and equate dimension "d" to x.
4796 * We then compute a isl_pw_multi_aff representation of the resulting map
4797 * and plug in the mapping above.
4799 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4800 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4802 isl_ctx *ctx;
4803 isl_space *space;
4804 isl_local_space *ls;
4805 isl_multi_aff *ma;
4806 isl_aff *aff;
4807 isl_vec *v;
4808 isl_map *insert;
4809 int offset;
4810 int n;
4811 int n_in;
4812 isl_pw_multi_aff *pma;
4813 isl_bool is_set;
4815 is_set = isl_map_is_set(map);
4816 if (is_set < 0)
4817 goto error;
4819 offset = isl_basic_map_offset(hull, isl_dim_out);
4820 ctx = isl_map_get_ctx(map);
4821 space = isl_space_domain(isl_map_get_space(map));
4822 n_in = isl_space_dim(space, isl_dim_set);
4823 n = isl_space_dim(space, isl_dim_all);
4825 v = isl_vec_alloc(ctx, 1 + 1 + n);
4826 if (v) {
4827 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4828 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4830 isl_basic_map_free(hull);
4832 ls = isl_local_space_from_space(isl_space_copy(space));
4833 aff = isl_aff_alloc_vec(ls, v);
4834 aff = isl_aff_floor(aff);
4835 if (is_set) {
4836 isl_space_free(space);
4837 ma = isl_multi_aff_from_aff(aff);
4838 } else {
4839 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4840 ma = isl_multi_aff_range_product(ma,
4841 isl_multi_aff_from_aff(aff));
4844 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4845 map = isl_map_apply_domain(map, insert);
4846 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4847 pma = isl_pw_multi_aff_from_map(map);
4848 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4850 return pma;
4851 error:
4852 isl_map_free(map);
4853 isl_basic_map_free(hull);
4854 return NULL;
4857 /* Is constraint "c" of the form
4859 * e(...) + c1 - m x >= 0
4861 * or
4863 * -e(...) + c2 + m x >= 0
4865 * where m > 1 and e only depends on parameters and input dimemnsions?
4867 * "offset" is the offset of the output dimensions
4868 * "pos" is the position of output dimension x.
4870 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4872 if (isl_int_is_zero(c[offset + d]))
4873 return 0;
4874 if (isl_int_is_one(c[offset + d]))
4875 return 0;
4876 if (isl_int_is_negone(c[offset + d]))
4877 return 0;
4878 if (isl_seq_first_non_zero(c + offset, d) != -1)
4879 return 0;
4880 if (isl_seq_first_non_zero(c + offset + d + 1,
4881 total - (offset + d + 1)) != -1)
4882 return 0;
4883 return 1;
4886 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4888 * As a special case, we first check if there is any pair of constraints,
4889 * shared by all the basic maps in "map" that force a given dimension
4890 * to be equal to the floor of some affine combination of the input dimensions.
4892 * In particular, if we can find two constraints
4894 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4896 * and
4898 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4900 * where m > 1 and e only depends on parameters and input dimemnsions,
4901 * and such that
4903 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4905 * then we know that we can take
4907 * x = floor((e(...) + c1) / m)
4909 * without having to perform any computation.
4911 * Note that we know that
4913 * c1 + c2 >= 1
4915 * If c1 + c2 were 0, then we would have detected an equality during
4916 * simplification. If c1 + c2 were negative, then we would have detected
4917 * a contradiction.
4919 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4920 __isl_take isl_map *map)
4922 int d, dim;
4923 int i, j, n;
4924 int offset, total;
4925 isl_int sum;
4926 isl_basic_map *hull;
4928 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4929 if (!hull)
4930 goto error;
4932 isl_int_init(sum);
4933 dim = isl_map_dim(map, isl_dim_out);
4934 offset = isl_basic_map_offset(hull, isl_dim_out);
4935 total = 1 + isl_basic_map_total_dim(hull);
4936 n = hull->n_ineq;
4937 for (d = 0; d < dim; ++d) {
4938 for (i = 0; i < n; ++i) {
4939 if (!is_potential_div_constraint(hull->ineq[i],
4940 offset, d, total))
4941 continue;
4942 for (j = i + 1; j < n; ++j) {
4943 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4944 hull->ineq[j] + 1, total - 1))
4945 continue;
4946 isl_int_add(sum, hull->ineq[i][0],
4947 hull->ineq[j][0]);
4948 if (isl_int_abs_lt(sum,
4949 hull->ineq[i][offset + d]))
4950 break;
4953 if (j >= n)
4954 continue;
4955 isl_int_clear(sum);
4956 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4957 j = i;
4958 return pw_multi_aff_from_map_div(map, hull, d, j);
4961 isl_int_clear(sum);
4962 isl_basic_map_free(hull);
4963 return pw_multi_aff_from_map_base(map);
4964 error:
4965 isl_map_free(map);
4966 isl_basic_map_free(hull);
4967 return NULL;
4970 /* Given an affine expression
4972 * [A -> B] -> f(A,B)
4974 * construct an isl_multi_aff
4976 * [A -> B] -> B'
4978 * such that dimension "d" in B' is set to "aff" and the remaining
4979 * dimensions are set equal to the corresponding dimensions in B.
4980 * "n_in" is the dimension of the space A.
4981 * "n_out" is the dimension of the space B.
4983 * If "is_set" is set, then the affine expression is of the form
4985 * [B] -> f(B)
4987 * and we construct an isl_multi_aff
4989 * B -> B'
4991 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4992 unsigned n_in, unsigned n_out, int is_set)
4994 int i;
4995 isl_multi_aff *ma;
4996 isl_space *space, *space2;
4997 isl_local_space *ls;
4999 space = isl_aff_get_domain_space(aff);
5000 ls = isl_local_space_from_space(isl_space_copy(space));
5001 space2 = isl_space_copy(space);
5002 if (!is_set)
5003 space2 = isl_space_range(isl_space_unwrap(space2));
5004 space = isl_space_map_from_domain_and_range(space, space2);
5005 ma = isl_multi_aff_alloc(space);
5006 ma = isl_multi_aff_set_aff(ma, d, aff);
5008 for (i = 0; i < n_out; ++i) {
5009 if (i == d)
5010 continue;
5011 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5012 isl_dim_set, n_in + i);
5013 ma = isl_multi_aff_set_aff(ma, i, aff);
5016 isl_local_space_free(ls);
5018 return ma;
5021 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5022 * taking into account that the dimension at position "d" can be written as
5024 * x = m a + f(..) (1)
5026 * where m is equal to "gcd".
5027 * "i" is the index of the equality in "hull" that defines f(..).
5028 * In particular, the equality is of the form
5030 * f(..) - x + m g(existentials) = 0
5032 * or
5034 * -f(..) + x + m g(existentials) = 0
5036 * We basically plug (1) into "map", resulting in a map with "a"
5037 * in the range instead of "x". The corresponding isl_pw_multi_aff
5038 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5040 * Specifically, given the input map
5042 * A -> B
5044 * We first wrap it into a set
5046 * [A -> B]
5048 * and define (1) on top of the corresponding space, resulting in "aff".
5049 * We use this to create an isl_multi_aff that maps the output position "d"
5050 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5051 * We plug this into the wrapped map, unwrap the result and compute the
5052 * corresponding isl_pw_multi_aff.
5053 * The result is an expression
5055 * A -> T(A)
5057 * We adjust that to
5059 * A -> [A -> T(A)]
5061 * so that we can plug that into "aff", after extending the latter to
5062 * a mapping
5064 * [A -> B] -> B'
5067 * If "map" is actually a set, then there is no "A" space, meaning
5068 * that we do not need to perform any wrapping, and that the result
5069 * of the recursive call is of the form
5071 * [T]
5073 * which is plugged into a mapping of the form
5075 * B -> B'
5077 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5078 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5079 isl_int gcd)
5081 isl_set *set;
5082 isl_space *space;
5083 isl_local_space *ls;
5084 isl_aff *aff;
5085 isl_multi_aff *ma;
5086 isl_pw_multi_aff *pma, *id;
5087 unsigned n_in;
5088 unsigned o_out;
5089 unsigned n_out;
5090 isl_bool is_set;
5092 is_set = isl_map_is_set(map);
5093 if (is_set < 0)
5094 goto error;
5096 n_in = isl_basic_map_dim(hull, isl_dim_in);
5097 n_out = isl_basic_map_dim(hull, isl_dim_out);
5098 o_out = isl_basic_map_offset(hull, isl_dim_out);
5100 if (is_set)
5101 set = map;
5102 else
5103 set = isl_map_wrap(map);
5104 space = isl_space_map_from_set(isl_set_get_space(set));
5105 ma = isl_multi_aff_identity(space);
5106 ls = isl_local_space_from_space(isl_set_get_space(set));
5107 aff = isl_aff_alloc(ls);
5108 if (aff) {
5109 isl_int_set_si(aff->v->el[0], 1);
5110 if (isl_int_is_one(hull->eq[i][o_out + d]))
5111 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5112 aff->v->size - 1);
5113 else
5114 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5115 aff->v->size - 1);
5116 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5118 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5119 set = isl_set_preimage_multi_aff(set, ma);
5121 ma = range_map(aff, d, n_in, n_out, is_set);
5123 if (is_set)
5124 map = set;
5125 else
5126 map = isl_set_unwrap(set);
5127 pma = isl_pw_multi_aff_from_map(map);
5129 if (!is_set) {
5130 space = isl_pw_multi_aff_get_domain_space(pma);
5131 space = isl_space_map_from_set(space);
5132 id = isl_pw_multi_aff_identity(space);
5133 pma = isl_pw_multi_aff_range_product(id, pma);
5135 id = isl_pw_multi_aff_from_multi_aff(ma);
5136 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5138 isl_basic_map_free(hull);
5139 return pma;
5140 error:
5141 isl_map_free(map);
5142 isl_basic_map_free(hull);
5143 return NULL;
5146 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5147 * "hull" contains the equalities valid for "map".
5149 * Check if any of the output dimensions is "strided".
5150 * That is, we check if it can be written as
5152 * x = m a + f(..)
5154 * with m greater than 1, a some combination of existentially quantified
5155 * variables and f an expression in the parameters and input dimensions.
5156 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5158 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5159 * special case.
5161 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5162 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5164 int i, j;
5165 unsigned n_out;
5166 unsigned o_out;
5167 unsigned n_div;
5168 unsigned o_div;
5169 isl_int gcd;
5171 n_div = isl_basic_map_dim(hull, isl_dim_div);
5172 o_div = isl_basic_map_offset(hull, isl_dim_div);
5174 if (n_div == 0) {
5175 isl_basic_map_free(hull);
5176 return pw_multi_aff_from_map_check_div(map);
5179 isl_int_init(gcd);
5181 n_out = isl_basic_map_dim(hull, isl_dim_out);
5182 o_out = isl_basic_map_offset(hull, isl_dim_out);
5184 for (i = 0; i < n_out; ++i) {
5185 for (j = 0; j < hull->n_eq; ++j) {
5186 isl_int *eq = hull->eq[j];
5187 isl_pw_multi_aff *res;
5189 if (!isl_int_is_one(eq[o_out + i]) &&
5190 !isl_int_is_negone(eq[o_out + i]))
5191 continue;
5192 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5193 continue;
5194 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5195 n_out - (i + 1)) != -1)
5196 continue;
5197 isl_seq_gcd(eq + o_div, n_div, &gcd);
5198 if (isl_int_is_zero(gcd))
5199 continue;
5200 if (isl_int_is_one(gcd))
5201 continue;
5203 res = pw_multi_aff_from_map_stride(map, hull,
5204 i, j, gcd);
5205 isl_int_clear(gcd);
5206 return res;
5210 isl_int_clear(gcd);
5211 isl_basic_map_free(hull);
5212 return pw_multi_aff_from_map_check_div(map);
5215 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5217 * As a special case, we first check if all output dimensions are uniquely
5218 * defined in terms of the parameters and input dimensions over the entire
5219 * domain. If so, we extract the desired isl_pw_multi_aff directly
5220 * from the affine hull of "map" and its domain.
5222 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5223 * special cases.
5225 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5227 isl_bool sv;
5228 isl_basic_map *hull;
5230 if (!map)
5231 return NULL;
5233 if (isl_map_n_basic_map(map) == 1) {
5234 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5235 hull = isl_basic_map_plain_affine_hull(hull);
5236 sv = isl_basic_map_plain_is_single_valued(hull);
5237 if (sv >= 0 && sv)
5238 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5239 hull);
5240 isl_basic_map_free(hull);
5242 map = isl_map_detect_equalities(map);
5243 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5244 sv = isl_basic_map_plain_is_single_valued(hull);
5245 if (sv >= 0 && sv)
5246 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5247 if (sv >= 0)
5248 return pw_multi_aff_from_map_check_strides(map, hull);
5249 isl_basic_map_free(hull);
5250 isl_map_free(map);
5251 return NULL;
5254 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5256 return isl_pw_multi_aff_from_map(set);
5259 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5260 * add it to *user.
5262 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5264 isl_union_pw_multi_aff **upma = user;
5265 isl_pw_multi_aff *pma;
5267 pma = isl_pw_multi_aff_from_map(map);
5268 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5270 return *upma ? isl_stat_ok : isl_stat_error;
5273 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5274 * domain.
5276 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5277 __isl_take isl_aff *aff)
5279 isl_multi_aff *ma;
5280 isl_pw_multi_aff *pma;
5282 ma = isl_multi_aff_from_aff(aff);
5283 pma = isl_pw_multi_aff_from_multi_aff(ma);
5284 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5287 /* Try and create an isl_union_pw_multi_aff that is equivalent
5288 * to the given isl_union_map.
5289 * The isl_union_map is required to be single-valued in each space.
5290 * Otherwise, an error is produced.
5292 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5293 __isl_take isl_union_map *umap)
5295 isl_space *space;
5296 isl_union_pw_multi_aff *upma;
5298 space = isl_union_map_get_space(umap);
5299 upma = isl_union_pw_multi_aff_empty(space);
5300 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5301 upma = isl_union_pw_multi_aff_free(upma);
5302 isl_union_map_free(umap);
5304 return upma;
5307 /* Try and create an isl_union_pw_multi_aff that is equivalent
5308 * to the given isl_union_set.
5309 * The isl_union_set is required to be a singleton in each space.
5310 * Otherwise, an error is produced.
5312 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5313 __isl_take isl_union_set *uset)
5315 return isl_union_pw_multi_aff_from_union_map(uset);
5318 /* Return the piecewise affine expression "set ? 1 : 0".
5320 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5322 isl_pw_aff *pa;
5323 isl_space *space = isl_set_get_space(set);
5324 isl_local_space *ls = isl_local_space_from_space(space);
5325 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5326 isl_aff *one = isl_aff_zero_on_domain(ls);
5328 one = isl_aff_add_constant_si(one, 1);
5329 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5330 set = isl_set_complement(set);
5331 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5333 return pa;
5336 /* Plug in "subs" for dimension "type", "pos" of "aff".
5338 * Let i be the dimension to replace and let "subs" be of the form
5340 * f/d
5342 * and "aff" of the form
5344 * (a i + g)/m
5346 * The result is
5348 * (a f + d g')/(m d)
5350 * where g' is the result of plugging in "subs" in each of the integer
5351 * divisions in g.
5353 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5354 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5356 isl_ctx *ctx;
5357 isl_int v;
5359 aff = isl_aff_cow(aff);
5360 if (!aff || !subs)
5361 return isl_aff_free(aff);
5363 ctx = isl_aff_get_ctx(aff);
5364 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5365 isl_die(ctx, isl_error_invalid,
5366 "spaces don't match", return isl_aff_free(aff));
5367 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5368 isl_die(ctx, isl_error_unsupported,
5369 "cannot handle divs yet", return isl_aff_free(aff));
5371 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5372 if (!aff->ls)
5373 return isl_aff_free(aff);
5375 aff->v = isl_vec_cow(aff->v);
5376 if (!aff->v)
5377 return isl_aff_free(aff);
5379 pos += isl_local_space_offset(aff->ls, type);
5381 isl_int_init(v);
5382 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5383 aff->v->size, subs->v->size, v);
5384 isl_int_clear(v);
5386 return aff;
5389 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5390 * expressions in "maff".
5392 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5393 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5394 __isl_keep isl_aff *subs)
5396 int i;
5398 maff = isl_multi_aff_cow(maff);
5399 if (!maff || !subs)
5400 return isl_multi_aff_free(maff);
5402 if (type == isl_dim_in)
5403 type = isl_dim_set;
5405 for (i = 0; i < maff->n; ++i) {
5406 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5407 type, pos, subs);
5408 if (!maff->u.p[i])
5409 return isl_multi_aff_free(maff);
5412 return maff;
5415 /* Plug in "subs" for dimension "type", "pos" of "pma".
5417 * pma is of the form
5419 * A_i(v) -> M_i(v)
5421 * while subs is of the form
5423 * v' = B_j(v) -> S_j
5425 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5426 * has a contribution in the result, in particular
5428 * C_ij(S_j) -> M_i(S_j)
5430 * Note that plugging in S_j in C_ij may also result in an empty set
5431 * and this contribution should simply be discarded.
5433 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5434 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5435 __isl_keep isl_pw_aff *subs)
5437 int i, j, n;
5438 isl_pw_multi_aff *res;
5440 if (!pma || !subs)
5441 return isl_pw_multi_aff_free(pma);
5443 n = pma->n * subs->n;
5444 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5446 for (i = 0; i < pma->n; ++i) {
5447 for (j = 0; j < subs->n; ++j) {
5448 isl_set *common;
5449 isl_multi_aff *res_ij;
5450 int empty;
5452 common = isl_set_intersect(
5453 isl_set_copy(pma->p[i].set),
5454 isl_set_copy(subs->p[j].set));
5455 common = isl_set_substitute(common,
5456 type, pos, subs->p[j].aff);
5457 empty = isl_set_plain_is_empty(common);
5458 if (empty < 0 || empty) {
5459 isl_set_free(common);
5460 if (empty < 0)
5461 goto error;
5462 continue;
5465 res_ij = isl_multi_aff_substitute(
5466 isl_multi_aff_copy(pma->p[i].maff),
5467 type, pos, subs->p[j].aff);
5469 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5473 isl_pw_multi_aff_free(pma);
5474 return res;
5475 error:
5476 isl_pw_multi_aff_free(pma);
5477 isl_pw_multi_aff_free(res);
5478 return NULL;
5481 /* Compute the preimage of a range of dimensions in the affine expression "src"
5482 * under "ma" and put the result in "dst". The number of dimensions in "src"
5483 * that precede the range is given by "n_before". The number of dimensions
5484 * in the range is given by the number of output dimensions of "ma".
5485 * The number of dimensions that follow the range is given by "n_after".
5486 * If "has_denom" is set (to one),
5487 * then "src" and "dst" have an extra initial denominator.
5488 * "n_div_ma" is the number of existentials in "ma"
5489 * "n_div_bset" is the number of existentials in "src"
5490 * The resulting "dst" (which is assumed to have been allocated by
5491 * the caller) contains coefficients for both sets of existentials,
5492 * first those in "ma" and then those in "src".
5493 * f, c1, c2 and g are temporary objects that have been initialized
5494 * by the caller.
5496 * Let src represent the expression
5498 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5500 * and let ma represent the expressions
5502 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5504 * We start out with the following expression for dst:
5506 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5508 * with the multiplication factor f initially equal to 1
5509 * and f \sum_i b_i v_i kept separately.
5510 * For each x_i that we substitute, we multiply the numerator
5511 * (and denominator) of dst by c_1 = m_i and add the numerator
5512 * of the x_i expression multiplied by c_2 = f b_i,
5513 * after removing the common factors of c_1 and c_2.
5514 * The multiplication factor f also needs to be multiplied by c_1
5515 * for the next x_j, j > i.
5517 void isl_seq_preimage(isl_int *dst, isl_int *src,
5518 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5519 int n_div_ma, int n_div_bmap,
5520 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5522 int i;
5523 int n_param, n_in, n_out;
5524 int o_dst, o_src;
5526 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5527 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5528 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5530 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5531 o_dst = o_src = has_denom + 1 + n_param + n_before;
5532 isl_seq_clr(dst + o_dst, n_in);
5533 o_dst += n_in;
5534 o_src += n_out;
5535 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5536 o_dst += n_after;
5537 o_src += n_after;
5538 isl_seq_clr(dst + o_dst, n_div_ma);
5539 o_dst += n_div_ma;
5540 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5542 isl_int_set_si(f, 1);
5544 for (i = 0; i < n_out; ++i) {
5545 int offset = has_denom + 1 + n_param + n_before + i;
5547 if (isl_int_is_zero(src[offset]))
5548 continue;
5549 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5550 isl_int_mul(c2, f, src[offset]);
5551 isl_int_gcd(g, c1, c2);
5552 isl_int_divexact(c1, c1, g);
5553 isl_int_divexact(c2, c2, g);
5555 isl_int_mul(f, f, c1);
5556 o_dst = has_denom;
5557 o_src = 1;
5558 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5559 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5560 o_dst += 1 + n_param;
5561 o_src += 1 + n_param;
5562 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5563 o_dst += n_before;
5564 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5565 c2, ma->u.p[i]->v->el + o_src, n_in);
5566 o_dst += n_in;
5567 o_src += n_in;
5568 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5569 o_dst += n_after;
5570 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5571 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5572 o_dst += n_div_ma;
5573 o_src += n_div_ma;
5574 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5575 if (has_denom)
5576 isl_int_mul(dst[0], dst[0], c1);
5580 /* Compute the pullback of "aff" by the function represented by "ma".
5581 * In other words, plug in "ma" in "aff". The result is an affine expression
5582 * defined over the domain space of "ma".
5584 * If "aff" is represented by
5586 * (a(p) + b x + c(divs))/d
5588 * and ma is represented by
5590 * x = D(p) + F(y) + G(divs')
5592 * then the result is
5594 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5596 * The divs in the local space of the input are similarly adjusted
5597 * through a call to isl_local_space_preimage_multi_aff.
5599 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5600 __isl_take isl_multi_aff *ma)
5602 isl_aff *res = NULL;
5603 isl_local_space *ls;
5604 int n_div_aff, n_div_ma;
5605 isl_int f, c1, c2, g;
5607 ma = isl_multi_aff_align_divs(ma);
5608 if (!aff || !ma)
5609 goto error;
5611 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5612 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5614 ls = isl_aff_get_domain_local_space(aff);
5615 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5616 res = isl_aff_alloc(ls);
5617 if (!res)
5618 goto error;
5620 isl_int_init(f);
5621 isl_int_init(c1);
5622 isl_int_init(c2);
5623 isl_int_init(g);
5625 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5626 f, c1, c2, g, 1);
5628 isl_int_clear(f);
5629 isl_int_clear(c1);
5630 isl_int_clear(c2);
5631 isl_int_clear(g);
5633 isl_aff_free(aff);
5634 isl_multi_aff_free(ma);
5635 res = isl_aff_normalize(res);
5636 return res;
5637 error:
5638 isl_aff_free(aff);
5639 isl_multi_aff_free(ma);
5640 isl_aff_free(res);
5641 return NULL;
5644 /* Compute the pullback of "aff1" by the function represented by "aff2".
5645 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5646 * defined over the domain space of "aff1".
5648 * The domain of "aff1" should match the range of "aff2", which means
5649 * that it should be single-dimensional.
5651 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5652 __isl_take isl_aff *aff2)
5654 isl_multi_aff *ma;
5656 ma = isl_multi_aff_from_aff(aff2);
5657 return isl_aff_pullback_multi_aff(aff1, ma);
5660 /* Compute the pullback of "ma1" by the function represented by "ma2".
5661 * In other words, plug in "ma2" in "ma1".
5663 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5665 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5666 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5668 int i;
5669 isl_space *space = NULL;
5671 ma2 = isl_multi_aff_align_divs(ma2);
5672 ma1 = isl_multi_aff_cow(ma1);
5673 if (!ma1 || !ma2)
5674 goto error;
5676 space = isl_space_join(isl_multi_aff_get_space(ma2),
5677 isl_multi_aff_get_space(ma1));
5679 for (i = 0; i < ma1->n; ++i) {
5680 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5681 isl_multi_aff_copy(ma2));
5682 if (!ma1->u.p[i])
5683 goto error;
5686 ma1 = isl_multi_aff_reset_space(ma1, space);
5687 isl_multi_aff_free(ma2);
5688 return ma1;
5689 error:
5690 isl_space_free(space);
5691 isl_multi_aff_free(ma2);
5692 isl_multi_aff_free(ma1);
5693 return NULL;
5696 /* Compute the pullback of "ma1" by the function represented by "ma2".
5697 * In other words, plug in "ma2" in "ma1".
5699 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5700 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5702 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5703 &isl_multi_aff_pullback_multi_aff_aligned);
5706 /* Extend the local space of "dst" to include the divs
5707 * in the local space of "src".
5709 * If "src" does not have any divs or if the local spaces of "dst" and
5710 * "src" are the same, then no extension is required.
5712 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5713 __isl_keep isl_aff *src)
5715 isl_ctx *ctx;
5716 int src_n_div, dst_n_div;
5717 int *exp1 = NULL;
5718 int *exp2 = NULL;
5719 isl_bool equal;
5720 isl_mat *div;
5722 if (!src || !dst)
5723 return isl_aff_free(dst);
5725 ctx = isl_aff_get_ctx(src);
5726 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5727 if (equal < 0)
5728 return isl_aff_free(dst);
5729 if (!equal)
5730 isl_die(ctx, isl_error_invalid,
5731 "spaces don't match", goto error);
5733 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5734 if (src_n_div == 0)
5735 return dst;
5736 equal = isl_local_space_is_equal(src->ls, dst->ls);
5737 if (equal < 0)
5738 return isl_aff_free(dst);
5739 if (equal)
5740 return dst;
5742 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5743 exp1 = isl_alloc_array(ctx, int, src_n_div);
5744 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5745 if (!exp1 || (dst_n_div && !exp2))
5746 goto error;
5748 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5749 dst = isl_aff_expand_divs(dst, div, exp2);
5750 free(exp1);
5751 free(exp2);
5753 return dst;
5754 error:
5755 free(exp1);
5756 free(exp2);
5757 return isl_aff_free(dst);
5760 /* Adjust the local spaces of the affine expressions in "maff"
5761 * such that they all have the save divs.
5763 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5764 __isl_take isl_multi_aff *maff)
5766 int i;
5768 if (!maff)
5769 return NULL;
5770 if (maff->n == 0)
5771 return maff;
5772 maff = isl_multi_aff_cow(maff);
5773 if (!maff)
5774 return NULL;
5776 for (i = 1; i < maff->n; ++i)
5777 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5778 for (i = 1; i < maff->n; ++i) {
5779 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5780 if (!maff->u.p[i])
5781 return isl_multi_aff_free(maff);
5784 return maff;
5787 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5789 aff = isl_aff_cow(aff);
5790 if (!aff)
5791 return NULL;
5793 aff->ls = isl_local_space_lift(aff->ls);
5794 if (!aff->ls)
5795 return isl_aff_free(aff);
5797 return aff;
5800 /* Lift "maff" to a space with extra dimensions such that the result
5801 * has no more existentially quantified variables.
5802 * If "ls" is not NULL, then *ls is assigned the local space that lies
5803 * at the basis of the lifting applied to "maff".
5805 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5806 __isl_give isl_local_space **ls)
5808 int i;
5809 isl_space *space;
5810 unsigned n_div;
5812 if (ls)
5813 *ls = NULL;
5815 if (!maff)
5816 return NULL;
5818 if (maff->n == 0) {
5819 if (ls) {
5820 isl_space *space = isl_multi_aff_get_domain_space(maff);
5821 *ls = isl_local_space_from_space(space);
5822 if (!*ls)
5823 return isl_multi_aff_free(maff);
5825 return maff;
5828 maff = isl_multi_aff_cow(maff);
5829 maff = isl_multi_aff_align_divs(maff);
5830 if (!maff)
5831 return NULL;
5833 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5834 space = isl_multi_aff_get_space(maff);
5835 space = isl_space_lift(isl_space_domain(space), n_div);
5836 space = isl_space_extend_domain_with_range(space,
5837 isl_multi_aff_get_space(maff));
5838 if (!space)
5839 return isl_multi_aff_free(maff);
5840 isl_space_free(maff->space);
5841 maff->space = space;
5843 if (ls) {
5844 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5845 if (!*ls)
5846 return isl_multi_aff_free(maff);
5849 for (i = 0; i < maff->n; ++i) {
5850 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5851 if (!maff->u.p[i])
5852 goto error;
5855 return maff;
5856 error:
5857 if (ls)
5858 isl_local_space_free(*ls);
5859 return isl_multi_aff_free(maff);
5863 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5865 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5866 __isl_keep isl_pw_multi_aff *pma, int pos)
5868 int i;
5869 int n_out;
5870 isl_space *space;
5871 isl_pw_aff *pa;
5873 if (!pma)
5874 return NULL;
5876 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5877 if (pos < 0 || pos >= n_out)
5878 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5879 "index out of bounds", return NULL);
5881 space = isl_pw_multi_aff_get_space(pma);
5882 space = isl_space_drop_dims(space, isl_dim_out,
5883 pos + 1, n_out - pos - 1);
5884 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5886 pa = isl_pw_aff_alloc_size(space, pma->n);
5887 for (i = 0; i < pma->n; ++i) {
5888 isl_aff *aff;
5889 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5890 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5893 return pa;
5896 /* Return an isl_pw_multi_aff with the given "set" as domain and
5897 * an unnamed zero-dimensional range.
5899 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5900 __isl_take isl_set *set)
5902 isl_multi_aff *ma;
5903 isl_space *space;
5905 space = isl_set_get_space(set);
5906 space = isl_space_from_domain(space);
5907 ma = isl_multi_aff_zero(space);
5908 return isl_pw_multi_aff_alloc(set, ma);
5911 /* Add an isl_pw_multi_aff with the given "set" as domain and
5912 * an unnamed zero-dimensional range to *user.
5914 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5915 void *user)
5917 isl_union_pw_multi_aff **upma = user;
5918 isl_pw_multi_aff *pma;
5920 pma = isl_pw_multi_aff_from_domain(set);
5921 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5923 return isl_stat_ok;
5926 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5927 * an unnamed zero-dimensional range.
5929 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5930 __isl_take isl_union_set *uset)
5932 isl_space *space;
5933 isl_union_pw_multi_aff *upma;
5935 if (!uset)
5936 return NULL;
5938 space = isl_union_set_get_space(uset);
5939 upma = isl_union_pw_multi_aff_empty(space);
5941 if (isl_union_set_foreach_set(uset,
5942 &add_pw_multi_aff_from_domain, &upma) < 0)
5943 goto error;
5945 isl_union_set_free(uset);
5946 return upma;
5947 error:
5948 isl_union_set_free(uset);
5949 isl_union_pw_multi_aff_free(upma);
5950 return NULL;
5953 /* Convert "pma" to an isl_map and add it to *umap.
5955 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5956 void *user)
5958 isl_union_map **umap = user;
5959 isl_map *map;
5961 map = isl_map_from_pw_multi_aff(pma);
5962 *umap = isl_union_map_add_map(*umap, map);
5964 return isl_stat_ok;
5967 /* Construct a union map mapping the domain of the union
5968 * piecewise multi-affine expression to its range, with each dimension
5969 * in the range equated to the corresponding affine expression on its cell.
5971 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5972 __isl_take isl_union_pw_multi_aff *upma)
5974 isl_space *space;
5975 isl_union_map *umap;
5977 if (!upma)
5978 return NULL;
5980 space = isl_union_pw_multi_aff_get_space(upma);
5981 umap = isl_union_map_empty(space);
5983 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5984 &map_from_pw_multi_aff, &umap) < 0)
5985 goto error;
5987 isl_union_pw_multi_aff_free(upma);
5988 return umap;
5989 error:
5990 isl_union_pw_multi_aff_free(upma);
5991 isl_union_map_free(umap);
5992 return NULL;
5995 /* Local data for bin_entry and the callback "fn".
5997 struct isl_union_pw_multi_aff_bin_data {
5998 isl_union_pw_multi_aff *upma2;
5999 isl_union_pw_multi_aff *res;
6000 isl_pw_multi_aff *pma;
6001 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6004 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6005 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6007 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6009 struct isl_union_pw_multi_aff_bin_data *data = user;
6010 isl_stat r;
6012 data->pma = pma;
6013 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6014 data->fn, data);
6015 isl_pw_multi_aff_free(pma);
6017 return r;
6020 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6021 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6022 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6023 * as *entry. The callback should adjust data->res if desired.
6025 static __isl_give isl_union_pw_multi_aff *bin_op(
6026 __isl_take isl_union_pw_multi_aff *upma1,
6027 __isl_take isl_union_pw_multi_aff *upma2,
6028 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6030 isl_space *space;
6031 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6033 space = isl_union_pw_multi_aff_get_space(upma2);
6034 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6035 space = isl_union_pw_multi_aff_get_space(upma1);
6036 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6038 if (!upma1 || !upma2)
6039 goto error;
6041 data.upma2 = upma2;
6042 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6043 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6044 &bin_entry, &data) < 0)
6045 goto error;
6047 isl_union_pw_multi_aff_free(upma1);
6048 isl_union_pw_multi_aff_free(upma2);
6049 return data.res;
6050 error:
6051 isl_union_pw_multi_aff_free(upma1);
6052 isl_union_pw_multi_aff_free(upma2);
6053 isl_union_pw_multi_aff_free(data.res);
6054 return NULL;
6057 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6058 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6060 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6061 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6063 isl_space *space;
6065 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6066 isl_pw_multi_aff_get_space(pma2));
6067 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6068 &isl_multi_aff_range_product);
6071 /* Given two isl_pw_multi_affs A -> B and C -> D,
6072 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6074 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6075 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6077 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6078 &pw_multi_aff_range_product);
6081 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6082 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6084 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6085 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6087 isl_space *space;
6089 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6090 isl_pw_multi_aff_get_space(pma2));
6091 space = isl_space_flatten_range(space);
6092 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6093 &isl_multi_aff_flat_range_product);
6096 /* Given two isl_pw_multi_affs A -> B and C -> D,
6097 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6099 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6100 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6102 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6103 &pw_multi_aff_flat_range_product);
6106 /* If data->pma and "pma2" have the same domain space, then compute
6107 * their flat range product and the result to data->res.
6109 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6110 void *user)
6112 struct isl_union_pw_multi_aff_bin_data *data = user;
6114 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6115 pma2->dim, isl_dim_in)) {
6116 isl_pw_multi_aff_free(pma2);
6117 return isl_stat_ok;
6120 pma2 = isl_pw_multi_aff_flat_range_product(
6121 isl_pw_multi_aff_copy(data->pma), pma2);
6123 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6125 return isl_stat_ok;
6128 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6129 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6131 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6132 __isl_take isl_union_pw_multi_aff *upma1,
6133 __isl_take isl_union_pw_multi_aff *upma2)
6135 return bin_op(upma1, upma2, &flat_range_product_entry);
6138 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6139 * The parameters are assumed to have been aligned.
6141 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6142 * except that it works on two different isl_pw_* types.
6144 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6145 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6146 __isl_take isl_pw_aff *pa)
6148 int i, j, n;
6149 isl_pw_multi_aff *res = NULL;
6151 if (!pma || !pa)
6152 goto error;
6154 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6155 pa->dim, isl_dim_in))
6156 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6157 "domains don't match", goto error);
6158 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6159 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6160 "index out of bounds", goto error);
6162 n = pma->n * pa->n;
6163 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6165 for (i = 0; i < pma->n; ++i) {
6166 for (j = 0; j < pa->n; ++j) {
6167 isl_set *common;
6168 isl_multi_aff *res_ij;
6169 int empty;
6171 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6172 isl_set_copy(pa->p[j].set));
6173 empty = isl_set_plain_is_empty(common);
6174 if (empty < 0 || empty) {
6175 isl_set_free(common);
6176 if (empty < 0)
6177 goto error;
6178 continue;
6181 res_ij = isl_multi_aff_set_aff(
6182 isl_multi_aff_copy(pma->p[i].maff), pos,
6183 isl_aff_copy(pa->p[j].aff));
6184 res_ij = isl_multi_aff_gist(res_ij,
6185 isl_set_copy(common));
6187 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6191 isl_pw_multi_aff_free(pma);
6192 isl_pw_aff_free(pa);
6193 return res;
6194 error:
6195 isl_pw_multi_aff_free(pma);
6196 isl_pw_aff_free(pa);
6197 return isl_pw_multi_aff_free(res);
6200 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6202 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6203 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6204 __isl_take isl_pw_aff *pa)
6206 isl_bool equal_params;
6208 if (!pma || !pa)
6209 goto error;
6210 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6211 if (equal_params < 0)
6212 goto error;
6213 if (equal_params)
6214 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6215 if (!isl_space_has_named_params(pma->dim) ||
6216 !isl_space_has_named_params(pa->dim))
6217 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6218 "unaligned unnamed parameters", goto error);
6219 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6220 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6221 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6222 error:
6223 isl_pw_multi_aff_free(pma);
6224 isl_pw_aff_free(pa);
6225 return NULL;
6228 /* Do the parameters of "pa" match those of "space"?
6230 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6231 __isl_keep isl_space *space)
6233 isl_space *pa_space;
6234 isl_bool match;
6236 if (!pa || !space)
6237 return isl_bool_error;
6239 pa_space = isl_pw_aff_get_space(pa);
6241 match = isl_space_has_equal_params(space, pa_space);
6243 isl_space_free(pa_space);
6244 return match;
6247 /* Check that the domain space of "pa" matches "space".
6249 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6250 __isl_keep isl_space *space)
6252 isl_space *pa_space;
6253 isl_bool match;
6255 if (!pa || !space)
6256 return isl_stat_error;
6258 pa_space = isl_pw_aff_get_space(pa);
6260 match = isl_space_has_equal_params(space, pa_space);
6261 if (match < 0)
6262 goto error;
6263 if (!match)
6264 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6265 "parameters don't match", goto error);
6266 match = isl_space_tuple_is_equal(space, isl_dim_in,
6267 pa_space, isl_dim_in);
6268 if (match < 0)
6269 goto error;
6270 if (!match)
6271 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6272 "domains don't match", goto error);
6273 isl_space_free(pa_space);
6274 return isl_stat_ok;
6275 error:
6276 isl_space_free(pa_space);
6277 return isl_stat_error;
6280 #undef BASE
6281 #define BASE pw_aff
6282 #undef DOMBASE
6283 #define DOMBASE set
6285 #include <isl_multi_explicit_domain.c>
6286 #include <isl_multi_pw_aff_explicit_domain.c>
6287 #include <isl_multi_templ.c>
6288 #include <isl_multi_apply_set.c>
6289 #include <isl_multi_coalesce.c>
6290 #include <isl_multi_dims.c>
6291 #include <isl_multi_gist.c>
6292 #include <isl_multi_hash.c>
6293 #include <isl_multi_align_set.c>
6294 #include <isl_multi_intersect.c>
6296 /* Does "mpa" have a non-trivial explicit domain?
6298 * The explicit domain, if present, is trivial if it represents
6299 * an (obviously) universe set.
6301 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6302 __isl_keep isl_multi_pw_aff *mpa)
6304 if (!mpa)
6305 return isl_bool_error;
6306 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6307 return isl_bool_false;
6308 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6311 /* Scale the elements of "pma" by the corresponding elements of "mv".
6313 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6314 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6316 int i;
6317 isl_bool equal_params;
6319 pma = isl_pw_multi_aff_cow(pma);
6320 if (!pma || !mv)
6321 goto error;
6322 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6323 mv->space, isl_dim_set))
6324 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6325 "spaces don't match", goto error);
6326 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6327 if (equal_params < 0)
6328 goto error;
6329 if (!equal_params) {
6330 pma = isl_pw_multi_aff_align_params(pma,
6331 isl_multi_val_get_space(mv));
6332 mv = isl_multi_val_align_params(mv,
6333 isl_pw_multi_aff_get_space(pma));
6334 if (!pma || !mv)
6335 goto error;
6338 for (i = 0; i < pma->n; ++i) {
6339 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6340 isl_multi_val_copy(mv));
6341 if (!pma->p[i].maff)
6342 goto error;
6345 isl_multi_val_free(mv);
6346 return pma;
6347 error:
6348 isl_multi_val_free(mv);
6349 isl_pw_multi_aff_free(pma);
6350 return NULL;
6353 /* This function is called for each entry of an isl_union_pw_multi_aff.
6354 * If the space of the entry matches that of data->mv,
6355 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6356 * Otherwise, return an empty isl_pw_multi_aff.
6358 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6359 __isl_take isl_pw_multi_aff *pma, void *user)
6361 isl_multi_val *mv = user;
6363 if (!pma)
6364 return NULL;
6365 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6366 mv->space, isl_dim_set)) {
6367 isl_space *space = isl_pw_multi_aff_get_space(pma);
6368 isl_pw_multi_aff_free(pma);
6369 return isl_pw_multi_aff_empty(space);
6372 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6375 /* Scale the elements of "upma" by the corresponding elements of "mv",
6376 * for those entries that match the space of "mv".
6378 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6379 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6381 upma = isl_union_pw_multi_aff_align_params(upma,
6382 isl_multi_val_get_space(mv));
6383 mv = isl_multi_val_align_params(mv,
6384 isl_union_pw_multi_aff_get_space(upma));
6385 if (!upma || !mv)
6386 goto error;
6388 return isl_union_pw_multi_aff_transform(upma,
6389 &union_pw_multi_aff_scale_multi_val_entry, mv);
6391 isl_multi_val_free(mv);
6392 return upma;
6393 error:
6394 isl_multi_val_free(mv);
6395 isl_union_pw_multi_aff_free(upma);
6396 return NULL;
6399 /* Construct and return a piecewise multi affine expression
6400 * in the given space with value zero in each of the output dimensions and
6401 * a universe domain.
6403 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6405 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6408 /* Construct and return a piecewise multi affine expression
6409 * that is equal to the given piecewise affine expression.
6411 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6412 __isl_take isl_pw_aff *pa)
6414 int i;
6415 isl_space *space;
6416 isl_pw_multi_aff *pma;
6418 if (!pa)
6419 return NULL;
6421 space = isl_pw_aff_get_space(pa);
6422 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6424 for (i = 0; i < pa->n; ++i) {
6425 isl_set *set;
6426 isl_multi_aff *ma;
6428 set = isl_set_copy(pa->p[i].set);
6429 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6430 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6433 isl_pw_aff_free(pa);
6434 return pma;
6437 /* Construct a set or map mapping the shared (parameter) domain
6438 * of the piecewise affine expressions to the range of "mpa"
6439 * with each dimension in the range equated to the
6440 * corresponding piecewise affine expression.
6442 static __isl_give isl_map *map_from_multi_pw_aff(
6443 __isl_take isl_multi_pw_aff *mpa)
6445 int i;
6446 isl_space *space;
6447 isl_map *map;
6449 if (!mpa)
6450 return NULL;
6452 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6453 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6454 "invalid space", goto error);
6456 space = isl_multi_pw_aff_get_domain_space(mpa);
6457 map = isl_map_universe(isl_space_from_domain(space));
6459 for (i = 0; i < mpa->n; ++i) {
6460 isl_pw_aff *pa;
6461 isl_map *map_i;
6463 pa = isl_pw_aff_copy(mpa->u.p[i]);
6464 map_i = map_from_pw_aff(pa);
6466 map = isl_map_flat_range_product(map, map_i);
6469 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6471 isl_multi_pw_aff_free(mpa);
6472 return map;
6473 error:
6474 isl_multi_pw_aff_free(mpa);
6475 return NULL;
6478 /* Construct a map mapping the shared domain
6479 * of the piecewise affine expressions to the range of "mpa"
6480 * with each dimension in the range equated to the
6481 * corresponding piecewise affine expression.
6483 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6485 if (!mpa)
6486 return NULL;
6487 if (isl_space_is_set(mpa->space))
6488 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6489 "space of input is not a map", goto error);
6491 return map_from_multi_pw_aff(mpa);
6492 error:
6493 isl_multi_pw_aff_free(mpa);
6494 return NULL;
6497 /* Construct a set mapping the shared parameter domain
6498 * of the piecewise affine expressions to the space of "mpa"
6499 * with each dimension in the range equated to the
6500 * corresponding piecewise affine expression.
6502 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6504 if (!mpa)
6505 return NULL;
6506 if (!isl_space_is_set(mpa->space))
6507 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6508 "space of input is not a set", goto error);
6510 return map_from_multi_pw_aff(mpa);
6511 error:
6512 isl_multi_pw_aff_free(mpa);
6513 return NULL;
6516 /* Construct and return a piecewise multi affine expression
6517 * that is equal to the given multi piecewise affine expression
6518 * on the shared domain of the piecewise affine expressions,
6519 * in the special case of a 0D multi piecewise affine expression.
6521 * Create a piecewise multi affine expression with the explicit domain of
6522 * the 0D multi piecewise affine expression as domain.
6524 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6525 __isl_take isl_multi_pw_aff *mpa)
6527 isl_space *space;
6528 isl_set *dom;
6529 isl_multi_aff *ma;
6531 space = isl_multi_pw_aff_get_space(mpa);
6532 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6533 isl_multi_pw_aff_free(mpa);
6535 ma = isl_multi_aff_zero(space);
6536 return isl_pw_multi_aff_alloc(dom, ma);
6539 /* Construct and return a piecewise multi affine expression
6540 * that is equal to the given multi piecewise affine expression
6541 * on the shared domain of the piecewise affine expressions.
6543 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6544 __isl_take isl_multi_pw_aff *mpa)
6546 int i;
6547 isl_space *space;
6548 isl_pw_aff *pa;
6549 isl_pw_multi_aff *pma;
6551 if (!mpa)
6552 return NULL;
6554 if (mpa->n == 0)
6555 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6557 space = isl_multi_pw_aff_get_space(mpa);
6558 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6559 pma = isl_pw_multi_aff_from_pw_aff(pa);
6561 for (i = 1; i < mpa->n; ++i) {
6562 isl_pw_multi_aff *pma_i;
6564 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6565 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6566 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6569 pma = isl_pw_multi_aff_reset_space(pma, space);
6571 isl_multi_pw_aff_free(mpa);
6572 return pma;
6575 /* Construct and return a multi piecewise affine expression
6576 * that is equal to the given multi affine expression.
6578 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6579 __isl_take isl_multi_aff *ma)
6581 int i, n;
6582 isl_multi_pw_aff *mpa;
6584 if (!ma)
6585 return NULL;
6587 n = isl_multi_aff_dim(ma, isl_dim_out);
6588 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6590 for (i = 0; i < n; ++i) {
6591 isl_pw_aff *pa;
6593 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6594 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6597 isl_multi_aff_free(ma);
6598 return mpa;
6601 /* Construct and return a multi piecewise affine expression
6602 * that is equal to the given piecewise multi affine expression.
6604 * If the resulting multi piecewise affine expression has
6605 * an explicit domain, then assign it the domain of the input.
6606 * In other cases, the domain is stored in the individual elements.
6608 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6609 __isl_take isl_pw_multi_aff *pma)
6611 int i, n;
6612 isl_space *space;
6613 isl_multi_pw_aff *mpa;
6615 if (!pma)
6616 return NULL;
6618 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6619 space = isl_pw_multi_aff_get_space(pma);
6620 mpa = isl_multi_pw_aff_alloc(space);
6622 for (i = 0; i < n; ++i) {
6623 isl_pw_aff *pa;
6625 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6626 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6628 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6629 isl_set *dom;
6631 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6632 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6635 isl_pw_multi_aff_free(pma);
6636 return mpa;
6639 /* Do "pa1" and "pa2" represent the same function?
6641 * We first check if they are obviously equal.
6642 * If not, we convert them to maps and check if those are equal.
6644 * If "pa1" or "pa2" contain any NaNs, then they are considered
6645 * not to be the same. A NaN is not equal to anything, not even
6646 * to another NaN.
6648 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6649 __isl_keep isl_pw_aff *pa2)
6651 isl_bool equal;
6652 isl_bool has_nan;
6653 isl_map *map1, *map2;
6655 if (!pa1 || !pa2)
6656 return isl_bool_error;
6658 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6659 if (equal < 0 || equal)
6660 return equal;
6661 has_nan = either_involves_nan(pa1, pa2);
6662 if (has_nan < 0)
6663 return isl_bool_error;
6664 if (has_nan)
6665 return isl_bool_false;
6667 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6668 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6669 equal = isl_map_is_equal(map1, map2);
6670 isl_map_free(map1);
6671 isl_map_free(map2);
6673 return equal;
6676 /* Do "mpa1" and "mpa2" represent the same function?
6678 * Note that we cannot convert the entire isl_multi_pw_aff
6679 * to a map because the domains of the piecewise affine expressions
6680 * may not be the same.
6682 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6683 __isl_keep isl_multi_pw_aff *mpa2)
6685 int i;
6686 isl_bool equal, equal_params;
6688 if (!mpa1 || !mpa2)
6689 return isl_bool_error;
6691 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6692 if (equal_params < 0)
6693 return isl_bool_error;
6694 if (!equal_params) {
6695 if (!isl_space_has_named_params(mpa1->space))
6696 return isl_bool_false;
6697 if (!isl_space_has_named_params(mpa2->space))
6698 return isl_bool_false;
6699 mpa1 = isl_multi_pw_aff_copy(mpa1);
6700 mpa2 = isl_multi_pw_aff_copy(mpa2);
6701 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6702 isl_multi_pw_aff_get_space(mpa2));
6703 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6704 isl_multi_pw_aff_get_space(mpa1));
6705 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6706 isl_multi_pw_aff_free(mpa1);
6707 isl_multi_pw_aff_free(mpa2);
6708 return equal;
6711 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6712 if (equal < 0 || !equal)
6713 return equal;
6715 for (i = 0; i < mpa1->n; ++i) {
6716 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6717 if (equal < 0 || !equal)
6718 return equal;
6721 return isl_bool_true;
6724 /* Do "pma1" and "pma2" represent the same function?
6726 * First check if they are obviously equal.
6727 * If not, then convert them to maps and check if those are equal.
6729 * If "pa1" or "pa2" contain any NaNs, then they are considered
6730 * not to be the same. A NaN is not equal to anything, not even
6731 * to another NaN.
6733 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6734 __isl_keep isl_pw_multi_aff *pma2)
6736 isl_bool equal;
6737 isl_bool has_nan;
6738 isl_map *map1, *map2;
6740 if (!pma1 || !pma2)
6741 return isl_bool_error;
6743 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6744 if (equal < 0 || equal)
6745 return equal;
6746 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6747 if (has_nan >= 0 && !has_nan)
6748 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6749 if (has_nan < 0 || has_nan)
6750 return isl_bool_not(has_nan);
6752 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6753 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6754 equal = isl_map_is_equal(map1, map2);
6755 isl_map_free(map1);
6756 isl_map_free(map2);
6758 return equal;
6761 /* Compute the pullback of "mpa" by the function represented by "ma".
6762 * In other words, plug in "ma" in "mpa".
6764 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6766 * If "mpa" has an explicit domain, then it is this domain
6767 * that needs to undergo a pullback, i.e., a preimage.
6769 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6770 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6772 int i;
6773 isl_space *space = NULL;
6775 mpa = isl_multi_pw_aff_cow(mpa);
6776 if (!mpa || !ma)
6777 goto error;
6779 space = isl_space_join(isl_multi_aff_get_space(ma),
6780 isl_multi_pw_aff_get_space(mpa));
6781 if (!space)
6782 goto error;
6784 for (i = 0; i < mpa->n; ++i) {
6785 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6786 isl_multi_aff_copy(ma));
6787 if (!mpa->u.p[i])
6788 goto error;
6790 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6791 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6792 isl_multi_aff_copy(ma));
6793 if (!mpa->u.dom)
6794 goto error;
6797 isl_multi_aff_free(ma);
6798 isl_space_free(mpa->space);
6799 mpa->space = space;
6800 return mpa;
6801 error:
6802 isl_space_free(space);
6803 isl_multi_pw_aff_free(mpa);
6804 isl_multi_aff_free(ma);
6805 return NULL;
6808 /* Compute the pullback of "mpa" by the function represented by "ma".
6809 * In other words, plug in "ma" in "mpa".
6811 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6812 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6814 isl_bool equal_params;
6816 if (!mpa || !ma)
6817 goto error;
6818 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6819 if (equal_params < 0)
6820 goto error;
6821 if (equal_params)
6822 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6823 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6824 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6825 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6826 error:
6827 isl_multi_pw_aff_free(mpa);
6828 isl_multi_aff_free(ma);
6829 return NULL;
6832 /* Compute the pullback of "mpa" by the function represented by "pma".
6833 * In other words, plug in "pma" in "mpa".
6835 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6837 * If "mpa" has an explicit domain, then it is this domain
6838 * that needs to undergo a pullback, i.e., a preimage.
6840 static __isl_give isl_multi_pw_aff *
6841 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6842 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6844 int i;
6845 isl_space *space = NULL;
6847 mpa = isl_multi_pw_aff_cow(mpa);
6848 if (!mpa || !pma)
6849 goto error;
6851 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6852 isl_multi_pw_aff_get_space(mpa));
6854 for (i = 0; i < mpa->n; ++i) {
6855 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6856 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6857 if (!mpa->u.p[i])
6858 goto error;
6860 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6861 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6862 isl_pw_multi_aff_copy(pma));
6863 if (!mpa->u.dom)
6864 goto error;
6867 isl_pw_multi_aff_free(pma);
6868 isl_space_free(mpa->space);
6869 mpa->space = space;
6870 return mpa;
6871 error:
6872 isl_space_free(space);
6873 isl_multi_pw_aff_free(mpa);
6874 isl_pw_multi_aff_free(pma);
6875 return NULL;
6878 /* Compute the pullback of "mpa" by the function represented by "pma".
6879 * In other words, plug in "pma" in "mpa".
6881 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6882 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6884 isl_bool equal_params;
6886 if (!mpa || !pma)
6887 goto error;
6888 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6889 if (equal_params < 0)
6890 goto error;
6891 if (equal_params)
6892 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6893 mpa = isl_multi_pw_aff_align_params(mpa,
6894 isl_pw_multi_aff_get_space(pma));
6895 pma = isl_pw_multi_aff_align_params(pma,
6896 isl_multi_pw_aff_get_space(mpa));
6897 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6898 error:
6899 isl_multi_pw_aff_free(mpa);
6900 isl_pw_multi_aff_free(pma);
6901 return NULL;
6904 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6905 * with the domain of "aff". The domain of the result is the same
6906 * as that of "mpa".
6907 * "mpa" and "aff" are assumed to have been aligned.
6909 * We first extract the parametric constant from "aff", defined
6910 * over the correct domain.
6911 * Then we add the appropriate combinations of the members of "mpa".
6912 * Finally, we add the integer divisions through recursive calls.
6914 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6915 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6917 int i, n_in, n_div;
6918 isl_space *space;
6919 isl_val *v;
6920 isl_pw_aff *pa;
6921 isl_aff *tmp;
6923 n_in = isl_aff_dim(aff, isl_dim_in);
6924 n_div = isl_aff_dim(aff, isl_dim_div);
6926 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6927 tmp = isl_aff_copy(aff);
6928 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6929 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6930 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6931 isl_space_dim(space, isl_dim_set));
6932 tmp = isl_aff_reset_domain_space(tmp, space);
6933 pa = isl_pw_aff_from_aff(tmp);
6935 for (i = 0; i < n_in; ++i) {
6936 isl_pw_aff *pa_i;
6938 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6939 continue;
6940 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6941 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6942 pa_i = isl_pw_aff_scale_val(pa_i, v);
6943 pa = isl_pw_aff_add(pa, pa_i);
6946 for (i = 0; i < n_div; ++i) {
6947 isl_aff *div;
6948 isl_pw_aff *pa_i;
6950 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6951 continue;
6952 div = isl_aff_get_div(aff, i);
6953 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6954 isl_multi_pw_aff_copy(mpa), div);
6955 pa_i = isl_pw_aff_floor(pa_i);
6956 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6957 pa_i = isl_pw_aff_scale_val(pa_i, v);
6958 pa = isl_pw_aff_add(pa, pa_i);
6961 isl_multi_pw_aff_free(mpa);
6962 isl_aff_free(aff);
6964 return pa;
6967 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6968 * with the domain of "aff". The domain of the result is the same
6969 * as that of "mpa".
6971 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6972 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6974 isl_bool equal_params;
6976 if (!aff || !mpa)
6977 goto error;
6978 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6979 if (equal_params < 0)
6980 goto error;
6981 if (equal_params)
6982 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6984 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6985 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6987 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6988 error:
6989 isl_aff_free(aff);
6990 isl_multi_pw_aff_free(mpa);
6991 return NULL;
6994 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6995 * with the domain of "pa". The domain of the result is the same
6996 * as that of "mpa".
6997 * "mpa" and "pa" are assumed to have been aligned.
6999 * We consider each piece in turn. Note that the domains of the
7000 * pieces are assumed to be disjoint and they remain disjoint
7001 * after taking the preimage (over the same function).
7003 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7004 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7006 isl_space *space;
7007 isl_pw_aff *res;
7008 int i;
7010 if (!mpa || !pa)
7011 goto error;
7013 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7014 isl_pw_aff_get_space(pa));
7015 res = isl_pw_aff_empty(space);
7017 for (i = 0; i < pa->n; ++i) {
7018 isl_pw_aff *pa_i;
7019 isl_set *domain;
7021 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7022 isl_multi_pw_aff_copy(mpa),
7023 isl_aff_copy(pa->p[i].aff));
7024 domain = isl_set_copy(pa->p[i].set);
7025 domain = isl_set_preimage_multi_pw_aff(domain,
7026 isl_multi_pw_aff_copy(mpa));
7027 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7028 res = isl_pw_aff_add_disjoint(res, pa_i);
7031 isl_pw_aff_free(pa);
7032 isl_multi_pw_aff_free(mpa);
7033 return res;
7034 error:
7035 isl_pw_aff_free(pa);
7036 isl_multi_pw_aff_free(mpa);
7037 return NULL;
7040 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7041 * with the domain of "pa". The domain of the result is the same
7042 * as that of "mpa".
7044 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7045 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7047 isl_bool equal_params;
7049 if (!pa || !mpa)
7050 goto error;
7051 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7052 if (equal_params < 0)
7053 goto error;
7054 if (equal_params)
7055 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7057 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7058 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7060 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7061 error:
7062 isl_pw_aff_free(pa);
7063 isl_multi_pw_aff_free(mpa);
7064 return NULL;
7067 /* Compute the pullback of "pa" by the function represented by "mpa".
7068 * In other words, plug in "mpa" in "pa".
7069 * "pa" and "mpa" are assumed to have been aligned.
7071 * The pullback is computed by applying "pa" to "mpa".
7073 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7074 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7076 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7079 /* Compute the pullback of "pa" by the function represented by "mpa".
7080 * In other words, plug in "mpa" in "pa".
7082 * The pullback is computed by applying "pa" to "mpa".
7084 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7085 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7087 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7090 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7091 * In other words, plug in "mpa2" in "mpa1".
7093 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7095 * We pullback each member of "mpa1" in turn.
7097 * If "mpa1" has an explicit domain, then it is this domain
7098 * that needs to undergo a pullback instead, i.e., a preimage.
7100 static __isl_give isl_multi_pw_aff *
7101 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7102 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7104 int i;
7105 isl_space *space = NULL;
7107 mpa1 = isl_multi_pw_aff_cow(mpa1);
7108 if (!mpa1 || !mpa2)
7109 goto error;
7111 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7112 isl_multi_pw_aff_get_space(mpa1));
7114 for (i = 0; i < mpa1->n; ++i) {
7115 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7116 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7117 if (!mpa1->u.p[i])
7118 goto error;
7121 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7122 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7123 isl_multi_pw_aff_copy(mpa2));
7124 if (!mpa1->u.dom)
7125 goto error;
7127 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7129 isl_multi_pw_aff_free(mpa2);
7130 return mpa1;
7131 error:
7132 isl_space_free(space);
7133 isl_multi_pw_aff_free(mpa1);
7134 isl_multi_pw_aff_free(mpa2);
7135 return NULL;
7138 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7139 * In other words, plug in "mpa2" in "mpa1".
7141 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7142 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7144 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7145 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7148 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7149 * of "mpa1" and "mpa2" live in the same space, construct map space
7150 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7151 * with this map space as extract argument.
7153 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7154 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7155 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7156 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7158 int match;
7159 isl_space *space1, *space2;
7160 isl_map *res;
7162 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7163 isl_multi_pw_aff_get_space(mpa2));
7164 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7165 isl_multi_pw_aff_get_space(mpa1));
7166 if (!mpa1 || !mpa2)
7167 goto error;
7168 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7169 mpa2->space, isl_dim_out);
7170 if (match < 0)
7171 goto error;
7172 if (!match)
7173 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7174 "range spaces don't match", goto error);
7175 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7176 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7177 space1 = isl_space_map_from_domain_and_range(space1, space2);
7179 res = order(mpa1, mpa2, space1);
7180 isl_multi_pw_aff_free(mpa1);
7181 isl_multi_pw_aff_free(mpa2);
7182 return res;
7183 error:
7184 isl_multi_pw_aff_free(mpa1);
7185 isl_multi_pw_aff_free(mpa2);
7186 return NULL;
7189 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7190 * where the function values are equal. "space" is the space of the result.
7191 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7193 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7194 * in the sequences are equal.
7196 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7197 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7198 __isl_take isl_space *space)
7200 int i, n;
7201 isl_map *res;
7203 res = isl_map_universe(space);
7205 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7206 for (i = 0; i < n; ++i) {
7207 isl_pw_aff *pa1, *pa2;
7208 isl_map *map;
7210 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7211 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7212 map = isl_pw_aff_eq_map(pa1, pa2);
7213 res = isl_map_intersect(res, map);
7216 return res;
7219 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7220 * where the function values are equal.
7222 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7223 __isl_take isl_multi_pw_aff *mpa2)
7225 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7226 &isl_multi_pw_aff_eq_map_on_space);
7229 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7230 * where the function values of "mpa1" is lexicographically satisfies "base"
7231 * compared to that of "mpa2". "space" is the space of the result.
7232 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7234 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7235 * if its i-th element satisfies "base" when compared to
7236 * the i-th element of "mpa2" while all previous elements are
7237 * pairwise equal.
7239 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7240 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7241 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7242 __isl_take isl_pw_aff *pa2),
7243 __isl_take isl_space *space)
7245 int i, n;
7246 isl_map *res, *rest;
7248 res = isl_map_empty(isl_space_copy(space));
7249 rest = isl_map_universe(space);
7251 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7252 for (i = 0; i < n; ++i) {
7253 isl_pw_aff *pa1, *pa2;
7254 isl_map *map;
7256 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7257 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7258 map = base(pa1, pa2);
7259 map = isl_map_intersect(map, isl_map_copy(rest));
7260 res = isl_map_union(res, map);
7262 if (i == n - 1)
7263 continue;
7265 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7266 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7267 map = isl_pw_aff_eq_map(pa1, pa2);
7268 rest = isl_map_intersect(rest, map);
7271 isl_map_free(rest);
7272 return res;
7275 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7276 * where the function value of "mpa1" is lexicographically less than that
7277 * of "mpa2". "space" is the space of the result.
7278 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7280 * "mpa1" is less than "mpa2" if its i-th element is smaller
7281 * than the i-th element of "mpa2" while all previous elements are
7282 * pairwise equal.
7284 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7285 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7286 __isl_take isl_space *space)
7288 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7289 &isl_pw_aff_lt_map, space);
7292 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7293 * where the function value of "mpa1" is lexicographically less than that
7294 * of "mpa2".
7296 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7297 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7299 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7300 &isl_multi_pw_aff_lex_lt_map_on_space);
7303 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7304 * where the function value of "mpa1" is lexicographically greater than that
7305 * of "mpa2". "space" is the space of the result.
7306 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7308 * "mpa1" is greater than "mpa2" if its i-th element is greater
7309 * than the i-th element of "mpa2" while all previous elements are
7310 * pairwise equal.
7312 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7313 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7314 __isl_take isl_space *space)
7316 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7317 &isl_pw_aff_gt_map, space);
7320 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7321 * where the function value of "mpa1" is lexicographically greater than that
7322 * of "mpa2".
7324 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7325 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7327 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7328 &isl_multi_pw_aff_lex_gt_map_on_space);
7331 /* Compare two isl_affs.
7333 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7334 * than "aff2" and 0 if they are equal.
7336 * The order is fairly arbitrary. We do consider expressions that only involve
7337 * earlier dimensions as "smaller".
7339 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7341 int cmp;
7342 int last1, last2;
7344 if (aff1 == aff2)
7345 return 0;
7347 if (!aff1)
7348 return -1;
7349 if (!aff2)
7350 return 1;
7352 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7353 if (cmp != 0)
7354 return cmp;
7356 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7357 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7358 if (last1 != last2)
7359 return last1 - last2;
7361 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7364 /* Compare two isl_pw_affs.
7366 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7367 * than "pa2" and 0 if they are equal.
7369 * The order is fairly arbitrary. We do consider expressions that only involve
7370 * earlier dimensions as "smaller".
7372 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7373 __isl_keep isl_pw_aff *pa2)
7375 int i;
7376 int cmp;
7378 if (pa1 == pa2)
7379 return 0;
7381 if (!pa1)
7382 return -1;
7383 if (!pa2)
7384 return 1;
7386 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7387 if (cmp != 0)
7388 return cmp;
7390 if (pa1->n != pa2->n)
7391 return pa1->n - pa2->n;
7393 for (i = 0; i < pa1->n; ++i) {
7394 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7395 if (cmp != 0)
7396 return cmp;
7397 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7398 if (cmp != 0)
7399 return cmp;
7402 return 0;
7405 /* Return a piecewise affine expression that is equal to "v" on "domain".
7407 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7408 __isl_take isl_val *v)
7410 isl_space *space;
7411 isl_local_space *ls;
7412 isl_aff *aff;
7414 space = isl_set_get_space(domain);
7415 ls = isl_local_space_from_space(space);
7416 aff = isl_aff_val_on_domain(ls, v);
7418 return isl_pw_aff_alloc(domain, aff);
7421 /* Return a multi affine expression that is equal to "mv" on domain
7422 * space "space".
7424 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7425 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7427 int i, n;
7428 isl_space *space2;
7429 isl_local_space *ls;
7430 isl_multi_aff *ma;
7432 if (!space || !mv)
7433 goto error;
7435 n = isl_multi_val_dim(mv, isl_dim_set);
7436 space2 = isl_multi_val_get_space(mv);
7437 space2 = isl_space_align_params(space2, isl_space_copy(space));
7438 space = isl_space_align_params(space, isl_space_copy(space2));
7439 space = isl_space_map_from_domain_and_range(space, space2);
7440 ma = isl_multi_aff_alloc(isl_space_copy(space));
7441 ls = isl_local_space_from_space(isl_space_domain(space));
7442 for (i = 0; i < n; ++i) {
7443 isl_val *v;
7444 isl_aff *aff;
7446 v = isl_multi_val_get_val(mv, i);
7447 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7448 ma = isl_multi_aff_set_aff(ma, i, aff);
7450 isl_local_space_free(ls);
7452 isl_multi_val_free(mv);
7453 return ma;
7454 error:
7455 isl_space_free(space);
7456 isl_multi_val_free(mv);
7457 return NULL;
7460 /* Return a piecewise multi-affine expression
7461 * that is equal to "mv" on "domain".
7463 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7464 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7466 isl_space *space;
7467 isl_multi_aff *ma;
7469 space = isl_set_get_space(domain);
7470 ma = isl_multi_aff_multi_val_on_space(space, mv);
7472 return isl_pw_multi_aff_alloc(domain, ma);
7475 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7476 * mv is the value that should be attained on each domain set
7477 * res collects the results
7479 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7480 isl_multi_val *mv;
7481 isl_union_pw_multi_aff *res;
7484 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7485 * and add it to data->res.
7487 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7488 void *user)
7490 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7491 isl_pw_multi_aff *pma;
7492 isl_multi_val *mv;
7494 mv = isl_multi_val_copy(data->mv);
7495 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7496 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7498 return data->res ? isl_stat_ok : isl_stat_error;
7501 /* Return a union piecewise multi-affine expression
7502 * that is equal to "mv" on "domain".
7504 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7505 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7507 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7508 isl_space *space;
7510 space = isl_union_set_get_space(domain);
7511 data.res = isl_union_pw_multi_aff_empty(space);
7512 data.mv = mv;
7513 if (isl_union_set_foreach_set(domain,
7514 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7515 data.res = isl_union_pw_multi_aff_free(data.res);
7516 isl_union_set_free(domain);
7517 isl_multi_val_free(mv);
7518 return data.res;
7521 /* Compute the pullback of data->pma by the function represented by "pma2",
7522 * provided the spaces match, and add the results to data->res.
7524 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7526 struct isl_union_pw_multi_aff_bin_data *data = user;
7528 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7529 pma2->dim, isl_dim_out)) {
7530 isl_pw_multi_aff_free(pma2);
7531 return isl_stat_ok;
7534 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7535 isl_pw_multi_aff_copy(data->pma), pma2);
7537 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7538 if (!data->res)
7539 return isl_stat_error;
7541 return isl_stat_ok;
7544 /* Compute the pullback of "upma1" by the function represented by "upma2".
7546 __isl_give isl_union_pw_multi_aff *
7547 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7548 __isl_take isl_union_pw_multi_aff *upma1,
7549 __isl_take isl_union_pw_multi_aff *upma2)
7551 return bin_op(upma1, upma2, &pullback_entry);
7554 /* Check that the domain space of "upa" matches "space".
7556 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7557 * can in principle never fail since the space "space" is that
7558 * of the isl_multi_union_pw_aff and is a set space such that
7559 * there is no domain space to match.
7561 * We check the parameters and double-check that "space" is
7562 * indeed that of a set.
7564 static isl_stat isl_union_pw_aff_check_match_domain_space(
7565 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7567 isl_space *upa_space;
7568 isl_bool match;
7570 if (!upa || !space)
7571 return isl_stat_error;
7573 match = isl_space_is_set(space);
7574 if (match < 0)
7575 return isl_stat_error;
7576 if (!match)
7577 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7578 "expecting set space", return -1);
7580 upa_space = isl_union_pw_aff_get_space(upa);
7581 match = isl_space_has_equal_params(space, upa_space);
7582 if (match < 0)
7583 goto error;
7584 if (!match)
7585 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7586 "parameters don't match", goto error);
7588 isl_space_free(upa_space);
7589 return isl_stat_ok;
7590 error:
7591 isl_space_free(upa_space);
7592 return isl_stat_error;
7595 /* Do the parameters of "upa" match those of "space"?
7597 static isl_bool isl_union_pw_aff_matching_params(
7598 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7600 isl_space *upa_space;
7601 isl_bool match;
7603 if (!upa || !space)
7604 return isl_bool_error;
7606 upa_space = isl_union_pw_aff_get_space(upa);
7608 match = isl_space_has_equal_params(space, upa_space);
7610 isl_space_free(upa_space);
7611 return match;
7614 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7615 * space represents the new parameters.
7616 * res collects the results.
7618 struct isl_union_pw_aff_reset_params_data {
7619 isl_space *space;
7620 isl_union_pw_aff *res;
7623 /* Replace the parameters of "pa" by data->space and
7624 * add the result to data->res.
7626 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7628 struct isl_union_pw_aff_reset_params_data *data = user;
7629 isl_space *space;
7631 space = isl_pw_aff_get_space(pa);
7632 space = isl_space_replace_params(space, data->space);
7633 pa = isl_pw_aff_reset_space(pa, space);
7634 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7636 return data->res ? isl_stat_ok : isl_stat_error;
7639 /* Replace the domain space of "upa" by "space".
7640 * Since a union expression does not have a (single) domain space,
7641 * "space" is necessarily a parameter space.
7643 * Since the order and the names of the parameters determine
7644 * the hash value, we need to create a new hash table.
7646 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7647 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7649 struct isl_union_pw_aff_reset_params_data data = { space };
7650 isl_bool match;
7652 match = isl_union_pw_aff_matching_params(upa, space);
7653 if (match < 0)
7654 upa = isl_union_pw_aff_free(upa);
7655 else if (match) {
7656 isl_space_free(space);
7657 return upa;
7660 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7661 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7662 data.res = isl_union_pw_aff_free(data.res);
7664 isl_union_pw_aff_free(upa);
7665 isl_space_free(space);
7666 return data.res;
7669 /* Return the floor of "pa".
7671 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7673 return isl_pw_aff_floor(pa);
7676 /* Given f, return floor(f).
7678 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7679 __isl_take isl_union_pw_aff *upa)
7681 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7684 /* Compute
7686 * upa mod m = upa - m * floor(upa/m)
7688 * with m an integer value.
7690 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7691 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7693 isl_union_pw_aff *res;
7695 if (!upa || !m)
7696 goto error;
7698 if (!isl_val_is_int(m))
7699 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7700 "expecting integer modulo", goto error);
7701 if (!isl_val_is_pos(m))
7702 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7703 "expecting positive modulo", goto error);
7705 res = isl_union_pw_aff_copy(upa);
7706 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7707 upa = isl_union_pw_aff_floor(upa);
7708 upa = isl_union_pw_aff_scale_val(upa, m);
7709 res = isl_union_pw_aff_sub(res, upa);
7711 return res;
7712 error:
7713 isl_val_free(m);
7714 isl_union_pw_aff_free(upa);
7715 return NULL;
7718 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7719 * pos is the output position that needs to be extracted.
7720 * res collects the results.
7722 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7723 int pos;
7724 isl_union_pw_aff *res;
7727 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7728 * (assuming it has such a dimension) and add it to data->res.
7730 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7732 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7733 int n_out;
7734 isl_pw_aff *pa;
7736 if (!pma)
7737 return isl_stat_error;
7739 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7740 if (data->pos >= n_out) {
7741 isl_pw_multi_aff_free(pma);
7742 return isl_stat_ok;
7745 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7746 isl_pw_multi_aff_free(pma);
7748 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7750 return data->res ? isl_stat_ok : isl_stat_error;
7753 /* Extract an isl_union_pw_aff corresponding to
7754 * output dimension "pos" of "upma".
7756 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7757 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7759 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7760 isl_space *space;
7762 if (!upma)
7763 return NULL;
7765 if (pos < 0)
7766 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7767 "cannot extract at negative position", return NULL);
7769 space = isl_union_pw_multi_aff_get_space(upma);
7770 data.res = isl_union_pw_aff_empty(space);
7771 data.pos = pos;
7772 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7773 &get_union_pw_aff, &data) < 0)
7774 data.res = isl_union_pw_aff_free(data.res);
7776 return data.res;
7779 /* Return a union piecewise affine expression
7780 * that is equal to "aff" on "domain".
7782 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7783 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7785 isl_pw_aff *pa;
7787 pa = isl_pw_aff_from_aff(aff);
7788 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7791 /* Return a union piecewise affine expression
7792 * that is equal to the parameter identified by "id" on "domain".
7794 * Make sure the parameter appears in the space passed to
7795 * isl_aff_param_on_domain_space_id.
7797 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7798 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7800 isl_space *space;
7801 isl_aff *aff;
7803 space = isl_union_set_get_space(domain);
7804 space = isl_space_add_param_id(space, isl_id_copy(id));
7805 aff = isl_aff_param_on_domain_space_id(space, id);
7806 return isl_union_pw_aff_aff_on_domain(domain, aff);
7809 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7810 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7811 * needs to attain.
7812 * "res" collects the results.
7814 struct isl_union_pw_aff_pw_aff_on_domain_data {
7815 isl_pw_aff *pa;
7816 isl_union_pw_aff *res;
7819 /* Construct a piecewise affine expression that is equal to data->pa
7820 * on "domain" and add the result to data->res.
7822 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7824 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7825 isl_pw_aff *pa;
7826 int dim;
7828 pa = isl_pw_aff_copy(data->pa);
7829 dim = isl_set_dim(domain, isl_dim_set);
7830 pa = isl_pw_aff_from_range(pa);
7831 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7832 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7833 pa = isl_pw_aff_intersect_domain(pa, domain);
7834 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7836 return data->res ? isl_stat_ok : isl_stat_error;
7839 /* Return a union piecewise affine expression
7840 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7841 * have been aligned.
7843 * Construct an isl_pw_aff on each of the sets in "domain" and
7844 * collect the results.
7846 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7847 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7849 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7850 isl_space *space;
7852 space = isl_union_set_get_space(domain);
7853 data.res = isl_union_pw_aff_empty(space);
7854 data.pa = pa;
7855 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7856 data.res = isl_union_pw_aff_free(data.res);
7857 isl_union_set_free(domain);
7858 isl_pw_aff_free(pa);
7859 return data.res;
7862 /* Return a union piecewise affine expression
7863 * that is equal to "pa" on "domain".
7865 * Check that "pa" is a parametric expression,
7866 * align the parameters if needed and call
7867 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7869 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7870 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7872 isl_bool is_set;
7873 isl_bool equal_params;
7874 isl_space *domain_space, *pa_space;
7876 pa_space = isl_pw_aff_peek_space(pa);
7877 is_set = isl_space_is_set(pa_space);
7878 if (is_set < 0)
7879 goto error;
7880 if (!is_set)
7881 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7882 "expecting parametric expression", goto error);
7884 domain_space = isl_union_set_get_space(domain);
7885 pa_space = isl_pw_aff_get_space(pa);
7886 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7887 if (equal_params >= 0 && !equal_params) {
7888 isl_space *space;
7890 space = isl_space_align_params(domain_space, pa_space);
7891 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7892 domain = isl_union_set_align_params(domain, space);
7893 } else {
7894 isl_space_free(domain_space);
7895 isl_space_free(pa_space);
7898 if (equal_params < 0)
7899 goto error;
7900 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7901 error:
7902 isl_union_set_free(domain);
7903 isl_pw_aff_free(pa);
7904 return NULL;
7907 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7908 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7909 * "res" collects the results.
7911 struct isl_union_pw_aff_val_on_domain_data {
7912 isl_val *v;
7913 isl_union_pw_aff *res;
7916 /* Construct a piecewise affine expression that is equal to data->v
7917 * on "domain" and add the result to data->res.
7919 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7921 struct isl_union_pw_aff_val_on_domain_data *data = user;
7922 isl_pw_aff *pa;
7923 isl_val *v;
7925 v = isl_val_copy(data->v);
7926 pa = isl_pw_aff_val_on_domain(domain, v);
7927 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7929 return data->res ? isl_stat_ok : isl_stat_error;
7932 /* Return a union piecewise affine expression
7933 * that is equal to "v" on "domain".
7935 * Construct an isl_pw_aff on each of the sets in "domain" and
7936 * collect the results.
7938 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7939 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7941 struct isl_union_pw_aff_val_on_domain_data data;
7942 isl_space *space;
7944 space = isl_union_set_get_space(domain);
7945 data.res = isl_union_pw_aff_empty(space);
7946 data.v = v;
7947 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7948 data.res = isl_union_pw_aff_free(data.res);
7949 isl_union_set_free(domain);
7950 isl_val_free(v);
7951 return data.res;
7954 /* Construct a piecewise multi affine expression
7955 * that is equal to "pa" and add it to upma.
7957 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7958 void *user)
7960 isl_union_pw_multi_aff **upma = user;
7961 isl_pw_multi_aff *pma;
7963 pma = isl_pw_multi_aff_from_pw_aff(pa);
7964 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7966 return *upma ? isl_stat_ok : isl_stat_error;
7969 /* Construct and return a union piecewise multi affine expression
7970 * that is equal to the given union piecewise affine expression.
7972 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7973 __isl_take isl_union_pw_aff *upa)
7975 isl_space *space;
7976 isl_union_pw_multi_aff *upma;
7978 if (!upa)
7979 return NULL;
7981 space = isl_union_pw_aff_get_space(upa);
7982 upma = isl_union_pw_multi_aff_empty(space);
7984 if (isl_union_pw_aff_foreach_pw_aff(upa,
7985 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7986 upma = isl_union_pw_multi_aff_free(upma);
7988 isl_union_pw_aff_free(upa);
7989 return upma;
7992 /* Compute the set of elements in the domain of "pa" where it is zero and
7993 * add this set to "uset".
7995 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7997 isl_union_set **uset = (isl_union_set **)user;
7999 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8001 return *uset ? isl_stat_ok : isl_stat_error;
8004 /* Return a union set containing those elements in the domain
8005 * of "upa" where it is zero.
8007 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8008 __isl_take isl_union_pw_aff *upa)
8010 isl_union_set *zero;
8012 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8013 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8014 zero = isl_union_set_free(zero);
8016 isl_union_pw_aff_free(upa);
8017 return zero;
8020 /* Convert "pa" to an isl_map and add it to *umap.
8022 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
8024 isl_union_map **umap = user;
8025 isl_map *map;
8027 map = isl_map_from_pw_aff(pa);
8028 *umap = isl_union_map_add_map(*umap, map);
8030 return *umap ? isl_stat_ok : isl_stat_error;
8033 /* Construct a union map mapping the domain of the union
8034 * piecewise affine expression to its range, with the single output dimension
8035 * equated to the corresponding affine expressions on their cells.
8037 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
8038 __isl_take isl_union_pw_aff *upa)
8040 isl_space *space;
8041 isl_union_map *umap;
8043 if (!upa)
8044 return NULL;
8046 space = isl_union_pw_aff_get_space(upa);
8047 umap = isl_union_map_empty(space);
8049 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
8050 &umap) < 0)
8051 umap = isl_union_map_free(umap);
8053 isl_union_pw_aff_free(upa);
8054 return umap;
8057 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8058 * upma is the function that is plugged in.
8059 * pa is the current part of the function in which upma is plugged in.
8060 * res collects the results.
8062 struct isl_union_pw_aff_pullback_upma_data {
8063 isl_union_pw_multi_aff *upma;
8064 isl_pw_aff *pa;
8065 isl_union_pw_aff *res;
8068 /* Check if "pma" can be plugged into data->pa.
8069 * If so, perform the pullback and add the result to data->res.
8071 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8073 struct isl_union_pw_aff_pullback_upma_data *data = user;
8074 isl_pw_aff *pa;
8076 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8077 pma->dim, isl_dim_out)) {
8078 isl_pw_multi_aff_free(pma);
8079 return isl_stat_ok;
8082 pa = isl_pw_aff_copy(data->pa);
8083 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8085 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8087 return data->res ? isl_stat_ok : isl_stat_error;
8090 /* Check if any of the elements of data->upma can be plugged into pa,
8091 * add if so add the result to data->res.
8093 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8095 struct isl_union_pw_aff_pullback_upma_data *data = user;
8096 isl_stat r;
8098 data->pa = pa;
8099 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8100 &pa_pb_pma, data);
8101 isl_pw_aff_free(pa);
8103 return r;
8106 /* Compute the pullback of "upa" by the function represented by "upma".
8107 * In other words, plug in "upma" in "upa". The result contains
8108 * expressions defined over the domain space of "upma".
8110 * Run over all pairs of elements in "upa" and "upma", perform
8111 * the pullback when appropriate and collect the results.
8112 * If the hash value were based on the domain space rather than
8113 * the function space, then we could run through all elements
8114 * of "upma" and directly pick out the corresponding element of "upa".
8116 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8117 __isl_take isl_union_pw_aff *upa,
8118 __isl_take isl_union_pw_multi_aff *upma)
8120 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8121 isl_space *space;
8123 space = isl_union_pw_multi_aff_get_space(upma);
8124 upa = isl_union_pw_aff_align_params(upa, space);
8125 space = isl_union_pw_aff_get_space(upa);
8126 upma = isl_union_pw_multi_aff_align_params(upma, space);
8128 if (!upa || !upma)
8129 goto error;
8131 data.upma = upma;
8132 data.res = isl_union_pw_aff_alloc_same_size(upa);
8133 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8134 data.res = isl_union_pw_aff_free(data.res);
8136 isl_union_pw_aff_free(upa);
8137 isl_union_pw_multi_aff_free(upma);
8138 return data.res;
8139 error:
8140 isl_union_pw_aff_free(upa);
8141 isl_union_pw_multi_aff_free(upma);
8142 return NULL;
8145 #undef BASE
8146 #define BASE union_pw_aff
8147 #undef DOMBASE
8148 #define DOMBASE union_set
8150 #define NO_MOVE_DIMS
8151 #define NO_DOMAIN
8152 #define NO_PRODUCT
8153 #define NO_SPLICE
8154 #define NO_ZERO
8155 #define NO_IDENTITY
8156 #define NO_GIST
8158 #include <isl_multi_explicit_domain.c>
8159 #include <isl_multi_union_pw_aff_explicit_domain.c>
8160 #include <isl_multi_templ.c>
8161 #include <isl_multi_apply_set.c>
8162 #include <isl_multi_apply_union_set.c>
8163 #include <isl_multi_coalesce.c>
8164 #include <isl_multi_floor.c>
8165 #include <isl_multi_gist.c>
8166 #include <isl_multi_align_set.c>
8167 #include <isl_multi_align_union_set.c>
8168 #include <isl_multi_intersect.c>
8170 /* Does "mupa" have a non-trivial explicit domain?
8172 * The explicit domain, if present, is trivial if it represents
8173 * an (obviously) universe parameter set.
8175 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8176 __isl_keep isl_multi_union_pw_aff *mupa)
8178 isl_bool is_params, trivial;
8179 isl_set *set;
8181 if (!mupa)
8182 return isl_bool_error;
8183 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8184 return isl_bool_false;
8185 is_params = isl_union_set_is_params(mupa->u.dom);
8186 if (is_params < 0 || !is_params)
8187 return isl_bool_not(is_params);
8188 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8189 trivial = isl_set_plain_is_universe(set);
8190 isl_set_free(set);
8191 return isl_bool_not(trivial);
8194 /* Construct a multiple union piecewise affine expression
8195 * in the given space with value zero in each of the output dimensions.
8197 * Since there is no canonical zero value for
8198 * a union piecewise affine expression, we can only construct
8199 * a zero-dimensional "zero" value.
8201 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8202 __isl_take isl_space *space)
8204 isl_bool params;
8206 if (!space)
8207 return NULL;
8209 params = isl_space_is_params(space);
8210 if (params < 0)
8211 goto error;
8212 if (params)
8213 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8214 "expecting proper set space", goto error);
8215 if (!isl_space_is_set(space))
8216 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8217 "expecting set space", goto error);
8218 if (isl_space_dim(space , isl_dim_out) != 0)
8219 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8220 "expecting 0D space", goto error);
8222 return isl_multi_union_pw_aff_alloc(space);
8223 error:
8224 isl_space_free(space);
8225 return NULL;
8228 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8229 * with the actual sum on the shared domain and
8230 * the defined expression on the symmetric difference of the domains.
8232 * We simply iterate over the elements in both arguments and
8233 * call isl_union_pw_aff_union_add on each of them, if there is
8234 * at least one element.
8236 * Otherwise, the two expressions have an explicit domain and
8237 * the union of these explicit domains is computed.
8238 * This assumes that the explicit domains are either both in terms
8239 * of specific domains elements or both in terms of parameters.
8240 * However, if one of the expressions does not have any constraints
8241 * on its explicit domain, then this is allowed as well and the result
8242 * is the expression with no constraints on its explicit domain.
8244 static __isl_give isl_multi_union_pw_aff *
8245 isl_multi_union_pw_aff_union_add_aligned(
8246 __isl_take isl_multi_union_pw_aff *mupa1,
8247 __isl_take isl_multi_union_pw_aff *mupa2)
8249 isl_bool has_domain, is_params1, is_params2;
8251 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8252 goto error;
8253 if (mupa1->n > 0)
8254 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8255 &isl_union_pw_aff_union_add);
8256 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8257 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8258 goto error;
8260 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8261 if (has_domain < 0)
8262 goto error;
8263 if (!has_domain) {
8264 isl_multi_union_pw_aff_free(mupa2);
8265 return mupa1;
8267 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8268 if (has_domain < 0)
8269 goto error;
8270 if (!has_domain) {
8271 isl_multi_union_pw_aff_free(mupa1);
8272 return mupa2;
8275 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8276 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8277 if (is_params1 < 0 || is_params2 < 0)
8278 goto error;
8279 if (is_params1 != is_params2)
8280 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8281 isl_error_invalid,
8282 "cannot compute union of concrete domain and "
8283 "parameter constraints", goto error);
8284 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8285 if (!mupa1)
8286 goto error;
8287 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8288 isl_union_set_copy(mupa2->u.dom));
8289 if (!mupa1->u.dom)
8290 goto error;
8291 isl_multi_union_pw_aff_free(mupa2);
8292 return mupa1;
8293 error:
8294 isl_multi_union_pw_aff_free(mupa1);
8295 isl_multi_union_pw_aff_free(mupa2);
8296 return NULL;
8299 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8300 * with the actual sum on the shared domain and
8301 * the defined expression on the symmetric difference of the domains.
8303 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8304 __isl_take isl_multi_union_pw_aff *mupa1,
8305 __isl_take isl_multi_union_pw_aff *mupa2)
8307 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8308 &isl_multi_union_pw_aff_union_add_aligned);
8311 /* Construct and return a multi union piecewise affine expression
8312 * that is equal to the given multi affine expression.
8314 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8315 __isl_take isl_multi_aff *ma)
8317 isl_multi_pw_aff *mpa;
8319 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8320 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8323 /* Construct and return a multi union piecewise affine expression
8324 * that is equal to the given multi piecewise affine expression.
8326 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8327 __isl_take isl_multi_pw_aff *mpa)
8329 int i, n;
8330 isl_space *space;
8331 isl_multi_union_pw_aff *mupa;
8333 if (!mpa)
8334 return NULL;
8336 space = isl_multi_pw_aff_get_space(mpa);
8337 space = isl_space_range(space);
8338 mupa = isl_multi_union_pw_aff_alloc(space);
8340 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8341 for (i = 0; i < n; ++i) {
8342 isl_pw_aff *pa;
8343 isl_union_pw_aff *upa;
8345 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8346 upa = isl_union_pw_aff_from_pw_aff(pa);
8347 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8350 isl_multi_pw_aff_free(mpa);
8352 return mupa;
8355 /* Extract the range space of "pma" and assign it to *space.
8356 * If *space has already been set (through a previous call to this function),
8357 * then check that the range space is the same.
8359 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8361 isl_space **space = user;
8362 isl_space *pma_space;
8363 isl_bool equal;
8365 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8366 isl_pw_multi_aff_free(pma);
8368 if (!pma_space)
8369 return isl_stat_error;
8370 if (!*space) {
8371 *space = pma_space;
8372 return isl_stat_ok;
8375 equal = isl_space_is_equal(pma_space, *space);
8376 isl_space_free(pma_space);
8378 if (equal < 0)
8379 return isl_stat_error;
8380 if (!equal)
8381 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8382 "range spaces not the same", return isl_stat_error);
8383 return isl_stat_ok;
8386 /* Construct and return a multi union piecewise affine expression
8387 * that is equal to the given union piecewise multi affine expression.
8389 * In order to be able to perform the conversion, the input
8390 * needs to be non-empty and may only involve a single range space.
8392 * If the resulting multi union piecewise affine expression has
8393 * an explicit domain, then assign it the domain of the input.
8394 * In other cases, the domain is stored in the individual elements.
8396 __isl_give isl_multi_union_pw_aff *
8397 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8398 __isl_take isl_union_pw_multi_aff *upma)
8400 isl_space *space = NULL;
8401 isl_multi_union_pw_aff *mupa;
8402 int i, n;
8404 if (!upma)
8405 return NULL;
8406 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8407 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8408 "cannot extract range space from empty input",
8409 goto error);
8410 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8411 &space) < 0)
8412 goto error;
8414 if (!space)
8415 goto error;
8417 n = isl_space_dim(space, isl_dim_set);
8418 mupa = isl_multi_union_pw_aff_alloc(space);
8420 for (i = 0; i < n; ++i) {
8421 isl_union_pw_aff *upa;
8423 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8424 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8426 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8427 isl_union_set *dom;
8428 isl_union_pw_multi_aff *copy;
8430 copy = isl_union_pw_multi_aff_copy(upma);
8431 dom = isl_union_pw_multi_aff_domain(copy);
8432 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8435 isl_union_pw_multi_aff_free(upma);
8436 return mupa;
8437 error:
8438 isl_space_free(space);
8439 isl_union_pw_multi_aff_free(upma);
8440 return NULL;
8443 /* Try and create an isl_multi_union_pw_aff that is equivalent
8444 * to the given isl_union_map.
8445 * The isl_union_map is required to be single-valued in each space.
8446 * Moreover, it cannot be empty and all range spaces need to be the same.
8447 * Otherwise, an error is produced.
8449 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8450 __isl_take isl_union_map *umap)
8452 isl_union_pw_multi_aff *upma;
8454 upma = isl_union_pw_multi_aff_from_union_map(umap);
8455 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8458 /* Return a multiple union piecewise affine expression
8459 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8460 * have been aligned.
8462 static __isl_give isl_multi_union_pw_aff *
8463 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8464 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8466 int i, n;
8467 isl_space *space;
8468 isl_multi_union_pw_aff *mupa;
8470 if (!domain || !mv)
8471 goto error;
8473 n = isl_multi_val_dim(mv, isl_dim_set);
8474 space = isl_multi_val_get_space(mv);
8475 mupa = isl_multi_union_pw_aff_alloc(space);
8476 for (i = 0; i < n; ++i) {
8477 isl_val *v;
8478 isl_union_pw_aff *upa;
8480 v = isl_multi_val_get_val(mv, i);
8481 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8483 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8486 isl_union_set_free(domain);
8487 isl_multi_val_free(mv);
8488 return mupa;
8489 error:
8490 isl_union_set_free(domain);
8491 isl_multi_val_free(mv);
8492 return NULL;
8495 /* Return a multiple union piecewise affine expression
8496 * that is equal to "mv" on "domain".
8498 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8499 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8501 isl_bool equal_params;
8503 if (!domain || !mv)
8504 goto error;
8505 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8506 if (equal_params < 0)
8507 goto error;
8508 if (equal_params)
8509 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8510 domain, mv);
8511 domain = isl_union_set_align_params(domain,
8512 isl_multi_val_get_space(mv));
8513 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8514 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8515 error:
8516 isl_union_set_free(domain);
8517 isl_multi_val_free(mv);
8518 return NULL;
8521 /* Return a multiple union piecewise affine expression
8522 * that is equal to "ma" on "domain".
8524 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8525 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8527 isl_pw_multi_aff *pma;
8529 pma = isl_pw_multi_aff_from_multi_aff(ma);
8530 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8533 /* Return a multiple union piecewise affine expression
8534 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8535 * have been aligned.
8537 * If the resulting multi union piecewise affine expression has
8538 * an explicit domain, then assign it the input domain.
8539 * In other cases, the domain is stored in the individual elements.
8541 static __isl_give isl_multi_union_pw_aff *
8542 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8543 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8545 int i, n;
8546 isl_space *space;
8547 isl_multi_union_pw_aff *mupa;
8549 if (!domain || !pma)
8550 goto error;
8552 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8553 space = isl_pw_multi_aff_get_space(pma);
8554 mupa = isl_multi_union_pw_aff_alloc(space);
8555 for (i = 0; i < n; ++i) {
8556 isl_pw_aff *pa;
8557 isl_union_pw_aff *upa;
8559 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8560 upa = isl_union_pw_aff_pw_aff_on_domain(
8561 isl_union_set_copy(domain), pa);
8562 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8564 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8565 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8566 isl_union_set_copy(domain));
8568 isl_union_set_free(domain);
8569 isl_pw_multi_aff_free(pma);
8570 return mupa;
8571 error:
8572 isl_union_set_free(domain);
8573 isl_pw_multi_aff_free(pma);
8574 return NULL;
8577 /* Return a multiple union piecewise affine expression
8578 * that is equal to "pma" on "domain".
8580 __isl_give isl_multi_union_pw_aff *
8581 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8582 __isl_take isl_pw_multi_aff *pma)
8584 isl_bool equal_params;
8585 isl_space *space;
8587 space = isl_pw_multi_aff_peek_space(pma);
8588 equal_params = isl_union_set_space_has_equal_params(domain, space);
8589 if (equal_params < 0)
8590 goto error;
8591 if (equal_params)
8592 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8593 domain, pma);
8594 domain = isl_union_set_align_params(domain,
8595 isl_pw_multi_aff_get_space(pma));
8596 pma = isl_pw_multi_aff_align_params(pma,
8597 isl_union_set_get_space(domain));
8598 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8599 pma);
8600 error:
8601 isl_union_set_free(domain);
8602 isl_pw_multi_aff_free(pma);
8603 return NULL;
8606 /* Return a union set containing those elements in the domains
8607 * of the elements of "mupa" where they are all zero.
8609 * If there are no elements, then simply return the entire domain.
8611 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8612 __isl_take isl_multi_union_pw_aff *mupa)
8614 int i, n;
8615 isl_union_pw_aff *upa;
8616 isl_union_set *zero;
8618 if (!mupa)
8619 return NULL;
8621 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8622 if (n == 0)
8623 return isl_multi_union_pw_aff_domain(mupa);
8625 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8626 zero = isl_union_pw_aff_zero_union_set(upa);
8628 for (i = 1; i < n; ++i) {
8629 isl_union_set *zero_i;
8631 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8632 zero_i = isl_union_pw_aff_zero_union_set(upa);
8634 zero = isl_union_set_intersect(zero, zero_i);
8637 isl_multi_union_pw_aff_free(mupa);
8638 return zero;
8641 /* Construct a union map mapping the shared domain
8642 * of the union piecewise affine expressions to the range of "mupa"
8643 * in the special case of a 0D multi union piecewise affine expression.
8645 * Construct a map between the explicit domain of "mupa" and
8646 * the range space.
8647 * Note that this assumes that the domain consists of explicit elements.
8649 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8650 __isl_take isl_multi_union_pw_aff *mupa)
8652 isl_bool is_params;
8653 isl_space *space;
8654 isl_union_set *dom, *ran;
8656 space = isl_multi_union_pw_aff_get_space(mupa);
8657 dom = isl_multi_union_pw_aff_domain(mupa);
8658 ran = isl_union_set_from_set(isl_set_universe(space));
8660 is_params = isl_union_set_is_params(dom);
8661 if (is_params < 0)
8662 dom = isl_union_set_free(dom);
8663 else if (is_params)
8664 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8665 "cannot create union map from expression without "
8666 "explicit domain elements",
8667 dom = isl_union_set_free(dom));
8669 return isl_union_map_from_domain_and_range(dom, ran);
8672 /* Construct a union map mapping the shared domain
8673 * of the union piecewise affine expressions to the range of "mupa"
8674 * with each dimension in the range equated to the
8675 * corresponding union piecewise affine expression.
8677 * If the input is zero-dimensional, then construct a mapping
8678 * from its explicit domain.
8680 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8681 __isl_take isl_multi_union_pw_aff *mupa)
8683 int i, n;
8684 isl_space *space;
8685 isl_union_map *umap;
8686 isl_union_pw_aff *upa;
8688 if (!mupa)
8689 return NULL;
8691 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8692 if (n == 0)
8693 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8695 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8696 umap = isl_union_map_from_union_pw_aff(upa);
8698 for (i = 1; i < n; ++i) {
8699 isl_union_map *umap_i;
8701 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8702 umap_i = isl_union_map_from_union_pw_aff(upa);
8703 umap = isl_union_map_flat_range_product(umap, umap_i);
8706 space = isl_multi_union_pw_aff_get_space(mupa);
8707 umap = isl_union_map_reset_range_space(umap, space);
8709 isl_multi_union_pw_aff_free(mupa);
8710 return umap;
8713 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8714 * "range" is the space from which to set the range space.
8715 * "res" collects the results.
8717 struct isl_union_pw_multi_aff_reset_range_space_data {
8718 isl_space *range;
8719 isl_union_pw_multi_aff *res;
8722 /* Replace the range space of "pma" by the range space of data->range and
8723 * add the result to data->res.
8725 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8727 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8728 isl_space *space;
8730 space = isl_pw_multi_aff_get_space(pma);
8731 space = isl_space_domain(space);
8732 space = isl_space_extend_domain_with_range(space,
8733 isl_space_copy(data->range));
8734 pma = isl_pw_multi_aff_reset_space(pma, space);
8735 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8737 return data->res ? isl_stat_ok : isl_stat_error;
8740 /* Replace the range space of all the piecewise affine expressions in "upma" by
8741 * the range space of "space".
8743 * This assumes that all these expressions have the same output dimension.
8745 * Since the spaces of the expressions change, so do their hash values.
8746 * We therefore need to create a new isl_union_pw_multi_aff.
8747 * Note that the hash value is currently computed based on the entire
8748 * space even though there can only be a single expression with a given
8749 * domain space.
8751 static __isl_give isl_union_pw_multi_aff *
8752 isl_union_pw_multi_aff_reset_range_space(
8753 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8755 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8756 isl_space *space_upma;
8758 space_upma = isl_union_pw_multi_aff_get_space(upma);
8759 data.res = isl_union_pw_multi_aff_empty(space_upma);
8760 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8761 &reset_range_space, &data) < 0)
8762 data.res = isl_union_pw_multi_aff_free(data.res);
8764 isl_space_free(space);
8765 isl_union_pw_multi_aff_free(upma);
8766 return data.res;
8769 /* Construct and return a union piecewise multi affine expression
8770 * that is equal to the given multi union piecewise affine expression,
8771 * in the special case of a 0D multi union piecewise affine expression.
8773 * Construct a union piecewise multi affine expression
8774 * on top of the explicit domain of the input.
8776 __isl_give isl_union_pw_multi_aff *
8777 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8778 __isl_take isl_multi_union_pw_aff *mupa)
8780 isl_space *space;
8781 isl_multi_val *mv;
8782 isl_union_set *domain;
8784 space = isl_multi_union_pw_aff_get_space(mupa);
8785 mv = isl_multi_val_zero(space);
8786 domain = isl_multi_union_pw_aff_domain(mupa);
8787 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8790 /* Construct and return a union piecewise multi affine expression
8791 * that is equal to the given multi union piecewise affine expression.
8793 * If the input is zero-dimensional, then
8794 * construct a union piecewise multi affine expression
8795 * on top of the explicit domain of the input.
8797 __isl_give isl_union_pw_multi_aff *
8798 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8799 __isl_take isl_multi_union_pw_aff *mupa)
8801 int i, n;
8802 isl_space *space;
8803 isl_union_pw_multi_aff *upma;
8804 isl_union_pw_aff *upa;
8806 if (!mupa)
8807 return NULL;
8809 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8810 if (n == 0)
8811 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8813 space = isl_multi_union_pw_aff_get_space(mupa);
8814 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8815 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8817 for (i = 1; i < n; ++i) {
8818 isl_union_pw_multi_aff *upma_i;
8820 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8821 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8822 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8825 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8827 isl_multi_union_pw_aff_free(mupa);
8828 return upma;
8831 /* Intersect the range of "mupa" with "range",
8832 * in the special case where "mupa" is 0D.
8834 * Intersect the domain of "mupa" with the constraints on the parameters
8835 * of "range".
8837 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8838 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8840 range = isl_set_params(range);
8841 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8842 return mupa;
8845 /* Intersect the range of "mupa" with "range".
8846 * That is, keep only those domain elements that have a function value
8847 * in "range".
8849 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8850 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8852 isl_union_pw_multi_aff *upma;
8853 isl_union_set *domain;
8854 isl_space *space;
8855 int n;
8856 int match;
8858 if (!mupa || !range)
8859 goto error;
8861 space = isl_set_get_space(range);
8862 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8863 space, isl_dim_set);
8864 isl_space_free(space);
8865 if (match < 0)
8866 goto error;
8867 if (!match)
8868 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8869 "space don't match", goto error);
8870 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8871 if (n == 0)
8872 return mupa_intersect_range_0D(mupa, range);
8874 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8875 isl_multi_union_pw_aff_copy(mupa));
8876 domain = isl_union_set_from_set(range);
8877 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8878 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8880 return mupa;
8881 error:
8882 isl_multi_union_pw_aff_free(mupa);
8883 isl_set_free(range);
8884 return NULL;
8887 /* Return the shared domain of the elements of "mupa",
8888 * in the special case where "mupa" is zero-dimensional.
8890 * Return the explicit domain of "mupa".
8891 * Note that this domain may be a parameter set, either
8892 * because "mupa" is meant to live in a set space or
8893 * because no explicit domain has been set.
8895 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8896 __isl_take isl_multi_union_pw_aff *mupa)
8898 isl_union_set *dom;
8900 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8901 isl_multi_union_pw_aff_free(mupa);
8903 return dom;
8906 /* Return the shared domain of the elements of "mupa".
8908 * If "mupa" is zero-dimensional, then return its explicit domain.
8910 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8911 __isl_take isl_multi_union_pw_aff *mupa)
8913 int i, n;
8914 isl_union_pw_aff *upa;
8915 isl_union_set *dom;
8917 if (!mupa)
8918 return NULL;
8920 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8921 if (n == 0)
8922 return isl_multi_union_pw_aff_domain_0D(mupa);
8924 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8925 dom = isl_union_pw_aff_domain(upa);
8926 for (i = 1; i < n; ++i) {
8927 isl_union_set *dom_i;
8929 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8930 dom_i = isl_union_pw_aff_domain(upa);
8931 dom = isl_union_set_intersect(dom, dom_i);
8934 isl_multi_union_pw_aff_free(mupa);
8935 return dom;
8938 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8939 * In particular, the spaces have been aligned.
8940 * The result is defined over the shared domain of the elements of "mupa"
8942 * We first extract the parametric constant part of "aff" and
8943 * define that over the shared domain.
8944 * Then we iterate over all input dimensions of "aff" and add the corresponding
8945 * multiples of the elements of "mupa".
8946 * Finally, we consider the integer divisions, calling the function
8947 * recursively to obtain an isl_union_pw_aff corresponding to the
8948 * integer division argument.
8950 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8951 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8953 int i, n_in, n_div;
8954 isl_union_pw_aff *upa;
8955 isl_union_set *uset;
8956 isl_val *v;
8957 isl_aff *cst;
8959 n_in = isl_aff_dim(aff, isl_dim_in);
8960 n_div = isl_aff_dim(aff, isl_dim_div);
8962 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8963 cst = isl_aff_copy(aff);
8964 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8965 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8966 cst = isl_aff_project_domain_on_params(cst);
8967 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8969 for (i = 0; i < n_in; ++i) {
8970 isl_union_pw_aff *upa_i;
8972 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8973 continue;
8974 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8975 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8976 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8977 upa = isl_union_pw_aff_add(upa, upa_i);
8980 for (i = 0; i < n_div; ++i) {
8981 isl_aff *div;
8982 isl_union_pw_aff *upa_i;
8984 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8985 continue;
8986 div = isl_aff_get_div(aff, i);
8987 upa_i = multi_union_pw_aff_apply_aff(
8988 isl_multi_union_pw_aff_copy(mupa), div);
8989 upa_i = isl_union_pw_aff_floor(upa_i);
8990 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8991 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8992 upa = isl_union_pw_aff_add(upa, upa_i);
8995 isl_multi_union_pw_aff_free(mupa);
8996 isl_aff_free(aff);
8998 return upa;
9001 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9002 * with the domain of "aff".
9003 * Furthermore, the dimension of this space needs to be greater than zero.
9004 * The result is defined over the shared domain of the elements of "mupa"
9006 * We perform these checks and then hand over control to
9007 * multi_union_pw_aff_apply_aff.
9009 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9010 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9012 isl_space *space1, *space2;
9013 int equal;
9015 mupa = isl_multi_union_pw_aff_align_params(mupa,
9016 isl_aff_get_space(aff));
9017 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9018 if (!mupa || !aff)
9019 goto error;
9021 space1 = isl_multi_union_pw_aff_get_space(mupa);
9022 space2 = isl_aff_get_domain_space(aff);
9023 equal = isl_space_is_equal(space1, space2);
9024 isl_space_free(space1);
9025 isl_space_free(space2);
9026 if (equal < 0)
9027 goto error;
9028 if (!equal)
9029 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9030 "spaces don't match", goto error);
9031 if (isl_aff_dim(aff, isl_dim_in) == 0)
9032 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9033 "cannot determine domains", goto error);
9035 return multi_union_pw_aff_apply_aff(mupa, aff);
9036 error:
9037 isl_multi_union_pw_aff_free(mupa);
9038 isl_aff_free(aff);
9039 return NULL;
9042 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9043 * The space of "mupa" is known to be compatible with the domain of "ma".
9045 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9046 * on the domain of "mupa".
9048 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9049 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9051 isl_union_set *dom;
9053 dom = isl_multi_union_pw_aff_domain(mupa);
9054 ma = isl_multi_aff_project_domain_on_params(ma);
9056 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9059 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9060 * with the domain of "ma".
9061 * The result is defined over the shared domain of the elements of "mupa"
9063 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9064 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9066 isl_space *space1, *space2;
9067 isl_multi_union_pw_aff *res;
9068 int equal;
9069 int i, n_out;
9071 mupa = isl_multi_union_pw_aff_align_params(mupa,
9072 isl_multi_aff_get_space(ma));
9073 ma = isl_multi_aff_align_params(ma,
9074 isl_multi_union_pw_aff_get_space(mupa));
9075 if (!mupa || !ma)
9076 goto error;
9078 space1 = isl_multi_union_pw_aff_get_space(mupa);
9079 space2 = isl_multi_aff_get_domain_space(ma);
9080 equal = isl_space_is_equal(space1, space2);
9081 isl_space_free(space1);
9082 isl_space_free(space2);
9083 if (equal < 0)
9084 goto error;
9085 if (!equal)
9086 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9087 "spaces don't match", goto error);
9088 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9089 if (isl_multi_aff_dim(ma, isl_dim_in) == 0)
9090 return mupa_apply_multi_aff_0D(mupa, ma);
9092 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9093 res = isl_multi_union_pw_aff_alloc(space1);
9095 for (i = 0; i < n_out; ++i) {
9096 isl_aff *aff;
9097 isl_union_pw_aff *upa;
9099 aff = isl_multi_aff_get_aff(ma, i);
9100 upa = multi_union_pw_aff_apply_aff(
9101 isl_multi_union_pw_aff_copy(mupa), aff);
9102 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9105 isl_multi_aff_free(ma);
9106 isl_multi_union_pw_aff_free(mupa);
9107 return res;
9108 error:
9109 isl_multi_union_pw_aff_free(mupa);
9110 isl_multi_aff_free(ma);
9111 return NULL;
9114 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9115 * The space of "mupa" is known to be compatible with the domain of "pa".
9117 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9118 * on the domain of "mupa".
9120 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9121 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9123 isl_union_set *dom;
9125 dom = isl_multi_union_pw_aff_domain(mupa);
9126 pa = isl_pw_aff_project_domain_on_params(pa);
9128 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9131 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9132 * with the domain of "pa".
9133 * Furthermore, the dimension of this space needs to be greater than zero.
9134 * The result is defined over the shared domain of the elements of "mupa"
9136 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9137 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9139 int i;
9140 int equal;
9141 isl_space *space, *space2;
9142 isl_union_pw_aff *upa;
9144 mupa = isl_multi_union_pw_aff_align_params(mupa,
9145 isl_pw_aff_get_space(pa));
9146 pa = isl_pw_aff_align_params(pa,
9147 isl_multi_union_pw_aff_get_space(mupa));
9148 if (!mupa || !pa)
9149 goto error;
9151 space = isl_multi_union_pw_aff_get_space(mupa);
9152 space2 = isl_pw_aff_get_domain_space(pa);
9153 equal = isl_space_is_equal(space, space2);
9154 isl_space_free(space);
9155 isl_space_free(space2);
9156 if (equal < 0)
9157 goto error;
9158 if (!equal)
9159 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9160 "spaces don't match", goto error);
9161 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
9162 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9164 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9165 upa = isl_union_pw_aff_empty(space);
9167 for (i = 0; i < pa->n; ++i) {
9168 isl_aff *aff;
9169 isl_set *domain;
9170 isl_multi_union_pw_aff *mupa_i;
9171 isl_union_pw_aff *upa_i;
9173 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9174 domain = isl_set_copy(pa->p[i].set);
9175 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9176 aff = isl_aff_copy(pa->p[i].aff);
9177 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9178 upa = isl_union_pw_aff_union_add(upa, upa_i);
9181 isl_multi_union_pw_aff_free(mupa);
9182 isl_pw_aff_free(pa);
9183 return upa;
9184 error:
9185 isl_multi_union_pw_aff_free(mupa);
9186 isl_pw_aff_free(pa);
9187 return NULL;
9190 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9191 * The space of "mupa" is known to be compatible with the domain of "pma".
9193 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9194 * on the domain of "mupa".
9196 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9197 __isl_take isl_multi_union_pw_aff *mupa,
9198 __isl_take isl_pw_multi_aff *pma)
9200 isl_union_set *dom;
9202 dom = isl_multi_union_pw_aff_domain(mupa);
9203 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9205 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9208 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9209 * with the domain of "pma".
9210 * The result is defined over the shared domain of the elements of "mupa"
9212 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9213 __isl_take isl_multi_union_pw_aff *mupa,
9214 __isl_take isl_pw_multi_aff *pma)
9216 isl_space *space1, *space2;
9217 isl_multi_union_pw_aff *res;
9218 int equal;
9219 int i, n_out;
9221 mupa = isl_multi_union_pw_aff_align_params(mupa,
9222 isl_pw_multi_aff_get_space(pma));
9223 pma = isl_pw_multi_aff_align_params(pma,
9224 isl_multi_union_pw_aff_get_space(mupa));
9225 if (!mupa || !pma)
9226 goto error;
9228 space1 = isl_multi_union_pw_aff_get_space(mupa);
9229 space2 = isl_pw_multi_aff_get_domain_space(pma);
9230 equal = isl_space_is_equal(space1, space2);
9231 isl_space_free(space1);
9232 isl_space_free(space2);
9233 if (equal < 0)
9234 goto error;
9235 if (!equal)
9236 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9237 "spaces don't match", goto error);
9238 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9239 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0)
9240 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9242 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9243 res = isl_multi_union_pw_aff_alloc(space1);
9245 for (i = 0; i < n_out; ++i) {
9246 isl_pw_aff *pa;
9247 isl_union_pw_aff *upa;
9249 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9250 upa = isl_multi_union_pw_aff_apply_pw_aff(
9251 isl_multi_union_pw_aff_copy(mupa), pa);
9252 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9255 isl_pw_multi_aff_free(pma);
9256 isl_multi_union_pw_aff_free(mupa);
9257 return res;
9258 error:
9259 isl_multi_union_pw_aff_free(mupa);
9260 isl_pw_multi_aff_free(pma);
9261 return NULL;
9264 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9265 * If the explicit domain only keeps track of constraints on the parameters,
9266 * then only update those constraints.
9268 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9269 __isl_take isl_multi_union_pw_aff *mupa,
9270 __isl_keep isl_union_pw_multi_aff *upma)
9272 isl_bool is_params;
9274 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9275 return isl_multi_union_pw_aff_free(mupa);
9277 mupa = isl_multi_union_pw_aff_cow(mupa);
9278 if (!mupa)
9279 return NULL;
9281 is_params = isl_union_set_is_params(mupa->u.dom);
9282 if (is_params < 0)
9283 return isl_multi_union_pw_aff_free(mupa);
9285 upma = isl_union_pw_multi_aff_copy(upma);
9286 if (is_params)
9287 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9288 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9289 else
9290 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9291 mupa->u.dom, upma);
9292 if (!mupa->u.dom)
9293 return isl_multi_union_pw_aff_free(mupa);
9294 return mupa;
9297 /* Compute the pullback of "mupa" by the function represented by "upma".
9298 * In other words, plug in "upma" in "mupa". The result contains
9299 * expressions defined over the domain space of "upma".
9301 * Run over all elements of "mupa" and plug in "upma" in each of them.
9303 * If "mupa" has an explicit domain, then it is this domain
9304 * that needs to undergo a pullback instead, i.e., a preimage.
9306 __isl_give isl_multi_union_pw_aff *
9307 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9308 __isl_take isl_multi_union_pw_aff *mupa,
9309 __isl_take isl_union_pw_multi_aff *upma)
9311 int i, n;
9313 mupa = isl_multi_union_pw_aff_align_params(mupa,
9314 isl_union_pw_multi_aff_get_space(upma));
9315 upma = isl_union_pw_multi_aff_align_params(upma,
9316 isl_multi_union_pw_aff_get_space(mupa));
9317 mupa = isl_multi_union_pw_aff_cow(mupa);
9318 if (!mupa || !upma)
9319 goto error;
9321 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9322 for (i = 0; i < n; ++i) {
9323 isl_union_pw_aff *upa;
9325 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9326 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9327 isl_union_pw_multi_aff_copy(upma));
9328 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9331 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9332 mupa = preimage_explicit_domain(mupa, upma);
9334 isl_union_pw_multi_aff_free(upma);
9335 return mupa;
9336 error:
9337 isl_multi_union_pw_aff_free(mupa);
9338 isl_union_pw_multi_aff_free(upma);
9339 return NULL;
9342 /* Extract the sequence of elements in "mupa" with domain space "space"
9343 * (ignoring parameters).
9345 * For the elements of "mupa" that are not defined on the specified space,
9346 * the corresponding element in the result is empty.
9348 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9349 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9351 int i, n;
9352 isl_bool equal_params;
9353 isl_space *space_mpa = NULL;
9354 isl_multi_pw_aff *mpa;
9356 if (!mupa || !space)
9357 goto error;
9359 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9360 equal_params = isl_space_has_equal_params(space_mpa, space);
9361 if (equal_params < 0)
9362 goto error;
9363 if (!equal_params) {
9364 space = isl_space_drop_dims(space, isl_dim_param,
9365 0, isl_space_dim(space, isl_dim_param));
9366 space = isl_space_align_params(space,
9367 isl_space_copy(space_mpa));
9368 if (!space)
9369 goto error;
9371 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9372 space_mpa);
9373 mpa = isl_multi_pw_aff_alloc(space_mpa);
9375 space = isl_space_from_domain(space);
9376 space = isl_space_add_dims(space, isl_dim_out, 1);
9377 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9378 for (i = 0; i < n; ++i) {
9379 isl_union_pw_aff *upa;
9380 isl_pw_aff *pa;
9382 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9383 pa = isl_union_pw_aff_extract_pw_aff(upa,
9384 isl_space_copy(space));
9385 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9386 isl_union_pw_aff_free(upa);
9389 isl_space_free(space);
9390 return mpa;
9391 error:
9392 isl_space_free(space_mpa);
9393 isl_space_free(space);
9394 return NULL;
9397 /* Evaluate the affine function "aff" in the void point "pnt".
9398 * In particular, return the value NaN.
9400 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9401 __isl_take isl_point *pnt)
9403 isl_ctx *ctx;
9405 ctx = isl_point_get_ctx(pnt);
9406 isl_aff_free(aff);
9407 isl_point_free(pnt);
9408 return isl_val_nan(ctx);
9411 /* Evaluate the affine expression "aff"
9412 * in the coordinates (with denominator) "pnt".
9414 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9415 __isl_keep isl_vec *pnt)
9417 isl_int n, d;
9418 isl_ctx *ctx;
9419 isl_val *v;
9421 if (!aff || !pnt)
9422 return NULL;
9424 ctx = isl_vec_get_ctx(aff);
9425 isl_int_init(n);
9426 isl_int_init(d);
9427 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9428 isl_int_mul(d, aff->el[0], pnt->el[0]);
9429 v = isl_val_rat_from_isl_int(ctx, n, d);
9430 v = isl_val_normalize(v);
9431 isl_int_clear(n);
9432 isl_int_clear(d);
9434 return v;
9437 /* Check that the domain space of "aff" is equal to "space".
9439 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9440 __isl_keep isl_space *space)
9442 isl_bool ok;
9444 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9445 if (ok < 0)
9446 return isl_stat_error;
9447 if (!ok)
9448 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9449 "incompatible spaces", return isl_stat_error);
9450 return isl_stat_ok;
9453 /* Evaluate the affine function "aff" in "pnt".
9455 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9456 __isl_take isl_point *pnt)
9458 isl_bool is_void;
9459 isl_val *v;
9460 isl_local_space *ls;
9462 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9463 goto error;
9464 is_void = isl_point_is_void(pnt);
9465 if (is_void < 0)
9466 goto error;
9467 if (is_void)
9468 return eval_void(aff, pnt);
9470 ls = isl_aff_get_domain_local_space(aff);
9471 pnt = isl_local_space_lift_point(ls, pnt);
9473 v = eval(aff->v, isl_point_peek_vec(pnt));
9475 isl_aff_free(aff);
9476 isl_point_free(pnt);
9478 return v;
9479 error:
9480 isl_aff_free(aff);
9481 isl_point_free(pnt);
9482 return NULL;