interface/cpp.cc: cpp_generator::print_method_impl: drop unused variable
[isl.git] / isl_aff.c
blobbec379642dfce226342c014a3b343502031d3dfe
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_config.h>
32 #undef BASE
33 #define BASE aff
35 #include <isl_list_templ.c>
37 #undef BASE
38 #define BASE pw_aff
40 #include <isl_list_templ.c>
42 #undef BASE
43 #define BASE union_pw_aff
45 #include <isl_list_templ.c>
47 #undef BASE
48 #define BASE union_pw_multi_aff
50 #include <isl_list_templ.c>
52 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
53 __isl_take isl_vec *v)
55 isl_aff *aff;
57 if (!ls || !v)
58 goto error;
60 aff = isl_calloc_type(v->ctx, struct isl_aff);
61 if (!aff)
62 goto error;
64 aff->ref = 1;
65 aff->ls = ls;
66 aff->v = v;
68 return aff;
69 error:
70 isl_local_space_free(ls);
71 isl_vec_free(v);
72 return NULL;
75 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
77 isl_ctx *ctx;
78 isl_vec *v;
79 unsigned total;
81 if (!ls)
82 return NULL;
84 ctx = isl_local_space_get_ctx(ls);
85 if (!isl_local_space_divs_known(ls))
86 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
87 goto error);
88 if (!isl_local_space_is_set(ls))
89 isl_die(ctx, isl_error_invalid,
90 "domain of affine expression should be a set",
91 goto error);
93 total = isl_local_space_dim(ls, isl_dim_all);
94 v = isl_vec_alloc(ctx, 1 + 1 + total);
95 return isl_aff_alloc_vec(ls, v);
96 error:
97 isl_local_space_free(ls);
98 return NULL;
101 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
103 isl_aff *aff;
105 aff = isl_aff_alloc(ls);
106 if (!aff)
107 return NULL;
109 isl_int_set_si(aff->v->el[0], 1);
110 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
112 return aff;
115 /* Return a piecewise affine expression defined on the specified domain
116 * that is equal to zero.
118 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
120 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
123 /* Return an affine expression defined on the specified domain
124 * that represents NaN.
126 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
128 isl_aff *aff;
130 aff = isl_aff_alloc(ls);
131 if (!aff)
132 return NULL;
134 isl_seq_clr(aff->v->el, aff->v->size);
136 return aff;
139 /* Return a piecewise affine expression defined on the specified domain
140 * that represents NaN.
142 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
144 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
147 /* Return an affine expression that is equal to "val" on
148 * domain local space "ls".
150 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
151 __isl_take isl_val *val)
153 isl_aff *aff;
155 if (!ls || !val)
156 goto error;
157 if (!isl_val_is_rat(val))
158 isl_die(isl_val_get_ctx(val), isl_error_invalid,
159 "expecting rational value", goto error);
161 aff = isl_aff_alloc(isl_local_space_copy(ls));
162 if (!aff)
163 goto error;
165 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
166 isl_int_set(aff->v->el[1], val->n);
167 isl_int_set(aff->v->el[0], val->d);
169 isl_local_space_free(ls);
170 isl_val_free(val);
171 return aff;
172 error:
173 isl_local_space_free(ls);
174 isl_val_free(val);
175 return NULL;
178 /* Return an affine expression that is equal to the specified dimension
179 * in "ls".
181 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
182 enum isl_dim_type type, unsigned pos)
184 isl_space *space;
185 isl_aff *aff;
187 if (!ls)
188 return NULL;
190 space = isl_local_space_get_space(ls);
191 if (!space)
192 goto error;
193 if (isl_space_is_map(space))
194 isl_die(isl_space_get_ctx(space), isl_error_invalid,
195 "expecting (parameter) set space", goto error);
196 if (pos >= isl_local_space_dim(ls, type))
197 isl_die(isl_space_get_ctx(space), isl_error_invalid,
198 "position out of bounds", goto error);
200 isl_space_free(space);
201 aff = isl_aff_alloc(ls);
202 if (!aff)
203 return NULL;
205 pos += isl_local_space_offset(aff->ls, type);
207 isl_int_set_si(aff->v->el[0], 1);
208 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
209 isl_int_set_si(aff->v->el[1 + pos], 1);
211 return aff;
212 error:
213 isl_local_space_free(ls);
214 isl_space_free(space);
215 return NULL;
218 /* Return a piecewise affine expression that is equal to
219 * the specified dimension in "ls".
221 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
222 enum isl_dim_type type, unsigned pos)
224 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
227 /* Return an affine expression that is equal to the parameter
228 * in the domain space "space" with identifier "id".
230 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
231 __isl_take isl_space *space, __isl_take isl_id *id)
233 int pos;
234 isl_local_space *ls;
236 if (!space || !id)
237 goto error;
238 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
239 if (pos < 0)
240 isl_die(isl_space_get_ctx(space), isl_error_invalid,
241 "parameter not found in space", goto error);
242 isl_id_free(id);
243 ls = isl_local_space_from_space(space);
244 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
245 error:
246 isl_space_free(space);
247 isl_id_free(id);
248 return NULL;
251 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
253 if (!aff)
254 return NULL;
256 aff->ref++;
257 return aff;
260 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
262 if (!aff)
263 return NULL;
265 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
266 isl_vec_copy(aff->v));
269 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
271 if (!aff)
272 return NULL;
274 if (aff->ref == 1)
275 return aff;
276 aff->ref--;
277 return isl_aff_dup(aff);
280 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
282 if (!aff)
283 return NULL;
285 if (--aff->ref > 0)
286 return NULL;
288 isl_local_space_free(aff->ls);
289 isl_vec_free(aff->v);
291 free(aff);
293 return NULL;
296 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
298 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
301 /* Return a hash value that digests "aff".
303 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
305 uint32_t hash, ls_hash, v_hash;
307 if (!aff)
308 return 0;
310 hash = isl_hash_init();
311 ls_hash = isl_local_space_get_hash(aff->ls);
312 isl_hash_hash(hash, ls_hash);
313 v_hash = isl_vec_get_hash(aff->v);
314 isl_hash_hash(hash, v_hash);
316 return hash;
319 /* Externally, an isl_aff has a map space, but internally, the
320 * ls field corresponds to the domain of that space.
322 int isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
324 if (!aff)
325 return 0;
326 if (type == isl_dim_out)
327 return 1;
328 if (type == isl_dim_in)
329 type = isl_dim_set;
330 return isl_local_space_dim(aff->ls, type);
333 /* Return the position of the dimension of the given type and name
334 * in "aff".
335 * Return -1 if no such dimension can be found.
337 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
338 const char *name)
340 if (!aff)
341 return -1;
342 if (type == isl_dim_out)
343 return -1;
344 if (type == isl_dim_in)
345 type = isl_dim_set;
346 return isl_local_space_find_dim_by_name(aff->ls, type, name);
349 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
351 return aff ? isl_local_space_get_space(aff->ls) : NULL;
354 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
356 isl_space *space;
357 if (!aff)
358 return NULL;
359 space = isl_local_space_get_space(aff->ls);
360 space = isl_space_from_domain(space);
361 space = isl_space_add_dims(space, isl_dim_out, 1);
362 return space;
365 __isl_give isl_local_space *isl_aff_get_domain_local_space(
366 __isl_keep isl_aff *aff)
368 return aff ? isl_local_space_copy(aff->ls) : NULL;
371 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
373 isl_local_space *ls;
374 if (!aff)
375 return NULL;
376 ls = isl_local_space_copy(aff->ls);
377 ls = isl_local_space_from_domain(ls);
378 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
379 return ls;
382 /* Return the local space of the domain of "aff".
383 * This may be either a copy or the local space itself
384 * if there is only one reference to "aff".
385 * This allows the local space to be modified inplace
386 * if both the expression and its local space have only a single reference.
387 * The caller is not allowed to modify "aff" between this call and
388 * a subsequent call to isl_aff_restore_domain_local_space.
389 * The only exception is that isl_aff_free can be called instead.
391 __isl_give isl_local_space *isl_aff_take_domain_local_space(
392 __isl_keep isl_aff *aff)
394 isl_local_space *ls;
396 if (!aff)
397 return NULL;
398 if (aff->ref != 1)
399 return isl_aff_get_domain_local_space(aff);
400 ls = aff->ls;
401 aff->ls = NULL;
402 return ls;
405 /* Set the local space of the domain of "aff" to "ls",
406 * where the local space of "aff" may be missing
407 * due to a preceding call to isl_aff_take_domain_local_space.
408 * However, in this case, "aff" only has a single reference and
409 * then the call to isl_aff_cow has no effect.
411 __isl_give isl_aff *isl_aff_restore_domain_local_space(
412 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
414 if (!aff || !ls)
415 goto error;
417 if (aff->ls == ls) {
418 isl_local_space_free(ls);
419 return aff;
422 aff = isl_aff_cow(aff);
423 if (!aff)
424 goto error;
425 isl_local_space_free(aff->ls);
426 aff->ls = ls;
428 return aff;
429 error:
430 isl_aff_free(aff);
431 isl_local_space_free(ls);
432 return NULL;
435 /* Externally, an isl_aff has a map space, but internally, the
436 * ls field corresponds to the domain of that space.
438 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
439 enum isl_dim_type type, unsigned pos)
441 if (!aff)
442 return NULL;
443 if (type == isl_dim_out)
444 return NULL;
445 if (type == isl_dim_in)
446 type = isl_dim_set;
447 return isl_local_space_get_dim_name(aff->ls, type, pos);
450 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
451 __isl_take isl_space *dim)
453 aff = isl_aff_cow(aff);
454 if (!aff || !dim)
455 goto error;
457 aff->ls = isl_local_space_reset_space(aff->ls, dim);
458 if (!aff->ls)
459 return isl_aff_free(aff);
461 return aff;
462 error:
463 isl_aff_free(aff);
464 isl_space_free(dim);
465 return NULL;
468 /* Reset the space of "aff". This function is called from isl_pw_templ.c
469 * and doesn't know if the space of an element object is represented
470 * directly or through its domain. It therefore passes along both.
472 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
473 __isl_take isl_space *space, __isl_take isl_space *domain)
475 isl_space_free(space);
476 return isl_aff_reset_domain_space(aff, domain);
479 /* Reorder the coefficients of the affine expression based
480 * on the given reordering.
481 * The reordering r is assumed to have been extended with the local
482 * variables.
484 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
485 __isl_take isl_reordering *r, int n_div)
487 isl_vec *res;
488 int i;
490 if (!vec || !r)
491 goto error;
493 res = isl_vec_alloc(vec->ctx,
494 2 + isl_space_dim(r->dim, isl_dim_all) + n_div);
495 if (!res)
496 goto error;
497 isl_seq_cpy(res->el, vec->el, 2);
498 isl_seq_clr(res->el + 2, res->size - 2);
499 for (i = 0; i < r->len; ++i)
500 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
502 isl_reordering_free(r);
503 isl_vec_free(vec);
504 return res;
505 error:
506 isl_vec_free(vec);
507 isl_reordering_free(r);
508 return NULL;
511 /* Reorder the dimensions of the domain of "aff" according
512 * to the given reordering.
514 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
515 __isl_take isl_reordering *r)
517 aff = isl_aff_cow(aff);
518 if (!aff)
519 goto error;
521 r = isl_reordering_extend(r, aff->ls->div->n_row);
522 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
523 aff->ls->div->n_row);
524 aff->ls = isl_local_space_realign(aff->ls, r);
526 if (!aff->v || !aff->ls)
527 return isl_aff_free(aff);
529 return aff;
530 error:
531 isl_aff_free(aff);
532 isl_reordering_free(r);
533 return NULL;
536 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
537 __isl_take isl_space *model)
539 isl_bool equal_params;
541 if (!aff || !model)
542 goto error;
544 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
545 if (equal_params < 0)
546 goto error;
547 if (!equal_params) {
548 isl_reordering *exp;
550 model = isl_space_drop_dims(model, isl_dim_in,
551 0, isl_space_dim(model, isl_dim_in));
552 model = isl_space_drop_dims(model, isl_dim_out,
553 0, isl_space_dim(model, isl_dim_out));
554 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
555 exp = isl_reordering_extend_space(exp,
556 isl_aff_get_domain_space(aff));
557 aff = isl_aff_realign_domain(aff, exp);
560 isl_space_free(model);
561 return aff;
562 error:
563 isl_space_free(model);
564 isl_aff_free(aff);
565 return NULL;
568 /* Is "aff" obviously equal to zero?
570 * If the denominator is zero, then "aff" is not equal to zero.
572 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
574 if (!aff)
575 return isl_bool_error;
577 if (isl_int_is_zero(aff->v->el[0]))
578 return isl_bool_false;
579 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
582 /* Does "aff" represent NaN?
584 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
586 if (!aff)
587 return isl_bool_error;
589 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
592 /* Are "aff1" and "aff2" obviously equal?
594 * NaN is not equal to anything, not even to another NaN.
596 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
597 __isl_keep isl_aff *aff2)
599 isl_bool equal;
601 if (!aff1 || !aff2)
602 return isl_bool_error;
604 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
605 return isl_bool_false;
607 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
608 if (equal < 0 || !equal)
609 return equal;
611 return isl_vec_is_equal(aff1->v, aff2->v);
614 /* Return the common denominator of "aff" in "v".
616 * We cannot return anything meaningful in case of a NaN.
618 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
620 if (!aff)
621 return isl_stat_error;
622 if (isl_aff_is_nan(aff))
623 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
624 "cannot get denominator of NaN", return isl_stat_error);
625 isl_int_set(*v, aff->v->el[0]);
626 return isl_stat_ok;
629 /* Return the common denominator of "aff".
631 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
633 isl_ctx *ctx;
635 if (!aff)
636 return NULL;
638 ctx = isl_aff_get_ctx(aff);
639 if (isl_aff_is_nan(aff))
640 return isl_val_nan(ctx);
641 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
644 /* Return the constant term of "aff".
646 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
648 isl_ctx *ctx;
649 isl_val *v;
651 if (!aff)
652 return NULL;
654 ctx = isl_aff_get_ctx(aff);
655 if (isl_aff_is_nan(aff))
656 return isl_val_nan(ctx);
657 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
658 return isl_val_normalize(v);
661 /* Return the coefficient of the variable of type "type" at position "pos"
662 * of "aff".
664 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
665 enum isl_dim_type type, int pos)
667 isl_ctx *ctx;
668 isl_val *v;
670 if (!aff)
671 return NULL;
673 ctx = isl_aff_get_ctx(aff);
674 if (type == isl_dim_out)
675 isl_die(ctx, isl_error_invalid,
676 "output/set dimension does not have a coefficient",
677 return NULL);
678 if (type == isl_dim_in)
679 type = isl_dim_set;
681 if (pos >= isl_local_space_dim(aff->ls, type))
682 isl_die(ctx, isl_error_invalid,
683 "position out of bounds", return NULL);
685 if (isl_aff_is_nan(aff))
686 return isl_val_nan(ctx);
687 pos += isl_local_space_offset(aff->ls, type);
688 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
689 return isl_val_normalize(v);
692 /* Return the sign of the coefficient of the variable of type "type"
693 * at position "pos" of "aff".
695 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
696 int pos)
698 isl_ctx *ctx;
700 if (!aff)
701 return 0;
703 ctx = isl_aff_get_ctx(aff);
704 if (type == isl_dim_out)
705 isl_die(ctx, isl_error_invalid,
706 "output/set dimension does not have a coefficient",
707 return 0);
708 if (type == isl_dim_in)
709 type = isl_dim_set;
711 if (pos >= isl_local_space_dim(aff->ls, type))
712 isl_die(ctx, isl_error_invalid,
713 "position out of bounds", return 0);
715 pos += isl_local_space_offset(aff->ls, type);
716 return isl_int_sgn(aff->v->el[1 + pos]);
719 /* Replace the numerator of the constant term of "aff" by "v".
721 * A NaN is unaffected by this operation.
723 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
725 if (!aff)
726 return NULL;
727 if (isl_aff_is_nan(aff))
728 return aff;
729 aff = isl_aff_cow(aff);
730 if (!aff)
731 return NULL;
733 aff->v = isl_vec_cow(aff->v);
734 if (!aff->v)
735 return isl_aff_free(aff);
737 isl_int_set(aff->v->el[1], v);
739 return aff;
742 /* Replace the constant term of "aff" by "v".
744 * A NaN is unaffected by this operation.
746 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
747 __isl_take isl_val *v)
749 if (!aff || !v)
750 goto error;
752 if (isl_aff_is_nan(aff)) {
753 isl_val_free(v);
754 return aff;
757 if (!isl_val_is_rat(v))
758 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
759 "expecting rational value", goto error);
761 if (isl_int_eq(aff->v->el[1], v->n) &&
762 isl_int_eq(aff->v->el[0], v->d)) {
763 isl_val_free(v);
764 return aff;
767 aff = isl_aff_cow(aff);
768 if (!aff)
769 goto error;
770 aff->v = isl_vec_cow(aff->v);
771 if (!aff->v)
772 goto error;
774 if (isl_int_eq(aff->v->el[0], v->d)) {
775 isl_int_set(aff->v->el[1], v->n);
776 } else if (isl_int_is_one(v->d)) {
777 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
778 } else {
779 isl_seq_scale(aff->v->el + 1,
780 aff->v->el + 1, v->d, aff->v->size - 1);
781 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
782 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
783 aff->v = isl_vec_normalize(aff->v);
784 if (!aff->v)
785 goto error;
788 isl_val_free(v);
789 return aff;
790 error:
791 isl_aff_free(aff);
792 isl_val_free(v);
793 return NULL;
796 /* Add "v" to the constant term of "aff".
798 * A NaN is unaffected by this operation.
800 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
802 if (isl_int_is_zero(v))
803 return aff;
805 if (!aff)
806 return NULL;
807 if (isl_aff_is_nan(aff))
808 return aff;
809 aff = isl_aff_cow(aff);
810 if (!aff)
811 return NULL;
813 aff->v = isl_vec_cow(aff->v);
814 if (!aff->v)
815 return isl_aff_free(aff);
817 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
819 return aff;
822 /* Add "v" to the constant term of "aff".
824 * A NaN is unaffected by this operation.
826 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
827 __isl_take isl_val *v)
829 if (!aff || !v)
830 goto error;
832 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
833 isl_val_free(v);
834 return aff;
837 if (!isl_val_is_rat(v))
838 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
839 "expecting rational value", goto error);
841 aff = isl_aff_cow(aff);
842 if (!aff)
843 goto error;
845 aff->v = isl_vec_cow(aff->v);
846 if (!aff->v)
847 goto error;
849 if (isl_int_is_one(v->d)) {
850 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
851 } else if (isl_int_eq(aff->v->el[0], v->d)) {
852 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
853 aff->v = isl_vec_normalize(aff->v);
854 if (!aff->v)
855 goto error;
856 } else {
857 isl_seq_scale(aff->v->el + 1,
858 aff->v->el + 1, v->d, aff->v->size - 1);
859 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
860 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
861 aff->v = isl_vec_normalize(aff->v);
862 if (!aff->v)
863 goto error;
866 isl_val_free(v);
867 return aff;
868 error:
869 isl_aff_free(aff);
870 isl_val_free(v);
871 return NULL;
874 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
876 isl_int t;
878 isl_int_init(t);
879 isl_int_set_si(t, v);
880 aff = isl_aff_add_constant(aff, t);
881 isl_int_clear(t);
883 return aff;
886 /* Add "v" to the numerator of the constant term of "aff".
888 * A NaN is unaffected by this operation.
890 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
892 if (isl_int_is_zero(v))
893 return aff;
895 if (!aff)
896 return NULL;
897 if (isl_aff_is_nan(aff))
898 return aff;
899 aff = isl_aff_cow(aff);
900 if (!aff)
901 return NULL;
903 aff->v = isl_vec_cow(aff->v);
904 if (!aff->v)
905 return isl_aff_free(aff);
907 isl_int_add(aff->v->el[1], aff->v->el[1], v);
909 return aff;
912 /* Add "v" to the numerator of the constant term of "aff".
914 * A NaN is unaffected by this operation.
916 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
918 isl_int t;
920 if (v == 0)
921 return aff;
923 isl_int_init(t);
924 isl_int_set_si(t, v);
925 aff = isl_aff_add_constant_num(aff, t);
926 isl_int_clear(t);
928 return aff;
931 /* Replace the numerator of the constant term of "aff" by "v".
933 * A NaN is unaffected by this operation.
935 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
937 if (!aff)
938 return NULL;
939 if (isl_aff_is_nan(aff))
940 return aff;
941 aff = isl_aff_cow(aff);
942 if (!aff)
943 return NULL;
945 aff->v = isl_vec_cow(aff->v);
946 if (!aff->v)
947 return isl_aff_free(aff);
949 isl_int_set_si(aff->v->el[1], v);
951 return aff;
954 /* Replace the numerator of the coefficient of the variable of type "type"
955 * at position "pos" of "aff" by "v".
957 * A NaN is unaffected by this operation.
959 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
960 enum isl_dim_type type, int pos, isl_int v)
962 if (!aff)
963 return NULL;
965 if (type == isl_dim_out)
966 isl_die(aff->v->ctx, isl_error_invalid,
967 "output/set dimension does not have a coefficient",
968 return isl_aff_free(aff));
969 if (type == isl_dim_in)
970 type = isl_dim_set;
972 if (pos >= isl_local_space_dim(aff->ls, type))
973 isl_die(aff->v->ctx, isl_error_invalid,
974 "position out of bounds", return isl_aff_free(aff));
976 if (isl_aff_is_nan(aff))
977 return aff;
978 aff = isl_aff_cow(aff);
979 if (!aff)
980 return NULL;
982 aff->v = isl_vec_cow(aff->v);
983 if (!aff->v)
984 return isl_aff_free(aff);
986 pos += isl_local_space_offset(aff->ls, type);
987 isl_int_set(aff->v->el[1 + pos], v);
989 return aff;
992 /* Replace the numerator of the coefficient of the variable of type "type"
993 * at position "pos" of "aff" by "v".
995 * A NaN is unaffected by this operation.
997 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
998 enum isl_dim_type type, int pos, int v)
1000 if (!aff)
1001 return NULL;
1003 if (type == isl_dim_out)
1004 isl_die(aff->v->ctx, isl_error_invalid,
1005 "output/set dimension does not have a coefficient",
1006 return isl_aff_free(aff));
1007 if (type == isl_dim_in)
1008 type = isl_dim_set;
1010 if (pos < 0 || pos >= isl_local_space_dim(aff->ls, type))
1011 isl_die(aff->v->ctx, isl_error_invalid,
1012 "position out of bounds", return isl_aff_free(aff));
1014 if (isl_aff_is_nan(aff))
1015 return aff;
1016 pos += isl_local_space_offset(aff->ls, type);
1017 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1018 return aff;
1020 aff = isl_aff_cow(aff);
1021 if (!aff)
1022 return NULL;
1024 aff->v = isl_vec_cow(aff->v);
1025 if (!aff->v)
1026 return isl_aff_free(aff);
1028 isl_int_set_si(aff->v->el[1 + pos], v);
1030 return aff;
1033 /* Replace the coefficient of the variable of type "type" at position "pos"
1034 * of "aff" by "v".
1036 * A NaN is unaffected by this operation.
1038 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1039 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1041 if (!aff || !v)
1042 goto error;
1044 if (type == isl_dim_out)
1045 isl_die(aff->v->ctx, isl_error_invalid,
1046 "output/set dimension does not have a coefficient",
1047 goto error);
1048 if (type == isl_dim_in)
1049 type = isl_dim_set;
1051 if (pos >= isl_local_space_dim(aff->ls, type))
1052 isl_die(aff->v->ctx, isl_error_invalid,
1053 "position out of bounds", goto error);
1055 if (isl_aff_is_nan(aff)) {
1056 isl_val_free(v);
1057 return aff;
1059 if (!isl_val_is_rat(v))
1060 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1061 "expecting rational value", goto error);
1063 pos += isl_local_space_offset(aff->ls, type);
1064 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1065 isl_int_eq(aff->v->el[0], v->d)) {
1066 isl_val_free(v);
1067 return aff;
1070 aff = isl_aff_cow(aff);
1071 if (!aff)
1072 goto error;
1073 aff->v = isl_vec_cow(aff->v);
1074 if (!aff->v)
1075 goto error;
1077 if (isl_int_eq(aff->v->el[0], v->d)) {
1078 isl_int_set(aff->v->el[1 + pos], v->n);
1079 } else if (isl_int_is_one(v->d)) {
1080 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1081 } else {
1082 isl_seq_scale(aff->v->el + 1,
1083 aff->v->el + 1, v->d, aff->v->size - 1);
1084 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1085 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1086 aff->v = isl_vec_normalize(aff->v);
1087 if (!aff->v)
1088 goto error;
1091 isl_val_free(v);
1092 return aff;
1093 error:
1094 isl_aff_free(aff);
1095 isl_val_free(v);
1096 return NULL;
1099 /* Add "v" to the coefficient of the variable of type "type"
1100 * at position "pos" of "aff".
1102 * A NaN is unaffected by this operation.
1104 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1105 enum isl_dim_type type, int pos, isl_int v)
1107 if (!aff)
1108 return NULL;
1110 if (type == isl_dim_out)
1111 isl_die(aff->v->ctx, isl_error_invalid,
1112 "output/set dimension does not have a coefficient",
1113 return isl_aff_free(aff));
1114 if (type == isl_dim_in)
1115 type = isl_dim_set;
1117 if (pos >= isl_local_space_dim(aff->ls, type))
1118 isl_die(aff->v->ctx, isl_error_invalid,
1119 "position out of bounds", return isl_aff_free(aff));
1121 if (isl_aff_is_nan(aff))
1122 return aff;
1123 aff = isl_aff_cow(aff);
1124 if (!aff)
1125 return NULL;
1127 aff->v = isl_vec_cow(aff->v);
1128 if (!aff->v)
1129 return isl_aff_free(aff);
1131 pos += isl_local_space_offset(aff->ls, type);
1132 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1134 return aff;
1137 /* Add "v" to the coefficient of the variable of type "type"
1138 * at position "pos" of "aff".
1140 * A NaN is unaffected by this operation.
1142 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1143 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1145 if (!aff || !v)
1146 goto error;
1148 if (isl_val_is_zero(v)) {
1149 isl_val_free(v);
1150 return aff;
1153 if (type == isl_dim_out)
1154 isl_die(aff->v->ctx, isl_error_invalid,
1155 "output/set dimension does not have a coefficient",
1156 goto error);
1157 if (type == isl_dim_in)
1158 type = isl_dim_set;
1160 if (pos >= isl_local_space_dim(aff->ls, type))
1161 isl_die(aff->v->ctx, isl_error_invalid,
1162 "position out of bounds", goto error);
1164 if (isl_aff_is_nan(aff)) {
1165 isl_val_free(v);
1166 return aff;
1168 if (!isl_val_is_rat(v))
1169 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1170 "expecting rational value", goto error);
1172 aff = isl_aff_cow(aff);
1173 if (!aff)
1174 goto error;
1176 aff->v = isl_vec_cow(aff->v);
1177 if (!aff->v)
1178 goto error;
1180 pos += isl_local_space_offset(aff->ls, type);
1181 if (isl_int_is_one(v->d)) {
1182 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1183 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1184 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1185 aff->v = isl_vec_normalize(aff->v);
1186 if (!aff->v)
1187 goto error;
1188 } else {
1189 isl_seq_scale(aff->v->el + 1,
1190 aff->v->el + 1, v->d, aff->v->size - 1);
1191 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1192 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1193 aff->v = isl_vec_normalize(aff->v);
1194 if (!aff->v)
1195 goto error;
1198 isl_val_free(v);
1199 return aff;
1200 error:
1201 isl_aff_free(aff);
1202 isl_val_free(v);
1203 return NULL;
1206 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1207 enum isl_dim_type type, int pos, int v)
1209 isl_int t;
1211 isl_int_init(t);
1212 isl_int_set_si(t, v);
1213 aff = isl_aff_add_coefficient(aff, type, pos, t);
1214 isl_int_clear(t);
1216 return aff;
1219 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1221 if (!aff)
1222 return NULL;
1224 return isl_local_space_get_div(aff->ls, pos);
1227 /* Return the negation of "aff".
1229 * As a special case, -NaN = NaN.
1231 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1233 if (!aff)
1234 return NULL;
1235 if (isl_aff_is_nan(aff))
1236 return aff;
1237 aff = isl_aff_cow(aff);
1238 if (!aff)
1239 return NULL;
1240 aff->v = isl_vec_cow(aff->v);
1241 if (!aff->v)
1242 return isl_aff_free(aff);
1244 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1246 return aff;
1249 /* Remove divs from the local space that do not appear in the affine
1250 * expression.
1251 * We currently only remove divs at the end.
1252 * Some intermediate divs may also not appear directly in the affine
1253 * expression, but we would also need to check that no other divs are
1254 * defined in terms of them.
1256 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1258 int pos;
1259 int off;
1260 int n;
1262 if (!aff)
1263 return NULL;
1265 n = isl_local_space_dim(aff->ls, isl_dim_div);
1266 off = isl_local_space_offset(aff->ls, isl_dim_div);
1268 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1269 if (pos == n)
1270 return aff;
1272 aff = isl_aff_cow(aff);
1273 if (!aff)
1274 return NULL;
1276 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1277 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1278 if (!aff->ls || !aff->v)
1279 return isl_aff_free(aff);
1281 return aff;
1284 /* Look for any divs in the aff->ls with a denominator equal to one
1285 * and plug them into the affine expression and any subsequent divs
1286 * that may reference the div.
1288 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1290 int i, n;
1291 int len;
1292 isl_int v;
1293 isl_vec *vec;
1294 isl_local_space *ls;
1295 unsigned pos;
1297 if (!aff)
1298 return NULL;
1300 n = isl_local_space_dim(aff->ls, isl_dim_div);
1301 len = aff->v->size;
1302 for (i = 0; i < n; ++i) {
1303 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1304 continue;
1305 ls = isl_local_space_copy(aff->ls);
1306 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1307 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1308 vec = isl_vec_copy(aff->v);
1309 vec = isl_vec_cow(vec);
1310 if (!ls || !vec)
1311 goto error;
1313 isl_int_init(v);
1315 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1316 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1317 len, len, v);
1319 isl_int_clear(v);
1321 isl_vec_free(aff->v);
1322 aff->v = vec;
1323 isl_local_space_free(aff->ls);
1324 aff->ls = ls;
1327 return aff;
1328 error:
1329 isl_vec_free(vec);
1330 isl_local_space_free(ls);
1331 return isl_aff_free(aff);
1334 /* Look for any divs j that appear with a unit coefficient inside
1335 * the definitions of other divs i and plug them into the definitions
1336 * of the divs i.
1338 * In particular, an expression of the form
1340 * floor((f(..) + floor(g(..)/n))/m)
1342 * is simplified to
1344 * floor((n * f(..) + g(..))/(n * m))
1346 * This simplification is correct because we can move the expression
1347 * f(..) into the inner floor in the original expression to obtain
1349 * floor(floor((n * f(..) + g(..))/n)/m)
1351 * from which we can derive the simplified expression.
1353 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1355 int i, j, n;
1356 int off;
1358 if (!aff)
1359 return NULL;
1361 n = isl_local_space_dim(aff->ls, isl_dim_div);
1362 off = isl_local_space_offset(aff->ls, isl_dim_div);
1363 for (i = 1; i < n; ++i) {
1364 for (j = 0; j < i; ++j) {
1365 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1366 continue;
1367 aff->ls = isl_local_space_substitute_seq(aff->ls,
1368 isl_dim_div, j, aff->ls->div->row[j],
1369 aff->v->size, i, 1);
1370 if (!aff->ls)
1371 return isl_aff_free(aff);
1375 return aff;
1378 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1380 * Even though this function is only called on isl_affs with a single
1381 * reference, we are careful to only change aff->v and aff->ls together.
1383 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1385 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1386 isl_local_space *ls;
1387 isl_vec *v;
1389 ls = isl_local_space_copy(aff->ls);
1390 ls = isl_local_space_swap_div(ls, a, b);
1391 v = isl_vec_copy(aff->v);
1392 v = isl_vec_cow(v);
1393 if (!ls || !v)
1394 goto error;
1396 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1397 isl_vec_free(aff->v);
1398 aff->v = v;
1399 isl_local_space_free(aff->ls);
1400 aff->ls = ls;
1402 return aff;
1403 error:
1404 isl_vec_free(v);
1405 isl_local_space_free(ls);
1406 return isl_aff_free(aff);
1409 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1411 * We currently do not actually remove div "b", but simply add its
1412 * coefficient to that of "a" and then zero it out.
1414 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1416 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1418 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1419 return aff;
1421 aff->v = isl_vec_cow(aff->v);
1422 if (!aff->v)
1423 return isl_aff_free(aff);
1425 isl_int_add(aff->v->el[1 + off + a],
1426 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1427 isl_int_set_si(aff->v->el[1 + off + b], 0);
1429 return aff;
1432 /* Sort the divs in the local space of "aff" according to
1433 * the comparison function "cmp_row" in isl_local_space.c,
1434 * combining the coefficients of identical divs.
1436 * Reordering divs does not change the semantics of "aff",
1437 * so there is no need to call isl_aff_cow.
1438 * Moreover, this function is currently only called on isl_affs
1439 * with a single reference.
1441 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1443 int i, j, n;
1445 if (!aff)
1446 return NULL;
1448 n = isl_aff_dim(aff, isl_dim_div);
1449 for (i = 1; i < n; ++i) {
1450 for (j = i - 1; j >= 0; --j) {
1451 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1452 if (cmp < 0)
1453 break;
1454 if (cmp == 0)
1455 aff = merge_divs(aff, j, j + 1);
1456 else
1457 aff = swap_div(aff, j, j + 1);
1458 if (!aff)
1459 return NULL;
1463 return aff;
1466 /* Normalize the representation of "aff".
1468 * This function should only be called of "new" isl_affs, i.e.,
1469 * with only a single reference. We therefore do not need to
1470 * worry about affecting other instances.
1472 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1474 if (!aff)
1475 return NULL;
1476 aff->v = isl_vec_normalize(aff->v);
1477 if (!aff->v)
1478 return isl_aff_free(aff);
1479 aff = plug_in_integral_divs(aff);
1480 aff = plug_in_unit_divs(aff);
1481 aff = sort_divs(aff);
1482 aff = isl_aff_remove_unused_divs(aff);
1483 return aff;
1486 /* Given f, return floor(f).
1487 * If f is an integer expression, then just return f.
1488 * If f is a constant, then return the constant floor(f).
1489 * Otherwise, if f = g/m, write g = q m + r,
1490 * create a new div d = [r/m] and return the expression q + d.
1491 * The coefficients in r are taken to lie between -m/2 and m/2.
1493 * reduce_div_coefficients performs the same normalization.
1495 * As a special case, floor(NaN) = NaN.
1497 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1499 int i;
1500 int size;
1501 isl_ctx *ctx;
1502 isl_vec *div;
1504 if (!aff)
1505 return NULL;
1507 if (isl_aff_is_nan(aff))
1508 return aff;
1509 if (isl_int_is_one(aff->v->el[0]))
1510 return aff;
1512 aff = isl_aff_cow(aff);
1513 if (!aff)
1514 return NULL;
1516 aff->v = isl_vec_cow(aff->v);
1517 if (!aff->v)
1518 return isl_aff_free(aff);
1520 if (isl_aff_is_cst(aff)) {
1521 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1522 isl_int_set_si(aff->v->el[0], 1);
1523 return aff;
1526 div = isl_vec_copy(aff->v);
1527 div = isl_vec_cow(div);
1528 if (!div)
1529 return isl_aff_free(aff);
1531 ctx = isl_aff_get_ctx(aff);
1532 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1533 for (i = 1; i < aff->v->size; ++i) {
1534 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1535 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1536 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1537 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1538 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1542 aff->ls = isl_local_space_add_div(aff->ls, div);
1543 if (!aff->ls)
1544 return isl_aff_free(aff);
1546 size = aff->v->size;
1547 aff->v = isl_vec_extend(aff->v, size + 1);
1548 if (!aff->v)
1549 return isl_aff_free(aff);
1550 isl_int_set_si(aff->v->el[0], 1);
1551 isl_int_set_si(aff->v->el[size], 1);
1553 aff = isl_aff_normalize(aff);
1555 return aff;
1558 /* Compute
1560 * aff mod m = aff - m * floor(aff/m)
1562 * with m an integer value.
1564 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1565 __isl_take isl_val *m)
1567 isl_aff *res;
1569 if (!aff || !m)
1570 goto error;
1572 if (!isl_val_is_int(m))
1573 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1574 "expecting integer modulo", goto error);
1576 res = isl_aff_copy(aff);
1577 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1578 aff = isl_aff_floor(aff);
1579 aff = isl_aff_scale_val(aff, m);
1580 res = isl_aff_sub(res, aff);
1582 return res;
1583 error:
1584 isl_aff_free(aff);
1585 isl_val_free(m);
1586 return NULL;
1589 /* Compute
1591 * pwaff mod m = pwaff - m * floor(pwaff/m)
1593 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1595 isl_pw_aff *res;
1597 res = isl_pw_aff_copy(pwaff);
1598 pwaff = isl_pw_aff_scale_down(pwaff, m);
1599 pwaff = isl_pw_aff_floor(pwaff);
1600 pwaff = isl_pw_aff_scale(pwaff, m);
1601 res = isl_pw_aff_sub(res, pwaff);
1603 return res;
1606 /* Compute
1608 * pa mod m = pa - m * floor(pa/m)
1610 * with m an integer value.
1612 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1613 __isl_take isl_val *m)
1615 if (!pa || !m)
1616 goto error;
1617 if (!isl_val_is_int(m))
1618 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1619 "expecting integer modulo", goto error);
1620 pa = isl_pw_aff_mod(pa, m->n);
1621 isl_val_free(m);
1622 return pa;
1623 error:
1624 isl_pw_aff_free(pa);
1625 isl_val_free(m);
1626 return NULL;
1629 /* Given f, return ceil(f).
1630 * If f is an integer expression, then just return f.
1631 * Otherwise, let f be the expression
1633 * e/m
1635 * then return
1637 * floor((e + m - 1)/m)
1639 * As a special case, ceil(NaN) = NaN.
1641 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1643 if (!aff)
1644 return NULL;
1646 if (isl_aff_is_nan(aff))
1647 return aff;
1648 if (isl_int_is_one(aff->v->el[0]))
1649 return aff;
1651 aff = isl_aff_cow(aff);
1652 if (!aff)
1653 return NULL;
1654 aff->v = isl_vec_cow(aff->v);
1655 if (!aff->v)
1656 return isl_aff_free(aff);
1658 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1659 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1660 aff = isl_aff_floor(aff);
1662 return aff;
1665 /* Apply the expansion computed by isl_merge_divs.
1666 * The expansion itself is given by "exp" while the resulting
1667 * list of divs is given by "div".
1669 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1670 __isl_take isl_mat *div, int *exp)
1672 int old_n_div;
1673 int new_n_div;
1674 int offset;
1676 aff = isl_aff_cow(aff);
1677 if (!aff || !div)
1678 goto error;
1680 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1681 new_n_div = isl_mat_rows(div);
1682 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1684 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1685 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1686 if (!aff->v || !aff->ls)
1687 return isl_aff_free(aff);
1688 return aff;
1689 error:
1690 isl_aff_free(aff);
1691 isl_mat_free(div);
1692 return NULL;
1695 /* Add two affine expressions that live in the same local space.
1697 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1698 __isl_take isl_aff *aff2)
1700 isl_int gcd, f;
1702 aff1 = isl_aff_cow(aff1);
1703 if (!aff1 || !aff2)
1704 goto error;
1706 aff1->v = isl_vec_cow(aff1->v);
1707 if (!aff1->v)
1708 goto error;
1710 isl_int_init(gcd);
1711 isl_int_init(f);
1712 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1713 isl_int_divexact(f, aff2->v->el[0], gcd);
1714 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1715 isl_int_divexact(f, aff1->v->el[0], gcd);
1716 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1717 isl_int_divexact(f, aff2->v->el[0], gcd);
1718 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1719 isl_int_clear(f);
1720 isl_int_clear(gcd);
1722 isl_aff_free(aff2);
1723 return aff1;
1724 error:
1725 isl_aff_free(aff1);
1726 isl_aff_free(aff2);
1727 return NULL;
1730 /* Return the sum of "aff1" and "aff2".
1732 * If either of the two is NaN, then the result is NaN.
1734 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1735 __isl_take isl_aff *aff2)
1737 isl_ctx *ctx;
1738 int *exp1 = NULL;
1739 int *exp2 = NULL;
1740 isl_mat *div;
1741 int n_div1, n_div2;
1743 if (!aff1 || !aff2)
1744 goto error;
1746 ctx = isl_aff_get_ctx(aff1);
1747 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1748 isl_die(ctx, isl_error_invalid,
1749 "spaces don't match", goto error);
1751 if (isl_aff_is_nan(aff1)) {
1752 isl_aff_free(aff2);
1753 return aff1;
1755 if (isl_aff_is_nan(aff2)) {
1756 isl_aff_free(aff1);
1757 return aff2;
1760 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1761 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1762 if (n_div1 == 0 && n_div2 == 0)
1763 return add_expanded(aff1, aff2);
1765 exp1 = isl_alloc_array(ctx, int, n_div1);
1766 exp2 = isl_alloc_array(ctx, int, n_div2);
1767 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1768 goto error;
1770 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1771 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1772 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1773 free(exp1);
1774 free(exp2);
1776 return add_expanded(aff1, aff2);
1777 error:
1778 free(exp1);
1779 free(exp2);
1780 isl_aff_free(aff1);
1781 isl_aff_free(aff2);
1782 return NULL;
1785 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1786 __isl_take isl_aff *aff2)
1788 return isl_aff_add(aff1, isl_aff_neg(aff2));
1791 /* Return the result of scaling "aff" by a factor of "f".
1793 * As a special case, f * NaN = NaN.
1795 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1797 isl_int gcd;
1799 if (!aff)
1800 return NULL;
1801 if (isl_aff_is_nan(aff))
1802 return aff;
1804 if (isl_int_is_one(f))
1805 return aff;
1807 aff = isl_aff_cow(aff);
1808 if (!aff)
1809 return NULL;
1810 aff->v = isl_vec_cow(aff->v);
1811 if (!aff->v)
1812 return isl_aff_free(aff);
1814 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1815 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1816 return aff;
1819 isl_int_init(gcd);
1820 isl_int_gcd(gcd, aff->v->el[0], f);
1821 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1822 isl_int_divexact(gcd, f, gcd);
1823 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1824 isl_int_clear(gcd);
1826 return aff;
1829 /* Multiple "aff" by "v".
1831 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1832 __isl_take isl_val *v)
1834 if (!aff || !v)
1835 goto error;
1837 if (isl_val_is_one(v)) {
1838 isl_val_free(v);
1839 return aff;
1842 if (!isl_val_is_rat(v))
1843 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1844 "expecting rational factor", goto error);
1846 aff = isl_aff_scale(aff, v->n);
1847 aff = isl_aff_scale_down(aff, v->d);
1849 isl_val_free(v);
1850 return aff;
1851 error:
1852 isl_aff_free(aff);
1853 isl_val_free(v);
1854 return NULL;
1857 /* Return the result of scaling "aff" down by a factor of "f".
1859 * As a special case, NaN/f = NaN.
1861 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1863 isl_int gcd;
1865 if (!aff)
1866 return NULL;
1867 if (isl_aff_is_nan(aff))
1868 return aff;
1870 if (isl_int_is_one(f))
1871 return aff;
1873 aff = isl_aff_cow(aff);
1874 if (!aff)
1875 return NULL;
1877 if (isl_int_is_zero(f))
1878 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1879 "cannot scale down by zero", return isl_aff_free(aff));
1881 aff->v = isl_vec_cow(aff->v);
1882 if (!aff->v)
1883 return isl_aff_free(aff);
1885 isl_int_init(gcd);
1886 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1887 isl_int_gcd(gcd, gcd, f);
1888 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1889 isl_int_divexact(gcd, f, gcd);
1890 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1891 isl_int_clear(gcd);
1893 return aff;
1896 /* Divide "aff" by "v".
1898 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1899 __isl_take isl_val *v)
1901 if (!aff || !v)
1902 goto error;
1904 if (isl_val_is_one(v)) {
1905 isl_val_free(v);
1906 return aff;
1909 if (!isl_val_is_rat(v))
1910 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1911 "expecting rational factor", goto error);
1912 if (!isl_val_is_pos(v))
1913 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1914 "factor needs to be positive", goto error);
1916 aff = isl_aff_scale(aff, v->d);
1917 aff = isl_aff_scale_down(aff, v->n);
1919 isl_val_free(v);
1920 return aff;
1921 error:
1922 isl_aff_free(aff);
1923 isl_val_free(v);
1924 return NULL;
1927 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1929 isl_int v;
1931 if (f == 1)
1932 return aff;
1934 isl_int_init(v);
1935 isl_int_set_ui(v, f);
1936 aff = isl_aff_scale_down(aff, v);
1937 isl_int_clear(v);
1939 return aff;
1942 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1943 enum isl_dim_type type, unsigned pos, const char *s)
1945 aff = isl_aff_cow(aff);
1946 if (!aff)
1947 return NULL;
1948 if (type == isl_dim_out)
1949 isl_die(aff->v->ctx, isl_error_invalid,
1950 "cannot set name of output/set dimension",
1951 return isl_aff_free(aff));
1952 if (type == isl_dim_in)
1953 type = isl_dim_set;
1954 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1955 if (!aff->ls)
1956 return isl_aff_free(aff);
1958 return aff;
1961 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1962 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1964 aff = isl_aff_cow(aff);
1965 if (!aff)
1966 goto error;
1967 if (type == isl_dim_out)
1968 isl_die(aff->v->ctx, isl_error_invalid,
1969 "cannot set name of output/set dimension",
1970 goto error);
1971 if (type == isl_dim_in)
1972 type = isl_dim_set;
1973 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1974 if (!aff->ls)
1975 return isl_aff_free(aff);
1977 return aff;
1978 error:
1979 isl_id_free(id);
1980 isl_aff_free(aff);
1981 return NULL;
1984 /* Replace the identifier of the input tuple of "aff" by "id".
1985 * type is currently required to be equal to isl_dim_in
1987 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
1988 enum isl_dim_type type, __isl_take isl_id *id)
1990 aff = isl_aff_cow(aff);
1991 if (!aff)
1992 goto error;
1993 if (type != isl_dim_out)
1994 isl_die(aff->v->ctx, isl_error_invalid,
1995 "cannot only set id of input tuple", goto error);
1996 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
1997 if (!aff->ls)
1998 return isl_aff_free(aff);
2000 return aff;
2001 error:
2002 isl_id_free(id);
2003 isl_aff_free(aff);
2004 return NULL;
2007 /* Exploit the equalities in "eq" to simplify the affine expression
2008 * and the expressions of the integer divisions in the local space.
2009 * The integer divisions in this local space are assumed to appear
2010 * as regular dimensions in "eq".
2012 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2013 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2015 int i, j;
2016 unsigned total;
2017 unsigned n_div;
2019 if (!eq)
2020 goto error;
2021 if (eq->n_eq == 0) {
2022 isl_basic_set_free(eq);
2023 return aff;
2026 aff = isl_aff_cow(aff);
2027 if (!aff)
2028 goto error;
2030 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2031 isl_basic_set_copy(eq));
2032 aff->v = isl_vec_cow(aff->v);
2033 if (!aff->ls || !aff->v)
2034 goto error;
2036 total = 1 + isl_space_dim(eq->dim, isl_dim_all);
2037 n_div = eq->n_div;
2038 for (i = 0; i < eq->n_eq; ++i) {
2039 j = isl_seq_last_non_zero(eq->eq[i], total + n_div);
2040 if (j < 0 || j == 0 || j >= total)
2041 continue;
2043 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, total,
2044 &aff->v->el[0]);
2047 isl_basic_set_free(eq);
2048 aff = isl_aff_normalize(aff);
2049 return aff;
2050 error:
2051 isl_basic_set_free(eq);
2052 isl_aff_free(aff);
2053 return NULL;
2056 /* Exploit the equalities in "eq" to simplify the affine expression
2057 * and the expressions of the integer divisions in the local space.
2059 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2060 __isl_take isl_basic_set *eq)
2062 int n_div;
2064 if (!aff || !eq)
2065 goto error;
2066 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2067 if (n_div > 0)
2068 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2069 return isl_aff_substitute_equalities_lifted(aff, eq);
2070 error:
2071 isl_basic_set_free(eq);
2072 isl_aff_free(aff);
2073 return NULL;
2076 /* Look for equalities among the variables shared by context and aff
2077 * and the integer divisions of aff, if any.
2078 * The equalities are then used to eliminate coefficients and/or integer
2079 * divisions from aff.
2081 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2082 __isl_take isl_set *context)
2084 isl_basic_set *hull;
2085 int n_div;
2087 if (!aff)
2088 goto error;
2089 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2090 if (n_div > 0) {
2091 isl_basic_set *bset;
2092 isl_local_space *ls;
2093 context = isl_set_add_dims(context, isl_dim_set, n_div);
2094 ls = isl_aff_get_domain_local_space(aff);
2095 bset = isl_basic_set_from_local_space(ls);
2096 bset = isl_basic_set_lift(bset);
2097 bset = isl_basic_set_flatten(bset);
2098 context = isl_set_intersect(context,
2099 isl_set_from_basic_set(bset));
2102 hull = isl_set_affine_hull(context);
2103 return isl_aff_substitute_equalities_lifted(aff, hull);
2104 error:
2105 isl_aff_free(aff);
2106 isl_set_free(context);
2107 return NULL;
2110 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2111 __isl_take isl_set *context)
2113 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2114 dom_context = isl_set_intersect_params(dom_context, context);
2115 return isl_aff_gist(aff, dom_context);
2118 /* Return a basic set containing those elements in the space
2119 * of aff where it is positive. "rational" should not be set.
2121 * If "aff" is NaN, then it is not positive.
2123 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2124 int rational)
2126 isl_constraint *ineq;
2127 isl_basic_set *bset;
2128 isl_val *c;
2130 if (!aff)
2131 return NULL;
2132 if (isl_aff_is_nan(aff)) {
2133 isl_space *space = isl_aff_get_domain_space(aff);
2134 isl_aff_free(aff);
2135 return isl_basic_set_empty(space);
2137 if (rational)
2138 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2139 "rational sets not supported", goto error);
2141 ineq = isl_inequality_from_aff(aff);
2142 c = isl_constraint_get_constant_val(ineq);
2143 c = isl_val_sub_ui(c, 1);
2144 ineq = isl_constraint_set_constant_val(ineq, c);
2146 bset = isl_basic_set_from_constraint(ineq);
2147 bset = isl_basic_set_simplify(bset);
2148 return bset;
2149 error:
2150 isl_aff_free(aff);
2151 return NULL;
2154 /* Return a basic set containing those elements in the space
2155 * of aff where it is non-negative.
2156 * If "rational" is set, then return a rational basic set.
2158 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2160 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2161 __isl_take isl_aff *aff, int rational)
2163 isl_constraint *ineq;
2164 isl_basic_set *bset;
2166 if (!aff)
2167 return NULL;
2168 if (isl_aff_is_nan(aff)) {
2169 isl_space *space = isl_aff_get_domain_space(aff);
2170 isl_aff_free(aff);
2171 return isl_basic_set_empty(space);
2174 ineq = isl_inequality_from_aff(aff);
2176 bset = isl_basic_set_from_constraint(ineq);
2177 if (rational)
2178 bset = isl_basic_set_set_rational(bset);
2179 bset = isl_basic_set_simplify(bset);
2180 return bset;
2183 /* Return a basic set containing those elements in the space
2184 * of aff where it is non-negative.
2186 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2188 return aff_nonneg_basic_set(aff, 0);
2191 /* Return a basic set containing those elements in the domain space
2192 * of "aff" where it is positive.
2194 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2196 aff = isl_aff_add_constant_num_si(aff, -1);
2197 return isl_aff_nonneg_basic_set(aff);
2200 /* Return a basic set containing those elements in the domain space
2201 * of aff where it is negative.
2203 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2205 aff = isl_aff_neg(aff);
2206 return isl_aff_pos_basic_set(aff);
2209 /* Return a basic set containing those elements in the space
2210 * of aff where it is zero.
2211 * If "rational" is set, then return a rational basic set.
2213 * If "aff" is NaN, then it is not zero.
2215 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2216 int rational)
2218 isl_constraint *ineq;
2219 isl_basic_set *bset;
2221 if (!aff)
2222 return NULL;
2223 if (isl_aff_is_nan(aff)) {
2224 isl_space *space = isl_aff_get_domain_space(aff);
2225 isl_aff_free(aff);
2226 return isl_basic_set_empty(space);
2229 ineq = isl_equality_from_aff(aff);
2231 bset = isl_basic_set_from_constraint(ineq);
2232 if (rational)
2233 bset = isl_basic_set_set_rational(bset);
2234 bset = isl_basic_set_simplify(bset);
2235 return bset;
2238 /* Return a basic set containing those elements in the space
2239 * of aff where it is zero.
2241 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2243 return aff_zero_basic_set(aff, 0);
2246 /* Return a basic set containing those elements in the shared space
2247 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2249 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2250 __isl_take isl_aff *aff2)
2252 aff1 = isl_aff_sub(aff1, aff2);
2254 return isl_aff_nonneg_basic_set(aff1);
2257 /* Return a basic set containing those elements in the shared domain space
2258 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2260 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2261 __isl_take isl_aff *aff2)
2263 aff1 = isl_aff_sub(aff1, aff2);
2265 return isl_aff_pos_basic_set(aff1);
2268 /* Return a set containing those elements in the shared space
2269 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2271 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2272 __isl_take isl_aff *aff2)
2274 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2277 /* Return a set containing those elements in the shared domain space
2278 * of aff1 and aff2 where aff1 is greater than aff2.
2280 * If either of the two inputs is NaN, then the result is empty,
2281 * as comparisons with NaN always return false.
2283 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2284 __isl_take isl_aff *aff2)
2286 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2289 /* Return a basic set containing those elements in the shared space
2290 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2292 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2293 __isl_take isl_aff *aff2)
2295 return isl_aff_ge_basic_set(aff2, aff1);
2298 /* Return a basic set containing those elements in the shared domain space
2299 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2301 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2302 __isl_take isl_aff *aff2)
2304 return isl_aff_gt_basic_set(aff2, aff1);
2307 /* Return a set containing those elements in the shared space
2308 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2310 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2311 __isl_take isl_aff *aff2)
2313 return isl_aff_ge_set(aff2, aff1);
2316 /* Return a set containing those elements in the shared domain space
2317 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2319 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2320 __isl_take isl_aff *aff2)
2322 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2325 /* Return a basic set containing those elements in the shared space
2326 * of aff1 and aff2 where aff1 and aff2 are equal.
2328 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2329 __isl_take isl_aff *aff2)
2331 aff1 = isl_aff_sub(aff1, aff2);
2333 return isl_aff_zero_basic_set(aff1);
2336 /* Return a set containing those elements in the shared space
2337 * of aff1 and aff2 where aff1 and aff2 are equal.
2339 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2340 __isl_take isl_aff *aff2)
2342 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2345 /* Return a set containing those elements in the shared domain space
2346 * of aff1 and aff2 where aff1 and aff2 are not equal.
2348 * If either of the two inputs is NaN, then the result is empty,
2349 * as comparisons with NaN always return false.
2351 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2352 __isl_take isl_aff *aff2)
2354 isl_set *set_lt, *set_gt;
2356 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2357 isl_aff_copy(aff2));
2358 set_gt = isl_aff_gt_set(aff1, aff2);
2359 return isl_set_union_disjoint(set_lt, set_gt);
2362 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2363 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2365 aff1 = isl_aff_add(aff1, aff2);
2366 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2367 return aff1;
2370 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2372 if (!aff)
2373 return -1;
2375 return 0;
2378 /* Check whether the given affine expression has non-zero coefficient
2379 * for any dimension in the given range or if any of these dimensions
2380 * appear with non-zero coefficients in any of the integer divisions
2381 * involved in the affine expression.
2383 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2384 enum isl_dim_type type, unsigned first, unsigned n)
2386 int i;
2387 isl_ctx *ctx;
2388 int *active = NULL;
2389 isl_bool involves = isl_bool_false;
2391 if (!aff)
2392 return isl_bool_error;
2393 if (n == 0)
2394 return isl_bool_false;
2396 ctx = isl_aff_get_ctx(aff);
2397 if (first + n > isl_aff_dim(aff, type))
2398 isl_die(ctx, isl_error_invalid,
2399 "range out of bounds", return isl_bool_error);
2401 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2402 if (!active)
2403 goto error;
2405 first += isl_local_space_offset(aff->ls, type) - 1;
2406 for (i = 0; i < n; ++i)
2407 if (active[first + i]) {
2408 involves = isl_bool_true;
2409 break;
2412 free(active);
2414 return involves;
2415 error:
2416 free(active);
2417 return isl_bool_error;
2420 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2421 enum isl_dim_type type, unsigned first, unsigned n)
2423 isl_ctx *ctx;
2425 if (!aff)
2426 return NULL;
2427 if (type == isl_dim_out)
2428 isl_die(aff->v->ctx, isl_error_invalid,
2429 "cannot drop output/set dimension",
2430 return isl_aff_free(aff));
2431 if (type == isl_dim_in)
2432 type = isl_dim_set;
2433 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2434 return aff;
2436 ctx = isl_aff_get_ctx(aff);
2437 if (first + n > isl_local_space_dim(aff->ls, type))
2438 isl_die(ctx, isl_error_invalid, "range out of bounds",
2439 return isl_aff_free(aff));
2441 aff = isl_aff_cow(aff);
2442 if (!aff)
2443 return NULL;
2445 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2446 if (!aff->ls)
2447 return isl_aff_free(aff);
2449 first += 1 + isl_local_space_offset(aff->ls, type);
2450 aff->v = isl_vec_drop_els(aff->v, first, n);
2451 if (!aff->v)
2452 return isl_aff_free(aff);
2454 return aff;
2457 /* Project the domain of the affine expression onto its parameter space.
2458 * The affine expression may not involve any of the domain dimensions.
2460 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2462 isl_space *space;
2463 unsigned n;
2464 int involves;
2466 n = isl_aff_dim(aff, isl_dim_in);
2467 involves = isl_aff_involves_dims(aff, isl_dim_in, 0, n);
2468 if (involves < 0)
2469 return isl_aff_free(aff);
2470 if (involves)
2471 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2472 "affine expression involves some of the domain dimensions",
2473 return isl_aff_free(aff));
2474 aff = isl_aff_drop_dims(aff, isl_dim_in, 0, n);
2475 space = isl_aff_get_domain_space(aff);
2476 space = isl_space_params(space);
2477 aff = isl_aff_reset_domain_space(aff, space);
2478 return aff;
2481 /* Convert an affine expression defined over a parameter domain
2482 * into one that is defined over a zero-dimensional set.
2484 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2486 isl_local_space *ls;
2488 ls = isl_aff_take_domain_local_space(aff);
2489 ls = isl_local_space_set_from_params(ls);
2490 aff = isl_aff_restore_domain_local_space(aff, ls);
2492 return aff;
2495 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2496 enum isl_dim_type type, unsigned first, unsigned n)
2498 isl_ctx *ctx;
2500 if (!aff)
2501 return NULL;
2502 if (type == isl_dim_out)
2503 isl_die(aff->v->ctx, isl_error_invalid,
2504 "cannot insert output/set dimensions",
2505 return isl_aff_free(aff));
2506 if (type == isl_dim_in)
2507 type = isl_dim_set;
2508 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2509 return aff;
2511 ctx = isl_aff_get_ctx(aff);
2512 if (first > isl_local_space_dim(aff->ls, type))
2513 isl_die(ctx, isl_error_invalid, "position out of bounds",
2514 return isl_aff_free(aff));
2516 aff = isl_aff_cow(aff);
2517 if (!aff)
2518 return NULL;
2520 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2521 if (!aff->ls)
2522 return isl_aff_free(aff);
2524 first += 1 + isl_local_space_offset(aff->ls, type);
2525 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2526 if (!aff->v)
2527 return isl_aff_free(aff);
2529 return aff;
2532 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2533 enum isl_dim_type type, unsigned n)
2535 unsigned pos;
2537 pos = isl_aff_dim(aff, type);
2539 return isl_aff_insert_dims(aff, type, pos, n);
2542 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2543 enum isl_dim_type type, unsigned n)
2545 unsigned pos;
2547 pos = isl_pw_aff_dim(pwaff, type);
2549 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2552 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2553 * to dimensions of "dst_type" at "dst_pos".
2555 * We only support moving input dimensions to parameters and vice versa.
2557 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2558 enum isl_dim_type dst_type, unsigned dst_pos,
2559 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2561 unsigned g_dst_pos;
2562 unsigned g_src_pos;
2564 if (!aff)
2565 return NULL;
2566 if (n == 0 &&
2567 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2568 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2569 return aff;
2571 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2572 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2573 "cannot move output/set dimension",
2574 return isl_aff_free(aff));
2575 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2576 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2577 "cannot move divs", return isl_aff_free(aff));
2578 if (dst_type == isl_dim_in)
2579 dst_type = isl_dim_set;
2580 if (src_type == isl_dim_in)
2581 src_type = isl_dim_set;
2583 if (src_pos + n > isl_local_space_dim(aff->ls, src_type))
2584 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2585 "range out of bounds", return isl_aff_free(aff));
2586 if (dst_type == src_type)
2587 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2588 "moving dims within the same type not supported",
2589 return isl_aff_free(aff));
2591 aff = isl_aff_cow(aff);
2592 if (!aff)
2593 return NULL;
2595 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2596 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2597 if (dst_type > src_type)
2598 g_dst_pos -= n;
2600 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2601 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2602 src_type, src_pos, n);
2603 if (!aff->v || !aff->ls)
2604 return isl_aff_free(aff);
2606 aff = sort_divs(aff);
2608 return aff;
2611 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2613 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2614 return isl_pw_aff_alloc(dom, aff);
2617 #define isl_aff_involves_nan isl_aff_is_nan
2619 #undef PW
2620 #define PW isl_pw_aff
2621 #undef EL
2622 #define EL isl_aff
2623 #undef EL_IS_ZERO
2624 #define EL_IS_ZERO is_empty
2625 #undef ZERO
2626 #define ZERO empty
2627 #undef IS_ZERO
2628 #define IS_ZERO is_empty
2629 #undef FIELD
2630 #define FIELD aff
2631 #undef DEFAULT_IS_ZERO
2632 #define DEFAULT_IS_ZERO 0
2634 #define NO_EVAL
2635 #define NO_OPT
2636 #define NO_LIFT
2637 #define NO_MORPH
2639 #include <isl_pw_templ.c>
2640 #include <isl_pw_hash.c>
2641 #include <isl_pw_union_opt.c>
2643 #undef UNION
2644 #define UNION isl_union_pw_aff
2645 #undef PART
2646 #define PART isl_pw_aff
2647 #undef PARTS
2648 #define PARTS pw_aff
2650 #include <isl_union_single.c>
2651 #include <isl_union_neg.c>
2653 static __isl_give isl_set *align_params_pw_pw_set_and(
2654 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2655 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2656 __isl_take isl_pw_aff *pwaff2))
2658 isl_bool equal_params;
2660 if (!pwaff1 || !pwaff2)
2661 goto error;
2662 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2663 if (equal_params < 0)
2664 goto error;
2665 if (equal_params)
2666 return fn(pwaff1, pwaff2);
2667 if (!isl_space_has_named_params(pwaff1->dim) ||
2668 !isl_space_has_named_params(pwaff2->dim))
2669 isl_die(isl_pw_aff_get_ctx(pwaff1), isl_error_invalid,
2670 "unaligned unnamed parameters", goto error);
2671 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2672 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2673 return fn(pwaff1, pwaff2);
2674 error:
2675 isl_pw_aff_free(pwaff1);
2676 isl_pw_aff_free(pwaff2);
2677 return NULL;
2680 /* Align the parameters of the to isl_pw_aff arguments and
2681 * then apply a function "fn" on them that returns an isl_map.
2683 static __isl_give isl_map *align_params_pw_pw_map_and(
2684 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2685 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2686 __isl_take isl_pw_aff *pa2))
2688 isl_bool equal_params;
2690 if (!pa1 || !pa2)
2691 goto error;
2692 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2693 if (equal_params < 0)
2694 goto error;
2695 if (equal_params)
2696 return fn(pa1, pa2);
2697 if (!isl_space_has_named_params(pa1->dim) ||
2698 !isl_space_has_named_params(pa2->dim))
2699 isl_die(isl_pw_aff_get_ctx(pa1), isl_error_invalid,
2700 "unaligned unnamed parameters", goto error);
2701 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2702 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2703 return fn(pa1, pa2);
2704 error:
2705 isl_pw_aff_free(pa1);
2706 isl_pw_aff_free(pa2);
2707 return NULL;
2710 /* Compute a piecewise quasi-affine expression with a domain that
2711 * is the union of those of pwaff1 and pwaff2 and such that on each
2712 * cell, the quasi-affine expression is the maximum of those of pwaff1
2713 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2714 * cell, then the associated expression is the defined one.
2716 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2717 __isl_take isl_pw_aff *pwaff2)
2719 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2722 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2723 __isl_take isl_pw_aff *pwaff2)
2725 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2726 &pw_aff_union_max);
2729 /* Compute a piecewise quasi-affine expression with a domain that
2730 * is the union of those of pwaff1 and pwaff2 and such that on each
2731 * cell, the quasi-affine expression is the minimum of those of pwaff1
2732 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2733 * cell, then the associated expression is the defined one.
2735 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2736 __isl_take isl_pw_aff *pwaff2)
2738 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2741 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2742 __isl_take isl_pw_aff *pwaff2)
2744 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2745 &pw_aff_union_min);
2748 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2749 __isl_take isl_pw_aff *pwaff2, int max)
2751 if (max)
2752 return isl_pw_aff_union_max(pwaff1, pwaff2);
2753 else
2754 return isl_pw_aff_union_min(pwaff1, pwaff2);
2757 /* Construct a map with as domain the domain of pwaff and
2758 * one-dimensional range corresponding to the affine expressions.
2760 static __isl_give isl_map *map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2762 int i;
2763 isl_space *dim;
2764 isl_map *map;
2766 if (!pwaff)
2767 return NULL;
2769 dim = isl_pw_aff_get_space(pwaff);
2770 map = isl_map_empty(dim);
2772 for (i = 0; i < pwaff->n; ++i) {
2773 isl_basic_map *bmap;
2774 isl_map *map_i;
2776 bmap = isl_basic_map_from_aff(isl_aff_copy(pwaff->p[i].aff));
2777 map_i = isl_map_from_basic_map(bmap);
2778 map_i = isl_map_intersect_domain(map_i,
2779 isl_set_copy(pwaff->p[i].set));
2780 map = isl_map_union_disjoint(map, map_i);
2783 isl_pw_aff_free(pwaff);
2785 return map;
2788 /* Construct a map with as domain the domain of pwaff and
2789 * one-dimensional range corresponding to the affine expressions.
2791 __isl_give isl_map *isl_map_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2793 if (!pwaff)
2794 return NULL;
2795 if (isl_space_is_set(pwaff->dim))
2796 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2797 "space of input is not a map", goto error);
2798 return map_from_pw_aff(pwaff);
2799 error:
2800 isl_pw_aff_free(pwaff);
2801 return NULL;
2804 /* Construct a one-dimensional set with as parameter domain
2805 * the domain of pwaff and the single set dimension
2806 * corresponding to the affine expressions.
2808 __isl_give isl_set *isl_set_from_pw_aff(__isl_take isl_pw_aff *pwaff)
2810 if (!pwaff)
2811 return NULL;
2812 if (!isl_space_is_set(pwaff->dim))
2813 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
2814 "space of input is not a set", goto error);
2815 return map_from_pw_aff(pwaff);
2816 error:
2817 isl_pw_aff_free(pwaff);
2818 return NULL;
2821 /* Return a set containing those elements in the domain
2822 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2823 * does not satisfy "fn" (if complement is 1).
2825 * The pieces with a NaN never belong to the result since
2826 * NaN does not satisfy any property.
2828 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2829 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2830 int complement)
2832 int i;
2833 isl_set *set;
2835 if (!pwaff)
2836 return NULL;
2838 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2840 for (i = 0; i < pwaff->n; ++i) {
2841 isl_basic_set *bset;
2842 isl_set *set_i, *locus;
2843 isl_bool rational;
2845 if (isl_aff_is_nan(pwaff->p[i].aff))
2846 continue;
2848 rational = isl_set_has_rational(pwaff->p[i].set);
2849 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2850 locus = isl_set_from_basic_set(bset);
2851 set_i = isl_set_copy(pwaff->p[i].set);
2852 if (complement)
2853 set_i = isl_set_subtract(set_i, locus);
2854 else
2855 set_i = isl_set_intersect(set_i, locus);
2856 set = isl_set_union_disjoint(set, set_i);
2859 isl_pw_aff_free(pwaff);
2861 return set;
2864 /* Return a set containing those elements in the domain
2865 * of "pa" where it is positive.
2867 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2869 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2872 /* Return a set containing those elements in the domain
2873 * of pwaff where it is non-negative.
2875 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2877 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2880 /* Return a set containing those elements in the domain
2881 * of pwaff where it is zero.
2883 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2885 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2888 /* Return a set containing those elements in the domain
2889 * of pwaff where it is not zero.
2891 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2893 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2896 /* Return a set containing those elements in the shared domain
2897 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2899 * We compute the difference on the shared domain and then construct
2900 * the set of values where this difference is non-negative.
2901 * If strict is set, we first subtract 1 from the difference.
2902 * If equal is set, we only return the elements where pwaff1 and pwaff2
2903 * are equal.
2905 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2906 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2908 isl_set *set1, *set2;
2910 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2911 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2912 set1 = isl_set_intersect(set1, set2);
2913 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2914 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2915 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2917 if (strict) {
2918 isl_space *dim = isl_set_get_space(set1);
2919 isl_aff *aff;
2920 aff = isl_aff_zero_on_domain(isl_local_space_from_space(dim));
2921 aff = isl_aff_add_constant_si(aff, -1);
2922 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2923 } else
2924 isl_set_free(set1);
2926 if (equal)
2927 return isl_pw_aff_zero_set(pwaff1);
2928 return isl_pw_aff_nonneg_set(pwaff1);
2931 /* Return a set containing those elements in the shared domain
2932 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2934 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2935 __isl_take isl_pw_aff *pwaff2)
2937 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2940 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2941 __isl_take isl_pw_aff *pwaff2)
2943 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2946 /* Return a set containing those elements in the shared domain
2947 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2949 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2950 __isl_take isl_pw_aff *pwaff2)
2952 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2955 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2956 __isl_take isl_pw_aff *pwaff2)
2958 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2961 /* Return a set containing those elements in the shared domain
2962 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2964 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2965 __isl_take isl_pw_aff *pwaff2)
2967 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2970 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2971 __isl_take isl_pw_aff *pwaff2)
2973 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2976 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2977 __isl_take isl_pw_aff *pwaff2)
2979 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2982 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2983 __isl_take isl_pw_aff *pwaff2)
2985 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2988 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2989 * where the function values are ordered in the same way as "order",
2990 * which returns a set in the shared domain of its two arguments.
2991 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2993 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2994 * We first pull back the two functions such that they are defined on
2995 * the domain [A -> B]. Then we apply "order", resulting in a set
2996 * in the space [A -> B]. Finally, we unwrap this set to obtain
2997 * a map in the space A -> B.
2999 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
3000 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3001 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3002 __isl_take isl_pw_aff *pa2))
3004 isl_space *space1, *space2;
3005 isl_multi_aff *ma;
3006 isl_set *set;
3008 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3009 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3010 space1 = isl_space_map_from_domain_and_range(space1, space2);
3011 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3012 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3013 ma = isl_multi_aff_range_map(space1);
3014 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3015 set = order(pa1, pa2);
3017 return isl_set_unwrap(set);
3020 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3021 * where the function values are equal.
3022 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3024 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3025 __isl_take isl_pw_aff *pa2)
3027 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3030 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3031 * where the function values are equal.
3033 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3034 __isl_take isl_pw_aff *pa2)
3036 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3039 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3040 * where the function value of "pa1" is less than the function value of "pa2".
3041 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3043 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3044 __isl_take isl_pw_aff *pa2)
3046 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3049 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3050 * where the function value of "pa1" is less than the function value of "pa2".
3052 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3053 __isl_take isl_pw_aff *pa2)
3055 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3058 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3059 * where the function value of "pa1" is greater than the function value
3060 * of "pa2".
3061 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3063 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3064 __isl_take isl_pw_aff *pa2)
3066 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3069 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3070 * where the function value of "pa1" is greater than the function value
3071 * of "pa2".
3073 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3074 __isl_take isl_pw_aff *pa2)
3076 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3079 /* Return a set containing those elements in the shared domain
3080 * of the elements of list1 and list2 where each element in list1
3081 * has the relation specified by "fn" with each element in list2.
3083 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3084 __isl_take isl_pw_aff_list *list2,
3085 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3086 __isl_take isl_pw_aff *pwaff2))
3088 int i, j;
3089 isl_ctx *ctx;
3090 isl_set *set;
3092 if (!list1 || !list2)
3093 goto error;
3095 ctx = isl_pw_aff_list_get_ctx(list1);
3096 if (list1->n < 1 || list2->n < 1)
3097 isl_die(ctx, isl_error_invalid,
3098 "list should contain at least one element", goto error);
3100 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3101 for (i = 0; i < list1->n; ++i)
3102 for (j = 0; j < list2->n; ++j) {
3103 isl_set *set_ij;
3105 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3106 isl_pw_aff_copy(list2->p[j]));
3107 set = isl_set_intersect(set, set_ij);
3110 isl_pw_aff_list_free(list1);
3111 isl_pw_aff_list_free(list2);
3112 return set;
3113 error:
3114 isl_pw_aff_list_free(list1);
3115 isl_pw_aff_list_free(list2);
3116 return NULL;
3119 /* Return a set containing those elements in the shared domain
3120 * of the elements of list1 and list2 where each element in list1
3121 * is equal to each element in list2.
3123 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3124 __isl_take isl_pw_aff_list *list2)
3126 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3129 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3130 __isl_take isl_pw_aff_list *list2)
3132 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3135 /* Return a set containing those elements in the shared domain
3136 * of the elements of list1 and list2 where each element in list1
3137 * is less than or equal to each element in list2.
3139 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3140 __isl_take isl_pw_aff_list *list2)
3142 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3145 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3146 __isl_take isl_pw_aff_list *list2)
3148 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3151 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3152 __isl_take isl_pw_aff_list *list2)
3154 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3157 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3158 __isl_take isl_pw_aff_list *list2)
3160 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3164 /* Return a set containing those elements in the shared domain
3165 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3167 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3168 __isl_take isl_pw_aff *pwaff2)
3170 isl_set *set_lt, *set_gt;
3172 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3173 isl_pw_aff_copy(pwaff2));
3174 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3175 return isl_set_union_disjoint(set_lt, set_gt);
3178 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3179 __isl_take isl_pw_aff *pwaff2)
3181 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3184 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3185 isl_int v)
3187 int i;
3189 if (isl_int_is_one(v))
3190 return pwaff;
3191 if (!isl_int_is_pos(v))
3192 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3193 "factor needs to be positive",
3194 return isl_pw_aff_free(pwaff));
3195 pwaff = isl_pw_aff_cow(pwaff);
3196 if (!pwaff)
3197 return NULL;
3198 if (pwaff->n == 0)
3199 return pwaff;
3201 for (i = 0; i < pwaff->n; ++i) {
3202 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3203 if (!pwaff->p[i].aff)
3204 return isl_pw_aff_free(pwaff);
3207 return pwaff;
3210 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3212 int i;
3214 pwaff = isl_pw_aff_cow(pwaff);
3215 if (!pwaff)
3216 return NULL;
3217 if (pwaff->n == 0)
3218 return pwaff;
3220 for (i = 0; i < pwaff->n; ++i) {
3221 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3222 if (!pwaff->p[i].aff)
3223 return isl_pw_aff_free(pwaff);
3226 return pwaff;
3229 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3231 int i;
3233 pwaff = isl_pw_aff_cow(pwaff);
3234 if (!pwaff)
3235 return NULL;
3236 if (pwaff->n == 0)
3237 return pwaff;
3239 for (i = 0; i < pwaff->n; ++i) {
3240 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3241 if (!pwaff->p[i].aff)
3242 return isl_pw_aff_free(pwaff);
3245 return pwaff;
3248 /* Assuming that "cond1" and "cond2" are disjoint,
3249 * return an affine expression that is equal to pwaff1 on cond1
3250 * and to pwaff2 on cond2.
3252 static __isl_give isl_pw_aff *isl_pw_aff_select(
3253 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3254 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3256 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3257 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3259 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3262 /* Return an affine expression that is equal to pwaff_true for elements
3263 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3264 * is zero.
3265 * That is, return cond ? pwaff_true : pwaff_false;
3267 * If "cond" involves and NaN, then we conservatively return a NaN
3268 * on its entire domain. In principle, we could consider the pieces
3269 * where it is NaN separately from those where it is not.
3271 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3272 * then only use the domain of "cond" to restrict the domain.
3274 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3275 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3277 isl_set *cond_true, *cond_false;
3278 isl_bool equal;
3280 if (!cond)
3281 goto error;
3282 if (isl_pw_aff_involves_nan(cond)) {
3283 isl_space *space = isl_pw_aff_get_domain_space(cond);
3284 isl_local_space *ls = isl_local_space_from_space(space);
3285 isl_pw_aff_free(cond);
3286 isl_pw_aff_free(pwaff_true);
3287 isl_pw_aff_free(pwaff_false);
3288 return isl_pw_aff_nan_on_domain(ls);
3291 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3292 isl_pw_aff_get_space(pwaff_false));
3293 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3294 isl_pw_aff_get_space(pwaff_true));
3295 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3296 if (equal < 0)
3297 goto error;
3298 if (equal) {
3299 isl_set *dom;
3301 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3302 isl_pw_aff_free(pwaff_false);
3303 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3306 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3307 cond_false = isl_pw_aff_zero_set(cond);
3308 return isl_pw_aff_select(cond_true, pwaff_true,
3309 cond_false, pwaff_false);
3310 error:
3311 isl_pw_aff_free(cond);
3312 isl_pw_aff_free(pwaff_true);
3313 isl_pw_aff_free(pwaff_false);
3314 return NULL;
3317 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3319 if (!aff)
3320 return isl_bool_error;
3322 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3325 /* Check whether pwaff is a piecewise constant.
3327 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3329 int i;
3331 if (!pwaff)
3332 return isl_bool_error;
3334 for (i = 0; i < pwaff->n; ++i) {
3335 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3336 if (is_cst < 0 || !is_cst)
3337 return is_cst;
3340 return isl_bool_true;
3343 /* Are all elements of "mpa" piecewise constants?
3345 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3347 int i;
3349 if (!mpa)
3350 return isl_bool_error;
3352 for (i = 0; i < mpa->n; ++i) {
3353 isl_bool is_cst = isl_pw_aff_is_cst(mpa->p[i]);
3354 if (is_cst < 0 || !is_cst)
3355 return is_cst;
3358 return isl_bool_true;
3361 /* Return the product of "aff1" and "aff2".
3363 * If either of the two is NaN, then the result is NaN.
3365 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3367 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3368 __isl_take isl_aff *aff2)
3370 if (!aff1 || !aff2)
3371 goto error;
3373 if (isl_aff_is_nan(aff1)) {
3374 isl_aff_free(aff2);
3375 return aff1;
3377 if (isl_aff_is_nan(aff2)) {
3378 isl_aff_free(aff1);
3379 return aff2;
3382 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3383 return isl_aff_mul(aff2, aff1);
3385 if (!isl_aff_is_cst(aff2))
3386 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3387 "at least one affine expression should be constant",
3388 goto error);
3390 aff1 = isl_aff_cow(aff1);
3391 if (!aff1 || !aff2)
3392 goto error;
3394 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3395 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3397 isl_aff_free(aff2);
3398 return aff1;
3399 error:
3400 isl_aff_free(aff1);
3401 isl_aff_free(aff2);
3402 return NULL;
3405 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3407 * If either of the two is NaN, then the result is NaN.
3409 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3410 __isl_take isl_aff *aff2)
3412 int is_cst;
3413 int neg;
3415 if (!aff1 || !aff2)
3416 goto error;
3418 if (isl_aff_is_nan(aff1)) {
3419 isl_aff_free(aff2);
3420 return aff1;
3422 if (isl_aff_is_nan(aff2)) {
3423 isl_aff_free(aff1);
3424 return aff2;
3427 is_cst = isl_aff_is_cst(aff2);
3428 if (is_cst < 0)
3429 goto error;
3430 if (!is_cst)
3431 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3432 "second argument should be a constant", goto error);
3434 if (!aff2)
3435 goto error;
3437 neg = isl_int_is_neg(aff2->v->el[1]);
3438 if (neg) {
3439 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3440 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3443 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3444 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3446 if (neg) {
3447 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3448 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3451 isl_aff_free(aff2);
3452 return aff1;
3453 error:
3454 isl_aff_free(aff1);
3455 isl_aff_free(aff2);
3456 return NULL;
3459 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3460 __isl_take isl_pw_aff *pwaff2)
3462 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3465 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3466 __isl_take isl_pw_aff *pwaff2)
3468 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3471 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3472 __isl_take isl_pw_aff *pwaff2)
3474 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3477 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3478 __isl_take isl_pw_aff *pwaff2)
3480 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3483 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3484 __isl_take isl_pw_aff *pwaff2)
3486 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3489 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3490 __isl_take isl_pw_aff *pa2)
3492 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3495 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3497 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3498 __isl_take isl_pw_aff *pa2)
3500 int is_cst;
3502 is_cst = isl_pw_aff_is_cst(pa2);
3503 if (is_cst < 0)
3504 goto error;
3505 if (!is_cst)
3506 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3507 "second argument should be a piecewise constant",
3508 goto error);
3509 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3510 error:
3511 isl_pw_aff_free(pa1);
3512 isl_pw_aff_free(pa2);
3513 return NULL;
3516 /* Compute the quotient of the integer division of "pa1" by "pa2"
3517 * with rounding towards zero.
3518 * "pa2" is assumed to be a piecewise constant.
3520 * In particular, return
3522 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3525 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3526 __isl_take isl_pw_aff *pa2)
3528 int is_cst;
3529 isl_set *cond;
3530 isl_pw_aff *f, *c;
3532 is_cst = isl_pw_aff_is_cst(pa2);
3533 if (is_cst < 0)
3534 goto error;
3535 if (!is_cst)
3536 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3537 "second argument should be a piecewise constant",
3538 goto error);
3540 pa1 = isl_pw_aff_div(pa1, pa2);
3542 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3543 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3544 c = isl_pw_aff_ceil(pa1);
3545 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3546 error:
3547 isl_pw_aff_free(pa1);
3548 isl_pw_aff_free(pa2);
3549 return NULL;
3552 /* Compute the remainder of the integer division of "pa1" by "pa2"
3553 * with rounding towards zero.
3554 * "pa2" is assumed to be a piecewise constant.
3556 * In particular, return
3558 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3561 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3562 __isl_take isl_pw_aff *pa2)
3564 int is_cst;
3565 isl_pw_aff *res;
3567 is_cst = isl_pw_aff_is_cst(pa2);
3568 if (is_cst < 0)
3569 goto error;
3570 if (!is_cst)
3571 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3572 "second argument should be a piecewise constant",
3573 goto error);
3574 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3575 res = isl_pw_aff_mul(pa2, res);
3576 res = isl_pw_aff_sub(pa1, res);
3577 return res;
3578 error:
3579 isl_pw_aff_free(pa1);
3580 isl_pw_aff_free(pa2);
3581 return NULL;
3584 /* Does either of "pa1" or "pa2" involve any NaN2?
3586 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3587 __isl_keep isl_pw_aff *pa2)
3589 isl_bool has_nan;
3591 has_nan = isl_pw_aff_involves_nan(pa1);
3592 if (has_nan < 0 || has_nan)
3593 return has_nan;
3594 return isl_pw_aff_involves_nan(pa2);
3597 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3598 * by a NaN on their shared domain.
3600 * In principle, the result could be refined to only being NaN
3601 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3603 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3604 __isl_take isl_pw_aff *pa2)
3606 isl_local_space *ls;
3607 isl_set *dom;
3608 isl_pw_aff *pa;
3610 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3611 ls = isl_local_space_from_space(isl_set_get_space(dom));
3612 pa = isl_pw_aff_nan_on_domain(ls);
3613 pa = isl_pw_aff_intersect_domain(pa, dom);
3615 return pa;
3618 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3619 __isl_take isl_pw_aff *pwaff2)
3621 isl_set *le;
3622 isl_set *dom;
3624 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3625 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3626 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3627 isl_pw_aff_copy(pwaff2));
3628 dom = isl_set_subtract(dom, isl_set_copy(le));
3629 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3632 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3633 __isl_take isl_pw_aff *pwaff2)
3635 isl_set *ge;
3636 isl_set *dom;
3638 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3639 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3640 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3641 isl_pw_aff_copy(pwaff2));
3642 dom = isl_set_subtract(dom, isl_set_copy(ge));
3643 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3646 /* Return an expression for the minimum (if "max" is not set) or
3647 * the maximum (if "max" is set) of "pa1" and "pa2".
3648 * If either expression involves any NaN, then return a NaN
3649 * on the shared domain as result.
3651 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3652 __isl_take isl_pw_aff *pa2, int max)
3654 isl_bool has_nan;
3656 has_nan = either_involves_nan(pa1, pa2);
3657 if (has_nan < 0)
3658 pa1 = isl_pw_aff_free(pa1);
3659 else if (has_nan)
3660 return replace_by_nan(pa1, pa2);
3662 if (max)
3663 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3664 else
3665 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3668 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3670 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3671 __isl_take isl_pw_aff *pwaff2)
3673 return pw_aff_min_max(pwaff1, pwaff2, 0);
3676 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3678 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3679 __isl_take isl_pw_aff *pwaff2)
3681 return pw_aff_min_max(pwaff1, pwaff2, 1);
3684 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3685 __isl_take isl_pw_aff_list *list,
3686 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3687 __isl_take isl_pw_aff *pwaff2))
3689 int i;
3690 isl_ctx *ctx;
3691 isl_pw_aff *res;
3693 if (!list)
3694 return NULL;
3696 ctx = isl_pw_aff_list_get_ctx(list);
3697 if (list->n < 1)
3698 isl_die(ctx, isl_error_invalid,
3699 "list should contain at least one element", goto error);
3701 res = isl_pw_aff_copy(list->p[0]);
3702 for (i = 1; i < list->n; ++i)
3703 res = fn(res, isl_pw_aff_copy(list->p[i]));
3705 isl_pw_aff_list_free(list);
3706 return res;
3707 error:
3708 isl_pw_aff_list_free(list);
3709 return NULL;
3712 /* Return an isl_pw_aff that maps each element in the intersection of the
3713 * domains of the elements of list to the minimal corresponding affine
3714 * expression.
3716 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3718 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3721 /* Return an isl_pw_aff that maps each element in the intersection of the
3722 * domains of the elements of list to the maximal corresponding affine
3723 * expression.
3725 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3727 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3730 /* Mark the domains of "pwaff" as rational.
3732 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3734 int i;
3736 pwaff = isl_pw_aff_cow(pwaff);
3737 if (!pwaff)
3738 return NULL;
3739 if (pwaff->n == 0)
3740 return pwaff;
3742 for (i = 0; i < pwaff->n; ++i) {
3743 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3744 if (!pwaff->p[i].set)
3745 return isl_pw_aff_free(pwaff);
3748 return pwaff;
3751 /* Mark the domains of the elements of "list" as rational.
3753 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3754 __isl_take isl_pw_aff_list *list)
3756 int i, n;
3758 if (!list)
3759 return NULL;
3760 if (list->n == 0)
3761 return list;
3763 n = list->n;
3764 for (i = 0; i < n; ++i) {
3765 isl_pw_aff *pa;
3767 pa = isl_pw_aff_list_get_pw_aff(list, i);
3768 pa = isl_pw_aff_set_rational(pa);
3769 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3772 return list;
3775 /* Do the parameters of "aff" match those of "space"?
3777 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3778 __isl_keep isl_space *space)
3780 isl_space *aff_space;
3781 isl_bool match;
3783 if (!aff || !space)
3784 return isl_bool_error;
3786 aff_space = isl_aff_get_domain_space(aff);
3788 match = isl_space_has_equal_params(space, aff_space);
3790 isl_space_free(aff_space);
3791 return match;
3794 /* Check that the domain space of "aff" matches "space".
3796 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3797 __isl_keep isl_space *space)
3799 isl_space *aff_space;
3800 isl_bool match;
3802 if (!aff || !space)
3803 return isl_stat_error;
3805 aff_space = isl_aff_get_domain_space(aff);
3807 match = isl_space_has_equal_params(space, aff_space);
3808 if (match < 0)
3809 goto error;
3810 if (!match)
3811 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3812 "parameters don't match", goto error);
3813 match = isl_space_tuple_is_equal(space, isl_dim_in,
3814 aff_space, isl_dim_set);
3815 if (match < 0)
3816 goto error;
3817 if (!match)
3818 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3819 "domains don't match", goto error);
3820 isl_space_free(aff_space);
3821 return isl_stat_ok;
3822 error:
3823 isl_space_free(aff_space);
3824 return isl_stat_error;
3827 #undef BASE
3828 #define BASE aff
3829 #undef DOMBASE
3830 #define DOMBASE set
3831 #define NO_DOMAIN
3833 #include <isl_multi_templ.c>
3834 #include <isl_multi_apply_set.c>
3835 #include <isl_multi_cmp.c>
3836 #include <isl_multi_dims.c>
3837 #include <isl_multi_floor.c>
3838 #include <isl_multi_gist.c>
3840 #undef NO_DOMAIN
3842 /* Construct an isl_multi_aff living in "space" that corresponds
3843 * to the affine transformation matrix "mat".
3845 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3846 __isl_take isl_space *space, __isl_take isl_mat *mat)
3848 isl_ctx *ctx;
3849 isl_local_space *ls = NULL;
3850 isl_multi_aff *ma = NULL;
3851 int n_row, n_col, n_out, total;
3852 int i;
3854 if (!space || !mat)
3855 goto error;
3857 ctx = isl_mat_get_ctx(mat);
3859 n_row = isl_mat_rows(mat);
3860 n_col = isl_mat_cols(mat);
3861 if (n_row < 1)
3862 isl_die(ctx, isl_error_invalid,
3863 "insufficient number of rows", goto error);
3864 if (n_col < 1)
3865 isl_die(ctx, isl_error_invalid,
3866 "insufficient number of columns", goto error);
3867 n_out = isl_space_dim(space, isl_dim_out);
3868 total = isl_space_dim(space, isl_dim_all);
3869 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3870 isl_die(ctx, isl_error_invalid,
3871 "dimension mismatch", goto error);
3873 ma = isl_multi_aff_zero(isl_space_copy(space));
3874 ls = isl_local_space_from_space(isl_space_domain(space));
3876 for (i = 0; i < n_row - 1; ++i) {
3877 isl_vec *v;
3878 isl_aff *aff;
3880 v = isl_vec_alloc(ctx, 1 + n_col);
3881 if (!v)
3882 goto error;
3883 isl_int_set(v->el[0], mat->row[0][0]);
3884 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3885 v = isl_vec_normalize(v);
3886 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3887 ma = isl_multi_aff_set_aff(ma, i, aff);
3890 isl_local_space_free(ls);
3891 isl_mat_free(mat);
3892 return ma;
3893 error:
3894 isl_local_space_free(ls);
3895 isl_mat_free(mat);
3896 isl_multi_aff_free(ma);
3897 return NULL;
3900 /* Remove any internal structure of the domain of "ma".
3901 * If there is any such internal structure in the input,
3902 * then the name of the corresponding space is also removed.
3904 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3905 __isl_take isl_multi_aff *ma)
3907 isl_space *space;
3909 if (!ma)
3910 return NULL;
3912 if (!ma->space->nested[0])
3913 return ma;
3915 space = isl_multi_aff_get_space(ma);
3916 space = isl_space_flatten_domain(space);
3917 ma = isl_multi_aff_reset_space(ma, space);
3919 return ma;
3922 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3923 * of the space to its domain.
3925 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3927 int i, n_in;
3928 isl_local_space *ls;
3929 isl_multi_aff *ma;
3931 if (!space)
3932 return NULL;
3933 if (!isl_space_is_map(space))
3934 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3935 "not a map space", goto error);
3937 n_in = isl_space_dim(space, isl_dim_in);
3938 space = isl_space_domain_map(space);
3940 ma = isl_multi_aff_alloc(isl_space_copy(space));
3941 if (n_in == 0) {
3942 isl_space_free(space);
3943 return ma;
3946 space = isl_space_domain(space);
3947 ls = isl_local_space_from_space(space);
3948 for (i = 0; i < n_in; ++i) {
3949 isl_aff *aff;
3951 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3952 isl_dim_set, i);
3953 ma = isl_multi_aff_set_aff(ma, i, aff);
3955 isl_local_space_free(ls);
3956 return ma;
3957 error:
3958 isl_space_free(space);
3959 return NULL;
3962 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3963 * of the space to its range.
3965 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3967 int i, n_in, n_out;
3968 isl_local_space *ls;
3969 isl_multi_aff *ma;
3971 if (!space)
3972 return NULL;
3973 if (!isl_space_is_map(space))
3974 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3975 "not a map space", goto error);
3977 n_in = isl_space_dim(space, isl_dim_in);
3978 n_out = isl_space_dim(space, isl_dim_out);
3979 space = isl_space_range_map(space);
3981 ma = isl_multi_aff_alloc(isl_space_copy(space));
3982 if (n_out == 0) {
3983 isl_space_free(space);
3984 return ma;
3987 space = isl_space_domain(space);
3988 ls = isl_local_space_from_space(space);
3989 for (i = 0; i < n_out; ++i) {
3990 isl_aff *aff;
3992 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3993 isl_dim_set, n_in + i);
3994 ma = isl_multi_aff_set_aff(ma, i, aff);
3996 isl_local_space_free(ls);
3997 return ma;
3998 error:
3999 isl_space_free(space);
4000 return NULL;
4003 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4004 * of the space to its range.
4006 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4007 __isl_take isl_space *space)
4009 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4012 /* Given the space of a set and a range of set dimensions,
4013 * construct an isl_multi_aff that projects out those dimensions.
4015 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4016 __isl_take isl_space *space, enum isl_dim_type type,
4017 unsigned first, unsigned n)
4019 int i, dim;
4020 isl_local_space *ls;
4021 isl_multi_aff *ma;
4023 if (!space)
4024 return NULL;
4025 if (!isl_space_is_set(space))
4026 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4027 "expecting set space", goto error);
4028 if (type != isl_dim_set)
4029 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4030 "only set dimensions can be projected out", goto error);
4032 dim = isl_space_dim(space, isl_dim_set);
4033 if (first + n > dim)
4034 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4035 "range out of bounds", goto error);
4037 space = isl_space_from_domain(space);
4038 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4040 if (dim == n)
4041 return isl_multi_aff_alloc(space);
4043 ma = isl_multi_aff_alloc(isl_space_copy(space));
4044 space = isl_space_domain(space);
4045 ls = isl_local_space_from_space(space);
4047 for (i = 0; i < first; ++i) {
4048 isl_aff *aff;
4050 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4051 isl_dim_set, i);
4052 ma = isl_multi_aff_set_aff(ma, i, aff);
4055 for (i = 0; i < dim - (first + n); ++i) {
4056 isl_aff *aff;
4058 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4059 isl_dim_set, first + n + i);
4060 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4063 isl_local_space_free(ls);
4064 return ma;
4065 error:
4066 isl_space_free(space);
4067 return NULL;
4070 /* Given the space of a set and a range of set dimensions,
4071 * construct an isl_pw_multi_aff that projects out those dimensions.
4073 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4074 __isl_take isl_space *space, enum isl_dim_type type,
4075 unsigned first, unsigned n)
4077 isl_multi_aff *ma;
4079 ma = isl_multi_aff_project_out_map(space, type, first, n);
4080 return isl_pw_multi_aff_from_multi_aff(ma);
4083 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4084 * domain.
4086 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4087 __isl_take isl_multi_aff *ma)
4089 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4090 return isl_pw_multi_aff_alloc(dom, ma);
4093 /* Create a piecewise multi-affine expression in the given space that maps each
4094 * input dimension to the corresponding output dimension.
4096 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4097 __isl_take isl_space *space)
4099 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4102 /* Exploit the equalities in "eq" to simplify the affine expressions.
4104 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4105 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4107 int i;
4109 maff = isl_multi_aff_cow(maff);
4110 if (!maff || !eq)
4111 goto error;
4113 for (i = 0; i < maff->n; ++i) {
4114 maff->p[i] = isl_aff_substitute_equalities(maff->p[i],
4115 isl_basic_set_copy(eq));
4116 if (!maff->p[i])
4117 goto error;
4120 isl_basic_set_free(eq);
4121 return maff;
4122 error:
4123 isl_basic_set_free(eq);
4124 isl_multi_aff_free(maff);
4125 return NULL;
4128 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4129 isl_int f)
4131 int i;
4133 maff = isl_multi_aff_cow(maff);
4134 if (!maff)
4135 return NULL;
4137 for (i = 0; i < maff->n; ++i) {
4138 maff->p[i] = isl_aff_scale(maff->p[i], f);
4139 if (!maff->p[i])
4140 return isl_multi_aff_free(maff);
4143 return maff;
4146 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4147 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4149 maff1 = isl_multi_aff_add(maff1, maff2);
4150 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4151 return maff1;
4154 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4156 if (!maff)
4157 return -1;
4159 return 0;
4162 /* Return the set of domain elements where "ma1" is lexicographically
4163 * smaller than or equal to "ma2".
4165 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4166 __isl_take isl_multi_aff *ma2)
4168 return isl_multi_aff_lex_ge_set(ma2, ma1);
4171 /* Return the set of domain elements where "ma1" is lexicographically
4172 * smaller than "ma2".
4174 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4175 __isl_take isl_multi_aff *ma2)
4177 return isl_multi_aff_lex_gt_set(ma2, ma1);
4180 /* Return the set of domain elements where "ma1" and "ma2"
4181 * satisfy "order".
4183 static __isl_give isl_set *isl_multi_aff_order_set(
4184 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4185 __isl_give isl_map *order(__isl_take isl_space *set_space))
4187 isl_space *space;
4188 isl_map *map1, *map2;
4189 isl_map *map, *ge;
4191 map1 = isl_map_from_multi_aff(ma1);
4192 map2 = isl_map_from_multi_aff(ma2);
4193 map = isl_map_range_product(map1, map2);
4194 space = isl_space_range(isl_map_get_space(map));
4195 space = isl_space_domain(isl_space_unwrap(space));
4196 ge = order(space);
4197 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4199 return isl_map_domain(map);
4202 /* Return the set of domain elements where "ma1" is lexicographically
4203 * greater than or equal to "ma2".
4205 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4206 __isl_take isl_multi_aff *ma2)
4208 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4211 /* Return the set of domain elements where "ma1" is lexicographically
4212 * greater than "ma2".
4214 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4215 __isl_take isl_multi_aff *ma2)
4217 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4220 #undef PW
4221 #define PW isl_pw_multi_aff
4222 #undef EL
4223 #define EL isl_multi_aff
4224 #undef EL_IS_ZERO
4225 #define EL_IS_ZERO is_empty
4226 #undef ZERO
4227 #define ZERO empty
4228 #undef IS_ZERO
4229 #define IS_ZERO is_empty
4230 #undef FIELD
4231 #define FIELD maff
4232 #undef DEFAULT_IS_ZERO
4233 #define DEFAULT_IS_ZERO 0
4235 #define NO_SUB
4236 #define NO_EVAL
4237 #define NO_OPT
4238 #define NO_INVOLVES_DIMS
4239 #define NO_INSERT_DIMS
4240 #define NO_LIFT
4241 #define NO_MORPH
4243 #include <isl_pw_templ.c>
4244 #include <isl_pw_union_opt.c>
4246 #undef NO_SUB
4248 #undef UNION
4249 #define UNION isl_union_pw_multi_aff
4250 #undef PART
4251 #define PART isl_pw_multi_aff
4252 #undef PARTS
4253 #define PARTS pw_multi_aff
4255 #include <isl_union_multi.c>
4256 #include <isl_union_neg.c>
4258 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4259 __isl_take isl_pw_multi_aff *pma1,
4260 __isl_take isl_pw_multi_aff *pma2)
4262 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4263 &isl_multi_aff_lex_ge_set);
4266 /* Given two piecewise multi affine expressions, return a piecewise
4267 * multi-affine expression defined on the union of the definition domains
4268 * of the inputs that is equal to the lexicographic maximum of the two
4269 * inputs on each cell. If only one of the two inputs is defined on
4270 * a given cell, then it is considered to be the maximum.
4272 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4273 __isl_take isl_pw_multi_aff *pma1,
4274 __isl_take isl_pw_multi_aff *pma2)
4276 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4277 &pw_multi_aff_union_lexmax);
4280 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4281 __isl_take isl_pw_multi_aff *pma1,
4282 __isl_take isl_pw_multi_aff *pma2)
4284 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4285 &isl_multi_aff_lex_le_set);
4288 /* Given two piecewise multi affine expressions, return a piecewise
4289 * multi-affine expression defined on the union of the definition domains
4290 * of the inputs that is equal to the lexicographic minimum of the two
4291 * inputs on each cell. If only one of the two inputs is defined on
4292 * a given cell, then it is considered to be the minimum.
4294 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4295 __isl_take isl_pw_multi_aff *pma1,
4296 __isl_take isl_pw_multi_aff *pma2)
4298 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4299 &pw_multi_aff_union_lexmin);
4302 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4303 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4305 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4306 &isl_multi_aff_add);
4309 __isl_give isl_pw_multi_aff *isl_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_align_params_pw_pw_and(pma1, pma2,
4313 &pw_multi_aff_add);
4316 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4317 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4319 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4320 &isl_multi_aff_sub);
4323 /* Subtract "pma2" from "pma1" and return the result.
4325 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4326 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4328 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4329 &pw_multi_aff_sub);
4332 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4333 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4335 return isl_pw_multi_aff_union_add_(pma1, pma2);
4338 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4339 * with the actual sum on the shared domain and
4340 * the defined expression on the symmetric difference of the domains.
4342 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4343 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4345 return isl_union_pw_aff_union_add_(upa1, upa2);
4348 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4349 * with the actual sum on the shared domain and
4350 * the defined expression on the symmetric difference of the domains.
4352 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4353 __isl_take isl_union_pw_multi_aff *upma1,
4354 __isl_take isl_union_pw_multi_aff *upma2)
4356 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4359 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4360 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4362 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4363 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4365 int i, j, n;
4366 isl_space *space;
4367 isl_pw_multi_aff *res;
4369 if (!pma1 || !pma2)
4370 goto error;
4372 n = pma1->n * pma2->n;
4373 space = isl_space_product(isl_space_copy(pma1->dim),
4374 isl_space_copy(pma2->dim));
4375 res = isl_pw_multi_aff_alloc_size(space, n);
4377 for (i = 0; i < pma1->n; ++i) {
4378 for (j = 0; j < pma2->n; ++j) {
4379 isl_set *domain;
4380 isl_multi_aff *ma;
4382 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4383 isl_set_copy(pma2->p[j].set));
4384 ma = isl_multi_aff_product(
4385 isl_multi_aff_copy(pma1->p[i].maff),
4386 isl_multi_aff_copy(pma2->p[j].maff));
4387 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4391 isl_pw_multi_aff_free(pma1);
4392 isl_pw_multi_aff_free(pma2);
4393 return res;
4394 error:
4395 isl_pw_multi_aff_free(pma1);
4396 isl_pw_multi_aff_free(pma2);
4397 return NULL;
4400 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4401 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4403 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4404 &pw_multi_aff_product);
4407 /* Construct a map mapping the domain of the piecewise multi-affine expression
4408 * to its range, with each dimension in the range equated to the
4409 * corresponding affine expression on its cell.
4411 * If the domain of "pma" is rational, then so is the constructed "map".
4413 __isl_give isl_map *isl_map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4415 int i;
4416 isl_map *map;
4418 if (!pma)
4419 return NULL;
4421 map = isl_map_empty(isl_pw_multi_aff_get_space(pma));
4423 for (i = 0; i < pma->n; ++i) {
4424 isl_bool rational;
4425 isl_multi_aff *maff;
4426 isl_basic_map *bmap;
4427 isl_map *map_i;
4429 rational = isl_set_is_rational(pma->p[i].set);
4430 if (rational < 0)
4431 map = isl_map_free(map);
4432 maff = isl_multi_aff_copy(pma->p[i].maff);
4433 bmap = isl_basic_map_from_multi_aff2(maff, rational);
4434 map_i = isl_map_from_basic_map(bmap);
4435 map_i = isl_map_intersect_domain(map_i,
4436 isl_set_copy(pma->p[i].set));
4437 map = isl_map_union_disjoint(map, map_i);
4440 isl_pw_multi_aff_free(pma);
4441 return map;
4444 __isl_give isl_set *isl_set_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma)
4446 if (!pma)
4447 return NULL;
4449 if (!isl_space_is_set(pma->dim))
4450 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4451 "isl_pw_multi_aff cannot be converted into an isl_set",
4452 goto error);
4454 return isl_map_from_pw_multi_aff(pma);
4455 error:
4456 isl_pw_multi_aff_free(pma);
4457 return NULL;
4460 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4461 * denominator "denom".
4462 * "denom" is allowed to be negative, in which case the actual denominator
4463 * is -denom and the expressions are added instead.
4465 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4466 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4468 int i, first;
4469 int sign;
4470 isl_int d;
4472 first = isl_seq_first_non_zero(c, n);
4473 if (first == -1)
4474 return aff;
4476 sign = isl_int_sgn(denom);
4477 isl_int_init(d);
4478 isl_int_abs(d, denom);
4479 for (i = first; i < n; ++i) {
4480 isl_aff *aff_i;
4482 if (isl_int_is_zero(c[i]))
4483 continue;
4484 aff_i = isl_multi_aff_get_aff(ma, i);
4485 aff_i = isl_aff_scale(aff_i, c[i]);
4486 aff_i = isl_aff_scale_down(aff_i, d);
4487 if (sign >= 0)
4488 aff = isl_aff_sub(aff, aff_i);
4489 else
4490 aff = isl_aff_add(aff, aff_i);
4492 isl_int_clear(d);
4494 return aff;
4497 /* Extract an affine expression that expresses the output dimension "pos"
4498 * of "bmap" in terms of the parameters and input dimensions from
4499 * equality "eq".
4500 * Note that this expression may involve integer divisions defined
4501 * in terms of parameters and input dimensions.
4502 * The equality may also involve references to earlier (but not later)
4503 * output dimensions. These are replaced by the corresponding elements
4504 * in "ma".
4506 * If the equality is of the form
4508 * f(i) + h(j) + a x + g(i) = 0,
4510 * with f(i) a linear combinations of the parameters and input dimensions,
4511 * g(i) a linear combination of integer divisions defined in terms of the same
4512 * and h(j) a linear combinations of earlier output dimensions,
4513 * then the affine expression is
4515 * (-f(i) - g(i))/a - h(j)/a
4517 * If the equality is of the form
4519 * f(i) + h(j) - a x + g(i) = 0,
4521 * then the affine expression is
4523 * (f(i) + g(i))/a - h(j)/(-a)
4526 * If "div" refers to an integer division (i.e., it is smaller than
4527 * the number of integer divisions), then the equality constraint
4528 * does involve an integer division (the one at position "div") that
4529 * is defined in terms of output dimensions. However, this integer
4530 * division can be eliminated by exploiting a pair of constraints
4531 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4532 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4533 * -l + x >= 0.
4534 * In particular, let
4536 * x = e(i) + m floor(...)
4538 * with e(i) the expression derived above and floor(...) the integer
4539 * division involving output dimensions.
4540 * From
4542 * l <= x <= l + n,
4544 * we have
4546 * 0 <= x - l <= n
4548 * This means
4550 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4551 * = (e(i) - l) mod m
4553 * Therefore,
4555 * x - l = (e(i) - l) mod m
4557 * or
4559 * x = ((e(i) - l) mod m) + l
4561 * The variable "shift" below contains the expression -l, which may
4562 * also involve a linear combination of earlier output dimensions.
4564 static __isl_give isl_aff *extract_aff_from_equality(
4565 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4566 __isl_keep isl_multi_aff *ma)
4568 unsigned o_out;
4569 unsigned n_div, n_out;
4570 isl_ctx *ctx;
4571 isl_local_space *ls;
4572 isl_aff *aff, *shift;
4573 isl_val *mod;
4575 ctx = isl_basic_map_get_ctx(bmap);
4576 ls = isl_basic_map_get_local_space(bmap);
4577 ls = isl_local_space_domain(ls);
4578 aff = isl_aff_alloc(isl_local_space_copy(ls));
4579 if (!aff)
4580 goto error;
4581 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4582 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4583 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4584 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4585 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4586 isl_seq_cpy(aff->v->el + 1 + o_out,
4587 bmap->eq[eq] + o_out + n_out, n_div);
4588 } else {
4589 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4590 isl_seq_neg(aff->v->el + 1 + o_out,
4591 bmap->eq[eq] + o_out + n_out, n_div);
4593 if (div < n_div)
4594 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4595 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4596 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4597 bmap->eq[eq][o_out + pos]);
4598 if (div < n_div) {
4599 shift = isl_aff_alloc(isl_local_space_copy(ls));
4600 if (!shift)
4601 goto error;
4602 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4603 isl_seq_cpy(shift->v->el + 1 + o_out,
4604 bmap->ineq[ineq] + o_out + n_out, n_div);
4605 isl_int_set_si(shift->v->el[0], 1);
4606 shift = subtract_initial(shift, ma, pos,
4607 bmap->ineq[ineq] + o_out, ctx->negone);
4608 aff = isl_aff_add(aff, isl_aff_copy(shift));
4609 mod = isl_val_int_from_isl_int(ctx,
4610 bmap->eq[eq][o_out + n_out + div]);
4611 mod = isl_val_abs(mod);
4612 aff = isl_aff_mod_val(aff, mod);
4613 aff = isl_aff_sub(aff, shift);
4616 isl_local_space_free(ls);
4617 return aff;
4618 error:
4619 isl_local_space_free(ls);
4620 isl_aff_free(aff);
4621 return NULL;
4624 /* Given a basic map with output dimensions defined
4625 * in terms of the parameters input dimensions and earlier
4626 * output dimensions using an equality (and possibly a pair on inequalities),
4627 * extract an isl_aff that expresses output dimension "pos" in terms
4628 * of the parameters and input dimensions.
4629 * Note that this expression may involve integer divisions defined
4630 * in terms of parameters and input dimensions.
4631 * "ma" contains the expressions corresponding to earlier output dimensions.
4633 * This function shares some similarities with
4634 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4636 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4637 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4639 int eq, div, ineq;
4640 isl_aff *aff;
4642 if (!bmap)
4643 return NULL;
4644 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4645 if (eq >= bmap->n_eq)
4646 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4647 "unable to find suitable equality", return NULL);
4648 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4650 aff = isl_aff_remove_unused_divs(aff);
4651 return aff;
4654 /* Given a basic map where each output dimension is defined
4655 * in terms of the parameters and input dimensions using an equality,
4656 * extract an isl_multi_aff that expresses the output dimensions in terms
4657 * of the parameters and input dimensions.
4659 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4660 __isl_take isl_basic_map *bmap)
4662 int i;
4663 unsigned n_out;
4664 isl_multi_aff *ma;
4666 if (!bmap)
4667 return NULL;
4669 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4670 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4672 for (i = 0; i < n_out; ++i) {
4673 isl_aff *aff;
4675 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4676 ma = isl_multi_aff_set_aff(ma, i, aff);
4679 isl_basic_map_free(bmap);
4681 return ma;
4684 /* Given a basic set where each set dimension is defined
4685 * in terms of the parameters using an equality,
4686 * extract an isl_multi_aff that expresses the set dimensions in terms
4687 * of the parameters.
4689 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4690 __isl_take isl_basic_set *bset)
4692 return extract_isl_multi_aff_from_basic_map(bset);
4695 /* Create an isl_pw_multi_aff that is equivalent to
4696 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4697 * The given basic map is such that each output dimension is defined
4698 * in terms of the parameters and input dimensions using an equality.
4700 * Since some applications expect the result of isl_pw_multi_aff_from_map
4701 * to only contain integer affine expressions, we compute the floor
4702 * of the expression before returning.
4704 * Remove all constraints involving local variables without
4705 * an explicit representation (resulting in the removal of those
4706 * local variables) prior to the actual extraction to ensure
4707 * that the local spaces in which the resulting affine expressions
4708 * are created do not contain any unknown local variables.
4709 * Removing such constraints is safe because constraints involving
4710 * unknown local variables are not used to determine whether
4711 * a basic map is obviously single-valued.
4713 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4714 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4716 isl_multi_aff *ma;
4718 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4719 ma = extract_isl_multi_aff_from_basic_map(bmap);
4720 ma = isl_multi_aff_floor(ma);
4721 return isl_pw_multi_aff_alloc(domain, ma);
4724 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4725 * This obviously only works if the input "map" is single-valued.
4726 * If so, we compute the lexicographic minimum of the image in the form
4727 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4728 * to its lexicographic minimum.
4729 * If the input is not single-valued, we produce an error.
4731 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4732 __isl_take isl_map *map)
4734 int i;
4735 int sv;
4736 isl_pw_multi_aff *pma;
4738 sv = isl_map_is_single_valued(map);
4739 if (sv < 0)
4740 goto error;
4741 if (!sv)
4742 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4743 "map is not single-valued", goto error);
4744 map = isl_map_make_disjoint(map);
4745 if (!map)
4746 return NULL;
4748 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4750 for (i = 0; i < map->n; ++i) {
4751 isl_pw_multi_aff *pma_i;
4752 isl_basic_map *bmap;
4753 bmap = isl_basic_map_copy(map->p[i]);
4754 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4755 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4758 isl_map_free(map);
4759 return pma;
4760 error:
4761 isl_map_free(map);
4762 return NULL;
4765 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4766 * taking into account that the output dimension at position "d"
4767 * can be represented as
4769 * x = floor((e(...) + c1) / m)
4771 * given that constraint "i" is of the form
4773 * e(...) + c1 - m x >= 0
4776 * Let "map" be of the form
4778 * A -> B
4780 * We construct a mapping
4782 * A -> [A -> x = floor(...)]
4784 * apply that to the map, obtaining
4786 * [A -> x = floor(...)] -> B
4788 * and equate dimension "d" to x.
4789 * We then compute a isl_pw_multi_aff representation of the resulting map
4790 * and plug in the mapping above.
4792 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4793 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4795 isl_ctx *ctx;
4796 isl_space *space;
4797 isl_local_space *ls;
4798 isl_multi_aff *ma;
4799 isl_aff *aff;
4800 isl_vec *v;
4801 isl_map *insert;
4802 int offset;
4803 int n;
4804 int n_in;
4805 isl_pw_multi_aff *pma;
4806 isl_bool is_set;
4808 is_set = isl_map_is_set(map);
4809 if (is_set < 0)
4810 goto error;
4812 offset = isl_basic_map_offset(hull, isl_dim_out);
4813 ctx = isl_map_get_ctx(map);
4814 space = isl_space_domain(isl_map_get_space(map));
4815 n_in = isl_space_dim(space, isl_dim_set);
4816 n = isl_space_dim(space, isl_dim_all);
4818 v = isl_vec_alloc(ctx, 1 + 1 + n);
4819 if (v) {
4820 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4821 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4823 isl_basic_map_free(hull);
4825 ls = isl_local_space_from_space(isl_space_copy(space));
4826 aff = isl_aff_alloc_vec(ls, v);
4827 aff = isl_aff_floor(aff);
4828 if (is_set) {
4829 isl_space_free(space);
4830 ma = isl_multi_aff_from_aff(aff);
4831 } else {
4832 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4833 ma = isl_multi_aff_range_product(ma,
4834 isl_multi_aff_from_aff(aff));
4837 insert = isl_map_from_multi_aff(isl_multi_aff_copy(ma));
4838 map = isl_map_apply_domain(map, insert);
4839 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4840 pma = isl_pw_multi_aff_from_map(map);
4841 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4843 return pma;
4844 error:
4845 isl_map_free(map);
4846 isl_basic_map_free(hull);
4847 return NULL;
4850 /* Is constraint "c" of the form
4852 * e(...) + c1 - m x >= 0
4854 * or
4856 * -e(...) + c2 + m x >= 0
4858 * where m > 1 and e only depends on parameters and input dimemnsions?
4860 * "offset" is the offset of the output dimensions
4861 * "pos" is the position of output dimension x.
4863 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4865 if (isl_int_is_zero(c[offset + d]))
4866 return 0;
4867 if (isl_int_is_one(c[offset + d]))
4868 return 0;
4869 if (isl_int_is_negone(c[offset + d]))
4870 return 0;
4871 if (isl_seq_first_non_zero(c + offset, d) != -1)
4872 return 0;
4873 if (isl_seq_first_non_zero(c + offset + d + 1,
4874 total - (offset + d + 1)) != -1)
4875 return 0;
4876 return 1;
4879 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4881 * As a special case, we first check if there is any pair of constraints,
4882 * shared by all the basic maps in "map" that force a given dimension
4883 * to be equal to the floor of some affine combination of the input dimensions.
4885 * In particular, if we can find two constraints
4887 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4889 * and
4891 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4893 * where m > 1 and e only depends on parameters and input dimemnsions,
4894 * and such that
4896 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4898 * then we know that we can take
4900 * x = floor((e(...) + c1) / m)
4902 * without having to perform any computation.
4904 * Note that we know that
4906 * c1 + c2 >= 1
4908 * If c1 + c2 were 0, then we would have detected an equality during
4909 * simplification. If c1 + c2 were negative, then we would have detected
4910 * a contradiction.
4912 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4913 __isl_take isl_map *map)
4915 int d, dim;
4916 int i, j, n;
4917 int offset, total;
4918 isl_int sum;
4919 isl_basic_map *hull;
4921 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4922 if (!hull)
4923 goto error;
4925 isl_int_init(sum);
4926 dim = isl_map_dim(map, isl_dim_out);
4927 offset = isl_basic_map_offset(hull, isl_dim_out);
4928 total = 1 + isl_basic_map_total_dim(hull);
4929 n = hull->n_ineq;
4930 for (d = 0; d < dim; ++d) {
4931 for (i = 0; i < n; ++i) {
4932 if (!is_potential_div_constraint(hull->ineq[i],
4933 offset, d, total))
4934 continue;
4935 for (j = i + 1; j < n; ++j) {
4936 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4937 hull->ineq[j] + 1, total - 1))
4938 continue;
4939 isl_int_add(sum, hull->ineq[i][0],
4940 hull->ineq[j][0]);
4941 if (isl_int_abs_lt(sum,
4942 hull->ineq[i][offset + d]))
4943 break;
4946 if (j >= n)
4947 continue;
4948 isl_int_clear(sum);
4949 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4950 j = i;
4951 return pw_multi_aff_from_map_div(map, hull, d, j);
4954 isl_int_clear(sum);
4955 isl_basic_map_free(hull);
4956 return pw_multi_aff_from_map_base(map);
4957 error:
4958 isl_map_free(map);
4959 isl_basic_map_free(hull);
4960 return NULL;
4963 /* Given an affine expression
4965 * [A -> B] -> f(A,B)
4967 * construct an isl_multi_aff
4969 * [A -> B] -> B'
4971 * such that dimension "d" in B' is set to "aff" and the remaining
4972 * dimensions are set equal to the corresponding dimensions in B.
4973 * "n_in" is the dimension of the space A.
4974 * "n_out" is the dimension of the space B.
4976 * If "is_set" is set, then the affine expression is of the form
4978 * [B] -> f(B)
4980 * and we construct an isl_multi_aff
4982 * B -> B'
4984 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4985 unsigned n_in, unsigned n_out, int is_set)
4987 int i;
4988 isl_multi_aff *ma;
4989 isl_space *space, *space2;
4990 isl_local_space *ls;
4992 space = isl_aff_get_domain_space(aff);
4993 ls = isl_local_space_from_space(isl_space_copy(space));
4994 space2 = isl_space_copy(space);
4995 if (!is_set)
4996 space2 = isl_space_range(isl_space_unwrap(space2));
4997 space = isl_space_map_from_domain_and_range(space, space2);
4998 ma = isl_multi_aff_alloc(space);
4999 ma = isl_multi_aff_set_aff(ma, d, aff);
5001 for (i = 0; i < n_out; ++i) {
5002 if (i == d)
5003 continue;
5004 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5005 isl_dim_set, n_in + i);
5006 ma = isl_multi_aff_set_aff(ma, i, aff);
5009 isl_local_space_free(ls);
5011 return ma;
5014 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5015 * taking into account that the dimension at position "d" can be written as
5017 * x = m a + f(..) (1)
5019 * where m is equal to "gcd".
5020 * "i" is the index of the equality in "hull" that defines f(..).
5021 * In particular, the equality is of the form
5023 * f(..) - x + m g(existentials) = 0
5025 * or
5027 * -f(..) + x + m g(existentials) = 0
5029 * We basically plug (1) into "map", resulting in a map with "a"
5030 * in the range instead of "x". The corresponding isl_pw_multi_aff
5031 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5033 * Specifically, given the input map
5035 * A -> B
5037 * We first wrap it into a set
5039 * [A -> B]
5041 * and define (1) on top of the corresponding space, resulting in "aff".
5042 * We use this to create an isl_multi_aff that maps the output position "d"
5043 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5044 * We plug this into the wrapped map, unwrap the result and compute the
5045 * corresponding isl_pw_multi_aff.
5046 * The result is an expression
5048 * A -> T(A)
5050 * We adjust that to
5052 * A -> [A -> T(A)]
5054 * so that we can plug that into "aff", after extending the latter to
5055 * a mapping
5057 * [A -> B] -> B'
5060 * If "map" is actually a set, then there is no "A" space, meaning
5061 * that we do not need to perform any wrapping, and that the result
5062 * of the recursive call is of the form
5064 * [T]
5066 * which is plugged into a mapping of the form
5068 * B -> B'
5070 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5071 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5072 isl_int gcd)
5074 isl_set *set;
5075 isl_space *space;
5076 isl_local_space *ls;
5077 isl_aff *aff;
5078 isl_multi_aff *ma;
5079 isl_pw_multi_aff *pma, *id;
5080 unsigned n_in;
5081 unsigned o_out;
5082 unsigned n_out;
5083 isl_bool is_set;
5085 is_set = isl_map_is_set(map);
5086 if (is_set < 0)
5087 goto error;
5089 n_in = isl_basic_map_dim(hull, isl_dim_in);
5090 n_out = isl_basic_map_dim(hull, isl_dim_out);
5091 o_out = isl_basic_map_offset(hull, isl_dim_out);
5093 if (is_set)
5094 set = map;
5095 else
5096 set = isl_map_wrap(map);
5097 space = isl_space_map_from_set(isl_set_get_space(set));
5098 ma = isl_multi_aff_identity(space);
5099 ls = isl_local_space_from_space(isl_set_get_space(set));
5100 aff = isl_aff_alloc(ls);
5101 if (aff) {
5102 isl_int_set_si(aff->v->el[0], 1);
5103 if (isl_int_is_one(hull->eq[i][o_out + d]))
5104 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5105 aff->v->size - 1);
5106 else
5107 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5108 aff->v->size - 1);
5109 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5111 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5112 set = isl_set_preimage_multi_aff(set, ma);
5114 ma = range_map(aff, d, n_in, n_out, is_set);
5116 if (is_set)
5117 map = set;
5118 else
5119 map = isl_set_unwrap(set);
5120 pma = isl_pw_multi_aff_from_map(map);
5122 if (!is_set) {
5123 space = isl_pw_multi_aff_get_domain_space(pma);
5124 space = isl_space_map_from_set(space);
5125 id = isl_pw_multi_aff_identity(space);
5126 pma = isl_pw_multi_aff_range_product(id, pma);
5128 id = isl_pw_multi_aff_from_multi_aff(ma);
5129 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5131 isl_basic_map_free(hull);
5132 return pma;
5133 error:
5134 isl_map_free(map);
5135 isl_basic_map_free(hull);
5136 return NULL;
5139 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5140 * "hull" contains the equalities valid for "map".
5142 * Check if any of the output dimensions is "strided".
5143 * That is, we check if it can be written as
5145 * x = m a + f(..)
5147 * with m greater than 1, a some combination of existentially quantified
5148 * variables and f an expression in the parameters and input dimensions.
5149 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5151 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5152 * special case.
5154 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5155 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5157 int i, j;
5158 unsigned n_out;
5159 unsigned o_out;
5160 unsigned n_div;
5161 unsigned o_div;
5162 isl_int gcd;
5164 n_div = isl_basic_map_dim(hull, isl_dim_div);
5165 o_div = isl_basic_map_offset(hull, isl_dim_div);
5167 if (n_div == 0) {
5168 isl_basic_map_free(hull);
5169 return pw_multi_aff_from_map_check_div(map);
5172 isl_int_init(gcd);
5174 n_out = isl_basic_map_dim(hull, isl_dim_out);
5175 o_out = isl_basic_map_offset(hull, isl_dim_out);
5177 for (i = 0; i < n_out; ++i) {
5178 for (j = 0; j < hull->n_eq; ++j) {
5179 isl_int *eq = hull->eq[j];
5180 isl_pw_multi_aff *res;
5182 if (!isl_int_is_one(eq[o_out + i]) &&
5183 !isl_int_is_negone(eq[o_out + i]))
5184 continue;
5185 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5186 continue;
5187 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5188 n_out - (i + 1)) != -1)
5189 continue;
5190 isl_seq_gcd(eq + o_div, n_div, &gcd);
5191 if (isl_int_is_zero(gcd))
5192 continue;
5193 if (isl_int_is_one(gcd))
5194 continue;
5196 res = pw_multi_aff_from_map_stride(map, hull,
5197 i, j, gcd);
5198 isl_int_clear(gcd);
5199 return res;
5203 isl_int_clear(gcd);
5204 isl_basic_map_free(hull);
5205 return pw_multi_aff_from_map_check_div(map);
5208 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5210 * As a special case, we first check if all output dimensions are uniquely
5211 * defined in terms of the parameters and input dimensions over the entire
5212 * domain. If so, we extract the desired isl_pw_multi_aff directly
5213 * from the affine hull of "map" and its domain.
5215 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5216 * special cases.
5218 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5220 isl_bool sv;
5221 isl_basic_map *hull;
5223 if (!map)
5224 return NULL;
5226 if (isl_map_n_basic_map(map) == 1) {
5227 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5228 hull = isl_basic_map_plain_affine_hull(hull);
5229 sv = isl_basic_map_plain_is_single_valued(hull);
5230 if (sv >= 0 && sv)
5231 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5232 hull);
5233 isl_basic_map_free(hull);
5235 map = isl_map_detect_equalities(map);
5236 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5237 sv = isl_basic_map_plain_is_single_valued(hull);
5238 if (sv >= 0 && sv)
5239 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5240 if (sv >= 0)
5241 return pw_multi_aff_from_map_check_strides(map, hull);
5242 isl_basic_map_free(hull);
5243 isl_map_free(map);
5244 return NULL;
5247 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5249 return isl_pw_multi_aff_from_map(set);
5252 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5253 * add it to *user.
5255 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5257 isl_union_pw_multi_aff **upma = user;
5258 isl_pw_multi_aff *pma;
5260 pma = isl_pw_multi_aff_from_map(map);
5261 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5263 return *upma ? isl_stat_ok : isl_stat_error;
5266 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5267 * domain.
5269 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5270 __isl_take isl_aff *aff)
5272 isl_multi_aff *ma;
5273 isl_pw_multi_aff *pma;
5275 ma = isl_multi_aff_from_aff(aff);
5276 pma = isl_pw_multi_aff_from_multi_aff(ma);
5277 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5280 /* Try and create an isl_union_pw_multi_aff that is equivalent
5281 * to the given isl_union_map.
5282 * The isl_union_map is required to be single-valued in each space.
5283 * Otherwise, an error is produced.
5285 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5286 __isl_take isl_union_map *umap)
5288 isl_space *space;
5289 isl_union_pw_multi_aff *upma;
5291 space = isl_union_map_get_space(umap);
5292 upma = isl_union_pw_multi_aff_empty(space);
5293 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5294 upma = isl_union_pw_multi_aff_free(upma);
5295 isl_union_map_free(umap);
5297 return upma;
5300 /* Try and create an isl_union_pw_multi_aff that is equivalent
5301 * to the given isl_union_set.
5302 * The isl_union_set is required to be a singleton in each space.
5303 * Otherwise, an error is produced.
5305 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5306 __isl_take isl_union_set *uset)
5308 return isl_union_pw_multi_aff_from_union_map(uset);
5311 /* Return the piecewise affine expression "set ? 1 : 0".
5313 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5315 isl_pw_aff *pa;
5316 isl_space *space = isl_set_get_space(set);
5317 isl_local_space *ls = isl_local_space_from_space(space);
5318 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5319 isl_aff *one = isl_aff_zero_on_domain(ls);
5321 one = isl_aff_add_constant_si(one, 1);
5322 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5323 set = isl_set_complement(set);
5324 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5326 return pa;
5329 /* Plug in "subs" for dimension "type", "pos" of "aff".
5331 * Let i be the dimension to replace and let "subs" be of the form
5333 * f/d
5335 * and "aff" of the form
5337 * (a i + g)/m
5339 * The result is
5341 * (a f + d g')/(m d)
5343 * where g' is the result of plugging in "subs" in each of the integer
5344 * divisions in g.
5346 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5347 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5349 isl_ctx *ctx;
5350 isl_int v;
5352 aff = isl_aff_cow(aff);
5353 if (!aff || !subs)
5354 return isl_aff_free(aff);
5356 ctx = isl_aff_get_ctx(aff);
5357 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5358 isl_die(ctx, isl_error_invalid,
5359 "spaces don't match", return isl_aff_free(aff));
5360 if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
5361 isl_die(ctx, isl_error_unsupported,
5362 "cannot handle divs yet", return isl_aff_free(aff));
5364 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5365 if (!aff->ls)
5366 return isl_aff_free(aff);
5368 aff->v = isl_vec_cow(aff->v);
5369 if (!aff->v)
5370 return isl_aff_free(aff);
5372 pos += isl_local_space_offset(aff->ls, type);
5374 isl_int_init(v);
5375 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5376 aff->v->size, subs->v->size, v);
5377 isl_int_clear(v);
5379 return aff;
5382 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5383 * expressions in "maff".
5385 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5386 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5387 __isl_keep isl_aff *subs)
5389 int i;
5391 maff = isl_multi_aff_cow(maff);
5392 if (!maff || !subs)
5393 return isl_multi_aff_free(maff);
5395 if (type == isl_dim_in)
5396 type = isl_dim_set;
5398 for (i = 0; i < maff->n; ++i) {
5399 maff->p[i] = isl_aff_substitute(maff->p[i], type, pos, subs);
5400 if (!maff->p[i])
5401 return isl_multi_aff_free(maff);
5404 return maff;
5407 /* Plug in "subs" for dimension "type", "pos" of "pma".
5409 * pma is of the form
5411 * A_i(v) -> M_i(v)
5413 * while subs is of the form
5415 * v' = B_j(v) -> S_j
5417 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5418 * has a contribution in the result, in particular
5420 * C_ij(S_j) -> M_i(S_j)
5422 * Note that plugging in S_j in C_ij may also result in an empty set
5423 * and this contribution should simply be discarded.
5425 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5426 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5427 __isl_keep isl_pw_aff *subs)
5429 int i, j, n;
5430 isl_pw_multi_aff *res;
5432 if (!pma || !subs)
5433 return isl_pw_multi_aff_free(pma);
5435 n = pma->n * subs->n;
5436 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5438 for (i = 0; i < pma->n; ++i) {
5439 for (j = 0; j < subs->n; ++j) {
5440 isl_set *common;
5441 isl_multi_aff *res_ij;
5442 int empty;
5444 common = isl_set_intersect(
5445 isl_set_copy(pma->p[i].set),
5446 isl_set_copy(subs->p[j].set));
5447 common = isl_set_substitute(common,
5448 type, pos, subs->p[j].aff);
5449 empty = isl_set_plain_is_empty(common);
5450 if (empty < 0 || empty) {
5451 isl_set_free(common);
5452 if (empty < 0)
5453 goto error;
5454 continue;
5457 res_ij = isl_multi_aff_substitute(
5458 isl_multi_aff_copy(pma->p[i].maff),
5459 type, pos, subs->p[j].aff);
5461 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5465 isl_pw_multi_aff_free(pma);
5466 return res;
5467 error:
5468 isl_pw_multi_aff_free(pma);
5469 isl_pw_multi_aff_free(res);
5470 return NULL;
5473 /* Compute the preimage of a range of dimensions in the affine expression "src"
5474 * under "ma" and put the result in "dst". The number of dimensions in "src"
5475 * that precede the range is given by "n_before". The number of dimensions
5476 * in the range is given by the number of output dimensions of "ma".
5477 * The number of dimensions that follow the range is given by "n_after".
5478 * If "has_denom" is set (to one),
5479 * then "src" and "dst" have an extra initial denominator.
5480 * "n_div_ma" is the number of existentials in "ma"
5481 * "n_div_bset" is the number of existentials in "src"
5482 * The resulting "dst" (which is assumed to have been allocated by
5483 * the caller) contains coefficients for both sets of existentials,
5484 * first those in "ma" and then those in "src".
5485 * f, c1, c2 and g are temporary objects that have been initialized
5486 * by the caller.
5488 * Let src represent the expression
5490 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5492 * and let ma represent the expressions
5494 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5496 * We start out with the following expression for dst:
5498 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5500 * with the multiplication factor f initially equal to 1
5501 * and f \sum_i b_i v_i kept separately.
5502 * For each x_i that we substitute, we multiply the numerator
5503 * (and denominator) of dst by c_1 = m_i and add the numerator
5504 * of the x_i expression multiplied by c_2 = f b_i,
5505 * after removing the common factors of c_1 and c_2.
5506 * The multiplication factor f also needs to be multiplied by c_1
5507 * for the next x_j, j > i.
5509 void isl_seq_preimage(isl_int *dst, isl_int *src,
5510 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5511 int n_div_ma, int n_div_bmap,
5512 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5514 int i;
5515 int n_param, n_in, n_out;
5516 int o_dst, o_src;
5518 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5519 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5520 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5522 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5523 o_dst = o_src = has_denom + 1 + n_param + n_before;
5524 isl_seq_clr(dst + o_dst, n_in);
5525 o_dst += n_in;
5526 o_src += n_out;
5527 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5528 o_dst += n_after;
5529 o_src += n_after;
5530 isl_seq_clr(dst + o_dst, n_div_ma);
5531 o_dst += n_div_ma;
5532 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5534 isl_int_set_si(f, 1);
5536 for (i = 0; i < n_out; ++i) {
5537 int offset = has_denom + 1 + n_param + n_before + i;
5539 if (isl_int_is_zero(src[offset]))
5540 continue;
5541 isl_int_set(c1, ma->p[i]->v->el[0]);
5542 isl_int_mul(c2, f, src[offset]);
5543 isl_int_gcd(g, c1, c2);
5544 isl_int_divexact(c1, c1, g);
5545 isl_int_divexact(c2, c2, g);
5547 isl_int_mul(f, f, c1);
5548 o_dst = has_denom;
5549 o_src = 1;
5550 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5551 c2, ma->p[i]->v->el + o_src, 1 + n_param);
5552 o_dst += 1 + n_param;
5553 o_src += 1 + n_param;
5554 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5555 o_dst += n_before;
5556 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5557 c2, ma->p[i]->v->el + o_src, n_in);
5558 o_dst += n_in;
5559 o_src += n_in;
5560 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5561 o_dst += n_after;
5562 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5563 c2, ma->p[i]->v->el + o_src, n_div_ma);
5564 o_dst += n_div_ma;
5565 o_src += n_div_ma;
5566 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5567 if (has_denom)
5568 isl_int_mul(dst[0], dst[0], c1);
5572 /* Compute the pullback of "aff" by the function represented by "ma".
5573 * In other words, plug in "ma" in "aff". The result is an affine expression
5574 * defined over the domain space of "ma".
5576 * If "aff" is represented by
5578 * (a(p) + b x + c(divs))/d
5580 * and ma is represented by
5582 * x = D(p) + F(y) + G(divs')
5584 * then the result is
5586 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5588 * The divs in the local space of the input are similarly adjusted
5589 * through a call to isl_local_space_preimage_multi_aff.
5591 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5592 __isl_take isl_multi_aff *ma)
5594 isl_aff *res = NULL;
5595 isl_local_space *ls;
5596 int n_div_aff, n_div_ma;
5597 isl_int f, c1, c2, g;
5599 ma = isl_multi_aff_align_divs(ma);
5600 if (!aff || !ma)
5601 goto error;
5603 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5604 n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
5606 ls = isl_aff_get_domain_local_space(aff);
5607 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5608 res = isl_aff_alloc(ls);
5609 if (!res)
5610 goto error;
5612 isl_int_init(f);
5613 isl_int_init(c1);
5614 isl_int_init(c2);
5615 isl_int_init(g);
5617 isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0, n_div_ma, n_div_aff,
5618 f, c1, c2, g, 1);
5620 isl_int_clear(f);
5621 isl_int_clear(c1);
5622 isl_int_clear(c2);
5623 isl_int_clear(g);
5625 isl_aff_free(aff);
5626 isl_multi_aff_free(ma);
5627 res = isl_aff_normalize(res);
5628 return res;
5629 error:
5630 isl_aff_free(aff);
5631 isl_multi_aff_free(ma);
5632 isl_aff_free(res);
5633 return NULL;
5636 /* Compute the pullback of "aff1" by the function represented by "aff2".
5637 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5638 * defined over the domain space of "aff1".
5640 * The domain of "aff1" should match the range of "aff2", which means
5641 * that it should be single-dimensional.
5643 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5644 __isl_take isl_aff *aff2)
5646 isl_multi_aff *ma;
5648 ma = isl_multi_aff_from_aff(aff2);
5649 return isl_aff_pullback_multi_aff(aff1, ma);
5652 /* Compute the pullback of "ma1" by the function represented by "ma2".
5653 * In other words, plug in "ma2" in "ma1".
5655 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5657 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5658 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5660 int i;
5661 isl_space *space = NULL;
5663 ma2 = isl_multi_aff_align_divs(ma2);
5664 ma1 = isl_multi_aff_cow(ma1);
5665 if (!ma1 || !ma2)
5666 goto error;
5668 space = isl_space_join(isl_multi_aff_get_space(ma2),
5669 isl_multi_aff_get_space(ma1));
5671 for (i = 0; i < ma1->n; ++i) {
5672 ma1->p[i] = isl_aff_pullback_multi_aff(ma1->p[i],
5673 isl_multi_aff_copy(ma2));
5674 if (!ma1->p[i])
5675 goto error;
5678 ma1 = isl_multi_aff_reset_space(ma1, space);
5679 isl_multi_aff_free(ma2);
5680 return ma1;
5681 error:
5682 isl_space_free(space);
5683 isl_multi_aff_free(ma2);
5684 isl_multi_aff_free(ma1);
5685 return NULL;
5688 /* Compute the pullback of "ma1" by the function represented by "ma2".
5689 * In other words, plug in "ma2" in "ma1".
5691 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5692 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5694 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5695 &isl_multi_aff_pullback_multi_aff_aligned);
5698 /* Extend the local space of "dst" to include the divs
5699 * in the local space of "src".
5701 * If "src" does not have any divs or if the local spaces of "dst" and
5702 * "src" are the same, then no extension is required.
5704 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5705 __isl_keep isl_aff *src)
5707 isl_ctx *ctx;
5708 int src_n_div, dst_n_div;
5709 int *exp1 = NULL;
5710 int *exp2 = NULL;
5711 isl_bool equal;
5712 isl_mat *div;
5714 if (!src || !dst)
5715 return isl_aff_free(dst);
5717 ctx = isl_aff_get_ctx(src);
5718 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5719 if (equal < 0)
5720 return isl_aff_free(dst);
5721 if (!equal)
5722 isl_die(ctx, isl_error_invalid,
5723 "spaces don't match", goto error);
5725 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5726 if (src_n_div == 0)
5727 return dst;
5728 equal = isl_local_space_is_equal(src->ls, dst->ls);
5729 if (equal < 0)
5730 return isl_aff_free(dst);
5731 if (equal)
5732 return dst;
5734 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5735 exp1 = isl_alloc_array(ctx, int, src_n_div);
5736 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5737 if (!exp1 || (dst_n_div && !exp2))
5738 goto error;
5740 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5741 dst = isl_aff_expand_divs(dst, div, exp2);
5742 free(exp1);
5743 free(exp2);
5745 return dst;
5746 error:
5747 free(exp1);
5748 free(exp2);
5749 return isl_aff_free(dst);
5752 /* Adjust the local spaces of the affine expressions in "maff"
5753 * such that they all have the save divs.
5755 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5756 __isl_take isl_multi_aff *maff)
5758 int i;
5760 if (!maff)
5761 return NULL;
5762 if (maff->n == 0)
5763 return maff;
5764 maff = isl_multi_aff_cow(maff);
5765 if (!maff)
5766 return NULL;
5768 for (i = 1; i < maff->n; ++i)
5769 maff->p[0] = isl_aff_align_divs(maff->p[0], maff->p[i]);
5770 for (i = 1; i < maff->n; ++i) {
5771 maff->p[i] = isl_aff_align_divs(maff->p[i], maff->p[0]);
5772 if (!maff->p[i])
5773 return isl_multi_aff_free(maff);
5776 return maff;
5779 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5781 aff = isl_aff_cow(aff);
5782 if (!aff)
5783 return NULL;
5785 aff->ls = isl_local_space_lift(aff->ls);
5786 if (!aff->ls)
5787 return isl_aff_free(aff);
5789 return aff;
5792 /* Lift "maff" to a space with extra dimensions such that the result
5793 * has no more existentially quantified variables.
5794 * If "ls" is not NULL, then *ls is assigned the local space that lies
5795 * at the basis of the lifting applied to "maff".
5797 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5798 __isl_give isl_local_space **ls)
5800 int i;
5801 isl_space *space;
5802 unsigned n_div;
5804 if (ls)
5805 *ls = NULL;
5807 if (!maff)
5808 return NULL;
5810 if (maff->n == 0) {
5811 if (ls) {
5812 isl_space *space = isl_multi_aff_get_domain_space(maff);
5813 *ls = isl_local_space_from_space(space);
5814 if (!*ls)
5815 return isl_multi_aff_free(maff);
5817 return maff;
5820 maff = isl_multi_aff_cow(maff);
5821 maff = isl_multi_aff_align_divs(maff);
5822 if (!maff)
5823 return NULL;
5825 n_div = isl_aff_dim(maff->p[0], isl_dim_div);
5826 space = isl_multi_aff_get_space(maff);
5827 space = isl_space_lift(isl_space_domain(space), n_div);
5828 space = isl_space_extend_domain_with_range(space,
5829 isl_multi_aff_get_space(maff));
5830 if (!space)
5831 return isl_multi_aff_free(maff);
5832 isl_space_free(maff->space);
5833 maff->space = space;
5835 if (ls) {
5836 *ls = isl_aff_get_domain_local_space(maff->p[0]);
5837 if (!*ls)
5838 return isl_multi_aff_free(maff);
5841 for (i = 0; i < maff->n; ++i) {
5842 maff->p[i] = isl_aff_lift(maff->p[i]);
5843 if (!maff->p[i])
5844 goto error;
5847 return maff;
5848 error:
5849 if (ls)
5850 isl_local_space_free(*ls);
5851 return isl_multi_aff_free(maff);
5855 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5857 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5858 __isl_keep isl_pw_multi_aff *pma, int pos)
5860 int i;
5861 int n_out;
5862 isl_space *space;
5863 isl_pw_aff *pa;
5865 if (!pma)
5866 return NULL;
5868 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5869 if (pos < 0 || pos >= n_out)
5870 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
5871 "index out of bounds", return NULL);
5873 space = isl_pw_multi_aff_get_space(pma);
5874 space = isl_space_drop_dims(space, isl_dim_out,
5875 pos + 1, n_out - pos - 1);
5876 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5878 pa = isl_pw_aff_alloc_size(space, pma->n);
5879 for (i = 0; i < pma->n; ++i) {
5880 isl_aff *aff;
5881 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5882 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5885 return pa;
5888 /* Return an isl_pw_multi_aff with the given "set" as domain and
5889 * an unnamed zero-dimensional range.
5891 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5892 __isl_take isl_set *set)
5894 isl_multi_aff *ma;
5895 isl_space *space;
5897 space = isl_set_get_space(set);
5898 space = isl_space_from_domain(space);
5899 ma = isl_multi_aff_zero(space);
5900 return isl_pw_multi_aff_alloc(set, ma);
5903 /* Add an isl_pw_multi_aff with the given "set" as domain and
5904 * an unnamed zero-dimensional range to *user.
5906 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5907 void *user)
5909 isl_union_pw_multi_aff **upma = user;
5910 isl_pw_multi_aff *pma;
5912 pma = isl_pw_multi_aff_from_domain(set);
5913 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5915 return isl_stat_ok;
5918 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5919 * an unnamed zero-dimensional range.
5921 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5922 __isl_take isl_union_set *uset)
5924 isl_space *space;
5925 isl_union_pw_multi_aff *upma;
5927 if (!uset)
5928 return NULL;
5930 space = isl_union_set_get_space(uset);
5931 upma = isl_union_pw_multi_aff_empty(space);
5933 if (isl_union_set_foreach_set(uset,
5934 &add_pw_multi_aff_from_domain, &upma) < 0)
5935 goto error;
5937 isl_union_set_free(uset);
5938 return upma;
5939 error:
5940 isl_union_set_free(uset);
5941 isl_union_pw_multi_aff_free(upma);
5942 return NULL;
5945 /* Convert "pma" to an isl_map and add it to *umap.
5947 static isl_stat map_from_pw_multi_aff(__isl_take isl_pw_multi_aff *pma,
5948 void *user)
5950 isl_union_map **umap = user;
5951 isl_map *map;
5953 map = isl_map_from_pw_multi_aff(pma);
5954 *umap = isl_union_map_add_map(*umap, map);
5956 return isl_stat_ok;
5959 /* Construct a union map mapping the domain of the union
5960 * piecewise multi-affine expression to its range, with each dimension
5961 * in the range equated to the corresponding affine expression on its cell.
5963 __isl_give isl_union_map *isl_union_map_from_union_pw_multi_aff(
5964 __isl_take isl_union_pw_multi_aff *upma)
5966 isl_space *space;
5967 isl_union_map *umap;
5969 if (!upma)
5970 return NULL;
5972 space = isl_union_pw_multi_aff_get_space(upma);
5973 umap = isl_union_map_empty(space);
5975 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
5976 &map_from_pw_multi_aff, &umap) < 0)
5977 goto error;
5979 isl_union_pw_multi_aff_free(upma);
5980 return umap;
5981 error:
5982 isl_union_pw_multi_aff_free(upma);
5983 isl_union_map_free(umap);
5984 return NULL;
5987 /* Local data for bin_entry and the callback "fn".
5989 struct isl_union_pw_multi_aff_bin_data {
5990 isl_union_pw_multi_aff *upma2;
5991 isl_union_pw_multi_aff *res;
5992 isl_pw_multi_aff *pma;
5993 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5996 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5997 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5999 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6001 struct isl_union_pw_multi_aff_bin_data *data = user;
6002 isl_stat r;
6004 data->pma = pma;
6005 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6006 data->fn, data);
6007 isl_pw_multi_aff_free(pma);
6009 return r;
6012 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6013 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6014 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6015 * as *entry. The callback should adjust data->res if desired.
6017 static __isl_give isl_union_pw_multi_aff *bin_op(
6018 __isl_take isl_union_pw_multi_aff *upma1,
6019 __isl_take isl_union_pw_multi_aff *upma2,
6020 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6022 isl_space *space;
6023 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6025 space = isl_union_pw_multi_aff_get_space(upma2);
6026 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6027 space = isl_union_pw_multi_aff_get_space(upma1);
6028 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6030 if (!upma1 || !upma2)
6031 goto error;
6033 data.upma2 = upma2;
6034 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6035 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6036 &bin_entry, &data) < 0)
6037 goto error;
6039 isl_union_pw_multi_aff_free(upma1);
6040 isl_union_pw_multi_aff_free(upma2);
6041 return data.res;
6042 error:
6043 isl_union_pw_multi_aff_free(upma1);
6044 isl_union_pw_multi_aff_free(upma2);
6045 isl_union_pw_multi_aff_free(data.res);
6046 return NULL;
6049 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6050 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6052 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
6053 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6055 isl_space *space;
6057 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6058 isl_pw_multi_aff_get_space(pma2));
6059 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6060 &isl_multi_aff_range_product);
6063 /* Given two isl_pw_multi_affs A -> B and C -> D,
6064 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6066 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6067 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6069 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6070 &pw_multi_aff_range_product);
6073 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6074 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6076 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6077 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6079 isl_space *space;
6081 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6082 isl_pw_multi_aff_get_space(pma2));
6083 space = isl_space_flatten_range(space);
6084 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6085 &isl_multi_aff_flat_range_product);
6088 /* Given two isl_pw_multi_affs A -> B and C -> D,
6089 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6091 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6092 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6094 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6095 &pw_multi_aff_flat_range_product);
6098 /* If data->pma and "pma2" have the same domain space, then compute
6099 * their flat range product and the result to data->res.
6101 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6102 void *user)
6104 struct isl_union_pw_multi_aff_bin_data *data = user;
6106 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6107 pma2->dim, isl_dim_in)) {
6108 isl_pw_multi_aff_free(pma2);
6109 return isl_stat_ok;
6112 pma2 = isl_pw_multi_aff_flat_range_product(
6113 isl_pw_multi_aff_copy(data->pma), pma2);
6115 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6117 return isl_stat_ok;
6120 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6121 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6123 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6124 __isl_take isl_union_pw_multi_aff *upma1,
6125 __isl_take isl_union_pw_multi_aff *upma2)
6127 return bin_op(upma1, upma2, &flat_range_product_entry);
6130 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6131 * The parameters are assumed to have been aligned.
6133 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6134 * except that it works on two different isl_pw_* types.
6136 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6137 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6138 __isl_take isl_pw_aff *pa)
6140 int i, j, n;
6141 isl_pw_multi_aff *res = NULL;
6143 if (!pma || !pa)
6144 goto error;
6146 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6147 pa->dim, isl_dim_in))
6148 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6149 "domains don't match", goto error);
6150 if (pos >= isl_pw_multi_aff_dim(pma, isl_dim_out))
6151 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6152 "index out of bounds", goto error);
6154 n = pma->n * pa->n;
6155 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6157 for (i = 0; i < pma->n; ++i) {
6158 for (j = 0; j < pa->n; ++j) {
6159 isl_set *common;
6160 isl_multi_aff *res_ij;
6161 int empty;
6163 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6164 isl_set_copy(pa->p[j].set));
6165 empty = isl_set_plain_is_empty(common);
6166 if (empty < 0 || empty) {
6167 isl_set_free(common);
6168 if (empty < 0)
6169 goto error;
6170 continue;
6173 res_ij = isl_multi_aff_set_aff(
6174 isl_multi_aff_copy(pma->p[i].maff), pos,
6175 isl_aff_copy(pa->p[j].aff));
6176 res_ij = isl_multi_aff_gist(res_ij,
6177 isl_set_copy(common));
6179 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6183 isl_pw_multi_aff_free(pma);
6184 isl_pw_aff_free(pa);
6185 return res;
6186 error:
6187 isl_pw_multi_aff_free(pma);
6188 isl_pw_aff_free(pa);
6189 return isl_pw_multi_aff_free(res);
6192 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6194 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6195 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6196 __isl_take isl_pw_aff *pa)
6198 isl_bool equal_params;
6200 if (!pma || !pa)
6201 goto error;
6202 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6203 if (equal_params < 0)
6204 goto error;
6205 if (equal_params)
6206 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6207 if (!isl_space_has_named_params(pma->dim) ||
6208 !isl_space_has_named_params(pa->dim))
6209 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6210 "unaligned unnamed parameters", goto error);
6211 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6212 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6213 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6214 error:
6215 isl_pw_multi_aff_free(pma);
6216 isl_pw_aff_free(pa);
6217 return NULL;
6220 /* Do the parameters of "pa" match those of "space"?
6222 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6223 __isl_keep isl_space *space)
6225 isl_space *pa_space;
6226 isl_bool match;
6228 if (!pa || !space)
6229 return isl_bool_error;
6231 pa_space = isl_pw_aff_get_space(pa);
6233 match = isl_space_has_equal_params(space, pa_space);
6235 isl_space_free(pa_space);
6236 return match;
6239 /* Check that the domain space of "pa" matches "space".
6241 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6242 __isl_keep isl_space *space)
6244 isl_space *pa_space;
6245 isl_bool match;
6247 if (!pa || !space)
6248 return isl_stat_error;
6250 pa_space = isl_pw_aff_get_space(pa);
6252 match = isl_space_has_equal_params(space, pa_space);
6253 if (match < 0)
6254 goto error;
6255 if (!match)
6256 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6257 "parameters don't match", goto error);
6258 match = isl_space_tuple_is_equal(space, isl_dim_in,
6259 pa_space, isl_dim_in);
6260 if (match < 0)
6261 goto error;
6262 if (!match)
6263 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6264 "domains don't match", goto error);
6265 isl_space_free(pa_space);
6266 return isl_stat_ok;
6267 error:
6268 isl_space_free(pa_space);
6269 return isl_stat_error;
6272 #undef BASE
6273 #define BASE pw_aff
6274 #undef DOMBASE
6275 #define DOMBASE set
6277 #include <isl_multi_templ.c>
6278 #include <isl_multi_apply_set.c>
6279 #include <isl_multi_coalesce.c>
6280 #include <isl_multi_dims.c>
6281 #include <isl_multi_gist.c>
6282 #include <isl_multi_hash.c>
6283 #include <isl_multi_intersect.c>
6285 /* Scale the elements of "pma" by the corresponding elements of "mv".
6287 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6288 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6290 int i;
6291 isl_bool equal_params;
6293 pma = isl_pw_multi_aff_cow(pma);
6294 if (!pma || !mv)
6295 goto error;
6296 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6297 mv->space, isl_dim_set))
6298 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6299 "spaces don't match", goto error);
6300 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6301 if (equal_params < 0)
6302 goto error;
6303 if (!equal_params) {
6304 pma = isl_pw_multi_aff_align_params(pma,
6305 isl_multi_val_get_space(mv));
6306 mv = isl_multi_val_align_params(mv,
6307 isl_pw_multi_aff_get_space(pma));
6308 if (!pma || !mv)
6309 goto error;
6312 for (i = 0; i < pma->n; ++i) {
6313 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6314 isl_multi_val_copy(mv));
6315 if (!pma->p[i].maff)
6316 goto error;
6319 isl_multi_val_free(mv);
6320 return pma;
6321 error:
6322 isl_multi_val_free(mv);
6323 isl_pw_multi_aff_free(pma);
6324 return NULL;
6327 /* This function is called for each entry of an isl_union_pw_multi_aff.
6328 * If the space of the entry matches that of data->mv,
6329 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6330 * Otherwise, return an empty isl_pw_multi_aff.
6332 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6333 __isl_take isl_pw_multi_aff *pma, void *user)
6335 isl_multi_val *mv = user;
6337 if (!pma)
6338 return NULL;
6339 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6340 mv->space, isl_dim_set)) {
6341 isl_space *space = isl_pw_multi_aff_get_space(pma);
6342 isl_pw_multi_aff_free(pma);
6343 return isl_pw_multi_aff_empty(space);
6346 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6349 /* Scale the elements of "upma" by the corresponding elements of "mv",
6350 * for those entries that match the space of "mv".
6352 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6353 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6355 upma = isl_union_pw_multi_aff_align_params(upma,
6356 isl_multi_val_get_space(mv));
6357 mv = isl_multi_val_align_params(mv,
6358 isl_union_pw_multi_aff_get_space(upma));
6359 if (!upma || !mv)
6360 goto error;
6362 return isl_union_pw_multi_aff_transform(upma,
6363 &union_pw_multi_aff_scale_multi_val_entry, mv);
6365 isl_multi_val_free(mv);
6366 return upma;
6367 error:
6368 isl_multi_val_free(mv);
6369 isl_union_pw_multi_aff_free(upma);
6370 return NULL;
6373 /* Construct and return a piecewise multi affine expression
6374 * in the given space with value zero in each of the output dimensions and
6375 * a universe domain.
6377 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6379 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6382 /* Construct and return a piecewise multi affine expression
6383 * that is equal to the given piecewise affine expression.
6385 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6386 __isl_take isl_pw_aff *pa)
6388 int i;
6389 isl_space *space;
6390 isl_pw_multi_aff *pma;
6392 if (!pa)
6393 return NULL;
6395 space = isl_pw_aff_get_space(pa);
6396 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6398 for (i = 0; i < pa->n; ++i) {
6399 isl_set *set;
6400 isl_multi_aff *ma;
6402 set = isl_set_copy(pa->p[i].set);
6403 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6404 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6407 isl_pw_aff_free(pa);
6408 return pma;
6411 /* Construct a set or map mapping the shared (parameter) domain
6412 * of the piecewise affine expressions to the range of "mpa"
6413 * with each dimension in the range equated to the
6414 * corresponding piecewise affine expression.
6416 static __isl_give isl_map *map_from_multi_pw_aff(
6417 __isl_take isl_multi_pw_aff *mpa)
6419 int i;
6420 isl_space *space;
6421 isl_map *map;
6423 if (!mpa)
6424 return NULL;
6426 if (isl_space_dim(mpa->space, isl_dim_out) != mpa->n)
6427 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6428 "invalid space", goto error);
6430 space = isl_multi_pw_aff_get_domain_space(mpa);
6431 map = isl_map_universe(isl_space_from_domain(space));
6433 for (i = 0; i < mpa->n; ++i) {
6434 isl_pw_aff *pa;
6435 isl_map *map_i;
6437 pa = isl_pw_aff_copy(mpa->p[i]);
6438 map_i = map_from_pw_aff(pa);
6440 map = isl_map_flat_range_product(map, map_i);
6443 map = isl_map_reset_space(map, isl_multi_pw_aff_get_space(mpa));
6445 isl_multi_pw_aff_free(mpa);
6446 return map;
6447 error:
6448 isl_multi_pw_aff_free(mpa);
6449 return NULL;
6452 /* Construct a map mapping the shared domain
6453 * of the piecewise affine expressions to the range of "mpa"
6454 * with each dimension in the range equated to the
6455 * corresponding piecewise affine expression.
6457 __isl_give isl_map *isl_map_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6459 if (!mpa)
6460 return NULL;
6461 if (isl_space_is_set(mpa->space))
6462 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6463 "space of input is not a map", goto error);
6465 return map_from_multi_pw_aff(mpa);
6466 error:
6467 isl_multi_pw_aff_free(mpa);
6468 return NULL;
6471 /* Construct a set mapping the shared parameter domain
6472 * of the piecewise affine expressions to the space of "mpa"
6473 * with each dimension in the range equated to the
6474 * corresponding piecewise affine expression.
6476 __isl_give isl_set *isl_set_from_multi_pw_aff(__isl_take isl_multi_pw_aff *mpa)
6478 if (!mpa)
6479 return NULL;
6480 if (!isl_space_is_set(mpa->space))
6481 isl_die(isl_multi_pw_aff_get_ctx(mpa), isl_error_internal,
6482 "space of input is not a set", goto error);
6484 return map_from_multi_pw_aff(mpa);
6485 error:
6486 isl_multi_pw_aff_free(mpa);
6487 return NULL;
6490 /* Construct and return a piecewise multi affine expression
6491 * that is equal to the given multi piecewise affine expression
6492 * on the shared domain of the piecewise affine expressions.
6494 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6495 __isl_take isl_multi_pw_aff *mpa)
6497 int i;
6498 isl_space *space;
6499 isl_pw_aff *pa;
6500 isl_pw_multi_aff *pma;
6502 if (!mpa)
6503 return NULL;
6505 space = isl_multi_pw_aff_get_space(mpa);
6507 if (mpa->n == 0) {
6508 isl_multi_pw_aff_free(mpa);
6509 return isl_pw_multi_aff_zero(space);
6512 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6513 pma = isl_pw_multi_aff_from_pw_aff(pa);
6515 for (i = 1; i < mpa->n; ++i) {
6516 isl_pw_multi_aff *pma_i;
6518 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6519 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6520 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6523 pma = isl_pw_multi_aff_reset_space(pma, space);
6525 isl_multi_pw_aff_free(mpa);
6526 return pma;
6529 /* Construct and return a multi piecewise affine expression
6530 * that is equal to the given multi affine expression.
6532 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6533 __isl_take isl_multi_aff *ma)
6535 int i, n;
6536 isl_multi_pw_aff *mpa;
6538 if (!ma)
6539 return NULL;
6541 n = isl_multi_aff_dim(ma, isl_dim_out);
6542 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6544 for (i = 0; i < n; ++i) {
6545 isl_pw_aff *pa;
6547 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6548 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6551 isl_multi_aff_free(ma);
6552 return mpa;
6555 /* Construct and return a multi piecewise affine expression
6556 * that is equal to the given piecewise multi affine expression.
6558 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6559 __isl_take isl_pw_multi_aff *pma)
6561 int i, n;
6562 isl_space *space;
6563 isl_multi_pw_aff *mpa;
6565 if (!pma)
6566 return NULL;
6568 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6569 space = isl_pw_multi_aff_get_space(pma);
6570 mpa = isl_multi_pw_aff_alloc(space);
6572 for (i = 0; i < n; ++i) {
6573 isl_pw_aff *pa;
6575 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6576 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6579 isl_pw_multi_aff_free(pma);
6580 return mpa;
6583 /* Do "pa1" and "pa2" represent the same function?
6585 * We first check if they are obviously equal.
6586 * If not, we convert them to maps and check if those are equal.
6588 * If "pa1" or "pa2" contain any NaNs, then they are considered
6589 * not to be the same. A NaN is not equal to anything, not even
6590 * to another NaN.
6592 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6593 __isl_keep isl_pw_aff *pa2)
6595 isl_bool equal;
6596 isl_bool has_nan;
6597 isl_map *map1, *map2;
6599 if (!pa1 || !pa2)
6600 return isl_bool_error;
6602 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6603 if (equal < 0 || equal)
6604 return equal;
6605 has_nan = either_involves_nan(pa1, pa2);
6606 if (has_nan < 0)
6607 return isl_bool_error;
6608 if (has_nan)
6609 return isl_bool_false;
6611 map1 = map_from_pw_aff(isl_pw_aff_copy(pa1));
6612 map2 = map_from_pw_aff(isl_pw_aff_copy(pa2));
6613 equal = isl_map_is_equal(map1, map2);
6614 isl_map_free(map1);
6615 isl_map_free(map2);
6617 return equal;
6620 /* Do "mpa1" and "mpa2" represent the same function?
6622 * Note that we cannot convert the entire isl_multi_pw_aff
6623 * to a map because the domains of the piecewise affine expressions
6624 * may not be the same.
6626 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6627 __isl_keep isl_multi_pw_aff *mpa2)
6629 int i;
6630 isl_bool equal, equal_params;
6632 if (!mpa1 || !mpa2)
6633 return isl_bool_error;
6635 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6636 if (equal_params < 0)
6637 return isl_bool_error;
6638 if (!equal_params) {
6639 if (!isl_space_has_named_params(mpa1->space))
6640 return isl_bool_false;
6641 if (!isl_space_has_named_params(mpa2->space))
6642 return isl_bool_false;
6643 mpa1 = isl_multi_pw_aff_copy(mpa1);
6644 mpa2 = isl_multi_pw_aff_copy(mpa2);
6645 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6646 isl_multi_pw_aff_get_space(mpa2));
6647 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6648 isl_multi_pw_aff_get_space(mpa1));
6649 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6650 isl_multi_pw_aff_free(mpa1);
6651 isl_multi_pw_aff_free(mpa2);
6652 return equal;
6655 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6656 if (equal < 0 || !equal)
6657 return equal;
6659 for (i = 0; i < mpa1->n; ++i) {
6660 equal = isl_pw_aff_is_equal(mpa1->p[i], mpa2->p[i]);
6661 if (equal < 0 || !equal)
6662 return equal;
6665 return isl_bool_true;
6668 /* Do "pma1" and "pma2" represent the same function?
6670 * First check if they are obviously equal.
6671 * If not, then convert them to maps and check if those are equal.
6673 * If "pa1" or "pa2" contain any NaNs, then they are considered
6674 * not to be the same. A NaN is not equal to anything, not even
6675 * to another NaN.
6677 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6678 __isl_keep isl_pw_multi_aff *pma2)
6680 isl_bool equal;
6681 isl_bool has_nan;
6682 isl_map *map1, *map2;
6684 if (!pma1 || !pma2)
6685 return isl_bool_error;
6687 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6688 if (equal < 0 || equal)
6689 return equal;
6690 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6691 if (has_nan >= 0 && !has_nan)
6692 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6693 if (has_nan < 0 || has_nan)
6694 return isl_bool_not(has_nan);
6696 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6697 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6698 equal = isl_map_is_equal(map1, map2);
6699 isl_map_free(map1);
6700 isl_map_free(map2);
6702 return equal;
6705 /* Compute the pullback of "mpa" by the function represented by "ma".
6706 * In other words, plug in "ma" in "mpa".
6708 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6710 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6711 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6713 int i;
6714 isl_space *space = NULL;
6716 mpa = isl_multi_pw_aff_cow(mpa);
6717 if (!mpa || !ma)
6718 goto error;
6720 space = isl_space_join(isl_multi_aff_get_space(ma),
6721 isl_multi_pw_aff_get_space(mpa));
6722 if (!space)
6723 goto error;
6725 for (i = 0; i < mpa->n; ++i) {
6726 mpa->p[i] = isl_pw_aff_pullback_multi_aff(mpa->p[i],
6727 isl_multi_aff_copy(ma));
6728 if (!mpa->p[i])
6729 goto error;
6732 isl_multi_aff_free(ma);
6733 isl_space_free(mpa->space);
6734 mpa->space = space;
6735 return mpa;
6736 error:
6737 isl_space_free(space);
6738 isl_multi_pw_aff_free(mpa);
6739 isl_multi_aff_free(ma);
6740 return NULL;
6743 /* Compute the pullback of "mpa" by the function represented by "ma".
6744 * In other words, plug in "ma" in "mpa".
6746 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6747 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6749 isl_bool equal_params;
6751 if (!mpa || !ma)
6752 goto error;
6753 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6754 if (equal_params < 0)
6755 goto error;
6756 if (equal_params)
6757 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6758 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6759 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6760 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6761 error:
6762 isl_multi_pw_aff_free(mpa);
6763 isl_multi_aff_free(ma);
6764 return NULL;
6767 /* Compute the pullback of "mpa" by the function represented by "pma".
6768 * In other words, plug in "pma" in "mpa".
6770 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6772 static __isl_give isl_multi_pw_aff *
6773 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6774 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6776 int i;
6777 isl_space *space = NULL;
6779 mpa = isl_multi_pw_aff_cow(mpa);
6780 if (!mpa || !pma)
6781 goto error;
6783 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6784 isl_multi_pw_aff_get_space(mpa));
6786 for (i = 0; i < mpa->n; ++i) {
6787 mpa->p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(mpa->p[i],
6788 isl_pw_multi_aff_copy(pma));
6789 if (!mpa->p[i])
6790 goto error;
6793 isl_pw_multi_aff_free(pma);
6794 isl_space_free(mpa->space);
6795 mpa->space = space;
6796 return mpa;
6797 error:
6798 isl_space_free(space);
6799 isl_multi_pw_aff_free(mpa);
6800 isl_pw_multi_aff_free(pma);
6801 return NULL;
6804 /* Compute the pullback of "mpa" by the function represented by "pma".
6805 * In other words, plug in "pma" in "mpa".
6807 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6808 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6810 isl_bool equal_params;
6812 if (!mpa || !pma)
6813 goto error;
6814 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6815 if (equal_params < 0)
6816 goto error;
6817 if (equal_params)
6818 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6819 mpa = isl_multi_pw_aff_align_params(mpa,
6820 isl_pw_multi_aff_get_space(pma));
6821 pma = isl_pw_multi_aff_align_params(pma,
6822 isl_multi_pw_aff_get_space(mpa));
6823 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6824 error:
6825 isl_multi_pw_aff_free(mpa);
6826 isl_pw_multi_aff_free(pma);
6827 return NULL;
6830 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6831 * with the domain of "aff". The domain of the result is the same
6832 * as that of "mpa".
6833 * "mpa" and "aff" are assumed to have been aligned.
6835 * We first extract the parametric constant from "aff", defined
6836 * over the correct domain.
6837 * Then we add the appropriate combinations of the members of "mpa".
6838 * Finally, we add the integer divisions through recursive calls.
6840 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6841 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6843 int i, n_in, n_div;
6844 isl_space *space;
6845 isl_val *v;
6846 isl_pw_aff *pa;
6847 isl_aff *tmp;
6849 n_in = isl_aff_dim(aff, isl_dim_in);
6850 n_div = isl_aff_dim(aff, isl_dim_div);
6852 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6853 tmp = isl_aff_copy(aff);
6854 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6855 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6856 tmp = isl_aff_add_dims(tmp, isl_dim_in,
6857 isl_space_dim(space, isl_dim_set));
6858 tmp = isl_aff_reset_domain_space(tmp, space);
6859 pa = isl_pw_aff_from_aff(tmp);
6861 for (i = 0; i < n_in; ++i) {
6862 isl_pw_aff *pa_i;
6864 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6865 continue;
6866 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6867 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6868 pa_i = isl_pw_aff_scale_val(pa_i, v);
6869 pa = isl_pw_aff_add(pa, pa_i);
6872 for (i = 0; i < n_div; ++i) {
6873 isl_aff *div;
6874 isl_pw_aff *pa_i;
6876 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6877 continue;
6878 div = isl_aff_get_div(aff, i);
6879 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6880 isl_multi_pw_aff_copy(mpa), div);
6881 pa_i = isl_pw_aff_floor(pa_i);
6882 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6883 pa_i = isl_pw_aff_scale_val(pa_i, v);
6884 pa = isl_pw_aff_add(pa, pa_i);
6887 isl_multi_pw_aff_free(mpa);
6888 isl_aff_free(aff);
6890 return pa;
6893 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6894 * with the domain of "aff". The domain of the result is the same
6895 * as that of "mpa".
6897 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6898 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6900 isl_bool equal_params;
6902 if (!aff || !mpa)
6903 goto error;
6904 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6905 if (equal_params < 0)
6906 goto error;
6907 if (equal_params)
6908 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6910 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6911 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6913 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6914 error:
6915 isl_aff_free(aff);
6916 isl_multi_pw_aff_free(mpa);
6917 return NULL;
6920 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6921 * with the domain of "pa". The domain of the result is the same
6922 * as that of "mpa".
6923 * "mpa" and "pa" are assumed to have been aligned.
6925 * We consider each piece in turn. Note that the domains of the
6926 * pieces are assumed to be disjoint and they remain disjoint
6927 * after taking the preimage (over the same function).
6929 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6930 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6932 isl_space *space;
6933 isl_pw_aff *res;
6934 int i;
6936 if (!mpa || !pa)
6937 goto error;
6939 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6940 isl_pw_aff_get_space(pa));
6941 res = isl_pw_aff_empty(space);
6943 for (i = 0; i < pa->n; ++i) {
6944 isl_pw_aff *pa_i;
6945 isl_set *domain;
6947 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6948 isl_multi_pw_aff_copy(mpa),
6949 isl_aff_copy(pa->p[i].aff));
6950 domain = isl_set_copy(pa->p[i].set);
6951 domain = isl_set_preimage_multi_pw_aff(domain,
6952 isl_multi_pw_aff_copy(mpa));
6953 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6954 res = isl_pw_aff_add_disjoint(res, pa_i);
6957 isl_pw_aff_free(pa);
6958 isl_multi_pw_aff_free(mpa);
6959 return res;
6960 error:
6961 isl_pw_aff_free(pa);
6962 isl_multi_pw_aff_free(mpa);
6963 return NULL;
6966 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6967 * with the domain of "pa". The domain of the result is the same
6968 * as that of "mpa".
6970 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6971 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6973 isl_bool equal_params;
6975 if (!pa || !mpa)
6976 goto error;
6977 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6978 if (equal_params < 0)
6979 goto error;
6980 if (equal_params)
6981 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6983 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6984 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6986 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6987 error:
6988 isl_pw_aff_free(pa);
6989 isl_multi_pw_aff_free(mpa);
6990 return NULL;
6993 /* Compute the pullback of "pa" by the function represented by "mpa".
6994 * In other words, plug in "mpa" in "pa".
6995 * "pa" and "mpa" are assumed to have been aligned.
6997 * The pullback is computed by applying "pa" to "mpa".
6999 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7000 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7002 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7005 /* Compute the pullback of "pa" by the function represented by "mpa".
7006 * In other words, plug in "mpa" in "pa".
7008 * The pullback is computed by applying "pa" to "mpa".
7010 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7011 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7013 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7016 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7017 * In other words, plug in "mpa2" in "mpa1".
7019 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7021 * We pullback each member of "mpa1" in turn.
7023 static __isl_give isl_multi_pw_aff *
7024 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
7025 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7027 int i;
7028 isl_space *space = NULL;
7030 mpa1 = isl_multi_pw_aff_cow(mpa1);
7031 if (!mpa1 || !mpa2)
7032 goto error;
7034 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7035 isl_multi_pw_aff_get_space(mpa1));
7037 for (i = 0; i < mpa1->n; ++i) {
7038 mpa1->p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7039 mpa1->p[i], isl_multi_pw_aff_copy(mpa2));
7040 if (!mpa1->p[i])
7041 goto error;
7044 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7046 isl_multi_pw_aff_free(mpa2);
7047 return mpa1;
7048 error:
7049 isl_space_free(space);
7050 isl_multi_pw_aff_free(mpa1);
7051 isl_multi_pw_aff_free(mpa2);
7052 return NULL;
7055 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7056 * In other words, plug in "mpa2" in "mpa1".
7058 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7059 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7061 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7062 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7065 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7066 * of "mpa1" and "mpa2" live in the same space, construct map space
7067 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7068 * with this map space as extract argument.
7070 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7071 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7072 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7073 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7075 int match;
7076 isl_space *space1, *space2;
7077 isl_map *res;
7079 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7080 isl_multi_pw_aff_get_space(mpa2));
7081 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7082 isl_multi_pw_aff_get_space(mpa1));
7083 if (!mpa1 || !mpa2)
7084 goto error;
7085 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7086 mpa2->space, isl_dim_out);
7087 if (match < 0)
7088 goto error;
7089 if (!match)
7090 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7091 "range spaces don't match", goto error);
7092 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7093 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7094 space1 = isl_space_map_from_domain_and_range(space1, space2);
7096 res = order(mpa1, mpa2, space1);
7097 isl_multi_pw_aff_free(mpa1);
7098 isl_multi_pw_aff_free(mpa2);
7099 return res;
7100 error:
7101 isl_multi_pw_aff_free(mpa1);
7102 isl_multi_pw_aff_free(mpa2);
7103 return NULL;
7106 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7107 * where the function values are equal. "space" is the space of the result.
7108 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7110 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7111 * in the sequences are equal.
7113 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7114 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7115 __isl_take isl_space *space)
7117 int i, n;
7118 isl_map *res;
7120 res = isl_map_universe(space);
7122 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7123 for (i = 0; i < n; ++i) {
7124 isl_pw_aff *pa1, *pa2;
7125 isl_map *map;
7127 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7128 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7129 map = isl_pw_aff_eq_map(pa1, pa2);
7130 res = isl_map_intersect(res, map);
7133 return res;
7136 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7137 * where the function values are equal.
7139 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7140 __isl_take isl_multi_pw_aff *mpa2)
7142 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7143 &isl_multi_pw_aff_eq_map_on_space);
7146 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7147 * where the function values of "mpa1" is lexicographically satisfies "base"
7148 * compared to that of "mpa2". "space" is the space of the result.
7149 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7151 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7152 * if its i-th element satisfies "base" when compared to
7153 * the i-th element of "mpa2" while all previous elements are
7154 * pairwise equal.
7156 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7157 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7158 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7159 __isl_take isl_pw_aff *pa2),
7160 __isl_take isl_space *space)
7162 int i, n;
7163 isl_map *res, *rest;
7165 res = isl_map_empty(isl_space_copy(space));
7166 rest = isl_map_universe(space);
7168 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7169 for (i = 0; i < n; ++i) {
7170 isl_pw_aff *pa1, *pa2;
7171 isl_map *map;
7173 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7174 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7175 map = base(pa1, pa2);
7176 map = isl_map_intersect(map, isl_map_copy(rest));
7177 res = isl_map_union(res, map);
7179 if (i == n - 1)
7180 continue;
7182 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7183 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7184 map = isl_pw_aff_eq_map(pa1, pa2);
7185 rest = isl_map_intersect(rest, map);
7188 isl_map_free(rest);
7189 return res;
7192 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7193 * where the function value of "mpa1" is lexicographically less than that
7194 * of "mpa2". "space" is the space of the result.
7195 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7197 * "mpa1" is less than "mpa2" if its i-th element is smaller
7198 * than the i-th element of "mpa2" while all previous elements are
7199 * pairwise equal.
7201 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7202 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7203 __isl_take isl_space *space)
7205 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7206 &isl_pw_aff_lt_map, space);
7209 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7210 * where the function value of "mpa1" is lexicographically less than that
7211 * of "mpa2".
7213 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7214 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7216 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7217 &isl_multi_pw_aff_lex_lt_map_on_space);
7220 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7221 * where the function value of "mpa1" is lexicographically greater than that
7222 * of "mpa2". "space" is the space of the result.
7223 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7225 * "mpa1" is greater than "mpa2" if its i-th element is greater
7226 * than the i-th element of "mpa2" while all previous elements are
7227 * pairwise equal.
7229 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7230 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7231 __isl_take isl_space *space)
7233 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7234 &isl_pw_aff_gt_map, space);
7237 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7238 * where the function value of "mpa1" is lexicographically greater than that
7239 * of "mpa2".
7241 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7242 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7244 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7245 &isl_multi_pw_aff_lex_gt_map_on_space);
7248 /* Compare two isl_affs.
7250 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7251 * than "aff2" and 0 if they are equal.
7253 * The order is fairly arbitrary. We do consider expressions that only involve
7254 * earlier dimensions as "smaller".
7256 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7258 int cmp;
7259 int last1, last2;
7261 if (aff1 == aff2)
7262 return 0;
7264 if (!aff1)
7265 return -1;
7266 if (!aff2)
7267 return 1;
7269 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7270 if (cmp != 0)
7271 return cmp;
7273 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7274 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7275 if (last1 != last2)
7276 return last1 - last2;
7278 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7281 /* Compare two isl_pw_affs.
7283 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7284 * than "pa2" and 0 if they are equal.
7286 * The order is fairly arbitrary. We do consider expressions that only involve
7287 * earlier dimensions as "smaller".
7289 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7290 __isl_keep isl_pw_aff *pa2)
7292 int i;
7293 int cmp;
7295 if (pa1 == pa2)
7296 return 0;
7298 if (!pa1)
7299 return -1;
7300 if (!pa2)
7301 return 1;
7303 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7304 if (cmp != 0)
7305 return cmp;
7307 if (pa1->n != pa2->n)
7308 return pa1->n - pa2->n;
7310 for (i = 0; i < pa1->n; ++i) {
7311 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7312 if (cmp != 0)
7313 return cmp;
7314 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7315 if (cmp != 0)
7316 return cmp;
7319 return 0;
7322 /* Return a piecewise affine expression that is equal to "v" on "domain".
7324 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7325 __isl_take isl_val *v)
7327 isl_space *space;
7328 isl_local_space *ls;
7329 isl_aff *aff;
7331 space = isl_set_get_space(domain);
7332 ls = isl_local_space_from_space(space);
7333 aff = isl_aff_val_on_domain(ls, v);
7335 return isl_pw_aff_alloc(domain, aff);
7338 /* Return a multi affine expression that is equal to "mv" on domain
7339 * space "space".
7341 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7342 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7344 int i, n;
7345 isl_space *space2;
7346 isl_local_space *ls;
7347 isl_multi_aff *ma;
7349 if (!space || !mv)
7350 goto error;
7352 n = isl_multi_val_dim(mv, isl_dim_set);
7353 space2 = isl_multi_val_get_space(mv);
7354 space2 = isl_space_align_params(space2, isl_space_copy(space));
7355 space = isl_space_align_params(space, isl_space_copy(space2));
7356 space = isl_space_map_from_domain_and_range(space, space2);
7357 ma = isl_multi_aff_alloc(isl_space_copy(space));
7358 ls = isl_local_space_from_space(isl_space_domain(space));
7359 for (i = 0; i < n; ++i) {
7360 isl_val *v;
7361 isl_aff *aff;
7363 v = isl_multi_val_get_val(mv, i);
7364 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7365 ma = isl_multi_aff_set_aff(ma, i, aff);
7367 isl_local_space_free(ls);
7369 isl_multi_val_free(mv);
7370 return ma;
7371 error:
7372 isl_space_free(space);
7373 isl_multi_val_free(mv);
7374 return NULL;
7377 /* Return a piecewise multi-affine expression
7378 * that is equal to "mv" on "domain".
7380 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7381 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7383 isl_space *space;
7384 isl_multi_aff *ma;
7386 space = isl_set_get_space(domain);
7387 ma = isl_multi_aff_multi_val_on_space(space, mv);
7389 return isl_pw_multi_aff_alloc(domain, ma);
7392 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7393 * mv is the value that should be attained on each domain set
7394 * res collects the results
7396 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7397 isl_multi_val *mv;
7398 isl_union_pw_multi_aff *res;
7401 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7402 * and add it to data->res.
7404 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7405 void *user)
7407 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7408 isl_pw_multi_aff *pma;
7409 isl_multi_val *mv;
7411 mv = isl_multi_val_copy(data->mv);
7412 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7413 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7415 return data->res ? isl_stat_ok : isl_stat_error;
7418 /* Return a union piecewise multi-affine expression
7419 * that is equal to "mv" on "domain".
7421 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7422 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7424 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7425 isl_space *space;
7427 space = isl_union_set_get_space(domain);
7428 data.res = isl_union_pw_multi_aff_empty(space);
7429 data.mv = mv;
7430 if (isl_union_set_foreach_set(domain,
7431 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7432 data.res = isl_union_pw_multi_aff_free(data.res);
7433 isl_union_set_free(domain);
7434 isl_multi_val_free(mv);
7435 return data.res;
7438 /* Compute the pullback of data->pma by the function represented by "pma2",
7439 * provided the spaces match, and add the results to data->res.
7441 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7443 struct isl_union_pw_multi_aff_bin_data *data = user;
7445 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7446 pma2->dim, isl_dim_out)) {
7447 isl_pw_multi_aff_free(pma2);
7448 return isl_stat_ok;
7451 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7452 isl_pw_multi_aff_copy(data->pma), pma2);
7454 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7455 if (!data->res)
7456 return isl_stat_error;
7458 return isl_stat_ok;
7461 /* Compute the pullback of "upma1" by the function represented by "upma2".
7463 __isl_give isl_union_pw_multi_aff *
7464 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7465 __isl_take isl_union_pw_multi_aff *upma1,
7466 __isl_take isl_union_pw_multi_aff *upma2)
7468 return bin_op(upma1, upma2, &pullback_entry);
7471 /* Check that the domain space of "upa" matches "space".
7473 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7474 * can in principle never fail since the space "space" is that
7475 * of the isl_multi_union_pw_aff and is a set space such that
7476 * there is no domain space to match.
7478 * We check the parameters and double-check that "space" is
7479 * indeed that of a set.
7481 static isl_stat isl_union_pw_aff_check_match_domain_space(
7482 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7484 isl_space *upa_space;
7485 isl_bool match;
7487 if (!upa || !space)
7488 return isl_stat_error;
7490 match = isl_space_is_set(space);
7491 if (match < 0)
7492 return isl_stat_error;
7493 if (!match)
7494 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7495 "expecting set space", return -1);
7497 upa_space = isl_union_pw_aff_get_space(upa);
7498 match = isl_space_has_equal_params(space, upa_space);
7499 if (match < 0)
7500 goto error;
7501 if (!match)
7502 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7503 "parameters don't match", goto error);
7505 isl_space_free(upa_space);
7506 return isl_stat_ok;
7507 error:
7508 isl_space_free(upa_space);
7509 return isl_stat_error;
7512 /* Do the parameters of "upa" match those of "space"?
7514 static isl_bool isl_union_pw_aff_matching_params(
7515 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7517 isl_space *upa_space;
7518 isl_bool match;
7520 if (!upa || !space)
7521 return isl_bool_error;
7523 upa_space = isl_union_pw_aff_get_space(upa);
7525 match = isl_space_has_equal_params(space, upa_space);
7527 isl_space_free(upa_space);
7528 return match;
7531 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7532 * space represents the new parameters.
7533 * res collects the results.
7535 struct isl_union_pw_aff_reset_params_data {
7536 isl_space *space;
7537 isl_union_pw_aff *res;
7540 /* Replace the parameters of "pa" by data->space and
7541 * add the result to data->res.
7543 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7545 struct isl_union_pw_aff_reset_params_data *data = user;
7546 isl_space *space;
7548 space = isl_pw_aff_get_space(pa);
7549 space = isl_space_replace_params(space, data->space);
7550 pa = isl_pw_aff_reset_space(pa, space);
7551 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7553 return data->res ? isl_stat_ok : isl_stat_error;
7556 /* Replace the domain space of "upa" by "space".
7557 * Since a union expression does not have a (single) domain space,
7558 * "space" is necessarily a parameter space.
7560 * Since the order and the names of the parameters determine
7561 * the hash value, we need to create a new hash table.
7563 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7564 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7566 struct isl_union_pw_aff_reset_params_data data = { space };
7567 isl_bool match;
7569 match = isl_union_pw_aff_matching_params(upa, space);
7570 if (match < 0)
7571 upa = isl_union_pw_aff_free(upa);
7572 else if (match) {
7573 isl_space_free(space);
7574 return upa;
7577 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7578 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7579 data.res = isl_union_pw_aff_free(data.res);
7581 isl_union_pw_aff_free(upa);
7582 isl_space_free(space);
7583 return data.res;
7586 /* Return the floor of "pa".
7588 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7590 return isl_pw_aff_floor(pa);
7593 /* Given f, return floor(f).
7595 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7596 __isl_take isl_union_pw_aff *upa)
7598 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7601 /* Compute
7603 * upa mod m = upa - m * floor(upa/m)
7605 * with m an integer value.
7607 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7608 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7610 isl_union_pw_aff *res;
7612 if (!upa || !m)
7613 goto error;
7615 if (!isl_val_is_int(m))
7616 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7617 "expecting integer modulo", goto error);
7618 if (!isl_val_is_pos(m))
7619 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7620 "expecting positive modulo", goto error);
7622 res = isl_union_pw_aff_copy(upa);
7623 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7624 upa = isl_union_pw_aff_floor(upa);
7625 upa = isl_union_pw_aff_scale_val(upa, m);
7626 res = isl_union_pw_aff_sub(res, upa);
7628 return res;
7629 error:
7630 isl_val_free(m);
7631 isl_union_pw_aff_free(upa);
7632 return NULL;
7635 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7636 * pos is the output position that needs to be extracted.
7637 * res collects the results.
7639 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7640 int pos;
7641 isl_union_pw_aff *res;
7644 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7645 * (assuming it has such a dimension) and add it to data->res.
7647 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7649 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7650 int n_out;
7651 isl_pw_aff *pa;
7653 if (!pma)
7654 return isl_stat_error;
7656 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7657 if (data->pos >= n_out) {
7658 isl_pw_multi_aff_free(pma);
7659 return isl_stat_ok;
7662 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7663 isl_pw_multi_aff_free(pma);
7665 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7667 return data->res ? isl_stat_ok : isl_stat_error;
7670 /* Extract an isl_union_pw_aff corresponding to
7671 * output dimension "pos" of "upma".
7673 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7674 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7676 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7677 isl_space *space;
7679 if (!upma)
7680 return NULL;
7682 if (pos < 0)
7683 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7684 "cannot extract at negative position", return NULL);
7686 space = isl_union_pw_multi_aff_get_space(upma);
7687 data.res = isl_union_pw_aff_empty(space);
7688 data.pos = pos;
7689 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7690 &get_union_pw_aff, &data) < 0)
7691 data.res = isl_union_pw_aff_free(data.res);
7693 return data.res;
7696 /* Return a union piecewise affine expression
7697 * that is equal to "aff" on "domain".
7699 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7700 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7702 isl_pw_aff *pa;
7704 pa = isl_pw_aff_from_aff(aff);
7705 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7708 /* Return a union piecewise affine expression
7709 * that is equal to the parameter identified by "id" on "domain".
7711 * Make sure the parameter appears in the space passed to
7712 * isl_aff_param_on_domain_space_id.
7714 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7715 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7717 isl_space *space;
7718 isl_aff *aff;
7720 space = isl_union_set_get_space(domain);
7721 space = isl_space_add_param_id(space, isl_id_copy(id));
7722 aff = isl_aff_param_on_domain_space_id(space, id);
7723 return isl_union_pw_aff_aff_on_domain(domain, aff);
7726 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7727 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7728 * needs to attain.
7729 * "res" collects the results.
7731 struct isl_union_pw_aff_pw_aff_on_domain_data {
7732 isl_pw_aff *pa;
7733 isl_union_pw_aff *res;
7736 /* Construct a piecewise affine expression that is equal to data->pa
7737 * on "domain" and add the result to data->res.
7739 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7741 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7742 isl_pw_aff *pa;
7743 int dim;
7745 pa = isl_pw_aff_copy(data->pa);
7746 dim = isl_set_dim(domain, isl_dim_set);
7747 pa = isl_pw_aff_from_range(pa);
7748 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7749 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7750 pa = isl_pw_aff_intersect_domain(pa, domain);
7751 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7753 return data->res ? isl_stat_ok : isl_stat_error;
7756 /* Return a union piecewise affine expression
7757 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7758 * have been aligned.
7760 * Construct an isl_pw_aff on each of the sets in "domain" and
7761 * collect the results.
7763 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7764 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7766 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7767 isl_space *space;
7769 space = isl_union_set_get_space(domain);
7770 data.res = isl_union_pw_aff_empty(space);
7771 data.pa = pa;
7772 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7773 data.res = isl_union_pw_aff_free(data.res);
7774 isl_union_set_free(domain);
7775 isl_pw_aff_free(pa);
7776 return data.res;
7779 /* Return a union piecewise affine expression
7780 * that is equal to "pa" on "domain".
7782 * Check that "pa" is a parametric expression,
7783 * align the parameters if needed and call
7784 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7786 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7787 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7789 isl_bool is_set;
7790 isl_bool equal_params;
7791 isl_space *domain_space, *pa_space;
7793 pa_space = isl_pw_aff_peek_space(pa);
7794 is_set = isl_space_is_set(pa_space);
7795 if (is_set < 0)
7796 goto error;
7797 if (!is_set)
7798 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7799 "expecting parametric expression", goto error);
7801 domain_space = isl_union_set_get_space(domain);
7802 pa_space = isl_pw_aff_get_space(pa);
7803 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7804 if (equal_params >= 0 && !equal_params) {
7805 isl_space *space;
7807 space = isl_space_align_params(domain_space, pa_space);
7808 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7809 domain = isl_union_set_align_params(domain, space);
7810 } else {
7811 isl_space_free(domain_space);
7812 isl_space_free(pa_space);
7815 if (equal_params < 0)
7816 goto error;
7817 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7818 error:
7819 isl_union_set_free(domain);
7820 isl_pw_aff_free(pa);
7821 return NULL;
7824 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7825 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7826 * "res" collects the results.
7828 struct isl_union_pw_aff_val_on_domain_data {
7829 isl_val *v;
7830 isl_union_pw_aff *res;
7833 /* Construct a piecewise affine expression that is equal to data->v
7834 * on "domain" and add the result to data->res.
7836 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7838 struct isl_union_pw_aff_val_on_domain_data *data = user;
7839 isl_pw_aff *pa;
7840 isl_val *v;
7842 v = isl_val_copy(data->v);
7843 pa = isl_pw_aff_val_on_domain(domain, v);
7844 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7846 return data->res ? isl_stat_ok : isl_stat_error;
7849 /* Return a union piecewise affine expression
7850 * that is equal to "v" on "domain".
7852 * Construct an isl_pw_aff on each of the sets in "domain" and
7853 * collect the results.
7855 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7856 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7858 struct isl_union_pw_aff_val_on_domain_data data;
7859 isl_space *space;
7861 space = isl_union_set_get_space(domain);
7862 data.res = isl_union_pw_aff_empty(space);
7863 data.v = v;
7864 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7865 data.res = isl_union_pw_aff_free(data.res);
7866 isl_union_set_free(domain);
7867 isl_val_free(v);
7868 return data.res;
7871 /* Construct a piecewise multi affine expression
7872 * that is equal to "pa" and add it to upma.
7874 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7875 void *user)
7877 isl_union_pw_multi_aff **upma = user;
7878 isl_pw_multi_aff *pma;
7880 pma = isl_pw_multi_aff_from_pw_aff(pa);
7881 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7883 return *upma ? isl_stat_ok : isl_stat_error;
7886 /* Construct and return a union piecewise multi affine expression
7887 * that is equal to the given union piecewise affine expression.
7889 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7890 __isl_take isl_union_pw_aff *upa)
7892 isl_space *space;
7893 isl_union_pw_multi_aff *upma;
7895 if (!upa)
7896 return NULL;
7898 space = isl_union_pw_aff_get_space(upa);
7899 upma = isl_union_pw_multi_aff_empty(space);
7901 if (isl_union_pw_aff_foreach_pw_aff(upa,
7902 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7903 upma = isl_union_pw_multi_aff_free(upma);
7905 isl_union_pw_aff_free(upa);
7906 return upma;
7909 /* Compute the set of elements in the domain of "pa" where it is zero and
7910 * add this set to "uset".
7912 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7914 isl_union_set **uset = (isl_union_set **)user;
7916 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7918 return *uset ? isl_stat_ok : isl_stat_error;
7921 /* Return a union set containing those elements in the domain
7922 * of "upa" where it is zero.
7924 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7925 __isl_take isl_union_pw_aff *upa)
7927 isl_union_set *zero;
7929 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7930 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7931 zero = isl_union_set_free(zero);
7933 isl_union_pw_aff_free(upa);
7934 return zero;
7937 /* Convert "pa" to an isl_map and add it to *umap.
7939 static isl_stat map_from_pw_aff_entry(__isl_take isl_pw_aff *pa, void *user)
7941 isl_union_map **umap = user;
7942 isl_map *map;
7944 map = isl_map_from_pw_aff(pa);
7945 *umap = isl_union_map_add_map(*umap, map);
7947 return *umap ? isl_stat_ok : isl_stat_error;
7950 /* Construct a union map mapping the domain of the union
7951 * piecewise affine expression to its range, with the single output dimension
7952 * equated to the corresponding affine expressions on their cells.
7954 __isl_give isl_union_map *isl_union_map_from_union_pw_aff(
7955 __isl_take isl_union_pw_aff *upa)
7957 isl_space *space;
7958 isl_union_map *umap;
7960 if (!upa)
7961 return NULL;
7963 space = isl_union_pw_aff_get_space(upa);
7964 umap = isl_union_map_empty(space);
7966 if (isl_union_pw_aff_foreach_pw_aff(upa, &map_from_pw_aff_entry,
7967 &umap) < 0)
7968 umap = isl_union_map_free(umap);
7970 isl_union_pw_aff_free(upa);
7971 return umap;
7974 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7975 * upma is the function that is plugged in.
7976 * pa is the current part of the function in which upma is plugged in.
7977 * res collects the results.
7979 struct isl_union_pw_aff_pullback_upma_data {
7980 isl_union_pw_multi_aff *upma;
7981 isl_pw_aff *pa;
7982 isl_union_pw_aff *res;
7985 /* Check if "pma" can be plugged into data->pa.
7986 * If so, perform the pullback and add the result to data->res.
7988 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7990 struct isl_union_pw_aff_pullback_upma_data *data = user;
7991 isl_pw_aff *pa;
7993 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7994 pma->dim, isl_dim_out)) {
7995 isl_pw_multi_aff_free(pma);
7996 return isl_stat_ok;
7999 pa = isl_pw_aff_copy(data->pa);
8000 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8002 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8004 return data->res ? isl_stat_ok : isl_stat_error;
8007 /* Check if any of the elements of data->upma can be plugged into pa,
8008 * add if so add the result to data->res.
8010 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8012 struct isl_union_pw_aff_pullback_upma_data *data = user;
8013 isl_stat r;
8015 data->pa = pa;
8016 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8017 &pa_pb_pma, data);
8018 isl_pw_aff_free(pa);
8020 return r;
8023 /* Compute the pullback of "upa" by the function represented by "upma".
8024 * In other words, plug in "upma" in "upa". The result contains
8025 * expressions defined over the domain space of "upma".
8027 * Run over all pairs of elements in "upa" and "upma", perform
8028 * the pullback when appropriate and collect the results.
8029 * If the hash value were based on the domain space rather than
8030 * the function space, then we could run through all elements
8031 * of "upma" and directly pick out the corresponding element of "upa".
8033 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8034 __isl_take isl_union_pw_aff *upa,
8035 __isl_take isl_union_pw_multi_aff *upma)
8037 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8038 isl_space *space;
8040 space = isl_union_pw_multi_aff_get_space(upma);
8041 upa = isl_union_pw_aff_align_params(upa, space);
8042 space = isl_union_pw_aff_get_space(upa);
8043 upma = isl_union_pw_multi_aff_align_params(upma, space);
8045 if (!upa || !upma)
8046 goto error;
8048 data.upma = upma;
8049 data.res = isl_union_pw_aff_alloc_same_size(upa);
8050 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8051 data.res = isl_union_pw_aff_free(data.res);
8053 isl_union_pw_aff_free(upa);
8054 isl_union_pw_multi_aff_free(upma);
8055 return data.res;
8056 error:
8057 isl_union_pw_aff_free(upa);
8058 isl_union_pw_multi_aff_free(upma);
8059 return NULL;
8062 #undef BASE
8063 #define BASE union_pw_aff
8064 #undef DOMBASE
8065 #define DOMBASE union_set
8067 #define NO_MOVE_DIMS
8068 #define NO_DOMAIN
8069 #define NO_PRODUCT
8070 #define NO_SPLICE
8071 #define NO_ZERO
8072 #define NO_IDENTITY
8073 #define NO_GIST
8075 #include <isl_multi_templ.c>
8076 #include <isl_multi_apply_set.c>
8077 #include <isl_multi_apply_union_set.c>
8078 #include <isl_multi_coalesce.c>
8079 #include <isl_multi_floor.c>
8080 #include <isl_multi_gist.c>
8081 #include <isl_multi_intersect.c>
8083 /* Construct a multiple union piecewise affine expression
8084 * in the given space with value zero in each of the output dimensions.
8086 * Since there is no canonical zero value for
8087 * a union piecewise affine expression, we can only construct
8088 * a zero-dimensional "zero" value.
8090 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8091 __isl_take isl_space *space)
8093 isl_bool params;
8095 if (!space)
8096 return NULL;
8098 params = isl_space_is_params(space);
8099 if (params < 0)
8100 goto error;
8101 if (params)
8102 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8103 "expecting proper set space", goto error);
8104 if (!isl_space_is_set(space))
8105 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8106 "expecting set space", goto error);
8107 if (isl_space_dim(space , isl_dim_out) != 0)
8108 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8109 "expecting 0D space", goto error);
8111 return isl_multi_union_pw_aff_alloc(space);
8112 error:
8113 isl_space_free(space);
8114 return NULL;
8117 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8118 * with the actual sum on the shared domain and
8119 * the defined expression on the symmetric difference of the domains.
8121 * We simply iterate over the elements in both arguments and
8122 * call isl_union_pw_aff_union_add on each of them.
8124 static __isl_give isl_multi_union_pw_aff *
8125 isl_multi_union_pw_aff_union_add_aligned(
8126 __isl_take isl_multi_union_pw_aff *mupa1,
8127 __isl_take isl_multi_union_pw_aff *mupa2)
8129 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8130 &isl_union_pw_aff_union_add);
8133 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8134 * with the actual sum on the shared domain and
8135 * the defined expression on the symmetric difference of the domains.
8137 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8138 __isl_take isl_multi_union_pw_aff *mupa1,
8139 __isl_take isl_multi_union_pw_aff *mupa2)
8141 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8142 &isl_multi_union_pw_aff_union_add_aligned);
8145 /* Construct and return a multi union piecewise affine expression
8146 * that is equal to the given multi affine expression.
8148 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8149 __isl_take isl_multi_aff *ma)
8151 isl_multi_pw_aff *mpa;
8153 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8154 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8157 /* Construct and return a multi union piecewise affine expression
8158 * that is equal to the given multi piecewise affine expression.
8160 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8161 __isl_take isl_multi_pw_aff *mpa)
8163 int i, n;
8164 isl_space *space;
8165 isl_multi_union_pw_aff *mupa;
8167 if (!mpa)
8168 return NULL;
8170 space = isl_multi_pw_aff_get_space(mpa);
8171 space = isl_space_range(space);
8172 mupa = isl_multi_union_pw_aff_alloc(space);
8174 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8175 for (i = 0; i < n; ++i) {
8176 isl_pw_aff *pa;
8177 isl_union_pw_aff *upa;
8179 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8180 upa = isl_union_pw_aff_from_pw_aff(pa);
8181 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8184 isl_multi_pw_aff_free(mpa);
8186 return mupa;
8189 /* Extract the range space of "pma" and assign it to *space.
8190 * If *space has already been set (through a previous call to this function),
8191 * then check that the range space is the same.
8193 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8195 isl_space **space = user;
8196 isl_space *pma_space;
8197 isl_bool equal;
8199 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8200 isl_pw_multi_aff_free(pma);
8202 if (!pma_space)
8203 return isl_stat_error;
8204 if (!*space) {
8205 *space = pma_space;
8206 return isl_stat_ok;
8209 equal = isl_space_is_equal(pma_space, *space);
8210 isl_space_free(pma_space);
8212 if (equal < 0)
8213 return isl_stat_error;
8214 if (!equal)
8215 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8216 "range spaces not the same", return isl_stat_error);
8217 return isl_stat_ok;
8220 /* Construct and return a multi union piecewise affine expression
8221 * that is equal to the given union piecewise multi affine expression.
8223 * In order to be able to perform the conversion, the input
8224 * needs to be non-empty and may only involve a single range space.
8226 __isl_give isl_multi_union_pw_aff *
8227 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8228 __isl_take isl_union_pw_multi_aff *upma)
8230 isl_space *space = NULL;
8231 isl_multi_union_pw_aff *mupa;
8232 int i, n;
8234 if (!upma)
8235 return NULL;
8236 if (isl_union_pw_multi_aff_n_pw_multi_aff(upma) == 0)
8237 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8238 "cannot extract range space from empty input",
8239 goto error);
8240 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8241 &space) < 0)
8242 goto error;
8244 if (!space)
8245 goto error;
8247 n = isl_space_dim(space, isl_dim_set);
8248 mupa = isl_multi_union_pw_aff_alloc(space);
8250 for (i = 0; i < n; ++i) {
8251 isl_union_pw_aff *upa;
8253 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8254 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8257 isl_union_pw_multi_aff_free(upma);
8258 return mupa;
8259 error:
8260 isl_space_free(space);
8261 isl_union_pw_multi_aff_free(upma);
8262 return NULL;
8265 /* Try and create an isl_multi_union_pw_aff that is equivalent
8266 * to the given isl_union_map.
8267 * The isl_union_map is required to be single-valued in each space.
8268 * Moreover, it cannot be empty and all range spaces need to be the same.
8269 * Otherwise, an error is produced.
8271 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8272 __isl_take isl_union_map *umap)
8274 isl_union_pw_multi_aff *upma;
8276 upma = isl_union_pw_multi_aff_from_union_map(umap);
8277 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8280 /* Return a multiple union piecewise affine expression
8281 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8282 * have been aligned.
8284 static __isl_give isl_multi_union_pw_aff *
8285 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8286 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8288 int i, n;
8289 isl_space *space;
8290 isl_multi_union_pw_aff *mupa;
8292 if (!domain || !mv)
8293 goto error;
8295 n = isl_multi_val_dim(mv, isl_dim_set);
8296 space = isl_multi_val_get_space(mv);
8297 mupa = isl_multi_union_pw_aff_alloc(space);
8298 for (i = 0; i < n; ++i) {
8299 isl_val *v;
8300 isl_union_pw_aff *upa;
8302 v = isl_multi_val_get_val(mv, i);
8303 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8305 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8308 isl_union_set_free(domain);
8309 isl_multi_val_free(mv);
8310 return mupa;
8311 error:
8312 isl_union_set_free(domain);
8313 isl_multi_val_free(mv);
8314 return NULL;
8317 /* Return a multiple union piecewise affine expression
8318 * that is equal to "mv" on "domain".
8320 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8321 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8323 isl_bool equal_params;
8325 if (!domain || !mv)
8326 goto error;
8327 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8328 if (equal_params < 0)
8329 goto error;
8330 if (equal_params)
8331 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8332 domain, mv);
8333 domain = isl_union_set_align_params(domain,
8334 isl_multi_val_get_space(mv));
8335 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8336 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8337 error:
8338 isl_union_set_free(domain);
8339 isl_multi_val_free(mv);
8340 return NULL;
8343 /* Return a multiple union piecewise affine expression
8344 * that is equal to "ma" on "domain".
8346 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8347 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8349 isl_pw_multi_aff *pma;
8351 pma = isl_pw_multi_aff_from_multi_aff(ma);
8352 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8355 /* Return a multiple union piecewise affine expression
8356 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8357 * have been aligned.
8359 static __isl_give isl_multi_union_pw_aff *
8360 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8361 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8363 int i, n;
8364 isl_space *space;
8365 isl_multi_union_pw_aff *mupa;
8367 if (!domain || !pma)
8368 goto error;
8370 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8371 space = isl_pw_multi_aff_get_space(pma);
8372 mupa = isl_multi_union_pw_aff_alloc(space);
8373 for (i = 0; i < n; ++i) {
8374 isl_pw_aff *pa;
8375 isl_union_pw_aff *upa;
8377 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8378 upa = isl_union_pw_aff_pw_aff_on_domain(
8379 isl_union_set_copy(domain), pa);
8380 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8383 isl_union_set_free(domain);
8384 isl_pw_multi_aff_free(pma);
8385 return mupa;
8386 error:
8387 isl_union_set_free(domain);
8388 isl_pw_multi_aff_free(pma);
8389 return NULL;
8392 /* Return a multiple union piecewise affine expression
8393 * that is equal to "pma" on "domain".
8395 __isl_give isl_multi_union_pw_aff *
8396 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8397 __isl_take isl_pw_multi_aff *pma)
8399 isl_bool equal_params;
8400 isl_space *space;
8402 space = isl_pw_multi_aff_peek_space(pma);
8403 equal_params = isl_union_set_space_has_equal_params(domain, space);
8404 if (equal_params < 0)
8405 goto error;
8406 if (equal_params)
8407 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8408 domain, pma);
8409 domain = isl_union_set_align_params(domain,
8410 isl_pw_multi_aff_get_space(pma));
8411 pma = isl_pw_multi_aff_align_params(pma,
8412 isl_union_set_get_space(domain));
8413 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8414 pma);
8415 error:
8416 isl_union_set_free(domain);
8417 isl_pw_multi_aff_free(pma);
8418 return NULL;
8421 /* Return a union set containing those elements in the domains
8422 * of the elements of "mupa" where they are all zero.
8424 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8425 __isl_take isl_multi_union_pw_aff *mupa)
8427 int i, n;
8428 isl_union_pw_aff *upa;
8429 isl_union_set *zero;
8431 if (!mupa)
8432 return NULL;
8434 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8435 if (n == 0)
8436 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8437 "cannot determine zero set "
8438 "of zero-dimensional function", goto error);
8440 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8441 zero = isl_union_pw_aff_zero_union_set(upa);
8443 for (i = 1; i < n; ++i) {
8444 isl_union_set *zero_i;
8446 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8447 zero_i = isl_union_pw_aff_zero_union_set(upa);
8449 zero = isl_union_set_intersect(zero, zero_i);
8452 isl_multi_union_pw_aff_free(mupa);
8453 return zero;
8454 error:
8455 isl_multi_union_pw_aff_free(mupa);
8456 return NULL;
8459 /* Construct a union map mapping the shared domain
8460 * of the union piecewise affine expressions to the range of "mupa"
8461 * with each dimension in the range equated to the
8462 * corresponding union piecewise affine expression.
8464 * The input cannot be zero-dimensional as there is
8465 * no way to extract a domain from a zero-dimensional isl_multi_union_pw_aff.
8467 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8468 __isl_take isl_multi_union_pw_aff *mupa)
8470 int i, n;
8471 isl_space *space;
8472 isl_union_map *umap;
8473 isl_union_pw_aff *upa;
8475 if (!mupa)
8476 return NULL;
8478 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8479 if (n == 0)
8480 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8481 "cannot determine domain of zero-dimensional "
8482 "isl_multi_union_pw_aff", goto error);
8484 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8485 umap = isl_union_map_from_union_pw_aff(upa);
8487 for (i = 1; i < n; ++i) {
8488 isl_union_map *umap_i;
8490 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8491 umap_i = isl_union_map_from_union_pw_aff(upa);
8492 umap = isl_union_map_flat_range_product(umap, umap_i);
8495 space = isl_multi_union_pw_aff_get_space(mupa);
8496 umap = isl_union_map_reset_range_space(umap, space);
8498 isl_multi_union_pw_aff_free(mupa);
8499 return umap;
8500 error:
8501 isl_multi_union_pw_aff_free(mupa);
8502 return NULL;
8505 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8506 * "range" is the space from which to set the range space.
8507 * "res" collects the results.
8509 struct isl_union_pw_multi_aff_reset_range_space_data {
8510 isl_space *range;
8511 isl_union_pw_multi_aff *res;
8514 /* Replace the range space of "pma" by the range space of data->range and
8515 * add the result to data->res.
8517 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8519 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8520 isl_space *space;
8522 space = isl_pw_multi_aff_get_space(pma);
8523 space = isl_space_domain(space);
8524 space = isl_space_extend_domain_with_range(space,
8525 isl_space_copy(data->range));
8526 pma = isl_pw_multi_aff_reset_space(pma, space);
8527 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8529 return data->res ? isl_stat_ok : isl_stat_error;
8532 /* Replace the range space of all the piecewise affine expressions in "upma" by
8533 * the range space of "space".
8535 * This assumes that all these expressions have the same output dimension.
8537 * Since the spaces of the expressions change, so do their hash values.
8538 * We therefore need to create a new isl_union_pw_multi_aff.
8539 * Note that the hash value is currently computed based on the entire
8540 * space even though there can only be a single expression with a given
8541 * domain space.
8543 static __isl_give isl_union_pw_multi_aff *
8544 isl_union_pw_multi_aff_reset_range_space(
8545 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8547 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8548 isl_space *space_upma;
8550 space_upma = isl_union_pw_multi_aff_get_space(upma);
8551 data.res = isl_union_pw_multi_aff_empty(space_upma);
8552 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8553 &reset_range_space, &data) < 0)
8554 data.res = isl_union_pw_multi_aff_free(data.res);
8556 isl_space_free(space);
8557 isl_union_pw_multi_aff_free(upma);
8558 return data.res;
8561 /* Construct and return a union piecewise multi affine expression
8562 * that is equal to the given multi union piecewise affine expression.
8564 * In order to be able to perform the conversion, the input
8565 * needs to have a least one output dimension.
8567 __isl_give isl_union_pw_multi_aff *
8568 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8569 __isl_take isl_multi_union_pw_aff *mupa)
8571 int i, n;
8572 isl_space *space;
8573 isl_union_pw_multi_aff *upma;
8574 isl_union_pw_aff *upa;
8576 if (!mupa)
8577 return NULL;
8579 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8580 if (n == 0)
8581 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8582 "cannot determine domain of zero-dimensional "
8583 "isl_multi_union_pw_aff", goto error);
8585 space = isl_multi_union_pw_aff_get_space(mupa);
8586 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8587 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8589 for (i = 1; i < n; ++i) {
8590 isl_union_pw_multi_aff *upma_i;
8592 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8593 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8594 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8597 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8599 isl_multi_union_pw_aff_free(mupa);
8600 return upma;
8601 error:
8602 isl_multi_union_pw_aff_free(mupa);
8603 return NULL;
8606 /* Intersect the range of "mupa" with "range".
8607 * That is, keep only those domain elements that have a function value
8608 * in "range".
8610 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8611 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8613 isl_union_pw_multi_aff *upma;
8614 isl_union_set *domain;
8615 isl_space *space;
8616 int n;
8617 int match;
8619 if (!mupa || !range)
8620 goto error;
8622 space = isl_set_get_space(range);
8623 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8624 space, isl_dim_set);
8625 isl_space_free(space);
8626 if (match < 0)
8627 goto error;
8628 if (!match)
8629 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8630 "space don't match", goto error);
8631 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8632 if (n == 0)
8633 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8634 "cannot intersect range of zero-dimensional "
8635 "isl_multi_union_pw_aff", goto error);
8637 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8638 isl_multi_union_pw_aff_copy(mupa));
8639 domain = isl_union_set_from_set(range);
8640 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8641 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8643 return mupa;
8644 error:
8645 isl_multi_union_pw_aff_free(mupa);
8646 isl_set_free(range);
8647 return NULL;
8650 /* Return the shared domain of the elements of "mupa".
8652 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8653 __isl_take isl_multi_union_pw_aff *mupa)
8655 int i, n;
8656 isl_union_pw_aff *upa;
8657 isl_union_set *dom;
8659 if (!mupa)
8660 return NULL;
8662 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8663 if (n == 0)
8664 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8665 "cannot determine domain", goto error);
8667 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8668 dom = isl_union_pw_aff_domain(upa);
8669 for (i = 1; i < n; ++i) {
8670 isl_union_set *dom_i;
8672 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8673 dom_i = isl_union_pw_aff_domain(upa);
8674 dom = isl_union_set_intersect(dom, dom_i);
8677 isl_multi_union_pw_aff_free(mupa);
8678 return dom;
8679 error:
8680 isl_multi_union_pw_aff_free(mupa);
8681 return NULL;
8684 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8685 * In particular, the spaces have been aligned.
8686 * The result is defined over the shared domain of the elements of "mupa"
8688 * We first extract the parametric constant part of "aff" and
8689 * define that over the shared domain.
8690 * Then we iterate over all input dimensions of "aff" and add the corresponding
8691 * multiples of the elements of "mupa".
8692 * Finally, we consider the integer divisions, calling the function
8693 * recursively to obtain an isl_union_pw_aff corresponding to the
8694 * integer division argument.
8696 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8697 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8699 int i, n_in, n_div;
8700 isl_union_pw_aff *upa;
8701 isl_union_set *uset;
8702 isl_val *v;
8703 isl_aff *cst;
8705 n_in = isl_aff_dim(aff, isl_dim_in);
8706 n_div = isl_aff_dim(aff, isl_dim_div);
8708 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8709 cst = isl_aff_copy(aff);
8710 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8711 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8712 cst = isl_aff_project_domain_on_params(cst);
8713 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8715 for (i = 0; i < n_in; ++i) {
8716 isl_union_pw_aff *upa_i;
8718 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8719 continue;
8720 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8721 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8722 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8723 upa = isl_union_pw_aff_add(upa, upa_i);
8726 for (i = 0; i < n_div; ++i) {
8727 isl_aff *div;
8728 isl_union_pw_aff *upa_i;
8730 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8731 continue;
8732 div = isl_aff_get_div(aff, i);
8733 upa_i = multi_union_pw_aff_apply_aff(
8734 isl_multi_union_pw_aff_copy(mupa), div);
8735 upa_i = isl_union_pw_aff_floor(upa_i);
8736 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8737 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8738 upa = isl_union_pw_aff_add(upa, upa_i);
8741 isl_multi_union_pw_aff_free(mupa);
8742 isl_aff_free(aff);
8744 return upa;
8747 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8748 * with the domain of "aff".
8749 * Furthermore, the dimension of this space needs to be greater than zero.
8750 * The result is defined over the shared domain of the elements of "mupa"
8752 * We perform these checks and then hand over control to
8753 * multi_union_pw_aff_apply_aff.
8755 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8756 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8758 isl_space *space1, *space2;
8759 int equal;
8761 mupa = isl_multi_union_pw_aff_align_params(mupa,
8762 isl_aff_get_space(aff));
8763 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8764 if (!mupa || !aff)
8765 goto error;
8767 space1 = isl_multi_union_pw_aff_get_space(mupa);
8768 space2 = isl_aff_get_domain_space(aff);
8769 equal = isl_space_is_equal(space1, space2);
8770 isl_space_free(space1);
8771 isl_space_free(space2);
8772 if (equal < 0)
8773 goto error;
8774 if (!equal)
8775 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8776 "spaces don't match", goto error);
8777 if (isl_aff_dim(aff, isl_dim_in) == 0)
8778 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8779 "cannot determine domains", goto error);
8781 return multi_union_pw_aff_apply_aff(mupa, aff);
8782 error:
8783 isl_multi_union_pw_aff_free(mupa);
8784 isl_aff_free(aff);
8785 return NULL;
8788 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8789 * with the domain of "ma".
8790 * Furthermore, the dimension of this space needs to be greater than zero,
8791 * unless the dimension of the target space of "ma" is also zero.
8792 * The result is defined over the shared domain of the elements of "mupa"
8794 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8795 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8797 isl_space *space1, *space2;
8798 isl_multi_union_pw_aff *res;
8799 int equal;
8800 int i, n_out;
8802 mupa = isl_multi_union_pw_aff_align_params(mupa,
8803 isl_multi_aff_get_space(ma));
8804 ma = isl_multi_aff_align_params(ma,
8805 isl_multi_union_pw_aff_get_space(mupa));
8806 if (!mupa || !ma)
8807 goto error;
8809 space1 = isl_multi_union_pw_aff_get_space(mupa);
8810 space2 = isl_multi_aff_get_domain_space(ma);
8811 equal = isl_space_is_equal(space1, space2);
8812 isl_space_free(space1);
8813 isl_space_free(space2);
8814 if (equal < 0)
8815 goto error;
8816 if (!equal)
8817 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8818 "spaces don't match", goto error);
8819 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8820 if (isl_multi_aff_dim(ma, isl_dim_in) == 0 && n_out != 0)
8821 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8822 "cannot determine domains", goto error);
8824 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8825 res = isl_multi_union_pw_aff_alloc(space1);
8827 for (i = 0; i < n_out; ++i) {
8828 isl_aff *aff;
8829 isl_union_pw_aff *upa;
8831 aff = isl_multi_aff_get_aff(ma, i);
8832 upa = multi_union_pw_aff_apply_aff(
8833 isl_multi_union_pw_aff_copy(mupa), aff);
8834 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8837 isl_multi_aff_free(ma);
8838 isl_multi_union_pw_aff_free(mupa);
8839 return res;
8840 error:
8841 isl_multi_union_pw_aff_free(mupa);
8842 isl_multi_aff_free(ma);
8843 return NULL;
8846 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
8847 * with the domain of "pa".
8848 * Furthermore, the dimension of this space needs to be greater than zero.
8849 * The result is defined over the shared domain of the elements of "mupa"
8851 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
8852 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8854 int i;
8855 int equal;
8856 isl_space *space, *space2;
8857 isl_union_pw_aff *upa;
8859 mupa = isl_multi_union_pw_aff_align_params(mupa,
8860 isl_pw_aff_get_space(pa));
8861 pa = isl_pw_aff_align_params(pa,
8862 isl_multi_union_pw_aff_get_space(mupa));
8863 if (!mupa || !pa)
8864 goto error;
8866 space = isl_multi_union_pw_aff_get_space(mupa);
8867 space2 = isl_pw_aff_get_domain_space(pa);
8868 equal = isl_space_is_equal(space, space2);
8869 isl_space_free(space);
8870 isl_space_free(space2);
8871 if (equal < 0)
8872 goto error;
8873 if (!equal)
8874 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8875 "spaces don't match", goto error);
8876 if (isl_pw_aff_dim(pa, isl_dim_in) == 0)
8877 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8878 "cannot determine domains", goto error);
8880 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
8881 upa = isl_union_pw_aff_empty(space);
8883 for (i = 0; i < pa->n; ++i) {
8884 isl_aff *aff;
8885 isl_set *domain;
8886 isl_multi_union_pw_aff *mupa_i;
8887 isl_union_pw_aff *upa_i;
8889 mupa_i = isl_multi_union_pw_aff_copy(mupa);
8890 domain = isl_set_copy(pa->p[i].set);
8891 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
8892 aff = isl_aff_copy(pa->p[i].aff);
8893 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
8894 upa = isl_union_pw_aff_union_add(upa, upa_i);
8897 isl_multi_union_pw_aff_free(mupa);
8898 isl_pw_aff_free(pa);
8899 return upa;
8900 error:
8901 isl_multi_union_pw_aff_free(mupa);
8902 isl_pw_aff_free(pa);
8903 return NULL;
8906 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
8907 * with the domain of "pma".
8908 * Furthermore, the dimension of this space needs to be greater than zero,
8909 * unless the dimension of the target space of "pma" is also zero.
8910 * The result is defined over the shared domain of the elements of "mupa"
8912 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
8913 __isl_take isl_multi_union_pw_aff *mupa,
8914 __isl_take isl_pw_multi_aff *pma)
8916 isl_space *space1, *space2;
8917 isl_multi_union_pw_aff *res;
8918 int equal;
8919 int i, n_out;
8921 mupa = isl_multi_union_pw_aff_align_params(mupa,
8922 isl_pw_multi_aff_get_space(pma));
8923 pma = isl_pw_multi_aff_align_params(pma,
8924 isl_multi_union_pw_aff_get_space(mupa));
8925 if (!mupa || !pma)
8926 goto error;
8928 space1 = isl_multi_union_pw_aff_get_space(mupa);
8929 space2 = isl_pw_multi_aff_get_domain_space(pma);
8930 equal = isl_space_is_equal(space1, space2);
8931 isl_space_free(space1);
8932 isl_space_free(space2);
8933 if (equal < 0)
8934 goto error;
8935 if (!equal)
8936 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8937 "spaces don't match", goto error);
8938 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
8939 if (isl_pw_multi_aff_dim(pma, isl_dim_in) == 0 && n_out != 0)
8940 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
8941 "cannot determine domains", goto error);
8943 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
8944 res = isl_multi_union_pw_aff_alloc(space1);
8946 for (i = 0; i < n_out; ++i) {
8947 isl_pw_aff *pa;
8948 isl_union_pw_aff *upa;
8950 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8951 upa = isl_multi_union_pw_aff_apply_pw_aff(
8952 isl_multi_union_pw_aff_copy(mupa), pa);
8953 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8956 isl_pw_multi_aff_free(pma);
8957 isl_multi_union_pw_aff_free(mupa);
8958 return res;
8959 error:
8960 isl_multi_union_pw_aff_free(mupa);
8961 isl_pw_multi_aff_free(pma);
8962 return NULL;
8965 /* Compute the pullback of "mupa" by the function represented by "upma".
8966 * In other words, plug in "upma" in "mupa". The result contains
8967 * expressions defined over the domain space of "upma".
8969 * Run over all elements of "mupa" and plug in "upma" in each of them.
8971 __isl_give isl_multi_union_pw_aff *
8972 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
8973 __isl_take isl_multi_union_pw_aff *mupa,
8974 __isl_take isl_union_pw_multi_aff *upma)
8976 int i, n;
8978 mupa = isl_multi_union_pw_aff_align_params(mupa,
8979 isl_union_pw_multi_aff_get_space(upma));
8980 upma = isl_union_pw_multi_aff_align_params(upma,
8981 isl_multi_union_pw_aff_get_space(mupa));
8982 if (!mupa || !upma)
8983 goto error;
8985 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8986 for (i = 0; i < n; ++i) {
8987 isl_union_pw_aff *upa;
8989 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8990 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
8991 isl_union_pw_multi_aff_copy(upma));
8992 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8995 isl_union_pw_multi_aff_free(upma);
8996 return mupa;
8997 error:
8998 isl_multi_union_pw_aff_free(mupa);
8999 isl_union_pw_multi_aff_free(upma);
9000 return NULL;
9003 /* Extract the sequence of elements in "mupa" with domain space "space"
9004 * (ignoring parameters).
9006 * For the elements of "mupa" that are not defined on the specified space,
9007 * the corresponding element in the result is empty.
9009 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9010 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9012 int i, n;
9013 isl_bool equal_params;
9014 isl_space *space_mpa = NULL;
9015 isl_multi_pw_aff *mpa;
9017 if (!mupa || !space)
9018 goto error;
9020 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9021 equal_params = isl_space_has_equal_params(space_mpa, space);
9022 if (equal_params < 0)
9023 goto error;
9024 if (!equal_params) {
9025 space = isl_space_drop_dims(space, isl_dim_param,
9026 0, isl_space_dim(space, isl_dim_param));
9027 space = isl_space_align_params(space,
9028 isl_space_copy(space_mpa));
9029 if (!space)
9030 goto error;
9032 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9033 space_mpa);
9034 mpa = isl_multi_pw_aff_alloc(space_mpa);
9036 space = isl_space_from_domain(space);
9037 space = isl_space_add_dims(space, isl_dim_out, 1);
9038 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9039 for (i = 0; i < n; ++i) {
9040 isl_union_pw_aff *upa;
9041 isl_pw_aff *pa;
9043 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9044 pa = isl_union_pw_aff_extract_pw_aff(upa,
9045 isl_space_copy(space));
9046 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9047 isl_union_pw_aff_free(upa);
9050 isl_space_free(space);
9051 return mpa;
9052 error:
9053 isl_space_free(space_mpa);
9054 isl_space_free(space);
9055 return NULL;