isl_union_map.c: is_subset_entry: use isl_map_peek_space
[isl.git] / isl_aff.c
blob831d2200691b6a5ea87530accb11adaddafa651c
1 /*
2 * Copyright 2011 INRIA Saclay
3 * Copyright 2011 Sven Verdoolaege
4 * Copyright 2012-2014 Ecole Normale Superieure
5 * Copyright 2014 INRIA Rocquencourt
6 * Copyright 2016 Sven Verdoolaege
7 * Copyright 2018 Cerebras Systems
9 * Use of this software is governed by the MIT license
11 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
12 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
13 * 91893 Orsay, France
14 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 * and Inria Paris - Rocquencourt, Domaine de Voluceau - Rocquencourt,
16 * B.P. 105 - 78153 Le Chesnay, France
17 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
20 #include <isl_ctx_private.h>
21 #include <isl_map_private.h>
22 #include <isl_union_map_private.h>
23 #include <isl_aff_private.h>
24 #include <isl_space_private.h>
25 #include <isl_local_space_private.h>
26 #include <isl_vec_private.h>
27 #include <isl_mat_private.h>
28 #include <isl_id_private.h>
29 #include <isl/constraint.h>
30 #include <isl_seq.h>
31 #include <isl/set.h>
32 #include <isl_val_private.h>
33 #include <isl_point_private.h>
34 #include <isl_config.h>
36 #undef EL_BASE
37 #define EL_BASE aff
39 #include <isl_list_templ.c>
41 #undef EL_BASE
42 #define EL_BASE pw_aff
44 #include <isl_list_templ.c>
46 #undef EL_BASE
47 #define EL_BASE pw_multi_aff
49 #include <isl_list_templ.c>
51 #undef EL_BASE
52 #define EL_BASE union_pw_aff
54 #include <isl_list_templ.c>
56 #undef EL_BASE
57 #define EL_BASE union_pw_multi_aff
59 #include <isl_list_templ.c>
61 __isl_give isl_aff *isl_aff_alloc_vec(__isl_take isl_local_space *ls,
62 __isl_take isl_vec *v)
64 isl_aff *aff;
66 if (!ls || !v)
67 goto error;
69 aff = isl_calloc_type(v->ctx, struct isl_aff);
70 if (!aff)
71 goto error;
73 aff->ref = 1;
74 aff->ls = ls;
75 aff->v = v;
77 return aff;
78 error:
79 isl_local_space_free(ls);
80 isl_vec_free(v);
81 return NULL;
84 __isl_give isl_aff *isl_aff_alloc(__isl_take isl_local_space *ls)
86 isl_ctx *ctx;
87 isl_vec *v;
88 isl_size total;
90 if (!ls)
91 return NULL;
93 ctx = isl_local_space_get_ctx(ls);
94 if (!isl_local_space_divs_known(ls))
95 isl_die(ctx, isl_error_invalid, "local space has unknown divs",
96 goto error);
97 if (!isl_local_space_is_set(ls))
98 isl_die(ctx, isl_error_invalid,
99 "domain of affine expression should be a set",
100 goto error);
102 total = isl_local_space_dim(ls, isl_dim_all);
103 if (total < 0)
104 goto error;
105 v = isl_vec_alloc(ctx, 1 + 1 + total);
106 return isl_aff_alloc_vec(ls, v);
107 error:
108 isl_local_space_free(ls);
109 return NULL;
112 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
114 if (!aff)
115 return NULL;
117 aff->ref++;
118 return aff;
121 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
123 if (!aff)
124 return NULL;
126 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
127 isl_vec_copy(aff->v));
130 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
132 if (!aff)
133 return NULL;
135 if (aff->ref == 1)
136 return aff;
137 aff->ref--;
138 return isl_aff_dup(aff);
141 __isl_give isl_aff *isl_aff_zero_on_domain(__isl_take isl_local_space *ls)
143 isl_aff *aff;
145 aff = isl_aff_alloc(ls);
146 if (!aff)
147 return NULL;
149 isl_int_set_si(aff->v->el[0], 1);
150 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
152 return aff;
155 /* Return an affine expression that is equal to zero on domain space "space".
157 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
159 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
162 /* Return a piecewise affine expression defined on the specified domain
163 * that is equal to zero.
165 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
167 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
170 /* Change "aff" into a NaN.
172 * Note that this function gets called from isl_aff_nan_on_domain,
173 * so "aff" may not have been initialized yet.
175 static __isl_give isl_aff *isl_aff_set_nan(__isl_take isl_aff *aff)
177 aff = isl_aff_cow(aff);
178 if (!aff)
179 return NULL;
181 aff->v = isl_vec_clr(aff->v);
182 if (!aff->v)
183 return isl_aff_free(aff);
185 return aff;
188 /* Return an affine expression defined on the specified domain
189 * that represents NaN.
191 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
193 isl_aff *aff;
195 aff = isl_aff_alloc(ls);
196 return isl_aff_set_nan(aff);
199 /* Return a piecewise affine expression defined on the specified domain
200 * that represents NaN.
202 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
204 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
207 /* Return an affine expression that is equal to "val" on
208 * domain local space "ls".
210 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
211 __isl_take isl_val *val)
213 isl_aff *aff;
215 if (!ls || !val)
216 goto error;
217 if (!isl_val_is_rat(val))
218 isl_die(isl_val_get_ctx(val), isl_error_invalid,
219 "expecting rational value", goto error);
221 aff = isl_aff_alloc(isl_local_space_copy(ls));
222 if (!aff)
223 goto error;
225 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
226 isl_int_set(aff->v->el[1], val->n);
227 isl_int_set(aff->v->el[0], val->d);
229 isl_local_space_free(ls);
230 isl_val_free(val);
231 return aff;
232 error:
233 isl_local_space_free(ls);
234 isl_val_free(val);
235 return NULL;
238 /* Return an affine expression that is equal to "val" on domain space "space".
240 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
241 __isl_take isl_val *val)
243 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
246 /* Return an affine expression that is equal to the specified dimension
247 * in "ls".
249 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
250 enum isl_dim_type type, unsigned pos)
252 isl_space *space;
253 isl_aff *aff;
255 if (!ls)
256 return NULL;
258 space = isl_local_space_get_space(ls);
259 if (!space)
260 goto error;
261 if (isl_space_is_map(space))
262 isl_die(isl_space_get_ctx(space), isl_error_invalid,
263 "expecting (parameter) set space", goto error);
264 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
265 goto error;
267 isl_space_free(space);
268 aff = isl_aff_alloc(ls);
269 if (!aff)
270 return NULL;
272 pos += isl_local_space_offset(aff->ls, type);
274 isl_int_set_si(aff->v->el[0], 1);
275 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
276 isl_int_set_si(aff->v->el[1 + pos], 1);
278 return aff;
279 error:
280 isl_local_space_free(ls);
281 isl_space_free(space);
282 return NULL;
285 /* Return a piecewise affine expression that is equal to
286 * the specified dimension in "ls".
288 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
289 enum isl_dim_type type, unsigned pos)
291 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
294 /* Return an affine expression that is equal to the parameter
295 * in the domain space "space" with identifier "id".
297 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
298 __isl_take isl_space *space, __isl_take isl_id *id)
300 int pos;
301 isl_local_space *ls;
303 if (!space || !id)
304 goto error;
305 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
306 if (pos < 0)
307 isl_die(isl_space_get_ctx(space), isl_error_invalid,
308 "parameter not found in space", goto error);
309 isl_id_free(id);
310 ls = isl_local_space_from_space(space);
311 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
312 error:
313 isl_space_free(space);
314 isl_id_free(id);
315 return NULL;
318 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
320 if (!aff)
321 return NULL;
323 if (--aff->ref > 0)
324 return NULL;
326 isl_local_space_free(aff->ls);
327 isl_vec_free(aff->v);
329 free(aff);
331 return NULL;
334 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
336 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
339 /* Return a hash value that digests "aff".
341 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
343 uint32_t hash, ls_hash, v_hash;
345 if (!aff)
346 return 0;
348 hash = isl_hash_init();
349 ls_hash = isl_local_space_get_hash(aff->ls);
350 isl_hash_hash(hash, ls_hash);
351 v_hash = isl_vec_get_hash(aff->v);
352 isl_hash_hash(hash, v_hash);
354 return hash;
357 /* Return the domain local space of "aff".
359 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
360 __isl_keep isl_aff *aff)
362 return aff ? aff->ls : NULL;
365 /* Return the number of variables of the given type in the domain of "aff".
367 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
369 isl_local_space *ls;
371 ls = isl_aff_peek_domain_local_space(aff);
372 return isl_local_space_dim(ls, type);
375 /* Externally, an isl_aff has a map space, but internally, the
376 * ls field corresponds to the domain of that space.
378 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
380 if (!aff)
381 return isl_size_error;
382 if (type == isl_dim_out)
383 return 1;
384 if (type == isl_dim_in)
385 type = isl_dim_set;
386 return isl_aff_domain_dim(aff, type);
389 /* Return the offset of the first coefficient of type "type" in
390 * the domain of "aff".
392 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
394 isl_local_space *ls;
396 ls = isl_aff_peek_domain_local_space(aff);
397 return isl_local_space_offset(ls, type);
400 /* Return the position of the dimension of the given type and name
401 * in "aff".
402 * Return -1 if no such dimension can be found.
404 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
405 const char *name)
407 if (!aff)
408 return -1;
409 if (type == isl_dim_out)
410 return -1;
411 if (type == isl_dim_in)
412 type = isl_dim_set;
413 return isl_local_space_find_dim_by_name(aff->ls, type, name);
416 /* Return the domain space of "aff".
418 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
420 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
423 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
425 return isl_space_copy(isl_aff_peek_domain_space(aff));
428 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
430 isl_space *space;
431 if (!aff)
432 return NULL;
433 space = isl_local_space_get_space(aff->ls);
434 space = isl_space_from_domain(space);
435 space = isl_space_add_dims(space, isl_dim_out, 1);
436 return space;
439 /* Return a copy of the domain space of "aff".
441 __isl_give isl_local_space *isl_aff_get_domain_local_space(
442 __isl_keep isl_aff *aff)
444 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
447 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
449 isl_local_space *ls;
450 if (!aff)
451 return NULL;
452 ls = isl_local_space_copy(aff->ls);
453 ls = isl_local_space_from_domain(ls);
454 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
455 return ls;
458 /* Return the local space of the domain of "aff".
459 * This may be either a copy or the local space itself
460 * if there is only one reference to "aff".
461 * This allows the local space to be modified inplace
462 * if both the expression and its local space have only a single reference.
463 * The caller is not allowed to modify "aff" between this call and
464 * a subsequent call to isl_aff_restore_domain_local_space.
465 * The only exception is that isl_aff_free can be called instead.
467 __isl_give isl_local_space *isl_aff_take_domain_local_space(
468 __isl_keep isl_aff *aff)
470 isl_local_space *ls;
472 if (!aff)
473 return NULL;
474 if (aff->ref != 1)
475 return isl_aff_get_domain_local_space(aff);
476 ls = aff->ls;
477 aff->ls = NULL;
478 return ls;
481 /* Set the local space of the domain of "aff" to "ls",
482 * where the local space of "aff" may be missing
483 * due to a preceding call to isl_aff_take_domain_local_space.
484 * However, in this case, "aff" only has a single reference and
485 * then the call to isl_aff_cow has no effect.
487 __isl_give isl_aff *isl_aff_restore_domain_local_space(
488 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
490 if (!aff || !ls)
491 goto error;
493 if (aff->ls == ls) {
494 isl_local_space_free(ls);
495 return aff;
498 aff = isl_aff_cow(aff);
499 if (!aff)
500 goto error;
501 isl_local_space_free(aff->ls);
502 aff->ls = ls;
504 return aff;
505 error:
506 isl_aff_free(aff);
507 isl_local_space_free(ls);
508 return NULL;
511 /* Externally, an isl_aff has a map space, but internally, the
512 * ls field corresponds to the domain of that space.
514 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
515 enum isl_dim_type type, unsigned pos)
517 if (!aff)
518 return NULL;
519 if (type == isl_dim_out)
520 return NULL;
521 if (type == isl_dim_in)
522 type = isl_dim_set;
523 return isl_local_space_get_dim_name(aff->ls, type, pos);
526 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
527 __isl_take isl_space *space)
529 aff = isl_aff_cow(aff);
530 if (!aff || !space)
531 goto error;
533 aff->ls = isl_local_space_reset_space(aff->ls, space);
534 if (!aff->ls)
535 return isl_aff_free(aff);
537 return aff;
538 error:
539 isl_aff_free(aff);
540 isl_space_free(space);
541 return NULL;
544 /* Reset the space of "aff". This function is called from isl_pw_templ.c
545 * and doesn't know if the space of an element object is represented
546 * directly or through its domain. It therefore passes along both.
548 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
549 __isl_take isl_space *space, __isl_take isl_space *domain)
551 isl_space_free(space);
552 return isl_aff_reset_domain_space(aff, domain);
555 /* Reorder the coefficients of the affine expression based
556 * on the given reordering.
557 * The reordering r is assumed to have been extended with the local
558 * variables.
560 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
561 __isl_take isl_reordering *r, int n_div)
563 isl_space *space;
564 isl_vec *res;
565 isl_size dim;
566 int i;
568 if (!vec || !r)
569 goto error;
571 space = isl_reordering_peek_space(r);
572 dim = isl_space_dim(space, isl_dim_all);
573 if (dim < 0)
574 goto error;
575 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
576 if (!res)
577 goto error;
578 isl_seq_cpy(res->el, vec->el, 2);
579 isl_seq_clr(res->el + 2, res->size - 2);
580 for (i = 0; i < r->len; ++i)
581 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
583 isl_reordering_free(r);
584 isl_vec_free(vec);
585 return res;
586 error:
587 isl_vec_free(vec);
588 isl_reordering_free(r);
589 return NULL;
592 /* Reorder the dimensions of the domain of "aff" according
593 * to the given reordering.
595 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
596 __isl_take isl_reordering *r)
598 aff = isl_aff_cow(aff);
599 if (!aff)
600 goto error;
602 r = isl_reordering_extend(r, aff->ls->div->n_row);
603 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
604 aff->ls->div->n_row);
605 aff->ls = isl_local_space_realign(aff->ls, r);
607 if (!aff->v || !aff->ls)
608 return isl_aff_free(aff);
610 return aff;
611 error:
612 isl_aff_free(aff);
613 isl_reordering_free(r);
614 return NULL;
617 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
618 __isl_take isl_space *model)
620 isl_bool equal_params;
622 if (!aff || !model)
623 goto error;
625 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
626 if (equal_params < 0)
627 goto error;
628 if (!equal_params) {
629 isl_reordering *exp;
631 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
632 exp = isl_reordering_extend_space(exp,
633 isl_aff_get_domain_space(aff));
634 aff = isl_aff_realign_domain(aff, exp);
637 isl_space_free(model);
638 return aff;
639 error:
640 isl_space_free(model);
641 isl_aff_free(aff);
642 return NULL;
645 #undef TYPE
646 #define TYPE isl_aff
647 #include "isl_unbind_params_templ.c"
649 /* Is "aff" obviously equal to zero?
651 * If the denominator is zero, then "aff" is not equal to zero.
653 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
655 int pos;
657 if (!aff)
658 return isl_bool_error;
660 if (isl_int_is_zero(aff->v->el[0]))
661 return isl_bool_false;
662 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
663 return isl_bool_ok(pos < 0);
666 /* Does "aff" represent NaN?
668 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
670 if (!aff)
671 return isl_bool_error;
673 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
676 /* Are "aff1" and "aff2" obviously equal?
678 * NaN is not equal to anything, not even to another NaN.
680 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
681 __isl_keep isl_aff *aff2)
683 isl_bool equal;
685 if (!aff1 || !aff2)
686 return isl_bool_error;
688 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
689 return isl_bool_false;
691 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
692 if (equal < 0 || !equal)
693 return equal;
695 return isl_vec_is_equal(aff1->v, aff2->v);
698 /* Return the common denominator of "aff" in "v".
700 * We cannot return anything meaningful in case of a NaN.
702 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
704 if (!aff)
705 return isl_stat_error;
706 if (isl_aff_is_nan(aff))
707 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
708 "cannot get denominator of NaN", return isl_stat_error);
709 isl_int_set(*v, aff->v->el[0]);
710 return isl_stat_ok;
713 /* Return the common denominator of "aff".
715 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
717 isl_ctx *ctx;
719 if (!aff)
720 return NULL;
722 ctx = isl_aff_get_ctx(aff);
723 if (isl_aff_is_nan(aff))
724 return isl_val_nan(ctx);
725 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
728 /* Return the constant term of "aff".
730 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
732 isl_ctx *ctx;
733 isl_val *v;
735 if (!aff)
736 return NULL;
738 ctx = isl_aff_get_ctx(aff);
739 if (isl_aff_is_nan(aff))
740 return isl_val_nan(ctx);
741 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
742 return isl_val_normalize(v);
745 /* Return the coefficient of the variable of type "type" at position "pos"
746 * of "aff".
748 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
749 enum isl_dim_type type, int pos)
751 isl_ctx *ctx;
752 isl_val *v;
754 if (!aff)
755 return NULL;
757 ctx = isl_aff_get_ctx(aff);
758 if (type == isl_dim_out)
759 isl_die(ctx, isl_error_invalid,
760 "output/set dimension does not have a coefficient",
761 return NULL);
762 if (type == isl_dim_in)
763 type = isl_dim_set;
765 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
766 return NULL;
768 if (isl_aff_is_nan(aff))
769 return isl_val_nan(ctx);
770 pos += isl_local_space_offset(aff->ls, type);
771 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
772 return isl_val_normalize(v);
775 /* Return the sign of the coefficient of the variable of type "type"
776 * at position "pos" of "aff".
778 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
779 int pos)
781 isl_ctx *ctx;
783 if (!aff)
784 return 0;
786 ctx = isl_aff_get_ctx(aff);
787 if (type == isl_dim_out)
788 isl_die(ctx, isl_error_invalid,
789 "output/set dimension does not have a coefficient",
790 return 0);
791 if (type == isl_dim_in)
792 type = isl_dim_set;
794 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
795 return 0;
797 pos += isl_local_space_offset(aff->ls, type);
798 return isl_int_sgn(aff->v->el[1 + pos]);
801 /* Replace the numerator of the constant term of "aff" by "v".
803 * A NaN is unaffected by this operation.
805 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
807 if (!aff)
808 return NULL;
809 if (isl_aff_is_nan(aff))
810 return aff;
811 aff = isl_aff_cow(aff);
812 if (!aff)
813 return NULL;
815 aff->v = isl_vec_cow(aff->v);
816 if (!aff->v)
817 return isl_aff_free(aff);
819 isl_int_set(aff->v->el[1], v);
821 return aff;
824 /* Replace the constant term of "aff" by "v".
826 * A NaN is unaffected by this operation.
828 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
829 __isl_take isl_val *v)
831 if (!aff || !v)
832 goto error;
834 if (isl_aff_is_nan(aff)) {
835 isl_val_free(v);
836 return aff;
839 if (!isl_val_is_rat(v))
840 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
841 "expecting rational value", goto error);
843 if (isl_int_eq(aff->v->el[1], v->n) &&
844 isl_int_eq(aff->v->el[0], v->d)) {
845 isl_val_free(v);
846 return aff;
849 aff = isl_aff_cow(aff);
850 if (!aff)
851 goto error;
852 aff->v = isl_vec_cow(aff->v);
853 if (!aff->v)
854 goto error;
856 if (isl_int_eq(aff->v->el[0], v->d)) {
857 isl_int_set(aff->v->el[1], v->n);
858 } else if (isl_int_is_one(v->d)) {
859 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
860 } else {
861 isl_seq_scale(aff->v->el + 1,
862 aff->v->el + 1, v->d, aff->v->size - 1);
863 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
864 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
865 aff->v = isl_vec_normalize(aff->v);
866 if (!aff->v)
867 goto error;
870 isl_val_free(v);
871 return aff;
872 error:
873 isl_aff_free(aff);
874 isl_val_free(v);
875 return NULL;
878 /* Add "v" to the constant term of "aff".
880 * A NaN is unaffected by this operation.
882 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
884 if (isl_int_is_zero(v))
885 return aff;
887 if (!aff)
888 return NULL;
889 if (isl_aff_is_nan(aff))
890 return aff;
891 aff = isl_aff_cow(aff);
892 if (!aff)
893 return NULL;
895 aff->v = isl_vec_cow(aff->v);
896 if (!aff->v)
897 return isl_aff_free(aff);
899 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
901 return aff;
904 /* Add "v" to the constant term of "aff".
906 * A NaN is unaffected by this operation.
908 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
909 __isl_take isl_val *v)
911 if (!aff || !v)
912 goto error;
914 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
915 isl_val_free(v);
916 return aff;
919 if (!isl_val_is_rat(v))
920 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
921 "expecting rational value", goto error);
923 aff = isl_aff_cow(aff);
924 if (!aff)
925 goto error;
927 aff->v = isl_vec_cow(aff->v);
928 if (!aff->v)
929 goto error;
931 if (isl_int_is_one(v->d)) {
932 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
933 } else if (isl_int_eq(aff->v->el[0], v->d)) {
934 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
935 aff->v = isl_vec_normalize(aff->v);
936 if (!aff->v)
937 goto error;
938 } else {
939 isl_seq_scale(aff->v->el + 1,
940 aff->v->el + 1, v->d, aff->v->size - 1);
941 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
942 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
943 aff->v = isl_vec_normalize(aff->v);
944 if (!aff->v)
945 goto error;
948 isl_val_free(v);
949 return aff;
950 error:
951 isl_aff_free(aff);
952 isl_val_free(v);
953 return NULL;
956 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
958 isl_int t;
960 isl_int_init(t);
961 isl_int_set_si(t, v);
962 aff = isl_aff_add_constant(aff, t);
963 isl_int_clear(t);
965 return aff;
968 /* Add "v" to the numerator of the constant term of "aff".
970 * A NaN is unaffected by this operation.
972 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
974 if (isl_int_is_zero(v))
975 return aff;
977 if (!aff)
978 return NULL;
979 if (isl_aff_is_nan(aff))
980 return aff;
981 aff = isl_aff_cow(aff);
982 if (!aff)
983 return NULL;
985 aff->v = isl_vec_cow(aff->v);
986 if (!aff->v)
987 return isl_aff_free(aff);
989 isl_int_add(aff->v->el[1], aff->v->el[1], v);
991 return aff;
994 /* Add "v" to the numerator of the constant term of "aff".
996 * A NaN is unaffected by this operation.
998 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1000 isl_int t;
1002 if (v == 0)
1003 return aff;
1005 isl_int_init(t);
1006 isl_int_set_si(t, v);
1007 aff = isl_aff_add_constant_num(aff, t);
1008 isl_int_clear(t);
1010 return aff;
1013 /* Replace the numerator of the constant term of "aff" by "v".
1015 * A NaN is unaffected by this operation.
1017 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1019 if (!aff)
1020 return NULL;
1021 if (isl_aff_is_nan(aff))
1022 return aff;
1023 aff = isl_aff_cow(aff);
1024 if (!aff)
1025 return NULL;
1027 aff->v = isl_vec_cow(aff->v);
1028 if (!aff->v)
1029 return isl_aff_free(aff);
1031 isl_int_set_si(aff->v->el[1], v);
1033 return aff;
1036 /* Replace the numerator of the coefficient of the variable of type "type"
1037 * at position "pos" of "aff" by "v".
1039 * A NaN is unaffected by this operation.
1041 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1042 enum isl_dim_type type, int pos, isl_int v)
1044 if (!aff)
1045 return NULL;
1047 if (type == isl_dim_out)
1048 isl_die(aff->v->ctx, isl_error_invalid,
1049 "output/set dimension does not have a coefficient",
1050 return isl_aff_free(aff));
1051 if (type == isl_dim_in)
1052 type = isl_dim_set;
1054 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1055 return isl_aff_free(aff);
1057 if (isl_aff_is_nan(aff))
1058 return aff;
1059 aff = isl_aff_cow(aff);
1060 if (!aff)
1061 return NULL;
1063 aff->v = isl_vec_cow(aff->v);
1064 if (!aff->v)
1065 return isl_aff_free(aff);
1067 pos += isl_local_space_offset(aff->ls, type);
1068 isl_int_set(aff->v->el[1 + pos], v);
1070 return aff;
1073 /* Replace the numerator of the coefficient of the variable of type "type"
1074 * at position "pos" of "aff" by "v".
1076 * A NaN is unaffected by this operation.
1078 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1079 enum isl_dim_type type, int pos, int v)
1081 if (!aff)
1082 return NULL;
1084 if (type == isl_dim_out)
1085 isl_die(aff->v->ctx, isl_error_invalid,
1086 "output/set dimension does not have a coefficient",
1087 return isl_aff_free(aff));
1088 if (type == isl_dim_in)
1089 type = isl_dim_set;
1091 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1092 return isl_aff_free(aff);
1094 if (isl_aff_is_nan(aff))
1095 return aff;
1096 pos += isl_local_space_offset(aff->ls, type);
1097 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1098 return aff;
1100 aff = isl_aff_cow(aff);
1101 if (!aff)
1102 return NULL;
1104 aff->v = isl_vec_cow(aff->v);
1105 if (!aff->v)
1106 return isl_aff_free(aff);
1108 isl_int_set_si(aff->v->el[1 + pos], v);
1110 return aff;
1113 /* Replace the coefficient of the variable of type "type" at position "pos"
1114 * of "aff" by "v".
1116 * A NaN is unaffected by this operation.
1118 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1119 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1121 if (!aff || !v)
1122 goto error;
1124 if (type == isl_dim_out)
1125 isl_die(aff->v->ctx, isl_error_invalid,
1126 "output/set dimension does not have a coefficient",
1127 goto error);
1128 if (type == isl_dim_in)
1129 type = isl_dim_set;
1131 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1132 return isl_aff_free(aff);
1134 if (isl_aff_is_nan(aff)) {
1135 isl_val_free(v);
1136 return aff;
1138 if (!isl_val_is_rat(v))
1139 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1140 "expecting rational value", goto error);
1142 pos += isl_local_space_offset(aff->ls, type);
1143 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1144 isl_int_eq(aff->v->el[0], v->d)) {
1145 isl_val_free(v);
1146 return aff;
1149 aff = isl_aff_cow(aff);
1150 if (!aff)
1151 goto error;
1152 aff->v = isl_vec_cow(aff->v);
1153 if (!aff->v)
1154 goto error;
1156 if (isl_int_eq(aff->v->el[0], v->d)) {
1157 isl_int_set(aff->v->el[1 + pos], v->n);
1158 } else if (isl_int_is_one(v->d)) {
1159 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1160 } else {
1161 isl_seq_scale(aff->v->el + 1,
1162 aff->v->el + 1, v->d, aff->v->size - 1);
1163 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1164 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1165 aff->v = isl_vec_normalize(aff->v);
1166 if (!aff->v)
1167 goto error;
1170 isl_val_free(v);
1171 return aff;
1172 error:
1173 isl_aff_free(aff);
1174 isl_val_free(v);
1175 return NULL;
1178 /* Add "v" to the coefficient of the variable of type "type"
1179 * at position "pos" of "aff".
1181 * A NaN is unaffected by this operation.
1183 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1184 enum isl_dim_type type, int pos, isl_int v)
1186 if (!aff)
1187 return NULL;
1189 if (type == isl_dim_out)
1190 isl_die(aff->v->ctx, isl_error_invalid,
1191 "output/set dimension does not have a coefficient",
1192 return isl_aff_free(aff));
1193 if (type == isl_dim_in)
1194 type = isl_dim_set;
1196 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1197 return isl_aff_free(aff);
1199 if (isl_aff_is_nan(aff))
1200 return aff;
1201 aff = isl_aff_cow(aff);
1202 if (!aff)
1203 return NULL;
1205 aff->v = isl_vec_cow(aff->v);
1206 if (!aff->v)
1207 return isl_aff_free(aff);
1209 pos += isl_local_space_offset(aff->ls, type);
1210 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1212 return aff;
1215 /* Add "v" to the coefficient of the variable of type "type"
1216 * at position "pos" of "aff".
1218 * A NaN is unaffected by this operation.
1220 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1221 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1223 if (!aff || !v)
1224 goto error;
1226 if (isl_val_is_zero(v)) {
1227 isl_val_free(v);
1228 return aff;
1231 if (type == isl_dim_out)
1232 isl_die(aff->v->ctx, isl_error_invalid,
1233 "output/set dimension does not have a coefficient",
1234 goto error);
1235 if (type == isl_dim_in)
1236 type = isl_dim_set;
1238 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1239 goto error;
1241 if (isl_aff_is_nan(aff)) {
1242 isl_val_free(v);
1243 return aff;
1245 if (!isl_val_is_rat(v))
1246 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1247 "expecting rational value", goto error);
1249 aff = isl_aff_cow(aff);
1250 if (!aff)
1251 goto error;
1253 aff->v = isl_vec_cow(aff->v);
1254 if (!aff->v)
1255 goto error;
1257 pos += isl_local_space_offset(aff->ls, type);
1258 if (isl_int_is_one(v->d)) {
1259 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1260 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1261 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1262 aff->v = isl_vec_normalize(aff->v);
1263 if (!aff->v)
1264 goto error;
1265 } else {
1266 isl_seq_scale(aff->v->el + 1,
1267 aff->v->el + 1, v->d, aff->v->size - 1);
1268 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1269 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1270 aff->v = isl_vec_normalize(aff->v);
1271 if (!aff->v)
1272 goto error;
1275 isl_val_free(v);
1276 return aff;
1277 error:
1278 isl_aff_free(aff);
1279 isl_val_free(v);
1280 return NULL;
1283 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1284 enum isl_dim_type type, int pos, int v)
1286 isl_int t;
1288 isl_int_init(t);
1289 isl_int_set_si(t, v);
1290 aff = isl_aff_add_coefficient(aff, type, pos, t);
1291 isl_int_clear(t);
1293 return aff;
1296 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1298 if (!aff)
1299 return NULL;
1301 return isl_local_space_get_div(aff->ls, pos);
1304 /* Return the negation of "aff".
1306 * As a special case, -NaN = NaN.
1308 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1310 if (!aff)
1311 return NULL;
1312 if (isl_aff_is_nan(aff))
1313 return aff;
1314 aff = isl_aff_cow(aff);
1315 if (!aff)
1316 return NULL;
1317 aff->v = isl_vec_cow(aff->v);
1318 if (!aff->v)
1319 return isl_aff_free(aff);
1321 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1323 return aff;
1326 /* Remove divs from the local space that do not appear in the affine
1327 * expression.
1328 * We currently only remove divs at the end.
1329 * Some intermediate divs may also not appear directly in the affine
1330 * expression, but we would also need to check that no other divs are
1331 * defined in terms of them.
1333 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1335 int pos;
1336 isl_size off;
1337 isl_size n;
1339 n = isl_aff_domain_dim(aff, isl_dim_div);
1340 off = isl_aff_domain_offset(aff, isl_dim_div);
1341 if (n < 0 || off < 0)
1342 return isl_aff_free(aff);
1344 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1345 if (pos == n)
1346 return aff;
1348 aff = isl_aff_cow(aff);
1349 if (!aff)
1350 return NULL;
1352 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1353 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1354 if (!aff->ls || !aff->v)
1355 return isl_aff_free(aff);
1357 return aff;
1360 /* Look for any divs in the aff->ls with a denominator equal to one
1361 * and plug them into the affine expression and any subsequent divs
1362 * that may reference the div.
1364 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1366 int i;
1367 isl_size n;
1368 int len;
1369 isl_int v;
1370 isl_vec *vec;
1371 isl_local_space *ls;
1372 isl_size off;
1374 n = isl_aff_domain_dim(aff, isl_dim_div);
1375 off = isl_aff_domain_offset(aff, isl_dim_div);
1376 if (n < 0 || off < 0)
1377 return isl_aff_free(aff);
1378 len = aff->v->size;
1379 for (i = 0; i < n; ++i) {
1380 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1381 continue;
1382 ls = isl_local_space_copy(aff->ls);
1383 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1384 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1385 vec = isl_vec_copy(aff->v);
1386 vec = isl_vec_cow(vec);
1387 if (!ls || !vec)
1388 goto error;
1390 isl_int_init(v);
1392 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1393 len, len, v);
1395 isl_int_clear(v);
1397 isl_vec_free(aff->v);
1398 aff->v = vec;
1399 isl_local_space_free(aff->ls);
1400 aff->ls = ls;
1403 return aff;
1404 error:
1405 isl_vec_free(vec);
1406 isl_local_space_free(ls);
1407 return isl_aff_free(aff);
1410 /* Look for any divs j that appear with a unit coefficient inside
1411 * the definitions of other divs i and plug them into the definitions
1412 * of the divs i.
1414 * In particular, an expression of the form
1416 * floor((f(..) + floor(g(..)/n))/m)
1418 * is simplified to
1420 * floor((n * f(..) + g(..))/(n * m))
1422 * This simplification is correct because we can move the expression
1423 * f(..) into the inner floor in the original expression to obtain
1425 * floor(floor((n * f(..) + g(..))/n)/m)
1427 * from which we can derive the simplified expression.
1429 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1431 int i, j;
1432 isl_size n;
1433 isl_size off;
1435 n = isl_aff_domain_dim(aff, isl_dim_div);
1436 off = isl_aff_domain_offset(aff, isl_dim_div);
1437 if (n < 0 || off < 0)
1438 return isl_aff_free(aff);
1439 for (i = 1; i < n; ++i) {
1440 for (j = 0; j < i; ++j) {
1441 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1442 continue;
1443 aff->ls = isl_local_space_substitute_seq(aff->ls,
1444 isl_dim_div, j, aff->ls->div->row[j],
1445 aff->v->size, i, 1);
1446 if (!aff->ls)
1447 return isl_aff_free(aff);
1451 return aff;
1454 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1456 * Even though this function is only called on isl_affs with a single
1457 * reference, we are careful to only change aff->v and aff->ls together.
1459 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1461 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1462 isl_local_space *ls;
1463 isl_vec *v;
1465 if (off < 0)
1466 return isl_aff_free(aff);
1468 ls = isl_local_space_copy(aff->ls);
1469 ls = isl_local_space_swap_div(ls, a, b);
1470 v = isl_vec_copy(aff->v);
1471 v = isl_vec_cow(v);
1472 if (!ls || !v)
1473 goto error;
1475 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1476 isl_vec_free(aff->v);
1477 aff->v = v;
1478 isl_local_space_free(aff->ls);
1479 aff->ls = ls;
1481 return aff;
1482 error:
1483 isl_vec_free(v);
1484 isl_local_space_free(ls);
1485 return isl_aff_free(aff);
1488 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1490 * We currently do not actually remove div "b", but simply add its
1491 * coefficient to that of "a" and then zero it out.
1493 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1495 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1497 if (off < 0)
1498 return isl_aff_free(aff);
1500 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1501 return aff;
1503 aff->v = isl_vec_cow(aff->v);
1504 if (!aff->v)
1505 return isl_aff_free(aff);
1507 isl_int_add(aff->v->el[1 + off + a],
1508 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1509 isl_int_set_si(aff->v->el[1 + off + b], 0);
1511 return aff;
1514 /* Sort the divs in the local space of "aff" according to
1515 * the comparison function "cmp_row" in isl_local_space.c,
1516 * combining the coefficients of identical divs.
1518 * Reordering divs does not change the semantics of "aff",
1519 * so there is no need to call isl_aff_cow.
1520 * Moreover, this function is currently only called on isl_affs
1521 * with a single reference.
1523 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1525 isl_size n;
1526 int i, j;
1528 n = isl_aff_dim(aff, isl_dim_div);
1529 if (n < 0)
1530 return isl_aff_free(aff);
1531 for (i = 1; i < n; ++i) {
1532 for (j = i - 1; j >= 0; --j) {
1533 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1534 if (cmp < 0)
1535 break;
1536 if (cmp == 0)
1537 aff = merge_divs(aff, j, j + 1);
1538 else
1539 aff = swap_div(aff, j, j + 1);
1540 if (!aff)
1541 return NULL;
1545 return aff;
1548 /* Normalize the representation of "aff".
1550 * This function should only be called on "new" isl_affs, i.e.,
1551 * with only a single reference. We therefore do not need to
1552 * worry about affecting other instances.
1554 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1556 if (!aff)
1557 return NULL;
1558 aff->v = isl_vec_normalize(aff->v);
1559 if (!aff->v)
1560 return isl_aff_free(aff);
1561 aff = plug_in_integral_divs(aff);
1562 aff = plug_in_unit_divs(aff);
1563 aff = sort_divs(aff);
1564 aff = isl_aff_remove_unused_divs(aff);
1565 return aff;
1568 /* Given f, return floor(f).
1569 * If f is an integer expression, then just return f.
1570 * If f is a constant, then return the constant floor(f).
1571 * Otherwise, if f = g/m, write g = q m + r,
1572 * create a new div d = [r/m] and return the expression q + d.
1573 * The coefficients in r are taken to lie between -m/2 and m/2.
1575 * reduce_div_coefficients performs the same normalization.
1577 * As a special case, floor(NaN) = NaN.
1579 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1581 int i;
1582 int size;
1583 isl_ctx *ctx;
1584 isl_vec *div;
1586 if (!aff)
1587 return NULL;
1589 if (isl_aff_is_nan(aff))
1590 return aff;
1591 if (isl_int_is_one(aff->v->el[0]))
1592 return aff;
1594 aff = isl_aff_cow(aff);
1595 if (!aff)
1596 return NULL;
1598 aff->v = isl_vec_cow(aff->v);
1599 if (!aff->v)
1600 return isl_aff_free(aff);
1602 if (isl_aff_is_cst(aff)) {
1603 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1604 isl_int_set_si(aff->v->el[0], 1);
1605 return aff;
1608 div = isl_vec_copy(aff->v);
1609 div = isl_vec_cow(div);
1610 if (!div)
1611 return isl_aff_free(aff);
1613 ctx = isl_aff_get_ctx(aff);
1614 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1615 for (i = 1; i < aff->v->size; ++i) {
1616 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1617 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1618 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1619 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1620 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1624 aff->ls = isl_local_space_add_div(aff->ls, div);
1625 if (!aff->ls)
1626 return isl_aff_free(aff);
1628 size = aff->v->size;
1629 aff->v = isl_vec_extend(aff->v, size + 1);
1630 if (!aff->v)
1631 return isl_aff_free(aff);
1632 isl_int_set_si(aff->v->el[0], 1);
1633 isl_int_set_si(aff->v->el[size], 1);
1635 aff = isl_aff_normalize(aff);
1637 return aff;
1640 /* Compute
1642 * aff mod m = aff - m * floor(aff/m)
1644 * with m an integer value.
1646 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1647 __isl_take isl_val *m)
1649 isl_aff *res;
1651 if (!aff || !m)
1652 goto error;
1654 if (!isl_val_is_int(m))
1655 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1656 "expecting integer modulo", goto error);
1658 res = isl_aff_copy(aff);
1659 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1660 aff = isl_aff_floor(aff);
1661 aff = isl_aff_scale_val(aff, m);
1662 res = isl_aff_sub(res, aff);
1664 return res;
1665 error:
1666 isl_aff_free(aff);
1667 isl_val_free(m);
1668 return NULL;
1671 /* Compute
1673 * pwaff mod m = pwaff - m * floor(pwaff/m)
1675 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1677 isl_pw_aff *res;
1679 res = isl_pw_aff_copy(pwaff);
1680 pwaff = isl_pw_aff_scale_down(pwaff, m);
1681 pwaff = isl_pw_aff_floor(pwaff);
1682 pwaff = isl_pw_aff_scale(pwaff, m);
1683 res = isl_pw_aff_sub(res, pwaff);
1685 return res;
1688 /* Compute
1690 * pa mod m = pa - m * floor(pa/m)
1692 * with m an integer value.
1694 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1695 __isl_take isl_val *m)
1697 if (!pa || !m)
1698 goto error;
1699 if (!isl_val_is_int(m))
1700 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1701 "expecting integer modulo", goto error);
1702 pa = isl_pw_aff_mod(pa, m->n);
1703 isl_val_free(m);
1704 return pa;
1705 error:
1706 isl_pw_aff_free(pa);
1707 isl_val_free(m);
1708 return NULL;
1711 /* Given f, return ceil(f).
1712 * If f is an integer expression, then just return f.
1713 * Otherwise, let f be the expression
1715 * e/m
1717 * then return
1719 * floor((e + m - 1)/m)
1721 * As a special case, ceil(NaN) = NaN.
1723 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1725 if (!aff)
1726 return NULL;
1728 if (isl_aff_is_nan(aff))
1729 return aff;
1730 if (isl_int_is_one(aff->v->el[0]))
1731 return aff;
1733 aff = isl_aff_cow(aff);
1734 if (!aff)
1735 return NULL;
1736 aff->v = isl_vec_cow(aff->v);
1737 if (!aff->v)
1738 return isl_aff_free(aff);
1740 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1741 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1742 aff = isl_aff_floor(aff);
1744 return aff;
1747 /* Apply the expansion computed by isl_merge_divs.
1748 * The expansion itself is given by "exp" while the resulting
1749 * list of divs is given by "div".
1751 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1752 __isl_take isl_mat *div, int *exp)
1754 isl_size old_n_div;
1755 isl_size new_n_div;
1756 isl_size offset;
1758 aff = isl_aff_cow(aff);
1760 offset = isl_aff_domain_offset(aff, isl_dim_div);
1761 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1762 new_n_div = isl_mat_rows(div);
1763 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1764 goto error;
1766 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1767 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1768 if (!aff->v || !aff->ls)
1769 return isl_aff_free(aff);
1770 return aff;
1771 error:
1772 isl_aff_free(aff);
1773 isl_mat_free(div);
1774 return NULL;
1777 /* Add two affine expressions that live in the same local space.
1779 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1780 __isl_take isl_aff *aff2)
1782 isl_int gcd, f;
1784 aff1 = isl_aff_cow(aff1);
1785 if (!aff1 || !aff2)
1786 goto error;
1788 aff1->v = isl_vec_cow(aff1->v);
1789 if (!aff1->v)
1790 goto error;
1792 isl_int_init(gcd);
1793 isl_int_init(f);
1794 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1795 isl_int_divexact(f, aff2->v->el[0], gcd);
1796 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1797 isl_int_divexact(f, aff1->v->el[0], gcd);
1798 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1799 isl_int_divexact(f, aff2->v->el[0], gcd);
1800 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1801 isl_int_clear(f);
1802 isl_int_clear(gcd);
1804 isl_aff_free(aff2);
1805 aff1 = isl_aff_normalize(aff1);
1806 return aff1;
1807 error:
1808 isl_aff_free(aff1);
1809 isl_aff_free(aff2);
1810 return NULL;
1813 /* Replace one of the arguments by a NaN and free the other one.
1815 static __isl_give isl_aff *set_nan_free(__isl_take isl_aff *aff1,
1816 __isl_take isl_aff *aff2)
1818 isl_aff_free(aff2);
1819 return isl_aff_set_nan(aff1);
1822 /* Return the sum of "aff1" and "aff2".
1824 * If either of the two is NaN, then the result is NaN.
1826 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1827 __isl_take isl_aff *aff2)
1829 isl_ctx *ctx;
1830 int *exp1 = NULL;
1831 int *exp2 = NULL;
1832 isl_mat *div;
1833 isl_size n_div1, n_div2;
1835 if (!aff1 || !aff2)
1836 goto error;
1838 ctx = isl_aff_get_ctx(aff1);
1839 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1840 isl_die(ctx, isl_error_invalid,
1841 "spaces don't match", goto error);
1843 if (isl_aff_is_nan(aff1)) {
1844 isl_aff_free(aff2);
1845 return aff1;
1847 if (isl_aff_is_nan(aff2)) {
1848 isl_aff_free(aff1);
1849 return aff2;
1852 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1853 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1854 if (n_div1 < 0 || n_div2 < 0)
1855 goto error;
1856 if (n_div1 == 0 && n_div2 == 0)
1857 return add_expanded(aff1, aff2);
1859 exp1 = isl_alloc_array(ctx, int, n_div1);
1860 exp2 = isl_alloc_array(ctx, int, n_div2);
1861 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1862 goto error;
1864 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1865 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1866 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1867 free(exp1);
1868 free(exp2);
1870 return add_expanded(aff1, aff2);
1871 error:
1872 free(exp1);
1873 free(exp2);
1874 isl_aff_free(aff1);
1875 isl_aff_free(aff2);
1876 return NULL;
1879 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1880 __isl_take isl_aff *aff2)
1882 return isl_aff_add(aff1, isl_aff_neg(aff2));
1885 /* Return the result of scaling "aff" by a factor of "f".
1887 * As a special case, f * NaN = NaN.
1889 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1891 isl_int gcd;
1893 if (!aff)
1894 return NULL;
1895 if (isl_aff_is_nan(aff))
1896 return aff;
1898 if (isl_int_is_one(f))
1899 return aff;
1901 aff = isl_aff_cow(aff);
1902 if (!aff)
1903 return NULL;
1904 aff->v = isl_vec_cow(aff->v);
1905 if (!aff->v)
1906 return isl_aff_free(aff);
1908 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1909 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1910 return aff;
1913 isl_int_init(gcd);
1914 isl_int_gcd(gcd, aff->v->el[0], f);
1915 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1916 isl_int_divexact(gcd, f, gcd);
1917 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1918 isl_int_clear(gcd);
1920 return aff;
1923 /* Multiple "aff" by "v".
1925 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1926 __isl_take isl_val *v)
1928 if (!aff || !v)
1929 goto error;
1931 if (isl_val_is_one(v)) {
1932 isl_val_free(v);
1933 return aff;
1936 if (!isl_val_is_rat(v))
1937 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1938 "expecting rational factor", goto error);
1940 aff = isl_aff_scale(aff, v->n);
1941 aff = isl_aff_scale_down(aff, v->d);
1943 isl_val_free(v);
1944 return aff;
1945 error:
1946 isl_aff_free(aff);
1947 isl_val_free(v);
1948 return NULL;
1951 /* Return the result of scaling "aff" down by a factor of "f".
1953 * As a special case, NaN/f = NaN.
1955 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1957 isl_int gcd;
1959 if (!aff)
1960 return NULL;
1961 if (isl_aff_is_nan(aff))
1962 return aff;
1964 if (isl_int_is_one(f))
1965 return aff;
1967 aff = isl_aff_cow(aff);
1968 if (!aff)
1969 return NULL;
1971 if (isl_int_is_zero(f))
1972 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1973 "cannot scale down by zero", return isl_aff_free(aff));
1975 aff->v = isl_vec_cow(aff->v);
1976 if (!aff->v)
1977 return isl_aff_free(aff);
1979 isl_int_init(gcd);
1980 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1981 isl_int_gcd(gcd, gcd, f);
1982 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1983 isl_int_divexact(gcd, f, gcd);
1984 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1985 isl_int_clear(gcd);
1987 return aff;
1990 /* Divide "aff" by "v".
1992 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1993 __isl_take isl_val *v)
1995 if (!aff || !v)
1996 goto error;
1998 if (isl_val_is_one(v)) {
1999 isl_val_free(v);
2000 return aff;
2003 if (!isl_val_is_rat(v))
2004 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2005 "expecting rational factor", goto error);
2006 if (!isl_val_is_pos(v))
2007 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2008 "factor needs to be positive", goto error);
2010 aff = isl_aff_scale(aff, v->d);
2011 aff = isl_aff_scale_down(aff, v->n);
2013 isl_val_free(v);
2014 return aff;
2015 error:
2016 isl_aff_free(aff);
2017 isl_val_free(v);
2018 return NULL;
2021 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2023 isl_int v;
2025 if (f == 1)
2026 return aff;
2028 isl_int_init(v);
2029 isl_int_set_ui(v, f);
2030 aff = isl_aff_scale_down(aff, v);
2031 isl_int_clear(v);
2033 return aff;
2036 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2037 enum isl_dim_type type, unsigned pos, const char *s)
2039 aff = isl_aff_cow(aff);
2040 if (!aff)
2041 return NULL;
2042 if (type == isl_dim_out)
2043 isl_die(aff->v->ctx, isl_error_invalid,
2044 "cannot set name of output/set dimension",
2045 return isl_aff_free(aff));
2046 if (type == isl_dim_in)
2047 type = isl_dim_set;
2048 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2049 if (!aff->ls)
2050 return isl_aff_free(aff);
2052 return aff;
2055 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2056 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2058 aff = isl_aff_cow(aff);
2059 if (!aff)
2060 goto error;
2061 if (type == isl_dim_out)
2062 isl_die(aff->v->ctx, isl_error_invalid,
2063 "cannot set name of output/set dimension",
2064 goto error);
2065 if (type == isl_dim_in)
2066 type = isl_dim_set;
2067 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2068 if (!aff->ls)
2069 return isl_aff_free(aff);
2071 return aff;
2072 error:
2073 isl_id_free(id);
2074 isl_aff_free(aff);
2075 return NULL;
2078 /* Replace the identifier of the input tuple of "aff" by "id".
2079 * type is currently required to be equal to isl_dim_in
2081 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2082 enum isl_dim_type type, __isl_take isl_id *id)
2084 aff = isl_aff_cow(aff);
2085 if (!aff)
2086 goto error;
2087 if (type != isl_dim_in)
2088 isl_die(aff->v->ctx, isl_error_invalid,
2089 "cannot only set id of input tuple", goto error);
2090 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2091 if (!aff->ls)
2092 return isl_aff_free(aff);
2094 return aff;
2095 error:
2096 isl_id_free(id);
2097 isl_aff_free(aff);
2098 return NULL;
2101 /* Exploit the equalities in "eq" to simplify the affine expression
2102 * and the expressions of the integer divisions in the local space.
2103 * The integer divisions in this local space are assumed to appear
2104 * as regular dimensions in "eq".
2106 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2107 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2109 int i, j;
2110 unsigned o_div;
2111 unsigned n_div;
2113 if (!eq)
2114 goto error;
2115 if (eq->n_eq == 0) {
2116 isl_basic_set_free(eq);
2117 return aff;
2120 aff = isl_aff_cow(aff);
2121 if (!aff)
2122 goto error;
2124 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2125 isl_basic_set_copy(eq));
2126 aff->v = isl_vec_cow(aff->v);
2127 if (!aff->ls || !aff->v)
2128 goto error;
2130 o_div = isl_basic_set_offset(eq, isl_dim_div);
2131 n_div = eq->n_div;
2132 for (i = 0; i < eq->n_eq; ++i) {
2133 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2134 if (j < 0 || j == 0 || j >= o_div)
2135 continue;
2137 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2138 &aff->v->el[0]);
2141 isl_basic_set_free(eq);
2142 aff = isl_aff_normalize(aff);
2143 return aff;
2144 error:
2145 isl_basic_set_free(eq);
2146 isl_aff_free(aff);
2147 return NULL;
2150 /* Exploit the equalities in "eq" to simplify the affine expression
2151 * and the expressions of the integer divisions in the local space.
2153 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2154 __isl_take isl_basic_set *eq)
2156 isl_size n_div;
2158 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2159 if (n_div < 0)
2160 goto error;
2161 if (n_div > 0)
2162 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2163 return isl_aff_substitute_equalities_lifted(aff, eq);
2164 error:
2165 isl_basic_set_free(eq);
2166 isl_aff_free(aff);
2167 return NULL;
2170 /* Look for equalities among the variables shared by context and aff
2171 * and the integer divisions of aff, if any.
2172 * The equalities are then used to eliminate coefficients and/or integer
2173 * divisions from aff.
2175 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2176 __isl_take isl_set *context)
2178 isl_local_space *ls;
2179 isl_basic_set *hull;
2181 ls = isl_aff_get_domain_local_space(aff);
2182 context = isl_local_space_lift_set(ls, context);
2184 hull = isl_set_affine_hull(context);
2185 return isl_aff_substitute_equalities_lifted(aff, hull);
2188 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2189 __isl_take isl_set *context)
2191 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2192 dom_context = isl_set_intersect_params(dom_context, context);
2193 return isl_aff_gist(aff, dom_context);
2196 /* Return a basic set containing those elements in the space
2197 * of aff where it is positive. "rational" should not be set.
2199 * If "aff" is NaN, then it is not positive.
2201 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2202 int rational, void *user)
2204 isl_constraint *ineq;
2205 isl_basic_set *bset;
2206 isl_val *c;
2208 if (!aff)
2209 return NULL;
2210 if (isl_aff_is_nan(aff)) {
2211 isl_space *space = isl_aff_get_domain_space(aff);
2212 isl_aff_free(aff);
2213 return isl_basic_set_empty(space);
2215 if (rational)
2216 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2217 "rational sets not supported", goto error);
2219 ineq = isl_inequality_from_aff(aff);
2220 c = isl_constraint_get_constant_val(ineq);
2221 c = isl_val_sub_ui(c, 1);
2222 ineq = isl_constraint_set_constant_val(ineq, c);
2224 bset = isl_basic_set_from_constraint(ineq);
2225 bset = isl_basic_set_simplify(bset);
2226 return bset;
2227 error:
2228 isl_aff_free(aff);
2229 return NULL;
2232 /* Return a basic set containing those elements in the space
2233 * of aff where it is non-negative.
2234 * If "rational" is set, then return a rational basic set.
2236 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2238 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2239 __isl_take isl_aff *aff, int rational, void *user)
2241 isl_constraint *ineq;
2242 isl_basic_set *bset;
2244 if (!aff)
2245 return NULL;
2246 if (isl_aff_is_nan(aff)) {
2247 isl_space *space = isl_aff_get_domain_space(aff);
2248 isl_aff_free(aff);
2249 return isl_basic_set_empty(space);
2252 ineq = isl_inequality_from_aff(aff);
2254 bset = isl_basic_set_from_constraint(ineq);
2255 if (rational)
2256 bset = isl_basic_set_set_rational(bset);
2257 bset = isl_basic_set_simplify(bset);
2258 return bset;
2261 /* Return a basic set containing those elements in the space
2262 * of aff where it is non-negative.
2264 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2266 return aff_nonneg_basic_set(aff, 0, NULL);
2269 /* Return a basic set containing those elements in the domain space
2270 * of "aff" where it is positive.
2272 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2274 aff = isl_aff_add_constant_num_si(aff, -1);
2275 return isl_aff_nonneg_basic_set(aff);
2278 /* Return a basic set containing those elements in the domain space
2279 * of aff where it is negative.
2281 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2283 aff = isl_aff_neg(aff);
2284 return isl_aff_pos_basic_set(aff);
2287 /* Return a basic set containing those elements in the space
2288 * of aff where it is zero.
2289 * If "rational" is set, then return a rational basic set.
2291 * If "aff" is NaN, then it is not zero.
2293 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2294 int rational, void *user)
2296 isl_constraint *ineq;
2297 isl_basic_set *bset;
2299 if (!aff)
2300 return NULL;
2301 if (isl_aff_is_nan(aff)) {
2302 isl_space *space = isl_aff_get_domain_space(aff);
2303 isl_aff_free(aff);
2304 return isl_basic_set_empty(space);
2307 ineq = isl_equality_from_aff(aff);
2309 bset = isl_basic_set_from_constraint(ineq);
2310 if (rational)
2311 bset = isl_basic_set_set_rational(bset);
2312 bset = isl_basic_set_simplify(bset);
2313 return bset;
2316 /* Return a basic set containing those elements in the space
2317 * of aff where it is zero.
2319 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2321 return aff_zero_basic_set(aff, 0, NULL);
2324 /* Return a basic set containing those elements in the shared space
2325 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2327 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2328 __isl_take isl_aff *aff2)
2330 aff1 = isl_aff_sub(aff1, aff2);
2332 return isl_aff_nonneg_basic_set(aff1);
2335 /* Return a basic set containing those elements in the shared domain space
2336 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2338 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2339 __isl_take isl_aff *aff2)
2341 aff1 = isl_aff_sub(aff1, aff2);
2343 return isl_aff_pos_basic_set(aff1);
2346 /* Return a set containing those elements in the shared space
2347 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2349 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2350 __isl_take isl_aff *aff2)
2352 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2355 /* Return a set containing those elements in the shared domain space
2356 * of aff1 and aff2 where aff1 is greater than aff2.
2358 * If either of the two inputs is NaN, then the result is empty,
2359 * as comparisons with NaN always return false.
2361 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2362 __isl_take isl_aff *aff2)
2364 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2367 /* Return a basic set containing those elements in the shared space
2368 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2370 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2371 __isl_take isl_aff *aff2)
2373 return isl_aff_ge_basic_set(aff2, aff1);
2376 /* Return a basic set containing those elements in the shared domain space
2377 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2379 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2380 __isl_take isl_aff *aff2)
2382 return isl_aff_gt_basic_set(aff2, aff1);
2385 /* Return a set containing those elements in the shared space
2386 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2388 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2389 __isl_take isl_aff *aff2)
2391 return isl_aff_ge_set(aff2, aff1);
2394 /* Return a set containing those elements in the shared domain space
2395 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2397 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2398 __isl_take isl_aff *aff2)
2400 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2403 /* Return a basic set containing those elements in the shared space
2404 * of aff1 and aff2 where aff1 and aff2 are equal.
2406 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2407 __isl_take isl_aff *aff2)
2409 aff1 = isl_aff_sub(aff1, aff2);
2411 return isl_aff_zero_basic_set(aff1);
2414 /* Return a set containing those elements in the shared space
2415 * of aff1 and aff2 where aff1 and aff2 are equal.
2417 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2418 __isl_take isl_aff *aff2)
2420 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2423 /* Return a set containing those elements in the shared domain space
2424 * of aff1 and aff2 where aff1 and aff2 are not equal.
2426 * If either of the two inputs is NaN, then the result is empty,
2427 * as comparisons with NaN always return false.
2429 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2430 __isl_take isl_aff *aff2)
2432 isl_set *set_lt, *set_gt;
2434 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2435 isl_aff_copy(aff2));
2436 set_gt = isl_aff_gt_set(aff1, aff2);
2437 return isl_set_union_disjoint(set_lt, set_gt);
2440 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2441 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2443 aff1 = isl_aff_add(aff1, aff2);
2444 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2445 return aff1;
2448 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2450 if (!aff)
2451 return isl_bool_error;
2453 return isl_bool_false;
2456 #undef TYPE
2457 #define TYPE isl_aff
2458 static
2459 #include "check_type_range_templ.c"
2461 /* Check whether the given affine expression has non-zero coefficient
2462 * for any dimension in the given range or if any of these dimensions
2463 * appear with non-zero coefficients in any of the integer divisions
2464 * involved in the affine expression.
2466 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2467 enum isl_dim_type type, unsigned first, unsigned n)
2469 int i;
2470 int *active = NULL;
2471 isl_bool involves = isl_bool_false;
2473 if (!aff)
2474 return isl_bool_error;
2475 if (n == 0)
2476 return isl_bool_false;
2477 if (isl_aff_check_range(aff, type, first, n) < 0)
2478 return isl_bool_error;
2480 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2481 if (!active)
2482 goto error;
2484 first += isl_local_space_offset(aff->ls, type) - 1;
2485 for (i = 0; i < n; ++i)
2486 if (active[first + i]) {
2487 involves = isl_bool_true;
2488 break;
2491 free(active);
2493 return involves;
2494 error:
2495 free(active);
2496 return isl_bool_error;
2499 /* Does "aff" involve any local variables, i.e., integer divisions?
2501 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2503 isl_size n;
2505 n = isl_aff_dim(aff, isl_dim_div);
2506 if (n < 0)
2507 return isl_bool_error;
2508 return isl_bool_ok(n > 0);
2511 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2512 enum isl_dim_type type, unsigned first, unsigned n)
2514 isl_ctx *ctx;
2516 if (!aff)
2517 return NULL;
2518 if (type == isl_dim_out)
2519 isl_die(aff->v->ctx, isl_error_invalid,
2520 "cannot drop output/set dimension",
2521 return isl_aff_free(aff));
2522 if (type == isl_dim_in)
2523 type = isl_dim_set;
2524 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2525 return aff;
2527 ctx = isl_aff_get_ctx(aff);
2528 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2529 return isl_aff_free(aff);
2531 aff = isl_aff_cow(aff);
2532 if (!aff)
2533 return NULL;
2535 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2536 if (!aff->ls)
2537 return isl_aff_free(aff);
2539 first += 1 + isl_local_space_offset(aff->ls, type);
2540 aff->v = isl_vec_drop_els(aff->v, first, n);
2541 if (!aff->v)
2542 return isl_aff_free(aff);
2544 return aff;
2547 /* Is the domain of "aff" a product?
2549 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2551 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2554 #undef TYPE
2555 #define TYPE isl_aff
2556 #include <isl_domain_factor_templ.c>
2558 /* Project the domain of the affine expression onto its parameter space.
2559 * The affine expression may not involve any of the domain dimensions.
2561 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2563 isl_space *space;
2564 isl_size n;
2566 n = isl_aff_dim(aff, isl_dim_in);
2567 if (n < 0)
2568 return isl_aff_free(aff);
2569 aff = isl_aff_drop_domain(aff, 0, n);
2570 space = isl_aff_get_domain_space(aff);
2571 space = isl_space_params(space);
2572 aff = isl_aff_reset_domain_space(aff, space);
2573 return aff;
2576 /* Convert an affine expression defined over a parameter domain
2577 * into one that is defined over a zero-dimensional set.
2579 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2581 isl_local_space *ls;
2583 ls = isl_aff_take_domain_local_space(aff);
2584 ls = isl_local_space_set_from_params(ls);
2585 aff = isl_aff_restore_domain_local_space(aff, ls);
2587 return aff;
2590 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2591 enum isl_dim_type type, unsigned first, unsigned n)
2593 isl_ctx *ctx;
2595 if (!aff)
2596 return NULL;
2597 if (type == isl_dim_out)
2598 isl_die(aff->v->ctx, isl_error_invalid,
2599 "cannot insert output/set dimensions",
2600 return isl_aff_free(aff));
2601 if (type == isl_dim_in)
2602 type = isl_dim_set;
2603 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2604 return aff;
2606 ctx = isl_aff_get_ctx(aff);
2607 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2608 return isl_aff_free(aff);
2610 aff = isl_aff_cow(aff);
2611 if (!aff)
2612 return NULL;
2614 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2615 if (!aff->ls)
2616 return isl_aff_free(aff);
2618 first += 1 + isl_local_space_offset(aff->ls, type);
2619 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2620 if (!aff->v)
2621 return isl_aff_free(aff);
2623 return aff;
2626 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2627 enum isl_dim_type type, unsigned n)
2629 isl_size pos;
2631 pos = isl_aff_dim(aff, type);
2632 if (pos < 0)
2633 return isl_aff_free(aff);
2635 return isl_aff_insert_dims(aff, type, pos, n);
2638 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2639 * to dimensions of "dst_type" at "dst_pos".
2641 * We only support moving input dimensions to parameters and vice versa.
2643 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2644 enum isl_dim_type dst_type, unsigned dst_pos,
2645 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2647 unsigned g_dst_pos;
2648 unsigned g_src_pos;
2649 isl_size src_off, dst_off;
2651 if (!aff)
2652 return NULL;
2653 if (n == 0 &&
2654 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2655 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2656 return aff;
2658 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2659 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2660 "cannot move output/set dimension",
2661 return isl_aff_free(aff));
2662 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2663 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2664 "cannot move divs", return isl_aff_free(aff));
2665 if (dst_type == isl_dim_in)
2666 dst_type = isl_dim_set;
2667 if (src_type == isl_dim_in)
2668 src_type = isl_dim_set;
2670 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2671 return isl_aff_free(aff);
2672 if (dst_type == src_type)
2673 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2674 "moving dims within the same type not supported",
2675 return isl_aff_free(aff));
2677 aff = isl_aff_cow(aff);
2678 src_off = isl_aff_domain_offset(aff, src_type);
2679 dst_off = isl_aff_domain_offset(aff, dst_type);
2680 if (src_off < 0 || dst_off < 0)
2681 return isl_aff_free(aff);
2683 g_src_pos = 1 + src_off + src_pos;
2684 g_dst_pos = 1 + dst_off + dst_pos;
2685 if (dst_type > src_type)
2686 g_dst_pos -= n;
2688 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2689 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2690 src_type, src_pos, n);
2691 if (!aff->v || !aff->ls)
2692 return isl_aff_free(aff);
2694 aff = sort_divs(aff);
2696 return aff;
2699 /* Return a zero isl_aff in the given space.
2701 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2702 * interface over all piecewise types.
2704 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2706 isl_local_space *ls;
2708 ls = isl_local_space_from_space(isl_space_domain(space));
2709 return isl_aff_zero_on_domain(ls);
2712 #define isl_aff_involves_nan isl_aff_is_nan
2714 #undef PW
2715 #define PW isl_pw_aff
2716 #undef BASE
2717 #define BASE aff
2718 #undef EL_IS_ZERO
2719 #define EL_IS_ZERO is_empty
2720 #undef ZERO
2721 #define ZERO empty
2722 #undef IS_ZERO
2723 #define IS_ZERO is_empty
2724 #undef FIELD
2725 #define FIELD aff
2726 #undef DEFAULT_IS_ZERO
2727 #define DEFAULT_IS_ZERO 0
2729 #include <isl_pw_templ.c>
2730 #include <isl_pw_add_constant_val_templ.c>
2731 #include <isl_pw_bind_domain_templ.c>
2732 #include <isl_pw_eval.c>
2733 #include <isl_pw_hash.c>
2734 #include <isl_pw_insert_dims_templ.c>
2735 #include <isl_pw_insert_domain_templ.c>
2736 #include <isl_pw_move_dims_templ.c>
2737 #include <isl_pw_neg_templ.c>
2738 #include <isl_pw_pullback_templ.c>
2739 #include <isl_pw_sub_templ.c>
2740 #include <isl_pw_union_opt.c>
2742 #undef BASE
2743 #define BASE pw_aff
2745 #include <isl_union_single.c>
2746 #include <isl_union_neg.c>
2748 #undef BASE
2749 #define BASE aff
2751 #include <isl_union_pw_templ.c>
2753 /* Compute a piecewise quasi-affine expression with a domain that
2754 * is the union of those of pwaff1 and pwaff2 and such that on each
2755 * cell, the quasi-affine expression is the maximum of those of pwaff1
2756 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2757 * cell, then the associated expression is the defined one.
2759 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2760 __isl_take isl_pw_aff *pwaff2)
2762 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2763 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2766 /* Compute a piecewise quasi-affine expression with a domain that
2767 * is the union of those of pwaff1 and pwaff2 and such that on each
2768 * cell, the quasi-affine expression is the minimum of those of pwaff1
2769 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2770 * cell, then the associated expression is the defined one.
2772 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2773 __isl_take isl_pw_aff *pwaff2)
2775 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2776 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2779 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2780 __isl_take isl_pw_aff *pwaff2, int max)
2782 if (max)
2783 return isl_pw_aff_union_max(pwaff1, pwaff2);
2784 else
2785 return isl_pw_aff_union_min(pwaff1, pwaff2);
2788 /* Is the domain of "pa" a product?
2790 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2792 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2795 #undef TYPE
2796 #define TYPE isl_pw_aff
2797 #include <isl_domain_factor_templ.c>
2799 /* Return a set containing those elements in the domain
2800 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2801 * does not satisfy "fn" (if complement is 1).
2803 * The pieces with a NaN never belong to the result since
2804 * NaN does not satisfy any property.
2806 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2807 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2808 void *user),
2809 int complement, void *user)
2811 int i;
2812 isl_set *set;
2814 if (!pwaff)
2815 return NULL;
2817 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2819 for (i = 0; i < pwaff->n; ++i) {
2820 isl_basic_set *bset;
2821 isl_set *set_i, *locus;
2822 isl_bool rational;
2824 if (isl_aff_is_nan(pwaff->p[i].aff))
2825 continue;
2827 rational = isl_set_has_rational(pwaff->p[i].set);
2828 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2829 locus = isl_set_from_basic_set(bset);
2830 set_i = isl_set_copy(pwaff->p[i].set);
2831 if (complement)
2832 set_i = isl_set_subtract(set_i, locus);
2833 else
2834 set_i = isl_set_intersect(set_i, locus);
2835 set = isl_set_union_disjoint(set, set_i);
2838 isl_pw_aff_free(pwaff);
2840 return set;
2843 /* Return a set containing those elements in the domain
2844 * of "pa" where it is positive.
2846 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2848 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2851 /* Return a set containing those elements in the domain
2852 * of pwaff where it is non-negative.
2854 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2856 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2859 /* Return a set containing those elements in the domain
2860 * of pwaff where it is zero.
2862 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2864 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2867 /* Return a set containing those elements in the domain
2868 * of pwaff where it is not zero.
2870 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2872 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2875 /* Bind the affine function "aff" to the parameter "id",
2876 * returning the elements in the domain where the affine expression
2877 * is equal to the parameter.
2879 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2880 __isl_take isl_id *id)
2882 isl_space *space;
2883 isl_aff *aff_id;
2885 space = isl_aff_get_domain_space(aff);
2886 space = isl_space_add_param_id(space, isl_id_copy(id));
2888 aff = isl_aff_align_params(aff, isl_space_copy(space));
2889 aff_id = isl_aff_param_on_domain_space_id(space, id);
2891 return isl_aff_eq_basic_set(aff, aff_id);
2894 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2895 * "rational" should not be set.
2897 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2898 int rational, void *user)
2900 isl_id *id = user;
2902 if (!aff)
2903 return NULL;
2904 if (rational)
2905 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2906 "rational binding not supported", goto error);
2907 return isl_aff_bind_id(aff, isl_id_copy(id));
2908 error:
2909 isl_aff_free(aff);
2910 return NULL;
2913 /* Bind the piecewise affine function "pa" to the parameter "id",
2914 * returning the elements in the domain where the expression
2915 * is equal to the parameter.
2917 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2918 __isl_take isl_id *id)
2920 isl_set *bound;
2922 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2923 isl_id_free(id);
2925 return bound;
2928 /* Return a set containing those elements in the shared domain
2929 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2931 * We compute the difference on the shared domain and then construct
2932 * the set of values where this difference is non-negative.
2933 * If strict is set, we first subtract 1 from the difference.
2934 * If equal is set, we only return the elements where pwaff1 and pwaff2
2935 * are equal.
2937 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2938 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2940 isl_set *set1, *set2;
2942 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2943 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2944 set1 = isl_set_intersect(set1, set2);
2945 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2946 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2947 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2949 if (strict) {
2950 isl_space *space = isl_set_get_space(set1);
2951 isl_aff *aff;
2952 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2953 aff = isl_aff_add_constant_si(aff, -1);
2954 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2955 } else
2956 isl_set_free(set1);
2958 if (equal)
2959 return isl_pw_aff_zero_set(pwaff1);
2960 return isl_pw_aff_nonneg_set(pwaff1);
2963 /* Return a set containing those elements in the shared domain
2964 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2966 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2967 __isl_take isl_pw_aff *pwaff2)
2969 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2970 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2973 /* Return a set containing those elements in the shared domain
2974 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2976 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2977 __isl_take isl_pw_aff *pwaff2)
2979 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2980 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2983 /* Return a set containing those elements in the shared domain
2984 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2986 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2987 __isl_take isl_pw_aff *pwaff2)
2989 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2990 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
2993 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
2994 __isl_take isl_pw_aff *pwaff2)
2996 return isl_pw_aff_ge_set(pwaff2, pwaff1);
2999 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3000 __isl_take isl_pw_aff *pwaff2)
3002 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3005 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3006 * where the function values are ordered in the same way as "order",
3007 * which returns a set in the shared domain of its two arguments.
3009 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3010 * We first pull back the two functions such that they are defined on
3011 * the domain [A -> B]. Then we apply "order", resulting in a set
3012 * in the space [A -> B]. Finally, we unwrap this set to obtain
3013 * a map in the space A -> B.
3015 static __isl_give isl_map *isl_pw_aff_order_map(
3016 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3017 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3018 __isl_take isl_pw_aff *pa2))
3020 isl_space *space1, *space2;
3021 isl_multi_aff *ma;
3022 isl_set *set;
3024 isl_pw_aff_align_params_bin(&pa1, &pa2);
3025 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3026 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3027 space1 = isl_space_map_from_domain_and_range(space1, space2);
3028 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3029 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3030 ma = isl_multi_aff_range_map(space1);
3031 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3032 set = order(pa1, pa2);
3034 return isl_set_unwrap(set);
3037 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3038 * where the function values are equal.
3040 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3041 __isl_take isl_pw_aff *pa2)
3043 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3046 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3047 * where the function value of "pa1" is less than or equal to
3048 * the function value of "pa2".
3050 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3051 __isl_take isl_pw_aff *pa2)
3053 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3056 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3057 * where the function value of "pa1" is less than the function value of "pa2".
3059 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3060 __isl_take isl_pw_aff *pa2)
3062 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3065 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3066 * where the function value of "pa1" is greater than or equal to
3067 * the function value of "pa2".
3069 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3070 __isl_take isl_pw_aff *pa2)
3072 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3075 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3076 * where the function value of "pa1" is greater than the function value
3077 * of "pa2".
3079 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3080 __isl_take isl_pw_aff *pa2)
3082 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3085 /* Return a set containing those elements in the shared domain
3086 * of the elements of list1 and list2 where each element in list1
3087 * has the relation specified by "fn" with each element in list2.
3089 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3090 __isl_take isl_pw_aff_list *list2,
3091 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3092 __isl_take isl_pw_aff *pwaff2))
3094 int i, j;
3095 isl_ctx *ctx;
3096 isl_set *set;
3098 if (!list1 || !list2)
3099 goto error;
3101 ctx = isl_pw_aff_list_get_ctx(list1);
3102 if (list1->n < 1 || list2->n < 1)
3103 isl_die(ctx, isl_error_invalid,
3104 "list should contain at least one element", goto error);
3106 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3107 for (i = 0; i < list1->n; ++i)
3108 for (j = 0; j < list2->n; ++j) {
3109 isl_set *set_ij;
3111 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3112 isl_pw_aff_copy(list2->p[j]));
3113 set = isl_set_intersect(set, set_ij);
3116 isl_pw_aff_list_free(list1);
3117 isl_pw_aff_list_free(list2);
3118 return set;
3119 error:
3120 isl_pw_aff_list_free(list1);
3121 isl_pw_aff_list_free(list2);
3122 return NULL;
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 equal to each element in list2.
3129 __isl_give isl_set *isl_pw_aff_list_eq_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_eq_set);
3135 __isl_give isl_set *isl_pw_aff_list_ne_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_ne_set);
3141 /* Return a set containing those elements in the shared domain
3142 * of the elements of list1 and list2 where each element in list1
3143 * is less than or equal to each element in list2.
3145 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3146 __isl_take isl_pw_aff_list *list2)
3148 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3151 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3152 __isl_take isl_pw_aff_list *list2)
3154 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3157 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3158 __isl_take isl_pw_aff_list *list2)
3160 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3163 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3164 __isl_take isl_pw_aff_list *list2)
3166 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3170 /* Return a set containing those elements in the shared domain
3171 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3173 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3174 __isl_take isl_pw_aff *pwaff2)
3176 isl_set *set_lt, *set_gt;
3178 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3179 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3180 isl_pw_aff_copy(pwaff2));
3181 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3182 return isl_set_union_disjoint(set_lt, set_gt);
3185 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3186 isl_int v)
3188 int i;
3190 if (isl_int_is_one(v))
3191 return pwaff;
3192 if (!isl_int_is_pos(v))
3193 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3194 "factor needs to be positive",
3195 return isl_pw_aff_free(pwaff));
3196 pwaff = isl_pw_aff_cow(pwaff);
3197 if (!pwaff)
3198 return NULL;
3199 if (pwaff->n == 0)
3200 return pwaff;
3202 for (i = 0; i < pwaff->n; ++i) {
3203 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3204 if (!pwaff->p[i].aff)
3205 return isl_pw_aff_free(pwaff);
3208 return pwaff;
3211 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3213 int i;
3215 pwaff = isl_pw_aff_cow(pwaff);
3216 if (!pwaff)
3217 return NULL;
3218 if (pwaff->n == 0)
3219 return pwaff;
3221 for (i = 0; i < pwaff->n; ++i) {
3222 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3223 if (!pwaff->p[i].aff)
3224 return isl_pw_aff_free(pwaff);
3227 return pwaff;
3230 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3232 int i;
3234 pwaff = isl_pw_aff_cow(pwaff);
3235 if (!pwaff)
3236 return NULL;
3237 if (pwaff->n == 0)
3238 return pwaff;
3240 for (i = 0; i < pwaff->n; ++i) {
3241 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3242 if (!pwaff->p[i].aff)
3243 return isl_pw_aff_free(pwaff);
3246 return pwaff;
3249 /* Assuming that "cond1" and "cond2" are disjoint,
3250 * return an affine expression that is equal to pwaff1 on cond1
3251 * and to pwaff2 on cond2.
3253 static __isl_give isl_pw_aff *isl_pw_aff_select(
3254 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3255 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3257 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3258 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3260 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3263 /* Return an affine expression that is equal to pwaff_true for elements
3264 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3265 * is zero.
3266 * That is, return cond ? pwaff_true : pwaff_false;
3268 * If "cond" involves and NaN, then we conservatively return a NaN
3269 * on its entire domain. In principle, we could consider the pieces
3270 * where it is NaN separately from those where it is not.
3272 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3273 * then only use the domain of "cond" to restrict the domain.
3275 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3276 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3278 isl_set *cond_true, *cond_false;
3279 isl_bool equal;
3281 if (!cond)
3282 goto error;
3283 if (isl_pw_aff_involves_nan(cond)) {
3284 isl_space *space = isl_pw_aff_get_domain_space(cond);
3285 isl_local_space *ls = isl_local_space_from_space(space);
3286 isl_pw_aff_free(cond);
3287 isl_pw_aff_free(pwaff_true);
3288 isl_pw_aff_free(pwaff_false);
3289 return isl_pw_aff_nan_on_domain(ls);
3292 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3293 isl_pw_aff_get_space(pwaff_false));
3294 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3295 isl_pw_aff_get_space(pwaff_true));
3296 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3297 if (equal < 0)
3298 goto error;
3299 if (equal) {
3300 isl_set *dom;
3302 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3303 isl_pw_aff_free(pwaff_false);
3304 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3307 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3308 cond_false = isl_pw_aff_zero_set(cond);
3309 return isl_pw_aff_select(cond_true, pwaff_true,
3310 cond_false, pwaff_false);
3311 error:
3312 isl_pw_aff_free(cond);
3313 isl_pw_aff_free(pwaff_true);
3314 isl_pw_aff_free(pwaff_false);
3315 return NULL;
3318 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3320 int pos;
3322 if (!aff)
3323 return isl_bool_error;
3325 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3326 return isl_bool_ok(pos == -1);
3329 /* Check whether pwaff is a piecewise constant.
3331 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3333 int i;
3335 if (!pwaff)
3336 return isl_bool_error;
3338 for (i = 0; i < pwaff->n; ++i) {
3339 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3340 if (is_cst < 0 || !is_cst)
3341 return is_cst;
3344 return isl_bool_true;
3347 /* Return the product of "aff1" and "aff2".
3349 * If either of the two is NaN, then the result is NaN.
3351 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3353 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3354 __isl_take isl_aff *aff2)
3356 if (!aff1 || !aff2)
3357 goto error;
3359 if (isl_aff_is_nan(aff1)) {
3360 isl_aff_free(aff2);
3361 return aff1;
3363 if (isl_aff_is_nan(aff2)) {
3364 isl_aff_free(aff1);
3365 return aff2;
3368 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3369 return isl_aff_mul(aff2, aff1);
3371 if (!isl_aff_is_cst(aff2))
3372 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3373 "at least one affine expression should be constant",
3374 goto error);
3376 aff1 = isl_aff_cow(aff1);
3377 if (!aff1 || !aff2)
3378 goto error;
3380 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3381 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3383 isl_aff_free(aff2);
3384 return aff1;
3385 error:
3386 isl_aff_free(aff1);
3387 isl_aff_free(aff2);
3388 return NULL;
3391 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3393 * If either of the two is NaN, then the result is NaN.
3394 * A division by zero also results in NaN.
3396 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3397 __isl_take isl_aff *aff2)
3399 isl_bool is_cst, is_zero;
3400 int neg;
3402 if (!aff1 || !aff2)
3403 goto error;
3405 if (isl_aff_is_nan(aff1)) {
3406 isl_aff_free(aff2);
3407 return aff1;
3409 if (isl_aff_is_nan(aff2)) {
3410 isl_aff_free(aff1);
3411 return aff2;
3414 is_cst = isl_aff_is_cst(aff2);
3415 if (is_cst < 0)
3416 goto error;
3417 if (!is_cst)
3418 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3419 "second argument should be a constant", goto error);
3420 is_zero = isl_aff_plain_is_zero(aff2);
3421 if (is_zero < 0)
3422 goto error;
3423 if (is_zero)
3424 return set_nan_free(aff1, aff2);
3426 neg = isl_int_is_neg(aff2->v->el[1]);
3427 if (neg) {
3428 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3429 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3432 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3433 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3435 if (neg) {
3436 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3437 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3440 isl_aff_free(aff2);
3441 return aff1;
3442 error:
3443 isl_aff_free(aff1);
3444 isl_aff_free(aff2);
3445 return NULL;
3448 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3449 __isl_take isl_pw_aff *pwaff2)
3451 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3452 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3455 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3456 __isl_take isl_pw_aff *pwaff2)
3458 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3461 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3462 __isl_take isl_pw_aff *pwaff2)
3464 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3465 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3468 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3470 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3471 __isl_take isl_pw_aff *pa2)
3473 int is_cst;
3475 is_cst = isl_pw_aff_is_cst(pa2);
3476 if (is_cst < 0)
3477 goto error;
3478 if (!is_cst)
3479 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3480 "second argument should be a piecewise constant",
3481 goto error);
3482 isl_pw_aff_align_params_bin(&pa1, &pa2);
3483 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3484 error:
3485 isl_pw_aff_free(pa1);
3486 isl_pw_aff_free(pa2);
3487 return NULL;
3490 /* Compute the quotient of the integer division of "pa1" by "pa2"
3491 * with rounding towards zero.
3492 * "pa2" is assumed to be a piecewise constant.
3494 * In particular, return
3496 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3499 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3500 __isl_take isl_pw_aff *pa2)
3502 int is_cst;
3503 isl_set *cond;
3504 isl_pw_aff *f, *c;
3506 is_cst = isl_pw_aff_is_cst(pa2);
3507 if (is_cst < 0)
3508 goto error;
3509 if (!is_cst)
3510 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3511 "second argument should be a piecewise constant",
3512 goto error);
3514 pa1 = isl_pw_aff_div(pa1, pa2);
3516 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3517 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3518 c = isl_pw_aff_ceil(pa1);
3519 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3520 error:
3521 isl_pw_aff_free(pa1);
3522 isl_pw_aff_free(pa2);
3523 return NULL;
3526 /* Compute the remainder of the integer division of "pa1" by "pa2"
3527 * with rounding towards zero.
3528 * "pa2" is assumed to be a piecewise constant.
3530 * In particular, return
3532 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3535 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3536 __isl_take isl_pw_aff *pa2)
3538 int is_cst;
3539 isl_pw_aff *res;
3541 is_cst = isl_pw_aff_is_cst(pa2);
3542 if (is_cst < 0)
3543 goto error;
3544 if (!is_cst)
3545 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3546 "second argument should be a piecewise constant",
3547 goto error);
3548 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3549 res = isl_pw_aff_mul(pa2, res);
3550 res = isl_pw_aff_sub(pa1, res);
3551 return res;
3552 error:
3553 isl_pw_aff_free(pa1);
3554 isl_pw_aff_free(pa2);
3555 return NULL;
3558 /* Does either of "pa1" or "pa2" involve any NaN2?
3560 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3561 __isl_keep isl_pw_aff *pa2)
3563 isl_bool has_nan;
3565 has_nan = isl_pw_aff_involves_nan(pa1);
3566 if (has_nan < 0 || has_nan)
3567 return has_nan;
3568 return isl_pw_aff_involves_nan(pa2);
3571 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3572 * by a NaN on their shared domain.
3574 * In principle, the result could be refined to only being NaN
3575 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3577 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3578 __isl_take isl_pw_aff *pa2)
3580 isl_local_space *ls;
3581 isl_set *dom;
3582 isl_pw_aff *pa;
3584 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3585 ls = isl_local_space_from_space(isl_set_get_space(dom));
3586 pa = isl_pw_aff_nan_on_domain(ls);
3587 pa = isl_pw_aff_intersect_domain(pa, dom);
3589 return pa;
3592 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3593 __isl_take isl_pw_aff *pwaff2)
3595 isl_set *le;
3596 isl_set *dom;
3598 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3599 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3600 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3601 isl_pw_aff_copy(pwaff2));
3602 dom = isl_set_subtract(dom, isl_set_copy(le));
3603 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3606 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3607 __isl_take isl_pw_aff *pwaff2)
3609 isl_set *ge;
3610 isl_set *dom;
3612 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3613 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3614 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3615 isl_pw_aff_copy(pwaff2));
3616 dom = isl_set_subtract(dom, isl_set_copy(ge));
3617 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3620 /* Return an expression for the minimum (if "max" is not set) or
3621 * the maximum (if "max" is set) of "pa1" and "pa2".
3622 * If either expression involves any NaN, then return a NaN
3623 * on the shared domain as result.
3625 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3626 __isl_take isl_pw_aff *pa2, int max)
3628 isl_bool has_nan;
3630 has_nan = either_involves_nan(pa1, pa2);
3631 if (has_nan < 0)
3632 pa1 = isl_pw_aff_free(pa1);
3633 else if (has_nan)
3634 return replace_by_nan(pa1, pa2);
3636 isl_pw_aff_align_params_bin(&pa1, &pa2);
3637 if (max)
3638 return pw_aff_max(pa1, pa2);
3639 else
3640 return pw_aff_min(pa1, pa2);
3643 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3645 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3646 __isl_take isl_pw_aff *pwaff2)
3648 return pw_aff_min_max(pwaff1, pwaff2, 0);
3651 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3653 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3654 __isl_take isl_pw_aff *pwaff2)
3656 return pw_aff_min_max(pwaff1, pwaff2, 1);
3659 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3660 __isl_take isl_pw_aff_list *list,
3661 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3662 __isl_take isl_pw_aff *pwaff2))
3664 int i;
3665 isl_ctx *ctx;
3666 isl_pw_aff *res;
3668 if (!list)
3669 return NULL;
3671 ctx = isl_pw_aff_list_get_ctx(list);
3672 if (list->n < 1)
3673 isl_die(ctx, isl_error_invalid,
3674 "list should contain at least one element", goto error);
3676 res = isl_pw_aff_copy(list->p[0]);
3677 for (i = 1; i < list->n; ++i)
3678 res = fn(res, isl_pw_aff_copy(list->p[i]));
3680 isl_pw_aff_list_free(list);
3681 return res;
3682 error:
3683 isl_pw_aff_list_free(list);
3684 return NULL;
3687 /* Return an isl_pw_aff that maps each element in the intersection of the
3688 * domains of the elements of list to the minimal corresponding affine
3689 * expression.
3691 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3693 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3696 /* Return an isl_pw_aff that maps each element in the intersection of the
3697 * domains of the elements of list to the maximal corresponding affine
3698 * expression.
3700 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3702 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3705 /* Mark the domains of "pwaff" as rational.
3707 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3709 int i;
3711 pwaff = isl_pw_aff_cow(pwaff);
3712 if (!pwaff)
3713 return NULL;
3714 if (pwaff->n == 0)
3715 return pwaff;
3717 for (i = 0; i < pwaff->n; ++i) {
3718 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3719 if (!pwaff->p[i].set)
3720 return isl_pw_aff_free(pwaff);
3723 return pwaff;
3726 /* Mark the domains of the elements of "list" as rational.
3728 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3729 __isl_take isl_pw_aff_list *list)
3731 int i, n;
3733 if (!list)
3734 return NULL;
3735 if (list->n == 0)
3736 return list;
3738 n = list->n;
3739 for (i = 0; i < n; ++i) {
3740 isl_pw_aff *pa;
3742 pa = isl_pw_aff_list_get_pw_aff(list, i);
3743 pa = isl_pw_aff_set_rational(pa);
3744 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3747 return list;
3750 /* Do the parameters of "aff" match those of "space"?
3752 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3753 __isl_keep isl_space *space)
3755 isl_space *aff_space;
3756 isl_bool match;
3758 if (!aff || !space)
3759 return isl_bool_error;
3761 aff_space = isl_aff_get_domain_space(aff);
3763 match = isl_space_has_equal_params(space, aff_space);
3765 isl_space_free(aff_space);
3766 return match;
3769 /* Check that the domain space of "aff" matches "space".
3771 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3772 __isl_keep isl_space *space)
3774 isl_space *aff_space;
3775 isl_bool match;
3777 if (!aff || !space)
3778 return isl_stat_error;
3780 aff_space = isl_aff_get_domain_space(aff);
3782 match = isl_space_has_equal_params(space, aff_space);
3783 if (match < 0)
3784 goto error;
3785 if (!match)
3786 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3787 "parameters don't match", goto error);
3788 match = isl_space_tuple_is_equal(space, isl_dim_in,
3789 aff_space, isl_dim_set);
3790 if (match < 0)
3791 goto error;
3792 if (!match)
3793 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3794 "domains don't match", goto error);
3795 isl_space_free(aff_space);
3796 return isl_stat_ok;
3797 error:
3798 isl_space_free(aff_space);
3799 return isl_stat_error;
3802 /* Return the shared (universe) domain of the elements of "ma".
3804 * Since an isl_multi_aff (and an isl_aff) is always total,
3805 * the domain is always the universe set in its domain space.
3806 * This is a helper function for use in the generic isl_multi_*_bind.
3808 static __isl_give isl_basic_set *isl_multi_aff_domain(
3809 __isl_take isl_multi_aff *ma)
3811 isl_space *space;
3813 space = isl_multi_aff_get_space(ma);
3814 isl_multi_aff_free(ma);
3816 return isl_basic_set_universe(isl_space_domain(space));
3819 #undef BASE
3820 #define BASE aff
3822 #include <isl_multi_no_explicit_domain.c>
3823 #include <isl_multi_templ.c>
3824 #include <isl_multi_add_constant_templ.c>
3825 #include <isl_multi_apply_set.c>
3826 #include <isl_multi_arith_templ.c>
3827 #include <isl_multi_bind_domain_templ.c>
3828 #include <isl_multi_cmp.c>
3829 #include <isl_multi_dim_id_templ.c>
3830 #include <isl_multi_dims.c>
3831 #include <isl_multi_floor.c>
3832 #include <isl_multi_from_base_templ.c>
3833 #include <isl_multi_identity_templ.c>
3834 #include <isl_multi_insert_domain_templ.c>
3835 #include <isl_multi_locals_templ.c>
3836 #include <isl_multi_move_dims_templ.c>
3837 #include <isl_multi_nan_templ.c>
3838 #include <isl_multi_product_templ.c>
3839 #include <isl_multi_splice_templ.c>
3840 #include <isl_multi_tuple_id_templ.c>
3841 #include <isl_multi_unbind_params_templ.c>
3842 #include <isl_multi_zero_templ.c>
3844 #undef DOMBASE
3845 #define DOMBASE set
3846 #include <isl_multi_gist.c>
3848 #undef DOMBASE
3849 #define DOMBASE basic_set
3850 #include <isl_multi_bind_templ.c>
3852 /* Construct an isl_multi_aff living in "space" that corresponds
3853 * to the affine transformation matrix "mat".
3855 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3856 __isl_take isl_space *space, __isl_take isl_mat *mat)
3858 isl_ctx *ctx;
3859 isl_local_space *ls = NULL;
3860 isl_multi_aff *ma = NULL;
3861 isl_size n_row, n_col, n_out, total;
3862 int i;
3864 if (!space || !mat)
3865 goto error;
3867 ctx = isl_mat_get_ctx(mat);
3869 n_row = isl_mat_rows(mat);
3870 n_col = isl_mat_cols(mat);
3871 n_out = isl_space_dim(space, isl_dim_out);
3872 total = isl_space_dim(space, isl_dim_all);
3873 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3874 goto error;
3875 if (n_row < 1)
3876 isl_die(ctx, isl_error_invalid,
3877 "insufficient number of rows", goto error);
3878 if (n_col < 1)
3879 isl_die(ctx, isl_error_invalid,
3880 "insufficient number of columns", goto error);
3881 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3882 isl_die(ctx, isl_error_invalid,
3883 "dimension mismatch", goto error);
3885 ma = isl_multi_aff_zero(isl_space_copy(space));
3886 space = isl_space_domain(space);
3887 ls = isl_local_space_from_space(isl_space_copy(space));
3889 for (i = 0; i < n_row - 1; ++i) {
3890 isl_vec *v;
3891 isl_aff *aff;
3893 v = isl_vec_alloc(ctx, 1 + n_col);
3894 if (!v)
3895 goto error;
3896 isl_int_set(v->el[0], mat->row[0][0]);
3897 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3898 v = isl_vec_normalize(v);
3899 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3900 ma = isl_multi_aff_set_aff(ma, i, aff);
3903 isl_space_free(space);
3904 isl_local_space_free(ls);
3905 isl_mat_free(mat);
3906 return ma;
3907 error:
3908 isl_space_free(space);
3909 isl_local_space_free(ls);
3910 isl_mat_free(mat);
3911 isl_multi_aff_free(ma);
3912 return NULL;
3915 /* Return the constant terms of the affine expressions of "ma".
3917 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3918 __isl_keep isl_multi_aff *ma)
3920 int i;
3921 isl_size n;
3922 isl_space *space;
3923 isl_multi_val *mv;
3925 n = isl_multi_aff_size(ma);
3926 if (n < 0)
3927 return NULL;
3928 space = isl_space_range(isl_multi_aff_get_space(ma));
3929 space = isl_space_drop_all_params(space);
3930 mv = isl_multi_val_zero(space);
3932 for (i = 0; i < n; ++i) {
3933 isl_aff *aff;
3934 isl_val *val;
3936 aff = isl_multi_aff_get_at(ma, i);
3937 val = isl_aff_get_constant_val(aff);
3938 isl_aff_free(aff);
3939 mv = isl_multi_val_set_at(mv, i, val);
3942 return mv;
3945 /* Remove any internal structure of the domain of "ma".
3946 * If there is any such internal structure in the input,
3947 * then the name of the corresponding space is also removed.
3949 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3950 __isl_take isl_multi_aff *ma)
3952 isl_space *space;
3954 if (!ma)
3955 return NULL;
3957 if (!ma->space->nested[0])
3958 return ma;
3960 space = isl_multi_aff_get_space(ma);
3961 space = isl_space_flatten_domain(space);
3962 ma = isl_multi_aff_reset_space(ma, space);
3964 return ma;
3967 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3968 * of the space to its domain.
3970 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3972 int i;
3973 isl_size n_in;
3974 isl_local_space *ls;
3975 isl_multi_aff *ma;
3977 if (!space)
3978 return NULL;
3979 if (!isl_space_is_map(space))
3980 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3981 "not a map space", goto error);
3983 n_in = isl_space_dim(space, isl_dim_in);
3984 if (n_in < 0)
3985 goto error;
3986 space = isl_space_domain_map(space);
3988 ma = isl_multi_aff_alloc(isl_space_copy(space));
3989 if (n_in == 0) {
3990 isl_space_free(space);
3991 return ma;
3994 space = isl_space_domain(space);
3995 ls = isl_local_space_from_space(space);
3996 for (i = 0; i < n_in; ++i) {
3997 isl_aff *aff;
3999 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4000 isl_dim_set, i);
4001 ma = isl_multi_aff_set_aff(ma, i, aff);
4003 isl_local_space_free(ls);
4004 return ma;
4005 error:
4006 isl_space_free(space);
4007 return NULL;
4010 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4011 * of the space to its range.
4013 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4015 int i;
4016 isl_size n_in, n_out;
4017 isl_local_space *ls;
4018 isl_multi_aff *ma;
4020 if (!space)
4021 return NULL;
4022 if (!isl_space_is_map(space))
4023 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4024 "not a map space", goto error);
4026 n_in = isl_space_dim(space, isl_dim_in);
4027 n_out = isl_space_dim(space, isl_dim_out);
4028 if (n_in < 0 || n_out < 0)
4029 goto error;
4030 space = isl_space_range_map(space);
4032 ma = isl_multi_aff_alloc(isl_space_copy(space));
4033 if (n_out == 0) {
4034 isl_space_free(space);
4035 return ma;
4038 space = isl_space_domain(space);
4039 ls = isl_local_space_from_space(space);
4040 for (i = 0; i < n_out; ++i) {
4041 isl_aff *aff;
4043 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4044 isl_dim_set, n_in + i);
4045 ma = isl_multi_aff_set_aff(ma, i, aff);
4047 isl_local_space_free(ls);
4048 return ma;
4049 error:
4050 isl_space_free(space);
4051 return NULL;
4054 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4055 * of the space to its domain.
4057 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_domain_map(
4058 __isl_take isl_space *space)
4060 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_domain_map(space));
4063 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4064 * of the space to its range.
4066 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4067 __isl_take isl_space *space)
4069 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4072 /* Given the space of a set and a range of set dimensions,
4073 * construct an isl_multi_aff that projects out those dimensions.
4075 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4076 __isl_take isl_space *space, enum isl_dim_type type,
4077 unsigned first, unsigned n)
4079 int i;
4080 isl_size dim;
4081 isl_local_space *ls;
4082 isl_multi_aff *ma;
4084 if (!space)
4085 return NULL;
4086 if (!isl_space_is_set(space))
4087 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4088 "expecting set space", goto error);
4089 if (type != isl_dim_set)
4090 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4091 "only set dimensions can be projected out", goto error);
4092 if (isl_space_check_range(space, type, first, n) < 0)
4093 goto error;
4095 dim = isl_space_dim(space, isl_dim_set);
4096 if (dim < 0)
4097 goto error;
4099 space = isl_space_from_domain(space);
4100 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4102 if (dim == n)
4103 return isl_multi_aff_alloc(space);
4105 ma = isl_multi_aff_alloc(isl_space_copy(space));
4106 space = isl_space_domain(space);
4107 ls = isl_local_space_from_space(space);
4109 for (i = 0; i < first; ++i) {
4110 isl_aff *aff;
4112 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4113 isl_dim_set, i);
4114 ma = isl_multi_aff_set_aff(ma, i, aff);
4117 for (i = 0; i < dim - (first + n); ++i) {
4118 isl_aff *aff;
4120 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4121 isl_dim_set, first + n + i);
4122 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4125 isl_local_space_free(ls);
4126 return ma;
4127 error:
4128 isl_space_free(space);
4129 return NULL;
4132 /* Given the space of a set and a range of set dimensions,
4133 * construct an isl_pw_multi_aff that projects out those dimensions.
4135 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4136 __isl_take isl_space *space, enum isl_dim_type type,
4137 unsigned first, unsigned n)
4139 isl_multi_aff *ma;
4141 ma = isl_multi_aff_project_out_map(space, type, first, n);
4142 return isl_pw_multi_aff_from_multi_aff(ma);
4145 /* Create a piecewise multi-affine expression in the given space that maps each
4146 * input dimension to the corresponding output dimension.
4148 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4149 __isl_take isl_space *space)
4151 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4154 /* Exploit the equalities in "eq" to simplify the affine expressions.
4156 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4157 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4159 int i;
4161 maff = isl_multi_aff_cow(maff);
4162 if (!maff || !eq)
4163 goto error;
4165 for (i = 0; i < maff->n; ++i) {
4166 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4167 isl_basic_set_copy(eq));
4168 if (!maff->u.p[i])
4169 goto error;
4172 isl_basic_set_free(eq);
4173 return maff;
4174 error:
4175 isl_basic_set_free(eq);
4176 isl_multi_aff_free(maff);
4177 return NULL;
4180 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4181 isl_int f)
4183 int i;
4185 maff = isl_multi_aff_cow(maff);
4186 if (!maff)
4187 return NULL;
4189 for (i = 0; i < maff->n; ++i) {
4190 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4191 if (!maff->u.p[i])
4192 return isl_multi_aff_free(maff);
4195 return maff;
4198 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4199 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4201 maff1 = isl_multi_aff_add(maff1, maff2);
4202 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4203 return maff1;
4206 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4208 if (!maff)
4209 return isl_bool_error;
4211 return isl_bool_false;
4214 /* Return the set of domain elements where "ma1" is lexicographically
4215 * smaller than or equal to "ma2".
4217 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4218 __isl_take isl_multi_aff *ma2)
4220 return isl_multi_aff_lex_ge_set(ma2, ma1);
4223 /* Return the set of domain elements where "ma1" is lexicographically
4224 * smaller than "ma2".
4226 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4227 __isl_take isl_multi_aff *ma2)
4229 return isl_multi_aff_lex_gt_set(ma2, ma1);
4232 /* Return the set of domain elements where "ma1" is lexicographically
4233 * greater than to "ma2". If "equal" is set, then include the domain
4234 * elements where they are equal.
4235 * Do this for the case where there are no entries.
4236 * In this case, "ma1" cannot be greater than "ma2",
4237 * but it is (greater than or) equal to "ma2".
4239 static __isl_give isl_set *isl_multi_aff_lex_gte_set_0d(
4240 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4242 isl_space *space;
4244 space = isl_multi_aff_get_domain_space(ma1);
4246 isl_multi_aff_free(ma1);
4247 isl_multi_aff_free(ma2);
4249 if (equal)
4250 return isl_set_universe(space);
4251 else
4252 return isl_set_empty(space);
4255 /* Return the set where entry "i" of "ma1" and "ma2"
4256 * satisfy the relation prescribed by "cmp".
4258 static __isl_give isl_set *isl_multi_aff_order_at(__isl_keep isl_multi_aff *ma1,
4259 __isl_keep isl_multi_aff *ma2, int i,
4260 __isl_give isl_set *(*cmp)(__isl_take isl_aff *aff1,
4261 __isl_take isl_aff *aff2))
4263 isl_aff *aff1, *aff2;
4265 aff1 = isl_multi_aff_get_at(ma1, i);
4266 aff2 = isl_multi_aff_get_at(ma2, i);
4267 return cmp(aff1, aff2);
4270 /* Return the set of domain elements where "ma1" is lexicographically
4271 * greater than to "ma2". If "equal" is set, then include the domain
4272 * elements where they are equal.
4274 * In particular, for all but the final entry,
4275 * include the set of elements where this entry is strictly greater in "ma1"
4276 * and all previous entries are equal.
4277 * The final entry is also allowed to be equal in the two functions
4278 * if "equal" is set.
4280 * The case where there are no entries is handled separately.
4282 static __isl_give isl_set *isl_multi_aff_lex_gte_set(
4283 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2, int equal)
4285 int i;
4286 isl_size n;
4287 isl_space *space;
4288 isl_set *res;
4289 isl_set *equal_set;
4290 isl_set *gte;
4292 if (isl_multi_aff_check_equal_space(ma1, ma2) < 0)
4293 goto error;
4294 n = isl_multi_aff_size(ma1);
4295 if (n < 0)
4296 goto error;
4297 if (n == 0)
4298 return isl_multi_aff_lex_gte_set_0d(ma1, ma2, equal);
4300 space = isl_multi_aff_get_domain_space(ma1);
4301 res = isl_set_empty(isl_space_copy(space));
4302 equal_set = isl_set_universe(space);
4304 for (i = 0; i + 1 < n; ++i) {
4305 isl_bool empty;
4306 isl_set *gt, *eq;
4308 gt = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_gt_set);
4309 gt = isl_set_intersect(gt, isl_set_copy(equal_set));
4310 res = isl_set_union(res, gt);
4311 eq = isl_multi_aff_order_at(ma1, ma2, i, &isl_aff_eq_set);
4312 equal_set = isl_set_intersect(equal_set, eq);
4314 empty = isl_set_is_empty(equal_set);
4315 if (empty >= 0 && empty)
4316 break;
4319 if (equal)
4320 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_ge_set);
4321 else
4322 gte = isl_multi_aff_order_at(ma1, ma2, n - 1, &isl_aff_gt_set);
4323 isl_multi_aff_free(ma1);
4324 isl_multi_aff_free(ma2);
4326 gte = isl_set_intersect(gte, equal_set);
4327 return isl_set_union(res, gte);
4328 error:
4329 isl_multi_aff_free(ma1);
4330 isl_multi_aff_free(ma2);
4331 return NULL;
4334 /* Return the set of domain elements where "ma1" is lexicographically
4335 * greater than or equal to "ma2".
4337 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4338 __isl_take isl_multi_aff *ma2)
4340 return isl_multi_aff_lex_gte_set(ma1, ma2, 1);
4343 /* Return the set of domain elements where "ma1" is lexicographically
4344 * greater than "ma2".
4346 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4347 __isl_take isl_multi_aff *ma2)
4349 return isl_multi_aff_lex_gte_set(ma1, ma2, 0);
4352 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4354 #undef PW
4355 #define PW isl_pw_multi_aff
4356 #undef BASE
4357 #define BASE multi_aff
4358 #undef EL_IS_ZERO
4359 #define EL_IS_ZERO is_empty
4360 #undef ZERO
4361 #define ZERO empty
4362 #undef IS_ZERO
4363 #define IS_ZERO is_empty
4364 #undef FIELD
4365 #define FIELD maff
4366 #undef DEFAULT_IS_ZERO
4367 #define DEFAULT_IS_ZERO 0
4369 #include <isl_pw_templ.c>
4370 #include <isl_pw_add_constant_multi_val_templ.c>
4371 #include <isl_pw_add_constant_val_templ.c>
4372 #include <isl_pw_bind_domain_templ.c>
4373 #include <isl_pw_insert_dims_templ.c>
4374 #include <isl_pw_insert_domain_templ.c>
4375 #include <isl_pw_locals_templ.c>
4376 #include <isl_pw_move_dims_templ.c>
4377 #include <isl_pw_neg_templ.c>
4378 #include <isl_pw_pullback_templ.c>
4379 #include <isl_pw_union_opt.c>
4381 #undef BASE
4382 #define BASE pw_multi_aff
4384 #include <isl_union_multi.c>
4385 #include "isl_union_locals_templ.c"
4386 #include <isl_union_neg.c>
4388 #undef BASE
4389 #define BASE multi_aff
4391 #include <isl_union_pw_templ.c>
4393 /* Generic function for extracting a factor from a product "pma".
4394 * "check_space" checks that the space is that of the right kind of product.
4395 * "space_factor" extracts the factor from the space.
4396 * "multi_aff_factor" extracts the factor from the constituent functions.
4398 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4399 __isl_take isl_pw_multi_aff *pma,
4400 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4401 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4402 __isl_give isl_multi_aff *(*multi_aff_factor)(
4403 __isl_take isl_multi_aff *ma))
4405 int i;
4406 isl_space *space;
4408 if (check_space(pma) < 0)
4409 return isl_pw_multi_aff_free(pma);
4411 space = isl_pw_multi_aff_take_space(pma);
4412 space = space_factor(space);
4414 for (i = 0; pma && i < pma->n; ++i) {
4415 isl_multi_aff *ma;
4417 ma = isl_pw_multi_aff_take_base_at(pma, i);
4418 ma = multi_aff_factor(ma);
4419 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4422 pma = isl_pw_multi_aff_restore_space(pma, space);
4424 return pma;
4427 /* Is the range of "pma" a wrapped relation?
4429 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4430 __isl_keep isl_pw_multi_aff *pma)
4432 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4435 /* Check that the range of "pma" is a product.
4437 static isl_stat pw_multi_aff_check_range_product(
4438 __isl_keep isl_pw_multi_aff *pma)
4440 isl_bool wraps;
4442 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4443 if (wraps < 0)
4444 return isl_stat_error;
4445 if (!wraps)
4446 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4447 "range is not a product", return isl_stat_error);
4448 return isl_stat_ok;
4451 /* Given a function A -> [B -> C], extract the function A -> B.
4453 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4454 __isl_take isl_pw_multi_aff *pma)
4456 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4457 &isl_space_range_factor_domain,
4458 &isl_multi_aff_range_factor_domain);
4461 /* Given a function A -> [B -> C], extract the function A -> C.
4463 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4464 __isl_take isl_pw_multi_aff *pma)
4466 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4467 &isl_space_range_factor_range,
4468 &isl_multi_aff_range_factor_range);
4471 /* Given two piecewise multi affine expressions, return a piecewise
4472 * multi-affine expression defined on the union of the definition domains
4473 * of the inputs that is equal to the lexicographic maximum of the two
4474 * inputs on each cell. If only one of the two inputs is defined on
4475 * a given cell, then it is considered to be the maximum.
4477 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4478 __isl_take isl_pw_multi_aff *pma1,
4479 __isl_take isl_pw_multi_aff *pma2)
4481 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4482 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4483 &isl_multi_aff_lex_ge_set);
4486 /* Given two piecewise multi affine expressions, return a piecewise
4487 * multi-affine expression defined on the union of the definition domains
4488 * of the inputs that is equal to the lexicographic minimum of the two
4489 * inputs on each cell. If only one of the two inputs is defined on
4490 * a given cell, then it is considered to be the minimum.
4492 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4493 __isl_take isl_pw_multi_aff *pma1,
4494 __isl_take isl_pw_multi_aff *pma2)
4496 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4497 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4498 &isl_multi_aff_lex_le_set);
4501 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4502 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4504 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4505 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4506 &isl_multi_aff_add);
4509 /* Subtract "pma2" from "pma1" and return the result.
4511 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4512 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4514 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4515 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4516 &isl_multi_aff_sub);
4519 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4520 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4522 return isl_pw_multi_aff_union_add_(pma1, pma2);
4525 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4526 * with the actual sum on the shared domain and
4527 * the defined expression on the symmetric difference of the domains.
4529 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4530 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4532 return isl_union_pw_aff_union_add_(upa1, upa2);
4535 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4536 * with the actual sum on the shared domain and
4537 * the defined expression on the symmetric difference of the domains.
4539 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4540 __isl_take isl_union_pw_multi_aff *upma1,
4541 __isl_take isl_union_pw_multi_aff *upma2)
4543 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4546 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4547 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4549 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4550 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4552 int i, j, n;
4553 isl_space *space;
4554 isl_pw_multi_aff *res;
4556 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4557 goto error;
4559 n = pma1->n * pma2->n;
4560 space = isl_space_product(isl_space_copy(pma1->dim),
4561 isl_space_copy(pma2->dim));
4562 res = isl_pw_multi_aff_alloc_size(space, n);
4564 for (i = 0; i < pma1->n; ++i) {
4565 for (j = 0; j < pma2->n; ++j) {
4566 isl_set *domain;
4567 isl_multi_aff *ma;
4569 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4570 isl_set_copy(pma2->p[j].set));
4571 ma = isl_multi_aff_product(
4572 isl_multi_aff_copy(pma1->p[i].maff),
4573 isl_multi_aff_copy(pma2->p[j].maff));
4574 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4578 isl_pw_multi_aff_free(pma1);
4579 isl_pw_multi_aff_free(pma2);
4580 return res;
4581 error:
4582 isl_pw_multi_aff_free(pma1);
4583 isl_pw_multi_aff_free(pma2);
4584 return NULL;
4587 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4588 * denominator "denom".
4589 * "denom" is allowed to be negative, in which case the actual denominator
4590 * is -denom and the expressions are added instead.
4592 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4593 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4595 int i, first;
4596 int sign;
4597 isl_int d;
4599 first = isl_seq_first_non_zero(c, n);
4600 if (first == -1)
4601 return aff;
4603 sign = isl_int_sgn(denom);
4604 isl_int_init(d);
4605 isl_int_abs(d, denom);
4606 for (i = first; i < n; ++i) {
4607 isl_aff *aff_i;
4609 if (isl_int_is_zero(c[i]))
4610 continue;
4611 aff_i = isl_multi_aff_get_aff(ma, i);
4612 aff_i = isl_aff_scale(aff_i, c[i]);
4613 aff_i = isl_aff_scale_down(aff_i, d);
4614 if (sign >= 0)
4615 aff = isl_aff_sub(aff, aff_i);
4616 else
4617 aff = isl_aff_add(aff, aff_i);
4619 isl_int_clear(d);
4621 return aff;
4624 /* Extract an affine expression that expresses the output dimension "pos"
4625 * of "bmap" in terms of the parameters and input dimensions from
4626 * equality "eq".
4627 * Note that this expression may involve integer divisions defined
4628 * in terms of parameters and input dimensions.
4629 * The equality may also involve references to earlier (but not later)
4630 * output dimensions. These are replaced by the corresponding elements
4631 * in "ma".
4633 * If the equality is of the form
4635 * f(i) + h(j) + a x + g(i) = 0,
4637 * with f(i) a linear combinations of the parameters and input dimensions,
4638 * g(i) a linear combination of integer divisions defined in terms of the same
4639 * and h(j) a linear combinations of earlier output dimensions,
4640 * then the affine expression is
4642 * (-f(i) - g(i))/a - h(j)/a
4644 * If the equality is of the form
4646 * f(i) + h(j) - a x + g(i) = 0,
4648 * then the affine expression is
4650 * (f(i) + g(i))/a - h(j)/(-a)
4653 * If "div" refers to an integer division (i.e., it is smaller than
4654 * the number of integer divisions), then the equality constraint
4655 * does involve an integer division (the one at position "div") that
4656 * is defined in terms of output dimensions. However, this integer
4657 * division can be eliminated by exploiting a pair of constraints
4658 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4659 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4660 * -l + x >= 0.
4661 * In particular, let
4663 * x = e(i) + m floor(...)
4665 * with e(i) the expression derived above and floor(...) the integer
4666 * division involving output dimensions.
4667 * From
4669 * l <= x <= l + n,
4671 * we have
4673 * 0 <= x - l <= n
4675 * This means
4677 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4678 * = (e(i) - l) mod m
4680 * Therefore,
4682 * x - l = (e(i) - l) mod m
4684 * or
4686 * x = ((e(i) - l) mod m) + l
4688 * The variable "shift" below contains the expression -l, which may
4689 * also involve a linear combination of earlier output dimensions.
4691 static __isl_give isl_aff *extract_aff_from_equality(
4692 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4693 __isl_keep isl_multi_aff *ma)
4695 unsigned o_out;
4696 isl_size n_div, n_out;
4697 isl_ctx *ctx;
4698 isl_local_space *ls;
4699 isl_aff *aff, *shift;
4700 isl_val *mod;
4702 ctx = isl_basic_map_get_ctx(bmap);
4703 ls = isl_basic_map_get_local_space(bmap);
4704 ls = isl_local_space_domain(ls);
4705 aff = isl_aff_alloc(isl_local_space_copy(ls));
4706 if (!aff)
4707 goto error;
4708 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4709 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4710 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4711 if (n_out < 0 || n_div < 0)
4712 goto error;
4713 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4714 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4715 isl_seq_cpy(aff->v->el + 1 + o_out,
4716 bmap->eq[eq] + o_out + n_out, n_div);
4717 } else {
4718 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4719 isl_seq_neg(aff->v->el + 1 + o_out,
4720 bmap->eq[eq] + o_out + n_out, n_div);
4722 if (div < n_div)
4723 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4724 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4725 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4726 bmap->eq[eq][o_out + pos]);
4727 if (div < n_div) {
4728 shift = isl_aff_alloc(isl_local_space_copy(ls));
4729 if (!shift)
4730 goto error;
4731 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4732 isl_seq_cpy(shift->v->el + 1 + o_out,
4733 bmap->ineq[ineq] + o_out + n_out, n_div);
4734 isl_int_set_si(shift->v->el[0], 1);
4735 shift = subtract_initial(shift, ma, pos,
4736 bmap->ineq[ineq] + o_out, ctx->negone);
4737 aff = isl_aff_add(aff, isl_aff_copy(shift));
4738 mod = isl_val_int_from_isl_int(ctx,
4739 bmap->eq[eq][o_out + n_out + div]);
4740 mod = isl_val_abs(mod);
4741 aff = isl_aff_mod_val(aff, mod);
4742 aff = isl_aff_sub(aff, shift);
4745 isl_local_space_free(ls);
4746 return aff;
4747 error:
4748 isl_local_space_free(ls);
4749 isl_aff_free(aff);
4750 return NULL;
4753 /* Given a basic map with output dimensions defined
4754 * in terms of the parameters input dimensions and earlier
4755 * output dimensions using an equality (and possibly a pair on inequalities),
4756 * extract an isl_aff that expresses output dimension "pos" in terms
4757 * of the parameters and input dimensions.
4758 * Note that this expression may involve integer divisions defined
4759 * in terms of parameters and input dimensions.
4760 * "ma" contains the expressions corresponding to earlier output dimensions.
4762 * This function shares some similarities with
4763 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4765 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4766 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4768 int eq, div, ineq;
4769 isl_aff *aff;
4771 if (!bmap)
4772 return NULL;
4773 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4774 if (eq >= bmap->n_eq)
4775 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4776 "unable to find suitable equality", return NULL);
4777 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4779 aff = isl_aff_remove_unused_divs(aff);
4780 return aff;
4783 /* Given a basic map where each output dimension is defined
4784 * in terms of the parameters and input dimensions using an equality,
4785 * extract an isl_multi_aff that expresses the output dimensions in terms
4786 * of the parameters and input dimensions.
4788 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4789 __isl_take isl_basic_map *bmap)
4791 int i;
4792 isl_size n_out;
4793 isl_multi_aff *ma;
4795 if (!bmap)
4796 return NULL;
4798 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4799 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4800 if (n_out < 0)
4801 ma = isl_multi_aff_free(ma);
4803 for (i = 0; i < n_out; ++i) {
4804 isl_aff *aff;
4806 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4807 ma = isl_multi_aff_set_aff(ma, i, aff);
4810 isl_basic_map_free(bmap);
4812 return ma;
4815 /* Given a basic set where each set dimension is defined
4816 * in terms of the parameters using an equality,
4817 * extract an isl_multi_aff that expresses the set dimensions in terms
4818 * of the parameters.
4820 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4821 __isl_take isl_basic_set *bset)
4823 return extract_isl_multi_aff_from_basic_map(bset);
4826 /* Create an isl_pw_multi_aff that is equivalent to
4827 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4828 * The given basic map is such that each output dimension is defined
4829 * in terms of the parameters and input dimensions using an equality.
4831 * Since some applications expect the result of isl_pw_multi_aff_from_map
4832 * to only contain integer affine expressions, we compute the floor
4833 * of the expression before returning.
4835 * Remove all constraints involving local variables without
4836 * an explicit representation (resulting in the removal of those
4837 * local variables) prior to the actual extraction to ensure
4838 * that the local spaces in which the resulting affine expressions
4839 * are created do not contain any unknown local variables.
4840 * Removing such constraints is safe because constraints involving
4841 * unknown local variables are not used to determine whether
4842 * a basic map is obviously single-valued.
4844 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4845 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4847 isl_multi_aff *ma;
4849 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4850 ma = extract_isl_multi_aff_from_basic_map(bmap);
4851 ma = isl_multi_aff_floor(ma);
4852 return isl_pw_multi_aff_alloc(domain, ma);
4855 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4856 * This obviously only works if the input "map" is single-valued.
4857 * If so, we compute the lexicographic minimum of the image in the form
4858 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4859 * to its lexicographic minimum.
4860 * If the input is not single-valued, we produce an error.
4862 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4863 __isl_take isl_map *map)
4865 int i;
4866 int sv;
4867 isl_pw_multi_aff *pma;
4869 sv = isl_map_is_single_valued(map);
4870 if (sv < 0)
4871 goto error;
4872 if (!sv)
4873 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4874 "map is not single-valued", goto error);
4875 map = isl_map_make_disjoint(map);
4876 if (!map)
4877 return NULL;
4879 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4881 for (i = 0; i < map->n; ++i) {
4882 isl_pw_multi_aff *pma_i;
4883 isl_basic_map *bmap;
4884 bmap = isl_basic_map_copy(map->p[i]);
4885 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4886 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4889 isl_map_free(map);
4890 return pma;
4891 error:
4892 isl_map_free(map);
4893 return NULL;
4896 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4897 * taking into account that the output dimension at position "d"
4898 * can be represented as
4900 * x = floor((e(...) + c1) / m)
4902 * given that constraint "i" is of the form
4904 * e(...) + c1 - m x >= 0
4907 * Let "map" be of the form
4909 * A -> B
4911 * We construct a mapping
4913 * A -> [A -> x = floor(...)]
4915 * apply that to the map, obtaining
4917 * [A -> x = floor(...)] -> B
4919 * and equate dimension "d" to x.
4920 * We then compute a isl_pw_multi_aff representation of the resulting map
4921 * and plug in the mapping above.
4923 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4924 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4926 isl_ctx *ctx;
4927 isl_space *space = NULL;
4928 isl_local_space *ls;
4929 isl_multi_aff *ma;
4930 isl_aff *aff;
4931 isl_vec *v;
4932 isl_map *insert;
4933 int offset;
4934 isl_size n;
4935 isl_size n_in;
4936 isl_pw_multi_aff *pma;
4937 isl_bool is_set;
4939 is_set = isl_map_is_set(map);
4940 if (is_set < 0)
4941 goto error;
4943 offset = isl_basic_map_offset(hull, isl_dim_out);
4944 ctx = isl_map_get_ctx(map);
4945 space = isl_space_domain(isl_map_get_space(map));
4946 n_in = isl_space_dim(space, isl_dim_set);
4947 n = isl_space_dim(space, isl_dim_all);
4948 if (n_in < 0 || n < 0)
4949 goto error;
4951 v = isl_vec_alloc(ctx, 1 + 1 + n);
4952 if (v) {
4953 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4954 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4956 isl_basic_map_free(hull);
4958 ls = isl_local_space_from_space(isl_space_copy(space));
4959 aff = isl_aff_alloc_vec(ls, v);
4960 aff = isl_aff_floor(aff);
4961 if (is_set) {
4962 isl_space_free(space);
4963 ma = isl_multi_aff_from_aff(aff);
4964 } else {
4965 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4966 ma = isl_multi_aff_range_product(ma,
4967 isl_multi_aff_from_aff(aff));
4970 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4971 map = isl_map_apply_domain(map, insert);
4972 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4973 pma = isl_pw_multi_aff_from_map(map);
4974 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4976 return pma;
4977 error:
4978 isl_space_free(space);
4979 isl_map_free(map);
4980 isl_basic_map_free(hull);
4981 return NULL;
4984 /* Is constraint "c" of the form
4986 * e(...) + c1 - m x >= 0
4988 * or
4990 * -e(...) + c2 + m x >= 0
4992 * where m > 1 and e only depends on parameters and input dimemnsions?
4994 * "offset" is the offset of the output dimensions
4995 * "pos" is the position of output dimension x.
4997 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4999 if (isl_int_is_zero(c[offset + d]))
5000 return 0;
5001 if (isl_int_is_one(c[offset + d]))
5002 return 0;
5003 if (isl_int_is_negone(c[offset + d]))
5004 return 0;
5005 if (isl_seq_first_non_zero(c + offset, d) != -1)
5006 return 0;
5007 if (isl_seq_first_non_zero(c + offset + d + 1,
5008 total - (offset + d + 1)) != -1)
5009 return 0;
5010 return 1;
5013 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5015 * As a special case, we first check if there is any pair of constraints,
5016 * shared by all the basic maps in "map" that force a given dimension
5017 * to be equal to the floor of some affine combination of the input dimensions.
5019 * In particular, if we can find two constraints
5021 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
5023 * and
5025 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
5027 * where m > 1 and e only depends on parameters and input dimemnsions,
5028 * and such that
5030 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
5032 * then we know that we can take
5034 * x = floor((e(...) + c1) / m)
5036 * without having to perform any computation.
5038 * Note that we know that
5040 * c1 + c2 >= 1
5042 * If c1 + c2 were 0, then we would have detected an equality during
5043 * simplification. If c1 + c2 were negative, then we would have detected
5044 * a contradiction.
5046 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
5047 __isl_take isl_map *map)
5049 int d;
5050 isl_size dim;
5051 int i, j, n;
5052 int offset;
5053 isl_size total;
5054 isl_int sum;
5055 isl_basic_map *hull;
5057 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5058 dim = isl_map_dim(map, isl_dim_out);
5059 total = isl_basic_map_dim(hull, isl_dim_all);
5060 if (dim < 0 || total < 0)
5061 goto error;
5063 isl_int_init(sum);
5064 offset = isl_basic_map_offset(hull, isl_dim_out);
5065 n = hull->n_ineq;
5066 for (d = 0; d < dim; ++d) {
5067 for (i = 0; i < n; ++i) {
5068 if (!is_potential_div_constraint(hull->ineq[i],
5069 offset, d, 1 + total))
5070 continue;
5071 for (j = i + 1; j < n; ++j) {
5072 if (!isl_seq_is_neg(hull->ineq[i] + 1,
5073 hull->ineq[j] + 1, total))
5074 continue;
5075 isl_int_add(sum, hull->ineq[i][0],
5076 hull->ineq[j][0]);
5077 if (isl_int_abs_lt(sum,
5078 hull->ineq[i][offset + d]))
5079 break;
5082 if (j >= n)
5083 continue;
5084 isl_int_clear(sum);
5085 if (isl_int_is_pos(hull->ineq[j][offset + d]))
5086 j = i;
5087 return pw_multi_aff_from_map_div(map, hull, d, j);
5090 isl_int_clear(sum);
5091 isl_basic_map_free(hull);
5092 return pw_multi_aff_from_map_base(map);
5093 error:
5094 isl_map_free(map);
5095 isl_basic_map_free(hull);
5096 return NULL;
5099 /* Given an affine expression
5101 * [A -> B] -> f(A,B)
5103 * construct an isl_multi_aff
5105 * [A -> B] -> B'
5107 * such that dimension "d" in B' is set to "aff" and the remaining
5108 * dimensions are set equal to the corresponding dimensions in B.
5109 * "n_in" is the dimension of the space A.
5110 * "n_out" is the dimension of the space B.
5112 * If "is_set" is set, then the affine expression is of the form
5114 * [B] -> f(B)
5116 * and we construct an isl_multi_aff
5118 * B -> B'
5120 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5121 unsigned n_in, unsigned n_out, int is_set)
5123 int i;
5124 isl_multi_aff *ma;
5125 isl_space *space, *space2;
5126 isl_local_space *ls;
5128 space = isl_aff_get_domain_space(aff);
5129 ls = isl_local_space_from_space(isl_space_copy(space));
5130 space2 = isl_space_copy(space);
5131 if (!is_set)
5132 space2 = isl_space_range(isl_space_unwrap(space2));
5133 space = isl_space_map_from_domain_and_range(space, space2);
5134 ma = isl_multi_aff_alloc(space);
5135 ma = isl_multi_aff_set_aff(ma, d, aff);
5137 for (i = 0; i < n_out; ++i) {
5138 if (i == d)
5139 continue;
5140 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5141 isl_dim_set, n_in + i);
5142 ma = isl_multi_aff_set_aff(ma, i, aff);
5145 isl_local_space_free(ls);
5147 return ma;
5150 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5151 * taking into account that the dimension at position "d" can be written as
5153 * x = m a + f(..) (1)
5155 * where m is equal to "gcd".
5156 * "i" is the index of the equality in "hull" that defines f(..).
5157 * In particular, the equality is of the form
5159 * f(..) - x + m g(existentials) = 0
5161 * or
5163 * -f(..) + x + m g(existentials) = 0
5165 * We basically plug (1) into "map", resulting in a map with "a"
5166 * in the range instead of "x". The corresponding isl_pw_multi_aff
5167 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5169 * Specifically, given the input map
5171 * A -> B
5173 * We first wrap it into a set
5175 * [A -> B]
5177 * and define (1) on top of the corresponding space, resulting in "aff".
5178 * We use this to create an isl_multi_aff that maps the output position "d"
5179 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5180 * We plug this into the wrapped map, unwrap the result and compute the
5181 * corresponding isl_pw_multi_aff.
5182 * The result is an expression
5184 * A -> T(A)
5186 * We adjust that to
5188 * A -> [A -> T(A)]
5190 * so that we can plug that into "aff", after extending the latter to
5191 * a mapping
5193 * [A -> B] -> B'
5196 * If "map" is actually a set, then there is no "A" space, meaning
5197 * that we do not need to perform any wrapping, and that the result
5198 * of the recursive call is of the form
5200 * [T]
5202 * which is plugged into a mapping of the form
5204 * B -> B'
5206 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5207 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5208 isl_int gcd)
5210 isl_set *set;
5211 isl_space *space;
5212 isl_local_space *ls;
5213 isl_aff *aff;
5214 isl_multi_aff *ma;
5215 isl_pw_multi_aff *pma, *id;
5216 isl_size n_in;
5217 unsigned o_out;
5218 isl_size n_out;
5219 isl_bool is_set;
5221 is_set = isl_map_is_set(map);
5222 if (is_set < 0)
5223 goto error;
5225 n_in = isl_basic_map_dim(hull, isl_dim_in);
5226 n_out = isl_basic_map_dim(hull, isl_dim_out);
5227 if (n_in < 0 || n_out < 0)
5228 goto error;
5229 o_out = isl_basic_map_offset(hull, isl_dim_out);
5231 if (is_set)
5232 set = map;
5233 else
5234 set = isl_map_wrap(map);
5235 space = isl_space_map_from_set(isl_set_get_space(set));
5236 ma = isl_multi_aff_identity(space);
5237 ls = isl_local_space_from_space(isl_set_get_space(set));
5238 aff = isl_aff_alloc(ls);
5239 if (aff) {
5240 isl_int_set_si(aff->v->el[0], 1);
5241 if (isl_int_is_one(hull->eq[i][o_out + d]))
5242 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5243 aff->v->size - 1);
5244 else
5245 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5246 aff->v->size - 1);
5247 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5249 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5250 set = isl_set_preimage_multi_aff(set, ma);
5252 ma = range_map(aff, d, n_in, n_out, is_set);
5254 if (is_set)
5255 map = set;
5256 else
5257 map = isl_set_unwrap(set);
5258 pma = isl_pw_multi_aff_from_map(map);
5260 if (!is_set) {
5261 space = isl_pw_multi_aff_get_domain_space(pma);
5262 space = isl_space_map_from_set(space);
5263 id = isl_pw_multi_aff_identity(space);
5264 pma = isl_pw_multi_aff_range_product(id, pma);
5266 id = isl_pw_multi_aff_from_multi_aff(ma);
5267 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5269 isl_basic_map_free(hull);
5270 return pma;
5271 error:
5272 isl_map_free(map);
5273 isl_basic_map_free(hull);
5274 return NULL;
5277 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5278 * "hull" contains the equalities valid for "map".
5280 * Check if any of the output dimensions is "strided".
5281 * That is, we check if it can be written as
5283 * x = m a + f(..)
5285 * with m greater than 1, a some combination of existentially quantified
5286 * variables and f an expression in the parameters and input dimensions.
5287 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5289 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5290 * special case.
5292 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5293 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5295 int i, j;
5296 isl_size n_out;
5297 unsigned o_out;
5298 isl_size n_div;
5299 unsigned o_div;
5300 isl_int gcd;
5302 n_div = isl_basic_map_dim(hull, isl_dim_div);
5303 n_out = isl_basic_map_dim(hull, isl_dim_out);
5304 if (n_div < 0 || n_out < 0)
5305 goto error;
5307 if (n_div == 0) {
5308 isl_basic_map_free(hull);
5309 return pw_multi_aff_from_map_check_div(map);
5312 isl_int_init(gcd);
5314 o_div = isl_basic_map_offset(hull, isl_dim_div);
5315 o_out = isl_basic_map_offset(hull, isl_dim_out);
5317 for (i = 0; i < n_out; ++i) {
5318 for (j = 0; j < hull->n_eq; ++j) {
5319 isl_int *eq = hull->eq[j];
5320 isl_pw_multi_aff *res;
5322 if (!isl_int_is_one(eq[o_out + i]) &&
5323 !isl_int_is_negone(eq[o_out + i]))
5324 continue;
5325 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5326 continue;
5327 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5328 n_out - (i + 1)) != -1)
5329 continue;
5330 isl_seq_gcd(eq + o_div, n_div, &gcd);
5331 if (isl_int_is_zero(gcd))
5332 continue;
5333 if (isl_int_is_one(gcd))
5334 continue;
5336 res = pw_multi_aff_from_map_stride(map, hull,
5337 i, j, gcd);
5338 isl_int_clear(gcd);
5339 return res;
5343 isl_int_clear(gcd);
5344 isl_basic_map_free(hull);
5345 return pw_multi_aff_from_map_check_div(map);
5346 error:
5347 isl_map_free(map);
5348 isl_basic_map_free(hull);
5349 return NULL;
5352 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5354 * As a special case, we first check if all output dimensions are uniquely
5355 * defined in terms of the parameters and input dimensions over the entire
5356 * domain. If so, we extract the desired isl_pw_multi_aff directly
5357 * from the affine hull of "map" and its domain.
5359 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5360 * special cases.
5362 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5364 isl_bool sv;
5365 isl_size n;
5366 isl_basic_map *hull;
5368 n = isl_map_n_basic_map(map);
5369 if (n < 0)
5370 goto error;
5372 if (n == 1) {
5373 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5374 hull = isl_basic_map_plain_affine_hull(hull);
5375 sv = isl_basic_map_plain_is_single_valued(hull);
5376 if (sv >= 0 && sv)
5377 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5378 hull);
5379 isl_basic_map_free(hull);
5381 map = isl_map_detect_equalities(map);
5382 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5383 sv = isl_basic_map_plain_is_single_valued(hull);
5384 if (sv >= 0 && sv)
5385 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5386 if (sv >= 0)
5387 return pw_multi_aff_from_map_check_strides(map, hull);
5388 isl_basic_map_free(hull);
5389 error:
5390 isl_map_free(map);
5391 return NULL;
5394 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5396 return isl_pw_multi_aff_from_map(set);
5399 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5400 * add it to *user.
5402 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5404 isl_union_pw_multi_aff **upma = user;
5405 isl_pw_multi_aff *pma;
5407 pma = isl_pw_multi_aff_from_map(map);
5408 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5410 return *upma ? isl_stat_ok : isl_stat_error;
5413 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5414 * domain.
5416 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5417 __isl_take isl_aff *aff)
5419 isl_multi_aff *ma;
5420 isl_pw_multi_aff *pma;
5422 ma = isl_multi_aff_from_aff(aff);
5423 pma = isl_pw_multi_aff_from_multi_aff(ma);
5424 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5427 /* Try and create an isl_union_pw_multi_aff that is equivalent
5428 * to the given isl_union_map.
5429 * The isl_union_map is required to be single-valued in each space.
5430 * Otherwise, an error is produced.
5432 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5433 __isl_take isl_union_map *umap)
5435 isl_space *space;
5436 isl_union_pw_multi_aff *upma;
5438 space = isl_union_map_get_space(umap);
5439 upma = isl_union_pw_multi_aff_empty(space);
5440 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5441 upma = isl_union_pw_multi_aff_free(upma);
5442 isl_union_map_free(umap);
5444 return upma;
5447 /* Try and create an isl_union_pw_multi_aff that is equivalent
5448 * to the given isl_union_set.
5449 * The isl_union_set is required to be a singleton in each space.
5450 * Otherwise, an error is produced.
5452 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5453 __isl_take isl_union_set *uset)
5455 return isl_union_pw_multi_aff_from_union_map(uset);
5458 /* Return the piecewise affine expression "set ? 1 : 0".
5460 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5462 isl_pw_aff *pa;
5463 isl_space *space = isl_set_get_space(set);
5464 isl_local_space *ls = isl_local_space_from_space(space);
5465 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5466 isl_aff *one = isl_aff_zero_on_domain(ls);
5468 one = isl_aff_add_constant_si(one, 1);
5469 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5470 set = isl_set_complement(set);
5471 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5473 return pa;
5476 /* Plug in "subs" for dimension "type", "pos" of "aff".
5478 * Let i be the dimension to replace and let "subs" be of the form
5480 * f/d
5482 * and "aff" of the form
5484 * (a i + g)/m
5486 * The result is
5488 * (a f + d g')/(m d)
5490 * where g' is the result of plugging in "subs" in each of the integer
5491 * divisions in g.
5493 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5494 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5496 isl_ctx *ctx;
5497 isl_int v;
5498 isl_size n_div;
5500 aff = isl_aff_cow(aff);
5501 if (!aff || !subs)
5502 return isl_aff_free(aff);
5504 ctx = isl_aff_get_ctx(aff);
5505 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5506 isl_die(ctx, isl_error_invalid,
5507 "spaces don't match", return isl_aff_free(aff));
5508 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5509 if (n_div < 0)
5510 return isl_aff_free(aff);
5511 if (n_div != 0)
5512 isl_die(ctx, isl_error_unsupported,
5513 "cannot handle divs yet", return isl_aff_free(aff));
5515 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5516 if (!aff->ls)
5517 return isl_aff_free(aff);
5519 aff->v = isl_vec_cow(aff->v);
5520 if (!aff->v)
5521 return isl_aff_free(aff);
5523 pos += isl_local_space_offset(aff->ls, type);
5525 isl_int_init(v);
5526 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5527 aff->v->size, subs->v->size, v);
5528 isl_int_clear(v);
5530 return aff;
5533 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5534 * expressions in "maff".
5536 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5537 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5538 __isl_keep isl_aff *subs)
5540 int i;
5542 maff = isl_multi_aff_cow(maff);
5543 if (!maff || !subs)
5544 return isl_multi_aff_free(maff);
5546 if (type == isl_dim_in)
5547 type = isl_dim_set;
5549 for (i = 0; i < maff->n; ++i) {
5550 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5551 type, pos, subs);
5552 if (!maff->u.p[i])
5553 return isl_multi_aff_free(maff);
5556 return maff;
5559 /* Plug in "subs" for dimension "type", "pos" of "pma".
5561 * pma is of the form
5563 * A_i(v) -> M_i(v)
5565 * while subs is of the form
5567 * v' = B_j(v) -> S_j
5569 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5570 * has a contribution in the result, in particular
5572 * C_ij(S_j) -> M_i(S_j)
5574 * Note that plugging in S_j in C_ij may also result in an empty set
5575 * and this contribution should simply be discarded.
5577 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5578 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5579 __isl_keep isl_pw_aff *subs)
5581 int i, j, n;
5582 isl_pw_multi_aff *res;
5584 if (!pma || !subs)
5585 return isl_pw_multi_aff_free(pma);
5587 n = pma->n * subs->n;
5588 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5590 for (i = 0; i < pma->n; ++i) {
5591 for (j = 0; j < subs->n; ++j) {
5592 isl_set *common;
5593 isl_multi_aff *res_ij;
5594 int empty;
5596 common = isl_set_intersect(
5597 isl_set_copy(pma->p[i].set),
5598 isl_set_copy(subs->p[j].set));
5599 common = isl_set_substitute(common,
5600 type, pos, subs->p[j].aff);
5601 empty = isl_set_plain_is_empty(common);
5602 if (empty < 0 || empty) {
5603 isl_set_free(common);
5604 if (empty < 0)
5605 goto error;
5606 continue;
5609 res_ij = isl_multi_aff_substitute(
5610 isl_multi_aff_copy(pma->p[i].maff),
5611 type, pos, subs->p[j].aff);
5613 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5617 isl_pw_multi_aff_free(pma);
5618 return res;
5619 error:
5620 isl_pw_multi_aff_free(pma);
5621 isl_pw_multi_aff_free(res);
5622 return NULL;
5625 /* Compute the preimage of a range of dimensions in the affine expression "src"
5626 * under "ma" and put the result in "dst". The number of dimensions in "src"
5627 * that precede the range is given by "n_before". The number of dimensions
5628 * in the range is given by the number of output dimensions of "ma".
5629 * The number of dimensions that follow the range is given by "n_after".
5630 * If "has_denom" is set (to one),
5631 * then "src" and "dst" have an extra initial denominator.
5632 * "n_div_ma" is the number of existentials in "ma"
5633 * "n_div_bset" is the number of existentials in "src"
5634 * The resulting "dst" (which is assumed to have been allocated by
5635 * the caller) contains coefficients for both sets of existentials,
5636 * first those in "ma" and then those in "src".
5637 * f, c1, c2 and g are temporary objects that have been initialized
5638 * by the caller.
5640 * Let src represent the expression
5642 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5644 * and let ma represent the expressions
5646 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5648 * We start out with the following expression for dst:
5650 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5652 * with the multiplication factor f initially equal to 1
5653 * and f \sum_i b_i v_i kept separately.
5654 * For each x_i that we substitute, we multiply the numerator
5655 * (and denominator) of dst by c_1 = m_i and add the numerator
5656 * of the x_i expression multiplied by c_2 = f b_i,
5657 * after removing the common factors of c_1 and c_2.
5658 * The multiplication factor f also needs to be multiplied by c_1
5659 * for the next x_j, j > i.
5661 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5662 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5663 int n_div_ma, int n_div_bmap,
5664 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5666 int i;
5667 isl_size n_param, n_in, n_out;
5668 int o_dst, o_src;
5670 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5671 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5672 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5673 if (n_param < 0 || n_in < 0 || n_out < 0)
5674 return isl_stat_error;
5676 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5677 o_dst = o_src = has_denom + 1 + n_param + n_before;
5678 isl_seq_clr(dst + o_dst, n_in);
5679 o_dst += n_in;
5680 o_src += n_out;
5681 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5682 o_dst += n_after;
5683 o_src += n_after;
5684 isl_seq_clr(dst + o_dst, n_div_ma);
5685 o_dst += n_div_ma;
5686 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5688 isl_int_set_si(f, 1);
5690 for (i = 0; i < n_out; ++i) {
5691 int offset = has_denom + 1 + n_param + n_before + i;
5693 if (isl_int_is_zero(src[offset]))
5694 continue;
5695 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5696 isl_int_mul(c2, f, src[offset]);
5697 isl_int_gcd(g, c1, c2);
5698 isl_int_divexact(c1, c1, g);
5699 isl_int_divexact(c2, c2, g);
5701 isl_int_mul(f, f, c1);
5702 o_dst = has_denom;
5703 o_src = 1;
5704 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5705 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5706 o_dst += 1 + n_param;
5707 o_src += 1 + n_param;
5708 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5709 o_dst += n_before;
5710 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5711 c2, ma->u.p[i]->v->el + o_src, n_in);
5712 o_dst += n_in;
5713 o_src += n_in;
5714 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5715 o_dst += n_after;
5716 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5717 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5718 o_dst += n_div_ma;
5719 o_src += n_div_ma;
5720 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5721 if (has_denom)
5722 isl_int_mul(dst[0], dst[0], c1);
5725 return isl_stat_ok;
5728 /* Compute the pullback of "aff" by the function represented by "ma".
5729 * In other words, plug in "ma" in "aff". The result is an affine expression
5730 * defined over the domain space of "ma".
5732 * If "aff" is represented by
5734 * (a(p) + b x + c(divs))/d
5736 * and ma is represented by
5738 * x = D(p) + F(y) + G(divs')
5740 * then the result is
5742 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5744 * The divs in the local space of the input are similarly adjusted
5745 * through a call to isl_local_space_preimage_multi_aff.
5747 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5748 __isl_take isl_multi_aff *ma)
5750 isl_aff *res = NULL;
5751 isl_local_space *ls;
5752 isl_size n_div_aff, n_div_ma;
5753 isl_int f, c1, c2, g;
5755 ma = isl_multi_aff_align_divs(ma);
5756 if (!aff || !ma)
5757 goto error;
5759 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5760 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5761 if (n_div_aff < 0 || n_div_ma < 0)
5762 goto error;
5764 ls = isl_aff_get_domain_local_space(aff);
5765 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5766 res = isl_aff_alloc(ls);
5767 if (!res)
5768 goto error;
5770 isl_int_init(f);
5771 isl_int_init(c1);
5772 isl_int_init(c2);
5773 isl_int_init(g);
5775 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5776 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5777 res = isl_aff_free(res);
5779 isl_int_clear(f);
5780 isl_int_clear(c1);
5781 isl_int_clear(c2);
5782 isl_int_clear(g);
5784 isl_aff_free(aff);
5785 isl_multi_aff_free(ma);
5786 res = isl_aff_normalize(res);
5787 return res;
5788 error:
5789 isl_aff_free(aff);
5790 isl_multi_aff_free(ma);
5791 isl_aff_free(res);
5792 return NULL;
5795 /* Compute the pullback of "aff1" by the function represented by "aff2".
5796 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5797 * defined over the domain space of "aff1".
5799 * The domain of "aff1" should match the range of "aff2", which means
5800 * that it should be single-dimensional.
5802 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5803 __isl_take isl_aff *aff2)
5805 isl_multi_aff *ma;
5807 ma = isl_multi_aff_from_aff(aff2);
5808 return isl_aff_pullback_multi_aff(aff1, ma);
5811 /* Compute the pullback of "ma1" by the function represented by "ma2".
5812 * In other words, plug in "ma2" in "ma1".
5814 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5815 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5817 int i;
5818 isl_space *space = NULL;
5820 isl_multi_aff_align_params_bin(&ma1, &ma2);
5821 ma2 = isl_multi_aff_align_divs(ma2);
5822 ma1 = isl_multi_aff_cow(ma1);
5823 if (!ma1 || !ma2)
5824 goto error;
5826 space = isl_space_join(isl_multi_aff_get_space(ma2),
5827 isl_multi_aff_get_space(ma1));
5829 for (i = 0; i < ma1->n; ++i) {
5830 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5831 isl_multi_aff_copy(ma2));
5832 if (!ma1->u.p[i])
5833 goto error;
5836 ma1 = isl_multi_aff_reset_space(ma1, space);
5837 isl_multi_aff_free(ma2);
5838 return ma1;
5839 error:
5840 isl_space_free(space);
5841 isl_multi_aff_free(ma2);
5842 isl_multi_aff_free(ma1);
5843 return NULL;
5846 /* Extend the local space of "dst" to include the divs
5847 * in the local space of "src".
5849 * If "src" does not have any divs or if the local spaces of "dst" and
5850 * "src" are the same, then no extension is required.
5852 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5853 __isl_keep isl_aff *src)
5855 isl_ctx *ctx;
5856 isl_size src_n_div, dst_n_div;
5857 int *exp1 = NULL;
5858 int *exp2 = NULL;
5859 isl_bool equal;
5860 isl_mat *div;
5862 if (!src || !dst)
5863 return isl_aff_free(dst);
5865 ctx = isl_aff_get_ctx(src);
5866 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5867 if (equal < 0)
5868 return isl_aff_free(dst);
5869 if (!equal)
5870 isl_die(ctx, isl_error_invalid,
5871 "spaces don't match", goto error);
5873 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
5874 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
5875 if (src_n_div == 0)
5876 return dst;
5877 equal = isl_local_space_is_equal(src->ls, dst->ls);
5878 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5879 return isl_aff_free(dst);
5880 if (equal)
5881 return dst;
5883 exp1 = isl_alloc_array(ctx, int, src_n_div);
5884 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5885 if (!exp1 || (dst_n_div && !exp2))
5886 goto error;
5888 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5889 dst = isl_aff_expand_divs(dst, div, exp2);
5890 free(exp1);
5891 free(exp2);
5893 return dst;
5894 error:
5895 free(exp1);
5896 free(exp2);
5897 return isl_aff_free(dst);
5900 /* Adjust the local spaces of the affine expressions in "maff"
5901 * such that they all have the save divs.
5903 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5904 __isl_take isl_multi_aff *maff)
5906 int i;
5908 if (!maff)
5909 return NULL;
5910 if (maff->n == 0)
5911 return maff;
5912 maff = isl_multi_aff_cow(maff);
5913 if (!maff)
5914 return NULL;
5916 for (i = 1; i < maff->n; ++i)
5917 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5918 for (i = 1; i < maff->n; ++i) {
5919 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5920 if (!maff->u.p[i])
5921 return isl_multi_aff_free(maff);
5924 return maff;
5927 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5929 aff = isl_aff_cow(aff);
5930 if (!aff)
5931 return NULL;
5933 aff->ls = isl_local_space_lift(aff->ls);
5934 if (!aff->ls)
5935 return isl_aff_free(aff);
5937 return aff;
5940 /* Lift "maff" to a space with extra dimensions such that the result
5941 * has no more existentially quantified variables.
5942 * If "ls" is not NULL, then *ls is assigned the local space that lies
5943 * at the basis of the lifting applied to "maff".
5945 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5946 __isl_give isl_local_space **ls)
5948 int i;
5949 isl_space *space;
5950 isl_size n_div;
5952 if (ls)
5953 *ls = NULL;
5955 if (!maff)
5956 return NULL;
5958 if (maff->n == 0) {
5959 if (ls) {
5960 isl_space *space = isl_multi_aff_get_domain_space(maff);
5961 *ls = isl_local_space_from_space(space);
5962 if (!*ls)
5963 return isl_multi_aff_free(maff);
5965 return maff;
5968 maff = isl_multi_aff_cow(maff);
5969 maff = isl_multi_aff_align_divs(maff);
5970 if (!maff)
5971 return NULL;
5973 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5974 if (n_div < 0)
5975 return isl_multi_aff_free(maff);
5976 space = isl_multi_aff_get_space(maff);
5977 space = isl_space_lift(isl_space_domain(space), n_div);
5978 space = isl_space_extend_domain_with_range(space,
5979 isl_multi_aff_get_space(maff));
5980 if (!space)
5981 return isl_multi_aff_free(maff);
5982 isl_space_free(maff->space);
5983 maff->space = space;
5985 if (ls) {
5986 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5987 if (!*ls)
5988 return isl_multi_aff_free(maff);
5991 for (i = 0; i < maff->n; ++i) {
5992 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5993 if (!maff->u.p[i])
5994 goto error;
5997 return maff;
5998 error:
5999 if (ls)
6000 isl_local_space_free(*ls);
6001 return isl_multi_aff_free(maff);
6004 #undef TYPE
6005 #define TYPE isl_pw_multi_aff
6006 static
6007 #include "check_type_range_templ.c"
6009 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
6011 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
6012 __isl_keep isl_pw_multi_aff *pma, int pos)
6014 int i;
6015 isl_size n_out;
6016 isl_space *space;
6017 isl_pw_aff *pa;
6019 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6020 return NULL;
6022 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
6023 if (n_out < 0)
6024 return NULL;
6026 space = isl_pw_multi_aff_get_space(pma);
6027 space = isl_space_drop_dims(space, isl_dim_out,
6028 pos + 1, n_out - pos - 1);
6029 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
6031 pa = isl_pw_aff_alloc_size(space, pma->n);
6032 for (i = 0; i < pma->n; ++i) {
6033 isl_aff *aff;
6034 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
6035 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
6038 return pa;
6041 /* Return an isl_pw_multi_aff with the given "set" as domain and
6042 * an unnamed zero-dimensional range.
6044 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
6045 __isl_take isl_set *set)
6047 isl_multi_aff *ma;
6048 isl_space *space;
6050 space = isl_set_get_space(set);
6051 space = isl_space_from_domain(space);
6052 ma = isl_multi_aff_zero(space);
6053 return isl_pw_multi_aff_alloc(set, ma);
6056 /* Add an isl_pw_multi_aff with the given "set" as domain and
6057 * an unnamed zero-dimensional range to *user.
6059 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
6060 void *user)
6062 isl_union_pw_multi_aff **upma = user;
6063 isl_pw_multi_aff *pma;
6065 pma = isl_pw_multi_aff_from_domain(set);
6066 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
6068 return isl_stat_ok;
6071 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
6072 * an unnamed zero-dimensional range.
6074 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
6075 __isl_take isl_union_set *uset)
6077 isl_space *space;
6078 isl_union_pw_multi_aff *upma;
6080 if (!uset)
6081 return NULL;
6083 space = isl_union_set_get_space(uset);
6084 upma = isl_union_pw_multi_aff_empty(space);
6086 if (isl_union_set_foreach_set(uset,
6087 &add_pw_multi_aff_from_domain, &upma) < 0)
6088 goto error;
6090 isl_union_set_free(uset);
6091 return upma;
6092 error:
6093 isl_union_set_free(uset);
6094 isl_union_pw_multi_aff_free(upma);
6095 return NULL;
6098 /* Local data for bin_entry and the callback "fn".
6100 struct isl_union_pw_multi_aff_bin_data {
6101 isl_union_pw_multi_aff *upma2;
6102 isl_union_pw_multi_aff *res;
6103 isl_pw_multi_aff *pma;
6104 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6107 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6108 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6110 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6112 struct isl_union_pw_multi_aff_bin_data *data = user;
6113 isl_stat r;
6115 data->pma = pma;
6116 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6117 data->fn, data);
6118 isl_pw_multi_aff_free(pma);
6120 return r;
6123 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6124 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6125 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6126 * as *entry. The callback should adjust data->res if desired.
6128 static __isl_give isl_union_pw_multi_aff *bin_op(
6129 __isl_take isl_union_pw_multi_aff *upma1,
6130 __isl_take isl_union_pw_multi_aff *upma2,
6131 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6133 isl_space *space;
6134 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6136 space = isl_union_pw_multi_aff_get_space(upma2);
6137 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6138 space = isl_union_pw_multi_aff_get_space(upma1);
6139 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6141 if (!upma1 || !upma2)
6142 goto error;
6144 data.upma2 = upma2;
6145 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6146 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6147 &bin_entry, &data) < 0)
6148 goto error;
6150 isl_union_pw_multi_aff_free(upma1);
6151 isl_union_pw_multi_aff_free(upma2);
6152 return data.res;
6153 error:
6154 isl_union_pw_multi_aff_free(upma1);
6155 isl_union_pw_multi_aff_free(upma2);
6156 isl_union_pw_multi_aff_free(data.res);
6157 return NULL;
6160 /* Given two isl_pw_multi_affs A -> B and C -> D,
6161 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6163 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6164 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6166 isl_space *space;
6168 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6169 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6170 isl_pw_multi_aff_get_space(pma2));
6171 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6172 &isl_multi_aff_range_product);
6175 /* Given two isl_pw_multi_affs A -> B and C -> D,
6176 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6178 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6179 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6181 isl_space *space;
6183 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6184 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6185 isl_pw_multi_aff_get_space(pma2));
6186 space = isl_space_flatten_range(space);
6187 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6188 &isl_multi_aff_flat_range_product);
6191 /* If data->pma and "pma2" have the same domain space, then use "range_product"
6192 * to compute some form of range product and add the result to data->res.
6194 static isl_stat gen_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6195 __isl_give isl_pw_multi_aff *(*range_product)(
6196 __isl_take isl_pw_multi_aff *pma1,
6197 __isl_take isl_pw_multi_aff *pma2),
6198 void *user)
6200 struct isl_union_pw_multi_aff_bin_data *data = user;
6201 isl_bool match;
6202 isl_space *space1, *space2;
6204 space1 = isl_pw_multi_aff_peek_space(data->pma);
6205 space2 = isl_pw_multi_aff_peek_space(pma2);
6206 match = isl_space_tuple_is_equal(space1, isl_dim_in,
6207 space2, isl_dim_in);
6208 if (match < 0 || !match) {
6209 isl_pw_multi_aff_free(pma2);
6210 return match < 0 ? isl_stat_error : isl_stat_ok;
6213 pma2 = range_product(isl_pw_multi_aff_copy(data->pma), pma2);
6215 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6217 return isl_stat_ok;
6220 /* If data->pma and "pma2" have the same domain space, then compute
6221 * their flat range product and add the result to data->res.
6223 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6224 void *user)
6226 return gen_range_product_entry(pma2,
6227 &isl_pw_multi_aff_flat_range_product, user);
6230 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6231 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6233 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6234 __isl_take isl_union_pw_multi_aff *upma1,
6235 __isl_take isl_union_pw_multi_aff *upma2)
6237 return bin_op(upma1, upma2, &flat_range_product_entry);
6240 /* If data->pma and "pma2" have the same domain space, then compute
6241 * their range product and add the result to data->res.
6243 static isl_stat range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6244 void *user)
6246 return gen_range_product_entry(pma2,
6247 &isl_pw_multi_aff_range_product, user);
6250 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6251 * construct an isl_union_pw_multi_aff (A * C) -> [B -> D].
6253 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_product(
6254 __isl_take isl_union_pw_multi_aff *upma1,
6255 __isl_take isl_union_pw_multi_aff *upma2)
6257 return bin_op(upma1, upma2, &range_product_entry);
6260 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6261 * The parameters are assumed to have been aligned.
6263 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6264 * except that it works on two different isl_pw_* types.
6266 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6267 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6268 __isl_take isl_pw_aff *pa)
6270 int i, j, n;
6271 isl_pw_multi_aff *res = NULL;
6273 if (!pma || !pa)
6274 goto error;
6276 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6277 pa->dim, isl_dim_in))
6278 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6279 "domains don't match", goto error);
6280 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6281 goto error;
6283 n = pma->n * pa->n;
6284 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6286 for (i = 0; i < pma->n; ++i) {
6287 for (j = 0; j < pa->n; ++j) {
6288 isl_set *common;
6289 isl_multi_aff *res_ij;
6290 int empty;
6292 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6293 isl_set_copy(pa->p[j].set));
6294 empty = isl_set_plain_is_empty(common);
6295 if (empty < 0 || empty) {
6296 isl_set_free(common);
6297 if (empty < 0)
6298 goto error;
6299 continue;
6302 res_ij = isl_multi_aff_set_aff(
6303 isl_multi_aff_copy(pma->p[i].maff), pos,
6304 isl_aff_copy(pa->p[j].aff));
6305 res_ij = isl_multi_aff_gist(res_ij,
6306 isl_set_copy(common));
6308 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6312 isl_pw_multi_aff_free(pma);
6313 isl_pw_aff_free(pa);
6314 return res;
6315 error:
6316 isl_pw_multi_aff_free(pma);
6317 isl_pw_aff_free(pa);
6318 return isl_pw_multi_aff_free(res);
6321 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6323 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6324 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6325 __isl_take isl_pw_aff *pa)
6327 isl_bool equal_params;
6329 if (!pma || !pa)
6330 goto error;
6331 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6332 if (equal_params < 0)
6333 goto error;
6334 if (equal_params)
6335 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6336 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6337 isl_pw_aff_check_named_params(pa) < 0)
6338 goto error;
6339 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6340 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6341 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6342 error:
6343 isl_pw_multi_aff_free(pma);
6344 isl_pw_aff_free(pa);
6345 return NULL;
6348 /* Do the parameters of "pa" match those of "space"?
6350 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6351 __isl_keep isl_space *space)
6353 isl_space *pa_space;
6354 isl_bool match;
6356 if (!pa || !space)
6357 return isl_bool_error;
6359 pa_space = isl_pw_aff_get_space(pa);
6361 match = isl_space_has_equal_params(space, pa_space);
6363 isl_space_free(pa_space);
6364 return match;
6367 /* Check that the domain space of "pa" matches "space".
6369 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6370 __isl_keep isl_space *space)
6372 isl_space *pa_space;
6373 isl_bool match;
6375 if (!pa || !space)
6376 return isl_stat_error;
6378 pa_space = isl_pw_aff_get_space(pa);
6380 match = isl_space_has_equal_params(space, pa_space);
6381 if (match < 0)
6382 goto error;
6383 if (!match)
6384 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6385 "parameters don't match", goto error);
6386 match = isl_space_tuple_is_equal(space, isl_dim_in,
6387 pa_space, isl_dim_in);
6388 if (match < 0)
6389 goto error;
6390 if (!match)
6391 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6392 "domains don't match", goto error);
6393 isl_space_free(pa_space);
6394 return isl_stat_ok;
6395 error:
6396 isl_space_free(pa_space);
6397 return isl_stat_error;
6400 #undef BASE
6401 #define BASE pw_aff
6402 #undef DOMBASE
6403 #define DOMBASE set
6405 #include <isl_multi_explicit_domain.c>
6406 #include <isl_multi_pw_aff_explicit_domain.c>
6407 #include <isl_multi_templ.c>
6408 #include <isl_multi_add_constant_templ.c>
6409 #include <isl_multi_apply_set.c>
6410 #include <isl_multi_arith_templ.c>
6411 #include <isl_multi_bind_templ.c>
6412 #include <isl_multi_bind_domain_templ.c>
6413 #include <isl_multi_coalesce.c>
6414 #include <isl_multi_domain_templ.c>
6415 #include <isl_multi_dim_id_templ.c>
6416 #include <isl_multi_dims.c>
6417 #include <isl_multi_from_base_templ.c>
6418 #include <isl_multi_gist.c>
6419 #include <isl_multi_hash.c>
6420 #include <isl_multi_identity_templ.c>
6421 #include <isl_multi_align_set.c>
6422 #include <isl_multi_insert_domain_templ.c>
6423 #include <isl_multi_intersect.c>
6424 #include <isl_multi_min_max_templ.c>
6425 #include <isl_multi_move_dims_templ.c>
6426 #include <isl_multi_nan_templ.c>
6427 #include <isl_multi_param_templ.c>
6428 #include <isl_multi_product_templ.c>
6429 #include <isl_multi_splice_templ.c>
6430 #include <isl_multi_tuple_id_templ.c>
6431 #include <isl_multi_union_add_templ.c>
6432 #include <isl_multi_zero_templ.c>
6433 #include <isl_multi_unbind_params_templ.c>
6435 /* Are all elements of "mpa" piecewise constants?
6437 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6439 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6442 /* Does "mpa" have a non-trivial explicit domain?
6444 * The explicit domain, if present, is trivial if it represents
6445 * an (obviously) universe set.
6447 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6448 __isl_keep isl_multi_pw_aff *mpa)
6450 if (!mpa)
6451 return isl_bool_error;
6452 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6453 return isl_bool_false;
6454 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6457 #undef BASE
6458 #define BASE set
6460 #include "isl_opt_mpa_templ.c"
6462 /* Compute the minima of the set dimensions as a function of the
6463 * parameters, but independently of the other set dimensions.
6465 __isl_give isl_multi_pw_aff *isl_set_min_multi_pw_aff(__isl_take isl_set *set)
6467 return set_opt_mpa(set, &isl_set_dim_min);
6470 /* Compute the maxima of the set dimensions as a function of the
6471 * parameters, but independently of the other set dimensions.
6473 __isl_give isl_multi_pw_aff *isl_set_max_multi_pw_aff(__isl_take isl_set *set)
6475 return set_opt_mpa(set, &isl_set_dim_max);
6478 #undef BASE
6479 #define BASE map
6481 #include "isl_opt_mpa_templ.c"
6483 /* Compute the minima of the output dimensions as a function of the
6484 * parameters and input dimensions, but independently of
6485 * the other output dimensions.
6487 __isl_give isl_multi_pw_aff *isl_map_min_multi_pw_aff(__isl_take isl_map *map)
6489 return map_opt_mpa(map, &isl_map_dim_min);
6492 /* Compute the maxima of the output dimensions as a function of the
6493 * parameters and input dimensions, but independently of
6494 * the other output dimensions.
6496 __isl_give isl_multi_pw_aff *isl_map_max_multi_pw_aff(__isl_take isl_map *map)
6498 return map_opt_mpa(map, &isl_map_dim_max);
6501 /* Scale the elements of "pma" by the corresponding elements of "mv".
6503 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6504 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6506 int i;
6507 isl_bool equal_params;
6509 pma = isl_pw_multi_aff_cow(pma);
6510 if (!pma || !mv)
6511 goto error;
6512 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6513 mv->space, isl_dim_set))
6514 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6515 "spaces don't match", goto error);
6516 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6517 if (equal_params < 0)
6518 goto error;
6519 if (!equal_params) {
6520 pma = isl_pw_multi_aff_align_params(pma,
6521 isl_multi_val_get_space(mv));
6522 mv = isl_multi_val_align_params(mv,
6523 isl_pw_multi_aff_get_space(pma));
6524 if (!pma || !mv)
6525 goto error;
6528 for (i = 0; i < pma->n; ++i) {
6529 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6530 isl_multi_val_copy(mv));
6531 if (!pma->p[i].maff)
6532 goto error;
6535 isl_multi_val_free(mv);
6536 return pma;
6537 error:
6538 isl_multi_val_free(mv);
6539 isl_pw_multi_aff_free(pma);
6540 return NULL;
6543 /* This function is called for each entry of an isl_union_pw_multi_aff.
6544 * If the space of the entry matches that of data->mv,
6545 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6546 * Otherwise, return an empty isl_pw_multi_aff.
6548 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6549 __isl_take isl_pw_multi_aff *pma, void *user)
6551 isl_multi_val *mv = user;
6553 if (!pma)
6554 return NULL;
6555 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6556 mv->space, isl_dim_set)) {
6557 isl_space *space = isl_pw_multi_aff_get_space(pma);
6558 isl_pw_multi_aff_free(pma);
6559 return isl_pw_multi_aff_empty(space);
6562 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6565 /* Scale the elements of "upma" by the corresponding elements of "mv",
6566 * for those entries that match the space of "mv".
6568 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6569 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6571 struct isl_union_pw_multi_aff_transform_control control = {
6572 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6573 .fn_user = mv,
6576 upma = isl_union_pw_multi_aff_align_params(upma,
6577 isl_multi_val_get_space(mv));
6578 mv = isl_multi_val_align_params(mv,
6579 isl_union_pw_multi_aff_get_space(upma));
6580 if (!upma || !mv)
6581 goto error;
6583 return isl_union_pw_multi_aff_transform(upma, &control);
6585 isl_multi_val_free(mv);
6586 return upma;
6587 error:
6588 isl_multi_val_free(mv);
6589 isl_union_pw_multi_aff_free(upma);
6590 return NULL;
6593 /* Construct and return a piecewise multi affine expression
6594 * in the given space with value zero in each of the output dimensions and
6595 * a universe domain.
6597 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6599 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6602 /* Construct and return a piecewise multi affine expression
6603 * that is equal to the given piecewise affine expression.
6605 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6606 __isl_take isl_pw_aff *pa)
6608 int i;
6609 isl_space *space;
6610 isl_pw_multi_aff *pma;
6612 if (!pa)
6613 return NULL;
6615 space = isl_pw_aff_get_space(pa);
6616 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6618 for (i = 0; i < pa->n; ++i) {
6619 isl_set *set;
6620 isl_multi_aff *ma;
6622 set = isl_set_copy(pa->p[i].set);
6623 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6624 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6627 isl_pw_aff_free(pa);
6628 return pma;
6631 /* Construct and return a piecewise multi affine expression
6632 * that is equal to the given multi piecewise affine expression
6633 * on the shared domain of the piecewise affine expressions,
6634 * in the special case of a 0D multi piecewise affine expression.
6636 * Create a piecewise multi affine expression with the explicit domain of
6637 * the 0D multi piecewise affine expression as domain.
6639 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6640 __isl_take isl_multi_pw_aff *mpa)
6642 isl_space *space;
6643 isl_set *dom;
6644 isl_multi_aff *ma;
6646 space = isl_multi_pw_aff_get_space(mpa);
6647 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6648 isl_multi_pw_aff_free(mpa);
6650 ma = isl_multi_aff_zero(space);
6651 return isl_pw_multi_aff_alloc(dom, ma);
6654 /* Construct and return a piecewise multi affine expression
6655 * that is equal to the given multi piecewise affine expression
6656 * on the shared domain of the piecewise affine expressions.
6658 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6659 __isl_take isl_multi_pw_aff *mpa)
6661 int i;
6662 isl_space *space;
6663 isl_pw_aff *pa;
6664 isl_pw_multi_aff *pma;
6666 if (!mpa)
6667 return NULL;
6669 if (mpa->n == 0)
6670 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6672 space = isl_multi_pw_aff_get_space(mpa);
6673 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6674 pma = isl_pw_multi_aff_from_pw_aff(pa);
6676 for (i = 1; i < mpa->n; ++i) {
6677 isl_pw_multi_aff *pma_i;
6679 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6680 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6681 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6684 pma = isl_pw_multi_aff_reset_space(pma, space);
6686 isl_multi_pw_aff_free(mpa);
6687 return pma;
6690 /* Convenience function that constructs an isl_multi_pw_aff
6691 * directly from an isl_aff.
6693 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_aff(__isl_take isl_aff *aff)
6695 return isl_multi_pw_aff_from_pw_aff(isl_pw_aff_from_aff(aff));
6698 /* Construct and return a multi piecewise affine expression
6699 * that is equal to the given multi affine expression.
6701 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6702 __isl_take isl_multi_aff *ma)
6704 int i;
6705 isl_size n;
6706 isl_multi_pw_aff *mpa;
6708 n = isl_multi_aff_dim(ma, isl_dim_out);
6709 if (n < 0)
6710 ma = isl_multi_aff_free(ma);
6711 if (!ma)
6712 return NULL;
6714 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6716 for (i = 0; i < n; ++i) {
6717 isl_pw_aff *pa;
6719 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6720 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6723 isl_multi_aff_free(ma);
6724 return mpa;
6727 /* Construct and return a multi piecewise affine expression
6728 * that is equal to the given piecewise multi affine expression.
6730 * If the resulting multi piecewise affine expression has
6731 * an explicit domain, then assign it the domain of the input.
6732 * In other cases, the domain is stored in the individual elements.
6734 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6735 __isl_take isl_pw_multi_aff *pma)
6737 int i;
6738 isl_size n;
6739 isl_space *space;
6740 isl_multi_pw_aff *mpa;
6742 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6743 if (n < 0)
6744 pma = isl_pw_multi_aff_free(pma);
6745 space = isl_pw_multi_aff_get_space(pma);
6746 mpa = isl_multi_pw_aff_alloc(space);
6748 for (i = 0; i < n; ++i) {
6749 isl_pw_aff *pa;
6751 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6752 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6754 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6755 isl_set *dom;
6757 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6758 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6761 isl_pw_multi_aff_free(pma);
6762 return mpa;
6765 /* Do "pa1" and "pa2" represent the same function?
6767 * We first check if they are obviously equal.
6768 * If not, we convert them to maps and check if those are equal.
6770 * If "pa1" or "pa2" contain any NaNs, then they are considered
6771 * not to be the same. A NaN is not equal to anything, not even
6772 * to another NaN.
6774 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6775 __isl_keep isl_pw_aff *pa2)
6777 isl_bool equal;
6778 isl_bool has_nan;
6779 isl_map *map1, *map2;
6781 if (!pa1 || !pa2)
6782 return isl_bool_error;
6784 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6785 if (equal < 0 || equal)
6786 return equal;
6787 has_nan = either_involves_nan(pa1, pa2);
6788 if (has_nan < 0)
6789 return isl_bool_error;
6790 if (has_nan)
6791 return isl_bool_false;
6793 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6794 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6795 equal = isl_map_is_equal(map1, map2);
6796 isl_map_free(map1);
6797 isl_map_free(map2);
6799 return equal;
6802 /* Do "mpa1" and "mpa2" represent the same function?
6804 * Note that we cannot convert the entire isl_multi_pw_aff
6805 * to a map because the domains of the piecewise affine expressions
6806 * may not be the same.
6808 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6809 __isl_keep isl_multi_pw_aff *mpa2)
6811 int i;
6812 isl_bool equal, equal_params;
6814 if (!mpa1 || !mpa2)
6815 return isl_bool_error;
6817 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6818 if (equal_params < 0)
6819 return isl_bool_error;
6820 if (!equal_params) {
6821 if (!isl_space_has_named_params(mpa1->space))
6822 return isl_bool_false;
6823 if (!isl_space_has_named_params(mpa2->space))
6824 return isl_bool_false;
6825 mpa1 = isl_multi_pw_aff_copy(mpa1);
6826 mpa2 = isl_multi_pw_aff_copy(mpa2);
6827 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6828 isl_multi_pw_aff_get_space(mpa2));
6829 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6830 isl_multi_pw_aff_get_space(mpa1));
6831 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6832 isl_multi_pw_aff_free(mpa1);
6833 isl_multi_pw_aff_free(mpa2);
6834 return equal;
6837 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6838 if (equal < 0 || !equal)
6839 return equal;
6841 for (i = 0; i < mpa1->n; ++i) {
6842 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6843 if (equal < 0 || !equal)
6844 return equal;
6847 return isl_bool_true;
6850 /* Do "pma1" and "pma2" represent the same function?
6852 * First check if they are obviously equal.
6853 * If not, then convert them to maps and check if those are equal.
6855 * If "pa1" or "pa2" contain any NaNs, then they are considered
6856 * not to be the same. A NaN is not equal to anything, not even
6857 * to another NaN.
6859 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6860 __isl_keep isl_pw_multi_aff *pma2)
6862 isl_bool equal;
6863 isl_bool has_nan;
6864 isl_map *map1, *map2;
6866 if (!pma1 || !pma2)
6867 return isl_bool_error;
6869 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6870 if (equal < 0 || equal)
6871 return equal;
6872 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6873 if (has_nan >= 0 && !has_nan)
6874 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6875 if (has_nan < 0 || has_nan)
6876 return isl_bool_not(has_nan);
6878 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
6879 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
6880 equal = isl_map_is_equal(map1, map2);
6881 isl_map_free(map1);
6882 isl_map_free(map2);
6884 return equal;
6887 /* Compute the pullback of "mpa" by the function represented by "ma".
6888 * In other words, plug in "ma" in "mpa".
6890 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6892 * If "mpa" has an explicit domain, then it is this domain
6893 * that needs to undergo a pullback, i.e., a preimage.
6895 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6896 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6898 int i;
6899 isl_space *space = NULL;
6901 mpa = isl_multi_pw_aff_cow(mpa);
6902 if (!mpa || !ma)
6903 goto error;
6905 space = isl_space_join(isl_multi_aff_get_space(ma),
6906 isl_multi_pw_aff_get_space(mpa));
6907 if (!space)
6908 goto error;
6910 for (i = 0; i < mpa->n; ++i) {
6911 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6912 isl_multi_aff_copy(ma));
6913 if (!mpa->u.p[i])
6914 goto error;
6916 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6917 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6918 isl_multi_aff_copy(ma));
6919 if (!mpa->u.dom)
6920 goto error;
6923 isl_multi_aff_free(ma);
6924 isl_space_free(mpa->space);
6925 mpa->space = space;
6926 return mpa;
6927 error:
6928 isl_space_free(space);
6929 isl_multi_pw_aff_free(mpa);
6930 isl_multi_aff_free(ma);
6931 return NULL;
6934 /* Compute the pullback of "mpa" by the function represented by "ma".
6935 * In other words, plug in "ma" in "mpa".
6937 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6938 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6940 isl_bool equal_params;
6942 if (!mpa || !ma)
6943 goto error;
6944 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6945 if (equal_params < 0)
6946 goto error;
6947 if (equal_params)
6948 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6949 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6950 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6951 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6952 error:
6953 isl_multi_pw_aff_free(mpa);
6954 isl_multi_aff_free(ma);
6955 return NULL;
6958 /* Compute the pullback of "mpa" by the function represented by "pma".
6959 * In other words, plug in "pma" in "mpa".
6961 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6963 * If "mpa" has an explicit domain, then it is this domain
6964 * that needs to undergo a pullback, i.e., a preimage.
6966 static __isl_give isl_multi_pw_aff *
6967 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6968 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6970 int i;
6971 isl_space *space = NULL;
6973 mpa = isl_multi_pw_aff_cow(mpa);
6974 if (!mpa || !pma)
6975 goto error;
6977 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6978 isl_multi_pw_aff_get_space(mpa));
6980 for (i = 0; i < mpa->n; ++i) {
6981 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6982 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6983 if (!mpa->u.p[i])
6984 goto error;
6986 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6987 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6988 isl_pw_multi_aff_copy(pma));
6989 if (!mpa->u.dom)
6990 goto error;
6993 isl_pw_multi_aff_free(pma);
6994 isl_space_free(mpa->space);
6995 mpa->space = space;
6996 return mpa;
6997 error:
6998 isl_space_free(space);
6999 isl_multi_pw_aff_free(mpa);
7000 isl_pw_multi_aff_free(pma);
7001 return NULL;
7004 /* Compute the pullback of "mpa" by the function represented by "pma".
7005 * In other words, plug in "pma" in "mpa".
7007 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
7008 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
7010 isl_bool equal_params;
7012 if (!mpa || !pma)
7013 goto error;
7014 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
7015 if (equal_params < 0)
7016 goto error;
7017 if (equal_params)
7018 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7019 mpa = isl_multi_pw_aff_align_params(mpa,
7020 isl_pw_multi_aff_get_space(pma));
7021 pma = isl_pw_multi_aff_align_params(pma,
7022 isl_multi_pw_aff_get_space(mpa));
7023 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
7024 error:
7025 isl_multi_pw_aff_free(mpa);
7026 isl_pw_multi_aff_free(pma);
7027 return NULL;
7030 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7031 * with the domain of "aff". The domain of the result is the same
7032 * as that of "mpa".
7033 * "mpa" and "aff" are assumed to have been aligned.
7035 * We first extract the parametric constant from "aff", defined
7036 * over the correct domain.
7037 * Then we add the appropriate combinations of the members of "mpa".
7038 * Finally, we add the integer divisions through recursive calls.
7040 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
7041 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7043 int i;
7044 isl_size n_in, n_div, n_mpa_in;
7045 isl_space *space;
7046 isl_val *v;
7047 isl_pw_aff *pa;
7048 isl_aff *tmp;
7050 n_in = isl_aff_dim(aff, isl_dim_in);
7051 n_div = isl_aff_dim(aff, isl_dim_div);
7052 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
7053 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
7054 goto error;
7056 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
7057 tmp = isl_aff_copy(aff);
7058 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
7059 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
7060 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
7061 tmp = isl_aff_reset_domain_space(tmp, space);
7062 pa = isl_pw_aff_from_aff(tmp);
7064 for (i = 0; i < n_in; ++i) {
7065 isl_pw_aff *pa_i;
7067 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
7068 continue;
7069 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
7070 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
7071 pa_i = isl_pw_aff_scale_val(pa_i, v);
7072 pa = isl_pw_aff_add(pa, pa_i);
7075 for (i = 0; i < n_div; ++i) {
7076 isl_aff *div;
7077 isl_pw_aff *pa_i;
7079 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
7080 continue;
7081 div = isl_aff_get_div(aff, i);
7082 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7083 isl_multi_pw_aff_copy(mpa), div);
7084 pa_i = isl_pw_aff_floor(pa_i);
7085 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
7086 pa_i = isl_pw_aff_scale_val(pa_i, v);
7087 pa = isl_pw_aff_add(pa, pa_i);
7090 isl_multi_pw_aff_free(mpa);
7091 isl_aff_free(aff);
7093 return pa;
7094 error:
7095 isl_multi_pw_aff_free(mpa);
7096 isl_aff_free(aff);
7097 return NULL;
7100 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
7101 * with the domain of "aff". The domain of the result is the same
7102 * as that of "mpa".
7104 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
7105 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
7107 isl_bool equal_params;
7109 if (!aff || !mpa)
7110 goto error;
7111 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
7112 if (equal_params < 0)
7113 goto error;
7114 if (equal_params)
7115 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7117 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
7118 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
7120 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
7121 error:
7122 isl_aff_free(aff);
7123 isl_multi_pw_aff_free(mpa);
7124 return NULL;
7127 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7128 * with the domain of "pa". The domain of the result is the same
7129 * as that of "mpa".
7130 * "mpa" and "pa" are assumed to have been aligned.
7132 * We consider each piece in turn. Note that the domains of the
7133 * pieces are assumed to be disjoint and they remain disjoint
7134 * after taking the preimage (over the same function).
7136 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
7137 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7139 isl_space *space;
7140 isl_pw_aff *res;
7141 int i;
7143 if (!mpa || !pa)
7144 goto error;
7146 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
7147 isl_pw_aff_get_space(pa));
7148 res = isl_pw_aff_empty(space);
7150 for (i = 0; i < pa->n; ++i) {
7151 isl_pw_aff *pa_i;
7152 isl_set *domain;
7154 pa_i = isl_multi_pw_aff_apply_aff_aligned(
7155 isl_multi_pw_aff_copy(mpa),
7156 isl_aff_copy(pa->p[i].aff));
7157 domain = isl_set_copy(pa->p[i].set);
7158 domain = isl_set_preimage_multi_pw_aff(domain,
7159 isl_multi_pw_aff_copy(mpa));
7160 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
7161 res = isl_pw_aff_add_disjoint(res, pa_i);
7164 isl_pw_aff_free(pa);
7165 isl_multi_pw_aff_free(mpa);
7166 return res;
7167 error:
7168 isl_pw_aff_free(pa);
7169 isl_multi_pw_aff_free(mpa);
7170 return NULL;
7173 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
7174 * with the domain of "pa". The domain of the result is the same
7175 * as that of "mpa".
7177 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
7178 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
7180 isl_bool equal_params;
7182 if (!pa || !mpa)
7183 goto error;
7184 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
7185 if (equal_params < 0)
7186 goto error;
7187 if (equal_params)
7188 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7190 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7191 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7193 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7194 error:
7195 isl_pw_aff_free(pa);
7196 isl_multi_pw_aff_free(mpa);
7197 return NULL;
7200 /* Compute the pullback of "pa" by the function represented by "mpa".
7201 * In other words, plug in "mpa" in "pa".
7202 * "pa" and "mpa" are assumed to have been aligned.
7204 * The pullback is computed by applying "pa" to "mpa".
7206 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7207 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7209 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7212 /* Compute the pullback of "pa" by the function represented by "mpa".
7213 * In other words, plug in "mpa" in "pa".
7215 * The pullback is computed by applying "pa" to "mpa".
7217 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7218 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7220 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7223 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7224 * In other words, plug in "mpa2" in "mpa1".
7226 * We pullback each member of "mpa1" in turn.
7228 * If "mpa1" has an explicit domain, then it is this domain
7229 * that needs to undergo a pullback instead, i.e., a preimage.
7231 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7232 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7234 int i;
7235 isl_space *space = NULL;
7237 isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
7238 mpa1 = isl_multi_pw_aff_cow(mpa1);
7239 if (!mpa1 || !mpa2)
7240 goto error;
7242 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7243 isl_multi_pw_aff_get_space(mpa1));
7245 for (i = 0; i < mpa1->n; ++i) {
7246 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7247 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7248 if (!mpa1->u.p[i])
7249 goto error;
7252 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7253 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7254 isl_multi_pw_aff_copy(mpa2));
7255 if (!mpa1->u.dom)
7256 goto error;
7258 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7260 isl_multi_pw_aff_free(mpa2);
7261 return mpa1;
7262 error:
7263 isl_space_free(space);
7264 isl_multi_pw_aff_free(mpa1);
7265 isl_multi_pw_aff_free(mpa2);
7266 return NULL;
7269 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7270 * of "mpa1" and "mpa2" live in the same space, construct map space
7271 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7272 * with this map space as extract argument.
7274 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7275 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7276 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7277 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7279 int match;
7280 isl_space *space1, *space2;
7281 isl_map *res;
7283 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7284 isl_multi_pw_aff_get_space(mpa2));
7285 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7286 isl_multi_pw_aff_get_space(mpa1));
7287 if (!mpa1 || !mpa2)
7288 goto error;
7289 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7290 mpa2->space, isl_dim_out);
7291 if (match < 0)
7292 goto error;
7293 if (!match)
7294 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7295 "range spaces don't match", goto error);
7296 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7297 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7298 space1 = isl_space_map_from_domain_and_range(space1, space2);
7300 res = order(mpa1, mpa2, space1);
7301 isl_multi_pw_aff_free(mpa1);
7302 isl_multi_pw_aff_free(mpa2);
7303 return res;
7304 error:
7305 isl_multi_pw_aff_free(mpa1);
7306 isl_multi_pw_aff_free(mpa2);
7307 return NULL;
7310 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7311 * where the function values are equal. "space" is the space of the result.
7312 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7314 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7315 * in the sequences are equal.
7317 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7318 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7319 __isl_take isl_space *space)
7321 int i;
7322 isl_size n;
7323 isl_map *res;
7325 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7326 if (n < 0)
7327 space = isl_space_free(space);
7328 res = isl_map_universe(space);
7330 for (i = 0; i < n; ++i) {
7331 isl_pw_aff *pa1, *pa2;
7332 isl_map *map;
7334 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7335 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7336 map = isl_pw_aff_eq_map(pa1, pa2);
7337 res = isl_map_intersect(res, map);
7340 return res;
7343 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7344 * where the function values are equal.
7346 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7347 __isl_take isl_multi_pw_aff *mpa2)
7349 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7350 &isl_multi_pw_aff_eq_map_on_space);
7353 /* Intersect "map" with the result of applying "order"
7354 * on two copies of "mpa".
7356 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7357 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7358 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7359 __isl_take isl_multi_pw_aff *mpa2))
7361 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7364 /* Return the subset of "map" where the domain and the range
7365 * have equal "mpa" values.
7367 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7368 __isl_take isl_multi_pw_aff *mpa)
7370 return isl_map_order_at_multi_pw_aff(map, mpa,
7371 &isl_multi_pw_aff_eq_map);
7374 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7375 * where the function values of "mpa1" lexicographically satisfies "base"
7376 * compared to that of "mpa2". "space" is the space of the result.
7377 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7379 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7380 * if its i-th element satisfies "base" when compared to
7381 * the i-th element of "mpa2" while all previous elements are
7382 * pairwise equal.
7384 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7385 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7386 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7387 __isl_take isl_pw_aff *pa2),
7388 __isl_take isl_space *space)
7390 int i;
7391 isl_size n;
7392 isl_map *res, *rest;
7394 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7395 if (n < 0)
7396 space = isl_space_free(space);
7397 res = isl_map_empty(isl_space_copy(space));
7398 rest = isl_map_universe(space);
7400 for (i = 0; i < n; ++i) {
7401 isl_pw_aff *pa1, *pa2;
7402 isl_map *map;
7404 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7405 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7406 map = base(pa1, pa2);
7407 map = isl_map_intersect(map, isl_map_copy(rest));
7408 res = isl_map_union(res, map);
7410 if (i == n - 1)
7411 continue;
7413 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7414 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7415 map = isl_pw_aff_eq_map(pa1, pa2);
7416 rest = isl_map_intersect(rest, map);
7419 isl_map_free(rest);
7420 return res;
7423 #undef ORDER
7424 #define ORDER le
7425 #include "isl_aff_lex_templ.c"
7427 #undef ORDER
7428 #define ORDER lt
7429 #include "isl_aff_lex_templ.c"
7431 #undef ORDER
7432 #define ORDER ge
7433 #include "isl_aff_lex_templ.c"
7435 #undef ORDER
7436 #define ORDER gt
7437 #include "isl_aff_lex_templ.c"
7439 /* Compare two isl_affs.
7441 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7442 * than "aff2" and 0 if they are equal.
7444 * The order is fairly arbitrary. We do consider expressions that only involve
7445 * earlier dimensions as "smaller".
7447 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7449 int cmp;
7450 int last1, last2;
7452 if (aff1 == aff2)
7453 return 0;
7455 if (!aff1)
7456 return -1;
7457 if (!aff2)
7458 return 1;
7460 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7461 if (cmp != 0)
7462 return cmp;
7464 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7465 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7466 if (last1 != last2)
7467 return last1 - last2;
7469 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7472 /* Compare two isl_pw_affs.
7474 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7475 * than "pa2" and 0 if they are equal.
7477 * The order is fairly arbitrary. We do consider expressions that only involve
7478 * earlier dimensions as "smaller".
7480 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7481 __isl_keep isl_pw_aff *pa2)
7483 int i;
7484 int cmp;
7486 if (pa1 == pa2)
7487 return 0;
7489 if (!pa1)
7490 return -1;
7491 if (!pa2)
7492 return 1;
7494 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7495 if (cmp != 0)
7496 return cmp;
7498 if (pa1->n != pa2->n)
7499 return pa1->n - pa2->n;
7501 for (i = 0; i < pa1->n; ++i) {
7502 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7503 if (cmp != 0)
7504 return cmp;
7505 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7506 if (cmp != 0)
7507 return cmp;
7510 return 0;
7513 /* Return a piecewise affine expression that is equal to "v" on "domain".
7515 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7516 __isl_take isl_val *v)
7518 isl_space *space;
7519 isl_local_space *ls;
7520 isl_aff *aff;
7522 space = isl_set_get_space(domain);
7523 ls = isl_local_space_from_space(space);
7524 aff = isl_aff_val_on_domain(ls, v);
7526 return isl_pw_aff_alloc(domain, aff);
7529 /* Return a piecewise affine expression that is equal to the parameter
7530 * with identifier "id" on "domain".
7532 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7533 __isl_take isl_set *domain, __isl_take isl_id *id)
7535 isl_space *space;
7536 isl_aff *aff;
7538 space = isl_set_get_space(domain);
7539 space = isl_space_add_param_id(space, isl_id_copy(id));
7540 domain = isl_set_align_params(domain, isl_space_copy(space));
7541 aff = isl_aff_param_on_domain_space_id(space, id);
7543 return isl_pw_aff_alloc(domain, aff);
7546 /* Return a multi affine expression that is equal to "mv" on domain
7547 * space "space".
7549 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7550 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7552 int i;
7553 isl_size n;
7554 isl_space *space2;
7555 isl_local_space *ls;
7556 isl_multi_aff *ma;
7558 n = isl_multi_val_dim(mv, isl_dim_set);
7559 if (!space || n < 0)
7560 goto error;
7562 space2 = isl_multi_val_get_space(mv);
7563 space2 = isl_space_align_params(space2, isl_space_copy(space));
7564 space = isl_space_align_params(space, isl_space_copy(space2));
7565 space = isl_space_map_from_domain_and_range(space, space2);
7566 ma = isl_multi_aff_alloc(isl_space_copy(space));
7567 ls = isl_local_space_from_space(isl_space_domain(space));
7568 for (i = 0; i < n; ++i) {
7569 isl_val *v;
7570 isl_aff *aff;
7572 v = isl_multi_val_get_val(mv, i);
7573 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7574 ma = isl_multi_aff_set_aff(ma, i, aff);
7576 isl_local_space_free(ls);
7578 isl_multi_val_free(mv);
7579 return ma;
7580 error:
7581 isl_space_free(space);
7582 isl_multi_val_free(mv);
7583 return NULL;
7586 /* Return a piecewise multi-affine expression
7587 * that is equal to "mv" on "domain".
7589 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7590 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7592 isl_space *space;
7593 isl_multi_aff *ma;
7595 space = isl_set_get_space(domain);
7596 ma = isl_multi_aff_multi_val_on_space(space, mv);
7598 return isl_pw_multi_aff_alloc(domain, ma);
7601 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7602 * mv is the value that should be attained on each domain set
7603 * res collects the results
7605 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7606 isl_multi_val *mv;
7607 isl_union_pw_multi_aff *res;
7610 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7611 * and add it to data->res.
7613 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7614 void *user)
7616 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7617 isl_pw_multi_aff *pma;
7618 isl_multi_val *mv;
7620 mv = isl_multi_val_copy(data->mv);
7621 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7622 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7624 return data->res ? isl_stat_ok : isl_stat_error;
7627 /* Return a union piecewise multi-affine expression
7628 * that is equal to "mv" on "domain".
7630 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7631 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7633 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7634 isl_space *space;
7636 space = isl_union_set_get_space(domain);
7637 data.res = isl_union_pw_multi_aff_empty(space);
7638 data.mv = mv;
7639 if (isl_union_set_foreach_set(domain,
7640 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7641 data.res = isl_union_pw_multi_aff_free(data.res);
7642 isl_union_set_free(domain);
7643 isl_multi_val_free(mv);
7644 return data.res;
7647 /* Compute the pullback of data->pma by the function represented by "pma2",
7648 * provided the spaces match, and add the results to data->res.
7650 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7652 struct isl_union_pw_multi_aff_bin_data *data = user;
7654 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7655 pma2->dim, isl_dim_out)) {
7656 isl_pw_multi_aff_free(pma2);
7657 return isl_stat_ok;
7660 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7661 isl_pw_multi_aff_copy(data->pma), pma2);
7663 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7664 if (!data->res)
7665 return isl_stat_error;
7667 return isl_stat_ok;
7670 /* Compute the pullback of "upma1" by the function represented by "upma2".
7672 __isl_give isl_union_pw_multi_aff *
7673 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7674 __isl_take isl_union_pw_multi_aff *upma1,
7675 __isl_take isl_union_pw_multi_aff *upma2)
7677 return bin_op(upma1, upma2, &pullback_entry);
7680 /* Apply "upma2" to "upma1".
7682 * That is, compute the pullback of "upma2" by "upma1".
7684 __isl_give isl_union_pw_multi_aff *
7685 isl_union_pw_multi_aff_apply_union_pw_multi_aff(
7686 __isl_take isl_union_pw_multi_aff *upma1,
7687 __isl_take isl_union_pw_multi_aff *upma2)
7689 return isl_union_pw_multi_aff_pullback_union_pw_multi_aff(upma2, upma1);
7692 /* Check that the domain space of "upa" matches "space".
7694 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7695 * can in principle never fail since the space "space" is that
7696 * of the isl_multi_union_pw_aff and is a set space such that
7697 * there is no domain space to match.
7699 * We check the parameters and double-check that "space" is
7700 * indeed that of a set.
7702 static isl_stat isl_union_pw_aff_check_match_domain_space(
7703 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7705 isl_space *upa_space;
7706 isl_bool match;
7708 if (!upa || !space)
7709 return isl_stat_error;
7711 match = isl_space_is_set(space);
7712 if (match < 0)
7713 return isl_stat_error;
7714 if (!match)
7715 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7716 "expecting set space", return isl_stat_error);
7718 upa_space = isl_union_pw_aff_get_space(upa);
7719 match = isl_space_has_equal_params(space, upa_space);
7720 if (match < 0)
7721 goto error;
7722 if (!match)
7723 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7724 "parameters don't match", goto error);
7726 isl_space_free(upa_space);
7727 return isl_stat_ok;
7728 error:
7729 isl_space_free(upa_space);
7730 return isl_stat_error;
7733 /* Do the parameters of "upa" match those of "space"?
7735 static isl_bool isl_union_pw_aff_matching_params(
7736 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7738 isl_space *upa_space;
7739 isl_bool match;
7741 if (!upa || !space)
7742 return isl_bool_error;
7744 upa_space = isl_union_pw_aff_get_space(upa);
7746 match = isl_space_has_equal_params(space, upa_space);
7748 isl_space_free(upa_space);
7749 return match;
7752 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7753 * space represents the new parameters.
7754 * res collects the results.
7756 struct isl_union_pw_aff_reset_params_data {
7757 isl_space *space;
7758 isl_union_pw_aff *res;
7761 /* Replace the parameters of "pa" by data->space and
7762 * add the result to data->res.
7764 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7766 struct isl_union_pw_aff_reset_params_data *data = user;
7767 isl_space *space;
7769 space = isl_pw_aff_get_space(pa);
7770 space = isl_space_replace_params(space, data->space);
7771 pa = isl_pw_aff_reset_space(pa, space);
7772 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7774 return data->res ? isl_stat_ok : isl_stat_error;
7777 /* Replace the domain space of "upa" by "space".
7778 * Since a union expression does not have a (single) domain space,
7779 * "space" is necessarily a parameter space.
7781 * Since the order and the names of the parameters determine
7782 * the hash value, we need to create a new hash table.
7784 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7785 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7787 struct isl_union_pw_aff_reset_params_data data = { space };
7788 isl_bool match;
7790 match = isl_union_pw_aff_matching_params(upa, space);
7791 if (match < 0)
7792 upa = isl_union_pw_aff_free(upa);
7793 else if (match) {
7794 isl_space_free(space);
7795 return upa;
7798 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7799 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7800 data.res = isl_union_pw_aff_free(data.res);
7802 isl_union_pw_aff_free(upa);
7803 isl_space_free(space);
7804 return data.res;
7807 /* Return the floor of "pa".
7809 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7811 return isl_pw_aff_floor(pa);
7814 /* Given f, return floor(f).
7816 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7817 __isl_take isl_union_pw_aff *upa)
7819 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7822 /* Compute
7824 * upa mod m = upa - m * floor(upa/m)
7826 * with m an integer value.
7828 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7829 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7831 isl_union_pw_aff *res;
7833 if (!upa || !m)
7834 goto error;
7836 if (!isl_val_is_int(m))
7837 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7838 "expecting integer modulo", goto error);
7839 if (!isl_val_is_pos(m))
7840 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7841 "expecting positive modulo", goto error);
7843 res = isl_union_pw_aff_copy(upa);
7844 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7845 upa = isl_union_pw_aff_floor(upa);
7846 upa = isl_union_pw_aff_scale_val(upa, m);
7847 res = isl_union_pw_aff_sub(res, upa);
7849 return res;
7850 error:
7851 isl_val_free(m);
7852 isl_union_pw_aff_free(upa);
7853 return NULL;
7856 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7857 * pos is the output position that needs to be extracted.
7858 * res collects the results.
7860 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7861 int pos;
7862 isl_union_pw_aff *res;
7865 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7866 * (assuming it has such a dimension) and add it to data->res.
7868 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7870 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7871 isl_size n_out;
7872 isl_pw_aff *pa;
7874 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7875 if (n_out < 0)
7876 return isl_stat_error;
7877 if (data->pos >= n_out) {
7878 isl_pw_multi_aff_free(pma);
7879 return isl_stat_ok;
7882 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7883 isl_pw_multi_aff_free(pma);
7885 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7887 return data->res ? isl_stat_ok : isl_stat_error;
7890 /* Extract an isl_union_pw_aff corresponding to
7891 * output dimension "pos" of "upma".
7893 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7894 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7896 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7897 isl_space *space;
7899 if (!upma)
7900 return NULL;
7902 if (pos < 0)
7903 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7904 "cannot extract at negative position", return NULL);
7906 space = isl_union_pw_multi_aff_get_space(upma);
7907 data.res = isl_union_pw_aff_empty(space);
7908 data.pos = pos;
7909 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7910 &get_union_pw_aff, &data) < 0)
7911 data.res = isl_union_pw_aff_free(data.res);
7913 return data.res;
7916 /* Return a union piecewise affine expression
7917 * that is equal to "aff" on "domain".
7919 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7920 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7922 isl_pw_aff *pa;
7924 pa = isl_pw_aff_from_aff(aff);
7925 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7928 /* Return a union piecewise affine expression
7929 * that is equal to the parameter identified by "id" on "domain".
7931 * Make sure the parameter appears in the space passed to
7932 * isl_aff_param_on_domain_space_id.
7934 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7935 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7937 isl_space *space;
7938 isl_aff *aff;
7940 space = isl_union_set_get_space(domain);
7941 space = isl_space_add_param_id(space, isl_id_copy(id));
7942 aff = isl_aff_param_on_domain_space_id(space, id);
7943 return isl_union_pw_aff_aff_on_domain(domain, aff);
7946 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7947 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7948 * needs to attain.
7949 * "res" collects the results.
7951 struct isl_union_pw_aff_pw_aff_on_domain_data {
7952 isl_pw_aff *pa;
7953 isl_union_pw_aff *res;
7956 /* Construct a piecewise affine expression that is equal to data->pa
7957 * on "domain" and add the result to data->res.
7959 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7961 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7962 isl_pw_aff *pa;
7963 isl_size dim;
7965 pa = isl_pw_aff_copy(data->pa);
7966 dim = isl_set_dim(domain, isl_dim_set);
7967 if (dim < 0)
7968 pa = isl_pw_aff_free(pa);
7969 pa = isl_pw_aff_from_range(pa);
7970 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7971 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7972 pa = isl_pw_aff_intersect_domain(pa, domain);
7973 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7975 return data->res ? isl_stat_ok : isl_stat_error;
7978 /* Return a union piecewise affine expression
7979 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7980 * have been aligned.
7982 * Construct an isl_pw_aff on each of the sets in "domain" and
7983 * collect the results.
7985 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7986 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7988 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7989 isl_space *space;
7991 space = isl_union_set_get_space(domain);
7992 data.res = isl_union_pw_aff_empty(space);
7993 data.pa = pa;
7994 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7995 data.res = isl_union_pw_aff_free(data.res);
7996 isl_union_set_free(domain);
7997 isl_pw_aff_free(pa);
7998 return data.res;
8001 /* Return a union piecewise affine expression
8002 * that is equal to "pa" on "domain".
8004 * Check that "pa" is a parametric expression,
8005 * align the parameters if needed and call
8006 * isl_union_pw_aff_pw_aff_on_domain_aligned.
8008 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
8009 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
8011 isl_bool is_set;
8012 isl_bool equal_params;
8013 isl_space *domain_space, *pa_space;
8015 pa_space = isl_pw_aff_peek_space(pa);
8016 is_set = isl_space_is_set(pa_space);
8017 if (is_set < 0)
8018 goto error;
8019 if (!is_set)
8020 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
8021 "expecting parametric expression", goto error);
8023 domain_space = isl_union_set_get_space(domain);
8024 pa_space = isl_pw_aff_get_space(pa);
8025 equal_params = isl_space_has_equal_params(domain_space, pa_space);
8026 if (equal_params >= 0 && !equal_params) {
8027 isl_space *space;
8029 space = isl_space_align_params(domain_space, pa_space);
8030 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
8031 domain = isl_union_set_align_params(domain, space);
8032 } else {
8033 isl_space_free(domain_space);
8034 isl_space_free(pa_space);
8037 if (equal_params < 0)
8038 goto error;
8039 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
8040 error:
8041 isl_union_set_free(domain);
8042 isl_pw_aff_free(pa);
8043 return NULL;
8046 /* Internal data structure for isl_union_pw_aff_val_on_domain.
8047 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
8048 * "res" collects the results.
8050 struct isl_union_pw_aff_val_on_domain_data {
8051 isl_val *v;
8052 isl_union_pw_aff *res;
8055 /* Construct a piecewise affine expression that is equal to data->v
8056 * on "domain" and add the result to data->res.
8058 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
8060 struct isl_union_pw_aff_val_on_domain_data *data = user;
8061 isl_pw_aff *pa;
8062 isl_val *v;
8064 v = isl_val_copy(data->v);
8065 pa = isl_pw_aff_val_on_domain(domain, v);
8066 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8068 return data->res ? isl_stat_ok : isl_stat_error;
8071 /* Return a union piecewise affine expression
8072 * that is equal to "v" on "domain".
8074 * Construct an isl_pw_aff on each of the sets in "domain" and
8075 * collect the results.
8077 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
8078 __isl_take isl_union_set *domain, __isl_take isl_val *v)
8080 struct isl_union_pw_aff_val_on_domain_data data;
8081 isl_space *space;
8083 space = isl_union_set_get_space(domain);
8084 data.res = isl_union_pw_aff_empty(space);
8085 data.v = v;
8086 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
8087 data.res = isl_union_pw_aff_free(data.res);
8088 isl_union_set_free(domain);
8089 isl_val_free(v);
8090 return data.res;
8093 /* Construct a piecewise multi affine expression
8094 * that is equal to "pa" and add it to upma.
8096 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
8097 void *user)
8099 isl_union_pw_multi_aff **upma = user;
8100 isl_pw_multi_aff *pma;
8102 pma = isl_pw_multi_aff_from_pw_aff(pa);
8103 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
8105 return *upma ? isl_stat_ok : isl_stat_error;
8108 /* Construct and return a union piecewise multi affine expression
8109 * that is equal to the given union piecewise affine expression.
8111 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
8112 __isl_take isl_union_pw_aff *upa)
8114 isl_space *space;
8115 isl_union_pw_multi_aff *upma;
8117 if (!upa)
8118 return NULL;
8120 space = isl_union_pw_aff_get_space(upa);
8121 upma = isl_union_pw_multi_aff_empty(space);
8123 if (isl_union_pw_aff_foreach_pw_aff(upa,
8124 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
8125 upma = isl_union_pw_multi_aff_free(upma);
8127 isl_union_pw_aff_free(upa);
8128 return upma;
8131 /* Compute the set of elements in the domain of "pa" where it is zero and
8132 * add this set to "uset".
8134 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
8136 isl_union_set **uset = (isl_union_set **)user;
8138 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
8140 return *uset ? isl_stat_ok : isl_stat_error;
8143 /* Return a union set containing those elements in the domain
8144 * of "upa" where it is zero.
8146 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
8147 __isl_take isl_union_pw_aff *upa)
8149 isl_union_set *zero;
8151 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8152 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
8153 zero = isl_union_set_free(zero);
8155 isl_union_pw_aff_free(upa);
8156 return zero;
8159 /* Internal data structure for isl_union_pw_aff_bind_id,
8160 * storing the parameter that needs to be bound and
8161 * the accumulated results.
8163 struct isl_bind_id_data {
8164 isl_id *id;
8165 isl_union_set *bound;
8168 /* Bind the piecewise affine function "pa" to the parameter data->id,
8169 * adding the resulting elements in the domain where the expression
8170 * is equal to the parameter to data->bound.
8172 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
8174 struct isl_bind_id_data *data = user;
8175 isl_set *bound;
8177 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
8178 data->bound = isl_union_set_add_set(data->bound, bound);
8180 return data->bound ? isl_stat_ok : isl_stat_error;
8183 /* Bind the union piecewise affine function "upa" to the parameter "id",
8184 * returning the elements in the domain where the expression
8185 * is equal to the parameter.
8187 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
8188 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
8190 struct isl_bind_id_data data = { id };
8192 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
8193 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
8194 data.bound = isl_union_set_free(data.bound);
8196 isl_union_pw_aff_free(upa);
8197 isl_id_free(id);
8198 return data.bound;
8201 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8202 * upma is the function that is plugged in.
8203 * pa is the current part of the function in which upma is plugged in.
8204 * res collects the results.
8206 struct isl_union_pw_aff_pullback_upma_data {
8207 isl_union_pw_multi_aff *upma;
8208 isl_pw_aff *pa;
8209 isl_union_pw_aff *res;
8212 /* Check if "pma" can be plugged into data->pa.
8213 * If so, perform the pullback and add the result to data->res.
8215 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8217 struct isl_union_pw_aff_pullback_upma_data *data = user;
8218 isl_pw_aff *pa;
8220 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8221 pma->dim, isl_dim_out)) {
8222 isl_pw_multi_aff_free(pma);
8223 return isl_stat_ok;
8226 pa = isl_pw_aff_copy(data->pa);
8227 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8229 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8231 return data->res ? isl_stat_ok : isl_stat_error;
8234 /* Check if any of the elements of data->upma can be plugged into pa,
8235 * add if so add the result to data->res.
8237 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8239 struct isl_union_pw_aff_pullback_upma_data *data = user;
8240 isl_stat r;
8242 data->pa = pa;
8243 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8244 &pa_pb_pma, data);
8245 isl_pw_aff_free(pa);
8247 return r;
8250 /* Compute the pullback of "upa" by the function represented by "upma".
8251 * In other words, plug in "upma" in "upa". The result contains
8252 * expressions defined over the domain space of "upma".
8254 * Run over all pairs of elements in "upa" and "upma", perform
8255 * the pullback when appropriate and collect the results.
8256 * If the hash value were based on the domain space rather than
8257 * the function space, then we could run through all elements
8258 * of "upma" and directly pick out the corresponding element of "upa".
8260 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8261 __isl_take isl_union_pw_aff *upa,
8262 __isl_take isl_union_pw_multi_aff *upma)
8264 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8265 isl_space *space;
8267 space = isl_union_pw_multi_aff_get_space(upma);
8268 upa = isl_union_pw_aff_align_params(upa, space);
8269 space = isl_union_pw_aff_get_space(upa);
8270 upma = isl_union_pw_multi_aff_align_params(upma, space);
8272 if (!upa || !upma)
8273 goto error;
8275 data.upma = upma;
8276 data.res = isl_union_pw_aff_alloc_same_size(upa);
8277 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8278 data.res = isl_union_pw_aff_free(data.res);
8280 isl_union_pw_aff_free(upa);
8281 isl_union_pw_multi_aff_free(upma);
8282 return data.res;
8283 error:
8284 isl_union_pw_aff_free(upa);
8285 isl_union_pw_multi_aff_free(upma);
8286 return NULL;
8289 #undef BASE
8290 #define BASE union_pw_aff
8291 #undef DOMBASE
8292 #define DOMBASE union_set
8294 #include <isl_multi_explicit_domain.c>
8295 #include <isl_multi_union_pw_aff_explicit_domain.c>
8296 #include <isl_multi_templ.c>
8297 #include <isl_multi_apply_set.c>
8298 #include <isl_multi_apply_union_set.c>
8299 #include <isl_multi_arith_templ.c>
8300 #include <isl_multi_bind_templ.c>
8301 #include <isl_multi_coalesce.c>
8302 #include <isl_multi_dim_id_templ.c>
8303 #include <isl_multi_floor.c>
8304 #include <isl_multi_from_base_templ.c>
8305 #include <isl_multi_gist.c>
8306 #include <isl_multi_align_set.c>
8307 #include <isl_multi_align_union_set.c>
8308 #include <isl_multi_intersect.c>
8309 #include <isl_multi_nan_templ.c>
8310 #include <isl_multi_tuple_id_templ.c>
8311 #include <isl_multi_union_add_templ.c>
8313 /* Does "mupa" have a non-trivial explicit domain?
8315 * The explicit domain, if present, is trivial if it represents
8316 * an (obviously) universe parameter set.
8318 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8319 __isl_keep isl_multi_union_pw_aff *mupa)
8321 isl_bool is_params, trivial;
8322 isl_set *set;
8324 if (!mupa)
8325 return isl_bool_error;
8326 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8327 return isl_bool_false;
8328 is_params = isl_union_set_is_params(mupa->u.dom);
8329 if (is_params < 0 || !is_params)
8330 return isl_bool_not(is_params);
8331 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8332 trivial = isl_set_plain_is_universe(set);
8333 isl_set_free(set);
8334 return isl_bool_not(trivial);
8337 /* Construct a multiple union piecewise affine expression
8338 * in the given space with value zero in each of the output dimensions.
8340 * Since there is no canonical zero value for
8341 * a union piecewise affine expression, we can only construct
8342 * a zero-dimensional "zero" value.
8344 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8345 __isl_take isl_space *space)
8347 isl_bool params;
8348 isl_size dim;
8350 if (!space)
8351 return NULL;
8353 params = isl_space_is_params(space);
8354 if (params < 0)
8355 goto error;
8356 if (params)
8357 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8358 "expecting proper set space", goto error);
8359 if (!isl_space_is_set(space))
8360 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8361 "expecting set space", goto error);
8362 dim = isl_space_dim(space, isl_dim_out);
8363 if (dim < 0)
8364 goto error;
8365 if (dim != 0)
8366 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8367 "expecting 0D space", goto error);
8369 return isl_multi_union_pw_aff_alloc(space);
8370 error:
8371 isl_space_free(space);
8372 return NULL;
8375 /* Construct and return a multi union piecewise affine expression
8376 * that is equal to the given multi affine expression.
8378 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8379 __isl_take isl_multi_aff *ma)
8381 isl_multi_pw_aff *mpa;
8383 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8384 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8387 /* Construct and return a multi union piecewise affine expression
8388 * that is equal to the given multi piecewise affine expression.
8390 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8391 __isl_take isl_multi_pw_aff *mpa)
8393 int i;
8394 isl_size n;
8395 isl_space *space;
8396 isl_multi_union_pw_aff *mupa;
8398 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8399 if (n < 0)
8400 mpa = isl_multi_pw_aff_free(mpa);
8401 if (!mpa)
8402 return NULL;
8404 space = isl_multi_pw_aff_get_space(mpa);
8405 space = isl_space_range(space);
8406 mupa = isl_multi_union_pw_aff_alloc(space);
8408 for (i = 0; i < n; ++i) {
8409 isl_pw_aff *pa;
8410 isl_union_pw_aff *upa;
8412 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8413 upa = isl_union_pw_aff_from_pw_aff(pa);
8414 mupa = isl_multi_union_pw_aff_restore_check_space(mupa, i, upa);
8417 isl_multi_pw_aff_free(mpa);
8419 return mupa;
8422 /* Extract the range space of "pma" and assign it to *space.
8423 * If *space has already been set (through a previous call to this function),
8424 * then check that the range space is the same.
8426 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8428 isl_space **space = user;
8429 isl_space *pma_space;
8430 isl_bool equal;
8432 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8433 isl_pw_multi_aff_free(pma);
8435 if (!pma_space)
8436 return isl_stat_error;
8437 if (!*space) {
8438 *space = pma_space;
8439 return isl_stat_ok;
8442 equal = isl_space_is_equal(pma_space, *space);
8443 isl_space_free(pma_space);
8445 if (equal < 0)
8446 return isl_stat_error;
8447 if (!equal)
8448 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8449 "range spaces not the same", return isl_stat_error);
8450 return isl_stat_ok;
8453 /* Construct and return a multi union piecewise affine expression
8454 * that is equal to the given union piecewise multi affine expression.
8456 * In order to be able to perform the conversion, the input
8457 * needs to be non-empty and may only involve a single range space.
8459 * If the resulting multi union piecewise affine expression has
8460 * an explicit domain, then assign it the domain of the input.
8461 * In other cases, the domain is stored in the individual elements.
8463 __isl_give isl_multi_union_pw_aff *
8464 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8465 __isl_take isl_union_pw_multi_aff *upma)
8467 isl_space *space = NULL;
8468 isl_multi_union_pw_aff *mupa;
8469 int i;
8470 isl_size n;
8472 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8473 if (n < 0)
8474 goto error;
8475 if (n == 0)
8476 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8477 "cannot extract range space from empty input",
8478 goto error);
8479 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8480 &space) < 0)
8481 goto error;
8483 if (!space)
8484 goto error;
8486 n = isl_space_dim(space, isl_dim_set);
8487 if (n < 0)
8488 space = isl_space_free(space);
8489 mupa = isl_multi_union_pw_aff_alloc(space);
8491 for (i = 0; i < n; ++i) {
8492 isl_union_pw_aff *upa;
8494 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8495 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8497 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8498 isl_union_set *dom;
8499 isl_union_pw_multi_aff *copy;
8501 copy = isl_union_pw_multi_aff_copy(upma);
8502 dom = isl_union_pw_multi_aff_domain(copy);
8503 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8506 isl_union_pw_multi_aff_free(upma);
8507 return mupa;
8508 error:
8509 isl_space_free(space);
8510 isl_union_pw_multi_aff_free(upma);
8511 return NULL;
8514 /* Try and create an isl_multi_union_pw_aff that is equivalent
8515 * to the given isl_union_map.
8516 * The isl_union_map is required to be single-valued in each space.
8517 * Moreover, it cannot be empty and all range spaces need to be the same.
8518 * Otherwise, an error is produced.
8520 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8521 __isl_take isl_union_map *umap)
8523 isl_union_pw_multi_aff *upma;
8525 upma = isl_union_pw_multi_aff_from_union_map(umap);
8526 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8529 /* Return a multiple union piecewise affine expression
8530 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8531 * have been aligned.
8533 * If the resulting multi union piecewise affine expression has
8534 * an explicit domain, then assign it the input domain.
8535 * In other cases, the domain is stored in the individual elements.
8537 static __isl_give isl_multi_union_pw_aff *
8538 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8539 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8541 int i;
8542 isl_size n;
8543 isl_space *space;
8544 isl_multi_union_pw_aff *mupa;
8546 n = isl_multi_val_dim(mv, isl_dim_set);
8547 if (!domain || n < 0)
8548 goto error;
8550 space = isl_multi_val_get_space(mv);
8551 mupa = isl_multi_union_pw_aff_alloc(space);
8552 for (i = 0; i < n; ++i) {
8553 isl_val *v;
8554 isl_union_pw_aff *upa;
8556 v = isl_multi_val_get_val(mv, i);
8557 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8559 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8561 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8562 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8563 isl_union_set_copy(domain));
8565 isl_union_set_free(domain);
8566 isl_multi_val_free(mv);
8567 return mupa;
8568 error:
8569 isl_union_set_free(domain);
8570 isl_multi_val_free(mv);
8571 return NULL;
8574 /* Return a multiple union piecewise affine expression
8575 * that is equal to "mv" on "domain".
8577 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8578 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8580 isl_bool equal_params;
8582 if (!domain || !mv)
8583 goto error;
8584 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8585 if (equal_params < 0)
8586 goto error;
8587 if (equal_params)
8588 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8589 domain, mv);
8590 domain = isl_union_set_align_params(domain,
8591 isl_multi_val_get_space(mv));
8592 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8593 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8594 error:
8595 isl_union_set_free(domain);
8596 isl_multi_val_free(mv);
8597 return NULL;
8600 /* Return a multiple union piecewise affine expression
8601 * that is equal to "ma" on "domain".
8603 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8604 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8606 isl_pw_multi_aff *pma;
8608 pma = isl_pw_multi_aff_from_multi_aff(ma);
8609 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8612 /* Return a multiple union piecewise affine expression
8613 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8614 * have been aligned.
8616 * If the resulting multi union piecewise affine expression has
8617 * an explicit domain, then assign it the input domain.
8618 * In other cases, the domain is stored in the individual elements.
8620 static __isl_give isl_multi_union_pw_aff *
8621 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8622 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8624 int i;
8625 isl_size n;
8626 isl_space *space;
8627 isl_multi_union_pw_aff *mupa;
8629 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8630 if (!domain || n < 0)
8631 goto error;
8632 space = isl_pw_multi_aff_get_space(pma);
8633 mupa = isl_multi_union_pw_aff_alloc(space);
8634 for (i = 0; i < n; ++i) {
8635 isl_pw_aff *pa;
8636 isl_union_pw_aff *upa;
8638 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8639 upa = isl_union_pw_aff_pw_aff_on_domain(
8640 isl_union_set_copy(domain), pa);
8641 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8643 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8644 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8645 isl_union_set_copy(domain));
8647 isl_union_set_free(domain);
8648 isl_pw_multi_aff_free(pma);
8649 return mupa;
8650 error:
8651 isl_union_set_free(domain);
8652 isl_pw_multi_aff_free(pma);
8653 return NULL;
8656 /* Return a multiple union piecewise affine expression
8657 * that is equal to "pma" on "domain".
8659 __isl_give isl_multi_union_pw_aff *
8660 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8661 __isl_take isl_pw_multi_aff *pma)
8663 isl_bool equal_params;
8664 isl_space *space;
8666 space = isl_pw_multi_aff_peek_space(pma);
8667 equal_params = isl_union_set_space_has_equal_params(domain, space);
8668 if (equal_params < 0)
8669 goto error;
8670 if (equal_params)
8671 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8672 domain, pma);
8673 domain = isl_union_set_align_params(domain,
8674 isl_pw_multi_aff_get_space(pma));
8675 pma = isl_pw_multi_aff_align_params(pma,
8676 isl_union_set_get_space(domain));
8677 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8678 pma);
8679 error:
8680 isl_union_set_free(domain);
8681 isl_pw_multi_aff_free(pma);
8682 return NULL;
8685 /* Return a union set containing those elements in the domains
8686 * of the elements of "mupa" where they are all zero.
8688 * If there are no elements, then simply return the entire domain.
8690 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8691 __isl_take isl_multi_union_pw_aff *mupa)
8693 int i;
8694 isl_size n;
8695 isl_union_pw_aff *upa;
8696 isl_union_set *zero;
8698 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8699 if (n < 0)
8700 mupa = isl_multi_union_pw_aff_free(mupa);
8701 if (!mupa)
8702 return NULL;
8704 if (n == 0)
8705 return isl_multi_union_pw_aff_domain(mupa);
8707 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8708 zero = isl_union_pw_aff_zero_union_set(upa);
8710 for (i = 1; i < n; ++i) {
8711 isl_union_set *zero_i;
8713 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8714 zero_i = isl_union_pw_aff_zero_union_set(upa);
8716 zero = isl_union_set_intersect(zero, zero_i);
8719 isl_multi_union_pw_aff_free(mupa);
8720 return zero;
8723 /* Construct a union map mapping the shared domain
8724 * of the union piecewise affine expressions to the range of "mupa"
8725 * in the special case of a 0D multi union piecewise affine expression.
8727 * Construct a map between the explicit domain of "mupa" and
8728 * the range space.
8729 * Note that this assumes that the domain consists of explicit elements.
8731 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8732 __isl_take isl_multi_union_pw_aff *mupa)
8734 isl_bool is_params;
8735 isl_space *space;
8736 isl_union_set *dom, *ran;
8738 space = isl_multi_union_pw_aff_get_space(mupa);
8739 dom = isl_multi_union_pw_aff_domain(mupa);
8740 ran = isl_union_set_from_set(isl_set_universe(space));
8742 is_params = isl_union_set_is_params(dom);
8743 if (is_params < 0)
8744 dom = isl_union_set_free(dom);
8745 else if (is_params)
8746 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8747 "cannot create union map from expression without "
8748 "explicit domain elements",
8749 dom = isl_union_set_free(dom));
8751 return isl_union_map_from_domain_and_range(dom, ran);
8754 /* Construct a union map mapping the shared domain
8755 * of the union piecewise affine expressions to the range of "mupa"
8756 * with each dimension in the range equated to the
8757 * corresponding union piecewise affine expression.
8759 * If the input is zero-dimensional, then construct a mapping
8760 * from its explicit domain.
8762 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8763 __isl_take isl_multi_union_pw_aff *mupa)
8765 int i;
8766 isl_size n;
8767 isl_space *space;
8768 isl_union_map *umap;
8769 isl_union_pw_aff *upa;
8771 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8772 if (n < 0)
8773 mupa = isl_multi_union_pw_aff_free(mupa);
8774 if (!mupa)
8775 return NULL;
8777 if (n == 0)
8778 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8780 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8781 umap = isl_union_map_from_union_pw_aff(upa);
8783 for (i = 1; i < n; ++i) {
8784 isl_union_map *umap_i;
8786 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8787 umap_i = isl_union_map_from_union_pw_aff(upa);
8788 umap = isl_union_map_flat_range_product(umap, umap_i);
8791 space = isl_multi_union_pw_aff_get_space(mupa);
8792 umap = isl_union_map_reset_range_space(umap, space);
8794 isl_multi_union_pw_aff_free(mupa);
8795 return umap;
8798 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8799 * "range" is the space from which to set the range space.
8800 * "res" collects the results.
8802 struct isl_union_pw_multi_aff_reset_range_space_data {
8803 isl_space *range;
8804 isl_union_pw_multi_aff *res;
8807 /* Replace the range space of "pma" by the range space of data->range and
8808 * add the result to data->res.
8810 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8812 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8813 isl_space *space;
8815 space = isl_pw_multi_aff_get_space(pma);
8816 space = isl_space_domain(space);
8817 space = isl_space_extend_domain_with_range(space,
8818 isl_space_copy(data->range));
8819 pma = isl_pw_multi_aff_reset_space(pma, space);
8820 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8822 return data->res ? isl_stat_ok : isl_stat_error;
8825 /* Replace the range space of all the piecewise affine expressions in "upma" by
8826 * the range space of "space".
8828 * This assumes that all these expressions have the same output dimension.
8830 * Since the spaces of the expressions change, so do their hash values.
8831 * We therefore need to create a new isl_union_pw_multi_aff.
8832 * Note that the hash value is currently computed based on the entire
8833 * space even though there can only be a single expression with a given
8834 * domain space.
8836 static __isl_give isl_union_pw_multi_aff *
8837 isl_union_pw_multi_aff_reset_range_space(
8838 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8840 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8841 isl_space *space_upma;
8843 space_upma = isl_union_pw_multi_aff_get_space(upma);
8844 data.res = isl_union_pw_multi_aff_empty(space_upma);
8845 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8846 &reset_range_space, &data) < 0)
8847 data.res = isl_union_pw_multi_aff_free(data.res);
8849 isl_space_free(space);
8850 isl_union_pw_multi_aff_free(upma);
8851 return data.res;
8854 /* Construct and return a union piecewise multi affine expression
8855 * that is equal to the given multi union piecewise affine expression,
8856 * in the special case of a 0D multi union piecewise affine expression.
8858 * Construct a union piecewise multi affine expression
8859 * on top of the explicit domain of the input.
8861 __isl_give isl_union_pw_multi_aff *
8862 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8863 __isl_take isl_multi_union_pw_aff *mupa)
8865 isl_space *space;
8866 isl_multi_val *mv;
8867 isl_union_set *domain;
8869 space = isl_multi_union_pw_aff_get_space(mupa);
8870 mv = isl_multi_val_zero(space);
8871 domain = isl_multi_union_pw_aff_domain(mupa);
8872 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8875 /* Construct and return a union piecewise multi affine expression
8876 * that is equal to the given multi union piecewise affine expression.
8878 * If the input is zero-dimensional, then
8879 * construct a union piecewise multi affine expression
8880 * on top of the explicit domain of the input.
8882 __isl_give isl_union_pw_multi_aff *
8883 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8884 __isl_take isl_multi_union_pw_aff *mupa)
8886 int i;
8887 isl_size n;
8888 isl_space *space;
8889 isl_union_pw_multi_aff *upma;
8890 isl_union_pw_aff *upa;
8892 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8893 if (n < 0)
8894 mupa = isl_multi_union_pw_aff_free(mupa);
8895 if (!mupa)
8896 return NULL;
8898 if (n == 0)
8899 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8901 space = isl_multi_union_pw_aff_get_space(mupa);
8902 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8903 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8905 for (i = 1; i < n; ++i) {
8906 isl_union_pw_multi_aff *upma_i;
8908 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8909 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8910 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8913 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8915 isl_multi_union_pw_aff_free(mupa);
8916 return upma;
8919 /* Intersect the range of "mupa" with "range",
8920 * in the special case where "mupa" is 0D.
8922 * Intersect the domain of "mupa" with the constraints on the parameters
8923 * of "range".
8925 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8926 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8928 range = isl_set_params(range);
8929 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8930 return mupa;
8933 /* Intersect the range of "mupa" with "range".
8934 * That is, keep only those domain elements that have a function value
8935 * in "range".
8937 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8938 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8940 isl_union_pw_multi_aff *upma;
8941 isl_union_set *domain;
8942 isl_space *space;
8943 isl_size n;
8944 int match;
8946 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8947 if (n < 0 || !range)
8948 goto error;
8950 space = isl_set_get_space(range);
8951 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8952 space, isl_dim_set);
8953 isl_space_free(space);
8954 if (match < 0)
8955 goto error;
8956 if (!match)
8957 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8958 "space don't match", goto error);
8959 if (n == 0)
8960 return mupa_intersect_range_0D(mupa, range);
8962 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8963 isl_multi_union_pw_aff_copy(mupa));
8964 domain = isl_union_set_from_set(range);
8965 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8966 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8968 return mupa;
8969 error:
8970 isl_multi_union_pw_aff_free(mupa);
8971 isl_set_free(range);
8972 return NULL;
8975 /* Return the shared domain of the elements of "mupa",
8976 * in the special case where "mupa" is zero-dimensional.
8978 * Return the explicit domain of "mupa".
8979 * Note that this domain may be a parameter set, either
8980 * because "mupa" is meant to live in a set space or
8981 * because no explicit domain has been set.
8983 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8984 __isl_take isl_multi_union_pw_aff *mupa)
8986 isl_union_set *dom;
8988 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8989 isl_multi_union_pw_aff_free(mupa);
8991 return dom;
8994 /* Return the shared domain of the elements of "mupa".
8996 * If "mupa" is zero-dimensional, then return its explicit domain.
8998 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8999 __isl_take isl_multi_union_pw_aff *mupa)
9001 int i;
9002 isl_size n;
9003 isl_union_pw_aff *upa;
9004 isl_union_set *dom;
9006 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9007 if (n < 0)
9008 mupa = isl_multi_union_pw_aff_free(mupa);
9009 if (!mupa)
9010 return NULL;
9012 if (n == 0)
9013 return isl_multi_union_pw_aff_domain_0D(mupa);
9015 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
9016 dom = isl_union_pw_aff_domain(upa);
9017 for (i = 1; i < n; ++i) {
9018 isl_union_set *dom_i;
9020 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9021 dom_i = isl_union_pw_aff_domain(upa);
9022 dom = isl_union_set_intersect(dom, dom_i);
9025 isl_multi_union_pw_aff_free(mupa);
9026 return dom;
9029 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
9030 * In particular, the spaces have been aligned.
9031 * The result is defined over the shared domain of the elements of "mupa"
9033 * We first extract the parametric constant part of "aff" and
9034 * define that over the shared domain.
9035 * Then we iterate over all input dimensions of "aff" and add the corresponding
9036 * multiples of the elements of "mupa".
9037 * Finally, we consider the integer divisions, calling the function
9038 * recursively to obtain an isl_union_pw_aff corresponding to the
9039 * integer division argument.
9041 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
9042 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9044 int i;
9045 isl_size n_in, n_div;
9046 isl_union_pw_aff *upa;
9047 isl_union_set *uset;
9048 isl_val *v;
9049 isl_aff *cst;
9051 n_in = isl_aff_dim(aff, isl_dim_in);
9052 n_div = isl_aff_dim(aff, isl_dim_div);
9053 if (n_in < 0 || n_div < 0)
9054 goto error;
9056 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
9057 cst = isl_aff_copy(aff);
9058 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
9059 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
9060 cst = isl_aff_project_domain_on_params(cst);
9061 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
9063 for (i = 0; i < n_in; ++i) {
9064 isl_union_pw_aff *upa_i;
9066 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
9067 continue;
9068 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
9069 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9070 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9071 upa = isl_union_pw_aff_add(upa, upa_i);
9074 for (i = 0; i < n_div; ++i) {
9075 isl_aff *div;
9076 isl_union_pw_aff *upa_i;
9078 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
9079 continue;
9080 div = isl_aff_get_div(aff, i);
9081 upa_i = multi_union_pw_aff_apply_aff(
9082 isl_multi_union_pw_aff_copy(mupa), div);
9083 upa_i = isl_union_pw_aff_floor(upa_i);
9084 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
9085 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
9086 upa = isl_union_pw_aff_add(upa, upa_i);
9089 isl_multi_union_pw_aff_free(mupa);
9090 isl_aff_free(aff);
9092 return upa;
9093 error:
9094 isl_multi_union_pw_aff_free(mupa);
9095 isl_aff_free(aff);
9096 return NULL;
9099 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
9100 * with the domain of "aff".
9101 * Furthermore, the dimension of this space needs to be greater than zero.
9102 * The result is defined over the shared domain of the elements of "mupa"
9104 * We perform these checks and then hand over control to
9105 * multi_union_pw_aff_apply_aff.
9107 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
9108 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
9110 isl_size dim;
9111 isl_space *space1, *space2;
9112 isl_bool equal;
9114 mupa = isl_multi_union_pw_aff_align_params(mupa,
9115 isl_aff_get_space(aff));
9116 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
9117 if (!mupa || !aff)
9118 goto error;
9120 space1 = isl_multi_union_pw_aff_get_space(mupa);
9121 space2 = isl_aff_get_domain_space(aff);
9122 equal = isl_space_is_equal(space1, space2);
9123 isl_space_free(space1);
9124 isl_space_free(space2);
9125 if (equal < 0)
9126 goto error;
9127 if (!equal)
9128 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9129 "spaces don't match", goto error);
9130 dim = isl_aff_dim(aff, isl_dim_in);
9131 if (dim < 0)
9132 goto error;
9133 if (dim == 0)
9134 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9135 "cannot determine domains", goto error);
9137 return multi_union_pw_aff_apply_aff(mupa, aff);
9138 error:
9139 isl_multi_union_pw_aff_free(mupa);
9140 isl_aff_free(aff);
9141 return NULL;
9144 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
9145 * The space of "mupa" is known to be compatible with the domain of "ma".
9147 * Construct an isl_multi_union_pw_aff that is equal to "ma"
9148 * on the domain of "mupa".
9150 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
9151 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9153 isl_union_set *dom;
9155 dom = isl_multi_union_pw_aff_domain(mupa);
9156 ma = isl_multi_aff_project_domain_on_params(ma);
9158 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
9161 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
9162 * with the domain of "ma".
9163 * The result is defined over the shared domain of the elements of "mupa"
9165 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
9166 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
9168 isl_space *space1, *space2;
9169 isl_multi_union_pw_aff *res;
9170 isl_bool equal;
9171 int i;
9172 isl_size n_in, n_out;
9174 mupa = isl_multi_union_pw_aff_align_params(mupa,
9175 isl_multi_aff_get_space(ma));
9176 ma = isl_multi_aff_align_params(ma,
9177 isl_multi_union_pw_aff_get_space(mupa));
9178 n_in = isl_multi_aff_dim(ma, isl_dim_in);
9179 n_out = isl_multi_aff_dim(ma, isl_dim_out);
9180 if (!mupa || n_in < 0 || n_out < 0)
9181 goto error;
9183 space1 = isl_multi_union_pw_aff_get_space(mupa);
9184 space2 = isl_multi_aff_get_domain_space(ma);
9185 equal = isl_space_is_equal(space1, space2);
9186 isl_space_free(space1);
9187 isl_space_free(space2);
9188 if (equal < 0)
9189 goto error;
9190 if (!equal)
9191 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
9192 "spaces don't match", goto error);
9193 if (n_in == 0)
9194 return mupa_apply_multi_aff_0D(mupa, ma);
9196 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9197 res = isl_multi_union_pw_aff_alloc(space1);
9199 for (i = 0; i < n_out; ++i) {
9200 isl_aff *aff;
9201 isl_union_pw_aff *upa;
9203 aff = isl_multi_aff_get_aff(ma, i);
9204 upa = multi_union_pw_aff_apply_aff(
9205 isl_multi_union_pw_aff_copy(mupa), aff);
9206 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9209 isl_multi_aff_free(ma);
9210 isl_multi_union_pw_aff_free(mupa);
9211 return res;
9212 error:
9213 isl_multi_union_pw_aff_free(mupa);
9214 isl_multi_aff_free(ma);
9215 return NULL;
9218 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9219 * The space of "mupa" is known to be compatible with the domain of "pa".
9221 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9222 * on the domain of "mupa".
9224 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9225 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9227 isl_union_set *dom;
9229 dom = isl_multi_union_pw_aff_domain(mupa);
9230 pa = isl_pw_aff_project_domain_on_params(pa);
9232 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9235 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9236 * with the domain of "pa".
9237 * Furthermore, the dimension of this space needs to be greater than zero.
9238 * The result is defined over the shared domain of the elements of "mupa"
9240 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9241 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9243 int i;
9244 isl_bool equal;
9245 isl_size n_in;
9246 isl_space *space, *space2;
9247 isl_union_pw_aff *upa;
9249 mupa = isl_multi_union_pw_aff_align_params(mupa,
9250 isl_pw_aff_get_space(pa));
9251 pa = isl_pw_aff_align_params(pa,
9252 isl_multi_union_pw_aff_get_space(mupa));
9253 if (!mupa || !pa)
9254 goto error;
9256 space = isl_multi_union_pw_aff_get_space(mupa);
9257 space2 = isl_pw_aff_get_domain_space(pa);
9258 equal = isl_space_is_equal(space, space2);
9259 isl_space_free(space);
9260 isl_space_free(space2);
9261 if (equal < 0)
9262 goto error;
9263 if (!equal)
9264 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9265 "spaces don't match", goto error);
9266 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9267 if (n_in < 0)
9268 goto error;
9269 if (n_in == 0)
9270 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9272 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9273 upa = isl_union_pw_aff_empty(space);
9275 for (i = 0; i < pa->n; ++i) {
9276 isl_aff *aff;
9277 isl_set *domain;
9278 isl_multi_union_pw_aff *mupa_i;
9279 isl_union_pw_aff *upa_i;
9281 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9282 domain = isl_set_copy(pa->p[i].set);
9283 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9284 aff = isl_aff_copy(pa->p[i].aff);
9285 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9286 upa = isl_union_pw_aff_union_add(upa, upa_i);
9289 isl_multi_union_pw_aff_free(mupa);
9290 isl_pw_aff_free(pa);
9291 return upa;
9292 error:
9293 isl_multi_union_pw_aff_free(mupa);
9294 isl_pw_aff_free(pa);
9295 return NULL;
9298 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9299 * The space of "mupa" is known to be compatible with the domain of "pma".
9301 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9302 * on the domain of "mupa".
9304 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9305 __isl_take isl_multi_union_pw_aff *mupa,
9306 __isl_take isl_pw_multi_aff *pma)
9308 isl_union_set *dom;
9310 dom = isl_multi_union_pw_aff_domain(mupa);
9311 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9313 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9316 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9317 * with the domain of "pma".
9318 * The result is defined over the shared domain of the elements of "mupa"
9320 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9321 __isl_take isl_multi_union_pw_aff *mupa,
9322 __isl_take isl_pw_multi_aff *pma)
9324 isl_space *space1, *space2;
9325 isl_multi_union_pw_aff *res;
9326 isl_bool equal;
9327 int i;
9328 isl_size n_in, n_out;
9330 mupa = isl_multi_union_pw_aff_align_params(mupa,
9331 isl_pw_multi_aff_get_space(pma));
9332 pma = isl_pw_multi_aff_align_params(pma,
9333 isl_multi_union_pw_aff_get_space(mupa));
9334 if (!mupa || !pma)
9335 goto error;
9337 space1 = isl_multi_union_pw_aff_get_space(mupa);
9338 space2 = isl_pw_multi_aff_get_domain_space(pma);
9339 equal = isl_space_is_equal(space1, space2);
9340 isl_space_free(space1);
9341 isl_space_free(space2);
9342 if (equal < 0)
9343 goto error;
9344 if (!equal)
9345 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9346 "spaces don't match", goto error);
9347 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9348 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9349 if (n_in < 0 || n_out < 0)
9350 goto error;
9351 if (n_in == 0)
9352 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9354 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9355 res = isl_multi_union_pw_aff_alloc(space1);
9357 for (i = 0; i < n_out; ++i) {
9358 isl_pw_aff *pa;
9359 isl_union_pw_aff *upa;
9361 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9362 upa = isl_multi_union_pw_aff_apply_pw_aff(
9363 isl_multi_union_pw_aff_copy(mupa), pa);
9364 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9367 isl_pw_multi_aff_free(pma);
9368 isl_multi_union_pw_aff_free(mupa);
9369 return res;
9370 error:
9371 isl_multi_union_pw_aff_free(mupa);
9372 isl_pw_multi_aff_free(pma);
9373 return NULL;
9376 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9377 * If the explicit domain only keeps track of constraints on the parameters,
9378 * then only update those constraints.
9380 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9381 __isl_take isl_multi_union_pw_aff *mupa,
9382 __isl_keep isl_union_pw_multi_aff *upma)
9384 isl_bool is_params;
9386 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9387 return isl_multi_union_pw_aff_free(mupa);
9389 mupa = isl_multi_union_pw_aff_cow(mupa);
9390 if (!mupa)
9391 return NULL;
9393 is_params = isl_union_set_is_params(mupa->u.dom);
9394 if (is_params < 0)
9395 return isl_multi_union_pw_aff_free(mupa);
9397 upma = isl_union_pw_multi_aff_copy(upma);
9398 if (is_params)
9399 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9400 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9401 else
9402 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9403 mupa->u.dom, upma);
9404 if (!mupa->u.dom)
9405 return isl_multi_union_pw_aff_free(mupa);
9406 return mupa;
9409 /* Compute the pullback of "mupa" by the function represented by "upma".
9410 * In other words, plug in "upma" in "mupa". The result contains
9411 * expressions defined over the domain space of "upma".
9413 * Run over all elements of "mupa" and plug in "upma" in each of them.
9415 * If "mupa" has an explicit domain, then it is this domain
9416 * that needs to undergo a pullback instead, i.e., a preimage.
9418 __isl_give isl_multi_union_pw_aff *
9419 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9420 __isl_take isl_multi_union_pw_aff *mupa,
9421 __isl_take isl_union_pw_multi_aff *upma)
9423 int i;
9424 isl_size n;
9426 mupa = isl_multi_union_pw_aff_align_params(mupa,
9427 isl_union_pw_multi_aff_get_space(upma));
9428 upma = isl_union_pw_multi_aff_align_params(upma,
9429 isl_multi_union_pw_aff_get_space(mupa));
9430 mupa = isl_multi_union_pw_aff_cow(mupa);
9431 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9432 if (n < 0 || !upma)
9433 goto error;
9435 for (i = 0; i < n; ++i) {
9436 isl_union_pw_aff *upa;
9438 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9439 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9440 isl_union_pw_multi_aff_copy(upma));
9441 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9444 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9445 mupa = preimage_explicit_domain(mupa, upma);
9447 isl_union_pw_multi_aff_free(upma);
9448 return mupa;
9449 error:
9450 isl_multi_union_pw_aff_free(mupa);
9451 isl_union_pw_multi_aff_free(upma);
9452 return NULL;
9455 /* Extract the sequence of elements in "mupa" with domain space "space"
9456 * (ignoring parameters).
9458 * For the elements of "mupa" that are not defined on the specified space,
9459 * the corresponding element in the result is empty.
9461 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9462 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9464 int i;
9465 isl_size n;
9466 isl_space *space_mpa;
9467 isl_multi_pw_aff *mpa;
9469 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9470 if (n < 0 || !space)
9471 goto error;
9473 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9474 space = isl_space_replace_params(space, space_mpa);
9475 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9476 space_mpa);
9477 mpa = isl_multi_pw_aff_alloc(space_mpa);
9479 space = isl_space_from_domain(space);
9480 space = isl_space_add_dims(space, isl_dim_out, 1);
9481 for (i = 0; i < n; ++i) {
9482 isl_union_pw_aff *upa;
9483 isl_pw_aff *pa;
9485 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9486 pa = isl_union_pw_aff_extract_pw_aff(upa,
9487 isl_space_copy(space));
9488 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9489 isl_union_pw_aff_free(upa);
9492 isl_space_free(space);
9493 return mpa;
9494 error:
9495 isl_space_free(space);
9496 return NULL;
9499 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9500 * should modify the base expressions in the input.
9502 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9503 * are taken into account.
9504 * "fn" is applied to each entry in the input.
9506 struct isl_union_pw_multi_aff_un_op_control {
9507 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9508 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9511 /* Wrapper for isl_union_pw_multi_aff_un_op filter functions (which do not take
9512 * a second argument) for use as an isl_union_pw_multi_aff_transform
9513 * filter function (which does take a second argument).
9514 * Simply call control->filter without the second argument.
9516 static isl_bool isl_union_pw_multi_aff_un_op_filter_drop_user(
9517 __isl_take isl_pw_multi_aff *pma, void *user)
9519 struct isl_union_pw_multi_aff_un_op_control *control = user;
9521 return control->filter(pma);
9524 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9525 * a second argument) for use as an isl_union_pw_multi_aff_transform
9526 * base function (which does take a second argument).
9527 * Simply call control->fn without the second argument.
9529 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9530 __isl_take isl_pw_multi_aff *pma, void *user)
9532 struct isl_union_pw_multi_aff_un_op_control *control = user;
9534 return control->fn(pma);
9537 /* Construct an isl_union_pw_multi_aff that is obtained by
9538 * modifying "upma" according to "control".
9540 * isl_union_pw_multi_aff_transform performs essentially
9541 * the same operation, but takes a filter and a callback function
9542 * of a different form (with an extra argument).
9543 * Call isl_union_pw_multi_aff_transform with wrappers
9544 * that remove this extra argument.
9546 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9547 __isl_take isl_union_pw_multi_aff *upma,
9548 struct isl_union_pw_multi_aff_un_op_control *control)
9550 struct isl_union_pw_multi_aff_transform_control t_control = {
9551 .filter = &isl_union_pw_multi_aff_un_op_filter_drop_user,
9552 .filter_user = control,
9553 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9554 .fn_user = control,
9557 return isl_union_pw_multi_aff_transform(upma, &t_control);
9560 /* For each function in "upma" of the form A -> [B -> C],
9561 * extract the function A -> B and collect the results.
9563 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9564 __isl_take isl_union_pw_multi_aff *upma)
9566 struct isl_union_pw_multi_aff_un_op_control control = {
9567 .filter = &isl_pw_multi_aff_range_is_wrapping,
9568 .fn = &isl_pw_multi_aff_range_factor_domain,
9570 return isl_union_pw_multi_aff_un_op(upma, &control);
9573 /* For each function in "upma" of the form A -> [B -> C],
9574 * extract the function A -> C and collect the results.
9576 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9577 __isl_take isl_union_pw_multi_aff *upma)
9579 struct isl_union_pw_multi_aff_un_op_control control = {
9580 .filter = &isl_pw_multi_aff_range_is_wrapping,
9581 .fn = &isl_pw_multi_aff_range_factor_range,
9583 return isl_union_pw_multi_aff_un_op(upma, &control);
9586 /* Evaluate the affine function "aff" in the void point "pnt".
9587 * In particular, return the value NaN.
9589 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9590 __isl_take isl_point *pnt)
9592 isl_ctx *ctx;
9594 ctx = isl_point_get_ctx(pnt);
9595 isl_aff_free(aff);
9596 isl_point_free(pnt);
9597 return isl_val_nan(ctx);
9600 /* Evaluate the affine expression "aff"
9601 * in the coordinates (with denominator) "pnt".
9603 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9604 __isl_keep isl_vec *pnt)
9606 isl_int n, d;
9607 isl_ctx *ctx;
9608 isl_val *v;
9610 if (!aff || !pnt)
9611 return NULL;
9613 ctx = isl_vec_get_ctx(aff);
9614 isl_int_init(n);
9615 isl_int_init(d);
9616 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9617 isl_int_mul(d, aff->el[0], pnt->el[0]);
9618 v = isl_val_rat_from_isl_int(ctx, n, d);
9619 v = isl_val_normalize(v);
9620 isl_int_clear(n);
9621 isl_int_clear(d);
9623 return v;
9626 /* Check that the domain space of "aff" is equal to "space".
9628 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9629 __isl_keep isl_space *space)
9631 isl_bool ok;
9633 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9634 if (ok < 0)
9635 return isl_stat_error;
9636 if (!ok)
9637 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9638 "incompatible spaces", return isl_stat_error);
9639 return isl_stat_ok;
9642 /* Evaluate the affine function "aff" in "pnt".
9644 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9645 __isl_take isl_point *pnt)
9647 isl_bool is_void;
9648 isl_val *v;
9649 isl_local_space *ls;
9651 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9652 goto error;
9653 is_void = isl_point_is_void(pnt);
9654 if (is_void < 0)
9655 goto error;
9656 if (is_void)
9657 return eval_void(aff, pnt);
9659 ls = isl_aff_get_domain_local_space(aff);
9660 pnt = isl_local_space_lift_point(ls, pnt);
9662 v = eval(aff->v, isl_point_peek_vec(pnt));
9664 isl_aff_free(aff);
9665 isl_point_free(pnt);
9667 return v;
9668 error:
9669 isl_aff_free(aff);
9670 isl_point_free(pnt);
9671 return NULL;