isl_tab.c: context_tab_insert_div: use isl_bool_ok
[isl.git] / isl_aff.c
blob1f3e90a19b0bfb2524c6e9f71140f9c5e9491dc4
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 int pos;
591 if (!aff)
592 return isl_bool_error;
594 if (isl_int_is_zero(aff->v->el[0]))
595 return isl_bool_false;
596 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
597 return isl_bool_ok(pos < 0);
600 /* Does "aff" represent NaN?
602 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
604 if (!aff)
605 return isl_bool_error;
607 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
610 /* Are "aff1" and "aff2" obviously equal?
612 * NaN is not equal to anything, not even to another NaN.
614 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
615 __isl_keep isl_aff *aff2)
617 isl_bool equal;
619 if (!aff1 || !aff2)
620 return isl_bool_error;
622 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
623 return isl_bool_false;
625 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
626 if (equal < 0 || !equal)
627 return equal;
629 return isl_vec_is_equal(aff1->v, aff2->v);
632 /* Return the common denominator of "aff" in "v".
634 * We cannot return anything meaningful in case of a NaN.
636 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
638 if (!aff)
639 return isl_stat_error;
640 if (isl_aff_is_nan(aff))
641 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
642 "cannot get denominator of NaN", return isl_stat_error);
643 isl_int_set(*v, aff->v->el[0]);
644 return isl_stat_ok;
647 /* Return the common denominator of "aff".
649 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
651 isl_ctx *ctx;
653 if (!aff)
654 return NULL;
656 ctx = isl_aff_get_ctx(aff);
657 if (isl_aff_is_nan(aff))
658 return isl_val_nan(ctx);
659 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
662 /* Return the constant term of "aff".
664 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
666 isl_ctx *ctx;
667 isl_val *v;
669 if (!aff)
670 return NULL;
672 ctx = isl_aff_get_ctx(aff);
673 if (isl_aff_is_nan(aff))
674 return isl_val_nan(ctx);
675 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
676 return isl_val_normalize(v);
679 /* Return the coefficient of the variable of type "type" at position "pos"
680 * of "aff".
682 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
683 enum isl_dim_type type, int pos)
685 isl_ctx *ctx;
686 isl_val *v;
688 if (!aff)
689 return NULL;
691 ctx = isl_aff_get_ctx(aff);
692 if (type == isl_dim_out)
693 isl_die(ctx, isl_error_invalid,
694 "output/set dimension does not have a coefficient",
695 return NULL);
696 if (type == isl_dim_in)
697 type = isl_dim_set;
699 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
700 return NULL;
702 if (isl_aff_is_nan(aff))
703 return isl_val_nan(ctx);
704 pos += isl_local_space_offset(aff->ls, type);
705 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
706 return isl_val_normalize(v);
709 /* Return the sign of the coefficient of the variable of type "type"
710 * at position "pos" of "aff".
712 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
713 int pos)
715 isl_ctx *ctx;
717 if (!aff)
718 return 0;
720 ctx = isl_aff_get_ctx(aff);
721 if (type == isl_dim_out)
722 isl_die(ctx, isl_error_invalid,
723 "output/set dimension does not have a coefficient",
724 return 0);
725 if (type == isl_dim_in)
726 type = isl_dim_set;
728 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
729 return 0;
731 pos += isl_local_space_offset(aff->ls, type);
732 return isl_int_sgn(aff->v->el[1 + pos]);
735 /* Replace the numerator of the constant term of "aff" by "v".
737 * A NaN is unaffected by this operation.
739 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
741 if (!aff)
742 return NULL;
743 if (isl_aff_is_nan(aff))
744 return aff;
745 aff = isl_aff_cow(aff);
746 if (!aff)
747 return NULL;
749 aff->v = isl_vec_cow(aff->v);
750 if (!aff->v)
751 return isl_aff_free(aff);
753 isl_int_set(aff->v->el[1], v);
755 return aff;
758 /* Replace the constant term of "aff" by "v".
760 * A NaN is unaffected by this operation.
762 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
763 __isl_take isl_val *v)
765 if (!aff || !v)
766 goto error;
768 if (isl_aff_is_nan(aff)) {
769 isl_val_free(v);
770 return aff;
773 if (!isl_val_is_rat(v))
774 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
775 "expecting rational value", goto error);
777 if (isl_int_eq(aff->v->el[1], v->n) &&
778 isl_int_eq(aff->v->el[0], v->d)) {
779 isl_val_free(v);
780 return aff;
783 aff = isl_aff_cow(aff);
784 if (!aff)
785 goto error;
786 aff->v = isl_vec_cow(aff->v);
787 if (!aff->v)
788 goto error;
790 if (isl_int_eq(aff->v->el[0], v->d)) {
791 isl_int_set(aff->v->el[1], v->n);
792 } else if (isl_int_is_one(v->d)) {
793 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
794 } else {
795 isl_seq_scale(aff->v->el + 1,
796 aff->v->el + 1, v->d, aff->v->size - 1);
797 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
798 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
799 aff->v = isl_vec_normalize(aff->v);
800 if (!aff->v)
801 goto error;
804 isl_val_free(v);
805 return aff;
806 error:
807 isl_aff_free(aff);
808 isl_val_free(v);
809 return NULL;
812 /* Add "v" to the constant term of "aff".
814 * A NaN is unaffected by this operation.
816 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
818 if (isl_int_is_zero(v))
819 return aff;
821 if (!aff)
822 return NULL;
823 if (isl_aff_is_nan(aff))
824 return aff;
825 aff = isl_aff_cow(aff);
826 if (!aff)
827 return NULL;
829 aff->v = isl_vec_cow(aff->v);
830 if (!aff->v)
831 return isl_aff_free(aff);
833 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
835 return aff;
838 /* Add "v" to the constant term of "aff".
840 * A NaN is unaffected by this operation.
842 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
843 __isl_take isl_val *v)
845 if (!aff || !v)
846 goto error;
848 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
849 isl_val_free(v);
850 return aff;
853 if (!isl_val_is_rat(v))
854 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
855 "expecting rational value", goto error);
857 aff = isl_aff_cow(aff);
858 if (!aff)
859 goto error;
861 aff->v = isl_vec_cow(aff->v);
862 if (!aff->v)
863 goto error;
865 if (isl_int_is_one(v->d)) {
866 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
867 } else if (isl_int_eq(aff->v->el[0], v->d)) {
868 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
869 aff->v = isl_vec_normalize(aff->v);
870 if (!aff->v)
871 goto error;
872 } else {
873 isl_seq_scale(aff->v->el + 1,
874 aff->v->el + 1, v->d, aff->v->size - 1);
875 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
876 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
877 aff->v = isl_vec_normalize(aff->v);
878 if (!aff->v)
879 goto error;
882 isl_val_free(v);
883 return aff;
884 error:
885 isl_aff_free(aff);
886 isl_val_free(v);
887 return NULL;
890 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
892 isl_int t;
894 isl_int_init(t);
895 isl_int_set_si(t, v);
896 aff = isl_aff_add_constant(aff, t);
897 isl_int_clear(t);
899 return aff;
902 /* Add "v" to the numerator of the constant term of "aff".
904 * A NaN is unaffected by this operation.
906 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
908 if (isl_int_is_zero(v))
909 return aff;
911 if (!aff)
912 return NULL;
913 if (isl_aff_is_nan(aff))
914 return aff;
915 aff = isl_aff_cow(aff);
916 if (!aff)
917 return NULL;
919 aff->v = isl_vec_cow(aff->v);
920 if (!aff->v)
921 return isl_aff_free(aff);
923 isl_int_add(aff->v->el[1], aff->v->el[1], v);
925 return aff;
928 /* Add "v" to the numerator of the constant term of "aff".
930 * A NaN is unaffected by this operation.
932 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
934 isl_int t;
936 if (v == 0)
937 return aff;
939 isl_int_init(t);
940 isl_int_set_si(t, v);
941 aff = isl_aff_add_constant_num(aff, t);
942 isl_int_clear(t);
944 return aff;
947 /* Replace the numerator of the constant term of "aff" by "v".
949 * A NaN is unaffected by this operation.
951 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
953 if (!aff)
954 return NULL;
955 if (isl_aff_is_nan(aff))
956 return aff;
957 aff = isl_aff_cow(aff);
958 if (!aff)
959 return NULL;
961 aff->v = isl_vec_cow(aff->v);
962 if (!aff->v)
963 return isl_aff_free(aff);
965 isl_int_set_si(aff->v->el[1], v);
967 return aff;
970 /* Replace the numerator of the coefficient of the variable of type "type"
971 * at position "pos" of "aff" by "v".
973 * A NaN is unaffected by this operation.
975 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
976 enum isl_dim_type type, int pos, isl_int v)
978 if (!aff)
979 return NULL;
981 if (type == isl_dim_out)
982 isl_die(aff->v->ctx, isl_error_invalid,
983 "output/set dimension does not have a coefficient",
984 return isl_aff_free(aff));
985 if (type == isl_dim_in)
986 type = isl_dim_set;
988 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
989 return isl_aff_free(aff);
991 if (isl_aff_is_nan(aff))
992 return aff;
993 aff = isl_aff_cow(aff);
994 if (!aff)
995 return NULL;
997 aff->v = isl_vec_cow(aff->v);
998 if (!aff->v)
999 return isl_aff_free(aff);
1001 pos += isl_local_space_offset(aff->ls, type);
1002 isl_int_set(aff->v->el[1 + pos], v);
1004 return aff;
1007 /* Replace the numerator of the coefficient of the variable of type "type"
1008 * at position "pos" of "aff" by "v".
1010 * A NaN is unaffected by this operation.
1012 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1013 enum isl_dim_type type, int pos, int v)
1015 if (!aff)
1016 return NULL;
1018 if (type == isl_dim_out)
1019 isl_die(aff->v->ctx, isl_error_invalid,
1020 "output/set dimension does not have a coefficient",
1021 return isl_aff_free(aff));
1022 if (type == isl_dim_in)
1023 type = isl_dim_set;
1025 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1026 return isl_aff_free(aff);
1028 if (isl_aff_is_nan(aff))
1029 return aff;
1030 pos += isl_local_space_offset(aff->ls, type);
1031 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1032 return aff;
1034 aff = isl_aff_cow(aff);
1035 if (!aff)
1036 return NULL;
1038 aff->v = isl_vec_cow(aff->v);
1039 if (!aff->v)
1040 return isl_aff_free(aff);
1042 isl_int_set_si(aff->v->el[1 + pos], v);
1044 return aff;
1047 /* Replace the coefficient of the variable of type "type" at position "pos"
1048 * of "aff" by "v".
1050 * A NaN is unaffected by this operation.
1052 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1053 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1055 if (!aff || !v)
1056 goto error;
1058 if (type == isl_dim_out)
1059 isl_die(aff->v->ctx, isl_error_invalid,
1060 "output/set dimension does not have a coefficient",
1061 goto error);
1062 if (type == isl_dim_in)
1063 type = isl_dim_set;
1065 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1066 return isl_aff_free(aff);
1068 if (isl_aff_is_nan(aff)) {
1069 isl_val_free(v);
1070 return aff;
1072 if (!isl_val_is_rat(v))
1073 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1074 "expecting rational value", goto error);
1076 pos += isl_local_space_offset(aff->ls, type);
1077 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1078 isl_int_eq(aff->v->el[0], v->d)) {
1079 isl_val_free(v);
1080 return aff;
1083 aff = isl_aff_cow(aff);
1084 if (!aff)
1085 goto error;
1086 aff->v = isl_vec_cow(aff->v);
1087 if (!aff->v)
1088 goto error;
1090 if (isl_int_eq(aff->v->el[0], v->d)) {
1091 isl_int_set(aff->v->el[1 + pos], v->n);
1092 } else if (isl_int_is_one(v->d)) {
1093 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1094 } else {
1095 isl_seq_scale(aff->v->el + 1,
1096 aff->v->el + 1, v->d, aff->v->size - 1);
1097 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1098 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1099 aff->v = isl_vec_normalize(aff->v);
1100 if (!aff->v)
1101 goto error;
1104 isl_val_free(v);
1105 return aff;
1106 error:
1107 isl_aff_free(aff);
1108 isl_val_free(v);
1109 return NULL;
1112 /* Add "v" to the coefficient of the variable of type "type"
1113 * at position "pos" of "aff".
1115 * A NaN is unaffected by this operation.
1117 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1118 enum isl_dim_type type, int pos, isl_int v)
1120 if (!aff)
1121 return NULL;
1123 if (type == isl_dim_out)
1124 isl_die(aff->v->ctx, isl_error_invalid,
1125 "output/set dimension does not have a coefficient",
1126 return isl_aff_free(aff));
1127 if (type == isl_dim_in)
1128 type = isl_dim_set;
1130 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1131 return isl_aff_free(aff);
1133 if (isl_aff_is_nan(aff))
1134 return aff;
1135 aff = isl_aff_cow(aff);
1136 if (!aff)
1137 return NULL;
1139 aff->v = isl_vec_cow(aff->v);
1140 if (!aff->v)
1141 return isl_aff_free(aff);
1143 pos += isl_local_space_offset(aff->ls, type);
1144 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1146 return aff;
1149 /* Add "v" to the coefficient of the variable of type "type"
1150 * at position "pos" of "aff".
1152 * A NaN is unaffected by this operation.
1154 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1155 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1157 if (!aff || !v)
1158 goto error;
1160 if (isl_val_is_zero(v)) {
1161 isl_val_free(v);
1162 return aff;
1165 if (type == isl_dim_out)
1166 isl_die(aff->v->ctx, isl_error_invalid,
1167 "output/set dimension does not have a coefficient",
1168 goto error);
1169 if (type == isl_dim_in)
1170 type = isl_dim_set;
1172 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1173 goto error;
1175 if (isl_aff_is_nan(aff)) {
1176 isl_val_free(v);
1177 return aff;
1179 if (!isl_val_is_rat(v))
1180 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1181 "expecting rational value", goto error);
1183 aff = isl_aff_cow(aff);
1184 if (!aff)
1185 goto error;
1187 aff->v = isl_vec_cow(aff->v);
1188 if (!aff->v)
1189 goto error;
1191 pos += isl_local_space_offset(aff->ls, type);
1192 if (isl_int_is_one(v->d)) {
1193 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1194 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1195 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1196 aff->v = isl_vec_normalize(aff->v);
1197 if (!aff->v)
1198 goto error;
1199 } else {
1200 isl_seq_scale(aff->v->el + 1,
1201 aff->v->el + 1, v->d, aff->v->size - 1);
1202 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1203 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1204 aff->v = isl_vec_normalize(aff->v);
1205 if (!aff->v)
1206 goto error;
1209 isl_val_free(v);
1210 return aff;
1211 error:
1212 isl_aff_free(aff);
1213 isl_val_free(v);
1214 return NULL;
1217 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1218 enum isl_dim_type type, int pos, int v)
1220 isl_int t;
1222 isl_int_init(t);
1223 isl_int_set_si(t, v);
1224 aff = isl_aff_add_coefficient(aff, type, pos, t);
1225 isl_int_clear(t);
1227 return aff;
1230 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1232 if (!aff)
1233 return NULL;
1235 return isl_local_space_get_div(aff->ls, pos);
1238 /* Return the negation of "aff".
1240 * As a special case, -NaN = NaN.
1242 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1244 if (!aff)
1245 return NULL;
1246 if (isl_aff_is_nan(aff))
1247 return aff;
1248 aff = isl_aff_cow(aff);
1249 if (!aff)
1250 return NULL;
1251 aff->v = isl_vec_cow(aff->v);
1252 if (!aff->v)
1253 return isl_aff_free(aff);
1255 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1257 return aff;
1260 /* Remove divs from the local space that do not appear in the affine
1261 * expression.
1262 * We currently only remove divs at the end.
1263 * Some intermediate divs may also not appear directly in the affine
1264 * expression, but we would also need to check that no other divs are
1265 * defined in terms of them.
1267 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1269 int pos;
1270 int off;
1271 isl_size n;
1273 if (!aff)
1274 return NULL;
1276 n = isl_local_space_dim(aff->ls, isl_dim_div);
1277 if (n < 0)
1278 return isl_aff_free(aff);
1279 off = isl_local_space_offset(aff->ls, isl_dim_div);
1281 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1282 if (pos == n)
1283 return aff;
1285 aff = isl_aff_cow(aff);
1286 if (!aff)
1287 return NULL;
1289 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1290 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1291 if (!aff->ls || !aff->v)
1292 return isl_aff_free(aff);
1294 return aff;
1297 /* Look for any divs in the aff->ls with a denominator equal to one
1298 * and plug them into the affine expression and any subsequent divs
1299 * that may reference the div.
1301 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1303 int i;
1304 isl_size n;
1305 int len;
1306 isl_int v;
1307 isl_vec *vec;
1308 isl_local_space *ls;
1309 unsigned pos;
1311 if (!aff)
1312 return NULL;
1314 n = isl_local_space_dim(aff->ls, isl_dim_div);
1315 if (n < 0)
1316 return isl_aff_free(aff);
1317 len = aff->v->size;
1318 for (i = 0; i < n; ++i) {
1319 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1320 continue;
1321 ls = isl_local_space_copy(aff->ls);
1322 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1323 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1324 vec = isl_vec_copy(aff->v);
1325 vec = isl_vec_cow(vec);
1326 if (!ls || !vec)
1327 goto error;
1329 isl_int_init(v);
1331 pos = isl_local_space_offset(aff->ls, isl_dim_div) + i;
1332 isl_seq_substitute(vec->el, pos, aff->ls->div->row[i],
1333 len, len, v);
1335 isl_int_clear(v);
1337 isl_vec_free(aff->v);
1338 aff->v = vec;
1339 isl_local_space_free(aff->ls);
1340 aff->ls = ls;
1343 return aff;
1344 error:
1345 isl_vec_free(vec);
1346 isl_local_space_free(ls);
1347 return isl_aff_free(aff);
1350 /* Look for any divs j that appear with a unit coefficient inside
1351 * the definitions of other divs i and plug them into the definitions
1352 * of the divs i.
1354 * In particular, an expression of the form
1356 * floor((f(..) + floor(g(..)/n))/m)
1358 * is simplified to
1360 * floor((n * f(..) + g(..))/(n * m))
1362 * This simplification is correct because we can move the expression
1363 * f(..) into the inner floor in the original expression to obtain
1365 * floor(floor((n * f(..) + g(..))/n)/m)
1367 * from which we can derive the simplified expression.
1369 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1371 int i, j;
1372 isl_size n;
1373 int off;
1375 if (!aff)
1376 return NULL;
1378 n = isl_local_space_dim(aff->ls, isl_dim_div);
1379 if (n < 0)
1380 return isl_aff_free(aff);
1381 off = isl_local_space_offset(aff->ls, isl_dim_div);
1382 for (i = 1; i < n; ++i) {
1383 for (j = 0; j < i; ++j) {
1384 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1385 continue;
1386 aff->ls = isl_local_space_substitute_seq(aff->ls,
1387 isl_dim_div, j, aff->ls->div->row[j],
1388 aff->v->size, i, 1);
1389 if (!aff->ls)
1390 return isl_aff_free(aff);
1394 return aff;
1397 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1399 * Even though this function is only called on isl_affs with a single
1400 * reference, we are careful to only change aff->v and aff->ls together.
1402 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1404 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1405 isl_local_space *ls;
1406 isl_vec *v;
1408 ls = isl_local_space_copy(aff->ls);
1409 ls = isl_local_space_swap_div(ls, a, b);
1410 v = isl_vec_copy(aff->v);
1411 v = isl_vec_cow(v);
1412 if (!ls || !v)
1413 goto error;
1415 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1416 isl_vec_free(aff->v);
1417 aff->v = v;
1418 isl_local_space_free(aff->ls);
1419 aff->ls = ls;
1421 return aff;
1422 error:
1423 isl_vec_free(v);
1424 isl_local_space_free(ls);
1425 return isl_aff_free(aff);
1428 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1430 * We currently do not actually remove div "b", but simply add its
1431 * coefficient to that of "a" and then zero it out.
1433 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1435 unsigned off = isl_local_space_offset(aff->ls, isl_dim_div);
1437 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1438 return aff;
1440 aff->v = isl_vec_cow(aff->v);
1441 if (!aff->v)
1442 return isl_aff_free(aff);
1444 isl_int_add(aff->v->el[1 + off + a],
1445 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1446 isl_int_set_si(aff->v->el[1 + off + b], 0);
1448 return aff;
1451 /* Sort the divs in the local space of "aff" according to
1452 * the comparison function "cmp_row" in isl_local_space.c,
1453 * combining the coefficients of identical divs.
1455 * Reordering divs does not change the semantics of "aff",
1456 * so there is no need to call isl_aff_cow.
1457 * Moreover, this function is currently only called on isl_affs
1458 * with a single reference.
1460 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1462 isl_size n;
1463 int i, j;
1465 n = isl_aff_dim(aff, isl_dim_div);
1466 if (n < 0)
1467 return isl_aff_free(aff);
1468 for (i = 1; i < n; ++i) {
1469 for (j = i - 1; j >= 0; --j) {
1470 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1471 if (cmp < 0)
1472 break;
1473 if (cmp == 0)
1474 aff = merge_divs(aff, j, j + 1);
1475 else
1476 aff = swap_div(aff, j, j + 1);
1477 if (!aff)
1478 return NULL;
1482 return aff;
1485 /* Normalize the representation of "aff".
1487 * This function should only be called of "new" isl_affs, i.e.,
1488 * with only a single reference. We therefore do not need to
1489 * worry about affecting other instances.
1491 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1493 if (!aff)
1494 return NULL;
1495 aff->v = isl_vec_normalize(aff->v);
1496 if (!aff->v)
1497 return isl_aff_free(aff);
1498 aff = plug_in_integral_divs(aff);
1499 aff = plug_in_unit_divs(aff);
1500 aff = sort_divs(aff);
1501 aff = isl_aff_remove_unused_divs(aff);
1502 return aff;
1505 /* Given f, return floor(f).
1506 * If f is an integer expression, then just return f.
1507 * If f is a constant, then return the constant floor(f).
1508 * Otherwise, if f = g/m, write g = q m + r,
1509 * create a new div d = [r/m] and return the expression q + d.
1510 * The coefficients in r are taken to lie between -m/2 and m/2.
1512 * reduce_div_coefficients performs the same normalization.
1514 * As a special case, floor(NaN) = NaN.
1516 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1518 int i;
1519 int size;
1520 isl_ctx *ctx;
1521 isl_vec *div;
1523 if (!aff)
1524 return NULL;
1526 if (isl_aff_is_nan(aff))
1527 return aff;
1528 if (isl_int_is_one(aff->v->el[0]))
1529 return aff;
1531 aff = isl_aff_cow(aff);
1532 if (!aff)
1533 return NULL;
1535 aff->v = isl_vec_cow(aff->v);
1536 if (!aff->v)
1537 return isl_aff_free(aff);
1539 if (isl_aff_is_cst(aff)) {
1540 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1541 isl_int_set_si(aff->v->el[0], 1);
1542 return aff;
1545 div = isl_vec_copy(aff->v);
1546 div = isl_vec_cow(div);
1547 if (!div)
1548 return isl_aff_free(aff);
1550 ctx = isl_aff_get_ctx(aff);
1551 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1552 for (i = 1; i < aff->v->size; ++i) {
1553 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1554 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1555 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1556 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1557 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1561 aff->ls = isl_local_space_add_div(aff->ls, div);
1562 if (!aff->ls)
1563 return isl_aff_free(aff);
1565 size = aff->v->size;
1566 aff->v = isl_vec_extend(aff->v, size + 1);
1567 if (!aff->v)
1568 return isl_aff_free(aff);
1569 isl_int_set_si(aff->v->el[0], 1);
1570 isl_int_set_si(aff->v->el[size], 1);
1572 aff = isl_aff_normalize(aff);
1574 return aff;
1577 /* Compute
1579 * aff mod m = aff - m * floor(aff/m)
1581 * with m an integer value.
1583 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1584 __isl_take isl_val *m)
1586 isl_aff *res;
1588 if (!aff || !m)
1589 goto error;
1591 if (!isl_val_is_int(m))
1592 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1593 "expecting integer modulo", goto error);
1595 res = isl_aff_copy(aff);
1596 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1597 aff = isl_aff_floor(aff);
1598 aff = isl_aff_scale_val(aff, m);
1599 res = isl_aff_sub(res, aff);
1601 return res;
1602 error:
1603 isl_aff_free(aff);
1604 isl_val_free(m);
1605 return NULL;
1608 /* Compute
1610 * pwaff mod m = pwaff - m * floor(pwaff/m)
1612 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1614 isl_pw_aff *res;
1616 res = isl_pw_aff_copy(pwaff);
1617 pwaff = isl_pw_aff_scale_down(pwaff, m);
1618 pwaff = isl_pw_aff_floor(pwaff);
1619 pwaff = isl_pw_aff_scale(pwaff, m);
1620 res = isl_pw_aff_sub(res, pwaff);
1622 return res;
1625 /* Compute
1627 * pa mod m = pa - m * floor(pa/m)
1629 * with m an integer value.
1631 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1632 __isl_take isl_val *m)
1634 if (!pa || !m)
1635 goto error;
1636 if (!isl_val_is_int(m))
1637 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1638 "expecting integer modulo", goto error);
1639 pa = isl_pw_aff_mod(pa, m->n);
1640 isl_val_free(m);
1641 return pa;
1642 error:
1643 isl_pw_aff_free(pa);
1644 isl_val_free(m);
1645 return NULL;
1648 /* Given f, return ceil(f).
1649 * If f is an integer expression, then just return f.
1650 * Otherwise, let f be the expression
1652 * e/m
1654 * then return
1656 * floor((e + m - 1)/m)
1658 * As a special case, ceil(NaN) = NaN.
1660 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1662 if (!aff)
1663 return NULL;
1665 if (isl_aff_is_nan(aff))
1666 return aff;
1667 if (isl_int_is_one(aff->v->el[0]))
1668 return aff;
1670 aff = isl_aff_cow(aff);
1671 if (!aff)
1672 return NULL;
1673 aff->v = isl_vec_cow(aff->v);
1674 if (!aff->v)
1675 return isl_aff_free(aff);
1677 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1678 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1679 aff = isl_aff_floor(aff);
1681 return aff;
1684 /* Apply the expansion computed by isl_merge_divs.
1685 * The expansion itself is given by "exp" while the resulting
1686 * list of divs is given by "div".
1688 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1689 __isl_take isl_mat *div, int *exp)
1691 isl_size old_n_div;
1692 isl_size new_n_div;
1693 int offset;
1695 aff = isl_aff_cow(aff);
1696 if (!aff || !div)
1697 goto error;
1699 old_n_div = isl_local_space_dim(aff->ls, isl_dim_div);
1700 new_n_div = isl_mat_rows(div);
1701 if (old_n_div < 0 || new_n_div < 0)
1702 goto error;
1703 offset = 1 + isl_local_space_offset(aff->ls, isl_dim_div);
1705 aff->v = isl_vec_expand(aff->v, offset, old_n_div, exp, new_n_div);
1706 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1707 if (!aff->v || !aff->ls)
1708 return isl_aff_free(aff);
1709 return aff;
1710 error:
1711 isl_aff_free(aff);
1712 isl_mat_free(div);
1713 return NULL;
1716 /* Add two affine expressions that live in the same local space.
1718 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1719 __isl_take isl_aff *aff2)
1721 isl_int gcd, f;
1723 aff1 = isl_aff_cow(aff1);
1724 if (!aff1 || !aff2)
1725 goto error;
1727 aff1->v = isl_vec_cow(aff1->v);
1728 if (!aff1->v)
1729 goto error;
1731 isl_int_init(gcd);
1732 isl_int_init(f);
1733 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1734 isl_int_divexact(f, aff2->v->el[0], gcd);
1735 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1736 isl_int_divexact(f, aff1->v->el[0], gcd);
1737 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1738 isl_int_divexact(f, aff2->v->el[0], gcd);
1739 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1740 isl_int_clear(f);
1741 isl_int_clear(gcd);
1743 isl_aff_free(aff2);
1744 return aff1;
1745 error:
1746 isl_aff_free(aff1);
1747 isl_aff_free(aff2);
1748 return NULL;
1751 /* Return the sum of "aff1" and "aff2".
1753 * If either of the two is NaN, then the result is NaN.
1755 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1756 __isl_take isl_aff *aff2)
1758 isl_ctx *ctx;
1759 int *exp1 = NULL;
1760 int *exp2 = NULL;
1761 isl_mat *div;
1762 isl_size n_div1, n_div2;
1764 if (!aff1 || !aff2)
1765 goto error;
1767 ctx = isl_aff_get_ctx(aff1);
1768 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1769 isl_die(ctx, isl_error_invalid,
1770 "spaces don't match", goto error);
1772 if (isl_aff_is_nan(aff1)) {
1773 isl_aff_free(aff2);
1774 return aff1;
1776 if (isl_aff_is_nan(aff2)) {
1777 isl_aff_free(aff1);
1778 return aff2;
1781 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1782 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1783 if (n_div1 < 0 || n_div2 < 0)
1784 goto error;
1785 if (n_div1 == 0 && n_div2 == 0)
1786 return add_expanded(aff1, aff2);
1788 exp1 = isl_alloc_array(ctx, int, n_div1);
1789 exp2 = isl_alloc_array(ctx, int, n_div2);
1790 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1791 goto error;
1793 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1794 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1795 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1796 free(exp1);
1797 free(exp2);
1799 return add_expanded(aff1, aff2);
1800 error:
1801 free(exp1);
1802 free(exp2);
1803 isl_aff_free(aff1);
1804 isl_aff_free(aff2);
1805 return NULL;
1808 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1809 __isl_take isl_aff *aff2)
1811 return isl_aff_add(aff1, isl_aff_neg(aff2));
1814 /* Return the result of scaling "aff" by a factor of "f".
1816 * As a special case, f * NaN = NaN.
1818 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1820 isl_int gcd;
1822 if (!aff)
1823 return NULL;
1824 if (isl_aff_is_nan(aff))
1825 return aff;
1827 if (isl_int_is_one(f))
1828 return aff;
1830 aff = isl_aff_cow(aff);
1831 if (!aff)
1832 return NULL;
1833 aff->v = isl_vec_cow(aff->v);
1834 if (!aff->v)
1835 return isl_aff_free(aff);
1837 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1838 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1839 return aff;
1842 isl_int_init(gcd);
1843 isl_int_gcd(gcd, aff->v->el[0], f);
1844 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1845 isl_int_divexact(gcd, f, gcd);
1846 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1847 isl_int_clear(gcd);
1849 return aff;
1852 /* Multiple "aff" by "v".
1854 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1855 __isl_take isl_val *v)
1857 if (!aff || !v)
1858 goto error;
1860 if (isl_val_is_one(v)) {
1861 isl_val_free(v);
1862 return aff;
1865 if (!isl_val_is_rat(v))
1866 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1867 "expecting rational factor", goto error);
1869 aff = isl_aff_scale(aff, v->n);
1870 aff = isl_aff_scale_down(aff, v->d);
1872 isl_val_free(v);
1873 return aff;
1874 error:
1875 isl_aff_free(aff);
1876 isl_val_free(v);
1877 return NULL;
1880 /* Return the result of scaling "aff" down by a factor of "f".
1882 * As a special case, NaN/f = NaN.
1884 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1886 isl_int gcd;
1888 if (!aff)
1889 return NULL;
1890 if (isl_aff_is_nan(aff))
1891 return aff;
1893 if (isl_int_is_one(f))
1894 return aff;
1896 aff = isl_aff_cow(aff);
1897 if (!aff)
1898 return NULL;
1900 if (isl_int_is_zero(f))
1901 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1902 "cannot scale down by zero", return isl_aff_free(aff));
1904 aff->v = isl_vec_cow(aff->v);
1905 if (!aff->v)
1906 return isl_aff_free(aff);
1908 isl_int_init(gcd);
1909 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1910 isl_int_gcd(gcd, gcd, f);
1911 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1912 isl_int_divexact(gcd, f, gcd);
1913 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1914 isl_int_clear(gcd);
1916 return aff;
1919 /* Divide "aff" by "v".
1921 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1922 __isl_take isl_val *v)
1924 if (!aff || !v)
1925 goto error;
1927 if (isl_val_is_one(v)) {
1928 isl_val_free(v);
1929 return aff;
1932 if (!isl_val_is_rat(v))
1933 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1934 "expecting rational factor", goto error);
1935 if (!isl_val_is_pos(v))
1936 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1937 "factor needs to be positive", goto error);
1939 aff = isl_aff_scale(aff, v->d);
1940 aff = isl_aff_scale_down(aff, v->n);
1942 isl_val_free(v);
1943 return aff;
1944 error:
1945 isl_aff_free(aff);
1946 isl_val_free(v);
1947 return NULL;
1950 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
1952 isl_int v;
1954 if (f == 1)
1955 return aff;
1957 isl_int_init(v);
1958 isl_int_set_ui(v, f);
1959 aff = isl_aff_scale_down(aff, v);
1960 isl_int_clear(v);
1962 return aff;
1965 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
1966 enum isl_dim_type type, unsigned pos, const char *s)
1968 aff = isl_aff_cow(aff);
1969 if (!aff)
1970 return NULL;
1971 if (type == isl_dim_out)
1972 isl_die(aff->v->ctx, isl_error_invalid,
1973 "cannot set name of output/set dimension",
1974 return isl_aff_free(aff));
1975 if (type == isl_dim_in)
1976 type = isl_dim_set;
1977 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
1978 if (!aff->ls)
1979 return isl_aff_free(aff);
1981 return aff;
1984 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
1985 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
1987 aff = isl_aff_cow(aff);
1988 if (!aff)
1989 goto error;
1990 if (type == isl_dim_out)
1991 isl_die(aff->v->ctx, isl_error_invalid,
1992 "cannot set name of output/set dimension",
1993 goto error);
1994 if (type == isl_dim_in)
1995 type = isl_dim_set;
1996 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
1997 if (!aff->ls)
1998 return isl_aff_free(aff);
2000 return aff;
2001 error:
2002 isl_id_free(id);
2003 isl_aff_free(aff);
2004 return NULL;
2007 /* Replace the identifier of the input tuple of "aff" by "id".
2008 * type is currently required to be equal to isl_dim_in
2010 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2011 enum isl_dim_type type, __isl_take isl_id *id)
2013 aff = isl_aff_cow(aff);
2014 if (!aff)
2015 goto error;
2016 if (type != isl_dim_in)
2017 isl_die(aff->v->ctx, isl_error_invalid,
2018 "cannot only set id of input tuple", goto error);
2019 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2020 if (!aff->ls)
2021 return isl_aff_free(aff);
2023 return aff;
2024 error:
2025 isl_id_free(id);
2026 isl_aff_free(aff);
2027 return NULL;
2030 /* Exploit the equalities in "eq" to simplify the affine expression
2031 * and the expressions of the integer divisions in the local space.
2032 * The integer divisions in this local space are assumed to appear
2033 * as regular dimensions in "eq".
2035 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2036 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2038 int i, j;
2039 unsigned o_div;
2040 unsigned n_div;
2042 if (!eq)
2043 goto error;
2044 if (eq->n_eq == 0) {
2045 isl_basic_set_free(eq);
2046 return aff;
2049 aff = isl_aff_cow(aff);
2050 if (!aff)
2051 goto error;
2053 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2054 isl_basic_set_copy(eq));
2055 aff->v = isl_vec_cow(aff->v);
2056 if (!aff->ls || !aff->v)
2057 goto error;
2059 o_div = isl_basic_set_offset(eq, isl_dim_div);
2060 n_div = eq->n_div;
2061 for (i = 0; i < eq->n_eq; ++i) {
2062 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2063 if (j < 0 || j == 0 || j >= o_div)
2064 continue;
2066 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2067 &aff->v->el[0]);
2070 isl_basic_set_free(eq);
2071 aff = isl_aff_normalize(aff);
2072 return aff;
2073 error:
2074 isl_basic_set_free(eq);
2075 isl_aff_free(aff);
2076 return NULL;
2079 /* Exploit the equalities in "eq" to simplify the affine expression
2080 * and the expressions of the integer divisions in the local space.
2082 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2083 __isl_take isl_basic_set *eq)
2085 isl_size n_div;
2087 if (!aff || !eq)
2088 goto error;
2089 n_div = isl_local_space_dim(aff->ls, isl_dim_div);
2090 if (n_div < 0)
2091 goto error;
2092 if (n_div > 0)
2093 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2094 return isl_aff_substitute_equalities_lifted(aff, eq);
2095 error:
2096 isl_basic_set_free(eq);
2097 isl_aff_free(aff);
2098 return NULL;
2101 /* Look for equalities among the variables shared by context and aff
2102 * and the integer divisions of aff, if any.
2103 * The equalities are then used to eliminate coefficients and/or integer
2104 * divisions from aff.
2106 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2107 __isl_take isl_set *context)
2109 isl_local_space *ls;
2110 isl_basic_set *hull;
2112 ls = isl_aff_get_domain_local_space(aff);
2113 context = isl_local_space_lift_set(ls, context);
2115 hull = isl_set_affine_hull(context);
2116 return isl_aff_substitute_equalities_lifted(aff, hull);
2119 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2120 __isl_take isl_set *context)
2122 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2123 dom_context = isl_set_intersect_params(dom_context, context);
2124 return isl_aff_gist(aff, dom_context);
2127 /* Return a basic set containing those elements in the space
2128 * of aff where it is positive. "rational" should not be set.
2130 * If "aff" is NaN, then it is not positive.
2132 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2133 int rational)
2135 isl_constraint *ineq;
2136 isl_basic_set *bset;
2137 isl_val *c;
2139 if (!aff)
2140 return NULL;
2141 if (isl_aff_is_nan(aff)) {
2142 isl_space *space = isl_aff_get_domain_space(aff);
2143 isl_aff_free(aff);
2144 return isl_basic_set_empty(space);
2146 if (rational)
2147 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2148 "rational sets not supported", goto error);
2150 ineq = isl_inequality_from_aff(aff);
2151 c = isl_constraint_get_constant_val(ineq);
2152 c = isl_val_sub_ui(c, 1);
2153 ineq = isl_constraint_set_constant_val(ineq, c);
2155 bset = isl_basic_set_from_constraint(ineq);
2156 bset = isl_basic_set_simplify(bset);
2157 return bset;
2158 error:
2159 isl_aff_free(aff);
2160 return NULL;
2163 /* Return a basic set containing those elements in the space
2164 * of aff where it is non-negative.
2165 * If "rational" is set, then return a rational basic set.
2167 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2169 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2170 __isl_take isl_aff *aff, int rational)
2172 isl_constraint *ineq;
2173 isl_basic_set *bset;
2175 if (!aff)
2176 return NULL;
2177 if (isl_aff_is_nan(aff)) {
2178 isl_space *space = isl_aff_get_domain_space(aff);
2179 isl_aff_free(aff);
2180 return isl_basic_set_empty(space);
2183 ineq = isl_inequality_from_aff(aff);
2185 bset = isl_basic_set_from_constraint(ineq);
2186 if (rational)
2187 bset = isl_basic_set_set_rational(bset);
2188 bset = isl_basic_set_simplify(bset);
2189 return bset;
2192 /* Return a basic set containing those elements in the space
2193 * of aff where it is non-negative.
2195 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2197 return aff_nonneg_basic_set(aff, 0);
2200 /* Return a basic set containing those elements in the domain space
2201 * of "aff" where it is positive.
2203 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2205 aff = isl_aff_add_constant_num_si(aff, -1);
2206 return isl_aff_nonneg_basic_set(aff);
2209 /* Return a basic set containing those elements in the domain space
2210 * of aff where it is negative.
2212 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2214 aff = isl_aff_neg(aff);
2215 return isl_aff_pos_basic_set(aff);
2218 /* Return a basic set containing those elements in the space
2219 * of aff where it is zero.
2220 * If "rational" is set, then return a rational basic set.
2222 * If "aff" is NaN, then it is not zero.
2224 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2225 int rational)
2227 isl_constraint *ineq;
2228 isl_basic_set *bset;
2230 if (!aff)
2231 return NULL;
2232 if (isl_aff_is_nan(aff)) {
2233 isl_space *space = isl_aff_get_domain_space(aff);
2234 isl_aff_free(aff);
2235 return isl_basic_set_empty(space);
2238 ineq = isl_equality_from_aff(aff);
2240 bset = isl_basic_set_from_constraint(ineq);
2241 if (rational)
2242 bset = isl_basic_set_set_rational(bset);
2243 bset = isl_basic_set_simplify(bset);
2244 return bset;
2247 /* Return a basic set containing those elements in the space
2248 * of aff where it is zero.
2250 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2252 return aff_zero_basic_set(aff, 0);
2255 /* Return a basic set containing those elements in the shared space
2256 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2258 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2259 __isl_take isl_aff *aff2)
2261 aff1 = isl_aff_sub(aff1, aff2);
2263 return isl_aff_nonneg_basic_set(aff1);
2266 /* Return a basic set containing those elements in the shared domain space
2267 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2269 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2270 __isl_take isl_aff *aff2)
2272 aff1 = isl_aff_sub(aff1, aff2);
2274 return isl_aff_pos_basic_set(aff1);
2277 /* Return a set containing those elements in the shared space
2278 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2280 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2281 __isl_take isl_aff *aff2)
2283 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2286 /* Return a set containing those elements in the shared domain space
2287 * of aff1 and aff2 where aff1 is greater than aff2.
2289 * If either of the two inputs is NaN, then the result is empty,
2290 * as comparisons with NaN always return false.
2292 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2293 __isl_take isl_aff *aff2)
2295 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2298 /* Return a basic set containing those elements in the shared space
2299 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2301 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2302 __isl_take isl_aff *aff2)
2304 return isl_aff_ge_basic_set(aff2, aff1);
2307 /* Return a basic set containing those elements in the shared domain space
2308 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2310 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2311 __isl_take isl_aff *aff2)
2313 return isl_aff_gt_basic_set(aff2, aff1);
2316 /* Return a set containing those elements in the shared space
2317 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2319 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2320 __isl_take isl_aff *aff2)
2322 return isl_aff_ge_set(aff2, aff1);
2325 /* Return a set containing those elements in the shared domain space
2326 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2328 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2329 __isl_take isl_aff *aff2)
2331 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2334 /* Return a basic set containing those elements in the shared space
2335 * of aff1 and aff2 where aff1 and aff2 are equal.
2337 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2338 __isl_take isl_aff *aff2)
2340 aff1 = isl_aff_sub(aff1, aff2);
2342 return isl_aff_zero_basic_set(aff1);
2345 /* Return a set containing those elements in the shared space
2346 * of aff1 and aff2 where aff1 and aff2 are equal.
2348 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2349 __isl_take isl_aff *aff2)
2351 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2354 /* Return a set containing those elements in the shared domain space
2355 * of aff1 and aff2 where aff1 and aff2 are not equal.
2357 * If either of the two inputs is NaN, then the result is empty,
2358 * as comparisons with NaN always return false.
2360 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2361 __isl_take isl_aff *aff2)
2363 isl_set *set_lt, *set_gt;
2365 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2366 isl_aff_copy(aff2));
2367 set_gt = isl_aff_gt_set(aff1, aff2);
2368 return isl_set_union_disjoint(set_lt, set_gt);
2371 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2372 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2374 aff1 = isl_aff_add(aff1, aff2);
2375 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2376 return aff1;
2379 int isl_aff_is_empty(__isl_keep isl_aff *aff)
2381 if (!aff)
2382 return -1;
2384 return 0;
2387 #undef TYPE
2388 #define TYPE isl_aff
2389 static
2390 #include "check_type_range_templ.c"
2392 /* Check whether the given affine expression has non-zero coefficient
2393 * for any dimension in the given range or if any of these dimensions
2394 * appear with non-zero coefficients in any of the integer divisions
2395 * involved in the affine expression.
2397 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2398 enum isl_dim_type type, unsigned first, unsigned n)
2400 int i;
2401 int *active = NULL;
2402 isl_bool involves = isl_bool_false;
2404 if (!aff)
2405 return isl_bool_error;
2406 if (n == 0)
2407 return isl_bool_false;
2408 if (isl_aff_check_range(aff, type, first, n) < 0)
2409 return isl_bool_error;
2411 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2412 if (!active)
2413 goto error;
2415 first += isl_local_space_offset(aff->ls, type) - 1;
2416 for (i = 0; i < n; ++i)
2417 if (active[first + i]) {
2418 involves = isl_bool_true;
2419 break;
2422 free(active);
2424 return involves;
2425 error:
2426 free(active);
2427 return isl_bool_error;
2430 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2431 enum isl_dim_type type, unsigned first, unsigned n)
2433 isl_ctx *ctx;
2435 if (!aff)
2436 return NULL;
2437 if (type == isl_dim_out)
2438 isl_die(aff->v->ctx, isl_error_invalid,
2439 "cannot drop output/set dimension",
2440 return isl_aff_free(aff));
2441 if (type == isl_dim_in)
2442 type = isl_dim_set;
2443 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2444 return aff;
2446 ctx = isl_aff_get_ctx(aff);
2447 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2448 return isl_aff_free(aff);
2450 aff = isl_aff_cow(aff);
2451 if (!aff)
2452 return NULL;
2454 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2455 if (!aff->ls)
2456 return isl_aff_free(aff);
2458 first += 1 + isl_local_space_offset(aff->ls, type);
2459 aff->v = isl_vec_drop_els(aff->v, first, n);
2460 if (!aff->v)
2461 return isl_aff_free(aff);
2463 return aff;
2466 /* Drop the "n" domain dimensions starting at "first" from "aff",
2467 * after checking that they do not appear in the affine expression.
2469 static __isl_give isl_aff *drop_domain(__isl_take isl_aff *aff, unsigned first,
2470 unsigned n)
2472 isl_bool involves;
2474 involves = isl_aff_involves_dims(aff, isl_dim_in, first, n);
2475 if (involves < 0)
2476 return isl_aff_free(aff);
2477 if (involves)
2478 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2479 "affine expression involves some of the domain dimensions",
2480 return isl_aff_free(aff));
2481 return isl_aff_drop_dims(aff, isl_dim_in, first, n);
2484 /* Project the domain of the affine expression onto its parameter space.
2485 * The affine expression may not involve any of the domain dimensions.
2487 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2489 isl_space *space;
2490 isl_size n;
2492 n = isl_aff_dim(aff, isl_dim_in);
2493 if (n < 0)
2494 return isl_aff_free(aff);
2495 aff = drop_domain(aff, 0, n);
2496 space = isl_aff_get_domain_space(aff);
2497 space = isl_space_params(space);
2498 aff = isl_aff_reset_domain_space(aff, space);
2499 return aff;
2502 /* Check that the domain of "aff" is a product.
2504 static isl_stat check_domain_product(__isl_keep isl_aff *aff)
2506 isl_bool is_product;
2508 is_product = isl_space_is_product(isl_aff_peek_domain_space(aff));
2509 if (is_product < 0)
2510 return isl_stat_error;
2511 if (!is_product)
2512 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2513 "domain is not a product", return isl_stat_error);
2514 return isl_stat_ok;
2517 /* Given an affine function with a domain of the form [A -> B] that
2518 * does not depend on B, return the same function on domain A.
2520 __isl_give isl_aff *isl_aff_domain_factor_domain(__isl_take isl_aff *aff)
2522 isl_space *space;
2523 isl_size n, n_in;
2525 if (check_domain_product(aff) < 0)
2526 return isl_aff_free(aff);
2527 space = isl_aff_get_domain_space(aff);
2528 n = isl_space_dim(space, isl_dim_set);
2529 space = isl_space_factor_domain(space);
2530 n_in = isl_space_dim(space, isl_dim_set);
2531 if (n < 0 || n_in < 0)
2532 aff = isl_aff_free(aff);
2533 else
2534 aff = drop_domain(aff, n_in, n - n_in);
2535 aff = isl_aff_reset_domain_space(aff, space);
2536 return aff;
2539 /* Convert an affine expression defined over a parameter domain
2540 * into one that is defined over a zero-dimensional set.
2542 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2544 isl_local_space *ls;
2546 ls = isl_aff_take_domain_local_space(aff);
2547 ls = isl_local_space_set_from_params(ls);
2548 aff = isl_aff_restore_domain_local_space(aff, ls);
2550 return aff;
2553 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2554 enum isl_dim_type type, unsigned first, unsigned n)
2556 isl_ctx *ctx;
2558 if (!aff)
2559 return NULL;
2560 if (type == isl_dim_out)
2561 isl_die(aff->v->ctx, isl_error_invalid,
2562 "cannot insert output/set dimensions",
2563 return isl_aff_free(aff));
2564 if (type == isl_dim_in)
2565 type = isl_dim_set;
2566 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2567 return aff;
2569 ctx = isl_aff_get_ctx(aff);
2570 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2571 return isl_aff_free(aff);
2573 aff = isl_aff_cow(aff);
2574 if (!aff)
2575 return NULL;
2577 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2578 if (!aff->ls)
2579 return isl_aff_free(aff);
2581 first += 1 + isl_local_space_offset(aff->ls, type);
2582 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2583 if (!aff->v)
2584 return isl_aff_free(aff);
2586 return aff;
2589 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2590 enum isl_dim_type type, unsigned n)
2592 isl_size pos;
2594 pos = isl_aff_dim(aff, type);
2595 if (pos < 0)
2596 return isl_aff_free(aff);
2598 return isl_aff_insert_dims(aff, type, pos, n);
2601 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2602 enum isl_dim_type type, unsigned n)
2604 isl_size pos;
2606 pos = isl_pw_aff_dim(pwaff, type);
2607 if (pos < 0)
2608 return isl_pw_aff_free(pwaff);
2610 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2613 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2614 * to dimensions of "dst_type" at "dst_pos".
2616 * We only support moving input dimensions to parameters and vice versa.
2618 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2619 enum isl_dim_type dst_type, unsigned dst_pos,
2620 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2622 unsigned g_dst_pos;
2623 unsigned g_src_pos;
2625 if (!aff)
2626 return NULL;
2627 if (n == 0 &&
2628 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2629 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2630 return aff;
2632 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2633 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2634 "cannot move output/set dimension",
2635 return isl_aff_free(aff));
2636 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2637 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2638 "cannot move divs", return isl_aff_free(aff));
2639 if (dst_type == isl_dim_in)
2640 dst_type = isl_dim_set;
2641 if (src_type == isl_dim_in)
2642 src_type = isl_dim_set;
2644 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2645 return isl_aff_free(aff);
2646 if (dst_type == src_type)
2647 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2648 "moving dims within the same type not supported",
2649 return isl_aff_free(aff));
2651 aff = isl_aff_cow(aff);
2652 if (!aff)
2653 return NULL;
2655 g_src_pos = 1 + isl_local_space_offset(aff->ls, src_type) + src_pos;
2656 g_dst_pos = 1 + isl_local_space_offset(aff->ls, dst_type) + dst_pos;
2657 if (dst_type > src_type)
2658 g_dst_pos -= n;
2660 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2661 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2662 src_type, src_pos, n);
2663 if (!aff->v || !aff->ls)
2664 return isl_aff_free(aff);
2666 aff = sort_divs(aff);
2668 return aff;
2671 __isl_give isl_pw_aff *isl_pw_aff_from_aff(__isl_take isl_aff *aff)
2673 isl_set *dom = isl_set_universe(isl_aff_get_domain_space(aff));
2674 return isl_pw_aff_alloc(dom, aff);
2677 #define isl_aff_involves_nan isl_aff_is_nan
2679 #undef PW
2680 #define PW isl_pw_aff
2681 #undef EL
2682 #define EL isl_aff
2683 #undef EL_IS_ZERO
2684 #define EL_IS_ZERO is_empty
2685 #undef ZERO
2686 #define ZERO empty
2687 #undef IS_ZERO
2688 #define IS_ZERO is_empty
2689 #undef FIELD
2690 #define FIELD aff
2691 #undef DEFAULT_IS_ZERO
2692 #define DEFAULT_IS_ZERO 0
2694 #define NO_OPT
2695 #define NO_LIFT
2696 #define NO_MORPH
2698 #include <isl_pw_templ.c>
2699 #include <isl_pw_eval.c>
2700 #include <isl_pw_hash.c>
2701 #include <isl_pw_union_opt.c>
2703 #undef BASE
2704 #define BASE pw_aff
2706 #include <isl_union_single.c>
2707 #include <isl_union_neg.c>
2709 static __isl_give isl_set *align_params_pw_pw_set_and(
2710 __isl_take isl_pw_aff *pwaff1, __isl_take isl_pw_aff *pwaff2,
2711 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
2712 __isl_take isl_pw_aff *pwaff2))
2714 isl_bool equal_params;
2716 if (!pwaff1 || !pwaff2)
2717 goto error;
2718 equal_params = isl_space_has_equal_params(pwaff1->dim, pwaff2->dim);
2719 if (equal_params < 0)
2720 goto error;
2721 if (equal_params)
2722 return fn(pwaff1, pwaff2);
2723 if (isl_pw_aff_check_named_params(pwaff1) < 0 ||
2724 isl_pw_aff_check_named_params(pwaff2) < 0)
2725 goto error;
2726 pwaff1 = isl_pw_aff_align_params(pwaff1, isl_pw_aff_get_space(pwaff2));
2727 pwaff2 = isl_pw_aff_align_params(pwaff2, isl_pw_aff_get_space(pwaff1));
2728 return fn(pwaff1, pwaff2);
2729 error:
2730 isl_pw_aff_free(pwaff1);
2731 isl_pw_aff_free(pwaff2);
2732 return NULL;
2735 /* Align the parameters of the to isl_pw_aff arguments and
2736 * then apply a function "fn" on them that returns an isl_map.
2738 static __isl_give isl_map *align_params_pw_pw_map_and(
2739 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2740 __isl_give isl_map *(*fn)(__isl_take isl_pw_aff *pa1,
2741 __isl_take isl_pw_aff *pa2))
2743 isl_bool equal_params;
2745 if (!pa1 || !pa2)
2746 goto error;
2747 equal_params = isl_space_has_equal_params(pa1->dim, pa2->dim);
2748 if (equal_params < 0)
2749 goto error;
2750 if (equal_params)
2751 return fn(pa1, pa2);
2752 if (isl_pw_aff_check_named_params(pa1) < 0 ||
2753 isl_pw_aff_check_named_params(pa2) < 0)
2754 goto error;
2755 pa1 = isl_pw_aff_align_params(pa1, isl_pw_aff_get_space(pa2));
2756 pa2 = isl_pw_aff_align_params(pa2, isl_pw_aff_get_space(pa1));
2757 return fn(pa1, pa2);
2758 error:
2759 isl_pw_aff_free(pa1);
2760 isl_pw_aff_free(pa2);
2761 return NULL;
2764 /* Compute a piecewise quasi-affine expression with a domain that
2765 * is the union of those of pwaff1 and pwaff2 and such that on each
2766 * cell, the quasi-affine expression is the maximum of those of pwaff1
2767 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2768 * cell, then the associated expression is the defined one.
2770 static __isl_give isl_pw_aff *pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2771 __isl_take isl_pw_aff *pwaff2)
2773 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2776 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2777 __isl_take isl_pw_aff *pwaff2)
2779 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2780 &pw_aff_union_max);
2783 /* Compute a piecewise quasi-affine expression with a domain that
2784 * is the union of those of pwaff1 and pwaff2 and such that on each
2785 * cell, the quasi-affine expression is the minimum of those of pwaff1
2786 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2787 * cell, then the associated expression is the defined one.
2789 static __isl_give isl_pw_aff *pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2790 __isl_take isl_pw_aff *pwaff2)
2792 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2795 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2796 __isl_take isl_pw_aff *pwaff2)
2798 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2,
2799 &pw_aff_union_min);
2802 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2803 __isl_take isl_pw_aff *pwaff2, int max)
2805 if (max)
2806 return isl_pw_aff_union_max(pwaff1, pwaff2);
2807 else
2808 return isl_pw_aff_union_min(pwaff1, pwaff2);
2811 /* Return a set containing those elements in the domain
2812 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2813 * does not satisfy "fn" (if complement is 1).
2815 * The pieces with a NaN never belong to the result since
2816 * NaN does not satisfy any property.
2818 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2819 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational),
2820 int complement)
2822 int i;
2823 isl_set *set;
2825 if (!pwaff)
2826 return NULL;
2828 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2830 for (i = 0; i < pwaff->n; ++i) {
2831 isl_basic_set *bset;
2832 isl_set *set_i, *locus;
2833 isl_bool rational;
2835 if (isl_aff_is_nan(pwaff->p[i].aff))
2836 continue;
2838 rational = isl_set_has_rational(pwaff->p[i].set);
2839 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational);
2840 locus = isl_set_from_basic_set(bset);
2841 set_i = isl_set_copy(pwaff->p[i].set);
2842 if (complement)
2843 set_i = isl_set_subtract(set_i, locus);
2844 else
2845 set_i = isl_set_intersect(set_i, locus);
2846 set = isl_set_union_disjoint(set, set_i);
2849 isl_pw_aff_free(pwaff);
2851 return set;
2854 /* Return a set containing those elements in the domain
2855 * of "pa" where it is positive.
2857 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2859 return pw_aff_locus(pa, &aff_pos_basic_set, 0);
2862 /* Return a set containing those elements in the domain
2863 * of pwaff where it is non-negative.
2865 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2867 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0);
2870 /* Return a set containing those elements in the domain
2871 * of pwaff where it is zero.
2873 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2875 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0);
2878 /* Return a set containing those elements in the domain
2879 * of pwaff where it is not zero.
2881 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2883 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1);
2886 /* Return a set containing those elements in the shared domain
2887 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2889 * We compute the difference on the shared domain and then construct
2890 * the set of values where this difference is non-negative.
2891 * If strict is set, we first subtract 1 from the difference.
2892 * If equal is set, we only return the elements where pwaff1 and pwaff2
2893 * are equal.
2895 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2896 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2898 isl_set *set1, *set2;
2900 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2901 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2902 set1 = isl_set_intersect(set1, set2);
2903 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2904 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2905 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2907 if (strict) {
2908 isl_space *space = isl_set_get_space(set1);
2909 isl_aff *aff;
2910 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2911 aff = isl_aff_add_constant_si(aff, -1);
2912 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2913 } else
2914 isl_set_free(set1);
2916 if (equal)
2917 return isl_pw_aff_zero_set(pwaff1);
2918 return isl_pw_aff_nonneg_set(pwaff1);
2921 /* Return a set containing those elements in the shared domain
2922 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2924 static __isl_give isl_set *pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2925 __isl_take isl_pw_aff *pwaff2)
2927 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2930 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2931 __isl_take isl_pw_aff *pwaff2)
2933 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_eq_set);
2936 /* Return a set containing those elements in the shared domain
2937 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2939 static __isl_give isl_set *pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2940 __isl_take isl_pw_aff *pwaff2)
2942 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2945 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2946 __isl_take isl_pw_aff *pwaff2)
2948 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ge_set);
2951 /* Return a set containing those elements in the shared domain
2952 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2954 static __isl_give isl_set *pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2955 __isl_take isl_pw_aff *pwaff2)
2957 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2960 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2961 __isl_take isl_pw_aff *pwaff2)
2963 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_gt_set);
2966 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2967 __isl_take isl_pw_aff *pwaff2)
2969 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2972 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
2973 __isl_take isl_pw_aff *pwaff2)
2975 return isl_pw_aff_gt_set(pwaff2, pwaff1);
2978 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
2979 * where the function values are ordered in the same way as "order",
2980 * which returns a set in the shared domain of its two arguments.
2981 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
2983 * Let "pa1" and "pa2" be defined on domains A and B respectively.
2984 * We first pull back the two functions such that they are defined on
2985 * the domain [A -> B]. Then we apply "order", resulting in a set
2986 * in the space [A -> B]. Finally, we unwrap this set to obtain
2987 * a map in the space A -> B.
2989 static __isl_give isl_map *isl_pw_aff_order_map_aligned(
2990 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
2991 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
2992 __isl_take isl_pw_aff *pa2))
2994 isl_space *space1, *space2;
2995 isl_multi_aff *ma;
2996 isl_set *set;
2998 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
2999 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3000 space1 = isl_space_map_from_domain_and_range(space1, space2);
3001 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3002 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3003 ma = isl_multi_aff_range_map(space1);
3004 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3005 set = order(pa1, pa2);
3007 return isl_set_unwrap(set);
3010 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3011 * where the function values are equal.
3012 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3014 static __isl_give isl_map *isl_pw_aff_eq_map_aligned(__isl_take isl_pw_aff *pa1,
3015 __isl_take isl_pw_aff *pa2)
3017 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_eq_set);
3020 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3021 * where the function values are equal.
3023 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3024 __isl_take isl_pw_aff *pa2)
3026 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_eq_map_aligned);
3029 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3030 * where the function value of "pa1" is less than the function value of "pa2".
3031 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3033 static __isl_give isl_map *isl_pw_aff_lt_map_aligned(__isl_take isl_pw_aff *pa1,
3034 __isl_take isl_pw_aff *pa2)
3036 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_lt_set);
3039 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3040 * where the function value of "pa1" is less than the function value of "pa2".
3042 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3043 __isl_take isl_pw_aff *pa2)
3045 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_lt_map_aligned);
3048 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3049 * where the function value of "pa1" is greater than the function value
3050 * of "pa2".
3051 * The parameters of "pa1" and "pa2" are assumed to have been aligned.
3053 static __isl_give isl_map *isl_pw_aff_gt_map_aligned(__isl_take isl_pw_aff *pa1,
3054 __isl_take isl_pw_aff *pa2)
3056 return isl_pw_aff_order_map_aligned(pa1, pa2, &isl_pw_aff_gt_set);
3059 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3060 * where the function value of "pa1" is greater than the function value
3061 * of "pa2".
3063 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3064 __isl_take isl_pw_aff *pa2)
3066 return align_params_pw_pw_map_and(pa1, pa2, &isl_pw_aff_gt_map_aligned);
3069 /* Return a set containing those elements in the shared domain
3070 * of the elements of list1 and list2 where each element in list1
3071 * has the relation specified by "fn" with each element in list2.
3073 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3074 __isl_take isl_pw_aff_list *list2,
3075 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3076 __isl_take isl_pw_aff *pwaff2))
3078 int i, j;
3079 isl_ctx *ctx;
3080 isl_set *set;
3082 if (!list1 || !list2)
3083 goto error;
3085 ctx = isl_pw_aff_list_get_ctx(list1);
3086 if (list1->n < 1 || list2->n < 1)
3087 isl_die(ctx, isl_error_invalid,
3088 "list should contain at least one element", goto error);
3090 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3091 for (i = 0; i < list1->n; ++i)
3092 for (j = 0; j < list2->n; ++j) {
3093 isl_set *set_ij;
3095 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3096 isl_pw_aff_copy(list2->p[j]));
3097 set = isl_set_intersect(set, set_ij);
3100 isl_pw_aff_list_free(list1);
3101 isl_pw_aff_list_free(list2);
3102 return set;
3103 error:
3104 isl_pw_aff_list_free(list1);
3105 isl_pw_aff_list_free(list2);
3106 return NULL;
3109 /* Return a set containing those elements in the shared domain
3110 * of the elements of list1 and list2 where each element in list1
3111 * is equal to each element in list2.
3113 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3114 __isl_take isl_pw_aff_list *list2)
3116 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3119 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3120 __isl_take isl_pw_aff_list *list2)
3122 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3125 /* Return a set containing those elements in the shared domain
3126 * of the elements of list1 and list2 where each element in list1
3127 * is less than or equal to each element in list2.
3129 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3130 __isl_take isl_pw_aff_list *list2)
3132 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3135 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3136 __isl_take isl_pw_aff_list *list2)
3138 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3141 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3142 __isl_take isl_pw_aff_list *list2)
3144 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3147 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3148 __isl_take isl_pw_aff_list *list2)
3150 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3154 /* Return a set containing those elements in the shared domain
3155 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3157 static __isl_give isl_set *pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3158 __isl_take isl_pw_aff *pwaff2)
3160 isl_set *set_lt, *set_gt;
3162 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3163 isl_pw_aff_copy(pwaff2));
3164 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3165 return isl_set_union_disjoint(set_lt, set_gt);
3168 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3169 __isl_take isl_pw_aff *pwaff2)
3171 return align_params_pw_pw_set_and(pwaff1, pwaff2, &pw_aff_ne_set);
3174 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3175 isl_int v)
3177 int i;
3179 if (isl_int_is_one(v))
3180 return pwaff;
3181 if (!isl_int_is_pos(v))
3182 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3183 "factor needs to be positive",
3184 return isl_pw_aff_free(pwaff));
3185 pwaff = isl_pw_aff_cow(pwaff);
3186 if (!pwaff)
3187 return NULL;
3188 if (pwaff->n == 0)
3189 return pwaff;
3191 for (i = 0; i < pwaff->n; ++i) {
3192 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3193 if (!pwaff->p[i].aff)
3194 return isl_pw_aff_free(pwaff);
3197 return pwaff;
3200 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3202 int i;
3204 pwaff = isl_pw_aff_cow(pwaff);
3205 if (!pwaff)
3206 return NULL;
3207 if (pwaff->n == 0)
3208 return pwaff;
3210 for (i = 0; i < pwaff->n; ++i) {
3211 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3212 if (!pwaff->p[i].aff)
3213 return isl_pw_aff_free(pwaff);
3216 return pwaff;
3219 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3221 int i;
3223 pwaff = isl_pw_aff_cow(pwaff);
3224 if (!pwaff)
3225 return NULL;
3226 if (pwaff->n == 0)
3227 return pwaff;
3229 for (i = 0; i < pwaff->n; ++i) {
3230 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3231 if (!pwaff->p[i].aff)
3232 return isl_pw_aff_free(pwaff);
3235 return pwaff;
3238 /* Assuming that "cond1" and "cond2" are disjoint,
3239 * return an affine expression that is equal to pwaff1 on cond1
3240 * and to pwaff2 on cond2.
3242 static __isl_give isl_pw_aff *isl_pw_aff_select(
3243 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3244 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3246 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3247 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3249 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3252 /* Return an affine expression that is equal to pwaff_true for elements
3253 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3254 * is zero.
3255 * That is, return cond ? pwaff_true : pwaff_false;
3257 * If "cond" involves and NaN, then we conservatively return a NaN
3258 * on its entire domain. In principle, we could consider the pieces
3259 * where it is NaN separately from those where it is not.
3261 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3262 * then only use the domain of "cond" to restrict the domain.
3264 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3265 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3267 isl_set *cond_true, *cond_false;
3268 isl_bool equal;
3270 if (!cond)
3271 goto error;
3272 if (isl_pw_aff_involves_nan(cond)) {
3273 isl_space *space = isl_pw_aff_get_domain_space(cond);
3274 isl_local_space *ls = isl_local_space_from_space(space);
3275 isl_pw_aff_free(cond);
3276 isl_pw_aff_free(pwaff_true);
3277 isl_pw_aff_free(pwaff_false);
3278 return isl_pw_aff_nan_on_domain(ls);
3281 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3282 isl_pw_aff_get_space(pwaff_false));
3283 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3284 isl_pw_aff_get_space(pwaff_true));
3285 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3286 if (equal < 0)
3287 goto error;
3288 if (equal) {
3289 isl_set *dom;
3291 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3292 isl_pw_aff_free(pwaff_false);
3293 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3296 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3297 cond_false = isl_pw_aff_zero_set(cond);
3298 return isl_pw_aff_select(cond_true, pwaff_true,
3299 cond_false, pwaff_false);
3300 error:
3301 isl_pw_aff_free(cond);
3302 isl_pw_aff_free(pwaff_true);
3303 isl_pw_aff_free(pwaff_false);
3304 return NULL;
3307 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3309 int pos;
3311 if (!aff)
3312 return isl_bool_error;
3314 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3315 return isl_bool_ok(pos == -1);
3318 /* Check whether pwaff is a piecewise constant.
3320 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3322 int i;
3324 if (!pwaff)
3325 return isl_bool_error;
3327 for (i = 0; i < pwaff->n; ++i) {
3328 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3329 if (is_cst < 0 || !is_cst)
3330 return is_cst;
3333 return isl_bool_true;
3336 /* Are all elements of "mpa" piecewise constants?
3338 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
3340 int i;
3342 if (!mpa)
3343 return isl_bool_error;
3345 for (i = 0; i < mpa->n; ++i) {
3346 isl_bool is_cst = isl_pw_aff_is_cst(mpa->u.p[i]);
3347 if (is_cst < 0 || !is_cst)
3348 return is_cst;
3351 return isl_bool_true;
3354 /* Return the product of "aff1" and "aff2".
3356 * If either of the two is NaN, then the result is NaN.
3358 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3360 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3361 __isl_take isl_aff *aff2)
3363 if (!aff1 || !aff2)
3364 goto error;
3366 if (isl_aff_is_nan(aff1)) {
3367 isl_aff_free(aff2);
3368 return aff1;
3370 if (isl_aff_is_nan(aff2)) {
3371 isl_aff_free(aff1);
3372 return aff2;
3375 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3376 return isl_aff_mul(aff2, aff1);
3378 if (!isl_aff_is_cst(aff2))
3379 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3380 "at least one affine expression should be constant",
3381 goto error);
3383 aff1 = isl_aff_cow(aff1);
3384 if (!aff1 || !aff2)
3385 goto error;
3387 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3388 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3390 isl_aff_free(aff2);
3391 return aff1;
3392 error:
3393 isl_aff_free(aff1);
3394 isl_aff_free(aff2);
3395 return NULL;
3398 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3400 * If either of the two is NaN, then the result is NaN.
3402 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3403 __isl_take isl_aff *aff2)
3405 int is_cst;
3406 int neg;
3408 if (!aff1 || !aff2)
3409 goto error;
3411 if (isl_aff_is_nan(aff1)) {
3412 isl_aff_free(aff2);
3413 return aff1;
3415 if (isl_aff_is_nan(aff2)) {
3416 isl_aff_free(aff1);
3417 return aff2;
3420 is_cst = isl_aff_is_cst(aff2);
3421 if (is_cst < 0)
3422 goto error;
3423 if (!is_cst)
3424 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3425 "second argument should be a constant", goto error);
3427 if (!aff2)
3428 goto error;
3430 neg = isl_int_is_neg(aff2->v->el[1]);
3431 if (neg) {
3432 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3433 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3436 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3437 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3439 if (neg) {
3440 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3441 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3444 isl_aff_free(aff2);
3445 return aff1;
3446 error:
3447 isl_aff_free(aff1);
3448 isl_aff_free(aff2);
3449 return NULL;
3452 static __isl_give isl_pw_aff *pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3453 __isl_take isl_pw_aff *pwaff2)
3455 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3458 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3459 __isl_take isl_pw_aff *pwaff2)
3461 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_add);
3464 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3465 __isl_take isl_pw_aff *pwaff2)
3467 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3470 static __isl_give isl_pw_aff *pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3471 __isl_take isl_pw_aff *pwaff2)
3473 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3476 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3477 __isl_take isl_pw_aff *pwaff2)
3479 return isl_pw_aff_align_params_pw_pw_and(pwaff1, pwaff2, &pw_aff_mul);
3482 static __isl_give isl_pw_aff *pw_aff_div(__isl_take isl_pw_aff *pa1,
3483 __isl_take isl_pw_aff *pa2)
3485 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3488 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3490 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3491 __isl_take isl_pw_aff *pa2)
3493 int is_cst;
3495 is_cst = isl_pw_aff_is_cst(pa2);
3496 if (is_cst < 0)
3497 goto error;
3498 if (!is_cst)
3499 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3500 "second argument should be a piecewise constant",
3501 goto error);
3502 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_div);
3503 error:
3504 isl_pw_aff_free(pa1);
3505 isl_pw_aff_free(pa2);
3506 return NULL;
3509 /* Compute the quotient of the integer division of "pa1" by "pa2"
3510 * with rounding towards zero.
3511 * "pa2" is assumed to be a piecewise constant.
3513 * In particular, return
3515 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3518 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3519 __isl_take isl_pw_aff *pa2)
3521 int is_cst;
3522 isl_set *cond;
3523 isl_pw_aff *f, *c;
3525 is_cst = isl_pw_aff_is_cst(pa2);
3526 if (is_cst < 0)
3527 goto error;
3528 if (!is_cst)
3529 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3530 "second argument should be a piecewise constant",
3531 goto error);
3533 pa1 = isl_pw_aff_div(pa1, pa2);
3535 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3536 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3537 c = isl_pw_aff_ceil(pa1);
3538 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3539 error:
3540 isl_pw_aff_free(pa1);
3541 isl_pw_aff_free(pa2);
3542 return NULL;
3545 /* Compute the remainder of the integer division of "pa1" by "pa2"
3546 * with rounding towards zero.
3547 * "pa2" is assumed to be a piecewise constant.
3549 * In particular, return
3551 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3554 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3555 __isl_take isl_pw_aff *pa2)
3557 int is_cst;
3558 isl_pw_aff *res;
3560 is_cst = isl_pw_aff_is_cst(pa2);
3561 if (is_cst < 0)
3562 goto error;
3563 if (!is_cst)
3564 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3565 "second argument should be a piecewise constant",
3566 goto error);
3567 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3568 res = isl_pw_aff_mul(pa2, res);
3569 res = isl_pw_aff_sub(pa1, res);
3570 return res;
3571 error:
3572 isl_pw_aff_free(pa1);
3573 isl_pw_aff_free(pa2);
3574 return NULL;
3577 /* Does either of "pa1" or "pa2" involve any NaN2?
3579 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3580 __isl_keep isl_pw_aff *pa2)
3582 isl_bool has_nan;
3584 has_nan = isl_pw_aff_involves_nan(pa1);
3585 if (has_nan < 0 || has_nan)
3586 return has_nan;
3587 return isl_pw_aff_involves_nan(pa2);
3590 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3591 * by a NaN on their shared domain.
3593 * In principle, the result could be refined to only being NaN
3594 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3596 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3597 __isl_take isl_pw_aff *pa2)
3599 isl_local_space *ls;
3600 isl_set *dom;
3601 isl_pw_aff *pa;
3603 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3604 ls = isl_local_space_from_space(isl_set_get_space(dom));
3605 pa = isl_pw_aff_nan_on_domain(ls);
3606 pa = isl_pw_aff_intersect_domain(pa, dom);
3608 return pa;
3611 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3612 __isl_take isl_pw_aff *pwaff2)
3614 isl_set *le;
3615 isl_set *dom;
3617 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3618 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3619 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3620 isl_pw_aff_copy(pwaff2));
3621 dom = isl_set_subtract(dom, isl_set_copy(le));
3622 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3625 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3626 __isl_take isl_pw_aff *pwaff2)
3628 isl_set *ge;
3629 isl_set *dom;
3631 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3632 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3633 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3634 isl_pw_aff_copy(pwaff2));
3635 dom = isl_set_subtract(dom, isl_set_copy(ge));
3636 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3639 /* Return an expression for the minimum (if "max" is not set) or
3640 * the maximum (if "max" is set) of "pa1" and "pa2".
3641 * If either expression involves any NaN, then return a NaN
3642 * on the shared domain as result.
3644 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3645 __isl_take isl_pw_aff *pa2, int max)
3647 isl_bool has_nan;
3649 has_nan = either_involves_nan(pa1, pa2);
3650 if (has_nan < 0)
3651 pa1 = isl_pw_aff_free(pa1);
3652 else if (has_nan)
3653 return replace_by_nan(pa1, pa2);
3655 if (max)
3656 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_max);
3657 else
3658 return isl_pw_aff_align_params_pw_pw_and(pa1, pa2, &pw_aff_min);
3661 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3663 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3664 __isl_take isl_pw_aff *pwaff2)
3666 return pw_aff_min_max(pwaff1, pwaff2, 0);
3669 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3671 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3672 __isl_take isl_pw_aff *pwaff2)
3674 return pw_aff_min_max(pwaff1, pwaff2, 1);
3677 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3678 __isl_take isl_pw_aff_list *list,
3679 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3680 __isl_take isl_pw_aff *pwaff2))
3682 int i;
3683 isl_ctx *ctx;
3684 isl_pw_aff *res;
3686 if (!list)
3687 return NULL;
3689 ctx = isl_pw_aff_list_get_ctx(list);
3690 if (list->n < 1)
3691 isl_die(ctx, isl_error_invalid,
3692 "list should contain at least one element", goto error);
3694 res = isl_pw_aff_copy(list->p[0]);
3695 for (i = 1; i < list->n; ++i)
3696 res = fn(res, isl_pw_aff_copy(list->p[i]));
3698 isl_pw_aff_list_free(list);
3699 return res;
3700 error:
3701 isl_pw_aff_list_free(list);
3702 return NULL;
3705 /* Return an isl_pw_aff that maps each element in the intersection of the
3706 * domains of the elements of list to the minimal corresponding affine
3707 * expression.
3709 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3711 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3714 /* Return an isl_pw_aff that maps each element in the intersection of the
3715 * domains of the elements of list to the maximal corresponding affine
3716 * expression.
3718 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3720 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3723 /* Mark the domains of "pwaff" as rational.
3725 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3727 int i;
3729 pwaff = isl_pw_aff_cow(pwaff);
3730 if (!pwaff)
3731 return NULL;
3732 if (pwaff->n == 0)
3733 return pwaff;
3735 for (i = 0; i < pwaff->n; ++i) {
3736 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3737 if (!pwaff->p[i].set)
3738 return isl_pw_aff_free(pwaff);
3741 return pwaff;
3744 /* Mark the domains of the elements of "list" as rational.
3746 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3747 __isl_take isl_pw_aff_list *list)
3749 int i, n;
3751 if (!list)
3752 return NULL;
3753 if (list->n == 0)
3754 return list;
3756 n = list->n;
3757 for (i = 0; i < n; ++i) {
3758 isl_pw_aff *pa;
3760 pa = isl_pw_aff_list_get_pw_aff(list, i);
3761 pa = isl_pw_aff_set_rational(pa);
3762 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3765 return list;
3768 /* Do the parameters of "aff" match those of "space"?
3770 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3771 __isl_keep isl_space *space)
3773 isl_space *aff_space;
3774 isl_bool match;
3776 if (!aff || !space)
3777 return isl_bool_error;
3779 aff_space = isl_aff_get_domain_space(aff);
3781 match = isl_space_has_equal_params(space, aff_space);
3783 isl_space_free(aff_space);
3784 return match;
3787 /* Check that the domain space of "aff" matches "space".
3789 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3790 __isl_keep isl_space *space)
3792 isl_space *aff_space;
3793 isl_bool match;
3795 if (!aff || !space)
3796 return isl_stat_error;
3798 aff_space = isl_aff_get_domain_space(aff);
3800 match = isl_space_has_equal_params(space, aff_space);
3801 if (match < 0)
3802 goto error;
3803 if (!match)
3804 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3805 "parameters don't match", goto error);
3806 match = isl_space_tuple_is_equal(space, isl_dim_in,
3807 aff_space, isl_dim_set);
3808 if (match < 0)
3809 goto error;
3810 if (!match)
3811 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3812 "domains don't match", goto error);
3813 isl_space_free(aff_space);
3814 return isl_stat_ok;
3815 error:
3816 isl_space_free(aff_space);
3817 return isl_stat_error;
3820 #undef BASE
3821 #define BASE aff
3822 #undef DOMBASE
3823 #define DOMBASE set
3825 #include <isl_multi_no_explicit_domain.c>
3826 #include <isl_multi_templ.c>
3827 #include <isl_multi_apply_set.c>
3828 #include <isl_multi_cmp.c>
3829 #include <isl_multi_dims.c>
3830 #include <isl_multi_floor.c>
3831 #include <isl_multi_from_base_templ.c>
3832 #include <isl_multi_gist.c>
3833 #include <isl_multi_identity_templ.c>
3834 #include <isl_multi_move_dims_templ.c>
3835 #include <isl_multi_product_templ.c>
3836 #include <isl_multi_splice_templ.c>
3837 #include <isl_multi_zero_templ.c>
3839 /* Construct an isl_multi_aff living in "space" that corresponds
3840 * to the affine transformation matrix "mat".
3842 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3843 __isl_take isl_space *space, __isl_take isl_mat *mat)
3845 isl_ctx *ctx;
3846 isl_local_space *ls = NULL;
3847 isl_multi_aff *ma = NULL;
3848 isl_size n_row, n_col, n_out, total;
3849 int i;
3851 if (!space || !mat)
3852 goto error;
3854 ctx = isl_mat_get_ctx(mat);
3856 n_row = isl_mat_rows(mat);
3857 n_col = isl_mat_cols(mat);
3858 n_out = isl_space_dim(space, isl_dim_out);
3859 total = isl_space_dim(space, isl_dim_all);
3860 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3861 goto error;
3862 if (n_row < 1)
3863 isl_die(ctx, isl_error_invalid,
3864 "insufficient number of rows", goto error);
3865 if (n_col < 1)
3866 isl_die(ctx, isl_error_invalid,
3867 "insufficient number of columns", goto error);
3868 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3869 isl_die(ctx, isl_error_invalid,
3870 "dimension mismatch", goto error);
3872 ma = isl_multi_aff_zero(isl_space_copy(space));
3873 ls = isl_local_space_from_space(isl_space_domain(space));
3875 for (i = 0; i < n_row - 1; ++i) {
3876 isl_vec *v;
3877 isl_aff *aff;
3879 v = isl_vec_alloc(ctx, 1 + n_col);
3880 if (!v)
3881 goto error;
3882 isl_int_set(v->el[0], mat->row[0][0]);
3883 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3884 v = isl_vec_normalize(v);
3885 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3886 ma = isl_multi_aff_set_aff(ma, i, aff);
3889 isl_local_space_free(ls);
3890 isl_mat_free(mat);
3891 return ma;
3892 error:
3893 isl_local_space_free(ls);
3894 isl_mat_free(mat);
3895 isl_multi_aff_free(ma);
3896 return NULL;
3899 /* Remove any internal structure of the domain of "ma".
3900 * If there is any such internal structure in the input,
3901 * then the name of the corresponding space is also removed.
3903 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3904 __isl_take isl_multi_aff *ma)
3906 isl_space *space;
3908 if (!ma)
3909 return NULL;
3911 if (!ma->space->nested[0])
3912 return ma;
3914 space = isl_multi_aff_get_space(ma);
3915 space = isl_space_flatten_domain(space);
3916 ma = isl_multi_aff_reset_space(ma, space);
3918 return ma;
3921 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3922 * of the space to its domain.
3924 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3926 int i;
3927 isl_size n_in;
3928 isl_local_space *ls;
3929 isl_multi_aff *ma;
3931 if (!space)
3932 return NULL;
3933 if (!isl_space_is_map(space))
3934 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3935 "not a map space", goto error);
3937 n_in = isl_space_dim(space, isl_dim_in);
3938 if (n_in < 0)
3939 goto error;
3940 space = isl_space_domain_map(space);
3942 ma = isl_multi_aff_alloc(isl_space_copy(space));
3943 if (n_in == 0) {
3944 isl_space_free(space);
3945 return ma;
3948 space = isl_space_domain(space);
3949 ls = isl_local_space_from_space(space);
3950 for (i = 0; i < n_in; ++i) {
3951 isl_aff *aff;
3953 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3954 isl_dim_set, i);
3955 ma = isl_multi_aff_set_aff(ma, i, aff);
3957 isl_local_space_free(ls);
3958 return ma;
3959 error:
3960 isl_space_free(space);
3961 return NULL;
3964 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3965 * of the space to its range.
3967 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
3969 int i;
3970 isl_size n_in, n_out;
3971 isl_local_space *ls;
3972 isl_multi_aff *ma;
3974 if (!space)
3975 return NULL;
3976 if (!isl_space_is_map(space))
3977 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3978 "not a map space", goto error);
3980 n_in = isl_space_dim(space, isl_dim_in);
3981 n_out = isl_space_dim(space, isl_dim_out);
3982 if (n_in < 0 || n_out < 0)
3983 goto error;
3984 space = isl_space_range_map(space);
3986 ma = isl_multi_aff_alloc(isl_space_copy(space));
3987 if (n_out == 0) {
3988 isl_space_free(space);
3989 return ma;
3992 space = isl_space_domain(space);
3993 ls = isl_local_space_from_space(space);
3994 for (i = 0; i < n_out; ++i) {
3995 isl_aff *aff;
3997 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
3998 isl_dim_set, n_in + i);
3999 ma = isl_multi_aff_set_aff(ma, i, aff);
4001 isl_local_space_free(ls);
4002 return ma;
4003 error:
4004 isl_space_free(space);
4005 return NULL;
4008 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4009 * of the space to its range.
4011 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4012 __isl_take isl_space *space)
4014 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4017 /* Given the space of a set and a range of set dimensions,
4018 * construct an isl_multi_aff that projects out those dimensions.
4020 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4021 __isl_take isl_space *space, enum isl_dim_type type,
4022 unsigned first, unsigned n)
4024 int i;
4025 isl_size dim;
4026 isl_local_space *ls;
4027 isl_multi_aff *ma;
4029 if (!space)
4030 return NULL;
4031 if (!isl_space_is_set(space))
4032 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4033 "expecting set space", goto error);
4034 if (type != isl_dim_set)
4035 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4036 "only set dimensions can be projected out", goto error);
4037 if (isl_space_check_range(space, type, first, n) < 0)
4038 goto error;
4040 dim = isl_space_dim(space, isl_dim_set);
4041 if (dim < 0)
4042 goto error;
4044 space = isl_space_from_domain(space);
4045 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4047 if (dim == n)
4048 return isl_multi_aff_alloc(space);
4050 ma = isl_multi_aff_alloc(isl_space_copy(space));
4051 space = isl_space_domain(space);
4052 ls = isl_local_space_from_space(space);
4054 for (i = 0; i < first; ++i) {
4055 isl_aff *aff;
4057 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4058 isl_dim_set, i);
4059 ma = isl_multi_aff_set_aff(ma, i, aff);
4062 for (i = 0; i < dim - (first + n); ++i) {
4063 isl_aff *aff;
4065 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4066 isl_dim_set, first + n + i);
4067 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4070 isl_local_space_free(ls);
4071 return ma;
4072 error:
4073 isl_space_free(space);
4074 return NULL;
4077 /* Given the space of a set and a range of set dimensions,
4078 * construct an isl_pw_multi_aff that projects out those dimensions.
4080 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4081 __isl_take isl_space *space, enum isl_dim_type type,
4082 unsigned first, unsigned n)
4084 isl_multi_aff *ma;
4086 ma = isl_multi_aff_project_out_map(space, type, first, n);
4087 return isl_pw_multi_aff_from_multi_aff(ma);
4090 /* Create an isl_pw_multi_aff with the given isl_multi_aff on a universe
4091 * domain.
4093 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_aff(
4094 __isl_take isl_multi_aff *ma)
4096 isl_set *dom = isl_set_universe(isl_multi_aff_get_domain_space(ma));
4097 return isl_pw_multi_aff_alloc(dom, ma);
4100 /* Create a piecewise multi-affine expression in the given space that maps each
4101 * input dimension to the corresponding output dimension.
4103 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4104 __isl_take isl_space *space)
4106 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4109 /* Exploit the equalities in "eq" to simplify the affine expressions.
4111 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4112 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4114 int i;
4116 maff = isl_multi_aff_cow(maff);
4117 if (!maff || !eq)
4118 goto error;
4120 for (i = 0; i < maff->n; ++i) {
4121 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4122 isl_basic_set_copy(eq));
4123 if (!maff->u.p[i])
4124 goto error;
4127 isl_basic_set_free(eq);
4128 return maff;
4129 error:
4130 isl_basic_set_free(eq);
4131 isl_multi_aff_free(maff);
4132 return NULL;
4135 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4136 isl_int f)
4138 int i;
4140 maff = isl_multi_aff_cow(maff);
4141 if (!maff)
4142 return NULL;
4144 for (i = 0; i < maff->n; ++i) {
4145 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4146 if (!maff->u.p[i])
4147 return isl_multi_aff_free(maff);
4150 return maff;
4153 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4154 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4156 maff1 = isl_multi_aff_add(maff1, maff2);
4157 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4158 return maff1;
4161 int isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4163 if (!maff)
4164 return -1;
4166 return 0;
4169 /* Return the set of domain elements where "ma1" is lexicographically
4170 * smaller than or equal to "ma2".
4172 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4173 __isl_take isl_multi_aff *ma2)
4175 return isl_multi_aff_lex_ge_set(ma2, ma1);
4178 /* Return the set of domain elements where "ma1" is lexicographically
4179 * smaller than "ma2".
4181 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4182 __isl_take isl_multi_aff *ma2)
4184 return isl_multi_aff_lex_gt_set(ma2, ma1);
4187 /* Return the set of domain elements where "ma1" and "ma2"
4188 * satisfy "order".
4190 static __isl_give isl_set *isl_multi_aff_order_set(
4191 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4192 __isl_give isl_map *order(__isl_take isl_space *set_space))
4194 isl_space *space;
4195 isl_map *map1, *map2;
4196 isl_map *map, *ge;
4198 map1 = isl_map_from_multi_aff_internal(ma1);
4199 map2 = isl_map_from_multi_aff_internal(ma2);
4200 map = isl_map_range_product(map1, map2);
4201 space = isl_space_range(isl_map_get_space(map));
4202 space = isl_space_domain(isl_space_unwrap(space));
4203 ge = order(space);
4204 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4206 return isl_map_domain(map);
4209 /* Return the set of domain elements where "ma1" is lexicographically
4210 * greater than or equal to "ma2".
4212 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4213 __isl_take isl_multi_aff *ma2)
4215 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4218 /* Return the set of domain elements where "ma1" is lexicographically
4219 * greater than "ma2".
4221 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4222 __isl_take isl_multi_aff *ma2)
4224 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4227 #undef PW
4228 #define PW isl_pw_multi_aff
4229 #undef EL
4230 #define EL isl_multi_aff
4231 #undef EL_IS_ZERO
4232 #define EL_IS_ZERO is_empty
4233 #undef ZERO
4234 #define ZERO empty
4235 #undef IS_ZERO
4236 #define IS_ZERO is_empty
4237 #undef FIELD
4238 #define FIELD maff
4239 #undef DEFAULT_IS_ZERO
4240 #define DEFAULT_IS_ZERO 0
4242 #define NO_SUB
4243 #define NO_OPT
4244 #define NO_INSERT_DIMS
4245 #define NO_LIFT
4246 #define NO_MORPH
4248 #include <isl_pw_templ.c>
4249 #include <isl_pw_union_opt.c>
4251 #undef NO_SUB
4253 #undef BASE
4254 #define BASE pw_multi_aff
4256 #include <isl_union_multi.c>
4257 #include <isl_union_neg.c>
4259 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmax(
4260 __isl_take isl_pw_multi_aff *pma1,
4261 __isl_take isl_pw_multi_aff *pma2)
4263 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4264 &isl_multi_aff_lex_ge_set);
4267 /* Given two piecewise multi affine expressions, return a piecewise
4268 * multi-affine expression defined on the union of the definition domains
4269 * of the inputs that is equal to the lexicographic maximum of the two
4270 * inputs on each cell. If only one of the two inputs is defined on
4271 * a given cell, then it is considered to be the maximum.
4273 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4274 __isl_take isl_pw_multi_aff *pma1,
4275 __isl_take isl_pw_multi_aff *pma2)
4277 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4278 &pw_multi_aff_union_lexmax);
4281 static __isl_give isl_pw_multi_aff *pw_multi_aff_union_lexmin(
4282 __isl_take isl_pw_multi_aff *pma1,
4283 __isl_take isl_pw_multi_aff *pma2)
4285 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4286 &isl_multi_aff_lex_le_set);
4289 /* Given two piecewise multi affine expressions, return a piecewise
4290 * multi-affine expression defined on the union of the definition domains
4291 * of the inputs that is equal to the lexicographic minimum of the two
4292 * inputs on each cell. If only one of the two inputs is defined on
4293 * a given cell, then it is considered to be the minimum.
4295 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4296 __isl_take isl_pw_multi_aff *pma1,
4297 __isl_take isl_pw_multi_aff *pma2)
4299 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4300 &pw_multi_aff_union_lexmin);
4303 static __isl_give isl_pw_multi_aff *pw_multi_aff_add(
4304 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4306 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4307 &isl_multi_aff_add);
4310 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4311 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4313 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4314 &pw_multi_aff_add);
4317 static __isl_give isl_pw_multi_aff *pw_multi_aff_sub(
4318 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4320 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4321 &isl_multi_aff_sub);
4324 /* Subtract "pma2" from "pma1" and return the result.
4326 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4327 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4329 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4330 &pw_multi_aff_sub);
4333 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4334 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4336 return isl_pw_multi_aff_union_add_(pma1, pma2);
4339 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4340 * with the actual sum on the shared domain and
4341 * the defined expression on the symmetric difference of the domains.
4343 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4344 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4346 return isl_union_pw_aff_union_add_(upa1, upa2);
4349 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4350 * with the actual sum on the shared domain and
4351 * the defined expression on the symmetric difference of the domains.
4353 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4354 __isl_take isl_union_pw_multi_aff *upma1,
4355 __isl_take isl_union_pw_multi_aff *upma2)
4357 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4360 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4361 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4363 static __isl_give isl_pw_multi_aff *pw_multi_aff_product(
4364 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4366 int i, j, n;
4367 isl_space *space;
4368 isl_pw_multi_aff *res;
4370 if (!pma1 || !pma2)
4371 goto error;
4373 n = pma1->n * pma2->n;
4374 space = isl_space_product(isl_space_copy(pma1->dim),
4375 isl_space_copy(pma2->dim));
4376 res = isl_pw_multi_aff_alloc_size(space, n);
4378 for (i = 0; i < pma1->n; ++i) {
4379 for (j = 0; j < pma2->n; ++j) {
4380 isl_set *domain;
4381 isl_multi_aff *ma;
4383 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4384 isl_set_copy(pma2->p[j].set));
4385 ma = isl_multi_aff_product(
4386 isl_multi_aff_copy(pma1->p[i].maff),
4387 isl_multi_aff_copy(pma2->p[j].maff));
4388 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4392 isl_pw_multi_aff_free(pma1);
4393 isl_pw_multi_aff_free(pma2);
4394 return res;
4395 error:
4396 isl_pw_multi_aff_free(pma1);
4397 isl_pw_multi_aff_free(pma2);
4398 return NULL;
4401 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4402 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4404 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
4405 &pw_multi_aff_product);
4408 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4409 * denominator "denom".
4410 * "denom" is allowed to be negative, in which case the actual denominator
4411 * is -denom and the expressions are added instead.
4413 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4414 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4416 int i, first;
4417 int sign;
4418 isl_int d;
4420 first = isl_seq_first_non_zero(c, n);
4421 if (first == -1)
4422 return aff;
4424 sign = isl_int_sgn(denom);
4425 isl_int_init(d);
4426 isl_int_abs(d, denom);
4427 for (i = first; i < n; ++i) {
4428 isl_aff *aff_i;
4430 if (isl_int_is_zero(c[i]))
4431 continue;
4432 aff_i = isl_multi_aff_get_aff(ma, i);
4433 aff_i = isl_aff_scale(aff_i, c[i]);
4434 aff_i = isl_aff_scale_down(aff_i, d);
4435 if (sign >= 0)
4436 aff = isl_aff_sub(aff, aff_i);
4437 else
4438 aff = isl_aff_add(aff, aff_i);
4440 isl_int_clear(d);
4442 return aff;
4445 /* Extract an affine expression that expresses the output dimension "pos"
4446 * of "bmap" in terms of the parameters and input dimensions from
4447 * equality "eq".
4448 * Note that this expression may involve integer divisions defined
4449 * in terms of parameters and input dimensions.
4450 * The equality may also involve references to earlier (but not later)
4451 * output dimensions. These are replaced by the corresponding elements
4452 * in "ma".
4454 * If the equality is of the form
4456 * f(i) + h(j) + a x + g(i) = 0,
4458 * with f(i) a linear combinations of the parameters and input dimensions,
4459 * g(i) a linear combination of integer divisions defined in terms of the same
4460 * and h(j) a linear combinations of earlier output dimensions,
4461 * then the affine expression is
4463 * (-f(i) - g(i))/a - h(j)/a
4465 * If the equality is of the form
4467 * f(i) + h(j) - a x + g(i) = 0,
4469 * then the affine expression is
4471 * (f(i) + g(i))/a - h(j)/(-a)
4474 * If "div" refers to an integer division (i.e., it is smaller than
4475 * the number of integer divisions), then the equality constraint
4476 * does involve an integer division (the one at position "div") that
4477 * is defined in terms of output dimensions. However, this integer
4478 * division can be eliminated by exploiting a pair of constraints
4479 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4480 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4481 * -l + x >= 0.
4482 * In particular, let
4484 * x = e(i) + m floor(...)
4486 * with e(i) the expression derived above and floor(...) the integer
4487 * division involving output dimensions.
4488 * From
4490 * l <= x <= l + n,
4492 * we have
4494 * 0 <= x - l <= n
4496 * This means
4498 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4499 * = (e(i) - l) mod m
4501 * Therefore,
4503 * x - l = (e(i) - l) mod m
4505 * or
4507 * x = ((e(i) - l) mod m) + l
4509 * The variable "shift" below contains the expression -l, which may
4510 * also involve a linear combination of earlier output dimensions.
4512 static __isl_give isl_aff *extract_aff_from_equality(
4513 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4514 __isl_keep isl_multi_aff *ma)
4516 unsigned o_out;
4517 isl_size n_div, n_out;
4518 isl_ctx *ctx;
4519 isl_local_space *ls;
4520 isl_aff *aff, *shift;
4521 isl_val *mod;
4523 ctx = isl_basic_map_get_ctx(bmap);
4524 ls = isl_basic_map_get_local_space(bmap);
4525 ls = isl_local_space_domain(ls);
4526 aff = isl_aff_alloc(isl_local_space_copy(ls));
4527 if (!aff)
4528 goto error;
4529 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4530 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4531 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4532 if (n_out < 0 || n_div < 0)
4533 goto error;
4534 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4535 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4536 isl_seq_cpy(aff->v->el + 1 + o_out,
4537 bmap->eq[eq] + o_out + n_out, n_div);
4538 } else {
4539 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4540 isl_seq_neg(aff->v->el + 1 + o_out,
4541 bmap->eq[eq] + o_out + n_out, n_div);
4543 if (div < n_div)
4544 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4545 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4546 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4547 bmap->eq[eq][o_out + pos]);
4548 if (div < n_div) {
4549 shift = isl_aff_alloc(isl_local_space_copy(ls));
4550 if (!shift)
4551 goto error;
4552 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4553 isl_seq_cpy(shift->v->el + 1 + o_out,
4554 bmap->ineq[ineq] + o_out + n_out, n_div);
4555 isl_int_set_si(shift->v->el[0], 1);
4556 shift = subtract_initial(shift, ma, pos,
4557 bmap->ineq[ineq] + o_out, ctx->negone);
4558 aff = isl_aff_add(aff, isl_aff_copy(shift));
4559 mod = isl_val_int_from_isl_int(ctx,
4560 bmap->eq[eq][o_out + n_out + div]);
4561 mod = isl_val_abs(mod);
4562 aff = isl_aff_mod_val(aff, mod);
4563 aff = isl_aff_sub(aff, shift);
4566 isl_local_space_free(ls);
4567 return aff;
4568 error:
4569 isl_local_space_free(ls);
4570 isl_aff_free(aff);
4571 return NULL;
4574 /* Given a basic map with output dimensions defined
4575 * in terms of the parameters input dimensions and earlier
4576 * output dimensions using an equality (and possibly a pair on inequalities),
4577 * extract an isl_aff that expresses output dimension "pos" in terms
4578 * of the parameters and input dimensions.
4579 * Note that this expression may involve integer divisions defined
4580 * in terms of parameters and input dimensions.
4581 * "ma" contains the expressions corresponding to earlier output dimensions.
4583 * This function shares some similarities with
4584 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4586 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4587 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4589 int eq, div, ineq;
4590 isl_aff *aff;
4592 if (!bmap)
4593 return NULL;
4594 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4595 if (eq >= bmap->n_eq)
4596 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4597 "unable to find suitable equality", return NULL);
4598 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4600 aff = isl_aff_remove_unused_divs(aff);
4601 return aff;
4604 /* Given a basic map where each output dimension is defined
4605 * in terms of the parameters and input dimensions using an equality,
4606 * extract an isl_multi_aff that expresses the output dimensions in terms
4607 * of the parameters and input dimensions.
4609 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4610 __isl_take isl_basic_map *bmap)
4612 int i;
4613 isl_size n_out;
4614 isl_multi_aff *ma;
4616 if (!bmap)
4617 return NULL;
4619 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4620 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4621 if (n_out < 0)
4622 ma = isl_multi_aff_free(ma);
4624 for (i = 0; i < n_out; ++i) {
4625 isl_aff *aff;
4627 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4628 ma = isl_multi_aff_set_aff(ma, i, aff);
4631 isl_basic_map_free(bmap);
4633 return ma;
4636 /* Given a basic set where each set dimension is defined
4637 * in terms of the parameters using an equality,
4638 * extract an isl_multi_aff that expresses the set dimensions in terms
4639 * of the parameters.
4641 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4642 __isl_take isl_basic_set *bset)
4644 return extract_isl_multi_aff_from_basic_map(bset);
4647 /* Create an isl_pw_multi_aff that is equivalent to
4648 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4649 * The given basic map is such that each output dimension is defined
4650 * in terms of the parameters and input dimensions using an equality.
4652 * Since some applications expect the result of isl_pw_multi_aff_from_map
4653 * to only contain integer affine expressions, we compute the floor
4654 * of the expression before returning.
4656 * Remove all constraints involving local variables without
4657 * an explicit representation (resulting in the removal of those
4658 * local variables) prior to the actual extraction to ensure
4659 * that the local spaces in which the resulting affine expressions
4660 * are created do not contain any unknown local variables.
4661 * Removing such constraints is safe because constraints involving
4662 * unknown local variables are not used to determine whether
4663 * a basic map is obviously single-valued.
4665 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4666 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4668 isl_multi_aff *ma;
4670 bmap = isl_basic_map_drop_constraint_involving_unknown_divs(bmap);
4671 ma = extract_isl_multi_aff_from_basic_map(bmap);
4672 ma = isl_multi_aff_floor(ma);
4673 return isl_pw_multi_aff_alloc(domain, ma);
4676 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4677 * This obviously only works if the input "map" is single-valued.
4678 * If so, we compute the lexicographic minimum of the image in the form
4679 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4680 * to its lexicographic minimum.
4681 * If the input is not single-valued, we produce an error.
4683 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4684 __isl_take isl_map *map)
4686 int i;
4687 int sv;
4688 isl_pw_multi_aff *pma;
4690 sv = isl_map_is_single_valued(map);
4691 if (sv < 0)
4692 goto error;
4693 if (!sv)
4694 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4695 "map is not single-valued", goto error);
4696 map = isl_map_make_disjoint(map);
4697 if (!map)
4698 return NULL;
4700 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4702 for (i = 0; i < map->n; ++i) {
4703 isl_pw_multi_aff *pma_i;
4704 isl_basic_map *bmap;
4705 bmap = isl_basic_map_copy(map->p[i]);
4706 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4707 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4710 isl_map_free(map);
4711 return pma;
4712 error:
4713 isl_map_free(map);
4714 return NULL;
4717 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4718 * taking into account that the output dimension at position "d"
4719 * can be represented as
4721 * x = floor((e(...) + c1) / m)
4723 * given that constraint "i" is of the form
4725 * e(...) + c1 - m x >= 0
4728 * Let "map" be of the form
4730 * A -> B
4732 * We construct a mapping
4734 * A -> [A -> x = floor(...)]
4736 * apply that to the map, obtaining
4738 * [A -> x = floor(...)] -> B
4740 * and equate dimension "d" to x.
4741 * We then compute a isl_pw_multi_aff representation of the resulting map
4742 * and plug in the mapping above.
4744 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4745 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4747 isl_ctx *ctx;
4748 isl_space *space = NULL;
4749 isl_local_space *ls;
4750 isl_multi_aff *ma;
4751 isl_aff *aff;
4752 isl_vec *v;
4753 isl_map *insert;
4754 int offset;
4755 isl_size n;
4756 isl_size n_in;
4757 isl_pw_multi_aff *pma;
4758 isl_bool is_set;
4760 is_set = isl_map_is_set(map);
4761 if (is_set < 0)
4762 goto error;
4764 offset = isl_basic_map_offset(hull, isl_dim_out);
4765 ctx = isl_map_get_ctx(map);
4766 space = isl_space_domain(isl_map_get_space(map));
4767 n_in = isl_space_dim(space, isl_dim_set);
4768 n = isl_space_dim(space, isl_dim_all);
4769 if (n_in < 0 || n < 0)
4770 goto error;
4772 v = isl_vec_alloc(ctx, 1 + 1 + n);
4773 if (v) {
4774 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4775 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4777 isl_basic_map_free(hull);
4779 ls = isl_local_space_from_space(isl_space_copy(space));
4780 aff = isl_aff_alloc_vec(ls, v);
4781 aff = isl_aff_floor(aff);
4782 if (is_set) {
4783 isl_space_free(space);
4784 ma = isl_multi_aff_from_aff(aff);
4785 } else {
4786 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4787 ma = isl_multi_aff_range_product(ma,
4788 isl_multi_aff_from_aff(aff));
4791 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4792 map = isl_map_apply_domain(map, insert);
4793 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4794 pma = isl_pw_multi_aff_from_map(map);
4795 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4797 return pma;
4798 error:
4799 isl_space_free(space);
4800 isl_map_free(map);
4801 isl_basic_map_free(hull);
4802 return NULL;
4805 /* Is constraint "c" of the form
4807 * e(...) + c1 - m x >= 0
4809 * or
4811 * -e(...) + c2 + m x >= 0
4813 * where m > 1 and e only depends on parameters and input dimemnsions?
4815 * "offset" is the offset of the output dimensions
4816 * "pos" is the position of output dimension x.
4818 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4820 if (isl_int_is_zero(c[offset + d]))
4821 return 0;
4822 if (isl_int_is_one(c[offset + d]))
4823 return 0;
4824 if (isl_int_is_negone(c[offset + d]))
4825 return 0;
4826 if (isl_seq_first_non_zero(c + offset, d) != -1)
4827 return 0;
4828 if (isl_seq_first_non_zero(c + offset + d + 1,
4829 total - (offset + d + 1)) != -1)
4830 return 0;
4831 return 1;
4834 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4836 * As a special case, we first check if there is any pair of constraints,
4837 * shared by all the basic maps in "map" that force a given dimension
4838 * to be equal to the floor of some affine combination of the input dimensions.
4840 * In particular, if we can find two constraints
4842 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4844 * and
4846 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4848 * where m > 1 and e only depends on parameters and input dimemnsions,
4849 * and such that
4851 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4853 * then we know that we can take
4855 * x = floor((e(...) + c1) / m)
4857 * without having to perform any computation.
4859 * Note that we know that
4861 * c1 + c2 >= 1
4863 * If c1 + c2 were 0, then we would have detected an equality during
4864 * simplification. If c1 + c2 were negative, then we would have detected
4865 * a contradiction.
4867 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4868 __isl_take isl_map *map)
4870 int d;
4871 isl_size dim;
4872 int i, j, n;
4873 int offset;
4874 isl_size total;
4875 isl_int sum;
4876 isl_basic_map *hull;
4878 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4879 dim = isl_map_dim(map, isl_dim_out);
4880 total = isl_basic_map_dim(hull, isl_dim_all);
4881 if (dim < 0 || total < 0)
4882 goto error;
4884 isl_int_init(sum);
4885 offset = isl_basic_map_offset(hull, isl_dim_out);
4886 n = hull->n_ineq;
4887 for (d = 0; d < dim; ++d) {
4888 for (i = 0; i < n; ++i) {
4889 if (!is_potential_div_constraint(hull->ineq[i],
4890 offset, d, 1 + total))
4891 continue;
4892 for (j = i + 1; j < n; ++j) {
4893 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4894 hull->ineq[j] + 1, total))
4895 continue;
4896 isl_int_add(sum, hull->ineq[i][0],
4897 hull->ineq[j][0]);
4898 if (isl_int_abs_lt(sum,
4899 hull->ineq[i][offset + d]))
4900 break;
4903 if (j >= n)
4904 continue;
4905 isl_int_clear(sum);
4906 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4907 j = i;
4908 return pw_multi_aff_from_map_div(map, hull, d, j);
4911 isl_int_clear(sum);
4912 isl_basic_map_free(hull);
4913 return pw_multi_aff_from_map_base(map);
4914 error:
4915 isl_map_free(map);
4916 isl_basic_map_free(hull);
4917 return NULL;
4920 /* Given an affine expression
4922 * [A -> B] -> f(A,B)
4924 * construct an isl_multi_aff
4926 * [A -> B] -> B'
4928 * such that dimension "d" in B' is set to "aff" and the remaining
4929 * dimensions are set equal to the corresponding dimensions in B.
4930 * "n_in" is the dimension of the space A.
4931 * "n_out" is the dimension of the space B.
4933 * If "is_set" is set, then the affine expression is of the form
4935 * [B] -> f(B)
4937 * and we construct an isl_multi_aff
4939 * B -> B'
4941 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
4942 unsigned n_in, unsigned n_out, int is_set)
4944 int i;
4945 isl_multi_aff *ma;
4946 isl_space *space, *space2;
4947 isl_local_space *ls;
4949 space = isl_aff_get_domain_space(aff);
4950 ls = isl_local_space_from_space(isl_space_copy(space));
4951 space2 = isl_space_copy(space);
4952 if (!is_set)
4953 space2 = isl_space_range(isl_space_unwrap(space2));
4954 space = isl_space_map_from_domain_and_range(space, space2);
4955 ma = isl_multi_aff_alloc(space);
4956 ma = isl_multi_aff_set_aff(ma, d, aff);
4958 for (i = 0; i < n_out; ++i) {
4959 if (i == d)
4960 continue;
4961 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4962 isl_dim_set, n_in + i);
4963 ma = isl_multi_aff_set_aff(ma, i, aff);
4966 isl_local_space_free(ls);
4968 return ma;
4971 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4972 * taking into account that the dimension at position "d" can be written as
4974 * x = m a + f(..) (1)
4976 * where m is equal to "gcd".
4977 * "i" is the index of the equality in "hull" that defines f(..).
4978 * In particular, the equality is of the form
4980 * f(..) - x + m g(existentials) = 0
4982 * or
4984 * -f(..) + x + m g(existentials) = 0
4986 * We basically plug (1) into "map", resulting in a map with "a"
4987 * in the range instead of "x". The corresponding isl_pw_multi_aff
4988 * defining "a" is then plugged back into (1) to obtain a definition for "x".
4990 * Specifically, given the input map
4992 * A -> B
4994 * We first wrap it into a set
4996 * [A -> B]
4998 * and define (1) on top of the corresponding space, resulting in "aff".
4999 * We use this to create an isl_multi_aff that maps the output position "d"
5000 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5001 * We plug this into the wrapped map, unwrap the result and compute the
5002 * corresponding isl_pw_multi_aff.
5003 * The result is an expression
5005 * A -> T(A)
5007 * We adjust that to
5009 * A -> [A -> T(A)]
5011 * so that we can plug that into "aff", after extending the latter to
5012 * a mapping
5014 * [A -> B] -> B'
5017 * If "map" is actually a set, then there is no "A" space, meaning
5018 * that we do not need to perform any wrapping, and that the result
5019 * of the recursive call is of the form
5021 * [T]
5023 * which is plugged into a mapping of the form
5025 * B -> B'
5027 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5028 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5029 isl_int gcd)
5031 isl_set *set;
5032 isl_space *space;
5033 isl_local_space *ls;
5034 isl_aff *aff;
5035 isl_multi_aff *ma;
5036 isl_pw_multi_aff *pma, *id;
5037 isl_size n_in;
5038 unsigned o_out;
5039 isl_size n_out;
5040 isl_bool is_set;
5042 is_set = isl_map_is_set(map);
5043 if (is_set < 0)
5044 goto error;
5046 n_in = isl_basic_map_dim(hull, isl_dim_in);
5047 n_out = isl_basic_map_dim(hull, isl_dim_out);
5048 if (n_in < 0 || n_out < 0)
5049 goto error;
5050 o_out = isl_basic_map_offset(hull, isl_dim_out);
5052 if (is_set)
5053 set = map;
5054 else
5055 set = isl_map_wrap(map);
5056 space = isl_space_map_from_set(isl_set_get_space(set));
5057 ma = isl_multi_aff_identity(space);
5058 ls = isl_local_space_from_space(isl_set_get_space(set));
5059 aff = isl_aff_alloc(ls);
5060 if (aff) {
5061 isl_int_set_si(aff->v->el[0], 1);
5062 if (isl_int_is_one(hull->eq[i][o_out + d]))
5063 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5064 aff->v->size - 1);
5065 else
5066 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5067 aff->v->size - 1);
5068 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5070 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5071 set = isl_set_preimage_multi_aff(set, ma);
5073 ma = range_map(aff, d, n_in, n_out, is_set);
5075 if (is_set)
5076 map = set;
5077 else
5078 map = isl_set_unwrap(set);
5079 pma = isl_pw_multi_aff_from_map(map);
5081 if (!is_set) {
5082 space = isl_pw_multi_aff_get_domain_space(pma);
5083 space = isl_space_map_from_set(space);
5084 id = isl_pw_multi_aff_identity(space);
5085 pma = isl_pw_multi_aff_range_product(id, pma);
5087 id = isl_pw_multi_aff_from_multi_aff(ma);
5088 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5090 isl_basic_map_free(hull);
5091 return pma;
5092 error:
5093 isl_map_free(map);
5094 isl_basic_map_free(hull);
5095 return NULL;
5098 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5099 * "hull" contains the equalities valid for "map".
5101 * Check if any of the output dimensions is "strided".
5102 * That is, we check if it can be written as
5104 * x = m a + f(..)
5106 * with m greater than 1, a some combination of existentially quantified
5107 * variables and f an expression in the parameters and input dimensions.
5108 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5110 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5111 * special case.
5113 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5114 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5116 int i, j;
5117 isl_size n_out;
5118 unsigned o_out;
5119 isl_size n_div;
5120 unsigned o_div;
5121 isl_int gcd;
5123 n_div = isl_basic_map_dim(hull, isl_dim_div);
5124 n_out = isl_basic_map_dim(hull, isl_dim_out);
5125 if (n_div < 0 || n_out < 0)
5126 goto error;
5128 if (n_div == 0) {
5129 isl_basic_map_free(hull);
5130 return pw_multi_aff_from_map_check_div(map);
5133 isl_int_init(gcd);
5135 o_div = isl_basic_map_offset(hull, isl_dim_div);
5136 o_out = isl_basic_map_offset(hull, isl_dim_out);
5138 for (i = 0; i < n_out; ++i) {
5139 for (j = 0; j < hull->n_eq; ++j) {
5140 isl_int *eq = hull->eq[j];
5141 isl_pw_multi_aff *res;
5143 if (!isl_int_is_one(eq[o_out + i]) &&
5144 !isl_int_is_negone(eq[o_out + i]))
5145 continue;
5146 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5147 continue;
5148 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5149 n_out - (i + 1)) != -1)
5150 continue;
5151 isl_seq_gcd(eq + o_div, n_div, &gcd);
5152 if (isl_int_is_zero(gcd))
5153 continue;
5154 if (isl_int_is_one(gcd))
5155 continue;
5157 res = pw_multi_aff_from_map_stride(map, hull,
5158 i, j, gcd);
5159 isl_int_clear(gcd);
5160 return res;
5164 isl_int_clear(gcd);
5165 isl_basic_map_free(hull);
5166 return pw_multi_aff_from_map_check_div(map);
5167 error:
5168 isl_map_free(map);
5169 isl_basic_map_free(hull);
5170 return NULL;
5173 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5175 * As a special case, we first check if all output dimensions are uniquely
5176 * defined in terms of the parameters and input dimensions over the entire
5177 * domain. If so, we extract the desired isl_pw_multi_aff directly
5178 * from the affine hull of "map" and its domain.
5180 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5181 * special cases.
5183 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5185 isl_bool sv;
5186 isl_size n;
5187 isl_basic_map *hull;
5189 n = isl_map_n_basic_map(map);
5190 if (n < 0)
5191 goto error;
5193 if (n == 1) {
5194 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5195 hull = isl_basic_map_plain_affine_hull(hull);
5196 sv = isl_basic_map_plain_is_single_valued(hull);
5197 if (sv >= 0 && sv)
5198 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5199 hull);
5200 isl_basic_map_free(hull);
5202 map = isl_map_detect_equalities(map);
5203 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5204 sv = isl_basic_map_plain_is_single_valued(hull);
5205 if (sv >= 0 && sv)
5206 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5207 if (sv >= 0)
5208 return pw_multi_aff_from_map_check_strides(map, hull);
5209 isl_basic_map_free(hull);
5210 error:
5211 isl_map_free(map);
5212 return NULL;
5215 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5217 return isl_pw_multi_aff_from_map(set);
5220 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5221 * add it to *user.
5223 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5225 isl_union_pw_multi_aff **upma = user;
5226 isl_pw_multi_aff *pma;
5228 pma = isl_pw_multi_aff_from_map(map);
5229 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5231 return *upma ? isl_stat_ok : isl_stat_error;
5234 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5235 * domain.
5237 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5238 __isl_take isl_aff *aff)
5240 isl_multi_aff *ma;
5241 isl_pw_multi_aff *pma;
5243 ma = isl_multi_aff_from_aff(aff);
5244 pma = isl_pw_multi_aff_from_multi_aff(ma);
5245 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5248 /* Try and create an isl_union_pw_multi_aff that is equivalent
5249 * to the given isl_union_map.
5250 * The isl_union_map is required to be single-valued in each space.
5251 * Otherwise, an error is produced.
5253 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5254 __isl_take isl_union_map *umap)
5256 isl_space *space;
5257 isl_union_pw_multi_aff *upma;
5259 space = isl_union_map_get_space(umap);
5260 upma = isl_union_pw_multi_aff_empty(space);
5261 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5262 upma = isl_union_pw_multi_aff_free(upma);
5263 isl_union_map_free(umap);
5265 return upma;
5268 /* Try and create an isl_union_pw_multi_aff that is equivalent
5269 * to the given isl_union_set.
5270 * The isl_union_set is required to be a singleton in each space.
5271 * Otherwise, an error is produced.
5273 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5274 __isl_take isl_union_set *uset)
5276 return isl_union_pw_multi_aff_from_union_map(uset);
5279 /* Return the piecewise affine expression "set ? 1 : 0".
5281 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5283 isl_pw_aff *pa;
5284 isl_space *space = isl_set_get_space(set);
5285 isl_local_space *ls = isl_local_space_from_space(space);
5286 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5287 isl_aff *one = isl_aff_zero_on_domain(ls);
5289 one = isl_aff_add_constant_si(one, 1);
5290 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5291 set = isl_set_complement(set);
5292 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5294 return pa;
5297 /* Plug in "subs" for dimension "type", "pos" of "aff".
5299 * Let i be the dimension to replace and let "subs" be of the form
5301 * f/d
5303 * and "aff" of the form
5305 * (a i + g)/m
5307 * The result is
5309 * (a f + d g')/(m d)
5311 * where g' is the result of plugging in "subs" in each of the integer
5312 * divisions in g.
5314 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5315 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5317 isl_ctx *ctx;
5318 isl_int v;
5319 isl_size n_div;
5321 aff = isl_aff_cow(aff);
5322 if (!aff || !subs)
5323 return isl_aff_free(aff);
5325 ctx = isl_aff_get_ctx(aff);
5326 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5327 isl_die(ctx, isl_error_invalid,
5328 "spaces don't match", return isl_aff_free(aff));
5329 n_div = isl_local_space_dim(subs->ls, isl_dim_div);
5330 if (n_div < 0)
5331 return isl_aff_free(aff);
5332 if (n_div != 0)
5333 isl_die(ctx, isl_error_unsupported,
5334 "cannot handle divs yet", return isl_aff_free(aff));
5336 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5337 if (!aff->ls)
5338 return isl_aff_free(aff);
5340 aff->v = isl_vec_cow(aff->v);
5341 if (!aff->v)
5342 return isl_aff_free(aff);
5344 pos += isl_local_space_offset(aff->ls, type);
5346 isl_int_init(v);
5347 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5348 aff->v->size, subs->v->size, v);
5349 isl_int_clear(v);
5351 return aff;
5354 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5355 * expressions in "maff".
5357 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5358 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5359 __isl_keep isl_aff *subs)
5361 int i;
5363 maff = isl_multi_aff_cow(maff);
5364 if (!maff || !subs)
5365 return isl_multi_aff_free(maff);
5367 if (type == isl_dim_in)
5368 type = isl_dim_set;
5370 for (i = 0; i < maff->n; ++i) {
5371 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5372 type, pos, subs);
5373 if (!maff->u.p[i])
5374 return isl_multi_aff_free(maff);
5377 return maff;
5380 /* Plug in "subs" for dimension "type", "pos" of "pma".
5382 * pma is of the form
5384 * A_i(v) -> M_i(v)
5386 * while subs is of the form
5388 * v' = B_j(v) -> S_j
5390 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5391 * has a contribution in the result, in particular
5393 * C_ij(S_j) -> M_i(S_j)
5395 * Note that plugging in S_j in C_ij may also result in an empty set
5396 * and this contribution should simply be discarded.
5398 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5399 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5400 __isl_keep isl_pw_aff *subs)
5402 int i, j, n;
5403 isl_pw_multi_aff *res;
5405 if (!pma || !subs)
5406 return isl_pw_multi_aff_free(pma);
5408 n = pma->n * subs->n;
5409 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5411 for (i = 0; i < pma->n; ++i) {
5412 for (j = 0; j < subs->n; ++j) {
5413 isl_set *common;
5414 isl_multi_aff *res_ij;
5415 int empty;
5417 common = isl_set_intersect(
5418 isl_set_copy(pma->p[i].set),
5419 isl_set_copy(subs->p[j].set));
5420 common = isl_set_substitute(common,
5421 type, pos, subs->p[j].aff);
5422 empty = isl_set_plain_is_empty(common);
5423 if (empty < 0 || empty) {
5424 isl_set_free(common);
5425 if (empty < 0)
5426 goto error;
5427 continue;
5430 res_ij = isl_multi_aff_substitute(
5431 isl_multi_aff_copy(pma->p[i].maff),
5432 type, pos, subs->p[j].aff);
5434 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5438 isl_pw_multi_aff_free(pma);
5439 return res;
5440 error:
5441 isl_pw_multi_aff_free(pma);
5442 isl_pw_multi_aff_free(res);
5443 return NULL;
5446 /* Compute the preimage of a range of dimensions in the affine expression "src"
5447 * under "ma" and put the result in "dst". The number of dimensions in "src"
5448 * that precede the range is given by "n_before". The number of dimensions
5449 * in the range is given by the number of output dimensions of "ma".
5450 * The number of dimensions that follow the range is given by "n_after".
5451 * If "has_denom" is set (to one),
5452 * then "src" and "dst" have an extra initial denominator.
5453 * "n_div_ma" is the number of existentials in "ma"
5454 * "n_div_bset" is the number of existentials in "src"
5455 * The resulting "dst" (which is assumed to have been allocated by
5456 * the caller) contains coefficients for both sets of existentials,
5457 * first those in "ma" and then those in "src".
5458 * f, c1, c2 and g are temporary objects that have been initialized
5459 * by the caller.
5461 * Let src represent the expression
5463 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5465 * and let ma represent the expressions
5467 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5469 * We start out with the following expression for dst:
5471 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5473 * with the multiplication factor f initially equal to 1
5474 * and f \sum_i b_i v_i kept separately.
5475 * For each x_i that we substitute, we multiply the numerator
5476 * (and denominator) of dst by c_1 = m_i and add the numerator
5477 * of the x_i expression multiplied by c_2 = f b_i,
5478 * after removing the common factors of c_1 and c_2.
5479 * The multiplication factor f also needs to be multiplied by c_1
5480 * for the next x_j, j > i.
5482 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5483 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5484 int n_div_ma, int n_div_bmap,
5485 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5487 int i;
5488 isl_size n_param, n_in, n_out;
5489 int o_dst, o_src;
5491 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5492 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5493 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5494 if (n_param < 0 || n_in < 0 || n_out < 0)
5495 return isl_stat_error;
5497 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5498 o_dst = o_src = has_denom + 1 + n_param + n_before;
5499 isl_seq_clr(dst + o_dst, n_in);
5500 o_dst += n_in;
5501 o_src += n_out;
5502 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5503 o_dst += n_after;
5504 o_src += n_after;
5505 isl_seq_clr(dst + o_dst, n_div_ma);
5506 o_dst += n_div_ma;
5507 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5509 isl_int_set_si(f, 1);
5511 for (i = 0; i < n_out; ++i) {
5512 int offset = has_denom + 1 + n_param + n_before + i;
5514 if (isl_int_is_zero(src[offset]))
5515 continue;
5516 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5517 isl_int_mul(c2, f, src[offset]);
5518 isl_int_gcd(g, c1, c2);
5519 isl_int_divexact(c1, c1, g);
5520 isl_int_divexact(c2, c2, g);
5522 isl_int_mul(f, f, c1);
5523 o_dst = has_denom;
5524 o_src = 1;
5525 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5526 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5527 o_dst += 1 + n_param;
5528 o_src += 1 + n_param;
5529 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5530 o_dst += n_before;
5531 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5532 c2, ma->u.p[i]->v->el + o_src, n_in);
5533 o_dst += n_in;
5534 o_src += n_in;
5535 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5536 o_dst += n_after;
5537 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5538 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5539 o_dst += n_div_ma;
5540 o_src += n_div_ma;
5541 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5542 if (has_denom)
5543 isl_int_mul(dst[0], dst[0], c1);
5546 return isl_stat_ok;
5549 /* Compute the pullback of "aff" by the function represented by "ma".
5550 * In other words, plug in "ma" in "aff". The result is an affine expression
5551 * defined over the domain space of "ma".
5553 * If "aff" is represented by
5555 * (a(p) + b x + c(divs))/d
5557 * and ma is represented by
5559 * x = D(p) + F(y) + G(divs')
5561 * then the result is
5563 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5565 * The divs in the local space of the input are similarly adjusted
5566 * through a call to isl_local_space_preimage_multi_aff.
5568 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5569 __isl_take isl_multi_aff *ma)
5571 isl_aff *res = NULL;
5572 isl_local_space *ls;
5573 isl_size n_div_aff, n_div_ma;
5574 isl_int f, c1, c2, g;
5576 ma = isl_multi_aff_align_divs(ma);
5577 if (!aff || !ma)
5578 goto error;
5580 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5581 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5582 if (n_div_aff < 0 || n_div_ma < 0)
5583 goto error;
5585 ls = isl_aff_get_domain_local_space(aff);
5586 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5587 res = isl_aff_alloc(ls);
5588 if (!res)
5589 goto error;
5591 isl_int_init(f);
5592 isl_int_init(c1);
5593 isl_int_init(c2);
5594 isl_int_init(g);
5596 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5597 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5598 res = isl_aff_free(res);
5600 isl_int_clear(f);
5601 isl_int_clear(c1);
5602 isl_int_clear(c2);
5603 isl_int_clear(g);
5605 isl_aff_free(aff);
5606 isl_multi_aff_free(ma);
5607 res = isl_aff_normalize(res);
5608 return res;
5609 error:
5610 isl_aff_free(aff);
5611 isl_multi_aff_free(ma);
5612 isl_aff_free(res);
5613 return NULL;
5616 /* Compute the pullback of "aff1" by the function represented by "aff2".
5617 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5618 * defined over the domain space of "aff1".
5620 * The domain of "aff1" should match the range of "aff2", which means
5621 * that it should be single-dimensional.
5623 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5624 __isl_take isl_aff *aff2)
5626 isl_multi_aff *ma;
5628 ma = isl_multi_aff_from_aff(aff2);
5629 return isl_aff_pullback_multi_aff(aff1, ma);
5632 /* Compute the pullback of "ma1" by the function represented by "ma2".
5633 * In other words, plug in "ma2" in "ma1".
5635 * The parameters of "ma1" and "ma2" are assumed to have been aligned.
5637 static __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff_aligned(
5638 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5640 int i;
5641 isl_space *space = NULL;
5643 ma2 = isl_multi_aff_align_divs(ma2);
5644 ma1 = isl_multi_aff_cow(ma1);
5645 if (!ma1 || !ma2)
5646 goto error;
5648 space = isl_space_join(isl_multi_aff_get_space(ma2),
5649 isl_multi_aff_get_space(ma1));
5651 for (i = 0; i < ma1->n; ++i) {
5652 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5653 isl_multi_aff_copy(ma2));
5654 if (!ma1->u.p[i])
5655 goto error;
5658 ma1 = isl_multi_aff_reset_space(ma1, space);
5659 isl_multi_aff_free(ma2);
5660 return ma1;
5661 error:
5662 isl_space_free(space);
5663 isl_multi_aff_free(ma2);
5664 isl_multi_aff_free(ma1);
5665 return NULL;
5668 /* Compute the pullback of "ma1" by the function represented by "ma2".
5669 * In other words, plug in "ma2" in "ma1".
5671 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5672 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5674 return isl_multi_aff_align_params_multi_multi_and(ma1, ma2,
5675 &isl_multi_aff_pullback_multi_aff_aligned);
5678 /* Extend the local space of "dst" to include the divs
5679 * in the local space of "src".
5681 * If "src" does not have any divs or if the local spaces of "dst" and
5682 * "src" are the same, then no extension is required.
5684 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5685 __isl_keep isl_aff *src)
5687 isl_ctx *ctx;
5688 isl_size src_n_div, dst_n_div;
5689 int *exp1 = NULL;
5690 int *exp2 = NULL;
5691 isl_bool equal;
5692 isl_mat *div;
5694 if (!src || !dst)
5695 return isl_aff_free(dst);
5697 ctx = isl_aff_get_ctx(src);
5698 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5699 if (equal < 0)
5700 return isl_aff_free(dst);
5701 if (!equal)
5702 isl_die(ctx, isl_error_invalid,
5703 "spaces don't match", goto error);
5705 src_n_div = isl_local_space_dim(src->ls, isl_dim_div);
5706 dst_n_div = isl_local_space_dim(dst->ls, isl_dim_div);
5707 if (src_n_div == 0)
5708 return dst;
5709 equal = isl_local_space_is_equal(src->ls, dst->ls);
5710 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5711 return isl_aff_free(dst);
5712 if (equal)
5713 return dst;
5715 exp1 = isl_alloc_array(ctx, int, src_n_div);
5716 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5717 if (!exp1 || (dst_n_div && !exp2))
5718 goto error;
5720 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5721 dst = isl_aff_expand_divs(dst, div, exp2);
5722 free(exp1);
5723 free(exp2);
5725 return dst;
5726 error:
5727 free(exp1);
5728 free(exp2);
5729 return isl_aff_free(dst);
5732 /* Adjust the local spaces of the affine expressions in "maff"
5733 * such that they all have the save divs.
5735 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5736 __isl_take isl_multi_aff *maff)
5738 int i;
5740 if (!maff)
5741 return NULL;
5742 if (maff->n == 0)
5743 return maff;
5744 maff = isl_multi_aff_cow(maff);
5745 if (!maff)
5746 return NULL;
5748 for (i = 1; i < maff->n; ++i)
5749 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5750 for (i = 1; i < maff->n; ++i) {
5751 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5752 if (!maff->u.p[i])
5753 return isl_multi_aff_free(maff);
5756 return maff;
5759 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5761 aff = isl_aff_cow(aff);
5762 if (!aff)
5763 return NULL;
5765 aff->ls = isl_local_space_lift(aff->ls);
5766 if (!aff->ls)
5767 return isl_aff_free(aff);
5769 return aff;
5772 /* Lift "maff" to a space with extra dimensions such that the result
5773 * has no more existentially quantified variables.
5774 * If "ls" is not NULL, then *ls is assigned the local space that lies
5775 * at the basis of the lifting applied to "maff".
5777 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5778 __isl_give isl_local_space **ls)
5780 int i;
5781 isl_space *space;
5782 isl_size n_div;
5784 if (ls)
5785 *ls = NULL;
5787 if (!maff)
5788 return NULL;
5790 if (maff->n == 0) {
5791 if (ls) {
5792 isl_space *space = isl_multi_aff_get_domain_space(maff);
5793 *ls = isl_local_space_from_space(space);
5794 if (!*ls)
5795 return isl_multi_aff_free(maff);
5797 return maff;
5800 maff = isl_multi_aff_cow(maff);
5801 maff = isl_multi_aff_align_divs(maff);
5802 if (!maff)
5803 return NULL;
5805 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5806 if (n_div < 0)
5807 return isl_multi_aff_free(maff);
5808 space = isl_multi_aff_get_space(maff);
5809 space = isl_space_lift(isl_space_domain(space), n_div);
5810 space = isl_space_extend_domain_with_range(space,
5811 isl_multi_aff_get_space(maff));
5812 if (!space)
5813 return isl_multi_aff_free(maff);
5814 isl_space_free(maff->space);
5815 maff->space = space;
5817 if (ls) {
5818 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5819 if (!*ls)
5820 return isl_multi_aff_free(maff);
5823 for (i = 0; i < maff->n; ++i) {
5824 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5825 if (!maff->u.p[i])
5826 goto error;
5829 return maff;
5830 error:
5831 if (ls)
5832 isl_local_space_free(*ls);
5833 return isl_multi_aff_free(maff);
5836 #undef TYPE
5837 #define TYPE isl_pw_multi_aff
5838 static
5839 #include "check_type_range_templ.c"
5841 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5843 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5844 __isl_keep isl_pw_multi_aff *pma, int pos)
5846 int i;
5847 isl_size n_out;
5848 isl_space *space;
5849 isl_pw_aff *pa;
5851 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
5852 return NULL;
5854 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5855 if (n_out < 0)
5856 return NULL;
5858 space = isl_pw_multi_aff_get_space(pma);
5859 space = isl_space_drop_dims(space, isl_dim_out,
5860 pos + 1, n_out - pos - 1);
5861 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5863 pa = isl_pw_aff_alloc_size(space, pma->n);
5864 for (i = 0; i < pma->n; ++i) {
5865 isl_aff *aff;
5866 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5867 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5870 return pa;
5873 /* Return an isl_pw_multi_aff with the given "set" as domain and
5874 * an unnamed zero-dimensional range.
5876 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5877 __isl_take isl_set *set)
5879 isl_multi_aff *ma;
5880 isl_space *space;
5882 space = isl_set_get_space(set);
5883 space = isl_space_from_domain(space);
5884 ma = isl_multi_aff_zero(space);
5885 return isl_pw_multi_aff_alloc(set, ma);
5888 /* Add an isl_pw_multi_aff with the given "set" as domain and
5889 * an unnamed zero-dimensional range to *user.
5891 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5892 void *user)
5894 isl_union_pw_multi_aff **upma = user;
5895 isl_pw_multi_aff *pma;
5897 pma = isl_pw_multi_aff_from_domain(set);
5898 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5900 return isl_stat_ok;
5903 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5904 * an unnamed zero-dimensional range.
5906 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5907 __isl_take isl_union_set *uset)
5909 isl_space *space;
5910 isl_union_pw_multi_aff *upma;
5912 if (!uset)
5913 return NULL;
5915 space = isl_union_set_get_space(uset);
5916 upma = isl_union_pw_multi_aff_empty(space);
5918 if (isl_union_set_foreach_set(uset,
5919 &add_pw_multi_aff_from_domain, &upma) < 0)
5920 goto error;
5922 isl_union_set_free(uset);
5923 return upma;
5924 error:
5925 isl_union_set_free(uset);
5926 isl_union_pw_multi_aff_free(upma);
5927 return NULL;
5930 /* Local data for bin_entry and the callback "fn".
5932 struct isl_union_pw_multi_aff_bin_data {
5933 isl_union_pw_multi_aff *upma2;
5934 isl_union_pw_multi_aff *res;
5935 isl_pw_multi_aff *pma;
5936 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
5939 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
5940 * and call data->fn for each isl_pw_multi_aff in data->upma2.
5942 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
5944 struct isl_union_pw_multi_aff_bin_data *data = user;
5945 isl_stat r;
5947 data->pma = pma;
5948 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
5949 data->fn, data);
5950 isl_pw_multi_aff_free(pma);
5952 return r;
5955 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
5956 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
5957 * passed as user field) and the isl_pw_multi_aff from upma2 is available
5958 * as *entry. The callback should adjust data->res if desired.
5960 static __isl_give isl_union_pw_multi_aff *bin_op(
5961 __isl_take isl_union_pw_multi_aff *upma1,
5962 __isl_take isl_union_pw_multi_aff *upma2,
5963 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
5965 isl_space *space;
5966 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
5968 space = isl_union_pw_multi_aff_get_space(upma2);
5969 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
5970 space = isl_union_pw_multi_aff_get_space(upma1);
5971 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
5973 if (!upma1 || !upma2)
5974 goto error;
5976 data.upma2 = upma2;
5977 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
5978 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
5979 &bin_entry, &data) < 0)
5980 goto error;
5982 isl_union_pw_multi_aff_free(upma1);
5983 isl_union_pw_multi_aff_free(upma2);
5984 return data.res;
5985 error:
5986 isl_union_pw_multi_aff_free(upma1);
5987 isl_union_pw_multi_aff_free(upma2);
5988 isl_union_pw_multi_aff_free(data.res);
5989 return NULL;
5992 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
5993 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
5995 static __isl_give isl_pw_multi_aff *pw_multi_aff_range_product(
5996 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
5998 isl_space *space;
6000 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6001 isl_pw_multi_aff_get_space(pma2));
6002 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6003 &isl_multi_aff_range_product);
6006 /* Given two isl_pw_multi_affs A -> B and C -> D,
6007 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6009 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6010 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6012 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6013 &pw_multi_aff_range_product);
6016 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6017 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6019 static __isl_give isl_pw_multi_aff *pw_multi_aff_flat_range_product(
6020 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6022 isl_space *space;
6024 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6025 isl_pw_multi_aff_get_space(pma2));
6026 space = isl_space_flatten_range(space);
6027 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6028 &isl_multi_aff_flat_range_product);
6031 /* Given two isl_pw_multi_affs A -> B and C -> D,
6032 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6034 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6035 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6037 return isl_pw_multi_aff_align_params_pw_pw_and(pma1, pma2,
6038 &pw_multi_aff_flat_range_product);
6041 /* If data->pma and "pma2" have the same domain space, then compute
6042 * their flat range product and the result to data->res.
6044 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6045 void *user)
6047 struct isl_union_pw_multi_aff_bin_data *data = user;
6049 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6050 pma2->dim, isl_dim_in)) {
6051 isl_pw_multi_aff_free(pma2);
6052 return isl_stat_ok;
6055 pma2 = isl_pw_multi_aff_flat_range_product(
6056 isl_pw_multi_aff_copy(data->pma), pma2);
6058 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6060 return isl_stat_ok;
6063 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6064 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6066 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6067 __isl_take isl_union_pw_multi_aff *upma1,
6068 __isl_take isl_union_pw_multi_aff *upma2)
6070 return bin_op(upma1, upma2, &flat_range_product_entry);
6073 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6074 * The parameters are assumed to have been aligned.
6076 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6077 * except that it works on two different isl_pw_* types.
6079 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6080 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6081 __isl_take isl_pw_aff *pa)
6083 int i, j, n;
6084 isl_pw_multi_aff *res = NULL;
6086 if (!pma || !pa)
6087 goto error;
6089 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6090 pa->dim, isl_dim_in))
6091 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6092 "domains don't match", goto error);
6093 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6094 goto error;
6096 n = pma->n * pa->n;
6097 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6099 for (i = 0; i < pma->n; ++i) {
6100 for (j = 0; j < pa->n; ++j) {
6101 isl_set *common;
6102 isl_multi_aff *res_ij;
6103 int empty;
6105 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6106 isl_set_copy(pa->p[j].set));
6107 empty = isl_set_plain_is_empty(common);
6108 if (empty < 0 || empty) {
6109 isl_set_free(common);
6110 if (empty < 0)
6111 goto error;
6112 continue;
6115 res_ij = isl_multi_aff_set_aff(
6116 isl_multi_aff_copy(pma->p[i].maff), pos,
6117 isl_aff_copy(pa->p[j].aff));
6118 res_ij = isl_multi_aff_gist(res_ij,
6119 isl_set_copy(common));
6121 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6125 isl_pw_multi_aff_free(pma);
6126 isl_pw_aff_free(pa);
6127 return res;
6128 error:
6129 isl_pw_multi_aff_free(pma);
6130 isl_pw_aff_free(pa);
6131 return isl_pw_multi_aff_free(res);
6134 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6136 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6137 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6138 __isl_take isl_pw_aff *pa)
6140 isl_bool equal_params;
6142 if (!pma || !pa)
6143 goto error;
6144 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6145 if (equal_params < 0)
6146 goto error;
6147 if (equal_params)
6148 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6149 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6150 isl_pw_aff_check_named_params(pa) < 0)
6151 goto error;
6152 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6153 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6154 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6155 error:
6156 isl_pw_multi_aff_free(pma);
6157 isl_pw_aff_free(pa);
6158 return NULL;
6161 /* Do the parameters of "pa" match those of "space"?
6163 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6164 __isl_keep isl_space *space)
6166 isl_space *pa_space;
6167 isl_bool match;
6169 if (!pa || !space)
6170 return isl_bool_error;
6172 pa_space = isl_pw_aff_get_space(pa);
6174 match = isl_space_has_equal_params(space, pa_space);
6176 isl_space_free(pa_space);
6177 return match;
6180 /* Check that the domain space of "pa" matches "space".
6182 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6183 __isl_keep isl_space *space)
6185 isl_space *pa_space;
6186 isl_bool match;
6188 if (!pa || !space)
6189 return isl_stat_error;
6191 pa_space = isl_pw_aff_get_space(pa);
6193 match = isl_space_has_equal_params(space, pa_space);
6194 if (match < 0)
6195 goto error;
6196 if (!match)
6197 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6198 "parameters don't match", goto error);
6199 match = isl_space_tuple_is_equal(space, isl_dim_in,
6200 pa_space, isl_dim_in);
6201 if (match < 0)
6202 goto error;
6203 if (!match)
6204 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6205 "domains don't match", goto error);
6206 isl_space_free(pa_space);
6207 return isl_stat_ok;
6208 error:
6209 isl_space_free(pa_space);
6210 return isl_stat_error;
6213 #undef BASE
6214 #define BASE pw_aff
6215 #undef DOMBASE
6216 #define DOMBASE set
6218 #include <isl_multi_explicit_domain.c>
6219 #include <isl_multi_pw_aff_explicit_domain.c>
6220 #include <isl_multi_templ.c>
6221 #include <isl_multi_apply_set.c>
6222 #include <isl_multi_coalesce.c>
6223 #include <isl_multi_domain_templ.c>
6224 #include <isl_multi_dims.c>
6225 #include <isl_multi_from_base_templ.c>
6226 #include <isl_multi_gist.c>
6227 #include <isl_multi_hash.c>
6228 #include <isl_multi_identity_templ.c>
6229 #include <isl_multi_align_set.c>
6230 #include <isl_multi_intersect.c>
6231 #include <isl_multi_move_dims_templ.c>
6232 #include <isl_multi_product_templ.c>
6233 #include <isl_multi_splice_templ.c>
6234 #include <isl_multi_zero_templ.c>
6236 /* Does "mpa" have a non-trivial explicit domain?
6238 * The explicit domain, if present, is trivial if it represents
6239 * an (obviously) universe set.
6241 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6242 __isl_keep isl_multi_pw_aff *mpa)
6244 if (!mpa)
6245 return isl_bool_error;
6246 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6247 return isl_bool_false;
6248 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6251 /* Scale the elements of "pma" by the corresponding elements of "mv".
6253 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6254 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6256 int i;
6257 isl_bool equal_params;
6259 pma = isl_pw_multi_aff_cow(pma);
6260 if (!pma || !mv)
6261 goto error;
6262 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6263 mv->space, isl_dim_set))
6264 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6265 "spaces don't match", goto error);
6266 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6267 if (equal_params < 0)
6268 goto error;
6269 if (!equal_params) {
6270 pma = isl_pw_multi_aff_align_params(pma,
6271 isl_multi_val_get_space(mv));
6272 mv = isl_multi_val_align_params(mv,
6273 isl_pw_multi_aff_get_space(pma));
6274 if (!pma || !mv)
6275 goto error;
6278 for (i = 0; i < pma->n; ++i) {
6279 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6280 isl_multi_val_copy(mv));
6281 if (!pma->p[i].maff)
6282 goto error;
6285 isl_multi_val_free(mv);
6286 return pma;
6287 error:
6288 isl_multi_val_free(mv);
6289 isl_pw_multi_aff_free(pma);
6290 return NULL;
6293 /* This function is called for each entry of an isl_union_pw_multi_aff.
6294 * If the space of the entry matches that of data->mv,
6295 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6296 * Otherwise, return an empty isl_pw_multi_aff.
6298 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6299 __isl_take isl_pw_multi_aff *pma, void *user)
6301 isl_multi_val *mv = user;
6303 if (!pma)
6304 return NULL;
6305 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6306 mv->space, isl_dim_set)) {
6307 isl_space *space = isl_pw_multi_aff_get_space(pma);
6308 isl_pw_multi_aff_free(pma);
6309 return isl_pw_multi_aff_empty(space);
6312 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6315 /* Scale the elements of "upma" by the corresponding elements of "mv",
6316 * for those entries that match the space of "mv".
6318 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6319 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6321 upma = isl_union_pw_multi_aff_align_params(upma,
6322 isl_multi_val_get_space(mv));
6323 mv = isl_multi_val_align_params(mv,
6324 isl_union_pw_multi_aff_get_space(upma));
6325 if (!upma || !mv)
6326 goto error;
6328 return isl_union_pw_multi_aff_transform(upma,
6329 &union_pw_multi_aff_scale_multi_val_entry, mv);
6331 isl_multi_val_free(mv);
6332 return upma;
6333 error:
6334 isl_multi_val_free(mv);
6335 isl_union_pw_multi_aff_free(upma);
6336 return NULL;
6339 /* Construct and return a piecewise multi affine expression
6340 * in the given space with value zero in each of the output dimensions and
6341 * a universe domain.
6343 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6345 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6348 /* Construct and return a piecewise multi affine expression
6349 * that is equal to the given piecewise affine expression.
6351 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6352 __isl_take isl_pw_aff *pa)
6354 int i;
6355 isl_space *space;
6356 isl_pw_multi_aff *pma;
6358 if (!pa)
6359 return NULL;
6361 space = isl_pw_aff_get_space(pa);
6362 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6364 for (i = 0; i < pa->n; ++i) {
6365 isl_set *set;
6366 isl_multi_aff *ma;
6368 set = isl_set_copy(pa->p[i].set);
6369 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6370 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6373 isl_pw_aff_free(pa);
6374 return pma;
6377 /* Construct and return a piecewise multi affine expression
6378 * that is equal to the given multi piecewise affine expression
6379 * on the shared domain of the piecewise affine expressions,
6380 * in the special case of a 0D multi piecewise affine expression.
6382 * Create a piecewise multi affine expression with the explicit domain of
6383 * the 0D multi piecewise affine expression as domain.
6385 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6386 __isl_take isl_multi_pw_aff *mpa)
6388 isl_space *space;
6389 isl_set *dom;
6390 isl_multi_aff *ma;
6392 space = isl_multi_pw_aff_get_space(mpa);
6393 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6394 isl_multi_pw_aff_free(mpa);
6396 ma = isl_multi_aff_zero(space);
6397 return isl_pw_multi_aff_alloc(dom, ma);
6400 /* Construct and return a piecewise multi affine expression
6401 * that is equal to the given multi piecewise affine expression
6402 * on the shared domain of the piecewise affine expressions.
6404 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6405 __isl_take isl_multi_pw_aff *mpa)
6407 int i;
6408 isl_space *space;
6409 isl_pw_aff *pa;
6410 isl_pw_multi_aff *pma;
6412 if (!mpa)
6413 return NULL;
6415 if (mpa->n == 0)
6416 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6418 space = isl_multi_pw_aff_get_space(mpa);
6419 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6420 pma = isl_pw_multi_aff_from_pw_aff(pa);
6422 for (i = 1; i < mpa->n; ++i) {
6423 isl_pw_multi_aff *pma_i;
6425 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6426 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6427 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6430 pma = isl_pw_multi_aff_reset_space(pma, space);
6432 isl_multi_pw_aff_free(mpa);
6433 return pma;
6436 /* Construct and return a multi piecewise affine expression
6437 * that is equal to the given multi affine expression.
6439 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6440 __isl_take isl_multi_aff *ma)
6442 int i;
6443 isl_size n;
6444 isl_multi_pw_aff *mpa;
6446 n = isl_multi_aff_dim(ma, isl_dim_out);
6447 if (n < 0)
6448 ma = isl_multi_aff_free(ma);
6449 if (!ma)
6450 return NULL;
6452 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6454 for (i = 0; i < n; ++i) {
6455 isl_pw_aff *pa;
6457 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6458 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6461 isl_multi_aff_free(ma);
6462 return mpa;
6465 /* Construct and return a multi piecewise affine expression
6466 * that is equal to the given piecewise multi affine expression.
6468 * If the resulting multi piecewise affine expression has
6469 * an explicit domain, then assign it the domain of the input.
6470 * In other cases, the domain is stored in the individual elements.
6472 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6473 __isl_take isl_pw_multi_aff *pma)
6475 int i;
6476 isl_size n;
6477 isl_space *space;
6478 isl_multi_pw_aff *mpa;
6480 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6481 if (n < 0)
6482 pma = isl_pw_multi_aff_free(pma);
6483 space = isl_pw_multi_aff_get_space(pma);
6484 mpa = isl_multi_pw_aff_alloc(space);
6486 for (i = 0; i < n; ++i) {
6487 isl_pw_aff *pa;
6489 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6490 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6492 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6493 isl_set *dom;
6495 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6496 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6499 isl_pw_multi_aff_free(pma);
6500 return mpa;
6503 /* Do "pa1" and "pa2" represent the same function?
6505 * We first check if they are obviously equal.
6506 * If not, we convert them to maps and check if those are equal.
6508 * If "pa1" or "pa2" contain any NaNs, then they are considered
6509 * not to be the same. A NaN is not equal to anything, not even
6510 * to another NaN.
6512 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6513 __isl_keep isl_pw_aff *pa2)
6515 isl_bool equal;
6516 isl_bool has_nan;
6517 isl_map *map1, *map2;
6519 if (!pa1 || !pa2)
6520 return isl_bool_error;
6522 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6523 if (equal < 0 || equal)
6524 return equal;
6525 has_nan = either_involves_nan(pa1, pa2);
6526 if (has_nan < 0)
6527 return isl_bool_error;
6528 if (has_nan)
6529 return isl_bool_false;
6531 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6532 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6533 equal = isl_map_is_equal(map1, map2);
6534 isl_map_free(map1);
6535 isl_map_free(map2);
6537 return equal;
6540 /* Do "mpa1" and "mpa2" represent the same function?
6542 * Note that we cannot convert the entire isl_multi_pw_aff
6543 * to a map because the domains of the piecewise affine expressions
6544 * may not be the same.
6546 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6547 __isl_keep isl_multi_pw_aff *mpa2)
6549 int i;
6550 isl_bool equal, equal_params;
6552 if (!mpa1 || !mpa2)
6553 return isl_bool_error;
6555 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6556 if (equal_params < 0)
6557 return isl_bool_error;
6558 if (!equal_params) {
6559 if (!isl_space_has_named_params(mpa1->space))
6560 return isl_bool_false;
6561 if (!isl_space_has_named_params(mpa2->space))
6562 return isl_bool_false;
6563 mpa1 = isl_multi_pw_aff_copy(mpa1);
6564 mpa2 = isl_multi_pw_aff_copy(mpa2);
6565 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6566 isl_multi_pw_aff_get_space(mpa2));
6567 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6568 isl_multi_pw_aff_get_space(mpa1));
6569 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6570 isl_multi_pw_aff_free(mpa1);
6571 isl_multi_pw_aff_free(mpa2);
6572 return equal;
6575 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6576 if (equal < 0 || !equal)
6577 return equal;
6579 for (i = 0; i < mpa1->n; ++i) {
6580 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6581 if (equal < 0 || !equal)
6582 return equal;
6585 return isl_bool_true;
6588 /* Do "pma1" and "pma2" represent the same function?
6590 * First check if they are obviously equal.
6591 * If not, then convert them to maps and check if those are equal.
6593 * If "pa1" or "pa2" contain any NaNs, then they are considered
6594 * not to be the same. A NaN is not equal to anything, not even
6595 * to another NaN.
6597 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6598 __isl_keep isl_pw_multi_aff *pma2)
6600 isl_bool equal;
6601 isl_bool has_nan;
6602 isl_map *map1, *map2;
6604 if (!pma1 || !pma2)
6605 return isl_bool_error;
6607 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6608 if (equal < 0 || equal)
6609 return equal;
6610 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6611 if (has_nan >= 0 && !has_nan)
6612 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6613 if (has_nan < 0 || has_nan)
6614 return isl_bool_not(has_nan);
6616 map1 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma1));
6617 map2 = isl_map_from_pw_multi_aff(isl_pw_multi_aff_copy(pma2));
6618 equal = isl_map_is_equal(map1, map2);
6619 isl_map_free(map1);
6620 isl_map_free(map2);
6622 return equal;
6625 /* Compute the pullback of "mpa" by the function represented by "ma".
6626 * In other words, plug in "ma" in "mpa".
6628 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6630 * If "mpa" has an explicit domain, then it is this domain
6631 * that needs to undergo a pullback, i.e., a preimage.
6633 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6634 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6636 int i;
6637 isl_space *space = NULL;
6639 mpa = isl_multi_pw_aff_cow(mpa);
6640 if (!mpa || !ma)
6641 goto error;
6643 space = isl_space_join(isl_multi_aff_get_space(ma),
6644 isl_multi_pw_aff_get_space(mpa));
6645 if (!space)
6646 goto error;
6648 for (i = 0; i < mpa->n; ++i) {
6649 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6650 isl_multi_aff_copy(ma));
6651 if (!mpa->u.p[i])
6652 goto error;
6654 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6655 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6656 isl_multi_aff_copy(ma));
6657 if (!mpa->u.dom)
6658 goto error;
6661 isl_multi_aff_free(ma);
6662 isl_space_free(mpa->space);
6663 mpa->space = space;
6664 return mpa;
6665 error:
6666 isl_space_free(space);
6667 isl_multi_pw_aff_free(mpa);
6668 isl_multi_aff_free(ma);
6669 return NULL;
6672 /* Compute the pullback of "mpa" by the function represented by "ma".
6673 * In other words, plug in "ma" in "mpa".
6675 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6676 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6678 isl_bool equal_params;
6680 if (!mpa || !ma)
6681 goto error;
6682 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6683 if (equal_params < 0)
6684 goto error;
6685 if (equal_params)
6686 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6687 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6688 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6689 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6690 error:
6691 isl_multi_pw_aff_free(mpa);
6692 isl_multi_aff_free(ma);
6693 return NULL;
6696 /* Compute the pullback of "mpa" by the function represented by "pma".
6697 * In other words, plug in "pma" in "mpa".
6699 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6701 * If "mpa" has an explicit domain, then it is this domain
6702 * that needs to undergo a pullback, i.e., a preimage.
6704 static __isl_give isl_multi_pw_aff *
6705 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6706 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6708 int i;
6709 isl_space *space = NULL;
6711 mpa = isl_multi_pw_aff_cow(mpa);
6712 if (!mpa || !pma)
6713 goto error;
6715 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6716 isl_multi_pw_aff_get_space(mpa));
6718 for (i = 0; i < mpa->n; ++i) {
6719 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6720 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6721 if (!mpa->u.p[i])
6722 goto error;
6724 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6725 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6726 isl_pw_multi_aff_copy(pma));
6727 if (!mpa->u.dom)
6728 goto error;
6731 isl_pw_multi_aff_free(pma);
6732 isl_space_free(mpa->space);
6733 mpa->space = space;
6734 return mpa;
6735 error:
6736 isl_space_free(space);
6737 isl_multi_pw_aff_free(mpa);
6738 isl_pw_multi_aff_free(pma);
6739 return NULL;
6742 /* Compute the pullback of "mpa" by the function represented by "pma".
6743 * In other words, plug in "pma" in "mpa".
6745 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6746 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6748 isl_bool equal_params;
6750 if (!mpa || !pma)
6751 goto error;
6752 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6753 if (equal_params < 0)
6754 goto error;
6755 if (equal_params)
6756 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6757 mpa = isl_multi_pw_aff_align_params(mpa,
6758 isl_pw_multi_aff_get_space(pma));
6759 pma = isl_pw_multi_aff_align_params(pma,
6760 isl_multi_pw_aff_get_space(mpa));
6761 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6762 error:
6763 isl_multi_pw_aff_free(mpa);
6764 isl_pw_multi_aff_free(pma);
6765 return NULL;
6768 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6769 * with the domain of "aff". The domain of the result is the same
6770 * as that of "mpa".
6771 * "mpa" and "aff" are assumed to have been aligned.
6773 * We first extract the parametric constant from "aff", defined
6774 * over the correct domain.
6775 * Then we add the appropriate combinations of the members of "mpa".
6776 * Finally, we add the integer divisions through recursive calls.
6778 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6779 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6781 int i;
6782 isl_size n_in, n_div, n_mpa_in;
6783 isl_space *space;
6784 isl_val *v;
6785 isl_pw_aff *pa;
6786 isl_aff *tmp;
6788 n_in = isl_aff_dim(aff, isl_dim_in);
6789 n_div = isl_aff_dim(aff, isl_dim_div);
6790 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
6791 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
6792 goto error;
6794 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6795 tmp = isl_aff_copy(aff);
6796 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6797 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6798 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
6799 tmp = isl_aff_reset_domain_space(tmp, space);
6800 pa = isl_pw_aff_from_aff(tmp);
6802 for (i = 0; i < n_in; ++i) {
6803 isl_pw_aff *pa_i;
6805 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6806 continue;
6807 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6808 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6809 pa_i = isl_pw_aff_scale_val(pa_i, v);
6810 pa = isl_pw_aff_add(pa, pa_i);
6813 for (i = 0; i < n_div; ++i) {
6814 isl_aff *div;
6815 isl_pw_aff *pa_i;
6817 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6818 continue;
6819 div = isl_aff_get_div(aff, i);
6820 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6821 isl_multi_pw_aff_copy(mpa), div);
6822 pa_i = isl_pw_aff_floor(pa_i);
6823 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6824 pa_i = isl_pw_aff_scale_val(pa_i, v);
6825 pa = isl_pw_aff_add(pa, pa_i);
6828 isl_multi_pw_aff_free(mpa);
6829 isl_aff_free(aff);
6831 return pa;
6832 error:
6833 isl_multi_pw_aff_free(mpa);
6834 isl_aff_free(aff);
6835 return NULL;
6838 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6839 * with the domain of "aff". The domain of the result is the same
6840 * as that of "mpa".
6842 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6843 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6845 isl_bool equal_params;
6847 if (!aff || !mpa)
6848 goto error;
6849 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6850 if (equal_params < 0)
6851 goto error;
6852 if (equal_params)
6853 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6855 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6856 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6858 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6859 error:
6860 isl_aff_free(aff);
6861 isl_multi_pw_aff_free(mpa);
6862 return NULL;
6865 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6866 * with the domain of "pa". The domain of the result is the same
6867 * as that of "mpa".
6868 * "mpa" and "pa" are assumed to have been aligned.
6870 * We consider each piece in turn. Note that the domains of the
6871 * pieces are assumed to be disjoint and they remain disjoint
6872 * after taking the preimage (over the same function).
6874 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6875 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6877 isl_space *space;
6878 isl_pw_aff *res;
6879 int i;
6881 if (!mpa || !pa)
6882 goto error;
6884 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6885 isl_pw_aff_get_space(pa));
6886 res = isl_pw_aff_empty(space);
6888 for (i = 0; i < pa->n; ++i) {
6889 isl_pw_aff *pa_i;
6890 isl_set *domain;
6892 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6893 isl_multi_pw_aff_copy(mpa),
6894 isl_aff_copy(pa->p[i].aff));
6895 domain = isl_set_copy(pa->p[i].set);
6896 domain = isl_set_preimage_multi_pw_aff(domain,
6897 isl_multi_pw_aff_copy(mpa));
6898 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6899 res = isl_pw_aff_add_disjoint(res, pa_i);
6902 isl_pw_aff_free(pa);
6903 isl_multi_pw_aff_free(mpa);
6904 return res;
6905 error:
6906 isl_pw_aff_free(pa);
6907 isl_multi_pw_aff_free(mpa);
6908 return NULL;
6911 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6912 * with the domain of "pa". The domain of the result is the same
6913 * as that of "mpa".
6915 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6916 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6918 isl_bool equal_params;
6920 if (!pa || !mpa)
6921 goto error;
6922 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6923 if (equal_params < 0)
6924 goto error;
6925 if (equal_params)
6926 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6928 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
6929 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
6931 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6932 error:
6933 isl_pw_aff_free(pa);
6934 isl_multi_pw_aff_free(mpa);
6935 return NULL;
6938 /* Compute the pullback of "pa" by the function represented by "mpa".
6939 * In other words, plug in "mpa" in "pa".
6940 * "pa" and "mpa" are assumed to have been aligned.
6942 * The pullback is computed by applying "pa" to "mpa".
6944 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
6945 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6947 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
6950 /* Compute the pullback of "pa" by the function represented by "mpa".
6951 * In other words, plug in "mpa" in "pa".
6953 * The pullback is computed by applying "pa" to "mpa".
6955 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
6956 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
6958 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
6961 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
6962 * In other words, plug in "mpa2" in "mpa1".
6964 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
6966 * We pullback each member of "mpa1" in turn.
6968 * If "mpa1" has an explicit domain, then it is this domain
6969 * that needs to undergo a pullback instead, i.e., a preimage.
6971 static __isl_give isl_multi_pw_aff *
6972 isl_multi_pw_aff_pullback_multi_pw_aff_aligned(
6973 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
6975 int i;
6976 isl_space *space = NULL;
6978 mpa1 = isl_multi_pw_aff_cow(mpa1);
6979 if (!mpa1 || !mpa2)
6980 goto error;
6982 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
6983 isl_multi_pw_aff_get_space(mpa1));
6985 for (i = 0; i < mpa1->n; ++i) {
6986 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
6987 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
6988 if (!mpa1->u.p[i])
6989 goto error;
6992 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
6993 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
6994 isl_multi_pw_aff_copy(mpa2));
6995 if (!mpa1->u.dom)
6996 goto error;
6998 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7000 isl_multi_pw_aff_free(mpa2);
7001 return mpa1;
7002 error:
7003 isl_space_free(space);
7004 isl_multi_pw_aff_free(mpa1);
7005 isl_multi_pw_aff_free(mpa2);
7006 return NULL;
7009 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7010 * In other words, plug in "mpa2" in "mpa1".
7012 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7013 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7015 return isl_multi_pw_aff_align_params_multi_multi_and(mpa1, mpa2,
7016 &isl_multi_pw_aff_pullback_multi_pw_aff_aligned);
7019 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7020 * of "mpa1" and "mpa2" live in the same space, construct map space
7021 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7022 * with this map space as extract argument.
7024 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7025 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7026 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7027 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7029 int match;
7030 isl_space *space1, *space2;
7031 isl_map *res;
7033 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7034 isl_multi_pw_aff_get_space(mpa2));
7035 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7036 isl_multi_pw_aff_get_space(mpa1));
7037 if (!mpa1 || !mpa2)
7038 goto error;
7039 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7040 mpa2->space, isl_dim_out);
7041 if (match < 0)
7042 goto error;
7043 if (!match)
7044 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7045 "range spaces don't match", goto error);
7046 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7047 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7048 space1 = isl_space_map_from_domain_and_range(space1, space2);
7050 res = order(mpa1, mpa2, space1);
7051 isl_multi_pw_aff_free(mpa1);
7052 isl_multi_pw_aff_free(mpa2);
7053 return res;
7054 error:
7055 isl_multi_pw_aff_free(mpa1);
7056 isl_multi_pw_aff_free(mpa2);
7057 return NULL;
7060 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7061 * where the function values are equal. "space" is the space of the result.
7062 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7064 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7065 * in the sequences are equal.
7067 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7068 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7069 __isl_take isl_space *space)
7071 int i;
7072 isl_size n;
7073 isl_map *res;
7075 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7076 if (n < 0)
7077 space = isl_space_free(space);
7078 res = isl_map_universe(space);
7080 for (i = 0; i < n; ++i) {
7081 isl_pw_aff *pa1, *pa2;
7082 isl_map *map;
7084 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7085 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7086 map = isl_pw_aff_eq_map(pa1, pa2);
7087 res = isl_map_intersect(res, map);
7090 return res;
7093 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7094 * where the function values are equal.
7096 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7097 __isl_take isl_multi_pw_aff *mpa2)
7099 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7100 &isl_multi_pw_aff_eq_map_on_space);
7103 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7104 * where the function values of "mpa1" is lexicographically satisfies "base"
7105 * compared to that of "mpa2". "space" is the space of the result.
7106 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7108 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7109 * if its i-th element satisfies "base" when compared to
7110 * the i-th element of "mpa2" while all previous elements are
7111 * pairwise equal.
7113 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7114 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7115 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7116 __isl_take isl_pw_aff *pa2),
7117 __isl_take isl_space *space)
7119 int i;
7120 isl_size n;
7121 isl_map *res, *rest;
7123 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7124 if (n < 0)
7125 space = isl_space_free(space);
7126 res = isl_map_empty(isl_space_copy(space));
7127 rest = isl_map_universe(space);
7129 for (i = 0; i < n; ++i) {
7130 isl_pw_aff *pa1, *pa2;
7131 isl_map *map;
7133 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7134 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7135 map = base(pa1, pa2);
7136 map = isl_map_intersect(map, isl_map_copy(rest));
7137 res = isl_map_union(res, map);
7139 if (i == n - 1)
7140 continue;
7142 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7143 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7144 map = isl_pw_aff_eq_map(pa1, pa2);
7145 rest = isl_map_intersect(rest, map);
7148 isl_map_free(rest);
7149 return res;
7152 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7153 * where the function value of "mpa1" is lexicographically less than that
7154 * of "mpa2". "space" is the space of the result.
7155 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7157 * "mpa1" is less than "mpa2" if its i-th element is smaller
7158 * than the i-th element of "mpa2" while all previous elements are
7159 * pairwise equal.
7161 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map_on_space(
7162 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7163 __isl_take isl_space *space)
7165 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7166 &isl_pw_aff_lt_map, space);
7169 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7170 * where the function value of "mpa1" is lexicographically less than that
7171 * of "mpa2".
7173 __isl_give isl_map *isl_multi_pw_aff_lex_lt_map(
7174 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7176 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7177 &isl_multi_pw_aff_lex_lt_map_on_space);
7180 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7181 * where the function value of "mpa1" is lexicographically greater than that
7182 * of "mpa2". "space" is the space of the result.
7183 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7185 * "mpa1" is greater than "mpa2" if its i-th element is greater
7186 * than the i-th element of "mpa2" while all previous elements are
7187 * pairwise equal.
7189 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map_on_space(
7190 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7191 __isl_take isl_space *space)
7193 return isl_multi_pw_aff_lex_map_on_space(mpa1, mpa2,
7194 &isl_pw_aff_gt_map, space);
7197 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7198 * where the function value of "mpa1" is lexicographically greater than that
7199 * of "mpa2".
7201 __isl_give isl_map *isl_multi_pw_aff_lex_gt_map(
7202 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7204 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7205 &isl_multi_pw_aff_lex_gt_map_on_space);
7208 /* Compare two isl_affs.
7210 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7211 * than "aff2" and 0 if they are equal.
7213 * The order is fairly arbitrary. We do consider expressions that only involve
7214 * earlier dimensions as "smaller".
7216 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7218 int cmp;
7219 int last1, last2;
7221 if (aff1 == aff2)
7222 return 0;
7224 if (!aff1)
7225 return -1;
7226 if (!aff2)
7227 return 1;
7229 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7230 if (cmp != 0)
7231 return cmp;
7233 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7234 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7235 if (last1 != last2)
7236 return last1 - last2;
7238 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7241 /* Compare two isl_pw_affs.
7243 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7244 * than "pa2" and 0 if they are equal.
7246 * The order is fairly arbitrary. We do consider expressions that only involve
7247 * earlier dimensions as "smaller".
7249 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7250 __isl_keep isl_pw_aff *pa2)
7252 int i;
7253 int cmp;
7255 if (pa1 == pa2)
7256 return 0;
7258 if (!pa1)
7259 return -1;
7260 if (!pa2)
7261 return 1;
7263 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7264 if (cmp != 0)
7265 return cmp;
7267 if (pa1->n != pa2->n)
7268 return pa1->n - pa2->n;
7270 for (i = 0; i < pa1->n; ++i) {
7271 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7272 if (cmp != 0)
7273 return cmp;
7274 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7275 if (cmp != 0)
7276 return cmp;
7279 return 0;
7282 /* Return a piecewise affine expression that is equal to "v" on "domain".
7284 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7285 __isl_take isl_val *v)
7287 isl_space *space;
7288 isl_local_space *ls;
7289 isl_aff *aff;
7291 space = isl_set_get_space(domain);
7292 ls = isl_local_space_from_space(space);
7293 aff = isl_aff_val_on_domain(ls, v);
7295 return isl_pw_aff_alloc(domain, aff);
7298 /* Return a multi affine expression that is equal to "mv" on domain
7299 * space "space".
7301 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7302 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7304 int i;
7305 isl_size n;
7306 isl_space *space2;
7307 isl_local_space *ls;
7308 isl_multi_aff *ma;
7310 n = isl_multi_val_dim(mv, isl_dim_set);
7311 if (!space || n < 0)
7312 goto error;
7314 space2 = isl_multi_val_get_space(mv);
7315 space2 = isl_space_align_params(space2, isl_space_copy(space));
7316 space = isl_space_align_params(space, isl_space_copy(space2));
7317 space = isl_space_map_from_domain_and_range(space, space2);
7318 ma = isl_multi_aff_alloc(isl_space_copy(space));
7319 ls = isl_local_space_from_space(isl_space_domain(space));
7320 for (i = 0; i < n; ++i) {
7321 isl_val *v;
7322 isl_aff *aff;
7324 v = isl_multi_val_get_val(mv, i);
7325 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7326 ma = isl_multi_aff_set_aff(ma, i, aff);
7328 isl_local_space_free(ls);
7330 isl_multi_val_free(mv);
7331 return ma;
7332 error:
7333 isl_space_free(space);
7334 isl_multi_val_free(mv);
7335 return NULL;
7338 /* Return a piecewise multi-affine expression
7339 * that is equal to "mv" on "domain".
7341 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7342 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7344 isl_space *space;
7345 isl_multi_aff *ma;
7347 space = isl_set_get_space(domain);
7348 ma = isl_multi_aff_multi_val_on_space(space, mv);
7350 return isl_pw_multi_aff_alloc(domain, ma);
7353 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7354 * mv is the value that should be attained on each domain set
7355 * res collects the results
7357 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7358 isl_multi_val *mv;
7359 isl_union_pw_multi_aff *res;
7362 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7363 * and add it to data->res.
7365 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7366 void *user)
7368 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7369 isl_pw_multi_aff *pma;
7370 isl_multi_val *mv;
7372 mv = isl_multi_val_copy(data->mv);
7373 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7374 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7376 return data->res ? isl_stat_ok : isl_stat_error;
7379 /* Return a union piecewise multi-affine expression
7380 * that is equal to "mv" on "domain".
7382 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7383 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7385 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7386 isl_space *space;
7388 space = isl_union_set_get_space(domain);
7389 data.res = isl_union_pw_multi_aff_empty(space);
7390 data.mv = mv;
7391 if (isl_union_set_foreach_set(domain,
7392 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7393 data.res = isl_union_pw_multi_aff_free(data.res);
7394 isl_union_set_free(domain);
7395 isl_multi_val_free(mv);
7396 return data.res;
7399 /* Compute the pullback of data->pma by the function represented by "pma2",
7400 * provided the spaces match, and add the results to data->res.
7402 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7404 struct isl_union_pw_multi_aff_bin_data *data = user;
7406 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7407 pma2->dim, isl_dim_out)) {
7408 isl_pw_multi_aff_free(pma2);
7409 return isl_stat_ok;
7412 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7413 isl_pw_multi_aff_copy(data->pma), pma2);
7415 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7416 if (!data->res)
7417 return isl_stat_error;
7419 return isl_stat_ok;
7422 /* Compute the pullback of "upma1" by the function represented by "upma2".
7424 __isl_give isl_union_pw_multi_aff *
7425 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7426 __isl_take isl_union_pw_multi_aff *upma1,
7427 __isl_take isl_union_pw_multi_aff *upma2)
7429 return bin_op(upma1, upma2, &pullback_entry);
7432 /* Check that the domain space of "upa" matches "space".
7434 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7435 * can in principle never fail since the space "space" is that
7436 * of the isl_multi_union_pw_aff and is a set space such that
7437 * there is no domain space to match.
7439 * We check the parameters and double-check that "space" is
7440 * indeed that of a set.
7442 static isl_stat isl_union_pw_aff_check_match_domain_space(
7443 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7445 isl_space *upa_space;
7446 isl_bool match;
7448 if (!upa || !space)
7449 return isl_stat_error;
7451 match = isl_space_is_set(space);
7452 if (match < 0)
7453 return isl_stat_error;
7454 if (!match)
7455 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7456 "expecting set space", return isl_stat_error);
7458 upa_space = isl_union_pw_aff_get_space(upa);
7459 match = isl_space_has_equal_params(space, upa_space);
7460 if (match < 0)
7461 goto error;
7462 if (!match)
7463 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7464 "parameters don't match", goto error);
7466 isl_space_free(upa_space);
7467 return isl_stat_ok;
7468 error:
7469 isl_space_free(upa_space);
7470 return isl_stat_error;
7473 /* Do the parameters of "upa" match those of "space"?
7475 static isl_bool isl_union_pw_aff_matching_params(
7476 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7478 isl_space *upa_space;
7479 isl_bool match;
7481 if (!upa || !space)
7482 return isl_bool_error;
7484 upa_space = isl_union_pw_aff_get_space(upa);
7486 match = isl_space_has_equal_params(space, upa_space);
7488 isl_space_free(upa_space);
7489 return match;
7492 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7493 * space represents the new parameters.
7494 * res collects the results.
7496 struct isl_union_pw_aff_reset_params_data {
7497 isl_space *space;
7498 isl_union_pw_aff *res;
7501 /* Replace the parameters of "pa" by data->space and
7502 * add the result to data->res.
7504 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7506 struct isl_union_pw_aff_reset_params_data *data = user;
7507 isl_space *space;
7509 space = isl_pw_aff_get_space(pa);
7510 space = isl_space_replace_params(space, data->space);
7511 pa = isl_pw_aff_reset_space(pa, space);
7512 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7514 return data->res ? isl_stat_ok : isl_stat_error;
7517 /* Replace the domain space of "upa" by "space".
7518 * Since a union expression does not have a (single) domain space,
7519 * "space" is necessarily a parameter space.
7521 * Since the order and the names of the parameters determine
7522 * the hash value, we need to create a new hash table.
7524 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7525 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7527 struct isl_union_pw_aff_reset_params_data data = { space };
7528 isl_bool match;
7530 match = isl_union_pw_aff_matching_params(upa, space);
7531 if (match < 0)
7532 upa = isl_union_pw_aff_free(upa);
7533 else if (match) {
7534 isl_space_free(space);
7535 return upa;
7538 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7539 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7540 data.res = isl_union_pw_aff_free(data.res);
7542 isl_union_pw_aff_free(upa);
7543 isl_space_free(space);
7544 return data.res;
7547 /* Return the floor of "pa".
7549 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7551 return isl_pw_aff_floor(pa);
7554 /* Given f, return floor(f).
7556 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7557 __isl_take isl_union_pw_aff *upa)
7559 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7562 /* Compute
7564 * upa mod m = upa - m * floor(upa/m)
7566 * with m an integer value.
7568 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7569 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7571 isl_union_pw_aff *res;
7573 if (!upa || !m)
7574 goto error;
7576 if (!isl_val_is_int(m))
7577 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7578 "expecting integer modulo", goto error);
7579 if (!isl_val_is_pos(m))
7580 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7581 "expecting positive modulo", goto error);
7583 res = isl_union_pw_aff_copy(upa);
7584 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7585 upa = isl_union_pw_aff_floor(upa);
7586 upa = isl_union_pw_aff_scale_val(upa, m);
7587 res = isl_union_pw_aff_sub(res, upa);
7589 return res;
7590 error:
7591 isl_val_free(m);
7592 isl_union_pw_aff_free(upa);
7593 return NULL;
7596 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7597 * pos is the output position that needs to be extracted.
7598 * res collects the results.
7600 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7601 int pos;
7602 isl_union_pw_aff *res;
7605 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7606 * (assuming it has such a dimension) and add it to data->res.
7608 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7610 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7611 isl_size n_out;
7612 isl_pw_aff *pa;
7614 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7615 if (n_out < 0)
7616 return isl_stat_error;
7617 if (data->pos >= n_out) {
7618 isl_pw_multi_aff_free(pma);
7619 return isl_stat_ok;
7622 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7623 isl_pw_multi_aff_free(pma);
7625 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7627 return data->res ? isl_stat_ok : isl_stat_error;
7630 /* Extract an isl_union_pw_aff corresponding to
7631 * output dimension "pos" of "upma".
7633 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7634 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7636 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7637 isl_space *space;
7639 if (!upma)
7640 return NULL;
7642 if (pos < 0)
7643 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7644 "cannot extract at negative position", return NULL);
7646 space = isl_union_pw_multi_aff_get_space(upma);
7647 data.res = isl_union_pw_aff_empty(space);
7648 data.pos = pos;
7649 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7650 &get_union_pw_aff, &data) < 0)
7651 data.res = isl_union_pw_aff_free(data.res);
7653 return data.res;
7656 /* Return a union piecewise affine expression
7657 * that is equal to "aff" on "domain".
7659 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7660 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7662 isl_pw_aff *pa;
7664 pa = isl_pw_aff_from_aff(aff);
7665 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7668 /* Return a union piecewise affine expression
7669 * that is equal to the parameter identified by "id" on "domain".
7671 * Make sure the parameter appears in the space passed to
7672 * isl_aff_param_on_domain_space_id.
7674 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7675 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7677 isl_space *space;
7678 isl_aff *aff;
7680 space = isl_union_set_get_space(domain);
7681 space = isl_space_add_param_id(space, isl_id_copy(id));
7682 aff = isl_aff_param_on_domain_space_id(space, id);
7683 return isl_union_pw_aff_aff_on_domain(domain, aff);
7686 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7687 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7688 * needs to attain.
7689 * "res" collects the results.
7691 struct isl_union_pw_aff_pw_aff_on_domain_data {
7692 isl_pw_aff *pa;
7693 isl_union_pw_aff *res;
7696 /* Construct a piecewise affine expression that is equal to data->pa
7697 * on "domain" and add the result to data->res.
7699 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7701 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7702 isl_pw_aff *pa;
7703 isl_size dim;
7705 pa = isl_pw_aff_copy(data->pa);
7706 dim = isl_set_dim(domain, isl_dim_set);
7707 if (dim < 0)
7708 pa = isl_pw_aff_free(pa);
7709 pa = isl_pw_aff_from_range(pa);
7710 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7711 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7712 pa = isl_pw_aff_intersect_domain(pa, domain);
7713 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7715 return data->res ? isl_stat_ok : isl_stat_error;
7718 /* Return a union piecewise affine expression
7719 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7720 * have been aligned.
7722 * Construct an isl_pw_aff on each of the sets in "domain" and
7723 * collect the results.
7725 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7726 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7728 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7729 isl_space *space;
7731 space = isl_union_set_get_space(domain);
7732 data.res = isl_union_pw_aff_empty(space);
7733 data.pa = pa;
7734 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7735 data.res = isl_union_pw_aff_free(data.res);
7736 isl_union_set_free(domain);
7737 isl_pw_aff_free(pa);
7738 return data.res;
7741 /* Return a union piecewise affine expression
7742 * that is equal to "pa" on "domain".
7744 * Check that "pa" is a parametric expression,
7745 * align the parameters if needed and call
7746 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7748 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7749 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7751 isl_bool is_set;
7752 isl_bool equal_params;
7753 isl_space *domain_space, *pa_space;
7755 pa_space = isl_pw_aff_peek_space(pa);
7756 is_set = isl_space_is_set(pa_space);
7757 if (is_set < 0)
7758 goto error;
7759 if (!is_set)
7760 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7761 "expecting parametric expression", goto error);
7763 domain_space = isl_union_set_get_space(domain);
7764 pa_space = isl_pw_aff_get_space(pa);
7765 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7766 if (equal_params >= 0 && !equal_params) {
7767 isl_space *space;
7769 space = isl_space_align_params(domain_space, pa_space);
7770 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7771 domain = isl_union_set_align_params(domain, space);
7772 } else {
7773 isl_space_free(domain_space);
7774 isl_space_free(pa_space);
7777 if (equal_params < 0)
7778 goto error;
7779 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7780 error:
7781 isl_union_set_free(domain);
7782 isl_pw_aff_free(pa);
7783 return NULL;
7786 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7787 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7788 * "res" collects the results.
7790 struct isl_union_pw_aff_val_on_domain_data {
7791 isl_val *v;
7792 isl_union_pw_aff *res;
7795 /* Construct a piecewise affine expression that is equal to data->v
7796 * on "domain" and add the result to data->res.
7798 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7800 struct isl_union_pw_aff_val_on_domain_data *data = user;
7801 isl_pw_aff *pa;
7802 isl_val *v;
7804 v = isl_val_copy(data->v);
7805 pa = isl_pw_aff_val_on_domain(domain, v);
7806 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7808 return data->res ? isl_stat_ok : isl_stat_error;
7811 /* Return a union piecewise affine expression
7812 * that is equal to "v" on "domain".
7814 * Construct an isl_pw_aff on each of the sets in "domain" and
7815 * collect the results.
7817 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7818 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7820 struct isl_union_pw_aff_val_on_domain_data data;
7821 isl_space *space;
7823 space = isl_union_set_get_space(domain);
7824 data.res = isl_union_pw_aff_empty(space);
7825 data.v = v;
7826 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7827 data.res = isl_union_pw_aff_free(data.res);
7828 isl_union_set_free(domain);
7829 isl_val_free(v);
7830 return data.res;
7833 /* Construct a piecewise multi affine expression
7834 * that is equal to "pa" and add it to upma.
7836 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7837 void *user)
7839 isl_union_pw_multi_aff **upma = user;
7840 isl_pw_multi_aff *pma;
7842 pma = isl_pw_multi_aff_from_pw_aff(pa);
7843 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7845 return *upma ? isl_stat_ok : isl_stat_error;
7848 /* Construct and return a union piecewise multi affine expression
7849 * that is equal to the given union piecewise affine expression.
7851 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7852 __isl_take isl_union_pw_aff *upa)
7854 isl_space *space;
7855 isl_union_pw_multi_aff *upma;
7857 if (!upa)
7858 return NULL;
7860 space = isl_union_pw_aff_get_space(upa);
7861 upma = isl_union_pw_multi_aff_empty(space);
7863 if (isl_union_pw_aff_foreach_pw_aff(upa,
7864 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7865 upma = isl_union_pw_multi_aff_free(upma);
7867 isl_union_pw_aff_free(upa);
7868 return upma;
7871 /* Compute the set of elements in the domain of "pa" where it is zero and
7872 * add this set to "uset".
7874 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7876 isl_union_set **uset = (isl_union_set **)user;
7878 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7880 return *uset ? isl_stat_ok : isl_stat_error;
7883 /* Return a union set containing those elements in the domain
7884 * of "upa" where it is zero.
7886 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7887 __isl_take isl_union_pw_aff *upa)
7889 isl_union_set *zero;
7891 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7892 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7893 zero = isl_union_set_free(zero);
7895 isl_union_pw_aff_free(upa);
7896 return zero;
7899 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
7900 * upma is the function that is plugged in.
7901 * pa is the current part of the function in which upma is plugged in.
7902 * res collects the results.
7904 struct isl_union_pw_aff_pullback_upma_data {
7905 isl_union_pw_multi_aff *upma;
7906 isl_pw_aff *pa;
7907 isl_union_pw_aff *res;
7910 /* Check if "pma" can be plugged into data->pa.
7911 * If so, perform the pullback and add the result to data->res.
7913 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
7915 struct isl_union_pw_aff_pullback_upma_data *data = user;
7916 isl_pw_aff *pa;
7918 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
7919 pma->dim, isl_dim_out)) {
7920 isl_pw_multi_aff_free(pma);
7921 return isl_stat_ok;
7924 pa = isl_pw_aff_copy(data->pa);
7925 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
7927 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7929 return data->res ? isl_stat_ok : isl_stat_error;
7932 /* Check if any of the elements of data->upma can be plugged into pa,
7933 * add if so add the result to data->res.
7935 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
7937 struct isl_union_pw_aff_pullback_upma_data *data = user;
7938 isl_stat r;
7940 data->pa = pa;
7941 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
7942 &pa_pb_pma, data);
7943 isl_pw_aff_free(pa);
7945 return r;
7948 /* Compute the pullback of "upa" by the function represented by "upma".
7949 * In other words, plug in "upma" in "upa". The result contains
7950 * expressions defined over the domain space of "upma".
7952 * Run over all pairs of elements in "upa" and "upma", perform
7953 * the pullback when appropriate and collect the results.
7954 * If the hash value were based on the domain space rather than
7955 * the function space, then we could run through all elements
7956 * of "upma" and directly pick out the corresponding element of "upa".
7958 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
7959 __isl_take isl_union_pw_aff *upa,
7960 __isl_take isl_union_pw_multi_aff *upma)
7962 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
7963 isl_space *space;
7965 space = isl_union_pw_multi_aff_get_space(upma);
7966 upa = isl_union_pw_aff_align_params(upa, space);
7967 space = isl_union_pw_aff_get_space(upa);
7968 upma = isl_union_pw_multi_aff_align_params(upma, space);
7970 if (!upa || !upma)
7971 goto error;
7973 data.upma = upma;
7974 data.res = isl_union_pw_aff_alloc_same_size(upa);
7975 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
7976 data.res = isl_union_pw_aff_free(data.res);
7978 isl_union_pw_aff_free(upa);
7979 isl_union_pw_multi_aff_free(upma);
7980 return data.res;
7981 error:
7982 isl_union_pw_aff_free(upa);
7983 isl_union_pw_multi_aff_free(upma);
7984 return NULL;
7987 #undef BASE
7988 #define BASE union_pw_aff
7989 #undef DOMBASE
7990 #define DOMBASE union_set
7992 #include <isl_multi_explicit_domain.c>
7993 #include <isl_multi_union_pw_aff_explicit_domain.c>
7994 #include <isl_multi_templ.c>
7995 #include <isl_multi_apply_set.c>
7996 #include <isl_multi_apply_union_set.c>
7997 #include <isl_multi_coalesce.c>
7998 #include <isl_multi_floor.c>
7999 #include <isl_multi_from_base_templ.c>
8000 #include <isl_multi_gist.c>
8001 #include <isl_multi_align_set.c>
8002 #include <isl_multi_align_union_set.c>
8003 #include <isl_multi_intersect.c>
8005 /* Does "mupa" have a non-trivial explicit domain?
8007 * The explicit domain, if present, is trivial if it represents
8008 * an (obviously) universe parameter set.
8010 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8011 __isl_keep isl_multi_union_pw_aff *mupa)
8013 isl_bool is_params, trivial;
8014 isl_set *set;
8016 if (!mupa)
8017 return isl_bool_error;
8018 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8019 return isl_bool_false;
8020 is_params = isl_union_set_is_params(mupa->u.dom);
8021 if (is_params < 0 || !is_params)
8022 return isl_bool_not(is_params);
8023 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8024 trivial = isl_set_plain_is_universe(set);
8025 isl_set_free(set);
8026 return isl_bool_not(trivial);
8029 /* Construct a multiple union piecewise affine expression
8030 * in the given space with value zero in each of the output dimensions.
8032 * Since there is no canonical zero value for
8033 * a union piecewise affine expression, we can only construct
8034 * a zero-dimensional "zero" value.
8036 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8037 __isl_take isl_space *space)
8039 isl_bool params;
8040 isl_size dim;
8042 if (!space)
8043 return NULL;
8045 params = isl_space_is_params(space);
8046 if (params < 0)
8047 goto error;
8048 if (params)
8049 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8050 "expecting proper set space", goto error);
8051 if (!isl_space_is_set(space))
8052 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8053 "expecting set space", goto error);
8054 dim = isl_space_dim(space, isl_dim_out);
8055 if (dim < 0)
8056 goto error;
8057 if (dim != 0)
8058 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8059 "expecting 0D space", goto error);
8061 return isl_multi_union_pw_aff_alloc(space);
8062 error:
8063 isl_space_free(space);
8064 return NULL;
8067 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8068 * with the actual sum on the shared domain and
8069 * the defined expression on the symmetric difference of the domains.
8071 * We simply iterate over the elements in both arguments and
8072 * call isl_union_pw_aff_union_add on each of them, if there is
8073 * at least one element.
8075 * Otherwise, the two expressions have an explicit domain and
8076 * the union of these explicit domains is computed.
8077 * This assumes that the explicit domains are either both in terms
8078 * of specific domains elements or both in terms of parameters.
8079 * However, if one of the expressions does not have any constraints
8080 * on its explicit domain, then this is allowed as well and the result
8081 * is the expression with no constraints on its explicit domain.
8083 static __isl_give isl_multi_union_pw_aff *
8084 isl_multi_union_pw_aff_union_add_aligned(
8085 __isl_take isl_multi_union_pw_aff *mupa1,
8086 __isl_take isl_multi_union_pw_aff *mupa2)
8088 isl_bool has_domain, is_params1, is_params2;
8090 if (isl_multi_union_pw_aff_check_equal_space(mupa1, mupa2) < 0)
8091 goto error;
8092 if (mupa1->n > 0)
8093 return isl_multi_union_pw_aff_bin_op(mupa1, mupa2,
8094 &isl_union_pw_aff_union_add);
8095 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa1) < 0 ||
8096 isl_multi_union_pw_aff_check_has_explicit_domain(mupa2) < 0)
8097 goto error;
8099 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa1);
8100 if (has_domain < 0)
8101 goto error;
8102 if (!has_domain) {
8103 isl_multi_union_pw_aff_free(mupa2);
8104 return mupa1;
8106 has_domain = isl_multi_union_pw_aff_has_non_trivial_domain(mupa2);
8107 if (has_domain < 0)
8108 goto error;
8109 if (!has_domain) {
8110 isl_multi_union_pw_aff_free(mupa1);
8111 return mupa2;
8114 is_params1 = isl_union_set_is_params(mupa1->u.dom);
8115 is_params2 = isl_union_set_is_params(mupa2->u.dom);
8116 if (is_params1 < 0 || is_params2 < 0)
8117 goto error;
8118 if (is_params1 != is_params2)
8119 isl_die(isl_multi_union_pw_aff_get_ctx(mupa1),
8120 isl_error_invalid,
8121 "cannot compute union of concrete domain and "
8122 "parameter constraints", goto error);
8123 mupa1 = isl_multi_union_pw_aff_cow(mupa1);
8124 if (!mupa1)
8125 goto error;
8126 mupa1->u.dom = isl_union_set_union(mupa1->u.dom,
8127 isl_union_set_copy(mupa2->u.dom));
8128 if (!mupa1->u.dom)
8129 goto error;
8130 isl_multi_union_pw_aff_free(mupa2);
8131 return mupa1;
8132 error:
8133 isl_multi_union_pw_aff_free(mupa1);
8134 isl_multi_union_pw_aff_free(mupa2);
8135 return NULL;
8138 /* Compute the sum of "mupa1" and "mupa2" on the union of their domains,
8139 * with the actual sum on the shared domain and
8140 * the defined expression on the symmetric difference of the domains.
8142 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_union_add(
8143 __isl_take isl_multi_union_pw_aff *mupa1,
8144 __isl_take isl_multi_union_pw_aff *mupa2)
8146 return isl_multi_union_pw_aff_align_params_multi_multi_and(mupa1, mupa2,
8147 &isl_multi_union_pw_aff_union_add_aligned);
8150 /* Construct and return a multi union piecewise affine expression
8151 * that is equal to the given multi affine expression.
8153 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8154 __isl_take isl_multi_aff *ma)
8156 isl_multi_pw_aff *mpa;
8158 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8159 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8162 /* Construct and return a multi union piecewise affine expression
8163 * that is equal to the given multi piecewise affine expression.
8165 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8166 __isl_take isl_multi_pw_aff *mpa)
8168 int i;
8169 isl_size n;
8170 isl_space *space;
8171 isl_multi_union_pw_aff *mupa;
8173 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8174 if (n < 0)
8175 mpa = isl_multi_pw_aff_free(mpa);
8176 if (!mpa)
8177 return NULL;
8179 space = isl_multi_pw_aff_get_space(mpa);
8180 space = isl_space_range(space);
8181 mupa = isl_multi_union_pw_aff_alloc(space);
8183 for (i = 0; i < n; ++i) {
8184 isl_pw_aff *pa;
8185 isl_union_pw_aff *upa;
8187 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8188 upa = isl_union_pw_aff_from_pw_aff(pa);
8189 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8192 isl_multi_pw_aff_free(mpa);
8194 return mupa;
8197 /* Extract the range space of "pma" and assign it to *space.
8198 * If *space has already been set (through a previous call to this function),
8199 * then check that the range space is the same.
8201 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8203 isl_space **space = user;
8204 isl_space *pma_space;
8205 isl_bool equal;
8207 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8208 isl_pw_multi_aff_free(pma);
8210 if (!pma_space)
8211 return isl_stat_error;
8212 if (!*space) {
8213 *space = pma_space;
8214 return isl_stat_ok;
8217 equal = isl_space_is_equal(pma_space, *space);
8218 isl_space_free(pma_space);
8220 if (equal < 0)
8221 return isl_stat_error;
8222 if (!equal)
8223 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8224 "range spaces not the same", return isl_stat_error);
8225 return isl_stat_ok;
8228 /* Construct and return a multi union piecewise affine expression
8229 * that is equal to the given union piecewise multi affine expression.
8231 * In order to be able to perform the conversion, the input
8232 * needs to be non-empty and may only involve a single range space.
8234 * If the resulting multi union piecewise affine expression has
8235 * an explicit domain, then assign it the domain of the input.
8236 * In other cases, the domain is stored in the individual elements.
8238 __isl_give isl_multi_union_pw_aff *
8239 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8240 __isl_take isl_union_pw_multi_aff *upma)
8242 isl_space *space = NULL;
8243 isl_multi_union_pw_aff *mupa;
8244 int i;
8245 isl_size n;
8247 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8248 if (n < 0)
8249 goto error;
8250 if (n == 0)
8251 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8252 "cannot extract range space from empty input",
8253 goto error);
8254 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8255 &space) < 0)
8256 goto error;
8258 if (!space)
8259 goto error;
8261 n = isl_space_dim(space, isl_dim_set);
8262 if (n < 0)
8263 space = isl_space_free(space);
8264 mupa = isl_multi_union_pw_aff_alloc(space);
8266 for (i = 0; i < n; ++i) {
8267 isl_union_pw_aff *upa;
8269 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8270 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8272 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8273 isl_union_set *dom;
8274 isl_union_pw_multi_aff *copy;
8276 copy = isl_union_pw_multi_aff_copy(upma);
8277 dom = isl_union_pw_multi_aff_domain(copy);
8278 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8281 isl_union_pw_multi_aff_free(upma);
8282 return mupa;
8283 error:
8284 isl_space_free(space);
8285 isl_union_pw_multi_aff_free(upma);
8286 return NULL;
8289 /* Try and create an isl_multi_union_pw_aff that is equivalent
8290 * to the given isl_union_map.
8291 * The isl_union_map is required to be single-valued in each space.
8292 * Moreover, it cannot be empty and all range spaces need to be the same.
8293 * Otherwise, an error is produced.
8295 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8296 __isl_take isl_union_map *umap)
8298 isl_union_pw_multi_aff *upma;
8300 upma = isl_union_pw_multi_aff_from_union_map(umap);
8301 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8304 /* Return a multiple union piecewise affine expression
8305 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8306 * have been aligned.
8308 * If the resulting multi union piecewise affine expression has
8309 * an explicit domain, then assign it the input domain.
8310 * In other cases, the domain is stored in the individual elements.
8312 static __isl_give isl_multi_union_pw_aff *
8313 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8314 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8316 int i;
8317 isl_size n;
8318 isl_space *space;
8319 isl_multi_union_pw_aff *mupa;
8321 n = isl_multi_val_dim(mv, isl_dim_set);
8322 if (!domain || n < 0)
8323 goto error;
8325 space = isl_multi_val_get_space(mv);
8326 mupa = isl_multi_union_pw_aff_alloc(space);
8327 for (i = 0; i < n; ++i) {
8328 isl_val *v;
8329 isl_union_pw_aff *upa;
8331 v = isl_multi_val_get_val(mv, i);
8332 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8334 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8336 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8337 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8338 isl_union_set_copy(domain));
8340 isl_union_set_free(domain);
8341 isl_multi_val_free(mv);
8342 return mupa;
8343 error:
8344 isl_union_set_free(domain);
8345 isl_multi_val_free(mv);
8346 return NULL;
8349 /* Return a multiple union piecewise affine expression
8350 * that is equal to "mv" on "domain".
8352 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8353 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8355 isl_bool equal_params;
8357 if (!domain || !mv)
8358 goto error;
8359 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8360 if (equal_params < 0)
8361 goto error;
8362 if (equal_params)
8363 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8364 domain, mv);
8365 domain = isl_union_set_align_params(domain,
8366 isl_multi_val_get_space(mv));
8367 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8368 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8369 error:
8370 isl_union_set_free(domain);
8371 isl_multi_val_free(mv);
8372 return NULL;
8375 /* Return a multiple union piecewise affine expression
8376 * that is equal to "ma" on "domain".
8378 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8379 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8381 isl_pw_multi_aff *pma;
8383 pma = isl_pw_multi_aff_from_multi_aff(ma);
8384 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8387 /* Return a multiple union piecewise affine expression
8388 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8389 * have been aligned.
8391 * If the resulting multi union piecewise affine expression has
8392 * an explicit domain, then assign it the input domain.
8393 * In other cases, the domain is stored in the individual elements.
8395 static __isl_give isl_multi_union_pw_aff *
8396 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8397 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8399 int i;
8400 isl_size n;
8401 isl_space *space;
8402 isl_multi_union_pw_aff *mupa;
8404 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8405 if (!domain || n < 0)
8406 goto error;
8407 space = isl_pw_multi_aff_get_space(pma);
8408 mupa = isl_multi_union_pw_aff_alloc(space);
8409 for (i = 0; i < n; ++i) {
8410 isl_pw_aff *pa;
8411 isl_union_pw_aff *upa;
8413 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8414 upa = isl_union_pw_aff_pw_aff_on_domain(
8415 isl_union_set_copy(domain), pa);
8416 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8418 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8419 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8420 isl_union_set_copy(domain));
8422 isl_union_set_free(domain);
8423 isl_pw_multi_aff_free(pma);
8424 return mupa;
8425 error:
8426 isl_union_set_free(domain);
8427 isl_pw_multi_aff_free(pma);
8428 return NULL;
8431 /* Return a multiple union piecewise affine expression
8432 * that is equal to "pma" on "domain".
8434 __isl_give isl_multi_union_pw_aff *
8435 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8436 __isl_take isl_pw_multi_aff *pma)
8438 isl_bool equal_params;
8439 isl_space *space;
8441 space = isl_pw_multi_aff_peek_space(pma);
8442 equal_params = isl_union_set_space_has_equal_params(domain, space);
8443 if (equal_params < 0)
8444 goto error;
8445 if (equal_params)
8446 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8447 domain, pma);
8448 domain = isl_union_set_align_params(domain,
8449 isl_pw_multi_aff_get_space(pma));
8450 pma = isl_pw_multi_aff_align_params(pma,
8451 isl_union_set_get_space(domain));
8452 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8453 pma);
8454 error:
8455 isl_union_set_free(domain);
8456 isl_pw_multi_aff_free(pma);
8457 return NULL;
8460 /* Return a union set containing those elements in the domains
8461 * of the elements of "mupa" where they are all zero.
8463 * If there are no elements, then simply return the entire domain.
8465 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8466 __isl_take isl_multi_union_pw_aff *mupa)
8468 int i;
8469 isl_size n;
8470 isl_union_pw_aff *upa;
8471 isl_union_set *zero;
8473 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8474 if (n < 0)
8475 mupa = isl_multi_union_pw_aff_free(mupa);
8476 if (!mupa)
8477 return NULL;
8479 if (n == 0)
8480 return isl_multi_union_pw_aff_domain(mupa);
8482 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8483 zero = isl_union_pw_aff_zero_union_set(upa);
8485 for (i = 1; i < n; ++i) {
8486 isl_union_set *zero_i;
8488 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8489 zero_i = isl_union_pw_aff_zero_union_set(upa);
8491 zero = isl_union_set_intersect(zero, zero_i);
8494 isl_multi_union_pw_aff_free(mupa);
8495 return zero;
8498 /* Construct a union map mapping the shared domain
8499 * of the union piecewise affine expressions to the range of "mupa"
8500 * in the special case of a 0D multi union piecewise affine expression.
8502 * Construct a map between the explicit domain of "mupa" and
8503 * the range space.
8504 * Note that this assumes that the domain consists of explicit elements.
8506 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8507 __isl_take isl_multi_union_pw_aff *mupa)
8509 isl_bool is_params;
8510 isl_space *space;
8511 isl_union_set *dom, *ran;
8513 space = isl_multi_union_pw_aff_get_space(mupa);
8514 dom = isl_multi_union_pw_aff_domain(mupa);
8515 ran = isl_union_set_from_set(isl_set_universe(space));
8517 is_params = isl_union_set_is_params(dom);
8518 if (is_params < 0)
8519 dom = isl_union_set_free(dom);
8520 else if (is_params)
8521 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8522 "cannot create union map from expression without "
8523 "explicit domain elements",
8524 dom = isl_union_set_free(dom));
8526 return isl_union_map_from_domain_and_range(dom, ran);
8529 /* Construct a union map mapping the shared domain
8530 * of the union piecewise affine expressions to the range of "mupa"
8531 * with each dimension in the range equated to the
8532 * corresponding union piecewise affine expression.
8534 * If the input is zero-dimensional, then construct a mapping
8535 * from its explicit domain.
8537 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8538 __isl_take isl_multi_union_pw_aff *mupa)
8540 int i;
8541 isl_size n;
8542 isl_space *space;
8543 isl_union_map *umap;
8544 isl_union_pw_aff *upa;
8546 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8547 if (n < 0)
8548 mupa = isl_multi_union_pw_aff_free(mupa);
8549 if (!mupa)
8550 return NULL;
8552 if (n == 0)
8553 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8555 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8556 umap = isl_union_map_from_union_pw_aff(upa);
8558 for (i = 1; i < n; ++i) {
8559 isl_union_map *umap_i;
8561 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8562 umap_i = isl_union_map_from_union_pw_aff(upa);
8563 umap = isl_union_map_flat_range_product(umap, umap_i);
8566 space = isl_multi_union_pw_aff_get_space(mupa);
8567 umap = isl_union_map_reset_range_space(umap, space);
8569 isl_multi_union_pw_aff_free(mupa);
8570 return umap;
8573 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8574 * "range" is the space from which to set the range space.
8575 * "res" collects the results.
8577 struct isl_union_pw_multi_aff_reset_range_space_data {
8578 isl_space *range;
8579 isl_union_pw_multi_aff *res;
8582 /* Replace the range space of "pma" by the range space of data->range and
8583 * add the result to data->res.
8585 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8587 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8588 isl_space *space;
8590 space = isl_pw_multi_aff_get_space(pma);
8591 space = isl_space_domain(space);
8592 space = isl_space_extend_domain_with_range(space,
8593 isl_space_copy(data->range));
8594 pma = isl_pw_multi_aff_reset_space(pma, space);
8595 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8597 return data->res ? isl_stat_ok : isl_stat_error;
8600 /* Replace the range space of all the piecewise affine expressions in "upma" by
8601 * the range space of "space".
8603 * This assumes that all these expressions have the same output dimension.
8605 * Since the spaces of the expressions change, so do their hash values.
8606 * We therefore need to create a new isl_union_pw_multi_aff.
8607 * Note that the hash value is currently computed based on the entire
8608 * space even though there can only be a single expression with a given
8609 * domain space.
8611 static __isl_give isl_union_pw_multi_aff *
8612 isl_union_pw_multi_aff_reset_range_space(
8613 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8615 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8616 isl_space *space_upma;
8618 space_upma = isl_union_pw_multi_aff_get_space(upma);
8619 data.res = isl_union_pw_multi_aff_empty(space_upma);
8620 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8621 &reset_range_space, &data) < 0)
8622 data.res = isl_union_pw_multi_aff_free(data.res);
8624 isl_space_free(space);
8625 isl_union_pw_multi_aff_free(upma);
8626 return data.res;
8629 /* Construct and return a union piecewise multi affine expression
8630 * that is equal to the given multi union piecewise affine expression,
8631 * in the special case of a 0D multi union piecewise affine expression.
8633 * Construct a union piecewise multi affine expression
8634 * on top of the explicit domain of the input.
8636 __isl_give isl_union_pw_multi_aff *
8637 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8638 __isl_take isl_multi_union_pw_aff *mupa)
8640 isl_space *space;
8641 isl_multi_val *mv;
8642 isl_union_set *domain;
8644 space = isl_multi_union_pw_aff_get_space(mupa);
8645 mv = isl_multi_val_zero(space);
8646 domain = isl_multi_union_pw_aff_domain(mupa);
8647 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8650 /* Construct and return a union piecewise multi affine expression
8651 * that is equal to the given multi union piecewise affine expression.
8653 * If the input is zero-dimensional, then
8654 * construct a union piecewise multi affine expression
8655 * on top of the explicit domain of the input.
8657 __isl_give isl_union_pw_multi_aff *
8658 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8659 __isl_take isl_multi_union_pw_aff *mupa)
8661 int i;
8662 isl_size n;
8663 isl_space *space;
8664 isl_union_pw_multi_aff *upma;
8665 isl_union_pw_aff *upa;
8667 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8668 if (n < 0)
8669 mupa = isl_multi_union_pw_aff_free(mupa);
8670 if (!mupa)
8671 return NULL;
8673 if (n == 0)
8674 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8676 space = isl_multi_union_pw_aff_get_space(mupa);
8677 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8678 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8680 for (i = 1; i < n; ++i) {
8681 isl_union_pw_multi_aff *upma_i;
8683 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8684 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8685 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8688 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8690 isl_multi_union_pw_aff_free(mupa);
8691 return upma;
8694 /* Intersect the range of "mupa" with "range",
8695 * in the special case where "mupa" is 0D.
8697 * Intersect the domain of "mupa" with the constraints on the parameters
8698 * of "range".
8700 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8701 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8703 range = isl_set_params(range);
8704 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8705 return mupa;
8708 /* Intersect the range of "mupa" with "range".
8709 * That is, keep only those domain elements that have a function value
8710 * in "range".
8712 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8713 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8715 isl_union_pw_multi_aff *upma;
8716 isl_union_set *domain;
8717 isl_space *space;
8718 isl_size n;
8719 int match;
8721 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8722 if (n < 0 || !range)
8723 goto error;
8725 space = isl_set_get_space(range);
8726 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8727 space, isl_dim_set);
8728 isl_space_free(space);
8729 if (match < 0)
8730 goto error;
8731 if (!match)
8732 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8733 "space don't match", goto error);
8734 if (n == 0)
8735 return mupa_intersect_range_0D(mupa, range);
8737 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8738 isl_multi_union_pw_aff_copy(mupa));
8739 domain = isl_union_set_from_set(range);
8740 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8741 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8743 return mupa;
8744 error:
8745 isl_multi_union_pw_aff_free(mupa);
8746 isl_set_free(range);
8747 return NULL;
8750 /* Return the shared domain of the elements of "mupa",
8751 * in the special case where "mupa" is zero-dimensional.
8753 * Return the explicit domain of "mupa".
8754 * Note that this domain may be a parameter set, either
8755 * because "mupa" is meant to live in a set space or
8756 * because no explicit domain has been set.
8758 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8759 __isl_take isl_multi_union_pw_aff *mupa)
8761 isl_union_set *dom;
8763 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8764 isl_multi_union_pw_aff_free(mupa);
8766 return dom;
8769 /* Return the shared domain of the elements of "mupa".
8771 * If "mupa" is zero-dimensional, then return its explicit domain.
8773 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8774 __isl_take isl_multi_union_pw_aff *mupa)
8776 int i;
8777 isl_size n;
8778 isl_union_pw_aff *upa;
8779 isl_union_set *dom;
8781 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8782 if (n < 0)
8783 mupa = isl_multi_union_pw_aff_free(mupa);
8784 if (!mupa)
8785 return NULL;
8787 if (n == 0)
8788 return isl_multi_union_pw_aff_domain_0D(mupa);
8790 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8791 dom = isl_union_pw_aff_domain(upa);
8792 for (i = 1; i < n; ++i) {
8793 isl_union_set *dom_i;
8795 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8796 dom_i = isl_union_pw_aff_domain(upa);
8797 dom = isl_union_set_intersect(dom, dom_i);
8800 isl_multi_union_pw_aff_free(mupa);
8801 return dom;
8804 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8805 * In particular, the spaces have been aligned.
8806 * The result is defined over the shared domain of the elements of "mupa"
8808 * We first extract the parametric constant part of "aff" and
8809 * define that over the shared domain.
8810 * Then we iterate over all input dimensions of "aff" and add the corresponding
8811 * multiples of the elements of "mupa".
8812 * Finally, we consider the integer divisions, calling the function
8813 * recursively to obtain an isl_union_pw_aff corresponding to the
8814 * integer division argument.
8816 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8817 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8819 int i;
8820 isl_size n_in, n_div;
8821 isl_union_pw_aff *upa;
8822 isl_union_set *uset;
8823 isl_val *v;
8824 isl_aff *cst;
8826 n_in = isl_aff_dim(aff, isl_dim_in);
8827 n_div = isl_aff_dim(aff, isl_dim_div);
8828 if (n_in < 0 || n_div < 0)
8829 goto error;
8831 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8832 cst = isl_aff_copy(aff);
8833 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8834 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8835 cst = isl_aff_project_domain_on_params(cst);
8836 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8838 for (i = 0; i < n_in; ++i) {
8839 isl_union_pw_aff *upa_i;
8841 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8842 continue;
8843 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8844 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8845 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8846 upa = isl_union_pw_aff_add(upa, upa_i);
8849 for (i = 0; i < n_div; ++i) {
8850 isl_aff *div;
8851 isl_union_pw_aff *upa_i;
8853 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8854 continue;
8855 div = isl_aff_get_div(aff, i);
8856 upa_i = multi_union_pw_aff_apply_aff(
8857 isl_multi_union_pw_aff_copy(mupa), div);
8858 upa_i = isl_union_pw_aff_floor(upa_i);
8859 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8860 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8861 upa = isl_union_pw_aff_add(upa, upa_i);
8864 isl_multi_union_pw_aff_free(mupa);
8865 isl_aff_free(aff);
8867 return upa;
8868 error:
8869 isl_multi_union_pw_aff_free(mupa);
8870 isl_aff_free(aff);
8871 return NULL;
8874 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8875 * with the domain of "aff".
8876 * Furthermore, the dimension of this space needs to be greater than zero.
8877 * The result is defined over the shared domain of the elements of "mupa"
8879 * We perform these checks and then hand over control to
8880 * multi_union_pw_aff_apply_aff.
8882 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8883 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8885 isl_size dim;
8886 isl_space *space1, *space2;
8887 isl_bool equal;
8889 mupa = isl_multi_union_pw_aff_align_params(mupa,
8890 isl_aff_get_space(aff));
8891 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8892 if (!mupa || !aff)
8893 goto error;
8895 space1 = isl_multi_union_pw_aff_get_space(mupa);
8896 space2 = isl_aff_get_domain_space(aff);
8897 equal = isl_space_is_equal(space1, space2);
8898 isl_space_free(space1);
8899 isl_space_free(space2);
8900 if (equal < 0)
8901 goto error;
8902 if (!equal)
8903 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8904 "spaces don't match", goto error);
8905 dim = isl_aff_dim(aff, isl_dim_in);
8906 if (dim < 0)
8907 goto error;
8908 if (dim == 0)
8909 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8910 "cannot determine domains", goto error);
8912 return multi_union_pw_aff_apply_aff(mupa, aff);
8913 error:
8914 isl_multi_union_pw_aff_free(mupa);
8915 isl_aff_free(aff);
8916 return NULL;
8919 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8920 * The space of "mupa" is known to be compatible with the domain of "ma".
8922 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8923 * on the domain of "mupa".
8925 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
8926 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8928 isl_union_set *dom;
8930 dom = isl_multi_union_pw_aff_domain(mupa);
8931 ma = isl_multi_aff_project_domain_on_params(ma);
8933 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
8936 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8937 * with the domain of "ma".
8938 * The result is defined over the shared domain of the elements of "mupa"
8940 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8941 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8943 isl_space *space1, *space2;
8944 isl_multi_union_pw_aff *res;
8945 isl_bool equal;
8946 int i;
8947 isl_size n_in, n_out;
8949 mupa = isl_multi_union_pw_aff_align_params(mupa,
8950 isl_multi_aff_get_space(ma));
8951 ma = isl_multi_aff_align_params(ma,
8952 isl_multi_union_pw_aff_get_space(mupa));
8953 n_in = isl_multi_aff_dim(ma, isl_dim_in);
8954 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8955 if (!mupa || n_in < 0 || n_out < 0)
8956 goto error;
8958 space1 = isl_multi_union_pw_aff_get_space(mupa);
8959 space2 = isl_multi_aff_get_domain_space(ma);
8960 equal = isl_space_is_equal(space1, space2);
8961 isl_space_free(space1);
8962 isl_space_free(space2);
8963 if (equal < 0)
8964 goto error;
8965 if (!equal)
8966 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8967 "spaces don't match", goto error);
8968 if (n_in == 0)
8969 return mupa_apply_multi_aff_0D(mupa, ma);
8971 space1 = isl_space_range(isl_multi_aff_get_space(ma));
8972 res = isl_multi_union_pw_aff_alloc(space1);
8974 for (i = 0; i < n_out; ++i) {
8975 isl_aff *aff;
8976 isl_union_pw_aff *upa;
8978 aff = isl_multi_aff_get_aff(ma, i);
8979 upa = multi_union_pw_aff_apply_aff(
8980 isl_multi_union_pw_aff_copy(mupa), aff);
8981 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
8984 isl_multi_aff_free(ma);
8985 isl_multi_union_pw_aff_free(mupa);
8986 return res;
8987 error:
8988 isl_multi_union_pw_aff_free(mupa);
8989 isl_multi_aff_free(ma);
8990 return NULL;
8993 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
8994 * The space of "mupa" is known to be compatible with the domain of "pa".
8996 * Construct an isl_multi_union_pw_aff that is equal to "pa"
8997 * on the domain of "mupa".
8999 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9000 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9002 isl_union_set *dom;
9004 dom = isl_multi_union_pw_aff_domain(mupa);
9005 pa = isl_pw_aff_project_domain_on_params(pa);
9007 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9010 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9011 * with the domain of "pa".
9012 * Furthermore, the dimension of this space needs to be greater than zero.
9013 * The result is defined over the shared domain of the elements of "mupa"
9015 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9016 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9018 int i;
9019 isl_bool equal;
9020 isl_size n_in;
9021 isl_space *space, *space2;
9022 isl_union_pw_aff *upa;
9024 mupa = isl_multi_union_pw_aff_align_params(mupa,
9025 isl_pw_aff_get_space(pa));
9026 pa = isl_pw_aff_align_params(pa,
9027 isl_multi_union_pw_aff_get_space(mupa));
9028 if (!mupa || !pa)
9029 goto error;
9031 space = isl_multi_union_pw_aff_get_space(mupa);
9032 space2 = isl_pw_aff_get_domain_space(pa);
9033 equal = isl_space_is_equal(space, space2);
9034 isl_space_free(space);
9035 isl_space_free(space2);
9036 if (equal < 0)
9037 goto error;
9038 if (!equal)
9039 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9040 "spaces don't match", goto error);
9041 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9042 if (n_in < 0)
9043 goto error;
9044 if (n_in == 0)
9045 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9047 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9048 upa = isl_union_pw_aff_empty(space);
9050 for (i = 0; i < pa->n; ++i) {
9051 isl_aff *aff;
9052 isl_set *domain;
9053 isl_multi_union_pw_aff *mupa_i;
9054 isl_union_pw_aff *upa_i;
9056 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9057 domain = isl_set_copy(pa->p[i].set);
9058 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9059 aff = isl_aff_copy(pa->p[i].aff);
9060 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9061 upa = isl_union_pw_aff_union_add(upa, upa_i);
9064 isl_multi_union_pw_aff_free(mupa);
9065 isl_pw_aff_free(pa);
9066 return upa;
9067 error:
9068 isl_multi_union_pw_aff_free(mupa);
9069 isl_pw_aff_free(pa);
9070 return NULL;
9073 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9074 * The space of "mupa" is known to be compatible with the domain of "pma".
9076 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9077 * on the domain of "mupa".
9079 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9080 __isl_take isl_multi_union_pw_aff *mupa,
9081 __isl_take isl_pw_multi_aff *pma)
9083 isl_union_set *dom;
9085 dom = isl_multi_union_pw_aff_domain(mupa);
9086 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9088 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9091 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9092 * with the domain of "pma".
9093 * The result is defined over the shared domain of the elements of "mupa"
9095 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9096 __isl_take isl_multi_union_pw_aff *mupa,
9097 __isl_take isl_pw_multi_aff *pma)
9099 isl_space *space1, *space2;
9100 isl_multi_union_pw_aff *res;
9101 isl_bool equal;
9102 int i;
9103 isl_size n_in, n_out;
9105 mupa = isl_multi_union_pw_aff_align_params(mupa,
9106 isl_pw_multi_aff_get_space(pma));
9107 pma = isl_pw_multi_aff_align_params(pma,
9108 isl_multi_union_pw_aff_get_space(mupa));
9109 if (!mupa || !pma)
9110 goto error;
9112 space1 = isl_multi_union_pw_aff_get_space(mupa);
9113 space2 = isl_pw_multi_aff_get_domain_space(pma);
9114 equal = isl_space_is_equal(space1, space2);
9115 isl_space_free(space1);
9116 isl_space_free(space2);
9117 if (equal < 0)
9118 goto error;
9119 if (!equal)
9120 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9121 "spaces don't match", goto error);
9122 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9123 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9124 if (n_in < 0 || n_out < 0)
9125 goto error;
9126 if (n_in == 0)
9127 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9129 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9130 res = isl_multi_union_pw_aff_alloc(space1);
9132 for (i = 0; i < n_out; ++i) {
9133 isl_pw_aff *pa;
9134 isl_union_pw_aff *upa;
9136 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9137 upa = isl_multi_union_pw_aff_apply_pw_aff(
9138 isl_multi_union_pw_aff_copy(mupa), pa);
9139 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9142 isl_pw_multi_aff_free(pma);
9143 isl_multi_union_pw_aff_free(mupa);
9144 return res;
9145 error:
9146 isl_multi_union_pw_aff_free(mupa);
9147 isl_pw_multi_aff_free(pma);
9148 return NULL;
9151 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9152 * If the explicit domain only keeps track of constraints on the parameters,
9153 * then only update those constraints.
9155 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9156 __isl_take isl_multi_union_pw_aff *mupa,
9157 __isl_keep isl_union_pw_multi_aff *upma)
9159 isl_bool is_params;
9161 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9162 return isl_multi_union_pw_aff_free(mupa);
9164 mupa = isl_multi_union_pw_aff_cow(mupa);
9165 if (!mupa)
9166 return NULL;
9168 is_params = isl_union_set_is_params(mupa->u.dom);
9169 if (is_params < 0)
9170 return isl_multi_union_pw_aff_free(mupa);
9172 upma = isl_union_pw_multi_aff_copy(upma);
9173 if (is_params)
9174 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9175 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9176 else
9177 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9178 mupa->u.dom, upma);
9179 if (!mupa->u.dom)
9180 return isl_multi_union_pw_aff_free(mupa);
9181 return mupa;
9184 /* Compute the pullback of "mupa" by the function represented by "upma".
9185 * In other words, plug in "upma" in "mupa". The result contains
9186 * expressions defined over the domain space of "upma".
9188 * Run over all elements of "mupa" and plug in "upma" in each of them.
9190 * If "mupa" has an explicit domain, then it is this domain
9191 * that needs to undergo a pullback instead, i.e., a preimage.
9193 __isl_give isl_multi_union_pw_aff *
9194 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9195 __isl_take isl_multi_union_pw_aff *mupa,
9196 __isl_take isl_union_pw_multi_aff *upma)
9198 int i;
9199 isl_size n;
9201 mupa = isl_multi_union_pw_aff_align_params(mupa,
9202 isl_union_pw_multi_aff_get_space(upma));
9203 upma = isl_union_pw_multi_aff_align_params(upma,
9204 isl_multi_union_pw_aff_get_space(mupa));
9205 mupa = isl_multi_union_pw_aff_cow(mupa);
9206 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9207 if (n < 0 || !upma)
9208 goto error;
9210 for (i = 0; i < n; ++i) {
9211 isl_union_pw_aff *upa;
9213 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9214 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9215 isl_union_pw_multi_aff_copy(upma));
9216 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9219 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9220 mupa = preimage_explicit_domain(mupa, upma);
9222 isl_union_pw_multi_aff_free(upma);
9223 return mupa;
9224 error:
9225 isl_multi_union_pw_aff_free(mupa);
9226 isl_union_pw_multi_aff_free(upma);
9227 return NULL;
9230 /* Extract the sequence of elements in "mupa" with domain space "space"
9231 * (ignoring parameters).
9233 * For the elements of "mupa" that are not defined on the specified space,
9234 * the corresponding element in the result is empty.
9236 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9237 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9239 int i;
9240 isl_size n;
9241 isl_space *space_mpa;
9242 isl_multi_pw_aff *mpa;
9244 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9245 if (n < 0 || !space)
9246 goto error;
9248 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9249 space = isl_space_replace_params(space, space_mpa);
9250 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9251 space_mpa);
9252 mpa = isl_multi_pw_aff_alloc(space_mpa);
9254 space = isl_space_from_domain(space);
9255 space = isl_space_add_dims(space, isl_dim_out, 1);
9256 for (i = 0; i < n; ++i) {
9257 isl_union_pw_aff *upa;
9258 isl_pw_aff *pa;
9260 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9261 pa = isl_union_pw_aff_extract_pw_aff(upa,
9262 isl_space_copy(space));
9263 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9264 isl_union_pw_aff_free(upa);
9267 isl_space_free(space);
9268 return mpa;
9269 error:
9270 isl_space_free(space);
9271 return NULL;
9274 /* Evaluate the affine function "aff" in the void point "pnt".
9275 * In particular, return the value NaN.
9277 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9278 __isl_take isl_point *pnt)
9280 isl_ctx *ctx;
9282 ctx = isl_point_get_ctx(pnt);
9283 isl_aff_free(aff);
9284 isl_point_free(pnt);
9285 return isl_val_nan(ctx);
9288 /* Evaluate the affine expression "aff"
9289 * in the coordinates (with denominator) "pnt".
9291 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9292 __isl_keep isl_vec *pnt)
9294 isl_int n, d;
9295 isl_ctx *ctx;
9296 isl_val *v;
9298 if (!aff || !pnt)
9299 return NULL;
9301 ctx = isl_vec_get_ctx(aff);
9302 isl_int_init(n);
9303 isl_int_init(d);
9304 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9305 isl_int_mul(d, aff->el[0], pnt->el[0]);
9306 v = isl_val_rat_from_isl_int(ctx, n, d);
9307 v = isl_val_normalize(v);
9308 isl_int_clear(n);
9309 isl_int_clear(d);
9311 return v;
9314 /* Check that the domain space of "aff" is equal to "space".
9316 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9317 __isl_keep isl_space *space)
9319 isl_bool ok;
9321 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9322 if (ok < 0)
9323 return isl_stat_error;
9324 if (!ok)
9325 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9326 "incompatible spaces", return isl_stat_error);
9327 return isl_stat_ok;
9330 /* Evaluate the affine function "aff" in "pnt".
9332 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9333 __isl_take isl_point *pnt)
9335 isl_bool is_void;
9336 isl_val *v;
9337 isl_local_space *ls;
9339 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9340 goto error;
9341 is_void = isl_point_is_void(pnt);
9342 if (is_void < 0)
9343 goto error;
9344 if (is_void)
9345 return eval_void(aff, pnt);
9347 ls = isl_aff_get_domain_local_space(aff);
9348 pnt = isl_local_space_lift_point(ls, pnt);
9350 v = eval(aff->v, isl_point_peek_vec(pnt));
9352 isl_aff_free(aff);
9353 isl_point_free(pnt);
9355 return v;
9356 error:
9357 isl_aff_free(aff);
9358 isl_point_free(pnt);
9359 return NULL;