isl_poly_is_infty: use isl_bool_ok
[isl.git] / isl_aff.c
blobeb409125e63ff65c8d1b49af3e7f312c4befb642
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
10 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 * 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
14 * B.P. 105 - 78153 Le Chesnay, France
17 #include <isl_ctx_private.h>
18 #include <isl_map_private.h>
19 #include <isl_union_map_private.h>
20 #include <isl_aff_private.h>
21 #include <isl_space_private.h>
22 #include <isl_local_space_private.h>
23 #include <isl_vec_private.h>
24 #include <isl_mat_private.h>
25 #include <isl/id.h>
26 #include <isl/constraint.h>
27 #include <isl_seq.h>
28 #include <isl/set.h>
29 #include <isl_val_private.h>
30 #include <isl_point_private.h>
31 #include <isl_config.h>
33 #undef BASE
34 #define BASE aff
36 #include <isl_list_templ.c>
38 #undef BASE
39 #define BASE pw_aff
41 #include <isl_list_templ.c>
43 #undef BASE
44 #define BASE pw_multi_aff
46 #include <isl_list_templ.c>
48 #undef BASE
49 #define BASE union_pw_aff
51 #include <isl_list_templ.c>
53 #undef BASE
54 #define BASE union_pw_multi_aff
56 #include <isl_list_templ.c>
58 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
59 __isl_take isl_vec *v)
61 isl_aff *aff;
63 if (!ls || !v)
64 goto error;
66 aff = isl_calloc_type(v->ctx, struct isl_aff);
67 if (!aff)
68 goto error;
70 aff->ref = 1;
71 aff->ls = ls;
72 aff->v = v;
74 return aff;
75 error:
76 isl_local_space_free(ls);
77 isl_vec_free(v);
78 return NULL;
81 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
83 isl_ctx *ctx;
84 isl_vec *v;
85 isl_size total;
87 if (!ls)
88 return NULL;
90 ctx = isl_local_space_get_ctx(ls);
91 if (!isl_local_space_divs_known(ls))
92 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
93 goto error);
94 if (!isl_local_space_is_set(ls))
95 isl_die(ctx, isl_error_invalid,
96 "domain of affine expression should be a set",
97 goto error);
99 total = isl_local_space_dim(ls, isl_dim_all);
100 if (total < 0)
101 goto error;
102 v = isl_vec_alloc(ctx, 1 + 1 + total);
103 return isl_aff_alloc_vec(ls, v);
104 error:
105 isl_local_space_free(ls);
106 return NULL;
109 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
111 isl_aff *aff;
113 aff = isl_aff_alloc(ls);
114 if (!aff)
115 return NULL;
117 isl_int_set_si(aff->v->el[0], 1);
118 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
120 return aff;
123 /* Return a piecewise affine expression defined on the specified domain
124 * that is equal to zero.
126 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
128 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
131 /* Return an affine expression defined on the specified domain
132 * that represents NaN.
134 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
136 isl_aff *aff;
138 aff = isl_aff_alloc(ls);
139 if (!aff)
140 return NULL;
142 isl_seq_clr(aff->v->el, aff->v->size);
144 return aff;
147 /* Return a piecewise affine expression defined on the specified domain
148 * that represents NaN.
150 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
152 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
155 /* Return an affine expression that is equal to "val" on
156 * domain local space "ls".
158 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
159 __isl_take isl_val *val)
161 isl_aff *aff;
163 if (!ls || !val)
164 goto error;
165 if (!isl_val_is_rat(val))
166 isl_die(isl_val_get_ctx(val), isl_error_invalid,
167 "expecting rational value", goto error);
169 aff = isl_aff_alloc(isl_local_space_copy(ls));
170 if (!aff)
171 goto error;
173 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
174 isl_int_set(aff->v->el[1], val->n);
175 isl_int_set(aff->v->el[0], val->d);
177 isl_local_space_free(ls);
178 isl_val_free(val);
179 return aff;
180 error:
181 isl_local_space_free(ls);
182 isl_val_free(val);
183 return NULL;
186 /* Return an affine expression that is equal to the specified dimension
187 * in "ls".
189 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
190 enum isl_dim_type type, unsigned pos)
192 isl_space *space;
193 isl_aff *aff;
195 if (!ls)
196 return NULL;
198 space = isl_local_space_get_space(ls);
199 if (!space)
200 goto error;
201 if (isl_space_is_map(space))
202 isl_die(isl_space_get_ctx(space), isl_error_invalid,
203 "expecting (parameter) set space", goto error);
204 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
205 goto error;
207 isl_space_free(space);
208 aff = isl_aff_alloc(ls);
209 if (!aff)
210 return NULL;
212 pos += isl_local_space_offset(aff->ls, type);
214 isl_int_set_si(aff->v->el[0], 1);
215 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
216 isl_int_set_si(aff->v->el[1 + pos], 1);
218 return aff;
219 error:
220 isl_local_space_free(ls);
221 isl_space_free(space);
222 return NULL;
225 /* Return a piecewise affine expression that is equal to
226 * the specified dimension in "ls".
228 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
229 enum isl_dim_type type, unsigned pos)
231 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
234 /* Return an affine expression that is equal to the parameter
235 * in the domain space "space" with identifier "id".
237 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
238 __isl_take isl_space *space, __isl_take isl_id *id)
240 int pos;
241 isl_local_space *ls;
243 if (!space || !id)
244 goto error;
245 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
246 if (pos < 0)
247 isl_die(isl_space_get_ctx(space), isl_error_invalid,
248 "parameter not found in space", goto error);
249 isl_id_free(id);
250 ls = isl_local_space_from_space(space);
251 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
252 error:
253 isl_space_free(space);
254 isl_id_free(id);
255 return NULL;
258 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
260 if (!aff)
261 return NULL;
263 aff->ref++;
264 return aff;
267 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
269 if (!aff)
270 return NULL;
272 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
273 isl_vec_copy(aff->v));
276 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
278 if (!aff)
279 return NULL;
281 if (aff->ref == 1)
282 return aff;
283 aff->ref--;
284 return isl_aff_dup(aff);
287 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
289 if (!aff)
290 return NULL;
292 if (--aff->ref > 0)
293 return NULL;
295 isl_local_space_free(aff->ls);
296 isl_vec_free(aff->v);
298 free(aff);
300 return NULL;
303 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
305 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
308 /* Return a hash value that digests "aff".
310 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
312 uint32_t hash, ls_hash, v_hash;
314 if (!aff)
315 return 0;
317 hash = isl_hash_init();
318 ls_hash = isl_local_space_get_hash(aff->ls);
319 isl_hash_hash(hash, ls_hash);
320 v_hash = isl_vec_get_hash(aff->v);
321 isl_hash_hash(hash, v_hash);
323 return hash;
326 /* Externally, an isl_aff has a map space, but internally, the
327 * ls field corresponds to the domain of that space.
329 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
331 if (!aff)
332 return isl_size_error;
333 if (type == isl_dim_out)
334 return 1;
335 if (type == isl_dim_in)
336 type = isl_dim_set;
337 return isl_local_space_dim(aff->ls, type);
340 /* Return the position of the dimension of the given type and name
341 * in "aff".
342 * Return -1 if no such dimension can be found.
344 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
345 const char *name)
347 if (!aff)
348 return -1;
349 if (type == isl_dim_out)
350 return -1;
351 if (type == isl_dim_in)
352 type = isl_dim_set;
353 return isl_local_space_find_dim_by_name(aff->ls, type, name);
356 /* Return the domain space of "aff".
358 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
360 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
363 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
365 return isl_space_copy(isl_aff_peek_domain_space(aff));
368 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
370 isl_space *space;
371 if (!aff)
372 return NULL;
373 space = isl_local_space_get_space(aff->ls);
374 space = isl_space_from_domain(space);
375 space = isl_space_add_dims(space, isl_dim_out, 1);
376 return space;
379 __isl_give isl_local_space *isl_aff_get_domain_local_space(
380 __isl_keep isl_aff *aff)
382 return aff ? isl_local_space_copy(aff->ls) : NULL;
385 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
387 isl_local_space *ls;
388 if (!aff)
389 return NULL;
390 ls = isl_local_space_copy(aff->ls);
391 ls = isl_local_space_from_domain(ls);
392 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
393 return ls;
396 /* Return the local space of the domain of "aff".
397 * This may be either a copy or the local space itself
398 * if there is only one reference to "aff".
399 * This allows the local space to be modified inplace
400 * if both the expression and its local space have only a single reference.
401 * The caller is not allowed to modify "aff" between this call and
402 * a subsequent call to isl_aff_restore_domain_local_space.
403 * The only exception is that isl_aff_free can be called instead.
405 __isl_give isl_local_space *isl_aff_take_domain_local_space(
406 __isl_keep isl_aff *aff)
408 isl_local_space *ls;
410 if (!aff)
411 return NULL;
412 if (aff->ref != 1)
413 return isl_aff_get_domain_local_space(aff);
414 ls = aff->ls;
415 aff->ls = NULL;
416 return ls;
419 /* Set the local space of the domain of "aff" to "ls",
420 * where the local space of "aff" may be missing
421 * due to a preceding call to isl_aff_take_domain_local_space.
422 * However, in this case, "aff" only has a single reference and
423 * then the call to isl_aff_cow has no effect.
425 __isl_give isl_aff *isl_aff_restore_domain_local_space(
426 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
428 if (!aff || !ls)
429 goto error;
431 if (aff->ls == ls) {
432 isl_local_space_free(ls);
433 return aff;
436 aff = isl_aff_cow(aff);
437 if (!aff)
438 goto error;
439 isl_local_space_free(aff->ls);
440 aff->ls = ls;
442 return aff;
443 error:
444 isl_aff_free(aff);
445 isl_local_space_free(ls);
446 return NULL;
449 /* Externally, an isl_aff has a map space, but internally, the
450 * ls field corresponds to the domain of that space.
452 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
453 enum isl_dim_type type, unsigned pos)
455 if (!aff)
456 return NULL;
457 if (type == isl_dim_out)
458 return NULL;
459 if (type == isl_dim_in)
460 type = isl_dim_set;
461 return isl_local_space_get_dim_name(aff->ls, type, pos);
464 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
465 __isl_take isl_space *dim)
467 aff = isl_aff_cow(aff);
468 if (!aff || !dim)
469 goto error;
471 aff->ls = isl_local_space_reset_space(aff->ls, dim);
472 if (!aff->ls)
473 return isl_aff_free(aff);
475 return aff;
476 error:
477 isl_aff_free(aff);
478 isl_space_free(dim);
479 return NULL;
482 /* Reset the space of "aff". This function is called from isl_pw_templ.c
483 * and doesn't know if the space of an element object is represented
484 * directly or through its domain. It therefore passes along both.
486 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
487 __isl_take isl_space *space, __isl_take isl_space *domain)
489 isl_space_free(space);
490 return isl_aff_reset_domain_space(aff, domain);
493 /* Reorder the coefficients of the affine expression based
494 * on the given reordering.
495 * The reordering r is assumed to have been extended with the local
496 * variables.
498 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
499 __isl_take isl_reordering *r, int n_div)
501 isl_space *space;
502 isl_vec *res;
503 isl_size dim;
504 int i;
506 if (!vec || !r)
507 goto error;
509 space = isl_reordering_peek_space(r);
510 dim = isl_space_dim(space, isl_dim_all);
511 if (dim < 0)
512 goto error;
513 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
514 if (!res)
515 goto error;
516 isl_seq_cpy(res->el, vec->el, 2);
517 isl_seq_clr(res->el + 2, res->size - 2);
518 for (i = 0; i < r->len; ++i)
519 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
521 isl_reordering_free(r);
522 isl_vec_free(vec);
523 return res;
524 error:
525 isl_vec_free(vec);
526 isl_reordering_free(r);
527 return NULL;
530 /* Reorder the dimensions of the domain of "aff" according
531 * to the given reordering.
533 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
534 __isl_take isl_reordering *r)
536 aff = isl_aff_cow(aff);
537 if (!aff)
538 goto error;
540 r = isl_reordering_extend(r, aff->ls->div->n_row);
541 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
542 aff->ls->div->n_row);
543 aff->ls = isl_local_space_realign(aff->ls, r);
545 if (!aff->v || !aff->ls)
546 return isl_aff_free(aff);
548 return aff;
549 error:
550 isl_aff_free(aff);
551 isl_reordering_free(r);
552 return NULL;
555 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
556 __isl_take isl_space *model)
558 isl_bool equal_params;
560 if (!aff || !model)
561 goto error;
563 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
564 if (equal_params < 0)
565 goto error;
566 if (!equal_params) {
567 isl_reordering *exp;
569 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
570 exp = isl_reordering_extend_space(exp,
571 isl_aff_get_domain_space(aff));
572 aff = isl_aff_realign_domain(aff, exp);
575 isl_space_free(model);
576 return aff;
577 error:
578 isl_space_free(model);
579 isl_aff_free(aff);
580 return NULL;
583 /* Is "aff" obviously equal to zero?
585 * If the denominator is zero, then "aff" is not equal to zero.
587 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
589 if (!aff)
590 return isl_bool_error;
592 if (isl_int_is_zero(aff->v->el[0]))
593 return isl_bool_false;
594 return isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1) < 0;
597 /* Does "aff" represent NaN?
599 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
601 if (!aff)
602 return isl_bool_error;
604 return isl_seq_first_non_zero(aff->v->el, 2) < 0;
607 /* Are "aff1" and "aff2" obviously equal?
609 * NaN is not equal to anything, not even to another NaN.
611 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
612 __isl_keep isl_aff *aff2)
614 isl_bool equal;
616 if (!aff1 || !aff2)
617 return isl_bool_error;
619 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
620 return isl_bool_false;
622 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
623 if (equal < 0 || !equal)
624 return equal;
626 return isl_vec_is_equal(aff1->v, aff2->v);
629 /* Return the common denominator of "aff" in "v".
631 * We cannot return anything meaningful in case of a NaN.
633 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
635 if (!aff)
636 return isl_stat_error;
637 if (isl_aff_is_nan(aff))
638 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
639 "cannot get denominator of NaN", return isl_stat_error);
640 isl_int_set(*v, aff->v->el[0]);
641 return isl_stat_ok;
644 /* Return the common denominator of "aff".
646 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
648 isl_ctx *ctx;
650 if (!aff)
651 return NULL;
653 ctx = isl_aff_get_ctx(aff);
654 if (isl_aff_is_nan(aff))
655 return isl_val_nan(ctx);
656 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
659 /* Return the constant term of "aff".
661 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
663 isl_ctx *ctx;
664 isl_val *v;
666 if (!aff)
667 return NULL;
669 ctx = isl_aff_get_ctx(aff);
670 if (isl_aff_is_nan(aff))
671 return isl_val_nan(ctx);
672 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
673 return isl_val_normalize(v);
676 /* Return the coefficient of the variable of type "type" at position "pos"
677 * of "aff".
679 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
680 enum isl_dim_type type, int pos)
682 isl_ctx *ctx;
683 isl_val *v;
685 if (!aff)
686 return NULL;
688 ctx = isl_aff_get_ctx(aff);
689 if (type == isl_dim_out)
690 isl_die(ctx, isl_error_invalid,
691 "output/set dimension does not have a coefficient",
692 return NULL);
693 if (type == isl_dim_in)
694 type = isl_dim_set;
696 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
697 return NULL;
699 if (isl_aff_is_nan(aff))
700 return isl_val_nan(ctx);
701 pos += isl_local_space_offset(aff->ls, type);
702 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
703 return isl_val_normalize(v);
706 /* Return the sign of the coefficient of the variable of type "type"
707 * at position "pos" of "aff".
709 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
710 int pos)
712 isl_ctx *ctx;
714 if (!aff)
715 return 0;
717 ctx = isl_aff_get_ctx(aff);
718 if (type == isl_dim_out)
719 isl_die(ctx, isl_error_invalid,
720 "output/set dimension does not have a coefficient",
721 return 0);
722 if (type == isl_dim_in)
723 type = isl_dim_set;
725 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
726 return 0;
728 pos += isl_local_space_offset(aff->ls, type);
729 return isl_int_sgn(aff->v->el[1 + pos]);
732 /* Replace the numerator of the constant term of "aff" by "v".
734 * A NaN is unaffected by this operation.
736 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
738 if (!aff)
739 return NULL;
740 if (isl_aff_is_nan(aff))
741 return aff;
742 aff = isl_aff_cow(aff);
743 if (!aff)
744 return NULL;
746 aff->v = isl_vec_cow(aff->v);
747 if (!aff->v)
748 return isl_aff_free(aff);
750 isl_int_set(aff->v->el[1], v);
752 return aff;
755 /* Replace the constant term of "aff" by "v".
757 * A NaN is unaffected by this operation.
759 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
760 __isl_take isl_val *v)
762 if (!aff || !v)
763 goto error;
765 if (isl_aff_is_nan(aff)) {
766 isl_val_free(v);
767 return aff;
770 if (!isl_val_is_rat(v))
771 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
772 "expecting rational value", goto error);
774 if (isl_int_eq(aff->v->el[1], v->n) &&
775 isl_int_eq(aff->v->el[0], v->d)) {
776 isl_val_free(v);
777 return aff;
780 aff = isl_aff_cow(aff);
781 if (!aff)
782 goto error;
783 aff->v = isl_vec_cow(aff->v);
784 if (!aff->v)
785 goto error;
787 if (isl_int_eq(aff->v->el[0], v->d)) {
788 isl_int_set(aff->v->el[1], v->n);
789 } else if (isl_int_is_one(v->d)) {
790 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
791 } else {
792 isl_seq_scale(aff->v->el + 1,
793 aff->v->el + 1, v->d, aff->v->size - 1);
794 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
795 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
796 aff->v = isl_vec_normalize(aff->v);
797 if (!aff->v)
798 goto error;
801 isl_val_free(v);
802 return aff;
803 error:
804 isl_aff_free(aff);
805 isl_val_free(v);
806 return NULL;
809 /* Add "v" to the constant term of "aff".
811 * A NaN is unaffected by this operation.
813 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
815 if (isl_int_is_zero(v))
816 return aff;
818 if (!aff)
819 return NULL;
820 if (isl_aff_is_nan(aff))
821 return aff;
822 aff = isl_aff_cow(aff);
823 if (!aff)
824 return NULL;
826 aff->v = isl_vec_cow(aff->v);
827 if (!aff->v)
828 return isl_aff_free(aff);
830 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
832 return aff;
835 /* Add "v" to the constant term of "aff".
837 * A NaN is unaffected by this operation.
839 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
840 __isl_take isl_val *v)
842 if (!aff || !v)
843 goto error;
845 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
846 isl_val_free(v);
847 return aff;
850 if (!isl_val_is_rat(v))
851 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
852 "expecting rational value", goto error);
854 aff = isl_aff_cow(aff);
855 if (!aff)
856 goto error;
858 aff->v = isl_vec_cow(aff->v);
859 if (!aff->v)
860 goto error;
862 if (isl_int_is_one(v->d)) {
863 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
864 } else if (isl_int_eq(aff->v->el[0], v->d)) {
865 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
866 aff->v = isl_vec_normalize(aff->v);
867 if (!aff->v)
868 goto error;
869 } else {
870 isl_seq_scale(aff->v->el + 1,
871 aff->v->el + 1, v->d, aff->v->size - 1);
872 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
873 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
874 aff->v = isl_vec_normalize(aff->v);
875 if (!aff->v)
876 goto error;
879 isl_val_free(v);
880 return aff;
881 error:
882 isl_aff_free(aff);
883 isl_val_free(v);
884 return NULL;
887 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
889 isl_int t;
891 isl_int_init(t);
892 isl_int_set_si(t, v);
893 aff = isl_aff_add_constant(aff, t);
894 isl_int_clear(t);
896 return aff;
899 /* Add "v" to the numerator of the constant term of "aff".
901 * A NaN is unaffected by this operation.
903 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
905 if (isl_int_is_zero(v))
906 return aff;
908 if (!aff)
909 return NULL;
910 if (isl_aff_is_nan(aff))
911 return aff;
912 aff = isl_aff_cow(aff);
913 if (!aff)
914 return NULL;
916 aff->v = isl_vec_cow(aff->v);
917 if (!aff->v)
918 return isl_aff_free(aff);
920 isl_int_add(aff->v->el[1], aff->v->el[1], v);
922 return aff;
925 /* Add "v" to the numerator of the constant term of "aff".
927 * A NaN is unaffected by this operation.
929 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
931 isl_int t;
933 if (v == 0)
934 return aff;
936 isl_int_init(t);
937 isl_int_set_si(t, v);
938 aff = isl_aff_add_constant_num(aff, t);
939 isl_int_clear(t);
941 return aff;
944 /* Replace the numerator of the constant term of "aff" by "v".
946 * A NaN is unaffected by this operation.
948 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
950 if (!aff)
951 return NULL;
952 if (isl_aff_is_nan(aff))
953 return aff;
954 aff = isl_aff_cow(aff);
955 if (!aff)
956 return NULL;
958 aff->v = isl_vec_cow(aff->v);
959 if (!aff->v)
960 return isl_aff_free(aff);
962 isl_int_set_si(aff->v->el[1], v);
964 return aff;
967 /* Replace the numerator of the coefficient of the variable of type "type"
968 * at position "pos" of "aff" by "v".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
973 enum isl_dim_type type, int pos, isl_int v)
975 if (!aff)
976 return NULL;
978 if (type == isl_dim_out)
979 isl_die(aff->v->ctx, isl_error_invalid,
980 "output/set dimension does not have a coefficient",
981 return isl_aff_free(aff));
982 if (type == isl_dim_in)
983 type = isl_dim_set;
985 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
986 return isl_aff_free(aff);
988 if (isl_aff_is_nan(aff))
989 return aff;
990 aff = isl_aff_cow(aff);
991 if (!aff)
992 return NULL;
994 aff->v = isl_vec_cow(aff->v);
995 if (!aff->v)
996 return isl_aff_free(aff);
998 pos += isl_local_space_offset(aff->ls, type);
999 isl_int_set(aff->v->el[1 + pos], v);
1001 return aff;
1004 /* Replace the numerator of the coefficient of the variable of type "type"
1005 * at position "pos" of "aff" by "v".
1007 * A NaN is unaffected by this operation.
1009 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1010 enum isl_dim_type type, int pos, int v)
1012 if (!aff)
1013 return NULL;
1015 if (type == isl_dim_out)
1016 isl_die(aff->v->ctx, isl_error_invalid,
1017 "output/set dimension does not have a coefficient",
1018 return isl_aff_free(aff));
1019 if (type == isl_dim_in)
1020 type = isl_dim_set;
1022 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1023 return isl_aff_free(aff);
1025 if (isl_aff_is_nan(aff))
1026 return aff;
1027 pos += isl_local_space_offset(aff->ls, type);
1028 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1029 return aff;
1031 aff = isl_aff_cow(aff);
1032 if (!aff)
1033 return NULL;
1035 aff->v = isl_vec_cow(aff->v);
1036 if (!aff->v)
1037 return isl_aff_free(aff);
1039 isl_int_set_si(aff->v->el[1 + pos], v);
1041 return aff;
1044 /* Replace the coefficient of the variable of type "type" at position "pos"
1045 * of "aff" by "v".
1047 * A NaN is unaffected by this operation.
1049 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1050 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1052 if (!aff || !v)
1053 goto error;
1055 if (type == isl_dim_out)
1056 isl_die(aff->v->ctx, isl_error_invalid,
1057 "output/set dimension does not have a coefficient",
1058 goto error);
1059 if (type == isl_dim_in)
1060 type = isl_dim_set;
1062 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1063 return isl_aff_free(aff);
1065 if (isl_aff_is_nan(aff)) {
1066 isl_val_free(v);
1067 return aff;
1069 if (!isl_val_is_rat(v))
1070 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1071 "expecting rational value", goto error);
1073 pos += isl_local_space_offset(aff->ls, type);
1074 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1075 isl_int_eq(aff->v->el[0], v->d)) {
1076 isl_val_free(v);
1077 return aff;
1080 aff = isl_aff_cow(aff);
1081 if (!aff)
1082 goto error;
1083 aff->v = isl_vec_cow(aff->v);
1084 if (!aff->v)
1085 goto error;
1087 if (isl_int_eq(aff->v->el[0], v->d)) {
1088 isl_int_set(aff->v->el[1 + pos], v->n);
1089 } else if (isl_int_is_one(v->d)) {
1090 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1091 } else {
1092 isl_seq_scale(aff->v->el + 1,
1093 aff->v->el + 1, v->d, aff->v->size - 1);
1094 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1095 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1096 aff->v = isl_vec_normalize(aff->v);
1097 if (!aff->v)
1098 goto error;
1101 isl_val_free(v);
1102 return aff;
1103 error:
1104 isl_aff_free(aff);
1105 isl_val_free(v);
1106 return NULL;
1109 /* Add "v" to the coefficient of the variable of type "type"
1110 * at position "pos" of "aff".
1112 * A NaN is unaffected by this operation.
1114 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1115 enum isl_dim_type type, int pos, isl_int v)
1117 if (!aff)
1118 return NULL;
1120 if (type == isl_dim_out)
1121 isl_die(aff->v->ctx, isl_error_invalid,
1122 "output/set dimension does not have a coefficient",
1123 return isl_aff_free(aff));
1124 if (type == isl_dim_in)
1125 type = isl_dim_set;
1127 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1128 return isl_aff_free(aff);
1130 if (isl_aff_is_nan(aff))
1131 return aff;
1132 aff = isl_aff_cow(aff);
1133 if (!aff)
1134 return NULL;
1136 aff->v = isl_vec_cow(aff->v);
1137 if (!aff->v)
1138 return isl_aff_free(aff);
1140 pos += isl_local_space_offset(aff->ls, type);
1141 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1143 return aff;
1146 /* Add "v" to the coefficient of the variable of type "type"
1147 * at position "pos" of "aff".
1149 * A NaN is unaffected by this operation.
1151 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1152 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1154 if (!aff || !v)
1155 goto error;
1157 if (isl_val_is_zero(v)) {
1158 isl_val_free(v);
1159 return aff;
1162 if (type == isl_dim_out)
1163 isl_die(aff->v->ctx, isl_error_invalid,
1164 "output/set dimension does not have a coefficient",
1165 goto error);
1166 if (type == isl_dim_in)
1167 type = isl_dim_set;
1169 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1170 goto error;
1172 if (isl_aff_is_nan(aff)) {
1173 isl_val_free(v);
1174 return aff;
1176 if (!isl_val_is_rat(v))
1177 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1178 "expecting rational value", goto error);
1180 aff = isl_aff_cow(aff);
1181 if (!aff)
1182 goto error;
1184 aff->v = isl_vec_cow(aff->v);
1185 if (!aff->v)
1186 goto error;
1188 pos += isl_local_space_offset(aff->ls, type);
1189 if (isl_int_is_one(v->d)) {
1190 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1191 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1192 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1193 aff->v = isl_vec_normalize(aff->v);
1194 if (!aff->v)
1195 goto error;
1196 } else {
1197 isl_seq_scale(aff->v->el + 1,
1198 aff->v->el + 1, v->d, aff->v->size - 1);
1199 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1200 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1201 aff->v = isl_vec_normalize(aff->v);
1202 if (!aff->v)
1203 goto error;
1206 isl_val_free(v);
1207 return aff;
1208 error:
1209 isl_aff_free(aff);
1210 isl_val_free(v);
1211 return NULL;
1214 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1215 enum isl_dim_type type, int pos, int v)
1217 isl_int t;
1219 isl_int_init(t);
1220 isl_int_set_si(t, v);
1221 aff = isl_aff_add_coefficient(aff, type, pos, t);
1222 isl_int_clear(t);
1224 return aff;
1227 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1229 if (!aff)
1230 return NULL;
1232 return isl_local_space_get_div(aff->ls, pos);
1235 /* Return the negation of "aff".
1237 * As a special case, -NaN = NaN.
1239 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1241 if (!aff)
1242 return NULL;
1243 if (isl_aff_is_nan(aff))
1244 return aff;
1245 aff = isl_aff_cow(aff);
1246 if (!aff)
1247 return NULL;
1248 aff->v = isl_vec_cow(aff->v);
1249 if (!aff->v)
1250 return isl_aff_free(aff);
1252 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1254 return aff;
1257 /* Remove divs from the local space that do not appear in the affine
1258 * expression.
1259 * We currently only remove divs at the end.
1260 * Some intermediate divs may also not appear directly in the affine
1261 * expression, but we would also need to check that no other divs are
1262 * defined in terms of them.
1264 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1266 int pos;
1267 int off;
1268 isl_size n;
1270 if (!aff)
1271 return NULL;
1273 n = isl_local_space_dim(aff->ls, isl_dim_div);
1274 if (n < 0)
1275 return isl_aff_free(aff);
1276 off = isl_local_space_offset(aff->ls, isl_dim_div);
1278 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1279 if (pos == n)
1280 return aff;
1282 aff = isl_aff_cow(aff);
1283 if (!aff)
1284 return NULL;
1286 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1287 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1288 if (!aff->ls || !aff->v)
1289 return isl_aff_free(aff);
1291 return aff;
1294 /* Look for any divs in the aff->ls with a denominator equal to one
1295 * and plug them into the affine expression and any subsequent divs
1296 * that may reference the div.
1298 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1300 int i;
1301 isl_size n;
1302 int len;
1303 isl_int v;
1304 isl_vec *vec;
1305 isl_local_space *ls;
1306 unsigned pos;
1308 if (!aff)
1309 return NULL;
1311 n = isl_local_space_dim(aff->ls, isl_dim_div);
1312 if (n < 0)
1313 return isl_aff_free(aff);
1314 len = aff->v->size;
1315 for (i = 0; i < n; ++i) {
1316 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1317 continue;
1318 ls = isl_local_space_copy(aff->ls);
1319 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1320 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1321 vec = isl_vec_copy(aff->v);
1322 vec = isl_vec_cow(vec);
1323 if (!ls || !vec)
1324 goto error;
1326 isl_int_init(v);
1328 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1329 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1330 len, len, v);
1332 isl_int_clear(v);
1334 isl_vec_free(aff->v);
1335 aff->v = vec;
1336 isl_local_space_free(aff->ls);
1337 aff->ls = ls;
1340 return aff;
1341 error:
1342 isl_vec_free(vec);
1343 isl_local_space_free(ls);
1344 return isl_aff_free(aff);
1347 /* Look for any divs j that appear with a unit coefficient inside
1348 * the definitions of other divs i and plug them into the definitions
1349 * of the divs i.
1351 * In particular, an expression of the form
1353 * floor((f(..) + floor(g(..)/n))/m)
1355 * is simplified to
1357 * floor((n * f(..) + g(..))/(n * m))
1359 * This simplification is correct because we can move the expression
1360 * f(..) into the inner floor in the original expression to obtain
1362 * floor(floor((n * f(..) + g(..))/n)/m)
1364 * from which we can derive the simplified expression.
1366 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1368 int i, j;
1369 isl_size n;
1370 int off;
1372 if (!aff)
1373 return NULL;
1375 n = isl_local_space_dim(aff->ls, isl_dim_div);
1376 if (n < 0)
1377 return isl_aff_free(aff);
1378 off = isl_local_space_offset(aff->ls, isl_dim_div);
1379 for (i = 1; i < n; ++i) {
1380 for (j = 0; j < i; ++j) {
1381 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1382 continue;
1383 aff->ls = isl_local_space_substitute_seq(aff->ls,
1384 isl_dim_div, j, aff->ls->div->row[j],
1385 aff->v->size, i, 1);
1386 if (!aff->ls)
1387 return isl_aff_free(aff);
1391 return aff;
1394 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1396 * Even though this function is only called on isl_affs with a single
1397 * reference, we are careful to only change aff->v and aff->ls together.
1399 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1401 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1402 isl_local_space *ls;
1403 isl_vec *v;
1405 ls = isl_local_space_copy(aff->ls);
1406 ls = isl_local_space_swap_div(ls, a, b);
1407 v = isl_vec_copy(aff->v);
1408 v = isl_vec_cow(v);
1409 if (!ls || !v)
1410 goto error;
1412 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1413 isl_vec_free(aff->v);
1414 aff->v = v;
1415 isl_local_space_free(aff->ls);
1416 aff->ls = ls;
1418 return aff;
1419 error:
1420 isl_vec_free(v);
1421 isl_local_space_free(ls);
1422 return isl_aff_free(aff);
1425 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1427 * We currently do not actually remove div "b", but simply add its
1428 * coefficient to that of "a" and then zero it out.
1430 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1432 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1434 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1435 return aff;
1437 aff->v = isl_vec_cow(aff->v);
1438 if (!aff->v)
1439 return isl_aff_free(aff);
1441 isl_int_add(aff->v->el[1 + off + a],
1442 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1443 isl_int_set_si(aff->v->el[1 + off + b], 0);
1445 return aff;
1448 /* Sort the divs in the local space of "aff" according to
1449 * the comparison function "cmp_row" in isl_local_space.c,
1450 * combining the coefficients of identical divs.
1452 * Reordering divs does not change the semantics of "aff",
1453 * so there is no need to call isl_aff_cow.
1454 * Moreover, this function is currently only called on isl_affs
1455 * with a single reference.
1457 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1459 isl_size n;
1460 int i, j;
1462 n = isl_aff_dim(aff, isl_dim_div);
1463 if (n < 0)
1464 return isl_aff_free(aff);
1465 for (i = 1; i < n; ++i) {
1466 for (j = i - 1; j >= 0; --j) {
1467 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1468 if (cmp < 0)
1469 break;
1470 if (cmp == 0)
1471 aff = merge_divs(aff, j, j + 1);
1472 else
1473 aff = swap_div(aff, j, j + 1);
1474 if (!aff)
1475 return NULL;
1479 return aff;
1482 /* Normalize the representation of "aff".
1484 * This function should only be called of "new" isl_affs, i.e.,
1485 * with only a single reference. We therefore do not need to
1486 * worry about affecting other instances.
1488 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1490 if (!aff)
1491 return NULL;
1492 aff->v = isl_vec_normalize(aff->v);
1493 if (!aff->v)
1494 return isl_aff_free(aff);
1495 aff = plug_in_integral_divs(aff);
1496 aff = plug_in_unit_divs(aff);
1497 aff = sort_divs(aff);
1498 aff = isl_aff_remove_unused_divs(aff);
1499 return aff;
1502 /* Given f, return floor(f).
1503 * If f is an integer expression, then just return f.
1504 * If f is a constant, then return the constant floor(f).
1505 * Otherwise, if f = g/m, write g = q m + r,
1506 * create a new div d = [r/m] and return the expression q + d.
1507 * The coefficients in r are taken to lie between -m/2 and m/2.
1509 * reduce_div_coefficients performs the same normalization.
1511 * As a special case, floor(NaN) = NaN.
1513 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1515 int i;
1516 int size;
1517 isl_ctx *ctx;
1518 isl_vec *div;
1520 if (!aff)
1521 return NULL;
1523 if (isl_aff_is_nan(aff))
1524 return aff;
1525 if (isl_int_is_one(aff->v->el[0]))
1526 return aff;
1528 aff = isl_aff_cow(aff);
1529 if (!aff)
1530 return NULL;
1532 aff->v = isl_vec_cow(aff->v);
1533 if (!aff->v)
1534 return isl_aff_free(aff);
1536 if (isl_aff_is_cst(aff)) {
1537 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1538 isl_int_set_si(aff->v->el[0], 1);
1539 return aff;
1542 div = isl_vec_copy(aff->v);
1543 div = isl_vec_cow(div);
1544 if (!div)
1545 return isl_aff_free(aff);
1547 ctx = isl_aff_get_ctx(aff);
1548 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1549 for (i = 1; i < aff->v->size; ++i) {
1550 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1551 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1552 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1553 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1554 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1558 aff->ls = isl_local_space_add_div(aff->ls, div);
1559 if (!aff->ls)
1560 return isl_aff_free(aff);
1562 size = aff->v->size;
1563 aff->v = isl_vec_extend(aff->v, size + 1);
1564 if (!aff->v)
1565 return isl_aff_free(aff);
1566 isl_int_set_si(aff->v->el[0], 1);
1567 isl_int_set_si(aff->v->el[size], 1);
1569 aff = isl_aff_normalize(aff);
1571 return aff;
1574 /* Compute
1576 * aff mod m = aff - m * floor(aff/m)
1578 * with m an integer value.
1580 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1581 __isl_take isl_val *m)
1583 isl_aff *res;
1585 if (!aff || !m)
1586 goto error;
1588 if (!isl_val_is_int(m))
1589 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1590 "expecting integer modulo", goto error);
1592 res = isl_aff_copy(aff);
1593 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1594 aff = isl_aff_floor(aff);
1595 aff = isl_aff_scale_val(aff, m);
1596 res = isl_aff_sub(res, aff);
1598 return res;
1599 error:
1600 isl_aff_free(aff);
1601 isl_val_free(m);
1602 return NULL;
1605 /* Compute
1607 * pwaff mod m = pwaff - m * floor(pwaff/m)
1609 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1611 isl_pw_aff *res;
1613 res = isl_pw_aff_copy(pwaff);
1614 pwaff = isl_pw_aff_scale_down(pwaff, m);
1615 pwaff = isl_pw_aff_floor(pwaff);
1616 pwaff = isl_pw_aff_scale(pwaff, m);
1617 res = isl_pw_aff_sub(res, pwaff);
1619 return res;
1622 /* Compute
1624 * pa mod m = pa - m * floor(pa/m)
1626 * with m an integer value.
1628 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1629 __isl_take isl_val *m)
1631 if (!pa || !m)
1632 goto error;
1633 if (!isl_val_is_int(m))
1634 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1635 "expecting integer modulo", goto error);
1636 pa = isl_pw_aff_mod(pa, m->n);
1637 isl_val_free(m);
1638 return pa;
1639 error:
1640 isl_pw_aff_free(pa);
1641 isl_val_free(m);
1642 return NULL;
1645 /* Given f, return ceil(f).
1646 * If f is an integer expression, then just return f.
1647 * Otherwise, let f be the expression
1649 * e/m
1651 * then return
1653 * floor((e + m - 1)/m)
1655 * As a special case, ceil(NaN) = NaN.
1657 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1659 if (!aff)
1660 return NULL;
1662 if (isl_aff_is_nan(aff))
1663 return aff;
1664 if (isl_int_is_one(aff->v->el[0]))
1665 return aff;
1667 aff = isl_aff_cow(aff);
1668 if (!aff)
1669 return NULL;
1670 aff->v = isl_vec_cow(aff->v);
1671 if (!aff->v)
1672 return isl_aff_free(aff);
1674 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1675 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1676 aff = isl_aff_floor(aff);
1678 return aff;
1681 /* Apply the expansion computed by isl_merge_divs.
1682 * The expansion itself is given by "exp" while the resulting
1683 * list of divs is given by "div".
1685 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1686 __isl_take isl_mat *div, int *exp)
1688 isl_size old_n_div;
1689 isl_size new_n_div;
1690 int offset;
1692 aff = isl_aff_cow(aff);
1693 if (!aff || !div)
1694 goto error;
1696 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1697 new_n_div = isl_mat_rows(div);
1698 if (old_n_div < 0 || new_n_div < 0)
1699 goto error;
1700 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1702 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1703 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1704 if (!aff->v || !aff->ls)
1705 return isl_aff_free(aff);
1706 return aff;
1707 error:
1708 isl_aff_free(aff);
1709 isl_mat_free(div);
1710 return NULL;
1713 /* Add two affine expressions that live in the same local space.
1715 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1716 __isl_take isl_aff *aff2)
1718 isl_int gcd, f;
1720 aff1 = isl_aff_cow(aff1);
1721 if (!aff1 || !aff2)
1722 goto error;
1724 aff1->v = isl_vec_cow(aff1->v);
1725 if (!aff1->v)
1726 goto error;
1728 isl_int_init(gcd);
1729 isl_int_init(f);
1730 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1731 isl_int_divexact(f, aff2->v->el[0], gcd);
1732 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1733 isl_int_divexact(f, aff1->v->el[0], gcd);
1734 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1735 isl_int_divexact(f, aff2->v->el[0], gcd);
1736 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1737 isl_int_clear(f);
1738 isl_int_clear(gcd);
1740 isl_aff_free(aff2);
1741 return aff1;
1742 error:
1743 isl_aff_free(aff1);
1744 isl_aff_free(aff2);
1745 return NULL;
1748 /* Return the sum of "aff1" and "aff2".
1750 * If either of the two is NaN, then the result is NaN.
1752 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1753 __isl_take isl_aff *aff2)
1755 isl_ctx *ctx;
1756 int *exp1 = NULL;
1757 int *exp2 = NULL;
1758 isl_mat *div;
1759 isl_size n_div1, n_div2;
1761 if (!aff1 || !aff2)
1762 goto error;
1764 ctx = isl_aff_get_ctx(aff1);
1765 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1766 isl_die(ctx, isl_error_invalid,
1767 "spaces don't match", goto error);
1769 if (isl_aff_is_nan(aff1)) {
1770 isl_aff_free(aff2);
1771 return aff1;
1773 if (isl_aff_is_nan(aff2)) {
1774 isl_aff_free(aff1);
1775 return aff2;
1778 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1779 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1780 if (n_div1 < 0 || n_div2 < 0)
1781 goto error;
1782 if (n_div1 == 0 && n_div2 == 0)
1783 return add_expanded(aff1, aff2);
1785 exp1 = isl_alloc_array(ctx, int, n_div1);
1786 exp2 = isl_alloc_array(ctx, int, n_div2);
1787 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1788 goto error;
1790 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1791 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1792 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1793 free(exp1);
1794 free(exp2);
1796 return add_expanded(aff1, aff2);
1797 error:
1798 free(exp1);
1799 free(exp2);
1800 isl_aff_free(aff1);
1801 isl_aff_free(aff2);
1802 return NULL;
1805 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1806 __isl_take isl_aff *aff2)
1808 return isl_aff_add(aff1, isl_aff_neg(aff2));
1811 /* Return the result of scaling "aff" by a factor of "f".
1813 * As a special case, f * NaN = NaN.
1815 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1817 isl_int gcd;
1819 if (!aff)
1820 return NULL;
1821 if (isl_aff_is_nan(aff))
1822 return aff;
1824 if (isl_int_is_one(f))
1825 return aff;
1827 aff = isl_aff_cow(aff);
1828 if (!aff)
1829 return NULL;
1830 aff->v = isl_vec_cow(aff->v);
1831 if (!aff->v)
1832 return isl_aff_free(aff);
1834 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1835 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1836 return aff;
1839 isl_int_init(gcd);
1840 isl_int_gcd(gcd, aff->v->el[0], f);
1841 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1842 isl_int_divexact(gcd, f, gcd);
1843 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1844 isl_int_clear(gcd);
1846 return aff;
1849 /* Multiple "aff" by "v".
1851 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1852 __isl_take isl_val *v)
1854 if (!aff || !v)
1855 goto error;
1857 if (isl_val_is_one(v)) {
1858 isl_val_free(v);
1859 return aff;
1862 if (!isl_val_is_rat(v))
1863 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1864 "expecting rational factor", goto error);
1866 aff = isl_aff_scale(aff, v->n);
1867 aff = isl_aff_scale_down(aff, v->d);
1869 isl_val_free(v);
1870 return aff;
1871 error:
1872 isl_aff_free(aff);
1873 isl_val_free(v);
1874 return NULL;
1877 /* Return the result of scaling "aff" down by a factor of "f".
1879 * As a special case, NaN/f = NaN.
1881 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1883 isl_int gcd;
1885 if (!aff)
1886 return NULL;
1887 if (isl_aff_is_nan(aff))
1888 return aff;
1890 if (isl_int_is_one(f))
1891 return aff;
1893 aff = isl_aff_cow(aff);
1894 if (!aff)
1895 return NULL;
1897 if (isl_int_is_zero(f))
1898 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1899 "cannot scale down by zero", return isl_aff_free(aff));
1901 aff->v = isl_vec_cow(aff->v);
1902 if (!aff->v)
1903 return isl_aff_free(aff);
1905 isl_int_init(gcd);
1906 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1907 isl_int_gcd(gcd, gcd, f);
1908 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1909 isl_int_divexact(gcd, f, gcd);
1910 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1911 isl_int_clear(gcd);
1913 return aff;
1916 /* Divide "aff" by "v".
1918 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1919 __isl_take isl_val *v)
1921 if (!aff || !v)
1922 goto error;
1924 if (isl_val_is_one(v)) {
1925 isl_val_free(v);
1926 return aff;
1929 if (!isl_val_is_rat(v))
1930 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1931 "expecting rational factor", goto error);
1932 if (!isl_val_is_pos(v))
1933 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1934 "factor needs to be positive", goto error);
1936 aff = isl_aff_scale(aff, v->d);
1937 aff = isl_aff_scale_down(aff, v->n);
1939 isl_val_free(v);
1940 return aff;
1941 error:
1942 isl_aff_free(aff);
1943 isl_val_free(v);
1944 return NULL;
1947 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1949 isl_int v;
1951 if (f == 1)
1952 return aff;
1954 isl_int_init(v);
1955 isl_int_set_ui(v, f);
1956 aff = isl_aff_scale_down(aff, v);
1957 isl_int_clear(v);
1959 return aff;
1962 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1963 enum isl_dim_type type, unsigned pos, const char *s)
1965 aff = isl_aff_cow(aff);
1966 if (!aff)
1967 return NULL;
1968 if (type == isl_dim_out)
1969 isl_die(aff->v->ctx, isl_error_invalid,
1970 "cannot set name of output/set dimension",
1971 return isl_aff_free(aff));
1972 if (type == isl_dim_in)
1973 type = isl_dim_set;
1974 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1975 if (!aff->ls)
1976 return isl_aff_free(aff);
1978 return aff;
1981 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1982 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1984 aff = isl_aff_cow(aff);
1985 if (!aff)
1986 goto error;
1987 if (type == isl_dim_out)
1988 isl_die(aff->v->ctx, isl_error_invalid,
1989 "cannot set name of output/set dimension",
1990 goto error);
1991 if (type == isl_dim_in)
1992 type = isl_dim_set;
1993 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1994 if (!aff->ls)
1995 return isl_aff_free(aff);
1997 return aff;
1998 error:
1999 isl_id_free(id);
2000 isl_aff_free(aff);
2001 return NULL;
2004 /* Replace the identifier of the input tuple of "aff" by "id".
2005 * type is currently required to be equal to isl_dim_in
2007 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2008 enum isl_dim_type type, __isl_take isl_id *id)
2010 aff = isl_aff_cow(aff);
2011 if (!aff)
2012 goto error;
2013 if (type != isl_dim_in)
2014 isl_die(aff->v->ctx, isl_error_invalid,
2015 "cannot only set id of input tuple", goto error);
2016 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2017 if (!aff->ls)
2018 return isl_aff_free(aff);
2020 return aff;
2021 error:
2022 isl_id_free(id);
2023 isl_aff_free(aff);
2024 return NULL;
2027 /* Exploit the equalities in "eq" to simplify the affine expression
2028 * and the expressions of the integer divisions in the local space.
2029 * The integer divisions in this local space are assumed to appear
2030 * as regular dimensions in "eq".
2032 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2033 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2035 int i, j;
2036 unsigned o_div;
2037 unsigned n_div;
2039 if (!eq)
2040 goto error;
2041 if (eq->n_eq == 0) {
2042 isl_basic_set_free(eq);
2043 return aff;
2046 aff = isl_aff_cow(aff);
2047 if (!aff)
2048 goto error;
2050 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2051 isl_basic_set_copy(eq));
2052 aff->v = isl_vec_cow(aff->v);
2053 if (!aff->ls || !aff->v)
2054 goto error;
2056 o_div = isl_basic_set_offset(eq, isl_dim_div);
2057 n_div = eq->n_div;
2058 for (i = 0; i < eq->n_eq; ++i) {
2059 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2060 if (j < 0 || j == 0 || j >= o_div)
2061 continue;
2063 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2064 &aff->v->el[0]);
2067 isl_basic_set_free(eq);
2068 aff = isl_aff_normalize(aff);
2069 return aff;
2070 error:
2071 isl_basic_set_free(eq);
2072 isl_aff_free(aff);
2073 return NULL;
2076 /* Exploit the equalities in "eq" to simplify the affine expression
2077 * and the expressions of the integer divisions in the local space.
2079 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2080 __isl_take isl_basic_set *eq)
2082 isl_size n_div;
2084 if (!aff || !eq)
2085 goto error;
2086 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2087 if (n_div < 0)
2088 goto error;
2089 if (n_div > 0)
2090 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2091 return isl_aff_substitute_equalities_lifted(aff, eq);
2092 error:
2093 isl_basic_set_free(eq);
2094 isl_aff_free(aff);
2095 return NULL;
2098 /* Look for equalities among the variables shared by context and aff
2099 * and the integer divisions of aff, if any.
2100 * The equalities are then used to eliminate coefficients and/or integer
2101 * divisions from aff.
2103 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2104 __isl_take isl_set *context)
2106 isl_local_space *ls;
2107 isl_basic_set *hull;
2109 ls = isl_aff_get_domain_local_space(aff);
2110 context = isl_local_space_lift_set(ls, context);
2112 hull = isl_set_affine_hull(context);
2113 return isl_aff_substitute_equalities_lifted(aff, hull);
2116 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2117 __isl_take isl_set *context)
2119 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2120 dom_context = isl_set_intersect_params(dom_context, context);
2121 return isl_aff_gist(aff, dom_context);
2124 /* Return a basic set containing those elements in the space
2125 * of aff where it is positive. "rational" should not be set.
2127 * If "aff" is NaN, then it is not positive.
2129 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2130 int rational)
2132 isl_constraint *ineq;
2133 isl_basic_set *bset;
2134 isl_val *c;
2136 if (!aff)
2137 return NULL;
2138 if (isl_aff_is_nan(aff)) {
2139 isl_space *space = isl_aff_get_domain_space(aff);
2140 isl_aff_free(aff);
2141 return isl_basic_set_empty(space);
2143 if (rational)
2144 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2145 "rational sets not supported", goto error);
2147 ineq = isl_inequality_from_aff(aff);
2148 c = isl_constraint_get_constant_val(ineq);
2149 c = isl_val_sub_ui(c, 1);
2150 ineq = isl_constraint_set_constant_val(ineq, c);
2152 bset = isl_basic_set_from_constraint(ineq);
2153 bset = isl_basic_set_simplify(bset);
2154 return bset;
2155 error:
2156 isl_aff_free(aff);
2157 return NULL;
2160 /* Return a basic set containing those elements in the space
2161 * of aff where it is non-negative.
2162 * If "rational" is set, then return a rational basic set.
2164 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2166 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2167 __isl_take isl_aff *aff, int rational)
2169 isl_constraint *ineq;
2170 isl_basic_set *bset;
2172 if (!aff)
2173 return NULL;
2174 if (isl_aff_is_nan(aff)) {
2175 isl_space *space = isl_aff_get_domain_space(aff);
2176 isl_aff_free(aff);
2177 return isl_basic_set_empty(space);
2180 ineq = isl_inequality_from_aff(aff);
2182 bset = isl_basic_set_from_constraint(ineq);
2183 if (rational)
2184 bset = isl_basic_set_set_rational(bset);
2185 bset = isl_basic_set_simplify(bset);
2186 return bset;
2189 /* Return a basic set containing those elements in the space
2190 * of aff where it is non-negative.
2192 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2194 return aff_nonneg_basic_set(aff, 0);
2197 /* Return a basic set containing those elements in the domain space
2198 * of "aff" where it is positive.
2200 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2202 aff = isl_aff_add_constant_num_si(aff, -1);
2203 return isl_aff_nonneg_basic_set(aff);
2206 /* Return a basic set containing those elements in the domain space
2207 * of aff where it is negative.
2209 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2211 aff = isl_aff_neg(aff);
2212 return isl_aff_pos_basic_set(aff);
2215 /* Return a basic set containing those elements in the space
2216 * of aff where it is zero.
2217 * If "rational" is set, then return a rational basic set.
2219 * If "aff" is NaN, then it is not zero.
2221 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2222 int rational)
2224 isl_constraint *ineq;
2225 isl_basic_set *bset;
2227 if (!aff)
2228 return NULL;
2229 if (isl_aff_is_nan(aff)) {
2230 isl_space *space = isl_aff_get_domain_space(aff);
2231 isl_aff_free(aff);
2232 return isl_basic_set_empty(space);
2235 ineq = isl_equality_from_aff(aff);
2237 bset = isl_basic_set_from_constraint(ineq);
2238 if (rational)
2239 bset = isl_basic_set_set_rational(bset);
2240 bset = isl_basic_set_simplify(bset);
2241 return bset;
2244 /* Return a basic set containing those elements in the space
2245 * of aff where it is zero.
2247 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2249 return aff_zero_basic_set(aff, 0);
2252 /* Return a basic set containing those elements in the shared space
2253 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2255 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2256 __isl_take isl_aff *aff2)
2258 aff1 = isl_aff_sub(aff1, aff2);
2260 return isl_aff_nonneg_basic_set(aff1);
2263 /* Return a basic set containing those elements in the shared domain space
2264 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2266 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2267 __isl_take isl_aff *aff2)
2269 aff1 = isl_aff_sub(aff1, aff2);
2271 return isl_aff_pos_basic_set(aff1);
2274 /* Return a set containing those elements in the shared space
2275 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2277 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2278 __isl_take isl_aff *aff2)
2280 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2283 /* Return a set containing those elements in the shared domain space
2284 * of aff1 and aff2 where aff1 is greater than aff2.
2286 * If either of the two inputs is NaN, then the result is empty,
2287 * as comparisons with NaN always return false.
2289 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2290 __isl_take isl_aff *aff2)
2292 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2295 /* Return a basic set containing those elements in the shared space
2296 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2298 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2299 __isl_take isl_aff *aff2)
2301 return isl_aff_ge_basic_set(aff2, aff1);
2304 /* Return a basic set containing those elements in the shared domain space
2305 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2307 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2308 __isl_take isl_aff *aff2)
2310 return isl_aff_gt_basic_set(aff2, aff1);
2313 /* Return a set containing those elements in the shared space
2314 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2316 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2317 __isl_take isl_aff *aff2)
2319 return isl_aff_ge_set(aff2, aff1);
2322 /* Return a set containing those elements in the shared domain space
2323 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2325 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2326 __isl_take isl_aff *aff2)
2328 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2331 /* Return a basic set containing those elements in the shared space
2332 * of aff1 and aff2 where aff1 and aff2 are equal.
2334 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2335 __isl_take isl_aff *aff2)
2337 aff1 = isl_aff_sub(aff1, aff2);
2339 return isl_aff_zero_basic_set(aff1);
2342 /* Return a set containing those elements in the shared space
2343 * of aff1 and aff2 where aff1 and aff2 are equal.
2345 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2346 __isl_take isl_aff *aff2)
2348 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2351 /* Return a set containing those elements in the shared domain space
2352 * of aff1 and aff2 where aff1 and aff2 are not equal.
2354 * If either of the two inputs is NaN, then the result is empty,
2355 * as comparisons with NaN always return false.
2357 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2358 __isl_take isl_aff *aff2)
2360 isl_set *set_lt, *set_gt;
2362 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2363 isl_aff_copy(aff2));
2364 set_gt = isl_aff_gt_set(aff1, aff2);
2365 return isl_set_union_disjoint(set_lt, set_gt);
2368 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2369 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2371 aff1 = isl_aff_add(aff1, aff2);
2372 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2373 return aff1;
2376 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2378 if (!aff)
2379 return -1;
2381 return 0;
2384 #undef TYPE
2385 #define TYPE isl_aff
2386 static
2387 #include "check_type_range_templ.c"
2389 /* Check whether the given affine expression has non-zero coefficient
2390 * for any dimension in the given range or if any of these dimensions
2391 * appear with non-zero coefficients in any of the integer divisions
2392 * involved in the affine expression.
2394 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2395 enum isl_dim_type type, unsigned first, unsigned n)
2397 int i;
2398 int *active = NULL;
2399 isl_bool involves = isl_bool_false;
2401 if (!aff)
2402 return isl_bool_error;
2403 if (n == 0)
2404 return isl_bool_false;
2405 if (isl_aff_check_range(aff, type, first, n) < 0)
2406 return isl_bool_error;
2408 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2409 if (!active)
2410 goto error;
2412 first += isl_local_space_offset(aff->ls, type) - 1;
2413 for (i = 0; i < n; ++i)
2414 if (active[first + i]) {
2415 involves = isl_bool_true;
2416 break;
2419 free(active);
2421 return involves;
2422 error:
2423 free(active);
2424 return isl_bool_error;
2427 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2428 enum isl_dim_type type, unsigned first, unsigned n)
2430 isl_ctx *ctx;
2432 if (!aff)
2433 return NULL;
2434 if (type == isl_dim_out)
2435 isl_die(aff->v->ctx, isl_error_invalid,
2436 "cannot drop output/set dimension",
2437 return isl_aff_free(aff));
2438 if (type == isl_dim_in)
2439 type = isl_dim_set;
2440 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2441 return aff;
2443 ctx = isl_aff_get_ctx(aff);
2444 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2445 return isl_aff_free(aff);
2447 aff = isl_aff_cow(aff);
2448 if (!aff)
2449 return NULL;
2451 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2452 if (!aff->ls)
2453 return isl_aff_free(aff);
2455 first += 1 + isl_local_space_offset(aff->ls, type);
2456 aff->v = isl_vec_drop_els(aff->v, first, n);
2457 if (!aff->v)
2458 return isl_aff_free(aff);
2460 return aff;
2463 /* Drop the "n" domain dimensions starting at "first" from "aff",
2464 * after checking that they do not appear in the affine expression.
2466 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2467 unsigned n)
2469 isl_bool involves;
2471 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2472 if (involves < 0)
2473 return isl_aff_free(aff);
2474 if (involves)
2475 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2476 "affine expression involves some of the domain dimensions",
2477 return isl_aff_free(aff));
2478 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2481 /* Project the domain of the affine expression onto its parameter space.
2482 * The affine expression may not involve any of the domain dimensions.
2484 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2486 isl_space *space;
2487 isl_size n;
2489 n = isl_aff_dim(aff, isl_dim_in);
2490 if (n < 0)
2491 return isl_aff_free(aff);
2492 aff = drop_domain(aff, 0, n);
2493 space = isl_aff_get_domain_space(aff);
2494 space = isl_space_params(space);
2495 aff = isl_aff_reset_domain_space(aff, space);
2496 return aff;
2499 /* Check that the domain of "aff" is a product.
2501 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2503 isl_bool is_product;
2505 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2506 if (is_product < 0)
2507 return isl_stat_error;
2508 if (!is_product)
2509 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2510 "domain is not a product", return isl_stat_error);
2511 return isl_stat_ok;
2514 /* Given an affine function with a domain of the form [A -> B] that
2515 * does not depend on B, return the same function on domain A.
2517 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2519 isl_space *space;
2520 isl_size n, n_in;
2522 if (check_domain_product(aff) < 0)
2523 return isl_aff_free(aff);
2524 space = isl_aff_get_domain_space(aff);
2525 n = isl_space_dim(space, isl_dim_set);
2526 space = isl_space_factor_domain(space);
2527 n_in = isl_space_dim(space, isl_dim_set);
2528 if (n < 0 || n_in < 0)
2529 aff = isl_aff_free(aff);
2530 else
2531 aff = drop_domain(aff, n_in, n - n_in);
2532 aff = isl_aff_reset_domain_space(aff, space);
2533 return aff;
2536 /* Convert an affine expression defined over a parameter domain
2537 * into one that is defined over a zero-dimensional set.
2539 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2541 isl_local_space *ls;
2543 ls = isl_aff_take_domain_local_space(aff);
2544 ls = isl_local_space_set_from_params(ls);
2545 aff = isl_aff_restore_domain_local_space(aff, ls);
2547 return aff;
2550 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2551 enum isl_dim_type type, unsigned first, unsigned n)
2553 isl_ctx *ctx;
2555 if (!aff)
2556 return NULL;
2557 if (type == isl_dim_out)
2558 isl_die(aff->v->ctx, isl_error_invalid,
2559 "cannot insert output/set dimensions",
2560 return isl_aff_free(aff));
2561 if (type == isl_dim_in)
2562 type = isl_dim_set;
2563 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2564 return aff;
2566 ctx = isl_aff_get_ctx(aff);
2567 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2568 return isl_aff_free(aff);
2570 aff = isl_aff_cow(aff);
2571 if (!aff)
2572 return NULL;
2574 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2575 if (!aff->ls)
2576 return isl_aff_free(aff);
2578 first += 1 + isl_local_space_offset(aff->ls, type);
2579 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2580 if (!aff->v)
2581 return isl_aff_free(aff);
2583 return aff;
2586 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2587 enum isl_dim_type type, unsigned n)
2589 isl_size pos;
2591 pos = isl_aff_dim(aff, type);
2592 if (pos < 0)
2593 return isl_aff_free(aff);
2595 return isl_aff_insert_dims(aff, type, pos, n);
2598 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2599 enum isl_dim_type type, unsigned n)
2601 isl_size pos;
2603 pos = isl_pw_aff_dim(pwaff, type);
2604 if (pos < 0)
2605 return isl_pw_aff_free(pwaff);
2607 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2610 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2611 * to dimensions of "dst_type" at "dst_pos".
2613 * We only support moving input dimensions to parameters and vice versa.
2615 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2616 enum isl_dim_type dst_type, unsigned dst_pos,
2617 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2619 unsigned g_dst_pos;
2620 unsigned g_src_pos;
2622 if (!aff)
2623 return NULL;
2624 if (n == 0 &&
2625 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2626 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2627 return aff;
2629 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2630 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2631 "cannot move output/set dimension",
2632 return isl_aff_free(aff));
2633 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2634 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2635 "cannot move divs", return isl_aff_free(aff));
2636 if (dst_type == isl_dim_in)
2637 dst_type = isl_dim_set;
2638 if (src_type == isl_dim_in)
2639 src_type = isl_dim_set;
2641 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2642 return isl_aff_free(aff);
2643 if (dst_type == src_type)
2644 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2645 "moving dims within the same type not supported",
2646 return isl_aff_free(aff));
2648 aff = isl_aff_cow(aff);
2649 if (!aff)
2650 return NULL;
2652 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2653 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2654 if (dst_type > src_type)
2655 g_dst_pos -= n;
2657 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2658 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2659 src_type, src_pos, n);
2660 if (!aff->v || !aff->ls)
2661 return isl_aff_free(aff);
2663 aff = sort_divs(aff);
2665 return aff;
2668 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2670 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2671 return isl_pw_aff_alloc(dom, aff);
2674 #define isl_aff_involves_nan isl_aff_is_nan
2676 #undef PW
2677 #define PW isl_pw_aff
2678 #undef EL
2679 #define EL isl_aff
2680 #undef EL_IS_ZERO
2681 #define EL_IS_ZERO is_empty
2682 #undef ZERO
2683 #define ZERO empty
2684 #undef IS_ZERO
2685 #define IS_ZERO is_empty
2686 #undef FIELD
2687 #define FIELD aff
2688 #undef DEFAULT_IS_ZERO
2689 #define DEFAULT_IS_ZERO 0
2691 #define NO_OPT
2692 #define NO_LIFT
2693 #define NO_MORPH
2695 #include <isl_pw_templ.c>
2696 #include <isl_pw_eval.c>
2697 #include <isl_pw_hash.c>
2698 #include <isl_pw_union_opt.c>
2700 #undef BASE
2701 #define BASE pw_aff
2703 #include <isl_union_single.c>
2704 #include <isl_union_neg.c>
2706 static __isl_give isl_set *align_params_pw_pw_set_and(
2707 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2708 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2709 __isl_take isl_pw_aff *pwaff2))
2711 isl_bool equal_params;
2713 if (!pwaff1 || !pwaff2)
2714 goto error;
2715 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2716 if (equal_params < 0)
2717 goto error;
2718 if (equal_params)
2719 return fn(pwaff1, pwaff2);
2720 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2721 isl_pw_aff_check_named_params(pwaff2) < 0)
2722 goto error;
2723 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2724 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2725 return fn(pwaff1, pwaff2);
2726 error:
2727 isl_pw_aff_free(pwaff1);
2728 isl_pw_aff_free(pwaff2);
2729 return NULL;
2732 /* Align the parameters of the to isl_pw_aff arguments and
2733 * then apply a function "fn" on them that returns an isl_map.
2735 static __isl_give isl_map *align_params_pw_pw_map_and(
2736 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2737 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2738 __isl_take isl_pw_aff *pa2))
2740 isl_bool equal_params;
2742 if (!pa1 || !pa2)
2743 goto error;
2744 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2745 if (equal_params < 0)
2746 goto error;
2747 if (equal_params)
2748 return fn(pa1, pa2);
2749 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2750 isl_pw_aff_check_named_params(pa2) < 0)
2751 goto error;
2752 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2753 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2754 return fn(pa1, pa2);
2755 error:
2756 isl_pw_aff_free(pa1);
2757 isl_pw_aff_free(pa2);
2758 return NULL;
2761 /* Compute a piecewise quasi-affine expression with a domain that
2762 * is the union of those of pwaff1 and pwaff2 and such that on each
2763 * cell, the quasi-affine expression is the maximum of those of pwaff1
2764 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2765 * cell, then the associated expression is the defined one.
2767 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2768 __isl_take isl_pw_aff *pwaff2)
2770 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2773 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2774 __isl_take isl_pw_aff *pwaff2)
2776 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2777 &pw_aff_union_max);
2780 /* Compute a piecewise quasi-affine expression with a domain that
2781 * is the union of those of pwaff1 and pwaff2 and such that on each
2782 * cell, the quasi-affine expression is the minimum of those of pwaff1
2783 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2784 * cell, then the associated expression is the defined one.
2786 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2787 __isl_take isl_pw_aff *pwaff2)
2789 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2792 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2793 __isl_take isl_pw_aff *pwaff2)
2795 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2796 &pw_aff_union_min);
2799 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2800 __isl_take isl_pw_aff *pwaff2, int max)
2802 if (max)
2803 return isl_pw_aff_union_max(pwaff1, pwaff2);
2804 else
2805 return isl_pw_aff_union_min(pwaff1, pwaff2);
2808 /* Return a set containing those elements in the domain
2809 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2810 * does not satisfy "fn" (if complement is 1).
2812 * The pieces with a NaN never belong to the result since
2813 * NaN does not satisfy any property.
2815 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2816 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2817 int complement)
2819 int i;
2820 isl_set *set;
2822 if (!pwaff)
2823 return NULL;
2825 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2827 for (i = 0; i < pwaff->n; ++i) {
2828 isl_basic_set *bset;
2829 isl_set *set_i, *locus;
2830 isl_bool rational;
2832 if (isl_aff_is_nan(pwaff->p[i].aff))
2833 continue;
2835 rational = isl_set_has_rational(pwaff->p[i].set);
2836 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2837 locus = isl_set_from_basic_set(bset);
2838 set_i = isl_set_copy(pwaff->p[i].set);
2839 if (complement)
2840 set_i = isl_set_subtract(set_i, locus);
2841 else
2842 set_i = isl_set_intersect(set_i, locus);
2843 set = isl_set_union_disjoint(set, set_i);
2846 isl_pw_aff_free(pwaff);
2848 return set;
2851 /* Return a set containing those elements in the domain
2852 * of "pa" where it is positive.
2854 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2856 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2859 /* Return a set containing those elements in the domain
2860 * of pwaff where it is non-negative.
2862 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2864 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2867 /* Return a set containing those elements in the domain
2868 * of pwaff where it is zero.
2870 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2872 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2875 /* Return a set containing those elements in the domain
2876 * of pwaff where it is not zero.
2878 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2880 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2883 /* Return a set containing those elements in the shared domain
2884 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2886 * We compute the difference on the shared domain and then construct
2887 * the set of values where this difference is non-negative.
2888 * If strict is set, we first subtract 1 from the difference.
2889 * If equal is set, we only return the elements where pwaff1 and pwaff2
2890 * are equal.
2892 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2893 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2895 isl_set *set1, *set2;
2897 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2898 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2899 set1 = isl_set_intersect(set1, set2);
2900 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2901 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2902 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2904 if (strict) {
2905 isl_space *space = isl_set_get_space(set1);
2906 isl_aff *aff;
2907 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2908 aff = isl_aff_add_constant_si(aff, -1);
2909 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2910 } else
2911 isl_set_free(set1);
2913 if (equal)
2914 return isl_pw_aff_zero_set(pwaff1);
2915 return isl_pw_aff_nonneg_set(pwaff1);
2918 /* Return a set containing those elements in the shared domain
2919 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2921 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2922 __isl_take isl_pw_aff *pwaff2)
2924 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2927 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2928 __isl_take isl_pw_aff *pwaff2)
2930 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2933 /* Return a set containing those elements in the shared domain
2934 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2936 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2937 __isl_take isl_pw_aff *pwaff2)
2939 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2942 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2943 __isl_take isl_pw_aff *pwaff2)
2945 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2948 /* Return a set containing those elements in the shared domain
2949 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2951 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2952 __isl_take isl_pw_aff *pwaff2)
2954 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2957 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2958 __isl_take isl_pw_aff *pwaff2)
2960 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2963 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2964 __isl_take isl_pw_aff *pwaff2)
2966 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2969 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2970 __isl_take isl_pw_aff *pwaff2)
2972 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2975 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2976 * where the function values are ordered in the same way as "order",
2977 * which returns a set in the shared domain of its two arguments.
2978 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2980 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2981 * We first pull back the two functions such that they are defined on
2982 * the domain [A -> B]. Then we apply "order", resulting in a set
2983 * in the space [A -> B]. Finally, we unwrap this set to obtain
2984 * a map in the space A -> B.
2986 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2987 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2988 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2989 __isl_take isl_pw_aff *pa2))
2991 isl_space *space1, *space2;
2992 isl_multi_aff *ma;
2993 isl_set *set;
2995 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2996 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
2997 space1 = isl_space_map_from_domain_and_range(space1, space2);
2998 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
2999 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3000 ma = isl_multi_aff_range_map(space1);
3001 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3002 set = order(pa1, pa2);
3004 return isl_set_unwrap(set);
3007 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3008 * where the function values are equal.
3009 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3011 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3012 __isl_take isl_pw_aff *pa2)
3014 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3017 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3018 * where the function values are equal.
3020 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3021 __isl_take isl_pw_aff *pa2)
3023 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3026 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3027 * where the function value of "pa1" is less than the function value of "pa2".
3028 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3030 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3031 __isl_take isl_pw_aff *pa2)
3033 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3036 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3037 * where the function value of "pa1" is less than the function value of "pa2".
3039 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3040 __isl_take isl_pw_aff *pa2)
3042 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3045 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3046 * where the function value of "pa1" is greater than the function value
3047 * of "pa2".
3048 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3050 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3051 __isl_take isl_pw_aff *pa2)
3053 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3056 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3057 * where the function value of "pa1" is greater than the function value
3058 * of "pa2".
3060 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3061 __isl_take isl_pw_aff *pa2)
3063 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3066 /* Return a set containing those elements in the shared domain
3067 * of the elements of list1 and list2 where each element in list1
3068 * has the relation specified by "fn" with each element in list2.
3070 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3071 __isl_take isl_pw_aff_list *list2,
3072 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3073 __isl_take isl_pw_aff *pwaff2))
3075 int i, j;
3076 isl_ctx *ctx;
3077 isl_set *set;
3079 if (!list1 || !list2)
3080 goto error;
3082 ctx = isl_pw_aff_list_get_ctx(list1);
3083 if (list1->n < 1 || list2->n < 1)
3084 isl_die(ctx, isl_error_invalid,
3085 "list should contain at least one element", goto error);
3087 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3088 for (i = 0; i < list1->n; ++i)
3089 for (j = 0; j < list2->n; ++j) {
3090 isl_set *set_ij;
3092 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3093 isl_pw_aff_copy(list2->p[j]));
3094 set = isl_set_intersect(set, set_ij);
3097 isl_pw_aff_list_free(list1);
3098 isl_pw_aff_list_free(list2);
3099 return set;
3100 error:
3101 isl_pw_aff_list_free(list1);
3102 isl_pw_aff_list_free(list2);
3103 return NULL;
3106 /* Return a set containing those elements in the shared domain
3107 * of the elements of list1 and list2 where each element in list1
3108 * is equal to each element in list2.
3110 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3111 __isl_take isl_pw_aff_list *list2)
3113 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3116 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3117 __isl_take isl_pw_aff_list *list2)
3119 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3122 /* Return a set containing those elements in the shared domain
3123 * of the elements of list1 and list2 where each element in list1
3124 * is less than or equal to each element in list2.
3126 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3127 __isl_take isl_pw_aff_list *list2)
3129 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3132 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3133 __isl_take isl_pw_aff_list *list2)
3135 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3138 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3139 __isl_take isl_pw_aff_list *list2)
3141 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3144 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3145 __isl_take isl_pw_aff_list *list2)
3147 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3151 /* Return a set containing those elements in the shared domain
3152 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3154 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3155 __isl_take isl_pw_aff *pwaff2)
3157 isl_set *set_lt, *set_gt;
3159 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3160 isl_pw_aff_copy(pwaff2));
3161 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3162 return isl_set_union_disjoint(set_lt, set_gt);
3165 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3166 __isl_take isl_pw_aff *pwaff2)
3168 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3171 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3172 isl_int v)
3174 int i;
3176 if (isl_int_is_one(v))
3177 return pwaff;
3178 if (!isl_int_is_pos(v))
3179 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3180 "factor needs to be positive",
3181 return isl_pw_aff_free(pwaff));
3182 pwaff = isl_pw_aff_cow(pwaff);
3183 if (!pwaff)
3184 return NULL;
3185 if (pwaff->n == 0)
3186 return pwaff;
3188 for (i = 0; i < pwaff->n; ++i) {
3189 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3190 if (!pwaff->p[i].aff)
3191 return isl_pw_aff_free(pwaff);
3194 return pwaff;
3197 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3199 int i;
3201 pwaff = isl_pw_aff_cow(pwaff);
3202 if (!pwaff)
3203 return NULL;
3204 if (pwaff->n == 0)
3205 return pwaff;
3207 for (i = 0; i < pwaff->n; ++i) {
3208 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3209 if (!pwaff->p[i].aff)
3210 return isl_pw_aff_free(pwaff);
3213 return pwaff;
3216 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3218 int i;
3220 pwaff = isl_pw_aff_cow(pwaff);
3221 if (!pwaff)
3222 return NULL;
3223 if (pwaff->n == 0)
3224 return pwaff;
3226 for (i = 0; i < pwaff->n; ++i) {
3227 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3228 if (!pwaff->p[i].aff)
3229 return isl_pw_aff_free(pwaff);
3232 return pwaff;
3235 /* Assuming that "cond1" and "cond2" are disjoint,
3236 * return an affine expression that is equal to pwaff1 on cond1
3237 * and to pwaff2 on cond2.
3239 static __isl_give isl_pw_aff *isl_pw_aff_select(
3240 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3241 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3243 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3244 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3246 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3249 /* Return an affine expression that is equal to pwaff_true for elements
3250 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3251 * is zero.
3252 * That is, return cond ? pwaff_true : pwaff_false;
3254 * If "cond" involves and NaN, then we conservatively return a NaN
3255 * on its entire domain. In principle, we could consider the pieces
3256 * where it is NaN separately from those where it is not.
3258 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3259 * then only use the domain of "cond" to restrict the domain.
3261 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3262 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3264 isl_set *cond_true, *cond_false;
3265 isl_bool equal;
3267 if (!cond)
3268 goto error;
3269 if (isl_pw_aff_involves_nan(cond)) {
3270 isl_space *space = isl_pw_aff_get_domain_space(cond);
3271 isl_local_space *ls = isl_local_space_from_space(space);
3272 isl_pw_aff_free(cond);
3273 isl_pw_aff_free(pwaff_true);
3274 isl_pw_aff_free(pwaff_false);
3275 return isl_pw_aff_nan_on_domain(ls);
3278 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3279 isl_pw_aff_get_space(pwaff_false));
3280 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3281 isl_pw_aff_get_space(pwaff_true));
3282 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3283 if (equal < 0)
3284 goto error;
3285 if (equal) {
3286 isl_set *dom;
3288 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3289 isl_pw_aff_free(pwaff_false);
3290 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3293 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3294 cond_false = isl_pw_aff_zero_set(cond);
3295 return isl_pw_aff_select(cond_true, pwaff_true,
3296 cond_false, pwaff_false);
3297 error:
3298 isl_pw_aff_free(cond);
3299 isl_pw_aff_free(pwaff_true);
3300 isl_pw_aff_free(pwaff_false);
3301 return NULL;
3304 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3306 if (!aff)
3307 return isl_bool_error;
3309 return isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2) == -1;
3312 /* Check whether pwaff is a piecewise constant.
3314 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3316 int i;
3318 if (!pwaff)
3319 return isl_bool_error;
3321 for (i = 0; i < pwaff->n; ++i) {
3322 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3323 if (is_cst < 0 || !is_cst)
3324 return is_cst;
3327 return isl_bool_true;
3330 /* Are all elements of "mpa" piecewise constants?
3332 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3334 int i;
3336 if (!mpa)
3337 return isl_bool_error;
3339 for (i = 0; i < mpa->n; ++i) {
3340 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3341 if (is_cst < 0 || !is_cst)
3342 return is_cst;
3345 return isl_bool_true;
3348 /* Return the product of "aff1" and "aff2".
3350 * If either of the two is NaN, then the result is NaN.
3352 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3354 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3355 __isl_take isl_aff *aff2)
3357 if (!aff1 || !aff2)
3358 goto error;
3360 if (isl_aff_is_nan(aff1)) {
3361 isl_aff_free(aff2);
3362 return aff1;
3364 if (isl_aff_is_nan(aff2)) {
3365 isl_aff_free(aff1);
3366 return aff2;
3369 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3370 return isl_aff_mul(aff2, aff1);
3372 if (!isl_aff_is_cst(aff2))
3373 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3374 "at least one affine expression should be constant",
3375 goto error);
3377 aff1 = isl_aff_cow(aff1);
3378 if (!aff1 || !aff2)
3379 goto error;
3381 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3382 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3384 isl_aff_free(aff2);
3385 return aff1;
3386 error:
3387 isl_aff_free(aff1);
3388 isl_aff_free(aff2);
3389 return NULL;
3392 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3394 * If either of the two is NaN, then the result is NaN.
3396 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3397 __isl_take isl_aff *aff2)
3399 int is_cst;
3400 int neg;
3402 if (!aff1 || !aff2)
3403 goto error;
3405 if (isl_aff_is_nan(aff1)) {
3406 isl_aff_free(aff2);
3407 return aff1;
3409 if (isl_aff_is_nan(aff2)) {
3410 isl_aff_free(aff1);
3411 return aff2;
3414 is_cst = isl_aff_is_cst(aff2);
3415 if (is_cst < 0)
3416 goto error;
3417 if (!is_cst)
3418 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3419 "second argument should be a constant", goto error);
3421 if (!aff2)
3422 goto error;
3424 neg = isl_int_is_neg(aff2->v->el[1]);
3425 if (neg) {
3426 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3427 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3430 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3431 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3433 if (neg) {
3434 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3435 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3438 isl_aff_free(aff2);
3439 return aff1;
3440 error:
3441 isl_aff_free(aff1);
3442 isl_aff_free(aff2);
3443 return NULL;
3446 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3447 __isl_take isl_pw_aff *pwaff2)
3449 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3452 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3453 __isl_take isl_pw_aff *pwaff2)
3455 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3458 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3459 __isl_take isl_pw_aff *pwaff2)
3461 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3464 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3465 __isl_take isl_pw_aff *pwaff2)
3467 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3470 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3471 __isl_take isl_pw_aff *pwaff2)
3473 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3476 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3477 __isl_take isl_pw_aff *pa2)
3479 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3482 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3484 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3485 __isl_take isl_pw_aff *pa2)
3487 int is_cst;
3489 is_cst = isl_pw_aff_is_cst(pa2);
3490 if (is_cst < 0)
3491 goto error;
3492 if (!is_cst)
3493 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3494 "second argument should be a piecewise constant",
3495 goto error);
3496 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3497 error:
3498 isl_pw_aff_free(pa1);
3499 isl_pw_aff_free(pa2);
3500 return NULL;
3503 /* Compute the quotient of the integer division of "pa1" by "pa2"
3504 * with rounding towards zero.
3505 * "pa2" is assumed to be a piecewise constant.
3507 * In particular, return
3509 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3512 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3513 __isl_take isl_pw_aff *pa2)
3515 int is_cst;
3516 isl_set *cond;
3517 isl_pw_aff *f, *c;
3519 is_cst = isl_pw_aff_is_cst(pa2);
3520 if (is_cst < 0)
3521 goto error;
3522 if (!is_cst)
3523 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3524 "second argument should be a piecewise constant",
3525 goto error);
3527 pa1 = isl_pw_aff_div(pa1, pa2);
3529 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3530 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3531 c = isl_pw_aff_ceil(pa1);
3532 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3533 error:
3534 isl_pw_aff_free(pa1);
3535 isl_pw_aff_free(pa2);
3536 return NULL;
3539 /* Compute the remainder of the integer division of "pa1" by "pa2"
3540 * with rounding towards zero.
3541 * "pa2" is assumed to be a piecewise constant.
3543 * In particular, return
3545 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3548 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3549 __isl_take isl_pw_aff *pa2)
3551 int is_cst;
3552 isl_pw_aff *res;
3554 is_cst = isl_pw_aff_is_cst(pa2);
3555 if (is_cst < 0)
3556 goto error;
3557 if (!is_cst)
3558 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3559 "second argument should be a piecewise constant",
3560 goto error);
3561 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3562 res = isl_pw_aff_mul(pa2, res);
3563 res = isl_pw_aff_sub(pa1, res);
3564 return res;
3565 error:
3566 isl_pw_aff_free(pa1);
3567 isl_pw_aff_free(pa2);
3568 return NULL;
3571 /* Does either of "pa1" or "pa2" involve any NaN2?
3573 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3574 __isl_keep isl_pw_aff *pa2)
3576 isl_bool has_nan;
3578 has_nan = isl_pw_aff_involves_nan(pa1);
3579 if (has_nan < 0 || has_nan)
3580 return has_nan;
3581 return isl_pw_aff_involves_nan(pa2);
3584 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3585 * by a NaN on their shared domain.
3587 * In principle, the result could be refined to only being NaN
3588 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3590 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3591 __isl_take isl_pw_aff *pa2)
3593 isl_local_space *ls;
3594 isl_set *dom;
3595 isl_pw_aff *pa;
3597 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3598 ls = isl_local_space_from_space(isl_set_get_space(dom));
3599 pa = isl_pw_aff_nan_on_domain(ls);
3600 pa = isl_pw_aff_intersect_domain(pa, dom);
3602 return pa;
3605 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3606 __isl_take isl_pw_aff *pwaff2)
3608 isl_set *le;
3609 isl_set *dom;
3611 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3612 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3613 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3614 isl_pw_aff_copy(pwaff2));
3615 dom = isl_set_subtract(dom, isl_set_copy(le));
3616 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3619 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3620 __isl_take isl_pw_aff *pwaff2)
3622 isl_set *ge;
3623 isl_set *dom;
3625 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3626 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3627 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3628 isl_pw_aff_copy(pwaff2));
3629 dom = isl_set_subtract(dom, isl_set_copy(ge));
3630 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3633 /* Return an expression for the minimum (if "max" is not set) or
3634 * the maximum (if "max" is set) of "pa1" and "pa2".
3635 * If either expression involves any NaN, then return a NaN
3636 * on the shared domain as result.
3638 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3639 __isl_take isl_pw_aff *pa2, int max)
3641 isl_bool has_nan;
3643 has_nan = either_involves_nan(pa1, pa2);
3644 if (has_nan < 0)
3645 pa1 = isl_pw_aff_free(pa1);
3646 else if (has_nan)
3647 return replace_by_nan(pa1, pa2);
3649 if (max)
3650 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3651 else
3652 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3655 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3657 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3658 __isl_take isl_pw_aff *pwaff2)
3660 return pw_aff_min_max(pwaff1, pwaff2, 0);
3663 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3665 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3666 __isl_take isl_pw_aff *pwaff2)
3668 return pw_aff_min_max(pwaff1, pwaff2, 1);
3671 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3672 __isl_take isl_pw_aff_list *list,
3673 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3674 __isl_take isl_pw_aff *pwaff2))
3676 int i;
3677 isl_ctx *ctx;
3678 isl_pw_aff *res;
3680 if (!list)
3681 return NULL;
3683 ctx = isl_pw_aff_list_get_ctx(list);
3684 if (list->n < 1)
3685 isl_die(ctx, isl_error_invalid,
3686 "list should contain at least one element", goto error);
3688 res = isl_pw_aff_copy(list->p[0]);
3689 for (i = 1; i < list->n; ++i)
3690 res = fn(res, isl_pw_aff_copy(list->p[i]));
3692 isl_pw_aff_list_free(list);
3693 return res;
3694 error:
3695 isl_pw_aff_list_free(list);
3696 return NULL;
3699 /* Return an isl_pw_aff that maps each element in the intersection of the
3700 * domains of the elements of list to the minimal corresponding affine
3701 * expression.
3703 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3705 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3708 /* Return an isl_pw_aff that maps each element in the intersection of the
3709 * domains of the elements of list to the maximal corresponding affine
3710 * expression.
3712 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3714 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3717 /* Mark the domains of "pwaff" as rational.
3719 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3721 int i;
3723 pwaff = isl_pw_aff_cow(pwaff);
3724 if (!pwaff)
3725 return NULL;
3726 if (pwaff->n == 0)
3727 return pwaff;
3729 for (i = 0; i < pwaff->n; ++i) {
3730 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3731 if (!pwaff->p[i].set)
3732 return isl_pw_aff_free(pwaff);
3735 return pwaff;
3738 /* Mark the domains of the elements of "list" as rational.
3740 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3741 __isl_take isl_pw_aff_list *list)
3743 int i, n;
3745 if (!list)
3746 return NULL;
3747 if (list->n == 0)
3748 return list;
3750 n = list->n;
3751 for (i = 0; i < n; ++i) {
3752 isl_pw_aff *pa;
3754 pa = isl_pw_aff_list_get_pw_aff(list, i);
3755 pa = isl_pw_aff_set_rational(pa);
3756 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3759 return list;
3762 /* Do the parameters of "aff" match those of "space"?
3764 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3765 __isl_keep isl_space *space)
3767 isl_space *aff_space;
3768 isl_bool match;
3770 if (!aff || !space)
3771 return isl_bool_error;
3773 aff_space = isl_aff_get_domain_space(aff);
3775 match = isl_space_has_equal_params(space, aff_space);
3777 isl_space_free(aff_space);
3778 return match;
3781 /* Check that the domain space of "aff" matches "space".
3783 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3784 __isl_keep isl_space *space)
3786 isl_space *aff_space;
3787 isl_bool match;
3789 if (!aff || !space)
3790 return isl_stat_error;
3792 aff_space = isl_aff_get_domain_space(aff);
3794 match = isl_space_has_equal_params(space, aff_space);
3795 if (match < 0)
3796 goto error;
3797 if (!match)
3798 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3799 "parameters don't match", goto error);
3800 match = isl_space_tuple_is_equal(space, isl_dim_in,
3801 aff_space, isl_dim_set);
3802 if (match < 0)
3803 goto error;
3804 if (!match)
3805 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3806 "domains don't match", goto error);
3807 isl_space_free(aff_space);
3808 return isl_stat_ok;
3809 error:
3810 isl_space_free(aff_space);
3811 return isl_stat_error;
3814 #undef BASE
3815 #define BASE aff
3816 #undef DOMBASE
3817 #define DOMBASE set
3819 #include <isl_multi_no_explicit_domain.c>
3820 #include <isl_multi_templ.c>
3821 #include <isl_multi_apply_set.c>
3822 #include <isl_multi_cmp.c>
3823 #include <isl_multi_dims.c>
3824 #include <isl_multi_floor.c>
3825 #include <isl_multi_from_base_templ.c>
3826 #include <isl_multi_gist.c>
3827 #include <isl_multi_identity_templ.c>
3828 #include <isl_multi_move_dims_templ.c>
3829 #include <isl_multi_product_templ.c>
3830 #include <isl_multi_splice_templ.c>
3831 #include <isl_multi_zero_templ.c>
3833 /* Construct an isl_multi_aff living in "space" that corresponds
3834 * to the affine transformation matrix "mat".
3836 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3837 __isl_take isl_space *space, __isl_take isl_mat *mat)
3839 isl_ctx *ctx;
3840 isl_local_space *ls = NULL;
3841 isl_multi_aff *ma = NULL;
3842 isl_size n_row, n_col, n_out, total;
3843 int i;
3845 if (!space || !mat)
3846 goto error;
3848 ctx = isl_mat_get_ctx(mat);
3850 n_row = isl_mat_rows(mat);
3851 n_col = isl_mat_cols(mat);
3852 n_out = isl_space_dim(space, isl_dim_out);
3853 total = isl_space_dim(space, isl_dim_all);
3854 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3855 goto error;
3856 if (n_row < 1)
3857 isl_die(ctx, isl_error_invalid,
3858 "insufficient number of rows", goto error);
3859 if (n_col < 1)
3860 isl_die(ctx, isl_error_invalid,
3861 "insufficient number of columns", goto error);
3862 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3863 isl_die(ctx, isl_error_invalid,
3864 "dimension mismatch", goto error);
3866 ma = isl_multi_aff_zero(isl_space_copy(space));
3867 ls = isl_local_space_from_space(isl_space_domain(space));
3869 for (i = 0; i < n_row - 1; ++i) {
3870 isl_vec *v;
3871 isl_aff *aff;
3873 v = isl_vec_alloc(ctx, 1 + n_col);
3874 if (!v)
3875 goto error;
3876 isl_int_set(v->el[0], mat->row[0][0]);
3877 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3878 v = isl_vec_normalize(v);
3879 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3880 ma = isl_multi_aff_set_aff(ma, i, aff);
3883 isl_local_space_free(ls);
3884 isl_mat_free(mat);
3885 return ma;
3886 error:
3887 isl_local_space_free(ls);
3888 isl_mat_free(mat);
3889 isl_multi_aff_free(ma);
3890 return NULL;
3893 /* Remove any internal structure of the domain of "ma".
3894 * If there is any such internal structure in the input,
3895 * then the name of the corresponding space is also removed.
3897 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3898 __isl_take isl_multi_aff *ma)
3900 isl_space *space;
3902 if (!ma)
3903 return NULL;
3905 if (!ma->space->nested[0])
3906 return ma;
3908 space = isl_multi_aff_get_space(ma);
3909 space = isl_space_flatten_domain(space);
3910 ma = isl_multi_aff_reset_space(ma, space);
3912 return ma;
3915 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3916 * of the space to its domain.
3918 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3920 int i;
3921 isl_size n_in;
3922 isl_local_space *ls;
3923 isl_multi_aff *ma;
3925 if (!space)
3926 return NULL;
3927 if (!isl_space_is_map(space))
3928 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3929 "not a map space", goto error);
3931 n_in = isl_space_dim(space, isl_dim_in);
3932 if (n_in < 0)
3933 goto error;
3934 space = isl_space_domain_map(space);
3936 ma = isl_multi_aff_alloc(isl_space_copy(space));
3937 if (n_in == 0) {
3938 isl_space_free(space);
3939 return ma;
3942 space = isl_space_domain(space);
3943 ls = isl_local_space_from_space(space);
3944 for (i = 0; i < n_in; ++i) {
3945 isl_aff *aff;
3947 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3948 isl_dim_set, i);
3949 ma = isl_multi_aff_set_aff(ma, i, aff);
3951 isl_local_space_free(ls);
3952 return ma;
3953 error:
3954 isl_space_free(space);
3955 return NULL;
3958 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3959 * of the space to its range.
3961 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3963 int i;
3964 isl_size n_in, n_out;
3965 isl_local_space *ls;
3966 isl_multi_aff *ma;
3968 if (!space)
3969 return NULL;
3970 if (!isl_space_is_map(space))
3971 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3972 "not a map space", goto error);
3974 n_in = isl_space_dim(space, isl_dim_in);
3975 n_out = isl_space_dim(space, isl_dim_out);
3976 if (n_in < 0 || n_out < 0)
3977 goto error;
3978 space = isl_space_range_map(space);
3980 ma = isl_multi_aff_alloc(isl_space_copy(space));
3981 if (n_out == 0) {
3982 isl_space_free(space);
3983 return ma;
3986 space = isl_space_domain(space);
3987 ls = isl_local_space_from_space(space);
3988 for (i = 0; i < n_out; ++i) {
3989 isl_aff *aff;
3991 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3992 isl_dim_set, n_in + i);
3993 ma = isl_multi_aff_set_aff(ma, i, aff);
3995 isl_local_space_free(ls);
3996 return ma;
3997 error:
3998 isl_space_free(space);
3999 return NULL;
4002 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4003 * of the space to its range.
4005 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4006 __isl_take isl_space *space)
4008 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4011 /* Given the space of a set and a range of set dimensions,
4012 * construct an isl_multi_aff that projects out those dimensions.
4014 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4015 __isl_take isl_space *space, enum isl_dim_type type,
4016 unsigned first, unsigned n)
4018 int i;
4019 isl_size 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);
4031 if (isl_space_check_range(space, type, first, n) < 0)
4032 goto error;
4034 dim = isl_space_dim(space, isl_dim_set);
4035 if (dim < 0)
4036 goto error;
4038 space = isl_space_from_domain(space);
4039 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4041 if (dim == n)
4042 return isl_multi_aff_alloc(space);
4044 ma = isl_multi_aff_alloc(isl_space_copy(space));
4045 space = isl_space_domain(space);
4046 ls = isl_local_space_from_space(space);
4048 for (i = 0; i < first; ++i) {
4049 isl_aff *aff;
4051 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4052 isl_dim_set, i);
4053 ma = isl_multi_aff_set_aff(ma, i, aff);
4056 for (i = 0; i < dim - (first + n); ++i) {
4057 isl_aff *aff;
4059 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4060 isl_dim_set, first + n + i);
4061 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4064 isl_local_space_free(ls);
4065 return ma;
4066 error:
4067 isl_space_free(space);
4068 return NULL;
4071 /* Given the space of a set and a range of set dimensions,
4072 * construct an isl_pw_multi_aff that projects out those dimensions.
4074 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4075 __isl_take isl_space *space, enum isl_dim_type type,
4076 unsigned first, unsigned n)
4078 isl_multi_aff *ma;
4080 ma = isl_multi_aff_project_out_map(space, type, first, n);
4081 return isl_pw_multi_aff_from_multi_aff(ma);
4084 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4085 * domain.
4087 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4088 __isl_take isl_multi_aff *ma)
4090 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4091 return isl_pw_multi_aff_alloc(dom, ma);
4094 /* Create a piecewise multi-affine expression in the given space that maps each
4095 * input dimension to the corresponding output dimension.
4097 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4098 __isl_take isl_space *space)
4100 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4103 /* Exploit the equalities in "eq" to simplify the affine expressions.
4105 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4106 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4108 int i;
4110 maff = isl_multi_aff_cow(maff);
4111 if (!maff || !eq)
4112 goto error;
4114 for (i = 0; i < maff->n; ++i) {
4115 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4116 isl_basic_set_copy(eq));
4117 if (!maff->u.p[i])
4118 goto error;
4121 isl_basic_set_free(eq);
4122 return maff;
4123 error:
4124 isl_basic_set_free(eq);
4125 isl_multi_aff_free(maff);
4126 return NULL;
4129 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4130 isl_int f)
4132 int i;
4134 maff = isl_multi_aff_cow(maff);
4135 if (!maff)
4136 return NULL;
4138 for (i = 0; i < maff->n; ++i) {
4139 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4140 if (!maff->u.p[i])
4141 return isl_multi_aff_free(maff);
4144 return maff;
4147 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4148 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4150 maff1 = isl_multi_aff_add(maff1, maff2);
4151 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4152 return maff1;
4155 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4157 if (!maff)
4158 return -1;
4160 return 0;
4163 /* Return the set of domain elements where "ma1" is lexicographically
4164 * smaller than or equal to "ma2".
4166 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4167 __isl_take isl_multi_aff *ma2)
4169 return isl_multi_aff_lex_ge_set(ma2, ma1);
4172 /* Return the set of domain elements where "ma1" is lexicographically
4173 * smaller than "ma2".
4175 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4176 __isl_take isl_multi_aff *ma2)
4178 return isl_multi_aff_lex_gt_set(ma2, ma1);
4181 /* Return the set of domain elements where "ma1" and "ma2"
4182 * satisfy "order".
4184 static __isl_give isl_set *isl_multi_aff_order_set(
4185 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4186 __isl_give isl_map *order(__isl_take isl_space *set_space))
4188 isl_space *space;
4189 isl_map *map1, *map2;
4190 isl_map *map, *ge;
4192 map1 = isl_map_from_multi_aff_internal(ma1);
4193 map2 = isl_map_from_multi_aff_internal(ma2);
4194 map = isl_map_range_product(map1, map2);
4195 space = isl_space_range(isl_map_get_space(map));
4196 space = isl_space_domain(isl_space_unwrap(space));
4197 ge = order(space);
4198 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4200 return isl_map_domain(map);
4203 /* Return the set of domain elements where "ma1" is lexicographically
4204 * greater than or equal to "ma2".
4206 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4207 __isl_take isl_multi_aff *ma2)
4209 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4212 /* Return the set of domain elements where "ma1" is lexicographically
4213 * greater than "ma2".
4215 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4216 __isl_take isl_multi_aff *ma2)
4218 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4221 #undef PW
4222 #define PW isl_pw_multi_aff
4223 #undef EL
4224 #define EL isl_multi_aff
4225 #undef EL_IS_ZERO
4226 #define EL_IS_ZERO is_empty
4227 #undef ZERO
4228 #define ZERO empty
4229 #undef IS_ZERO
4230 #define IS_ZERO is_empty
4231 #undef FIELD
4232 #define FIELD maff
4233 #undef DEFAULT_IS_ZERO
4234 #define DEFAULT_IS_ZERO 0
4236 #define NO_SUB
4237 #define NO_OPT
4238 #define NO_INSERT_DIMS
4239 #define NO_LIFT
4240 #define NO_MORPH
4242 #include <isl_pw_templ.c>
4243 #include <isl_pw_union_opt.c>
4245 #undef NO_SUB
4247 #undef BASE
4248 #define BASE pw_multi_aff
4250 #include <isl_union_multi.c>
4251 #include <isl_union_neg.c>
4253 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4254 __isl_take isl_pw_multi_aff *pma1,
4255 __isl_take isl_pw_multi_aff *pma2)
4257 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4258 &isl_multi_aff_lex_ge_set);
4261 /* Given two piecewise multi affine expressions, return a piecewise
4262 * multi-affine expression defined on the union of the definition domains
4263 * of the inputs that is equal to the lexicographic maximum of the two
4264 * inputs on each cell. If only one of the two inputs is defined on
4265 * a given cell, then it is considered to be the maximum.
4267 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4268 __isl_take isl_pw_multi_aff *pma1,
4269 __isl_take isl_pw_multi_aff *pma2)
4271 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4272 &pw_multi_aff_union_lexmax);
4275 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4276 __isl_take isl_pw_multi_aff *pma1,
4277 __isl_take isl_pw_multi_aff *pma2)
4279 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4280 &isl_multi_aff_lex_le_set);
4283 /* Given two piecewise multi affine expressions, return a piecewise
4284 * multi-affine expression defined on the union of the definition domains
4285 * of the inputs that is equal to the lexicographic minimum of the two
4286 * inputs on each cell. If only one of the two inputs is defined on
4287 * a given cell, then it is considered to be the minimum.
4289 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4290 __isl_take isl_pw_multi_aff *pma1,
4291 __isl_take isl_pw_multi_aff *pma2)
4293 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4294 &pw_multi_aff_union_lexmin);
4297 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4298 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4300 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4301 &isl_multi_aff_add);
4304 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4305 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4307 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4308 &pw_multi_aff_add);
4311 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4312 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4314 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4315 &isl_multi_aff_sub);
4318 /* Subtract "pma2" from "pma1" and return the result.
4320 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4321 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4323 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4324 &pw_multi_aff_sub);
4327 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4328 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4330 return isl_pw_multi_aff_union_add_(pma1, pma2);
4333 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4334 * with the actual sum on the shared domain and
4335 * the defined expression on the symmetric difference of the domains.
4337 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4338 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4340 return isl_union_pw_aff_union_add_(upa1, upa2);
4343 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4344 * with the actual sum on the shared domain and
4345 * the defined expression on the symmetric difference of the domains.
4347 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4348 __isl_take isl_union_pw_multi_aff *upma1,
4349 __isl_take isl_union_pw_multi_aff *upma2)
4351 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4354 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4355 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4357 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4358 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4360 int i, j, n;
4361 isl_space *space;
4362 isl_pw_multi_aff *res;
4364 if (!pma1 || !pma2)
4365 goto error;
4367 n = pma1->n * pma2->n;
4368 space = isl_space_product(isl_space_copy(pma1->dim),
4369 isl_space_copy(pma2->dim));
4370 res = isl_pw_multi_aff_alloc_size(space, n);
4372 for (i = 0; i < pma1->n; ++i) {
4373 for (j = 0; j < pma2->n; ++j) {
4374 isl_set *domain;
4375 isl_multi_aff *ma;
4377 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4378 isl_set_copy(pma2->p[j].set));
4379 ma = isl_multi_aff_product(
4380 isl_multi_aff_copy(pma1->p[i].maff),
4381 isl_multi_aff_copy(pma2->p[j].maff));
4382 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4386 isl_pw_multi_aff_free(pma1);
4387 isl_pw_multi_aff_free(pma2);
4388 return res;
4389 error:
4390 isl_pw_multi_aff_free(pma1);
4391 isl_pw_multi_aff_free(pma2);
4392 return NULL;
4395 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4396 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4398 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4399 &pw_multi_aff_product);
4402 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4403 * denominator "denom".
4404 * "denom" is allowed to be negative, in which case the actual denominator
4405 * is -denom and the expressions are added instead.
4407 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4408 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4410 int i, first;
4411 int sign;
4412 isl_int d;
4414 first = isl_seq_first_non_zero(c, n);
4415 if (first == -1)
4416 return aff;
4418 sign = isl_int_sgn(denom);
4419 isl_int_init(d);
4420 isl_int_abs(d, denom);
4421 for (i = first; i < n; ++i) {
4422 isl_aff *aff_i;
4424 if (isl_int_is_zero(c[i]))
4425 continue;
4426 aff_i = isl_multi_aff_get_aff(ma, i);
4427 aff_i = isl_aff_scale(aff_i, c[i]);
4428 aff_i = isl_aff_scale_down(aff_i, d);
4429 if (sign >= 0)
4430 aff = isl_aff_sub(aff, aff_i);
4431 else
4432 aff = isl_aff_add(aff, aff_i);
4434 isl_int_clear(d);
4436 return aff;
4439 /* Extract an affine expression that expresses the output dimension "pos"
4440 * of "bmap" in terms of the parameters and input dimensions from
4441 * equality "eq".
4442 * Note that this expression may involve integer divisions defined
4443 * in terms of parameters and input dimensions.
4444 * The equality may also involve references to earlier (but not later)
4445 * output dimensions. These are replaced by the corresponding elements
4446 * in "ma".
4448 * If the equality is of the form
4450 * f(i) + h(j) + a x + g(i) = 0,
4452 * with f(i) a linear combinations of the parameters and input dimensions,
4453 * g(i) a linear combination of integer divisions defined in terms of the same
4454 * and h(j) a linear combinations of earlier output dimensions,
4455 * then the affine expression is
4457 * (-f(i) - g(i))/a - h(j)/a
4459 * If the equality is of the form
4461 * f(i) + h(j) - a x + g(i) = 0,
4463 * then the affine expression is
4465 * (f(i) + g(i))/a - h(j)/(-a)
4468 * If "div" refers to an integer division (i.e., it is smaller than
4469 * the number of integer divisions), then the equality constraint
4470 * does involve an integer division (the one at position "div") that
4471 * is defined in terms of output dimensions. However, this integer
4472 * division can be eliminated by exploiting a pair of constraints
4473 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4474 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4475 * -l + x >= 0.
4476 * In particular, let
4478 * x = e(i) + m floor(...)
4480 * with e(i) the expression derived above and floor(...) the integer
4481 * division involving output dimensions.
4482 * From
4484 * l <= x <= l + n,
4486 * we have
4488 * 0 <= x - l <= n
4490 * This means
4492 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4493 * = (e(i) - l) mod m
4495 * Therefore,
4497 * x - l = (e(i) - l) mod m
4499 * or
4501 * x = ((e(i) - l) mod m) + l
4503 * The variable "shift" below contains the expression -l, which may
4504 * also involve a linear combination of earlier output dimensions.
4506 static __isl_give isl_aff *extract_aff_from_equality(
4507 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4508 __isl_keep isl_multi_aff *ma)
4510 unsigned o_out;
4511 isl_size n_div, n_out;
4512 isl_ctx *ctx;
4513 isl_local_space *ls;
4514 isl_aff *aff, *shift;
4515 isl_val *mod;
4517 ctx = isl_basic_map_get_ctx(bmap);
4518 ls = isl_basic_map_get_local_space(bmap);
4519 ls = isl_local_space_domain(ls);
4520 aff = isl_aff_alloc(isl_local_space_copy(ls));
4521 if (!aff)
4522 goto error;
4523 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4524 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4525 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4526 if (n_out < 0 || n_div < 0)
4527 goto error;
4528 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4529 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4530 isl_seq_cpy(aff->v->el + 1 + o_out,
4531 bmap->eq[eq] + o_out + n_out, n_div);
4532 } else {
4533 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4534 isl_seq_neg(aff->v->el + 1 + o_out,
4535 bmap->eq[eq] + o_out + n_out, n_div);
4537 if (div < n_div)
4538 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4539 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4540 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4541 bmap->eq[eq][o_out + pos]);
4542 if (div < n_div) {
4543 shift = isl_aff_alloc(isl_local_space_copy(ls));
4544 if (!shift)
4545 goto error;
4546 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4547 isl_seq_cpy(shift->v->el + 1 + o_out,
4548 bmap->ineq[ineq] + o_out + n_out, n_div);
4549 isl_int_set_si(shift->v->el[0], 1);
4550 shift = subtract_initial(shift, ma, pos,
4551 bmap->ineq[ineq] + o_out, ctx->negone);
4552 aff = isl_aff_add(aff, isl_aff_copy(shift));
4553 mod = isl_val_int_from_isl_int(ctx,
4554 bmap->eq[eq][o_out + n_out + div]);
4555 mod = isl_val_abs(mod);
4556 aff = isl_aff_mod_val(aff, mod);
4557 aff = isl_aff_sub(aff, shift);
4560 isl_local_space_free(ls);
4561 return aff;
4562 error:
4563 isl_local_space_free(ls);
4564 isl_aff_free(aff);
4565 return NULL;
4568 /* Given a basic map with output dimensions defined
4569 * in terms of the parameters input dimensions and earlier
4570 * output dimensions using an equality (and possibly a pair on inequalities),
4571 * extract an isl_aff that expresses output dimension "pos" in terms
4572 * of the parameters and input dimensions.
4573 * Note that this expression may involve integer divisions defined
4574 * in terms of parameters and input dimensions.
4575 * "ma" contains the expressions corresponding to earlier output dimensions.
4577 * This function shares some similarities with
4578 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4580 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4581 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4583 int eq, div, ineq;
4584 isl_aff *aff;
4586 if (!bmap)
4587 return NULL;
4588 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4589 if (eq >= bmap->n_eq)
4590 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4591 "unable to find suitable equality", return NULL);
4592 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4594 aff = isl_aff_remove_unused_divs(aff);
4595 return aff;
4598 /* Given a basic map where each output dimension is defined
4599 * in terms of the parameters and input dimensions using an equality,
4600 * extract an isl_multi_aff that expresses the output dimensions in terms
4601 * of the parameters and input dimensions.
4603 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4604 __isl_take isl_basic_map *bmap)
4606 int i;
4607 isl_size n_out;
4608 isl_multi_aff *ma;
4610 if (!bmap)
4611 return NULL;
4613 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4614 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4615 if (n_out < 0)
4616 ma = isl_multi_aff_free(ma);
4618 for (i = 0; i < n_out; ++i) {
4619 isl_aff *aff;
4621 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4622 ma = isl_multi_aff_set_aff(ma, i, aff);
4625 isl_basic_map_free(bmap);
4627 return ma;
4630 /* Given a basic set where each set dimension is defined
4631 * in terms of the parameters using an equality,
4632 * extract an isl_multi_aff that expresses the set dimensions in terms
4633 * of the parameters.
4635 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4636 __isl_take isl_basic_set *bset)
4638 return extract_isl_multi_aff_from_basic_map(bset);
4641 /* Create an isl_pw_multi_aff that is equivalent to
4642 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4643 * The given basic map is such that each output dimension is defined
4644 * in terms of the parameters and input dimensions using an equality.
4646 * Since some applications expect the result of isl_pw_multi_aff_from_map
4647 * to only contain integer affine expressions, we compute the floor
4648 * of the expression before returning.
4650 * Remove all constraints involving local variables without
4651 * an explicit representation (resulting in the removal of those
4652 * local variables) prior to the actual extraction to ensure
4653 * that the local spaces in which the resulting affine expressions
4654 * are created do not contain any unknown local variables.
4655 * Removing such constraints is safe because constraints involving
4656 * unknown local variables are not used to determine whether
4657 * a basic map is obviously single-valued.
4659 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4660 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4662 isl_multi_aff *ma;
4664 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4665 ma = extract_isl_multi_aff_from_basic_map(bmap);
4666 ma = isl_multi_aff_floor(ma);
4667 return isl_pw_multi_aff_alloc(domain, ma);
4670 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4671 * This obviously only works if the input "map" is single-valued.
4672 * If so, we compute the lexicographic minimum of the image in the form
4673 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4674 * to its lexicographic minimum.
4675 * If the input is not single-valued, we produce an error.
4677 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4678 __isl_take isl_map *map)
4680 int i;
4681 int sv;
4682 isl_pw_multi_aff *pma;
4684 sv = isl_map_is_single_valued(map);
4685 if (sv < 0)
4686 goto error;
4687 if (!sv)
4688 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4689 "map is not single-valued", goto error);
4690 map = isl_map_make_disjoint(map);
4691 if (!map)
4692 return NULL;
4694 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4696 for (i = 0; i < map->n; ++i) {
4697 isl_pw_multi_aff *pma_i;
4698 isl_basic_map *bmap;
4699 bmap = isl_basic_map_copy(map->p[i]);
4700 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4701 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4704 isl_map_free(map);
4705 return pma;
4706 error:
4707 isl_map_free(map);
4708 return NULL;
4711 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4712 * taking into account that the output dimension at position "d"
4713 * can be represented as
4715 * x = floor((e(...) + c1) / m)
4717 * given that constraint "i" is of the form
4719 * e(...) + c1 - m x >= 0
4722 * Let "map" be of the form
4724 * A -> B
4726 * We construct a mapping
4728 * A -> [A -> x = floor(...)]
4730 * apply that to the map, obtaining
4732 * [A -> x = floor(...)] -> B
4734 * and equate dimension "d" to x.
4735 * We then compute a isl_pw_multi_aff representation of the resulting map
4736 * and plug in the mapping above.
4738 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4739 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4741 isl_ctx *ctx;
4742 isl_space *space = NULL;
4743 isl_local_space *ls;
4744 isl_multi_aff *ma;
4745 isl_aff *aff;
4746 isl_vec *v;
4747 isl_map *insert;
4748 int offset;
4749 isl_size n;
4750 isl_size n_in;
4751 isl_pw_multi_aff *pma;
4752 isl_bool is_set;
4754 is_set = isl_map_is_set(map);
4755 if (is_set < 0)
4756 goto error;
4758 offset = isl_basic_map_offset(hull, isl_dim_out);
4759 ctx = isl_map_get_ctx(map);
4760 space = isl_space_domain(isl_map_get_space(map));
4761 n_in = isl_space_dim(space, isl_dim_set);
4762 n = isl_space_dim(space, isl_dim_all);
4763 if (n_in < 0 || n < 0)
4764 goto error;
4766 v = isl_vec_alloc(ctx, 1 + 1 + n);
4767 if (v) {
4768 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4769 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4771 isl_basic_map_free(hull);
4773 ls = isl_local_space_from_space(isl_space_copy(space));
4774 aff = isl_aff_alloc_vec(ls, v);
4775 aff = isl_aff_floor(aff);
4776 if (is_set) {
4777 isl_space_free(space);
4778 ma = isl_multi_aff_from_aff(aff);
4779 } else {
4780 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4781 ma = isl_multi_aff_range_product(ma,
4782 isl_multi_aff_from_aff(aff));
4785 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4786 map = isl_map_apply_domain(map, insert);
4787 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4788 pma = isl_pw_multi_aff_from_map(map);
4789 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4791 return pma;
4792 error:
4793 isl_space_free(space);
4794 isl_map_free(map);
4795 isl_basic_map_free(hull);
4796 return NULL;
4799 /* Is constraint "c" of the form
4801 * e(...) + c1 - m x >= 0
4803 * or
4805 * -e(...) + c2 + m x >= 0
4807 * where m > 1 and e only depends on parameters and input dimemnsions?
4809 * "offset" is the offset of the output dimensions
4810 * "pos" is the position of output dimension x.
4812 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4814 if (isl_int_is_zero(c[offset + d]))
4815 return 0;
4816 if (isl_int_is_one(c[offset + d]))
4817 return 0;
4818 if (isl_int_is_negone(c[offset + d]))
4819 return 0;
4820 if (isl_seq_first_non_zero(c + offset, d) != -1)
4821 return 0;
4822 if (isl_seq_first_non_zero(c + offset + d + 1,
4823 total - (offset + d + 1)) != -1)
4824 return 0;
4825 return 1;
4828 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4830 * As a special case, we first check if there is any pair of constraints,
4831 * shared by all the basic maps in "map" that force a given dimension
4832 * to be equal to the floor of some affine combination of the input dimensions.
4834 * In particular, if we can find two constraints
4836 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4838 * and
4840 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4842 * where m > 1 and e only depends on parameters and input dimemnsions,
4843 * and such that
4845 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4847 * then we know that we can take
4849 * x = floor((e(...) + c1) / m)
4851 * without having to perform any computation.
4853 * Note that we know that
4855 * c1 + c2 >= 1
4857 * If c1 + c2 were 0, then we would have detected an equality during
4858 * simplification. If c1 + c2 were negative, then we would have detected
4859 * a contradiction.
4861 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4862 __isl_take isl_map *map)
4864 int d;
4865 isl_size dim;
4866 int i, j, n;
4867 int offset;
4868 isl_size total;
4869 isl_int sum;
4870 isl_basic_map *hull;
4872 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4873 dim = isl_map_dim(map, isl_dim_out);
4874 total = isl_basic_map_dim(hull, isl_dim_all);
4875 if (dim < 0 || total < 0)
4876 goto error;
4878 isl_int_init(sum);
4879 offset = isl_basic_map_offset(hull, isl_dim_out);
4880 n = hull->n_ineq;
4881 for (d = 0; d < dim; ++d) {
4882 for (i = 0; i < n; ++i) {
4883 if (!is_potential_div_constraint(hull->ineq[i],
4884 offset, d, 1 + total))
4885 continue;
4886 for (j = i + 1; j < n; ++j) {
4887 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4888 hull->ineq[j] + 1, total))
4889 continue;
4890 isl_int_add(sum, hull->ineq[i][0],
4891 hull->ineq[j][0]);
4892 if (isl_int_abs_lt(sum,
4893 hull->ineq[i][offset + d]))
4894 break;
4897 if (j >= n)
4898 continue;
4899 isl_int_clear(sum);
4900 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4901 j = i;
4902 return pw_multi_aff_from_map_div(map, hull, d, j);
4905 isl_int_clear(sum);
4906 isl_basic_map_free(hull);
4907 return pw_multi_aff_from_map_base(map);
4908 error:
4909 isl_map_free(map);
4910 isl_basic_map_free(hull);
4911 return NULL;
4914 /* Given an affine expression
4916 * [A -> B] -> f(A,B)
4918 * construct an isl_multi_aff
4920 * [A -> B] -> B'
4922 * such that dimension "d" in B' is set to "aff" and the remaining
4923 * dimensions are set equal to the corresponding dimensions in B.
4924 * "n_in" is the dimension of the space A.
4925 * "n_out" is the dimension of the space B.
4927 * If "is_set" is set, then the affine expression is of the form
4929 * [B] -> f(B)
4931 * and we construct an isl_multi_aff
4933 * B -> B'
4935 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4936 unsigned n_in, unsigned n_out, int is_set)
4938 int i;
4939 isl_multi_aff *ma;
4940 isl_space *space, *space2;
4941 isl_local_space *ls;
4943 space = isl_aff_get_domain_space(aff);
4944 ls = isl_local_space_from_space(isl_space_copy(space));
4945 space2 = isl_space_copy(space);
4946 if (!is_set)
4947 space2 = isl_space_range(isl_space_unwrap(space2));
4948 space = isl_space_map_from_domain_and_range(space, space2);
4949 ma = isl_multi_aff_alloc(space);
4950 ma = isl_multi_aff_set_aff(ma, d, aff);
4952 for (i = 0; i < n_out; ++i) {
4953 if (i == d)
4954 continue;
4955 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4956 isl_dim_set, n_in + i);
4957 ma = isl_multi_aff_set_aff(ma, i, aff);
4960 isl_local_space_free(ls);
4962 return ma;
4965 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4966 * taking into account that the dimension at position "d" can be written as
4968 * x = m a + f(..) (1)
4970 * where m is equal to "gcd".
4971 * "i" is the index of the equality in "hull" that defines f(..).
4972 * In particular, the equality is of the form
4974 * f(..) - x + m g(existentials) = 0
4976 * or
4978 * -f(..) + x + m g(existentials) = 0
4980 * We basically plug (1) into "map", resulting in a map with "a"
4981 * in the range instead of "x". The corresponding isl_pw_multi_aff
4982 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4984 * Specifically, given the input map
4986 * A -> B
4988 * We first wrap it into a set
4990 * [A -> B]
4992 * and define (1) on top of the corresponding space, resulting in "aff".
4993 * We use this to create an isl_multi_aff that maps the output position "d"
4994 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
4995 * We plug this into the wrapped map, unwrap the result and compute the
4996 * corresponding isl_pw_multi_aff.
4997 * The result is an expression
4999 * A -> T(A)
5001 * We adjust that to
5003 * A -> [A -> T(A)]
5005 * so that we can plug that into "aff", after extending the latter to
5006 * a mapping
5008 * [A -> B] -> B'
5011 * If "map" is actually a set, then there is no "A" space, meaning
5012 * that we do not need to perform any wrapping, and that the result
5013 * of the recursive call is of the form
5015 * [T]
5017 * which is plugged into a mapping of the form
5019 * B -> B'
5021 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5022 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5023 isl_int gcd)
5025 isl_set *set;
5026 isl_space *space;
5027 isl_local_space *ls;
5028 isl_aff *aff;
5029 isl_multi_aff *ma;
5030 isl_pw_multi_aff *pma, *id;
5031 isl_size n_in;
5032 unsigned o_out;
5033 isl_size n_out;
5034 isl_bool is_set;
5036 is_set = isl_map_is_set(map);
5037 if (is_set < 0)
5038 goto error;
5040 n_in = isl_basic_map_dim(hull, isl_dim_in);
5041 n_out = isl_basic_map_dim(hull, isl_dim_out);
5042 if (n_in < 0 || n_out < 0)
5043 goto error;
5044 o_out = isl_basic_map_offset(hull, isl_dim_out);
5046 if (is_set)
5047 set = map;
5048 else
5049 set = isl_map_wrap(map);
5050 space = isl_space_map_from_set(isl_set_get_space(set));
5051 ma = isl_multi_aff_identity(space);
5052 ls = isl_local_space_from_space(isl_set_get_space(set));
5053 aff = isl_aff_alloc(ls);
5054 if (aff) {
5055 isl_int_set_si(aff->v->el[0], 1);
5056 if (isl_int_is_one(hull->eq[i][o_out + d]))
5057 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5058 aff->v->size - 1);
5059 else
5060 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5061 aff->v->size - 1);
5062 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5064 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5065 set = isl_set_preimage_multi_aff(set, ma);
5067 ma = range_map(aff, d, n_in, n_out, is_set);
5069 if (is_set)
5070 map = set;
5071 else
5072 map = isl_set_unwrap(set);
5073 pma = isl_pw_multi_aff_from_map(map);
5075 if (!is_set) {
5076 space = isl_pw_multi_aff_get_domain_space(pma);
5077 space = isl_space_map_from_set(space);
5078 id = isl_pw_multi_aff_identity(space);
5079 pma = isl_pw_multi_aff_range_product(id, pma);
5081 id = isl_pw_multi_aff_from_multi_aff(ma);
5082 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5084 isl_basic_map_free(hull);
5085 return pma;
5086 error:
5087 isl_map_free(map);
5088 isl_basic_map_free(hull);
5089 return NULL;
5092 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5093 * "hull" contains the equalities valid for "map".
5095 * Check if any of the output dimensions is "strided".
5096 * That is, we check if it can be written as
5098 * x = m a + f(..)
5100 * with m greater than 1, a some combination of existentially quantified
5101 * variables and f an expression in the parameters and input dimensions.
5102 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5104 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5105 * special case.
5107 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5108 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5110 int i, j;
5111 isl_size n_out;
5112 unsigned o_out;
5113 isl_size n_div;
5114 unsigned o_div;
5115 isl_int gcd;
5117 n_div = isl_basic_map_dim(hull, isl_dim_div);
5118 n_out = isl_basic_map_dim(hull, isl_dim_out);
5119 if (n_div < 0 || n_out < 0)
5120 goto error;
5122 if (n_div == 0) {
5123 isl_basic_map_free(hull);
5124 return pw_multi_aff_from_map_check_div(map);
5127 isl_int_init(gcd);
5129 o_div = isl_basic_map_offset(hull, isl_dim_div);
5130 o_out = isl_basic_map_offset(hull, isl_dim_out);
5132 for (i = 0; i < n_out; ++i) {
5133 for (j = 0; j < hull->n_eq; ++j) {
5134 isl_int *eq = hull->eq[j];
5135 isl_pw_multi_aff *res;
5137 if (!isl_int_is_one(eq[o_out + i]) &&
5138 !isl_int_is_negone(eq[o_out + i]))
5139 continue;
5140 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5141 continue;
5142 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5143 n_out - (i + 1)) != -1)
5144 continue;
5145 isl_seq_gcd(eq + o_div, n_div, &gcd);
5146 if (isl_int_is_zero(gcd))
5147 continue;
5148 if (isl_int_is_one(gcd))
5149 continue;
5151 res = pw_multi_aff_from_map_stride(map, hull,
5152 i, j, gcd);
5153 isl_int_clear(gcd);
5154 return res;
5158 isl_int_clear(gcd);
5159 isl_basic_map_free(hull);
5160 return pw_multi_aff_from_map_check_div(map);
5161 error:
5162 isl_map_free(map);
5163 isl_basic_map_free(hull);
5164 return NULL;
5167 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5169 * As a special case, we first check if all output dimensions are uniquely
5170 * defined in terms of the parameters and input dimensions over the entire
5171 * domain. If so, we extract the desired isl_pw_multi_aff directly
5172 * from the affine hull of "map" and its domain.
5174 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5175 * special cases.
5177 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5179 isl_bool sv;
5180 isl_size n;
5181 isl_basic_map *hull;
5183 n = isl_map_n_basic_map(map);
5184 if (n < 0)
5185 goto error;
5187 if (n == 1) {
5188 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5189 hull = isl_basic_map_plain_affine_hull(hull);
5190 sv = isl_basic_map_plain_is_single_valued(hull);
5191 if (sv >= 0 && sv)
5192 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5193 hull);
5194 isl_basic_map_free(hull);
5196 map = isl_map_detect_equalities(map);
5197 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5198 sv = isl_basic_map_plain_is_single_valued(hull);
5199 if (sv >= 0 && sv)
5200 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5201 if (sv >= 0)
5202 return pw_multi_aff_from_map_check_strides(map, hull);
5203 isl_basic_map_free(hull);
5204 error:
5205 isl_map_free(map);
5206 return NULL;
5209 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5211 return isl_pw_multi_aff_from_map(set);
5214 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5215 * add it to *user.
5217 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5219 isl_union_pw_multi_aff **upma = user;
5220 isl_pw_multi_aff *pma;
5222 pma = isl_pw_multi_aff_from_map(map);
5223 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5225 return *upma ? isl_stat_ok : isl_stat_error;
5228 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5229 * domain.
5231 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5232 __isl_take isl_aff *aff)
5234 isl_multi_aff *ma;
5235 isl_pw_multi_aff *pma;
5237 ma = isl_multi_aff_from_aff(aff);
5238 pma = isl_pw_multi_aff_from_multi_aff(ma);
5239 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5242 /* Try and create an isl_union_pw_multi_aff that is equivalent
5243 * to the given isl_union_map.
5244 * The isl_union_map is required to be single-valued in each space.
5245 * Otherwise, an error is produced.
5247 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5248 __isl_take isl_union_map *umap)
5250 isl_space *space;
5251 isl_union_pw_multi_aff *upma;
5253 space = isl_union_map_get_space(umap);
5254 upma = isl_union_pw_multi_aff_empty(space);
5255 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5256 upma = isl_union_pw_multi_aff_free(upma);
5257 isl_union_map_free(umap);
5259 return upma;
5262 /* Try and create an isl_union_pw_multi_aff that is equivalent
5263 * to the given isl_union_set.
5264 * The isl_union_set is required to be a singleton in each space.
5265 * Otherwise, an error is produced.
5267 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5268 __isl_take isl_union_set *uset)
5270 return isl_union_pw_multi_aff_from_union_map(uset);
5273 /* Return the piecewise affine expression "set ? 1 : 0".
5275 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5277 isl_pw_aff *pa;
5278 isl_space *space = isl_set_get_space(set);
5279 isl_local_space *ls = isl_local_space_from_space(space);
5280 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5281 isl_aff *one = isl_aff_zero_on_domain(ls);
5283 one = isl_aff_add_constant_si(one, 1);
5284 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5285 set = isl_set_complement(set);
5286 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5288 return pa;
5291 /* Plug in "subs" for dimension "type", "pos" of "aff".
5293 * Let i be the dimension to replace and let "subs" be of the form
5295 * f/d
5297 * and "aff" of the form
5299 * (a i + g)/m
5301 * The result is
5303 * (a f + d g')/(m d)
5305 * where g' is the result of plugging in "subs" in each of the integer
5306 * divisions in g.
5308 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5309 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5311 isl_ctx *ctx;
5312 isl_int v;
5313 isl_size n_div;
5315 aff = isl_aff_cow(aff);
5316 if (!aff || !subs)
5317 return isl_aff_free(aff);
5319 ctx = isl_aff_get_ctx(aff);
5320 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5321 isl_die(ctx, isl_error_invalid,
5322 "spaces don't match", return isl_aff_free(aff));
5323 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
5324 if (n_div < 0)
5325 return isl_aff_free(aff);
5326 if (n_div != 0)
5327 isl_die(ctx, isl_error_unsupported,
5328 "cannot handle divs yet", return isl_aff_free(aff));
5330 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5331 if (!aff->ls)
5332 return isl_aff_free(aff);
5334 aff->v = isl_vec_cow(aff->v);
5335 if (!aff->v)
5336 return isl_aff_free(aff);
5338 pos += isl_local_space_offset(aff->ls, type);
5340 isl_int_init(v);
5341 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5342 aff->v->size, subs->v->size, v);
5343 isl_int_clear(v);
5345 return aff;
5348 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5349 * expressions in "maff".
5351 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5352 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5353 __isl_keep isl_aff *subs)
5355 int i;
5357 maff = isl_multi_aff_cow(maff);
5358 if (!maff || !subs)
5359 return isl_multi_aff_free(maff);
5361 if (type == isl_dim_in)
5362 type = isl_dim_set;
5364 for (i = 0; i < maff->n; ++i) {
5365 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5366 type, pos, subs);
5367 if (!maff->u.p[i])
5368 return isl_multi_aff_free(maff);
5371 return maff;
5374 /* Plug in "subs" for dimension "type", "pos" of "pma".
5376 * pma is of the form
5378 * A_i(v) -> M_i(v)
5380 * while subs is of the form
5382 * v' = B_j(v) -> S_j
5384 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5385 * has a contribution in the result, in particular
5387 * C_ij(S_j) -> M_i(S_j)
5389 * Note that plugging in S_j in C_ij may also result in an empty set
5390 * and this contribution should simply be discarded.
5392 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5393 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5394 __isl_keep isl_pw_aff *subs)
5396 int i, j, n;
5397 isl_pw_multi_aff *res;
5399 if (!pma || !subs)
5400 return isl_pw_multi_aff_free(pma);
5402 n = pma->n * subs->n;
5403 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5405 for (i = 0; i < pma->n; ++i) {
5406 for (j = 0; j < subs->n; ++j) {
5407 isl_set *common;
5408 isl_multi_aff *res_ij;
5409 int empty;
5411 common = isl_set_intersect(
5412 isl_set_copy(pma->p[i].set),
5413 isl_set_copy(subs->p[j].set));
5414 common = isl_set_substitute(common,
5415 type, pos, subs->p[j].aff);
5416 empty = isl_set_plain_is_empty(common);
5417 if (empty < 0 || empty) {
5418 isl_set_free(common);
5419 if (empty < 0)
5420 goto error;
5421 continue;
5424 res_ij = isl_multi_aff_substitute(
5425 isl_multi_aff_copy(pma->p[i].maff),
5426 type, pos, subs->p[j].aff);
5428 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5432 isl_pw_multi_aff_free(pma);
5433 return res;
5434 error:
5435 isl_pw_multi_aff_free(pma);
5436 isl_pw_multi_aff_free(res);
5437 return NULL;
5440 /* Compute the preimage of a range of dimensions in the affine expression "src"
5441 * under "ma" and put the result in "dst". The number of dimensions in "src"
5442 * that precede the range is given by "n_before". The number of dimensions
5443 * in the range is given by the number of output dimensions of "ma".
5444 * The number of dimensions that follow the range is given by "n_after".
5445 * If "has_denom" is set (to one),
5446 * then "src" and "dst" have an extra initial denominator.
5447 * "n_div_ma" is the number of existentials in "ma"
5448 * "n_div_bset" is the number of existentials in "src"
5449 * The resulting "dst" (which is assumed to have been allocated by
5450 * the caller) contains coefficients for both sets of existentials,
5451 * first those in "ma" and then those in "src".
5452 * f, c1, c2 and g are temporary objects that have been initialized
5453 * by the caller.
5455 * Let src represent the expression
5457 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5459 * and let ma represent the expressions
5461 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5463 * We start out with the following expression for dst:
5465 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5467 * with the multiplication factor f initially equal to 1
5468 * and f \sum_i b_i v_i kept separately.
5469 * For each x_i that we substitute, we multiply the numerator
5470 * (and denominator) of dst by c_1 = m_i and add the numerator
5471 * of the x_i expression multiplied by c_2 = f b_i,
5472 * after removing the common factors of c_1 and c_2.
5473 * The multiplication factor f also needs to be multiplied by c_1
5474 * for the next x_j, j > i.
5476 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5477 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5478 int n_div_ma, int n_div_bmap,
5479 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5481 int i;
5482 isl_size n_param, n_in, n_out;
5483 int o_dst, o_src;
5485 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5486 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5487 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5488 if (n_param < 0 || n_in < 0 || n_out < 0)
5489 return isl_stat_error;
5491 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5492 o_dst = o_src = has_denom + 1 + n_param + n_before;
5493 isl_seq_clr(dst + o_dst, n_in);
5494 o_dst += n_in;
5495 o_src += n_out;
5496 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5497 o_dst += n_after;
5498 o_src += n_after;
5499 isl_seq_clr(dst + o_dst, n_div_ma);
5500 o_dst += n_div_ma;
5501 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5503 isl_int_set_si(f, 1);
5505 for (i = 0; i < n_out; ++i) {
5506 int offset = has_denom + 1 + n_param + n_before + i;
5508 if (isl_int_is_zero(src[offset]))
5509 continue;
5510 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5511 isl_int_mul(c2, f, src[offset]);
5512 isl_int_gcd(g, c1, c2);
5513 isl_int_divexact(c1, c1, g);
5514 isl_int_divexact(c2, c2, g);
5516 isl_int_mul(f, f, c1);
5517 o_dst = has_denom;
5518 o_src = 1;
5519 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5520 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5521 o_dst += 1 + n_param;
5522 o_src += 1 + n_param;
5523 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5524 o_dst += n_before;
5525 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5526 c2, ma->u.p[i]->v->el + o_src, n_in);
5527 o_dst += n_in;
5528 o_src += n_in;
5529 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5530 o_dst += n_after;
5531 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5532 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5533 o_dst += n_div_ma;
5534 o_src += n_div_ma;
5535 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5536 if (has_denom)
5537 isl_int_mul(dst[0], dst[0], c1);
5540 return isl_stat_ok;
5543 /* Compute the pullback of "aff" by the function represented by "ma".
5544 * In other words, plug in "ma" in "aff". The result is an affine expression
5545 * defined over the domain space of "ma".
5547 * If "aff" is represented by
5549 * (a(p) + b x + c(divs))/d
5551 * and ma is represented by
5553 * x = D(p) + F(y) + G(divs')
5555 * then the result is
5557 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5559 * The divs in the local space of the input are similarly adjusted
5560 * through a call to isl_local_space_preimage_multi_aff.
5562 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5563 __isl_take isl_multi_aff *ma)
5565 isl_aff *res = NULL;
5566 isl_local_space *ls;
5567 isl_size n_div_aff, n_div_ma;
5568 isl_int f, c1, c2, g;
5570 ma = isl_multi_aff_align_divs(ma);
5571 if (!aff || !ma)
5572 goto error;
5574 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5575 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5576 if (n_div_aff < 0 || n_div_ma < 0)
5577 goto error;
5579 ls = isl_aff_get_domain_local_space(aff);
5580 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5581 res = isl_aff_alloc(ls);
5582 if (!res)
5583 goto error;
5585 isl_int_init(f);
5586 isl_int_init(c1);
5587 isl_int_init(c2);
5588 isl_int_init(g);
5590 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5591 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5592 res = isl_aff_free(res);
5594 isl_int_clear(f);
5595 isl_int_clear(c1);
5596 isl_int_clear(c2);
5597 isl_int_clear(g);
5599 isl_aff_free(aff);
5600 isl_multi_aff_free(ma);
5601 res = isl_aff_normalize(res);
5602 return res;
5603 error:
5604 isl_aff_free(aff);
5605 isl_multi_aff_free(ma);
5606 isl_aff_free(res);
5607 return NULL;
5610 /* Compute the pullback of "aff1" by the function represented by "aff2".
5611 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5612 * defined over the domain space of "aff1".
5614 * The domain of "aff1" should match the range of "aff2", which means
5615 * that it should be single-dimensional.
5617 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5618 __isl_take isl_aff *aff2)
5620 isl_multi_aff *ma;
5622 ma = isl_multi_aff_from_aff(aff2);
5623 return isl_aff_pullback_multi_aff(aff1, ma);
5626 /* Compute the pullback of "ma1" by the function represented by "ma2".
5627 * In other words, plug in "ma2" in "ma1".
5629 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5631 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5632 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5634 int i;
5635 isl_space *space = NULL;
5637 ma2 = isl_multi_aff_align_divs(ma2);
5638 ma1 = isl_multi_aff_cow(ma1);
5639 if (!ma1 || !ma2)
5640 goto error;
5642 space = isl_space_join(isl_multi_aff_get_space(ma2),
5643 isl_multi_aff_get_space(ma1));
5645 for (i = 0; i < ma1->n; ++i) {
5646 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5647 isl_multi_aff_copy(ma2));
5648 if (!ma1->u.p[i])
5649 goto error;
5652 ma1 = isl_multi_aff_reset_space(ma1, space);
5653 isl_multi_aff_free(ma2);
5654 return ma1;
5655 error:
5656 isl_space_free(space);
5657 isl_multi_aff_free(ma2);
5658 isl_multi_aff_free(ma1);
5659 return NULL;
5662 /* Compute the pullback of "ma1" by the function represented by "ma2".
5663 * In other words, plug in "ma2" in "ma1".
5665 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5666 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5668 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5669 &isl_multi_aff_pullback_multi_aff_aligned);
5672 /* Extend the local space of "dst" to include the divs
5673 * in the local space of "src".
5675 * If "src" does not have any divs or if the local spaces of "dst" and
5676 * "src" are the same, then no extension is required.
5678 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5679 __isl_keep isl_aff *src)
5681 isl_ctx *ctx;
5682 isl_size src_n_div, dst_n_div;
5683 int *exp1 = NULL;
5684 int *exp2 = NULL;
5685 isl_bool equal;
5686 isl_mat *div;
5688 if (!src || !dst)
5689 return isl_aff_free(dst);
5691 ctx = isl_aff_get_ctx(src);
5692 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5693 if (equal < 0)
5694 return isl_aff_free(dst);
5695 if (!equal)
5696 isl_die(ctx, isl_error_invalid,
5697 "spaces don't match", goto error);
5699 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5700 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5701 if (src_n_div == 0)
5702 return dst;
5703 equal = isl_local_space_is_equal(src->ls, dst->ls);
5704 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5705 return isl_aff_free(dst);
5706 if (equal)
5707 return dst;
5709 exp1 = isl_alloc_array(ctx, int, src_n_div);
5710 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5711 if (!exp1 || (dst_n_div && !exp2))
5712 goto error;
5714 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5715 dst = isl_aff_expand_divs(dst, div, exp2);
5716 free(exp1);
5717 free(exp2);
5719 return dst;
5720 error:
5721 free(exp1);
5722 free(exp2);
5723 return isl_aff_free(dst);
5726 /* Adjust the local spaces of the affine expressions in "maff"
5727 * such that they all have the save divs.
5729 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5730 __isl_take isl_multi_aff *maff)
5732 int i;
5734 if (!maff)
5735 return NULL;
5736 if (maff->n == 0)
5737 return maff;
5738 maff = isl_multi_aff_cow(maff);
5739 if (!maff)
5740 return NULL;
5742 for (i = 1; i < maff->n; ++i)
5743 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5744 for (i = 1; i < maff->n; ++i) {
5745 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5746 if (!maff->u.p[i])
5747 return isl_multi_aff_free(maff);
5750 return maff;
5753 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5755 aff = isl_aff_cow(aff);
5756 if (!aff)
5757 return NULL;
5759 aff->ls = isl_local_space_lift(aff->ls);
5760 if (!aff->ls)
5761 return isl_aff_free(aff);
5763 return aff;
5766 /* Lift "maff" to a space with extra dimensions such that the result
5767 * has no more existentially quantified variables.
5768 * If "ls" is not NULL, then *ls is assigned the local space that lies
5769 * at the basis of the lifting applied to "maff".
5771 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5772 __isl_give isl_local_space **ls)
5774 int i;
5775 isl_space *space;
5776 isl_size n_div;
5778 if (ls)
5779 *ls = NULL;
5781 if (!maff)
5782 return NULL;
5784 if (maff->n == 0) {
5785 if (ls) {
5786 isl_space *space = isl_multi_aff_get_domain_space(maff);
5787 *ls = isl_local_space_from_space(space);
5788 if (!*ls)
5789 return isl_multi_aff_free(maff);
5791 return maff;
5794 maff = isl_multi_aff_cow(maff);
5795 maff = isl_multi_aff_align_divs(maff);
5796 if (!maff)
5797 return NULL;
5799 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5800 if (n_div < 0)
5801 return isl_multi_aff_free(maff);
5802 space = isl_multi_aff_get_space(maff);
5803 space = isl_space_lift(isl_space_domain(space), n_div);
5804 space = isl_space_extend_domain_with_range(space,
5805 isl_multi_aff_get_space(maff));
5806 if (!space)
5807 return isl_multi_aff_free(maff);
5808 isl_space_free(maff->space);
5809 maff->space = space;
5811 if (ls) {
5812 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5813 if (!*ls)
5814 return isl_multi_aff_free(maff);
5817 for (i = 0; i < maff->n; ++i) {
5818 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5819 if (!maff->u.p[i])
5820 goto error;
5823 return maff;
5824 error:
5825 if (ls)
5826 isl_local_space_free(*ls);
5827 return isl_multi_aff_free(maff);
5830 #undef TYPE
5831 #define TYPE isl_pw_multi_aff
5832 static
5833 #include "check_type_range_templ.c"
5835 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5837 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5838 __isl_keep isl_pw_multi_aff *pma, int pos)
5840 int i;
5841 isl_size n_out;
5842 isl_space *space;
5843 isl_pw_aff *pa;
5845 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
5846 return NULL;
5848 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5849 if (n_out < 0)
5850 return NULL;
5852 space = isl_pw_multi_aff_get_space(pma);
5853 space = isl_space_drop_dims(space, isl_dim_out,
5854 pos + 1, n_out - pos - 1);
5855 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5857 pa = isl_pw_aff_alloc_size(space, pma->n);
5858 for (i = 0; i < pma->n; ++i) {
5859 isl_aff *aff;
5860 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5861 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5864 return pa;
5867 /* Return an isl_pw_multi_aff with the given "set" as domain and
5868 * an unnamed zero-dimensional range.
5870 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5871 __isl_take isl_set *set)
5873 isl_multi_aff *ma;
5874 isl_space *space;
5876 space = isl_set_get_space(set);
5877 space = isl_space_from_domain(space);
5878 ma = isl_multi_aff_zero(space);
5879 return isl_pw_multi_aff_alloc(set, ma);
5882 /* Add an isl_pw_multi_aff with the given "set" as domain and
5883 * an unnamed zero-dimensional range to *user.
5885 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5886 void *user)
5888 isl_union_pw_multi_aff **upma = user;
5889 isl_pw_multi_aff *pma;
5891 pma = isl_pw_multi_aff_from_domain(set);
5892 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5894 return isl_stat_ok;
5897 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5898 * an unnamed zero-dimensional range.
5900 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5901 __isl_take isl_union_set *uset)
5903 isl_space *space;
5904 isl_union_pw_multi_aff *upma;
5906 if (!uset)
5907 return NULL;
5909 space = isl_union_set_get_space(uset);
5910 upma = isl_union_pw_multi_aff_empty(space);
5912 if (isl_union_set_foreach_set(uset,
5913 &add_pw_multi_aff_from_domain, &upma) < 0)
5914 goto error;
5916 isl_union_set_free(uset);
5917 return upma;
5918 error:
5919 isl_union_set_free(uset);
5920 isl_union_pw_multi_aff_free(upma);
5921 return NULL;
5924 /* Local data for bin_entry and the callback "fn".
5926 struct isl_union_pw_multi_aff_bin_data {
5927 isl_union_pw_multi_aff *upma2;
5928 isl_union_pw_multi_aff *res;
5929 isl_pw_multi_aff *pma;
5930 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5933 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5934 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5936 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5938 struct isl_union_pw_multi_aff_bin_data *data = user;
5939 isl_stat r;
5941 data->pma = pma;
5942 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5943 data->fn, data);
5944 isl_pw_multi_aff_free(pma);
5946 return r;
5949 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5950 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5951 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5952 * as *entry. The callback should adjust data->res if desired.
5954 static __isl_give isl_union_pw_multi_aff *bin_op(
5955 __isl_take isl_union_pw_multi_aff *upma1,
5956 __isl_take isl_union_pw_multi_aff *upma2,
5957 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5959 isl_space *space;
5960 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5962 space = isl_union_pw_multi_aff_get_space(upma2);
5963 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5964 space = isl_union_pw_multi_aff_get_space(upma1);
5965 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5967 if (!upma1 || !upma2)
5968 goto error;
5970 data.upma2 = upma2;
5971 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5972 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5973 &bin_entry, &data) < 0)
5974 goto error;
5976 isl_union_pw_multi_aff_free(upma1);
5977 isl_union_pw_multi_aff_free(upma2);
5978 return data.res;
5979 error:
5980 isl_union_pw_multi_aff_free(upma1);
5981 isl_union_pw_multi_aff_free(upma2);
5982 isl_union_pw_multi_aff_free(data.res);
5983 return NULL;
5986 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5987 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5989 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5990 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5992 isl_space *space;
5994 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
5995 isl_pw_multi_aff_get_space(pma2));
5996 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
5997 &isl_multi_aff_range_product);
6000 /* Given two isl_pw_multi_affs A -> B and C -> D,
6001 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6003 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6004 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6006 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6007 &pw_multi_aff_range_product);
6010 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6011 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6013 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6014 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6016 isl_space *space;
6018 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6019 isl_pw_multi_aff_get_space(pma2));
6020 space = isl_space_flatten_range(space);
6021 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6022 &isl_multi_aff_flat_range_product);
6025 /* Given two isl_pw_multi_affs A -> B and C -> D,
6026 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6028 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6029 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6031 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6032 &pw_multi_aff_flat_range_product);
6035 /* If data->pma and "pma2" have the same domain space, then compute
6036 * their flat range product and the result to data->res.
6038 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6039 void *user)
6041 struct isl_union_pw_multi_aff_bin_data *data = user;
6043 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6044 pma2->dim, isl_dim_in)) {
6045 isl_pw_multi_aff_free(pma2);
6046 return isl_stat_ok;
6049 pma2 = isl_pw_multi_aff_flat_range_product(
6050 isl_pw_multi_aff_copy(data->pma), pma2);
6052 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6054 return isl_stat_ok;
6057 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6058 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6060 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6061 __isl_take isl_union_pw_multi_aff *upma1,
6062 __isl_take isl_union_pw_multi_aff *upma2)
6064 return bin_op(upma1, upma2, &flat_range_product_entry);
6067 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6068 * The parameters are assumed to have been aligned.
6070 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6071 * except that it works on two different isl_pw_* types.
6073 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6074 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6075 __isl_take isl_pw_aff *pa)
6077 int i, j, n;
6078 isl_pw_multi_aff *res = NULL;
6080 if (!pma || !pa)
6081 goto error;
6083 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6084 pa->dim, isl_dim_in))
6085 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6086 "domains don't match", goto error);
6087 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6088 goto error;
6090 n = pma->n * pa->n;
6091 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6093 for (i = 0; i < pma->n; ++i) {
6094 for (j = 0; j < pa->n; ++j) {
6095 isl_set *common;
6096 isl_multi_aff *res_ij;
6097 int empty;
6099 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6100 isl_set_copy(pa->p[j].set));
6101 empty = isl_set_plain_is_empty(common);
6102 if (empty < 0 || empty) {
6103 isl_set_free(common);
6104 if (empty < 0)
6105 goto error;
6106 continue;
6109 res_ij = isl_multi_aff_set_aff(
6110 isl_multi_aff_copy(pma->p[i].maff), pos,
6111 isl_aff_copy(pa->p[j].aff));
6112 res_ij = isl_multi_aff_gist(res_ij,
6113 isl_set_copy(common));
6115 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6119 isl_pw_multi_aff_free(pma);
6120 isl_pw_aff_free(pa);
6121 return res;
6122 error:
6123 isl_pw_multi_aff_free(pma);
6124 isl_pw_aff_free(pa);
6125 return isl_pw_multi_aff_free(res);
6128 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6130 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6131 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6132 __isl_take isl_pw_aff *pa)
6134 isl_bool equal_params;
6136 if (!pma || !pa)
6137 goto error;
6138 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6139 if (equal_params < 0)
6140 goto error;
6141 if (equal_params)
6142 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6143 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6144 isl_pw_aff_check_named_params(pa) < 0)
6145 goto error;
6146 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6147 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6148 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6149 error:
6150 isl_pw_multi_aff_free(pma);
6151 isl_pw_aff_free(pa);
6152 return NULL;
6155 /* Do the parameters of "pa" match those of "space"?
6157 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6158 __isl_keep isl_space *space)
6160 isl_space *pa_space;
6161 isl_bool match;
6163 if (!pa || !space)
6164 return isl_bool_error;
6166 pa_space = isl_pw_aff_get_space(pa);
6168 match = isl_space_has_equal_params(space, pa_space);
6170 isl_space_free(pa_space);
6171 return match;
6174 /* Check that the domain space of "pa" matches "space".
6176 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6177 __isl_keep isl_space *space)
6179 isl_space *pa_space;
6180 isl_bool match;
6182 if (!pa || !space)
6183 return isl_stat_error;
6185 pa_space = isl_pw_aff_get_space(pa);
6187 match = isl_space_has_equal_params(space, pa_space);
6188 if (match < 0)
6189 goto error;
6190 if (!match)
6191 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6192 "parameters don't match", goto error);
6193 match = isl_space_tuple_is_equal(space, isl_dim_in,
6194 pa_space, isl_dim_in);
6195 if (match < 0)
6196 goto error;
6197 if (!match)
6198 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6199 "domains don't match", goto error);
6200 isl_space_free(pa_space);
6201 return isl_stat_ok;
6202 error:
6203 isl_space_free(pa_space);
6204 return isl_stat_error;
6207 #undef BASE
6208 #define BASE pw_aff
6209 #undef DOMBASE
6210 #define DOMBASE set
6212 #include <isl_multi_explicit_domain.c>
6213 #include <isl_multi_pw_aff_explicit_domain.c>
6214 #include <isl_multi_templ.c>
6215 #include <isl_multi_apply_set.c>
6216 #include <isl_multi_coalesce.c>
6217 #include <isl_multi_domain_templ.c>
6218 #include <isl_multi_dims.c>
6219 #include <isl_multi_from_base_templ.c>
6220 #include <isl_multi_gist.c>
6221 #include <isl_multi_hash.c>
6222 #include <isl_multi_identity_templ.c>
6223 #include <isl_multi_align_set.c>
6224 #include <isl_multi_intersect.c>
6225 #include <isl_multi_move_dims_templ.c>
6226 #include <isl_multi_product_templ.c>
6227 #include <isl_multi_splice_templ.c>
6228 #include <isl_multi_zero_templ.c>
6230 /* Does "mpa" have a non-trivial explicit domain?
6232 * The explicit domain, if present, is trivial if it represents
6233 * an (obviously) universe set.
6235 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6236 __isl_keep isl_multi_pw_aff *mpa)
6238 if (!mpa)
6239 return isl_bool_error;
6240 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6241 return isl_bool_false;
6242 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6245 /* Scale the elements of "pma" by the corresponding elements of "mv".
6247 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6248 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6250 int i;
6251 isl_bool equal_params;
6253 pma = isl_pw_multi_aff_cow(pma);
6254 if (!pma || !mv)
6255 goto error;
6256 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6257 mv->space, isl_dim_set))
6258 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6259 "spaces don't match", goto error);
6260 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6261 if (equal_params < 0)
6262 goto error;
6263 if (!equal_params) {
6264 pma = isl_pw_multi_aff_align_params(pma,
6265 isl_multi_val_get_space(mv));
6266 mv = isl_multi_val_align_params(mv,
6267 isl_pw_multi_aff_get_space(pma));
6268 if (!pma || !mv)
6269 goto error;
6272 for (i = 0; i < pma->n; ++i) {
6273 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6274 isl_multi_val_copy(mv));
6275 if (!pma->p[i].maff)
6276 goto error;
6279 isl_multi_val_free(mv);
6280 return pma;
6281 error:
6282 isl_multi_val_free(mv);
6283 isl_pw_multi_aff_free(pma);
6284 return NULL;
6287 /* This function is called for each entry of an isl_union_pw_multi_aff.
6288 * If the space of the entry matches that of data->mv,
6289 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6290 * Otherwise, return an empty isl_pw_multi_aff.
6292 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6293 __isl_take isl_pw_multi_aff *pma, void *user)
6295 isl_multi_val *mv = user;
6297 if (!pma)
6298 return NULL;
6299 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6300 mv->space, isl_dim_set)) {
6301 isl_space *space = isl_pw_multi_aff_get_space(pma);
6302 isl_pw_multi_aff_free(pma);
6303 return isl_pw_multi_aff_empty(space);
6306 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6309 /* Scale the elements of "upma" by the corresponding elements of "mv",
6310 * for those entries that match the space of "mv".
6312 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6313 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6315 upma = isl_union_pw_multi_aff_align_params(upma,
6316 isl_multi_val_get_space(mv));
6317 mv = isl_multi_val_align_params(mv,
6318 isl_union_pw_multi_aff_get_space(upma));
6319 if (!upma || !mv)
6320 goto error;
6322 return isl_union_pw_multi_aff_transform(upma,
6323 &union_pw_multi_aff_scale_multi_val_entry, mv);
6325 isl_multi_val_free(mv);
6326 return upma;
6327 error:
6328 isl_multi_val_free(mv);
6329 isl_union_pw_multi_aff_free(upma);
6330 return NULL;
6333 /* Construct and return a piecewise multi affine expression
6334 * in the given space with value zero in each of the output dimensions and
6335 * a universe domain.
6337 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6339 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6342 /* Construct and return a piecewise multi affine expression
6343 * that is equal to the given piecewise affine expression.
6345 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6346 __isl_take isl_pw_aff *pa)
6348 int i;
6349 isl_space *space;
6350 isl_pw_multi_aff *pma;
6352 if (!pa)
6353 return NULL;
6355 space = isl_pw_aff_get_space(pa);
6356 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6358 for (i = 0; i < pa->n; ++i) {
6359 isl_set *set;
6360 isl_multi_aff *ma;
6362 set = isl_set_copy(pa->p[i].set);
6363 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6364 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6367 isl_pw_aff_free(pa);
6368 return pma;
6371 /* Construct and return a piecewise multi affine expression
6372 * that is equal to the given multi piecewise affine expression
6373 * on the shared domain of the piecewise affine expressions,
6374 * in the special case of a 0D multi piecewise affine expression.
6376 * Create a piecewise multi affine expression with the explicit domain of
6377 * the 0D multi piecewise affine expression as domain.
6379 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6380 __isl_take isl_multi_pw_aff *mpa)
6382 isl_space *space;
6383 isl_set *dom;
6384 isl_multi_aff *ma;
6386 space = isl_multi_pw_aff_get_space(mpa);
6387 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6388 isl_multi_pw_aff_free(mpa);
6390 ma = isl_multi_aff_zero(space);
6391 return isl_pw_multi_aff_alloc(dom, ma);
6394 /* Construct and return a piecewise multi affine expression
6395 * that is equal to the given multi piecewise affine expression
6396 * on the shared domain of the piecewise affine expressions.
6398 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6399 __isl_take isl_multi_pw_aff *mpa)
6401 int i;
6402 isl_space *space;
6403 isl_pw_aff *pa;
6404 isl_pw_multi_aff *pma;
6406 if (!mpa)
6407 return NULL;
6409 if (mpa->n == 0)
6410 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6412 space = isl_multi_pw_aff_get_space(mpa);
6413 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6414 pma = isl_pw_multi_aff_from_pw_aff(pa);
6416 for (i = 1; i < mpa->n; ++i) {
6417 isl_pw_multi_aff *pma_i;
6419 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6420 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6421 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6424 pma = isl_pw_multi_aff_reset_space(pma, space);
6426 isl_multi_pw_aff_free(mpa);
6427 return pma;
6430 /* Construct and return a multi piecewise affine expression
6431 * that is equal to the given multi affine expression.
6433 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6434 __isl_take isl_multi_aff *ma)
6436 int i;
6437 isl_size n;
6438 isl_multi_pw_aff *mpa;
6440 n = isl_multi_aff_dim(ma, isl_dim_out);
6441 if (n < 0)
6442 ma = isl_multi_aff_free(ma);
6443 if (!ma)
6444 return NULL;
6446 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6448 for (i = 0; i < n; ++i) {
6449 isl_pw_aff *pa;
6451 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6452 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6455 isl_multi_aff_free(ma);
6456 return mpa;
6459 /* Construct and return a multi piecewise affine expression
6460 * that is equal to the given piecewise multi affine expression.
6462 * If the resulting multi piecewise affine expression has
6463 * an explicit domain, then assign it the domain of the input.
6464 * In other cases, the domain is stored in the individual elements.
6466 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6467 __isl_take isl_pw_multi_aff *pma)
6469 int i;
6470 isl_size n;
6471 isl_space *space;
6472 isl_multi_pw_aff *mpa;
6474 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6475 if (n < 0)
6476 pma = isl_pw_multi_aff_free(pma);
6477 space = isl_pw_multi_aff_get_space(pma);
6478 mpa = isl_multi_pw_aff_alloc(space);
6480 for (i = 0; i < n; ++i) {
6481 isl_pw_aff *pa;
6483 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6484 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6486 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6487 isl_set *dom;
6489 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6490 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6493 isl_pw_multi_aff_free(pma);
6494 return mpa;
6497 /* Do "pa1" and "pa2" represent the same function?
6499 * We first check if they are obviously equal.
6500 * If not, we convert them to maps and check if those are equal.
6502 * If "pa1" or "pa2" contain any NaNs, then they are considered
6503 * not to be the same. A NaN is not equal to anything, not even
6504 * to another NaN.
6506 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6507 __isl_keep isl_pw_aff *pa2)
6509 isl_bool equal;
6510 isl_bool has_nan;
6511 isl_map *map1, *map2;
6513 if (!pa1 || !pa2)
6514 return isl_bool_error;
6516 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6517 if (equal < 0 || equal)
6518 return equal;
6519 has_nan = either_involves_nan(pa1, pa2);
6520 if (has_nan < 0)
6521 return isl_bool_error;
6522 if (has_nan)
6523 return isl_bool_false;
6525 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6526 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6527 equal = isl_map_is_equal(map1, map2);
6528 isl_map_free(map1);
6529 isl_map_free(map2);
6531 return equal;
6534 /* Do "mpa1" and "mpa2" represent the same function?
6536 * Note that we cannot convert the entire isl_multi_pw_aff
6537 * to a map because the domains of the piecewise affine expressions
6538 * may not be the same.
6540 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6541 __isl_keep isl_multi_pw_aff *mpa2)
6543 int i;
6544 isl_bool equal, equal_params;
6546 if (!mpa1 || !mpa2)
6547 return isl_bool_error;
6549 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6550 if (equal_params < 0)
6551 return isl_bool_error;
6552 if (!equal_params) {
6553 if (!isl_space_has_named_params(mpa1->space))
6554 return isl_bool_false;
6555 if (!isl_space_has_named_params(mpa2->space))
6556 return isl_bool_false;
6557 mpa1 = isl_multi_pw_aff_copy(mpa1);
6558 mpa2 = isl_multi_pw_aff_copy(mpa2);
6559 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6560 isl_multi_pw_aff_get_space(mpa2));
6561 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6562 isl_multi_pw_aff_get_space(mpa1));
6563 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6564 isl_multi_pw_aff_free(mpa1);
6565 isl_multi_pw_aff_free(mpa2);
6566 return equal;
6569 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6570 if (equal < 0 || !equal)
6571 return equal;
6573 for (i = 0; i < mpa1->n; ++i) {
6574 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6575 if (equal < 0 || !equal)
6576 return equal;
6579 return isl_bool_true;
6582 /* Do "pma1" and "pma2" represent the same function?
6584 * First check if they are obviously equal.
6585 * If not, then convert them to maps and check if those are equal.
6587 * If "pa1" or "pa2" contain any NaNs, then they are considered
6588 * not to be the same. A NaN is not equal to anything, not even
6589 * to another NaN.
6591 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6592 __isl_keep isl_pw_multi_aff *pma2)
6594 isl_bool equal;
6595 isl_bool has_nan;
6596 isl_map *map1, *map2;
6598 if (!pma1 || !pma2)
6599 return isl_bool_error;
6601 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6602 if (equal < 0 || equal)
6603 return equal;
6604 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6605 if (has_nan >= 0 && !has_nan)
6606 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6607 if (has_nan < 0 || has_nan)
6608 return isl_bool_not(has_nan);
6610 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6611 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6612 equal = isl_map_is_equal(map1, map2);
6613 isl_map_free(map1);
6614 isl_map_free(map2);
6616 return equal;
6619 /* Compute the pullback of "mpa" by the function represented by "ma".
6620 * In other words, plug in "ma" in "mpa".
6622 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6624 * If "mpa" has an explicit domain, then it is this domain
6625 * that needs to undergo a pullback, i.e., a preimage.
6627 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6628 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6630 int i;
6631 isl_space *space = NULL;
6633 mpa = isl_multi_pw_aff_cow(mpa);
6634 if (!mpa || !ma)
6635 goto error;
6637 space = isl_space_join(isl_multi_aff_get_space(ma),
6638 isl_multi_pw_aff_get_space(mpa));
6639 if (!space)
6640 goto error;
6642 for (i = 0; i < mpa->n; ++i) {
6643 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6644 isl_multi_aff_copy(ma));
6645 if (!mpa->u.p[i])
6646 goto error;
6648 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6649 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6650 isl_multi_aff_copy(ma));
6651 if (!mpa->u.dom)
6652 goto error;
6655 isl_multi_aff_free(ma);
6656 isl_space_free(mpa->space);
6657 mpa->space = space;
6658 return mpa;
6659 error:
6660 isl_space_free(space);
6661 isl_multi_pw_aff_free(mpa);
6662 isl_multi_aff_free(ma);
6663 return NULL;
6666 /* Compute the pullback of "mpa" by the function represented by "ma".
6667 * In other words, plug in "ma" in "mpa".
6669 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6670 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6672 isl_bool equal_params;
6674 if (!mpa || !ma)
6675 goto error;
6676 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6677 if (equal_params < 0)
6678 goto error;
6679 if (equal_params)
6680 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6681 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6682 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6683 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6684 error:
6685 isl_multi_pw_aff_free(mpa);
6686 isl_multi_aff_free(ma);
6687 return NULL;
6690 /* Compute the pullback of "mpa" by the function represented by "pma".
6691 * In other words, plug in "pma" in "mpa".
6693 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6695 * If "mpa" has an explicit domain, then it is this domain
6696 * that needs to undergo a pullback, i.e., a preimage.
6698 static __isl_give isl_multi_pw_aff *
6699 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6700 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6702 int i;
6703 isl_space *space = NULL;
6705 mpa = isl_multi_pw_aff_cow(mpa);
6706 if (!mpa || !pma)
6707 goto error;
6709 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6710 isl_multi_pw_aff_get_space(mpa));
6712 for (i = 0; i < mpa->n; ++i) {
6713 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6714 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6715 if (!mpa->u.p[i])
6716 goto error;
6718 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6719 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6720 isl_pw_multi_aff_copy(pma));
6721 if (!mpa->u.dom)
6722 goto error;
6725 isl_pw_multi_aff_free(pma);
6726 isl_space_free(mpa->space);
6727 mpa->space = space;
6728 return mpa;
6729 error:
6730 isl_space_free(space);
6731 isl_multi_pw_aff_free(mpa);
6732 isl_pw_multi_aff_free(pma);
6733 return NULL;
6736 /* Compute the pullback of "mpa" by the function represented by "pma".
6737 * In other words, plug in "pma" in "mpa".
6739 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6740 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6742 isl_bool equal_params;
6744 if (!mpa || !pma)
6745 goto error;
6746 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6747 if (equal_params < 0)
6748 goto error;
6749 if (equal_params)
6750 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6751 mpa = isl_multi_pw_aff_align_params(mpa,
6752 isl_pw_multi_aff_get_space(pma));
6753 pma = isl_pw_multi_aff_align_params(pma,
6754 isl_multi_pw_aff_get_space(mpa));
6755 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6756 error:
6757 isl_multi_pw_aff_free(mpa);
6758 isl_pw_multi_aff_free(pma);
6759 return NULL;
6762 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6763 * with the domain of "aff". The domain of the result is the same
6764 * as that of "mpa".
6765 * "mpa" and "aff" are assumed to have been aligned.
6767 * We first extract the parametric constant from "aff", defined
6768 * over the correct domain.
6769 * Then we add the appropriate combinations of the members of "mpa".
6770 * Finally, we add the integer divisions through recursive calls.
6772 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6773 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6775 int i;
6776 isl_size n_in, n_div, n_mpa_in;
6777 isl_space *space;
6778 isl_val *v;
6779 isl_pw_aff *pa;
6780 isl_aff *tmp;
6782 n_in = isl_aff_dim(aff, isl_dim_in);
6783 n_div = isl_aff_dim(aff, isl_dim_div);
6784 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
6785 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
6786 goto error;
6788 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6789 tmp = isl_aff_copy(aff);
6790 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6791 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6792 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
6793 tmp = isl_aff_reset_domain_space(tmp, space);
6794 pa = isl_pw_aff_from_aff(tmp);
6796 for (i = 0; i < n_in; ++i) {
6797 isl_pw_aff *pa_i;
6799 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6800 continue;
6801 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6802 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6803 pa_i = isl_pw_aff_scale_val(pa_i, v);
6804 pa = isl_pw_aff_add(pa, pa_i);
6807 for (i = 0; i < n_div; ++i) {
6808 isl_aff *div;
6809 isl_pw_aff *pa_i;
6811 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6812 continue;
6813 div = isl_aff_get_div(aff, i);
6814 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6815 isl_multi_pw_aff_copy(mpa), div);
6816 pa_i = isl_pw_aff_floor(pa_i);
6817 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6818 pa_i = isl_pw_aff_scale_val(pa_i, v);
6819 pa = isl_pw_aff_add(pa, pa_i);
6822 isl_multi_pw_aff_free(mpa);
6823 isl_aff_free(aff);
6825 return pa;
6826 error:
6827 isl_multi_pw_aff_free(mpa);
6828 isl_aff_free(aff);
6829 return NULL;
6832 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6833 * with the domain of "aff". The domain of the result is the same
6834 * as that of "mpa".
6836 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6837 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6839 isl_bool equal_params;
6841 if (!aff || !mpa)
6842 goto error;
6843 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6844 if (equal_params < 0)
6845 goto error;
6846 if (equal_params)
6847 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6849 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6850 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6852 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6853 error:
6854 isl_aff_free(aff);
6855 isl_multi_pw_aff_free(mpa);
6856 return NULL;
6859 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6860 * with the domain of "pa". The domain of the result is the same
6861 * as that of "mpa".
6862 * "mpa" and "pa" are assumed to have been aligned.
6864 * We consider each piece in turn. Note that the domains of the
6865 * pieces are assumed to be disjoint and they remain disjoint
6866 * after taking the preimage (over the same function).
6868 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6869 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6871 isl_space *space;
6872 isl_pw_aff *res;
6873 int i;
6875 if (!mpa || !pa)
6876 goto error;
6878 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6879 isl_pw_aff_get_space(pa));
6880 res = isl_pw_aff_empty(space);
6882 for (i = 0; i < pa->n; ++i) {
6883 isl_pw_aff *pa_i;
6884 isl_set *domain;
6886 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6887 isl_multi_pw_aff_copy(mpa),
6888 isl_aff_copy(pa->p[i].aff));
6889 domain = isl_set_copy(pa->p[i].set);
6890 domain = isl_set_preimage_multi_pw_aff(domain,
6891 isl_multi_pw_aff_copy(mpa));
6892 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6893 res = isl_pw_aff_add_disjoint(res, pa_i);
6896 isl_pw_aff_free(pa);
6897 isl_multi_pw_aff_free(mpa);
6898 return res;
6899 error:
6900 isl_pw_aff_free(pa);
6901 isl_multi_pw_aff_free(mpa);
6902 return NULL;
6905 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6906 * with the domain of "pa". The domain of the result is the same
6907 * as that of "mpa".
6909 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6910 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6912 isl_bool equal_params;
6914 if (!pa || !mpa)
6915 goto error;
6916 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6917 if (equal_params < 0)
6918 goto error;
6919 if (equal_params)
6920 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6922 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6923 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6925 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6926 error:
6927 isl_pw_aff_free(pa);
6928 isl_multi_pw_aff_free(mpa);
6929 return NULL;
6932 /* Compute the pullback of "pa" by the function represented by "mpa".
6933 * In other words, plug in "mpa" in "pa".
6934 * "pa" and "mpa" are assumed to have been aligned.
6936 * The pullback is computed by applying "pa" to "mpa".
6938 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6939 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6941 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6944 /* Compute the pullback of "pa" by the function represented by "mpa".
6945 * In other words, plug in "mpa" in "pa".
6947 * The pullback is computed by applying "pa" to "mpa".
6949 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6950 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6952 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6955 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6956 * In other words, plug in "mpa2" in "mpa1".
6958 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6960 * We pullback each member of "mpa1" in turn.
6962 * If "mpa1" has an explicit domain, then it is this domain
6963 * that needs to undergo a pullback instead, i.e., a preimage.
6965 static __isl_give isl_multi_pw_aff *
6966 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6967 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6969 int i;
6970 isl_space *space = NULL;
6972 mpa1 = isl_multi_pw_aff_cow(mpa1);
6973 if (!mpa1 || !mpa2)
6974 goto error;
6976 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6977 isl_multi_pw_aff_get_space(mpa1));
6979 for (i = 0; i < mpa1->n; ++i) {
6980 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6981 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
6982 if (!mpa1->u.p[i])
6983 goto error;
6986 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
6987 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
6988 isl_multi_pw_aff_copy(mpa2));
6989 if (!mpa1->u.dom)
6990 goto error;
6992 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
6994 isl_multi_pw_aff_free(mpa2);
6995 return mpa1;
6996 error:
6997 isl_space_free(space);
6998 isl_multi_pw_aff_free(mpa1);
6999 isl_multi_pw_aff_free(mpa2);
7000 return NULL;
7003 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7004 * In other words, plug in "mpa2" in "mpa1".
7006 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7007 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7009 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7010 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7013 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7014 * of "mpa1" and "mpa2" live in the same space, construct map space
7015 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7016 * with this map space as extract argument.
7018 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7019 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7020 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7021 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7023 int match;
7024 isl_space *space1, *space2;
7025 isl_map *res;
7027 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7028 isl_multi_pw_aff_get_space(mpa2));
7029 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7030 isl_multi_pw_aff_get_space(mpa1));
7031 if (!mpa1 || !mpa2)
7032 goto error;
7033 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7034 mpa2->space, isl_dim_out);
7035 if (match < 0)
7036 goto error;
7037 if (!match)
7038 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7039 "range spaces don't match", goto error);
7040 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7041 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7042 space1 = isl_space_map_from_domain_and_range(space1, space2);
7044 res = order(mpa1, mpa2, space1);
7045 isl_multi_pw_aff_free(mpa1);
7046 isl_multi_pw_aff_free(mpa2);
7047 return res;
7048 error:
7049 isl_multi_pw_aff_free(mpa1);
7050 isl_multi_pw_aff_free(mpa2);
7051 return NULL;
7054 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7055 * where the function values are equal. "space" is the space of the result.
7056 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7058 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7059 * in the sequences are equal.
7061 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7062 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7063 __isl_take isl_space *space)
7065 int i;
7066 isl_size n;
7067 isl_map *res;
7069 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7070 if (n < 0)
7071 space = isl_space_free(space);
7072 res = isl_map_universe(space);
7074 for (i = 0; i < n; ++i) {
7075 isl_pw_aff *pa1, *pa2;
7076 isl_map *map;
7078 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7079 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7080 map = isl_pw_aff_eq_map(pa1, pa2);
7081 res = isl_map_intersect(res, map);
7084 return res;
7087 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7088 * where the function values are equal.
7090 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7091 __isl_take isl_multi_pw_aff *mpa2)
7093 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7094 &isl_multi_pw_aff_eq_map_on_space);
7097 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7098 * where the function values of "mpa1" is lexicographically satisfies "base"
7099 * compared to that of "mpa2". "space" is the space of the result.
7100 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7102 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7103 * if its i-th element satisfies "base" when compared to
7104 * the i-th element of "mpa2" while all previous elements are
7105 * pairwise equal.
7107 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7108 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7109 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7110 __isl_take isl_pw_aff *pa2),
7111 __isl_take isl_space *space)
7113 int i;
7114 isl_size n;
7115 isl_map *res, *rest;
7117 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7118 if (n < 0)
7119 space = isl_space_free(space);
7120 res = isl_map_empty(isl_space_copy(space));
7121 rest = isl_map_universe(space);
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 = base(pa1, pa2);
7130 map = isl_map_intersect(map, isl_map_copy(rest));
7131 res = isl_map_union(res, map);
7133 if (i == n - 1)
7134 continue;
7136 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7137 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7138 map = isl_pw_aff_eq_map(pa1, pa2);
7139 rest = isl_map_intersect(rest, map);
7142 isl_map_free(rest);
7143 return res;
7146 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7147 * where the function value of "mpa1" is lexicographically less than that
7148 * of "mpa2". "space" is the space of the result.
7149 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7151 * "mpa1" is less than "mpa2" if its i-th element is smaller
7152 * than the i-th element of "mpa2" while all previous elements are
7153 * pairwise equal.
7155 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7156 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7157 __isl_take isl_space *space)
7159 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7160 &isl_pw_aff_lt_map, space);
7163 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7164 * where the function value of "mpa1" is lexicographically less than that
7165 * of "mpa2".
7167 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7168 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7170 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7171 &isl_multi_pw_aff_lex_lt_map_on_space);
7174 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7175 * where the function value of "mpa1" is lexicographically greater than that
7176 * of "mpa2". "space" is the space of the result.
7177 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7179 * "mpa1" is greater than "mpa2" if its i-th element is greater
7180 * than the i-th element of "mpa2" while all previous elements are
7181 * pairwise equal.
7183 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7184 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7185 __isl_take isl_space *space)
7187 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7188 &isl_pw_aff_gt_map, space);
7191 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7192 * where the function value of "mpa1" is lexicographically greater than that
7193 * of "mpa2".
7195 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7196 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7198 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7199 &isl_multi_pw_aff_lex_gt_map_on_space);
7202 /* Compare two isl_affs.
7204 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7205 * than "aff2" and 0 if they are equal.
7207 * The order is fairly arbitrary. We do consider expressions that only involve
7208 * earlier dimensions as "smaller".
7210 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7212 int cmp;
7213 int last1, last2;
7215 if (aff1 == aff2)
7216 return 0;
7218 if (!aff1)
7219 return -1;
7220 if (!aff2)
7221 return 1;
7223 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7224 if (cmp != 0)
7225 return cmp;
7227 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7228 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7229 if (last1 != last2)
7230 return last1 - last2;
7232 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7235 /* Compare two isl_pw_affs.
7237 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7238 * than "pa2" and 0 if they are equal.
7240 * The order is fairly arbitrary. We do consider expressions that only involve
7241 * earlier dimensions as "smaller".
7243 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7244 __isl_keep isl_pw_aff *pa2)
7246 int i;
7247 int cmp;
7249 if (pa1 == pa2)
7250 return 0;
7252 if (!pa1)
7253 return -1;
7254 if (!pa2)
7255 return 1;
7257 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7258 if (cmp != 0)
7259 return cmp;
7261 if (pa1->n != pa2->n)
7262 return pa1->n - pa2->n;
7264 for (i = 0; i < pa1->n; ++i) {
7265 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7266 if (cmp != 0)
7267 return cmp;
7268 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7269 if (cmp != 0)
7270 return cmp;
7273 return 0;
7276 /* Return a piecewise affine expression that is equal to "v" on "domain".
7278 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7279 __isl_take isl_val *v)
7281 isl_space *space;
7282 isl_local_space *ls;
7283 isl_aff *aff;
7285 space = isl_set_get_space(domain);
7286 ls = isl_local_space_from_space(space);
7287 aff = isl_aff_val_on_domain(ls, v);
7289 return isl_pw_aff_alloc(domain, aff);
7292 /* Return a multi affine expression that is equal to "mv" on domain
7293 * space "space".
7295 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7296 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7298 int i;
7299 isl_size n;
7300 isl_space *space2;
7301 isl_local_space *ls;
7302 isl_multi_aff *ma;
7304 n = isl_multi_val_dim(mv, isl_dim_set);
7305 if (!space || n < 0)
7306 goto error;
7308 space2 = isl_multi_val_get_space(mv);
7309 space2 = isl_space_align_params(space2, isl_space_copy(space));
7310 space = isl_space_align_params(space, isl_space_copy(space2));
7311 space = isl_space_map_from_domain_and_range(space, space2);
7312 ma = isl_multi_aff_alloc(isl_space_copy(space));
7313 ls = isl_local_space_from_space(isl_space_domain(space));
7314 for (i = 0; i < n; ++i) {
7315 isl_val *v;
7316 isl_aff *aff;
7318 v = isl_multi_val_get_val(mv, i);
7319 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7320 ma = isl_multi_aff_set_aff(ma, i, aff);
7322 isl_local_space_free(ls);
7324 isl_multi_val_free(mv);
7325 return ma;
7326 error:
7327 isl_space_free(space);
7328 isl_multi_val_free(mv);
7329 return NULL;
7332 /* Return a piecewise multi-affine expression
7333 * that is equal to "mv" on "domain".
7335 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7336 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7338 isl_space *space;
7339 isl_multi_aff *ma;
7341 space = isl_set_get_space(domain);
7342 ma = isl_multi_aff_multi_val_on_space(space, mv);
7344 return isl_pw_multi_aff_alloc(domain, ma);
7347 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7348 * mv is the value that should be attained on each domain set
7349 * res collects the results
7351 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7352 isl_multi_val *mv;
7353 isl_union_pw_multi_aff *res;
7356 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7357 * and add it to data->res.
7359 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7360 void *user)
7362 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7363 isl_pw_multi_aff *pma;
7364 isl_multi_val *mv;
7366 mv = isl_multi_val_copy(data->mv);
7367 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7368 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7370 return data->res ? isl_stat_ok : isl_stat_error;
7373 /* Return a union piecewise multi-affine expression
7374 * that is equal to "mv" on "domain".
7376 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7377 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7379 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7380 isl_space *space;
7382 space = isl_union_set_get_space(domain);
7383 data.res = isl_union_pw_multi_aff_empty(space);
7384 data.mv = mv;
7385 if (isl_union_set_foreach_set(domain,
7386 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7387 data.res = isl_union_pw_multi_aff_free(data.res);
7388 isl_union_set_free(domain);
7389 isl_multi_val_free(mv);
7390 return data.res;
7393 /* Compute the pullback of data->pma by the function represented by "pma2",
7394 * provided the spaces match, and add the results to data->res.
7396 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7398 struct isl_union_pw_multi_aff_bin_data *data = user;
7400 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7401 pma2->dim, isl_dim_out)) {
7402 isl_pw_multi_aff_free(pma2);
7403 return isl_stat_ok;
7406 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7407 isl_pw_multi_aff_copy(data->pma), pma2);
7409 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7410 if (!data->res)
7411 return isl_stat_error;
7413 return isl_stat_ok;
7416 /* Compute the pullback of "upma1" by the function represented by "upma2".
7418 __isl_give isl_union_pw_multi_aff *
7419 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7420 __isl_take isl_union_pw_multi_aff *upma1,
7421 __isl_take isl_union_pw_multi_aff *upma2)
7423 return bin_op(upma1, upma2, &pullback_entry);
7426 /* Check that the domain space of "upa" matches "space".
7428 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7429 * can in principle never fail since the space "space" is that
7430 * of the isl_multi_union_pw_aff and is a set space such that
7431 * there is no domain space to match.
7433 * We check the parameters and double-check that "space" is
7434 * indeed that of a set.
7436 static isl_stat isl_union_pw_aff_check_match_domain_space(
7437 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7439 isl_space *upa_space;
7440 isl_bool match;
7442 if (!upa || !space)
7443 return isl_stat_error;
7445 match = isl_space_is_set(space);
7446 if (match < 0)
7447 return isl_stat_error;
7448 if (!match)
7449 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7450 "expecting set space", return isl_stat_error);
7452 upa_space = isl_union_pw_aff_get_space(upa);
7453 match = isl_space_has_equal_params(space, upa_space);
7454 if (match < 0)
7455 goto error;
7456 if (!match)
7457 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7458 "parameters don't match", goto error);
7460 isl_space_free(upa_space);
7461 return isl_stat_ok;
7462 error:
7463 isl_space_free(upa_space);
7464 return isl_stat_error;
7467 /* Do the parameters of "upa" match those of "space"?
7469 static isl_bool isl_union_pw_aff_matching_params(
7470 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7472 isl_space *upa_space;
7473 isl_bool match;
7475 if (!upa || !space)
7476 return isl_bool_error;
7478 upa_space = isl_union_pw_aff_get_space(upa);
7480 match = isl_space_has_equal_params(space, upa_space);
7482 isl_space_free(upa_space);
7483 return match;
7486 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7487 * space represents the new parameters.
7488 * res collects the results.
7490 struct isl_union_pw_aff_reset_params_data {
7491 isl_space *space;
7492 isl_union_pw_aff *res;
7495 /* Replace the parameters of "pa" by data->space and
7496 * add the result to data->res.
7498 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7500 struct isl_union_pw_aff_reset_params_data *data = user;
7501 isl_space *space;
7503 space = isl_pw_aff_get_space(pa);
7504 space = isl_space_replace_params(space, data->space);
7505 pa = isl_pw_aff_reset_space(pa, space);
7506 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7508 return data->res ? isl_stat_ok : isl_stat_error;
7511 /* Replace the domain space of "upa" by "space".
7512 * Since a union expression does not have a (single) domain space,
7513 * "space" is necessarily a parameter space.
7515 * Since the order and the names of the parameters determine
7516 * the hash value, we need to create a new hash table.
7518 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7519 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7521 struct isl_union_pw_aff_reset_params_data data = { space };
7522 isl_bool match;
7524 match = isl_union_pw_aff_matching_params(upa, space);
7525 if (match < 0)
7526 upa = isl_union_pw_aff_free(upa);
7527 else if (match) {
7528 isl_space_free(space);
7529 return upa;
7532 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7533 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7534 data.res = isl_union_pw_aff_free(data.res);
7536 isl_union_pw_aff_free(upa);
7537 isl_space_free(space);
7538 return data.res;
7541 /* Return the floor of "pa".
7543 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7545 return isl_pw_aff_floor(pa);
7548 /* Given f, return floor(f).
7550 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7551 __isl_take isl_union_pw_aff *upa)
7553 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7556 /* Compute
7558 * upa mod m = upa - m * floor(upa/m)
7560 * with m an integer value.
7562 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7563 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7565 isl_union_pw_aff *res;
7567 if (!upa || !m)
7568 goto error;
7570 if (!isl_val_is_int(m))
7571 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7572 "expecting integer modulo", goto error);
7573 if (!isl_val_is_pos(m))
7574 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7575 "expecting positive modulo", goto error);
7577 res = isl_union_pw_aff_copy(upa);
7578 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7579 upa = isl_union_pw_aff_floor(upa);
7580 upa = isl_union_pw_aff_scale_val(upa, m);
7581 res = isl_union_pw_aff_sub(res, upa);
7583 return res;
7584 error:
7585 isl_val_free(m);
7586 isl_union_pw_aff_free(upa);
7587 return NULL;
7590 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7591 * pos is the output position that needs to be extracted.
7592 * res collects the results.
7594 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7595 int pos;
7596 isl_union_pw_aff *res;
7599 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7600 * (assuming it has such a dimension) and add it to data->res.
7602 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7604 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7605 isl_size n_out;
7606 isl_pw_aff *pa;
7608 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7609 if (n_out < 0)
7610 return isl_stat_error;
7611 if (data->pos >= n_out) {
7612 isl_pw_multi_aff_free(pma);
7613 return isl_stat_ok;
7616 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7617 isl_pw_multi_aff_free(pma);
7619 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7621 return data->res ? isl_stat_ok : isl_stat_error;
7624 /* Extract an isl_union_pw_aff corresponding to
7625 * output dimension "pos" of "upma".
7627 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7628 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7630 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7631 isl_space *space;
7633 if (!upma)
7634 return NULL;
7636 if (pos < 0)
7637 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7638 "cannot extract at negative position", return NULL);
7640 space = isl_union_pw_multi_aff_get_space(upma);
7641 data.res = isl_union_pw_aff_empty(space);
7642 data.pos = pos;
7643 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7644 &get_union_pw_aff, &data) < 0)
7645 data.res = isl_union_pw_aff_free(data.res);
7647 return data.res;
7650 /* Return a union piecewise affine expression
7651 * that is equal to "aff" on "domain".
7653 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7654 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7656 isl_pw_aff *pa;
7658 pa = isl_pw_aff_from_aff(aff);
7659 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7662 /* Return a union piecewise affine expression
7663 * that is equal to the parameter identified by "id" on "domain".
7665 * Make sure the parameter appears in the space passed to
7666 * isl_aff_param_on_domain_space_id.
7668 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7669 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7671 isl_space *space;
7672 isl_aff *aff;
7674 space = isl_union_set_get_space(domain);
7675 space = isl_space_add_param_id(space, isl_id_copy(id));
7676 aff = isl_aff_param_on_domain_space_id(space, id);
7677 return isl_union_pw_aff_aff_on_domain(domain, aff);
7680 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7681 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7682 * needs to attain.
7683 * "res" collects the results.
7685 struct isl_union_pw_aff_pw_aff_on_domain_data {
7686 isl_pw_aff *pa;
7687 isl_union_pw_aff *res;
7690 /* Construct a piecewise affine expression that is equal to data->pa
7691 * on "domain" and add the result to data->res.
7693 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7695 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7696 isl_pw_aff *pa;
7697 isl_size dim;
7699 pa = isl_pw_aff_copy(data->pa);
7700 dim = isl_set_dim(domain, isl_dim_set);
7701 if (dim < 0)
7702 pa = isl_pw_aff_free(pa);
7703 pa = isl_pw_aff_from_range(pa);
7704 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7705 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7706 pa = isl_pw_aff_intersect_domain(pa, domain);
7707 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7709 return data->res ? isl_stat_ok : isl_stat_error;
7712 /* Return a union piecewise affine expression
7713 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7714 * have been aligned.
7716 * Construct an isl_pw_aff on each of the sets in "domain" and
7717 * collect the results.
7719 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7720 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7722 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7723 isl_space *space;
7725 space = isl_union_set_get_space(domain);
7726 data.res = isl_union_pw_aff_empty(space);
7727 data.pa = pa;
7728 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7729 data.res = isl_union_pw_aff_free(data.res);
7730 isl_union_set_free(domain);
7731 isl_pw_aff_free(pa);
7732 return data.res;
7735 /* Return a union piecewise affine expression
7736 * that is equal to "pa" on "domain".
7738 * Check that "pa" is a parametric expression,
7739 * align the parameters if needed and call
7740 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7742 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7743 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7745 isl_bool is_set;
7746 isl_bool equal_params;
7747 isl_space *domain_space, *pa_space;
7749 pa_space = isl_pw_aff_peek_space(pa);
7750 is_set = isl_space_is_set(pa_space);
7751 if (is_set < 0)
7752 goto error;
7753 if (!is_set)
7754 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7755 "expecting parametric expression", goto error);
7757 domain_space = isl_union_set_get_space(domain);
7758 pa_space = isl_pw_aff_get_space(pa);
7759 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7760 if (equal_params >= 0 && !equal_params) {
7761 isl_space *space;
7763 space = isl_space_align_params(domain_space, pa_space);
7764 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7765 domain = isl_union_set_align_params(domain, space);
7766 } else {
7767 isl_space_free(domain_space);
7768 isl_space_free(pa_space);
7771 if (equal_params < 0)
7772 goto error;
7773 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7774 error:
7775 isl_union_set_free(domain);
7776 isl_pw_aff_free(pa);
7777 return NULL;
7780 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7781 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7782 * "res" collects the results.
7784 struct isl_union_pw_aff_val_on_domain_data {
7785 isl_val *v;
7786 isl_union_pw_aff *res;
7789 /* Construct a piecewise affine expression that is equal to data->v
7790 * on "domain" and add the result to data->res.
7792 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7794 struct isl_union_pw_aff_val_on_domain_data *data = user;
7795 isl_pw_aff *pa;
7796 isl_val *v;
7798 v = isl_val_copy(data->v);
7799 pa = isl_pw_aff_val_on_domain(domain, v);
7800 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7802 return data->res ? isl_stat_ok : isl_stat_error;
7805 /* Return a union piecewise affine expression
7806 * that is equal to "v" on "domain".
7808 * Construct an isl_pw_aff on each of the sets in "domain" and
7809 * collect the results.
7811 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7812 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7814 struct isl_union_pw_aff_val_on_domain_data data;
7815 isl_space *space;
7817 space = isl_union_set_get_space(domain);
7818 data.res = isl_union_pw_aff_empty(space);
7819 data.v = v;
7820 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7821 data.res = isl_union_pw_aff_free(data.res);
7822 isl_union_set_free(domain);
7823 isl_val_free(v);
7824 return data.res;
7827 /* Construct a piecewise multi affine expression
7828 * that is equal to "pa" and add it to upma.
7830 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7831 void *user)
7833 isl_union_pw_multi_aff **upma = user;
7834 isl_pw_multi_aff *pma;
7836 pma = isl_pw_multi_aff_from_pw_aff(pa);
7837 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7839 return *upma ? isl_stat_ok : isl_stat_error;
7842 /* Construct and return a union piecewise multi affine expression
7843 * that is equal to the given union piecewise affine expression.
7845 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7846 __isl_take isl_union_pw_aff *upa)
7848 isl_space *space;
7849 isl_union_pw_multi_aff *upma;
7851 if (!upa)
7852 return NULL;
7854 space = isl_union_pw_aff_get_space(upa);
7855 upma = isl_union_pw_multi_aff_empty(space);
7857 if (isl_union_pw_aff_foreach_pw_aff(upa,
7858 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7859 upma = isl_union_pw_multi_aff_free(upma);
7861 isl_union_pw_aff_free(upa);
7862 return upma;
7865 /* Compute the set of elements in the domain of "pa" where it is zero and
7866 * add this set to "uset".
7868 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7870 isl_union_set **uset = (isl_union_set **)user;
7872 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7874 return *uset ? isl_stat_ok : isl_stat_error;
7877 /* Return a union set containing those elements in the domain
7878 * of "upa" where it is zero.
7880 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7881 __isl_take isl_union_pw_aff *upa)
7883 isl_union_set *zero;
7885 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7886 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7887 zero = isl_union_set_free(zero);
7889 isl_union_pw_aff_free(upa);
7890 return zero;
7893 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7894 * upma is the function that is plugged in.
7895 * pa is the current part of the function in which upma is plugged in.
7896 * res collects the results.
7898 struct isl_union_pw_aff_pullback_upma_data {
7899 isl_union_pw_multi_aff *upma;
7900 isl_pw_aff *pa;
7901 isl_union_pw_aff *res;
7904 /* Check if "pma" can be plugged into data->pa.
7905 * If so, perform the pullback and add the result to data->res.
7907 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7909 struct isl_union_pw_aff_pullback_upma_data *data = user;
7910 isl_pw_aff *pa;
7912 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7913 pma->dim, isl_dim_out)) {
7914 isl_pw_multi_aff_free(pma);
7915 return isl_stat_ok;
7918 pa = isl_pw_aff_copy(data->pa);
7919 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7921 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7923 return data->res ? isl_stat_ok : isl_stat_error;
7926 /* Check if any of the elements of data->upma can be plugged into pa,
7927 * add if so add the result to data->res.
7929 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7931 struct isl_union_pw_aff_pullback_upma_data *data = user;
7932 isl_stat r;
7934 data->pa = pa;
7935 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7936 &pa_pb_pma, data);
7937 isl_pw_aff_free(pa);
7939 return r;
7942 /* Compute the pullback of "upa" by the function represented by "upma".
7943 * In other words, plug in "upma" in "upa". The result contains
7944 * expressions defined over the domain space of "upma".
7946 * Run over all pairs of elements in "upa" and "upma", perform
7947 * the pullback when appropriate and collect the results.
7948 * If the hash value were based on the domain space rather than
7949 * the function space, then we could run through all elements
7950 * of "upma" and directly pick out the corresponding element of "upa".
7952 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7953 __isl_take isl_union_pw_aff *upa,
7954 __isl_take isl_union_pw_multi_aff *upma)
7956 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7957 isl_space *space;
7959 space = isl_union_pw_multi_aff_get_space(upma);
7960 upa = isl_union_pw_aff_align_params(upa, space);
7961 space = isl_union_pw_aff_get_space(upa);
7962 upma = isl_union_pw_multi_aff_align_params(upma, space);
7964 if (!upa || !upma)
7965 goto error;
7967 data.upma = upma;
7968 data.res = isl_union_pw_aff_alloc_same_size(upa);
7969 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7970 data.res = isl_union_pw_aff_free(data.res);
7972 isl_union_pw_aff_free(upa);
7973 isl_union_pw_multi_aff_free(upma);
7974 return data.res;
7975 error:
7976 isl_union_pw_aff_free(upa);
7977 isl_union_pw_multi_aff_free(upma);
7978 return NULL;
7981 #undef BASE
7982 #define BASE union_pw_aff
7983 #undef DOMBASE
7984 #define DOMBASE union_set
7986 #include <isl_multi_explicit_domain.c>
7987 #include <isl_multi_union_pw_aff_explicit_domain.c>
7988 #include <isl_multi_templ.c>
7989 #include <isl_multi_apply_set.c>
7990 #include <isl_multi_apply_union_set.c>
7991 #include <isl_multi_coalesce.c>
7992 #include <isl_multi_floor.c>
7993 #include <isl_multi_from_base_templ.c>
7994 #include <isl_multi_gist.c>
7995 #include <isl_multi_align_set.c>
7996 #include <isl_multi_align_union_set.c>
7997 #include <isl_multi_intersect.c>
7999 /* Does "mupa" have a non-trivial explicit domain?
8001 * The explicit domain, if present, is trivial if it represents
8002 * an (obviously) universe parameter set.
8004 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8005 __isl_keep isl_multi_union_pw_aff *mupa)
8007 isl_bool is_params, trivial;
8008 isl_set *set;
8010 if (!mupa)
8011 return isl_bool_error;
8012 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8013 return isl_bool_false;
8014 is_params = isl_union_set_is_params(mupa->u.dom);
8015 if (is_params < 0 || !is_params)
8016 return isl_bool_not(is_params);
8017 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8018 trivial = isl_set_plain_is_universe(set);
8019 isl_set_free(set);
8020 return isl_bool_not(trivial);
8023 /* Construct a multiple union piecewise affine expression
8024 * in the given space with value zero in each of the output dimensions.
8026 * Since there is no canonical zero value for
8027 * a union piecewise affine expression, we can only construct
8028 * a zero-dimensional "zero" value.
8030 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8031 __isl_take isl_space *space)
8033 isl_bool params;
8034 isl_size dim;
8036 if (!space)
8037 return NULL;
8039 params = isl_space_is_params(space);
8040 if (params < 0)
8041 goto error;
8042 if (params)
8043 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8044 "expecting proper set space", goto error);
8045 if (!isl_space_is_set(space))
8046 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8047 "expecting set space", goto error);
8048 dim = isl_space_dim(space, isl_dim_out);
8049 if (dim < 0)
8050 goto error;
8051 if (dim != 0)
8052 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8053 "expecting 0D space", goto error);
8055 return isl_multi_union_pw_aff_alloc(space);
8056 error:
8057 isl_space_free(space);
8058 return NULL;
8061 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8062 * with the actual sum on the shared domain and
8063 * the defined expression on the symmetric difference of the domains.
8065 * We simply iterate over the elements in both arguments and
8066 * call isl_union_pw_aff_union_add on each of them, if there is
8067 * at least one element.
8069 * Otherwise, the two expressions have an explicit domain and
8070 * the union of these explicit domains is computed.
8071 * This assumes that the explicit domains are either both in terms
8072 * of specific domains elements or both in terms of parameters.
8073 * However, if one of the expressions does not have any constraints
8074 * on its explicit domain, then this is allowed as well and the result
8075 * is the expression with no constraints on its explicit domain.
8077 static __isl_give isl_multi_union_pw_aff *
8078 isl_multi_union_pw_aff_union_add_aligned(
8079 __isl_take isl_multi_union_pw_aff *mupa1,
8080 __isl_take isl_multi_union_pw_aff *mupa2)
8082 isl_bool has_domain, is_params1, is_params2;
8084 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8085 goto error;
8086 if (mupa1->n > 0)
8087 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8088 &isl_union_pw_aff_union_add);
8089 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8090 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8091 goto error;
8093 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8094 if (has_domain < 0)
8095 goto error;
8096 if (!has_domain) {
8097 isl_multi_union_pw_aff_free(mupa2);
8098 return mupa1;
8100 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8101 if (has_domain < 0)
8102 goto error;
8103 if (!has_domain) {
8104 isl_multi_union_pw_aff_free(mupa1);
8105 return mupa2;
8108 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8109 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8110 if (is_params1 < 0 || is_params2 < 0)
8111 goto error;
8112 if (is_params1 != is_params2)
8113 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8114 isl_error_invalid,
8115 "cannot compute union of concrete domain and "
8116 "parameter constraints", goto error);
8117 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8118 if (!mupa1)
8119 goto error;
8120 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8121 isl_union_set_copy(mupa2->u.dom));
8122 if (!mupa1->u.dom)
8123 goto error;
8124 isl_multi_union_pw_aff_free(mupa2);
8125 return mupa1;
8126 error:
8127 isl_multi_union_pw_aff_free(mupa1);
8128 isl_multi_union_pw_aff_free(mupa2);
8129 return NULL;
8132 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8133 * with the actual sum on the shared domain and
8134 * the defined expression on the symmetric difference of the domains.
8136 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8137 __isl_take isl_multi_union_pw_aff *mupa1,
8138 __isl_take isl_multi_union_pw_aff *mupa2)
8140 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8141 &isl_multi_union_pw_aff_union_add_aligned);
8144 /* Construct and return a multi union piecewise affine expression
8145 * that is equal to the given multi affine expression.
8147 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8148 __isl_take isl_multi_aff *ma)
8150 isl_multi_pw_aff *mpa;
8152 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8153 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8156 /* Construct and return a multi union piecewise affine expression
8157 * that is equal to the given multi piecewise affine expression.
8159 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8160 __isl_take isl_multi_pw_aff *mpa)
8162 int i;
8163 isl_size n;
8164 isl_space *space;
8165 isl_multi_union_pw_aff *mupa;
8167 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8168 if (n < 0)
8169 mpa = isl_multi_pw_aff_free(mpa);
8170 if (!mpa)
8171 return NULL;
8173 space = isl_multi_pw_aff_get_space(mpa);
8174 space = isl_space_range(space);
8175 mupa = isl_multi_union_pw_aff_alloc(space);
8177 for (i = 0; i < n; ++i) {
8178 isl_pw_aff *pa;
8179 isl_union_pw_aff *upa;
8181 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8182 upa = isl_union_pw_aff_from_pw_aff(pa);
8183 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8186 isl_multi_pw_aff_free(mpa);
8188 return mupa;
8191 /* Extract the range space of "pma" and assign it to *space.
8192 * If *space has already been set (through a previous call to this function),
8193 * then check that the range space is the same.
8195 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8197 isl_space **space = user;
8198 isl_space *pma_space;
8199 isl_bool equal;
8201 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8202 isl_pw_multi_aff_free(pma);
8204 if (!pma_space)
8205 return isl_stat_error;
8206 if (!*space) {
8207 *space = pma_space;
8208 return isl_stat_ok;
8211 equal = isl_space_is_equal(pma_space, *space);
8212 isl_space_free(pma_space);
8214 if (equal < 0)
8215 return isl_stat_error;
8216 if (!equal)
8217 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8218 "range spaces not the same", return isl_stat_error);
8219 return isl_stat_ok;
8222 /* Construct and return a multi union piecewise affine expression
8223 * that is equal to the given union piecewise multi affine expression.
8225 * In order to be able to perform the conversion, the input
8226 * needs to be non-empty and may only involve a single range space.
8228 * If the resulting multi union piecewise affine expression has
8229 * an explicit domain, then assign it the domain of the input.
8230 * In other cases, the domain is stored in the individual elements.
8232 __isl_give isl_multi_union_pw_aff *
8233 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8234 __isl_take isl_union_pw_multi_aff *upma)
8236 isl_space *space = NULL;
8237 isl_multi_union_pw_aff *mupa;
8238 int i;
8239 isl_size n;
8241 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8242 if (n < 0)
8243 goto error;
8244 if (n == 0)
8245 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8246 "cannot extract range space from empty input",
8247 goto error);
8248 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8249 &space) < 0)
8250 goto error;
8252 if (!space)
8253 goto error;
8255 n = isl_space_dim(space, isl_dim_set);
8256 if (n < 0)
8257 space = isl_space_free(space);
8258 mupa = isl_multi_union_pw_aff_alloc(space);
8260 for (i = 0; i < n; ++i) {
8261 isl_union_pw_aff *upa;
8263 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8264 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8266 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8267 isl_union_set *dom;
8268 isl_union_pw_multi_aff *copy;
8270 copy = isl_union_pw_multi_aff_copy(upma);
8271 dom = isl_union_pw_multi_aff_domain(copy);
8272 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8275 isl_union_pw_multi_aff_free(upma);
8276 return mupa;
8277 error:
8278 isl_space_free(space);
8279 isl_union_pw_multi_aff_free(upma);
8280 return NULL;
8283 /* Try and create an isl_multi_union_pw_aff that is equivalent
8284 * to the given isl_union_map.
8285 * The isl_union_map is required to be single-valued in each space.
8286 * Moreover, it cannot be empty and all range spaces need to be the same.
8287 * Otherwise, an error is produced.
8289 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8290 __isl_take isl_union_map *umap)
8292 isl_union_pw_multi_aff *upma;
8294 upma = isl_union_pw_multi_aff_from_union_map(umap);
8295 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8298 /* Return a multiple union piecewise affine expression
8299 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8300 * have been aligned.
8302 * If the resulting multi union piecewise affine expression has
8303 * an explicit domain, then assign it the input domain.
8304 * In other cases, the domain is stored in the individual elements.
8306 static __isl_give isl_multi_union_pw_aff *
8307 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8308 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8310 int i;
8311 isl_size n;
8312 isl_space *space;
8313 isl_multi_union_pw_aff *mupa;
8315 n = isl_multi_val_dim(mv, isl_dim_set);
8316 if (!domain || n < 0)
8317 goto error;
8319 space = isl_multi_val_get_space(mv);
8320 mupa = isl_multi_union_pw_aff_alloc(space);
8321 for (i = 0; i < n; ++i) {
8322 isl_val *v;
8323 isl_union_pw_aff *upa;
8325 v = isl_multi_val_get_val(mv, i);
8326 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8328 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8330 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8331 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8332 isl_union_set_copy(domain));
8334 isl_union_set_free(domain);
8335 isl_multi_val_free(mv);
8336 return mupa;
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 "mv" on "domain".
8346 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8347 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8349 isl_bool equal_params;
8351 if (!domain || !mv)
8352 goto error;
8353 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8354 if (equal_params < 0)
8355 goto error;
8356 if (equal_params)
8357 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8358 domain, mv);
8359 domain = isl_union_set_align_params(domain,
8360 isl_multi_val_get_space(mv));
8361 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8362 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8363 error:
8364 isl_union_set_free(domain);
8365 isl_multi_val_free(mv);
8366 return NULL;
8369 /* Return a multiple union piecewise affine expression
8370 * that is equal to "ma" on "domain".
8372 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8373 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8375 isl_pw_multi_aff *pma;
8377 pma = isl_pw_multi_aff_from_multi_aff(ma);
8378 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8381 /* Return a multiple union piecewise affine expression
8382 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8383 * have been aligned.
8385 * If the resulting multi union piecewise affine expression has
8386 * an explicit domain, then assign it the input domain.
8387 * In other cases, the domain is stored in the individual elements.
8389 static __isl_give isl_multi_union_pw_aff *
8390 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8391 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8393 int i;
8394 isl_size n;
8395 isl_space *space;
8396 isl_multi_union_pw_aff *mupa;
8398 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8399 if (!domain || n < 0)
8400 goto error;
8401 space = isl_pw_multi_aff_get_space(pma);
8402 mupa = isl_multi_union_pw_aff_alloc(space);
8403 for (i = 0; i < n; ++i) {
8404 isl_pw_aff *pa;
8405 isl_union_pw_aff *upa;
8407 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8408 upa = isl_union_pw_aff_pw_aff_on_domain(
8409 isl_union_set_copy(domain), pa);
8410 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8412 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8413 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8414 isl_union_set_copy(domain));
8416 isl_union_set_free(domain);
8417 isl_pw_multi_aff_free(pma);
8418 return mupa;
8419 error:
8420 isl_union_set_free(domain);
8421 isl_pw_multi_aff_free(pma);
8422 return NULL;
8425 /* Return a multiple union piecewise affine expression
8426 * that is equal to "pma" on "domain".
8428 __isl_give isl_multi_union_pw_aff *
8429 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8430 __isl_take isl_pw_multi_aff *pma)
8432 isl_bool equal_params;
8433 isl_space *space;
8435 space = isl_pw_multi_aff_peek_space(pma);
8436 equal_params = isl_union_set_space_has_equal_params(domain, space);
8437 if (equal_params < 0)
8438 goto error;
8439 if (equal_params)
8440 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8441 domain, pma);
8442 domain = isl_union_set_align_params(domain,
8443 isl_pw_multi_aff_get_space(pma));
8444 pma = isl_pw_multi_aff_align_params(pma,
8445 isl_union_set_get_space(domain));
8446 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8447 pma);
8448 error:
8449 isl_union_set_free(domain);
8450 isl_pw_multi_aff_free(pma);
8451 return NULL;
8454 /* Return a union set containing those elements in the domains
8455 * of the elements of "mupa" where they are all zero.
8457 * If there are no elements, then simply return the entire domain.
8459 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8460 __isl_take isl_multi_union_pw_aff *mupa)
8462 int i;
8463 isl_size n;
8464 isl_union_pw_aff *upa;
8465 isl_union_set *zero;
8467 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8468 if (n < 0)
8469 mupa = isl_multi_union_pw_aff_free(mupa);
8470 if (!mupa)
8471 return NULL;
8473 if (n == 0)
8474 return isl_multi_union_pw_aff_domain(mupa);
8476 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8477 zero = isl_union_pw_aff_zero_union_set(upa);
8479 for (i = 1; i < n; ++i) {
8480 isl_union_set *zero_i;
8482 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8483 zero_i = isl_union_pw_aff_zero_union_set(upa);
8485 zero = isl_union_set_intersect(zero, zero_i);
8488 isl_multi_union_pw_aff_free(mupa);
8489 return zero;
8492 /* Construct a union map mapping the shared domain
8493 * of the union piecewise affine expressions to the range of "mupa"
8494 * in the special case of a 0D multi union piecewise affine expression.
8496 * Construct a map between the explicit domain of "mupa" and
8497 * the range space.
8498 * Note that this assumes that the domain consists of explicit elements.
8500 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8501 __isl_take isl_multi_union_pw_aff *mupa)
8503 isl_bool is_params;
8504 isl_space *space;
8505 isl_union_set *dom, *ran;
8507 space = isl_multi_union_pw_aff_get_space(mupa);
8508 dom = isl_multi_union_pw_aff_domain(mupa);
8509 ran = isl_union_set_from_set(isl_set_universe(space));
8511 is_params = isl_union_set_is_params(dom);
8512 if (is_params < 0)
8513 dom = isl_union_set_free(dom);
8514 else if (is_params)
8515 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8516 "cannot create union map from expression without "
8517 "explicit domain elements",
8518 dom = isl_union_set_free(dom));
8520 return isl_union_map_from_domain_and_range(dom, ran);
8523 /* Construct a union map mapping the shared domain
8524 * of the union piecewise affine expressions to the range of "mupa"
8525 * with each dimension in the range equated to the
8526 * corresponding union piecewise affine expression.
8528 * If the input is zero-dimensional, then construct a mapping
8529 * from its explicit domain.
8531 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8532 __isl_take isl_multi_union_pw_aff *mupa)
8534 int i;
8535 isl_size n;
8536 isl_space *space;
8537 isl_union_map *umap;
8538 isl_union_pw_aff *upa;
8540 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8541 if (n < 0)
8542 mupa = isl_multi_union_pw_aff_free(mupa);
8543 if (!mupa)
8544 return NULL;
8546 if (n == 0)
8547 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8549 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8550 umap = isl_union_map_from_union_pw_aff(upa);
8552 for (i = 1; i < n; ++i) {
8553 isl_union_map *umap_i;
8555 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8556 umap_i = isl_union_map_from_union_pw_aff(upa);
8557 umap = isl_union_map_flat_range_product(umap, umap_i);
8560 space = isl_multi_union_pw_aff_get_space(mupa);
8561 umap = isl_union_map_reset_range_space(umap, space);
8563 isl_multi_union_pw_aff_free(mupa);
8564 return umap;
8567 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8568 * "range" is the space from which to set the range space.
8569 * "res" collects the results.
8571 struct isl_union_pw_multi_aff_reset_range_space_data {
8572 isl_space *range;
8573 isl_union_pw_multi_aff *res;
8576 /* Replace the range space of "pma" by the range space of data->range and
8577 * add the result to data->res.
8579 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8581 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8582 isl_space *space;
8584 space = isl_pw_multi_aff_get_space(pma);
8585 space = isl_space_domain(space);
8586 space = isl_space_extend_domain_with_range(space,
8587 isl_space_copy(data->range));
8588 pma = isl_pw_multi_aff_reset_space(pma, space);
8589 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8591 return data->res ? isl_stat_ok : isl_stat_error;
8594 /* Replace the range space of all the piecewise affine expressions in "upma" by
8595 * the range space of "space".
8597 * This assumes that all these expressions have the same output dimension.
8599 * Since the spaces of the expressions change, so do their hash values.
8600 * We therefore need to create a new isl_union_pw_multi_aff.
8601 * Note that the hash value is currently computed based on the entire
8602 * space even though there can only be a single expression with a given
8603 * domain space.
8605 static __isl_give isl_union_pw_multi_aff *
8606 isl_union_pw_multi_aff_reset_range_space(
8607 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8609 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8610 isl_space *space_upma;
8612 space_upma = isl_union_pw_multi_aff_get_space(upma);
8613 data.res = isl_union_pw_multi_aff_empty(space_upma);
8614 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8615 &reset_range_space, &data) < 0)
8616 data.res = isl_union_pw_multi_aff_free(data.res);
8618 isl_space_free(space);
8619 isl_union_pw_multi_aff_free(upma);
8620 return data.res;
8623 /* Construct and return a union piecewise multi affine expression
8624 * that is equal to the given multi union piecewise affine expression,
8625 * in the special case of a 0D multi union piecewise affine expression.
8627 * Construct a union piecewise multi affine expression
8628 * on top of the explicit domain of the input.
8630 __isl_give isl_union_pw_multi_aff *
8631 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8632 __isl_take isl_multi_union_pw_aff *mupa)
8634 isl_space *space;
8635 isl_multi_val *mv;
8636 isl_union_set *domain;
8638 space = isl_multi_union_pw_aff_get_space(mupa);
8639 mv = isl_multi_val_zero(space);
8640 domain = isl_multi_union_pw_aff_domain(mupa);
8641 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8644 /* Construct and return a union piecewise multi affine expression
8645 * that is equal to the given multi union piecewise affine expression.
8647 * If the input is zero-dimensional, then
8648 * construct a union piecewise multi affine expression
8649 * on top of the explicit domain of the input.
8651 __isl_give isl_union_pw_multi_aff *
8652 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8653 __isl_take isl_multi_union_pw_aff *mupa)
8655 int i;
8656 isl_size n;
8657 isl_space *space;
8658 isl_union_pw_multi_aff *upma;
8659 isl_union_pw_aff *upa;
8661 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8662 if (n < 0)
8663 mupa = isl_multi_union_pw_aff_free(mupa);
8664 if (!mupa)
8665 return NULL;
8667 if (n == 0)
8668 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8670 space = isl_multi_union_pw_aff_get_space(mupa);
8671 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8672 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8674 for (i = 1; i < n; ++i) {
8675 isl_union_pw_multi_aff *upma_i;
8677 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8678 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8679 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8682 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8684 isl_multi_union_pw_aff_free(mupa);
8685 return upma;
8688 /* Intersect the range of "mupa" with "range",
8689 * in the special case where "mupa" is 0D.
8691 * Intersect the domain of "mupa" with the constraints on the parameters
8692 * of "range".
8694 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8695 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8697 range = isl_set_params(range);
8698 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8699 return mupa;
8702 /* Intersect the range of "mupa" with "range".
8703 * That is, keep only those domain elements that have a function value
8704 * in "range".
8706 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8707 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8709 isl_union_pw_multi_aff *upma;
8710 isl_union_set *domain;
8711 isl_space *space;
8712 isl_size n;
8713 int match;
8715 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8716 if (n < 0 || !range)
8717 goto error;
8719 space = isl_set_get_space(range);
8720 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8721 space, isl_dim_set);
8722 isl_space_free(space);
8723 if (match < 0)
8724 goto error;
8725 if (!match)
8726 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8727 "space don't match", goto error);
8728 if (n == 0)
8729 return mupa_intersect_range_0D(mupa, range);
8731 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8732 isl_multi_union_pw_aff_copy(mupa));
8733 domain = isl_union_set_from_set(range);
8734 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8735 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8737 return mupa;
8738 error:
8739 isl_multi_union_pw_aff_free(mupa);
8740 isl_set_free(range);
8741 return NULL;
8744 /* Return the shared domain of the elements of "mupa",
8745 * in the special case where "mupa" is zero-dimensional.
8747 * Return the explicit domain of "mupa".
8748 * Note that this domain may be a parameter set, either
8749 * because "mupa" is meant to live in a set space or
8750 * because no explicit domain has been set.
8752 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8753 __isl_take isl_multi_union_pw_aff *mupa)
8755 isl_union_set *dom;
8757 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8758 isl_multi_union_pw_aff_free(mupa);
8760 return dom;
8763 /* Return the shared domain of the elements of "mupa".
8765 * If "mupa" is zero-dimensional, then return its explicit domain.
8767 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8768 __isl_take isl_multi_union_pw_aff *mupa)
8770 int i;
8771 isl_size n;
8772 isl_union_pw_aff *upa;
8773 isl_union_set *dom;
8775 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8776 if (n < 0)
8777 mupa = isl_multi_union_pw_aff_free(mupa);
8778 if (!mupa)
8779 return NULL;
8781 if (n == 0)
8782 return isl_multi_union_pw_aff_domain_0D(mupa);
8784 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8785 dom = isl_union_pw_aff_domain(upa);
8786 for (i = 1; i < n; ++i) {
8787 isl_union_set *dom_i;
8789 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8790 dom_i = isl_union_pw_aff_domain(upa);
8791 dom = isl_union_set_intersect(dom, dom_i);
8794 isl_multi_union_pw_aff_free(mupa);
8795 return dom;
8798 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8799 * In particular, the spaces have been aligned.
8800 * The result is defined over the shared domain of the elements of "mupa"
8802 * We first extract the parametric constant part of "aff" and
8803 * define that over the shared domain.
8804 * Then we iterate over all input dimensions of "aff" and add the corresponding
8805 * multiples of the elements of "mupa".
8806 * Finally, we consider the integer divisions, calling the function
8807 * recursively to obtain an isl_union_pw_aff corresponding to the
8808 * integer division argument.
8810 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8811 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8813 int i;
8814 isl_size n_in, n_div;
8815 isl_union_pw_aff *upa;
8816 isl_union_set *uset;
8817 isl_val *v;
8818 isl_aff *cst;
8820 n_in = isl_aff_dim(aff, isl_dim_in);
8821 n_div = isl_aff_dim(aff, isl_dim_div);
8822 if (n_in < 0 || n_div < 0)
8823 goto error;
8825 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8826 cst = isl_aff_copy(aff);
8827 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8828 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8829 cst = isl_aff_project_domain_on_params(cst);
8830 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8832 for (i = 0; i < n_in; ++i) {
8833 isl_union_pw_aff *upa_i;
8835 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8836 continue;
8837 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8838 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8839 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8840 upa = isl_union_pw_aff_add(upa, upa_i);
8843 for (i = 0; i < n_div; ++i) {
8844 isl_aff *div;
8845 isl_union_pw_aff *upa_i;
8847 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8848 continue;
8849 div = isl_aff_get_div(aff, i);
8850 upa_i = multi_union_pw_aff_apply_aff(
8851 isl_multi_union_pw_aff_copy(mupa), div);
8852 upa_i = isl_union_pw_aff_floor(upa_i);
8853 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8854 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8855 upa = isl_union_pw_aff_add(upa, upa_i);
8858 isl_multi_union_pw_aff_free(mupa);
8859 isl_aff_free(aff);
8861 return upa;
8862 error:
8863 isl_multi_union_pw_aff_free(mupa);
8864 isl_aff_free(aff);
8865 return NULL;
8868 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8869 * with the domain of "aff".
8870 * Furthermore, the dimension of this space needs to be greater than zero.
8871 * The result is defined over the shared domain of the elements of "mupa"
8873 * We perform these checks and then hand over control to
8874 * multi_union_pw_aff_apply_aff.
8876 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8877 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8879 isl_size dim;
8880 isl_space *space1, *space2;
8881 isl_bool equal;
8883 mupa = isl_multi_union_pw_aff_align_params(mupa,
8884 isl_aff_get_space(aff));
8885 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8886 if (!mupa || !aff)
8887 goto error;
8889 space1 = isl_multi_union_pw_aff_get_space(mupa);
8890 space2 = isl_aff_get_domain_space(aff);
8891 equal = isl_space_is_equal(space1, space2);
8892 isl_space_free(space1);
8893 isl_space_free(space2);
8894 if (equal < 0)
8895 goto error;
8896 if (!equal)
8897 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8898 "spaces don't match", goto error);
8899 dim = isl_aff_dim(aff, isl_dim_in);
8900 if (dim < 0)
8901 goto error;
8902 if (dim == 0)
8903 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8904 "cannot determine domains", goto error);
8906 return multi_union_pw_aff_apply_aff(mupa, aff);
8907 error:
8908 isl_multi_union_pw_aff_free(mupa);
8909 isl_aff_free(aff);
8910 return NULL;
8913 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8914 * The space of "mupa" is known to be compatible with the domain of "ma".
8916 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8917 * on the domain of "mupa".
8919 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
8920 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8922 isl_union_set *dom;
8924 dom = isl_multi_union_pw_aff_domain(mupa);
8925 ma = isl_multi_aff_project_domain_on_params(ma);
8927 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
8930 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8931 * with the domain of "ma".
8932 * The result is defined over the shared domain of the elements of "mupa"
8934 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8935 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8937 isl_space *space1, *space2;
8938 isl_multi_union_pw_aff *res;
8939 isl_bool equal;
8940 int i;
8941 isl_size n_in, n_out;
8943 mupa = isl_multi_union_pw_aff_align_params(mupa,
8944 isl_multi_aff_get_space(ma));
8945 ma = isl_multi_aff_align_params(ma,
8946 isl_multi_union_pw_aff_get_space(mupa));
8947 n_in = isl_multi_aff_dim(ma, isl_dim_in);
8948 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8949 if (!mupa || n_in < 0 || n_out < 0)
8950 goto error;
8952 space1 = isl_multi_union_pw_aff_get_space(mupa);
8953 space2 = isl_multi_aff_get_domain_space(ma);
8954 equal = isl_space_is_equal(space1, space2);
8955 isl_space_free(space1);
8956 isl_space_free(space2);
8957 if (equal < 0)
8958 goto error;
8959 if (!equal)
8960 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8961 "spaces don't match", goto error);
8962 if (n_in == 0)
8963 return mupa_apply_multi_aff_0D(mupa, ma);
8965 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8966 res = isl_multi_union_pw_aff_alloc(space1);
8968 for (i = 0; i < n_out; ++i) {
8969 isl_aff *aff;
8970 isl_union_pw_aff *upa;
8972 aff = isl_multi_aff_get_aff(ma, i);
8973 upa = multi_union_pw_aff_apply_aff(
8974 isl_multi_union_pw_aff_copy(mupa), aff);
8975 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8978 isl_multi_aff_free(ma);
8979 isl_multi_union_pw_aff_free(mupa);
8980 return res;
8981 error:
8982 isl_multi_union_pw_aff_free(mupa);
8983 isl_multi_aff_free(ma);
8984 return NULL;
8987 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
8988 * The space of "mupa" is known to be compatible with the domain of "pa".
8990 * Construct an isl_multi_union_pw_aff that is equal to "pa"
8991 * on the domain of "mupa".
8993 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
8994 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
8996 isl_union_set *dom;
8998 dom = isl_multi_union_pw_aff_domain(mupa);
8999 pa = isl_pw_aff_project_domain_on_params(pa);
9001 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9004 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9005 * with the domain of "pa".
9006 * Furthermore, the dimension of this space needs to be greater than zero.
9007 * The result is defined over the shared domain of the elements of "mupa"
9009 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9010 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9012 int i;
9013 isl_bool equal;
9014 isl_size n_in;
9015 isl_space *space, *space2;
9016 isl_union_pw_aff *upa;
9018 mupa = isl_multi_union_pw_aff_align_params(mupa,
9019 isl_pw_aff_get_space(pa));
9020 pa = isl_pw_aff_align_params(pa,
9021 isl_multi_union_pw_aff_get_space(mupa));
9022 if (!mupa || !pa)
9023 goto error;
9025 space = isl_multi_union_pw_aff_get_space(mupa);
9026 space2 = isl_pw_aff_get_domain_space(pa);
9027 equal = isl_space_is_equal(space, space2);
9028 isl_space_free(space);
9029 isl_space_free(space2);
9030 if (equal < 0)
9031 goto error;
9032 if (!equal)
9033 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9034 "spaces don't match", goto error);
9035 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9036 if (n_in < 0)
9037 goto error;
9038 if (n_in == 0)
9039 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9041 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9042 upa = isl_union_pw_aff_empty(space);
9044 for (i = 0; i < pa->n; ++i) {
9045 isl_aff *aff;
9046 isl_set *domain;
9047 isl_multi_union_pw_aff *mupa_i;
9048 isl_union_pw_aff *upa_i;
9050 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9051 domain = isl_set_copy(pa->p[i].set);
9052 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9053 aff = isl_aff_copy(pa->p[i].aff);
9054 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9055 upa = isl_union_pw_aff_union_add(upa, upa_i);
9058 isl_multi_union_pw_aff_free(mupa);
9059 isl_pw_aff_free(pa);
9060 return upa;
9061 error:
9062 isl_multi_union_pw_aff_free(mupa);
9063 isl_pw_aff_free(pa);
9064 return NULL;
9067 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9068 * The space of "mupa" is known to be compatible with the domain of "pma".
9070 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9071 * on the domain of "mupa".
9073 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9074 __isl_take isl_multi_union_pw_aff *mupa,
9075 __isl_take isl_pw_multi_aff *pma)
9077 isl_union_set *dom;
9079 dom = isl_multi_union_pw_aff_domain(mupa);
9080 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9082 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9085 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9086 * with the domain of "pma".
9087 * The result is defined over the shared domain of the elements of "mupa"
9089 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9090 __isl_take isl_multi_union_pw_aff *mupa,
9091 __isl_take isl_pw_multi_aff *pma)
9093 isl_space *space1, *space2;
9094 isl_multi_union_pw_aff *res;
9095 isl_bool equal;
9096 int i;
9097 isl_size n_in, n_out;
9099 mupa = isl_multi_union_pw_aff_align_params(mupa,
9100 isl_pw_multi_aff_get_space(pma));
9101 pma = isl_pw_multi_aff_align_params(pma,
9102 isl_multi_union_pw_aff_get_space(mupa));
9103 if (!mupa || !pma)
9104 goto error;
9106 space1 = isl_multi_union_pw_aff_get_space(mupa);
9107 space2 = isl_pw_multi_aff_get_domain_space(pma);
9108 equal = isl_space_is_equal(space1, space2);
9109 isl_space_free(space1);
9110 isl_space_free(space2);
9111 if (equal < 0)
9112 goto error;
9113 if (!equal)
9114 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9115 "spaces don't match", goto error);
9116 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9117 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9118 if (n_in < 0 || n_out < 0)
9119 goto error;
9120 if (n_in == 0)
9121 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9123 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9124 res = isl_multi_union_pw_aff_alloc(space1);
9126 for (i = 0; i < n_out; ++i) {
9127 isl_pw_aff *pa;
9128 isl_union_pw_aff *upa;
9130 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9131 upa = isl_multi_union_pw_aff_apply_pw_aff(
9132 isl_multi_union_pw_aff_copy(mupa), pa);
9133 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9136 isl_pw_multi_aff_free(pma);
9137 isl_multi_union_pw_aff_free(mupa);
9138 return res;
9139 error:
9140 isl_multi_union_pw_aff_free(mupa);
9141 isl_pw_multi_aff_free(pma);
9142 return NULL;
9145 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9146 * If the explicit domain only keeps track of constraints on the parameters,
9147 * then only update those constraints.
9149 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9150 __isl_take isl_multi_union_pw_aff *mupa,
9151 __isl_keep isl_union_pw_multi_aff *upma)
9153 isl_bool is_params;
9155 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9156 return isl_multi_union_pw_aff_free(mupa);
9158 mupa = isl_multi_union_pw_aff_cow(mupa);
9159 if (!mupa)
9160 return NULL;
9162 is_params = isl_union_set_is_params(mupa->u.dom);
9163 if (is_params < 0)
9164 return isl_multi_union_pw_aff_free(mupa);
9166 upma = isl_union_pw_multi_aff_copy(upma);
9167 if (is_params)
9168 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9169 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9170 else
9171 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9172 mupa->u.dom, upma);
9173 if (!mupa->u.dom)
9174 return isl_multi_union_pw_aff_free(mupa);
9175 return mupa;
9178 /* Compute the pullback of "mupa" by the function represented by "upma".
9179 * In other words, plug in "upma" in "mupa". The result contains
9180 * expressions defined over the domain space of "upma".
9182 * Run over all elements of "mupa" and plug in "upma" in each of them.
9184 * If "mupa" has an explicit domain, then it is this domain
9185 * that needs to undergo a pullback instead, i.e., a preimage.
9187 __isl_give isl_multi_union_pw_aff *
9188 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9189 __isl_take isl_multi_union_pw_aff *mupa,
9190 __isl_take isl_union_pw_multi_aff *upma)
9192 int i;
9193 isl_size n;
9195 mupa = isl_multi_union_pw_aff_align_params(mupa,
9196 isl_union_pw_multi_aff_get_space(upma));
9197 upma = isl_union_pw_multi_aff_align_params(upma,
9198 isl_multi_union_pw_aff_get_space(mupa));
9199 mupa = isl_multi_union_pw_aff_cow(mupa);
9200 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9201 if (n < 0 || !upma)
9202 goto error;
9204 for (i = 0; i < n; ++i) {
9205 isl_union_pw_aff *upa;
9207 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9208 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9209 isl_union_pw_multi_aff_copy(upma));
9210 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9213 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9214 mupa = preimage_explicit_domain(mupa, upma);
9216 isl_union_pw_multi_aff_free(upma);
9217 return mupa;
9218 error:
9219 isl_multi_union_pw_aff_free(mupa);
9220 isl_union_pw_multi_aff_free(upma);
9221 return NULL;
9224 /* Extract the sequence of elements in "mupa" with domain space "space"
9225 * (ignoring parameters).
9227 * For the elements of "mupa" that are not defined on the specified space,
9228 * the corresponding element in the result is empty.
9230 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9231 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9233 int i;
9234 isl_size n;
9235 isl_space *space_mpa;
9236 isl_multi_pw_aff *mpa;
9238 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9239 if (n < 0 || !space)
9240 goto error;
9242 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9243 space = isl_space_replace_params(space, space_mpa);
9244 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9245 space_mpa);
9246 mpa = isl_multi_pw_aff_alloc(space_mpa);
9248 space = isl_space_from_domain(space);
9249 space = isl_space_add_dims(space, isl_dim_out, 1);
9250 for (i = 0; i < n; ++i) {
9251 isl_union_pw_aff *upa;
9252 isl_pw_aff *pa;
9254 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9255 pa = isl_union_pw_aff_extract_pw_aff(upa,
9256 isl_space_copy(space));
9257 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9258 isl_union_pw_aff_free(upa);
9261 isl_space_free(space);
9262 return mpa;
9263 error:
9264 isl_space_free(space);
9265 return NULL;
9268 /* Evaluate the affine function "aff" in the void point "pnt".
9269 * In particular, return the value NaN.
9271 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9272 __isl_take isl_point *pnt)
9274 isl_ctx *ctx;
9276 ctx = isl_point_get_ctx(pnt);
9277 isl_aff_free(aff);
9278 isl_point_free(pnt);
9279 return isl_val_nan(ctx);
9282 /* Evaluate the affine expression "aff"
9283 * in the coordinates (with denominator) "pnt".
9285 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9286 __isl_keep isl_vec *pnt)
9288 isl_int n, d;
9289 isl_ctx *ctx;
9290 isl_val *v;
9292 if (!aff || !pnt)
9293 return NULL;
9295 ctx = isl_vec_get_ctx(aff);
9296 isl_int_init(n);
9297 isl_int_init(d);
9298 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9299 isl_int_mul(d, aff->el[0], pnt->el[0]);
9300 v = isl_val_rat_from_isl_int(ctx, n, d);
9301 v = isl_val_normalize(v);
9302 isl_int_clear(n);
9303 isl_int_clear(d);
9305 return v;
9308 /* Check that the domain space of "aff" is equal to "space".
9310 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9311 __isl_keep isl_space *space)
9313 isl_bool ok;
9315 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9316 if (ok < 0)
9317 return isl_stat_error;
9318 if (!ok)
9319 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9320 "incompatible spaces", return isl_stat_error);
9321 return isl_stat_ok;
9324 /* Evaluate the affine function "aff" in "pnt".
9326 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9327 __isl_take isl_point *pnt)
9329 isl_bool is_void;
9330 isl_val *v;
9331 isl_local_space *ls;
9333 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9334 goto error;
9335 is_void = isl_point_is_void(pnt);
9336 if (is_void < 0)
9337 goto error;
9338 if (is_void)
9339 return eval_void(aff, pnt);
9341 ls = isl_aff_get_domain_local_space(aff);
9342 pnt = isl_local_space_lift_point(ls, pnt);
9344 v = eval(aff->v, isl_point_peek_vec(pnt));
9346 isl_aff_free(aff);
9347 isl_point_free(pnt);
9349 return v;
9350 error:
9351 isl_aff_free(aff);
9352 isl_point_free(pnt);
9353 return NULL;