isl_codegen: skip explicit printing of outermost block by default
[isl.git] / isl_aff.c
blobd1c7aa8231d9561b419778fc9e4e4518574f045a
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_zero_on_domain(__isl_take isl_local_space *ls)
114 isl_aff *aff;
116 aff = isl_aff_alloc(ls);
117 if (!aff)
118 return NULL;
120 isl_int_set_si(aff->v->el[0], 1);
121 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
123 return aff;
126 /* Return an affine expression that is equal to zero on domain space "space".
128 __isl_give isl_aff *isl_aff_zero_on_domain_space(__isl_take isl_space *space)
130 return isl_aff_zero_on_domain(isl_local_space_from_space(space));
133 /* Return a piecewise affine expression defined on the specified domain
134 * that is equal to zero.
136 __isl_give isl_pw_aff *isl_pw_aff_zero_on_domain(__isl_take isl_local_space *ls)
138 return isl_pw_aff_from_aff(isl_aff_zero_on_domain(ls));
141 /* Return an affine expression defined on the specified domain
142 * that represents NaN.
144 __isl_give isl_aff *isl_aff_nan_on_domain(__isl_take isl_local_space *ls)
146 isl_aff *aff;
148 aff = isl_aff_alloc(ls);
149 if (!aff)
150 return NULL;
152 isl_seq_clr(aff->v->el, aff->v->size);
154 return aff;
157 /* Return a piecewise affine expression defined on the specified domain
158 * that represents NaN.
160 __isl_give isl_pw_aff *isl_pw_aff_nan_on_domain(__isl_take isl_local_space *ls)
162 return isl_pw_aff_from_aff(isl_aff_nan_on_domain(ls));
165 /* Return an affine expression that is equal to "val" on
166 * domain local space "ls".
168 __isl_give isl_aff *isl_aff_val_on_domain(__isl_take isl_local_space *ls,
169 __isl_take isl_val *val)
171 isl_aff *aff;
173 if (!ls || !val)
174 goto error;
175 if (!isl_val_is_rat(val))
176 isl_die(isl_val_get_ctx(val), isl_error_invalid,
177 "expecting rational value", goto error);
179 aff = isl_aff_alloc(isl_local_space_copy(ls));
180 if (!aff)
181 goto error;
183 isl_seq_clr(aff->v->el + 2, aff->v->size - 2);
184 isl_int_set(aff->v->el[1], val->n);
185 isl_int_set(aff->v->el[0], val->d);
187 isl_local_space_free(ls);
188 isl_val_free(val);
189 return aff;
190 error:
191 isl_local_space_free(ls);
192 isl_val_free(val);
193 return NULL;
196 /* Return an affine expression that is equal to "val" on domain space "space".
198 __isl_give isl_aff *isl_aff_val_on_domain_space(__isl_take isl_space *space,
199 __isl_take isl_val *val)
201 return isl_aff_val_on_domain(isl_local_space_from_space(space), val);
204 /* Return an affine expression that is equal to the specified dimension
205 * in "ls".
207 __isl_give isl_aff *isl_aff_var_on_domain(__isl_take isl_local_space *ls,
208 enum isl_dim_type type, unsigned pos)
210 isl_space *space;
211 isl_aff *aff;
213 if (!ls)
214 return NULL;
216 space = isl_local_space_get_space(ls);
217 if (!space)
218 goto error;
219 if (isl_space_is_map(space))
220 isl_die(isl_space_get_ctx(space), isl_error_invalid,
221 "expecting (parameter) set space", goto error);
222 if (isl_local_space_check_range(ls, type, pos, 1) < 0)
223 goto error;
225 isl_space_free(space);
226 aff = isl_aff_alloc(ls);
227 if (!aff)
228 return NULL;
230 pos += isl_local_space_offset(aff->ls, type);
232 isl_int_set_si(aff->v->el[0], 1);
233 isl_seq_clr(aff->v->el + 1, aff->v->size - 1);
234 isl_int_set_si(aff->v->el[1 + pos], 1);
236 return aff;
237 error:
238 isl_local_space_free(ls);
239 isl_space_free(space);
240 return NULL;
243 /* Return a piecewise affine expression that is equal to
244 * the specified dimension in "ls".
246 __isl_give isl_pw_aff *isl_pw_aff_var_on_domain(__isl_take isl_local_space *ls,
247 enum isl_dim_type type, unsigned pos)
249 return isl_pw_aff_from_aff(isl_aff_var_on_domain(ls, type, pos));
252 /* Return an affine expression that is equal to the parameter
253 * in the domain space "space" with identifier "id".
255 __isl_give isl_aff *isl_aff_param_on_domain_space_id(
256 __isl_take isl_space *space, __isl_take isl_id *id)
258 int pos;
259 isl_local_space *ls;
261 if (!space || !id)
262 goto error;
263 pos = isl_space_find_dim_by_id(space, isl_dim_param, id);
264 if (pos < 0)
265 isl_die(isl_space_get_ctx(space), isl_error_invalid,
266 "parameter not found in space", goto error);
267 isl_id_free(id);
268 ls = isl_local_space_from_space(space);
269 return isl_aff_var_on_domain(ls, isl_dim_param, pos);
270 error:
271 isl_space_free(space);
272 isl_id_free(id);
273 return NULL;
276 __isl_give isl_aff *isl_aff_copy(__isl_keep isl_aff *aff)
278 if (!aff)
279 return NULL;
281 aff->ref++;
282 return aff;
285 __isl_give isl_aff *isl_aff_dup(__isl_keep isl_aff *aff)
287 if (!aff)
288 return NULL;
290 return isl_aff_alloc_vec(isl_local_space_copy(aff->ls),
291 isl_vec_copy(aff->v));
294 __isl_give isl_aff *isl_aff_cow(__isl_take isl_aff *aff)
296 if (!aff)
297 return NULL;
299 if (aff->ref == 1)
300 return aff;
301 aff->ref--;
302 return isl_aff_dup(aff);
305 __isl_null isl_aff *isl_aff_free(__isl_take isl_aff *aff)
307 if (!aff)
308 return NULL;
310 if (--aff->ref > 0)
311 return NULL;
313 isl_local_space_free(aff->ls);
314 isl_vec_free(aff->v);
316 free(aff);
318 return NULL;
321 isl_ctx *isl_aff_get_ctx(__isl_keep isl_aff *aff)
323 return aff ? isl_local_space_get_ctx(aff->ls) : NULL;
326 /* Return a hash value that digests "aff".
328 uint32_t isl_aff_get_hash(__isl_keep isl_aff *aff)
330 uint32_t hash, ls_hash, v_hash;
332 if (!aff)
333 return 0;
335 hash = isl_hash_init();
336 ls_hash = isl_local_space_get_hash(aff->ls);
337 isl_hash_hash(hash, ls_hash);
338 v_hash = isl_vec_get_hash(aff->v);
339 isl_hash_hash(hash, v_hash);
341 return hash;
344 /* Return the domain local space of "aff".
346 static __isl_keep isl_local_space *isl_aff_peek_domain_local_space(
347 __isl_keep isl_aff *aff)
349 return aff ? aff->ls : NULL;
352 /* Return the number of variables of the given type in the domain of "aff".
354 isl_size isl_aff_domain_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
356 isl_local_space *ls;
358 ls = isl_aff_peek_domain_local_space(aff);
359 return isl_local_space_dim(ls, type);
362 /* Externally, an isl_aff has a map space, but internally, the
363 * ls field corresponds to the domain of that space.
365 isl_size isl_aff_dim(__isl_keep isl_aff *aff, enum isl_dim_type type)
367 if (!aff)
368 return isl_size_error;
369 if (type == isl_dim_out)
370 return 1;
371 if (type == isl_dim_in)
372 type = isl_dim_set;
373 return isl_aff_domain_dim(aff, type);
376 /* Return the offset of the first coefficient of type "type" in
377 * the domain of "aff".
379 isl_size isl_aff_domain_offset(__isl_keep isl_aff *aff, enum isl_dim_type type)
381 isl_local_space *ls;
383 ls = isl_aff_peek_domain_local_space(aff);
384 return isl_local_space_offset(ls, type);
387 /* Return the position of the dimension of the given type and name
388 * in "aff".
389 * Return -1 if no such dimension can be found.
391 int isl_aff_find_dim_by_name(__isl_keep isl_aff *aff, enum isl_dim_type type,
392 const char *name)
394 if (!aff)
395 return -1;
396 if (type == isl_dim_out)
397 return -1;
398 if (type == isl_dim_in)
399 type = isl_dim_set;
400 return isl_local_space_find_dim_by_name(aff->ls, type, name);
403 /* Return the domain space of "aff".
405 static __isl_keep isl_space *isl_aff_peek_domain_space(__isl_keep isl_aff *aff)
407 return aff ? isl_local_space_peek_space(aff->ls) : NULL;
410 __isl_give isl_space *isl_aff_get_domain_space(__isl_keep isl_aff *aff)
412 return isl_space_copy(isl_aff_peek_domain_space(aff));
415 __isl_give isl_space *isl_aff_get_space(__isl_keep isl_aff *aff)
417 isl_space *space;
418 if (!aff)
419 return NULL;
420 space = isl_local_space_get_space(aff->ls);
421 space = isl_space_from_domain(space);
422 space = isl_space_add_dims(space, isl_dim_out, 1);
423 return space;
426 /* Return a copy of the domain space of "aff".
428 __isl_give isl_local_space *isl_aff_get_domain_local_space(
429 __isl_keep isl_aff *aff)
431 return isl_local_space_copy(isl_aff_peek_domain_local_space(aff));
434 __isl_give isl_local_space *isl_aff_get_local_space(__isl_keep isl_aff *aff)
436 isl_local_space *ls;
437 if (!aff)
438 return NULL;
439 ls = isl_local_space_copy(aff->ls);
440 ls = isl_local_space_from_domain(ls);
441 ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
442 return ls;
445 /* Return the local space of the domain of "aff".
446 * This may be either a copy or the local space itself
447 * if there is only one reference to "aff".
448 * This allows the local space to be modified inplace
449 * if both the expression and its local space have only a single reference.
450 * The caller is not allowed to modify "aff" between this call and
451 * a subsequent call to isl_aff_restore_domain_local_space.
452 * The only exception is that isl_aff_free can be called instead.
454 __isl_give isl_local_space *isl_aff_take_domain_local_space(
455 __isl_keep isl_aff *aff)
457 isl_local_space *ls;
459 if (!aff)
460 return NULL;
461 if (aff->ref != 1)
462 return isl_aff_get_domain_local_space(aff);
463 ls = aff->ls;
464 aff->ls = NULL;
465 return ls;
468 /* Set the local space of the domain of "aff" to "ls",
469 * where the local space of "aff" may be missing
470 * due to a preceding call to isl_aff_take_domain_local_space.
471 * However, in this case, "aff" only has a single reference and
472 * then the call to isl_aff_cow has no effect.
474 __isl_give isl_aff *isl_aff_restore_domain_local_space(
475 __isl_keep isl_aff *aff, __isl_take isl_local_space *ls)
477 if (!aff || !ls)
478 goto error;
480 if (aff->ls == ls) {
481 isl_local_space_free(ls);
482 return aff;
485 aff = isl_aff_cow(aff);
486 if (!aff)
487 goto error;
488 isl_local_space_free(aff->ls);
489 aff->ls = ls;
491 return aff;
492 error:
493 isl_aff_free(aff);
494 isl_local_space_free(ls);
495 return NULL;
498 /* Externally, an isl_aff has a map space, but internally, the
499 * ls field corresponds to the domain of that space.
501 const char *isl_aff_get_dim_name(__isl_keep isl_aff *aff,
502 enum isl_dim_type type, unsigned pos)
504 if (!aff)
505 return NULL;
506 if (type == isl_dim_out)
507 return NULL;
508 if (type == isl_dim_in)
509 type = isl_dim_set;
510 return isl_local_space_get_dim_name(aff->ls, type, pos);
513 __isl_give isl_aff *isl_aff_reset_domain_space(__isl_take isl_aff *aff,
514 __isl_take isl_space *space)
516 aff = isl_aff_cow(aff);
517 if (!aff || !space)
518 goto error;
520 aff->ls = isl_local_space_reset_space(aff->ls, space);
521 if (!aff->ls)
522 return isl_aff_free(aff);
524 return aff;
525 error:
526 isl_aff_free(aff);
527 isl_space_free(space);
528 return NULL;
531 /* Reset the space of "aff". This function is called from isl_pw_templ.c
532 * and doesn't know if the space of an element object is represented
533 * directly or through its domain. It therefore passes along both.
535 __isl_give isl_aff *isl_aff_reset_space_and_domain(__isl_take isl_aff *aff,
536 __isl_take isl_space *space, __isl_take isl_space *domain)
538 isl_space_free(space);
539 return isl_aff_reset_domain_space(aff, domain);
542 /* Reorder the coefficients of the affine expression based
543 * on the given reordering.
544 * The reordering r is assumed to have been extended with the local
545 * variables.
547 static __isl_give isl_vec *vec_reorder(__isl_take isl_vec *vec,
548 __isl_take isl_reordering *r, int n_div)
550 isl_space *space;
551 isl_vec *res;
552 isl_size dim;
553 int i;
555 if (!vec || !r)
556 goto error;
558 space = isl_reordering_peek_space(r);
559 dim = isl_space_dim(space, isl_dim_all);
560 if (dim < 0)
561 goto error;
562 res = isl_vec_alloc(vec->ctx, 2 + dim + n_div);
563 if (!res)
564 goto error;
565 isl_seq_cpy(res->el, vec->el, 2);
566 isl_seq_clr(res->el + 2, res->size - 2);
567 for (i = 0; i < r->len; ++i)
568 isl_int_set(res->el[2 + r->pos[i]], vec->el[2 + i]);
570 isl_reordering_free(r);
571 isl_vec_free(vec);
572 return res;
573 error:
574 isl_vec_free(vec);
575 isl_reordering_free(r);
576 return NULL;
579 /* Reorder the dimensions of the domain of "aff" according
580 * to the given reordering.
582 __isl_give isl_aff *isl_aff_realign_domain(__isl_take isl_aff *aff,
583 __isl_take isl_reordering *r)
585 aff = isl_aff_cow(aff);
586 if (!aff)
587 goto error;
589 r = isl_reordering_extend(r, aff->ls->div->n_row);
590 aff->v = vec_reorder(aff->v, isl_reordering_copy(r),
591 aff->ls->div->n_row);
592 aff->ls = isl_local_space_realign(aff->ls, r);
594 if (!aff->v || !aff->ls)
595 return isl_aff_free(aff);
597 return aff;
598 error:
599 isl_aff_free(aff);
600 isl_reordering_free(r);
601 return NULL;
604 __isl_give isl_aff *isl_aff_align_params(__isl_take isl_aff *aff,
605 __isl_take isl_space *model)
607 isl_bool equal_params;
609 if (!aff || !model)
610 goto error;
612 equal_params = isl_space_has_equal_params(aff->ls->dim, model);
613 if (equal_params < 0)
614 goto error;
615 if (!equal_params) {
616 isl_reordering *exp;
618 exp = isl_parameter_alignment_reordering(aff->ls->dim, model);
619 exp = isl_reordering_extend_space(exp,
620 isl_aff_get_domain_space(aff));
621 aff = isl_aff_realign_domain(aff, exp);
624 isl_space_free(model);
625 return aff;
626 error:
627 isl_space_free(model);
628 isl_aff_free(aff);
629 return NULL;
632 /* Given an affine function "aff" defined over a parameter domain,
633 * convert it to a function defined over a domain corresponding
634 * to "domain".
635 * Any parameters with identifiers in "domain" are reinterpreted
636 * as the corresponding domain dimensions.
638 __isl_give isl_aff *isl_aff_unbind_params_insert_domain(
639 __isl_take isl_aff *aff, __isl_take isl_multi_id *domain)
641 isl_bool is_params;
642 isl_space *space;
643 isl_reordering *r;
645 space = isl_aff_peek_domain_space(aff);
646 is_params = isl_space_is_params(space);
647 if (is_params < 0)
648 domain = isl_multi_id_free(domain);
649 else if (!is_params)
650 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
651 "expecting function with parameter domain",
652 domain = isl_multi_id_free(domain));
653 r = isl_reordering_unbind_params_insert_domain(space, domain);
654 isl_multi_id_free(domain);
656 return isl_aff_realign_domain(aff, r);
659 /* Is "aff" obviously equal to zero?
661 * If the denominator is zero, then "aff" is not equal to zero.
663 isl_bool isl_aff_plain_is_zero(__isl_keep isl_aff *aff)
665 int pos;
667 if (!aff)
668 return isl_bool_error;
670 if (isl_int_is_zero(aff->v->el[0]))
671 return isl_bool_false;
672 pos = isl_seq_first_non_zero(aff->v->el + 1, aff->v->size - 1);
673 return isl_bool_ok(pos < 0);
676 /* Does "aff" represent NaN?
678 isl_bool isl_aff_is_nan(__isl_keep isl_aff *aff)
680 if (!aff)
681 return isl_bool_error;
683 return isl_bool_ok(isl_seq_first_non_zero(aff->v->el, 2) < 0);
686 /* Are "aff1" and "aff2" obviously equal?
688 * NaN is not equal to anything, not even to another NaN.
690 isl_bool isl_aff_plain_is_equal(__isl_keep isl_aff *aff1,
691 __isl_keep isl_aff *aff2)
693 isl_bool equal;
695 if (!aff1 || !aff2)
696 return isl_bool_error;
698 if (isl_aff_is_nan(aff1) || isl_aff_is_nan(aff2))
699 return isl_bool_false;
701 equal = isl_local_space_is_equal(aff1->ls, aff2->ls);
702 if (equal < 0 || !equal)
703 return equal;
705 return isl_vec_is_equal(aff1->v, aff2->v);
708 /* Return the common denominator of "aff" in "v".
710 * We cannot return anything meaningful in case of a NaN.
712 isl_stat isl_aff_get_denominator(__isl_keep isl_aff *aff, isl_int *v)
714 if (!aff)
715 return isl_stat_error;
716 if (isl_aff_is_nan(aff))
717 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
718 "cannot get denominator of NaN", return isl_stat_error);
719 isl_int_set(*v, aff->v->el[0]);
720 return isl_stat_ok;
723 /* Return the common denominator of "aff".
725 __isl_give isl_val *isl_aff_get_denominator_val(__isl_keep isl_aff *aff)
727 isl_ctx *ctx;
729 if (!aff)
730 return NULL;
732 ctx = isl_aff_get_ctx(aff);
733 if (isl_aff_is_nan(aff))
734 return isl_val_nan(ctx);
735 return isl_val_int_from_isl_int(ctx, aff->v->el[0]);
738 /* Return the constant term of "aff".
740 __isl_give isl_val *isl_aff_get_constant_val(__isl_keep isl_aff *aff)
742 isl_ctx *ctx;
743 isl_val *v;
745 if (!aff)
746 return NULL;
748 ctx = isl_aff_get_ctx(aff);
749 if (isl_aff_is_nan(aff))
750 return isl_val_nan(ctx);
751 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1], aff->v->el[0]);
752 return isl_val_normalize(v);
755 /* Return the coefficient of the variable of type "type" at position "pos"
756 * of "aff".
758 __isl_give isl_val *isl_aff_get_coefficient_val(__isl_keep isl_aff *aff,
759 enum isl_dim_type type, int pos)
761 isl_ctx *ctx;
762 isl_val *v;
764 if (!aff)
765 return NULL;
767 ctx = isl_aff_get_ctx(aff);
768 if (type == isl_dim_out)
769 isl_die(ctx, isl_error_invalid,
770 "output/set dimension does not have a coefficient",
771 return NULL);
772 if (type == isl_dim_in)
773 type = isl_dim_set;
775 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
776 return NULL;
778 if (isl_aff_is_nan(aff))
779 return isl_val_nan(ctx);
780 pos += isl_local_space_offset(aff->ls, type);
781 v = isl_val_rat_from_isl_int(ctx, aff->v->el[1 + pos], aff->v->el[0]);
782 return isl_val_normalize(v);
785 /* Return the sign of the coefficient of the variable of type "type"
786 * at position "pos" of "aff".
788 int isl_aff_coefficient_sgn(__isl_keep isl_aff *aff, enum isl_dim_type type,
789 int pos)
791 isl_ctx *ctx;
793 if (!aff)
794 return 0;
796 ctx = isl_aff_get_ctx(aff);
797 if (type == isl_dim_out)
798 isl_die(ctx, isl_error_invalid,
799 "output/set dimension does not have a coefficient",
800 return 0);
801 if (type == isl_dim_in)
802 type = isl_dim_set;
804 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
805 return 0;
807 pos += isl_local_space_offset(aff->ls, type);
808 return isl_int_sgn(aff->v->el[1 + pos]);
811 /* Replace the numerator of the constant term of "aff" by "v".
813 * A NaN is unaffected by this operation.
815 __isl_give isl_aff *isl_aff_set_constant(__isl_take isl_aff *aff, isl_int v)
817 if (!aff)
818 return NULL;
819 if (isl_aff_is_nan(aff))
820 return aff;
821 aff = isl_aff_cow(aff);
822 if (!aff)
823 return NULL;
825 aff->v = isl_vec_cow(aff->v);
826 if (!aff->v)
827 return isl_aff_free(aff);
829 isl_int_set(aff->v->el[1], v);
831 return aff;
834 /* Replace the constant term of "aff" by "v".
836 * A NaN is unaffected by this operation.
838 __isl_give isl_aff *isl_aff_set_constant_val(__isl_take isl_aff *aff,
839 __isl_take isl_val *v)
841 if (!aff || !v)
842 goto error;
844 if (isl_aff_is_nan(aff)) {
845 isl_val_free(v);
846 return aff;
849 if (!isl_val_is_rat(v))
850 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
851 "expecting rational value", goto error);
853 if (isl_int_eq(aff->v->el[1], v->n) &&
854 isl_int_eq(aff->v->el[0], v->d)) {
855 isl_val_free(v);
856 return aff;
859 aff = isl_aff_cow(aff);
860 if (!aff)
861 goto error;
862 aff->v = isl_vec_cow(aff->v);
863 if (!aff->v)
864 goto error;
866 if (isl_int_eq(aff->v->el[0], v->d)) {
867 isl_int_set(aff->v->el[1], v->n);
868 } else if (isl_int_is_one(v->d)) {
869 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
870 } else {
871 isl_seq_scale(aff->v->el + 1,
872 aff->v->el + 1, v->d, aff->v->size - 1);
873 isl_int_mul(aff->v->el[1], aff->v->el[0], v->n);
874 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
875 aff->v = isl_vec_normalize(aff->v);
876 if (!aff->v)
877 goto error;
880 isl_val_free(v);
881 return aff;
882 error:
883 isl_aff_free(aff);
884 isl_val_free(v);
885 return NULL;
888 /* Add "v" to the constant term of "aff".
890 * A NaN is unaffected by this operation.
892 __isl_give isl_aff *isl_aff_add_constant(__isl_take isl_aff *aff, isl_int v)
894 if (isl_int_is_zero(v))
895 return aff;
897 if (!aff)
898 return NULL;
899 if (isl_aff_is_nan(aff))
900 return aff;
901 aff = isl_aff_cow(aff);
902 if (!aff)
903 return NULL;
905 aff->v = isl_vec_cow(aff->v);
906 if (!aff->v)
907 return isl_aff_free(aff);
909 isl_int_addmul(aff->v->el[1], aff->v->el[0], v);
911 return aff;
914 /* Add "v" to the constant term of "aff".
916 * A NaN is unaffected by this operation.
918 __isl_give isl_aff *isl_aff_add_constant_val(__isl_take isl_aff *aff,
919 __isl_take isl_val *v)
921 if (!aff || !v)
922 goto error;
924 if (isl_aff_is_nan(aff) || isl_val_is_zero(v)) {
925 isl_val_free(v);
926 return aff;
929 if (!isl_val_is_rat(v))
930 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
931 "expecting rational value", goto error);
933 aff = isl_aff_cow(aff);
934 if (!aff)
935 goto error;
937 aff->v = isl_vec_cow(aff->v);
938 if (!aff->v)
939 goto error;
941 if (isl_int_is_one(v->d)) {
942 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
943 } else if (isl_int_eq(aff->v->el[0], v->d)) {
944 isl_int_add(aff->v->el[1], aff->v->el[1], v->n);
945 aff->v = isl_vec_normalize(aff->v);
946 if (!aff->v)
947 goto error;
948 } else {
949 isl_seq_scale(aff->v->el + 1,
950 aff->v->el + 1, v->d, aff->v->size - 1);
951 isl_int_addmul(aff->v->el[1], aff->v->el[0], v->n);
952 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
953 aff->v = isl_vec_normalize(aff->v);
954 if (!aff->v)
955 goto error;
958 isl_val_free(v);
959 return aff;
960 error:
961 isl_aff_free(aff);
962 isl_val_free(v);
963 return NULL;
966 __isl_give isl_aff *isl_aff_add_constant_si(__isl_take isl_aff *aff, int v)
968 isl_int t;
970 isl_int_init(t);
971 isl_int_set_si(t, v);
972 aff = isl_aff_add_constant(aff, t);
973 isl_int_clear(t);
975 return aff;
978 /* Add "v" to the numerator of the constant term of "aff".
980 * A NaN is unaffected by this operation.
982 __isl_give isl_aff *isl_aff_add_constant_num(__isl_take isl_aff *aff, isl_int v)
984 if (isl_int_is_zero(v))
985 return aff;
987 if (!aff)
988 return NULL;
989 if (isl_aff_is_nan(aff))
990 return aff;
991 aff = isl_aff_cow(aff);
992 if (!aff)
993 return NULL;
995 aff->v = isl_vec_cow(aff->v);
996 if (!aff->v)
997 return isl_aff_free(aff);
999 isl_int_add(aff->v->el[1], aff->v->el[1], v);
1001 return aff;
1004 /* Add "v" to the numerator of the constant term of "aff".
1006 * A NaN is unaffected by this operation.
1008 __isl_give isl_aff *isl_aff_add_constant_num_si(__isl_take isl_aff *aff, int v)
1010 isl_int t;
1012 if (v == 0)
1013 return aff;
1015 isl_int_init(t);
1016 isl_int_set_si(t, v);
1017 aff = isl_aff_add_constant_num(aff, t);
1018 isl_int_clear(t);
1020 return aff;
1023 /* Replace the numerator of the constant term of "aff" by "v".
1025 * A NaN is unaffected by this operation.
1027 __isl_give isl_aff *isl_aff_set_constant_si(__isl_take isl_aff *aff, int v)
1029 if (!aff)
1030 return NULL;
1031 if (isl_aff_is_nan(aff))
1032 return aff;
1033 aff = isl_aff_cow(aff);
1034 if (!aff)
1035 return NULL;
1037 aff->v = isl_vec_cow(aff->v);
1038 if (!aff->v)
1039 return isl_aff_free(aff);
1041 isl_int_set_si(aff->v->el[1], v);
1043 return aff;
1046 /* Replace the numerator of the coefficient of the variable of type "type"
1047 * at position "pos" of "aff" by "v".
1049 * A NaN is unaffected by this operation.
1051 __isl_give isl_aff *isl_aff_set_coefficient(__isl_take isl_aff *aff,
1052 enum isl_dim_type type, int pos, isl_int v)
1054 if (!aff)
1055 return NULL;
1057 if (type == isl_dim_out)
1058 isl_die(aff->v->ctx, isl_error_invalid,
1059 "output/set dimension does not have a coefficient",
1060 return isl_aff_free(aff));
1061 if (type == isl_dim_in)
1062 type = isl_dim_set;
1064 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1065 return isl_aff_free(aff);
1067 if (isl_aff_is_nan(aff))
1068 return aff;
1069 aff = isl_aff_cow(aff);
1070 if (!aff)
1071 return NULL;
1073 aff->v = isl_vec_cow(aff->v);
1074 if (!aff->v)
1075 return isl_aff_free(aff);
1077 pos += isl_local_space_offset(aff->ls, type);
1078 isl_int_set(aff->v->el[1 + pos], v);
1080 return aff;
1083 /* Replace the numerator of the coefficient of the variable of type "type"
1084 * at position "pos" of "aff" by "v".
1086 * A NaN is unaffected by this operation.
1088 __isl_give isl_aff *isl_aff_set_coefficient_si(__isl_take isl_aff *aff,
1089 enum isl_dim_type type, int pos, int v)
1091 if (!aff)
1092 return NULL;
1094 if (type == isl_dim_out)
1095 isl_die(aff->v->ctx, isl_error_invalid,
1096 "output/set dimension does not have a coefficient",
1097 return isl_aff_free(aff));
1098 if (type == isl_dim_in)
1099 type = isl_dim_set;
1101 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1102 return isl_aff_free(aff);
1104 if (isl_aff_is_nan(aff))
1105 return aff;
1106 pos += isl_local_space_offset(aff->ls, type);
1107 if (isl_int_cmp_si(aff->v->el[1 + pos], v) == 0)
1108 return aff;
1110 aff = isl_aff_cow(aff);
1111 if (!aff)
1112 return NULL;
1114 aff->v = isl_vec_cow(aff->v);
1115 if (!aff->v)
1116 return isl_aff_free(aff);
1118 isl_int_set_si(aff->v->el[1 + pos], v);
1120 return aff;
1123 /* Replace the coefficient of the variable of type "type" at position "pos"
1124 * of "aff" by "v".
1126 * A NaN is unaffected by this operation.
1128 __isl_give isl_aff *isl_aff_set_coefficient_val(__isl_take isl_aff *aff,
1129 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1131 if (!aff || !v)
1132 goto error;
1134 if (type == isl_dim_out)
1135 isl_die(aff->v->ctx, isl_error_invalid,
1136 "output/set dimension does not have a coefficient",
1137 goto error);
1138 if (type == isl_dim_in)
1139 type = isl_dim_set;
1141 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1142 return isl_aff_free(aff);
1144 if (isl_aff_is_nan(aff)) {
1145 isl_val_free(v);
1146 return aff;
1148 if (!isl_val_is_rat(v))
1149 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1150 "expecting rational value", goto error);
1152 pos += isl_local_space_offset(aff->ls, type);
1153 if (isl_int_eq(aff->v->el[1 + pos], v->n) &&
1154 isl_int_eq(aff->v->el[0], v->d)) {
1155 isl_val_free(v);
1156 return aff;
1159 aff = isl_aff_cow(aff);
1160 if (!aff)
1161 goto error;
1162 aff->v = isl_vec_cow(aff->v);
1163 if (!aff->v)
1164 goto error;
1166 if (isl_int_eq(aff->v->el[0], v->d)) {
1167 isl_int_set(aff->v->el[1 + pos], v->n);
1168 } else if (isl_int_is_one(v->d)) {
1169 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1170 } else {
1171 isl_seq_scale(aff->v->el + 1,
1172 aff->v->el + 1, v->d, aff->v->size - 1);
1173 isl_int_mul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1174 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1175 aff->v = isl_vec_normalize(aff->v);
1176 if (!aff->v)
1177 goto error;
1180 isl_val_free(v);
1181 return aff;
1182 error:
1183 isl_aff_free(aff);
1184 isl_val_free(v);
1185 return NULL;
1188 /* Add "v" to the coefficient of the variable of type "type"
1189 * at position "pos" of "aff".
1191 * A NaN is unaffected by this operation.
1193 __isl_give isl_aff *isl_aff_add_coefficient(__isl_take isl_aff *aff,
1194 enum isl_dim_type type, int pos, isl_int v)
1196 if (!aff)
1197 return NULL;
1199 if (type == isl_dim_out)
1200 isl_die(aff->v->ctx, isl_error_invalid,
1201 "output/set dimension does not have a coefficient",
1202 return isl_aff_free(aff));
1203 if (type == isl_dim_in)
1204 type = isl_dim_set;
1206 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1207 return isl_aff_free(aff);
1209 if (isl_aff_is_nan(aff))
1210 return aff;
1211 aff = isl_aff_cow(aff);
1212 if (!aff)
1213 return NULL;
1215 aff->v = isl_vec_cow(aff->v);
1216 if (!aff->v)
1217 return isl_aff_free(aff);
1219 pos += isl_local_space_offset(aff->ls, type);
1220 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v);
1222 return aff;
1225 /* Add "v" to the coefficient of the variable of type "type"
1226 * at position "pos" of "aff".
1228 * A NaN is unaffected by this operation.
1230 __isl_give isl_aff *isl_aff_add_coefficient_val(__isl_take isl_aff *aff,
1231 enum isl_dim_type type, int pos, __isl_take isl_val *v)
1233 if (!aff || !v)
1234 goto error;
1236 if (isl_val_is_zero(v)) {
1237 isl_val_free(v);
1238 return aff;
1241 if (type == isl_dim_out)
1242 isl_die(aff->v->ctx, isl_error_invalid,
1243 "output/set dimension does not have a coefficient",
1244 goto error);
1245 if (type == isl_dim_in)
1246 type = isl_dim_set;
1248 if (isl_local_space_check_range(aff->ls, type, pos, 1) < 0)
1249 goto error;
1251 if (isl_aff_is_nan(aff)) {
1252 isl_val_free(v);
1253 return aff;
1255 if (!isl_val_is_rat(v))
1256 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1257 "expecting rational value", goto error);
1259 aff = isl_aff_cow(aff);
1260 if (!aff)
1261 goto error;
1263 aff->v = isl_vec_cow(aff->v);
1264 if (!aff->v)
1265 goto error;
1267 pos += isl_local_space_offset(aff->ls, type);
1268 if (isl_int_is_one(v->d)) {
1269 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1270 } else if (isl_int_eq(aff->v->el[0], v->d)) {
1271 isl_int_add(aff->v->el[1 + pos], aff->v->el[1 + pos], v->n);
1272 aff->v = isl_vec_normalize(aff->v);
1273 if (!aff->v)
1274 goto error;
1275 } else {
1276 isl_seq_scale(aff->v->el + 1,
1277 aff->v->el + 1, v->d, aff->v->size - 1);
1278 isl_int_addmul(aff->v->el[1 + pos], aff->v->el[0], v->n);
1279 isl_int_mul(aff->v->el[0], aff->v->el[0], v->d);
1280 aff->v = isl_vec_normalize(aff->v);
1281 if (!aff->v)
1282 goto error;
1285 isl_val_free(v);
1286 return aff;
1287 error:
1288 isl_aff_free(aff);
1289 isl_val_free(v);
1290 return NULL;
1293 __isl_give isl_aff *isl_aff_add_coefficient_si(__isl_take isl_aff *aff,
1294 enum isl_dim_type type, int pos, int v)
1296 isl_int t;
1298 isl_int_init(t);
1299 isl_int_set_si(t, v);
1300 aff = isl_aff_add_coefficient(aff, type, pos, t);
1301 isl_int_clear(t);
1303 return aff;
1306 __isl_give isl_aff *isl_aff_get_div(__isl_keep isl_aff *aff, int pos)
1308 if (!aff)
1309 return NULL;
1311 return isl_local_space_get_div(aff->ls, pos);
1314 /* Return the negation of "aff".
1316 * As a special case, -NaN = NaN.
1318 __isl_give isl_aff *isl_aff_neg(__isl_take isl_aff *aff)
1320 if (!aff)
1321 return NULL;
1322 if (isl_aff_is_nan(aff))
1323 return aff;
1324 aff = isl_aff_cow(aff);
1325 if (!aff)
1326 return NULL;
1327 aff->v = isl_vec_cow(aff->v);
1328 if (!aff->v)
1329 return isl_aff_free(aff);
1331 isl_seq_neg(aff->v->el + 1, aff->v->el + 1, aff->v->size - 1);
1333 return aff;
1336 /* Remove divs from the local space that do not appear in the affine
1337 * expression.
1338 * We currently only remove divs at the end.
1339 * Some intermediate divs may also not appear directly in the affine
1340 * expression, but we would also need to check that no other divs are
1341 * defined in terms of them.
1343 __isl_give isl_aff *isl_aff_remove_unused_divs(__isl_take isl_aff *aff)
1345 int pos;
1346 isl_size off;
1347 isl_size n;
1349 n = isl_aff_domain_dim(aff, isl_dim_div);
1350 off = isl_aff_domain_offset(aff, isl_dim_div);
1351 if (n < 0 || off < 0)
1352 return isl_aff_free(aff);
1354 pos = isl_seq_last_non_zero(aff->v->el + 1 + off, n) + 1;
1355 if (pos == n)
1356 return aff;
1358 aff = isl_aff_cow(aff);
1359 if (!aff)
1360 return NULL;
1362 aff->ls = isl_local_space_drop_dims(aff->ls, isl_dim_div, pos, n - pos);
1363 aff->v = isl_vec_drop_els(aff->v, 1 + off + pos, n - pos);
1364 if (!aff->ls || !aff->v)
1365 return isl_aff_free(aff);
1367 return aff;
1370 /* Look for any divs in the aff->ls with a denominator equal to one
1371 * and plug them into the affine expression and any subsequent divs
1372 * that may reference the div.
1374 static __isl_give isl_aff *plug_in_integral_divs(__isl_take isl_aff *aff)
1376 int i;
1377 isl_size n;
1378 int len;
1379 isl_int v;
1380 isl_vec *vec;
1381 isl_local_space *ls;
1382 isl_size off;
1384 n = isl_aff_domain_dim(aff, isl_dim_div);
1385 off = isl_aff_domain_offset(aff, isl_dim_div);
1386 if (n < 0 || off < 0)
1387 return isl_aff_free(aff);
1388 len = aff->v->size;
1389 for (i = 0; i < n; ++i) {
1390 if (!isl_int_is_one(aff->ls->div->row[i][0]))
1391 continue;
1392 ls = isl_local_space_copy(aff->ls);
1393 ls = isl_local_space_substitute_seq(ls, isl_dim_div, i,
1394 aff->ls->div->row[i], len, i + 1, n - (i + 1));
1395 vec = isl_vec_copy(aff->v);
1396 vec = isl_vec_cow(vec);
1397 if (!ls || !vec)
1398 goto error;
1400 isl_int_init(v);
1402 isl_seq_substitute(vec->el, off + i, aff->ls->div->row[i],
1403 len, len, v);
1405 isl_int_clear(v);
1407 isl_vec_free(aff->v);
1408 aff->v = vec;
1409 isl_local_space_free(aff->ls);
1410 aff->ls = ls;
1413 return aff;
1414 error:
1415 isl_vec_free(vec);
1416 isl_local_space_free(ls);
1417 return isl_aff_free(aff);
1420 /* Look for any divs j that appear with a unit coefficient inside
1421 * the definitions of other divs i and plug them into the definitions
1422 * of the divs i.
1424 * In particular, an expression of the form
1426 * floor((f(..) + floor(g(..)/n))/m)
1428 * is simplified to
1430 * floor((n * f(..) + g(..))/(n * m))
1432 * This simplification is correct because we can move the expression
1433 * f(..) into the inner floor in the original expression to obtain
1435 * floor(floor((n * f(..) + g(..))/n)/m)
1437 * from which we can derive the simplified expression.
1439 static __isl_give isl_aff *plug_in_unit_divs(__isl_take isl_aff *aff)
1441 int i, j;
1442 isl_size n;
1443 isl_size off;
1445 n = isl_aff_domain_dim(aff, isl_dim_div);
1446 off = isl_aff_domain_offset(aff, isl_dim_div);
1447 if (n < 0 || off < 0)
1448 return isl_aff_free(aff);
1449 for (i = 1; i < n; ++i) {
1450 for (j = 0; j < i; ++j) {
1451 if (!isl_int_is_one(aff->ls->div->row[i][1 + off + j]))
1452 continue;
1453 aff->ls = isl_local_space_substitute_seq(aff->ls,
1454 isl_dim_div, j, aff->ls->div->row[j],
1455 aff->v->size, i, 1);
1456 if (!aff->ls)
1457 return isl_aff_free(aff);
1461 return aff;
1464 /* Swap divs "a" and "b" in "aff", which is assumed to be non-NULL.
1466 * Even though this function is only called on isl_affs with a single
1467 * reference, we are careful to only change aff->v and aff->ls together.
1469 static __isl_give isl_aff *swap_div(__isl_take isl_aff *aff, int a, int b)
1471 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1472 isl_local_space *ls;
1473 isl_vec *v;
1475 if (off < 0)
1476 return isl_aff_free(aff);
1478 ls = isl_local_space_copy(aff->ls);
1479 ls = isl_local_space_swap_div(ls, a, b);
1480 v = isl_vec_copy(aff->v);
1481 v = isl_vec_cow(v);
1482 if (!ls || !v)
1483 goto error;
1485 isl_int_swap(v->el[1 + off + a], v->el[1 + off + b]);
1486 isl_vec_free(aff->v);
1487 aff->v = v;
1488 isl_local_space_free(aff->ls);
1489 aff->ls = ls;
1491 return aff;
1492 error:
1493 isl_vec_free(v);
1494 isl_local_space_free(ls);
1495 return isl_aff_free(aff);
1498 /* Merge divs "a" and "b" in "aff", which is assumed to be non-NULL.
1500 * We currently do not actually remove div "b", but simply add its
1501 * coefficient to that of "a" and then zero it out.
1503 static __isl_give isl_aff *merge_divs(__isl_take isl_aff *aff, int a, int b)
1505 isl_size off = isl_aff_domain_offset(aff, isl_dim_div);
1507 if (off < 0)
1508 return isl_aff_free(aff);
1510 if (isl_int_is_zero(aff->v->el[1 + off + b]))
1511 return aff;
1513 aff->v = isl_vec_cow(aff->v);
1514 if (!aff->v)
1515 return isl_aff_free(aff);
1517 isl_int_add(aff->v->el[1 + off + a],
1518 aff->v->el[1 + off + a], aff->v->el[1 + off + b]);
1519 isl_int_set_si(aff->v->el[1 + off + b], 0);
1521 return aff;
1524 /* Sort the divs in the local space of "aff" according to
1525 * the comparison function "cmp_row" in isl_local_space.c,
1526 * combining the coefficients of identical divs.
1528 * Reordering divs does not change the semantics of "aff",
1529 * so there is no need to call isl_aff_cow.
1530 * Moreover, this function is currently only called on isl_affs
1531 * with a single reference.
1533 static __isl_give isl_aff *sort_divs(__isl_take isl_aff *aff)
1535 isl_size n;
1536 int i, j;
1538 n = isl_aff_dim(aff, isl_dim_div);
1539 if (n < 0)
1540 return isl_aff_free(aff);
1541 for (i = 1; i < n; ++i) {
1542 for (j = i - 1; j >= 0; --j) {
1543 int cmp = isl_mat_cmp_div(aff->ls->div, j, j + 1);
1544 if (cmp < 0)
1545 break;
1546 if (cmp == 0)
1547 aff = merge_divs(aff, j, j + 1);
1548 else
1549 aff = swap_div(aff, j, j + 1);
1550 if (!aff)
1551 return NULL;
1555 return aff;
1558 /* Normalize the representation of "aff".
1560 * This function should only be called of "new" isl_affs, i.e.,
1561 * with only a single reference. We therefore do not need to
1562 * worry about affecting other instances.
1564 __isl_give isl_aff *isl_aff_normalize(__isl_take isl_aff *aff)
1566 if (!aff)
1567 return NULL;
1568 aff->v = isl_vec_normalize(aff->v);
1569 if (!aff->v)
1570 return isl_aff_free(aff);
1571 aff = plug_in_integral_divs(aff);
1572 aff = plug_in_unit_divs(aff);
1573 aff = sort_divs(aff);
1574 aff = isl_aff_remove_unused_divs(aff);
1575 return aff;
1578 /* Given f, return floor(f).
1579 * If f is an integer expression, then just return f.
1580 * If f is a constant, then return the constant floor(f).
1581 * Otherwise, if f = g/m, write g = q m + r,
1582 * create a new div d = [r/m] and return the expression q + d.
1583 * The coefficients in r are taken to lie between -m/2 and m/2.
1585 * reduce_div_coefficients performs the same normalization.
1587 * As a special case, floor(NaN) = NaN.
1589 __isl_give isl_aff *isl_aff_floor(__isl_take isl_aff *aff)
1591 int i;
1592 int size;
1593 isl_ctx *ctx;
1594 isl_vec *div;
1596 if (!aff)
1597 return NULL;
1599 if (isl_aff_is_nan(aff))
1600 return aff;
1601 if (isl_int_is_one(aff->v->el[0]))
1602 return aff;
1604 aff = isl_aff_cow(aff);
1605 if (!aff)
1606 return NULL;
1608 aff->v = isl_vec_cow(aff->v);
1609 if (!aff->v)
1610 return isl_aff_free(aff);
1612 if (isl_aff_is_cst(aff)) {
1613 isl_int_fdiv_q(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1614 isl_int_set_si(aff->v->el[0], 1);
1615 return aff;
1618 div = isl_vec_copy(aff->v);
1619 div = isl_vec_cow(div);
1620 if (!div)
1621 return isl_aff_free(aff);
1623 ctx = isl_aff_get_ctx(aff);
1624 isl_int_fdiv_q(aff->v->el[0], aff->v->el[0], ctx->two);
1625 for (i = 1; i < aff->v->size; ++i) {
1626 isl_int_fdiv_r(div->el[i], div->el[i], div->el[0]);
1627 isl_int_fdiv_q(aff->v->el[i], aff->v->el[i], div->el[0]);
1628 if (isl_int_gt(div->el[i], aff->v->el[0])) {
1629 isl_int_sub(div->el[i], div->el[i], div->el[0]);
1630 isl_int_add_ui(aff->v->el[i], aff->v->el[i], 1);
1634 aff->ls = isl_local_space_add_div(aff->ls, div);
1635 if (!aff->ls)
1636 return isl_aff_free(aff);
1638 size = aff->v->size;
1639 aff->v = isl_vec_extend(aff->v, size + 1);
1640 if (!aff->v)
1641 return isl_aff_free(aff);
1642 isl_int_set_si(aff->v->el[0], 1);
1643 isl_int_set_si(aff->v->el[size], 1);
1645 aff = isl_aff_normalize(aff);
1647 return aff;
1650 /* Compute
1652 * aff mod m = aff - m * floor(aff/m)
1654 * with m an integer value.
1656 __isl_give isl_aff *isl_aff_mod_val(__isl_take isl_aff *aff,
1657 __isl_take isl_val *m)
1659 isl_aff *res;
1661 if (!aff || !m)
1662 goto error;
1664 if (!isl_val_is_int(m))
1665 isl_die(isl_val_get_ctx(m), isl_error_invalid,
1666 "expecting integer modulo", goto error);
1668 res = isl_aff_copy(aff);
1669 aff = isl_aff_scale_down_val(aff, isl_val_copy(m));
1670 aff = isl_aff_floor(aff);
1671 aff = isl_aff_scale_val(aff, m);
1672 res = isl_aff_sub(res, aff);
1674 return res;
1675 error:
1676 isl_aff_free(aff);
1677 isl_val_free(m);
1678 return NULL;
1681 /* Compute
1683 * pwaff mod m = pwaff - m * floor(pwaff/m)
1685 __isl_give isl_pw_aff *isl_pw_aff_mod(__isl_take isl_pw_aff *pwaff, isl_int m)
1687 isl_pw_aff *res;
1689 res = isl_pw_aff_copy(pwaff);
1690 pwaff = isl_pw_aff_scale_down(pwaff, m);
1691 pwaff = isl_pw_aff_floor(pwaff);
1692 pwaff = isl_pw_aff_scale(pwaff, m);
1693 res = isl_pw_aff_sub(res, pwaff);
1695 return res;
1698 /* Compute
1700 * pa mod m = pa - m * floor(pa/m)
1702 * with m an integer value.
1704 __isl_give isl_pw_aff *isl_pw_aff_mod_val(__isl_take isl_pw_aff *pa,
1705 __isl_take isl_val *m)
1707 if (!pa || !m)
1708 goto error;
1709 if (!isl_val_is_int(m))
1710 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
1711 "expecting integer modulo", goto error);
1712 pa = isl_pw_aff_mod(pa, m->n);
1713 isl_val_free(m);
1714 return pa;
1715 error:
1716 isl_pw_aff_free(pa);
1717 isl_val_free(m);
1718 return NULL;
1721 /* Given f, return ceil(f).
1722 * If f is an integer expression, then just return f.
1723 * Otherwise, let f be the expression
1725 * e/m
1727 * then return
1729 * floor((e + m - 1)/m)
1731 * As a special case, ceil(NaN) = NaN.
1733 __isl_give isl_aff *isl_aff_ceil(__isl_take isl_aff *aff)
1735 if (!aff)
1736 return NULL;
1738 if (isl_aff_is_nan(aff))
1739 return aff;
1740 if (isl_int_is_one(aff->v->el[0]))
1741 return aff;
1743 aff = isl_aff_cow(aff);
1744 if (!aff)
1745 return NULL;
1746 aff->v = isl_vec_cow(aff->v);
1747 if (!aff->v)
1748 return isl_aff_free(aff);
1750 isl_int_add(aff->v->el[1], aff->v->el[1], aff->v->el[0]);
1751 isl_int_sub_ui(aff->v->el[1], aff->v->el[1], 1);
1752 aff = isl_aff_floor(aff);
1754 return aff;
1757 /* Apply the expansion computed by isl_merge_divs.
1758 * The expansion itself is given by "exp" while the resulting
1759 * list of divs is given by "div".
1761 __isl_give isl_aff *isl_aff_expand_divs(__isl_take isl_aff *aff,
1762 __isl_take isl_mat *div, int *exp)
1764 isl_size old_n_div;
1765 isl_size new_n_div;
1766 isl_size offset;
1768 aff = isl_aff_cow(aff);
1770 offset = isl_aff_domain_offset(aff, isl_dim_div);
1771 old_n_div = isl_aff_domain_dim(aff, isl_dim_div);
1772 new_n_div = isl_mat_rows(div);
1773 if (offset < 0 || old_n_div < 0 || new_n_div < 0)
1774 goto error;
1776 aff->v = isl_vec_expand(aff->v, 1 + offset, old_n_div, exp, new_n_div);
1777 aff->ls = isl_local_space_replace_divs(aff->ls, div);
1778 if (!aff->v || !aff->ls)
1779 return isl_aff_free(aff);
1780 return aff;
1781 error:
1782 isl_aff_free(aff);
1783 isl_mat_free(div);
1784 return NULL;
1787 /* Add two affine expressions that live in the same local space.
1789 static __isl_give isl_aff *add_expanded(__isl_take isl_aff *aff1,
1790 __isl_take isl_aff *aff2)
1792 isl_int gcd, f;
1794 aff1 = isl_aff_cow(aff1);
1795 if (!aff1 || !aff2)
1796 goto error;
1798 aff1->v = isl_vec_cow(aff1->v);
1799 if (!aff1->v)
1800 goto error;
1802 isl_int_init(gcd);
1803 isl_int_init(f);
1804 isl_int_gcd(gcd, aff1->v->el[0], aff2->v->el[0]);
1805 isl_int_divexact(f, aff2->v->el[0], gcd);
1806 isl_seq_scale(aff1->v->el + 1, aff1->v->el + 1, f, aff1->v->size - 1);
1807 isl_int_divexact(f, aff1->v->el[0], gcd);
1808 isl_seq_addmul(aff1->v->el + 1, f, aff2->v->el + 1, aff1->v->size - 1);
1809 isl_int_divexact(f, aff2->v->el[0], gcd);
1810 isl_int_mul(aff1->v->el[0], aff1->v->el[0], f);
1811 isl_int_clear(f);
1812 isl_int_clear(gcd);
1814 isl_aff_free(aff2);
1815 aff1 = isl_aff_normalize(aff1);
1816 return aff1;
1817 error:
1818 isl_aff_free(aff1);
1819 isl_aff_free(aff2);
1820 return NULL;
1823 /* Return the sum of "aff1" and "aff2".
1825 * If either of the two is NaN, then the result is NaN.
1827 __isl_give isl_aff *isl_aff_add(__isl_take isl_aff *aff1,
1828 __isl_take isl_aff *aff2)
1830 isl_ctx *ctx;
1831 int *exp1 = NULL;
1832 int *exp2 = NULL;
1833 isl_mat *div;
1834 isl_size n_div1, n_div2;
1836 if (!aff1 || !aff2)
1837 goto error;
1839 ctx = isl_aff_get_ctx(aff1);
1840 if (!isl_space_is_equal(aff1->ls->dim, aff2->ls->dim))
1841 isl_die(ctx, isl_error_invalid,
1842 "spaces don't match", goto error);
1844 if (isl_aff_is_nan(aff1)) {
1845 isl_aff_free(aff2);
1846 return aff1;
1848 if (isl_aff_is_nan(aff2)) {
1849 isl_aff_free(aff1);
1850 return aff2;
1853 n_div1 = isl_aff_dim(aff1, isl_dim_div);
1854 n_div2 = isl_aff_dim(aff2, isl_dim_div);
1855 if (n_div1 < 0 || n_div2 < 0)
1856 goto error;
1857 if (n_div1 == 0 && n_div2 == 0)
1858 return add_expanded(aff1, aff2);
1860 exp1 = isl_alloc_array(ctx, int, n_div1);
1861 exp2 = isl_alloc_array(ctx, int, n_div2);
1862 if ((n_div1 && !exp1) || (n_div2 && !exp2))
1863 goto error;
1865 div = isl_merge_divs(aff1->ls->div, aff2->ls->div, exp1, exp2);
1866 aff1 = isl_aff_expand_divs(aff1, isl_mat_copy(div), exp1);
1867 aff2 = isl_aff_expand_divs(aff2, div, exp2);
1868 free(exp1);
1869 free(exp2);
1871 return add_expanded(aff1, aff2);
1872 error:
1873 free(exp1);
1874 free(exp2);
1875 isl_aff_free(aff1);
1876 isl_aff_free(aff2);
1877 return NULL;
1880 __isl_give isl_aff *isl_aff_sub(__isl_take isl_aff *aff1,
1881 __isl_take isl_aff *aff2)
1883 return isl_aff_add(aff1, isl_aff_neg(aff2));
1886 /* Return the result of scaling "aff" by a factor of "f".
1888 * As a special case, f * NaN = NaN.
1890 __isl_give isl_aff *isl_aff_scale(__isl_take isl_aff *aff, isl_int f)
1892 isl_int gcd;
1894 if (!aff)
1895 return NULL;
1896 if (isl_aff_is_nan(aff))
1897 return aff;
1899 if (isl_int_is_one(f))
1900 return aff;
1902 aff = isl_aff_cow(aff);
1903 if (!aff)
1904 return NULL;
1905 aff->v = isl_vec_cow(aff->v);
1906 if (!aff->v)
1907 return isl_aff_free(aff);
1909 if (isl_int_is_pos(f) && isl_int_is_divisible_by(aff->v->el[0], f)) {
1910 isl_int_divexact(aff->v->el[0], aff->v->el[0], f);
1911 return aff;
1914 isl_int_init(gcd);
1915 isl_int_gcd(gcd, aff->v->el[0], f);
1916 isl_int_divexact(aff->v->el[0], aff->v->el[0], gcd);
1917 isl_int_divexact(gcd, f, gcd);
1918 isl_seq_scale(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1919 isl_int_clear(gcd);
1921 return aff;
1924 /* Multiple "aff" by "v".
1926 __isl_give isl_aff *isl_aff_scale_val(__isl_take isl_aff *aff,
1927 __isl_take isl_val *v)
1929 if (!aff || !v)
1930 goto error;
1932 if (isl_val_is_one(v)) {
1933 isl_val_free(v);
1934 return aff;
1937 if (!isl_val_is_rat(v))
1938 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1939 "expecting rational factor", goto error);
1941 aff = isl_aff_scale(aff, v->n);
1942 aff = isl_aff_scale_down(aff, v->d);
1944 isl_val_free(v);
1945 return aff;
1946 error:
1947 isl_aff_free(aff);
1948 isl_val_free(v);
1949 return NULL;
1952 /* Return the result of scaling "aff" down by a factor of "f".
1954 * As a special case, NaN/f = NaN.
1956 __isl_give isl_aff *isl_aff_scale_down(__isl_take isl_aff *aff, isl_int f)
1958 isl_int gcd;
1960 if (!aff)
1961 return NULL;
1962 if (isl_aff_is_nan(aff))
1963 return aff;
1965 if (isl_int_is_one(f))
1966 return aff;
1968 aff = isl_aff_cow(aff);
1969 if (!aff)
1970 return NULL;
1972 if (isl_int_is_zero(f))
1973 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
1974 "cannot scale down by zero", return isl_aff_free(aff));
1976 aff->v = isl_vec_cow(aff->v);
1977 if (!aff->v)
1978 return isl_aff_free(aff);
1980 isl_int_init(gcd);
1981 isl_seq_gcd(aff->v->el + 1, aff->v->size - 1, &gcd);
1982 isl_int_gcd(gcd, gcd, f);
1983 isl_seq_scale_down(aff->v->el + 1, aff->v->el + 1, gcd, aff->v->size - 1);
1984 isl_int_divexact(gcd, f, gcd);
1985 isl_int_mul(aff->v->el[0], aff->v->el[0], gcd);
1986 isl_int_clear(gcd);
1988 return aff;
1991 /* Divide "aff" by "v".
1993 __isl_give isl_aff *isl_aff_scale_down_val(__isl_take isl_aff *aff,
1994 __isl_take isl_val *v)
1996 if (!aff || !v)
1997 goto error;
1999 if (isl_val_is_one(v)) {
2000 isl_val_free(v);
2001 return aff;
2004 if (!isl_val_is_rat(v))
2005 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2006 "expecting rational factor", goto error);
2007 if (!isl_val_is_pos(v))
2008 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2009 "factor needs to be positive", goto error);
2011 aff = isl_aff_scale(aff, v->d);
2012 aff = isl_aff_scale_down(aff, v->n);
2014 isl_val_free(v);
2015 return aff;
2016 error:
2017 isl_aff_free(aff);
2018 isl_val_free(v);
2019 return NULL;
2022 __isl_give isl_aff *isl_aff_scale_down_ui(__isl_take isl_aff *aff, unsigned f)
2024 isl_int v;
2026 if (f == 1)
2027 return aff;
2029 isl_int_init(v);
2030 isl_int_set_ui(v, f);
2031 aff = isl_aff_scale_down(aff, v);
2032 isl_int_clear(v);
2034 return aff;
2037 __isl_give isl_aff *isl_aff_set_dim_name(__isl_take isl_aff *aff,
2038 enum isl_dim_type type, unsigned pos, const char *s)
2040 aff = isl_aff_cow(aff);
2041 if (!aff)
2042 return NULL;
2043 if (type == isl_dim_out)
2044 isl_die(aff->v->ctx, isl_error_invalid,
2045 "cannot set name of output/set dimension",
2046 return isl_aff_free(aff));
2047 if (type == isl_dim_in)
2048 type = isl_dim_set;
2049 aff->ls = isl_local_space_set_dim_name(aff->ls, type, pos, s);
2050 if (!aff->ls)
2051 return isl_aff_free(aff);
2053 return aff;
2056 __isl_give isl_aff *isl_aff_set_dim_id(__isl_take isl_aff *aff,
2057 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
2059 aff = isl_aff_cow(aff);
2060 if (!aff)
2061 goto error;
2062 if (type == isl_dim_out)
2063 isl_die(aff->v->ctx, isl_error_invalid,
2064 "cannot set name of output/set dimension",
2065 goto error);
2066 if (type == isl_dim_in)
2067 type = isl_dim_set;
2068 aff->ls = isl_local_space_set_dim_id(aff->ls, type, pos, id);
2069 if (!aff->ls)
2070 return isl_aff_free(aff);
2072 return aff;
2073 error:
2074 isl_id_free(id);
2075 isl_aff_free(aff);
2076 return NULL;
2079 /* Replace the identifier of the input tuple of "aff" by "id".
2080 * type is currently required to be equal to isl_dim_in
2082 __isl_give isl_aff *isl_aff_set_tuple_id(__isl_take isl_aff *aff,
2083 enum isl_dim_type type, __isl_take isl_id *id)
2085 aff = isl_aff_cow(aff);
2086 if (!aff)
2087 goto error;
2088 if (type != isl_dim_in)
2089 isl_die(aff->v->ctx, isl_error_invalid,
2090 "cannot only set id of input tuple", goto error);
2091 aff->ls = isl_local_space_set_tuple_id(aff->ls, isl_dim_set, id);
2092 if (!aff->ls)
2093 return isl_aff_free(aff);
2095 return aff;
2096 error:
2097 isl_id_free(id);
2098 isl_aff_free(aff);
2099 return NULL;
2102 /* Exploit the equalities in "eq" to simplify the affine expression
2103 * and the expressions of the integer divisions in the local space.
2104 * The integer divisions in this local space are assumed to appear
2105 * as regular dimensions in "eq".
2107 static __isl_give isl_aff *isl_aff_substitute_equalities_lifted(
2108 __isl_take isl_aff *aff, __isl_take isl_basic_set *eq)
2110 int i, j;
2111 unsigned o_div;
2112 unsigned n_div;
2114 if (!eq)
2115 goto error;
2116 if (eq->n_eq == 0) {
2117 isl_basic_set_free(eq);
2118 return aff;
2121 aff = isl_aff_cow(aff);
2122 if (!aff)
2123 goto error;
2125 aff->ls = isl_local_space_substitute_equalities(aff->ls,
2126 isl_basic_set_copy(eq));
2127 aff->v = isl_vec_cow(aff->v);
2128 if (!aff->ls || !aff->v)
2129 goto error;
2131 o_div = isl_basic_set_offset(eq, isl_dim_div);
2132 n_div = eq->n_div;
2133 for (i = 0; i < eq->n_eq; ++i) {
2134 j = isl_seq_last_non_zero(eq->eq[i], o_div + n_div);
2135 if (j < 0 || j == 0 || j >= o_div)
2136 continue;
2138 isl_seq_elim(aff->v->el + 1, eq->eq[i], j, o_div,
2139 &aff->v->el[0]);
2142 isl_basic_set_free(eq);
2143 aff = isl_aff_normalize(aff);
2144 return aff;
2145 error:
2146 isl_basic_set_free(eq);
2147 isl_aff_free(aff);
2148 return NULL;
2151 /* Exploit the equalities in "eq" to simplify the affine expression
2152 * and the expressions of the integer divisions in the local space.
2154 __isl_give isl_aff *isl_aff_substitute_equalities(__isl_take isl_aff *aff,
2155 __isl_take isl_basic_set *eq)
2157 isl_size n_div;
2159 n_div = isl_aff_domain_dim(aff, isl_dim_div);
2160 if (n_div < 0)
2161 goto error;
2162 if (n_div > 0)
2163 eq = isl_basic_set_add_dims(eq, isl_dim_set, n_div);
2164 return isl_aff_substitute_equalities_lifted(aff, eq);
2165 error:
2166 isl_basic_set_free(eq);
2167 isl_aff_free(aff);
2168 return NULL;
2171 /* Look for equalities among the variables shared by context and aff
2172 * and the integer divisions of aff, if any.
2173 * The equalities are then used to eliminate coefficients and/or integer
2174 * divisions from aff.
2176 __isl_give isl_aff *isl_aff_gist(__isl_take isl_aff *aff,
2177 __isl_take isl_set *context)
2179 isl_local_space *ls;
2180 isl_basic_set *hull;
2182 ls = isl_aff_get_domain_local_space(aff);
2183 context = isl_local_space_lift_set(ls, context);
2185 hull = isl_set_affine_hull(context);
2186 return isl_aff_substitute_equalities_lifted(aff, hull);
2189 __isl_give isl_aff *isl_aff_gist_params(__isl_take isl_aff *aff,
2190 __isl_take isl_set *context)
2192 isl_set *dom_context = isl_set_universe(isl_aff_get_domain_space(aff));
2193 dom_context = isl_set_intersect_params(dom_context, context);
2194 return isl_aff_gist(aff, dom_context);
2197 /* Return a basic set containing those elements in the space
2198 * of aff where it is positive. "rational" should not be set.
2200 * If "aff" is NaN, then it is not positive.
2202 static __isl_give isl_basic_set *aff_pos_basic_set(__isl_take isl_aff *aff,
2203 int rational, void *user)
2205 isl_constraint *ineq;
2206 isl_basic_set *bset;
2207 isl_val *c;
2209 if (!aff)
2210 return NULL;
2211 if (isl_aff_is_nan(aff)) {
2212 isl_space *space = isl_aff_get_domain_space(aff);
2213 isl_aff_free(aff);
2214 return isl_basic_set_empty(space);
2216 if (rational)
2217 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2218 "rational sets not supported", goto error);
2220 ineq = isl_inequality_from_aff(aff);
2221 c = isl_constraint_get_constant_val(ineq);
2222 c = isl_val_sub_ui(c, 1);
2223 ineq = isl_constraint_set_constant_val(ineq, c);
2225 bset = isl_basic_set_from_constraint(ineq);
2226 bset = isl_basic_set_simplify(bset);
2227 return bset;
2228 error:
2229 isl_aff_free(aff);
2230 return NULL;
2233 /* Return a basic set containing those elements in the space
2234 * of aff where it is non-negative.
2235 * If "rational" is set, then return a rational basic set.
2237 * If "aff" is NaN, then it is not non-negative (it's not negative either).
2239 static __isl_give isl_basic_set *aff_nonneg_basic_set(
2240 __isl_take isl_aff *aff, int rational, void *user)
2242 isl_constraint *ineq;
2243 isl_basic_set *bset;
2245 if (!aff)
2246 return NULL;
2247 if (isl_aff_is_nan(aff)) {
2248 isl_space *space = isl_aff_get_domain_space(aff);
2249 isl_aff_free(aff);
2250 return isl_basic_set_empty(space);
2253 ineq = isl_inequality_from_aff(aff);
2255 bset = isl_basic_set_from_constraint(ineq);
2256 if (rational)
2257 bset = isl_basic_set_set_rational(bset);
2258 bset = isl_basic_set_simplify(bset);
2259 return bset;
2262 /* Return a basic set containing those elements in the space
2263 * of aff where it is non-negative.
2265 __isl_give isl_basic_set *isl_aff_nonneg_basic_set(__isl_take isl_aff *aff)
2267 return aff_nonneg_basic_set(aff, 0, NULL);
2270 /* Return a basic set containing those elements in the domain space
2271 * of "aff" where it is positive.
2273 __isl_give isl_basic_set *isl_aff_pos_basic_set(__isl_take isl_aff *aff)
2275 aff = isl_aff_add_constant_num_si(aff, -1);
2276 return isl_aff_nonneg_basic_set(aff);
2279 /* Return a basic set containing those elements in the domain space
2280 * of aff where it is negative.
2282 __isl_give isl_basic_set *isl_aff_neg_basic_set(__isl_take isl_aff *aff)
2284 aff = isl_aff_neg(aff);
2285 return isl_aff_pos_basic_set(aff);
2288 /* Return a basic set containing those elements in the space
2289 * of aff where it is zero.
2290 * If "rational" is set, then return a rational basic set.
2292 * If "aff" is NaN, then it is not zero.
2294 static __isl_give isl_basic_set *aff_zero_basic_set(__isl_take isl_aff *aff,
2295 int rational, void *user)
2297 isl_constraint *ineq;
2298 isl_basic_set *bset;
2300 if (!aff)
2301 return NULL;
2302 if (isl_aff_is_nan(aff)) {
2303 isl_space *space = isl_aff_get_domain_space(aff);
2304 isl_aff_free(aff);
2305 return isl_basic_set_empty(space);
2308 ineq = isl_equality_from_aff(aff);
2310 bset = isl_basic_set_from_constraint(ineq);
2311 if (rational)
2312 bset = isl_basic_set_set_rational(bset);
2313 bset = isl_basic_set_simplify(bset);
2314 return bset;
2317 /* Return a basic set containing those elements in the space
2318 * of aff where it is zero.
2320 __isl_give isl_basic_set *isl_aff_zero_basic_set(__isl_take isl_aff *aff)
2322 return aff_zero_basic_set(aff, 0, NULL);
2325 /* Return a basic set containing those elements in the shared space
2326 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2328 __isl_give isl_basic_set *isl_aff_ge_basic_set(__isl_take isl_aff *aff1,
2329 __isl_take isl_aff *aff2)
2331 aff1 = isl_aff_sub(aff1, aff2);
2333 return isl_aff_nonneg_basic_set(aff1);
2336 /* Return a basic set containing those elements in the shared domain space
2337 * of "aff1" and "aff2" where "aff1" is greater than "aff2".
2339 __isl_give isl_basic_set *isl_aff_gt_basic_set(__isl_take isl_aff *aff1,
2340 __isl_take isl_aff *aff2)
2342 aff1 = isl_aff_sub(aff1, aff2);
2344 return isl_aff_pos_basic_set(aff1);
2347 /* Return a set containing those elements in the shared space
2348 * of aff1 and aff2 where aff1 is greater than or equal to aff2.
2350 __isl_give isl_set *isl_aff_ge_set(__isl_take isl_aff *aff1,
2351 __isl_take isl_aff *aff2)
2353 return isl_set_from_basic_set(isl_aff_ge_basic_set(aff1, aff2));
2356 /* Return a set containing those elements in the shared domain space
2357 * of aff1 and aff2 where aff1 is greater than aff2.
2359 * If either of the two inputs is NaN, then the result is empty,
2360 * as comparisons with NaN always return false.
2362 __isl_give isl_set *isl_aff_gt_set(__isl_take isl_aff *aff1,
2363 __isl_take isl_aff *aff2)
2365 return isl_set_from_basic_set(isl_aff_gt_basic_set(aff1, aff2));
2368 /* Return a basic set containing those elements in the shared space
2369 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2371 __isl_give isl_basic_set *isl_aff_le_basic_set(__isl_take isl_aff *aff1,
2372 __isl_take isl_aff *aff2)
2374 return isl_aff_ge_basic_set(aff2, aff1);
2377 /* Return a basic set containing those elements in the shared domain space
2378 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2380 __isl_give isl_basic_set *isl_aff_lt_basic_set(__isl_take isl_aff *aff1,
2381 __isl_take isl_aff *aff2)
2383 return isl_aff_gt_basic_set(aff2, aff1);
2386 /* Return a set containing those elements in the shared space
2387 * of aff1 and aff2 where aff1 is smaller than or equal to aff2.
2389 __isl_give isl_set *isl_aff_le_set(__isl_take isl_aff *aff1,
2390 __isl_take isl_aff *aff2)
2392 return isl_aff_ge_set(aff2, aff1);
2395 /* Return a set containing those elements in the shared domain space
2396 * of "aff1" and "aff2" where "aff1" is smaller than "aff2".
2398 __isl_give isl_set *isl_aff_lt_set(__isl_take isl_aff *aff1,
2399 __isl_take isl_aff *aff2)
2401 return isl_set_from_basic_set(isl_aff_lt_basic_set(aff1, aff2));
2404 /* Return a basic set containing those elements in the shared space
2405 * of aff1 and aff2 where aff1 and aff2 are equal.
2407 __isl_give isl_basic_set *isl_aff_eq_basic_set(__isl_take isl_aff *aff1,
2408 __isl_take isl_aff *aff2)
2410 aff1 = isl_aff_sub(aff1, aff2);
2412 return isl_aff_zero_basic_set(aff1);
2415 /* Return a set containing those elements in the shared space
2416 * of aff1 and aff2 where aff1 and aff2 are equal.
2418 __isl_give isl_set *isl_aff_eq_set(__isl_take isl_aff *aff1,
2419 __isl_take isl_aff *aff2)
2421 return isl_set_from_basic_set(isl_aff_eq_basic_set(aff1, aff2));
2424 /* Return a set containing those elements in the shared domain space
2425 * of aff1 and aff2 where aff1 and aff2 are not equal.
2427 * If either of the two inputs is NaN, then the result is empty,
2428 * as comparisons with NaN always return false.
2430 __isl_give isl_set *isl_aff_ne_set(__isl_take isl_aff *aff1,
2431 __isl_take isl_aff *aff2)
2433 isl_set *set_lt, *set_gt;
2435 set_lt = isl_aff_lt_set(isl_aff_copy(aff1),
2436 isl_aff_copy(aff2));
2437 set_gt = isl_aff_gt_set(aff1, aff2);
2438 return isl_set_union_disjoint(set_lt, set_gt);
2441 __isl_give isl_aff *isl_aff_add_on_domain(__isl_keep isl_set *dom,
2442 __isl_take isl_aff *aff1, __isl_take isl_aff *aff2)
2444 aff1 = isl_aff_add(aff1, aff2);
2445 aff1 = isl_aff_gist(aff1, isl_set_copy(dom));
2446 return aff1;
2449 isl_bool isl_aff_is_empty(__isl_keep isl_aff *aff)
2451 if (!aff)
2452 return isl_bool_error;
2454 return isl_bool_false;
2457 #undef TYPE
2458 #define TYPE isl_aff
2459 static
2460 #include "check_type_range_templ.c"
2462 /* Check whether the given affine expression has non-zero coefficient
2463 * for any dimension in the given range or if any of these dimensions
2464 * appear with non-zero coefficients in any of the integer divisions
2465 * involved in the affine expression.
2467 isl_bool isl_aff_involves_dims(__isl_keep isl_aff *aff,
2468 enum isl_dim_type type, unsigned first, unsigned n)
2470 int i;
2471 int *active = NULL;
2472 isl_bool involves = isl_bool_false;
2474 if (!aff)
2475 return isl_bool_error;
2476 if (n == 0)
2477 return isl_bool_false;
2478 if (isl_aff_check_range(aff, type, first, n) < 0)
2479 return isl_bool_error;
2481 active = isl_local_space_get_active(aff->ls, aff->v->el + 2);
2482 if (!active)
2483 goto error;
2485 first += isl_local_space_offset(aff->ls, type) - 1;
2486 for (i = 0; i < n; ++i)
2487 if (active[first + i]) {
2488 involves = isl_bool_true;
2489 break;
2492 free(active);
2494 return involves;
2495 error:
2496 free(active);
2497 return isl_bool_error;
2500 /* Does "aff" involve any local variables, i.e., integer divisions?
2502 isl_bool isl_aff_involves_locals(__isl_keep isl_aff *aff)
2504 isl_size n;
2506 n = isl_aff_dim(aff, isl_dim_div);
2507 if (n < 0)
2508 return isl_bool_error;
2509 return isl_aff_involves_dims(aff, isl_dim_div, 0, n);
2512 __isl_give isl_aff *isl_aff_drop_dims(__isl_take isl_aff *aff,
2513 enum isl_dim_type type, unsigned first, unsigned n)
2515 isl_ctx *ctx;
2517 if (!aff)
2518 return NULL;
2519 if (type == isl_dim_out)
2520 isl_die(aff->v->ctx, isl_error_invalid,
2521 "cannot drop output/set dimension",
2522 return isl_aff_free(aff));
2523 if (type == isl_dim_in)
2524 type = isl_dim_set;
2525 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2526 return aff;
2528 ctx = isl_aff_get_ctx(aff);
2529 if (isl_local_space_check_range(aff->ls, type, first, n) < 0)
2530 return isl_aff_free(aff);
2532 aff = isl_aff_cow(aff);
2533 if (!aff)
2534 return NULL;
2536 aff->ls = isl_local_space_drop_dims(aff->ls, type, first, n);
2537 if (!aff->ls)
2538 return isl_aff_free(aff);
2540 first += 1 + isl_local_space_offset(aff->ls, type);
2541 aff->v = isl_vec_drop_els(aff->v, first, n);
2542 if (!aff->v)
2543 return isl_aff_free(aff);
2545 return aff;
2548 /* Is the domain of "aff" a product?
2550 static isl_bool isl_aff_domain_is_product(__isl_keep isl_aff *aff)
2552 return isl_space_is_product(isl_aff_peek_domain_space(aff));
2555 #undef TYPE
2556 #define TYPE isl_aff
2557 #include <isl_domain_factor_templ.c>
2559 /* Project the domain of the affine expression onto its parameter space.
2560 * The affine expression may not involve any of the domain dimensions.
2562 __isl_give isl_aff *isl_aff_project_domain_on_params(__isl_take isl_aff *aff)
2564 isl_space *space;
2565 isl_size n;
2567 n = isl_aff_dim(aff, isl_dim_in);
2568 if (n < 0)
2569 return isl_aff_free(aff);
2570 aff = isl_aff_drop_domain(aff, 0, n);
2571 space = isl_aff_get_domain_space(aff);
2572 space = isl_space_params(space);
2573 aff = isl_aff_reset_domain_space(aff, space);
2574 return aff;
2577 /* Convert an affine expression defined over a parameter domain
2578 * into one that is defined over a zero-dimensional set.
2580 __isl_give isl_aff *isl_aff_from_range(__isl_take isl_aff *aff)
2582 isl_local_space *ls;
2584 ls = isl_aff_take_domain_local_space(aff);
2585 ls = isl_local_space_set_from_params(ls);
2586 aff = isl_aff_restore_domain_local_space(aff, ls);
2588 return aff;
2591 __isl_give isl_aff *isl_aff_insert_dims(__isl_take isl_aff *aff,
2592 enum isl_dim_type type, unsigned first, unsigned n)
2594 isl_ctx *ctx;
2596 if (!aff)
2597 return NULL;
2598 if (type == isl_dim_out)
2599 isl_die(aff->v->ctx, isl_error_invalid,
2600 "cannot insert output/set dimensions",
2601 return isl_aff_free(aff));
2602 if (type == isl_dim_in)
2603 type = isl_dim_set;
2604 if (n == 0 && !isl_local_space_is_named_or_nested(aff->ls, type))
2605 return aff;
2607 ctx = isl_aff_get_ctx(aff);
2608 if (isl_local_space_check_range(aff->ls, type, first, 0) < 0)
2609 return isl_aff_free(aff);
2611 aff = isl_aff_cow(aff);
2612 if (!aff)
2613 return NULL;
2615 aff->ls = isl_local_space_insert_dims(aff->ls, type, first, n);
2616 if (!aff->ls)
2617 return isl_aff_free(aff);
2619 first += 1 + isl_local_space_offset(aff->ls, type);
2620 aff->v = isl_vec_insert_zero_els(aff->v, first, n);
2621 if (!aff->v)
2622 return isl_aff_free(aff);
2624 return aff;
2627 __isl_give isl_aff *isl_aff_add_dims(__isl_take isl_aff *aff,
2628 enum isl_dim_type type, unsigned n)
2630 isl_size pos;
2632 pos = isl_aff_dim(aff, type);
2633 if (pos < 0)
2634 return isl_aff_free(aff);
2636 return isl_aff_insert_dims(aff, type, pos, n);
2639 __isl_give isl_pw_aff *isl_pw_aff_add_dims(__isl_take isl_pw_aff *pwaff,
2640 enum isl_dim_type type, unsigned n)
2642 isl_size pos;
2644 pos = isl_pw_aff_dim(pwaff, type);
2645 if (pos < 0)
2646 return isl_pw_aff_free(pwaff);
2648 return isl_pw_aff_insert_dims(pwaff, type, pos, n);
2651 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "aff"
2652 * to dimensions of "dst_type" at "dst_pos".
2654 * We only support moving input dimensions to parameters and vice versa.
2656 __isl_give isl_aff *isl_aff_move_dims(__isl_take isl_aff *aff,
2657 enum isl_dim_type dst_type, unsigned dst_pos,
2658 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2660 unsigned g_dst_pos;
2661 unsigned g_src_pos;
2662 isl_size src_off, dst_off;
2664 if (!aff)
2665 return NULL;
2666 if (n == 0 &&
2667 !isl_local_space_is_named_or_nested(aff->ls, src_type) &&
2668 !isl_local_space_is_named_or_nested(aff->ls, dst_type))
2669 return aff;
2671 if (dst_type == isl_dim_out || src_type == isl_dim_out)
2672 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2673 "cannot move output/set dimension",
2674 return isl_aff_free(aff));
2675 if (dst_type == isl_dim_div || src_type == isl_dim_div)
2676 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
2677 "cannot move divs", return isl_aff_free(aff));
2678 if (dst_type == isl_dim_in)
2679 dst_type = isl_dim_set;
2680 if (src_type == isl_dim_in)
2681 src_type = isl_dim_set;
2683 if (isl_local_space_check_range(aff->ls, src_type, src_pos, n) < 0)
2684 return isl_aff_free(aff);
2685 if (dst_type == src_type)
2686 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2687 "moving dims within the same type not supported",
2688 return isl_aff_free(aff));
2690 aff = isl_aff_cow(aff);
2691 src_off = isl_aff_domain_offset(aff, src_type);
2692 dst_off = isl_aff_domain_offset(aff, dst_type);
2693 if (src_off < 0 || dst_off < 0)
2694 return isl_aff_free(aff);
2696 g_src_pos = 1 + src_off + src_pos;
2697 g_dst_pos = 1 + dst_off + dst_pos;
2698 if (dst_type > src_type)
2699 g_dst_pos -= n;
2701 aff->v = isl_vec_move_els(aff->v, g_dst_pos, g_src_pos, n);
2702 aff->ls = isl_local_space_move_dims(aff->ls, dst_type, dst_pos,
2703 src_type, src_pos, n);
2704 if (!aff->v || !aff->ls)
2705 return isl_aff_free(aff);
2707 aff = sort_divs(aff);
2709 return aff;
2712 /* Return a zero isl_aff in the given space.
2714 * This is a helper function for isl_pw_*_as_* that ensures a uniform
2715 * interface over all piecewise types.
2717 static __isl_give isl_aff *isl_aff_zero_in_space(__isl_take isl_space *space)
2719 isl_local_space *ls;
2721 ls = isl_local_space_from_space(isl_space_domain(space));
2722 return isl_aff_zero_on_domain(ls);
2725 #define isl_aff_involves_nan isl_aff_is_nan
2727 #undef PW
2728 #define PW isl_pw_aff
2729 #undef BASE
2730 #define BASE aff
2731 #undef EL_IS_ZERO
2732 #define EL_IS_ZERO is_empty
2733 #undef ZERO
2734 #define ZERO empty
2735 #undef IS_ZERO
2736 #define IS_ZERO is_empty
2737 #undef FIELD
2738 #define FIELD aff
2739 #undef DEFAULT_IS_ZERO
2740 #define DEFAULT_IS_ZERO 0
2742 #include <isl_pw_templ.c>
2743 #include <isl_pw_add_constant_val_templ.c>
2744 #include <isl_pw_bind_domain_templ.c>
2745 #include <isl_pw_eval.c>
2746 #include <isl_pw_hash.c>
2747 #include <isl_pw_insert_dims_templ.c>
2748 #include <isl_pw_move_dims_templ.c>
2749 #include <isl_pw_neg_templ.c>
2750 #include <isl_pw_pullback_templ.c>
2751 #include <isl_pw_sub_templ.c>
2752 #include <isl_pw_union_opt.c>
2754 #undef BASE
2755 #define BASE pw_aff
2757 #include <isl_union_single.c>
2758 #include <isl_union_neg.c>
2760 /* Compute a piecewise quasi-affine expression with a domain that
2761 * is the union of those of pwaff1 and pwaff2 and such that on each
2762 * cell, the quasi-affine expression is the maximum of those of pwaff1
2763 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2764 * cell, then the associated expression is the defined one.
2766 __isl_give isl_pw_aff *isl_pw_aff_union_max(__isl_take isl_pw_aff *pwaff1,
2767 __isl_take isl_pw_aff *pwaff2)
2769 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2770 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_ge_set);
2773 /* Compute a piecewise quasi-affine expression with a domain that
2774 * is the union of those of pwaff1 and pwaff2 and such that on each
2775 * cell, the quasi-affine expression is the minimum of those of pwaff1
2776 * and pwaff2. If only one of pwaff1 or pwaff2 is defined on a given
2777 * cell, then the associated expression is the defined one.
2779 __isl_give isl_pw_aff *isl_pw_aff_union_min(__isl_take isl_pw_aff *pwaff1,
2780 __isl_take isl_pw_aff *pwaff2)
2782 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2783 return isl_pw_aff_union_opt_cmp(pwaff1, pwaff2, &isl_aff_le_set);
2786 __isl_give isl_pw_aff *isl_pw_aff_union_opt(__isl_take isl_pw_aff *pwaff1,
2787 __isl_take isl_pw_aff *pwaff2, int max)
2789 if (max)
2790 return isl_pw_aff_union_max(pwaff1, pwaff2);
2791 else
2792 return isl_pw_aff_union_min(pwaff1, pwaff2);
2795 /* Is the domain of "pa" a product?
2797 static isl_bool isl_pw_aff_domain_is_product(__isl_keep isl_pw_aff *pa)
2799 return isl_space_domain_is_wrapping(isl_pw_aff_peek_space(pa));
2802 #undef TYPE
2803 #define TYPE isl_pw_aff
2804 #include <isl_domain_factor_templ.c>
2806 /* Return a set containing those elements in the domain
2807 * of "pwaff" where it satisfies "fn" (if complement is 0) or
2808 * does not satisfy "fn" (if complement is 1).
2810 * The pieces with a NaN never belong to the result since
2811 * NaN does not satisfy any property.
2813 static __isl_give isl_set *pw_aff_locus(__isl_take isl_pw_aff *pwaff,
2814 __isl_give isl_basic_set *(*fn)(__isl_take isl_aff *aff, int rational,
2815 void *user),
2816 int complement, void *user)
2818 int i;
2819 isl_set *set;
2821 if (!pwaff)
2822 return NULL;
2824 set = isl_set_empty(isl_pw_aff_get_domain_space(pwaff));
2826 for (i = 0; i < pwaff->n; ++i) {
2827 isl_basic_set *bset;
2828 isl_set *set_i, *locus;
2829 isl_bool rational;
2831 if (isl_aff_is_nan(pwaff->p[i].aff))
2832 continue;
2834 rational = isl_set_has_rational(pwaff->p[i].set);
2835 bset = fn(isl_aff_copy(pwaff->p[i].aff), rational, user);
2836 locus = isl_set_from_basic_set(bset);
2837 set_i = isl_set_copy(pwaff->p[i].set);
2838 if (complement)
2839 set_i = isl_set_subtract(set_i, locus);
2840 else
2841 set_i = isl_set_intersect(set_i, locus);
2842 set = isl_set_union_disjoint(set, set_i);
2845 isl_pw_aff_free(pwaff);
2847 return set;
2850 /* Return a set containing those elements in the domain
2851 * of "pa" where it is positive.
2853 __isl_give isl_set *isl_pw_aff_pos_set(__isl_take isl_pw_aff *pa)
2855 return pw_aff_locus(pa, &aff_pos_basic_set, 0, NULL);
2858 /* Return a set containing those elements in the domain
2859 * of pwaff where it is non-negative.
2861 __isl_give isl_set *isl_pw_aff_nonneg_set(__isl_take isl_pw_aff *pwaff)
2863 return pw_aff_locus(pwaff, &aff_nonneg_basic_set, 0, NULL);
2866 /* Return a set containing those elements in the domain
2867 * of pwaff where it is zero.
2869 __isl_give isl_set *isl_pw_aff_zero_set(__isl_take isl_pw_aff *pwaff)
2871 return pw_aff_locus(pwaff, &aff_zero_basic_set, 0, NULL);
2874 /* Return a set containing those elements in the domain
2875 * of pwaff where it is not zero.
2877 __isl_give isl_set *isl_pw_aff_non_zero_set(__isl_take isl_pw_aff *pwaff)
2879 return pw_aff_locus(pwaff, &aff_zero_basic_set, 1, NULL);
2882 /* Bind the affine function "aff" to the parameter "id",
2883 * returning the elements in the domain where the affine expression
2884 * is equal to the parameter.
2886 __isl_give isl_basic_set *isl_aff_bind_id(__isl_take isl_aff *aff,
2887 __isl_take isl_id *id)
2889 isl_space *space;
2890 isl_aff *aff_id;
2892 space = isl_aff_get_domain_space(aff);
2893 space = isl_space_add_param_id(space, isl_id_copy(id));
2895 aff = isl_aff_align_params(aff, isl_space_copy(space));
2896 aff_id = isl_aff_param_on_domain_space_id(space, id);
2898 return isl_aff_eq_basic_set(aff, aff_id);
2901 /* Wrapper around isl_aff_bind_id for use as pw_aff_locus callback.
2902 * "rational" should not be set.
2904 static __isl_give isl_basic_set *aff_bind_id(__isl_take isl_aff *aff,
2905 int rational, void *user)
2907 isl_id *id = user;
2909 if (!aff)
2910 return NULL;
2911 if (rational)
2912 isl_die(isl_aff_get_ctx(aff), isl_error_unsupported,
2913 "rational binding not supported", goto error);
2914 return isl_aff_bind_id(aff, isl_id_copy(id));
2915 error:
2916 isl_aff_free(aff);
2917 return NULL;
2920 /* Bind the piecewise affine function "pa" to the parameter "id",
2921 * returning the elements in the domain where the expression
2922 * is equal to the parameter.
2924 __isl_give isl_set *isl_pw_aff_bind_id(__isl_take isl_pw_aff *pa,
2925 __isl_take isl_id *id)
2927 isl_set *bound;
2929 bound = pw_aff_locus(pa, &aff_bind_id, 0, id);
2930 isl_id_free(id);
2932 return bound;
2935 /* Return a set containing those elements in the shared domain
2936 * of pwaff1 and pwaff2 where pwaff1 is greater than (or equal) to pwaff2.
2938 * We compute the difference on the shared domain and then construct
2939 * the set of values where this difference is non-negative.
2940 * If strict is set, we first subtract 1 from the difference.
2941 * If equal is set, we only return the elements where pwaff1 and pwaff2
2942 * are equal.
2944 static __isl_give isl_set *pw_aff_gte_set(__isl_take isl_pw_aff *pwaff1,
2945 __isl_take isl_pw_aff *pwaff2, int strict, int equal)
2947 isl_set *set1, *set2;
2949 set1 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff1));
2950 set2 = isl_pw_aff_domain(isl_pw_aff_copy(pwaff2));
2951 set1 = isl_set_intersect(set1, set2);
2952 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, isl_set_copy(set1));
2953 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, isl_set_copy(set1));
2954 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_neg(pwaff2));
2956 if (strict) {
2957 isl_space *space = isl_set_get_space(set1);
2958 isl_aff *aff;
2959 aff = isl_aff_zero_on_domain(isl_local_space_from_space(space));
2960 aff = isl_aff_add_constant_si(aff, -1);
2961 pwaff1 = isl_pw_aff_add(pwaff1, isl_pw_aff_alloc(set1, aff));
2962 } else
2963 isl_set_free(set1);
2965 if (equal)
2966 return isl_pw_aff_zero_set(pwaff1);
2967 return isl_pw_aff_nonneg_set(pwaff1);
2970 /* Return a set containing those elements in the shared domain
2971 * of pwaff1 and pwaff2 where pwaff1 is equal to pwaff2.
2973 __isl_give isl_set *isl_pw_aff_eq_set(__isl_take isl_pw_aff *pwaff1,
2974 __isl_take isl_pw_aff *pwaff2)
2976 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2977 return pw_aff_gte_set(pwaff1, pwaff2, 0, 1);
2980 /* Return a set containing those elements in the shared domain
2981 * of pwaff1 and pwaff2 where pwaff1 is greater than or equal to pwaff2.
2983 __isl_give isl_set *isl_pw_aff_ge_set(__isl_take isl_pw_aff *pwaff1,
2984 __isl_take isl_pw_aff *pwaff2)
2986 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2987 return pw_aff_gte_set(pwaff1, pwaff2, 0, 0);
2990 /* Return a set containing those elements in the shared domain
2991 * of pwaff1 and pwaff2 where pwaff1 is strictly greater than pwaff2.
2993 __isl_give isl_set *isl_pw_aff_gt_set(__isl_take isl_pw_aff *pwaff1,
2994 __isl_take isl_pw_aff *pwaff2)
2996 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
2997 return pw_aff_gte_set(pwaff1, pwaff2, 1, 0);
3000 __isl_give isl_set *isl_pw_aff_le_set(__isl_take isl_pw_aff *pwaff1,
3001 __isl_take isl_pw_aff *pwaff2)
3003 return isl_pw_aff_ge_set(pwaff2, pwaff1);
3006 __isl_give isl_set *isl_pw_aff_lt_set(__isl_take isl_pw_aff *pwaff1,
3007 __isl_take isl_pw_aff *pwaff2)
3009 return isl_pw_aff_gt_set(pwaff2, pwaff1);
3012 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3013 * where the function values are ordered in the same way as "order",
3014 * which returns a set in the shared domain of its two arguments.
3016 * Let "pa1" and "pa2" be defined on domains A and B respectively.
3017 * We first pull back the two functions such that they are defined on
3018 * the domain [A -> B]. Then we apply "order", resulting in a set
3019 * in the space [A -> B]. Finally, we unwrap this set to obtain
3020 * a map in the space A -> B.
3022 static __isl_give isl_map *isl_pw_aff_order_map(
3023 __isl_take isl_pw_aff *pa1, __isl_take isl_pw_aff *pa2,
3024 __isl_give isl_set *(*order)(__isl_take isl_pw_aff *pa1,
3025 __isl_take isl_pw_aff *pa2))
3027 isl_space *space1, *space2;
3028 isl_multi_aff *ma;
3029 isl_set *set;
3031 isl_pw_aff_align_params_bin(&pa1, &pa2);
3032 space1 = isl_space_domain(isl_pw_aff_get_space(pa1));
3033 space2 = isl_space_domain(isl_pw_aff_get_space(pa2));
3034 space1 = isl_space_map_from_domain_and_range(space1, space2);
3035 ma = isl_multi_aff_domain_map(isl_space_copy(space1));
3036 pa1 = isl_pw_aff_pullback_multi_aff(pa1, ma);
3037 ma = isl_multi_aff_range_map(space1);
3038 pa2 = isl_pw_aff_pullback_multi_aff(pa2, ma);
3039 set = order(pa1, pa2);
3041 return isl_set_unwrap(set);
3044 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3045 * where the function values are equal.
3047 __isl_give isl_map *isl_pw_aff_eq_map(__isl_take isl_pw_aff *pa1,
3048 __isl_take isl_pw_aff *pa2)
3050 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_eq_set);
3053 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3054 * where the function value of "pa1" is less than or equal to
3055 * the function value of "pa2".
3057 __isl_give isl_map *isl_pw_aff_le_map(__isl_take isl_pw_aff *pa1,
3058 __isl_take isl_pw_aff *pa2)
3060 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_le_set);
3063 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3064 * where the function value of "pa1" is less than the function value of "pa2".
3066 __isl_give isl_map *isl_pw_aff_lt_map(__isl_take isl_pw_aff *pa1,
3067 __isl_take isl_pw_aff *pa2)
3069 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_lt_set);
3072 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3073 * where the function value of "pa1" is greater than or equal to
3074 * the function value of "pa2".
3076 __isl_give isl_map *isl_pw_aff_ge_map(__isl_take isl_pw_aff *pa1,
3077 __isl_take isl_pw_aff *pa2)
3079 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_ge_set);
3082 /* Return a map containing pairs of elements in the domains of "pa1" and "pa2"
3083 * where the function value of "pa1" is greater than the function value
3084 * of "pa2".
3086 __isl_give isl_map *isl_pw_aff_gt_map(__isl_take isl_pw_aff *pa1,
3087 __isl_take isl_pw_aff *pa2)
3089 return isl_pw_aff_order_map(pa1, pa2, &isl_pw_aff_gt_set);
3092 /* Return a set containing those elements in the shared domain
3093 * of the elements of list1 and list2 where each element in list1
3094 * has the relation specified by "fn" with each element in list2.
3096 static __isl_give isl_set *pw_aff_list_set(__isl_take isl_pw_aff_list *list1,
3097 __isl_take isl_pw_aff_list *list2,
3098 __isl_give isl_set *(*fn)(__isl_take isl_pw_aff *pwaff1,
3099 __isl_take isl_pw_aff *pwaff2))
3101 int i, j;
3102 isl_ctx *ctx;
3103 isl_set *set;
3105 if (!list1 || !list2)
3106 goto error;
3108 ctx = isl_pw_aff_list_get_ctx(list1);
3109 if (list1->n < 1 || list2->n < 1)
3110 isl_die(ctx, isl_error_invalid,
3111 "list should contain at least one element", goto error);
3113 set = isl_set_universe(isl_pw_aff_get_domain_space(list1->p[0]));
3114 for (i = 0; i < list1->n; ++i)
3115 for (j = 0; j < list2->n; ++j) {
3116 isl_set *set_ij;
3118 set_ij = fn(isl_pw_aff_copy(list1->p[i]),
3119 isl_pw_aff_copy(list2->p[j]));
3120 set = isl_set_intersect(set, set_ij);
3123 isl_pw_aff_list_free(list1);
3124 isl_pw_aff_list_free(list2);
3125 return set;
3126 error:
3127 isl_pw_aff_list_free(list1);
3128 isl_pw_aff_list_free(list2);
3129 return NULL;
3132 /* Return a set containing those elements in the shared domain
3133 * of the elements of list1 and list2 where each element in list1
3134 * is equal to each element in list2.
3136 __isl_give isl_set *isl_pw_aff_list_eq_set(__isl_take isl_pw_aff_list *list1,
3137 __isl_take isl_pw_aff_list *list2)
3139 return pw_aff_list_set(list1, list2, &isl_pw_aff_eq_set);
3142 __isl_give isl_set *isl_pw_aff_list_ne_set(__isl_take isl_pw_aff_list *list1,
3143 __isl_take isl_pw_aff_list *list2)
3145 return pw_aff_list_set(list1, list2, &isl_pw_aff_ne_set);
3148 /* Return a set containing those elements in the shared domain
3149 * of the elements of list1 and list2 where each element in list1
3150 * is less than or equal to each element in list2.
3152 __isl_give isl_set *isl_pw_aff_list_le_set(__isl_take isl_pw_aff_list *list1,
3153 __isl_take isl_pw_aff_list *list2)
3155 return pw_aff_list_set(list1, list2, &isl_pw_aff_le_set);
3158 __isl_give isl_set *isl_pw_aff_list_lt_set(__isl_take isl_pw_aff_list *list1,
3159 __isl_take isl_pw_aff_list *list2)
3161 return pw_aff_list_set(list1, list2, &isl_pw_aff_lt_set);
3164 __isl_give isl_set *isl_pw_aff_list_ge_set(__isl_take isl_pw_aff_list *list1,
3165 __isl_take isl_pw_aff_list *list2)
3167 return pw_aff_list_set(list1, list2, &isl_pw_aff_ge_set);
3170 __isl_give isl_set *isl_pw_aff_list_gt_set(__isl_take isl_pw_aff_list *list1,
3171 __isl_take isl_pw_aff_list *list2)
3173 return pw_aff_list_set(list1, list2, &isl_pw_aff_gt_set);
3177 /* Return a set containing those elements in the shared domain
3178 * of pwaff1 and pwaff2 where pwaff1 is not equal to pwaff2.
3180 __isl_give isl_set *isl_pw_aff_ne_set(__isl_take isl_pw_aff *pwaff1,
3181 __isl_take isl_pw_aff *pwaff2)
3183 isl_set *set_lt, *set_gt;
3185 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3186 set_lt = isl_pw_aff_lt_set(isl_pw_aff_copy(pwaff1),
3187 isl_pw_aff_copy(pwaff2));
3188 set_gt = isl_pw_aff_gt_set(pwaff1, pwaff2);
3189 return isl_set_union_disjoint(set_lt, set_gt);
3192 __isl_give isl_pw_aff *isl_pw_aff_scale_down(__isl_take isl_pw_aff *pwaff,
3193 isl_int v)
3195 int i;
3197 if (isl_int_is_one(v))
3198 return pwaff;
3199 if (!isl_int_is_pos(v))
3200 isl_die(isl_pw_aff_get_ctx(pwaff), isl_error_invalid,
3201 "factor needs to be positive",
3202 return isl_pw_aff_free(pwaff));
3203 pwaff = isl_pw_aff_cow(pwaff);
3204 if (!pwaff)
3205 return NULL;
3206 if (pwaff->n == 0)
3207 return pwaff;
3209 for (i = 0; i < pwaff->n; ++i) {
3210 pwaff->p[i].aff = isl_aff_scale_down(pwaff->p[i].aff, v);
3211 if (!pwaff->p[i].aff)
3212 return isl_pw_aff_free(pwaff);
3215 return pwaff;
3218 __isl_give isl_pw_aff *isl_pw_aff_floor(__isl_take isl_pw_aff *pwaff)
3220 int i;
3222 pwaff = isl_pw_aff_cow(pwaff);
3223 if (!pwaff)
3224 return NULL;
3225 if (pwaff->n == 0)
3226 return pwaff;
3228 for (i = 0; i < pwaff->n; ++i) {
3229 pwaff->p[i].aff = isl_aff_floor(pwaff->p[i].aff);
3230 if (!pwaff->p[i].aff)
3231 return isl_pw_aff_free(pwaff);
3234 return pwaff;
3237 __isl_give isl_pw_aff *isl_pw_aff_ceil(__isl_take isl_pw_aff *pwaff)
3239 int i;
3241 pwaff = isl_pw_aff_cow(pwaff);
3242 if (!pwaff)
3243 return NULL;
3244 if (pwaff->n == 0)
3245 return pwaff;
3247 for (i = 0; i < pwaff->n; ++i) {
3248 pwaff->p[i].aff = isl_aff_ceil(pwaff->p[i].aff);
3249 if (!pwaff->p[i].aff)
3250 return isl_pw_aff_free(pwaff);
3253 return pwaff;
3256 /* Assuming that "cond1" and "cond2" are disjoint,
3257 * return an affine expression that is equal to pwaff1 on cond1
3258 * and to pwaff2 on cond2.
3260 static __isl_give isl_pw_aff *isl_pw_aff_select(
3261 __isl_take isl_set *cond1, __isl_take isl_pw_aff *pwaff1,
3262 __isl_take isl_set *cond2, __isl_take isl_pw_aff *pwaff2)
3264 pwaff1 = isl_pw_aff_intersect_domain(pwaff1, cond1);
3265 pwaff2 = isl_pw_aff_intersect_domain(pwaff2, cond2);
3267 return isl_pw_aff_add_disjoint(pwaff1, pwaff2);
3270 /* Return an affine expression that is equal to pwaff_true for elements
3271 * where "cond" is non-zero and to pwaff_false for elements where "cond"
3272 * is zero.
3273 * That is, return cond ? pwaff_true : pwaff_false;
3275 * If "cond" involves and NaN, then we conservatively return a NaN
3276 * on its entire domain. In principle, we could consider the pieces
3277 * where it is NaN separately from those where it is not.
3279 * If "pwaff_true" and "pwaff_false" are obviously equal to each other,
3280 * then only use the domain of "cond" to restrict the domain.
3282 __isl_give isl_pw_aff *isl_pw_aff_cond(__isl_take isl_pw_aff *cond,
3283 __isl_take isl_pw_aff *pwaff_true, __isl_take isl_pw_aff *pwaff_false)
3285 isl_set *cond_true, *cond_false;
3286 isl_bool equal;
3288 if (!cond)
3289 goto error;
3290 if (isl_pw_aff_involves_nan(cond)) {
3291 isl_space *space = isl_pw_aff_get_domain_space(cond);
3292 isl_local_space *ls = isl_local_space_from_space(space);
3293 isl_pw_aff_free(cond);
3294 isl_pw_aff_free(pwaff_true);
3295 isl_pw_aff_free(pwaff_false);
3296 return isl_pw_aff_nan_on_domain(ls);
3299 pwaff_true = isl_pw_aff_align_params(pwaff_true,
3300 isl_pw_aff_get_space(pwaff_false));
3301 pwaff_false = isl_pw_aff_align_params(pwaff_false,
3302 isl_pw_aff_get_space(pwaff_true));
3303 equal = isl_pw_aff_plain_is_equal(pwaff_true, pwaff_false);
3304 if (equal < 0)
3305 goto error;
3306 if (equal) {
3307 isl_set *dom;
3309 dom = isl_set_coalesce(isl_pw_aff_domain(cond));
3310 isl_pw_aff_free(pwaff_false);
3311 return isl_pw_aff_intersect_domain(pwaff_true, dom);
3314 cond_true = isl_pw_aff_non_zero_set(isl_pw_aff_copy(cond));
3315 cond_false = isl_pw_aff_zero_set(cond);
3316 return isl_pw_aff_select(cond_true, pwaff_true,
3317 cond_false, pwaff_false);
3318 error:
3319 isl_pw_aff_free(cond);
3320 isl_pw_aff_free(pwaff_true);
3321 isl_pw_aff_free(pwaff_false);
3322 return NULL;
3325 isl_bool isl_aff_is_cst(__isl_keep isl_aff *aff)
3327 int pos;
3329 if (!aff)
3330 return isl_bool_error;
3332 pos = isl_seq_first_non_zero(aff->v->el + 2, aff->v->size - 2);
3333 return isl_bool_ok(pos == -1);
3336 /* Check whether pwaff is a piecewise constant.
3338 isl_bool isl_pw_aff_is_cst(__isl_keep isl_pw_aff *pwaff)
3340 int i;
3342 if (!pwaff)
3343 return isl_bool_error;
3345 for (i = 0; i < pwaff->n; ++i) {
3346 isl_bool is_cst = isl_aff_is_cst(pwaff->p[i].aff);
3347 if (is_cst < 0 || !is_cst)
3348 return is_cst;
3351 return isl_bool_true;
3354 /* Return the product of "aff1" and "aff2".
3356 * If either of the two is NaN, then the result is NaN.
3358 * Otherwise, at least one of "aff1" or "aff2" needs to be a constant.
3360 __isl_give isl_aff *isl_aff_mul(__isl_take isl_aff *aff1,
3361 __isl_take isl_aff *aff2)
3363 if (!aff1 || !aff2)
3364 goto error;
3366 if (isl_aff_is_nan(aff1)) {
3367 isl_aff_free(aff2);
3368 return aff1;
3370 if (isl_aff_is_nan(aff2)) {
3371 isl_aff_free(aff1);
3372 return aff2;
3375 if (!isl_aff_is_cst(aff2) && isl_aff_is_cst(aff1))
3376 return isl_aff_mul(aff2, aff1);
3378 if (!isl_aff_is_cst(aff2))
3379 isl_die(isl_aff_get_ctx(aff1), isl_error_invalid,
3380 "at least one affine expression should be constant",
3381 goto error);
3383 aff1 = isl_aff_cow(aff1);
3384 if (!aff1 || !aff2)
3385 goto error;
3387 aff1 = isl_aff_scale(aff1, aff2->v->el[1]);
3388 aff1 = isl_aff_scale_down(aff1, aff2->v->el[0]);
3390 isl_aff_free(aff2);
3391 return aff1;
3392 error:
3393 isl_aff_free(aff1);
3394 isl_aff_free(aff2);
3395 return NULL;
3398 /* Divide "aff1" by "aff2", assuming "aff2" is a constant.
3400 * If either of the two is NaN, then the result is NaN.
3402 __isl_give isl_aff *isl_aff_div(__isl_take isl_aff *aff1,
3403 __isl_take isl_aff *aff2)
3405 int is_cst;
3406 int neg;
3408 if (!aff1 || !aff2)
3409 goto error;
3411 if (isl_aff_is_nan(aff1)) {
3412 isl_aff_free(aff2);
3413 return aff1;
3415 if (isl_aff_is_nan(aff2)) {
3416 isl_aff_free(aff1);
3417 return aff2;
3420 is_cst = isl_aff_is_cst(aff2);
3421 if (is_cst < 0)
3422 goto error;
3423 if (!is_cst)
3424 isl_die(isl_aff_get_ctx(aff2), isl_error_invalid,
3425 "second argument should be a constant", goto error);
3427 if (!aff2)
3428 goto error;
3430 neg = isl_int_is_neg(aff2->v->el[1]);
3431 if (neg) {
3432 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3433 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3436 aff1 = isl_aff_scale(aff1, aff2->v->el[0]);
3437 aff1 = isl_aff_scale_down(aff1, aff2->v->el[1]);
3439 if (neg) {
3440 isl_int_neg(aff2->v->el[0], aff2->v->el[0]);
3441 isl_int_neg(aff2->v->el[1], aff2->v->el[1]);
3444 isl_aff_free(aff2);
3445 return aff1;
3446 error:
3447 isl_aff_free(aff1);
3448 isl_aff_free(aff2);
3449 return NULL;
3452 __isl_give isl_pw_aff *isl_pw_aff_add(__isl_take isl_pw_aff *pwaff1,
3453 __isl_take isl_pw_aff *pwaff2)
3455 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3456 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_add);
3459 __isl_give isl_pw_aff *isl_pw_aff_union_add(__isl_take isl_pw_aff *pwaff1,
3460 __isl_take isl_pw_aff *pwaff2)
3462 return isl_pw_aff_union_add_(pwaff1, pwaff2);
3465 __isl_give isl_pw_aff *isl_pw_aff_mul(__isl_take isl_pw_aff *pwaff1,
3466 __isl_take isl_pw_aff *pwaff2)
3468 isl_pw_aff_align_params_bin(&pwaff1, &pwaff2);
3469 return isl_pw_aff_on_shared_domain(pwaff1, pwaff2, &isl_aff_mul);
3472 /* Divide "pa1" by "pa2", assuming "pa2" is a piecewise constant.
3474 __isl_give isl_pw_aff *isl_pw_aff_div(__isl_take isl_pw_aff *pa1,
3475 __isl_take isl_pw_aff *pa2)
3477 int is_cst;
3479 is_cst = isl_pw_aff_is_cst(pa2);
3480 if (is_cst < 0)
3481 goto error;
3482 if (!is_cst)
3483 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3484 "second argument should be a piecewise constant",
3485 goto error);
3486 isl_pw_aff_align_params_bin(&pa1, &pa2);
3487 return isl_pw_aff_on_shared_domain(pa1, pa2, &isl_aff_div);
3488 error:
3489 isl_pw_aff_free(pa1);
3490 isl_pw_aff_free(pa2);
3491 return NULL;
3494 /* Compute the quotient of the integer division of "pa1" by "pa2"
3495 * with rounding towards zero.
3496 * "pa2" is assumed to be a piecewise constant.
3498 * In particular, return
3500 * pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2)
3503 __isl_give isl_pw_aff *isl_pw_aff_tdiv_q(__isl_take isl_pw_aff *pa1,
3504 __isl_take isl_pw_aff *pa2)
3506 int is_cst;
3507 isl_set *cond;
3508 isl_pw_aff *f, *c;
3510 is_cst = isl_pw_aff_is_cst(pa2);
3511 if (is_cst < 0)
3512 goto error;
3513 if (!is_cst)
3514 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3515 "second argument should be a piecewise constant",
3516 goto error);
3518 pa1 = isl_pw_aff_div(pa1, pa2);
3520 cond = isl_pw_aff_nonneg_set(isl_pw_aff_copy(pa1));
3521 f = isl_pw_aff_floor(isl_pw_aff_copy(pa1));
3522 c = isl_pw_aff_ceil(pa1);
3523 return isl_pw_aff_cond(isl_set_indicator_function(cond), f, c);
3524 error:
3525 isl_pw_aff_free(pa1);
3526 isl_pw_aff_free(pa2);
3527 return NULL;
3530 /* Compute the remainder of the integer division of "pa1" by "pa2"
3531 * with rounding towards zero.
3532 * "pa2" is assumed to be a piecewise constant.
3534 * In particular, return
3536 * pa1 - pa2 * (pa1 >= 0 ? floor(pa1/pa2) : ceil(pa1/pa2))
3539 __isl_give isl_pw_aff *isl_pw_aff_tdiv_r(__isl_take isl_pw_aff *pa1,
3540 __isl_take isl_pw_aff *pa2)
3542 int is_cst;
3543 isl_pw_aff *res;
3545 is_cst = isl_pw_aff_is_cst(pa2);
3546 if (is_cst < 0)
3547 goto error;
3548 if (!is_cst)
3549 isl_die(isl_pw_aff_get_ctx(pa2), isl_error_invalid,
3550 "second argument should be a piecewise constant",
3551 goto error);
3552 res = isl_pw_aff_tdiv_q(isl_pw_aff_copy(pa1), isl_pw_aff_copy(pa2));
3553 res = isl_pw_aff_mul(pa2, res);
3554 res = isl_pw_aff_sub(pa1, res);
3555 return res;
3556 error:
3557 isl_pw_aff_free(pa1);
3558 isl_pw_aff_free(pa2);
3559 return NULL;
3562 /* Does either of "pa1" or "pa2" involve any NaN2?
3564 static isl_bool either_involves_nan(__isl_keep isl_pw_aff *pa1,
3565 __isl_keep isl_pw_aff *pa2)
3567 isl_bool has_nan;
3569 has_nan = isl_pw_aff_involves_nan(pa1);
3570 if (has_nan < 0 || has_nan)
3571 return has_nan;
3572 return isl_pw_aff_involves_nan(pa2);
3575 /* Replace "pa1" and "pa2" (at least one of which involves a NaN)
3576 * by a NaN on their shared domain.
3578 * In principle, the result could be refined to only being NaN
3579 * on the parts of this domain where at least one of "pa1" or "pa2" is NaN.
3581 static __isl_give isl_pw_aff *replace_by_nan(__isl_take isl_pw_aff *pa1,
3582 __isl_take isl_pw_aff *pa2)
3584 isl_local_space *ls;
3585 isl_set *dom;
3586 isl_pw_aff *pa;
3588 dom = isl_set_intersect(isl_pw_aff_domain(pa1), isl_pw_aff_domain(pa2));
3589 ls = isl_local_space_from_space(isl_set_get_space(dom));
3590 pa = isl_pw_aff_nan_on_domain(ls);
3591 pa = isl_pw_aff_intersect_domain(pa, dom);
3593 return pa;
3596 static __isl_give isl_pw_aff *pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3597 __isl_take isl_pw_aff *pwaff2)
3599 isl_set *le;
3600 isl_set *dom;
3602 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3603 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3604 le = isl_pw_aff_le_set(isl_pw_aff_copy(pwaff1),
3605 isl_pw_aff_copy(pwaff2));
3606 dom = isl_set_subtract(dom, isl_set_copy(le));
3607 return isl_pw_aff_select(le, pwaff1, dom, pwaff2);
3610 static __isl_give isl_pw_aff *pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3611 __isl_take isl_pw_aff *pwaff2)
3613 isl_set *ge;
3614 isl_set *dom;
3616 dom = isl_set_intersect(isl_pw_aff_domain(isl_pw_aff_copy(pwaff1)),
3617 isl_pw_aff_domain(isl_pw_aff_copy(pwaff2)));
3618 ge = isl_pw_aff_ge_set(isl_pw_aff_copy(pwaff1),
3619 isl_pw_aff_copy(pwaff2));
3620 dom = isl_set_subtract(dom, isl_set_copy(ge));
3621 return isl_pw_aff_select(ge, pwaff1, dom, pwaff2);
3624 /* Return an expression for the minimum (if "max" is not set) or
3625 * the maximum (if "max" is set) of "pa1" and "pa2".
3626 * If either expression involves any NaN, then return a NaN
3627 * on the shared domain as result.
3629 static __isl_give isl_pw_aff *pw_aff_min_max(__isl_take isl_pw_aff *pa1,
3630 __isl_take isl_pw_aff *pa2, int max)
3632 isl_bool has_nan;
3634 has_nan = either_involves_nan(pa1, pa2);
3635 if (has_nan < 0)
3636 pa1 = isl_pw_aff_free(pa1);
3637 else if (has_nan)
3638 return replace_by_nan(pa1, pa2);
3640 isl_pw_aff_align_params_bin(&pa1, &pa2);
3641 if (max)
3642 return pw_aff_max(pa1, pa2);
3643 else
3644 return pw_aff_min(pa1, pa2);
3647 /* Return an expression for the minimum of "pwaff1" and "pwaff2".
3649 __isl_give isl_pw_aff *isl_pw_aff_min(__isl_take isl_pw_aff *pwaff1,
3650 __isl_take isl_pw_aff *pwaff2)
3652 return pw_aff_min_max(pwaff1, pwaff2, 0);
3655 /* Return an expression for the maximum of "pwaff1" and "pwaff2".
3657 __isl_give isl_pw_aff *isl_pw_aff_max(__isl_take isl_pw_aff *pwaff1,
3658 __isl_take isl_pw_aff *pwaff2)
3660 return pw_aff_min_max(pwaff1, pwaff2, 1);
3663 static __isl_give isl_pw_aff *pw_aff_list_reduce(
3664 __isl_take isl_pw_aff_list *list,
3665 __isl_give isl_pw_aff *(*fn)(__isl_take isl_pw_aff *pwaff1,
3666 __isl_take isl_pw_aff *pwaff2))
3668 int i;
3669 isl_ctx *ctx;
3670 isl_pw_aff *res;
3672 if (!list)
3673 return NULL;
3675 ctx = isl_pw_aff_list_get_ctx(list);
3676 if (list->n < 1)
3677 isl_die(ctx, isl_error_invalid,
3678 "list should contain at least one element", goto error);
3680 res = isl_pw_aff_copy(list->p[0]);
3681 for (i = 1; i < list->n; ++i)
3682 res = fn(res, isl_pw_aff_copy(list->p[i]));
3684 isl_pw_aff_list_free(list);
3685 return res;
3686 error:
3687 isl_pw_aff_list_free(list);
3688 return NULL;
3691 /* Return an isl_pw_aff that maps each element in the intersection of the
3692 * domains of the elements of list to the minimal corresponding affine
3693 * expression.
3695 __isl_give isl_pw_aff *isl_pw_aff_list_min(__isl_take isl_pw_aff_list *list)
3697 return pw_aff_list_reduce(list, &isl_pw_aff_min);
3700 /* Return an isl_pw_aff that maps each element in the intersection of the
3701 * domains of the elements of list to the maximal corresponding affine
3702 * expression.
3704 __isl_give isl_pw_aff *isl_pw_aff_list_max(__isl_take isl_pw_aff_list *list)
3706 return pw_aff_list_reduce(list, &isl_pw_aff_max);
3709 /* Mark the domains of "pwaff" as rational.
3711 __isl_give isl_pw_aff *isl_pw_aff_set_rational(__isl_take isl_pw_aff *pwaff)
3713 int i;
3715 pwaff = isl_pw_aff_cow(pwaff);
3716 if (!pwaff)
3717 return NULL;
3718 if (pwaff->n == 0)
3719 return pwaff;
3721 for (i = 0; i < pwaff->n; ++i) {
3722 pwaff->p[i].set = isl_set_set_rational(pwaff->p[i].set);
3723 if (!pwaff->p[i].set)
3724 return isl_pw_aff_free(pwaff);
3727 return pwaff;
3730 /* Mark the domains of the elements of "list" as rational.
3732 __isl_give isl_pw_aff_list *isl_pw_aff_list_set_rational(
3733 __isl_take isl_pw_aff_list *list)
3735 int i, n;
3737 if (!list)
3738 return NULL;
3739 if (list->n == 0)
3740 return list;
3742 n = list->n;
3743 for (i = 0; i < n; ++i) {
3744 isl_pw_aff *pa;
3746 pa = isl_pw_aff_list_get_pw_aff(list, i);
3747 pa = isl_pw_aff_set_rational(pa);
3748 list = isl_pw_aff_list_set_pw_aff(list, i, pa);
3751 return list;
3754 /* Do the parameters of "aff" match those of "space"?
3756 isl_bool isl_aff_matching_params(__isl_keep isl_aff *aff,
3757 __isl_keep isl_space *space)
3759 isl_space *aff_space;
3760 isl_bool match;
3762 if (!aff || !space)
3763 return isl_bool_error;
3765 aff_space = isl_aff_get_domain_space(aff);
3767 match = isl_space_has_equal_params(space, aff_space);
3769 isl_space_free(aff_space);
3770 return match;
3773 /* Check that the domain space of "aff" matches "space".
3775 isl_stat isl_aff_check_match_domain_space(__isl_keep isl_aff *aff,
3776 __isl_keep isl_space *space)
3778 isl_space *aff_space;
3779 isl_bool match;
3781 if (!aff || !space)
3782 return isl_stat_error;
3784 aff_space = isl_aff_get_domain_space(aff);
3786 match = isl_space_has_equal_params(space, aff_space);
3787 if (match < 0)
3788 goto error;
3789 if (!match)
3790 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3791 "parameters don't match", goto error);
3792 match = isl_space_tuple_is_equal(space, isl_dim_in,
3793 aff_space, isl_dim_set);
3794 if (match < 0)
3795 goto error;
3796 if (!match)
3797 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
3798 "domains don't match", goto error);
3799 isl_space_free(aff_space);
3800 return isl_stat_ok;
3801 error:
3802 isl_space_free(aff_space);
3803 return isl_stat_error;
3806 /* Return the shared (universe) domain of the elements of "ma".
3808 * Since an isl_multi_aff (and an isl_aff) is always total,
3809 * the domain is always the universe set in its domain space.
3810 * This is a helper function for use in the generic isl_multi_*_bind.
3812 static __isl_give isl_basic_set *isl_multi_aff_domain(
3813 __isl_take isl_multi_aff *ma)
3815 isl_space *space;
3817 space = isl_multi_aff_get_space(ma);
3818 isl_multi_aff_free(ma);
3820 return isl_basic_set_universe(isl_space_domain(space));
3823 #undef BASE
3824 #define BASE aff
3826 #include <isl_multi_no_explicit_domain.c>
3827 #include <isl_multi_templ.c>
3828 #include <isl_multi_add_constant_templ.c>
3829 #include <isl_multi_apply_set.c>
3830 #include <isl_multi_arith_templ.c>
3831 #include <isl_multi_bind_domain_templ.c>
3832 #include <isl_multi_cmp.c>
3833 #include <isl_multi_dim_id_templ.c>
3834 #include <isl_multi_dims.c>
3835 #include <isl_multi_floor.c>
3836 #include <isl_multi_from_base_templ.c>
3837 #include <isl_multi_identity_templ.c>
3838 #include <isl_multi_locals_templ.c>
3839 #include <isl_multi_move_dims_templ.c>
3840 #include <isl_multi_nan_templ.c>
3841 #include <isl_multi_product_templ.c>
3842 #include <isl_multi_splice_templ.c>
3843 #include <isl_multi_tuple_id_templ.c>
3844 #include <isl_multi_zero_templ.c>
3846 #undef DOMBASE
3847 #define DOMBASE set
3848 #include <isl_multi_gist.c>
3850 #undef DOMBASE
3851 #define DOMBASE basic_set
3852 #include <isl_multi_bind_templ.c>
3854 /* Construct an isl_multi_aff living in "space" that corresponds
3855 * to the affine transformation matrix "mat".
3857 __isl_give isl_multi_aff *isl_multi_aff_from_aff_mat(
3858 __isl_take isl_space *space, __isl_take isl_mat *mat)
3860 isl_ctx *ctx;
3861 isl_local_space *ls = NULL;
3862 isl_multi_aff *ma = NULL;
3863 isl_size n_row, n_col, n_out, total;
3864 int i;
3866 if (!space || !mat)
3867 goto error;
3869 ctx = isl_mat_get_ctx(mat);
3871 n_row = isl_mat_rows(mat);
3872 n_col = isl_mat_cols(mat);
3873 n_out = isl_space_dim(space, isl_dim_out);
3874 total = isl_space_dim(space, isl_dim_all);
3875 if (n_row < 0 || n_col < 0 || n_out < 0 || total < 0)
3876 goto error;
3877 if (n_row < 1)
3878 isl_die(ctx, isl_error_invalid,
3879 "insufficient number of rows", goto error);
3880 if (n_col < 1)
3881 isl_die(ctx, isl_error_invalid,
3882 "insufficient number of columns", goto error);
3883 if (1 + n_out != n_row || 2 + total != n_row + n_col)
3884 isl_die(ctx, isl_error_invalid,
3885 "dimension mismatch", goto error);
3887 ma = isl_multi_aff_zero(isl_space_copy(space));
3888 space = isl_space_domain(space);
3889 ls = isl_local_space_from_space(isl_space_copy(space));
3891 for (i = 0; i < n_row - 1; ++i) {
3892 isl_vec *v;
3893 isl_aff *aff;
3895 v = isl_vec_alloc(ctx, 1 + n_col);
3896 if (!v)
3897 goto error;
3898 isl_int_set(v->el[0], mat->row[0][0]);
3899 isl_seq_cpy(v->el + 1, mat->row[1 + i], n_col);
3900 v = isl_vec_normalize(v);
3901 aff = isl_aff_alloc_vec(isl_local_space_copy(ls), v);
3902 ma = isl_multi_aff_set_aff(ma, i, aff);
3905 isl_space_free(space);
3906 isl_local_space_free(ls);
3907 isl_mat_free(mat);
3908 return ma;
3909 error:
3910 isl_space_free(space);
3911 isl_local_space_free(ls);
3912 isl_mat_free(mat);
3913 isl_multi_aff_free(ma);
3914 return NULL;
3917 /* Return the constant terms of the affine expressions of "ma".
3919 __isl_give isl_multi_val *isl_multi_aff_get_constant_multi_val(
3920 __isl_keep isl_multi_aff *ma)
3922 int i;
3923 isl_size n;
3924 isl_space *space;
3925 isl_multi_val *mv;
3927 n = isl_multi_aff_size(ma);
3928 if (n < 0)
3929 return NULL;
3930 space = isl_space_range(isl_multi_aff_get_space(ma));
3931 space = isl_space_drop_all_params(space);
3932 mv = isl_multi_val_zero(space);
3934 for (i = 0; i < n; ++i) {
3935 isl_aff *aff;
3936 isl_val *val;
3938 aff = isl_multi_aff_get_at(ma, i);
3939 val = isl_aff_get_constant_val(aff);
3940 isl_aff_free(aff);
3941 mv = isl_multi_val_set_at(mv, i, val);
3944 return mv;
3947 /* Remove any internal structure of the domain of "ma".
3948 * If there is any such internal structure in the input,
3949 * then the name of the corresponding space is also removed.
3951 __isl_give isl_multi_aff *isl_multi_aff_flatten_domain(
3952 __isl_take isl_multi_aff *ma)
3954 isl_space *space;
3956 if (!ma)
3957 return NULL;
3959 if (!ma->space->nested[0])
3960 return ma;
3962 space = isl_multi_aff_get_space(ma);
3963 space = isl_space_flatten_domain(space);
3964 ma = isl_multi_aff_reset_space(ma, space);
3966 return ma;
3969 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
3970 * of the space to its domain.
3972 __isl_give isl_multi_aff *isl_multi_aff_domain_map(__isl_take isl_space *space)
3974 int i;
3975 isl_size n_in;
3976 isl_local_space *ls;
3977 isl_multi_aff *ma;
3979 if (!space)
3980 return NULL;
3981 if (!isl_space_is_map(space))
3982 isl_die(isl_space_get_ctx(space), isl_error_invalid,
3983 "not a map space", goto error);
3985 n_in = isl_space_dim(space, isl_dim_in);
3986 if (n_in < 0)
3987 goto error;
3988 space = isl_space_domain_map(space);
3990 ma = isl_multi_aff_alloc(isl_space_copy(space));
3991 if (n_in == 0) {
3992 isl_space_free(space);
3993 return ma;
3996 space = isl_space_domain(space);
3997 ls = isl_local_space_from_space(space);
3998 for (i = 0; i < n_in; ++i) {
3999 isl_aff *aff;
4001 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4002 isl_dim_set, i);
4003 ma = isl_multi_aff_set_aff(ma, i, aff);
4005 isl_local_space_free(ls);
4006 return ma;
4007 error:
4008 isl_space_free(space);
4009 return NULL;
4012 /* Given a map space, return an isl_multi_aff that maps a wrapped copy
4013 * of the space to its range.
4015 __isl_give isl_multi_aff *isl_multi_aff_range_map(__isl_take isl_space *space)
4017 int i;
4018 isl_size n_in, n_out;
4019 isl_local_space *ls;
4020 isl_multi_aff *ma;
4022 if (!space)
4023 return NULL;
4024 if (!isl_space_is_map(space))
4025 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4026 "not a map space", goto error);
4028 n_in = isl_space_dim(space, isl_dim_in);
4029 n_out = isl_space_dim(space, isl_dim_out);
4030 if (n_in < 0 || n_out < 0)
4031 goto error;
4032 space = isl_space_range_map(space);
4034 ma = isl_multi_aff_alloc(isl_space_copy(space));
4035 if (n_out == 0) {
4036 isl_space_free(space);
4037 return ma;
4040 space = isl_space_domain(space);
4041 ls = isl_local_space_from_space(space);
4042 for (i = 0; i < n_out; ++i) {
4043 isl_aff *aff;
4045 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4046 isl_dim_set, n_in + i);
4047 ma = isl_multi_aff_set_aff(ma, i, aff);
4049 isl_local_space_free(ls);
4050 return ma;
4051 error:
4052 isl_space_free(space);
4053 return NULL;
4056 /* Given a map space, return an isl_pw_multi_aff that maps a wrapped copy
4057 * of the space to its range.
4059 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_map(
4060 __isl_take isl_space *space)
4062 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_range_map(space));
4065 /* Given the space of a set and a range of set dimensions,
4066 * construct an isl_multi_aff that projects out those dimensions.
4068 __isl_give isl_multi_aff *isl_multi_aff_project_out_map(
4069 __isl_take isl_space *space, enum isl_dim_type type,
4070 unsigned first, unsigned n)
4072 int i;
4073 isl_size dim;
4074 isl_local_space *ls;
4075 isl_multi_aff *ma;
4077 if (!space)
4078 return NULL;
4079 if (!isl_space_is_set(space))
4080 isl_die(isl_space_get_ctx(space), isl_error_unsupported,
4081 "expecting set space", goto error);
4082 if (type != isl_dim_set)
4083 isl_die(isl_space_get_ctx(space), isl_error_invalid,
4084 "only set dimensions can be projected out", goto error);
4085 if (isl_space_check_range(space, type, first, n) < 0)
4086 goto error;
4088 dim = isl_space_dim(space, isl_dim_set);
4089 if (dim < 0)
4090 goto error;
4092 space = isl_space_from_domain(space);
4093 space = isl_space_add_dims(space, isl_dim_out, dim - n);
4095 if (dim == n)
4096 return isl_multi_aff_alloc(space);
4098 ma = isl_multi_aff_alloc(isl_space_copy(space));
4099 space = isl_space_domain(space);
4100 ls = isl_local_space_from_space(space);
4102 for (i = 0; i < first; ++i) {
4103 isl_aff *aff;
4105 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4106 isl_dim_set, i);
4107 ma = isl_multi_aff_set_aff(ma, i, aff);
4110 for (i = 0; i < dim - (first + n); ++i) {
4111 isl_aff *aff;
4113 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
4114 isl_dim_set, first + n + i);
4115 ma = isl_multi_aff_set_aff(ma, first + i, aff);
4118 isl_local_space_free(ls);
4119 return ma;
4120 error:
4121 isl_space_free(space);
4122 return NULL;
4125 /* Given the space of a set and a range of set dimensions,
4126 * construct an isl_pw_multi_aff that projects out those dimensions.
4128 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_project_out_map(
4129 __isl_take isl_space *space, enum isl_dim_type type,
4130 unsigned first, unsigned n)
4132 isl_multi_aff *ma;
4134 ma = isl_multi_aff_project_out_map(space, type, first, n);
4135 return isl_pw_multi_aff_from_multi_aff(ma);
4138 /* Create a piecewise multi-affine expression in the given space that maps each
4139 * input dimension to the corresponding output dimension.
4141 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_identity(
4142 __isl_take isl_space *space)
4144 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_identity(space));
4147 /* Exploit the equalities in "eq" to simplify the affine expressions.
4149 static __isl_give isl_multi_aff *isl_multi_aff_substitute_equalities(
4150 __isl_take isl_multi_aff *maff, __isl_take isl_basic_set *eq)
4152 int i;
4154 maff = isl_multi_aff_cow(maff);
4155 if (!maff || !eq)
4156 goto error;
4158 for (i = 0; i < maff->n; ++i) {
4159 maff->u.p[i] = isl_aff_substitute_equalities(maff->u.p[i],
4160 isl_basic_set_copy(eq));
4161 if (!maff->u.p[i])
4162 goto error;
4165 isl_basic_set_free(eq);
4166 return maff;
4167 error:
4168 isl_basic_set_free(eq);
4169 isl_multi_aff_free(maff);
4170 return NULL;
4173 __isl_give isl_multi_aff *isl_multi_aff_scale(__isl_take isl_multi_aff *maff,
4174 isl_int f)
4176 int i;
4178 maff = isl_multi_aff_cow(maff);
4179 if (!maff)
4180 return NULL;
4182 for (i = 0; i < maff->n; ++i) {
4183 maff->u.p[i] = isl_aff_scale(maff->u.p[i], f);
4184 if (!maff->u.p[i])
4185 return isl_multi_aff_free(maff);
4188 return maff;
4191 __isl_give isl_multi_aff *isl_multi_aff_add_on_domain(__isl_keep isl_set *dom,
4192 __isl_take isl_multi_aff *maff1, __isl_take isl_multi_aff *maff2)
4194 maff1 = isl_multi_aff_add(maff1, maff2);
4195 maff1 = isl_multi_aff_gist(maff1, isl_set_copy(dom));
4196 return maff1;
4199 isl_bool isl_multi_aff_is_empty(__isl_keep isl_multi_aff *maff)
4201 if (!maff)
4202 return isl_bool_error;
4204 return isl_bool_false;
4207 /* Return the set of domain elements where "ma1" is lexicographically
4208 * smaller than or equal to "ma2".
4210 __isl_give isl_set *isl_multi_aff_lex_le_set(__isl_take isl_multi_aff *ma1,
4211 __isl_take isl_multi_aff *ma2)
4213 return isl_multi_aff_lex_ge_set(ma2, ma1);
4216 /* Return the set of domain elements where "ma1" is lexicographically
4217 * smaller than "ma2".
4219 __isl_give isl_set *isl_multi_aff_lex_lt_set(__isl_take isl_multi_aff *ma1,
4220 __isl_take isl_multi_aff *ma2)
4222 return isl_multi_aff_lex_gt_set(ma2, ma1);
4225 /* Return the set of domain elements where "ma1" and "ma2"
4226 * satisfy "order".
4228 static __isl_give isl_set *isl_multi_aff_order_set(
4229 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2,
4230 __isl_give isl_map *order(__isl_take isl_space *set_space))
4232 isl_space *space;
4233 isl_map *map1, *map2;
4234 isl_map *map, *ge;
4236 map1 = isl_map_from_multi_aff_internal(ma1);
4237 map2 = isl_map_from_multi_aff_internal(ma2);
4238 map = isl_map_range_product(map1, map2);
4239 space = isl_space_range(isl_map_get_space(map));
4240 space = isl_space_domain(isl_space_unwrap(space));
4241 ge = order(space);
4242 map = isl_map_intersect_range(map, isl_map_wrap(ge));
4244 return isl_map_domain(map);
4247 /* Return the set of domain elements where "ma1" is lexicographically
4248 * greater than or equal to "ma2".
4250 __isl_give isl_set *isl_multi_aff_lex_ge_set(__isl_take isl_multi_aff *ma1,
4251 __isl_take isl_multi_aff *ma2)
4253 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_ge);
4256 /* Return the set of domain elements where "ma1" is lexicographically
4257 * greater than "ma2".
4259 __isl_give isl_set *isl_multi_aff_lex_gt_set(__isl_take isl_multi_aff *ma1,
4260 __isl_take isl_multi_aff *ma2)
4262 return isl_multi_aff_order_set(ma1, ma2, &isl_map_lex_gt);
4265 #define isl_multi_aff_zero_in_space isl_multi_aff_zero
4267 #undef PW
4268 #define PW isl_pw_multi_aff
4269 #undef BASE
4270 #define BASE multi_aff
4271 #undef EL_IS_ZERO
4272 #define EL_IS_ZERO is_empty
4273 #undef ZERO
4274 #define ZERO empty
4275 #undef IS_ZERO
4276 #define IS_ZERO is_empty
4277 #undef FIELD
4278 #define FIELD maff
4279 #undef DEFAULT_IS_ZERO
4280 #define DEFAULT_IS_ZERO 0
4282 #include <isl_pw_templ.c>
4283 #include <isl_pw_add_constant_multi_val_templ.c>
4284 #include <isl_pw_add_constant_val_templ.c>
4285 #include <isl_pw_bind_domain_templ.c>
4286 #include <isl_pw_move_dims_templ.c>
4287 #include <isl_pw_neg_templ.c>
4288 #include <isl_pw_pullback_templ.c>
4289 #include <isl_pw_union_opt.c>
4291 #undef BASE
4292 #define BASE pw_multi_aff
4294 #include <isl_union_multi.c>
4295 #include <isl_union_neg.c>
4297 /* Generic function for extracting a factor from a product "pma".
4298 * "check_space" checks that the space is that of the right kind of product.
4299 * "space_factor" extracts the factor from the space.
4300 * "multi_aff_factor" extracts the factor from the constituent functions.
4302 static __isl_give isl_pw_multi_aff *pw_multi_aff_factor(
4303 __isl_take isl_pw_multi_aff *pma,
4304 isl_stat (*check_space)(__isl_keep isl_pw_multi_aff *pma),
4305 __isl_give isl_space *(*space_factor)(__isl_take isl_space *space),
4306 __isl_give isl_multi_aff *(*multi_aff_factor)(
4307 __isl_take isl_multi_aff *ma))
4309 int i;
4310 isl_space *space;
4312 if (check_space(pma) < 0)
4313 return isl_pw_multi_aff_free(pma);
4315 space = isl_pw_multi_aff_take_space(pma);
4316 space = space_factor(space);
4318 for (i = 0; pma && i < pma->n; ++i) {
4319 isl_multi_aff *ma;
4321 ma = isl_pw_multi_aff_take_base_at(pma, i);
4322 ma = multi_aff_factor(ma);
4323 pma = isl_pw_multi_aff_restore_base_at(pma, i, ma);
4326 pma = isl_pw_multi_aff_restore_space(pma, space);
4328 return pma;
4331 /* Is the range of "pma" a wrapped relation?
4333 static isl_bool isl_pw_multi_aff_range_is_wrapping(
4334 __isl_keep isl_pw_multi_aff *pma)
4336 return isl_space_range_is_wrapping(isl_pw_multi_aff_peek_space(pma));
4339 /* Check that the range of "pma" is a product.
4341 static isl_stat pw_multi_aff_check_range_product(
4342 __isl_keep isl_pw_multi_aff *pma)
4344 isl_bool wraps;
4346 wraps = isl_pw_multi_aff_range_is_wrapping(pma);
4347 if (wraps < 0)
4348 return isl_stat_error;
4349 if (!wraps)
4350 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
4351 "range is not a product", return isl_stat_error);
4352 return isl_stat_ok;
4355 /* Given a function A -> [B -> C], extract the function A -> B.
4357 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_domain(
4358 __isl_take isl_pw_multi_aff *pma)
4360 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4361 &isl_space_range_factor_domain,
4362 &isl_multi_aff_range_factor_domain);
4365 /* Given a function A -> [B -> C], extract the function A -> C.
4367 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_factor_range(
4368 __isl_take isl_pw_multi_aff *pma)
4370 return pw_multi_aff_factor(pma, &pw_multi_aff_check_range_product,
4371 &isl_space_range_factor_range,
4372 &isl_multi_aff_range_factor_range);
4375 /* Given two piecewise multi affine expressions, return a piecewise
4376 * multi-affine expression defined on the union of the definition domains
4377 * of the inputs that is equal to the lexicographic maximum of the two
4378 * inputs on each cell. If only one of the two inputs is defined on
4379 * a given cell, then it is considered to be the maximum.
4381 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmax(
4382 __isl_take isl_pw_multi_aff *pma1,
4383 __isl_take isl_pw_multi_aff *pma2)
4385 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4386 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4387 &isl_multi_aff_lex_ge_set);
4390 /* Given two piecewise multi affine expressions, return a piecewise
4391 * multi-affine expression defined on the union of the definition domains
4392 * of the inputs that is equal to the lexicographic minimum of the two
4393 * inputs on each cell. If only one of the two inputs is defined on
4394 * a given cell, then it is considered to be the minimum.
4396 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_lexmin(
4397 __isl_take isl_pw_multi_aff *pma1,
4398 __isl_take isl_pw_multi_aff *pma2)
4400 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4401 return isl_pw_multi_aff_union_opt_cmp(pma1, pma2,
4402 &isl_multi_aff_lex_le_set);
4405 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_add(
4406 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4408 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4409 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4410 &isl_multi_aff_add);
4413 /* Subtract "pma2" from "pma1" and return the result.
4415 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_sub(
4416 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4418 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
4419 return isl_pw_multi_aff_on_shared_domain(pma1, pma2,
4420 &isl_multi_aff_sub);
4423 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_union_add(
4424 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4426 return isl_pw_multi_aff_union_add_(pma1, pma2);
4429 /* Compute the sum of "upa1" and "upa2" on the union of their domains,
4430 * with the actual sum on the shared domain and
4431 * the defined expression on the symmetric difference of the domains.
4433 __isl_give isl_union_pw_aff *isl_union_pw_aff_union_add(
4434 __isl_take isl_union_pw_aff *upa1, __isl_take isl_union_pw_aff *upa2)
4436 return isl_union_pw_aff_union_add_(upa1, upa2);
4439 /* Compute the sum of "upma1" and "upma2" on the union of their domains,
4440 * with the actual sum on the shared domain and
4441 * the defined expression on the symmetric difference of the domains.
4443 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_union_add(
4444 __isl_take isl_union_pw_multi_aff *upma1,
4445 __isl_take isl_union_pw_multi_aff *upma2)
4447 return isl_union_pw_multi_aff_union_add_(upma1, upma2);
4450 /* Given two piecewise multi-affine expressions A -> B and C -> D,
4451 * construct a piecewise multi-affine expression [A -> C] -> [B -> D].
4453 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_product(
4454 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
4456 int i, j, n;
4457 isl_space *space;
4458 isl_pw_multi_aff *res;
4460 if (isl_pw_multi_aff_align_params_bin(&pma1, &pma2) < 0)
4461 goto error;
4463 n = pma1->n * pma2->n;
4464 space = isl_space_product(isl_space_copy(pma1->dim),
4465 isl_space_copy(pma2->dim));
4466 res = isl_pw_multi_aff_alloc_size(space, n);
4468 for (i = 0; i < pma1->n; ++i) {
4469 for (j = 0; j < pma2->n; ++j) {
4470 isl_set *domain;
4471 isl_multi_aff *ma;
4473 domain = isl_set_product(isl_set_copy(pma1->p[i].set),
4474 isl_set_copy(pma2->p[j].set));
4475 ma = isl_multi_aff_product(
4476 isl_multi_aff_copy(pma1->p[i].maff),
4477 isl_multi_aff_copy(pma2->p[j].maff));
4478 res = isl_pw_multi_aff_add_piece(res, domain, ma);
4482 isl_pw_multi_aff_free(pma1);
4483 isl_pw_multi_aff_free(pma2);
4484 return res;
4485 error:
4486 isl_pw_multi_aff_free(pma1);
4487 isl_pw_multi_aff_free(pma2);
4488 return NULL;
4491 /* Subtract the initial "n" elements in "ma" with coefficients in "c" and
4492 * denominator "denom".
4493 * "denom" is allowed to be negative, in which case the actual denominator
4494 * is -denom and the expressions are added instead.
4496 static __isl_give isl_aff *subtract_initial(__isl_take isl_aff *aff,
4497 __isl_keep isl_multi_aff *ma, int n, isl_int *c, isl_int denom)
4499 int i, first;
4500 int sign;
4501 isl_int d;
4503 first = isl_seq_first_non_zero(c, n);
4504 if (first == -1)
4505 return aff;
4507 sign = isl_int_sgn(denom);
4508 isl_int_init(d);
4509 isl_int_abs(d, denom);
4510 for (i = first; i < n; ++i) {
4511 isl_aff *aff_i;
4513 if (isl_int_is_zero(c[i]))
4514 continue;
4515 aff_i = isl_multi_aff_get_aff(ma, i);
4516 aff_i = isl_aff_scale(aff_i, c[i]);
4517 aff_i = isl_aff_scale_down(aff_i, d);
4518 if (sign >= 0)
4519 aff = isl_aff_sub(aff, aff_i);
4520 else
4521 aff = isl_aff_add(aff, aff_i);
4523 isl_int_clear(d);
4525 return aff;
4528 /* Extract an affine expression that expresses the output dimension "pos"
4529 * of "bmap" in terms of the parameters and input dimensions from
4530 * equality "eq".
4531 * Note that this expression may involve integer divisions defined
4532 * in terms of parameters and input dimensions.
4533 * The equality may also involve references to earlier (but not later)
4534 * output dimensions. These are replaced by the corresponding elements
4535 * in "ma".
4537 * If the equality is of the form
4539 * f(i) + h(j) + a x + g(i) = 0,
4541 * with f(i) a linear combinations of the parameters and input dimensions,
4542 * g(i) a linear combination of integer divisions defined in terms of the same
4543 * and h(j) a linear combinations of earlier output dimensions,
4544 * then the affine expression is
4546 * (-f(i) - g(i))/a - h(j)/a
4548 * If the equality is of the form
4550 * f(i) + h(j) - a x + g(i) = 0,
4552 * then the affine expression is
4554 * (f(i) + g(i))/a - h(j)/(-a)
4557 * If "div" refers to an integer division (i.e., it is smaller than
4558 * the number of integer divisions), then the equality constraint
4559 * does involve an integer division (the one at position "div") that
4560 * is defined in terms of output dimensions. However, this integer
4561 * division can be eliminated by exploiting a pair of constraints
4562 * x >= l and x <= l + n, with n smaller than the coefficient of "div"
4563 * in the equality constraint. "ineq" refers to inequality x >= l, i.e.,
4564 * -l + x >= 0.
4565 * In particular, let
4567 * x = e(i) + m floor(...)
4569 * with e(i) the expression derived above and floor(...) the integer
4570 * division involving output dimensions.
4571 * From
4573 * l <= x <= l + n,
4575 * we have
4577 * 0 <= x - l <= n
4579 * This means
4581 * e(i) + m floor(...) - l = (e(i) + m floor(...) - l) mod m
4582 * = (e(i) - l) mod m
4584 * Therefore,
4586 * x - l = (e(i) - l) mod m
4588 * or
4590 * x = ((e(i) - l) mod m) + l
4592 * The variable "shift" below contains the expression -l, which may
4593 * also involve a linear combination of earlier output dimensions.
4595 static __isl_give isl_aff *extract_aff_from_equality(
4596 __isl_keep isl_basic_map *bmap, int pos, int eq, int div, int ineq,
4597 __isl_keep isl_multi_aff *ma)
4599 unsigned o_out;
4600 isl_size n_div, n_out;
4601 isl_ctx *ctx;
4602 isl_local_space *ls;
4603 isl_aff *aff, *shift;
4604 isl_val *mod;
4606 ctx = isl_basic_map_get_ctx(bmap);
4607 ls = isl_basic_map_get_local_space(bmap);
4608 ls = isl_local_space_domain(ls);
4609 aff = isl_aff_alloc(isl_local_space_copy(ls));
4610 if (!aff)
4611 goto error;
4612 o_out = isl_basic_map_offset(bmap, isl_dim_out);
4613 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4614 n_div = isl_basic_map_dim(bmap, isl_dim_div);
4615 if (n_out < 0 || n_div < 0)
4616 goto error;
4617 if (isl_int_is_neg(bmap->eq[eq][o_out + pos])) {
4618 isl_seq_cpy(aff->v->el + 1, bmap->eq[eq], o_out);
4619 isl_seq_cpy(aff->v->el + 1 + o_out,
4620 bmap->eq[eq] + o_out + n_out, n_div);
4621 } else {
4622 isl_seq_neg(aff->v->el + 1, bmap->eq[eq], o_out);
4623 isl_seq_neg(aff->v->el + 1 + o_out,
4624 bmap->eq[eq] + o_out + n_out, n_div);
4626 if (div < n_div)
4627 isl_int_set_si(aff->v->el[1 + o_out + div], 0);
4628 isl_int_abs(aff->v->el[0], bmap->eq[eq][o_out + pos]);
4629 aff = subtract_initial(aff, ma, pos, bmap->eq[eq] + o_out,
4630 bmap->eq[eq][o_out + pos]);
4631 if (div < n_div) {
4632 shift = isl_aff_alloc(isl_local_space_copy(ls));
4633 if (!shift)
4634 goto error;
4635 isl_seq_cpy(shift->v->el + 1, bmap->ineq[ineq], o_out);
4636 isl_seq_cpy(shift->v->el + 1 + o_out,
4637 bmap->ineq[ineq] + o_out + n_out, n_div);
4638 isl_int_set_si(shift->v->el[0], 1);
4639 shift = subtract_initial(shift, ma, pos,
4640 bmap->ineq[ineq] + o_out, ctx->negone);
4641 aff = isl_aff_add(aff, isl_aff_copy(shift));
4642 mod = isl_val_int_from_isl_int(ctx,
4643 bmap->eq[eq][o_out + n_out + div]);
4644 mod = isl_val_abs(mod);
4645 aff = isl_aff_mod_val(aff, mod);
4646 aff = isl_aff_sub(aff, shift);
4649 isl_local_space_free(ls);
4650 return aff;
4651 error:
4652 isl_local_space_free(ls);
4653 isl_aff_free(aff);
4654 return NULL;
4657 /* Given a basic map with output dimensions defined
4658 * in terms of the parameters input dimensions and earlier
4659 * output dimensions using an equality (and possibly a pair on inequalities),
4660 * extract an isl_aff that expresses output dimension "pos" in terms
4661 * of the parameters and input dimensions.
4662 * Note that this expression may involve integer divisions defined
4663 * in terms of parameters and input dimensions.
4664 * "ma" contains the expressions corresponding to earlier output dimensions.
4666 * This function shares some similarities with
4667 * isl_basic_map_has_defining_equality and isl_constraint_get_bound.
4669 static __isl_give isl_aff *extract_isl_aff_from_basic_map(
4670 __isl_keep isl_basic_map *bmap, int pos, __isl_keep isl_multi_aff *ma)
4672 int eq, div, ineq;
4673 isl_aff *aff;
4675 if (!bmap)
4676 return NULL;
4677 eq = isl_basic_map_output_defining_equality(bmap, pos, &div, &ineq);
4678 if (eq >= bmap->n_eq)
4679 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
4680 "unable to find suitable equality", return NULL);
4681 aff = extract_aff_from_equality(bmap, pos, eq, div, ineq, ma);
4683 aff = isl_aff_remove_unused_divs(aff);
4684 return aff;
4687 /* Given a basic map where each output dimension is defined
4688 * in terms of the parameters and input dimensions using an equality,
4689 * extract an isl_multi_aff that expresses the output dimensions in terms
4690 * of the parameters and input dimensions.
4692 static __isl_give isl_multi_aff *extract_isl_multi_aff_from_basic_map(
4693 __isl_take isl_basic_map *bmap)
4695 int i;
4696 isl_size n_out;
4697 isl_multi_aff *ma;
4699 if (!bmap)
4700 return NULL;
4702 ma = isl_multi_aff_alloc(isl_basic_map_get_space(bmap));
4703 n_out = isl_basic_map_dim(bmap, isl_dim_out);
4704 if (n_out < 0)
4705 ma = isl_multi_aff_free(ma);
4707 for (i = 0; i < n_out; ++i) {
4708 isl_aff *aff;
4710 aff = extract_isl_aff_from_basic_map(bmap, i, ma);
4711 ma = isl_multi_aff_set_aff(ma, i, aff);
4714 isl_basic_map_free(bmap);
4716 return ma;
4719 /* Given a basic set where each set dimension is defined
4720 * in terms of the parameters using an equality,
4721 * extract an isl_multi_aff that expresses the set dimensions in terms
4722 * of the parameters.
4724 __isl_give isl_multi_aff *isl_multi_aff_from_basic_set_equalities(
4725 __isl_take isl_basic_set *bset)
4727 return extract_isl_multi_aff_from_basic_map(bset);
4730 /* Create an isl_pw_multi_aff that is equivalent to
4731 * isl_map_intersect_domain(isl_map_from_basic_map(bmap), domain).
4732 * The given basic map is such that each output dimension is defined
4733 * in terms of the parameters and input dimensions using an equality.
4735 * Since some applications expect the result of isl_pw_multi_aff_from_map
4736 * to only contain integer affine expressions, we compute the floor
4737 * of the expression before returning.
4739 * Remove all constraints involving local variables without
4740 * an explicit representation (resulting in the removal of those
4741 * local variables) prior to the actual extraction to ensure
4742 * that the local spaces in which the resulting affine expressions
4743 * are created do not contain any unknown local variables.
4744 * Removing such constraints is safe because constraints involving
4745 * unknown local variables are not used to determine whether
4746 * a basic map is obviously single-valued.
4748 static __isl_give isl_pw_multi_aff *plain_pw_multi_aff_from_map(
4749 __isl_take isl_set *domain, __isl_take isl_basic_map *bmap)
4751 isl_multi_aff *ma;
4753 bmap = isl_basic_map_drop_constraints_involving_unknown_divs(bmap);
4754 ma = extract_isl_multi_aff_from_basic_map(bmap);
4755 ma = isl_multi_aff_floor(ma);
4756 return isl_pw_multi_aff_alloc(domain, ma);
4759 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4760 * This obviously only works if the input "map" is single-valued.
4761 * If so, we compute the lexicographic minimum of the image in the form
4762 * of an isl_pw_multi_aff. Since the image is unique, it is equal
4763 * to its lexicographic minimum.
4764 * If the input is not single-valued, we produce an error.
4766 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_base(
4767 __isl_take isl_map *map)
4769 int i;
4770 int sv;
4771 isl_pw_multi_aff *pma;
4773 sv = isl_map_is_single_valued(map);
4774 if (sv < 0)
4775 goto error;
4776 if (!sv)
4777 isl_die(isl_map_get_ctx(map), isl_error_invalid,
4778 "map is not single-valued", goto error);
4779 map = isl_map_make_disjoint(map);
4780 if (!map)
4781 return NULL;
4783 pma = isl_pw_multi_aff_empty(isl_map_get_space(map));
4785 for (i = 0; i < map->n; ++i) {
4786 isl_pw_multi_aff *pma_i;
4787 isl_basic_map *bmap;
4788 bmap = isl_basic_map_copy(map->p[i]);
4789 pma_i = isl_basic_map_lexmin_pw_multi_aff(bmap);
4790 pma = isl_pw_multi_aff_add_disjoint(pma, pma_i);
4793 isl_map_free(map);
4794 return pma;
4795 error:
4796 isl_map_free(map);
4797 return NULL;
4800 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
4801 * taking into account that the output dimension at position "d"
4802 * can be represented as
4804 * x = floor((e(...) + c1) / m)
4806 * given that constraint "i" is of the form
4808 * e(...) + c1 - m x >= 0
4811 * Let "map" be of the form
4813 * A -> B
4815 * We construct a mapping
4817 * A -> [A -> x = floor(...)]
4819 * apply that to the map, obtaining
4821 * [A -> x = floor(...)] -> B
4823 * and equate dimension "d" to x.
4824 * We then compute a isl_pw_multi_aff representation of the resulting map
4825 * and plug in the mapping above.
4827 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_div(
4828 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i)
4830 isl_ctx *ctx;
4831 isl_space *space = NULL;
4832 isl_local_space *ls;
4833 isl_multi_aff *ma;
4834 isl_aff *aff;
4835 isl_vec *v;
4836 isl_map *insert;
4837 int offset;
4838 isl_size n;
4839 isl_size n_in;
4840 isl_pw_multi_aff *pma;
4841 isl_bool is_set;
4843 is_set = isl_map_is_set(map);
4844 if (is_set < 0)
4845 goto error;
4847 offset = isl_basic_map_offset(hull, isl_dim_out);
4848 ctx = isl_map_get_ctx(map);
4849 space = isl_space_domain(isl_map_get_space(map));
4850 n_in = isl_space_dim(space, isl_dim_set);
4851 n = isl_space_dim(space, isl_dim_all);
4852 if (n_in < 0 || n < 0)
4853 goto error;
4855 v = isl_vec_alloc(ctx, 1 + 1 + n);
4856 if (v) {
4857 isl_int_neg(v->el[0], hull->ineq[i][offset + d]);
4858 isl_seq_cpy(v->el + 1, hull->ineq[i], 1 + n);
4860 isl_basic_map_free(hull);
4862 ls = isl_local_space_from_space(isl_space_copy(space));
4863 aff = isl_aff_alloc_vec(ls, v);
4864 aff = isl_aff_floor(aff);
4865 if (is_set) {
4866 isl_space_free(space);
4867 ma = isl_multi_aff_from_aff(aff);
4868 } else {
4869 ma = isl_multi_aff_identity(isl_space_map_from_set(space));
4870 ma = isl_multi_aff_range_product(ma,
4871 isl_multi_aff_from_aff(aff));
4874 insert = isl_map_from_multi_aff_internal(isl_multi_aff_copy(ma));
4875 map = isl_map_apply_domain(map, insert);
4876 map = isl_map_equate(map, isl_dim_in, n_in, isl_dim_out, d);
4877 pma = isl_pw_multi_aff_from_map(map);
4878 pma = isl_pw_multi_aff_pullback_multi_aff(pma, ma);
4880 return pma;
4881 error:
4882 isl_space_free(space);
4883 isl_map_free(map);
4884 isl_basic_map_free(hull);
4885 return NULL;
4888 /* Is constraint "c" of the form
4890 * e(...) + c1 - m x >= 0
4892 * or
4894 * -e(...) + c2 + m x >= 0
4896 * where m > 1 and e only depends on parameters and input dimemnsions?
4898 * "offset" is the offset of the output dimensions
4899 * "pos" is the position of output dimension x.
4901 static int is_potential_div_constraint(isl_int *c, int offset, int d, int total)
4903 if (isl_int_is_zero(c[offset + d]))
4904 return 0;
4905 if (isl_int_is_one(c[offset + d]))
4906 return 0;
4907 if (isl_int_is_negone(c[offset + d]))
4908 return 0;
4909 if (isl_seq_first_non_zero(c + offset, d) != -1)
4910 return 0;
4911 if (isl_seq_first_non_zero(c + offset + d + 1,
4912 total - (offset + d + 1)) != -1)
4913 return 0;
4914 return 1;
4917 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
4919 * As a special case, we first check if there is any pair of constraints,
4920 * shared by all the basic maps in "map" that force a given dimension
4921 * to be equal to the floor of some affine combination of the input dimensions.
4923 * In particular, if we can find two constraints
4925 * e(...) + c1 - m x >= 0 i.e., m x <= e(...) + c1
4927 * and
4929 * -e(...) + c2 + m x >= 0 i.e., m x >= e(...) - c2
4931 * where m > 1 and e only depends on parameters and input dimemnsions,
4932 * and such that
4934 * c1 + c2 < m i.e., -c2 >= c1 - (m - 1)
4936 * then we know that we can take
4938 * x = floor((e(...) + c1) / m)
4940 * without having to perform any computation.
4942 * Note that we know that
4944 * c1 + c2 >= 1
4946 * If c1 + c2 were 0, then we would have detected an equality during
4947 * simplification. If c1 + c2 were negative, then we would have detected
4948 * a contradiction.
4950 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_div(
4951 __isl_take isl_map *map)
4953 int d;
4954 isl_size dim;
4955 int i, j, n;
4956 int offset;
4957 isl_size total;
4958 isl_int sum;
4959 isl_basic_map *hull;
4961 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
4962 dim = isl_map_dim(map, isl_dim_out);
4963 total = isl_basic_map_dim(hull, isl_dim_all);
4964 if (dim < 0 || total < 0)
4965 goto error;
4967 isl_int_init(sum);
4968 offset = isl_basic_map_offset(hull, isl_dim_out);
4969 n = hull->n_ineq;
4970 for (d = 0; d < dim; ++d) {
4971 for (i = 0; i < n; ++i) {
4972 if (!is_potential_div_constraint(hull->ineq[i],
4973 offset, d, 1 + total))
4974 continue;
4975 for (j = i + 1; j < n; ++j) {
4976 if (!isl_seq_is_neg(hull->ineq[i] + 1,
4977 hull->ineq[j] + 1, total))
4978 continue;
4979 isl_int_add(sum, hull->ineq[i][0],
4980 hull->ineq[j][0]);
4981 if (isl_int_abs_lt(sum,
4982 hull->ineq[i][offset + d]))
4983 break;
4986 if (j >= n)
4987 continue;
4988 isl_int_clear(sum);
4989 if (isl_int_is_pos(hull->ineq[j][offset + d]))
4990 j = i;
4991 return pw_multi_aff_from_map_div(map, hull, d, j);
4994 isl_int_clear(sum);
4995 isl_basic_map_free(hull);
4996 return pw_multi_aff_from_map_base(map);
4997 error:
4998 isl_map_free(map);
4999 isl_basic_map_free(hull);
5000 return NULL;
5003 /* Given an affine expression
5005 * [A -> B] -> f(A,B)
5007 * construct an isl_multi_aff
5009 * [A -> B] -> B'
5011 * such that dimension "d" in B' is set to "aff" and the remaining
5012 * dimensions are set equal to the corresponding dimensions in B.
5013 * "n_in" is the dimension of the space A.
5014 * "n_out" is the dimension of the space B.
5016 * If "is_set" is set, then the affine expression is of the form
5018 * [B] -> f(B)
5020 * and we construct an isl_multi_aff
5022 * B -> B'
5024 static __isl_give isl_multi_aff *range_map(__isl_take isl_aff *aff, int d,
5025 unsigned n_in, unsigned n_out, int is_set)
5027 int i;
5028 isl_multi_aff *ma;
5029 isl_space *space, *space2;
5030 isl_local_space *ls;
5032 space = isl_aff_get_domain_space(aff);
5033 ls = isl_local_space_from_space(isl_space_copy(space));
5034 space2 = isl_space_copy(space);
5035 if (!is_set)
5036 space2 = isl_space_range(isl_space_unwrap(space2));
5037 space = isl_space_map_from_domain_and_range(space, space2);
5038 ma = isl_multi_aff_alloc(space);
5039 ma = isl_multi_aff_set_aff(ma, d, aff);
5041 for (i = 0; i < n_out; ++i) {
5042 if (i == d)
5043 continue;
5044 aff = isl_aff_var_on_domain(isl_local_space_copy(ls),
5045 isl_dim_set, n_in + i);
5046 ma = isl_multi_aff_set_aff(ma, i, aff);
5049 isl_local_space_free(ls);
5051 return ma;
5054 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map,
5055 * taking into account that the dimension at position "d" can be written as
5057 * x = m a + f(..) (1)
5059 * where m is equal to "gcd".
5060 * "i" is the index of the equality in "hull" that defines f(..).
5061 * In particular, the equality is of the form
5063 * f(..) - x + m g(existentials) = 0
5065 * or
5067 * -f(..) + x + m g(existentials) = 0
5069 * We basically plug (1) into "map", resulting in a map with "a"
5070 * in the range instead of "x". The corresponding isl_pw_multi_aff
5071 * defining "a" is then plugged back into (1) to obtain a definition for "x".
5073 * Specifically, given the input map
5075 * A -> B
5077 * We first wrap it into a set
5079 * [A -> B]
5081 * and define (1) on top of the corresponding space, resulting in "aff".
5082 * We use this to create an isl_multi_aff that maps the output position "d"
5083 * from "a" to "x", leaving all other (intput and output) dimensions unchanged.
5084 * We plug this into the wrapped map, unwrap the result and compute the
5085 * corresponding isl_pw_multi_aff.
5086 * The result is an expression
5088 * A -> T(A)
5090 * We adjust that to
5092 * A -> [A -> T(A)]
5094 * so that we can plug that into "aff", after extending the latter to
5095 * a mapping
5097 * [A -> B] -> B'
5100 * If "map" is actually a set, then there is no "A" space, meaning
5101 * that we do not need to perform any wrapping, and that the result
5102 * of the recursive call is of the form
5104 * [T]
5106 * which is plugged into a mapping of the form
5108 * B -> B'
5110 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_stride(
5111 __isl_take isl_map *map, __isl_take isl_basic_map *hull, int d, int i,
5112 isl_int gcd)
5114 isl_set *set;
5115 isl_space *space;
5116 isl_local_space *ls;
5117 isl_aff *aff;
5118 isl_multi_aff *ma;
5119 isl_pw_multi_aff *pma, *id;
5120 isl_size n_in;
5121 unsigned o_out;
5122 isl_size n_out;
5123 isl_bool is_set;
5125 is_set = isl_map_is_set(map);
5126 if (is_set < 0)
5127 goto error;
5129 n_in = isl_basic_map_dim(hull, isl_dim_in);
5130 n_out = isl_basic_map_dim(hull, isl_dim_out);
5131 if (n_in < 0 || n_out < 0)
5132 goto error;
5133 o_out = isl_basic_map_offset(hull, isl_dim_out);
5135 if (is_set)
5136 set = map;
5137 else
5138 set = isl_map_wrap(map);
5139 space = isl_space_map_from_set(isl_set_get_space(set));
5140 ma = isl_multi_aff_identity(space);
5141 ls = isl_local_space_from_space(isl_set_get_space(set));
5142 aff = isl_aff_alloc(ls);
5143 if (aff) {
5144 isl_int_set_si(aff->v->el[0], 1);
5145 if (isl_int_is_one(hull->eq[i][o_out + d]))
5146 isl_seq_neg(aff->v->el + 1, hull->eq[i],
5147 aff->v->size - 1);
5148 else
5149 isl_seq_cpy(aff->v->el + 1, hull->eq[i],
5150 aff->v->size - 1);
5151 isl_int_set(aff->v->el[1 + o_out + d], gcd);
5153 ma = isl_multi_aff_set_aff(ma, n_in + d, isl_aff_copy(aff));
5154 set = isl_set_preimage_multi_aff(set, ma);
5156 ma = range_map(aff, d, n_in, n_out, is_set);
5158 if (is_set)
5159 map = set;
5160 else
5161 map = isl_set_unwrap(set);
5162 pma = isl_pw_multi_aff_from_map(map);
5164 if (!is_set) {
5165 space = isl_pw_multi_aff_get_domain_space(pma);
5166 space = isl_space_map_from_set(space);
5167 id = isl_pw_multi_aff_identity(space);
5168 pma = isl_pw_multi_aff_range_product(id, pma);
5170 id = isl_pw_multi_aff_from_multi_aff(ma);
5171 pma = isl_pw_multi_aff_pullback_pw_multi_aff(id, pma);
5173 isl_basic_map_free(hull);
5174 return pma;
5175 error:
5176 isl_map_free(map);
5177 isl_basic_map_free(hull);
5178 return NULL;
5181 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5182 * "hull" contains the equalities valid for "map".
5184 * Check if any of the output dimensions is "strided".
5185 * That is, we check if it can be written as
5187 * x = m a + f(..)
5189 * with m greater than 1, a some combination of existentially quantified
5190 * variables and f an expression in the parameters and input dimensions.
5191 * If so, we remove the stride in pw_multi_aff_from_map_stride.
5193 * Otherwise, we continue with pw_multi_aff_from_map_check_div for a further
5194 * special case.
5196 static __isl_give isl_pw_multi_aff *pw_multi_aff_from_map_check_strides(
5197 __isl_take isl_map *map, __isl_take isl_basic_map *hull)
5199 int i, j;
5200 isl_size n_out;
5201 unsigned o_out;
5202 isl_size n_div;
5203 unsigned o_div;
5204 isl_int gcd;
5206 n_div = isl_basic_map_dim(hull, isl_dim_div);
5207 n_out = isl_basic_map_dim(hull, isl_dim_out);
5208 if (n_div < 0 || n_out < 0)
5209 goto error;
5211 if (n_div == 0) {
5212 isl_basic_map_free(hull);
5213 return pw_multi_aff_from_map_check_div(map);
5216 isl_int_init(gcd);
5218 o_div = isl_basic_map_offset(hull, isl_dim_div);
5219 o_out = isl_basic_map_offset(hull, isl_dim_out);
5221 for (i = 0; i < n_out; ++i) {
5222 for (j = 0; j < hull->n_eq; ++j) {
5223 isl_int *eq = hull->eq[j];
5224 isl_pw_multi_aff *res;
5226 if (!isl_int_is_one(eq[o_out + i]) &&
5227 !isl_int_is_negone(eq[o_out + i]))
5228 continue;
5229 if (isl_seq_first_non_zero(eq + o_out, i) != -1)
5230 continue;
5231 if (isl_seq_first_non_zero(eq + o_out + i + 1,
5232 n_out - (i + 1)) != -1)
5233 continue;
5234 isl_seq_gcd(eq + o_div, n_div, &gcd);
5235 if (isl_int_is_zero(gcd))
5236 continue;
5237 if (isl_int_is_one(gcd))
5238 continue;
5240 res = pw_multi_aff_from_map_stride(map, hull,
5241 i, j, gcd);
5242 isl_int_clear(gcd);
5243 return res;
5247 isl_int_clear(gcd);
5248 isl_basic_map_free(hull);
5249 return pw_multi_aff_from_map_check_div(map);
5250 error:
5251 isl_map_free(map);
5252 isl_basic_map_free(hull);
5253 return NULL;
5256 /* Try and create an isl_pw_multi_aff that is equivalent to the given isl_map.
5258 * As a special case, we first check if all output dimensions are uniquely
5259 * defined in terms of the parameters and input dimensions over the entire
5260 * domain. If so, we extract the desired isl_pw_multi_aff directly
5261 * from the affine hull of "map" and its domain.
5263 * Otherwise, continue with pw_multi_aff_from_map_check_strides for more
5264 * special cases.
5266 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_map(__isl_take isl_map *map)
5268 isl_bool sv;
5269 isl_size n;
5270 isl_basic_map *hull;
5272 n = isl_map_n_basic_map(map);
5273 if (n < 0)
5274 goto error;
5276 if (n == 1) {
5277 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5278 hull = isl_basic_map_plain_affine_hull(hull);
5279 sv = isl_basic_map_plain_is_single_valued(hull);
5280 if (sv >= 0 && sv)
5281 return plain_pw_multi_aff_from_map(isl_map_domain(map),
5282 hull);
5283 isl_basic_map_free(hull);
5285 map = isl_map_detect_equalities(map);
5286 hull = isl_map_unshifted_simple_hull(isl_map_copy(map));
5287 sv = isl_basic_map_plain_is_single_valued(hull);
5288 if (sv >= 0 && sv)
5289 return plain_pw_multi_aff_from_map(isl_map_domain(map), hull);
5290 if (sv >= 0)
5291 return pw_multi_aff_from_map_check_strides(map, hull);
5292 isl_basic_map_free(hull);
5293 error:
5294 isl_map_free(map);
5295 return NULL;
5298 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_set(__isl_take isl_set *set)
5300 return isl_pw_multi_aff_from_map(set);
5303 /* Convert "map" into an isl_pw_multi_aff (if possible) and
5304 * add it to *user.
5306 static isl_stat pw_multi_aff_from_map(__isl_take isl_map *map, void *user)
5308 isl_union_pw_multi_aff **upma = user;
5309 isl_pw_multi_aff *pma;
5311 pma = isl_pw_multi_aff_from_map(map);
5312 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5314 return *upma ? isl_stat_ok : isl_stat_error;
5317 /* Create an isl_union_pw_multi_aff with the given isl_aff on a universe
5318 * domain.
5320 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_aff(
5321 __isl_take isl_aff *aff)
5323 isl_multi_aff *ma;
5324 isl_pw_multi_aff *pma;
5326 ma = isl_multi_aff_from_aff(aff);
5327 pma = isl_pw_multi_aff_from_multi_aff(ma);
5328 return isl_union_pw_multi_aff_from_pw_multi_aff(pma);
5331 /* Try and create an isl_union_pw_multi_aff that is equivalent
5332 * to the given isl_union_map.
5333 * The isl_union_map is required to be single-valued in each space.
5334 * Otherwise, an error is produced.
5336 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_map(
5337 __isl_take isl_union_map *umap)
5339 isl_space *space;
5340 isl_union_pw_multi_aff *upma;
5342 space = isl_union_map_get_space(umap);
5343 upma = isl_union_pw_multi_aff_empty(space);
5344 if (isl_union_map_foreach_map(umap, &pw_multi_aff_from_map, &upma) < 0)
5345 upma = isl_union_pw_multi_aff_free(upma);
5346 isl_union_map_free(umap);
5348 return upma;
5351 /* Try and create an isl_union_pw_multi_aff that is equivalent
5352 * to the given isl_union_set.
5353 * The isl_union_set is required to be a singleton in each space.
5354 * Otherwise, an error is produced.
5356 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_set(
5357 __isl_take isl_union_set *uset)
5359 return isl_union_pw_multi_aff_from_union_map(uset);
5362 /* Return the piecewise affine expression "set ? 1 : 0".
5364 __isl_give isl_pw_aff *isl_set_indicator_function(__isl_take isl_set *set)
5366 isl_pw_aff *pa;
5367 isl_space *space = isl_set_get_space(set);
5368 isl_local_space *ls = isl_local_space_from_space(space);
5369 isl_aff *zero = isl_aff_zero_on_domain(isl_local_space_copy(ls));
5370 isl_aff *one = isl_aff_zero_on_domain(ls);
5372 one = isl_aff_add_constant_si(one, 1);
5373 pa = isl_pw_aff_alloc(isl_set_copy(set), one);
5374 set = isl_set_complement(set);
5375 pa = isl_pw_aff_add_disjoint(pa, isl_pw_aff_alloc(set, zero));
5377 return pa;
5380 /* Plug in "subs" for dimension "type", "pos" of "aff".
5382 * Let i be the dimension to replace and let "subs" be of the form
5384 * f/d
5386 * and "aff" of the form
5388 * (a i + g)/m
5390 * The result is
5392 * (a f + d g')/(m d)
5394 * where g' is the result of plugging in "subs" in each of the integer
5395 * divisions in g.
5397 __isl_give isl_aff *isl_aff_substitute(__isl_take isl_aff *aff,
5398 enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
5400 isl_ctx *ctx;
5401 isl_int v;
5402 isl_size n_div;
5404 aff = isl_aff_cow(aff);
5405 if (!aff || !subs)
5406 return isl_aff_free(aff);
5408 ctx = isl_aff_get_ctx(aff);
5409 if (!isl_space_is_equal(aff->ls->dim, subs->ls->dim))
5410 isl_die(ctx, isl_error_invalid,
5411 "spaces don't match", return isl_aff_free(aff));
5412 n_div = isl_aff_domain_dim(subs, isl_dim_div);
5413 if (n_div < 0)
5414 return isl_aff_free(aff);
5415 if (n_div != 0)
5416 isl_die(ctx, isl_error_unsupported,
5417 "cannot handle divs yet", return isl_aff_free(aff));
5419 aff->ls = isl_local_space_substitute(aff->ls, type, pos, subs);
5420 if (!aff->ls)
5421 return isl_aff_free(aff);
5423 aff->v = isl_vec_cow(aff->v);
5424 if (!aff->v)
5425 return isl_aff_free(aff);
5427 pos += isl_local_space_offset(aff->ls, type);
5429 isl_int_init(v);
5430 isl_seq_substitute(aff->v->el, pos, subs->v->el,
5431 aff->v->size, subs->v->size, v);
5432 isl_int_clear(v);
5434 return aff;
5437 /* Plug in "subs" for dimension "type", "pos" in each of the affine
5438 * expressions in "maff".
5440 __isl_give isl_multi_aff *isl_multi_aff_substitute(
5441 __isl_take isl_multi_aff *maff, enum isl_dim_type type, unsigned pos,
5442 __isl_keep isl_aff *subs)
5444 int i;
5446 maff = isl_multi_aff_cow(maff);
5447 if (!maff || !subs)
5448 return isl_multi_aff_free(maff);
5450 if (type == isl_dim_in)
5451 type = isl_dim_set;
5453 for (i = 0; i < maff->n; ++i) {
5454 maff->u.p[i] = isl_aff_substitute(maff->u.p[i],
5455 type, pos, subs);
5456 if (!maff->u.p[i])
5457 return isl_multi_aff_free(maff);
5460 return maff;
5463 /* Plug in "subs" for dimension "type", "pos" of "pma".
5465 * pma is of the form
5467 * A_i(v) -> M_i(v)
5469 * while subs is of the form
5471 * v' = B_j(v) -> S_j
5473 * Each pair i,j such that C_ij = A_i \cap B_i is non-empty
5474 * has a contribution in the result, in particular
5476 * C_ij(S_j) -> M_i(S_j)
5478 * Note that plugging in S_j in C_ij may also result in an empty set
5479 * and this contribution should simply be discarded.
5481 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_substitute(
5482 __isl_take isl_pw_multi_aff *pma, enum isl_dim_type type, unsigned pos,
5483 __isl_keep isl_pw_aff *subs)
5485 int i, j, n;
5486 isl_pw_multi_aff *res;
5488 if (!pma || !subs)
5489 return isl_pw_multi_aff_free(pma);
5491 n = pma->n * subs->n;
5492 res = isl_pw_multi_aff_alloc_size(isl_space_copy(pma->dim), n);
5494 for (i = 0; i < pma->n; ++i) {
5495 for (j = 0; j < subs->n; ++j) {
5496 isl_set *common;
5497 isl_multi_aff *res_ij;
5498 int empty;
5500 common = isl_set_intersect(
5501 isl_set_copy(pma->p[i].set),
5502 isl_set_copy(subs->p[j].set));
5503 common = isl_set_substitute(common,
5504 type, pos, subs->p[j].aff);
5505 empty = isl_set_plain_is_empty(common);
5506 if (empty < 0 || empty) {
5507 isl_set_free(common);
5508 if (empty < 0)
5509 goto error;
5510 continue;
5513 res_ij = isl_multi_aff_substitute(
5514 isl_multi_aff_copy(pma->p[i].maff),
5515 type, pos, subs->p[j].aff);
5517 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
5521 isl_pw_multi_aff_free(pma);
5522 return res;
5523 error:
5524 isl_pw_multi_aff_free(pma);
5525 isl_pw_multi_aff_free(res);
5526 return NULL;
5529 /* Compute the preimage of a range of dimensions in the affine expression "src"
5530 * under "ma" and put the result in "dst". The number of dimensions in "src"
5531 * that precede the range is given by "n_before". The number of dimensions
5532 * in the range is given by the number of output dimensions of "ma".
5533 * The number of dimensions that follow the range is given by "n_after".
5534 * If "has_denom" is set (to one),
5535 * then "src" and "dst" have an extra initial denominator.
5536 * "n_div_ma" is the number of existentials in "ma"
5537 * "n_div_bset" is the number of existentials in "src"
5538 * The resulting "dst" (which is assumed to have been allocated by
5539 * the caller) contains coefficients for both sets of existentials,
5540 * first those in "ma" and then those in "src".
5541 * f, c1, c2 and g are temporary objects that have been initialized
5542 * by the caller.
5544 * Let src represent the expression
5546 * (a(p) + f_u u + b v + f_w w + c(divs))/d
5548 * and let ma represent the expressions
5550 * v_i = (r_i(p) + s_i(y) + t_i(divs'))/m_i
5552 * We start out with the following expression for dst:
5554 * (a(p) + f_u u + 0 y + f_w w + 0 divs' + c(divs) + f \sum_i b_i v_i)/d
5556 * with the multiplication factor f initially equal to 1
5557 * and f \sum_i b_i v_i kept separately.
5558 * For each x_i that we substitute, we multiply the numerator
5559 * (and denominator) of dst by c_1 = m_i and add the numerator
5560 * of the x_i expression multiplied by c_2 = f b_i,
5561 * after removing the common factors of c_1 and c_2.
5562 * The multiplication factor f also needs to be multiplied by c_1
5563 * for the next x_j, j > i.
5565 isl_stat isl_seq_preimage(isl_int *dst, isl_int *src,
5566 __isl_keep isl_multi_aff *ma, int n_before, int n_after,
5567 int n_div_ma, int n_div_bmap,
5568 isl_int f, isl_int c1, isl_int c2, isl_int g, int has_denom)
5570 int i;
5571 isl_size n_param, n_in, n_out;
5572 int o_dst, o_src;
5574 n_param = isl_multi_aff_dim(ma, isl_dim_param);
5575 n_in = isl_multi_aff_dim(ma, isl_dim_in);
5576 n_out = isl_multi_aff_dim(ma, isl_dim_out);
5577 if (n_param < 0 || n_in < 0 || n_out < 0)
5578 return isl_stat_error;
5580 isl_seq_cpy(dst, src, has_denom + 1 + n_param + n_before);
5581 o_dst = o_src = has_denom + 1 + n_param + n_before;
5582 isl_seq_clr(dst + o_dst, n_in);
5583 o_dst += n_in;
5584 o_src += n_out;
5585 isl_seq_cpy(dst + o_dst, src + o_src, n_after);
5586 o_dst += n_after;
5587 o_src += n_after;
5588 isl_seq_clr(dst + o_dst, n_div_ma);
5589 o_dst += n_div_ma;
5590 isl_seq_cpy(dst + o_dst, src + o_src, n_div_bmap);
5592 isl_int_set_si(f, 1);
5594 for (i = 0; i < n_out; ++i) {
5595 int offset = has_denom + 1 + n_param + n_before + i;
5597 if (isl_int_is_zero(src[offset]))
5598 continue;
5599 isl_int_set(c1, ma->u.p[i]->v->el[0]);
5600 isl_int_mul(c2, f, src[offset]);
5601 isl_int_gcd(g, c1, c2);
5602 isl_int_divexact(c1, c1, g);
5603 isl_int_divexact(c2, c2, g);
5605 isl_int_mul(f, f, c1);
5606 o_dst = has_denom;
5607 o_src = 1;
5608 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5609 c2, ma->u.p[i]->v->el + o_src, 1 + n_param);
5610 o_dst += 1 + n_param;
5611 o_src += 1 + n_param;
5612 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_before);
5613 o_dst += n_before;
5614 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5615 c2, ma->u.p[i]->v->el + o_src, n_in);
5616 o_dst += n_in;
5617 o_src += n_in;
5618 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_after);
5619 o_dst += n_after;
5620 isl_seq_combine(dst + o_dst, c1, dst + o_dst,
5621 c2, ma->u.p[i]->v->el + o_src, n_div_ma);
5622 o_dst += n_div_ma;
5623 o_src += n_div_ma;
5624 isl_seq_scale(dst + o_dst, dst + o_dst, c1, n_div_bmap);
5625 if (has_denom)
5626 isl_int_mul(dst[0], dst[0], c1);
5629 return isl_stat_ok;
5632 /* Compute the pullback of "aff" by the function represented by "ma".
5633 * In other words, plug in "ma" in "aff". The result is an affine expression
5634 * defined over the domain space of "ma".
5636 * If "aff" is represented by
5638 * (a(p) + b x + c(divs))/d
5640 * and ma is represented by
5642 * x = D(p) + F(y) + G(divs')
5644 * then the result is
5646 * (a(p) + b D(p) + b F(y) + b G(divs') + c(divs))/d
5648 * The divs in the local space of the input are similarly adjusted
5649 * through a call to isl_local_space_preimage_multi_aff.
5651 __isl_give isl_aff *isl_aff_pullback_multi_aff(__isl_take isl_aff *aff,
5652 __isl_take isl_multi_aff *ma)
5654 isl_aff *res = NULL;
5655 isl_local_space *ls;
5656 isl_size n_div_aff, n_div_ma;
5657 isl_int f, c1, c2, g;
5659 ma = isl_multi_aff_align_divs(ma);
5660 if (!aff || !ma)
5661 goto error;
5663 n_div_aff = isl_aff_dim(aff, isl_dim_div);
5664 n_div_ma = ma->n ? isl_aff_dim(ma->u.p[0], isl_dim_div) : 0;
5665 if (n_div_aff < 0 || n_div_ma < 0)
5666 goto error;
5668 ls = isl_aff_get_domain_local_space(aff);
5669 ls = isl_local_space_preimage_multi_aff(ls, isl_multi_aff_copy(ma));
5670 res = isl_aff_alloc(ls);
5671 if (!res)
5672 goto error;
5674 isl_int_init(f);
5675 isl_int_init(c1);
5676 isl_int_init(c2);
5677 isl_int_init(g);
5679 if (isl_seq_preimage(res->v->el, aff->v->el, ma, 0, 0,
5680 n_div_ma, n_div_aff, f, c1, c2, g, 1) < 0)
5681 res = isl_aff_free(res);
5683 isl_int_clear(f);
5684 isl_int_clear(c1);
5685 isl_int_clear(c2);
5686 isl_int_clear(g);
5688 isl_aff_free(aff);
5689 isl_multi_aff_free(ma);
5690 res = isl_aff_normalize(res);
5691 return res;
5692 error:
5693 isl_aff_free(aff);
5694 isl_multi_aff_free(ma);
5695 isl_aff_free(res);
5696 return NULL;
5699 /* Compute the pullback of "aff1" by the function represented by "aff2".
5700 * In other words, plug in "aff2" in "aff1". The result is an affine expression
5701 * defined over the domain space of "aff1".
5703 * The domain of "aff1" should match the range of "aff2", which means
5704 * that it should be single-dimensional.
5706 __isl_give isl_aff *isl_aff_pullback_aff(__isl_take isl_aff *aff1,
5707 __isl_take isl_aff *aff2)
5709 isl_multi_aff *ma;
5711 ma = isl_multi_aff_from_aff(aff2);
5712 return isl_aff_pullback_multi_aff(aff1, ma);
5715 /* Compute the pullback of "ma1" by the function represented by "ma2".
5716 * In other words, plug in "ma2" in "ma1".
5718 __isl_give isl_multi_aff *isl_multi_aff_pullback_multi_aff(
5719 __isl_take isl_multi_aff *ma1, __isl_take isl_multi_aff *ma2)
5721 int i;
5722 isl_space *space = NULL;
5724 isl_multi_aff_align_params_bin(&ma1, &ma2);
5725 ma2 = isl_multi_aff_align_divs(ma2);
5726 ma1 = isl_multi_aff_cow(ma1);
5727 if (!ma1 || !ma2)
5728 goto error;
5730 space = isl_space_join(isl_multi_aff_get_space(ma2),
5731 isl_multi_aff_get_space(ma1));
5733 for (i = 0; i < ma1->n; ++i) {
5734 ma1->u.p[i] = isl_aff_pullback_multi_aff(ma1->u.p[i],
5735 isl_multi_aff_copy(ma2));
5736 if (!ma1->u.p[i])
5737 goto error;
5740 ma1 = isl_multi_aff_reset_space(ma1, space);
5741 isl_multi_aff_free(ma2);
5742 return ma1;
5743 error:
5744 isl_space_free(space);
5745 isl_multi_aff_free(ma2);
5746 isl_multi_aff_free(ma1);
5747 return NULL;
5750 /* Extend the local space of "dst" to include the divs
5751 * in the local space of "src".
5753 * If "src" does not have any divs or if the local spaces of "dst" and
5754 * "src" are the same, then no extension is required.
5756 __isl_give isl_aff *isl_aff_align_divs(__isl_take isl_aff *dst,
5757 __isl_keep isl_aff *src)
5759 isl_ctx *ctx;
5760 isl_size src_n_div, dst_n_div;
5761 int *exp1 = NULL;
5762 int *exp2 = NULL;
5763 isl_bool equal;
5764 isl_mat *div;
5766 if (!src || !dst)
5767 return isl_aff_free(dst);
5769 ctx = isl_aff_get_ctx(src);
5770 equal = isl_local_space_has_equal_space(src->ls, dst->ls);
5771 if (equal < 0)
5772 return isl_aff_free(dst);
5773 if (!equal)
5774 isl_die(ctx, isl_error_invalid,
5775 "spaces don't match", goto error);
5777 src_n_div = isl_aff_domain_dim(src, isl_dim_div);
5778 dst_n_div = isl_aff_domain_dim(dst, isl_dim_div);
5779 if (src_n_div == 0)
5780 return dst;
5781 equal = isl_local_space_is_equal(src->ls, dst->ls);
5782 if (equal < 0 || src_n_div < 0 || dst_n_div < 0)
5783 return isl_aff_free(dst);
5784 if (equal)
5785 return dst;
5787 exp1 = isl_alloc_array(ctx, int, src_n_div);
5788 exp2 = isl_alloc_array(ctx, int, dst_n_div);
5789 if (!exp1 || (dst_n_div && !exp2))
5790 goto error;
5792 div = isl_merge_divs(src->ls->div, dst->ls->div, exp1, exp2);
5793 dst = isl_aff_expand_divs(dst, div, exp2);
5794 free(exp1);
5795 free(exp2);
5797 return dst;
5798 error:
5799 free(exp1);
5800 free(exp2);
5801 return isl_aff_free(dst);
5804 /* Adjust the local spaces of the affine expressions in "maff"
5805 * such that they all have the save divs.
5807 __isl_give isl_multi_aff *isl_multi_aff_align_divs(
5808 __isl_take isl_multi_aff *maff)
5810 int i;
5812 if (!maff)
5813 return NULL;
5814 if (maff->n == 0)
5815 return maff;
5816 maff = isl_multi_aff_cow(maff);
5817 if (!maff)
5818 return NULL;
5820 for (i = 1; i < maff->n; ++i)
5821 maff->u.p[0] = isl_aff_align_divs(maff->u.p[0], maff->u.p[i]);
5822 for (i = 1; i < maff->n; ++i) {
5823 maff->u.p[i] = isl_aff_align_divs(maff->u.p[i], maff->u.p[0]);
5824 if (!maff->u.p[i])
5825 return isl_multi_aff_free(maff);
5828 return maff;
5831 __isl_give isl_aff *isl_aff_lift(__isl_take isl_aff *aff)
5833 aff = isl_aff_cow(aff);
5834 if (!aff)
5835 return NULL;
5837 aff->ls = isl_local_space_lift(aff->ls);
5838 if (!aff->ls)
5839 return isl_aff_free(aff);
5841 return aff;
5844 /* Lift "maff" to a space with extra dimensions such that the result
5845 * has no more existentially quantified variables.
5846 * If "ls" is not NULL, then *ls is assigned the local space that lies
5847 * at the basis of the lifting applied to "maff".
5849 __isl_give isl_multi_aff *isl_multi_aff_lift(__isl_take isl_multi_aff *maff,
5850 __isl_give isl_local_space **ls)
5852 int i;
5853 isl_space *space;
5854 isl_size n_div;
5856 if (ls)
5857 *ls = NULL;
5859 if (!maff)
5860 return NULL;
5862 if (maff->n == 0) {
5863 if (ls) {
5864 isl_space *space = isl_multi_aff_get_domain_space(maff);
5865 *ls = isl_local_space_from_space(space);
5866 if (!*ls)
5867 return isl_multi_aff_free(maff);
5869 return maff;
5872 maff = isl_multi_aff_cow(maff);
5873 maff = isl_multi_aff_align_divs(maff);
5874 if (!maff)
5875 return NULL;
5877 n_div = isl_aff_dim(maff->u.p[0], isl_dim_div);
5878 if (n_div < 0)
5879 return isl_multi_aff_free(maff);
5880 space = isl_multi_aff_get_space(maff);
5881 space = isl_space_lift(isl_space_domain(space), n_div);
5882 space = isl_space_extend_domain_with_range(space,
5883 isl_multi_aff_get_space(maff));
5884 if (!space)
5885 return isl_multi_aff_free(maff);
5886 isl_space_free(maff->space);
5887 maff->space = space;
5889 if (ls) {
5890 *ls = isl_aff_get_domain_local_space(maff->u.p[0]);
5891 if (!*ls)
5892 return isl_multi_aff_free(maff);
5895 for (i = 0; i < maff->n; ++i) {
5896 maff->u.p[i] = isl_aff_lift(maff->u.p[i]);
5897 if (!maff->u.p[i])
5898 goto error;
5901 return maff;
5902 error:
5903 if (ls)
5904 isl_local_space_free(*ls);
5905 return isl_multi_aff_free(maff);
5908 #undef TYPE
5909 #define TYPE isl_pw_multi_aff
5910 static
5911 #include "check_type_range_templ.c"
5913 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma".
5915 __isl_give isl_pw_aff *isl_pw_multi_aff_get_pw_aff(
5916 __isl_keep isl_pw_multi_aff *pma, int pos)
5918 int i;
5919 isl_size n_out;
5920 isl_space *space;
5921 isl_pw_aff *pa;
5923 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
5924 return NULL;
5926 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
5927 if (n_out < 0)
5928 return NULL;
5930 space = isl_pw_multi_aff_get_space(pma);
5931 space = isl_space_drop_dims(space, isl_dim_out,
5932 pos + 1, n_out - pos - 1);
5933 space = isl_space_drop_dims(space, isl_dim_out, 0, pos);
5935 pa = isl_pw_aff_alloc_size(space, pma->n);
5936 for (i = 0; i < pma->n; ++i) {
5937 isl_aff *aff;
5938 aff = isl_multi_aff_get_aff(pma->p[i].maff, pos);
5939 pa = isl_pw_aff_add_piece(pa, isl_set_copy(pma->p[i].set), aff);
5942 return pa;
5945 /* Return an isl_pw_multi_aff with the given "set" as domain and
5946 * an unnamed zero-dimensional range.
5948 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_domain(
5949 __isl_take isl_set *set)
5951 isl_multi_aff *ma;
5952 isl_space *space;
5954 space = isl_set_get_space(set);
5955 space = isl_space_from_domain(space);
5956 ma = isl_multi_aff_zero(space);
5957 return isl_pw_multi_aff_alloc(set, ma);
5960 /* Add an isl_pw_multi_aff with the given "set" as domain and
5961 * an unnamed zero-dimensional range to *user.
5963 static isl_stat add_pw_multi_aff_from_domain(__isl_take isl_set *set,
5964 void *user)
5966 isl_union_pw_multi_aff **upma = user;
5967 isl_pw_multi_aff *pma;
5969 pma = isl_pw_multi_aff_from_domain(set);
5970 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
5972 return isl_stat_ok;
5975 /* Return an isl_union_pw_multi_aff with the given "uset" as domain and
5976 * an unnamed zero-dimensional range.
5978 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_domain(
5979 __isl_take isl_union_set *uset)
5981 isl_space *space;
5982 isl_union_pw_multi_aff *upma;
5984 if (!uset)
5985 return NULL;
5987 space = isl_union_set_get_space(uset);
5988 upma = isl_union_pw_multi_aff_empty(space);
5990 if (isl_union_set_foreach_set(uset,
5991 &add_pw_multi_aff_from_domain, &upma) < 0)
5992 goto error;
5994 isl_union_set_free(uset);
5995 return upma;
5996 error:
5997 isl_union_set_free(uset);
5998 isl_union_pw_multi_aff_free(upma);
5999 return NULL;
6002 /* Local data for bin_entry and the callback "fn".
6004 struct isl_union_pw_multi_aff_bin_data {
6005 isl_union_pw_multi_aff *upma2;
6006 isl_union_pw_multi_aff *res;
6007 isl_pw_multi_aff *pma;
6008 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user);
6011 /* Given an isl_pw_multi_aff from upma1, store it in data->pma
6012 * and call data->fn for each isl_pw_multi_aff in data->upma2.
6014 static isl_stat bin_entry(__isl_take isl_pw_multi_aff *pma, void *user)
6016 struct isl_union_pw_multi_aff_bin_data *data = user;
6017 isl_stat r;
6019 data->pma = pma;
6020 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma2,
6021 data->fn, data);
6022 isl_pw_multi_aff_free(pma);
6024 return r;
6027 /* Call "fn" on each pair of isl_pw_multi_affs in "upma1" and "upma2".
6028 * The isl_pw_multi_aff from upma1 is stored in data->pma (where data is
6029 * passed as user field) and the isl_pw_multi_aff from upma2 is available
6030 * as *entry. The callback should adjust data->res if desired.
6032 static __isl_give isl_union_pw_multi_aff *bin_op(
6033 __isl_take isl_union_pw_multi_aff *upma1,
6034 __isl_take isl_union_pw_multi_aff *upma2,
6035 isl_stat (*fn)(__isl_take isl_pw_multi_aff *pma, void *user))
6037 isl_space *space;
6038 struct isl_union_pw_multi_aff_bin_data data = { NULL, NULL, NULL, fn };
6040 space = isl_union_pw_multi_aff_get_space(upma2);
6041 upma1 = isl_union_pw_multi_aff_align_params(upma1, space);
6042 space = isl_union_pw_multi_aff_get_space(upma1);
6043 upma2 = isl_union_pw_multi_aff_align_params(upma2, space);
6045 if (!upma1 || !upma2)
6046 goto error;
6048 data.upma2 = upma2;
6049 data.res = isl_union_pw_multi_aff_alloc_same_size(upma1);
6050 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma1,
6051 &bin_entry, &data) < 0)
6052 goto error;
6054 isl_union_pw_multi_aff_free(upma1);
6055 isl_union_pw_multi_aff_free(upma2);
6056 return data.res;
6057 error:
6058 isl_union_pw_multi_aff_free(upma1);
6059 isl_union_pw_multi_aff_free(upma2);
6060 isl_union_pw_multi_aff_free(data.res);
6061 return NULL;
6064 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6065 * construct an isl_pw_multi_aff (A * C) -> [B -> D].
6067 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_range_product(
6068 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6070 isl_space *space;
6072 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6073 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6074 isl_pw_multi_aff_get_space(pma2));
6075 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6076 &isl_multi_aff_range_product);
6079 /* Given two aligned isl_pw_multi_affs A -> B and C -> D,
6080 * construct an isl_pw_multi_aff (A * C) -> (B, D).
6082 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_flat_range_product(
6083 __isl_take isl_pw_multi_aff *pma1, __isl_take isl_pw_multi_aff *pma2)
6085 isl_space *space;
6087 isl_pw_multi_aff_align_params_bin(&pma1, &pma2);
6088 space = isl_space_range_product(isl_pw_multi_aff_get_space(pma1),
6089 isl_pw_multi_aff_get_space(pma2));
6090 space = isl_space_flatten_range(space);
6091 return isl_pw_multi_aff_on_shared_domain_in(pma1, pma2, space,
6092 &isl_multi_aff_flat_range_product);
6095 /* If data->pma and "pma2" have the same domain space, then compute
6096 * their flat range product and the result to data->res.
6098 static isl_stat flat_range_product_entry(__isl_take isl_pw_multi_aff *pma2,
6099 void *user)
6101 struct isl_union_pw_multi_aff_bin_data *data = user;
6103 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
6104 pma2->dim, isl_dim_in)) {
6105 isl_pw_multi_aff_free(pma2);
6106 return isl_stat_ok;
6109 pma2 = isl_pw_multi_aff_flat_range_product(
6110 isl_pw_multi_aff_copy(data->pma), pma2);
6112 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
6114 return isl_stat_ok;
6117 /* Given two isl_union_pw_multi_affs A -> B and C -> D,
6118 * construct an isl_union_pw_multi_aff (A * C) -> (B, D).
6120 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_flat_range_product(
6121 __isl_take isl_union_pw_multi_aff *upma1,
6122 __isl_take isl_union_pw_multi_aff *upma2)
6124 return bin_op(upma1, upma2, &flat_range_product_entry);
6127 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6128 * The parameters are assumed to have been aligned.
6130 * The implementation essentially performs an isl_pw_*_on_shared_domain,
6131 * except that it works on two different isl_pw_* types.
6133 static __isl_give isl_pw_multi_aff *pw_multi_aff_set_pw_aff(
6134 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6135 __isl_take isl_pw_aff *pa)
6137 int i, j, n;
6138 isl_pw_multi_aff *res = NULL;
6140 if (!pma || !pa)
6141 goto error;
6143 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_in,
6144 pa->dim, isl_dim_in))
6145 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6146 "domains don't match", goto error);
6147 if (isl_pw_multi_aff_check_range(pma, isl_dim_out, pos, 1) < 0)
6148 goto error;
6150 n = pma->n * pa->n;
6151 res = isl_pw_multi_aff_alloc_size(isl_pw_multi_aff_get_space(pma), n);
6153 for (i = 0; i < pma->n; ++i) {
6154 for (j = 0; j < pa->n; ++j) {
6155 isl_set *common;
6156 isl_multi_aff *res_ij;
6157 int empty;
6159 common = isl_set_intersect(isl_set_copy(pma->p[i].set),
6160 isl_set_copy(pa->p[j].set));
6161 empty = isl_set_plain_is_empty(common);
6162 if (empty < 0 || empty) {
6163 isl_set_free(common);
6164 if (empty < 0)
6165 goto error;
6166 continue;
6169 res_ij = isl_multi_aff_set_aff(
6170 isl_multi_aff_copy(pma->p[i].maff), pos,
6171 isl_aff_copy(pa->p[j].aff));
6172 res_ij = isl_multi_aff_gist(res_ij,
6173 isl_set_copy(common));
6175 res = isl_pw_multi_aff_add_piece(res, common, res_ij);
6179 isl_pw_multi_aff_free(pma);
6180 isl_pw_aff_free(pa);
6181 return res;
6182 error:
6183 isl_pw_multi_aff_free(pma);
6184 isl_pw_aff_free(pa);
6185 return isl_pw_multi_aff_free(res);
6188 /* Replace the affine expressions at position "pos" in "pma" by "pa".
6190 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_set_pw_aff(
6191 __isl_take isl_pw_multi_aff *pma, unsigned pos,
6192 __isl_take isl_pw_aff *pa)
6194 isl_bool equal_params;
6196 if (!pma || !pa)
6197 goto error;
6198 equal_params = isl_space_has_equal_params(pma->dim, pa->dim);
6199 if (equal_params < 0)
6200 goto error;
6201 if (equal_params)
6202 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6203 if (isl_pw_multi_aff_check_named_params(pma) < 0 ||
6204 isl_pw_aff_check_named_params(pa) < 0)
6205 goto error;
6206 pma = isl_pw_multi_aff_align_params(pma, isl_pw_aff_get_space(pa));
6207 pa = isl_pw_aff_align_params(pa, isl_pw_multi_aff_get_space(pma));
6208 return pw_multi_aff_set_pw_aff(pma, pos, pa);
6209 error:
6210 isl_pw_multi_aff_free(pma);
6211 isl_pw_aff_free(pa);
6212 return NULL;
6215 /* Do the parameters of "pa" match those of "space"?
6217 isl_bool isl_pw_aff_matching_params(__isl_keep isl_pw_aff *pa,
6218 __isl_keep isl_space *space)
6220 isl_space *pa_space;
6221 isl_bool match;
6223 if (!pa || !space)
6224 return isl_bool_error;
6226 pa_space = isl_pw_aff_get_space(pa);
6228 match = isl_space_has_equal_params(space, pa_space);
6230 isl_space_free(pa_space);
6231 return match;
6234 /* Check that the domain space of "pa" matches "space".
6236 isl_stat isl_pw_aff_check_match_domain_space(__isl_keep isl_pw_aff *pa,
6237 __isl_keep isl_space *space)
6239 isl_space *pa_space;
6240 isl_bool match;
6242 if (!pa || !space)
6243 return isl_stat_error;
6245 pa_space = isl_pw_aff_get_space(pa);
6247 match = isl_space_has_equal_params(space, pa_space);
6248 if (match < 0)
6249 goto error;
6250 if (!match)
6251 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6252 "parameters don't match", goto error);
6253 match = isl_space_tuple_is_equal(space, isl_dim_in,
6254 pa_space, isl_dim_in);
6255 if (match < 0)
6256 goto error;
6257 if (!match)
6258 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
6259 "domains don't match", goto error);
6260 isl_space_free(pa_space);
6261 return isl_stat_ok;
6262 error:
6263 isl_space_free(pa_space);
6264 return isl_stat_error;
6267 #undef BASE
6268 #define BASE pw_aff
6269 #undef DOMBASE
6270 #define DOMBASE set
6272 #include <isl_multi_explicit_domain.c>
6273 #include <isl_multi_pw_aff_explicit_domain.c>
6274 #include <isl_multi_templ.c>
6275 #include <isl_multi_add_constant_templ.c>
6276 #include <isl_multi_apply_set.c>
6277 #include <isl_multi_arith_templ.c>
6278 #include <isl_multi_bind_templ.c>
6279 #include <isl_multi_bind_domain_templ.c>
6280 #include <isl_multi_coalesce.c>
6281 #include <isl_multi_domain_templ.c>
6282 #include <isl_multi_dim_id_templ.c>
6283 #include <isl_multi_dims.c>
6284 #include <isl_multi_from_base_templ.c>
6285 #include <isl_multi_gist.c>
6286 #include <isl_multi_hash.c>
6287 #include <isl_multi_identity_templ.c>
6288 #include <isl_multi_align_set.c>
6289 #include <isl_multi_intersect.c>
6290 #include <isl_multi_min_max_templ.c>
6291 #include <isl_multi_move_dims_templ.c>
6292 #include <isl_multi_nan_templ.c>
6293 #include <isl_multi_param_templ.c>
6294 #include <isl_multi_product_templ.c>
6295 #include <isl_multi_splice_templ.c>
6296 #include <isl_multi_tuple_id_templ.c>
6297 #include <isl_multi_union_add_templ.c>
6298 #include <isl_multi_zero_templ.c>
6300 /* Are all elements of "mpa" piecewise constants?
6302 isl_bool isl_multi_pw_aff_is_cst(__isl_keep isl_multi_pw_aff *mpa)
6304 return isl_multi_pw_aff_every(mpa, &isl_pw_aff_is_cst);
6307 /* Does "mpa" have a non-trivial explicit domain?
6309 * The explicit domain, if present, is trivial if it represents
6310 * an (obviously) universe set.
6312 isl_bool isl_multi_pw_aff_has_non_trivial_domain(
6313 __isl_keep isl_multi_pw_aff *mpa)
6315 if (!mpa)
6316 return isl_bool_error;
6317 if (!isl_multi_pw_aff_has_explicit_domain(mpa))
6318 return isl_bool_false;
6319 return isl_bool_not(isl_set_plain_is_universe(mpa->u.dom));
6322 /* Scale the elements of "pma" by the corresponding elements of "mv".
6324 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_scale_multi_val(
6325 __isl_take isl_pw_multi_aff *pma, __isl_take isl_multi_val *mv)
6327 int i;
6328 isl_bool equal_params;
6330 pma = isl_pw_multi_aff_cow(pma);
6331 if (!pma || !mv)
6332 goto error;
6333 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6334 mv->space, isl_dim_set))
6335 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
6336 "spaces don't match", goto error);
6337 equal_params = isl_space_has_equal_params(pma->dim, mv->space);
6338 if (equal_params < 0)
6339 goto error;
6340 if (!equal_params) {
6341 pma = isl_pw_multi_aff_align_params(pma,
6342 isl_multi_val_get_space(mv));
6343 mv = isl_multi_val_align_params(mv,
6344 isl_pw_multi_aff_get_space(pma));
6345 if (!pma || !mv)
6346 goto error;
6349 for (i = 0; i < pma->n; ++i) {
6350 pma->p[i].maff = isl_multi_aff_scale_multi_val(pma->p[i].maff,
6351 isl_multi_val_copy(mv));
6352 if (!pma->p[i].maff)
6353 goto error;
6356 isl_multi_val_free(mv);
6357 return pma;
6358 error:
6359 isl_multi_val_free(mv);
6360 isl_pw_multi_aff_free(pma);
6361 return NULL;
6364 /* This function is called for each entry of an isl_union_pw_multi_aff.
6365 * If the space of the entry matches that of data->mv,
6366 * then apply isl_pw_multi_aff_scale_multi_val and return the result.
6367 * Otherwise, return an empty isl_pw_multi_aff.
6369 static __isl_give isl_pw_multi_aff *union_pw_multi_aff_scale_multi_val_entry(
6370 __isl_take isl_pw_multi_aff *pma, void *user)
6372 isl_multi_val *mv = user;
6374 if (!pma)
6375 return NULL;
6376 if (!isl_space_tuple_is_equal(pma->dim, isl_dim_out,
6377 mv->space, isl_dim_set)) {
6378 isl_space *space = isl_pw_multi_aff_get_space(pma);
6379 isl_pw_multi_aff_free(pma);
6380 return isl_pw_multi_aff_empty(space);
6383 return isl_pw_multi_aff_scale_multi_val(pma, isl_multi_val_copy(mv));
6386 /* Scale the elements of "upma" by the corresponding elements of "mv",
6387 * for those entries that match the space of "mv".
6389 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_scale_multi_val(
6390 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_multi_val *mv)
6392 struct isl_union_pw_multi_aff_transform_control control = {
6393 .fn = &union_pw_multi_aff_scale_multi_val_entry,
6394 .fn_user = mv,
6397 upma = isl_union_pw_multi_aff_align_params(upma,
6398 isl_multi_val_get_space(mv));
6399 mv = isl_multi_val_align_params(mv,
6400 isl_union_pw_multi_aff_get_space(upma));
6401 if (!upma || !mv)
6402 goto error;
6404 return isl_union_pw_multi_aff_transform(upma, &control);
6406 isl_multi_val_free(mv);
6407 return upma;
6408 error:
6409 isl_multi_val_free(mv);
6410 isl_union_pw_multi_aff_free(upma);
6411 return NULL;
6414 /* Construct and return a piecewise multi affine expression
6415 * in the given space with value zero in each of the output dimensions and
6416 * a universe domain.
6418 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_zero(__isl_take isl_space *space)
6420 return isl_pw_multi_aff_from_multi_aff(isl_multi_aff_zero(space));
6423 /* Construct and return a piecewise multi affine expression
6424 * that is equal to the given piecewise affine expression.
6426 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_pw_aff(
6427 __isl_take isl_pw_aff *pa)
6429 int i;
6430 isl_space *space;
6431 isl_pw_multi_aff *pma;
6433 if (!pa)
6434 return NULL;
6436 space = isl_pw_aff_get_space(pa);
6437 pma = isl_pw_multi_aff_alloc_size(space, pa->n);
6439 for (i = 0; i < pa->n; ++i) {
6440 isl_set *set;
6441 isl_multi_aff *ma;
6443 set = isl_set_copy(pa->p[i].set);
6444 ma = isl_multi_aff_from_aff(isl_aff_copy(pa->p[i].aff));
6445 pma = isl_pw_multi_aff_add_piece(pma, set, ma);
6448 isl_pw_aff_free(pa);
6449 return pma;
6452 /* Construct and return a piecewise multi affine expression
6453 * that is equal to the given multi piecewise affine expression
6454 * on the shared domain of the piecewise affine expressions,
6455 * in the special case of a 0D multi piecewise affine expression.
6457 * Create a piecewise multi affine expression with the explicit domain of
6458 * the 0D multi piecewise affine expression as domain.
6460 static __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff_0D(
6461 __isl_take isl_multi_pw_aff *mpa)
6463 isl_space *space;
6464 isl_set *dom;
6465 isl_multi_aff *ma;
6467 space = isl_multi_pw_aff_get_space(mpa);
6468 dom = isl_multi_pw_aff_get_explicit_domain(mpa);
6469 isl_multi_pw_aff_free(mpa);
6471 ma = isl_multi_aff_zero(space);
6472 return isl_pw_multi_aff_alloc(dom, ma);
6475 /* Construct and return a piecewise multi affine expression
6476 * that is equal to the given multi piecewise affine expression
6477 * on the shared domain of the piecewise affine expressions.
6479 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_from_multi_pw_aff(
6480 __isl_take isl_multi_pw_aff *mpa)
6482 int i;
6483 isl_space *space;
6484 isl_pw_aff *pa;
6485 isl_pw_multi_aff *pma;
6487 if (!mpa)
6488 return NULL;
6490 if (mpa->n == 0)
6491 return isl_pw_multi_aff_from_multi_pw_aff_0D(mpa);
6493 space = isl_multi_pw_aff_get_space(mpa);
6494 pa = isl_multi_pw_aff_get_pw_aff(mpa, 0);
6495 pma = isl_pw_multi_aff_from_pw_aff(pa);
6497 for (i = 1; i < mpa->n; ++i) {
6498 isl_pw_multi_aff *pma_i;
6500 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
6501 pma_i = isl_pw_multi_aff_from_pw_aff(pa);
6502 pma = isl_pw_multi_aff_range_product(pma, pma_i);
6505 pma = isl_pw_multi_aff_reset_space(pma, space);
6507 isl_multi_pw_aff_free(mpa);
6508 return pma;
6511 /* Construct and return a multi piecewise affine expression
6512 * that is equal to the given multi affine expression.
6514 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_multi_aff(
6515 __isl_take isl_multi_aff *ma)
6517 int i;
6518 isl_size n;
6519 isl_multi_pw_aff *mpa;
6521 n = isl_multi_aff_dim(ma, isl_dim_out);
6522 if (n < 0)
6523 ma = isl_multi_aff_free(ma);
6524 if (!ma)
6525 return NULL;
6527 mpa = isl_multi_pw_aff_alloc(isl_multi_aff_get_space(ma));
6529 for (i = 0; i < n; ++i) {
6530 isl_pw_aff *pa;
6532 pa = isl_pw_aff_from_aff(isl_multi_aff_get_aff(ma, i));
6533 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6536 isl_multi_aff_free(ma);
6537 return mpa;
6540 /* Construct and return a multi piecewise affine expression
6541 * that is equal to the given piecewise multi affine expression.
6543 * If the resulting multi piecewise affine expression has
6544 * an explicit domain, then assign it the domain of the input.
6545 * In other cases, the domain is stored in the individual elements.
6547 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_from_pw_multi_aff(
6548 __isl_take isl_pw_multi_aff *pma)
6550 int i;
6551 isl_size n;
6552 isl_space *space;
6553 isl_multi_pw_aff *mpa;
6555 n = isl_pw_multi_aff_dim(pma, isl_dim_out);
6556 if (n < 0)
6557 pma = isl_pw_multi_aff_free(pma);
6558 space = isl_pw_multi_aff_get_space(pma);
6559 mpa = isl_multi_pw_aff_alloc(space);
6561 for (i = 0; i < n; ++i) {
6562 isl_pw_aff *pa;
6564 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
6565 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
6567 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6568 isl_set *dom;
6570 dom = isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(pma));
6571 mpa = isl_multi_pw_aff_intersect_domain(mpa, dom);
6574 isl_pw_multi_aff_free(pma);
6575 return mpa;
6578 /* Do "pa1" and "pa2" represent the same function?
6580 * We first check if they are obviously equal.
6581 * If not, we convert them to maps and check if those are equal.
6583 * If "pa1" or "pa2" contain any NaNs, then they are considered
6584 * not to be the same. A NaN is not equal to anything, not even
6585 * to another NaN.
6587 isl_bool isl_pw_aff_is_equal(__isl_keep isl_pw_aff *pa1,
6588 __isl_keep isl_pw_aff *pa2)
6590 isl_bool equal;
6591 isl_bool has_nan;
6592 isl_map *map1, *map2;
6594 if (!pa1 || !pa2)
6595 return isl_bool_error;
6597 equal = isl_pw_aff_plain_is_equal(pa1, pa2);
6598 if (equal < 0 || equal)
6599 return equal;
6600 has_nan = either_involves_nan(pa1, pa2);
6601 if (has_nan < 0)
6602 return isl_bool_error;
6603 if (has_nan)
6604 return isl_bool_false;
6606 map1 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa1));
6607 map2 = isl_map_from_pw_aff_internal(isl_pw_aff_copy(pa2));
6608 equal = isl_map_is_equal(map1, map2);
6609 isl_map_free(map1);
6610 isl_map_free(map2);
6612 return equal;
6615 /* Do "mpa1" and "mpa2" represent the same function?
6617 * Note that we cannot convert the entire isl_multi_pw_aff
6618 * to a map because the domains of the piecewise affine expressions
6619 * may not be the same.
6621 isl_bool isl_multi_pw_aff_is_equal(__isl_keep isl_multi_pw_aff *mpa1,
6622 __isl_keep isl_multi_pw_aff *mpa2)
6624 int i;
6625 isl_bool equal, equal_params;
6627 if (!mpa1 || !mpa2)
6628 return isl_bool_error;
6630 equal_params = isl_space_has_equal_params(mpa1->space, mpa2->space);
6631 if (equal_params < 0)
6632 return isl_bool_error;
6633 if (!equal_params) {
6634 if (!isl_space_has_named_params(mpa1->space))
6635 return isl_bool_false;
6636 if (!isl_space_has_named_params(mpa2->space))
6637 return isl_bool_false;
6638 mpa1 = isl_multi_pw_aff_copy(mpa1);
6639 mpa2 = isl_multi_pw_aff_copy(mpa2);
6640 mpa1 = isl_multi_pw_aff_align_params(mpa1,
6641 isl_multi_pw_aff_get_space(mpa2));
6642 mpa2 = isl_multi_pw_aff_align_params(mpa2,
6643 isl_multi_pw_aff_get_space(mpa1));
6644 equal = isl_multi_pw_aff_is_equal(mpa1, mpa2);
6645 isl_multi_pw_aff_free(mpa1);
6646 isl_multi_pw_aff_free(mpa2);
6647 return equal;
6650 equal = isl_space_is_equal(mpa1->space, mpa2->space);
6651 if (equal < 0 || !equal)
6652 return equal;
6654 for (i = 0; i < mpa1->n; ++i) {
6655 equal = isl_pw_aff_is_equal(mpa1->u.p[i], mpa2->u.p[i]);
6656 if (equal < 0 || !equal)
6657 return equal;
6660 return isl_bool_true;
6663 /* Do "pma1" and "pma2" represent the same function?
6665 * First check if they are obviously equal.
6666 * If not, then convert them to maps and check if those are equal.
6668 * If "pa1" or "pa2" contain any NaNs, then they are considered
6669 * not to be the same. A NaN is not equal to anything, not even
6670 * to another NaN.
6672 isl_bool isl_pw_multi_aff_is_equal(__isl_keep isl_pw_multi_aff *pma1,
6673 __isl_keep isl_pw_multi_aff *pma2)
6675 isl_bool equal;
6676 isl_bool has_nan;
6677 isl_map *map1, *map2;
6679 if (!pma1 || !pma2)
6680 return isl_bool_error;
6682 equal = isl_pw_multi_aff_plain_is_equal(pma1, pma2);
6683 if (equal < 0 || equal)
6684 return equal;
6685 has_nan = isl_pw_multi_aff_involves_nan(pma1);
6686 if (has_nan >= 0 && !has_nan)
6687 has_nan = isl_pw_multi_aff_involves_nan(pma2);
6688 if (has_nan < 0 || has_nan)
6689 return isl_bool_not(has_nan);
6691 map1 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma1));
6692 map2 = isl_map_from_pw_multi_aff_internal(isl_pw_multi_aff_copy(pma2));
6693 equal = isl_map_is_equal(map1, map2);
6694 isl_map_free(map1);
6695 isl_map_free(map2);
6697 return equal;
6700 /* Compute the pullback of "mpa" by the function represented by "ma".
6701 * In other words, plug in "ma" in "mpa".
6703 * The parameters of "mpa" and "ma" are assumed to have been aligned.
6705 * If "mpa" has an explicit domain, then it is this domain
6706 * that needs to undergo a pullback, i.e., a preimage.
6708 static __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff_aligned(
6709 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6711 int i;
6712 isl_space *space = NULL;
6714 mpa = isl_multi_pw_aff_cow(mpa);
6715 if (!mpa || !ma)
6716 goto error;
6718 space = isl_space_join(isl_multi_aff_get_space(ma),
6719 isl_multi_pw_aff_get_space(mpa));
6720 if (!space)
6721 goto error;
6723 for (i = 0; i < mpa->n; ++i) {
6724 mpa->u.p[i] = isl_pw_aff_pullback_multi_aff(mpa->u.p[i],
6725 isl_multi_aff_copy(ma));
6726 if (!mpa->u.p[i])
6727 goto error;
6729 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6730 mpa->u.dom = isl_set_preimage_multi_aff(mpa->u.dom,
6731 isl_multi_aff_copy(ma));
6732 if (!mpa->u.dom)
6733 goto error;
6736 isl_multi_aff_free(ma);
6737 isl_space_free(mpa->space);
6738 mpa->space = space;
6739 return mpa;
6740 error:
6741 isl_space_free(space);
6742 isl_multi_pw_aff_free(mpa);
6743 isl_multi_aff_free(ma);
6744 return NULL;
6747 /* Compute the pullback of "mpa" by the function represented by "ma".
6748 * In other words, plug in "ma" in "mpa".
6750 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_aff(
6751 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_multi_aff *ma)
6753 isl_bool equal_params;
6755 if (!mpa || !ma)
6756 goto error;
6757 equal_params = isl_space_has_equal_params(mpa->space, ma->space);
6758 if (equal_params < 0)
6759 goto error;
6760 if (equal_params)
6761 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6762 mpa = isl_multi_pw_aff_align_params(mpa, isl_multi_aff_get_space(ma));
6763 ma = isl_multi_aff_align_params(ma, isl_multi_pw_aff_get_space(mpa));
6764 return isl_multi_pw_aff_pullback_multi_aff_aligned(mpa, ma);
6765 error:
6766 isl_multi_pw_aff_free(mpa);
6767 isl_multi_aff_free(ma);
6768 return NULL;
6771 /* Compute the pullback of "mpa" by the function represented by "pma".
6772 * In other words, plug in "pma" in "mpa".
6774 * The parameters of "mpa" and "mpa" are assumed to have been aligned.
6776 * If "mpa" has an explicit domain, then it is this domain
6777 * that needs to undergo a pullback, i.e., a preimage.
6779 static __isl_give isl_multi_pw_aff *
6780 isl_multi_pw_aff_pullback_pw_multi_aff_aligned(
6781 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6783 int i;
6784 isl_space *space = NULL;
6786 mpa = isl_multi_pw_aff_cow(mpa);
6787 if (!mpa || !pma)
6788 goto error;
6790 space = isl_space_join(isl_pw_multi_aff_get_space(pma),
6791 isl_multi_pw_aff_get_space(mpa));
6793 for (i = 0; i < mpa->n; ++i) {
6794 mpa->u.p[i] = isl_pw_aff_pullback_pw_multi_aff_aligned(
6795 mpa->u.p[i], isl_pw_multi_aff_copy(pma));
6796 if (!mpa->u.p[i])
6797 goto error;
6799 if (isl_multi_pw_aff_has_explicit_domain(mpa)) {
6800 mpa->u.dom = isl_set_preimage_pw_multi_aff(mpa->u.dom,
6801 isl_pw_multi_aff_copy(pma));
6802 if (!mpa->u.dom)
6803 goto error;
6806 isl_pw_multi_aff_free(pma);
6807 isl_space_free(mpa->space);
6808 mpa->space = space;
6809 return mpa;
6810 error:
6811 isl_space_free(space);
6812 isl_multi_pw_aff_free(mpa);
6813 isl_pw_multi_aff_free(pma);
6814 return NULL;
6817 /* Compute the pullback of "mpa" by the function represented by "pma".
6818 * In other words, plug in "pma" in "mpa".
6820 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_pw_multi_aff(
6821 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_multi_aff *pma)
6823 isl_bool equal_params;
6825 if (!mpa || !pma)
6826 goto error;
6827 equal_params = isl_space_has_equal_params(mpa->space, pma->dim);
6828 if (equal_params < 0)
6829 goto error;
6830 if (equal_params)
6831 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6832 mpa = isl_multi_pw_aff_align_params(mpa,
6833 isl_pw_multi_aff_get_space(pma));
6834 pma = isl_pw_multi_aff_align_params(pma,
6835 isl_multi_pw_aff_get_space(mpa));
6836 return isl_multi_pw_aff_pullback_pw_multi_aff_aligned(mpa, pma);
6837 error:
6838 isl_multi_pw_aff_free(mpa);
6839 isl_pw_multi_aff_free(pma);
6840 return NULL;
6843 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6844 * with the domain of "aff". The domain of the result is the same
6845 * as that of "mpa".
6846 * "mpa" and "aff" are assumed to have been aligned.
6848 * We first extract the parametric constant from "aff", defined
6849 * over the correct domain.
6850 * Then we add the appropriate combinations of the members of "mpa".
6851 * Finally, we add the integer divisions through recursive calls.
6853 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff_aligned(
6854 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6856 int i;
6857 isl_size n_in, n_div, n_mpa_in;
6858 isl_space *space;
6859 isl_val *v;
6860 isl_pw_aff *pa;
6861 isl_aff *tmp;
6863 n_in = isl_aff_dim(aff, isl_dim_in);
6864 n_div = isl_aff_dim(aff, isl_dim_div);
6865 n_mpa_in = isl_multi_pw_aff_dim(mpa, isl_dim_in);
6866 if (n_in < 0 || n_div < 0 || n_mpa_in < 0)
6867 goto error;
6869 space = isl_space_domain(isl_multi_pw_aff_get_space(mpa));
6870 tmp = isl_aff_copy(aff);
6871 tmp = isl_aff_drop_dims(tmp, isl_dim_div, 0, n_div);
6872 tmp = isl_aff_drop_dims(tmp, isl_dim_in, 0, n_in);
6873 tmp = isl_aff_add_dims(tmp, isl_dim_in, n_mpa_in);
6874 tmp = isl_aff_reset_domain_space(tmp, space);
6875 pa = isl_pw_aff_from_aff(tmp);
6877 for (i = 0; i < n_in; ++i) {
6878 isl_pw_aff *pa_i;
6880 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
6881 continue;
6882 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
6883 pa_i = isl_multi_pw_aff_get_pw_aff(mpa, i);
6884 pa_i = isl_pw_aff_scale_val(pa_i, v);
6885 pa = isl_pw_aff_add(pa, pa_i);
6888 for (i = 0; i < n_div; ++i) {
6889 isl_aff *div;
6890 isl_pw_aff *pa_i;
6892 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
6893 continue;
6894 div = isl_aff_get_div(aff, i);
6895 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6896 isl_multi_pw_aff_copy(mpa), div);
6897 pa_i = isl_pw_aff_floor(pa_i);
6898 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
6899 pa_i = isl_pw_aff_scale_val(pa_i, v);
6900 pa = isl_pw_aff_add(pa, pa_i);
6903 isl_multi_pw_aff_free(mpa);
6904 isl_aff_free(aff);
6906 return pa;
6907 error:
6908 isl_multi_pw_aff_free(mpa);
6909 isl_aff_free(aff);
6910 return NULL;
6913 /* Apply "aff" to "mpa". The range of "mpa" needs to be compatible
6914 * with the domain of "aff". The domain of the result is the same
6915 * as that of "mpa".
6917 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_aff(
6918 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_aff *aff)
6920 isl_bool equal_params;
6922 if (!aff || !mpa)
6923 goto error;
6924 equal_params = isl_space_has_equal_params(aff->ls->dim, mpa->space);
6925 if (equal_params < 0)
6926 goto error;
6927 if (equal_params)
6928 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6930 aff = isl_aff_align_params(aff, isl_multi_pw_aff_get_space(mpa));
6931 mpa = isl_multi_pw_aff_align_params(mpa, isl_aff_get_space(aff));
6933 return isl_multi_pw_aff_apply_aff_aligned(mpa, aff);
6934 error:
6935 isl_aff_free(aff);
6936 isl_multi_pw_aff_free(mpa);
6937 return NULL;
6940 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6941 * with the domain of "pa". The domain of the result is the same
6942 * as that of "mpa".
6943 * "mpa" and "pa" are assumed to have been aligned.
6945 * We consider each piece in turn. Note that the domains of the
6946 * pieces are assumed to be disjoint and they remain disjoint
6947 * after taking the preimage (over the same function).
6949 static __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff_aligned(
6950 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6952 isl_space *space;
6953 isl_pw_aff *res;
6954 int i;
6956 if (!mpa || !pa)
6957 goto error;
6959 space = isl_space_join(isl_multi_pw_aff_get_space(mpa),
6960 isl_pw_aff_get_space(pa));
6961 res = isl_pw_aff_empty(space);
6963 for (i = 0; i < pa->n; ++i) {
6964 isl_pw_aff *pa_i;
6965 isl_set *domain;
6967 pa_i = isl_multi_pw_aff_apply_aff_aligned(
6968 isl_multi_pw_aff_copy(mpa),
6969 isl_aff_copy(pa->p[i].aff));
6970 domain = isl_set_copy(pa->p[i].set);
6971 domain = isl_set_preimage_multi_pw_aff(domain,
6972 isl_multi_pw_aff_copy(mpa));
6973 pa_i = isl_pw_aff_intersect_domain(pa_i, domain);
6974 res = isl_pw_aff_add_disjoint(res, pa_i);
6977 isl_pw_aff_free(pa);
6978 isl_multi_pw_aff_free(mpa);
6979 return res;
6980 error:
6981 isl_pw_aff_free(pa);
6982 isl_multi_pw_aff_free(mpa);
6983 return NULL;
6986 /* Apply "pa" to "mpa". The range of "mpa" needs to be compatible
6987 * with the domain of "pa". The domain of the result is the same
6988 * as that of "mpa".
6990 __isl_give isl_pw_aff *isl_multi_pw_aff_apply_pw_aff(
6991 __isl_take isl_multi_pw_aff *mpa, __isl_take isl_pw_aff *pa)
6993 isl_bool equal_params;
6995 if (!pa || !mpa)
6996 goto error;
6997 equal_params = isl_space_has_equal_params(pa->dim, mpa->space);
6998 if (equal_params < 0)
6999 goto error;
7000 if (equal_params)
7001 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7003 pa = isl_pw_aff_align_params(pa, isl_multi_pw_aff_get_space(mpa));
7004 mpa = isl_multi_pw_aff_align_params(mpa, isl_pw_aff_get_space(pa));
7006 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7007 error:
7008 isl_pw_aff_free(pa);
7009 isl_multi_pw_aff_free(mpa);
7010 return NULL;
7013 /* Compute the pullback of "pa" by the function represented by "mpa".
7014 * In other words, plug in "mpa" in "pa".
7015 * "pa" and "mpa" are assumed to have been aligned.
7017 * The pullback is computed by applying "pa" to "mpa".
7019 static __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff_aligned(
7020 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7022 return isl_multi_pw_aff_apply_pw_aff_aligned(mpa, pa);
7025 /* Compute the pullback of "pa" by the function represented by "mpa".
7026 * In other words, plug in "mpa" in "pa".
7028 * The pullback is computed by applying "pa" to "mpa".
7030 __isl_give isl_pw_aff *isl_pw_aff_pullback_multi_pw_aff(
7031 __isl_take isl_pw_aff *pa, __isl_take isl_multi_pw_aff *mpa)
7033 return isl_multi_pw_aff_apply_pw_aff(mpa, pa);
7036 /* Compute the pullback of "mpa1" by the function represented by "mpa2".
7037 * In other words, plug in "mpa2" in "mpa1".
7039 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7041 * We pullback each member of "mpa1" in turn.
7043 * If "mpa1" has an explicit domain, then it is this domain
7044 * that needs to undergo a pullback instead, i.e., a preimage.
7046 __isl_give isl_multi_pw_aff *isl_multi_pw_aff_pullback_multi_pw_aff(
7047 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2)
7049 int i;
7050 isl_space *space = NULL;
7052 isl_multi_pw_aff_align_params_bin(&mpa1, &mpa2);
7053 mpa1 = isl_multi_pw_aff_cow(mpa1);
7054 if (!mpa1 || !mpa2)
7055 goto error;
7057 space = isl_space_join(isl_multi_pw_aff_get_space(mpa2),
7058 isl_multi_pw_aff_get_space(mpa1));
7060 for (i = 0; i < mpa1->n; ++i) {
7061 mpa1->u.p[i] = isl_pw_aff_pullback_multi_pw_aff_aligned(
7062 mpa1->u.p[i], isl_multi_pw_aff_copy(mpa2));
7063 if (!mpa1->u.p[i])
7064 goto error;
7067 if (isl_multi_pw_aff_has_explicit_domain(mpa1)) {
7068 mpa1->u.dom = isl_set_preimage_multi_pw_aff(mpa1->u.dom,
7069 isl_multi_pw_aff_copy(mpa2));
7070 if (!mpa1->u.dom)
7071 goto error;
7073 mpa1 = isl_multi_pw_aff_reset_space(mpa1, space);
7075 isl_multi_pw_aff_free(mpa2);
7076 return mpa1;
7077 error:
7078 isl_space_free(space);
7079 isl_multi_pw_aff_free(mpa1);
7080 isl_multi_pw_aff_free(mpa2);
7081 return NULL;
7084 /* Align the parameters of "mpa1" and "mpa2", check that the ranges
7085 * of "mpa1" and "mpa2" live in the same space, construct map space
7086 * between the domain spaces of "mpa1" and "mpa2" and call "order"
7087 * with this map space as extract argument.
7089 static __isl_give isl_map *isl_multi_pw_aff_order_map(
7090 __isl_take isl_multi_pw_aff *mpa1, __isl_take isl_multi_pw_aff *mpa2,
7091 __isl_give isl_map *(*order)(__isl_keep isl_multi_pw_aff *mpa1,
7092 __isl_keep isl_multi_pw_aff *mpa2, __isl_take isl_space *space))
7094 int match;
7095 isl_space *space1, *space2;
7096 isl_map *res;
7098 mpa1 = isl_multi_pw_aff_align_params(mpa1,
7099 isl_multi_pw_aff_get_space(mpa2));
7100 mpa2 = isl_multi_pw_aff_align_params(mpa2,
7101 isl_multi_pw_aff_get_space(mpa1));
7102 if (!mpa1 || !mpa2)
7103 goto error;
7104 match = isl_space_tuple_is_equal(mpa1->space, isl_dim_out,
7105 mpa2->space, isl_dim_out);
7106 if (match < 0)
7107 goto error;
7108 if (!match)
7109 isl_die(isl_multi_pw_aff_get_ctx(mpa1), isl_error_invalid,
7110 "range spaces don't match", goto error);
7111 space1 = isl_space_domain(isl_multi_pw_aff_get_space(mpa1));
7112 space2 = isl_space_domain(isl_multi_pw_aff_get_space(mpa2));
7113 space1 = isl_space_map_from_domain_and_range(space1, space2);
7115 res = order(mpa1, mpa2, space1);
7116 isl_multi_pw_aff_free(mpa1);
7117 isl_multi_pw_aff_free(mpa2);
7118 return res;
7119 error:
7120 isl_multi_pw_aff_free(mpa1);
7121 isl_multi_pw_aff_free(mpa2);
7122 return NULL;
7125 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7126 * where the function values are equal. "space" is the space of the result.
7127 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7129 * "mpa1" and "mpa2" are equal when each of the pairs of elements
7130 * in the sequences are equal.
7132 static __isl_give isl_map *isl_multi_pw_aff_eq_map_on_space(
7133 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7134 __isl_take isl_space *space)
7136 int i;
7137 isl_size n;
7138 isl_map *res;
7140 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7141 if (n < 0)
7142 space = isl_space_free(space);
7143 res = isl_map_universe(space);
7145 for (i = 0; i < n; ++i) {
7146 isl_pw_aff *pa1, *pa2;
7147 isl_map *map;
7149 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7150 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7151 map = isl_pw_aff_eq_map(pa1, pa2);
7152 res = isl_map_intersect(res, map);
7155 return res;
7158 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7159 * where the function values are equal.
7161 __isl_give isl_map *isl_multi_pw_aff_eq_map(__isl_take isl_multi_pw_aff *mpa1,
7162 __isl_take isl_multi_pw_aff *mpa2)
7164 return isl_multi_pw_aff_order_map(mpa1, mpa2,
7165 &isl_multi_pw_aff_eq_map_on_space);
7168 /* Intersect "map" with the result of applying "order"
7169 * on two copies of "mpa".
7171 static __isl_give isl_map *isl_map_order_at_multi_pw_aff(
7172 __isl_take isl_map *map, __isl_take isl_multi_pw_aff *mpa,
7173 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
7174 __isl_take isl_multi_pw_aff *mpa2))
7176 return isl_map_intersect(map, order(mpa, isl_multi_pw_aff_copy(mpa)));
7179 /* Return the subset of "map" where the domain and the range
7180 * have equal "mpa" values.
7182 __isl_give isl_map *isl_map_eq_at_multi_pw_aff(__isl_take isl_map *map,
7183 __isl_take isl_multi_pw_aff *mpa)
7185 return isl_map_order_at_multi_pw_aff(map, mpa,
7186 &isl_multi_pw_aff_eq_map);
7189 /* Return a map containing pairs of elements in the domains of "mpa1" and "mpa2"
7190 * where the function values of "mpa1" lexicographically satisfies "base"
7191 * compared to that of "mpa2". "space" is the space of the result.
7192 * The parameters of "mpa1" and "mpa2" are assumed to have been aligned.
7194 * "mpa1" lexicographically satisfies "base" compared to "mpa2"
7195 * if its i-th element satisfies "base" when compared to
7196 * the i-th element of "mpa2" while all previous elements are
7197 * pairwise equal.
7199 static __isl_give isl_map *isl_multi_pw_aff_lex_map_on_space(
7200 __isl_keep isl_multi_pw_aff *mpa1, __isl_keep isl_multi_pw_aff *mpa2,
7201 __isl_give isl_map *(*base)(__isl_take isl_pw_aff *pa1,
7202 __isl_take isl_pw_aff *pa2),
7203 __isl_take isl_space *space)
7205 int i;
7206 isl_size n;
7207 isl_map *res, *rest;
7209 n = isl_multi_pw_aff_dim(mpa1, isl_dim_out);
7210 if (n < 0)
7211 space = isl_space_free(space);
7212 res = isl_map_empty(isl_space_copy(space));
7213 rest = isl_map_universe(space);
7215 for (i = 0; i < n; ++i) {
7216 isl_pw_aff *pa1, *pa2;
7217 isl_map *map;
7219 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7220 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7221 map = base(pa1, pa2);
7222 map = isl_map_intersect(map, isl_map_copy(rest));
7223 res = isl_map_union(res, map);
7225 if (i == n - 1)
7226 continue;
7228 pa1 = isl_multi_pw_aff_get_pw_aff(mpa1, i);
7229 pa2 = isl_multi_pw_aff_get_pw_aff(mpa2, i);
7230 map = isl_pw_aff_eq_map(pa1, pa2);
7231 rest = isl_map_intersect(rest, map);
7234 isl_map_free(rest);
7235 return res;
7238 #undef ORDER
7239 #define ORDER le
7240 #include "isl_aff_lex_templ.c"
7242 #undef ORDER
7243 #define ORDER lt
7244 #include "isl_aff_lex_templ.c"
7246 #undef ORDER
7247 #define ORDER ge
7248 #include "isl_aff_lex_templ.c"
7250 #undef ORDER
7251 #define ORDER gt
7252 #include "isl_aff_lex_templ.c"
7254 /* Compare two isl_affs.
7256 * Return -1 if "aff1" is "smaller" than "aff2", 1 if "aff1" is "greater"
7257 * than "aff2" and 0 if they are equal.
7259 * The order is fairly arbitrary. We do consider expressions that only involve
7260 * earlier dimensions as "smaller".
7262 int isl_aff_plain_cmp(__isl_keep isl_aff *aff1, __isl_keep isl_aff *aff2)
7264 int cmp;
7265 int last1, last2;
7267 if (aff1 == aff2)
7268 return 0;
7270 if (!aff1)
7271 return -1;
7272 if (!aff2)
7273 return 1;
7275 cmp = isl_local_space_cmp(aff1->ls, aff2->ls);
7276 if (cmp != 0)
7277 return cmp;
7279 last1 = isl_seq_last_non_zero(aff1->v->el + 1, aff1->v->size - 1);
7280 last2 = isl_seq_last_non_zero(aff2->v->el + 1, aff1->v->size - 1);
7281 if (last1 != last2)
7282 return last1 - last2;
7284 return isl_seq_cmp(aff1->v->el, aff2->v->el, aff1->v->size);
7287 /* Compare two isl_pw_affs.
7289 * Return -1 if "pa1" is "smaller" than "pa2", 1 if "pa1" is "greater"
7290 * than "pa2" and 0 if they are equal.
7292 * The order is fairly arbitrary. We do consider expressions that only involve
7293 * earlier dimensions as "smaller".
7295 int isl_pw_aff_plain_cmp(__isl_keep isl_pw_aff *pa1,
7296 __isl_keep isl_pw_aff *pa2)
7298 int i;
7299 int cmp;
7301 if (pa1 == pa2)
7302 return 0;
7304 if (!pa1)
7305 return -1;
7306 if (!pa2)
7307 return 1;
7309 cmp = isl_space_cmp(pa1->dim, pa2->dim);
7310 if (cmp != 0)
7311 return cmp;
7313 if (pa1->n != pa2->n)
7314 return pa1->n - pa2->n;
7316 for (i = 0; i < pa1->n; ++i) {
7317 cmp = isl_set_plain_cmp(pa1->p[i].set, pa2->p[i].set);
7318 if (cmp != 0)
7319 return cmp;
7320 cmp = isl_aff_plain_cmp(pa1->p[i].aff, pa2->p[i].aff);
7321 if (cmp != 0)
7322 return cmp;
7325 return 0;
7328 /* Return a piecewise affine expression that is equal to "v" on "domain".
7330 __isl_give isl_pw_aff *isl_pw_aff_val_on_domain(__isl_take isl_set *domain,
7331 __isl_take isl_val *v)
7333 isl_space *space;
7334 isl_local_space *ls;
7335 isl_aff *aff;
7337 space = isl_set_get_space(domain);
7338 ls = isl_local_space_from_space(space);
7339 aff = isl_aff_val_on_domain(ls, v);
7341 return isl_pw_aff_alloc(domain, aff);
7344 /* Return a piecewise affine expression that is equal to the parameter
7345 * with identifier "id" on "domain".
7347 __isl_give isl_pw_aff *isl_pw_aff_param_on_domain_id(
7348 __isl_take isl_set *domain, __isl_take isl_id *id)
7350 isl_space *space;
7351 isl_aff *aff;
7353 space = isl_set_get_space(domain);
7354 space = isl_space_add_param_id(space, isl_id_copy(id));
7355 domain = isl_set_align_params(domain, isl_space_copy(space));
7356 aff = isl_aff_param_on_domain_space_id(space, id);
7358 return isl_pw_aff_alloc(domain, aff);
7361 /* Return a multi affine expression that is equal to "mv" on domain
7362 * space "space".
7364 __isl_give isl_multi_aff *isl_multi_aff_multi_val_on_space(
7365 __isl_take isl_space *space, __isl_take isl_multi_val *mv)
7367 int i;
7368 isl_size n;
7369 isl_space *space2;
7370 isl_local_space *ls;
7371 isl_multi_aff *ma;
7373 n = isl_multi_val_dim(mv, isl_dim_set);
7374 if (!space || n < 0)
7375 goto error;
7377 space2 = isl_multi_val_get_space(mv);
7378 space2 = isl_space_align_params(space2, isl_space_copy(space));
7379 space = isl_space_align_params(space, isl_space_copy(space2));
7380 space = isl_space_map_from_domain_and_range(space, space2);
7381 ma = isl_multi_aff_alloc(isl_space_copy(space));
7382 ls = isl_local_space_from_space(isl_space_domain(space));
7383 for (i = 0; i < n; ++i) {
7384 isl_val *v;
7385 isl_aff *aff;
7387 v = isl_multi_val_get_val(mv, i);
7388 aff = isl_aff_val_on_domain(isl_local_space_copy(ls), v);
7389 ma = isl_multi_aff_set_aff(ma, i, aff);
7391 isl_local_space_free(ls);
7393 isl_multi_val_free(mv);
7394 return ma;
7395 error:
7396 isl_space_free(space);
7397 isl_multi_val_free(mv);
7398 return NULL;
7401 /* Return a piecewise multi-affine expression
7402 * that is equal to "mv" on "domain".
7404 __isl_give isl_pw_multi_aff *isl_pw_multi_aff_multi_val_on_domain(
7405 __isl_take isl_set *domain, __isl_take isl_multi_val *mv)
7407 isl_space *space;
7408 isl_multi_aff *ma;
7410 space = isl_set_get_space(domain);
7411 ma = isl_multi_aff_multi_val_on_space(space, mv);
7413 return isl_pw_multi_aff_alloc(domain, ma);
7416 /* Internal data structure for isl_union_pw_multi_aff_multi_val_on_domain.
7417 * mv is the value that should be attained on each domain set
7418 * res collects the results
7420 struct isl_union_pw_multi_aff_multi_val_on_domain_data {
7421 isl_multi_val *mv;
7422 isl_union_pw_multi_aff *res;
7425 /* Create an isl_pw_multi_aff equal to data->mv on "domain"
7426 * and add it to data->res.
7428 static isl_stat pw_multi_aff_multi_val_on_domain(__isl_take isl_set *domain,
7429 void *user)
7431 struct isl_union_pw_multi_aff_multi_val_on_domain_data *data = user;
7432 isl_pw_multi_aff *pma;
7433 isl_multi_val *mv;
7435 mv = isl_multi_val_copy(data->mv);
7436 pma = isl_pw_multi_aff_multi_val_on_domain(domain, mv);
7437 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
7439 return data->res ? isl_stat_ok : isl_stat_error;
7442 /* Return a union piecewise multi-affine expression
7443 * that is equal to "mv" on "domain".
7445 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_multi_val_on_domain(
7446 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
7448 struct isl_union_pw_multi_aff_multi_val_on_domain_data data;
7449 isl_space *space;
7451 space = isl_union_set_get_space(domain);
7452 data.res = isl_union_pw_multi_aff_empty(space);
7453 data.mv = mv;
7454 if (isl_union_set_foreach_set(domain,
7455 &pw_multi_aff_multi_val_on_domain, &data) < 0)
7456 data.res = isl_union_pw_multi_aff_free(data.res);
7457 isl_union_set_free(domain);
7458 isl_multi_val_free(mv);
7459 return data.res;
7462 /* Compute the pullback of data->pma by the function represented by "pma2",
7463 * provided the spaces match, and add the results to data->res.
7465 static isl_stat pullback_entry(__isl_take isl_pw_multi_aff *pma2, void *user)
7467 struct isl_union_pw_multi_aff_bin_data *data = user;
7469 if (!isl_space_tuple_is_equal(data->pma->dim, isl_dim_in,
7470 pma2->dim, isl_dim_out)) {
7471 isl_pw_multi_aff_free(pma2);
7472 return isl_stat_ok;
7475 pma2 = isl_pw_multi_aff_pullback_pw_multi_aff(
7476 isl_pw_multi_aff_copy(data->pma), pma2);
7478 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma2);
7479 if (!data->res)
7480 return isl_stat_error;
7482 return isl_stat_ok;
7485 /* Compute the pullback of "upma1" by the function represented by "upma2".
7487 __isl_give isl_union_pw_multi_aff *
7488 isl_union_pw_multi_aff_pullback_union_pw_multi_aff(
7489 __isl_take isl_union_pw_multi_aff *upma1,
7490 __isl_take isl_union_pw_multi_aff *upma2)
7492 return bin_op(upma1, upma2, &pullback_entry);
7495 /* Check that the domain space of "upa" matches "space".
7497 * This function is called from isl_multi_union_pw_aff_set_union_pw_aff and
7498 * can in principle never fail since the space "space" is that
7499 * of the isl_multi_union_pw_aff and is a set space such that
7500 * there is no domain space to match.
7502 * We check the parameters and double-check that "space" is
7503 * indeed that of a set.
7505 static isl_stat isl_union_pw_aff_check_match_domain_space(
7506 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7508 isl_space *upa_space;
7509 isl_bool match;
7511 if (!upa || !space)
7512 return isl_stat_error;
7514 match = isl_space_is_set(space);
7515 if (match < 0)
7516 return isl_stat_error;
7517 if (!match)
7518 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7519 "expecting set space", return isl_stat_error);
7521 upa_space = isl_union_pw_aff_get_space(upa);
7522 match = isl_space_has_equal_params(space, upa_space);
7523 if (match < 0)
7524 goto error;
7525 if (!match)
7526 isl_die(isl_space_get_ctx(space), isl_error_invalid,
7527 "parameters don't match", goto error);
7529 isl_space_free(upa_space);
7530 return isl_stat_ok;
7531 error:
7532 isl_space_free(upa_space);
7533 return isl_stat_error;
7536 /* Do the parameters of "upa" match those of "space"?
7538 static isl_bool isl_union_pw_aff_matching_params(
7539 __isl_keep isl_union_pw_aff *upa, __isl_keep isl_space *space)
7541 isl_space *upa_space;
7542 isl_bool match;
7544 if (!upa || !space)
7545 return isl_bool_error;
7547 upa_space = isl_union_pw_aff_get_space(upa);
7549 match = isl_space_has_equal_params(space, upa_space);
7551 isl_space_free(upa_space);
7552 return match;
7555 /* Internal data structure for isl_union_pw_aff_reset_domain_space.
7556 * space represents the new parameters.
7557 * res collects the results.
7559 struct isl_union_pw_aff_reset_params_data {
7560 isl_space *space;
7561 isl_union_pw_aff *res;
7564 /* Replace the parameters of "pa" by data->space and
7565 * add the result to data->res.
7567 static isl_stat reset_params(__isl_take isl_pw_aff *pa, void *user)
7569 struct isl_union_pw_aff_reset_params_data *data = user;
7570 isl_space *space;
7572 space = isl_pw_aff_get_space(pa);
7573 space = isl_space_replace_params(space, data->space);
7574 pa = isl_pw_aff_reset_space(pa, space);
7575 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7577 return data->res ? isl_stat_ok : isl_stat_error;
7580 /* Replace the domain space of "upa" by "space".
7581 * Since a union expression does not have a (single) domain space,
7582 * "space" is necessarily a parameter space.
7584 * Since the order and the names of the parameters determine
7585 * the hash value, we need to create a new hash table.
7587 static __isl_give isl_union_pw_aff *isl_union_pw_aff_reset_domain_space(
7588 __isl_take isl_union_pw_aff *upa, __isl_take isl_space *space)
7590 struct isl_union_pw_aff_reset_params_data data = { space };
7591 isl_bool match;
7593 match = isl_union_pw_aff_matching_params(upa, space);
7594 if (match < 0)
7595 upa = isl_union_pw_aff_free(upa);
7596 else if (match) {
7597 isl_space_free(space);
7598 return upa;
7601 data.res = isl_union_pw_aff_empty(isl_space_copy(space));
7602 if (isl_union_pw_aff_foreach_pw_aff(upa, &reset_params, &data) < 0)
7603 data.res = isl_union_pw_aff_free(data.res);
7605 isl_union_pw_aff_free(upa);
7606 isl_space_free(space);
7607 return data.res;
7610 /* Return the floor of "pa".
7612 static __isl_give isl_pw_aff *floor_entry(__isl_take isl_pw_aff *pa, void *user)
7614 return isl_pw_aff_floor(pa);
7617 /* Given f, return floor(f).
7619 __isl_give isl_union_pw_aff *isl_union_pw_aff_floor(
7620 __isl_take isl_union_pw_aff *upa)
7622 return isl_union_pw_aff_transform_inplace(upa, &floor_entry, NULL);
7625 /* Compute
7627 * upa mod m = upa - m * floor(upa/m)
7629 * with m an integer value.
7631 __isl_give isl_union_pw_aff *isl_union_pw_aff_mod_val(
7632 __isl_take isl_union_pw_aff *upa, __isl_take isl_val *m)
7634 isl_union_pw_aff *res;
7636 if (!upa || !m)
7637 goto error;
7639 if (!isl_val_is_int(m))
7640 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7641 "expecting integer modulo", goto error);
7642 if (!isl_val_is_pos(m))
7643 isl_die(isl_val_get_ctx(m), isl_error_invalid,
7644 "expecting positive modulo", goto error);
7646 res = isl_union_pw_aff_copy(upa);
7647 upa = isl_union_pw_aff_scale_down_val(upa, isl_val_copy(m));
7648 upa = isl_union_pw_aff_floor(upa);
7649 upa = isl_union_pw_aff_scale_val(upa, m);
7650 res = isl_union_pw_aff_sub(res, upa);
7652 return res;
7653 error:
7654 isl_val_free(m);
7655 isl_union_pw_aff_free(upa);
7656 return NULL;
7659 /* Internal data structure for isl_union_pw_multi_aff_get_union_pw_aff.
7660 * pos is the output position that needs to be extracted.
7661 * res collects the results.
7663 struct isl_union_pw_multi_aff_get_union_pw_aff_data {
7664 int pos;
7665 isl_union_pw_aff *res;
7668 /* Extract an isl_pw_aff corresponding to output dimension "pos" of "pma"
7669 * (assuming it has such a dimension) and add it to data->res.
7671 static isl_stat get_union_pw_aff(__isl_take isl_pw_multi_aff *pma, void *user)
7673 struct isl_union_pw_multi_aff_get_union_pw_aff_data *data = user;
7674 isl_size n_out;
7675 isl_pw_aff *pa;
7677 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
7678 if (n_out < 0)
7679 return isl_stat_error;
7680 if (data->pos >= n_out) {
7681 isl_pw_multi_aff_free(pma);
7682 return isl_stat_ok;
7685 pa = isl_pw_multi_aff_get_pw_aff(pma, data->pos);
7686 isl_pw_multi_aff_free(pma);
7688 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7690 return data->res ? isl_stat_ok : isl_stat_error;
7693 /* Extract an isl_union_pw_aff corresponding to
7694 * output dimension "pos" of "upma".
7696 __isl_give isl_union_pw_aff *isl_union_pw_multi_aff_get_union_pw_aff(
7697 __isl_keep isl_union_pw_multi_aff *upma, int pos)
7699 struct isl_union_pw_multi_aff_get_union_pw_aff_data data;
7700 isl_space *space;
7702 if (!upma)
7703 return NULL;
7705 if (pos < 0)
7706 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
7707 "cannot extract at negative position", return NULL);
7709 space = isl_union_pw_multi_aff_get_space(upma);
7710 data.res = isl_union_pw_aff_empty(space);
7711 data.pos = pos;
7712 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
7713 &get_union_pw_aff, &data) < 0)
7714 data.res = isl_union_pw_aff_free(data.res);
7716 return data.res;
7719 /* Return a union piecewise affine expression
7720 * that is equal to "aff" on "domain".
7722 __isl_give isl_union_pw_aff *isl_union_pw_aff_aff_on_domain(
7723 __isl_take isl_union_set *domain, __isl_take isl_aff *aff)
7725 isl_pw_aff *pa;
7727 pa = isl_pw_aff_from_aff(aff);
7728 return isl_union_pw_aff_pw_aff_on_domain(domain, pa);
7731 /* Return a union piecewise affine expression
7732 * that is equal to the parameter identified by "id" on "domain".
7734 * Make sure the parameter appears in the space passed to
7735 * isl_aff_param_on_domain_space_id.
7737 __isl_give isl_union_pw_aff *isl_union_pw_aff_param_on_domain_id(
7738 __isl_take isl_union_set *domain, __isl_take isl_id *id)
7740 isl_space *space;
7741 isl_aff *aff;
7743 space = isl_union_set_get_space(domain);
7744 space = isl_space_add_param_id(space, isl_id_copy(id));
7745 aff = isl_aff_param_on_domain_space_id(space, id);
7746 return isl_union_pw_aff_aff_on_domain(domain, aff);
7749 /* Internal data structure for isl_union_pw_aff_pw_aff_on_domain.
7750 * "pa" is the piecewise symbolic value that the resulting isl_union_pw_aff
7751 * needs to attain.
7752 * "res" collects the results.
7754 struct isl_union_pw_aff_pw_aff_on_domain_data {
7755 isl_pw_aff *pa;
7756 isl_union_pw_aff *res;
7759 /* Construct a piecewise affine expression that is equal to data->pa
7760 * on "domain" and add the result to data->res.
7762 static isl_stat pw_aff_on_domain(__isl_take isl_set *domain, void *user)
7764 struct isl_union_pw_aff_pw_aff_on_domain_data *data = user;
7765 isl_pw_aff *pa;
7766 isl_size dim;
7768 pa = isl_pw_aff_copy(data->pa);
7769 dim = isl_set_dim(domain, isl_dim_set);
7770 if (dim < 0)
7771 pa = isl_pw_aff_free(pa);
7772 pa = isl_pw_aff_from_range(pa);
7773 pa = isl_pw_aff_add_dims(pa, isl_dim_in, dim);
7774 pa = isl_pw_aff_reset_domain_space(pa, isl_set_get_space(domain));
7775 pa = isl_pw_aff_intersect_domain(pa, domain);
7776 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7778 return data->res ? isl_stat_ok : isl_stat_error;
7781 /* Return a union piecewise affine expression
7782 * that is equal to "pa" on "domain", assuming "domain" and "pa"
7783 * have been aligned.
7785 * Construct an isl_pw_aff on each of the sets in "domain" and
7786 * collect the results.
7788 static __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain_aligned(
7789 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7791 struct isl_union_pw_aff_pw_aff_on_domain_data data;
7792 isl_space *space;
7794 space = isl_union_set_get_space(domain);
7795 data.res = isl_union_pw_aff_empty(space);
7796 data.pa = pa;
7797 if (isl_union_set_foreach_set(domain, &pw_aff_on_domain, &data) < 0)
7798 data.res = isl_union_pw_aff_free(data.res);
7799 isl_union_set_free(domain);
7800 isl_pw_aff_free(pa);
7801 return data.res;
7804 /* Return a union piecewise affine expression
7805 * that is equal to "pa" on "domain".
7807 * Check that "pa" is a parametric expression,
7808 * align the parameters if needed and call
7809 * isl_union_pw_aff_pw_aff_on_domain_aligned.
7811 __isl_give isl_union_pw_aff *isl_union_pw_aff_pw_aff_on_domain(
7812 __isl_take isl_union_set *domain, __isl_take isl_pw_aff *pa)
7814 isl_bool is_set;
7815 isl_bool equal_params;
7816 isl_space *domain_space, *pa_space;
7818 pa_space = isl_pw_aff_peek_space(pa);
7819 is_set = isl_space_is_set(pa_space);
7820 if (is_set < 0)
7821 goto error;
7822 if (!is_set)
7823 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
7824 "expecting parametric expression", goto error);
7826 domain_space = isl_union_set_get_space(domain);
7827 pa_space = isl_pw_aff_get_space(pa);
7828 equal_params = isl_space_has_equal_params(domain_space, pa_space);
7829 if (equal_params >= 0 && !equal_params) {
7830 isl_space *space;
7832 space = isl_space_align_params(domain_space, pa_space);
7833 pa = isl_pw_aff_align_params(pa, isl_space_copy(space));
7834 domain = isl_union_set_align_params(domain, space);
7835 } else {
7836 isl_space_free(domain_space);
7837 isl_space_free(pa_space);
7840 if (equal_params < 0)
7841 goto error;
7842 return isl_union_pw_aff_pw_aff_on_domain_aligned(domain, pa);
7843 error:
7844 isl_union_set_free(domain);
7845 isl_pw_aff_free(pa);
7846 return NULL;
7849 /* Internal data structure for isl_union_pw_aff_val_on_domain.
7850 * "v" is the value that the resulting isl_union_pw_aff needs to attain.
7851 * "res" collects the results.
7853 struct isl_union_pw_aff_val_on_domain_data {
7854 isl_val *v;
7855 isl_union_pw_aff *res;
7858 /* Construct a piecewise affine expression that is equal to data->v
7859 * on "domain" and add the result to data->res.
7861 static isl_stat pw_aff_val_on_domain(__isl_take isl_set *domain, void *user)
7863 struct isl_union_pw_aff_val_on_domain_data *data = user;
7864 isl_pw_aff *pa;
7865 isl_val *v;
7867 v = isl_val_copy(data->v);
7868 pa = isl_pw_aff_val_on_domain(domain, v);
7869 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
7871 return data->res ? isl_stat_ok : isl_stat_error;
7874 /* Return a union piecewise affine expression
7875 * that is equal to "v" on "domain".
7877 * Construct an isl_pw_aff on each of the sets in "domain" and
7878 * collect the results.
7880 __isl_give isl_union_pw_aff *isl_union_pw_aff_val_on_domain(
7881 __isl_take isl_union_set *domain, __isl_take isl_val *v)
7883 struct isl_union_pw_aff_val_on_domain_data data;
7884 isl_space *space;
7886 space = isl_union_set_get_space(domain);
7887 data.res = isl_union_pw_aff_empty(space);
7888 data.v = v;
7889 if (isl_union_set_foreach_set(domain, &pw_aff_val_on_domain, &data) < 0)
7890 data.res = isl_union_pw_aff_free(data.res);
7891 isl_union_set_free(domain);
7892 isl_val_free(v);
7893 return data.res;
7896 /* Construct a piecewise multi affine expression
7897 * that is equal to "pa" and add it to upma.
7899 static isl_stat pw_multi_aff_from_pw_aff_entry(__isl_take isl_pw_aff *pa,
7900 void *user)
7902 isl_union_pw_multi_aff **upma = user;
7903 isl_pw_multi_aff *pma;
7905 pma = isl_pw_multi_aff_from_pw_aff(pa);
7906 *upma = isl_union_pw_multi_aff_add_pw_multi_aff(*upma, pma);
7908 return *upma ? isl_stat_ok : isl_stat_error;
7911 /* Construct and return a union piecewise multi affine expression
7912 * that is equal to the given union piecewise affine expression.
7914 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_from_union_pw_aff(
7915 __isl_take isl_union_pw_aff *upa)
7917 isl_space *space;
7918 isl_union_pw_multi_aff *upma;
7920 if (!upa)
7921 return NULL;
7923 space = isl_union_pw_aff_get_space(upa);
7924 upma = isl_union_pw_multi_aff_empty(space);
7926 if (isl_union_pw_aff_foreach_pw_aff(upa,
7927 &pw_multi_aff_from_pw_aff_entry, &upma) < 0)
7928 upma = isl_union_pw_multi_aff_free(upma);
7930 isl_union_pw_aff_free(upa);
7931 return upma;
7934 /* Compute the set of elements in the domain of "pa" where it is zero and
7935 * add this set to "uset".
7937 static isl_stat zero_union_set(__isl_take isl_pw_aff *pa, void *user)
7939 isl_union_set **uset = (isl_union_set **)user;
7941 *uset = isl_union_set_add_set(*uset, isl_pw_aff_zero_set(pa));
7943 return *uset ? isl_stat_ok : isl_stat_error;
7946 /* Return a union set containing those elements in the domain
7947 * of "upa" where it is zero.
7949 __isl_give isl_union_set *isl_union_pw_aff_zero_union_set(
7950 __isl_take isl_union_pw_aff *upa)
7952 isl_union_set *zero;
7954 zero = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7955 if (isl_union_pw_aff_foreach_pw_aff(upa, &zero_union_set, &zero) < 0)
7956 zero = isl_union_set_free(zero);
7958 isl_union_pw_aff_free(upa);
7959 return zero;
7962 /* Internal data structure for isl_union_pw_aff_bind_id,
7963 * storing the parameter that needs to be bound and
7964 * the accumulated results.
7966 struct isl_bind_id_data {
7967 isl_id *id;
7968 isl_union_set *bound;
7971 /* Bind the piecewise affine function "pa" to the parameter data->id,
7972 * adding the resulting elements in the domain where the expression
7973 * is equal to the parameter to data->bound.
7975 static isl_stat bind_id(__isl_take isl_pw_aff *pa, void *user)
7977 struct isl_bind_id_data *data = user;
7978 isl_set *bound;
7980 bound = isl_pw_aff_bind_id(pa, isl_id_copy(data->id));
7981 data->bound = isl_union_set_add_set(data->bound, bound);
7983 return data->bound ? isl_stat_ok : isl_stat_error;
7986 /* Bind the union piecewise affine function "upa" to the parameter "id",
7987 * returning the elements in the domain where the expression
7988 * is equal to the parameter.
7990 __isl_give isl_union_set *isl_union_pw_aff_bind_id(
7991 __isl_take isl_union_pw_aff *upa, __isl_take isl_id *id)
7993 struct isl_bind_id_data data = { id };
7995 data.bound = isl_union_set_empty(isl_union_pw_aff_get_space(upa));
7996 if (isl_union_pw_aff_foreach_pw_aff(upa, &bind_id, &data) < 0)
7997 data.bound = isl_union_set_free(data.bound);
7999 isl_union_pw_aff_free(upa);
8000 isl_id_free(id);
8001 return data.bound;
8004 /* Internal data structure for isl_union_pw_aff_pullback_union_pw_multi_aff.
8005 * upma is the function that is plugged in.
8006 * pa is the current part of the function in which upma is plugged in.
8007 * res collects the results.
8009 struct isl_union_pw_aff_pullback_upma_data {
8010 isl_union_pw_multi_aff *upma;
8011 isl_pw_aff *pa;
8012 isl_union_pw_aff *res;
8015 /* Check if "pma" can be plugged into data->pa.
8016 * If so, perform the pullback and add the result to data->res.
8018 static isl_stat pa_pb_pma(__isl_take isl_pw_multi_aff *pma, void *user)
8020 struct isl_union_pw_aff_pullback_upma_data *data = user;
8021 isl_pw_aff *pa;
8023 if (!isl_space_tuple_is_equal(data->pa->dim, isl_dim_in,
8024 pma->dim, isl_dim_out)) {
8025 isl_pw_multi_aff_free(pma);
8026 return isl_stat_ok;
8029 pa = isl_pw_aff_copy(data->pa);
8030 pa = isl_pw_aff_pullback_pw_multi_aff(pa, pma);
8032 data->res = isl_union_pw_aff_add_pw_aff(data->res, pa);
8034 return data->res ? isl_stat_ok : isl_stat_error;
8037 /* Check if any of the elements of data->upma can be plugged into pa,
8038 * add if so add the result to data->res.
8040 static isl_stat upa_pb_upma(__isl_take isl_pw_aff *pa, void *user)
8042 struct isl_union_pw_aff_pullback_upma_data *data = user;
8043 isl_stat r;
8045 data->pa = pa;
8046 r = isl_union_pw_multi_aff_foreach_pw_multi_aff(data->upma,
8047 &pa_pb_pma, data);
8048 isl_pw_aff_free(pa);
8050 return r;
8053 /* Compute the pullback of "upa" by the function represented by "upma".
8054 * In other words, plug in "upma" in "upa". The result contains
8055 * expressions defined over the domain space of "upma".
8057 * Run over all pairs of elements in "upa" and "upma", perform
8058 * the pullback when appropriate and collect the results.
8059 * If the hash value were based on the domain space rather than
8060 * the function space, then we could run through all elements
8061 * of "upma" and directly pick out the corresponding element of "upa".
8063 __isl_give isl_union_pw_aff *isl_union_pw_aff_pullback_union_pw_multi_aff(
8064 __isl_take isl_union_pw_aff *upa,
8065 __isl_take isl_union_pw_multi_aff *upma)
8067 struct isl_union_pw_aff_pullback_upma_data data = { NULL, NULL };
8068 isl_space *space;
8070 space = isl_union_pw_multi_aff_get_space(upma);
8071 upa = isl_union_pw_aff_align_params(upa, space);
8072 space = isl_union_pw_aff_get_space(upa);
8073 upma = isl_union_pw_multi_aff_align_params(upma, space);
8075 if (!upa || !upma)
8076 goto error;
8078 data.upma = upma;
8079 data.res = isl_union_pw_aff_alloc_same_size(upa);
8080 if (isl_union_pw_aff_foreach_pw_aff(upa, &upa_pb_upma, &data) < 0)
8081 data.res = isl_union_pw_aff_free(data.res);
8083 isl_union_pw_aff_free(upa);
8084 isl_union_pw_multi_aff_free(upma);
8085 return data.res;
8086 error:
8087 isl_union_pw_aff_free(upa);
8088 isl_union_pw_multi_aff_free(upma);
8089 return NULL;
8092 #undef BASE
8093 #define BASE union_pw_aff
8094 #undef DOMBASE
8095 #define DOMBASE union_set
8097 #include <isl_multi_explicit_domain.c>
8098 #include <isl_multi_union_pw_aff_explicit_domain.c>
8099 #include <isl_multi_templ.c>
8100 #include <isl_multi_apply_set.c>
8101 #include <isl_multi_apply_union_set.c>
8102 #include <isl_multi_arith_templ.c>
8103 #include <isl_multi_bind_templ.c>
8104 #include <isl_multi_coalesce.c>
8105 #include <isl_multi_dim_id_templ.c>
8106 #include <isl_multi_floor.c>
8107 #include <isl_multi_from_base_templ.c>
8108 #include <isl_multi_gist.c>
8109 #include <isl_multi_align_set.c>
8110 #include <isl_multi_align_union_set.c>
8111 #include <isl_multi_intersect.c>
8112 #include <isl_multi_nan_templ.c>
8113 #include <isl_multi_tuple_id_templ.c>
8114 #include <isl_multi_union_add_templ.c>
8116 /* Does "mupa" have a non-trivial explicit domain?
8118 * The explicit domain, if present, is trivial if it represents
8119 * an (obviously) universe parameter set.
8121 isl_bool isl_multi_union_pw_aff_has_non_trivial_domain(
8122 __isl_keep isl_multi_union_pw_aff *mupa)
8124 isl_bool is_params, trivial;
8125 isl_set *set;
8127 if (!mupa)
8128 return isl_bool_error;
8129 if (!isl_multi_union_pw_aff_has_explicit_domain(mupa))
8130 return isl_bool_false;
8131 is_params = isl_union_set_is_params(mupa->u.dom);
8132 if (is_params < 0 || !is_params)
8133 return isl_bool_not(is_params);
8134 set = isl_set_from_union_set(isl_union_set_copy(mupa->u.dom));
8135 trivial = isl_set_plain_is_universe(set);
8136 isl_set_free(set);
8137 return isl_bool_not(trivial);
8140 /* Construct a multiple union piecewise affine expression
8141 * in the given space with value zero in each of the output dimensions.
8143 * Since there is no canonical zero value for
8144 * a union piecewise affine expression, we can only construct
8145 * a zero-dimensional "zero" value.
8147 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_zero(
8148 __isl_take isl_space *space)
8150 isl_bool params;
8151 isl_size dim;
8153 if (!space)
8154 return NULL;
8156 params = isl_space_is_params(space);
8157 if (params < 0)
8158 goto error;
8159 if (params)
8160 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8161 "expecting proper set space", goto error);
8162 if (!isl_space_is_set(space))
8163 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8164 "expecting set space", goto error);
8165 dim = isl_space_dim(space, isl_dim_out);
8166 if (dim < 0)
8167 goto error;
8168 if (dim != 0)
8169 isl_die(isl_space_get_ctx(space), isl_error_invalid,
8170 "expecting 0D space", goto error);
8172 return isl_multi_union_pw_aff_alloc(space);
8173 error:
8174 isl_space_free(space);
8175 return NULL;
8178 /* Construct and return a multi union piecewise affine expression
8179 * that is equal to the given multi affine expression.
8181 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_aff(
8182 __isl_take isl_multi_aff *ma)
8184 isl_multi_pw_aff *mpa;
8186 mpa = isl_multi_pw_aff_from_multi_aff(ma);
8187 return isl_multi_union_pw_aff_from_multi_pw_aff(mpa);
8190 /* Construct and return a multi union piecewise affine expression
8191 * that is equal to the given multi piecewise affine expression.
8193 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_multi_pw_aff(
8194 __isl_take isl_multi_pw_aff *mpa)
8196 int i;
8197 isl_size n;
8198 isl_space *space;
8199 isl_multi_union_pw_aff *mupa;
8201 n = isl_multi_pw_aff_dim(mpa, isl_dim_out);
8202 if (n < 0)
8203 mpa = isl_multi_pw_aff_free(mpa);
8204 if (!mpa)
8205 return NULL;
8207 space = isl_multi_pw_aff_get_space(mpa);
8208 space = isl_space_range(space);
8209 mupa = isl_multi_union_pw_aff_alloc(space);
8211 for (i = 0; i < n; ++i) {
8212 isl_pw_aff *pa;
8213 isl_union_pw_aff *upa;
8215 pa = isl_multi_pw_aff_get_pw_aff(mpa, i);
8216 upa = isl_union_pw_aff_from_pw_aff(pa);
8217 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8220 isl_multi_pw_aff_free(mpa);
8222 return mupa;
8225 /* Extract the range space of "pma" and assign it to *space.
8226 * If *space has already been set (through a previous call to this function),
8227 * then check that the range space is the same.
8229 static isl_stat extract_space(__isl_take isl_pw_multi_aff *pma, void *user)
8231 isl_space **space = user;
8232 isl_space *pma_space;
8233 isl_bool equal;
8235 pma_space = isl_space_range(isl_pw_multi_aff_get_space(pma));
8236 isl_pw_multi_aff_free(pma);
8238 if (!pma_space)
8239 return isl_stat_error;
8240 if (!*space) {
8241 *space = pma_space;
8242 return isl_stat_ok;
8245 equal = isl_space_is_equal(pma_space, *space);
8246 isl_space_free(pma_space);
8248 if (equal < 0)
8249 return isl_stat_error;
8250 if (!equal)
8251 isl_die(isl_space_get_ctx(*space), isl_error_invalid,
8252 "range spaces not the same", return isl_stat_error);
8253 return isl_stat_ok;
8256 /* Construct and return a multi union piecewise affine expression
8257 * that is equal to the given union piecewise multi affine expression.
8259 * In order to be able to perform the conversion, the input
8260 * needs to be non-empty and may only involve a single range space.
8262 * If the resulting multi union piecewise affine expression has
8263 * an explicit domain, then assign it the domain of the input.
8264 * In other cases, the domain is stored in the individual elements.
8266 __isl_give isl_multi_union_pw_aff *
8267 isl_multi_union_pw_aff_from_union_pw_multi_aff(
8268 __isl_take isl_union_pw_multi_aff *upma)
8270 isl_space *space = NULL;
8271 isl_multi_union_pw_aff *mupa;
8272 int i;
8273 isl_size n;
8275 n = isl_union_pw_multi_aff_n_pw_multi_aff(upma);
8276 if (n < 0)
8277 goto error;
8278 if (n == 0)
8279 isl_die(isl_union_pw_multi_aff_get_ctx(upma), isl_error_invalid,
8280 "cannot extract range space from empty input",
8281 goto error);
8282 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma, &extract_space,
8283 &space) < 0)
8284 goto error;
8286 if (!space)
8287 goto error;
8289 n = isl_space_dim(space, isl_dim_set);
8290 if (n < 0)
8291 space = isl_space_free(space);
8292 mupa = isl_multi_union_pw_aff_alloc(space);
8294 for (i = 0; i < n; ++i) {
8295 isl_union_pw_aff *upa;
8297 upa = isl_union_pw_multi_aff_get_union_pw_aff(upma, i);
8298 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8300 if (isl_multi_union_pw_aff_has_explicit_domain(mupa)) {
8301 isl_union_set *dom;
8302 isl_union_pw_multi_aff *copy;
8304 copy = isl_union_pw_multi_aff_copy(upma);
8305 dom = isl_union_pw_multi_aff_domain(copy);
8306 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, dom);
8309 isl_union_pw_multi_aff_free(upma);
8310 return mupa;
8311 error:
8312 isl_space_free(space);
8313 isl_union_pw_multi_aff_free(upma);
8314 return NULL;
8317 /* Try and create an isl_multi_union_pw_aff that is equivalent
8318 * to the given isl_union_map.
8319 * The isl_union_map is required to be single-valued in each space.
8320 * Moreover, it cannot be empty and all range spaces need to be the same.
8321 * Otherwise, an error is produced.
8323 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_from_union_map(
8324 __isl_take isl_union_map *umap)
8326 isl_union_pw_multi_aff *upma;
8328 upma = isl_union_pw_multi_aff_from_union_map(umap);
8329 return isl_multi_union_pw_aff_from_union_pw_multi_aff(upma);
8332 /* Return a multiple union piecewise affine expression
8333 * that is equal to "mv" on "domain", assuming "domain" and "mv"
8334 * have been aligned.
8336 * If the resulting multi union piecewise affine expression has
8337 * an explicit domain, then assign it the input domain.
8338 * In other cases, the domain is stored in the individual elements.
8340 static __isl_give isl_multi_union_pw_aff *
8341 isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8342 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8344 int i;
8345 isl_size n;
8346 isl_space *space;
8347 isl_multi_union_pw_aff *mupa;
8349 n = isl_multi_val_dim(mv, isl_dim_set);
8350 if (!domain || n < 0)
8351 goto error;
8353 space = isl_multi_val_get_space(mv);
8354 mupa = isl_multi_union_pw_aff_alloc(space);
8355 for (i = 0; i < n; ++i) {
8356 isl_val *v;
8357 isl_union_pw_aff *upa;
8359 v = isl_multi_val_get_val(mv, i);
8360 upa = isl_union_pw_aff_val_on_domain(isl_union_set_copy(domain),
8362 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8364 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8365 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8366 isl_union_set_copy(domain));
8368 isl_union_set_free(domain);
8369 isl_multi_val_free(mv);
8370 return mupa;
8371 error:
8372 isl_union_set_free(domain);
8373 isl_multi_val_free(mv);
8374 return NULL;
8377 /* Return a multiple union piecewise affine expression
8378 * that is equal to "mv" on "domain".
8380 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_val_on_domain(
8381 __isl_take isl_union_set *domain, __isl_take isl_multi_val *mv)
8383 isl_bool equal_params;
8385 if (!domain || !mv)
8386 goto error;
8387 equal_params = isl_space_has_equal_params(domain->dim, mv->space);
8388 if (equal_params < 0)
8389 goto error;
8390 if (equal_params)
8391 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(
8392 domain, mv);
8393 domain = isl_union_set_align_params(domain,
8394 isl_multi_val_get_space(mv));
8395 mv = isl_multi_val_align_params(mv, isl_union_set_get_space(domain));
8396 return isl_multi_union_pw_aff_multi_val_on_domain_aligned(domain, mv);
8397 error:
8398 isl_union_set_free(domain);
8399 isl_multi_val_free(mv);
8400 return NULL;
8403 /* Return a multiple union piecewise affine expression
8404 * that is equal to "ma" on "domain".
8406 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_multi_aff_on_domain(
8407 __isl_take isl_union_set *domain, __isl_take isl_multi_aff *ma)
8409 isl_pw_multi_aff *pma;
8411 pma = isl_pw_multi_aff_from_multi_aff(ma);
8412 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(domain, pma);
8415 /* Return a multiple union piecewise affine expression
8416 * that is equal to "pma" on "domain", assuming "domain" and "pma"
8417 * have been aligned.
8419 * If the resulting multi union piecewise affine expression has
8420 * an explicit domain, then assign it the input domain.
8421 * In other cases, the domain is stored in the individual elements.
8423 static __isl_give isl_multi_union_pw_aff *
8424 isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8425 __isl_take isl_union_set *domain, __isl_take isl_pw_multi_aff *pma)
8427 int i;
8428 isl_size n;
8429 isl_space *space;
8430 isl_multi_union_pw_aff *mupa;
8432 n = isl_pw_multi_aff_dim(pma, isl_dim_set);
8433 if (!domain || n < 0)
8434 goto error;
8435 space = isl_pw_multi_aff_get_space(pma);
8436 mupa = isl_multi_union_pw_aff_alloc(space);
8437 for (i = 0; i < n; ++i) {
8438 isl_pw_aff *pa;
8439 isl_union_pw_aff *upa;
8441 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
8442 upa = isl_union_pw_aff_pw_aff_on_domain(
8443 isl_union_set_copy(domain), pa);
8444 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
8446 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
8447 mupa = isl_multi_union_pw_aff_intersect_domain(mupa,
8448 isl_union_set_copy(domain));
8450 isl_union_set_free(domain);
8451 isl_pw_multi_aff_free(pma);
8452 return mupa;
8453 error:
8454 isl_union_set_free(domain);
8455 isl_pw_multi_aff_free(pma);
8456 return NULL;
8459 /* Return a multiple union piecewise affine expression
8460 * that is equal to "pma" on "domain".
8462 __isl_give isl_multi_union_pw_aff *
8463 isl_multi_union_pw_aff_pw_multi_aff_on_domain(__isl_take isl_union_set *domain,
8464 __isl_take isl_pw_multi_aff *pma)
8466 isl_bool equal_params;
8467 isl_space *space;
8469 space = isl_pw_multi_aff_peek_space(pma);
8470 equal_params = isl_union_set_space_has_equal_params(domain, space);
8471 if (equal_params < 0)
8472 goto error;
8473 if (equal_params)
8474 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(
8475 domain, pma);
8476 domain = isl_union_set_align_params(domain,
8477 isl_pw_multi_aff_get_space(pma));
8478 pma = isl_pw_multi_aff_align_params(pma,
8479 isl_union_set_get_space(domain));
8480 return isl_multi_union_pw_aff_pw_multi_aff_on_domain_aligned(domain,
8481 pma);
8482 error:
8483 isl_union_set_free(domain);
8484 isl_pw_multi_aff_free(pma);
8485 return NULL;
8488 /* Return a union set containing those elements in the domains
8489 * of the elements of "mupa" where they are all zero.
8491 * If there are no elements, then simply return the entire domain.
8493 __isl_give isl_union_set *isl_multi_union_pw_aff_zero_union_set(
8494 __isl_take isl_multi_union_pw_aff *mupa)
8496 int i;
8497 isl_size n;
8498 isl_union_pw_aff *upa;
8499 isl_union_set *zero;
8501 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8502 if (n < 0)
8503 mupa = isl_multi_union_pw_aff_free(mupa);
8504 if (!mupa)
8505 return NULL;
8507 if (n == 0)
8508 return isl_multi_union_pw_aff_domain(mupa);
8510 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8511 zero = isl_union_pw_aff_zero_union_set(upa);
8513 for (i = 1; i < n; ++i) {
8514 isl_union_set *zero_i;
8516 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8517 zero_i = isl_union_pw_aff_zero_union_set(upa);
8519 zero = isl_union_set_intersect(zero, zero_i);
8522 isl_multi_union_pw_aff_free(mupa);
8523 return zero;
8526 /* Construct a union map mapping the shared domain
8527 * of the union piecewise affine expressions to the range of "mupa"
8528 * in the special case of a 0D multi union piecewise affine expression.
8530 * Construct a map between the explicit domain of "mupa" and
8531 * the range space.
8532 * Note that this assumes that the domain consists of explicit elements.
8534 static __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff_0D(
8535 __isl_take isl_multi_union_pw_aff *mupa)
8537 isl_bool is_params;
8538 isl_space *space;
8539 isl_union_set *dom, *ran;
8541 space = isl_multi_union_pw_aff_get_space(mupa);
8542 dom = isl_multi_union_pw_aff_domain(mupa);
8543 ran = isl_union_set_from_set(isl_set_universe(space));
8545 is_params = isl_union_set_is_params(dom);
8546 if (is_params < 0)
8547 dom = isl_union_set_free(dom);
8548 else if (is_params)
8549 isl_die(isl_union_set_get_ctx(dom), isl_error_invalid,
8550 "cannot create union map from expression without "
8551 "explicit domain elements",
8552 dom = isl_union_set_free(dom));
8554 return isl_union_map_from_domain_and_range(dom, ran);
8557 /* Construct a union map mapping the shared domain
8558 * of the union piecewise affine expressions to the range of "mupa"
8559 * with each dimension in the range equated to the
8560 * corresponding union piecewise affine expression.
8562 * If the input is zero-dimensional, then construct a mapping
8563 * from its explicit domain.
8565 __isl_give isl_union_map *isl_union_map_from_multi_union_pw_aff(
8566 __isl_take isl_multi_union_pw_aff *mupa)
8568 int i;
8569 isl_size n;
8570 isl_space *space;
8571 isl_union_map *umap;
8572 isl_union_pw_aff *upa;
8574 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8575 if (n < 0)
8576 mupa = isl_multi_union_pw_aff_free(mupa);
8577 if (!mupa)
8578 return NULL;
8580 if (n == 0)
8581 return isl_union_map_from_multi_union_pw_aff_0D(mupa);
8583 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8584 umap = isl_union_map_from_union_pw_aff(upa);
8586 for (i = 1; i < n; ++i) {
8587 isl_union_map *umap_i;
8589 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8590 umap_i = isl_union_map_from_union_pw_aff(upa);
8591 umap = isl_union_map_flat_range_product(umap, umap_i);
8594 space = isl_multi_union_pw_aff_get_space(mupa);
8595 umap = isl_union_map_reset_range_space(umap, space);
8597 isl_multi_union_pw_aff_free(mupa);
8598 return umap;
8601 /* Internal data structure for isl_union_pw_multi_aff_reset_range_space.
8602 * "range" is the space from which to set the range space.
8603 * "res" collects the results.
8605 struct isl_union_pw_multi_aff_reset_range_space_data {
8606 isl_space *range;
8607 isl_union_pw_multi_aff *res;
8610 /* Replace the range space of "pma" by the range space of data->range and
8611 * add the result to data->res.
8613 static isl_stat reset_range_space(__isl_take isl_pw_multi_aff *pma, void *user)
8615 struct isl_union_pw_multi_aff_reset_range_space_data *data = user;
8616 isl_space *space;
8618 space = isl_pw_multi_aff_get_space(pma);
8619 space = isl_space_domain(space);
8620 space = isl_space_extend_domain_with_range(space,
8621 isl_space_copy(data->range));
8622 pma = isl_pw_multi_aff_reset_space(pma, space);
8623 data->res = isl_union_pw_multi_aff_add_pw_multi_aff(data->res, pma);
8625 return data->res ? isl_stat_ok : isl_stat_error;
8628 /* Replace the range space of all the piecewise affine expressions in "upma" by
8629 * the range space of "space".
8631 * This assumes that all these expressions have the same output dimension.
8633 * Since the spaces of the expressions change, so do their hash values.
8634 * We therefore need to create a new isl_union_pw_multi_aff.
8635 * Note that the hash value is currently computed based on the entire
8636 * space even though there can only be a single expression with a given
8637 * domain space.
8639 static __isl_give isl_union_pw_multi_aff *
8640 isl_union_pw_multi_aff_reset_range_space(
8641 __isl_take isl_union_pw_multi_aff *upma, __isl_take isl_space *space)
8643 struct isl_union_pw_multi_aff_reset_range_space_data data = { space };
8644 isl_space *space_upma;
8646 space_upma = isl_union_pw_multi_aff_get_space(upma);
8647 data.res = isl_union_pw_multi_aff_empty(space_upma);
8648 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
8649 &reset_range_space, &data) < 0)
8650 data.res = isl_union_pw_multi_aff_free(data.res);
8652 isl_space_free(space);
8653 isl_union_pw_multi_aff_free(upma);
8654 return data.res;
8657 /* Construct and return a union piecewise multi affine expression
8658 * that is equal to the given multi union piecewise affine expression,
8659 * in the special case of a 0D multi union piecewise affine expression.
8661 * Construct a union piecewise multi affine expression
8662 * on top of the explicit domain of the input.
8664 __isl_give isl_union_pw_multi_aff *
8665 isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(
8666 __isl_take isl_multi_union_pw_aff *mupa)
8668 isl_space *space;
8669 isl_multi_val *mv;
8670 isl_union_set *domain;
8672 space = isl_multi_union_pw_aff_get_space(mupa);
8673 mv = isl_multi_val_zero(space);
8674 domain = isl_multi_union_pw_aff_domain(mupa);
8675 return isl_union_pw_multi_aff_multi_val_on_domain(domain, mv);
8678 /* Construct and return a union piecewise multi affine expression
8679 * that is equal to the given multi union piecewise affine expression.
8681 * If the input is zero-dimensional, then
8682 * construct a union piecewise multi affine expression
8683 * on top of the explicit domain of the input.
8685 __isl_give isl_union_pw_multi_aff *
8686 isl_union_pw_multi_aff_from_multi_union_pw_aff(
8687 __isl_take isl_multi_union_pw_aff *mupa)
8689 int i;
8690 isl_size n;
8691 isl_space *space;
8692 isl_union_pw_multi_aff *upma;
8693 isl_union_pw_aff *upa;
8695 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8696 if (n < 0)
8697 mupa = isl_multi_union_pw_aff_free(mupa);
8698 if (!mupa)
8699 return NULL;
8701 if (n == 0)
8702 return isl_union_pw_multi_aff_from_multi_union_pw_aff_0D(mupa);
8704 space = isl_multi_union_pw_aff_get_space(mupa);
8705 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8706 upma = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8708 for (i = 1; i < n; ++i) {
8709 isl_union_pw_multi_aff *upma_i;
8711 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8712 upma_i = isl_union_pw_multi_aff_from_union_pw_aff(upa);
8713 upma = isl_union_pw_multi_aff_flat_range_product(upma, upma_i);
8716 upma = isl_union_pw_multi_aff_reset_range_space(upma, space);
8718 isl_multi_union_pw_aff_free(mupa);
8719 return upma;
8722 /* Intersect the range of "mupa" with "range",
8723 * in the special case where "mupa" is 0D.
8725 * Intersect the domain of "mupa" with the constraints on the parameters
8726 * of "range".
8728 static __isl_give isl_multi_union_pw_aff *mupa_intersect_range_0D(
8729 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8731 range = isl_set_params(range);
8732 mupa = isl_multi_union_pw_aff_intersect_params(mupa, range);
8733 return mupa;
8736 /* Intersect the range of "mupa" with "range".
8737 * That is, keep only those domain elements that have a function value
8738 * in "range".
8740 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_intersect_range(
8741 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_set *range)
8743 isl_union_pw_multi_aff *upma;
8744 isl_union_set *domain;
8745 isl_space *space;
8746 isl_size n;
8747 int match;
8749 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8750 if (n < 0 || !range)
8751 goto error;
8753 space = isl_set_get_space(range);
8754 match = isl_space_tuple_is_equal(mupa->space, isl_dim_set,
8755 space, isl_dim_set);
8756 isl_space_free(space);
8757 if (match < 0)
8758 goto error;
8759 if (!match)
8760 isl_die(isl_multi_union_pw_aff_get_ctx(mupa), isl_error_invalid,
8761 "space don't match", goto error);
8762 if (n == 0)
8763 return mupa_intersect_range_0D(mupa, range);
8765 upma = isl_union_pw_multi_aff_from_multi_union_pw_aff(
8766 isl_multi_union_pw_aff_copy(mupa));
8767 domain = isl_union_set_from_set(range);
8768 domain = isl_union_set_preimage_union_pw_multi_aff(domain, upma);
8769 mupa = isl_multi_union_pw_aff_intersect_domain(mupa, domain);
8771 return mupa;
8772 error:
8773 isl_multi_union_pw_aff_free(mupa);
8774 isl_set_free(range);
8775 return NULL;
8778 /* Return the shared domain of the elements of "mupa",
8779 * in the special case where "mupa" is zero-dimensional.
8781 * Return the explicit domain of "mupa".
8782 * Note that this domain may be a parameter set, either
8783 * because "mupa" is meant to live in a set space or
8784 * because no explicit domain has been set.
8786 __isl_give isl_union_set *isl_multi_union_pw_aff_domain_0D(
8787 __isl_take isl_multi_union_pw_aff *mupa)
8789 isl_union_set *dom;
8791 dom = isl_multi_union_pw_aff_get_explicit_domain(mupa);
8792 isl_multi_union_pw_aff_free(mupa);
8794 return dom;
8797 /* Return the shared domain of the elements of "mupa".
8799 * If "mupa" is zero-dimensional, then return its explicit domain.
8801 __isl_give isl_union_set *isl_multi_union_pw_aff_domain(
8802 __isl_take isl_multi_union_pw_aff *mupa)
8804 int i;
8805 isl_size n;
8806 isl_union_pw_aff *upa;
8807 isl_union_set *dom;
8809 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
8810 if (n < 0)
8811 mupa = isl_multi_union_pw_aff_free(mupa);
8812 if (!mupa)
8813 return NULL;
8815 if (n == 0)
8816 return isl_multi_union_pw_aff_domain_0D(mupa);
8818 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, 0);
8819 dom = isl_union_pw_aff_domain(upa);
8820 for (i = 1; i < n; ++i) {
8821 isl_union_set *dom_i;
8823 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8824 dom_i = isl_union_pw_aff_domain(upa);
8825 dom = isl_union_set_intersect(dom, dom_i);
8828 isl_multi_union_pw_aff_free(mupa);
8829 return dom;
8832 /* Apply "aff" to "mupa". The space of "mupa" is equal to the domain of "aff".
8833 * In particular, the spaces have been aligned.
8834 * The result is defined over the shared domain of the elements of "mupa"
8836 * We first extract the parametric constant part of "aff" and
8837 * define that over the shared domain.
8838 * Then we iterate over all input dimensions of "aff" and add the corresponding
8839 * multiples of the elements of "mupa".
8840 * Finally, we consider the integer divisions, calling the function
8841 * recursively to obtain an isl_union_pw_aff corresponding to the
8842 * integer division argument.
8844 static __isl_give isl_union_pw_aff *multi_union_pw_aff_apply_aff(
8845 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8847 int i;
8848 isl_size n_in, n_div;
8849 isl_union_pw_aff *upa;
8850 isl_union_set *uset;
8851 isl_val *v;
8852 isl_aff *cst;
8854 n_in = isl_aff_dim(aff, isl_dim_in);
8855 n_div = isl_aff_dim(aff, isl_dim_div);
8856 if (n_in < 0 || n_div < 0)
8857 goto error;
8859 uset = isl_multi_union_pw_aff_domain(isl_multi_union_pw_aff_copy(mupa));
8860 cst = isl_aff_copy(aff);
8861 cst = isl_aff_drop_dims(cst, isl_dim_div, 0, n_div);
8862 cst = isl_aff_drop_dims(cst, isl_dim_in, 0, n_in);
8863 cst = isl_aff_project_domain_on_params(cst);
8864 upa = isl_union_pw_aff_aff_on_domain(uset, cst);
8866 for (i = 0; i < n_in; ++i) {
8867 isl_union_pw_aff *upa_i;
8869 if (!isl_aff_involves_dims(aff, isl_dim_in, i, 1))
8870 continue;
8871 v = isl_aff_get_coefficient_val(aff, isl_dim_in, i);
8872 upa_i = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
8873 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8874 upa = isl_union_pw_aff_add(upa, upa_i);
8877 for (i = 0; i < n_div; ++i) {
8878 isl_aff *div;
8879 isl_union_pw_aff *upa_i;
8881 if (!isl_aff_involves_dims(aff, isl_dim_div, i, 1))
8882 continue;
8883 div = isl_aff_get_div(aff, i);
8884 upa_i = multi_union_pw_aff_apply_aff(
8885 isl_multi_union_pw_aff_copy(mupa), div);
8886 upa_i = isl_union_pw_aff_floor(upa_i);
8887 v = isl_aff_get_coefficient_val(aff, isl_dim_div, i);
8888 upa_i = isl_union_pw_aff_scale_val(upa_i, v);
8889 upa = isl_union_pw_aff_add(upa, upa_i);
8892 isl_multi_union_pw_aff_free(mupa);
8893 isl_aff_free(aff);
8895 return upa;
8896 error:
8897 isl_multi_union_pw_aff_free(mupa);
8898 isl_aff_free(aff);
8899 return NULL;
8902 /* Apply "aff" to "mupa". The space of "mupa" needs to be compatible
8903 * with the domain of "aff".
8904 * Furthermore, the dimension of this space needs to be greater than zero.
8905 * The result is defined over the shared domain of the elements of "mupa"
8907 * We perform these checks and then hand over control to
8908 * multi_union_pw_aff_apply_aff.
8910 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_aff(
8911 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_aff *aff)
8913 isl_size dim;
8914 isl_space *space1, *space2;
8915 isl_bool equal;
8917 mupa = isl_multi_union_pw_aff_align_params(mupa,
8918 isl_aff_get_space(aff));
8919 aff = isl_aff_align_params(aff, isl_multi_union_pw_aff_get_space(mupa));
8920 if (!mupa || !aff)
8921 goto error;
8923 space1 = isl_multi_union_pw_aff_get_space(mupa);
8924 space2 = isl_aff_get_domain_space(aff);
8925 equal = isl_space_is_equal(space1, space2);
8926 isl_space_free(space1);
8927 isl_space_free(space2);
8928 if (equal < 0)
8929 goto error;
8930 if (!equal)
8931 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8932 "spaces don't match", goto error);
8933 dim = isl_aff_dim(aff, isl_dim_in);
8934 if (dim < 0)
8935 goto error;
8936 if (dim == 0)
8937 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
8938 "cannot determine domains", goto error);
8940 return multi_union_pw_aff_apply_aff(mupa, aff);
8941 error:
8942 isl_multi_union_pw_aff_free(mupa);
8943 isl_aff_free(aff);
8944 return NULL;
8947 /* Apply "ma" to "mupa", in the special case where "mupa" is 0D.
8948 * The space of "mupa" is known to be compatible with the domain of "ma".
8950 * Construct an isl_multi_union_pw_aff that is equal to "ma"
8951 * on the domain of "mupa".
8953 static __isl_give isl_multi_union_pw_aff *mupa_apply_multi_aff_0D(
8954 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8956 isl_union_set *dom;
8958 dom = isl_multi_union_pw_aff_domain(mupa);
8959 ma = isl_multi_aff_project_domain_on_params(ma);
8961 return isl_multi_union_pw_aff_multi_aff_on_domain(dom, ma);
8964 /* Apply "ma" to "mupa". The space of "mupa" needs to be compatible
8965 * with the domain of "ma".
8966 * The result is defined over the shared domain of the elements of "mupa"
8968 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_multi_aff(
8969 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_multi_aff *ma)
8971 isl_space *space1, *space2;
8972 isl_multi_union_pw_aff *res;
8973 isl_bool equal;
8974 int i;
8975 isl_size n_in, n_out;
8977 mupa = isl_multi_union_pw_aff_align_params(mupa,
8978 isl_multi_aff_get_space(ma));
8979 ma = isl_multi_aff_align_params(ma,
8980 isl_multi_union_pw_aff_get_space(mupa));
8981 n_in = isl_multi_aff_dim(ma, isl_dim_in);
8982 n_out = isl_multi_aff_dim(ma, isl_dim_out);
8983 if (!mupa || n_in < 0 || n_out < 0)
8984 goto error;
8986 space1 = isl_multi_union_pw_aff_get_space(mupa);
8987 space2 = isl_multi_aff_get_domain_space(ma);
8988 equal = isl_space_is_equal(space1, space2);
8989 isl_space_free(space1);
8990 isl_space_free(space2);
8991 if (equal < 0)
8992 goto error;
8993 if (!equal)
8994 isl_die(isl_multi_aff_get_ctx(ma), isl_error_invalid,
8995 "spaces don't match", goto error);
8996 if (n_in == 0)
8997 return mupa_apply_multi_aff_0D(mupa, ma);
8999 space1 = isl_space_range(isl_multi_aff_get_space(ma));
9000 res = isl_multi_union_pw_aff_alloc(space1);
9002 for (i = 0; i < n_out; ++i) {
9003 isl_aff *aff;
9004 isl_union_pw_aff *upa;
9006 aff = isl_multi_aff_get_aff(ma, i);
9007 upa = multi_union_pw_aff_apply_aff(
9008 isl_multi_union_pw_aff_copy(mupa), aff);
9009 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9012 isl_multi_aff_free(ma);
9013 isl_multi_union_pw_aff_free(mupa);
9014 return res;
9015 error:
9016 isl_multi_union_pw_aff_free(mupa);
9017 isl_multi_aff_free(ma);
9018 return NULL;
9021 /* Apply "pa" to "mupa", in the special case where "mupa" is 0D.
9022 * The space of "mupa" is known to be compatible with the domain of "pa".
9024 * Construct an isl_multi_union_pw_aff that is equal to "pa"
9025 * on the domain of "mupa".
9027 static __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff_0D(
9028 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9030 isl_union_set *dom;
9032 dom = isl_multi_union_pw_aff_domain(mupa);
9033 pa = isl_pw_aff_project_domain_on_params(pa);
9035 return isl_union_pw_aff_pw_aff_on_domain(dom, pa);
9038 /* Apply "pa" to "mupa". The space of "mupa" needs to be compatible
9039 * with the domain of "pa".
9040 * Furthermore, the dimension of this space needs to be greater than zero.
9041 * The result is defined over the shared domain of the elements of "mupa"
9043 __isl_give isl_union_pw_aff *isl_multi_union_pw_aff_apply_pw_aff(
9044 __isl_take isl_multi_union_pw_aff *mupa, __isl_take isl_pw_aff *pa)
9046 int i;
9047 isl_bool equal;
9048 isl_size n_in;
9049 isl_space *space, *space2;
9050 isl_union_pw_aff *upa;
9052 mupa = isl_multi_union_pw_aff_align_params(mupa,
9053 isl_pw_aff_get_space(pa));
9054 pa = isl_pw_aff_align_params(pa,
9055 isl_multi_union_pw_aff_get_space(mupa));
9056 if (!mupa || !pa)
9057 goto error;
9059 space = isl_multi_union_pw_aff_get_space(mupa);
9060 space2 = isl_pw_aff_get_domain_space(pa);
9061 equal = isl_space_is_equal(space, space2);
9062 isl_space_free(space);
9063 isl_space_free(space2);
9064 if (equal < 0)
9065 goto error;
9066 if (!equal)
9067 isl_die(isl_pw_aff_get_ctx(pa), isl_error_invalid,
9068 "spaces don't match", goto error);
9069 n_in = isl_pw_aff_dim(pa, isl_dim_in);
9070 if (n_in < 0)
9071 goto error;
9072 if (n_in == 0)
9073 return isl_multi_union_pw_aff_apply_pw_aff_0D(mupa, pa);
9075 space = isl_space_params(isl_multi_union_pw_aff_get_space(mupa));
9076 upa = isl_union_pw_aff_empty(space);
9078 for (i = 0; i < pa->n; ++i) {
9079 isl_aff *aff;
9080 isl_set *domain;
9081 isl_multi_union_pw_aff *mupa_i;
9082 isl_union_pw_aff *upa_i;
9084 mupa_i = isl_multi_union_pw_aff_copy(mupa);
9085 domain = isl_set_copy(pa->p[i].set);
9086 mupa_i = isl_multi_union_pw_aff_intersect_range(mupa_i, domain);
9087 aff = isl_aff_copy(pa->p[i].aff);
9088 upa_i = multi_union_pw_aff_apply_aff(mupa_i, aff);
9089 upa = isl_union_pw_aff_union_add(upa, upa_i);
9092 isl_multi_union_pw_aff_free(mupa);
9093 isl_pw_aff_free(pa);
9094 return upa;
9095 error:
9096 isl_multi_union_pw_aff_free(mupa);
9097 isl_pw_aff_free(pa);
9098 return NULL;
9101 /* Apply "pma" to "mupa", in the special case where "mupa" is 0D.
9102 * The space of "mupa" is known to be compatible with the domain of "pma".
9104 * Construct an isl_multi_union_pw_aff that is equal to "pma"
9105 * on the domain of "mupa".
9107 static __isl_give isl_multi_union_pw_aff *mupa_apply_pw_multi_aff_0D(
9108 __isl_take isl_multi_union_pw_aff *mupa,
9109 __isl_take isl_pw_multi_aff *pma)
9111 isl_union_set *dom;
9113 dom = isl_multi_union_pw_aff_domain(mupa);
9114 pma = isl_pw_multi_aff_project_domain_on_params(pma);
9116 return isl_multi_union_pw_aff_pw_multi_aff_on_domain(dom, pma);
9119 /* Apply "pma" to "mupa". The space of "mupa" needs to be compatible
9120 * with the domain of "pma".
9121 * The result is defined over the shared domain of the elements of "mupa"
9123 __isl_give isl_multi_union_pw_aff *isl_multi_union_pw_aff_apply_pw_multi_aff(
9124 __isl_take isl_multi_union_pw_aff *mupa,
9125 __isl_take isl_pw_multi_aff *pma)
9127 isl_space *space1, *space2;
9128 isl_multi_union_pw_aff *res;
9129 isl_bool equal;
9130 int i;
9131 isl_size n_in, n_out;
9133 mupa = isl_multi_union_pw_aff_align_params(mupa,
9134 isl_pw_multi_aff_get_space(pma));
9135 pma = isl_pw_multi_aff_align_params(pma,
9136 isl_multi_union_pw_aff_get_space(mupa));
9137 if (!mupa || !pma)
9138 goto error;
9140 space1 = isl_multi_union_pw_aff_get_space(mupa);
9141 space2 = isl_pw_multi_aff_get_domain_space(pma);
9142 equal = isl_space_is_equal(space1, space2);
9143 isl_space_free(space1);
9144 isl_space_free(space2);
9145 if (equal < 0)
9146 goto error;
9147 if (!equal)
9148 isl_die(isl_pw_multi_aff_get_ctx(pma), isl_error_invalid,
9149 "spaces don't match", goto error);
9150 n_in = isl_pw_multi_aff_dim(pma, isl_dim_in);
9151 n_out = isl_pw_multi_aff_dim(pma, isl_dim_out);
9152 if (n_in < 0 || n_out < 0)
9153 goto error;
9154 if (n_in == 0)
9155 return mupa_apply_pw_multi_aff_0D(mupa, pma);
9157 space1 = isl_space_range(isl_pw_multi_aff_get_space(pma));
9158 res = isl_multi_union_pw_aff_alloc(space1);
9160 for (i = 0; i < n_out; ++i) {
9161 isl_pw_aff *pa;
9162 isl_union_pw_aff *upa;
9164 pa = isl_pw_multi_aff_get_pw_aff(pma, i);
9165 upa = isl_multi_union_pw_aff_apply_pw_aff(
9166 isl_multi_union_pw_aff_copy(mupa), pa);
9167 res = isl_multi_union_pw_aff_set_union_pw_aff(res, i, upa);
9170 isl_pw_multi_aff_free(pma);
9171 isl_multi_union_pw_aff_free(mupa);
9172 return res;
9173 error:
9174 isl_multi_union_pw_aff_free(mupa);
9175 isl_pw_multi_aff_free(pma);
9176 return NULL;
9179 /* Replace the explicit domain of "mupa" by its preimage under "upma".
9180 * If the explicit domain only keeps track of constraints on the parameters,
9181 * then only update those constraints.
9183 static __isl_give isl_multi_union_pw_aff *preimage_explicit_domain(
9184 __isl_take isl_multi_union_pw_aff *mupa,
9185 __isl_keep isl_union_pw_multi_aff *upma)
9187 isl_bool is_params;
9189 if (isl_multi_union_pw_aff_check_has_explicit_domain(mupa) < 0)
9190 return isl_multi_union_pw_aff_free(mupa);
9192 mupa = isl_multi_union_pw_aff_cow(mupa);
9193 if (!mupa)
9194 return NULL;
9196 is_params = isl_union_set_is_params(mupa->u.dom);
9197 if (is_params < 0)
9198 return isl_multi_union_pw_aff_free(mupa);
9200 upma = isl_union_pw_multi_aff_copy(upma);
9201 if (is_params)
9202 mupa->u.dom = isl_union_set_intersect_params(mupa->u.dom,
9203 isl_union_set_params(isl_union_pw_multi_aff_domain(upma)));
9204 else
9205 mupa->u.dom = isl_union_set_preimage_union_pw_multi_aff(
9206 mupa->u.dom, upma);
9207 if (!mupa->u.dom)
9208 return isl_multi_union_pw_aff_free(mupa);
9209 return mupa;
9212 /* Compute the pullback of "mupa" by the function represented by "upma".
9213 * In other words, plug in "upma" in "mupa". The result contains
9214 * expressions defined over the domain space of "upma".
9216 * Run over all elements of "mupa" and plug in "upma" in each of them.
9218 * If "mupa" has an explicit domain, then it is this domain
9219 * that needs to undergo a pullback instead, i.e., a preimage.
9221 __isl_give isl_multi_union_pw_aff *
9222 isl_multi_union_pw_aff_pullback_union_pw_multi_aff(
9223 __isl_take isl_multi_union_pw_aff *mupa,
9224 __isl_take isl_union_pw_multi_aff *upma)
9226 int i;
9227 isl_size n;
9229 mupa = isl_multi_union_pw_aff_align_params(mupa,
9230 isl_union_pw_multi_aff_get_space(upma));
9231 upma = isl_union_pw_multi_aff_align_params(upma,
9232 isl_multi_union_pw_aff_get_space(mupa));
9233 mupa = isl_multi_union_pw_aff_cow(mupa);
9234 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9235 if (n < 0 || !upma)
9236 goto error;
9238 for (i = 0; i < n; ++i) {
9239 isl_union_pw_aff *upa;
9241 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9242 upa = isl_union_pw_aff_pullback_union_pw_multi_aff(upa,
9243 isl_union_pw_multi_aff_copy(upma));
9244 mupa = isl_multi_union_pw_aff_set_union_pw_aff(mupa, i, upa);
9247 if (isl_multi_union_pw_aff_has_explicit_domain(mupa))
9248 mupa = preimage_explicit_domain(mupa, upma);
9250 isl_union_pw_multi_aff_free(upma);
9251 return mupa;
9252 error:
9253 isl_multi_union_pw_aff_free(mupa);
9254 isl_union_pw_multi_aff_free(upma);
9255 return NULL;
9258 /* Extract the sequence of elements in "mupa" with domain space "space"
9259 * (ignoring parameters).
9261 * For the elements of "mupa" that are not defined on the specified space,
9262 * the corresponding element in the result is empty.
9264 __isl_give isl_multi_pw_aff *isl_multi_union_pw_aff_extract_multi_pw_aff(
9265 __isl_keep isl_multi_union_pw_aff *mupa, __isl_take isl_space *space)
9267 int i;
9268 isl_size n;
9269 isl_space *space_mpa;
9270 isl_multi_pw_aff *mpa;
9272 n = isl_multi_union_pw_aff_dim(mupa, isl_dim_set);
9273 if (n < 0 || !space)
9274 goto error;
9276 space_mpa = isl_multi_union_pw_aff_get_space(mupa);
9277 space = isl_space_replace_params(space, space_mpa);
9278 space_mpa = isl_space_map_from_domain_and_range(isl_space_copy(space),
9279 space_mpa);
9280 mpa = isl_multi_pw_aff_alloc(space_mpa);
9282 space = isl_space_from_domain(space);
9283 space = isl_space_add_dims(space, isl_dim_out, 1);
9284 for (i = 0; i < n; ++i) {
9285 isl_union_pw_aff *upa;
9286 isl_pw_aff *pa;
9288 upa = isl_multi_union_pw_aff_get_union_pw_aff(mupa, i);
9289 pa = isl_union_pw_aff_extract_pw_aff(upa,
9290 isl_space_copy(space));
9291 mpa = isl_multi_pw_aff_set_pw_aff(mpa, i, pa);
9292 isl_union_pw_aff_free(upa);
9295 isl_space_free(space);
9296 return mpa;
9297 error:
9298 isl_space_free(space);
9299 return NULL;
9302 /* Data structure that specifies how isl_union_pw_multi_aff_un_op
9303 * should modify the base expressions in the input.
9305 * If "filter" is not NULL, then only the base expressions that satisfy "filter"
9306 * are taken into account.
9307 * "fn" is applied to each entry in the input.
9309 struct isl_union_pw_multi_aff_un_op_control {
9310 isl_bool (*filter)(__isl_keep isl_pw_multi_aff *part);
9311 __isl_give isl_pw_multi_aff *(*fn)(__isl_take isl_pw_multi_aff *pma);
9314 /* Wrapper for isl_union_pw_multi_aff_un_op base functions (which do not take
9315 * a second argument) for use as an isl_union_pw_multi_aff_transform
9316 * base function (which does take a second argument).
9317 * Simply call control->fn without the second argument.
9319 static __isl_give isl_pw_multi_aff *isl_union_pw_multi_aff_un_op_drop_user(
9320 __isl_take isl_pw_multi_aff *pma, void *user)
9322 struct isl_union_pw_multi_aff_un_op_control *control = user;
9324 return control->fn(pma);
9327 /* Construct an isl_union_pw_multi_aff that is obtained by
9328 * modifying "upma" according to "control".
9330 * isl_union_pw_multi_aff_transform performs essentially
9331 * the same operation, but takes a callback function
9332 * of a different form (with an extra argument).
9333 * Call isl_union_pw_multi_aff_transform with a wrapper
9334 * that removes this extra argument.
9336 static __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_un_op(
9337 __isl_take isl_union_pw_multi_aff *upma,
9338 struct isl_union_pw_multi_aff_un_op_control *control)
9340 struct isl_union_pw_multi_aff_transform_control t_control = {
9341 .filter = control->filter,
9342 .fn = &isl_union_pw_multi_aff_un_op_drop_user,
9343 .fn_user = control,
9346 return isl_union_pw_multi_aff_transform(upma, &t_control);
9349 /* For each function in "upma" of the form A -> [B -> C],
9350 * extract the function A -> B and collect the results.
9352 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_domain(
9353 __isl_take isl_union_pw_multi_aff *upma)
9355 struct isl_union_pw_multi_aff_un_op_control control = {
9356 .filter = &isl_pw_multi_aff_range_is_wrapping,
9357 .fn = &isl_pw_multi_aff_range_factor_domain,
9359 return isl_union_pw_multi_aff_un_op(upma, &control);
9362 /* For each function in "upma" of the form A -> [B -> C],
9363 * extract the function A -> C and collect the results.
9365 __isl_give isl_union_pw_multi_aff *isl_union_pw_multi_aff_range_factor_range(
9366 __isl_take isl_union_pw_multi_aff *upma)
9368 struct isl_union_pw_multi_aff_un_op_control control = {
9369 .filter = &isl_pw_multi_aff_range_is_wrapping,
9370 .fn = &isl_pw_multi_aff_range_factor_range,
9372 return isl_union_pw_multi_aff_un_op(upma, &control);
9375 /* Evaluate the affine function "aff" in the void point "pnt".
9376 * In particular, return the value NaN.
9378 static __isl_give isl_val *eval_void(__isl_take isl_aff *aff,
9379 __isl_take isl_point *pnt)
9381 isl_ctx *ctx;
9383 ctx = isl_point_get_ctx(pnt);
9384 isl_aff_free(aff);
9385 isl_point_free(pnt);
9386 return isl_val_nan(ctx);
9389 /* Evaluate the affine expression "aff"
9390 * in the coordinates (with denominator) "pnt".
9392 static __isl_give isl_val *eval(__isl_keep isl_vec *aff,
9393 __isl_keep isl_vec *pnt)
9395 isl_int n, d;
9396 isl_ctx *ctx;
9397 isl_val *v;
9399 if (!aff || !pnt)
9400 return NULL;
9402 ctx = isl_vec_get_ctx(aff);
9403 isl_int_init(n);
9404 isl_int_init(d);
9405 isl_seq_inner_product(aff->el + 1, pnt->el, pnt->size, &n);
9406 isl_int_mul(d, aff->el[0], pnt->el[0]);
9407 v = isl_val_rat_from_isl_int(ctx, n, d);
9408 v = isl_val_normalize(v);
9409 isl_int_clear(n);
9410 isl_int_clear(d);
9412 return v;
9415 /* Check that the domain space of "aff" is equal to "space".
9417 static isl_stat isl_aff_check_has_domain_space(__isl_keep isl_aff *aff,
9418 __isl_keep isl_space *space)
9420 isl_bool ok;
9422 ok = isl_space_is_equal(isl_aff_peek_domain_space(aff), space);
9423 if (ok < 0)
9424 return isl_stat_error;
9425 if (!ok)
9426 isl_die(isl_aff_get_ctx(aff), isl_error_invalid,
9427 "incompatible spaces", return isl_stat_error);
9428 return isl_stat_ok;
9431 /* Evaluate the affine function "aff" in "pnt".
9433 __isl_give isl_val *isl_aff_eval(__isl_take isl_aff *aff,
9434 __isl_take isl_point *pnt)
9436 isl_bool is_void;
9437 isl_val *v;
9438 isl_local_space *ls;
9440 if (isl_aff_check_has_domain_space(aff, isl_point_peek_space(pnt)) < 0)
9441 goto error;
9442 is_void = isl_point_is_void(pnt);
9443 if (is_void < 0)
9444 goto error;
9445 if (is_void)
9446 return eval_void(aff, pnt);
9448 ls = isl_aff_get_domain_local_space(aff);
9449 pnt = isl_local_space_lift_point(ls, pnt);
9451 v = eval(aff->v, isl_point_peek_vec(pnt));
9453 isl_aff_free(aff);
9454 isl_point_free(pnt);
9456 return v;
9457 error:
9458 isl_aff_free(aff);
9459 isl_point_free(pnt);
9460 return NULL;